1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
29 #include "coretypes.h"
33 #include "pointer-set.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "tree-inline.h"
43 #include "tree-iterator.h"
44 #include "type-utils.h"
47 /* The type of functions taking a tree, and some additional data, and
49 typedef int (*tree_fn_t
) (tree
, void*);
51 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
52 instantiations have been deferred, either because their definitions
53 were not yet available, or because we were putting off doing the work. */
54 struct GTY ((chain_next ("%h.next"))) pending_template
{
55 struct pending_template
*next
;
56 struct tinst_level
*tinst
;
59 static GTY(()) struct pending_template
*pending_templates
;
60 static GTY(()) struct pending_template
*last_pending_template
;
62 int processing_template_parmlist
;
63 static int template_header_count
;
65 static GTY(()) tree saved_trees
;
66 static vec
<int> inline_parm_levels
;
68 static GTY(()) struct tinst_level
*current_tinst_level
;
70 static GTY(()) tree saved_access_scope
;
72 /* Live only within one (recursive) call to tsubst_expr. We use
73 this to pass the statement expression node from the STMT_EXPR
74 to the EXPR_STMT that is its result. */
75 static tree cur_stmt_expr
;
77 /* A map from local variable declarations in the body of the template
78 presently being instantiated to the corresponding instantiated
80 static struct pointer_map_t
*local_specializations
;
82 /* True if we've recursed into fn_type_unification too many times. */
83 static bool excessive_deduction_depth
;
85 typedef struct GTY(()) spec_entry
92 static GTY ((param_is (spec_entry
)))
93 htab_t decl_specializations
;
95 static GTY ((param_is (spec_entry
)))
96 htab_t type_specializations
;
98 /* Contains canonical template parameter types. The vector is indexed by
99 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
100 TREE_LIST, whose TREE_VALUEs contain the canonical template
101 parameters of various types and levels. */
102 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
104 #define UNIFY_ALLOW_NONE 0
105 #define UNIFY_ALLOW_MORE_CV_QUAL 1
106 #define UNIFY_ALLOW_LESS_CV_QUAL 2
107 #define UNIFY_ALLOW_DERIVED 4
108 #define UNIFY_ALLOW_INTEGER 8
109 #define UNIFY_ALLOW_OUTER_LEVEL 16
110 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
111 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
113 enum template_base_result
{
115 tbr_ambiguous_baseclass
,
119 static void push_access_scope (tree
);
120 static void pop_access_scope (tree
);
121 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
122 unification_kind_t
, int,
124 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
125 unification_kind_t
, int, bool, bool);
126 static int unify (tree
, tree
, tree
, tree
, int, bool);
127 static void add_pending_template (tree
);
128 static tree
reopen_tinst_level (struct tinst_level
*);
129 static tree
tsubst_initializer_list (tree
, tree
);
130 static tree
get_class_bindings (tree
, tree
, tree
, tree
);
131 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
133 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
135 static void tsubst_enum (tree
, tree
, tree
);
136 static tree
add_to_template_args (tree
, tree
);
137 static tree
add_outermost_template_args (tree
, tree
);
138 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
139 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
141 static int type_unification_real (tree
, tree
, tree
, const tree
*,
142 unsigned int, int, unification_kind_t
, int,
143 vec
<deferred_access_check
, va_gc
> **,
145 static void note_template_header (int);
146 static tree
convert_nontype_argument_function (tree
, tree
);
147 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
148 static tree
convert_template_argument (tree
, tree
, tree
,
149 tsubst_flags_t
, int, tree
);
150 static int for_each_template_parm (tree
, tree_fn_t
, void*,
151 struct pointer_set_t
*, bool);
152 static tree
expand_template_argument_pack (tree
);
153 static tree
build_template_parm_index (int, int, int, tree
, tree
);
154 static bool inline_needs_template_parms (tree
);
155 static void push_inline_template_parms_recursive (tree
, int);
156 static tree
retrieve_local_specialization (tree
);
157 static void register_local_specialization (tree
, tree
);
158 static hashval_t
hash_specialization (const void *p
);
159 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
160 static int mark_template_parm (tree
, void *);
161 static int template_parm_this_level_p (tree
, void *);
162 static tree
tsubst_friend_function (tree
, tree
);
163 static tree
tsubst_friend_class (tree
, tree
);
164 static int can_complete_type_without_circularity (tree
);
165 static tree
get_bindings (tree
, tree
, tree
, bool);
166 static int template_decl_level (tree
);
167 static int check_cv_quals_for_unify (int, tree
, tree
);
168 static void template_parm_level_and_index (tree
, int*, int*);
169 static int unify_pack_expansion (tree
, tree
, tree
,
170 tree
, unification_kind_t
, bool, bool);
171 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
172 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
173 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
174 static void regenerate_decl_from_template (tree
, tree
);
175 static tree
most_specialized_class (tree
, tree
, tsubst_flags_t
);
176 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
177 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
178 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
179 static bool check_specialization_scope (void);
180 static tree
process_partial_specialization (tree
);
181 static void set_current_access_from_decl (tree
);
182 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
184 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
185 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
187 static bool template_template_parm_bindings_ok_p (tree
, tree
);
188 static int template_args_equal (tree
, tree
);
189 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
190 static tree
for_each_template_parm_r (tree
*, int *, void *);
191 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
192 static void copy_default_args_to_explicit_spec (tree
);
193 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
194 static bool dependent_template_arg_p (tree
);
195 static bool any_template_arguments_need_structural_equality_p (tree
);
196 static bool dependent_type_p_r (tree
);
197 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
, bool);
198 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
199 static tree
tsubst_pack_expansion (tree
, tree
, tsubst_flags_t
, tree
);
200 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
201 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
202 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
204 static tree
listify (tree
);
205 static tree
listify_autos (tree
, tree
);
206 static tree
template_parm_to_arg (tree t
);
207 static tree
current_template_args (void);
208 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
209 static tree
instantiate_alias_template (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, VAR_DECL for static member variable, or TYPE_DECL for
214 alias template (needed by instantiate_decl). */
217 push_access_scope (tree t
)
219 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
220 || TREE_CODE (t
) == TYPE_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 (OVERLOAD_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 inline_parm_levels
.safe_push (levels
);
479 /* Undo the effects of maybe_begin_member_template_processing. */
482 maybe_end_member_template_processing (void)
487 if (inline_parm_levels
.length () == 0)
490 last
= inline_parm_levels
.pop ();
491 for (i
= 0; i
< last
; ++i
)
493 --processing_template_decl
;
494 current_template_parms
= TREE_CHAIN (current_template_parms
);
499 /* Return a new template argument vector which contains all of ARGS,
500 but has as its innermost set of arguments the EXTRA_ARGS. */
503 add_to_template_args (tree args
, tree extra_args
)
510 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
513 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
514 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
516 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
517 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
519 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
520 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
525 /* Like add_to_template_args, but only the outermost ARGS are added to
526 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
527 (EXTRA_ARGS) levels are added. This function is used to combine
528 the template arguments from a partial instantiation with the
529 template arguments used to attain the full instantiation from the
530 partial instantiation. */
533 add_outermost_template_args (tree args
, tree extra_args
)
537 /* If there are more levels of EXTRA_ARGS than there are ARGS,
538 something very fishy is going on. */
539 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
541 /* If *all* the new arguments will be the EXTRA_ARGS, just return
543 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
546 /* For the moment, we make ARGS look like it contains fewer levels. */
547 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
549 new_args
= add_to_template_args (args
, extra_args
);
551 /* Now, we restore ARGS to its full dimensions. */
552 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
557 /* Return the N levels of innermost template arguments from the ARGS. */
560 get_innermost_template_args (tree args
, int n
)
568 /* If N is 1, just return the innermost set of template arguments. */
570 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
572 /* If we're not removing anything, just return the arguments we were
574 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
575 gcc_assert (extra_levels
>= 0);
576 if (extra_levels
== 0)
579 /* Make a new set of arguments, not containing the outer arguments. */
580 new_args
= make_tree_vec (n
);
581 for (i
= 1; i
<= n
; ++i
)
582 SET_TMPL_ARGS_LEVEL (new_args
, i
,
583 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
588 /* The inverse of get_innermost_template_args: Return all but the innermost
589 EXTRA_LEVELS levels of template arguments from the ARGS. */
592 strip_innermost_template_args (tree args
, int extra_levels
)
595 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
600 /* If N is 1, just return the outermost set of template arguments. */
602 return TMPL_ARGS_LEVEL (args
, 1);
604 /* If we're not removing anything, just return the arguments we were
606 gcc_assert (extra_levels
>= 0);
607 if (extra_levels
== 0)
610 /* Make a new set of arguments, not containing the inner arguments. */
611 new_args
= make_tree_vec (n
);
612 for (i
= 1; i
<= n
; ++i
)
613 SET_TMPL_ARGS_LEVEL (new_args
, i
,
614 TMPL_ARGS_LEVEL (args
, i
));
619 /* We've got a template header coming up; push to a new level for storing
623 begin_template_parm_list (void)
625 /* We use a non-tag-transparent scope here, which causes pushtag to
626 put tags in this scope, rather than in the enclosing class or
627 namespace scope. This is the right thing, since we want
628 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
629 global template class, push_template_decl handles putting the
630 TEMPLATE_DECL into top-level scope. For a nested template class,
633 template <class T> struct S1 {
634 template <class T> struct S2 {};
637 pushtag contains special code to call pushdecl_with_scope on the
638 TEMPLATE_DECL for S2. */
639 begin_scope (sk_template_parms
, NULL
);
640 ++processing_template_decl
;
641 ++processing_template_parmlist
;
642 note_template_header (0);
645 /* This routine is called when a specialization is declared. If it is
646 invalid to declare a specialization here, an error is reported and
647 false is returned, otherwise this routine will return true. */
650 check_specialization_scope (void)
652 tree scope
= current_scope ();
656 An explicit specialization shall be declared in the namespace of
657 which the template is a member, or, for member templates, in the
658 namespace of which the enclosing class or enclosing class
659 template is a member. An explicit specialization of a member
660 function, member class or static data member of a class template
661 shall be declared in the namespace of which the class template
663 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
665 error ("explicit specialization in non-namespace scope %qD", scope
);
671 In an explicit specialization declaration for a member of a class
672 template or a member template that appears in namespace scope,
673 the member template and some of its enclosing class templates may
674 remain unspecialized, except that the declaration shall not
675 explicitly specialize a class member template if its enclosing
676 class templates are not explicitly specialized as well. */
677 if (current_template_parms
)
679 error ("enclosing class templates are not explicitly specialized");
686 /* We've just seen template <>. */
689 begin_specialization (void)
691 begin_scope (sk_template_spec
, NULL
);
692 note_template_header (1);
693 return check_specialization_scope ();
696 /* Called at then end of processing a declaration preceded by
700 end_specialization (void)
703 reset_specialization ();
706 /* Any template <>'s that we have seen thus far are not referring to a
707 function specialization. */
710 reset_specialization (void)
712 processing_specialization
= 0;
713 template_header_count
= 0;
716 /* We've just seen a template header. If SPECIALIZATION is nonzero,
717 it was of the form template <>. */
720 note_template_header (int specialization
)
722 processing_specialization
= specialization
;
723 template_header_count
++;
726 /* We're beginning an explicit instantiation. */
729 begin_explicit_instantiation (void)
731 gcc_assert (!processing_explicit_instantiation
);
732 processing_explicit_instantiation
= true;
737 end_explicit_instantiation (void)
739 gcc_assert (processing_explicit_instantiation
);
740 processing_explicit_instantiation
= false;
743 /* An explicit specialization or partial specialization of TMPL is being
744 declared. Check that the namespace in which the specialization is
745 occurring is permissible. Returns false iff it is invalid to
746 specialize TMPL in the current namespace. */
749 check_specialization_namespace (tree tmpl
)
751 tree tpl_ns
= decl_namespace_context (tmpl
);
755 An explicit specialization shall be declared in the namespace of
756 which the template is a member, or, for member templates, in the
757 namespace of which the enclosing class or enclosing class
758 template is a member. An explicit specialization of a member
759 function, member class or static data member of a class template
760 shall be declared in the namespace of which the class template is
762 if (current_scope() != DECL_CONTEXT (tmpl
)
763 && !at_namespace_scope_p ())
765 error ("specialization of %qD must appear at namespace scope", tmpl
);
768 if (is_associated_namespace (current_namespace
, tpl_ns
))
769 /* Same or super-using namespace. */
773 permerror (input_location
, "specialization of %qD in different namespace", tmpl
);
774 permerror (input_location
, " from definition of %q+#D", tmpl
);
779 /* SPEC is an explicit instantiation. Check that it is valid to
780 perform this explicit instantiation in the current namespace. */
783 check_explicit_instantiation_namespace (tree spec
)
787 /* DR 275: An explicit instantiation shall appear in an enclosing
788 namespace of its template. */
789 ns
= decl_namespace_context (spec
);
790 if (!is_ancestor (current_namespace
, ns
))
791 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
792 "(which does not enclose namespace %qD)",
793 spec
, current_namespace
, ns
);
796 /* The TYPE is being declared. If it is a template type, that means it
797 is a partial specialization. Do appropriate error-checking. */
800 maybe_process_partial_specialization (tree type
)
804 if (type
== error_mark_node
)
805 return error_mark_node
;
807 /* A lambda that appears in specialization context is not itself a
809 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
812 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
814 error ("name of class shadows template template parameter %qD",
816 return error_mark_node
;
819 context
= TYPE_CONTEXT (type
);
821 if (TYPE_ALIAS_P (type
))
823 if (TYPE_TEMPLATE_INFO (type
)
824 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type
)))
825 error ("specialization of alias template %qD",
826 TYPE_TI_TEMPLATE (type
));
828 error ("explicit specialization of non-template %qT", type
);
829 return error_mark_node
;
831 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
833 /* This is for ordinary explicit specialization and partial
834 specialization of a template class such as:
836 template <> class C<int>;
840 template <class T> class C<T*>;
842 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
844 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
845 && !COMPLETE_TYPE_P (type
))
847 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
));
848 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
849 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
850 if (processing_template_decl
)
852 if (push_template_decl (TYPE_MAIN_DECL (type
))
854 return error_mark_node
;
857 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
858 error ("specialization of %qT after instantiation", type
);
859 else if (errorcount
&& !processing_specialization
860 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
861 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
862 /* Trying to define a specialization either without a template<> header
863 or in an inappropriate place. We've already given an error, so just
864 bail now so we don't actually define the specialization. */
865 return error_mark_node
;
867 else if (CLASS_TYPE_P (type
)
868 && !CLASSTYPE_USE_TEMPLATE (type
)
869 && CLASSTYPE_TEMPLATE_INFO (type
)
870 && context
&& CLASS_TYPE_P (context
)
871 && CLASSTYPE_TEMPLATE_INFO (context
))
873 /* This is for an explicit specialization of member class
874 template according to [temp.expl.spec/18]:
876 template <> template <class U> class C<int>::D;
878 The context `C<int>' must be an implicit instantiation.
879 Otherwise this is just a member class template declared
882 template <> class C<int> { template <class U> class D; };
883 template <> template <class U> class C<int>::D;
885 In the first case, `C<int>::D' is a specialization of `C<T>::D'
886 while in the second case, `C<int>::D' is a primary template
887 and `C<T>::D' may not exist. */
889 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
890 && !COMPLETE_TYPE_P (type
))
893 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
895 if (current_namespace
896 != decl_namespace_context (tmpl
))
898 permerror (input_location
, "specializing %q#T in different namespace", type
);
899 permerror (input_location
, " from definition of %q+#D", tmpl
);
902 /* Check for invalid specialization after instantiation:
904 template <> template <> class C<int>::D<int>;
905 template <> template <class U> class C<int>::D; */
907 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
908 t
; t
= TREE_CHAIN (t
))
910 tree inst
= TREE_VALUE (t
);
911 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
))
913 /* We already have a full specialization of this partial
914 instantiation. Reassign it to the new member
915 specialization template. */
920 elt
.tmpl
= most_general_template (tmpl
);
921 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
924 htab_remove_elt (type_specializations
, &elt
);
927 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
929 slot
= htab_find_slot (type_specializations
, &elt
, INSERT
);
930 entry
= ggc_alloc_spec_entry ();
934 else if (COMPLETE_OR_OPEN_TYPE_P (inst
))
935 /* But if we've had an implicit instantiation, that's a
936 problem ([temp.expl.spec]/6). */
937 error ("specialization %qT after instantiation %qT",
941 /* Mark TYPE as a specialization. And as a result, we only
942 have one level of template argument for the innermost
944 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
945 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
946 CLASSTYPE_TI_ARGS (type
)
947 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
950 else if (processing_specialization
)
952 /* Someday C++0x may allow for enum template specialization. */
953 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
954 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
955 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
956 "of %qD not allowed by ISO C++", type
);
959 error ("explicit specialization of non-template %qT", type
);
960 return error_mark_node
;
967 /* Returns nonzero if we can optimize the retrieval of specializations
968 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
969 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
972 optimize_specialization_lookup_p (tree tmpl
)
974 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
975 && DECL_CLASS_SCOPE_P (tmpl
)
976 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
978 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
979 /* The optimized lookup depends on the fact that the
980 template arguments for the member function template apply
981 purely to the containing class, which is not true if the
982 containing class is an explicit or partial
984 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
985 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
986 && !DECL_CONV_FN_P (tmpl
)
987 /* It is possible to have a template that is not a member
988 template and is not a member of a template class:
990 template <typename T>
991 struct S { friend A::f(); };
993 Here, the friend function is a template, but the context does
994 not have template information. The optimized lookup relies
995 on having ARGS be the template arguments for both the class
996 and the function template. */
997 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1000 /* Retrieve the specialization (in the sense of [temp.spec] - a
1001 specialization is either an instantiation or an explicit
1002 specialization) of TMPL for the given template ARGS. If there is
1003 no such specialization, return NULL_TREE. The ARGS are a vector of
1004 arguments, or a vector of vectors of arguments, in the case of
1005 templates with more than one level of parameters.
1007 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1008 then we search for a partial specialization matching ARGS. This
1009 parameter is ignored if TMPL is not a class template.
1011 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1012 result is a NONTYPE_ARGUMENT_PACK. */
1015 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1017 if (tmpl
== NULL_TREE
)
1020 if (args
== error_mark_node
)
1023 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1024 || TREE_CODE (tmpl
) == FIELD_DECL
);
1026 /* There should be as many levels of arguments as there are
1027 levels of parameters. */
1028 gcc_assert (TMPL_ARGS_DEPTH (args
)
1029 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1030 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1031 : template_class_depth (DECL_CONTEXT (tmpl
))));
1033 if (optimize_specialization_lookup_p (tmpl
))
1035 tree class_template
;
1036 tree class_specialization
;
1037 vec
<tree
, va_gc
> *methods
;
1041 /* The template arguments actually apply to the containing
1042 class. Find the class specialization with those
1044 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1045 class_specialization
1046 = retrieve_specialization (class_template
, args
, 0);
1047 if (!class_specialization
)
1049 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1050 for the specialization. */
1051 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1054 /* Iterate through the methods with the indicated name, looking
1055 for the one that has an instance of TMPL. */
1056 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1057 for (fns
= (*methods
)[idx
]; fns
; fns
= OVL_NEXT (fns
))
1059 tree fn
= OVL_CURRENT (fns
);
1060 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1061 /* using-declarations can add base methods to the method vec,
1062 and we don't want those here. */
1063 && DECL_CONTEXT (fn
) == class_specialization
)
1072 htab_t specializations
;
1076 elt
.spec
= NULL_TREE
;
1078 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1079 specializations
= type_specializations
;
1081 specializations
= decl_specializations
;
1084 hash
= hash_specialization (&elt
);
1085 found
= (spec_entry
*) htab_find_with_hash (specializations
, &elt
, hash
);
1093 /* Like retrieve_specialization, but for local declarations. */
1096 retrieve_local_specialization (tree tmpl
)
1100 if (local_specializations
== NULL
)
1103 slot
= pointer_map_contains (local_specializations
, tmpl
);
1104 return slot
? (tree
) *slot
: NULL_TREE
;
1107 /* Returns nonzero iff DECL is a specialization of TMPL. */
1110 is_specialization_of (tree decl
, tree tmpl
)
1114 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1118 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1124 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1126 for (t
= TREE_TYPE (decl
);
1128 t
= CLASSTYPE_USE_TEMPLATE (t
)
1129 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1130 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1137 /* Returns nonzero iff DECL is a specialization of friend declaration
1138 FRIEND_DECL according to [temp.friend]. */
1141 is_specialization_of_friend (tree decl
, tree friend_decl
)
1143 bool need_template
= true;
1146 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1147 || TREE_CODE (decl
) == TYPE_DECL
);
1149 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1150 of a template class, we want to check if DECL is a specialization
1152 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1153 && DECL_TEMPLATE_INFO (friend_decl
)
1154 && !DECL_USE_TEMPLATE (friend_decl
))
1156 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1157 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1158 need_template
= false;
1160 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1161 && !PRIMARY_TEMPLATE_P (friend_decl
))
1162 need_template
= false;
1164 /* There is nothing to do if this is not a template friend. */
1165 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1168 if (is_specialization_of (decl
, friend_decl
))
1172 A member of a class template may be declared to be a friend of a
1173 non-template class. In this case, the corresponding member of
1174 every specialization of the class template is a friend of the
1175 class granting friendship.
1177 For example, given a template friend declaration
1179 template <class T> friend void A<T>::f();
1181 the member function below is considered a friend
1183 template <> struct A<int> {
1187 For this type of template friend, TEMPLATE_DEPTH below will be
1188 nonzero. To determine if DECL is a friend of FRIEND, we first
1189 check if the enclosing class is a specialization of another. */
1191 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1193 && DECL_CLASS_SCOPE_P (decl
)
1194 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1195 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1197 /* Next, we check the members themselves. In order to handle
1198 a few tricky cases, such as when FRIEND_DECL's are
1200 template <class T> friend void A<T>::g(T t);
1201 template <class T> template <T t> friend void A<T>::h();
1205 void A<int>::g(int);
1206 template <int> void A<int>::h();
1208 we need to figure out ARGS, the template arguments from
1209 the context of DECL. This is required for template substitution
1210 of `T' in the function parameter of `g' and template parameter
1211 of `h' in the above examples. Here ARGS corresponds to `int'. */
1213 tree context
= DECL_CONTEXT (decl
);
1214 tree args
= NULL_TREE
;
1215 int current_depth
= 0;
1217 while (current_depth
< template_depth
)
1219 if (CLASSTYPE_TEMPLATE_INFO (context
))
1221 if (current_depth
== 0)
1222 args
= TYPE_TI_ARGS (context
);
1224 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1227 context
= TYPE_CONTEXT (context
);
1230 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1235 tree friend_args_type
;
1236 tree decl_args_type
;
1238 /* Make sure that both DECL and FRIEND_DECL are templates or
1240 is_template
= DECL_TEMPLATE_INFO (decl
)
1241 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1242 if (need_template
^ is_template
)
1244 else if (is_template
)
1246 /* If both are templates, check template parameter list. */
1248 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1250 if (!comp_template_parms
1251 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1255 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1258 decl_type
= TREE_TYPE (decl
);
1260 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1261 tf_none
, NULL_TREE
);
1262 if (friend_type
== error_mark_node
)
1265 /* Check if return types match. */
1266 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1269 /* Check if function parameter types match, ignoring the
1270 `this' parameter. */
1271 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1272 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1273 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1274 friend_args_type
= TREE_CHAIN (friend_args_type
);
1275 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1276 decl_args_type
= TREE_CHAIN (decl_args_type
);
1278 return compparms (decl_args_type
, friend_args_type
);
1282 /* DECL is a TYPE_DECL */
1284 tree decl_type
= TREE_TYPE (decl
);
1286 /* Make sure that both DECL and FRIEND_DECL are templates or
1289 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1290 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1292 if (need_template
^ is_template
)
1294 else if (is_template
)
1297 /* If both are templates, check the name of the two
1298 TEMPLATE_DECL's first because is_friend didn't. */
1299 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1300 != DECL_NAME (friend_decl
))
1303 /* Now check template parameter list. */
1305 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1307 return comp_template_parms
1308 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1312 return (DECL_NAME (decl
)
1313 == DECL_NAME (friend_decl
));
1319 /* Register the specialization SPEC as a specialization of TMPL with
1320 the indicated ARGS. IS_FRIEND indicates whether the specialization
1321 is actually just a friend declaration. Returns SPEC, or an
1322 equivalent prior declaration, if available.
1324 We also store instantiations of field packs in the hash table, even
1325 though they are not themselves templates, to make lookup easier. */
1328 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1335 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1336 || (TREE_CODE (tmpl
) == FIELD_DECL
1337 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1339 if (TREE_CODE (spec
) == FUNCTION_DECL
1340 && uses_template_parms (DECL_TI_ARGS (spec
)))
1341 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1342 register it; we want the corresponding TEMPLATE_DECL instead.
1343 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1344 the more obvious `uses_template_parms (spec)' to avoid problems
1345 with default function arguments. In particular, given
1346 something like this:
1348 template <class T> void f(T t1, T t = T())
1350 the default argument expression is not substituted for in an
1351 instantiation unless and until it is actually needed. */
1354 if (optimize_specialization_lookup_p (tmpl
))
1355 /* We don't put these specializations in the hash table, but we might
1356 want to give an error about a mismatch. */
1357 fn
= retrieve_specialization (tmpl
, args
, 0);
1365 hash
= hash_specialization (&elt
);
1368 htab_find_slot_with_hash (decl_specializations
, &elt
, hash
, INSERT
);
1370 fn
= ((spec_entry
*) *slot
)->spec
;
1375 /* We can sometimes try to re-register a specialization that we've
1376 already got. In particular, regenerate_decl_from_template calls
1377 duplicate_decls which will update the specialization list. But,
1378 we'll still get called again here anyhow. It's more convenient
1379 to simply allow this than to try to prevent it. */
1382 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1384 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1386 if (DECL_ODR_USED (fn
)
1387 || DECL_EXPLICIT_INSTANTIATION (fn
))
1389 error ("specialization of %qD after instantiation",
1391 return error_mark_node
;
1396 /* This situation should occur only if the first
1397 specialization is an implicit instantiation, the
1398 second is an explicit specialization, and the
1399 implicit instantiation has not yet been used. That
1400 situation can occur if we have implicitly
1401 instantiated a member function and then specialized
1404 We can also wind up here if a friend declaration that
1405 looked like an instantiation turns out to be a
1408 template <class T> void foo(T);
1409 class S { friend void foo<>(int) };
1410 template <> void foo(int);
1412 We transform the existing DECL in place so that any
1413 pointers to it become pointers to the updated
1416 If there was a definition for the template, but not
1417 for the specialization, we want this to look as if
1418 there were no definition, and vice versa. */
1419 DECL_INITIAL (fn
) = NULL_TREE
;
1420 duplicate_decls (spec
, fn
, is_friend
);
1421 /* The call to duplicate_decls will have applied
1424 An explicit specialization of a function template
1425 is inline only if it is explicitly declared to be,
1426 and independently of whether its function template
1429 to the primary function; now copy the inline bits to
1430 the various clones. */
1431 FOR_EACH_CLONE (clone
, fn
)
1433 DECL_DECLARED_INLINE_P (clone
)
1434 = DECL_DECLARED_INLINE_P (fn
);
1435 DECL_SOURCE_LOCATION (clone
)
1436 = DECL_SOURCE_LOCATION (fn
);
1438 check_specialization_namespace (tmpl
);
1443 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1445 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1446 /* Dup decl failed, but this is a new definition. Set the
1447 line number so any errors match this new
1449 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1455 return duplicate_decls (spec
, fn
, is_friend
);
1457 /* A specialization must be declared in the same namespace as the
1458 template it is specializing. */
1459 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1460 && !check_specialization_namespace (tmpl
))
1461 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1463 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1465 spec_entry
*entry
= ggc_alloc_spec_entry ();
1466 gcc_assert (tmpl
&& args
&& spec
);
1469 if (TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1470 && PRIMARY_TEMPLATE_P (tmpl
)
1471 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1472 /* TMPL is a forward declaration of a template function; keep a list
1473 of all specializations in case we need to reassign them to a friend
1474 template later in tsubst_friend_function. */
1475 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1476 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1482 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1483 TMPL and ARGS members, ignores SPEC. */
1485 int comparing_specializations
;
1488 eq_specializations (const void *p1
, const void *p2
)
1490 const spec_entry
*e1
= (const spec_entry
*)p1
;
1491 const spec_entry
*e2
= (const spec_entry
*)p2
;
1494 ++comparing_specializations
;
1495 equal
= (e1
->tmpl
== e2
->tmpl
1496 && comp_template_args (e1
->args
, e2
->args
));
1497 --comparing_specializations
;
1502 /* Returns a hash for a template TMPL and template arguments ARGS. */
1505 hash_tmpl_and_args (tree tmpl
, tree args
)
1507 hashval_t val
= DECL_UID (tmpl
);
1508 return iterative_hash_template_arg (args
, val
);
1511 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1515 hash_specialization (const void *p
)
1517 const spec_entry
*e
= (const spec_entry
*)p
;
1518 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1521 /* Recursively calculate a hash value for a template argument ARG, for use
1522 in the hash tables of template specializations. */
1525 iterative_hash_template_arg (tree arg
, hashval_t val
)
1527 unsigned HOST_WIDE_INT i
;
1528 enum tree_code code
;
1531 if (arg
== NULL_TREE
)
1532 return iterative_hash_object (arg
, val
);
1537 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1538 /* We can get one of these when re-hashing a previous entry in the middle
1539 of substituting into a pack expansion. Just look through it. */
1540 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1542 code
= TREE_CODE (arg
);
1543 tclass
= TREE_CODE_CLASS (code
);
1545 val
= iterative_hash_object (code
, val
);
1552 case IDENTIFIER_NODE
:
1553 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1557 int i
, len
= TREE_VEC_LENGTH (arg
);
1558 for (i
= 0; i
< len
; ++i
)
1559 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1563 case TYPE_PACK_EXPANSION
:
1564 case EXPR_PACK_EXPANSION
:
1565 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1566 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1568 case TYPE_ARGUMENT_PACK
:
1569 case NONTYPE_ARGUMENT_PACK
:
1570 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1573 for (; arg
; arg
= TREE_CHAIN (arg
))
1574 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1578 for (; arg
; arg
= OVL_NEXT (arg
))
1579 val
= iterative_hash_template_arg (OVL_CURRENT (arg
), val
);
1585 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1587 val
= iterative_hash_template_arg (field
, val
);
1588 val
= iterative_hash_template_arg (value
, val
);
1594 if (!DECL_ARTIFICIAL (arg
))
1596 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1597 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1599 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1602 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1605 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1606 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1608 case TEMPLATE_PARM_INDEX
:
1609 val
= iterative_hash_template_arg
1610 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1611 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1612 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1615 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1616 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1617 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1620 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1622 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1626 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1627 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1628 val
= iterative_hash_object (code
, val
);
1629 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1632 /* A lambda can't appear in a template arg, but don't crash on
1634 gcc_assert (seen_error ());
1638 case IMPLICIT_CONV_EXPR
:
1639 case STATIC_CAST_EXPR
:
1640 case REINTERPRET_CAST_EXPR
:
1641 case CONST_CAST_EXPR
:
1642 case DYNAMIC_CAST_EXPR
:
1644 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1645 /* Now hash operands as usual. */
1655 if (TYPE_CANONICAL (arg
))
1656 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1658 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1659 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1660 /* Otherwise just compare the types during lookup. */
1663 case tcc_declaration
:
1665 return iterative_hash_expr (arg
, val
);
1668 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1670 unsigned n
= cp_tree_operand_length (arg
);
1671 for (i
= 0; i
< n
; ++i
)
1672 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1680 /* Unregister the specialization SPEC as a specialization of TMPL.
1681 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1682 if the SPEC was listed as a specialization of TMPL.
1684 Note that SPEC has been ggc_freed, so we can't look inside it. */
1687 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1692 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1693 elt
.args
= TI_ARGS (tinfo
);
1694 elt
.spec
= NULL_TREE
;
1696 entry
= (spec_entry
*) htab_find (decl_specializations
, &elt
);
1699 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1700 gcc_assert (new_spec
!= NULL_TREE
);
1701 entry
->spec
= new_spec
;
1708 /* Like register_specialization, but for local declarations. We are
1709 registering SPEC, an instantiation of TMPL. */
1712 register_local_specialization (tree spec
, tree tmpl
)
1716 slot
= pointer_map_insert (local_specializations
, tmpl
);
1720 /* TYPE is a class type. Returns true if TYPE is an explicitly
1721 specialized class. */
1724 explicit_class_specialization_p (tree type
)
1726 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1728 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1731 /* Print the list of functions at FNS, going through all the overloads
1732 for each element of the list. Alternatively, FNS can not be a
1733 TREE_LIST, in which case it will be printed together with all the
1736 MORE and *STR should respectively be FALSE and NULL when the function
1737 is called from the outside. They are used internally on recursive
1738 calls. print_candidates manages the two parameters and leaves NULL
1739 in *STR when it ends. */
1742 print_candidates_1 (tree fns
, bool more
, const char **str
)
1745 char *spaces
= NULL
;
1747 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1748 if (TREE_CODE (fn
) == TREE_LIST
)
1750 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1751 print_candidates_1 (TREE_VALUE (fn2
),
1752 TREE_CHAIN (fn2
) || more
, str
);
1756 tree cand
= OVL_CURRENT (fn
);
1759 /* Pick the prefix string. */
1760 if (!more
&& !OVL_NEXT (fns
))
1762 inform (DECL_SOURCE_LOCATION (cand
),
1763 "candidate is: %#D", cand
);
1767 *str
= _("candidates are:");
1768 spaces
= get_spaces (*str
);
1770 inform (DECL_SOURCE_LOCATION (cand
), "%s %#D", *str
, cand
);
1771 *str
= spaces
? spaces
: *str
;
1781 /* Print the list of candidate FNS in an error message. FNS can also
1782 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1785 print_candidates (tree fns
)
1787 const char *str
= NULL
;
1788 print_candidates_1 (fns
, false, &str
);
1789 gcc_assert (str
== NULL
);
1792 /* Returns the template (one of the functions given by TEMPLATE_ID)
1793 which can be specialized to match the indicated DECL with the
1794 explicit template args given in TEMPLATE_ID. The DECL may be
1795 NULL_TREE if none is available. In that case, the functions in
1796 TEMPLATE_ID are non-members.
1798 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1799 specialization of a member template.
1801 The TEMPLATE_COUNT is the number of references to qualifying
1802 template classes that appeared in the name of the function. See
1803 check_explicit_specialization for a more accurate description.
1805 TSK indicates what kind of template declaration (if any) is being
1806 declared. TSK_TEMPLATE indicates that the declaration given by
1807 DECL, though a FUNCTION_DECL, has template parameters, and is
1808 therefore a template function.
1810 The template args (those explicitly specified and those deduced)
1811 are output in a newly created vector *TARGS_OUT.
1813 If it is impossible to determine the result, an error message is
1814 issued. The error_mark_node is returned to indicate failure. */
1817 determine_specialization (tree template_id
,
1820 int need_member_template
,
1826 tree explicit_targs
;
1827 tree candidates
= NULL_TREE
;
1828 /* A TREE_LIST of templates of which DECL may be a specialization.
1829 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1830 corresponding TREE_PURPOSE is the set of template arguments that,
1831 when used to instantiate the template, would produce a function
1832 with the signature of DECL. */
1833 tree templates
= NULL_TREE
;
1835 cp_binding_level
*b
;
1837 *targs_out
= NULL_TREE
;
1839 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1840 return error_mark_node
;
1842 /* We shouldn't be specializing a member template of an
1843 unspecialized class template; we already gave an error in
1844 check_specialization_scope, now avoid crashing. */
1845 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
1846 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
1848 gcc_assert (errorcount
);
1849 return error_mark_node
;
1852 fns
= TREE_OPERAND (template_id
, 0);
1853 explicit_targs
= TREE_OPERAND (template_id
, 1);
1855 if (fns
== error_mark_node
)
1856 return error_mark_node
;
1858 /* Check for baselinks. */
1859 if (BASELINK_P (fns
))
1860 fns
= BASELINK_FUNCTIONS (fns
);
1862 if (!is_overloaded_fn (fns
))
1864 error ("%qD is not a function template", fns
);
1865 return error_mark_node
;
1868 /* Count the number of template headers specified for this
1871 for (b
= current_binding_level
;
1872 b
->kind
== sk_template_parms
;
1876 for (; fns
; fns
= OVL_NEXT (fns
))
1878 tree fn
= OVL_CURRENT (fns
);
1880 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1882 tree decl_arg_types
;
1886 /* In case of explicit specialization, we need to check if
1887 the number of template headers appearing in the specialization
1888 is correct. This is usually done in check_explicit_specialization,
1889 but the check done there cannot be exhaustive when specializing
1890 member functions. Consider the following code:
1892 template <> void A<int>::f(int);
1893 template <> template <> void A<int>::f(int);
1895 Assuming that A<int> is not itself an explicit specialization
1896 already, the first line specializes "f" which is a non-template
1897 member function, whilst the second line specializes "f" which
1898 is a template member function. So both lines are syntactically
1899 correct, and check_explicit_specialization does not reject
1902 Here, we can do better, as we are matching the specialization
1903 against the declarations. We count the number of template
1904 headers, and we check if they match TEMPLATE_COUNT + 1
1905 (TEMPLATE_COUNT is the number of qualifying template classes,
1906 plus there must be another header for the member template
1909 Notice that if header_count is zero, this is not a
1910 specialization but rather a template instantiation, so there
1911 is no check we can perform here. */
1912 if (header_count
&& header_count
!= template_count
+ 1)
1915 /* Check that the number of template arguments at the
1916 innermost level for DECL is the same as for FN. */
1917 if (current_binding_level
->kind
== sk_template_parms
1918 && !current_binding_level
->explicit_spec_p
1919 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1920 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1921 (current_template_parms
))))
1924 /* DECL might be a specialization of FN. */
1925 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1926 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1928 /* For a non-static member function, we need to make sure
1929 that the const qualification is the same. Since
1930 get_bindings does not try to merge the "this" parameter,
1931 we must do the comparison explicitly. */
1932 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1933 && !same_type_p (TREE_VALUE (fn_arg_types
),
1934 TREE_VALUE (decl_arg_types
)))
1937 /* Skip the "this" parameter and, for constructors of
1938 classes with virtual bases, the VTT parameter. A
1939 full specialization of a constructor will have a VTT
1940 parameter, but a template never will. */
1942 = skip_artificial_parms_for (decl
, decl_arg_types
);
1944 = skip_artificial_parms_for (fn
, fn_arg_types
);
1946 /* Function templates cannot be specializations; there are
1947 no partial specializations of functions. Therefore, if
1948 the type of DECL does not match FN, there is no
1950 if (tsk
== tsk_template
)
1952 if (compparms (fn_arg_types
, decl_arg_types
))
1953 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1957 /* See whether this function might be a specialization of this
1958 template. Suppress access control because we might be trying
1959 to make this specialization a friend, and we have already done
1960 access control for the declaration of the specialization. */
1961 push_deferring_access_checks (dk_no_check
);
1962 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1963 pop_deferring_access_checks ();
1966 /* We cannot deduce template arguments that when used to
1967 specialize TMPL will produce DECL. */
1970 /* Make sure that the deduced arguments actually work. */
1971 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
1972 if (insttype
== error_mark_node
)
1975 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
1976 if (!compparms (fn_arg_types
, decl_arg_types
))
1979 /* Save this template, and the arguments deduced. */
1980 templates
= tree_cons (targs
, fn
, templates
);
1982 else if (need_member_template
)
1983 /* FN is an ordinary member function, and we need a
1984 specialization of a member template. */
1986 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1987 /* We can get IDENTIFIER_NODEs here in certain erroneous
1990 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1991 /* This is just an ordinary non-member function. Nothing can
1992 be a specialization of that. */
1994 else if (DECL_ARTIFICIAL (fn
))
1995 /* Cannot specialize functions that are created implicitly. */
1999 tree decl_arg_types
;
2001 /* This is an ordinary member function. However, since
2002 we're here, we can assume its enclosing class is a
2003 template class. For example,
2005 template <typename T> struct S { void f(); };
2006 template <> void S<int>::f() {}
2008 Here, S<int>::f is a non-template, but S<int> is a
2009 template class. If FN has the same type as DECL, we
2010 might be in business. */
2012 if (!DECL_TEMPLATE_INFO (fn
))
2013 /* Its enclosing class is an explicit specialization
2014 of a template class. This is not a candidate. */
2017 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2018 TREE_TYPE (TREE_TYPE (fn
))))
2019 /* The return types differ. */
2022 /* Adjust the type of DECL in case FN is a static member. */
2023 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2024 if (DECL_STATIC_FUNCTION_P (fn
)
2025 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2026 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2028 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2031 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2035 if (templates
&& TREE_CHAIN (templates
))
2041 It is possible for a specialization with a given function
2042 signature to be instantiated from more than one function
2043 template. In such cases, explicit specification of the
2044 template arguments must be used to uniquely identify the
2045 function template specialization being specialized.
2047 Note that here, there's no suggestion that we're supposed to
2048 determine which of the candidate templates is most
2049 specialized. However, we, also have:
2053 Partial ordering of overloaded function template
2054 declarations is used in the following contexts to select
2055 the function template to which a function template
2056 specialization refers:
2058 -- when an explicit specialization refers to a function
2061 So, we do use the partial ordering rules, at least for now.
2062 This extension can only serve to make invalid programs valid,
2063 so it's safe. And, there is strong anecdotal evidence that
2064 the committee intended the partial ordering rules to apply;
2065 the EDG front end has that behavior, and John Spicer claims
2066 that the committee simply forgot to delete the wording in
2067 [temp.expl.spec]. */
2068 tree tmpl
= most_specialized_instantiation (templates
);
2069 if (tmpl
!= error_mark_node
)
2072 TREE_CHAIN (templates
) = NULL_TREE
;
2076 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2078 error ("template-id %qD for %q+D does not match any template "
2079 "declaration", template_id
, decl
);
2080 if (header_count
&& header_count
!= template_count
+ 1)
2081 inform (input_location
, "saw %d %<template<>%>, need %d for "
2082 "specializing a member function template",
2083 header_count
, template_count
+ 1);
2084 return error_mark_node
;
2086 else if ((templates
&& TREE_CHAIN (templates
))
2087 || (candidates
&& TREE_CHAIN (candidates
))
2088 || (templates
&& candidates
))
2090 error ("ambiguous template specialization %qD for %q+D",
2092 candidates
= chainon (candidates
, templates
);
2093 print_candidates (candidates
);
2094 return error_mark_node
;
2097 /* We have one, and exactly one, match. */
2100 tree fn
= TREE_VALUE (candidates
);
2101 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2102 /* DECL is a re-declaration or partial instantiation of a template
2104 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2106 /* It was a specialization of an ordinary member function in a
2108 return DECL_TI_TEMPLATE (fn
);
2111 /* It was a specialization of a template. */
2112 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2113 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2115 *targs_out
= copy_node (targs
);
2116 SET_TMPL_ARGS_LEVEL (*targs_out
,
2117 TMPL_ARGS_DEPTH (*targs_out
),
2118 TREE_PURPOSE (templates
));
2121 *targs_out
= TREE_PURPOSE (templates
);
2122 return TREE_VALUE (templates
);
2125 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2126 but with the default argument values filled in from those in the
2130 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2133 tree new_spec_types
;
2138 if (spec_types
== void_list_node
)
2139 return void_list_node
;
2141 /* Substitute into the rest of the list. */
2143 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2144 TREE_CHAIN (tmpl_types
));
2146 /* Add the default argument for this parameter. */
2147 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2148 TREE_VALUE (spec_types
),
2152 /* DECL is an explicit specialization. Replicate default arguments
2153 from the template it specializes. (That way, code like:
2155 template <class T> void f(T = 3);
2156 template <> void f(double);
2159 works, as required.) An alternative approach would be to look up
2160 the correct default arguments at the call-site, but this approach
2161 is consistent with how implicit instantiations are handled. */
2164 copy_default_args_to_explicit_spec (tree decl
)
2169 tree new_spec_types
;
2173 tree object_type
= NULL_TREE
;
2174 tree in_charge
= NULL_TREE
;
2175 tree vtt
= NULL_TREE
;
2177 /* See if there's anything we need to do. */
2178 tmpl
= DECL_TI_TEMPLATE (decl
);
2179 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2180 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2181 if (TREE_PURPOSE (t
))
2186 old_type
= TREE_TYPE (decl
);
2187 spec_types
= TYPE_ARG_TYPES (old_type
);
2189 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2191 /* Remove the this pointer, but remember the object's type for
2193 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2194 spec_types
= TREE_CHAIN (spec_types
);
2195 tmpl_types
= TREE_CHAIN (tmpl_types
);
2197 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2199 /* DECL may contain more parameters than TMPL due to the extra
2200 in-charge parameter in constructors and destructors. */
2201 in_charge
= spec_types
;
2202 spec_types
= TREE_CHAIN (spec_types
);
2204 if (DECL_HAS_VTT_PARM_P (decl
))
2207 spec_types
= TREE_CHAIN (spec_types
);
2211 /* Compute the merged default arguments. */
2213 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2215 /* Compute the new FUNCTION_TYPE. */
2219 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2224 /* Put the in-charge parameter back. */
2225 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2226 TREE_VALUE (in_charge
),
2229 new_type
= build_method_type_directly (object_type
,
2230 TREE_TYPE (old_type
),
2234 new_type
= build_function_type (TREE_TYPE (old_type
),
2236 new_type
= cp_build_type_attribute_variant (new_type
,
2237 TYPE_ATTRIBUTES (old_type
));
2238 new_type
= build_exception_variant (new_type
,
2239 TYPE_RAISES_EXCEPTIONS (old_type
));
2240 TREE_TYPE (decl
) = new_type
;
2243 /* Return the number of template headers we expect to see for a definition
2244 or specialization of CTYPE or one of its non-template members. */
2247 num_template_headers_for_class (tree ctype
)
2249 int num_templates
= 0;
2251 while (ctype
&& CLASS_TYPE_P (ctype
))
2253 /* You're supposed to have one `template <...>' for every
2254 template class, but you don't need one for a full
2255 specialization. For example:
2257 template <class T> struct S{};
2258 template <> struct S<int> { void f(); };
2259 void S<int>::f () {}
2261 is correct; there shouldn't be a `template <>' for the
2262 definition of `S<int>::f'. */
2263 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2264 /* If CTYPE does not have template information of any
2265 kind, then it is not a template, nor is it nested
2266 within a template. */
2268 if (explicit_class_specialization_p (ctype
))
2270 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2273 ctype
= TYPE_CONTEXT (ctype
);
2276 return num_templates
;
2279 /* Do a simple sanity check on the template headers that precede the
2280 variable declaration DECL. */
2283 check_template_variable (tree decl
)
2285 tree ctx
= CP_DECL_CONTEXT (decl
);
2286 int wanted
= num_template_headers_for_class (ctx
);
2287 if (!TYPE_P (ctx
) || !CLASSTYPE_TEMPLATE_INFO (ctx
))
2288 permerror (DECL_SOURCE_LOCATION (decl
),
2289 "%qD is not a static data member of a class template", decl
);
2290 else if (template_header_count
> wanted
)
2292 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2293 "too many template headers for %D (should be %d)",
2295 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2296 inform (DECL_SOURCE_LOCATION (decl
),
2297 "members of an explicitly specialized class are defined "
2298 "without a template header");
2302 /* Check to see if the function just declared, as indicated in
2303 DECLARATOR, and in DECL, is a specialization of a function
2304 template. We may also discover that the declaration is an explicit
2305 instantiation at this point.
2307 Returns DECL, or an equivalent declaration that should be used
2308 instead if all goes well. Issues an error message if something is
2309 amiss. Returns error_mark_node if the error is not easily
2312 FLAGS is a bitmask consisting of the following flags:
2314 2: The function has a definition.
2315 4: The function is a friend.
2317 The TEMPLATE_COUNT is the number of references to qualifying
2318 template classes that appeared in the name of the function. For
2321 template <class T> struct S { void f(); };
2324 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2325 classes are not counted in the TEMPLATE_COUNT, so that in
2327 template <class T> struct S {};
2328 template <> struct S<int> { void f(); }
2329 template <> void S<int>::f();
2331 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2332 invalid; there should be no template <>.)
2334 If the function is a specialization, it is marked as such via
2335 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2336 is set up correctly, and it is added to the list of specializations
2337 for that template. */
2340 check_explicit_specialization (tree declarator
,
2345 int have_def
= flags
& 2;
2346 int is_friend
= flags
& 4;
2347 int specialization
= 0;
2348 int explicit_instantiation
= 0;
2349 int member_specialization
= 0;
2350 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2351 tree dname
= DECL_NAME (decl
);
2356 if (!processing_specialization
)
2359 tsk
= tsk_excessive_parms
;
2362 tsk
= current_tmpl_spec_kind (template_count
);
2367 if (processing_specialization
)
2370 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2372 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2375 /* This could be something like:
2377 template <class T> void f(T);
2378 class S { friend void f<>(int); } */
2382 /* This case handles bogus declarations like template <>
2383 template <class T> void f<int>(); */
2385 error ("template-id %qD in declaration of primary template",
2392 case tsk_invalid_member_spec
:
2393 /* The error has already been reported in
2394 check_specialization_scope. */
2395 return error_mark_node
;
2397 case tsk_invalid_expl_inst
:
2398 error ("template parameter list used in explicit instantiation");
2404 error ("definition provided for explicit instantiation");
2406 explicit_instantiation
= 1;
2409 case tsk_excessive_parms
:
2410 case tsk_insufficient_parms
:
2411 if (tsk
== tsk_excessive_parms
)
2412 error ("too many template parameter lists in declaration of %qD",
2414 else if (template_header_count
)
2415 error("too few template parameter lists in declaration of %qD", decl
);
2417 error("explicit specialization of %qD must be introduced by "
2418 "%<template <>%>", decl
);
2422 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2424 member_specialization
= 1;
2430 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2432 /* This case handles bogus declarations like template <>
2433 template <class T> void f<int>(); */
2435 if (uses_template_parms (declarator
))
2436 error ("function template partial specialization %qD "
2437 "is not allowed", declarator
);
2439 error ("template-id %qD in declaration of primary template",
2444 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2445 /* This is a specialization of a member template, without
2446 specialization the containing class. Something like:
2448 template <class T> struct S {
2449 template <class U> void f (U);
2451 template <> template <class U> void S<int>::f(U) {}
2453 That's a specialization -- but of the entire template. */
2461 if (specialization
|| member_specialization
)
2463 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2464 for (; t
; t
= TREE_CHAIN (t
))
2465 if (TREE_PURPOSE (t
))
2467 permerror (input_location
,
2468 "default argument specified in explicit specialization");
2473 if (specialization
|| member_specialization
|| explicit_instantiation
)
2475 tree tmpl
= NULL_TREE
;
2476 tree targs
= NULL_TREE
;
2478 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2479 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2483 gcc_assert (identifier_p (declarator
));
2488 /* If there is no class context, the explicit instantiation
2489 must be at namespace scope. */
2490 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2492 /* Find the namespace binding, using the declaration
2494 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2496 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2498 error ("%qD is not a template function", dname
);
2499 fns
= error_mark_node
;
2503 tree fn
= OVL_CURRENT (fns
);
2504 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2505 CP_DECL_CONTEXT (fn
)))
2506 error ("%qD is not declared in %qD",
2507 decl
, current_namespace
);
2511 declarator
= lookup_template_function (fns
, NULL_TREE
);
2514 if (declarator
== error_mark_node
)
2515 return error_mark_node
;
2517 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2519 if (!explicit_instantiation
)
2520 /* A specialization in class scope. This is invalid,
2521 but the error will already have been flagged by
2522 check_specialization_scope. */
2523 return error_mark_node
;
2526 /* It's not valid to write an explicit instantiation in
2529 class C { template void f(); }
2531 This case is caught by the parser. However, on
2534 template class C { void f(); };
2536 (which is invalid) we can get here. The error will be
2543 else if (ctype
!= NULL_TREE
2544 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2546 /* Find the list of functions in ctype that have the same
2547 name as the declared function. */
2548 tree name
= TREE_OPERAND (declarator
, 0);
2549 tree fns
= NULL_TREE
;
2552 if (constructor_name_p (name
, ctype
))
2554 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2556 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2557 : !CLASSTYPE_DESTRUCTORS (ctype
))
2559 /* From [temp.expl.spec]:
2561 If such an explicit specialization for the member
2562 of a class template names an implicitly-declared
2563 special member function (clause _special_), the
2564 program is ill-formed.
2566 Similar language is found in [temp.explicit]. */
2567 error ("specialization of implicitly-declared special member function");
2568 return error_mark_node
;
2571 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2574 if (!DECL_CONV_FN_P (decl
))
2576 idx
= lookup_fnfields_1 (ctype
, name
);
2578 fns
= (*CLASSTYPE_METHOD_VEC (ctype
))[idx
];
2582 vec
<tree
, va_gc
> *methods
;
2585 /* For a type-conversion operator, we cannot do a
2586 name-based lookup. We might be looking for `operator
2587 int' which will be a specialization of `operator T'.
2588 So, we find *all* the conversion operators, and then
2589 select from them. */
2592 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2594 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2595 methods
->iterate (idx
, &ovl
);
2598 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2599 /* There are no more conversion functions. */
2602 /* Glue all these conversion functions together
2603 with those we already have. */
2604 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2605 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2609 if (fns
== NULL_TREE
)
2611 error ("no member function %qD declared in %qT", name
, ctype
);
2612 return error_mark_node
;
2615 TREE_OPERAND (declarator
, 0) = fns
;
2618 /* Figure out what exactly is being specialized at this point.
2619 Note that for an explicit instantiation, even one for a
2620 member function, we cannot tell apriori whether the
2621 instantiation is for a member template, or just a member
2622 function of a template class. Even if a member template is
2623 being instantiated, the member template arguments may be
2624 elided if they can be deduced from the rest of the
2626 tmpl
= determine_specialization (declarator
, decl
,
2628 member_specialization
,
2632 if (!tmpl
|| tmpl
== error_mark_node
)
2633 /* We couldn't figure out what this declaration was
2635 return error_mark_node
;
2638 tree gen_tmpl
= most_general_template (tmpl
);
2640 if (explicit_instantiation
)
2642 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2643 is done by do_decl_instantiation later. */
2645 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2646 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2648 if (arg_depth
> parm_depth
)
2650 /* If TMPL is not the most general template (for
2651 example, if TMPL is a friend template that is
2652 injected into namespace scope), then there will
2653 be too many levels of TARGS. Remove some of them
2658 new_targs
= make_tree_vec (parm_depth
);
2659 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2660 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2661 = TREE_VEC_ELT (targs
, i
);
2665 return instantiate_template (tmpl
, targs
, tf_error
);
2668 /* If we thought that the DECL was a member function, but it
2669 turns out to be specializing a static member function,
2670 make DECL a static member function as well. */
2671 if (DECL_STATIC_FUNCTION_P (tmpl
)
2672 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2673 revert_static_member_fn (decl
);
2675 /* If this is a specialization of a member template of a
2676 template class, we want to return the TEMPLATE_DECL, not
2677 the specialization of it. */
2678 if (tsk
== tsk_template
)
2680 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2681 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2682 DECL_INITIAL (result
) = NULL_TREE
;
2686 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2687 DECL_SOURCE_LOCATION (result
)
2688 = DECL_SOURCE_LOCATION (decl
);
2689 /* We want to use the argument list specified in the
2690 definition, not in the original declaration. */
2691 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2692 for (parm
= DECL_ARGUMENTS (result
); parm
;
2693 parm
= DECL_CHAIN (parm
))
2694 DECL_CONTEXT (parm
) = result
;
2696 return register_specialization (tmpl
, gen_tmpl
, targs
,
2700 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2701 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2703 /* Inherit default function arguments from the template
2704 DECL is specializing. */
2705 copy_default_args_to_explicit_spec (decl
);
2707 /* This specialization has the same protection as the
2708 template it specializes. */
2709 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2710 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2712 /* 7.1.1-1 [dcl.stc]
2714 A storage-class-specifier shall not be specified in an
2715 explicit specialization...
2717 The parser rejects these, so unless action is taken here,
2718 explicit function specializations will always appear with
2721 The action recommended by the C++ CWG in response to C++
2722 defect report 605 is to make the storage class and linkage
2723 of the explicit specialization match the templated function:
2725 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2727 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2729 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2730 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2732 /* This specialization has the same linkage and visibility as
2733 the function template it specializes. */
2734 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2735 if (! TREE_PUBLIC (decl
))
2737 DECL_INTERFACE_KNOWN (decl
) = 1;
2738 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2740 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2741 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2743 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2744 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2748 /* If DECL is a friend declaration, declared using an
2749 unqualified name, the namespace associated with DECL may
2750 have been set incorrectly. For example, in:
2752 template <typename T> void f(T);
2754 struct S { friend void f<int>(int); }
2757 we will have set the DECL_CONTEXT for the friend
2758 declaration to N, rather than to the global namespace. */
2759 if (DECL_NAMESPACE_SCOPE_P (decl
))
2760 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2762 if (is_friend
&& !have_def
)
2763 /* This is not really a declaration of a specialization.
2764 It's just the name of an instantiation. But, it's not
2765 a request for an instantiation, either. */
2766 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2767 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2768 /* This is indeed a specialization. In case of constructors
2769 and destructors, we need in-charge and not-in-charge
2770 versions in V3 ABI. */
2771 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2773 /* Register this specialization so that we can find it
2775 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2782 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2783 parameters. These are represented in the same format used for
2784 DECL_TEMPLATE_PARMS. */
2787 comp_template_parms (const_tree parms1
, const_tree parms2
)
2792 if (parms1
== parms2
)
2795 for (p1
= parms1
, p2
= parms2
;
2796 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2797 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2799 tree t1
= TREE_VALUE (p1
);
2800 tree t2
= TREE_VALUE (p2
);
2803 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2804 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2806 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2809 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2811 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2812 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2814 /* If either of the template parameters are invalid, assume
2815 they match for the sake of error recovery. */
2816 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2819 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2822 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2823 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2824 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2826 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2831 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2832 /* One set of parameters has more parameters lists than the
2839 /* Determine whether PARM is a parameter pack. */
2842 template_parameter_pack_p (const_tree parm
)
2844 /* Determine if we have a non-type template parameter pack. */
2845 if (TREE_CODE (parm
) == PARM_DECL
)
2846 return (DECL_TEMPLATE_PARM_P (parm
)
2847 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2848 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
2849 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
2851 /* If this is a list of template parameters, we could get a
2852 TYPE_DECL or a TEMPLATE_DECL. */
2853 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2854 parm
= TREE_TYPE (parm
);
2856 /* Otherwise it must be a type template parameter. */
2857 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2858 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2859 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2862 /* Determine if T is a function parameter pack. */
2865 function_parameter_pack_p (const_tree t
)
2867 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2868 return DECL_PACK_P (t
);
2872 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2873 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2876 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2878 if (! primary_func_tmpl_inst
2879 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2880 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2883 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2886 /* Return true iff the function parameter PARAM_DECL was expanded
2887 from the function parameter pack PACK. */
2890 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
2892 if (DECL_ARTIFICIAL (param_decl
)
2893 || !function_parameter_pack_p (pack
))
2896 /* The parameter pack and its pack arguments have the same
2898 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
2901 /* Determine whether ARGS describes a variadic template args list,
2902 i.e., one that is terminated by a template argument pack. */
2905 template_args_variadic_p (tree args
)
2910 if (args
== NULL_TREE
)
2913 args
= INNERMOST_TEMPLATE_ARGS (args
);
2914 nargs
= TREE_VEC_LENGTH (args
);
2919 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2921 return ARGUMENT_PACK_P (last_parm
);
2924 /* Generate a new name for the parameter pack name NAME (an
2925 IDENTIFIER_NODE) that incorporates its */
2928 make_ith_pack_parameter_name (tree name
, int i
)
2930 /* Munge the name to include the parameter index. */
2931 #define NUMBUF_LEN 128
2932 char numbuf
[NUMBUF_LEN
];
2936 if (name
== NULL_TREE
)
2938 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
2939 newname_len
= IDENTIFIER_LENGTH (name
)
2940 + strlen (numbuf
) + 2;
2941 newname
= (char*)alloca (newname_len
);
2942 snprintf (newname
, newname_len
,
2943 "%s#%i", IDENTIFIER_POINTER (name
), i
);
2944 return get_identifier (newname
);
2947 /* Return true if T is a primary function, class or alias template
2951 primary_template_instantiation_p (const_tree t
)
2956 if (TREE_CODE (t
) == FUNCTION_DECL
)
2957 return DECL_LANG_SPECIFIC (t
)
2958 && DECL_TEMPLATE_INSTANTIATION (t
)
2959 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
2960 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
2961 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
2962 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
2963 else if (alias_template_specialization_p (t
))
2968 /* Return true if PARM is a template template parameter. */
2971 template_template_parameter_p (const_tree parm
)
2973 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
2976 /* Return true iff PARM is a DECL representing a type template
2980 template_type_parameter_p (const_tree parm
)
2983 && (TREE_CODE (parm
) == TYPE_DECL
2984 || TREE_CODE (parm
) == TEMPLATE_DECL
)
2985 && DECL_TEMPLATE_PARM_P (parm
));
2988 /* Return the template parameters of T if T is a
2989 primary template instantiation, NULL otherwise. */
2992 get_primary_template_innermost_parameters (const_tree t
)
2994 tree parms
= NULL
, template_info
= NULL
;
2996 if ((template_info
= get_template_info (t
))
2997 && primary_template_instantiation_p (t
))
2998 parms
= INNERMOST_TEMPLATE_PARMS
2999 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3004 /* Return the template parameters of the LEVELth level from the full list
3005 of template parameters PARMS. */
3008 get_template_parms_at_level (tree parms
, int level
)
3012 || TREE_CODE (parms
) != TREE_LIST
3013 || level
> TMPL_PARMS_DEPTH (parms
))
3016 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3017 if (TMPL_PARMS_DEPTH (p
) == level
)
3023 /* Returns the template arguments of T if T is a template instantiation,
3027 get_template_innermost_arguments (const_tree t
)
3029 tree args
= NULL
, template_info
= NULL
;
3031 if ((template_info
= get_template_info (t
))
3032 && TI_ARGS (template_info
))
3033 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3038 /* Return the argument pack elements of T if T is a template argument pack,
3042 get_template_argument_pack_elems (const_tree t
)
3044 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3045 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3048 return ARGUMENT_PACK_ARGS (t
);
3051 /* Structure used to track the progress of find_parameter_packs_r. */
3052 struct find_parameter_pack_data
3054 /* TREE_LIST that will contain all of the parameter packs found by
3056 tree
* parameter_packs
;
3058 /* Set of AST nodes that have been visited by the traversal. */
3059 struct pointer_set_t
*visited
;
3062 /* Identifies all of the argument packs that occur in a template
3063 argument and appends them to the TREE_LIST inside DATA, which is a
3064 find_parameter_pack_data structure. This is a subroutine of
3065 make_pack_expansion and uses_parameter_packs. */
3067 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3070 struct find_parameter_pack_data
* ppd
=
3071 (struct find_parameter_pack_data
*)data
;
3072 bool parameter_pack_p
= false;
3074 /* Handle type aliases/typedefs. */
3075 if (TYPE_ALIAS_P (t
))
3077 if (TYPE_TEMPLATE_INFO (t
))
3078 cp_walk_tree (&TYPE_TI_ARGS (t
),
3079 &find_parameter_packs_r
,
3085 /* Identify whether this is a parameter pack or not. */
3086 switch (TREE_CODE (t
))
3088 case TEMPLATE_PARM_INDEX
:
3089 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3090 parameter_pack_p
= true;
3093 case TEMPLATE_TYPE_PARM
:
3094 t
= TYPE_MAIN_VARIANT (t
);
3095 case TEMPLATE_TEMPLATE_PARM
:
3096 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3097 parameter_pack_p
= true;
3102 if (DECL_PACK_P (t
))
3104 /* We don't want to walk into the type of a PARM_DECL,
3105 because we don't want to see the type parameter pack. */
3107 parameter_pack_p
= true;
3111 /* Look through a lambda capture proxy to the field pack. */
3113 if (DECL_HAS_VALUE_EXPR_P (t
))
3115 tree v
= DECL_VALUE_EXPR (t
);
3117 &find_parameter_packs_r
,
3124 parameter_pack_p
= true;
3127 /* Not a parameter pack. */
3131 if (parameter_pack_p
)
3133 /* Add this parameter pack to the list. */
3134 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3138 cp_walk_tree (&TYPE_CONTEXT (t
),
3139 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3141 /* This switch statement will return immediately if we don't find a
3143 switch (TREE_CODE (t
))
3145 case TEMPLATE_PARM_INDEX
:
3148 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3149 /* Check the template itself. */
3150 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3151 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3152 /* Check the template arguments. */
3153 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3158 case TEMPLATE_TYPE_PARM
:
3159 case TEMPLATE_TEMPLATE_PARM
:
3166 if (TYPE_PTRMEMFUNC_P (t
))
3172 if (TYPE_TEMPLATE_INFO (t
))
3173 cp_walk_tree (&TYPE_TI_ARGS (t
),
3174 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3181 cp_walk_tree (&TREE_TYPE (t
),
3182 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3186 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3191 case TYPE_PACK_EXPANSION
:
3192 case EXPR_PACK_EXPANSION
:
3197 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3202 case IDENTIFIER_NODE
:
3203 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3215 /* Determines if the expression or type T uses any parameter packs. */
3217 uses_parameter_packs (tree t
)
3219 tree parameter_packs
= NULL_TREE
;
3220 struct find_parameter_pack_data ppd
;
3221 ppd
.parameter_packs
= ¶meter_packs
;
3222 ppd
.visited
= pointer_set_create ();
3223 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3224 pointer_set_destroy (ppd
.visited
);
3225 return parameter_packs
!= NULL_TREE
;
3228 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3229 representation a base-class initializer into a parameter pack
3230 expansion. If all goes well, the resulting node will be an
3231 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3234 make_pack_expansion (tree arg
)
3237 tree parameter_packs
= NULL_TREE
;
3238 bool for_types
= false;
3239 struct find_parameter_pack_data ppd
;
3241 if (!arg
|| arg
== error_mark_node
)
3244 if (TREE_CODE (arg
) == TREE_LIST
)
3246 /* The only time we will see a TREE_LIST here is for a base
3247 class initializer. In this case, the TREE_PURPOSE will be a
3248 _TYPE node (representing the base class expansion we're
3249 initializing) and the TREE_VALUE will be a TREE_LIST
3250 containing the initialization arguments.
3252 The resulting expansion looks somewhat different from most
3253 expansions. Rather than returning just one _EXPANSION, we
3254 return a TREE_LIST whose TREE_PURPOSE is a
3255 TYPE_PACK_EXPANSION containing the bases that will be
3256 initialized. The TREE_VALUE will be identical to the
3257 original TREE_VALUE, which is a list of arguments that will
3258 be passed to each base. We do not introduce any new pack
3259 expansion nodes into the TREE_VALUE (although it is possible
3260 that some already exist), because the TREE_PURPOSE and
3261 TREE_VALUE all need to be expanded together with the same
3262 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3263 resulting TREE_PURPOSE will mention the parameter packs in
3264 both the bases and the arguments to the bases. */
3267 tree parameter_packs
= NULL_TREE
;
3269 /* Determine which parameter packs will be used by the base
3271 ppd
.visited
= pointer_set_create ();
3272 ppd
.parameter_packs
= ¶meter_packs
;
3273 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3276 if (parameter_packs
== NULL_TREE
)
3278 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3279 pointer_set_destroy (ppd
.visited
);
3280 return error_mark_node
;
3283 if (TREE_VALUE (arg
) != void_type_node
)
3285 /* Collect the sets of parameter packs used in each of the
3286 initialization arguments. */
3287 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3289 /* Determine which parameter packs will be expanded in this
3291 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3296 pointer_set_destroy (ppd
.visited
);
3298 /* Create the pack expansion type for the base type. */
3299 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3300 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3301 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3303 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3304 they will rarely be compared to anything. */
3305 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3307 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3310 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3313 /* Build the PACK_EXPANSION_* node. */
3315 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3316 : make_node (EXPR_PACK_EXPANSION
);
3317 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3318 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3320 /* Propagate type and const-expression information. */
3321 TREE_TYPE (result
) = TREE_TYPE (arg
);
3322 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3325 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3326 they will rarely be compared to anything. */
3327 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3329 /* Determine which parameter packs will be expanded. */
3330 ppd
.parameter_packs
= ¶meter_packs
;
3331 ppd
.visited
= pointer_set_create ();
3332 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3333 pointer_set_destroy (ppd
.visited
);
3335 /* Make sure we found some parameter packs. */
3336 if (parameter_packs
== NULL_TREE
)
3339 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3341 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3342 return error_mark_node
;
3344 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3346 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3351 /* Checks T for any "bare" parameter packs, which have not yet been
3352 expanded, and issues an error if any are found. This operation can
3353 only be done on full expressions or types (e.g., an expression
3354 statement, "if" condition, etc.), because we could have expressions like:
3356 foo(f(g(h(args)))...)
3358 where "args" is a parameter pack. check_for_bare_parameter_packs
3359 should not be called for the subexpressions args, h(args),
3360 g(h(args)), or f(g(h(args))), because we would produce erroneous
3363 Returns TRUE and emits an error if there were bare parameter packs,
3364 returns FALSE otherwise. */
3366 check_for_bare_parameter_packs (tree t
)
3368 tree parameter_packs
= NULL_TREE
;
3369 struct find_parameter_pack_data ppd
;
3371 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3374 if (TREE_CODE (t
) == TYPE_DECL
)
3377 ppd
.parameter_packs
= ¶meter_packs
;
3378 ppd
.visited
= pointer_set_create ();
3379 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3380 pointer_set_destroy (ppd
.visited
);
3382 if (parameter_packs
)
3384 error ("parameter packs not expanded with %<...%>:");
3385 while (parameter_packs
)
3387 tree pack
= TREE_VALUE (parameter_packs
);
3388 tree name
= NULL_TREE
;
3390 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3391 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3392 name
= TYPE_NAME (pack
);
3393 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3394 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3396 name
= DECL_NAME (pack
);
3399 inform (input_location
, " %qD", name
);
3401 inform (input_location
, " <anonymous>");
3403 parameter_packs
= TREE_CHAIN (parameter_packs
);
3412 /* Expand any parameter packs that occur in the template arguments in
3415 expand_template_argument_pack (tree args
)
3417 tree result_args
= NULL_TREE
;
3418 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3419 int num_result_args
= -1;
3420 int non_default_args_count
= -1;
3422 /* First, determine if we need to expand anything, and the number of
3423 slots we'll need. */
3424 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3426 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3427 if (arg
== NULL_TREE
)
3429 if (ARGUMENT_PACK_P (arg
))
3431 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3432 if (num_result_args
< 0)
3433 num_result_args
= in_arg
+ num_packed
;
3435 num_result_args
+= num_packed
;
3439 if (num_result_args
>= 0)
3444 /* If no expansion is necessary, we're done. */
3445 if (num_result_args
< 0)
3448 /* Expand arguments. */
3449 result_args
= make_tree_vec (num_result_args
);
3450 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3451 non_default_args_count
=
3452 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3453 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3455 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3456 if (ARGUMENT_PACK_P (arg
))
3458 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3459 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3460 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3461 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3462 if (non_default_args_count
> 0)
3463 non_default_args_count
+= num_packed
;
3467 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3471 if (non_default_args_count
>= 0)
3472 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3476 /* Checks if DECL shadows a template parameter.
3478 [temp.local]: A template-parameter shall not be redeclared within its
3479 scope (including nested scopes).
3481 Emits an error and returns TRUE if the DECL shadows a parameter,
3482 returns FALSE otherwise. */
3485 check_template_shadow (tree decl
)
3489 /* If we're not in a template, we can't possibly shadow a template
3491 if (!current_template_parms
)
3494 /* Figure out what we're shadowing. */
3495 if (TREE_CODE (decl
) == OVERLOAD
)
3496 decl
= OVL_CURRENT (decl
);
3497 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3499 /* If there's no previous binding for this name, we're not shadowing
3500 anything, let alone a template parameter. */
3504 /* If we're not shadowing a template parameter, we're done. Note
3505 that OLDDECL might be an OVERLOAD (or perhaps even an
3506 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3508 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3511 /* We check for decl != olddecl to avoid bogus errors for using a
3512 name inside a class. We check TPFI to avoid duplicate errors for
3513 inline member templates. */
3515 || (DECL_TEMPLATE_PARM_P (decl
)
3516 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
3519 error ("declaration of %q+#D", decl
);
3520 error (" shadows template parm %q+#D", olddecl
);
3524 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3525 ORIG_LEVEL, DECL, and TYPE. */
3528 build_template_parm_index (int index
,
3534 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3535 TEMPLATE_PARM_IDX (t
) = index
;
3536 TEMPLATE_PARM_LEVEL (t
) = level
;
3537 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3538 TEMPLATE_PARM_DECL (t
) = decl
;
3539 TREE_TYPE (t
) = type
;
3540 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3541 TREE_READONLY (t
) = TREE_READONLY (decl
);
3546 /* Find the canonical type parameter for the given template type
3547 parameter. Returns the canonical type parameter, which may be TYPE
3548 if no such parameter existed. */
3551 canonical_type_parameter (tree type
)
3554 int idx
= TEMPLATE_TYPE_IDX (type
);
3555 if (!canonical_template_parms
)
3556 vec_alloc (canonical_template_parms
, idx
+1);
3558 while (canonical_template_parms
->length () <= (unsigned)idx
)
3559 vec_safe_push (canonical_template_parms
, NULL_TREE
);
3561 list
= (*canonical_template_parms
)[idx
];
3562 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3563 list
= TREE_CHAIN (list
);
3566 return TREE_VALUE (list
);
3569 (*canonical_template_parms
)[idx
]
3570 = tree_cons (NULL_TREE
, type
,
3571 (*canonical_template_parms
)[idx
]);
3576 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3577 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3578 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3579 new one is created. */
3582 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3583 tsubst_flags_t complain
)
3585 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3586 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3587 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3588 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3590 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3593 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3594 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3595 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3596 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3597 DECL_ARTIFICIAL (decl
) = 1;
3598 SET_DECL_TEMPLATE_PARM_P (decl
);
3600 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3601 TEMPLATE_PARM_LEVEL (index
) - levels
,
3602 TEMPLATE_PARM_ORIG_LEVEL (index
),
3604 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3605 TEMPLATE_PARM_PARAMETER_PACK (t
)
3606 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3608 /* Template template parameters need this. */
3609 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3610 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3611 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3615 return TEMPLATE_PARM_DESCENDANTS (index
);
3618 /* Process information from new template parameter PARM and append it
3619 to the LIST being built. This new parameter is a non-type
3620 parameter iff IS_NON_TYPE is true. This new parameter is a
3621 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3622 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3623 parameter list PARM belongs to. This is used used to create a
3624 proper canonical type for the type of PARM that is to be created,
3625 iff PARM is a type. If the size is not known, this parameter shall
3629 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
3630 bool is_non_type
, bool is_parameter_pack
)
3637 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3638 defval
= TREE_PURPOSE (parm
);
3642 tree p
= tree_last (list
);
3644 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3647 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3648 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3650 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3660 parm
= TREE_VALUE (parm
);
3662 SET_DECL_TEMPLATE_PARM_P (parm
);
3664 if (TREE_TYPE (parm
) == error_mark_node
)
3666 err_parm_list
= build_tree_list (defval
, parm
);
3667 TREE_VALUE (err_parm_list
) = error_mark_node
;
3668 return chainon (list
, err_parm_list
);
3674 The top-level cv-qualifiers on the template-parameter are
3675 ignored when determining its type. */
3676 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3677 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3679 err_parm_list
= build_tree_list (defval
, parm
);
3680 TREE_VALUE (err_parm_list
) = error_mark_node
;
3681 return chainon (list
, err_parm_list
);
3684 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
)
3686 /* This template parameter is not a parameter pack, but it
3687 should be. Complain about "bare" parameter packs. */
3688 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3690 /* Recover by calling this a parameter pack. */
3691 is_parameter_pack
= true;
3695 /* A template parameter is not modifiable. */
3696 TREE_CONSTANT (parm
) = 1;
3697 TREE_READONLY (parm
) = 1;
3698 decl
= build_decl (parm_loc
,
3699 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3700 TREE_CONSTANT (decl
) = 1;
3701 TREE_READONLY (decl
) = 1;
3702 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3703 = build_template_parm_index (idx
, processing_template_decl
,
3704 processing_template_decl
,
3705 decl
, TREE_TYPE (parm
));
3707 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3708 = is_parameter_pack
;
3713 parm
= TREE_VALUE (TREE_VALUE (parm
));
3715 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3717 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3718 /* This is for distinguishing between real templates and template
3719 template parameters */
3720 TREE_TYPE (parm
) = t
;
3721 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3726 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3727 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3728 decl
= build_decl (parm_loc
,
3729 TYPE_DECL
, parm
, t
);
3732 TYPE_NAME (t
) = decl
;
3733 TYPE_STUB_DECL (t
) = decl
;
3735 TEMPLATE_TYPE_PARM_INDEX (t
)
3736 = build_template_parm_index (idx
, processing_template_decl
,
3737 processing_template_decl
,
3738 decl
, TREE_TYPE (parm
));
3739 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3740 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3742 DECL_ARTIFICIAL (decl
) = 1;
3743 SET_DECL_TEMPLATE_PARM_P (decl
);
3745 parm
= build_tree_list (defval
, parm
);
3746 return chainon (list
, parm
);
3749 /* The end of a template parameter list has been reached. Process the
3750 tree list into a parameter vector, converting each parameter into a more
3751 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3755 end_template_parm_list (tree parms
)
3759 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3761 current_template_parms
3762 = tree_cons (size_int (processing_template_decl
),
3763 saved_parmlist
, current_template_parms
);
3765 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3767 next
= TREE_CHAIN (parm
);
3768 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3769 TREE_CHAIN (parm
) = NULL_TREE
;
3772 --processing_template_parmlist
;
3774 return saved_parmlist
;
3777 /* end_template_decl is called after a template declaration is seen. */
3780 end_template_decl (void)
3782 reset_specialization ();
3784 if (! processing_template_decl
)
3787 /* This matches the pushlevel in begin_template_parm_list. */
3790 --processing_template_decl
;
3791 current_template_parms
= TREE_CHAIN (current_template_parms
);
3794 /* Takes a TREE_LIST representing a template parameter and convert it
3795 into an argument suitable to be passed to the type substitution
3796 functions. Note that If the TREE_LIST contains an error_mark
3797 node, the returned argument is error_mark_node. */
3800 template_parm_to_arg (tree t
)
3804 || TREE_CODE (t
) != TREE_LIST
)
3807 if (error_operand_p (TREE_VALUE (t
)))
3808 return error_mark_node
;
3812 if (TREE_CODE (t
) == TYPE_DECL
3813 || TREE_CODE (t
) == TEMPLATE_DECL
)
3817 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3819 /* Turn this argument into a TYPE_ARGUMENT_PACK
3820 with a single element, which expands T. */
3821 tree vec
= make_tree_vec (1);
3822 #ifdef ENABLE_CHECKING
3823 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3824 (vec
, TREE_VEC_LENGTH (vec
));
3826 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3828 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
3829 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3834 t
= DECL_INITIAL (t
);
3836 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3838 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3839 with a single element, which expands T. */
3840 tree vec
= make_tree_vec (1);
3841 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3842 #ifdef ENABLE_CHECKING
3843 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3844 (vec
, TREE_VEC_LENGTH (vec
));
3846 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3848 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3849 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3850 TREE_TYPE (t
) = type
;
3856 /* Given a set of template parameters, return them as a set of template
3857 arguments. The template parameters are represented as a TREE_VEC, in
3858 the form documented in cp-tree.h for template arguments. */
3861 template_parms_to_args (tree parms
)
3864 tree args
= NULL_TREE
;
3865 int length
= TMPL_PARMS_DEPTH (parms
);
3868 /* If there is only one level of template parameters, we do not
3869 create a TREE_VEC of TREE_VECs. Instead, we return a single
3870 TREE_VEC containing the arguments. */
3872 args
= make_tree_vec (length
);
3874 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
3876 tree a
= copy_node (TREE_VALUE (header
));
3879 TREE_TYPE (a
) = NULL_TREE
;
3880 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
3881 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
3883 #ifdef ENABLE_CHECKING
3884 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
3888 TREE_VEC_ELT (args
, --l
) = a
;
3893 if (length
> 1 && TREE_VEC_ELT (args
, 0) == NULL_TREE
)
3894 /* This can happen for template parms of a template template
3897 template<template<class T, class U> class TT> struct S;
3899 Consider the level of the parms of TT; T and U both have
3900 level 2; TT has no template parm of level 1. So in this case
3901 the first element of full_template_args is NULL_TREE. If we
3902 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
3903 of 2. This will make tsubst wrongly consider that T and U
3904 have level 1. Instead, let's create a dummy vector as the
3905 first element of full_template_args so that TMPL_ARGS_DEPTH
3906 returns the correct depth for args. */
3907 TREE_VEC_ELT (args
, 0) = make_tree_vec (1);
3911 /* Within the declaration of a template, return the currently active
3912 template parameters as an argument TREE_VEC. */
3915 current_template_args (void)
3917 return template_parms_to_args (current_template_parms
);
3920 /* Update the declared TYPE by doing any lookups which were thought to be
3921 dependent, but are not now that we know the SCOPE of the declarator. */
3924 maybe_update_decl_type (tree orig_type
, tree scope
)
3926 tree type
= orig_type
;
3928 if (type
== NULL_TREE
)
3931 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3932 type
= TREE_TYPE (type
);
3934 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
3935 && dependent_type_p (type
)
3936 /* Don't bother building up the args in this case. */
3937 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
3939 /* tsubst in the args corresponding to the template parameters,
3940 including auto if present. Most things will be unchanged, but
3941 make_typename_type and tsubst_qualified_id will resolve
3942 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3943 tree args
= current_template_args ();
3944 tree auto_node
= type_uses_auto (type
);
3948 tree auto_vec
= make_tree_vec (1);
3949 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
3950 args
= add_to_template_args (args
, auto_vec
);
3952 pushed
= push_scope (scope
);
3953 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
3958 if (type
== error_mark_node
)
3961 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3963 if (same_type_p (type
, TREE_TYPE (orig_type
)))
3966 type
= TYPE_NAME (type
);
3971 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3972 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3973 a member template. Used by push_template_decl below. */
3976 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
3978 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
3979 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
3980 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
3981 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3982 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
3987 struct template_parm_data
3989 /* The level of the template parameters we are currently
3993 /* The index of the specialization argument we are currently
3997 /* An array whose size is the number of template parameters. The
3998 elements are nonzero if the parameter has been used in any one
3999 of the arguments processed so far. */
4002 /* An array whose size is the number of template arguments. The
4003 elements are nonzero if the argument makes use of template
4004 parameters of this level. */
4005 int* arg_uses_template_parms
;
4008 /* Subroutine of push_template_decl used to see if each template
4009 parameter in a partial specialization is used in the explicit
4010 argument list. If T is of the LEVEL given in DATA (which is
4011 treated as a template_parm_data*), then DATA->PARMS is marked
4015 mark_template_parm (tree t
, void* data
)
4019 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4021 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4023 level
= TEMPLATE_PARM_LEVEL (t
);
4024 idx
= TEMPLATE_PARM_IDX (t
);
4028 level
= TEMPLATE_TYPE_LEVEL (t
);
4029 idx
= TEMPLATE_TYPE_IDX (t
);
4032 if (level
== tpd
->level
)
4034 tpd
->parms
[idx
] = 1;
4035 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4038 /* Return zero so that for_each_template_parm will continue the
4039 traversal of the tree; we want to mark *every* template parm. */
4043 /* Process the partial specialization DECL. */
4046 process_partial_specialization (tree decl
)
4048 tree type
= TREE_TYPE (decl
);
4049 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
4050 tree specargs
= CLASSTYPE_TI_ARGS (type
);
4051 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4052 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4055 int nargs
= TREE_VEC_LENGTH (inner_args
);
4058 bool did_error_intro
= false;
4059 struct template_parm_data tpd
;
4060 struct template_parm_data tpd2
;
4062 gcc_assert (current_template_parms
);
4064 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4065 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4067 /* We check that each of the template parameters given in the
4068 partial specialization is used in the argument list to the
4069 specialization. For example:
4071 template <class T> struct S;
4072 template <class T> struct S<T*>;
4074 The second declaration is OK because `T*' uses the template
4075 parameter T, whereas
4077 template <class T> struct S<int>;
4079 is no good. Even trickier is:
4090 The S2<T> declaration is actually invalid; it is a
4091 full-specialization. Of course,
4094 struct S2<T (*)(U)>;
4096 or some such would have been OK. */
4097 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4098 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4099 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4101 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4102 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4103 for (i
= 0; i
< nargs
; ++i
)
4105 tpd
.current_arg
= i
;
4106 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4107 &mark_template_parm
,
4110 /*include_nondeduced_p=*/false);
4112 for (i
= 0; i
< ntparms
; ++i
)
4113 if (tpd
.parms
[i
] == 0)
4115 /* One of the template parms was not used in the
4117 if (!did_error_intro
)
4119 error ("template parameters not used in partial specialization:");
4120 did_error_intro
= true;
4123 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4126 if (did_error_intro
)
4127 return error_mark_node
;
4129 /* [temp.class.spec]
4131 The argument list of the specialization shall not be identical to
4132 the implicit argument list of the primary template. */
4133 if (comp_template_args
4135 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4137 error ("partial specialization %qT does not specialize any template arguments", type
);
4139 /* A partial specialization that replaces multiple parameters of the
4140 primary template with a pack expansion is less specialized for those
4142 if (nargs
< DECL_NTPARMS (maintmpl
))
4144 error ("partial specialization is not more specialized than the "
4145 "primary template because it replaces multiple parameters "
4146 "with a pack expansion");
4147 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4151 /* [temp.class.spec]
4153 A partially specialized non-type argument expression shall not
4154 involve template parameters of the partial specialization except
4155 when the argument expression is a simple identifier.
4157 The type of a template parameter corresponding to a specialized
4158 non-type argument shall not be dependent on a parameter of the
4161 Also, we verify that pack expansions only occur at the
4162 end of the argument list. */
4163 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4165 for (i
= 0; i
< nargs
; ++i
)
4167 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4168 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4169 tree packed_args
= NULL_TREE
;
4172 if (ARGUMENT_PACK_P (arg
))
4174 /* Extract the arguments from the argument pack. We'll be
4175 iterating over these in the following loop. */
4176 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4177 len
= TREE_VEC_LENGTH (packed_args
);
4180 for (j
= 0; j
< len
; j
++)
4183 /* Get the Jth argument in the parameter pack. */
4184 arg
= TREE_VEC_ELT (packed_args
, j
);
4186 if (PACK_EXPANSION_P (arg
))
4188 /* Pack expansions must come at the end of the
4190 if ((packed_args
&& j
< len
- 1)
4191 || (!packed_args
&& i
< nargs
- 1))
4193 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4194 error ("parameter pack argument %qE must be at the "
4195 "end of the template argument list", arg
);
4197 error ("parameter pack argument %qT must be at the "
4198 "end of the template argument list", arg
);
4202 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4203 /* We only care about the pattern. */
4204 arg
= PACK_EXPANSION_PATTERN (arg
);
4206 if (/* These first two lines are the `non-type' bit. */
4208 && TREE_CODE (arg
) != TEMPLATE_DECL
4209 /* This next line is the `argument expression is not just a
4210 simple identifier' condition and also the `specialized
4211 non-type argument' bit. */
4212 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
4214 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4215 || (packed_args
&& uses_template_parms (arg
)))
4216 error ("template argument %qE involves template parameter(s)",
4220 /* Look at the corresponding template parameter,
4221 marking which template parameters its type depends
4223 tree type
= TREE_TYPE (parm
);
4227 /* We haven't yet initialized TPD2. Do so now. */
4228 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4229 /* The number of parameters here is the number in the
4230 main template, which, as checked in the assertion
4232 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4234 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4237 /* Mark the template parameters. But this time, we're
4238 looking for the template parameters of the main
4239 template, not in the specialization. */
4240 tpd2
.current_arg
= i
;
4241 tpd2
.arg_uses_template_parms
[i
] = 0;
4242 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4243 for_each_template_parm (type
,
4244 &mark_template_parm
,
4247 /*include_nondeduced_p=*/false);
4249 if (tpd2
.arg_uses_template_parms
[i
])
4251 /* The type depended on some template parameters.
4252 If they are fully specialized in the
4253 specialization, that's OK. */
4256 for (j
= 0; j
< nargs
; ++j
)
4257 if (tpd2
.parms
[j
] != 0
4258 && tpd
.arg_uses_template_parms
[j
])
4261 error_n (input_location
, count
,
4262 "type %qT of template argument %qE depends "
4263 "on a template parameter",
4264 "type %qT of template argument %qE depends "
4265 "on template parameters",
4274 /* We should only get here once. */
4275 gcc_assert (!COMPLETE_TYPE_P (type
));
4277 tree tmpl
= build_template_decl (decl
, current_template_parms
,
4278 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4279 TREE_TYPE (tmpl
) = type
;
4280 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4281 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4282 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4283 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4285 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4286 = tree_cons (specargs
, tmpl
,
4287 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4288 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4290 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4291 inst
= TREE_CHAIN (inst
))
4293 tree inst_type
= TREE_VALUE (inst
);
4294 if (COMPLETE_TYPE_P (inst_type
)
4295 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type
))
4297 tree spec
= most_specialized_class (inst_type
, maintmpl
, tf_none
);
4298 if (spec
&& TREE_TYPE (spec
) == type
)
4299 permerror (input_location
,
4300 "partial specialization of %qT after instantiation "
4301 "of %qT", type
, inst_type
);
4308 /* Check that a template declaration's use of default arguments and
4309 parameter packs is not invalid. Here, PARMS are the template
4310 parameters. IS_PRIMARY is true if DECL is the thing declared by
4311 a primary template. IS_PARTIAL is true if DECL is a partial
4314 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4315 declaration (but not a definition); 1 indicates a declaration, 2
4316 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4317 emitted for extraneous default arguments.
4319 Returns TRUE if there were no errors found, FALSE otherwise. */
4322 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4323 bool is_partial
, int is_friend_decl
)
4326 int last_level_to_check
;
4328 bool no_errors
= true;
4332 A default template-argument shall not be specified in a
4333 function template declaration or a function template definition, nor
4334 in the template-parameter-list of the definition of a member of a
4337 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
4338 /* You can't have a function template declaration in a local
4339 scope, nor you can you define a member of a class template in a
4343 if (TREE_CODE (decl
) == TYPE_DECL
4345 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4346 /* A lambda doesn't have an explicit declaration; don't complain
4347 about the parms of the enclosing class. */
4350 if (current_class_type
4351 && !TYPE_BEING_DEFINED (current_class_type
)
4352 && DECL_LANG_SPECIFIC (decl
)
4353 && DECL_DECLARES_FUNCTION_P (decl
)
4354 /* If this is either a friend defined in the scope of the class
4355 or a member function. */
4356 && (DECL_FUNCTION_MEMBER_P (decl
)
4357 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4358 : DECL_FRIEND_CONTEXT (decl
)
4359 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4361 /* And, if it was a member function, it really was defined in
4362 the scope of the class. */
4363 && (!DECL_FUNCTION_MEMBER_P (decl
)
4364 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4365 /* We already checked these parameters when the template was
4366 declared, so there's no need to do it again now. This function
4367 was defined in class scope, but we're processing its body now
4368 that the class is complete. */
4371 /* Core issue 226 (C++0x only): the following only applies to class
4374 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
4378 If a template-parameter has a default template-argument, all
4379 subsequent template-parameters shall have a default
4380 template-argument supplied. */
4381 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4383 tree inner_parms
= TREE_VALUE (parm_level
);
4384 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4385 int seen_def_arg_p
= 0;
4388 for (i
= 0; i
< ntparms
; ++i
)
4390 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4392 if (parm
== error_mark_node
)
4395 if (TREE_PURPOSE (parm
))
4397 else if (seen_def_arg_p
4398 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4400 error ("no default argument for %qD", TREE_VALUE (parm
));
4401 /* For better subsequent error-recovery, we indicate that
4402 there should have been a default argument. */
4403 TREE_PURPOSE (parm
) = error_mark_node
;
4406 else if (!is_partial
4408 /* Don't complain about an enclosing partial
4410 && parm_level
== parms
4411 && TREE_CODE (decl
) == TYPE_DECL
4413 && template_parameter_pack_p (TREE_VALUE (parm
)))
4415 /* A primary class template can only have one
4416 parameter pack, at the end of the template
4419 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
4420 error ("parameter pack %qE must be at the end of the"
4421 " template parameter list", TREE_VALUE (parm
));
4423 error ("parameter pack %qT must be at the end of the"
4424 " template parameter list",
4425 TREE_TYPE (TREE_VALUE (parm
)));
4427 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4435 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4439 /* For an ordinary class template, default template arguments are
4440 allowed at the innermost level, e.g.:
4441 template <class T = int>
4443 but, in a partial specialization, they're not allowed even
4444 there, as we have in [temp.class.spec]:
4446 The template parameter list of a specialization shall not
4447 contain default template argument values.
4449 So, for a partial specialization, or for a function template
4450 (in C++98/C++03), we look at all of them. */
4453 /* But, for a primary class template that is not a partial
4454 specialization we look at all template parameters except the
4456 parms
= TREE_CHAIN (parms
);
4458 /* Figure out what error message to issue. */
4459 if (is_friend_decl
== 2)
4460 msg
= G_("default template arguments may not be used in function template "
4461 "friend re-declaration");
4462 else if (is_friend_decl
)
4463 msg
= G_("default template arguments may not be used in function template "
4464 "friend declarations");
4465 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4466 msg
= G_("default template arguments may not be used in function templates "
4467 "without -std=c++11 or -std=gnu++11");
4468 else if (is_partial
)
4469 msg
= G_("default template arguments may not be used in "
4470 "partial specializations");
4472 msg
= G_("default argument for template parameter for class enclosing %qD");
4474 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4475 /* If we're inside a class definition, there's no need to
4476 examine the parameters to the class itself. On the one
4477 hand, they will be checked when the class is defined, and,
4478 on the other, default arguments are valid in things like:
4479 template <class T = double>
4480 struct S { template <class U> void f(U); };
4481 Here the default argument for `S' has no bearing on the
4482 declaration of `f'. */
4483 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4485 /* Check everything. */
4486 last_level_to_check
= 0;
4488 for (parm_level
= parms
;
4489 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4490 parm_level
= TREE_CHAIN (parm_level
))
4492 tree inner_parms
= TREE_VALUE (parm_level
);
4496 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4497 for (i
= 0; i
< ntparms
; ++i
)
4499 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4502 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4507 if (is_friend_decl
== 2)
4514 /* Clear out the default argument so that we are not
4516 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4520 /* At this point, if we're still interested in issuing messages,
4521 they must apply to classes surrounding the object declared. */
4523 msg
= G_("default argument for template parameter for class "
4530 /* Worker for push_template_decl_real, called via
4531 for_each_template_parm. DATA is really an int, indicating the
4532 level of the parameters we are interested in. If T is a template
4533 parameter of that level, return nonzero. */
4536 template_parm_this_level_p (tree t
, void* data
)
4538 int this_level
= *(int *)data
;
4541 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4542 level
= TEMPLATE_PARM_LEVEL (t
);
4544 level
= TEMPLATE_TYPE_LEVEL (t
);
4545 return level
== this_level
;
4548 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4549 parameters given by current_template_args, or reuses a
4550 previously existing one, if appropriate. Returns the DECL, or an
4551 equivalent one, if it is replaced via a call to duplicate_decls.
4553 If IS_FRIEND is true, DECL is a friend declaration. */
4556 push_template_decl_real (tree decl
, bool is_friend
)
4564 int new_template_p
= 0;
4565 /* True if the template is a member template, in the sense of
4567 bool member_template_p
= false;
4569 if (decl
== error_mark_node
|| !current_template_parms
)
4570 return error_mark_node
;
4572 /* See if this is a partial specialization. */
4573 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
4574 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4575 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
4577 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4581 /* For a friend, we want the context of the friend function, not
4582 the type of which it is a friend. */
4583 ctx
= CP_DECL_CONTEXT (decl
);
4584 else if (CP_DECL_CONTEXT (decl
)
4585 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4586 /* In the case of a virtual function, we want the class in which
4588 ctx
= CP_DECL_CONTEXT (decl
);
4590 /* Otherwise, if we're currently defining some class, the DECL
4591 is assumed to be a member of the class. */
4592 ctx
= current_scope ();
4594 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4597 if (!DECL_CONTEXT (decl
))
4598 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4600 /* See if this is a primary template. */
4601 if (is_friend
&& ctx
)
4602 /* A friend template that specifies a class context, i.e.
4603 template <typename T> friend void A<T>::f();
4607 is_primary
= template_parm_scope_p ();
4611 if (DECL_CLASS_SCOPE_P (decl
))
4612 member_template_p
= true;
4613 if (TREE_CODE (decl
) == TYPE_DECL
4614 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4616 error ("template class without a name");
4617 return error_mark_node
;
4619 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4621 if (DECL_DESTRUCTOR_P (decl
))
4625 A destructor shall not be a member template. */
4626 error ("destructor %qD declared as member template", decl
);
4627 return error_mark_node
;
4629 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4630 && (!prototype_p (TREE_TYPE (decl
))
4631 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4632 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4633 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4634 == void_list_node
)))
4636 /* [basic.stc.dynamic.allocation]
4638 An allocation function can be a function
4639 template. ... Template allocation functions shall
4640 have two or more parameters. */
4641 error ("invalid template declaration of %qD", decl
);
4642 return error_mark_node
;
4645 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4646 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4648 else if (TREE_CODE (decl
) == TYPE_DECL
4649 && TYPE_DECL_ALIAS_P (decl
))
4650 /* alias-declaration */
4651 gcc_assert (!DECL_ARTIFICIAL (decl
));
4654 error ("template declaration of %q#D", decl
);
4655 return error_mark_node
;
4659 /* Check to see that the rules regarding the use of default
4660 arguments are not being violated. */
4661 check_default_tmpl_args (decl
, current_template_parms
,
4662 is_primary
, is_partial
, /*is_friend_decl=*/0);
4664 /* Ensure that there are no parameter packs in the type of this
4665 declaration that have not been expanded. */
4666 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4668 /* Check each of the arguments individually to see if there are
4669 any bare parameter packs. */
4670 tree type
= TREE_TYPE (decl
);
4671 tree arg
= DECL_ARGUMENTS (decl
);
4672 tree argtype
= TYPE_ARG_TYPES (type
);
4674 while (arg
&& argtype
)
4676 if (!DECL_PACK_P (arg
)
4677 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4679 /* This is a PARM_DECL that contains unexpanded parameter
4680 packs. We have already complained about this in the
4681 check_for_bare_parameter_packs call, so just replace
4682 these types with ERROR_MARK_NODE. */
4683 TREE_TYPE (arg
) = error_mark_node
;
4684 TREE_VALUE (argtype
) = error_mark_node
;
4687 arg
= DECL_CHAIN (arg
);
4688 argtype
= TREE_CHAIN (argtype
);
4691 /* Check for bare parameter packs in the return type and the
4692 exception specifiers. */
4693 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4694 /* Errors were already issued, set return type to int
4695 as the frontend doesn't expect error_mark_node as
4697 TREE_TYPE (type
) = integer_type_node
;
4698 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4699 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4701 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
4702 && TYPE_DECL_ALIAS_P (decl
))
4703 ? DECL_ORIGINAL_TYPE (decl
)
4704 : TREE_TYPE (decl
)))
4706 TREE_TYPE (decl
) = error_mark_node
;
4707 return error_mark_node
;
4711 return process_partial_specialization (decl
);
4713 args
= current_template_args ();
4716 || TREE_CODE (ctx
) == FUNCTION_DECL
4717 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4718 || (TREE_CODE (decl
) == TYPE_DECL
4719 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4720 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4722 if (DECL_LANG_SPECIFIC (decl
)
4723 && DECL_TEMPLATE_INFO (decl
)
4724 && DECL_TI_TEMPLATE (decl
))
4725 tmpl
= DECL_TI_TEMPLATE (decl
);
4726 /* If DECL is a TYPE_DECL for a class-template, then there won't
4727 be DECL_LANG_SPECIFIC. The information equivalent to
4728 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4729 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4730 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4731 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4733 /* Since a template declaration already existed for this
4734 class-type, we must be redeclaring it here. Make sure
4735 that the redeclaration is valid. */
4736 redeclare_class_template (TREE_TYPE (decl
),
4737 current_template_parms
);
4738 /* We don't need to create a new TEMPLATE_DECL; just use the
4739 one we already had. */
4740 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4744 tmpl
= build_template_decl (decl
, current_template_parms
,
4748 if (DECL_LANG_SPECIFIC (decl
)
4749 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4751 /* A specialization of a member template of a template
4753 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4754 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4755 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4761 tree a
, t
, current
, parms
;
4763 tree tinfo
= get_template_info (decl
);
4767 error ("template definition of non-template %q#D", decl
);
4768 return error_mark_node
;
4771 tmpl
= TI_TEMPLATE (tinfo
);
4773 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4774 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4775 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4776 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4780 /* The declaration is a specialization of a member
4781 template, declared outside the class. Therefore, the
4782 innermost template arguments will be NULL, so we
4783 replace them with the arguments determined by the
4784 earlier call to check_explicit_specialization. */
4785 args
= DECL_TI_ARGS (decl
);
4788 = build_template_decl (decl
, current_template_parms
,
4790 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4791 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4792 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4793 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4794 DECL_TEMPLATE_INFO (new_tmpl
)
4795 = build_template_info (tmpl
, args
);
4797 register_specialization (new_tmpl
,
4798 most_general_template (tmpl
),
4804 /* Make sure the template headers we got make sense. */
4806 parms
= DECL_TEMPLATE_PARMS (tmpl
);
4807 i
= TMPL_PARMS_DEPTH (parms
);
4808 if (TMPL_ARGS_DEPTH (args
) != i
)
4810 error ("expected %d levels of template parms for %q#D, got %d",
4811 i
, decl
, TMPL_ARGS_DEPTH (args
));
4814 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
4816 a
= TMPL_ARGS_LEVEL (args
, i
);
4817 t
= INNERMOST_TEMPLATE_PARMS (parms
);
4819 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
4821 if (current
== decl
)
4822 error ("got %d template parameters for %q#D",
4823 TREE_VEC_LENGTH (a
), decl
);
4825 error ("got %d template parameters for %q#T",
4826 TREE_VEC_LENGTH (a
), current
);
4827 error (" but %d required", TREE_VEC_LENGTH (t
));
4828 /* Avoid crash in import_export_decl. */
4829 DECL_INTERFACE_KNOWN (decl
) = 1;
4830 return error_mark_node
;
4833 if (current
== decl
)
4835 else if (current
== NULL_TREE
)
4836 /* Can happen in erroneous input. */
4839 current
= get_containing_scope (current
);
4842 /* Check that the parms are used in the appropriate qualifying scopes
4843 in the declarator. */
4844 if (!comp_template_args
4846 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
4849 template arguments to %qD do not match original template %qD",
4850 decl
, DECL_TEMPLATE_RESULT (tmpl
));
4851 if (!uses_template_parms (TI_ARGS (tinfo
)))
4852 inform (input_location
, "use template<> for an explicit specialization");
4853 /* Avoid crash in import_export_decl. */
4854 DECL_INTERFACE_KNOWN (decl
) = 1;
4855 return error_mark_node
;
4859 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4860 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4862 /* Push template declarations for global functions and types. Note
4863 that we do not try to push a global template friend declared in a
4864 template class; such a thing may well depend on the template
4865 parameters of the class. */
4866 if (new_template_p
&& !ctx
4867 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
4869 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
4870 if (tmpl
== error_mark_node
)
4871 return error_mark_node
;
4873 /* Hide template friend classes that haven't been declared yet. */
4874 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
4876 DECL_ANTICIPATED (tmpl
) = 1;
4877 DECL_FRIEND_P (tmpl
) = 1;
4883 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
4886 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4887 if (DECL_CONV_FN_P (tmpl
))
4889 int depth
= TMPL_PARMS_DEPTH (parms
);
4891 /* It is a conversion operator. See if the type converted to
4892 depends on innermost template operands. */
4894 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
4896 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
4899 /* Give template template parms a DECL_CONTEXT of the template
4900 for which they are a parameter. */
4901 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4902 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
4904 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4905 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4906 DECL_CONTEXT (parm
) = tmpl
;
4910 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4911 back to its most general template. If TMPL is a specialization,
4912 ARGS may only have the innermost set of arguments. Add the missing
4913 argument levels if necessary. */
4914 if (DECL_TEMPLATE_INFO (tmpl
))
4915 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
4917 info
= build_template_info (tmpl
, args
);
4919 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
4920 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
4923 if (is_primary
&& !DECL_LANG_SPECIFIC (decl
))
4924 retrofit_lang_decl (decl
);
4925 if (DECL_LANG_SPECIFIC (decl
))
4926 DECL_TEMPLATE_INFO (decl
) = info
;
4929 return DECL_TEMPLATE_RESULT (tmpl
);
4933 push_template_decl (tree decl
)
4935 return push_template_decl_real (decl
, false);
4938 /* FN is an inheriting constructor that inherits from the constructor
4939 template INHERITED; turn FN into a constructor template with a matching
4943 add_inherited_template_parms (tree fn
, tree inherited
)
4946 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
4947 inner_parms
= copy_node (inner_parms
);
4949 = tree_cons (size_int (processing_template_decl
+ 1),
4950 inner_parms
, current_template_parms
);
4951 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
4952 tree args
= template_parms_to_args (parms
);
4953 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
4954 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
4955 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
4956 DECL_ARTIFICIAL (tmpl
) = true;
4957 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4961 /* Called when a class template TYPE is redeclared with the indicated
4962 template PARMS, e.g.:
4964 template <class T> struct S;
4965 template <class T> struct S {}; */
4968 redeclare_class_template (tree type
, tree parms
)
4974 if (!TYPE_TEMPLATE_INFO (type
))
4976 error ("%qT is not a template type", type
);
4980 tmpl
= TYPE_TI_TEMPLATE (type
);
4981 if (!PRIMARY_TEMPLATE_P (tmpl
))
4982 /* The type is nested in some template class. Nothing to worry
4983 about here; there are no new template parameters for the nested
4989 error ("template specifiers not specified in declaration of %qD",
4994 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4995 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
4997 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
4999 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5000 "redeclared with %d template parameter",
5001 "redeclared with %d template parameters",
5002 TREE_VEC_LENGTH (parms
));
5003 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
5004 "previous declaration %q+D used %d template parameter",
5005 "previous declaration %q+D used %d template parameters",
5006 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5010 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5017 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5018 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5021 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5022 if (tmpl_parm
== error_mark_node
)
5025 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5026 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5027 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5029 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5031 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5032 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5033 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5034 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5035 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5036 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5037 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5038 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5039 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5041 error ("template parameter %q+#D", tmpl_parm
);
5042 error ("redeclared here as %q#D", parm
);
5046 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5048 /* We have in [temp.param]:
5050 A template-parameter may not be given default arguments
5051 by two different declarations in the same scope. */
5052 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5053 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5054 "original definition appeared here");
5058 if (parm_default
!= NULL_TREE
)
5059 /* Update the previous template parameters (which are the ones
5060 that will really count) with the new default value. */
5061 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5062 else if (tmpl_default
!= NULL_TREE
)
5063 /* Update the new parameters, too; they'll be used as the
5064 parameters for any members. */
5065 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5071 /* Simplify EXPR if it is a non-dependent expression. Returns the
5072 (possibly simplified) expression. */
5075 fold_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5077 if (expr
== NULL_TREE
)
5080 /* If we're in a template, but EXPR isn't value dependent, simplify
5081 it. We're supposed to treat:
5083 template <typename T> void f(T[1 + 1]);
5084 template <typename T> void f(T[2]);
5086 as two declarations of the same function, for example. */
5087 if (processing_template_decl
5088 && !instantiation_dependent_expression_p (expr
)
5089 && potential_constant_expression (expr
))
5091 HOST_WIDE_INT saved_processing_template_decl
;
5093 saved_processing_template_decl
= processing_template_decl
;
5094 processing_template_decl
= 0;
5095 expr
= tsubst_copy_and_build (expr
,
5098 /*in_decl=*/NULL_TREE
,
5099 /*function_p=*/false,
5100 /*integral_constant_expression_p=*/true);
5101 processing_template_decl
= saved_processing_template_decl
;
5107 fold_non_dependent_expr (tree expr
)
5109 return fold_non_dependent_expr_sfinae (expr
, tf_error
);
5112 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5113 template declaration, or a TYPE_DECL for an alias declaration. */
5116 alias_type_or_template_p (tree t
)
5120 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5123 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5124 || DECL_ALIAS_TEMPLATE_P (t
));
5127 /* Return TRUE iff is a specialization of an alias template. */
5130 alias_template_specialization_p (const_tree t
)
5136 && TYPE_TEMPLATE_INFO (t
)
5137 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t
))
5138 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t
)));
5141 /* Return either TMPL or another template that it is equivalent to under DR
5142 1286: An alias that just changes the name of a template is equivalent to
5143 the other template. */
5146 get_underlying_template (tree tmpl
)
5148 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
5149 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
5151 tree result
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5152 if (TYPE_TEMPLATE_INFO (result
))
5154 tree sub
= TYPE_TI_TEMPLATE (result
);
5155 if (PRIMARY_TEMPLATE_P (sub
)
5156 && same_type_p (result
, TREE_TYPE (sub
)))
5158 /* The alias type is equivalent to the pattern of the
5159 underlying template, so strip the alias. */
5169 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5170 must be a function or a pointer-to-function type, as specified
5171 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5172 and check that the resulting function has external linkage. */
5175 convert_nontype_argument_function (tree type
, tree expr
)
5179 linkage_kind linkage
;
5181 fn
= instantiate_type (type
, fns
, tf_none
);
5182 if (fn
== error_mark_node
)
5183 return error_mark_node
;
5186 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5187 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5188 if (BASELINK_P (fn_no_ptr
))
5189 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5191 /* [temp.arg.nontype]/1
5193 A template-argument for a non-type, non-template template-parameter
5196 -- the address of an object or function with external [C++11: or
5197 internal] linkage. */
5199 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
5201 error ("%qE is not a valid template argument for type %qT", expr
, type
);
5202 if (TYPE_PTR_P (type
))
5203 error ("it must be the address of a function with external linkage");
5205 error ("it must be the name of a function with external linkage");
5209 linkage
= decl_linkage (fn_no_ptr
);
5210 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
5212 if (cxx_dialect
>= cxx11
)
5213 error ("%qE is not a valid template argument for type %qT "
5214 "because %qD has no linkage",
5215 expr
, type
, fn_no_ptr
);
5217 error ("%qE is not a valid template argument for type %qT "
5218 "because %qD does not have external linkage",
5219 expr
, type
, fn_no_ptr
);
5226 /* Subroutine of convert_nontype_argument.
5227 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5228 Emit an error otherwise. */
5231 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5232 tsubst_flags_t complain
)
5235 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5237 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
5239 if (complain
& tf_error
)
5241 error ("%qE is not a valid template argument for type %qT",
5243 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5248 /* Returns TRUE iff the address of OP is value-dependent.
5250 14.6.2.4 [temp.dep.temp]:
5251 A non-integral non-type template-argument is dependent if its type is
5252 dependent or it has either of the following forms
5255 and contains a nested-name-specifier which specifies a class-name that
5256 names a dependent type.
5258 We generalize this to just say that the address of a member of a
5259 dependent class is value-dependent; the above doesn't cover the
5260 address of a static data member named with an unqualified-id. */
5263 has_value_dependent_address (tree op
)
5265 /* We could use get_inner_reference here, but there's no need;
5266 this is only relevant for template non-type arguments, which
5267 can only be expressed as &id-expression. */
5270 tree ctx
= CP_DECL_CONTEXT (op
);
5271 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5278 /* The next set of functions are used for providing helpful explanatory
5279 diagnostics for failed overload resolution. Their messages should be
5280 indented by two spaces for consistency with the messages in
5284 unify_success (bool /*explain_p*/)
5290 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5293 inform (input_location
,
5294 " couldn't deduce template parameter %qD", parm
);
5299 unify_invalid (bool /*explain_p*/)
5305 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5308 inform (input_location
,
5309 " types %qT and %qT have incompatible cv-qualifiers",
5315 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5318 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5323 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
5326 inform (input_location
,
5327 " template parameter %qD is not a parameter pack, but "
5334 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
5337 inform (input_location
,
5338 " template argument %qE does not match "
5339 "pointer-to-member constant %qE",
5345 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
5348 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
5353 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
5356 inform (input_location
,
5357 " inconsistent parameter pack deduction with %qT and %qT",
5363 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
5368 inform (input_location
,
5369 " deduced conflicting types for parameter %qT (%qT and %qT)",
5370 parm
, first
, second
);
5372 inform (input_location
,
5373 " deduced conflicting values for non-type parameter "
5374 "%qE (%qE and %qE)", parm
, first
, second
);
5380 unify_vla_arg (bool explain_p
, tree arg
)
5383 inform (input_location
,
5384 " variable-sized array type %qT is not "
5385 "a valid template argument",
5391 unify_method_type_error (bool explain_p
, tree arg
)
5394 inform (input_location
,
5395 " member function type %qT is not a valid template argument",
5401 unify_arity (bool explain_p
, int have
, int wanted
)
5404 inform_n (input_location
, wanted
,
5405 " candidate expects %d argument, %d provided",
5406 " candidate expects %d arguments, %d provided",
5412 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
5414 return unify_arity (explain_p
, have
, wanted
);
5418 unify_too_few_arguments (bool explain_p
, int have
, int wanted
)
5420 return unify_arity (explain_p
, have
, wanted
);
5424 unify_arg_conversion (bool explain_p
, tree to_type
,
5425 tree from_type
, tree arg
)
5428 inform (EXPR_LOC_OR_HERE (arg
),
5429 " cannot convert %qE (type %qT) to type %qT",
5430 arg
, from_type
, to_type
);
5435 unify_no_common_base (bool explain_p
, enum template_base_result r
,
5436 tree parm
, tree arg
)
5441 case tbr_ambiguous_baseclass
:
5442 inform (input_location
, " %qT is an ambiguous base class of %qT",
5446 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
5453 unify_inconsistent_template_template_parameters (bool explain_p
)
5456 inform (input_location
,
5457 " template parameters of a template template argument are "
5458 "inconsistent with other deduced template arguments");
5463 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
5466 inform (input_location
,
5467 " can't deduce a template for %qT from non-template type %qT",
5473 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
5476 inform (input_location
,
5477 " template argument %qE does not match %qD", arg
, parm
);
5482 unify_overload_resolution_failure (bool explain_p
, tree arg
)
5485 inform (input_location
,
5486 " could not resolve address from overloaded function %qE",
5491 /* Attempt to convert the non-type template parameter EXPR to the
5492 indicated TYPE. If the conversion is successful, return the
5493 converted value. If the conversion is unsuccessful, return
5494 NULL_TREE if we issued an error message, or error_mark_node if we
5495 did not. We issue error messages for out-and-out bad template
5496 parameters, but not simply because the conversion failed, since we
5497 might be just trying to do argument deduction. Both TYPE and EXPR
5498 must be non-dependent.
5500 The conversion follows the special rules described in
5501 [temp.arg.nontype], and it is much more strict than an implicit
5504 This function is called twice for each template argument (see
5505 lookup_template_class for a more accurate description of this
5506 problem). This means that we need to handle expressions which
5507 are not valid in a C++ source, but can be created from the
5508 first call (for instance, casts to perform conversions). These
5509 hacks can go away after we fix the double coercion problem. */
5512 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5516 /* Detect immediately string literals as invalid non-type argument.
5517 This special-case is not needed for correctness (we would easily
5518 catch this later), but only to provide better diagnostic for this
5519 common user mistake. As suggested by DR 100, we do not mention
5520 linkage issues in the diagnostic as this is not the point. */
5521 /* FIXME we're making this OK. */
5522 if (TREE_CODE (expr
) == STRING_CST
)
5524 if (complain
& tf_error
)
5525 error ("%qE is not a valid template argument for type %qT "
5526 "because string literals can never be used in this context",
5531 /* Add the ADDR_EXPR now for the benefit of
5532 value_dependent_expression_p. */
5533 if (TYPE_PTROBV_P (type
)
5534 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
5536 expr
= decay_conversion (expr
, complain
);
5537 if (expr
== error_mark_node
)
5538 return error_mark_node
;
5541 /* If we are in a template, EXPR may be non-dependent, but still
5542 have a syntactic, rather than semantic, form. For example, EXPR
5543 might be a SCOPE_REF, rather than the VAR_DECL to which the
5544 SCOPE_REF refers. Preserving the qualifying scope is necessary
5545 so that access checking can be performed when the template is
5546 instantiated -- but here we need the resolved form so that we can
5547 convert the argument. */
5548 if (TYPE_REF_OBJ_P (type
)
5549 && has_value_dependent_address (expr
))
5550 /* If we want the address and it's value-dependent, don't fold. */;
5551 else if (!type_unknown_p (expr
))
5552 expr
= fold_non_dependent_expr_sfinae (expr
, complain
);
5553 if (error_operand_p (expr
))
5554 return error_mark_node
;
5555 expr_type
= TREE_TYPE (expr
);
5556 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5557 expr
= mark_lvalue_use (expr
);
5559 expr
= mark_rvalue_use (expr
);
5561 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5562 to a non-type argument of "nullptr". */
5563 if (expr
== nullptr_node
&& TYPE_PTR_OR_PTRMEM_P (type
))
5564 expr
= convert (type
, expr
);
5566 /* In C++11, integral or enumeration non-type template arguments can be
5567 arbitrary constant expressions. Pointer and pointer to
5568 member arguments can be general constant expressions that evaluate
5569 to a null value, but otherwise still need to be of a specific form. */
5570 if (cxx_dialect
>= cxx11
)
5572 if (TREE_CODE (expr
) == PTRMEM_CST
)
5573 /* A PTRMEM_CST is already constant, and a valid template
5574 argument for a parameter of pointer to member type, we just want
5575 to leave it in that form rather than lower it to a
5577 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5578 expr
= maybe_constant_value (expr
);
5579 else if (TYPE_PTR_OR_PTRMEM_P (type
))
5581 tree folded
= maybe_constant_value (expr
);
5582 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
5583 : null_member_pointer_value_p (folded
))
5588 /* HACK: Due to double coercion, we can get a
5589 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5590 which is the tree that we built on the first call (see
5591 below when coercing to reference to object or to reference to
5592 function). We just strip everything and get to the arg.
5593 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5595 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5597 tree probe_type
, probe
= expr
;
5598 if (REFERENCE_REF_P (probe
))
5599 probe
= TREE_OPERAND (probe
, 0);
5600 probe_type
= TREE_TYPE (probe
);
5601 if (TREE_CODE (probe
) == NOP_EXPR
)
5603 /* ??? Maybe we could use convert_from_reference here, but we
5604 would need to relax its constraints because the NOP_EXPR
5605 could actually change the type to something more cv-qualified,
5606 and this is not folded by convert_from_reference. */
5607 tree addr
= TREE_OPERAND (probe
, 0);
5608 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
5609 && TREE_CODE (addr
) == ADDR_EXPR
5610 && TYPE_PTR_P (TREE_TYPE (addr
))
5611 && (same_type_ignoring_top_level_qualifiers_p
5612 (TREE_TYPE (probe_type
),
5613 TREE_TYPE (TREE_TYPE (addr
)))))
5615 expr
= TREE_OPERAND (addr
, 0);
5616 expr_type
= TREE_TYPE (probe_type
);
5621 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5622 parameter is a pointer to object, through decay and
5623 qualification conversion. Let's strip everything. */
5624 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
5628 if (TREE_CODE (probe
) == ADDR_EXPR
5629 && TYPE_PTR_P (TREE_TYPE (probe
)))
5631 /* Skip the ADDR_EXPR only if it is part of the decay for
5632 an array. Otherwise, it is part of the original argument
5633 in the source code. */
5634 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe
, 0))) == ARRAY_TYPE
)
5635 probe
= TREE_OPERAND (probe
, 0);
5637 expr_type
= TREE_TYPE (expr
);
5641 /* [temp.arg.nontype]/5, bullet 1
5643 For a non-type template-parameter of integral or enumeration type,
5644 integral promotions (_conv.prom_) and integral conversions
5645 (_conv.integral_) are applied. */
5646 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5648 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
5649 t
= maybe_constant_value (t
);
5650 if (t
!= error_mark_node
)
5653 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
5654 return error_mark_node
;
5656 /* Notice that there are constant expressions like '4 % 0' which
5657 do not fold into integer constants. */
5658 if (TREE_CODE (expr
) != INTEGER_CST
)
5660 if (complain
& tf_error
)
5662 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
5663 if (processing_template_decl
5664 && !require_potential_constant_expression (expr
))
5666 expr
= cxx_constant_value (expr
);
5667 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
5668 inform (EXPR_LOC_OR_HERE (expr
),
5669 "in template argument for type %qT ", type
);
5670 if (expr
== error_mark_node
)
5672 /* else cxx_constant_value complained but gave us
5673 a real constant, so go ahead. */
5674 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
5680 /* Avoid typedef problems. */
5681 if (TREE_TYPE (expr
) != type
)
5682 expr
= fold_convert (type
, expr
);
5684 /* [temp.arg.nontype]/5, bullet 2
5686 For a non-type template-parameter of type pointer to object,
5687 qualification conversions (_conv.qual_) and the array-to-pointer
5688 conversion (_conv.array_) are applied. */
5689 else if (TYPE_PTROBV_P (type
))
5691 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5693 A template-argument for a non-type, non-template template-parameter
5694 shall be one of: [...]
5696 -- the name of a non-type template-parameter;
5697 -- the address of an object or function with external linkage, [...]
5698 expressed as "& id-expression" where the & is optional if the name
5699 refers to a function or array, or if the corresponding
5700 template-parameter is a reference.
5702 Here, we do not care about functions, as they are invalid anyway
5703 for a parameter of type pointer-to-object. */
5705 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5706 /* Non-type template parameters are OK. */
5708 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
5709 /* Null pointer values are OK in C++11. */;
5710 else if (TREE_CODE (expr
) != ADDR_EXPR
5711 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5715 error ("%qD is not a valid template argument "
5716 "because %qD is a variable, not the address of "
5721 if (POINTER_TYPE_P (expr_type
))
5723 error ("%qE is not a valid template argument for %qT "
5724 "because it is not the address of a variable",
5728 /* Other values, like integer constants, might be valid
5729 non-type arguments of some other type. */
5730 return error_mark_node
;
5736 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
5737 ? TREE_OPERAND (expr
, 0) : expr
);
5740 error ("%qE is not a valid template argument of type %qT "
5741 "because %qE is not a variable",
5745 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
5747 error ("%qE is not a valid template argument of type %qT "
5748 "because %qD does not have external linkage",
5752 else if (cxx_dialect
>= cxx11
&& decl_linkage (decl
) == lk_none
)
5754 error ("%qE is not a valid template argument of type %qT "
5755 "because %qD has no linkage",
5761 expr
= decay_conversion (expr
, complain
);
5762 if (expr
== error_mark_node
)
5763 return error_mark_node
;
5765 expr
= perform_qualification_conversions (type
, expr
);
5766 if (expr
== error_mark_node
)
5767 return error_mark_node
;
5769 /* [temp.arg.nontype]/5, bullet 3
5771 For a non-type template-parameter of type reference to object, no
5772 conversions apply. The type referred to by the reference may be more
5773 cv-qualified than the (otherwise identical) type of the
5774 template-argument. The template-parameter is bound directly to the
5775 template-argument, which must be an lvalue. */
5776 else if (TYPE_REF_OBJ_P (type
))
5778 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
5780 return error_mark_node
;
5782 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5784 error ("%qE is not a valid template argument for type %qT "
5785 "because of conflicts in cv-qualification", expr
, type
);
5789 if (!real_lvalue_p (expr
))
5791 error ("%qE is not a valid template argument for type %qT "
5792 "because it is not an lvalue", expr
, type
);
5796 /* [temp.arg.nontype]/1
5798 A template-argument for a non-type, non-template template-parameter
5799 shall be one of: [...]
5801 -- the address of an object or function with external linkage. */
5802 if (INDIRECT_REF_P (expr
)
5803 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5805 expr
= TREE_OPERAND (expr
, 0);
5808 error ("%q#D is not a valid template argument for type %qT "
5809 "because a reference variable does not have a constant "
5810 "address", expr
, type
);
5817 error ("%qE is not a valid template argument for type %qT "
5818 "because it is not an object with external linkage",
5823 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5825 error ("%qE is not a valid template argument for type %qT "
5826 "because object %qD has not external linkage",
5831 expr
= build_nop (type
, build_address (expr
));
5833 /* [temp.arg.nontype]/5, bullet 4
5835 For a non-type template-parameter of type pointer to function, only
5836 the function-to-pointer conversion (_conv.func_) is applied. If the
5837 template-argument represents a set of overloaded functions (or a
5838 pointer to such), the matching function is selected from the set
5840 else if (TYPE_PTRFN_P (type
))
5842 /* If the argument is a template-id, we might not have enough
5843 context information to decay the pointer. */
5844 if (!type_unknown_p (expr_type
))
5846 expr
= decay_conversion (expr
, complain
);
5847 if (expr
== error_mark_node
)
5848 return error_mark_node
;
5851 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
5852 /* Null pointer values are OK in C++11. */
5853 return perform_qualification_conversions (type
, expr
);
5855 expr
= convert_nontype_argument_function (type
, expr
);
5856 if (!expr
|| expr
== error_mark_node
)
5859 /* [temp.arg.nontype]/5, bullet 5
5861 For a non-type template-parameter of type reference to function, no
5862 conversions apply. If the template-argument represents a set of
5863 overloaded functions, the matching function is selected from the set
5865 else if (TYPE_REFFN_P (type
))
5867 if (TREE_CODE (expr
) == ADDR_EXPR
)
5869 error ("%qE is not a valid template argument for type %qT "
5870 "because it is a pointer", expr
, type
);
5871 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
5875 expr
= convert_nontype_argument_function (type
, expr
);
5876 if (!expr
|| expr
== error_mark_node
)
5879 expr
= build_nop (type
, build_address (expr
));
5881 /* [temp.arg.nontype]/5, bullet 6
5883 For a non-type template-parameter of type pointer to member function,
5884 no conversions apply. If the template-argument represents a set of
5885 overloaded member functions, the matching member function is selected
5886 from the set (_over.over_). */
5887 else if (TYPE_PTRMEMFUNC_P (type
))
5889 expr
= instantiate_type (type
, expr
, tf_none
);
5890 if (expr
== error_mark_node
)
5891 return error_mark_node
;
5893 /* [temp.arg.nontype] bullet 1 says the pointer to member
5894 expression must be a pointer-to-member constant. */
5895 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5896 return error_mark_node
;
5898 /* There is no way to disable standard conversions in
5899 resolve_address_of_overloaded_function (called by
5900 instantiate_type). It is possible that the call succeeded by
5901 converting &B::I to &D::I (where B is a base of D), so we need
5902 to reject this conversion here.
5904 Actually, even if there was a way to disable standard conversions,
5905 it would still be better to reject them here so that we can
5906 provide a superior diagnostic. */
5907 if (!same_type_p (TREE_TYPE (expr
), type
))
5909 error ("%qE is not a valid template argument for type %qT "
5910 "because it is of type %qT", expr
, type
,
5912 /* If we are just one standard conversion off, explain. */
5913 if (can_convert_standard (type
, TREE_TYPE (expr
), complain
))
5914 inform (input_location
,
5915 "standard conversions are not allowed in this context");
5919 /* [temp.arg.nontype]/5, bullet 7
5921 For a non-type template-parameter of type pointer to data member,
5922 qualification conversions (_conv.qual_) are applied. */
5923 else if (TYPE_PTRDATAMEM_P (type
))
5925 /* [temp.arg.nontype] bullet 1 says the pointer to member
5926 expression must be a pointer-to-member constant. */
5927 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5928 return error_mark_node
;
5930 expr
= perform_qualification_conversions (type
, expr
);
5931 if (expr
== error_mark_node
)
5934 else if (NULLPTR_TYPE_P (type
))
5936 if (expr
!= nullptr_node
)
5938 error ("%qE is not a valid template argument for type %qT "
5939 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
5944 /* A template non-type parameter must be one of the above. */
5948 /* Sanity check: did we actually convert the argument to the
5950 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5951 (type
, TREE_TYPE (expr
)));
5955 /* Subroutine of coerce_template_template_parms, which returns 1 if
5956 PARM_PARM and ARG_PARM match using the rule for the template
5957 parameters of template template parameters. Both PARM and ARG are
5958 template parameters; the rest of the arguments are the same as for
5959 coerce_template_template_parms.
5962 coerce_template_template_parm (tree parm
,
5964 tsubst_flags_t complain
,
5968 if (arg
== NULL_TREE
|| arg
== error_mark_node
5969 || parm
== NULL_TREE
|| parm
== error_mark_node
)
5972 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5975 switch (TREE_CODE (parm
))
5978 /* We encounter instantiations of templates like
5979 template <template <template <class> class> class TT>
5982 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
5983 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
5985 if (!coerce_template_template_parms
5986 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
5992 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
5993 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5994 /* Argument is a parameter pack but parameter is not. */
5999 /* The tsubst call is used to handle cases such as
6001 template <int> class C {};
6002 template <class T, template <T> class TT> class D {};
6005 i.e. the parameter list of TT depends on earlier parameters. */
6006 if (!uses_template_parms (TREE_TYPE (arg
))
6008 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
6012 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6013 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6014 /* Argument is a parameter pack but parameter is not. */
6027 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6028 template template parameters. Both PARM_PARMS and ARG_PARMS are
6029 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6032 Consider the example:
6033 template <class T> class A;
6034 template<template <class U> class TT> class B;
6036 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6037 the parameters to A, and OUTER_ARGS contains A. */
6040 coerce_template_template_parms (tree parm_parms
,
6042 tsubst_flags_t complain
,
6046 int nparms
, nargs
, i
;
6050 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
6051 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
6053 nparms
= TREE_VEC_LENGTH (parm_parms
);
6054 nargs
= TREE_VEC_LENGTH (arg_parms
);
6056 /* Determine whether we have a parameter pack at the end of the
6057 template template parameter's template parameter list. */
6058 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
6060 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
6062 if (parm
== error_mark_node
)
6065 switch (TREE_CODE (parm
))
6069 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6074 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6084 && !(variadic_p
&& nargs
>= nparms
- 1))
6087 /* Check all of the template parameters except the parameter pack at
6088 the end (if any). */
6089 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6091 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6092 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6095 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6096 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6098 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6106 /* Check each of the template parameters in the template
6107 argument against the template parameter pack at the end of
6108 the template template parameter. */
6109 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6112 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6114 for (; i
< nargs
; ++i
)
6116 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6119 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6121 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6130 /* Verifies that the deduced template arguments (in TARGS) for the
6131 template template parameters (in TPARMS) represent valid bindings,
6132 by comparing the template parameter list of each template argument
6133 to the template parameter list of its corresponding template
6134 template parameter, in accordance with DR150. This
6135 routine can only be called after all template arguments have been
6136 deduced. It will return TRUE if all of the template template
6137 parameter bindings are okay, FALSE otherwise. */
6139 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6141 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6144 /* We're dealing with template parms in this process. */
6145 ++processing_template_decl
;
6147 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6149 for (i
= 0; i
< ntparms
; ++i
)
6151 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6152 tree targ
= TREE_VEC_ELT (targs
, i
);
6154 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6156 tree packed_args
= NULL_TREE
;
6159 if (ARGUMENT_PACK_P (targ
))
6161 /* Look inside the argument pack. */
6162 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6163 len
= TREE_VEC_LENGTH (packed_args
);
6166 for (idx
= 0; idx
< len
; ++idx
)
6168 tree targ_parms
= NULL_TREE
;
6171 /* Extract the next argument from the argument
6173 targ
= TREE_VEC_ELT (packed_args
, idx
);
6175 if (PACK_EXPANSION_P (targ
))
6176 /* Look at the pattern of the pack expansion. */
6177 targ
= PACK_EXPANSION_PATTERN (targ
);
6179 /* Extract the template parameters from the template
6181 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6182 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6183 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6184 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6186 /* Verify that we can coerce the template template
6187 parameters from the template argument to the template
6188 parameter. This requires an exact match. */
6190 && !coerce_template_template_parms
6191 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6206 --processing_template_decl
;
6210 /* Since type attributes aren't mangled, we need to strip them from
6211 template type arguments. */
6214 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6217 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6219 mv
= TYPE_MAIN_VARIANT (arg
);
6220 arg
= strip_typedefs (arg
);
6221 if (TYPE_ALIGN (arg
) != TYPE_ALIGN (mv
)
6222 || TYPE_ATTRIBUTES (arg
) != TYPE_ATTRIBUTES (mv
))
6224 if (complain
& tf_warning
)
6225 warning (0, "ignoring attributes on template argument %qT", arg
);
6226 arg
= build_aligned_type (arg
, TYPE_ALIGN (mv
));
6227 arg
= cp_build_type_attribute_variant (arg
, TYPE_ATTRIBUTES (mv
));
6232 /* Convert the indicated template ARG as necessary to match the
6233 indicated template PARM. Returns the converted ARG, or
6234 error_mark_node if the conversion was unsuccessful. Error and
6235 warning messages are issued under control of COMPLAIN. This
6236 conversion is for the Ith parameter in the parameter list. ARGS is
6237 the full set of template arguments deduced so far. */
6240 convert_template_argument (tree parm
,
6243 tsubst_flags_t complain
,
6249 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
6251 if (TREE_CODE (arg
) == TREE_LIST
6252 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
6254 /* The template argument was the name of some
6255 member function. That's usually
6256 invalid, but static members are OK. In any
6257 case, grab the underlying fields/functions
6258 and issue an error later if required. */
6259 orig_arg
= TREE_VALUE (arg
);
6260 TREE_TYPE (arg
) = unknown_type_node
;
6265 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
6266 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
6267 || requires_tmpl_type
);
6269 /* When determining whether an argument pack expansion is a template,
6270 look at the pattern. */
6271 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
6272 arg
= PACK_EXPANSION_PATTERN (arg
);
6274 /* Deal with an injected-class-name used as a template template arg. */
6275 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
6277 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
6278 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6280 if (cxx_dialect
>= cxx11
)
6281 /* OK under DR 1004. */;
6282 else if (complain
& tf_warning_or_error
)
6283 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
6284 " used as template template argument", TYPE_NAME (arg
));
6285 else if (flag_pedantic_errors
)
6293 ((TREE_CODE (arg
) == TEMPLATE_DECL
6294 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
6295 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
6296 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6297 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
6300 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6301 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
6302 arg
= TYPE_STUB_DECL (arg
);
6304 is_type
= TYPE_P (arg
) || is_tmpl_type
;
6306 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
6307 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
6309 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
6311 if (complain
& tf_error
)
6312 error ("invalid use of destructor %qE as a type", orig_arg
);
6313 return error_mark_node
;
6316 permerror (input_location
,
6317 "to refer to a type member of a template parameter, "
6318 "use %<typename %E%>", orig_arg
);
6320 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
6321 TREE_OPERAND (arg
, 1),
6327 if (is_type
!= requires_type
)
6331 if (complain
& tf_error
)
6333 error ("type/value mismatch at argument %d in template "
6334 "parameter list for %qD",
6337 error (" expected a constant of type %qT, got %qT",
6339 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6340 else if (requires_tmpl_type
)
6341 error (" expected a class template, got %qE", orig_arg
);
6343 error (" expected a type, got %qE", orig_arg
);
6346 return error_mark_node
;
6348 if (is_tmpl_type
^ requires_tmpl_type
)
6350 if (in_decl
&& (complain
& tf_error
))
6352 error ("type/value mismatch at argument %d in template "
6353 "parameter list for %qD",
6356 error (" expected a type, got %qT", DECL_NAME (arg
));
6358 error (" expected a class template, got %qT", orig_arg
);
6360 return error_mark_node
;
6365 if (requires_tmpl_type
)
6367 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6369 else if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6370 /* The number of argument required is not known yet.
6371 Just accept it for now. */
6372 val
= TREE_TYPE (arg
);
6375 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6378 /* Strip alias templates that are equivalent to another
6380 arg
= get_underlying_template (arg
);
6381 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6383 if (coerce_template_template_parms (parmparm
, argparm
,
6389 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6391 if (val
!= error_mark_node
)
6393 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6394 val
= TREE_TYPE (val
);
6395 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6396 val
= make_pack_expansion (val
);
6401 if (in_decl
&& (complain
& tf_error
))
6403 error ("type/value mismatch at argument %d in "
6404 "template parameter list for %qD",
6406 error (" expected a template of type %qD, got %qT",
6410 val
= error_mark_node
;
6416 /* We only form one instance of each template specialization.
6417 Therefore, if we use a non-canonical variant (i.e., a
6418 typedef), any future messages referring to the type will use
6419 the typedef, which is confusing if those future uses do not
6420 themselves also use the typedef. */
6422 val
= canonicalize_type_argument (val
, complain
);
6426 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6428 if (invalid_nontype_parm_type_p (t
, complain
))
6429 return error_mark_node
;
6431 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6433 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6437 /* Not sure if this is reachable, but it doesn't hurt
6439 error ("type mismatch in nontype parameter pack");
6440 val
= error_mark_node
;
6443 else if (!dependent_template_arg_p (orig_arg
)
6444 && !uses_template_parms (t
))
6445 /* We used to call digest_init here. However, digest_init
6446 will report errors, which we don't want when complain
6447 is zero. More importantly, digest_init will try too
6448 hard to convert things: for example, `0' should not be
6449 converted to pointer type at this point according to
6450 the standard. Accepting this is not merely an
6451 extension, since deciding whether or not these
6452 conversions can occur is part of determining which
6453 function template to call, or whether a given explicit
6454 argument specification is valid. */
6455 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6457 val
= strip_typedefs_expr (orig_arg
);
6459 if (val
== NULL_TREE
)
6460 val
= error_mark_node
;
6461 else if (val
== error_mark_node
&& (complain
& tf_error
))
6462 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6464 if (TREE_CODE (val
) == SCOPE_REF
)
6466 /* Strip typedefs from the SCOPE_REF. */
6467 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
6468 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
6470 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6471 QUALIFIED_NAME_IS_TEMPLATE (val
));
6478 /* Coerces the remaining template arguments in INNER_ARGS (from
6479 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6480 Returns the coerced argument pack. PARM_IDX is the position of this
6481 parameter in the template parameter list. ARGS is the original
6482 template argument list. */
6484 coerce_template_parameter_pack (tree parms
,
6492 tsubst_flags_t complain
)
6494 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6495 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6498 tree packed_types
= NULL_TREE
;
6500 if (arg_idx
> nargs
)
6503 packed_args
= make_tree_vec (nargs
- arg_idx
);
6505 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
6506 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
6508 /* When the template parameter is a non-type template
6509 parameter pack whose type uses parameter packs, we need
6510 to look at each of the template arguments
6511 separately. Build a vector of the types for these
6512 non-type template parameters in PACKED_TYPES. */
6514 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
6515 packed_types
= tsubst_pack_expansion (expansion
, args
,
6518 if (packed_types
== error_mark_node
)
6519 return error_mark_node
;
6521 /* Check that we have the right number of arguments. */
6523 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
6524 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
6527 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
6528 error ("wrong number of template arguments (%d, should be %d)",
6529 nargs
, needed_parms
);
6530 return error_mark_node
;
6533 /* If we aren't able to check the actual arguments now
6534 (because they haven't been expanded yet), we can at least
6535 verify that all of the types used for the non-type
6536 template parameter pack are, in fact, valid for non-type
6537 template parameters. */
6539 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6541 int j
, len
= TREE_VEC_LENGTH (packed_types
);
6542 for (j
= 0; j
< len
; ++j
)
6544 tree t
= TREE_VEC_ELT (packed_types
, j
);
6545 if (invalid_nontype_parm_type_p (t
, complain
))
6546 return error_mark_node
;
6551 /* Convert the remaining arguments, which will be a part of the
6552 parameter pack "parm". */
6553 for (; arg_idx
< nargs
; ++arg_idx
)
6555 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6556 tree actual_parm
= TREE_VALUE (parm
);
6558 if (packed_types
&& !PACK_EXPANSION_P (arg
))
6560 /* When we have a vector of types (corresponding to the
6561 non-type template parameter pack that uses parameter
6562 packs in its type, as mention above), and the
6563 argument is not an expansion (which expands to a
6564 currently unknown number of arguments), clone the
6565 parm and give it the next type in PACKED_TYPES. */
6566 actual_parm
= copy_node (actual_parm
);
6567 TREE_TYPE (actual_parm
) =
6568 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
6571 if (arg
== error_mark_node
)
6573 if (complain
& tf_error
)
6574 error ("template argument %d is invalid", arg_idx
+ 1);
6577 arg
= convert_template_argument (actual_parm
,
6578 arg
, new_args
, complain
, parm_idx
,
6580 if (arg
== error_mark_node
)
6582 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
6585 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6586 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
6587 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
6590 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
6591 TREE_TYPE (argument_pack
)
6592 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
6593 TREE_CONSTANT (argument_pack
) = 1;
6596 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
6597 #ifdef ENABLE_CHECKING
6598 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
6599 TREE_VEC_LENGTH (packed_args
));
6601 return argument_pack
;
6604 /* Returns the number of pack expansions in the template argument vector
6608 pack_expansion_args_count (tree args
)
6613 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
6615 tree elt
= TREE_VEC_ELT (args
, i
);
6616 if (elt
&& PACK_EXPANSION_P (elt
))
6622 /* Convert all template arguments to their appropriate types, and
6623 return a vector containing the innermost resulting template
6624 arguments. If any error occurs, return error_mark_node. Error and
6625 warning messages are issued under control of COMPLAIN.
6627 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6628 for arguments not specified in ARGS. Otherwise, if
6629 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6630 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6631 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6635 coerce_template_parms (tree parms
,
6638 tsubst_flags_t complain
,
6639 bool require_all_args
,
6640 bool use_default_args
)
6642 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
6645 tree new_inner_args
;
6646 int saved_unevaluated_operand
;
6647 int saved_inhibit_evaluation_warnings
;
6649 /* When used as a boolean value, indicates whether this is a
6650 variadic template parameter list. Since it's an int, we can also
6651 subtract it from nparms to get the number of non-variadic
6654 int variadic_args_p
= 0;
6655 int post_variadic_parms
= 0;
6657 if (args
== error_mark_node
)
6658 return error_mark_node
;
6660 nparms
= TREE_VEC_LENGTH (parms
);
6662 /* Determine if there are any parameter packs. */
6663 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
6665 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
6667 ++post_variadic_parms
;
6668 if (template_parameter_pack_p (tparm
))
6672 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
6673 /* If there are no parameters that follow a parameter pack, we need to
6674 expand any argument packs so that we can deduce a parameter pack from
6675 some non-packed args followed by an argument pack, as in variadic85.C.
6676 If there are such parameters, we need to leave argument packs intact
6677 so the arguments are assigned properly. This can happen when dealing
6678 with a nested class inside a partial specialization of a class
6679 template, as in variadic92.C, or when deducing a template parameter pack
6680 from a sub-declarator, as in variadic114.C. */
6681 if (!post_variadic_parms
)
6682 inner_args
= expand_template_argument_pack (inner_args
);
6684 /* Count any pack expansion args. */
6685 variadic_args_p
= pack_expansion_args_count (inner_args
);
6687 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6688 if ((nargs
> nparms
&& !variadic_p
)
6689 || (nargs
< nparms
- variadic_p
6692 && (!use_default_args
6693 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
6694 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
6696 if (complain
& tf_error
)
6700 nparms
-= variadic_p
;
6701 error ("wrong number of template arguments "
6702 "(%d, should be %d or more)", nargs
, nparms
);
6705 error ("wrong number of template arguments "
6706 "(%d, should be %d)", nargs
, nparms
);
6709 error ("provided for %q+D", in_decl
);
6712 return error_mark_node
;
6714 /* We can't pass a pack expansion to a non-pack parameter of an alias
6715 template (DR 1430). */
6716 else if (in_decl
&& DECL_ALIAS_TEMPLATE_P (in_decl
)
6718 && nargs
- variadic_args_p
< nparms
- variadic_p
)
6720 if (complain
& tf_error
)
6722 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
6724 tree arg
= TREE_VEC_ELT (inner_args
, i
);
6725 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6727 if (PACK_EXPANSION_P (arg
)
6728 && !template_parameter_pack_p (parm
))
6730 error ("pack expansion argument for non-pack parameter "
6731 "%qD of alias template %qD", parm
, in_decl
);
6732 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
6739 return error_mark_node
;
6742 /* We need to evaluate the template arguments, even though this
6743 template-id may be nested within a "sizeof". */
6744 saved_unevaluated_operand
= cp_unevaluated_operand
;
6745 cp_unevaluated_operand
= 0;
6746 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6747 c_inhibit_evaluation_warnings
= 0;
6748 new_inner_args
= make_tree_vec (nparms
);
6749 new_args
= add_outermost_template_args (args
, new_inner_args
);
6750 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
6755 /* Get the Ith template parameter. */
6756 parm
= TREE_VEC_ELT (parms
, parm_idx
);
6758 if (parm
== error_mark_node
)
6760 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
6764 /* Calculate the next argument. */
6765 if (arg_idx
< nargs
)
6766 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6770 if (template_parameter_pack_p (TREE_VALUE (parm
))
6771 && !(arg
&& ARGUMENT_PACK_P (arg
)))
6773 /* All remaining arguments will be placed in the
6774 template parameter pack PARM. */
6775 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
6776 inner_args
, arg_idx
,
6780 /* Store this argument. */
6781 if (arg
== error_mark_node
)
6783 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
6785 /* We are done with all of the arguments. */
6792 if (PACK_EXPANSION_P (arg
))
6794 /* We don't know how many args we have yet, just
6795 use the unconverted ones for now. */
6796 new_inner_args
= inner_args
;
6800 else if (require_all_args
)
6802 /* There must be a default arg in this case. */
6803 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
6805 /* The position of the first default template argument,
6806 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6808 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6809 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
, arg_idx
);
6814 if (arg
== error_mark_node
)
6816 if (complain
& tf_error
)
6817 error ("template argument %d is invalid", arg_idx
+ 1);
6820 /* This only occurs if there was an error in the template
6821 parameter list itself (which we would already have
6822 reported) that we are trying to recover from, e.g., a class
6823 template with a parameter list such as
6824 template<typename..., typename>. */
6827 arg
= convert_template_argument (TREE_VALUE (parm
),
6828 arg
, new_args
, complain
,
6831 if (arg
== error_mark_node
)
6833 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
6835 cp_unevaluated_operand
= saved_unevaluated_operand
;
6836 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
6839 return error_mark_node
;
6841 #ifdef ENABLE_CHECKING
6842 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6843 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6844 TREE_VEC_LENGTH (new_inner_args
));
6847 return new_inner_args
;
6850 /* Like coerce_template_parms. If PARMS represents all template
6851 parameters levels, this function returns a vector of vectors
6852 representing all the resulting argument levels. Note that in this
6853 case, only the innermost arguments are coerced because the
6854 outermost ones are supposed to have been coerced already.
6856 Otherwise, if PARMS represents only (the innermost) vector of
6857 parameters, this function returns a vector containing just the
6858 innermost resulting arguments. */
6861 coerce_innermost_template_parms (tree parms
,
6864 tsubst_flags_t complain
,
6865 bool require_all_args
,
6866 bool use_default_args
)
6868 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
6869 int args_depth
= TMPL_ARGS_DEPTH (args
);
6872 if (parms_depth
> 1)
6874 coerced_args
= make_tree_vec (parms_depth
);
6878 for (level
= parms
, cur_depth
= parms_depth
;
6879 parms_depth
> 0 && level
!= NULL_TREE
;
6880 level
= TREE_CHAIN (level
), --cur_depth
)
6883 if (cur_depth
== args_depth
)
6884 l
= coerce_template_parms (TREE_VALUE (level
),
6885 args
, in_decl
, complain
,
6889 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
6891 if (l
== error_mark_node
)
6892 return error_mark_node
;
6894 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
6898 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
6899 args
, in_decl
, complain
,
6902 return coerced_args
;
6905 /* Returns 1 if template args OT and NT are equivalent. */
6908 template_args_equal (tree ot
, tree nt
)
6912 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
6915 if (TREE_CODE (nt
) == TREE_VEC
)
6916 /* For member templates */
6917 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
6918 else if (PACK_EXPANSION_P (ot
))
6919 return (PACK_EXPANSION_P (nt
)
6920 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
6921 PACK_EXPANSION_PATTERN (nt
))
6922 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
6923 PACK_EXPANSION_EXTRA_ARGS (nt
)));
6924 else if (ARGUMENT_PACK_P (ot
))
6929 if (!ARGUMENT_PACK_P (nt
))
6932 opack
= ARGUMENT_PACK_ARGS (ot
);
6933 npack
= ARGUMENT_PACK_ARGS (nt
);
6934 len
= TREE_VEC_LENGTH (opack
);
6935 if (TREE_VEC_LENGTH (npack
) != len
)
6937 for (i
= 0; i
< len
; ++i
)
6938 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
6939 TREE_VEC_ELT (npack
, i
)))
6943 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
6945 /* We get here probably because we are in the middle of substituting
6946 into the pattern of a pack expansion. In that case the
6947 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6948 interested in. So we want to use the initial pack argument for
6950 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
6951 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
6952 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
6953 return template_args_equal (ot
, nt
);
6955 else if (TYPE_P (nt
))
6956 return TYPE_P (ot
) && same_type_p (ot
, nt
);
6957 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
6960 return cp_tree_equal (ot
, nt
);
6963 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6964 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6965 NEWARG_PTR with the offending arguments if they are non-NULL. */
6968 comp_template_args_with_info (tree oldargs
, tree newargs
,
6969 tree
*oldarg_ptr
, tree
*newarg_ptr
)
6973 if (oldargs
== newargs
)
6976 if (!oldargs
|| !newargs
)
6979 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
6982 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
6984 tree nt
= TREE_VEC_ELT (newargs
, i
);
6985 tree ot
= TREE_VEC_ELT (oldargs
, i
);
6987 if (! template_args_equal (ot
, nt
))
6989 if (oldarg_ptr
!= NULL
)
6991 if (newarg_ptr
!= NULL
)
6999 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7000 of template arguments. Returns 0 otherwise. */
7003 comp_template_args (tree oldargs
, tree newargs
)
7005 return comp_template_args_with_info (oldargs
, newargs
, NULL
, NULL
);
7009 add_pending_template (tree d
)
7011 tree ti
= (TYPE_P (d
)
7012 ? CLASSTYPE_TEMPLATE_INFO (d
)
7013 : DECL_TEMPLATE_INFO (d
));
7014 struct pending_template
*pt
;
7017 if (TI_PENDING_TEMPLATE_FLAG (ti
))
7020 /* We are called both from instantiate_decl, where we've already had a
7021 tinst_level pushed, and instantiate_template, where we haven't.
7023 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
7026 push_tinst_level (d
);
7028 pt
= ggc_alloc_pending_template ();
7030 pt
->tinst
= current_tinst_level
;
7031 if (last_pending_template
)
7032 last_pending_template
->next
= pt
;
7034 pending_templates
= pt
;
7036 last_pending_template
= pt
;
7038 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
7045 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7046 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7047 documentation for TEMPLATE_ID_EXPR. */
7050 lookup_template_function (tree fns
, tree arglist
)
7054 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
7055 return error_mark_node
;
7057 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
7059 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
7061 error ("%q#D is not a function template", fns
);
7062 return error_mark_node
;
7065 if (BASELINK_P (fns
))
7067 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
7069 BASELINK_FUNCTIONS (fns
),
7074 type
= TREE_TYPE (fns
);
7075 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
7076 type
= unknown_type_node
;
7078 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
7081 /* Within the scope of a template class S<T>, the name S gets bound
7082 (in build_self_reference) to a TYPE_DECL for the class, not a
7083 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7084 or one of its enclosing classes, and that type is a template,
7085 return the associated TEMPLATE_DECL. Otherwise, the original
7088 Also handle the case when DECL is a TREE_LIST of ambiguous
7089 injected-class-names from different bases. */
7092 maybe_get_template_decl_from_type_decl (tree decl
)
7094 if (decl
== NULL_TREE
)
7097 /* DR 176: A lookup that finds an injected-class-name (10.2
7098 [class.member.lookup]) can result in an ambiguity in certain cases
7099 (for example, if it is found in more than one base class). If all of
7100 the injected-class-names that are found refer to specializations of
7101 the same class template, and if the name is followed by a
7102 template-argument-list, the reference refers to the class template
7103 itself and not a specialization thereof, and is not ambiguous. */
7104 if (TREE_CODE (decl
) == TREE_LIST
)
7106 tree t
, tmpl
= NULL_TREE
;
7107 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
7109 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
7112 else if (tmpl
!= elt
)
7115 if (tmpl
&& t
== NULL_TREE
)
7121 return (decl
!= NULL_TREE
7122 && DECL_SELF_REFERENCE_P (decl
)
7123 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
7124 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
7127 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7128 parameters, find the desired type.
7130 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7132 IN_DECL, if non-NULL, is the template declaration we are trying to
7135 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7136 the class we are looking up.
7138 Issue error and warning messages under control of COMPLAIN.
7140 If the template class is really a local class in a template
7141 function, then the FUNCTION_CONTEXT is the function in which it is
7144 ??? Note that this function is currently called *twice* for each
7145 template-id: the first time from the parser, while creating the
7146 incomplete type (finish_template_type), and the second type during the
7147 real instantiation (instantiate_template_class). This is surely something
7148 that we want to avoid. It also causes some problems with argument
7149 coercion (see convert_nontype_argument for more information on this). */
7152 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
7153 int entering_scope
, tsubst_flags_t complain
)
7155 tree templ
= NULL_TREE
, parmlist
;
7162 if (identifier_p (d1
))
7164 tree value
= innermost_non_namespace_value (d1
);
7165 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
7170 push_decl_namespace (context
);
7171 templ
= lookup_name (d1
);
7172 templ
= maybe_get_template_decl_from_type_decl (templ
);
7174 pop_decl_namespace ();
7177 context
= DECL_CONTEXT (templ
);
7179 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
7181 tree type
= TREE_TYPE (d1
);
7183 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7184 an implicit typename for the second A. Deal with it. */
7185 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
7186 type
= TREE_TYPE (type
);
7188 if (CLASSTYPE_TEMPLATE_INFO (type
))
7190 templ
= CLASSTYPE_TI_TEMPLATE (type
);
7191 d1
= DECL_NAME (templ
);
7194 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
7195 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
7197 templ
= TYPE_TI_TEMPLATE (d1
);
7198 d1
= DECL_NAME (templ
);
7200 else if (TREE_CODE (d1
) == TEMPLATE_DECL
7201 && DECL_TEMPLATE_RESULT (d1
)
7202 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
7205 d1
= DECL_NAME (templ
);
7206 context
= DECL_CONTEXT (templ
);
7208 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
7211 d1
= DECL_NAME (templ
);
7214 /* Issue an error message if we didn't find a template. */
7217 if (complain
& tf_error
)
7218 error ("%qT is not a template", d1
);
7219 return error_mark_node
;
7222 if (TREE_CODE (templ
) != TEMPLATE_DECL
7223 /* Make sure it's a user visible template, if it was named by
7225 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
7226 && !PRIMARY_TEMPLATE_P (templ
)))
7228 if (complain
& tf_error
)
7230 error ("non-template type %qT used as a template", d1
);
7232 error ("for template declaration %q+D", in_decl
);
7234 return error_mark_node
;
7237 complain
&= ~tf_user
;
7239 /* An alias that just changes the name of a template is equivalent to the
7240 other template, so if any of the arguments are pack expansions, strip
7241 the alias to avoid problems with a pack expansion passed to a non-pack
7242 alias template parameter (DR 1430). */
7243 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
7244 templ
= get_underlying_template (templ
);
7246 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
7248 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7249 template arguments */
7255 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7257 /* Consider an example where a template template parameter declared as
7259 template <class T, class U = std::allocator<T> > class TT
7261 The template parameter level of T and U are one level larger than
7262 of TT. To proper process the default argument of U, say when an
7263 instantiation `TT<int>' is seen, we need to build the full
7264 arguments containing {int} as the innermost level. Outer levels,
7265 available when not appearing as default template argument, can be
7266 obtained from the arguments of the enclosing template.
7268 Suppose that TT is later substituted with std::vector. The above
7269 instantiation is `TT<int, std::allocator<T> >' with TT at
7270 level 1, and T at level 2, while the template arguments at level 1
7271 becomes {std::vector} and the inner level 2 is {int}. */
7273 outer
= DECL_CONTEXT (templ
);
7275 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7276 else if (current_template_parms
)
7277 /* This is an argument of the current template, so we haven't set
7278 DECL_CONTEXT yet. */
7279 outer
= current_template_args ();
7282 arglist
= add_to_template_args (outer
, arglist
);
7284 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
7286 /*require_all_args=*/true,
7287 /*use_default_args=*/true);
7288 if (arglist2
== error_mark_node
7289 || (!uses_template_parms (arglist2
)
7290 && check_instantiated_args (templ
, arglist2
, complain
)))
7291 return error_mark_node
;
7293 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
7298 tree template_type
= TREE_TYPE (templ
);
7301 tree found
= NULL_TREE
;
7304 int is_dependent_type
;
7305 int use_partial_inst_tmpl
= false;
7307 if (template_type
== error_mark_node
)
7308 /* An error occurred while building the template TEMPL, and a
7309 diagnostic has most certainly been emitted for that
7310 already. Let's propagate that error. */
7311 return error_mark_node
;
7313 gen_tmpl
= most_general_template (templ
);
7314 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7315 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
7316 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7318 if (arg_depth
== 1 && parm_depth
> 1)
7320 /* We've been given an incomplete set of template arguments.
7323 template <class T> struct S1 {
7324 template <class U> struct S2 {};
7325 template <class U> struct S2<U*> {};
7328 we will be called with an ARGLIST of `U*', but the
7329 TEMPLATE will be `template <class T> template
7330 <class U> struct S1<T>::S2'. We must fill in the missing
7333 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
7335 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7338 /* Now we should have enough arguments. */
7339 gcc_assert (parm_depth
== arg_depth
);
7341 /* From here on, we're only interested in the most general
7344 /* Calculate the BOUND_ARGS. These will be the args that are
7345 actually tsubst'd into the definition to create the
7349 /* We have multiple levels of arguments to coerce, at once. */
7351 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
7353 tree bound_args
= make_tree_vec (parm_depth
);
7355 for (i
= saved_depth
,
7356 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7357 i
> 0 && t
!= NULL_TREE
;
7358 --i
, t
= TREE_CHAIN (t
))
7361 if (i
== saved_depth
)
7362 a
= coerce_template_parms (TREE_VALUE (t
),
7365 /*require_all_args=*/true,
7366 /*use_default_args=*/true);
7368 /* Outer levels should have already been coerced. */
7369 a
= TMPL_ARGS_LEVEL (arglist
, i
);
7371 /* Don't process further if one of the levels fails. */
7372 if (a
== error_mark_node
)
7374 /* Restore the ARGLIST to its full size. */
7375 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7376 return error_mark_node
;
7379 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
7381 /* We temporarily reduce the length of the ARGLIST so
7382 that coerce_template_parms will see only the arguments
7383 corresponding to the template parameters it is
7385 TREE_VEC_LENGTH (arglist
)--;
7388 /* Restore the ARGLIST to its full size. */
7389 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7391 arglist
= bound_args
;
7395 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
7396 INNERMOST_TEMPLATE_ARGS (arglist
),
7399 /*require_all_args=*/true,
7400 /*use_default_args=*/true);
7402 if (arglist
== error_mark_node
)
7403 /* We were unable to bind the arguments. */
7404 return error_mark_node
;
7406 /* In the scope of a template class, explicit references to the
7407 template class refer to the type of the template, not any
7408 instantiation of it. For example, in:
7410 template <class T> class C { void f(C<T>); }
7412 the `C<T>' is just the same as `C'. Outside of the
7413 class, however, such a reference is an instantiation. */
7415 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
7416 || currently_open_class (template_type
))
7417 /* comp_template_args is expensive, check it last. */
7418 && comp_template_args (TYPE_TI_ARGS (template_type
),
7420 return template_type
;
7422 /* If we already have this specialization, return it. */
7423 elt
.tmpl
= gen_tmpl
;
7425 hash
= hash_specialization (&elt
);
7426 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
7432 is_dependent_type
= uses_template_parms (arglist
);
7434 /* If the deduced arguments are invalid, then the binding
7436 if (!is_dependent_type
7437 && check_instantiated_args (gen_tmpl
,
7438 INNERMOST_TEMPLATE_ARGS (arglist
),
7440 return error_mark_node
;
7442 if (!is_dependent_type
7443 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
7444 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
7445 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
7447 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
7448 DECL_NAME (gen_tmpl
),
7449 /*tag_scope=*/ts_global
);
7453 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
7455 if (context
== error_mark_node
)
7456 return error_mark_node
;
7459 context
= global_namespace
;
7461 /* Create the type. */
7462 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7464 /* The user referred to a specialization of an alias
7465 template represented by GEN_TMPL.
7467 [temp.alias]/2 says:
7469 When a template-id refers to the specialization of an
7470 alias template, it is equivalent to the associated
7471 type obtained by substitution of its
7472 template-arguments for the template-parameters in the
7473 type-id of the alias template. */
7475 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
7476 /* Note that the call above (by indirectly calling
7477 register_specialization in tsubst_decl) registers the
7478 TYPE_DECL representing the specialization of the alias
7479 template. So next time someone substitutes ARGLIST for
7480 the template parms into the alias template (GEN_TMPL),
7481 she'll get that TYPE_DECL back. */
7483 if (t
== error_mark_node
)
7486 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
7488 if (!is_dependent_type
)
7490 set_current_access_from_decl (TYPE_NAME (template_type
));
7491 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
7492 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
7493 arglist
, complain
, in_decl
),
7494 SCOPED_ENUM_P (template_type
), NULL
);
7498 /* We don't want to call start_enum for this type, since
7499 the values for the enumeration constants may involve
7500 template parameters. And, no one should be interested
7501 in the enumeration constants for such a type. */
7502 t
= cxx_make_type (ENUMERAL_TYPE
);
7503 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
7505 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
7506 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
7507 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7509 else if (CLASS_TYPE_P (template_type
))
7511 t
= make_class_type (TREE_CODE (template_type
));
7512 CLASSTYPE_DECLARED_CLASS (t
)
7513 = CLASSTYPE_DECLARED_CLASS (template_type
);
7514 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7515 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7517 /* A local class. Make sure the decl gets registered properly. */
7518 if (context
== current_function_decl
)
7519 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7521 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7522 /* This instantiation is another name for the primary
7523 template type. Set the TYPE_CANONICAL field
7525 TYPE_CANONICAL (t
) = template_type
;
7526 else if (any_template_arguments_need_structural_equality_p (arglist
))
7527 /* Some of the template arguments require structural
7528 equality testing, so this template class requires
7529 structural equality testing. */
7530 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7535 /* If we called start_enum or pushtag above, this information
7536 will already be set up. */
7539 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
7541 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
7542 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
7543 DECL_SOURCE_LOCATION (type_decl
)
7544 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
7547 type_decl
= TYPE_NAME (t
);
7549 if (CLASS_TYPE_P (template_type
))
7551 TREE_PRIVATE (type_decl
)
7552 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
7553 TREE_PROTECTED (type_decl
)
7554 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
7555 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
7557 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
7558 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
7562 /* Let's consider the explicit specialization of a member
7563 of a class template specialization that is implicitely instantiated,
7568 template<class U> struct M {}; //#0
7573 struct S<int>::M<char> //#1
7577 [temp.expl.spec]/4 says this is valid.
7579 In this case, when we write:
7582 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7585 When we encounter #1, we want to store the partial instantiation
7586 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7588 For all cases other than this "explicit specialization of member of a
7589 class template", we just want to store the most general template into
7590 the CLASSTYPE_TI_TEMPLATE of M.
7592 This case of "explicit specialization of member of a class template"
7594 1/ the enclosing class is an instantiation of, and therefore not
7595 the same as, the context of the most general template, and
7596 2/ we aren't looking at the partial instantiation itself, i.e.
7597 the innermost arguments are not the same as the innermost parms of
7598 the most general template.
7600 So it's only when 1/ and 2/ happens that we want to use the partial
7601 instantiation of the member template in lieu of its most general
7604 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
7605 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
7606 /* the enclosing class must be an instantiation... */
7607 && CLASS_TYPE_P (context
)
7608 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
7610 tree partial_inst_args
;
7611 TREE_VEC_LENGTH (arglist
)--;
7612 ++processing_template_decl
;
7614 tsubst (INNERMOST_TEMPLATE_ARGS
7615 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
7616 arglist
, complain
, NULL_TREE
);
7617 --processing_template_decl
;
7618 TREE_VEC_LENGTH (arglist
)++;
7619 use_partial_inst_tmpl
=
7620 /*...and we must not be looking at the partial instantiation
7622 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
7626 if (!use_partial_inst_tmpl
)
7627 /* This case is easy; there are no member templates involved. */
7631 /* This is a full instantiation of a member template. Find
7632 the partial instantiation of which this is an instance. */
7634 /* Temporarily reduce by one the number of levels in the ARGLIST
7635 so as to avoid comparing the last set of arguments. */
7636 TREE_VEC_LENGTH (arglist
)--;
7637 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
7638 TREE_VEC_LENGTH (arglist
)++;
7639 /* FOUND is either a proper class type, or an alias
7640 template specialization. In the later case, it's a
7641 TYPE_DECL, resulting from the substituting of arguments
7642 for parameters in the TYPE_DECL of the alias template
7643 done earlier. So be careful while getting the template
7645 found
= TREE_CODE (found
) == TYPE_DECL
7646 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
7647 : CLASSTYPE_TI_TEMPLATE (found
);
7650 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
7653 slot
= htab_find_slot_with_hash (type_specializations
,
7654 &elt
, hash
, INSERT
);
7655 entry
= ggc_alloc_spec_entry ();
7659 /* Note this use of the partial instantiation so we can check it
7660 later in maybe_process_partial_specialization. */
7661 DECL_TEMPLATE_INSTANTIATIONS (templ
)
7662 = tree_cons (arglist
, t
,
7663 DECL_TEMPLATE_INSTANTIATIONS (templ
));
7665 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
7666 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7667 /* Now that the type has been registered on the instantiations
7668 list, we set up the enumerators. Because the enumeration
7669 constants may involve the enumeration type itself, we make
7670 sure to register the type first, and then create the
7671 constants. That way, doing tsubst_expr for the enumeration
7672 constants won't result in recursive calls here; we'll find
7673 the instantiation and exit above. */
7674 tsubst_enum (template_type
, t
, arglist
);
7676 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
7677 /* If the type makes use of template parameters, the
7678 code that generates debugging information will crash. */
7679 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
7681 /* Possibly limit visibility based on template args. */
7682 TREE_PUBLIC (type_decl
) = 1;
7683 determine_visibility (type_decl
);
7689 /* Wrapper for lookup_template_class_1. */
7692 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
7693 int entering_scope
, tsubst_flags_t complain
)
7696 timevar_push (TV_TEMPLATE_INST
);
7697 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
7698 entering_scope
, complain
);
7699 timevar_pop (TV_TEMPLATE_INST
);
7707 /* True when we should also visit template parameters that occur in
7708 non-deduced contexts. */
7709 bool include_nondeduced_p
;
7710 struct pointer_set_t
*visited
;
7713 /* Called from for_each_template_parm via walk_tree. */
7716 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
7719 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
7720 tree_fn_t fn
= pfd
->fn
;
7721 void *data
= pfd
->data
;
7724 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
7725 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
7726 pfd
->include_nondeduced_p
))
7727 return error_mark_node
;
7729 switch (TREE_CODE (t
))
7732 if (TYPE_PTRMEMFUNC_P (t
))
7738 if (!TYPE_TEMPLATE_INFO (t
))
7740 else if (for_each_template_parm (TYPE_TI_ARGS (t
),
7741 fn
, data
, pfd
->visited
,
7742 pfd
->include_nondeduced_p
))
7743 return error_mark_node
;
7747 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
7748 fn
, data
, pfd
->visited
,
7749 pfd
->include_nondeduced_p
)
7750 || for_each_template_parm (TYPE_MAX_VALUE (t
),
7751 fn
, data
, pfd
->visited
,
7752 pfd
->include_nondeduced_p
))
7753 return error_mark_node
;
7757 /* Since we're not going to walk subtrees, we have to do this
7759 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
7760 pfd
->visited
, pfd
->include_nondeduced_p
))
7761 return error_mark_node
;
7765 /* Check the return type. */
7766 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7767 pfd
->include_nondeduced_p
))
7768 return error_mark_node
;
7770 /* Check the parameter types. Since default arguments are not
7771 instantiated until they are needed, the TYPE_ARG_TYPES may
7772 contain expressions that involve template parameters. But,
7773 no-one should be looking at them yet. And, once they're
7774 instantiated, they don't contain template parameters, so
7775 there's no point in looking at them then, either. */
7779 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
7780 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
7781 pfd
->visited
, pfd
->include_nondeduced_p
))
7782 return error_mark_node
;
7784 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7785 want walk_tree walking into them itself. */
7791 case UNDERLYING_TYPE
:
7792 if (pfd
->include_nondeduced_p
7793 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
7795 pfd
->include_nondeduced_p
))
7796 return error_mark_node
;
7801 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
7802 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
7803 pfd
->visited
, pfd
->include_nondeduced_p
))
7804 return error_mark_node
;
7809 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
7810 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
7811 pfd
->visited
, pfd
->include_nondeduced_p
))
7812 return error_mark_node
;
7813 if (DECL_CONTEXT (t
)
7814 && pfd
->include_nondeduced_p
7815 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
7816 pfd
->visited
, pfd
->include_nondeduced_p
))
7817 return error_mark_node
;
7820 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7821 /* Record template parameters such as `T' inside `TT<T>'. */
7822 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
7823 pfd
->include_nondeduced_p
))
7824 return error_mark_node
;
7827 case TEMPLATE_TEMPLATE_PARM
:
7828 case TEMPLATE_TYPE_PARM
:
7829 case TEMPLATE_PARM_INDEX
:
7830 if (fn
&& (*fn
)(t
, data
))
7831 return error_mark_node
;
7833 return error_mark_node
;
7837 /* A template template parameter is encountered. */
7838 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
7839 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7840 pfd
->include_nondeduced_p
))
7841 return error_mark_node
;
7843 /* Already substituted template template parameter */
7849 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
7851 pfd
->include_nondeduced_p
))
7852 return error_mark_node
;
7856 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
7857 && pfd
->include_nondeduced_p
7858 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7859 (TREE_TYPE (t
)), fn
, data
,
7860 pfd
->visited
, pfd
->include_nondeduced_p
))
7861 return error_mark_node
;
7866 /* If there's no type, then this thing must be some expression
7867 involving template parameters. */
7868 if (!fn
&& !TREE_TYPE (t
))
7869 return error_mark_node
;
7874 case IMPLICIT_CONV_EXPR
:
7875 case REINTERPRET_CAST_EXPR
:
7876 case CONST_CAST_EXPR
:
7877 case STATIC_CAST_EXPR
:
7878 case DYNAMIC_CAST_EXPR
:
7882 case PSEUDO_DTOR_EXPR
:
7884 return error_mark_node
;
7891 /* We didn't find any template parameters we liked. */
7895 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7896 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7897 call FN with the parameter and the DATA.
7898 If FN returns nonzero, the iteration is terminated, and
7899 for_each_template_parm returns 1. Otherwise, the iteration
7900 continues. If FN never returns a nonzero value, the value
7901 returned by for_each_template_parm is 0. If FN is NULL, it is
7902 considered to be the function which always returns 1.
7904 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7905 parameters that occur in non-deduced contexts. When false, only
7906 visits those template parameters that can be deduced. */
7909 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
7910 struct pointer_set_t
*visited
,
7911 bool include_nondeduced_p
)
7913 struct pair_fn_data pfd
;
7919 pfd
.include_nondeduced_p
= include_nondeduced_p
;
7921 /* Walk the tree. (Conceptually, we would like to walk without
7922 duplicates, but for_each_template_parm_r recursively calls
7923 for_each_template_parm, so we would need to reorganize a fair
7924 bit to use walk_tree_without_duplicates, so we keep our own
7927 pfd
.visited
= visited
;
7929 pfd
.visited
= pointer_set_create ();
7930 result
= cp_walk_tree (&t
,
7931 for_each_template_parm_r
,
7933 pfd
.visited
) != NULL_TREE
;
7938 pointer_set_destroy (pfd
.visited
);
7945 /* Returns true if T depends on any template parameter. */
7948 uses_template_parms (tree t
)
7951 int saved_processing_template_decl
;
7953 saved_processing_template_decl
= processing_template_decl
;
7954 if (!saved_processing_template_decl
)
7955 processing_template_decl
= 1;
7957 dependent_p
= dependent_type_p (t
);
7958 else if (TREE_CODE (t
) == TREE_VEC
)
7959 dependent_p
= any_dependent_template_arguments_p (t
);
7960 else if (TREE_CODE (t
) == TREE_LIST
)
7961 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
7962 || uses_template_parms (TREE_CHAIN (t
)));
7963 else if (TREE_CODE (t
) == TYPE_DECL
)
7964 dependent_p
= dependent_type_p (TREE_TYPE (t
));
7967 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
7968 || TREE_CODE (t
) == OVERLOAD
7971 || TREE_CODE (t
) == TRAIT_EXPR
7972 || TREE_CODE (t
) == CONSTRUCTOR
7973 || CONSTANT_CLASS_P (t
))
7974 dependent_p
= (type_dependent_expression_p (t
)
7975 || value_dependent_expression_p (t
));
7978 gcc_assert (t
== error_mark_node
);
7979 dependent_p
= false;
7982 processing_template_decl
= saved_processing_template_decl
;
7987 /* Returns true iff current_function_decl is an incompletely instantiated
7988 template. Useful instead of processing_template_decl because the latter
7989 is set to 0 during fold_non_dependent_expr. */
7992 in_template_function (void)
7994 tree fn
= current_function_decl
;
7996 ++processing_template_decl
;
7997 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
7998 && DECL_TEMPLATE_INFO (fn
)
7999 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
8000 --processing_template_decl
;
8004 /* Returns true if T depends on any template parameter with level LEVEL. */
8007 uses_template_parms_level (tree t
, int level
)
8009 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
8010 /*include_nondeduced_p=*/true);
8013 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8014 ill-formed translation unit, i.e. a variable or function that isn't
8015 usable in a constant expression. */
8018 neglectable_inst_p (tree d
)
8021 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
8022 : decl_maybe_constant_var_p (d
)));
8025 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8026 neglectable and instantiated from within an erroneous instantiation. */
8029 limit_bad_template_recursion (tree decl
)
8031 struct tinst_level
*lev
= current_tinst_level
;
8032 int errs
= errorcount
+ sorrycount
;
8033 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
8036 for (; lev
; lev
= lev
->next
)
8037 if (neglectable_inst_p (lev
->decl
))
8040 return (lev
&& errs
> lev
->errors
);
8043 static int tinst_depth
;
8044 extern int max_tinst_depth
;
8047 static GTY(()) struct tinst_level
*last_error_tinst_level
;
8049 /* We're starting to instantiate D; record the template instantiation context
8050 for diagnostics and to restore it later. */
8053 push_tinst_level (tree d
)
8055 struct tinst_level
*new_level
;
8057 if (tinst_depth
>= max_tinst_depth
)
8059 last_error_tinst_level
= current_tinst_level
;
8060 if (TREE_CODE (d
) == TREE_LIST
)
8061 error ("template instantiation depth exceeds maximum of %d (use "
8062 "-ftemplate-depth= to increase the maximum) substituting %qS",
8063 max_tinst_depth
, d
);
8065 error ("template instantiation depth exceeds maximum of %d (use "
8066 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8067 max_tinst_depth
, d
);
8069 print_instantiation_context ();
8074 /* If the current instantiation caused problems, don't let it instantiate
8075 anything else. Do allow deduction substitution and decls usable in
8076 constant expressions. */
8077 if (limit_bad_template_recursion (d
))
8080 new_level
= ggc_alloc_tinst_level ();
8081 new_level
->decl
= d
;
8082 new_level
->locus
= input_location
;
8083 new_level
->errors
= errorcount
+sorrycount
;
8084 new_level
->in_system_header_p
= in_system_header
;
8085 new_level
->next
= current_tinst_level
;
8086 current_tinst_level
= new_level
;
8089 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
8090 depth_reached
= tinst_depth
;
8095 /* We're done instantiating this template; return to the instantiation
8099 pop_tinst_level (void)
8101 /* Restore the filename and line number stashed away when we started
8102 this instantiation. */
8103 input_location
= current_tinst_level
->locus
;
8104 current_tinst_level
= current_tinst_level
->next
;
8108 /* We're instantiating a deferred template; restore the template
8109 instantiation context in which the instantiation was requested, which
8110 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8113 reopen_tinst_level (struct tinst_level
*level
)
8115 struct tinst_level
*t
;
8118 for (t
= level
; t
; t
= t
->next
)
8121 current_tinst_level
= level
;
8123 if (current_tinst_level
)
8124 current_tinst_level
->errors
= errorcount
+sorrycount
;
8128 /* Returns the TINST_LEVEL which gives the original instantiation
8131 struct tinst_level
*
8132 outermost_tinst_level (void)
8134 struct tinst_level
*level
= current_tinst_level
;
8137 level
= level
->next
;
8141 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8142 vector of template arguments, as for tsubst.
8144 Returns an appropriate tsubst'd friend declaration. */
8147 tsubst_friend_function (tree decl
, tree args
)
8151 if (TREE_CODE (decl
) == FUNCTION_DECL
8152 && DECL_TEMPLATE_INSTANTIATION (decl
)
8153 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
8154 /* This was a friend declared with an explicit template
8155 argument list, e.g.:
8159 to indicate that f was a template instantiation, not a new
8160 function declaration. Now, we have to figure out what
8161 instantiation of what template. */
8163 tree template_id
, arglist
, fns
;
8166 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
8168 /* Friend functions are looked up in the containing namespace scope.
8169 We must enter that scope, to avoid finding member functions of the
8170 current class with same name. */
8171 push_nested_namespace (ns
);
8172 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
8173 tf_warning_or_error
, NULL_TREE
,
8174 /*integral_constant_expression_p=*/false);
8175 pop_nested_namespace (ns
);
8176 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
8177 tf_warning_or_error
, NULL_TREE
);
8178 template_id
= lookup_template_function (fns
, arglist
);
8180 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8181 tmpl
= determine_specialization (template_id
, new_friend
,
8183 /*need_member_template=*/0,
8184 TREE_VEC_LENGTH (args
),
8186 return instantiate_template (tmpl
, new_args
, tf_error
);
8189 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8191 /* The NEW_FRIEND will look like an instantiation, to the
8192 compiler, but is not an instantiation from the point of view of
8193 the language. For example, we might have had:
8195 template <class T> struct S {
8196 template <class U> friend void f(T, U);
8199 Then, in S<int>, template <class U> void f(int, U) is not an
8200 instantiation of anything. */
8201 if (new_friend
== error_mark_node
)
8202 return error_mark_node
;
8204 DECL_USE_TEMPLATE (new_friend
) = 0;
8205 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8207 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
8208 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
8209 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
8212 /* The mangled name for the NEW_FRIEND is incorrect. The function
8213 is not a template instantiation and should not be mangled like
8214 one. Therefore, we forget the mangling here; we'll recompute it
8215 later if we need it. */
8216 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
8218 SET_DECL_RTL (new_friend
, NULL
);
8219 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
8222 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
8225 tree new_friend_template_info
;
8226 tree new_friend_result_template_info
;
8228 int new_friend_is_defn
;
8230 /* We must save some information from NEW_FRIEND before calling
8231 duplicate decls since that function will free NEW_FRIEND if
8233 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
8234 new_friend_is_defn
=
8235 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8236 (template_for_substitution (new_friend
)))
8238 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
8240 /* This declaration is a `primary' template. */
8241 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
8243 new_friend_result_template_info
8244 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
8247 new_friend_result_template_info
= NULL_TREE
;
8249 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8250 if (new_friend_is_defn
)
8251 DECL_INITIAL (new_friend
) = error_mark_node
;
8253 /* Inside pushdecl_namespace_level, we will push into the
8254 current namespace. However, the friend function should go
8255 into the namespace of the template. */
8256 ns
= decl_namespace_context (new_friend
);
8257 push_nested_namespace (ns
);
8258 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
8259 pop_nested_namespace (ns
);
8261 if (old_decl
== error_mark_node
)
8262 return error_mark_node
;
8264 if (old_decl
!= new_friend
)
8266 /* This new friend declaration matched an existing
8267 declaration. For example, given:
8269 template <class T> void f(T);
8270 template <class U> class C {
8271 template <class T> friend void f(T) {}
8274 the friend declaration actually provides the definition
8275 of `f', once C has been instantiated for some type. So,
8276 old_decl will be the out-of-class template declaration,
8277 while new_friend is the in-class definition.
8279 But, if `f' was called before this point, the
8280 instantiation of `f' will have DECL_TI_ARGS corresponding
8281 to `T' but not to `U', references to which might appear
8282 in the definition of `f'. Previously, the most general
8283 template for an instantiation of `f' was the out-of-class
8284 version; now it is the in-class version. Therefore, we
8285 run through all specialization of `f', adding to their
8286 DECL_TI_ARGS appropriately. In particular, they need a
8287 new set of outer arguments, corresponding to the
8288 arguments for this class instantiation.
8290 The same situation can arise with something like this:
8293 template <class T> class C {
8297 when `C<int>' is instantiated. Now, `f(int)' is defined
8300 if (!new_friend_is_defn
)
8301 /* On the other hand, if the in-class declaration does
8302 *not* provide a definition, then we don't want to alter
8303 existing definitions. We can just leave everything
8308 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
8309 tree new_args
= TI_ARGS (new_friend_template_info
);
8311 /* Overwrite whatever template info was there before, if
8312 any, with the new template information pertaining to
8314 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
8316 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
8318 /* We should have called reregister_specialization in
8320 gcc_assert (retrieve_specialization (new_template
,
8324 /* Instantiate it if the global has already been used. */
8325 if (DECL_ODR_USED (old_decl
))
8326 instantiate_decl (old_decl
, /*defer_ok=*/true,
8327 /*expl_inst_class_mem_p=*/false);
8333 /* Indicate that the old function template is a partial
8335 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
8336 = new_friend_result_template_info
;
8338 gcc_assert (new_template
8339 == most_general_template (new_template
));
8340 gcc_assert (new_template
!= old_decl
);
8342 /* Reassign any specializations already in the hash table
8343 to the new more general template, and add the
8344 additional template args. */
8345 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
8349 tree spec
= TREE_VALUE (t
);
8352 elt
.tmpl
= old_decl
;
8353 elt
.args
= DECL_TI_ARGS (spec
);
8354 elt
.spec
= NULL_TREE
;
8356 htab_remove_elt (decl_specializations
, &elt
);
8359 = add_outermost_template_args (new_args
,
8360 DECL_TI_ARGS (spec
));
8362 register_specialization
8363 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
8366 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
8370 /* The information from NEW_FRIEND has been merged into OLD_DECL
8371 by duplicate_decls. */
8372 new_friend
= old_decl
;
8377 tree context
= DECL_CONTEXT (new_friend
);
8381 template <class T> class C {
8382 template <class U> friend void C1<U>::f (); // case 1
8383 friend void C2<T>::f (); // case 2
8385 we only need to make sure CONTEXT is a complete type for
8386 case 2. To distinguish between the two cases, we note that
8387 CONTEXT of case 1 remains dependent type after tsubst while
8388 this isn't true for case 2. */
8389 ++processing_template_decl
;
8390 dependent_p
= dependent_type_p (context
);
8391 --processing_template_decl
;
8394 && !complete_type_or_else (context
, NULL_TREE
))
8395 return error_mark_node
;
8397 if (COMPLETE_TYPE_P (context
))
8399 /* Check to see that the declaration is really present, and,
8400 possibly obtain an improved declaration. */
8401 tree fn
= check_classfn (context
,
8402 new_friend
, NULL_TREE
);
8412 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8413 template arguments, as for tsubst.
8415 Returns an appropriate tsubst'd friend type or error_mark_node on
8419 tsubst_friend_class (tree friend_tmpl
, tree args
)
8425 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
8427 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
8428 return TREE_TYPE (t
);
8431 context
= CP_DECL_CONTEXT (friend_tmpl
);
8433 if (context
!= global_namespace
)
8435 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8436 push_nested_namespace (context
);
8438 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
8441 /* Look for a class template declaration. We look for hidden names
8442 because two friend declarations of the same template are the
8443 same. For example, in:
8446 template <typename> friend class F;
8448 template <typename> struct B {
8449 template <typename> friend class F;
8452 both F templates are the same. */
8453 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
8454 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
8456 /* But, if we don't find one, it might be because we're in a
8457 situation like this:
8465 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8466 for `S<int>', not the TEMPLATE_DECL. */
8467 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
8469 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
8470 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
8473 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
8475 /* The friend template has already been declared. Just
8476 check to see that the declarations match, and install any new
8477 default parameters. We must tsubst the default parameters,
8478 of course. We only need the innermost template parameters
8479 because that is all that redeclare_class_template will look
8481 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
8482 > TMPL_ARGS_DEPTH (args
))
8485 location_t saved_input_location
;
8486 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
8487 args
, tf_warning_or_error
);
8489 saved_input_location
= input_location
;
8490 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
8491 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
8492 input_location
= saved_input_location
;
8496 friend_type
= TREE_TYPE (tmpl
);
8500 /* The friend template has not already been declared. In this
8501 case, the instantiation of the template class will cause the
8502 injection of this template into the global scope. */
8503 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
8504 if (tmpl
== error_mark_node
)
8505 return error_mark_node
;
8507 /* The new TMPL is not an instantiation of anything, so we
8508 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8509 the new type because that is supposed to be the corresponding
8510 template decl, i.e., TMPL. */
8511 DECL_USE_TEMPLATE (tmpl
) = 0;
8512 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
8513 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
8514 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
8515 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
8517 /* Inject this template into the global scope. */
8518 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
8521 if (context
!= global_namespace
)
8523 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8524 pop_nested_namespace (context
);
8526 pop_nested_class ();
8532 /* Returns zero if TYPE cannot be completed later due to circularity.
8533 Otherwise returns one. */
8536 can_complete_type_without_circularity (tree type
)
8538 if (type
== NULL_TREE
|| type
== error_mark_node
)
8540 else if (COMPLETE_TYPE_P (type
))
8542 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
8543 return can_complete_type_without_circularity (TREE_TYPE (type
));
8544 else if (CLASS_TYPE_P (type
)
8545 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
8551 static tree
tsubst_omp_clauses (tree
, bool, tree
, tsubst_flags_t
, tree
);
8553 /* Apply any attributes which had to be deferred until instantiation
8554 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8555 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8558 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
8559 tree args
, tsubst_flags_t complain
, tree in_decl
)
8561 tree last_dep
= NULL_TREE
;
8565 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
8566 if (ATTR_IS_DEPENDENT (t
))
8569 attributes
= copy_list (attributes
);
8573 if (DECL_P (*decl_p
))
8575 if (TREE_TYPE (*decl_p
) == error_mark_node
)
8577 p
= &DECL_ATTRIBUTES (*decl_p
);
8580 p
= &TYPE_ATTRIBUTES (*decl_p
);
8584 tree late_attrs
= NULL_TREE
;
8585 tree
*q
= &late_attrs
;
8587 for (*p
= attributes
; *p
; )
8590 if (ATTR_IS_DEPENDENT (t
))
8592 *p
= TREE_CHAIN (t
);
8593 TREE_CHAIN (t
) = NULL_TREE
;
8595 && is_attribute_p ("omp declare simd",
8596 get_attribute_name (t
))
8599 tree clauses
= TREE_VALUE (TREE_VALUE (t
));
8600 clauses
= tsubst_omp_clauses (clauses
, true, args
,
8602 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
8603 clauses
= finish_omp_clauses (clauses
);
8604 tree parms
= DECL_ARGUMENTS (*decl_p
);
8606 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
8608 TREE_VALUE (TREE_VALUE (t
)) = clauses
;
8610 TREE_VALUE (t
) = NULL_TREE
;
8612 /* If the first attribute argument is an identifier, don't
8613 pass it through tsubst. Attributes like mode, format,
8614 cleanup and several target specific attributes expect it
8616 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
8619 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
8621 /*integral_constant_expression_p=*/false);
8622 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
8624 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
8629 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
8630 /*integral_constant_expression_p=*/false);
8632 q
= &TREE_CHAIN (t
);
8635 p
= &TREE_CHAIN (t
);
8638 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
8642 /* Perform (or defer) access check for typedefs that were referenced
8643 from within the template TMPL code.
8644 This is a subroutine of instantiate_decl and instantiate_class_template.
8645 TMPL is the template to consider and TARGS is the list of arguments of
8649 perform_typedefs_access_check (tree tmpl
, tree targs
)
8651 location_t saved_location
;
8653 qualified_typedef_usage_t
*iter
;
8656 || (!CLASS_TYPE_P (tmpl
)
8657 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
8660 saved_location
= input_location
;
8661 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
8663 tree type_decl
= iter
->typedef_decl
;
8664 tree type_scope
= iter
->context
;
8666 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
8669 if (uses_template_parms (type_decl
))
8670 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
8671 if (uses_template_parms (type_scope
))
8672 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
8674 /* Make access check error messages point to the location
8675 of the use of the typedef. */
8676 input_location
= iter
->locus
;
8677 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
8678 type_decl
, type_decl
,
8679 tf_warning_or_error
);
8681 input_location
= saved_location
;
8685 instantiate_class_template_1 (tree type
)
8687 tree templ
, args
, pattern
, t
, member
;
8691 unsigned int saved_maximum_field_alignment
;
8694 if (type
== error_mark_node
)
8695 return error_mark_node
;
8697 if (COMPLETE_OR_OPEN_TYPE_P (type
)
8698 || uses_template_parms (type
))
8701 /* Figure out which template is being instantiated. */
8702 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
8703 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
8705 /* Determine what specialization of the original template to
8707 t
= most_specialized_class (type
, templ
, tf_warning_or_error
);
8708 if (t
== error_mark_node
)
8710 TYPE_BEING_DEFINED (type
) = 1;
8711 return error_mark_node
;
8715 /* This TYPE is actually an instantiation of a partial
8716 specialization. We replace the innermost set of ARGS with
8717 the arguments appropriate for substitution. For example,
8720 template <class T> struct S {};
8721 template <class T> struct S<T*> {};
8723 and supposing that we are instantiating S<int*>, ARGS will
8724 presently be {int*} -- but we need {int}. */
8725 pattern
= TREE_TYPE (t
);
8726 args
= TREE_PURPOSE (t
);
8730 pattern
= TREE_TYPE (templ
);
8731 args
= CLASSTYPE_TI_ARGS (type
);
8734 /* If the template we're instantiating is incomplete, then clearly
8735 there's nothing we can do. */
8736 if (!COMPLETE_TYPE_P (pattern
))
8739 /* If we've recursively instantiated too many templates, stop. */
8740 if (! push_tinst_level (type
))
8743 /* Now we're really doing the instantiation. Mark the type as in
8744 the process of being defined. */
8745 TYPE_BEING_DEFINED (type
) = 1;
8747 /* We may be in the middle of deferred access check. Disable
8749 push_deferring_access_checks (dk_no_deferred
);
8751 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
8753 push_to_top_level ();
8754 /* Use #pragma pack from the template context. */
8755 saved_maximum_field_alignment
= maximum_field_alignment
;
8756 maximum_field_alignment
= TYPE_PRECISION (pattern
);
8758 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
8760 /* Set the input location to the most specialized template definition.
8761 This is needed if tsubsting causes an error. */
8762 typedecl
= TYPE_MAIN_DECL (pattern
);
8763 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
8764 DECL_SOURCE_LOCATION (typedecl
);
8766 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
8767 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
8768 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
8769 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
8770 if (ANON_AGGR_TYPE_P (pattern
))
8771 SET_ANON_AGGR_TYPE_P (type
);
8772 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
8774 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
8775 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
8776 /* Adjust visibility for template arguments. */
8777 determine_visibility (TYPE_MAIN_DECL (type
));
8779 if (CLASS_TYPE_P (type
))
8780 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
8782 pbinfo
= TYPE_BINFO (pattern
);
8784 /* We should never instantiate a nested class before its enclosing
8785 class; we need to look up the nested class by name before we can
8786 instantiate it, and that lookup should instantiate the enclosing
8788 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
8789 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
8791 base_list
= NULL_TREE
;
8792 if (BINFO_N_BASE_BINFOS (pbinfo
))
8798 /* We must enter the scope containing the type, as that is where
8799 the accessibility of types named in dependent bases are
8801 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
8803 /* Substitute into each of the bases to determine the actual
8805 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
8808 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
8809 tree expanded_bases
= NULL_TREE
;
8812 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
8815 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
8816 args
, tf_error
, NULL_TREE
);
8817 if (expanded_bases
== error_mark_node
)
8820 len
= TREE_VEC_LENGTH (expanded_bases
);
8823 for (idx
= 0; idx
< len
; idx
++)
8826 /* Extract the already-expanded base class. */
8827 base
= TREE_VEC_ELT (expanded_bases
, idx
);
8829 /* Substitute to figure out the base class. */
8830 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
8833 if (base
== error_mark_node
)
8836 base_list
= tree_cons (access
, base
, base_list
);
8837 if (BINFO_VIRTUAL_P (pbase_binfo
))
8838 TREE_TYPE (base_list
) = integer_type_node
;
8842 /* The list is now in reverse order; correct that. */
8843 base_list
= nreverse (base_list
);
8846 pop_scope (pushed_scope
);
8848 /* Now call xref_basetypes to set up all the base-class
8850 xref_basetypes (type
, base_list
);
8852 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
8853 (int) ATTR_FLAG_TYPE_IN_PLACE
,
8854 args
, tf_error
, NULL_TREE
);
8855 fixup_attribute_variants (type
);
8857 /* Now that our base classes are set up, enter the scope of the
8858 class, so that name lookups into base classes, etc. will work
8859 correctly. This is precisely analogous to what we do in
8860 begin_class_definition when defining an ordinary non-template
8861 class, except we also need to push the enclosing classes. */
8862 push_nested_class (type
);
8864 /* Now members are processed in the order of declaration. */
8865 for (member
= CLASSTYPE_DECL_LIST (pattern
);
8866 member
; member
= TREE_CHAIN (member
))
8868 tree t
= TREE_VALUE (member
);
8870 if (TREE_PURPOSE (member
))
8874 /* Build new CLASSTYPE_NESTED_UTDS. */
8877 bool class_template_p
;
8879 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
8880 && TYPE_LANG_SPECIFIC (t
)
8881 && CLASSTYPE_IS_TEMPLATE (t
));
8882 /* If the member is a class template, then -- even after
8883 substitution -- there may be dependent types in the
8884 template argument list for the class. We increment
8885 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8886 that function will assume that no types are dependent
8887 when outside of a template. */
8888 if (class_template_p
)
8889 ++processing_template_decl
;
8890 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8891 if (class_template_p
)
8892 --processing_template_decl
;
8893 if (newtag
== error_mark_node
)
8896 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
8898 tree name
= TYPE_IDENTIFIER (t
);
8900 if (class_template_p
)
8901 /* Unfortunately, lookup_template_class sets
8902 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8903 instantiation (i.e., for the type of a member
8904 template class nested within a template class.)
8905 This behavior is required for
8906 maybe_process_partial_specialization to work
8907 correctly, but is not accurate in this case;
8908 the TAG is not an instantiation of anything.
8909 (The corresponding TEMPLATE_DECL is an
8910 instantiation, but the TYPE is not.) */
8911 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
8913 /* Now, we call pushtag to put this NEWTAG into the scope of
8914 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8915 pushtag calling push_template_decl. We don't have to do
8916 this for enums because it will already have been done in
8919 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
8920 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
8923 else if (DECL_DECLARES_FUNCTION_P (t
))
8925 /* Build new TYPE_METHODS. */
8928 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8929 ++processing_template_decl
;
8930 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8931 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8932 --processing_template_decl
;
8933 set_current_access_from_decl (r
);
8934 finish_member_declaration (r
);
8935 /* Instantiate members marked with attribute used. */
8936 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8938 if (TREE_CODE (r
) == FUNCTION_DECL
8939 && DECL_OMP_DECLARE_REDUCTION_P (r
))
8940 cp_check_omp_declare_reduction (r
);
8944 /* Build new TYPE_FIELDS. */
8945 if (TREE_CODE (t
) == STATIC_ASSERT
)
8949 ++c_inhibit_evaluation_warnings
;
8951 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
8952 tf_warning_or_error
, NULL_TREE
,
8953 /*integral_constant_expression_p=*/true);
8954 --c_inhibit_evaluation_warnings
;
8956 finish_static_assert (condition
,
8957 STATIC_ASSERT_MESSAGE (t
),
8958 STATIC_ASSERT_SOURCE_LOCATION (t
),
8961 else if (TREE_CODE (t
) != CONST_DECL
)
8964 tree vec
= NULL_TREE
;
8967 /* The file and line for this declaration, to
8968 assist in error message reporting. Since we
8969 called push_tinst_level above, we don't need to
8971 input_location
= DECL_SOURCE_LOCATION (t
);
8973 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8974 ++processing_template_decl
;
8975 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
8976 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8977 --processing_template_decl
;
8979 if (TREE_CODE (r
) == TREE_VEC
)
8981 /* A capture pack became multiple fields. */
8983 len
= TREE_VEC_LENGTH (vec
);
8986 for (int i
= 0; i
< len
; ++i
)
8989 r
= TREE_VEC_ELT (vec
, i
);
8994 [t]he initialization (and any associated
8995 side-effects) of a static data member does
8996 not occur unless the static data member is
8997 itself used in a way that requires the
8998 definition of the static data member to
9001 Therefore, we do not substitute into the
9002 initialized for the static data member here. */
9003 finish_static_data_member_decl
9006 /*init_const_expr_p=*/false,
9007 /*asmspec_tree=*/NULL_TREE
,
9009 /* Instantiate members marked with attribute used. */
9010 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
9013 else if (TREE_CODE (r
) == FIELD_DECL
)
9015 /* Determine whether R has a valid type and can be
9016 completed later. If R is invalid, then its type
9017 is replaced by error_mark_node. */
9018 tree rtype
= TREE_TYPE (r
);
9019 if (can_complete_type_without_circularity (rtype
))
9020 complete_type (rtype
);
9022 if (!COMPLETE_TYPE_P (rtype
))
9024 cxx_incomplete_type_error (r
, rtype
);
9025 TREE_TYPE (r
) = error_mark_node
;
9029 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9030 such a thing will already have been added to the field
9031 list by tsubst_enum in finish_member_declaration in the
9032 CLASSTYPE_NESTED_UTDS case above. */
9033 if (!(TREE_CODE (r
) == TYPE_DECL
9034 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
9035 && DECL_ARTIFICIAL (r
)))
9037 set_current_access_from_decl (r
);
9038 finish_member_declaration (r
);
9046 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
9047 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9049 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9051 tree friend_type
= t
;
9052 bool adjust_processing_template_decl
= false;
9054 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9056 /* template <class T> friend class C; */
9057 friend_type
= tsubst_friend_class (friend_type
, args
);
9058 adjust_processing_template_decl
= true;
9060 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
9062 /* template <class T> friend class C::D; */
9063 friend_type
= tsubst (friend_type
, args
,
9064 tf_warning_or_error
, NULL_TREE
);
9065 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9066 friend_type
= TREE_TYPE (friend_type
);
9067 adjust_processing_template_decl
= true;
9069 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
9070 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
9072 /* This could be either
9076 when dependent_type_p is false or
9078 template <class U> friend class T::C;
9081 friend_type
= tsubst (friend_type
, args
,
9082 tf_warning_or_error
, NULL_TREE
);
9083 /* Bump processing_template_decl for correct
9084 dependent_type_p calculation. */
9085 ++processing_template_decl
;
9086 if (dependent_type_p (friend_type
))
9087 adjust_processing_template_decl
= true;
9088 --processing_template_decl
;
9090 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
9091 && hidden_name_p (TYPE_NAME (friend_type
)))
9095 where C hasn't been declared yet. Let's lookup name
9096 from namespace scope directly, bypassing any name that
9097 come from dependent base class. */
9098 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
9100 /* The call to xref_tag_from_type does injection for friend
9102 push_nested_namespace (ns
);
9104 xref_tag_from_type (friend_type
, NULL_TREE
,
9105 /*tag_scope=*/ts_current
);
9106 pop_nested_namespace (ns
);
9108 else if (uses_template_parms (friend_type
))
9109 /* friend class C<T>; */
9110 friend_type
= tsubst (friend_type
, args
,
9111 tf_warning_or_error
, NULL_TREE
);
9116 where C is already declared or
9118 friend class C<int>;
9120 We don't have to do anything in these cases. */
9122 if (adjust_processing_template_decl
)
9123 /* Trick make_friend_class into realizing that the friend
9124 we're adding is a template, not an ordinary class. It's
9125 important that we use make_friend_class since it will
9126 perform some error-checking and output cross-reference
9128 ++processing_template_decl
;
9130 if (friend_type
!= error_mark_node
)
9131 make_friend_class (type
, friend_type
, /*complain=*/false);
9133 if (adjust_processing_template_decl
)
9134 --processing_template_decl
;
9138 /* Build new DECL_FRIENDLIST. */
9141 /* The file and line for this declaration, to
9142 assist in error message reporting. Since we
9143 called push_tinst_level above, we don't need to
9145 input_location
= DECL_SOURCE_LOCATION (t
);
9147 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9149 ++processing_template_decl
;
9150 push_deferring_access_checks (dk_no_check
);
9153 r
= tsubst_friend_function (t
, args
);
9154 add_friend (type
, r
, /*complain=*/false);
9155 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9157 pop_deferring_access_checks ();
9158 --processing_template_decl
;
9164 if (tree expr
= CLASSTYPE_LAMBDA_EXPR (type
))
9166 tree decl
= lambda_function (type
);
9169 if (!DECL_TEMPLATE_INFO (decl
)
9170 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl
)) != decl
)
9171 instantiate_decl (decl
, false, false);
9173 /* We need to instantiate the capture list from the template
9174 after we've instantiated the closure members, but before we
9175 consider adding the conversion op. Also keep any captures
9176 that may have been added during instantiation of the op(). */
9177 tree tmpl_expr
= CLASSTYPE_LAMBDA_EXPR (pattern
);
9179 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr
),
9180 args
, tf_warning_or_error
, NULL_TREE
,
9183 LAMBDA_EXPR_CAPTURE_LIST (expr
)
9184 = chainon (tmpl_cap
, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr
)));
9186 maybe_add_lambda_conv_op (type
);
9189 gcc_assert (errorcount
);
9192 /* Set the file and line number information to whatever is given for
9193 the class itself. This puts error messages involving generated
9194 implicit functions at a predictable point, and the same point
9195 that would be used for non-template classes. */
9196 input_location
= DECL_SOURCE_LOCATION (typedecl
);
9198 unreverse_member_declarations (type
);
9199 finish_struct_1 (type
);
9200 TYPE_BEING_DEFINED (type
) = 0;
9202 /* We don't instantiate default arguments for member functions. 14.7.1:
9204 The implicit instantiation of a class template specialization causes
9205 the implicit instantiation of the declarations, but not of the
9206 definitions or default arguments, of the class member functions,
9207 member classes, static data members and member templates.... */
9209 /* Some typedefs referenced from within the template code need to be access
9210 checked at template instantiation time, i.e now. These types were
9211 added to the template at parsing time. Let's get those and perform
9212 the access checks then. */
9213 perform_typedefs_access_check (pattern
, args
);
9214 perform_deferred_access_checks (tf_warning_or_error
);
9215 pop_nested_class ();
9216 maximum_field_alignment
= saved_maximum_field_alignment
;
9218 pop_from_top_level ();
9219 pop_deferring_access_checks ();
9222 /* The vtable for a template class can be emitted in any translation
9223 unit in which the class is instantiated. When there is no key
9224 method, however, finish_struct_1 will already have added TYPE to
9225 the keyed_classes list. */
9226 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
9227 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
9232 /* Wrapper for instantiate_class_template_1. */
9235 instantiate_class_template (tree type
)
9238 timevar_push (TV_TEMPLATE_INST
);
9239 ret
= instantiate_class_template_1 (type
);
9240 timevar_pop (TV_TEMPLATE_INST
);
9245 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9251 else if (TYPE_P (t
))
9252 r
= tsubst (t
, args
, complain
, in_decl
);
9255 if (!(complain
& tf_warning
))
9256 ++c_inhibit_evaluation_warnings
;
9257 r
= tsubst_expr (t
, args
, complain
, in_decl
,
9258 /*integral_constant_expression_p=*/true);
9259 if (!(complain
& tf_warning
))
9260 --c_inhibit_evaluation_warnings
;
9261 /* Preserve the raw-reference nature of T. */
9262 if (TREE_TYPE (t
) && TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
9263 && REFERENCE_REF_P (r
))
9264 r
= TREE_OPERAND (r
, 0);
9269 /* Given a function parameter pack TMPL_PARM and some function parameters
9270 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9271 and set *SPEC_P to point at the next point in the list. */
9274 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
9276 /* Collect all of the extra "packed" parameters into an
9280 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
9281 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
9282 tree spec_parm
= *spec_p
;
9285 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
9287 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
9290 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9291 parmvec
= make_tree_vec (len
);
9292 parmtypevec
= make_tree_vec (len
);
9293 spec_parm
= *spec_p
;
9294 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
9296 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
9297 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
9300 /* Build the argument packs. */
9301 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
9302 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
9303 TREE_TYPE (argpack
) = argtypepack
;
9304 *spec_p
= spec_parm
;
9309 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9310 NONTYPE_ARGUMENT_PACK. */
9313 make_fnparm_pack (tree spec_parm
)
9315 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
9318 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9322 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
9324 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
9325 if (i
>= TREE_VEC_LENGTH (vec
))
9327 return PACK_EXPANSION_P (TREE_VEC_ELT (vec
, i
));
9331 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9334 make_argument_pack_select (tree arg_pack
, unsigned index
)
9336 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
9338 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
9339 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9344 /* This is a subroutine of tsubst_pack_expansion.
9346 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9347 mechanism to store the (non complete list of) arguments of the
9348 substitution and return a non substituted pack expansion, in order
9349 to wait for when we have enough arguments to really perform the
9353 use_pack_expansion_extra_args_p (tree parm_packs
,
9357 /* If one pack has an expansion and another pack has a normal
9358 argument or if one pack has an empty argument and an another
9359 one hasn't then tsubst_pack_expansion cannot perform the
9360 substitution and need to fall back on the
9361 PACK_EXPANSION_EXTRA mechanism. */
9362 if (parm_packs
== NULL_TREE
)
9364 else if (has_empty_arg
)
9367 bool has_expansion_arg
= false;
9368 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
9370 bool has_non_expansion_arg
= false;
9371 for (tree parm_pack
= parm_packs
;
9373 parm_pack
= TREE_CHAIN (parm_pack
))
9375 tree arg
= TREE_VALUE (parm_pack
);
9377 if (argument_pack_element_is_expansion_p (arg
, i
))
9378 has_expansion_arg
= true;
9380 has_non_expansion_arg
= true;
9383 if (has_expansion_arg
&& has_non_expansion_arg
)
9389 /* [temp.variadic]/6 says that:
9391 The instantiation of a pack expansion [...]
9392 produces a list E1,E2, ..., En, where N is the number of elements
9393 in the pack expansion parameters.
9395 This subroutine of tsubst_pack_expansion produces one of these Ei.
9397 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9398 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9399 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9400 INDEX is the index 'i' of the element Ei to produce. ARGS,
9401 COMPLAIN, and IN_DECL are the same parameters as for the
9402 tsubst_pack_expansion function.
9404 The function returns the resulting Ei upon successful completion,
9407 Note that this function possibly modifies the ARGS parameter, so
9408 it's the responsibility of the caller to restore it. */
9411 gen_elem_of_pack_expansion_instantiation (tree pattern
,
9414 tree args
/* This parm gets
9416 tsubst_flags_t complain
,
9420 bool ith_elem_is_expansion
= false;
9422 /* For each parameter pack, change the substitution of the parameter
9423 pack to the ith argument in its argument pack, then expand the
9425 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
9427 tree parm
= TREE_PURPOSE (pack
);
9428 tree arg_pack
= TREE_VALUE (pack
);
9429 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
9431 ith_elem_is_expansion
|=
9432 argument_pack_element_is_expansion_p (arg_pack
, index
);
9434 /* Select the Ith argument from the pack. */
9435 if (TREE_CODE (parm
) == PARM_DECL
9436 || TREE_CODE (parm
) == FIELD_DECL
)
9440 aps
= make_argument_pack_select (arg_pack
, index
);
9442 register_local_specialization (aps
, parm
);
9445 aps
= retrieve_local_specialization (parm
);
9450 template_parm_level_and_index (parm
, &level
, &idx
);
9454 aps
= make_argument_pack_select (arg_pack
, index
);
9455 /* Update the corresponding argument. */
9456 TMPL_ARG (args
, level
, idx
) = aps
;
9459 /* Re-use the ARGUMENT_PACK_SELECT. */
9460 aps
= TMPL_ARG (args
, level
, idx
);
9462 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9465 /* Substitute into the PATTERN with the (possibly altered)
9467 if (!TYPE_P (pattern
))
9468 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9469 /*integral_constant_expression_p=*/false);
9471 t
= tsubst (pattern
, args
, complain
, in_decl
);
9473 /* If the Ith argument pack element is a pack expansion, then
9474 the Ith element resulting from the substituting is going to
9475 be a pack expansion as well. */
9476 if (ith_elem_is_expansion
)
9477 t
= make_pack_expansion (t
);
9482 /* Substitute ARGS into T, which is an pack expansion
9483 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9484 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9485 (if only a partial substitution could be performed) or
9486 ERROR_MARK_NODE if there was an error. */
9488 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
9492 tree pack
, packs
= NULL_TREE
;
9493 bool unsubstituted_packs
= false;
9496 struct pointer_map_t
*saved_local_specializations
= NULL
;
9497 bool need_local_specializations
= false;
9500 gcc_assert (PACK_EXPANSION_P (t
));
9501 pattern
= PACK_EXPANSION_PATTERN (t
);
9503 /* Add in any args remembered from an earlier partial instantiation. */
9504 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
9506 levels
= TMPL_ARGS_DEPTH (args
);
9508 /* Determine the argument packs that will instantiate the parameter
9509 packs used in the expansion expression. While we're at it,
9510 compute the number of arguments to be expanded and make sure it
9512 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
9513 pack
= TREE_CHAIN (pack
))
9515 tree parm_pack
= TREE_VALUE (pack
);
9516 tree arg_pack
= NULL_TREE
;
9517 tree orig_arg
= NULL_TREE
;
9520 if (TREE_CODE (parm_pack
) == BASES
)
9522 if (BASES_DIRECT (parm_pack
))
9523 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9524 args
, complain
, in_decl
, false));
9526 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9527 args
, complain
, in_decl
, false));
9529 if (TREE_CODE (parm_pack
) == PARM_DECL
)
9531 if (PACK_EXPANSION_LOCAL_P (t
))
9532 arg_pack
= retrieve_local_specialization (parm_pack
);
9535 /* We can't rely on local_specializations for a parameter
9536 name used later in a function declaration (such as in a
9537 late-specified return type). Even if it exists, it might
9538 have the wrong value for a recursive call. Just make a
9539 dummy decl, since it's only used for its type. */
9540 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
9541 if (arg_pack
&& DECL_PACK_P (arg_pack
))
9542 /* Partial instantiation of the parm_pack, we can't build
9543 up an argument pack yet. */
9544 arg_pack
= NULL_TREE
;
9546 arg_pack
= make_fnparm_pack (arg_pack
);
9547 need_local_specializations
= true;
9550 else if (TREE_CODE (parm_pack
) == FIELD_DECL
)
9551 arg_pack
= tsubst_copy (parm_pack
, args
, complain
, in_decl
);
9555 template_parm_level_and_index (parm_pack
, &level
, &idx
);
9557 if (level
<= levels
)
9558 arg_pack
= TMPL_ARG (args
, level
, idx
);
9561 orig_arg
= arg_pack
;
9562 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
9563 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
9565 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
9566 /* This can only happen if we forget to expand an argument
9567 pack somewhere else. Just return an error, silently. */
9569 result
= make_tree_vec (1);
9570 TREE_VEC_ELT (result
, 0) = error_mark_node
;
9577 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
9579 /* Don't bother trying to do a partial substitution with
9580 incomplete packs; we'll try again after deduction. */
9581 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
9586 else if (len
!= my_len
)
9588 if (!(complain
& tf_error
))
9589 /* Fail quietly. */;
9590 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
9591 error ("mismatched argument pack lengths while expanding "
9595 error ("mismatched argument pack lengths while expanding "
9598 return error_mark_node
;
9601 /* Keep track of the parameter packs and their corresponding
9603 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
9604 TREE_TYPE (packs
) = orig_arg
;
9608 /* We can't substitute for this parameter pack. We use a flag as
9609 well as the missing_level counter because function parameter
9610 packs don't have a level. */
9611 unsubstituted_packs
= true;
9615 /* We cannot expand this expansion expression, because we don't have
9616 all of the argument packs we need. */
9617 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
9619 /* We got some full packs, but we can't substitute them in until we
9620 have values for all the packs. So remember these until then. */
9622 t
= make_pack_expansion (pattern
);
9623 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
9626 else if (unsubstituted_packs
)
9628 /* There were no real arguments, we're just replacing a parameter
9629 pack with another version of itself. Substitute into the
9630 pattern and return a PACK_EXPANSION_*. The caller will need to
9632 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
9633 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9634 /*integral_constant_expression_p=*/false);
9636 t
= tsubst (pattern
, args
, complain
, in_decl
);
9637 t
= make_pack_expansion (t
);
9641 gcc_assert (len
>= 0);
9643 if (need_local_specializations
)
9645 /* We're in a late-specified return type, so create our own local
9646 specializations map; the current map is either NULL or (in the
9647 case of recursive unification) might have bindings that we don't
9648 want to use or alter. */
9649 saved_local_specializations
= local_specializations
;
9650 local_specializations
= pointer_map_create ();
9653 /* For each argument in each argument pack, substitute into the
9655 result
= make_tree_vec (len
);
9656 for (i
= 0; i
< len
; ++i
)
9658 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
9662 TREE_VEC_ELT (result
, i
) = t
;
9663 if (t
== error_mark_node
)
9665 result
= error_mark_node
;
9670 /* Update ARGS to restore the substitution from parameter packs to
9671 their argument packs. */
9672 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9674 tree parm
= TREE_PURPOSE (pack
);
9676 if (TREE_CODE (parm
) == PARM_DECL
9677 || TREE_CODE (parm
) == FIELD_DECL
)
9678 register_local_specialization (TREE_TYPE (pack
), parm
);
9683 if (TREE_VALUE (pack
) == NULL_TREE
)
9686 template_parm_level_and_index (parm
, &level
, &idx
);
9688 /* Update the corresponding argument. */
9689 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
9690 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
9693 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
9697 if (need_local_specializations
)
9699 pointer_map_destroy (local_specializations
);
9700 local_specializations
= saved_local_specializations
;
9706 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9707 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9708 parameter packs; all parms generated from a function parameter pack will
9709 have the same DECL_PARM_INDEX. */
9712 get_pattern_parm (tree parm
, tree tmpl
)
9714 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9717 if (DECL_ARTIFICIAL (parm
))
9719 for (patparm
= DECL_ARGUMENTS (pattern
);
9720 patparm
; patparm
= DECL_CHAIN (patparm
))
9721 if (DECL_ARTIFICIAL (patparm
)
9722 && DECL_NAME (parm
) == DECL_NAME (patparm
))
9727 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
9728 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
9729 gcc_assert (DECL_PARM_INDEX (patparm
)
9730 == DECL_PARM_INDEX (parm
));
9736 /* Substitute ARGS into the vector or list of template arguments T. */
9739 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9742 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
9745 if (t
== error_mark_node
)
9746 return error_mark_node
;
9748 len
= TREE_VEC_LENGTH (t
);
9749 elts
= XALLOCAVEC (tree
, len
);
9751 for (i
= 0; i
< len
; i
++)
9753 tree orig_arg
= TREE_VEC_ELT (t
, i
);
9756 if (TREE_CODE (orig_arg
) == TREE_VEC
)
9757 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
9758 else if (PACK_EXPANSION_P (orig_arg
))
9760 /* Substitute into an expansion expression. */
9761 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
9763 if (TREE_CODE (new_arg
) == TREE_VEC
)
9764 /* Add to the expanded length adjustment the number of
9765 expanded arguments. We subtract one from this
9766 measurement, because the argument pack expression
9767 itself is already counted as 1 in
9768 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9769 the argument pack is empty. */
9770 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
9772 else if (ARGUMENT_PACK_P (orig_arg
))
9774 /* Substitute into each of the arguments. */
9775 new_arg
= TYPE_P (orig_arg
)
9776 ? cxx_make_type (TREE_CODE (orig_arg
))
9777 : make_node (TREE_CODE (orig_arg
));
9779 SET_ARGUMENT_PACK_ARGS (
9781 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
9782 args
, complain
, in_decl
));
9784 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
9785 new_arg
= error_mark_node
;
9787 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
9788 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
9790 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
9792 if (TREE_TYPE (new_arg
) == error_mark_node
)
9793 new_arg
= error_mark_node
;
9797 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
9799 if (new_arg
== error_mark_node
)
9800 return error_mark_node
;
9803 if (new_arg
!= orig_arg
)
9810 /* Make space for the expanded arguments coming from template
9812 t
= make_tree_vec (len
+ expanded_len_adjust
);
9813 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9814 arguments for a member template.
9815 In that case each TREE_VEC in ORIG_T represents a level of template
9816 arguments, and ORIG_T won't carry any non defaulted argument count.
9817 It will rather be the nested TREE_VECs that will carry one.
9818 In other words, ORIG_T carries a non defaulted argument count only
9819 if it doesn't contain any nested TREE_VEC. */
9820 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
9822 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
9823 count
+= expanded_len_adjust
;
9824 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
9826 for (i
= 0, out
= 0; i
< len
; i
++)
9828 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
9829 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
9830 && TREE_CODE (elts
[i
]) == TREE_VEC
)
9834 /* Now expand the template argument pack "in place". */
9835 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
9836 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
9840 TREE_VEC_ELT (t
, out
) = elts
[i
];
9848 /* Return the result of substituting ARGS into the template parameters
9849 given by PARMS. If there are m levels of ARGS and m + n levels of
9850 PARMS, then the result will contain n levels of PARMS. For
9851 example, if PARMS is `template <class T> template <class U>
9852 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9853 result will be `template <int*, double, class V>'. */
9856 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
9861 /* When substituting into a template, we must set
9862 PROCESSING_TEMPLATE_DECL as the template parameters may be
9863 dependent if they are based on one-another, and the dependency
9864 predicates are short-circuit outside of templates. */
9865 ++processing_template_decl
;
9867 for (new_parms
= &r
;
9868 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
9869 new_parms
= &(TREE_CHAIN (*new_parms
)),
9870 parms
= TREE_CHAIN (parms
))
9873 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
9876 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
9880 if (parms
== error_mark_node
)
9883 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
9885 if (tuple
== error_mark_node
)
9888 TREE_VEC_ELT (new_vec
, i
) =
9889 tsubst_template_parm (tuple
, args
, complain
);
9893 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
9894 - TMPL_ARGS_DEPTH (args
)),
9895 new_vec
, NULL_TREE
);
9898 --processing_template_decl
;
9903 /* Return the result of substituting ARGS into one template parameter
9904 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9905 parameter and which TREE_PURPOSE is the default argument of the
9906 template parameter. */
9909 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
9911 tree default_value
, parm_decl
;
9913 if (args
== NULL_TREE
9915 || t
== error_mark_node
)
9918 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
9920 default_value
= TREE_PURPOSE (t
);
9921 parm_decl
= TREE_VALUE (t
);
9923 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
9924 if (TREE_CODE (parm_decl
) == PARM_DECL
9925 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
9926 parm_decl
= error_mark_node
;
9927 default_value
= tsubst_template_arg (default_value
, args
,
9928 complain
, NULL_TREE
);
9930 return build_tree_list (default_value
, parm_decl
);
9933 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9934 type T. If T is not an aggregate or enumeration type, it is
9935 handled as if by tsubst. IN_DECL is as for tsubst. If
9936 ENTERING_SCOPE is nonzero, T is the context for a template which
9937 we are presently tsubst'ing. Return the substituted value. */
9940 tsubst_aggr_type (tree t
,
9942 tsubst_flags_t complain
,
9949 switch (TREE_CODE (t
))
9952 if (TYPE_PTRMEMFUNC_P (t
))
9953 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
9955 /* Else fall through. */
9958 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
9963 int saved_unevaluated_operand
;
9964 int saved_inhibit_evaluation_warnings
;
9966 /* In "sizeof(X<I>)" we need to evaluate "I". */
9967 saved_unevaluated_operand
= cp_unevaluated_operand
;
9968 cp_unevaluated_operand
= 0;
9969 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9970 c_inhibit_evaluation_warnings
= 0;
9972 /* First, determine the context for the type we are looking
9974 context
= TYPE_CONTEXT (t
);
9975 if (context
&& TYPE_P (context
))
9977 context
= tsubst_aggr_type (context
, args
, complain
,
9978 in_decl
, /*entering_scope=*/1);
9979 /* If context is a nested class inside a class template,
9980 it may still need to be instantiated (c++/33959). */
9981 context
= complete_type (context
);
9984 /* Then, figure out what arguments are appropriate for the
9985 type we are trying to find. For example, given:
9987 template <class T> struct S;
9988 template <class T, class U> void f(T, U) { S<U> su; }
9990 and supposing that we are instantiating f<int, double>,
9991 then our ARGS will be {int, double}, but, when looking up
9992 S we only want {double}. */
9993 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
9995 if (argvec
== error_mark_node
)
9996 r
= error_mark_node
;
9999 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
10000 entering_scope
, complain
);
10001 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
10004 cp_unevaluated_operand
= saved_unevaluated_operand
;
10005 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
10010 /* This is not a template type, so there's nothing to do. */
10014 return tsubst (t
, args
, complain
, in_decl
);
10018 /* Substitute into the default argument ARG (a default argument for
10019 FN), which has the indicated TYPE. */
10022 tsubst_default_argument (tree fn
, tree type
, tree arg
, tsubst_flags_t complain
)
10024 tree saved_class_ptr
= NULL_TREE
;
10025 tree saved_class_ref
= NULL_TREE
;
10026 int errs
= errorcount
+ sorrycount
;
10028 /* This can happen in invalid code. */
10029 if (TREE_CODE (arg
) == DEFAULT_ARG
)
10032 /* This default argument came from a template. Instantiate the
10033 default argument here, not in tsubst. In the case of
10042 we must be careful to do name lookup in the scope of S<T>,
10043 rather than in the current class. */
10044 push_access_scope (fn
);
10045 /* The "this" pointer is not valid in a default argument. */
10048 saved_class_ptr
= current_class_ptr
;
10049 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
10050 saved_class_ref
= current_class_ref
;
10051 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
10054 push_deferring_access_checks(dk_no_deferred
);
10055 /* The default argument expression may cause implicitly defined
10056 member functions to be synthesized, which will result in garbage
10057 collection. We must treat this situation as if we were within
10058 the body of function so as to avoid collecting live data on the
10061 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
10062 complain
, NULL_TREE
,
10063 /*integral_constant_expression_p=*/false);
10065 pop_deferring_access_checks();
10067 /* Restore the "this" pointer. */
10070 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
10071 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
10074 if (errorcount
+sorrycount
> errs
10075 && (complain
& tf_warning_or_error
))
10076 inform (input_location
,
10077 " when instantiating default argument for call to %D", fn
);
10079 /* Make sure the default argument is reasonable. */
10080 arg
= check_default_argument (type
, arg
, complain
);
10082 pop_access_scope (fn
);
10087 /* Substitute into all the default arguments for FN. */
10090 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
10095 tmpl_args
= DECL_TI_ARGS (fn
);
10097 /* If this function is not yet instantiated, we certainly don't need
10098 its default arguments. */
10099 if (uses_template_parms (tmpl_args
))
10101 /* Don't do this again for clones. */
10102 if (DECL_CLONED_FUNCTION_P (fn
))
10105 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
10107 arg
= TREE_CHAIN (arg
))
10108 if (TREE_PURPOSE (arg
))
10109 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
10111 TREE_PURPOSE (arg
),
10115 /* Substitute the ARGS into the T, which is a _DECL. Return the
10116 result of the substitution. Issue error and warning messages under
10117 control of COMPLAIN. */
10120 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
10122 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10123 location_t saved_loc
;
10124 tree r
= NULL_TREE
;
10126 hashval_t hash
= 0;
10128 /* Set the filename and linenumber to improve error-reporting. */
10129 saved_loc
= input_location
;
10130 input_location
= DECL_SOURCE_LOCATION (t
);
10132 switch (TREE_CODE (t
))
10134 case TEMPLATE_DECL
:
10136 /* We can get here when processing a member function template,
10137 member class template, or template template parameter. */
10138 tree decl
= DECL_TEMPLATE_RESULT (t
);
10143 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10145 /* Template template parameter is treated here. */
10146 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10147 if (new_type
== error_mark_node
)
10148 RETURN (error_mark_node
);
10149 /* If we get a real template back, return it. This can happen in
10150 the context of most_specialized_class. */
10151 if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
10155 DECL_CHAIN (r
) = NULL_TREE
;
10156 TREE_TYPE (r
) = new_type
;
10157 DECL_TEMPLATE_RESULT (r
)
10158 = build_decl (DECL_SOURCE_LOCATION (decl
),
10159 TYPE_DECL
, DECL_NAME (decl
), new_type
);
10160 DECL_TEMPLATE_PARMS (r
)
10161 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10163 TYPE_NAME (new_type
) = r
;
10167 /* We might already have an instance of this template.
10168 The ARGS are for the surrounding class type, so the
10169 full args contain the tsubst'd args for the context,
10170 plus the innermost args from the template decl. */
10171 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
10172 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
10173 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
10174 /* Because this is a template, the arguments will still be
10175 dependent, even after substitution. If
10176 PROCESSING_TEMPLATE_DECL is not set, the dependency
10177 predicates will short-circuit. */
10178 ++processing_template_decl
;
10179 full_args
= tsubst_template_args (tmpl_args
, args
,
10180 complain
, in_decl
);
10181 --processing_template_decl
;
10182 if (full_args
== error_mark_node
)
10183 RETURN (error_mark_node
);
10185 /* If this is a default template template argument,
10186 tsubst might not have changed anything. */
10187 if (full_args
== tmpl_args
)
10190 hash
= hash_tmpl_and_args (t
, full_args
);
10191 spec
= retrieve_specialization (t
, full_args
, hash
);
10192 if (spec
!= NULL_TREE
)
10198 /* Make a new template decl. It will be similar to the
10199 original, but will record the current template arguments.
10200 We also create a new function declaration, which is just
10201 like the old one, but points to this new template, rather
10202 than the old one. */
10204 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
10205 DECL_CHAIN (r
) = NULL_TREE
;
10207 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10209 if (TREE_CODE (decl
) == TYPE_DECL
10210 && !TYPE_DECL_ALIAS_P (decl
))
10213 ++processing_template_decl
;
10214 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10215 --processing_template_decl
;
10216 if (new_type
== error_mark_node
)
10217 RETURN (error_mark_node
);
10219 TREE_TYPE (r
) = new_type
;
10220 /* For a partial specialization, we need to keep pointing to
10221 the primary template. */
10222 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
10223 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
10224 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
10225 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
10226 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
10231 ++processing_template_decl
;
10232 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
10233 --processing_template_decl
;
10234 if (new_decl
== error_mark_node
)
10235 RETURN (error_mark_node
);
10237 DECL_TEMPLATE_RESULT (r
) = new_decl
;
10238 DECL_TI_TEMPLATE (new_decl
) = r
;
10239 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
10240 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
10241 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
10244 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10245 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
10246 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
10248 /* The template parameters for this new template are all the
10249 template parameters for the old template, except the
10250 outermost level of parameters. */
10251 DECL_TEMPLATE_PARMS (r
)
10252 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10255 if (PRIMARY_TEMPLATE_P (t
))
10256 DECL_PRIMARY_TEMPLATE (r
) = r
;
10258 if (TREE_CODE (decl
) != TYPE_DECL
)
10259 /* Record this non-type partial instantiation. */
10260 register_specialization (r
, t
,
10261 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
10266 case FUNCTION_DECL
:
10269 tree argvec
= NULL_TREE
;
10277 /* Nobody should be tsubst'ing into non-template functions. */
10278 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
10280 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
10285 /* If T is not dependent, just return it. We have to
10286 increment PROCESSING_TEMPLATE_DECL because
10287 value_dependent_expression_p assumes that nothing is
10288 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10289 ++processing_template_decl
;
10290 dependent_p
= value_dependent_expression_p (t
);
10291 --processing_template_decl
;
10295 /* Calculate the most general template of which R is a
10296 specialization, and the complete set of arguments used to
10298 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
10299 argvec
= tsubst_template_args (DECL_TI_ARGS
10300 (DECL_TEMPLATE_RESULT
10301 (DECL_TI_TEMPLATE (t
))),
10302 args
, complain
, in_decl
);
10303 if (argvec
== error_mark_node
)
10304 RETURN (error_mark_node
);
10306 /* Check to see if we already have this specialization. */
10307 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10308 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10316 /* We can see more levels of arguments than parameters if
10317 there was a specialization of a member template, like
10320 template <class T> struct S { template <class U> void f(); }
10321 template <> template <class U> void S<int>::f(U);
10323 Here, we'll be substituting into the specialization,
10324 because that's where we can find the code we actually
10325 want to generate, but we'll have enough arguments for
10326 the most general template.
10328 We also deal with the peculiar case:
10330 template <class T> struct S {
10331 template <class U> friend void f();
10333 template <class U> void f() {}
10335 template void f<double>();
10337 Here, the ARGS for the instantiation of will be {int,
10338 double}. But, we only need as many ARGS as there are
10339 levels of template parameters in CODE_PATTERN. We are
10340 careful not to get fooled into reducing the ARGS in
10343 template <class T> struct S { template <class U> void f(U); }
10344 template <class T> template <> void S<T>::f(int) {}
10346 which we can spot because the pattern will be a
10347 specialization in this case. */
10348 args_depth
= TMPL_ARGS_DEPTH (args
);
10350 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
10351 if (args_depth
> parms_depth
10352 && !DECL_TEMPLATE_SPECIALIZATION (t
))
10353 args
= get_innermost_template_args (args
, parms_depth
);
10357 /* This special case arises when we have something like this:
10359 template <class T> struct S {
10360 friend void f<int>(int, double);
10363 Here, the DECL_TI_TEMPLATE for the friend declaration
10364 will be an IDENTIFIER_NODE. We are being called from
10365 tsubst_friend_function, and we want only to create a
10366 new decl (R) with appropriate types so that we can call
10367 determine_specialization. */
10368 gen_tmpl
= NULL_TREE
;
10371 if (DECL_CLASS_SCOPE_P (t
))
10373 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
10377 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
10378 complain
, t
, /*entering_scope=*/1);
10383 ctx
= DECL_CONTEXT (t
);
10385 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10386 if (type
== error_mark_node
)
10387 RETURN (error_mark_node
);
10389 /* If we hit excessive deduction depth, the type is bogus even if
10390 it isn't error_mark_node, so don't build a decl. */
10391 if (excessive_deduction_depth
)
10392 RETURN (error_mark_node
);
10394 /* We do NOT check for matching decls pushed separately at this
10395 point, as they may not represent instantiations of this
10396 template, and in any case are considered separate under the
10399 DECL_USE_TEMPLATE (r
) = 0;
10400 TREE_TYPE (r
) = type
;
10401 /* Clear out the mangled name and RTL for the instantiation. */
10402 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10403 SET_DECL_RTL (r
, NULL
);
10404 /* Leave DECL_INITIAL set on deleted instantiations. */
10405 if (!DECL_DELETED_FN (r
))
10406 DECL_INITIAL (r
) = NULL_TREE
;
10407 DECL_CONTEXT (r
) = ctx
;
10409 /* OpenMP UDRs have the only argument a reference to the declared
10410 type. We want to diagnose if the declared type is a reference,
10411 which is invalid, but as references to references are usually
10412 quietly merged, diagnose it here. */
10413 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
10416 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
10417 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
10418 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
10419 error_at (DECL_SOURCE_LOCATION (t
),
10420 "reference type %qT in "
10421 "%<#pragma omp declare reduction%>", argtype
);
10422 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
10423 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
10427 if (member
&& DECL_CONV_FN_P (r
))
10428 /* Type-conversion operator. Reconstruct the name, in
10429 case it's the name of one of the template's parameters. */
10430 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
10432 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
10434 DECL_RESULT (r
) = NULL_TREE
;
10436 TREE_STATIC (r
) = 0;
10437 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
10438 DECL_EXTERNAL (r
) = 1;
10439 /* If this is an instantiation of a function with internal
10440 linkage, we already know what object file linkage will be
10441 assigned to the instantiation. */
10442 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
10443 DECL_DEFER_OUTPUT (r
) = 0;
10444 DECL_CHAIN (r
) = NULL_TREE
;
10445 DECL_PENDING_INLINE_INFO (r
) = 0;
10446 DECL_PENDING_INLINE_P (r
) = 0;
10447 DECL_SAVED_TREE (r
) = NULL_TREE
;
10448 DECL_STRUCT_FUNCTION (r
) = NULL
;
10450 /* We'll re-clone as appropriate in instantiate_template. */
10451 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
10453 /* If we aren't complaining now, return on error before we register
10454 the specialization so that we'll complain eventually. */
10455 if ((complain
& tf_error
) == 0
10456 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10457 && !grok_op_properties (r
, /*complain=*/false))
10458 RETURN (error_mark_node
);
10460 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10461 this in the special friend case mentioned above where
10462 GEN_TMPL is NULL. */
10465 DECL_TEMPLATE_INFO (r
)
10466 = build_template_info (gen_tmpl
, argvec
);
10467 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10470 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10472 /* We instantiated this while substituting into
10473 the type earlier (template/friend54.C). */
10476 /* We're not supposed to instantiate default arguments
10477 until they are called, for a template. But, for a
10480 template <class T> void f ()
10481 { extern void g(int i = T()); }
10483 we should do the substitution when the template is
10484 instantiated. We handle the member function case in
10485 instantiate_class_template since the default arguments
10486 might refer to other members of the class. */
10488 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
10489 && !uses_template_parms (argvec
))
10490 tsubst_default_arguments (r
, complain
);
10493 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
10495 /* Copy the list of befriending classes. */
10496 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
10498 friends
= &TREE_CHAIN (*friends
))
10500 *friends
= copy_node (*friends
);
10501 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
10506 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
10508 maybe_retrofit_in_chrg (r
);
10509 if (DECL_CONSTRUCTOR_P (r
))
10510 grok_ctor_properties (ctx
, r
);
10511 if (DECL_INHERITED_CTOR_BASE (r
))
10512 deduce_inheriting_ctor (r
);
10513 /* If this is an instantiation of a member template, clone it.
10514 If it isn't, that'll be handled by
10515 clone_constructors_and_destructors. */
10516 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
10517 clone_function_decl (r
, /*update_method_vec_p=*/0);
10519 else if ((complain
& tf_error
) != 0
10520 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10521 && !grok_op_properties (r
, /*complain=*/true))
10522 RETURN (error_mark_node
);
10524 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
10525 SET_DECL_FRIEND_CONTEXT (r
,
10526 tsubst (DECL_FRIEND_CONTEXT (t
),
10527 args
, complain
, in_decl
));
10529 /* Possibly limit visibility based on template args. */
10530 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10531 if (DECL_VISIBILITY_SPECIFIED (t
))
10533 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10534 DECL_ATTRIBUTES (r
)
10535 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10537 determine_visibility (r
);
10538 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
10539 && !processing_template_decl
)
10540 defaulted_late_check (r
);
10542 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10543 args
, complain
, in_decl
);
10549 tree type
= NULL_TREE
;
10551 tree expanded_types
= NULL_TREE
;
10552 tree prev_r
= NULL_TREE
;
10553 tree first_r
= NULL_TREE
;
10555 if (DECL_PACK_P (t
))
10557 /* If there is a local specialization that isn't a
10558 parameter pack, it means that we're doing a "simple"
10559 substitution from inside tsubst_pack_expansion. Just
10560 return the local specialization (which will be a single
10562 tree spec
= retrieve_local_specialization (t
);
10564 && TREE_CODE (spec
) == PARM_DECL
10565 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
10568 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10569 the parameters in this function parameter pack. */
10570 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10571 complain
, in_decl
);
10572 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10574 len
= TREE_VEC_LENGTH (expanded_types
);
10576 /* Zero-length parameter packs are boring. Just substitute
10579 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
10584 /* All we did was update the type. Make a note of that. */
10585 type
= expanded_types
;
10586 expanded_types
= NULL_TREE
;
10590 /* Loop through all of the parameters we'll build. When T is
10591 a function parameter pack, LEN is the number of expanded
10592 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10594 for (i
= 0; i
< len
; ++i
)
10598 if (DECL_TEMPLATE_PARM_P (t
))
10599 SET_DECL_TEMPLATE_PARM_P (r
);
10601 if (expanded_types
)
10602 /* We're on the Ith parameter of the function parameter
10605 /* Get the Ith type. */
10606 type
= TREE_VEC_ELT (expanded_types
, i
);
10608 /* Rename the parameter to include the index. */
10610 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10613 /* We're dealing with a normal parameter. */
10614 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10616 type
= type_decays_to (type
);
10617 TREE_TYPE (r
) = type
;
10618 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10620 if (DECL_INITIAL (r
))
10622 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
10623 DECL_INITIAL (r
) = TREE_TYPE (r
);
10625 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
10626 complain
, in_decl
);
10629 DECL_CONTEXT (r
) = NULL_TREE
;
10631 if (!DECL_TEMPLATE_PARM_P (r
))
10632 DECL_ARG_TYPE (r
) = type_passed_as (type
);
10634 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10635 args
, complain
, in_decl
);
10637 /* Keep track of the first new parameter we
10638 generate. That's what will be returned to the
10643 /* Build a proper chain of parameters when substituting
10644 into a function parameter pack. */
10646 DECL_CHAIN (prev_r
) = r
;
10649 /* If cp_unevaluated_operand is set, we're just looking for a
10650 single dummy parameter, so don't keep going. */
10651 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
10652 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
10653 complain
, DECL_CHAIN (t
));
10655 /* FIRST_R contains the start of the chain we've built. */
10662 tree type
= NULL_TREE
;
10663 tree vec
= NULL_TREE
;
10664 tree expanded_types
= NULL_TREE
;
10667 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
10669 /* This field is a lambda capture pack. Return a TREE_VEC of
10670 the expanded fields to instantiate_class_template_1 and
10671 store them in the specializations hash table as a
10672 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
10673 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10674 complain
, in_decl
);
10675 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10677 len
= TREE_VEC_LENGTH (expanded_types
);
10678 vec
= make_tree_vec (len
);
10682 /* All we did was update the type. Make a note of that. */
10683 type
= expanded_types
;
10684 expanded_types
= NULL_TREE
;
10688 for (int i
= 0; i
< len
; ++i
)
10691 if (expanded_types
)
10693 type
= TREE_VEC_ELT (expanded_types
, i
);
10695 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10698 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10700 if (type
== error_mark_node
)
10701 RETURN (error_mark_node
);
10702 TREE_TYPE (r
) = type
;
10703 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10705 if (DECL_C_BIT_FIELD (r
))
10706 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10707 non-bit-fields DECL_INITIAL is a non-static data member
10708 initializer, which gets deferred instantiation. */
10710 = tsubst_expr (DECL_INITIAL (t
), args
,
10712 /*integral_constant_expression_p=*/true);
10713 else if (DECL_INITIAL (t
))
10715 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10716 NSDMI in perform_member_init. Still set DECL_INITIAL
10717 so that we know there is one. */
10718 DECL_INITIAL (r
) = void_zero_node
;
10719 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
10720 retrofit_lang_decl (r
);
10721 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10723 /* We don't have to set DECL_CONTEXT here; it is set by
10724 finish_member_declaration. */
10725 DECL_CHAIN (r
) = NULL_TREE
;
10727 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10728 args
, complain
, in_decl
);
10731 TREE_VEC_ELT (vec
, i
) = r
;
10737 tree pack
= make_node (NONTYPE_ARGUMENT_PACK
);
10738 tree tpack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
10739 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
10740 SET_ARGUMENT_PACK_ARGS (tpack
, expanded_types
);
10741 TREE_TYPE (pack
) = tpack
;
10742 register_specialization (pack
, t
, args
, false, 0);
10748 /* We reach here only for member using decls. We also need to check
10749 uses_template_parms because DECL_DEPENDENT_P is not set for a
10750 using-declaration that designates a member of the current
10751 instantiation (c++/53549). */
10752 if (DECL_DEPENDENT_P (t
)
10753 || uses_template_parms (USING_DECL_SCOPE (t
)))
10755 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
10756 complain
, in_decl
);
10757 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
10758 r
= do_class_using_decl (inst_scope
, name
);
10760 r
= error_mark_node
;
10763 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
10764 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
10770 DECL_CHAIN (r
) = NULL_TREE
;
10777 tree argvec
= NULL_TREE
;
10778 tree gen_tmpl
= NULL_TREE
;
10780 tree tmpl
= NULL_TREE
;
10782 tree type
= NULL_TREE
;
10785 if (TREE_CODE (t
) == TYPE_DECL
10786 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
10788 /* If this is the canonical decl, we don't have to
10789 mess with instantiations, and often we can't (for
10790 typename, template type parms and such). Note that
10791 TYPE_NAME is not correct for the above test if
10792 we've copied the type for a typedef. */
10793 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10794 if (type
== error_mark_node
)
10795 RETURN (error_mark_node
);
10796 r
= TYPE_NAME (type
);
10800 /* Check to see if we already have the specialization we
10803 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
10805 /* T is a static data member or namespace-scope entity.
10806 We have to substitute into namespace-scope variables
10807 (even though such entities are never templates) because
10810 template <class T> void f() { extern T t; }
10812 where the entity referenced is not known until
10813 instantiation time. */
10815 ctx
= DECL_CONTEXT (t
);
10816 if (DECL_CLASS_SCOPE_P (t
))
10818 ctx
= tsubst_aggr_type (ctx
, args
,
10820 in_decl
, /*entering_scope=*/1);
10821 /* If CTX is unchanged, then T is in fact the
10822 specialization we want. That situation occurs when
10823 referencing a static data member within in its own
10824 class. We can use pointer equality, rather than
10825 same_type_p, because DECL_CONTEXT is always
10827 if (ctx
== DECL_CONTEXT (t
)
10828 && (TREE_CODE (t
) != TYPE_DECL
10829 /* ... unless T is a member template; in which
10830 case our caller can be willing to create a
10831 specialization of that template represented
10833 || !(DECL_TI_TEMPLATE (t
)
10834 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))))
10840 tmpl
= DECL_TI_TEMPLATE (t
);
10841 gen_tmpl
= most_general_template (tmpl
);
10842 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
10843 if (argvec
== error_mark_node
)
10844 RETURN (error_mark_node
);
10845 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10846 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10851 /* A local variable. */
10853 /* Subsequent calls to pushdecl will fill this in. */
10855 spec
= retrieve_local_specialization (t
);
10857 /* If we already have the specialization we need, there is
10858 nothing more to do. */
10865 /* Create a new node for the specialization we need. */
10867 if (type
== NULL_TREE
)
10869 if (is_typedef_decl (t
))
10870 type
= DECL_ORIGINAL_TYPE (t
);
10872 type
= TREE_TYPE (t
);
10874 && VAR_HAD_UNKNOWN_BOUND (t
)
10875 && type
!= error_mark_node
)
10876 type
= strip_array_domain (type
);
10877 type
= tsubst (type
, args
, complain
, in_decl
);
10881 /* Even if the original location is out of scope, the
10882 newly substituted one is not. */
10883 DECL_DEAD_FOR_LOCAL (r
) = 0;
10884 DECL_INITIALIZED_P (r
) = 0;
10885 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
10886 if (type
== error_mark_node
)
10887 RETURN (error_mark_node
);
10888 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10890 /* It may seem that this case cannot occur, since:
10895 declares a function, not a variable. However:
10898 template <typename T> void g() { T t; }
10899 template void g<f>();
10901 is an attempt to declare a variable with function
10903 error ("variable %qD has function type",
10904 /* R is not yet sufficiently initialized, so we
10905 just use its name. */
10907 RETURN (error_mark_node
);
10909 type
= complete_type (type
);
10910 /* Wait until cp_finish_decl to set this again, to handle
10911 circular dependency (template/instantiate6.C). */
10912 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
10913 type
= check_var_type (DECL_NAME (r
), type
);
10915 if (DECL_HAS_VALUE_EXPR_P (t
))
10917 tree ve
= DECL_VALUE_EXPR (t
);
10918 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
10919 /*constant_expression_p=*/false);
10920 if (REFERENCE_REF_P (ve
))
10922 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
10923 ve
= TREE_OPERAND (ve
, 0);
10925 SET_DECL_VALUE_EXPR (r
, ve
);
10928 else if (DECL_SELF_REFERENCE_P (t
))
10929 SET_DECL_SELF_REFERENCE_P (r
);
10930 TREE_TYPE (r
) = type
;
10931 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10932 DECL_CONTEXT (r
) = ctx
;
10933 /* Clear out the mangled name and RTL for the instantiation. */
10934 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10935 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10936 SET_DECL_RTL (r
, NULL
);
10937 /* The initializer must not be expanded until it is required;
10938 see [temp.inst]. */
10939 DECL_INITIAL (r
) = NULL_TREE
;
10940 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10941 SET_DECL_RTL (r
, NULL
);
10942 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
10945 /* Possibly limit visibility based on template args. */
10946 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10947 if (DECL_VISIBILITY_SPECIFIED (t
))
10949 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10950 DECL_ATTRIBUTES (r
)
10951 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10953 determine_visibility (r
);
10958 /* A static data member declaration is always marked
10959 external when it is declared in-class, even if an
10960 initializer is present. We mimic the non-template
10961 processing here. */
10962 DECL_EXTERNAL (r
) = 1;
10964 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10965 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
10966 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10968 else if (cp_unevaluated_operand
)
10969 gcc_unreachable ();
10971 register_local_specialization (r
, t
);
10973 DECL_CHAIN (r
) = NULL_TREE
;
10975 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
10977 args
, complain
, in_decl
);
10979 /* Preserve a typedef that names a type. */
10980 if (is_typedef_decl (r
))
10982 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
10983 set_underlying_type (r
);
10986 layout_decl (r
, 0);
10991 gcc_unreachable ();
10996 /* Restore the file and line information. */
10997 input_location
= saved_loc
;
11002 /* Substitute into the ARG_TYPES of a function type.
11003 If END is a TREE_CHAIN, leave it and any following types
11007 tsubst_arg_types (tree arg_types
,
11010 tsubst_flags_t complain
,
11013 tree remaining_arg_types
;
11014 tree type
= NULL_TREE
;
11016 tree expanded_args
= NULL_TREE
;
11019 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
11022 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
11023 args
, end
, complain
, in_decl
);
11024 if (remaining_arg_types
== error_mark_node
)
11025 return error_mark_node
;
11027 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
11029 /* For a pack expansion, perform substitution on the
11030 entire expression. Later on, we'll handle the arguments
11032 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
11033 args
, complain
, in_decl
);
11035 if (TREE_CODE (expanded_args
) == TREE_VEC
)
11036 /* So that we'll spin through the parameters, one by one. */
11037 i
= TREE_VEC_LENGTH (expanded_args
);
11040 /* We only partially substituted into the parameter
11041 pack. Our type is TYPE_PACK_EXPANSION. */
11042 type
= expanded_args
;
11043 expanded_args
= NULL_TREE
;
11051 type
= TREE_VEC_ELT (expanded_args
, i
);
11053 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
11055 if (type
== error_mark_node
)
11056 return error_mark_node
;
11057 if (VOID_TYPE_P (type
))
11059 if (complain
& tf_error
)
11061 error ("invalid parameter type %qT", type
);
11063 error ("in declaration %q+D", in_decl
);
11065 return error_mark_node
;
11068 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
11069 return error_mark_node
;
11071 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11072 top-level qualifiers as required. */
11073 type
= cv_unqualified (type_decays_to (type
));
11075 /* We do not substitute into default arguments here. The standard
11076 mandates that they be instantiated only when needed, which is
11077 done in build_over_call. */
11078 default_arg
= TREE_PURPOSE (arg_types
);
11080 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
11082 /* We've instantiated a template before its default arguments
11083 have been parsed. This can happen for a nested template
11084 class, and is not an error unless we require the default
11085 argument in a call of this function. */
11086 remaining_arg_types
=
11087 tree_cons (default_arg
, type
, remaining_arg_types
);
11088 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
11091 remaining_arg_types
=
11092 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
11095 return remaining_arg_types
;
11098 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11099 *not* handle the exception-specification for FNTYPE, because the
11100 initial substitution of explicitly provided template parameters
11101 during argument deduction forbids substitution into the
11102 exception-specification:
11106 All references in the function type of the function template to the
11107 corresponding template parameters are replaced by the specified tem-
11108 plate argument values. If a substitution in a template parameter or
11109 in the function type of the function template results in an invalid
11110 type, type deduction fails. [Note: The equivalent substitution in
11111 exception specifications is done only when the function is instanti-
11112 ated, at which point a program is ill-formed if the substitution
11113 results in an invalid type.] */
11116 tsubst_function_type (tree t
,
11118 tsubst_flags_t complain
,
11125 /* The TYPE_CONTEXT is not used for function/method types. */
11126 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
11128 /* Substitute the return type. */
11129 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11130 if (return_type
== error_mark_node
)
11131 return error_mark_node
;
11132 /* DR 486 clarifies that creation of a function type with an
11133 invalid return type is a deduction failure. */
11134 if (TREE_CODE (return_type
) == ARRAY_TYPE
11135 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
11137 if (complain
& tf_error
)
11139 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
11140 error ("function returning an array");
11142 error ("function returning a function");
11144 return error_mark_node
;
11147 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
11148 return error_mark_node
;
11150 /* Substitute the argument types. */
11151 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
11152 complain
, in_decl
);
11153 if (arg_types
== error_mark_node
)
11154 return error_mark_node
;
11156 /* Construct a new type node and return it. */
11157 if (TREE_CODE (t
) == FUNCTION_TYPE
)
11159 fntype
= build_function_type (return_type
, arg_types
);
11160 fntype
= apply_memfn_quals (fntype
,
11161 type_memfn_quals (t
),
11162 type_memfn_rqual (t
));
11166 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
11167 if (! MAYBE_CLASS_TYPE_P (r
))
11171 Type deduction may fail for any of the following
11174 -- Attempting to create "pointer to member of T" when T
11175 is not a class type. */
11176 if (complain
& tf_error
)
11177 error ("creating pointer to member function of non-class type %qT",
11179 return error_mark_node
;
11182 fntype
= build_method_type_directly (r
, return_type
,
11183 TREE_CHAIN (arg_types
));
11184 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
11186 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
11191 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11192 ARGS into that specification, and return the substituted
11193 specification. If there is no specification, return NULL_TREE. */
11196 tsubst_exception_specification (tree fntype
,
11198 tsubst_flags_t complain
,
11205 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
11206 new_specs
= NULL_TREE
;
11207 if (specs
&& TREE_PURPOSE (specs
))
11209 /* A noexcept-specifier. */
11210 tree expr
= TREE_PURPOSE (specs
);
11211 if (TREE_CODE (expr
) == INTEGER_CST
)
11215 /* Defer instantiation of noexcept-specifiers to avoid
11216 excessive instantiations (c++/49107). */
11217 new_specs
= make_node (DEFERRED_NOEXCEPT
);
11218 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
11220 /* We already partially instantiated this member template,
11221 so combine the new args with the old. */
11222 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
11223 = DEFERRED_NOEXCEPT_PATTERN (expr
);
11224 DEFERRED_NOEXCEPT_ARGS (new_specs
)
11225 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
11229 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
11230 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
11234 new_specs
= tsubst_copy_and_build
11235 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
11236 /*integral_constant_expression_p=*/true);
11237 new_specs
= build_noexcept_spec (new_specs
, complain
);
11241 if (! TREE_VALUE (specs
))
11248 tree expanded_specs
= NULL_TREE
;
11250 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
11252 /* Expand the pack expansion type. */
11253 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
11257 if (expanded_specs
== error_mark_node
)
11258 return error_mark_node
;
11259 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
11260 len
= TREE_VEC_LENGTH (expanded_specs
);
11263 /* We're substituting into a member template, so
11264 we got a TYPE_PACK_EXPANSION back. Add that
11265 expansion and move on. */
11266 gcc_assert (TREE_CODE (expanded_specs
)
11267 == TYPE_PACK_EXPANSION
);
11268 new_specs
= add_exception_specifier (new_specs
,
11271 specs
= TREE_CHAIN (specs
);
11276 for (i
= 0; i
< len
; ++i
)
11278 if (expanded_specs
)
11279 spec
= TREE_VEC_ELT (expanded_specs
, i
);
11281 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
11282 if (spec
== error_mark_node
)
11284 new_specs
= add_exception_specifier (new_specs
, spec
,
11288 specs
= TREE_CHAIN (specs
);
11294 /* Take the tree structure T and replace template parameters used
11295 therein with the argument vector ARGS. IN_DECL is an associated
11296 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11297 Issue error and warning messages under control of COMPLAIN. Note
11298 that we must be relatively non-tolerant of extensions here, in
11299 order to preserve conformance; if we allow substitutions that
11300 should not be allowed, we may allow argument deductions that should
11301 not succeed, and therefore report ambiguous overload situations
11302 where there are none. In theory, we could allow the substitution,
11303 but indicate that it should have failed, and allow our caller to
11304 make sure that the right thing happens, but we don't try to do this
11307 This function is used for dealing with types, decls and the like;
11308 for expressions, use tsubst_expr or tsubst_copy. */
11311 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11313 enum tree_code code
;
11314 tree type
, r
= NULL_TREE
;
11316 if (t
== NULL_TREE
|| t
== error_mark_node
11317 || t
== integer_type_node
11318 || t
== void_type_node
11319 || t
== char_type_node
11320 || t
== unknown_type_node
11321 || TREE_CODE (t
) == NAMESPACE_DECL
11322 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
11326 return tsubst_decl (t
, args
, complain
);
11328 if (args
== NULL_TREE
)
11331 code
= TREE_CODE (t
);
11333 if (code
== IDENTIFIER_NODE
)
11334 type
= IDENTIFIER_TYPE_VALUE (t
);
11336 type
= TREE_TYPE (t
);
11338 gcc_assert (type
!= unknown_type_node
);
11340 /* Reuse typedefs. We need to do this to handle dependent attributes,
11341 such as attribute aligned. */
11343 && typedef_variant_p (t
))
11345 tree decl
= TYPE_NAME (t
);
11347 if (alias_template_specialization_p (t
))
11349 /* DECL represents an alias template and we want to
11351 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11352 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11353 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
11355 else if (DECL_CLASS_SCOPE_P (decl
)
11356 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11357 && uses_template_parms (DECL_CONTEXT (decl
)))
11359 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11360 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11361 r
= retrieve_specialization (tmpl
, gen_args
, 0);
11363 else if (DECL_FUNCTION_SCOPE_P (decl
)
11364 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11365 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
11366 r
= retrieve_local_specialization (decl
);
11368 /* The typedef is from a non-template context. */
11374 r
= cp_build_qualified_type_real
11375 (r
, cp_type_quals (t
) | cp_type_quals (r
),
11376 complain
| tf_ignore_bad_quals
);
11381 /* We don't have an instantiation yet, so drop the typedef. */
11382 int quals
= cp_type_quals (t
);
11383 t
= DECL_ORIGINAL_TYPE (decl
);
11384 t
= cp_build_qualified_type_real (t
, quals
,
11385 complain
| tf_ignore_bad_quals
);
11390 && code
!= TYPENAME_TYPE
11391 && code
!= TEMPLATE_TYPE_PARM
11392 && code
!= IDENTIFIER_NODE
11393 && code
!= FUNCTION_TYPE
11394 && code
!= METHOD_TYPE
)
11395 type
= tsubst (type
, args
, complain
, in_decl
);
11396 if (type
== error_mark_node
)
11397 return error_mark_node
;
11403 case ENUMERAL_TYPE
:
11404 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
11405 /*entering_scope=*/0);
11408 case IDENTIFIER_NODE
:
11419 if (t
== integer_type_node
)
11422 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
11423 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
11427 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
11429 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
11430 /*integral_constant_expression_p=*/false);
11432 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11434 if (TREE_CODE (max
) == NOP_EXPR
11435 && TREE_SIDE_EFFECTS (omax
)
11436 && !TREE_TYPE (max
))
11437 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
11439 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11440 with TREE_SIDE_EFFECTS that indicates this is not an integral
11441 constant expression. */
11442 if (processing_template_decl
11443 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
11445 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
11446 TREE_SIDE_EFFECTS (max
) = 1;
11449 return compute_array_index_type (NULL_TREE
, max
, complain
);
11452 case TEMPLATE_TYPE_PARM
:
11453 case TEMPLATE_TEMPLATE_PARM
:
11454 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11455 case TEMPLATE_PARM_INDEX
:
11460 tree arg
= NULL_TREE
;
11464 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
11465 template_parm_level_and_index (t
, &level
, &idx
);
11467 levels
= TMPL_ARGS_DEPTH (args
);
11468 if (level
<= levels
)
11470 arg
= TMPL_ARG (args
, level
, idx
);
11472 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
11474 /* See through ARGUMENT_PACK_SELECT arguments. */
11475 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
11476 /* If the selected argument is an expansion E, that most
11477 likely means we were called from
11478 gen_elem_of_pack_expansion_instantiation during the
11479 substituting of pack an argument pack (which Ith
11480 element is a pack expansion, where I is
11481 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11482 In this case, the Ith element resulting from this
11483 substituting is going to be a pack expansion, which
11484 pattern is the pattern of E. Let's return the
11486 gen_elem_of_pack_expansion_instantiation will
11487 build the resulting pack expansion from it. */
11488 if (PACK_EXPANSION_P (arg
))
11489 arg
= PACK_EXPANSION_PATTERN (arg
);
11493 if (arg
== error_mark_node
)
11494 return error_mark_node
;
11495 else if (arg
!= NULL_TREE
)
11497 if (ARGUMENT_PACK_P (arg
))
11498 /* If ARG is an argument pack, we don't actually want to
11499 perform a substitution here, because substitutions
11500 for argument packs are only done
11501 element-by-element. We can get to this point when
11502 substituting the type of a non-type template
11503 parameter pack, when that type actually contains
11504 template parameter packs from an outer template, e.g.,
11506 template<typename... Types> struct A {
11507 template<Types... Values> struct B { };
11511 if (code
== TEMPLATE_TYPE_PARM
)
11514 gcc_assert (TYPE_P (arg
));
11516 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
11518 return cp_build_qualified_type_real
11519 (arg
, quals
, complain
| tf_ignore_bad_quals
);
11521 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11523 /* We are processing a type constructed from a
11524 template template parameter. */
11525 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
11526 args
, complain
, in_decl
);
11527 if (argvec
== error_mark_node
)
11528 return error_mark_node
;
11530 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
11531 || TREE_CODE (arg
) == TEMPLATE_DECL
11532 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
11534 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
11535 /* Consider this code:
11537 template <template <class> class Template>
11539 template <class Arg> using Bind = Template<Arg>;
11542 template <template <class> class Template, class Arg>
11543 using Instantiate = Template<Arg>; //#0
11545 template <template <class> class Template,
11548 Instantiate<Internal<Template>::template Bind,
11551 When #1 is parsed, the
11552 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11553 parameter `Template' in #0 matches the
11554 UNBOUND_CLASS_TEMPLATE representing the argument
11555 `Internal<Template>::template Bind'; We then want
11556 to assemble the type `Bind<Argument>' that can't
11557 be fully created right now, because
11558 `Internal<Template>' not being complete, the Bind
11559 template cannot be looked up in that context. So
11560 we need to "store" `Bind<Argument>' for later
11561 when the context of Bind becomes complete. Let's
11562 store that in a TYPENAME_TYPE. */
11563 return make_typename_type (TYPE_CONTEXT (arg
),
11564 build_nt (TEMPLATE_ID_EXPR
,
11565 TYPE_IDENTIFIER (arg
),
11570 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11571 are resolving nested-types in the signature of a
11572 member function templates. Otherwise ARG is a
11573 TEMPLATE_DECL and is the real template to be
11575 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
11576 arg
= TYPE_NAME (arg
);
11578 r
= lookup_template_class (arg
,
11580 DECL_CONTEXT (arg
),
11581 /*entering_scope=*/0,
11583 return cp_build_qualified_type_real
11584 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
11587 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11588 return convert_from_reference (unshare_expr (arg
));
11592 /* This can happen during the attempted tsubst'ing in
11593 unify. This means that we don't yet have any information
11594 about the template parameter in question. */
11597 /* Early in template argument deduction substitution, we don't
11598 want to reduce the level of 'auto', or it will be confused
11599 with a normal template parm in subsequent deduction. */
11600 if (is_auto (t
) && (complain
& tf_partial
))
11603 /* If we get here, we must have been looking at a parm for a
11604 more deeply nested template. Make a new version of this
11605 template parameter, but with a lower level. */
11608 case TEMPLATE_TYPE_PARM
:
11609 case TEMPLATE_TEMPLATE_PARM
:
11610 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11611 if (cp_type_quals (t
))
11613 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
11614 r
= cp_build_qualified_type_real
11615 (r
, cp_type_quals (t
),
11616 complain
| (code
== TEMPLATE_TYPE_PARM
11617 ? tf_ignore_bad_quals
: 0));
11622 TEMPLATE_TYPE_PARM_INDEX (r
)
11623 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
11624 r
, levels
, args
, complain
);
11625 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
11626 TYPE_MAIN_VARIANT (r
) = r
;
11627 TYPE_POINTER_TO (r
) = NULL_TREE
;
11628 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
11630 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
11631 /* We have reduced the level of the template
11632 template parameter, but not the levels of its
11633 template parameters, so canonical_type_parameter
11634 will not be able to find the canonical template
11635 template parameter for this level. Thus, we
11636 require structural equality checking to compare
11637 TEMPLATE_TEMPLATE_PARMs. */
11638 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11639 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
11640 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11642 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
11644 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11646 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
11647 complain
, in_decl
);
11648 if (argvec
== error_mark_node
)
11649 return error_mark_node
;
11651 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
11652 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
11657 case TEMPLATE_PARM_INDEX
:
11658 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
11662 gcc_unreachable ();
11670 tree purpose
, value
, chain
;
11672 if (t
== void_list_node
)
11675 purpose
= TREE_PURPOSE (t
);
11678 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
11679 if (purpose
== error_mark_node
)
11680 return error_mark_node
;
11682 value
= TREE_VALUE (t
);
11685 value
= tsubst (value
, args
, complain
, in_decl
);
11686 if (value
== error_mark_node
)
11687 return error_mark_node
;
11689 chain
= TREE_CHAIN (t
);
11690 if (chain
&& chain
!= void_type_node
)
11692 chain
= tsubst (chain
, args
, complain
, in_decl
);
11693 if (chain
== error_mark_node
)
11694 return error_mark_node
;
11696 if (purpose
== TREE_PURPOSE (t
)
11697 && value
== TREE_VALUE (t
)
11698 && chain
== TREE_CHAIN (t
))
11700 return hash_tree_cons (purpose
, value
, chain
);
11704 /* We should never be tsubsting a binfo. */
11705 gcc_unreachable ();
11708 /* A vector of template arguments. */
11709 gcc_assert (!type
);
11710 return tsubst_template_args (t
, args
, complain
, in_decl
);
11713 case REFERENCE_TYPE
:
11715 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
11720 Type deduction may fail for any of the following
11723 -- Attempting to create a pointer to reference type.
11724 -- Attempting to create a reference to a reference type or
11725 a reference to void.
11727 Core issue 106 says that creating a reference to a reference
11728 during instantiation is no longer a cause for failure. We
11729 only enforce this check in strict C++98 mode. */
11730 if ((TREE_CODE (type
) == REFERENCE_TYPE
11731 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
11732 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
11734 static location_t last_loc
;
11736 /* We keep track of the last time we issued this error
11737 message to avoid spewing a ton of messages during a
11738 single bad template instantiation. */
11739 if (complain
& tf_error
11740 && last_loc
!= input_location
)
11742 if (VOID_TYPE_P (type
))
11743 error ("forming reference to void");
11744 else if (code
== POINTER_TYPE
)
11745 error ("forming pointer to reference type %qT", type
);
11747 error ("forming reference to reference type %qT", type
);
11748 last_loc
= input_location
;
11751 return error_mark_node
;
11753 else if (TREE_CODE (type
) == FUNCTION_TYPE
11754 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
11755 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
11757 if (complain
& tf_error
)
11759 if (code
== POINTER_TYPE
)
11760 error ("forming pointer to qualified function type %qT",
11763 error ("forming reference to qualified function type %qT",
11766 return error_mark_node
;
11768 else if (code
== POINTER_TYPE
)
11770 r
= build_pointer_type (type
);
11771 if (TREE_CODE (type
) == METHOD_TYPE
)
11772 r
= build_ptrmemfunc_type (r
);
11774 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
11775 /* In C++0x, during template argument substitution, when there is an
11776 attempt to create a reference to a reference type, reference
11777 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11779 "If a template-argument for a template-parameter T names a type
11780 that is a reference to a type A, an attempt to create the type
11781 'lvalue reference to cv T' creates the type 'lvalue reference to
11782 A,' while an attempt to create the type type rvalue reference to
11783 cv T' creates the type T"
11785 r
= cp_build_reference_type
11787 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
11789 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
11790 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11792 if (cxx_dialect
>= cxx1y
11793 && !(TREE_CODE (t
) == REFERENCE_TYPE
&& REFERENCE_VLA_OK (t
))
11794 && array_of_runtime_bound_p (type
))
11796 if (complain
& tf_warning_or_error
)
11798 (input_location
, OPT_Wvla
,
11799 code
== REFERENCE_TYPE
11800 ? G_("cannot declare reference to array of runtime bound")
11801 : G_("cannot declare pointer to array of runtime bound"));
11803 r
= error_mark_node
;
11806 if (r
!= error_mark_node
)
11807 /* Will this ever be needed for TYPE_..._TO values? */
11814 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
11815 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
11819 Type deduction may fail for any of the following
11822 -- Attempting to create "pointer to member of T" when T
11823 is not a class type. */
11824 if (complain
& tf_error
)
11825 error ("creating pointer to member of non-class type %qT", r
);
11826 return error_mark_node
;
11828 if (TREE_CODE (type
) == REFERENCE_TYPE
)
11830 if (complain
& tf_error
)
11831 error ("creating pointer to member reference type %qT", type
);
11832 return error_mark_node
;
11834 if (VOID_TYPE_P (type
))
11836 if (complain
& tf_error
)
11837 error ("creating pointer to member of type void");
11838 return error_mark_node
;
11840 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
11841 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11843 /* The type of the implicit object parameter gets its
11844 cv-qualifiers from the FUNCTION_TYPE. */
11847 = build_memfn_type (type
, r
, type_memfn_quals (type
),
11848 type_memfn_rqual (type
));
11849 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
11850 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
11854 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
11858 case FUNCTION_TYPE
:
11863 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
11864 if (fntype
== error_mark_node
)
11865 return error_mark_node
;
11867 /* Substitute the exception specification. */
11868 specs
= tsubst_exception_specification (t
, args
, complain
,
11869 in_decl
, /*defer_ok*/true);
11870 if (specs
== error_mark_node
)
11871 return error_mark_node
;
11873 fntype
= build_exception_variant (fntype
, specs
);
11878 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
11879 if (domain
== error_mark_node
)
11880 return error_mark_node
;
11882 /* As an optimization, we avoid regenerating the array type if
11883 it will obviously be the same as T. */
11884 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
11887 /* These checks should match the ones in grokdeclarator.
11891 The deduction may fail for any of the following reasons:
11893 -- Attempting to create an array with an element type that
11894 is void, a function type, or a reference type, or [DR337]
11895 an abstract class type. */
11896 if (VOID_TYPE_P (type
)
11897 || TREE_CODE (type
) == FUNCTION_TYPE
11898 || TREE_CODE (type
) == REFERENCE_TYPE
)
11900 if (complain
& tf_error
)
11901 error ("creating array of %qT", type
);
11902 return error_mark_node
;
11905 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
11906 return error_mark_node
;
11908 r
= build_cplus_array_type (type
, domain
);
11910 if (TYPE_USER_ALIGN (t
))
11912 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
11913 TYPE_USER_ALIGN (r
) = 1;
11919 case TYPENAME_TYPE
:
11921 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11922 in_decl
, /*entering_scope=*/1);
11923 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
11924 complain
, in_decl
);
11926 if (ctx
== error_mark_node
|| f
== error_mark_node
)
11927 return error_mark_node
;
11929 if (!MAYBE_CLASS_TYPE_P (ctx
))
11931 if (complain
& tf_error
)
11932 error ("%qT is not a class, struct, or union type", ctx
);
11933 return error_mark_node
;
11935 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
11937 /* Normally, make_typename_type does not require that the CTX
11938 have complete type in order to allow things like:
11940 template <class T> struct S { typename S<T>::X Y; };
11942 But, such constructs have already been resolved by this
11943 point, so here CTX really should have complete type, unless
11944 it's a partial instantiation. */
11945 ctx
= complete_type (ctx
);
11946 if (!COMPLETE_TYPE_P (ctx
))
11948 if (complain
& tf_error
)
11949 cxx_incomplete_type_error (NULL_TREE
, ctx
);
11950 return error_mark_node
;
11954 f
= make_typename_type (ctx
, f
, typename_type
,
11955 complain
| tf_keep_type_decl
);
11956 if (f
== error_mark_node
)
11958 if (TREE_CODE (f
) == TYPE_DECL
)
11960 complain
|= tf_ignore_bad_quals
;
11964 if (TREE_CODE (f
) != TYPENAME_TYPE
)
11966 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
11968 if (complain
& tf_error
)
11969 error ("%qT resolves to %qT, which is not an enumeration type",
11972 return error_mark_node
;
11974 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
11976 if (complain
& tf_error
)
11977 error ("%qT resolves to %qT, which is is not a class type",
11980 return error_mark_node
;
11984 return cp_build_qualified_type_real
11985 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
11988 case UNBOUND_CLASS_TEMPLATE
:
11990 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11991 in_decl
, /*entering_scope=*/1);
11992 tree name
= TYPE_IDENTIFIER (t
);
11993 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
11995 if (ctx
== error_mark_node
|| name
== error_mark_node
)
11996 return error_mark_node
;
11999 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
12000 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
12007 ++cp_unevaluated_operand
;
12008 ++c_inhibit_evaluation_warnings
;
12010 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
12012 /*integral_constant_expression_p=*/false);
12014 --cp_unevaluated_operand
;
12015 --c_inhibit_evaluation_warnings
;
12017 type
= finish_typeof (type
);
12018 return cp_build_qualified_type_real (type
,
12020 | cp_type_quals (type
),
12024 case DECLTYPE_TYPE
:
12028 ++cp_unevaluated_operand
;
12029 ++c_inhibit_evaluation_warnings
;
12031 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
12032 complain
|tf_decltype
, in_decl
,
12033 /*function_p*/false,
12034 /*integral_constant_expression*/false);
12036 --cp_unevaluated_operand
;
12037 --c_inhibit_evaluation_warnings
;
12039 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
12040 type
= lambda_capture_field_type (type
,
12041 DECLTYPE_FOR_INIT_CAPTURE (t
));
12042 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
12043 type
= lambda_proxy_type (type
);
12046 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
12047 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
12049 /* In a template ~id could be either a complement expression
12050 or an unqualified-id naming a destructor; if instantiating
12051 it produces an expression, it's not an id-expression or
12054 type
= finish_decltype_type (type
, id
, complain
);
12056 return cp_build_qualified_type_real (type
,
12058 | cp_type_quals (type
),
12062 case UNDERLYING_TYPE
:
12064 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
12065 complain
, in_decl
);
12066 return finish_underlying_type (type
);
12069 case TYPE_ARGUMENT_PACK
:
12070 case NONTYPE_ARGUMENT_PACK
:
12072 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
12074 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
12078 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
12080 /* For template nontype argument packs, also substitute into
12082 if (code
== NONTYPE_ARGUMENT_PACK
)
12083 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12101 /* We should use one of the expression tsubsts for these codes. */
12102 gcc_unreachable ();
12105 sorry ("use of %qs in template", get_tree_code_name (code
));
12106 return error_mark_node
;
12110 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12111 type of the expression on the left-hand side of the "." or "->"
12115 tsubst_baselink (tree baselink
, tree object_type
,
12116 tree args
, tsubst_flags_t complain
, tree in_decl
)
12119 tree qualifying_scope
;
12122 tree template_args
= 0;
12123 bool template_id_p
= false;
12124 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
12126 /* A baselink indicates a function from a base class. Both the
12127 BASELINK_ACCESS_BINFO and the base class referenced may
12128 indicate bases of the template class, rather than the
12129 instantiated class. In addition, lookups that were not
12130 ambiguous before may be ambiguous now. Therefore, we perform
12131 the lookup again. */
12132 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
12133 qualifying_scope
= tsubst (qualifying_scope
, args
,
12134 complain
, in_decl
);
12135 fns
= BASELINK_FUNCTIONS (baselink
);
12136 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
12137 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
12139 template_id_p
= true;
12140 template_args
= TREE_OPERAND (fns
, 1);
12141 fns
= TREE_OPERAND (fns
, 0);
12143 template_args
= tsubst_template_args (template_args
, args
,
12144 complain
, in_decl
);
12146 name
= DECL_NAME (get_first_fn (fns
));
12147 if (IDENTIFIER_TYPENAME_P (name
))
12148 name
= mangle_conv_op_name_for_type (optype
);
12149 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
12151 return error_mark_node
;
12153 /* If lookup found a single function, mark it as used at this
12154 point. (If it lookup found multiple functions the one selected
12155 later by overload resolution will be marked as used at that
12157 if (BASELINK_P (baselink
))
12158 fns
= BASELINK_FUNCTIONS (baselink
);
12159 if (!template_id_p
&& !really_overloaded_fn (fns
))
12160 mark_used (OVL_CURRENT (fns
));
12162 /* Add back the template arguments, if present. */
12163 if (BASELINK_P (baselink
) && template_id_p
)
12164 BASELINK_FUNCTIONS (baselink
)
12165 = build_nt (TEMPLATE_ID_EXPR
,
12166 BASELINK_FUNCTIONS (baselink
),
12168 /* Update the conversion operator type. */
12169 BASELINK_OPTYPE (baselink
) = optype
;
12172 object_type
= current_class_type
;
12175 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
12181 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12182 true if the qualified-id will be a postfix-expression in-and-of
12183 itself; false if more of the postfix-expression follows the
12184 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12188 tsubst_qualified_id (tree qualified_id
, tree args
,
12189 tsubst_flags_t complain
, tree in_decl
,
12190 bool done
, bool address_p
)
12196 tree template_args
;
12197 location_t loc
= UNKNOWN_LOCATION
;
12199 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
12201 /* Figure out what name to look up. */
12202 name
= TREE_OPERAND (qualified_id
, 1);
12203 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
12205 is_template
= true;
12206 loc
= EXPR_LOCATION (name
);
12207 template_args
= TREE_OPERAND (name
, 1);
12209 template_args
= tsubst_template_args (template_args
, args
,
12210 complain
, in_decl
);
12211 name
= TREE_OPERAND (name
, 0);
12215 is_template
= false;
12216 template_args
= NULL_TREE
;
12219 /* Substitute into the qualifying scope. When there are no ARGS, we
12220 are just trying to simplify a non-dependent expression. In that
12221 case the qualifying scope may be dependent, and, in any case,
12222 substituting will not help. */
12223 scope
= TREE_OPERAND (qualified_id
, 0);
12226 scope
= tsubst (scope
, args
, complain
, in_decl
);
12227 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
12232 if (dependent_scope_p (scope
))
12235 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
12236 return build_qualified_name (NULL_TREE
, scope
, expr
,
12237 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
12240 if (!BASELINK_P (name
) && !DECL_P (expr
))
12242 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
12244 /* A BIT_NOT_EXPR is used to represent a destructor. */
12245 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
12247 error ("qualifying type %qT does not match destructor name ~%qT",
12248 scope
, TREE_OPERAND (expr
, 0));
12249 expr
= error_mark_node
;
12252 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
12253 /*is_type_p=*/0, false);
12256 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
12257 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
12258 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
12260 if (complain
& tf_error
)
12262 error ("dependent-name %qE is parsed as a non-type, but "
12263 "instantiation yields a type", qualified_id
);
12264 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
12266 return error_mark_node
;
12272 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
12274 /* Remember that there was a reference to this entity. */
12278 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
12280 if (complain
& tf_error
)
12281 qualified_name_lookup_error (scope
,
12282 TREE_OPERAND (qualified_id
, 1),
12283 expr
, input_location
);
12284 return error_mark_node
;
12288 expr
= lookup_template_function (expr
, template_args
);
12290 if (expr
== error_mark_node
&& complain
& tf_error
)
12291 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
12292 expr
, input_location
);
12293 else if (TYPE_P (scope
))
12295 expr
= (adjust_result_of_qualified_name_lookup
12296 (expr
, scope
, current_nonlambda_class_type ()));
12297 expr
= (finish_qualified_id_expr
12298 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
12299 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
12300 /*template_arg_p=*/false, complain
));
12303 /* Expressions do not generally have reference type. */
12304 if (TREE_CODE (expr
) != SCOPE_REF
12305 /* However, if we're about to form a pointer-to-member, we just
12306 want the referenced member referenced. */
12307 && TREE_CODE (expr
) != OFFSET_REF
)
12308 expr
= convert_from_reference (expr
);
12313 /* Like tsubst, but deals with expressions. This function just replaces
12314 template parms; to finish processing the resultant expression, use
12315 tsubst_copy_and_build or tsubst_expr. */
12318 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12320 enum tree_code code
;
12323 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
12326 code
= TREE_CODE (t
);
12331 r
= retrieve_local_specialization (t
);
12333 if (r
== NULL_TREE
)
12335 /* We get here for a use of 'this' in an NSDMI. */
12336 if (DECL_NAME (t
) == this_identifier
12337 && at_function_scope_p ()
12338 && DECL_CONSTRUCTOR_P (current_function_decl
))
12339 return current_class_ptr
;
12341 /* This can happen for a parameter name used later in a function
12342 declaration (such as in a late-specified return type). Just
12343 make a dummy decl, since it's only used for its type. */
12344 gcc_assert (cp_unevaluated_operand
!= 0);
12345 r
= tsubst_decl (t
, args
, complain
);
12346 /* Give it the template pattern as its context; its true context
12347 hasn't been instantiated yet and this is good enough for
12349 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
12352 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12353 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12362 if (DECL_TEMPLATE_PARM_P (t
))
12363 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
12364 /* There is no need to substitute into namespace-scope
12366 if (DECL_NAMESPACE_SCOPE_P (t
))
12368 /* If ARGS is NULL, then T is known to be non-dependent. */
12369 if (args
== NULL_TREE
)
12370 return integral_constant_value (t
);
12372 /* Unfortunately, we cannot just call lookup_name here.
12375 template <int I> int f() {
12377 struct S { void g() { E e = a; } };
12380 When we instantiate f<7>::S::g(), say, lookup_name is not
12381 clever enough to find f<7>::a. */
12383 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12384 /*entering_scope=*/0);
12386 for (v
= TYPE_VALUES (enum_type
);
12388 v
= TREE_CHAIN (v
))
12389 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
12390 return TREE_VALUE (v
);
12392 /* We didn't find the name. That should never happen; if
12393 name-lookup found it during preliminary parsing, we
12394 should find it again here during instantiation. */
12395 gcc_unreachable ();
12400 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
12402 /* Check for a local specialization set up by
12403 tsubst_pack_expansion. */
12404 tree r
= retrieve_local_specialization (t
);
12407 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12408 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12412 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12413 tsubst_decl put in the hash table. */
12414 return retrieve_specialization (t
, args
, 0);
12417 if (DECL_CONTEXT (t
))
12421 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12422 /*entering_scope=*/1);
12423 if (ctx
!= DECL_CONTEXT (t
))
12425 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
12428 if (complain
& tf_error
)
12429 error ("using invalid field %qD", t
);
12430 return error_mark_node
;
12439 case FUNCTION_DECL
:
12440 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
12441 r
= tsubst (t
, args
, complain
, in_decl
);
12442 else if (local_variable_p (t
))
12444 r
= retrieve_local_specialization (t
);
12445 if (r
== NULL_TREE
)
12447 if (DECL_ANON_UNION_VAR_P (t
))
12449 /* Just use name lookup to find a member alias for an
12450 anonymous union, but then add it to the hash table. */
12451 r
= lookup_name (DECL_NAME (t
));
12452 gcc_assert (DECL_ANON_UNION_VAR_P (r
));
12453 register_local_specialization (r
, t
);
12457 gcc_assert (errorcount
|| sorrycount
);
12458 return error_mark_node
;
12467 case NAMESPACE_DECL
:
12471 /* An OVERLOAD will always be a non-dependent overload set; an
12472 overload set from function scope will just be represented with an
12473 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12474 gcc_assert (!uses_template_parms (t
));
12478 return tsubst_baselink (t
, current_nonlambda_class_type (),
12479 args
, complain
, in_decl
);
12481 case TEMPLATE_DECL
:
12482 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12483 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
12484 args
, complain
, in_decl
);
12485 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
12486 return tsubst (t
, args
, complain
, in_decl
);
12487 else if (DECL_CLASS_SCOPE_P (t
)
12488 && uses_template_parms (DECL_CONTEXT (t
)))
12490 /* Template template argument like the following example need
12493 template <template <class> class TT> struct C {};
12494 template <class T> struct D {
12495 template <class U> struct E {};
12500 We are processing the template argument `E' in #1 for
12501 the template instantiation #2. Originally, `E' is a
12502 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12503 have to substitute this with one having context `D<int>'. */
12505 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
12506 return lookup_field (context
, DECL_NAME(t
), 0, false);
12509 /* Ordinary template template argument. */
12513 case REINTERPRET_CAST_EXPR
:
12514 case CONST_CAST_EXPR
:
12515 case STATIC_CAST_EXPR
:
12516 case DYNAMIC_CAST_EXPR
:
12517 case IMPLICIT_CONV_EXPR
:
12521 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12522 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12525 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
12528 tree expanded
, op
= TREE_OPERAND (t
, 0);
12531 if (SIZEOF_EXPR_TYPE_P (t
))
12532 op
= TREE_TYPE (op
);
12534 ++cp_unevaluated_operand
;
12535 ++c_inhibit_evaluation_warnings
;
12536 /* We only want to compute the number of arguments. */
12537 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
12538 --cp_unevaluated_operand
;
12539 --c_inhibit_evaluation_warnings
;
12541 if (TREE_CODE (expanded
) == TREE_VEC
)
12542 len
= TREE_VEC_LENGTH (expanded
);
12544 if (expanded
== error_mark_node
)
12545 return error_mark_node
;
12546 else if (PACK_EXPANSION_P (expanded
)
12547 || (TREE_CODE (expanded
) == TREE_VEC
12549 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
12551 if (TREE_CODE (expanded
) == TREE_VEC
)
12552 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
12554 if (TYPE_P (expanded
))
12555 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
12556 complain
& tf_error
);
12558 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
12559 complain
& tf_error
);
12562 return build_int_cst (size_type_node
, len
);
12564 if (SIZEOF_EXPR_TYPE_P (t
))
12566 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
12567 args
, complain
, in_decl
);
12568 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
12569 r
= build1 (SIZEOF_EXPR
,
12570 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
12571 SIZEOF_EXPR_TYPE_P (r
) = 1;
12578 case TRUTH_NOT_EXPR
:
12581 case UNARY_PLUS_EXPR
: /* Unary + */
12583 case AT_ENCODE_EXPR
:
12587 case REALPART_EXPR
:
12588 case IMAGPART_EXPR
:
12591 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12592 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12594 case COMPONENT_REF
:
12599 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
12600 name
= TREE_OPERAND (t
, 1);
12601 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
12603 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12604 complain
, in_decl
);
12605 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12607 else if (TREE_CODE (name
) == SCOPE_REF
12608 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
12610 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12611 complain
, in_decl
);
12612 name
= TREE_OPERAND (name
, 1);
12613 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12614 complain
, in_decl
);
12615 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12616 name
= build_qualified_name (/*type=*/NULL_TREE
,
12618 /*template_p=*/false);
12620 else if (BASELINK_P (name
))
12621 name
= tsubst_baselink (name
,
12622 non_reference (TREE_TYPE (object
)),
12626 name
= tsubst_copy (name
, args
, complain
, in_decl
);
12627 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
12633 case TRUNC_DIV_EXPR
:
12634 case CEIL_DIV_EXPR
:
12635 case FLOOR_DIV_EXPR
:
12636 case ROUND_DIV_EXPR
:
12637 case EXACT_DIV_EXPR
:
12641 case TRUNC_MOD_EXPR
:
12642 case FLOOR_MOD_EXPR
:
12643 case TRUTH_ANDIF_EXPR
:
12644 case TRUTH_ORIF_EXPR
:
12645 case TRUTH_AND_EXPR
:
12646 case TRUTH_OR_EXPR
:
12659 case COMPOUND_EXPR
:
12662 case PREDECREMENT_EXPR
:
12663 case PREINCREMENT_EXPR
:
12664 case POSTDECREMENT_EXPR
:
12665 case POSTINCREMENT_EXPR
:
12667 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12668 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12671 return build_qualified_name (/*type=*/NULL_TREE
,
12672 tsubst_copy (TREE_OPERAND (t
, 0),
12673 args
, complain
, in_decl
),
12674 tsubst_copy (TREE_OPERAND (t
, 1),
12675 args
, complain
, in_decl
),
12676 QUALIFIED_NAME_IS_TEMPLATE (t
));
12681 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12682 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12683 NULL_TREE
, NULL_TREE
);
12687 int n
= VL_EXP_OPERAND_LENGTH (t
);
12688 tree result
= build_vl_exp (CALL_EXPR
, n
);
12690 for (i
= 0; i
< n
; i
++)
12691 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
12692 complain
, in_decl
);
12698 case PSEUDO_DTOR_EXPR
:
12699 case VEC_PERM_EXPR
:
12702 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12703 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12704 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12705 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12712 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12713 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12714 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12715 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
12722 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12723 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12724 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
12725 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
12729 case TEMPLATE_ID_EXPR
:
12731 /* Substituted template arguments */
12732 tree fn
= TREE_OPERAND (t
, 0);
12733 tree targs
= TREE_OPERAND (t
, 1);
12735 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
12737 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12739 return lookup_template_function (fn
, targs
);
12744 tree purpose
, value
, chain
;
12746 if (t
== void_list_node
)
12749 purpose
= TREE_PURPOSE (t
);
12751 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
12752 value
= TREE_VALUE (t
);
12754 value
= tsubst_copy (value
, args
, complain
, in_decl
);
12755 chain
= TREE_CHAIN (t
);
12756 if (chain
&& chain
!= void_type_node
)
12757 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
12758 if (purpose
== TREE_PURPOSE (t
)
12759 && value
== TREE_VALUE (t
)
12760 && chain
== TREE_CHAIN (t
))
12762 return tree_cons (purpose
, value
, chain
);
12767 case ENUMERAL_TYPE
:
12769 case TEMPLATE_TYPE_PARM
:
12770 case TEMPLATE_TEMPLATE_PARM
:
12771 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12772 case TEMPLATE_PARM_INDEX
:
12774 case REFERENCE_TYPE
:
12776 case FUNCTION_TYPE
:
12779 case TYPENAME_TYPE
:
12780 case UNBOUND_CLASS_TEMPLATE
:
12782 case DECLTYPE_TYPE
:
12784 return tsubst (t
, args
, complain
, in_decl
);
12788 /* Fall through. */
12789 case IDENTIFIER_NODE
:
12790 if (IDENTIFIER_TYPENAME_P (t
))
12792 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12793 return mangle_conv_op_name_for_type (new_type
);
12799 /* This is handled by tsubst_copy_and_build. */
12800 gcc_unreachable ();
12803 return build_x_va_arg (EXPR_LOCATION (t
),
12804 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
12806 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
12808 case CLEANUP_POINT_EXPR
:
12809 /* We shouldn't have built any of these during initial template
12810 generation. Instead, they should be built during instantiation
12811 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12812 gcc_unreachable ();
12816 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12817 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12818 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12819 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
12820 mark_used (TREE_OPERAND (r
, 1));
12823 case EXPR_PACK_EXPANSION
:
12824 error ("invalid use of pack expansion expression");
12825 return error_mark_node
;
12827 case NONTYPE_ARGUMENT_PACK
:
12828 error ("use %<...%> to expand argument pack");
12829 return error_mark_node
;
12836 /* Instantiate any typedefs in the type. */
12837 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12838 r
= fold_convert (type
, t
);
12839 gcc_assert (TREE_CODE (r
) == code
);
12844 /* These can sometimes show up in a partial instantiation, but never
12845 involve template parms. */
12846 gcc_assert (!uses_template_parms (t
));
12850 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12851 gcc_checking_assert (false);
12856 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12859 tsubst_omp_clauses (tree clauses
, bool declare_simd
,
12860 tree args
, tsubst_flags_t complain
, tree in_decl
)
12862 tree new_clauses
= NULL
, nc
, oc
;
12864 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
12866 nc
= copy_node (oc
);
12867 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
12870 switch (OMP_CLAUSE_CODE (nc
))
12872 case OMP_CLAUSE_LASTPRIVATE
:
12873 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
12875 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
12876 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
12877 in_decl
, /*integral_constant_expression_p=*/false);
12878 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
12879 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
12882 case OMP_CLAUSE_PRIVATE
:
12883 case OMP_CLAUSE_SHARED
:
12884 case OMP_CLAUSE_FIRSTPRIVATE
:
12885 case OMP_CLAUSE_COPYIN
:
12886 case OMP_CLAUSE_COPYPRIVATE
:
12887 case OMP_CLAUSE_IF
:
12888 case OMP_CLAUSE_NUM_THREADS
:
12889 case OMP_CLAUSE_SCHEDULE
:
12890 case OMP_CLAUSE_COLLAPSE
:
12891 case OMP_CLAUSE_FINAL
:
12892 case OMP_CLAUSE_DEPEND
:
12893 case OMP_CLAUSE_FROM
:
12894 case OMP_CLAUSE_TO
:
12895 case OMP_CLAUSE_UNIFORM
:
12896 case OMP_CLAUSE_MAP
:
12897 case OMP_CLAUSE_DEVICE
:
12898 case OMP_CLAUSE_DIST_SCHEDULE
:
12899 case OMP_CLAUSE_NUM_TEAMS
:
12900 case OMP_CLAUSE_THREAD_LIMIT
:
12901 case OMP_CLAUSE_SAFELEN
:
12902 case OMP_CLAUSE_SIMDLEN
:
12903 OMP_CLAUSE_OPERAND (nc
, 0)
12904 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12905 in_decl
, /*integral_constant_expression_p=*/false);
12907 case OMP_CLAUSE_REDUCTION
:
12908 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
12910 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
12911 if (TREE_CODE (placeholder
) == SCOPE_REF
)
12913 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
12914 complain
, in_decl
);
12915 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
12916 = build_qualified_name (NULL_TREE
, scope
,
12917 TREE_OPERAND (placeholder
, 1),
12921 gcc_assert (identifier_p (placeholder
));
12923 OMP_CLAUSE_OPERAND (nc
, 0)
12924 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12925 in_decl
, /*integral_constant_expression_p=*/false);
12927 case OMP_CLAUSE_LINEAR
:
12928 case OMP_CLAUSE_ALIGNED
:
12929 OMP_CLAUSE_OPERAND (nc
, 0)
12930 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12931 in_decl
, /*integral_constant_expression_p=*/false);
12932 OMP_CLAUSE_OPERAND (nc
, 1)
12933 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
12934 in_decl
, /*integral_constant_expression_p=*/false);
12937 case OMP_CLAUSE_NOWAIT
:
12938 case OMP_CLAUSE_ORDERED
:
12939 case OMP_CLAUSE_DEFAULT
:
12940 case OMP_CLAUSE_UNTIED
:
12941 case OMP_CLAUSE_MERGEABLE
:
12942 case OMP_CLAUSE_INBRANCH
:
12943 case OMP_CLAUSE_NOTINBRANCH
:
12944 case OMP_CLAUSE_PROC_BIND
:
12945 case OMP_CLAUSE_FOR
:
12946 case OMP_CLAUSE_PARALLEL
:
12947 case OMP_CLAUSE_SECTIONS
:
12948 case OMP_CLAUSE_TASKGROUP
:
12951 gcc_unreachable ();
12955 new_clauses
= nreverse (new_clauses
);
12957 new_clauses
= finish_omp_clauses (new_clauses
);
12958 return new_clauses
;
12961 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12964 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
12967 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12969 tree purpose
, value
, chain
;
12974 if (TREE_CODE (t
) != TREE_LIST
)
12975 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12976 /*function_p=*/false,
12977 /*integral_constant_expression_p=*/false);
12979 if (t
== void_list_node
)
12982 purpose
= TREE_PURPOSE (t
);
12984 purpose
= RECUR (purpose
);
12985 value
= TREE_VALUE (t
);
12988 if (TREE_CODE (value
) != LABEL_DECL
)
12989 value
= RECUR (value
);
12992 value
= lookup_label (DECL_NAME (value
));
12993 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
12994 TREE_USED (value
) = 1;
12997 chain
= TREE_CHAIN (t
);
12998 if (chain
&& chain
!= void_type_node
)
12999 chain
= RECUR (chain
);
13000 return tree_cons (purpose
, value
, chain
);
13004 /* Substitute one OMP_FOR iterator. */
13007 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
13008 tree condv
, tree incrv
, tree
*clauses
,
13009 tree args
, tsubst_flags_t complain
, tree in_decl
,
13010 bool integral_constant_expression_p
)
13012 #define RECUR(NODE) \
13013 tsubst_expr ((NODE), args, complain, in_decl, \
13014 integral_constant_expression_p)
13015 tree decl
, init
, cond
, incr
;
13018 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
13019 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
13020 decl
= TREE_OPERAND (init
, 0);
13021 init
= TREE_OPERAND (init
, 1);
13022 /* Do this before substituting into decl to handle 'auto'. */
13023 init_decl
= (init
&& TREE_CODE (init
) == DECL_EXPR
);
13024 init
= RECUR (init
);
13025 decl
= RECUR (decl
);
13028 gcc_assert (!processing_template_decl
);
13029 init
= DECL_INITIAL (decl
);
13030 DECL_INITIAL (decl
) = NULL_TREE
;
13033 gcc_assert (!type_dependent_expression_p (decl
));
13035 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
13037 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
13038 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
13039 if (TREE_CODE (incr
) == MODIFY_EXPR
)
13040 incr
= build_x_modify_expr (EXPR_LOCATION (incr
),
13041 RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
13042 RECUR (TREE_OPERAND (incr
, 1)),
13045 incr
= RECUR (incr
);
13046 TREE_VEC_ELT (declv
, i
) = decl
;
13047 TREE_VEC_ELT (initv
, i
) = init
;
13048 TREE_VEC_ELT (condv
, i
) = cond
;
13049 TREE_VEC_ELT (incrv
, i
) = incr
;
13053 if (init
&& !init_decl
)
13056 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
13058 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
13059 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
13060 && OMP_CLAUSE_DECL (c
) == decl
)
13062 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
13063 && OMP_CLAUSE_DECL (c
) == decl
)
13064 error ("iteration variable %qD should not be firstprivate", decl
);
13065 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
13066 && OMP_CLAUSE_DECL (c
) == decl
)
13067 error ("iteration variable %qD should not be reduction", decl
);
13071 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
13072 OMP_CLAUSE_DECL (c
) = decl
;
13073 c
= finish_omp_clauses (c
);
13076 OMP_CLAUSE_CHAIN (c
) = *clauses
;
13081 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
13082 if (COMPARISON_CLASS_P (cond
))
13083 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
13084 RECUR (TREE_OPERAND (cond
, 0)),
13085 RECUR (TREE_OPERAND (cond
, 1)));
13087 cond
= RECUR (cond
);
13088 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
13089 switch (TREE_CODE (incr
))
13091 case PREINCREMENT_EXPR
:
13092 case PREDECREMENT_EXPR
:
13093 case POSTINCREMENT_EXPR
:
13094 case POSTDECREMENT_EXPR
:
13095 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
13096 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
13099 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
13100 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
13102 tree rhs
= TREE_OPERAND (incr
, 1);
13103 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
13104 RECUR (TREE_OPERAND (incr
, 0)),
13105 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
13106 RECUR (TREE_OPERAND (rhs
, 0)),
13107 RECUR (TREE_OPERAND (rhs
, 1))));
13110 incr
= RECUR (incr
);
13113 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
13114 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
13116 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
13117 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
13118 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
13119 TREE_TYPE (decl
), lhs
,
13120 RECUR (TREE_OPERAND (incr
, 2))));
13122 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
13123 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
13124 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
13126 tree rhs
= TREE_OPERAND (incr
, 2);
13127 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
13128 RECUR (TREE_OPERAND (incr
, 0)),
13129 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
13130 RECUR (TREE_OPERAND (rhs
, 0)),
13131 RECUR (TREE_OPERAND (rhs
, 1))));
13134 incr
= RECUR (incr
);
13137 incr
= RECUR (incr
);
13141 TREE_VEC_ELT (declv
, i
) = decl
;
13142 TREE_VEC_ELT (initv
, i
) = init
;
13143 TREE_VEC_ELT (condv
, i
) = cond
;
13144 TREE_VEC_ELT (incrv
, i
) = incr
;
13148 /* Like tsubst_copy for expressions, etc. but also does semantic
13152 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
13153 bool integral_constant_expression_p
)
13155 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13156 #define RECUR(NODE) \
13157 tsubst_expr ((NODE), args, complain, in_decl, \
13158 integral_constant_expression_p)
13164 if (t
== NULL_TREE
|| t
== error_mark_node
)
13167 loc
= input_location
;
13168 if (EXPR_HAS_LOCATION (t
))
13169 input_location
= EXPR_LOCATION (t
);
13170 if (STATEMENT_CODE_P (TREE_CODE (t
)))
13171 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
13173 switch (TREE_CODE (t
))
13175 case STATEMENT_LIST
:
13177 tree_stmt_iterator i
;
13178 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
13179 RECUR (tsi_stmt (i
));
13183 case CTOR_INITIALIZER
:
13184 finish_mem_initializers (tsubst_initializer_list
13185 (TREE_OPERAND (t
, 0), args
));
13189 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
13193 tmp
= RECUR (EXPR_STMT_EXPR (t
));
13194 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
13195 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
13197 finish_expr_stmt (tmp
);
13201 do_using_directive (USING_STMT_NAMESPACE (t
));
13206 tree decl
, pattern_decl
;
13209 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
13210 if (TREE_CODE (decl
) == LABEL_DECL
)
13211 finish_label_decl (DECL_NAME (decl
));
13212 else if (TREE_CODE (decl
) == USING_DECL
)
13214 tree scope
= USING_DECL_SCOPE (decl
);
13215 tree name
= DECL_NAME (decl
);
13218 scope
= tsubst (scope
, args
, complain
, in_decl
);
13219 decl
= lookup_qualified_name (scope
, name
,
13220 /*is_type_p=*/false,
13221 /*complain=*/false);
13222 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
13223 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
13225 do_local_using_decl (decl
, scope
, name
);
13227 else if (DECL_PACK_P (decl
))
13229 /* Don't build up decls for a variadic capture proxy, we'll
13230 instantiate the elements directly as needed. */
13235 init
= DECL_INITIAL (decl
);
13236 decl
= tsubst (decl
, args
, complain
, in_decl
);
13237 if (decl
!= error_mark_node
)
13239 /* By marking the declaration as instantiated, we avoid
13240 trying to instantiate it. Since instantiate_decl can't
13241 handle local variables, and since we've already done
13242 all that needs to be done, that's the right thing to
13245 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13247 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
13248 /* Anonymous aggregates are a special case. */
13249 finish_anon_union (decl
);
13250 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
13252 DECL_CONTEXT (decl
) = current_function_decl
;
13253 if (DECL_NAME (decl
) == this_identifier
)
13255 tree lam
= DECL_CONTEXT (current_function_decl
);
13256 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
13257 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
13259 insert_capture_proxy (decl
);
13261 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
13262 /* We already did a pushtag. */;
13263 else if (TREE_CODE (decl
) == FUNCTION_DECL
13264 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
13265 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
13267 DECL_CONTEXT (decl
) = NULL_TREE
;
13269 DECL_CONTEXT (decl
) = current_function_decl
;
13270 cp_check_omp_declare_reduction (decl
);
13274 int const_init
= false;
13275 maybe_push_decl (decl
);
13277 && DECL_PRETTY_FUNCTION_P (decl
))
13279 /* For __PRETTY_FUNCTION__ we have to adjust the
13281 const char *const name
13282 = cxx_printable_name (current_function_decl
, 2);
13283 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
13287 tree t
= RECUR (init
);
13291 /* If we had an initializer but it
13292 instantiated to nothing,
13293 value-initialize the object. This will
13294 only occur when the initializer was a
13295 pack expansion where the parameter packs
13296 used in that expansion were of length
13298 init
= build_value_init (TREE_TYPE (decl
),
13300 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
13301 init
= get_target_expr_sfinae (init
, complain
);
13308 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13310 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
13319 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
13320 RECUR (FOR_INIT_STMT (t
));
13321 finish_for_init_stmt (stmt
);
13322 tmp
= RECUR (FOR_COND (t
));
13323 finish_for_cond (tmp
, stmt
, false);
13324 tmp
= RECUR (FOR_EXPR (t
));
13325 finish_for_expr (tmp
, stmt
);
13326 RECUR (FOR_BODY (t
));
13327 finish_for_stmt (stmt
);
13330 case RANGE_FOR_STMT
:
13333 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
13334 decl
= RANGE_FOR_DECL (t
);
13335 decl
= tsubst (decl
, args
, complain
, in_decl
);
13336 maybe_push_decl (decl
);
13337 expr
= RECUR (RANGE_FOR_EXPR (t
));
13338 stmt
= cp_convert_range_for (stmt
, decl
, expr
, RANGE_FOR_IVDEP (t
));
13339 RECUR (RANGE_FOR_BODY (t
));
13340 finish_for_stmt (stmt
);
13345 stmt
= begin_while_stmt ();
13346 tmp
= RECUR (WHILE_COND (t
));
13347 finish_while_stmt_cond (tmp
, stmt
, false);
13348 RECUR (WHILE_BODY (t
));
13349 finish_while_stmt (stmt
);
13353 stmt
= begin_do_stmt ();
13354 RECUR (DO_BODY (t
));
13355 finish_do_body (stmt
);
13356 tmp
= RECUR (DO_COND (t
));
13357 finish_do_stmt (tmp
, stmt
, false);
13361 stmt
= begin_if_stmt ();
13362 tmp
= RECUR (IF_COND (t
));
13363 finish_if_stmt_cond (tmp
, stmt
);
13364 RECUR (THEN_CLAUSE (t
));
13365 finish_then_clause (stmt
);
13367 if (ELSE_CLAUSE (t
))
13369 begin_else_clause (stmt
);
13370 RECUR (ELSE_CLAUSE (t
));
13371 finish_else_clause (stmt
);
13374 finish_if_stmt (stmt
);
13378 if (BIND_EXPR_BODY_BLOCK (t
))
13379 stmt
= begin_function_body ();
13381 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
13382 ? BCS_TRY_BLOCK
: 0);
13384 RECUR (BIND_EXPR_BODY (t
));
13386 if (BIND_EXPR_BODY_BLOCK (t
))
13387 finish_function_body (stmt
);
13389 finish_compound_stmt (stmt
);
13393 finish_break_stmt ();
13396 case CONTINUE_STMT
:
13397 finish_continue_stmt ();
13401 stmt
= begin_switch_stmt ();
13402 tmp
= RECUR (SWITCH_STMT_COND (t
));
13403 finish_switch_cond (tmp
, stmt
);
13404 RECUR (SWITCH_STMT_BODY (t
));
13405 finish_switch_stmt (stmt
);
13408 case CASE_LABEL_EXPR
:
13409 finish_case_label (EXPR_LOCATION (t
),
13410 RECUR (CASE_LOW (t
)),
13411 RECUR (CASE_HIGH (t
)));
13416 tree decl
= LABEL_EXPR_LABEL (t
);
13419 label
= finish_label_stmt (DECL_NAME (decl
));
13420 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
13421 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
13426 tmp
= GOTO_DESTINATION (t
);
13427 if (TREE_CODE (tmp
) != LABEL_DECL
)
13428 /* Computed goto's must be tsubst'd into. On the other hand,
13429 non-computed gotos must not be; the identifier in question
13430 will have no binding. */
13433 tmp
= DECL_NAME (tmp
);
13434 finish_goto_stmt (tmp
);
13438 tmp
= finish_asm_stmt
13439 (ASM_VOLATILE_P (t
),
13440 RECUR (ASM_STRING (t
)),
13441 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
13442 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
13443 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
13444 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
13446 tree asm_expr
= tmp
;
13447 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
13448 asm_expr
= TREE_OPERAND (asm_expr
, 0);
13449 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
13456 stmt
= begin_try_block ();
13457 RECUR (TRY_STMTS (t
));
13458 finish_cleanup_try_block (stmt
);
13459 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
13463 tree compound_stmt
= NULL_TREE
;
13465 if (FN_TRY_BLOCK_P (t
))
13466 stmt
= begin_function_try_block (&compound_stmt
);
13468 stmt
= begin_try_block ();
13470 RECUR (TRY_STMTS (t
));
13472 if (FN_TRY_BLOCK_P (t
))
13473 finish_function_try_block (stmt
);
13475 finish_try_block (stmt
);
13477 RECUR (TRY_HANDLERS (t
));
13478 if (FN_TRY_BLOCK_P (t
))
13479 finish_function_handler_sequence (stmt
, compound_stmt
);
13481 finish_handler_sequence (stmt
);
13487 tree decl
= HANDLER_PARMS (t
);
13491 decl
= tsubst (decl
, args
, complain
, in_decl
);
13492 /* Prevent instantiate_decl from trying to instantiate
13493 this variable. We've already done all that needs to be
13495 if (decl
!= error_mark_node
)
13496 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13498 stmt
= begin_handler ();
13499 finish_handler_parms (decl
, stmt
);
13500 RECUR (HANDLER_BODY (t
));
13501 finish_handler (stmt
);
13506 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
13507 if (CLASS_TYPE_P (tmp
))
13509 /* Local classes are not independent templates; they are
13510 instantiated along with their containing function. And this
13511 way we don't have to deal with pushing out of one local class
13512 to instantiate a member of another local class. */
13514 /* Closures are handled by the LAMBDA_EXPR. */
13515 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
13516 complete_type (tmp
);
13517 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
13518 if (!DECL_ARTIFICIAL (fn
))
13519 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
13523 case STATIC_ASSERT
:
13527 ++c_inhibit_evaluation_warnings
;
13529 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
13532 /*integral_constant_expression_p=*/true);
13533 --c_inhibit_evaluation_warnings
;
13535 finish_static_assert (condition
,
13536 STATIC_ASSERT_MESSAGE (t
),
13537 STATIC_ASSERT_SOURCE_LOCATION (t
),
13538 /*member_p=*/false);
13543 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), false,
13544 args
, complain
, in_decl
);
13545 stmt
= begin_omp_parallel ();
13546 RECUR (OMP_PARALLEL_BODY (t
));
13547 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
13548 = OMP_PARALLEL_COMBINED (t
);
13552 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), false,
13553 args
, complain
, in_decl
);
13554 stmt
= begin_omp_task ();
13555 RECUR (OMP_TASK_BODY (t
));
13556 finish_omp_task (tmp
, stmt
);
13561 case OMP_DISTRIBUTE
:
13563 tree clauses
, body
, pre_body
;
13564 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
13565 tree incrv
= NULL_TREE
;
13568 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), false,
13569 args
, complain
, in_decl
);
13570 if (OMP_FOR_INIT (t
) != NULL_TREE
)
13572 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13573 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13574 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13575 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13578 stmt
= begin_omp_structured_block ();
13580 pre_body
= push_stmt_list ();
13581 RECUR (OMP_FOR_PRE_BODY (t
));
13582 pre_body
= pop_stmt_list (pre_body
);
13584 if (OMP_FOR_INIT (t
) != NULL_TREE
)
13585 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
13586 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
13587 &clauses
, args
, complain
, in_decl
,
13588 integral_constant_expression_p
);
13590 body
= push_stmt_list ();
13591 RECUR (OMP_FOR_BODY (t
));
13592 body
= pop_stmt_list (body
);
13594 if (OMP_FOR_INIT (t
) != NULL_TREE
)
13595 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
, initv
,
13596 condv
, incrv
, body
, pre_body
, clauses
);
13599 t
= make_node (TREE_CODE (t
));
13600 TREE_TYPE (t
) = void_type_node
;
13601 OMP_FOR_BODY (t
) = body
;
13602 OMP_FOR_PRE_BODY (t
) = pre_body
;
13603 OMP_FOR_CLAUSES (t
) = clauses
;
13604 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
13608 add_stmt (finish_omp_structured_block (stmt
));
13615 case OMP_TARGET_DATA
:
13617 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false,
13618 args
, complain
, in_decl
);
13619 stmt
= push_stmt_list ();
13620 RECUR (OMP_BODY (t
));
13621 stmt
= pop_stmt_list (stmt
);
13624 OMP_BODY (t
) = stmt
;
13625 OMP_CLAUSES (t
) = tmp
;
13629 case OMP_TARGET_UPDATE
:
13630 tmp
= tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t
), false,
13631 args
, complain
, in_decl
);
13633 OMP_CLAUSES (t
) = tmp
;
13640 case OMP_TASKGROUP
:
13642 stmt
= push_stmt_list ();
13643 RECUR (OMP_BODY (t
));
13644 stmt
= pop_stmt_list (stmt
);
13647 OMP_BODY (t
) = stmt
;
13652 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
13653 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
13655 tree op1
= TREE_OPERAND (t
, 1);
13656 tree rhs1
= NULL_TREE
;
13658 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
13660 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
13661 op1
= TREE_OPERAND (op1
, 1);
13663 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13664 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13665 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
13666 NULL_TREE
, NULL_TREE
, rhs1
,
13667 OMP_ATOMIC_SEQ_CST (t
));
13671 tree op1
= TREE_OPERAND (t
, 1);
13672 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
13673 tree rhs1
= NULL_TREE
;
13674 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
13675 enum tree_code opcode
= NOP_EXPR
;
13676 if (code
== OMP_ATOMIC_READ
)
13678 v
= RECUR (TREE_OPERAND (op1
, 0));
13679 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13681 else if (code
== OMP_ATOMIC_CAPTURE_OLD
13682 || code
== OMP_ATOMIC_CAPTURE_NEW
)
13684 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
13685 v
= RECUR (TREE_OPERAND (op1
, 0));
13686 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13687 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
13689 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
13690 op11
= TREE_OPERAND (op11
, 1);
13692 lhs
= RECUR (TREE_OPERAND (op11
, 0));
13693 rhs
= RECUR (TREE_OPERAND (op11
, 1));
13694 opcode
= TREE_CODE (op11
);
13695 if (opcode
== MODIFY_EXPR
)
13701 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13702 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13704 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
13705 OMP_ATOMIC_SEQ_CST (t
));
13709 case TRANSACTION_EXPR
:
13712 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
13713 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
13715 if (TRANSACTION_EXPR_IS_STMT (t
))
13717 tree body
= TRANSACTION_EXPR_BODY (t
);
13718 tree noex
= NULL_TREE
;
13719 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
13721 noex
= MUST_NOT_THROW_COND (body
);
13722 if (noex
== NULL_TREE
)
13723 noex
= boolean_true_node
;
13724 body
= TREE_OPERAND (body
, 0);
13726 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
13728 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
13732 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
13733 RECUR (TRANSACTION_EXPR_BODY (t
)),
13740 case MUST_NOT_THROW_EXPR
:
13741 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t
, 0)),
13742 RECUR (MUST_NOT_THROW_COND (t
))));
13744 case EXPR_PACK_EXPANSION
:
13745 error ("invalid use of pack expansion expression");
13746 RETURN (error_mark_node
);
13748 case NONTYPE_ARGUMENT_PACK
:
13749 error ("use %<...%> to expand argument pack");
13750 RETURN (error_mark_node
);
13752 case COMPOUND_EXPR
:
13753 tmp
= RECUR (TREE_OPERAND (t
, 0));
13754 if (tmp
== NULL_TREE
)
13755 /* If the first operand was a statement, we're done with it. */
13756 RETURN (RECUR (TREE_OPERAND (t
, 1)));
13757 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
13758 RECUR (TREE_OPERAND (t
, 1)),
13762 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
13764 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13765 /*function_p=*/false,
13766 integral_constant_expression_p
));
13769 RETURN (NULL_TREE
);
13771 input_location
= loc
;
13777 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
13778 function. For description of the body see comment above
13779 cp_parser_omp_declare_reduction_exprs. */
13782 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13784 if (t
== NULL_TREE
|| t
== error_mark_node
)
13787 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
13789 tree_stmt_iterator tsi
;
13792 memset (stmts
, 0, sizeof stmts
);
13793 for (i
= 0, tsi
= tsi_start (t
);
13794 i
< 7 && !tsi_end_p (tsi
);
13795 i
++, tsi_next (&tsi
))
13796 stmts
[i
] = tsi_stmt (tsi
);
13797 gcc_assert (tsi_end_p (tsi
));
13801 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
13802 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
13803 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
13804 args
, complain
, in_decl
);
13805 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
13806 args
, complain
, in_decl
);
13807 DECL_CONTEXT (omp_out
) = current_function_decl
;
13808 DECL_CONTEXT (omp_in
) = current_function_decl
;
13809 keep_next_level (true);
13810 tree block
= begin_omp_structured_block ();
13811 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
13812 block
= finish_omp_structured_block (block
);
13813 block
= maybe_cleanup_point_expr_void (block
);
13814 add_decl_expr (omp_out
);
13815 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
13816 TREE_NO_WARNING (omp_out
) = 1;
13817 add_decl_expr (omp_in
);
13818 finish_expr_stmt (block
);
13822 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
13823 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
13824 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
13825 args
, complain
, in_decl
);
13826 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
13827 args
, complain
, in_decl
);
13828 DECL_CONTEXT (omp_priv
) = current_function_decl
;
13829 DECL_CONTEXT (omp_orig
) = current_function_decl
;
13830 keep_next_level (true);
13831 tree block
= begin_omp_structured_block ();
13832 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
13833 block
= finish_omp_structured_block (block
);
13834 block
= maybe_cleanup_point_expr_void (block
);
13835 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
13836 add_decl_expr (omp_priv
);
13837 add_decl_expr (omp_orig
);
13838 finish_expr_stmt (block
);
13840 add_decl_expr (omp_orig
);
13844 /* T is a postfix-expression that is not being used in a function
13845 call. Return the substituted version of T. */
13848 tsubst_non_call_postfix_expression (tree t
, tree args
,
13849 tsubst_flags_t complain
,
13852 if (TREE_CODE (t
) == SCOPE_REF
)
13853 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
13854 /*done=*/false, /*address_p=*/false);
13856 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13857 /*function_p=*/false,
13858 /*integral_constant_expression_p=*/false);
13863 /* Like tsubst but deals with expressions and performs semantic
13864 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13867 tsubst_copy_and_build (tree t
,
13869 tsubst_flags_t complain
,
13872 bool integral_constant_expression_p
)
13874 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13875 #define RECUR(NODE) \
13876 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13877 /*function_p=*/false, \
13878 integral_constant_expression_p)
13883 if (t
== NULL_TREE
|| t
== error_mark_node
)
13886 loc
= input_location
;
13887 if (EXPR_HAS_LOCATION (t
))
13888 input_location
= EXPR_LOCATION (t
);
13890 /* N3276 decltype magic only applies to calls at the top level or on the
13891 right side of a comma. */
13892 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
13893 complain
&= ~tf_decltype
;
13895 switch (TREE_CODE (t
))
13899 /* Fall through. */
13900 case IDENTIFIER_NODE
:
13904 bool non_integral_constant_expression_p
;
13905 const char *error_msg
;
13907 if (IDENTIFIER_TYPENAME_P (t
))
13909 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13910 t
= mangle_conv_op_name_for_type (new_type
);
13913 /* Look up the name. */
13914 decl
= lookup_name (t
);
13916 /* By convention, expressions use ERROR_MARK_NODE to indicate
13917 failure, not NULL_TREE. */
13918 if (decl
== NULL_TREE
)
13919 decl
= error_mark_node
;
13921 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
13923 integral_constant_expression_p
,
13924 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
13925 &non_integral_constant_expression_p
,
13926 /*template_p=*/false,
13928 /*address_p=*/false,
13929 /*template_arg_p=*/false,
13934 if (!function_p
&& identifier_p (decl
))
13936 if (complain
& tf_error
)
13937 unqualified_name_lookup_error (decl
);
13938 decl
= error_mark_node
;
13943 case TEMPLATE_ID_EXPR
:
13946 tree templ
= RECUR (TREE_OPERAND (t
, 0));
13947 tree targs
= TREE_OPERAND (t
, 1);
13950 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
13952 if (TREE_CODE (templ
) == COMPONENT_REF
)
13954 object
= TREE_OPERAND (templ
, 0);
13955 templ
= TREE_OPERAND (templ
, 1);
13958 object
= NULL_TREE
;
13959 templ
= lookup_template_function (templ
, targs
);
13962 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
13963 object
, templ
, NULL_TREE
));
13965 RETURN (baselink_for_fns (templ
));
13970 tree r
= RECUR (TREE_OPERAND (t
, 0));
13972 if (REFERENCE_REF_P (t
))
13974 /* A type conversion to reference type will be enclosed in
13975 such an indirect ref, but the substitution of the cast
13976 will have also added such an indirect ref. */
13977 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
13978 r
= convert_from_reference (r
);
13981 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
13982 complain
|decltype_flag
);
13988 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13989 RECUR (TREE_OPERAND (t
, 0))));
13991 case IMPLICIT_CONV_EXPR
:
13993 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13994 tree expr
= RECUR (TREE_OPERAND (t
, 0));
13995 int flags
= LOOKUP_IMPLICIT
;
13996 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
13997 flags
= LOOKUP_NORMAL
;
13998 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
14005 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
14006 RECUR (TREE_OPERAND (t
, 0))));
14009 case REINTERPRET_CAST_EXPR
:
14010 case CONST_CAST_EXPR
:
14011 case DYNAMIC_CAST_EXPR
:
14012 case STATIC_CAST_EXPR
:
14015 tree op
, r
= NULL_TREE
;
14017 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14018 if (integral_constant_expression_p
14019 && !cast_valid_in_integral_constant_expression_p (type
))
14021 if (complain
& tf_error
)
14022 error ("a cast to a type other than an integral or "
14023 "enumeration type cannot appear in a constant-expression");
14024 RETURN (error_mark_node
);
14027 op
= RECUR (TREE_OPERAND (t
, 0));
14029 ++c_inhibit_evaluation_warnings
;
14030 switch (TREE_CODE (t
))
14033 r
= build_functional_cast (type
, op
, complain
);
14035 case REINTERPRET_CAST_EXPR
:
14036 r
= build_reinterpret_cast (type
, op
, complain
);
14038 case CONST_CAST_EXPR
:
14039 r
= build_const_cast (type
, op
, complain
);
14041 case DYNAMIC_CAST_EXPR
:
14042 r
= build_dynamic_cast (type
, op
, complain
);
14044 case STATIC_CAST_EXPR
:
14045 r
= build_static_cast (type
, op
, complain
);
14048 gcc_unreachable ();
14050 --c_inhibit_evaluation_warnings
;
14055 case POSTDECREMENT_EXPR
:
14056 case POSTINCREMENT_EXPR
:
14057 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14058 args
, complain
, in_decl
);
14059 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
14060 complain
|decltype_flag
));
14062 case PREDECREMENT_EXPR
:
14063 case PREINCREMENT_EXPR
:
14067 case TRUTH_NOT_EXPR
:
14068 case UNARY_PLUS_EXPR
: /* Unary + */
14069 case REALPART_EXPR
:
14070 case IMAGPART_EXPR
:
14071 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
14072 RECUR (TREE_OPERAND (t
, 0)),
14073 complain
|decltype_flag
));
14075 case FIX_TRUNC_EXPR
:
14076 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
14080 op1
= TREE_OPERAND (t
, 0);
14081 if (TREE_CODE (op1
) == LABEL_DECL
)
14082 RETURN (finish_label_address_expr (DECL_NAME (op1
),
14083 EXPR_LOCATION (op1
)));
14084 if (TREE_CODE (op1
) == SCOPE_REF
)
14085 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
14086 /*done=*/true, /*address_p=*/true);
14088 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
14090 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
14091 complain
|decltype_flag
));
14096 case TRUNC_DIV_EXPR
:
14097 case CEIL_DIV_EXPR
:
14098 case FLOOR_DIV_EXPR
:
14099 case ROUND_DIV_EXPR
:
14100 case EXACT_DIV_EXPR
:
14104 case TRUNC_MOD_EXPR
:
14105 case FLOOR_MOD_EXPR
:
14106 case TRUTH_ANDIF_EXPR
:
14107 case TRUTH_ORIF_EXPR
:
14108 case TRUTH_AND_EXPR
:
14109 case TRUTH_OR_EXPR
:
14127 ++c_inhibit_evaluation_warnings
;
14129 r
= build_x_binary_op
14130 (input_location
, TREE_CODE (t
),
14131 RECUR (TREE_OPERAND (t
, 0)),
14132 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
14134 : TREE_CODE (TREE_OPERAND (t
, 0))),
14135 RECUR (TREE_OPERAND (t
, 1)),
14136 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
14138 : TREE_CODE (TREE_OPERAND (t
, 1))),
14140 complain
|decltype_flag
);
14141 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
14142 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14144 --c_inhibit_evaluation_warnings
;
14150 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
14151 /*address_p=*/false));
14153 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14154 args
, complain
, in_decl
);
14155 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
14156 RECUR (TREE_OPERAND (t
, 1)),
14157 complain
|decltype_flag
));
14159 case ARRAY_NOTATION_REF
:
14161 tree start_index
, length
, stride
;
14162 op1
= tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t
),
14163 args
, complain
, in_decl
);
14164 start_index
= RECUR (ARRAY_NOTATION_START (t
));
14165 length
= RECUR (ARRAY_NOTATION_LENGTH (t
));
14166 stride
= RECUR (ARRAY_NOTATION_STRIDE (t
));
14167 RETURN (build_array_notation_ref (EXPR_LOCATION (t
), op1
, start_index
,
14168 length
, stride
, TREE_TYPE (op1
)));
14171 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
14172 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
14179 op1
= TREE_OPERAND (t
, 0);
14180 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
14181 op1
= TREE_TYPE (op1
);
14184 /* When there are no ARGS, we are trying to evaluate a
14185 non-dependent expression from the parser. Trying to do
14186 the substitutions may not work. */
14188 op1
= TREE_TYPE (op1
);
14192 ++cp_unevaluated_operand
;
14193 ++c_inhibit_evaluation_warnings
;
14195 op1
= tsubst (op1
, args
, complain
, in_decl
);
14197 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14198 /*function_p=*/false,
14199 /*integral_constant_expression_p=*/
14201 --cp_unevaluated_operand
;
14202 --c_inhibit_evaluation_warnings
;
14205 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
14206 complain
& tf_error
);
14208 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
14209 complain
& tf_error
);
14210 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
14212 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
14214 if (!processing_template_decl
&& TYPE_P (op1
))
14216 r
= build_min (SIZEOF_EXPR
, size_type_node
,
14217 build1 (NOP_EXPR
, op1
, error_mark_node
));
14218 SIZEOF_EXPR_TYPE_P (r
) = 1;
14221 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
14222 TREE_SIDE_EFFECTS (r
) = 0;
14223 TREE_READONLY (r
) = 1;
14225 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
14230 case AT_ENCODE_EXPR
:
14232 op1
= TREE_OPERAND (t
, 0);
14233 ++cp_unevaluated_operand
;
14234 ++c_inhibit_evaluation_warnings
;
14235 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14236 /*function_p=*/false,
14237 /*integral_constant_expression_p=*/false);
14238 --cp_unevaluated_operand
;
14239 --c_inhibit_evaluation_warnings
;
14240 RETURN (objc_build_encode_expr (op1
));
14243 case NOEXCEPT_EXPR
:
14244 op1
= TREE_OPERAND (t
, 0);
14245 ++cp_unevaluated_operand
;
14246 ++c_inhibit_evaluation_warnings
;
14247 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14248 /*function_p=*/false,
14249 /*integral_constant_expression_p=*/false);
14250 --cp_unevaluated_operand
;
14251 --c_inhibit_evaluation_warnings
;
14252 RETURN (finish_noexcept_expr (op1
, complain
));
14258 ++c_inhibit_evaluation_warnings
;
14260 r
= build_x_modify_expr
14261 (EXPR_LOCATION (t
),
14262 RECUR (TREE_OPERAND (t
, 0)),
14263 TREE_CODE (TREE_OPERAND (t
, 1)),
14264 RECUR (TREE_OPERAND (t
, 2)),
14265 complain
|decltype_flag
);
14266 /* TREE_NO_WARNING must be set if either the expression was
14267 parenthesized or it uses an operator such as >>= rather
14268 than plain assignment. In the former case, it was already
14269 set and must be copied. In the latter case,
14270 build_x_modify_expr sets it and it must not be reset
14272 if (TREE_NO_WARNING (t
))
14273 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14275 --c_inhibit_evaluation_warnings
;
14281 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14282 args
, complain
, in_decl
);
14283 /* Remember that there was a reference to this entity. */
14286 RETURN (build_x_arrow (input_location
, op1
, complain
));
14290 tree placement
= RECUR (TREE_OPERAND (t
, 0));
14291 tree init
= RECUR (TREE_OPERAND (t
, 3));
14292 vec
<tree
, va_gc
> *placement_vec
;
14293 vec
<tree
, va_gc
> *init_vec
;
14296 if (placement
== NULL_TREE
)
14297 placement_vec
= NULL
;
14300 placement_vec
= make_tree_vector ();
14301 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
14302 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
14305 /* If there was an initializer in the original tree, but it
14306 instantiated to an empty list, then we should pass a
14307 non-NULL empty vector to tell build_new that it was an
14308 empty initializer() rather than no initializer. This can
14309 only happen when the initializer is a pack expansion whose
14310 parameter packs are of length zero. */
14311 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
14315 init_vec
= make_tree_vector ();
14316 if (init
== void_zero_node
)
14317 gcc_assert (init_vec
!= NULL
);
14320 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
14321 vec_safe_push (init_vec
, TREE_VALUE (init
));
14325 ret
= build_new (&placement_vec
,
14326 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
14327 RECUR (TREE_OPERAND (t
, 2)),
14329 NEW_EXPR_USE_GLOBAL (t
),
14332 if (placement_vec
!= NULL
)
14333 release_tree_vector (placement_vec
);
14334 if (init_vec
!= NULL
)
14335 release_tree_vector (init_vec
);
14341 RETURN (delete_sanity
14342 (RECUR (TREE_OPERAND (t
, 0)),
14343 RECUR (TREE_OPERAND (t
, 1)),
14344 DELETE_EXPR_USE_VEC (t
),
14345 DELETE_EXPR_USE_GLOBAL (t
),
14348 case COMPOUND_EXPR
:
14350 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
14351 complain
& ~tf_decltype
, in_decl
,
14352 /*function_p=*/false,
14353 integral_constant_expression_p
);
14354 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
14356 RECUR (TREE_OPERAND (t
, 1)),
14357 complain
|decltype_flag
));
14363 vec
<tree
, va_gc
> *call_args
;
14364 unsigned int nargs
, i
;
14369 function
= CALL_EXPR_FN (t
);
14370 /* When we parsed the expression, we determined whether or
14371 not Koenig lookup should be performed. */
14372 koenig_p
= KOENIG_LOOKUP_P (t
);
14373 if (TREE_CODE (function
) == SCOPE_REF
)
14375 qualified_p
= true;
14376 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
14378 /*address_p=*/false);
14380 else if (koenig_p
&& identifier_p (function
))
14382 /* Do nothing; calling tsubst_copy_and_build on an identifier
14383 would incorrectly perform unqualified lookup again.
14385 Note that we can also have an IDENTIFIER_NODE if the earlier
14386 unqualified lookup found a member function; in that case
14387 koenig_p will be false and we do want to do the lookup
14388 again to find the instantiated member function.
14390 FIXME but doing that causes c++/15272, so we need to stop
14391 using IDENTIFIER_NODE in that situation. */
14392 qualified_p
= false;
14396 if (TREE_CODE (function
) == COMPONENT_REF
)
14398 tree op
= TREE_OPERAND (function
, 1);
14400 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
14401 || (BASELINK_P (op
)
14402 && BASELINK_QUALIFIED_P (op
)));
14405 qualified_p
= false;
14407 if (TREE_CODE (function
) == ADDR_EXPR
14408 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
14409 /* Avoid error about taking the address of a constructor. */
14410 function
= TREE_OPERAND (function
, 0);
14412 function
= tsubst_copy_and_build (function
, args
, complain
,
14415 integral_constant_expression_p
);
14417 if (BASELINK_P (function
))
14418 qualified_p
= true;
14421 nargs
= call_expr_nargs (t
);
14422 call_args
= make_tree_vector ();
14423 for (i
= 0; i
< nargs
; ++i
)
14425 tree arg
= CALL_EXPR_ARG (t
, i
);
14427 if (!PACK_EXPANSION_P (arg
))
14428 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
14431 /* Expand the pack expansion and push each entry onto
14433 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
14434 if (TREE_CODE (arg
) == TREE_VEC
)
14436 unsigned int len
, j
;
14438 len
= TREE_VEC_LENGTH (arg
);
14439 for (j
= 0; j
< len
; ++j
)
14441 tree value
= TREE_VEC_ELT (arg
, j
);
14442 if (value
!= NULL_TREE
)
14443 value
= convert_from_reference (value
);
14444 vec_safe_push (call_args
, value
);
14449 /* A partial substitution. Add one entry. */
14450 vec_safe_push (call_args
, arg
);
14455 /* We do not perform argument-dependent lookup if normal
14456 lookup finds a non-function, in accordance with the
14457 expected resolution of DR 218. */
14459 && ((is_overloaded_fn (function
)
14460 /* If lookup found a member function, the Koenig lookup is
14461 not appropriate, even if an unqualified-name was used
14462 to denote the function. */
14463 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
14464 || identifier_p (function
))
14465 /* Only do this when substitution turns a dependent call
14466 into a non-dependent call. */
14467 && type_dependent_expression_p_push (t
)
14468 && !any_type_dependent_arguments_p (call_args
))
14469 function
= perform_koenig_lookup (function
, call_args
, false,
14472 if (identifier_p (function
)
14473 && !any_type_dependent_arguments_p (call_args
))
14475 if (koenig_p
&& (complain
& tf_warning_or_error
))
14477 /* For backwards compatibility and good diagnostics, try
14478 the unqualified lookup again if we aren't in SFINAE
14480 tree unq
= (tsubst_copy_and_build
14481 (function
, args
, complain
, in_decl
, true,
14482 integral_constant_expression_p
));
14483 if (unq
== error_mark_node
)
14484 RETURN (error_mark_node
);
14486 if (unq
!= function
)
14489 if (INDIRECT_REF_P (fn
))
14490 fn
= TREE_OPERAND (fn
, 0);
14491 if (TREE_CODE (fn
) == COMPONENT_REF
)
14492 fn
= TREE_OPERAND (fn
, 1);
14493 if (is_overloaded_fn (fn
))
14494 fn
= get_first_fn (fn
);
14495 if (permerror (EXPR_LOC_OR_HERE (t
),
14496 "%qD was not declared in this scope, "
14497 "and no declarations were found by "
14498 "argument-dependent lookup at the point "
14499 "of instantiation", function
))
14502 /* Can't say anything more. */;
14503 else if (DECL_CLASS_SCOPE_P (fn
))
14505 inform (EXPR_LOC_OR_HERE (t
),
14506 "declarations in dependent base %qT are "
14507 "not found by unqualified lookup",
14508 DECL_CLASS_CONTEXT (fn
));
14509 if (current_class_ptr
)
14510 inform (EXPR_LOC_OR_HERE (t
),
14511 "use %<this->%D%> instead", function
);
14513 inform (EXPR_LOC_OR_HERE (t
),
14514 "use %<%T::%D%> instead",
14515 current_class_name
, function
);
14518 inform (0, "%q+D declared here, later in the "
14519 "translation unit", fn
);
14524 if (identifier_p (function
))
14526 if (complain
& tf_error
)
14527 unqualified_name_lookup_error (function
);
14528 release_tree_vector (call_args
);
14529 RETURN (error_mark_node
);
14533 /* Remember that there was a reference to this entity. */
14534 if (DECL_P (function
))
14535 mark_used (function
);
14537 /* Put back tf_decltype for the actual call. */
14538 complain
|= decltype_flag
;
14540 if (TREE_CODE (function
) == OFFSET_REF
)
14541 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
14543 else if (TREE_CODE (function
) == COMPONENT_REF
)
14545 tree instance
= TREE_OPERAND (function
, 0);
14546 tree fn
= TREE_OPERAND (function
, 1);
14548 if (processing_template_decl
14549 && (type_dependent_expression_p (instance
)
14550 || (!BASELINK_P (fn
)
14551 && TREE_CODE (fn
) != FIELD_DECL
)
14552 || type_dependent_expression_p (fn
)
14553 || any_type_dependent_arguments_p (call_args
)))
14554 ret
= build_nt_call_vec (function
, call_args
);
14555 else if (!BASELINK_P (fn
))
14556 ret
= finish_call_expr (function
, &call_args
,
14557 /*disallow_virtual=*/false,
14558 /*koenig_p=*/false,
14561 ret
= (build_new_method_call
14563 &call_args
, NULL_TREE
,
14564 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
14569 ret
= finish_call_expr (function
, &call_args
,
14570 /*disallow_virtual=*/qualified_p
,
14574 release_tree_vector (call_args
);
14581 tree cond
= RECUR (TREE_OPERAND (t
, 0));
14584 if (TREE_CODE (cond
) == INTEGER_CST
)
14586 if (integer_zerop (cond
))
14588 ++c_inhibit_evaluation_warnings
;
14589 exp1
= RECUR (TREE_OPERAND (t
, 1));
14590 --c_inhibit_evaluation_warnings
;
14591 exp2
= RECUR (TREE_OPERAND (t
, 2));
14595 exp1
= RECUR (TREE_OPERAND (t
, 1));
14596 ++c_inhibit_evaluation_warnings
;
14597 exp2
= RECUR (TREE_OPERAND (t
, 2));
14598 --c_inhibit_evaluation_warnings
;
14603 exp1
= RECUR (TREE_OPERAND (t
, 1));
14604 exp2
= RECUR (TREE_OPERAND (t
, 2));
14607 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
14608 cond
, exp1
, exp2
, complain
));
14611 case PSEUDO_DTOR_EXPR
:
14612 RETURN (finish_pseudo_destructor_expr
14613 (RECUR (TREE_OPERAND (t
, 0)),
14614 RECUR (TREE_OPERAND (t
, 1)),
14615 tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
),
14620 tree purpose
, value
, chain
;
14622 if (t
== void_list_node
)
14625 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
14626 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
14628 /* We have pack expansions, so expand those and
14629 create a new list out of it. */
14630 tree purposevec
= NULL_TREE
;
14631 tree valuevec
= NULL_TREE
;
14635 /* Expand the argument expressions. */
14636 if (TREE_PURPOSE (t
))
14637 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
14638 complain
, in_decl
);
14639 if (TREE_VALUE (t
))
14640 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
14641 complain
, in_decl
);
14643 /* Build the rest of the list. */
14644 chain
= TREE_CHAIN (t
);
14645 if (chain
&& chain
!= void_type_node
)
14646 chain
= RECUR (chain
);
14648 /* Determine the number of arguments. */
14649 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
14651 len
= TREE_VEC_LENGTH (purposevec
);
14652 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
14654 else if (TREE_CODE (valuevec
) == TREE_VEC
)
14655 len
= TREE_VEC_LENGTH (valuevec
);
14658 /* Since we only performed a partial substitution into
14659 the argument pack, we only RETURN (a single list
14661 if (purposevec
== TREE_PURPOSE (t
)
14662 && valuevec
== TREE_VALUE (t
)
14663 && chain
== TREE_CHAIN (t
))
14666 RETURN (tree_cons (purposevec
, valuevec
, chain
));
14669 /* Convert the argument vectors into a TREE_LIST */
14673 /* Grab the Ith values. */
14675 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
14678 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
14681 /* Build the list (backwards). */
14682 chain
= tree_cons (purpose
, value
, chain
);
14688 purpose
= TREE_PURPOSE (t
);
14690 purpose
= RECUR (purpose
);
14691 value
= TREE_VALUE (t
);
14693 value
= RECUR (value
);
14694 chain
= TREE_CHAIN (t
);
14695 if (chain
&& chain
!= void_type_node
)
14696 chain
= RECUR (chain
);
14697 if (purpose
== TREE_PURPOSE (t
)
14698 && value
== TREE_VALUE (t
)
14699 && chain
== TREE_CHAIN (t
))
14701 RETURN (tree_cons (purpose
, value
, chain
));
14704 case COMPONENT_REF
:
14710 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14711 args
, complain
, in_decl
);
14712 /* Remember that there was a reference to this entity. */
14713 if (DECL_P (object
))
14714 mark_used (object
);
14715 object_type
= TREE_TYPE (object
);
14717 member
= TREE_OPERAND (t
, 1);
14718 if (BASELINK_P (member
))
14719 member
= tsubst_baselink (member
,
14720 non_reference (TREE_TYPE (object
)),
14721 args
, complain
, in_decl
);
14723 member
= tsubst_copy (member
, args
, complain
, in_decl
);
14724 if (member
== error_mark_node
)
14725 RETURN (error_mark_node
);
14727 if (type_dependent_expression_p (object
))
14728 /* We can't do much here. */;
14729 else if (!CLASS_TYPE_P (object_type
))
14731 if (scalarish_type_p (object_type
))
14733 tree s
= NULL_TREE
;
14734 tree dtor
= member
;
14736 if (TREE_CODE (dtor
) == SCOPE_REF
)
14738 s
= TREE_OPERAND (dtor
, 0);
14739 dtor
= TREE_OPERAND (dtor
, 1);
14741 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
14743 dtor
= TREE_OPERAND (dtor
, 0);
14745 RETURN (finish_pseudo_destructor_expr
14746 (object
, s
, dtor
, input_location
));
14750 else if (TREE_CODE (member
) == SCOPE_REF
14751 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
14753 /* Lookup the template functions now that we know what the
14755 tree scope
= TREE_OPERAND (member
, 0);
14756 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
14757 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
14758 member
= lookup_qualified_name (scope
, tmpl
,
14759 /*is_type_p=*/false,
14760 /*complain=*/false);
14761 if (BASELINK_P (member
))
14763 BASELINK_FUNCTIONS (member
)
14764 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
14766 member
= (adjust_result_of_qualified_name_lookup
14767 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
14772 qualified_name_lookup_error (scope
, tmpl
, member
,
14774 RETURN (error_mark_node
);
14777 else if (TREE_CODE (member
) == SCOPE_REF
14778 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
14779 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
14781 if (complain
& tf_error
)
14783 if (TYPE_P (TREE_OPERAND (member
, 0)))
14784 error ("%qT is not a class or namespace",
14785 TREE_OPERAND (member
, 0));
14787 error ("%qD is not a class or namespace",
14788 TREE_OPERAND (member
, 0));
14790 RETURN (error_mark_node
);
14792 else if (TREE_CODE (member
) == FIELD_DECL
)
14793 RETURN (finish_non_static_data_member (member
, object
, NULL_TREE
));
14795 RETURN (finish_class_member_access_expr (object
, member
,
14796 /*template_p=*/false,
14801 RETURN (build_throw
14802 (RECUR (TREE_OPERAND (t
, 0))));
14806 vec
<constructor_elt
, va_gc
> *n
;
14807 constructor_elt
*ce
;
14808 unsigned HOST_WIDE_INT idx
;
14809 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14810 bool process_index_p
;
14812 bool need_copy_p
= false;
14815 if (type
== error_mark_node
)
14816 RETURN (error_mark_node
);
14818 /* digest_init will do the wrong thing if we let it. */
14819 if (type
&& TYPE_PTRMEMFUNC_P (type
))
14822 /* We do not want to process the index of aggregate
14823 initializers as they are identifier nodes which will be
14824 looked up by digest_init. */
14825 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
14827 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
14828 newlen
= vec_safe_length (n
);
14829 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
14831 if (ce
->index
&& process_index_p
14832 /* An identifier index is looked up in the type
14833 being initialized, not the current scope. */
14834 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
14835 ce
->index
= RECUR (ce
->index
);
14837 if (PACK_EXPANSION_P (ce
->value
))
14839 /* Substitute into the pack expansion. */
14840 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
14843 if (ce
->value
== error_mark_node
14844 || PACK_EXPANSION_P (ce
->value
))
14846 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
14847 /* Just move the argument into place. */
14848 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
14851 /* Update the length of the final CONSTRUCTOR
14852 arguments vector, and note that we will need to
14854 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
14855 need_copy_p
= true;
14859 ce
->value
= RECUR (ce
->value
);
14864 vec
<constructor_elt
, va_gc
> *old_n
= n
;
14866 vec_alloc (n
, newlen
);
14867 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
14869 if (TREE_CODE (ce
->value
) == TREE_VEC
)
14871 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
14872 for (i
= 0; i
< len
; ++i
)
14873 CONSTRUCTOR_APPEND_ELT (n
, 0,
14874 TREE_VEC_ELT (ce
->value
, i
));
14877 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
14881 r
= build_constructor (init_list_type_node
, n
);
14882 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
14884 if (TREE_HAS_CONSTRUCTOR (t
))
14885 RETURN (finish_compound_literal (type
, r
, complain
));
14887 TREE_TYPE (r
) = type
;
14893 tree operand_0
= TREE_OPERAND (t
, 0);
14894 if (TYPE_P (operand_0
))
14896 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
14897 RETURN (get_typeid (operand_0
, complain
));
14901 operand_0
= RECUR (operand_0
);
14902 RETURN (build_typeid (operand_0
, complain
));
14909 else if (DECL_PACK_P (t
))
14911 /* We don't build decls for an instantiation of a
14912 variadic capture proxy, we instantiate the elements
14914 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
14915 return RECUR (DECL_VALUE_EXPR (t
));
14921 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
14923 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
14924 /* If the original type was a reference, we'll be wrapped in
14925 the appropriate INDIRECT_REF. */
14926 r
= convert_from_reference (r
);
14931 RETURN (build_x_va_arg (EXPR_LOCATION (t
),
14932 RECUR (TREE_OPERAND (t
, 0)),
14933 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
)));
14935 case OFFSETOF_EXPR
:
14936 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t
, 0))));
14940 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
14941 complain
, in_decl
);
14943 tree type2
= TRAIT_EXPR_TYPE2 (t
);
14945 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
14947 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
14952 tree old_stmt_expr
= cur_stmt_expr
;
14953 tree stmt_expr
= begin_stmt_expr ();
14955 cur_stmt_expr
= stmt_expr
;
14956 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
14957 integral_constant_expression_p
);
14958 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
14959 cur_stmt_expr
= old_stmt_expr
;
14961 /* If the resulting list of expression statement is empty,
14962 fold it further into void_zero_node. */
14963 if (empty_expr_stmt_p (stmt_expr
))
14964 stmt_expr
= void_zero_node
;
14966 RETURN (stmt_expr
);
14971 tree r
= build_lambda_expr ();
14973 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
14974 LAMBDA_EXPR_CLOSURE (r
) = type
;
14975 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
14977 LAMBDA_EXPR_LOCATION (r
)
14978 = LAMBDA_EXPR_LOCATION (t
);
14979 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
14980 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
14981 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
14982 LAMBDA_EXPR_DISCRIMINATOR (r
)
14983 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
14984 /* For a function scope, we want to use tsubst so that we don't
14985 complain about referring to an auto function before its return
14986 type has been deduced. Otherwise, we want to use tsubst_copy so
14987 that we look up the existing field/parameter/variable rather
14988 than build a new one. */
14989 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
14990 if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
14991 scope
= tsubst (scope
, args
, complain
, in_decl
);
14992 else if (scope
&& TREE_CODE (scope
) == PARM_DECL
)
14994 /* Look up the parameter we want directly, as tsubst_copy
14995 doesn't do what we need. */
14996 tree fn
= tsubst (DECL_CONTEXT (scope
), args
, complain
, in_decl
);
14997 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
14998 while (DECL_PARM_INDEX (parm
) != DECL_PARM_INDEX (scope
))
14999 parm
= DECL_CHAIN (parm
);
15001 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15002 if (DECL_CONTEXT (scope
) == NULL_TREE
)
15003 DECL_CONTEXT (scope
) = fn
;
15006 scope
= RECUR (scope
);
15007 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
15008 LAMBDA_EXPR_RETURN_TYPE (r
)
15009 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
15011 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
15012 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
15014 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15015 determine_visibility (TYPE_NAME (type
));
15016 /* Now that we know visibility, instantiate the type so we have a
15017 declaration of the op() for later calls to lambda_function. */
15018 complete_type (type
);
15020 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
15022 RETURN (build_lambda_object (r
));
15026 /* We can get here for a constant initializer of non-dependent type.
15027 FIXME stop folding in cp_parser_initializer_clause. */
15029 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
15034 case TRANSACTION_EXPR
:
15035 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
15036 integral_constant_expression_p
));
15039 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
15041 case VEC_PERM_EXPR
:
15042 RETURN (build_x_vec_perm_expr (input_location
,
15043 RECUR (TREE_OPERAND (t
, 0)),
15044 RECUR (TREE_OPERAND (t
, 1)),
15045 RECUR (TREE_OPERAND (t
, 2)),
15049 /* Handle Objective-C++ constructs, if appropriate. */
15052 = objcp_tsubst_copy_and_build (t
, args
, complain
,
15053 in_decl
, /*function_p=*/false);
15057 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
15063 input_location
= loc
;
15067 /* Verify that the instantiated ARGS are valid. For type arguments,
15068 make sure that the type's linkage is ok. For non-type arguments,
15069 make sure they are constants if they are integral or enumerations.
15070 Emit an error under control of COMPLAIN, and return TRUE on error. */
15073 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
15075 if (dependent_template_arg_p (t
))
15077 if (ARGUMENT_PACK_P (t
))
15079 tree vec
= ARGUMENT_PACK_ARGS (t
);
15080 int len
= TREE_VEC_LENGTH (vec
);
15081 bool result
= false;
15084 for (i
= 0; i
< len
; ++i
)
15085 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
15089 else if (TYPE_P (t
))
15091 /* [basic.link]: A name with no linkage (notably, the name
15092 of a class or enumeration declared in a local scope)
15093 shall not be used to declare an entity with linkage.
15094 This implies that names with no linkage cannot be used as
15097 DR 757 relaxes this restriction for C++0x. */
15098 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
15099 : no_linkage_check (t
, /*relaxed_p=*/false));
15103 /* DR 488 makes use of a type with no linkage cause
15104 type deduction to fail. */
15105 if (complain
& tf_error
)
15107 if (TYPE_ANONYMOUS_P (nt
))
15108 error ("%qT is/uses anonymous type", t
);
15110 error ("template argument for %qD uses local type %qT",
15115 /* In order to avoid all sorts of complications, we do not
15116 allow variably-modified types as template arguments. */
15117 else if (variably_modified_type_p (t
, NULL_TREE
))
15119 if (complain
& tf_error
)
15120 error ("%qT is a variably modified type", t
);
15124 /* Class template and alias template arguments should be OK. */
15125 else if (DECL_TYPE_TEMPLATE_P (t
))
15127 /* A non-type argument of integral or enumerated type must be a
15129 else if (TREE_TYPE (t
)
15130 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
15131 && !TREE_CONSTANT (t
))
15133 if (complain
& tf_error
)
15134 error ("integral expression %qE is not constant", t
);
15141 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
15143 int ix
, len
= DECL_NTPARMS (tmpl
);
15144 bool result
= false;
15146 for (ix
= 0; ix
!= len
; ix
++)
15148 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
15151 if (result
&& (complain
& tf_error
))
15152 error (" trying to instantiate %qD", tmpl
);
15156 /* We're out of SFINAE context now, so generate diagnostics for the access
15157 errors we saw earlier when instantiating D from TMPL and ARGS. */
15160 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
15162 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
15163 tree type
= TREE_TYPE (pattern
);
15164 location_t loc
= input_location
;
15166 push_access_scope (d
);
15167 push_deferring_access_checks (dk_no_deferred
);
15168 input_location
= DECL_SOURCE_LOCATION (pattern
);
15169 tsubst (type
, args
, tf_warning_or_error
, d
);
15170 input_location
= loc
;
15171 pop_deferring_access_checks ();
15172 pop_access_scope (d
);
15175 /* Instantiate the indicated variable, function, or alias template TMPL with
15176 the template arguments in TARG_PTR. */
15179 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
15181 tree targ_ptr
= orig_args
;
15185 bool access_ok
= true;
15187 if (tmpl
== error_mark_node
)
15188 return error_mark_node
;
15190 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
15192 /* If this function is a clone, handle it specially. */
15193 if (DECL_CLONED_FUNCTION_P (tmpl
))
15198 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15199 DECL_CLONED_FUNCTION. */
15200 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
15201 targ_ptr
, complain
);
15202 if (spec
== error_mark_node
)
15203 return error_mark_node
;
15205 /* Look for the clone. */
15206 FOR_EACH_CLONE (clone
, spec
)
15207 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
15209 /* We should always have found the clone by now. */
15210 gcc_unreachable ();
15214 /* Check to see if we already have this specialization. */
15215 gen_tmpl
= most_general_template (tmpl
);
15216 if (tmpl
!= gen_tmpl
)
15217 /* The TMPL is a partial instantiation. To get a full set of
15218 arguments we must add the arguments used to perform the
15219 partial instantiation. */
15220 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
15223 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15224 but it doesn't seem to be on the hot path. */
15225 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
15227 gcc_assert (tmpl
== gen_tmpl
15228 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
15230 || fndecl
== NULL_TREE
);
15232 if (spec
!= NULL_TREE
)
15234 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
15236 if (complain
& tf_error
)
15237 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
15238 return error_mark_node
;
15243 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
15245 return error_mark_node
;
15247 /* We are building a FUNCTION_DECL, during which the access of its
15248 parameters and return types have to be checked. However this
15249 FUNCTION_DECL which is the desired context for access checking
15250 is not built yet. We solve this chicken-and-egg problem by
15251 deferring all checks until we have the FUNCTION_DECL. */
15252 push_deferring_access_checks (dk_deferred
);
15254 /* Instantiation of the function happens in the context of the function
15255 template, not the context of the overload resolution we're doing. */
15256 push_to_top_level ();
15257 /* If there are dependent arguments, e.g. because we're doing partial
15258 ordering, make sure processing_template_decl stays set. */
15259 if (uses_template_parms (targ_ptr
))
15260 ++processing_template_decl
;
15261 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
15263 tree ctx
= tsubst (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
15264 complain
, gen_tmpl
);
15265 push_nested_class (ctx
);
15267 /* Substitute template parameters to obtain the specialization. */
15268 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
15269 targ_ptr
, complain
, gen_tmpl
);
15270 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
15271 pop_nested_class ();
15272 pop_from_top_level ();
15274 if (fndecl
== error_mark_node
)
15276 pop_deferring_access_checks ();
15277 return error_mark_node
;
15280 /* The DECL_TI_TEMPLATE should always be the immediate parent
15281 template, not the most general template. */
15282 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
15284 /* Now we know the specialization, compute access previously
15286 push_access_scope (fndecl
);
15287 if (!perform_deferred_access_checks (complain
))
15289 pop_access_scope (fndecl
);
15290 pop_deferring_access_checks ();
15292 /* If we've just instantiated the main entry point for a function,
15293 instantiate all the alternate entry points as well. We do this
15294 by cloning the instantiation of the main entry point, not by
15295 instantiating the template clones. */
15296 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
15297 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
15301 if (!(complain
& tf_error
))
15303 /* Remember to reinstantiate when we're out of SFINAE so the user
15304 can see the errors. */
15305 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
15307 return error_mark_node
;
15312 /* Wrapper for instantiate_template_1. */
15315 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
15318 timevar_push (TV_TEMPLATE_INST
);
15319 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
15320 timevar_pop (TV_TEMPLATE_INST
);
15324 /* Instantiate the alias template TMPL with ARGS. Also push a template
15325 instantiation level, which instantiate_template doesn't do because
15326 functions and variables have sufficient context established by the
15330 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
15332 struct pending_template
*old_last_pend
= last_pending_template
;
15333 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
15334 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
15335 return error_mark_node
;
15336 tree tinst
= build_tree_list (tmpl
, args
);
15337 if (!push_tinst_level (tinst
))
15340 return error_mark_node
;
15344 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
15345 args
, tmpl
, complain
,
15346 /*require_all_args=*/true,
15347 /*use_default_args=*/true);
15349 tree r
= instantiate_template (tmpl
, args
, complain
);
15350 pop_tinst_level ();
15351 /* We can't free this if a pending_template entry or last_error_tinst_level
15352 is pointing at it. */
15353 if (last_pending_template
== old_last_pend
15354 && last_error_tinst_level
== old_error_tinst
)
15360 /* PARM is a template parameter pack for FN. Returns true iff
15361 PARM is used in a deducible way in the argument list of FN. */
15364 pack_deducible_p (tree parm
, tree fn
)
15366 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
15367 for (; t
; t
= TREE_CHAIN (t
))
15369 tree type
= TREE_VALUE (t
);
15371 if (!PACK_EXPANSION_P (type
))
15373 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
15374 packs
; packs
= TREE_CHAIN (packs
))
15375 if (TREE_VALUE (packs
) == parm
)
15377 /* The template parameter pack is used in a function parameter
15378 pack. If this is the end of the parameter list, the
15379 template parameter pack is deducible. */
15380 if (TREE_CHAIN (t
) == void_list_node
)
15383 /* Otherwise, not. Well, it could be deduced from
15384 a non-pack parameter, but doing so would end up with
15385 a deduction mismatch, so don't bother. */
15389 /* The template parameter pack isn't used in any function parameter
15390 packs, but it might be used deeper, e.g. tuple<Args...>. */
15394 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
15395 NARGS elements of the arguments that are being used when calling
15396 it. TARGS is a vector into which the deduced template arguments
15399 Return zero for success, 2 for an incomplete match that doesn't resolve
15400 all the types, and 1 for complete failure. An error message will be
15401 printed only for an incomplete match.
15403 If FN is a conversion operator, or we are trying to produce a specific
15404 specialization, RETURN_TYPE is the return type desired.
15406 The EXPLICIT_TARGS are explicit template arguments provided via a
15409 The parameter STRICT is one of:
15412 We are deducing arguments for a function call, as in
15413 [temp.deduct.call].
15416 We are deducing arguments for a conversion function, as in
15417 [temp.deduct.conv].
15420 We are deducing arguments when doing an explicit instantiation
15421 as in [temp.explicit], when determining an explicit specialization
15422 as in [temp.expl.spec], or when taking the address of a function
15423 template, as in [temp.deduct.funcaddr]. */
15426 fn_type_unification (tree fn
,
15427 tree explicit_targs
,
15430 unsigned int nargs
,
15432 unification_kind_t strict
,
15439 tree decl
= NULL_TREE
;
15440 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
15442 static int deduction_depth
;
15443 struct pending_template
*old_last_pend
= last_pending_template
;
15444 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
15445 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
15447 tree r
= error_mark_node
;
15450 complain
|= tf_decltype
;
15452 /* In C++0x, it's possible to have a function template whose type depends
15453 on itself recursively. This is most obvious with decltype, but can also
15454 occur with enumeration scope (c++/48969). So we need to catch infinite
15455 recursion and reject the substitution at deduction time; this function
15456 will return error_mark_node for any repeated substitution.
15458 This also catches excessive recursion such as when f<N> depends on
15459 f<N-1> across all integers, and returns error_mark_node for all the
15460 substitutions back up to the initial one.
15462 This is, of course, not reentrant. */
15463 if (excessive_deduction_depth
)
15464 return error_mark_node
;
15465 tinst
= build_tree_list (fn
, NULL_TREE
);
15468 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
15470 fntype
= TREE_TYPE (fn
);
15471 if (explicit_targs
)
15475 The specified template arguments must match the template
15476 parameters in kind (i.e., type, nontype, template), and there
15477 must not be more arguments than there are parameters;
15478 otherwise type deduction fails.
15480 Nontype arguments must match the types of the corresponding
15481 nontype template parameters, or must be convertible to the
15482 types of the corresponding nontype parameters as specified in
15483 _temp.arg.nontype_, otherwise type deduction fails.
15485 All references in the function type of the function template
15486 to the corresponding template parameters are replaced by the
15487 specified template argument values. If a substitution in a
15488 template parameter or in the function type of the function
15489 template results in an invalid type, type deduction fails. */
15490 int i
, len
= TREE_VEC_LENGTH (tparms
);
15491 location_t loc
= input_location
;
15492 bool incomplete
= false;
15494 /* Adjust any explicit template arguments before entering the
15495 substitution context. */
15497 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
15499 /*require_all_args=*/false,
15500 /*use_default_args=*/false));
15501 if (explicit_targs
== error_mark_node
)
15504 /* Substitute the explicit args into the function type. This is
15505 necessary so that, for instance, explicitly declared function
15506 arguments can match null pointed constants. If we were given
15507 an incomplete set of explicit args, we must not do semantic
15508 processing during substitution as we could create partial
15510 for (i
= 0; i
< len
; i
++)
15512 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
15513 bool parameter_pack
= false;
15514 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
15516 /* Dig out the actual parm. */
15517 if (TREE_CODE (parm
) == TYPE_DECL
15518 || TREE_CODE (parm
) == TEMPLATE_DECL
)
15520 parm
= TREE_TYPE (parm
);
15521 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
15523 else if (TREE_CODE (parm
) == PARM_DECL
)
15525 parm
= DECL_INITIAL (parm
);
15526 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
15529 if (!parameter_pack
&& targ
== NULL_TREE
)
15530 /* No explicit argument for this template parameter. */
15533 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
15535 /* Mark the argument pack as "incomplete". We could
15536 still deduce more arguments during unification.
15537 We remove this mark in type_unification_real. */
15540 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
15541 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
15542 = ARGUMENT_PACK_ARGS (targ
);
15545 /* We have some incomplete argument packs. */
15550 TREE_VALUE (tinst
) = explicit_targs
;
15551 if (!push_tinst_level (tinst
))
15553 excessive_deduction_depth
= true;
15556 processing_template_decl
+= incomplete
;
15557 input_location
= DECL_SOURCE_LOCATION (fn
);
15558 /* Ignore any access checks; we'll see them again in
15559 instantiate_template and they might have the wrong
15560 access path at this point. */
15561 push_deferring_access_checks (dk_deferred
);
15562 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
15563 complain
| tf_partial
, NULL_TREE
);
15564 pop_deferring_access_checks ();
15565 input_location
= loc
;
15566 processing_template_decl
-= incomplete
;
15567 pop_tinst_level ();
15569 if (fntype
== error_mark_node
)
15572 /* Place the explicitly specified arguments in TARGS. */
15573 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
15574 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
15577 /* Never do unification on the 'this' parameter. */
15578 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
15584 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
15585 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
15586 new_args
[0] = return_type
;
15587 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
15592 /* We allow incomplete unification without an error message here
15593 because the standard doesn't seem to explicitly prohibit it. Our
15594 callers must be ready to deal with unification failures in any
15597 TREE_VALUE (tinst
) = targs
;
15598 /* If we aren't explaining yet, push tinst context so we can see where
15599 any errors (e.g. from class instantiations triggered by instantiation
15600 of default template arguments) come from. If we are explaining, this
15601 context is redundant. */
15602 if (!explain_p
&& !push_tinst_level (tinst
))
15604 excessive_deduction_depth
= true;
15608 /* type_unification_real will pass back any access checks from default
15609 template argument substitution. */
15610 vec
<deferred_access_check
, va_gc
> *checks
;
15613 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15614 targs
, parms
, args
, nargs
, /*subr=*/0,
15615 strict
, flags
, &checks
, explain_p
);
15617 pop_tinst_level ();
15621 /* Now that we have bindings for all of the template arguments,
15622 ensure that the arguments deduced for the template template
15623 parameters have compatible template parameter lists. We cannot
15624 check this property before we have deduced all template
15625 arguments, because the template parameter types of a template
15626 template parameter might depend on prior template parameters
15627 deduced after the template template parameter. The following
15628 ill-formed example illustrates this issue:
15630 template<typename T, template<T> class C> void f(C<5>, T);
15632 template<int N> struct X {};
15635 f(X<5>(), 5l); // error: template argument deduction fails
15638 The template parameter list of 'C' depends on the template type
15639 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15640 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15641 time that we deduce 'C'. */
15642 if (!template_template_parm_bindings_ok_p
15643 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
15645 unify_inconsistent_template_template_parameters (explain_p
);
15649 /* All is well so far. Now, check:
15653 When all template arguments have been deduced, all uses of
15654 template parameters in nondeduced contexts are replaced with
15655 the corresponding deduced argument values. If the
15656 substitution results in an invalid type, as described above,
15657 type deduction fails. */
15658 TREE_VALUE (tinst
) = targs
;
15659 if (!push_tinst_level (tinst
))
15661 excessive_deduction_depth
= true;
15665 /* Also collect access checks from the instantiation. */
15666 reopen_deferring_access_checks (checks
);
15668 decl
= instantiate_template (fn
, targs
, complain
);
15670 checks
= get_deferred_access_checks ();
15671 pop_deferring_access_checks ();
15673 pop_tinst_level ();
15675 if (decl
== error_mark_node
)
15678 /* Now perform any access checks encountered during substitution. */
15679 push_access_scope (decl
);
15680 ok
= perform_access_checks (checks
, complain
);
15681 pop_access_scope (decl
);
15685 /* If we're looking for an exact match, check that what we got
15686 is indeed an exact match. It might not be if some template
15687 parameters are used in non-deduced contexts. */
15688 if (strict
== DEDUCE_EXACT
)
15690 tree substed
= TREE_TYPE (decl
);
15694 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
15696 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
15697 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
15698 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
15700 unify_type_mismatch (explain_p
, args
[i
],
15701 TREE_VALUE (sarg
));
15710 if (excessive_deduction_depth
)
15712 if (deduction_depth
== 0)
15713 /* Reset once we're all the way out. */
15714 excessive_deduction_depth
= false;
15717 /* We can't free this if a pending_template entry or last_error_tinst_level
15718 is pointing at it. */
15719 if (last_pending_template
== old_last_pend
15720 && last_error_tinst_level
== old_error_tinst
)
15726 /* Adjust types before performing type deduction, as described in
15727 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15728 sections are symmetric. PARM is the type of a function parameter
15729 or the return type of the conversion function. ARG is the type of
15730 the argument passed to the call, or the type of the value
15731 initialized with the result of the conversion function.
15732 ARG_EXPR is the original argument expression, which may be null. */
15735 maybe_adjust_types_for_deduction (unification_kind_t strict
,
15749 /* Swap PARM and ARG throughout the remainder of this
15750 function; the handling is precisely symmetric since PARM
15751 will initialize ARG rather than vice versa. */
15759 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15760 too, but here handle it by stripping the reference from PARM
15761 rather than by adding it to ARG. */
15762 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15763 && TYPE_REF_IS_RVALUE (*parm
)
15764 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15765 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15766 && TREE_CODE (*arg
) == REFERENCE_TYPE
15767 && !TYPE_REF_IS_RVALUE (*arg
))
15768 *parm
= TREE_TYPE (*parm
);
15769 /* Nothing else to do in this case. */
15773 gcc_unreachable ();
15776 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
15778 /* [temp.deduct.call]
15780 If P is not a reference type:
15782 --If A is an array type, the pointer type produced by the
15783 array-to-pointer standard conversion (_conv.array_) is
15784 used in place of A for type deduction; otherwise,
15786 --If A is a function type, the pointer type produced by
15787 the function-to-pointer standard conversion
15788 (_conv.func_) is used in place of A for type deduction;
15791 --If A is a cv-qualified type, the top level
15792 cv-qualifiers of A's type are ignored for type
15794 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
15795 *arg
= build_pointer_type (TREE_TYPE (*arg
));
15796 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
15797 *arg
= build_pointer_type (*arg
);
15799 *arg
= TYPE_MAIN_VARIANT (*arg
);
15802 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15803 of the form T&&, where T is a template parameter, and the argument
15804 is an lvalue, T is deduced as A& */
15805 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15806 && TYPE_REF_IS_RVALUE (*parm
)
15807 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15808 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15809 && (arg_expr
? real_lvalue_p (arg_expr
)
15810 /* try_one_overload doesn't provide an arg_expr, but
15811 functions are always lvalues. */
15812 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
15813 *arg
= build_reference_type (*arg
);
15815 /* [temp.deduct.call]
15817 If P is a cv-qualified type, the top level cv-qualifiers
15818 of P's type are ignored for type deduction. If P is a
15819 reference type, the type referred to by P is used for
15821 *parm
= TYPE_MAIN_VARIANT (*parm
);
15822 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
15824 *parm
= TREE_TYPE (*parm
);
15825 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
15828 /* DR 322. For conversion deduction, remove a reference type on parm
15829 too (which has been swapped into ARG). */
15830 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
15831 *arg
= TREE_TYPE (*arg
);
15836 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15837 template which does contain any deducible template parameters; check if
15838 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15839 unify_one_argument. */
15842 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
15843 int flags
, bool explain_p
)
15848 type
= TREE_TYPE (arg
);
15852 if (same_type_p (parm
, type
))
15853 return unify_success (explain_p
);
15855 if (strict
== DEDUCE_CONV
)
15857 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
15858 explain_p
? tf_warning_or_error
: tf_none
))
15859 return unify_success (explain_p
);
15861 else if (strict
!= DEDUCE_EXACT
)
15863 if (can_convert_arg (parm
, type
,
15864 TYPE_P (arg
) ? NULL_TREE
: arg
,
15865 flags
, explain_p
? tf_warning_or_error
: tf_none
))
15866 return unify_success (explain_p
);
15869 if (strict
== DEDUCE_EXACT
)
15870 return unify_type_mismatch (explain_p
, parm
, arg
);
15872 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
15875 static bool uses_deducible_template_parms (tree type
);
15877 /* Returns true iff the expression EXPR is one from which a template
15878 argument can be deduced. In other words, if it's an undecorated
15879 use of a template non-type parameter. */
15882 deducible_expression (tree expr
)
15884 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
15887 /* Returns true iff the array domain DOMAIN uses a template parameter in a
15888 deducible way; that is, if it has a max value of <PARM> - 1. */
15891 deducible_array_bound (tree domain
)
15893 if (domain
== NULL_TREE
)
15896 tree max
= TYPE_MAX_VALUE (domain
);
15897 if (TREE_CODE (max
) != MINUS_EXPR
)
15900 return deducible_expression (TREE_OPERAND (max
, 0));
15903 /* Returns true iff the template arguments ARGS use a template parameter
15904 in a deducible way. */
15907 deducible_template_args (tree args
)
15909 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
15912 tree elt
= TREE_VEC_ELT (args
, i
);
15913 if (ARGUMENT_PACK_P (elt
))
15914 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
15917 if (PACK_EXPANSION_P (elt
))
15918 elt
= PACK_EXPANSION_PATTERN (elt
);
15919 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
15921 else if (TYPE_P (elt
))
15922 deducible
= uses_deducible_template_parms (elt
);
15924 deducible
= deducible_expression (elt
);
15932 /* Returns true iff TYPE contains any deducible references to template
15933 parameters, as per 14.8.2.5. */
15936 uses_deducible_template_parms (tree type
)
15938 if (PACK_EXPANSION_P (type
))
15939 type
= PACK_EXPANSION_PATTERN (type
);
15946 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
15947 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
15953 if (POINTER_TYPE_P (type
))
15954 return uses_deducible_template_parms (TREE_TYPE (type
));
15956 /* T[integer-constant ]
15958 if (TREE_CODE (type
) == ARRAY_TYPE
)
15959 return (uses_deducible_template_parms (TREE_TYPE (type
))
15960 || deducible_array_bound (TYPE_DOMAIN (type
)));
15972 if (TYPE_PTRMEM_P (type
))
15973 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
15974 || (uses_deducible_template_parms
15975 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
15977 /* template-name <T> (where template-name refers to a class template)
15978 template-name <i> (where template-name refers to a class template) */
15979 if (CLASS_TYPE_P (type
)
15980 && CLASSTYPE_TEMPLATE_INFO (type
)
15981 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
15982 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
15983 (CLASSTYPE_TI_ARGS (type
)));
15988 if (TREE_CODE (type
) == FUNCTION_TYPE
15989 || TREE_CODE (type
) == METHOD_TYPE
)
15991 if (uses_deducible_template_parms (TREE_TYPE (type
)))
15993 tree parm
= TYPE_ARG_TYPES (type
);
15994 if (TREE_CODE (type
) == METHOD_TYPE
)
15995 parm
= TREE_CHAIN (parm
);
15996 for (; parm
; parm
= TREE_CHAIN (parm
))
15997 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
16004 /* Subroutine of type_unification_real and unify_pack_expansion to
16005 handle unification of a single P/A pair. Parameters are as
16006 for those functions. */
16009 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
16010 int subr
, unification_kind_t strict
, int flags
,
16013 tree arg_expr
= NULL_TREE
;
16016 if (arg
== error_mark_node
|| parm
== error_mark_node
)
16017 return unify_invalid (explain_p
);
16018 if (arg
== unknown_type_node
)
16019 /* We can't deduce anything from this, but we might get all the
16020 template args from other function args. */
16021 return unify_success (explain_p
);
16023 /* Implicit conversions (Clause 4) will be performed on a function
16024 argument to convert it to the type of the corresponding function
16025 parameter if the parameter type contains no template-parameters that
16026 participate in template argument deduction. */
16027 if (TYPE_P (parm
) && !uses_template_parms (parm
))
16028 /* For function parameters that contain no template-parameters at all,
16029 we have historically checked for convertibility in order to shortcut
16030 consideration of this candidate. */
16031 return check_non_deducible_conversion (parm
, arg
, strict
, flags
,
16033 else if (strict
== DEDUCE_CALL
16034 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
16035 /* For function parameters with only non-deducible template parameters,
16037 return unify_success (explain_p
);
16042 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
16043 | UNIFY_ALLOW_MORE_CV_QUAL
16044 | UNIFY_ALLOW_DERIVED
);
16048 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
16052 arg_strict
= UNIFY_ALLOW_NONE
;
16056 gcc_unreachable ();
16059 /* We only do these transformations if this is the top-level
16060 parameter_type_list in a call or declaration matching; in other
16061 situations (nested function declarators, template argument lists) we
16062 won't be comparing a type to an expression, and we don't do any type
16068 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
16069 if (type_unknown_p (arg
))
16071 /* [temp.deduct.type] A template-argument can be
16072 deduced from a pointer to function or pointer
16073 to member function argument if the set of
16074 overloaded functions does not contain function
16075 templates and at most one of a set of
16076 overloaded functions provides a unique
16079 if (resolve_overloaded_unification
16080 (tparms
, targs
, parm
, arg
, strict
,
16081 arg_strict
, explain_p
))
16082 return unify_success (explain_p
);
16083 return unify_overload_resolution_failure (explain_p
, arg
);
16087 arg
= unlowered_expr_type (arg
);
16088 if (arg
== error_mark_node
)
16089 return unify_invalid (explain_p
);
16093 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
16096 gcc_assert ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
16097 == (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
));
16099 /* For deduction from an init-list we need the actual list. */
16100 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
16102 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
16105 /* Most parms like fn_type_unification.
16107 If SUBR is 1, we're being called recursively (to unify the
16108 arguments of a function or method parameter of a function
16111 CHECKS is a pointer to a vector of access checks encountered while
16112 substituting default template arguments. */
16115 type_unification_real (tree tparms
,
16119 unsigned int xnargs
,
16121 unification_kind_t strict
,
16123 vec
<deferred_access_check
, va_gc
> **checks
,
16128 int ntparms
= TREE_VEC_LENGTH (tparms
);
16129 int saw_undeduced
= 0;
16132 unsigned int nargs
;
16135 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
16136 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
16137 gcc_assert (ntparms
> 0);
16139 /* Reset the number of non-defaulted template arguments contained
16141 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
16149 while (parms
&& parms
!= void_list_node
16152 parm
= TREE_VALUE (parms
);
16154 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
16155 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
16156 /* For a function parameter pack that occurs at the end of the
16157 parameter-declaration-list, the type A of each remaining
16158 argument of the call is compared with the type P of the
16159 declarator-id of the function parameter pack. */
16162 parms
= TREE_CHAIN (parms
);
16164 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
16165 /* For a function parameter pack that does not occur at the
16166 end of the parameter-declaration-list, the type of the
16167 parameter pack is a non-deduced context. */
16173 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
16179 && parms
!= void_list_node
16180 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
16182 /* Unify the remaining arguments with the pack expansion type. */
16184 tree parmvec
= make_tree_vec (1);
16186 /* Allocate a TREE_VEC and copy in all of the arguments */
16187 argvec
= make_tree_vec (nargs
- ia
);
16188 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
16189 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
16191 /* Copy the parameter into parmvec. */
16192 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
16193 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
16194 /*subr=*/subr
, explain_p
))
16197 /* Advance to the end of the list of parameters. */
16198 parms
= TREE_CHAIN (parms
);
16201 /* Fail if we've reached the end of the parm list, and more args
16202 are present, and the parm list isn't variadic. */
16203 if (ia
< nargs
&& parms
== void_list_node
)
16204 return unify_too_many_arguments (explain_p
, nargs
, ia
);
16205 /* Fail if parms are left and they don't have default values. */
16206 if (parms
&& parms
!= void_list_node
16207 && TREE_PURPOSE (parms
) == NULL_TREE
)
16209 unsigned int count
= nargs
;
16211 while (p
&& p
!= void_list_node
)
16214 p
= TREE_CHAIN (p
);
16216 return unify_too_few_arguments (explain_p
, ia
, count
);
16221 tsubst_flags_t complain
= (explain_p
16222 ? tf_warning_or_error
16225 for (i
= 0; i
< ntparms
; i
++)
16227 tree targ
= TREE_VEC_ELT (targs
, i
);
16228 tree tparm
= TREE_VEC_ELT (tparms
, i
);
16230 /* Clear the "incomplete" flags on all argument packs now so that
16231 substituting them into later default arguments works. */
16232 if (targ
&& ARGUMENT_PACK_P (targ
))
16234 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
16235 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
16238 if (targ
|| tparm
== error_mark_node
)
16240 tparm
= TREE_VALUE (tparm
);
16242 /* If this is an undeduced nontype parameter that depends on
16243 a type parameter, try another pass; its type may have been
16244 deduced from a later argument than the one from which
16245 this parameter can be deduced. */
16246 if (TREE_CODE (tparm
) == PARM_DECL
16247 && uses_template_parms (TREE_TYPE (tparm
))
16248 && !saw_undeduced
++)
16251 /* Core issue #226 (C++0x) [temp.deduct]:
16253 If a template argument has not been deduced, its
16254 default template argument, if any, is used.
16256 When we are in C++98 mode, TREE_PURPOSE will either
16257 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16258 to explicitly check cxx_dialect here. */
16259 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
16261 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
16262 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
16263 reopen_deferring_access_checks (*checks
);
16264 location_t save_loc
= input_location
;
16266 input_location
= DECL_SOURCE_LOCATION (parm
);
16267 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
16268 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
16270 input_location
= save_loc
;
16271 *checks
= get_deferred_access_checks ();
16272 pop_deferring_access_checks ();
16273 if (arg
== error_mark_node
)
16277 TREE_VEC_ELT (targs
, i
) = arg
;
16278 /* The position of the first default template argument,
16279 is also the number of non-defaulted arguments in TARGS.
16281 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
16282 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
16287 /* If the type parameter is a parameter pack, then it will
16288 be deduced to an empty parameter pack. */
16289 if (template_parameter_pack_p (tparm
))
16293 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
16295 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
16296 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
16297 TREE_CONSTANT (arg
) = 1;
16300 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
16302 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
16304 TREE_VEC_ELT (targs
, i
) = arg
;
16308 return unify_parameter_deduction_failure (explain_p
, tparm
);
16311 #ifdef ENABLE_CHECKING
16312 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
16313 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
16316 return unify_success (explain_p
);
16319 /* Subroutine of type_unification_real. Args are like the variables
16320 at the call site. ARG is an overloaded function (or template-id);
16321 we try deducing template args from each of the overloads, and if
16322 only one succeeds, we go with that. Modifies TARGS and returns
16323 true on success. */
16326 resolve_overloaded_unification (tree tparms
,
16330 unification_kind_t strict
,
16334 tree tempargs
= copy_node (targs
);
16336 tree goodfn
= NULL_TREE
;
16339 if (TREE_CODE (arg
) == ADDR_EXPR
)
16341 arg
= TREE_OPERAND (arg
, 0);
16347 if (TREE_CODE (arg
) == COMPONENT_REF
)
16348 /* Handle `&x' where `x' is some static or non-static member
16350 arg
= TREE_OPERAND (arg
, 1);
16352 if (TREE_CODE (arg
) == OFFSET_REF
)
16353 arg
= TREE_OPERAND (arg
, 1);
16355 /* Strip baselink information. */
16356 if (BASELINK_P (arg
))
16357 arg
= BASELINK_FUNCTIONS (arg
);
16359 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
16361 /* If we got some explicit template args, we need to plug them into
16362 the affected templates before we try to unify, in case the
16363 explicit args will completely resolve the templates in question. */
16366 tree expl_subargs
= TREE_OPERAND (arg
, 1);
16367 arg
= TREE_OPERAND (arg
, 0);
16369 for (; arg
; arg
= OVL_NEXT (arg
))
16371 tree fn
= OVL_CURRENT (arg
);
16372 tree subargs
, elem
;
16374 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
16377 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
16378 expl_subargs
, NULL_TREE
, tf_none
,
16379 /*require_all_args=*/true,
16380 /*use_default_args=*/true);
16381 if (subargs
!= error_mark_node
16382 && !any_dependent_template_arguments_p (subargs
))
16384 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
16385 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
16386 elem
, strict
, sub_strict
, addr_p
, explain_p
)
16387 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
16396 /* If no templates (or more than one) are fully resolved by the
16397 explicit arguments, this template-id is a non-deduced context; it
16398 could still be OK if we deduce all template arguments for the
16399 enclosing call through other arguments. */
16403 else if (TREE_CODE (arg
) != OVERLOAD
16404 && TREE_CODE (arg
) != FUNCTION_DECL
)
16405 /* If ARG is, for example, "(0, &f)" then its type will be unknown
16406 -- but the deduction does not succeed because the expression is
16407 not just the function on its own. */
16410 for (; arg
; arg
= OVL_NEXT (arg
))
16411 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
16412 TREE_TYPE (OVL_CURRENT (arg
)),
16413 strict
, sub_strict
, addr_p
, explain_p
)
16414 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
16416 goodfn
= OVL_CURRENT (arg
);
16420 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16421 to function or pointer to member function argument if the set of
16422 overloaded functions does not contain function templates and at most
16423 one of a set of overloaded functions provides a unique match.
16425 So if we found multiple possibilities, we return success but don't
16426 deduce anything. */
16430 int i
= TREE_VEC_LENGTH (targs
);
16432 if (TREE_VEC_ELT (tempargs
, i
))
16433 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
16441 /* Core DR 115: In contexts where deduction is done and fails, or in
16442 contexts where deduction is not done, if a template argument list is
16443 specified and it, along with any default template arguments, identifies
16444 a single function template specialization, then the template-id is an
16445 lvalue for the function template specialization. */
16448 resolve_nondeduced_context (tree orig_expr
)
16450 tree expr
, offset
, baselink
;
16453 if (!type_unknown_p (orig_expr
))
16458 offset
= NULL_TREE
;
16459 baselink
= NULL_TREE
;
16461 if (TREE_CODE (expr
) == ADDR_EXPR
)
16463 expr
= TREE_OPERAND (expr
, 0);
16466 if (TREE_CODE (expr
) == OFFSET_REF
)
16469 expr
= TREE_OPERAND (expr
, 1);
16471 if (BASELINK_P (expr
))
16474 expr
= BASELINK_FUNCTIONS (expr
);
16477 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
16480 tree goodfn
= NULL_TREE
;
16482 /* If we got some explicit template args, we need to plug them into
16483 the affected templates before we try to unify, in case the
16484 explicit args will completely resolve the templates in question. */
16486 tree expl_subargs
= TREE_OPERAND (expr
, 1);
16487 tree arg
= TREE_OPERAND (expr
, 0);
16488 tree badfn
= NULL_TREE
;
16489 tree badargs
= NULL_TREE
;
16491 for (; arg
; arg
= OVL_NEXT (arg
))
16493 tree fn
= OVL_CURRENT (arg
);
16494 tree subargs
, elem
;
16496 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
16499 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
16500 expl_subargs
, NULL_TREE
, tf_none
,
16501 /*require_all_args=*/true,
16502 /*use_default_args=*/true);
16503 if (subargs
!= error_mark_node
16504 && !any_dependent_template_arguments_p (subargs
))
16506 elem
= instantiate_template (fn
, subargs
, tf_none
);
16507 if (elem
== error_mark_node
)
16512 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
16521 mark_used (goodfn
);
16524 expr
= build_baselink (BASELINK_BINFO (baselink
),
16525 BASELINK_ACCESS_BINFO (baselink
),
16526 expr
, BASELINK_OPTYPE (baselink
));
16530 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
16531 expr
= build_offset_ref (base
, expr
, addr
, tf_warning_or_error
);
16534 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
16537 else if (good
== 0 && badargs
)
16538 /* There were no good options and at least one bad one, so let the
16539 user know what the problem is. */
16540 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
16545 /* Subroutine of resolve_overloaded_unification; does deduction for a single
16546 overload. Fills TARGS with any deduced arguments, or error_mark_node if
16547 different overloads deduce different arguments for a given parm.
16548 ADDR_P is true if the expression for which deduction is being
16549 performed was of the form "& fn" rather than simply "fn".
16551 Returns 1 on success. */
16554 try_one_overload (tree tparms
,
16559 unification_kind_t strict
,
16568 if (arg
== error_mark_node
)
16571 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16572 to function or pointer to member function argument if the set of
16573 overloaded functions does not contain function templates and at most
16574 one of a set of overloaded functions provides a unique match.
16576 So if this is a template, just return success. */
16578 if (uses_template_parms (arg
))
16581 if (TREE_CODE (arg
) == METHOD_TYPE
)
16582 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
16584 arg
= build_pointer_type (arg
);
16586 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
16588 /* We don't copy orig_targs for this because if we have already deduced
16589 some template args from previous args, unify would complain when we
16590 try to deduce a template parameter for the same argument, even though
16591 there isn't really a conflict. */
16592 nargs
= TREE_VEC_LENGTH (targs
);
16593 tempargs
= make_tree_vec (nargs
);
16595 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
16598 /* First make sure we didn't deduce anything that conflicts with
16599 explicitly specified args. */
16600 for (i
= nargs
; i
--; )
16602 tree elt
= TREE_VEC_ELT (tempargs
, i
);
16603 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
16607 else if (uses_template_parms (elt
))
16608 /* Since we're unifying against ourselves, we will fill in
16609 template args used in the function parm list with our own
16610 template parms. Discard them. */
16611 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
16612 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
16616 for (i
= nargs
; i
--; )
16618 tree elt
= TREE_VEC_ELT (tempargs
, i
);
16621 TREE_VEC_ELT (targs
, i
) = elt
;
16627 /* PARM is a template class (perhaps with unbound template
16628 parameters). ARG is a fully instantiated type. If ARG can be
16629 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
16630 TARGS are as for unify. */
16633 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
16636 tree copy_of_targs
;
16638 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
16639 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
16640 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
16643 /* We need to make a new template argument vector for the call to
16644 unify. If we used TARGS, we'd clutter it up with the result of
16645 the attempted unification, even if this class didn't work out.
16646 We also don't want to commit ourselves to all the unifications
16647 we've already done, since unification is supposed to be done on
16648 an argument-by-argument basis. In other words, consider the
16649 following pathological case:
16651 template <int I, int J, int K>
16654 template <int I, int J>
16655 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
16657 template <int I, int J, int K>
16658 void f(S<I, J, K>, S<I, I, I>);
16667 Now, by the time we consider the unification involving `s2', we
16668 already know that we must have `f<0, 0, 0>'. But, even though
16669 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16670 because there are two ways to unify base classes of S<0, 1, 2>
16671 with S<I, I, I>. If we kept the already deduced knowledge, we
16672 would reject the possibility I=1. */
16673 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
16675 /* If unification failed, we're done. */
16676 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
16677 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
16683 /* Given a template type PARM and a class type ARG, find the unique
16684 base type in ARG that is an instance of PARM. We do not examine
16685 ARG itself; only its base-classes. If there is not exactly one
16686 appropriate base class, return NULL_TREE. PARM may be the type of
16687 a partial specialization, as well as a plain template type. Used
16690 static enum template_base_result
16691 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
16692 bool explain_p
, tree
*result
)
16694 tree rval
= NULL_TREE
;
16697 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
16699 binfo
= TYPE_BINFO (complete_type (arg
));
16702 /* The type could not be completed. */
16703 *result
= NULL_TREE
;
16704 return tbr_incomplete_type
;
16707 /* Walk in inheritance graph order. The search order is not
16708 important, and this avoids multiple walks of virtual bases. */
16709 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
16711 tree r
= try_class_unification (tparms
, targs
, parm
,
16712 BINFO_TYPE (binfo
), explain_p
);
16716 /* If there is more than one satisfactory baseclass, then:
16720 If they yield more than one possible deduced A, the type
16724 if (rval
&& !same_type_p (r
, rval
))
16726 *result
= NULL_TREE
;
16727 return tbr_ambiguous_baseclass
;
16735 return tbr_success
;
16738 /* Returns the level of DECL, which declares a template parameter. */
16741 template_decl_level (tree decl
)
16743 switch (TREE_CODE (decl
))
16746 case TEMPLATE_DECL
:
16747 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
16750 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
16753 gcc_unreachable ();
16758 /* Decide whether ARG can be unified with PARM, considering only the
16759 cv-qualifiers of each type, given STRICT as documented for unify.
16760 Returns nonzero iff the unification is OK on that basis. */
16763 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
16765 int arg_quals
= cp_type_quals (arg
);
16766 int parm_quals
= cp_type_quals (parm
);
16768 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16769 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
16771 /* Although a CVR qualifier is ignored when being applied to a
16772 substituted template parameter ([8.3.2]/1 for example), that
16773 does not allow us to unify "const T" with "int&" because both
16774 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16775 It is ok when we're allowing additional CV qualifiers
16776 at the outer level [14.8.2.1]/3,1st bullet. */
16777 if ((TREE_CODE (arg
) == REFERENCE_TYPE
16778 || TREE_CODE (arg
) == FUNCTION_TYPE
16779 || TREE_CODE (arg
) == METHOD_TYPE
)
16780 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
16783 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
16784 && (parm_quals
& TYPE_QUAL_RESTRICT
))
16788 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
16789 && (arg_quals
& parm_quals
) != parm_quals
)
16792 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
16793 && (parm_quals
& arg_quals
) != arg_quals
)
16799 /* Determines the LEVEL and INDEX for the template parameter PARM. */
16801 template_parm_level_and_index (tree parm
, int* level
, int* index
)
16803 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16804 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16805 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16807 *index
= TEMPLATE_TYPE_IDX (parm
);
16808 *level
= TEMPLATE_TYPE_LEVEL (parm
);
16812 *index
= TEMPLATE_PARM_IDX (parm
);
16813 *level
= TEMPLATE_PARM_LEVEL (parm
);
16817 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
16819 if (unify (TP, TA, P, A, S, EP)) \
16823 /* Unifies the remaining arguments in PACKED_ARGS with the pack
16824 expansion at the end of PACKED_PARMS. Returns 0 if the type
16825 deduction succeeds, 1 otherwise. STRICT is the same as in
16826 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
16827 call argument list. We'll need to adjust the arguments to make them
16828 types. SUBR tells us if this is from a recursive call to
16829 type_unification_real, or for comparing two template argument
16833 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
16834 tree packed_args
, unification_kind_t strict
,
16835 bool subr
, bool explain_p
)
16838 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
16839 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
16840 tree pack
, packs
= NULL_TREE
;
16841 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
16842 int len
= TREE_VEC_LENGTH (packed_args
);
16844 /* Determine the parameter packs we will be deducing from the
16845 pattern, and record their current deductions. */
16846 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
16847 pack
; pack
= TREE_CHAIN (pack
))
16849 tree parm_pack
= TREE_VALUE (pack
);
16852 /* Determine the index and level of this parameter pack. */
16853 template_parm_level_and_index (parm_pack
, &level
, &idx
);
16855 /* Keep track of the parameter packs and their corresponding
16857 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
16858 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
16861 /* Loop through all of the arguments that have not yet been
16862 unified and unify each with the pattern. */
16863 for (i
= start
; i
< len
; i
++)
16866 bool any_explicit
= false;
16867 tree arg
= TREE_VEC_ELT (packed_args
, i
);
16869 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16870 or the element of its argument pack at the current index if
16871 this argument was explicitly specified. */
16872 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16876 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16879 if (TREE_VALUE (pack
)
16880 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
16881 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
16883 any_explicit
= true;
16884 arg
= TREE_VEC_ELT (pargs
, i
- start
);
16886 TMPL_ARG (targs
, level
, idx
) = arg
;
16889 /* If we had explicit template arguments, substitute them into the
16890 pattern before deduction. */
16893 /* Some arguments might still be unspecified or dependent. */
16895 ++processing_template_decl
;
16896 dependent
= any_dependent_template_arguments_p (targs
);
16898 --processing_template_decl
;
16899 parm
= tsubst (pattern
, targs
,
16900 explain_p
? tf_warning_or_error
: tf_none
,
16903 --processing_template_decl
;
16904 if (parm
== error_mark_node
)
16910 /* Unify the pattern with the current argument. */
16911 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
16912 LOOKUP_IMPLICIT
, explain_p
))
16915 /* For each parameter pack, collect the deduced value. */
16916 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16919 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16921 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
16922 TMPL_ARG (targs
, level
, idx
);
16926 /* Verify that the results of unification with the parameter packs
16927 produce results consistent with what we've seen before, and make
16928 the deduced argument packs available. */
16929 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16931 tree old_pack
= TREE_VALUE (pack
);
16932 tree new_args
= TREE_TYPE (pack
);
16933 int i
, len
= TREE_VEC_LENGTH (new_args
);
16935 bool nondeduced_p
= false;
16937 /* By default keep the original deduced argument pack.
16938 If necessary, more specific code is going to update the
16939 resulting deduced argument later down in this function. */
16940 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16941 TMPL_ARG (targs
, level
, idx
) = old_pack
;
16943 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16944 actually deduce anything. */
16945 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
16946 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
16947 nondeduced_p
= true;
16951 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
16953 /* If we had fewer function args than explicit template args,
16954 just use the explicits. */
16955 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16956 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
16957 if (len
< explicit_len
)
16958 new_args
= explicit_args
;
16964 /* Build the deduced *_ARGUMENT_PACK. */
16965 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
16967 result
= make_node (NONTYPE_ARGUMENT_PACK
);
16968 TREE_TYPE (result
) =
16969 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
16970 TREE_CONSTANT (result
) = 1;
16973 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
16975 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
16977 /* Note the deduced argument packs for this parameter
16979 TMPL_ARG (targs
, level
, idx
) = result
;
16981 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
16982 && (ARGUMENT_PACK_ARGS (old_pack
)
16983 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
16985 /* We only had the explicitly-provided arguments before, but
16986 now we have a complete set of arguments. */
16987 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16989 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
16990 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
16991 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
16995 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
16996 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
16998 if (!comp_template_args_with_info (old_args
, new_args
,
16999 &bad_old_arg
, &bad_new_arg
))
17000 /* Inconsistent unification of this parameter pack. */
17001 return unify_parameter_pack_inconsistent (explain_p
,
17007 return unify_success (explain_p
);
17010 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17011 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17012 parameters and return value are as for unify. */
17015 unify_array_domain (tree tparms
, tree targs
,
17016 tree parm_dom
, tree arg_dom
,
17024 /* Our representation of array types uses "N - 1" as the
17025 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17026 not an integer constant. We cannot unify arbitrarily
17027 complex expressions, so we eliminate the MINUS_EXPRs
17029 parm_max
= TYPE_MAX_VALUE (parm_dom
);
17030 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
17033 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
17034 parm_max
= TREE_OPERAND (parm_max
, 0);
17036 arg_max
= TYPE_MAX_VALUE (arg_dom
);
17037 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
17040 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17041 trying to unify the type of a variable with the type
17042 of a template parameter. For example:
17044 template <unsigned int N>
17045 void f (char (&) [N]);
17052 Here, the type of the ARG will be "int [g(i)]", and
17053 may be a SAVE_EXPR, etc. */
17054 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
17055 return unify_vla_arg (explain_p
, arg_dom
);
17056 arg_max
= TREE_OPERAND (arg_max
, 0);
17059 /* If only one of the bounds used a MINUS_EXPR, compensate
17060 by adding one to the other bound. */
17061 if (parm_cst
&& !arg_cst
)
17062 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
17066 else if (arg_cst
&& !parm_cst
)
17067 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
17072 return unify (tparms
, targs
, parm_max
, arg_max
,
17073 UNIFY_ALLOW_INTEGER
, explain_p
);
17076 /* Deduce the value of template parameters. TPARMS is the (innermost)
17077 set of template parameters to a template. TARGS is the bindings
17078 for those template parameters, as determined thus far; TARGS may
17079 include template arguments for outer levels of template parameters
17080 as well. PARM is a parameter to a template function, or a
17081 subcomponent of that parameter; ARG is the corresponding argument.
17082 This function attempts to match PARM with ARG in a manner
17083 consistent with the existing assignments in TARGS. If more values
17084 are deduced, then TARGS is updated.
17086 Returns 0 if the type deduction succeeds, 1 otherwise. The
17087 parameter STRICT is a bitwise or of the following flags:
17090 Require an exact match between PARM and ARG.
17091 UNIFY_ALLOW_MORE_CV_QUAL:
17092 Allow the deduced ARG to be more cv-qualified (by qualification
17093 conversion) than ARG.
17094 UNIFY_ALLOW_LESS_CV_QUAL:
17095 Allow the deduced ARG to be less cv-qualified than ARG.
17096 UNIFY_ALLOW_DERIVED:
17097 Allow the deduced ARG to be a template base class of ARG,
17098 or a pointer to a template base class of the type pointed to by
17100 UNIFY_ALLOW_INTEGER:
17101 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17102 case for more information.
17103 UNIFY_ALLOW_OUTER_LEVEL:
17104 This is the outermost level of a deduction. Used to determine validity
17105 of qualification conversions. A valid qualification conversion must
17106 have const qualified pointers leading up to the inner type which
17107 requires additional CV quals, except at the outer level, where const
17108 is not required [conv.qual]. It would be normal to set this flag in
17109 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17110 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17111 This is the outermost level of a deduction, and PARM can be more CV
17112 qualified at this point.
17113 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17114 This is the outermost level of a deduction, and PARM can be less CV
17115 qualified at this point. */
17118 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
17124 int strict_in
= strict
;
17126 /* I don't think this will do the right thing with respect to types.
17127 But the only case I've seen it in so far has been array bounds, where
17128 signedness is the only information lost, and I think that will be
17130 while (TREE_CODE (parm
) == NOP_EXPR
)
17131 parm
= TREE_OPERAND (parm
, 0);
17133 if (arg
== error_mark_node
)
17134 return unify_invalid (explain_p
);
17135 if (arg
== unknown_type_node
17136 || arg
== init_list_type_node
)
17137 /* We can't deduce anything from this, but we might get all the
17138 template args from other function args. */
17139 return unify_success (explain_p
);
17141 /* If PARM uses template parameters, then we can't bail out here,
17142 even if ARG == PARM, since we won't record unifications for the
17143 template parameters. We might need them if we're trying to
17144 figure out which of two things is more specialized. */
17145 if (arg
== parm
&& !uses_template_parms (parm
))
17146 return unify_success (explain_p
);
17148 /* Handle init lists early, so the rest of the function can assume
17149 we're dealing with a type. */
17150 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
17154 tree orig_parm
= parm
;
17156 /* Replace T with std::initializer_list<T> for deduction. */
17157 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17158 && flag_deduce_init_list
)
17159 parm
= listify (parm
);
17161 if (!is_std_init_list (parm
)
17162 && TREE_CODE (parm
) != ARRAY_TYPE
)
17163 /* We can only deduce from an initializer list argument if the
17164 parameter is std::initializer_list or an array; otherwise this
17165 is a non-deduced context. */
17166 return unify_success (explain_p
);
17168 if (TREE_CODE (parm
) == ARRAY_TYPE
)
17169 elttype
= TREE_TYPE (parm
);
17171 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
17173 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
17175 int elt_strict
= strict
;
17177 if (elt
== error_mark_node
)
17178 return unify_invalid (explain_p
);
17180 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
17182 tree type
= TREE_TYPE (elt
);
17183 /* It should only be possible to get here for a call. */
17184 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
17185 elt_strict
|= maybe_adjust_types_for_deduction
17186 (DEDUCE_CALL
, &elttype
, &type
, elt
);
17190 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
17194 if (TREE_CODE (parm
) == ARRAY_TYPE
)
17196 /* Also deduce from the length of the initializer list. */
17197 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
17198 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
17199 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
17203 /* If the std::initializer_list<T> deduction worked, replace the
17204 deduced A with std::initializer_list<A>. */
17205 if (orig_parm
!= parm
)
17207 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
17208 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17209 targ
= listify (targ
);
17210 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
17212 return unify_success (explain_p
);
17215 /* Immediately reject some pairs that won't unify because of
17216 cv-qualification mismatches. */
17217 if (TREE_CODE (arg
) == TREE_CODE (parm
)
17219 /* It is the elements of the array which hold the cv quals of an array
17220 type, and the elements might be template type parms. We'll check
17221 when we recurse. */
17222 && TREE_CODE (arg
) != ARRAY_TYPE
17223 /* We check the cv-qualifiers when unifying with template type
17224 parameters below. We want to allow ARG `const T' to unify with
17225 PARM `T' for example, when computing which of two templates
17226 is more specialized, for example. */
17227 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
17228 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
17229 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17231 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
17232 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
17233 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
17234 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
17235 strict
&= ~UNIFY_ALLOW_DERIVED
;
17236 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
17237 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
17239 switch (TREE_CODE (parm
))
17241 case TYPENAME_TYPE
:
17243 case UNBOUND_CLASS_TEMPLATE
:
17244 /* In a type which contains a nested-name-specifier, template
17245 argument values cannot be deduced for template parameters used
17246 within the nested-name-specifier. */
17247 return unify_success (explain_p
);
17249 case TEMPLATE_TYPE_PARM
:
17250 case TEMPLATE_TEMPLATE_PARM
:
17251 case BOUND_TEMPLATE_TEMPLATE_PARM
:
17252 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
17253 if (tparm
== error_mark_node
)
17254 return unify_invalid (explain_p
);
17256 if (TEMPLATE_TYPE_LEVEL (parm
)
17257 != template_decl_level (tparm
))
17258 /* The PARM is not one we're trying to unify. Just check
17259 to see if it matches ARG. */
17261 if (TREE_CODE (arg
) == TREE_CODE (parm
)
17262 && (is_auto (parm
) ? is_auto (arg
)
17263 : same_type_p (parm
, arg
)))
17264 return unify_success (explain_p
);
17266 return unify_type_mismatch (explain_p
, parm
, arg
);
17268 idx
= TEMPLATE_TYPE_IDX (parm
);
17269 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17270 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
17271 if (tparm
== error_mark_node
)
17272 return unify_invalid (explain_p
);
17274 /* Check for mixed types and values. */
17275 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17276 && TREE_CODE (tparm
) != TYPE_DECL
)
17277 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
17278 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
17279 gcc_unreachable ();
17281 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17283 /* ARG must be constructed from a template class or a template
17284 template parameter. */
17285 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
17286 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
17287 return unify_template_deduction_failure (explain_p
, parm
, arg
);
17290 tree parmvec
= TYPE_TI_ARGS (parm
);
17291 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
17292 tree full_argvec
= add_to_template_args (targs
, argvec
);
17294 = DECL_INNERMOST_TEMPLATE_PARMS
17295 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
17297 int parm_variadic_p
= 0;
17299 /* The resolution to DR150 makes clear that default
17300 arguments for an N-argument may not be used to bind T
17301 to a template template parameter with fewer than N
17302 parameters. It is not safe to permit the binding of
17303 default arguments as an extension, as that may change
17304 the meaning of a conforming program. Consider:
17306 struct Dense { static const unsigned int dim = 1; };
17308 template <template <typename> class View,
17310 void operator+(float, View<Block> const&);
17312 template <typename Block,
17313 unsigned int Dim = Block::dim>
17314 struct Lvalue_proxy { operator float() const; };
17318 Lvalue_proxy<Dense> p;
17323 Here, if Lvalue_proxy is permitted to bind to View, then
17324 the global operator+ will be used; if they are not, the
17325 Lvalue_proxy will be converted to float. */
17326 if (coerce_template_parms (parm_parms
,
17328 TYPE_TI_TEMPLATE (parm
),
17330 ? tf_warning_or_error
17332 /*require_all_args=*/true,
17333 /*use_default_args=*/false)
17334 == error_mark_node
)
17337 /* Deduce arguments T, i from TT<T> or TT<i>.
17338 We check each element of PARMVEC and ARGVEC individually
17339 rather than the whole TREE_VEC since they can have
17340 different number of elements. */
17342 parmvec
= expand_template_argument_pack (parmvec
);
17343 argvec
= expand_template_argument_pack (argvec
);
17345 len
= TREE_VEC_LENGTH (parmvec
);
17347 /* Check if the parameters end in a pack, making them
17350 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
17351 parm_variadic_p
= 1;
17353 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17354 /* If the template argument list of P contains a pack
17355 expansion that is not the last template argument, the
17356 entire template argument list is a non-deduced
17358 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
17359 return unify_success (explain_p
);
17361 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
17362 return unify_too_few_arguments (explain_p
,
17363 TREE_VEC_LENGTH (argvec
), len
);
17365 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17367 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
17368 TREE_VEC_ELT (parmvec
, i
),
17369 TREE_VEC_ELT (argvec
, i
),
17370 UNIFY_ALLOW_NONE
, explain_p
);
17373 if (parm_variadic_p
17374 && unify_pack_expansion (tparms
, targs
,
17377 /*subr=*/true, explain_p
))
17380 arg
= TYPE_TI_TEMPLATE (arg
);
17382 /* Fall through to deduce template name. */
17385 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
17386 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17388 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
17390 /* Simple cases: Value already set, does match or doesn't. */
17391 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
17392 return unify_success (explain_p
);
17394 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
17398 /* If PARM is `const T' and ARG is only `int', we don't have
17399 a match unless we are allowing additional qualification.
17400 If ARG is `const int' and PARM is just `T' that's OK;
17401 that binds `const int' to `T'. */
17402 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
17404 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17406 /* Consider the case where ARG is `const volatile int' and
17407 PARM is `const T'. Then, T should be `volatile int'. */
17408 arg
= cp_build_qualified_type_real
17409 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
17410 if (arg
== error_mark_node
)
17411 return unify_invalid (explain_p
);
17413 /* Simple cases: Value already set, does match or doesn't. */
17414 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
17415 return unify_success (explain_p
);
17417 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
17419 /* Make sure that ARG is not a variable-sized array. (Note
17420 that were talking about variable-sized arrays (like
17421 `int[n]'), rather than arrays of unknown size (like
17422 `int[]').) We'll get very confused by such a type since
17423 the bound of the array is not constant, and therefore
17424 not mangleable. Besides, such types are not allowed in
17425 ISO C++, so we can do as we please here. We do allow
17426 them for 'auto' deduction, since that isn't ABI-exposed. */
17427 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
17428 return unify_vla_arg (explain_p
, arg
);
17430 /* Strip typedefs as in convert_template_argument. */
17431 arg
= canonicalize_type_argument (arg
, tf_none
);
17434 /* If ARG is a parameter pack or an expansion, we cannot unify
17435 against it unless PARM is also a parameter pack. */
17436 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
17437 && !template_parameter_pack_p (parm
))
17438 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
17440 /* If the argument deduction results is a METHOD_TYPE,
17441 then there is a problem.
17442 METHOD_TYPE doesn't map to any real C++ type the result of
17443 the deduction can not be of that type. */
17444 if (TREE_CODE (arg
) == METHOD_TYPE
)
17445 return unify_method_type_error (explain_p
, arg
);
17447 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
17448 return unify_success (explain_p
);
17450 case TEMPLATE_PARM_INDEX
:
17451 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
17452 if (tparm
== error_mark_node
)
17453 return unify_invalid (explain_p
);
17455 if (TEMPLATE_PARM_LEVEL (parm
)
17456 != template_decl_level (tparm
))
17458 /* The PARM is not one we're trying to unify. Just check
17459 to see if it matches ARG. */
17460 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
17461 && cp_tree_equal (parm
, arg
));
17463 unify_expression_unequal (explain_p
, parm
, arg
);
17467 idx
= TEMPLATE_PARM_IDX (parm
);
17468 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17472 int x
= !cp_tree_equal (targ
, arg
);
17474 unify_inconsistency (explain_p
, parm
, targ
, arg
);
17478 /* [temp.deduct.type] If, in the declaration of a function template
17479 with a non-type template-parameter, the non-type
17480 template-parameter is used in an expression in the function
17481 parameter-list and, if the corresponding template-argument is
17482 deduced, the template-argument type shall match the type of the
17483 template-parameter exactly, except that a template-argument
17484 deduced from an array bound may be of any integral type.
17485 The non-type parameter might use already deduced type parameters. */
17486 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
17487 if (!TREE_TYPE (arg
))
17488 /* Template-parameter dependent expression. Just accept it for now.
17489 It will later be processed in convert_template_argument. */
17491 else if (same_type_p (TREE_TYPE (arg
), tparm
))
17493 else if ((strict
& UNIFY_ALLOW_INTEGER
)
17494 && CP_INTEGRAL_TYPE_P (tparm
))
17495 /* Convert the ARG to the type of PARM; the deduced non-type
17496 template argument must exactly match the types of the
17497 corresponding parameter. */
17498 arg
= fold (build_nop (tparm
, arg
));
17499 else if (uses_template_parms (tparm
))
17500 /* We haven't deduced the type of this parameter yet. Try again
17502 return unify_success (explain_p
);
17504 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
17506 /* If ARG is a parameter pack or an expansion, we cannot unify
17507 against it unless PARM is also a parameter pack. */
17508 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
17509 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
17510 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
17512 arg
= strip_typedefs_expr (arg
);
17513 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
17514 return unify_success (explain_p
);
17518 /* A pointer-to-member constant can be unified only with
17519 another constant. */
17520 if (TREE_CODE (arg
) != PTRMEM_CST
)
17521 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
17523 /* Just unify the class member. It would be useless (and possibly
17524 wrong, depending on the strict flags) to unify also
17525 PTRMEM_CST_CLASS, because we want to be sure that both parm and
17526 arg refer to the same variable, even if through different
17527 classes. For instance:
17529 struct A { int x; };
17532 Unification of &A::x and &B::x must succeed. */
17533 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
17534 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
17539 if (!TYPE_PTR_P (arg
))
17540 return unify_type_mismatch (explain_p
, parm
, arg
);
17542 /* [temp.deduct.call]
17544 A can be another pointer or pointer to member type that can
17545 be converted to the deduced A via a qualification
17546 conversion (_conv.qual_).
17548 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
17549 This will allow for additional cv-qualification of the
17550 pointed-to types if appropriate. */
17552 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
17553 /* The derived-to-base conversion only persists through one
17554 level of pointers. */
17555 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
17557 return unify (tparms
, targs
, TREE_TYPE (parm
),
17558 TREE_TYPE (arg
), strict
, explain_p
);
17561 case REFERENCE_TYPE
:
17562 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
17563 return unify_type_mismatch (explain_p
, parm
, arg
);
17564 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17565 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
17568 if (TREE_CODE (arg
) != ARRAY_TYPE
)
17569 return unify_type_mismatch (explain_p
, parm
, arg
);
17570 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
17571 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
17572 return unify_type_mismatch (explain_p
, parm
, arg
);
17573 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17574 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
17575 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
17576 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
17577 TYPE_DOMAIN (arg
), explain_p
);
17578 return unify_success (explain_p
);
17585 case ENUMERAL_TYPE
:
17588 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17589 return unify_type_mismatch (explain_p
, parm
, arg
);
17591 /* We have already checked cv-qualification at the top of the
17593 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
17594 return unify_type_mismatch (explain_p
, parm
, arg
);
17596 /* As far as unification is concerned, this wins. Later checks
17597 will invalidate it if necessary. */
17598 return unify_success (explain_p
);
17600 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
17601 /* Type INTEGER_CST can come from ordinary constant template args. */
17603 while (TREE_CODE (arg
) == NOP_EXPR
)
17604 arg
= TREE_OPERAND (arg
, 0);
17606 if (TREE_CODE (arg
) != INTEGER_CST
)
17607 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17608 return (tree_int_cst_equal (parm
, arg
)
17609 ? unify_success (explain_p
)
17610 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
17614 int i
, len
, argslen
;
17615 int parm_variadic_p
= 0;
17617 if (TREE_CODE (arg
) != TREE_VEC
)
17618 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17620 len
= TREE_VEC_LENGTH (parm
);
17621 argslen
= TREE_VEC_LENGTH (arg
);
17623 /* Check for pack expansions in the parameters. */
17624 for (i
= 0; i
< len
; ++i
)
17626 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
17629 /* We can unify against something with a trailing
17631 parm_variadic_p
= 1;
17633 /* [temp.deduct.type]/9: If the template argument list of
17634 P contains a pack expansion that is not the last
17635 template argument, the entire template argument list
17636 is a non-deduced context. */
17637 return unify_success (explain_p
);
17641 /* If we don't have enough arguments to satisfy the parameters
17642 (not counting the pack expression at the end), or we have
17643 too many arguments for a parameter list that doesn't end in
17644 a pack expression, we can't unify. */
17645 if (parm_variadic_p
17646 ? argslen
< len
- parm_variadic_p
17648 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
17650 /* Unify all of the parameters that precede the (optional)
17651 pack expression. */
17652 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17654 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
17655 TREE_VEC_ELT (parm
, i
),
17656 TREE_VEC_ELT (arg
, i
),
17657 UNIFY_ALLOW_NONE
, explain_p
);
17659 if (parm_variadic_p
)
17660 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
17662 /*subr=*/true, explain_p
);
17663 return unify_success (explain_p
);
17668 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17669 return unify_type_mismatch (explain_p
, parm
, arg
);
17671 if (TYPE_PTRMEMFUNC_P (parm
))
17673 if (!TYPE_PTRMEMFUNC_P (arg
))
17674 return unify_type_mismatch (explain_p
, parm
, arg
);
17676 return unify (tparms
, targs
,
17677 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
17678 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
17679 strict
, explain_p
);
17682 if (CLASSTYPE_TEMPLATE_INFO (parm
))
17684 tree t
= NULL_TREE
;
17686 if (strict_in
& UNIFY_ALLOW_DERIVED
)
17688 /* First, we try to unify the PARM and ARG directly. */
17689 t
= try_class_unification (tparms
, targs
,
17690 parm
, arg
, explain_p
);
17694 /* Fallback to the special case allowed in
17695 [temp.deduct.call]:
17697 If P is a class, and P has the form
17698 template-id, then A can be a derived class of
17699 the deduced A. Likewise, if P is a pointer to
17700 a class of the form template-id, A can be a
17701 pointer to a derived class pointed to by the
17703 enum template_base_result r
;
17704 r
= get_template_base (tparms
, targs
, parm
, arg
,
17708 return unify_no_common_base (explain_p
, r
, parm
, arg
);
17711 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
17712 && (CLASSTYPE_TI_TEMPLATE (parm
)
17713 == CLASSTYPE_TI_TEMPLATE (arg
)))
17714 /* Perhaps PARM is something like S<U> and ARG is S<int>.
17715 Then, we should unify `int' and `U'. */
17718 /* There's no chance of unification succeeding. */
17719 return unify_type_mismatch (explain_p
, parm
, arg
);
17721 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
17722 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
17724 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
17725 return unify_type_mismatch (explain_p
, parm
, arg
);
17726 return unify_success (explain_p
);
17729 case FUNCTION_TYPE
:
17731 unsigned int nargs
;
17736 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17737 return unify_type_mismatch (explain_p
, parm
, arg
);
17739 /* CV qualifications for methods can never be deduced, they must
17740 match exactly. We need to check them explicitly here,
17741 because type_unification_real treats them as any other
17742 cv-qualified parameter. */
17743 if (TREE_CODE (parm
) == METHOD_TYPE
17744 && (!check_cv_quals_for_unify
17746 class_of_this_parm (arg
),
17747 class_of_this_parm (parm
))))
17748 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17750 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
17751 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
17753 nargs
= list_length (TYPE_ARG_TYPES (arg
));
17754 args
= XALLOCAVEC (tree
, nargs
);
17755 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
17756 a
!= NULL_TREE
&& a
!= void_list_node
;
17757 a
= TREE_CHAIN (a
), ++i
)
17758 args
[i
] = TREE_VALUE (a
);
17761 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
17762 args
, nargs
, 1, DEDUCE_EXACT
,
17763 LOOKUP_NORMAL
, NULL
, explain_p
);
17767 /* Unify a pointer to member with a pointer to member function, which
17768 deduces the type of the member as a function type. */
17769 if (TYPE_PTRMEMFUNC_P (arg
))
17771 /* Check top-level cv qualifiers */
17772 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
17773 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17775 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
17776 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
17777 UNIFY_ALLOW_NONE
, explain_p
);
17779 /* Determine the type of the function we are unifying against. */
17780 tree fntype
= static_fn_type (arg
);
17782 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
17785 if (TREE_CODE (arg
) != OFFSET_TYPE
)
17786 return unify_type_mismatch (explain_p
, parm
, arg
);
17787 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
17788 TYPE_OFFSET_BASETYPE (arg
),
17789 UNIFY_ALLOW_NONE
, explain_p
);
17790 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17791 strict
, explain_p
);
17794 if (DECL_TEMPLATE_PARM_P (parm
))
17795 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
17796 if (arg
!= integral_constant_value (parm
))
17797 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17798 return unify_success (explain_p
);
17801 case TEMPLATE_DECL
:
17802 /* Matched cases are handled by the ARG == PARM test above. */
17803 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17806 /* A non-type template parameter that is a variable should be a
17807 an integral constant, in which case, it whould have been
17808 folded into its (constant) value. So we should not be getting
17809 a variable here. */
17810 gcc_unreachable ();
17812 case TYPE_ARGUMENT_PACK
:
17813 case NONTYPE_ARGUMENT_PACK
:
17814 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
17815 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
17818 case DECLTYPE_TYPE
:
17819 case UNDERLYING_TYPE
:
17820 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
17821 or UNDERLYING_TYPE nodes. */
17822 return unify_success (explain_p
);
17825 /* Unification fails if we hit an error node. */
17826 return unify_invalid (explain_p
);
17829 /* An unresolved overload is a nondeduced context. */
17830 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
17831 return unify_success (explain_p
);
17832 gcc_assert (EXPR_P (parm
));
17834 /* We must be looking at an expression. This can happen with
17838 void foo(S<I>, S<I + 2>);
17840 This is a "nondeduced context":
17844 The nondeduced contexts are:
17846 --A type that is a template-id in which one or more of
17847 the template-arguments is an expression that references
17848 a template-parameter.
17850 In these cases, we assume deduction succeeded, but don't
17851 actually infer any unifications. */
17853 if (!uses_template_parms (parm
)
17854 && !template_args_equal (parm
, arg
))
17855 return unify_expression_unequal (explain_p
, parm
, arg
);
17857 return unify_success (explain_p
);
17860 #undef RECUR_AND_CHECK_FAILURE
17862 /* Note that DECL can be defined in this translation unit, if
17866 mark_definable (tree decl
)
17869 DECL_NOT_REALLY_EXTERN (decl
) = 1;
17870 FOR_EACH_CLONE (clone
, decl
)
17871 DECL_NOT_REALLY_EXTERN (clone
) = 1;
17874 /* Called if RESULT is explicitly instantiated, or is a member of an
17875 explicitly instantiated class. */
17878 mark_decl_instantiated (tree result
, int extern_p
)
17880 SET_DECL_EXPLICIT_INSTANTIATION (result
);
17882 /* If this entity has already been written out, it's too late to
17883 make any modifications. */
17884 if (TREE_ASM_WRITTEN (result
))
17887 /* For anonymous namespace we don't need to do anything. */
17888 if (decl_anon_ns_mem_p (result
))
17890 gcc_assert (!TREE_PUBLIC (result
));
17894 if (TREE_CODE (result
) != FUNCTION_DECL
)
17895 /* The TREE_PUBLIC flag for function declarations will have been
17896 set correctly by tsubst. */
17897 TREE_PUBLIC (result
) = 1;
17899 /* This might have been set by an earlier implicit instantiation. */
17900 DECL_COMDAT (result
) = 0;
17903 DECL_NOT_REALLY_EXTERN (result
) = 0;
17906 mark_definable (result
);
17907 /* Always make artificials weak. */
17908 if (DECL_ARTIFICIAL (result
) && flag_weak
)
17909 comdat_linkage (result
);
17910 /* For WIN32 we also want to put explicit instantiations in
17911 linkonce sections. */
17912 else if (TREE_PUBLIC (result
))
17913 maybe_make_one_only (result
);
17916 /* If EXTERN_P, then this function will not be emitted -- unless
17917 followed by an explicit instantiation, at which point its linkage
17918 will be adjusted. If !EXTERN_P, then this function will be
17919 emitted here. In neither circumstance do we want
17920 import_export_decl to adjust the linkage. */
17921 DECL_INTERFACE_KNOWN (result
) = 1;
17924 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17925 important template arguments. If any are missing, we check whether
17926 they're important by using error_mark_node for substituting into any
17927 args that were used for partial ordering (the ones between ARGS and END)
17928 and seeing if it bubbles up. */
17931 check_undeduced_parms (tree targs
, tree args
, tree end
)
17933 bool found
= false;
17935 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
17936 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
17939 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
17943 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
17944 if (substed
== error_mark_node
)
17950 /* Given two function templates PAT1 and PAT2, return:
17952 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17953 -1 if PAT2 is more specialized than PAT1.
17954 0 if neither is more specialized.
17956 LEN indicates the number of parameters we should consider
17957 (defaulted parameters should not be considered).
17959 The 1998 std underspecified function template partial ordering, and
17960 DR214 addresses the issue. We take pairs of arguments, one from
17961 each of the templates, and deduce them against each other. One of
17962 the templates will be more specialized if all the *other*
17963 template's arguments deduce against its arguments and at least one
17964 of its arguments *does* *not* deduce against the other template's
17965 corresponding argument. Deduction is done as for class templates.
17966 The arguments used in deduction have reference and top level cv
17967 qualifiers removed. Iff both arguments were originally reference
17968 types *and* deduction succeeds in both directions, an lvalue reference
17969 wins against an rvalue reference and otherwise the template
17970 with the more cv-qualified argument wins for that pairing (if
17971 neither is more cv-qualified, they both are equal). Unlike regular
17972 deduction, after all the arguments have been deduced in this way,
17973 we do *not* verify the deduced template argument values can be
17974 substituted into non-deduced contexts.
17976 The logic can be a bit confusing here, because we look at deduce1 and
17977 targs1 to see if pat2 is at least as specialized, and vice versa; if we
17978 can find template arguments for pat1 to make arg1 look like arg2, that
17979 means that arg2 is at least as specialized as arg1. */
17982 more_specialized_fn (tree pat1
, tree pat2
, int len
)
17984 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
17985 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
17986 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
17987 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
17988 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
17989 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
17990 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
17991 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
17992 tree origs1
, origs2
;
17993 bool lose1
= false;
17994 bool lose2
= false;
17996 /* Remove the this parameter from non-static member functions. If
17997 one is a non-static member function and the other is not a static
17998 member function, remove the first parameter from that function
17999 also. This situation occurs for operator functions where we
18000 locate both a member function (with this pointer) and non-member
18001 operator (with explicit first operand). */
18002 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
18004 len
--; /* LEN is the number of significant arguments for DECL1 */
18005 args1
= TREE_CHAIN (args1
);
18006 if (!DECL_STATIC_FUNCTION_P (decl2
))
18007 args2
= TREE_CHAIN (args2
);
18009 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
18011 args2
= TREE_CHAIN (args2
);
18012 if (!DECL_STATIC_FUNCTION_P (decl1
))
18015 args1
= TREE_CHAIN (args1
);
18019 /* If only one is a conversion operator, they are unordered. */
18020 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
18023 /* Consider the return type for a conversion function */
18024 if (DECL_CONV_FN_P (decl1
))
18026 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
18027 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
18031 processing_template_decl
++;
18037 /* Stop when an ellipsis is seen. */
18038 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
18040 tree arg1
= TREE_VALUE (args1
);
18041 tree arg2
= TREE_VALUE (args2
);
18042 int deduce1
, deduce2
;
18048 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
18049 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18051 /* When both arguments are pack expansions, we need only
18052 unify the patterns themselves. */
18053 arg1
= PACK_EXPANSION_PATTERN (arg1
);
18054 arg2
= PACK_EXPANSION_PATTERN (arg2
);
18056 /* This is the last comparison we need to do. */
18060 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
18062 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
18063 arg1
= TREE_TYPE (arg1
);
18064 quals1
= cp_type_quals (arg1
);
18067 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
18069 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
18070 arg2
= TREE_TYPE (arg2
);
18071 quals2
= cp_type_quals (arg2
);
18074 arg1
= TYPE_MAIN_VARIANT (arg1
);
18075 arg2
= TYPE_MAIN_VARIANT (arg2
);
18077 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
18079 int i
, len2
= list_length (args2
);
18080 tree parmvec
= make_tree_vec (1);
18081 tree argvec
= make_tree_vec (len2
);
18084 /* Setup the parameter vector, which contains only ARG1. */
18085 TREE_VEC_ELT (parmvec
, 0) = arg1
;
18087 /* Setup the argument vector, which contains the remaining
18089 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
18090 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
18092 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
18093 argvec
, DEDUCE_EXACT
,
18094 /*subr=*/true, /*explain_p=*/false)
18097 /* We cannot deduce in the other direction, because ARG1 is
18098 a pack expansion but ARG2 is not. */
18101 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18103 int i
, len1
= list_length (args1
);
18104 tree parmvec
= make_tree_vec (1);
18105 tree argvec
= make_tree_vec (len1
);
18108 /* Setup the parameter vector, which contains only ARG1. */
18109 TREE_VEC_ELT (parmvec
, 0) = arg2
;
18111 /* Setup the argument vector, which contains the remaining
18113 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
18114 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
18116 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
18117 argvec
, DEDUCE_EXACT
,
18118 /*subr=*/true, /*explain_p=*/false)
18121 /* We cannot deduce in the other direction, because ARG2 is
18122 a pack expansion but ARG1 is not.*/
18128 /* The normal case, where neither argument is a pack
18130 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
18131 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
18133 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
18134 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
18138 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18139 arg2, then arg2 is not as specialized as arg1. */
18145 /* "If, for a given type, deduction succeeds in both directions
18146 (i.e., the types are identical after the transformations above)
18147 and both P and A were reference types (before being replaced with
18148 the type referred to above):
18149 - if the type from the argument template was an lvalue reference and
18150 the type from the parameter template was not, the argument type is
18151 considered to be more specialized than the other; otherwise,
18152 - if the type from the argument template is more cv-qualified
18153 than the type from the parameter template (as described above),
18154 the argument type is considered to be more specialized than the other;
18156 - neither type is more specialized than the other." */
18158 if (deduce1
&& deduce2
)
18160 if (ref1
&& ref2
&& ref1
!= ref2
)
18167 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
18169 if ((quals1
& quals2
) == quals2
)
18171 if ((quals1
& quals2
) == quals1
)
18176 if (lose1
&& lose2
)
18177 /* We've failed to deduce something in either direction.
18178 These must be unordered. */
18181 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
18182 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18183 /* We have already processed all of the arguments in our
18184 handing of the pack expansion type. */
18187 args1
= TREE_CHAIN (args1
);
18188 args2
= TREE_CHAIN (args2
);
18191 /* "In most cases, all template parameters must have values in order for
18192 deduction to succeed, but for partial ordering purposes a template
18193 parameter may remain without a value provided it is not used in the
18194 types being used for partial ordering."
18196 Thus, if we are missing any of the targs1 we need to substitute into
18197 origs1, then pat2 is not as specialized as pat1. This can happen when
18198 there is a nondeduced context. */
18199 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
18201 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
18204 processing_template_decl
--;
18206 /* All things being equal, if the next argument is a pack expansion
18207 for one function but not for the other, prefer the
18208 non-variadic function. FIXME this is bogus; see c++/41958. */
18210 && args1
&& TREE_VALUE (args1
)
18211 && args2
&& TREE_VALUE (args2
))
18213 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
18214 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
18217 if (lose1
== lose2
)
18225 /* Determine which of two partial specializations of MAIN_TMPL is more
18228 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18229 to the first partial specialization. The TREE_VALUE is the
18230 innermost set of template parameters for the partial
18231 specialization. PAT2 is similar, but for the second template.
18233 Return 1 if the first partial specialization is more specialized;
18234 -1 if the second is more specialized; 0 if neither is more
18237 See [temp.class.order] for information about determining which of
18238 two templates is more specialized. */
18241 more_specialized_class (tree main_tmpl
, tree pat1
, tree pat2
)
18246 bool any_deductions
= false;
18248 tmpl1
= TREE_TYPE (pat1
);
18249 tmpl2
= TREE_TYPE (pat2
);
18251 /* Just like what happens for functions, if we are ordering between
18252 different class template specializations, we may encounter dependent
18253 types in the arguments, and we need our dependency check functions
18254 to behave correctly. */
18255 ++processing_template_decl
;
18256 targs
= get_class_bindings (main_tmpl
, TREE_VALUE (pat1
),
18257 CLASSTYPE_TI_ARGS (tmpl1
),
18258 CLASSTYPE_TI_ARGS (tmpl2
));
18262 any_deductions
= true;
18265 targs
= get_class_bindings (main_tmpl
, TREE_VALUE (pat2
),
18266 CLASSTYPE_TI_ARGS (tmpl2
),
18267 CLASSTYPE_TI_ARGS (tmpl1
));
18271 any_deductions
= true;
18273 --processing_template_decl
;
18275 /* In the case of a tie where at least one of the class templates
18276 has a parameter pack at the end, the template with the most
18277 non-packed parameters wins. */
18280 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
18281 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
18283 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
18284 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
18285 int len1
= TREE_VEC_LENGTH (args1
);
18286 int len2
= TREE_VEC_LENGTH (args2
);
18288 /* We don't count the pack expansion at the end. */
18289 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
18291 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
18296 else if (len1
< len2
)
18303 /* Return the template arguments that will produce the function signature
18304 DECL from the function template FN, with the explicit template
18305 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18306 also match. Return NULL_TREE if no satisfactory arguments could be
18310 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
18312 int ntparms
= DECL_NTPARMS (fn
);
18313 tree targs
= make_tree_vec (ntparms
);
18314 tree decl_type
= TREE_TYPE (decl
);
18315 tree decl_arg_types
;
18317 unsigned int nargs
, ix
;
18320 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
18322 /* Never do unification on the 'this' parameter. */
18323 decl_arg_types
= skip_artificial_parms_for (decl
,
18324 TYPE_ARG_TYPES (decl_type
));
18326 nargs
= list_length (decl_arg_types
);
18327 args
= XALLOCAVEC (tree
, nargs
);
18328 for (arg
= decl_arg_types
, ix
= 0;
18329 arg
!= NULL_TREE
&& arg
!= void_list_node
;
18330 arg
= TREE_CHAIN (arg
), ++ix
)
18331 args
[ix
] = TREE_VALUE (arg
);
18333 if (fn_type_unification (fn
, explicit_args
, targs
,
18335 (check_rettype
|| DECL_CONV_FN_P (fn
)
18336 ? TREE_TYPE (decl_type
) : NULL_TREE
),
18337 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
18339 == error_mark_node
)
18345 /* Return the innermost template arguments that, when applied to a partial
18346 specialization of MAIN_TMPL whose innermost template parameters are
18347 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
18350 For example, suppose we have:
18352 template <class T, class U> struct S {};
18353 template <class T> struct S<T*, int> {};
18355 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
18356 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
18357 int}. The resulting vector will be {double}, indicating that `T'
18358 is bound to `double'. */
18361 get_class_bindings (tree main_tmpl
, tree tparms
, tree spec_args
, tree args
)
18363 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
18365 tree innermost_deduced_args
;
18367 innermost_deduced_args
= make_tree_vec (ntparms
);
18368 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
18370 deduced_args
= copy_node (args
);
18371 SET_TMPL_ARGS_LEVEL (deduced_args
,
18372 TMPL_ARGS_DEPTH (deduced_args
),
18373 innermost_deduced_args
);
18376 deduced_args
= innermost_deduced_args
;
18378 if (unify (tparms
, deduced_args
,
18379 INNERMOST_TEMPLATE_ARGS (spec_args
),
18380 INNERMOST_TEMPLATE_ARGS (args
),
18381 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
18384 for (i
= 0; i
< ntparms
; ++i
)
18385 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
18388 /* Verify that nondeduced template arguments agree with the type
18389 obtained from argument deduction.
18393 struct A { typedef int X; };
18394 template <class T, class U> struct C {};
18395 template <class T> struct C<T, typename T::X> {};
18397 Then with the instantiation `C<A, int>', we can deduce that
18398 `T' is `A' but unify () does not check whether `typename T::X'
18400 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
18401 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (main_tmpl
),
18402 spec_args
, main_tmpl
,
18403 tf_none
, false, false);
18404 if (spec_args
== error_mark_node
18405 /* We only need to check the innermost arguments; the other
18406 arguments will always agree. */
18407 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
18408 INNERMOST_TEMPLATE_ARGS (args
)))
18411 /* Now that we have bindings for all of the template arguments,
18412 ensure that the arguments deduced for the template template
18413 parameters have compatible template parameter lists. See the use
18414 of template_template_parm_bindings_ok_p in fn_type_unification
18415 for more information. */
18416 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
18419 return deduced_args
;
18422 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
18423 Return the TREE_LIST node with the most specialized template, if
18424 any. If there is no most specialized template, the error_mark_node
18427 Note that this function does not look at, or modify, the
18428 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
18429 returned is one of the elements of INSTANTIATIONS, callers may
18430 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
18431 and retrieve it from the value returned. */
18434 most_specialized_instantiation (tree templates
)
18438 ++processing_template_decl
;
18441 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
18445 if (get_bindings (TREE_VALUE (champ
),
18446 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
18447 NULL_TREE
, /*check_ret=*/true))
18450 if (get_bindings (TREE_VALUE (fn
),
18451 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
18452 NULL_TREE
, /*check_ret=*/true))
18459 /* Equally specialized, move to next function. If there
18460 is no next function, nothing's most specialized. */
18461 fn
= TREE_CHAIN (fn
);
18469 /* Now verify that champ is better than everything earlier in the
18470 instantiation list. */
18471 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
18472 if (get_bindings (TREE_VALUE (champ
),
18473 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
18474 NULL_TREE
, /*check_ret=*/true)
18475 || !get_bindings (TREE_VALUE (fn
),
18476 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
18477 NULL_TREE
, /*check_ret=*/true))
18483 processing_template_decl
--;
18486 return error_mark_node
;
18491 /* If DECL is a specialization of some template, return the most
18492 general such template. Otherwise, returns NULL_TREE.
18494 For example, given:
18496 template <class T> struct S { template <class U> void f(U); };
18498 if TMPL is `template <class U> void S<int>::f(U)' this will return
18499 the full template. This function will not trace past partial
18500 specializations, however. For example, given in addition:
18502 template <class T> struct S<T*> { template <class U> void f(U); };
18504 if TMPL is `template <class U> void S<int*>::f(U)' this will return
18505 `template <class T> template <class U> S<T*>::f(U)'. */
18508 most_general_template (tree decl
)
18510 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
18511 an immediate specialization. */
18512 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18514 if (DECL_TEMPLATE_INFO (decl
)) {
18515 decl
= DECL_TI_TEMPLATE (decl
);
18517 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
18518 template friend. */
18519 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
18525 /* Look for more and more general templates. */
18526 while (DECL_TEMPLATE_INFO (decl
))
18528 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
18529 (See cp-tree.h for details.) */
18530 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
18533 if (CLASS_TYPE_P (TREE_TYPE (decl
))
18534 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
18537 /* Stop if we run into an explicitly specialized class template. */
18538 if (!DECL_NAMESPACE_SCOPE_P (decl
)
18539 && DECL_CONTEXT (decl
)
18540 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
18543 decl
= DECL_TI_TEMPLATE (decl
);
18549 /* Return the most specialized of the class template partial
18550 specializations of TMPL which can produce TYPE, a specialization of
18551 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
18552 a _TYPE node corresponding to the partial specialization, while the
18553 TREE_PURPOSE is the set of template arguments that must be
18554 substituted into the TREE_TYPE in order to generate TYPE.
18556 If the choice of partial specialization is ambiguous, a diagnostic
18557 is issued, and the error_mark_node is returned. If there are no
18558 partial specializations of TMPL matching TYPE, then NULL_TREE is
18562 most_specialized_class (tree type
, tree tmpl
, tsubst_flags_t complain
)
18564 tree list
= NULL_TREE
;
18570 tree outer_args
= NULL_TREE
;
18572 tmpl
= most_general_template (tmpl
);
18573 args
= CLASSTYPE_TI_ARGS (type
);
18575 /* For determining which partial specialization to use, only the
18576 innermost args are interesting. */
18577 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
18579 outer_args
= strip_innermost_template_args (args
, 1);
18580 args
= INNERMOST_TEMPLATE_ARGS (args
);
18583 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
18585 tree partial_spec_args
;
18587 tree spec_tmpl
= TREE_VALUE (t
);
18588 tree orig_parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
18590 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
18592 ++processing_template_decl
;
18596 /* Discard the outer levels of args, and then substitute in the
18597 template args from the enclosing class. */
18598 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
18599 partial_spec_args
= tsubst_template_args
18600 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
18602 /* And the same for the partial specialization TEMPLATE_DECL. */
18603 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
18606 partial_spec_args
=
18607 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
18608 add_to_template_args (outer_args
,
18609 partial_spec_args
),
18611 /*require_all_args=*/true,
18612 /*use_default_args=*/true);
18614 --processing_template_decl
;
18616 if (partial_spec_args
== error_mark_node
)
18617 return error_mark_node
;
18618 if (spec_tmpl
== error_mark_node
)
18619 return error_mark_node
;
18621 tree parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
18622 spec_args
= get_class_bindings (tmpl
, parms
,
18628 spec_args
= add_to_template_args (outer_args
, spec_args
);
18629 list
= tree_cons (spec_args
, orig_parms
, list
);
18630 TREE_TYPE (list
) = TREE_TYPE (t
);
18637 ambiguous_p
= false;
18640 t
= TREE_CHAIN (t
);
18641 for (; t
; t
= TREE_CHAIN (t
))
18643 fate
= more_specialized_class (tmpl
, champ
, t
);
18650 t
= TREE_CHAIN (t
);
18653 ambiguous_p
= true;
18662 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
18664 fate
= more_specialized_class (tmpl
, champ
, t
);
18667 ambiguous_p
= true;
18675 char *spaces
= NULL
;
18676 if (!(complain
& tf_error
))
18677 return error_mark_node
;
18678 error ("ambiguous class template instantiation for %q#T", type
);
18679 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
18680 for (t
= list
; t
; t
= TREE_CHAIN (t
))
18682 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
18683 spaces
= spaces
? spaces
: get_spaces (str
);
18686 return error_mark_node
;
18692 /* Explicitly instantiate DECL. */
18695 do_decl_instantiation (tree decl
, tree storage
)
18697 tree result
= NULL_TREE
;
18700 if (!decl
|| decl
== error_mark_node
)
18701 /* An error occurred, for which grokdeclarator has already issued
18702 an appropriate message. */
18704 else if (! DECL_LANG_SPECIFIC (decl
))
18706 error ("explicit instantiation of non-template %q#D", decl
);
18709 else if (VAR_P (decl
))
18711 /* There is an asymmetry here in the way VAR_DECLs and
18712 FUNCTION_DECLs are handled by grokdeclarator. In the case of
18713 the latter, the DECL we get back will be marked as a
18714 template instantiation, and the appropriate
18715 DECL_TEMPLATE_INFO will be set up. This does not happen for
18716 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
18717 should handle VAR_DECLs as it currently handles
18719 if (!DECL_CLASS_SCOPE_P (decl
))
18721 error ("%qD is not a static data member of a class template", decl
);
18724 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
18725 if (!result
|| !VAR_P (result
))
18727 error ("no matching template for %qD found", decl
);
18730 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
18732 error ("type %qT for explicit instantiation %qD does not match "
18733 "declared type %qT", TREE_TYPE (result
), decl
,
18738 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
18740 error ("explicit instantiation of %q#D", decl
);
18746 /* Check for various error cases. Note that if the explicit
18747 instantiation is valid the RESULT will currently be marked as an
18748 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
18749 until we get here. */
18751 if (DECL_TEMPLATE_SPECIALIZATION (result
))
18753 /* DR 259 [temp.spec].
18755 Both an explicit instantiation and a declaration of an explicit
18756 specialization shall not appear in a program unless the explicit
18757 instantiation follows a declaration of the explicit specialization.
18759 For a given set of template parameters, if an explicit
18760 instantiation of a template appears after a declaration of an
18761 explicit specialization for that template, the explicit
18762 instantiation has no effect. */
18765 else if (DECL_EXPLICIT_INSTANTIATION (result
))
18769 No program shall explicitly instantiate any template more
18772 We check DECL_NOT_REALLY_EXTERN so as not to complain when
18773 the first instantiation was `extern' and the second is not,
18774 and EXTERN_P for the opposite case. */
18775 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
18776 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
18777 /* If an "extern" explicit instantiation follows an ordinary
18778 explicit instantiation, the template is instantiated. */
18782 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
18784 error ("no matching template for %qD found", result
);
18787 else if (!DECL_TEMPLATE_INFO (result
))
18789 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
18793 if (storage
== NULL_TREE
)
18795 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18797 if (!in_system_header
&& (cxx_dialect
== cxx98
))
18798 pedwarn (input_location
, OPT_Wpedantic
,
18799 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
18804 error ("storage class %qD applied to template instantiation", storage
);
18806 check_explicit_instantiation_namespace (result
);
18807 mark_decl_instantiated (result
, extern_p
);
18809 instantiate_decl (result
, /*defer_ok=*/1,
18810 /*expl_inst_class_mem_p=*/false);
18814 mark_class_instantiated (tree t
, int extern_p
)
18816 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
18817 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
18818 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
18819 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
18822 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
18823 rest_of_type_compilation (t
, 1);
18827 /* Called from do_type_instantiation through binding_table_foreach to
18828 do recursive instantiation for the type bound in ENTRY. */
18830 bt_instantiate_type_proc (binding_entry entry
, void *data
)
18832 tree storage
= *(tree
*) data
;
18834 if (MAYBE_CLASS_TYPE_P (entry
->type
)
18835 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
18836 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
18839 /* Called from do_type_instantiation to instantiate a member
18840 (a member function or a static member variable) of an
18841 explicitly instantiated class template. */
18843 instantiate_class_member (tree decl
, int extern_p
)
18845 mark_decl_instantiated (decl
, extern_p
);
18847 instantiate_decl (decl
, /*defer_ok=*/1,
18848 /*expl_inst_class_mem_p=*/true);
18851 /* Perform an explicit instantiation of template class T. STORAGE, if
18852 non-null, is the RID for extern, inline or static. COMPLAIN is
18853 nonzero if this is called from the parser, zero if called recursively,
18854 since the standard is unclear (as detailed below). */
18857 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
18862 int previous_instantiation_extern_p
= 0;
18864 if (TREE_CODE (t
) == TYPE_DECL
)
18867 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
18870 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
18872 error ("explicit instantiation of non-class template %qD", tmpl
);
18874 error ("explicit instantiation of non-template type %qT", t
);
18880 if (!COMPLETE_TYPE_P (t
))
18882 if (complain
& tf_error
)
18883 error ("explicit instantiation of %q#T before definition of template",
18888 if (storage
!= NULL_TREE
)
18890 if (!in_system_header
)
18892 if (storage
== ridpointers
[(int) RID_EXTERN
])
18894 if (cxx_dialect
== cxx98
)
18895 pedwarn (input_location
, OPT_Wpedantic
,
18896 "ISO C++ 1998 forbids the use of %<extern%> on "
18897 "explicit instantiations");
18900 pedwarn (input_location
, OPT_Wpedantic
,
18901 "ISO C++ forbids the use of %qE"
18902 " on explicit instantiations", storage
);
18905 if (storage
== ridpointers
[(int) RID_INLINE
])
18907 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18909 else if (storage
== ridpointers
[(int) RID_STATIC
])
18913 error ("storage class %qD applied to template instantiation",
18919 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
18921 /* DR 259 [temp.spec].
18923 Both an explicit instantiation and a declaration of an explicit
18924 specialization shall not appear in a program unless the explicit
18925 instantiation follows a declaration of the explicit specialization.
18927 For a given set of template parameters, if an explicit
18928 instantiation of a template appears after a declaration of an
18929 explicit specialization for that template, the explicit
18930 instantiation has no effect. */
18933 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
18937 No program shall explicitly instantiate any template more
18940 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18941 instantiation was `extern'. If EXTERN_P then the second is.
18942 These cases are OK. */
18943 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
18945 if (!previous_instantiation_extern_p
&& !extern_p
18946 && (complain
& tf_error
))
18947 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
18949 /* If we've already instantiated the template, just return now. */
18950 if (!CLASSTYPE_INTERFACE_ONLY (t
))
18954 check_explicit_instantiation_namespace (TYPE_NAME (t
));
18955 mark_class_instantiated (t
, extern_p
);
18963 /* In contrast to implicit instantiation, where only the
18964 declarations, and not the definitions, of members are
18965 instantiated, we have here:
18969 The explicit instantiation of a class template specialization
18970 implies the instantiation of all of its members not
18971 previously explicitly specialized in the translation unit
18972 containing the explicit instantiation.
18974 Of course, we can't instantiate member template classes, since
18975 we don't have any arguments for them. Note that the standard
18976 is unclear on whether the instantiation of the members are
18977 *explicit* instantiations or not. However, the most natural
18978 interpretation is that it should be an explicit instantiation. */
18981 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18982 if (TREE_CODE (tmp
) == FUNCTION_DECL
18983 && DECL_TEMPLATE_INSTANTIATION (tmp
))
18984 instantiate_class_member (tmp
, extern_p
);
18986 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18987 if (VAR_P (tmp
) && DECL_TEMPLATE_INSTANTIATION (tmp
))
18988 instantiate_class_member (tmp
, extern_p
);
18990 if (CLASSTYPE_NESTED_UTDS (t
))
18991 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
18992 bt_instantiate_type_proc
, &storage
);
18996 /* Given a function DECL, which is a specialization of TMPL, modify
18997 DECL to be a re-instantiation of TMPL with the same template
18998 arguments. TMPL should be the template into which tsubst'ing
18999 should occur for DECL, not the most general template.
19001 One reason for doing this is a scenario like this:
19004 void f(const T&, int i);
19006 void g() { f(3, 7); }
19009 void f(const T& t, const int i) { }
19011 Note that when the template is first instantiated, with
19012 instantiate_template, the resulting DECL will have no name for the
19013 first parameter, and the wrong type for the second. So, when we go
19014 to instantiate the DECL, we regenerate it. */
19017 regenerate_decl_from_template (tree decl
, tree tmpl
)
19019 /* The arguments used to instantiate DECL, from the most general
19024 args
= DECL_TI_ARGS (decl
);
19025 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
19027 /* Make sure that we can see identifiers, and compute access
19029 push_access_scope (decl
);
19031 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19039 args_depth
= TMPL_ARGS_DEPTH (args
);
19040 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
19041 if (args_depth
> parms_depth
)
19042 args
= get_innermost_template_args (args
, parms_depth
);
19044 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
19045 args
, tf_error
, NULL_TREE
,
19046 /*defer_ok*/false);
19047 if (specs
&& specs
!= error_mark_node
)
19048 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
19051 /* Merge parameter declarations. */
19052 decl_parm
= skip_artificial_parms_for (decl
,
19053 DECL_ARGUMENTS (decl
));
19055 = skip_artificial_parms_for (code_pattern
,
19056 DECL_ARGUMENTS (code_pattern
));
19057 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
19062 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
19063 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
19064 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
19066 parm_type
= type_decays_to (parm_type
);
19067 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
19068 TREE_TYPE (decl_parm
) = parm_type
;
19069 attributes
= DECL_ATTRIBUTES (pattern_parm
);
19070 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
19072 DECL_ATTRIBUTES (decl_parm
) = attributes
;
19073 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
19075 decl_parm
= DECL_CHAIN (decl_parm
);
19076 pattern_parm
= DECL_CHAIN (pattern_parm
);
19078 /* Merge any parameters that match with the function parameter
19080 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
19083 tree expanded_types
;
19084 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19085 the parameters in this function parameter pack. */
19086 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
19087 args
, tf_error
, NULL_TREE
);
19088 len
= TREE_VEC_LENGTH (expanded_types
);
19089 for (i
= 0; i
< len
; i
++)
19094 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
19095 /* Rename the parameter to include the index. */
19096 DECL_NAME (decl_parm
) =
19097 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
19098 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
19099 parm_type
= type_decays_to (parm_type
);
19100 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
19101 TREE_TYPE (decl_parm
) = parm_type
;
19102 attributes
= DECL_ATTRIBUTES (pattern_parm
);
19103 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
19105 DECL_ATTRIBUTES (decl_parm
) = attributes
;
19106 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
19108 decl_parm
= DECL_CHAIN (decl_parm
);
19111 /* Merge additional specifiers from the CODE_PATTERN. */
19112 if (DECL_DECLARED_INLINE_P (code_pattern
)
19113 && !DECL_DECLARED_INLINE_P (decl
))
19114 DECL_DECLARED_INLINE_P (decl
) = 1;
19116 else if (VAR_P (decl
))
19118 DECL_INITIAL (decl
) =
19119 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
19120 tf_error
, DECL_TI_TEMPLATE (decl
),
19121 /*integral_constant_expression_p=*/false);
19122 if (VAR_HAD_UNKNOWN_BOUND (decl
))
19123 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
19124 tf_error
, DECL_TI_TEMPLATE (decl
));
19127 gcc_unreachable ();
19129 pop_access_scope (decl
);
19132 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19133 substituted to get DECL. */
19136 template_for_substitution (tree decl
)
19138 tree tmpl
= DECL_TI_TEMPLATE (decl
);
19140 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19141 for the instantiation. This is not always the most general
19142 template. Consider, for example:
19145 struct S { template <class U> void f();
19146 template <> void f<int>(); };
19148 and an instantiation of S<double>::f<int>. We want TD to be the
19149 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19150 while (/* An instantiation cannot have a definition, so we need a
19151 more general template. */
19152 DECL_TEMPLATE_INSTANTIATION (tmpl
)
19153 /* We must also deal with friend templates. Given:
19155 template <class T> struct S {
19156 template <class U> friend void f() {};
19159 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19160 so far as the language is concerned, but that's still
19161 where we get the pattern for the instantiation from. On
19162 other hand, if the definition comes outside the class, say:
19164 template <class T> struct S {
19165 template <class U> friend void f();
19167 template <class U> friend void f() {}
19169 we don't need to look any further. That's what the check for
19170 DECL_INITIAL is for. */
19171 || (TREE_CODE (decl
) == FUNCTION_DECL
19172 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
19173 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
19175 /* The present template, TD, should not be a definition. If it
19176 were a definition, we should be using it! Note that we
19177 cannot restructure the loop to just keep going until we find
19178 a template with a definition, since that might go too far if
19179 a specialization was declared, but not defined. */
19180 gcc_assert (!VAR_P (decl
)
19181 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
19183 /* Fetch the more general template. */
19184 tmpl
= DECL_TI_TEMPLATE (tmpl
);
19190 /* Returns true if we need to instantiate this template instance even if we
19191 know we aren't going to emit it.. */
19194 always_instantiate_p (tree decl
)
19196 /* We always instantiate inline functions so that we can inline them. An
19197 explicit instantiation declaration prohibits implicit instantiation of
19198 non-inline functions. With high levels of optimization, we would
19199 normally inline non-inline functions -- but we're not allowed to do
19200 that for "extern template" functions. Therefore, we check
19201 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19202 return ((TREE_CODE (decl
) == FUNCTION_DECL
19203 && (DECL_DECLARED_INLINE_P (decl
)
19204 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
19205 /* And we need to instantiate static data members so that
19206 their initializers are available in integral constant
19209 && decl_maybe_constant_var_p (decl
)));
19212 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19213 instantiate it now, modifying TREE_TYPE (fn). */
19216 maybe_instantiate_noexcept (tree fn
)
19218 tree fntype
, spec
, noex
, clone
;
19220 if (DECL_CLONED_FUNCTION_P (fn
))
19221 fn
= DECL_CLONED_FUNCTION (fn
);
19222 fntype
= TREE_TYPE (fn
);
19223 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
19225 if (!DEFERRED_NOEXCEPT_SPEC_P (spec
))
19228 noex
= TREE_PURPOSE (spec
);
19230 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
19232 if (push_tinst_level (fn
))
19234 push_access_scope (fn
);
19235 push_deferring_access_checks (dk_no_deferred
);
19236 input_location
= DECL_SOURCE_LOCATION (fn
);
19237 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
19238 DEFERRED_NOEXCEPT_ARGS (noex
),
19239 tf_warning_or_error
, fn
,
19240 /*function_p=*/false,
19241 /*integral_constant_expression_p=*/true);
19242 pop_deferring_access_checks ();
19243 pop_access_scope (fn
);
19244 pop_tinst_level ();
19245 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
19246 if (spec
== error_mark_node
)
19247 spec
= noexcept_false_spec
;
19250 spec
= noexcept_false_spec
;
19254 /* This is an implicitly declared function, so NOEX is a list of
19255 other functions to evaluate and merge. */
19257 spec
= noexcept_true_spec
;
19258 for (elt
= noex
; elt
; elt
= OVL_NEXT (elt
))
19260 tree fn
= OVL_CURRENT (elt
);
19262 maybe_instantiate_noexcept (fn
);
19263 subspec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
19264 spec
= merge_exception_specifiers (spec
, subspec
, NULL_TREE
);
19268 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
19270 FOR_EACH_CLONE (clone
, fn
)
19272 if (TREE_TYPE (clone
) == fntype
)
19273 TREE_TYPE (clone
) = TREE_TYPE (fn
);
19275 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
19279 /* Produce the definition of D, a _DECL generated from a template. If
19280 DEFER_OK is nonzero, then we don't have to actually do the
19281 instantiation now; we just have to do it sometime. Normally it is
19282 an error if this is an explicit instantiation but D is undefined.
19283 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19284 explicitly instantiated class template. */
19287 instantiate_decl (tree d
, int defer_ok
,
19288 bool expl_inst_class_mem_p
)
19290 tree tmpl
= DECL_TI_TEMPLATE (d
);
19297 bool pattern_defined
;
19298 location_t saved_loc
= input_location
;
19299 int saved_unevaluated_operand
= cp_unevaluated_operand
;
19300 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
19305 /* This function should only be used to instantiate templates for
19306 functions and static member variables. */
19307 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
19309 /* Variables are never deferred; if instantiation is required, they
19310 are instantiated right away. That allows for better code in the
19311 case that an expression refers to the value of the variable --
19312 if the variable has a constant value the referring expression can
19313 take advantage of that fact. */
19315 || DECL_DECLARED_CONSTEXPR_P (d
))
19318 /* Don't instantiate cloned functions. Instead, instantiate the
19319 functions they cloned. */
19320 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
19321 d
= DECL_CLONED_FUNCTION (d
);
19323 if (DECL_TEMPLATE_INSTANTIATED (d
)
19324 || (TREE_CODE (d
) == FUNCTION_DECL
19325 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
19326 || DECL_TEMPLATE_SPECIALIZATION (d
))
19327 /* D has already been instantiated or explicitly specialized, so
19328 there's nothing for us to do here.
19330 It might seem reasonable to check whether or not D is an explicit
19331 instantiation, and, if so, stop here. But when an explicit
19332 instantiation is deferred until the end of the compilation,
19333 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
19334 the instantiation. */
19337 /* Check to see whether we know that this template will be
19338 instantiated in some other file, as with "extern template"
19340 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
19342 /* In general, we do not instantiate such templates. */
19343 if (external_p
&& !always_instantiate_p (d
))
19346 gen_tmpl
= most_general_template (tmpl
);
19347 gen_args
= DECL_TI_ARGS (d
);
19349 if (tmpl
!= gen_tmpl
)
19350 /* We should already have the extra args. */
19351 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
19352 == TMPL_ARGS_DEPTH (gen_args
));
19353 /* And what's in the hash table should match D. */
19354 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
19355 || spec
== NULL_TREE
);
19357 /* This needs to happen before any tsubsting. */
19358 if (! push_tinst_level (d
))
19361 timevar_push (TV_TEMPLATE_INST
);
19363 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
19364 for the instantiation. */
19365 td
= template_for_substitution (d
);
19366 code_pattern
= DECL_TEMPLATE_RESULT (td
);
19368 /* We should never be trying to instantiate a member of a class
19369 template or partial specialization. */
19370 gcc_assert (d
!= code_pattern
);
19372 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
19373 || DECL_TEMPLATE_SPECIALIZATION (td
))
19374 /* In the case of a friend template whose definition is provided
19375 outside the class, we may have too many arguments. Drop the
19376 ones we don't need. The same is true for specializations. */
19377 args
= get_innermost_template_args
19378 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
19382 if (TREE_CODE (d
) == FUNCTION_DECL
)
19383 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
19384 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
));
19386 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
19388 /* We may be in the middle of deferred access check. Disable it now. */
19389 push_deferring_access_checks (dk_no_deferred
);
19391 /* Unless an explicit instantiation directive has already determined
19392 the linkage of D, remember that a definition is available for
19394 if (pattern_defined
19395 && !DECL_INTERFACE_KNOWN (d
)
19396 && !DECL_NOT_REALLY_EXTERN (d
))
19397 mark_definable (d
);
19399 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
19400 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
19401 input_location
= DECL_SOURCE_LOCATION (d
);
19403 /* If D is a member of an explicitly instantiated class template,
19404 and no definition is available, treat it like an implicit
19406 if (!pattern_defined
&& expl_inst_class_mem_p
19407 && DECL_EXPLICIT_INSTANTIATION (d
))
19409 /* Leave linkage flags alone on instantiations with anonymous
19411 if (TREE_PUBLIC (d
))
19413 DECL_NOT_REALLY_EXTERN (d
) = 0;
19414 DECL_INTERFACE_KNOWN (d
) = 0;
19416 SET_DECL_IMPLICIT_INSTANTIATION (d
);
19419 if (TREE_CODE (d
) == FUNCTION_DECL
)
19420 maybe_instantiate_noexcept (d
);
19422 /* Defer all other templates, unless we have been explicitly
19423 forbidden from doing so. */
19424 if (/* If there is no definition, we cannot instantiate the
19427 /* If it's OK to postpone instantiation, do so. */
19429 /* If this is a static data member that will be defined
19430 elsewhere, we don't want to instantiate the entire data
19431 member, but we do want to instantiate the initializer so that
19432 we can substitute that elsewhere. */
19433 || (external_p
&& VAR_P (d
)))
19435 /* The definition of the static data member is now required so
19436 we must substitute the initializer. */
19438 && !DECL_INITIAL (d
)
19439 && DECL_INITIAL (code_pattern
))
19443 bool const_init
= false;
19445 ns
= decl_namespace_context (d
);
19446 push_nested_namespace (ns
);
19447 push_nested_class (DECL_CONTEXT (d
));
19448 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
19450 tf_warning_or_error
, NULL_TREE
,
19451 /*integral_constant_expression_p=*/false);
19452 /* Make sure the initializer is still constant, in case of
19453 circular dependency (template/instantiate6.C). */
19455 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
19456 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
19457 /*asmspec_tree=*/NULL_TREE
,
19458 LOOKUP_ONLYCONVERTING
);
19459 pop_nested_class ();
19460 pop_nested_namespace (ns
);
19463 /* We restore the source position here because it's used by
19464 add_pending_template. */
19465 input_location
= saved_loc
;
19467 if (at_eof
&& !pattern_defined
19468 && DECL_EXPLICIT_INSTANTIATION (d
)
19469 && DECL_NOT_REALLY_EXTERN (d
))
19472 The definition of a non-exported function template, a
19473 non-exported member function template, or a non-exported
19474 member function or static data member of a class template
19475 shall be present in every translation unit in which it is
19476 explicitly instantiated. */
19477 permerror (input_location
, "explicit instantiation of %qD "
19478 "but no definition available", d
);
19480 /* If we're in unevaluated context, we just wanted to get the
19481 constant value; this isn't an odr use, so don't queue
19482 a full instantiation. */
19483 if (cp_unevaluated_operand
!= 0)
19485 /* ??? Historically, we have instantiated inline functions, even
19486 when marked as "extern template". */
19487 if (!(external_p
&& VAR_P (d
)))
19488 add_pending_template (d
);
19491 /* Tell the repository that D is available in this translation unit
19492 -- and see if it is supposed to be instantiated here. */
19493 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
19495 /* In a PCH file, despite the fact that the repository hasn't
19496 requested instantiation in the PCH it is still possible that
19497 an instantiation will be required in a file that includes the
19500 add_pending_template (d
);
19501 /* Instantiate inline functions so that the inliner can do its
19502 job, even though we'll not be emitting a copy of this
19504 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
19508 fn_context
= decl_function_context (d
);
19509 nested
= (current_function_decl
!= NULL_TREE
);
19511 push_to_top_level ();
19515 push_function_context ();
19516 cp_unevaluated_operand
= 0;
19517 c_inhibit_evaluation_warnings
= 0;
19520 /* Mark D as instantiated so that recursive calls to
19521 instantiate_decl do not try to instantiate it again. */
19522 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
19524 /* Regenerate the declaration in case the template has been modified
19525 by a subsequent redeclaration. */
19526 regenerate_decl_from_template (d
, td
);
19528 /* We already set the file and line above. Reset them now in case
19529 they changed as a result of calling regenerate_decl_from_template. */
19530 input_location
= DECL_SOURCE_LOCATION (d
);
19535 bool const_init
= false;
19537 /* Clear out DECL_RTL; whatever was there before may not be right
19538 since we've reset the type of the declaration. */
19539 SET_DECL_RTL (d
, NULL
);
19540 DECL_IN_AGGR_P (d
) = 0;
19542 /* The initializer is placed in DECL_INITIAL by
19543 regenerate_decl_from_template so we don't need to
19544 push/pop_access_scope again here. Pull it out so that
19545 cp_finish_decl can process it. */
19546 init
= DECL_INITIAL (d
);
19547 DECL_INITIAL (d
) = NULL_TREE
;
19548 DECL_INITIALIZED_P (d
) = 0;
19550 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
19551 initializer. That function will defer actual emission until
19552 we have a chance to determine linkage. */
19553 DECL_EXTERNAL (d
) = 0;
19555 /* Enter the scope of D so that access-checking works correctly. */
19556 push_nested_class (DECL_CONTEXT (d
));
19557 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
19558 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
19559 pop_nested_class ();
19561 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
19562 synthesize_method (d
);
19563 else if (TREE_CODE (d
) == FUNCTION_DECL
)
19565 struct pointer_map_t
*saved_local_specializations
;
19569 tree block
= NULL_TREE
;
19571 /* Save away the current list, in case we are instantiating one
19572 template from within the body of another. */
19573 saved_local_specializations
= local_specializations
;
19575 /* Set up the list of local specializations. */
19576 local_specializations
= pointer_map_create ();
19578 /* Set up context. */
19579 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
19580 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
19581 block
= push_stmt_list ();
19583 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
19585 /* Some typedefs referenced from within the template code need to be
19586 access checked at template instantiation time, i.e now. These
19587 types were added to the template at parsing time. Let's get those
19588 and perform the access checks then. */
19589 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl
),
19592 /* Create substitution entries for the parameters. */
19593 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
19594 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
19595 spec_parm
= DECL_ARGUMENTS (d
);
19596 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
19598 register_local_specialization (spec_parm
, tmpl_parm
);
19599 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
19600 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
19602 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
19604 if (!DECL_PACK_P (tmpl_parm
))
19606 register_local_specialization (spec_parm
, tmpl_parm
);
19607 spec_parm
= DECL_CHAIN (spec_parm
);
19611 /* Register the (value) argument pack as a specialization of
19612 TMPL_PARM, then move on. */
19613 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
19614 register_local_specialization (argpack
, tmpl_parm
);
19617 gcc_assert (!spec_parm
);
19619 /* Substitute into the body of the function. */
19620 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
19621 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
19622 tf_warning_or_error
, tmpl
);
19625 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
19626 tf_warning_or_error
, tmpl
,
19627 /*integral_constant_expression_p=*/false);
19629 /* Set the current input_location to the end of the function
19630 so that finish_function knows where we are. */
19632 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
19635 /* We don't need the local specializations any more. */
19636 pointer_map_destroy (local_specializations
);
19637 local_specializations
= saved_local_specializations
;
19639 /* Finish the function. */
19640 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
19641 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
19642 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
19645 d
= finish_function (0);
19646 expand_or_defer_fn (d
);
19649 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
19650 cp_check_omp_declare_reduction (d
);
19653 /* We're not deferring instantiation any more. */
19654 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
19657 pop_from_top_level ();
19659 pop_function_context ();
19662 input_location
= saved_loc
;
19663 cp_unevaluated_operand
= saved_unevaluated_operand
;
19664 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
19665 pop_deferring_access_checks ();
19666 pop_tinst_level ();
19668 timevar_pop (TV_TEMPLATE_INST
);
19673 /* Run through the list of templates that we wish we could
19674 instantiate, and instantiate any we can. RETRIES is the
19675 number of times we retry pending template instantiation. */
19678 instantiate_pending_templates (int retries
)
19681 location_t saved_loc
= input_location
;
19683 /* Instantiating templates may trigger vtable generation. This in turn
19684 may require further template instantiations. We place a limit here
19685 to avoid infinite loop. */
19686 if (pending_templates
&& retries
>= max_tinst_depth
)
19688 tree decl
= pending_templates
->tinst
->decl
;
19690 error ("template instantiation depth exceeds maximum of %d"
19691 " instantiating %q+D, possibly from virtual table generation"
19692 " (use -ftemplate-depth= to increase the maximum)",
19693 max_tinst_depth
, decl
);
19694 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19695 /* Pretend that we defined it. */
19696 DECL_INITIAL (decl
) = error_mark_node
;
19702 struct pending_template
**t
= &pending_templates
;
19703 struct pending_template
*last
= NULL
;
19707 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
19708 bool complete
= false;
19710 if (TYPE_P (instantiation
))
19714 if (!COMPLETE_TYPE_P (instantiation
))
19716 instantiate_class_template (instantiation
);
19717 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
19718 for (fn
= TYPE_METHODS (instantiation
);
19720 fn
= TREE_CHAIN (fn
))
19721 if (! DECL_ARTIFICIAL (fn
))
19722 instantiate_decl (fn
,
19724 /*expl_inst_class_mem_p=*/false);
19725 if (COMPLETE_TYPE_P (instantiation
))
19729 complete
= COMPLETE_TYPE_P (instantiation
);
19733 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
19734 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
19737 = instantiate_decl (instantiation
,
19739 /*expl_inst_class_mem_p=*/false);
19740 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
19744 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
19745 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
19749 /* If INSTANTIATION has been instantiated, then we don't
19750 need to consider it again in the future. */
19758 current_tinst_level
= NULL
;
19760 last_pending_template
= last
;
19762 while (reconsider
);
19764 input_location
= saved_loc
;
19767 /* Substitute ARGVEC into T, which is a list of initializers for
19768 either base class or a non-static data member. The TREE_PURPOSEs
19769 are DECLs, and the TREE_VALUEs are the initializer values. Used by
19770 instantiate_decl. */
19773 tsubst_initializer_list (tree t
, tree argvec
)
19775 tree inits
= NULL_TREE
;
19777 for (; t
; t
= TREE_CHAIN (t
))
19781 tree expanded_bases
= NULL_TREE
;
19782 tree expanded_arguments
= NULL_TREE
;
19785 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
19790 /* Expand the base class expansion type into separate base
19792 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
19793 tf_warning_or_error
,
19795 if (expanded_bases
== error_mark_node
)
19798 /* We'll be building separate TREE_LISTs of arguments for
19800 len
= TREE_VEC_LENGTH (expanded_bases
);
19801 expanded_arguments
= make_tree_vec (len
);
19802 for (i
= 0; i
< len
; i
++)
19803 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
19805 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
19806 expand each argument in the TREE_VALUE of t. */
19807 expr
= make_node (EXPR_PACK_EXPANSION
);
19808 PACK_EXPANSION_LOCAL_P (expr
) = true;
19809 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
19810 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
19812 if (TREE_VALUE (t
) == void_type_node
)
19813 /* VOID_TYPE_NODE is used to indicate
19814 value-initialization. */
19816 for (i
= 0; i
< len
; i
++)
19817 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
19821 /* Substitute parameter packs into each argument in the
19823 in_base_initializer
= 1;
19824 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
19826 tree expanded_exprs
;
19828 /* Expand the argument. */
19829 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
19831 = tsubst_pack_expansion (expr
, argvec
,
19832 tf_warning_or_error
,
19834 if (expanded_exprs
== error_mark_node
)
19837 /* Prepend each of the expanded expressions to the
19838 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
19839 for (i
= 0; i
< len
; i
++)
19841 TREE_VEC_ELT (expanded_arguments
, i
) =
19842 tree_cons (NULL_TREE
,
19843 TREE_VEC_ELT (expanded_exprs
, i
),
19844 TREE_VEC_ELT (expanded_arguments
, i
));
19847 in_base_initializer
= 0;
19849 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
19850 since we built them backwards. */
19851 for (i
= 0; i
< len
; i
++)
19853 TREE_VEC_ELT (expanded_arguments
, i
) =
19854 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
19859 for (i
= 0; i
< len
; ++i
)
19861 if (expanded_bases
)
19863 decl
= TREE_VEC_ELT (expanded_bases
, i
);
19864 decl
= expand_member_init (decl
);
19865 init
= TREE_VEC_ELT (expanded_arguments
, i
);
19870 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
19871 tf_warning_or_error
, NULL_TREE
);
19873 decl
= expand_member_init (decl
);
19874 if (decl
&& !DECL_P (decl
))
19875 in_base_initializer
= 1;
19877 init
= TREE_VALUE (t
);
19879 if (init
!= void_type_node
)
19880 init
= tsubst_expr (init
, argvec
,
19881 tf_warning_or_error
, NULL_TREE
,
19882 /*integral_constant_expression_p=*/false);
19883 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
19884 /* If we had an initializer but it instantiated to nothing,
19885 value-initialize the object. This will only occur when
19886 the initializer was a pack expansion where the parameter
19887 packs used in that expansion were of length zero. */
19888 init
= void_type_node
;
19889 in_base_initializer
= 0;
19894 init
= build_tree_list (decl
, init
);
19895 TREE_CHAIN (init
) = inits
;
19903 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19906 set_current_access_from_decl (tree decl
)
19908 if (TREE_PRIVATE (decl
))
19909 current_access_specifier
= access_private_node
;
19910 else if (TREE_PROTECTED (decl
))
19911 current_access_specifier
= access_protected_node
;
19913 current_access_specifier
= access_public_node
;
19916 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19917 is the instantiation (which should have been created with
19918 start_enum) and ARGS are the template arguments to use. */
19921 tsubst_enum (tree tag
, tree newtag
, tree args
)
19925 if (SCOPED_ENUM_P (newtag
))
19926 begin_scope (sk_scoped_enum
, newtag
);
19928 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
19933 decl
= TREE_VALUE (e
);
19934 /* Note that in a template enum, the TREE_VALUE is the
19935 CONST_DECL, not the corresponding INTEGER_CST. */
19936 value
= tsubst_expr (DECL_INITIAL (decl
),
19937 args
, tf_warning_or_error
, NULL_TREE
,
19938 /*integral_constant_expression_p=*/true);
19940 /* Give this enumeration constant the correct access. */
19941 set_current_access_from_decl (decl
);
19943 /* Actually build the enumerator itself. */
19945 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
19948 if (SCOPED_ENUM_P (newtag
))
19951 finish_enum_value_list (newtag
);
19952 finish_enum (newtag
);
19954 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
19955 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
19958 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19959 its type -- but without substituting the innermost set of template
19960 arguments. So, innermost set of template parameters will appear in
19964 get_mostly_instantiated_function_type (tree decl
)
19972 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
19973 targs
= DECL_TI_ARGS (decl
);
19974 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
19975 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
19977 /* There should be as many levels of arguments as there are levels
19979 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
19981 fn_type
= TREE_TYPE (tmpl
);
19983 if (parm_depth
== 1)
19984 /* No substitution is necessary. */
19991 /* Replace the innermost level of the TARGS with NULL_TREEs to
19992 let tsubst know not to substitute for those parameters. */
19993 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
19994 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
19995 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
19996 TMPL_ARGS_LEVEL (targs
, i
));
19997 SET_TMPL_ARGS_LEVEL (partial_args
,
19998 TMPL_ARGS_DEPTH (targs
),
19999 make_tree_vec (DECL_NTPARMS (tmpl
)));
20001 /* Make sure that we can see identifiers, and compute access
20003 push_access_scope (decl
);
20005 ++processing_template_decl
;
20006 /* Now, do the (partial) substitution to figure out the
20007 appropriate function type. */
20008 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
20009 --processing_template_decl
;
20011 /* Substitute into the template parameters to obtain the real
20012 innermost set of parameters. This step is important if the
20013 innermost set of template parameters contains value
20014 parameters whose types depend on outer template parameters. */
20015 TREE_VEC_LENGTH (partial_args
)--;
20016 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
20018 pop_access_scope (decl
);
20024 /* Return truthvalue if we're processing a template different from
20025 the last one involved in diagnostics. */
20027 problematic_instantiation_changed (void)
20029 return current_tinst_level
!= last_error_tinst_level
;
20032 /* Remember current template involved in diagnostics. */
20034 record_last_problematic_instantiation (void)
20036 last_error_tinst_level
= current_tinst_level
;
20039 struct tinst_level
*
20040 current_instantiation (void)
20042 return current_tinst_level
;
20045 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20046 type. Return zero for ok, nonzero for disallowed. Issue error and
20047 warning messages under control of COMPLAIN. */
20050 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
20052 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
20054 else if (POINTER_TYPE_P (type
))
20056 else if (TYPE_PTRMEM_P (type
))
20058 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
20060 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
20062 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
20064 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
20067 if (complain
& tf_error
)
20069 if (type
== error_mark_node
)
20070 inform (input_location
, "invalid template non-type parameter");
20072 error ("%q#T is not a valid type for a template non-type parameter",
20078 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20079 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20082 dependent_type_p_r (tree type
)
20088 A type is dependent if it is:
20090 -- a template parameter. Template template parameters are types
20091 for us (since TYPE_P holds true for them) so we handle
20093 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20094 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
20096 /* -- a qualified-id with a nested-name-specifier which contains a
20097 class-name that names a dependent type or whose unqualified-id
20098 names a dependent type. */
20099 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20101 /* -- a cv-qualified type where the cv-unqualified type is
20103 type
= TYPE_MAIN_VARIANT (type
);
20104 /* -- a compound type constructed from any dependent type. */
20105 if (TYPE_PTRMEM_P (type
))
20106 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
20107 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20109 else if (TYPE_PTR_P (type
)
20110 || TREE_CODE (type
) == REFERENCE_TYPE
)
20111 return dependent_type_p (TREE_TYPE (type
));
20112 else if (TREE_CODE (type
) == FUNCTION_TYPE
20113 || TREE_CODE (type
) == METHOD_TYPE
)
20117 if (dependent_type_p (TREE_TYPE (type
)))
20119 for (arg_type
= TYPE_ARG_TYPES (type
);
20121 arg_type
= TREE_CHAIN (arg_type
))
20122 if (dependent_type_p (TREE_VALUE (arg_type
)))
20126 /* -- an array type constructed from any dependent type or whose
20127 size is specified by a constant expression that is
20130 We checked for type- and value-dependence of the bounds in
20131 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20132 if (TREE_CODE (type
) == ARRAY_TYPE
)
20134 if (TYPE_DOMAIN (type
)
20135 && dependent_type_p (TYPE_DOMAIN (type
)))
20137 return dependent_type_p (TREE_TYPE (type
));
20140 /* -- a template-id in which either the template name is a template
20142 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20144 /* ... or any of the template arguments is a dependent type or
20145 an expression that is type-dependent or value-dependent. */
20146 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
20147 && (any_dependent_template_arguments_p
20148 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
20151 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20152 dependent; if the argument of the `typeof' expression is not
20153 type-dependent, then it should already been have resolved. */
20154 if (TREE_CODE (type
) == TYPEOF_TYPE
20155 || TREE_CODE (type
) == DECLTYPE_TYPE
20156 || TREE_CODE (type
) == UNDERLYING_TYPE
)
20159 /* A template argument pack is dependent if any of its packed
20161 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
20163 tree args
= ARGUMENT_PACK_ARGS (type
);
20164 int i
, len
= TREE_VEC_LENGTH (args
);
20165 for (i
= 0; i
< len
; ++i
)
20166 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
20170 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20171 be template parameters. */
20172 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
20175 /* The standard does not specifically mention types that are local
20176 to template functions or local classes, but they should be
20177 considered dependent too. For example:
20179 template <int I> void f() {
20184 The size of `E' cannot be known until the value of `I' has been
20185 determined. Therefore, `E' must be considered dependent. */
20186 scope
= TYPE_CONTEXT (type
);
20187 if (scope
&& TYPE_P (scope
))
20188 return dependent_type_p (scope
);
20189 /* Don't use type_dependent_expression_p here, as it can lead
20190 to infinite recursion trying to determine whether a lambda
20191 nested in a lambda is dependent (c++/47687). */
20192 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
20193 && DECL_LANG_SPECIFIC (scope
)
20194 && DECL_TEMPLATE_INFO (scope
)
20195 && (any_dependent_template_arguments_p
20196 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
20199 /* Other types are non-dependent. */
20203 /* Returns TRUE if TYPE is dependent, in the sense of
20204 [temp.dep.type]. Note that a NULL type is considered dependent. */
20207 dependent_type_p (tree type
)
20209 /* If there are no template parameters in scope, then there can't be
20210 any dependent types. */
20211 if (!processing_template_decl
)
20213 /* If we are not processing a template, then nobody should be
20214 providing us with a dependent type. */
20216 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
20220 /* If the type is NULL, we have not computed a type for the entity
20221 in question; in that case, the type is dependent. */
20225 /* Erroneous types can be considered non-dependent. */
20226 if (type
== error_mark_node
)
20229 /* If we have not already computed the appropriate value for TYPE,
20231 if (!TYPE_DEPENDENT_P_VALID (type
))
20233 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
20234 TYPE_DEPENDENT_P_VALID (type
) = 1;
20237 return TYPE_DEPENDENT_P (type
);
20240 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20241 lookup. In other words, a dependent type that is not the current
20245 dependent_scope_p (tree scope
)
20247 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
20248 && !currently_open_class (scope
));
20251 /* T is a SCOPE_REF; return whether we need to consider it
20252 instantiation-dependent so that we can check access at instantiation
20253 time even though we know which member it resolves to. */
20256 instantiation_dependent_scope_ref_p (tree t
)
20258 if (DECL_P (TREE_OPERAND (t
, 1))
20259 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
20260 && accessible_in_template_p (TREE_OPERAND (t
, 0),
20261 TREE_OPERAND (t
, 1)))
20267 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20268 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20271 /* Note that this predicate is not appropriate for general expressions;
20272 only constant expressions (that satisfy potential_constant_expression)
20273 can be tested for value dependence. */
20276 value_dependent_expression_p (tree expression
)
20278 if (!processing_template_decl
)
20281 /* A name declared with a dependent type. */
20282 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
20285 switch (TREE_CODE (expression
))
20287 case IDENTIFIER_NODE
:
20288 /* A name that has not been looked up -- must be dependent. */
20291 case TEMPLATE_PARM_INDEX
:
20292 /* A non-type template parm. */
20296 /* A non-type template parm. */
20297 if (DECL_TEMPLATE_PARM_P (expression
))
20299 return value_dependent_expression_p (DECL_INITIAL (expression
));
20302 /* A constant with literal type and is initialized
20303 with an expression that is value-dependent.
20305 Note that a non-dependent parenthesized initializer will have
20306 already been replaced with its constant value, so if we see
20307 a TREE_LIST it must be dependent. */
20308 if (DECL_INITIAL (expression
)
20309 && decl_constant_var_p (expression
)
20310 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
20311 || value_dependent_expression_p (DECL_INITIAL (expression
))))
20315 case DYNAMIC_CAST_EXPR
:
20316 case STATIC_CAST_EXPR
:
20317 case CONST_CAST_EXPR
:
20318 case REINTERPRET_CAST_EXPR
:
20320 /* These expressions are value-dependent if the type to which
20321 the cast occurs is dependent or the expression being casted
20322 is value-dependent. */
20324 tree type
= TREE_TYPE (expression
);
20326 if (dependent_type_p (type
))
20329 /* A functional cast has a list of operands. */
20330 expression
= TREE_OPERAND (expression
, 0);
20333 /* If there are no operands, it must be an expression such
20334 as "int()". This should not happen for aggregate types
20335 because it would form non-constant expressions. */
20336 gcc_assert (cxx_dialect
>= cxx11
20337 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
20342 if (TREE_CODE (expression
) == TREE_LIST
)
20343 return any_value_dependent_elements_p (expression
);
20345 return value_dependent_expression_p (expression
);
20349 if (SIZEOF_EXPR_TYPE_P (expression
))
20350 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
20354 /* A `sizeof' expression is value-dependent if the operand is
20355 type-dependent or is a pack expansion. */
20356 expression
= TREE_OPERAND (expression
, 0);
20357 if (PACK_EXPANSION_P (expression
))
20359 else if (TYPE_P (expression
))
20360 return dependent_type_p (expression
);
20361 return instantiation_dependent_expression_p (expression
);
20363 case AT_ENCODE_EXPR
:
20364 /* An 'encode' expression is value-dependent if the operand is
20366 expression
= TREE_OPERAND (expression
, 0);
20367 return dependent_type_p (expression
);
20369 case NOEXCEPT_EXPR
:
20370 expression
= TREE_OPERAND (expression
, 0);
20371 return instantiation_dependent_expression_p (expression
);
20374 /* All instantiation-dependent expressions should also be considered
20375 value-dependent. */
20376 return instantiation_dependent_scope_ref_p (expression
);
20378 case COMPONENT_REF
:
20379 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
20380 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
20382 case NONTYPE_ARGUMENT_PACK
:
20383 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
20384 is value-dependent. */
20386 tree values
= ARGUMENT_PACK_ARGS (expression
);
20387 int i
, len
= TREE_VEC_LENGTH (values
);
20389 for (i
= 0; i
< len
; ++i
)
20390 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
20398 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
20399 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
20400 || (type2
? dependent_type_p (type2
) : false));
20404 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20405 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
20408 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20409 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
20413 tree op
= TREE_OPERAND (expression
, 0);
20414 return (value_dependent_expression_p (op
)
20415 || has_value_dependent_address (op
));
20420 tree fn
= get_callee_fndecl (expression
);
20422 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
20424 nargs
= call_expr_nargs (expression
);
20425 for (i
= 0; i
< nargs
; ++i
)
20427 tree op
= CALL_EXPR_ARG (expression
, i
);
20428 /* In a call to a constexpr member function, look through the
20429 implicit ADDR_EXPR on the object argument so that it doesn't
20430 cause the call to be considered value-dependent. We also
20431 look through it in potential_constant_expression. */
20432 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
20433 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
20434 && TREE_CODE (op
) == ADDR_EXPR
)
20435 op
= TREE_OPERAND (op
, 0);
20436 if (value_dependent_expression_p (op
))
20442 case TEMPLATE_ID_EXPR
:
20443 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
20445 return type_dependent_expression_p (expression
);
20451 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
20452 if (value_dependent_expression_p (val
))
20458 /* Treat a GNU statement expression as dependent to avoid crashing
20459 under fold_non_dependent_expr; it can't be constant. */
20463 /* A constant expression is value-dependent if any subexpression is
20464 value-dependent. */
20465 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
20467 case tcc_reference
:
20469 case tcc_comparison
:
20471 case tcc_expression
:
20474 int i
, len
= cp_tree_operand_length (expression
);
20476 for (i
= 0; i
< len
; i
++)
20478 tree t
= TREE_OPERAND (expression
, i
);
20480 /* In some cases, some of the operands may be missing.l
20481 (For example, in the case of PREDECREMENT_EXPR, the
20482 amount to increment by may be missing.) That doesn't
20483 make the expression dependent. */
20484 if (t
&& value_dependent_expression_p (t
))
20495 /* The expression is not value-dependent. */
20499 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
20500 [temp.dep.expr]. Note that an expression with no type is
20501 considered dependent. Other parts of the compiler arrange for an
20502 expression with type-dependent subexpressions to have no type, so
20503 this function doesn't have to be fully recursive. */
20506 type_dependent_expression_p (tree expression
)
20508 if (!processing_template_decl
)
20511 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
20514 /* An unresolved name is always dependent. */
20515 if (identifier_p (expression
) || TREE_CODE (expression
) == USING_DECL
)
20518 /* Some expression forms are never type-dependent. */
20519 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
20520 || TREE_CODE (expression
) == SIZEOF_EXPR
20521 || TREE_CODE (expression
) == ALIGNOF_EXPR
20522 || TREE_CODE (expression
) == AT_ENCODE_EXPR
20523 || TREE_CODE (expression
) == NOEXCEPT_EXPR
20524 || TREE_CODE (expression
) == TRAIT_EXPR
20525 || TREE_CODE (expression
) == TYPEID_EXPR
20526 || TREE_CODE (expression
) == DELETE_EXPR
20527 || TREE_CODE (expression
) == VEC_DELETE_EXPR
20528 || TREE_CODE (expression
) == THROW_EXPR
)
20531 /* The types of these expressions depends only on the type to which
20532 the cast occurs. */
20533 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
20534 || TREE_CODE (expression
) == STATIC_CAST_EXPR
20535 || TREE_CODE (expression
) == CONST_CAST_EXPR
20536 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
20537 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
20538 || TREE_CODE (expression
) == CAST_EXPR
)
20539 return dependent_type_p (TREE_TYPE (expression
));
20541 /* The types of these expressions depends only on the type created
20542 by the expression. */
20543 if (TREE_CODE (expression
) == NEW_EXPR
20544 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
20546 /* For NEW_EXPR tree nodes created inside a template, either
20547 the object type itself or a TREE_LIST may appear as the
20549 tree type
= TREE_OPERAND (expression
, 1);
20550 if (TREE_CODE (type
) == TREE_LIST
)
20551 /* This is an array type. We need to check array dimensions
20553 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
20554 || value_dependent_expression_p
20555 (TREE_OPERAND (TREE_VALUE (type
), 1));
20557 return dependent_type_p (type
);
20560 if (TREE_CODE (expression
) == SCOPE_REF
)
20562 tree scope
= TREE_OPERAND (expression
, 0);
20563 tree name
= TREE_OPERAND (expression
, 1);
20565 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
20566 contains an identifier associated by name lookup with one or more
20567 declarations declared with a dependent type, or...a
20568 nested-name-specifier or qualified-id that names a member of an
20569 unknown specialization. */
20570 return (type_dependent_expression_p (name
)
20571 || dependent_scope_p (scope
));
20574 if (TREE_CODE (expression
) == FUNCTION_DECL
20575 && DECL_LANG_SPECIFIC (expression
)
20576 && DECL_TEMPLATE_INFO (expression
)
20577 && (any_dependent_template_arguments_p
20578 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
20581 if (TREE_CODE (expression
) == TEMPLATE_DECL
20582 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
20585 if (TREE_CODE (expression
) == STMT_EXPR
)
20586 expression
= stmt_expr_value_expr (expression
);
20588 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
20593 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
20595 if (type_dependent_expression_p (elt
))
20601 /* A static data member of the current instantiation with incomplete
20602 array type is type-dependent, as the definition and specializations
20603 can have different bounds. */
20604 if (VAR_P (expression
)
20605 && DECL_CLASS_SCOPE_P (expression
)
20606 && dependent_type_p (DECL_CONTEXT (expression
))
20607 && VAR_HAD_UNKNOWN_BOUND (expression
))
20610 /* An array of unknown bound depending on a variadic parameter, eg:
20612 template<typename... Args>
20613 void foo (Args... args)
20615 int arr[] = { args... };
20618 template<int... vals>
20621 int arr[] = { vals... };
20624 If the array has no length and has an initializer, it must be that
20625 we couldn't determine its length in cp_complete_array_type because
20626 it is dependent. */
20627 if (VAR_P (expression
)
20628 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
20629 && !TYPE_DOMAIN (TREE_TYPE (expression
))
20630 && DECL_INITIAL (expression
))
20633 if (TREE_TYPE (expression
) == unknown_type_node
)
20635 if (TREE_CODE (expression
) == ADDR_EXPR
)
20636 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
20637 if (TREE_CODE (expression
) == COMPONENT_REF
20638 || TREE_CODE (expression
) == OFFSET_REF
)
20640 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20642 expression
= TREE_OPERAND (expression
, 1);
20643 if (identifier_p (expression
))
20646 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
20647 if (TREE_CODE (expression
) == SCOPE_REF
)
20650 /* Always dependent, on the number of arguments if nothing else. */
20651 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
20654 if (BASELINK_P (expression
))
20655 expression
= BASELINK_FUNCTIONS (expression
);
20657 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
20659 if (any_dependent_template_arguments_p
20660 (TREE_OPERAND (expression
, 1)))
20662 expression
= TREE_OPERAND (expression
, 0);
20664 gcc_assert (TREE_CODE (expression
) == OVERLOAD
20665 || TREE_CODE (expression
) == FUNCTION_DECL
);
20669 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
20671 expression
= OVL_NEXT (expression
);
20676 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
20678 return (dependent_type_p (TREE_TYPE (expression
)));
20681 /* walk_tree callback function for instantiation_dependent_expression_p,
20682 below. Returns non-zero if a dependent subexpression is found. */
20685 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
20690 /* We don't have to worry about decltype currently because decltype
20691 of an instantiation-dependent expr is a dependent type. This
20692 might change depending on the resolution of DR 1172. */
20693 *walk_subtrees
= false;
20696 enum tree_code code
= TREE_CODE (*tp
);
20699 /* Don't treat an argument list as dependent just because it has no
20707 /* A constant with a dependent initializer is dependent. */
20708 if (value_dependent_expression_p (*tp
))
20712 case TEMPLATE_PARM_INDEX
:
20715 /* Handle expressions with type operands. */
20719 case AT_ENCODE_EXPR
:
20721 tree op
= TREE_OPERAND (*tp
, 0);
20722 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
20723 op
= TREE_TYPE (op
);
20726 if (dependent_type_p (op
))
20730 *walk_subtrees
= false;
20738 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp
))
20739 || (TRAIT_EXPR_TYPE2 (*tp
)
20740 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp
))))
20742 *walk_subtrees
= false;
20745 case COMPONENT_REF
:
20746 if (identifier_p (TREE_OPERAND (*tp
, 1)))
20747 /* In a template, finish_class_member_access_expr creates a
20748 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
20749 type-dependent, so that we can check access control at
20750 instantiation time (PR 42277). See also Core issue 1273. */
20755 if (instantiation_dependent_scope_ref_p (*tp
))
20760 /* Treat statement-expressions as dependent. */
20768 if (type_dependent_expression_p (*tp
))
20774 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
20775 sense defined by the ABI:
20777 "An expression is instantiation-dependent if it is type-dependent
20778 or value-dependent, or it has a subexpression that is type-dependent
20779 or value-dependent." */
20782 instantiation_dependent_expression_p (tree expression
)
20786 if (!processing_template_decl
)
20789 if (expression
== error_mark_node
)
20792 result
= cp_walk_tree_without_duplicates (&expression
,
20793 instantiation_dependent_r
, NULL
);
20794 return result
!= NULL_TREE
;
20797 /* Like type_dependent_expression_p, but it also works while not processing
20798 a template definition, i.e. during substitution or mangling. */
20801 type_dependent_expression_p_push (tree expr
)
20804 ++processing_template_decl
;
20805 b
= type_dependent_expression_p (expr
);
20806 --processing_template_decl
;
20810 /* Returns TRUE if ARGS contains a type-dependent expression. */
20813 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
20818 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
20820 if (type_dependent_expression_p (arg
))
20826 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20827 expressions) contains any type-dependent expressions. */
20830 any_type_dependent_elements_p (const_tree list
)
20832 for (; list
; list
= TREE_CHAIN (list
))
20833 if (type_dependent_expression_p (TREE_VALUE (list
)))
20839 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20840 expressions) contains any value-dependent expressions. */
20843 any_value_dependent_elements_p (const_tree list
)
20845 for (; list
; list
= TREE_CHAIN (list
))
20846 if (value_dependent_expression_p (TREE_VALUE (list
)))
20852 /* Returns TRUE if the ARG (a template argument) is dependent. */
20855 dependent_template_arg_p (tree arg
)
20857 if (!processing_template_decl
)
20860 /* Assume a template argument that was wrongly written by the user
20861 is dependent. This is consistent with what
20862 any_dependent_template_arguments_p [that calls this function]
20864 if (!arg
|| arg
== error_mark_node
)
20867 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
20868 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
20870 if (TREE_CODE (arg
) == TEMPLATE_DECL
20871 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
20872 return dependent_template_p (arg
);
20873 else if (ARGUMENT_PACK_P (arg
))
20875 tree args
= ARGUMENT_PACK_ARGS (arg
);
20876 int i
, len
= TREE_VEC_LENGTH (args
);
20877 for (i
= 0; i
< len
; ++i
)
20879 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
20885 else if (TYPE_P (arg
))
20886 return dependent_type_p (arg
);
20888 return (type_dependent_expression_p (arg
)
20889 || value_dependent_expression_p (arg
));
20892 /* Returns true if ARGS (a collection of template arguments) contains
20893 any types that require structural equality testing. */
20896 any_template_arguments_need_structural_equality_p (tree args
)
20903 if (args
== error_mark_node
)
20906 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
20908 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
20909 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
20911 tree arg
= TREE_VEC_ELT (level
, j
);
20912 tree packed_args
= NULL_TREE
;
20915 if (ARGUMENT_PACK_P (arg
))
20917 /* Look inside the argument pack. */
20918 packed_args
= ARGUMENT_PACK_ARGS (arg
);
20919 len
= TREE_VEC_LENGTH (packed_args
);
20922 for (k
= 0; k
< len
; ++k
)
20925 arg
= TREE_VEC_ELT (packed_args
, k
);
20927 if (error_operand_p (arg
))
20929 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
20931 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
20933 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
20934 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
20943 /* Returns true if ARGS (a collection of template arguments) contains
20944 any dependent arguments. */
20947 any_dependent_template_arguments_p (const_tree args
)
20954 if (args
== error_mark_node
)
20957 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
20959 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
20960 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
20961 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
20968 /* Returns TRUE if the template TMPL is dependent. */
20971 dependent_template_p (tree tmpl
)
20973 if (TREE_CODE (tmpl
) == OVERLOAD
)
20977 if (dependent_template_p (OVL_CURRENT (tmpl
)))
20979 tmpl
= OVL_NEXT (tmpl
);
20984 /* Template template parameters are dependent. */
20985 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
20986 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
20988 /* So are names that have not been looked up. */
20989 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
20991 /* So are member templates of dependent classes. */
20992 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
20993 return dependent_type_p (DECL_CONTEXT (tmpl
));
20997 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21000 dependent_template_id_p (tree tmpl
, tree args
)
21002 return (dependent_template_p (tmpl
)
21003 || any_dependent_template_arguments_p (args
));
21006 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21010 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
21014 if (!processing_template_decl
)
21017 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
21019 tree decl
= TREE_VEC_ELT (declv
, i
);
21020 tree init
= TREE_VEC_ELT (initv
, i
);
21021 tree cond
= TREE_VEC_ELT (condv
, i
);
21022 tree incr
= TREE_VEC_ELT (incrv
, i
);
21024 if (type_dependent_expression_p (decl
))
21027 if (init
&& type_dependent_expression_p (init
))
21030 if (type_dependent_expression_p (cond
))
21033 if (COMPARISON_CLASS_P (cond
)
21034 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
21035 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
21038 if (TREE_CODE (incr
) == MODOP_EXPR
)
21040 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
21041 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
21044 else if (type_dependent_expression_p (incr
))
21046 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
21048 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
21050 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
21052 tree t
= TREE_OPERAND (incr
, 1);
21053 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
21054 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
21063 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21064 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21065 no such TYPE can be found. Note that this function peers inside
21066 uninstantiated templates and therefore should be used only in
21067 extremely limited situations. ONLY_CURRENT_P restricts this
21068 peering to the currently open classes hierarchy (which is required
21069 when comparing types). */
21072 resolve_typename_type (tree type
, bool only_current_p
)
21081 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
21083 scope
= TYPE_CONTEXT (type
);
21084 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21085 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21086 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21087 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21088 identifier of the TYPENAME_TYPE anymore.
21089 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21090 TYPENAME_TYPE instead, we avoid messing up with a possible
21091 typedef variant case. */
21092 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
21094 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21095 it first before we can figure out what NAME refers to. */
21096 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
21098 if (TYPENAME_IS_RESOLVING_P (scope
))
21099 /* Given a class template A with a dependent base with nested type C,
21100 typedef typename A::C::C C will land us here, as trying to resolve
21101 the initial A::C leads to the local C typedef, which leads back to
21102 A::C::C. So we break the recursion now. */
21105 scope
= resolve_typename_type (scope
, only_current_p
);
21107 /* If we don't know what SCOPE refers to, then we cannot resolve the
21109 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
21111 /* If the SCOPE is a template type parameter, we have no way of
21112 resolving the name. */
21113 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
21115 /* If the SCOPE is not the current instantiation, there's no reason
21116 to look inside it. */
21117 if (only_current_p
&& !currently_open_class (scope
))
21119 /* If this is a typedef, we don't want to look inside (c++/11987). */
21120 if (typedef_variant_p (type
))
21122 /* If SCOPE isn't the template itself, it will not have a valid
21123 TYPE_FIELDS list. */
21124 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
21125 /* scope is either the template itself or a compatible instantiation
21126 like X<T>, so look up the name in the original template. */
21127 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
21129 /* scope is a partial instantiation, so we can't do the lookup or we
21130 will lose the template arguments. */
21132 /* Enter the SCOPE so that name lookup will be resolved as if we
21133 were in the class definition. In particular, SCOPE will no
21134 longer be considered a dependent type. */
21135 pushed_scope
= push_scope (scope
);
21136 /* Look up the declaration. */
21137 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
21138 tf_warning_or_error
);
21140 result
= NULL_TREE
;
21142 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21143 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21146 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type
))
21147 && TREE_CODE (decl
) == TYPE_DECL
)
21149 result
= TREE_TYPE (decl
);
21150 if (result
== error_mark_node
)
21151 result
= NULL_TREE
;
21153 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
21154 && DECL_CLASS_TEMPLATE_P (decl
))
21158 /* Obtain the template and the arguments. */
21159 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
21160 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
21161 /* Instantiate the template. */
21162 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
21163 /*entering_scope=*/0,
21164 tf_error
| tf_user
);
21165 if (result
== error_mark_node
)
21166 result
= NULL_TREE
;
21169 /* Leave the SCOPE. */
21171 pop_scope (pushed_scope
);
21173 /* If we failed to resolve it, return the original typename. */
21177 /* If lookup found a typename type, resolve that too. */
21178 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
21180 /* Ill-formed programs can cause infinite recursion here, so we
21181 must catch that. */
21182 TYPENAME_IS_RESOLVING_P (type
) = 1;
21183 result
= resolve_typename_type (result
, only_current_p
);
21184 TYPENAME_IS_RESOLVING_P (type
) = 0;
21187 /* Qualify the resulting type. */
21188 quals
= cp_type_quals (type
);
21190 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
21195 /* EXPR is an expression which is not type-dependent. Return a proxy
21196 for EXPR that can be used to compute the types of larger
21197 expressions containing EXPR. */
21200 build_non_dependent_expr (tree expr
)
21204 #ifdef ENABLE_CHECKING
21205 /* Try to get a constant value for all non-dependent expressions in
21206 order to expose bugs in *_dependent_expression_p and constexpr. */
21207 if (cxx_dialect
>= cxx11
)
21208 maybe_constant_value (fold_non_dependent_expr_sfinae (expr
, tf_none
));
21211 /* Preserve OVERLOADs; the functions must be available to resolve
21214 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
21215 inner_expr
= stmt_expr_value_expr (inner_expr
);
21216 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
21217 inner_expr
= TREE_OPERAND (inner_expr
, 0);
21218 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
21219 inner_expr
= TREE_OPERAND (inner_expr
, 1);
21220 if (is_overloaded_fn (inner_expr
)
21221 || TREE_CODE (inner_expr
) == OFFSET_REF
)
21223 /* There is no need to return a proxy for a variable. */
21226 /* Preserve string constants; conversions from string constants to
21227 "char *" are allowed, even though normally a "const char *"
21228 cannot be used to initialize a "char *". */
21229 if (TREE_CODE (expr
) == STRING_CST
)
21231 /* Preserve arithmetic constants, as an optimization -- there is no
21232 reason to create a new node. */
21233 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
21235 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21236 There is at least one place where we want to know that a
21237 particular expression is a throw-expression: when checking a ?:
21238 expression, there are special rules if the second or third
21239 argument is a throw-expression. */
21240 if (TREE_CODE (expr
) == THROW_EXPR
)
21243 /* Don't wrap an initializer list, we need to be able to look inside. */
21244 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
21247 /* Don't wrap a dummy object, we need to be able to test for it. */
21248 if (is_dummy_object (expr
))
21251 if (TREE_CODE (expr
) == COND_EXPR
)
21252 return build3 (COND_EXPR
,
21254 TREE_OPERAND (expr
, 0),
21255 (TREE_OPERAND (expr
, 1)
21256 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
21257 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
21258 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
21259 if (TREE_CODE (expr
) == COMPOUND_EXPR
21260 && !COMPOUND_EXPR_OVERLOADED (expr
))
21261 return build2 (COMPOUND_EXPR
,
21263 TREE_OPERAND (expr
, 0),
21264 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
21266 /* If the type is unknown, it can't really be non-dependent */
21267 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
21269 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21270 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
21273 /* ARGS is a vector of expressions as arguments to a function call.
21274 Replace the arguments with equivalent non-dependent expressions.
21275 This modifies ARGS in place. */
21278 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
21283 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
21285 tree newarg
= build_non_dependent_expr (arg
);
21287 (*args
)[ix
] = newarg
;
21291 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21292 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21296 make_auto_1 (tree name
)
21298 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
21299 TYPE_NAME (au
) = build_decl (input_location
,
21300 TYPE_DECL
, name
, au
);
21301 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
21302 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
21303 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
21304 TYPE_NAME (au
), NULL_TREE
);
21305 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
21306 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
21307 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
21313 make_decltype_auto (void)
21315 return make_auto_1 (get_identifier ("decltype(auto)"));
21321 return make_auto_1 (get_identifier ("auto"));
21324 /* Given type ARG, return std::initializer_list<ARG>. */
21329 tree std_init_list
= namespace_binding
21330 (get_identifier ("initializer_list"), std_node
);
21332 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
21334 error ("deducing from brace-enclosed initializer list requires "
21335 "#include <initializer_list>");
21336 return error_mark_node
;
21338 argvec
= make_tree_vec (1);
21339 TREE_VEC_ELT (argvec
, 0) = arg
;
21340 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
21341 NULL_TREE
, 0, tf_warning_or_error
);
21344 /* Replace auto in TYPE with std::initializer_list<auto>. */
21347 listify_autos (tree type
, tree auto_node
)
21349 tree init_auto
= listify (auto_node
);
21350 tree argvec
= make_tree_vec (1);
21351 TREE_VEC_ELT (argvec
, 0) = init_auto
;
21352 if (processing_template_decl
)
21353 argvec
= add_to_template_args (current_template_args (), argvec
);
21354 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
21357 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
21358 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
21361 do_auto_deduction (tree type
, tree init
, tree auto_node
)
21365 if (init
== error_mark_node
)
21366 return error_mark_node
;
21368 if (type_dependent_expression_p (init
))
21369 /* Defining a subset of type-dependent expressions that we can deduce
21370 from ahead of time isn't worth the trouble. */
21373 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
21374 with either a new invented type template parameter U or, if the
21375 initializer is a braced-init-list (8.5.4), with
21376 std::initializer_list<U>. */
21377 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
21378 type
= listify_autos (type
, auto_node
);
21380 init
= resolve_nondeduced_context (init
);
21382 targs
= make_tree_vec (1);
21383 if (AUTO_IS_DECLTYPE (auto_node
))
21385 bool id
= (DECL_P (init
) || TREE_CODE (init
) == COMPONENT_REF
);
21386 TREE_VEC_ELT (targs
, 0)
21387 = finish_decltype_type (init
, id
, tf_warning_or_error
);
21388 if (type
!= auto_node
)
21390 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
21391 return error_mark_node
;
21396 tree parms
= build_tree_list (NULL_TREE
, type
);
21397 tree tparms
= make_tree_vec (1);
21400 TREE_VEC_ELT (tparms
, 0)
21401 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
21402 val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
21403 DEDUCE_CALL
, LOOKUP_NORMAL
,
21404 NULL
, /*explain_p=*/false);
21407 if (processing_template_decl
)
21408 /* Try again at instantiation time. */
21410 if (type
&& type
!= error_mark_node
)
21411 /* If type is error_mark_node a diagnostic must have been
21412 emitted by now. Also, having a mention to '<type error>'
21413 in the diagnostic is not really useful to the user. */
21415 if (cfun
&& auto_node
== current_function_auto_return_pattern
21416 && LAMBDA_FUNCTION_P (current_function_decl
))
21417 error ("unable to deduce lambda return type from %qE", init
);
21419 error ("unable to deduce %qT from %qE", type
, init
);
21421 return error_mark_node
;
21425 /* If the list of declarators contains more than one declarator, the type
21426 of each declared variable is determined as described above. If the
21427 type deduced for the template parameter U is not the same in each
21428 deduction, the program is ill-formed. */
21429 if (TREE_TYPE (auto_node
)
21430 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
21432 if (cfun
&& auto_node
== current_function_auto_return_pattern
21433 && LAMBDA_FUNCTION_P (current_function_decl
))
21434 error ("inconsistent types %qT and %qT deduced for "
21435 "lambda return type", TREE_TYPE (auto_node
),
21436 TREE_VEC_ELT (targs
, 0));
21438 error ("inconsistent deduction for %qT: %qT and then %qT",
21439 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
21440 return error_mark_node
;
21442 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
21444 if (processing_template_decl
)
21445 targs
= add_to_template_args (current_template_args (), targs
);
21446 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
21449 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
21453 splice_late_return_type (tree type
, tree late_return_type
)
21457 if (late_return_type
== NULL_TREE
)
21459 argvec
= make_tree_vec (1);
21460 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
21461 if (processing_template_parmlist
)
21462 /* For a late-specified return type in a template type-parameter, we
21463 need to add a dummy argument level for its parmlist. */
21464 argvec
= add_to_template_args
21465 (make_tree_vec (processing_template_parmlist
), argvec
);
21466 if (current_template_parms
)
21467 argvec
= add_to_template_args (current_template_args (), argvec
);
21468 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
21471 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
21472 'decltype(auto)'. */
21475 is_auto (const_tree type
)
21477 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21478 && (TYPE_IDENTIFIER (type
) == get_identifier ("auto")
21479 || TYPE_IDENTIFIER (type
) == get_identifier ("decltype(auto)")))
21485 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
21486 a use of `auto'. Returns NULL_TREE otherwise. */
21489 type_uses_auto (tree type
)
21491 return find_type_usage (type
, is_auto
);
21494 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
21495 'decltype(auto)' or a concept. */
21498 is_auto_or_concept (const_tree type
)
21500 return is_auto (type
); // or concept
21503 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
21504 a concept identifier) iff TYPE contains a use of a generic type. Returns
21505 NULL_TREE otherwise. */
21508 type_uses_auto_or_concept (tree type
)
21510 return find_type_usage (type
, is_auto_or_concept
);
21514 /* For a given template T, return the vector of typedefs referenced
21515 in T for which access check is needed at T instantiation time.
21516 T is either a FUNCTION_DECL or a RECORD_TYPE.
21517 Those typedefs were added to T by the function
21518 append_type_to_template_for_access_check. */
21520 vec
<qualified_typedef_usage_t
, va_gc
> *
21521 get_types_needing_access_check (tree t
)
21524 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
21526 if (!t
|| t
== error_mark_node
)
21529 if (!(ti
= get_template_info (t
)))
21532 if (CLASS_TYPE_P (t
)
21533 || TREE_CODE (t
) == FUNCTION_DECL
)
21535 if (!TI_TEMPLATE (ti
))
21538 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
21544 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
21545 tied to T. That list of typedefs will be access checked at
21546 T instantiation time.
21547 T is either a FUNCTION_DECL or a RECORD_TYPE.
21548 TYPE_DECL is a TYPE_DECL node representing a typedef.
21549 SCOPE is the scope through which TYPE_DECL is accessed.
21550 LOCATION is the location of the usage point of TYPE_DECL.
21552 This function is a subroutine of
21553 append_type_to_template_for_access_check. */
21556 append_type_to_template_for_access_check_1 (tree t
,
21559 location_t location
)
21561 qualified_typedef_usage_t typedef_usage
;
21564 if (!t
|| t
== error_mark_node
)
21567 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
21568 || CLASS_TYPE_P (t
))
21570 && TREE_CODE (type_decl
) == TYPE_DECL
21573 if (!(ti
= get_template_info (t
)))
21576 gcc_assert (TI_TEMPLATE (ti
));
21578 typedef_usage
.typedef_decl
= type_decl
;
21579 typedef_usage
.context
= scope
;
21580 typedef_usage
.locus
= location
;
21582 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
21585 /* Append TYPE_DECL to the template TEMPL.
21586 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
21587 At TEMPL instanciation time, TYPE_DECL will be checked to see
21588 if it can be accessed through SCOPE.
21589 LOCATION is the location of the usage point of TYPE_DECL.
21591 e.g. consider the following code snippet:
21598 template<class U> struct S
21600 C::myint mi; // <-- usage point of the typedef C::myint
21605 At S<char> instantiation time, we need to check the access of C::myint
21606 In other words, we need to check the access of the myint typedef through
21607 the C scope. For that purpose, this function will add the myint typedef
21608 and the scope C through which its being accessed to a list of typedefs
21609 tied to the template S. That list will be walked at template instantiation
21610 time and access check performed on each typedefs it contains.
21611 Note that this particular code snippet should yield an error because
21612 myint is private to C. */
21615 append_type_to_template_for_access_check (tree templ
,
21618 location_t location
)
21620 qualified_typedef_usage_t
*iter
;
21623 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
21625 /* Make sure we don't append the type to the template twice. */
21626 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
21627 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
21630 append_type_to_template_for_access_check_1 (templ
, type_decl
,
21634 /* Convert the generic type parameters in PARM that match the types given in the
21635 range [START_IDX, END_IDX) from the current_template_parms into generic type
21639 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
21641 tree current
= current_template_parms
;
21642 int depth
= TMPL_PARMS_DEPTH (current
);
21643 current
= INNERMOST_TEMPLATE_PARMS (current
);
21644 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
21646 for (int i
= 0; i
< start_idx
; ++i
)
21647 TREE_VEC_ELT (replacement
, i
)
21648 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
21650 for (int i
= start_idx
; i
< end_idx
; ++i
)
21652 /* Create a distinct parameter pack type from the current parm and add it
21653 to the replacement args to tsubst below into the generic function
21656 tree o
= TREE_TYPE (TREE_VALUE
21657 (TREE_VEC_ELT (current
, i
)));
21658 tree t
= copy_type (o
);
21659 TEMPLATE_TYPE_PARM_INDEX (t
)
21660 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
21662 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
21663 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
21664 TYPE_MAIN_VARIANT (t
) = t
;
21665 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
21666 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
21667 TREE_VEC_ELT (replacement
, i
) = t
;
21668 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
21671 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
21672 TREE_VEC_ELT (replacement
, i
)
21673 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
21675 /* If there are more levels then build up the replacement with the outer
21678 replacement
= add_to_template_args (template_parms_to_args
21679 (TREE_CHAIN (current_template_parms
)),
21682 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
21686 /* Set up the hash tables for template instantiations. */
21689 init_template_processing (void)
21691 decl_specializations
= htab_create_ggc (37,
21692 hash_specialization
,
21693 eq_specializations
,
21695 type_specializations
= htab_create_ggc (37,
21696 hash_specialization
,
21697 eq_specializations
,
21701 /* Print stats about the template hash tables for -fstats. */
21704 print_template_statistics (void)
21706 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
21707 "%f collisions\n", (long) htab_size (decl_specializations
),
21708 (long) htab_elements (decl_specializations
),
21709 htab_collisions (decl_specializations
));
21710 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
21711 "%f collisions\n", (long) htab_size (type_specializations
),
21712 (long) htab_elements (type_specializations
),
21713 htab_collisions (type_specializations
));
21716 #include "gt-cp-pt.h"