1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2015 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"
34 #include "double-int.h"
41 #include "stringpool.h"
44 #include "stor-layout.h"
48 #include "c-family/c-common.h"
49 #include "c-family/c-objc.h"
50 #include "cp-objcp-common.h"
51 #include "tree-inline.h"
55 #include "tree-iterator.h"
56 #include "type-utils.h"
59 /* The type of functions taking a tree, and some additional data, and
61 typedef int (*tree_fn_t
) (tree
, void*);
63 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
64 instantiations have been deferred, either because their definitions
65 were not yet available, or because we were putting off doing the work. */
66 struct GTY ((chain_next ("%h.next"))) pending_template
{
67 struct pending_template
*next
;
68 struct tinst_level
*tinst
;
71 static GTY(()) struct pending_template
*pending_templates
;
72 static GTY(()) struct pending_template
*last_pending_template
;
74 int processing_template_parmlist
;
75 static int template_header_count
;
77 static GTY(()) tree saved_trees
;
78 static vec
<int> inline_parm_levels
;
80 static GTY(()) struct tinst_level
*current_tinst_level
;
82 static GTY(()) tree saved_access_scope
;
84 /* Live only within one (recursive) call to tsubst_expr. We use
85 this to pass the statement expression node from the STMT_EXPR
86 to the EXPR_STMT that is its result. */
87 static tree cur_stmt_expr
;
89 /* True if we've recursed into fn_type_unification too many times. */
90 static bool excessive_deduction_depth
;
92 struct GTY((for_user
)) spec_entry
99 struct spec_hasher
: ggc_hasher
<spec_entry
*>
101 static hashval_t
hash (spec_entry
*);
102 static bool equal (spec_entry
*, spec_entry
*);
105 static GTY (()) hash_table
<spec_hasher
> *decl_specializations
;
107 static GTY (()) hash_table
<spec_hasher
> *type_specializations
;
109 /* Contains canonical template parameter types. The vector is indexed by
110 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
111 TREE_LIST, whose TREE_VALUEs contain the canonical template
112 parameters of various types and levels. */
113 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
115 #define UNIFY_ALLOW_NONE 0
116 #define UNIFY_ALLOW_MORE_CV_QUAL 1
117 #define UNIFY_ALLOW_LESS_CV_QUAL 2
118 #define UNIFY_ALLOW_DERIVED 4
119 #define UNIFY_ALLOW_INTEGER 8
120 #define UNIFY_ALLOW_OUTER_LEVEL 16
121 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
122 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
124 enum template_base_result
{
126 tbr_ambiguous_baseclass
,
130 static void push_access_scope (tree
);
131 static void pop_access_scope (tree
);
132 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
133 unification_kind_t
, int,
135 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
136 unification_kind_t
, int, bool, bool);
137 static int unify (tree
, tree
, tree
, tree
, int, bool);
138 static void add_pending_template (tree
);
139 static tree
reopen_tinst_level (struct tinst_level
*);
140 static tree
tsubst_initializer_list (tree
, tree
);
141 static tree
get_partial_spec_bindings (tree
, tree
, tree
, tree
);
142 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
144 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
146 static void tsubst_enum (tree
, tree
, tree
);
147 static tree
add_to_template_args (tree
, tree
);
148 static tree
add_outermost_template_args (tree
, tree
);
149 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
150 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
152 static int type_unification_real (tree
, tree
, tree
, const tree
*,
153 unsigned int, int, unification_kind_t
, int,
154 vec
<deferred_access_check
, va_gc
> **,
156 static void note_template_header (int);
157 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
158 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
159 static tree
convert_template_argument (tree
, tree
, tree
,
160 tsubst_flags_t
, int, tree
);
161 static int for_each_template_parm (tree
, tree_fn_t
, void*,
162 hash_set
<tree
> *, bool);
163 static tree
expand_template_argument_pack (tree
);
164 static tree
build_template_parm_index (int, int, int, tree
, tree
);
165 static bool inline_needs_template_parms (tree
, bool);
166 static void push_inline_template_parms_recursive (tree
, int);
167 static tree
retrieve_local_specialization (tree
);
168 static void register_local_specialization (tree
, tree
);
169 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
170 static int mark_template_parm (tree
, void *);
171 static int template_parm_this_level_p (tree
, void *);
172 static tree
tsubst_friend_function (tree
, tree
);
173 static tree
tsubst_friend_class (tree
, tree
);
174 static int can_complete_type_without_circularity (tree
);
175 static tree
get_bindings (tree
, tree
, tree
, bool);
176 static int template_decl_level (tree
);
177 static int check_cv_quals_for_unify (int, tree
, tree
);
178 static void template_parm_level_and_index (tree
, int*, int*);
179 static int unify_pack_expansion (tree
, tree
, tree
,
180 tree
, unification_kind_t
, bool, bool);
181 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
182 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
183 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
184 static void regenerate_decl_from_template (tree
, tree
);
185 static tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
186 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
187 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
188 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
189 static bool check_specialization_scope (void);
190 static tree
process_partial_specialization (tree
);
191 static void set_current_access_from_decl (tree
);
192 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
194 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
195 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
197 static bool template_template_parm_bindings_ok_p (tree
, tree
);
198 static int template_args_equal (tree
, tree
);
199 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
200 static tree
for_each_template_parm_r (tree
*, int *, void *);
201 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
202 static void copy_default_args_to_explicit_spec (tree
);
203 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
204 static bool dependent_template_arg_p (tree
);
205 static bool any_template_arguments_need_structural_equality_p (tree
);
206 static bool dependent_type_p_r (tree
);
207 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
, bool);
208 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
209 static tree
tsubst_pack_expansion (tree
, tree
, tsubst_flags_t
, tree
);
210 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
211 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
212 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
214 static tree
listify (tree
);
215 static tree
listify_autos (tree
, tree
);
216 static tree
template_parm_to_arg (tree t
);
217 static tree
current_template_args (void);
218 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
219 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
221 /* Make the current scope suitable for access checking when we are
222 processing T. T can be FUNCTION_DECL for instantiated function
223 template, VAR_DECL for static member variable, or TYPE_DECL for
224 alias template (needed by instantiate_decl). */
227 push_access_scope (tree t
)
229 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
230 || TREE_CODE (t
) == TYPE_DECL
);
232 if (DECL_FRIEND_CONTEXT (t
))
233 push_nested_class (DECL_FRIEND_CONTEXT (t
));
234 else if (DECL_CLASS_SCOPE_P (t
))
235 push_nested_class (DECL_CONTEXT (t
));
237 push_to_top_level ();
239 if (TREE_CODE (t
) == FUNCTION_DECL
)
241 saved_access_scope
= tree_cons
242 (NULL_TREE
, current_function_decl
, saved_access_scope
);
243 current_function_decl
= t
;
247 /* Restore the scope set up by push_access_scope. T is the node we
251 pop_access_scope (tree t
)
253 if (TREE_CODE (t
) == FUNCTION_DECL
)
255 current_function_decl
= TREE_VALUE (saved_access_scope
);
256 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
259 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
262 pop_from_top_level ();
265 /* Do any processing required when DECL (a member template
266 declaration) is finished. Returns the TEMPLATE_DECL corresponding
267 to DECL, unless it is a specialization, in which case the DECL
268 itself is returned. */
271 finish_member_template_decl (tree decl
)
273 if (decl
== error_mark_node
)
274 return error_mark_node
;
276 gcc_assert (DECL_P (decl
));
278 if (TREE_CODE (decl
) == TYPE_DECL
)
282 type
= TREE_TYPE (decl
);
283 if (type
== error_mark_node
)
284 return error_mark_node
;
285 if (MAYBE_CLASS_TYPE_P (type
)
286 && CLASSTYPE_TEMPLATE_INFO (type
)
287 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
289 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
290 check_member_template (tmpl
);
295 else if (TREE_CODE (decl
) == FIELD_DECL
)
296 error ("data member %qD cannot be a member template", decl
);
297 else if (DECL_TEMPLATE_INFO (decl
))
299 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
301 check_member_template (DECL_TI_TEMPLATE (decl
));
302 return DECL_TI_TEMPLATE (decl
);
308 error ("invalid member template declaration %qD", decl
);
310 return error_mark_node
;
313 /* Create a template info node. */
316 build_template_info (tree template_decl
, tree template_args
)
318 tree result
= make_node (TEMPLATE_INFO
);
319 TI_TEMPLATE (result
) = template_decl
;
320 TI_ARGS (result
) = template_args
;
324 /* Return the template info node corresponding to T, whatever T is. */
327 get_template_info (const_tree t
)
329 tree tinfo
= NULL_TREE
;
331 if (!t
|| t
== error_mark_node
)
334 if (TREE_CODE (t
) == NAMESPACE_DECL
)
337 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
338 tinfo
= DECL_TEMPLATE_INFO (t
);
340 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
343 if (OVERLOAD_TYPE_P (t
))
344 tinfo
= TYPE_TEMPLATE_INFO (t
);
345 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
346 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
351 /* Returns the template nesting level of the indicated class TYPE.
361 A<T>::B<U> has depth two, while A<T> has depth one.
362 Both A<T>::B<int> and A<int>::B<U> have depth one, if
363 they are instantiations, not specializations.
365 This function is guaranteed to return 0 if passed NULL_TREE so
366 that, for example, `template_class_depth (current_class_type)' is
370 template_class_depth (tree type
)
375 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
376 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
377 ? CP_DECL_CONTEXT (type
) : CP_TYPE_CONTEXT (type
))
379 tree tinfo
= get_template_info (type
);
381 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
382 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
389 /* Subroutine of maybe_begin_member_template_processing.
390 Returns true if processing DECL needs us to push template parms. */
393 inline_needs_template_parms (tree decl
, bool nsdmi
)
395 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
398 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
399 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
402 /* Subroutine of maybe_begin_member_template_processing.
403 Push the template parms in PARMS, starting from LEVELS steps into the
404 chain, and ending at the beginning, since template parms are listed
408 push_inline_template_parms_recursive (tree parmlist
, int levels
)
410 tree parms
= TREE_VALUE (parmlist
);
414 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
416 ++processing_template_decl
;
417 current_template_parms
418 = tree_cons (size_int (processing_template_decl
),
419 parms
, current_template_parms
);
420 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
422 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
424 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
426 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
428 if (error_operand_p (parm
))
431 gcc_assert (DECL_P (parm
));
433 switch (TREE_CODE (parm
))
442 /* Make a CONST_DECL as is done in process_template_parm.
443 It is ugly that we recreate this here; the original
444 version built in process_template_parm is no longer
446 tree decl
= build_decl (DECL_SOURCE_LOCATION (parm
),
447 CONST_DECL
, DECL_NAME (parm
),
449 DECL_ARTIFICIAL (decl
) = 1;
450 TREE_CONSTANT (decl
) = 1;
451 TREE_READONLY (decl
) = 1;
452 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
453 SET_DECL_TEMPLATE_PARM_P (decl
);
464 /* Restore the template parameter context for a member template, a
465 friend template defined in a class definition, or a non-template
466 member of template class. */
469 maybe_begin_member_template_processing (tree decl
)
473 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
477 tree ctx
= DECL_CONTEXT (decl
);
478 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
479 /* Disregard full specializations (c++/60999). */
480 && uses_template_parms (ctx
)
481 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
484 if (inline_needs_template_parms (decl
, nsdmi
))
486 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
487 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
489 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
492 parms
= TREE_CHAIN (parms
);
495 push_inline_template_parms_recursive (parms
, levels
);
498 /* Remember how many levels of template parameters we pushed so that
499 we can pop them later. */
500 inline_parm_levels
.safe_push (levels
);
503 /* Undo the effects of maybe_begin_member_template_processing. */
506 maybe_end_member_template_processing (void)
511 if (inline_parm_levels
.length () == 0)
514 last
= inline_parm_levels
.pop ();
515 for (i
= 0; i
< last
; ++i
)
517 --processing_template_decl
;
518 current_template_parms
= TREE_CHAIN (current_template_parms
);
523 /* Return a new template argument vector which contains all of ARGS,
524 but has as its innermost set of arguments the EXTRA_ARGS. */
527 add_to_template_args (tree args
, tree extra_args
)
534 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
537 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
538 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
540 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
541 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
543 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
544 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
549 /* Like add_to_template_args, but only the outermost ARGS are added to
550 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
551 (EXTRA_ARGS) levels are added. This function is used to combine
552 the template arguments from a partial instantiation with the
553 template arguments used to attain the full instantiation from the
554 partial instantiation. */
557 add_outermost_template_args (tree args
, tree extra_args
)
561 /* If there are more levels of EXTRA_ARGS than there are ARGS,
562 something very fishy is going on. */
563 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
565 /* If *all* the new arguments will be the EXTRA_ARGS, just return
567 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
570 /* For the moment, we make ARGS look like it contains fewer levels. */
571 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
573 new_args
= add_to_template_args (args
, extra_args
);
575 /* Now, we restore ARGS to its full dimensions. */
576 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
581 /* Return the N levels of innermost template arguments from the ARGS. */
584 get_innermost_template_args (tree args
, int n
)
592 /* If N is 1, just return the innermost set of template arguments. */
594 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
596 /* If we're not removing anything, just return the arguments we were
598 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
599 gcc_assert (extra_levels
>= 0);
600 if (extra_levels
== 0)
603 /* Make a new set of arguments, not containing the outer arguments. */
604 new_args
= make_tree_vec (n
);
605 for (i
= 1; i
<= n
; ++i
)
606 SET_TMPL_ARGS_LEVEL (new_args
, i
,
607 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
612 /* The inverse of get_innermost_template_args: Return all but the innermost
613 EXTRA_LEVELS levels of template arguments from the ARGS. */
616 strip_innermost_template_args (tree args
, int extra_levels
)
619 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
624 /* If N is 1, just return the outermost set of template arguments. */
626 return TMPL_ARGS_LEVEL (args
, 1);
628 /* If we're not removing anything, just return the arguments we were
630 gcc_assert (extra_levels
>= 0);
631 if (extra_levels
== 0)
634 /* Make a new set of arguments, not containing the inner arguments. */
635 new_args
= make_tree_vec (n
);
636 for (i
= 1; i
<= n
; ++i
)
637 SET_TMPL_ARGS_LEVEL (new_args
, i
,
638 TMPL_ARGS_LEVEL (args
, i
));
643 /* We've got a template header coming up; push to a new level for storing
647 begin_template_parm_list (void)
649 /* We use a non-tag-transparent scope here, which causes pushtag to
650 put tags in this scope, rather than in the enclosing class or
651 namespace scope. This is the right thing, since we want
652 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
653 global template class, push_template_decl handles putting the
654 TEMPLATE_DECL into top-level scope. For a nested template class,
657 template <class T> struct S1 {
658 template <class T> struct S2 {};
661 pushtag contains special code to call pushdecl_with_scope on the
662 TEMPLATE_DECL for S2. */
663 begin_scope (sk_template_parms
, NULL
);
664 ++processing_template_decl
;
665 ++processing_template_parmlist
;
666 note_template_header (0);
669 /* This routine is called when a specialization is declared. If it is
670 invalid to declare a specialization here, an error is reported and
671 false is returned, otherwise this routine will return true. */
674 check_specialization_scope (void)
676 tree scope
= current_scope ();
680 An explicit specialization shall be declared in the namespace of
681 which the template is a member, or, for member templates, in the
682 namespace of which the enclosing class or enclosing class
683 template is a member. An explicit specialization of a member
684 function, member class or static data member of a class template
685 shall be declared in the namespace of which the class template
687 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
689 error ("explicit specialization in non-namespace scope %qD", scope
);
695 In an explicit specialization declaration for a member of a class
696 template or a member template that appears in namespace scope,
697 the member template and some of its enclosing class templates may
698 remain unspecialized, except that the declaration shall not
699 explicitly specialize a class member template if its enclosing
700 class templates are not explicitly specialized as well. */
701 if (current_template_parms
)
703 error ("enclosing class templates are not explicitly specialized");
710 /* We've just seen template <>. */
713 begin_specialization (void)
715 begin_scope (sk_template_spec
, NULL
);
716 note_template_header (1);
717 return check_specialization_scope ();
720 /* Called at then end of processing a declaration preceded by
724 end_specialization (void)
727 reset_specialization ();
730 /* Any template <>'s that we have seen thus far are not referring to a
731 function specialization. */
734 reset_specialization (void)
736 processing_specialization
= 0;
737 template_header_count
= 0;
740 /* We've just seen a template header. If SPECIALIZATION is nonzero,
741 it was of the form template <>. */
744 note_template_header (int specialization
)
746 processing_specialization
= specialization
;
747 template_header_count
++;
750 /* We're beginning an explicit instantiation. */
753 begin_explicit_instantiation (void)
755 gcc_assert (!processing_explicit_instantiation
);
756 processing_explicit_instantiation
= true;
761 end_explicit_instantiation (void)
763 gcc_assert (processing_explicit_instantiation
);
764 processing_explicit_instantiation
= false;
767 /* An explicit specialization or partial specialization of TMPL is being
768 declared. Check that the namespace in which the specialization is
769 occurring is permissible. Returns false iff it is invalid to
770 specialize TMPL in the current namespace. */
773 check_specialization_namespace (tree tmpl
)
775 tree tpl_ns
= decl_namespace_context (tmpl
);
779 An explicit specialization shall be declared in the namespace of
780 which the template is a member, or, for member templates, in the
781 namespace of which the enclosing class or enclosing class
782 template is a member. An explicit specialization of a member
783 function, member class or static data member of a class template
784 shall be declared in the namespace of which the class template is
786 if (current_scope() != DECL_CONTEXT (tmpl
)
787 && !at_namespace_scope_p ())
789 error ("specialization of %qD must appear at namespace scope", tmpl
);
792 if (is_associated_namespace (current_namespace
, tpl_ns
))
793 /* Same or super-using namespace. */
797 permerror (input_location
, "specialization of %qD in different namespace", tmpl
);
798 permerror (input_location
, " from definition of %q+#D", tmpl
);
803 /* SPEC is an explicit instantiation. Check that it is valid to
804 perform this explicit instantiation in the current namespace. */
807 check_explicit_instantiation_namespace (tree spec
)
811 /* DR 275: An explicit instantiation shall appear in an enclosing
812 namespace of its template. */
813 ns
= decl_namespace_context (spec
);
814 if (!is_ancestor (current_namespace
, ns
))
815 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
816 "(which does not enclose namespace %qD)",
817 spec
, current_namespace
, ns
);
820 /* The TYPE is being declared. If it is a template type, that means it
821 is a partial specialization. Do appropriate error-checking. */
824 maybe_process_partial_specialization (tree type
)
828 if (type
== error_mark_node
)
829 return error_mark_node
;
831 /* A lambda that appears in specialization context is not itself a
833 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
836 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
838 error ("name of class shadows template template parameter %qD",
840 return error_mark_node
;
843 context
= TYPE_CONTEXT (type
);
845 if (TYPE_ALIAS_P (type
))
847 if (TYPE_TEMPLATE_INFO (type
)
848 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type
)))
849 error ("specialization of alias template %qD",
850 TYPE_TI_TEMPLATE (type
));
852 error ("explicit specialization of non-template %qT", type
);
853 return error_mark_node
;
855 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
857 /* This is for ordinary explicit specialization and partial
858 specialization of a template class such as:
860 template <> class C<int>;
864 template <class T> class C<T*>;
866 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
868 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
869 && !COMPLETE_TYPE_P (type
))
871 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
))
872 && !at_namespace_scope_p ())
873 return error_mark_node
;
874 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
875 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
876 if (processing_template_decl
)
878 if (push_template_decl (TYPE_MAIN_DECL (type
))
880 return error_mark_node
;
883 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
884 error ("specialization of %qT after instantiation", type
);
885 else if (errorcount
&& !processing_specialization
886 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
887 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
888 /* Trying to define a specialization either without a template<> header
889 or in an inappropriate place. We've already given an error, so just
890 bail now so we don't actually define the specialization. */
891 return error_mark_node
;
893 else if (CLASS_TYPE_P (type
)
894 && !CLASSTYPE_USE_TEMPLATE (type
)
895 && CLASSTYPE_TEMPLATE_INFO (type
)
896 && context
&& CLASS_TYPE_P (context
)
897 && CLASSTYPE_TEMPLATE_INFO (context
))
899 /* This is for an explicit specialization of member class
900 template according to [temp.expl.spec/18]:
902 template <> template <class U> class C<int>::D;
904 The context `C<int>' must be an implicit instantiation.
905 Otherwise this is just a member class template declared
908 template <> class C<int> { template <class U> class D; };
909 template <> template <class U> class C<int>::D;
911 In the first case, `C<int>::D' is a specialization of `C<T>::D'
912 while in the second case, `C<int>::D' is a primary template
913 and `C<T>::D' may not exist. */
915 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
916 && !COMPLETE_TYPE_P (type
))
919 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
921 if (current_namespace
922 != decl_namespace_context (tmpl
))
924 permerror (input_location
, "specializing %q#T in different namespace", type
);
925 permerror (input_location
, " from definition of %q+#D", tmpl
);
928 /* Check for invalid specialization after instantiation:
930 template <> template <> class C<int>::D<int>;
931 template <> template <class U> class C<int>::D; */
933 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
934 t
; t
= TREE_CHAIN (t
))
936 tree inst
= TREE_VALUE (t
);
937 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
938 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
940 /* We already have a full specialization of this partial
941 instantiation, or a full specialization has been
942 looked up but not instantiated. Reassign it to the
943 new member specialization template. */
947 elt
.tmpl
= most_general_template (tmpl
);
948 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
951 type_specializations
->remove_elt (&elt
);
954 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
957 = type_specializations
->find_slot (&elt
, INSERT
);
958 entry
= ggc_alloc
<spec_entry
> ();
963 /* But if we've had an implicit instantiation, that's a
964 problem ([temp.expl.spec]/6). */
965 error ("specialization %qT after instantiation %qT",
969 /* Mark TYPE as a specialization. And as a result, we only
970 have one level of template argument for the innermost
972 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
973 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
974 CLASSTYPE_TI_ARGS (type
)
975 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
978 else if (processing_specialization
)
980 /* Someday C++0x may allow for enum template specialization. */
981 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
982 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
983 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
984 "of %qD not allowed by ISO C++", type
);
987 error ("explicit specialization of non-template %qT", type
);
988 return error_mark_node
;
995 /* Returns nonzero if we can optimize the retrieval of specializations
996 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
997 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1000 optimize_specialization_lookup_p (tree tmpl
)
1002 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1003 && DECL_CLASS_SCOPE_P (tmpl
)
1004 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1006 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1007 /* The optimized lookup depends on the fact that the
1008 template arguments for the member function template apply
1009 purely to the containing class, which is not true if the
1010 containing class is an explicit or partial
1012 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1013 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1014 && !DECL_CONV_FN_P (tmpl
)
1015 /* It is possible to have a template that is not a member
1016 template and is not a member of a template class:
1018 template <typename T>
1019 struct S { friend A::f(); };
1021 Here, the friend function is a template, but the context does
1022 not have template information. The optimized lookup relies
1023 on having ARGS be the template arguments for both the class
1024 and the function template. */
1025 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1028 /* Retrieve the specialization (in the sense of [temp.spec] - a
1029 specialization is either an instantiation or an explicit
1030 specialization) of TMPL for the given template ARGS. If there is
1031 no such specialization, return NULL_TREE. The ARGS are a vector of
1032 arguments, or a vector of vectors of arguments, in the case of
1033 templates with more than one level of parameters.
1035 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1036 then we search for a partial specialization matching ARGS. This
1037 parameter is ignored if TMPL is not a class template.
1039 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1040 result is a NONTYPE_ARGUMENT_PACK. */
1043 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1045 if (tmpl
== NULL_TREE
)
1048 if (args
== error_mark_node
)
1051 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1052 || TREE_CODE (tmpl
) == FIELD_DECL
);
1054 /* There should be as many levels of arguments as there are
1055 levels of parameters. */
1056 gcc_assert (TMPL_ARGS_DEPTH (args
)
1057 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1058 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1059 : template_class_depth (DECL_CONTEXT (tmpl
))));
1061 #ifdef ENABLE_CHECKING
1062 /* We should have gone through coerce_template_parms by now. */
1063 ++processing_template_decl
;
1064 if (!any_dependent_template_arguments_p (args
))
1065 gcc_assert (strip_typedefs_expr (args
, NULL
) == args
);
1066 --processing_template_decl
;
1069 if (optimize_specialization_lookup_p (tmpl
))
1071 tree class_template
;
1072 tree class_specialization
;
1073 vec
<tree
, va_gc
> *methods
;
1077 /* The template arguments actually apply to the containing
1078 class. Find the class specialization with those
1080 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1081 class_specialization
1082 = retrieve_specialization (class_template
, args
, 0);
1083 if (!class_specialization
)
1085 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1086 for the specialization. */
1087 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1090 /* Iterate through the methods with the indicated name, looking
1091 for the one that has an instance of TMPL. */
1092 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1093 for (fns
= (*methods
)[idx
]; fns
; fns
= OVL_NEXT (fns
))
1095 tree fn
= OVL_CURRENT (fns
);
1096 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1097 /* using-declarations can add base methods to the method vec,
1098 and we don't want those here. */
1099 && DECL_CONTEXT (fn
) == class_specialization
)
1108 hash_table
<spec_hasher
> *specializations
;
1112 elt
.spec
= NULL_TREE
;
1114 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1115 specializations
= type_specializations
;
1117 specializations
= decl_specializations
;
1120 hash
= spec_hasher::hash (&elt
);
1121 found
= specializations
->find_with_hash (&elt
, hash
);
1129 /* Like retrieve_specialization, but for local declarations. */
1132 retrieve_local_specialization (tree tmpl
)
1134 if (local_specializations
== NULL
)
1137 tree
*slot
= local_specializations
->get (tmpl
);
1138 return slot
? *slot
: NULL_TREE
;
1141 /* Returns nonzero iff DECL is a specialization of TMPL. */
1144 is_specialization_of (tree decl
, tree tmpl
)
1148 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1152 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1158 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1160 for (t
= TREE_TYPE (decl
);
1162 t
= CLASSTYPE_USE_TEMPLATE (t
)
1163 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1164 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1171 /* Returns nonzero iff DECL is a specialization of friend declaration
1172 FRIEND_DECL according to [temp.friend]. */
1175 is_specialization_of_friend (tree decl
, tree friend_decl
)
1177 bool need_template
= true;
1180 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1181 || TREE_CODE (decl
) == TYPE_DECL
);
1183 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1184 of a template class, we want to check if DECL is a specialization
1186 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1187 && DECL_TEMPLATE_INFO (friend_decl
)
1188 && !DECL_USE_TEMPLATE (friend_decl
))
1190 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1191 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1192 need_template
= false;
1194 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1195 && !PRIMARY_TEMPLATE_P (friend_decl
))
1196 need_template
= false;
1198 /* There is nothing to do if this is not a template friend. */
1199 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1202 if (is_specialization_of (decl
, friend_decl
))
1206 A member of a class template may be declared to be a friend of a
1207 non-template class. In this case, the corresponding member of
1208 every specialization of the class template is a friend of the
1209 class granting friendship.
1211 For example, given a template friend declaration
1213 template <class T> friend void A<T>::f();
1215 the member function below is considered a friend
1217 template <> struct A<int> {
1221 For this type of template friend, TEMPLATE_DEPTH below will be
1222 nonzero. To determine if DECL is a friend of FRIEND, we first
1223 check if the enclosing class is a specialization of another. */
1225 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1227 && DECL_CLASS_SCOPE_P (decl
)
1228 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1229 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1231 /* Next, we check the members themselves. In order to handle
1232 a few tricky cases, such as when FRIEND_DECL's are
1234 template <class T> friend void A<T>::g(T t);
1235 template <class T> template <T t> friend void A<T>::h();
1239 void A<int>::g(int);
1240 template <int> void A<int>::h();
1242 we need to figure out ARGS, the template arguments from
1243 the context of DECL. This is required for template substitution
1244 of `T' in the function parameter of `g' and template parameter
1245 of `h' in the above examples. Here ARGS corresponds to `int'. */
1247 tree context
= DECL_CONTEXT (decl
);
1248 tree args
= NULL_TREE
;
1249 int current_depth
= 0;
1251 while (current_depth
< template_depth
)
1253 if (CLASSTYPE_TEMPLATE_INFO (context
))
1255 if (current_depth
== 0)
1256 args
= TYPE_TI_ARGS (context
);
1258 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1261 context
= TYPE_CONTEXT (context
);
1264 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1269 tree friend_args_type
;
1270 tree decl_args_type
;
1272 /* Make sure that both DECL and FRIEND_DECL are templates or
1274 is_template
= DECL_TEMPLATE_INFO (decl
)
1275 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1276 if (need_template
^ is_template
)
1278 else if (is_template
)
1280 /* If both are templates, check template parameter list. */
1282 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1284 if (!comp_template_parms
1285 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1289 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1292 decl_type
= TREE_TYPE (decl
);
1294 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1295 tf_none
, NULL_TREE
);
1296 if (friend_type
== error_mark_node
)
1299 /* Check if return types match. */
1300 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1303 /* Check if function parameter types match, ignoring the
1304 `this' parameter. */
1305 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1306 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1307 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1308 friend_args_type
= TREE_CHAIN (friend_args_type
);
1309 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1310 decl_args_type
= TREE_CHAIN (decl_args_type
);
1312 return compparms (decl_args_type
, friend_args_type
);
1316 /* DECL is a TYPE_DECL */
1318 tree decl_type
= TREE_TYPE (decl
);
1320 /* Make sure that both DECL and FRIEND_DECL are templates or
1323 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1324 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1326 if (need_template
^ is_template
)
1328 else if (is_template
)
1331 /* If both are templates, check the name of the two
1332 TEMPLATE_DECL's first because is_friend didn't. */
1333 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1334 != DECL_NAME (friend_decl
))
1337 /* Now check template parameter list. */
1339 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1341 return comp_template_parms
1342 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1346 return (DECL_NAME (decl
)
1347 == DECL_NAME (friend_decl
));
1353 /* Register the specialization SPEC as a specialization of TMPL with
1354 the indicated ARGS. IS_FRIEND indicates whether the specialization
1355 is actually just a friend declaration. Returns SPEC, or an
1356 equivalent prior declaration, if available.
1358 We also store instantiations of field packs in the hash table, even
1359 though they are not themselves templates, to make lookup easier. */
1362 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1366 spec_entry
**slot
= NULL
;
1369 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1370 || (TREE_CODE (tmpl
) == FIELD_DECL
1371 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1373 if (TREE_CODE (spec
) == FUNCTION_DECL
1374 && uses_template_parms (DECL_TI_ARGS (spec
)))
1375 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1376 register it; we want the corresponding TEMPLATE_DECL instead.
1377 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1378 the more obvious `uses_template_parms (spec)' to avoid problems
1379 with default function arguments. In particular, given
1380 something like this:
1382 template <class T> void f(T t1, T t = T())
1384 the default argument expression is not substituted for in an
1385 instantiation unless and until it is actually needed. */
1388 if (optimize_specialization_lookup_p (tmpl
))
1389 /* We don't put these specializations in the hash table, but we might
1390 want to give an error about a mismatch. */
1391 fn
= retrieve_specialization (tmpl
, args
, 0);
1399 hash
= spec_hasher::hash (&elt
);
1402 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1404 fn
= ((spec_entry
*) *slot
)->spec
;
1409 /* We can sometimes try to re-register a specialization that we've
1410 already got. In particular, regenerate_decl_from_template calls
1411 duplicate_decls which will update the specialization list. But,
1412 we'll still get called again here anyhow. It's more convenient
1413 to simply allow this than to try to prevent it. */
1416 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1418 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1420 if (DECL_ODR_USED (fn
)
1421 || DECL_EXPLICIT_INSTANTIATION (fn
))
1423 error ("specialization of %qD after instantiation",
1425 return error_mark_node
;
1430 /* This situation should occur only if the first
1431 specialization is an implicit instantiation, the
1432 second is an explicit specialization, and the
1433 implicit instantiation has not yet been used. That
1434 situation can occur if we have implicitly
1435 instantiated a member function and then specialized
1438 We can also wind up here if a friend declaration that
1439 looked like an instantiation turns out to be a
1442 template <class T> void foo(T);
1443 class S { friend void foo<>(int) };
1444 template <> void foo(int);
1446 We transform the existing DECL in place so that any
1447 pointers to it become pointers to the updated
1450 If there was a definition for the template, but not
1451 for the specialization, we want this to look as if
1452 there were no definition, and vice versa. */
1453 DECL_INITIAL (fn
) = NULL_TREE
;
1454 duplicate_decls (spec
, fn
, is_friend
);
1455 /* The call to duplicate_decls will have applied
1458 An explicit specialization of a function template
1459 is inline only if it is explicitly declared to be,
1460 and independently of whether its function template
1463 to the primary function; now copy the inline bits to
1464 the various clones. */
1465 FOR_EACH_CLONE (clone
, fn
)
1467 DECL_DECLARED_INLINE_P (clone
)
1468 = DECL_DECLARED_INLINE_P (fn
);
1469 DECL_SOURCE_LOCATION (clone
)
1470 = DECL_SOURCE_LOCATION (fn
);
1471 DECL_DELETED_FN (clone
)
1472 = DECL_DELETED_FN (fn
);
1474 check_specialization_namespace (tmpl
);
1479 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1481 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1482 /* Dup decl failed, but this is a new definition. Set the
1483 line number so any errors match this new
1485 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1491 return duplicate_decls (spec
, fn
, is_friend
);
1493 /* A specialization must be declared in the same namespace as the
1494 template it is specializing. */
1495 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1496 && !check_specialization_namespace (tmpl
))
1497 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1499 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1501 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1502 gcc_assert (tmpl
&& args
&& spec
);
1505 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1506 && PRIMARY_TEMPLATE_P (tmpl
)
1507 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1508 || variable_template_p (tmpl
))
1509 /* If TMPL is a forward declaration of a template function, keep a list
1510 of all specializations in case we need to reassign them to a friend
1511 template later in tsubst_friend_function.
1513 Also keep a list of all variable template instantiations so that
1514 process_partial_specialization can check whether a later partial
1515 specialization would have used it. */
1516 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1517 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1523 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1524 TMPL and ARGS members, ignores SPEC. */
1526 int comparing_specializations
;
1529 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1533 ++comparing_specializations
;
1534 equal
= (e1
->tmpl
== e2
->tmpl
1535 && comp_template_args (e1
->args
, e2
->args
));
1536 --comparing_specializations
;
1541 /* Returns a hash for a template TMPL and template arguments ARGS. */
1544 hash_tmpl_and_args (tree tmpl
, tree args
)
1546 hashval_t val
= DECL_UID (tmpl
);
1547 return iterative_hash_template_arg (args
, val
);
1550 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1554 spec_hasher::hash (spec_entry
*e
)
1556 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1559 /* Recursively calculate a hash value for a template argument ARG, for use
1560 in the hash tables of template specializations. */
1563 iterative_hash_template_arg (tree arg
, hashval_t val
)
1565 unsigned HOST_WIDE_INT i
;
1566 enum tree_code code
;
1569 if (arg
== NULL_TREE
)
1570 return iterative_hash_object (arg
, val
);
1575 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1576 /* We can get one of these when re-hashing a previous entry in the middle
1577 of substituting into a pack expansion. Just look through it. */
1578 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1580 code
= TREE_CODE (arg
);
1581 tclass
= TREE_CODE_CLASS (code
);
1583 val
= iterative_hash_object (code
, val
);
1590 case IDENTIFIER_NODE
:
1591 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1595 int i
, len
= TREE_VEC_LENGTH (arg
);
1596 for (i
= 0; i
< len
; ++i
)
1597 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1601 case TYPE_PACK_EXPANSION
:
1602 case EXPR_PACK_EXPANSION
:
1603 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1604 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1606 case TYPE_ARGUMENT_PACK
:
1607 case NONTYPE_ARGUMENT_PACK
:
1608 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1611 for (; arg
; arg
= TREE_CHAIN (arg
))
1612 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1616 for (; arg
; arg
= OVL_NEXT (arg
))
1617 val
= iterative_hash_template_arg (OVL_CURRENT (arg
), val
);
1623 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1624 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1626 val
= iterative_hash_template_arg (field
, val
);
1627 val
= iterative_hash_template_arg (value
, val
);
1633 if (!DECL_ARTIFICIAL (arg
))
1635 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1636 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1638 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1641 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1644 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1645 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1647 case TEMPLATE_PARM_INDEX
:
1648 val
= iterative_hash_template_arg
1649 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1650 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1651 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1654 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1655 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1656 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1659 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1661 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1665 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1666 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1667 val
= iterative_hash_object (code
, val
);
1668 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1671 /* A lambda can't appear in a template arg, but don't crash on
1673 gcc_assert (seen_error ());
1677 case IMPLICIT_CONV_EXPR
:
1678 case STATIC_CAST_EXPR
:
1679 case REINTERPRET_CAST_EXPR
:
1680 case CONST_CAST_EXPR
:
1681 case DYNAMIC_CAST_EXPR
:
1683 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1684 /* Now hash operands as usual. */
1694 if (alias_template_specialization_p (arg
))
1696 // We want an alias specialization that survived strip_typedefs
1697 // to hash differently from its TYPE_CANONICAL, to avoid hash
1698 // collisions that compare as different in template_args_equal.
1699 // These could be dependent specializations that strip_typedefs
1700 // left alone, or untouched specializations because
1701 // coerce_template_parms returns the unconverted template
1702 // arguments if it sees incomplete argument packs.
1703 tree ti
= TYPE_TEMPLATE_INFO (arg
);
1704 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1706 if (TYPE_CANONICAL (arg
))
1707 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1709 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1710 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1711 /* Otherwise just compare the types during lookup. */
1714 case tcc_declaration
:
1716 return iterative_hash_expr (arg
, val
);
1719 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1721 unsigned n
= cp_tree_operand_length (arg
);
1722 for (i
= 0; i
< n
; ++i
)
1723 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1731 /* Unregister the specialization SPEC as a specialization of TMPL.
1732 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1733 if the SPEC was listed as a specialization of TMPL.
1735 Note that SPEC has been ggc_freed, so we can't look inside it. */
1738 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1743 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1744 elt
.args
= TI_ARGS (tinfo
);
1745 elt
.spec
= NULL_TREE
;
1747 entry
= decl_specializations
->find (&elt
);
1750 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1751 gcc_assert (new_spec
!= NULL_TREE
);
1752 entry
->spec
= new_spec
;
1759 /* Like register_specialization, but for local declarations. We are
1760 registering SPEC, an instantiation of TMPL. */
1763 register_local_specialization (tree spec
, tree tmpl
)
1765 local_specializations
->put (tmpl
, spec
);
1768 /* TYPE is a class type. Returns true if TYPE is an explicitly
1769 specialized class. */
1772 explicit_class_specialization_p (tree type
)
1774 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1776 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1779 /* Print the list of functions at FNS, going through all the overloads
1780 for each element of the list. Alternatively, FNS can not be a
1781 TREE_LIST, in which case it will be printed together with all the
1784 MORE and *STR should respectively be FALSE and NULL when the function
1785 is called from the outside. They are used internally on recursive
1786 calls. print_candidates manages the two parameters and leaves NULL
1787 in *STR when it ends. */
1790 print_candidates_1 (tree fns
, bool more
, const char **str
)
1793 char *spaces
= NULL
;
1795 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1796 if (TREE_CODE (fn
) == TREE_LIST
)
1798 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1799 print_candidates_1 (TREE_VALUE (fn2
),
1800 TREE_CHAIN (fn2
) || more
, str
);
1804 tree cand
= OVL_CURRENT (fn
);
1807 /* Pick the prefix string. */
1808 if (!more
&& !OVL_NEXT (fns
))
1810 inform (DECL_SOURCE_LOCATION (cand
),
1811 "candidate is: %#D", cand
);
1815 *str
= _("candidates are:");
1816 spaces
= get_spaces (*str
);
1818 inform (DECL_SOURCE_LOCATION (cand
), "%s %#D", *str
, cand
);
1819 *str
= spaces
? spaces
: *str
;
1829 /* Print the list of candidate FNS in an error message. FNS can also
1830 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1833 print_candidates (tree fns
)
1835 const char *str
= NULL
;
1836 print_candidates_1 (fns
, false, &str
);
1837 gcc_assert (str
== NULL
);
1840 /* Returns the template (one of the functions given by TEMPLATE_ID)
1841 which can be specialized to match the indicated DECL with the
1842 explicit template args given in TEMPLATE_ID. The DECL may be
1843 NULL_TREE if none is available. In that case, the functions in
1844 TEMPLATE_ID are non-members.
1846 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1847 specialization of a member template.
1849 The TEMPLATE_COUNT is the number of references to qualifying
1850 template classes that appeared in the name of the function. See
1851 check_explicit_specialization for a more accurate description.
1853 TSK indicates what kind of template declaration (if any) is being
1854 declared. TSK_TEMPLATE indicates that the declaration given by
1855 DECL, though a FUNCTION_DECL, has template parameters, and is
1856 therefore a template function.
1858 The template args (those explicitly specified and those deduced)
1859 are output in a newly created vector *TARGS_OUT.
1861 If it is impossible to determine the result, an error message is
1862 issued. The error_mark_node is returned to indicate failure. */
1865 determine_specialization (tree template_id
,
1868 int need_member_template
,
1874 tree explicit_targs
;
1875 tree candidates
= NULL_TREE
;
1876 /* A TREE_LIST of templates of which DECL may be a specialization.
1877 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1878 corresponding TREE_PURPOSE is the set of template arguments that,
1879 when used to instantiate the template, would produce a function
1880 with the signature of DECL. */
1881 tree templates
= NULL_TREE
;
1883 cp_binding_level
*b
;
1885 *targs_out
= NULL_TREE
;
1887 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1888 return error_mark_node
;
1890 /* We shouldn't be specializing a member template of an
1891 unspecialized class template; we already gave an error in
1892 check_specialization_scope, now avoid crashing. */
1893 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
1894 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
1896 gcc_assert (errorcount
);
1897 return error_mark_node
;
1900 fns
= TREE_OPERAND (template_id
, 0);
1901 explicit_targs
= TREE_OPERAND (template_id
, 1);
1903 if (fns
== error_mark_node
)
1904 return error_mark_node
;
1906 /* Check for baselinks. */
1907 if (BASELINK_P (fns
))
1908 fns
= BASELINK_FUNCTIONS (fns
);
1910 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
1912 error ("%qD is not a function template", fns
);
1913 return error_mark_node
;
1915 else if (VAR_P (decl
) && !variable_template_p (fns
))
1917 error ("%qD is not a variable template", fns
);
1918 return error_mark_node
;
1921 /* Count the number of template headers specified for this
1924 for (b
= current_binding_level
;
1925 b
->kind
== sk_template_parms
;
1929 if (variable_template_p (fns
))
1931 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
1932 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
1933 tf_warning_or_error
,
1934 /*req_all*/true, /*use_defarg*/true);
1935 templates
= tree_cons (targs
, fns
, templates
);
1937 else for (; fns
; fns
= OVL_NEXT (fns
))
1939 tree fn
= OVL_CURRENT (fns
);
1941 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1943 tree decl_arg_types
;
1947 /* In case of explicit specialization, we need to check if
1948 the number of template headers appearing in the specialization
1949 is correct. This is usually done in check_explicit_specialization,
1950 but the check done there cannot be exhaustive when specializing
1951 member functions. Consider the following code:
1953 template <> void A<int>::f(int);
1954 template <> template <> void A<int>::f(int);
1956 Assuming that A<int> is not itself an explicit specialization
1957 already, the first line specializes "f" which is a non-template
1958 member function, whilst the second line specializes "f" which
1959 is a template member function. So both lines are syntactically
1960 correct, and check_explicit_specialization does not reject
1963 Here, we can do better, as we are matching the specialization
1964 against the declarations. We count the number of template
1965 headers, and we check if they match TEMPLATE_COUNT + 1
1966 (TEMPLATE_COUNT is the number of qualifying template classes,
1967 plus there must be another header for the member template
1970 Notice that if header_count is zero, this is not a
1971 specialization but rather a template instantiation, so there
1972 is no check we can perform here. */
1973 if (header_count
&& header_count
!= template_count
+ 1)
1976 /* Check that the number of template arguments at the
1977 innermost level for DECL is the same as for FN. */
1978 if (current_binding_level
->kind
== sk_template_parms
1979 && !current_binding_level
->explicit_spec_p
1980 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1981 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1982 (current_template_parms
))))
1985 /* DECL might be a specialization of FN. */
1986 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1987 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1989 /* For a non-static member function, we need to make sure
1990 that the const qualification is the same. Since
1991 get_bindings does not try to merge the "this" parameter,
1992 we must do the comparison explicitly. */
1993 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1994 && !same_type_p (TREE_VALUE (fn_arg_types
),
1995 TREE_VALUE (decl_arg_types
)))
1998 /* Skip the "this" parameter and, for constructors of
1999 classes with virtual bases, the VTT parameter. A
2000 full specialization of a constructor will have a VTT
2001 parameter, but a template never will. */
2003 = skip_artificial_parms_for (decl
, decl_arg_types
);
2005 = skip_artificial_parms_for (fn
, fn_arg_types
);
2007 /* Function templates cannot be specializations; there are
2008 no partial specializations of functions. Therefore, if
2009 the type of DECL does not match FN, there is no
2011 if (tsk
== tsk_template
)
2013 if (compparms (fn_arg_types
, decl_arg_types
))
2014 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2018 /* See whether this function might be a specialization of this
2019 template. Suppress access control because we might be trying
2020 to make this specialization a friend, and we have already done
2021 access control for the declaration of the specialization. */
2022 push_deferring_access_checks (dk_no_check
);
2023 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2024 pop_deferring_access_checks ();
2027 /* We cannot deduce template arguments that when used to
2028 specialize TMPL will produce DECL. */
2031 /* Make sure that the deduced arguments actually work. */
2032 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
2033 if (insttype
== error_mark_node
)
2036 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2037 if (!compparms (fn_arg_types
, decl_arg_types
))
2040 /* Save this template, and the arguments deduced. */
2041 templates
= tree_cons (targs
, fn
, templates
);
2043 else if (need_member_template
)
2044 /* FN is an ordinary member function, and we need a
2045 specialization of a member template. */
2047 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2048 /* We can get IDENTIFIER_NODEs here in certain erroneous
2051 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2052 /* This is just an ordinary non-member function. Nothing can
2053 be a specialization of that. */
2055 else if (DECL_ARTIFICIAL (fn
))
2056 /* Cannot specialize functions that are created implicitly. */
2060 tree decl_arg_types
;
2062 /* This is an ordinary member function. However, since
2063 we're here, we can assume its enclosing class is a
2064 template class. For example,
2066 template <typename T> struct S { void f(); };
2067 template <> void S<int>::f() {}
2069 Here, S<int>::f is a non-template, but S<int> is a
2070 template class. If FN has the same type as DECL, we
2071 might be in business. */
2073 if (!DECL_TEMPLATE_INFO (fn
))
2074 /* Its enclosing class is an explicit specialization
2075 of a template class. This is not a candidate. */
2078 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2079 TREE_TYPE (TREE_TYPE (fn
))))
2080 /* The return types differ. */
2083 /* Adjust the type of DECL in case FN is a static member. */
2084 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2085 if (DECL_STATIC_FUNCTION_P (fn
)
2086 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2087 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2089 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2092 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2096 if (templates
&& TREE_CHAIN (templates
))
2102 It is possible for a specialization with a given function
2103 signature to be instantiated from more than one function
2104 template. In such cases, explicit specification of the
2105 template arguments must be used to uniquely identify the
2106 function template specialization being specialized.
2108 Note that here, there's no suggestion that we're supposed to
2109 determine which of the candidate templates is most
2110 specialized. However, we, also have:
2114 Partial ordering of overloaded function template
2115 declarations is used in the following contexts to select
2116 the function template to which a function template
2117 specialization refers:
2119 -- when an explicit specialization refers to a function
2122 So, we do use the partial ordering rules, at least for now.
2123 This extension can only serve to make invalid programs valid,
2124 so it's safe. And, there is strong anecdotal evidence that
2125 the committee intended the partial ordering rules to apply;
2126 the EDG front end has that behavior, and John Spicer claims
2127 that the committee simply forgot to delete the wording in
2128 [temp.expl.spec]. */
2129 tree tmpl
= most_specialized_instantiation (templates
);
2130 if (tmpl
!= error_mark_node
)
2133 TREE_CHAIN (templates
) = NULL_TREE
;
2137 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2139 error ("template-id %qD for %q+D does not match any template "
2140 "declaration", template_id
, decl
);
2141 if (header_count
&& header_count
!= template_count
+ 1)
2142 inform (input_location
, "saw %d %<template<>%>, need %d for "
2143 "specializing a member function template",
2144 header_count
, template_count
+ 1);
2145 return error_mark_node
;
2147 else if ((templates
&& TREE_CHAIN (templates
))
2148 || (candidates
&& TREE_CHAIN (candidates
))
2149 || (templates
&& candidates
))
2151 error ("ambiguous template specialization %qD for %q+D",
2153 candidates
= chainon (candidates
, templates
);
2154 print_candidates (candidates
);
2155 return error_mark_node
;
2158 /* We have one, and exactly one, match. */
2161 tree fn
= TREE_VALUE (candidates
);
2162 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2163 /* DECL is a re-declaration or partial instantiation of a template
2165 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2167 /* It was a specialization of an ordinary member function in a
2169 return DECL_TI_TEMPLATE (fn
);
2172 /* It was a specialization of a template. */
2173 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2174 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2176 *targs_out
= copy_node (targs
);
2177 SET_TMPL_ARGS_LEVEL (*targs_out
,
2178 TMPL_ARGS_DEPTH (*targs_out
),
2179 TREE_PURPOSE (templates
));
2182 *targs_out
= TREE_PURPOSE (templates
);
2183 return TREE_VALUE (templates
);
2186 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2187 but with the default argument values filled in from those in the
2191 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2194 tree new_spec_types
;
2199 if (spec_types
== void_list_node
)
2200 return void_list_node
;
2202 /* Substitute into the rest of the list. */
2204 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2205 TREE_CHAIN (tmpl_types
));
2207 /* Add the default argument for this parameter. */
2208 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2209 TREE_VALUE (spec_types
),
2213 /* DECL is an explicit specialization. Replicate default arguments
2214 from the template it specializes. (That way, code like:
2216 template <class T> void f(T = 3);
2217 template <> void f(double);
2220 works, as required.) An alternative approach would be to look up
2221 the correct default arguments at the call-site, but this approach
2222 is consistent with how implicit instantiations are handled. */
2225 copy_default_args_to_explicit_spec (tree decl
)
2230 tree new_spec_types
;
2234 tree object_type
= NULL_TREE
;
2235 tree in_charge
= NULL_TREE
;
2236 tree vtt
= NULL_TREE
;
2238 /* See if there's anything we need to do. */
2239 tmpl
= DECL_TI_TEMPLATE (decl
);
2240 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2241 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2242 if (TREE_PURPOSE (t
))
2247 old_type
= TREE_TYPE (decl
);
2248 spec_types
= TYPE_ARG_TYPES (old_type
);
2250 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2252 /* Remove the this pointer, but remember the object's type for
2254 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2255 spec_types
= TREE_CHAIN (spec_types
);
2256 tmpl_types
= TREE_CHAIN (tmpl_types
);
2258 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2260 /* DECL may contain more parameters than TMPL due to the extra
2261 in-charge parameter in constructors and destructors. */
2262 in_charge
= spec_types
;
2263 spec_types
= TREE_CHAIN (spec_types
);
2265 if (DECL_HAS_VTT_PARM_P (decl
))
2268 spec_types
= TREE_CHAIN (spec_types
);
2272 /* Compute the merged default arguments. */
2274 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2276 /* Compute the new FUNCTION_TYPE. */
2280 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2285 /* Put the in-charge parameter back. */
2286 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2287 TREE_VALUE (in_charge
),
2290 new_type
= build_method_type_directly (object_type
,
2291 TREE_TYPE (old_type
),
2295 new_type
= build_function_type (TREE_TYPE (old_type
),
2297 new_type
= cp_build_type_attribute_variant (new_type
,
2298 TYPE_ATTRIBUTES (old_type
));
2299 new_type
= build_exception_variant (new_type
,
2300 TYPE_RAISES_EXCEPTIONS (old_type
));
2302 if (TYPE_HAS_LATE_RETURN_TYPE (old_type
))
2303 TYPE_HAS_LATE_RETURN_TYPE (new_type
) = 1;
2305 TREE_TYPE (decl
) = new_type
;
2308 /* Return the number of template headers we expect to see for a definition
2309 or specialization of CTYPE or one of its non-template members. */
2312 num_template_headers_for_class (tree ctype
)
2314 int num_templates
= 0;
2316 while (ctype
&& CLASS_TYPE_P (ctype
))
2318 /* You're supposed to have one `template <...>' for every
2319 template class, but you don't need one for a full
2320 specialization. For example:
2322 template <class T> struct S{};
2323 template <> struct S<int> { void f(); };
2324 void S<int>::f () {}
2326 is correct; there shouldn't be a `template <>' for the
2327 definition of `S<int>::f'. */
2328 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2329 /* If CTYPE does not have template information of any
2330 kind, then it is not a template, nor is it nested
2331 within a template. */
2333 if (explicit_class_specialization_p (ctype
))
2335 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2338 ctype
= TYPE_CONTEXT (ctype
);
2341 return num_templates
;
2344 /* Do a simple sanity check on the template headers that precede the
2345 variable declaration DECL. */
2348 check_template_variable (tree decl
)
2350 tree ctx
= CP_DECL_CONTEXT (decl
);
2351 int wanted
= num_template_headers_for_class (ctx
);
2352 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2353 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2355 if (cxx_dialect
< cxx14
)
2356 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2357 "variable templates only available with "
2358 "-std=c++14 or -std=gnu++14");
2360 // Namespace-scope variable templates should have a template header.
2363 if (template_header_count
> wanted
)
2365 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2366 "too many template headers for %D (should be %d)",
2368 if (warned
&& CLASS_TYPE_P (ctx
)
2369 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2370 inform (DECL_SOURCE_LOCATION (decl
),
2371 "members of an explicitly specialized class are defined "
2372 "without a template header");
2376 /* Check to see if the function just declared, as indicated in
2377 DECLARATOR, and in DECL, is a specialization of a function
2378 template. We may also discover that the declaration is an explicit
2379 instantiation at this point.
2381 Returns DECL, or an equivalent declaration that should be used
2382 instead if all goes well. Issues an error message if something is
2383 amiss. Returns error_mark_node if the error is not easily
2386 FLAGS is a bitmask consisting of the following flags:
2388 2: The function has a definition.
2389 4: The function is a friend.
2391 The TEMPLATE_COUNT is the number of references to qualifying
2392 template classes that appeared in the name of the function. For
2395 template <class T> struct S { void f(); };
2398 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2399 classes are not counted in the TEMPLATE_COUNT, so that in
2401 template <class T> struct S {};
2402 template <> struct S<int> { void f(); }
2403 template <> void S<int>::f();
2405 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2406 invalid; there should be no template <>.)
2408 If the function is a specialization, it is marked as such via
2409 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2410 is set up correctly, and it is added to the list of specializations
2411 for that template. */
2414 check_explicit_specialization (tree declarator
,
2419 int have_def
= flags
& 2;
2420 int is_friend
= flags
& 4;
2421 int specialization
= 0;
2422 int explicit_instantiation
= 0;
2423 int member_specialization
= 0;
2424 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2425 tree dname
= DECL_NAME (decl
);
2430 if (!processing_specialization
)
2433 tsk
= tsk_excessive_parms
;
2436 tsk
= current_tmpl_spec_kind (template_count
);
2441 if (processing_specialization
&& TREE_CODE (decl
) != VAR_DECL
)
2444 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2446 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2449 /* This could be something like:
2451 template <class T> void f(T);
2452 class S { friend void f<>(int); } */
2456 /* This case handles bogus declarations like template <>
2457 template <class T> void f<int>(); */
2459 error ("template-id %qD in declaration of primary template",
2466 case tsk_invalid_member_spec
:
2467 /* The error has already been reported in
2468 check_specialization_scope. */
2469 return error_mark_node
;
2471 case tsk_invalid_expl_inst
:
2472 error ("template parameter list used in explicit instantiation");
2478 error ("definition provided for explicit instantiation");
2480 explicit_instantiation
= 1;
2483 case tsk_excessive_parms
:
2484 case tsk_insufficient_parms
:
2485 if (tsk
== tsk_excessive_parms
)
2486 error ("too many template parameter lists in declaration of %qD",
2488 else if (template_header_count
)
2489 error("too few template parameter lists in declaration of %qD", decl
);
2491 error("explicit specialization of %qD must be introduced by "
2492 "%<template <>%>", decl
);
2496 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2497 /* In cases like template<> constexpr bool v = true;
2498 We'll give an error in check_template_variable. */
2501 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2503 member_specialization
= 1;
2509 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2511 /* This case handles bogus declarations like template <>
2512 template <class T> void f<int>(); */
2514 if (!uses_template_parms (declarator
))
2515 error ("template-id %qD in declaration of primary template",
2517 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2519 /* Partial specialization of variable template. */
2520 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2524 else if (cxx_dialect
< cxx14
)
2525 error ("non-type partial specialization %qD "
2526 "is not allowed", declarator
);
2528 error ("non-class, non-variable partial specialization %qD "
2529 "is not allowed", declarator
);
2534 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2535 /* This is a specialization of a member template, without
2536 specialization the containing class. Something like:
2538 template <class T> struct S {
2539 template <class U> void f (U);
2541 template <> template <class U> void S<int>::f(U) {}
2543 That's a specialization -- but of the entire template. */
2551 if ((specialization
|| member_specialization
)
2552 /* This doesn't apply to variable templates. */
2553 && (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
2554 || TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
))
2556 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2557 for (; t
; t
= TREE_CHAIN (t
))
2558 if (TREE_PURPOSE (t
))
2560 permerror (input_location
,
2561 "default argument specified in explicit specialization");
2566 if (specialization
|| member_specialization
|| explicit_instantiation
)
2568 tree tmpl
= NULL_TREE
;
2569 tree targs
= NULL_TREE
;
2570 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2572 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2573 if (!was_template_id
)
2577 gcc_assert (identifier_p (declarator
));
2582 /* If there is no class context, the explicit instantiation
2583 must be at namespace scope. */
2584 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2586 /* Find the namespace binding, using the declaration
2588 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2590 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2592 error ("%qD is not a template function", dname
);
2593 fns
= error_mark_node
;
2597 tree fn
= OVL_CURRENT (fns
);
2598 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2599 CP_DECL_CONTEXT (fn
)))
2600 error ("%qD is not declared in %qD",
2601 decl
, current_namespace
);
2605 declarator
= lookup_template_function (fns
, NULL_TREE
);
2608 if (declarator
== error_mark_node
)
2609 return error_mark_node
;
2611 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2613 if (!explicit_instantiation
)
2614 /* A specialization in class scope. This is invalid,
2615 but the error will already have been flagged by
2616 check_specialization_scope. */
2617 return error_mark_node
;
2620 /* It's not valid to write an explicit instantiation in
2623 class C { template void f(); }
2625 This case is caught by the parser. However, on
2628 template class C { void f(); };
2630 (which is invalid) we can get here. The error will be
2637 else if (ctype
!= NULL_TREE
2638 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2640 // We'll match variable templates in start_decl.
2644 /* Find the list of functions in ctype that have the same
2645 name as the declared function. */
2646 tree name
= TREE_OPERAND (declarator
, 0);
2647 tree fns
= NULL_TREE
;
2650 if (constructor_name_p (name
, ctype
))
2652 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2654 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2655 : !CLASSTYPE_DESTRUCTORS (ctype
))
2657 /* From [temp.expl.spec]:
2659 If such an explicit specialization for the member
2660 of a class template names an implicitly-declared
2661 special member function (clause _special_), the
2662 program is ill-formed.
2664 Similar language is found in [temp.explicit]. */
2665 error ("specialization of implicitly-declared special member function");
2666 return error_mark_node
;
2669 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2672 if (!DECL_CONV_FN_P (decl
))
2674 idx
= lookup_fnfields_1 (ctype
, name
);
2676 fns
= (*CLASSTYPE_METHOD_VEC (ctype
))[idx
];
2680 vec
<tree
, va_gc
> *methods
;
2683 /* For a type-conversion operator, we cannot do a
2684 name-based lookup. We might be looking for `operator
2685 int' which will be a specialization of `operator T'.
2686 So, we find *all* the conversion operators, and then
2687 select from them. */
2690 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2692 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2693 methods
->iterate (idx
, &ovl
);
2696 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2697 /* There are no more conversion functions. */
2700 /* Glue all these conversion functions together
2701 with those we already have. */
2702 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2703 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2707 if (fns
== NULL_TREE
)
2709 error ("no member function %qD declared in %qT", name
, ctype
);
2710 return error_mark_node
;
2713 TREE_OPERAND (declarator
, 0) = fns
;
2716 /* Figure out what exactly is being specialized at this point.
2717 Note that for an explicit instantiation, even one for a
2718 member function, we cannot tell apriori whether the
2719 instantiation is for a member template, or just a member
2720 function of a template class. Even if a member template is
2721 being instantiated, the member template arguments may be
2722 elided if they can be deduced from the rest of the
2724 tmpl
= determine_specialization (declarator
, decl
,
2726 member_specialization
,
2730 if (!tmpl
|| tmpl
== error_mark_node
)
2731 /* We couldn't figure out what this declaration was
2733 return error_mark_node
;
2736 tree gen_tmpl
= most_general_template (tmpl
);
2738 if (explicit_instantiation
)
2740 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2741 is done by do_decl_instantiation later. */
2743 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2744 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2746 if (arg_depth
> parm_depth
)
2748 /* If TMPL is not the most general template (for
2749 example, if TMPL is a friend template that is
2750 injected into namespace scope), then there will
2751 be too many levels of TARGS. Remove some of them
2756 new_targs
= make_tree_vec (parm_depth
);
2757 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2758 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2759 = TREE_VEC_ELT (targs
, i
);
2763 return instantiate_template (tmpl
, targs
, tf_error
);
2766 /* If we thought that the DECL was a member function, but it
2767 turns out to be specializing a static member function,
2768 make DECL a static member function as well. */
2769 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
2770 && DECL_STATIC_FUNCTION_P (tmpl
)
2771 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2772 revert_static_member_fn (decl
);
2774 /* If this is a specialization of a member template of a
2775 template class, we want to return the TEMPLATE_DECL, not
2776 the specialization of it. */
2777 if (tsk
== tsk_template
&& !was_template_id
)
2779 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2780 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2781 DECL_INITIAL (result
) = NULL_TREE
;
2785 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2786 DECL_SOURCE_LOCATION (result
)
2787 = DECL_SOURCE_LOCATION (decl
);
2788 /* We want to use the argument list specified in the
2789 definition, not in the original declaration. */
2790 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2791 for (parm
= DECL_ARGUMENTS (result
); parm
;
2792 parm
= DECL_CHAIN (parm
))
2793 DECL_CONTEXT (parm
) = result
;
2795 return register_specialization (tmpl
, gen_tmpl
, targs
,
2799 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2800 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2802 if (was_template_id
)
2803 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
2805 /* Inherit default function arguments from the template
2806 DECL is specializing. */
2807 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2808 copy_default_args_to_explicit_spec (decl
);
2810 /* This specialization has the same protection as the
2811 template it specializes. */
2812 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2813 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2815 /* 7.1.1-1 [dcl.stc]
2817 A storage-class-specifier shall not be specified in an
2818 explicit specialization...
2820 The parser rejects these, so unless action is taken here,
2821 explicit function specializations will always appear with
2824 The action recommended by the C++ CWG in response to C++
2825 defect report 605 is to make the storage class and linkage
2826 of the explicit specialization match the templated function:
2828 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2830 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2832 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2833 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2835 /* This specialization has the same linkage and visibility as
2836 the function template it specializes. */
2837 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2838 if (! TREE_PUBLIC (decl
))
2840 DECL_INTERFACE_KNOWN (decl
) = 1;
2841 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2843 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2844 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2846 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2847 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2851 /* If DECL is a friend declaration, declared using an
2852 unqualified name, the namespace associated with DECL may
2853 have been set incorrectly. For example, in:
2855 template <typename T> void f(T);
2857 struct S { friend void f<int>(int); }
2860 we will have set the DECL_CONTEXT for the friend
2861 declaration to N, rather than to the global namespace. */
2862 if (DECL_NAMESPACE_SCOPE_P (decl
))
2863 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2865 if (is_friend
&& !have_def
)
2866 /* This is not really a declaration of a specialization.
2867 It's just the name of an instantiation. But, it's not
2868 a request for an instantiation, either. */
2869 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2870 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
2871 /* A specialization is not necessarily COMDAT. */
2872 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
2873 && DECL_DECLARED_INLINE_P (decl
));
2874 else if (TREE_CODE (decl
) == VAR_DECL
)
2875 DECL_COMDAT (decl
) = false;
2877 /* Register this specialization so that we can find it
2879 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2881 /* A 'structor should already have clones. */
2882 gcc_assert (decl
== error_mark_node
2883 || variable_template_p (tmpl
)
2884 || !(DECL_CONSTRUCTOR_P (decl
)
2885 || DECL_DESTRUCTOR_P (decl
))
2886 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
2893 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2894 parameters. These are represented in the same format used for
2895 DECL_TEMPLATE_PARMS. */
2898 comp_template_parms (const_tree parms1
, const_tree parms2
)
2903 if (parms1
== parms2
)
2906 for (p1
= parms1
, p2
= parms2
;
2907 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2908 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2910 tree t1
= TREE_VALUE (p1
);
2911 tree t2
= TREE_VALUE (p2
);
2914 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2915 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2917 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2920 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2922 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2923 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2925 /* If either of the template parameters are invalid, assume
2926 they match for the sake of error recovery. */
2927 if (error_operand_p (parm1
) || error_operand_p (parm2
))
2930 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2933 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2934 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2935 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2937 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2942 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2943 /* One set of parameters has more parameters lists than the
2950 /* Determine whether PARM is a parameter pack. */
2953 template_parameter_pack_p (const_tree parm
)
2955 /* Determine if we have a non-type template parameter pack. */
2956 if (TREE_CODE (parm
) == PARM_DECL
)
2957 return (DECL_TEMPLATE_PARM_P (parm
)
2958 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2959 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
2960 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
2962 /* If this is a list of template parameters, we could get a
2963 TYPE_DECL or a TEMPLATE_DECL. */
2964 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2965 parm
= TREE_TYPE (parm
);
2967 /* Otherwise it must be a type template parameter. */
2968 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2969 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2970 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2973 /* Determine if T is a function parameter pack. */
2976 function_parameter_pack_p (const_tree t
)
2978 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2979 return DECL_PACK_P (t
);
2983 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2984 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2987 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2989 if (! primary_func_tmpl_inst
2990 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2991 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2994 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2997 /* Return true iff the function parameter PARAM_DECL was expanded
2998 from the function parameter pack PACK. */
3001 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3003 if (DECL_ARTIFICIAL (param_decl
)
3004 || !function_parameter_pack_p (pack
))
3007 /* The parameter pack and its pack arguments have the same
3009 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3012 /* Determine whether ARGS describes a variadic template args list,
3013 i.e., one that is terminated by a template argument pack. */
3016 template_args_variadic_p (tree args
)
3021 if (args
== NULL_TREE
)
3024 args
= INNERMOST_TEMPLATE_ARGS (args
);
3025 nargs
= TREE_VEC_LENGTH (args
);
3030 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3032 return ARGUMENT_PACK_P (last_parm
);
3035 /* Generate a new name for the parameter pack name NAME (an
3036 IDENTIFIER_NODE) that incorporates its */
3039 make_ith_pack_parameter_name (tree name
, int i
)
3041 /* Munge the name to include the parameter index. */
3042 #define NUMBUF_LEN 128
3043 char numbuf
[NUMBUF_LEN
];
3047 if (name
== NULL_TREE
)
3049 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3050 newname_len
= IDENTIFIER_LENGTH (name
)
3051 + strlen (numbuf
) + 2;
3052 newname
= (char*)alloca (newname_len
);
3053 snprintf (newname
, newname_len
,
3054 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3055 return get_identifier (newname
);
3058 /* Return true if T is a primary function, class or alias template
3062 primary_template_instantiation_p (const_tree t
)
3067 if (TREE_CODE (t
) == FUNCTION_DECL
)
3068 return DECL_LANG_SPECIFIC (t
)
3069 && DECL_TEMPLATE_INSTANTIATION (t
)
3070 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
3071 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3072 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
3073 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
3074 else if (alias_template_specialization_p (t
))
3079 /* Return true if PARM is a template template parameter. */
3082 template_template_parameter_p (const_tree parm
)
3084 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3087 /* Return true iff PARM is a DECL representing a type template
3091 template_type_parameter_p (const_tree parm
)
3094 && (TREE_CODE (parm
) == TYPE_DECL
3095 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3096 && DECL_TEMPLATE_PARM_P (parm
));
3099 /* Return the template parameters of T if T is a
3100 primary template instantiation, NULL otherwise. */
3103 get_primary_template_innermost_parameters (const_tree t
)
3105 tree parms
= NULL
, template_info
= NULL
;
3107 if ((template_info
= get_template_info (t
))
3108 && primary_template_instantiation_p (t
))
3109 parms
= INNERMOST_TEMPLATE_PARMS
3110 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3115 /* Return the template parameters of the LEVELth level from the full list
3116 of template parameters PARMS. */
3119 get_template_parms_at_level (tree parms
, int level
)
3123 || TREE_CODE (parms
) != TREE_LIST
3124 || level
> TMPL_PARMS_DEPTH (parms
))
3127 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3128 if (TMPL_PARMS_DEPTH (p
) == level
)
3134 /* Returns the template arguments of T if T is a template instantiation,
3138 get_template_innermost_arguments (const_tree t
)
3140 tree args
= NULL
, template_info
= NULL
;
3142 if ((template_info
= get_template_info (t
))
3143 && TI_ARGS (template_info
))
3144 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3149 /* Return the argument pack elements of T if T is a template argument pack,
3153 get_template_argument_pack_elems (const_tree t
)
3155 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3156 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3159 return ARGUMENT_PACK_ARGS (t
);
3162 /* Structure used to track the progress of find_parameter_packs_r. */
3163 struct find_parameter_pack_data
3165 /* TREE_LIST that will contain all of the parameter packs found by
3167 tree
* parameter_packs
;
3169 /* Set of AST nodes that have been visited by the traversal. */
3170 hash_set
<tree
> *visited
;
3173 /* Identifies all of the argument packs that occur in a template
3174 argument and appends them to the TREE_LIST inside DATA, which is a
3175 find_parameter_pack_data structure. This is a subroutine of
3176 make_pack_expansion and uses_parameter_packs. */
3178 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3181 struct find_parameter_pack_data
* ppd
=
3182 (struct find_parameter_pack_data
*)data
;
3183 bool parameter_pack_p
= false;
3185 /* Handle type aliases/typedefs. */
3186 if (TYPE_ALIAS_P (t
))
3188 if (TYPE_TEMPLATE_INFO (t
))
3189 cp_walk_tree (&TYPE_TI_ARGS (t
),
3190 &find_parameter_packs_r
,
3196 /* Identify whether this is a parameter pack or not. */
3197 switch (TREE_CODE (t
))
3199 case TEMPLATE_PARM_INDEX
:
3200 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3201 parameter_pack_p
= true;
3204 case TEMPLATE_TYPE_PARM
:
3205 t
= TYPE_MAIN_VARIANT (t
);
3206 case TEMPLATE_TEMPLATE_PARM
:
3207 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3208 parameter_pack_p
= true;
3213 if (DECL_PACK_P (t
))
3215 /* We don't want to walk into the type of a PARM_DECL,
3216 because we don't want to see the type parameter pack. */
3218 parameter_pack_p
= true;
3222 /* Look through a lambda capture proxy to the field pack. */
3224 if (DECL_HAS_VALUE_EXPR_P (t
))
3226 tree v
= DECL_VALUE_EXPR (t
);
3228 &find_parameter_packs_r
,
3235 parameter_pack_p
= true;
3238 /* Not a parameter pack. */
3242 if (parameter_pack_p
)
3244 /* Add this parameter pack to the list. */
3245 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3249 cp_walk_tree (&TYPE_CONTEXT (t
),
3250 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3252 /* This switch statement will return immediately if we don't find a
3254 switch (TREE_CODE (t
))
3256 case TEMPLATE_PARM_INDEX
:
3259 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3260 /* Check the template itself. */
3261 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3262 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3263 /* Check the template arguments. */
3264 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3269 case TEMPLATE_TYPE_PARM
:
3270 case TEMPLATE_TEMPLATE_PARM
:
3277 if (TYPE_PTRMEMFUNC_P (t
))
3283 if (TYPE_TEMPLATE_INFO (t
))
3284 cp_walk_tree (&TYPE_TI_ARGS (t
),
3285 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3292 cp_walk_tree (&TREE_TYPE (t
),
3293 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3297 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3302 case TYPE_PACK_EXPANSION
:
3303 case EXPR_PACK_EXPANSION
:
3308 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3313 case IDENTIFIER_NODE
:
3314 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3326 /* Determines if the expression or type T uses any parameter packs. */
3328 uses_parameter_packs (tree t
)
3330 tree parameter_packs
= NULL_TREE
;
3331 struct find_parameter_pack_data ppd
;
3332 ppd
.parameter_packs
= ¶meter_packs
;
3333 ppd
.visited
= new hash_set
<tree
>;
3334 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3336 return parameter_packs
!= NULL_TREE
;
3339 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3340 representation a base-class initializer into a parameter pack
3341 expansion. If all goes well, the resulting node will be an
3342 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3345 make_pack_expansion (tree arg
)
3348 tree parameter_packs
= NULL_TREE
;
3349 bool for_types
= false;
3350 struct find_parameter_pack_data ppd
;
3352 if (!arg
|| arg
== error_mark_node
)
3355 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
3357 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3358 class initializer. In this case, the TREE_PURPOSE will be a
3359 _TYPE node (representing the base class expansion we're
3360 initializing) and the TREE_VALUE will be a TREE_LIST
3361 containing the initialization arguments.
3363 The resulting expansion looks somewhat different from most
3364 expansions. Rather than returning just one _EXPANSION, we
3365 return a TREE_LIST whose TREE_PURPOSE is a
3366 TYPE_PACK_EXPANSION containing the bases that will be
3367 initialized. The TREE_VALUE will be identical to the
3368 original TREE_VALUE, which is a list of arguments that will
3369 be passed to each base. We do not introduce any new pack
3370 expansion nodes into the TREE_VALUE (although it is possible
3371 that some already exist), because the TREE_PURPOSE and
3372 TREE_VALUE all need to be expanded together with the same
3373 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3374 resulting TREE_PURPOSE will mention the parameter packs in
3375 both the bases and the arguments to the bases. */
3378 tree parameter_packs
= NULL_TREE
;
3380 /* Determine which parameter packs will be used by the base
3382 ppd
.visited
= new hash_set
<tree
>;
3383 ppd
.parameter_packs
= ¶meter_packs
;
3384 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3387 if (parameter_packs
== NULL_TREE
)
3389 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3391 return error_mark_node
;
3394 if (TREE_VALUE (arg
) != void_type_node
)
3396 /* Collect the sets of parameter packs used in each of the
3397 initialization arguments. */
3398 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3400 /* Determine which parameter packs will be expanded in this
3402 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3409 /* Create the pack expansion type for the base type. */
3410 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3411 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3412 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3414 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3415 they will rarely be compared to anything. */
3416 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3418 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3421 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3424 /* Build the PACK_EXPANSION_* node. */
3426 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3427 : make_node (EXPR_PACK_EXPANSION
);
3428 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3429 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3431 /* Propagate type and const-expression information. */
3432 TREE_TYPE (result
) = TREE_TYPE (arg
);
3433 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3436 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3437 they will rarely be compared to anything. */
3438 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3440 /* Determine which parameter packs will be expanded. */
3441 ppd
.parameter_packs
= ¶meter_packs
;
3442 ppd
.visited
= new hash_set
<tree
>;
3443 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3446 /* Make sure we found some parameter packs. */
3447 if (parameter_packs
== NULL_TREE
)
3450 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3452 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3453 return error_mark_node
;
3455 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3457 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3462 /* Checks T for any "bare" parameter packs, which have not yet been
3463 expanded, and issues an error if any are found. This operation can
3464 only be done on full expressions or types (e.g., an expression
3465 statement, "if" condition, etc.), because we could have expressions like:
3467 foo(f(g(h(args)))...)
3469 where "args" is a parameter pack. check_for_bare_parameter_packs
3470 should not be called for the subexpressions args, h(args),
3471 g(h(args)), or f(g(h(args))), because we would produce erroneous
3474 Returns TRUE and emits an error if there were bare parameter packs,
3475 returns FALSE otherwise. */
3477 check_for_bare_parameter_packs (tree t
)
3479 tree parameter_packs
= NULL_TREE
;
3480 struct find_parameter_pack_data ppd
;
3482 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3485 if (TREE_CODE (t
) == TYPE_DECL
)
3488 ppd
.parameter_packs
= ¶meter_packs
;
3489 ppd
.visited
= new hash_set
<tree
>;
3490 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3493 if (parameter_packs
)
3495 error ("parameter packs not expanded with %<...%>:");
3496 while (parameter_packs
)
3498 tree pack
= TREE_VALUE (parameter_packs
);
3499 tree name
= NULL_TREE
;
3501 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3502 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3503 name
= TYPE_NAME (pack
);
3504 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3505 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3507 name
= DECL_NAME (pack
);
3510 inform (input_location
, " %qD", name
);
3512 inform (input_location
, " <anonymous>");
3514 parameter_packs
= TREE_CHAIN (parameter_packs
);
3523 /* Expand any parameter packs that occur in the template arguments in
3526 expand_template_argument_pack (tree args
)
3528 tree result_args
= NULL_TREE
;
3529 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3530 int num_result_args
= -1;
3531 int non_default_args_count
= -1;
3533 /* First, determine if we need to expand anything, and the number of
3534 slots we'll need. */
3535 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3537 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3538 if (arg
== NULL_TREE
)
3540 if (ARGUMENT_PACK_P (arg
))
3542 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3543 if (num_result_args
< 0)
3544 num_result_args
= in_arg
+ num_packed
;
3546 num_result_args
+= num_packed
;
3550 if (num_result_args
>= 0)
3555 /* If no expansion is necessary, we're done. */
3556 if (num_result_args
< 0)
3559 /* Expand arguments. */
3560 result_args
= make_tree_vec (num_result_args
);
3561 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3562 non_default_args_count
=
3563 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3564 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3566 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3567 if (ARGUMENT_PACK_P (arg
))
3569 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3570 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3571 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3572 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3573 if (non_default_args_count
> 0)
3574 non_default_args_count
+= num_packed
- 1;
3578 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3582 if (non_default_args_count
>= 0)
3583 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3587 /* Checks if DECL shadows a template parameter.
3589 [temp.local]: A template-parameter shall not be redeclared within its
3590 scope (including nested scopes).
3592 Emits an error and returns TRUE if the DECL shadows a parameter,
3593 returns FALSE otherwise. */
3596 check_template_shadow (tree decl
)
3600 /* If we're not in a template, we can't possibly shadow a template
3602 if (!current_template_parms
)
3605 /* Figure out what we're shadowing. */
3606 if (TREE_CODE (decl
) == OVERLOAD
)
3607 decl
= OVL_CURRENT (decl
);
3608 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3610 /* If there's no previous binding for this name, we're not shadowing
3611 anything, let alone a template parameter. */
3615 /* If we're not shadowing a template parameter, we're done. Note
3616 that OLDDECL might be an OVERLOAD (or perhaps even an
3617 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3619 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3622 /* We check for decl != olddecl to avoid bogus errors for using a
3623 name inside a class. We check TPFI to avoid duplicate errors for
3624 inline member templates. */
3626 || (DECL_TEMPLATE_PARM_P (decl
)
3627 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
3630 /* Don't complain about the injected class name, as we've already
3631 complained about the class itself. */
3632 if (DECL_SELF_REFERENCE_P (decl
))
3635 error ("declaration of %q+#D", decl
);
3636 error (" shadows template parm %q+#D", olddecl
);
3640 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3641 ORIG_LEVEL, DECL, and TYPE. */
3644 build_template_parm_index (int index
,
3650 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3651 TEMPLATE_PARM_IDX (t
) = index
;
3652 TEMPLATE_PARM_LEVEL (t
) = level
;
3653 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3654 TEMPLATE_PARM_DECL (t
) = decl
;
3655 TREE_TYPE (t
) = type
;
3656 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3657 TREE_READONLY (t
) = TREE_READONLY (decl
);
3662 /* Find the canonical type parameter for the given template type
3663 parameter. Returns the canonical type parameter, which may be TYPE
3664 if no such parameter existed. */
3667 canonical_type_parameter (tree type
)
3670 int idx
= TEMPLATE_TYPE_IDX (type
);
3671 if (!canonical_template_parms
)
3672 vec_alloc (canonical_template_parms
, idx
+1);
3674 while (canonical_template_parms
->length () <= (unsigned)idx
)
3675 vec_safe_push (canonical_template_parms
, NULL_TREE
);
3677 list
= (*canonical_template_parms
)[idx
];
3678 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3679 list
= TREE_CHAIN (list
);
3682 return TREE_VALUE (list
);
3685 (*canonical_template_parms
)[idx
]
3686 = tree_cons (NULL_TREE
, type
,
3687 (*canonical_template_parms
)[idx
]);
3692 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3693 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3694 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3695 new one is created. */
3698 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3699 tsubst_flags_t complain
)
3701 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3702 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3703 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3704 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3706 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3709 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3710 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3711 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3712 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3713 DECL_ARTIFICIAL (decl
) = 1;
3714 SET_DECL_TEMPLATE_PARM_P (decl
);
3716 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3717 TEMPLATE_PARM_LEVEL (index
) - levels
,
3718 TEMPLATE_PARM_ORIG_LEVEL (index
),
3720 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3721 TEMPLATE_PARM_PARAMETER_PACK (t
)
3722 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3724 /* Template template parameters need this. */
3725 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3726 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3727 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3731 return TEMPLATE_PARM_DESCENDANTS (index
);
3734 /* Process information from new template parameter PARM and append it
3735 to the LIST being built. This new parameter is a non-type
3736 parameter iff IS_NON_TYPE is true. This new parameter is a
3737 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3741 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
3742 bool is_non_type
, bool is_parameter_pack
)
3748 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3749 defval
= TREE_PURPOSE (parm
);
3753 tree p
= tree_last (list
);
3755 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3758 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3759 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3761 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3769 parm
= TREE_VALUE (parm
);
3771 SET_DECL_TEMPLATE_PARM_P (parm
);
3773 if (TREE_TYPE (parm
) != error_mark_node
)
3777 The top-level cv-qualifiers on the template-parameter are
3778 ignored when determining its type. */
3779 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3780 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3781 TREE_TYPE (parm
) = error_mark_node
;
3782 else if (uses_parameter_packs (TREE_TYPE (parm
))
3783 && !is_parameter_pack
3784 /* If we're in a nested template parameter list, the template
3785 template parameter could be a parameter pack. */
3786 && processing_template_parmlist
== 1)
3788 /* This template parameter is not a parameter pack, but it
3789 should be. Complain about "bare" parameter packs. */
3790 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3792 /* Recover by calling this a parameter pack. */
3793 is_parameter_pack
= true;
3797 /* A template parameter is not modifiable. */
3798 TREE_CONSTANT (parm
) = 1;
3799 TREE_READONLY (parm
) = 1;
3800 decl
= build_decl (parm_loc
,
3801 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3802 TREE_CONSTANT (decl
) = 1;
3803 TREE_READONLY (decl
) = 1;
3804 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3805 = build_template_parm_index (idx
, processing_template_decl
,
3806 processing_template_decl
,
3807 decl
, TREE_TYPE (parm
));
3809 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3810 = is_parameter_pack
;
3815 parm
= TREE_VALUE (TREE_VALUE (parm
));
3817 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3819 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3820 /* This is for distinguishing between real templates and template
3821 template parameters */
3822 TREE_TYPE (parm
) = t
;
3823 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3828 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3829 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3830 decl
= build_decl (parm_loc
,
3831 TYPE_DECL
, parm
, t
);
3834 TYPE_NAME (t
) = decl
;
3835 TYPE_STUB_DECL (t
) = decl
;
3837 TEMPLATE_TYPE_PARM_INDEX (t
)
3838 = build_template_parm_index (idx
, processing_template_decl
,
3839 processing_template_decl
,
3840 decl
, TREE_TYPE (parm
));
3841 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3842 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3844 DECL_ARTIFICIAL (decl
) = 1;
3845 SET_DECL_TEMPLATE_PARM_P (decl
);
3847 parm
= build_tree_list (defval
, parm
);
3848 return chainon (list
, parm
);
3851 /* The end of a template parameter list has been reached. Process the
3852 tree list into a parameter vector, converting each parameter into a more
3853 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3857 end_template_parm_list (tree parms
)
3861 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3863 current_template_parms
3864 = tree_cons (size_int (processing_template_decl
),
3865 saved_parmlist
, current_template_parms
);
3867 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3869 next
= TREE_CHAIN (parm
);
3870 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3871 TREE_CHAIN (parm
) = NULL_TREE
;
3874 --processing_template_parmlist
;
3876 return saved_parmlist
;
3879 /* end_template_decl is called after a template declaration is seen. */
3882 end_template_decl (void)
3884 reset_specialization ();
3886 if (! processing_template_decl
)
3889 /* This matches the pushlevel in begin_template_parm_list. */
3892 --processing_template_decl
;
3893 current_template_parms
= TREE_CHAIN (current_template_parms
);
3896 /* Takes a TREE_LIST representing a template parameter and convert it
3897 into an argument suitable to be passed to the type substitution
3898 functions. Note that If the TREE_LIST contains an error_mark
3899 node, the returned argument is error_mark_node. */
3902 template_parm_to_arg (tree t
)
3906 || TREE_CODE (t
) != TREE_LIST
)
3909 if (error_operand_p (TREE_VALUE (t
)))
3910 return error_mark_node
;
3914 if (TREE_CODE (t
) == TYPE_DECL
3915 || TREE_CODE (t
) == TEMPLATE_DECL
)
3919 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3921 /* Turn this argument into a TYPE_ARGUMENT_PACK
3922 with a single element, which expands T. */
3923 tree vec
= make_tree_vec (1);
3924 #ifdef ENABLE_CHECKING
3925 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3926 (vec
, TREE_VEC_LENGTH (vec
));
3928 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3930 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
3931 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3936 t
= DECL_INITIAL (t
);
3938 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3940 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3941 with a single element, which expands T. */
3942 tree vec
= make_tree_vec (1);
3943 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3944 #ifdef ENABLE_CHECKING
3945 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3946 (vec
, TREE_VEC_LENGTH (vec
));
3948 t
= convert_from_reference (t
);
3949 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3951 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3952 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3953 TREE_TYPE (t
) = type
;
3956 t
= convert_from_reference (t
);
3961 /* Given a set of template parameters, return them as a set of template
3962 arguments. The template parameters are represented as a TREE_VEC, in
3963 the form documented in cp-tree.h for template arguments. */
3966 template_parms_to_args (tree parms
)
3969 tree args
= NULL_TREE
;
3970 int length
= TMPL_PARMS_DEPTH (parms
);
3973 /* If there is only one level of template parameters, we do not
3974 create a TREE_VEC of TREE_VECs. Instead, we return a single
3975 TREE_VEC containing the arguments. */
3977 args
= make_tree_vec (length
);
3979 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
3981 tree a
= copy_node (TREE_VALUE (header
));
3984 TREE_TYPE (a
) = NULL_TREE
;
3985 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
3986 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
3988 #ifdef ENABLE_CHECKING
3989 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
3993 TREE_VEC_ELT (args
, --l
) = a
;
3998 if (length
> 1 && TREE_VEC_ELT (args
, 0) == NULL_TREE
)
3999 /* This can happen for template parms of a template template
4002 template<template<class T, class U> class TT> struct S;
4004 Consider the level of the parms of TT; T and U both have
4005 level 2; TT has no template parm of level 1. So in this case
4006 the first element of full_template_args is NULL_TREE. If we
4007 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
4008 of 2. This will make tsubst wrongly consider that T and U
4009 have level 1. Instead, let's create a dummy vector as the
4010 first element of full_template_args so that TMPL_ARGS_DEPTH
4011 returns the correct depth for args. */
4012 TREE_VEC_ELT (args
, 0) = make_tree_vec (1);
4016 /* Within the declaration of a template, return the currently active
4017 template parameters as an argument TREE_VEC. */
4020 current_template_args (void)
4022 return template_parms_to_args (current_template_parms
);
4025 /* Update the declared TYPE by doing any lookups which were thought to be
4026 dependent, but are not now that we know the SCOPE of the declarator. */
4029 maybe_update_decl_type (tree orig_type
, tree scope
)
4031 tree type
= orig_type
;
4033 if (type
== NULL_TREE
)
4036 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4037 type
= TREE_TYPE (type
);
4039 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4040 && dependent_type_p (type
)
4041 /* Don't bother building up the args in this case. */
4042 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4044 /* tsubst in the args corresponding to the template parameters,
4045 including auto if present. Most things will be unchanged, but
4046 make_typename_type and tsubst_qualified_id will resolve
4047 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4048 tree args
= current_template_args ();
4049 tree auto_node
= type_uses_auto (type
);
4053 tree auto_vec
= make_tree_vec (1);
4054 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4055 args
= add_to_template_args (args
, auto_vec
);
4057 pushed
= push_scope (scope
);
4058 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4063 if (type
== error_mark_node
)
4066 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4068 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4071 type
= TYPE_NAME (type
);
4076 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4077 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
4078 a member template. Used by push_template_decl below. */
4081 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4083 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4084 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4085 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4086 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4087 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4092 struct template_parm_data
4094 /* The level of the template parameters we are currently
4098 /* The index of the specialization argument we are currently
4102 /* An array whose size is the number of template parameters. The
4103 elements are nonzero if the parameter has been used in any one
4104 of the arguments processed so far. */
4107 /* An array whose size is the number of template arguments. The
4108 elements are nonzero if the argument makes use of template
4109 parameters of this level. */
4110 int* arg_uses_template_parms
;
4113 /* Subroutine of push_template_decl used to see if each template
4114 parameter in a partial specialization is used in the explicit
4115 argument list. If T is of the LEVEL given in DATA (which is
4116 treated as a template_parm_data*), then DATA->PARMS is marked
4120 mark_template_parm (tree t
, void* data
)
4124 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4126 template_parm_level_and_index (t
, &level
, &idx
);
4128 if (level
== tpd
->level
)
4130 tpd
->parms
[idx
] = 1;
4131 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4134 /* Return zero so that for_each_template_parm will continue the
4135 traversal of the tree; we want to mark *every* template parm. */
4139 /* Process the partial specialization DECL. */
4142 process_partial_specialization (tree decl
)
4144 tree type
= TREE_TYPE (decl
);
4145 tree tinfo
= get_template_info (decl
);
4146 tree maintmpl
= TI_TEMPLATE (tinfo
);
4147 tree specargs
= TI_ARGS (tinfo
);
4148 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4149 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4152 int nargs
= TREE_VEC_LENGTH (inner_args
);
4155 bool did_error_intro
= false;
4156 struct template_parm_data tpd
;
4157 struct template_parm_data tpd2
;
4159 gcc_assert (current_template_parms
);
4161 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4162 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4164 /* We check that each of the template parameters given in the
4165 partial specialization is used in the argument list to the
4166 specialization. For example:
4168 template <class T> struct S;
4169 template <class T> struct S<T*>;
4171 The second declaration is OK because `T*' uses the template
4172 parameter T, whereas
4174 template <class T> struct S<int>;
4176 is no good. Even trickier is:
4187 The S2<T> declaration is actually invalid; it is a
4188 full-specialization. Of course,
4191 struct S2<T (*)(U)>;
4193 or some such would have been OK. */
4194 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4195 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4196 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4198 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4199 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4200 for (i
= 0; i
< nargs
; ++i
)
4202 tpd
.current_arg
= i
;
4203 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4204 &mark_template_parm
,
4207 /*include_nondeduced_p=*/false);
4209 for (i
= 0; i
< ntparms
; ++i
)
4210 if (tpd
.parms
[i
] == 0)
4212 /* One of the template parms was not used in a deduced context in the
4214 if (!did_error_intro
)
4216 error ("template parameters not deducible in "
4217 "partial specialization:");
4218 did_error_intro
= true;
4221 inform (input_location
, " %qD",
4222 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4225 if (did_error_intro
)
4226 return error_mark_node
;
4228 /* [temp.class.spec]
4230 The argument list of the specialization shall not be identical to
4231 the implicit argument list of the primary template. */
4233 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
4234 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
)))
4235 error ("partial specialization %qD does not specialize "
4236 "any template arguments", decl
);
4238 /* A partial specialization that replaces multiple parameters of the
4239 primary template with a pack expansion is less specialized for those
4241 if (nargs
< DECL_NTPARMS (maintmpl
))
4243 error ("partial specialization is not more specialized than the "
4244 "primary template because it replaces multiple parameters "
4245 "with a pack expansion");
4246 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4250 /* [temp.class.spec]
4252 A partially specialized non-type argument expression shall not
4253 involve template parameters of the partial specialization except
4254 when the argument expression is a simple identifier.
4256 The type of a template parameter corresponding to a specialized
4257 non-type argument shall not be dependent on a parameter of the
4260 Also, we verify that pack expansions only occur at the
4261 end of the argument list. */
4262 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4264 for (i
= 0; i
< nargs
; ++i
)
4266 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4267 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4268 tree packed_args
= NULL_TREE
;
4271 if (ARGUMENT_PACK_P (arg
))
4273 /* Extract the arguments from the argument pack. We'll be
4274 iterating over these in the following loop. */
4275 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4276 len
= TREE_VEC_LENGTH (packed_args
);
4279 for (j
= 0; j
< len
; j
++)
4282 /* Get the Jth argument in the parameter pack. */
4283 arg
= TREE_VEC_ELT (packed_args
, j
);
4285 if (PACK_EXPANSION_P (arg
))
4287 /* Pack expansions must come at the end of the
4289 if ((packed_args
&& j
< len
- 1)
4290 || (!packed_args
&& i
< nargs
- 1))
4292 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4293 error ("parameter pack argument %qE must be at the "
4294 "end of the template argument list", arg
);
4296 error ("parameter pack argument %qT must be at the "
4297 "end of the template argument list", arg
);
4301 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4302 /* We only care about the pattern. */
4303 arg
= PACK_EXPANSION_PATTERN (arg
);
4305 if (/* These first two lines are the `non-type' bit. */
4307 && TREE_CODE (arg
) != TEMPLATE_DECL
4308 /* This next two lines are the `argument expression is not just a
4309 simple identifier' condition and also the `specialized
4310 non-type argument' bit. */
4311 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4312 && !(REFERENCE_REF_P (arg
)
4313 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4315 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4316 || (packed_args
&& uses_template_parms (arg
)))
4317 error ("template argument %qE involves template parameter(s)",
4321 /* Look at the corresponding template parameter,
4322 marking which template parameters its type depends
4324 tree type
= TREE_TYPE (parm
);
4328 /* We haven't yet initialized TPD2. Do so now. */
4329 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4330 /* The number of parameters here is the number in the
4331 main template, which, as checked in the assertion
4333 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4335 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4338 /* Mark the template parameters. But this time, we're
4339 looking for the template parameters of the main
4340 template, not in the specialization. */
4341 tpd2
.current_arg
= i
;
4342 tpd2
.arg_uses_template_parms
[i
] = 0;
4343 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4344 for_each_template_parm (type
,
4345 &mark_template_parm
,
4348 /*include_nondeduced_p=*/false);
4350 if (tpd2
.arg_uses_template_parms
[i
])
4352 /* The type depended on some template parameters.
4353 If they are fully specialized in the
4354 specialization, that's OK. */
4357 for (j
= 0; j
< nargs
; ++j
)
4358 if (tpd2
.parms
[j
] != 0
4359 && tpd
.arg_uses_template_parms
[j
])
4362 error_n (input_location
, count
,
4363 "type %qT of template argument %qE depends "
4364 "on a template parameter",
4365 "type %qT of template argument %qE depends "
4366 "on template parameters",
4375 /* We should only get here once. */
4376 if (TREE_CODE (decl
) == TYPE_DECL
)
4377 gcc_assert (!COMPLETE_TYPE_P (type
));
4379 tree tmpl
= build_template_decl (decl
, current_template_parms
,
4380 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4381 TREE_TYPE (tmpl
) = type
;
4382 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4383 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4384 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4385 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4387 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4388 = tree_cons (specargs
, tmpl
,
4389 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4390 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4392 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4393 inst
= TREE_CHAIN (inst
))
4395 tree instance
= TREE_VALUE (inst
);
4396 if (TYPE_P (instance
)
4397 ? (COMPLETE_TYPE_P (instance
)
4398 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
4399 : DECL_TEMPLATE_INSTANTIATION (instance
))
4401 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
4402 if (spec
&& TREE_VALUE (spec
) == tmpl
)
4404 tree inst_decl
= (DECL_P (instance
)
4405 ? instance
: TYPE_NAME (instance
));
4406 permerror (input_location
,
4407 "partial specialization of %qD after instantiation "
4408 "of %qD", decl
, inst_decl
);
4416 /* PARM is a template parameter of some form; return the corresponding
4417 TEMPLATE_PARM_INDEX. */
4420 get_template_parm_index (tree parm
)
4422 if (TREE_CODE (parm
) == PARM_DECL
4423 || TREE_CODE (parm
) == CONST_DECL
)
4424 parm
= DECL_INITIAL (parm
);
4425 else if (TREE_CODE (parm
) == TYPE_DECL
4426 || TREE_CODE (parm
) == TEMPLATE_DECL
)
4427 parm
= TREE_TYPE (parm
);
4428 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
4429 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
4430 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
4431 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
4435 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4436 parameter packs used by the template parameter PARM. */
4439 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
4441 /* A type parm can't refer to another parm. */
4442 if (TREE_CODE (parm
) == TYPE_DECL
)
4444 else if (TREE_CODE (parm
) == PARM_DECL
)
4446 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
4451 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
4453 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
4454 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
4455 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
4458 /* PARM is a template parameter pack. Return any parameter packs used in
4459 its type or the type of any of its template parameters. If there are
4460 any such packs, it will be instantiated into a fixed template parameter
4461 list by partial instantiation rather than be fully deduced. */
4464 fixed_parameter_pack_p (tree parm
)
4466 /* This can only be true in a member template. */
4467 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
4469 /* This can only be true for a parameter pack. */
4470 if (!template_parameter_pack_p (parm
))
4472 /* A type parm can't refer to another parm. */
4473 if (TREE_CODE (parm
) == TYPE_DECL
)
4476 tree parameter_packs
= NULL_TREE
;
4477 struct find_parameter_pack_data ppd
;
4478 ppd
.parameter_packs
= ¶meter_packs
;
4479 ppd
.visited
= new hash_set
<tree
>;
4481 fixed_parameter_pack_p_1 (parm
, &ppd
);
4484 return parameter_packs
;
4487 /* Check that a template declaration's use of default arguments and
4488 parameter packs is not invalid. Here, PARMS are the template
4489 parameters. IS_PRIMARY is true if DECL is the thing declared by
4490 a primary template. IS_PARTIAL is true if DECL is a partial
4493 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4494 declaration (but not a definition); 1 indicates a declaration, 2
4495 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4496 emitted for extraneous default arguments.
4498 Returns TRUE if there were no errors found, FALSE otherwise. */
4501 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4502 bool is_partial
, int is_friend_decl
)
4505 int last_level_to_check
;
4507 bool no_errors
= true;
4511 A default template-argument shall not be specified in a
4512 function template declaration or a function template definition, nor
4513 in the template-parameter-list of the definition of a member of a
4516 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
4517 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
4518 /* You can't have a function template declaration in a local
4519 scope, nor you can you define a member of a class template in a
4523 if ((TREE_CODE (decl
) == TYPE_DECL
4525 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4526 || (TREE_CODE (decl
) == FUNCTION_DECL
4527 && LAMBDA_FUNCTION_P (decl
)))
4528 /* A lambda doesn't have an explicit declaration; don't complain
4529 about the parms of the enclosing class. */
4532 if (current_class_type
4533 && !TYPE_BEING_DEFINED (current_class_type
)
4534 && DECL_LANG_SPECIFIC (decl
)
4535 && DECL_DECLARES_FUNCTION_P (decl
)
4536 /* If this is either a friend defined in the scope of the class
4537 or a member function. */
4538 && (DECL_FUNCTION_MEMBER_P (decl
)
4539 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4540 : DECL_FRIEND_CONTEXT (decl
)
4541 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4543 /* And, if it was a member function, it really was defined in
4544 the scope of the class. */
4545 && (!DECL_FUNCTION_MEMBER_P (decl
)
4546 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4547 /* We already checked these parameters when the template was
4548 declared, so there's no need to do it again now. This function
4549 was defined in class scope, but we're processing its body now
4550 that the class is complete. */
4553 /* Core issue 226 (C++0x only): the following only applies to class
4556 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
4560 If a template-parameter has a default template-argument, all
4561 subsequent template-parameters shall have a default
4562 template-argument supplied. */
4563 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4565 tree inner_parms
= TREE_VALUE (parm_level
);
4566 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4567 int seen_def_arg_p
= 0;
4570 for (i
= 0; i
< ntparms
; ++i
)
4572 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4574 if (parm
== error_mark_node
)
4577 if (TREE_PURPOSE (parm
))
4579 else if (seen_def_arg_p
4580 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4582 error ("no default argument for %qD", TREE_VALUE (parm
));
4583 /* For better subsequent error-recovery, we indicate that
4584 there should have been a default argument. */
4585 TREE_PURPOSE (parm
) = error_mark_node
;
4588 else if (!is_partial
4590 /* Don't complain about an enclosing partial
4592 && parm_level
== parms
4593 && TREE_CODE (decl
) == TYPE_DECL
4595 && template_parameter_pack_p (TREE_VALUE (parm
))
4596 /* A fixed parameter pack will be partially
4597 instantiated into a fixed length list. */
4598 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
4600 /* A primary class template can only have one
4601 parameter pack, at the end of the template
4604 error ("parameter pack %q+D must be at the end of the"
4605 " template parameter list", TREE_VALUE (parm
));
4607 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4615 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4619 /* For an ordinary class template, default template arguments are
4620 allowed at the innermost level, e.g.:
4621 template <class T = int>
4623 but, in a partial specialization, they're not allowed even
4624 there, as we have in [temp.class.spec]:
4626 The template parameter list of a specialization shall not
4627 contain default template argument values.
4629 So, for a partial specialization, or for a function template
4630 (in C++98/C++03), we look at all of them. */
4633 /* But, for a primary class template that is not a partial
4634 specialization we look at all template parameters except the
4636 parms
= TREE_CHAIN (parms
);
4638 /* Figure out what error message to issue. */
4639 if (is_friend_decl
== 2)
4640 msg
= G_("default template arguments may not be used in function template "
4641 "friend re-declaration");
4642 else if (is_friend_decl
)
4643 msg
= G_("default template arguments may not be used in function template "
4644 "friend declarations");
4645 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4646 msg
= G_("default template arguments may not be used in function templates "
4647 "without -std=c++11 or -std=gnu++11");
4648 else if (is_partial
)
4649 msg
= G_("default template arguments may not be used in "
4650 "partial specializations");
4652 msg
= G_("default argument for template parameter for class enclosing %qD");
4654 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4655 /* If we're inside a class definition, there's no need to
4656 examine the parameters to the class itself. On the one
4657 hand, they will be checked when the class is defined, and,
4658 on the other, default arguments are valid in things like:
4659 template <class T = double>
4660 struct S { template <class U> void f(U); };
4661 Here the default argument for `S' has no bearing on the
4662 declaration of `f'. */
4663 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4665 /* Check everything. */
4666 last_level_to_check
= 0;
4668 for (parm_level
= parms
;
4669 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4670 parm_level
= TREE_CHAIN (parm_level
))
4672 tree inner_parms
= TREE_VALUE (parm_level
);
4676 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4677 for (i
= 0; i
< ntparms
; ++i
)
4679 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4682 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4687 if (is_friend_decl
== 2)
4694 /* Clear out the default argument so that we are not
4696 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4700 /* At this point, if we're still interested in issuing messages,
4701 they must apply to classes surrounding the object declared. */
4703 msg
= G_("default argument for template parameter for class "
4710 /* Worker for push_template_decl_real, called via
4711 for_each_template_parm. DATA is really an int, indicating the
4712 level of the parameters we are interested in. If T is a template
4713 parameter of that level, return nonzero. */
4716 template_parm_this_level_p (tree t
, void* data
)
4718 int this_level
= *(int *)data
;
4721 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4722 level
= TEMPLATE_PARM_LEVEL (t
);
4724 level
= TEMPLATE_TYPE_LEVEL (t
);
4725 return level
== this_level
;
4728 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4729 parameters given by current_template_args, or reuses a
4730 previously existing one, if appropriate. Returns the DECL, or an
4731 equivalent one, if it is replaced via a call to duplicate_decls.
4733 If IS_FRIEND is true, DECL is a friend declaration. */
4736 push_template_decl_real (tree decl
, bool is_friend
)
4744 int new_template_p
= 0;
4745 /* True if the template is a member template, in the sense of
4747 bool member_template_p
= false;
4749 if (decl
== error_mark_node
|| !current_template_parms
)
4750 return error_mark_node
;
4752 /* See if this is a partial specialization. */
4753 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
4754 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4755 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
4756 || (TREE_CODE (decl
) == VAR_DECL
4757 && DECL_LANG_SPECIFIC (decl
)
4758 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4759 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
4761 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4765 /* For a friend, we want the context of the friend function, not
4766 the type of which it is a friend. */
4767 ctx
= CP_DECL_CONTEXT (decl
);
4768 else if (CP_DECL_CONTEXT (decl
)
4769 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4770 /* In the case of a virtual function, we want the class in which
4772 ctx
= CP_DECL_CONTEXT (decl
);
4774 /* Otherwise, if we're currently defining some class, the DECL
4775 is assumed to be a member of the class. */
4776 ctx
= current_scope ();
4778 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4781 if (!DECL_CONTEXT (decl
))
4782 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4784 /* See if this is a primary template. */
4785 if (is_friend
&& ctx
4786 && uses_template_parms_level (ctx
, processing_template_decl
))
4787 /* A friend template that specifies a class context, i.e.
4788 template <typename T> friend void A<T>::f();
4791 else if (TREE_CODE (decl
) == TYPE_DECL
4792 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4795 is_primary
= template_parm_scope_p ();
4799 if (DECL_CLASS_SCOPE_P (decl
))
4800 member_template_p
= true;
4801 if (TREE_CODE (decl
) == TYPE_DECL
4802 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4804 error ("template class without a name");
4805 return error_mark_node
;
4807 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4809 if (member_template_p
)
4811 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
4812 error ("member template %qD may not have virt-specifiers", decl
);
4814 if (DECL_DESTRUCTOR_P (decl
))
4818 A destructor shall not be a member template. */
4819 error ("destructor %qD declared as member template", decl
);
4820 return error_mark_node
;
4822 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4823 && (!prototype_p (TREE_TYPE (decl
))
4824 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4825 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4826 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4827 == void_list_node
)))
4829 /* [basic.stc.dynamic.allocation]
4831 An allocation function can be a function
4832 template. ... Template allocation functions shall
4833 have two or more parameters. */
4834 error ("invalid template declaration of %qD", decl
);
4835 return error_mark_node
;
4838 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4839 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4841 else if (TREE_CODE (decl
) == TYPE_DECL
4842 && TYPE_DECL_ALIAS_P (decl
))
4843 /* alias-declaration */
4844 gcc_assert (!DECL_ARTIFICIAL (decl
));
4845 else if (VAR_P (decl
))
4846 /* C++14 variable template. */;
4849 error ("template declaration of %q#D", decl
);
4850 return error_mark_node
;
4854 /* Check to see that the rules regarding the use of default
4855 arguments are not being violated. */
4856 check_default_tmpl_args (decl
, current_template_parms
,
4857 is_primary
, is_partial
, /*is_friend_decl=*/0);
4859 /* Ensure that there are no parameter packs in the type of this
4860 declaration that have not been expanded. */
4861 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4863 /* Check each of the arguments individually to see if there are
4864 any bare parameter packs. */
4865 tree type
= TREE_TYPE (decl
);
4866 tree arg
= DECL_ARGUMENTS (decl
);
4867 tree argtype
= TYPE_ARG_TYPES (type
);
4869 while (arg
&& argtype
)
4871 if (!DECL_PACK_P (arg
)
4872 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4874 /* This is a PARM_DECL that contains unexpanded parameter
4875 packs. We have already complained about this in the
4876 check_for_bare_parameter_packs call, so just replace
4877 these types with ERROR_MARK_NODE. */
4878 TREE_TYPE (arg
) = error_mark_node
;
4879 TREE_VALUE (argtype
) = error_mark_node
;
4882 arg
= DECL_CHAIN (arg
);
4883 argtype
= TREE_CHAIN (argtype
);
4886 /* Check for bare parameter packs in the return type and the
4887 exception specifiers. */
4888 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4889 /* Errors were already issued, set return type to int
4890 as the frontend doesn't expect error_mark_node as
4892 TREE_TYPE (type
) = integer_type_node
;
4893 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4894 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4896 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
4897 && TYPE_DECL_ALIAS_P (decl
))
4898 ? DECL_ORIGINAL_TYPE (decl
)
4899 : TREE_TYPE (decl
)))
4901 TREE_TYPE (decl
) = error_mark_node
;
4902 return error_mark_node
;
4906 return process_partial_specialization (decl
);
4908 args
= current_template_args ();
4911 || TREE_CODE (ctx
) == FUNCTION_DECL
4912 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4913 || (TREE_CODE (decl
) == TYPE_DECL
4914 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4915 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4917 if (DECL_LANG_SPECIFIC (decl
)
4918 && DECL_TEMPLATE_INFO (decl
)
4919 && DECL_TI_TEMPLATE (decl
))
4920 tmpl
= DECL_TI_TEMPLATE (decl
);
4921 /* If DECL is a TYPE_DECL for a class-template, then there won't
4922 be DECL_LANG_SPECIFIC. The information equivalent to
4923 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4924 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4925 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4926 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4928 /* Since a template declaration already existed for this
4929 class-type, we must be redeclaring it here. Make sure
4930 that the redeclaration is valid. */
4931 redeclare_class_template (TREE_TYPE (decl
),
4932 current_template_parms
);
4933 /* We don't need to create a new TEMPLATE_DECL; just use the
4934 one we already had. */
4935 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4939 tmpl
= build_template_decl (decl
, current_template_parms
,
4943 if (DECL_LANG_SPECIFIC (decl
)
4944 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4946 /* A specialization of a member template of a template
4948 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4949 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4950 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4956 tree a
, t
, current
, parms
;
4958 tree tinfo
= get_template_info (decl
);
4962 error ("template definition of non-template %q#D", decl
);
4963 return error_mark_node
;
4966 tmpl
= TI_TEMPLATE (tinfo
);
4968 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4969 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4970 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4971 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4975 /* The declaration is a specialization of a member
4976 template, declared outside the class. Therefore, the
4977 innermost template arguments will be NULL, so we
4978 replace them with the arguments determined by the
4979 earlier call to check_explicit_specialization. */
4980 args
= DECL_TI_ARGS (decl
);
4983 = build_template_decl (decl
, current_template_parms
,
4985 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4986 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4987 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4988 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4989 DECL_TEMPLATE_INFO (new_tmpl
)
4990 = build_template_info (tmpl
, args
);
4992 register_specialization (new_tmpl
,
4993 most_general_template (tmpl
),
4999 /* Make sure the template headers we got make sense. */
5001 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5002 i
= TMPL_PARMS_DEPTH (parms
);
5003 if (TMPL_ARGS_DEPTH (args
) != i
)
5005 error ("expected %d levels of template parms for %q#D, got %d",
5006 i
, decl
, TMPL_ARGS_DEPTH (args
));
5007 DECL_INTERFACE_KNOWN (decl
) = 1;
5008 return error_mark_node
;
5011 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5013 a
= TMPL_ARGS_LEVEL (args
, i
);
5014 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5016 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5018 if (current
== decl
)
5019 error ("got %d template parameters for %q#D",
5020 TREE_VEC_LENGTH (a
), decl
);
5022 error ("got %d template parameters for %q#T",
5023 TREE_VEC_LENGTH (a
), current
);
5024 error (" but %d required", TREE_VEC_LENGTH (t
));
5025 /* Avoid crash in import_export_decl. */
5026 DECL_INTERFACE_KNOWN (decl
) = 1;
5027 return error_mark_node
;
5030 if (current
== decl
)
5032 else if (current
== NULL_TREE
)
5033 /* Can happen in erroneous input. */
5036 current
= get_containing_scope (current
);
5039 /* Check that the parms are used in the appropriate qualifying scopes
5040 in the declarator. */
5041 if (!comp_template_args
5043 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5046 template arguments to %qD do not match original template %qD",
5047 decl
, DECL_TEMPLATE_RESULT (tmpl
));
5048 if (!uses_template_parms (TI_ARGS (tinfo
)))
5049 inform (input_location
, "use template<> for an explicit specialization");
5050 /* Avoid crash in import_export_decl. */
5051 DECL_INTERFACE_KNOWN (decl
) = 1;
5052 return error_mark_node
;
5056 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5057 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5059 /* Push template declarations for global functions and types. Note
5060 that we do not try to push a global template friend declared in a
5061 template class; such a thing may well depend on the template
5062 parameters of the class. */
5063 if (new_template_p
&& !ctx
5064 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5066 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5067 if (tmpl
== error_mark_node
)
5068 return error_mark_node
;
5070 /* Hide template friend classes that haven't been declared yet. */
5071 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5073 DECL_ANTICIPATED (tmpl
) = 1;
5074 DECL_FRIEND_P (tmpl
) = 1;
5080 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5083 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5084 if (DECL_CONV_FN_P (tmpl
))
5086 int depth
= TMPL_PARMS_DEPTH (parms
);
5088 /* It is a conversion operator. See if the type converted to
5089 depends on innermost template operands. */
5091 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
5093 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
5096 /* Give template template parms a DECL_CONTEXT of the template
5097 for which they are a parameter. */
5098 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5099 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5101 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5102 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5103 DECL_CONTEXT (parm
) = tmpl
;
5107 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5108 back to its most general template. If TMPL is a specialization,
5109 ARGS may only have the innermost set of arguments. Add the missing
5110 argument levels if necessary. */
5111 if (DECL_TEMPLATE_INFO (tmpl
))
5112 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5114 info
= build_template_info (tmpl
, args
);
5116 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5117 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5120 if (is_primary
&& !DECL_LANG_SPECIFIC (decl
))
5121 retrofit_lang_decl (decl
);
5122 if (DECL_LANG_SPECIFIC (decl
))
5123 DECL_TEMPLATE_INFO (decl
) = info
;
5126 if (flag_implicit_templates
5128 && TREE_PUBLIC (decl
)
5129 && VAR_OR_FUNCTION_DECL_P (decl
))
5130 /* Set DECL_COMDAT on template instantiations; if we force
5131 them to be emitted by explicit instantiation or -frepo,
5132 mark_needed will tell cgraph to do the right thing. */
5133 DECL_COMDAT (decl
) = true;
5135 return DECL_TEMPLATE_RESULT (tmpl
);
5139 push_template_decl (tree decl
)
5141 return push_template_decl_real (decl
, false);
5144 /* FN is an inheriting constructor that inherits from the constructor
5145 template INHERITED; turn FN into a constructor template with a matching
5149 add_inherited_template_parms (tree fn
, tree inherited
)
5152 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5153 inner_parms
= copy_node (inner_parms
);
5155 = tree_cons (size_int (processing_template_decl
+ 1),
5156 inner_parms
, current_template_parms
);
5157 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5158 tree args
= template_parms_to_args (parms
);
5159 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5160 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5161 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5162 DECL_ARTIFICIAL (tmpl
) = true;
5163 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5167 /* Called when a class template TYPE is redeclared with the indicated
5168 template PARMS, e.g.:
5170 template <class T> struct S;
5171 template <class T> struct S {}; */
5174 redeclare_class_template (tree type
, tree parms
)
5180 if (!TYPE_TEMPLATE_INFO (type
))
5182 error ("%qT is not a template type", type
);
5186 tmpl
= TYPE_TI_TEMPLATE (type
);
5187 if (!PRIMARY_TEMPLATE_P (tmpl
))
5188 /* The type is nested in some template class. Nothing to worry
5189 about here; there are no new template parameters for the nested
5195 error ("template specifiers not specified in declaration of %qD",
5200 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5201 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5203 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5205 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5206 "redeclared with %d template parameter",
5207 "redeclared with %d template parameters",
5208 TREE_VEC_LENGTH (parms
));
5209 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
5210 "previous declaration %q+D used %d template parameter",
5211 "previous declaration %q+D used %d template parameters",
5212 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5216 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5223 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5224 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5227 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5228 if (error_operand_p (tmpl_parm
))
5231 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5232 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5233 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5235 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5237 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5238 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5239 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5240 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5241 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5242 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5243 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5244 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5245 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5247 error ("template parameter %q+#D", tmpl_parm
);
5248 error ("redeclared here as %q#D", parm
);
5252 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5254 /* We have in [temp.param]:
5256 A template-parameter may not be given default arguments
5257 by two different declarations in the same scope. */
5258 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5259 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5260 "original definition appeared here");
5264 if (parm_default
!= NULL_TREE
)
5265 /* Update the previous template parameters (which are the ones
5266 that will really count) with the new default value. */
5267 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5268 else if (tmpl_default
!= NULL_TREE
)
5269 /* Update the new parameters, too; they'll be used as the
5270 parameters for any members. */
5271 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5277 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5278 to be used when the caller has already checked
5279 (processing_template_decl
5280 && !instantiation_dependent_expression_p (expr)
5281 && potential_constant_expression (expr))
5282 and cleared processing_template_decl. */
5285 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
5287 return tsubst_copy_and_build (expr
,
5290 /*in_decl=*/NULL_TREE
,
5291 /*function_p=*/false,
5292 /*integral_constant_expression_p=*/true);
5295 /* Simplify EXPR if it is a non-dependent expression. Returns the
5296 (possibly simplified) expression. */
5299 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5301 if (expr
== NULL_TREE
)
5304 /* If we're in a template, but EXPR isn't value dependent, simplify
5305 it. We're supposed to treat:
5307 template <typename T> void f(T[1 + 1]);
5308 template <typename T> void f(T[2]);
5310 as two declarations of the same function, for example. */
5311 if (processing_template_decl
5312 && !instantiation_dependent_expression_p (expr
)
5313 && potential_constant_expression (expr
))
5315 processing_template_decl_sentinel s
;
5316 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
5322 instantiate_non_dependent_expr (tree expr
)
5324 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
5327 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5328 template declaration, or a TYPE_DECL for an alias declaration. */
5331 alias_type_or_template_p (tree t
)
5335 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5338 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5339 || DECL_ALIAS_TEMPLATE_P (t
));
5342 /* Return TRUE iff T is a specialization of an alias template. */
5345 alias_template_specialization_p (const_tree t
)
5347 /* It's an alias template specialization if it's an alias and its
5348 TYPE_NAME is a specialization of a primary template. */
5349 if (TYPE_ALIAS_P (t
))
5351 tree name
= TYPE_NAME (t
);
5352 if (DECL_LANG_SPECIFIC (name
))
5353 if (tree ti
= DECL_TEMPLATE_INFO (name
))
5355 tree tmpl
= TI_TEMPLATE (ti
);
5356 return PRIMARY_TEMPLATE_P (tmpl
);
5362 /* Return TRUE iff T is a specialization of an alias template with
5363 dependent template-arguments. */
5366 dependent_alias_template_spec_p (const_tree t
)
5368 return (alias_template_specialization_p (t
)
5369 && (any_dependent_template_arguments_p
5370 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (t
)))));
5373 /* Return the number of innermost template parameters in TMPL. */
5376 num_innermost_template_parms (tree tmpl
)
5378 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
5379 return TREE_VEC_LENGTH (parms
);
5382 /* Return either TMPL or another template that it is equivalent to under DR
5383 1286: An alias that just changes the name of a template is equivalent to
5384 the other template. */
5387 get_underlying_template (tree tmpl
)
5389 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
5390 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
5392 tree result
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5393 if (TYPE_TEMPLATE_INFO (result
))
5395 tree sub
= TYPE_TI_TEMPLATE (result
);
5396 if (PRIMARY_TEMPLATE_P (sub
)
5397 && (num_innermost_template_parms (tmpl
)
5398 == num_innermost_template_parms (sub
)))
5400 tree alias_args
= INNERMOST_TEMPLATE_ARGS
5401 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
5402 if (!comp_template_args (TYPE_TI_ARGS (result
), alias_args
))
5404 /* The alias type is equivalent to the pattern of the
5405 underlying template, so strip the alias. */
5415 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5416 must be a function or a pointer-to-function type, as specified
5417 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5418 and check that the resulting function has external linkage. */
5421 convert_nontype_argument_function (tree type
, tree expr
,
5422 tsubst_flags_t complain
)
5426 linkage_kind linkage
;
5428 fn
= instantiate_type (type
, fns
, tf_none
);
5429 if (fn
== error_mark_node
)
5430 return error_mark_node
;
5433 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5434 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5435 if (BASELINK_P (fn_no_ptr
))
5436 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5438 /* [temp.arg.nontype]/1
5440 A template-argument for a non-type, non-template template-parameter
5443 -- the address of an object or function with external [C++11: or
5444 internal] linkage. */
5446 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
5448 if (complain
& tf_error
)
5450 error ("%qE is not a valid template argument for type %qT",
5452 if (TYPE_PTR_P (type
))
5453 error ("it must be the address of a function with "
5454 "external linkage");
5456 error ("it must be the name of a function with "
5457 "external linkage");
5462 linkage
= decl_linkage (fn_no_ptr
);
5463 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
5465 if (complain
& tf_error
)
5467 if (cxx_dialect
>= cxx11
)
5468 error ("%qE is not a valid template argument for type %qT "
5469 "because %qD has no linkage",
5470 expr
, type
, fn_no_ptr
);
5472 error ("%qE is not a valid template argument for type %qT "
5473 "because %qD does not have external linkage",
5474 expr
, type
, fn_no_ptr
);
5482 /* Subroutine of convert_nontype_argument.
5483 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5484 Emit an error otherwise. */
5487 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5488 tsubst_flags_t complain
)
5491 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5493 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
5495 if (processing_template_decl
5496 && TREE_CODE (expr
) == ADDR_EXPR
5497 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
5499 if (complain
& tf_error
)
5501 error ("%qE is not a valid template argument for type %qT",
5503 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5508 /* Returns TRUE iff the address of OP is value-dependent.
5510 14.6.2.4 [temp.dep.temp]:
5511 A non-integral non-type template-argument is dependent if its type is
5512 dependent or it has either of the following forms
5515 and contains a nested-name-specifier which specifies a class-name that
5516 names a dependent type.
5518 We generalize this to just say that the address of a member of a
5519 dependent class is value-dependent; the above doesn't cover the
5520 address of a static data member named with an unqualified-id. */
5523 has_value_dependent_address (tree op
)
5525 /* We could use get_inner_reference here, but there's no need;
5526 this is only relevant for template non-type arguments, which
5527 can only be expressed as &id-expression. */
5530 tree ctx
= CP_DECL_CONTEXT (op
);
5531 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5538 /* The next set of functions are used for providing helpful explanatory
5539 diagnostics for failed overload resolution. Their messages should be
5540 indented by two spaces for consistency with the messages in
5544 unify_success (bool /*explain_p*/)
5550 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5553 inform (input_location
,
5554 " couldn't deduce template parameter %qD", parm
);
5559 unify_invalid (bool /*explain_p*/)
5565 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5568 inform (input_location
,
5569 " types %qT and %qT have incompatible cv-qualifiers",
5575 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5578 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5583 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
5586 inform (input_location
,
5587 " template parameter %qD is not a parameter pack, but "
5594 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
5597 inform (input_location
,
5598 " template argument %qE does not match "
5599 "pointer-to-member constant %qE",
5605 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
5608 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
5613 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
5616 inform (input_location
,
5617 " inconsistent parameter pack deduction with %qT and %qT",
5623 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
5628 inform (input_location
,
5629 " deduced conflicting types for parameter %qT (%qT and %qT)",
5630 parm
, first
, second
);
5632 inform (input_location
,
5633 " deduced conflicting values for non-type parameter "
5634 "%qE (%qE and %qE)", parm
, first
, second
);
5640 unify_vla_arg (bool explain_p
, tree arg
)
5643 inform (input_location
,
5644 " variable-sized array type %qT is not "
5645 "a valid template argument",
5651 unify_method_type_error (bool explain_p
, tree arg
)
5654 inform (input_location
,
5655 " member function type %qT is not a valid template argument",
5661 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
5666 inform_n (input_location
, wanted
,
5667 " candidate expects at least %d argument, %d provided",
5668 " candidate expects at least %d arguments, %d provided",
5671 inform_n (input_location
, wanted
,
5672 " candidate expects %d argument, %d provided",
5673 " candidate expects %d arguments, %d provided",
5680 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
5682 return unify_arity (explain_p
, have
, wanted
);
5686 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
5687 bool least_p
= false)
5689 return unify_arity (explain_p
, have
, wanted
, least_p
);
5693 unify_arg_conversion (bool explain_p
, tree to_type
,
5694 tree from_type
, tree arg
)
5697 inform (EXPR_LOC_OR_LOC (arg
, input_location
),
5698 " cannot convert %qE (type %qT) to type %qT",
5699 arg
, from_type
, to_type
);
5704 unify_no_common_base (bool explain_p
, enum template_base_result r
,
5705 tree parm
, tree arg
)
5710 case tbr_ambiguous_baseclass
:
5711 inform (input_location
, " %qT is an ambiguous base class of %qT",
5715 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
5722 unify_inconsistent_template_template_parameters (bool explain_p
)
5725 inform (input_location
,
5726 " template parameters of a template template argument are "
5727 "inconsistent with other deduced template arguments");
5732 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
5735 inform (input_location
,
5736 " can't deduce a template for %qT from non-template type %qT",
5742 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
5745 inform (input_location
,
5746 " template argument %qE does not match %qD", arg
, parm
);
5751 unify_overload_resolution_failure (bool explain_p
, tree arg
)
5754 inform (input_location
,
5755 " could not resolve address from overloaded function %qE",
5760 /* Attempt to convert the non-type template parameter EXPR to the
5761 indicated TYPE. If the conversion is successful, return the
5762 converted value. If the conversion is unsuccessful, return
5763 NULL_TREE if we issued an error message, or error_mark_node if we
5764 did not. We issue error messages for out-and-out bad template
5765 parameters, but not simply because the conversion failed, since we
5766 might be just trying to do argument deduction. Both TYPE and EXPR
5767 must be non-dependent.
5769 The conversion follows the special rules described in
5770 [temp.arg.nontype], and it is much more strict than an implicit
5773 This function is called twice for each template argument (see
5774 lookup_template_class for a more accurate description of this
5775 problem). This means that we need to handle expressions which
5776 are not valid in a C++ source, but can be created from the
5777 first call (for instance, casts to perform conversions). These
5778 hacks can go away after we fix the double coercion problem. */
5781 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5785 /* Detect immediately string literals as invalid non-type argument.
5786 This special-case is not needed for correctness (we would easily
5787 catch this later), but only to provide better diagnostic for this
5788 common user mistake. As suggested by DR 100, we do not mention
5789 linkage issues in the diagnostic as this is not the point. */
5790 /* FIXME we're making this OK. */
5791 if (TREE_CODE (expr
) == STRING_CST
)
5793 if (complain
& tf_error
)
5794 error ("%qE is not a valid template argument for type %qT "
5795 "because string literals can never be used in this context",
5800 /* Add the ADDR_EXPR now for the benefit of
5801 value_dependent_expression_p. */
5802 if (TYPE_PTROBV_P (type
)
5803 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
5805 expr
= decay_conversion (expr
, complain
);
5806 if (expr
== error_mark_node
)
5807 return error_mark_node
;
5810 /* If we are in a template, EXPR may be non-dependent, but still
5811 have a syntactic, rather than semantic, form. For example, EXPR
5812 might be a SCOPE_REF, rather than the VAR_DECL to which the
5813 SCOPE_REF refers. Preserving the qualifying scope is necessary
5814 so that access checking can be performed when the template is
5815 instantiated -- but here we need the resolved form so that we can
5816 convert the argument. */
5817 bool non_dep
= false;
5818 if (TYPE_REF_OBJ_P (type
)
5819 && has_value_dependent_address (expr
))
5820 /* If we want the address and it's value-dependent, don't fold. */;
5821 else if (!type_unknown_p (expr
)
5822 && processing_template_decl
5823 && !instantiation_dependent_expression_p (expr
)
5824 && potential_constant_expression (expr
))
5826 if (error_operand_p (expr
))
5827 return error_mark_node
;
5828 expr_type
= TREE_TYPE (expr
);
5829 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5830 expr
= mark_lvalue_use (expr
);
5832 expr
= mark_rvalue_use (expr
);
5834 /* If the argument is non-dependent, perform any conversions in
5835 non-dependent context as well. */
5836 processing_template_decl_sentinel
s (non_dep
);
5838 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
5840 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5841 to a non-type argument of "nullptr". */
5842 if (expr
== nullptr_node
&& TYPE_PTR_OR_PTRMEM_P (type
))
5843 expr
= convert (type
, expr
);
5845 /* In C++11, integral or enumeration non-type template arguments can be
5846 arbitrary constant expressions. Pointer and pointer to
5847 member arguments can be general constant expressions that evaluate
5848 to a null value, but otherwise still need to be of a specific form. */
5849 if (cxx_dialect
>= cxx11
)
5851 if (TREE_CODE (expr
) == PTRMEM_CST
)
5852 /* A PTRMEM_CST is already constant, and a valid template
5853 argument for a parameter of pointer to member type, we just want
5854 to leave it in that form rather than lower it to a
5856 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5857 expr
= maybe_constant_value (expr
);
5858 else if (TYPE_PTR_OR_PTRMEM_P (type
))
5860 tree folded
= maybe_constant_value (expr
);
5861 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
5862 : null_member_pointer_value_p (folded
))
5867 /* HACK: Due to double coercion, we can get a
5868 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5869 which is the tree that we built on the first call (see
5870 below when coercing to reference to object or to reference to
5871 function). We just strip everything and get to the arg.
5872 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5874 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5876 tree probe_type
, probe
= expr
;
5877 if (REFERENCE_REF_P (probe
))
5878 probe
= TREE_OPERAND (probe
, 0);
5879 probe_type
= TREE_TYPE (probe
);
5880 if (TREE_CODE (probe
) == NOP_EXPR
)
5882 /* ??? Maybe we could use convert_from_reference here, but we
5883 would need to relax its constraints because the NOP_EXPR
5884 could actually change the type to something more cv-qualified,
5885 and this is not folded by convert_from_reference. */
5886 tree addr
= TREE_OPERAND (probe
, 0);
5887 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
5888 && TREE_CODE (addr
) == ADDR_EXPR
5889 && TYPE_PTR_P (TREE_TYPE (addr
))
5890 && (same_type_ignoring_top_level_qualifiers_p
5891 (TREE_TYPE (probe_type
),
5892 TREE_TYPE (TREE_TYPE (addr
)))))
5894 expr
= TREE_OPERAND (addr
, 0);
5895 expr_type
= TREE_TYPE (probe_type
);
5900 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5901 parameter is a pointer to object, through decay and
5902 qualification conversion. Let's strip everything. */
5903 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
5907 if (TREE_CODE (probe
) == ADDR_EXPR
5908 && TYPE_PTR_P (TREE_TYPE (probe
)))
5910 /* Skip the ADDR_EXPR only if it is part of the decay for
5911 an array. Otherwise, it is part of the original argument
5912 in the source code. */
5913 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe
, 0))) == ARRAY_TYPE
)
5914 probe
= TREE_OPERAND (probe
, 0);
5916 expr_type
= TREE_TYPE (expr
);
5920 /* [temp.arg.nontype]/5, bullet 1
5922 For a non-type template-parameter of integral or enumeration type,
5923 integral promotions (_conv.prom_) and integral conversions
5924 (_conv.integral_) are applied. */
5925 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5927 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
5928 t
= maybe_constant_value (t
);
5929 if (t
!= error_mark_node
)
5932 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
5933 return error_mark_node
;
5935 /* Notice that there are constant expressions like '4 % 0' which
5936 do not fold into integer constants. */
5937 if (TREE_CODE (expr
) != INTEGER_CST
)
5939 if (complain
& tf_error
)
5941 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
5942 if (processing_template_decl
5943 && !require_potential_constant_expression (expr
))
5945 expr
= cxx_constant_value (expr
);
5946 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
5947 inform (EXPR_LOC_OR_LOC (expr
, input_location
),
5948 "in template argument for type %qT ", type
);
5949 if (expr
== error_mark_node
)
5951 /* else cxx_constant_value complained but gave us
5952 a real constant, so go ahead. */
5953 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
5959 /* Avoid typedef problems. */
5960 if (TREE_TYPE (expr
) != type
)
5961 expr
= fold_convert (type
, expr
);
5963 /* [temp.arg.nontype]/5, bullet 2
5965 For a non-type template-parameter of type pointer to object,
5966 qualification conversions (_conv.qual_) and the array-to-pointer
5967 conversion (_conv.array_) are applied. */
5968 else if (TYPE_PTROBV_P (type
))
5970 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5972 A template-argument for a non-type, non-template template-parameter
5973 shall be one of: [...]
5975 -- the name of a non-type template-parameter;
5976 -- the address of an object or function with external linkage, [...]
5977 expressed as "& id-expression" where the & is optional if the name
5978 refers to a function or array, or if the corresponding
5979 template-parameter is a reference.
5981 Here, we do not care about functions, as they are invalid anyway
5982 for a parameter of type pointer-to-object. */
5984 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5985 /* Non-type template parameters are OK. */
5987 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
5988 /* Null pointer values are OK in C++11. */;
5989 else if (TREE_CODE (expr
) != ADDR_EXPR
5990 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5994 if (complain
& tf_error
)
5995 error ("%qD is not a valid template argument "
5996 "because %qD is a variable, not the address of "
5997 "a variable", expr
, expr
);
6000 if (POINTER_TYPE_P (expr_type
))
6002 if (complain
& tf_error
)
6003 error ("%qE is not a valid template argument for %qT "
6004 "because it is not the address of a variable",
6008 /* Other values, like integer constants, might be valid
6009 non-type arguments of some other type. */
6010 return error_mark_node
;
6016 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
6017 ? TREE_OPERAND (expr
, 0) : expr
);
6020 if (complain
& tf_error
)
6021 error ("%qE is not a valid template argument of type %qT "
6022 "because %qE is not a variable", expr
, type
, decl
);
6025 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6027 if (complain
& tf_error
)
6028 error ("%qE is not a valid template argument of type %qT "
6029 "because %qD does not have external linkage",
6033 else if (cxx_dialect
>= cxx11
&& decl_linkage (decl
) == lk_none
)
6035 if (complain
& tf_error
)
6036 error ("%qE is not a valid template argument of type %qT "
6037 "because %qD has no linkage", expr
, type
, decl
);
6042 expr
= decay_conversion (expr
, complain
);
6043 if (expr
== error_mark_node
)
6044 return error_mark_node
;
6046 expr
= perform_qualification_conversions (type
, expr
);
6047 if (expr
== error_mark_node
)
6048 return error_mark_node
;
6050 /* [temp.arg.nontype]/5, bullet 3
6052 For a non-type template-parameter of type reference to object, no
6053 conversions apply. The type referred to by the reference may be more
6054 cv-qualified than the (otherwise identical) type of the
6055 template-argument. The template-parameter is bound directly to the
6056 template-argument, which must be an lvalue. */
6057 else if (TYPE_REF_OBJ_P (type
))
6059 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
6061 return error_mark_node
;
6063 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
6065 if (complain
& tf_error
)
6066 error ("%qE is not a valid template argument for type %qT "
6067 "because of conflicts in cv-qualification", expr
, type
);
6071 if (!real_lvalue_p (expr
))
6073 if (complain
& tf_error
)
6074 error ("%qE is not a valid template argument for type %qT "
6075 "because it is not an lvalue", expr
, type
);
6079 /* [temp.arg.nontype]/1
6081 A template-argument for a non-type, non-template template-parameter
6082 shall be one of: [...]
6084 -- the address of an object or function with external linkage. */
6085 if (INDIRECT_REF_P (expr
)
6086 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
6088 expr
= TREE_OPERAND (expr
, 0);
6091 if (complain
& tf_error
)
6092 error ("%q#D is not a valid template argument for type %qT "
6093 "because a reference variable does not have a constant "
6094 "address", expr
, type
);
6101 if (complain
& tf_error
)
6102 error ("%qE is not a valid template argument for type %qT "
6103 "because it is not an object with external linkage",
6108 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
6110 if (complain
& tf_error
)
6111 error ("%qE is not a valid template argument for type %qT "
6112 "because object %qD has not external linkage",
6117 expr
= build_nop (type
, build_address (expr
));
6119 /* [temp.arg.nontype]/5, bullet 4
6121 For a non-type template-parameter of type pointer to function, only
6122 the function-to-pointer conversion (_conv.func_) is applied. If the
6123 template-argument represents a set of overloaded functions (or a
6124 pointer to such), the matching function is selected from the set
6126 else if (TYPE_PTRFN_P (type
))
6128 /* If the argument is a template-id, we might not have enough
6129 context information to decay the pointer. */
6130 if (!type_unknown_p (expr_type
))
6132 expr
= decay_conversion (expr
, complain
);
6133 if (expr
== error_mark_node
)
6134 return error_mark_node
;
6137 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6138 /* Null pointer values are OK in C++11. */
6139 return perform_qualification_conversions (type
, expr
);
6141 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6142 if (!expr
|| expr
== error_mark_node
)
6145 /* [temp.arg.nontype]/5, bullet 5
6147 For a non-type template-parameter of type reference to function, no
6148 conversions apply. If the template-argument represents a set of
6149 overloaded functions, the matching function is selected from the set
6151 else if (TYPE_REFFN_P (type
))
6153 if (TREE_CODE (expr
) == ADDR_EXPR
)
6155 if (complain
& tf_error
)
6157 error ("%qE is not a valid template argument for type %qT "
6158 "because it is a pointer", expr
, type
);
6159 inform (input_location
, "try using %qE instead",
6160 TREE_OPERAND (expr
, 0));
6165 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6166 if (!expr
|| expr
== error_mark_node
)
6169 expr
= build_nop (type
, build_address (expr
));
6171 /* [temp.arg.nontype]/5, bullet 6
6173 For a non-type template-parameter of type pointer to member function,
6174 no conversions apply. If the template-argument represents a set of
6175 overloaded member functions, the matching member function is selected
6176 from the set (_over.over_). */
6177 else if (TYPE_PTRMEMFUNC_P (type
))
6179 expr
= instantiate_type (type
, expr
, tf_none
);
6180 if (expr
== error_mark_node
)
6181 return error_mark_node
;
6183 /* [temp.arg.nontype] bullet 1 says the pointer to member
6184 expression must be a pointer-to-member constant. */
6185 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6186 return error_mark_node
;
6188 /* There is no way to disable standard conversions in
6189 resolve_address_of_overloaded_function (called by
6190 instantiate_type). It is possible that the call succeeded by
6191 converting &B::I to &D::I (where B is a base of D), so we need
6192 to reject this conversion here.
6194 Actually, even if there was a way to disable standard conversions,
6195 it would still be better to reject them here so that we can
6196 provide a superior diagnostic. */
6197 if (!same_type_p (TREE_TYPE (expr
), type
))
6199 if (complain
& tf_error
)
6201 error ("%qE is not a valid template argument for type %qT "
6202 "because it is of type %qT", expr
, type
,
6204 /* If we are just one standard conversion off, explain. */
6205 if (can_convert_standard (type
, TREE_TYPE (expr
), complain
))
6206 inform (input_location
,
6207 "standard conversions are not allowed in this context");
6212 /* [temp.arg.nontype]/5, bullet 7
6214 For a non-type template-parameter of type pointer to data member,
6215 qualification conversions (_conv.qual_) are applied. */
6216 else if (TYPE_PTRDATAMEM_P (type
))
6218 /* [temp.arg.nontype] bullet 1 says the pointer to member
6219 expression must be a pointer-to-member constant. */
6220 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6221 return error_mark_node
;
6223 expr
= perform_qualification_conversions (type
, expr
);
6224 if (expr
== error_mark_node
)
6227 else if (NULLPTR_TYPE_P (type
))
6229 if (expr
!= nullptr_node
)
6231 if (complain
& tf_error
)
6232 error ("%qE is not a valid template argument for type %qT "
6233 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
6238 /* A template non-type parameter must be one of the above. */
6242 /* Sanity check: did we actually convert the argument to the
6244 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6245 (type
, TREE_TYPE (expr
)));
6246 return convert_from_reference (expr
);
6249 /* Subroutine of coerce_template_template_parms, which returns 1 if
6250 PARM_PARM and ARG_PARM match using the rule for the template
6251 parameters of template template parameters. Both PARM and ARG are
6252 template parameters; the rest of the arguments are the same as for
6253 coerce_template_template_parms.
6256 coerce_template_template_parm (tree parm
,
6258 tsubst_flags_t complain
,
6262 if (arg
== NULL_TREE
|| error_operand_p (arg
)
6263 || parm
== NULL_TREE
|| error_operand_p (parm
))
6266 if (TREE_CODE (arg
) != TREE_CODE (parm
))
6269 switch (TREE_CODE (parm
))
6272 /* We encounter instantiations of templates like
6273 template <template <template <class> class> class TT>
6276 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6277 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6279 if (!coerce_template_template_parms
6280 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
6286 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
6287 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6288 /* Argument is a parameter pack but parameter is not. */
6293 /* The tsubst call is used to handle cases such as
6295 template <int> class C {};
6296 template <class T, template <T> class TT> class D {};
6299 i.e. the parameter list of TT depends on earlier parameters. */
6300 if (!uses_template_parms (TREE_TYPE (arg
))
6302 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
6306 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6307 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6308 /* Argument is a parameter pack but parameter is not. */
6321 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6322 template template parameters. Both PARM_PARMS and ARG_PARMS are
6323 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6326 Consider the example:
6327 template <class T> class A;
6328 template<template <class U> class TT> class B;
6330 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6331 the parameters to A, and OUTER_ARGS contains A. */
6334 coerce_template_template_parms (tree parm_parms
,
6336 tsubst_flags_t complain
,
6340 int nparms
, nargs
, i
;
6344 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
6345 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
6347 nparms
= TREE_VEC_LENGTH (parm_parms
);
6348 nargs
= TREE_VEC_LENGTH (arg_parms
);
6350 /* Determine whether we have a parameter pack at the end of the
6351 template template parameter's template parameter list. */
6352 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
6354 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
6356 if (error_operand_p (parm
))
6359 switch (TREE_CODE (parm
))
6363 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6368 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6378 && !(variadic_p
&& nargs
>= nparms
- 1))
6381 /* Check all of the template parameters except the parameter pack at
6382 the end (if any). */
6383 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6385 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6386 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6389 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6390 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6392 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6400 /* Check each of the template parameters in the template
6401 argument against the template parameter pack at the end of
6402 the template template parameter. */
6403 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6406 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6408 for (; i
< nargs
; ++i
)
6410 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6413 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6415 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6424 /* Verifies that the deduced template arguments (in TARGS) for the
6425 template template parameters (in TPARMS) represent valid bindings,
6426 by comparing the template parameter list of each template argument
6427 to the template parameter list of its corresponding template
6428 template parameter, in accordance with DR150. This
6429 routine can only be called after all template arguments have been
6430 deduced. It will return TRUE if all of the template template
6431 parameter bindings are okay, FALSE otherwise. */
6433 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6435 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6438 /* We're dealing with template parms in this process. */
6439 ++processing_template_decl
;
6441 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6443 for (i
= 0; i
< ntparms
; ++i
)
6445 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6446 tree targ
= TREE_VEC_ELT (targs
, i
);
6448 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6450 tree packed_args
= NULL_TREE
;
6453 if (ARGUMENT_PACK_P (targ
))
6455 /* Look inside the argument pack. */
6456 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6457 len
= TREE_VEC_LENGTH (packed_args
);
6460 for (idx
= 0; idx
< len
; ++idx
)
6462 tree targ_parms
= NULL_TREE
;
6465 /* Extract the next argument from the argument
6467 targ
= TREE_VEC_ELT (packed_args
, idx
);
6469 if (PACK_EXPANSION_P (targ
))
6470 /* Look at the pattern of the pack expansion. */
6471 targ
= PACK_EXPANSION_PATTERN (targ
);
6473 /* Extract the template parameters from the template
6475 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6476 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6477 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6478 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6480 /* Verify that we can coerce the template template
6481 parameters from the template argument to the template
6482 parameter. This requires an exact match. */
6484 && !coerce_template_template_parms
6485 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6500 --processing_template_decl
;
6504 /* Since type attributes aren't mangled, we need to strip them from
6505 template type arguments. */
6508 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6510 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6512 bool removed_attributes
= false;
6513 tree canon
= strip_typedefs (arg
, &removed_attributes
);
6514 if (removed_attributes
6515 && (complain
& tf_warning
))
6516 warning (0, "ignoring attributes on template argument %qT", arg
);
6520 /* Convert the indicated template ARG as necessary to match the
6521 indicated template PARM. Returns the converted ARG, or
6522 error_mark_node if the conversion was unsuccessful. Error and
6523 warning messages are issued under control of COMPLAIN. This
6524 conversion is for the Ith parameter in the parameter list. ARGS is
6525 the full set of template arguments deduced so far. */
6528 convert_template_argument (tree parm
,
6531 tsubst_flags_t complain
,
6537 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
6539 if (parm
== error_mark_node
)
6540 return error_mark_node
;
6542 if (TREE_CODE (arg
) == TREE_LIST
6543 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
6545 /* The template argument was the name of some
6546 member function. That's usually
6547 invalid, but static members are OK. In any
6548 case, grab the underlying fields/functions
6549 and issue an error later if required. */
6550 orig_arg
= TREE_VALUE (arg
);
6551 TREE_TYPE (arg
) = unknown_type_node
;
6556 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
6557 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
6558 || requires_tmpl_type
);
6560 /* When determining whether an argument pack expansion is a template,
6561 look at the pattern. */
6562 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
6563 arg
= PACK_EXPANSION_PATTERN (arg
);
6565 /* Deal with an injected-class-name used as a template template arg. */
6566 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
6568 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
6569 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6571 if (cxx_dialect
>= cxx11
)
6572 /* OK under DR 1004. */;
6573 else if (complain
& tf_warning_or_error
)
6574 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
6575 " used as template template argument", TYPE_NAME (arg
));
6576 else if (flag_pedantic_errors
)
6584 ((TREE_CODE (arg
) == TEMPLATE_DECL
6585 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
6586 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
6587 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6588 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
6591 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6592 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
6593 arg
= TYPE_STUB_DECL (arg
);
6595 is_type
= TYPE_P (arg
) || is_tmpl_type
;
6597 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
6598 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
6600 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
6602 if (complain
& tf_error
)
6603 error ("invalid use of destructor %qE as a type", orig_arg
);
6604 return error_mark_node
;
6607 permerror (input_location
,
6608 "to refer to a type member of a template parameter, "
6609 "use %<typename %E%>", orig_arg
);
6611 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
6612 TREE_OPERAND (arg
, 1),
6618 if (is_type
!= requires_type
)
6622 if (complain
& tf_error
)
6624 error ("type/value mismatch at argument %d in template "
6625 "parameter list for %qD",
6628 inform (input_location
,
6629 " expected a constant of type %qT, got %qT",
6631 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6632 else if (requires_tmpl_type
)
6633 inform (input_location
,
6634 " expected a class template, got %qE", orig_arg
);
6636 inform (input_location
,
6637 " expected a type, got %qE", orig_arg
);
6640 return error_mark_node
;
6642 if (is_tmpl_type
^ requires_tmpl_type
)
6644 if (in_decl
&& (complain
& tf_error
))
6646 error ("type/value mismatch at argument %d in template "
6647 "parameter list for %qD",
6650 inform (input_location
,
6651 " expected a type, got %qT", DECL_NAME (arg
));
6653 inform (input_location
,
6654 " expected a class template, got %qT", orig_arg
);
6656 return error_mark_node
;
6661 if (requires_tmpl_type
)
6663 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6665 else if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6666 /* The number of argument required is not known yet.
6667 Just accept it for now. */
6668 val
= TREE_TYPE (arg
);
6671 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6674 /* Strip alias templates that are equivalent to another
6676 arg
= get_underlying_template (arg
);
6677 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6679 if (coerce_template_template_parms (parmparm
, argparm
,
6685 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6687 if (val
!= error_mark_node
)
6689 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6690 val
= TREE_TYPE (val
);
6691 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6692 val
= make_pack_expansion (val
);
6697 if (in_decl
&& (complain
& tf_error
))
6699 error ("type/value mismatch at argument %d in "
6700 "template parameter list for %qD",
6702 inform (input_location
,
6703 " expected a template of type %qD, got %qT",
6707 val
= error_mark_node
;
6713 /* We only form one instance of each template specialization.
6714 Therefore, if we use a non-canonical variant (i.e., a
6715 typedef), any future messages referring to the type will use
6716 the typedef, which is confusing if those future uses do not
6717 themselves also use the typedef. */
6719 val
= canonicalize_type_argument (val
, complain
);
6723 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6725 if (invalid_nontype_parm_type_p (t
, complain
))
6726 return error_mark_node
;
6728 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6730 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6734 /* Not sure if this is reachable, but it doesn't hurt
6736 error ("type mismatch in nontype parameter pack");
6737 val
= error_mark_node
;
6740 else if (!dependent_template_arg_p (orig_arg
)
6741 && !uses_template_parms (t
))
6742 /* We used to call digest_init here. However, digest_init
6743 will report errors, which we don't want when complain
6744 is zero. More importantly, digest_init will try too
6745 hard to convert things: for example, `0' should not be
6746 converted to pointer type at this point according to
6747 the standard. Accepting this is not merely an
6748 extension, since deciding whether or not these
6749 conversions can occur is part of determining which
6750 function template to call, or whether a given explicit
6751 argument specification is valid. */
6752 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6755 bool removed_attr
= false;
6756 val
= strip_typedefs_expr (orig_arg
, &removed_attr
);
6759 if (val
== NULL_TREE
)
6760 val
= error_mark_node
;
6761 else if (val
== error_mark_node
&& (complain
& tf_error
))
6762 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6764 if (TREE_CODE (val
) == SCOPE_REF
)
6766 /* Strip typedefs from the SCOPE_REF. */
6767 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
6768 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
6770 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6771 QUALIFIED_NAME_IS_TEMPLATE (val
));
6778 /* Coerces the remaining template arguments in INNER_ARGS (from
6779 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6780 Returns the coerced argument pack. PARM_IDX is the position of this
6781 parameter in the template parameter list. ARGS is the original
6782 template argument list. */
6784 coerce_template_parameter_pack (tree parms
,
6792 tsubst_flags_t complain
)
6794 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6795 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6798 tree packed_parms
= NULL_TREE
;
6800 if (arg_idx
> nargs
)
6803 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
6805 /* When the template parameter is a non-type template parameter pack
6806 or template template parameter pack whose type or template
6807 parameters use parameter packs, we know exactly how many arguments
6808 we are looking for. Build a vector of the instantiated decls for
6809 these template parameters in PACKED_PARMS. */
6810 /* We can't use make_pack_expansion here because it would interpret a
6811 _DECL as a use rather than a declaration. */
6812 tree decl
= TREE_VALUE (parm
);
6813 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
6814 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
6815 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
6816 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
6818 TREE_VEC_LENGTH (args
)--;
6819 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
6820 TREE_VEC_LENGTH (args
)++;
6822 if (packed_parms
== error_mark_node
)
6823 return error_mark_node
;
6825 /* If we're doing a partial instantiation of a member template,
6826 verify that all of the types used for the non-type
6827 template parameter pack are, in fact, valid for non-type
6828 template parameters. */
6830 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6832 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
6833 for (j
= 0; j
< len
; ++j
)
6835 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
6836 if (invalid_nontype_parm_type_p (t
, complain
))
6837 return error_mark_node
;
6839 /* We don't know how many args we have yet, just
6840 use the unconverted ones for now. */
6844 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
6847 packed_args
= make_tree_vec (nargs
- arg_idx
);
6849 /* Convert the remaining arguments, which will be a part of the
6850 parameter pack "parm". */
6851 for (; arg_idx
< nargs
; ++arg_idx
)
6853 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6854 tree actual_parm
= TREE_VALUE (parm
);
6855 int pack_idx
= arg_idx
- parm_idx
;
6859 /* Once we've packed as many args as we have types, stop. */
6860 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
6862 else if (PACK_EXPANSION_P (arg
))
6863 /* We don't know how many args we have yet, just
6864 use the unconverted ones for now. */
6867 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
6870 if (arg
== error_mark_node
)
6872 if (complain
& tf_error
)
6873 error ("template argument %d is invalid", arg_idx
+ 1);
6876 arg
= convert_template_argument (actual_parm
,
6877 arg
, new_args
, complain
, parm_idx
,
6879 if (arg
== error_mark_node
)
6881 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
6884 if (arg_idx
- parm_idx
< TREE_VEC_LENGTH (packed_args
)
6885 && TREE_VEC_LENGTH (packed_args
) > 0)
6887 if (complain
& tf_error
)
6888 error ("wrong number of template arguments (%d, should be %d)",
6889 arg_idx
- parm_idx
, TREE_VEC_LENGTH (packed_args
));
6890 return error_mark_node
;
6893 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6894 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
6895 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
6898 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
6899 TREE_TYPE (argument_pack
)
6900 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
6901 TREE_CONSTANT (argument_pack
) = 1;
6904 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
6905 #ifdef ENABLE_CHECKING
6906 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
6907 TREE_VEC_LENGTH (packed_args
));
6909 return argument_pack
;
6912 /* Returns the number of pack expansions in the template argument vector
6916 pack_expansion_args_count (tree args
)
6921 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
6923 tree elt
= TREE_VEC_ELT (args
, i
);
6924 if (elt
&& PACK_EXPANSION_P (elt
))
6930 /* Convert all template arguments to their appropriate types, and
6931 return a vector containing the innermost resulting template
6932 arguments. If any error occurs, return error_mark_node. Error and
6933 warning messages are issued under control of COMPLAIN.
6935 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6936 for arguments not specified in ARGS. Otherwise, if
6937 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6938 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6939 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6943 coerce_template_parms (tree parms
,
6946 tsubst_flags_t complain
,
6947 bool require_all_args
,
6948 bool use_default_args
)
6950 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
6951 tree orig_inner_args
;
6954 tree new_inner_args
;
6955 int saved_unevaluated_operand
;
6956 int saved_inhibit_evaluation_warnings
;
6958 /* When used as a boolean value, indicates whether this is a
6959 variadic template parameter list. Since it's an int, we can also
6960 subtract it from nparms to get the number of non-variadic
6963 int variadic_args_p
= 0;
6964 int post_variadic_parms
= 0;
6966 /* Likewise for parameters with default arguments. */
6969 if (args
== error_mark_node
)
6970 return error_mark_node
;
6972 nparms
= TREE_VEC_LENGTH (parms
);
6974 /* Determine if there are any parameter packs or default arguments. */
6975 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
6977 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6979 ++post_variadic_parms
;
6980 if (template_parameter_pack_p (TREE_VALUE (parm
)))
6982 if (TREE_PURPOSE (parm
))
6986 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
6987 /* If there are no parameters that follow a parameter pack, we need to
6988 expand any argument packs so that we can deduce a parameter pack from
6989 some non-packed args followed by an argument pack, as in variadic85.C.
6990 If there are such parameters, we need to leave argument packs intact
6991 so the arguments are assigned properly. This can happen when dealing
6992 with a nested class inside a partial specialization of a class
6993 template, as in variadic92.C, or when deducing a template parameter pack
6994 from a sub-declarator, as in variadic114.C. */
6995 if (!post_variadic_parms
)
6996 inner_args
= expand_template_argument_pack (inner_args
);
6998 /* Count any pack expansion args. */
6999 variadic_args_p
= pack_expansion_args_count (inner_args
);
7001 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
7002 if ((nargs
> nparms
&& !variadic_p
)
7003 || (nargs
< nparms
- variadic_p
7006 && (!use_default_args
7007 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
7008 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
7010 if (complain
& tf_error
)
7012 if (variadic_p
|| default_p
)
7014 nparms
-= variadic_p
+ default_p
;
7015 error ("wrong number of template arguments "
7016 "(%d, should be at least %d)", nargs
, nparms
);
7019 error ("wrong number of template arguments "
7020 "(%d, should be %d)", nargs
, nparms
);
7023 inform (input_location
, "provided for %q+D", in_decl
);
7026 return error_mark_node
;
7028 /* We can't pass a pack expansion to a non-pack parameter of an alias
7029 template (DR 1430). */
7030 else if (in_decl
&& DECL_ALIAS_TEMPLATE_P (in_decl
)
7032 && nargs
- variadic_args_p
< nparms
- variadic_p
)
7034 if (complain
& tf_error
)
7036 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
7038 tree arg
= TREE_VEC_ELT (inner_args
, i
);
7039 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
7041 if (PACK_EXPANSION_P (arg
)
7042 && !template_parameter_pack_p (parm
))
7044 error ("pack expansion argument for non-pack parameter "
7045 "%qD of alias template %qD", parm
, in_decl
);
7046 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
7053 return error_mark_node
;
7056 /* We need to evaluate the template arguments, even though this
7057 template-id may be nested within a "sizeof". */
7058 saved_unevaluated_operand
= cp_unevaluated_operand
;
7059 cp_unevaluated_operand
= 0;
7060 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
7061 c_inhibit_evaluation_warnings
= 0;
7062 new_inner_args
= make_tree_vec (nparms
);
7063 new_args
= add_outermost_template_args (args
, new_inner_args
);
7064 int pack_adjust
= 0;
7065 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
7070 /* Get the Ith template parameter. */
7071 parm
= TREE_VEC_ELT (parms
, parm_idx
);
7073 if (parm
== error_mark_node
)
7075 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
7079 /* Calculate the next argument. */
7080 if (arg_idx
< nargs
)
7081 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
7085 if (template_parameter_pack_p (TREE_VALUE (parm
))
7086 && !(arg
&& ARGUMENT_PACK_P (arg
)))
7088 /* Some arguments will be placed in the
7089 template parameter pack PARM. */
7090 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
7091 inner_args
, arg_idx
,
7095 if (arg
== NULL_TREE
)
7097 /* We don't know how many args we have yet, just use the
7098 unconverted (and still packed) ones for now. */
7099 new_inner_args
= orig_inner_args
;
7104 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
7106 /* Store this argument. */
7107 if (arg
== error_mark_node
)
7110 /* We are done with all of the arguments. */
7115 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
7116 arg_idx
+= pack_adjust
;
7123 if (PACK_EXPANSION_P (arg
))
7125 /* "If every valid specialization of a variadic template
7126 requires an empty template parameter pack, the template is
7127 ill-formed, no diagnostic required." So check that the
7128 pattern works with this parameter. */
7129 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
7130 tree conv
= convert_template_argument (TREE_VALUE (parm
),
7134 if (conv
== error_mark_node
)
7136 inform (input_location
, "so any instantiation with a "
7137 "non-empty parameter pack would be ill-formed");
7140 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
7141 /* Recover from missing typename. */
7142 TREE_VEC_ELT (inner_args
, arg_idx
)
7143 = make_pack_expansion (conv
);
7145 /* We don't know how many args we have yet, just
7146 use the unconverted ones for now. */
7147 new_inner_args
= inner_args
;
7152 else if (require_all_args
)
7154 /* There must be a default arg in this case. */
7155 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
7157 /* The position of the first default template argument,
7158 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
7160 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
7161 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
7162 arg_idx
- pack_adjust
);
7167 if (arg
== error_mark_node
)
7169 if (complain
& tf_error
)
7170 error ("template argument %d is invalid", arg_idx
+ 1);
7173 /* This only occurs if there was an error in the template
7174 parameter list itself (which we would already have
7175 reported) that we are trying to recover from, e.g., a class
7176 template with a parameter list such as
7177 template<typename..., typename>. */
7180 arg
= convert_template_argument (TREE_VALUE (parm
),
7181 arg
, new_args
, complain
,
7184 if (arg
== error_mark_node
)
7186 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
7188 cp_unevaluated_operand
= saved_unevaluated_operand
;
7189 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
7191 if (variadic_p
&& arg_idx
< nargs
)
7193 if (complain
& tf_error
)
7195 error ("wrong number of template arguments "
7196 "(%d, should be %d)", nargs
, arg_idx
);
7198 error ("provided for %q+D", in_decl
);
7200 return error_mark_node
;
7204 return error_mark_node
;
7206 #ifdef ENABLE_CHECKING
7207 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
7208 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
7209 TREE_VEC_LENGTH (new_inner_args
));
7212 return new_inner_args
;
7215 /* Like coerce_template_parms. If PARMS represents all template
7216 parameters levels, this function returns a vector of vectors
7217 representing all the resulting argument levels. Note that in this
7218 case, only the innermost arguments are coerced because the
7219 outermost ones are supposed to have been coerced already.
7221 Otherwise, if PARMS represents only (the innermost) vector of
7222 parameters, this function returns a vector containing just the
7223 innermost resulting arguments. */
7226 coerce_innermost_template_parms (tree parms
,
7229 tsubst_flags_t complain
,
7230 bool require_all_args
,
7231 bool use_default_args
)
7233 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
7234 int args_depth
= TMPL_ARGS_DEPTH (args
);
7237 if (parms_depth
> 1)
7239 coerced_args
= make_tree_vec (parms_depth
);
7243 for (level
= parms
, cur_depth
= parms_depth
;
7244 parms_depth
> 0 && level
!= NULL_TREE
;
7245 level
= TREE_CHAIN (level
), --cur_depth
)
7248 if (cur_depth
== args_depth
)
7249 l
= coerce_template_parms (TREE_VALUE (level
),
7250 args
, in_decl
, complain
,
7254 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
7256 if (l
== error_mark_node
)
7257 return error_mark_node
;
7259 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
7263 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
7264 args
, in_decl
, complain
,
7267 return coerced_args
;
7270 /* Returns 1 if template args OT and NT are equivalent. */
7273 template_args_equal (tree ot
, tree nt
)
7277 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
7280 if (TREE_CODE (nt
) == TREE_VEC
)
7281 /* For member templates */
7282 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
7283 else if (PACK_EXPANSION_P (ot
))
7284 return (PACK_EXPANSION_P (nt
)
7285 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
7286 PACK_EXPANSION_PATTERN (nt
))
7287 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
7288 PACK_EXPANSION_EXTRA_ARGS (nt
)));
7289 else if (ARGUMENT_PACK_P (ot
))
7294 if (!ARGUMENT_PACK_P (nt
))
7297 opack
= ARGUMENT_PACK_ARGS (ot
);
7298 npack
= ARGUMENT_PACK_ARGS (nt
);
7299 len
= TREE_VEC_LENGTH (opack
);
7300 if (TREE_VEC_LENGTH (npack
) != len
)
7302 for (i
= 0; i
< len
; ++i
)
7303 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
7304 TREE_VEC_ELT (npack
, i
)))
7308 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
7310 /* We get here probably because we are in the middle of substituting
7311 into the pattern of a pack expansion. In that case the
7312 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
7313 interested in. So we want to use the initial pack argument for
7315 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
7316 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
7317 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
7318 return template_args_equal (ot
, nt
);
7320 else if (TYPE_P (nt
))
7324 /* Don't treat an alias template specialization with dependent
7325 arguments as equivalent to its underlying type when used as a
7326 template argument; we need them to be distinct so that we
7327 substitute into the specialization arguments at instantiation
7328 time. And aliases can't be equivalent without being ==, so
7329 we don't need to look any deeper. */
7330 if (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
))
7333 return same_type_p (ot
, nt
);
7335 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
7339 /* Try to treat a template non-type argument that has been converted
7340 to the parameter type as equivalent to one that hasn't yet. */
7341 for (enum tree_code code1
= TREE_CODE (ot
);
7342 CONVERT_EXPR_CODE_P (code1
)
7343 || code1
== NON_LVALUE_EXPR
;
7344 code1
= TREE_CODE (ot
))
7345 ot
= TREE_OPERAND (ot
, 0);
7346 for (enum tree_code code2
= TREE_CODE (nt
);
7347 CONVERT_EXPR_CODE_P (code2
)
7348 || code2
== NON_LVALUE_EXPR
;
7349 code2
= TREE_CODE (nt
))
7350 nt
= TREE_OPERAND (nt
, 0);
7352 return cp_tree_equal (ot
, nt
);
7356 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7357 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7358 NEWARG_PTR with the offending arguments if they are non-NULL. */
7361 comp_template_args_with_info (tree oldargs
, tree newargs
,
7362 tree
*oldarg_ptr
, tree
*newarg_ptr
)
7366 if (oldargs
== newargs
)
7369 if (!oldargs
|| !newargs
)
7372 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
7375 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
7377 tree nt
= TREE_VEC_ELT (newargs
, i
);
7378 tree ot
= TREE_VEC_ELT (oldargs
, i
);
7380 if (! template_args_equal (ot
, nt
))
7382 if (oldarg_ptr
!= NULL
)
7384 if (newarg_ptr
!= NULL
)
7392 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7393 of template arguments. Returns 0 otherwise. */
7396 comp_template_args (tree oldargs
, tree newargs
)
7398 return comp_template_args_with_info (oldargs
, newargs
, NULL
, NULL
);
7402 add_pending_template (tree d
)
7404 tree ti
= (TYPE_P (d
)
7405 ? CLASSTYPE_TEMPLATE_INFO (d
)
7406 : DECL_TEMPLATE_INFO (d
));
7407 struct pending_template
*pt
;
7410 if (TI_PENDING_TEMPLATE_FLAG (ti
))
7413 /* We are called both from instantiate_decl, where we've already had a
7414 tinst_level pushed, and instantiate_template, where we haven't.
7416 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
7419 push_tinst_level (d
);
7421 pt
= ggc_alloc
<pending_template
> ();
7423 pt
->tinst
= current_tinst_level
;
7424 if (last_pending_template
)
7425 last_pending_template
->next
= pt
;
7427 pending_templates
= pt
;
7429 last_pending_template
= pt
;
7431 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
7438 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7439 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7440 documentation for TEMPLATE_ID_EXPR. */
7443 lookup_template_function (tree fns
, tree arglist
)
7447 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
7448 return error_mark_node
;
7450 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
7452 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
7454 error ("%q#D is not a function template", fns
);
7455 return error_mark_node
;
7458 if (BASELINK_P (fns
))
7460 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
7462 BASELINK_FUNCTIONS (fns
),
7467 type
= TREE_TYPE (fns
);
7468 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
7469 type
= unknown_type_node
;
7471 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
7474 /* Within the scope of a template class S<T>, the name S gets bound
7475 (in build_self_reference) to a TYPE_DECL for the class, not a
7476 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7477 or one of its enclosing classes, and that type is a template,
7478 return the associated TEMPLATE_DECL. Otherwise, the original
7481 Also handle the case when DECL is a TREE_LIST of ambiguous
7482 injected-class-names from different bases. */
7485 maybe_get_template_decl_from_type_decl (tree decl
)
7487 if (decl
== NULL_TREE
)
7490 /* DR 176: A lookup that finds an injected-class-name (10.2
7491 [class.member.lookup]) can result in an ambiguity in certain cases
7492 (for example, if it is found in more than one base class). If all of
7493 the injected-class-names that are found refer to specializations of
7494 the same class template, and if the name is followed by a
7495 template-argument-list, the reference refers to the class template
7496 itself and not a specialization thereof, and is not ambiguous. */
7497 if (TREE_CODE (decl
) == TREE_LIST
)
7499 tree t
, tmpl
= NULL_TREE
;
7500 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
7502 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
7505 else if (tmpl
!= elt
)
7508 if (tmpl
&& t
== NULL_TREE
)
7514 return (decl
!= NULL_TREE
7515 && DECL_SELF_REFERENCE_P (decl
)
7516 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
7517 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
7520 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7521 parameters, find the desired type.
7523 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7525 IN_DECL, if non-NULL, is the template declaration we are trying to
7528 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7529 the class we are looking up.
7531 Issue error and warning messages under control of COMPLAIN.
7533 If the template class is really a local class in a template
7534 function, then the FUNCTION_CONTEXT is the function in which it is
7537 ??? Note that this function is currently called *twice* for each
7538 template-id: the first time from the parser, while creating the
7539 incomplete type (finish_template_type), and the second type during the
7540 real instantiation (instantiate_template_class). This is surely something
7541 that we want to avoid. It also causes some problems with argument
7542 coercion (see convert_nontype_argument for more information on this). */
7545 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
7546 int entering_scope
, tsubst_flags_t complain
)
7548 tree templ
= NULL_TREE
, parmlist
;
7555 if (identifier_p (d1
))
7557 tree value
= innermost_non_namespace_value (d1
);
7558 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
7563 push_decl_namespace (context
);
7564 templ
= lookup_name (d1
);
7565 templ
= maybe_get_template_decl_from_type_decl (templ
);
7567 pop_decl_namespace ();
7570 context
= DECL_CONTEXT (templ
);
7572 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
7574 tree type
= TREE_TYPE (d1
);
7576 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7577 an implicit typename for the second A. Deal with it. */
7578 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
7579 type
= TREE_TYPE (type
);
7581 if (CLASSTYPE_TEMPLATE_INFO (type
))
7583 templ
= CLASSTYPE_TI_TEMPLATE (type
);
7584 d1
= DECL_NAME (templ
);
7587 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
7588 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
7590 templ
= TYPE_TI_TEMPLATE (d1
);
7591 d1
= DECL_NAME (templ
);
7593 else if (DECL_TYPE_TEMPLATE_P (d1
))
7596 d1
= DECL_NAME (templ
);
7597 context
= DECL_CONTEXT (templ
);
7599 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
7602 d1
= DECL_NAME (templ
);
7605 /* Issue an error message if we didn't find a template. */
7608 if (complain
& tf_error
)
7609 error ("%qT is not a template", d1
);
7610 return error_mark_node
;
7613 if (TREE_CODE (templ
) != TEMPLATE_DECL
7614 /* Make sure it's a user visible template, if it was named by
7616 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
7617 && !PRIMARY_TEMPLATE_P (templ
)))
7619 if (complain
& tf_error
)
7621 error ("non-template type %qT used as a template", d1
);
7623 error ("for template declaration %q+D", in_decl
);
7625 return error_mark_node
;
7628 complain
&= ~tf_user
;
7630 /* An alias that just changes the name of a template is equivalent to the
7631 other template, so if any of the arguments are pack expansions, strip
7632 the alias to avoid problems with a pack expansion passed to a non-pack
7633 alias template parameter (DR 1430). */
7634 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
7635 templ
= get_underlying_template (templ
);
7637 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
7639 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7640 template arguments */
7646 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7648 /* Consider an example where a template template parameter declared as
7650 template <class T, class U = std::allocator<T> > class TT
7652 The template parameter level of T and U are one level larger than
7653 of TT. To proper process the default argument of U, say when an
7654 instantiation `TT<int>' is seen, we need to build the full
7655 arguments containing {int} as the innermost level. Outer levels,
7656 available when not appearing as default template argument, can be
7657 obtained from the arguments of the enclosing template.
7659 Suppose that TT is later substituted with std::vector. The above
7660 instantiation is `TT<int, std::allocator<T> >' with TT at
7661 level 1, and T at level 2, while the template arguments at level 1
7662 becomes {std::vector} and the inner level 2 is {int}. */
7664 outer
= DECL_CONTEXT (templ
);
7666 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7667 else if (current_template_parms
)
7668 /* This is an argument of the current template, so we haven't set
7669 DECL_CONTEXT yet. */
7670 outer
= current_template_args ();
7673 arglist
= add_to_template_args (outer
, arglist
);
7675 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
7677 /*require_all_args=*/true,
7678 /*use_default_args=*/true);
7679 if (arglist2
== error_mark_node
7680 || (!uses_template_parms (arglist2
)
7681 && check_instantiated_args (templ
, arglist2
, complain
)))
7682 return error_mark_node
;
7684 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
7689 tree template_type
= TREE_TYPE (templ
);
7692 tree found
= NULL_TREE
;
7695 int is_dependent_type
;
7696 int use_partial_inst_tmpl
= false;
7698 if (template_type
== error_mark_node
)
7699 /* An error occurred while building the template TEMPL, and a
7700 diagnostic has most certainly been emitted for that
7701 already. Let's propagate that error. */
7702 return error_mark_node
;
7704 gen_tmpl
= most_general_template (templ
);
7705 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7706 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
7707 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7709 if (arg_depth
== 1 && parm_depth
> 1)
7711 /* We've been given an incomplete set of template arguments.
7714 template <class T> struct S1 {
7715 template <class U> struct S2 {};
7716 template <class U> struct S2<U*> {};
7719 we will be called with an ARGLIST of `U*', but the
7720 TEMPLATE will be `template <class T> template
7721 <class U> struct S1<T>::S2'. We must fill in the missing
7724 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
7726 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7729 /* Now we should have enough arguments. */
7730 gcc_assert (parm_depth
== arg_depth
);
7732 /* From here on, we're only interested in the most general
7735 /* Calculate the BOUND_ARGS. These will be the args that are
7736 actually tsubst'd into the definition to create the
7738 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
7740 /*require_all_args=*/true,
7741 /*use_default_args=*/true);
7743 if (arglist
== error_mark_node
)
7744 /* We were unable to bind the arguments. */
7745 return error_mark_node
;
7747 /* In the scope of a template class, explicit references to the
7748 template class refer to the type of the template, not any
7749 instantiation of it. For example, in:
7751 template <class T> class C { void f(C<T>); }
7753 the `C<T>' is just the same as `C'. Outside of the
7754 class, however, such a reference is an instantiation. */
7756 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
7757 || currently_open_class (template_type
))
7758 /* comp_template_args is expensive, check it last. */
7759 && comp_template_args (TYPE_TI_ARGS (template_type
),
7761 return template_type
;
7763 /* If we already have this specialization, return it. */
7764 elt
.tmpl
= gen_tmpl
;
7766 hash
= spec_hasher::hash (&elt
);
7767 entry
= type_specializations
->find_with_hash (&elt
, hash
);
7772 is_dependent_type
= uses_template_parms (arglist
);
7774 /* If the deduced arguments are invalid, then the binding
7776 if (!is_dependent_type
7777 && check_instantiated_args (gen_tmpl
,
7778 INNERMOST_TEMPLATE_ARGS (arglist
),
7780 return error_mark_node
;
7782 if (!is_dependent_type
7783 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
7784 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
7785 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
7787 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
7788 DECL_NAME (gen_tmpl
),
7789 /*tag_scope=*/ts_global
);
7793 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
7795 if (context
== error_mark_node
)
7796 return error_mark_node
;
7799 context
= global_namespace
;
7801 /* Create the type. */
7802 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7804 /* The user referred to a specialization of an alias
7805 template represented by GEN_TMPL.
7807 [temp.alias]/2 says:
7809 When a template-id refers to the specialization of an
7810 alias template, it is equivalent to the associated
7811 type obtained by substitution of its
7812 template-arguments for the template-parameters in the
7813 type-id of the alias template. */
7815 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
7816 /* Note that the call above (by indirectly calling
7817 register_specialization in tsubst_decl) registers the
7818 TYPE_DECL representing the specialization of the alias
7819 template. So next time someone substitutes ARGLIST for
7820 the template parms into the alias template (GEN_TMPL),
7821 she'll get that TYPE_DECL back. */
7823 if (t
== error_mark_node
)
7826 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
7828 if (!is_dependent_type
)
7830 set_current_access_from_decl (TYPE_NAME (template_type
));
7831 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
7832 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
7833 arglist
, complain
, in_decl
),
7834 SCOPED_ENUM_P (template_type
), NULL
);
7836 if (t
== error_mark_node
)
7841 /* We don't want to call start_enum for this type, since
7842 the values for the enumeration constants may involve
7843 template parameters. And, no one should be interested
7844 in the enumeration constants for such a type. */
7845 t
= cxx_make_type (ENUMERAL_TYPE
);
7846 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
7848 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
7849 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
7850 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7852 else if (CLASS_TYPE_P (template_type
))
7854 t
= make_class_type (TREE_CODE (template_type
));
7855 CLASSTYPE_DECLARED_CLASS (t
)
7856 = CLASSTYPE_DECLARED_CLASS (template_type
);
7857 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7858 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7860 /* A local class. Make sure the decl gets registered properly. */
7861 if (context
== current_function_decl
)
7862 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7864 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7865 /* This instantiation is another name for the primary
7866 template type. Set the TYPE_CANONICAL field
7868 TYPE_CANONICAL (t
) = template_type
;
7869 else if (any_template_arguments_need_structural_equality_p (arglist
))
7870 /* Some of the template arguments require structural
7871 equality testing, so this template class requires
7872 structural equality testing. */
7873 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7878 /* If we called start_enum or pushtag above, this information
7879 will already be set up. */
7882 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
7884 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
7885 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
7886 DECL_SOURCE_LOCATION (type_decl
)
7887 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
7890 type_decl
= TYPE_NAME (t
);
7892 if (CLASS_TYPE_P (template_type
))
7894 TREE_PRIVATE (type_decl
)
7895 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
7896 TREE_PROTECTED (type_decl
)
7897 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
7898 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
7900 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
7901 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
7905 if (OVERLOAD_TYPE_P (t
)
7906 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7909 = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (template_type
)))
7911 if (!TREE_CHAIN (attributes
))
7912 TYPE_ATTRIBUTES (t
) = attributes
;
7915 = build_tree_list (TREE_PURPOSE (attributes
),
7916 TREE_VALUE (attributes
));
7920 /* Let's consider the explicit specialization of a member
7921 of a class template specialization that is implicitly instantiated,
7926 template<class U> struct M {}; //#0
7931 struct S<int>::M<char> //#1
7935 [temp.expl.spec]/4 says this is valid.
7937 In this case, when we write:
7940 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7943 When we encounter #1, we want to store the partial instantiation
7944 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7946 For all cases other than this "explicit specialization of member of a
7947 class template", we just want to store the most general template into
7948 the CLASSTYPE_TI_TEMPLATE of M.
7950 This case of "explicit specialization of member of a class template"
7952 1/ the enclosing class is an instantiation of, and therefore not
7953 the same as, the context of the most general template, and
7954 2/ we aren't looking at the partial instantiation itself, i.e.
7955 the innermost arguments are not the same as the innermost parms of
7956 the most general template.
7958 So it's only when 1/ and 2/ happens that we want to use the partial
7959 instantiation of the member template in lieu of its most general
7962 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
7963 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
7964 /* the enclosing class must be an instantiation... */
7965 && CLASS_TYPE_P (context
)
7966 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
7968 tree partial_inst_args
;
7969 TREE_VEC_LENGTH (arglist
)--;
7970 ++processing_template_decl
;
7972 tsubst (INNERMOST_TEMPLATE_ARGS
7973 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
7974 arglist
, complain
, NULL_TREE
);
7975 --processing_template_decl
;
7976 TREE_VEC_LENGTH (arglist
)++;
7977 use_partial_inst_tmpl
=
7978 /*...and we must not be looking at the partial instantiation
7980 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
7984 if (!use_partial_inst_tmpl
)
7985 /* This case is easy; there are no member templates involved. */
7989 /* This is a full instantiation of a member template. Find
7990 the partial instantiation of which this is an instance. */
7992 /* Temporarily reduce by one the number of levels in the ARGLIST
7993 so as to avoid comparing the last set of arguments. */
7994 TREE_VEC_LENGTH (arglist
)--;
7995 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
7996 TREE_VEC_LENGTH (arglist
)++;
7997 /* FOUND is either a proper class type, or an alias
7998 template specialization. In the later case, it's a
7999 TYPE_DECL, resulting from the substituting of arguments
8000 for parameters in the TYPE_DECL of the alias template
8001 done earlier. So be careful while getting the template
8003 found
= TREE_CODE (found
) == TYPE_DECL
8004 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
8005 : CLASSTYPE_TI_TEMPLATE (found
);
8008 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
8011 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
8012 entry
= ggc_alloc
<spec_entry
> ();
8016 /* Note this use of the partial instantiation so we can check it
8017 later in maybe_process_partial_specialization. */
8018 DECL_TEMPLATE_INSTANTIATIONS (found
)
8019 = tree_cons (arglist
, t
,
8020 DECL_TEMPLATE_INSTANTIATIONS (found
));
8022 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
8023 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8024 /* Now that the type has been registered on the instantiations
8025 list, we set up the enumerators. Because the enumeration
8026 constants may involve the enumeration type itself, we make
8027 sure to register the type first, and then create the
8028 constants. That way, doing tsubst_expr for the enumeration
8029 constants won't result in recursive calls here; we'll find
8030 the instantiation and exit above. */
8031 tsubst_enum (template_type
, t
, arglist
);
8033 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
8034 /* If the type makes use of template parameters, the
8035 code that generates debugging information will crash. */
8036 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
8038 /* Possibly limit visibility based on template args. */
8039 TREE_PUBLIC (type_decl
) = 1;
8040 determine_visibility (type_decl
);
8042 inherit_targ_abi_tags (t
);
8048 /* Wrapper for lookup_template_class_1. */
8051 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
8052 int entering_scope
, tsubst_flags_t complain
)
8055 timevar_push (TV_TEMPLATE_INST
);
8056 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
8057 entering_scope
, complain
);
8058 timevar_pop (TV_TEMPLATE_INST
);
8062 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST.
8063 The type of the expression is the unknown_type_node since the
8064 template-id could refer to an explicit or partial specialization. */
8067 lookup_template_variable (tree templ
, tree arglist
)
8069 tree type
= unknown_type_node
;
8070 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
8073 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
8076 finish_template_variable (tree var
)
8078 tree templ
= TREE_OPERAND (var
, 0);
8080 tree arglist
= TREE_OPERAND (var
, 1);
8081 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
8082 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
8084 tree parms
= DECL_TEMPLATE_PARMS (templ
);
8085 tsubst_flags_t complain
= tf_warning_or_error
;
8086 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
8088 /*use_default*/true);
8090 return instantiate_template (templ
, arglist
, complain
);
8097 /* True when we should also visit template parameters that occur in
8098 non-deduced contexts. */
8099 bool include_nondeduced_p
;
8100 hash_set
<tree
> *visited
;
8103 /* Called from for_each_template_parm via walk_tree. */
8106 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
8109 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
8110 tree_fn_t fn
= pfd
->fn
;
8111 void *data
= pfd
->data
;
8114 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
8115 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
8116 pfd
->include_nondeduced_p
))
8117 return error_mark_node
;
8119 switch (TREE_CODE (t
))
8122 if (TYPE_PTRMEMFUNC_P (t
))
8128 if (!TYPE_TEMPLATE_INFO (t
))
8130 else if (for_each_template_parm (TYPE_TI_ARGS (t
),
8131 fn
, data
, pfd
->visited
,
8132 pfd
->include_nondeduced_p
))
8133 return error_mark_node
;
8137 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
8138 fn
, data
, pfd
->visited
,
8139 pfd
->include_nondeduced_p
)
8140 || for_each_template_parm (TYPE_MAX_VALUE (t
),
8141 fn
, data
, pfd
->visited
,
8142 pfd
->include_nondeduced_p
))
8143 return error_mark_node
;
8147 /* Since we're not going to walk subtrees, we have to do this
8149 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
8150 pfd
->visited
, pfd
->include_nondeduced_p
))
8151 return error_mark_node
;
8155 /* Check the return type. */
8156 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
8157 pfd
->include_nondeduced_p
))
8158 return error_mark_node
;
8160 /* Check the parameter types. Since default arguments are not
8161 instantiated until they are needed, the TYPE_ARG_TYPES may
8162 contain expressions that involve template parameters. But,
8163 no-one should be looking at them yet. And, once they're
8164 instantiated, they don't contain template parameters, so
8165 there's no point in looking at them then, either. */
8169 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
8170 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
8171 pfd
->visited
, pfd
->include_nondeduced_p
))
8172 return error_mark_node
;
8174 /* Since we've already handled the TYPE_ARG_TYPES, we don't
8175 want walk_tree walking into them itself. */
8181 case UNDERLYING_TYPE
:
8182 if (pfd
->include_nondeduced_p
8183 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
8185 pfd
->include_nondeduced_p
))
8186 return error_mark_node
;
8191 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
8192 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
8193 pfd
->visited
, pfd
->include_nondeduced_p
))
8194 return error_mark_node
;
8199 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
8200 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
8201 pfd
->visited
, pfd
->include_nondeduced_p
))
8202 return error_mark_node
;
8203 if (DECL_CONTEXT (t
)
8204 && pfd
->include_nondeduced_p
8205 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
8206 pfd
->visited
, pfd
->include_nondeduced_p
))
8207 return error_mark_node
;
8210 case BOUND_TEMPLATE_TEMPLATE_PARM
:
8211 /* Record template parameters such as `T' inside `TT<T>'. */
8212 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
8213 pfd
->include_nondeduced_p
))
8214 return error_mark_node
;
8217 case TEMPLATE_TEMPLATE_PARM
:
8218 case TEMPLATE_TYPE_PARM
:
8219 case TEMPLATE_PARM_INDEX
:
8220 if (fn
&& (*fn
)(t
, data
))
8221 return error_mark_node
;
8223 return error_mark_node
;
8227 /* A template template parameter is encountered. */
8228 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
8229 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
8230 pfd
->include_nondeduced_p
))
8231 return error_mark_node
;
8233 /* Already substituted template template parameter */
8239 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
8241 pfd
->include_nondeduced_p
))
8242 return error_mark_node
;
8246 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
8247 && pfd
->include_nondeduced_p
8248 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
8249 (TREE_TYPE (t
)), fn
, data
,
8250 pfd
->visited
, pfd
->include_nondeduced_p
))
8251 return error_mark_node
;
8256 /* If there's no type, then this thing must be some expression
8257 involving template parameters. */
8258 if (!fn
&& !TREE_TYPE (t
))
8259 return error_mark_node
;
8264 case IMPLICIT_CONV_EXPR
:
8265 case REINTERPRET_CAST_EXPR
:
8266 case CONST_CAST_EXPR
:
8267 case STATIC_CAST_EXPR
:
8268 case DYNAMIC_CAST_EXPR
:
8272 case PSEUDO_DTOR_EXPR
:
8274 return error_mark_node
;
8281 /* We didn't find any template parameters we liked. */
8285 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8286 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8287 call FN with the parameter and the DATA.
8288 If FN returns nonzero, the iteration is terminated, and
8289 for_each_template_parm returns 1. Otherwise, the iteration
8290 continues. If FN never returns a nonzero value, the value
8291 returned by for_each_template_parm is 0. If FN is NULL, it is
8292 considered to be the function which always returns 1.
8294 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8295 parameters that occur in non-deduced contexts. When false, only
8296 visits those template parameters that can be deduced. */
8299 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
8300 hash_set
<tree
> *visited
,
8301 bool include_nondeduced_p
)
8303 struct pair_fn_data pfd
;
8309 pfd
.include_nondeduced_p
= include_nondeduced_p
;
8311 /* Walk the tree. (Conceptually, we would like to walk without
8312 duplicates, but for_each_template_parm_r recursively calls
8313 for_each_template_parm, so we would need to reorganize a fair
8314 bit to use walk_tree_without_duplicates, so we keep our own
8317 pfd
.visited
= visited
;
8319 pfd
.visited
= new hash_set
<tree
>;
8320 result
= cp_walk_tree (&t
,
8321 for_each_template_parm_r
,
8323 pfd
.visited
) != NULL_TREE
;
8335 /* Returns true if T depends on any template parameter. */
8338 uses_template_parms (tree t
)
8344 int saved_processing_template_decl
;
8346 saved_processing_template_decl
= processing_template_decl
;
8347 if (!saved_processing_template_decl
)
8348 processing_template_decl
= 1;
8350 dependent_p
= dependent_type_p (t
);
8351 else if (TREE_CODE (t
) == TREE_VEC
)
8352 dependent_p
= any_dependent_template_arguments_p (t
);
8353 else if (TREE_CODE (t
) == TREE_LIST
)
8354 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
8355 || uses_template_parms (TREE_CHAIN (t
)));
8356 else if (TREE_CODE (t
) == TYPE_DECL
)
8357 dependent_p
= dependent_type_p (TREE_TYPE (t
));
8360 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
8361 || TREE_CODE (t
) == OVERLOAD
8364 || TREE_CODE (t
) == TRAIT_EXPR
8365 || TREE_CODE (t
) == CONSTRUCTOR
8366 || CONSTANT_CLASS_P (t
))
8367 dependent_p
= (type_dependent_expression_p (t
)
8368 || value_dependent_expression_p (t
));
8371 gcc_assert (t
== error_mark_node
);
8372 dependent_p
= false;
8375 processing_template_decl
= saved_processing_template_decl
;
8380 /* Returns true iff current_function_decl is an incompletely instantiated
8381 template. Useful instead of processing_template_decl because the latter
8382 is set to 0 during instantiate_non_dependent_expr. */
8385 in_template_function (void)
8387 tree fn
= current_function_decl
;
8389 ++processing_template_decl
;
8390 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
8391 && DECL_TEMPLATE_INFO (fn
)
8392 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
8393 --processing_template_decl
;
8397 /* Returns true if T depends on any template parameter with level LEVEL. */
8400 uses_template_parms_level (tree t
, int level
)
8402 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
8403 /*include_nondeduced_p=*/true);
8406 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8407 ill-formed translation unit, i.e. a variable or function that isn't
8408 usable in a constant expression. */
8411 neglectable_inst_p (tree d
)
8414 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
8415 : decl_maybe_constant_var_p (d
)));
8418 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8419 neglectable and instantiated from within an erroneous instantiation. */
8422 limit_bad_template_recursion (tree decl
)
8424 struct tinst_level
*lev
= current_tinst_level
;
8425 int errs
= errorcount
+ sorrycount
;
8426 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
8429 for (; lev
; lev
= lev
->next
)
8430 if (neglectable_inst_p (lev
->decl
))
8433 return (lev
&& errs
> lev
->errors
);
8436 static int tinst_depth
;
8437 extern int max_tinst_depth
;
8440 static GTY(()) struct tinst_level
*last_error_tinst_level
;
8442 /* We're starting to instantiate D; record the template instantiation context
8443 for diagnostics and to restore it later. */
8446 push_tinst_level (tree d
)
8448 return push_tinst_level_loc (d
, input_location
);
8451 /* We're starting to instantiate D; record the template instantiation context
8452 at LOC for diagnostics and to restore it later. */
8455 push_tinst_level_loc (tree d
, location_t loc
)
8457 struct tinst_level
*new_level
;
8459 if (tinst_depth
>= max_tinst_depth
)
8461 fatal_error (input_location
,
8462 "template instantiation depth exceeds maximum of %d"
8463 " (use -ftemplate-depth= to increase the maximum)",
8468 /* If the current instantiation caused problems, don't let it instantiate
8469 anything else. Do allow deduction substitution and decls usable in
8470 constant expressions. */
8471 if (limit_bad_template_recursion (d
))
8474 new_level
= ggc_alloc
<tinst_level
> ();
8475 new_level
->decl
= d
;
8476 new_level
->locus
= loc
;
8477 new_level
->errors
= errorcount
+sorrycount
;
8478 new_level
->in_system_header_p
= in_system_header_at (input_location
);
8479 new_level
->next
= current_tinst_level
;
8480 current_tinst_level
= new_level
;
8483 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
8484 depth_reached
= tinst_depth
;
8489 /* We're done instantiating this template; return to the instantiation
8493 pop_tinst_level (void)
8495 /* Restore the filename and line number stashed away when we started
8496 this instantiation. */
8497 input_location
= current_tinst_level
->locus
;
8498 current_tinst_level
= current_tinst_level
->next
;
8502 /* We're instantiating a deferred template; restore the template
8503 instantiation context in which the instantiation was requested, which
8504 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8507 reopen_tinst_level (struct tinst_level
*level
)
8509 struct tinst_level
*t
;
8512 for (t
= level
; t
; t
= t
->next
)
8515 current_tinst_level
= level
;
8517 if (current_tinst_level
)
8518 current_tinst_level
->errors
= errorcount
+sorrycount
;
8522 /* Returns the TINST_LEVEL which gives the original instantiation
8525 struct tinst_level
*
8526 outermost_tinst_level (void)
8528 struct tinst_level
*level
= current_tinst_level
;
8531 level
= level
->next
;
8535 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8536 vector of template arguments, as for tsubst.
8538 Returns an appropriate tsubst'd friend declaration. */
8541 tsubst_friend_function (tree decl
, tree args
)
8545 if (TREE_CODE (decl
) == FUNCTION_DECL
8546 && DECL_TEMPLATE_INSTANTIATION (decl
)
8547 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
8548 /* This was a friend declared with an explicit template
8549 argument list, e.g.:
8553 to indicate that f was a template instantiation, not a new
8554 function declaration. Now, we have to figure out what
8555 instantiation of what template. */
8557 tree template_id
, arglist
, fns
;
8560 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
8562 /* Friend functions are looked up in the containing namespace scope.
8563 We must enter that scope, to avoid finding member functions of the
8564 current class with same name. */
8565 push_nested_namespace (ns
);
8566 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
8567 tf_warning_or_error
, NULL_TREE
,
8568 /*integral_constant_expression_p=*/false);
8569 pop_nested_namespace (ns
);
8570 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
8571 tf_warning_or_error
, NULL_TREE
);
8572 template_id
= lookup_template_function (fns
, arglist
);
8574 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8575 tmpl
= determine_specialization (template_id
, new_friend
,
8577 /*need_member_template=*/0,
8578 TREE_VEC_LENGTH (args
),
8580 return instantiate_template (tmpl
, new_args
, tf_error
);
8583 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8585 /* The NEW_FRIEND will look like an instantiation, to the
8586 compiler, but is not an instantiation from the point of view of
8587 the language. For example, we might have had:
8589 template <class T> struct S {
8590 template <class U> friend void f(T, U);
8593 Then, in S<int>, template <class U> void f(int, U) is not an
8594 instantiation of anything. */
8595 if (new_friend
== error_mark_node
)
8596 return error_mark_node
;
8598 DECL_USE_TEMPLATE (new_friend
) = 0;
8599 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8601 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
8602 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
8603 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
8606 /* The mangled name for the NEW_FRIEND is incorrect. The function
8607 is not a template instantiation and should not be mangled like
8608 one. Therefore, we forget the mangling here; we'll recompute it
8609 later if we need it. */
8610 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
8612 SET_DECL_RTL (new_friend
, NULL
);
8613 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
8616 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
8619 tree new_friend_template_info
;
8620 tree new_friend_result_template_info
;
8622 int new_friend_is_defn
;
8624 /* We must save some information from NEW_FRIEND before calling
8625 duplicate decls since that function will free NEW_FRIEND if
8627 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
8628 new_friend_is_defn
=
8629 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8630 (template_for_substitution (new_friend
)))
8632 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
8634 /* This declaration is a `primary' template. */
8635 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
8637 new_friend_result_template_info
8638 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
8641 new_friend_result_template_info
= NULL_TREE
;
8643 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8644 if (new_friend_is_defn
)
8645 DECL_INITIAL (new_friend
) = error_mark_node
;
8647 /* Inside pushdecl_namespace_level, we will push into the
8648 current namespace. However, the friend function should go
8649 into the namespace of the template. */
8650 ns
= decl_namespace_context (new_friend
);
8651 push_nested_namespace (ns
);
8652 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
8653 pop_nested_namespace (ns
);
8655 if (old_decl
== error_mark_node
)
8656 return error_mark_node
;
8658 if (old_decl
!= new_friend
)
8660 /* This new friend declaration matched an existing
8661 declaration. For example, given:
8663 template <class T> void f(T);
8664 template <class U> class C {
8665 template <class T> friend void f(T) {}
8668 the friend declaration actually provides the definition
8669 of `f', once C has been instantiated for some type. So,
8670 old_decl will be the out-of-class template declaration,
8671 while new_friend is the in-class definition.
8673 But, if `f' was called before this point, the
8674 instantiation of `f' will have DECL_TI_ARGS corresponding
8675 to `T' but not to `U', references to which might appear
8676 in the definition of `f'. Previously, the most general
8677 template for an instantiation of `f' was the out-of-class
8678 version; now it is the in-class version. Therefore, we
8679 run through all specialization of `f', adding to their
8680 DECL_TI_ARGS appropriately. In particular, they need a
8681 new set of outer arguments, corresponding to the
8682 arguments for this class instantiation.
8684 The same situation can arise with something like this:
8687 template <class T> class C {
8691 when `C<int>' is instantiated. Now, `f(int)' is defined
8694 if (!new_friend_is_defn
)
8695 /* On the other hand, if the in-class declaration does
8696 *not* provide a definition, then we don't want to alter
8697 existing definitions. We can just leave everything
8702 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
8703 tree new_args
= TI_ARGS (new_friend_template_info
);
8705 /* Overwrite whatever template info was there before, if
8706 any, with the new template information pertaining to
8708 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
8710 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
8712 /* We should have called reregister_specialization in
8714 gcc_assert (retrieve_specialization (new_template
,
8718 /* Instantiate it if the global has already been used. */
8719 if (DECL_ODR_USED (old_decl
))
8720 instantiate_decl (old_decl
, /*defer_ok=*/true,
8721 /*expl_inst_class_mem_p=*/false);
8727 /* Indicate that the old function template is a partial
8729 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
8730 = new_friend_result_template_info
;
8732 gcc_assert (new_template
8733 == most_general_template (new_template
));
8734 gcc_assert (new_template
!= old_decl
);
8736 /* Reassign any specializations already in the hash table
8737 to the new more general template, and add the
8738 additional template args. */
8739 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
8743 tree spec
= TREE_VALUE (t
);
8746 elt
.tmpl
= old_decl
;
8747 elt
.args
= DECL_TI_ARGS (spec
);
8748 elt
.spec
= NULL_TREE
;
8750 decl_specializations
->remove_elt (&elt
);
8753 = add_outermost_template_args (new_args
,
8754 DECL_TI_ARGS (spec
));
8756 register_specialization
8757 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
8760 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
8764 /* The information from NEW_FRIEND has been merged into OLD_DECL
8765 by duplicate_decls. */
8766 new_friend
= old_decl
;
8771 tree context
= DECL_CONTEXT (new_friend
);
8775 template <class T> class C {
8776 template <class U> friend void C1<U>::f (); // case 1
8777 friend void C2<T>::f (); // case 2
8779 we only need to make sure CONTEXT is a complete type for
8780 case 2. To distinguish between the two cases, we note that
8781 CONTEXT of case 1 remains dependent type after tsubst while
8782 this isn't true for case 2. */
8783 ++processing_template_decl
;
8784 dependent_p
= dependent_type_p (context
);
8785 --processing_template_decl
;
8788 && !complete_type_or_else (context
, NULL_TREE
))
8789 return error_mark_node
;
8791 if (COMPLETE_TYPE_P (context
))
8793 tree fn
= new_friend
;
8794 /* do_friend adds the TEMPLATE_DECL for any member friend
8795 template even if it isn't a member template, i.e.
8796 template <class T> friend A<T>::f();
8797 Look through it in that case. */
8798 if (TREE_CODE (fn
) == TEMPLATE_DECL
8799 && !PRIMARY_TEMPLATE_P (fn
))
8800 fn
= DECL_TEMPLATE_RESULT (fn
);
8801 /* Check to see that the declaration is really present, and,
8802 possibly obtain an improved declaration. */
8803 fn
= check_classfn (context
, fn
, NULL_TREE
);
8813 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8814 template arguments, as for tsubst.
8816 Returns an appropriate tsubst'd friend type or error_mark_node on
8820 tsubst_friend_class (tree friend_tmpl
, tree args
)
8826 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
8828 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
8829 return TREE_TYPE (t
);
8832 context
= CP_DECL_CONTEXT (friend_tmpl
);
8834 if (context
!= global_namespace
)
8836 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8837 push_nested_namespace (context
);
8839 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
8842 /* Look for a class template declaration. We look for hidden names
8843 because two friend declarations of the same template are the
8844 same. For example, in:
8847 template <typename> friend class F;
8849 template <typename> struct B {
8850 template <typename> friend class F;
8853 both F templates are the same. */
8854 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
8855 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
8857 /* But, if we don't find one, it might be because we're in a
8858 situation like this:
8866 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8867 for `S<int>', not the TEMPLATE_DECL. */
8868 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
8870 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
8871 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
8874 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
8876 /* The friend template has already been declared. Just
8877 check to see that the declarations match, and install any new
8878 default parameters. We must tsubst the default parameters,
8879 of course. We only need the innermost template parameters
8880 because that is all that redeclare_class_template will look
8882 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
8883 > TMPL_ARGS_DEPTH (args
))
8886 location_t saved_input_location
;
8887 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
8888 args
, tf_warning_or_error
);
8890 saved_input_location
= input_location
;
8891 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
8892 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
8893 input_location
= saved_input_location
;
8897 friend_type
= TREE_TYPE (tmpl
);
8901 /* The friend template has not already been declared. In this
8902 case, the instantiation of the template class will cause the
8903 injection of this template into the global scope. */
8904 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
8905 if (tmpl
== error_mark_node
)
8906 return error_mark_node
;
8908 /* The new TMPL is not an instantiation of anything, so we
8909 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8910 the new type because that is supposed to be the corresponding
8911 template decl, i.e., TMPL. */
8912 DECL_USE_TEMPLATE (tmpl
) = 0;
8913 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
8914 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
8915 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
8916 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
8918 /* Inject this template into the global scope. */
8919 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
8922 if (context
!= global_namespace
)
8924 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8925 pop_nested_namespace (context
);
8927 pop_nested_class ();
8933 /* Returns zero if TYPE cannot be completed later due to circularity.
8934 Otherwise returns one. */
8937 can_complete_type_without_circularity (tree type
)
8939 if (type
== NULL_TREE
|| type
== error_mark_node
)
8941 else if (COMPLETE_TYPE_P (type
))
8943 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
8944 return can_complete_type_without_circularity (TREE_TYPE (type
));
8945 else if (CLASS_TYPE_P (type
)
8946 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
8952 static tree
tsubst_omp_clauses (tree
, bool, tree
, tsubst_flags_t
, tree
);
8954 /* Apply any attributes which had to be deferred until instantiation
8955 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8956 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8959 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
8960 tree args
, tsubst_flags_t complain
, tree in_decl
)
8962 tree last_dep
= NULL_TREE
;
8966 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
8967 if (ATTR_IS_DEPENDENT (t
))
8970 attributes
= copy_list (attributes
);
8974 if (DECL_P (*decl_p
))
8976 if (TREE_TYPE (*decl_p
) == error_mark_node
)
8978 p
= &DECL_ATTRIBUTES (*decl_p
);
8981 p
= &TYPE_ATTRIBUTES (*decl_p
);
8985 tree late_attrs
= NULL_TREE
;
8986 tree
*q
= &late_attrs
;
8988 for (*p
= attributes
; *p
; )
8991 if (ATTR_IS_DEPENDENT (t
))
8993 *p
= TREE_CHAIN (t
);
8994 TREE_CHAIN (t
) = NULL_TREE
;
8995 if ((flag_openmp
|| flag_cilkplus
)
8996 && is_attribute_p ("omp declare simd",
8997 get_attribute_name (t
))
9000 tree clauses
= TREE_VALUE (TREE_VALUE (t
));
9001 clauses
= tsubst_omp_clauses (clauses
, true, args
,
9003 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
9004 clauses
= finish_omp_clauses (clauses
);
9005 tree parms
= DECL_ARGUMENTS (*decl_p
);
9007 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
9009 TREE_VALUE (TREE_VALUE (t
)) = clauses
;
9011 TREE_VALUE (t
) = NULL_TREE
;
9013 /* If the first attribute argument is an identifier, don't
9014 pass it through tsubst. Attributes like mode, format,
9015 cleanup and several target specific attributes expect it
9017 else if (attribute_takes_identifier_p (get_attribute_name (t
))
9021 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
9023 /*integral_constant_expression_p=*/false);
9024 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
9026 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
9029 else if (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
)))
9031 /* An attribute pack expansion. */
9032 tree purp
= TREE_PURPOSE (t
);
9033 tree pack
= (tsubst_pack_expansion
9034 (TREE_VALUE (t
), args
, complain
, in_decl
));
9035 int len
= TREE_VEC_LENGTH (pack
);
9036 for (int i
= 0; i
< len
; ++i
)
9038 tree elt
= TREE_VEC_ELT (pack
, i
);
9039 *q
= build_tree_list (purp
, elt
);
9040 q
= &TREE_CHAIN (*q
);
9046 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
9047 /*integral_constant_expression_p=*/false);
9049 q
= &TREE_CHAIN (t
);
9052 p
= &TREE_CHAIN (t
);
9055 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
9059 /* Perform (or defer) access check for typedefs that were referenced
9060 from within the template TMPL code.
9061 This is a subroutine of instantiate_decl and instantiate_class_template.
9062 TMPL is the template to consider and TARGS is the list of arguments of
9066 perform_typedefs_access_check (tree tmpl
, tree targs
)
9068 location_t saved_location
;
9070 qualified_typedef_usage_t
*iter
;
9073 || (!CLASS_TYPE_P (tmpl
)
9074 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
9077 saved_location
= input_location
;
9078 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
9080 tree type_decl
= iter
->typedef_decl
;
9081 tree type_scope
= iter
->context
;
9083 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
9086 if (uses_template_parms (type_decl
))
9087 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
9088 if (uses_template_parms (type_scope
))
9089 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
9091 /* Make access check error messages point to the location
9092 of the use of the typedef. */
9093 input_location
= iter
->locus
;
9094 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
9095 type_decl
, type_decl
,
9096 tf_warning_or_error
);
9098 input_location
= saved_location
;
9102 instantiate_class_template_1 (tree type
)
9104 tree templ
, args
, pattern
, t
, member
;
9108 unsigned int saved_maximum_field_alignment
;
9111 if (type
== error_mark_node
)
9112 return error_mark_node
;
9114 if (COMPLETE_OR_OPEN_TYPE_P (type
)
9115 || uses_template_parms (type
))
9118 /* Figure out which template is being instantiated. */
9119 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
9120 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
9122 /* Determine what specialization of the original template to
9124 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
9125 if (t
== error_mark_node
)
9127 TYPE_BEING_DEFINED (type
) = 1;
9128 return error_mark_node
;
9132 /* This TYPE is actually an instantiation of a partial
9133 specialization. We replace the innermost set of ARGS with
9134 the arguments appropriate for substitution. For example,
9137 template <class T> struct S {};
9138 template <class T> struct S<T*> {};
9140 and supposing that we are instantiating S<int*>, ARGS will
9141 presently be {int*} -- but we need {int}. */
9142 pattern
= TREE_TYPE (t
);
9143 args
= TREE_PURPOSE (t
);
9147 pattern
= TREE_TYPE (templ
);
9148 args
= CLASSTYPE_TI_ARGS (type
);
9151 /* If the template we're instantiating is incomplete, then clearly
9152 there's nothing we can do. */
9153 if (!COMPLETE_TYPE_P (pattern
))
9156 /* If we've recursively instantiated too many templates, stop. */
9157 if (! push_tinst_level (type
))
9160 /* Now we're really doing the instantiation. Mark the type as in
9161 the process of being defined. */
9162 TYPE_BEING_DEFINED (type
) = 1;
9164 /* We may be in the middle of deferred access check. Disable
9166 push_deferring_access_checks (dk_no_deferred
);
9168 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
9169 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
9170 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
9171 fn_context
= error_mark_node
;
9173 push_to_top_level ();
9174 /* Use #pragma pack from the template context. */
9175 saved_maximum_field_alignment
= maximum_field_alignment
;
9176 maximum_field_alignment
= TYPE_PRECISION (pattern
);
9178 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
9180 /* Set the input location to the most specialized template definition.
9181 This is needed if tsubsting causes an error. */
9182 typedecl
= TYPE_MAIN_DECL (pattern
);
9183 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
9184 DECL_SOURCE_LOCATION (typedecl
);
9186 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
9187 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
9188 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
9189 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
9190 if (ANON_AGGR_TYPE_P (pattern
))
9191 SET_ANON_AGGR_TYPE_P (type
);
9192 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
9194 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
9195 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
9196 /* Adjust visibility for template arguments. */
9197 determine_visibility (TYPE_MAIN_DECL (type
));
9199 if (CLASS_TYPE_P (type
))
9200 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
9202 pbinfo
= TYPE_BINFO (pattern
);
9204 /* We should never instantiate a nested class before its enclosing
9205 class; we need to look up the nested class by name before we can
9206 instantiate it, and that lookup should instantiate the enclosing
9208 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
9209 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
9211 base_list
= NULL_TREE
;
9212 if (BINFO_N_BASE_BINFOS (pbinfo
))
9218 /* We must enter the scope containing the type, as that is where
9219 the accessibility of types named in dependent bases are
9221 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
9223 /* Substitute into each of the bases to determine the actual
9225 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
9228 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
9229 tree expanded_bases
= NULL_TREE
;
9232 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
9235 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
9236 args
, tf_error
, NULL_TREE
);
9237 if (expanded_bases
== error_mark_node
)
9240 len
= TREE_VEC_LENGTH (expanded_bases
);
9243 for (idx
= 0; idx
< len
; idx
++)
9246 /* Extract the already-expanded base class. */
9247 base
= TREE_VEC_ELT (expanded_bases
, idx
);
9249 /* Substitute to figure out the base class. */
9250 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
9253 if (base
== error_mark_node
)
9256 base_list
= tree_cons (access
, base
, base_list
);
9257 if (BINFO_VIRTUAL_P (pbase_binfo
))
9258 TREE_TYPE (base_list
) = integer_type_node
;
9262 /* The list is now in reverse order; correct that. */
9263 base_list
= nreverse (base_list
);
9266 pop_scope (pushed_scope
);
9268 /* Now call xref_basetypes to set up all the base-class
9270 xref_basetypes (type
, base_list
);
9272 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
9273 (int) ATTR_FLAG_TYPE_IN_PLACE
,
9274 args
, tf_error
, NULL_TREE
);
9275 fixup_attribute_variants (type
);
9277 /* Now that our base classes are set up, enter the scope of the
9278 class, so that name lookups into base classes, etc. will work
9279 correctly. This is precisely analogous to what we do in
9280 begin_class_definition when defining an ordinary non-template
9281 class, except we also need to push the enclosing classes. */
9282 push_nested_class (type
);
9284 /* Now members are processed in the order of declaration. */
9285 for (member
= CLASSTYPE_DECL_LIST (pattern
);
9286 member
; member
= TREE_CHAIN (member
))
9288 tree t
= TREE_VALUE (member
);
9290 if (TREE_PURPOSE (member
))
9294 /* Build new CLASSTYPE_NESTED_UTDS. */
9297 bool class_template_p
;
9299 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
9300 && TYPE_LANG_SPECIFIC (t
)
9301 && CLASSTYPE_IS_TEMPLATE (t
));
9302 /* If the member is a class template, then -- even after
9303 substitution -- there may be dependent types in the
9304 template argument list for the class. We increment
9305 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
9306 that function will assume that no types are dependent
9307 when outside of a template. */
9308 if (class_template_p
)
9309 ++processing_template_decl
;
9310 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
9311 if (class_template_p
)
9312 --processing_template_decl
;
9313 if (newtag
== error_mark_node
)
9316 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
9318 tree name
= TYPE_IDENTIFIER (t
);
9320 if (class_template_p
)
9321 /* Unfortunately, lookup_template_class sets
9322 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
9323 instantiation (i.e., for the type of a member
9324 template class nested within a template class.)
9325 This behavior is required for
9326 maybe_process_partial_specialization to work
9327 correctly, but is not accurate in this case;
9328 the TAG is not an instantiation of anything.
9329 (The corresponding TEMPLATE_DECL is an
9330 instantiation, but the TYPE is not.) */
9331 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
9333 /* Now, we call pushtag to put this NEWTAG into the scope of
9334 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
9335 pushtag calling push_template_decl. We don't have to do
9336 this for enums because it will already have been done in
9339 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
9340 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
9343 else if (DECL_DECLARES_FUNCTION_P (t
))
9345 /* Build new TYPE_METHODS. */
9348 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9349 ++processing_template_decl
;
9350 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
9351 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9352 --processing_template_decl
;
9353 set_current_access_from_decl (r
);
9354 finish_member_declaration (r
);
9355 /* Instantiate members marked with attribute used. */
9356 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
9358 if (TREE_CODE (r
) == FUNCTION_DECL
9359 && DECL_OMP_DECLARE_REDUCTION_P (r
))
9360 cp_check_omp_declare_reduction (r
);
9362 else if (DECL_CLASS_TEMPLATE_P (t
)
9363 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
9364 /* A closure type for a lambda in a default argument for a
9365 member template. Ignore it; it will be instantiated with
9366 the default argument. */;
9369 /* Build new TYPE_FIELDS. */
9370 if (TREE_CODE (t
) == STATIC_ASSERT
)
9374 ++c_inhibit_evaluation_warnings
;
9376 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
9377 tf_warning_or_error
, NULL_TREE
,
9378 /*integral_constant_expression_p=*/true);
9379 --c_inhibit_evaluation_warnings
;
9381 finish_static_assert (condition
,
9382 STATIC_ASSERT_MESSAGE (t
),
9383 STATIC_ASSERT_SOURCE_LOCATION (t
),
9386 else if (TREE_CODE (t
) != CONST_DECL
)
9389 tree vec
= NULL_TREE
;
9392 /* The file and line for this declaration, to
9393 assist in error message reporting. Since we
9394 called push_tinst_level above, we don't need to
9396 input_location
= DECL_SOURCE_LOCATION (t
);
9398 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9399 ++processing_template_decl
;
9400 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
9401 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9402 --processing_template_decl
;
9404 if (TREE_CODE (r
) == TREE_VEC
)
9406 /* A capture pack became multiple fields. */
9408 len
= TREE_VEC_LENGTH (vec
);
9411 for (int i
= 0; i
< len
; ++i
)
9414 r
= TREE_VEC_ELT (vec
, i
);
9419 [t]he initialization (and any associated
9420 side-effects) of a static data member does
9421 not occur unless the static data member is
9422 itself used in a way that requires the
9423 definition of the static data member to
9426 Therefore, we do not substitute into the
9427 initialized for the static data member here. */
9428 finish_static_data_member_decl
9431 /*init_const_expr_p=*/false,
9432 /*asmspec_tree=*/NULL_TREE
,
9434 /* Instantiate members marked with attribute used. */
9435 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
9438 else if (TREE_CODE (r
) == FIELD_DECL
)
9440 /* Determine whether R has a valid type and can be
9441 completed later. If R is invalid, then its type
9442 is replaced by error_mark_node. */
9443 tree rtype
= TREE_TYPE (r
);
9444 if (can_complete_type_without_circularity (rtype
))
9445 complete_type (rtype
);
9447 if (!COMPLETE_TYPE_P (rtype
))
9449 cxx_incomplete_type_error (r
, rtype
);
9450 TREE_TYPE (r
) = error_mark_node
;
9454 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9455 such a thing will already have been added to the field
9456 list by tsubst_enum in finish_member_declaration in the
9457 CLASSTYPE_NESTED_UTDS case above. */
9458 if (!(TREE_CODE (r
) == TYPE_DECL
9459 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
9460 && DECL_ARTIFICIAL (r
)))
9462 set_current_access_from_decl (r
);
9463 finish_member_declaration (r
);
9471 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
9472 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9474 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9476 tree friend_type
= t
;
9477 bool adjust_processing_template_decl
= false;
9479 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9481 /* template <class T> friend class C; */
9482 friend_type
= tsubst_friend_class (friend_type
, args
);
9483 adjust_processing_template_decl
= true;
9485 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
9487 /* template <class T> friend class C::D; */
9488 friend_type
= tsubst (friend_type
, args
,
9489 tf_warning_or_error
, NULL_TREE
);
9490 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9491 friend_type
= TREE_TYPE (friend_type
);
9492 adjust_processing_template_decl
= true;
9494 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
9495 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
9497 /* This could be either
9501 when dependent_type_p is false or
9503 template <class U> friend class T::C;
9506 friend_type
= tsubst (friend_type
, args
,
9507 tf_warning_or_error
, NULL_TREE
);
9508 /* Bump processing_template_decl for correct
9509 dependent_type_p calculation. */
9510 ++processing_template_decl
;
9511 if (dependent_type_p (friend_type
))
9512 adjust_processing_template_decl
= true;
9513 --processing_template_decl
;
9515 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
9516 && hidden_name_p (TYPE_NAME (friend_type
)))
9520 where C hasn't been declared yet. Let's lookup name
9521 from namespace scope directly, bypassing any name that
9522 come from dependent base class. */
9523 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
9525 /* The call to xref_tag_from_type does injection for friend
9527 push_nested_namespace (ns
);
9529 xref_tag_from_type (friend_type
, NULL_TREE
,
9530 /*tag_scope=*/ts_current
);
9531 pop_nested_namespace (ns
);
9533 else if (uses_template_parms (friend_type
))
9534 /* friend class C<T>; */
9535 friend_type
= tsubst (friend_type
, args
,
9536 tf_warning_or_error
, NULL_TREE
);
9541 where C is already declared or
9543 friend class C<int>;
9545 We don't have to do anything in these cases. */
9547 if (adjust_processing_template_decl
)
9548 /* Trick make_friend_class into realizing that the friend
9549 we're adding is a template, not an ordinary class. It's
9550 important that we use make_friend_class since it will
9551 perform some error-checking and output cross-reference
9553 ++processing_template_decl
;
9555 if (friend_type
!= error_mark_node
)
9556 make_friend_class (type
, friend_type
, /*complain=*/false);
9558 if (adjust_processing_template_decl
)
9559 --processing_template_decl
;
9563 /* Build new DECL_FRIENDLIST. */
9566 /* The file and line for this declaration, to
9567 assist in error message reporting. Since we
9568 called push_tinst_level above, we don't need to
9570 input_location
= DECL_SOURCE_LOCATION (t
);
9572 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9574 ++processing_template_decl
;
9575 push_deferring_access_checks (dk_no_check
);
9578 r
= tsubst_friend_function (t
, args
);
9579 add_friend (type
, r
, /*complain=*/false);
9580 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9582 pop_deferring_access_checks ();
9583 --processing_template_decl
;
9589 if (tree expr
= CLASSTYPE_LAMBDA_EXPR (type
))
9591 tree decl
= lambda_function (type
);
9594 if (!DECL_TEMPLATE_INFO (decl
)
9595 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl
)) != decl
)
9596 instantiate_decl (decl
, false, false);
9598 /* We need to instantiate the capture list from the template
9599 after we've instantiated the closure members, but before we
9600 consider adding the conversion op. Also keep any captures
9601 that may have been added during instantiation of the op(). */
9602 tree tmpl_expr
= CLASSTYPE_LAMBDA_EXPR (pattern
);
9604 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr
),
9605 args
, tf_warning_or_error
, NULL_TREE
,
9608 LAMBDA_EXPR_CAPTURE_LIST (expr
)
9609 = chainon (tmpl_cap
, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr
)));
9611 maybe_add_lambda_conv_op (type
);
9614 gcc_assert (errorcount
);
9617 /* Set the file and line number information to whatever is given for
9618 the class itself. This puts error messages involving generated
9619 implicit functions at a predictable point, and the same point
9620 that would be used for non-template classes. */
9621 input_location
= DECL_SOURCE_LOCATION (typedecl
);
9623 unreverse_member_declarations (type
);
9624 finish_struct_1 (type
);
9625 TYPE_BEING_DEFINED (type
) = 0;
9627 /* We don't instantiate default arguments for member functions. 14.7.1:
9629 The implicit instantiation of a class template specialization causes
9630 the implicit instantiation of the declarations, but not of the
9631 definitions or default arguments, of the class member functions,
9632 member classes, static data members and member templates.... */
9634 /* Some typedefs referenced from within the template code need to be access
9635 checked at template instantiation time, i.e now. These types were
9636 added to the template at parsing time. Let's get those and perform
9637 the access checks then. */
9638 perform_typedefs_access_check (pattern
, args
);
9639 perform_deferred_access_checks (tf_warning_or_error
);
9640 pop_nested_class ();
9641 maximum_field_alignment
= saved_maximum_field_alignment
;
9643 pop_from_top_level ();
9644 pop_deferring_access_checks ();
9647 /* The vtable for a template class can be emitted in any translation
9648 unit in which the class is instantiated. When there is no key
9649 method, however, finish_struct_1 will already have added TYPE to
9650 the keyed_classes list. */
9651 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
9652 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
9657 /* Wrapper for instantiate_class_template_1. */
9660 instantiate_class_template (tree type
)
9663 timevar_push (TV_TEMPLATE_INST
);
9664 ret
= instantiate_class_template_1 (type
);
9665 timevar_pop (TV_TEMPLATE_INST
);
9670 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9676 else if (TYPE_P (t
))
9677 r
= tsubst (t
, args
, complain
, in_decl
);
9680 if (!(complain
& tf_warning
))
9681 ++c_inhibit_evaluation_warnings
;
9682 r
= tsubst_expr (t
, args
, complain
, in_decl
,
9683 /*integral_constant_expression_p=*/true);
9684 if (!(complain
& tf_warning
))
9685 --c_inhibit_evaluation_warnings
;
9690 /* Given a function parameter pack TMPL_PARM and some function parameters
9691 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9692 and set *SPEC_P to point at the next point in the list. */
9695 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
9697 /* Collect all of the extra "packed" parameters into an
9701 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
9702 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
9703 tree spec_parm
= *spec_p
;
9706 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
9708 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
9711 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9712 parmvec
= make_tree_vec (len
);
9713 parmtypevec
= make_tree_vec (len
);
9714 spec_parm
= *spec_p
;
9715 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
9717 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
9718 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
9721 /* Build the argument packs. */
9722 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
9723 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
9724 TREE_TYPE (argpack
) = argtypepack
;
9725 *spec_p
= spec_parm
;
9730 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9731 NONTYPE_ARGUMENT_PACK. */
9734 make_fnparm_pack (tree spec_parm
)
9736 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
9739 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9743 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
9745 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
9746 if (i
>= TREE_VEC_LENGTH (vec
))
9748 return PACK_EXPANSION_P (TREE_VEC_ELT (vec
, i
));
9752 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9755 make_argument_pack_select (tree arg_pack
, unsigned index
)
9757 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
9759 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
9760 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9765 /* This is a subroutine of tsubst_pack_expansion.
9767 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9768 mechanism to store the (non complete list of) arguments of the
9769 substitution and return a non substituted pack expansion, in order
9770 to wait for when we have enough arguments to really perform the
9774 use_pack_expansion_extra_args_p (tree parm_packs
,
9778 /* If one pack has an expansion and another pack has a normal
9779 argument or if one pack has an empty argument and an another
9780 one hasn't then tsubst_pack_expansion cannot perform the
9781 substitution and need to fall back on the
9782 PACK_EXPANSION_EXTRA mechanism. */
9783 if (parm_packs
== NULL_TREE
)
9785 else if (has_empty_arg
)
9788 bool has_expansion_arg
= false;
9789 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
9791 bool has_non_expansion_arg
= false;
9792 for (tree parm_pack
= parm_packs
;
9794 parm_pack
= TREE_CHAIN (parm_pack
))
9796 tree arg
= TREE_VALUE (parm_pack
);
9798 if (argument_pack_element_is_expansion_p (arg
, i
))
9799 has_expansion_arg
= true;
9801 has_non_expansion_arg
= true;
9804 if (has_expansion_arg
&& has_non_expansion_arg
)
9810 /* [temp.variadic]/6 says that:
9812 The instantiation of a pack expansion [...]
9813 produces a list E1,E2, ..., En, where N is the number of elements
9814 in the pack expansion parameters.
9816 This subroutine of tsubst_pack_expansion produces one of these Ei.
9818 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9819 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9820 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9821 INDEX is the index 'i' of the element Ei to produce. ARGS,
9822 COMPLAIN, and IN_DECL are the same parameters as for the
9823 tsubst_pack_expansion function.
9825 The function returns the resulting Ei upon successful completion,
9828 Note that this function possibly modifies the ARGS parameter, so
9829 it's the responsibility of the caller to restore it. */
9832 gen_elem_of_pack_expansion_instantiation (tree pattern
,
9835 tree args
/* This parm gets
9837 tsubst_flags_t complain
,
9841 bool ith_elem_is_expansion
= false;
9843 /* For each parameter pack, change the substitution of the parameter
9844 pack to the ith argument in its argument pack, then expand the
9846 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
9848 tree parm
= TREE_PURPOSE (pack
);
9849 tree arg_pack
= TREE_VALUE (pack
);
9850 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
9852 ith_elem_is_expansion
|=
9853 argument_pack_element_is_expansion_p (arg_pack
, index
);
9855 /* Select the Ith argument from the pack. */
9856 if (TREE_CODE (parm
) == PARM_DECL
9857 || TREE_CODE (parm
) == FIELD_DECL
)
9861 aps
= make_argument_pack_select (arg_pack
, index
);
9862 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
9863 return error_mark_node
;
9864 register_local_specialization (aps
, parm
);
9867 aps
= retrieve_local_specialization (parm
);
9872 template_parm_level_and_index (parm
, &level
, &idx
);
9876 aps
= make_argument_pack_select (arg_pack
, index
);
9877 /* Update the corresponding argument. */
9878 TMPL_ARG (args
, level
, idx
) = aps
;
9881 /* Re-use the ARGUMENT_PACK_SELECT. */
9882 aps
= TMPL_ARG (args
, level
, idx
);
9884 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9887 /* Substitute into the PATTERN with the (possibly altered)
9889 if (pattern
== in_decl
)
9890 /* Expanding a fixed parameter pack from
9891 coerce_template_parameter_pack. */
9892 t
= tsubst_decl (pattern
, args
, complain
);
9893 else if (!TYPE_P (pattern
))
9894 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9895 /*integral_constant_expression_p=*/false);
9897 t
= tsubst (pattern
, args
, complain
, in_decl
);
9899 /* If the Ith argument pack element is a pack expansion, then
9900 the Ith element resulting from the substituting is going to
9901 be a pack expansion as well. */
9902 if (ith_elem_is_expansion
)
9903 t
= make_pack_expansion (t
);
9908 /* Substitute ARGS into T, which is an pack expansion
9909 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9910 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9911 (if only a partial substitution could be performed) or
9912 ERROR_MARK_NODE if there was an error. */
9914 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
9918 tree pack
, packs
= NULL_TREE
;
9919 bool unsubstituted_packs
= false;
9922 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
9923 bool need_local_specializations
= false;
9926 gcc_assert (PACK_EXPANSION_P (t
));
9927 pattern
= PACK_EXPANSION_PATTERN (t
);
9929 /* Add in any args remembered from an earlier partial instantiation. */
9930 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
9932 levels
= TMPL_ARGS_DEPTH (args
);
9934 /* Determine the argument packs that will instantiate the parameter
9935 packs used in the expansion expression. While we're at it,
9936 compute the number of arguments to be expanded and make sure it
9938 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
9939 pack
= TREE_CHAIN (pack
))
9941 tree parm_pack
= TREE_VALUE (pack
);
9942 tree arg_pack
= NULL_TREE
;
9943 tree orig_arg
= NULL_TREE
;
9946 if (TREE_CODE (parm_pack
) == BASES
)
9948 if (BASES_DIRECT (parm_pack
))
9949 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9950 args
, complain
, in_decl
, false));
9952 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9953 args
, complain
, in_decl
, false));
9955 if (TREE_CODE (parm_pack
) == PARM_DECL
)
9957 if (PACK_EXPANSION_LOCAL_P (t
))
9958 arg_pack
= retrieve_local_specialization (parm_pack
);
9961 /* We can't rely on local_specializations for a parameter
9962 name used later in a function declaration (such as in a
9963 late-specified return type). Even if it exists, it might
9964 have the wrong value for a recursive call. Just make a
9965 dummy decl, since it's only used for its type. */
9966 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
9967 if (arg_pack
&& DECL_PACK_P (arg_pack
))
9968 /* Partial instantiation of the parm_pack, we can't build
9969 up an argument pack yet. */
9970 arg_pack
= NULL_TREE
;
9972 arg_pack
= make_fnparm_pack (arg_pack
);
9973 need_local_specializations
= true;
9976 else if (TREE_CODE (parm_pack
) == FIELD_DECL
)
9977 arg_pack
= tsubst_copy (parm_pack
, args
, complain
, in_decl
);
9981 template_parm_level_and_index (parm_pack
, &level
, &idx
);
9983 if (level
<= levels
)
9984 arg_pack
= TMPL_ARG (args
, level
, idx
);
9987 orig_arg
= arg_pack
;
9988 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
9989 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
9991 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
9992 /* This can only happen if we forget to expand an argument
9993 pack somewhere else. Just return an error, silently. */
9995 result
= make_tree_vec (1);
9996 TREE_VEC_ELT (result
, 0) = error_mark_node
;
10003 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
10005 /* Don't bother trying to do a partial substitution with
10006 incomplete packs; we'll try again after deduction. */
10007 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
10012 else if (len
!= my_len
)
10014 if (!(complain
& tf_error
))
10015 /* Fail quietly. */;
10016 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
10017 error ("mismatched argument pack lengths while expanding "
10021 error ("mismatched argument pack lengths while expanding "
10024 return error_mark_node
;
10027 /* Keep track of the parameter packs and their corresponding
10029 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
10030 TREE_TYPE (packs
) = orig_arg
;
10034 /* We can't substitute for this parameter pack. We use a flag as
10035 well as the missing_level counter because function parameter
10036 packs don't have a level. */
10037 unsubstituted_packs
= true;
10041 /* If the expansion is just T..., return the matching argument pack. */
10042 if (!unsubstituted_packs
10043 && TREE_PURPOSE (packs
) == pattern
)
10045 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
10046 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
10047 || pack_expansion_args_count (args
))
10049 /* Otherwise use the normal path so we get convert_from_reference. */
10052 /* We cannot expand this expansion expression, because we don't have
10053 all of the argument packs we need. */
10054 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
10056 /* We got some full packs, but we can't substitute them in until we
10057 have values for all the packs. So remember these until then. */
10059 t
= make_pack_expansion (pattern
);
10060 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
10063 else if (unsubstituted_packs
)
10065 /* There were no real arguments, we're just replacing a parameter
10066 pack with another version of itself. Substitute into the
10067 pattern and return a PACK_EXPANSION_*. The caller will need to
10069 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
10070 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
10071 /*integral_constant_expression_p=*/false);
10073 t
= tsubst (pattern
, args
, complain
, in_decl
);
10074 t
= make_pack_expansion (t
);
10078 gcc_assert (len
>= 0);
10080 if (need_local_specializations
)
10082 /* We're in a late-specified return type, so create our own local
10083 specializations map; the current map is either NULL or (in the
10084 case of recursive unification) might have bindings that we don't
10085 want to use or alter. */
10086 saved_local_specializations
= local_specializations
;
10087 local_specializations
= new hash_map
<tree
, tree
>;
10090 /* For each argument in each argument pack, substitute into the
10092 result
= make_tree_vec (len
);
10093 for (i
= 0; i
< len
; ++i
)
10095 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
10099 TREE_VEC_ELT (result
, i
) = t
;
10100 if (t
== error_mark_node
)
10102 result
= error_mark_node
;
10107 /* Update ARGS to restore the substitution from parameter packs to
10108 their argument packs. */
10109 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
10111 tree parm
= TREE_PURPOSE (pack
);
10113 if (TREE_CODE (parm
) == PARM_DECL
10114 || TREE_CODE (parm
) == FIELD_DECL
)
10115 register_local_specialization (TREE_TYPE (pack
), parm
);
10120 if (TREE_VALUE (pack
) == NULL_TREE
)
10123 template_parm_level_and_index (parm
, &level
, &idx
);
10125 /* Update the corresponding argument. */
10126 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
10127 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
10130 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
10134 if (need_local_specializations
)
10136 delete local_specializations
;
10137 local_specializations
= saved_local_specializations
;
10143 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
10144 TMPL. We do this using DECL_PARM_INDEX, which should work even with
10145 parameter packs; all parms generated from a function parameter pack will
10146 have the same DECL_PARM_INDEX. */
10149 get_pattern_parm (tree parm
, tree tmpl
)
10151 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
10154 if (DECL_ARTIFICIAL (parm
))
10156 for (patparm
= DECL_ARGUMENTS (pattern
);
10157 patparm
; patparm
= DECL_CHAIN (patparm
))
10158 if (DECL_ARTIFICIAL (patparm
)
10159 && DECL_NAME (parm
) == DECL_NAME (patparm
))
10164 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
10165 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
10166 gcc_assert (DECL_PARM_INDEX (patparm
)
10167 == DECL_PARM_INDEX (parm
));
10173 /* Substitute ARGS into the vector or list of template arguments T. */
10176 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10179 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
10182 if (t
== error_mark_node
)
10183 return error_mark_node
;
10185 len
= TREE_VEC_LENGTH (t
);
10186 elts
= XALLOCAVEC (tree
, len
);
10188 for (i
= 0; i
< len
; i
++)
10190 tree orig_arg
= TREE_VEC_ELT (t
, i
);
10193 if (TREE_CODE (orig_arg
) == TREE_VEC
)
10194 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
10195 else if (PACK_EXPANSION_P (orig_arg
))
10197 /* Substitute into an expansion expression. */
10198 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
10200 if (TREE_CODE (new_arg
) == TREE_VEC
)
10201 /* Add to the expanded length adjustment the number of
10202 expanded arguments. We subtract one from this
10203 measurement, because the argument pack expression
10204 itself is already counted as 1 in
10205 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
10206 the argument pack is empty. */
10207 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
10209 else if (ARGUMENT_PACK_P (orig_arg
))
10211 /* Substitute into each of the arguments. */
10212 new_arg
= TYPE_P (orig_arg
)
10213 ? cxx_make_type (TREE_CODE (orig_arg
))
10214 : make_node (TREE_CODE (orig_arg
));
10216 SET_ARGUMENT_PACK_ARGS (
10218 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
10219 args
, complain
, in_decl
));
10221 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
10222 new_arg
= error_mark_node
;
10224 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
10225 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
10226 complain
, in_decl
);
10227 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
10229 if (TREE_TYPE (new_arg
) == error_mark_node
)
10230 new_arg
= error_mark_node
;
10234 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
10236 if (new_arg
== error_mark_node
)
10237 return error_mark_node
;
10240 if (new_arg
!= orig_arg
)
10247 /* Make space for the expanded arguments coming from template
10249 t
= make_tree_vec (len
+ expanded_len_adjust
);
10250 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
10251 arguments for a member template.
10252 In that case each TREE_VEC in ORIG_T represents a level of template
10253 arguments, and ORIG_T won't carry any non defaulted argument count.
10254 It will rather be the nested TREE_VECs that will carry one.
10255 In other words, ORIG_T carries a non defaulted argument count only
10256 if it doesn't contain any nested TREE_VEC. */
10257 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
10259 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
10260 count
+= expanded_len_adjust
;
10261 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
10263 for (i
= 0, out
= 0; i
< len
; i
++)
10265 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
10266 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
10267 && TREE_CODE (elts
[i
]) == TREE_VEC
)
10271 /* Now expand the template argument pack "in place". */
10272 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
10273 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
10277 TREE_VEC_ELT (t
, out
) = elts
[i
];
10285 /* Return the result of substituting ARGS into the template parameters
10286 given by PARMS. If there are m levels of ARGS and m + n levels of
10287 PARMS, then the result will contain n levels of PARMS. For
10288 example, if PARMS is `template <class T> template <class U>
10289 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
10290 result will be `template <int*, double, class V>'. */
10293 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
10295 tree r
= NULL_TREE
;
10298 /* When substituting into a template, we must set
10299 PROCESSING_TEMPLATE_DECL as the template parameters may be
10300 dependent if they are based on one-another, and the dependency
10301 predicates are short-circuit outside of templates. */
10302 ++processing_template_decl
;
10304 for (new_parms
= &r
;
10305 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
10306 new_parms
= &(TREE_CHAIN (*new_parms
)),
10307 parms
= TREE_CHAIN (parms
))
10310 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
10313 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
10317 if (parms
== error_mark_node
)
10320 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
10322 if (tuple
== error_mark_node
)
10325 TREE_VEC_ELT (new_vec
, i
) =
10326 tsubst_template_parm (tuple
, args
, complain
);
10330 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
10331 - TMPL_ARGS_DEPTH (args
)),
10332 new_vec
, NULL_TREE
);
10335 --processing_template_decl
;
10340 /* Return the result of substituting ARGS into one template parameter
10341 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
10342 parameter and which TREE_PURPOSE is the default argument of the
10343 template parameter. */
10346 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
10348 tree default_value
, parm_decl
;
10350 if (args
== NULL_TREE
10352 || t
== error_mark_node
)
10355 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
10357 default_value
= TREE_PURPOSE (t
);
10358 parm_decl
= TREE_VALUE (t
);
10360 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
10361 if (TREE_CODE (parm_decl
) == PARM_DECL
10362 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
10363 parm_decl
= error_mark_node
;
10364 default_value
= tsubst_template_arg (default_value
, args
,
10365 complain
, NULL_TREE
);
10367 return build_tree_list (default_value
, parm_decl
);
10370 /* Substitute the ARGS into the indicated aggregate (or enumeration)
10371 type T. If T is not an aggregate or enumeration type, it is
10372 handled as if by tsubst. IN_DECL is as for tsubst. If
10373 ENTERING_SCOPE is nonzero, T is the context for a template which
10374 we are presently tsubst'ing. Return the substituted value. */
10377 tsubst_aggr_type (tree t
,
10379 tsubst_flags_t complain
,
10381 int entering_scope
)
10383 if (t
== NULL_TREE
)
10386 switch (TREE_CODE (t
))
10389 if (TYPE_PTRMEMFUNC_P (t
))
10390 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
10392 /* Else fall through. */
10393 case ENUMERAL_TYPE
:
10395 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
10400 int saved_unevaluated_operand
;
10401 int saved_inhibit_evaluation_warnings
;
10403 /* In "sizeof(X<I>)" we need to evaluate "I". */
10404 saved_unevaluated_operand
= cp_unevaluated_operand
;
10405 cp_unevaluated_operand
= 0;
10406 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
10407 c_inhibit_evaluation_warnings
= 0;
10409 /* First, determine the context for the type we are looking
10411 context
= TYPE_CONTEXT (t
);
10412 if (context
&& TYPE_P (context
))
10414 context
= tsubst_aggr_type (context
, args
, complain
,
10415 in_decl
, /*entering_scope=*/1);
10416 /* If context is a nested class inside a class template,
10417 it may still need to be instantiated (c++/33959). */
10418 context
= complete_type (context
);
10421 /* Then, figure out what arguments are appropriate for the
10422 type we are trying to find. For example, given:
10424 template <class T> struct S;
10425 template <class T, class U> void f(T, U) { S<U> su; }
10427 and supposing that we are instantiating f<int, double>,
10428 then our ARGS will be {int, double}, but, when looking up
10429 S we only want {double}. */
10430 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
10431 complain
, in_decl
);
10432 if (argvec
== error_mark_node
)
10433 r
= error_mark_node
;
10436 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
10437 entering_scope
, complain
);
10438 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
10441 cp_unevaluated_operand
= saved_unevaluated_operand
;
10442 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
10447 /* This is not a template type, so there's nothing to do. */
10451 return tsubst (t
, args
, complain
, in_decl
);
10455 /* Substitute into the default argument ARG (a default argument for
10456 FN), which has the indicated TYPE. */
10459 tsubst_default_argument (tree fn
, tree type
, tree arg
, tsubst_flags_t complain
)
10461 tree saved_class_ptr
= NULL_TREE
;
10462 tree saved_class_ref
= NULL_TREE
;
10463 int errs
= errorcount
+ sorrycount
;
10465 /* This can happen in invalid code. */
10466 if (TREE_CODE (arg
) == DEFAULT_ARG
)
10469 /* This default argument came from a template. Instantiate the
10470 default argument here, not in tsubst. In the case of
10479 we must be careful to do name lookup in the scope of S<T>,
10480 rather than in the current class. */
10481 push_access_scope (fn
);
10482 /* The "this" pointer is not valid in a default argument. */
10485 saved_class_ptr
= current_class_ptr
;
10486 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
10487 saved_class_ref
= current_class_ref
;
10488 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
10491 push_deferring_access_checks(dk_no_deferred
);
10492 /* The default argument expression may cause implicitly defined
10493 member functions to be synthesized, which will result in garbage
10494 collection. We must treat this situation as if we were within
10495 the body of function so as to avoid collecting live data on the
10498 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
10499 complain
, NULL_TREE
,
10500 /*integral_constant_expression_p=*/false);
10502 pop_deferring_access_checks();
10504 /* Restore the "this" pointer. */
10507 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
10508 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
10511 if (errorcount
+sorrycount
> errs
10512 && (complain
& tf_warning_or_error
))
10513 inform (input_location
,
10514 " when instantiating default argument for call to %D", fn
);
10516 /* Make sure the default argument is reasonable. */
10517 arg
= check_default_argument (type
, arg
, complain
);
10519 pop_access_scope (fn
);
10524 /* Substitute into all the default arguments for FN. */
10527 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
10532 tmpl_args
= DECL_TI_ARGS (fn
);
10534 /* If this function is not yet instantiated, we certainly don't need
10535 its default arguments. */
10536 if (uses_template_parms (tmpl_args
))
10538 /* Don't do this again for clones. */
10539 if (DECL_CLONED_FUNCTION_P (fn
))
10542 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
10544 arg
= TREE_CHAIN (arg
))
10545 if (TREE_PURPOSE (arg
))
10546 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
10548 TREE_PURPOSE (arg
),
10552 /* Substitute the ARGS into the T, which is a _DECL. Return the
10553 result of the substitution. Issue error and warning messages under
10554 control of COMPLAIN. */
10557 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
10559 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10560 location_t saved_loc
;
10561 tree r
= NULL_TREE
;
10563 hashval_t hash
= 0;
10565 /* Set the filename and linenumber to improve error-reporting. */
10566 saved_loc
= input_location
;
10567 input_location
= DECL_SOURCE_LOCATION (t
);
10569 switch (TREE_CODE (t
))
10571 case TEMPLATE_DECL
:
10573 /* We can get here when processing a member function template,
10574 member class template, or template template parameter. */
10575 tree decl
= DECL_TEMPLATE_RESULT (t
);
10580 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10582 /* Template template parameter is treated here. */
10583 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10584 if (new_type
== error_mark_node
)
10585 RETURN (error_mark_node
);
10586 /* If we get a real template back, return it. This can happen in
10587 the context of most_specialized_partial_spec. */
10588 if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
10592 DECL_CHAIN (r
) = NULL_TREE
;
10593 TREE_TYPE (r
) = new_type
;
10594 DECL_TEMPLATE_RESULT (r
)
10595 = build_decl (DECL_SOURCE_LOCATION (decl
),
10596 TYPE_DECL
, DECL_NAME (decl
), new_type
);
10597 DECL_TEMPLATE_PARMS (r
)
10598 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10600 TYPE_NAME (new_type
) = r
;
10604 /* We might already have an instance of this template.
10605 The ARGS are for the surrounding class type, so the
10606 full args contain the tsubst'd args for the context,
10607 plus the innermost args from the template decl. */
10608 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
10609 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
10610 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
10611 /* Because this is a template, the arguments will still be
10612 dependent, even after substitution. If
10613 PROCESSING_TEMPLATE_DECL is not set, the dependency
10614 predicates will short-circuit. */
10615 ++processing_template_decl
;
10616 full_args
= tsubst_template_args (tmpl_args
, args
,
10617 complain
, in_decl
);
10618 --processing_template_decl
;
10619 if (full_args
== error_mark_node
)
10620 RETURN (error_mark_node
);
10622 /* If this is a default template template argument,
10623 tsubst might not have changed anything. */
10624 if (full_args
== tmpl_args
)
10627 hash
= hash_tmpl_and_args (t
, full_args
);
10628 spec
= retrieve_specialization (t
, full_args
, hash
);
10629 if (spec
!= NULL_TREE
)
10635 /* Make a new template decl. It will be similar to the
10636 original, but will record the current template arguments.
10637 We also create a new function declaration, which is just
10638 like the old one, but points to this new template, rather
10639 than the old one. */
10641 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
10642 DECL_CHAIN (r
) = NULL_TREE
;
10644 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10646 if (TREE_CODE (decl
) == TYPE_DECL
10647 && !TYPE_DECL_ALIAS_P (decl
))
10650 ++processing_template_decl
;
10651 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10652 --processing_template_decl
;
10653 if (new_type
== error_mark_node
)
10654 RETURN (error_mark_node
);
10656 TREE_TYPE (r
) = new_type
;
10657 /* For a partial specialization, we need to keep pointing to
10658 the primary template. */
10659 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
10660 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
10661 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
10662 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
10663 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
10668 ++processing_template_decl
;
10669 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
10670 --processing_template_decl
;
10671 if (new_decl
== error_mark_node
)
10672 RETURN (error_mark_node
);
10674 DECL_TEMPLATE_RESULT (r
) = new_decl
;
10675 DECL_TI_TEMPLATE (new_decl
) = r
;
10676 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
10677 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
10678 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
10681 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10682 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
10683 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
10685 /* The template parameters for this new template are all the
10686 template parameters for the old template, except the
10687 outermost level of parameters. */
10688 DECL_TEMPLATE_PARMS (r
)
10689 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10692 if (PRIMARY_TEMPLATE_P (t
))
10693 DECL_PRIMARY_TEMPLATE (r
) = r
;
10695 if (TREE_CODE (decl
) != TYPE_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
10696 /* Record this non-type partial instantiation. */
10697 register_specialization (r
, t
,
10698 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
10703 case FUNCTION_DECL
:
10706 tree argvec
= NULL_TREE
;
10714 /* Nobody should be tsubst'ing into non-template functions. */
10715 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
10717 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
10722 /* If T is not dependent, just return it. We have to
10723 increment PROCESSING_TEMPLATE_DECL because
10724 value_dependent_expression_p assumes that nothing is
10725 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10726 ++processing_template_decl
;
10727 dependent_p
= value_dependent_expression_p (t
);
10728 --processing_template_decl
;
10732 /* Calculate the most general template of which R is a
10733 specialization, and the complete set of arguments used to
10735 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
10736 argvec
= tsubst_template_args (DECL_TI_ARGS
10737 (DECL_TEMPLATE_RESULT
10738 (DECL_TI_TEMPLATE (t
))),
10739 args
, complain
, in_decl
);
10740 if (argvec
== error_mark_node
)
10741 RETURN (error_mark_node
);
10743 /* Check to see if we already have this specialization. */
10744 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10745 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10753 /* We can see more levels of arguments than parameters if
10754 there was a specialization of a member template, like
10757 template <class T> struct S { template <class U> void f(); }
10758 template <> template <class U> void S<int>::f(U);
10760 Here, we'll be substituting into the specialization,
10761 because that's where we can find the code we actually
10762 want to generate, but we'll have enough arguments for
10763 the most general template.
10765 We also deal with the peculiar case:
10767 template <class T> struct S {
10768 template <class U> friend void f();
10770 template <class U> void f() {}
10772 template void f<double>();
10774 Here, the ARGS for the instantiation of will be {int,
10775 double}. But, we only need as many ARGS as there are
10776 levels of template parameters in CODE_PATTERN. We are
10777 careful not to get fooled into reducing the ARGS in
10780 template <class T> struct S { template <class U> void f(U); }
10781 template <class T> template <> void S<T>::f(int) {}
10783 which we can spot because the pattern will be a
10784 specialization in this case. */
10785 args_depth
= TMPL_ARGS_DEPTH (args
);
10787 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
10788 if (args_depth
> parms_depth
10789 && !DECL_TEMPLATE_SPECIALIZATION (t
))
10790 args
= get_innermost_template_args (args
, parms_depth
);
10794 /* This special case arises when we have something like this:
10796 template <class T> struct S {
10797 friend void f<int>(int, double);
10800 Here, the DECL_TI_TEMPLATE for the friend declaration
10801 will be an IDENTIFIER_NODE. We are being called from
10802 tsubst_friend_function, and we want only to create a
10803 new decl (R) with appropriate types so that we can call
10804 determine_specialization. */
10805 gen_tmpl
= NULL_TREE
;
10808 if (DECL_CLASS_SCOPE_P (t
))
10810 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
10814 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
10815 complain
, t
, /*entering_scope=*/1);
10820 ctx
= DECL_CONTEXT (t
);
10822 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10823 if (type
== error_mark_node
)
10824 RETURN (error_mark_node
);
10826 /* If we hit excessive deduction depth, the type is bogus even if
10827 it isn't error_mark_node, so don't build a decl. */
10828 if (excessive_deduction_depth
)
10829 RETURN (error_mark_node
);
10831 /* We do NOT check for matching decls pushed separately at this
10832 point, as they may not represent instantiations of this
10833 template, and in any case are considered separate under the
10836 DECL_USE_TEMPLATE (r
) = 0;
10837 TREE_TYPE (r
) = type
;
10838 /* Clear out the mangled name and RTL for the instantiation. */
10839 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10840 SET_DECL_RTL (r
, NULL
);
10841 /* Leave DECL_INITIAL set on deleted instantiations. */
10842 if (!DECL_DELETED_FN (r
))
10843 DECL_INITIAL (r
) = NULL_TREE
;
10844 DECL_CONTEXT (r
) = ctx
;
10846 /* OpenMP UDRs have the only argument a reference to the declared
10847 type. We want to diagnose if the declared type is a reference,
10848 which is invalid, but as references to references are usually
10849 quietly merged, diagnose it here. */
10850 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
10853 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
10854 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
10855 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
10856 error_at (DECL_SOURCE_LOCATION (t
),
10857 "reference type %qT in "
10858 "%<#pragma omp declare reduction%>", argtype
);
10859 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
10860 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
10864 if (member
&& DECL_CONV_FN_P (r
))
10865 /* Type-conversion operator. Reconstruct the name, in
10866 case it's the name of one of the template's parameters. */
10867 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
10869 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
10871 DECL_RESULT (r
) = NULL_TREE
;
10873 TREE_STATIC (r
) = 0;
10874 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
10875 DECL_EXTERNAL (r
) = 1;
10876 /* If this is an instantiation of a function with internal
10877 linkage, we already know what object file linkage will be
10878 assigned to the instantiation. */
10879 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
10880 DECL_DEFER_OUTPUT (r
) = 0;
10881 DECL_CHAIN (r
) = NULL_TREE
;
10882 DECL_PENDING_INLINE_INFO (r
) = 0;
10883 DECL_PENDING_INLINE_P (r
) = 0;
10884 DECL_SAVED_TREE (r
) = NULL_TREE
;
10885 DECL_STRUCT_FUNCTION (r
) = NULL
;
10887 /* We'll re-clone as appropriate in instantiate_template. */
10888 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
10890 /* If we aren't complaining now, return on error before we register
10891 the specialization so that we'll complain eventually. */
10892 if ((complain
& tf_error
) == 0
10893 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10894 && !grok_op_properties (r
, /*complain=*/false))
10895 RETURN (error_mark_node
);
10897 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10898 this in the special friend case mentioned above where
10899 GEN_TMPL is NULL. */
10902 DECL_TEMPLATE_INFO (r
)
10903 = build_template_info (gen_tmpl
, argvec
);
10904 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10907 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10909 /* We instantiated this while substituting into
10910 the type earlier (template/friend54.C). */
10913 /* We're not supposed to instantiate default arguments
10914 until they are called, for a template. But, for a
10917 template <class T> void f ()
10918 { extern void g(int i = T()); }
10920 we should do the substitution when the template is
10921 instantiated. We handle the member function case in
10922 instantiate_class_template since the default arguments
10923 might refer to other members of the class. */
10925 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
10926 && !uses_template_parms (argvec
))
10927 tsubst_default_arguments (r
, complain
);
10930 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
10932 /* Copy the list of befriending classes. */
10933 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
10935 friends
= &TREE_CHAIN (*friends
))
10937 *friends
= copy_node (*friends
);
10938 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
10943 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
10945 maybe_retrofit_in_chrg (r
);
10946 if (DECL_CONSTRUCTOR_P (r
))
10947 grok_ctor_properties (ctx
, r
);
10948 if (DECL_INHERITED_CTOR_BASE (r
))
10949 deduce_inheriting_ctor (r
);
10950 /* If this is an instantiation of a member template, clone it.
10951 If it isn't, that'll be handled by
10952 clone_constructors_and_destructors. */
10953 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
10954 clone_function_decl (r
, /*update_method_vec_p=*/0);
10956 else if ((complain
& tf_error
) != 0
10957 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10958 && !grok_op_properties (r
, /*complain=*/true))
10959 RETURN (error_mark_node
);
10961 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
10962 SET_DECL_FRIEND_CONTEXT (r
,
10963 tsubst (DECL_FRIEND_CONTEXT (t
),
10964 args
, complain
, in_decl
));
10966 /* Possibly limit visibility based on template args. */
10967 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10968 if (DECL_VISIBILITY_SPECIFIED (t
))
10970 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10971 DECL_ATTRIBUTES (r
)
10972 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10974 determine_visibility (r
);
10975 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
10976 && !processing_template_decl
)
10977 defaulted_late_check (r
);
10979 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10980 args
, complain
, in_decl
);
10986 tree type
= NULL_TREE
;
10988 tree expanded_types
= NULL_TREE
;
10989 tree prev_r
= NULL_TREE
;
10990 tree first_r
= NULL_TREE
;
10992 if (DECL_PACK_P (t
))
10994 /* If there is a local specialization that isn't a
10995 parameter pack, it means that we're doing a "simple"
10996 substitution from inside tsubst_pack_expansion. Just
10997 return the local specialization (which will be a single
10999 tree spec
= retrieve_local_specialization (t
);
11001 && TREE_CODE (spec
) == PARM_DECL
11002 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
11005 /* Expand the TYPE_PACK_EXPANSION that provides the types for
11006 the parameters in this function parameter pack. */
11007 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
11008 complain
, in_decl
);
11009 if (TREE_CODE (expanded_types
) == TREE_VEC
)
11011 len
= TREE_VEC_LENGTH (expanded_types
);
11013 /* Zero-length parameter packs are boring. Just substitute
11016 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
11021 /* All we did was update the type. Make a note of that. */
11022 type
= expanded_types
;
11023 expanded_types
= NULL_TREE
;
11027 /* Loop through all of the parameters we'll build. When T is
11028 a function parameter pack, LEN is the number of expanded
11029 types in EXPANDED_TYPES; otherwise, LEN is 1. */
11031 for (i
= 0; i
< len
; ++i
)
11035 if (DECL_TEMPLATE_PARM_P (t
))
11036 SET_DECL_TEMPLATE_PARM_P (r
);
11038 if (expanded_types
)
11039 /* We're on the Ith parameter of the function parameter
11042 /* Get the Ith type. */
11043 type
= TREE_VEC_ELT (expanded_types
, i
);
11045 /* Rename the parameter to include the index. */
11047 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
11050 /* We're dealing with a normal parameter. */
11051 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11053 type
= type_decays_to (type
);
11054 TREE_TYPE (r
) = type
;
11055 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
11057 if (DECL_INITIAL (r
))
11059 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
11060 DECL_INITIAL (r
) = TREE_TYPE (r
);
11062 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
11063 complain
, in_decl
);
11066 DECL_CONTEXT (r
) = NULL_TREE
;
11068 if (!DECL_TEMPLATE_PARM_P (r
))
11069 DECL_ARG_TYPE (r
) = type_passed_as (type
);
11071 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
11072 args
, complain
, in_decl
);
11074 /* Keep track of the first new parameter we
11075 generate. That's what will be returned to the
11080 /* Build a proper chain of parameters when substituting
11081 into a function parameter pack. */
11083 DECL_CHAIN (prev_r
) = r
;
11086 /* If cp_unevaluated_operand is set, we're just looking for a
11087 single dummy parameter, so don't keep going. */
11088 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
11089 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
11090 complain
, DECL_CHAIN (t
));
11092 /* FIRST_R contains the start of the chain we've built. */
11099 tree type
= NULL_TREE
;
11100 tree vec
= NULL_TREE
;
11101 tree expanded_types
= NULL_TREE
;
11104 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
11106 /* This field is a lambda capture pack. Return a TREE_VEC of
11107 the expanded fields to instantiate_class_template_1 and
11108 store them in the specializations hash table as a
11109 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
11110 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
11111 complain
, in_decl
);
11112 if (TREE_CODE (expanded_types
) == TREE_VEC
)
11114 len
= TREE_VEC_LENGTH (expanded_types
);
11115 vec
= make_tree_vec (len
);
11119 /* All we did was update the type. Make a note of that. */
11120 type
= expanded_types
;
11121 expanded_types
= NULL_TREE
;
11125 for (int i
= 0; i
< len
; ++i
)
11128 if (expanded_types
)
11130 type
= TREE_VEC_ELT (expanded_types
, i
);
11132 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
11135 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11137 if (type
== error_mark_node
)
11138 RETURN (error_mark_node
);
11139 TREE_TYPE (r
) = type
;
11140 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
11142 if (DECL_C_BIT_FIELD (r
))
11143 /* For bit-fields, DECL_INITIAL gives the number of bits. For
11144 non-bit-fields DECL_INITIAL is a non-static data member
11145 initializer, which gets deferred instantiation. */
11147 = tsubst_expr (DECL_INITIAL (t
), args
,
11149 /*integral_constant_expression_p=*/true);
11150 else if (DECL_INITIAL (t
))
11152 /* Set up DECL_TEMPLATE_INFO so that we can get at the
11153 NSDMI in perform_member_init. Still set DECL_INITIAL
11154 so that we know there is one. */
11155 DECL_INITIAL (r
) = void_node
;
11156 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
11157 retrofit_lang_decl (r
);
11158 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
11160 /* We don't have to set DECL_CONTEXT here; it is set by
11161 finish_member_declaration. */
11162 DECL_CHAIN (r
) = NULL_TREE
;
11164 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
11165 args
, complain
, in_decl
);
11168 TREE_VEC_ELT (vec
, i
) = r
;
11174 tree pack
= make_node (NONTYPE_ARGUMENT_PACK
);
11175 tree tpack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
11176 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
11177 SET_ARGUMENT_PACK_ARGS (tpack
, expanded_types
);
11178 TREE_TYPE (pack
) = tpack
;
11179 register_specialization (pack
, t
, args
, false, 0);
11185 /* We reach here only for member using decls. We also need to check
11186 uses_template_parms because DECL_DEPENDENT_P is not set for a
11187 using-declaration that designates a member of the current
11188 instantiation (c++/53549). */
11189 if (DECL_DEPENDENT_P (t
)
11190 || uses_template_parms (USING_DECL_SCOPE (t
)))
11192 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
11193 complain
, in_decl
);
11194 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
11195 r
= do_class_using_decl (inst_scope
, name
);
11197 r
= error_mark_node
;
11200 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
11201 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
11207 DECL_CHAIN (r
) = NULL_TREE
;
11214 tree argvec
= NULL_TREE
;
11215 tree gen_tmpl
= NULL_TREE
;
11217 tree tmpl
= NULL_TREE
;
11219 tree type
= NULL_TREE
;
11222 if (TREE_TYPE (t
) == error_mark_node
)
11223 RETURN (error_mark_node
);
11225 if (TREE_CODE (t
) == TYPE_DECL
11226 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
11228 /* If this is the canonical decl, we don't have to
11229 mess with instantiations, and often we can't (for
11230 typename, template type parms and such). Note that
11231 TYPE_NAME is not correct for the above test if
11232 we've copied the type for a typedef. */
11233 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11234 if (type
== error_mark_node
)
11235 RETURN (error_mark_node
);
11236 r
= TYPE_NAME (type
);
11240 /* Check to see if we already have the specialization we
11243 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
11245 /* T is a static data member or namespace-scope entity.
11246 We have to substitute into namespace-scope variables
11247 (even though such entities are never templates) because
11250 template <class T> void f() { extern T t; }
11252 where the entity referenced is not known until
11253 instantiation time. */
11255 ctx
= DECL_CONTEXT (t
);
11256 if (DECL_CLASS_SCOPE_P (t
))
11258 ctx
= tsubst_aggr_type (ctx
, args
,
11260 in_decl
, /*entering_scope=*/1);
11261 /* If CTX is unchanged, then T is in fact the
11262 specialization we want. That situation occurs when
11263 referencing a static data member within in its own
11264 class. We can use pointer equality, rather than
11265 same_type_p, because DECL_CONTEXT is always
11267 if (ctx
== DECL_CONTEXT (t
)
11268 /* ... unless T is a member template; in which
11269 case our caller can be willing to create a
11270 specialization of that template represented
11272 && !(DECL_TI_TEMPLATE (t
)
11273 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
11279 tmpl
= DECL_TI_TEMPLATE (t
);
11280 gen_tmpl
= most_general_template (tmpl
);
11281 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
11282 if (argvec
!= error_mark_node
)
11283 argvec
= (coerce_innermost_template_parms
11284 (DECL_TEMPLATE_PARMS (gen_tmpl
),
11285 argvec
, t
, complain
,
11286 /*all*/true, /*defarg*/true));
11287 if (argvec
== error_mark_node
)
11288 RETURN (error_mark_node
);
11289 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
11290 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
11295 /* A local variable. */
11297 /* Subsequent calls to pushdecl will fill this in. */
11299 spec
= retrieve_local_specialization (t
);
11301 /* If we already have the specialization we need, there is
11302 nothing more to do. */
11309 /* Create a new node for the specialization we need. */
11311 if (type
== NULL_TREE
)
11313 if (is_typedef_decl (t
))
11314 type
= DECL_ORIGINAL_TYPE (t
);
11316 type
= TREE_TYPE (t
);
11318 && VAR_HAD_UNKNOWN_BOUND (t
)
11319 && type
!= error_mark_node
)
11320 type
= strip_array_domain (type
);
11321 type
= tsubst (type
, args
, complain
, in_decl
);
11325 /* Even if the original location is out of scope, the
11326 newly substituted one is not. */
11327 DECL_DEAD_FOR_LOCAL (r
) = 0;
11328 DECL_INITIALIZED_P (r
) = 0;
11329 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
11330 if (type
== error_mark_node
)
11331 RETURN (error_mark_node
);
11332 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11334 /* It may seem that this case cannot occur, since:
11339 declares a function, not a variable. However:
11342 template <typename T> void g() { T t; }
11343 template void g<f>();
11345 is an attempt to declare a variable with function
11347 error ("variable %qD has function type",
11348 /* R is not yet sufficiently initialized, so we
11349 just use its name. */
11351 RETURN (error_mark_node
);
11353 type
= complete_type (type
);
11354 /* Wait until cp_finish_decl to set this again, to handle
11355 circular dependency (template/instantiate6.C). */
11356 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
11357 type
= check_var_type (DECL_NAME (r
), type
);
11359 if (DECL_HAS_VALUE_EXPR_P (t
))
11361 tree ve
= DECL_VALUE_EXPR (t
);
11362 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
11363 /*constant_expression_p=*/false);
11364 if (REFERENCE_REF_P (ve
))
11366 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
11367 ve
= TREE_OPERAND (ve
, 0);
11369 SET_DECL_VALUE_EXPR (r
, ve
);
11371 if (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
11372 set_decl_tls_model (r
, decl_tls_model (t
));
11374 else if (DECL_SELF_REFERENCE_P (t
))
11375 SET_DECL_SELF_REFERENCE_P (r
);
11376 TREE_TYPE (r
) = type
;
11377 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
11378 DECL_CONTEXT (r
) = ctx
;
11379 /* Clear out the mangled name and RTL for the instantiation. */
11380 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
11381 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
11382 SET_DECL_RTL (r
, NULL
);
11383 /* The initializer must not be expanded until it is required;
11384 see [temp.inst]. */
11385 DECL_INITIAL (r
) = NULL_TREE
;
11386 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
11387 SET_DECL_RTL (r
, NULL
);
11388 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
11391 /* Possibly limit visibility based on template args. */
11392 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
11393 if (DECL_VISIBILITY_SPECIFIED (t
))
11395 DECL_VISIBILITY_SPECIFIED (r
) = 0;
11396 DECL_ATTRIBUTES (r
)
11397 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
11399 determine_visibility (r
);
11404 /* A static data member declaration is always marked
11405 external when it is declared in-class, even if an
11406 initializer is present. We mimic the non-template
11407 processing here. */
11408 DECL_EXTERNAL (r
) = 1;
11410 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
11411 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
11412 SET_DECL_IMPLICIT_INSTANTIATION (r
);
11414 else if (!cp_unevaluated_operand
)
11415 register_local_specialization (r
, t
);
11417 DECL_CHAIN (r
) = NULL_TREE
;
11419 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
11421 args
, complain
, in_decl
);
11423 /* Preserve a typedef that names a type. */
11424 if (is_typedef_decl (r
))
11426 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
11427 set_underlying_type (r
);
11430 layout_decl (r
, 0);
11435 gcc_unreachable ();
11440 /* Restore the file and line information. */
11441 input_location
= saved_loc
;
11446 /* Substitute into the ARG_TYPES of a function type.
11447 If END is a TREE_CHAIN, leave it and any following types
11451 tsubst_arg_types (tree arg_types
,
11454 tsubst_flags_t complain
,
11457 tree remaining_arg_types
;
11458 tree type
= NULL_TREE
;
11460 tree expanded_args
= NULL_TREE
;
11463 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
11466 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
11467 args
, end
, complain
, in_decl
);
11468 if (remaining_arg_types
== error_mark_node
)
11469 return error_mark_node
;
11471 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
11473 /* For a pack expansion, perform substitution on the
11474 entire expression. Later on, we'll handle the arguments
11476 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
11477 args
, complain
, in_decl
);
11479 if (TREE_CODE (expanded_args
) == TREE_VEC
)
11480 /* So that we'll spin through the parameters, one by one. */
11481 i
= TREE_VEC_LENGTH (expanded_args
);
11484 /* We only partially substituted into the parameter
11485 pack. Our type is TYPE_PACK_EXPANSION. */
11486 type
= expanded_args
;
11487 expanded_args
= NULL_TREE
;
11495 type
= TREE_VEC_ELT (expanded_args
, i
);
11497 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
11499 if (type
== error_mark_node
)
11500 return error_mark_node
;
11501 if (VOID_TYPE_P (type
))
11503 if (complain
& tf_error
)
11505 error ("invalid parameter type %qT", type
);
11507 error ("in declaration %q+D", in_decl
);
11509 return error_mark_node
;
11512 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
11513 return error_mark_node
;
11515 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11516 top-level qualifiers as required. */
11517 type
= cv_unqualified (type_decays_to (type
));
11519 /* We do not substitute into default arguments here. The standard
11520 mandates that they be instantiated only when needed, which is
11521 done in build_over_call. */
11522 default_arg
= TREE_PURPOSE (arg_types
);
11524 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
11526 /* We've instantiated a template before its default arguments
11527 have been parsed. This can happen for a nested template
11528 class, and is not an error unless we require the default
11529 argument in a call of this function. */
11530 remaining_arg_types
=
11531 tree_cons (default_arg
, type
, remaining_arg_types
);
11532 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
11535 remaining_arg_types
=
11536 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
11539 return remaining_arg_types
;
11542 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11543 *not* handle the exception-specification for FNTYPE, because the
11544 initial substitution of explicitly provided template parameters
11545 during argument deduction forbids substitution into the
11546 exception-specification:
11550 All references in the function type of the function template to the
11551 corresponding template parameters are replaced by the specified tem-
11552 plate argument values. If a substitution in a template parameter or
11553 in the function type of the function template results in an invalid
11554 type, type deduction fails. [Note: The equivalent substitution in
11555 exception specifications is done only when the function is instanti-
11556 ated, at which point a program is ill-formed if the substitution
11557 results in an invalid type.] */
11560 tsubst_function_type (tree t
,
11562 tsubst_flags_t complain
,
11566 tree arg_types
= NULL_TREE
;
11569 /* The TYPE_CONTEXT is not used for function/method types. */
11570 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
11572 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
11574 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
11576 if (late_return_type_p
)
11578 /* Substitute the argument types. */
11579 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
11580 complain
, in_decl
);
11581 if (arg_types
== error_mark_node
)
11582 return error_mark_node
;
11584 tree save_ccp
= current_class_ptr
;
11585 tree save_ccr
= current_class_ref
;
11586 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
11587 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
11588 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
11591 /* DR 1207: 'this' is in scope in the trailing return type. */
11592 inject_this_parameter (this_type
, cp_type_quals (this_type
));
11595 /* Substitute the return type. */
11596 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11600 current_class_ptr
= save_ccp
;
11601 current_class_ref
= save_ccr
;
11605 /* Substitute the return type. */
11606 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11608 if (return_type
== error_mark_node
)
11609 return error_mark_node
;
11610 /* DR 486 clarifies that creation of a function type with an
11611 invalid return type is a deduction failure. */
11612 if (TREE_CODE (return_type
) == ARRAY_TYPE
11613 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
11615 if (complain
& tf_error
)
11617 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
11618 error ("function returning an array");
11620 error ("function returning a function");
11622 return error_mark_node
;
11625 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
11626 return error_mark_node
;
11628 if (!late_return_type_p
)
11630 /* Substitute the argument types. */
11631 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
11632 complain
, in_decl
);
11633 if (arg_types
== error_mark_node
)
11634 return error_mark_node
;
11637 /* Construct a new type node and return it. */
11638 if (TREE_CODE (t
) == FUNCTION_TYPE
)
11640 fntype
= build_function_type (return_type
, arg_types
);
11641 fntype
= apply_memfn_quals (fntype
,
11642 type_memfn_quals (t
),
11643 type_memfn_rqual (t
));
11647 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
11648 /* Don't pick up extra function qualifiers from the basetype. */
11649 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
11650 if (! MAYBE_CLASS_TYPE_P (r
))
11654 Type deduction may fail for any of the following
11657 -- Attempting to create "pointer to member of T" when T
11658 is not a class type. */
11659 if (complain
& tf_error
)
11660 error ("creating pointer to member function of non-class type %qT",
11662 return error_mark_node
;
11665 fntype
= build_method_type_directly (r
, return_type
,
11666 TREE_CHAIN (arg_types
));
11667 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
11669 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
11671 if (late_return_type_p
)
11672 TYPE_HAS_LATE_RETURN_TYPE (fntype
) = 1;
11677 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11678 ARGS into that specification, and return the substituted
11679 specification. If there is no specification, return NULL_TREE. */
11682 tsubst_exception_specification (tree fntype
,
11684 tsubst_flags_t complain
,
11691 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
11692 new_specs
= NULL_TREE
;
11693 if (specs
&& TREE_PURPOSE (specs
))
11695 /* A noexcept-specifier. */
11696 tree expr
= TREE_PURPOSE (specs
);
11697 if (TREE_CODE (expr
) == INTEGER_CST
)
11701 /* Defer instantiation of noexcept-specifiers to avoid
11702 excessive instantiations (c++/49107). */
11703 new_specs
= make_node (DEFERRED_NOEXCEPT
);
11704 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
11706 /* We already partially instantiated this member template,
11707 so combine the new args with the old. */
11708 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
11709 = DEFERRED_NOEXCEPT_PATTERN (expr
);
11710 DEFERRED_NOEXCEPT_ARGS (new_specs
)
11711 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
11715 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
11716 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
11720 new_specs
= tsubst_copy_and_build
11721 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
11722 /*integral_constant_expression_p=*/true);
11723 new_specs
= build_noexcept_spec (new_specs
, complain
);
11727 if (! TREE_VALUE (specs
))
11734 tree expanded_specs
= NULL_TREE
;
11736 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
11738 /* Expand the pack expansion type. */
11739 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
11743 if (expanded_specs
== error_mark_node
)
11744 return error_mark_node
;
11745 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
11746 len
= TREE_VEC_LENGTH (expanded_specs
);
11749 /* We're substituting into a member template, so
11750 we got a TYPE_PACK_EXPANSION back. Add that
11751 expansion and move on. */
11752 gcc_assert (TREE_CODE (expanded_specs
)
11753 == TYPE_PACK_EXPANSION
);
11754 new_specs
= add_exception_specifier (new_specs
,
11757 specs
= TREE_CHAIN (specs
);
11762 for (i
= 0; i
< len
; ++i
)
11764 if (expanded_specs
)
11765 spec
= TREE_VEC_ELT (expanded_specs
, i
);
11767 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
11768 if (spec
== error_mark_node
)
11770 new_specs
= add_exception_specifier (new_specs
, spec
,
11774 specs
= TREE_CHAIN (specs
);
11780 /* Take the tree structure T and replace template parameters used
11781 therein with the argument vector ARGS. IN_DECL is an associated
11782 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11783 Issue error and warning messages under control of COMPLAIN. Note
11784 that we must be relatively non-tolerant of extensions here, in
11785 order to preserve conformance; if we allow substitutions that
11786 should not be allowed, we may allow argument deductions that should
11787 not succeed, and therefore report ambiguous overload situations
11788 where there are none. In theory, we could allow the substitution,
11789 but indicate that it should have failed, and allow our caller to
11790 make sure that the right thing happens, but we don't try to do this
11793 This function is used for dealing with types, decls and the like;
11794 for expressions, use tsubst_expr or tsubst_copy. */
11797 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11799 enum tree_code code
;
11800 tree type
, r
= NULL_TREE
;
11802 if (t
== NULL_TREE
|| t
== error_mark_node
11803 || t
== integer_type_node
11804 || t
== void_type_node
11805 || t
== char_type_node
11806 || t
== unknown_type_node
11807 || TREE_CODE (t
) == NAMESPACE_DECL
11808 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
11812 return tsubst_decl (t
, args
, complain
);
11814 if (args
== NULL_TREE
)
11817 code
= TREE_CODE (t
);
11819 if (code
== IDENTIFIER_NODE
)
11820 type
= IDENTIFIER_TYPE_VALUE (t
);
11822 type
= TREE_TYPE (t
);
11824 gcc_assert (type
!= unknown_type_node
);
11826 /* Reuse typedefs. We need to do this to handle dependent attributes,
11827 such as attribute aligned. */
11829 && typedef_variant_p (t
))
11831 tree decl
= TYPE_NAME (t
);
11833 if (alias_template_specialization_p (t
))
11835 /* DECL represents an alias template and we want to
11837 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11838 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11839 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
11841 else if (DECL_CLASS_SCOPE_P (decl
)
11842 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11843 && uses_template_parms (DECL_CONTEXT (decl
)))
11845 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11846 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11847 r
= retrieve_specialization (tmpl
, gen_args
, 0);
11849 else if (DECL_FUNCTION_SCOPE_P (decl
)
11850 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11851 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
11852 r
= retrieve_local_specialization (decl
);
11854 /* The typedef is from a non-template context. */
11860 r
= cp_build_qualified_type_real
11861 (r
, cp_type_quals (t
) | cp_type_quals (r
),
11862 complain
| tf_ignore_bad_quals
);
11867 /* We don't have an instantiation yet, so drop the typedef. */
11868 int quals
= cp_type_quals (t
);
11869 t
= DECL_ORIGINAL_TYPE (decl
);
11870 t
= cp_build_qualified_type_real (t
, quals
,
11871 complain
| tf_ignore_bad_quals
);
11876 && code
!= TYPENAME_TYPE
11877 && code
!= TEMPLATE_TYPE_PARM
11878 && code
!= IDENTIFIER_NODE
11879 && code
!= FUNCTION_TYPE
11880 && code
!= METHOD_TYPE
)
11881 type
= tsubst (type
, args
, complain
, in_decl
);
11882 if (type
== error_mark_node
)
11883 return error_mark_node
;
11889 case ENUMERAL_TYPE
:
11890 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
11891 /*entering_scope=*/0);
11894 case IDENTIFIER_NODE
:
11905 if (t
== integer_type_node
)
11908 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
11909 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
11913 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
11915 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
11916 /*integral_constant_expression_p=*/false);
11918 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11920 if (TREE_CODE (max
) == NOP_EXPR
11921 && TREE_SIDE_EFFECTS (omax
)
11922 && !TREE_TYPE (max
))
11923 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
11925 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11926 with TREE_SIDE_EFFECTS that indicates this is not an integral
11927 constant expression. */
11928 if (processing_template_decl
11929 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
11931 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
11932 TREE_SIDE_EFFECTS (max
) = 1;
11935 return compute_array_index_type (NULL_TREE
, max
, complain
);
11938 case TEMPLATE_TYPE_PARM
:
11939 case TEMPLATE_TEMPLATE_PARM
:
11940 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11941 case TEMPLATE_PARM_INDEX
:
11946 tree arg
= NULL_TREE
;
11950 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
11951 template_parm_level_and_index (t
, &level
, &idx
);
11953 levels
= TMPL_ARGS_DEPTH (args
);
11954 if (level
<= levels
)
11956 arg
= TMPL_ARG (args
, level
, idx
);
11958 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
11960 /* See through ARGUMENT_PACK_SELECT arguments. */
11961 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
11962 /* If the selected argument is an expansion E, that most
11963 likely means we were called from
11964 gen_elem_of_pack_expansion_instantiation during the
11965 substituting of pack an argument pack (which Ith
11966 element is a pack expansion, where I is
11967 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11968 In this case, the Ith element resulting from this
11969 substituting is going to be a pack expansion, which
11970 pattern is the pattern of E. Let's return the
11972 gen_elem_of_pack_expansion_instantiation will
11973 build the resulting pack expansion from it. */
11974 if (PACK_EXPANSION_P (arg
))
11976 /* Make sure we aren't throwing away arg info. */
11977 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
11978 arg
= PACK_EXPANSION_PATTERN (arg
);
11983 if (arg
== error_mark_node
)
11984 return error_mark_node
;
11985 else if (arg
!= NULL_TREE
)
11987 if (ARGUMENT_PACK_P (arg
))
11988 /* If ARG is an argument pack, we don't actually want to
11989 perform a substitution here, because substitutions
11990 for argument packs are only done
11991 element-by-element. We can get to this point when
11992 substituting the type of a non-type template
11993 parameter pack, when that type actually contains
11994 template parameter packs from an outer template, e.g.,
11996 template<typename... Types> struct A {
11997 template<Types... Values> struct B { };
12001 if (code
== TEMPLATE_TYPE_PARM
)
12004 gcc_assert (TYPE_P (arg
));
12006 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
12008 return cp_build_qualified_type_real
12009 (arg
, quals
, complain
| tf_ignore_bad_quals
);
12011 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
12013 /* We are processing a type constructed from a
12014 template template parameter. */
12015 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
12016 args
, complain
, in_decl
);
12017 if (argvec
== error_mark_node
)
12018 return error_mark_node
;
12020 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
12021 || TREE_CODE (arg
) == TEMPLATE_DECL
12022 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
12024 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
12025 /* Consider this code:
12027 template <template <class> class Template>
12029 template <class Arg> using Bind = Template<Arg>;
12032 template <template <class> class Template, class Arg>
12033 using Instantiate = Template<Arg>; //#0
12035 template <template <class> class Template,
12038 Instantiate<Internal<Template>::template Bind,
12041 When #1 is parsed, the
12042 BOUND_TEMPLATE_TEMPLATE_PARM representing the
12043 parameter `Template' in #0 matches the
12044 UNBOUND_CLASS_TEMPLATE representing the argument
12045 `Internal<Template>::template Bind'; We then want
12046 to assemble the type `Bind<Argument>' that can't
12047 be fully created right now, because
12048 `Internal<Template>' not being complete, the Bind
12049 template cannot be looked up in that context. So
12050 we need to "store" `Bind<Argument>' for later
12051 when the context of Bind becomes complete. Let's
12052 store that in a TYPENAME_TYPE. */
12053 return make_typename_type (TYPE_CONTEXT (arg
),
12054 build_nt (TEMPLATE_ID_EXPR
,
12055 TYPE_IDENTIFIER (arg
),
12060 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
12061 are resolving nested-types in the signature of a
12062 member function templates. Otherwise ARG is a
12063 TEMPLATE_DECL and is the real template to be
12065 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
12066 arg
= TYPE_NAME (arg
);
12068 r
= lookup_template_class (arg
,
12070 DECL_CONTEXT (arg
),
12071 /*entering_scope=*/0,
12073 return cp_build_qualified_type_real
12074 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
12077 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
12078 return convert_from_reference (unshare_expr (arg
));
12082 /* This can happen during the attempted tsubst'ing in
12083 unify. This means that we don't yet have any information
12084 about the template parameter in question. */
12087 /* Early in template argument deduction substitution, we don't
12088 want to reduce the level of 'auto', or it will be confused
12089 with a normal template parm in subsequent deduction. */
12090 if (is_auto (t
) && (complain
& tf_partial
))
12093 /* If we get here, we must have been looking at a parm for a
12094 more deeply nested template. Make a new version of this
12095 template parameter, but with a lower level. */
12098 case TEMPLATE_TYPE_PARM
:
12099 case TEMPLATE_TEMPLATE_PARM
:
12100 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12101 if (cp_type_quals (t
))
12103 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
12104 r
= cp_build_qualified_type_real
12105 (r
, cp_type_quals (t
),
12106 complain
| (code
== TEMPLATE_TYPE_PARM
12107 ? tf_ignore_bad_quals
: 0));
12112 TEMPLATE_TYPE_PARM_INDEX (r
)
12113 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
12114 r
, levels
, args
, complain
);
12115 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
12116 TYPE_MAIN_VARIANT (r
) = r
;
12117 TYPE_POINTER_TO (r
) = NULL_TREE
;
12118 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
12120 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
12121 /* We have reduced the level of the template
12122 template parameter, but not the levels of its
12123 template parameters, so canonical_type_parameter
12124 will not be able to find the canonical template
12125 template parameter for this level. Thus, we
12126 require structural equality checking to compare
12127 TEMPLATE_TEMPLATE_PARMs. */
12128 SET_TYPE_STRUCTURAL_EQUALITY (r
);
12129 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
12130 SET_TYPE_STRUCTURAL_EQUALITY (r
);
12132 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
12134 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
12136 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
12137 complain
, in_decl
);
12138 if (argvec
== error_mark_node
)
12139 return error_mark_node
;
12141 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
12142 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
12147 case TEMPLATE_PARM_INDEX
:
12148 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
12152 gcc_unreachable ();
12160 tree purpose
, value
, chain
;
12162 if (t
== void_list_node
)
12165 purpose
= TREE_PURPOSE (t
);
12168 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
12169 if (purpose
== error_mark_node
)
12170 return error_mark_node
;
12172 value
= TREE_VALUE (t
);
12175 value
= tsubst (value
, args
, complain
, in_decl
);
12176 if (value
== error_mark_node
)
12177 return error_mark_node
;
12179 chain
= TREE_CHAIN (t
);
12180 if (chain
&& chain
!= void_type_node
)
12182 chain
= tsubst (chain
, args
, complain
, in_decl
);
12183 if (chain
== error_mark_node
)
12184 return error_mark_node
;
12186 if (purpose
== TREE_PURPOSE (t
)
12187 && value
== TREE_VALUE (t
)
12188 && chain
== TREE_CHAIN (t
))
12190 return hash_tree_cons (purpose
, value
, chain
);
12194 /* We should never be tsubsting a binfo. */
12195 gcc_unreachable ();
12198 /* A vector of template arguments. */
12199 gcc_assert (!type
);
12200 return tsubst_template_args (t
, args
, complain
, in_decl
);
12203 case REFERENCE_TYPE
:
12205 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
12210 Type deduction may fail for any of the following
12213 -- Attempting to create a pointer to reference type.
12214 -- Attempting to create a reference to a reference type or
12215 a reference to void.
12217 Core issue 106 says that creating a reference to a reference
12218 during instantiation is no longer a cause for failure. We
12219 only enforce this check in strict C++98 mode. */
12220 if ((TREE_CODE (type
) == REFERENCE_TYPE
12221 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
12222 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
12224 static location_t last_loc
;
12226 /* We keep track of the last time we issued this error
12227 message to avoid spewing a ton of messages during a
12228 single bad template instantiation. */
12229 if (complain
& tf_error
12230 && last_loc
!= input_location
)
12232 if (VOID_TYPE_P (type
))
12233 error ("forming reference to void");
12234 else if (code
== POINTER_TYPE
)
12235 error ("forming pointer to reference type %qT", type
);
12237 error ("forming reference to reference type %qT", type
);
12238 last_loc
= input_location
;
12241 return error_mark_node
;
12243 else if (TREE_CODE (type
) == FUNCTION_TYPE
12244 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
12245 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
12247 if (complain
& tf_error
)
12249 if (code
== POINTER_TYPE
)
12250 error ("forming pointer to qualified function type %qT",
12253 error ("forming reference to qualified function type %qT",
12256 return error_mark_node
;
12258 else if (code
== POINTER_TYPE
)
12260 r
= build_pointer_type (type
);
12261 if (TREE_CODE (type
) == METHOD_TYPE
)
12262 r
= build_ptrmemfunc_type (r
);
12264 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
12265 /* In C++0x, during template argument substitution, when there is an
12266 attempt to create a reference to a reference type, reference
12267 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
12269 "If a template-argument for a template-parameter T names a type
12270 that is a reference to a type A, an attempt to create the type
12271 'lvalue reference to cv T' creates the type 'lvalue reference to
12272 A,' while an attempt to create the type type rvalue reference to
12273 cv T' creates the type T"
12275 r
= cp_build_reference_type
12277 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
12279 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
12280 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
12282 if (r
!= error_mark_node
)
12283 /* Will this ever be needed for TYPE_..._TO values? */
12290 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
12291 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
12295 Type deduction may fail for any of the following
12298 -- Attempting to create "pointer to member of T" when T
12299 is not a class type. */
12300 if (complain
& tf_error
)
12301 error ("creating pointer to member of non-class type %qT", r
);
12302 return error_mark_node
;
12304 if (TREE_CODE (type
) == REFERENCE_TYPE
)
12306 if (complain
& tf_error
)
12307 error ("creating pointer to member reference type %qT", type
);
12308 return error_mark_node
;
12310 if (VOID_TYPE_P (type
))
12312 if (complain
& tf_error
)
12313 error ("creating pointer to member of type void");
12314 return error_mark_node
;
12316 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
12317 if (TREE_CODE (type
) == FUNCTION_TYPE
)
12319 /* The type of the implicit object parameter gets its
12320 cv-qualifiers from the FUNCTION_TYPE. */
12323 = build_memfn_type (type
, r
, type_memfn_quals (type
),
12324 type_memfn_rqual (type
));
12325 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
12326 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
12330 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
12334 case FUNCTION_TYPE
:
12339 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
12340 if (fntype
== error_mark_node
)
12341 return error_mark_node
;
12343 /* Substitute the exception specification. */
12344 specs
= tsubst_exception_specification (t
, args
, complain
,
12345 in_decl
, /*defer_ok*/true);
12346 if (specs
== error_mark_node
)
12347 return error_mark_node
;
12349 fntype
= build_exception_variant (fntype
, specs
);
12354 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
12355 if (domain
== error_mark_node
)
12356 return error_mark_node
;
12358 /* As an optimization, we avoid regenerating the array type if
12359 it will obviously be the same as T. */
12360 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
12363 /* These checks should match the ones in create_array_type_for_decl.
12367 The deduction may fail for any of the following reasons:
12369 -- Attempting to create an array with an element type that
12370 is void, a function type, or a reference type, or [DR337]
12371 an abstract class type. */
12372 if (VOID_TYPE_P (type
)
12373 || TREE_CODE (type
) == FUNCTION_TYPE
12374 || (TREE_CODE (type
) == ARRAY_TYPE
12375 && TYPE_DOMAIN (type
) == NULL_TREE
)
12376 || TREE_CODE (type
) == REFERENCE_TYPE
)
12378 if (complain
& tf_error
)
12379 error ("creating array of %qT", type
);
12380 return error_mark_node
;
12383 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
12384 return error_mark_node
;
12386 r
= build_cplus_array_type (type
, domain
);
12388 if (TYPE_USER_ALIGN (t
))
12390 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
12391 TYPE_USER_ALIGN (r
) = 1;
12397 case TYPENAME_TYPE
:
12399 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
12400 in_decl
, /*entering_scope=*/1);
12401 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
12402 complain
, in_decl
);
12404 if (ctx
== error_mark_node
|| f
== error_mark_node
)
12405 return error_mark_node
;
12407 if (!MAYBE_CLASS_TYPE_P (ctx
))
12409 if (complain
& tf_error
)
12410 error ("%qT is not a class, struct, or union type", ctx
);
12411 return error_mark_node
;
12413 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
12415 /* Normally, make_typename_type does not require that the CTX
12416 have complete type in order to allow things like:
12418 template <class T> struct S { typename S<T>::X Y; };
12420 But, such constructs have already been resolved by this
12421 point, so here CTX really should have complete type, unless
12422 it's a partial instantiation. */
12423 ctx
= complete_type (ctx
);
12424 if (!COMPLETE_TYPE_P (ctx
))
12426 if (complain
& tf_error
)
12427 cxx_incomplete_type_error (NULL_TREE
, ctx
);
12428 return error_mark_node
;
12432 f
= make_typename_type (ctx
, f
, typename_type
,
12433 complain
| tf_keep_type_decl
);
12434 if (f
== error_mark_node
)
12436 if (TREE_CODE (f
) == TYPE_DECL
)
12438 complain
|= tf_ignore_bad_quals
;
12442 if (TREE_CODE (f
) != TYPENAME_TYPE
)
12444 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
12446 if (complain
& tf_error
)
12447 error ("%qT resolves to %qT, which is not an enumeration type",
12450 return error_mark_node
;
12452 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
12454 if (complain
& tf_error
)
12455 error ("%qT resolves to %qT, which is is not a class type",
12458 return error_mark_node
;
12462 return cp_build_qualified_type_real
12463 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
12466 case UNBOUND_CLASS_TEMPLATE
:
12468 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
12469 in_decl
, /*entering_scope=*/1);
12470 tree name
= TYPE_IDENTIFIER (t
);
12471 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
12473 if (ctx
== error_mark_node
|| name
== error_mark_node
)
12474 return error_mark_node
;
12477 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
12478 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
12485 ++cp_unevaluated_operand
;
12486 ++c_inhibit_evaluation_warnings
;
12488 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
12490 /*integral_constant_expression_p=*/false);
12492 --cp_unevaluated_operand
;
12493 --c_inhibit_evaluation_warnings
;
12495 type
= finish_typeof (type
);
12496 return cp_build_qualified_type_real (type
,
12498 | cp_type_quals (type
),
12502 case DECLTYPE_TYPE
:
12506 ++cp_unevaluated_operand
;
12507 ++c_inhibit_evaluation_warnings
;
12509 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
12510 complain
|tf_decltype
, in_decl
,
12511 /*function_p*/false,
12512 /*integral_constant_expression*/false);
12514 --cp_unevaluated_operand
;
12515 --c_inhibit_evaluation_warnings
;
12517 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
12518 type
= lambda_capture_field_type (type
,
12519 DECLTYPE_FOR_INIT_CAPTURE (t
));
12520 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
12521 type
= lambda_proxy_type (type
);
12524 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
12525 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
12527 /* In a template ~id could be either a complement expression
12528 or an unqualified-id naming a destructor; if instantiating
12529 it produces an expression, it's not an id-expression or
12532 type
= finish_decltype_type (type
, id
, complain
);
12534 return cp_build_qualified_type_real (type
,
12536 | cp_type_quals (type
),
12537 complain
| tf_ignore_bad_quals
);
12540 case UNDERLYING_TYPE
:
12542 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
12543 complain
, in_decl
);
12544 return finish_underlying_type (type
);
12547 case TYPE_ARGUMENT_PACK
:
12548 case NONTYPE_ARGUMENT_PACK
:
12550 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
12552 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
12556 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
12558 /* For template nontype argument packs, also substitute into
12560 if (code
== NONTYPE_ARGUMENT_PACK
)
12561 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12580 /* We should use one of the expression tsubsts for these codes. */
12581 gcc_unreachable ();
12584 sorry ("use of %qs in template", get_tree_code_name (code
));
12585 return error_mark_node
;
12589 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12590 type of the expression on the left-hand side of the "." or "->"
12594 tsubst_baselink (tree baselink
, tree object_type
,
12595 tree args
, tsubst_flags_t complain
, tree in_decl
)
12598 tree qualifying_scope
;
12601 tree template_args
= 0;
12602 bool template_id_p
= false;
12603 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
12605 /* A baselink indicates a function from a base class. Both the
12606 BASELINK_ACCESS_BINFO and the base class referenced may
12607 indicate bases of the template class, rather than the
12608 instantiated class. In addition, lookups that were not
12609 ambiguous before may be ambiguous now. Therefore, we perform
12610 the lookup again. */
12611 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
12612 qualifying_scope
= tsubst (qualifying_scope
, args
,
12613 complain
, in_decl
);
12614 fns
= BASELINK_FUNCTIONS (baselink
);
12615 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
12616 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
12618 template_id_p
= true;
12619 template_args
= TREE_OPERAND (fns
, 1);
12620 fns
= TREE_OPERAND (fns
, 0);
12622 template_args
= tsubst_template_args (template_args
, args
,
12623 complain
, in_decl
);
12625 name
= DECL_NAME (get_first_fn (fns
));
12626 if (IDENTIFIER_TYPENAME_P (name
))
12627 name
= mangle_conv_op_name_for_type (optype
);
12628 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
12630 return error_mark_node
;
12632 /* If lookup found a single function, mark it as used at this
12633 point. (If it lookup found multiple functions the one selected
12634 later by overload resolution will be marked as used at that
12636 if (BASELINK_P (baselink
))
12637 fns
= BASELINK_FUNCTIONS (baselink
);
12638 if (!template_id_p
&& !really_overloaded_fn (fns
)
12639 && !mark_used (OVL_CURRENT (fns
), complain
) && !(complain
& tf_error
))
12640 return error_mark_node
;
12642 /* Add back the template arguments, if present. */
12643 if (BASELINK_P (baselink
) && template_id_p
)
12644 BASELINK_FUNCTIONS (baselink
)
12645 = build_nt (TEMPLATE_ID_EXPR
,
12646 BASELINK_FUNCTIONS (baselink
),
12648 /* Update the conversion operator type. */
12649 BASELINK_OPTYPE (baselink
) = optype
;
12652 object_type
= current_class_type
;
12655 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
12661 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12662 true if the qualified-id will be a postfix-expression in-and-of
12663 itself; false if more of the postfix-expression follows the
12664 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12668 tsubst_qualified_id (tree qualified_id
, tree args
,
12669 tsubst_flags_t complain
, tree in_decl
,
12670 bool done
, bool address_p
)
12676 tree template_args
;
12677 location_t loc
= UNKNOWN_LOCATION
;
12679 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
12681 /* Figure out what name to look up. */
12682 name
= TREE_OPERAND (qualified_id
, 1);
12683 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
12685 is_template
= true;
12686 loc
= EXPR_LOCATION (name
);
12687 template_args
= TREE_OPERAND (name
, 1);
12689 template_args
= tsubst_template_args (template_args
, args
,
12690 complain
, in_decl
);
12691 name
= TREE_OPERAND (name
, 0);
12695 is_template
= false;
12696 template_args
= NULL_TREE
;
12699 /* Substitute into the qualifying scope. When there are no ARGS, we
12700 are just trying to simplify a non-dependent expression. In that
12701 case the qualifying scope may be dependent, and, in any case,
12702 substituting will not help. */
12703 scope
= TREE_OPERAND (qualified_id
, 0);
12706 scope
= tsubst (scope
, args
, complain
, in_decl
);
12707 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
12712 if (dependent_scope_p (scope
))
12715 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
12716 return build_qualified_name (NULL_TREE
, scope
, expr
,
12717 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
12720 if (!BASELINK_P (name
) && !DECL_P (expr
))
12722 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
12724 /* A BIT_NOT_EXPR is used to represent a destructor. */
12725 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
12727 error ("qualifying type %qT does not match destructor name ~%qT",
12728 scope
, TREE_OPERAND (expr
, 0));
12729 expr
= error_mark_node
;
12732 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
12733 /*is_type_p=*/0, false);
12736 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
12737 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
12738 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
12740 if (complain
& tf_error
)
12742 error ("dependent-name %qE is parsed as a non-type, but "
12743 "instantiation yields a type", qualified_id
);
12744 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
12746 return error_mark_node
;
12752 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
12754 /* Remember that there was a reference to this entity. */
12755 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
12756 return error_mark_node
;
12759 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
12761 if (complain
& tf_error
)
12762 qualified_name_lookup_error (scope
,
12763 TREE_OPERAND (qualified_id
, 1),
12764 expr
, input_location
);
12765 return error_mark_node
;
12769 expr
= lookup_template_function (expr
, template_args
);
12771 if (expr
== error_mark_node
&& complain
& tf_error
)
12772 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
12773 expr
, input_location
);
12774 else if (TYPE_P (scope
))
12776 expr
= (adjust_result_of_qualified_name_lookup
12777 (expr
, scope
, current_nonlambda_class_type ()));
12778 expr
= (finish_qualified_id_expr
12779 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
12780 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
12781 /*template_arg_p=*/false, complain
));
12784 /* Expressions do not generally have reference type. */
12785 if (TREE_CODE (expr
) != SCOPE_REF
12786 /* However, if we're about to form a pointer-to-member, we just
12787 want the referenced member referenced. */
12788 && TREE_CODE (expr
) != OFFSET_REF
)
12789 expr
= convert_from_reference (expr
);
12794 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
12795 initializer, DECL is the substituted VAR_DECL. Other arguments are as
12799 tsubst_init (tree init
, tree decl
, tree args
,
12800 tsubst_flags_t complain
, tree in_decl
)
12805 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
12809 /* If we had an initializer but it
12810 instantiated to nothing,
12811 value-initialize the object. This will
12812 only occur when the initializer was a
12813 pack expansion where the parameter packs
12814 used in that expansion were of length
12816 init
= build_value_init (TREE_TYPE (decl
),
12818 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
12819 init
= get_target_expr_sfinae (init
, complain
);
12825 /* Like tsubst, but deals with expressions. This function just replaces
12826 template parms; to finish processing the resultant expression, use
12827 tsubst_copy_and_build or tsubst_expr. */
12830 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12832 enum tree_code code
;
12835 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
12838 code
= TREE_CODE (t
);
12843 r
= retrieve_local_specialization (t
);
12845 if (r
== NULL_TREE
)
12847 /* We get here for a use of 'this' in an NSDMI. */
12848 if (DECL_NAME (t
) == this_identifier
12849 && current_function_decl
12850 && DECL_CONSTRUCTOR_P (current_function_decl
))
12851 return current_class_ptr
;
12853 /* This can happen for a parameter name used later in a function
12854 declaration (such as in a late-specified return type). Just
12855 make a dummy decl, since it's only used for its type. */
12856 gcc_assert (cp_unevaluated_operand
!= 0);
12857 r
= tsubst_decl (t
, args
, complain
);
12858 /* Give it the template pattern as its context; its true context
12859 hasn't been instantiated yet and this is good enough for
12861 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
12864 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12865 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12866 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
12867 return error_mark_node
;
12875 if (DECL_TEMPLATE_PARM_P (t
))
12876 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
12877 /* There is no need to substitute into namespace-scope
12879 if (DECL_NAMESPACE_SCOPE_P (t
))
12881 /* If ARGS is NULL, then T is known to be non-dependent. */
12882 if (args
== NULL_TREE
)
12883 return scalar_constant_value (t
);
12885 /* Unfortunately, we cannot just call lookup_name here.
12888 template <int I> int f() {
12890 struct S { void g() { E e = a; } };
12893 When we instantiate f<7>::S::g(), say, lookup_name is not
12894 clever enough to find f<7>::a. */
12896 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12897 /*entering_scope=*/0);
12899 for (v
= TYPE_VALUES (enum_type
);
12901 v
= TREE_CHAIN (v
))
12902 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
12903 return TREE_VALUE (v
);
12905 /* We didn't find the name. That should never happen; if
12906 name-lookup found it during preliminary parsing, we
12907 should find it again here during instantiation. */
12908 gcc_unreachable ();
12913 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
12915 /* Check for a local specialization set up by
12916 tsubst_pack_expansion. */
12917 if (tree r
= retrieve_local_specialization (t
))
12919 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12920 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12924 /* When retrieving a capture pack from a generic lambda, remove the
12925 lambda call op's own template argument list from ARGS. Only the
12926 template arguments active for the closure type should be used to
12927 retrieve the pack specialization. */
12928 if (LAMBDA_FUNCTION_P (current_function_decl
)
12929 && (template_class_depth (DECL_CONTEXT (t
))
12930 != TMPL_ARGS_DEPTH (args
)))
12931 args
= strip_innermost_template_args (args
, 1);
12933 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12934 tsubst_decl put in the hash table. */
12935 return retrieve_specialization (t
, args
, 0);
12938 if (DECL_CONTEXT (t
))
12942 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12943 /*entering_scope=*/1);
12944 if (ctx
!= DECL_CONTEXT (t
))
12946 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
12949 if (complain
& tf_error
)
12950 error ("using invalid field %qD", t
);
12951 return error_mark_node
;
12960 case FUNCTION_DECL
:
12961 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
12962 r
= tsubst (t
, args
, complain
, in_decl
);
12963 else if (local_variable_p (t
))
12965 r
= retrieve_local_specialization (t
);
12966 if (r
== NULL_TREE
)
12968 /* First try name lookup to find the instantiation. */
12969 r
= lookup_name (DECL_NAME (t
));
12972 /* Make sure that the one we found is the one we want. */
12973 tree ctx
= tsubst (DECL_CONTEXT (t
), args
,
12974 complain
, in_decl
);
12975 if (ctx
!= DECL_CONTEXT (r
))
12983 /* This can happen for a variable used in a
12984 late-specified return type of a local lambda, or for a
12985 local static or constant. Building a new VAR_DECL
12986 should be OK in all those cases. */
12987 r
= tsubst_decl (t
, args
, complain
);
12988 if (decl_maybe_constant_var_p (r
))
12990 /* We can't call cp_finish_decl, so handle the
12991 initializer by hand. */
12992 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
12993 complain
, in_decl
);
12994 if (!processing_template_decl
)
12995 init
= maybe_constant_init (init
);
12996 if (processing_template_decl
12997 ? potential_constant_expression (init
)
12998 : reduced_constant_expression_p (init
))
12999 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
13000 = TREE_CONSTANT (r
) = true;
13001 DECL_INITIAL (r
) = init
;
13003 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
13004 || decl_constant_var_p (r
)
13005 || errorcount
|| sorrycount
);
13006 if (!processing_template_decl
)
13008 if (TREE_STATIC (r
))
13009 rest_of_decl_compilation (r
, toplevel_bindings_p (),
13011 else if (decl_constant_var_p (r
))
13012 /* A use of a local constant decays to its value.
13013 FIXME update for core DR 696. */
13014 r
= scalar_constant_value (r
);
13017 /* Remember this for subsequent uses. */
13018 if (local_specializations
)
13019 register_local_specialization (r
, t
);
13024 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
13025 return error_mark_node
;
13028 case NAMESPACE_DECL
:
13032 /* An OVERLOAD will always be a non-dependent overload set; an
13033 overload set from function scope will just be represented with an
13034 IDENTIFIER_NODE, and from class scope with a BASELINK. */
13035 gcc_assert (!uses_template_parms (t
));
13039 return tsubst_baselink (t
, current_nonlambda_class_type (),
13040 args
, complain
, in_decl
);
13042 case TEMPLATE_DECL
:
13043 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
13044 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
13045 args
, complain
, in_decl
);
13046 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
13047 return tsubst (t
, args
, complain
, in_decl
);
13048 else if (DECL_CLASS_SCOPE_P (t
)
13049 && uses_template_parms (DECL_CONTEXT (t
)))
13051 /* Template template argument like the following example need
13054 template <template <class> class TT> struct C {};
13055 template <class T> struct D {
13056 template <class U> struct E {};
13061 We are processing the template argument `E' in #1 for
13062 the template instantiation #2. Originally, `E' is a
13063 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
13064 have to substitute this with one having context `D<int>'. */
13066 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
13067 return lookup_field (context
, DECL_NAME(t
), 0, false);
13070 /* Ordinary template template argument. */
13074 case REINTERPRET_CAST_EXPR
:
13075 case CONST_CAST_EXPR
:
13076 case STATIC_CAST_EXPR
:
13077 case DYNAMIC_CAST_EXPR
:
13078 case IMPLICIT_CONV_EXPR
:
13082 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13083 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13084 return build1 (code
, type
, op0
);
13088 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
13091 tree expanded
, op
= TREE_OPERAND (t
, 0);
13094 if (SIZEOF_EXPR_TYPE_P (t
))
13095 op
= TREE_TYPE (op
);
13097 ++cp_unevaluated_operand
;
13098 ++c_inhibit_evaluation_warnings
;
13099 /* We only want to compute the number of arguments. */
13100 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
13101 --cp_unevaluated_operand
;
13102 --c_inhibit_evaluation_warnings
;
13104 if (TREE_CODE (expanded
) == TREE_VEC
)
13105 len
= TREE_VEC_LENGTH (expanded
);
13107 if (expanded
== error_mark_node
)
13108 return error_mark_node
;
13109 else if (PACK_EXPANSION_P (expanded
)
13110 || (TREE_CODE (expanded
) == TREE_VEC
13112 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
13114 if (TREE_CODE (expanded
) == TREE_VEC
)
13115 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
13117 if (TYPE_P (expanded
))
13118 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
13119 complain
& tf_error
);
13121 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
13122 complain
& tf_error
);
13125 return build_int_cst (size_type_node
, len
);
13127 if (SIZEOF_EXPR_TYPE_P (t
))
13129 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
13130 args
, complain
, in_decl
);
13131 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
13132 r
= build1 (SIZEOF_EXPR
,
13133 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
13134 SIZEOF_EXPR_TYPE_P (r
) = 1;
13141 case TRUTH_NOT_EXPR
:
13144 case UNARY_PLUS_EXPR
: /* Unary + */
13146 case AT_ENCODE_EXPR
:
13150 case REALPART_EXPR
:
13151 case IMAGPART_EXPR
:
13154 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13155 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13156 return build1 (code
, type
, op0
);
13159 case COMPONENT_REF
:
13164 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13165 name
= TREE_OPERAND (t
, 1);
13166 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
13168 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
13169 complain
, in_decl
);
13170 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
13172 else if (TREE_CODE (name
) == SCOPE_REF
13173 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
13175 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
13176 complain
, in_decl
);
13177 name
= TREE_OPERAND (name
, 1);
13178 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
13179 complain
, in_decl
);
13180 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
13181 name
= build_qualified_name (/*type=*/NULL_TREE
,
13183 /*template_p=*/false);
13185 else if (BASELINK_P (name
))
13186 name
= tsubst_baselink (name
,
13187 non_reference (TREE_TYPE (object
)),
13191 name
= tsubst_copy (name
, args
, complain
, in_decl
);
13192 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
13198 case TRUNC_DIV_EXPR
:
13199 case CEIL_DIV_EXPR
:
13200 case FLOOR_DIV_EXPR
:
13201 case ROUND_DIV_EXPR
:
13202 case EXACT_DIV_EXPR
:
13206 case TRUNC_MOD_EXPR
:
13207 case FLOOR_MOD_EXPR
:
13208 case TRUTH_ANDIF_EXPR
:
13209 case TRUTH_ORIF_EXPR
:
13210 case TRUTH_AND_EXPR
:
13211 case TRUTH_OR_EXPR
:
13224 case COMPOUND_EXPR
:
13227 case PREDECREMENT_EXPR
:
13228 case PREINCREMENT_EXPR
:
13229 case POSTDECREMENT_EXPR
:
13230 case POSTINCREMENT_EXPR
:
13232 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13233 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13234 return build_nt (code
, op0
, op1
);
13239 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13240 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13241 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
13242 QUALIFIED_NAME_IS_TEMPLATE (t
));
13247 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13248 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13249 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
13254 int n
= VL_EXP_OPERAND_LENGTH (t
);
13255 tree result
= build_vl_exp (CALL_EXPR
, n
);
13257 for (i
= 0; i
< n
; i
++)
13258 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
13259 complain
, in_decl
);
13265 case PSEUDO_DTOR_EXPR
:
13266 case VEC_PERM_EXPR
:
13268 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13269 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13270 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
13271 r
= build_nt (code
, op0
, op1
, op2
);
13272 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13278 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13279 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13280 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
13281 r
= build_nt (code
, op0
, op1
, op2
);
13282 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
13288 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13289 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13290 r
= build_nt (code
, op0
, op1
);
13291 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
13292 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
13296 case TEMPLATE_ID_EXPR
:
13298 /* Substituted template arguments */
13299 tree fn
= TREE_OPERAND (t
, 0);
13300 tree targs
= TREE_OPERAND (t
, 1);
13302 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
13304 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
13306 return lookup_template_function (fn
, targs
);
13311 tree purpose
, value
, chain
;
13313 if (t
== void_list_node
)
13316 purpose
= TREE_PURPOSE (t
);
13318 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
13319 value
= TREE_VALUE (t
);
13321 value
= tsubst_copy (value
, args
, complain
, in_decl
);
13322 chain
= TREE_CHAIN (t
);
13323 if (chain
&& chain
!= void_type_node
)
13324 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
13325 if (purpose
== TREE_PURPOSE (t
)
13326 && value
== TREE_VALUE (t
)
13327 && chain
== TREE_CHAIN (t
))
13329 return tree_cons (purpose
, value
, chain
);
13334 case ENUMERAL_TYPE
:
13336 case TEMPLATE_TYPE_PARM
:
13337 case TEMPLATE_TEMPLATE_PARM
:
13338 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13339 case TEMPLATE_PARM_INDEX
:
13341 case REFERENCE_TYPE
:
13343 case FUNCTION_TYPE
:
13346 case TYPENAME_TYPE
:
13347 case UNBOUND_CLASS_TEMPLATE
:
13349 case DECLTYPE_TYPE
:
13351 return tsubst (t
, args
, complain
, in_decl
);
13355 /* Fall through. */
13356 case IDENTIFIER_NODE
:
13357 if (IDENTIFIER_TYPENAME_P (t
))
13359 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13360 return mangle_conv_op_name_for_type (new_type
);
13366 /* This is handled by tsubst_copy_and_build. */
13367 gcc_unreachable ();
13371 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13372 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13373 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
13376 case CLEANUP_POINT_EXPR
:
13377 /* We shouldn't have built any of these during initial template
13378 generation. Instead, they should be built during instantiation
13379 in response to the saved STMT_IS_FULL_EXPR_P setting. */
13380 gcc_unreachable ();
13384 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13385 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13386 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13387 r
= build2 (code
, type
, op0
, op1
);
13388 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
13389 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
13390 && !(complain
& tf_error
))
13391 return error_mark_node
;
13395 case EXPR_PACK_EXPANSION
:
13396 error ("invalid use of pack expansion expression");
13397 return error_mark_node
;
13399 case NONTYPE_ARGUMENT_PACK
:
13400 error ("use %<...%> to expand argument pack");
13401 return error_mark_node
;
13404 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
13412 /* Instantiate any typedefs in the type. */
13413 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13414 r
= fold_convert (type
, t
);
13415 gcc_assert (TREE_CODE (r
) == code
);
13420 /* These can sometimes show up in a partial instantiation, but never
13421 involve template parms. */
13422 gcc_assert (!uses_template_parms (t
));
13426 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
13427 gcc_checking_assert (false);
13432 /* Like tsubst_copy, but specifically for OpenMP clauses. */
13435 tsubst_omp_clauses (tree clauses
, bool declare_simd
,
13436 tree args
, tsubst_flags_t complain
, tree in_decl
)
13438 tree new_clauses
= NULL
, nc
, oc
;
13440 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
13442 nc
= copy_node (oc
);
13443 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
13446 switch (OMP_CLAUSE_CODE (nc
))
13448 case OMP_CLAUSE_LASTPRIVATE
:
13449 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
13451 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
13452 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
13453 in_decl
, /*integral_constant_expression_p=*/false);
13454 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
13455 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
13458 case OMP_CLAUSE_PRIVATE
:
13459 case OMP_CLAUSE_SHARED
:
13460 case OMP_CLAUSE_FIRSTPRIVATE
:
13461 case OMP_CLAUSE_COPYIN
:
13462 case OMP_CLAUSE_COPYPRIVATE
:
13463 case OMP_CLAUSE_IF
:
13464 case OMP_CLAUSE_NUM_THREADS
:
13465 case OMP_CLAUSE_SCHEDULE
:
13466 case OMP_CLAUSE_COLLAPSE
:
13467 case OMP_CLAUSE_FINAL
:
13468 case OMP_CLAUSE_DEPEND
:
13469 case OMP_CLAUSE_FROM
:
13470 case OMP_CLAUSE_TO
:
13471 case OMP_CLAUSE_UNIFORM
:
13472 case OMP_CLAUSE_MAP
:
13473 case OMP_CLAUSE_DEVICE
:
13474 case OMP_CLAUSE_DIST_SCHEDULE
:
13475 case OMP_CLAUSE_NUM_TEAMS
:
13476 case OMP_CLAUSE_THREAD_LIMIT
:
13477 case OMP_CLAUSE_SAFELEN
:
13478 case OMP_CLAUSE_SIMDLEN
:
13479 OMP_CLAUSE_OPERAND (nc
, 0)
13480 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
13481 in_decl
, /*integral_constant_expression_p=*/false);
13483 case OMP_CLAUSE_REDUCTION
:
13484 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
13486 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
13487 if (TREE_CODE (placeholder
) == SCOPE_REF
)
13489 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
13490 complain
, in_decl
);
13491 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
13492 = build_qualified_name (NULL_TREE
, scope
,
13493 TREE_OPERAND (placeholder
, 1),
13497 gcc_assert (identifier_p (placeholder
));
13499 OMP_CLAUSE_OPERAND (nc
, 0)
13500 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
13501 in_decl
, /*integral_constant_expression_p=*/false);
13503 case OMP_CLAUSE_LINEAR
:
13504 case OMP_CLAUSE_ALIGNED
:
13505 OMP_CLAUSE_OPERAND (nc
, 0)
13506 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
13507 in_decl
, /*integral_constant_expression_p=*/false);
13508 OMP_CLAUSE_OPERAND (nc
, 1)
13509 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
13510 in_decl
, /*integral_constant_expression_p=*/false);
13513 case OMP_CLAUSE_NOWAIT
:
13514 case OMP_CLAUSE_ORDERED
:
13515 case OMP_CLAUSE_DEFAULT
:
13516 case OMP_CLAUSE_UNTIED
:
13517 case OMP_CLAUSE_MERGEABLE
:
13518 case OMP_CLAUSE_INBRANCH
:
13519 case OMP_CLAUSE_NOTINBRANCH
:
13520 case OMP_CLAUSE_PROC_BIND
:
13521 case OMP_CLAUSE_FOR
:
13522 case OMP_CLAUSE_PARALLEL
:
13523 case OMP_CLAUSE_SECTIONS
:
13524 case OMP_CLAUSE_TASKGROUP
:
13527 gcc_unreachable ();
13531 new_clauses
= nreverse (new_clauses
);
13533 new_clauses
= finish_omp_clauses (new_clauses
);
13534 return new_clauses
;
13537 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
13540 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
13543 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
13545 tree purpose
, value
, chain
;
13550 if (TREE_CODE (t
) != TREE_LIST
)
13551 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13552 /*function_p=*/false,
13553 /*integral_constant_expression_p=*/false);
13555 if (t
== void_list_node
)
13558 purpose
= TREE_PURPOSE (t
);
13560 purpose
= RECUR (purpose
);
13561 value
= TREE_VALUE (t
);
13564 if (TREE_CODE (value
) != LABEL_DECL
)
13565 value
= RECUR (value
);
13568 value
= lookup_label (DECL_NAME (value
));
13569 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
13570 TREE_USED (value
) = 1;
13573 chain
= TREE_CHAIN (t
);
13574 if (chain
&& chain
!= void_type_node
)
13575 chain
= RECUR (chain
);
13576 return tree_cons (purpose
, value
, chain
);
13580 /* Substitute one OMP_FOR iterator. */
13583 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
13584 tree condv
, tree incrv
, tree
*clauses
,
13585 tree args
, tsubst_flags_t complain
, tree in_decl
,
13586 bool integral_constant_expression_p
)
13588 #define RECUR(NODE) \
13589 tsubst_expr ((NODE), args, complain, in_decl, \
13590 integral_constant_expression_p)
13591 tree decl
, init
, cond
, incr
;
13593 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
13594 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
13595 decl
= TREE_OPERAND (init
, 0);
13596 init
= TREE_OPERAND (init
, 1);
13597 tree decl_expr
= NULL_TREE
;
13598 if (init
&& TREE_CODE (init
) == DECL_EXPR
)
13600 /* We need to jump through some hoops to handle declarations in the
13601 for-init-statement, since we might need to handle auto deduction,
13602 but we need to keep control of initialization. */
13604 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
13605 decl
= tsubst_decl (decl
, args
, complain
);
13608 decl
= RECUR (decl
);
13609 init
= RECUR (init
);
13611 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
13612 if (auto_node
&& init
)
13614 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
);
13616 gcc_assert (!type_dependent_expression_p (decl
));
13618 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
13622 /* Declare the variable, but don't let that initialize it. */
13623 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
13624 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
13626 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
13629 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
13630 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
13631 if (TREE_CODE (incr
) == MODIFY_EXPR
)
13633 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
13634 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
13635 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
13636 NOP_EXPR
, rhs
, complain
);
13639 incr
= RECUR (incr
);
13640 TREE_VEC_ELT (declv
, i
) = decl
;
13641 TREE_VEC_ELT (initv
, i
) = init
;
13642 TREE_VEC_ELT (condv
, i
) = cond
;
13643 TREE_VEC_ELT (incrv
, i
) = incr
;
13649 /* Declare and initialize the variable. */
13656 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
13658 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
13659 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
13660 && OMP_CLAUSE_DECL (c
) == decl
)
13662 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
13663 && OMP_CLAUSE_DECL (c
) == decl
)
13664 error ("iteration variable %qD should not be firstprivate", decl
);
13665 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
13666 && OMP_CLAUSE_DECL (c
) == decl
)
13667 error ("iteration variable %qD should not be reduction", decl
);
13671 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
13672 OMP_CLAUSE_DECL (c
) = decl
;
13673 c
= finish_omp_clauses (c
);
13676 OMP_CLAUSE_CHAIN (c
) = *clauses
;
13681 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
13682 if (COMPARISON_CLASS_P (cond
))
13684 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
13685 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
13686 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
13689 cond
= RECUR (cond
);
13690 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
13691 switch (TREE_CODE (incr
))
13693 case PREINCREMENT_EXPR
:
13694 case PREDECREMENT_EXPR
:
13695 case POSTINCREMENT_EXPR
:
13696 case POSTDECREMENT_EXPR
:
13697 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
13698 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
13701 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
13702 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
13704 tree rhs
= TREE_OPERAND (incr
, 1);
13705 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
13706 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
13707 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
13708 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
13709 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
13713 incr
= RECUR (incr
);
13716 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
13717 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
13719 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
13720 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
13721 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
13722 TREE_TYPE (decl
), lhs
,
13723 RECUR (TREE_OPERAND (incr
, 2))));
13725 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
13726 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
13727 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
13729 tree rhs
= TREE_OPERAND (incr
, 2);
13730 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
13731 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
13732 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
13733 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
13734 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
13738 incr
= RECUR (incr
);
13741 incr
= RECUR (incr
);
13745 TREE_VEC_ELT (declv
, i
) = decl
;
13746 TREE_VEC_ELT (initv
, i
) = init
;
13747 TREE_VEC_ELT (condv
, i
) = cond
;
13748 TREE_VEC_ELT (incrv
, i
) = incr
;
13752 /* Like tsubst_copy for expressions, etc. but also does semantic
13756 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
13757 bool integral_constant_expression_p
)
13759 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13760 #define RECUR(NODE) \
13761 tsubst_expr ((NODE), args, complain, in_decl, \
13762 integral_constant_expression_p)
13768 if (t
== NULL_TREE
|| t
== error_mark_node
)
13771 loc
= input_location
;
13772 if (EXPR_HAS_LOCATION (t
))
13773 input_location
= EXPR_LOCATION (t
);
13774 if (STATEMENT_CODE_P (TREE_CODE (t
)))
13775 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
13777 switch (TREE_CODE (t
))
13779 case STATEMENT_LIST
:
13781 tree_stmt_iterator i
;
13782 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
13783 RECUR (tsi_stmt (i
));
13787 case CTOR_INITIALIZER
:
13788 finish_mem_initializers (tsubst_initializer_list
13789 (TREE_OPERAND (t
, 0), args
));
13793 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
13797 tmp
= RECUR (EXPR_STMT_EXPR (t
));
13798 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
13799 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
13801 finish_expr_stmt (tmp
);
13805 do_using_directive (USING_STMT_NAMESPACE (t
));
13810 tree decl
, pattern_decl
;
13813 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
13814 if (TREE_CODE (decl
) == LABEL_DECL
)
13815 finish_label_decl (DECL_NAME (decl
));
13816 else if (TREE_CODE (decl
) == USING_DECL
)
13818 tree scope
= USING_DECL_SCOPE (decl
);
13819 tree name
= DECL_NAME (decl
);
13822 scope
= tsubst (scope
, args
, complain
, in_decl
);
13823 decl
= lookup_qualified_name (scope
, name
,
13824 /*is_type_p=*/false,
13825 /*complain=*/false);
13826 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
13827 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
13829 do_local_using_decl (decl
, scope
, name
);
13831 else if (DECL_PACK_P (decl
))
13833 /* Don't build up decls for a variadic capture proxy, we'll
13834 instantiate the elements directly as needed. */
13839 init
= DECL_INITIAL (decl
);
13840 decl
= tsubst (decl
, args
, complain
, in_decl
);
13841 if (decl
!= error_mark_node
)
13843 /* By marking the declaration as instantiated, we avoid
13844 trying to instantiate it. Since instantiate_decl can't
13845 handle local variables, and since we've already done
13846 all that needs to be done, that's the right thing to
13849 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13851 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
13852 /* Anonymous aggregates are a special case. */
13853 finish_anon_union (decl
);
13854 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
13856 DECL_CONTEXT (decl
) = current_function_decl
;
13857 if (DECL_NAME (decl
) == this_identifier
)
13859 tree lam
= DECL_CONTEXT (current_function_decl
);
13860 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
13861 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
13863 insert_capture_proxy (decl
);
13865 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
13866 /* We already did a pushtag. */;
13867 else if (TREE_CODE (decl
) == FUNCTION_DECL
13868 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
13869 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
13871 DECL_CONTEXT (decl
) = NULL_TREE
;
13873 DECL_CONTEXT (decl
) = current_function_decl
;
13874 cp_check_omp_declare_reduction (decl
);
13878 int const_init
= false;
13879 maybe_push_decl (decl
);
13881 && DECL_PRETTY_FUNCTION_P (decl
))
13883 /* For __PRETTY_FUNCTION__ we have to adjust the
13885 const char *const name
13886 = cxx_printable_name (current_function_decl
, 2);
13887 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
13890 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
13893 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13895 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
13904 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
13905 RECUR (FOR_INIT_STMT (t
));
13906 finish_for_init_stmt (stmt
);
13907 tmp
= RECUR (FOR_COND (t
));
13908 finish_for_cond (tmp
, stmt
, false);
13909 tmp
= RECUR (FOR_EXPR (t
));
13910 finish_for_expr (tmp
, stmt
);
13911 RECUR (FOR_BODY (t
));
13912 finish_for_stmt (stmt
);
13915 case RANGE_FOR_STMT
:
13918 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
13919 decl
= RANGE_FOR_DECL (t
);
13920 decl
= tsubst (decl
, args
, complain
, in_decl
);
13921 maybe_push_decl (decl
);
13922 expr
= RECUR (RANGE_FOR_EXPR (t
));
13923 stmt
= cp_convert_range_for (stmt
, decl
, expr
, RANGE_FOR_IVDEP (t
));
13924 RECUR (RANGE_FOR_BODY (t
));
13925 finish_for_stmt (stmt
);
13930 stmt
= begin_while_stmt ();
13931 tmp
= RECUR (WHILE_COND (t
));
13932 finish_while_stmt_cond (tmp
, stmt
, false);
13933 RECUR (WHILE_BODY (t
));
13934 finish_while_stmt (stmt
);
13938 stmt
= begin_do_stmt ();
13939 RECUR (DO_BODY (t
));
13940 finish_do_body (stmt
);
13941 tmp
= RECUR (DO_COND (t
));
13942 finish_do_stmt (tmp
, stmt
, false);
13946 stmt
= begin_if_stmt ();
13947 tmp
= RECUR (IF_COND (t
));
13948 finish_if_stmt_cond (tmp
, stmt
);
13949 RECUR (THEN_CLAUSE (t
));
13950 finish_then_clause (stmt
);
13952 if (ELSE_CLAUSE (t
))
13954 begin_else_clause (stmt
);
13955 RECUR (ELSE_CLAUSE (t
));
13956 finish_else_clause (stmt
);
13959 finish_if_stmt (stmt
);
13963 if (BIND_EXPR_BODY_BLOCK (t
))
13964 stmt
= begin_function_body ();
13966 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
13967 ? BCS_TRY_BLOCK
: 0);
13969 RECUR (BIND_EXPR_BODY (t
));
13971 if (BIND_EXPR_BODY_BLOCK (t
))
13972 finish_function_body (stmt
);
13974 finish_compound_stmt (stmt
);
13978 finish_break_stmt ();
13981 case CONTINUE_STMT
:
13982 finish_continue_stmt ();
13986 stmt
= begin_switch_stmt ();
13987 tmp
= RECUR (SWITCH_STMT_COND (t
));
13988 finish_switch_cond (tmp
, stmt
);
13989 RECUR (SWITCH_STMT_BODY (t
));
13990 finish_switch_stmt (stmt
);
13993 case CASE_LABEL_EXPR
:
13995 tree low
= RECUR (CASE_LOW (t
));
13996 tree high
= RECUR (CASE_HIGH (t
));
13997 finish_case_label (EXPR_LOCATION (t
), low
, high
);
14003 tree decl
= LABEL_EXPR_LABEL (t
);
14006 label
= finish_label_stmt (DECL_NAME (decl
));
14007 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
14008 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
14013 tmp
= GOTO_DESTINATION (t
);
14014 if (TREE_CODE (tmp
) != LABEL_DECL
)
14015 /* Computed goto's must be tsubst'd into. On the other hand,
14016 non-computed gotos must not be; the identifier in question
14017 will have no binding. */
14020 tmp
= DECL_NAME (tmp
);
14021 finish_goto_stmt (tmp
);
14026 tree string
= RECUR (ASM_STRING (t
));
14027 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
14028 complain
, in_decl
);
14029 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
14030 complain
, in_decl
);
14031 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
14032 complain
, in_decl
);
14033 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
14034 complain
, in_decl
);
14035 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
14037 tree asm_expr
= tmp
;
14038 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
14039 asm_expr
= TREE_OPERAND (asm_expr
, 0);
14040 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
14047 stmt
= begin_try_block ();
14048 RECUR (TRY_STMTS (t
));
14049 finish_cleanup_try_block (stmt
);
14050 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
14054 tree compound_stmt
= NULL_TREE
;
14056 if (FN_TRY_BLOCK_P (t
))
14057 stmt
= begin_function_try_block (&compound_stmt
);
14059 stmt
= begin_try_block ();
14061 RECUR (TRY_STMTS (t
));
14063 if (FN_TRY_BLOCK_P (t
))
14064 finish_function_try_block (stmt
);
14066 finish_try_block (stmt
);
14068 RECUR (TRY_HANDLERS (t
));
14069 if (FN_TRY_BLOCK_P (t
))
14070 finish_function_handler_sequence (stmt
, compound_stmt
);
14072 finish_handler_sequence (stmt
);
14078 tree decl
= HANDLER_PARMS (t
);
14082 decl
= tsubst (decl
, args
, complain
, in_decl
);
14083 /* Prevent instantiate_decl from trying to instantiate
14084 this variable. We've already done all that needs to be
14086 if (decl
!= error_mark_node
)
14087 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
14089 stmt
= begin_handler ();
14090 finish_handler_parms (decl
, stmt
);
14091 RECUR (HANDLER_BODY (t
));
14092 finish_handler (stmt
);
14097 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
14098 if (CLASS_TYPE_P (tmp
))
14100 /* Local classes are not independent templates; they are
14101 instantiated along with their containing function. And this
14102 way we don't have to deal with pushing out of one local class
14103 to instantiate a member of another local class. */
14105 /* Closures are handled by the LAMBDA_EXPR. */
14106 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
14107 complete_type (tmp
);
14108 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
14109 if (!DECL_ARTIFICIAL (fn
))
14110 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
14114 case STATIC_ASSERT
:
14118 ++c_inhibit_evaluation_warnings
;
14120 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
14123 /*integral_constant_expression_p=*/true);
14124 --c_inhibit_evaluation_warnings
;
14126 finish_static_assert (condition
,
14127 STATIC_ASSERT_MESSAGE (t
),
14128 STATIC_ASSERT_SOURCE_LOCATION (t
),
14129 /*member_p=*/false);
14134 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), false,
14135 args
, complain
, in_decl
);
14136 stmt
= begin_omp_parallel ();
14137 RECUR (OMP_PARALLEL_BODY (t
));
14138 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
14139 = OMP_PARALLEL_COMBINED (t
);
14143 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), false,
14144 args
, complain
, in_decl
);
14145 stmt
= begin_omp_task ();
14146 RECUR (OMP_TASK_BODY (t
));
14147 finish_omp_task (tmp
, stmt
);
14154 case OMP_DISTRIBUTE
:
14156 tree clauses
, body
, pre_body
;
14157 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
14158 tree incrv
= NULL_TREE
;
14161 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), false,
14162 args
, complain
, in_decl
);
14163 if (OMP_FOR_INIT (t
) != NULL_TREE
)
14165 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
14166 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
14167 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
14168 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
14171 stmt
= begin_omp_structured_block ();
14173 pre_body
= push_stmt_list ();
14174 RECUR (OMP_FOR_PRE_BODY (t
));
14175 pre_body
= pop_stmt_list (pre_body
);
14177 if (OMP_FOR_INIT (t
) != NULL_TREE
)
14178 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
14179 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
14180 &clauses
, args
, complain
, in_decl
,
14181 integral_constant_expression_p
);
14183 body
= push_stmt_list ();
14184 RECUR (OMP_FOR_BODY (t
));
14185 body
= pop_stmt_list (body
);
14187 if (OMP_FOR_INIT (t
) != NULL_TREE
)
14188 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
, initv
,
14189 condv
, incrv
, body
, pre_body
, clauses
);
14192 t
= make_node (TREE_CODE (t
));
14193 TREE_TYPE (t
) = void_type_node
;
14194 OMP_FOR_BODY (t
) = body
;
14195 OMP_FOR_PRE_BODY (t
) = pre_body
;
14196 OMP_FOR_CLAUSES (t
) = clauses
;
14197 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
14201 add_stmt (finish_omp_structured_block (stmt
));
14208 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false,
14209 args
, complain
, in_decl
);
14210 stmt
= push_stmt_list ();
14211 RECUR (OMP_BODY (t
));
14212 stmt
= pop_stmt_list (stmt
);
14215 OMP_BODY (t
) = stmt
;
14216 OMP_CLAUSES (t
) = tmp
;
14220 case OMP_TARGET_DATA
:
14222 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false,
14223 args
, complain
, in_decl
);
14224 keep_next_level (true);
14225 stmt
= begin_omp_structured_block ();
14227 RECUR (OMP_BODY (t
));
14228 stmt
= finish_omp_structured_block (stmt
);
14231 OMP_BODY (t
) = stmt
;
14232 OMP_CLAUSES (t
) = tmp
;
14236 case OMP_TARGET_UPDATE
:
14237 tmp
= tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t
), false,
14238 args
, complain
, in_decl
);
14240 OMP_TARGET_UPDATE_CLAUSES (t
) = tmp
;
14247 case OMP_TASKGROUP
:
14249 stmt
= push_stmt_list ();
14250 RECUR (OMP_BODY (t
));
14251 stmt
= pop_stmt_list (stmt
);
14254 OMP_BODY (t
) = stmt
;
14259 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
14260 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
14262 tree op1
= TREE_OPERAND (t
, 1);
14263 tree rhs1
= NULL_TREE
;
14265 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
14267 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
14268 op1
= TREE_OPERAND (op1
, 1);
14270 lhs
= RECUR (TREE_OPERAND (op1
, 0));
14271 rhs
= RECUR (TREE_OPERAND (op1
, 1));
14272 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
14273 NULL_TREE
, NULL_TREE
, rhs1
,
14274 OMP_ATOMIC_SEQ_CST (t
));
14278 tree op1
= TREE_OPERAND (t
, 1);
14279 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
14280 tree rhs1
= NULL_TREE
;
14281 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
14282 enum tree_code opcode
= NOP_EXPR
;
14283 if (code
== OMP_ATOMIC_READ
)
14285 v
= RECUR (TREE_OPERAND (op1
, 0));
14286 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
14288 else if (code
== OMP_ATOMIC_CAPTURE_OLD
14289 || code
== OMP_ATOMIC_CAPTURE_NEW
)
14291 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
14292 v
= RECUR (TREE_OPERAND (op1
, 0));
14293 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
14294 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
14296 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
14297 op11
= TREE_OPERAND (op11
, 1);
14299 lhs
= RECUR (TREE_OPERAND (op11
, 0));
14300 rhs
= RECUR (TREE_OPERAND (op11
, 1));
14301 opcode
= TREE_CODE (op11
);
14302 if (opcode
== MODIFY_EXPR
)
14308 lhs
= RECUR (TREE_OPERAND (op1
, 0));
14309 rhs
= RECUR (TREE_OPERAND (op1
, 1));
14311 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
14312 OMP_ATOMIC_SEQ_CST (t
));
14316 case TRANSACTION_EXPR
:
14319 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
14320 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
14322 if (TRANSACTION_EXPR_IS_STMT (t
))
14324 tree body
= TRANSACTION_EXPR_BODY (t
);
14325 tree noex
= NULL_TREE
;
14326 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
14328 noex
= MUST_NOT_THROW_COND (body
);
14329 if (noex
== NULL_TREE
)
14330 noex
= boolean_true_node
;
14331 body
= TREE_OPERAND (body
, 0);
14333 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
14335 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
14339 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
14340 RECUR (TRANSACTION_EXPR_BODY (t
)),
14347 case MUST_NOT_THROW_EXPR
:
14349 tree op0
= RECUR (TREE_OPERAND (t
, 0));
14350 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
14351 RETURN (build_must_not_throw_expr (op0
, cond
));
14354 case EXPR_PACK_EXPANSION
:
14355 error ("invalid use of pack expansion expression");
14356 RETURN (error_mark_node
);
14358 case NONTYPE_ARGUMENT_PACK
:
14359 error ("use %<...%> to expand argument pack");
14360 RETURN (error_mark_node
);
14362 case CILK_SPAWN_STMT
:
14363 cfun
->calls_cilk_spawn
= 1;
14364 RETURN (build_cilk_spawn (EXPR_LOCATION (t
), RECUR (CILK_SPAWN_FN (t
))));
14366 case CILK_SYNC_STMT
:
14367 RETURN (build_cilk_sync ());
14369 case COMPOUND_EXPR
:
14370 tmp
= RECUR (TREE_OPERAND (t
, 0));
14371 if (tmp
== NULL_TREE
)
14372 /* If the first operand was a statement, we're done with it. */
14373 RETURN (RECUR (TREE_OPERAND (t
, 1)));
14374 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
14375 RECUR (TREE_OPERAND (t
, 1)),
14378 case ANNOTATE_EXPR
:
14379 tmp
= RECUR (TREE_OPERAND (t
, 0));
14380 RETURN (build2_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
14381 TREE_TYPE (tmp
), tmp
, RECUR (TREE_OPERAND (t
, 1))));
14384 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
14386 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
14387 /*function_p=*/false,
14388 integral_constant_expression_p
));
14391 RETURN (NULL_TREE
);
14393 input_location
= loc
;
14399 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
14400 function. For description of the body see comment above
14401 cp_parser_omp_declare_reduction_exprs. */
14404 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14406 if (t
== NULL_TREE
|| t
== error_mark_node
)
14409 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
14411 tree_stmt_iterator tsi
;
14414 memset (stmts
, 0, sizeof stmts
);
14415 for (i
= 0, tsi
= tsi_start (t
);
14416 i
< 7 && !tsi_end_p (tsi
);
14417 i
++, tsi_next (&tsi
))
14418 stmts
[i
] = tsi_stmt (tsi
);
14419 gcc_assert (tsi_end_p (tsi
));
14423 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
14424 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
14425 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
14426 args
, complain
, in_decl
);
14427 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
14428 args
, complain
, in_decl
);
14429 DECL_CONTEXT (omp_out
) = current_function_decl
;
14430 DECL_CONTEXT (omp_in
) = current_function_decl
;
14431 keep_next_level (true);
14432 tree block
= begin_omp_structured_block ();
14433 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
14434 block
= finish_omp_structured_block (block
);
14435 block
= maybe_cleanup_point_expr_void (block
);
14436 add_decl_expr (omp_out
);
14437 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
14438 TREE_NO_WARNING (omp_out
) = 1;
14439 add_decl_expr (omp_in
);
14440 finish_expr_stmt (block
);
14444 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
14445 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
14446 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
14447 args
, complain
, in_decl
);
14448 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
14449 args
, complain
, in_decl
);
14450 DECL_CONTEXT (omp_priv
) = current_function_decl
;
14451 DECL_CONTEXT (omp_orig
) = current_function_decl
;
14452 keep_next_level (true);
14453 tree block
= begin_omp_structured_block ();
14454 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
14455 block
= finish_omp_structured_block (block
);
14456 block
= maybe_cleanup_point_expr_void (block
);
14457 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
14458 add_decl_expr (omp_priv
);
14459 add_decl_expr (omp_orig
);
14460 finish_expr_stmt (block
);
14462 add_decl_expr (omp_orig
);
14466 /* T is a postfix-expression that is not being used in a function
14467 call. Return the substituted version of T. */
14470 tsubst_non_call_postfix_expression (tree t
, tree args
,
14471 tsubst_flags_t complain
,
14474 if (TREE_CODE (t
) == SCOPE_REF
)
14475 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
14476 /*done=*/false, /*address_p=*/false);
14478 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
14479 /*function_p=*/false,
14480 /*integral_constant_expression_p=*/false);
14485 /* Like tsubst but deals with expressions and performs semantic
14486 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
14489 tsubst_copy_and_build (tree t
,
14491 tsubst_flags_t complain
,
14494 bool integral_constant_expression_p
)
14496 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
14497 #define RECUR(NODE) \
14498 tsubst_copy_and_build (NODE, args, complain, in_decl, \
14499 /*function_p=*/false, \
14500 integral_constant_expression_p)
14505 if (t
== NULL_TREE
|| t
== error_mark_node
)
14508 loc
= input_location
;
14509 if (EXPR_HAS_LOCATION (t
))
14510 input_location
= EXPR_LOCATION (t
);
14512 /* N3276 decltype magic only applies to calls at the top level or on the
14513 right side of a comma. */
14514 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
14515 complain
&= ~tf_decltype
;
14517 switch (TREE_CODE (t
))
14521 /* Fall through. */
14522 case IDENTIFIER_NODE
:
14526 bool non_integral_constant_expression_p
;
14527 const char *error_msg
;
14529 if (IDENTIFIER_TYPENAME_P (t
))
14531 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14532 t
= mangle_conv_op_name_for_type (new_type
);
14535 /* Look up the name. */
14536 decl
= lookup_name (t
);
14538 /* By convention, expressions use ERROR_MARK_NODE to indicate
14539 failure, not NULL_TREE. */
14540 if (decl
== NULL_TREE
)
14541 decl
= error_mark_node
;
14543 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
14545 integral_constant_expression_p
,
14546 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
14547 &non_integral_constant_expression_p
,
14548 /*template_p=*/false,
14550 /*address_p=*/false,
14551 /*template_arg_p=*/false,
14556 if (!function_p
&& identifier_p (decl
))
14558 if (complain
& tf_error
)
14559 unqualified_name_lookup_error (decl
);
14560 decl
= error_mark_node
;
14565 case TEMPLATE_ID_EXPR
:
14568 tree templ
= RECUR (TREE_OPERAND (t
, 0));
14569 tree targs
= TREE_OPERAND (t
, 1);
14572 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
14574 if (TREE_CODE (templ
) == COMPONENT_REF
)
14576 object
= TREE_OPERAND (templ
, 0);
14577 templ
= TREE_OPERAND (templ
, 1);
14580 object
= NULL_TREE
;
14581 templ
= lookup_template_function (templ
, targs
);
14584 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
14585 object
, templ
, NULL_TREE
));
14587 RETURN (baselink_for_fns (templ
));
14592 tree r
= RECUR (TREE_OPERAND (t
, 0));
14594 if (REFERENCE_REF_P (t
))
14596 /* A type conversion to reference type will be enclosed in
14597 such an indirect ref, but the substitution of the cast
14598 will have also added such an indirect ref. */
14599 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
14600 r
= convert_from_reference (r
);
14603 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
14604 complain
|decltype_flag
);
14610 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14611 tree op0
= RECUR (TREE_OPERAND (t
, 0));
14612 RETURN (build_nop (type
, op0
));
14615 case IMPLICIT_CONV_EXPR
:
14617 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14618 tree expr
= RECUR (TREE_OPERAND (t
, 0));
14619 int flags
= LOOKUP_IMPLICIT
;
14620 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
14621 flags
= LOOKUP_NORMAL
;
14622 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
14628 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14629 tree op0
= RECUR (TREE_OPERAND (t
, 0));
14630 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
14634 case REINTERPRET_CAST_EXPR
:
14635 case CONST_CAST_EXPR
:
14636 case DYNAMIC_CAST_EXPR
:
14637 case STATIC_CAST_EXPR
:
14640 tree op
, r
= NULL_TREE
;
14642 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14643 if (integral_constant_expression_p
14644 && !cast_valid_in_integral_constant_expression_p (type
))
14646 if (complain
& tf_error
)
14647 error ("a cast to a type other than an integral or "
14648 "enumeration type cannot appear in a constant-expression");
14649 RETURN (error_mark_node
);
14652 op
= RECUR (TREE_OPERAND (t
, 0));
14654 warning_sentinel
s(warn_useless_cast
);
14655 switch (TREE_CODE (t
))
14658 r
= build_functional_cast (type
, op
, complain
);
14660 case REINTERPRET_CAST_EXPR
:
14661 r
= build_reinterpret_cast (type
, op
, complain
);
14663 case CONST_CAST_EXPR
:
14664 r
= build_const_cast (type
, op
, complain
);
14666 case DYNAMIC_CAST_EXPR
:
14667 r
= build_dynamic_cast (type
, op
, complain
);
14669 case STATIC_CAST_EXPR
:
14670 r
= build_static_cast (type
, op
, complain
);
14673 gcc_unreachable ();
14679 case POSTDECREMENT_EXPR
:
14680 case POSTINCREMENT_EXPR
:
14681 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14682 args
, complain
, in_decl
);
14683 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
14684 complain
|decltype_flag
));
14686 case PREDECREMENT_EXPR
:
14687 case PREINCREMENT_EXPR
:
14691 case TRUTH_NOT_EXPR
:
14692 case UNARY_PLUS_EXPR
: /* Unary + */
14693 case REALPART_EXPR
:
14694 case IMAGPART_EXPR
:
14695 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
14696 RECUR (TREE_OPERAND (t
, 0)),
14697 complain
|decltype_flag
));
14699 case FIX_TRUNC_EXPR
:
14700 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
14704 op1
= TREE_OPERAND (t
, 0);
14705 if (TREE_CODE (op1
) == LABEL_DECL
)
14706 RETURN (finish_label_address_expr (DECL_NAME (op1
),
14707 EXPR_LOCATION (op1
)));
14708 if (TREE_CODE (op1
) == SCOPE_REF
)
14709 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
14710 /*done=*/true, /*address_p=*/true);
14712 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
14714 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
14715 complain
|decltype_flag
));
14720 case TRUNC_DIV_EXPR
:
14721 case CEIL_DIV_EXPR
:
14722 case FLOOR_DIV_EXPR
:
14723 case ROUND_DIV_EXPR
:
14724 case EXACT_DIV_EXPR
:
14728 case TRUNC_MOD_EXPR
:
14729 case FLOOR_MOD_EXPR
:
14730 case TRUTH_ANDIF_EXPR
:
14731 case TRUTH_ORIF_EXPR
:
14732 case TRUTH_AND_EXPR
:
14733 case TRUTH_OR_EXPR
:
14749 warning_sentinel
s1(warn_type_limits
);
14750 warning_sentinel
s2(warn_div_by_zero
);
14751 tree op0
= RECUR (TREE_OPERAND (t
, 0));
14752 tree op1
= RECUR (TREE_OPERAND (t
, 1));
14753 tree r
= build_x_binary_op
14754 (input_location
, TREE_CODE (t
),
14756 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
14758 : TREE_CODE (TREE_OPERAND (t
, 0))),
14760 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
14762 : TREE_CODE (TREE_OPERAND (t
, 1))),
14764 complain
|decltype_flag
);
14765 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
14766 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14771 case POINTER_PLUS_EXPR
:
14773 tree op0
= RECUR (TREE_OPERAND (t
, 0));
14774 tree op1
= RECUR (TREE_OPERAND (t
, 1));
14775 return fold_build_pointer_plus (op0
, op1
);
14779 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
14780 /*address_p=*/false));
14782 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14783 args
, complain
, in_decl
);
14784 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
14785 RECUR (TREE_OPERAND (t
, 1)),
14786 complain
|decltype_flag
));
14788 case ARRAY_NOTATION_REF
:
14790 tree start_index
, length
, stride
;
14791 op1
= tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t
),
14792 args
, complain
, in_decl
);
14793 start_index
= RECUR (ARRAY_NOTATION_START (t
));
14794 length
= RECUR (ARRAY_NOTATION_LENGTH (t
));
14795 stride
= RECUR (ARRAY_NOTATION_STRIDE (t
));
14796 RETURN (build_array_notation_ref (EXPR_LOCATION (t
), op1
, start_index
,
14797 length
, stride
, TREE_TYPE (op1
)));
14800 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
14801 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
14808 op1
= TREE_OPERAND (t
, 0);
14809 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
14810 op1
= TREE_TYPE (op1
);
14813 /* When there are no ARGS, we are trying to evaluate a
14814 non-dependent expression from the parser. Trying to do
14815 the substitutions may not work. */
14817 op1
= TREE_TYPE (op1
);
14821 ++cp_unevaluated_operand
;
14822 ++c_inhibit_evaluation_warnings
;
14824 op1
= tsubst (op1
, args
, complain
, in_decl
);
14826 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14827 /*function_p=*/false,
14828 /*integral_constant_expression_p=*/
14830 --cp_unevaluated_operand
;
14831 --c_inhibit_evaluation_warnings
;
14834 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
14835 complain
& tf_error
);
14837 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
14838 complain
& tf_error
);
14839 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
14841 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
14843 if (!processing_template_decl
&& TYPE_P (op1
))
14845 r
= build_min (SIZEOF_EXPR
, size_type_node
,
14846 build1 (NOP_EXPR
, op1
, error_mark_node
));
14847 SIZEOF_EXPR_TYPE_P (r
) = 1;
14850 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
14851 TREE_SIDE_EFFECTS (r
) = 0;
14852 TREE_READONLY (r
) = 1;
14854 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
14859 case AT_ENCODE_EXPR
:
14861 op1
= TREE_OPERAND (t
, 0);
14862 ++cp_unevaluated_operand
;
14863 ++c_inhibit_evaluation_warnings
;
14864 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14865 /*function_p=*/false,
14866 /*integral_constant_expression_p=*/false);
14867 --cp_unevaluated_operand
;
14868 --c_inhibit_evaluation_warnings
;
14869 RETURN (objc_build_encode_expr (op1
));
14872 case NOEXCEPT_EXPR
:
14873 op1
= TREE_OPERAND (t
, 0);
14874 ++cp_unevaluated_operand
;
14875 ++c_inhibit_evaluation_warnings
;
14876 ++cp_noexcept_operand
;
14877 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14878 /*function_p=*/false,
14879 /*integral_constant_expression_p=*/false);
14880 --cp_unevaluated_operand
;
14881 --c_inhibit_evaluation_warnings
;
14882 --cp_noexcept_operand
;
14883 RETURN (finish_noexcept_expr (op1
, complain
));
14887 warning_sentinel
s(warn_div_by_zero
);
14888 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
14889 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
14890 tree r
= build_x_modify_expr
14891 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
14892 complain
|decltype_flag
);
14893 /* TREE_NO_WARNING must be set if either the expression was
14894 parenthesized or it uses an operator such as >>= rather
14895 than plain assignment. In the former case, it was already
14896 set and must be copied. In the latter case,
14897 build_x_modify_expr sets it and it must not be reset
14899 if (TREE_NO_WARNING (t
))
14900 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14906 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14907 args
, complain
, in_decl
);
14908 /* Remember that there was a reference to this entity. */
14910 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
14911 RETURN (error_mark_node
);
14912 RETURN (build_x_arrow (input_location
, op1
, complain
));
14916 tree placement
= RECUR (TREE_OPERAND (t
, 0));
14917 tree init
= RECUR (TREE_OPERAND (t
, 3));
14918 vec
<tree
, va_gc
> *placement_vec
;
14919 vec
<tree
, va_gc
> *init_vec
;
14922 if (placement
== NULL_TREE
)
14923 placement_vec
= NULL
;
14926 placement_vec
= make_tree_vector ();
14927 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
14928 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
14931 /* If there was an initializer in the original tree, but it
14932 instantiated to an empty list, then we should pass a
14933 non-NULL empty vector to tell build_new that it was an
14934 empty initializer() rather than no initializer. This can
14935 only happen when the initializer is a pack expansion whose
14936 parameter packs are of length zero. */
14937 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
14941 init_vec
= make_tree_vector ();
14942 if (init
== void_node
)
14943 gcc_assert (init_vec
!= NULL
);
14946 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
14947 vec_safe_push (init_vec
, TREE_VALUE (init
));
14951 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14952 tree op2
= RECUR (TREE_OPERAND (t
, 2));
14953 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
14954 NEW_EXPR_USE_GLOBAL (t
),
14957 if (placement_vec
!= NULL
)
14958 release_tree_vector (placement_vec
);
14959 if (init_vec
!= NULL
)
14960 release_tree_vector (init_vec
);
14967 tree op0
= RECUR (TREE_OPERAND (t
, 0));
14968 tree op1
= RECUR (TREE_OPERAND (t
, 1));
14969 RETURN (delete_sanity (op0
, op1
,
14970 DELETE_EXPR_USE_VEC (t
),
14971 DELETE_EXPR_USE_GLOBAL (t
),
14975 case COMPOUND_EXPR
:
14977 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
14978 complain
& ~tf_decltype
, in_decl
,
14979 /*function_p=*/false,
14980 integral_constant_expression_p
);
14981 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
14983 RECUR (TREE_OPERAND (t
, 1)),
14984 complain
|decltype_flag
));
14990 vec
<tree
, va_gc
> *call_args
;
14991 unsigned int nargs
, i
;
14996 function
= CALL_EXPR_FN (t
);
14997 /* When we parsed the expression, we determined whether or
14998 not Koenig lookup should be performed. */
14999 koenig_p
= KOENIG_LOOKUP_P (t
);
15000 if (TREE_CODE (function
) == SCOPE_REF
)
15002 qualified_p
= true;
15003 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
15005 /*address_p=*/false);
15007 else if (koenig_p
&& identifier_p (function
))
15009 /* Do nothing; calling tsubst_copy_and_build on an identifier
15010 would incorrectly perform unqualified lookup again.
15012 Note that we can also have an IDENTIFIER_NODE if the earlier
15013 unqualified lookup found a member function; in that case
15014 koenig_p will be false and we do want to do the lookup
15015 again to find the instantiated member function.
15017 FIXME but doing that causes c++/15272, so we need to stop
15018 using IDENTIFIER_NODE in that situation. */
15019 qualified_p
= false;
15023 if (TREE_CODE (function
) == COMPONENT_REF
)
15025 tree op
= TREE_OPERAND (function
, 1);
15027 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
15028 || (BASELINK_P (op
)
15029 && BASELINK_QUALIFIED_P (op
)));
15032 qualified_p
= false;
15034 if (TREE_CODE (function
) == ADDR_EXPR
15035 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
15036 /* Avoid error about taking the address of a constructor. */
15037 function
= TREE_OPERAND (function
, 0);
15039 function
= tsubst_copy_and_build (function
, args
, complain
,
15042 integral_constant_expression_p
);
15044 if (BASELINK_P (function
))
15045 qualified_p
= true;
15048 nargs
= call_expr_nargs (t
);
15049 call_args
= make_tree_vector ();
15050 for (i
= 0; i
< nargs
; ++i
)
15052 tree arg
= CALL_EXPR_ARG (t
, i
);
15054 if (!PACK_EXPANSION_P (arg
))
15055 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
15058 /* Expand the pack expansion and push each entry onto
15060 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
15061 if (TREE_CODE (arg
) == TREE_VEC
)
15063 unsigned int len
, j
;
15065 len
= TREE_VEC_LENGTH (arg
);
15066 for (j
= 0; j
< len
; ++j
)
15068 tree value
= TREE_VEC_ELT (arg
, j
);
15069 if (value
!= NULL_TREE
)
15070 value
= convert_from_reference (value
);
15071 vec_safe_push (call_args
, value
);
15076 /* A partial substitution. Add one entry. */
15077 vec_safe_push (call_args
, arg
);
15082 /* We do not perform argument-dependent lookup if normal
15083 lookup finds a non-function, in accordance with the
15084 expected resolution of DR 218. */
15086 && ((is_overloaded_fn (function
)
15087 /* If lookup found a member function, the Koenig lookup is
15088 not appropriate, even if an unqualified-name was used
15089 to denote the function. */
15090 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
15091 || identifier_p (function
))
15092 /* Only do this when substitution turns a dependent call
15093 into a non-dependent call. */
15094 && type_dependent_expression_p_push (t
)
15095 && !any_type_dependent_arguments_p (call_args
))
15096 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
15098 if (identifier_p (function
)
15099 && !any_type_dependent_arguments_p (call_args
))
15101 if (koenig_p
&& (complain
& tf_warning_or_error
))
15103 /* For backwards compatibility and good diagnostics, try
15104 the unqualified lookup again if we aren't in SFINAE
15106 tree unq
= (tsubst_copy_and_build
15107 (function
, args
, complain
, in_decl
, true,
15108 integral_constant_expression_p
));
15109 if (unq
== error_mark_node
)
15110 RETURN (error_mark_node
);
15112 if (unq
!= function
)
15115 if (INDIRECT_REF_P (fn
))
15116 fn
= TREE_OPERAND (fn
, 0);
15117 if (TREE_CODE (fn
) == COMPONENT_REF
)
15118 fn
= TREE_OPERAND (fn
, 1);
15119 if (is_overloaded_fn (fn
))
15120 fn
= get_first_fn (fn
);
15121 if (permerror (EXPR_LOC_OR_LOC (t
, input_location
),
15122 "%qD was not declared in this scope, "
15123 "and no declarations were found by "
15124 "argument-dependent lookup at the point "
15125 "of instantiation", function
))
15128 /* Can't say anything more. */;
15129 else if (DECL_CLASS_SCOPE_P (fn
))
15131 location_t loc
= EXPR_LOC_OR_LOC (t
,
15134 "declarations in dependent base %qT are "
15135 "not found by unqualified lookup",
15136 DECL_CLASS_CONTEXT (fn
));
15137 if (current_class_ptr
)
15139 "use %<this->%D%> instead", function
);
15142 "use %<%T::%D%> instead",
15143 current_class_name
, function
);
15146 inform (0, "%q+D declared here, later in the "
15147 "translation unit", fn
);
15152 if (identifier_p (function
))
15154 if (complain
& tf_error
)
15155 unqualified_name_lookup_error (function
);
15156 release_tree_vector (call_args
);
15157 RETURN (error_mark_node
);
15161 /* Remember that there was a reference to this entity. */
15162 if (DECL_P (function
)
15163 && !mark_used (function
, complain
) && !(complain
& tf_error
))
15164 RETURN (error_mark_node
);
15166 /* Put back tf_decltype for the actual call. */
15167 complain
|= decltype_flag
;
15169 if (TREE_CODE (function
) == OFFSET_REF
)
15170 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
15172 else if (TREE_CODE (function
) == COMPONENT_REF
)
15174 tree instance
= TREE_OPERAND (function
, 0);
15175 tree fn
= TREE_OPERAND (function
, 1);
15177 if (processing_template_decl
15178 && (type_dependent_expression_p (instance
)
15179 || (!BASELINK_P (fn
)
15180 && TREE_CODE (fn
) != FIELD_DECL
)
15181 || type_dependent_expression_p (fn
)
15182 || any_type_dependent_arguments_p (call_args
)))
15183 ret
= build_nt_call_vec (function
, call_args
);
15184 else if (!BASELINK_P (fn
))
15185 ret
= finish_call_expr (function
, &call_args
,
15186 /*disallow_virtual=*/false,
15187 /*koenig_p=*/false,
15190 ret
= (build_new_method_call
15192 &call_args
, NULL_TREE
,
15193 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
15198 ret
= finish_call_expr (function
, &call_args
,
15199 /*disallow_virtual=*/qualified_p
,
15203 release_tree_vector (call_args
);
15210 tree cond
= RECUR (TREE_OPERAND (t
, 0));
15211 tree folded_cond
= fold_non_dependent_expr (cond
);
15214 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
15216 if (integer_zerop (folded_cond
))
15218 ++c_inhibit_evaluation_warnings
;
15219 exp1
= RECUR (TREE_OPERAND (t
, 1));
15220 --c_inhibit_evaluation_warnings
;
15221 exp2
= RECUR (TREE_OPERAND (t
, 2));
15225 exp1
= RECUR (TREE_OPERAND (t
, 1));
15226 ++c_inhibit_evaluation_warnings
;
15227 exp2
= RECUR (TREE_OPERAND (t
, 2));
15228 --c_inhibit_evaluation_warnings
;
15230 cond
= folded_cond
;
15234 exp1
= RECUR (TREE_OPERAND (t
, 1));
15235 exp2
= RECUR (TREE_OPERAND (t
, 2));
15238 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
15239 cond
, exp1
, exp2
, complain
));
15242 case PSEUDO_DTOR_EXPR
:
15244 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15245 tree op1
= RECUR (TREE_OPERAND (t
, 1));
15246 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15247 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
15253 tree purpose
, value
, chain
;
15255 if (t
== void_list_node
)
15258 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
15259 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
15261 /* We have pack expansions, so expand those and
15262 create a new list out of it. */
15263 tree purposevec
= NULL_TREE
;
15264 tree valuevec
= NULL_TREE
;
15268 /* Expand the argument expressions. */
15269 if (TREE_PURPOSE (t
))
15270 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
15271 complain
, in_decl
);
15272 if (TREE_VALUE (t
))
15273 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
15274 complain
, in_decl
);
15276 /* Build the rest of the list. */
15277 chain
= TREE_CHAIN (t
);
15278 if (chain
&& chain
!= void_type_node
)
15279 chain
= RECUR (chain
);
15281 /* Determine the number of arguments. */
15282 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
15284 len
= TREE_VEC_LENGTH (purposevec
);
15285 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
15287 else if (TREE_CODE (valuevec
) == TREE_VEC
)
15288 len
= TREE_VEC_LENGTH (valuevec
);
15291 /* Since we only performed a partial substitution into
15292 the argument pack, we only RETURN (a single list
15294 if (purposevec
== TREE_PURPOSE (t
)
15295 && valuevec
== TREE_VALUE (t
)
15296 && chain
== TREE_CHAIN (t
))
15299 RETURN (tree_cons (purposevec
, valuevec
, chain
));
15302 /* Convert the argument vectors into a TREE_LIST */
15306 /* Grab the Ith values. */
15308 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
15311 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
15314 /* Build the list (backwards). */
15315 chain
= tree_cons (purpose
, value
, chain
);
15321 purpose
= TREE_PURPOSE (t
);
15323 purpose
= RECUR (purpose
);
15324 value
= TREE_VALUE (t
);
15326 value
= RECUR (value
);
15327 chain
= TREE_CHAIN (t
);
15328 if (chain
&& chain
!= void_type_node
)
15329 chain
= RECUR (chain
);
15330 if (purpose
== TREE_PURPOSE (t
)
15331 && value
== TREE_VALUE (t
)
15332 && chain
== TREE_CHAIN (t
))
15334 RETURN (tree_cons (purpose
, value
, chain
));
15337 case COMPONENT_REF
:
15344 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
15345 args
, complain
, in_decl
);
15346 /* Remember that there was a reference to this entity. */
15347 if (DECL_P (object
)
15348 && !mark_used (object
, complain
) && !(complain
& tf_error
))
15349 RETURN (error_mark_node
);
15350 object_type
= TREE_TYPE (object
);
15352 member
= TREE_OPERAND (t
, 1);
15353 if (BASELINK_P (member
))
15354 member
= tsubst_baselink (member
,
15355 non_reference (TREE_TYPE (object
)),
15356 args
, complain
, in_decl
);
15358 member
= tsubst_copy (member
, args
, complain
, in_decl
);
15359 if (member
== error_mark_node
)
15360 RETURN (error_mark_node
);
15362 if (type_dependent_expression_p (object
))
15363 /* We can't do much here. */;
15364 else if (!CLASS_TYPE_P (object_type
))
15366 if (scalarish_type_p (object_type
))
15368 tree s
= NULL_TREE
;
15369 tree dtor
= member
;
15371 if (TREE_CODE (dtor
) == SCOPE_REF
)
15373 s
= TREE_OPERAND (dtor
, 0);
15374 dtor
= TREE_OPERAND (dtor
, 1);
15376 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
15378 dtor
= TREE_OPERAND (dtor
, 0);
15380 RETURN (finish_pseudo_destructor_expr
15381 (object
, s
, dtor
, input_location
));
15385 else if (TREE_CODE (member
) == SCOPE_REF
15386 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
15388 /* Lookup the template functions now that we know what the
15390 tree scope
= TREE_OPERAND (member
, 0);
15391 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
15392 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
15393 member
= lookup_qualified_name (scope
, tmpl
,
15394 /*is_type_p=*/false,
15395 /*complain=*/false);
15396 if (BASELINK_P (member
))
15398 BASELINK_FUNCTIONS (member
)
15399 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
15401 member
= (adjust_result_of_qualified_name_lookup
15402 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
15407 qualified_name_lookup_error (scope
, tmpl
, member
,
15409 RETURN (error_mark_node
);
15412 else if (TREE_CODE (member
) == SCOPE_REF
15413 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
15414 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
15416 if (complain
& tf_error
)
15418 if (TYPE_P (TREE_OPERAND (member
, 0)))
15419 error ("%qT is not a class or namespace",
15420 TREE_OPERAND (member
, 0));
15422 error ("%qD is not a class or namespace",
15423 TREE_OPERAND (member
, 0));
15425 RETURN (error_mark_node
);
15427 else if (TREE_CODE (member
) == FIELD_DECL
)
15429 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
15430 if (TREE_CODE (r
) == COMPONENT_REF
)
15431 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
15435 r
= finish_class_member_access_expr (object
, member
,
15436 /*template_p=*/false,
15438 if (TREE_CODE (r
) == COMPONENT_REF
)
15439 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
15444 RETURN (build_throw
15445 (RECUR (TREE_OPERAND (t
, 0))));
15449 vec
<constructor_elt
, va_gc
> *n
;
15450 constructor_elt
*ce
;
15451 unsigned HOST_WIDE_INT idx
;
15452 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15453 bool process_index_p
;
15455 bool need_copy_p
= false;
15458 if (type
== error_mark_node
)
15459 RETURN (error_mark_node
);
15461 /* digest_init will do the wrong thing if we let it. */
15462 if (type
&& TYPE_PTRMEMFUNC_P (type
))
15465 /* We do not want to process the index of aggregate
15466 initializers as they are identifier nodes which will be
15467 looked up by digest_init. */
15468 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
15470 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
15471 newlen
= vec_safe_length (n
);
15472 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
15474 if (ce
->index
&& process_index_p
15475 /* An identifier index is looked up in the type
15476 being initialized, not the current scope. */
15477 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
15478 ce
->index
= RECUR (ce
->index
);
15480 if (PACK_EXPANSION_P (ce
->value
))
15482 /* Substitute into the pack expansion. */
15483 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
15486 if (ce
->value
== error_mark_node
15487 || PACK_EXPANSION_P (ce
->value
))
15489 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
15490 /* Just move the argument into place. */
15491 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
15494 /* Update the length of the final CONSTRUCTOR
15495 arguments vector, and note that we will need to
15497 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
15498 need_copy_p
= true;
15502 ce
->value
= RECUR (ce
->value
);
15507 vec
<constructor_elt
, va_gc
> *old_n
= n
;
15509 vec_alloc (n
, newlen
);
15510 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
15512 if (TREE_CODE (ce
->value
) == TREE_VEC
)
15514 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
15515 for (i
= 0; i
< len
; ++i
)
15516 CONSTRUCTOR_APPEND_ELT (n
, 0,
15517 TREE_VEC_ELT (ce
->value
, i
));
15520 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
15524 r
= build_constructor (init_list_type_node
, n
);
15525 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
15527 if (TREE_HAS_CONSTRUCTOR (t
))
15528 RETURN (finish_compound_literal (type
, r
, complain
));
15530 TREE_TYPE (r
) = type
;
15536 tree operand_0
= TREE_OPERAND (t
, 0);
15537 if (TYPE_P (operand_0
))
15539 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
15540 RETURN (get_typeid (operand_0
, complain
));
15544 operand_0
= RECUR (operand_0
);
15545 RETURN (build_typeid (operand_0
, complain
));
15552 else if (DECL_PACK_P (t
))
15554 /* We don't build decls for an instantiation of a
15555 variadic capture proxy, we instantiate the elements
15557 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
15558 return RECUR (DECL_VALUE_EXPR (t
));
15564 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
15565 /* ??? We're doing a subset of finish_id_expression here. */
15567 && !processing_template_decl
15568 && !cp_unevaluated_operand
15569 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
15570 && DECL_THREAD_LOCAL_P (r
))
15572 if (tree wrap
= get_tls_wrapper_fn (r
))
15573 /* Replace an evaluated use of the thread_local variable with
15574 a call to its wrapper. */
15575 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
15577 else if (outer_automatic_var_p (r
))
15578 r
= process_outer_var_ref (r
, complain
);
15580 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
15581 /* If the original type was a reference, we'll be wrapped in
15582 the appropriate INDIRECT_REF. */
15583 r
= convert_from_reference (r
);
15589 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15590 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15591 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
15594 case OFFSETOF_EXPR
:
15595 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t
, 0)),
15596 EXPR_LOCATION (t
)));
15600 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
15601 complain
, in_decl
);
15603 tree type2
= TRAIT_EXPR_TYPE2 (t
);
15604 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
15605 type2
= RECUR (type2
);
15607 type2
= tsubst (type2
, args
, complain
, in_decl
);
15609 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
15614 tree old_stmt_expr
= cur_stmt_expr
;
15615 tree stmt_expr
= begin_stmt_expr ();
15617 cur_stmt_expr
= stmt_expr
;
15618 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
15619 integral_constant_expression_p
);
15620 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
15621 cur_stmt_expr
= old_stmt_expr
;
15623 /* If the resulting list of expression statement is empty,
15624 fold it further into void_node. */
15625 if (empty_expr_stmt_p (stmt_expr
))
15626 stmt_expr
= void_node
;
15628 RETURN (stmt_expr
);
15633 tree r
= build_lambda_expr ();
15635 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
15636 LAMBDA_EXPR_CLOSURE (r
) = type
;
15637 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
15639 LAMBDA_EXPR_LOCATION (r
)
15640 = LAMBDA_EXPR_LOCATION (t
);
15641 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
15642 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
15643 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
15644 LAMBDA_EXPR_DISCRIMINATOR (r
)
15645 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
15646 /* For a function scope, we want to use tsubst so that we don't
15647 complain about referring to an auto function before its return
15648 type has been deduced. Otherwise, we want to use tsubst_copy so
15649 that we look up the existing field/parameter/variable rather
15650 than build a new one. */
15651 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
15652 if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
15653 scope
= tsubst (scope
, args
, complain
, in_decl
);
15654 else if (scope
&& TREE_CODE (scope
) == PARM_DECL
)
15656 /* Look up the parameter we want directly, as tsubst_copy
15657 doesn't do what we need. */
15658 tree fn
= tsubst (DECL_CONTEXT (scope
), args
, complain
, in_decl
);
15659 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
15660 while (DECL_PARM_INDEX (parm
) != DECL_PARM_INDEX (scope
))
15661 parm
= DECL_CHAIN (parm
);
15663 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15664 if (DECL_CONTEXT (scope
) == NULL_TREE
)
15665 DECL_CONTEXT (scope
) = fn
;
15668 scope
= RECUR (scope
);
15669 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
15670 LAMBDA_EXPR_RETURN_TYPE (r
)
15671 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
15673 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
15674 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
15676 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15677 determine_visibility (TYPE_NAME (type
));
15678 /* Now that we know visibility, instantiate the type so we have a
15679 declaration of the op() for later calls to lambda_function. */
15680 complete_type (type
);
15682 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
15684 RETURN (build_lambda_object (r
));
15688 /* We can get here for a constant initializer of non-dependent type.
15689 FIXME stop folding in cp_parser_initializer_clause. */
15691 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
15696 case TRANSACTION_EXPR
:
15697 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
15698 integral_constant_expression_p
));
15701 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
15703 case VEC_PERM_EXPR
:
15705 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15706 tree op1
= RECUR (TREE_OPERAND (t
, 1));
15707 tree op2
= RECUR (TREE_OPERAND (t
, 2));
15708 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
15713 /* Handle Objective-C++ constructs, if appropriate. */
15716 = objcp_tsubst_copy_and_build (t
, args
, complain
,
15717 in_decl
, /*function_p=*/false);
15721 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
15727 input_location
= loc
;
15731 /* Verify that the instantiated ARGS are valid. For type arguments,
15732 make sure that the type's linkage is ok. For non-type arguments,
15733 make sure they are constants if they are integral or enumerations.
15734 Emit an error under control of COMPLAIN, and return TRUE on error. */
15737 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
15739 if (dependent_template_arg_p (t
))
15741 if (ARGUMENT_PACK_P (t
))
15743 tree vec
= ARGUMENT_PACK_ARGS (t
);
15744 int len
= TREE_VEC_LENGTH (vec
);
15745 bool result
= false;
15748 for (i
= 0; i
< len
; ++i
)
15749 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
15753 else if (TYPE_P (t
))
15755 /* [basic.link]: A name with no linkage (notably, the name
15756 of a class or enumeration declared in a local scope)
15757 shall not be used to declare an entity with linkage.
15758 This implies that names with no linkage cannot be used as
15761 DR 757 relaxes this restriction for C++0x. */
15762 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
15763 : no_linkage_check (t
, /*relaxed_p=*/false));
15767 /* DR 488 makes use of a type with no linkage cause
15768 type deduction to fail. */
15769 if (complain
& tf_error
)
15771 if (TYPE_ANONYMOUS_P (nt
))
15772 error ("%qT is/uses anonymous type", t
);
15774 error ("template argument for %qD uses local type %qT",
15779 /* In order to avoid all sorts of complications, we do not
15780 allow variably-modified types as template arguments. */
15781 else if (variably_modified_type_p (t
, NULL_TREE
))
15783 if (complain
& tf_error
)
15784 error ("%qT is a variably modified type", t
);
15788 /* Class template and alias template arguments should be OK. */
15789 else if (DECL_TYPE_TEMPLATE_P (t
))
15791 /* A non-type argument of integral or enumerated type must be a
15793 else if (TREE_TYPE (t
)
15794 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
15795 && !REFERENCE_REF_P (t
)
15796 && !TREE_CONSTANT (t
))
15798 if (complain
& tf_error
)
15799 error ("integral expression %qE is not constant", t
);
15806 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
15808 int ix
, len
= DECL_NTPARMS (tmpl
);
15809 bool result
= false;
15811 for (ix
= 0; ix
!= len
; ix
++)
15813 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
15816 if (result
&& (complain
& tf_error
))
15817 error (" trying to instantiate %qD", tmpl
);
15821 /* We're out of SFINAE context now, so generate diagnostics for the access
15822 errors we saw earlier when instantiating D from TMPL and ARGS. */
15825 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
15827 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
15828 tree type
= TREE_TYPE (pattern
);
15829 location_t loc
= input_location
;
15831 push_access_scope (d
);
15832 push_deferring_access_checks (dk_no_deferred
);
15833 input_location
= DECL_SOURCE_LOCATION (pattern
);
15834 tsubst (type
, args
, tf_warning_or_error
, d
);
15835 input_location
= loc
;
15836 pop_deferring_access_checks ();
15837 pop_access_scope (d
);
15840 /* Instantiate the indicated variable, function, or alias template TMPL with
15841 the template arguments in TARG_PTR. */
15844 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
15846 tree targ_ptr
= orig_args
;
15850 bool access_ok
= true;
15852 if (tmpl
== error_mark_node
)
15853 return error_mark_node
;
15855 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
15857 /* If this function is a clone, handle it specially. */
15858 if (DECL_CLONED_FUNCTION_P (tmpl
))
15863 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15864 DECL_CLONED_FUNCTION. */
15865 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
15866 targ_ptr
, complain
);
15867 if (spec
== error_mark_node
)
15868 return error_mark_node
;
15870 /* Look for the clone. */
15871 FOR_EACH_CLONE (clone
, spec
)
15872 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
15874 /* We should always have found the clone by now. */
15875 gcc_unreachable ();
15879 if (targ_ptr
== error_mark_node
)
15880 return error_mark_node
;
15882 /* Check to see if we already have this specialization. */
15883 gen_tmpl
= most_general_template (tmpl
);
15884 if (tmpl
!= gen_tmpl
)
15885 /* The TMPL is a partial instantiation. To get a full set of
15886 arguments we must add the arguments used to perform the
15887 partial instantiation. */
15888 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
15891 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15892 but it doesn't seem to be on the hot path. */
15893 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
15895 gcc_assert (tmpl
== gen_tmpl
15896 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
15898 || fndecl
== NULL_TREE
);
15900 if (spec
!= NULL_TREE
)
15902 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
15904 if (complain
& tf_error
)
15905 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
15906 return error_mark_node
;
15911 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
15913 return error_mark_node
;
15915 /* We are building a FUNCTION_DECL, during which the access of its
15916 parameters and return types have to be checked. However this
15917 FUNCTION_DECL which is the desired context for access checking
15918 is not built yet. We solve this chicken-and-egg problem by
15919 deferring all checks until we have the FUNCTION_DECL. */
15920 push_deferring_access_checks (dk_deferred
);
15922 /* Instantiation of the function happens in the context of the function
15923 template, not the context of the overload resolution we're doing. */
15924 push_to_top_level ();
15925 /* If there are dependent arguments, e.g. because we're doing partial
15926 ordering, make sure processing_template_decl stays set. */
15927 if (uses_template_parms (targ_ptr
))
15928 ++processing_template_decl
;
15929 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
15931 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
15932 complain
, gen_tmpl
, true);
15933 push_nested_class (ctx
);
15936 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
15938 if (VAR_P (pattern
))
15940 /* We need to determine if we're using a partial or explicit
15941 specialization now, because the type of the variable could be
15943 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
15944 tree elt
= most_specialized_partial_spec (tid
, complain
);
15945 if (elt
== error_mark_node
)
15946 pattern
= error_mark_node
;
15949 tmpl
= TREE_VALUE (elt
);
15950 pattern
= DECL_TEMPLATE_RESULT (tmpl
);
15951 targ_ptr
= TREE_PURPOSE (elt
);
15955 /* Substitute template parameters to obtain the specialization. */
15956 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
15957 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
15958 pop_nested_class ();
15959 pop_from_top_level ();
15961 if (fndecl
== error_mark_node
)
15963 pop_deferring_access_checks ();
15964 return error_mark_node
;
15967 /* The DECL_TI_TEMPLATE should always be the immediate parent
15968 template, not the most general template. */
15969 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
15971 /* Now we know the specialization, compute access previously
15973 push_access_scope (fndecl
);
15974 if (!perform_deferred_access_checks (complain
))
15976 pop_access_scope (fndecl
);
15977 pop_deferring_access_checks ();
15979 /* If we've just instantiated the main entry point for a function,
15980 instantiate all the alternate entry points as well. We do this
15981 by cloning the instantiation of the main entry point, not by
15982 instantiating the template clones. */
15983 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
15984 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
15988 if (!(complain
& tf_error
))
15990 /* Remember to reinstantiate when we're out of SFINAE so the user
15991 can see the errors. */
15992 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
15994 return error_mark_node
;
15999 /* Wrapper for instantiate_template_1. */
16002 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
16005 timevar_push (TV_TEMPLATE_INST
);
16006 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
16007 timevar_pop (TV_TEMPLATE_INST
);
16011 /* Instantiate the alias template TMPL with ARGS. Also push a template
16012 instantiation level, which instantiate_template doesn't do because
16013 functions and variables have sufficient context established by the
16017 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
16019 struct pending_template
*old_last_pend
= last_pending_template
;
16020 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
16021 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
16022 return error_mark_node
;
16023 tree tinst
= build_tree_list (tmpl
, args
);
16024 if (!push_tinst_level (tinst
))
16027 return error_mark_node
;
16031 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
16032 args
, tmpl
, complain
,
16033 /*require_all_args=*/true,
16034 /*use_default_args=*/true);
16036 tree r
= instantiate_template (tmpl
, args
, complain
);
16037 pop_tinst_level ();
16038 /* We can't free this if a pending_template entry or last_error_tinst_level
16039 is pointing at it. */
16040 if (last_pending_template
== old_last_pend
16041 && last_error_tinst_level
== old_error_tinst
)
16047 /* PARM is a template parameter pack for FN. Returns true iff
16048 PARM is used in a deducible way in the argument list of FN. */
16051 pack_deducible_p (tree parm
, tree fn
)
16053 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
16054 for (; t
; t
= TREE_CHAIN (t
))
16056 tree type
= TREE_VALUE (t
);
16058 if (!PACK_EXPANSION_P (type
))
16060 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
16061 packs
; packs
= TREE_CHAIN (packs
))
16062 if (template_args_equal (TREE_VALUE (packs
), parm
))
16064 /* The template parameter pack is used in a function parameter
16065 pack. If this is the end of the parameter list, the
16066 template parameter pack is deducible. */
16067 if (TREE_CHAIN (t
) == void_list_node
)
16070 /* Otherwise, not. Well, it could be deduced from
16071 a non-pack parameter, but doing so would end up with
16072 a deduction mismatch, so don't bother. */
16076 /* The template parameter pack isn't used in any function parameter
16077 packs, but it might be used deeper, e.g. tuple<Args...>. */
16081 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
16082 NARGS elements of the arguments that are being used when calling
16083 it. TARGS is a vector into which the deduced template arguments
16086 Returns either a FUNCTION_DECL for the matching specialization of FN or
16087 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
16088 true, diagnostics will be printed to explain why it failed.
16090 If FN is a conversion operator, or we are trying to produce a specific
16091 specialization, RETURN_TYPE is the return type desired.
16093 The EXPLICIT_TARGS are explicit template arguments provided via a
16096 The parameter STRICT is one of:
16099 We are deducing arguments for a function call, as in
16100 [temp.deduct.call].
16103 We are deducing arguments for a conversion function, as in
16104 [temp.deduct.conv].
16107 We are deducing arguments when doing an explicit instantiation
16108 as in [temp.explicit], when determining an explicit specialization
16109 as in [temp.expl.spec], or when taking the address of a function
16110 template, as in [temp.deduct.funcaddr]. */
16113 fn_type_unification (tree fn
,
16114 tree explicit_targs
,
16117 unsigned int nargs
,
16119 unification_kind_t strict
,
16126 tree decl
= NULL_TREE
;
16127 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
16129 static int deduction_depth
;
16130 struct pending_template
*old_last_pend
= last_pending_template
;
16131 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
16132 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
16134 tree r
= error_mark_node
;
16137 complain
|= tf_decltype
;
16139 /* In C++0x, it's possible to have a function template whose type depends
16140 on itself recursively. This is most obvious with decltype, but can also
16141 occur with enumeration scope (c++/48969). So we need to catch infinite
16142 recursion and reject the substitution at deduction time; this function
16143 will return error_mark_node for any repeated substitution.
16145 This also catches excessive recursion such as when f<N> depends on
16146 f<N-1> across all integers, and returns error_mark_node for all the
16147 substitutions back up to the initial one.
16149 This is, of course, not reentrant. */
16150 if (excessive_deduction_depth
)
16151 return error_mark_node
;
16152 tinst
= build_tree_list (fn
, NULL_TREE
);
16155 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
16157 fntype
= TREE_TYPE (fn
);
16158 if (explicit_targs
)
16162 The specified template arguments must match the template
16163 parameters in kind (i.e., type, nontype, template), and there
16164 must not be more arguments than there are parameters;
16165 otherwise type deduction fails.
16167 Nontype arguments must match the types of the corresponding
16168 nontype template parameters, or must be convertible to the
16169 types of the corresponding nontype parameters as specified in
16170 _temp.arg.nontype_, otherwise type deduction fails.
16172 All references in the function type of the function template
16173 to the corresponding template parameters are replaced by the
16174 specified template argument values. If a substitution in a
16175 template parameter or in the function type of the function
16176 template results in an invalid type, type deduction fails. */
16177 int i
, len
= TREE_VEC_LENGTH (tparms
);
16178 location_t loc
= input_location
;
16179 bool incomplete
= false;
16181 /* Adjust any explicit template arguments before entering the
16182 substitution context. */
16184 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
16186 /*require_all_args=*/false,
16187 /*use_default_args=*/false));
16188 if (explicit_targs
== error_mark_node
)
16191 /* Substitute the explicit args into the function type. This is
16192 necessary so that, for instance, explicitly declared function
16193 arguments can match null pointed constants. If we were given
16194 an incomplete set of explicit args, we must not do semantic
16195 processing during substitution as we could create partial
16197 for (i
= 0; i
< len
; i
++)
16199 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
16200 bool parameter_pack
= false;
16201 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
16203 /* Dig out the actual parm. */
16204 if (TREE_CODE (parm
) == TYPE_DECL
16205 || TREE_CODE (parm
) == TEMPLATE_DECL
)
16207 parm
= TREE_TYPE (parm
);
16208 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
16210 else if (TREE_CODE (parm
) == PARM_DECL
)
16212 parm
= DECL_INITIAL (parm
);
16213 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
16216 if (!parameter_pack
&& targ
== NULL_TREE
)
16217 /* No explicit argument for this template parameter. */
16220 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
16222 /* Mark the argument pack as "incomplete". We could
16223 still deduce more arguments during unification.
16224 We remove this mark in type_unification_real. */
16227 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
16228 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
16229 = ARGUMENT_PACK_ARGS (targ
);
16232 /* We have some incomplete argument packs. */
16237 TREE_VALUE (tinst
) = explicit_targs
;
16238 if (!push_tinst_level (tinst
))
16240 excessive_deduction_depth
= true;
16243 processing_template_decl
+= incomplete
;
16244 input_location
= DECL_SOURCE_LOCATION (fn
);
16245 /* Ignore any access checks; we'll see them again in
16246 instantiate_template and they might have the wrong
16247 access path at this point. */
16248 push_deferring_access_checks (dk_deferred
);
16249 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
16250 complain
| tf_partial
, NULL_TREE
);
16251 pop_deferring_access_checks ();
16252 input_location
= loc
;
16253 processing_template_decl
-= incomplete
;
16254 pop_tinst_level ();
16256 if (fntype
== error_mark_node
)
16259 /* Place the explicitly specified arguments in TARGS. */
16260 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
16261 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
16264 /* Never do unification on the 'this' parameter. */
16265 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
16271 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
16272 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
16273 new_args
[0] = return_type
;
16274 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
16279 /* We allow incomplete unification without an error message here
16280 because the standard doesn't seem to explicitly prohibit it. Our
16281 callers must be ready to deal with unification failures in any
16284 TREE_VALUE (tinst
) = targs
;
16285 /* If we aren't explaining yet, push tinst context so we can see where
16286 any errors (e.g. from class instantiations triggered by instantiation
16287 of default template arguments) come from. If we are explaining, this
16288 context is redundant. */
16289 if (!explain_p
&& !push_tinst_level (tinst
))
16291 excessive_deduction_depth
= true;
16295 /* type_unification_real will pass back any access checks from default
16296 template argument substitution. */
16297 vec
<deferred_access_check
, va_gc
> *checks
;
16300 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
16301 targs
, parms
, args
, nargs
, /*subr=*/0,
16302 strict
, flags
, &checks
, explain_p
);
16304 pop_tinst_level ();
16308 /* Now that we have bindings for all of the template arguments,
16309 ensure that the arguments deduced for the template template
16310 parameters have compatible template parameter lists. We cannot
16311 check this property before we have deduced all template
16312 arguments, because the template parameter types of a template
16313 template parameter might depend on prior template parameters
16314 deduced after the template template parameter. The following
16315 ill-formed example illustrates this issue:
16317 template<typename T, template<T> class C> void f(C<5>, T);
16319 template<int N> struct X {};
16322 f(X<5>(), 5l); // error: template argument deduction fails
16325 The template parameter list of 'C' depends on the template type
16326 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
16327 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
16328 time that we deduce 'C'. */
16329 if (!template_template_parm_bindings_ok_p
16330 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
16332 unify_inconsistent_template_template_parameters (explain_p
);
16336 /* All is well so far. Now, check:
16340 When all template arguments have been deduced, all uses of
16341 template parameters in nondeduced contexts are replaced with
16342 the corresponding deduced argument values. If the
16343 substitution results in an invalid type, as described above,
16344 type deduction fails. */
16345 TREE_VALUE (tinst
) = targs
;
16346 if (!push_tinst_level (tinst
))
16348 excessive_deduction_depth
= true;
16352 /* Also collect access checks from the instantiation. */
16353 reopen_deferring_access_checks (checks
);
16355 decl
= instantiate_template (fn
, targs
, complain
);
16357 checks
= get_deferred_access_checks ();
16358 pop_deferring_access_checks ();
16360 pop_tinst_level ();
16362 if (decl
== error_mark_node
)
16365 /* Now perform any access checks encountered during substitution. */
16366 push_access_scope (decl
);
16367 ok
= perform_access_checks (checks
, complain
);
16368 pop_access_scope (decl
);
16372 /* If we're looking for an exact match, check that what we got
16373 is indeed an exact match. It might not be if some template
16374 parameters are used in non-deduced contexts. But don't check
16375 for an exact match if we have dependent template arguments;
16376 in that case we're doing partial ordering, and we already know
16377 that we have two candidates that will provide the actual type. */
16378 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
16380 tree substed
= TREE_TYPE (decl
);
16384 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
16386 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
16387 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
16388 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
16390 unify_type_mismatch (explain_p
, args
[i
],
16391 TREE_VALUE (sarg
));
16400 if (excessive_deduction_depth
)
16402 if (deduction_depth
== 0)
16403 /* Reset once we're all the way out. */
16404 excessive_deduction_depth
= false;
16407 /* We can't free this if a pending_template entry or last_error_tinst_level
16408 is pointing at it. */
16409 if (last_pending_template
== old_last_pend
16410 && last_error_tinst_level
== old_error_tinst
)
16416 /* Adjust types before performing type deduction, as described in
16417 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
16418 sections are symmetric. PARM is the type of a function parameter
16419 or the return type of the conversion function. ARG is the type of
16420 the argument passed to the call, or the type of the value
16421 initialized with the result of the conversion function.
16422 ARG_EXPR is the original argument expression, which may be null. */
16425 maybe_adjust_types_for_deduction (unification_kind_t strict
,
16439 /* Swap PARM and ARG throughout the remainder of this
16440 function; the handling is precisely symmetric since PARM
16441 will initialize ARG rather than vice versa. */
16449 /* Core issue #873: Do the DR606 thing (see below) for these cases,
16450 too, but here handle it by stripping the reference from PARM
16451 rather than by adding it to ARG. */
16452 if (TREE_CODE (*parm
) == REFERENCE_TYPE
16453 && TYPE_REF_IS_RVALUE (*parm
)
16454 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
16455 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
16456 && TREE_CODE (*arg
) == REFERENCE_TYPE
16457 && !TYPE_REF_IS_RVALUE (*arg
))
16458 *parm
= TREE_TYPE (*parm
);
16459 /* Nothing else to do in this case. */
16463 gcc_unreachable ();
16466 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
16468 /* [temp.deduct.call]
16470 If P is not a reference type:
16472 --If A is an array type, the pointer type produced by the
16473 array-to-pointer standard conversion (_conv.array_) is
16474 used in place of A for type deduction; otherwise,
16476 --If A is a function type, the pointer type produced by
16477 the function-to-pointer standard conversion
16478 (_conv.func_) is used in place of A for type deduction;
16481 --If A is a cv-qualified type, the top level
16482 cv-qualifiers of A's type are ignored for type
16484 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
16485 *arg
= build_pointer_type (TREE_TYPE (*arg
));
16486 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
16487 *arg
= build_pointer_type (*arg
);
16489 *arg
= TYPE_MAIN_VARIANT (*arg
);
16492 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
16493 of the form T&&, where T is a template parameter, and the argument
16494 is an lvalue, T is deduced as A& */
16495 if (TREE_CODE (*parm
) == REFERENCE_TYPE
16496 && TYPE_REF_IS_RVALUE (*parm
)
16497 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
16498 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
16499 && (arg_expr
? real_lvalue_p (arg_expr
)
16500 /* try_one_overload doesn't provide an arg_expr, but
16501 functions are always lvalues. */
16502 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
16503 *arg
= build_reference_type (*arg
);
16505 /* [temp.deduct.call]
16507 If P is a cv-qualified type, the top level cv-qualifiers
16508 of P's type are ignored for type deduction. If P is a
16509 reference type, the type referred to by P is used for
16511 *parm
= TYPE_MAIN_VARIANT (*parm
);
16512 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
16514 *parm
= TREE_TYPE (*parm
);
16515 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
16518 /* DR 322. For conversion deduction, remove a reference type on parm
16519 too (which has been swapped into ARG). */
16520 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
16521 *arg
= TREE_TYPE (*arg
);
16526 /* Subroutine of unify_one_argument. PARM is a function parameter of a
16527 template which does contain any deducible template parameters; check if
16528 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
16529 unify_one_argument. */
16532 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
16533 int flags
, bool explain_p
)
16538 type
= TREE_TYPE (arg
);
16542 if (same_type_p (parm
, type
))
16543 return unify_success (explain_p
);
16545 if (strict
== DEDUCE_CONV
)
16547 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
16548 explain_p
? tf_warning_or_error
: tf_none
))
16549 return unify_success (explain_p
);
16551 else if (strict
!= DEDUCE_EXACT
)
16553 if (can_convert_arg (parm
, type
,
16554 TYPE_P (arg
) ? NULL_TREE
: arg
,
16555 flags
, explain_p
? tf_warning_or_error
: tf_none
))
16556 return unify_success (explain_p
);
16559 if (strict
== DEDUCE_EXACT
)
16560 return unify_type_mismatch (explain_p
, parm
, arg
);
16562 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
16565 static bool uses_deducible_template_parms (tree type
);
16567 /* Returns true iff the expression EXPR is one from which a template
16568 argument can be deduced. In other words, if it's an undecorated
16569 use of a template non-type parameter. */
16572 deducible_expression (tree expr
)
16574 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
16577 /* Returns true iff the array domain DOMAIN uses a template parameter in a
16578 deducible way; that is, if it has a max value of <PARM> - 1. */
16581 deducible_array_bound (tree domain
)
16583 if (domain
== NULL_TREE
)
16586 tree max
= TYPE_MAX_VALUE (domain
);
16587 if (TREE_CODE (max
) != MINUS_EXPR
)
16590 return deducible_expression (TREE_OPERAND (max
, 0));
16593 /* Returns true iff the template arguments ARGS use a template parameter
16594 in a deducible way. */
16597 deducible_template_args (tree args
)
16599 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
16602 tree elt
= TREE_VEC_ELT (args
, i
);
16603 if (ARGUMENT_PACK_P (elt
))
16604 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
16607 if (PACK_EXPANSION_P (elt
))
16608 elt
= PACK_EXPANSION_PATTERN (elt
);
16609 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
16611 else if (TYPE_P (elt
))
16612 deducible
= uses_deducible_template_parms (elt
);
16614 deducible
= deducible_expression (elt
);
16622 /* Returns true iff TYPE contains any deducible references to template
16623 parameters, as per 14.8.2.5. */
16626 uses_deducible_template_parms (tree type
)
16628 if (PACK_EXPANSION_P (type
))
16629 type
= PACK_EXPANSION_PATTERN (type
);
16636 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
16637 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16643 if (POINTER_TYPE_P (type
))
16644 return uses_deducible_template_parms (TREE_TYPE (type
));
16646 /* T[integer-constant ]
16648 if (TREE_CODE (type
) == ARRAY_TYPE
)
16649 return (uses_deducible_template_parms (TREE_TYPE (type
))
16650 || deducible_array_bound (TYPE_DOMAIN (type
)));
16662 if (TYPE_PTRMEM_P (type
))
16663 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
16664 || (uses_deducible_template_parms
16665 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
16667 /* template-name <T> (where template-name refers to a class template)
16668 template-name <i> (where template-name refers to a class template) */
16669 if (CLASS_TYPE_P (type
)
16670 && CLASSTYPE_TEMPLATE_INFO (type
)
16671 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
16672 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
16673 (CLASSTYPE_TI_ARGS (type
)));
16678 if (TREE_CODE (type
) == FUNCTION_TYPE
16679 || TREE_CODE (type
) == METHOD_TYPE
)
16681 if (uses_deducible_template_parms (TREE_TYPE (type
)))
16683 tree parm
= TYPE_ARG_TYPES (type
);
16684 if (TREE_CODE (type
) == METHOD_TYPE
)
16685 parm
= TREE_CHAIN (parm
);
16686 for (; parm
; parm
= TREE_CHAIN (parm
))
16687 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
16694 /* Subroutine of type_unification_real and unify_pack_expansion to
16695 handle unification of a single P/A pair. Parameters are as
16696 for those functions. */
16699 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
16700 int subr
, unification_kind_t strict
,
16703 tree arg_expr
= NULL_TREE
;
16706 if (arg
== error_mark_node
|| parm
== error_mark_node
)
16707 return unify_invalid (explain_p
);
16708 if (arg
== unknown_type_node
)
16709 /* We can't deduce anything from this, but we might get all the
16710 template args from other function args. */
16711 return unify_success (explain_p
);
16713 /* Implicit conversions (Clause 4) will be performed on a function
16714 argument to convert it to the type of the corresponding function
16715 parameter if the parameter type contains no template-parameters that
16716 participate in template argument deduction. */
16717 if (strict
!= DEDUCE_EXACT
16718 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
16719 /* For function parameters with no deducible template parameters,
16720 just return. We'll check non-dependent conversions later. */
16721 return unify_success (explain_p
);
16726 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
16727 | UNIFY_ALLOW_MORE_CV_QUAL
16728 | UNIFY_ALLOW_DERIVED
);
16732 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
16736 arg_strict
= UNIFY_ALLOW_NONE
;
16740 gcc_unreachable ();
16743 /* We only do these transformations if this is the top-level
16744 parameter_type_list in a call or declaration matching; in other
16745 situations (nested function declarators, template argument lists) we
16746 won't be comparing a type to an expression, and we don't do any type
16752 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
16753 if (type_unknown_p (arg
))
16755 /* [temp.deduct.type] A template-argument can be
16756 deduced from a pointer to function or pointer
16757 to member function argument if the set of
16758 overloaded functions does not contain function
16759 templates and at most one of a set of
16760 overloaded functions provides a unique
16763 if (resolve_overloaded_unification
16764 (tparms
, targs
, parm
, arg
, strict
,
16765 arg_strict
, explain_p
))
16766 return unify_success (explain_p
);
16767 return unify_overload_resolution_failure (explain_p
, arg
);
16771 arg
= unlowered_expr_type (arg
);
16772 if (arg
== error_mark_node
)
16773 return unify_invalid (explain_p
);
16777 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
16780 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
16781 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
16782 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16784 /* For deduction from an init-list we need the actual list. */
16785 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
16787 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
16790 /* Most parms like fn_type_unification.
16792 If SUBR is 1, we're being called recursively (to unify the
16793 arguments of a function or method parameter of a function
16796 CHECKS is a pointer to a vector of access checks encountered while
16797 substituting default template arguments. */
16800 type_unification_real (tree tparms
,
16804 unsigned int xnargs
,
16806 unification_kind_t strict
,
16808 vec
<deferred_access_check
, va_gc
> **checks
,
16813 int ntparms
= TREE_VEC_LENGTH (tparms
);
16814 int saw_undeduced
= 0;
16817 unsigned int nargs
;
16820 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
16821 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
16822 gcc_assert (ntparms
> 0);
16824 /* Reset the number of non-defaulted template arguments contained
16826 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
16834 while (parms
&& parms
!= void_list_node
16837 parm
= TREE_VALUE (parms
);
16839 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
16840 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
16841 /* For a function parameter pack that occurs at the end of the
16842 parameter-declaration-list, the type A of each remaining
16843 argument of the call is compared with the type P of the
16844 declarator-id of the function parameter pack. */
16847 parms
= TREE_CHAIN (parms
);
16849 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
16850 /* For a function parameter pack that does not occur at the
16851 end of the parameter-declaration-list, the type of the
16852 parameter pack is a non-deduced context. */
16858 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
16864 && parms
!= void_list_node
16865 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
16867 /* Unify the remaining arguments with the pack expansion type. */
16869 tree parmvec
= make_tree_vec (1);
16871 /* Allocate a TREE_VEC and copy in all of the arguments */
16872 argvec
= make_tree_vec (nargs
- ia
);
16873 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
16874 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
16876 /* Copy the parameter into parmvec. */
16877 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
16878 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
16879 /*subr=*/subr
, explain_p
))
16882 /* Advance to the end of the list of parameters. */
16883 parms
= TREE_CHAIN (parms
);
16886 /* Fail if we've reached the end of the parm list, and more args
16887 are present, and the parm list isn't variadic. */
16888 if (ia
< nargs
&& parms
== void_list_node
)
16889 return unify_too_many_arguments (explain_p
, nargs
, ia
);
16890 /* Fail if parms are left and they don't have default values and
16891 they aren't all deduced as empty packs (c++/57397). This is
16892 consistent with sufficient_parms_p. */
16893 if (parms
&& parms
!= void_list_node
16894 && TREE_PURPOSE (parms
) == NULL_TREE
)
16896 unsigned int count
= nargs
;
16901 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
16904 p
= TREE_CHAIN (p
);
16906 while (p
&& p
!= void_list_node
);
16907 if (count
!= nargs
)
16908 return unify_too_few_arguments (explain_p
, ia
, count
,
16914 tsubst_flags_t complain
= (explain_p
16915 ? tf_warning_or_error
16918 for (i
= 0; i
< ntparms
; i
++)
16920 tree targ
= TREE_VEC_ELT (targs
, i
);
16921 tree tparm
= TREE_VEC_ELT (tparms
, i
);
16923 /* Clear the "incomplete" flags on all argument packs now so that
16924 substituting them into later default arguments works. */
16925 if (targ
&& ARGUMENT_PACK_P (targ
))
16927 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
16928 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
16931 if (targ
|| tparm
== error_mark_node
)
16933 tparm
= TREE_VALUE (tparm
);
16935 /* If this is an undeduced nontype parameter that depends on
16936 a type parameter, try another pass; its type may have been
16937 deduced from a later argument than the one from which
16938 this parameter can be deduced. */
16939 if (TREE_CODE (tparm
) == PARM_DECL
16940 && uses_template_parms (TREE_TYPE (tparm
))
16941 && saw_undeduced
< 2)
16947 /* Core issue #226 (C++0x) [temp.deduct]:
16949 If a template argument has not been deduced, its
16950 default template argument, if any, is used.
16952 When we are in C++98 mode, TREE_PURPOSE will either
16953 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16954 to explicitly check cxx_dialect here. */
16955 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
16956 /* OK, there is a default argument. Wait until after the
16957 conversion check to do substitution. */
16960 /* If the type parameter is a parameter pack, then it will
16961 be deduced to an empty parameter pack. */
16962 if (template_parameter_pack_p (tparm
))
16966 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
16968 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
16969 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
16970 TREE_CONSTANT (arg
) = 1;
16973 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
16975 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
16977 TREE_VEC_ELT (targs
, i
) = arg
;
16981 return unify_parameter_deduction_failure (explain_p
, tparm
);
16984 /* DR 1391: All parameters have args, now check non-dependent parms for
16986 if (saw_undeduced
< 2)
16987 for (ia
= 0, parms
= xparms
, args
= xargs
, nargs
= xnargs
;
16988 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
16990 parm
= TREE_VALUE (parms
);
16992 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
16993 && (!TREE_CHAIN (parms
)
16994 || TREE_CHAIN (parms
) == void_list_node
))
16995 /* For a function parameter pack that occurs at the end of the
16996 parameter-declaration-list, the type A of each remaining
16997 argument of the call is compared with the type P of the
16998 declarator-id of the function parameter pack. */
17001 parms
= TREE_CHAIN (parms
);
17003 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
17004 /* For a function parameter pack that does not occur at the
17005 end of the parameter-declaration-list, the type of the
17006 parameter pack is a non-deduced context. */
17012 if (uses_template_parms (parm
))
17014 if (check_non_deducible_conversion (parm
, arg
, strict
, flags
,
17019 /* Now substitute into the default template arguments. */
17020 for (i
= 0; i
< ntparms
; i
++)
17022 tree targ
= TREE_VEC_ELT (targs
, i
);
17023 tree tparm
= TREE_VEC_ELT (tparms
, i
);
17025 if (targ
|| tparm
== error_mark_node
)
17027 tree parm
= TREE_VALUE (tparm
);
17029 if (TREE_CODE (parm
) == PARM_DECL
17030 && uses_template_parms (TREE_TYPE (parm
))
17031 && saw_undeduced
< 2)
17034 tree arg
= TREE_PURPOSE (tparm
);
17035 reopen_deferring_access_checks (*checks
);
17036 location_t save_loc
= input_location
;
17038 input_location
= DECL_SOURCE_LOCATION (parm
);
17039 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
17040 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
17042 input_location
= save_loc
;
17043 *checks
= get_deferred_access_checks ();
17044 pop_deferring_access_checks ();
17045 if (arg
== error_mark_node
)
17049 TREE_VEC_ELT (targs
, i
) = arg
;
17050 /* The position of the first default template argument,
17051 is also the number of non-defaulted arguments in TARGS.
17053 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
17054 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
17059 if (saw_undeduced
++ == 1)
17062 #ifdef ENABLE_CHECKING
17063 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
17064 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
17067 return unify_success (explain_p
);
17070 /* Subroutine of type_unification_real. Args are like the variables
17071 at the call site. ARG is an overloaded function (or template-id);
17072 we try deducing template args from each of the overloads, and if
17073 only one succeeds, we go with that. Modifies TARGS and returns
17074 true on success. */
17077 resolve_overloaded_unification (tree tparms
,
17081 unification_kind_t strict
,
17085 tree tempargs
= copy_node (targs
);
17087 tree goodfn
= NULL_TREE
;
17090 if (TREE_CODE (arg
) == ADDR_EXPR
)
17092 arg
= TREE_OPERAND (arg
, 0);
17098 if (TREE_CODE (arg
) == COMPONENT_REF
)
17099 /* Handle `&x' where `x' is some static or non-static member
17101 arg
= TREE_OPERAND (arg
, 1);
17103 if (TREE_CODE (arg
) == OFFSET_REF
)
17104 arg
= TREE_OPERAND (arg
, 1);
17106 /* Strip baselink information. */
17107 if (BASELINK_P (arg
))
17108 arg
= BASELINK_FUNCTIONS (arg
);
17110 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
17112 /* If we got some explicit template args, we need to plug them into
17113 the affected templates before we try to unify, in case the
17114 explicit args will completely resolve the templates in question. */
17117 tree expl_subargs
= TREE_OPERAND (arg
, 1);
17118 arg
= TREE_OPERAND (arg
, 0);
17120 for (; arg
; arg
= OVL_NEXT (arg
))
17122 tree fn
= OVL_CURRENT (arg
);
17123 tree subargs
, elem
;
17125 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
17128 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
17129 expl_subargs
, NULL_TREE
, tf_none
,
17130 /*require_all_args=*/true,
17131 /*use_default_args=*/true);
17132 if (subargs
!= error_mark_node
17133 && !any_dependent_template_arguments_p (subargs
))
17135 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
17136 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
17137 elem
, strict
, sub_strict
, addr_p
, explain_p
)
17138 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
17147 /* If no templates (or more than one) are fully resolved by the
17148 explicit arguments, this template-id is a non-deduced context; it
17149 could still be OK if we deduce all template arguments for the
17150 enclosing call through other arguments. */
17154 else if (TREE_CODE (arg
) != OVERLOAD
17155 && TREE_CODE (arg
) != FUNCTION_DECL
)
17156 /* If ARG is, for example, "(0, &f)" then its type will be unknown
17157 -- but the deduction does not succeed because the expression is
17158 not just the function on its own. */
17161 for (; arg
; arg
= OVL_NEXT (arg
))
17162 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
17163 TREE_TYPE (OVL_CURRENT (arg
)),
17164 strict
, sub_strict
, addr_p
, explain_p
)
17165 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
17167 goodfn
= OVL_CURRENT (arg
);
17171 /* [temp.deduct.type] A template-argument can be deduced from a pointer
17172 to function or pointer to member function argument if the set of
17173 overloaded functions does not contain function templates and at most
17174 one of a set of overloaded functions provides a unique match.
17176 So if we found multiple possibilities, we return success but don't
17177 deduce anything. */
17181 int i
= TREE_VEC_LENGTH (targs
);
17183 if (TREE_VEC_ELT (tempargs
, i
))
17185 tree old
= TREE_VEC_ELT (targs
, i
);
17186 tree new_
= TREE_VEC_ELT (tempargs
, i
);
17187 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
17188 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
17189 /* Don't forget explicit template arguments in a pack. */
17190 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
17191 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
17192 TREE_VEC_ELT (targs
, i
) = new_
;
17201 /* Core DR 115: In contexts where deduction is done and fails, or in
17202 contexts where deduction is not done, if a template argument list is
17203 specified and it, along with any default template arguments, identifies
17204 a single function template specialization, then the template-id is an
17205 lvalue for the function template specialization. */
17208 resolve_nondeduced_context (tree orig_expr
)
17210 tree expr
, offset
, baselink
;
17213 if (!type_unknown_p (orig_expr
))
17218 offset
= NULL_TREE
;
17219 baselink
= NULL_TREE
;
17221 if (TREE_CODE (expr
) == ADDR_EXPR
)
17223 expr
= TREE_OPERAND (expr
, 0);
17226 if (TREE_CODE (expr
) == OFFSET_REF
)
17229 expr
= TREE_OPERAND (expr
, 1);
17231 if (BASELINK_P (expr
))
17234 expr
= BASELINK_FUNCTIONS (expr
);
17237 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
17240 tree goodfn
= NULL_TREE
;
17242 /* If we got some explicit template args, we need to plug them into
17243 the affected templates before we try to unify, in case the
17244 explicit args will completely resolve the templates in question. */
17246 tree expl_subargs
= TREE_OPERAND (expr
, 1);
17247 tree arg
= TREE_OPERAND (expr
, 0);
17248 tree badfn
= NULL_TREE
;
17249 tree badargs
= NULL_TREE
;
17251 for (; arg
; arg
= OVL_NEXT (arg
))
17253 tree fn
= OVL_CURRENT (arg
);
17254 tree subargs
, elem
;
17256 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
17259 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
17260 expl_subargs
, NULL_TREE
, tf_none
,
17261 /*require_all_args=*/true,
17262 /*use_default_args=*/true);
17263 if (subargs
!= error_mark_node
17264 && !any_dependent_template_arguments_p (subargs
))
17266 elem
= instantiate_template (fn
, subargs
, tf_none
);
17267 if (elem
== error_mark_node
)
17272 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
17281 mark_used (goodfn
);
17284 expr
= build_baselink (BASELINK_BINFO (baselink
),
17285 BASELINK_ACCESS_BINFO (baselink
),
17286 expr
, BASELINK_OPTYPE (baselink
));
17290 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
17291 expr
= build_offset_ref (base
, expr
, addr
, tf_warning_or_error
);
17294 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
17297 else if (good
== 0 && badargs
)
17298 /* There were no good options and at least one bad one, so let the
17299 user know what the problem is. */
17300 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
17305 /* Subroutine of resolve_overloaded_unification; does deduction for a single
17306 overload. Fills TARGS with any deduced arguments, or error_mark_node if
17307 different overloads deduce different arguments for a given parm.
17308 ADDR_P is true if the expression for which deduction is being
17309 performed was of the form "& fn" rather than simply "fn".
17311 Returns 1 on success. */
17314 try_one_overload (tree tparms
,
17319 unification_kind_t strict
,
17328 if (arg
== error_mark_node
)
17331 /* [temp.deduct.type] A template-argument can be deduced from a pointer
17332 to function or pointer to member function argument if the set of
17333 overloaded functions does not contain function templates and at most
17334 one of a set of overloaded functions provides a unique match.
17336 So if this is a template, just return success. */
17338 if (uses_template_parms (arg
))
17341 if (TREE_CODE (arg
) == METHOD_TYPE
)
17342 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
17344 arg
= build_pointer_type (arg
);
17346 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
17348 /* We don't copy orig_targs for this because if we have already deduced
17349 some template args from previous args, unify would complain when we
17350 try to deduce a template parameter for the same argument, even though
17351 there isn't really a conflict. */
17352 nargs
= TREE_VEC_LENGTH (targs
);
17353 tempargs
= make_tree_vec (nargs
);
17355 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
17358 /* First make sure we didn't deduce anything that conflicts with
17359 explicitly specified args. */
17360 for (i
= nargs
; i
--; )
17362 tree elt
= TREE_VEC_ELT (tempargs
, i
);
17363 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
17367 else if (uses_template_parms (elt
))
17368 /* Since we're unifying against ourselves, we will fill in
17369 template args used in the function parm list with our own
17370 template parms. Discard them. */
17371 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
17372 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
17376 for (i
= nargs
; i
--; )
17378 tree elt
= TREE_VEC_ELT (tempargs
, i
);
17381 TREE_VEC_ELT (targs
, i
) = elt
;
17387 /* PARM is a template class (perhaps with unbound template
17388 parameters). ARG is a fully instantiated type. If ARG can be
17389 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
17390 TARGS are as for unify. */
17393 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
17396 tree copy_of_targs
;
17398 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
17399 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
17400 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
17403 /* We need to make a new template argument vector for the call to
17404 unify. If we used TARGS, we'd clutter it up with the result of
17405 the attempted unification, even if this class didn't work out.
17406 We also don't want to commit ourselves to all the unifications
17407 we've already done, since unification is supposed to be done on
17408 an argument-by-argument basis. In other words, consider the
17409 following pathological case:
17411 template <int I, int J, int K>
17414 template <int I, int J>
17415 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
17417 template <int I, int J, int K>
17418 void f(S<I, J, K>, S<I, I, I>);
17427 Now, by the time we consider the unification involving `s2', we
17428 already know that we must have `f<0, 0, 0>'. But, even though
17429 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
17430 because there are two ways to unify base classes of S<0, 1, 2>
17431 with S<I, I, I>. If we kept the already deduced knowledge, we
17432 would reject the possibility I=1. */
17433 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
17435 /* If unification failed, we're done. */
17436 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
17437 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
17443 /* Given a template type PARM and a class type ARG, find the unique
17444 base type in ARG that is an instance of PARM. We do not examine
17445 ARG itself; only its base-classes. If there is not exactly one
17446 appropriate base class, return NULL_TREE. PARM may be the type of
17447 a partial specialization, as well as a plain template type. Used
17450 static enum template_base_result
17451 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
17452 bool explain_p
, tree
*result
)
17454 tree rval
= NULL_TREE
;
17457 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
17459 binfo
= TYPE_BINFO (complete_type (arg
));
17462 /* The type could not be completed. */
17463 *result
= NULL_TREE
;
17464 return tbr_incomplete_type
;
17467 /* Walk in inheritance graph order. The search order is not
17468 important, and this avoids multiple walks of virtual bases. */
17469 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
17471 tree r
= try_class_unification (tparms
, targs
, parm
,
17472 BINFO_TYPE (binfo
), explain_p
);
17476 /* If there is more than one satisfactory baseclass, then:
17480 If they yield more than one possible deduced A, the type
17484 if (rval
&& !same_type_p (r
, rval
))
17486 *result
= NULL_TREE
;
17487 return tbr_ambiguous_baseclass
;
17495 return tbr_success
;
17498 /* Returns the level of DECL, which declares a template parameter. */
17501 template_decl_level (tree decl
)
17503 switch (TREE_CODE (decl
))
17506 case TEMPLATE_DECL
:
17507 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
17510 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
17513 gcc_unreachable ();
17518 /* Decide whether ARG can be unified with PARM, considering only the
17519 cv-qualifiers of each type, given STRICT as documented for unify.
17520 Returns nonzero iff the unification is OK on that basis. */
17523 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
17525 int arg_quals
= cp_type_quals (arg
);
17526 int parm_quals
= cp_type_quals (parm
);
17528 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17529 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
17531 /* Although a CVR qualifier is ignored when being applied to a
17532 substituted template parameter ([8.3.2]/1 for example), that
17533 does not allow us to unify "const T" with "int&" because both
17534 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
17535 It is ok when we're allowing additional CV qualifiers
17536 at the outer level [14.8.2.1]/3,1st bullet. */
17537 if ((TREE_CODE (arg
) == REFERENCE_TYPE
17538 || TREE_CODE (arg
) == FUNCTION_TYPE
17539 || TREE_CODE (arg
) == METHOD_TYPE
)
17540 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
17543 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
17544 && (parm_quals
& TYPE_QUAL_RESTRICT
))
17548 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
17549 && (arg_quals
& parm_quals
) != parm_quals
)
17552 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
17553 && (parm_quals
& arg_quals
) != arg_quals
)
17559 /* Determines the LEVEL and INDEX for the template parameter PARM. */
17561 template_parm_level_and_index (tree parm
, int* level
, int* index
)
17563 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17564 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
17565 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17567 *index
= TEMPLATE_TYPE_IDX (parm
);
17568 *level
= TEMPLATE_TYPE_LEVEL (parm
);
17572 *index
= TEMPLATE_PARM_IDX (parm
);
17573 *level
= TEMPLATE_PARM_LEVEL (parm
);
17577 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
17579 if (unify (TP, TA, P, A, S, EP)) \
17583 /* Unifies the remaining arguments in PACKED_ARGS with the pack
17584 expansion at the end of PACKED_PARMS. Returns 0 if the type
17585 deduction succeeds, 1 otherwise. STRICT is the same as in
17586 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
17587 call argument list. We'll need to adjust the arguments to make them
17588 types. SUBR tells us if this is from a recursive call to
17589 type_unification_real, or for comparing two template argument
17593 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
17594 tree packed_args
, unification_kind_t strict
,
17595 bool subr
, bool explain_p
)
17598 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
17599 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
17600 tree pack
, packs
= NULL_TREE
;
17601 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
17603 packed_args
= expand_template_argument_pack (packed_args
);
17605 int len
= TREE_VEC_LENGTH (packed_args
);
17607 /* Determine the parameter packs we will be deducing from the
17608 pattern, and record their current deductions. */
17609 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
17610 pack
; pack
= TREE_CHAIN (pack
))
17612 tree parm_pack
= TREE_VALUE (pack
);
17615 /* Determine the index and level of this parameter pack. */
17616 template_parm_level_and_index (parm_pack
, &level
, &idx
);
17618 /* Keep track of the parameter packs and their corresponding
17620 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
17621 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
17624 /* Loop through all of the arguments that have not yet been
17625 unified and unify each with the pattern. */
17626 for (i
= start
; i
< len
; i
++)
17629 bool any_explicit
= false;
17630 tree arg
= TREE_VEC_ELT (packed_args
, i
);
17632 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
17633 or the element of its argument pack at the current index if
17634 this argument was explicitly specified. */
17635 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
17639 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
17642 if (TREE_VALUE (pack
)
17643 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
17644 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
17646 any_explicit
= true;
17647 arg
= TREE_VEC_ELT (pargs
, i
- start
);
17649 TMPL_ARG (targs
, level
, idx
) = arg
;
17652 /* If we had explicit template arguments, substitute them into the
17653 pattern before deduction. */
17656 /* Some arguments might still be unspecified or dependent. */
17658 ++processing_template_decl
;
17659 dependent
= any_dependent_template_arguments_p (targs
);
17661 --processing_template_decl
;
17662 parm
= tsubst (pattern
, targs
,
17663 explain_p
? tf_warning_or_error
: tf_none
,
17666 --processing_template_decl
;
17667 if (parm
== error_mark_node
)
17673 /* Unify the pattern with the current argument. */
17674 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
17678 /* For each parameter pack, collect the deduced value. */
17679 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
17682 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
17684 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
17685 TMPL_ARG (targs
, level
, idx
);
17689 /* Verify that the results of unification with the parameter packs
17690 produce results consistent with what we've seen before, and make
17691 the deduced argument packs available. */
17692 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
17694 tree old_pack
= TREE_VALUE (pack
);
17695 tree new_args
= TREE_TYPE (pack
);
17696 int i
, len
= TREE_VEC_LENGTH (new_args
);
17698 bool nondeduced_p
= false;
17700 /* By default keep the original deduced argument pack.
17701 If necessary, more specific code is going to update the
17702 resulting deduced argument later down in this function. */
17703 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
17704 TMPL_ARG (targs
, level
, idx
) = old_pack
;
17706 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
17707 actually deduce anything. */
17708 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
17709 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
17710 nondeduced_p
= true;
17714 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
17716 /* If we had fewer function args than explicit template args,
17717 just use the explicits. */
17718 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
17719 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
17720 if (len
< explicit_len
)
17721 new_args
= explicit_args
;
17727 /* Build the deduced *_ARGUMENT_PACK. */
17728 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
17730 result
= make_node (NONTYPE_ARGUMENT_PACK
);
17731 TREE_TYPE (result
) =
17732 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
17733 TREE_CONSTANT (result
) = 1;
17736 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
17738 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
17740 /* Note the deduced argument packs for this parameter
17742 TMPL_ARG (targs
, level
, idx
) = result
;
17744 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
17745 && (ARGUMENT_PACK_ARGS (old_pack
)
17746 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
17748 /* We only had the explicitly-provided arguments before, but
17749 now we have a complete set of arguments. */
17750 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
17752 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
17753 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
17754 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
17758 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
17759 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
17761 if (!comp_template_args_with_info (old_args
, new_args
,
17762 &bad_old_arg
, &bad_new_arg
))
17763 /* Inconsistent unification of this parameter pack. */
17764 return unify_parameter_pack_inconsistent (explain_p
,
17770 return unify_success (explain_p
);
17773 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17774 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17775 parameters and return value are as for unify. */
17778 unify_array_domain (tree tparms
, tree targs
,
17779 tree parm_dom
, tree arg_dom
,
17787 /* Our representation of array types uses "N - 1" as the
17788 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17789 not an integer constant. We cannot unify arbitrarily
17790 complex expressions, so we eliminate the MINUS_EXPRs
17792 parm_max
= TYPE_MAX_VALUE (parm_dom
);
17793 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
17796 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
17797 parm_max
= TREE_OPERAND (parm_max
, 0);
17799 arg_max
= TYPE_MAX_VALUE (arg_dom
);
17800 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
17803 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17804 trying to unify the type of a variable with the type
17805 of a template parameter. For example:
17807 template <unsigned int N>
17808 void f (char (&) [N]);
17815 Here, the type of the ARG will be "int [g(i)]", and
17816 may be a SAVE_EXPR, etc. */
17817 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
17818 return unify_vla_arg (explain_p
, arg_dom
);
17819 arg_max
= TREE_OPERAND (arg_max
, 0);
17822 /* If only one of the bounds used a MINUS_EXPR, compensate
17823 by adding one to the other bound. */
17824 if (parm_cst
&& !arg_cst
)
17825 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
17829 else if (arg_cst
&& !parm_cst
)
17830 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
17835 return unify (tparms
, targs
, parm_max
, arg_max
,
17836 UNIFY_ALLOW_INTEGER
, explain_p
);
17839 /* Deduce the value of template parameters. TPARMS is the (innermost)
17840 set of template parameters to a template. TARGS is the bindings
17841 for those template parameters, as determined thus far; TARGS may
17842 include template arguments for outer levels of template parameters
17843 as well. PARM is a parameter to a template function, or a
17844 subcomponent of that parameter; ARG is the corresponding argument.
17845 This function attempts to match PARM with ARG in a manner
17846 consistent with the existing assignments in TARGS. If more values
17847 are deduced, then TARGS is updated.
17849 Returns 0 if the type deduction succeeds, 1 otherwise. The
17850 parameter STRICT is a bitwise or of the following flags:
17853 Require an exact match between PARM and ARG.
17854 UNIFY_ALLOW_MORE_CV_QUAL:
17855 Allow the deduced ARG to be more cv-qualified (by qualification
17856 conversion) than ARG.
17857 UNIFY_ALLOW_LESS_CV_QUAL:
17858 Allow the deduced ARG to be less cv-qualified than ARG.
17859 UNIFY_ALLOW_DERIVED:
17860 Allow the deduced ARG to be a template base class of ARG,
17861 or a pointer to a template base class of the type pointed to by
17863 UNIFY_ALLOW_INTEGER:
17864 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17865 case for more information.
17866 UNIFY_ALLOW_OUTER_LEVEL:
17867 This is the outermost level of a deduction. Used to determine validity
17868 of qualification conversions. A valid qualification conversion must
17869 have const qualified pointers leading up to the inner type which
17870 requires additional CV quals, except at the outer level, where const
17871 is not required [conv.qual]. It would be normal to set this flag in
17872 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17873 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17874 This is the outermost level of a deduction, and PARM can be more CV
17875 qualified at this point.
17876 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17877 This is the outermost level of a deduction, and PARM can be less CV
17878 qualified at this point. */
17881 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
17887 int strict_in
= strict
;
17889 /* I don't think this will do the right thing with respect to types.
17890 But the only case I've seen it in so far has been array bounds, where
17891 signedness is the only information lost, and I think that will be
17893 while (TREE_CODE (parm
) == NOP_EXPR
)
17894 parm
= TREE_OPERAND (parm
, 0);
17896 if (arg
== error_mark_node
)
17897 return unify_invalid (explain_p
);
17898 if (arg
== unknown_type_node
17899 || arg
== init_list_type_node
)
17900 /* We can't deduce anything from this, but we might get all the
17901 template args from other function args. */
17902 return unify_success (explain_p
);
17904 /* If PARM uses template parameters, then we can't bail out here,
17905 even if ARG == PARM, since we won't record unifications for the
17906 template parameters. We might need them if we're trying to
17907 figure out which of two things is more specialized. */
17908 if (arg
== parm
&& !uses_template_parms (parm
))
17909 return unify_success (explain_p
);
17911 /* Handle init lists early, so the rest of the function can assume
17912 we're dealing with a type. */
17913 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
17917 tree orig_parm
= parm
;
17919 /* Replace T with std::initializer_list<T> for deduction. */
17920 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17921 && flag_deduce_init_list
)
17922 parm
= listify (parm
);
17924 if (!is_std_init_list (parm
)
17925 && TREE_CODE (parm
) != ARRAY_TYPE
)
17926 /* We can only deduce from an initializer list argument if the
17927 parameter is std::initializer_list or an array; otherwise this
17928 is a non-deduced context. */
17929 return unify_success (explain_p
);
17931 if (TREE_CODE (parm
) == ARRAY_TYPE
)
17932 elttype
= TREE_TYPE (parm
);
17935 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
17936 /* Deduction is defined in terms of a single type, so just punt
17937 on the (bizarre) std::initializer_list<T...>. */
17938 if (PACK_EXPANSION_P (elttype
))
17939 return unify_success (explain_p
);
17942 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
17944 int elt_strict
= strict
;
17946 if (elt
== error_mark_node
)
17947 return unify_invalid (explain_p
);
17949 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
17951 tree type
= TREE_TYPE (elt
);
17952 if (type
== error_mark_node
)
17953 return unify_invalid (explain_p
);
17954 /* It should only be possible to get here for a call. */
17955 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
17956 elt_strict
|= maybe_adjust_types_for_deduction
17957 (DEDUCE_CALL
, &elttype
, &type
, elt
);
17961 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
17965 if (TREE_CODE (parm
) == ARRAY_TYPE
17966 && deducible_array_bound (TYPE_DOMAIN (parm
)))
17968 /* Also deduce from the length of the initializer list. */
17969 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
17970 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
17971 if (idx
== error_mark_node
)
17972 return unify_invalid (explain_p
);
17973 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
17977 /* If the std::initializer_list<T> deduction worked, replace the
17978 deduced A with std::initializer_list<A>. */
17979 if (orig_parm
!= parm
)
17981 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
17982 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17983 targ
= listify (targ
);
17984 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
17986 return unify_success (explain_p
);
17989 /* Immediately reject some pairs that won't unify because of
17990 cv-qualification mismatches. */
17991 if (TREE_CODE (arg
) == TREE_CODE (parm
)
17993 /* It is the elements of the array which hold the cv quals of an array
17994 type, and the elements might be template type parms. We'll check
17995 when we recurse. */
17996 && TREE_CODE (arg
) != ARRAY_TYPE
17997 /* We check the cv-qualifiers when unifying with template type
17998 parameters below. We want to allow ARG `const T' to unify with
17999 PARM `T' for example, when computing which of two templates
18000 is more specialized, for example. */
18001 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
18002 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
18003 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
18005 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
18006 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
18007 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
18008 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
18009 strict
&= ~UNIFY_ALLOW_DERIVED
;
18010 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
18011 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
18013 switch (TREE_CODE (parm
))
18015 case TYPENAME_TYPE
:
18017 case UNBOUND_CLASS_TEMPLATE
:
18018 /* In a type which contains a nested-name-specifier, template
18019 argument values cannot be deduced for template parameters used
18020 within the nested-name-specifier. */
18021 return unify_success (explain_p
);
18023 case TEMPLATE_TYPE_PARM
:
18024 case TEMPLATE_TEMPLATE_PARM
:
18025 case BOUND_TEMPLATE_TEMPLATE_PARM
:
18026 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
18027 if (error_operand_p (tparm
))
18028 return unify_invalid (explain_p
);
18030 if (TEMPLATE_TYPE_LEVEL (parm
)
18031 != template_decl_level (tparm
))
18032 /* The PARM is not one we're trying to unify. Just check
18033 to see if it matches ARG. */
18035 if (TREE_CODE (arg
) == TREE_CODE (parm
)
18036 && (is_auto (parm
) ? is_auto (arg
)
18037 : same_type_p (parm
, arg
)))
18038 return unify_success (explain_p
);
18040 return unify_type_mismatch (explain_p
, parm
, arg
);
18042 idx
= TEMPLATE_TYPE_IDX (parm
);
18043 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
18044 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
18045 if (error_operand_p (tparm
))
18046 return unify_invalid (explain_p
);
18048 /* Check for mixed types and values. */
18049 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
18050 && TREE_CODE (tparm
) != TYPE_DECL
)
18051 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
18052 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
18053 gcc_unreachable ();
18055 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
18057 /* ARG must be constructed from a template class or a template
18058 template parameter. */
18059 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
18060 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
18061 return unify_template_deduction_failure (explain_p
, parm
, arg
);
18063 tree parmvec
= TYPE_TI_ARGS (parm
);
18064 /* An alias template name is never deduced. */
18065 if (TYPE_ALIAS_P (arg
))
18066 arg
= strip_typedefs (arg
);
18067 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
18068 tree full_argvec
= add_to_template_args (targs
, argvec
);
18070 = DECL_INNERMOST_TEMPLATE_PARMS
18071 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
18073 int parm_variadic_p
= 0;
18075 /* The resolution to DR150 makes clear that default
18076 arguments for an N-argument may not be used to bind T
18077 to a template template parameter with fewer than N
18078 parameters. It is not safe to permit the binding of
18079 default arguments as an extension, as that may change
18080 the meaning of a conforming program. Consider:
18082 struct Dense { static const unsigned int dim = 1; };
18084 template <template <typename> class View,
18086 void operator+(float, View<Block> const&);
18088 template <typename Block,
18089 unsigned int Dim = Block::dim>
18090 struct Lvalue_proxy { operator float() const; };
18094 Lvalue_proxy<Dense> p;
18099 Here, if Lvalue_proxy is permitted to bind to View, then
18100 the global operator+ will be used; if they are not, the
18101 Lvalue_proxy will be converted to float. */
18102 if (coerce_template_parms (parm_parms
,
18104 TYPE_TI_TEMPLATE (parm
),
18106 ? tf_warning_or_error
18108 /*require_all_args=*/true,
18109 /*use_default_args=*/false)
18110 == error_mark_node
)
18113 /* Deduce arguments T, i from TT<T> or TT<i>.
18114 We check each element of PARMVEC and ARGVEC individually
18115 rather than the whole TREE_VEC since they can have
18116 different number of elements. */
18118 parmvec
= expand_template_argument_pack (parmvec
);
18119 argvec
= expand_template_argument_pack (argvec
);
18121 len
= TREE_VEC_LENGTH (parmvec
);
18123 /* Check if the parameters end in a pack, making them
18126 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
18127 parm_variadic_p
= 1;
18129 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
18130 /* If the template argument list of P contains a pack
18131 expansion that is not the last template argument, the
18132 entire template argument list is a non-deduced
18134 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
18135 return unify_success (explain_p
);
18137 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
18138 return unify_too_few_arguments (explain_p
,
18139 TREE_VEC_LENGTH (argvec
), len
);
18141 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
18143 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
18144 TREE_VEC_ELT (parmvec
, i
),
18145 TREE_VEC_ELT (argvec
, i
),
18146 UNIFY_ALLOW_NONE
, explain_p
);
18149 if (parm_variadic_p
18150 && unify_pack_expansion (tparms
, targs
,
18153 /*subr=*/true, explain_p
))
18156 arg
= TYPE_TI_TEMPLATE (arg
);
18158 /* Fall through to deduce template name. */
18161 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
18162 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
18164 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
18166 /* Simple cases: Value already set, does match or doesn't. */
18167 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
18168 return unify_success (explain_p
);
18170 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
18174 /* If PARM is `const T' and ARG is only `int', we don't have
18175 a match unless we are allowing additional qualification.
18176 If ARG is `const int' and PARM is just `T' that's OK;
18177 that binds `const int' to `T'. */
18178 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
18180 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
18182 /* Consider the case where ARG is `const volatile int' and
18183 PARM is `const T'. Then, T should be `volatile int'. */
18184 arg
= cp_build_qualified_type_real
18185 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
18186 if (arg
== error_mark_node
)
18187 return unify_invalid (explain_p
);
18189 /* Simple cases: Value already set, does match or doesn't. */
18190 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
18191 return unify_success (explain_p
);
18193 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
18195 /* Make sure that ARG is not a variable-sized array. (Note
18196 that were talking about variable-sized arrays (like
18197 `int[n]'), rather than arrays of unknown size (like
18198 `int[]').) We'll get very confused by such a type since
18199 the bound of the array is not constant, and therefore
18200 not mangleable. Besides, such types are not allowed in
18201 ISO C++, so we can do as we please here. We do allow
18202 them for 'auto' deduction, since that isn't ABI-exposed. */
18203 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
18204 return unify_vla_arg (explain_p
, arg
);
18206 /* Strip typedefs as in convert_template_argument. */
18207 arg
= canonicalize_type_argument (arg
, tf_none
);
18210 /* If ARG is a parameter pack or an expansion, we cannot unify
18211 against it unless PARM is also a parameter pack. */
18212 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
18213 && !template_parameter_pack_p (parm
))
18214 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
18216 /* If the argument deduction results is a METHOD_TYPE,
18217 then there is a problem.
18218 METHOD_TYPE doesn't map to any real C++ type the result of
18219 the deduction can not be of that type. */
18220 if (TREE_CODE (arg
) == METHOD_TYPE
)
18221 return unify_method_type_error (explain_p
, arg
);
18223 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
18224 return unify_success (explain_p
);
18226 case TEMPLATE_PARM_INDEX
:
18227 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
18228 if (error_operand_p (tparm
))
18229 return unify_invalid (explain_p
);
18231 if (TEMPLATE_PARM_LEVEL (parm
)
18232 != template_decl_level (tparm
))
18234 /* The PARM is not one we're trying to unify. Just check
18235 to see if it matches ARG. */
18236 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
18237 && cp_tree_equal (parm
, arg
));
18239 unify_expression_unequal (explain_p
, parm
, arg
);
18243 idx
= TEMPLATE_PARM_IDX (parm
);
18244 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
18248 int x
= !cp_tree_equal (targ
, arg
);
18250 unify_inconsistency (explain_p
, parm
, targ
, arg
);
18254 /* [temp.deduct.type] If, in the declaration of a function template
18255 with a non-type template-parameter, the non-type
18256 template-parameter is used in an expression in the function
18257 parameter-list and, if the corresponding template-argument is
18258 deduced, the template-argument type shall match the type of the
18259 template-parameter exactly, except that a template-argument
18260 deduced from an array bound may be of any integral type.
18261 The non-type parameter might use already deduced type parameters. */
18262 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
18263 if (!TREE_TYPE (arg
))
18264 /* Template-parameter dependent expression. Just accept it for now.
18265 It will later be processed in convert_template_argument. */
18267 else if (same_type_p (TREE_TYPE (arg
), tparm
))
18269 else if ((strict
& UNIFY_ALLOW_INTEGER
)
18270 && CP_INTEGRAL_TYPE_P (tparm
))
18271 /* Convert the ARG to the type of PARM; the deduced non-type
18272 template argument must exactly match the types of the
18273 corresponding parameter. */
18274 arg
= fold (build_nop (tparm
, arg
));
18275 else if (uses_template_parms (tparm
))
18276 /* We haven't deduced the type of this parameter yet. Try again
18278 return unify_success (explain_p
);
18280 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
18282 /* If ARG is a parameter pack or an expansion, we cannot unify
18283 against it unless PARM is also a parameter pack. */
18284 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
18285 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
18286 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
18289 bool removed_attr
= false;
18290 arg
= strip_typedefs_expr (arg
, &removed_attr
);
18292 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
18293 return unify_success (explain_p
);
18297 /* A pointer-to-member constant can be unified only with
18298 another constant. */
18299 if (TREE_CODE (arg
) != PTRMEM_CST
)
18300 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
18302 /* Just unify the class member. It would be useless (and possibly
18303 wrong, depending on the strict flags) to unify also
18304 PTRMEM_CST_CLASS, because we want to be sure that both parm and
18305 arg refer to the same variable, even if through different
18306 classes. For instance:
18308 struct A { int x; };
18311 Unification of &A::x and &B::x must succeed. */
18312 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
18313 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
18318 if (!TYPE_PTR_P (arg
))
18319 return unify_type_mismatch (explain_p
, parm
, arg
);
18321 /* [temp.deduct.call]
18323 A can be another pointer or pointer to member type that can
18324 be converted to the deduced A via a qualification
18325 conversion (_conv.qual_).
18327 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
18328 This will allow for additional cv-qualification of the
18329 pointed-to types if appropriate. */
18331 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
18332 /* The derived-to-base conversion only persists through one
18333 level of pointers. */
18334 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
18336 return unify (tparms
, targs
, TREE_TYPE (parm
),
18337 TREE_TYPE (arg
), strict
, explain_p
);
18340 case REFERENCE_TYPE
:
18341 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
18342 return unify_type_mismatch (explain_p
, parm
, arg
);
18343 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
18344 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
18347 if (TREE_CODE (arg
) != ARRAY_TYPE
)
18348 return unify_type_mismatch (explain_p
, parm
, arg
);
18349 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
18350 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
18351 return unify_type_mismatch (explain_p
, parm
, arg
);
18352 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
18353 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
18354 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
18355 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
18356 TYPE_DOMAIN (arg
), explain_p
);
18357 return unify_success (explain_p
);
18364 case ENUMERAL_TYPE
:
18367 if (TREE_CODE (arg
) != TREE_CODE (parm
))
18368 return unify_type_mismatch (explain_p
, parm
, arg
);
18370 /* We have already checked cv-qualification at the top of the
18372 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
18373 return unify_type_mismatch (explain_p
, parm
, arg
);
18375 /* As far as unification is concerned, this wins. Later checks
18376 will invalidate it if necessary. */
18377 return unify_success (explain_p
);
18379 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
18380 /* Type INTEGER_CST can come from ordinary constant template args. */
18382 while (TREE_CODE (arg
) == NOP_EXPR
)
18383 arg
= TREE_OPERAND (arg
, 0);
18385 if (TREE_CODE (arg
) != INTEGER_CST
)
18386 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
18387 return (tree_int_cst_equal (parm
, arg
)
18388 ? unify_success (explain_p
)
18389 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
18393 int i
, len
, argslen
;
18394 int parm_variadic_p
= 0;
18396 if (TREE_CODE (arg
) != TREE_VEC
)
18397 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
18399 len
= TREE_VEC_LENGTH (parm
);
18400 argslen
= TREE_VEC_LENGTH (arg
);
18402 /* Check for pack expansions in the parameters. */
18403 for (i
= 0; i
< len
; ++i
)
18405 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
18408 /* We can unify against something with a trailing
18410 parm_variadic_p
= 1;
18412 /* [temp.deduct.type]/9: If the template argument list of
18413 P contains a pack expansion that is not the last
18414 template argument, the entire template argument list
18415 is a non-deduced context. */
18416 return unify_success (explain_p
);
18420 /* If we don't have enough arguments to satisfy the parameters
18421 (not counting the pack expression at the end), or we have
18422 too many arguments for a parameter list that doesn't end in
18423 a pack expression, we can't unify. */
18424 if (parm_variadic_p
18425 ? argslen
< len
- parm_variadic_p
18427 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
18429 /* Unify all of the parameters that precede the (optional)
18430 pack expression. */
18431 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
18433 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
18434 TREE_VEC_ELT (parm
, i
),
18435 TREE_VEC_ELT (arg
, i
),
18436 UNIFY_ALLOW_NONE
, explain_p
);
18438 if (parm_variadic_p
)
18439 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
18441 /*subr=*/true, explain_p
);
18442 return unify_success (explain_p
);
18447 if (TREE_CODE (arg
) != TREE_CODE (parm
))
18448 return unify_type_mismatch (explain_p
, parm
, arg
);
18450 if (TYPE_PTRMEMFUNC_P (parm
))
18452 if (!TYPE_PTRMEMFUNC_P (arg
))
18453 return unify_type_mismatch (explain_p
, parm
, arg
);
18455 return unify (tparms
, targs
,
18456 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
18457 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
18458 strict
, explain_p
);
18460 else if (TYPE_PTRMEMFUNC_P (arg
))
18461 return unify_type_mismatch (explain_p
, parm
, arg
);
18463 if (CLASSTYPE_TEMPLATE_INFO (parm
))
18465 tree t
= NULL_TREE
;
18467 if (strict_in
& UNIFY_ALLOW_DERIVED
)
18469 /* First, we try to unify the PARM and ARG directly. */
18470 t
= try_class_unification (tparms
, targs
,
18471 parm
, arg
, explain_p
);
18475 /* Fallback to the special case allowed in
18476 [temp.deduct.call]:
18478 If P is a class, and P has the form
18479 template-id, then A can be a derived class of
18480 the deduced A. Likewise, if P is a pointer to
18481 a class of the form template-id, A can be a
18482 pointer to a derived class pointed to by the
18484 enum template_base_result r
;
18485 r
= get_template_base (tparms
, targs
, parm
, arg
,
18489 return unify_no_common_base (explain_p
, r
, parm
, arg
);
18492 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
18493 && (CLASSTYPE_TI_TEMPLATE (parm
)
18494 == CLASSTYPE_TI_TEMPLATE (arg
)))
18495 /* Perhaps PARM is something like S<U> and ARG is S<int>.
18496 Then, we should unify `int' and `U'. */
18499 /* There's no chance of unification succeeding. */
18500 return unify_type_mismatch (explain_p
, parm
, arg
);
18502 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
18503 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
18505 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
18506 return unify_type_mismatch (explain_p
, parm
, arg
);
18507 return unify_success (explain_p
);
18510 case FUNCTION_TYPE
:
18512 unsigned int nargs
;
18517 if (TREE_CODE (arg
) != TREE_CODE (parm
))
18518 return unify_type_mismatch (explain_p
, parm
, arg
);
18520 /* CV qualifications for methods can never be deduced, they must
18521 match exactly. We need to check them explicitly here,
18522 because type_unification_real treats them as any other
18523 cv-qualified parameter. */
18524 if (TREE_CODE (parm
) == METHOD_TYPE
18525 && (!check_cv_quals_for_unify
18527 class_of_this_parm (arg
),
18528 class_of_this_parm (parm
))))
18529 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
18531 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
18532 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
18534 nargs
= list_length (TYPE_ARG_TYPES (arg
));
18535 args
= XALLOCAVEC (tree
, nargs
);
18536 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
18537 a
!= NULL_TREE
&& a
!= void_list_node
;
18538 a
= TREE_CHAIN (a
), ++i
)
18539 args
[i
] = TREE_VALUE (a
);
18542 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
18543 args
, nargs
, 1, DEDUCE_EXACT
,
18544 LOOKUP_NORMAL
, NULL
, explain_p
);
18548 /* Unify a pointer to member with a pointer to member function, which
18549 deduces the type of the member as a function type. */
18550 if (TYPE_PTRMEMFUNC_P (arg
))
18552 /* Check top-level cv qualifiers */
18553 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
18554 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
18556 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
18557 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
18558 UNIFY_ALLOW_NONE
, explain_p
);
18560 /* Determine the type of the function we are unifying against. */
18561 tree fntype
= static_fn_type (arg
);
18563 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
18566 if (TREE_CODE (arg
) != OFFSET_TYPE
)
18567 return unify_type_mismatch (explain_p
, parm
, arg
);
18568 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
18569 TYPE_OFFSET_BASETYPE (arg
),
18570 UNIFY_ALLOW_NONE
, explain_p
);
18571 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
18572 strict
, explain_p
);
18575 if (DECL_TEMPLATE_PARM_P (parm
))
18576 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
18577 if (arg
!= scalar_constant_value (parm
))
18578 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
18579 return unify_success (explain_p
);
18582 case TEMPLATE_DECL
:
18583 /* Matched cases are handled by the ARG == PARM test above. */
18584 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
18587 /* A non-type template parameter that is a variable should be a
18588 an integral constant, in which case, it whould have been
18589 folded into its (constant) value. So we should not be getting
18590 a variable here. */
18591 gcc_unreachable ();
18593 case TYPE_ARGUMENT_PACK
:
18594 case NONTYPE_ARGUMENT_PACK
:
18595 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
18596 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
18599 case DECLTYPE_TYPE
:
18600 case UNDERLYING_TYPE
:
18601 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
18602 or UNDERLYING_TYPE nodes. */
18603 return unify_success (explain_p
);
18606 /* Unification fails if we hit an error node. */
18607 return unify_invalid (explain_p
);
18610 if (REFERENCE_REF_P (parm
))
18612 if (REFERENCE_REF_P (arg
))
18613 arg
= TREE_OPERAND (arg
, 0);
18614 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
18615 strict
, explain_p
);
18620 /* An unresolved overload is a nondeduced context. */
18621 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
18622 return unify_success (explain_p
);
18623 gcc_assert (EXPR_P (parm
));
18625 /* We must be looking at an expression. This can happen with
18629 void foo(S<I>, S<I + 2>);
18631 This is a "nondeduced context":
18635 The nondeduced contexts are:
18637 --A type that is a template-id in which one or more of
18638 the template-arguments is an expression that references
18639 a template-parameter.
18641 In these cases, we assume deduction succeeded, but don't
18642 actually infer any unifications. */
18644 if (!uses_template_parms (parm
)
18645 && !template_args_equal (parm
, arg
))
18646 return unify_expression_unequal (explain_p
, parm
, arg
);
18648 return unify_success (explain_p
);
18651 #undef RECUR_AND_CHECK_FAILURE
18653 /* Note that DECL can be defined in this translation unit, if
18657 mark_definable (tree decl
)
18660 DECL_NOT_REALLY_EXTERN (decl
) = 1;
18661 FOR_EACH_CLONE (clone
, decl
)
18662 DECL_NOT_REALLY_EXTERN (clone
) = 1;
18665 /* Called if RESULT is explicitly instantiated, or is a member of an
18666 explicitly instantiated class. */
18669 mark_decl_instantiated (tree result
, int extern_p
)
18671 SET_DECL_EXPLICIT_INSTANTIATION (result
);
18673 /* If this entity has already been written out, it's too late to
18674 make any modifications. */
18675 if (TREE_ASM_WRITTEN (result
))
18678 /* For anonymous namespace we don't need to do anything. */
18679 if (decl_anon_ns_mem_p (result
))
18681 gcc_assert (!TREE_PUBLIC (result
));
18685 if (TREE_CODE (result
) != FUNCTION_DECL
)
18686 /* The TREE_PUBLIC flag for function declarations will have been
18687 set correctly by tsubst. */
18688 TREE_PUBLIC (result
) = 1;
18690 /* This might have been set by an earlier implicit instantiation. */
18691 DECL_COMDAT (result
) = 0;
18694 DECL_NOT_REALLY_EXTERN (result
) = 0;
18697 mark_definable (result
);
18698 mark_needed (result
);
18699 /* Always make artificials weak. */
18700 if (DECL_ARTIFICIAL (result
) && flag_weak
)
18701 comdat_linkage (result
);
18702 /* For WIN32 we also want to put explicit instantiations in
18703 linkonce sections. */
18704 else if (TREE_PUBLIC (result
))
18705 maybe_make_one_only (result
);
18708 /* If EXTERN_P, then this function will not be emitted -- unless
18709 followed by an explicit instantiation, at which point its linkage
18710 will be adjusted. If !EXTERN_P, then this function will be
18711 emitted here. In neither circumstance do we want
18712 import_export_decl to adjust the linkage. */
18713 DECL_INTERFACE_KNOWN (result
) = 1;
18716 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
18717 important template arguments. If any are missing, we check whether
18718 they're important by using error_mark_node for substituting into any
18719 args that were used for partial ordering (the ones between ARGS and END)
18720 and seeing if it bubbles up. */
18723 check_undeduced_parms (tree targs
, tree args
, tree end
)
18725 bool found
= false;
18727 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
18728 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
18731 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
18735 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
18736 if (substed
== error_mark_node
)
18742 /* Given two function templates PAT1 and PAT2, return:
18744 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
18745 -1 if PAT2 is more specialized than PAT1.
18746 0 if neither is more specialized.
18748 LEN indicates the number of parameters we should consider
18749 (defaulted parameters should not be considered).
18751 The 1998 std underspecified function template partial ordering, and
18752 DR214 addresses the issue. We take pairs of arguments, one from
18753 each of the templates, and deduce them against each other. One of
18754 the templates will be more specialized if all the *other*
18755 template's arguments deduce against its arguments and at least one
18756 of its arguments *does* *not* deduce against the other template's
18757 corresponding argument. Deduction is done as for class templates.
18758 The arguments used in deduction have reference and top level cv
18759 qualifiers removed. Iff both arguments were originally reference
18760 types *and* deduction succeeds in both directions, an lvalue reference
18761 wins against an rvalue reference and otherwise the template
18762 with the more cv-qualified argument wins for that pairing (if
18763 neither is more cv-qualified, they both are equal). Unlike regular
18764 deduction, after all the arguments have been deduced in this way,
18765 we do *not* verify the deduced template argument values can be
18766 substituted into non-deduced contexts.
18768 The logic can be a bit confusing here, because we look at deduce1 and
18769 targs1 to see if pat2 is at least as specialized, and vice versa; if we
18770 can find template arguments for pat1 to make arg1 look like arg2, that
18771 means that arg2 is at least as specialized as arg1. */
18774 more_specialized_fn (tree pat1
, tree pat2
, int len
)
18776 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
18777 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
18778 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
18779 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
18780 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
18781 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
18782 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
18783 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
18784 tree origs1
, origs2
;
18785 bool lose1
= false;
18786 bool lose2
= false;
18788 /* Remove the this parameter from non-static member functions. If
18789 one is a non-static member function and the other is not a static
18790 member function, remove the first parameter from that function
18791 also. This situation occurs for operator functions where we
18792 locate both a member function (with this pointer) and non-member
18793 operator (with explicit first operand). */
18794 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
18796 len
--; /* LEN is the number of significant arguments for DECL1 */
18797 args1
= TREE_CHAIN (args1
);
18798 if (!DECL_STATIC_FUNCTION_P (decl2
))
18799 args2
= TREE_CHAIN (args2
);
18801 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
18803 args2
= TREE_CHAIN (args2
);
18804 if (!DECL_STATIC_FUNCTION_P (decl1
))
18807 args1
= TREE_CHAIN (args1
);
18811 /* If only one is a conversion operator, they are unordered. */
18812 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
18815 /* Consider the return type for a conversion function */
18816 if (DECL_CONV_FN_P (decl1
))
18818 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
18819 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
18823 processing_template_decl
++;
18829 /* Stop when an ellipsis is seen. */
18830 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
18832 tree arg1
= TREE_VALUE (args1
);
18833 tree arg2
= TREE_VALUE (args2
);
18834 int deduce1
, deduce2
;
18840 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
18841 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18843 /* When both arguments are pack expansions, we need only
18844 unify the patterns themselves. */
18845 arg1
= PACK_EXPANSION_PATTERN (arg1
);
18846 arg2
= PACK_EXPANSION_PATTERN (arg2
);
18848 /* This is the last comparison we need to do. */
18852 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
18854 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
18855 arg1
= TREE_TYPE (arg1
);
18856 quals1
= cp_type_quals (arg1
);
18859 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
18861 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
18862 arg2
= TREE_TYPE (arg2
);
18863 quals2
= cp_type_quals (arg2
);
18866 arg1
= TYPE_MAIN_VARIANT (arg1
);
18867 arg2
= TYPE_MAIN_VARIANT (arg2
);
18869 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
18871 int i
, len2
= list_length (args2
);
18872 tree parmvec
= make_tree_vec (1);
18873 tree argvec
= make_tree_vec (len2
);
18876 /* Setup the parameter vector, which contains only ARG1. */
18877 TREE_VEC_ELT (parmvec
, 0) = arg1
;
18879 /* Setup the argument vector, which contains the remaining
18881 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
18882 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
18884 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
18885 argvec
, DEDUCE_EXACT
,
18886 /*subr=*/true, /*explain_p=*/false)
18889 /* We cannot deduce in the other direction, because ARG1 is
18890 a pack expansion but ARG2 is not. */
18893 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18895 int i
, len1
= list_length (args1
);
18896 tree parmvec
= make_tree_vec (1);
18897 tree argvec
= make_tree_vec (len1
);
18900 /* Setup the parameter vector, which contains only ARG1. */
18901 TREE_VEC_ELT (parmvec
, 0) = arg2
;
18903 /* Setup the argument vector, which contains the remaining
18905 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
18906 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
18908 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
18909 argvec
, DEDUCE_EXACT
,
18910 /*subr=*/true, /*explain_p=*/false)
18913 /* We cannot deduce in the other direction, because ARG2 is
18914 a pack expansion but ARG1 is not.*/
18920 /* The normal case, where neither argument is a pack
18922 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
18923 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
18925 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
18926 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
18930 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18931 arg2, then arg2 is not as specialized as arg1. */
18937 /* "If, for a given type, deduction succeeds in both directions
18938 (i.e., the types are identical after the transformations above)
18939 and both P and A were reference types (before being replaced with
18940 the type referred to above):
18941 - if the type from the argument template was an lvalue reference and
18942 the type from the parameter template was not, the argument type is
18943 considered to be more specialized than the other; otherwise,
18944 - if the type from the argument template is more cv-qualified
18945 than the type from the parameter template (as described above),
18946 the argument type is considered to be more specialized than the other;
18948 - neither type is more specialized than the other." */
18950 if (deduce1
&& deduce2
)
18952 if (ref1
&& ref2
&& ref1
!= ref2
)
18959 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
18961 if ((quals1
& quals2
) == quals2
)
18963 if ((quals1
& quals2
) == quals1
)
18968 if (lose1
&& lose2
)
18969 /* We've failed to deduce something in either direction.
18970 These must be unordered. */
18973 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
18974 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18975 /* We have already processed all of the arguments in our
18976 handing of the pack expansion type. */
18979 args1
= TREE_CHAIN (args1
);
18980 args2
= TREE_CHAIN (args2
);
18983 /* "In most cases, all template parameters must have values in order for
18984 deduction to succeed, but for partial ordering purposes a template
18985 parameter may remain without a value provided it is not used in the
18986 types being used for partial ordering."
18988 Thus, if we are missing any of the targs1 we need to substitute into
18989 origs1, then pat2 is not as specialized as pat1. This can happen when
18990 there is a nondeduced context. */
18991 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
18993 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
18996 processing_template_decl
--;
18998 /* All things being equal, if the next argument is a pack expansion
18999 for one function but not for the other, prefer the
19000 non-variadic function. FIXME this is bogus; see c++/41958. */
19002 && args1
&& TREE_VALUE (args1
)
19003 && args2
&& TREE_VALUE (args2
))
19005 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
19006 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
19009 if (lose1
== lose2
)
19017 /* Determine which of two partial specializations of TMPL is more
19020 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
19021 to the first partial specialization. The TREE_PURPOSE is the
19022 innermost set of template parameters for the partial
19023 specialization. PAT2 is similar, but for the second template.
19025 Return 1 if the first partial specialization is more specialized;
19026 -1 if the second is more specialized; 0 if neither is more
19029 See [temp.class.order] for information about determining which of
19030 two templates is more specialized. */
19033 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
19037 bool any_deductions
= false;
19039 tree tmpl1
= TREE_VALUE (pat1
);
19040 tree tmpl2
= TREE_VALUE (pat2
);
19041 tree parms1
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl1
);
19042 tree parms2
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl2
);
19043 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
19044 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
19046 /* Just like what happens for functions, if we are ordering between
19047 different template specializations, we may encounter dependent
19048 types in the arguments, and we need our dependency check functions
19049 to behave correctly. */
19050 ++processing_template_decl
;
19051 targs
= get_partial_spec_bindings (tmpl
, parms1
, specargs1
, specargs2
);
19055 any_deductions
= true;
19058 targs
= get_partial_spec_bindings (tmpl
, parms2
, specargs2
, specargs1
);
19062 any_deductions
= true;
19064 --processing_template_decl
;
19066 /* In the case of a tie where at least one of the templates
19067 has a parameter pack at the end, the template with the most
19068 non-packed parameters wins. */
19071 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
19072 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
19074 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
19075 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
19076 int len1
= TREE_VEC_LENGTH (args1
);
19077 int len2
= TREE_VEC_LENGTH (args2
);
19079 /* We don't count the pack expansion at the end. */
19080 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
19082 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
19087 else if (len1
< len2
)
19094 /* Return the template arguments that will produce the function signature
19095 DECL from the function template FN, with the explicit template
19096 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
19097 also match. Return NULL_TREE if no satisfactory arguments could be
19101 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
19103 int ntparms
= DECL_NTPARMS (fn
);
19104 tree targs
= make_tree_vec (ntparms
);
19105 tree decl_type
= TREE_TYPE (decl
);
19106 tree decl_arg_types
;
19108 unsigned int nargs
, ix
;
19111 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
19113 /* Never do unification on the 'this' parameter. */
19114 decl_arg_types
= skip_artificial_parms_for (decl
,
19115 TYPE_ARG_TYPES (decl_type
));
19117 nargs
= list_length (decl_arg_types
);
19118 args
= XALLOCAVEC (tree
, nargs
);
19119 for (arg
= decl_arg_types
, ix
= 0;
19120 arg
!= NULL_TREE
&& arg
!= void_list_node
;
19121 arg
= TREE_CHAIN (arg
), ++ix
)
19122 args
[ix
] = TREE_VALUE (arg
);
19124 if (fn_type_unification (fn
, explicit_args
, targs
,
19126 (check_rettype
|| DECL_CONV_FN_P (fn
)
19127 ? TREE_TYPE (decl_type
) : NULL_TREE
),
19128 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
19130 == error_mark_node
)
19136 /* Return the innermost template arguments that, when applied to a partial
19137 specialization of TMPL whose innermost template parameters are
19138 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
19141 For example, suppose we have:
19143 template <class T, class U> struct S {};
19144 template <class T> struct S<T*, int> {};
19146 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
19147 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
19148 int}. The resulting vector will be {double}, indicating that `T'
19149 is bound to `double'. */
19152 get_partial_spec_bindings (tree tmpl
, tree tparms
, tree spec_args
, tree args
)
19154 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
19156 tree innermost_deduced_args
;
19158 innermost_deduced_args
= make_tree_vec (ntparms
);
19159 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
19161 deduced_args
= copy_node (args
);
19162 SET_TMPL_ARGS_LEVEL (deduced_args
,
19163 TMPL_ARGS_DEPTH (deduced_args
),
19164 innermost_deduced_args
);
19167 deduced_args
= innermost_deduced_args
;
19169 if (unify (tparms
, deduced_args
,
19170 INNERMOST_TEMPLATE_ARGS (spec_args
),
19171 INNERMOST_TEMPLATE_ARGS (args
),
19172 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
19175 for (i
= 0; i
< ntparms
; ++i
)
19176 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
19179 /* Verify that nondeduced template arguments agree with the type
19180 obtained from argument deduction.
19184 struct A { typedef int X; };
19185 template <class T, class U> struct C {};
19186 template <class T> struct C<T, typename T::X> {};
19188 Then with the instantiation `C<A, int>', we can deduce that
19189 `T' is `A' but unify () does not check whether `typename T::X'
19191 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
19192 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
19194 tf_none
, false, false);
19195 if (spec_args
== error_mark_node
19196 /* We only need to check the innermost arguments; the other
19197 arguments will always agree. */
19198 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
19199 INNERMOST_TEMPLATE_ARGS (args
)))
19202 /* Now that we have bindings for all of the template arguments,
19203 ensure that the arguments deduced for the template template
19204 parameters have compatible template parameter lists. See the use
19205 of template_template_parm_bindings_ok_p in fn_type_unification
19206 for more information. */
19207 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
19210 return deduced_args
;
19213 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
19214 Return the TREE_LIST node with the most specialized template, if
19215 any. If there is no most specialized template, the error_mark_node
19218 Note that this function does not look at, or modify, the
19219 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
19220 returned is one of the elements of INSTANTIATIONS, callers may
19221 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
19222 and retrieve it from the value returned. */
19225 most_specialized_instantiation (tree templates
)
19229 ++processing_template_decl
;
19232 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
19236 if (get_bindings (TREE_VALUE (champ
),
19237 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
19238 NULL_TREE
, /*check_ret=*/true))
19241 if (get_bindings (TREE_VALUE (fn
),
19242 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
19243 NULL_TREE
, /*check_ret=*/true))
19250 /* Equally specialized, move to next function. If there
19251 is no next function, nothing's most specialized. */
19252 fn
= TREE_CHAIN (fn
);
19260 /* Now verify that champ is better than everything earlier in the
19261 instantiation list. */
19262 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
19263 if (get_bindings (TREE_VALUE (champ
),
19264 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
19265 NULL_TREE
, /*check_ret=*/true)
19266 || !get_bindings (TREE_VALUE (fn
),
19267 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
19268 NULL_TREE
, /*check_ret=*/true))
19274 processing_template_decl
--;
19277 return error_mark_node
;
19282 /* If DECL is a specialization of some template, return the most
19283 general such template. Otherwise, returns NULL_TREE.
19285 For example, given:
19287 template <class T> struct S { template <class U> void f(U); };
19289 if TMPL is `template <class U> void S<int>::f(U)' this will return
19290 the full template. This function will not trace past partial
19291 specializations, however. For example, given in addition:
19293 template <class T> struct S<T*> { template <class U> void f(U); };
19295 if TMPL is `template <class U> void S<int*>::f(U)' this will return
19296 `template <class T> template <class U> S<T*>::f(U)'. */
19299 most_general_template (tree decl
)
19301 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
19303 if (tree tinfo
= get_template_info (decl
))
19304 decl
= TI_TEMPLATE (tinfo
);
19305 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
19306 template friend, or a FIELD_DECL for a capture pack. */
19307 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
19311 /* Look for more and more general templates. */
19312 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
19314 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
19315 (See cp-tree.h for details.) */
19316 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
19319 if (CLASS_TYPE_P (TREE_TYPE (decl
))
19320 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
19321 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
19324 /* Stop if we run into an explicitly specialized class template. */
19325 if (!DECL_NAMESPACE_SCOPE_P (decl
)
19326 && DECL_CONTEXT (decl
)
19327 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
19330 decl
= DECL_TI_TEMPLATE (decl
);
19336 /* Return the most specialized of the template partial specializations
19337 which can produce TARGET, a specialization of some class or variable
19338 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
19339 a TEMPLATE_DECL node corresponding to the partial specialization, while
19340 the TREE_PURPOSE is the set of template arguments that must be
19341 substituted into the template pattern in order to generate TARGET.
19343 If the choice of partial specialization is ambiguous, a diagnostic
19344 is issued, and the error_mark_node is returned. If there are no
19345 partial specializations matching TARGET, then NULL_TREE is
19346 returned, indicating that the primary template should be used. */
19349 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
19351 tree list
= NULL_TREE
;
19356 tree outer_args
= NULL_TREE
;
19359 if (TYPE_P (target
))
19361 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
19362 tmpl
= TI_TEMPLATE (tinfo
);
19363 args
= TI_ARGS (tinfo
);
19365 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
19367 tmpl
= TREE_OPERAND (target
, 0);
19368 args
= TREE_OPERAND (target
, 1);
19370 else if (VAR_P (target
))
19372 tree tinfo
= DECL_TEMPLATE_INFO (target
);
19373 tmpl
= TI_TEMPLATE (tinfo
);
19374 args
= TI_ARGS (tinfo
);
19377 gcc_unreachable ();
19379 tree main_tmpl
= most_general_template (tmpl
);
19381 /* For determining which partial specialization to use, only the
19382 innermost args are interesting. */
19383 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
19385 outer_args
= strip_innermost_template_args (args
, 1);
19386 args
= INNERMOST_TEMPLATE_ARGS (args
);
19389 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
19391 tree partial_spec_args
;
19393 tree spec_tmpl
= TREE_VALUE (t
);
19395 partial_spec_args
= TREE_PURPOSE (t
);
19397 ++processing_template_decl
;
19401 /* Discard the outer levels of args, and then substitute in the
19402 template args from the enclosing class. */
19403 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
19404 partial_spec_args
= tsubst_template_args
19405 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
19407 /* And the same for the partial specialization TEMPLATE_DECL. */
19408 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
19411 partial_spec_args
=
19412 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
19415 /*require_all_args=*/true,
19416 /*use_default_args=*/true);
19418 --processing_template_decl
;
19420 if (partial_spec_args
== error_mark_node
)
19421 return error_mark_node
;
19422 if (spec_tmpl
== error_mark_node
)
19423 return error_mark_node
;
19425 tree parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
19426 spec_args
= get_partial_spec_bindings (tmpl
, parms
,
19432 spec_args
= add_to_template_args (outer_args
, spec_args
);
19433 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
19434 TREE_TYPE (list
) = TREE_TYPE (t
);
19441 ambiguous_p
= false;
19444 t
= TREE_CHAIN (t
);
19445 for (; t
; t
= TREE_CHAIN (t
))
19447 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
19454 t
= TREE_CHAIN (t
);
19457 ambiguous_p
= true;
19466 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
19468 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
19471 ambiguous_p
= true;
19479 char *spaces
= NULL
;
19480 if (!(complain
& tf_error
))
19481 return error_mark_node
;
19482 if (TYPE_P (target
))
19483 error ("ambiguous template instantiation for %q#T", target
);
19485 error ("ambiguous template instantiation for %q#D", target
);
19486 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
19487 for (t
= list
; t
; t
= TREE_CHAIN (t
))
19489 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
19490 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
19491 "%s %#S", spaces
? spaces
: str
, subst
);
19492 spaces
= spaces
? spaces
: get_spaces (str
);
19495 return error_mark_node
;
19501 /* Explicitly instantiate DECL. */
19504 do_decl_instantiation (tree decl
, tree storage
)
19506 tree result
= NULL_TREE
;
19509 if (!decl
|| decl
== error_mark_node
)
19510 /* An error occurred, for which grokdeclarator has already issued
19511 an appropriate message. */
19513 else if (! DECL_LANG_SPECIFIC (decl
))
19515 error ("explicit instantiation of non-template %q#D", decl
);
19519 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
19520 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
19522 if (VAR_P (decl
) && !var_templ
)
19524 /* There is an asymmetry here in the way VAR_DECLs and
19525 FUNCTION_DECLs are handled by grokdeclarator. In the case of
19526 the latter, the DECL we get back will be marked as a
19527 template instantiation, and the appropriate
19528 DECL_TEMPLATE_INFO will be set up. This does not happen for
19529 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
19530 should handle VAR_DECLs as it currently handles
19532 if (!DECL_CLASS_SCOPE_P (decl
))
19534 error ("%qD is not a static data member of a class template", decl
);
19537 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
19538 if (!result
|| !VAR_P (result
))
19540 error ("no matching template for %qD found", decl
);
19543 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
19545 error ("type %qT for explicit instantiation %qD does not match "
19546 "declared type %qT", TREE_TYPE (result
), decl
,
19551 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
19553 error ("explicit instantiation of %q#D", decl
);
19559 /* Check for various error cases. Note that if the explicit
19560 instantiation is valid the RESULT will currently be marked as an
19561 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
19562 until we get here. */
19564 if (DECL_TEMPLATE_SPECIALIZATION (result
))
19566 /* DR 259 [temp.spec].
19568 Both an explicit instantiation and a declaration of an explicit
19569 specialization shall not appear in a program unless the explicit
19570 instantiation follows a declaration of the explicit specialization.
19572 For a given set of template parameters, if an explicit
19573 instantiation of a template appears after a declaration of an
19574 explicit specialization for that template, the explicit
19575 instantiation has no effect. */
19578 else if (DECL_EXPLICIT_INSTANTIATION (result
))
19582 No program shall explicitly instantiate any template more
19585 We check DECL_NOT_REALLY_EXTERN so as not to complain when
19586 the first instantiation was `extern' and the second is not,
19587 and EXTERN_P for the opposite case. */
19588 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
19589 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
19590 /* If an "extern" explicit instantiation follows an ordinary
19591 explicit instantiation, the template is instantiated. */
19595 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
19597 error ("no matching template for %qD found", result
);
19600 else if (!DECL_TEMPLATE_INFO (result
))
19602 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
19606 if (storage
== NULL_TREE
)
19608 else if (storage
== ridpointers
[(int) RID_EXTERN
])
19610 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
19611 pedwarn (input_location
, OPT_Wpedantic
,
19612 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
19617 error ("storage class %qD applied to template instantiation", storage
);
19619 check_explicit_instantiation_namespace (result
);
19620 mark_decl_instantiated (result
, extern_p
);
19622 instantiate_decl (result
, /*defer_ok=*/1,
19623 /*expl_inst_class_mem_p=*/false);
19627 mark_class_instantiated (tree t
, int extern_p
)
19629 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
19630 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
19631 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
19632 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
19635 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
19636 rest_of_type_compilation (t
, 1);
19640 /* Called from do_type_instantiation through binding_table_foreach to
19641 do recursive instantiation for the type bound in ENTRY. */
19643 bt_instantiate_type_proc (binding_entry entry
, void *data
)
19645 tree storage
= *(tree
*) data
;
19647 if (MAYBE_CLASS_TYPE_P (entry
->type
)
19648 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
19649 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
19652 /* Called from do_type_instantiation to instantiate a member
19653 (a member function or a static member variable) of an
19654 explicitly instantiated class template. */
19656 instantiate_class_member (tree decl
, int extern_p
)
19658 mark_decl_instantiated (decl
, extern_p
);
19660 instantiate_decl (decl
, /*defer_ok=*/1,
19661 /*expl_inst_class_mem_p=*/true);
19664 /* Perform an explicit instantiation of template class T. STORAGE, if
19665 non-null, is the RID for extern, inline or static. COMPLAIN is
19666 nonzero if this is called from the parser, zero if called recursively,
19667 since the standard is unclear (as detailed below). */
19670 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
19675 int previous_instantiation_extern_p
= 0;
19677 if (TREE_CODE (t
) == TYPE_DECL
)
19680 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
19683 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
19685 error ("explicit instantiation of non-class template %qD", tmpl
);
19687 error ("explicit instantiation of non-template type %qT", t
);
19693 if (!COMPLETE_TYPE_P (t
))
19695 if (complain
& tf_error
)
19696 error ("explicit instantiation of %q#T before definition of template",
19701 if (storage
!= NULL_TREE
)
19703 if (!in_system_header_at (input_location
))
19705 if (storage
== ridpointers
[(int) RID_EXTERN
])
19707 if (cxx_dialect
== cxx98
)
19708 pedwarn (input_location
, OPT_Wpedantic
,
19709 "ISO C++ 1998 forbids the use of %<extern%> on "
19710 "explicit instantiations");
19713 pedwarn (input_location
, OPT_Wpedantic
,
19714 "ISO C++ forbids the use of %qE"
19715 " on explicit instantiations", storage
);
19718 if (storage
== ridpointers
[(int) RID_INLINE
])
19720 else if (storage
== ridpointers
[(int) RID_EXTERN
])
19722 else if (storage
== ridpointers
[(int) RID_STATIC
])
19726 error ("storage class %qD applied to template instantiation",
19732 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
19734 /* DR 259 [temp.spec].
19736 Both an explicit instantiation and a declaration of an explicit
19737 specialization shall not appear in a program unless the explicit
19738 instantiation follows a declaration of the explicit specialization.
19740 For a given set of template parameters, if an explicit
19741 instantiation of a template appears after a declaration of an
19742 explicit specialization for that template, the explicit
19743 instantiation has no effect. */
19746 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
19750 No program shall explicitly instantiate any template more
19753 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
19754 instantiation was `extern'. If EXTERN_P then the second is.
19755 These cases are OK. */
19756 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
19758 if (!previous_instantiation_extern_p
&& !extern_p
19759 && (complain
& tf_error
))
19760 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
19762 /* If we've already instantiated the template, just return now. */
19763 if (!CLASSTYPE_INTERFACE_ONLY (t
))
19767 check_explicit_instantiation_namespace (TYPE_NAME (t
));
19768 mark_class_instantiated (t
, extern_p
);
19776 /* In contrast to implicit instantiation, where only the
19777 declarations, and not the definitions, of members are
19778 instantiated, we have here:
19782 The explicit instantiation of a class template specialization
19783 implies the instantiation of all of its members not
19784 previously explicitly specialized in the translation unit
19785 containing the explicit instantiation.
19787 Of course, we can't instantiate member template classes, since
19788 we don't have any arguments for them. Note that the standard
19789 is unclear on whether the instantiation of the members are
19790 *explicit* instantiations or not. However, the most natural
19791 interpretation is that it should be an explicit instantiation. */
19794 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
19795 if (TREE_CODE (tmp
) == FUNCTION_DECL
19796 && DECL_TEMPLATE_INSTANTIATION (tmp
))
19797 instantiate_class_member (tmp
, extern_p
);
19799 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
19800 if (VAR_P (tmp
) && DECL_TEMPLATE_INSTANTIATION (tmp
))
19801 instantiate_class_member (tmp
, extern_p
);
19803 if (CLASSTYPE_NESTED_UTDS (t
))
19804 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
19805 bt_instantiate_type_proc
, &storage
);
19809 /* Given a function DECL, which is a specialization of TMPL, modify
19810 DECL to be a re-instantiation of TMPL with the same template
19811 arguments. TMPL should be the template into which tsubst'ing
19812 should occur for DECL, not the most general template.
19814 One reason for doing this is a scenario like this:
19817 void f(const T&, int i);
19819 void g() { f(3, 7); }
19822 void f(const T& t, const int i) { }
19824 Note that when the template is first instantiated, with
19825 instantiate_template, the resulting DECL will have no name for the
19826 first parameter, and the wrong type for the second. So, when we go
19827 to instantiate the DECL, we regenerate it. */
19830 regenerate_decl_from_template (tree decl
, tree tmpl
)
19832 /* The arguments used to instantiate DECL, from the most general
19837 args
= DECL_TI_ARGS (decl
);
19838 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
19840 /* Make sure that we can see identifiers, and compute access
19842 push_access_scope (decl
);
19844 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19852 args_depth
= TMPL_ARGS_DEPTH (args
);
19853 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
19854 if (args_depth
> parms_depth
)
19855 args
= get_innermost_template_args (args
, parms_depth
);
19857 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
19858 args
, tf_error
, NULL_TREE
,
19859 /*defer_ok*/false);
19860 if (specs
&& specs
!= error_mark_node
)
19861 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
19864 /* Merge parameter declarations. */
19865 decl_parm
= skip_artificial_parms_for (decl
,
19866 DECL_ARGUMENTS (decl
));
19868 = skip_artificial_parms_for (code_pattern
,
19869 DECL_ARGUMENTS (code_pattern
));
19870 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
19875 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
19876 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
19877 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
19879 parm_type
= type_decays_to (parm_type
);
19880 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
19881 TREE_TYPE (decl_parm
) = parm_type
;
19882 attributes
= DECL_ATTRIBUTES (pattern_parm
);
19883 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
19885 DECL_ATTRIBUTES (decl_parm
) = attributes
;
19886 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
19888 decl_parm
= DECL_CHAIN (decl_parm
);
19889 pattern_parm
= DECL_CHAIN (pattern_parm
);
19891 /* Merge any parameters that match with the function parameter
19893 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
19896 tree expanded_types
;
19897 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19898 the parameters in this function parameter pack. */
19899 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
19900 args
, tf_error
, NULL_TREE
);
19901 len
= TREE_VEC_LENGTH (expanded_types
);
19902 for (i
= 0; i
< len
; i
++)
19907 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
19908 /* Rename the parameter to include the index. */
19909 DECL_NAME (decl_parm
) =
19910 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
19911 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
19912 parm_type
= type_decays_to (parm_type
);
19913 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
19914 TREE_TYPE (decl_parm
) = parm_type
;
19915 attributes
= DECL_ATTRIBUTES (pattern_parm
);
19916 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
19918 DECL_ATTRIBUTES (decl_parm
) = attributes
;
19919 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
19921 decl_parm
= DECL_CHAIN (decl_parm
);
19924 /* Merge additional specifiers from the CODE_PATTERN. */
19925 if (DECL_DECLARED_INLINE_P (code_pattern
)
19926 && !DECL_DECLARED_INLINE_P (decl
))
19927 DECL_DECLARED_INLINE_P (decl
) = 1;
19929 else if (VAR_P (decl
))
19931 DECL_INITIAL (decl
) =
19932 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
19933 tf_error
, DECL_TI_TEMPLATE (decl
),
19934 /*integral_constant_expression_p=*/false);
19935 if (VAR_HAD_UNKNOWN_BOUND (decl
))
19936 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
19937 tf_error
, DECL_TI_TEMPLATE (decl
));
19940 gcc_unreachable ();
19942 pop_access_scope (decl
);
19945 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19946 substituted to get DECL. */
19949 template_for_substitution (tree decl
)
19951 tree tmpl
= DECL_TI_TEMPLATE (decl
);
19953 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19954 for the instantiation. This is not always the most general
19955 template. Consider, for example:
19958 struct S { template <class U> void f();
19959 template <> void f<int>(); };
19961 and an instantiation of S<double>::f<int>. We want TD to be the
19962 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19963 while (/* An instantiation cannot have a definition, so we need a
19964 more general template. */
19965 DECL_TEMPLATE_INSTANTIATION (tmpl
)
19966 /* We must also deal with friend templates. Given:
19968 template <class T> struct S {
19969 template <class U> friend void f() {};
19972 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19973 so far as the language is concerned, but that's still
19974 where we get the pattern for the instantiation from. On
19975 other hand, if the definition comes outside the class, say:
19977 template <class T> struct S {
19978 template <class U> friend void f();
19980 template <class U> friend void f() {}
19982 we don't need to look any further. That's what the check for
19983 DECL_INITIAL is for. */
19984 || (TREE_CODE (decl
) == FUNCTION_DECL
19985 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
19986 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
19988 /* The present template, TD, should not be a definition. If it
19989 were a definition, we should be using it! Note that we
19990 cannot restructure the loop to just keep going until we find
19991 a template with a definition, since that might go too far if
19992 a specialization was declared, but not defined. */
19994 /* Fetch the more general template. */
19995 tmpl
= DECL_TI_TEMPLATE (tmpl
);
20001 /* Returns true if we need to instantiate this template instance even if we
20002 know we aren't going to emit it.. */
20005 always_instantiate_p (tree decl
)
20007 /* We always instantiate inline functions so that we can inline them. An
20008 explicit instantiation declaration prohibits implicit instantiation of
20009 non-inline functions. With high levels of optimization, we would
20010 normally inline non-inline functions -- but we're not allowed to do
20011 that for "extern template" functions. Therefore, we check
20012 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
20013 return ((TREE_CODE (decl
) == FUNCTION_DECL
20014 && (DECL_DECLARED_INLINE_P (decl
)
20015 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
20016 /* And we need to instantiate static data members so that
20017 their initializers are available in integral constant
20020 && decl_maybe_constant_var_p (decl
)));
20023 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
20024 instantiate it now, modifying TREE_TYPE (fn). */
20027 maybe_instantiate_noexcept (tree fn
)
20029 tree fntype
, spec
, noex
, clone
;
20031 /* Don't instantiate a noexcept-specification from template context. */
20032 if (processing_template_decl
)
20035 if (DECL_CLONED_FUNCTION_P (fn
))
20036 fn
= DECL_CLONED_FUNCTION (fn
);
20037 fntype
= TREE_TYPE (fn
);
20038 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
20040 if (!spec
|| !TREE_PURPOSE (spec
))
20043 noex
= TREE_PURPOSE (spec
);
20045 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
20047 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
20048 spec
= get_defaulted_eh_spec (fn
);
20049 else if (push_tinst_level (fn
))
20051 push_access_scope (fn
);
20052 push_deferring_access_checks (dk_no_deferred
);
20053 input_location
= DECL_SOURCE_LOCATION (fn
);
20054 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
20055 DEFERRED_NOEXCEPT_ARGS (noex
),
20056 tf_warning_or_error
, fn
,
20057 /*function_p=*/false,
20058 /*integral_constant_expression_p=*/true);
20059 pop_deferring_access_checks ();
20060 pop_access_scope (fn
);
20061 pop_tinst_level ();
20062 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
20063 if (spec
== error_mark_node
)
20064 spec
= noexcept_false_spec
;
20067 spec
= noexcept_false_spec
;
20069 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
20072 FOR_EACH_CLONE (clone
, fn
)
20074 if (TREE_TYPE (clone
) == fntype
)
20075 TREE_TYPE (clone
) = TREE_TYPE (fn
);
20077 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
20081 /* Produce the definition of D, a _DECL generated from a template. If
20082 DEFER_OK is nonzero, then we don't have to actually do the
20083 instantiation now; we just have to do it sometime. Normally it is
20084 an error if this is an explicit instantiation but D is undefined.
20085 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
20086 explicitly instantiated class template. */
20089 instantiate_decl (tree d
, int defer_ok
,
20090 bool expl_inst_class_mem_p
)
20092 tree tmpl
= DECL_TI_TEMPLATE (d
);
20099 bool pattern_defined
;
20100 location_t saved_loc
= input_location
;
20101 int saved_unevaluated_operand
= cp_unevaluated_operand
;
20102 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
20108 /* This function should only be used to instantiate templates for
20109 functions and static member variables. */
20110 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
20112 /* Variables are never deferred; if instantiation is required, they
20113 are instantiated right away. That allows for better code in the
20114 case that an expression refers to the value of the variable --
20115 if the variable has a constant value the referring expression can
20116 take advantage of that fact. */
20118 || DECL_DECLARED_CONSTEXPR_P (d
))
20121 /* Don't instantiate cloned functions. Instead, instantiate the
20122 functions they cloned. */
20123 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
20124 d
= DECL_CLONED_FUNCTION (d
);
20126 if (DECL_TEMPLATE_INSTANTIATED (d
)
20127 || (TREE_CODE (d
) == FUNCTION_DECL
20128 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
20129 || DECL_TEMPLATE_SPECIALIZATION (d
))
20130 /* D has already been instantiated or explicitly specialized, so
20131 there's nothing for us to do here.
20133 It might seem reasonable to check whether or not D is an explicit
20134 instantiation, and, if so, stop here. But when an explicit
20135 instantiation is deferred until the end of the compilation,
20136 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
20137 the instantiation. */
20140 /* Check to see whether we know that this template will be
20141 instantiated in some other file, as with "extern template"
20143 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
20145 /* In general, we do not instantiate such templates. */
20146 if (external_p
&& !always_instantiate_p (d
))
20149 gen_tmpl
= most_general_template (tmpl
);
20150 gen_args
= DECL_TI_ARGS (d
);
20152 if (tmpl
!= gen_tmpl
)
20153 /* We should already have the extra args. */
20154 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
20155 == TMPL_ARGS_DEPTH (gen_args
));
20156 /* And what's in the hash table should match D. */
20157 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
20158 || spec
== NULL_TREE
);
20160 /* This needs to happen before any tsubsting. */
20161 if (! push_tinst_level (d
))
20164 timevar_push (TV_TEMPLATE_INST
);
20166 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
20167 for the instantiation. */
20168 td
= template_for_substitution (d
);
20169 code_pattern
= DECL_TEMPLATE_RESULT (td
);
20171 /* We should never be trying to instantiate a member of a class
20172 template or partial specialization. */
20173 gcc_assert (d
!= code_pattern
);
20175 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
20176 || DECL_TEMPLATE_SPECIALIZATION (td
))
20177 /* In the case of a friend template whose definition is provided
20178 outside the class, we may have too many arguments. Drop the
20179 ones we don't need. The same is true for specializations. */
20180 args
= get_innermost_template_args
20181 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
20185 if (TREE_CODE (d
) == FUNCTION_DECL
)
20187 deleted_p
= DECL_DELETED_FN (code_pattern
);
20188 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
20189 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
)
20195 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
20198 /* We may be in the middle of deferred access check. Disable it now. */
20199 push_deferring_access_checks (dk_no_deferred
);
20201 /* Unless an explicit instantiation directive has already determined
20202 the linkage of D, remember that a definition is available for
20204 if (pattern_defined
20205 && !DECL_INTERFACE_KNOWN (d
)
20206 && !DECL_NOT_REALLY_EXTERN (d
))
20207 mark_definable (d
);
20209 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
20210 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
20211 input_location
= DECL_SOURCE_LOCATION (d
);
20213 /* If D is a member of an explicitly instantiated class template,
20214 and no definition is available, treat it like an implicit
20216 if (!pattern_defined
&& expl_inst_class_mem_p
20217 && DECL_EXPLICIT_INSTANTIATION (d
))
20219 /* Leave linkage flags alone on instantiations with anonymous
20221 if (TREE_PUBLIC (d
))
20223 DECL_NOT_REALLY_EXTERN (d
) = 0;
20224 DECL_INTERFACE_KNOWN (d
) = 0;
20226 SET_DECL_IMPLICIT_INSTANTIATION (d
);
20229 /* Defer all other templates, unless we have been explicitly
20230 forbidden from doing so. */
20231 if (/* If there is no definition, we cannot instantiate the
20234 /* If it's OK to postpone instantiation, do so. */
20236 /* If this is a static data member that will be defined
20237 elsewhere, we don't want to instantiate the entire data
20238 member, but we do want to instantiate the initializer so that
20239 we can substitute that elsewhere. */
20240 || (external_p
&& VAR_P (d
))
20241 /* Handle here a deleted function too, avoid generating
20242 its body (c++/61080). */
20245 /* The definition of the static data member is now required so
20246 we must substitute the initializer. */
20248 && !DECL_INITIAL (d
)
20249 && DECL_INITIAL (code_pattern
))
20253 bool const_init
= false;
20254 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
20256 ns
= decl_namespace_context (d
);
20257 push_nested_namespace (ns
);
20259 push_nested_class (DECL_CONTEXT (d
));
20260 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
20262 tf_warning_or_error
, NULL_TREE
,
20263 /*integral_constant_expression_p=*/false);
20264 /* If instantiating the initializer involved instantiating this
20265 again, don't call cp_finish_decl twice. */
20266 if (!DECL_INITIAL (d
))
20268 /* Make sure the initializer is still constant, in case of
20269 circular dependency (template/instantiate6.C). */
20271 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
20272 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
20273 /*asmspec_tree=*/NULL_TREE
,
20274 LOOKUP_ONLYCONVERTING
);
20277 pop_nested_class ();
20278 pop_nested_namespace (ns
);
20281 /* We restore the source position here because it's used by
20282 add_pending_template. */
20283 input_location
= saved_loc
;
20285 if (at_eof
&& !pattern_defined
20286 && DECL_EXPLICIT_INSTANTIATION (d
)
20287 && DECL_NOT_REALLY_EXTERN (d
))
20290 The definition of a non-exported function template, a
20291 non-exported member function template, or a non-exported
20292 member function or static data member of a class template
20293 shall be present in every translation unit in which it is
20294 explicitly instantiated. */
20295 permerror (input_location
, "explicit instantiation of %qD "
20296 "but no definition available", d
);
20298 /* If we're in unevaluated context, we just wanted to get the
20299 constant value; this isn't an odr use, so don't queue
20300 a full instantiation. */
20301 if (cp_unevaluated_operand
!= 0)
20303 /* ??? Historically, we have instantiated inline functions, even
20304 when marked as "extern template". */
20305 if (!(external_p
&& VAR_P (d
)))
20306 add_pending_template (d
);
20309 /* Tell the repository that D is available in this translation unit
20310 -- and see if it is supposed to be instantiated here. */
20311 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
20313 /* In a PCH file, despite the fact that the repository hasn't
20314 requested instantiation in the PCH it is still possible that
20315 an instantiation will be required in a file that includes the
20318 add_pending_template (d
);
20319 /* Instantiate inline functions so that the inliner can do its
20320 job, even though we'll not be emitting a copy of this
20322 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
20326 fn_context
= decl_function_context (d
);
20327 nested
= (current_function_decl
!= NULL_TREE
);
20329 push_to_top_level ();
20333 push_function_context ();
20334 cp_unevaluated_operand
= 0;
20335 c_inhibit_evaluation_warnings
= 0;
20338 /* Mark D as instantiated so that recursive calls to
20339 instantiate_decl do not try to instantiate it again. */
20340 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
20342 /* Regenerate the declaration in case the template has been modified
20343 by a subsequent redeclaration. */
20344 regenerate_decl_from_template (d
, td
);
20346 /* We already set the file and line above. Reset them now in case
20347 they changed as a result of calling regenerate_decl_from_template. */
20348 input_location
= DECL_SOURCE_LOCATION (d
);
20353 bool const_init
= false;
20355 /* Clear out DECL_RTL; whatever was there before may not be right
20356 since we've reset the type of the declaration. */
20357 SET_DECL_RTL (d
, NULL
);
20358 DECL_IN_AGGR_P (d
) = 0;
20360 /* The initializer is placed in DECL_INITIAL by
20361 regenerate_decl_from_template so we don't need to
20362 push/pop_access_scope again here. Pull it out so that
20363 cp_finish_decl can process it. */
20364 init
= DECL_INITIAL (d
);
20365 DECL_INITIAL (d
) = NULL_TREE
;
20366 DECL_INITIALIZED_P (d
) = 0;
20368 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
20369 initializer. That function will defer actual emission until
20370 we have a chance to determine linkage. */
20371 DECL_EXTERNAL (d
) = 0;
20373 /* Enter the scope of D so that access-checking works correctly. */
20374 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
20376 push_nested_class (DECL_CONTEXT (d
));
20378 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
20379 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
20382 pop_nested_class ();
20384 if (variable_template_p (td
))
20385 note_variable_template_instantiation (d
);
20387 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
20388 synthesize_method (d
);
20389 else if (TREE_CODE (d
) == FUNCTION_DECL
)
20391 hash_map
<tree
, tree
> *saved_local_specializations
;
20395 tree block
= NULL_TREE
;
20397 /* Save away the current list, in case we are instantiating one
20398 template from within the body of another. */
20399 saved_local_specializations
= local_specializations
;
20401 /* Set up the list of local specializations. */
20402 local_specializations
= new hash_map
<tree
, tree
>;
20404 /* Set up context. */
20405 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
20406 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
20407 block
= push_stmt_list ();
20409 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
20411 /* Some typedefs referenced from within the template code need to be
20412 access checked at template instantiation time, i.e now. These
20413 types were added to the template at parsing time. Let's get those
20414 and perform the access checks then. */
20415 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl
),
20418 /* Create substitution entries for the parameters. */
20419 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
20420 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
20421 spec_parm
= DECL_ARGUMENTS (d
);
20422 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
20424 register_local_specialization (spec_parm
, tmpl_parm
);
20425 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
20426 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
20428 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
20430 if (!DECL_PACK_P (tmpl_parm
))
20432 register_local_specialization (spec_parm
, tmpl_parm
);
20433 spec_parm
= DECL_CHAIN (spec_parm
);
20437 /* Register the (value) argument pack as a specialization of
20438 TMPL_PARM, then move on. */
20439 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
20440 register_local_specialization (argpack
, tmpl_parm
);
20443 gcc_assert (!spec_parm
);
20445 /* Substitute into the body of the function. */
20446 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
20447 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
20448 tf_warning_or_error
, tmpl
);
20451 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
20452 tf_warning_or_error
, tmpl
,
20453 /*integral_constant_expression_p=*/false);
20455 /* Set the current input_location to the end of the function
20456 so that finish_function knows where we are. */
20458 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
20460 /* Remember if we saw an infinite loop in the template. */
20461 current_function_infinite_loop
20462 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
20465 /* We don't need the local specializations any more. */
20466 delete local_specializations
;
20467 local_specializations
= saved_local_specializations
;
20469 /* Finish the function. */
20470 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
20471 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
20472 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
20475 d
= finish_function (0);
20476 expand_or_defer_fn (d
);
20479 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
20480 cp_check_omp_declare_reduction (d
);
20483 /* We're not deferring instantiation any more. */
20484 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
20487 pop_from_top_level ();
20489 pop_function_context ();
20492 input_location
= saved_loc
;
20493 cp_unevaluated_operand
= saved_unevaluated_operand
;
20494 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
20495 pop_deferring_access_checks ();
20496 pop_tinst_level ();
20498 timevar_pop (TV_TEMPLATE_INST
);
20503 /* Run through the list of templates that we wish we could
20504 instantiate, and instantiate any we can. RETRIES is the
20505 number of times we retry pending template instantiation. */
20508 instantiate_pending_templates (int retries
)
20511 location_t saved_loc
= input_location
;
20513 /* Instantiating templates may trigger vtable generation. This in turn
20514 may require further template instantiations. We place a limit here
20515 to avoid infinite loop. */
20516 if (pending_templates
&& retries
>= max_tinst_depth
)
20518 tree decl
= pending_templates
->tinst
->decl
;
20520 fatal_error (input_location
,
20521 "template instantiation depth exceeds maximum of %d"
20522 " instantiating %q+D, possibly from virtual table generation"
20523 " (use -ftemplate-depth= to increase the maximum)",
20524 max_tinst_depth
, decl
);
20525 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20526 /* Pretend that we defined it. */
20527 DECL_INITIAL (decl
) = error_mark_node
;
20533 struct pending_template
**t
= &pending_templates
;
20534 struct pending_template
*last
= NULL
;
20538 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
20539 bool complete
= false;
20541 if (TYPE_P (instantiation
))
20545 if (!COMPLETE_TYPE_P (instantiation
))
20547 instantiate_class_template (instantiation
);
20548 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
20549 for (fn
= TYPE_METHODS (instantiation
);
20551 fn
= TREE_CHAIN (fn
))
20552 if (! DECL_ARTIFICIAL (fn
))
20553 instantiate_decl (fn
,
20555 /*expl_inst_class_mem_p=*/false);
20556 if (COMPLETE_TYPE_P (instantiation
))
20560 complete
= COMPLETE_TYPE_P (instantiation
);
20564 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
20565 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
20568 = instantiate_decl (instantiation
,
20570 /*expl_inst_class_mem_p=*/false);
20571 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
20575 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
20576 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
20580 /* If INSTANTIATION has been instantiated, then we don't
20581 need to consider it again in the future. */
20589 current_tinst_level
= NULL
;
20591 last_pending_template
= last
;
20593 while (reconsider
);
20595 input_location
= saved_loc
;
20598 /* Substitute ARGVEC into T, which is a list of initializers for
20599 either base class or a non-static data member. The TREE_PURPOSEs
20600 are DECLs, and the TREE_VALUEs are the initializer values. Used by
20601 instantiate_decl. */
20604 tsubst_initializer_list (tree t
, tree argvec
)
20606 tree inits
= NULL_TREE
;
20608 for (; t
; t
= TREE_CHAIN (t
))
20612 tree expanded_bases
= NULL_TREE
;
20613 tree expanded_arguments
= NULL_TREE
;
20616 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
20621 /* Expand the base class expansion type into separate base
20623 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
20624 tf_warning_or_error
,
20626 if (expanded_bases
== error_mark_node
)
20629 /* We'll be building separate TREE_LISTs of arguments for
20631 len
= TREE_VEC_LENGTH (expanded_bases
);
20632 expanded_arguments
= make_tree_vec (len
);
20633 for (i
= 0; i
< len
; i
++)
20634 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
20636 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
20637 expand each argument in the TREE_VALUE of t. */
20638 expr
= make_node (EXPR_PACK_EXPANSION
);
20639 PACK_EXPANSION_LOCAL_P (expr
) = true;
20640 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
20641 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
20643 if (TREE_VALUE (t
) == void_type_node
)
20644 /* VOID_TYPE_NODE is used to indicate
20645 value-initialization. */
20647 for (i
= 0; i
< len
; i
++)
20648 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
20652 /* Substitute parameter packs into each argument in the
20654 in_base_initializer
= 1;
20655 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
20657 tree expanded_exprs
;
20659 /* Expand the argument. */
20660 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
20662 = tsubst_pack_expansion (expr
, argvec
,
20663 tf_warning_or_error
,
20665 if (expanded_exprs
== error_mark_node
)
20668 /* Prepend each of the expanded expressions to the
20669 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
20670 for (i
= 0; i
< len
; i
++)
20672 TREE_VEC_ELT (expanded_arguments
, i
) =
20673 tree_cons (NULL_TREE
,
20674 TREE_VEC_ELT (expanded_exprs
, i
),
20675 TREE_VEC_ELT (expanded_arguments
, i
));
20678 in_base_initializer
= 0;
20680 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
20681 since we built them backwards. */
20682 for (i
= 0; i
< len
; i
++)
20684 TREE_VEC_ELT (expanded_arguments
, i
) =
20685 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
20690 for (i
= 0; i
< len
; ++i
)
20692 if (expanded_bases
)
20694 decl
= TREE_VEC_ELT (expanded_bases
, i
);
20695 decl
= expand_member_init (decl
);
20696 init
= TREE_VEC_ELT (expanded_arguments
, i
);
20701 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
20702 tf_warning_or_error
, NULL_TREE
);
20704 decl
= expand_member_init (decl
);
20705 if (decl
&& !DECL_P (decl
))
20706 in_base_initializer
= 1;
20708 init
= TREE_VALUE (t
);
20710 if (init
!= void_type_node
)
20711 init
= tsubst_expr (init
, argvec
,
20712 tf_warning_or_error
, NULL_TREE
,
20713 /*integral_constant_expression_p=*/false);
20714 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
20715 /* If we had an initializer but it instantiated to nothing,
20716 value-initialize the object. This will only occur when
20717 the initializer was a pack expansion where the parameter
20718 packs used in that expansion were of length zero. */
20719 init
= void_type_node
;
20720 in_base_initializer
= 0;
20725 init
= build_tree_list (decl
, init
);
20726 TREE_CHAIN (init
) = inits
;
20734 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
20737 set_current_access_from_decl (tree decl
)
20739 if (TREE_PRIVATE (decl
))
20740 current_access_specifier
= access_private_node
;
20741 else if (TREE_PROTECTED (decl
))
20742 current_access_specifier
= access_protected_node
;
20744 current_access_specifier
= access_public_node
;
20747 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
20748 is the instantiation (which should have been created with
20749 start_enum) and ARGS are the template arguments to use. */
20752 tsubst_enum (tree tag
, tree newtag
, tree args
)
20756 if (SCOPED_ENUM_P (newtag
))
20757 begin_scope (sk_scoped_enum
, newtag
);
20759 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
20764 decl
= TREE_VALUE (e
);
20765 /* Note that in a template enum, the TREE_VALUE is the
20766 CONST_DECL, not the corresponding INTEGER_CST. */
20767 value
= tsubst_expr (DECL_INITIAL (decl
),
20768 args
, tf_warning_or_error
, NULL_TREE
,
20769 /*integral_constant_expression_p=*/true);
20771 /* Give this enumeration constant the correct access. */
20772 set_current_access_from_decl (decl
);
20774 /* Actually build the enumerator itself. */
20776 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
20779 if (SCOPED_ENUM_P (newtag
))
20782 finish_enum_value_list (newtag
);
20783 finish_enum (newtag
);
20785 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
20786 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
20789 /* DECL is a FUNCTION_DECL that is a template specialization. Return
20790 its type -- but without substituting the innermost set of template
20791 arguments. So, innermost set of template parameters will appear in
20795 get_mostly_instantiated_function_type (tree decl
)
20797 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
20798 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
20801 /* Return truthvalue if we're processing a template different from
20802 the last one involved in diagnostics. */
20804 problematic_instantiation_changed (void)
20806 return current_tinst_level
!= last_error_tinst_level
;
20809 /* Remember current template involved in diagnostics. */
20811 record_last_problematic_instantiation (void)
20813 last_error_tinst_level
= current_tinst_level
;
20816 struct tinst_level
*
20817 current_instantiation (void)
20819 return current_tinst_level
;
20822 /* Return TRUE if current_function_decl is being instantiated, false
20826 instantiating_current_function_p (void)
20828 return (current_instantiation ()
20829 && current_instantiation ()->decl
== current_function_decl
);
20832 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20833 type. Return zero for ok, nonzero for disallowed. Issue error and
20834 warning messages under control of COMPLAIN. */
20837 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
20839 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
20841 else if (POINTER_TYPE_P (type
))
20843 else if (TYPE_PTRMEM_P (type
))
20845 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
20847 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
20849 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
20851 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
20854 if (complain
& tf_error
)
20856 if (type
== error_mark_node
)
20857 inform (input_location
, "invalid template non-type parameter");
20859 error ("%q#T is not a valid type for a template non-type parameter",
20865 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20866 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20869 dependent_type_p_r (tree type
)
20875 A type is dependent if it is:
20877 -- a template parameter. Template template parameters are types
20878 for us (since TYPE_P holds true for them) so we handle
20880 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20881 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
20883 /* -- a qualified-id with a nested-name-specifier which contains a
20884 class-name that names a dependent type or whose unqualified-id
20885 names a dependent type. */
20886 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20888 /* -- a cv-qualified type where the cv-unqualified type is
20890 No code is necessary for this bullet; the code below handles
20891 cv-qualified types, and we don't want to strip aliases with
20892 TYPE_MAIN_VARIANT because of DR 1558. */
20893 /* -- a compound type constructed from any dependent type. */
20894 if (TYPE_PTRMEM_P (type
))
20895 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
20896 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20898 else if (TYPE_PTR_P (type
)
20899 || TREE_CODE (type
) == REFERENCE_TYPE
)
20900 return dependent_type_p (TREE_TYPE (type
));
20901 else if (TREE_CODE (type
) == FUNCTION_TYPE
20902 || TREE_CODE (type
) == METHOD_TYPE
)
20906 if (dependent_type_p (TREE_TYPE (type
)))
20908 for (arg_type
= TYPE_ARG_TYPES (type
);
20910 arg_type
= TREE_CHAIN (arg_type
))
20911 if (dependent_type_p (TREE_VALUE (arg_type
)))
20915 /* -- an array type constructed from any dependent type or whose
20916 size is specified by a constant expression that is
20919 We checked for type- and value-dependence of the bounds in
20920 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20921 if (TREE_CODE (type
) == ARRAY_TYPE
)
20923 if (TYPE_DOMAIN (type
)
20924 && dependent_type_p (TYPE_DOMAIN (type
)))
20926 return dependent_type_p (TREE_TYPE (type
));
20929 /* -- a template-id in which either the template name is a template
20931 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20933 /* ... or any of the template arguments is a dependent type or
20934 an expression that is type-dependent or value-dependent. */
20935 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
20936 && (any_dependent_template_arguments_p
20937 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
20939 /* For an alias template specialization, check the arguments both to the
20940 class template and the alias template. */
20941 else if (alias_template_specialization_p (type
)
20942 && (any_dependent_template_arguments_p
20943 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (type
)))))
20946 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20947 dependent; if the argument of the `typeof' expression is not
20948 type-dependent, then it should already been have resolved. */
20949 if (TREE_CODE (type
) == TYPEOF_TYPE
20950 || TREE_CODE (type
) == DECLTYPE_TYPE
20951 || TREE_CODE (type
) == UNDERLYING_TYPE
)
20954 /* A template argument pack is dependent if any of its packed
20956 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
20958 tree args
= ARGUMENT_PACK_ARGS (type
);
20959 int i
, len
= TREE_VEC_LENGTH (args
);
20960 for (i
= 0; i
< len
; ++i
)
20961 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
20965 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20966 be template parameters. */
20967 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
20970 /* The standard does not specifically mention types that are local
20971 to template functions or local classes, but they should be
20972 considered dependent too. For example:
20974 template <int I> void f() {
20979 The size of `E' cannot be known until the value of `I' has been
20980 determined. Therefore, `E' must be considered dependent. */
20981 scope
= TYPE_CONTEXT (type
);
20982 if (scope
&& TYPE_P (scope
))
20983 return dependent_type_p (scope
);
20984 /* Don't use type_dependent_expression_p here, as it can lead
20985 to infinite recursion trying to determine whether a lambda
20986 nested in a lambda is dependent (c++/47687). */
20987 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
20988 && DECL_LANG_SPECIFIC (scope
)
20989 && DECL_TEMPLATE_INFO (scope
)
20990 && (any_dependent_template_arguments_p
20991 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
20994 /* Other types are non-dependent. */
20998 /* Returns TRUE if TYPE is dependent, in the sense of
20999 [temp.dep.type]. Note that a NULL type is considered dependent. */
21002 dependent_type_p (tree type
)
21004 /* If there are no template parameters in scope, then there can't be
21005 any dependent types. */
21006 if (!processing_template_decl
)
21008 /* If we are not processing a template, then nobody should be
21009 providing us with a dependent type. */
21011 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
21015 /* If the type is NULL, we have not computed a type for the entity
21016 in question; in that case, the type is dependent. */
21020 /* Erroneous types can be considered non-dependent. */
21021 if (type
== error_mark_node
)
21024 /* If we have not already computed the appropriate value for TYPE,
21026 if (!TYPE_DEPENDENT_P_VALID (type
))
21028 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
21029 TYPE_DEPENDENT_P_VALID (type
) = 1;
21032 return TYPE_DEPENDENT_P (type
);
21035 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
21036 lookup. In other words, a dependent type that is not the current
21040 dependent_scope_p (tree scope
)
21042 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
21043 && !currently_open_class (scope
));
21046 /* T is a SCOPE_REF; return whether we need to consider it
21047 instantiation-dependent so that we can check access at instantiation
21048 time even though we know which member it resolves to. */
21051 instantiation_dependent_scope_ref_p (tree t
)
21053 if (DECL_P (TREE_OPERAND (t
, 1))
21054 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
21055 && accessible_in_template_p (TREE_OPERAND (t
, 0),
21056 TREE_OPERAND (t
, 1)))
21062 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
21063 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
21066 /* Note that this predicate is not appropriate for general expressions;
21067 only constant expressions (that satisfy potential_constant_expression)
21068 can be tested for value dependence. */
21071 value_dependent_expression_p (tree expression
)
21073 if (!processing_template_decl
)
21076 /* A name declared with a dependent type. */
21077 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
21080 switch (TREE_CODE (expression
))
21082 case IDENTIFIER_NODE
:
21083 /* A name that has not been looked up -- must be dependent. */
21086 case TEMPLATE_PARM_INDEX
:
21087 /* A non-type template parm. */
21091 /* A non-type template parm. */
21092 if (DECL_TEMPLATE_PARM_P (expression
))
21094 return value_dependent_expression_p (DECL_INITIAL (expression
));
21097 /* A constant with literal type and is initialized
21098 with an expression that is value-dependent.
21100 Note that a non-dependent parenthesized initializer will have
21101 already been replaced with its constant value, so if we see
21102 a TREE_LIST it must be dependent. */
21103 if (DECL_INITIAL (expression
)
21104 && decl_constant_var_p (expression
)
21105 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
21106 /* cp_finish_decl doesn't fold reference initializers. */
21107 || TREE_CODE (TREE_TYPE (expression
)) == REFERENCE_TYPE
21108 || value_dependent_expression_p (DECL_INITIAL (expression
))))
21112 case DYNAMIC_CAST_EXPR
:
21113 case STATIC_CAST_EXPR
:
21114 case CONST_CAST_EXPR
:
21115 case REINTERPRET_CAST_EXPR
:
21117 /* These expressions are value-dependent if the type to which
21118 the cast occurs is dependent or the expression being casted
21119 is value-dependent. */
21121 tree type
= TREE_TYPE (expression
);
21123 if (dependent_type_p (type
))
21126 /* A functional cast has a list of operands. */
21127 expression
= TREE_OPERAND (expression
, 0);
21130 /* If there are no operands, it must be an expression such
21131 as "int()". This should not happen for aggregate types
21132 because it would form non-constant expressions. */
21133 gcc_assert (cxx_dialect
>= cxx11
21134 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
21139 if (TREE_CODE (expression
) == TREE_LIST
)
21140 return any_value_dependent_elements_p (expression
);
21142 return value_dependent_expression_p (expression
);
21146 if (SIZEOF_EXPR_TYPE_P (expression
))
21147 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
21151 /* A `sizeof' expression is value-dependent if the operand is
21152 type-dependent or is a pack expansion. */
21153 expression
= TREE_OPERAND (expression
, 0);
21154 if (PACK_EXPANSION_P (expression
))
21156 else if (TYPE_P (expression
))
21157 return dependent_type_p (expression
);
21158 return instantiation_dependent_expression_p (expression
);
21160 case AT_ENCODE_EXPR
:
21161 /* An 'encode' expression is value-dependent if the operand is
21163 expression
= TREE_OPERAND (expression
, 0);
21164 return dependent_type_p (expression
);
21166 case NOEXCEPT_EXPR
:
21167 expression
= TREE_OPERAND (expression
, 0);
21168 return instantiation_dependent_expression_p (expression
);
21171 /* All instantiation-dependent expressions should also be considered
21172 value-dependent. */
21173 return instantiation_dependent_scope_ref_p (expression
);
21175 case COMPONENT_REF
:
21176 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
21177 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
21179 case NONTYPE_ARGUMENT_PACK
:
21180 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
21181 is value-dependent. */
21183 tree values
= ARGUMENT_PACK_ARGS (expression
);
21184 int i
, len
= TREE_VEC_LENGTH (values
);
21186 for (i
= 0; i
< len
; ++i
)
21187 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
21195 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
21196 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
21197 || (type2
? dependent_type_p (type2
) : false));
21201 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
21202 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
21205 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
21206 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
21210 tree op
= TREE_OPERAND (expression
, 0);
21211 return (value_dependent_expression_p (op
)
21212 || has_value_dependent_address (op
));
21217 tree fn
= get_callee_fndecl (expression
);
21219 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
21221 nargs
= call_expr_nargs (expression
);
21222 for (i
= 0; i
< nargs
; ++i
)
21224 tree op
= CALL_EXPR_ARG (expression
, i
);
21225 /* In a call to a constexpr member function, look through the
21226 implicit ADDR_EXPR on the object argument so that it doesn't
21227 cause the call to be considered value-dependent. We also
21228 look through it in potential_constant_expression. */
21229 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
21230 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
21231 && TREE_CODE (op
) == ADDR_EXPR
)
21232 op
= TREE_OPERAND (op
, 0);
21233 if (value_dependent_expression_p (op
))
21239 case TEMPLATE_ID_EXPR
:
21240 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
21242 return type_dependent_expression_p (expression
);
21248 if (dependent_type_p (TREE_TYPE (expression
)))
21250 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
21251 if (value_dependent_expression_p (val
))
21257 /* Treat a GNU statement expression as dependent to avoid crashing
21258 under instantiate_non_dependent_expr; it can't be constant. */
21262 /* A constant expression is value-dependent if any subexpression is
21263 value-dependent. */
21264 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
21266 case tcc_reference
:
21268 case tcc_comparison
:
21270 case tcc_expression
:
21273 int i
, len
= cp_tree_operand_length (expression
);
21275 for (i
= 0; i
< len
; i
++)
21277 tree t
= TREE_OPERAND (expression
, i
);
21279 /* In some cases, some of the operands may be missing.l
21280 (For example, in the case of PREDECREMENT_EXPR, the
21281 amount to increment by may be missing.) That doesn't
21282 make the expression dependent. */
21283 if (t
&& value_dependent_expression_p (t
))
21294 /* The expression is not value-dependent. */
21298 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
21299 [temp.dep.expr]. Note that an expression with no type is
21300 considered dependent. Other parts of the compiler arrange for an
21301 expression with type-dependent subexpressions to have no type, so
21302 this function doesn't have to be fully recursive. */
21305 type_dependent_expression_p (tree expression
)
21307 if (!processing_template_decl
)
21310 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
21313 /* An unresolved name is always dependent. */
21314 if (identifier_p (expression
) || TREE_CODE (expression
) == USING_DECL
)
21317 /* Some expression forms are never type-dependent. */
21318 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
21319 || TREE_CODE (expression
) == SIZEOF_EXPR
21320 || TREE_CODE (expression
) == ALIGNOF_EXPR
21321 || TREE_CODE (expression
) == AT_ENCODE_EXPR
21322 || TREE_CODE (expression
) == NOEXCEPT_EXPR
21323 || TREE_CODE (expression
) == TRAIT_EXPR
21324 || TREE_CODE (expression
) == TYPEID_EXPR
21325 || TREE_CODE (expression
) == DELETE_EXPR
21326 || TREE_CODE (expression
) == VEC_DELETE_EXPR
21327 || TREE_CODE (expression
) == THROW_EXPR
)
21330 /* The types of these expressions depends only on the type to which
21331 the cast occurs. */
21332 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
21333 || TREE_CODE (expression
) == STATIC_CAST_EXPR
21334 || TREE_CODE (expression
) == CONST_CAST_EXPR
21335 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
21336 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
21337 || TREE_CODE (expression
) == CAST_EXPR
)
21338 return dependent_type_p (TREE_TYPE (expression
));
21340 /* The types of these expressions depends only on the type created
21341 by the expression. */
21342 if (TREE_CODE (expression
) == NEW_EXPR
21343 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
21345 /* For NEW_EXPR tree nodes created inside a template, either
21346 the object type itself or a TREE_LIST may appear as the
21348 tree type
= TREE_OPERAND (expression
, 1);
21349 if (TREE_CODE (type
) == TREE_LIST
)
21350 /* This is an array type. We need to check array dimensions
21352 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
21353 || value_dependent_expression_p
21354 (TREE_OPERAND (TREE_VALUE (type
), 1));
21356 return dependent_type_p (type
);
21359 if (TREE_CODE (expression
) == SCOPE_REF
)
21361 tree scope
= TREE_OPERAND (expression
, 0);
21362 tree name
= TREE_OPERAND (expression
, 1);
21364 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
21365 contains an identifier associated by name lookup with one or more
21366 declarations declared with a dependent type, or...a
21367 nested-name-specifier or qualified-id that names a member of an
21368 unknown specialization. */
21369 return (type_dependent_expression_p (name
)
21370 || dependent_scope_p (scope
));
21373 if (TREE_CODE (expression
) == FUNCTION_DECL
21374 && DECL_LANG_SPECIFIC (expression
)
21375 && DECL_TEMPLATE_INFO (expression
)
21376 && (any_dependent_template_arguments_p
21377 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
21380 if (TREE_CODE (expression
) == TEMPLATE_DECL
21381 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
21384 if (TREE_CODE (expression
) == STMT_EXPR
)
21385 expression
= stmt_expr_value_expr (expression
);
21387 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
21392 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
21394 if (type_dependent_expression_p (elt
))
21400 /* A static data member of the current instantiation with incomplete
21401 array type is type-dependent, as the definition and specializations
21402 can have different bounds. */
21403 if (VAR_P (expression
)
21404 && DECL_CLASS_SCOPE_P (expression
)
21405 && dependent_type_p (DECL_CONTEXT (expression
))
21406 && VAR_HAD_UNKNOWN_BOUND (expression
))
21409 /* An array of unknown bound depending on a variadic parameter, eg:
21411 template<typename... Args>
21412 void foo (Args... args)
21414 int arr[] = { args... };
21417 template<int... vals>
21420 int arr[] = { vals... };
21423 If the array has no length and has an initializer, it must be that
21424 we couldn't determine its length in cp_complete_array_type because
21425 it is dependent. */
21426 if (VAR_P (expression
)
21427 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
21428 && !TYPE_DOMAIN (TREE_TYPE (expression
))
21429 && DECL_INITIAL (expression
))
21432 /* A variable template specialization is type-dependent if it has any
21433 dependent template arguments. */
21434 if (VAR_P (expression
)
21435 && DECL_LANG_SPECIFIC (expression
)
21436 && DECL_TEMPLATE_INFO (expression
)
21437 && variable_template_p (DECL_TI_TEMPLATE (expression
)))
21438 return any_dependent_template_arguments_p (DECL_TI_ARGS (expression
));
21440 if (TREE_TYPE (expression
) == unknown_type_node
)
21442 if (TREE_CODE (expression
) == ADDR_EXPR
)
21443 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
21444 if (TREE_CODE (expression
) == COMPONENT_REF
21445 || TREE_CODE (expression
) == OFFSET_REF
)
21447 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
21449 expression
= TREE_OPERAND (expression
, 1);
21450 if (identifier_p (expression
))
21453 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
21454 if (TREE_CODE (expression
) == SCOPE_REF
)
21457 /* Always dependent, on the number of arguments if nothing else. */
21458 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
21461 if (BASELINK_P (expression
))
21463 if (BASELINK_OPTYPE (expression
)
21464 && dependent_type_p (BASELINK_OPTYPE (expression
)))
21466 expression
= BASELINK_FUNCTIONS (expression
);
21469 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
21471 if (any_dependent_template_arguments_p
21472 (TREE_OPERAND (expression
, 1)))
21474 expression
= TREE_OPERAND (expression
, 0);
21476 gcc_assert (TREE_CODE (expression
) == OVERLOAD
21477 || TREE_CODE (expression
) == FUNCTION_DECL
);
21481 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
21483 expression
= OVL_NEXT (expression
);
21488 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
21490 return (dependent_type_p (TREE_TYPE (expression
)));
21493 /* walk_tree callback function for instantiation_dependent_expression_p,
21494 below. Returns non-zero if a dependent subexpression is found. */
21497 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
21502 /* We don't have to worry about decltype currently because decltype
21503 of an instantiation-dependent expr is a dependent type. This
21504 might change depending on the resolution of DR 1172. */
21505 *walk_subtrees
= false;
21508 enum tree_code code
= TREE_CODE (*tp
);
21511 /* Don't treat an argument list as dependent just because it has no
21519 /* A constant with a dependent initializer is dependent. */
21520 if (value_dependent_expression_p (*tp
))
21524 case TEMPLATE_PARM_INDEX
:
21527 /* Handle expressions with type operands. */
21531 case AT_ENCODE_EXPR
:
21533 tree op
= TREE_OPERAND (*tp
, 0);
21534 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
21535 op
= TREE_TYPE (op
);
21538 if (dependent_type_p (op
))
21542 *walk_subtrees
= false;
21550 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp
))
21551 || (TRAIT_EXPR_TYPE2 (*tp
)
21552 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp
))))
21554 *walk_subtrees
= false;
21557 case COMPONENT_REF
:
21558 if (identifier_p (TREE_OPERAND (*tp
, 1)))
21559 /* In a template, finish_class_member_access_expr creates a
21560 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
21561 type-dependent, so that we can check access control at
21562 instantiation time (PR 42277). See also Core issue 1273. */
21567 if (instantiation_dependent_scope_ref_p (*tp
))
21572 /* Treat statement-expressions as dependent. */
21580 if (type_dependent_expression_p (*tp
))
21586 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
21587 sense defined by the ABI:
21589 "An expression is instantiation-dependent if it is type-dependent
21590 or value-dependent, or it has a subexpression that is type-dependent
21591 or value-dependent." */
21594 instantiation_dependent_expression_p (tree expression
)
21598 if (!processing_template_decl
)
21601 if (expression
== error_mark_node
)
21604 result
= cp_walk_tree_without_duplicates (&expression
,
21605 instantiation_dependent_r
, NULL
);
21606 return result
!= NULL_TREE
;
21609 /* Like type_dependent_expression_p, but it also works while not processing
21610 a template definition, i.e. during substitution or mangling. */
21613 type_dependent_expression_p_push (tree expr
)
21616 ++processing_template_decl
;
21617 b
= type_dependent_expression_p (expr
);
21618 --processing_template_decl
;
21622 /* Returns TRUE if ARGS contains a type-dependent expression. */
21625 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
21630 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
21632 if (type_dependent_expression_p (arg
))
21638 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21639 expressions) contains any type-dependent expressions. */
21642 any_type_dependent_elements_p (const_tree list
)
21644 for (; list
; list
= TREE_CHAIN (list
))
21645 if (type_dependent_expression_p (TREE_VALUE (list
)))
21651 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21652 expressions) contains any value-dependent expressions. */
21655 any_value_dependent_elements_p (const_tree list
)
21657 for (; list
; list
= TREE_CHAIN (list
))
21658 if (value_dependent_expression_p (TREE_VALUE (list
)))
21664 /* Returns TRUE if the ARG (a template argument) is dependent. */
21667 dependent_template_arg_p (tree arg
)
21669 if (!processing_template_decl
)
21672 /* Assume a template argument that was wrongly written by the user
21673 is dependent. This is consistent with what
21674 any_dependent_template_arguments_p [that calls this function]
21676 if (!arg
|| arg
== error_mark_node
)
21679 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
21680 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
21682 if (TREE_CODE (arg
) == TEMPLATE_DECL
21683 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
21684 return dependent_template_p (arg
);
21685 else if (ARGUMENT_PACK_P (arg
))
21687 tree args
= ARGUMENT_PACK_ARGS (arg
);
21688 int i
, len
= TREE_VEC_LENGTH (args
);
21689 for (i
= 0; i
< len
; ++i
)
21691 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
21697 else if (TYPE_P (arg
))
21698 return dependent_type_p (arg
);
21700 return (type_dependent_expression_p (arg
)
21701 || value_dependent_expression_p (arg
));
21704 /* Returns true if ARGS (a collection of template arguments) contains
21705 any types that require structural equality testing. */
21708 any_template_arguments_need_structural_equality_p (tree args
)
21715 if (args
== error_mark_node
)
21718 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
21720 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
21721 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
21723 tree arg
= TREE_VEC_ELT (level
, j
);
21724 tree packed_args
= NULL_TREE
;
21727 if (ARGUMENT_PACK_P (arg
))
21729 /* Look inside the argument pack. */
21730 packed_args
= ARGUMENT_PACK_ARGS (arg
);
21731 len
= TREE_VEC_LENGTH (packed_args
);
21734 for (k
= 0; k
< len
; ++k
)
21737 arg
= TREE_VEC_ELT (packed_args
, k
);
21739 if (error_operand_p (arg
))
21741 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
21743 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
21745 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
21746 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
21755 /* Returns true if ARGS (a collection of template arguments) contains
21756 any dependent arguments. */
21759 any_dependent_template_arguments_p (const_tree args
)
21766 if (args
== error_mark_node
)
21769 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
21771 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
21772 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
21773 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
21780 /* Returns TRUE if the template TMPL is dependent. */
21783 dependent_template_p (tree tmpl
)
21785 if (TREE_CODE (tmpl
) == OVERLOAD
)
21789 if (dependent_template_p (OVL_CURRENT (tmpl
)))
21791 tmpl
= OVL_NEXT (tmpl
);
21796 /* Template template parameters are dependent. */
21797 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
21798 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
21800 /* So are names that have not been looked up. */
21801 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
21803 /* So are member templates of dependent classes. */
21804 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
21805 return dependent_type_p (DECL_CONTEXT (tmpl
));
21809 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21812 dependent_template_id_p (tree tmpl
, tree args
)
21814 return (dependent_template_p (tmpl
)
21815 || any_dependent_template_arguments_p (args
));
21818 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21822 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
21826 if (!processing_template_decl
)
21829 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
21831 tree decl
= TREE_VEC_ELT (declv
, i
);
21832 tree init
= TREE_VEC_ELT (initv
, i
);
21833 tree cond
= TREE_VEC_ELT (condv
, i
);
21834 tree incr
= TREE_VEC_ELT (incrv
, i
);
21836 if (type_dependent_expression_p (decl
))
21839 if (init
&& type_dependent_expression_p (init
))
21842 if (type_dependent_expression_p (cond
))
21845 if (COMPARISON_CLASS_P (cond
)
21846 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
21847 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
21850 if (TREE_CODE (incr
) == MODOP_EXPR
)
21852 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
21853 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
21856 else if (type_dependent_expression_p (incr
))
21858 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
21860 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
21862 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
21864 tree t
= TREE_OPERAND (incr
, 1);
21865 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
21866 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
21875 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21876 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21877 no such TYPE can be found. Note that this function peers inside
21878 uninstantiated templates and therefore should be used only in
21879 extremely limited situations. ONLY_CURRENT_P restricts this
21880 peering to the currently open classes hierarchy (which is required
21881 when comparing types). */
21884 resolve_typename_type (tree type
, bool only_current_p
)
21893 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
21895 scope
= TYPE_CONTEXT (type
);
21896 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21897 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21898 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21899 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21900 identifier of the TYPENAME_TYPE anymore.
21901 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21902 TYPENAME_TYPE instead, we avoid messing up with a possible
21903 typedef variant case. */
21904 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
21906 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21907 it first before we can figure out what NAME refers to. */
21908 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
21910 if (TYPENAME_IS_RESOLVING_P (scope
))
21911 /* Given a class template A with a dependent base with nested type C,
21912 typedef typename A::C::C C will land us here, as trying to resolve
21913 the initial A::C leads to the local C typedef, which leads back to
21914 A::C::C. So we break the recursion now. */
21917 scope
= resolve_typename_type (scope
, only_current_p
);
21919 /* If we don't know what SCOPE refers to, then we cannot resolve the
21921 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
21923 /* If the SCOPE is a template type parameter, we have no way of
21924 resolving the name. */
21925 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
21927 /* If the SCOPE is not the current instantiation, there's no reason
21928 to look inside it. */
21929 if (only_current_p
&& !currently_open_class (scope
))
21931 /* If this is a typedef, we don't want to look inside (c++/11987). */
21932 if (typedef_variant_p (type
))
21934 /* If SCOPE isn't the template itself, it will not have a valid
21935 TYPE_FIELDS list. */
21936 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
21937 /* scope is either the template itself or a compatible instantiation
21938 like X<T>, so look up the name in the original template. */
21939 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
21941 /* scope is a partial instantiation, so we can't do the lookup or we
21942 will lose the template arguments. */
21944 /* Enter the SCOPE so that name lookup will be resolved as if we
21945 were in the class definition. In particular, SCOPE will no
21946 longer be considered a dependent type. */
21947 pushed_scope
= push_scope (scope
);
21948 /* Look up the declaration. */
21949 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
21950 tf_warning_or_error
);
21952 result
= NULL_TREE
;
21954 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21955 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21958 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type
))
21959 && TREE_CODE (decl
) == TYPE_DECL
)
21961 result
= TREE_TYPE (decl
);
21962 if (result
== error_mark_node
)
21963 result
= NULL_TREE
;
21965 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
21966 && DECL_CLASS_TEMPLATE_P (decl
))
21970 /* Obtain the template and the arguments. */
21971 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
21972 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
21973 /* Instantiate the template. */
21974 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
21975 /*entering_scope=*/0,
21976 tf_error
| tf_user
);
21977 if (result
== error_mark_node
)
21978 result
= NULL_TREE
;
21981 /* Leave the SCOPE. */
21983 pop_scope (pushed_scope
);
21985 /* If we failed to resolve it, return the original typename. */
21989 /* If lookup found a typename type, resolve that too. */
21990 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
21992 /* Ill-formed programs can cause infinite recursion here, so we
21993 must catch that. */
21994 TYPENAME_IS_RESOLVING_P (type
) = 1;
21995 result
= resolve_typename_type (result
, only_current_p
);
21996 TYPENAME_IS_RESOLVING_P (type
) = 0;
21999 /* Qualify the resulting type. */
22000 quals
= cp_type_quals (type
);
22002 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
22007 /* EXPR is an expression which is not type-dependent. Return a proxy
22008 for EXPR that can be used to compute the types of larger
22009 expressions containing EXPR. */
22012 build_non_dependent_expr (tree expr
)
22016 #ifdef ENABLE_CHECKING
22017 /* Try to get a constant value for all non-dependent expressions in
22018 order to expose bugs in *_dependent_expression_p and constexpr. */
22019 if (cxx_dialect
>= cxx11
)
22020 fold_non_dependent_expr (expr
);
22023 /* Preserve OVERLOADs; the functions must be available to resolve
22026 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
22027 inner_expr
= stmt_expr_value_expr (inner_expr
);
22028 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
22029 inner_expr
= TREE_OPERAND (inner_expr
, 0);
22030 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
22031 inner_expr
= TREE_OPERAND (inner_expr
, 1);
22032 if (is_overloaded_fn (inner_expr
)
22033 || TREE_CODE (inner_expr
) == OFFSET_REF
)
22035 /* There is no need to return a proxy for a variable. */
22038 /* Preserve string constants; conversions from string constants to
22039 "char *" are allowed, even though normally a "const char *"
22040 cannot be used to initialize a "char *". */
22041 if (TREE_CODE (expr
) == STRING_CST
)
22043 /* Preserve void and arithmetic constants, as an optimization -- there is no
22044 reason to create a new node. */
22045 if (TREE_CODE (expr
) == VOID_CST
22046 || TREE_CODE (expr
) == INTEGER_CST
22047 || TREE_CODE (expr
) == REAL_CST
)
22049 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
22050 There is at least one place where we want to know that a
22051 particular expression is a throw-expression: when checking a ?:
22052 expression, there are special rules if the second or third
22053 argument is a throw-expression. */
22054 if (TREE_CODE (expr
) == THROW_EXPR
)
22057 /* Don't wrap an initializer list, we need to be able to look inside. */
22058 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
22061 /* Don't wrap a dummy object, we need to be able to test for it. */
22062 if (is_dummy_object (expr
))
22065 if (TREE_CODE (expr
) == COND_EXPR
)
22066 return build3 (COND_EXPR
,
22068 TREE_OPERAND (expr
, 0),
22069 (TREE_OPERAND (expr
, 1)
22070 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
22071 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
22072 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
22073 if (TREE_CODE (expr
) == COMPOUND_EXPR
22074 && !COMPOUND_EXPR_OVERLOADED (expr
))
22075 return build2 (COMPOUND_EXPR
,
22077 TREE_OPERAND (expr
, 0),
22078 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
22080 /* If the type is unknown, it can't really be non-dependent */
22081 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
22083 /* Otherwise, build a NON_DEPENDENT_EXPR. */
22084 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
22087 /* ARGS is a vector of expressions as arguments to a function call.
22088 Replace the arguments with equivalent non-dependent expressions.
22089 This modifies ARGS in place. */
22092 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
22097 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
22099 tree newarg
= build_non_dependent_expr (arg
);
22101 (*args
)[ix
] = newarg
;
22105 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
22106 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
22110 make_auto_1 (tree name
)
22112 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
22113 TYPE_NAME (au
) = build_decl (input_location
,
22114 TYPE_DECL
, name
, au
);
22115 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
22116 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
22117 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
22118 TYPE_NAME (au
), NULL_TREE
);
22119 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
22120 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
22121 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
22127 make_decltype_auto (void)
22129 return make_auto_1 (get_identifier ("decltype(auto)"));
22135 return make_auto_1 (get_identifier ("auto"));
22138 /* Given type ARG, return std::initializer_list<ARG>. */
22143 tree std_init_list
= namespace_binding
22144 (get_identifier ("initializer_list"), std_node
);
22146 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
22148 error ("deducing from brace-enclosed initializer list requires "
22149 "#include <initializer_list>");
22150 return error_mark_node
;
22152 argvec
= make_tree_vec (1);
22153 TREE_VEC_ELT (argvec
, 0) = arg
;
22154 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
22155 NULL_TREE
, 0, tf_warning_or_error
);
22158 /* Replace auto in TYPE with std::initializer_list<auto>. */
22161 listify_autos (tree type
, tree auto_node
)
22163 tree init_auto
= listify (auto_node
);
22164 tree argvec
= make_tree_vec (1);
22165 TREE_VEC_ELT (argvec
, 0) = init_auto
;
22166 if (processing_template_decl
)
22167 argvec
= add_to_template_args (current_template_args (), argvec
);
22168 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
22171 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
22172 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
22175 do_auto_deduction (tree type
, tree init
, tree auto_node
)
22179 if (init
== error_mark_node
)
22180 return error_mark_node
;
22182 if (type_dependent_expression_p (init
))
22183 /* Defining a subset of type-dependent expressions that we can deduce
22184 from ahead of time isn't worth the trouble. */
22187 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
22188 with either a new invented type template parameter U or, if the
22189 initializer is a braced-init-list (8.5.4), with
22190 std::initializer_list<U>. */
22191 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
22193 if (!DIRECT_LIST_INIT_P (init
))
22194 type
= listify_autos (type
, auto_node
);
22195 else if (CONSTRUCTOR_NELTS (init
) == 1)
22196 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
22199 if (permerror (input_location
, "direct-list-initialization of "
22200 "%<auto%> requires exactly one element"))
22201 inform (input_location
,
22202 "for deduction to %<std::initializer_list%>, use copy-"
22203 "list-initialization (i.e. add %<=%> before the %<{%>)");
22204 type
= listify_autos (type
, auto_node
);
22208 init
= resolve_nondeduced_context (init
);
22210 targs
= make_tree_vec (1);
22211 if (AUTO_IS_DECLTYPE (auto_node
))
22213 bool id
= (DECL_P (init
) || (TREE_CODE (init
) == COMPONENT_REF
22214 && !REF_PARENTHESIZED_P (init
)));
22215 TREE_VEC_ELT (targs
, 0)
22216 = finish_decltype_type (init
, id
, tf_warning_or_error
);
22217 if (type
!= auto_node
)
22219 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
22220 return error_mark_node
;
22225 tree parms
= build_tree_list (NULL_TREE
, type
);
22226 tree tparms
= make_tree_vec (1);
22229 TREE_VEC_ELT (tparms
, 0)
22230 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
22231 val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
22232 DEDUCE_CALL
, LOOKUP_NORMAL
,
22233 NULL
, /*explain_p=*/false);
22236 if (processing_template_decl
)
22237 /* Try again at instantiation time. */
22239 if (type
&& type
!= error_mark_node
)
22240 /* If type is error_mark_node a diagnostic must have been
22241 emitted by now. Also, having a mention to '<type error>'
22242 in the diagnostic is not really useful to the user. */
22244 if (cfun
&& auto_node
== current_function_auto_return_pattern
22245 && LAMBDA_FUNCTION_P (current_function_decl
))
22246 error ("unable to deduce lambda return type from %qE", init
);
22248 error ("unable to deduce %qT from %qE", type
, init
);
22250 return error_mark_node
;
22254 /* If the list of declarators contains more than one declarator, the type
22255 of each declared variable is determined as described above. If the
22256 type deduced for the template parameter U is not the same in each
22257 deduction, the program is ill-formed. */
22258 if (TREE_TYPE (auto_node
)
22259 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
22261 if (cfun
&& auto_node
== current_function_auto_return_pattern
22262 && LAMBDA_FUNCTION_P (current_function_decl
))
22263 error ("inconsistent types %qT and %qT deduced for "
22264 "lambda return type", TREE_TYPE (auto_node
),
22265 TREE_VEC_ELT (targs
, 0));
22267 error ("inconsistent deduction for %qT: %qT and then %qT",
22268 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
22269 return error_mark_node
;
22271 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
22273 if (processing_template_decl
)
22274 targs
= add_to_template_args (current_template_args (), targs
);
22275 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
22278 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
22282 splice_late_return_type (tree type
, tree late_return_type
)
22286 if (late_return_type
== NULL_TREE
)
22288 argvec
= make_tree_vec (1);
22289 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
22290 if (processing_template_parmlist
)
22291 /* For a late-specified return type in a template type-parameter, we
22292 need to add a dummy argument level for its parmlist. */
22293 argvec
= add_to_template_args
22294 (make_tree_vec (processing_template_parmlist
), argvec
);
22295 if (current_template_parms
)
22296 argvec
= add_to_template_args (current_template_args (), argvec
);
22297 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
22300 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
22301 'decltype(auto)'. */
22304 is_auto (const_tree type
)
22306 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
22307 && (TYPE_IDENTIFIER (type
) == get_identifier ("auto")
22308 || TYPE_IDENTIFIER (type
) == get_identifier ("decltype(auto)")))
22314 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
22315 a use of `auto'. Returns NULL_TREE otherwise. */
22318 type_uses_auto (tree type
)
22320 return find_type_usage (type
, is_auto
);
22323 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
22324 'decltype(auto)' or a concept. */
22327 is_auto_or_concept (const_tree type
)
22329 return is_auto (type
); // or concept
22332 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
22333 a concept identifier) iff TYPE contains a use of a generic type. Returns
22334 NULL_TREE otherwise. */
22337 type_uses_auto_or_concept (tree type
)
22339 return find_type_usage (type
, is_auto_or_concept
);
22343 /* For a given template T, return the vector of typedefs referenced
22344 in T for which access check is needed at T instantiation time.
22345 T is either a FUNCTION_DECL or a RECORD_TYPE.
22346 Those typedefs were added to T by the function
22347 append_type_to_template_for_access_check. */
22349 vec
<qualified_typedef_usage_t
, va_gc
> *
22350 get_types_needing_access_check (tree t
)
22353 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
22355 if (!t
|| t
== error_mark_node
)
22358 if (!(ti
= get_template_info (t
)))
22361 if (CLASS_TYPE_P (t
)
22362 || TREE_CODE (t
) == FUNCTION_DECL
)
22364 if (!TI_TEMPLATE (ti
))
22367 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
22373 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
22374 tied to T. That list of typedefs will be access checked at
22375 T instantiation time.
22376 T is either a FUNCTION_DECL or a RECORD_TYPE.
22377 TYPE_DECL is a TYPE_DECL node representing a typedef.
22378 SCOPE is the scope through which TYPE_DECL is accessed.
22379 LOCATION is the location of the usage point of TYPE_DECL.
22381 This function is a subroutine of
22382 append_type_to_template_for_access_check. */
22385 append_type_to_template_for_access_check_1 (tree t
,
22388 location_t location
)
22390 qualified_typedef_usage_t typedef_usage
;
22393 if (!t
|| t
== error_mark_node
)
22396 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
22397 || CLASS_TYPE_P (t
))
22399 && TREE_CODE (type_decl
) == TYPE_DECL
22402 if (!(ti
= get_template_info (t
)))
22405 gcc_assert (TI_TEMPLATE (ti
));
22407 typedef_usage
.typedef_decl
= type_decl
;
22408 typedef_usage
.context
= scope
;
22409 typedef_usage
.locus
= location
;
22411 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
22414 /* Append TYPE_DECL to the template TEMPL.
22415 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
22416 At TEMPL instanciation time, TYPE_DECL will be checked to see
22417 if it can be accessed through SCOPE.
22418 LOCATION is the location of the usage point of TYPE_DECL.
22420 e.g. consider the following code snippet:
22427 template<class U> struct S
22429 C::myint mi; // <-- usage point of the typedef C::myint
22434 At S<char> instantiation time, we need to check the access of C::myint
22435 In other words, we need to check the access of the myint typedef through
22436 the C scope. For that purpose, this function will add the myint typedef
22437 and the scope C through which its being accessed to a list of typedefs
22438 tied to the template S. That list will be walked at template instantiation
22439 time and access check performed on each typedefs it contains.
22440 Note that this particular code snippet should yield an error because
22441 myint is private to C. */
22444 append_type_to_template_for_access_check (tree templ
,
22447 location_t location
)
22449 qualified_typedef_usage_t
*iter
;
22452 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
22454 /* Make sure we don't append the type to the template twice. */
22455 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
22456 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
22459 append_type_to_template_for_access_check_1 (templ
, type_decl
,
22463 /* Convert the generic type parameters in PARM that match the types given in the
22464 range [START_IDX, END_IDX) from the current_template_parms into generic type
22468 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
22470 tree current
= current_template_parms
;
22471 int depth
= TMPL_PARMS_DEPTH (current
);
22472 current
= INNERMOST_TEMPLATE_PARMS (current
);
22473 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
22475 for (int i
= 0; i
< start_idx
; ++i
)
22476 TREE_VEC_ELT (replacement
, i
)
22477 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
22479 for (int i
= start_idx
; i
< end_idx
; ++i
)
22481 /* Create a distinct parameter pack type from the current parm and add it
22482 to the replacement args to tsubst below into the generic function
22485 tree o
= TREE_TYPE (TREE_VALUE
22486 (TREE_VEC_ELT (current
, i
)));
22487 tree t
= copy_type (o
);
22488 TEMPLATE_TYPE_PARM_INDEX (t
)
22489 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
22491 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
22492 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
22493 TYPE_MAIN_VARIANT (t
) = t
;
22494 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
22495 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
22496 TREE_VEC_ELT (replacement
, i
) = t
;
22497 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
22500 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
22501 TREE_VEC_ELT (replacement
, i
)
22502 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
22504 /* If there are more levels then build up the replacement with the outer
22507 replacement
= add_to_template_args (template_parms_to_args
22508 (TREE_CHAIN (current_template_parms
)),
22511 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
22515 /* Set up the hash tables for template instantiations. */
22518 init_template_processing (void)
22520 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
22521 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
22524 /* Print stats about the template hash tables for -fstats. */
22527 print_template_statistics (void)
22529 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
22530 "%f collisions\n", (long) decl_specializations
->size (),
22531 (long) decl_specializations
->elements (),
22532 decl_specializations
->collisions ());
22533 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
22534 "%f collisions\n", (long) type_specializations
->size (),
22535 (long) type_specializations
->elements (),
22536 type_specializations
->collisions ());
22539 #include "gt-cp-pt.h"