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"
48 /* The type of functions taking a tree, and some additional data, and
50 typedef int (*tree_fn_t
) (tree
, void*);
52 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
53 instantiations have been deferred, either because their definitions
54 were not yet available, or because we were putting off doing the work. */
55 struct GTY ((chain_next ("%h.next"))) pending_template
{
56 struct pending_template
*next
;
57 struct tinst_level
*tinst
;
60 static GTY(()) struct pending_template
*pending_templates
;
61 static GTY(()) struct pending_template
*last_pending_template
;
63 int processing_template_parmlist
;
64 static int template_header_count
;
66 static GTY(()) tree saved_trees
;
67 static vec
<int> inline_parm_levels
;
69 static GTY(()) struct tinst_level
*current_tinst_level
;
71 static GTY(()) tree saved_access_scope
;
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr
;
78 /* A map from local variable declarations in the body of the template
79 presently being instantiated to the corresponding instantiated
81 static struct pointer_map_t
*local_specializations
;
83 /* True if we've recursed into fn_type_unification too many times. */
84 static bool excessive_deduction_depth
;
86 typedef struct GTY(()) spec_entry
93 static GTY ((param_is (spec_entry
)))
94 htab_t decl_specializations
;
96 static GTY ((param_is (spec_entry
)))
97 htab_t type_specializations
;
99 /* Contains canonical template parameter types. The vector is indexed by
100 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
101 TREE_LIST, whose TREE_VALUEs contain the canonical template
102 parameters of various types and levels. */
103 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
105 #define UNIFY_ALLOW_NONE 0
106 #define UNIFY_ALLOW_MORE_CV_QUAL 1
107 #define UNIFY_ALLOW_LESS_CV_QUAL 2
108 #define UNIFY_ALLOW_DERIVED 4
109 #define UNIFY_ALLOW_INTEGER 8
110 #define UNIFY_ALLOW_OUTER_LEVEL 16
111 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
112 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
114 enum template_base_result
{
116 tbr_ambiguous_baseclass
,
120 static void push_access_scope (tree
);
121 static void pop_access_scope (tree
);
122 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
123 unification_kind_t
, int,
125 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
126 unification_kind_t
, int, bool, bool);
127 static int unify (tree
, tree
, tree
, tree
, int, bool);
128 static void add_pending_template (tree
);
129 static tree
reopen_tinst_level (struct tinst_level
*);
130 static tree
tsubst_initializer_list (tree
, tree
);
131 static tree
get_class_bindings (tree
, tree
, tree
, tree
);
132 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
134 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
136 static void tsubst_enum (tree
, tree
, tree
);
137 static tree
add_to_template_args (tree
, tree
);
138 static tree
add_outermost_template_args (tree
, tree
);
139 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
140 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
142 static int type_unification_real (tree
, tree
, tree
, const tree
*,
143 unsigned int, int, unification_kind_t
, int,
144 vec
<deferred_access_check
, va_gc
> **,
146 static void note_template_header (int);
147 static tree
convert_nontype_argument_function (tree
, tree
);
148 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
149 static tree
convert_template_argument (tree
, tree
, tree
,
150 tsubst_flags_t
, int, tree
);
151 static int for_each_template_parm (tree
, tree_fn_t
, void*,
152 struct pointer_set_t
*, bool);
153 static tree
expand_template_argument_pack (tree
);
154 static tree
build_template_parm_index (int, int, int, tree
, tree
);
155 static bool inline_needs_template_parms (tree
, bool);
156 static void push_inline_template_parms_recursive (tree
, int);
157 static tree
retrieve_local_specialization (tree
);
158 static void register_local_specialization (tree
, tree
);
159 static hashval_t
hash_specialization (const void *p
);
160 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
161 static int mark_template_parm (tree
, void *);
162 static int template_parm_this_level_p (tree
, void *);
163 static tree
tsubst_friend_function (tree
, tree
);
164 static tree
tsubst_friend_class (tree
, tree
);
165 static int can_complete_type_without_circularity (tree
);
166 static tree
get_bindings (tree
, tree
, tree
, bool);
167 static int template_decl_level (tree
);
168 static int check_cv_quals_for_unify (int, tree
, tree
);
169 static void template_parm_level_and_index (tree
, int*, int*);
170 static int unify_pack_expansion (tree
, tree
, tree
,
171 tree
, unification_kind_t
, bool, bool);
172 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
173 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
174 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
175 static void regenerate_decl_from_template (tree
, tree
);
176 static tree
most_specialized_class (tree
, tree
, tsubst_flags_t
);
177 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
178 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
179 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
180 static bool check_specialization_scope (void);
181 static tree
process_partial_specialization (tree
);
182 static void set_current_access_from_decl (tree
);
183 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
185 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
186 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
188 static bool template_template_parm_bindings_ok_p (tree
, tree
);
189 static int template_args_equal (tree
, tree
);
190 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
191 static tree
for_each_template_parm_r (tree
*, int *, void *);
192 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
193 static void copy_default_args_to_explicit_spec (tree
);
194 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
195 static bool dependent_template_arg_p (tree
);
196 static bool any_template_arguments_need_structural_equality_p (tree
);
197 static bool dependent_type_p_r (tree
);
198 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
, bool);
199 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
200 static tree
tsubst_pack_expansion (tree
, tree
, tsubst_flags_t
, tree
);
201 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
202 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
203 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
205 static tree
listify (tree
);
206 static tree
listify_autos (tree
, tree
);
207 static tree
template_parm_to_arg (tree t
);
208 static tree
current_template_args (void);
209 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
210 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
212 /* Make the current scope suitable for access checking when we are
213 processing T. T can be FUNCTION_DECL for instantiated function
214 template, VAR_DECL for static member variable, or TYPE_DECL for
215 alias template (needed by instantiate_decl). */
218 push_access_scope (tree t
)
220 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
221 || TREE_CODE (t
) == TYPE_DECL
);
223 if (DECL_FRIEND_CONTEXT (t
))
224 push_nested_class (DECL_FRIEND_CONTEXT (t
));
225 else if (DECL_CLASS_SCOPE_P (t
))
226 push_nested_class (DECL_CONTEXT (t
));
228 push_to_top_level ();
230 if (TREE_CODE (t
) == FUNCTION_DECL
)
232 saved_access_scope
= tree_cons
233 (NULL_TREE
, current_function_decl
, saved_access_scope
);
234 current_function_decl
= t
;
238 /* Restore the scope set up by push_access_scope. T is the node we
242 pop_access_scope (tree t
)
244 if (TREE_CODE (t
) == FUNCTION_DECL
)
246 current_function_decl
= TREE_VALUE (saved_access_scope
);
247 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
250 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
253 pop_from_top_level ();
256 /* Do any processing required when DECL (a member template
257 declaration) is finished. Returns the TEMPLATE_DECL corresponding
258 to DECL, unless it is a specialization, in which case the DECL
259 itself is returned. */
262 finish_member_template_decl (tree decl
)
264 if (decl
== error_mark_node
)
265 return error_mark_node
;
267 gcc_assert (DECL_P (decl
));
269 if (TREE_CODE (decl
) == TYPE_DECL
)
273 type
= TREE_TYPE (decl
);
274 if (type
== error_mark_node
)
275 return error_mark_node
;
276 if (MAYBE_CLASS_TYPE_P (type
)
277 && CLASSTYPE_TEMPLATE_INFO (type
)
278 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
280 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
281 check_member_template (tmpl
);
286 else if (TREE_CODE (decl
) == FIELD_DECL
)
287 error ("data member %qD cannot be a member template", decl
);
288 else if (DECL_TEMPLATE_INFO (decl
))
290 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
292 check_member_template (DECL_TI_TEMPLATE (decl
));
293 return DECL_TI_TEMPLATE (decl
);
299 error ("invalid member template declaration %qD", decl
);
301 return error_mark_node
;
304 /* Create a template info node. */
307 build_template_info (tree template_decl
, tree template_args
)
309 tree result
= make_node (TEMPLATE_INFO
);
310 TI_TEMPLATE (result
) = template_decl
;
311 TI_ARGS (result
) = template_args
;
315 /* Return the template info node corresponding to T, whatever T is. */
318 get_template_info (const_tree t
)
320 tree tinfo
= NULL_TREE
;
322 if (!t
|| t
== error_mark_node
)
325 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
326 tinfo
= DECL_TEMPLATE_INFO (t
);
328 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
331 if (OVERLOAD_TYPE_P (t
))
332 tinfo
= TYPE_TEMPLATE_INFO (t
);
333 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
334 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
339 /* Returns the template nesting level of the indicated class TYPE.
349 A<T>::B<U> has depth two, while A<T> has depth one.
350 Both A<T>::B<int> and A<int>::B<U> have depth one, if
351 they are instantiations, not specializations.
353 This function is guaranteed to return 0 if passed NULL_TREE so
354 that, for example, `template_class_depth (current_class_type)' is
358 template_class_depth (tree type
)
363 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
364 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
365 ? CP_DECL_CONTEXT (type
) : CP_TYPE_CONTEXT (type
))
367 tree tinfo
= get_template_info (type
);
369 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
370 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
377 /* Subroutine of maybe_begin_member_template_processing.
378 Returns true if processing DECL needs us to push template parms. */
381 inline_needs_template_parms (tree decl
, bool nsdmi
)
383 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
386 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
387 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
390 /* Subroutine of maybe_begin_member_template_processing.
391 Push the template parms in PARMS, starting from LEVELS steps into the
392 chain, and ending at the beginning, since template parms are listed
396 push_inline_template_parms_recursive (tree parmlist
, int levels
)
398 tree parms
= TREE_VALUE (parmlist
);
402 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
404 ++processing_template_decl
;
405 current_template_parms
406 = tree_cons (size_int (processing_template_decl
),
407 parms
, current_template_parms
);
408 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
410 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
412 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
414 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
416 if (parm
== error_mark_node
)
419 gcc_assert (DECL_P (parm
));
421 switch (TREE_CODE (parm
))
430 /* Make a CONST_DECL as is done in process_template_parm.
431 It is ugly that we recreate this here; the original
432 version built in process_template_parm is no longer
434 tree decl
= build_decl (DECL_SOURCE_LOCATION (parm
),
435 CONST_DECL
, DECL_NAME (parm
),
437 DECL_ARTIFICIAL (decl
) = 1;
438 TREE_CONSTANT (decl
) = 1;
439 TREE_READONLY (decl
) = 1;
440 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
441 SET_DECL_TEMPLATE_PARM_P (decl
);
452 /* Restore the template parameter context for a member template, a
453 friend template defined in a class definition, or a non-template
454 member of template class. */
457 maybe_begin_member_template_processing (tree decl
)
461 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
464 decl
= (CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
465 ? CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (decl
))
468 if (inline_needs_template_parms (decl
, nsdmi
))
470 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
471 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
473 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
476 parms
= TREE_CHAIN (parms
);
479 push_inline_template_parms_recursive (parms
, levels
);
482 /* Remember how many levels of template parameters we pushed so that
483 we can pop them later. */
484 inline_parm_levels
.safe_push (levels
);
487 /* Undo the effects of maybe_begin_member_template_processing. */
490 maybe_end_member_template_processing (void)
495 if (inline_parm_levels
.length () == 0)
498 last
= inline_parm_levels
.pop ();
499 for (i
= 0; i
< last
; ++i
)
501 --processing_template_decl
;
502 current_template_parms
= TREE_CHAIN (current_template_parms
);
507 /* Return a new template argument vector which contains all of ARGS,
508 but has as its innermost set of arguments the EXTRA_ARGS. */
511 add_to_template_args (tree args
, tree extra_args
)
518 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
521 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
522 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
524 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
525 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
527 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
528 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
533 /* Like add_to_template_args, but only the outermost ARGS are added to
534 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
535 (EXTRA_ARGS) levels are added. This function is used to combine
536 the template arguments from a partial instantiation with the
537 template arguments used to attain the full instantiation from the
538 partial instantiation. */
541 add_outermost_template_args (tree args
, tree extra_args
)
545 /* If there are more levels of EXTRA_ARGS than there are ARGS,
546 something very fishy is going on. */
547 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
549 /* If *all* the new arguments will be the EXTRA_ARGS, just return
551 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
554 /* For the moment, we make ARGS look like it contains fewer levels. */
555 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
557 new_args
= add_to_template_args (args
, extra_args
);
559 /* Now, we restore ARGS to its full dimensions. */
560 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
565 /* Return the N levels of innermost template arguments from the ARGS. */
568 get_innermost_template_args (tree args
, int n
)
576 /* If N is 1, just return the innermost set of template arguments. */
578 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
580 /* If we're not removing anything, just return the arguments we were
582 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
583 gcc_assert (extra_levels
>= 0);
584 if (extra_levels
== 0)
587 /* Make a new set of arguments, not containing the outer arguments. */
588 new_args
= make_tree_vec (n
);
589 for (i
= 1; i
<= n
; ++i
)
590 SET_TMPL_ARGS_LEVEL (new_args
, i
,
591 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
596 /* The inverse of get_innermost_template_args: Return all but the innermost
597 EXTRA_LEVELS levels of template arguments from the ARGS. */
600 strip_innermost_template_args (tree args
, int extra_levels
)
603 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
608 /* If N is 1, just return the outermost set of template arguments. */
610 return TMPL_ARGS_LEVEL (args
, 1);
612 /* If we're not removing anything, just return the arguments we were
614 gcc_assert (extra_levels
>= 0);
615 if (extra_levels
== 0)
618 /* Make a new set of arguments, not containing the inner arguments. */
619 new_args
= make_tree_vec (n
);
620 for (i
= 1; i
<= n
; ++i
)
621 SET_TMPL_ARGS_LEVEL (new_args
, i
,
622 TMPL_ARGS_LEVEL (args
, i
));
627 /* We've got a template header coming up; push to a new level for storing
631 begin_template_parm_list (void)
633 /* We use a non-tag-transparent scope here, which causes pushtag to
634 put tags in this scope, rather than in the enclosing class or
635 namespace scope. This is the right thing, since we want
636 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
637 global template class, push_template_decl handles putting the
638 TEMPLATE_DECL into top-level scope. For a nested template class,
641 template <class T> struct S1 {
642 template <class T> struct S2 {};
645 pushtag contains special code to call pushdecl_with_scope on the
646 TEMPLATE_DECL for S2. */
647 begin_scope (sk_template_parms
, NULL
);
648 ++processing_template_decl
;
649 ++processing_template_parmlist
;
650 note_template_header (0);
653 /* This routine is called when a specialization is declared. If it is
654 invalid to declare a specialization here, an error is reported and
655 false is returned, otherwise this routine will return true. */
658 check_specialization_scope (void)
660 tree scope
= current_scope ();
664 An explicit specialization shall be declared in the namespace of
665 which the template is a member, or, for member templates, in the
666 namespace of which the enclosing class or enclosing class
667 template is a member. An explicit specialization of a member
668 function, member class or static data member of a class template
669 shall be declared in the namespace of which the class template
671 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
673 error ("explicit specialization in non-namespace scope %qD", scope
);
679 In an explicit specialization declaration for a member of a class
680 template or a member template that appears in namespace scope,
681 the member template and some of its enclosing class templates may
682 remain unspecialized, except that the declaration shall not
683 explicitly specialize a class member template if its enclosing
684 class templates are not explicitly specialized as well. */
685 if (current_template_parms
)
687 error ("enclosing class templates are not explicitly specialized");
694 /* We've just seen template <>. */
697 begin_specialization (void)
699 begin_scope (sk_template_spec
, NULL
);
700 note_template_header (1);
701 return check_specialization_scope ();
704 /* Called at then end of processing a declaration preceded by
708 end_specialization (void)
711 reset_specialization ();
714 /* Any template <>'s that we have seen thus far are not referring to a
715 function specialization. */
718 reset_specialization (void)
720 processing_specialization
= 0;
721 template_header_count
= 0;
724 /* We've just seen a template header. If SPECIALIZATION is nonzero,
725 it was of the form template <>. */
728 note_template_header (int specialization
)
730 processing_specialization
= specialization
;
731 template_header_count
++;
734 /* We're beginning an explicit instantiation. */
737 begin_explicit_instantiation (void)
739 gcc_assert (!processing_explicit_instantiation
);
740 processing_explicit_instantiation
= true;
745 end_explicit_instantiation (void)
747 gcc_assert (processing_explicit_instantiation
);
748 processing_explicit_instantiation
= false;
751 /* An explicit specialization or partial specialization of TMPL is being
752 declared. Check that the namespace in which the specialization is
753 occurring is permissible. Returns false iff it is invalid to
754 specialize TMPL in the current namespace. */
757 check_specialization_namespace (tree tmpl
)
759 tree tpl_ns
= decl_namespace_context (tmpl
);
763 An explicit specialization shall be declared in the namespace of
764 which the template is a member, or, for member templates, in the
765 namespace of which the enclosing class or enclosing class
766 template is a member. An explicit specialization of a member
767 function, member class or static data member of a class template
768 shall be declared in the namespace of which the class template is
770 if (current_scope() != DECL_CONTEXT (tmpl
)
771 && !at_namespace_scope_p ())
773 error ("specialization of %qD must appear at namespace scope", tmpl
);
776 if (is_associated_namespace (current_namespace
, tpl_ns
))
777 /* Same or super-using namespace. */
781 permerror (input_location
, "specialization of %qD in different namespace", tmpl
);
782 permerror (input_location
, " from definition of %q+#D", tmpl
);
787 /* SPEC is an explicit instantiation. Check that it is valid to
788 perform this explicit instantiation in the current namespace. */
791 check_explicit_instantiation_namespace (tree spec
)
795 /* DR 275: An explicit instantiation shall appear in an enclosing
796 namespace of its template. */
797 ns
= decl_namespace_context (spec
);
798 if (!is_ancestor (current_namespace
, ns
))
799 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
800 "(which does not enclose namespace %qD)",
801 spec
, current_namespace
, ns
);
804 /* The TYPE is being declared. If it is a template type, that means it
805 is a partial specialization. Do appropriate error-checking. */
808 maybe_process_partial_specialization (tree type
)
812 if (type
== error_mark_node
)
813 return error_mark_node
;
815 /* A lambda that appears in specialization context is not itself a
817 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
820 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
822 error ("name of class shadows template template parameter %qD",
824 return error_mark_node
;
827 context
= TYPE_CONTEXT (type
);
829 if (TYPE_ALIAS_P (type
))
831 if (TYPE_TEMPLATE_INFO (type
)
832 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type
)))
833 error ("specialization of alias template %qD",
834 TYPE_TI_TEMPLATE (type
));
836 error ("explicit specialization of non-template %qT", type
);
837 return error_mark_node
;
839 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
841 /* This is for ordinary explicit specialization and partial
842 specialization of a template class such as:
844 template <> class C<int>;
848 template <class T> class C<T*>;
850 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
852 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
853 && !COMPLETE_TYPE_P (type
))
855 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
));
856 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
857 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
858 if (processing_template_decl
)
860 if (push_template_decl (TYPE_MAIN_DECL (type
))
862 return error_mark_node
;
865 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
866 error ("specialization of %qT after instantiation", type
);
867 else if (errorcount
&& !processing_specialization
868 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
869 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
870 /* Trying to define a specialization either without a template<> header
871 or in an inappropriate place. We've already given an error, so just
872 bail now so we don't actually define the specialization. */
873 return error_mark_node
;
875 else if (CLASS_TYPE_P (type
)
876 && !CLASSTYPE_USE_TEMPLATE (type
)
877 && CLASSTYPE_TEMPLATE_INFO (type
)
878 && context
&& CLASS_TYPE_P (context
)
879 && CLASSTYPE_TEMPLATE_INFO (context
))
881 /* This is for an explicit specialization of member class
882 template according to [temp.expl.spec/18]:
884 template <> template <class U> class C<int>::D;
886 The context `C<int>' must be an implicit instantiation.
887 Otherwise this is just a member class template declared
890 template <> class C<int> { template <class U> class D; };
891 template <> template <class U> class C<int>::D;
893 In the first case, `C<int>::D' is a specialization of `C<T>::D'
894 while in the second case, `C<int>::D' is a primary template
895 and `C<T>::D' may not exist. */
897 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
898 && !COMPLETE_TYPE_P (type
))
901 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
903 if (current_namespace
904 != decl_namespace_context (tmpl
))
906 permerror (input_location
, "specializing %q#T in different namespace", type
);
907 permerror (input_location
, " from definition of %q+#D", tmpl
);
910 /* Check for invalid specialization after instantiation:
912 template <> template <> class C<int>::D<int>;
913 template <> template <class U> class C<int>::D; */
915 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
916 t
; t
= TREE_CHAIN (t
))
918 tree inst
= TREE_VALUE (t
);
919 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
))
921 /* We already have a full specialization of this partial
922 instantiation. Reassign it to the new member
923 specialization template. */
928 elt
.tmpl
= most_general_template (tmpl
);
929 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
932 htab_remove_elt (type_specializations
, &elt
);
935 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
937 slot
= htab_find_slot (type_specializations
, &elt
, INSERT
);
938 entry
= ggc_alloc_spec_entry ();
942 else if (COMPLETE_OR_OPEN_TYPE_P (inst
))
943 /* But if we've had an implicit instantiation, that's a
944 problem ([temp.expl.spec]/6). */
945 error ("specialization %qT after instantiation %qT",
949 /* Mark TYPE as a specialization. And as a result, we only
950 have one level of template argument for the innermost
952 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
953 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
954 CLASSTYPE_TI_ARGS (type
)
955 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
958 else if (processing_specialization
)
960 /* Someday C++0x may allow for enum template specialization. */
961 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
962 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
963 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
964 "of %qD not allowed by ISO C++", type
);
967 error ("explicit specialization of non-template %qT", type
);
968 return error_mark_node
;
975 /* Returns nonzero if we can optimize the retrieval of specializations
976 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
977 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
980 optimize_specialization_lookup_p (tree tmpl
)
982 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
983 && DECL_CLASS_SCOPE_P (tmpl
)
984 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
986 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
987 /* The optimized lookup depends on the fact that the
988 template arguments for the member function template apply
989 purely to the containing class, which is not true if the
990 containing class is an explicit or partial
992 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
993 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
994 && !DECL_CONV_FN_P (tmpl
)
995 /* It is possible to have a template that is not a member
996 template and is not a member of a template class:
998 template <typename T>
999 struct S { friend A::f(); };
1001 Here, the friend function is a template, but the context does
1002 not have template information. The optimized lookup relies
1003 on having ARGS be the template arguments for both the class
1004 and the function template. */
1005 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1008 /* Retrieve the specialization (in the sense of [temp.spec] - a
1009 specialization is either an instantiation or an explicit
1010 specialization) of TMPL for the given template ARGS. If there is
1011 no such specialization, return NULL_TREE. The ARGS are a vector of
1012 arguments, or a vector of vectors of arguments, in the case of
1013 templates with more than one level of parameters.
1015 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1016 then we search for a partial specialization matching ARGS. This
1017 parameter is ignored if TMPL is not a class template.
1019 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1020 result is a NONTYPE_ARGUMENT_PACK. */
1023 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1025 if (tmpl
== NULL_TREE
)
1028 if (args
== error_mark_node
)
1031 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1032 || TREE_CODE (tmpl
) == FIELD_DECL
);
1034 /* There should be as many levels of arguments as there are
1035 levels of parameters. */
1036 gcc_assert (TMPL_ARGS_DEPTH (args
)
1037 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1038 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1039 : template_class_depth (DECL_CONTEXT (tmpl
))));
1041 if (optimize_specialization_lookup_p (tmpl
))
1043 tree class_template
;
1044 tree class_specialization
;
1045 vec
<tree
, va_gc
> *methods
;
1049 /* The template arguments actually apply to the containing
1050 class. Find the class specialization with those
1052 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1053 class_specialization
1054 = retrieve_specialization (class_template
, args
, 0);
1055 if (!class_specialization
)
1057 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1058 for the specialization. */
1059 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1062 /* Iterate through the methods with the indicated name, looking
1063 for the one that has an instance of TMPL. */
1064 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1065 for (fns
= (*methods
)[idx
]; fns
; fns
= OVL_NEXT (fns
))
1067 tree fn
= OVL_CURRENT (fns
);
1068 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1069 /* using-declarations can add base methods to the method vec,
1070 and we don't want those here. */
1071 && DECL_CONTEXT (fn
) == class_specialization
)
1080 htab_t specializations
;
1084 elt
.spec
= NULL_TREE
;
1086 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1087 specializations
= type_specializations
;
1089 specializations
= decl_specializations
;
1092 hash
= hash_specialization (&elt
);
1093 found
= (spec_entry
*) htab_find_with_hash (specializations
, &elt
, hash
);
1101 /* Like retrieve_specialization, but for local declarations. */
1104 retrieve_local_specialization (tree tmpl
)
1108 if (local_specializations
== NULL
)
1111 slot
= pointer_map_contains (local_specializations
, tmpl
);
1112 return slot
? (tree
) *slot
: NULL_TREE
;
1115 /* Returns nonzero iff DECL is a specialization of TMPL. */
1118 is_specialization_of (tree decl
, tree tmpl
)
1122 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1126 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1132 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1134 for (t
= TREE_TYPE (decl
);
1136 t
= CLASSTYPE_USE_TEMPLATE (t
)
1137 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1138 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1145 /* Returns nonzero iff DECL is a specialization of friend declaration
1146 FRIEND_DECL according to [temp.friend]. */
1149 is_specialization_of_friend (tree decl
, tree friend_decl
)
1151 bool need_template
= true;
1154 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1155 || TREE_CODE (decl
) == TYPE_DECL
);
1157 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1158 of a template class, we want to check if DECL is a specialization
1160 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1161 && DECL_TEMPLATE_INFO (friend_decl
)
1162 && !DECL_USE_TEMPLATE (friend_decl
))
1164 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1165 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1166 need_template
= false;
1168 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1169 && !PRIMARY_TEMPLATE_P (friend_decl
))
1170 need_template
= false;
1172 /* There is nothing to do if this is not a template friend. */
1173 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1176 if (is_specialization_of (decl
, friend_decl
))
1180 A member of a class template may be declared to be a friend of a
1181 non-template class. In this case, the corresponding member of
1182 every specialization of the class template is a friend of the
1183 class granting friendship.
1185 For example, given a template friend declaration
1187 template <class T> friend void A<T>::f();
1189 the member function below is considered a friend
1191 template <> struct A<int> {
1195 For this type of template friend, TEMPLATE_DEPTH below will be
1196 nonzero. To determine if DECL is a friend of FRIEND, we first
1197 check if the enclosing class is a specialization of another. */
1199 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1201 && DECL_CLASS_SCOPE_P (decl
)
1202 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1203 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1205 /* Next, we check the members themselves. In order to handle
1206 a few tricky cases, such as when FRIEND_DECL's are
1208 template <class T> friend void A<T>::g(T t);
1209 template <class T> template <T t> friend void A<T>::h();
1213 void A<int>::g(int);
1214 template <int> void A<int>::h();
1216 we need to figure out ARGS, the template arguments from
1217 the context of DECL. This is required for template substitution
1218 of `T' in the function parameter of `g' and template parameter
1219 of `h' in the above examples. Here ARGS corresponds to `int'. */
1221 tree context
= DECL_CONTEXT (decl
);
1222 tree args
= NULL_TREE
;
1223 int current_depth
= 0;
1225 while (current_depth
< template_depth
)
1227 if (CLASSTYPE_TEMPLATE_INFO (context
))
1229 if (current_depth
== 0)
1230 args
= TYPE_TI_ARGS (context
);
1232 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1235 context
= TYPE_CONTEXT (context
);
1238 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1243 tree friend_args_type
;
1244 tree decl_args_type
;
1246 /* Make sure that both DECL and FRIEND_DECL are templates or
1248 is_template
= DECL_TEMPLATE_INFO (decl
)
1249 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1250 if (need_template
^ is_template
)
1252 else if (is_template
)
1254 /* If both are templates, check template parameter list. */
1256 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1258 if (!comp_template_parms
1259 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1263 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1266 decl_type
= TREE_TYPE (decl
);
1268 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1269 tf_none
, NULL_TREE
);
1270 if (friend_type
== error_mark_node
)
1273 /* Check if return types match. */
1274 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1277 /* Check if function parameter types match, ignoring the
1278 `this' parameter. */
1279 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1280 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1281 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1282 friend_args_type
= TREE_CHAIN (friend_args_type
);
1283 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1284 decl_args_type
= TREE_CHAIN (decl_args_type
);
1286 return compparms (decl_args_type
, friend_args_type
);
1290 /* DECL is a TYPE_DECL */
1292 tree decl_type
= TREE_TYPE (decl
);
1294 /* Make sure that both DECL and FRIEND_DECL are templates or
1297 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1298 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1300 if (need_template
^ is_template
)
1302 else if (is_template
)
1305 /* If both are templates, check the name of the two
1306 TEMPLATE_DECL's first because is_friend didn't. */
1307 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1308 != DECL_NAME (friend_decl
))
1311 /* Now check template parameter list. */
1313 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1315 return comp_template_parms
1316 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1320 return (DECL_NAME (decl
)
1321 == DECL_NAME (friend_decl
));
1327 /* Register the specialization SPEC as a specialization of TMPL with
1328 the indicated ARGS. IS_FRIEND indicates whether the specialization
1329 is actually just a friend declaration. Returns SPEC, or an
1330 equivalent prior declaration, if available.
1332 We also store instantiations of field packs in the hash table, even
1333 though they are not themselves templates, to make lookup easier. */
1336 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1343 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1344 || (TREE_CODE (tmpl
) == FIELD_DECL
1345 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1347 if (TREE_CODE (spec
) == FUNCTION_DECL
1348 && uses_template_parms (DECL_TI_ARGS (spec
)))
1349 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1350 register it; we want the corresponding TEMPLATE_DECL instead.
1351 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1352 the more obvious `uses_template_parms (spec)' to avoid problems
1353 with default function arguments. In particular, given
1354 something like this:
1356 template <class T> void f(T t1, T t = T())
1358 the default argument expression is not substituted for in an
1359 instantiation unless and until it is actually needed. */
1362 if (optimize_specialization_lookup_p (tmpl
))
1363 /* We don't put these specializations in the hash table, but we might
1364 want to give an error about a mismatch. */
1365 fn
= retrieve_specialization (tmpl
, args
, 0);
1373 hash
= hash_specialization (&elt
);
1376 htab_find_slot_with_hash (decl_specializations
, &elt
, hash
, INSERT
);
1378 fn
= ((spec_entry
*) *slot
)->spec
;
1383 /* We can sometimes try to re-register a specialization that we've
1384 already got. In particular, regenerate_decl_from_template calls
1385 duplicate_decls which will update the specialization list. But,
1386 we'll still get called again here anyhow. It's more convenient
1387 to simply allow this than to try to prevent it. */
1390 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1392 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1394 if (DECL_ODR_USED (fn
)
1395 || DECL_EXPLICIT_INSTANTIATION (fn
))
1397 error ("specialization of %qD after instantiation",
1399 return error_mark_node
;
1404 /* This situation should occur only if the first
1405 specialization is an implicit instantiation, the
1406 second is an explicit specialization, and the
1407 implicit instantiation has not yet been used. That
1408 situation can occur if we have implicitly
1409 instantiated a member function and then specialized
1412 We can also wind up here if a friend declaration that
1413 looked like an instantiation turns out to be a
1416 template <class T> void foo(T);
1417 class S { friend void foo<>(int) };
1418 template <> void foo(int);
1420 We transform the existing DECL in place so that any
1421 pointers to it become pointers to the updated
1424 If there was a definition for the template, but not
1425 for the specialization, we want this to look as if
1426 there were no definition, and vice versa. */
1427 DECL_INITIAL (fn
) = NULL_TREE
;
1428 duplicate_decls (spec
, fn
, is_friend
);
1429 /* The call to duplicate_decls will have applied
1432 An explicit specialization of a function template
1433 is inline only if it is explicitly declared to be,
1434 and independently of whether its function template
1437 to the primary function; now copy the inline bits to
1438 the various clones. */
1439 FOR_EACH_CLONE (clone
, fn
)
1441 DECL_DECLARED_INLINE_P (clone
)
1442 = DECL_DECLARED_INLINE_P (fn
);
1443 DECL_SOURCE_LOCATION (clone
)
1444 = DECL_SOURCE_LOCATION (fn
);
1446 check_specialization_namespace (tmpl
);
1451 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1453 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1454 /* Dup decl failed, but this is a new definition. Set the
1455 line number so any errors match this new
1457 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1463 return duplicate_decls (spec
, fn
, is_friend
);
1465 /* A specialization must be declared in the same namespace as the
1466 template it is specializing. */
1467 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1468 && !check_specialization_namespace (tmpl
))
1469 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1471 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1473 spec_entry
*entry
= ggc_alloc_spec_entry ();
1474 gcc_assert (tmpl
&& args
&& spec
);
1477 if (TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1478 && PRIMARY_TEMPLATE_P (tmpl
)
1479 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1480 /* TMPL is a forward declaration of a template function; keep a list
1481 of all specializations in case we need to reassign them to a friend
1482 template later in tsubst_friend_function. */
1483 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1484 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1490 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1491 TMPL and ARGS members, ignores SPEC. */
1493 int comparing_specializations
;
1496 eq_specializations (const void *p1
, const void *p2
)
1498 const spec_entry
*e1
= (const spec_entry
*)p1
;
1499 const spec_entry
*e2
= (const spec_entry
*)p2
;
1502 ++comparing_specializations
;
1503 equal
= (e1
->tmpl
== e2
->tmpl
1504 && comp_template_args (e1
->args
, e2
->args
));
1505 --comparing_specializations
;
1510 /* Returns a hash for a template TMPL and template arguments ARGS. */
1513 hash_tmpl_and_args (tree tmpl
, tree args
)
1515 hashval_t val
= DECL_UID (tmpl
);
1516 return iterative_hash_template_arg (args
, val
);
1519 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1523 hash_specialization (const void *p
)
1525 const spec_entry
*e
= (const spec_entry
*)p
;
1526 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1529 /* Recursively calculate a hash value for a template argument ARG, for use
1530 in the hash tables of template specializations. */
1533 iterative_hash_template_arg (tree arg
, hashval_t val
)
1535 unsigned HOST_WIDE_INT i
;
1536 enum tree_code code
;
1539 if (arg
== NULL_TREE
)
1540 return iterative_hash_object (arg
, val
);
1545 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1546 /* We can get one of these when re-hashing a previous entry in the middle
1547 of substituting into a pack expansion. Just look through it. */
1548 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1550 code
= TREE_CODE (arg
);
1551 tclass
= TREE_CODE_CLASS (code
);
1553 val
= iterative_hash_object (code
, val
);
1560 case IDENTIFIER_NODE
:
1561 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1565 int i
, len
= TREE_VEC_LENGTH (arg
);
1566 for (i
= 0; i
< len
; ++i
)
1567 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1571 case TYPE_PACK_EXPANSION
:
1572 case EXPR_PACK_EXPANSION
:
1573 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1574 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1576 case TYPE_ARGUMENT_PACK
:
1577 case NONTYPE_ARGUMENT_PACK
:
1578 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1581 for (; arg
; arg
= TREE_CHAIN (arg
))
1582 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1586 for (; arg
; arg
= OVL_NEXT (arg
))
1587 val
= iterative_hash_template_arg (OVL_CURRENT (arg
), val
);
1593 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1595 val
= iterative_hash_template_arg (field
, val
);
1596 val
= iterative_hash_template_arg (value
, val
);
1602 if (!DECL_ARTIFICIAL (arg
))
1604 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1605 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1607 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1610 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1613 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1614 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1616 case TEMPLATE_PARM_INDEX
:
1617 val
= iterative_hash_template_arg
1618 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1619 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1620 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1623 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1624 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1625 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1628 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1630 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1634 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1635 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1636 val
= iterative_hash_object (code
, val
);
1637 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1640 /* A lambda can't appear in a template arg, but don't crash on
1642 gcc_assert (seen_error ());
1646 case IMPLICIT_CONV_EXPR
:
1647 case STATIC_CAST_EXPR
:
1648 case REINTERPRET_CAST_EXPR
:
1649 case CONST_CAST_EXPR
:
1650 case DYNAMIC_CAST_EXPR
:
1652 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1653 /* Now hash operands as usual. */
1663 if (TYPE_CANONICAL (arg
))
1664 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1666 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1667 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1668 /* Otherwise just compare the types during lookup. */
1671 case tcc_declaration
:
1673 return iterative_hash_expr (arg
, val
);
1676 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1678 unsigned n
= cp_tree_operand_length (arg
);
1679 for (i
= 0; i
< n
; ++i
)
1680 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1688 /* Unregister the specialization SPEC as a specialization of TMPL.
1689 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1690 if the SPEC was listed as a specialization of TMPL.
1692 Note that SPEC has been ggc_freed, so we can't look inside it. */
1695 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1700 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1701 elt
.args
= TI_ARGS (tinfo
);
1702 elt
.spec
= NULL_TREE
;
1704 entry
= (spec_entry
*) htab_find (decl_specializations
, &elt
);
1707 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1708 gcc_assert (new_spec
!= NULL_TREE
);
1709 entry
->spec
= new_spec
;
1716 /* Like register_specialization, but for local declarations. We are
1717 registering SPEC, an instantiation of TMPL. */
1720 register_local_specialization (tree spec
, tree tmpl
)
1724 slot
= pointer_map_insert (local_specializations
, tmpl
);
1728 /* TYPE is a class type. Returns true if TYPE is an explicitly
1729 specialized class. */
1732 explicit_class_specialization_p (tree type
)
1734 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1736 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1739 /* Print the list of functions at FNS, going through all the overloads
1740 for each element of the list. Alternatively, FNS can not be a
1741 TREE_LIST, in which case it will be printed together with all the
1744 MORE and *STR should respectively be FALSE and NULL when the function
1745 is called from the outside. They are used internally on recursive
1746 calls. print_candidates manages the two parameters and leaves NULL
1747 in *STR when it ends. */
1750 print_candidates_1 (tree fns
, bool more
, const char **str
)
1753 char *spaces
= NULL
;
1755 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1756 if (TREE_CODE (fn
) == TREE_LIST
)
1758 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1759 print_candidates_1 (TREE_VALUE (fn2
),
1760 TREE_CHAIN (fn2
) || more
, str
);
1764 tree cand
= OVL_CURRENT (fn
);
1767 /* Pick the prefix string. */
1768 if (!more
&& !OVL_NEXT (fns
))
1770 inform (DECL_SOURCE_LOCATION (cand
),
1771 "candidate is: %#D", cand
);
1775 *str
= _("candidates are:");
1776 spaces
= get_spaces (*str
);
1778 inform (DECL_SOURCE_LOCATION (cand
), "%s %#D", *str
, cand
);
1779 *str
= spaces
? spaces
: *str
;
1789 /* Print the list of candidate FNS in an error message. FNS can also
1790 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1793 print_candidates (tree fns
)
1795 const char *str
= NULL
;
1796 print_candidates_1 (fns
, false, &str
);
1797 gcc_assert (str
== NULL
);
1800 /* Returns the template (one of the functions given by TEMPLATE_ID)
1801 which can be specialized to match the indicated DECL with the
1802 explicit template args given in TEMPLATE_ID. The DECL may be
1803 NULL_TREE if none is available. In that case, the functions in
1804 TEMPLATE_ID are non-members.
1806 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1807 specialization of a member template.
1809 The TEMPLATE_COUNT is the number of references to qualifying
1810 template classes that appeared in the name of the function. See
1811 check_explicit_specialization for a more accurate description.
1813 TSK indicates what kind of template declaration (if any) is being
1814 declared. TSK_TEMPLATE indicates that the declaration given by
1815 DECL, though a FUNCTION_DECL, has template parameters, and is
1816 therefore a template function.
1818 The template args (those explicitly specified and those deduced)
1819 are output in a newly created vector *TARGS_OUT.
1821 If it is impossible to determine the result, an error message is
1822 issued. The error_mark_node is returned to indicate failure. */
1825 determine_specialization (tree template_id
,
1828 int need_member_template
,
1834 tree explicit_targs
;
1835 tree candidates
= NULL_TREE
;
1836 /* A TREE_LIST of templates of which DECL may be a specialization.
1837 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1838 corresponding TREE_PURPOSE is the set of template arguments that,
1839 when used to instantiate the template, would produce a function
1840 with the signature of DECL. */
1841 tree templates
= NULL_TREE
;
1843 cp_binding_level
*b
;
1845 *targs_out
= NULL_TREE
;
1847 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1848 return error_mark_node
;
1850 /* We shouldn't be specializing a member template of an
1851 unspecialized class template; we already gave an error in
1852 check_specialization_scope, now avoid crashing. */
1853 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
1854 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
1856 gcc_assert (errorcount
);
1857 return error_mark_node
;
1860 fns
= TREE_OPERAND (template_id
, 0);
1861 explicit_targs
= TREE_OPERAND (template_id
, 1);
1863 if (fns
== error_mark_node
)
1864 return error_mark_node
;
1866 /* Check for baselinks. */
1867 if (BASELINK_P (fns
))
1868 fns
= BASELINK_FUNCTIONS (fns
);
1870 if (!is_overloaded_fn (fns
))
1872 error ("%qD is not a function template", fns
);
1873 return error_mark_node
;
1876 /* Count the number of template headers specified for this
1879 for (b
= current_binding_level
;
1880 b
->kind
== sk_template_parms
;
1884 for (; fns
; fns
= OVL_NEXT (fns
))
1886 tree fn
= OVL_CURRENT (fns
);
1888 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1890 tree decl_arg_types
;
1894 /* In case of explicit specialization, we need to check if
1895 the number of template headers appearing in the specialization
1896 is correct. This is usually done in check_explicit_specialization,
1897 but the check done there cannot be exhaustive when specializing
1898 member functions. Consider the following code:
1900 template <> void A<int>::f(int);
1901 template <> template <> void A<int>::f(int);
1903 Assuming that A<int> is not itself an explicit specialization
1904 already, the first line specializes "f" which is a non-template
1905 member function, whilst the second line specializes "f" which
1906 is a template member function. So both lines are syntactically
1907 correct, and check_explicit_specialization does not reject
1910 Here, we can do better, as we are matching the specialization
1911 against the declarations. We count the number of template
1912 headers, and we check if they match TEMPLATE_COUNT + 1
1913 (TEMPLATE_COUNT is the number of qualifying template classes,
1914 plus there must be another header for the member template
1917 Notice that if header_count is zero, this is not a
1918 specialization but rather a template instantiation, so there
1919 is no check we can perform here. */
1920 if (header_count
&& header_count
!= template_count
+ 1)
1923 /* Check that the number of template arguments at the
1924 innermost level for DECL is the same as for FN. */
1925 if (current_binding_level
->kind
== sk_template_parms
1926 && !current_binding_level
->explicit_spec_p
1927 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1928 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1929 (current_template_parms
))))
1932 /* DECL might be a specialization of FN. */
1933 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1934 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1936 /* For a non-static member function, we need to make sure
1937 that the const qualification is the same. Since
1938 get_bindings does not try to merge the "this" parameter,
1939 we must do the comparison explicitly. */
1940 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1941 && !same_type_p (TREE_VALUE (fn_arg_types
),
1942 TREE_VALUE (decl_arg_types
)))
1945 /* Skip the "this" parameter and, for constructors of
1946 classes with virtual bases, the VTT parameter. A
1947 full specialization of a constructor will have a VTT
1948 parameter, but a template never will. */
1950 = skip_artificial_parms_for (decl
, decl_arg_types
);
1952 = skip_artificial_parms_for (fn
, fn_arg_types
);
1954 /* Function templates cannot be specializations; there are
1955 no partial specializations of functions. Therefore, if
1956 the type of DECL does not match FN, there is no
1958 if (tsk
== tsk_template
)
1960 if (compparms (fn_arg_types
, decl_arg_types
))
1961 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1965 /* See whether this function might be a specialization of this
1966 template. Suppress access control because we might be trying
1967 to make this specialization a friend, and we have already done
1968 access control for the declaration of the specialization. */
1969 push_deferring_access_checks (dk_no_check
);
1970 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1971 pop_deferring_access_checks ();
1974 /* We cannot deduce template arguments that when used to
1975 specialize TMPL will produce DECL. */
1978 /* Make sure that the deduced arguments actually work. */
1979 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
1980 if (insttype
== error_mark_node
)
1983 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
1984 if (!compparms (fn_arg_types
, decl_arg_types
))
1987 /* Save this template, and the arguments deduced. */
1988 templates
= tree_cons (targs
, fn
, templates
);
1990 else if (need_member_template
)
1991 /* FN is an ordinary member function, and we need a
1992 specialization of a member template. */
1994 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1995 /* We can get IDENTIFIER_NODEs here in certain erroneous
1998 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1999 /* This is just an ordinary non-member function. Nothing can
2000 be a specialization of that. */
2002 else if (DECL_ARTIFICIAL (fn
))
2003 /* Cannot specialize functions that are created implicitly. */
2007 tree decl_arg_types
;
2009 /* This is an ordinary member function. However, since
2010 we're here, we can assume its enclosing class is a
2011 template class. For example,
2013 template <typename T> struct S { void f(); };
2014 template <> void S<int>::f() {}
2016 Here, S<int>::f is a non-template, but S<int> is a
2017 template class. If FN has the same type as DECL, we
2018 might be in business. */
2020 if (!DECL_TEMPLATE_INFO (fn
))
2021 /* Its enclosing class is an explicit specialization
2022 of a template class. This is not a candidate. */
2025 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2026 TREE_TYPE (TREE_TYPE (fn
))))
2027 /* The return types differ. */
2030 /* Adjust the type of DECL in case FN is a static member. */
2031 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2032 if (DECL_STATIC_FUNCTION_P (fn
)
2033 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2034 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2036 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2039 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2043 if (templates
&& TREE_CHAIN (templates
))
2049 It is possible for a specialization with a given function
2050 signature to be instantiated from more than one function
2051 template. In such cases, explicit specification of the
2052 template arguments must be used to uniquely identify the
2053 function template specialization being specialized.
2055 Note that here, there's no suggestion that we're supposed to
2056 determine which of the candidate templates is most
2057 specialized. However, we, also have:
2061 Partial ordering of overloaded function template
2062 declarations is used in the following contexts to select
2063 the function template to which a function template
2064 specialization refers:
2066 -- when an explicit specialization refers to a function
2069 So, we do use the partial ordering rules, at least for now.
2070 This extension can only serve to make invalid programs valid,
2071 so it's safe. And, there is strong anecdotal evidence that
2072 the committee intended the partial ordering rules to apply;
2073 the EDG front end has that behavior, and John Spicer claims
2074 that the committee simply forgot to delete the wording in
2075 [temp.expl.spec]. */
2076 tree tmpl
= most_specialized_instantiation (templates
);
2077 if (tmpl
!= error_mark_node
)
2080 TREE_CHAIN (templates
) = NULL_TREE
;
2084 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2086 error ("template-id %qD for %q+D does not match any template "
2087 "declaration", template_id
, decl
);
2088 if (header_count
&& header_count
!= template_count
+ 1)
2089 inform (input_location
, "saw %d %<template<>%>, need %d for "
2090 "specializing a member function template",
2091 header_count
, template_count
+ 1);
2092 return error_mark_node
;
2094 else if ((templates
&& TREE_CHAIN (templates
))
2095 || (candidates
&& TREE_CHAIN (candidates
))
2096 || (templates
&& candidates
))
2098 error ("ambiguous template specialization %qD for %q+D",
2100 candidates
= chainon (candidates
, templates
);
2101 print_candidates (candidates
);
2102 return error_mark_node
;
2105 /* We have one, and exactly one, match. */
2108 tree fn
= TREE_VALUE (candidates
);
2109 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2110 /* DECL is a re-declaration or partial instantiation of a template
2112 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2114 /* It was a specialization of an ordinary member function in a
2116 return DECL_TI_TEMPLATE (fn
);
2119 /* It was a specialization of a template. */
2120 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2121 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2123 *targs_out
= copy_node (targs
);
2124 SET_TMPL_ARGS_LEVEL (*targs_out
,
2125 TMPL_ARGS_DEPTH (*targs_out
),
2126 TREE_PURPOSE (templates
));
2129 *targs_out
= TREE_PURPOSE (templates
);
2130 return TREE_VALUE (templates
);
2133 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2134 but with the default argument values filled in from those in the
2138 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2141 tree new_spec_types
;
2146 if (spec_types
== void_list_node
)
2147 return void_list_node
;
2149 /* Substitute into the rest of the list. */
2151 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2152 TREE_CHAIN (tmpl_types
));
2154 /* Add the default argument for this parameter. */
2155 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2156 TREE_VALUE (spec_types
),
2160 /* DECL is an explicit specialization. Replicate default arguments
2161 from the template it specializes. (That way, code like:
2163 template <class T> void f(T = 3);
2164 template <> void f(double);
2167 works, as required.) An alternative approach would be to look up
2168 the correct default arguments at the call-site, but this approach
2169 is consistent with how implicit instantiations are handled. */
2172 copy_default_args_to_explicit_spec (tree decl
)
2177 tree new_spec_types
;
2181 tree object_type
= NULL_TREE
;
2182 tree in_charge
= NULL_TREE
;
2183 tree vtt
= NULL_TREE
;
2185 /* See if there's anything we need to do. */
2186 tmpl
= DECL_TI_TEMPLATE (decl
);
2187 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2188 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2189 if (TREE_PURPOSE (t
))
2194 old_type
= TREE_TYPE (decl
);
2195 spec_types
= TYPE_ARG_TYPES (old_type
);
2197 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2199 /* Remove the this pointer, but remember the object's type for
2201 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2202 spec_types
= TREE_CHAIN (spec_types
);
2203 tmpl_types
= TREE_CHAIN (tmpl_types
);
2205 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2207 /* DECL may contain more parameters than TMPL due to the extra
2208 in-charge parameter in constructors and destructors. */
2209 in_charge
= spec_types
;
2210 spec_types
= TREE_CHAIN (spec_types
);
2212 if (DECL_HAS_VTT_PARM_P (decl
))
2215 spec_types
= TREE_CHAIN (spec_types
);
2219 /* Compute the merged default arguments. */
2221 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2223 /* Compute the new FUNCTION_TYPE. */
2227 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2232 /* Put the in-charge parameter back. */
2233 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2234 TREE_VALUE (in_charge
),
2237 new_type
= build_method_type_directly (object_type
,
2238 TREE_TYPE (old_type
),
2242 new_type
= build_function_type (TREE_TYPE (old_type
),
2244 new_type
= cp_build_type_attribute_variant (new_type
,
2245 TYPE_ATTRIBUTES (old_type
));
2246 new_type
= build_exception_variant (new_type
,
2247 TYPE_RAISES_EXCEPTIONS (old_type
));
2248 TREE_TYPE (decl
) = new_type
;
2251 /* Return the number of template headers we expect to see for a definition
2252 or specialization of CTYPE or one of its non-template members. */
2255 num_template_headers_for_class (tree ctype
)
2257 int num_templates
= 0;
2259 while (ctype
&& CLASS_TYPE_P (ctype
))
2261 /* You're supposed to have one `template <...>' for every
2262 template class, but you don't need one for a full
2263 specialization. For example:
2265 template <class T> struct S{};
2266 template <> struct S<int> { void f(); };
2267 void S<int>::f () {}
2269 is correct; there shouldn't be a `template <>' for the
2270 definition of `S<int>::f'. */
2271 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2272 /* If CTYPE does not have template information of any
2273 kind, then it is not a template, nor is it nested
2274 within a template. */
2276 if (explicit_class_specialization_p (ctype
))
2278 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2281 ctype
= TYPE_CONTEXT (ctype
);
2284 return num_templates
;
2287 /* Do a simple sanity check on the template headers that precede the
2288 variable declaration DECL. */
2291 check_template_variable (tree decl
)
2293 tree ctx
= CP_DECL_CONTEXT (decl
);
2294 int wanted
= num_template_headers_for_class (ctx
);
2295 if (!TYPE_P (ctx
) || !CLASSTYPE_TEMPLATE_INFO (ctx
))
2296 permerror (DECL_SOURCE_LOCATION (decl
),
2297 "%qD is not a static data member of a class template", decl
);
2298 else if (template_header_count
> wanted
)
2300 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2301 "too many template headers for %D (should be %d)",
2303 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2304 inform (DECL_SOURCE_LOCATION (decl
),
2305 "members of an explicitly specialized class are defined "
2306 "without a template header");
2310 /* Check to see if the function just declared, as indicated in
2311 DECLARATOR, and in DECL, is a specialization of a function
2312 template. We may also discover that the declaration is an explicit
2313 instantiation at this point.
2315 Returns DECL, or an equivalent declaration that should be used
2316 instead if all goes well. Issues an error message if something is
2317 amiss. Returns error_mark_node if the error is not easily
2320 FLAGS is a bitmask consisting of the following flags:
2322 2: The function has a definition.
2323 4: The function is a friend.
2325 The TEMPLATE_COUNT is the number of references to qualifying
2326 template classes that appeared in the name of the function. For
2329 template <class T> struct S { void f(); };
2332 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2333 classes are not counted in the TEMPLATE_COUNT, so that in
2335 template <class T> struct S {};
2336 template <> struct S<int> { void f(); }
2337 template <> void S<int>::f();
2339 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2340 invalid; there should be no template <>.)
2342 If the function is a specialization, it is marked as such via
2343 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2344 is set up correctly, and it is added to the list of specializations
2345 for that template. */
2348 check_explicit_specialization (tree declarator
,
2353 int have_def
= flags
& 2;
2354 int is_friend
= flags
& 4;
2355 int specialization
= 0;
2356 int explicit_instantiation
= 0;
2357 int member_specialization
= 0;
2358 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2359 tree dname
= DECL_NAME (decl
);
2364 if (!processing_specialization
)
2367 tsk
= tsk_excessive_parms
;
2370 tsk
= current_tmpl_spec_kind (template_count
);
2375 if (processing_specialization
)
2378 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2380 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2383 /* This could be something like:
2385 template <class T> void f(T);
2386 class S { friend void f<>(int); } */
2390 /* This case handles bogus declarations like template <>
2391 template <class T> void f<int>(); */
2393 error ("template-id %qD in declaration of primary template",
2400 case tsk_invalid_member_spec
:
2401 /* The error has already been reported in
2402 check_specialization_scope. */
2403 return error_mark_node
;
2405 case tsk_invalid_expl_inst
:
2406 error ("template parameter list used in explicit instantiation");
2412 error ("definition provided for explicit instantiation");
2414 explicit_instantiation
= 1;
2417 case tsk_excessive_parms
:
2418 case tsk_insufficient_parms
:
2419 if (tsk
== tsk_excessive_parms
)
2420 error ("too many template parameter lists in declaration of %qD",
2422 else if (template_header_count
)
2423 error("too few template parameter lists in declaration of %qD", decl
);
2425 error("explicit specialization of %qD must be introduced by "
2426 "%<template <>%>", decl
);
2430 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2432 member_specialization
= 1;
2438 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2440 /* This case handles bogus declarations like template <>
2441 template <class T> void f<int>(); */
2443 if (uses_template_parms (declarator
))
2444 error ("function template partial specialization %qD "
2445 "is not allowed", declarator
);
2447 error ("template-id %qD in declaration of primary template",
2452 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2453 /* This is a specialization of a member template, without
2454 specialization the containing class. Something like:
2456 template <class T> struct S {
2457 template <class U> void f (U);
2459 template <> template <class U> void S<int>::f(U) {}
2461 That's a specialization -- but of the entire template. */
2469 if (specialization
|| member_specialization
)
2471 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2472 for (; t
; t
= TREE_CHAIN (t
))
2473 if (TREE_PURPOSE (t
))
2475 permerror (input_location
,
2476 "default argument specified in explicit specialization");
2481 if (specialization
|| member_specialization
|| explicit_instantiation
)
2483 tree tmpl
= NULL_TREE
;
2484 tree targs
= NULL_TREE
;
2486 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2487 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2491 gcc_assert (identifier_p (declarator
));
2496 /* If there is no class context, the explicit instantiation
2497 must be at namespace scope. */
2498 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2500 /* Find the namespace binding, using the declaration
2502 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2504 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2506 error ("%qD is not a template function", dname
);
2507 fns
= error_mark_node
;
2511 tree fn
= OVL_CURRENT (fns
);
2512 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2513 CP_DECL_CONTEXT (fn
)))
2514 error ("%qD is not declared in %qD",
2515 decl
, current_namespace
);
2519 declarator
= lookup_template_function (fns
, NULL_TREE
);
2522 if (declarator
== error_mark_node
)
2523 return error_mark_node
;
2525 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2527 if (!explicit_instantiation
)
2528 /* A specialization in class scope. This is invalid,
2529 but the error will already have been flagged by
2530 check_specialization_scope. */
2531 return error_mark_node
;
2534 /* It's not valid to write an explicit instantiation in
2537 class C { template void f(); }
2539 This case is caught by the parser. However, on
2542 template class C { void f(); };
2544 (which is invalid) we can get here. The error will be
2551 else if (ctype
!= NULL_TREE
2552 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2554 /* Find the list of functions in ctype that have the same
2555 name as the declared function. */
2556 tree name
= TREE_OPERAND (declarator
, 0);
2557 tree fns
= NULL_TREE
;
2560 if (constructor_name_p (name
, ctype
))
2562 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2564 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2565 : !CLASSTYPE_DESTRUCTORS (ctype
))
2567 /* From [temp.expl.spec]:
2569 If such an explicit specialization for the member
2570 of a class template names an implicitly-declared
2571 special member function (clause _special_), the
2572 program is ill-formed.
2574 Similar language is found in [temp.explicit]. */
2575 error ("specialization of implicitly-declared special member function");
2576 return error_mark_node
;
2579 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2582 if (!DECL_CONV_FN_P (decl
))
2584 idx
= lookup_fnfields_1 (ctype
, name
);
2586 fns
= (*CLASSTYPE_METHOD_VEC (ctype
))[idx
];
2590 vec
<tree
, va_gc
> *methods
;
2593 /* For a type-conversion operator, we cannot do a
2594 name-based lookup. We might be looking for `operator
2595 int' which will be a specialization of `operator T'.
2596 So, we find *all* the conversion operators, and then
2597 select from them. */
2600 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2602 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2603 methods
->iterate (idx
, &ovl
);
2606 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2607 /* There are no more conversion functions. */
2610 /* Glue all these conversion functions together
2611 with those we already have. */
2612 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2613 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2617 if (fns
== NULL_TREE
)
2619 error ("no member function %qD declared in %qT", name
, ctype
);
2620 return error_mark_node
;
2623 TREE_OPERAND (declarator
, 0) = fns
;
2626 /* Figure out what exactly is being specialized at this point.
2627 Note that for an explicit instantiation, even one for a
2628 member function, we cannot tell apriori whether the
2629 instantiation is for a member template, or just a member
2630 function of a template class. Even if a member template is
2631 being instantiated, the member template arguments may be
2632 elided if they can be deduced from the rest of the
2634 tmpl
= determine_specialization (declarator
, decl
,
2636 member_specialization
,
2640 if (!tmpl
|| tmpl
== error_mark_node
)
2641 /* We couldn't figure out what this declaration was
2643 return error_mark_node
;
2646 tree gen_tmpl
= most_general_template (tmpl
);
2648 if (explicit_instantiation
)
2650 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2651 is done by do_decl_instantiation later. */
2653 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2654 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2656 if (arg_depth
> parm_depth
)
2658 /* If TMPL is not the most general template (for
2659 example, if TMPL is a friend template that is
2660 injected into namespace scope), then there will
2661 be too many levels of TARGS. Remove some of them
2666 new_targs
= make_tree_vec (parm_depth
);
2667 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2668 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2669 = TREE_VEC_ELT (targs
, i
);
2673 return instantiate_template (tmpl
, targs
, tf_error
);
2676 /* If we thought that the DECL was a member function, but it
2677 turns out to be specializing a static member function,
2678 make DECL a static member function as well. */
2679 if (DECL_STATIC_FUNCTION_P (tmpl
)
2680 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2681 revert_static_member_fn (decl
);
2683 /* If this is a specialization of a member template of a
2684 template class, we want to return the TEMPLATE_DECL, not
2685 the specialization of it. */
2686 if (tsk
== tsk_template
)
2688 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2689 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2690 DECL_INITIAL (result
) = NULL_TREE
;
2694 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2695 DECL_SOURCE_LOCATION (result
)
2696 = DECL_SOURCE_LOCATION (decl
);
2697 /* We want to use the argument list specified in the
2698 definition, not in the original declaration. */
2699 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2700 for (parm
= DECL_ARGUMENTS (result
); parm
;
2701 parm
= DECL_CHAIN (parm
))
2702 DECL_CONTEXT (parm
) = result
;
2704 return register_specialization (tmpl
, gen_tmpl
, targs
,
2708 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2709 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2711 /* Inherit default function arguments from the template
2712 DECL is specializing. */
2713 copy_default_args_to_explicit_spec (decl
);
2715 /* This specialization has the same protection as the
2716 template it specializes. */
2717 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2718 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2720 /* 7.1.1-1 [dcl.stc]
2722 A storage-class-specifier shall not be specified in an
2723 explicit specialization...
2725 The parser rejects these, so unless action is taken here,
2726 explicit function specializations will always appear with
2729 The action recommended by the C++ CWG in response to C++
2730 defect report 605 is to make the storage class and linkage
2731 of the explicit specialization match the templated function:
2733 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2735 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2737 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2738 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2740 /* This specialization has the same linkage and visibility as
2741 the function template it specializes. */
2742 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2743 if (! TREE_PUBLIC (decl
))
2745 DECL_INTERFACE_KNOWN (decl
) = 1;
2746 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2748 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2749 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2751 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2752 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2756 /* If DECL is a friend declaration, declared using an
2757 unqualified name, the namespace associated with DECL may
2758 have been set incorrectly. For example, in:
2760 template <typename T> void f(T);
2762 struct S { friend void f<int>(int); }
2765 we will have set the DECL_CONTEXT for the friend
2766 declaration to N, rather than to the global namespace. */
2767 if (DECL_NAMESPACE_SCOPE_P (decl
))
2768 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2770 if (is_friend
&& !have_def
)
2771 /* This is not really a declaration of a specialization.
2772 It's just the name of an instantiation. But, it's not
2773 a request for an instantiation, either. */
2774 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2775 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2776 /* This is indeed a specialization. In case of constructors
2777 and destructors, we need in-charge and not-in-charge
2778 versions in V3 ABI. */
2779 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2781 /* Register this specialization so that we can find it
2783 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2790 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2791 parameters. These are represented in the same format used for
2792 DECL_TEMPLATE_PARMS. */
2795 comp_template_parms (const_tree parms1
, const_tree parms2
)
2800 if (parms1
== parms2
)
2803 for (p1
= parms1
, p2
= parms2
;
2804 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2805 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2807 tree t1
= TREE_VALUE (p1
);
2808 tree t2
= TREE_VALUE (p2
);
2811 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2812 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2814 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2817 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2819 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2820 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2822 /* If either of the template parameters are invalid, assume
2823 they match for the sake of error recovery. */
2824 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2827 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2830 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2831 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2832 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2834 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2839 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2840 /* One set of parameters has more parameters lists than the
2847 /* Determine whether PARM is a parameter pack. */
2850 template_parameter_pack_p (const_tree parm
)
2852 /* Determine if we have a non-type template parameter pack. */
2853 if (TREE_CODE (parm
) == PARM_DECL
)
2854 return (DECL_TEMPLATE_PARM_P (parm
)
2855 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2856 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
2857 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
2859 /* If this is a list of template parameters, we could get a
2860 TYPE_DECL or a TEMPLATE_DECL. */
2861 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2862 parm
= TREE_TYPE (parm
);
2864 /* Otherwise it must be a type template parameter. */
2865 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2866 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2867 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2870 /* Determine if T is a function parameter pack. */
2873 function_parameter_pack_p (const_tree t
)
2875 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2876 return DECL_PACK_P (t
);
2880 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2881 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2884 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2886 if (! primary_func_tmpl_inst
2887 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2888 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2891 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2894 /* Return true iff the function parameter PARAM_DECL was expanded
2895 from the function parameter pack PACK. */
2898 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
2900 if (DECL_ARTIFICIAL (param_decl
)
2901 || !function_parameter_pack_p (pack
))
2904 /* The parameter pack and its pack arguments have the same
2906 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
2909 /* Determine whether ARGS describes a variadic template args list,
2910 i.e., one that is terminated by a template argument pack. */
2913 template_args_variadic_p (tree args
)
2918 if (args
== NULL_TREE
)
2921 args
= INNERMOST_TEMPLATE_ARGS (args
);
2922 nargs
= TREE_VEC_LENGTH (args
);
2927 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2929 return ARGUMENT_PACK_P (last_parm
);
2932 /* Generate a new name for the parameter pack name NAME (an
2933 IDENTIFIER_NODE) that incorporates its */
2936 make_ith_pack_parameter_name (tree name
, int i
)
2938 /* Munge the name to include the parameter index. */
2939 #define NUMBUF_LEN 128
2940 char numbuf
[NUMBUF_LEN
];
2944 if (name
== NULL_TREE
)
2946 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
2947 newname_len
= IDENTIFIER_LENGTH (name
)
2948 + strlen (numbuf
) + 2;
2949 newname
= (char*)alloca (newname_len
);
2950 snprintf (newname
, newname_len
,
2951 "%s#%i", IDENTIFIER_POINTER (name
), i
);
2952 return get_identifier (newname
);
2955 /* Return true if T is a primary function, class or alias template
2959 primary_template_instantiation_p (const_tree t
)
2964 if (TREE_CODE (t
) == FUNCTION_DECL
)
2965 return DECL_LANG_SPECIFIC (t
)
2966 && DECL_TEMPLATE_INSTANTIATION (t
)
2967 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
2968 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
2969 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
2970 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
2971 else if (alias_template_specialization_p (t
))
2976 /* Return true if PARM is a template template parameter. */
2979 template_template_parameter_p (const_tree parm
)
2981 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
2984 /* Return true iff PARM is a DECL representing a type template
2988 template_type_parameter_p (const_tree parm
)
2991 && (TREE_CODE (parm
) == TYPE_DECL
2992 || TREE_CODE (parm
) == TEMPLATE_DECL
)
2993 && DECL_TEMPLATE_PARM_P (parm
));
2996 /* Return the template parameters of T if T is a
2997 primary template instantiation, NULL otherwise. */
3000 get_primary_template_innermost_parameters (const_tree t
)
3002 tree parms
= NULL
, template_info
= NULL
;
3004 if ((template_info
= get_template_info (t
))
3005 && primary_template_instantiation_p (t
))
3006 parms
= INNERMOST_TEMPLATE_PARMS
3007 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3012 /* Return the template parameters of the LEVELth level from the full list
3013 of template parameters PARMS. */
3016 get_template_parms_at_level (tree parms
, int level
)
3020 || TREE_CODE (parms
) != TREE_LIST
3021 || level
> TMPL_PARMS_DEPTH (parms
))
3024 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3025 if (TMPL_PARMS_DEPTH (p
) == level
)
3031 /* Returns the template arguments of T if T is a template instantiation,
3035 get_template_innermost_arguments (const_tree t
)
3037 tree args
= NULL
, template_info
= NULL
;
3039 if ((template_info
= get_template_info (t
))
3040 && TI_ARGS (template_info
))
3041 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3046 /* Return the argument pack elements of T if T is a template argument pack,
3050 get_template_argument_pack_elems (const_tree t
)
3052 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3053 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3056 return ARGUMENT_PACK_ARGS (t
);
3059 /* Structure used to track the progress of find_parameter_packs_r. */
3060 struct find_parameter_pack_data
3062 /* TREE_LIST that will contain all of the parameter packs found by
3064 tree
* parameter_packs
;
3066 /* Set of AST nodes that have been visited by the traversal. */
3067 struct pointer_set_t
*visited
;
3070 /* Identifies all of the argument packs that occur in a template
3071 argument and appends them to the TREE_LIST inside DATA, which is a
3072 find_parameter_pack_data structure. This is a subroutine of
3073 make_pack_expansion and uses_parameter_packs. */
3075 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3078 struct find_parameter_pack_data
* ppd
=
3079 (struct find_parameter_pack_data
*)data
;
3080 bool parameter_pack_p
= false;
3082 /* Handle type aliases/typedefs. */
3083 if (TYPE_ALIAS_P (t
))
3085 if (TYPE_TEMPLATE_INFO (t
))
3086 cp_walk_tree (&TYPE_TI_ARGS (t
),
3087 &find_parameter_packs_r
,
3093 /* Identify whether this is a parameter pack or not. */
3094 switch (TREE_CODE (t
))
3096 case TEMPLATE_PARM_INDEX
:
3097 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3098 parameter_pack_p
= true;
3101 case TEMPLATE_TYPE_PARM
:
3102 t
= TYPE_MAIN_VARIANT (t
);
3103 case TEMPLATE_TEMPLATE_PARM
:
3104 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3105 parameter_pack_p
= true;
3110 if (DECL_PACK_P (t
))
3112 /* We don't want to walk into the type of a PARM_DECL,
3113 because we don't want to see the type parameter pack. */
3115 parameter_pack_p
= true;
3119 /* Look through a lambda capture proxy to the field pack. */
3121 if (DECL_HAS_VALUE_EXPR_P (t
))
3123 tree v
= DECL_VALUE_EXPR (t
);
3125 &find_parameter_packs_r
,
3132 parameter_pack_p
= true;
3135 /* Not a parameter pack. */
3139 if (parameter_pack_p
)
3141 /* Add this parameter pack to the list. */
3142 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3146 cp_walk_tree (&TYPE_CONTEXT (t
),
3147 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3149 /* This switch statement will return immediately if we don't find a
3151 switch (TREE_CODE (t
))
3153 case TEMPLATE_PARM_INDEX
:
3156 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3157 /* Check the template itself. */
3158 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3159 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3160 /* Check the template arguments. */
3161 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3166 case TEMPLATE_TYPE_PARM
:
3167 case TEMPLATE_TEMPLATE_PARM
:
3174 if (TYPE_PTRMEMFUNC_P (t
))
3180 if (TYPE_TEMPLATE_INFO (t
))
3181 cp_walk_tree (&TYPE_TI_ARGS (t
),
3182 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3189 cp_walk_tree (&TREE_TYPE (t
),
3190 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3194 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3199 case TYPE_PACK_EXPANSION
:
3200 case EXPR_PACK_EXPANSION
:
3205 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3210 case IDENTIFIER_NODE
:
3211 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3223 /* Determines if the expression or type T uses any parameter packs. */
3225 uses_parameter_packs (tree t
)
3227 tree parameter_packs
= NULL_TREE
;
3228 struct find_parameter_pack_data ppd
;
3229 ppd
.parameter_packs
= ¶meter_packs
;
3230 ppd
.visited
= pointer_set_create ();
3231 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3232 pointer_set_destroy (ppd
.visited
);
3233 return parameter_packs
!= NULL_TREE
;
3236 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3237 representation a base-class initializer into a parameter pack
3238 expansion. If all goes well, the resulting node will be an
3239 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3242 make_pack_expansion (tree arg
)
3245 tree parameter_packs
= NULL_TREE
;
3246 bool for_types
= false;
3247 struct find_parameter_pack_data ppd
;
3249 if (!arg
|| arg
== error_mark_node
)
3252 if (TREE_CODE (arg
) == TREE_LIST
)
3254 /* The only time we will see a TREE_LIST here is for a base
3255 class initializer. In this case, the TREE_PURPOSE will be a
3256 _TYPE node (representing the base class expansion we're
3257 initializing) and the TREE_VALUE will be a TREE_LIST
3258 containing the initialization arguments.
3260 The resulting expansion looks somewhat different from most
3261 expansions. Rather than returning just one _EXPANSION, we
3262 return a TREE_LIST whose TREE_PURPOSE is a
3263 TYPE_PACK_EXPANSION containing the bases that will be
3264 initialized. The TREE_VALUE will be identical to the
3265 original TREE_VALUE, which is a list of arguments that will
3266 be passed to each base. We do not introduce any new pack
3267 expansion nodes into the TREE_VALUE (although it is possible
3268 that some already exist), because the TREE_PURPOSE and
3269 TREE_VALUE all need to be expanded together with the same
3270 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3271 resulting TREE_PURPOSE will mention the parameter packs in
3272 both the bases and the arguments to the bases. */
3275 tree parameter_packs
= NULL_TREE
;
3277 /* Determine which parameter packs will be used by the base
3279 ppd
.visited
= pointer_set_create ();
3280 ppd
.parameter_packs
= ¶meter_packs
;
3281 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3284 if (parameter_packs
== NULL_TREE
)
3286 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3287 pointer_set_destroy (ppd
.visited
);
3288 return error_mark_node
;
3291 if (TREE_VALUE (arg
) != void_type_node
)
3293 /* Collect the sets of parameter packs used in each of the
3294 initialization arguments. */
3295 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3297 /* Determine which parameter packs will be expanded in this
3299 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3304 pointer_set_destroy (ppd
.visited
);
3306 /* Create the pack expansion type for the base type. */
3307 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3308 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3309 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3311 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3312 they will rarely be compared to anything. */
3313 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3315 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3318 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3321 /* Build the PACK_EXPANSION_* node. */
3323 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3324 : make_node (EXPR_PACK_EXPANSION
);
3325 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3326 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3328 /* Propagate type and const-expression information. */
3329 TREE_TYPE (result
) = TREE_TYPE (arg
);
3330 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3333 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3334 they will rarely be compared to anything. */
3335 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3337 /* Determine which parameter packs will be expanded. */
3338 ppd
.parameter_packs
= ¶meter_packs
;
3339 ppd
.visited
= pointer_set_create ();
3340 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3341 pointer_set_destroy (ppd
.visited
);
3343 /* Make sure we found some parameter packs. */
3344 if (parameter_packs
== NULL_TREE
)
3347 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3349 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3350 return error_mark_node
;
3352 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3354 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3359 /* Checks T for any "bare" parameter packs, which have not yet been
3360 expanded, and issues an error if any are found. This operation can
3361 only be done on full expressions or types (e.g., an expression
3362 statement, "if" condition, etc.), because we could have expressions like:
3364 foo(f(g(h(args)))...)
3366 where "args" is a parameter pack. check_for_bare_parameter_packs
3367 should not be called for the subexpressions args, h(args),
3368 g(h(args)), or f(g(h(args))), because we would produce erroneous
3371 Returns TRUE and emits an error if there were bare parameter packs,
3372 returns FALSE otherwise. */
3374 check_for_bare_parameter_packs (tree t
)
3376 tree parameter_packs
= NULL_TREE
;
3377 struct find_parameter_pack_data ppd
;
3379 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3382 if (TREE_CODE (t
) == TYPE_DECL
)
3385 ppd
.parameter_packs
= ¶meter_packs
;
3386 ppd
.visited
= pointer_set_create ();
3387 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3388 pointer_set_destroy (ppd
.visited
);
3390 if (parameter_packs
)
3392 error ("parameter packs not expanded with %<...%>:");
3393 while (parameter_packs
)
3395 tree pack
= TREE_VALUE (parameter_packs
);
3396 tree name
= NULL_TREE
;
3398 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3399 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3400 name
= TYPE_NAME (pack
);
3401 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3402 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3404 name
= DECL_NAME (pack
);
3407 inform (input_location
, " %qD", name
);
3409 inform (input_location
, " <anonymous>");
3411 parameter_packs
= TREE_CHAIN (parameter_packs
);
3420 /* Expand any parameter packs that occur in the template arguments in
3423 expand_template_argument_pack (tree args
)
3425 tree result_args
= NULL_TREE
;
3426 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3427 int num_result_args
= -1;
3428 int non_default_args_count
= -1;
3430 /* First, determine if we need to expand anything, and the number of
3431 slots we'll need. */
3432 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3434 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3435 if (arg
== NULL_TREE
)
3437 if (ARGUMENT_PACK_P (arg
))
3439 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3440 if (num_result_args
< 0)
3441 num_result_args
= in_arg
+ num_packed
;
3443 num_result_args
+= num_packed
;
3447 if (num_result_args
>= 0)
3452 /* If no expansion is necessary, we're done. */
3453 if (num_result_args
< 0)
3456 /* Expand arguments. */
3457 result_args
= make_tree_vec (num_result_args
);
3458 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3459 non_default_args_count
=
3460 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3461 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3463 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3464 if (ARGUMENT_PACK_P (arg
))
3466 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3467 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3468 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3469 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3470 if (non_default_args_count
> 0)
3471 non_default_args_count
+= num_packed
;
3475 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3479 if (non_default_args_count
>= 0)
3480 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3484 /* Checks if DECL shadows a template parameter.
3486 [temp.local]: A template-parameter shall not be redeclared within its
3487 scope (including nested scopes).
3489 Emits an error and returns TRUE if the DECL shadows a parameter,
3490 returns FALSE otherwise. */
3493 check_template_shadow (tree decl
)
3497 /* If we're not in a template, we can't possibly shadow a template
3499 if (!current_template_parms
)
3502 /* Figure out what we're shadowing. */
3503 if (TREE_CODE (decl
) == OVERLOAD
)
3504 decl
= OVL_CURRENT (decl
);
3505 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3507 /* If there's no previous binding for this name, we're not shadowing
3508 anything, let alone a template parameter. */
3512 /* If we're not shadowing a template parameter, we're done. Note
3513 that OLDDECL might be an OVERLOAD (or perhaps even an
3514 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3516 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3519 /* We check for decl != olddecl to avoid bogus errors for using a
3520 name inside a class. We check TPFI to avoid duplicate errors for
3521 inline member templates. */
3523 || (DECL_TEMPLATE_PARM_P (decl
)
3524 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
3527 error ("declaration of %q+#D", decl
);
3528 error (" shadows template parm %q+#D", olddecl
);
3532 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3533 ORIG_LEVEL, DECL, and TYPE. */
3536 build_template_parm_index (int index
,
3542 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3543 TEMPLATE_PARM_IDX (t
) = index
;
3544 TEMPLATE_PARM_LEVEL (t
) = level
;
3545 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3546 TEMPLATE_PARM_DECL (t
) = decl
;
3547 TREE_TYPE (t
) = type
;
3548 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3549 TREE_READONLY (t
) = TREE_READONLY (decl
);
3554 /* Find the canonical type parameter for the given template type
3555 parameter. Returns the canonical type parameter, which may be TYPE
3556 if no such parameter existed. */
3559 canonical_type_parameter (tree type
)
3562 int idx
= TEMPLATE_TYPE_IDX (type
);
3563 if (!canonical_template_parms
)
3564 vec_alloc (canonical_template_parms
, idx
+1);
3566 while (canonical_template_parms
->length () <= (unsigned)idx
)
3567 vec_safe_push (canonical_template_parms
, NULL_TREE
);
3569 list
= (*canonical_template_parms
)[idx
];
3570 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3571 list
= TREE_CHAIN (list
);
3574 return TREE_VALUE (list
);
3577 (*canonical_template_parms
)[idx
]
3578 = tree_cons (NULL_TREE
, type
,
3579 (*canonical_template_parms
)[idx
]);
3584 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3585 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3586 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3587 new one is created. */
3590 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3591 tsubst_flags_t complain
)
3593 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3594 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3595 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3596 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3598 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3601 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3602 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3603 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3604 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3605 DECL_ARTIFICIAL (decl
) = 1;
3606 SET_DECL_TEMPLATE_PARM_P (decl
);
3608 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3609 TEMPLATE_PARM_LEVEL (index
) - levels
,
3610 TEMPLATE_PARM_ORIG_LEVEL (index
),
3612 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3613 TEMPLATE_PARM_PARAMETER_PACK (t
)
3614 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3616 /* Template template parameters need this. */
3617 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3618 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3619 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3623 return TEMPLATE_PARM_DESCENDANTS (index
);
3626 /* Process information from new template parameter PARM and append it
3627 to the LIST being built. This new parameter is a non-type
3628 parameter iff IS_NON_TYPE is true. This new parameter is a
3629 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3630 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3631 parameter list PARM belongs to. This is used used to create a
3632 proper canonical type for the type of PARM that is to be created,
3633 iff PARM is a type. If the size is not known, this parameter shall
3637 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
3638 bool is_non_type
, bool is_parameter_pack
)
3645 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3646 defval
= TREE_PURPOSE (parm
);
3650 tree p
= tree_last (list
);
3652 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3655 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3656 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3658 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3668 parm
= TREE_VALUE (parm
);
3670 SET_DECL_TEMPLATE_PARM_P (parm
);
3672 if (TREE_TYPE (parm
) == error_mark_node
)
3674 err_parm_list
= build_tree_list (defval
, parm
);
3675 TREE_VALUE (err_parm_list
) = error_mark_node
;
3676 return chainon (list
, err_parm_list
);
3682 The top-level cv-qualifiers on the template-parameter are
3683 ignored when determining its type. */
3684 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3685 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3687 err_parm_list
= build_tree_list (defval
, parm
);
3688 TREE_VALUE (err_parm_list
) = error_mark_node
;
3689 return chainon (list
, err_parm_list
);
3692 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
)
3694 /* This template parameter is not a parameter pack, but it
3695 should be. Complain about "bare" parameter packs. */
3696 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3698 /* Recover by calling this a parameter pack. */
3699 is_parameter_pack
= true;
3703 /* A template parameter is not modifiable. */
3704 TREE_CONSTANT (parm
) = 1;
3705 TREE_READONLY (parm
) = 1;
3706 decl
= build_decl (parm_loc
,
3707 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3708 TREE_CONSTANT (decl
) = 1;
3709 TREE_READONLY (decl
) = 1;
3710 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3711 = build_template_parm_index (idx
, processing_template_decl
,
3712 processing_template_decl
,
3713 decl
, TREE_TYPE (parm
));
3715 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3716 = is_parameter_pack
;
3721 parm
= TREE_VALUE (TREE_VALUE (parm
));
3723 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3725 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3726 /* This is for distinguishing between real templates and template
3727 template parameters */
3728 TREE_TYPE (parm
) = t
;
3729 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3734 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3735 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3736 decl
= build_decl (parm_loc
,
3737 TYPE_DECL
, parm
, t
);
3740 TYPE_NAME (t
) = decl
;
3741 TYPE_STUB_DECL (t
) = decl
;
3743 TEMPLATE_TYPE_PARM_INDEX (t
)
3744 = build_template_parm_index (idx
, processing_template_decl
,
3745 processing_template_decl
,
3746 decl
, TREE_TYPE (parm
));
3747 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3748 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3750 DECL_ARTIFICIAL (decl
) = 1;
3751 SET_DECL_TEMPLATE_PARM_P (decl
);
3753 parm
= build_tree_list (defval
, parm
);
3754 return chainon (list
, parm
);
3757 /* The end of a template parameter list has been reached. Process the
3758 tree list into a parameter vector, converting each parameter into a more
3759 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3763 end_template_parm_list (tree parms
)
3767 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3769 current_template_parms
3770 = tree_cons (size_int (processing_template_decl
),
3771 saved_parmlist
, current_template_parms
);
3773 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3775 next
= TREE_CHAIN (parm
);
3776 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3777 TREE_CHAIN (parm
) = NULL_TREE
;
3780 --processing_template_parmlist
;
3782 return saved_parmlist
;
3785 /* end_template_decl is called after a template declaration is seen. */
3788 end_template_decl (void)
3790 reset_specialization ();
3792 if (! processing_template_decl
)
3795 /* This matches the pushlevel in begin_template_parm_list. */
3798 --processing_template_decl
;
3799 current_template_parms
= TREE_CHAIN (current_template_parms
);
3802 /* Takes a TREE_LIST representing a template parameter and convert it
3803 into an argument suitable to be passed to the type substitution
3804 functions. Note that If the TREE_LIST contains an error_mark
3805 node, the returned argument is error_mark_node. */
3808 template_parm_to_arg (tree t
)
3812 || TREE_CODE (t
) != TREE_LIST
)
3815 if (error_operand_p (TREE_VALUE (t
)))
3816 return error_mark_node
;
3820 if (TREE_CODE (t
) == TYPE_DECL
3821 || TREE_CODE (t
) == TEMPLATE_DECL
)
3825 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3827 /* Turn this argument into a TYPE_ARGUMENT_PACK
3828 with a single element, which expands T. */
3829 tree vec
= make_tree_vec (1);
3830 #ifdef ENABLE_CHECKING
3831 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3832 (vec
, TREE_VEC_LENGTH (vec
));
3834 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3836 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
3837 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3842 t
= DECL_INITIAL (t
);
3844 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3846 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3847 with a single element, which expands T. */
3848 tree vec
= make_tree_vec (1);
3849 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3850 #ifdef ENABLE_CHECKING
3851 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3852 (vec
, TREE_VEC_LENGTH (vec
));
3854 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3856 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3857 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3858 TREE_TYPE (t
) = type
;
3864 /* Given a set of template parameters, return them as a set of template
3865 arguments. The template parameters are represented as a TREE_VEC, in
3866 the form documented in cp-tree.h for template arguments. */
3869 template_parms_to_args (tree parms
)
3872 tree args
= NULL_TREE
;
3873 int length
= TMPL_PARMS_DEPTH (parms
);
3876 /* If there is only one level of template parameters, we do not
3877 create a TREE_VEC of TREE_VECs. Instead, we return a single
3878 TREE_VEC containing the arguments. */
3880 args
= make_tree_vec (length
);
3882 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
3884 tree a
= copy_node (TREE_VALUE (header
));
3887 TREE_TYPE (a
) = NULL_TREE
;
3888 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
3889 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
3891 #ifdef ENABLE_CHECKING
3892 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
3896 TREE_VEC_ELT (args
, --l
) = a
;
3901 if (length
> 1 && TREE_VEC_ELT (args
, 0) == NULL_TREE
)
3902 /* This can happen for template parms of a template template
3905 template<template<class T, class U> class TT> struct S;
3907 Consider the level of the parms of TT; T and U both have
3908 level 2; TT has no template parm of level 1. So in this case
3909 the first element of full_template_args is NULL_TREE. If we
3910 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
3911 of 2. This will make tsubst wrongly consider that T and U
3912 have level 1. Instead, let's create a dummy vector as the
3913 first element of full_template_args so that TMPL_ARGS_DEPTH
3914 returns the correct depth for args. */
3915 TREE_VEC_ELT (args
, 0) = make_tree_vec (1);
3919 /* Within the declaration of a template, return the currently active
3920 template parameters as an argument TREE_VEC. */
3923 current_template_args (void)
3925 return template_parms_to_args (current_template_parms
);
3928 /* Update the declared TYPE by doing any lookups which were thought to be
3929 dependent, but are not now that we know the SCOPE of the declarator. */
3932 maybe_update_decl_type (tree orig_type
, tree scope
)
3934 tree type
= orig_type
;
3936 if (type
== NULL_TREE
)
3939 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3940 type
= TREE_TYPE (type
);
3942 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
3943 && dependent_type_p (type
)
3944 /* Don't bother building up the args in this case. */
3945 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
3947 /* tsubst in the args corresponding to the template parameters,
3948 including auto if present. Most things will be unchanged, but
3949 make_typename_type and tsubst_qualified_id will resolve
3950 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3951 tree args
= current_template_args ();
3952 tree auto_node
= type_uses_auto (type
);
3956 tree auto_vec
= make_tree_vec (1);
3957 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
3958 args
= add_to_template_args (args
, auto_vec
);
3960 pushed
= push_scope (scope
);
3961 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
3966 if (type
== error_mark_node
)
3969 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3971 if (same_type_p (type
, TREE_TYPE (orig_type
)))
3974 type
= TYPE_NAME (type
);
3979 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3980 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3981 a member template. Used by push_template_decl below. */
3984 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
3986 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
3987 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
3988 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
3989 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3990 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
3995 struct template_parm_data
3997 /* The level of the template parameters we are currently
4001 /* The index of the specialization argument we are currently
4005 /* An array whose size is the number of template parameters. The
4006 elements are nonzero if the parameter has been used in any one
4007 of the arguments processed so far. */
4010 /* An array whose size is the number of template arguments. The
4011 elements are nonzero if the argument makes use of template
4012 parameters of this level. */
4013 int* arg_uses_template_parms
;
4016 /* Subroutine of push_template_decl used to see if each template
4017 parameter in a partial specialization is used in the explicit
4018 argument list. If T is of the LEVEL given in DATA (which is
4019 treated as a template_parm_data*), then DATA->PARMS is marked
4023 mark_template_parm (tree t
, void* data
)
4027 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4029 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4031 level
= TEMPLATE_PARM_LEVEL (t
);
4032 idx
= TEMPLATE_PARM_IDX (t
);
4036 level
= TEMPLATE_TYPE_LEVEL (t
);
4037 idx
= TEMPLATE_TYPE_IDX (t
);
4040 if (level
== tpd
->level
)
4042 tpd
->parms
[idx
] = 1;
4043 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4046 /* Return zero so that for_each_template_parm will continue the
4047 traversal of the tree; we want to mark *every* template parm. */
4051 /* Process the partial specialization DECL. */
4054 process_partial_specialization (tree decl
)
4056 tree type
= TREE_TYPE (decl
);
4057 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
4058 tree specargs
= CLASSTYPE_TI_ARGS (type
);
4059 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4060 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4063 int nargs
= TREE_VEC_LENGTH (inner_args
);
4066 bool did_error_intro
= false;
4067 struct template_parm_data tpd
;
4068 struct template_parm_data tpd2
;
4070 gcc_assert (current_template_parms
);
4072 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4073 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4075 /* We check that each of the template parameters given in the
4076 partial specialization is used in the argument list to the
4077 specialization. For example:
4079 template <class T> struct S;
4080 template <class T> struct S<T*>;
4082 The second declaration is OK because `T*' uses the template
4083 parameter T, whereas
4085 template <class T> struct S<int>;
4087 is no good. Even trickier is:
4098 The S2<T> declaration is actually invalid; it is a
4099 full-specialization. Of course,
4102 struct S2<T (*)(U)>;
4104 or some such would have been OK. */
4105 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4106 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4107 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4109 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4110 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4111 for (i
= 0; i
< nargs
; ++i
)
4113 tpd
.current_arg
= i
;
4114 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4115 &mark_template_parm
,
4118 /*include_nondeduced_p=*/false);
4120 for (i
= 0; i
< ntparms
; ++i
)
4121 if (tpd
.parms
[i
] == 0)
4123 /* One of the template parms was not used in the
4125 if (!did_error_intro
)
4127 error ("template parameters not used in partial specialization:");
4128 did_error_intro
= true;
4131 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4134 if (did_error_intro
)
4135 return error_mark_node
;
4137 /* [temp.class.spec]
4139 The argument list of the specialization shall not be identical to
4140 the implicit argument list of the primary template. */
4141 if (comp_template_args
4143 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4145 error ("partial specialization %qT does not specialize any template arguments", type
);
4147 /* A partial specialization that replaces multiple parameters of the
4148 primary template with a pack expansion is less specialized for those
4150 if (nargs
< DECL_NTPARMS (maintmpl
))
4152 error ("partial specialization is not more specialized than the "
4153 "primary template because it replaces multiple parameters "
4154 "with a pack expansion");
4155 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4159 /* [temp.class.spec]
4161 A partially specialized non-type argument expression shall not
4162 involve template parameters of the partial specialization except
4163 when the argument expression is a simple identifier.
4165 The type of a template parameter corresponding to a specialized
4166 non-type argument shall not be dependent on a parameter of the
4169 Also, we verify that pack expansions only occur at the
4170 end of the argument list. */
4171 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4173 for (i
= 0; i
< nargs
; ++i
)
4175 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4176 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4177 tree packed_args
= NULL_TREE
;
4180 if (ARGUMENT_PACK_P (arg
))
4182 /* Extract the arguments from the argument pack. We'll be
4183 iterating over these in the following loop. */
4184 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4185 len
= TREE_VEC_LENGTH (packed_args
);
4188 for (j
= 0; j
< len
; j
++)
4191 /* Get the Jth argument in the parameter pack. */
4192 arg
= TREE_VEC_ELT (packed_args
, j
);
4194 if (PACK_EXPANSION_P (arg
))
4196 /* Pack expansions must come at the end of the
4198 if ((packed_args
&& j
< len
- 1)
4199 || (!packed_args
&& i
< nargs
- 1))
4201 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4202 error ("parameter pack argument %qE must be at the "
4203 "end of the template argument list", arg
);
4205 error ("parameter pack argument %qT must be at the "
4206 "end of the template argument list", arg
);
4210 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4211 /* We only care about the pattern. */
4212 arg
= PACK_EXPANSION_PATTERN (arg
);
4214 if (/* These first two lines are the `non-type' bit. */
4216 && TREE_CODE (arg
) != TEMPLATE_DECL
4217 /* This next line is the `argument expression is not just a
4218 simple identifier' condition and also the `specialized
4219 non-type argument' bit. */
4220 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
4222 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4223 || (packed_args
&& uses_template_parms (arg
)))
4224 error ("template argument %qE involves template parameter(s)",
4228 /* Look at the corresponding template parameter,
4229 marking which template parameters its type depends
4231 tree type
= TREE_TYPE (parm
);
4235 /* We haven't yet initialized TPD2. Do so now. */
4236 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4237 /* The number of parameters here is the number in the
4238 main template, which, as checked in the assertion
4240 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4242 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4245 /* Mark the template parameters. But this time, we're
4246 looking for the template parameters of the main
4247 template, not in the specialization. */
4248 tpd2
.current_arg
= i
;
4249 tpd2
.arg_uses_template_parms
[i
] = 0;
4250 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4251 for_each_template_parm (type
,
4252 &mark_template_parm
,
4255 /*include_nondeduced_p=*/false);
4257 if (tpd2
.arg_uses_template_parms
[i
])
4259 /* The type depended on some template parameters.
4260 If they are fully specialized in the
4261 specialization, that's OK. */
4264 for (j
= 0; j
< nargs
; ++j
)
4265 if (tpd2
.parms
[j
] != 0
4266 && tpd
.arg_uses_template_parms
[j
])
4269 error_n (input_location
, count
,
4270 "type %qT of template argument %qE depends "
4271 "on a template parameter",
4272 "type %qT of template argument %qE depends "
4273 "on template parameters",
4282 /* We should only get here once. */
4283 gcc_assert (!COMPLETE_TYPE_P (type
));
4285 tree tmpl
= build_template_decl (decl
, current_template_parms
,
4286 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4287 TREE_TYPE (tmpl
) = type
;
4288 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4289 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4290 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4291 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4293 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4294 = tree_cons (specargs
, tmpl
,
4295 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4296 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4298 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4299 inst
= TREE_CHAIN (inst
))
4301 tree inst_type
= TREE_VALUE (inst
);
4302 if (COMPLETE_TYPE_P (inst_type
)
4303 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type
))
4305 tree spec
= most_specialized_class (inst_type
, maintmpl
, tf_none
);
4306 if (spec
&& TREE_TYPE (spec
) == type
)
4307 permerror (input_location
,
4308 "partial specialization of %qT after instantiation "
4309 "of %qT", type
, inst_type
);
4316 /* Check that a template declaration's use of default arguments and
4317 parameter packs is not invalid. Here, PARMS are the template
4318 parameters. IS_PRIMARY is true if DECL is the thing declared by
4319 a primary template. IS_PARTIAL is true if DECL is a partial
4322 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4323 declaration (but not a definition); 1 indicates a declaration, 2
4324 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4325 emitted for extraneous default arguments.
4327 Returns TRUE if there were no errors found, FALSE otherwise. */
4330 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4331 bool is_partial
, int is_friend_decl
)
4334 int last_level_to_check
;
4336 bool no_errors
= true;
4340 A default template-argument shall not be specified in a
4341 function template declaration or a function template definition, nor
4342 in the template-parameter-list of the definition of a member of a
4345 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
4346 /* You can't have a function template declaration in a local
4347 scope, nor you can you define a member of a class template in a
4351 if (TREE_CODE (decl
) == TYPE_DECL
4353 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4354 /* A lambda doesn't have an explicit declaration; don't complain
4355 about the parms of the enclosing class. */
4358 if (current_class_type
4359 && !TYPE_BEING_DEFINED (current_class_type
)
4360 && DECL_LANG_SPECIFIC (decl
)
4361 && DECL_DECLARES_FUNCTION_P (decl
)
4362 /* If this is either a friend defined in the scope of the class
4363 or a member function. */
4364 && (DECL_FUNCTION_MEMBER_P (decl
)
4365 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4366 : DECL_FRIEND_CONTEXT (decl
)
4367 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4369 /* And, if it was a member function, it really was defined in
4370 the scope of the class. */
4371 && (!DECL_FUNCTION_MEMBER_P (decl
)
4372 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4373 /* We already checked these parameters when the template was
4374 declared, so there's no need to do it again now. This function
4375 was defined in class scope, but we're processing its body now
4376 that the class is complete. */
4379 /* Core issue 226 (C++0x only): the following only applies to class
4382 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
4386 If a template-parameter has a default template-argument, all
4387 subsequent template-parameters shall have a default
4388 template-argument supplied. */
4389 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4391 tree inner_parms
= TREE_VALUE (parm_level
);
4392 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4393 int seen_def_arg_p
= 0;
4396 for (i
= 0; i
< ntparms
; ++i
)
4398 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4400 if (parm
== error_mark_node
)
4403 if (TREE_PURPOSE (parm
))
4405 else if (seen_def_arg_p
4406 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4408 error ("no default argument for %qD", TREE_VALUE (parm
));
4409 /* For better subsequent error-recovery, we indicate that
4410 there should have been a default argument. */
4411 TREE_PURPOSE (parm
) = error_mark_node
;
4414 else if (!is_partial
4416 /* Don't complain about an enclosing partial
4418 && parm_level
== parms
4419 && TREE_CODE (decl
) == TYPE_DECL
4421 && template_parameter_pack_p (TREE_VALUE (parm
)))
4423 /* A primary class template can only have one
4424 parameter pack, at the end of the template
4427 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
4428 error ("parameter pack %qE must be at the end of the"
4429 " template parameter list", TREE_VALUE (parm
));
4431 error ("parameter pack %qT must be at the end of the"
4432 " template parameter list",
4433 TREE_TYPE (TREE_VALUE (parm
)));
4435 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4443 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4447 /* For an ordinary class template, default template arguments are
4448 allowed at the innermost level, e.g.:
4449 template <class T = int>
4451 but, in a partial specialization, they're not allowed even
4452 there, as we have in [temp.class.spec]:
4454 The template parameter list of a specialization shall not
4455 contain default template argument values.
4457 So, for a partial specialization, or for a function template
4458 (in C++98/C++03), we look at all of them. */
4461 /* But, for a primary class template that is not a partial
4462 specialization we look at all template parameters except the
4464 parms
= TREE_CHAIN (parms
);
4466 /* Figure out what error message to issue. */
4467 if (is_friend_decl
== 2)
4468 msg
= G_("default template arguments may not be used in function template "
4469 "friend re-declaration");
4470 else if (is_friend_decl
)
4471 msg
= G_("default template arguments may not be used in function template "
4472 "friend declarations");
4473 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4474 msg
= G_("default template arguments may not be used in function templates "
4475 "without -std=c++11 or -std=gnu++11");
4476 else if (is_partial
)
4477 msg
= G_("default template arguments may not be used in "
4478 "partial specializations");
4480 msg
= G_("default argument for template parameter for class enclosing %qD");
4482 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4483 /* If we're inside a class definition, there's no need to
4484 examine the parameters to the class itself. On the one
4485 hand, they will be checked when the class is defined, and,
4486 on the other, default arguments are valid in things like:
4487 template <class T = double>
4488 struct S { template <class U> void f(U); };
4489 Here the default argument for `S' has no bearing on the
4490 declaration of `f'. */
4491 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4493 /* Check everything. */
4494 last_level_to_check
= 0;
4496 for (parm_level
= parms
;
4497 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4498 parm_level
= TREE_CHAIN (parm_level
))
4500 tree inner_parms
= TREE_VALUE (parm_level
);
4504 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4505 for (i
= 0; i
< ntparms
; ++i
)
4507 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4510 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4515 if (is_friend_decl
== 2)
4522 /* Clear out the default argument so that we are not
4524 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4528 /* At this point, if we're still interested in issuing messages,
4529 they must apply to classes surrounding the object declared. */
4531 msg
= G_("default argument for template parameter for class "
4538 /* Worker for push_template_decl_real, called via
4539 for_each_template_parm. DATA is really an int, indicating the
4540 level of the parameters we are interested in. If T is a template
4541 parameter of that level, return nonzero. */
4544 template_parm_this_level_p (tree t
, void* data
)
4546 int this_level
= *(int *)data
;
4549 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4550 level
= TEMPLATE_PARM_LEVEL (t
);
4552 level
= TEMPLATE_TYPE_LEVEL (t
);
4553 return level
== this_level
;
4556 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4557 parameters given by current_template_args, or reuses a
4558 previously existing one, if appropriate. Returns the DECL, or an
4559 equivalent one, if it is replaced via a call to duplicate_decls.
4561 If IS_FRIEND is true, DECL is a friend declaration. */
4564 push_template_decl_real (tree decl
, bool is_friend
)
4572 int new_template_p
= 0;
4573 /* True if the template is a member template, in the sense of
4575 bool member_template_p
= false;
4577 if (decl
== error_mark_node
|| !current_template_parms
)
4578 return error_mark_node
;
4580 /* See if this is a partial specialization. */
4581 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
4582 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4583 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
4585 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4589 /* For a friend, we want the context of the friend function, not
4590 the type of which it is a friend. */
4591 ctx
= CP_DECL_CONTEXT (decl
);
4592 else if (CP_DECL_CONTEXT (decl
)
4593 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4594 /* In the case of a virtual function, we want the class in which
4596 ctx
= CP_DECL_CONTEXT (decl
);
4598 /* Otherwise, if we're currently defining some class, the DECL
4599 is assumed to be a member of the class. */
4600 ctx
= current_scope ();
4602 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4605 if (!DECL_CONTEXT (decl
))
4606 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4608 /* See if this is a primary template. */
4609 if (is_friend
&& ctx
)
4610 /* A friend template that specifies a class context, i.e.
4611 template <typename T> friend void A<T>::f();
4615 is_primary
= template_parm_scope_p ();
4619 if (DECL_CLASS_SCOPE_P (decl
))
4620 member_template_p
= true;
4621 if (TREE_CODE (decl
) == TYPE_DECL
4622 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4624 error ("template class without a name");
4625 return error_mark_node
;
4627 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4629 if (DECL_DESTRUCTOR_P (decl
))
4633 A destructor shall not be a member template. */
4634 error ("destructor %qD declared as member template", decl
);
4635 return error_mark_node
;
4637 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4638 && (!prototype_p (TREE_TYPE (decl
))
4639 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4640 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4641 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4642 == void_list_node
)))
4644 /* [basic.stc.dynamic.allocation]
4646 An allocation function can be a function
4647 template. ... Template allocation functions shall
4648 have two or more parameters. */
4649 error ("invalid template declaration of %qD", decl
);
4650 return error_mark_node
;
4653 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4654 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4656 else if (TREE_CODE (decl
) == TYPE_DECL
4657 && TYPE_DECL_ALIAS_P (decl
))
4658 /* alias-declaration */
4659 gcc_assert (!DECL_ARTIFICIAL (decl
));
4662 error ("template declaration of %q#D", decl
);
4663 return error_mark_node
;
4667 /* Check to see that the rules regarding the use of default
4668 arguments are not being violated. */
4669 check_default_tmpl_args (decl
, current_template_parms
,
4670 is_primary
, is_partial
, /*is_friend_decl=*/0);
4672 /* Ensure that there are no parameter packs in the type of this
4673 declaration that have not been expanded. */
4674 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4676 /* Check each of the arguments individually to see if there are
4677 any bare parameter packs. */
4678 tree type
= TREE_TYPE (decl
);
4679 tree arg
= DECL_ARGUMENTS (decl
);
4680 tree argtype
= TYPE_ARG_TYPES (type
);
4682 while (arg
&& argtype
)
4684 if (!DECL_PACK_P (arg
)
4685 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4687 /* This is a PARM_DECL that contains unexpanded parameter
4688 packs. We have already complained about this in the
4689 check_for_bare_parameter_packs call, so just replace
4690 these types with ERROR_MARK_NODE. */
4691 TREE_TYPE (arg
) = error_mark_node
;
4692 TREE_VALUE (argtype
) = error_mark_node
;
4695 arg
= DECL_CHAIN (arg
);
4696 argtype
= TREE_CHAIN (argtype
);
4699 /* Check for bare parameter packs in the return type and the
4700 exception specifiers. */
4701 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4702 /* Errors were already issued, set return type to int
4703 as the frontend doesn't expect error_mark_node as
4705 TREE_TYPE (type
) = integer_type_node
;
4706 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4707 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4709 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
4710 && TYPE_DECL_ALIAS_P (decl
))
4711 ? DECL_ORIGINAL_TYPE (decl
)
4712 : TREE_TYPE (decl
)))
4714 TREE_TYPE (decl
) = error_mark_node
;
4715 return error_mark_node
;
4719 return process_partial_specialization (decl
);
4721 args
= current_template_args ();
4724 || TREE_CODE (ctx
) == FUNCTION_DECL
4725 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4726 || (TREE_CODE (decl
) == TYPE_DECL
4727 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4728 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4730 if (DECL_LANG_SPECIFIC (decl
)
4731 && DECL_TEMPLATE_INFO (decl
)
4732 && DECL_TI_TEMPLATE (decl
))
4733 tmpl
= DECL_TI_TEMPLATE (decl
);
4734 /* If DECL is a TYPE_DECL for a class-template, then there won't
4735 be DECL_LANG_SPECIFIC. The information equivalent to
4736 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4737 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4738 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4739 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4741 /* Since a template declaration already existed for this
4742 class-type, we must be redeclaring it here. Make sure
4743 that the redeclaration is valid. */
4744 redeclare_class_template (TREE_TYPE (decl
),
4745 current_template_parms
);
4746 /* We don't need to create a new TEMPLATE_DECL; just use the
4747 one we already had. */
4748 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4752 tmpl
= build_template_decl (decl
, current_template_parms
,
4756 if (DECL_LANG_SPECIFIC (decl
)
4757 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4759 /* A specialization of a member template of a template
4761 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4762 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4763 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4769 tree a
, t
, current
, parms
;
4771 tree tinfo
= get_template_info (decl
);
4775 error ("template definition of non-template %q#D", decl
);
4776 return error_mark_node
;
4779 tmpl
= TI_TEMPLATE (tinfo
);
4781 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4782 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4783 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4784 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4788 /* The declaration is a specialization of a member
4789 template, declared outside the class. Therefore, the
4790 innermost template arguments will be NULL, so we
4791 replace them with the arguments determined by the
4792 earlier call to check_explicit_specialization. */
4793 args
= DECL_TI_ARGS (decl
);
4796 = build_template_decl (decl
, current_template_parms
,
4798 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4799 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4800 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4801 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4802 DECL_TEMPLATE_INFO (new_tmpl
)
4803 = build_template_info (tmpl
, args
);
4805 register_specialization (new_tmpl
,
4806 most_general_template (tmpl
),
4812 /* Make sure the template headers we got make sense. */
4814 parms
= DECL_TEMPLATE_PARMS (tmpl
);
4815 i
= TMPL_PARMS_DEPTH (parms
);
4816 if (TMPL_ARGS_DEPTH (args
) != i
)
4818 error ("expected %d levels of template parms for %q#D, got %d",
4819 i
, decl
, TMPL_ARGS_DEPTH (args
));
4822 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
4824 a
= TMPL_ARGS_LEVEL (args
, i
);
4825 t
= INNERMOST_TEMPLATE_PARMS (parms
);
4827 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
4829 if (current
== decl
)
4830 error ("got %d template parameters for %q#D",
4831 TREE_VEC_LENGTH (a
), decl
);
4833 error ("got %d template parameters for %q#T",
4834 TREE_VEC_LENGTH (a
), current
);
4835 error (" but %d required", TREE_VEC_LENGTH (t
));
4836 /* Avoid crash in import_export_decl. */
4837 DECL_INTERFACE_KNOWN (decl
) = 1;
4838 return error_mark_node
;
4841 if (current
== decl
)
4843 else if (current
== NULL_TREE
)
4844 /* Can happen in erroneous input. */
4847 current
= get_containing_scope (current
);
4850 /* Check that the parms are used in the appropriate qualifying scopes
4851 in the declarator. */
4852 if (!comp_template_args
4854 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
4857 template arguments to %qD do not match original template %qD",
4858 decl
, DECL_TEMPLATE_RESULT (tmpl
));
4859 if (!uses_template_parms (TI_ARGS (tinfo
)))
4860 inform (input_location
, "use template<> for an explicit specialization");
4861 /* Avoid crash in import_export_decl. */
4862 DECL_INTERFACE_KNOWN (decl
) = 1;
4863 return error_mark_node
;
4867 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4868 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4870 /* Push template declarations for global functions and types. Note
4871 that we do not try to push a global template friend declared in a
4872 template class; such a thing may well depend on the template
4873 parameters of the class. */
4874 if (new_template_p
&& !ctx
4875 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
4877 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
4878 if (tmpl
== error_mark_node
)
4879 return error_mark_node
;
4881 /* Hide template friend classes that haven't been declared yet. */
4882 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
4884 DECL_ANTICIPATED (tmpl
) = 1;
4885 DECL_FRIEND_P (tmpl
) = 1;
4891 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
4894 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4895 if (DECL_CONV_FN_P (tmpl
))
4897 int depth
= TMPL_PARMS_DEPTH (parms
);
4899 /* It is a conversion operator. See if the type converted to
4900 depends on innermost template operands. */
4902 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
4904 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
4907 /* Give template template parms a DECL_CONTEXT of the template
4908 for which they are a parameter. */
4909 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4910 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
4912 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4913 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4914 DECL_CONTEXT (parm
) = tmpl
;
4918 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4919 back to its most general template. If TMPL is a specialization,
4920 ARGS may only have the innermost set of arguments. Add the missing
4921 argument levels if necessary. */
4922 if (DECL_TEMPLATE_INFO (tmpl
))
4923 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
4925 info
= build_template_info (tmpl
, args
);
4927 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
4928 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
4931 if (is_primary
&& !DECL_LANG_SPECIFIC (decl
))
4932 retrofit_lang_decl (decl
);
4933 if (DECL_LANG_SPECIFIC (decl
))
4934 DECL_TEMPLATE_INFO (decl
) = info
;
4937 return DECL_TEMPLATE_RESULT (tmpl
);
4941 push_template_decl (tree decl
)
4943 return push_template_decl_real (decl
, false);
4946 /* FN is an inheriting constructor that inherits from the constructor
4947 template INHERITED; turn FN into a constructor template with a matching
4951 add_inherited_template_parms (tree fn
, tree inherited
)
4954 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
4955 inner_parms
= copy_node (inner_parms
);
4957 = tree_cons (size_int (processing_template_decl
+ 1),
4958 inner_parms
, current_template_parms
);
4959 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
4960 tree args
= template_parms_to_args (parms
);
4961 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
4962 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
4963 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
4964 DECL_ARTIFICIAL (tmpl
) = true;
4965 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4969 /* Called when a class template TYPE is redeclared with the indicated
4970 template PARMS, e.g.:
4972 template <class T> struct S;
4973 template <class T> struct S {}; */
4976 redeclare_class_template (tree type
, tree parms
)
4982 if (!TYPE_TEMPLATE_INFO (type
))
4984 error ("%qT is not a template type", type
);
4988 tmpl
= TYPE_TI_TEMPLATE (type
);
4989 if (!PRIMARY_TEMPLATE_P (tmpl
))
4990 /* The type is nested in some template class. Nothing to worry
4991 about here; there are no new template parameters for the nested
4997 error ("template specifiers not specified in declaration of %qD",
5002 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5003 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5005 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5007 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5008 "redeclared with %d template parameter",
5009 "redeclared with %d template parameters",
5010 TREE_VEC_LENGTH (parms
));
5011 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
5012 "previous declaration %q+D used %d template parameter",
5013 "previous declaration %q+D used %d template parameters",
5014 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5018 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5025 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5026 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5029 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5030 if (tmpl_parm
== error_mark_node
)
5033 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5034 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5035 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5037 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5039 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5040 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5041 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5042 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5043 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5044 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5045 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5046 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5047 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5049 error ("template parameter %q+#D", tmpl_parm
);
5050 error ("redeclared here as %q#D", parm
);
5054 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5056 /* We have in [temp.param]:
5058 A template-parameter may not be given default arguments
5059 by two different declarations in the same scope. */
5060 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5061 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5062 "original definition appeared here");
5066 if (parm_default
!= NULL_TREE
)
5067 /* Update the previous template parameters (which are the ones
5068 that will really count) with the new default value. */
5069 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5070 else if (tmpl_default
!= NULL_TREE
)
5071 /* Update the new parameters, too; they'll be used as the
5072 parameters for any members. */
5073 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5079 /* Simplify EXPR if it is a non-dependent expression. Returns the
5080 (possibly simplified) expression. */
5083 fold_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5085 if (expr
== NULL_TREE
)
5088 /* If we're in a template, but EXPR isn't value dependent, simplify
5089 it. We're supposed to treat:
5091 template <typename T> void f(T[1 + 1]);
5092 template <typename T> void f(T[2]);
5094 as two declarations of the same function, for example. */
5095 if (processing_template_decl
5096 && !instantiation_dependent_expression_p (expr
)
5097 && potential_constant_expression (expr
))
5099 HOST_WIDE_INT saved_processing_template_decl
;
5101 saved_processing_template_decl
= processing_template_decl
;
5102 processing_template_decl
= 0;
5103 expr
= tsubst_copy_and_build (expr
,
5106 /*in_decl=*/NULL_TREE
,
5107 /*function_p=*/false,
5108 /*integral_constant_expression_p=*/true);
5109 processing_template_decl
= saved_processing_template_decl
;
5115 fold_non_dependent_expr (tree expr
)
5117 return fold_non_dependent_expr_sfinae (expr
, tf_error
);
5120 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5121 template declaration, or a TYPE_DECL for an alias declaration. */
5124 alias_type_or_template_p (tree t
)
5128 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5131 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5132 || DECL_ALIAS_TEMPLATE_P (t
));
5135 /* Return TRUE iff is a specialization of an alias template. */
5138 alias_template_specialization_p (const_tree t
)
5144 && TYPE_TEMPLATE_INFO (t
)
5145 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t
))
5146 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t
)));
5149 /* Return either TMPL or another template that it is equivalent to under DR
5150 1286: An alias that just changes the name of a template is equivalent to
5151 the other template. */
5154 get_underlying_template (tree tmpl
)
5156 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
5157 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
5159 tree result
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5160 if (TYPE_TEMPLATE_INFO (result
))
5162 tree sub
= TYPE_TI_TEMPLATE (result
);
5163 if (PRIMARY_TEMPLATE_P (sub
)
5164 && same_type_p (result
, TREE_TYPE (sub
)))
5166 /* The alias type is equivalent to the pattern of the
5167 underlying template, so strip the alias. */
5177 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5178 must be a function or a pointer-to-function type, as specified
5179 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5180 and check that the resulting function has external linkage. */
5183 convert_nontype_argument_function (tree type
, tree expr
)
5187 linkage_kind linkage
;
5189 fn
= instantiate_type (type
, fns
, tf_none
);
5190 if (fn
== error_mark_node
)
5191 return error_mark_node
;
5194 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5195 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5196 if (BASELINK_P (fn_no_ptr
))
5197 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5199 /* [temp.arg.nontype]/1
5201 A template-argument for a non-type, non-template template-parameter
5204 -- the address of an object or function with external [C++11: or
5205 internal] linkage. */
5207 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
5209 error ("%qE is not a valid template argument for type %qT", expr
, type
);
5210 if (TYPE_PTR_P (type
))
5211 error ("it must be the address of a function with external linkage");
5213 error ("it must be the name of a function with external linkage");
5217 linkage
= decl_linkage (fn_no_ptr
);
5218 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
5220 if (cxx_dialect
>= cxx11
)
5221 error ("%qE is not a valid template argument for type %qT "
5222 "because %qD has no linkage",
5223 expr
, type
, fn_no_ptr
);
5225 error ("%qE is not a valid template argument for type %qT "
5226 "because %qD does not have external linkage",
5227 expr
, type
, fn_no_ptr
);
5234 /* Subroutine of convert_nontype_argument.
5235 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5236 Emit an error otherwise. */
5239 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5240 tsubst_flags_t complain
)
5243 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5245 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
5247 if (complain
& tf_error
)
5249 error ("%qE is not a valid template argument for type %qT",
5251 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5256 /* Returns TRUE iff the address of OP is value-dependent.
5258 14.6.2.4 [temp.dep.temp]:
5259 A non-integral non-type template-argument is dependent if its type is
5260 dependent or it has either of the following forms
5263 and contains a nested-name-specifier which specifies a class-name that
5264 names a dependent type.
5266 We generalize this to just say that the address of a member of a
5267 dependent class is value-dependent; the above doesn't cover the
5268 address of a static data member named with an unqualified-id. */
5271 has_value_dependent_address (tree op
)
5273 /* We could use get_inner_reference here, but there's no need;
5274 this is only relevant for template non-type arguments, which
5275 can only be expressed as &id-expression. */
5278 tree ctx
= CP_DECL_CONTEXT (op
);
5279 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5286 /* The next set of functions are used for providing helpful explanatory
5287 diagnostics for failed overload resolution. Their messages should be
5288 indented by two spaces for consistency with the messages in
5292 unify_success (bool /*explain_p*/)
5298 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5301 inform (input_location
,
5302 " couldn't deduce template parameter %qD", parm
);
5307 unify_invalid (bool /*explain_p*/)
5313 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5316 inform (input_location
,
5317 " types %qT and %qT have incompatible cv-qualifiers",
5323 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5326 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5331 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
5334 inform (input_location
,
5335 " template parameter %qD is not a parameter pack, but "
5342 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
5345 inform (input_location
,
5346 " template argument %qE does not match "
5347 "pointer-to-member constant %qE",
5353 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
5356 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
5361 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
5364 inform (input_location
,
5365 " inconsistent parameter pack deduction with %qT and %qT",
5371 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
5376 inform (input_location
,
5377 " deduced conflicting types for parameter %qT (%qT and %qT)",
5378 parm
, first
, second
);
5380 inform (input_location
,
5381 " deduced conflicting values for non-type parameter "
5382 "%qE (%qE and %qE)", parm
, first
, second
);
5388 unify_vla_arg (bool explain_p
, tree arg
)
5391 inform (input_location
,
5392 " variable-sized array type %qT is not "
5393 "a valid template argument",
5399 unify_method_type_error (bool explain_p
, tree arg
)
5402 inform (input_location
,
5403 " member function type %qT is not a valid template argument",
5409 unify_arity (bool explain_p
, int have
, int wanted
)
5412 inform_n (input_location
, wanted
,
5413 " candidate expects %d argument, %d provided",
5414 " candidate expects %d arguments, %d provided",
5420 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
5422 return unify_arity (explain_p
, have
, wanted
);
5426 unify_too_few_arguments (bool explain_p
, int have
, int wanted
)
5428 return unify_arity (explain_p
, have
, wanted
);
5432 unify_arg_conversion (bool explain_p
, tree to_type
,
5433 tree from_type
, tree arg
)
5436 inform (EXPR_LOC_OR_HERE (arg
),
5437 " cannot convert %qE (type %qT) to type %qT",
5438 arg
, from_type
, to_type
);
5443 unify_no_common_base (bool explain_p
, enum template_base_result r
,
5444 tree parm
, tree arg
)
5449 case tbr_ambiguous_baseclass
:
5450 inform (input_location
, " %qT is an ambiguous base class of %qT",
5454 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
5461 unify_inconsistent_template_template_parameters (bool explain_p
)
5464 inform (input_location
,
5465 " template parameters of a template template argument are "
5466 "inconsistent with other deduced template arguments");
5471 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
5474 inform (input_location
,
5475 " can't deduce a template for %qT from non-template type %qT",
5481 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
5484 inform (input_location
,
5485 " template argument %qE does not match %qD", arg
, parm
);
5490 unify_overload_resolution_failure (bool explain_p
, tree arg
)
5493 inform (input_location
,
5494 " could not resolve address from overloaded function %qE",
5499 /* Attempt to convert the non-type template parameter EXPR to the
5500 indicated TYPE. If the conversion is successful, return the
5501 converted value. If the conversion is unsuccessful, return
5502 NULL_TREE if we issued an error message, or error_mark_node if we
5503 did not. We issue error messages for out-and-out bad template
5504 parameters, but not simply because the conversion failed, since we
5505 might be just trying to do argument deduction. Both TYPE and EXPR
5506 must be non-dependent.
5508 The conversion follows the special rules described in
5509 [temp.arg.nontype], and it is much more strict than an implicit
5512 This function is called twice for each template argument (see
5513 lookup_template_class for a more accurate description of this
5514 problem). This means that we need to handle expressions which
5515 are not valid in a C++ source, but can be created from the
5516 first call (for instance, casts to perform conversions). These
5517 hacks can go away after we fix the double coercion problem. */
5520 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5524 /* Detect immediately string literals as invalid non-type argument.
5525 This special-case is not needed for correctness (we would easily
5526 catch this later), but only to provide better diagnostic for this
5527 common user mistake. As suggested by DR 100, we do not mention
5528 linkage issues in the diagnostic as this is not the point. */
5529 /* FIXME we're making this OK. */
5530 if (TREE_CODE (expr
) == STRING_CST
)
5532 if (complain
& tf_error
)
5533 error ("%qE is not a valid template argument for type %qT "
5534 "because string literals can never be used in this context",
5539 /* Add the ADDR_EXPR now for the benefit of
5540 value_dependent_expression_p. */
5541 if (TYPE_PTROBV_P (type
)
5542 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
5544 expr
= decay_conversion (expr
, complain
);
5545 if (expr
== error_mark_node
)
5546 return error_mark_node
;
5549 /* If we are in a template, EXPR may be non-dependent, but still
5550 have a syntactic, rather than semantic, form. For example, EXPR
5551 might be a SCOPE_REF, rather than the VAR_DECL to which the
5552 SCOPE_REF refers. Preserving the qualifying scope is necessary
5553 so that access checking can be performed when the template is
5554 instantiated -- but here we need the resolved form so that we can
5555 convert the argument. */
5556 if (TYPE_REF_OBJ_P (type
)
5557 && has_value_dependent_address (expr
))
5558 /* If we want the address and it's value-dependent, don't fold. */;
5559 else if (!type_unknown_p (expr
))
5560 expr
= fold_non_dependent_expr_sfinae (expr
, complain
);
5561 if (error_operand_p (expr
))
5562 return error_mark_node
;
5563 expr_type
= TREE_TYPE (expr
);
5564 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5565 expr
= mark_lvalue_use (expr
);
5567 expr
= mark_rvalue_use (expr
);
5569 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5570 to a non-type argument of "nullptr". */
5571 if (expr
== nullptr_node
&& TYPE_PTR_OR_PTRMEM_P (type
))
5572 expr
= convert (type
, expr
);
5574 /* In C++11, integral or enumeration non-type template arguments can be
5575 arbitrary constant expressions. Pointer and pointer to
5576 member arguments can be general constant expressions that evaluate
5577 to a null value, but otherwise still need to be of a specific form. */
5578 if (cxx_dialect
>= cxx11
)
5580 if (TREE_CODE (expr
) == PTRMEM_CST
)
5581 /* A PTRMEM_CST is already constant, and a valid template
5582 argument for a parameter of pointer to member type, we just want
5583 to leave it in that form rather than lower it to a
5585 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5586 expr
= maybe_constant_value (expr
);
5587 else if (TYPE_PTR_OR_PTRMEM_P (type
))
5589 tree folded
= maybe_constant_value (expr
);
5590 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
5591 : null_member_pointer_value_p (folded
))
5596 /* HACK: Due to double coercion, we can get a
5597 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5598 which is the tree that we built on the first call (see
5599 below when coercing to reference to object or to reference to
5600 function). We just strip everything and get to the arg.
5601 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5603 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5605 tree probe_type
, probe
= expr
;
5606 if (REFERENCE_REF_P (probe
))
5607 probe
= TREE_OPERAND (probe
, 0);
5608 probe_type
= TREE_TYPE (probe
);
5609 if (TREE_CODE (probe
) == NOP_EXPR
)
5611 /* ??? Maybe we could use convert_from_reference here, but we
5612 would need to relax its constraints because the NOP_EXPR
5613 could actually change the type to something more cv-qualified,
5614 and this is not folded by convert_from_reference. */
5615 tree addr
= TREE_OPERAND (probe
, 0);
5616 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
5617 && TREE_CODE (addr
) == ADDR_EXPR
5618 && TYPE_PTR_P (TREE_TYPE (addr
))
5619 && (same_type_ignoring_top_level_qualifiers_p
5620 (TREE_TYPE (probe_type
),
5621 TREE_TYPE (TREE_TYPE (addr
)))))
5623 expr
= TREE_OPERAND (addr
, 0);
5624 expr_type
= TREE_TYPE (probe_type
);
5629 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5630 parameter is a pointer to object, through decay and
5631 qualification conversion. Let's strip everything. */
5632 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
5636 if (TREE_CODE (probe
) == ADDR_EXPR
5637 && TYPE_PTR_P (TREE_TYPE (probe
)))
5639 /* Skip the ADDR_EXPR only if it is part of the decay for
5640 an array. Otherwise, it is part of the original argument
5641 in the source code. */
5642 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe
, 0))) == ARRAY_TYPE
)
5643 probe
= TREE_OPERAND (probe
, 0);
5645 expr_type
= TREE_TYPE (expr
);
5649 /* [temp.arg.nontype]/5, bullet 1
5651 For a non-type template-parameter of integral or enumeration type,
5652 integral promotions (_conv.prom_) and integral conversions
5653 (_conv.integral_) are applied. */
5654 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5656 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
5657 t
= maybe_constant_value (t
);
5658 if (t
!= error_mark_node
)
5661 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
5662 return error_mark_node
;
5664 /* Notice that there are constant expressions like '4 % 0' which
5665 do not fold into integer constants. */
5666 if (TREE_CODE (expr
) != INTEGER_CST
)
5668 if (complain
& tf_error
)
5670 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
5671 if (processing_template_decl
5672 && !require_potential_constant_expression (expr
))
5674 expr
= cxx_constant_value (expr
);
5675 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
5676 inform (EXPR_LOC_OR_HERE (expr
),
5677 "in template argument for type %qT ", type
);
5678 if (expr
== error_mark_node
)
5680 /* else cxx_constant_value complained but gave us
5681 a real constant, so go ahead. */
5682 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
5688 /* Avoid typedef problems. */
5689 if (TREE_TYPE (expr
) != type
)
5690 expr
= fold_convert (type
, expr
);
5692 /* [temp.arg.nontype]/5, bullet 2
5694 For a non-type template-parameter of type pointer to object,
5695 qualification conversions (_conv.qual_) and the array-to-pointer
5696 conversion (_conv.array_) are applied. */
5697 else if (TYPE_PTROBV_P (type
))
5699 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5701 A template-argument for a non-type, non-template template-parameter
5702 shall be one of: [...]
5704 -- the name of a non-type template-parameter;
5705 -- the address of an object or function with external linkage, [...]
5706 expressed as "& id-expression" where the & is optional if the name
5707 refers to a function or array, or if the corresponding
5708 template-parameter is a reference.
5710 Here, we do not care about functions, as they are invalid anyway
5711 for a parameter of type pointer-to-object. */
5713 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5714 /* Non-type template parameters are OK. */
5716 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
5717 /* Null pointer values are OK in C++11. */;
5718 else if (TREE_CODE (expr
) != ADDR_EXPR
5719 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5723 error ("%qD is not a valid template argument "
5724 "because %qD is a variable, not the address of "
5729 if (POINTER_TYPE_P (expr_type
))
5731 error ("%qE is not a valid template argument for %qT "
5732 "because it is not the address of a variable",
5736 /* Other values, like integer constants, might be valid
5737 non-type arguments of some other type. */
5738 return error_mark_node
;
5744 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
5745 ? TREE_OPERAND (expr
, 0) : expr
);
5748 error ("%qE is not a valid template argument of type %qT "
5749 "because %qE is not a variable",
5753 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
5755 error ("%qE is not a valid template argument of type %qT "
5756 "because %qD does not have external linkage",
5760 else if (cxx_dialect
>= cxx11
&& decl_linkage (decl
) == lk_none
)
5762 error ("%qE is not a valid template argument of type %qT "
5763 "because %qD has no linkage",
5769 expr
= decay_conversion (expr
, complain
);
5770 if (expr
== error_mark_node
)
5771 return error_mark_node
;
5773 expr
= perform_qualification_conversions (type
, expr
);
5774 if (expr
== error_mark_node
)
5775 return error_mark_node
;
5777 /* [temp.arg.nontype]/5, bullet 3
5779 For a non-type template-parameter of type reference to object, no
5780 conversions apply. The type referred to by the reference may be more
5781 cv-qualified than the (otherwise identical) type of the
5782 template-argument. The template-parameter is bound directly to the
5783 template-argument, which must be an lvalue. */
5784 else if (TYPE_REF_OBJ_P (type
))
5786 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
5788 return error_mark_node
;
5790 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5792 error ("%qE is not a valid template argument for type %qT "
5793 "because of conflicts in cv-qualification", expr
, type
);
5797 if (!real_lvalue_p (expr
))
5799 error ("%qE is not a valid template argument for type %qT "
5800 "because it is not an lvalue", expr
, type
);
5804 /* [temp.arg.nontype]/1
5806 A template-argument for a non-type, non-template template-parameter
5807 shall be one of: [...]
5809 -- the address of an object or function with external linkage. */
5810 if (INDIRECT_REF_P (expr
)
5811 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5813 expr
= TREE_OPERAND (expr
, 0);
5816 error ("%q#D is not a valid template argument for type %qT "
5817 "because a reference variable does not have a constant "
5818 "address", expr
, type
);
5825 error ("%qE is not a valid template argument for type %qT "
5826 "because it is not an object with external linkage",
5831 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5833 error ("%qE is not a valid template argument for type %qT "
5834 "because object %qD has not external linkage",
5839 expr
= build_nop (type
, build_address (expr
));
5841 /* [temp.arg.nontype]/5, bullet 4
5843 For a non-type template-parameter of type pointer to function, only
5844 the function-to-pointer conversion (_conv.func_) is applied. If the
5845 template-argument represents a set of overloaded functions (or a
5846 pointer to such), the matching function is selected from the set
5848 else if (TYPE_PTRFN_P (type
))
5850 /* If the argument is a template-id, we might not have enough
5851 context information to decay the pointer. */
5852 if (!type_unknown_p (expr_type
))
5854 expr
= decay_conversion (expr
, complain
);
5855 if (expr
== error_mark_node
)
5856 return error_mark_node
;
5859 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
5860 /* Null pointer values are OK in C++11. */
5861 return perform_qualification_conversions (type
, expr
);
5863 expr
= convert_nontype_argument_function (type
, expr
);
5864 if (!expr
|| expr
== error_mark_node
)
5867 /* [temp.arg.nontype]/5, bullet 5
5869 For a non-type template-parameter of type reference to function, no
5870 conversions apply. If the template-argument represents a set of
5871 overloaded functions, the matching function is selected from the set
5873 else if (TYPE_REFFN_P (type
))
5875 if (TREE_CODE (expr
) == ADDR_EXPR
)
5877 error ("%qE is not a valid template argument for type %qT "
5878 "because it is a pointer", expr
, type
);
5879 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
5883 expr
= convert_nontype_argument_function (type
, expr
);
5884 if (!expr
|| expr
== error_mark_node
)
5887 expr
= build_nop (type
, build_address (expr
));
5889 /* [temp.arg.nontype]/5, bullet 6
5891 For a non-type template-parameter of type pointer to member function,
5892 no conversions apply. If the template-argument represents a set of
5893 overloaded member functions, the matching member function is selected
5894 from the set (_over.over_). */
5895 else if (TYPE_PTRMEMFUNC_P (type
))
5897 expr
= instantiate_type (type
, expr
, tf_none
);
5898 if (expr
== error_mark_node
)
5899 return error_mark_node
;
5901 /* [temp.arg.nontype] bullet 1 says the pointer to member
5902 expression must be a pointer-to-member constant. */
5903 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5904 return error_mark_node
;
5906 /* There is no way to disable standard conversions in
5907 resolve_address_of_overloaded_function (called by
5908 instantiate_type). It is possible that the call succeeded by
5909 converting &B::I to &D::I (where B is a base of D), so we need
5910 to reject this conversion here.
5912 Actually, even if there was a way to disable standard conversions,
5913 it would still be better to reject them here so that we can
5914 provide a superior diagnostic. */
5915 if (!same_type_p (TREE_TYPE (expr
), type
))
5917 error ("%qE is not a valid template argument for type %qT "
5918 "because it is of type %qT", expr
, type
,
5920 /* If we are just one standard conversion off, explain. */
5921 if (can_convert_standard (type
, TREE_TYPE (expr
), complain
))
5922 inform (input_location
,
5923 "standard conversions are not allowed in this context");
5927 /* [temp.arg.nontype]/5, bullet 7
5929 For a non-type template-parameter of type pointer to data member,
5930 qualification conversions (_conv.qual_) are applied. */
5931 else if (TYPE_PTRDATAMEM_P (type
))
5933 /* [temp.arg.nontype] bullet 1 says the pointer to member
5934 expression must be a pointer-to-member constant. */
5935 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5936 return error_mark_node
;
5938 expr
= perform_qualification_conversions (type
, expr
);
5939 if (expr
== error_mark_node
)
5942 else if (NULLPTR_TYPE_P (type
))
5944 if (expr
!= nullptr_node
)
5946 error ("%qE is not a valid template argument for type %qT "
5947 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
5952 /* A template non-type parameter must be one of the above. */
5956 /* Sanity check: did we actually convert the argument to the
5958 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5959 (type
, TREE_TYPE (expr
)));
5963 /* Subroutine of coerce_template_template_parms, which returns 1 if
5964 PARM_PARM and ARG_PARM match using the rule for the template
5965 parameters of template template parameters. Both PARM and ARG are
5966 template parameters; the rest of the arguments are the same as for
5967 coerce_template_template_parms.
5970 coerce_template_template_parm (tree parm
,
5972 tsubst_flags_t complain
,
5976 if (arg
== NULL_TREE
|| arg
== error_mark_node
5977 || parm
== NULL_TREE
|| parm
== error_mark_node
)
5980 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5983 switch (TREE_CODE (parm
))
5986 /* We encounter instantiations of templates like
5987 template <template <template <class> class> class TT>
5990 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
5991 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
5993 if (!coerce_template_template_parms
5994 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
6000 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
6001 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6002 /* Argument is a parameter pack but parameter is not. */
6007 /* The tsubst call is used to handle cases such as
6009 template <int> class C {};
6010 template <class T, template <T> class TT> class D {};
6013 i.e. the parameter list of TT depends on earlier parameters. */
6014 if (!uses_template_parms (TREE_TYPE (arg
))
6016 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
6020 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6021 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6022 /* Argument is a parameter pack but parameter is not. */
6035 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6036 template template parameters. Both PARM_PARMS and ARG_PARMS are
6037 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6040 Consider the example:
6041 template <class T> class A;
6042 template<template <class U> class TT> class B;
6044 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6045 the parameters to A, and OUTER_ARGS contains A. */
6048 coerce_template_template_parms (tree parm_parms
,
6050 tsubst_flags_t complain
,
6054 int nparms
, nargs
, i
;
6058 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
6059 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
6061 nparms
= TREE_VEC_LENGTH (parm_parms
);
6062 nargs
= TREE_VEC_LENGTH (arg_parms
);
6064 /* Determine whether we have a parameter pack at the end of the
6065 template template parameter's template parameter list. */
6066 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
6068 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
6070 if (parm
== error_mark_node
)
6073 switch (TREE_CODE (parm
))
6077 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6082 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6092 && !(variadic_p
&& nargs
>= nparms
- 1))
6095 /* Check all of the template parameters except the parameter pack at
6096 the end (if any). */
6097 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6099 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6100 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6103 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6104 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6106 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6114 /* Check each of the template parameters in the template
6115 argument against the template parameter pack at the end of
6116 the template template parameter. */
6117 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6120 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6122 for (; i
< nargs
; ++i
)
6124 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6127 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6129 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6138 /* Verifies that the deduced template arguments (in TARGS) for the
6139 template template parameters (in TPARMS) represent valid bindings,
6140 by comparing the template parameter list of each template argument
6141 to the template parameter list of its corresponding template
6142 template parameter, in accordance with DR150. This
6143 routine can only be called after all template arguments have been
6144 deduced. It will return TRUE if all of the template template
6145 parameter bindings are okay, FALSE otherwise. */
6147 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6149 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6152 /* We're dealing with template parms in this process. */
6153 ++processing_template_decl
;
6155 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6157 for (i
= 0; i
< ntparms
; ++i
)
6159 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6160 tree targ
= TREE_VEC_ELT (targs
, i
);
6162 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6164 tree packed_args
= NULL_TREE
;
6167 if (ARGUMENT_PACK_P (targ
))
6169 /* Look inside the argument pack. */
6170 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6171 len
= TREE_VEC_LENGTH (packed_args
);
6174 for (idx
= 0; idx
< len
; ++idx
)
6176 tree targ_parms
= NULL_TREE
;
6179 /* Extract the next argument from the argument
6181 targ
= TREE_VEC_ELT (packed_args
, idx
);
6183 if (PACK_EXPANSION_P (targ
))
6184 /* Look at the pattern of the pack expansion. */
6185 targ
= PACK_EXPANSION_PATTERN (targ
);
6187 /* Extract the template parameters from the template
6189 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6190 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6191 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6192 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6194 /* Verify that we can coerce the template template
6195 parameters from the template argument to the template
6196 parameter. This requires an exact match. */
6198 && !coerce_template_template_parms
6199 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6214 --processing_template_decl
;
6218 /* Since type attributes aren't mangled, we need to strip them from
6219 template type arguments. */
6222 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6225 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6227 mv
= TYPE_MAIN_VARIANT (arg
);
6228 arg
= strip_typedefs (arg
);
6229 if (TYPE_ALIGN (arg
) != TYPE_ALIGN (mv
)
6230 || TYPE_ATTRIBUTES (arg
) != TYPE_ATTRIBUTES (mv
))
6232 if (complain
& tf_warning
)
6233 warning (0, "ignoring attributes on template argument %qT", arg
);
6234 arg
= build_aligned_type (arg
, TYPE_ALIGN (mv
));
6235 arg
= cp_build_type_attribute_variant (arg
, TYPE_ATTRIBUTES (mv
));
6240 /* Convert the indicated template ARG as necessary to match the
6241 indicated template PARM. Returns the converted ARG, or
6242 error_mark_node if the conversion was unsuccessful. Error and
6243 warning messages are issued under control of COMPLAIN. This
6244 conversion is for the Ith parameter in the parameter list. ARGS is
6245 the full set of template arguments deduced so far. */
6248 convert_template_argument (tree parm
,
6251 tsubst_flags_t complain
,
6257 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
6259 if (TREE_CODE (arg
) == TREE_LIST
6260 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
6262 /* The template argument was the name of some
6263 member function. That's usually
6264 invalid, but static members are OK. In any
6265 case, grab the underlying fields/functions
6266 and issue an error later if required. */
6267 orig_arg
= TREE_VALUE (arg
);
6268 TREE_TYPE (arg
) = unknown_type_node
;
6273 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
6274 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
6275 || requires_tmpl_type
);
6277 /* When determining whether an argument pack expansion is a template,
6278 look at the pattern. */
6279 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
6280 arg
= PACK_EXPANSION_PATTERN (arg
);
6282 /* Deal with an injected-class-name used as a template template arg. */
6283 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
6285 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
6286 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6288 if (cxx_dialect
>= cxx11
)
6289 /* OK under DR 1004. */;
6290 else if (complain
& tf_warning_or_error
)
6291 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
6292 " used as template template argument", TYPE_NAME (arg
));
6293 else if (flag_pedantic_errors
)
6301 ((TREE_CODE (arg
) == TEMPLATE_DECL
6302 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
6303 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
6304 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6305 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
6308 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6309 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
6310 arg
= TYPE_STUB_DECL (arg
);
6312 is_type
= TYPE_P (arg
) || is_tmpl_type
;
6314 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
6315 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
6317 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
6319 if (complain
& tf_error
)
6320 error ("invalid use of destructor %qE as a type", orig_arg
);
6321 return error_mark_node
;
6324 permerror (input_location
,
6325 "to refer to a type member of a template parameter, "
6326 "use %<typename %E%>", orig_arg
);
6328 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
6329 TREE_OPERAND (arg
, 1),
6335 if (is_type
!= requires_type
)
6339 if (complain
& tf_error
)
6341 error ("type/value mismatch at argument %d in template "
6342 "parameter list for %qD",
6345 error (" expected a constant of type %qT, got %qT",
6347 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6348 else if (requires_tmpl_type
)
6349 error (" expected a class template, got %qE", orig_arg
);
6351 error (" expected a type, got %qE", orig_arg
);
6354 return error_mark_node
;
6356 if (is_tmpl_type
^ requires_tmpl_type
)
6358 if (in_decl
&& (complain
& tf_error
))
6360 error ("type/value mismatch at argument %d in template "
6361 "parameter list for %qD",
6364 error (" expected a type, got %qT", DECL_NAME (arg
));
6366 error (" expected a class template, got %qT", orig_arg
);
6368 return error_mark_node
;
6373 if (requires_tmpl_type
)
6375 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6377 else if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6378 /* The number of argument required is not known yet.
6379 Just accept it for now. */
6380 val
= TREE_TYPE (arg
);
6383 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6386 /* Strip alias templates that are equivalent to another
6388 arg
= get_underlying_template (arg
);
6389 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6391 if (coerce_template_template_parms (parmparm
, argparm
,
6397 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6399 if (val
!= error_mark_node
)
6401 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6402 val
= TREE_TYPE (val
);
6403 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6404 val
= make_pack_expansion (val
);
6409 if (in_decl
&& (complain
& tf_error
))
6411 error ("type/value mismatch at argument %d in "
6412 "template parameter list for %qD",
6414 error (" expected a template of type %qD, got %qT",
6418 val
= error_mark_node
;
6424 /* We only form one instance of each template specialization.
6425 Therefore, if we use a non-canonical variant (i.e., a
6426 typedef), any future messages referring to the type will use
6427 the typedef, which is confusing if those future uses do not
6428 themselves also use the typedef. */
6430 val
= canonicalize_type_argument (val
, complain
);
6434 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6436 if (invalid_nontype_parm_type_p (t
, complain
))
6437 return error_mark_node
;
6439 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6441 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6445 /* Not sure if this is reachable, but it doesn't hurt
6447 error ("type mismatch in nontype parameter pack");
6448 val
= error_mark_node
;
6451 else if (!dependent_template_arg_p (orig_arg
)
6452 && !uses_template_parms (t
))
6453 /* We used to call digest_init here. However, digest_init
6454 will report errors, which we don't want when complain
6455 is zero. More importantly, digest_init will try too
6456 hard to convert things: for example, `0' should not be
6457 converted to pointer type at this point according to
6458 the standard. Accepting this is not merely an
6459 extension, since deciding whether or not these
6460 conversions can occur is part of determining which
6461 function template to call, or whether a given explicit
6462 argument specification is valid. */
6463 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6465 val
= strip_typedefs_expr (orig_arg
);
6467 if (val
== NULL_TREE
)
6468 val
= error_mark_node
;
6469 else if (val
== error_mark_node
&& (complain
& tf_error
))
6470 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6472 if (TREE_CODE (val
) == SCOPE_REF
)
6474 /* Strip typedefs from the SCOPE_REF. */
6475 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
6476 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
6478 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6479 QUALIFIED_NAME_IS_TEMPLATE (val
));
6486 /* Coerces the remaining template arguments in INNER_ARGS (from
6487 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6488 Returns the coerced argument pack. PARM_IDX is the position of this
6489 parameter in the template parameter list. ARGS is the original
6490 template argument list. */
6492 coerce_template_parameter_pack (tree parms
,
6500 tsubst_flags_t complain
)
6502 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6503 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6506 tree packed_types
= NULL_TREE
;
6508 if (arg_idx
> nargs
)
6511 packed_args
= make_tree_vec (nargs
- arg_idx
);
6513 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
6514 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
6516 /* When the template parameter is a non-type template
6517 parameter pack whose type uses parameter packs, we need
6518 to look at each of the template arguments
6519 separately. Build a vector of the types for these
6520 non-type template parameters in PACKED_TYPES. */
6522 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
6523 packed_types
= tsubst_pack_expansion (expansion
, args
,
6526 if (packed_types
== error_mark_node
)
6527 return error_mark_node
;
6529 /* Check that we have the right number of arguments. */
6531 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
6532 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
6535 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
6536 error ("wrong number of template arguments (%d, should be %d)",
6537 nargs
, needed_parms
);
6538 return error_mark_node
;
6541 /* If we aren't able to check the actual arguments now
6542 (because they haven't been expanded yet), we can at least
6543 verify that all of the types used for the non-type
6544 template parameter pack are, in fact, valid for non-type
6545 template parameters. */
6547 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6549 int j
, len
= TREE_VEC_LENGTH (packed_types
);
6550 for (j
= 0; j
< len
; ++j
)
6552 tree t
= TREE_VEC_ELT (packed_types
, j
);
6553 if (invalid_nontype_parm_type_p (t
, complain
))
6554 return error_mark_node
;
6559 /* Convert the remaining arguments, which will be a part of the
6560 parameter pack "parm". */
6561 for (; arg_idx
< nargs
; ++arg_idx
)
6563 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6564 tree actual_parm
= TREE_VALUE (parm
);
6566 if (packed_types
&& !PACK_EXPANSION_P (arg
))
6568 /* When we have a vector of types (corresponding to the
6569 non-type template parameter pack that uses parameter
6570 packs in its type, as mention above), and the
6571 argument is not an expansion (which expands to a
6572 currently unknown number of arguments), clone the
6573 parm and give it the next type in PACKED_TYPES. */
6574 actual_parm
= copy_node (actual_parm
);
6575 TREE_TYPE (actual_parm
) =
6576 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
6579 if (arg
== error_mark_node
)
6581 if (complain
& tf_error
)
6582 error ("template argument %d is invalid", arg_idx
+ 1);
6585 arg
= convert_template_argument (actual_parm
,
6586 arg
, new_args
, complain
, parm_idx
,
6588 if (arg
== error_mark_node
)
6590 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
6593 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6594 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
6595 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
6598 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
6599 TREE_TYPE (argument_pack
)
6600 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
6601 TREE_CONSTANT (argument_pack
) = 1;
6604 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
6605 #ifdef ENABLE_CHECKING
6606 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
6607 TREE_VEC_LENGTH (packed_args
));
6609 return argument_pack
;
6612 /* Returns the number of pack expansions in the template argument vector
6616 pack_expansion_args_count (tree args
)
6621 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
6623 tree elt
= TREE_VEC_ELT (args
, i
);
6624 if (elt
&& PACK_EXPANSION_P (elt
))
6630 /* Convert all template arguments to their appropriate types, and
6631 return a vector containing the innermost resulting template
6632 arguments. If any error occurs, return error_mark_node. Error and
6633 warning messages are issued under control of COMPLAIN.
6635 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6636 for arguments not specified in ARGS. Otherwise, if
6637 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6638 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6639 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6643 coerce_template_parms (tree parms
,
6646 tsubst_flags_t complain
,
6647 bool require_all_args
,
6648 bool use_default_args
)
6650 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
6653 tree new_inner_args
;
6654 int saved_unevaluated_operand
;
6655 int saved_inhibit_evaluation_warnings
;
6657 /* When used as a boolean value, indicates whether this is a
6658 variadic template parameter list. Since it's an int, we can also
6659 subtract it from nparms to get the number of non-variadic
6662 int variadic_args_p
= 0;
6663 int post_variadic_parms
= 0;
6665 if (args
== error_mark_node
)
6666 return error_mark_node
;
6668 nparms
= TREE_VEC_LENGTH (parms
);
6670 /* Determine if there are any parameter packs. */
6671 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
6673 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
6675 ++post_variadic_parms
;
6676 if (template_parameter_pack_p (tparm
))
6680 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
6681 /* If there are no parameters that follow a parameter pack, we need to
6682 expand any argument packs so that we can deduce a parameter pack from
6683 some non-packed args followed by an argument pack, as in variadic85.C.
6684 If there are such parameters, we need to leave argument packs intact
6685 so the arguments are assigned properly. This can happen when dealing
6686 with a nested class inside a partial specialization of a class
6687 template, as in variadic92.C, or when deducing a template parameter pack
6688 from a sub-declarator, as in variadic114.C. */
6689 if (!post_variadic_parms
)
6690 inner_args
= expand_template_argument_pack (inner_args
);
6692 /* Count any pack expansion args. */
6693 variadic_args_p
= pack_expansion_args_count (inner_args
);
6695 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6696 if ((nargs
> nparms
&& !variadic_p
)
6697 || (nargs
< nparms
- variadic_p
6700 && (!use_default_args
6701 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
6702 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
6704 if (complain
& tf_error
)
6708 nparms
-= variadic_p
;
6709 error ("wrong number of template arguments "
6710 "(%d, should be %d or more)", nargs
, nparms
);
6713 error ("wrong number of template arguments "
6714 "(%d, should be %d)", nargs
, nparms
);
6717 error ("provided for %q+D", in_decl
);
6720 return error_mark_node
;
6722 /* We can't pass a pack expansion to a non-pack parameter of an alias
6723 template (DR 1430). */
6724 else if (in_decl
&& DECL_ALIAS_TEMPLATE_P (in_decl
)
6726 && nargs
- variadic_args_p
< nparms
- variadic_p
)
6728 if (complain
& tf_error
)
6730 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
6732 tree arg
= TREE_VEC_ELT (inner_args
, i
);
6733 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6735 if (PACK_EXPANSION_P (arg
)
6736 && !template_parameter_pack_p (parm
))
6738 error ("pack expansion argument for non-pack parameter "
6739 "%qD of alias template %qD", parm
, in_decl
);
6740 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
6747 return error_mark_node
;
6750 /* We need to evaluate the template arguments, even though this
6751 template-id may be nested within a "sizeof". */
6752 saved_unevaluated_operand
= cp_unevaluated_operand
;
6753 cp_unevaluated_operand
= 0;
6754 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6755 c_inhibit_evaluation_warnings
= 0;
6756 new_inner_args
= make_tree_vec (nparms
);
6757 new_args
= add_outermost_template_args (args
, new_inner_args
);
6758 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
6763 /* Get the Ith template parameter. */
6764 parm
= TREE_VEC_ELT (parms
, parm_idx
);
6766 if (parm
== error_mark_node
)
6768 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
6772 /* Calculate the next argument. */
6773 if (arg_idx
< nargs
)
6774 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6778 if (template_parameter_pack_p (TREE_VALUE (parm
))
6779 && !(arg
&& ARGUMENT_PACK_P (arg
)))
6781 /* All remaining arguments will be placed in the
6782 template parameter pack PARM. */
6783 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
6784 inner_args
, arg_idx
,
6788 /* Store this argument. */
6789 if (arg
== error_mark_node
)
6791 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
6793 /* We are done with all of the arguments. */
6800 if (PACK_EXPANSION_P (arg
))
6802 /* We don't know how many args we have yet, just
6803 use the unconverted ones for now. */
6804 new_inner_args
= inner_args
;
6808 else if (require_all_args
)
6810 /* There must be a default arg in this case. */
6811 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
6813 /* The position of the first default template argument,
6814 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6816 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6817 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
, arg_idx
);
6822 if (arg
== error_mark_node
)
6824 if (complain
& tf_error
)
6825 error ("template argument %d is invalid", arg_idx
+ 1);
6828 /* This only occurs if there was an error in the template
6829 parameter list itself (which we would already have
6830 reported) that we are trying to recover from, e.g., a class
6831 template with a parameter list such as
6832 template<typename..., typename>. */
6835 arg
= convert_template_argument (TREE_VALUE (parm
),
6836 arg
, new_args
, complain
,
6839 if (arg
== error_mark_node
)
6841 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
6843 cp_unevaluated_operand
= saved_unevaluated_operand
;
6844 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
6847 return error_mark_node
;
6849 #ifdef ENABLE_CHECKING
6850 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6851 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6852 TREE_VEC_LENGTH (new_inner_args
));
6855 return new_inner_args
;
6858 /* Like coerce_template_parms. If PARMS represents all template
6859 parameters levels, this function returns a vector of vectors
6860 representing all the resulting argument levels. Note that in this
6861 case, only the innermost arguments are coerced because the
6862 outermost ones are supposed to have been coerced already.
6864 Otherwise, if PARMS represents only (the innermost) vector of
6865 parameters, this function returns a vector containing just the
6866 innermost resulting arguments. */
6869 coerce_innermost_template_parms (tree parms
,
6872 tsubst_flags_t complain
,
6873 bool require_all_args
,
6874 bool use_default_args
)
6876 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
6877 int args_depth
= TMPL_ARGS_DEPTH (args
);
6880 if (parms_depth
> 1)
6882 coerced_args
= make_tree_vec (parms_depth
);
6886 for (level
= parms
, cur_depth
= parms_depth
;
6887 parms_depth
> 0 && level
!= NULL_TREE
;
6888 level
= TREE_CHAIN (level
), --cur_depth
)
6891 if (cur_depth
== args_depth
)
6892 l
= coerce_template_parms (TREE_VALUE (level
),
6893 args
, in_decl
, complain
,
6897 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
6899 if (l
== error_mark_node
)
6900 return error_mark_node
;
6902 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
6906 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
6907 args
, in_decl
, complain
,
6910 return coerced_args
;
6913 /* Returns 1 if template args OT and NT are equivalent. */
6916 template_args_equal (tree ot
, tree nt
)
6920 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
6923 if (TREE_CODE (nt
) == TREE_VEC
)
6924 /* For member templates */
6925 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
6926 else if (PACK_EXPANSION_P (ot
))
6927 return (PACK_EXPANSION_P (nt
)
6928 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
6929 PACK_EXPANSION_PATTERN (nt
))
6930 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
6931 PACK_EXPANSION_EXTRA_ARGS (nt
)));
6932 else if (ARGUMENT_PACK_P (ot
))
6937 if (!ARGUMENT_PACK_P (nt
))
6940 opack
= ARGUMENT_PACK_ARGS (ot
);
6941 npack
= ARGUMENT_PACK_ARGS (nt
);
6942 len
= TREE_VEC_LENGTH (opack
);
6943 if (TREE_VEC_LENGTH (npack
) != len
)
6945 for (i
= 0; i
< len
; ++i
)
6946 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
6947 TREE_VEC_ELT (npack
, i
)))
6951 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
6953 /* We get here probably because we are in the middle of substituting
6954 into the pattern of a pack expansion. In that case the
6955 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6956 interested in. So we want to use the initial pack argument for
6958 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
6959 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
6960 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
6961 return template_args_equal (ot
, nt
);
6963 else if (TYPE_P (nt
))
6964 return TYPE_P (ot
) && same_type_p (ot
, nt
);
6965 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
6968 return cp_tree_equal (ot
, nt
);
6971 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6972 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6973 NEWARG_PTR with the offending arguments if they are non-NULL. */
6976 comp_template_args_with_info (tree oldargs
, tree newargs
,
6977 tree
*oldarg_ptr
, tree
*newarg_ptr
)
6981 if (oldargs
== newargs
)
6984 if (!oldargs
|| !newargs
)
6987 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
6990 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
6992 tree nt
= TREE_VEC_ELT (newargs
, i
);
6993 tree ot
= TREE_VEC_ELT (oldargs
, i
);
6995 if (! template_args_equal (ot
, nt
))
6997 if (oldarg_ptr
!= NULL
)
6999 if (newarg_ptr
!= NULL
)
7007 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7008 of template arguments. Returns 0 otherwise. */
7011 comp_template_args (tree oldargs
, tree newargs
)
7013 return comp_template_args_with_info (oldargs
, newargs
, NULL
, NULL
);
7017 add_pending_template (tree d
)
7019 tree ti
= (TYPE_P (d
)
7020 ? CLASSTYPE_TEMPLATE_INFO (d
)
7021 : DECL_TEMPLATE_INFO (d
));
7022 struct pending_template
*pt
;
7025 if (TI_PENDING_TEMPLATE_FLAG (ti
))
7028 /* We are called both from instantiate_decl, where we've already had a
7029 tinst_level pushed, and instantiate_template, where we haven't.
7031 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
7034 push_tinst_level (d
);
7036 pt
= ggc_alloc_pending_template ();
7038 pt
->tinst
= current_tinst_level
;
7039 if (last_pending_template
)
7040 last_pending_template
->next
= pt
;
7042 pending_templates
= pt
;
7044 last_pending_template
= pt
;
7046 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
7053 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7054 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7055 documentation for TEMPLATE_ID_EXPR. */
7058 lookup_template_function (tree fns
, tree arglist
)
7062 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
7063 return error_mark_node
;
7065 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
7067 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
7069 error ("%q#D is not a function template", fns
);
7070 return error_mark_node
;
7073 if (BASELINK_P (fns
))
7075 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
7077 BASELINK_FUNCTIONS (fns
),
7082 type
= TREE_TYPE (fns
);
7083 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
7084 type
= unknown_type_node
;
7086 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
7089 /* Within the scope of a template class S<T>, the name S gets bound
7090 (in build_self_reference) to a TYPE_DECL for the class, not a
7091 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7092 or one of its enclosing classes, and that type is a template,
7093 return the associated TEMPLATE_DECL. Otherwise, the original
7096 Also handle the case when DECL is a TREE_LIST of ambiguous
7097 injected-class-names from different bases. */
7100 maybe_get_template_decl_from_type_decl (tree decl
)
7102 if (decl
== NULL_TREE
)
7105 /* DR 176: A lookup that finds an injected-class-name (10.2
7106 [class.member.lookup]) can result in an ambiguity in certain cases
7107 (for example, if it is found in more than one base class). If all of
7108 the injected-class-names that are found refer to specializations of
7109 the same class template, and if the name is followed by a
7110 template-argument-list, the reference refers to the class template
7111 itself and not a specialization thereof, and is not ambiguous. */
7112 if (TREE_CODE (decl
) == TREE_LIST
)
7114 tree t
, tmpl
= NULL_TREE
;
7115 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
7117 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
7120 else if (tmpl
!= elt
)
7123 if (tmpl
&& t
== NULL_TREE
)
7129 return (decl
!= NULL_TREE
7130 && DECL_SELF_REFERENCE_P (decl
)
7131 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
7132 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
7135 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7136 parameters, find the desired type.
7138 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7140 IN_DECL, if non-NULL, is the template declaration we are trying to
7143 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7144 the class we are looking up.
7146 Issue error and warning messages under control of COMPLAIN.
7148 If the template class is really a local class in a template
7149 function, then the FUNCTION_CONTEXT is the function in which it is
7152 ??? Note that this function is currently called *twice* for each
7153 template-id: the first time from the parser, while creating the
7154 incomplete type (finish_template_type), and the second type during the
7155 real instantiation (instantiate_template_class). This is surely something
7156 that we want to avoid. It also causes some problems with argument
7157 coercion (see convert_nontype_argument for more information on this). */
7160 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
7161 int entering_scope
, tsubst_flags_t complain
)
7163 tree templ
= NULL_TREE
, parmlist
;
7170 if (identifier_p (d1
))
7172 tree value
= innermost_non_namespace_value (d1
);
7173 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
7178 push_decl_namespace (context
);
7179 templ
= lookup_name (d1
);
7180 templ
= maybe_get_template_decl_from_type_decl (templ
);
7182 pop_decl_namespace ();
7185 context
= DECL_CONTEXT (templ
);
7187 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
7189 tree type
= TREE_TYPE (d1
);
7191 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7192 an implicit typename for the second A. Deal with it. */
7193 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
7194 type
= TREE_TYPE (type
);
7196 if (CLASSTYPE_TEMPLATE_INFO (type
))
7198 templ
= CLASSTYPE_TI_TEMPLATE (type
);
7199 d1
= DECL_NAME (templ
);
7202 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
7203 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
7205 templ
= TYPE_TI_TEMPLATE (d1
);
7206 d1
= DECL_NAME (templ
);
7208 else if (TREE_CODE (d1
) == TEMPLATE_DECL
7209 && DECL_TEMPLATE_RESULT (d1
)
7210 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
7213 d1
= DECL_NAME (templ
);
7214 context
= DECL_CONTEXT (templ
);
7216 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
7219 d1
= DECL_NAME (templ
);
7222 /* Issue an error message if we didn't find a template. */
7225 if (complain
& tf_error
)
7226 error ("%qT is not a template", d1
);
7227 return error_mark_node
;
7230 if (TREE_CODE (templ
) != TEMPLATE_DECL
7231 /* Make sure it's a user visible template, if it was named by
7233 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
7234 && !PRIMARY_TEMPLATE_P (templ
)))
7236 if (complain
& tf_error
)
7238 error ("non-template type %qT used as a template", d1
);
7240 error ("for template declaration %q+D", in_decl
);
7242 return error_mark_node
;
7245 complain
&= ~tf_user
;
7247 /* An alias that just changes the name of a template is equivalent to the
7248 other template, so if any of the arguments are pack expansions, strip
7249 the alias to avoid problems with a pack expansion passed to a non-pack
7250 alias template parameter (DR 1430). */
7251 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
7252 templ
= get_underlying_template (templ
);
7254 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
7256 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7257 template arguments */
7263 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7265 /* Consider an example where a template template parameter declared as
7267 template <class T, class U = std::allocator<T> > class TT
7269 The template parameter level of T and U are one level larger than
7270 of TT. To proper process the default argument of U, say when an
7271 instantiation `TT<int>' is seen, we need to build the full
7272 arguments containing {int} as the innermost level. Outer levels,
7273 available when not appearing as default template argument, can be
7274 obtained from the arguments of the enclosing template.
7276 Suppose that TT is later substituted with std::vector. The above
7277 instantiation is `TT<int, std::allocator<T> >' with TT at
7278 level 1, and T at level 2, while the template arguments at level 1
7279 becomes {std::vector} and the inner level 2 is {int}. */
7281 outer
= DECL_CONTEXT (templ
);
7283 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7284 else if (current_template_parms
)
7285 /* This is an argument of the current template, so we haven't set
7286 DECL_CONTEXT yet. */
7287 outer
= current_template_args ();
7290 arglist
= add_to_template_args (outer
, arglist
);
7292 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
7294 /*require_all_args=*/true,
7295 /*use_default_args=*/true);
7296 if (arglist2
== error_mark_node
7297 || (!uses_template_parms (arglist2
)
7298 && check_instantiated_args (templ
, arglist2
, complain
)))
7299 return error_mark_node
;
7301 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
7306 tree template_type
= TREE_TYPE (templ
);
7309 tree found
= NULL_TREE
;
7312 int is_dependent_type
;
7313 int use_partial_inst_tmpl
= false;
7315 if (template_type
== error_mark_node
)
7316 /* An error occurred while building the template TEMPL, and a
7317 diagnostic has most certainly been emitted for that
7318 already. Let's propagate that error. */
7319 return error_mark_node
;
7321 gen_tmpl
= most_general_template (templ
);
7322 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7323 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
7324 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7326 if (arg_depth
== 1 && parm_depth
> 1)
7328 /* We've been given an incomplete set of template arguments.
7331 template <class T> struct S1 {
7332 template <class U> struct S2 {};
7333 template <class U> struct S2<U*> {};
7336 we will be called with an ARGLIST of `U*', but the
7337 TEMPLATE will be `template <class T> template
7338 <class U> struct S1<T>::S2'. We must fill in the missing
7341 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
7343 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7346 /* Now we should have enough arguments. */
7347 gcc_assert (parm_depth
== arg_depth
);
7349 /* From here on, we're only interested in the most general
7352 /* Calculate the BOUND_ARGS. These will be the args that are
7353 actually tsubst'd into the definition to create the
7357 /* We have multiple levels of arguments to coerce, at once. */
7359 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
7361 tree bound_args
= make_tree_vec (parm_depth
);
7363 for (i
= saved_depth
,
7364 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7365 i
> 0 && t
!= NULL_TREE
;
7366 --i
, t
= TREE_CHAIN (t
))
7369 if (i
== saved_depth
)
7370 a
= coerce_template_parms (TREE_VALUE (t
),
7373 /*require_all_args=*/true,
7374 /*use_default_args=*/true);
7376 /* Outer levels should have already been coerced. */
7377 a
= TMPL_ARGS_LEVEL (arglist
, i
);
7379 /* Don't process further if one of the levels fails. */
7380 if (a
== error_mark_node
)
7382 /* Restore the ARGLIST to its full size. */
7383 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7384 return error_mark_node
;
7387 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
7389 /* We temporarily reduce the length of the ARGLIST so
7390 that coerce_template_parms will see only the arguments
7391 corresponding to the template parameters it is
7393 TREE_VEC_LENGTH (arglist
)--;
7396 /* Restore the ARGLIST to its full size. */
7397 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7399 arglist
= bound_args
;
7403 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
7404 INNERMOST_TEMPLATE_ARGS (arglist
),
7407 /*require_all_args=*/true,
7408 /*use_default_args=*/true);
7410 if (arglist
== error_mark_node
)
7411 /* We were unable to bind the arguments. */
7412 return error_mark_node
;
7414 /* In the scope of a template class, explicit references to the
7415 template class refer to the type of the template, not any
7416 instantiation of it. For example, in:
7418 template <class T> class C { void f(C<T>); }
7420 the `C<T>' is just the same as `C'. Outside of the
7421 class, however, such a reference is an instantiation. */
7423 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
7424 || currently_open_class (template_type
))
7425 /* comp_template_args is expensive, check it last. */
7426 && comp_template_args (TYPE_TI_ARGS (template_type
),
7428 return template_type
;
7430 /* If we already have this specialization, return it. */
7431 elt
.tmpl
= gen_tmpl
;
7433 hash
= hash_specialization (&elt
);
7434 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
7440 is_dependent_type
= uses_template_parms (arglist
);
7442 /* If the deduced arguments are invalid, then the binding
7444 if (!is_dependent_type
7445 && check_instantiated_args (gen_tmpl
,
7446 INNERMOST_TEMPLATE_ARGS (arglist
),
7448 return error_mark_node
;
7450 if (!is_dependent_type
7451 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
7452 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
7453 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
7455 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
7456 DECL_NAME (gen_tmpl
),
7457 /*tag_scope=*/ts_global
);
7461 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
7463 if (context
== error_mark_node
)
7464 return error_mark_node
;
7467 context
= global_namespace
;
7469 /* Create the type. */
7470 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7472 /* The user referred to a specialization of an alias
7473 template represented by GEN_TMPL.
7475 [temp.alias]/2 says:
7477 When a template-id refers to the specialization of an
7478 alias template, it is equivalent to the associated
7479 type obtained by substitution of its
7480 template-arguments for the template-parameters in the
7481 type-id of the alias template. */
7483 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
7484 /* Note that the call above (by indirectly calling
7485 register_specialization in tsubst_decl) registers the
7486 TYPE_DECL representing the specialization of the alias
7487 template. So next time someone substitutes ARGLIST for
7488 the template parms into the alias template (GEN_TMPL),
7489 she'll get that TYPE_DECL back. */
7491 if (t
== error_mark_node
)
7494 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
7496 if (!is_dependent_type
)
7498 set_current_access_from_decl (TYPE_NAME (template_type
));
7499 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
7500 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
7501 arglist
, complain
, in_decl
),
7502 SCOPED_ENUM_P (template_type
), NULL
);
7506 /* We don't want to call start_enum for this type, since
7507 the values for the enumeration constants may involve
7508 template parameters. And, no one should be interested
7509 in the enumeration constants for such a type. */
7510 t
= cxx_make_type (ENUMERAL_TYPE
);
7511 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
7513 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
7514 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
7515 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7517 else if (CLASS_TYPE_P (template_type
))
7519 t
= make_class_type (TREE_CODE (template_type
));
7520 CLASSTYPE_DECLARED_CLASS (t
)
7521 = CLASSTYPE_DECLARED_CLASS (template_type
);
7522 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7523 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7525 /* A local class. Make sure the decl gets registered properly. */
7526 if (context
== current_function_decl
)
7527 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7529 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7530 /* This instantiation is another name for the primary
7531 template type. Set the TYPE_CANONICAL field
7533 TYPE_CANONICAL (t
) = template_type
;
7534 else if (any_template_arguments_need_structural_equality_p (arglist
))
7535 /* Some of the template arguments require structural
7536 equality testing, so this template class requires
7537 structural equality testing. */
7538 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7543 /* If we called start_enum or pushtag above, this information
7544 will already be set up. */
7547 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
7549 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
7550 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
7551 DECL_SOURCE_LOCATION (type_decl
)
7552 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
7555 type_decl
= TYPE_NAME (t
);
7557 if (CLASS_TYPE_P (template_type
))
7559 TREE_PRIVATE (type_decl
)
7560 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
7561 TREE_PROTECTED (type_decl
)
7562 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
7563 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
7565 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
7566 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
7570 /* Let's consider the explicit specialization of a member
7571 of a class template specialization that is implicitely instantiated,
7576 template<class U> struct M {}; //#0
7581 struct S<int>::M<char> //#1
7585 [temp.expl.spec]/4 says this is valid.
7587 In this case, when we write:
7590 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7593 When we encounter #1, we want to store the partial instantiation
7594 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7596 For all cases other than this "explicit specialization of member of a
7597 class template", we just want to store the most general template into
7598 the CLASSTYPE_TI_TEMPLATE of M.
7600 This case of "explicit specialization of member of a class template"
7602 1/ the enclosing class is an instantiation of, and therefore not
7603 the same as, the context of the most general template, and
7604 2/ we aren't looking at the partial instantiation itself, i.e.
7605 the innermost arguments are not the same as the innermost parms of
7606 the most general template.
7608 So it's only when 1/ and 2/ happens that we want to use the partial
7609 instantiation of the member template in lieu of its most general
7612 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
7613 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
7614 /* the enclosing class must be an instantiation... */
7615 && CLASS_TYPE_P (context
)
7616 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
7618 tree partial_inst_args
;
7619 TREE_VEC_LENGTH (arglist
)--;
7620 ++processing_template_decl
;
7622 tsubst (INNERMOST_TEMPLATE_ARGS
7623 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
7624 arglist
, complain
, NULL_TREE
);
7625 --processing_template_decl
;
7626 TREE_VEC_LENGTH (arglist
)++;
7627 use_partial_inst_tmpl
=
7628 /*...and we must not be looking at the partial instantiation
7630 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
7634 if (!use_partial_inst_tmpl
)
7635 /* This case is easy; there are no member templates involved. */
7639 /* This is a full instantiation of a member template. Find
7640 the partial instantiation of which this is an instance. */
7642 /* Temporarily reduce by one the number of levels in the ARGLIST
7643 so as to avoid comparing the last set of arguments. */
7644 TREE_VEC_LENGTH (arglist
)--;
7645 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
7646 TREE_VEC_LENGTH (arglist
)++;
7647 /* FOUND is either a proper class type, or an alias
7648 template specialization. In the later case, it's a
7649 TYPE_DECL, resulting from the substituting of arguments
7650 for parameters in the TYPE_DECL of the alias template
7651 done earlier. So be careful while getting the template
7653 found
= TREE_CODE (found
) == TYPE_DECL
7654 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
7655 : CLASSTYPE_TI_TEMPLATE (found
);
7658 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
7661 slot
= htab_find_slot_with_hash (type_specializations
,
7662 &elt
, hash
, INSERT
);
7663 entry
= ggc_alloc_spec_entry ();
7667 /* Note this use of the partial instantiation so we can check it
7668 later in maybe_process_partial_specialization. */
7669 DECL_TEMPLATE_INSTANTIATIONS (templ
)
7670 = tree_cons (arglist
, t
,
7671 DECL_TEMPLATE_INSTANTIATIONS (templ
));
7673 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
7674 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7675 /* Now that the type has been registered on the instantiations
7676 list, we set up the enumerators. Because the enumeration
7677 constants may involve the enumeration type itself, we make
7678 sure to register the type first, and then create the
7679 constants. That way, doing tsubst_expr for the enumeration
7680 constants won't result in recursive calls here; we'll find
7681 the instantiation and exit above. */
7682 tsubst_enum (template_type
, t
, arglist
);
7684 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
7685 /* If the type makes use of template parameters, the
7686 code that generates debugging information will crash. */
7687 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
7689 /* Possibly limit visibility based on template args. */
7690 TREE_PUBLIC (type_decl
) = 1;
7691 determine_visibility (type_decl
);
7697 /* Wrapper for lookup_template_class_1. */
7700 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
7701 int entering_scope
, tsubst_flags_t complain
)
7704 timevar_push (TV_TEMPLATE_INST
);
7705 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
7706 entering_scope
, complain
);
7707 timevar_pop (TV_TEMPLATE_INST
);
7715 /* True when we should also visit template parameters that occur in
7716 non-deduced contexts. */
7717 bool include_nondeduced_p
;
7718 struct pointer_set_t
*visited
;
7721 /* Called from for_each_template_parm via walk_tree. */
7724 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
7727 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
7728 tree_fn_t fn
= pfd
->fn
;
7729 void *data
= pfd
->data
;
7732 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
7733 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
7734 pfd
->include_nondeduced_p
))
7735 return error_mark_node
;
7737 switch (TREE_CODE (t
))
7740 if (TYPE_PTRMEMFUNC_P (t
))
7746 if (!TYPE_TEMPLATE_INFO (t
))
7748 else if (for_each_template_parm (TYPE_TI_ARGS (t
),
7749 fn
, data
, pfd
->visited
,
7750 pfd
->include_nondeduced_p
))
7751 return error_mark_node
;
7755 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
7756 fn
, data
, pfd
->visited
,
7757 pfd
->include_nondeduced_p
)
7758 || for_each_template_parm (TYPE_MAX_VALUE (t
),
7759 fn
, data
, pfd
->visited
,
7760 pfd
->include_nondeduced_p
))
7761 return error_mark_node
;
7765 /* Since we're not going to walk subtrees, we have to do this
7767 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
7768 pfd
->visited
, pfd
->include_nondeduced_p
))
7769 return error_mark_node
;
7773 /* Check the return type. */
7774 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7775 pfd
->include_nondeduced_p
))
7776 return error_mark_node
;
7778 /* Check the parameter types. Since default arguments are not
7779 instantiated until they are needed, the TYPE_ARG_TYPES may
7780 contain expressions that involve template parameters. But,
7781 no-one should be looking at them yet. And, once they're
7782 instantiated, they don't contain template parameters, so
7783 there's no point in looking at them then, either. */
7787 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
7788 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
7789 pfd
->visited
, pfd
->include_nondeduced_p
))
7790 return error_mark_node
;
7792 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7793 want walk_tree walking into them itself. */
7799 case UNDERLYING_TYPE
:
7800 if (pfd
->include_nondeduced_p
7801 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
7803 pfd
->include_nondeduced_p
))
7804 return error_mark_node
;
7809 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
7810 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
7811 pfd
->visited
, pfd
->include_nondeduced_p
))
7812 return error_mark_node
;
7817 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
7818 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
7819 pfd
->visited
, pfd
->include_nondeduced_p
))
7820 return error_mark_node
;
7821 if (DECL_CONTEXT (t
)
7822 && pfd
->include_nondeduced_p
7823 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
7824 pfd
->visited
, pfd
->include_nondeduced_p
))
7825 return error_mark_node
;
7828 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7829 /* Record template parameters such as `T' inside `TT<T>'. */
7830 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
7831 pfd
->include_nondeduced_p
))
7832 return error_mark_node
;
7835 case TEMPLATE_TEMPLATE_PARM
:
7836 case TEMPLATE_TYPE_PARM
:
7837 case TEMPLATE_PARM_INDEX
:
7838 if (fn
&& (*fn
)(t
, data
))
7839 return error_mark_node
;
7841 return error_mark_node
;
7845 /* A template template parameter is encountered. */
7846 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
7847 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7848 pfd
->include_nondeduced_p
))
7849 return error_mark_node
;
7851 /* Already substituted template template parameter */
7857 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
7859 pfd
->include_nondeduced_p
))
7860 return error_mark_node
;
7864 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
7865 && pfd
->include_nondeduced_p
7866 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7867 (TREE_TYPE (t
)), fn
, data
,
7868 pfd
->visited
, pfd
->include_nondeduced_p
))
7869 return error_mark_node
;
7874 /* If there's no type, then this thing must be some expression
7875 involving template parameters. */
7876 if (!fn
&& !TREE_TYPE (t
))
7877 return error_mark_node
;
7882 case IMPLICIT_CONV_EXPR
:
7883 case REINTERPRET_CAST_EXPR
:
7884 case CONST_CAST_EXPR
:
7885 case STATIC_CAST_EXPR
:
7886 case DYNAMIC_CAST_EXPR
:
7890 case PSEUDO_DTOR_EXPR
:
7892 return error_mark_node
;
7899 /* We didn't find any template parameters we liked. */
7903 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7904 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7905 call FN with the parameter and the DATA.
7906 If FN returns nonzero, the iteration is terminated, and
7907 for_each_template_parm returns 1. Otherwise, the iteration
7908 continues. If FN never returns a nonzero value, the value
7909 returned by for_each_template_parm is 0. If FN is NULL, it is
7910 considered to be the function which always returns 1.
7912 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7913 parameters that occur in non-deduced contexts. When false, only
7914 visits those template parameters that can be deduced. */
7917 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
7918 struct pointer_set_t
*visited
,
7919 bool include_nondeduced_p
)
7921 struct pair_fn_data pfd
;
7927 pfd
.include_nondeduced_p
= include_nondeduced_p
;
7929 /* Walk the tree. (Conceptually, we would like to walk without
7930 duplicates, but for_each_template_parm_r recursively calls
7931 for_each_template_parm, so we would need to reorganize a fair
7932 bit to use walk_tree_without_duplicates, so we keep our own
7935 pfd
.visited
= visited
;
7937 pfd
.visited
= pointer_set_create ();
7938 result
= cp_walk_tree (&t
,
7939 for_each_template_parm_r
,
7941 pfd
.visited
) != NULL_TREE
;
7946 pointer_set_destroy (pfd
.visited
);
7953 /* Returns true if T depends on any template parameter. */
7956 uses_template_parms (tree t
)
7959 int saved_processing_template_decl
;
7961 saved_processing_template_decl
= processing_template_decl
;
7962 if (!saved_processing_template_decl
)
7963 processing_template_decl
= 1;
7965 dependent_p
= dependent_type_p (t
);
7966 else if (TREE_CODE (t
) == TREE_VEC
)
7967 dependent_p
= any_dependent_template_arguments_p (t
);
7968 else if (TREE_CODE (t
) == TREE_LIST
)
7969 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
7970 || uses_template_parms (TREE_CHAIN (t
)));
7971 else if (TREE_CODE (t
) == TYPE_DECL
)
7972 dependent_p
= dependent_type_p (TREE_TYPE (t
));
7975 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
7976 || TREE_CODE (t
) == OVERLOAD
7979 || TREE_CODE (t
) == TRAIT_EXPR
7980 || TREE_CODE (t
) == CONSTRUCTOR
7981 || CONSTANT_CLASS_P (t
))
7982 dependent_p
= (type_dependent_expression_p (t
)
7983 || value_dependent_expression_p (t
));
7986 gcc_assert (t
== error_mark_node
);
7987 dependent_p
= false;
7990 processing_template_decl
= saved_processing_template_decl
;
7995 /* Returns true iff current_function_decl is an incompletely instantiated
7996 template. Useful instead of processing_template_decl because the latter
7997 is set to 0 during fold_non_dependent_expr. */
8000 in_template_function (void)
8002 tree fn
= current_function_decl
;
8004 ++processing_template_decl
;
8005 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
8006 && DECL_TEMPLATE_INFO (fn
)
8007 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
8008 --processing_template_decl
;
8012 /* Returns true if T depends on any template parameter with level LEVEL. */
8015 uses_template_parms_level (tree t
, int level
)
8017 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
8018 /*include_nondeduced_p=*/true);
8021 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8022 ill-formed translation unit, i.e. a variable or function that isn't
8023 usable in a constant expression. */
8026 neglectable_inst_p (tree d
)
8029 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
8030 : decl_maybe_constant_var_p (d
)));
8033 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8034 neglectable and instantiated from within an erroneous instantiation. */
8037 limit_bad_template_recursion (tree decl
)
8039 struct tinst_level
*lev
= current_tinst_level
;
8040 int errs
= errorcount
+ sorrycount
;
8041 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
8044 for (; lev
; lev
= lev
->next
)
8045 if (neglectable_inst_p (lev
->decl
))
8048 return (lev
&& errs
> lev
->errors
);
8051 static int tinst_depth
;
8052 extern int max_tinst_depth
;
8055 static GTY(()) struct tinst_level
*last_error_tinst_level
;
8057 /* We're starting to instantiate D; record the template instantiation context
8058 for diagnostics and to restore it later. */
8061 push_tinst_level (tree d
)
8063 struct tinst_level
*new_level
;
8065 if (tinst_depth
>= max_tinst_depth
)
8067 last_error_tinst_level
= current_tinst_level
;
8068 if (TREE_CODE (d
) == TREE_LIST
)
8069 error ("template instantiation depth exceeds maximum of %d (use "
8070 "-ftemplate-depth= to increase the maximum) substituting %qS",
8071 max_tinst_depth
, d
);
8073 error ("template instantiation depth exceeds maximum of %d (use "
8074 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8075 max_tinst_depth
, d
);
8077 print_instantiation_context ();
8082 /* If the current instantiation caused problems, don't let it instantiate
8083 anything else. Do allow deduction substitution and decls usable in
8084 constant expressions. */
8085 if (limit_bad_template_recursion (d
))
8088 new_level
= ggc_alloc_tinst_level ();
8089 new_level
->decl
= d
;
8090 new_level
->locus
= input_location
;
8091 new_level
->errors
= errorcount
+sorrycount
;
8092 new_level
->in_system_header_p
= in_system_header
;
8093 new_level
->next
= current_tinst_level
;
8094 current_tinst_level
= new_level
;
8097 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
8098 depth_reached
= tinst_depth
;
8103 /* We're done instantiating this template; return to the instantiation
8107 pop_tinst_level (void)
8109 /* Restore the filename and line number stashed away when we started
8110 this instantiation. */
8111 input_location
= current_tinst_level
->locus
;
8112 current_tinst_level
= current_tinst_level
->next
;
8116 /* We're instantiating a deferred template; restore the template
8117 instantiation context in which the instantiation was requested, which
8118 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8121 reopen_tinst_level (struct tinst_level
*level
)
8123 struct tinst_level
*t
;
8126 for (t
= level
; t
; t
= t
->next
)
8129 current_tinst_level
= level
;
8131 if (current_tinst_level
)
8132 current_tinst_level
->errors
= errorcount
+sorrycount
;
8136 /* Returns the TINST_LEVEL which gives the original instantiation
8139 struct tinst_level
*
8140 outermost_tinst_level (void)
8142 struct tinst_level
*level
= current_tinst_level
;
8145 level
= level
->next
;
8149 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8150 vector of template arguments, as for tsubst.
8152 Returns an appropriate tsubst'd friend declaration. */
8155 tsubst_friend_function (tree decl
, tree args
)
8159 if (TREE_CODE (decl
) == FUNCTION_DECL
8160 && DECL_TEMPLATE_INSTANTIATION (decl
)
8161 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
8162 /* This was a friend declared with an explicit template
8163 argument list, e.g.:
8167 to indicate that f was a template instantiation, not a new
8168 function declaration. Now, we have to figure out what
8169 instantiation of what template. */
8171 tree template_id
, arglist
, fns
;
8174 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
8176 /* Friend functions are looked up in the containing namespace scope.
8177 We must enter that scope, to avoid finding member functions of the
8178 current class with same name. */
8179 push_nested_namespace (ns
);
8180 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
8181 tf_warning_or_error
, NULL_TREE
,
8182 /*integral_constant_expression_p=*/false);
8183 pop_nested_namespace (ns
);
8184 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
8185 tf_warning_or_error
, NULL_TREE
);
8186 template_id
= lookup_template_function (fns
, arglist
);
8188 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8189 tmpl
= determine_specialization (template_id
, new_friend
,
8191 /*need_member_template=*/0,
8192 TREE_VEC_LENGTH (args
),
8194 return instantiate_template (tmpl
, new_args
, tf_error
);
8197 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8199 /* The NEW_FRIEND will look like an instantiation, to the
8200 compiler, but is not an instantiation from the point of view of
8201 the language. For example, we might have had:
8203 template <class T> struct S {
8204 template <class U> friend void f(T, U);
8207 Then, in S<int>, template <class U> void f(int, U) is not an
8208 instantiation of anything. */
8209 if (new_friend
== error_mark_node
)
8210 return error_mark_node
;
8212 DECL_USE_TEMPLATE (new_friend
) = 0;
8213 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8215 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
8216 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
8217 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
8220 /* The mangled name for the NEW_FRIEND is incorrect. The function
8221 is not a template instantiation and should not be mangled like
8222 one. Therefore, we forget the mangling here; we'll recompute it
8223 later if we need it. */
8224 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
8226 SET_DECL_RTL (new_friend
, NULL
);
8227 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
8230 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
8233 tree new_friend_template_info
;
8234 tree new_friend_result_template_info
;
8236 int new_friend_is_defn
;
8238 /* We must save some information from NEW_FRIEND before calling
8239 duplicate decls since that function will free NEW_FRIEND if
8241 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
8242 new_friend_is_defn
=
8243 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8244 (template_for_substitution (new_friend
)))
8246 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
8248 /* This declaration is a `primary' template. */
8249 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
8251 new_friend_result_template_info
8252 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
8255 new_friend_result_template_info
= NULL_TREE
;
8257 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8258 if (new_friend_is_defn
)
8259 DECL_INITIAL (new_friend
) = error_mark_node
;
8261 /* Inside pushdecl_namespace_level, we will push into the
8262 current namespace. However, the friend function should go
8263 into the namespace of the template. */
8264 ns
= decl_namespace_context (new_friend
);
8265 push_nested_namespace (ns
);
8266 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
8267 pop_nested_namespace (ns
);
8269 if (old_decl
== error_mark_node
)
8270 return error_mark_node
;
8272 if (old_decl
!= new_friend
)
8274 /* This new friend declaration matched an existing
8275 declaration. For example, given:
8277 template <class T> void f(T);
8278 template <class U> class C {
8279 template <class T> friend void f(T) {}
8282 the friend declaration actually provides the definition
8283 of `f', once C has been instantiated for some type. So,
8284 old_decl will be the out-of-class template declaration,
8285 while new_friend is the in-class definition.
8287 But, if `f' was called before this point, the
8288 instantiation of `f' will have DECL_TI_ARGS corresponding
8289 to `T' but not to `U', references to which might appear
8290 in the definition of `f'. Previously, the most general
8291 template for an instantiation of `f' was the out-of-class
8292 version; now it is the in-class version. Therefore, we
8293 run through all specialization of `f', adding to their
8294 DECL_TI_ARGS appropriately. In particular, they need a
8295 new set of outer arguments, corresponding to the
8296 arguments for this class instantiation.
8298 The same situation can arise with something like this:
8301 template <class T> class C {
8305 when `C<int>' is instantiated. Now, `f(int)' is defined
8308 if (!new_friend_is_defn
)
8309 /* On the other hand, if the in-class declaration does
8310 *not* provide a definition, then we don't want to alter
8311 existing definitions. We can just leave everything
8316 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
8317 tree new_args
= TI_ARGS (new_friend_template_info
);
8319 /* Overwrite whatever template info was there before, if
8320 any, with the new template information pertaining to
8322 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
8324 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
8326 /* We should have called reregister_specialization in
8328 gcc_assert (retrieve_specialization (new_template
,
8332 /* Instantiate it if the global has already been used. */
8333 if (DECL_ODR_USED (old_decl
))
8334 instantiate_decl (old_decl
, /*defer_ok=*/true,
8335 /*expl_inst_class_mem_p=*/false);
8341 /* Indicate that the old function template is a partial
8343 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
8344 = new_friend_result_template_info
;
8346 gcc_assert (new_template
8347 == most_general_template (new_template
));
8348 gcc_assert (new_template
!= old_decl
);
8350 /* Reassign any specializations already in the hash table
8351 to the new more general template, and add the
8352 additional template args. */
8353 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
8357 tree spec
= TREE_VALUE (t
);
8360 elt
.tmpl
= old_decl
;
8361 elt
.args
= DECL_TI_ARGS (spec
);
8362 elt
.spec
= NULL_TREE
;
8364 htab_remove_elt (decl_specializations
, &elt
);
8367 = add_outermost_template_args (new_args
,
8368 DECL_TI_ARGS (spec
));
8370 register_specialization
8371 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
8374 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
8378 /* The information from NEW_FRIEND has been merged into OLD_DECL
8379 by duplicate_decls. */
8380 new_friend
= old_decl
;
8385 tree context
= DECL_CONTEXT (new_friend
);
8389 template <class T> class C {
8390 template <class U> friend void C1<U>::f (); // case 1
8391 friend void C2<T>::f (); // case 2
8393 we only need to make sure CONTEXT is a complete type for
8394 case 2. To distinguish between the two cases, we note that
8395 CONTEXT of case 1 remains dependent type after tsubst while
8396 this isn't true for case 2. */
8397 ++processing_template_decl
;
8398 dependent_p
= dependent_type_p (context
);
8399 --processing_template_decl
;
8402 && !complete_type_or_else (context
, NULL_TREE
))
8403 return error_mark_node
;
8405 if (COMPLETE_TYPE_P (context
))
8407 /* Check to see that the declaration is really present, and,
8408 possibly obtain an improved declaration. */
8409 tree fn
= check_classfn (context
,
8410 new_friend
, NULL_TREE
);
8420 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8421 template arguments, as for tsubst.
8423 Returns an appropriate tsubst'd friend type or error_mark_node on
8427 tsubst_friend_class (tree friend_tmpl
, tree args
)
8433 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
8435 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
8436 return TREE_TYPE (t
);
8439 context
= CP_DECL_CONTEXT (friend_tmpl
);
8441 if (context
!= global_namespace
)
8443 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8444 push_nested_namespace (context
);
8446 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
8449 /* Look for a class template declaration. We look for hidden names
8450 because two friend declarations of the same template are the
8451 same. For example, in:
8454 template <typename> friend class F;
8456 template <typename> struct B {
8457 template <typename> friend class F;
8460 both F templates are the same. */
8461 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
8462 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
8464 /* But, if we don't find one, it might be because we're in a
8465 situation like this:
8473 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8474 for `S<int>', not the TEMPLATE_DECL. */
8475 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
8477 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
8478 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
8481 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
8483 /* The friend template has already been declared. Just
8484 check to see that the declarations match, and install any new
8485 default parameters. We must tsubst the default parameters,
8486 of course. We only need the innermost template parameters
8487 because that is all that redeclare_class_template will look
8489 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
8490 > TMPL_ARGS_DEPTH (args
))
8493 location_t saved_input_location
;
8494 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
8495 args
, tf_warning_or_error
);
8497 saved_input_location
= input_location
;
8498 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
8499 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
8500 input_location
= saved_input_location
;
8504 friend_type
= TREE_TYPE (tmpl
);
8508 /* The friend template has not already been declared. In this
8509 case, the instantiation of the template class will cause the
8510 injection of this template into the global scope. */
8511 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
8512 if (tmpl
== error_mark_node
)
8513 return error_mark_node
;
8515 /* The new TMPL is not an instantiation of anything, so we
8516 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8517 the new type because that is supposed to be the corresponding
8518 template decl, i.e., TMPL. */
8519 DECL_USE_TEMPLATE (tmpl
) = 0;
8520 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
8521 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
8522 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
8523 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
8525 /* Inject this template into the global scope. */
8526 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
8529 if (context
!= global_namespace
)
8531 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8532 pop_nested_namespace (context
);
8534 pop_nested_class ();
8540 /* Returns zero if TYPE cannot be completed later due to circularity.
8541 Otherwise returns one. */
8544 can_complete_type_without_circularity (tree type
)
8546 if (type
== NULL_TREE
|| type
== error_mark_node
)
8548 else if (COMPLETE_TYPE_P (type
))
8550 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
8551 return can_complete_type_without_circularity (TREE_TYPE (type
));
8552 else if (CLASS_TYPE_P (type
)
8553 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
8559 static tree
tsubst_omp_clauses (tree
, bool, tree
, tsubst_flags_t
, tree
);
8561 /* Apply any attributes which had to be deferred until instantiation
8562 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8563 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8566 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
8567 tree args
, tsubst_flags_t complain
, tree in_decl
)
8569 tree last_dep
= NULL_TREE
;
8573 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
8574 if (ATTR_IS_DEPENDENT (t
))
8577 attributes
= copy_list (attributes
);
8581 if (DECL_P (*decl_p
))
8583 if (TREE_TYPE (*decl_p
) == error_mark_node
)
8585 p
= &DECL_ATTRIBUTES (*decl_p
);
8588 p
= &TYPE_ATTRIBUTES (*decl_p
);
8592 tree late_attrs
= NULL_TREE
;
8593 tree
*q
= &late_attrs
;
8595 for (*p
= attributes
; *p
; )
8598 if (ATTR_IS_DEPENDENT (t
))
8600 *p
= TREE_CHAIN (t
);
8601 TREE_CHAIN (t
) = NULL_TREE
;
8603 && is_attribute_p ("omp declare simd",
8604 get_attribute_name (t
))
8607 tree clauses
= TREE_VALUE (TREE_VALUE (t
));
8608 clauses
= tsubst_omp_clauses (clauses
, true, args
,
8610 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
8611 clauses
= finish_omp_clauses (clauses
);
8612 tree parms
= DECL_ARGUMENTS (*decl_p
);
8614 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
8616 TREE_VALUE (TREE_VALUE (t
)) = clauses
;
8618 TREE_VALUE (t
) = NULL_TREE
;
8620 /* If the first attribute argument is an identifier, don't
8621 pass it through tsubst. Attributes like mode, format,
8622 cleanup and several target specific attributes expect it
8624 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
8627 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
8629 /*integral_constant_expression_p=*/false);
8630 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
8632 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
8637 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
8638 /*integral_constant_expression_p=*/false);
8640 q
= &TREE_CHAIN (t
);
8643 p
= &TREE_CHAIN (t
);
8646 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
8650 /* Perform (or defer) access check for typedefs that were referenced
8651 from within the template TMPL code.
8652 This is a subroutine of instantiate_decl and instantiate_class_template.
8653 TMPL is the template to consider and TARGS is the list of arguments of
8657 perform_typedefs_access_check (tree tmpl
, tree targs
)
8659 location_t saved_location
;
8661 qualified_typedef_usage_t
*iter
;
8664 || (!CLASS_TYPE_P (tmpl
)
8665 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
8668 saved_location
= input_location
;
8669 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
8671 tree type_decl
= iter
->typedef_decl
;
8672 tree type_scope
= iter
->context
;
8674 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
8677 if (uses_template_parms (type_decl
))
8678 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
8679 if (uses_template_parms (type_scope
))
8680 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
8682 /* Make access check error messages point to the location
8683 of the use of the typedef. */
8684 input_location
= iter
->locus
;
8685 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
8686 type_decl
, type_decl
,
8687 tf_warning_or_error
);
8689 input_location
= saved_location
;
8693 instantiate_class_template_1 (tree type
)
8695 tree templ
, args
, pattern
, t
, member
;
8699 unsigned int saved_maximum_field_alignment
;
8702 if (type
== error_mark_node
)
8703 return error_mark_node
;
8705 if (COMPLETE_OR_OPEN_TYPE_P (type
)
8706 || uses_template_parms (type
))
8709 /* Figure out which template is being instantiated. */
8710 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
8711 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
8713 /* Determine what specialization of the original template to
8715 t
= most_specialized_class (type
, templ
, tf_warning_or_error
);
8716 if (t
== error_mark_node
)
8718 TYPE_BEING_DEFINED (type
) = 1;
8719 return error_mark_node
;
8723 /* This TYPE is actually an instantiation of a partial
8724 specialization. We replace the innermost set of ARGS with
8725 the arguments appropriate for substitution. For example,
8728 template <class T> struct S {};
8729 template <class T> struct S<T*> {};
8731 and supposing that we are instantiating S<int*>, ARGS will
8732 presently be {int*} -- but we need {int}. */
8733 pattern
= TREE_TYPE (t
);
8734 args
= TREE_PURPOSE (t
);
8738 pattern
= TREE_TYPE (templ
);
8739 args
= CLASSTYPE_TI_ARGS (type
);
8742 /* If the template we're instantiating is incomplete, then clearly
8743 there's nothing we can do. */
8744 if (!COMPLETE_TYPE_P (pattern
))
8747 /* If we've recursively instantiated too many templates, stop. */
8748 if (! push_tinst_level (type
))
8751 /* Now we're really doing the instantiation. Mark the type as in
8752 the process of being defined. */
8753 TYPE_BEING_DEFINED (type
) = 1;
8755 /* We may be in the middle of deferred access check. Disable
8757 push_deferring_access_checks (dk_no_deferred
);
8759 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
8761 push_to_top_level ();
8762 /* Use #pragma pack from the template context. */
8763 saved_maximum_field_alignment
= maximum_field_alignment
;
8764 maximum_field_alignment
= TYPE_PRECISION (pattern
);
8766 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
8768 /* Set the input location to the most specialized template definition.
8769 This is needed if tsubsting causes an error. */
8770 typedecl
= TYPE_MAIN_DECL (pattern
);
8771 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
8772 DECL_SOURCE_LOCATION (typedecl
);
8774 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
8775 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
8776 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
8777 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
8778 if (ANON_AGGR_TYPE_P (pattern
))
8779 SET_ANON_AGGR_TYPE_P (type
);
8780 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
8782 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
8783 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
8784 /* Adjust visibility for template arguments. */
8785 determine_visibility (TYPE_MAIN_DECL (type
));
8787 if (CLASS_TYPE_P (type
))
8788 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
8790 pbinfo
= TYPE_BINFO (pattern
);
8792 /* We should never instantiate a nested class before its enclosing
8793 class; we need to look up the nested class by name before we can
8794 instantiate it, and that lookup should instantiate the enclosing
8796 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
8797 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
8799 base_list
= NULL_TREE
;
8800 if (BINFO_N_BASE_BINFOS (pbinfo
))
8806 /* We must enter the scope containing the type, as that is where
8807 the accessibility of types named in dependent bases are
8809 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
8811 /* Substitute into each of the bases to determine the actual
8813 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
8816 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
8817 tree expanded_bases
= NULL_TREE
;
8820 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
8823 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
8824 args
, tf_error
, NULL_TREE
);
8825 if (expanded_bases
== error_mark_node
)
8828 len
= TREE_VEC_LENGTH (expanded_bases
);
8831 for (idx
= 0; idx
< len
; idx
++)
8834 /* Extract the already-expanded base class. */
8835 base
= TREE_VEC_ELT (expanded_bases
, idx
);
8837 /* Substitute to figure out the base class. */
8838 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
8841 if (base
== error_mark_node
)
8844 base_list
= tree_cons (access
, base
, base_list
);
8845 if (BINFO_VIRTUAL_P (pbase_binfo
))
8846 TREE_TYPE (base_list
) = integer_type_node
;
8850 /* The list is now in reverse order; correct that. */
8851 base_list
= nreverse (base_list
);
8854 pop_scope (pushed_scope
);
8856 /* Now call xref_basetypes to set up all the base-class
8858 xref_basetypes (type
, base_list
);
8860 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
8861 (int) ATTR_FLAG_TYPE_IN_PLACE
,
8862 args
, tf_error
, NULL_TREE
);
8863 fixup_attribute_variants (type
);
8865 /* Now that our base classes are set up, enter the scope of the
8866 class, so that name lookups into base classes, etc. will work
8867 correctly. This is precisely analogous to what we do in
8868 begin_class_definition when defining an ordinary non-template
8869 class, except we also need to push the enclosing classes. */
8870 push_nested_class (type
);
8872 /* Now members are processed in the order of declaration. */
8873 for (member
= CLASSTYPE_DECL_LIST (pattern
);
8874 member
; member
= TREE_CHAIN (member
))
8876 tree t
= TREE_VALUE (member
);
8878 if (TREE_PURPOSE (member
))
8882 /* Build new CLASSTYPE_NESTED_UTDS. */
8885 bool class_template_p
;
8887 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
8888 && TYPE_LANG_SPECIFIC (t
)
8889 && CLASSTYPE_IS_TEMPLATE (t
));
8890 /* If the member is a class template, then -- even after
8891 substitution -- there may be dependent types in the
8892 template argument list for the class. We increment
8893 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8894 that function will assume that no types are dependent
8895 when outside of a template. */
8896 if (class_template_p
)
8897 ++processing_template_decl
;
8898 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8899 if (class_template_p
)
8900 --processing_template_decl
;
8901 if (newtag
== error_mark_node
)
8904 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
8906 tree name
= TYPE_IDENTIFIER (t
);
8908 if (class_template_p
)
8909 /* Unfortunately, lookup_template_class sets
8910 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8911 instantiation (i.e., for the type of a member
8912 template class nested within a template class.)
8913 This behavior is required for
8914 maybe_process_partial_specialization to work
8915 correctly, but is not accurate in this case;
8916 the TAG is not an instantiation of anything.
8917 (The corresponding TEMPLATE_DECL is an
8918 instantiation, but the TYPE is not.) */
8919 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
8921 /* Now, we call pushtag to put this NEWTAG into the scope of
8922 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8923 pushtag calling push_template_decl. We don't have to do
8924 this for enums because it will already have been done in
8927 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
8928 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
8931 else if (DECL_DECLARES_FUNCTION_P (t
))
8933 /* Build new TYPE_METHODS. */
8936 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8937 ++processing_template_decl
;
8938 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8939 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8940 --processing_template_decl
;
8941 set_current_access_from_decl (r
);
8942 finish_member_declaration (r
);
8943 /* Instantiate members marked with attribute used. */
8944 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8946 if (TREE_CODE (r
) == FUNCTION_DECL
8947 && DECL_OMP_DECLARE_REDUCTION_P (r
))
8948 cp_check_omp_declare_reduction (r
);
8952 /* Build new TYPE_FIELDS. */
8953 if (TREE_CODE (t
) == STATIC_ASSERT
)
8957 ++c_inhibit_evaluation_warnings
;
8959 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
8960 tf_warning_or_error
, NULL_TREE
,
8961 /*integral_constant_expression_p=*/true);
8962 --c_inhibit_evaluation_warnings
;
8964 finish_static_assert (condition
,
8965 STATIC_ASSERT_MESSAGE (t
),
8966 STATIC_ASSERT_SOURCE_LOCATION (t
),
8969 else if (TREE_CODE (t
) != CONST_DECL
)
8972 tree vec
= NULL_TREE
;
8975 /* The file and line for this declaration, to
8976 assist in error message reporting. Since we
8977 called push_tinst_level above, we don't need to
8979 input_location
= DECL_SOURCE_LOCATION (t
);
8981 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8982 ++processing_template_decl
;
8983 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
8984 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8985 --processing_template_decl
;
8987 if (TREE_CODE (r
) == TREE_VEC
)
8989 /* A capture pack became multiple fields. */
8991 len
= TREE_VEC_LENGTH (vec
);
8994 for (int i
= 0; i
< len
; ++i
)
8997 r
= TREE_VEC_ELT (vec
, i
);
9002 [t]he initialization (and any associated
9003 side-effects) of a static data member does
9004 not occur unless the static data member is
9005 itself used in a way that requires the
9006 definition of the static data member to
9009 Therefore, we do not substitute into the
9010 initialized for the static data member here. */
9011 finish_static_data_member_decl
9014 /*init_const_expr_p=*/false,
9015 /*asmspec_tree=*/NULL_TREE
,
9017 /* Instantiate members marked with attribute used. */
9018 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
9021 else if (TREE_CODE (r
) == FIELD_DECL
)
9023 /* Determine whether R has a valid type and can be
9024 completed later. If R is invalid, then its type
9025 is replaced by error_mark_node. */
9026 tree rtype
= TREE_TYPE (r
);
9027 if (can_complete_type_without_circularity (rtype
))
9028 complete_type (rtype
);
9030 if (!COMPLETE_TYPE_P (rtype
))
9032 cxx_incomplete_type_error (r
, rtype
);
9033 TREE_TYPE (r
) = error_mark_node
;
9037 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9038 such a thing will already have been added to the field
9039 list by tsubst_enum in finish_member_declaration in the
9040 CLASSTYPE_NESTED_UTDS case above. */
9041 if (!(TREE_CODE (r
) == TYPE_DECL
9042 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
9043 && DECL_ARTIFICIAL (r
)))
9045 set_current_access_from_decl (r
);
9046 finish_member_declaration (r
);
9054 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
9055 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9057 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9059 tree friend_type
= t
;
9060 bool adjust_processing_template_decl
= false;
9062 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9064 /* template <class T> friend class C; */
9065 friend_type
= tsubst_friend_class (friend_type
, args
);
9066 adjust_processing_template_decl
= true;
9068 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
9070 /* template <class T> friend class C::D; */
9071 friend_type
= tsubst (friend_type
, args
,
9072 tf_warning_or_error
, NULL_TREE
);
9073 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9074 friend_type
= TREE_TYPE (friend_type
);
9075 adjust_processing_template_decl
= true;
9077 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
9078 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
9080 /* This could be either
9084 when dependent_type_p is false or
9086 template <class U> friend class T::C;
9089 friend_type
= tsubst (friend_type
, args
,
9090 tf_warning_or_error
, NULL_TREE
);
9091 /* Bump processing_template_decl for correct
9092 dependent_type_p calculation. */
9093 ++processing_template_decl
;
9094 if (dependent_type_p (friend_type
))
9095 adjust_processing_template_decl
= true;
9096 --processing_template_decl
;
9098 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
9099 && hidden_name_p (TYPE_NAME (friend_type
)))
9103 where C hasn't been declared yet. Let's lookup name
9104 from namespace scope directly, bypassing any name that
9105 come from dependent base class. */
9106 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
9108 /* The call to xref_tag_from_type does injection for friend
9110 push_nested_namespace (ns
);
9112 xref_tag_from_type (friend_type
, NULL_TREE
,
9113 /*tag_scope=*/ts_current
);
9114 pop_nested_namespace (ns
);
9116 else if (uses_template_parms (friend_type
))
9117 /* friend class C<T>; */
9118 friend_type
= tsubst (friend_type
, args
,
9119 tf_warning_or_error
, NULL_TREE
);
9124 where C is already declared or
9126 friend class C<int>;
9128 We don't have to do anything in these cases. */
9130 if (adjust_processing_template_decl
)
9131 /* Trick make_friend_class into realizing that the friend
9132 we're adding is a template, not an ordinary class. It's
9133 important that we use make_friend_class since it will
9134 perform some error-checking and output cross-reference
9136 ++processing_template_decl
;
9138 if (friend_type
!= error_mark_node
)
9139 make_friend_class (type
, friend_type
, /*complain=*/false);
9141 if (adjust_processing_template_decl
)
9142 --processing_template_decl
;
9146 /* Build new DECL_FRIENDLIST. */
9149 /* The file and line for this declaration, to
9150 assist in error message reporting. Since we
9151 called push_tinst_level above, we don't need to
9153 input_location
= DECL_SOURCE_LOCATION (t
);
9155 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9157 ++processing_template_decl
;
9158 push_deferring_access_checks (dk_no_check
);
9161 r
= tsubst_friend_function (t
, args
);
9162 add_friend (type
, r
, /*complain=*/false);
9163 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9165 pop_deferring_access_checks ();
9166 --processing_template_decl
;
9172 if (tree expr
= CLASSTYPE_LAMBDA_EXPR (type
))
9174 tree decl
= lambda_function (type
);
9177 if (!DECL_TEMPLATE_INFO (decl
)
9178 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl
)) != decl
)
9179 instantiate_decl (decl
, false, false);
9181 /* We need to instantiate the capture list from the template
9182 after we've instantiated the closure members, but before we
9183 consider adding the conversion op. Also keep any captures
9184 that may have been added during instantiation of the op(). */
9185 tree tmpl_expr
= CLASSTYPE_LAMBDA_EXPR (pattern
);
9187 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr
),
9188 args
, tf_warning_or_error
, NULL_TREE
,
9191 LAMBDA_EXPR_CAPTURE_LIST (expr
)
9192 = chainon (tmpl_cap
, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr
)));
9194 maybe_add_lambda_conv_op (type
);
9197 gcc_assert (errorcount
);
9200 /* Set the file and line number information to whatever is given for
9201 the class itself. This puts error messages involving generated
9202 implicit functions at a predictable point, and the same point
9203 that would be used for non-template classes. */
9204 input_location
= DECL_SOURCE_LOCATION (typedecl
);
9206 unreverse_member_declarations (type
);
9207 finish_struct_1 (type
);
9208 TYPE_BEING_DEFINED (type
) = 0;
9210 /* We don't instantiate default arguments for member functions. 14.7.1:
9212 The implicit instantiation of a class template specialization causes
9213 the implicit instantiation of the declarations, but not of the
9214 definitions or default arguments, of the class member functions,
9215 member classes, static data members and member templates.... */
9217 /* Some typedefs referenced from within the template code need to be access
9218 checked at template instantiation time, i.e now. These types were
9219 added to the template at parsing time. Let's get those and perform
9220 the access checks then. */
9221 perform_typedefs_access_check (pattern
, args
);
9222 perform_deferred_access_checks (tf_warning_or_error
);
9223 pop_nested_class ();
9224 maximum_field_alignment
= saved_maximum_field_alignment
;
9226 pop_from_top_level ();
9227 pop_deferring_access_checks ();
9230 /* The vtable for a template class can be emitted in any translation
9231 unit in which the class is instantiated. When there is no key
9232 method, however, finish_struct_1 will already have added TYPE to
9233 the keyed_classes list. */
9234 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
9235 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
9240 /* Wrapper for instantiate_class_template_1. */
9243 instantiate_class_template (tree type
)
9246 timevar_push (TV_TEMPLATE_INST
);
9247 ret
= instantiate_class_template_1 (type
);
9248 timevar_pop (TV_TEMPLATE_INST
);
9253 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9259 else if (TYPE_P (t
))
9260 r
= tsubst (t
, args
, complain
, in_decl
);
9263 if (!(complain
& tf_warning
))
9264 ++c_inhibit_evaluation_warnings
;
9265 r
= tsubst_expr (t
, args
, complain
, in_decl
,
9266 /*integral_constant_expression_p=*/true);
9267 if (!(complain
& tf_warning
))
9268 --c_inhibit_evaluation_warnings
;
9269 /* Preserve the raw-reference nature of T. */
9270 if (TREE_TYPE (t
) && TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
9271 && REFERENCE_REF_P (r
))
9272 r
= TREE_OPERAND (r
, 0);
9277 /* Given a function parameter pack TMPL_PARM and some function parameters
9278 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9279 and set *SPEC_P to point at the next point in the list. */
9282 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
9284 /* Collect all of the extra "packed" parameters into an
9288 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
9289 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
9290 tree spec_parm
= *spec_p
;
9293 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
9295 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
9298 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9299 parmvec
= make_tree_vec (len
);
9300 parmtypevec
= make_tree_vec (len
);
9301 spec_parm
= *spec_p
;
9302 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
9304 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
9305 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
9308 /* Build the argument packs. */
9309 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
9310 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
9311 TREE_TYPE (argpack
) = argtypepack
;
9312 *spec_p
= spec_parm
;
9317 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9318 NONTYPE_ARGUMENT_PACK. */
9321 make_fnparm_pack (tree spec_parm
)
9323 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
9326 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9330 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
9332 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
9333 if (i
>= TREE_VEC_LENGTH (vec
))
9335 return PACK_EXPANSION_P (TREE_VEC_ELT (vec
, i
));
9339 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9342 make_argument_pack_select (tree arg_pack
, unsigned index
)
9344 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
9346 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
9347 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9352 /* This is a subroutine of tsubst_pack_expansion.
9354 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9355 mechanism to store the (non complete list of) arguments of the
9356 substitution and return a non substituted pack expansion, in order
9357 to wait for when we have enough arguments to really perform the
9361 use_pack_expansion_extra_args_p (tree parm_packs
,
9365 /* If one pack has an expansion and another pack has a normal
9366 argument or if one pack has an empty argument and an another
9367 one hasn't then tsubst_pack_expansion cannot perform the
9368 substitution and need to fall back on the
9369 PACK_EXPANSION_EXTRA mechanism. */
9370 if (parm_packs
== NULL_TREE
)
9372 else if (has_empty_arg
)
9375 bool has_expansion_arg
= false;
9376 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
9378 bool has_non_expansion_arg
= false;
9379 for (tree parm_pack
= parm_packs
;
9381 parm_pack
= TREE_CHAIN (parm_pack
))
9383 tree arg
= TREE_VALUE (parm_pack
);
9385 if (argument_pack_element_is_expansion_p (arg
, i
))
9386 has_expansion_arg
= true;
9388 has_non_expansion_arg
= true;
9391 if (has_expansion_arg
&& has_non_expansion_arg
)
9397 /* [temp.variadic]/6 says that:
9399 The instantiation of a pack expansion [...]
9400 produces a list E1,E2, ..., En, where N is the number of elements
9401 in the pack expansion parameters.
9403 This subroutine of tsubst_pack_expansion produces one of these Ei.
9405 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9406 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9407 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9408 INDEX is the index 'i' of the element Ei to produce. ARGS,
9409 COMPLAIN, and IN_DECL are the same parameters as for the
9410 tsubst_pack_expansion function.
9412 The function returns the resulting Ei upon successful completion,
9415 Note that this function possibly modifies the ARGS parameter, so
9416 it's the responsibility of the caller to restore it. */
9419 gen_elem_of_pack_expansion_instantiation (tree pattern
,
9422 tree args
/* This parm gets
9424 tsubst_flags_t complain
,
9428 bool ith_elem_is_expansion
= false;
9430 /* For each parameter pack, change the substitution of the parameter
9431 pack to the ith argument in its argument pack, then expand the
9433 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
9435 tree parm
= TREE_PURPOSE (pack
);
9436 tree arg_pack
= TREE_VALUE (pack
);
9437 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
9439 ith_elem_is_expansion
|=
9440 argument_pack_element_is_expansion_p (arg_pack
, index
);
9442 /* Select the Ith argument from the pack. */
9443 if (TREE_CODE (parm
) == PARM_DECL
9444 || TREE_CODE (parm
) == FIELD_DECL
)
9448 aps
= make_argument_pack_select (arg_pack
, index
);
9450 register_local_specialization (aps
, parm
);
9453 aps
= retrieve_local_specialization (parm
);
9458 template_parm_level_and_index (parm
, &level
, &idx
);
9462 aps
= make_argument_pack_select (arg_pack
, index
);
9463 /* Update the corresponding argument. */
9464 TMPL_ARG (args
, level
, idx
) = aps
;
9467 /* Re-use the ARGUMENT_PACK_SELECT. */
9468 aps
= TMPL_ARG (args
, level
, idx
);
9470 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9473 /* Substitute into the PATTERN with the (possibly altered)
9475 if (!TYPE_P (pattern
))
9476 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9477 /*integral_constant_expression_p=*/false);
9479 t
= tsubst (pattern
, args
, complain
, in_decl
);
9481 /* If the Ith argument pack element is a pack expansion, then
9482 the Ith element resulting from the substituting is going to
9483 be a pack expansion as well. */
9484 if (ith_elem_is_expansion
)
9485 t
= make_pack_expansion (t
);
9490 /* Substitute ARGS into T, which is an pack expansion
9491 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9492 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9493 (if only a partial substitution could be performed) or
9494 ERROR_MARK_NODE if there was an error. */
9496 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
9500 tree pack
, packs
= NULL_TREE
;
9501 bool unsubstituted_packs
= false;
9504 struct pointer_map_t
*saved_local_specializations
= NULL
;
9505 bool need_local_specializations
= false;
9508 gcc_assert (PACK_EXPANSION_P (t
));
9509 pattern
= PACK_EXPANSION_PATTERN (t
);
9511 /* Add in any args remembered from an earlier partial instantiation. */
9512 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
9514 levels
= TMPL_ARGS_DEPTH (args
);
9516 /* Determine the argument packs that will instantiate the parameter
9517 packs used in the expansion expression. While we're at it,
9518 compute the number of arguments to be expanded and make sure it
9520 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
9521 pack
= TREE_CHAIN (pack
))
9523 tree parm_pack
= TREE_VALUE (pack
);
9524 tree arg_pack
= NULL_TREE
;
9525 tree orig_arg
= NULL_TREE
;
9528 if (TREE_CODE (parm_pack
) == BASES
)
9530 if (BASES_DIRECT (parm_pack
))
9531 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9532 args
, complain
, in_decl
, false));
9534 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9535 args
, complain
, in_decl
, false));
9537 if (TREE_CODE (parm_pack
) == PARM_DECL
)
9539 if (PACK_EXPANSION_LOCAL_P (t
))
9540 arg_pack
= retrieve_local_specialization (parm_pack
);
9543 /* We can't rely on local_specializations for a parameter
9544 name used later in a function declaration (such as in a
9545 late-specified return type). Even if it exists, it might
9546 have the wrong value for a recursive call. Just make a
9547 dummy decl, since it's only used for its type. */
9548 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
9549 if (arg_pack
&& DECL_PACK_P (arg_pack
))
9550 /* Partial instantiation of the parm_pack, we can't build
9551 up an argument pack yet. */
9552 arg_pack
= NULL_TREE
;
9554 arg_pack
= make_fnparm_pack (arg_pack
);
9555 need_local_specializations
= true;
9558 else if (TREE_CODE (parm_pack
) == FIELD_DECL
)
9559 arg_pack
= tsubst_copy (parm_pack
, args
, complain
, in_decl
);
9563 template_parm_level_and_index (parm_pack
, &level
, &idx
);
9565 if (level
<= levels
)
9566 arg_pack
= TMPL_ARG (args
, level
, idx
);
9569 orig_arg
= arg_pack
;
9570 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
9571 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
9573 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
9574 /* This can only happen if we forget to expand an argument
9575 pack somewhere else. Just return an error, silently. */
9577 result
= make_tree_vec (1);
9578 TREE_VEC_ELT (result
, 0) = error_mark_node
;
9585 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
9587 /* Don't bother trying to do a partial substitution with
9588 incomplete packs; we'll try again after deduction. */
9589 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
9594 else if (len
!= my_len
)
9596 if (!(complain
& tf_error
))
9597 /* Fail quietly. */;
9598 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
9599 error ("mismatched argument pack lengths while expanding "
9603 error ("mismatched argument pack lengths while expanding "
9606 return error_mark_node
;
9609 /* Keep track of the parameter packs and their corresponding
9611 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
9612 TREE_TYPE (packs
) = orig_arg
;
9616 /* We can't substitute for this parameter pack. We use a flag as
9617 well as the missing_level counter because function parameter
9618 packs don't have a level. */
9619 unsubstituted_packs
= true;
9623 /* We cannot expand this expansion expression, because we don't have
9624 all of the argument packs we need. */
9625 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
9627 /* We got some full packs, but we can't substitute them in until we
9628 have values for all the packs. So remember these until then. */
9630 t
= make_pack_expansion (pattern
);
9631 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
9634 else if (unsubstituted_packs
)
9636 /* There were no real arguments, we're just replacing a parameter
9637 pack with another version of itself. Substitute into the
9638 pattern and return a PACK_EXPANSION_*. The caller will need to
9640 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
9641 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9642 /*integral_constant_expression_p=*/false);
9644 t
= tsubst (pattern
, args
, complain
, in_decl
);
9645 t
= make_pack_expansion (t
);
9649 gcc_assert (len
>= 0);
9651 if (need_local_specializations
)
9653 /* We're in a late-specified return type, so create our own local
9654 specializations map; the current map is either NULL or (in the
9655 case of recursive unification) might have bindings that we don't
9656 want to use or alter. */
9657 saved_local_specializations
= local_specializations
;
9658 local_specializations
= pointer_map_create ();
9661 /* For each argument in each argument pack, substitute into the
9663 result
= make_tree_vec (len
);
9664 for (i
= 0; i
< len
; ++i
)
9666 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
9670 TREE_VEC_ELT (result
, i
) = t
;
9671 if (t
== error_mark_node
)
9673 result
= error_mark_node
;
9678 /* Update ARGS to restore the substitution from parameter packs to
9679 their argument packs. */
9680 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9682 tree parm
= TREE_PURPOSE (pack
);
9684 if (TREE_CODE (parm
) == PARM_DECL
9685 || TREE_CODE (parm
) == FIELD_DECL
)
9686 register_local_specialization (TREE_TYPE (pack
), parm
);
9691 if (TREE_VALUE (pack
) == NULL_TREE
)
9694 template_parm_level_and_index (parm
, &level
, &idx
);
9696 /* Update the corresponding argument. */
9697 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
9698 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
9701 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
9705 if (need_local_specializations
)
9707 pointer_map_destroy (local_specializations
);
9708 local_specializations
= saved_local_specializations
;
9714 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9715 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9716 parameter packs; all parms generated from a function parameter pack will
9717 have the same DECL_PARM_INDEX. */
9720 get_pattern_parm (tree parm
, tree tmpl
)
9722 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9725 if (DECL_ARTIFICIAL (parm
))
9727 for (patparm
= DECL_ARGUMENTS (pattern
);
9728 patparm
; patparm
= DECL_CHAIN (patparm
))
9729 if (DECL_ARTIFICIAL (patparm
)
9730 && DECL_NAME (parm
) == DECL_NAME (patparm
))
9735 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
9736 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
9737 gcc_assert (DECL_PARM_INDEX (patparm
)
9738 == DECL_PARM_INDEX (parm
));
9744 /* Substitute ARGS into the vector or list of template arguments T. */
9747 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9750 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
9753 if (t
== error_mark_node
)
9754 return error_mark_node
;
9756 len
= TREE_VEC_LENGTH (t
);
9757 elts
= XALLOCAVEC (tree
, len
);
9759 for (i
= 0; i
< len
; i
++)
9761 tree orig_arg
= TREE_VEC_ELT (t
, i
);
9764 if (TREE_CODE (orig_arg
) == TREE_VEC
)
9765 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
9766 else if (PACK_EXPANSION_P (orig_arg
))
9768 /* Substitute into an expansion expression. */
9769 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
9771 if (TREE_CODE (new_arg
) == TREE_VEC
)
9772 /* Add to the expanded length adjustment the number of
9773 expanded arguments. We subtract one from this
9774 measurement, because the argument pack expression
9775 itself is already counted as 1 in
9776 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9777 the argument pack is empty. */
9778 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
9780 else if (ARGUMENT_PACK_P (orig_arg
))
9782 /* Substitute into each of the arguments. */
9783 new_arg
= TYPE_P (orig_arg
)
9784 ? cxx_make_type (TREE_CODE (orig_arg
))
9785 : make_node (TREE_CODE (orig_arg
));
9787 SET_ARGUMENT_PACK_ARGS (
9789 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
9790 args
, complain
, in_decl
));
9792 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
9793 new_arg
= error_mark_node
;
9795 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
9796 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
9798 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
9800 if (TREE_TYPE (new_arg
) == error_mark_node
)
9801 new_arg
= error_mark_node
;
9805 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
9807 if (new_arg
== error_mark_node
)
9808 return error_mark_node
;
9811 if (new_arg
!= orig_arg
)
9818 /* Make space for the expanded arguments coming from template
9820 t
= make_tree_vec (len
+ expanded_len_adjust
);
9821 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9822 arguments for a member template.
9823 In that case each TREE_VEC in ORIG_T represents a level of template
9824 arguments, and ORIG_T won't carry any non defaulted argument count.
9825 It will rather be the nested TREE_VECs that will carry one.
9826 In other words, ORIG_T carries a non defaulted argument count only
9827 if it doesn't contain any nested TREE_VEC. */
9828 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
9830 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
9831 count
+= expanded_len_adjust
;
9832 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
9834 for (i
= 0, out
= 0; i
< len
; i
++)
9836 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
9837 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
9838 && TREE_CODE (elts
[i
]) == TREE_VEC
)
9842 /* Now expand the template argument pack "in place". */
9843 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
9844 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
9848 TREE_VEC_ELT (t
, out
) = elts
[i
];
9856 /* Return the result of substituting ARGS into the template parameters
9857 given by PARMS. If there are m levels of ARGS and m + n levels of
9858 PARMS, then the result will contain n levels of PARMS. For
9859 example, if PARMS is `template <class T> template <class U>
9860 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9861 result will be `template <int*, double, class V>'. */
9864 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
9869 /* When substituting into a template, we must set
9870 PROCESSING_TEMPLATE_DECL as the template parameters may be
9871 dependent if they are based on one-another, and the dependency
9872 predicates are short-circuit outside of templates. */
9873 ++processing_template_decl
;
9875 for (new_parms
= &r
;
9876 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
9877 new_parms
= &(TREE_CHAIN (*new_parms
)),
9878 parms
= TREE_CHAIN (parms
))
9881 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
9884 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
9888 if (parms
== error_mark_node
)
9891 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
9893 if (tuple
== error_mark_node
)
9896 TREE_VEC_ELT (new_vec
, i
) =
9897 tsubst_template_parm (tuple
, args
, complain
);
9901 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
9902 - TMPL_ARGS_DEPTH (args
)),
9903 new_vec
, NULL_TREE
);
9906 --processing_template_decl
;
9911 /* Return the result of substituting ARGS into one template parameter
9912 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9913 parameter and which TREE_PURPOSE is the default argument of the
9914 template parameter. */
9917 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
9919 tree default_value
, parm_decl
;
9921 if (args
== NULL_TREE
9923 || t
== error_mark_node
)
9926 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
9928 default_value
= TREE_PURPOSE (t
);
9929 parm_decl
= TREE_VALUE (t
);
9931 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
9932 if (TREE_CODE (parm_decl
) == PARM_DECL
9933 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
9934 parm_decl
= error_mark_node
;
9935 default_value
= tsubst_template_arg (default_value
, args
,
9936 complain
, NULL_TREE
);
9938 return build_tree_list (default_value
, parm_decl
);
9941 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9942 type T. If T is not an aggregate or enumeration type, it is
9943 handled as if by tsubst. IN_DECL is as for tsubst. If
9944 ENTERING_SCOPE is nonzero, T is the context for a template which
9945 we are presently tsubst'ing. Return the substituted value. */
9948 tsubst_aggr_type (tree t
,
9950 tsubst_flags_t complain
,
9957 switch (TREE_CODE (t
))
9960 if (TYPE_PTRMEMFUNC_P (t
))
9961 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
9963 /* Else fall through. */
9966 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
9971 int saved_unevaluated_operand
;
9972 int saved_inhibit_evaluation_warnings
;
9974 /* In "sizeof(X<I>)" we need to evaluate "I". */
9975 saved_unevaluated_operand
= cp_unevaluated_operand
;
9976 cp_unevaluated_operand
= 0;
9977 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9978 c_inhibit_evaluation_warnings
= 0;
9980 /* First, determine the context for the type we are looking
9982 context
= TYPE_CONTEXT (t
);
9983 if (context
&& TYPE_P (context
))
9985 context
= tsubst_aggr_type (context
, args
, complain
,
9986 in_decl
, /*entering_scope=*/1);
9987 /* If context is a nested class inside a class template,
9988 it may still need to be instantiated (c++/33959). */
9989 context
= complete_type (context
);
9992 /* Then, figure out what arguments are appropriate for the
9993 type we are trying to find. For example, given:
9995 template <class T> struct S;
9996 template <class T, class U> void f(T, U) { S<U> su; }
9998 and supposing that we are instantiating f<int, double>,
9999 then our ARGS will be {int, double}, but, when looking up
10000 S we only want {double}. */
10001 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
10002 complain
, in_decl
);
10003 if (argvec
== error_mark_node
)
10004 r
= error_mark_node
;
10007 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
10008 entering_scope
, complain
);
10009 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
10012 cp_unevaluated_operand
= saved_unevaluated_operand
;
10013 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
10018 /* This is not a template type, so there's nothing to do. */
10022 return tsubst (t
, args
, complain
, in_decl
);
10026 /* Substitute into the default argument ARG (a default argument for
10027 FN), which has the indicated TYPE. */
10030 tsubst_default_argument (tree fn
, tree type
, tree arg
, tsubst_flags_t complain
)
10032 tree saved_class_ptr
= NULL_TREE
;
10033 tree saved_class_ref
= NULL_TREE
;
10034 int errs
= errorcount
+ sorrycount
;
10036 /* This can happen in invalid code. */
10037 if (TREE_CODE (arg
) == DEFAULT_ARG
)
10040 /* This default argument came from a template. Instantiate the
10041 default argument here, not in tsubst. In the case of
10050 we must be careful to do name lookup in the scope of S<T>,
10051 rather than in the current class. */
10052 push_access_scope (fn
);
10053 /* The "this" pointer is not valid in a default argument. */
10056 saved_class_ptr
= current_class_ptr
;
10057 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
10058 saved_class_ref
= current_class_ref
;
10059 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
10062 push_deferring_access_checks(dk_no_deferred
);
10063 /* The default argument expression may cause implicitly defined
10064 member functions to be synthesized, which will result in garbage
10065 collection. We must treat this situation as if we were within
10066 the body of function so as to avoid collecting live data on the
10069 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
10070 complain
, NULL_TREE
,
10071 /*integral_constant_expression_p=*/false);
10073 pop_deferring_access_checks();
10075 /* Restore the "this" pointer. */
10078 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
10079 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
10082 if (errorcount
+sorrycount
> errs
10083 && (complain
& tf_warning_or_error
))
10084 inform (input_location
,
10085 " when instantiating default argument for call to %D", fn
);
10087 /* Make sure the default argument is reasonable. */
10088 arg
= check_default_argument (type
, arg
, complain
);
10090 pop_access_scope (fn
);
10095 /* Substitute into all the default arguments for FN. */
10098 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
10103 tmpl_args
= DECL_TI_ARGS (fn
);
10105 /* If this function is not yet instantiated, we certainly don't need
10106 its default arguments. */
10107 if (uses_template_parms (tmpl_args
))
10109 /* Don't do this again for clones. */
10110 if (DECL_CLONED_FUNCTION_P (fn
))
10113 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
10115 arg
= TREE_CHAIN (arg
))
10116 if (TREE_PURPOSE (arg
))
10117 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
10119 TREE_PURPOSE (arg
),
10123 /* Substitute the ARGS into the T, which is a _DECL. Return the
10124 result of the substitution. Issue error and warning messages under
10125 control of COMPLAIN. */
10128 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
10130 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10131 location_t saved_loc
;
10132 tree r
= NULL_TREE
;
10134 hashval_t hash
= 0;
10136 /* Set the filename and linenumber to improve error-reporting. */
10137 saved_loc
= input_location
;
10138 input_location
= DECL_SOURCE_LOCATION (t
);
10140 switch (TREE_CODE (t
))
10142 case TEMPLATE_DECL
:
10144 /* We can get here when processing a member function template,
10145 member class template, or template template parameter. */
10146 tree decl
= DECL_TEMPLATE_RESULT (t
);
10151 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10153 /* Template template parameter is treated here. */
10154 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10155 if (new_type
== error_mark_node
)
10156 RETURN (error_mark_node
);
10157 /* If we get a real template back, return it. This can happen in
10158 the context of most_specialized_class. */
10159 if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
10163 DECL_CHAIN (r
) = NULL_TREE
;
10164 TREE_TYPE (r
) = new_type
;
10165 DECL_TEMPLATE_RESULT (r
)
10166 = build_decl (DECL_SOURCE_LOCATION (decl
),
10167 TYPE_DECL
, DECL_NAME (decl
), new_type
);
10168 DECL_TEMPLATE_PARMS (r
)
10169 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10171 TYPE_NAME (new_type
) = r
;
10175 /* We might already have an instance of this template.
10176 The ARGS are for the surrounding class type, so the
10177 full args contain the tsubst'd args for the context,
10178 plus the innermost args from the template decl. */
10179 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
10180 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
10181 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
10182 /* Because this is a template, the arguments will still be
10183 dependent, even after substitution. If
10184 PROCESSING_TEMPLATE_DECL is not set, the dependency
10185 predicates will short-circuit. */
10186 ++processing_template_decl
;
10187 full_args
= tsubst_template_args (tmpl_args
, args
,
10188 complain
, in_decl
);
10189 --processing_template_decl
;
10190 if (full_args
== error_mark_node
)
10191 RETURN (error_mark_node
);
10193 /* If this is a default template template argument,
10194 tsubst might not have changed anything. */
10195 if (full_args
== tmpl_args
)
10198 hash
= hash_tmpl_and_args (t
, full_args
);
10199 spec
= retrieve_specialization (t
, full_args
, hash
);
10200 if (spec
!= NULL_TREE
)
10206 /* Make a new template decl. It will be similar to the
10207 original, but will record the current template arguments.
10208 We also create a new function declaration, which is just
10209 like the old one, but points to this new template, rather
10210 than the old one. */
10212 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
10213 DECL_CHAIN (r
) = NULL_TREE
;
10215 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10217 if (TREE_CODE (decl
) == TYPE_DECL
10218 && !TYPE_DECL_ALIAS_P (decl
))
10221 ++processing_template_decl
;
10222 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10223 --processing_template_decl
;
10224 if (new_type
== error_mark_node
)
10225 RETURN (error_mark_node
);
10227 TREE_TYPE (r
) = new_type
;
10228 /* For a partial specialization, we need to keep pointing to
10229 the primary template. */
10230 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
10231 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
10232 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
10233 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
10234 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
10239 ++processing_template_decl
;
10240 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
10241 --processing_template_decl
;
10242 if (new_decl
== error_mark_node
)
10243 RETURN (error_mark_node
);
10245 DECL_TEMPLATE_RESULT (r
) = new_decl
;
10246 DECL_TI_TEMPLATE (new_decl
) = r
;
10247 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
10248 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
10249 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
10252 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10253 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
10254 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
10256 /* The template parameters for this new template are all the
10257 template parameters for the old template, except the
10258 outermost level of parameters. */
10259 DECL_TEMPLATE_PARMS (r
)
10260 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10263 if (PRIMARY_TEMPLATE_P (t
))
10264 DECL_PRIMARY_TEMPLATE (r
) = r
;
10266 if (TREE_CODE (decl
) != TYPE_DECL
)
10267 /* Record this non-type partial instantiation. */
10268 register_specialization (r
, t
,
10269 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
10274 case FUNCTION_DECL
:
10277 tree argvec
= NULL_TREE
;
10285 /* Nobody should be tsubst'ing into non-template functions. */
10286 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
10288 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
10293 /* If T is not dependent, just return it. We have to
10294 increment PROCESSING_TEMPLATE_DECL because
10295 value_dependent_expression_p assumes that nothing is
10296 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10297 ++processing_template_decl
;
10298 dependent_p
= value_dependent_expression_p (t
);
10299 --processing_template_decl
;
10303 /* Calculate the most general template of which R is a
10304 specialization, and the complete set of arguments used to
10306 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
10307 argvec
= tsubst_template_args (DECL_TI_ARGS
10308 (DECL_TEMPLATE_RESULT
10309 (DECL_TI_TEMPLATE (t
))),
10310 args
, complain
, in_decl
);
10311 if (argvec
== error_mark_node
)
10312 RETURN (error_mark_node
);
10314 /* Check to see if we already have this specialization. */
10315 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10316 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10324 /* We can see more levels of arguments than parameters if
10325 there was a specialization of a member template, like
10328 template <class T> struct S { template <class U> void f(); }
10329 template <> template <class U> void S<int>::f(U);
10331 Here, we'll be substituting into the specialization,
10332 because that's where we can find the code we actually
10333 want to generate, but we'll have enough arguments for
10334 the most general template.
10336 We also deal with the peculiar case:
10338 template <class T> struct S {
10339 template <class U> friend void f();
10341 template <class U> void f() {}
10343 template void f<double>();
10345 Here, the ARGS for the instantiation of will be {int,
10346 double}. But, we only need as many ARGS as there are
10347 levels of template parameters in CODE_PATTERN. We are
10348 careful not to get fooled into reducing the ARGS in
10351 template <class T> struct S { template <class U> void f(U); }
10352 template <class T> template <> void S<T>::f(int) {}
10354 which we can spot because the pattern will be a
10355 specialization in this case. */
10356 args_depth
= TMPL_ARGS_DEPTH (args
);
10358 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
10359 if (args_depth
> parms_depth
10360 && !DECL_TEMPLATE_SPECIALIZATION (t
))
10361 args
= get_innermost_template_args (args
, parms_depth
);
10365 /* This special case arises when we have something like this:
10367 template <class T> struct S {
10368 friend void f<int>(int, double);
10371 Here, the DECL_TI_TEMPLATE for the friend declaration
10372 will be an IDENTIFIER_NODE. We are being called from
10373 tsubst_friend_function, and we want only to create a
10374 new decl (R) with appropriate types so that we can call
10375 determine_specialization. */
10376 gen_tmpl
= NULL_TREE
;
10379 if (DECL_CLASS_SCOPE_P (t
))
10381 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
10385 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
10386 complain
, t
, /*entering_scope=*/1);
10391 ctx
= DECL_CONTEXT (t
);
10393 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10394 if (type
== error_mark_node
)
10395 RETURN (error_mark_node
);
10397 /* If we hit excessive deduction depth, the type is bogus even if
10398 it isn't error_mark_node, so don't build a decl. */
10399 if (excessive_deduction_depth
)
10400 RETURN (error_mark_node
);
10402 /* We do NOT check for matching decls pushed separately at this
10403 point, as they may not represent instantiations of this
10404 template, and in any case are considered separate under the
10407 DECL_USE_TEMPLATE (r
) = 0;
10408 TREE_TYPE (r
) = type
;
10409 /* Clear out the mangled name and RTL for the instantiation. */
10410 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10411 SET_DECL_RTL (r
, NULL
);
10412 /* Leave DECL_INITIAL set on deleted instantiations. */
10413 if (!DECL_DELETED_FN (r
))
10414 DECL_INITIAL (r
) = NULL_TREE
;
10415 DECL_CONTEXT (r
) = ctx
;
10417 /* OpenMP UDRs have the only argument a reference to the declared
10418 type. We want to diagnose if the declared type is a reference,
10419 which is invalid, but as references to references are usually
10420 quietly merged, diagnose it here. */
10421 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
10424 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
10425 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
10426 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
10427 error_at (DECL_SOURCE_LOCATION (t
),
10428 "reference type %qT in "
10429 "%<#pragma omp declare reduction%>", argtype
);
10430 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
10431 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
10435 if (member
&& DECL_CONV_FN_P (r
))
10436 /* Type-conversion operator. Reconstruct the name, in
10437 case it's the name of one of the template's parameters. */
10438 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
10440 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
10442 DECL_RESULT (r
) = NULL_TREE
;
10444 TREE_STATIC (r
) = 0;
10445 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
10446 DECL_EXTERNAL (r
) = 1;
10447 /* If this is an instantiation of a function with internal
10448 linkage, we already know what object file linkage will be
10449 assigned to the instantiation. */
10450 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
10451 DECL_DEFER_OUTPUT (r
) = 0;
10452 DECL_CHAIN (r
) = NULL_TREE
;
10453 DECL_PENDING_INLINE_INFO (r
) = 0;
10454 DECL_PENDING_INLINE_P (r
) = 0;
10455 DECL_SAVED_TREE (r
) = NULL_TREE
;
10456 DECL_STRUCT_FUNCTION (r
) = NULL
;
10458 /* We'll re-clone as appropriate in instantiate_template. */
10459 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
10461 /* If we aren't complaining now, return on error before we register
10462 the specialization so that we'll complain eventually. */
10463 if ((complain
& tf_error
) == 0
10464 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10465 && !grok_op_properties (r
, /*complain=*/false))
10466 RETURN (error_mark_node
);
10468 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10469 this in the special friend case mentioned above where
10470 GEN_TMPL is NULL. */
10473 DECL_TEMPLATE_INFO (r
)
10474 = build_template_info (gen_tmpl
, argvec
);
10475 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10478 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10480 /* We instantiated this while substituting into
10481 the type earlier (template/friend54.C). */
10484 /* We're not supposed to instantiate default arguments
10485 until they are called, for a template. But, for a
10488 template <class T> void f ()
10489 { extern void g(int i = T()); }
10491 we should do the substitution when the template is
10492 instantiated. We handle the member function case in
10493 instantiate_class_template since the default arguments
10494 might refer to other members of the class. */
10496 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
10497 && !uses_template_parms (argvec
))
10498 tsubst_default_arguments (r
, complain
);
10501 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
10503 /* Copy the list of befriending classes. */
10504 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
10506 friends
= &TREE_CHAIN (*friends
))
10508 *friends
= copy_node (*friends
);
10509 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
10514 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
10516 maybe_retrofit_in_chrg (r
);
10517 if (DECL_CONSTRUCTOR_P (r
))
10518 grok_ctor_properties (ctx
, r
);
10519 if (DECL_INHERITED_CTOR_BASE (r
))
10520 deduce_inheriting_ctor (r
);
10521 /* If this is an instantiation of a member template, clone it.
10522 If it isn't, that'll be handled by
10523 clone_constructors_and_destructors. */
10524 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
10525 clone_function_decl (r
, /*update_method_vec_p=*/0);
10527 else if ((complain
& tf_error
) != 0
10528 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10529 && !grok_op_properties (r
, /*complain=*/true))
10530 RETURN (error_mark_node
);
10532 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
10533 SET_DECL_FRIEND_CONTEXT (r
,
10534 tsubst (DECL_FRIEND_CONTEXT (t
),
10535 args
, complain
, in_decl
));
10537 /* Possibly limit visibility based on template args. */
10538 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10539 if (DECL_VISIBILITY_SPECIFIED (t
))
10541 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10542 DECL_ATTRIBUTES (r
)
10543 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10545 determine_visibility (r
);
10546 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
10547 && !processing_template_decl
)
10548 defaulted_late_check (r
);
10550 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10551 args
, complain
, in_decl
);
10557 tree type
= NULL_TREE
;
10559 tree expanded_types
= NULL_TREE
;
10560 tree prev_r
= NULL_TREE
;
10561 tree first_r
= NULL_TREE
;
10563 if (DECL_PACK_P (t
))
10565 /* If there is a local specialization that isn't a
10566 parameter pack, it means that we're doing a "simple"
10567 substitution from inside tsubst_pack_expansion. Just
10568 return the local specialization (which will be a single
10570 tree spec
= retrieve_local_specialization (t
);
10572 && TREE_CODE (spec
) == PARM_DECL
10573 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
10576 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10577 the parameters in this function parameter pack. */
10578 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10579 complain
, in_decl
);
10580 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10582 len
= TREE_VEC_LENGTH (expanded_types
);
10584 /* Zero-length parameter packs are boring. Just substitute
10587 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
10592 /* All we did was update the type. Make a note of that. */
10593 type
= expanded_types
;
10594 expanded_types
= NULL_TREE
;
10598 /* Loop through all of the parameters we'll build. When T is
10599 a function parameter pack, LEN is the number of expanded
10600 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10602 for (i
= 0; i
< len
; ++i
)
10606 if (DECL_TEMPLATE_PARM_P (t
))
10607 SET_DECL_TEMPLATE_PARM_P (r
);
10609 if (expanded_types
)
10610 /* We're on the Ith parameter of the function parameter
10613 /* Get the Ith type. */
10614 type
= TREE_VEC_ELT (expanded_types
, i
);
10616 /* Rename the parameter to include the index. */
10618 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10621 /* We're dealing with a normal parameter. */
10622 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10624 type
= type_decays_to (type
);
10625 TREE_TYPE (r
) = type
;
10626 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10628 if (DECL_INITIAL (r
))
10630 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
10631 DECL_INITIAL (r
) = TREE_TYPE (r
);
10633 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
10634 complain
, in_decl
);
10637 DECL_CONTEXT (r
) = NULL_TREE
;
10639 if (!DECL_TEMPLATE_PARM_P (r
))
10640 DECL_ARG_TYPE (r
) = type_passed_as (type
);
10642 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10643 args
, complain
, in_decl
);
10645 /* Keep track of the first new parameter we
10646 generate. That's what will be returned to the
10651 /* Build a proper chain of parameters when substituting
10652 into a function parameter pack. */
10654 DECL_CHAIN (prev_r
) = r
;
10657 /* If cp_unevaluated_operand is set, we're just looking for a
10658 single dummy parameter, so don't keep going. */
10659 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
10660 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
10661 complain
, DECL_CHAIN (t
));
10663 /* FIRST_R contains the start of the chain we've built. */
10670 tree type
= NULL_TREE
;
10671 tree vec
= NULL_TREE
;
10672 tree expanded_types
= NULL_TREE
;
10675 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
10677 /* This field is a lambda capture pack. Return a TREE_VEC of
10678 the expanded fields to instantiate_class_template_1 and
10679 store them in the specializations hash table as a
10680 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
10681 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10682 complain
, in_decl
);
10683 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10685 len
= TREE_VEC_LENGTH (expanded_types
);
10686 vec
= make_tree_vec (len
);
10690 /* All we did was update the type. Make a note of that. */
10691 type
= expanded_types
;
10692 expanded_types
= NULL_TREE
;
10696 for (int i
= 0; i
< len
; ++i
)
10699 if (expanded_types
)
10701 type
= TREE_VEC_ELT (expanded_types
, i
);
10703 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10706 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10708 if (type
== error_mark_node
)
10709 RETURN (error_mark_node
);
10710 TREE_TYPE (r
) = type
;
10711 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10713 if (DECL_C_BIT_FIELD (r
))
10714 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10715 non-bit-fields DECL_INITIAL is a non-static data member
10716 initializer, which gets deferred instantiation. */
10718 = tsubst_expr (DECL_INITIAL (t
), args
,
10720 /*integral_constant_expression_p=*/true);
10721 else if (DECL_INITIAL (t
))
10723 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10724 NSDMI in perform_member_init. Still set DECL_INITIAL
10725 so that we know there is one. */
10726 DECL_INITIAL (r
) = void_zero_node
;
10727 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
10728 retrofit_lang_decl (r
);
10729 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10731 /* We don't have to set DECL_CONTEXT here; it is set by
10732 finish_member_declaration. */
10733 DECL_CHAIN (r
) = NULL_TREE
;
10735 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10736 args
, complain
, in_decl
);
10739 TREE_VEC_ELT (vec
, i
) = r
;
10745 tree pack
= make_node (NONTYPE_ARGUMENT_PACK
);
10746 tree tpack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
10747 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
10748 SET_ARGUMENT_PACK_ARGS (tpack
, expanded_types
);
10749 TREE_TYPE (pack
) = tpack
;
10750 register_specialization (pack
, t
, args
, false, 0);
10756 /* We reach here only for member using decls. We also need to check
10757 uses_template_parms because DECL_DEPENDENT_P is not set for a
10758 using-declaration that designates a member of the current
10759 instantiation (c++/53549). */
10760 if (DECL_DEPENDENT_P (t
)
10761 || uses_template_parms (USING_DECL_SCOPE (t
)))
10763 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
10764 complain
, in_decl
);
10765 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
10766 r
= do_class_using_decl (inst_scope
, name
);
10768 r
= error_mark_node
;
10771 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
10772 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
10778 DECL_CHAIN (r
) = NULL_TREE
;
10785 tree argvec
= NULL_TREE
;
10786 tree gen_tmpl
= NULL_TREE
;
10788 tree tmpl
= NULL_TREE
;
10790 tree type
= NULL_TREE
;
10793 if (TREE_CODE (t
) == TYPE_DECL
10794 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
10796 /* If this is the canonical decl, we don't have to
10797 mess with instantiations, and often we can't (for
10798 typename, template type parms and such). Note that
10799 TYPE_NAME is not correct for the above test if
10800 we've copied the type for a typedef. */
10801 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10802 if (type
== error_mark_node
)
10803 RETURN (error_mark_node
);
10804 r
= TYPE_NAME (type
);
10808 /* Check to see if we already have the specialization we
10811 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
10813 /* T is a static data member or namespace-scope entity.
10814 We have to substitute into namespace-scope variables
10815 (even though such entities are never templates) because
10818 template <class T> void f() { extern T t; }
10820 where the entity referenced is not known until
10821 instantiation time. */
10823 ctx
= DECL_CONTEXT (t
);
10824 if (DECL_CLASS_SCOPE_P (t
))
10826 ctx
= tsubst_aggr_type (ctx
, args
,
10828 in_decl
, /*entering_scope=*/1);
10829 /* If CTX is unchanged, then T is in fact the
10830 specialization we want. That situation occurs when
10831 referencing a static data member within in its own
10832 class. We can use pointer equality, rather than
10833 same_type_p, because DECL_CONTEXT is always
10835 if (ctx
== DECL_CONTEXT (t
)
10836 && (TREE_CODE (t
) != TYPE_DECL
10837 /* ... unless T is a member template; in which
10838 case our caller can be willing to create a
10839 specialization of that template represented
10841 || !(DECL_TI_TEMPLATE (t
)
10842 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))))
10848 tmpl
= DECL_TI_TEMPLATE (t
);
10849 gen_tmpl
= most_general_template (tmpl
);
10850 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
10851 if (argvec
== error_mark_node
)
10852 RETURN (error_mark_node
);
10853 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10854 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10859 /* A local variable. */
10861 /* Subsequent calls to pushdecl will fill this in. */
10863 spec
= retrieve_local_specialization (t
);
10865 /* If we already have the specialization we need, there is
10866 nothing more to do. */
10873 /* Create a new node for the specialization we need. */
10875 if (type
== NULL_TREE
)
10877 if (is_typedef_decl (t
))
10878 type
= DECL_ORIGINAL_TYPE (t
);
10880 type
= TREE_TYPE (t
);
10882 && VAR_HAD_UNKNOWN_BOUND (t
)
10883 && type
!= error_mark_node
)
10884 type
= strip_array_domain (type
);
10885 type
= tsubst (type
, args
, complain
, in_decl
);
10889 /* Even if the original location is out of scope, the
10890 newly substituted one is not. */
10891 DECL_DEAD_FOR_LOCAL (r
) = 0;
10892 DECL_INITIALIZED_P (r
) = 0;
10893 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
10894 if (type
== error_mark_node
)
10895 RETURN (error_mark_node
);
10896 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10898 /* It may seem that this case cannot occur, since:
10903 declares a function, not a variable. However:
10906 template <typename T> void g() { T t; }
10907 template void g<f>();
10909 is an attempt to declare a variable with function
10911 error ("variable %qD has function type",
10912 /* R is not yet sufficiently initialized, so we
10913 just use its name. */
10915 RETURN (error_mark_node
);
10917 type
= complete_type (type
);
10918 /* Wait until cp_finish_decl to set this again, to handle
10919 circular dependency (template/instantiate6.C). */
10920 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
10921 type
= check_var_type (DECL_NAME (r
), type
);
10923 if (DECL_HAS_VALUE_EXPR_P (t
))
10925 tree ve
= DECL_VALUE_EXPR (t
);
10926 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
10927 /*constant_expression_p=*/false);
10928 if (REFERENCE_REF_P (ve
))
10930 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
10931 ve
= TREE_OPERAND (ve
, 0);
10933 SET_DECL_VALUE_EXPR (r
, ve
);
10936 else if (DECL_SELF_REFERENCE_P (t
))
10937 SET_DECL_SELF_REFERENCE_P (r
);
10938 TREE_TYPE (r
) = type
;
10939 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10940 DECL_CONTEXT (r
) = ctx
;
10941 /* Clear out the mangled name and RTL for the instantiation. */
10942 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10943 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10944 SET_DECL_RTL (r
, NULL
);
10945 /* The initializer must not be expanded until it is required;
10946 see [temp.inst]. */
10947 DECL_INITIAL (r
) = NULL_TREE
;
10948 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10949 SET_DECL_RTL (r
, NULL
);
10950 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
10953 /* Possibly limit visibility based on template args. */
10954 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10955 if (DECL_VISIBILITY_SPECIFIED (t
))
10957 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10958 DECL_ATTRIBUTES (r
)
10959 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10961 determine_visibility (r
);
10966 /* A static data member declaration is always marked
10967 external when it is declared in-class, even if an
10968 initializer is present. We mimic the non-template
10969 processing here. */
10970 DECL_EXTERNAL (r
) = 1;
10972 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10973 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
10974 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10976 else if (cp_unevaluated_operand
)
10977 gcc_unreachable ();
10979 register_local_specialization (r
, t
);
10981 DECL_CHAIN (r
) = NULL_TREE
;
10983 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
10985 args
, complain
, in_decl
);
10987 /* Preserve a typedef that names a type. */
10988 if (is_typedef_decl (r
))
10990 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
10991 set_underlying_type (r
);
10994 layout_decl (r
, 0);
10999 gcc_unreachable ();
11004 /* Restore the file and line information. */
11005 input_location
= saved_loc
;
11010 /* Substitute into the ARG_TYPES of a function type.
11011 If END is a TREE_CHAIN, leave it and any following types
11015 tsubst_arg_types (tree arg_types
,
11018 tsubst_flags_t complain
,
11021 tree remaining_arg_types
;
11022 tree type
= NULL_TREE
;
11024 tree expanded_args
= NULL_TREE
;
11027 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
11030 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
11031 args
, end
, complain
, in_decl
);
11032 if (remaining_arg_types
== error_mark_node
)
11033 return error_mark_node
;
11035 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
11037 /* For a pack expansion, perform substitution on the
11038 entire expression. Later on, we'll handle the arguments
11040 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
11041 args
, complain
, in_decl
);
11043 if (TREE_CODE (expanded_args
) == TREE_VEC
)
11044 /* So that we'll spin through the parameters, one by one. */
11045 i
= TREE_VEC_LENGTH (expanded_args
);
11048 /* We only partially substituted into the parameter
11049 pack. Our type is TYPE_PACK_EXPANSION. */
11050 type
= expanded_args
;
11051 expanded_args
= NULL_TREE
;
11059 type
= TREE_VEC_ELT (expanded_args
, i
);
11061 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
11063 if (type
== error_mark_node
)
11064 return error_mark_node
;
11065 if (VOID_TYPE_P (type
))
11067 if (complain
& tf_error
)
11069 error ("invalid parameter type %qT", type
);
11071 error ("in declaration %q+D", in_decl
);
11073 return error_mark_node
;
11076 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
11077 return error_mark_node
;
11079 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11080 top-level qualifiers as required. */
11081 type
= cv_unqualified (type_decays_to (type
));
11083 /* We do not substitute into default arguments here. The standard
11084 mandates that they be instantiated only when needed, which is
11085 done in build_over_call. */
11086 default_arg
= TREE_PURPOSE (arg_types
);
11088 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
11090 /* We've instantiated a template before its default arguments
11091 have been parsed. This can happen for a nested template
11092 class, and is not an error unless we require the default
11093 argument in a call of this function. */
11094 remaining_arg_types
=
11095 tree_cons (default_arg
, type
, remaining_arg_types
);
11096 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
11099 remaining_arg_types
=
11100 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
11103 return remaining_arg_types
;
11106 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11107 *not* handle the exception-specification for FNTYPE, because the
11108 initial substitution of explicitly provided template parameters
11109 during argument deduction forbids substitution into the
11110 exception-specification:
11114 All references in the function type of the function template to the
11115 corresponding template parameters are replaced by the specified tem-
11116 plate argument values. If a substitution in a template parameter or
11117 in the function type of the function template results in an invalid
11118 type, type deduction fails. [Note: The equivalent substitution in
11119 exception specifications is done only when the function is instanti-
11120 ated, at which point a program is ill-formed if the substitution
11121 results in an invalid type.] */
11124 tsubst_function_type (tree t
,
11126 tsubst_flags_t complain
,
11133 /* The TYPE_CONTEXT is not used for function/method types. */
11134 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
11136 /* Substitute the return type. */
11137 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11138 if (return_type
== error_mark_node
)
11139 return error_mark_node
;
11140 /* DR 486 clarifies that creation of a function type with an
11141 invalid return type is a deduction failure. */
11142 if (TREE_CODE (return_type
) == ARRAY_TYPE
11143 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
11145 if (complain
& tf_error
)
11147 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
11148 error ("function returning an array");
11150 error ("function returning a function");
11152 return error_mark_node
;
11155 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
11156 return error_mark_node
;
11158 /* Substitute the argument types. */
11159 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
11160 complain
, in_decl
);
11161 if (arg_types
== error_mark_node
)
11162 return error_mark_node
;
11164 /* Construct a new type node and return it. */
11165 if (TREE_CODE (t
) == FUNCTION_TYPE
)
11167 fntype
= build_function_type (return_type
, arg_types
);
11168 fntype
= apply_memfn_quals (fntype
,
11169 type_memfn_quals (t
),
11170 type_memfn_rqual (t
));
11174 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
11175 if (! MAYBE_CLASS_TYPE_P (r
))
11179 Type deduction may fail for any of the following
11182 -- Attempting to create "pointer to member of T" when T
11183 is not a class type. */
11184 if (complain
& tf_error
)
11185 error ("creating pointer to member function of non-class type %qT",
11187 return error_mark_node
;
11190 fntype
= build_method_type_directly (r
, return_type
,
11191 TREE_CHAIN (arg_types
));
11192 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
11194 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
11199 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11200 ARGS into that specification, and return the substituted
11201 specification. If there is no specification, return NULL_TREE. */
11204 tsubst_exception_specification (tree fntype
,
11206 tsubst_flags_t complain
,
11213 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
11214 new_specs
= NULL_TREE
;
11215 if (specs
&& TREE_PURPOSE (specs
))
11217 /* A noexcept-specifier. */
11218 tree expr
= TREE_PURPOSE (specs
);
11219 if (TREE_CODE (expr
) == INTEGER_CST
)
11223 /* Defer instantiation of noexcept-specifiers to avoid
11224 excessive instantiations (c++/49107). */
11225 new_specs
= make_node (DEFERRED_NOEXCEPT
);
11226 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
11228 /* We already partially instantiated this member template,
11229 so combine the new args with the old. */
11230 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
11231 = DEFERRED_NOEXCEPT_PATTERN (expr
);
11232 DEFERRED_NOEXCEPT_ARGS (new_specs
)
11233 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
11237 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
11238 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
11242 new_specs
= tsubst_copy_and_build
11243 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
11244 /*integral_constant_expression_p=*/true);
11245 new_specs
= build_noexcept_spec (new_specs
, complain
);
11249 if (! TREE_VALUE (specs
))
11256 tree expanded_specs
= NULL_TREE
;
11258 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
11260 /* Expand the pack expansion type. */
11261 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
11265 if (expanded_specs
== error_mark_node
)
11266 return error_mark_node
;
11267 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
11268 len
= TREE_VEC_LENGTH (expanded_specs
);
11271 /* We're substituting into a member template, so
11272 we got a TYPE_PACK_EXPANSION back. Add that
11273 expansion and move on. */
11274 gcc_assert (TREE_CODE (expanded_specs
)
11275 == TYPE_PACK_EXPANSION
);
11276 new_specs
= add_exception_specifier (new_specs
,
11279 specs
= TREE_CHAIN (specs
);
11284 for (i
= 0; i
< len
; ++i
)
11286 if (expanded_specs
)
11287 spec
= TREE_VEC_ELT (expanded_specs
, i
);
11289 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
11290 if (spec
== error_mark_node
)
11292 new_specs
= add_exception_specifier (new_specs
, spec
,
11296 specs
= TREE_CHAIN (specs
);
11302 /* Take the tree structure T and replace template parameters used
11303 therein with the argument vector ARGS. IN_DECL is an associated
11304 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11305 Issue error and warning messages under control of COMPLAIN. Note
11306 that we must be relatively non-tolerant of extensions here, in
11307 order to preserve conformance; if we allow substitutions that
11308 should not be allowed, we may allow argument deductions that should
11309 not succeed, and therefore report ambiguous overload situations
11310 where there are none. In theory, we could allow the substitution,
11311 but indicate that it should have failed, and allow our caller to
11312 make sure that the right thing happens, but we don't try to do this
11315 This function is used for dealing with types, decls and the like;
11316 for expressions, use tsubst_expr or tsubst_copy. */
11319 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11321 enum tree_code code
;
11322 tree type
, r
= NULL_TREE
;
11324 if (t
== NULL_TREE
|| t
== error_mark_node
11325 || t
== integer_type_node
11326 || t
== void_type_node
11327 || t
== char_type_node
11328 || t
== unknown_type_node
11329 || TREE_CODE (t
) == NAMESPACE_DECL
11330 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
11334 return tsubst_decl (t
, args
, complain
);
11336 if (args
== NULL_TREE
)
11339 code
= TREE_CODE (t
);
11341 if (code
== IDENTIFIER_NODE
)
11342 type
= IDENTIFIER_TYPE_VALUE (t
);
11344 type
= TREE_TYPE (t
);
11346 gcc_assert (type
!= unknown_type_node
);
11348 /* Reuse typedefs. We need to do this to handle dependent attributes,
11349 such as attribute aligned. */
11351 && typedef_variant_p (t
))
11353 tree decl
= TYPE_NAME (t
);
11355 if (alias_template_specialization_p (t
))
11357 /* DECL represents an alias template and we want to
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
= instantiate_alias_template (tmpl
, gen_args
, complain
);
11363 else if (DECL_CLASS_SCOPE_P (decl
)
11364 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11365 && uses_template_parms (DECL_CONTEXT (decl
)))
11367 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11368 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11369 r
= retrieve_specialization (tmpl
, gen_args
, 0);
11371 else if (DECL_FUNCTION_SCOPE_P (decl
)
11372 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11373 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
11374 r
= retrieve_local_specialization (decl
);
11376 /* The typedef is from a non-template context. */
11382 r
= cp_build_qualified_type_real
11383 (r
, cp_type_quals (t
) | cp_type_quals (r
),
11384 complain
| tf_ignore_bad_quals
);
11389 /* We don't have an instantiation yet, so drop the typedef. */
11390 int quals
= cp_type_quals (t
);
11391 t
= DECL_ORIGINAL_TYPE (decl
);
11392 t
= cp_build_qualified_type_real (t
, quals
,
11393 complain
| tf_ignore_bad_quals
);
11398 && code
!= TYPENAME_TYPE
11399 && code
!= TEMPLATE_TYPE_PARM
11400 && code
!= IDENTIFIER_NODE
11401 && code
!= FUNCTION_TYPE
11402 && code
!= METHOD_TYPE
)
11403 type
= tsubst (type
, args
, complain
, in_decl
);
11404 if (type
== error_mark_node
)
11405 return error_mark_node
;
11411 case ENUMERAL_TYPE
:
11412 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
11413 /*entering_scope=*/0);
11416 case IDENTIFIER_NODE
:
11427 if (t
== integer_type_node
)
11430 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
11431 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
11435 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
11437 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
11438 /*integral_constant_expression_p=*/false);
11440 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11442 if (TREE_CODE (max
) == NOP_EXPR
11443 && TREE_SIDE_EFFECTS (omax
)
11444 && !TREE_TYPE (max
))
11445 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
11447 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11448 with TREE_SIDE_EFFECTS that indicates this is not an integral
11449 constant expression. */
11450 if (processing_template_decl
11451 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
11453 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
11454 TREE_SIDE_EFFECTS (max
) = 1;
11457 return compute_array_index_type (NULL_TREE
, max
, complain
);
11460 case TEMPLATE_TYPE_PARM
:
11461 case TEMPLATE_TEMPLATE_PARM
:
11462 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11463 case TEMPLATE_PARM_INDEX
:
11468 tree arg
= NULL_TREE
;
11472 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
11473 template_parm_level_and_index (t
, &level
, &idx
);
11475 levels
= TMPL_ARGS_DEPTH (args
);
11476 if (level
<= levels
)
11478 arg
= TMPL_ARG (args
, level
, idx
);
11480 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
11482 /* See through ARGUMENT_PACK_SELECT arguments. */
11483 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
11484 /* If the selected argument is an expansion E, that most
11485 likely means we were called from
11486 gen_elem_of_pack_expansion_instantiation during the
11487 substituting of pack an argument pack (which Ith
11488 element is a pack expansion, where I is
11489 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11490 In this case, the Ith element resulting from this
11491 substituting is going to be a pack expansion, which
11492 pattern is the pattern of E. Let's return the
11494 gen_elem_of_pack_expansion_instantiation will
11495 build the resulting pack expansion from it. */
11496 if (PACK_EXPANSION_P (arg
))
11497 arg
= PACK_EXPANSION_PATTERN (arg
);
11501 if (arg
== error_mark_node
)
11502 return error_mark_node
;
11503 else if (arg
!= NULL_TREE
)
11505 if (ARGUMENT_PACK_P (arg
))
11506 /* If ARG is an argument pack, we don't actually want to
11507 perform a substitution here, because substitutions
11508 for argument packs are only done
11509 element-by-element. We can get to this point when
11510 substituting the type of a non-type template
11511 parameter pack, when that type actually contains
11512 template parameter packs from an outer template, e.g.,
11514 template<typename... Types> struct A {
11515 template<Types... Values> struct B { };
11519 if (code
== TEMPLATE_TYPE_PARM
)
11522 gcc_assert (TYPE_P (arg
));
11524 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
11526 return cp_build_qualified_type_real
11527 (arg
, quals
, complain
| tf_ignore_bad_quals
);
11529 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11531 /* We are processing a type constructed from a
11532 template template parameter. */
11533 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
11534 args
, complain
, in_decl
);
11535 if (argvec
== error_mark_node
)
11536 return error_mark_node
;
11538 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
11539 || TREE_CODE (arg
) == TEMPLATE_DECL
11540 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
11542 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
11543 /* Consider this code:
11545 template <template <class> class Template>
11547 template <class Arg> using Bind = Template<Arg>;
11550 template <template <class> class Template, class Arg>
11551 using Instantiate = Template<Arg>; //#0
11553 template <template <class> class Template,
11556 Instantiate<Internal<Template>::template Bind,
11559 When #1 is parsed, the
11560 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11561 parameter `Template' in #0 matches the
11562 UNBOUND_CLASS_TEMPLATE representing the argument
11563 `Internal<Template>::template Bind'; We then want
11564 to assemble the type `Bind<Argument>' that can't
11565 be fully created right now, because
11566 `Internal<Template>' not being complete, the Bind
11567 template cannot be looked up in that context. So
11568 we need to "store" `Bind<Argument>' for later
11569 when the context of Bind becomes complete. Let's
11570 store that in a TYPENAME_TYPE. */
11571 return make_typename_type (TYPE_CONTEXT (arg
),
11572 build_nt (TEMPLATE_ID_EXPR
,
11573 TYPE_IDENTIFIER (arg
),
11578 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11579 are resolving nested-types in the signature of a
11580 member function templates. Otherwise ARG is a
11581 TEMPLATE_DECL and is the real template to be
11583 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
11584 arg
= TYPE_NAME (arg
);
11586 r
= lookup_template_class (arg
,
11588 DECL_CONTEXT (arg
),
11589 /*entering_scope=*/0,
11591 return cp_build_qualified_type_real
11592 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
11595 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11596 return convert_from_reference (unshare_expr (arg
));
11600 /* This can happen during the attempted tsubst'ing in
11601 unify. This means that we don't yet have any information
11602 about the template parameter in question. */
11605 /* Early in template argument deduction substitution, we don't
11606 want to reduce the level of 'auto', or it will be confused
11607 with a normal template parm in subsequent deduction. */
11608 if (is_auto (t
) && (complain
& tf_partial
))
11611 /* If we get here, we must have been looking at a parm for a
11612 more deeply nested template. Make a new version of this
11613 template parameter, but with a lower level. */
11616 case TEMPLATE_TYPE_PARM
:
11617 case TEMPLATE_TEMPLATE_PARM
:
11618 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11619 if (cp_type_quals (t
))
11621 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
11622 r
= cp_build_qualified_type_real
11623 (r
, cp_type_quals (t
),
11624 complain
| (code
== TEMPLATE_TYPE_PARM
11625 ? tf_ignore_bad_quals
: 0));
11630 TEMPLATE_TYPE_PARM_INDEX (r
)
11631 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
11632 r
, levels
, args
, complain
);
11633 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
11634 TYPE_MAIN_VARIANT (r
) = r
;
11635 TYPE_POINTER_TO (r
) = NULL_TREE
;
11636 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
11638 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
11639 /* We have reduced the level of the template
11640 template parameter, but not the levels of its
11641 template parameters, so canonical_type_parameter
11642 will not be able to find the canonical template
11643 template parameter for this level. Thus, we
11644 require structural equality checking to compare
11645 TEMPLATE_TEMPLATE_PARMs. */
11646 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11647 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
11648 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11650 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
11652 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11654 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
11655 complain
, in_decl
);
11656 if (argvec
== error_mark_node
)
11657 return error_mark_node
;
11659 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
11660 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
11665 case TEMPLATE_PARM_INDEX
:
11666 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
11670 gcc_unreachable ();
11678 tree purpose
, value
, chain
;
11680 if (t
== void_list_node
)
11683 purpose
= TREE_PURPOSE (t
);
11686 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
11687 if (purpose
== error_mark_node
)
11688 return error_mark_node
;
11690 value
= TREE_VALUE (t
);
11693 value
= tsubst (value
, args
, complain
, in_decl
);
11694 if (value
== error_mark_node
)
11695 return error_mark_node
;
11697 chain
= TREE_CHAIN (t
);
11698 if (chain
&& chain
!= void_type_node
)
11700 chain
= tsubst (chain
, args
, complain
, in_decl
);
11701 if (chain
== error_mark_node
)
11702 return error_mark_node
;
11704 if (purpose
== TREE_PURPOSE (t
)
11705 && value
== TREE_VALUE (t
)
11706 && chain
== TREE_CHAIN (t
))
11708 return hash_tree_cons (purpose
, value
, chain
);
11712 /* We should never be tsubsting a binfo. */
11713 gcc_unreachable ();
11716 /* A vector of template arguments. */
11717 gcc_assert (!type
);
11718 return tsubst_template_args (t
, args
, complain
, in_decl
);
11721 case REFERENCE_TYPE
:
11723 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
11728 Type deduction may fail for any of the following
11731 -- Attempting to create a pointer to reference type.
11732 -- Attempting to create a reference to a reference type or
11733 a reference to void.
11735 Core issue 106 says that creating a reference to a reference
11736 during instantiation is no longer a cause for failure. We
11737 only enforce this check in strict C++98 mode. */
11738 if ((TREE_CODE (type
) == REFERENCE_TYPE
11739 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
11740 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
11742 static location_t last_loc
;
11744 /* We keep track of the last time we issued this error
11745 message to avoid spewing a ton of messages during a
11746 single bad template instantiation. */
11747 if (complain
& tf_error
11748 && last_loc
!= input_location
)
11750 if (VOID_TYPE_P (type
))
11751 error ("forming reference to void");
11752 else if (code
== POINTER_TYPE
)
11753 error ("forming pointer to reference type %qT", type
);
11755 error ("forming reference to reference type %qT", type
);
11756 last_loc
= input_location
;
11759 return error_mark_node
;
11761 else if (TREE_CODE (type
) == FUNCTION_TYPE
11762 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
11763 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
11765 if (complain
& tf_error
)
11767 if (code
== POINTER_TYPE
)
11768 error ("forming pointer to qualified function type %qT",
11771 error ("forming reference to qualified function type %qT",
11774 return error_mark_node
;
11776 else if (code
== POINTER_TYPE
)
11778 r
= build_pointer_type (type
);
11779 if (TREE_CODE (type
) == METHOD_TYPE
)
11780 r
= build_ptrmemfunc_type (r
);
11782 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
11783 /* In C++0x, during template argument substitution, when there is an
11784 attempt to create a reference to a reference type, reference
11785 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11787 "If a template-argument for a template-parameter T names a type
11788 that is a reference to a type A, an attempt to create the type
11789 'lvalue reference to cv T' creates the type 'lvalue reference to
11790 A,' while an attempt to create the type type rvalue reference to
11791 cv T' creates the type T"
11793 r
= cp_build_reference_type
11795 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
11797 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
11798 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11800 if (cxx_dialect
>= cxx1y
11801 && !(TREE_CODE (t
) == REFERENCE_TYPE
&& REFERENCE_VLA_OK (t
))
11802 && array_of_runtime_bound_p (type
))
11804 if (complain
& tf_warning_or_error
)
11806 (input_location
, OPT_Wvla
,
11807 code
== REFERENCE_TYPE
11808 ? G_("cannot declare reference to array of runtime bound")
11809 : G_("cannot declare pointer to array of runtime bound"));
11811 r
= error_mark_node
;
11814 if (r
!= error_mark_node
)
11815 /* Will this ever be needed for TYPE_..._TO values? */
11822 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
11823 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
11827 Type deduction may fail for any of the following
11830 -- Attempting to create "pointer to member of T" when T
11831 is not a class type. */
11832 if (complain
& tf_error
)
11833 error ("creating pointer to member of non-class type %qT", r
);
11834 return error_mark_node
;
11836 if (TREE_CODE (type
) == REFERENCE_TYPE
)
11838 if (complain
& tf_error
)
11839 error ("creating pointer to member reference type %qT", type
);
11840 return error_mark_node
;
11842 if (VOID_TYPE_P (type
))
11844 if (complain
& tf_error
)
11845 error ("creating pointer to member of type void");
11846 return error_mark_node
;
11848 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
11849 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11851 /* The type of the implicit object parameter gets its
11852 cv-qualifiers from the FUNCTION_TYPE. */
11855 = build_memfn_type (type
, r
, type_memfn_quals (type
),
11856 type_memfn_rqual (type
));
11857 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
11858 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
11862 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
11866 case FUNCTION_TYPE
:
11871 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
11872 if (fntype
== error_mark_node
)
11873 return error_mark_node
;
11875 /* Substitute the exception specification. */
11876 specs
= tsubst_exception_specification (t
, args
, complain
,
11877 in_decl
, /*defer_ok*/true);
11878 if (specs
== error_mark_node
)
11879 return error_mark_node
;
11881 fntype
= build_exception_variant (fntype
, specs
);
11886 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
11887 if (domain
== error_mark_node
)
11888 return error_mark_node
;
11890 /* As an optimization, we avoid regenerating the array type if
11891 it will obviously be the same as T. */
11892 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
11895 /* These checks should match the ones in grokdeclarator.
11899 The deduction may fail for any of the following reasons:
11901 -- Attempting to create an array with an element type that
11902 is void, a function type, or a reference type, or [DR337]
11903 an abstract class type. */
11904 if (VOID_TYPE_P (type
)
11905 || TREE_CODE (type
) == FUNCTION_TYPE
11906 || TREE_CODE (type
) == REFERENCE_TYPE
)
11908 if (complain
& tf_error
)
11909 error ("creating array of %qT", type
);
11910 return error_mark_node
;
11913 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
11914 return error_mark_node
;
11916 r
= build_cplus_array_type (type
, domain
);
11918 if (TYPE_USER_ALIGN (t
))
11920 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
11921 TYPE_USER_ALIGN (r
) = 1;
11927 case TYPENAME_TYPE
:
11929 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11930 in_decl
, /*entering_scope=*/1);
11931 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
11932 complain
, in_decl
);
11934 if (ctx
== error_mark_node
|| f
== error_mark_node
)
11935 return error_mark_node
;
11937 if (!MAYBE_CLASS_TYPE_P (ctx
))
11939 if (complain
& tf_error
)
11940 error ("%qT is not a class, struct, or union type", ctx
);
11941 return error_mark_node
;
11943 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
11945 /* Normally, make_typename_type does not require that the CTX
11946 have complete type in order to allow things like:
11948 template <class T> struct S { typename S<T>::X Y; };
11950 But, such constructs have already been resolved by this
11951 point, so here CTX really should have complete type, unless
11952 it's a partial instantiation. */
11953 ctx
= complete_type (ctx
);
11954 if (!COMPLETE_TYPE_P (ctx
))
11956 if (complain
& tf_error
)
11957 cxx_incomplete_type_error (NULL_TREE
, ctx
);
11958 return error_mark_node
;
11962 f
= make_typename_type (ctx
, f
, typename_type
,
11963 complain
| tf_keep_type_decl
);
11964 if (f
== error_mark_node
)
11966 if (TREE_CODE (f
) == TYPE_DECL
)
11968 complain
|= tf_ignore_bad_quals
;
11972 if (TREE_CODE (f
) != TYPENAME_TYPE
)
11974 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
11976 if (complain
& tf_error
)
11977 error ("%qT resolves to %qT, which is not an enumeration type",
11980 return error_mark_node
;
11982 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
11984 if (complain
& tf_error
)
11985 error ("%qT resolves to %qT, which is is not a class type",
11988 return error_mark_node
;
11992 return cp_build_qualified_type_real
11993 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
11996 case UNBOUND_CLASS_TEMPLATE
:
11998 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11999 in_decl
, /*entering_scope=*/1);
12000 tree name
= TYPE_IDENTIFIER (t
);
12001 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
12003 if (ctx
== error_mark_node
|| name
== error_mark_node
)
12004 return error_mark_node
;
12007 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
12008 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
12015 ++cp_unevaluated_operand
;
12016 ++c_inhibit_evaluation_warnings
;
12018 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
12020 /*integral_constant_expression_p=*/false);
12022 --cp_unevaluated_operand
;
12023 --c_inhibit_evaluation_warnings
;
12025 type
= finish_typeof (type
);
12026 return cp_build_qualified_type_real (type
,
12028 | cp_type_quals (type
),
12032 case DECLTYPE_TYPE
:
12036 ++cp_unevaluated_operand
;
12037 ++c_inhibit_evaluation_warnings
;
12039 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
12040 complain
|tf_decltype
, in_decl
,
12041 /*function_p*/false,
12042 /*integral_constant_expression*/false);
12044 --cp_unevaluated_operand
;
12045 --c_inhibit_evaluation_warnings
;
12047 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
12048 type
= lambda_capture_field_type (type
,
12049 DECLTYPE_FOR_INIT_CAPTURE (t
));
12050 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
12051 type
= lambda_proxy_type (type
);
12054 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
12055 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
12057 /* In a template ~id could be either a complement expression
12058 or an unqualified-id naming a destructor; if instantiating
12059 it produces an expression, it's not an id-expression or
12062 type
= finish_decltype_type (type
, id
, complain
);
12064 return cp_build_qualified_type_real (type
,
12066 | cp_type_quals (type
),
12070 case UNDERLYING_TYPE
:
12072 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
12073 complain
, in_decl
);
12074 return finish_underlying_type (type
);
12077 case TYPE_ARGUMENT_PACK
:
12078 case NONTYPE_ARGUMENT_PACK
:
12080 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
12082 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
12086 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
12088 /* For template nontype argument packs, also substitute into
12090 if (code
== NONTYPE_ARGUMENT_PACK
)
12091 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12109 /* We should use one of the expression tsubsts for these codes. */
12110 gcc_unreachable ();
12113 sorry ("use of %qs in template", get_tree_code_name (code
));
12114 return error_mark_node
;
12118 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12119 type of the expression on the left-hand side of the "." or "->"
12123 tsubst_baselink (tree baselink
, tree object_type
,
12124 tree args
, tsubst_flags_t complain
, tree in_decl
)
12127 tree qualifying_scope
;
12130 tree template_args
= 0;
12131 bool template_id_p
= false;
12132 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
12134 /* A baselink indicates a function from a base class. Both the
12135 BASELINK_ACCESS_BINFO and the base class referenced may
12136 indicate bases of the template class, rather than the
12137 instantiated class. In addition, lookups that were not
12138 ambiguous before may be ambiguous now. Therefore, we perform
12139 the lookup again. */
12140 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
12141 qualifying_scope
= tsubst (qualifying_scope
, args
,
12142 complain
, in_decl
);
12143 fns
= BASELINK_FUNCTIONS (baselink
);
12144 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
12145 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
12147 template_id_p
= true;
12148 template_args
= TREE_OPERAND (fns
, 1);
12149 fns
= TREE_OPERAND (fns
, 0);
12151 template_args
= tsubst_template_args (template_args
, args
,
12152 complain
, in_decl
);
12154 name
= DECL_NAME (get_first_fn (fns
));
12155 if (IDENTIFIER_TYPENAME_P (name
))
12156 name
= mangle_conv_op_name_for_type (optype
);
12157 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
12159 return error_mark_node
;
12161 /* If lookup found a single function, mark it as used at this
12162 point. (If it lookup found multiple functions the one selected
12163 later by overload resolution will be marked as used at that
12165 if (BASELINK_P (baselink
))
12166 fns
= BASELINK_FUNCTIONS (baselink
);
12167 if (!template_id_p
&& !really_overloaded_fn (fns
))
12168 mark_used (OVL_CURRENT (fns
));
12170 /* Add back the template arguments, if present. */
12171 if (BASELINK_P (baselink
) && template_id_p
)
12172 BASELINK_FUNCTIONS (baselink
)
12173 = build_nt (TEMPLATE_ID_EXPR
,
12174 BASELINK_FUNCTIONS (baselink
),
12176 /* Update the conversion operator type. */
12177 BASELINK_OPTYPE (baselink
) = optype
;
12180 object_type
= current_class_type
;
12183 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
12189 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12190 true if the qualified-id will be a postfix-expression in-and-of
12191 itself; false if more of the postfix-expression follows the
12192 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12196 tsubst_qualified_id (tree qualified_id
, tree args
,
12197 tsubst_flags_t complain
, tree in_decl
,
12198 bool done
, bool address_p
)
12204 tree template_args
;
12205 location_t loc
= UNKNOWN_LOCATION
;
12207 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
12209 /* Figure out what name to look up. */
12210 name
= TREE_OPERAND (qualified_id
, 1);
12211 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
12213 is_template
= true;
12214 loc
= EXPR_LOCATION (name
);
12215 template_args
= TREE_OPERAND (name
, 1);
12217 template_args
= tsubst_template_args (template_args
, args
,
12218 complain
, in_decl
);
12219 name
= TREE_OPERAND (name
, 0);
12223 is_template
= false;
12224 template_args
= NULL_TREE
;
12227 /* Substitute into the qualifying scope. When there are no ARGS, we
12228 are just trying to simplify a non-dependent expression. In that
12229 case the qualifying scope may be dependent, and, in any case,
12230 substituting will not help. */
12231 scope
= TREE_OPERAND (qualified_id
, 0);
12234 scope
= tsubst (scope
, args
, complain
, in_decl
);
12235 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
12240 if (dependent_scope_p (scope
))
12243 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
12244 return build_qualified_name (NULL_TREE
, scope
, expr
,
12245 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
12248 if (!BASELINK_P (name
) && !DECL_P (expr
))
12250 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
12252 /* A BIT_NOT_EXPR is used to represent a destructor. */
12253 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
12255 error ("qualifying type %qT does not match destructor name ~%qT",
12256 scope
, TREE_OPERAND (expr
, 0));
12257 expr
= error_mark_node
;
12260 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
12261 /*is_type_p=*/0, false);
12264 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
12265 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
12266 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
12268 if (complain
& tf_error
)
12270 error ("dependent-name %qE is parsed as a non-type, but "
12271 "instantiation yields a type", qualified_id
);
12272 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
12274 return error_mark_node
;
12280 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
12282 /* Remember that there was a reference to this entity. */
12286 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
12288 if (complain
& tf_error
)
12289 qualified_name_lookup_error (scope
,
12290 TREE_OPERAND (qualified_id
, 1),
12291 expr
, input_location
);
12292 return error_mark_node
;
12296 expr
= lookup_template_function (expr
, template_args
);
12298 if (expr
== error_mark_node
&& complain
& tf_error
)
12299 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
12300 expr
, input_location
);
12301 else if (TYPE_P (scope
))
12303 expr
= (adjust_result_of_qualified_name_lookup
12304 (expr
, scope
, current_nonlambda_class_type ()));
12305 expr
= (finish_qualified_id_expr
12306 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
12307 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
12308 /*template_arg_p=*/false, complain
));
12311 /* Expressions do not generally have reference type. */
12312 if (TREE_CODE (expr
) != SCOPE_REF
12313 /* However, if we're about to form a pointer-to-member, we just
12314 want the referenced member referenced. */
12315 && TREE_CODE (expr
) != OFFSET_REF
)
12316 expr
= convert_from_reference (expr
);
12321 /* Like tsubst, but deals with expressions. This function just replaces
12322 template parms; to finish processing the resultant expression, use
12323 tsubst_copy_and_build or tsubst_expr. */
12326 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12328 enum tree_code code
;
12331 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
12334 code
= TREE_CODE (t
);
12339 r
= retrieve_local_specialization (t
);
12341 if (r
== NULL_TREE
)
12343 /* We get here for a use of 'this' in an NSDMI. */
12344 if (DECL_NAME (t
) == this_identifier
12345 && at_function_scope_p ()
12346 && DECL_CONSTRUCTOR_P (current_function_decl
))
12347 return current_class_ptr
;
12349 /* This can happen for a parameter name used later in a function
12350 declaration (such as in a late-specified return type). Just
12351 make a dummy decl, since it's only used for its type. */
12352 gcc_assert (cp_unevaluated_operand
!= 0);
12353 r
= tsubst_decl (t
, args
, complain
);
12354 /* Give it the template pattern as its context; its true context
12355 hasn't been instantiated yet and this is good enough for
12357 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
12360 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12361 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12370 if (DECL_TEMPLATE_PARM_P (t
))
12371 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
12372 /* There is no need to substitute into namespace-scope
12374 if (DECL_NAMESPACE_SCOPE_P (t
))
12376 /* If ARGS is NULL, then T is known to be non-dependent. */
12377 if (args
== NULL_TREE
)
12378 return integral_constant_value (t
);
12380 /* Unfortunately, we cannot just call lookup_name here.
12383 template <int I> int f() {
12385 struct S { void g() { E e = a; } };
12388 When we instantiate f<7>::S::g(), say, lookup_name is not
12389 clever enough to find f<7>::a. */
12391 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12392 /*entering_scope=*/0);
12394 for (v
= TYPE_VALUES (enum_type
);
12396 v
= TREE_CHAIN (v
))
12397 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
12398 return TREE_VALUE (v
);
12400 /* We didn't find the name. That should never happen; if
12401 name-lookup found it during preliminary parsing, we
12402 should find it again here during instantiation. */
12403 gcc_unreachable ();
12408 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
12410 /* Check for a local specialization set up by
12411 tsubst_pack_expansion. */
12412 tree r
= retrieve_local_specialization (t
);
12415 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12416 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12420 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12421 tsubst_decl put in the hash table. */
12422 return retrieve_specialization (t
, args
, 0);
12425 if (DECL_CONTEXT (t
))
12429 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12430 /*entering_scope=*/1);
12431 if (ctx
!= DECL_CONTEXT (t
))
12433 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
12436 if (complain
& tf_error
)
12437 error ("using invalid field %qD", t
);
12438 return error_mark_node
;
12447 case FUNCTION_DECL
:
12448 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
12449 r
= tsubst (t
, args
, complain
, in_decl
);
12450 else if (local_variable_p (t
))
12452 r
= retrieve_local_specialization (t
);
12453 if (r
== NULL_TREE
)
12455 if (DECL_ANON_UNION_VAR_P (t
))
12457 /* Just use name lookup to find a member alias for an
12458 anonymous union, but then add it to the hash table. */
12459 r
= lookup_name (DECL_NAME (t
));
12460 gcc_assert (DECL_ANON_UNION_VAR_P (r
));
12461 register_local_specialization (r
, t
);
12465 gcc_assert (errorcount
|| sorrycount
);
12466 return error_mark_node
;
12475 case NAMESPACE_DECL
:
12479 /* An OVERLOAD will always be a non-dependent overload set; an
12480 overload set from function scope will just be represented with an
12481 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12482 gcc_assert (!uses_template_parms (t
));
12486 return tsubst_baselink (t
, current_nonlambda_class_type (),
12487 args
, complain
, in_decl
);
12489 case TEMPLATE_DECL
:
12490 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12491 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
12492 args
, complain
, in_decl
);
12493 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
12494 return tsubst (t
, args
, complain
, in_decl
);
12495 else if (DECL_CLASS_SCOPE_P (t
)
12496 && uses_template_parms (DECL_CONTEXT (t
)))
12498 /* Template template argument like the following example need
12501 template <template <class> class TT> struct C {};
12502 template <class T> struct D {
12503 template <class U> struct E {};
12508 We are processing the template argument `E' in #1 for
12509 the template instantiation #2. Originally, `E' is a
12510 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12511 have to substitute this with one having context `D<int>'. */
12513 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
12514 return lookup_field (context
, DECL_NAME(t
), 0, false);
12517 /* Ordinary template template argument. */
12521 case REINTERPRET_CAST_EXPR
:
12522 case CONST_CAST_EXPR
:
12523 case STATIC_CAST_EXPR
:
12524 case DYNAMIC_CAST_EXPR
:
12525 case IMPLICIT_CONV_EXPR
:
12529 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12530 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12533 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
12536 tree expanded
, op
= TREE_OPERAND (t
, 0);
12539 if (SIZEOF_EXPR_TYPE_P (t
))
12540 op
= TREE_TYPE (op
);
12542 ++cp_unevaluated_operand
;
12543 ++c_inhibit_evaluation_warnings
;
12544 /* We only want to compute the number of arguments. */
12545 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
12546 --cp_unevaluated_operand
;
12547 --c_inhibit_evaluation_warnings
;
12549 if (TREE_CODE (expanded
) == TREE_VEC
)
12550 len
= TREE_VEC_LENGTH (expanded
);
12552 if (expanded
== error_mark_node
)
12553 return error_mark_node
;
12554 else if (PACK_EXPANSION_P (expanded
)
12555 || (TREE_CODE (expanded
) == TREE_VEC
12557 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
12559 if (TREE_CODE (expanded
) == TREE_VEC
)
12560 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
12562 if (TYPE_P (expanded
))
12563 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
12564 complain
& tf_error
);
12566 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
12567 complain
& tf_error
);
12570 return build_int_cst (size_type_node
, len
);
12572 if (SIZEOF_EXPR_TYPE_P (t
))
12574 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
12575 args
, complain
, in_decl
);
12576 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
12577 r
= build1 (SIZEOF_EXPR
,
12578 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
12579 SIZEOF_EXPR_TYPE_P (r
) = 1;
12586 case TRUTH_NOT_EXPR
:
12589 case UNARY_PLUS_EXPR
: /* Unary + */
12591 case AT_ENCODE_EXPR
:
12595 case REALPART_EXPR
:
12596 case IMAGPART_EXPR
:
12599 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12600 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12602 case COMPONENT_REF
:
12607 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
12608 name
= TREE_OPERAND (t
, 1);
12609 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
12611 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12612 complain
, in_decl
);
12613 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12615 else if (TREE_CODE (name
) == SCOPE_REF
12616 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
12618 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12619 complain
, in_decl
);
12620 name
= TREE_OPERAND (name
, 1);
12621 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12622 complain
, in_decl
);
12623 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12624 name
= build_qualified_name (/*type=*/NULL_TREE
,
12626 /*template_p=*/false);
12628 else if (BASELINK_P (name
))
12629 name
= tsubst_baselink (name
,
12630 non_reference (TREE_TYPE (object
)),
12634 name
= tsubst_copy (name
, args
, complain
, in_decl
);
12635 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
12641 case TRUNC_DIV_EXPR
:
12642 case CEIL_DIV_EXPR
:
12643 case FLOOR_DIV_EXPR
:
12644 case ROUND_DIV_EXPR
:
12645 case EXACT_DIV_EXPR
:
12649 case TRUNC_MOD_EXPR
:
12650 case FLOOR_MOD_EXPR
:
12651 case TRUTH_ANDIF_EXPR
:
12652 case TRUTH_ORIF_EXPR
:
12653 case TRUTH_AND_EXPR
:
12654 case TRUTH_OR_EXPR
:
12667 case COMPOUND_EXPR
:
12670 case PREDECREMENT_EXPR
:
12671 case PREINCREMENT_EXPR
:
12672 case POSTDECREMENT_EXPR
:
12673 case POSTINCREMENT_EXPR
:
12675 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12676 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12679 return build_qualified_name (/*type=*/NULL_TREE
,
12680 tsubst_copy (TREE_OPERAND (t
, 0),
12681 args
, complain
, in_decl
),
12682 tsubst_copy (TREE_OPERAND (t
, 1),
12683 args
, complain
, in_decl
),
12684 QUALIFIED_NAME_IS_TEMPLATE (t
));
12689 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12690 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12691 NULL_TREE
, NULL_TREE
);
12695 int n
= VL_EXP_OPERAND_LENGTH (t
);
12696 tree result
= build_vl_exp (CALL_EXPR
, n
);
12698 for (i
= 0; i
< n
; i
++)
12699 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
12700 complain
, in_decl
);
12706 case PSEUDO_DTOR_EXPR
:
12707 case VEC_PERM_EXPR
:
12710 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12711 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12712 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12713 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12720 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12721 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12722 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12723 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
12730 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12731 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12732 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
12733 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
12737 case TEMPLATE_ID_EXPR
:
12739 /* Substituted template arguments */
12740 tree fn
= TREE_OPERAND (t
, 0);
12741 tree targs
= TREE_OPERAND (t
, 1);
12743 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
12745 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12747 return lookup_template_function (fn
, targs
);
12752 tree purpose
, value
, chain
;
12754 if (t
== void_list_node
)
12757 purpose
= TREE_PURPOSE (t
);
12759 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
12760 value
= TREE_VALUE (t
);
12762 value
= tsubst_copy (value
, args
, complain
, in_decl
);
12763 chain
= TREE_CHAIN (t
);
12764 if (chain
&& chain
!= void_type_node
)
12765 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
12766 if (purpose
== TREE_PURPOSE (t
)
12767 && value
== TREE_VALUE (t
)
12768 && chain
== TREE_CHAIN (t
))
12770 return tree_cons (purpose
, value
, chain
);
12775 case ENUMERAL_TYPE
:
12777 case TEMPLATE_TYPE_PARM
:
12778 case TEMPLATE_TEMPLATE_PARM
:
12779 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12780 case TEMPLATE_PARM_INDEX
:
12782 case REFERENCE_TYPE
:
12784 case FUNCTION_TYPE
:
12787 case TYPENAME_TYPE
:
12788 case UNBOUND_CLASS_TEMPLATE
:
12790 case DECLTYPE_TYPE
:
12792 return tsubst (t
, args
, complain
, in_decl
);
12796 /* Fall through. */
12797 case IDENTIFIER_NODE
:
12798 if (IDENTIFIER_TYPENAME_P (t
))
12800 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12801 return mangle_conv_op_name_for_type (new_type
);
12807 /* This is handled by tsubst_copy_and_build. */
12808 gcc_unreachable ();
12811 return build_x_va_arg (EXPR_LOCATION (t
),
12812 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
12814 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
12816 case CLEANUP_POINT_EXPR
:
12817 /* We shouldn't have built any of these during initial template
12818 generation. Instead, they should be built during instantiation
12819 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12820 gcc_unreachable ();
12824 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12825 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12826 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12827 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
12828 mark_used (TREE_OPERAND (r
, 1));
12831 case EXPR_PACK_EXPANSION
:
12832 error ("invalid use of pack expansion expression");
12833 return error_mark_node
;
12835 case NONTYPE_ARGUMENT_PACK
:
12836 error ("use %<...%> to expand argument pack");
12837 return error_mark_node
;
12844 /* Instantiate any typedefs in the type. */
12845 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12846 r
= fold_convert (type
, t
);
12847 gcc_assert (TREE_CODE (r
) == code
);
12852 /* These can sometimes show up in a partial instantiation, but never
12853 involve template parms. */
12854 gcc_assert (!uses_template_parms (t
));
12858 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12859 gcc_checking_assert (false);
12864 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12867 tsubst_omp_clauses (tree clauses
, bool declare_simd
,
12868 tree args
, tsubst_flags_t complain
, tree in_decl
)
12870 tree new_clauses
= NULL
, nc
, oc
;
12872 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
12874 nc
= copy_node (oc
);
12875 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
12878 switch (OMP_CLAUSE_CODE (nc
))
12880 case OMP_CLAUSE_LASTPRIVATE
:
12881 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
12883 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
12884 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
12885 in_decl
, /*integral_constant_expression_p=*/false);
12886 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
12887 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
12890 case OMP_CLAUSE_PRIVATE
:
12891 case OMP_CLAUSE_SHARED
:
12892 case OMP_CLAUSE_FIRSTPRIVATE
:
12893 case OMP_CLAUSE_COPYIN
:
12894 case OMP_CLAUSE_COPYPRIVATE
:
12895 case OMP_CLAUSE_IF
:
12896 case OMP_CLAUSE_NUM_THREADS
:
12897 case OMP_CLAUSE_SCHEDULE
:
12898 case OMP_CLAUSE_COLLAPSE
:
12899 case OMP_CLAUSE_FINAL
:
12900 case OMP_CLAUSE_DEPEND
:
12901 case OMP_CLAUSE_FROM
:
12902 case OMP_CLAUSE_TO
:
12903 case OMP_CLAUSE_UNIFORM
:
12904 case OMP_CLAUSE_MAP
:
12905 case OMP_CLAUSE_DEVICE
:
12906 case OMP_CLAUSE_DIST_SCHEDULE
:
12907 case OMP_CLAUSE_NUM_TEAMS
:
12908 case OMP_CLAUSE_THREAD_LIMIT
:
12909 case OMP_CLAUSE_SAFELEN
:
12910 case OMP_CLAUSE_SIMDLEN
:
12911 OMP_CLAUSE_OPERAND (nc
, 0)
12912 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12913 in_decl
, /*integral_constant_expression_p=*/false);
12915 case OMP_CLAUSE_REDUCTION
:
12916 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
12918 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
12919 if (TREE_CODE (placeholder
) == SCOPE_REF
)
12921 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
12922 complain
, in_decl
);
12923 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
12924 = build_qualified_name (NULL_TREE
, scope
,
12925 TREE_OPERAND (placeholder
, 1),
12929 gcc_assert (identifier_p (placeholder
));
12931 OMP_CLAUSE_OPERAND (nc
, 0)
12932 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12933 in_decl
, /*integral_constant_expression_p=*/false);
12935 case OMP_CLAUSE_LINEAR
:
12936 case OMP_CLAUSE_ALIGNED
:
12937 OMP_CLAUSE_OPERAND (nc
, 0)
12938 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12939 in_decl
, /*integral_constant_expression_p=*/false);
12940 OMP_CLAUSE_OPERAND (nc
, 1)
12941 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
12942 in_decl
, /*integral_constant_expression_p=*/false);
12945 case OMP_CLAUSE_NOWAIT
:
12946 case OMP_CLAUSE_ORDERED
:
12947 case OMP_CLAUSE_DEFAULT
:
12948 case OMP_CLAUSE_UNTIED
:
12949 case OMP_CLAUSE_MERGEABLE
:
12950 case OMP_CLAUSE_INBRANCH
:
12951 case OMP_CLAUSE_NOTINBRANCH
:
12952 case OMP_CLAUSE_PROC_BIND
:
12953 case OMP_CLAUSE_FOR
:
12954 case OMP_CLAUSE_PARALLEL
:
12955 case OMP_CLAUSE_SECTIONS
:
12956 case OMP_CLAUSE_TASKGROUP
:
12959 gcc_unreachable ();
12963 new_clauses
= nreverse (new_clauses
);
12965 new_clauses
= finish_omp_clauses (new_clauses
);
12966 return new_clauses
;
12969 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12972 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
12975 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12977 tree purpose
, value
, chain
;
12982 if (TREE_CODE (t
) != TREE_LIST
)
12983 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12984 /*function_p=*/false,
12985 /*integral_constant_expression_p=*/false);
12987 if (t
== void_list_node
)
12990 purpose
= TREE_PURPOSE (t
);
12992 purpose
= RECUR (purpose
);
12993 value
= TREE_VALUE (t
);
12996 if (TREE_CODE (value
) != LABEL_DECL
)
12997 value
= RECUR (value
);
13000 value
= lookup_label (DECL_NAME (value
));
13001 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
13002 TREE_USED (value
) = 1;
13005 chain
= TREE_CHAIN (t
);
13006 if (chain
&& chain
!= void_type_node
)
13007 chain
= RECUR (chain
);
13008 return tree_cons (purpose
, value
, chain
);
13012 /* Substitute one OMP_FOR iterator. */
13015 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
13016 tree condv
, tree incrv
, tree
*clauses
,
13017 tree args
, tsubst_flags_t complain
, tree in_decl
,
13018 bool integral_constant_expression_p
)
13020 #define RECUR(NODE) \
13021 tsubst_expr ((NODE), args, complain, in_decl, \
13022 integral_constant_expression_p)
13023 tree decl
, init
, cond
, incr
;
13026 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
13027 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
13028 decl
= TREE_OPERAND (init
, 0);
13029 init
= TREE_OPERAND (init
, 1);
13030 /* Do this before substituting into decl to handle 'auto'. */
13031 init_decl
= (init
&& TREE_CODE (init
) == DECL_EXPR
);
13032 init
= RECUR (init
);
13033 decl
= RECUR (decl
);
13036 gcc_assert (!processing_template_decl
);
13037 init
= DECL_INITIAL (decl
);
13038 DECL_INITIAL (decl
) = NULL_TREE
;
13041 gcc_assert (!type_dependent_expression_p (decl
));
13043 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
13045 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
13046 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
13047 if (TREE_CODE (incr
) == MODIFY_EXPR
)
13048 incr
= build_x_modify_expr (EXPR_LOCATION (incr
),
13049 RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
13050 RECUR (TREE_OPERAND (incr
, 1)),
13053 incr
= RECUR (incr
);
13054 TREE_VEC_ELT (declv
, i
) = decl
;
13055 TREE_VEC_ELT (initv
, i
) = init
;
13056 TREE_VEC_ELT (condv
, i
) = cond
;
13057 TREE_VEC_ELT (incrv
, i
) = incr
;
13061 if (init
&& !init_decl
)
13064 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
13066 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
13067 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
13068 && OMP_CLAUSE_DECL (c
) == decl
)
13070 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
13071 && OMP_CLAUSE_DECL (c
) == decl
)
13072 error ("iteration variable %qD should not be firstprivate", decl
);
13073 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
13074 && OMP_CLAUSE_DECL (c
) == decl
)
13075 error ("iteration variable %qD should not be reduction", decl
);
13079 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
13080 OMP_CLAUSE_DECL (c
) = decl
;
13081 c
= finish_omp_clauses (c
);
13084 OMP_CLAUSE_CHAIN (c
) = *clauses
;
13089 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
13090 if (COMPARISON_CLASS_P (cond
))
13091 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
13092 RECUR (TREE_OPERAND (cond
, 0)),
13093 RECUR (TREE_OPERAND (cond
, 1)));
13095 cond
= RECUR (cond
);
13096 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
13097 switch (TREE_CODE (incr
))
13099 case PREINCREMENT_EXPR
:
13100 case PREDECREMENT_EXPR
:
13101 case POSTINCREMENT_EXPR
:
13102 case POSTDECREMENT_EXPR
:
13103 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
13104 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
13107 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
13108 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
13110 tree rhs
= TREE_OPERAND (incr
, 1);
13111 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
13112 RECUR (TREE_OPERAND (incr
, 0)),
13113 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
13114 RECUR (TREE_OPERAND (rhs
, 0)),
13115 RECUR (TREE_OPERAND (rhs
, 1))));
13118 incr
= RECUR (incr
);
13121 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
13122 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
13124 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
13125 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
13126 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
13127 TREE_TYPE (decl
), lhs
,
13128 RECUR (TREE_OPERAND (incr
, 2))));
13130 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
13131 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
13132 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
13134 tree rhs
= TREE_OPERAND (incr
, 2);
13135 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
13136 RECUR (TREE_OPERAND (incr
, 0)),
13137 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
13138 RECUR (TREE_OPERAND (rhs
, 0)),
13139 RECUR (TREE_OPERAND (rhs
, 1))));
13142 incr
= RECUR (incr
);
13145 incr
= RECUR (incr
);
13149 TREE_VEC_ELT (declv
, i
) = decl
;
13150 TREE_VEC_ELT (initv
, i
) = init
;
13151 TREE_VEC_ELT (condv
, i
) = cond
;
13152 TREE_VEC_ELT (incrv
, i
) = incr
;
13156 /* Like tsubst_copy for expressions, etc. but also does semantic
13160 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
13161 bool integral_constant_expression_p
)
13163 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13164 #define RECUR(NODE) \
13165 tsubst_expr ((NODE), args, complain, in_decl, \
13166 integral_constant_expression_p)
13172 if (t
== NULL_TREE
|| t
== error_mark_node
)
13175 loc
= input_location
;
13176 if (EXPR_HAS_LOCATION (t
))
13177 input_location
= EXPR_LOCATION (t
);
13178 if (STATEMENT_CODE_P (TREE_CODE (t
)))
13179 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
13181 switch (TREE_CODE (t
))
13183 case STATEMENT_LIST
:
13185 tree_stmt_iterator i
;
13186 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
13187 RECUR (tsi_stmt (i
));
13191 case CTOR_INITIALIZER
:
13192 finish_mem_initializers (tsubst_initializer_list
13193 (TREE_OPERAND (t
, 0), args
));
13197 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
13201 tmp
= RECUR (EXPR_STMT_EXPR (t
));
13202 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
13203 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
13205 finish_expr_stmt (tmp
);
13209 do_using_directive (USING_STMT_NAMESPACE (t
));
13214 tree decl
, pattern_decl
;
13217 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
13218 if (TREE_CODE (decl
) == LABEL_DECL
)
13219 finish_label_decl (DECL_NAME (decl
));
13220 else if (TREE_CODE (decl
) == USING_DECL
)
13222 tree scope
= USING_DECL_SCOPE (decl
);
13223 tree name
= DECL_NAME (decl
);
13226 scope
= tsubst (scope
, args
, complain
, in_decl
);
13227 decl
= lookup_qualified_name (scope
, name
,
13228 /*is_type_p=*/false,
13229 /*complain=*/false);
13230 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
13231 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
13233 do_local_using_decl (decl
, scope
, name
);
13235 else if (DECL_PACK_P (decl
))
13237 /* Don't build up decls for a variadic capture proxy, we'll
13238 instantiate the elements directly as needed. */
13243 init
= DECL_INITIAL (decl
);
13244 decl
= tsubst (decl
, args
, complain
, in_decl
);
13245 if (decl
!= error_mark_node
)
13247 /* By marking the declaration as instantiated, we avoid
13248 trying to instantiate it. Since instantiate_decl can't
13249 handle local variables, and since we've already done
13250 all that needs to be done, that's the right thing to
13253 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13255 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
13256 /* Anonymous aggregates are a special case. */
13257 finish_anon_union (decl
);
13258 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
13260 DECL_CONTEXT (decl
) = current_function_decl
;
13261 if (DECL_NAME (decl
) == this_identifier
)
13263 tree lam
= DECL_CONTEXT (current_function_decl
);
13264 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
13265 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
13267 insert_capture_proxy (decl
);
13269 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
13270 /* We already did a pushtag. */;
13271 else if (TREE_CODE (decl
) == FUNCTION_DECL
13272 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
13273 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
13275 DECL_CONTEXT (decl
) = NULL_TREE
;
13277 DECL_CONTEXT (decl
) = current_function_decl
;
13278 cp_check_omp_declare_reduction (decl
);
13282 int const_init
= false;
13283 maybe_push_decl (decl
);
13285 && DECL_PRETTY_FUNCTION_P (decl
))
13287 /* For __PRETTY_FUNCTION__ we have to adjust the
13289 const char *const name
13290 = cxx_printable_name (current_function_decl
, 2);
13291 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
13295 tree t
= RECUR (init
);
13299 /* If we had an initializer but it
13300 instantiated to nothing,
13301 value-initialize the object. This will
13302 only occur when the initializer was a
13303 pack expansion where the parameter packs
13304 used in that expansion were of length
13306 init
= build_value_init (TREE_TYPE (decl
),
13308 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
13309 init
= get_target_expr_sfinae (init
, complain
);
13316 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13318 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
13327 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
13328 RECUR (FOR_INIT_STMT (t
));
13329 finish_for_init_stmt (stmt
);
13330 tmp
= RECUR (FOR_COND (t
));
13331 finish_for_cond (tmp
, stmt
, false);
13332 tmp
= RECUR (FOR_EXPR (t
));
13333 finish_for_expr (tmp
, stmt
);
13334 RECUR (FOR_BODY (t
));
13335 finish_for_stmt (stmt
);
13338 case RANGE_FOR_STMT
:
13341 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
13342 decl
= RANGE_FOR_DECL (t
);
13343 decl
= tsubst (decl
, args
, complain
, in_decl
);
13344 maybe_push_decl (decl
);
13345 expr
= RECUR (RANGE_FOR_EXPR (t
));
13346 stmt
= cp_convert_range_for (stmt
, decl
, expr
, RANGE_FOR_IVDEP (t
));
13347 RECUR (RANGE_FOR_BODY (t
));
13348 finish_for_stmt (stmt
);
13353 stmt
= begin_while_stmt ();
13354 tmp
= RECUR (WHILE_COND (t
));
13355 finish_while_stmt_cond (tmp
, stmt
, false);
13356 RECUR (WHILE_BODY (t
));
13357 finish_while_stmt (stmt
);
13361 stmt
= begin_do_stmt ();
13362 RECUR (DO_BODY (t
));
13363 finish_do_body (stmt
);
13364 tmp
= RECUR (DO_COND (t
));
13365 finish_do_stmt (tmp
, stmt
, false);
13369 stmt
= begin_if_stmt ();
13370 tmp
= RECUR (IF_COND (t
));
13371 finish_if_stmt_cond (tmp
, stmt
);
13372 RECUR (THEN_CLAUSE (t
));
13373 finish_then_clause (stmt
);
13375 if (ELSE_CLAUSE (t
))
13377 begin_else_clause (stmt
);
13378 RECUR (ELSE_CLAUSE (t
));
13379 finish_else_clause (stmt
);
13382 finish_if_stmt (stmt
);
13386 if (BIND_EXPR_BODY_BLOCK (t
))
13387 stmt
= begin_function_body ();
13389 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
13390 ? BCS_TRY_BLOCK
: 0);
13392 RECUR (BIND_EXPR_BODY (t
));
13394 if (BIND_EXPR_BODY_BLOCK (t
))
13395 finish_function_body (stmt
);
13397 finish_compound_stmt (stmt
);
13401 finish_break_stmt ();
13404 case CONTINUE_STMT
:
13405 finish_continue_stmt ();
13409 stmt
= begin_switch_stmt ();
13410 tmp
= RECUR (SWITCH_STMT_COND (t
));
13411 finish_switch_cond (tmp
, stmt
);
13412 RECUR (SWITCH_STMT_BODY (t
));
13413 finish_switch_stmt (stmt
);
13416 case CASE_LABEL_EXPR
:
13417 finish_case_label (EXPR_LOCATION (t
),
13418 RECUR (CASE_LOW (t
)),
13419 RECUR (CASE_HIGH (t
)));
13424 tree decl
= LABEL_EXPR_LABEL (t
);
13427 label
= finish_label_stmt (DECL_NAME (decl
));
13428 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
13429 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
13434 tmp
= GOTO_DESTINATION (t
);
13435 if (TREE_CODE (tmp
) != LABEL_DECL
)
13436 /* Computed goto's must be tsubst'd into. On the other hand,
13437 non-computed gotos must not be; the identifier in question
13438 will have no binding. */
13441 tmp
= DECL_NAME (tmp
);
13442 finish_goto_stmt (tmp
);
13446 tmp
= finish_asm_stmt
13447 (ASM_VOLATILE_P (t
),
13448 RECUR (ASM_STRING (t
)),
13449 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
13450 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
13451 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
13452 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
13454 tree asm_expr
= tmp
;
13455 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
13456 asm_expr
= TREE_OPERAND (asm_expr
, 0);
13457 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
13464 stmt
= begin_try_block ();
13465 RECUR (TRY_STMTS (t
));
13466 finish_cleanup_try_block (stmt
);
13467 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
13471 tree compound_stmt
= NULL_TREE
;
13473 if (FN_TRY_BLOCK_P (t
))
13474 stmt
= begin_function_try_block (&compound_stmt
);
13476 stmt
= begin_try_block ();
13478 RECUR (TRY_STMTS (t
));
13480 if (FN_TRY_BLOCK_P (t
))
13481 finish_function_try_block (stmt
);
13483 finish_try_block (stmt
);
13485 RECUR (TRY_HANDLERS (t
));
13486 if (FN_TRY_BLOCK_P (t
))
13487 finish_function_handler_sequence (stmt
, compound_stmt
);
13489 finish_handler_sequence (stmt
);
13495 tree decl
= HANDLER_PARMS (t
);
13499 decl
= tsubst (decl
, args
, complain
, in_decl
);
13500 /* Prevent instantiate_decl from trying to instantiate
13501 this variable. We've already done all that needs to be
13503 if (decl
!= error_mark_node
)
13504 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13506 stmt
= begin_handler ();
13507 finish_handler_parms (decl
, stmt
);
13508 RECUR (HANDLER_BODY (t
));
13509 finish_handler (stmt
);
13514 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
13515 if (CLASS_TYPE_P (tmp
))
13517 /* Local classes are not independent templates; they are
13518 instantiated along with their containing function. And this
13519 way we don't have to deal with pushing out of one local class
13520 to instantiate a member of another local class. */
13522 /* Closures are handled by the LAMBDA_EXPR. */
13523 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
13524 complete_type (tmp
);
13525 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
13526 if (!DECL_ARTIFICIAL (fn
))
13527 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
13531 case STATIC_ASSERT
:
13535 ++c_inhibit_evaluation_warnings
;
13537 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
13540 /*integral_constant_expression_p=*/true);
13541 --c_inhibit_evaluation_warnings
;
13543 finish_static_assert (condition
,
13544 STATIC_ASSERT_MESSAGE (t
),
13545 STATIC_ASSERT_SOURCE_LOCATION (t
),
13546 /*member_p=*/false);
13551 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), false,
13552 args
, complain
, in_decl
);
13553 stmt
= begin_omp_parallel ();
13554 RECUR (OMP_PARALLEL_BODY (t
));
13555 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
13556 = OMP_PARALLEL_COMBINED (t
);
13560 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), false,
13561 args
, complain
, in_decl
);
13562 stmt
= begin_omp_task ();
13563 RECUR (OMP_TASK_BODY (t
));
13564 finish_omp_task (tmp
, stmt
);
13570 case OMP_DISTRIBUTE
:
13572 tree clauses
, body
, pre_body
;
13573 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
13574 tree incrv
= NULL_TREE
;
13577 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), false,
13578 args
, complain
, in_decl
);
13579 if (OMP_FOR_INIT (t
) != NULL_TREE
)
13581 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13582 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13583 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13584 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13587 stmt
= begin_omp_structured_block ();
13589 pre_body
= push_stmt_list ();
13590 RECUR (OMP_FOR_PRE_BODY (t
));
13591 pre_body
= pop_stmt_list (pre_body
);
13593 if (OMP_FOR_INIT (t
) != NULL_TREE
)
13594 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
13595 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
13596 &clauses
, args
, complain
, in_decl
,
13597 integral_constant_expression_p
);
13599 body
= push_stmt_list ();
13600 RECUR (OMP_FOR_BODY (t
));
13601 body
= pop_stmt_list (body
);
13603 if (OMP_FOR_INIT (t
) != NULL_TREE
)
13604 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
, initv
,
13605 condv
, incrv
, body
, pre_body
, clauses
);
13608 t
= make_node (TREE_CODE (t
));
13609 TREE_TYPE (t
) = void_type_node
;
13610 OMP_FOR_BODY (t
) = body
;
13611 OMP_FOR_PRE_BODY (t
) = pre_body
;
13612 OMP_FOR_CLAUSES (t
) = clauses
;
13613 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
13617 add_stmt (finish_omp_structured_block (stmt
));
13624 case OMP_TARGET_DATA
:
13626 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false,
13627 args
, complain
, in_decl
);
13628 stmt
= push_stmt_list ();
13629 RECUR (OMP_BODY (t
));
13630 stmt
= pop_stmt_list (stmt
);
13633 OMP_BODY (t
) = stmt
;
13634 OMP_CLAUSES (t
) = tmp
;
13638 case OMP_TARGET_UPDATE
:
13639 tmp
= tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t
), false,
13640 args
, complain
, in_decl
);
13642 OMP_CLAUSES (t
) = tmp
;
13649 case OMP_TASKGROUP
:
13651 stmt
= push_stmt_list ();
13652 RECUR (OMP_BODY (t
));
13653 stmt
= pop_stmt_list (stmt
);
13656 OMP_BODY (t
) = stmt
;
13661 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
13662 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
13664 tree op1
= TREE_OPERAND (t
, 1);
13665 tree rhs1
= NULL_TREE
;
13667 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
13669 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
13670 op1
= TREE_OPERAND (op1
, 1);
13672 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13673 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13674 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
13675 NULL_TREE
, NULL_TREE
, rhs1
,
13676 OMP_ATOMIC_SEQ_CST (t
));
13680 tree op1
= TREE_OPERAND (t
, 1);
13681 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
13682 tree rhs1
= NULL_TREE
;
13683 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
13684 enum tree_code opcode
= NOP_EXPR
;
13685 if (code
== OMP_ATOMIC_READ
)
13687 v
= RECUR (TREE_OPERAND (op1
, 0));
13688 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13690 else if (code
== OMP_ATOMIC_CAPTURE_OLD
13691 || code
== OMP_ATOMIC_CAPTURE_NEW
)
13693 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
13694 v
= RECUR (TREE_OPERAND (op1
, 0));
13695 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13696 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
13698 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
13699 op11
= TREE_OPERAND (op11
, 1);
13701 lhs
= RECUR (TREE_OPERAND (op11
, 0));
13702 rhs
= RECUR (TREE_OPERAND (op11
, 1));
13703 opcode
= TREE_CODE (op11
);
13704 if (opcode
== MODIFY_EXPR
)
13710 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13711 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13713 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
13714 OMP_ATOMIC_SEQ_CST (t
));
13718 case TRANSACTION_EXPR
:
13721 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
13722 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
13724 if (TRANSACTION_EXPR_IS_STMT (t
))
13726 tree body
= TRANSACTION_EXPR_BODY (t
);
13727 tree noex
= NULL_TREE
;
13728 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
13730 noex
= MUST_NOT_THROW_COND (body
);
13731 if (noex
== NULL_TREE
)
13732 noex
= boolean_true_node
;
13733 body
= TREE_OPERAND (body
, 0);
13735 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
13737 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
13741 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
13742 RECUR (TRANSACTION_EXPR_BODY (t
)),
13749 case MUST_NOT_THROW_EXPR
:
13750 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t
, 0)),
13751 RECUR (MUST_NOT_THROW_COND (t
))));
13753 case EXPR_PACK_EXPANSION
:
13754 error ("invalid use of pack expansion expression");
13755 RETURN (error_mark_node
);
13757 case NONTYPE_ARGUMENT_PACK
:
13758 error ("use %<...%> to expand argument pack");
13759 RETURN (error_mark_node
);
13761 case COMPOUND_EXPR
:
13762 tmp
= RECUR (TREE_OPERAND (t
, 0));
13763 if (tmp
== NULL_TREE
)
13764 /* If the first operand was a statement, we're done with it. */
13765 RETURN (RECUR (TREE_OPERAND (t
, 1)));
13766 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
13767 RECUR (TREE_OPERAND (t
, 1)),
13771 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
13773 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13774 /*function_p=*/false,
13775 integral_constant_expression_p
));
13778 RETURN (NULL_TREE
);
13780 input_location
= loc
;
13786 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
13787 function. For description of the body see comment above
13788 cp_parser_omp_declare_reduction_exprs. */
13791 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13793 if (t
== NULL_TREE
|| t
== error_mark_node
)
13796 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
13798 tree_stmt_iterator tsi
;
13801 memset (stmts
, 0, sizeof stmts
);
13802 for (i
= 0, tsi
= tsi_start (t
);
13803 i
< 7 && !tsi_end_p (tsi
);
13804 i
++, tsi_next (&tsi
))
13805 stmts
[i
] = tsi_stmt (tsi
);
13806 gcc_assert (tsi_end_p (tsi
));
13810 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
13811 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
13812 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
13813 args
, complain
, in_decl
);
13814 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
13815 args
, complain
, in_decl
);
13816 DECL_CONTEXT (omp_out
) = current_function_decl
;
13817 DECL_CONTEXT (omp_in
) = current_function_decl
;
13818 keep_next_level (true);
13819 tree block
= begin_omp_structured_block ();
13820 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
13821 block
= finish_omp_structured_block (block
);
13822 block
= maybe_cleanup_point_expr_void (block
);
13823 add_decl_expr (omp_out
);
13824 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
13825 TREE_NO_WARNING (omp_out
) = 1;
13826 add_decl_expr (omp_in
);
13827 finish_expr_stmt (block
);
13831 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
13832 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
13833 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
13834 args
, complain
, in_decl
);
13835 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
13836 args
, complain
, in_decl
);
13837 DECL_CONTEXT (omp_priv
) = current_function_decl
;
13838 DECL_CONTEXT (omp_orig
) = current_function_decl
;
13839 keep_next_level (true);
13840 tree block
= begin_omp_structured_block ();
13841 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
13842 block
= finish_omp_structured_block (block
);
13843 block
= maybe_cleanup_point_expr_void (block
);
13844 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
13845 add_decl_expr (omp_priv
);
13846 add_decl_expr (omp_orig
);
13847 finish_expr_stmt (block
);
13849 add_decl_expr (omp_orig
);
13853 /* T is a postfix-expression that is not being used in a function
13854 call. Return the substituted version of T. */
13857 tsubst_non_call_postfix_expression (tree t
, tree args
,
13858 tsubst_flags_t complain
,
13861 if (TREE_CODE (t
) == SCOPE_REF
)
13862 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
13863 /*done=*/false, /*address_p=*/false);
13865 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13866 /*function_p=*/false,
13867 /*integral_constant_expression_p=*/false);
13872 /* Like tsubst but deals with expressions and performs semantic
13873 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13876 tsubst_copy_and_build (tree t
,
13878 tsubst_flags_t complain
,
13881 bool integral_constant_expression_p
)
13883 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13884 #define RECUR(NODE) \
13885 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13886 /*function_p=*/false, \
13887 integral_constant_expression_p)
13892 if (t
== NULL_TREE
|| t
== error_mark_node
)
13895 loc
= input_location
;
13896 if (EXPR_HAS_LOCATION (t
))
13897 input_location
= EXPR_LOCATION (t
);
13899 /* N3276 decltype magic only applies to calls at the top level or on the
13900 right side of a comma. */
13901 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
13902 complain
&= ~tf_decltype
;
13904 switch (TREE_CODE (t
))
13908 /* Fall through. */
13909 case IDENTIFIER_NODE
:
13913 bool non_integral_constant_expression_p
;
13914 const char *error_msg
;
13916 if (IDENTIFIER_TYPENAME_P (t
))
13918 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13919 t
= mangle_conv_op_name_for_type (new_type
);
13922 /* Look up the name. */
13923 decl
= lookup_name (t
);
13925 /* By convention, expressions use ERROR_MARK_NODE to indicate
13926 failure, not NULL_TREE. */
13927 if (decl
== NULL_TREE
)
13928 decl
= error_mark_node
;
13930 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
13932 integral_constant_expression_p
,
13933 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
13934 &non_integral_constant_expression_p
,
13935 /*template_p=*/false,
13937 /*address_p=*/false,
13938 /*template_arg_p=*/false,
13943 if (!function_p
&& identifier_p (decl
))
13945 if (complain
& tf_error
)
13946 unqualified_name_lookup_error (decl
);
13947 decl
= error_mark_node
;
13952 case TEMPLATE_ID_EXPR
:
13955 tree templ
= RECUR (TREE_OPERAND (t
, 0));
13956 tree targs
= TREE_OPERAND (t
, 1);
13959 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
13961 if (TREE_CODE (templ
) == COMPONENT_REF
)
13963 object
= TREE_OPERAND (templ
, 0);
13964 templ
= TREE_OPERAND (templ
, 1);
13967 object
= NULL_TREE
;
13968 templ
= lookup_template_function (templ
, targs
);
13971 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
13972 object
, templ
, NULL_TREE
));
13974 RETURN (baselink_for_fns (templ
));
13979 tree r
= RECUR (TREE_OPERAND (t
, 0));
13981 if (REFERENCE_REF_P (t
))
13983 /* A type conversion to reference type will be enclosed in
13984 such an indirect ref, but the substitution of the cast
13985 will have also added such an indirect ref. */
13986 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
13987 r
= convert_from_reference (r
);
13990 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
13991 complain
|decltype_flag
);
13997 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13998 RECUR (TREE_OPERAND (t
, 0))));
14000 case IMPLICIT_CONV_EXPR
:
14002 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14003 tree expr
= RECUR (TREE_OPERAND (t
, 0));
14004 int flags
= LOOKUP_IMPLICIT
;
14005 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
14006 flags
= LOOKUP_NORMAL
;
14007 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
14014 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
14015 RECUR (TREE_OPERAND (t
, 0))));
14018 case REINTERPRET_CAST_EXPR
:
14019 case CONST_CAST_EXPR
:
14020 case DYNAMIC_CAST_EXPR
:
14021 case STATIC_CAST_EXPR
:
14024 tree op
, r
= NULL_TREE
;
14026 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14027 if (integral_constant_expression_p
14028 && !cast_valid_in_integral_constant_expression_p (type
))
14030 if (complain
& tf_error
)
14031 error ("a cast to a type other than an integral or "
14032 "enumeration type cannot appear in a constant-expression");
14033 RETURN (error_mark_node
);
14036 op
= RECUR (TREE_OPERAND (t
, 0));
14038 ++c_inhibit_evaluation_warnings
;
14039 switch (TREE_CODE (t
))
14042 r
= build_functional_cast (type
, op
, complain
);
14044 case REINTERPRET_CAST_EXPR
:
14045 r
= build_reinterpret_cast (type
, op
, complain
);
14047 case CONST_CAST_EXPR
:
14048 r
= build_const_cast (type
, op
, complain
);
14050 case DYNAMIC_CAST_EXPR
:
14051 r
= build_dynamic_cast (type
, op
, complain
);
14053 case STATIC_CAST_EXPR
:
14054 r
= build_static_cast (type
, op
, complain
);
14057 gcc_unreachable ();
14059 --c_inhibit_evaluation_warnings
;
14064 case POSTDECREMENT_EXPR
:
14065 case POSTINCREMENT_EXPR
:
14066 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14067 args
, complain
, in_decl
);
14068 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
14069 complain
|decltype_flag
));
14071 case PREDECREMENT_EXPR
:
14072 case PREINCREMENT_EXPR
:
14076 case TRUTH_NOT_EXPR
:
14077 case UNARY_PLUS_EXPR
: /* Unary + */
14078 case REALPART_EXPR
:
14079 case IMAGPART_EXPR
:
14080 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
14081 RECUR (TREE_OPERAND (t
, 0)),
14082 complain
|decltype_flag
));
14084 case FIX_TRUNC_EXPR
:
14085 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
14089 op1
= TREE_OPERAND (t
, 0);
14090 if (TREE_CODE (op1
) == LABEL_DECL
)
14091 RETURN (finish_label_address_expr (DECL_NAME (op1
),
14092 EXPR_LOCATION (op1
)));
14093 if (TREE_CODE (op1
) == SCOPE_REF
)
14094 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
14095 /*done=*/true, /*address_p=*/true);
14097 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
14099 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
14100 complain
|decltype_flag
));
14105 case TRUNC_DIV_EXPR
:
14106 case CEIL_DIV_EXPR
:
14107 case FLOOR_DIV_EXPR
:
14108 case ROUND_DIV_EXPR
:
14109 case EXACT_DIV_EXPR
:
14113 case TRUNC_MOD_EXPR
:
14114 case FLOOR_MOD_EXPR
:
14115 case TRUTH_ANDIF_EXPR
:
14116 case TRUTH_ORIF_EXPR
:
14117 case TRUTH_AND_EXPR
:
14118 case TRUTH_OR_EXPR
:
14136 ++c_inhibit_evaluation_warnings
;
14138 r
= build_x_binary_op
14139 (input_location
, TREE_CODE (t
),
14140 RECUR (TREE_OPERAND (t
, 0)),
14141 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
14143 : TREE_CODE (TREE_OPERAND (t
, 0))),
14144 RECUR (TREE_OPERAND (t
, 1)),
14145 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
14147 : TREE_CODE (TREE_OPERAND (t
, 1))),
14149 complain
|decltype_flag
);
14150 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
14151 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14153 --c_inhibit_evaluation_warnings
;
14159 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
14160 /*address_p=*/false));
14162 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14163 args
, complain
, in_decl
);
14164 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
14165 RECUR (TREE_OPERAND (t
, 1)),
14166 complain
|decltype_flag
));
14168 case ARRAY_NOTATION_REF
:
14170 tree start_index
, length
, stride
;
14171 op1
= tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t
),
14172 args
, complain
, in_decl
);
14173 start_index
= RECUR (ARRAY_NOTATION_START (t
));
14174 length
= RECUR (ARRAY_NOTATION_LENGTH (t
));
14175 stride
= RECUR (ARRAY_NOTATION_STRIDE (t
));
14176 RETURN (build_array_notation_ref (EXPR_LOCATION (t
), op1
, start_index
,
14177 length
, stride
, TREE_TYPE (op1
)));
14180 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
14181 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
14188 op1
= TREE_OPERAND (t
, 0);
14189 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
14190 op1
= TREE_TYPE (op1
);
14193 /* When there are no ARGS, we are trying to evaluate a
14194 non-dependent expression from the parser. Trying to do
14195 the substitutions may not work. */
14197 op1
= TREE_TYPE (op1
);
14201 ++cp_unevaluated_operand
;
14202 ++c_inhibit_evaluation_warnings
;
14204 op1
= tsubst (op1
, args
, complain
, in_decl
);
14206 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14207 /*function_p=*/false,
14208 /*integral_constant_expression_p=*/
14210 --cp_unevaluated_operand
;
14211 --c_inhibit_evaluation_warnings
;
14214 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
14215 complain
& tf_error
);
14217 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
14218 complain
& tf_error
);
14219 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
14221 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
14223 if (!processing_template_decl
&& TYPE_P (op1
))
14225 r
= build_min (SIZEOF_EXPR
, size_type_node
,
14226 build1 (NOP_EXPR
, op1
, error_mark_node
));
14227 SIZEOF_EXPR_TYPE_P (r
) = 1;
14230 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
14231 TREE_SIDE_EFFECTS (r
) = 0;
14232 TREE_READONLY (r
) = 1;
14234 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
14239 case AT_ENCODE_EXPR
:
14241 op1
= TREE_OPERAND (t
, 0);
14242 ++cp_unevaluated_operand
;
14243 ++c_inhibit_evaluation_warnings
;
14244 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14245 /*function_p=*/false,
14246 /*integral_constant_expression_p=*/false);
14247 --cp_unevaluated_operand
;
14248 --c_inhibit_evaluation_warnings
;
14249 RETURN (objc_build_encode_expr (op1
));
14252 case NOEXCEPT_EXPR
:
14253 op1
= TREE_OPERAND (t
, 0);
14254 ++cp_unevaluated_operand
;
14255 ++c_inhibit_evaluation_warnings
;
14256 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14257 /*function_p=*/false,
14258 /*integral_constant_expression_p=*/false);
14259 --cp_unevaluated_operand
;
14260 --c_inhibit_evaluation_warnings
;
14261 RETURN (finish_noexcept_expr (op1
, complain
));
14267 ++c_inhibit_evaluation_warnings
;
14269 r
= build_x_modify_expr
14270 (EXPR_LOCATION (t
),
14271 RECUR (TREE_OPERAND (t
, 0)),
14272 TREE_CODE (TREE_OPERAND (t
, 1)),
14273 RECUR (TREE_OPERAND (t
, 2)),
14274 complain
|decltype_flag
);
14275 /* TREE_NO_WARNING must be set if either the expression was
14276 parenthesized or it uses an operator such as >>= rather
14277 than plain assignment. In the former case, it was already
14278 set and must be copied. In the latter case,
14279 build_x_modify_expr sets it and it must not be reset
14281 if (TREE_NO_WARNING (t
))
14282 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14284 --c_inhibit_evaluation_warnings
;
14290 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14291 args
, complain
, in_decl
);
14292 /* Remember that there was a reference to this entity. */
14295 RETURN (build_x_arrow (input_location
, op1
, complain
));
14299 tree placement
= RECUR (TREE_OPERAND (t
, 0));
14300 tree init
= RECUR (TREE_OPERAND (t
, 3));
14301 vec
<tree
, va_gc
> *placement_vec
;
14302 vec
<tree
, va_gc
> *init_vec
;
14305 if (placement
== NULL_TREE
)
14306 placement_vec
= NULL
;
14309 placement_vec
= make_tree_vector ();
14310 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
14311 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
14314 /* If there was an initializer in the original tree, but it
14315 instantiated to an empty list, then we should pass a
14316 non-NULL empty vector to tell build_new that it was an
14317 empty initializer() rather than no initializer. This can
14318 only happen when the initializer is a pack expansion whose
14319 parameter packs are of length zero. */
14320 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
14324 init_vec
= make_tree_vector ();
14325 if (init
== void_zero_node
)
14326 gcc_assert (init_vec
!= NULL
);
14329 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
14330 vec_safe_push (init_vec
, TREE_VALUE (init
));
14334 ret
= build_new (&placement_vec
,
14335 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
14336 RECUR (TREE_OPERAND (t
, 2)),
14338 NEW_EXPR_USE_GLOBAL (t
),
14341 if (placement_vec
!= NULL
)
14342 release_tree_vector (placement_vec
);
14343 if (init_vec
!= NULL
)
14344 release_tree_vector (init_vec
);
14350 RETURN (delete_sanity
14351 (RECUR (TREE_OPERAND (t
, 0)),
14352 RECUR (TREE_OPERAND (t
, 1)),
14353 DELETE_EXPR_USE_VEC (t
),
14354 DELETE_EXPR_USE_GLOBAL (t
),
14357 case COMPOUND_EXPR
:
14359 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
14360 complain
& ~tf_decltype
, in_decl
,
14361 /*function_p=*/false,
14362 integral_constant_expression_p
);
14363 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
14365 RECUR (TREE_OPERAND (t
, 1)),
14366 complain
|decltype_flag
));
14372 vec
<tree
, va_gc
> *call_args
;
14373 unsigned int nargs
, i
;
14378 function
= CALL_EXPR_FN (t
);
14379 /* When we parsed the expression, we determined whether or
14380 not Koenig lookup should be performed. */
14381 koenig_p
= KOENIG_LOOKUP_P (t
);
14382 if (TREE_CODE (function
) == SCOPE_REF
)
14384 qualified_p
= true;
14385 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
14387 /*address_p=*/false);
14389 else if (koenig_p
&& identifier_p (function
))
14391 /* Do nothing; calling tsubst_copy_and_build on an identifier
14392 would incorrectly perform unqualified lookup again.
14394 Note that we can also have an IDENTIFIER_NODE if the earlier
14395 unqualified lookup found a member function; in that case
14396 koenig_p will be false and we do want to do the lookup
14397 again to find the instantiated member function.
14399 FIXME but doing that causes c++/15272, so we need to stop
14400 using IDENTIFIER_NODE in that situation. */
14401 qualified_p
= false;
14405 if (TREE_CODE (function
) == COMPONENT_REF
)
14407 tree op
= TREE_OPERAND (function
, 1);
14409 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
14410 || (BASELINK_P (op
)
14411 && BASELINK_QUALIFIED_P (op
)));
14414 qualified_p
= false;
14416 if (TREE_CODE (function
) == ADDR_EXPR
14417 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
14418 /* Avoid error about taking the address of a constructor. */
14419 function
= TREE_OPERAND (function
, 0);
14421 function
= tsubst_copy_and_build (function
, args
, complain
,
14424 integral_constant_expression_p
);
14426 if (BASELINK_P (function
))
14427 qualified_p
= true;
14430 nargs
= call_expr_nargs (t
);
14431 call_args
= make_tree_vector ();
14432 for (i
= 0; i
< nargs
; ++i
)
14434 tree arg
= CALL_EXPR_ARG (t
, i
);
14436 if (!PACK_EXPANSION_P (arg
))
14437 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
14440 /* Expand the pack expansion and push each entry onto
14442 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
14443 if (TREE_CODE (arg
) == TREE_VEC
)
14445 unsigned int len
, j
;
14447 len
= TREE_VEC_LENGTH (arg
);
14448 for (j
= 0; j
< len
; ++j
)
14450 tree value
= TREE_VEC_ELT (arg
, j
);
14451 if (value
!= NULL_TREE
)
14452 value
= convert_from_reference (value
);
14453 vec_safe_push (call_args
, value
);
14458 /* A partial substitution. Add one entry. */
14459 vec_safe_push (call_args
, arg
);
14464 /* We do not perform argument-dependent lookup if normal
14465 lookup finds a non-function, in accordance with the
14466 expected resolution of DR 218. */
14468 && ((is_overloaded_fn (function
)
14469 /* If lookup found a member function, the Koenig lookup is
14470 not appropriate, even if an unqualified-name was used
14471 to denote the function. */
14472 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
14473 || identifier_p (function
))
14474 /* Only do this when substitution turns a dependent call
14475 into a non-dependent call. */
14476 && type_dependent_expression_p_push (t
)
14477 && !any_type_dependent_arguments_p (call_args
))
14478 function
= perform_koenig_lookup (function
, call_args
, false,
14481 if (identifier_p (function
)
14482 && !any_type_dependent_arguments_p (call_args
))
14484 if (koenig_p
&& (complain
& tf_warning_or_error
))
14486 /* For backwards compatibility and good diagnostics, try
14487 the unqualified lookup again if we aren't in SFINAE
14489 tree unq
= (tsubst_copy_and_build
14490 (function
, args
, complain
, in_decl
, true,
14491 integral_constant_expression_p
));
14492 if (unq
== error_mark_node
)
14493 RETURN (error_mark_node
);
14495 if (unq
!= function
)
14498 if (INDIRECT_REF_P (fn
))
14499 fn
= TREE_OPERAND (fn
, 0);
14500 if (TREE_CODE (fn
) == COMPONENT_REF
)
14501 fn
= TREE_OPERAND (fn
, 1);
14502 if (is_overloaded_fn (fn
))
14503 fn
= get_first_fn (fn
);
14504 if (permerror (EXPR_LOC_OR_HERE (t
),
14505 "%qD was not declared in this scope, "
14506 "and no declarations were found by "
14507 "argument-dependent lookup at the point "
14508 "of instantiation", function
))
14511 /* Can't say anything more. */;
14512 else if (DECL_CLASS_SCOPE_P (fn
))
14514 inform (EXPR_LOC_OR_HERE (t
),
14515 "declarations in dependent base %qT are "
14516 "not found by unqualified lookup",
14517 DECL_CLASS_CONTEXT (fn
));
14518 if (current_class_ptr
)
14519 inform (EXPR_LOC_OR_HERE (t
),
14520 "use %<this->%D%> instead", function
);
14522 inform (EXPR_LOC_OR_HERE (t
),
14523 "use %<%T::%D%> instead",
14524 current_class_name
, function
);
14527 inform (0, "%q+D declared here, later in the "
14528 "translation unit", fn
);
14533 if (identifier_p (function
))
14535 if (complain
& tf_error
)
14536 unqualified_name_lookup_error (function
);
14537 release_tree_vector (call_args
);
14538 RETURN (error_mark_node
);
14542 /* Remember that there was a reference to this entity. */
14543 if (DECL_P (function
))
14544 mark_used (function
);
14546 /* Put back tf_decltype for the actual call. */
14547 complain
|= decltype_flag
;
14549 if (TREE_CODE (function
) == OFFSET_REF
)
14550 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
14552 else if (TREE_CODE (function
) == COMPONENT_REF
)
14554 tree instance
= TREE_OPERAND (function
, 0);
14555 tree fn
= TREE_OPERAND (function
, 1);
14557 if (processing_template_decl
14558 && (type_dependent_expression_p (instance
)
14559 || (!BASELINK_P (fn
)
14560 && TREE_CODE (fn
) != FIELD_DECL
)
14561 || type_dependent_expression_p (fn
)
14562 || any_type_dependent_arguments_p (call_args
)))
14563 ret
= build_nt_call_vec (function
, call_args
);
14564 else if (!BASELINK_P (fn
))
14565 ret
= finish_call_expr (function
, &call_args
,
14566 /*disallow_virtual=*/false,
14567 /*koenig_p=*/false,
14570 ret
= (build_new_method_call
14572 &call_args
, NULL_TREE
,
14573 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
14578 ret
= finish_call_expr (function
, &call_args
,
14579 /*disallow_virtual=*/qualified_p
,
14583 release_tree_vector (call_args
);
14590 tree cond
= RECUR (TREE_OPERAND (t
, 0));
14593 if (TREE_CODE (cond
) == INTEGER_CST
)
14595 if (integer_zerop (cond
))
14597 ++c_inhibit_evaluation_warnings
;
14598 exp1
= RECUR (TREE_OPERAND (t
, 1));
14599 --c_inhibit_evaluation_warnings
;
14600 exp2
= RECUR (TREE_OPERAND (t
, 2));
14604 exp1
= RECUR (TREE_OPERAND (t
, 1));
14605 ++c_inhibit_evaluation_warnings
;
14606 exp2
= RECUR (TREE_OPERAND (t
, 2));
14607 --c_inhibit_evaluation_warnings
;
14612 exp1
= RECUR (TREE_OPERAND (t
, 1));
14613 exp2
= RECUR (TREE_OPERAND (t
, 2));
14616 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
14617 cond
, exp1
, exp2
, complain
));
14620 case PSEUDO_DTOR_EXPR
:
14621 RETURN (finish_pseudo_destructor_expr
14622 (RECUR (TREE_OPERAND (t
, 0)),
14623 RECUR (TREE_OPERAND (t
, 1)),
14624 tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
),
14629 tree purpose
, value
, chain
;
14631 if (t
== void_list_node
)
14634 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
14635 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
14637 /* We have pack expansions, so expand those and
14638 create a new list out of it. */
14639 tree purposevec
= NULL_TREE
;
14640 tree valuevec
= NULL_TREE
;
14644 /* Expand the argument expressions. */
14645 if (TREE_PURPOSE (t
))
14646 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
14647 complain
, in_decl
);
14648 if (TREE_VALUE (t
))
14649 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
14650 complain
, in_decl
);
14652 /* Build the rest of the list. */
14653 chain
= TREE_CHAIN (t
);
14654 if (chain
&& chain
!= void_type_node
)
14655 chain
= RECUR (chain
);
14657 /* Determine the number of arguments. */
14658 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
14660 len
= TREE_VEC_LENGTH (purposevec
);
14661 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
14663 else if (TREE_CODE (valuevec
) == TREE_VEC
)
14664 len
= TREE_VEC_LENGTH (valuevec
);
14667 /* Since we only performed a partial substitution into
14668 the argument pack, we only RETURN (a single list
14670 if (purposevec
== TREE_PURPOSE (t
)
14671 && valuevec
== TREE_VALUE (t
)
14672 && chain
== TREE_CHAIN (t
))
14675 RETURN (tree_cons (purposevec
, valuevec
, chain
));
14678 /* Convert the argument vectors into a TREE_LIST */
14682 /* Grab the Ith values. */
14684 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
14687 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
14690 /* Build the list (backwards). */
14691 chain
= tree_cons (purpose
, value
, chain
);
14697 purpose
= TREE_PURPOSE (t
);
14699 purpose
= RECUR (purpose
);
14700 value
= TREE_VALUE (t
);
14702 value
= RECUR (value
);
14703 chain
= TREE_CHAIN (t
);
14704 if (chain
&& chain
!= void_type_node
)
14705 chain
= RECUR (chain
);
14706 if (purpose
== TREE_PURPOSE (t
)
14707 && value
== TREE_VALUE (t
)
14708 && chain
== TREE_CHAIN (t
))
14710 RETURN (tree_cons (purpose
, value
, chain
));
14713 case COMPONENT_REF
:
14719 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14720 args
, complain
, in_decl
);
14721 /* Remember that there was a reference to this entity. */
14722 if (DECL_P (object
))
14723 mark_used (object
);
14724 object_type
= TREE_TYPE (object
);
14726 member
= TREE_OPERAND (t
, 1);
14727 if (BASELINK_P (member
))
14728 member
= tsubst_baselink (member
,
14729 non_reference (TREE_TYPE (object
)),
14730 args
, complain
, in_decl
);
14732 member
= tsubst_copy (member
, args
, complain
, in_decl
);
14733 if (member
== error_mark_node
)
14734 RETURN (error_mark_node
);
14736 if (type_dependent_expression_p (object
))
14737 /* We can't do much here. */;
14738 else if (!CLASS_TYPE_P (object_type
))
14740 if (scalarish_type_p (object_type
))
14742 tree s
= NULL_TREE
;
14743 tree dtor
= member
;
14745 if (TREE_CODE (dtor
) == SCOPE_REF
)
14747 s
= TREE_OPERAND (dtor
, 0);
14748 dtor
= TREE_OPERAND (dtor
, 1);
14750 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
14752 dtor
= TREE_OPERAND (dtor
, 0);
14754 RETURN (finish_pseudo_destructor_expr
14755 (object
, s
, dtor
, input_location
));
14759 else if (TREE_CODE (member
) == SCOPE_REF
14760 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
14762 /* Lookup the template functions now that we know what the
14764 tree scope
= TREE_OPERAND (member
, 0);
14765 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
14766 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
14767 member
= lookup_qualified_name (scope
, tmpl
,
14768 /*is_type_p=*/false,
14769 /*complain=*/false);
14770 if (BASELINK_P (member
))
14772 BASELINK_FUNCTIONS (member
)
14773 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
14775 member
= (adjust_result_of_qualified_name_lookup
14776 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
14781 qualified_name_lookup_error (scope
, tmpl
, member
,
14783 RETURN (error_mark_node
);
14786 else if (TREE_CODE (member
) == SCOPE_REF
14787 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
14788 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
14790 if (complain
& tf_error
)
14792 if (TYPE_P (TREE_OPERAND (member
, 0)))
14793 error ("%qT is not a class or namespace",
14794 TREE_OPERAND (member
, 0));
14796 error ("%qD is not a class or namespace",
14797 TREE_OPERAND (member
, 0));
14799 RETURN (error_mark_node
);
14801 else if (TREE_CODE (member
) == FIELD_DECL
)
14802 RETURN (finish_non_static_data_member (member
, object
, NULL_TREE
));
14804 RETURN (finish_class_member_access_expr (object
, member
,
14805 /*template_p=*/false,
14810 RETURN (build_throw
14811 (RECUR (TREE_OPERAND (t
, 0))));
14815 vec
<constructor_elt
, va_gc
> *n
;
14816 constructor_elt
*ce
;
14817 unsigned HOST_WIDE_INT idx
;
14818 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14819 bool process_index_p
;
14821 bool need_copy_p
= false;
14824 if (type
== error_mark_node
)
14825 RETURN (error_mark_node
);
14827 /* digest_init will do the wrong thing if we let it. */
14828 if (type
&& TYPE_PTRMEMFUNC_P (type
))
14831 /* We do not want to process the index of aggregate
14832 initializers as they are identifier nodes which will be
14833 looked up by digest_init. */
14834 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
14836 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
14837 newlen
= vec_safe_length (n
);
14838 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
14840 if (ce
->index
&& process_index_p
14841 /* An identifier index is looked up in the type
14842 being initialized, not the current scope. */
14843 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
14844 ce
->index
= RECUR (ce
->index
);
14846 if (PACK_EXPANSION_P (ce
->value
))
14848 /* Substitute into the pack expansion. */
14849 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
14852 if (ce
->value
== error_mark_node
14853 || PACK_EXPANSION_P (ce
->value
))
14855 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
14856 /* Just move the argument into place. */
14857 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
14860 /* Update the length of the final CONSTRUCTOR
14861 arguments vector, and note that we will need to
14863 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
14864 need_copy_p
= true;
14868 ce
->value
= RECUR (ce
->value
);
14873 vec
<constructor_elt
, va_gc
> *old_n
= n
;
14875 vec_alloc (n
, newlen
);
14876 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
14878 if (TREE_CODE (ce
->value
) == TREE_VEC
)
14880 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
14881 for (i
= 0; i
< len
; ++i
)
14882 CONSTRUCTOR_APPEND_ELT (n
, 0,
14883 TREE_VEC_ELT (ce
->value
, i
));
14886 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
14890 r
= build_constructor (init_list_type_node
, n
);
14891 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
14893 if (TREE_HAS_CONSTRUCTOR (t
))
14894 RETURN (finish_compound_literal (type
, r
, complain
));
14896 TREE_TYPE (r
) = type
;
14902 tree operand_0
= TREE_OPERAND (t
, 0);
14903 if (TYPE_P (operand_0
))
14905 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
14906 RETURN (get_typeid (operand_0
, complain
));
14910 operand_0
= RECUR (operand_0
);
14911 RETURN (build_typeid (operand_0
, complain
));
14918 else if (DECL_PACK_P (t
))
14920 /* We don't build decls for an instantiation of a
14921 variadic capture proxy, we instantiate the elements
14923 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
14924 return RECUR (DECL_VALUE_EXPR (t
));
14930 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
14932 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
14933 /* If the original type was a reference, we'll be wrapped in
14934 the appropriate INDIRECT_REF. */
14935 r
= convert_from_reference (r
);
14940 RETURN (build_x_va_arg (EXPR_LOCATION (t
),
14941 RECUR (TREE_OPERAND (t
, 0)),
14942 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
)));
14944 case OFFSETOF_EXPR
:
14945 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t
, 0))));
14949 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
14950 complain
, in_decl
);
14952 tree type2
= TRAIT_EXPR_TYPE2 (t
);
14954 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
14956 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
14961 tree old_stmt_expr
= cur_stmt_expr
;
14962 tree stmt_expr
= begin_stmt_expr ();
14964 cur_stmt_expr
= stmt_expr
;
14965 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
14966 integral_constant_expression_p
);
14967 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
14968 cur_stmt_expr
= old_stmt_expr
;
14970 /* If the resulting list of expression statement is empty,
14971 fold it further into void_zero_node. */
14972 if (empty_expr_stmt_p (stmt_expr
))
14973 stmt_expr
= void_zero_node
;
14975 RETURN (stmt_expr
);
14980 tree r
= build_lambda_expr ();
14982 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
14983 LAMBDA_EXPR_CLOSURE (r
) = type
;
14984 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
14986 LAMBDA_EXPR_LOCATION (r
)
14987 = LAMBDA_EXPR_LOCATION (t
);
14988 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
14989 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
14990 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
14991 LAMBDA_EXPR_DISCRIMINATOR (r
)
14992 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
14993 /* For a function scope, we want to use tsubst so that we don't
14994 complain about referring to an auto function before its return
14995 type has been deduced. Otherwise, we want to use tsubst_copy so
14996 that we look up the existing field/parameter/variable rather
14997 than build a new one. */
14998 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
14999 if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
15000 scope
= tsubst (scope
, args
, complain
, in_decl
);
15001 else if (scope
&& TREE_CODE (scope
) == PARM_DECL
)
15003 /* Look up the parameter we want directly, as tsubst_copy
15004 doesn't do what we need. */
15005 tree fn
= tsubst (DECL_CONTEXT (scope
), args
, complain
, in_decl
);
15006 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
15007 while (DECL_PARM_INDEX (parm
) != DECL_PARM_INDEX (scope
))
15008 parm
= DECL_CHAIN (parm
);
15010 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15011 if (DECL_CONTEXT (scope
) == NULL_TREE
)
15012 DECL_CONTEXT (scope
) = fn
;
15015 scope
= RECUR (scope
);
15016 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
15017 LAMBDA_EXPR_RETURN_TYPE (r
)
15018 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
15020 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
15021 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
15023 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15024 determine_visibility (TYPE_NAME (type
));
15025 /* Now that we know visibility, instantiate the type so we have a
15026 declaration of the op() for later calls to lambda_function. */
15027 complete_type (type
);
15029 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
15031 RETURN (build_lambda_object (r
));
15035 /* We can get here for a constant initializer of non-dependent type.
15036 FIXME stop folding in cp_parser_initializer_clause. */
15038 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
15043 case TRANSACTION_EXPR
:
15044 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
15045 integral_constant_expression_p
));
15048 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
15050 case VEC_PERM_EXPR
:
15051 RETURN (build_x_vec_perm_expr (input_location
,
15052 RECUR (TREE_OPERAND (t
, 0)),
15053 RECUR (TREE_OPERAND (t
, 1)),
15054 RECUR (TREE_OPERAND (t
, 2)),
15058 /* Handle Objective-C++ constructs, if appropriate. */
15061 = objcp_tsubst_copy_and_build (t
, args
, complain
,
15062 in_decl
, /*function_p=*/false);
15066 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
15072 input_location
= loc
;
15076 /* Verify that the instantiated ARGS are valid. For type arguments,
15077 make sure that the type's linkage is ok. For non-type arguments,
15078 make sure they are constants if they are integral or enumerations.
15079 Emit an error under control of COMPLAIN, and return TRUE on error. */
15082 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
15084 if (dependent_template_arg_p (t
))
15086 if (ARGUMENT_PACK_P (t
))
15088 tree vec
= ARGUMENT_PACK_ARGS (t
);
15089 int len
= TREE_VEC_LENGTH (vec
);
15090 bool result
= false;
15093 for (i
= 0; i
< len
; ++i
)
15094 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
15098 else if (TYPE_P (t
))
15100 /* [basic.link]: A name with no linkage (notably, the name
15101 of a class or enumeration declared in a local scope)
15102 shall not be used to declare an entity with linkage.
15103 This implies that names with no linkage cannot be used as
15106 DR 757 relaxes this restriction for C++0x. */
15107 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
15108 : no_linkage_check (t
, /*relaxed_p=*/false));
15112 /* DR 488 makes use of a type with no linkage cause
15113 type deduction to fail. */
15114 if (complain
& tf_error
)
15116 if (TYPE_ANONYMOUS_P (nt
))
15117 error ("%qT is/uses anonymous type", t
);
15119 error ("template argument for %qD uses local type %qT",
15124 /* In order to avoid all sorts of complications, we do not
15125 allow variably-modified types as template arguments. */
15126 else if (variably_modified_type_p (t
, NULL_TREE
))
15128 if (complain
& tf_error
)
15129 error ("%qT is a variably modified type", t
);
15133 /* Class template and alias template arguments should be OK. */
15134 else if (DECL_TYPE_TEMPLATE_P (t
))
15136 /* A non-type argument of integral or enumerated type must be a
15138 else if (TREE_TYPE (t
)
15139 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
15140 && !TREE_CONSTANT (t
))
15142 if (complain
& tf_error
)
15143 error ("integral expression %qE is not constant", t
);
15150 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
15152 int ix
, len
= DECL_NTPARMS (tmpl
);
15153 bool result
= false;
15155 for (ix
= 0; ix
!= len
; ix
++)
15157 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
15160 if (result
&& (complain
& tf_error
))
15161 error (" trying to instantiate %qD", tmpl
);
15165 /* We're out of SFINAE context now, so generate diagnostics for the access
15166 errors we saw earlier when instantiating D from TMPL and ARGS. */
15169 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
15171 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
15172 tree type
= TREE_TYPE (pattern
);
15173 location_t loc
= input_location
;
15175 push_access_scope (d
);
15176 push_deferring_access_checks (dk_no_deferred
);
15177 input_location
= DECL_SOURCE_LOCATION (pattern
);
15178 tsubst (type
, args
, tf_warning_or_error
, d
);
15179 input_location
= loc
;
15180 pop_deferring_access_checks ();
15181 pop_access_scope (d
);
15184 /* Instantiate the indicated variable, function, or alias template TMPL with
15185 the template arguments in TARG_PTR. */
15188 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
15190 tree targ_ptr
= orig_args
;
15194 bool access_ok
= true;
15196 if (tmpl
== error_mark_node
)
15197 return error_mark_node
;
15199 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
15201 /* If this function is a clone, handle it specially. */
15202 if (DECL_CLONED_FUNCTION_P (tmpl
))
15207 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15208 DECL_CLONED_FUNCTION. */
15209 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
15210 targ_ptr
, complain
);
15211 if (spec
== error_mark_node
)
15212 return error_mark_node
;
15214 /* Look for the clone. */
15215 FOR_EACH_CLONE (clone
, spec
)
15216 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
15218 /* We should always have found the clone by now. */
15219 gcc_unreachable ();
15223 /* Check to see if we already have this specialization. */
15224 gen_tmpl
= most_general_template (tmpl
);
15225 if (tmpl
!= gen_tmpl
)
15226 /* The TMPL is a partial instantiation. To get a full set of
15227 arguments we must add the arguments used to perform the
15228 partial instantiation. */
15229 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
15232 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15233 but it doesn't seem to be on the hot path. */
15234 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
15236 gcc_assert (tmpl
== gen_tmpl
15237 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
15239 || fndecl
== NULL_TREE
);
15241 if (spec
!= NULL_TREE
)
15243 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
15245 if (complain
& tf_error
)
15246 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
15247 return error_mark_node
;
15252 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
15254 return error_mark_node
;
15256 /* We are building a FUNCTION_DECL, during which the access of its
15257 parameters and return types have to be checked. However this
15258 FUNCTION_DECL which is the desired context for access checking
15259 is not built yet. We solve this chicken-and-egg problem by
15260 deferring all checks until we have the FUNCTION_DECL. */
15261 push_deferring_access_checks (dk_deferred
);
15263 /* Instantiation of the function happens in the context of the function
15264 template, not the context of the overload resolution we're doing. */
15265 push_to_top_level ();
15266 /* If there are dependent arguments, e.g. because we're doing partial
15267 ordering, make sure processing_template_decl stays set. */
15268 if (uses_template_parms (targ_ptr
))
15269 ++processing_template_decl
;
15270 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
15272 tree ctx
= tsubst (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
15273 complain
, gen_tmpl
);
15274 push_nested_class (ctx
);
15276 /* Substitute template parameters to obtain the specialization. */
15277 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
15278 targ_ptr
, complain
, gen_tmpl
);
15279 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
15280 pop_nested_class ();
15281 pop_from_top_level ();
15283 if (fndecl
== error_mark_node
)
15285 pop_deferring_access_checks ();
15286 return error_mark_node
;
15289 /* The DECL_TI_TEMPLATE should always be the immediate parent
15290 template, not the most general template. */
15291 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
15293 /* Now we know the specialization, compute access previously
15295 push_access_scope (fndecl
);
15296 if (!perform_deferred_access_checks (complain
))
15298 pop_access_scope (fndecl
);
15299 pop_deferring_access_checks ();
15301 /* If we've just instantiated the main entry point for a function,
15302 instantiate all the alternate entry points as well. We do this
15303 by cloning the instantiation of the main entry point, not by
15304 instantiating the template clones. */
15305 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
15306 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
15310 if (!(complain
& tf_error
))
15312 /* Remember to reinstantiate when we're out of SFINAE so the user
15313 can see the errors. */
15314 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
15316 return error_mark_node
;
15321 /* Wrapper for instantiate_template_1. */
15324 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
15327 timevar_push (TV_TEMPLATE_INST
);
15328 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
15329 timevar_pop (TV_TEMPLATE_INST
);
15333 /* Instantiate the alias template TMPL with ARGS. Also push a template
15334 instantiation level, which instantiate_template doesn't do because
15335 functions and variables have sufficient context established by the
15339 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
15341 struct pending_template
*old_last_pend
= last_pending_template
;
15342 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
15343 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
15344 return error_mark_node
;
15345 tree tinst
= build_tree_list (tmpl
, args
);
15346 if (!push_tinst_level (tinst
))
15349 return error_mark_node
;
15353 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
15354 args
, tmpl
, complain
,
15355 /*require_all_args=*/true,
15356 /*use_default_args=*/true);
15358 tree r
= instantiate_template (tmpl
, args
, complain
);
15359 pop_tinst_level ();
15360 /* We can't free this if a pending_template entry or last_error_tinst_level
15361 is pointing at it. */
15362 if (last_pending_template
== old_last_pend
15363 && last_error_tinst_level
== old_error_tinst
)
15369 /* PARM is a template parameter pack for FN. Returns true iff
15370 PARM is used in a deducible way in the argument list of FN. */
15373 pack_deducible_p (tree parm
, tree fn
)
15375 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
15376 for (; t
; t
= TREE_CHAIN (t
))
15378 tree type
= TREE_VALUE (t
);
15380 if (!PACK_EXPANSION_P (type
))
15382 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
15383 packs
; packs
= TREE_CHAIN (packs
))
15384 if (TREE_VALUE (packs
) == parm
)
15386 /* The template parameter pack is used in a function parameter
15387 pack. If this is the end of the parameter list, the
15388 template parameter pack is deducible. */
15389 if (TREE_CHAIN (t
) == void_list_node
)
15392 /* Otherwise, not. Well, it could be deduced from
15393 a non-pack parameter, but doing so would end up with
15394 a deduction mismatch, so don't bother. */
15398 /* The template parameter pack isn't used in any function parameter
15399 packs, but it might be used deeper, e.g. tuple<Args...>. */
15403 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
15404 NARGS elements of the arguments that are being used when calling
15405 it. TARGS is a vector into which the deduced template arguments
15408 Return zero for success, 2 for an incomplete match that doesn't resolve
15409 all the types, and 1 for complete failure. An error message will be
15410 printed only for an incomplete match.
15412 If FN is a conversion operator, or we are trying to produce a specific
15413 specialization, RETURN_TYPE is the return type desired.
15415 The EXPLICIT_TARGS are explicit template arguments provided via a
15418 The parameter STRICT is one of:
15421 We are deducing arguments for a function call, as in
15422 [temp.deduct.call].
15425 We are deducing arguments for a conversion function, as in
15426 [temp.deduct.conv].
15429 We are deducing arguments when doing an explicit instantiation
15430 as in [temp.explicit], when determining an explicit specialization
15431 as in [temp.expl.spec], or when taking the address of a function
15432 template, as in [temp.deduct.funcaddr]. */
15435 fn_type_unification (tree fn
,
15436 tree explicit_targs
,
15439 unsigned int nargs
,
15441 unification_kind_t strict
,
15448 tree decl
= NULL_TREE
;
15449 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
15451 static int deduction_depth
;
15452 struct pending_template
*old_last_pend
= last_pending_template
;
15453 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
15454 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
15456 tree r
= error_mark_node
;
15459 complain
|= tf_decltype
;
15461 /* In C++0x, it's possible to have a function template whose type depends
15462 on itself recursively. This is most obvious with decltype, but can also
15463 occur with enumeration scope (c++/48969). So we need to catch infinite
15464 recursion and reject the substitution at deduction time; this function
15465 will return error_mark_node for any repeated substitution.
15467 This also catches excessive recursion such as when f<N> depends on
15468 f<N-1> across all integers, and returns error_mark_node for all the
15469 substitutions back up to the initial one.
15471 This is, of course, not reentrant. */
15472 if (excessive_deduction_depth
)
15473 return error_mark_node
;
15474 tinst
= build_tree_list (fn
, NULL_TREE
);
15477 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
15479 fntype
= TREE_TYPE (fn
);
15480 if (explicit_targs
)
15484 The specified template arguments must match the template
15485 parameters in kind (i.e., type, nontype, template), and there
15486 must not be more arguments than there are parameters;
15487 otherwise type deduction fails.
15489 Nontype arguments must match the types of the corresponding
15490 nontype template parameters, or must be convertible to the
15491 types of the corresponding nontype parameters as specified in
15492 _temp.arg.nontype_, otherwise type deduction fails.
15494 All references in the function type of the function template
15495 to the corresponding template parameters are replaced by the
15496 specified template argument values. If a substitution in a
15497 template parameter or in the function type of the function
15498 template results in an invalid type, type deduction fails. */
15499 int i
, len
= TREE_VEC_LENGTH (tparms
);
15500 location_t loc
= input_location
;
15501 bool incomplete
= false;
15503 /* Adjust any explicit template arguments before entering the
15504 substitution context. */
15506 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
15508 /*require_all_args=*/false,
15509 /*use_default_args=*/false));
15510 if (explicit_targs
== error_mark_node
)
15513 /* Substitute the explicit args into the function type. This is
15514 necessary so that, for instance, explicitly declared function
15515 arguments can match null pointed constants. If we were given
15516 an incomplete set of explicit args, we must not do semantic
15517 processing during substitution as we could create partial
15519 for (i
= 0; i
< len
; i
++)
15521 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
15522 bool parameter_pack
= false;
15523 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
15525 /* Dig out the actual parm. */
15526 if (TREE_CODE (parm
) == TYPE_DECL
15527 || TREE_CODE (parm
) == TEMPLATE_DECL
)
15529 parm
= TREE_TYPE (parm
);
15530 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
15532 else if (TREE_CODE (parm
) == PARM_DECL
)
15534 parm
= DECL_INITIAL (parm
);
15535 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
15538 if (!parameter_pack
&& targ
== NULL_TREE
)
15539 /* No explicit argument for this template parameter. */
15542 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
15544 /* Mark the argument pack as "incomplete". We could
15545 still deduce more arguments during unification.
15546 We remove this mark in type_unification_real. */
15549 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
15550 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
15551 = ARGUMENT_PACK_ARGS (targ
);
15554 /* We have some incomplete argument packs. */
15559 TREE_VALUE (tinst
) = explicit_targs
;
15560 if (!push_tinst_level (tinst
))
15562 excessive_deduction_depth
= true;
15565 processing_template_decl
+= incomplete
;
15566 input_location
= DECL_SOURCE_LOCATION (fn
);
15567 /* Ignore any access checks; we'll see them again in
15568 instantiate_template and they might have the wrong
15569 access path at this point. */
15570 push_deferring_access_checks (dk_deferred
);
15571 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
15572 complain
| tf_partial
, NULL_TREE
);
15573 pop_deferring_access_checks ();
15574 input_location
= loc
;
15575 processing_template_decl
-= incomplete
;
15576 pop_tinst_level ();
15578 if (fntype
== error_mark_node
)
15581 /* Place the explicitly specified arguments in TARGS. */
15582 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
15583 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
15586 /* Never do unification on the 'this' parameter. */
15587 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
15593 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
15594 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
15595 new_args
[0] = return_type
;
15596 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
15601 /* We allow incomplete unification without an error message here
15602 because the standard doesn't seem to explicitly prohibit it. Our
15603 callers must be ready to deal with unification failures in any
15606 TREE_VALUE (tinst
) = targs
;
15607 /* If we aren't explaining yet, push tinst context so we can see where
15608 any errors (e.g. from class instantiations triggered by instantiation
15609 of default template arguments) come from. If we are explaining, this
15610 context is redundant. */
15611 if (!explain_p
&& !push_tinst_level (tinst
))
15613 excessive_deduction_depth
= true;
15617 /* type_unification_real will pass back any access checks from default
15618 template argument substitution. */
15619 vec
<deferred_access_check
, va_gc
> *checks
;
15622 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15623 targs
, parms
, args
, nargs
, /*subr=*/0,
15624 strict
, flags
, &checks
, explain_p
);
15626 pop_tinst_level ();
15630 /* Now that we have bindings for all of the template arguments,
15631 ensure that the arguments deduced for the template template
15632 parameters have compatible template parameter lists. We cannot
15633 check this property before we have deduced all template
15634 arguments, because the template parameter types of a template
15635 template parameter might depend on prior template parameters
15636 deduced after the template template parameter. The following
15637 ill-formed example illustrates this issue:
15639 template<typename T, template<T> class C> void f(C<5>, T);
15641 template<int N> struct X {};
15644 f(X<5>(), 5l); // error: template argument deduction fails
15647 The template parameter list of 'C' depends on the template type
15648 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15649 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15650 time that we deduce 'C'. */
15651 if (!template_template_parm_bindings_ok_p
15652 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
15654 unify_inconsistent_template_template_parameters (explain_p
);
15658 /* All is well so far. Now, check:
15662 When all template arguments have been deduced, all uses of
15663 template parameters in nondeduced contexts are replaced with
15664 the corresponding deduced argument values. If the
15665 substitution results in an invalid type, as described above,
15666 type deduction fails. */
15667 TREE_VALUE (tinst
) = targs
;
15668 if (!push_tinst_level (tinst
))
15670 excessive_deduction_depth
= true;
15674 /* Also collect access checks from the instantiation. */
15675 reopen_deferring_access_checks (checks
);
15677 decl
= instantiate_template (fn
, targs
, complain
);
15679 checks
= get_deferred_access_checks ();
15680 pop_deferring_access_checks ();
15682 pop_tinst_level ();
15684 if (decl
== error_mark_node
)
15687 /* Now perform any access checks encountered during substitution. */
15688 push_access_scope (decl
);
15689 ok
= perform_access_checks (checks
, complain
);
15690 pop_access_scope (decl
);
15694 /* If we're looking for an exact match, check that what we got
15695 is indeed an exact match. It might not be if some template
15696 parameters are used in non-deduced contexts. */
15697 if (strict
== DEDUCE_EXACT
)
15699 tree substed
= TREE_TYPE (decl
);
15703 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
15705 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
15706 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
15707 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
15709 unify_type_mismatch (explain_p
, args
[i
],
15710 TREE_VALUE (sarg
));
15719 if (excessive_deduction_depth
)
15721 if (deduction_depth
== 0)
15722 /* Reset once we're all the way out. */
15723 excessive_deduction_depth
= false;
15726 /* We can't free this if a pending_template entry or last_error_tinst_level
15727 is pointing at it. */
15728 if (last_pending_template
== old_last_pend
15729 && last_error_tinst_level
== old_error_tinst
)
15735 /* Adjust types before performing type deduction, as described in
15736 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15737 sections are symmetric. PARM is the type of a function parameter
15738 or the return type of the conversion function. ARG is the type of
15739 the argument passed to the call, or the type of the value
15740 initialized with the result of the conversion function.
15741 ARG_EXPR is the original argument expression, which may be null. */
15744 maybe_adjust_types_for_deduction (unification_kind_t strict
,
15758 /* Swap PARM and ARG throughout the remainder of this
15759 function; the handling is precisely symmetric since PARM
15760 will initialize ARG rather than vice versa. */
15768 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15769 too, but here handle it by stripping the reference from PARM
15770 rather than by adding it to ARG. */
15771 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15772 && TYPE_REF_IS_RVALUE (*parm
)
15773 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15774 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15775 && TREE_CODE (*arg
) == REFERENCE_TYPE
15776 && !TYPE_REF_IS_RVALUE (*arg
))
15777 *parm
= TREE_TYPE (*parm
);
15778 /* Nothing else to do in this case. */
15782 gcc_unreachable ();
15785 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
15787 /* [temp.deduct.call]
15789 If P is not a reference type:
15791 --If A is an array type, the pointer type produced by the
15792 array-to-pointer standard conversion (_conv.array_) is
15793 used in place of A for type deduction; otherwise,
15795 --If A is a function type, the pointer type produced by
15796 the function-to-pointer standard conversion
15797 (_conv.func_) is used in place of A for type deduction;
15800 --If A is a cv-qualified type, the top level
15801 cv-qualifiers of A's type are ignored for type
15803 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
15804 *arg
= build_pointer_type (TREE_TYPE (*arg
));
15805 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
15806 *arg
= build_pointer_type (*arg
);
15808 *arg
= TYPE_MAIN_VARIANT (*arg
);
15811 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15812 of the form T&&, where T is a template parameter, and the argument
15813 is an lvalue, T is deduced as A& */
15814 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15815 && TYPE_REF_IS_RVALUE (*parm
)
15816 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15817 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15818 && (arg_expr
? real_lvalue_p (arg_expr
)
15819 /* try_one_overload doesn't provide an arg_expr, but
15820 functions are always lvalues. */
15821 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
15822 *arg
= build_reference_type (*arg
);
15824 /* [temp.deduct.call]
15826 If P is a cv-qualified type, the top level cv-qualifiers
15827 of P's type are ignored for type deduction. If P is a
15828 reference type, the type referred to by P is used for
15830 *parm
= TYPE_MAIN_VARIANT (*parm
);
15831 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
15833 *parm
= TREE_TYPE (*parm
);
15834 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
15837 /* DR 322. For conversion deduction, remove a reference type on parm
15838 too (which has been swapped into ARG). */
15839 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
15840 *arg
= TREE_TYPE (*arg
);
15845 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15846 template which does contain any deducible template parameters; check if
15847 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15848 unify_one_argument. */
15851 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
15852 int flags
, bool explain_p
)
15857 type
= TREE_TYPE (arg
);
15861 if (same_type_p (parm
, type
))
15862 return unify_success (explain_p
);
15864 if (strict
== DEDUCE_CONV
)
15866 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
15867 explain_p
? tf_warning_or_error
: tf_none
))
15868 return unify_success (explain_p
);
15870 else if (strict
!= DEDUCE_EXACT
)
15872 if (can_convert_arg (parm
, type
,
15873 TYPE_P (arg
) ? NULL_TREE
: arg
,
15874 flags
, explain_p
? tf_warning_or_error
: tf_none
))
15875 return unify_success (explain_p
);
15878 if (strict
== DEDUCE_EXACT
)
15879 return unify_type_mismatch (explain_p
, parm
, arg
);
15881 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
15884 static bool uses_deducible_template_parms (tree type
);
15886 /* Returns true iff the expression EXPR is one from which a template
15887 argument can be deduced. In other words, if it's an undecorated
15888 use of a template non-type parameter. */
15891 deducible_expression (tree expr
)
15893 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
15896 /* Returns true iff the array domain DOMAIN uses a template parameter in a
15897 deducible way; that is, if it has a max value of <PARM> - 1. */
15900 deducible_array_bound (tree domain
)
15902 if (domain
== NULL_TREE
)
15905 tree max
= TYPE_MAX_VALUE (domain
);
15906 if (TREE_CODE (max
) != MINUS_EXPR
)
15909 return deducible_expression (TREE_OPERAND (max
, 0));
15912 /* Returns true iff the template arguments ARGS use a template parameter
15913 in a deducible way. */
15916 deducible_template_args (tree args
)
15918 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
15921 tree elt
= TREE_VEC_ELT (args
, i
);
15922 if (ARGUMENT_PACK_P (elt
))
15923 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
15926 if (PACK_EXPANSION_P (elt
))
15927 elt
= PACK_EXPANSION_PATTERN (elt
);
15928 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
15930 else if (TYPE_P (elt
))
15931 deducible
= uses_deducible_template_parms (elt
);
15933 deducible
= deducible_expression (elt
);
15941 /* Returns true iff TYPE contains any deducible references to template
15942 parameters, as per 14.8.2.5. */
15945 uses_deducible_template_parms (tree type
)
15947 if (PACK_EXPANSION_P (type
))
15948 type
= PACK_EXPANSION_PATTERN (type
);
15955 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
15956 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
15962 if (POINTER_TYPE_P (type
))
15963 return uses_deducible_template_parms (TREE_TYPE (type
));
15965 /* T[integer-constant ]
15967 if (TREE_CODE (type
) == ARRAY_TYPE
)
15968 return (uses_deducible_template_parms (TREE_TYPE (type
))
15969 || deducible_array_bound (TYPE_DOMAIN (type
)));
15981 if (TYPE_PTRMEM_P (type
))
15982 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
15983 || (uses_deducible_template_parms
15984 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
15986 /* template-name <T> (where template-name refers to a class template)
15987 template-name <i> (where template-name refers to a class template) */
15988 if (CLASS_TYPE_P (type
)
15989 && CLASSTYPE_TEMPLATE_INFO (type
)
15990 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
15991 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
15992 (CLASSTYPE_TI_ARGS (type
)));
15997 if (TREE_CODE (type
) == FUNCTION_TYPE
15998 || TREE_CODE (type
) == METHOD_TYPE
)
16000 if (uses_deducible_template_parms (TREE_TYPE (type
)))
16002 tree parm
= TYPE_ARG_TYPES (type
);
16003 if (TREE_CODE (type
) == METHOD_TYPE
)
16004 parm
= TREE_CHAIN (parm
);
16005 for (; parm
; parm
= TREE_CHAIN (parm
))
16006 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
16013 /* Subroutine of type_unification_real and unify_pack_expansion to
16014 handle unification of a single P/A pair. Parameters are as
16015 for those functions. */
16018 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
16019 int subr
, unification_kind_t strict
, int flags
,
16022 tree arg_expr
= NULL_TREE
;
16025 if (arg
== error_mark_node
|| parm
== error_mark_node
)
16026 return unify_invalid (explain_p
);
16027 if (arg
== unknown_type_node
)
16028 /* We can't deduce anything from this, but we might get all the
16029 template args from other function args. */
16030 return unify_success (explain_p
);
16032 /* Implicit conversions (Clause 4) will be performed on a function
16033 argument to convert it to the type of the corresponding function
16034 parameter if the parameter type contains no template-parameters that
16035 participate in template argument deduction. */
16036 if (TYPE_P (parm
) && !uses_template_parms (parm
))
16037 /* For function parameters that contain no template-parameters at all,
16038 we have historically checked for convertibility in order to shortcut
16039 consideration of this candidate. */
16040 return check_non_deducible_conversion (parm
, arg
, strict
, flags
,
16042 else if (strict
== DEDUCE_CALL
16043 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
16044 /* For function parameters with only non-deducible template parameters,
16046 return unify_success (explain_p
);
16051 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
16052 | UNIFY_ALLOW_MORE_CV_QUAL
16053 | UNIFY_ALLOW_DERIVED
);
16057 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
16061 arg_strict
= UNIFY_ALLOW_NONE
;
16065 gcc_unreachable ();
16068 /* We only do these transformations if this is the top-level
16069 parameter_type_list in a call or declaration matching; in other
16070 situations (nested function declarators, template argument lists) we
16071 won't be comparing a type to an expression, and we don't do any type
16077 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
16078 if (type_unknown_p (arg
))
16080 /* [temp.deduct.type] A template-argument can be
16081 deduced from a pointer to function or pointer
16082 to member function argument if the set of
16083 overloaded functions does not contain function
16084 templates and at most one of a set of
16085 overloaded functions provides a unique
16088 if (resolve_overloaded_unification
16089 (tparms
, targs
, parm
, arg
, strict
,
16090 arg_strict
, explain_p
))
16091 return unify_success (explain_p
);
16092 return unify_overload_resolution_failure (explain_p
, arg
);
16096 arg
= unlowered_expr_type (arg
);
16097 if (arg
== error_mark_node
)
16098 return unify_invalid (explain_p
);
16102 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
16105 gcc_assert ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
16106 == (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
));
16108 /* For deduction from an init-list we need the actual list. */
16109 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
16111 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
16114 /* Most parms like fn_type_unification.
16116 If SUBR is 1, we're being called recursively (to unify the
16117 arguments of a function or method parameter of a function
16120 CHECKS is a pointer to a vector of access checks encountered while
16121 substituting default template arguments. */
16124 type_unification_real (tree tparms
,
16128 unsigned int xnargs
,
16130 unification_kind_t strict
,
16132 vec
<deferred_access_check
, va_gc
> **checks
,
16137 int ntparms
= TREE_VEC_LENGTH (tparms
);
16138 int saw_undeduced
= 0;
16141 unsigned int nargs
;
16144 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
16145 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
16146 gcc_assert (ntparms
> 0);
16148 /* Reset the number of non-defaulted template arguments contained
16150 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
16158 while (parms
&& parms
!= void_list_node
16161 parm
= TREE_VALUE (parms
);
16163 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
16164 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
16165 /* For a function parameter pack that occurs at the end of the
16166 parameter-declaration-list, the type A of each remaining
16167 argument of the call is compared with the type P of the
16168 declarator-id of the function parameter pack. */
16171 parms
= TREE_CHAIN (parms
);
16173 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
16174 /* For a function parameter pack that does not occur at the
16175 end of the parameter-declaration-list, the type of the
16176 parameter pack is a non-deduced context. */
16182 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
16188 && parms
!= void_list_node
16189 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
16191 /* Unify the remaining arguments with the pack expansion type. */
16193 tree parmvec
= make_tree_vec (1);
16195 /* Allocate a TREE_VEC and copy in all of the arguments */
16196 argvec
= make_tree_vec (nargs
- ia
);
16197 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
16198 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
16200 /* Copy the parameter into parmvec. */
16201 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
16202 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
16203 /*subr=*/subr
, explain_p
))
16206 /* Advance to the end of the list of parameters. */
16207 parms
= TREE_CHAIN (parms
);
16210 /* Fail if we've reached the end of the parm list, and more args
16211 are present, and the parm list isn't variadic. */
16212 if (ia
< nargs
&& parms
== void_list_node
)
16213 return unify_too_many_arguments (explain_p
, nargs
, ia
);
16214 /* Fail if parms are left and they don't have default values. */
16215 if (parms
&& parms
!= void_list_node
16216 && TREE_PURPOSE (parms
) == NULL_TREE
)
16218 unsigned int count
= nargs
;
16220 while (p
&& p
!= void_list_node
)
16223 p
= TREE_CHAIN (p
);
16225 return unify_too_few_arguments (explain_p
, ia
, count
);
16230 tsubst_flags_t complain
= (explain_p
16231 ? tf_warning_or_error
16234 for (i
= 0; i
< ntparms
; i
++)
16236 tree targ
= TREE_VEC_ELT (targs
, i
);
16237 tree tparm
= TREE_VEC_ELT (tparms
, i
);
16239 /* Clear the "incomplete" flags on all argument packs now so that
16240 substituting them into later default arguments works. */
16241 if (targ
&& ARGUMENT_PACK_P (targ
))
16243 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
16244 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
16247 if (targ
|| tparm
== error_mark_node
)
16249 tparm
= TREE_VALUE (tparm
);
16251 /* If this is an undeduced nontype parameter that depends on
16252 a type parameter, try another pass; its type may have been
16253 deduced from a later argument than the one from which
16254 this parameter can be deduced. */
16255 if (TREE_CODE (tparm
) == PARM_DECL
16256 && uses_template_parms (TREE_TYPE (tparm
))
16257 && !saw_undeduced
++)
16260 /* Core issue #226 (C++0x) [temp.deduct]:
16262 If a template argument has not been deduced, its
16263 default template argument, if any, is used.
16265 When we are in C++98 mode, TREE_PURPOSE will either
16266 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16267 to explicitly check cxx_dialect here. */
16268 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
16270 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
16271 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
16272 reopen_deferring_access_checks (*checks
);
16273 location_t save_loc
= input_location
;
16275 input_location
= DECL_SOURCE_LOCATION (parm
);
16276 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
16277 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
16279 input_location
= save_loc
;
16280 *checks
= get_deferred_access_checks ();
16281 pop_deferring_access_checks ();
16282 if (arg
== error_mark_node
)
16286 TREE_VEC_ELT (targs
, i
) = arg
;
16287 /* The position of the first default template argument,
16288 is also the number of non-defaulted arguments in TARGS.
16290 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
16291 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
16296 /* If the type parameter is a parameter pack, then it will
16297 be deduced to an empty parameter pack. */
16298 if (template_parameter_pack_p (tparm
))
16302 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
16304 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
16305 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
16306 TREE_CONSTANT (arg
) = 1;
16309 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
16311 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
16313 TREE_VEC_ELT (targs
, i
) = arg
;
16317 return unify_parameter_deduction_failure (explain_p
, tparm
);
16320 #ifdef ENABLE_CHECKING
16321 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
16322 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
16325 return unify_success (explain_p
);
16328 /* Subroutine of type_unification_real. Args are like the variables
16329 at the call site. ARG is an overloaded function (or template-id);
16330 we try deducing template args from each of the overloads, and if
16331 only one succeeds, we go with that. Modifies TARGS and returns
16332 true on success. */
16335 resolve_overloaded_unification (tree tparms
,
16339 unification_kind_t strict
,
16343 tree tempargs
= copy_node (targs
);
16345 tree goodfn
= NULL_TREE
;
16348 if (TREE_CODE (arg
) == ADDR_EXPR
)
16350 arg
= TREE_OPERAND (arg
, 0);
16356 if (TREE_CODE (arg
) == COMPONENT_REF
)
16357 /* Handle `&x' where `x' is some static or non-static member
16359 arg
= TREE_OPERAND (arg
, 1);
16361 if (TREE_CODE (arg
) == OFFSET_REF
)
16362 arg
= TREE_OPERAND (arg
, 1);
16364 /* Strip baselink information. */
16365 if (BASELINK_P (arg
))
16366 arg
= BASELINK_FUNCTIONS (arg
);
16368 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
16370 /* If we got some explicit template args, we need to plug them into
16371 the affected templates before we try to unify, in case the
16372 explicit args will completely resolve the templates in question. */
16375 tree expl_subargs
= TREE_OPERAND (arg
, 1);
16376 arg
= TREE_OPERAND (arg
, 0);
16378 for (; arg
; arg
= OVL_NEXT (arg
))
16380 tree fn
= OVL_CURRENT (arg
);
16381 tree subargs
, elem
;
16383 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
16386 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
16387 expl_subargs
, NULL_TREE
, tf_none
,
16388 /*require_all_args=*/true,
16389 /*use_default_args=*/true);
16390 if (subargs
!= error_mark_node
16391 && !any_dependent_template_arguments_p (subargs
))
16393 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
16394 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
16395 elem
, strict
, sub_strict
, addr_p
, explain_p
)
16396 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
16405 /* If no templates (or more than one) are fully resolved by the
16406 explicit arguments, this template-id is a non-deduced context; it
16407 could still be OK if we deduce all template arguments for the
16408 enclosing call through other arguments. */
16412 else if (TREE_CODE (arg
) != OVERLOAD
16413 && TREE_CODE (arg
) != FUNCTION_DECL
)
16414 /* If ARG is, for example, "(0, &f)" then its type will be unknown
16415 -- but the deduction does not succeed because the expression is
16416 not just the function on its own. */
16419 for (; arg
; arg
= OVL_NEXT (arg
))
16420 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
16421 TREE_TYPE (OVL_CURRENT (arg
)),
16422 strict
, sub_strict
, addr_p
, explain_p
)
16423 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
16425 goodfn
= OVL_CURRENT (arg
);
16429 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16430 to function or pointer to member function argument if the set of
16431 overloaded functions does not contain function templates and at most
16432 one of a set of overloaded functions provides a unique match.
16434 So if we found multiple possibilities, we return success but don't
16435 deduce anything. */
16439 int i
= TREE_VEC_LENGTH (targs
);
16441 if (TREE_VEC_ELT (tempargs
, i
))
16442 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
16450 /* Core DR 115: In contexts where deduction is done and fails, or in
16451 contexts where deduction is not done, if a template argument list is
16452 specified and it, along with any default template arguments, identifies
16453 a single function template specialization, then the template-id is an
16454 lvalue for the function template specialization. */
16457 resolve_nondeduced_context (tree orig_expr
)
16459 tree expr
, offset
, baselink
;
16462 if (!type_unknown_p (orig_expr
))
16467 offset
= NULL_TREE
;
16468 baselink
= NULL_TREE
;
16470 if (TREE_CODE (expr
) == ADDR_EXPR
)
16472 expr
= TREE_OPERAND (expr
, 0);
16475 if (TREE_CODE (expr
) == OFFSET_REF
)
16478 expr
= TREE_OPERAND (expr
, 1);
16480 if (BASELINK_P (expr
))
16483 expr
= BASELINK_FUNCTIONS (expr
);
16486 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
16489 tree goodfn
= NULL_TREE
;
16491 /* If we got some explicit template args, we need to plug them into
16492 the affected templates before we try to unify, in case the
16493 explicit args will completely resolve the templates in question. */
16495 tree expl_subargs
= TREE_OPERAND (expr
, 1);
16496 tree arg
= TREE_OPERAND (expr
, 0);
16497 tree badfn
= NULL_TREE
;
16498 tree badargs
= NULL_TREE
;
16500 for (; arg
; arg
= OVL_NEXT (arg
))
16502 tree fn
= OVL_CURRENT (arg
);
16503 tree subargs
, elem
;
16505 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
16508 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
16509 expl_subargs
, NULL_TREE
, tf_none
,
16510 /*require_all_args=*/true,
16511 /*use_default_args=*/true);
16512 if (subargs
!= error_mark_node
16513 && !any_dependent_template_arguments_p (subargs
))
16515 elem
= instantiate_template (fn
, subargs
, tf_none
);
16516 if (elem
== error_mark_node
)
16521 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
16530 mark_used (goodfn
);
16533 expr
= build_baselink (BASELINK_BINFO (baselink
),
16534 BASELINK_ACCESS_BINFO (baselink
),
16535 expr
, BASELINK_OPTYPE (baselink
));
16539 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
16540 expr
= build_offset_ref (base
, expr
, addr
, tf_warning_or_error
);
16543 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
16546 else if (good
== 0 && badargs
)
16547 /* There were no good options and at least one bad one, so let the
16548 user know what the problem is. */
16549 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
16554 /* Subroutine of resolve_overloaded_unification; does deduction for a single
16555 overload. Fills TARGS with any deduced arguments, or error_mark_node if
16556 different overloads deduce different arguments for a given parm.
16557 ADDR_P is true if the expression for which deduction is being
16558 performed was of the form "& fn" rather than simply "fn".
16560 Returns 1 on success. */
16563 try_one_overload (tree tparms
,
16568 unification_kind_t strict
,
16577 if (arg
== error_mark_node
)
16580 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16581 to function or pointer to member function argument if the set of
16582 overloaded functions does not contain function templates and at most
16583 one of a set of overloaded functions provides a unique match.
16585 So if this is a template, just return success. */
16587 if (uses_template_parms (arg
))
16590 if (TREE_CODE (arg
) == METHOD_TYPE
)
16591 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
16593 arg
= build_pointer_type (arg
);
16595 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
16597 /* We don't copy orig_targs for this because if we have already deduced
16598 some template args from previous args, unify would complain when we
16599 try to deduce a template parameter for the same argument, even though
16600 there isn't really a conflict. */
16601 nargs
= TREE_VEC_LENGTH (targs
);
16602 tempargs
= make_tree_vec (nargs
);
16604 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
16607 /* First make sure we didn't deduce anything that conflicts with
16608 explicitly specified args. */
16609 for (i
= nargs
; i
--; )
16611 tree elt
= TREE_VEC_ELT (tempargs
, i
);
16612 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
16616 else if (uses_template_parms (elt
))
16617 /* Since we're unifying against ourselves, we will fill in
16618 template args used in the function parm list with our own
16619 template parms. Discard them. */
16620 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
16621 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
16625 for (i
= nargs
; i
--; )
16627 tree elt
= TREE_VEC_ELT (tempargs
, i
);
16630 TREE_VEC_ELT (targs
, i
) = elt
;
16636 /* PARM is a template class (perhaps with unbound template
16637 parameters). ARG is a fully instantiated type. If ARG can be
16638 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
16639 TARGS are as for unify. */
16642 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
16645 tree copy_of_targs
;
16647 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
16648 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
16649 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
16652 /* We need to make a new template argument vector for the call to
16653 unify. If we used TARGS, we'd clutter it up with the result of
16654 the attempted unification, even if this class didn't work out.
16655 We also don't want to commit ourselves to all the unifications
16656 we've already done, since unification is supposed to be done on
16657 an argument-by-argument basis. In other words, consider the
16658 following pathological case:
16660 template <int I, int J, int K>
16663 template <int I, int J>
16664 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
16666 template <int I, int J, int K>
16667 void f(S<I, J, K>, S<I, I, I>);
16676 Now, by the time we consider the unification involving `s2', we
16677 already know that we must have `f<0, 0, 0>'. But, even though
16678 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16679 because there are two ways to unify base classes of S<0, 1, 2>
16680 with S<I, I, I>. If we kept the already deduced knowledge, we
16681 would reject the possibility I=1. */
16682 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
16684 /* If unification failed, we're done. */
16685 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
16686 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
16692 /* Given a template type PARM and a class type ARG, find the unique
16693 base type in ARG that is an instance of PARM. We do not examine
16694 ARG itself; only its base-classes. If there is not exactly one
16695 appropriate base class, return NULL_TREE. PARM may be the type of
16696 a partial specialization, as well as a plain template type. Used
16699 static enum template_base_result
16700 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
16701 bool explain_p
, tree
*result
)
16703 tree rval
= NULL_TREE
;
16706 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
16708 binfo
= TYPE_BINFO (complete_type (arg
));
16711 /* The type could not be completed. */
16712 *result
= NULL_TREE
;
16713 return tbr_incomplete_type
;
16716 /* Walk in inheritance graph order. The search order is not
16717 important, and this avoids multiple walks of virtual bases. */
16718 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
16720 tree r
= try_class_unification (tparms
, targs
, parm
,
16721 BINFO_TYPE (binfo
), explain_p
);
16725 /* If there is more than one satisfactory baseclass, then:
16729 If they yield more than one possible deduced A, the type
16733 if (rval
&& !same_type_p (r
, rval
))
16735 *result
= NULL_TREE
;
16736 return tbr_ambiguous_baseclass
;
16744 return tbr_success
;
16747 /* Returns the level of DECL, which declares a template parameter. */
16750 template_decl_level (tree decl
)
16752 switch (TREE_CODE (decl
))
16755 case TEMPLATE_DECL
:
16756 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
16759 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
16762 gcc_unreachable ();
16767 /* Decide whether ARG can be unified with PARM, considering only the
16768 cv-qualifiers of each type, given STRICT as documented for unify.
16769 Returns nonzero iff the unification is OK on that basis. */
16772 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
16774 int arg_quals
= cp_type_quals (arg
);
16775 int parm_quals
= cp_type_quals (parm
);
16777 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16778 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
16780 /* Although a CVR qualifier is ignored when being applied to a
16781 substituted template parameter ([8.3.2]/1 for example), that
16782 does not allow us to unify "const T" with "int&" because both
16783 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16784 It is ok when we're allowing additional CV qualifiers
16785 at the outer level [14.8.2.1]/3,1st bullet. */
16786 if ((TREE_CODE (arg
) == REFERENCE_TYPE
16787 || TREE_CODE (arg
) == FUNCTION_TYPE
16788 || TREE_CODE (arg
) == METHOD_TYPE
)
16789 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
16792 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
16793 && (parm_quals
& TYPE_QUAL_RESTRICT
))
16797 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
16798 && (arg_quals
& parm_quals
) != parm_quals
)
16801 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
16802 && (parm_quals
& arg_quals
) != arg_quals
)
16808 /* Determines the LEVEL and INDEX for the template parameter PARM. */
16810 template_parm_level_and_index (tree parm
, int* level
, int* index
)
16812 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16813 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16814 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16816 *index
= TEMPLATE_TYPE_IDX (parm
);
16817 *level
= TEMPLATE_TYPE_LEVEL (parm
);
16821 *index
= TEMPLATE_PARM_IDX (parm
);
16822 *level
= TEMPLATE_PARM_LEVEL (parm
);
16826 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
16828 if (unify (TP, TA, P, A, S, EP)) \
16832 /* Unifies the remaining arguments in PACKED_ARGS with the pack
16833 expansion at the end of PACKED_PARMS. Returns 0 if the type
16834 deduction succeeds, 1 otherwise. STRICT is the same as in
16835 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
16836 call argument list. We'll need to adjust the arguments to make them
16837 types. SUBR tells us if this is from a recursive call to
16838 type_unification_real, or for comparing two template argument
16842 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
16843 tree packed_args
, unification_kind_t strict
,
16844 bool subr
, bool explain_p
)
16847 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
16848 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
16849 tree pack
, packs
= NULL_TREE
;
16850 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
16851 int len
= TREE_VEC_LENGTH (packed_args
);
16853 /* Determine the parameter packs we will be deducing from the
16854 pattern, and record their current deductions. */
16855 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
16856 pack
; pack
= TREE_CHAIN (pack
))
16858 tree parm_pack
= TREE_VALUE (pack
);
16861 /* Determine the index and level of this parameter pack. */
16862 template_parm_level_and_index (parm_pack
, &level
, &idx
);
16864 /* Keep track of the parameter packs and their corresponding
16866 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
16867 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
16870 /* Loop through all of the arguments that have not yet been
16871 unified and unify each with the pattern. */
16872 for (i
= start
; i
< len
; i
++)
16875 bool any_explicit
= false;
16876 tree arg
= TREE_VEC_ELT (packed_args
, i
);
16878 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16879 or the element of its argument pack at the current index if
16880 this argument was explicitly specified. */
16881 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16885 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16888 if (TREE_VALUE (pack
)
16889 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
16890 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
16892 any_explicit
= true;
16893 arg
= TREE_VEC_ELT (pargs
, i
- start
);
16895 TMPL_ARG (targs
, level
, idx
) = arg
;
16898 /* If we had explicit template arguments, substitute them into the
16899 pattern before deduction. */
16902 /* Some arguments might still be unspecified or dependent. */
16904 ++processing_template_decl
;
16905 dependent
= any_dependent_template_arguments_p (targs
);
16907 --processing_template_decl
;
16908 parm
= tsubst (pattern
, targs
,
16909 explain_p
? tf_warning_or_error
: tf_none
,
16912 --processing_template_decl
;
16913 if (parm
== error_mark_node
)
16919 /* Unify the pattern with the current argument. */
16920 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
16921 LOOKUP_IMPLICIT
, explain_p
))
16924 /* For each parameter pack, collect the deduced value. */
16925 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16928 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16930 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
16931 TMPL_ARG (targs
, level
, idx
);
16935 /* Verify that the results of unification with the parameter packs
16936 produce results consistent with what we've seen before, and make
16937 the deduced argument packs available. */
16938 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16940 tree old_pack
= TREE_VALUE (pack
);
16941 tree new_args
= TREE_TYPE (pack
);
16942 int i
, len
= TREE_VEC_LENGTH (new_args
);
16944 bool nondeduced_p
= false;
16946 /* By default keep the original deduced argument pack.
16947 If necessary, more specific code is going to update the
16948 resulting deduced argument later down in this function. */
16949 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16950 TMPL_ARG (targs
, level
, idx
) = old_pack
;
16952 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16953 actually deduce anything. */
16954 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
16955 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
16956 nondeduced_p
= true;
16960 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
16962 /* If we had fewer function args than explicit template args,
16963 just use the explicits. */
16964 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16965 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
16966 if (len
< explicit_len
)
16967 new_args
= explicit_args
;
16973 /* Build the deduced *_ARGUMENT_PACK. */
16974 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
16976 result
= make_node (NONTYPE_ARGUMENT_PACK
);
16977 TREE_TYPE (result
) =
16978 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
16979 TREE_CONSTANT (result
) = 1;
16982 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
16984 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
16986 /* Note the deduced argument packs for this parameter
16988 TMPL_ARG (targs
, level
, idx
) = result
;
16990 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
16991 && (ARGUMENT_PACK_ARGS (old_pack
)
16992 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
16994 /* We only had the explicitly-provided arguments before, but
16995 now we have a complete set of arguments. */
16996 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16998 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
16999 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
17000 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
17004 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
17005 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
17007 if (!comp_template_args_with_info (old_args
, new_args
,
17008 &bad_old_arg
, &bad_new_arg
))
17009 /* Inconsistent unification of this parameter pack. */
17010 return unify_parameter_pack_inconsistent (explain_p
,
17016 return unify_success (explain_p
);
17019 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17020 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17021 parameters and return value are as for unify. */
17024 unify_array_domain (tree tparms
, tree targs
,
17025 tree parm_dom
, tree arg_dom
,
17033 /* Our representation of array types uses "N - 1" as the
17034 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17035 not an integer constant. We cannot unify arbitrarily
17036 complex expressions, so we eliminate the MINUS_EXPRs
17038 parm_max
= TYPE_MAX_VALUE (parm_dom
);
17039 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
17042 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
17043 parm_max
= TREE_OPERAND (parm_max
, 0);
17045 arg_max
= TYPE_MAX_VALUE (arg_dom
);
17046 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
17049 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17050 trying to unify the type of a variable with the type
17051 of a template parameter. For example:
17053 template <unsigned int N>
17054 void f (char (&) [N]);
17061 Here, the type of the ARG will be "int [g(i)]", and
17062 may be a SAVE_EXPR, etc. */
17063 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
17064 return unify_vla_arg (explain_p
, arg_dom
);
17065 arg_max
= TREE_OPERAND (arg_max
, 0);
17068 /* If only one of the bounds used a MINUS_EXPR, compensate
17069 by adding one to the other bound. */
17070 if (parm_cst
&& !arg_cst
)
17071 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
17075 else if (arg_cst
&& !parm_cst
)
17076 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
17081 return unify (tparms
, targs
, parm_max
, arg_max
,
17082 UNIFY_ALLOW_INTEGER
, explain_p
);
17085 /* Deduce the value of template parameters. TPARMS is the (innermost)
17086 set of template parameters to a template. TARGS is the bindings
17087 for those template parameters, as determined thus far; TARGS may
17088 include template arguments for outer levels of template parameters
17089 as well. PARM is a parameter to a template function, or a
17090 subcomponent of that parameter; ARG is the corresponding argument.
17091 This function attempts to match PARM with ARG in a manner
17092 consistent with the existing assignments in TARGS. If more values
17093 are deduced, then TARGS is updated.
17095 Returns 0 if the type deduction succeeds, 1 otherwise. The
17096 parameter STRICT is a bitwise or of the following flags:
17099 Require an exact match between PARM and ARG.
17100 UNIFY_ALLOW_MORE_CV_QUAL:
17101 Allow the deduced ARG to be more cv-qualified (by qualification
17102 conversion) than ARG.
17103 UNIFY_ALLOW_LESS_CV_QUAL:
17104 Allow the deduced ARG to be less cv-qualified than ARG.
17105 UNIFY_ALLOW_DERIVED:
17106 Allow the deduced ARG to be a template base class of ARG,
17107 or a pointer to a template base class of the type pointed to by
17109 UNIFY_ALLOW_INTEGER:
17110 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17111 case for more information.
17112 UNIFY_ALLOW_OUTER_LEVEL:
17113 This is the outermost level of a deduction. Used to determine validity
17114 of qualification conversions. A valid qualification conversion must
17115 have const qualified pointers leading up to the inner type which
17116 requires additional CV quals, except at the outer level, where const
17117 is not required [conv.qual]. It would be normal to set this flag in
17118 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17119 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17120 This is the outermost level of a deduction, and PARM can be more CV
17121 qualified at this point.
17122 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17123 This is the outermost level of a deduction, and PARM can be less CV
17124 qualified at this point. */
17127 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
17133 int strict_in
= strict
;
17135 /* I don't think this will do the right thing with respect to types.
17136 But the only case I've seen it in so far has been array bounds, where
17137 signedness is the only information lost, and I think that will be
17139 while (TREE_CODE (parm
) == NOP_EXPR
)
17140 parm
= TREE_OPERAND (parm
, 0);
17142 if (arg
== error_mark_node
)
17143 return unify_invalid (explain_p
);
17144 if (arg
== unknown_type_node
17145 || arg
== init_list_type_node
)
17146 /* We can't deduce anything from this, but we might get all the
17147 template args from other function args. */
17148 return unify_success (explain_p
);
17150 /* If PARM uses template parameters, then we can't bail out here,
17151 even if ARG == PARM, since we won't record unifications for the
17152 template parameters. We might need them if we're trying to
17153 figure out which of two things is more specialized. */
17154 if (arg
== parm
&& !uses_template_parms (parm
))
17155 return unify_success (explain_p
);
17157 /* Handle init lists early, so the rest of the function can assume
17158 we're dealing with a type. */
17159 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
17163 tree orig_parm
= parm
;
17165 /* Replace T with std::initializer_list<T> for deduction. */
17166 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17167 && flag_deduce_init_list
)
17168 parm
= listify (parm
);
17170 if (!is_std_init_list (parm
)
17171 && TREE_CODE (parm
) != ARRAY_TYPE
)
17172 /* We can only deduce from an initializer list argument if the
17173 parameter is std::initializer_list or an array; otherwise this
17174 is a non-deduced context. */
17175 return unify_success (explain_p
);
17177 if (TREE_CODE (parm
) == ARRAY_TYPE
)
17178 elttype
= TREE_TYPE (parm
);
17180 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
17182 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
17184 int elt_strict
= strict
;
17186 if (elt
== error_mark_node
)
17187 return unify_invalid (explain_p
);
17189 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
17191 tree type
= TREE_TYPE (elt
);
17192 /* It should only be possible to get here for a call. */
17193 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
17194 elt_strict
|= maybe_adjust_types_for_deduction
17195 (DEDUCE_CALL
, &elttype
, &type
, elt
);
17199 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
17203 if (TREE_CODE (parm
) == ARRAY_TYPE
)
17205 /* Also deduce from the length of the initializer list. */
17206 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
17207 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
17208 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
17212 /* If the std::initializer_list<T> deduction worked, replace the
17213 deduced A with std::initializer_list<A>. */
17214 if (orig_parm
!= parm
)
17216 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
17217 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17218 targ
= listify (targ
);
17219 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
17221 return unify_success (explain_p
);
17224 /* Immediately reject some pairs that won't unify because of
17225 cv-qualification mismatches. */
17226 if (TREE_CODE (arg
) == TREE_CODE (parm
)
17228 /* It is the elements of the array which hold the cv quals of an array
17229 type, and the elements might be template type parms. We'll check
17230 when we recurse. */
17231 && TREE_CODE (arg
) != ARRAY_TYPE
17232 /* We check the cv-qualifiers when unifying with template type
17233 parameters below. We want to allow ARG `const T' to unify with
17234 PARM `T' for example, when computing which of two templates
17235 is more specialized, for example. */
17236 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
17237 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
17238 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17240 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
17241 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
17242 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
17243 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
17244 strict
&= ~UNIFY_ALLOW_DERIVED
;
17245 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
17246 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
17248 switch (TREE_CODE (parm
))
17250 case TYPENAME_TYPE
:
17252 case UNBOUND_CLASS_TEMPLATE
:
17253 /* In a type which contains a nested-name-specifier, template
17254 argument values cannot be deduced for template parameters used
17255 within the nested-name-specifier. */
17256 return unify_success (explain_p
);
17258 case TEMPLATE_TYPE_PARM
:
17259 case TEMPLATE_TEMPLATE_PARM
:
17260 case BOUND_TEMPLATE_TEMPLATE_PARM
:
17261 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
17262 if (tparm
== error_mark_node
)
17263 return unify_invalid (explain_p
);
17265 if (TEMPLATE_TYPE_LEVEL (parm
)
17266 != template_decl_level (tparm
))
17267 /* The PARM is not one we're trying to unify. Just check
17268 to see if it matches ARG. */
17270 if (TREE_CODE (arg
) == TREE_CODE (parm
)
17271 && (is_auto (parm
) ? is_auto (arg
)
17272 : same_type_p (parm
, arg
)))
17273 return unify_success (explain_p
);
17275 return unify_type_mismatch (explain_p
, parm
, arg
);
17277 idx
= TEMPLATE_TYPE_IDX (parm
);
17278 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17279 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
17280 if (tparm
== error_mark_node
)
17281 return unify_invalid (explain_p
);
17283 /* Check for mixed types and values. */
17284 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17285 && TREE_CODE (tparm
) != TYPE_DECL
)
17286 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
17287 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
17288 gcc_unreachable ();
17290 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17292 /* ARG must be constructed from a template class or a template
17293 template parameter. */
17294 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
17295 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
17296 return unify_template_deduction_failure (explain_p
, parm
, arg
);
17299 tree parmvec
= TYPE_TI_ARGS (parm
);
17300 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
17301 tree full_argvec
= add_to_template_args (targs
, argvec
);
17303 = DECL_INNERMOST_TEMPLATE_PARMS
17304 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
17306 int parm_variadic_p
= 0;
17308 /* The resolution to DR150 makes clear that default
17309 arguments for an N-argument may not be used to bind T
17310 to a template template parameter with fewer than N
17311 parameters. It is not safe to permit the binding of
17312 default arguments as an extension, as that may change
17313 the meaning of a conforming program. Consider:
17315 struct Dense { static const unsigned int dim = 1; };
17317 template <template <typename> class View,
17319 void operator+(float, View<Block> const&);
17321 template <typename Block,
17322 unsigned int Dim = Block::dim>
17323 struct Lvalue_proxy { operator float() const; };
17327 Lvalue_proxy<Dense> p;
17332 Here, if Lvalue_proxy is permitted to bind to View, then
17333 the global operator+ will be used; if they are not, the
17334 Lvalue_proxy will be converted to float. */
17335 if (coerce_template_parms (parm_parms
,
17337 TYPE_TI_TEMPLATE (parm
),
17339 ? tf_warning_or_error
17341 /*require_all_args=*/true,
17342 /*use_default_args=*/false)
17343 == error_mark_node
)
17346 /* Deduce arguments T, i from TT<T> or TT<i>.
17347 We check each element of PARMVEC and ARGVEC individually
17348 rather than the whole TREE_VEC since they can have
17349 different number of elements. */
17351 parmvec
= expand_template_argument_pack (parmvec
);
17352 argvec
= expand_template_argument_pack (argvec
);
17354 len
= TREE_VEC_LENGTH (parmvec
);
17356 /* Check if the parameters end in a pack, making them
17359 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
17360 parm_variadic_p
= 1;
17362 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17363 /* If the template argument list of P contains a pack
17364 expansion that is not the last template argument, the
17365 entire template argument list is a non-deduced
17367 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
17368 return unify_success (explain_p
);
17370 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
17371 return unify_too_few_arguments (explain_p
,
17372 TREE_VEC_LENGTH (argvec
), len
);
17374 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17376 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
17377 TREE_VEC_ELT (parmvec
, i
),
17378 TREE_VEC_ELT (argvec
, i
),
17379 UNIFY_ALLOW_NONE
, explain_p
);
17382 if (parm_variadic_p
17383 && unify_pack_expansion (tparms
, targs
,
17386 /*subr=*/true, explain_p
))
17389 arg
= TYPE_TI_TEMPLATE (arg
);
17391 /* Fall through to deduce template name. */
17394 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
17395 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17397 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
17399 /* Simple cases: Value already set, does match or doesn't. */
17400 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
17401 return unify_success (explain_p
);
17403 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
17407 /* If PARM is `const T' and ARG is only `int', we don't have
17408 a match unless we are allowing additional qualification.
17409 If ARG is `const int' and PARM is just `T' that's OK;
17410 that binds `const int' to `T'. */
17411 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
17413 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17415 /* Consider the case where ARG is `const volatile int' and
17416 PARM is `const T'. Then, T should be `volatile int'. */
17417 arg
= cp_build_qualified_type_real
17418 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
17419 if (arg
== error_mark_node
)
17420 return unify_invalid (explain_p
);
17422 /* Simple cases: Value already set, does match or doesn't. */
17423 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
17424 return unify_success (explain_p
);
17426 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
17428 /* Make sure that ARG is not a variable-sized array. (Note
17429 that were talking about variable-sized arrays (like
17430 `int[n]'), rather than arrays of unknown size (like
17431 `int[]').) We'll get very confused by such a type since
17432 the bound of the array is not constant, and therefore
17433 not mangleable. Besides, such types are not allowed in
17434 ISO C++, so we can do as we please here. We do allow
17435 them for 'auto' deduction, since that isn't ABI-exposed. */
17436 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
17437 return unify_vla_arg (explain_p
, arg
);
17439 /* Strip typedefs as in convert_template_argument. */
17440 arg
= canonicalize_type_argument (arg
, tf_none
);
17443 /* If ARG is a parameter pack or an expansion, we cannot unify
17444 against it unless PARM is also a parameter pack. */
17445 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
17446 && !template_parameter_pack_p (parm
))
17447 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
17449 /* If the argument deduction results is a METHOD_TYPE,
17450 then there is a problem.
17451 METHOD_TYPE doesn't map to any real C++ type the result of
17452 the deduction can not be of that type. */
17453 if (TREE_CODE (arg
) == METHOD_TYPE
)
17454 return unify_method_type_error (explain_p
, arg
);
17456 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
17457 return unify_success (explain_p
);
17459 case TEMPLATE_PARM_INDEX
:
17460 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
17461 if (tparm
== error_mark_node
)
17462 return unify_invalid (explain_p
);
17464 if (TEMPLATE_PARM_LEVEL (parm
)
17465 != template_decl_level (tparm
))
17467 /* The PARM is not one we're trying to unify. Just check
17468 to see if it matches ARG. */
17469 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
17470 && cp_tree_equal (parm
, arg
));
17472 unify_expression_unequal (explain_p
, parm
, arg
);
17476 idx
= TEMPLATE_PARM_IDX (parm
);
17477 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17481 int x
= !cp_tree_equal (targ
, arg
);
17483 unify_inconsistency (explain_p
, parm
, targ
, arg
);
17487 /* [temp.deduct.type] If, in the declaration of a function template
17488 with a non-type template-parameter, the non-type
17489 template-parameter is used in an expression in the function
17490 parameter-list and, if the corresponding template-argument is
17491 deduced, the template-argument type shall match the type of the
17492 template-parameter exactly, except that a template-argument
17493 deduced from an array bound may be of any integral type.
17494 The non-type parameter might use already deduced type parameters. */
17495 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
17496 if (!TREE_TYPE (arg
))
17497 /* Template-parameter dependent expression. Just accept it for now.
17498 It will later be processed in convert_template_argument. */
17500 else if (same_type_p (TREE_TYPE (arg
), tparm
))
17502 else if ((strict
& UNIFY_ALLOW_INTEGER
)
17503 && CP_INTEGRAL_TYPE_P (tparm
))
17504 /* Convert the ARG to the type of PARM; the deduced non-type
17505 template argument must exactly match the types of the
17506 corresponding parameter. */
17507 arg
= fold (build_nop (tparm
, arg
));
17508 else if (uses_template_parms (tparm
))
17509 /* We haven't deduced the type of this parameter yet. Try again
17511 return unify_success (explain_p
);
17513 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
17515 /* If ARG is a parameter pack or an expansion, we cannot unify
17516 against it unless PARM is also a parameter pack. */
17517 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
17518 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
17519 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
17521 arg
= strip_typedefs_expr (arg
);
17522 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
17523 return unify_success (explain_p
);
17527 /* A pointer-to-member constant can be unified only with
17528 another constant. */
17529 if (TREE_CODE (arg
) != PTRMEM_CST
)
17530 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
17532 /* Just unify the class member. It would be useless (and possibly
17533 wrong, depending on the strict flags) to unify also
17534 PTRMEM_CST_CLASS, because we want to be sure that both parm and
17535 arg refer to the same variable, even if through different
17536 classes. For instance:
17538 struct A { int x; };
17541 Unification of &A::x and &B::x must succeed. */
17542 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
17543 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
17548 if (!TYPE_PTR_P (arg
))
17549 return unify_type_mismatch (explain_p
, parm
, arg
);
17551 /* [temp.deduct.call]
17553 A can be another pointer or pointer to member type that can
17554 be converted to the deduced A via a qualification
17555 conversion (_conv.qual_).
17557 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
17558 This will allow for additional cv-qualification of the
17559 pointed-to types if appropriate. */
17561 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
17562 /* The derived-to-base conversion only persists through one
17563 level of pointers. */
17564 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
17566 return unify (tparms
, targs
, TREE_TYPE (parm
),
17567 TREE_TYPE (arg
), strict
, explain_p
);
17570 case REFERENCE_TYPE
:
17571 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
17572 return unify_type_mismatch (explain_p
, parm
, arg
);
17573 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17574 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
17577 if (TREE_CODE (arg
) != ARRAY_TYPE
)
17578 return unify_type_mismatch (explain_p
, parm
, arg
);
17579 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
17580 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
17581 return unify_type_mismatch (explain_p
, parm
, arg
);
17582 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17583 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
17584 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
17585 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
17586 TYPE_DOMAIN (arg
), explain_p
);
17587 return unify_success (explain_p
);
17594 case ENUMERAL_TYPE
:
17597 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17598 return unify_type_mismatch (explain_p
, parm
, arg
);
17600 /* We have already checked cv-qualification at the top of the
17602 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
17603 return unify_type_mismatch (explain_p
, parm
, arg
);
17605 /* As far as unification is concerned, this wins. Later checks
17606 will invalidate it if necessary. */
17607 return unify_success (explain_p
);
17609 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
17610 /* Type INTEGER_CST can come from ordinary constant template args. */
17612 while (TREE_CODE (arg
) == NOP_EXPR
)
17613 arg
= TREE_OPERAND (arg
, 0);
17615 if (TREE_CODE (arg
) != INTEGER_CST
)
17616 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17617 return (tree_int_cst_equal (parm
, arg
)
17618 ? unify_success (explain_p
)
17619 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
17623 int i
, len
, argslen
;
17624 int parm_variadic_p
= 0;
17626 if (TREE_CODE (arg
) != TREE_VEC
)
17627 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17629 len
= TREE_VEC_LENGTH (parm
);
17630 argslen
= TREE_VEC_LENGTH (arg
);
17632 /* Check for pack expansions in the parameters. */
17633 for (i
= 0; i
< len
; ++i
)
17635 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
17638 /* We can unify against something with a trailing
17640 parm_variadic_p
= 1;
17642 /* [temp.deduct.type]/9: If the template argument list of
17643 P contains a pack expansion that is not the last
17644 template argument, the entire template argument list
17645 is a non-deduced context. */
17646 return unify_success (explain_p
);
17650 /* If we don't have enough arguments to satisfy the parameters
17651 (not counting the pack expression at the end), or we have
17652 too many arguments for a parameter list that doesn't end in
17653 a pack expression, we can't unify. */
17654 if (parm_variadic_p
17655 ? argslen
< len
- parm_variadic_p
17657 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
17659 /* Unify all of the parameters that precede the (optional)
17660 pack expression. */
17661 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17663 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
17664 TREE_VEC_ELT (parm
, i
),
17665 TREE_VEC_ELT (arg
, i
),
17666 UNIFY_ALLOW_NONE
, explain_p
);
17668 if (parm_variadic_p
)
17669 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
17671 /*subr=*/true, explain_p
);
17672 return unify_success (explain_p
);
17677 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17678 return unify_type_mismatch (explain_p
, parm
, arg
);
17680 if (TYPE_PTRMEMFUNC_P (parm
))
17682 if (!TYPE_PTRMEMFUNC_P (arg
))
17683 return unify_type_mismatch (explain_p
, parm
, arg
);
17685 return unify (tparms
, targs
,
17686 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
17687 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
17688 strict
, explain_p
);
17691 if (CLASSTYPE_TEMPLATE_INFO (parm
))
17693 tree t
= NULL_TREE
;
17695 if (strict_in
& UNIFY_ALLOW_DERIVED
)
17697 /* First, we try to unify the PARM and ARG directly. */
17698 t
= try_class_unification (tparms
, targs
,
17699 parm
, arg
, explain_p
);
17703 /* Fallback to the special case allowed in
17704 [temp.deduct.call]:
17706 If P is a class, and P has the form
17707 template-id, then A can be a derived class of
17708 the deduced A. Likewise, if P is a pointer to
17709 a class of the form template-id, A can be a
17710 pointer to a derived class pointed to by the
17712 enum template_base_result r
;
17713 r
= get_template_base (tparms
, targs
, parm
, arg
,
17717 return unify_no_common_base (explain_p
, r
, parm
, arg
);
17720 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
17721 && (CLASSTYPE_TI_TEMPLATE (parm
)
17722 == CLASSTYPE_TI_TEMPLATE (arg
)))
17723 /* Perhaps PARM is something like S<U> and ARG is S<int>.
17724 Then, we should unify `int' and `U'. */
17727 /* There's no chance of unification succeeding. */
17728 return unify_type_mismatch (explain_p
, parm
, arg
);
17730 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
17731 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
17733 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
17734 return unify_type_mismatch (explain_p
, parm
, arg
);
17735 return unify_success (explain_p
);
17738 case FUNCTION_TYPE
:
17740 unsigned int nargs
;
17745 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17746 return unify_type_mismatch (explain_p
, parm
, arg
);
17748 /* CV qualifications for methods can never be deduced, they must
17749 match exactly. We need to check them explicitly here,
17750 because type_unification_real treats them as any other
17751 cv-qualified parameter. */
17752 if (TREE_CODE (parm
) == METHOD_TYPE
17753 && (!check_cv_quals_for_unify
17755 class_of_this_parm (arg
),
17756 class_of_this_parm (parm
))))
17757 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17759 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
17760 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
17762 nargs
= list_length (TYPE_ARG_TYPES (arg
));
17763 args
= XALLOCAVEC (tree
, nargs
);
17764 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
17765 a
!= NULL_TREE
&& a
!= void_list_node
;
17766 a
= TREE_CHAIN (a
), ++i
)
17767 args
[i
] = TREE_VALUE (a
);
17770 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
17771 args
, nargs
, 1, DEDUCE_EXACT
,
17772 LOOKUP_NORMAL
, NULL
, explain_p
);
17776 /* Unify a pointer to member with a pointer to member function, which
17777 deduces the type of the member as a function type. */
17778 if (TYPE_PTRMEMFUNC_P (arg
))
17780 /* Check top-level cv qualifiers */
17781 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
17782 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17784 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
17785 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
17786 UNIFY_ALLOW_NONE
, explain_p
);
17788 /* Determine the type of the function we are unifying against. */
17789 tree fntype
= static_fn_type (arg
);
17791 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
17794 if (TREE_CODE (arg
) != OFFSET_TYPE
)
17795 return unify_type_mismatch (explain_p
, parm
, arg
);
17796 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
17797 TYPE_OFFSET_BASETYPE (arg
),
17798 UNIFY_ALLOW_NONE
, explain_p
);
17799 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17800 strict
, explain_p
);
17803 if (DECL_TEMPLATE_PARM_P (parm
))
17804 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
17805 if (arg
!= integral_constant_value (parm
))
17806 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17807 return unify_success (explain_p
);
17810 case TEMPLATE_DECL
:
17811 /* Matched cases are handled by the ARG == PARM test above. */
17812 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17815 /* A non-type template parameter that is a variable should be a
17816 an integral constant, in which case, it whould have been
17817 folded into its (constant) value. So we should not be getting
17818 a variable here. */
17819 gcc_unreachable ();
17821 case TYPE_ARGUMENT_PACK
:
17822 case NONTYPE_ARGUMENT_PACK
:
17823 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
17824 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
17827 case DECLTYPE_TYPE
:
17828 case UNDERLYING_TYPE
:
17829 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
17830 or UNDERLYING_TYPE nodes. */
17831 return unify_success (explain_p
);
17834 /* Unification fails if we hit an error node. */
17835 return unify_invalid (explain_p
);
17838 /* An unresolved overload is a nondeduced context. */
17839 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
17840 return unify_success (explain_p
);
17841 gcc_assert (EXPR_P (parm
));
17843 /* We must be looking at an expression. This can happen with
17847 void foo(S<I>, S<I + 2>);
17849 This is a "nondeduced context":
17853 The nondeduced contexts are:
17855 --A type that is a template-id in which one or more of
17856 the template-arguments is an expression that references
17857 a template-parameter.
17859 In these cases, we assume deduction succeeded, but don't
17860 actually infer any unifications. */
17862 if (!uses_template_parms (parm
)
17863 && !template_args_equal (parm
, arg
))
17864 return unify_expression_unequal (explain_p
, parm
, arg
);
17866 return unify_success (explain_p
);
17869 #undef RECUR_AND_CHECK_FAILURE
17871 /* Note that DECL can be defined in this translation unit, if
17875 mark_definable (tree decl
)
17878 DECL_NOT_REALLY_EXTERN (decl
) = 1;
17879 FOR_EACH_CLONE (clone
, decl
)
17880 DECL_NOT_REALLY_EXTERN (clone
) = 1;
17883 /* Called if RESULT is explicitly instantiated, or is a member of an
17884 explicitly instantiated class. */
17887 mark_decl_instantiated (tree result
, int extern_p
)
17889 SET_DECL_EXPLICIT_INSTANTIATION (result
);
17891 /* If this entity has already been written out, it's too late to
17892 make any modifications. */
17893 if (TREE_ASM_WRITTEN (result
))
17896 /* For anonymous namespace we don't need to do anything. */
17897 if (decl_anon_ns_mem_p (result
))
17899 gcc_assert (!TREE_PUBLIC (result
));
17903 if (TREE_CODE (result
) != FUNCTION_DECL
)
17904 /* The TREE_PUBLIC flag for function declarations will have been
17905 set correctly by tsubst. */
17906 TREE_PUBLIC (result
) = 1;
17908 /* This might have been set by an earlier implicit instantiation. */
17909 DECL_COMDAT (result
) = 0;
17912 DECL_NOT_REALLY_EXTERN (result
) = 0;
17915 mark_definable (result
);
17916 /* Always make artificials weak. */
17917 if (DECL_ARTIFICIAL (result
) && flag_weak
)
17918 comdat_linkage (result
);
17919 /* For WIN32 we also want to put explicit instantiations in
17920 linkonce sections. */
17921 else if (TREE_PUBLIC (result
))
17922 maybe_make_one_only (result
);
17925 /* If EXTERN_P, then this function will not be emitted -- unless
17926 followed by an explicit instantiation, at which point its linkage
17927 will be adjusted. If !EXTERN_P, then this function will be
17928 emitted here. In neither circumstance do we want
17929 import_export_decl to adjust the linkage. */
17930 DECL_INTERFACE_KNOWN (result
) = 1;
17933 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17934 important template arguments. If any are missing, we check whether
17935 they're important by using error_mark_node for substituting into any
17936 args that were used for partial ordering (the ones between ARGS and END)
17937 and seeing if it bubbles up. */
17940 check_undeduced_parms (tree targs
, tree args
, tree end
)
17942 bool found
= false;
17944 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
17945 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
17948 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
17952 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
17953 if (substed
== error_mark_node
)
17959 /* Given two function templates PAT1 and PAT2, return:
17961 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17962 -1 if PAT2 is more specialized than PAT1.
17963 0 if neither is more specialized.
17965 LEN indicates the number of parameters we should consider
17966 (defaulted parameters should not be considered).
17968 The 1998 std underspecified function template partial ordering, and
17969 DR214 addresses the issue. We take pairs of arguments, one from
17970 each of the templates, and deduce them against each other. One of
17971 the templates will be more specialized if all the *other*
17972 template's arguments deduce against its arguments and at least one
17973 of its arguments *does* *not* deduce against the other template's
17974 corresponding argument. Deduction is done as for class templates.
17975 The arguments used in deduction have reference and top level cv
17976 qualifiers removed. Iff both arguments were originally reference
17977 types *and* deduction succeeds in both directions, an lvalue reference
17978 wins against an rvalue reference and otherwise the template
17979 with the more cv-qualified argument wins for that pairing (if
17980 neither is more cv-qualified, they both are equal). Unlike regular
17981 deduction, after all the arguments have been deduced in this way,
17982 we do *not* verify the deduced template argument values can be
17983 substituted into non-deduced contexts.
17985 The logic can be a bit confusing here, because we look at deduce1 and
17986 targs1 to see if pat2 is at least as specialized, and vice versa; if we
17987 can find template arguments for pat1 to make arg1 look like arg2, that
17988 means that arg2 is at least as specialized as arg1. */
17991 more_specialized_fn (tree pat1
, tree pat2
, int len
)
17993 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
17994 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
17995 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
17996 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
17997 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
17998 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
17999 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
18000 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
18001 tree origs1
, origs2
;
18002 bool lose1
= false;
18003 bool lose2
= false;
18005 /* Remove the this parameter from non-static member functions. If
18006 one is a non-static member function and the other is not a static
18007 member function, remove the first parameter from that function
18008 also. This situation occurs for operator functions where we
18009 locate both a member function (with this pointer) and non-member
18010 operator (with explicit first operand). */
18011 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
18013 len
--; /* LEN is the number of significant arguments for DECL1 */
18014 args1
= TREE_CHAIN (args1
);
18015 if (!DECL_STATIC_FUNCTION_P (decl2
))
18016 args2
= TREE_CHAIN (args2
);
18018 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
18020 args2
= TREE_CHAIN (args2
);
18021 if (!DECL_STATIC_FUNCTION_P (decl1
))
18024 args1
= TREE_CHAIN (args1
);
18028 /* If only one is a conversion operator, they are unordered. */
18029 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
18032 /* Consider the return type for a conversion function */
18033 if (DECL_CONV_FN_P (decl1
))
18035 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
18036 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
18040 processing_template_decl
++;
18046 /* Stop when an ellipsis is seen. */
18047 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
18049 tree arg1
= TREE_VALUE (args1
);
18050 tree arg2
= TREE_VALUE (args2
);
18051 int deduce1
, deduce2
;
18057 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
18058 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18060 /* When both arguments are pack expansions, we need only
18061 unify the patterns themselves. */
18062 arg1
= PACK_EXPANSION_PATTERN (arg1
);
18063 arg2
= PACK_EXPANSION_PATTERN (arg2
);
18065 /* This is the last comparison we need to do. */
18069 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
18071 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
18072 arg1
= TREE_TYPE (arg1
);
18073 quals1
= cp_type_quals (arg1
);
18076 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
18078 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
18079 arg2
= TREE_TYPE (arg2
);
18080 quals2
= cp_type_quals (arg2
);
18083 arg1
= TYPE_MAIN_VARIANT (arg1
);
18084 arg2
= TYPE_MAIN_VARIANT (arg2
);
18086 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
18088 int i
, len2
= list_length (args2
);
18089 tree parmvec
= make_tree_vec (1);
18090 tree argvec
= make_tree_vec (len2
);
18093 /* Setup the parameter vector, which contains only ARG1. */
18094 TREE_VEC_ELT (parmvec
, 0) = arg1
;
18096 /* Setup the argument vector, which contains the remaining
18098 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
18099 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
18101 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
18102 argvec
, DEDUCE_EXACT
,
18103 /*subr=*/true, /*explain_p=*/false)
18106 /* We cannot deduce in the other direction, because ARG1 is
18107 a pack expansion but ARG2 is not. */
18110 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18112 int i
, len1
= list_length (args1
);
18113 tree parmvec
= make_tree_vec (1);
18114 tree argvec
= make_tree_vec (len1
);
18117 /* Setup the parameter vector, which contains only ARG1. */
18118 TREE_VEC_ELT (parmvec
, 0) = arg2
;
18120 /* Setup the argument vector, which contains the remaining
18122 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
18123 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
18125 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
18126 argvec
, DEDUCE_EXACT
,
18127 /*subr=*/true, /*explain_p=*/false)
18130 /* We cannot deduce in the other direction, because ARG2 is
18131 a pack expansion but ARG1 is not.*/
18137 /* The normal case, where neither argument is a pack
18139 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
18140 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
18142 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
18143 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
18147 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18148 arg2, then arg2 is not as specialized as arg1. */
18154 /* "If, for a given type, deduction succeeds in both directions
18155 (i.e., the types are identical after the transformations above)
18156 and both P and A were reference types (before being replaced with
18157 the type referred to above):
18158 - if the type from the argument template was an lvalue reference and
18159 the type from the parameter template was not, the argument type is
18160 considered to be more specialized than the other; otherwise,
18161 - if the type from the argument template is more cv-qualified
18162 than the type from the parameter template (as described above),
18163 the argument type is considered to be more specialized than the other;
18165 - neither type is more specialized than the other." */
18167 if (deduce1
&& deduce2
)
18169 if (ref1
&& ref2
&& ref1
!= ref2
)
18176 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
18178 if ((quals1
& quals2
) == quals2
)
18180 if ((quals1
& quals2
) == quals1
)
18185 if (lose1
&& lose2
)
18186 /* We've failed to deduce something in either direction.
18187 These must be unordered. */
18190 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
18191 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18192 /* We have already processed all of the arguments in our
18193 handing of the pack expansion type. */
18196 args1
= TREE_CHAIN (args1
);
18197 args2
= TREE_CHAIN (args2
);
18200 /* "In most cases, all template parameters must have values in order for
18201 deduction to succeed, but for partial ordering purposes a template
18202 parameter may remain without a value provided it is not used in the
18203 types being used for partial ordering."
18205 Thus, if we are missing any of the targs1 we need to substitute into
18206 origs1, then pat2 is not as specialized as pat1. This can happen when
18207 there is a nondeduced context. */
18208 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
18210 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
18213 processing_template_decl
--;
18215 /* All things being equal, if the next argument is a pack expansion
18216 for one function but not for the other, prefer the
18217 non-variadic function. FIXME this is bogus; see c++/41958. */
18219 && args1
&& TREE_VALUE (args1
)
18220 && args2
&& TREE_VALUE (args2
))
18222 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
18223 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
18226 if (lose1
== lose2
)
18234 /* Determine which of two partial specializations of MAIN_TMPL is more
18237 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18238 to the first partial specialization. The TREE_VALUE is the
18239 innermost set of template parameters for the partial
18240 specialization. PAT2 is similar, but for the second template.
18242 Return 1 if the first partial specialization is more specialized;
18243 -1 if the second is more specialized; 0 if neither is more
18246 See [temp.class.order] for information about determining which of
18247 two templates is more specialized. */
18250 more_specialized_class (tree main_tmpl
, tree pat1
, tree pat2
)
18255 bool any_deductions
= false;
18257 tmpl1
= TREE_TYPE (pat1
);
18258 tmpl2
= TREE_TYPE (pat2
);
18260 /* Just like what happens for functions, if we are ordering between
18261 different class template specializations, we may encounter dependent
18262 types in the arguments, and we need our dependency check functions
18263 to behave correctly. */
18264 ++processing_template_decl
;
18265 targs
= get_class_bindings (main_tmpl
, TREE_VALUE (pat1
),
18266 CLASSTYPE_TI_ARGS (tmpl1
),
18267 CLASSTYPE_TI_ARGS (tmpl2
));
18271 any_deductions
= true;
18274 targs
= get_class_bindings (main_tmpl
, TREE_VALUE (pat2
),
18275 CLASSTYPE_TI_ARGS (tmpl2
),
18276 CLASSTYPE_TI_ARGS (tmpl1
));
18280 any_deductions
= true;
18282 --processing_template_decl
;
18284 /* In the case of a tie where at least one of the class templates
18285 has a parameter pack at the end, the template with the most
18286 non-packed parameters wins. */
18289 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
18290 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
18292 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
18293 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
18294 int len1
= TREE_VEC_LENGTH (args1
);
18295 int len2
= TREE_VEC_LENGTH (args2
);
18297 /* We don't count the pack expansion at the end. */
18298 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
18300 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
18305 else if (len1
< len2
)
18312 /* Return the template arguments that will produce the function signature
18313 DECL from the function template FN, with the explicit template
18314 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18315 also match. Return NULL_TREE if no satisfactory arguments could be
18319 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
18321 int ntparms
= DECL_NTPARMS (fn
);
18322 tree targs
= make_tree_vec (ntparms
);
18323 tree decl_type
= TREE_TYPE (decl
);
18324 tree decl_arg_types
;
18326 unsigned int nargs
, ix
;
18329 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
18331 /* Never do unification on the 'this' parameter. */
18332 decl_arg_types
= skip_artificial_parms_for (decl
,
18333 TYPE_ARG_TYPES (decl_type
));
18335 nargs
= list_length (decl_arg_types
);
18336 args
= XALLOCAVEC (tree
, nargs
);
18337 for (arg
= decl_arg_types
, ix
= 0;
18338 arg
!= NULL_TREE
&& arg
!= void_list_node
;
18339 arg
= TREE_CHAIN (arg
), ++ix
)
18340 args
[ix
] = TREE_VALUE (arg
);
18342 if (fn_type_unification (fn
, explicit_args
, targs
,
18344 (check_rettype
|| DECL_CONV_FN_P (fn
)
18345 ? TREE_TYPE (decl_type
) : NULL_TREE
),
18346 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
18348 == error_mark_node
)
18354 /* Return the innermost template arguments that, when applied to a partial
18355 specialization of MAIN_TMPL whose innermost template parameters are
18356 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
18359 For example, suppose we have:
18361 template <class T, class U> struct S {};
18362 template <class T> struct S<T*, int> {};
18364 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
18365 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
18366 int}. The resulting vector will be {double}, indicating that `T'
18367 is bound to `double'. */
18370 get_class_bindings (tree main_tmpl
, tree tparms
, tree spec_args
, tree args
)
18372 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
18374 tree innermost_deduced_args
;
18376 innermost_deduced_args
= make_tree_vec (ntparms
);
18377 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
18379 deduced_args
= copy_node (args
);
18380 SET_TMPL_ARGS_LEVEL (deduced_args
,
18381 TMPL_ARGS_DEPTH (deduced_args
),
18382 innermost_deduced_args
);
18385 deduced_args
= innermost_deduced_args
;
18387 if (unify (tparms
, deduced_args
,
18388 INNERMOST_TEMPLATE_ARGS (spec_args
),
18389 INNERMOST_TEMPLATE_ARGS (args
),
18390 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
18393 for (i
= 0; i
< ntparms
; ++i
)
18394 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
18397 /* Verify that nondeduced template arguments agree with the type
18398 obtained from argument deduction.
18402 struct A { typedef int X; };
18403 template <class T, class U> struct C {};
18404 template <class T> struct C<T, typename T::X> {};
18406 Then with the instantiation `C<A, int>', we can deduce that
18407 `T' is `A' but unify () does not check whether `typename T::X'
18409 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
18410 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (main_tmpl
),
18411 spec_args
, main_tmpl
,
18412 tf_none
, false, false);
18413 if (spec_args
== error_mark_node
18414 /* We only need to check the innermost arguments; the other
18415 arguments will always agree. */
18416 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
18417 INNERMOST_TEMPLATE_ARGS (args
)))
18420 /* Now that we have bindings for all of the template arguments,
18421 ensure that the arguments deduced for the template template
18422 parameters have compatible template parameter lists. See the use
18423 of template_template_parm_bindings_ok_p in fn_type_unification
18424 for more information. */
18425 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
18428 return deduced_args
;
18431 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
18432 Return the TREE_LIST node with the most specialized template, if
18433 any. If there is no most specialized template, the error_mark_node
18436 Note that this function does not look at, or modify, the
18437 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
18438 returned is one of the elements of INSTANTIATIONS, callers may
18439 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
18440 and retrieve it from the value returned. */
18443 most_specialized_instantiation (tree templates
)
18447 ++processing_template_decl
;
18450 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
18454 if (get_bindings (TREE_VALUE (champ
),
18455 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
18456 NULL_TREE
, /*check_ret=*/true))
18459 if (get_bindings (TREE_VALUE (fn
),
18460 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
18461 NULL_TREE
, /*check_ret=*/true))
18468 /* Equally specialized, move to next function. If there
18469 is no next function, nothing's most specialized. */
18470 fn
= TREE_CHAIN (fn
);
18478 /* Now verify that champ is better than everything earlier in the
18479 instantiation list. */
18480 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
18481 if (get_bindings (TREE_VALUE (champ
),
18482 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
18483 NULL_TREE
, /*check_ret=*/true)
18484 || !get_bindings (TREE_VALUE (fn
),
18485 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
18486 NULL_TREE
, /*check_ret=*/true))
18492 processing_template_decl
--;
18495 return error_mark_node
;
18500 /* If DECL is a specialization of some template, return the most
18501 general such template. Otherwise, returns NULL_TREE.
18503 For example, given:
18505 template <class T> struct S { template <class U> void f(U); };
18507 if TMPL is `template <class U> void S<int>::f(U)' this will return
18508 the full template. This function will not trace past partial
18509 specializations, however. For example, given in addition:
18511 template <class T> struct S<T*> { template <class U> void f(U); };
18513 if TMPL is `template <class U> void S<int*>::f(U)' this will return
18514 `template <class T> template <class U> S<T*>::f(U)'. */
18517 most_general_template (tree decl
)
18519 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
18520 an immediate specialization. */
18521 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18523 if (DECL_TEMPLATE_INFO (decl
)) {
18524 decl
= DECL_TI_TEMPLATE (decl
);
18526 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
18527 template friend. */
18528 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
18534 /* Look for more and more general templates. */
18535 while (DECL_TEMPLATE_INFO (decl
))
18537 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
18538 (See cp-tree.h for details.) */
18539 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
18542 if (CLASS_TYPE_P (TREE_TYPE (decl
))
18543 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
18546 /* Stop if we run into an explicitly specialized class template. */
18547 if (!DECL_NAMESPACE_SCOPE_P (decl
)
18548 && DECL_CONTEXT (decl
)
18549 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
18552 decl
= DECL_TI_TEMPLATE (decl
);
18558 /* Return the most specialized of the class template partial
18559 specializations of TMPL which can produce TYPE, a specialization of
18560 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
18561 a _TYPE node corresponding to the partial specialization, while the
18562 TREE_PURPOSE is the set of template arguments that must be
18563 substituted into the TREE_TYPE in order to generate TYPE.
18565 If the choice of partial specialization is ambiguous, a diagnostic
18566 is issued, and the error_mark_node is returned. If there are no
18567 partial specializations of TMPL matching TYPE, then NULL_TREE is
18571 most_specialized_class (tree type
, tree tmpl
, tsubst_flags_t complain
)
18573 tree list
= NULL_TREE
;
18579 tree outer_args
= NULL_TREE
;
18581 tmpl
= most_general_template (tmpl
);
18582 args
= CLASSTYPE_TI_ARGS (type
);
18584 /* For determining which partial specialization to use, only the
18585 innermost args are interesting. */
18586 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
18588 outer_args
= strip_innermost_template_args (args
, 1);
18589 args
= INNERMOST_TEMPLATE_ARGS (args
);
18592 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
18594 tree partial_spec_args
;
18596 tree spec_tmpl
= TREE_VALUE (t
);
18597 tree orig_parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
18599 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
18601 ++processing_template_decl
;
18605 /* Discard the outer levels of args, and then substitute in the
18606 template args from the enclosing class. */
18607 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
18608 partial_spec_args
= tsubst_template_args
18609 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
18611 /* And the same for the partial specialization TEMPLATE_DECL. */
18612 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
18615 partial_spec_args
=
18616 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
18617 add_to_template_args (outer_args
,
18618 partial_spec_args
),
18620 /*require_all_args=*/true,
18621 /*use_default_args=*/true);
18623 --processing_template_decl
;
18625 if (partial_spec_args
== error_mark_node
)
18626 return error_mark_node
;
18627 if (spec_tmpl
== error_mark_node
)
18628 return error_mark_node
;
18630 tree parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
18631 spec_args
= get_class_bindings (tmpl
, parms
,
18637 spec_args
= add_to_template_args (outer_args
, spec_args
);
18638 list
= tree_cons (spec_args
, orig_parms
, list
);
18639 TREE_TYPE (list
) = TREE_TYPE (t
);
18646 ambiguous_p
= false;
18649 t
= TREE_CHAIN (t
);
18650 for (; t
; t
= TREE_CHAIN (t
))
18652 fate
= more_specialized_class (tmpl
, champ
, t
);
18659 t
= TREE_CHAIN (t
);
18662 ambiguous_p
= true;
18671 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
18673 fate
= more_specialized_class (tmpl
, champ
, t
);
18676 ambiguous_p
= true;
18684 char *spaces
= NULL
;
18685 if (!(complain
& tf_error
))
18686 return error_mark_node
;
18687 error ("ambiguous class template instantiation for %q#T", type
);
18688 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
18689 for (t
= list
; t
; t
= TREE_CHAIN (t
))
18691 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
18692 spaces
= spaces
? spaces
: get_spaces (str
);
18695 return error_mark_node
;
18701 /* Explicitly instantiate DECL. */
18704 do_decl_instantiation (tree decl
, tree storage
)
18706 tree result
= NULL_TREE
;
18709 if (!decl
|| decl
== error_mark_node
)
18710 /* An error occurred, for which grokdeclarator has already issued
18711 an appropriate message. */
18713 else if (! DECL_LANG_SPECIFIC (decl
))
18715 error ("explicit instantiation of non-template %q#D", decl
);
18718 else if (VAR_P (decl
))
18720 /* There is an asymmetry here in the way VAR_DECLs and
18721 FUNCTION_DECLs are handled by grokdeclarator. In the case of
18722 the latter, the DECL we get back will be marked as a
18723 template instantiation, and the appropriate
18724 DECL_TEMPLATE_INFO will be set up. This does not happen for
18725 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
18726 should handle VAR_DECLs as it currently handles
18728 if (!DECL_CLASS_SCOPE_P (decl
))
18730 error ("%qD is not a static data member of a class template", decl
);
18733 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
18734 if (!result
|| !VAR_P (result
))
18736 error ("no matching template for %qD found", decl
);
18739 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
18741 error ("type %qT for explicit instantiation %qD does not match "
18742 "declared type %qT", TREE_TYPE (result
), decl
,
18747 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
18749 error ("explicit instantiation of %q#D", decl
);
18755 /* Check for various error cases. Note that if the explicit
18756 instantiation is valid the RESULT will currently be marked as an
18757 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
18758 until we get here. */
18760 if (DECL_TEMPLATE_SPECIALIZATION (result
))
18762 /* DR 259 [temp.spec].
18764 Both an explicit instantiation and a declaration of an explicit
18765 specialization shall not appear in a program unless the explicit
18766 instantiation follows a declaration of the explicit specialization.
18768 For a given set of template parameters, if an explicit
18769 instantiation of a template appears after a declaration of an
18770 explicit specialization for that template, the explicit
18771 instantiation has no effect. */
18774 else if (DECL_EXPLICIT_INSTANTIATION (result
))
18778 No program shall explicitly instantiate any template more
18781 We check DECL_NOT_REALLY_EXTERN so as not to complain when
18782 the first instantiation was `extern' and the second is not,
18783 and EXTERN_P for the opposite case. */
18784 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
18785 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
18786 /* If an "extern" explicit instantiation follows an ordinary
18787 explicit instantiation, the template is instantiated. */
18791 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
18793 error ("no matching template for %qD found", result
);
18796 else if (!DECL_TEMPLATE_INFO (result
))
18798 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
18802 if (storage
== NULL_TREE
)
18804 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18806 if (!in_system_header
&& (cxx_dialect
== cxx98
))
18807 pedwarn (input_location
, OPT_Wpedantic
,
18808 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
18813 error ("storage class %qD applied to template instantiation", storage
);
18815 check_explicit_instantiation_namespace (result
);
18816 mark_decl_instantiated (result
, extern_p
);
18818 instantiate_decl (result
, /*defer_ok=*/1,
18819 /*expl_inst_class_mem_p=*/false);
18823 mark_class_instantiated (tree t
, int extern_p
)
18825 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
18826 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
18827 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
18828 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
18831 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
18832 rest_of_type_compilation (t
, 1);
18836 /* Called from do_type_instantiation through binding_table_foreach to
18837 do recursive instantiation for the type bound in ENTRY. */
18839 bt_instantiate_type_proc (binding_entry entry
, void *data
)
18841 tree storage
= *(tree
*) data
;
18843 if (MAYBE_CLASS_TYPE_P (entry
->type
)
18844 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
18845 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
18848 /* Called from do_type_instantiation to instantiate a member
18849 (a member function or a static member variable) of an
18850 explicitly instantiated class template. */
18852 instantiate_class_member (tree decl
, int extern_p
)
18854 mark_decl_instantiated (decl
, extern_p
);
18856 instantiate_decl (decl
, /*defer_ok=*/1,
18857 /*expl_inst_class_mem_p=*/true);
18860 /* Perform an explicit instantiation of template class T. STORAGE, if
18861 non-null, is the RID for extern, inline or static. COMPLAIN is
18862 nonzero if this is called from the parser, zero if called recursively,
18863 since the standard is unclear (as detailed below). */
18866 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
18871 int previous_instantiation_extern_p
= 0;
18873 if (TREE_CODE (t
) == TYPE_DECL
)
18876 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
18879 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
18881 error ("explicit instantiation of non-class template %qD", tmpl
);
18883 error ("explicit instantiation of non-template type %qT", t
);
18889 if (!COMPLETE_TYPE_P (t
))
18891 if (complain
& tf_error
)
18892 error ("explicit instantiation of %q#T before definition of template",
18897 if (storage
!= NULL_TREE
)
18899 if (!in_system_header
)
18901 if (storage
== ridpointers
[(int) RID_EXTERN
])
18903 if (cxx_dialect
== cxx98
)
18904 pedwarn (input_location
, OPT_Wpedantic
,
18905 "ISO C++ 1998 forbids the use of %<extern%> on "
18906 "explicit instantiations");
18909 pedwarn (input_location
, OPT_Wpedantic
,
18910 "ISO C++ forbids the use of %qE"
18911 " on explicit instantiations", storage
);
18914 if (storage
== ridpointers
[(int) RID_INLINE
])
18916 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18918 else if (storage
== ridpointers
[(int) RID_STATIC
])
18922 error ("storage class %qD applied to template instantiation",
18928 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
18930 /* DR 259 [temp.spec].
18932 Both an explicit instantiation and a declaration of an explicit
18933 specialization shall not appear in a program unless the explicit
18934 instantiation follows a declaration of the explicit specialization.
18936 For a given set of template parameters, if an explicit
18937 instantiation of a template appears after a declaration of an
18938 explicit specialization for that template, the explicit
18939 instantiation has no effect. */
18942 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
18946 No program shall explicitly instantiate any template more
18949 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18950 instantiation was `extern'. If EXTERN_P then the second is.
18951 These cases are OK. */
18952 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
18954 if (!previous_instantiation_extern_p
&& !extern_p
18955 && (complain
& tf_error
))
18956 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
18958 /* If we've already instantiated the template, just return now. */
18959 if (!CLASSTYPE_INTERFACE_ONLY (t
))
18963 check_explicit_instantiation_namespace (TYPE_NAME (t
));
18964 mark_class_instantiated (t
, extern_p
);
18972 /* In contrast to implicit instantiation, where only the
18973 declarations, and not the definitions, of members are
18974 instantiated, we have here:
18978 The explicit instantiation of a class template specialization
18979 implies the instantiation of all of its members not
18980 previously explicitly specialized in the translation unit
18981 containing the explicit instantiation.
18983 Of course, we can't instantiate member template classes, since
18984 we don't have any arguments for them. Note that the standard
18985 is unclear on whether the instantiation of the members are
18986 *explicit* instantiations or not. However, the most natural
18987 interpretation is that it should be an explicit instantiation. */
18990 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18991 if (TREE_CODE (tmp
) == FUNCTION_DECL
18992 && DECL_TEMPLATE_INSTANTIATION (tmp
))
18993 instantiate_class_member (tmp
, extern_p
);
18995 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18996 if (VAR_P (tmp
) && DECL_TEMPLATE_INSTANTIATION (tmp
))
18997 instantiate_class_member (tmp
, extern_p
);
18999 if (CLASSTYPE_NESTED_UTDS (t
))
19000 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
19001 bt_instantiate_type_proc
, &storage
);
19005 /* Given a function DECL, which is a specialization of TMPL, modify
19006 DECL to be a re-instantiation of TMPL with the same template
19007 arguments. TMPL should be the template into which tsubst'ing
19008 should occur for DECL, not the most general template.
19010 One reason for doing this is a scenario like this:
19013 void f(const T&, int i);
19015 void g() { f(3, 7); }
19018 void f(const T& t, const int i) { }
19020 Note that when the template is first instantiated, with
19021 instantiate_template, the resulting DECL will have no name for the
19022 first parameter, and the wrong type for the second. So, when we go
19023 to instantiate the DECL, we regenerate it. */
19026 regenerate_decl_from_template (tree decl
, tree tmpl
)
19028 /* The arguments used to instantiate DECL, from the most general
19033 args
= DECL_TI_ARGS (decl
);
19034 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
19036 /* Make sure that we can see identifiers, and compute access
19038 push_access_scope (decl
);
19040 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19048 args_depth
= TMPL_ARGS_DEPTH (args
);
19049 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
19050 if (args_depth
> parms_depth
)
19051 args
= get_innermost_template_args (args
, parms_depth
);
19053 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
19054 args
, tf_error
, NULL_TREE
,
19055 /*defer_ok*/false);
19056 if (specs
&& specs
!= error_mark_node
)
19057 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
19060 /* Merge parameter declarations. */
19061 decl_parm
= skip_artificial_parms_for (decl
,
19062 DECL_ARGUMENTS (decl
));
19064 = skip_artificial_parms_for (code_pattern
,
19065 DECL_ARGUMENTS (code_pattern
));
19066 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
19071 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
19072 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
19073 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
19075 parm_type
= type_decays_to (parm_type
);
19076 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
19077 TREE_TYPE (decl_parm
) = parm_type
;
19078 attributes
= DECL_ATTRIBUTES (pattern_parm
);
19079 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
19081 DECL_ATTRIBUTES (decl_parm
) = attributes
;
19082 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
19084 decl_parm
= DECL_CHAIN (decl_parm
);
19085 pattern_parm
= DECL_CHAIN (pattern_parm
);
19087 /* Merge any parameters that match with the function parameter
19089 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
19092 tree expanded_types
;
19093 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19094 the parameters in this function parameter pack. */
19095 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
19096 args
, tf_error
, NULL_TREE
);
19097 len
= TREE_VEC_LENGTH (expanded_types
);
19098 for (i
= 0; i
< len
; i
++)
19103 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
19104 /* Rename the parameter to include the index. */
19105 DECL_NAME (decl_parm
) =
19106 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
19107 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
19108 parm_type
= type_decays_to (parm_type
);
19109 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
19110 TREE_TYPE (decl_parm
) = parm_type
;
19111 attributes
= DECL_ATTRIBUTES (pattern_parm
);
19112 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
19114 DECL_ATTRIBUTES (decl_parm
) = attributes
;
19115 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
19117 decl_parm
= DECL_CHAIN (decl_parm
);
19120 /* Merge additional specifiers from the CODE_PATTERN. */
19121 if (DECL_DECLARED_INLINE_P (code_pattern
)
19122 && !DECL_DECLARED_INLINE_P (decl
))
19123 DECL_DECLARED_INLINE_P (decl
) = 1;
19125 else if (VAR_P (decl
))
19127 DECL_INITIAL (decl
) =
19128 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
19129 tf_error
, DECL_TI_TEMPLATE (decl
),
19130 /*integral_constant_expression_p=*/false);
19131 if (VAR_HAD_UNKNOWN_BOUND (decl
))
19132 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
19133 tf_error
, DECL_TI_TEMPLATE (decl
));
19136 gcc_unreachable ();
19138 pop_access_scope (decl
);
19141 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19142 substituted to get DECL. */
19145 template_for_substitution (tree decl
)
19147 tree tmpl
= DECL_TI_TEMPLATE (decl
);
19149 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19150 for the instantiation. This is not always the most general
19151 template. Consider, for example:
19154 struct S { template <class U> void f();
19155 template <> void f<int>(); };
19157 and an instantiation of S<double>::f<int>. We want TD to be the
19158 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19159 while (/* An instantiation cannot have a definition, so we need a
19160 more general template. */
19161 DECL_TEMPLATE_INSTANTIATION (tmpl
)
19162 /* We must also deal with friend templates. Given:
19164 template <class T> struct S {
19165 template <class U> friend void f() {};
19168 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19169 so far as the language is concerned, but that's still
19170 where we get the pattern for the instantiation from. On
19171 other hand, if the definition comes outside the class, say:
19173 template <class T> struct S {
19174 template <class U> friend void f();
19176 template <class U> friend void f() {}
19178 we don't need to look any further. That's what the check for
19179 DECL_INITIAL is for. */
19180 || (TREE_CODE (decl
) == FUNCTION_DECL
19181 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
19182 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
19184 /* The present template, TD, should not be a definition. If it
19185 were a definition, we should be using it! Note that we
19186 cannot restructure the loop to just keep going until we find
19187 a template with a definition, since that might go too far if
19188 a specialization was declared, but not defined. */
19189 gcc_assert (!VAR_P (decl
)
19190 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
19192 /* Fetch the more general template. */
19193 tmpl
= DECL_TI_TEMPLATE (tmpl
);
19199 /* Returns true if we need to instantiate this template instance even if we
19200 know we aren't going to emit it.. */
19203 always_instantiate_p (tree decl
)
19205 /* We always instantiate inline functions so that we can inline them. An
19206 explicit instantiation declaration prohibits implicit instantiation of
19207 non-inline functions. With high levels of optimization, we would
19208 normally inline non-inline functions -- but we're not allowed to do
19209 that for "extern template" functions. Therefore, we check
19210 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19211 return ((TREE_CODE (decl
) == FUNCTION_DECL
19212 && (DECL_DECLARED_INLINE_P (decl
)
19213 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
19214 /* And we need to instantiate static data members so that
19215 their initializers are available in integral constant
19218 && decl_maybe_constant_var_p (decl
)));
19221 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19222 instantiate it now, modifying TREE_TYPE (fn). */
19225 maybe_instantiate_noexcept (tree fn
)
19227 tree fntype
, spec
, noex
, clone
;
19229 if (DECL_CLONED_FUNCTION_P (fn
))
19230 fn
= DECL_CLONED_FUNCTION (fn
);
19231 fntype
= TREE_TYPE (fn
);
19232 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
19234 if (!DEFERRED_NOEXCEPT_SPEC_P (spec
))
19237 noex
= TREE_PURPOSE (spec
);
19239 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
19241 if (push_tinst_level (fn
))
19243 push_access_scope (fn
);
19244 push_deferring_access_checks (dk_no_deferred
);
19245 input_location
= DECL_SOURCE_LOCATION (fn
);
19246 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
19247 DEFERRED_NOEXCEPT_ARGS (noex
),
19248 tf_warning_or_error
, fn
,
19249 /*function_p=*/false,
19250 /*integral_constant_expression_p=*/true);
19251 pop_deferring_access_checks ();
19252 pop_access_scope (fn
);
19253 pop_tinst_level ();
19254 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
19255 if (spec
== error_mark_node
)
19256 spec
= noexcept_false_spec
;
19259 spec
= noexcept_false_spec
;
19263 /* This is an implicitly declared function, so NOEX is a list of
19264 other functions to evaluate and merge. */
19266 spec
= noexcept_true_spec
;
19267 for (elt
= noex
; elt
; elt
= OVL_NEXT (elt
))
19269 tree fn
= OVL_CURRENT (elt
);
19271 maybe_instantiate_noexcept (fn
);
19272 subspec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
19273 spec
= merge_exception_specifiers (spec
, subspec
, NULL_TREE
);
19277 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
19279 FOR_EACH_CLONE (clone
, fn
)
19281 if (TREE_TYPE (clone
) == fntype
)
19282 TREE_TYPE (clone
) = TREE_TYPE (fn
);
19284 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
19288 /* Produce the definition of D, a _DECL generated from a template. If
19289 DEFER_OK is nonzero, then we don't have to actually do the
19290 instantiation now; we just have to do it sometime. Normally it is
19291 an error if this is an explicit instantiation but D is undefined.
19292 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19293 explicitly instantiated class template. */
19296 instantiate_decl (tree d
, int defer_ok
,
19297 bool expl_inst_class_mem_p
)
19299 tree tmpl
= DECL_TI_TEMPLATE (d
);
19306 bool pattern_defined
;
19307 location_t saved_loc
= input_location
;
19308 int saved_unevaluated_operand
= cp_unevaluated_operand
;
19309 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
19314 /* This function should only be used to instantiate templates for
19315 functions and static member variables. */
19316 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
19318 /* Variables are never deferred; if instantiation is required, they
19319 are instantiated right away. That allows for better code in the
19320 case that an expression refers to the value of the variable --
19321 if the variable has a constant value the referring expression can
19322 take advantage of that fact. */
19324 || DECL_DECLARED_CONSTEXPR_P (d
))
19327 /* Don't instantiate cloned functions. Instead, instantiate the
19328 functions they cloned. */
19329 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
19330 d
= DECL_CLONED_FUNCTION (d
);
19332 if (DECL_TEMPLATE_INSTANTIATED (d
)
19333 || (TREE_CODE (d
) == FUNCTION_DECL
19334 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
19335 || DECL_TEMPLATE_SPECIALIZATION (d
))
19336 /* D has already been instantiated or explicitly specialized, so
19337 there's nothing for us to do here.
19339 It might seem reasonable to check whether or not D is an explicit
19340 instantiation, and, if so, stop here. But when an explicit
19341 instantiation is deferred until the end of the compilation,
19342 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
19343 the instantiation. */
19346 /* Check to see whether we know that this template will be
19347 instantiated in some other file, as with "extern template"
19349 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
19351 /* In general, we do not instantiate such templates. */
19352 if (external_p
&& !always_instantiate_p (d
))
19355 gen_tmpl
= most_general_template (tmpl
);
19356 gen_args
= DECL_TI_ARGS (d
);
19358 if (tmpl
!= gen_tmpl
)
19359 /* We should already have the extra args. */
19360 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
19361 == TMPL_ARGS_DEPTH (gen_args
));
19362 /* And what's in the hash table should match D. */
19363 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
19364 || spec
== NULL_TREE
);
19366 /* This needs to happen before any tsubsting. */
19367 if (! push_tinst_level (d
))
19370 timevar_push (TV_TEMPLATE_INST
);
19372 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
19373 for the instantiation. */
19374 td
= template_for_substitution (d
);
19375 code_pattern
= DECL_TEMPLATE_RESULT (td
);
19377 /* We should never be trying to instantiate a member of a class
19378 template or partial specialization. */
19379 gcc_assert (d
!= code_pattern
);
19381 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
19382 || DECL_TEMPLATE_SPECIALIZATION (td
))
19383 /* In the case of a friend template whose definition is provided
19384 outside the class, we may have too many arguments. Drop the
19385 ones we don't need. The same is true for specializations. */
19386 args
= get_innermost_template_args
19387 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
19391 if (TREE_CODE (d
) == FUNCTION_DECL
)
19392 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
19393 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
));
19395 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
19397 /* We may be in the middle of deferred access check. Disable it now. */
19398 push_deferring_access_checks (dk_no_deferred
);
19400 /* Unless an explicit instantiation directive has already determined
19401 the linkage of D, remember that a definition is available for
19403 if (pattern_defined
19404 && !DECL_INTERFACE_KNOWN (d
)
19405 && !DECL_NOT_REALLY_EXTERN (d
))
19406 mark_definable (d
);
19408 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
19409 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
19410 input_location
= DECL_SOURCE_LOCATION (d
);
19412 /* If D is a member of an explicitly instantiated class template,
19413 and no definition is available, treat it like an implicit
19415 if (!pattern_defined
&& expl_inst_class_mem_p
19416 && DECL_EXPLICIT_INSTANTIATION (d
))
19418 /* Leave linkage flags alone on instantiations with anonymous
19420 if (TREE_PUBLIC (d
))
19422 DECL_NOT_REALLY_EXTERN (d
) = 0;
19423 DECL_INTERFACE_KNOWN (d
) = 0;
19425 SET_DECL_IMPLICIT_INSTANTIATION (d
);
19428 if (TREE_CODE (d
) == FUNCTION_DECL
)
19429 maybe_instantiate_noexcept (d
);
19431 /* Defer all other templates, unless we have been explicitly
19432 forbidden from doing so. */
19433 if (/* If there is no definition, we cannot instantiate the
19436 /* If it's OK to postpone instantiation, do so. */
19438 /* If this is a static data member that will be defined
19439 elsewhere, we don't want to instantiate the entire data
19440 member, but we do want to instantiate the initializer so that
19441 we can substitute that elsewhere. */
19442 || (external_p
&& VAR_P (d
)))
19444 /* The definition of the static data member is now required so
19445 we must substitute the initializer. */
19447 && !DECL_INITIAL (d
)
19448 && DECL_INITIAL (code_pattern
))
19452 bool const_init
= false;
19454 ns
= decl_namespace_context (d
);
19455 push_nested_namespace (ns
);
19456 push_nested_class (DECL_CONTEXT (d
));
19457 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
19459 tf_warning_or_error
, NULL_TREE
,
19460 /*integral_constant_expression_p=*/false);
19461 /* Make sure the initializer is still constant, in case of
19462 circular dependency (template/instantiate6.C). */
19464 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
19465 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
19466 /*asmspec_tree=*/NULL_TREE
,
19467 LOOKUP_ONLYCONVERTING
);
19468 pop_nested_class ();
19469 pop_nested_namespace (ns
);
19472 /* We restore the source position here because it's used by
19473 add_pending_template. */
19474 input_location
= saved_loc
;
19476 if (at_eof
&& !pattern_defined
19477 && DECL_EXPLICIT_INSTANTIATION (d
)
19478 && DECL_NOT_REALLY_EXTERN (d
))
19481 The definition of a non-exported function template, a
19482 non-exported member function template, or a non-exported
19483 member function or static data member of a class template
19484 shall be present in every translation unit in which it is
19485 explicitly instantiated. */
19486 permerror (input_location
, "explicit instantiation of %qD "
19487 "but no definition available", d
);
19489 /* If we're in unevaluated context, we just wanted to get the
19490 constant value; this isn't an odr use, so don't queue
19491 a full instantiation. */
19492 if (cp_unevaluated_operand
!= 0)
19494 /* ??? Historically, we have instantiated inline functions, even
19495 when marked as "extern template". */
19496 if (!(external_p
&& VAR_P (d
)))
19497 add_pending_template (d
);
19500 /* Tell the repository that D is available in this translation unit
19501 -- and see if it is supposed to be instantiated here. */
19502 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
19504 /* In a PCH file, despite the fact that the repository hasn't
19505 requested instantiation in the PCH it is still possible that
19506 an instantiation will be required in a file that includes the
19509 add_pending_template (d
);
19510 /* Instantiate inline functions so that the inliner can do its
19511 job, even though we'll not be emitting a copy of this
19513 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
19517 fn_context
= decl_function_context (d
);
19518 nested
= (current_function_decl
!= NULL_TREE
);
19520 push_to_top_level ();
19524 push_function_context ();
19525 cp_unevaluated_operand
= 0;
19526 c_inhibit_evaluation_warnings
= 0;
19529 /* Mark D as instantiated so that recursive calls to
19530 instantiate_decl do not try to instantiate it again. */
19531 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
19533 /* Regenerate the declaration in case the template has been modified
19534 by a subsequent redeclaration. */
19535 regenerate_decl_from_template (d
, td
);
19537 /* We already set the file and line above. Reset them now in case
19538 they changed as a result of calling regenerate_decl_from_template. */
19539 input_location
= DECL_SOURCE_LOCATION (d
);
19544 bool const_init
= false;
19546 /* Clear out DECL_RTL; whatever was there before may not be right
19547 since we've reset the type of the declaration. */
19548 SET_DECL_RTL (d
, NULL
);
19549 DECL_IN_AGGR_P (d
) = 0;
19551 /* The initializer is placed in DECL_INITIAL by
19552 regenerate_decl_from_template so we don't need to
19553 push/pop_access_scope again here. Pull it out so that
19554 cp_finish_decl can process it. */
19555 init
= DECL_INITIAL (d
);
19556 DECL_INITIAL (d
) = NULL_TREE
;
19557 DECL_INITIALIZED_P (d
) = 0;
19559 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
19560 initializer. That function will defer actual emission until
19561 we have a chance to determine linkage. */
19562 DECL_EXTERNAL (d
) = 0;
19564 /* Enter the scope of D so that access-checking works correctly. */
19565 push_nested_class (DECL_CONTEXT (d
));
19566 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
19567 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
19568 pop_nested_class ();
19570 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
19571 synthesize_method (d
);
19572 else if (TREE_CODE (d
) == FUNCTION_DECL
)
19574 struct pointer_map_t
*saved_local_specializations
;
19578 tree block
= NULL_TREE
;
19580 /* Save away the current list, in case we are instantiating one
19581 template from within the body of another. */
19582 saved_local_specializations
= local_specializations
;
19584 /* Set up the list of local specializations. */
19585 local_specializations
= pointer_map_create ();
19587 /* Set up context. */
19588 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
19589 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
19590 block
= push_stmt_list ();
19592 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
19594 /* Some typedefs referenced from within the template code need to be
19595 access checked at template instantiation time, i.e now. These
19596 types were added to the template at parsing time. Let's get those
19597 and perform the access checks then. */
19598 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl
),
19601 /* Create substitution entries for the parameters. */
19602 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
19603 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
19604 spec_parm
= DECL_ARGUMENTS (d
);
19605 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
19607 register_local_specialization (spec_parm
, tmpl_parm
);
19608 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
19609 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
19611 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
19613 if (!DECL_PACK_P (tmpl_parm
))
19615 register_local_specialization (spec_parm
, tmpl_parm
);
19616 spec_parm
= DECL_CHAIN (spec_parm
);
19620 /* Register the (value) argument pack as a specialization of
19621 TMPL_PARM, then move on. */
19622 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
19623 register_local_specialization (argpack
, tmpl_parm
);
19626 gcc_assert (!spec_parm
);
19628 /* Substitute into the body of the function. */
19629 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
19630 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
19631 tf_warning_or_error
, tmpl
);
19634 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
19635 tf_warning_or_error
, tmpl
,
19636 /*integral_constant_expression_p=*/false);
19638 /* Set the current input_location to the end of the function
19639 so that finish_function knows where we are. */
19641 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
19644 /* We don't need the local specializations any more. */
19645 pointer_map_destroy (local_specializations
);
19646 local_specializations
= saved_local_specializations
;
19648 /* Finish the function. */
19649 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
19650 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
19651 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
19654 d
= finish_function (0);
19655 expand_or_defer_fn (d
);
19658 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
19659 cp_check_omp_declare_reduction (d
);
19662 /* We're not deferring instantiation any more. */
19663 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
19666 pop_from_top_level ();
19668 pop_function_context ();
19671 input_location
= saved_loc
;
19672 cp_unevaluated_operand
= saved_unevaluated_operand
;
19673 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
19674 pop_deferring_access_checks ();
19675 pop_tinst_level ();
19677 timevar_pop (TV_TEMPLATE_INST
);
19682 /* Run through the list of templates that we wish we could
19683 instantiate, and instantiate any we can. RETRIES is the
19684 number of times we retry pending template instantiation. */
19687 instantiate_pending_templates (int retries
)
19690 location_t saved_loc
= input_location
;
19692 /* Instantiating templates may trigger vtable generation. This in turn
19693 may require further template instantiations. We place a limit here
19694 to avoid infinite loop. */
19695 if (pending_templates
&& retries
>= max_tinst_depth
)
19697 tree decl
= pending_templates
->tinst
->decl
;
19699 error ("template instantiation depth exceeds maximum of %d"
19700 " instantiating %q+D, possibly from virtual table generation"
19701 " (use -ftemplate-depth= to increase the maximum)",
19702 max_tinst_depth
, decl
);
19703 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19704 /* Pretend that we defined it. */
19705 DECL_INITIAL (decl
) = error_mark_node
;
19711 struct pending_template
**t
= &pending_templates
;
19712 struct pending_template
*last
= NULL
;
19716 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
19717 bool complete
= false;
19719 if (TYPE_P (instantiation
))
19723 if (!COMPLETE_TYPE_P (instantiation
))
19725 instantiate_class_template (instantiation
);
19726 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
19727 for (fn
= TYPE_METHODS (instantiation
);
19729 fn
= TREE_CHAIN (fn
))
19730 if (! DECL_ARTIFICIAL (fn
))
19731 instantiate_decl (fn
,
19733 /*expl_inst_class_mem_p=*/false);
19734 if (COMPLETE_TYPE_P (instantiation
))
19738 complete
= COMPLETE_TYPE_P (instantiation
);
19742 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
19743 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
19746 = instantiate_decl (instantiation
,
19748 /*expl_inst_class_mem_p=*/false);
19749 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
19753 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
19754 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
19758 /* If INSTANTIATION has been instantiated, then we don't
19759 need to consider it again in the future. */
19767 current_tinst_level
= NULL
;
19769 last_pending_template
= last
;
19771 while (reconsider
);
19773 input_location
= saved_loc
;
19776 /* Substitute ARGVEC into T, which is a list of initializers for
19777 either base class or a non-static data member. The TREE_PURPOSEs
19778 are DECLs, and the TREE_VALUEs are the initializer values. Used by
19779 instantiate_decl. */
19782 tsubst_initializer_list (tree t
, tree argvec
)
19784 tree inits
= NULL_TREE
;
19786 for (; t
; t
= TREE_CHAIN (t
))
19790 tree expanded_bases
= NULL_TREE
;
19791 tree expanded_arguments
= NULL_TREE
;
19794 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
19799 /* Expand the base class expansion type into separate base
19801 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
19802 tf_warning_or_error
,
19804 if (expanded_bases
== error_mark_node
)
19807 /* We'll be building separate TREE_LISTs of arguments for
19809 len
= TREE_VEC_LENGTH (expanded_bases
);
19810 expanded_arguments
= make_tree_vec (len
);
19811 for (i
= 0; i
< len
; i
++)
19812 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
19814 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
19815 expand each argument in the TREE_VALUE of t. */
19816 expr
= make_node (EXPR_PACK_EXPANSION
);
19817 PACK_EXPANSION_LOCAL_P (expr
) = true;
19818 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
19819 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
19821 if (TREE_VALUE (t
) == void_type_node
)
19822 /* VOID_TYPE_NODE is used to indicate
19823 value-initialization. */
19825 for (i
= 0; i
< len
; i
++)
19826 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
19830 /* Substitute parameter packs into each argument in the
19832 in_base_initializer
= 1;
19833 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
19835 tree expanded_exprs
;
19837 /* Expand the argument. */
19838 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
19840 = tsubst_pack_expansion (expr
, argvec
,
19841 tf_warning_or_error
,
19843 if (expanded_exprs
== error_mark_node
)
19846 /* Prepend each of the expanded expressions to the
19847 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
19848 for (i
= 0; i
< len
; i
++)
19850 TREE_VEC_ELT (expanded_arguments
, i
) =
19851 tree_cons (NULL_TREE
,
19852 TREE_VEC_ELT (expanded_exprs
, i
),
19853 TREE_VEC_ELT (expanded_arguments
, i
));
19856 in_base_initializer
= 0;
19858 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
19859 since we built them backwards. */
19860 for (i
= 0; i
< len
; i
++)
19862 TREE_VEC_ELT (expanded_arguments
, i
) =
19863 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
19868 for (i
= 0; i
< len
; ++i
)
19870 if (expanded_bases
)
19872 decl
= TREE_VEC_ELT (expanded_bases
, i
);
19873 decl
= expand_member_init (decl
);
19874 init
= TREE_VEC_ELT (expanded_arguments
, i
);
19879 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
19880 tf_warning_or_error
, NULL_TREE
);
19882 decl
= expand_member_init (decl
);
19883 if (decl
&& !DECL_P (decl
))
19884 in_base_initializer
= 1;
19886 init
= TREE_VALUE (t
);
19888 if (init
!= void_type_node
)
19889 init
= tsubst_expr (init
, argvec
,
19890 tf_warning_or_error
, NULL_TREE
,
19891 /*integral_constant_expression_p=*/false);
19892 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
19893 /* If we had an initializer but it instantiated to nothing,
19894 value-initialize the object. This will only occur when
19895 the initializer was a pack expansion where the parameter
19896 packs used in that expansion were of length zero. */
19897 init
= void_type_node
;
19898 in_base_initializer
= 0;
19903 init
= build_tree_list (decl
, init
);
19904 TREE_CHAIN (init
) = inits
;
19912 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19915 set_current_access_from_decl (tree decl
)
19917 if (TREE_PRIVATE (decl
))
19918 current_access_specifier
= access_private_node
;
19919 else if (TREE_PROTECTED (decl
))
19920 current_access_specifier
= access_protected_node
;
19922 current_access_specifier
= access_public_node
;
19925 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19926 is the instantiation (which should have been created with
19927 start_enum) and ARGS are the template arguments to use. */
19930 tsubst_enum (tree tag
, tree newtag
, tree args
)
19934 if (SCOPED_ENUM_P (newtag
))
19935 begin_scope (sk_scoped_enum
, newtag
);
19937 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
19942 decl
= TREE_VALUE (e
);
19943 /* Note that in a template enum, the TREE_VALUE is the
19944 CONST_DECL, not the corresponding INTEGER_CST. */
19945 value
= tsubst_expr (DECL_INITIAL (decl
),
19946 args
, tf_warning_or_error
, NULL_TREE
,
19947 /*integral_constant_expression_p=*/true);
19949 /* Give this enumeration constant the correct access. */
19950 set_current_access_from_decl (decl
);
19952 /* Actually build the enumerator itself. */
19954 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
19957 if (SCOPED_ENUM_P (newtag
))
19960 finish_enum_value_list (newtag
);
19961 finish_enum (newtag
);
19963 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
19964 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
19967 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19968 its type -- but without substituting the innermost set of template
19969 arguments. So, innermost set of template parameters will appear in
19973 get_mostly_instantiated_function_type (tree decl
)
19981 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
19982 targs
= DECL_TI_ARGS (decl
);
19983 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
19984 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
19986 /* There should be as many levels of arguments as there are levels
19988 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
19990 fn_type
= TREE_TYPE (tmpl
);
19992 if (parm_depth
== 1)
19993 /* No substitution is necessary. */
20000 /* Replace the innermost level of the TARGS with NULL_TREEs to
20001 let tsubst know not to substitute for those parameters. */
20002 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
20003 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
20004 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
20005 TMPL_ARGS_LEVEL (targs
, i
));
20006 SET_TMPL_ARGS_LEVEL (partial_args
,
20007 TMPL_ARGS_DEPTH (targs
),
20008 make_tree_vec (DECL_NTPARMS (tmpl
)));
20010 /* Make sure that we can see identifiers, and compute access
20012 push_access_scope (decl
);
20014 ++processing_template_decl
;
20015 /* Now, do the (partial) substitution to figure out the
20016 appropriate function type. */
20017 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
20018 --processing_template_decl
;
20020 /* Substitute into the template parameters to obtain the real
20021 innermost set of parameters. This step is important if the
20022 innermost set of template parameters contains value
20023 parameters whose types depend on outer template parameters. */
20024 TREE_VEC_LENGTH (partial_args
)--;
20025 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
20027 pop_access_scope (decl
);
20033 /* Return truthvalue if we're processing a template different from
20034 the last one involved in diagnostics. */
20036 problematic_instantiation_changed (void)
20038 return current_tinst_level
!= last_error_tinst_level
;
20041 /* Remember current template involved in diagnostics. */
20043 record_last_problematic_instantiation (void)
20045 last_error_tinst_level
= current_tinst_level
;
20048 struct tinst_level
*
20049 current_instantiation (void)
20051 return current_tinst_level
;
20054 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20055 type. Return zero for ok, nonzero for disallowed. Issue error and
20056 warning messages under control of COMPLAIN. */
20059 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
20061 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
20063 else if (POINTER_TYPE_P (type
))
20065 else if (TYPE_PTRMEM_P (type
))
20067 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
20069 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
20071 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
20073 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
20076 if (complain
& tf_error
)
20078 if (type
== error_mark_node
)
20079 inform (input_location
, "invalid template non-type parameter");
20081 error ("%q#T is not a valid type for a template non-type parameter",
20087 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20088 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20091 dependent_type_p_r (tree type
)
20097 A type is dependent if it is:
20099 -- a template parameter. Template template parameters are types
20100 for us (since TYPE_P holds true for them) so we handle
20102 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20103 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
20105 /* -- a qualified-id with a nested-name-specifier which contains a
20106 class-name that names a dependent type or whose unqualified-id
20107 names a dependent type. */
20108 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20110 /* -- a cv-qualified type where the cv-unqualified type is
20112 type
= TYPE_MAIN_VARIANT (type
);
20113 /* -- a compound type constructed from any dependent type. */
20114 if (TYPE_PTRMEM_P (type
))
20115 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
20116 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20118 else if (TYPE_PTR_P (type
)
20119 || TREE_CODE (type
) == REFERENCE_TYPE
)
20120 return dependent_type_p (TREE_TYPE (type
));
20121 else if (TREE_CODE (type
) == FUNCTION_TYPE
20122 || TREE_CODE (type
) == METHOD_TYPE
)
20126 if (dependent_type_p (TREE_TYPE (type
)))
20128 for (arg_type
= TYPE_ARG_TYPES (type
);
20130 arg_type
= TREE_CHAIN (arg_type
))
20131 if (dependent_type_p (TREE_VALUE (arg_type
)))
20135 /* -- an array type constructed from any dependent type or whose
20136 size is specified by a constant expression that is
20139 We checked for type- and value-dependence of the bounds in
20140 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20141 if (TREE_CODE (type
) == ARRAY_TYPE
)
20143 if (TYPE_DOMAIN (type
)
20144 && dependent_type_p (TYPE_DOMAIN (type
)))
20146 return dependent_type_p (TREE_TYPE (type
));
20149 /* -- a template-id in which either the template name is a template
20151 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20153 /* ... or any of the template arguments is a dependent type or
20154 an expression that is type-dependent or value-dependent. */
20155 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
20156 && (any_dependent_template_arguments_p
20157 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
20160 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20161 dependent; if the argument of the `typeof' expression is not
20162 type-dependent, then it should already been have resolved. */
20163 if (TREE_CODE (type
) == TYPEOF_TYPE
20164 || TREE_CODE (type
) == DECLTYPE_TYPE
20165 || TREE_CODE (type
) == UNDERLYING_TYPE
)
20168 /* A template argument pack is dependent if any of its packed
20170 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
20172 tree args
= ARGUMENT_PACK_ARGS (type
);
20173 int i
, len
= TREE_VEC_LENGTH (args
);
20174 for (i
= 0; i
< len
; ++i
)
20175 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
20179 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20180 be template parameters. */
20181 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
20184 /* The standard does not specifically mention types that are local
20185 to template functions or local classes, but they should be
20186 considered dependent too. For example:
20188 template <int I> void f() {
20193 The size of `E' cannot be known until the value of `I' has been
20194 determined. Therefore, `E' must be considered dependent. */
20195 scope
= TYPE_CONTEXT (type
);
20196 if (scope
&& TYPE_P (scope
))
20197 return dependent_type_p (scope
);
20198 /* Don't use type_dependent_expression_p here, as it can lead
20199 to infinite recursion trying to determine whether a lambda
20200 nested in a lambda is dependent (c++/47687). */
20201 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
20202 && DECL_LANG_SPECIFIC (scope
)
20203 && DECL_TEMPLATE_INFO (scope
)
20204 && (any_dependent_template_arguments_p
20205 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
20208 /* Other types are non-dependent. */
20212 /* Returns TRUE if TYPE is dependent, in the sense of
20213 [temp.dep.type]. Note that a NULL type is considered dependent. */
20216 dependent_type_p (tree type
)
20218 /* If there are no template parameters in scope, then there can't be
20219 any dependent types. */
20220 if (!processing_template_decl
)
20222 /* If we are not processing a template, then nobody should be
20223 providing us with a dependent type. */
20225 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
20229 /* If the type is NULL, we have not computed a type for the entity
20230 in question; in that case, the type is dependent. */
20234 /* Erroneous types can be considered non-dependent. */
20235 if (type
== error_mark_node
)
20238 /* If we have not already computed the appropriate value for TYPE,
20240 if (!TYPE_DEPENDENT_P_VALID (type
))
20242 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
20243 TYPE_DEPENDENT_P_VALID (type
) = 1;
20246 return TYPE_DEPENDENT_P (type
);
20249 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20250 lookup. In other words, a dependent type that is not the current
20254 dependent_scope_p (tree scope
)
20256 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
20257 && !currently_open_class (scope
));
20260 /* T is a SCOPE_REF; return whether we need to consider it
20261 instantiation-dependent so that we can check access at instantiation
20262 time even though we know which member it resolves to. */
20265 instantiation_dependent_scope_ref_p (tree t
)
20267 if (DECL_P (TREE_OPERAND (t
, 1))
20268 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
20269 && accessible_in_template_p (TREE_OPERAND (t
, 0),
20270 TREE_OPERAND (t
, 1)))
20276 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20277 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20280 /* Note that this predicate is not appropriate for general expressions;
20281 only constant expressions (that satisfy potential_constant_expression)
20282 can be tested for value dependence. */
20285 value_dependent_expression_p (tree expression
)
20287 if (!processing_template_decl
)
20290 /* A name declared with a dependent type. */
20291 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
20294 switch (TREE_CODE (expression
))
20296 case IDENTIFIER_NODE
:
20297 /* A name that has not been looked up -- must be dependent. */
20300 case TEMPLATE_PARM_INDEX
:
20301 /* A non-type template parm. */
20305 /* A non-type template parm. */
20306 if (DECL_TEMPLATE_PARM_P (expression
))
20308 return value_dependent_expression_p (DECL_INITIAL (expression
));
20311 /* A constant with literal type and is initialized
20312 with an expression that is value-dependent.
20314 Note that a non-dependent parenthesized initializer will have
20315 already been replaced with its constant value, so if we see
20316 a TREE_LIST it must be dependent. */
20317 if (DECL_INITIAL (expression
)
20318 && decl_constant_var_p (expression
)
20319 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
20320 || value_dependent_expression_p (DECL_INITIAL (expression
))))
20324 case DYNAMIC_CAST_EXPR
:
20325 case STATIC_CAST_EXPR
:
20326 case CONST_CAST_EXPR
:
20327 case REINTERPRET_CAST_EXPR
:
20329 /* These expressions are value-dependent if the type to which
20330 the cast occurs is dependent or the expression being casted
20331 is value-dependent. */
20333 tree type
= TREE_TYPE (expression
);
20335 if (dependent_type_p (type
))
20338 /* A functional cast has a list of operands. */
20339 expression
= TREE_OPERAND (expression
, 0);
20342 /* If there are no operands, it must be an expression such
20343 as "int()". This should not happen for aggregate types
20344 because it would form non-constant expressions. */
20345 gcc_assert (cxx_dialect
>= cxx11
20346 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
20351 if (TREE_CODE (expression
) == TREE_LIST
)
20352 return any_value_dependent_elements_p (expression
);
20354 return value_dependent_expression_p (expression
);
20358 if (SIZEOF_EXPR_TYPE_P (expression
))
20359 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
20363 /* A `sizeof' expression is value-dependent if the operand is
20364 type-dependent or is a pack expansion. */
20365 expression
= TREE_OPERAND (expression
, 0);
20366 if (PACK_EXPANSION_P (expression
))
20368 else if (TYPE_P (expression
))
20369 return dependent_type_p (expression
);
20370 return instantiation_dependent_expression_p (expression
);
20372 case AT_ENCODE_EXPR
:
20373 /* An 'encode' expression is value-dependent if the operand is
20375 expression
= TREE_OPERAND (expression
, 0);
20376 return dependent_type_p (expression
);
20378 case NOEXCEPT_EXPR
:
20379 expression
= TREE_OPERAND (expression
, 0);
20380 return instantiation_dependent_expression_p (expression
);
20383 /* All instantiation-dependent expressions should also be considered
20384 value-dependent. */
20385 return instantiation_dependent_scope_ref_p (expression
);
20387 case COMPONENT_REF
:
20388 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
20389 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
20391 case NONTYPE_ARGUMENT_PACK
:
20392 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
20393 is value-dependent. */
20395 tree values
= ARGUMENT_PACK_ARGS (expression
);
20396 int i
, len
= TREE_VEC_LENGTH (values
);
20398 for (i
= 0; i
< len
; ++i
)
20399 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
20407 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
20408 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
20409 || (type2
? dependent_type_p (type2
) : false));
20413 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20414 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
20417 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20418 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
20422 tree op
= TREE_OPERAND (expression
, 0);
20423 return (value_dependent_expression_p (op
)
20424 || has_value_dependent_address (op
));
20429 tree fn
= get_callee_fndecl (expression
);
20431 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
20433 nargs
= call_expr_nargs (expression
);
20434 for (i
= 0; i
< nargs
; ++i
)
20436 tree op
= CALL_EXPR_ARG (expression
, i
);
20437 /* In a call to a constexpr member function, look through the
20438 implicit ADDR_EXPR on the object argument so that it doesn't
20439 cause the call to be considered value-dependent. We also
20440 look through it in potential_constant_expression. */
20441 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
20442 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
20443 && TREE_CODE (op
) == ADDR_EXPR
)
20444 op
= TREE_OPERAND (op
, 0);
20445 if (value_dependent_expression_p (op
))
20451 case TEMPLATE_ID_EXPR
:
20452 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
20454 return type_dependent_expression_p (expression
);
20460 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
20461 if (value_dependent_expression_p (val
))
20467 /* Treat a GNU statement expression as dependent to avoid crashing
20468 under fold_non_dependent_expr; it can't be constant. */
20472 /* A constant expression is value-dependent if any subexpression is
20473 value-dependent. */
20474 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
20476 case tcc_reference
:
20478 case tcc_comparison
:
20480 case tcc_expression
:
20483 int i
, len
= cp_tree_operand_length (expression
);
20485 for (i
= 0; i
< len
; i
++)
20487 tree t
= TREE_OPERAND (expression
, i
);
20489 /* In some cases, some of the operands may be missing.l
20490 (For example, in the case of PREDECREMENT_EXPR, the
20491 amount to increment by may be missing.) That doesn't
20492 make the expression dependent. */
20493 if (t
&& value_dependent_expression_p (t
))
20504 /* The expression is not value-dependent. */
20508 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
20509 [temp.dep.expr]. Note that an expression with no type is
20510 considered dependent. Other parts of the compiler arrange for an
20511 expression with type-dependent subexpressions to have no type, so
20512 this function doesn't have to be fully recursive. */
20515 type_dependent_expression_p (tree expression
)
20517 if (!processing_template_decl
)
20520 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
20523 /* An unresolved name is always dependent. */
20524 if (identifier_p (expression
) || TREE_CODE (expression
) == USING_DECL
)
20527 /* Some expression forms are never type-dependent. */
20528 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
20529 || TREE_CODE (expression
) == SIZEOF_EXPR
20530 || TREE_CODE (expression
) == ALIGNOF_EXPR
20531 || TREE_CODE (expression
) == AT_ENCODE_EXPR
20532 || TREE_CODE (expression
) == NOEXCEPT_EXPR
20533 || TREE_CODE (expression
) == TRAIT_EXPR
20534 || TREE_CODE (expression
) == TYPEID_EXPR
20535 || TREE_CODE (expression
) == DELETE_EXPR
20536 || TREE_CODE (expression
) == VEC_DELETE_EXPR
20537 || TREE_CODE (expression
) == THROW_EXPR
)
20540 /* The types of these expressions depends only on the type to which
20541 the cast occurs. */
20542 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
20543 || TREE_CODE (expression
) == STATIC_CAST_EXPR
20544 || TREE_CODE (expression
) == CONST_CAST_EXPR
20545 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
20546 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
20547 || TREE_CODE (expression
) == CAST_EXPR
)
20548 return dependent_type_p (TREE_TYPE (expression
));
20550 /* The types of these expressions depends only on the type created
20551 by the expression. */
20552 if (TREE_CODE (expression
) == NEW_EXPR
20553 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
20555 /* For NEW_EXPR tree nodes created inside a template, either
20556 the object type itself or a TREE_LIST may appear as the
20558 tree type
= TREE_OPERAND (expression
, 1);
20559 if (TREE_CODE (type
) == TREE_LIST
)
20560 /* This is an array type. We need to check array dimensions
20562 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
20563 || value_dependent_expression_p
20564 (TREE_OPERAND (TREE_VALUE (type
), 1));
20566 return dependent_type_p (type
);
20569 if (TREE_CODE (expression
) == SCOPE_REF
)
20571 tree scope
= TREE_OPERAND (expression
, 0);
20572 tree name
= TREE_OPERAND (expression
, 1);
20574 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
20575 contains an identifier associated by name lookup with one or more
20576 declarations declared with a dependent type, or...a
20577 nested-name-specifier or qualified-id that names a member of an
20578 unknown specialization. */
20579 return (type_dependent_expression_p (name
)
20580 || dependent_scope_p (scope
));
20583 if (TREE_CODE (expression
) == FUNCTION_DECL
20584 && DECL_LANG_SPECIFIC (expression
)
20585 && DECL_TEMPLATE_INFO (expression
)
20586 && (any_dependent_template_arguments_p
20587 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
20590 if (TREE_CODE (expression
) == TEMPLATE_DECL
20591 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
20594 if (TREE_CODE (expression
) == STMT_EXPR
)
20595 expression
= stmt_expr_value_expr (expression
);
20597 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
20602 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
20604 if (type_dependent_expression_p (elt
))
20610 /* A static data member of the current instantiation with incomplete
20611 array type is type-dependent, as the definition and specializations
20612 can have different bounds. */
20613 if (VAR_P (expression
)
20614 && DECL_CLASS_SCOPE_P (expression
)
20615 && dependent_type_p (DECL_CONTEXT (expression
))
20616 && VAR_HAD_UNKNOWN_BOUND (expression
))
20619 /* An array of unknown bound depending on a variadic parameter, eg:
20621 template<typename... Args>
20622 void foo (Args... args)
20624 int arr[] = { args... };
20627 template<int... vals>
20630 int arr[] = { vals... };
20633 If the array has no length and has an initializer, it must be that
20634 we couldn't determine its length in cp_complete_array_type because
20635 it is dependent. */
20636 if (VAR_P (expression
)
20637 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
20638 && !TYPE_DOMAIN (TREE_TYPE (expression
))
20639 && DECL_INITIAL (expression
))
20642 if (TREE_TYPE (expression
) == unknown_type_node
)
20644 if (TREE_CODE (expression
) == ADDR_EXPR
)
20645 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
20646 if (TREE_CODE (expression
) == COMPONENT_REF
20647 || TREE_CODE (expression
) == OFFSET_REF
)
20649 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20651 expression
= TREE_OPERAND (expression
, 1);
20652 if (identifier_p (expression
))
20655 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
20656 if (TREE_CODE (expression
) == SCOPE_REF
)
20659 /* Always dependent, on the number of arguments if nothing else. */
20660 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
20663 if (BASELINK_P (expression
))
20664 expression
= BASELINK_FUNCTIONS (expression
);
20666 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
20668 if (any_dependent_template_arguments_p
20669 (TREE_OPERAND (expression
, 1)))
20671 expression
= TREE_OPERAND (expression
, 0);
20673 gcc_assert (TREE_CODE (expression
) == OVERLOAD
20674 || TREE_CODE (expression
) == FUNCTION_DECL
);
20678 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
20680 expression
= OVL_NEXT (expression
);
20685 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
20687 return (dependent_type_p (TREE_TYPE (expression
)));
20690 /* walk_tree callback function for instantiation_dependent_expression_p,
20691 below. Returns non-zero if a dependent subexpression is found. */
20694 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
20699 /* We don't have to worry about decltype currently because decltype
20700 of an instantiation-dependent expr is a dependent type. This
20701 might change depending on the resolution of DR 1172. */
20702 *walk_subtrees
= false;
20705 enum tree_code code
= TREE_CODE (*tp
);
20708 /* Don't treat an argument list as dependent just because it has no
20716 /* A constant with a dependent initializer is dependent. */
20717 if (value_dependent_expression_p (*tp
))
20721 case TEMPLATE_PARM_INDEX
:
20724 /* Handle expressions with type operands. */
20728 case AT_ENCODE_EXPR
:
20730 tree op
= TREE_OPERAND (*tp
, 0);
20731 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
20732 op
= TREE_TYPE (op
);
20735 if (dependent_type_p (op
))
20739 *walk_subtrees
= false;
20747 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp
))
20748 || (TRAIT_EXPR_TYPE2 (*tp
)
20749 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp
))))
20751 *walk_subtrees
= false;
20754 case COMPONENT_REF
:
20755 if (identifier_p (TREE_OPERAND (*tp
, 1)))
20756 /* In a template, finish_class_member_access_expr creates a
20757 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
20758 type-dependent, so that we can check access control at
20759 instantiation time (PR 42277). See also Core issue 1273. */
20764 if (instantiation_dependent_scope_ref_p (*tp
))
20769 /* Treat statement-expressions as dependent. */
20777 if (type_dependent_expression_p (*tp
))
20783 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
20784 sense defined by the ABI:
20786 "An expression is instantiation-dependent if it is type-dependent
20787 or value-dependent, or it has a subexpression that is type-dependent
20788 or value-dependent." */
20791 instantiation_dependent_expression_p (tree expression
)
20795 if (!processing_template_decl
)
20798 if (expression
== error_mark_node
)
20801 result
= cp_walk_tree_without_duplicates (&expression
,
20802 instantiation_dependent_r
, NULL
);
20803 return result
!= NULL_TREE
;
20806 /* Like type_dependent_expression_p, but it also works while not processing
20807 a template definition, i.e. during substitution or mangling. */
20810 type_dependent_expression_p_push (tree expr
)
20813 ++processing_template_decl
;
20814 b
= type_dependent_expression_p (expr
);
20815 --processing_template_decl
;
20819 /* Returns TRUE if ARGS contains a type-dependent expression. */
20822 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
20827 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
20829 if (type_dependent_expression_p (arg
))
20835 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20836 expressions) contains any type-dependent expressions. */
20839 any_type_dependent_elements_p (const_tree list
)
20841 for (; list
; list
= TREE_CHAIN (list
))
20842 if (type_dependent_expression_p (TREE_VALUE (list
)))
20848 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20849 expressions) contains any value-dependent expressions. */
20852 any_value_dependent_elements_p (const_tree list
)
20854 for (; list
; list
= TREE_CHAIN (list
))
20855 if (value_dependent_expression_p (TREE_VALUE (list
)))
20861 /* Returns TRUE if the ARG (a template argument) is dependent. */
20864 dependent_template_arg_p (tree arg
)
20866 if (!processing_template_decl
)
20869 /* Assume a template argument that was wrongly written by the user
20870 is dependent. This is consistent with what
20871 any_dependent_template_arguments_p [that calls this function]
20873 if (!arg
|| arg
== error_mark_node
)
20876 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
20877 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
20879 if (TREE_CODE (arg
) == TEMPLATE_DECL
20880 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
20881 return dependent_template_p (arg
);
20882 else if (ARGUMENT_PACK_P (arg
))
20884 tree args
= ARGUMENT_PACK_ARGS (arg
);
20885 int i
, len
= TREE_VEC_LENGTH (args
);
20886 for (i
= 0; i
< len
; ++i
)
20888 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
20894 else if (TYPE_P (arg
))
20895 return dependent_type_p (arg
);
20897 return (type_dependent_expression_p (arg
)
20898 || value_dependent_expression_p (arg
));
20901 /* Returns true if ARGS (a collection of template arguments) contains
20902 any types that require structural equality testing. */
20905 any_template_arguments_need_structural_equality_p (tree args
)
20912 if (args
== error_mark_node
)
20915 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
20917 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
20918 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
20920 tree arg
= TREE_VEC_ELT (level
, j
);
20921 tree packed_args
= NULL_TREE
;
20924 if (ARGUMENT_PACK_P (arg
))
20926 /* Look inside the argument pack. */
20927 packed_args
= ARGUMENT_PACK_ARGS (arg
);
20928 len
= TREE_VEC_LENGTH (packed_args
);
20931 for (k
= 0; k
< len
; ++k
)
20934 arg
= TREE_VEC_ELT (packed_args
, k
);
20936 if (error_operand_p (arg
))
20938 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
20940 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
20942 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
20943 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
20952 /* Returns true if ARGS (a collection of template arguments) contains
20953 any dependent arguments. */
20956 any_dependent_template_arguments_p (const_tree args
)
20963 if (args
== error_mark_node
)
20966 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
20968 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
20969 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
20970 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
20977 /* Returns TRUE if the template TMPL is dependent. */
20980 dependent_template_p (tree tmpl
)
20982 if (TREE_CODE (tmpl
) == OVERLOAD
)
20986 if (dependent_template_p (OVL_CURRENT (tmpl
)))
20988 tmpl
= OVL_NEXT (tmpl
);
20993 /* Template template parameters are dependent. */
20994 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
20995 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
20997 /* So are names that have not been looked up. */
20998 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
21000 /* So are member templates of dependent classes. */
21001 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
21002 return dependent_type_p (DECL_CONTEXT (tmpl
));
21006 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21009 dependent_template_id_p (tree tmpl
, tree args
)
21011 return (dependent_template_p (tmpl
)
21012 || any_dependent_template_arguments_p (args
));
21015 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21019 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
21023 if (!processing_template_decl
)
21026 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
21028 tree decl
= TREE_VEC_ELT (declv
, i
);
21029 tree init
= TREE_VEC_ELT (initv
, i
);
21030 tree cond
= TREE_VEC_ELT (condv
, i
);
21031 tree incr
= TREE_VEC_ELT (incrv
, i
);
21033 if (type_dependent_expression_p (decl
))
21036 if (init
&& type_dependent_expression_p (init
))
21039 if (type_dependent_expression_p (cond
))
21042 if (COMPARISON_CLASS_P (cond
)
21043 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
21044 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
21047 if (TREE_CODE (incr
) == MODOP_EXPR
)
21049 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
21050 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
21053 else if (type_dependent_expression_p (incr
))
21055 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
21057 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
21059 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
21061 tree t
= TREE_OPERAND (incr
, 1);
21062 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
21063 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
21072 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21073 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21074 no such TYPE can be found. Note that this function peers inside
21075 uninstantiated templates and therefore should be used only in
21076 extremely limited situations. ONLY_CURRENT_P restricts this
21077 peering to the currently open classes hierarchy (which is required
21078 when comparing types). */
21081 resolve_typename_type (tree type
, bool only_current_p
)
21090 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
21092 scope
= TYPE_CONTEXT (type
);
21093 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21094 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21095 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21096 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21097 identifier of the TYPENAME_TYPE anymore.
21098 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21099 TYPENAME_TYPE instead, we avoid messing up with a possible
21100 typedef variant case. */
21101 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
21103 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21104 it first before we can figure out what NAME refers to. */
21105 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
21107 if (TYPENAME_IS_RESOLVING_P (scope
))
21108 /* Given a class template A with a dependent base with nested type C,
21109 typedef typename A::C::C C will land us here, as trying to resolve
21110 the initial A::C leads to the local C typedef, which leads back to
21111 A::C::C. So we break the recursion now. */
21114 scope
= resolve_typename_type (scope
, only_current_p
);
21116 /* If we don't know what SCOPE refers to, then we cannot resolve the
21118 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
21120 /* If the SCOPE is a template type parameter, we have no way of
21121 resolving the name. */
21122 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
21124 /* If the SCOPE is not the current instantiation, there's no reason
21125 to look inside it. */
21126 if (only_current_p
&& !currently_open_class (scope
))
21128 /* If this is a typedef, we don't want to look inside (c++/11987). */
21129 if (typedef_variant_p (type
))
21131 /* If SCOPE isn't the template itself, it will not have a valid
21132 TYPE_FIELDS list. */
21133 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
21134 /* scope is either the template itself or a compatible instantiation
21135 like X<T>, so look up the name in the original template. */
21136 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
21138 /* scope is a partial instantiation, so we can't do the lookup or we
21139 will lose the template arguments. */
21141 /* Enter the SCOPE so that name lookup will be resolved as if we
21142 were in the class definition. In particular, SCOPE will no
21143 longer be considered a dependent type. */
21144 pushed_scope
= push_scope (scope
);
21145 /* Look up the declaration. */
21146 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
21147 tf_warning_or_error
);
21149 result
= NULL_TREE
;
21151 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21152 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21155 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type
))
21156 && TREE_CODE (decl
) == TYPE_DECL
)
21158 result
= TREE_TYPE (decl
);
21159 if (result
== error_mark_node
)
21160 result
= NULL_TREE
;
21162 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
21163 && DECL_CLASS_TEMPLATE_P (decl
))
21167 /* Obtain the template and the arguments. */
21168 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
21169 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
21170 /* Instantiate the template. */
21171 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
21172 /*entering_scope=*/0,
21173 tf_error
| tf_user
);
21174 if (result
== error_mark_node
)
21175 result
= NULL_TREE
;
21178 /* Leave the SCOPE. */
21180 pop_scope (pushed_scope
);
21182 /* If we failed to resolve it, return the original typename. */
21186 /* If lookup found a typename type, resolve that too. */
21187 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
21189 /* Ill-formed programs can cause infinite recursion here, so we
21190 must catch that. */
21191 TYPENAME_IS_RESOLVING_P (type
) = 1;
21192 result
= resolve_typename_type (result
, only_current_p
);
21193 TYPENAME_IS_RESOLVING_P (type
) = 0;
21196 /* Qualify the resulting type. */
21197 quals
= cp_type_quals (type
);
21199 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
21204 /* EXPR is an expression which is not type-dependent. Return a proxy
21205 for EXPR that can be used to compute the types of larger
21206 expressions containing EXPR. */
21209 build_non_dependent_expr (tree expr
)
21213 #ifdef ENABLE_CHECKING
21214 /* Try to get a constant value for all non-dependent expressions in
21215 order to expose bugs in *_dependent_expression_p and constexpr. */
21216 if (cxx_dialect
>= cxx11
)
21217 maybe_constant_value (fold_non_dependent_expr_sfinae (expr
, tf_none
));
21220 /* Preserve OVERLOADs; the functions must be available to resolve
21223 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
21224 inner_expr
= stmt_expr_value_expr (inner_expr
);
21225 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
21226 inner_expr
= TREE_OPERAND (inner_expr
, 0);
21227 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
21228 inner_expr
= TREE_OPERAND (inner_expr
, 1);
21229 if (is_overloaded_fn (inner_expr
)
21230 || TREE_CODE (inner_expr
) == OFFSET_REF
)
21232 /* There is no need to return a proxy for a variable. */
21235 /* Preserve string constants; conversions from string constants to
21236 "char *" are allowed, even though normally a "const char *"
21237 cannot be used to initialize a "char *". */
21238 if (TREE_CODE (expr
) == STRING_CST
)
21240 /* Preserve arithmetic constants, as an optimization -- there is no
21241 reason to create a new node. */
21242 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
21244 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21245 There is at least one place where we want to know that a
21246 particular expression is a throw-expression: when checking a ?:
21247 expression, there are special rules if the second or third
21248 argument is a throw-expression. */
21249 if (TREE_CODE (expr
) == THROW_EXPR
)
21252 /* Don't wrap an initializer list, we need to be able to look inside. */
21253 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
21256 /* Don't wrap a dummy object, we need to be able to test for it. */
21257 if (is_dummy_object (expr
))
21260 if (TREE_CODE (expr
) == COND_EXPR
)
21261 return build3 (COND_EXPR
,
21263 TREE_OPERAND (expr
, 0),
21264 (TREE_OPERAND (expr
, 1)
21265 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
21266 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
21267 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
21268 if (TREE_CODE (expr
) == COMPOUND_EXPR
21269 && !COMPOUND_EXPR_OVERLOADED (expr
))
21270 return build2 (COMPOUND_EXPR
,
21272 TREE_OPERAND (expr
, 0),
21273 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
21275 /* If the type is unknown, it can't really be non-dependent */
21276 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
21278 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21279 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
21282 /* ARGS is a vector of expressions as arguments to a function call.
21283 Replace the arguments with equivalent non-dependent expressions.
21284 This modifies ARGS in place. */
21287 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
21292 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
21294 tree newarg
= build_non_dependent_expr (arg
);
21296 (*args
)[ix
] = newarg
;
21300 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21301 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21305 make_auto_1 (tree name
)
21307 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
21308 TYPE_NAME (au
) = build_decl (input_location
,
21309 TYPE_DECL
, name
, au
);
21310 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
21311 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
21312 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
21313 TYPE_NAME (au
), NULL_TREE
);
21314 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
21315 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
21316 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
21322 make_decltype_auto (void)
21324 return make_auto_1 (get_identifier ("decltype(auto)"));
21330 return make_auto_1 (get_identifier ("auto"));
21333 /* Given type ARG, return std::initializer_list<ARG>. */
21338 tree std_init_list
= namespace_binding
21339 (get_identifier ("initializer_list"), std_node
);
21341 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
21343 error ("deducing from brace-enclosed initializer list requires "
21344 "#include <initializer_list>");
21345 return error_mark_node
;
21347 argvec
= make_tree_vec (1);
21348 TREE_VEC_ELT (argvec
, 0) = arg
;
21349 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
21350 NULL_TREE
, 0, tf_warning_or_error
);
21353 /* Replace auto in TYPE with std::initializer_list<auto>. */
21356 listify_autos (tree type
, tree auto_node
)
21358 tree init_auto
= listify (auto_node
);
21359 tree argvec
= make_tree_vec (1);
21360 TREE_VEC_ELT (argvec
, 0) = init_auto
;
21361 if (processing_template_decl
)
21362 argvec
= add_to_template_args (current_template_args (), argvec
);
21363 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
21366 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
21367 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
21370 do_auto_deduction (tree type
, tree init
, tree auto_node
)
21374 if (init
== error_mark_node
)
21375 return error_mark_node
;
21377 if (type_dependent_expression_p (init
))
21378 /* Defining a subset of type-dependent expressions that we can deduce
21379 from ahead of time isn't worth the trouble. */
21382 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
21383 with either a new invented type template parameter U or, if the
21384 initializer is a braced-init-list (8.5.4), with
21385 std::initializer_list<U>. */
21386 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
21387 type
= listify_autos (type
, auto_node
);
21389 init
= resolve_nondeduced_context (init
);
21391 targs
= make_tree_vec (1);
21392 if (AUTO_IS_DECLTYPE (auto_node
))
21394 bool id
= (DECL_P (init
) || TREE_CODE (init
) == COMPONENT_REF
);
21395 TREE_VEC_ELT (targs
, 0)
21396 = finish_decltype_type (init
, id
, tf_warning_or_error
);
21397 if (type
!= auto_node
)
21399 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
21400 return error_mark_node
;
21405 tree parms
= build_tree_list (NULL_TREE
, type
);
21406 tree tparms
= make_tree_vec (1);
21409 TREE_VEC_ELT (tparms
, 0)
21410 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
21411 val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
21412 DEDUCE_CALL
, LOOKUP_NORMAL
,
21413 NULL
, /*explain_p=*/false);
21416 if (processing_template_decl
)
21417 /* Try again at instantiation time. */
21419 if (type
&& type
!= error_mark_node
)
21420 /* If type is error_mark_node a diagnostic must have been
21421 emitted by now. Also, having a mention to '<type error>'
21422 in the diagnostic is not really useful to the user. */
21424 if (cfun
&& auto_node
== current_function_auto_return_pattern
21425 && LAMBDA_FUNCTION_P (current_function_decl
))
21426 error ("unable to deduce lambda return type from %qE", init
);
21428 error ("unable to deduce %qT from %qE", type
, init
);
21430 return error_mark_node
;
21434 /* If the list of declarators contains more than one declarator, the type
21435 of each declared variable is determined as described above. If the
21436 type deduced for the template parameter U is not the same in each
21437 deduction, the program is ill-formed. */
21438 if (TREE_TYPE (auto_node
)
21439 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
21441 if (cfun
&& auto_node
== current_function_auto_return_pattern
21442 && LAMBDA_FUNCTION_P (current_function_decl
))
21443 error ("inconsistent types %qT and %qT deduced for "
21444 "lambda return type", TREE_TYPE (auto_node
),
21445 TREE_VEC_ELT (targs
, 0));
21447 error ("inconsistent deduction for %qT: %qT and then %qT",
21448 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
21449 return error_mark_node
;
21451 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
21453 if (processing_template_decl
)
21454 targs
= add_to_template_args (current_template_args (), targs
);
21455 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
21458 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
21462 splice_late_return_type (tree type
, tree late_return_type
)
21466 if (late_return_type
== NULL_TREE
)
21468 argvec
= make_tree_vec (1);
21469 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
21470 if (processing_template_parmlist
)
21471 /* For a late-specified return type in a template type-parameter, we
21472 need to add a dummy argument level for its parmlist. */
21473 argvec
= add_to_template_args
21474 (make_tree_vec (processing_template_parmlist
), argvec
);
21475 if (current_template_parms
)
21476 argvec
= add_to_template_args (current_template_args (), argvec
);
21477 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
21480 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
21481 'decltype(auto)'. */
21484 is_auto (const_tree type
)
21486 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21487 && (TYPE_IDENTIFIER (type
) == get_identifier ("auto")
21488 || TYPE_IDENTIFIER (type
) == get_identifier ("decltype(auto)")))
21494 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
21495 a use of `auto'. Returns NULL_TREE otherwise. */
21498 type_uses_auto (tree type
)
21500 return find_type_usage (type
, is_auto
);
21503 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
21504 'decltype(auto)' or a concept. */
21507 is_auto_or_concept (const_tree type
)
21509 return is_auto (type
); // or concept
21512 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
21513 a concept identifier) iff TYPE contains a use of a generic type. Returns
21514 NULL_TREE otherwise. */
21517 type_uses_auto_or_concept (tree type
)
21519 return find_type_usage (type
, is_auto_or_concept
);
21523 /* For a given template T, return the vector of typedefs referenced
21524 in T for which access check is needed at T instantiation time.
21525 T is either a FUNCTION_DECL or a RECORD_TYPE.
21526 Those typedefs were added to T by the function
21527 append_type_to_template_for_access_check. */
21529 vec
<qualified_typedef_usage_t
, va_gc
> *
21530 get_types_needing_access_check (tree t
)
21533 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
21535 if (!t
|| t
== error_mark_node
)
21538 if (!(ti
= get_template_info (t
)))
21541 if (CLASS_TYPE_P (t
)
21542 || TREE_CODE (t
) == FUNCTION_DECL
)
21544 if (!TI_TEMPLATE (ti
))
21547 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
21553 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
21554 tied to T. That list of typedefs will be access checked at
21555 T instantiation time.
21556 T is either a FUNCTION_DECL or a RECORD_TYPE.
21557 TYPE_DECL is a TYPE_DECL node representing a typedef.
21558 SCOPE is the scope through which TYPE_DECL is accessed.
21559 LOCATION is the location of the usage point of TYPE_DECL.
21561 This function is a subroutine of
21562 append_type_to_template_for_access_check. */
21565 append_type_to_template_for_access_check_1 (tree t
,
21568 location_t location
)
21570 qualified_typedef_usage_t typedef_usage
;
21573 if (!t
|| t
== error_mark_node
)
21576 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
21577 || CLASS_TYPE_P (t
))
21579 && TREE_CODE (type_decl
) == TYPE_DECL
21582 if (!(ti
= get_template_info (t
)))
21585 gcc_assert (TI_TEMPLATE (ti
));
21587 typedef_usage
.typedef_decl
= type_decl
;
21588 typedef_usage
.context
= scope
;
21589 typedef_usage
.locus
= location
;
21591 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
21594 /* Append TYPE_DECL to the template TEMPL.
21595 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
21596 At TEMPL instanciation time, TYPE_DECL will be checked to see
21597 if it can be accessed through SCOPE.
21598 LOCATION is the location of the usage point of TYPE_DECL.
21600 e.g. consider the following code snippet:
21607 template<class U> struct S
21609 C::myint mi; // <-- usage point of the typedef C::myint
21614 At S<char> instantiation time, we need to check the access of C::myint
21615 In other words, we need to check the access of the myint typedef through
21616 the C scope. For that purpose, this function will add the myint typedef
21617 and the scope C through which its being accessed to a list of typedefs
21618 tied to the template S. That list will be walked at template instantiation
21619 time and access check performed on each typedefs it contains.
21620 Note that this particular code snippet should yield an error because
21621 myint is private to C. */
21624 append_type_to_template_for_access_check (tree templ
,
21627 location_t location
)
21629 qualified_typedef_usage_t
*iter
;
21632 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
21634 /* Make sure we don't append the type to the template twice. */
21635 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
21636 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
21639 append_type_to_template_for_access_check_1 (templ
, type_decl
,
21643 /* Convert the generic type parameters in PARM that match the types given in the
21644 range [START_IDX, END_IDX) from the current_template_parms into generic type
21648 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
21650 tree current
= current_template_parms
;
21651 int depth
= TMPL_PARMS_DEPTH (current
);
21652 current
= INNERMOST_TEMPLATE_PARMS (current
);
21653 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
21655 for (int i
= 0; i
< start_idx
; ++i
)
21656 TREE_VEC_ELT (replacement
, i
)
21657 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
21659 for (int i
= start_idx
; i
< end_idx
; ++i
)
21661 /* Create a distinct parameter pack type from the current parm and add it
21662 to the replacement args to tsubst below into the generic function
21665 tree o
= TREE_TYPE (TREE_VALUE
21666 (TREE_VEC_ELT (current
, i
)));
21667 tree t
= copy_type (o
);
21668 TEMPLATE_TYPE_PARM_INDEX (t
)
21669 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
21671 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
21672 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
21673 TYPE_MAIN_VARIANT (t
) = t
;
21674 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
21675 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
21676 TREE_VEC_ELT (replacement
, i
) = t
;
21677 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
21680 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
21681 TREE_VEC_ELT (replacement
, i
)
21682 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
21684 /* If there are more levels then build up the replacement with the outer
21687 replacement
= add_to_template_args (template_parms_to_args
21688 (TREE_CHAIN (current_template_parms
)),
21691 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
21695 /* Set up the hash tables for template instantiations. */
21698 init_template_processing (void)
21700 decl_specializations
= htab_create_ggc (37,
21701 hash_specialization
,
21702 eq_specializations
,
21704 type_specializations
= htab_create_ggc (37,
21705 hash_specialization
,
21706 eq_specializations
,
21710 /* Print stats about the template hash tables for -fstats. */
21713 print_template_statistics (void)
21715 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
21716 "%f collisions\n", (long) htab_size (decl_specializations
),
21717 (long) htab_elements (decl_specializations
),
21718 htab_collisions (decl_specializations
));
21719 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
21720 "%f collisions\n", (long) htab_size (type_specializations
),
21721 (long) htab_elements (type_specializations
),
21722 htab_collisions (type_specializations
));
21725 #include "gt-cp-pt.h"