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 "print-tree.h"
42 #include "stringpool.h"
45 #include "stor-layout.h"
49 #include "c-family/c-common.h"
50 #include "c-family/c-objc.h"
51 #include "cp-objcp-common.h"
52 #include "tree-inline.h"
56 #include "tree-iterator.h"
57 #include "type-utils.h"
61 /* The type of functions taking a tree, and some additional data, and
63 typedef int (*tree_fn_t
) (tree
, void*);
65 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
66 instantiations have been deferred, either because their definitions
67 were not yet available, or because we were putting off doing the work. */
68 struct GTY ((chain_next ("%h.next"))) pending_template
{
69 struct pending_template
*next
;
70 struct tinst_level
*tinst
;
73 static GTY(()) struct pending_template
*pending_templates
;
74 static GTY(()) struct pending_template
*last_pending_template
;
76 int processing_template_parmlist
;
77 static int template_header_count
;
79 static GTY(()) tree saved_trees
;
80 static vec
<int> inline_parm_levels
;
82 static GTY(()) struct tinst_level
*current_tinst_level
;
84 static GTY(()) tree saved_access_scope
;
86 /* Live only within one (recursive) call to tsubst_expr. We use
87 this to pass the statement expression node from the STMT_EXPR
88 to the EXPR_STMT that is its result. */
89 static tree cur_stmt_expr
;
91 // -------------------------------------------------------------------------- //
92 // Local Specialization Stack
94 // Implementation of the RAII helper for creating new local
96 local_specialization_stack::local_specialization_stack ()
97 : saved (local_specializations
)
99 local_specializations
= new hash_map
<tree
, tree
>;
102 local_specialization_stack::~local_specialization_stack ()
104 delete local_specializations
;
105 local_specializations
= saved
;
108 /* True if we've recursed into fn_type_unification too many times. */
109 static bool excessive_deduction_depth
;
111 struct GTY((for_user
)) spec_entry
118 struct spec_hasher
: ggc_hasher
<spec_entry
*>
120 static hashval_t
hash (spec_entry
*);
121 static bool equal (spec_entry
*, spec_entry
*);
124 static GTY (()) hash_table
<spec_hasher
> *decl_specializations
;
126 static GTY (()) hash_table
<spec_hasher
> *type_specializations
;
128 /* Contains canonical template parameter types. The vector is indexed by
129 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
130 TREE_LIST, whose TREE_VALUEs contain the canonical template
131 parameters of various types and levels. */
132 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
134 #define UNIFY_ALLOW_NONE 0
135 #define UNIFY_ALLOW_MORE_CV_QUAL 1
136 #define UNIFY_ALLOW_LESS_CV_QUAL 2
137 #define UNIFY_ALLOW_DERIVED 4
138 #define UNIFY_ALLOW_INTEGER 8
139 #define UNIFY_ALLOW_OUTER_LEVEL 16
140 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
141 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
143 enum template_base_result
{
145 tbr_ambiguous_baseclass
,
149 static void push_access_scope (tree
);
150 static void pop_access_scope (tree
);
151 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
152 unification_kind_t
, int,
154 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
155 unification_kind_t
, int, bool, bool);
156 static int unify (tree
, tree
, tree
, tree
, int, bool);
157 static void add_pending_template (tree
);
158 static tree
reopen_tinst_level (struct tinst_level
*);
159 static tree
tsubst_initializer_list (tree
, tree
);
160 static tree
get_partial_spec_bindings (tree
, tree
, tree
, tree
);
161 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
163 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
165 static void tsubst_enum (tree
, tree
, tree
);
166 static tree
add_to_template_args (tree
, tree
);
167 static tree
add_outermost_template_args (tree
, tree
);
168 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
169 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
171 static int type_unification_real (tree
, tree
, tree
, const tree
*,
172 unsigned int, int, unification_kind_t
, int,
173 vec
<deferred_access_check
, va_gc
> **,
175 static void note_template_header (int);
176 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
177 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
178 static tree
convert_template_argument (tree
, tree
, tree
,
179 tsubst_flags_t
, int, tree
);
180 static int for_each_template_parm (tree
, tree_fn_t
, void*,
181 hash_set
<tree
> *, bool);
182 static tree
expand_template_argument_pack (tree
);
183 static tree
build_template_parm_index (int, int, int, tree
, tree
);
184 static bool inline_needs_template_parms (tree
, bool);
185 static void push_inline_template_parms_recursive (tree
, int);
186 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
187 static int mark_template_parm (tree
, void *);
188 static int template_parm_this_level_p (tree
, void *);
189 static tree
tsubst_friend_function (tree
, tree
);
190 static tree
tsubst_friend_class (tree
, tree
);
191 static int can_complete_type_without_circularity (tree
);
192 static tree
get_bindings (tree
, tree
, tree
, bool);
193 static int template_decl_level (tree
);
194 static int check_cv_quals_for_unify (int, tree
, tree
);
195 static void template_parm_level_and_index (tree
, int*, int*);
196 static int unify_pack_expansion (tree
, tree
, tree
,
197 tree
, unification_kind_t
, bool, bool);
198 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
199 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
200 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
201 static void regenerate_decl_from_template (tree
, tree
);
202 static tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
203 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
204 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
205 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
206 static bool check_specialization_scope (void);
207 static tree
process_partial_specialization (tree
);
208 static void set_current_access_from_decl (tree
);
209 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
211 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
212 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
214 static bool template_template_parm_bindings_ok_p (tree
, tree
);
215 static int template_args_equal (tree
, tree
);
216 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
217 static tree
for_each_template_parm_r (tree
*, int *, void *);
218 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
219 static void copy_default_args_to_explicit_spec (tree
);
220 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
221 static bool dependent_template_arg_p (tree
);
222 static bool any_template_arguments_need_structural_equality_p (tree
);
223 static bool dependent_type_p_r (tree
);
224 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
225 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
226 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
227 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
229 static tree
listify (tree
);
230 static tree
listify_autos (tree
, tree
);
231 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
232 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
234 /* Make the current scope suitable for access checking when we are
235 processing T. T can be FUNCTION_DECL for instantiated function
236 template, VAR_DECL for static member variable, or TYPE_DECL for
237 alias template (needed by instantiate_decl). */
240 push_access_scope (tree t
)
242 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
243 || TREE_CODE (t
) == TYPE_DECL
);
245 if (DECL_FRIEND_CONTEXT (t
))
246 push_nested_class (DECL_FRIEND_CONTEXT (t
));
247 else if (DECL_CLASS_SCOPE_P (t
))
248 push_nested_class (DECL_CONTEXT (t
));
250 push_to_top_level ();
252 if (TREE_CODE (t
) == FUNCTION_DECL
)
254 saved_access_scope
= tree_cons
255 (NULL_TREE
, current_function_decl
, saved_access_scope
);
256 current_function_decl
= t
;
260 /* Restore the scope set up by push_access_scope. T is the node we
264 pop_access_scope (tree t
)
266 if (TREE_CODE (t
) == FUNCTION_DECL
)
268 current_function_decl
= TREE_VALUE (saved_access_scope
);
269 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
272 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
275 pop_from_top_level ();
278 /* Do any processing required when DECL (a member template
279 declaration) is finished. Returns the TEMPLATE_DECL corresponding
280 to DECL, unless it is a specialization, in which case the DECL
281 itself is returned. */
284 finish_member_template_decl (tree decl
)
286 if (decl
== error_mark_node
)
287 return error_mark_node
;
289 gcc_assert (DECL_P (decl
));
291 if (TREE_CODE (decl
) == TYPE_DECL
)
295 type
= TREE_TYPE (decl
);
296 if (type
== error_mark_node
)
297 return error_mark_node
;
298 if (MAYBE_CLASS_TYPE_P (type
)
299 && CLASSTYPE_TEMPLATE_INFO (type
)
300 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
302 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
303 check_member_template (tmpl
);
308 else if (TREE_CODE (decl
) == FIELD_DECL
)
309 error ("data member %qD cannot be a member template", decl
);
310 else if (DECL_TEMPLATE_INFO (decl
))
312 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
314 check_member_template (DECL_TI_TEMPLATE (decl
));
315 return DECL_TI_TEMPLATE (decl
);
321 error ("invalid member template declaration %qD", decl
);
323 return error_mark_node
;
326 /* Create a template info node. */
329 build_template_info (tree template_decl
, tree template_args
)
331 tree result
= make_node (TEMPLATE_INFO
);
332 TI_TEMPLATE (result
) = template_decl
;
333 TI_ARGS (result
) = template_args
;
337 /* Return the template info node corresponding to T, whatever T is. */
340 get_template_info (const_tree t
)
342 tree tinfo
= NULL_TREE
;
344 if (!t
|| t
== error_mark_node
)
347 if (TREE_CODE (t
) == NAMESPACE_DECL
)
350 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
351 tinfo
= DECL_TEMPLATE_INFO (t
);
353 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
356 if (OVERLOAD_TYPE_P (t
))
357 tinfo
= TYPE_TEMPLATE_INFO (t
);
358 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
359 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
364 /* Returns the template nesting level of the indicated class TYPE.
374 A<T>::B<U> has depth two, while A<T> has depth one.
375 Both A<T>::B<int> and A<int>::B<U> have depth one, if
376 they are instantiations, not specializations.
378 This function is guaranteed to return 0 if passed NULL_TREE so
379 that, for example, `template_class_depth (current_class_type)' is
383 template_class_depth (tree type
)
388 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
389 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
390 ? CP_DECL_CONTEXT (type
) : CP_TYPE_CONTEXT (type
))
392 tree tinfo
= get_template_info (type
);
394 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
395 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
402 /* Subroutine of maybe_begin_member_template_processing.
403 Returns true if processing DECL needs us to push template parms. */
406 inline_needs_template_parms (tree decl
, bool nsdmi
)
408 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
411 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
412 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
415 /* Subroutine of maybe_begin_member_template_processing.
416 Push the template parms in PARMS, starting from LEVELS steps into the
417 chain, and ending at the beginning, since template parms are listed
421 push_inline_template_parms_recursive (tree parmlist
, int levels
)
423 tree parms
= TREE_VALUE (parmlist
);
427 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
429 ++processing_template_decl
;
430 current_template_parms
431 = tree_cons (size_int (processing_template_decl
),
432 parms
, current_template_parms
);
433 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
435 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
437 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
439 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
441 if (error_operand_p (parm
))
444 gcc_assert (DECL_P (parm
));
446 switch (TREE_CODE (parm
))
455 /* Make a CONST_DECL as is done in process_template_parm.
456 It is ugly that we recreate this here; the original
457 version built in process_template_parm is no longer
459 tree decl
= build_decl (DECL_SOURCE_LOCATION (parm
),
460 CONST_DECL
, DECL_NAME (parm
),
462 DECL_ARTIFICIAL (decl
) = 1;
463 TREE_CONSTANT (decl
) = 1;
464 TREE_READONLY (decl
) = 1;
465 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
466 SET_DECL_TEMPLATE_PARM_P (decl
);
477 /* Restore the template parameter context for a member template, a
478 friend template defined in a class definition, or a non-template
479 member of template class. */
482 maybe_begin_member_template_processing (tree decl
)
486 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
490 tree ctx
= DECL_CONTEXT (decl
);
491 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
492 /* Disregard full specializations (c++/60999). */
493 && uses_template_parms (ctx
)
494 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
497 if (inline_needs_template_parms (decl
, nsdmi
))
499 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
500 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
502 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
505 parms
= TREE_CHAIN (parms
);
508 push_inline_template_parms_recursive (parms
, levels
);
511 /* Remember how many levels of template parameters we pushed so that
512 we can pop them later. */
513 inline_parm_levels
.safe_push (levels
);
516 /* Undo the effects of maybe_begin_member_template_processing. */
519 maybe_end_member_template_processing (void)
524 if (inline_parm_levels
.length () == 0)
527 last
= inline_parm_levels
.pop ();
528 for (i
= 0; i
< last
; ++i
)
530 --processing_template_decl
;
531 current_template_parms
= TREE_CHAIN (current_template_parms
);
536 /* Return a new template argument vector which contains all of ARGS,
537 but has as its innermost set of arguments the EXTRA_ARGS. */
540 add_to_template_args (tree args
, tree extra_args
)
547 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
550 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
551 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
553 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
554 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
556 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
557 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
562 /* Like add_to_template_args, but only the outermost ARGS are added to
563 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
564 (EXTRA_ARGS) levels are added. This function is used to combine
565 the template arguments from a partial instantiation with the
566 template arguments used to attain the full instantiation from the
567 partial instantiation. */
570 add_outermost_template_args (tree args
, tree extra_args
)
574 /* If there are more levels of EXTRA_ARGS than there are ARGS,
575 something very fishy is going on. */
576 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
578 /* If *all* the new arguments will be the EXTRA_ARGS, just return
580 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
583 /* For the moment, we make ARGS look like it contains fewer levels. */
584 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
586 new_args
= add_to_template_args (args
, extra_args
);
588 /* Now, we restore ARGS to its full dimensions. */
589 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
594 /* Return the N levels of innermost template arguments from the ARGS. */
597 get_innermost_template_args (tree args
, int n
)
605 /* If N is 1, just return the innermost set of template arguments. */
607 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
609 /* If we're not removing anything, just return the arguments we were
611 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
612 gcc_assert (extra_levels
>= 0);
613 if (extra_levels
== 0)
616 /* Make a new set of arguments, not containing the outer arguments. */
617 new_args
= make_tree_vec (n
);
618 for (i
= 1; i
<= n
; ++i
)
619 SET_TMPL_ARGS_LEVEL (new_args
, i
,
620 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
625 /* The inverse of get_innermost_template_args: Return all but the innermost
626 EXTRA_LEVELS levels of template arguments from the ARGS. */
629 strip_innermost_template_args (tree args
, int extra_levels
)
632 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
637 /* If N is 1, just return the outermost set of template arguments. */
639 return TMPL_ARGS_LEVEL (args
, 1);
641 /* If we're not removing anything, just return the arguments we were
643 gcc_assert (extra_levels
>= 0);
644 if (extra_levels
== 0)
647 /* Make a new set of arguments, not containing the inner arguments. */
648 new_args
= make_tree_vec (n
);
649 for (i
= 1; i
<= n
; ++i
)
650 SET_TMPL_ARGS_LEVEL (new_args
, i
,
651 TMPL_ARGS_LEVEL (args
, i
));
656 /* We've got a template header coming up; push to a new level for storing
660 begin_template_parm_list (void)
662 /* We use a non-tag-transparent scope here, which causes pushtag to
663 put tags in this scope, rather than in the enclosing class or
664 namespace scope. This is the right thing, since we want
665 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
666 global template class, push_template_decl handles putting the
667 TEMPLATE_DECL into top-level scope. For a nested template class,
670 template <class T> struct S1 {
671 template <class T> struct S2 {};
674 pushtag contains special code to call pushdecl_with_scope on the
675 TEMPLATE_DECL for S2. */
676 begin_scope (sk_template_parms
, NULL
);
677 ++processing_template_decl
;
678 ++processing_template_parmlist
;
679 note_template_header (0);
682 /* This routine is called when a specialization is declared. If it is
683 invalid to declare a specialization here, an error is reported and
684 false is returned, otherwise this routine will return true. */
687 check_specialization_scope (void)
689 tree scope
= current_scope ();
693 An explicit specialization shall be declared in the namespace of
694 which the template is a member, or, for member templates, in the
695 namespace of which the enclosing class or enclosing class
696 template is a member. An explicit specialization of a member
697 function, member class or static data member of a class template
698 shall be declared in the namespace of which the class template
700 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
702 error ("explicit specialization in non-namespace scope %qD", scope
);
708 In an explicit specialization declaration for a member of a class
709 template or a member template that appears in namespace scope,
710 the member template and some of its enclosing class templates may
711 remain unspecialized, except that the declaration shall not
712 explicitly specialize a class member template if its enclosing
713 class templates are not explicitly specialized as well. */
714 if (current_template_parms
)
716 error ("enclosing class templates are not explicitly specialized");
723 /* We've just seen template <>. */
726 begin_specialization (void)
728 begin_scope (sk_template_spec
, NULL
);
729 note_template_header (1);
730 return check_specialization_scope ();
733 /* Called at then end of processing a declaration preceded by
737 end_specialization (void)
740 reset_specialization ();
743 /* Any template <>'s that we have seen thus far are not referring to a
744 function specialization. */
747 reset_specialization (void)
749 processing_specialization
= 0;
750 template_header_count
= 0;
753 /* We've just seen a template header. If SPECIALIZATION is nonzero,
754 it was of the form template <>. */
757 note_template_header (int specialization
)
759 processing_specialization
= specialization
;
760 template_header_count
++;
763 /* We're beginning an explicit instantiation. */
766 begin_explicit_instantiation (void)
768 gcc_assert (!processing_explicit_instantiation
);
769 processing_explicit_instantiation
= true;
774 end_explicit_instantiation (void)
776 gcc_assert (processing_explicit_instantiation
);
777 processing_explicit_instantiation
= false;
780 /* An explicit specialization or partial specialization of TMPL is being
781 declared. Check that the namespace in which the specialization is
782 occurring is permissible. Returns false iff it is invalid to
783 specialize TMPL in the current namespace. */
786 check_specialization_namespace (tree tmpl
)
788 tree tpl_ns
= decl_namespace_context (tmpl
);
792 An explicit specialization shall be declared in the namespace of
793 which the template is a member, or, for member templates, in the
794 namespace of which the enclosing class or enclosing class
795 template is a member. An explicit specialization of a member
796 function, member class or static data member of a class template
797 shall be declared in the namespace of which the class template is
799 if (current_scope() != DECL_CONTEXT (tmpl
)
800 && !at_namespace_scope_p ())
802 error ("specialization of %qD must appear at namespace scope", tmpl
);
805 if (is_associated_namespace (current_namespace
, tpl_ns
))
806 /* Same or super-using namespace. */
810 permerror (input_location
, "specialization of %qD in different namespace", tmpl
);
811 permerror (input_location
, " from definition of %q+#D", tmpl
);
816 /* SPEC is an explicit instantiation. Check that it is valid to
817 perform this explicit instantiation in the current namespace. */
820 check_explicit_instantiation_namespace (tree spec
)
824 /* DR 275: An explicit instantiation shall appear in an enclosing
825 namespace of its template. */
826 ns
= decl_namespace_context (spec
);
827 if (!is_ancestor (current_namespace
, ns
))
828 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
829 "(which does not enclose namespace %qD)",
830 spec
, current_namespace
, ns
);
833 // Returns the TEMPLATE_DECL that defines the partial template specialization
834 // TYPE. If TYPE is not found in the list of partial specializations, then
835 // it must be an explicit specialization.
837 get_specializing_template_decl (tree type
)
839 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
840 tree decl
= TYPE_MAIN_DECL (type
);
841 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
844 tree spec
= TREE_VALUE (specs
);
845 if (DECL_TEMPLATE_RESULT (spec
) == decl
)
847 specs
= TREE_CHAIN (specs
);
852 // Return the template constraints for the template specialization TYPE.
854 get_specialization_constraints (tree type
)
856 // If TYPE is a partial specialization, return the constraints for
857 // that type. If it is an explicit specialization, return null since
858 // non-templates cannot be constrained.
859 if (tree d
= get_specializing_template_decl (type
))
860 return get_constraints (d
);
865 // Returns the type of a template specialization only if that
866 // specialization needs to defined. Otherwise (e.g., if the type has
867 // already been defined), the function returns NULL_TREE.
869 maybe_new_partial_specialization (tree type
)
871 // An implicit instantiation of an incomplete type implies
872 // the definition of a new class template.
874 // template<typename T>
877 // template<typename T>
880 // Here, S<T*> is an implicit instantiation of S whose type
882 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
885 // It can also be the case that TYPE is a completed specialization.
886 // Continuing the previous example, suppose we also declare:
888 // template<typename T>
889 // requires Integral<T>
892 // Here, S<T*> refers to the specialization S<T*> defined
893 // above. However, we need to differentiate definitions because
894 // we intend to define a new partial specialization. In this case,
895 // we rely on the fact that the constraints are different for
896 // this declaration than that above.
898 // Note that we also get here for injected class names and
899 // late-parsed template definitions. We must ensure that we
900 // do not create new type declarations for those cases.
902 // FIXME: This may allow multiple partial specializations to have
903 // equivalent constraints. We need to check this against all
904 // partial specializations with euqivalent type. For exmaple:
906 // template<typename T> struct S;
907 // template<typename T> struct S<T*>; // #1
908 // template<C T> struct S<T*>; // #2
909 // template<C T> struct S<T*>; // differs from #1 but not #2
910 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
912 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
913 tree args
= CLASSTYPE_TI_ARGS (type
);
915 // If TYPE is the same as the template's type, this is clearly
916 // not a specialization. This happens when you define a member
917 // template outside of its enclosing class.
918 if (type
== DECL_TEMPLATE_RESULT (tmpl
))
921 // If there are no template parameters, this cannot be a new
922 // partial template specializtion?
923 if (!current_template_parms
)
926 // If the constraints are not the same as those of the primary
927 // then, we can probably create a new specialization.
928 tree tmpl_constr
= TEMPLATE_PARM_CONSTRAINTS (current_template_parms
);
929 tree type_constr
= build_constraints (tmpl_constr
, NULL_TREE
);
930 tree main_constr
= get_constraints (tmpl
);
931 if (equivalent_constraints (type_constr
, main_constr
))
934 // Also, if there's a pre-existing specialization with matching
935 // constraints, then this also isn't new.
936 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
939 tree spec_tmpl
= TREE_VALUE (specs
);
940 tree spec_constr
= get_constraints (spec_tmpl
);
941 if (equivalent_constraints (type_constr
, spec_constr
))
943 specs
= TREE_CHAIN (specs
);
946 // Create a new type node (and corresponding type decl)
947 // for the newly declared specialization.
948 tree t
= make_class_type (TREE_CODE (type
));
949 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
950 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (type
);
951 TYPE_CANONICAL (t
) = t
;
952 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
954 // Build the corresponding type decl.
955 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
956 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
957 DECL_SOURCE_LOCATION (d
) = input_location
;
965 /* The TYPE is being declared. If it is a template type, that means it
966 is a partial specialization. Do appropriate error-checking. */
969 maybe_process_partial_specialization (tree type
)
972 if (type
== error_mark_node
)
973 return error_mark_node
;
975 /* A lambda that appears in specialization context is not itself a
977 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
980 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
982 error ("name of class shadows template template parameter %qD",
984 return error_mark_node
;
987 context
= TYPE_CONTEXT (type
);
989 if (TYPE_ALIAS_P (type
))
991 if (TYPE_TEMPLATE_INFO (type
)
992 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type
)))
993 error ("specialization of alias template %qD",
994 TYPE_TI_TEMPLATE (type
));
996 error ("explicit specialization of non-template %qT", type
);
997 return error_mark_node
;
999 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
1001 /* This is for ordinary explicit specialization and partial
1002 specialization of a template class such as:
1004 template <> class C<int>;
1008 template <class T> class C<T*>;
1010 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1012 if (tree t
= maybe_new_partial_specialization (type
))
1014 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
1015 && !at_namespace_scope_p ())
1016 return error_mark_node
;
1017 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
1018 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
1019 if (processing_template_decl
)
1021 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
1022 if (decl
== error_mark_node
)
1023 return error_mark_node
;
1024 return TREE_TYPE (decl
);
1027 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
1028 error ("specialization of %qT after instantiation", type
);
1029 else if (errorcount
&& !processing_specialization
1030 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
1031 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
1032 /* Trying to define a specialization either without a template<> header
1033 or in an inappropriate place. We've already given an error, so just
1034 bail now so we don't actually define the specialization. */
1035 return error_mark_node
;
1037 else if (CLASS_TYPE_P (type
)
1038 && !CLASSTYPE_USE_TEMPLATE (type
)
1039 && CLASSTYPE_TEMPLATE_INFO (type
)
1040 && context
&& CLASS_TYPE_P (context
)
1041 && CLASSTYPE_TEMPLATE_INFO (context
))
1043 /* This is for an explicit specialization of member class
1044 template according to [temp.expl.spec/18]:
1046 template <> template <class U> class C<int>::D;
1048 The context `C<int>' must be an implicit instantiation.
1049 Otherwise this is just a member class template declared
1052 template <> class C<int> { template <class U> class D; };
1053 template <> template <class U> class C<int>::D;
1055 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1056 while in the second case, `C<int>::D' is a primary template
1057 and `C<T>::D' may not exist. */
1059 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1060 && !COMPLETE_TYPE_P (type
))
1063 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1065 if (current_namespace
1066 != decl_namespace_context (tmpl
))
1068 permerror (input_location
, "specializing %q#T in different namespace", type
);
1069 permerror (input_location
, " from definition of %q+#D", tmpl
);
1072 /* Check for invalid specialization after instantiation:
1074 template <> template <> class C<int>::D<int>;
1075 template <> template <class U> class C<int>::D; */
1077 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1078 t
; t
= TREE_CHAIN (t
))
1080 tree inst
= TREE_VALUE (t
);
1081 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1082 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1084 /* We already have a full specialization of this partial
1085 instantiation, or a full specialization has been
1086 looked up but not instantiated. Reassign it to the
1087 new member specialization template. */
1091 elt
.tmpl
= most_general_template (tmpl
);
1092 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1095 type_specializations
->remove_elt (&elt
);
1098 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1101 = type_specializations
->find_slot (&elt
, INSERT
);
1102 entry
= ggc_alloc
<spec_entry
> ();
1107 /* But if we've had an implicit instantiation, that's a
1108 problem ([temp.expl.spec]/6). */
1109 error ("specialization %qT after instantiation %qT",
1113 /* Mark TYPE as a specialization. And as a result, we only
1114 have one level of template argument for the innermost
1116 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1117 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1118 CLASSTYPE_TI_ARGS (type
)
1119 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1122 else if (processing_specialization
)
1124 /* Someday C++0x may allow for enum template specialization. */
1125 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1126 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1127 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1128 "of %qD not allowed by ISO C++", type
);
1131 error ("explicit specialization of non-template %qT", type
);
1132 return error_mark_node
;
1139 /* Returns nonzero if we can optimize the retrieval of specializations
1140 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1141 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1144 optimize_specialization_lookup_p (tree tmpl
)
1146 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1147 && DECL_CLASS_SCOPE_P (tmpl
)
1148 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1150 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1151 /* The optimized lookup depends on the fact that the
1152 template arguments for the member function template apply
1153 purely to the containing class, which is not true if the
1154 containing class is an explicit or partial
1156 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1157 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1158 && !DECL_CONV_FN_P (tmpl
)
1159 /* It is possible to have a template that is not a member
1160 template and is not a member of a template class:
1162 template <typename T>
1163 struct S { friend A::f(); };
1165 Here, the friend function is a template, but the context does
1166 not have template information. The optimized lookup relies
1167 on having ARGS be the template arguments for both the class
1168 and the function template. */
1169 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1172 /* Retrieve the specialization (in the sense of [temp.spec] - a
1173 specialization is either an instantiation or an explicit
1174 specialization) of TMPL for the given template ARGS. If there is
1175 no such specialization, return NULL_TREE. The ARGS are a vector of
1176 arguments, or a vector of vectors of arguments, in the case of
1177 templates with more than one level of parameters.
1179 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1180 then we search for a partial specialization matching ARGS. This
1181 parameter is ignored if TMPL is not a class template.
1183 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1184 result is a NONTYPE_ARGUMENT_PACK. */
1187 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1189 if (tmpl
== NULL_TREE
)
1192 if (args
== error_mark_node
)
1195 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1196 || TREE_CODE (tmpl
) == FIELD_DECL
);
1198 /* There should be as many levels of arguments as there are
1199 levels of parameters. */
1200 gcc_assert (TMPL_ARGS_DEPTH (args
)
1201 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1202 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1203 : template_class_depth (DECL_CONTEXT (tmpl
))));
1205 if (optimize_specialization_lookup_p (tmpl
))
1207 tree class_template
;
1208 tree class_specialization
;
1209 vec
<tree
, va_gc
> *methods
;
1213 /* The template arguments actually apply to the containing
1214 class. Find the class specialization with those
1216 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1217 class_specialization
1218 = retrieve_specialization (class_template
, args
, 0);
1219 if (!class_specialization
)
1221 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1222 for the specialization. */
1223 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1226 /* Iterate through the methods with the indicated name, looking
1227 for the one that has an instance of TMPL. */
1228 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1229 for (fns
= (*methods
)[idx
]; fns
; fns
= OVL_NEXT (fns
))
1231 tree fn
= OVL_CURRENT (fns
);
1232 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1233 /* using-declarations can add base methods to the method vec,
1234 and we don't want those here. */
1235 && DECL_CONTEXT (fn
) == class_specialization
)
1244 hash_table
<spec_hasher
> *specializations
;
1248 elt
.spec
= NULL_TREE
;
1250 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1251 specializations
= type_specializations
;
1253 specializations
= decl_specializations
;
1256 hash
= spec_hasher::hash (&elt
);
1257 found
= specializations
->find_with_hash (&elt
, hash
);
1265 /* Like retrieve_specialization, but for local declarations. */
1268 retrieve_local_specialization (tree tmpl
)
1270 if (local_specializations
== NULL
)
1273 tree
*slot
= local_specializations
->get (tmpl
);
1274 return slot
? *slot
: NULL_TREE
;
1277 /* Returns nonzero iff DECL is a specialization of TMPL. */
1280 is_specialization_of (tree decl
, tree tmpl
)
1284 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1288 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1294 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1296 for (t
= TREE_TYPE (decl
);
1298 t
= CLASSTYPE_USE_TEMPLATE (t
)
1299 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1300 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1307 /* Returns nonzero iff DECL is a specialization of friend declaration
1308 FRIEND_DECL according to [temp.friend]. */
1311 is_specialization_of_friend (tree decl
, tree friend_decl
)
1313 bool need_template
= true;
1316 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1317 || TREE_CODE (decl
) == TYPE_DECL
);
1319 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1320 of a template class, we want to check if DECL is a specialization
1322 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1323 && DECL_TEMPLATE_INFO (friend_decl
)
1324 && !DECL_USE_TEMPLATE (friend_decl
))
1326 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1327 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1328 need_template
= false;
1330 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1331 && !PRIMARY_TEMPLATE_P (friend_decl
))
1332 need_template
= false;
1334 /* There is nothing to do if this is not a template friend. */
1335 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1338 if (is_specialization_of (decl
, friend_decl
))
1342 A member of a class template may be declared to be a friend of a
1343 non-template class. In this case, the corresponding member of
1344 every specialization of the class template is a friend of the
1345 class granting friendship.
1347 For example, given a template friend declaration
1349 template <class T> friend void A<T>::f();
1351 the member function below is considered a friend
1353 template <> struct A<int> {
1357 For this type of template friend, TEMPLATE_DEPTH below will be
1358 nonzero. To determine if DECL is a friend of FRIEND, we first
1359 check if the enclosing class is a specialization of another. */
1361 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1363 && DECL_CLASS_SCOPE_P (decl
)
1364 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1365 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1367 /* Next, we check the members themselves. In order to handle
1368 a few tricky cases, such as when FRIEND_DECL's are
1370 template <class T> friend void A<T>::g(T t);
1371 template <class T> template <T t> friend void A<T>::h();
1375 void A<int>::g(int);
1376 template <int> void A<int>::h();
1378 we need to figure out ARGS, the template arguments from
1379 the context of DECL. This is required for template substitution
1380 of `T' in the function parameter of `g' and template parameter
1381 of `h' in the above examples. Here ARGS corresponds to `int'. */
1383 tree context
= DECL_CONTEXT (decl
);
1384 tree args
= NULL_TREE
;
1385 int current_depth
= 0;
1387 while (current_depth
< template_depth
)
1389 if (CLASSTYPE_TEMPLATE_INFO (context
))
1391 if (current_depth
== 0)
1392 args
= TYPE_TI_ARGS (context
);
1394 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1397 context
= TYPE_CONTEXT (context
);
1400 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1405 tree friend_args_type
;
1406 tree decl_args_type
;
1408 /* Make sure that both DECL and FRIEND_DECL are templates or
1410 is_template
= DECL_TEMPLATE_INFO (decl
)
1411 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1412 if (need_template
^ is_template
)
1414 else if (is_template
)
1416 /* If both are templates, check template parameter list. */
1418 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1420 if (!comp_template_parms
1421 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1425 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1428 decl_type
= TREE_TYPE (decl
);
1430 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1431 tf_none
, NULL_TREE
);
1432 if (friend_type
== error_mark_node
)
1435 /* Check if return types match. */
1436 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1439 /* Check if function parameter types match, ignoring the
1440 `this' parameter. */
1441 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1442 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1443 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1444 friend_args_type
= TREE_CHAIN (friend_args_type
);
1445 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1446 decl_args_type
= TREE_CHAIN (decl_args_type
);
1448 return compparms (decl_args_type
, friend_args_type
);
1452 /* DECL is a TYPE_DECL */
1454 tree decl_type
= TREE_TYPE (decl
);
1456 /* Make sure that both DECL and FRIEND_DECL are templates or
1459 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1460 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1462 if (need_template
^ is_template
)
1464 else if (is_template
)
1467 /* If both are templates, check the name of the two
1468 TEMPLATE_DECL's first because is_friend didn't. */
1469 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1470 != DECL_NAME (friend_decl
))
1473 /* Now check template parameter list. */
1475 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1477 return comp_template_parms
1478 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1482 return (DECL_NAME (decl
)
1483 == DECL_NAME (friend_decl
));
1489 /* Register the specialization SPEC as a specialization of TMPL with
1490 the indicated ARGS. IS_FRIEND indicates whether the specialization
1491 is actually just a friend declaration. Returns SPEC, or an
1492 equivalent prior declaration, if available.
1494 We also store instantiations of field packs in the hash table, even
1495 though they are not themselves templates, to make lookup easier. */
1498 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1502 spec_entry
**slot
= NULL
;
1505 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1506 || (TREE_CODE (tmpl
) == FIELD_DECL
1507 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1509 if (TREE_CODE (spec
) == FUNCTION_DECL
1510 && uses_template_parms (DECL_TI_ARGS (spec
)))
1511 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1512 register it; we want the corresponding TEMPLATE_DECL instead.
1513 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1514 the more obvious `uses_template_parms (spec)' to avoid problems
1515 with default function arguments. In particular, given
1516 something like this:
1518 template <class T> void f(T t1, T t = T())
1520 the default argument expression is not substituted for in an
1521 instantiation unless and until it is actually needed. */
1524 if (optimize_specialization_lookup_p (tmpl
))
1525 /* We don't put these specializations in the hash table, but we might
1526 want to give an error about a mismatch. */
1527 fn
= retrieve_specialization (tmpl
, args
, 0);
1535 hash
= spec_hasher::hash (&elt
);
1538 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1540 fn
= ((spec_entry
*) *slot
)->spec
;
1545 /* We can sometimes try to re-register a specialization that we've
1546 already got. In particular, regenerate_decl_from_template calls
1547 duplicate_decls which will update the specialization list. But,
1548 we'll still get called again here anyhow. It's more convenient
1549 to simply allow this than to try to prevent it. */
1552 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1554 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1556 if (DECL_ODR_USED (fn
)
1557 || DECL_EXPLICIT_INSTANTIATION (fn
))
1559 error ("specialization of %qD after instantiation",
1561 return error_mark_node
;
1566 /* This situation should occur only if the first
1567 specialization is an implicit instantiation, the
1568 second is an explicit specialization, and the
1569 implicit instantiation has not yet been used. That
1570 situation can occur if we have implicitly
1571 instantiated a member function and then specialized
1574 We can also wind up here if a friend declaration that
1575 looked like an instantiation turns out to be a
1578 template <class T> void foo(T);
1579 class S { friend void foo<>(int) };
1580 template <> void foo(int);
1582 We transform the existing DECL in place so that any
1583 pointers to it become pointers to the updated
1586 If there was a definition for the template, but not
1587 for the specialization, we want this to look as if
1588 there were no definition, and vice versa. */
1589 DECL_INITIAL (fn
) = NULL_TREE
;
1590 duplicate_decls (spec
, fn
, is_friend
);
1592 /* The call to duplicate_decls will have applied
1595 An explicit specialization of a function template
1596 is inline only if it is explicitly declared to be,
1597 and independently of whether its function template
1600 to the primary function; now copy the inline bits to
1601 the various clones. */
1602 FOR_EACH_CLONE (clone
, fn
)
1604 DECL_DECLARED_INLINE_P (clone
)
1605 = DECL_DECLARED_INLINE_P (fn
);
1606 DECL_SOURCE_LOCATION (clone
)
1607 = DECL_SOURCE_LOCATION (fn
);
1608 DECL_DELETED_FN (clone
)
1609 = DECL_DELETED_FN (fn
);
1611 check_specialization_namespace (tmpl
);
1616 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1618 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1619 /* Dup decl failed, but this is a new definition. Set the
1620 line number so any errors match this new
1622 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1628 return duplicate_decls (spec
, fn
, is_friend
);
1630 /* A specialization must be declared in the same namespace as the
1631 template it is specializing. */
1632 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1633 && !check_specialization_namespace (tmpl
))
1634 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1636 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1638 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1639 gcc_assert (tmpl
&& args
&& spec
);
1642 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1643 && PRIMARY_TEMPLATE_P (tmpl
)
1644 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1645 || variable_template_p (tmpl
))
1646 /* If TMPL is a forward declaration of a template function, keep a list
1647 of all specializations in case we need to reassign them to a friend
1648 template later in tsubst_friend_function.
1650 Also keep a list of all variable template instantiations so that
1651 process_partial_specialization can check whether a later partial
1652 specialization would have used it. */
1653 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1654 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1661 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1662 TMPL and ARGS members, ignores SPEC. */
1664 int comparing_specializations
;
1667 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1671 ++comparing_specializations
;
1672 equal
= (e1
->tmpl
== e2
->tmpl
1673 && comp_template_args (e1
->args
, e2
->args
));
1674 --comparing_specializations
;
1679 /* Returns a hash for a template TMPL and template arguments ARGS. */
1682 hash_tmpl_and_args (tree tmpl
, tree args
)
1684 hashval_t val
= DECL_UID (tmpl
);
1685 return iterative_hash_template_arg (args
, val
);
1688 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1692 spec_hasher::hash (spec_entry
*e
)
1694 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1697 /* Recursively calculate a hash value for a template argument ARG, for use
1698 in the hash tables of template specializations. */
1701 iterative_hash_template_arg (tree arg
, hashval_t val
)
1703 unsigned HOST_WIDE_INT i
;
1704 enum tree_code code
;
1707 if (arg
== NULL_TREE
)
1708 return iterative_hash_object (arg
, val
);
1713 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1714 /* We can get one of these when re-hashing a previous entry in the middle
1715 of substituting into a pack expansion. Just look through it. */
1716 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1718 code
= TREE_CODE (arg
);
1719 tclass
= TREE_CODE_CLASS (code
);
1721 val
= iterative_hash_object (code
, val
);
1728 case IDENTIFIER_NODE
:
1729 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1733 int i
, len
= TREE_VEC_LENGTH (arg
);
1734 for (i
= 0; i
< len
; ++i
)
1735 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1739 case TYPE_PACK_EXPANSION
:
1740 case EXPR_PACK_EXPANSION
:
1741 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1742 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1744 case TYPE_ARGUMENT_PACK
:
1745 case NONTYPE_ARGUMENT_PACK
:
1746 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1749 for (; arg
; arg
= TREE_CHAIN (arg
))
1750 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1754 for (; arg
; arg
= OVL_NEXT (arg
))
1755 val
= iterative_hash_template_arg (OVL_CURRENT (arg
), val
);
1761 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1762 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1764 val
= iterative_hash_template_arg (field
, val
);
1765 val
= iterative_hash_template_arg (value
, val
);
1771 if (!DECL_ARTIFICIAL (arg
))
1773 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1774 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1776 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1779 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1782 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1783 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1785 case TEMPLATE_PARM_INDEX
:
1786 val
= iterative_hash_template_arg
1787 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1788 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1789 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1792 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1793 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1794 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1797 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1799 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1803 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1804 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1805 val
= iterative_hash_object (code
, val
);
1806 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1809 /* A lambda can't appear in a template arg, but don't crash on
1811 gcc_assert (seen_error ());
1815 case IMPLICIT_CONV_EXPR
:
1816 case STATIC_CAST_EXPR
:
1817 case REINTERPRET_CAST_EXPR
:
1818 case CONST_CAST_EXPR
:
1819 case DYNAMIC_CAST_EXPR
:
1821 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1822 /* Now hash operands as usual. */
1832 if (alias_template_specialization_p (arg
))
1834 // We want an alias specialization that survived strip_typedefs
1835 // to hash differently from its TYPE_CANONICAL, to avoid hash
1836 // collisions that compare as different in template_args_equal.
1837 // These could be dependent specializations that strip_typedefs
1838 // left alone, or untouched specializations because
1839 // coerce_template_parms returns the unconverted template
1840 // arguments if it sees incomplete argument packs.
1841 tree ti
= TYPE_TEMPLATE_INFO (arg
);
1842 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1844 if (TYPE_CANONICAL (arg
))
1845 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1847 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1848 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1849 /* Otherwise just compare the types during lookup. */
1852 case tcc_declaration
:
1854 return iterative_hash_expr (arg
, val
);
1857 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1859 unsigned n
= cp_tree_operand_length (arg
);
1860 for (i
= 0; i
< n
; ++i
)
1861 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1869 /* Unregister the specialization SPEC as a specialization of TMPL.
1870 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1871 if the SPEC was listed as a specialization of TMPL.
1873 Note that SPEC has been ggc_freed, so we can't look inside it. */
1876 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1881 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1882 elt
.args
= TI_ARGS (tinfo
);
1883 elt
.spec
= NULL_TREE
;
1885 entry
= decl_specializations
->find (&elt
);
1888 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1889 gcc_assert (new_spec
!= NULL_TREE
);
1890 entry
->spec
= new_spec
;
1897 /* Like register_specialization, but for local declarations. We are
1898 registering SPEC, an instantiation of TMPL. */
1901 register_local_specialization (tree spec
, tree tmpl
)
1903 local_specializations
->put (tmpl
, spec
);
1906 /* TYPE is a class type. Returns true if TYPE is an explicitly
1907 specialized class. */
1910 explicit_class_specialization_p (tree type
)
1912 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1914 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1917 /* Print the list of functions at FNS, going through all the overloads
1918 for each element of the list. Alternatively, FNS can not be a
1919 TREE_LIST, in which case it will be printed together with all the
1922 MORE and *STR should respectively be FALSE and NULL when the function
1923 is called from the outside. They are used internally on recursive
1924 calls. print_candidates manages the two parameters and leaves NULL
1925 in *STR when it ends. */
1928 print_candidates_1 (tree fns
, bool more
, const char **str
)
1931 char *spaces
= NULL
;
1933 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1934 if (TREE_CODE (fn
) == TREE_LIST
)
1936 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1937 print_candidates_1 (TREE_VALUE (fn2
),
1938 TREE_CHAIN (fn2
) || more
, str
);
1942 tree cand
= OVL_CURRENT (fn
);
1945 /* Pick the prefix string. */
1946 if (!more
&& !OVL_NEXT (fns
))
1948 inform (DECL_SOURCE_LOCATION (cand
),
1949 "candidate is: %#D", cand
);
1953 *str
= _("candidates are:");
1954 spaces
= get_spaces (*str
);
1956 inform (DECL_SOURCE_LOCATION (cand
), "%s %#D", *str
, cand
);
1957 *str
= spaces
? spaces
: *str
;
1967 /* Print the list of candidate FNS in an error message. FNS can also
1968 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1971 print_candidates (tree fns
)
1973 const char *str
= NULL
;
1974 print_candidates_1 (fns
, false, &str
);
1975 gcc_assert (str
== NULL
);
1978 /* Get a (possibly) constrained template declaration for the
1979 purpose of ordering candidates. */
1981 get_template_for_ordering (tree list
)
1983 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
1984 tree f
= TREE_VALUE (list
);
1985 if (tree ti
= DECL_TEMPLATE_INFO (f
))
1986 return TI_TEMPLATE (ti
);
1990 /* Among candidates having the same signature, return the
1991 most constrained or NULL_TREE if there is no best candidate.
1992 If the signatures of candidates vary (e.g., template
1993 specialization vs. member function), then there can be no
1996 Note that we don't compare constraints on the functions
1997 themselves, but rather those of their templates. */
1999 most_constrained_function (tree candidates
)
2001 // Try to find the best candidate in a first pass.
2002 tree champ
= candidates
;
2003 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
2005 int winner
= more_constrained (get_template_for_ordering (champ
),
2006 get_template_for_ordering (c
));
2008 champ
= c
; // The candidate is more constrained
2009 else if (winner
== 0)
2010 return NULL_TREE
; // Neither is more constrained
2013 // Verify that the champ is better than previous candidates.
2014 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2015 if (!more_constrained (get_template_for_ordering (champ
),
2016 get_template_for_ordering (c
)))
2024 /* Returns the template (one of the functions given by TEMPLATE_ID)
2025 which can be specialized to match the indicated DECL with the
2026 explicit template args given in TEMPLATE_ID. The DECL may be
2027 NULL_TREE if none is available. In that case, the functions in
2028 TEMPLATE_ID are non-members.
2030 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2031 specialization of a member template.
2033 The TEMPLATE_COUNT is the number of references to qualifying
2034 template classes that appeared in the name of the function. See
2035 check_explicit_specialization for a more accurate description.
2037 TSK indicates what kind of template declaration (if any) is being
2038 declared. TSK_TEMPLATE indicates that the declaration given by
2039 DECL, though a FUNCTION_DECL, has template parameters, and is
2040 therefore a template function.
2042 The template args (those explicitly specified and those deduced)
2043 are output in a newly created vector *TARGS_OUT.
2045 If it is impossible to determine the result, an error message is
2046 issued. The error_mark_node is returned to indicate failure. */
2049 determine_specialization (tree template_id
,
2052 int need_member_template
,
2058 tree explicit_targs
;
2059 tree candidates
= NULL_TREE
;
2060 tree rejections
= NULL_TREE
;
2062 /* A TREE_LIST of templates of which DECL may be a specialization.
2063 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2064 corresponding TREE_PURPOSE is the set of template arguments that,
2065 when used to instantiate the template, would produce a function
2066 with the signature of DECL. */
2067 tree templates
= NULL_TREE
;
2069 cp_binding_level
*b
;
2071 *targs_out
= NULL_TREE
;
2073 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2074 return error_mark_node
;
2076 /* We shouldn't be specializing a member template of an
2077 unspecialized class template; we already gave an error in
2078 check_specialization_scope, now avoid crashing. */
2079 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
2080 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2082 gcc_assert (errorcount
);
2083 return error_mark_node
;
2086 fns
= TREE_OPERAND (template_id
, 0);
2087 explicit_targs
= TREE_OPERAND (template_id
, 1);
2089 if (fns
== error_mark_node
)
2090 return error_mark_node
;
2092 /* Check for baselinks. */
2093 if (BASELINK_P (fns
))
2094 fns
= BASELINK_FUNCTIONS (fns
);
2096 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2098 error ("%qD is not a function template", fns
);
2099 return error_mark_node
;
2101 else if (VAR_P (decl
) && !variable_template_p (fns
))
2103 error ("%qD is not a variable template", fns
);
2104 return error_mark_node
;
2107 /* Count the number of template headers specified for this
2110 for (b
= current_binding_level
;
2111 b
->kind
== sk_template_parms
;
2115 if (variable_template_p (fns
))
2116 templates
= tree_cons (explicit_targs
, fns
, templates
);
2117 else for (; fns
; fns
= OVL_NEXT (fns
))
2119 tree fn
= OVL_CURRENT (fns
);
2121 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2123 tree decl_arg_types
;
2127 /* In case of explicit specialization, we need to check if
2128 the number of template headers appearing in the specialization
2129 is correct. This is usually done in check_explicit_specialization,
2130 but the check done there cannot be exhaustive when specializing
2131 member functions. Consider the following code:
2133 template <> void A<int>::f(int);
2134 template <> template <> void A<int>::f(int);
2136 Assuming that A<int> is not itself an explicit specialization
2137 already, the first line specializes "f" which is a non-template
2138 member function, whilst the second line specializes "f" which
2139 is a template member function. So both lines are syntactically
2140 correct, and check_explicit_specialization does not reject
2143 Here, we can do better, as we are matching the specialization
2144 against the declarations. We count the number of template
2145 headers, and we check if they match TEMPLATE_COUNT + 1
2146 (TEMPLATE_COUNT is the number of qualifying template classes,
2147 plus there must be another header for the member template
2150 Notice that if header_count is zero, this is not a
2151 specialization but rather a template instantiation, so there
2152 is no check we can perform here. */
2154 if (header_count
&& header_count
!= template_count
+ 1)
2157 /* Check that the number of template arguments at the
2158 innermost level for DECL is the same as for FN. */
2159 if (current_binding_level
->kind
== sk_template_parms
2160 && !current_binding_level
->explicit_spec_p
2161 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2162 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2163 (current_template_parms
))))
2166 /* DECL might be a specialization of FN. */
2167 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2168 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2170 /* For a non-static member function, we need to make sure
2171 that the const qualification is the same. Since
2172 get_bindings does not try to merge the "this" parameter,
2173 we must do the comparison explicitly. */
2174 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2175 && !same_type_p (TREE_VALUE (fn_arg_types
),
2176 TREE_VALUE (decl_arg_types
)))
2179 /* Skip the "this" parameter and, for constructors of
2180 classes with virtual bases, the VTT parameter. A
2181 full specialization of a constructor will have a VTT
2182 parameter, but a template never will. */
2184 = skip_artificial_parms_for (decl
, decl_arg_types
);
2186 = skip_artificial_parms_for (fn
, fn_arg_types
);
2188 /* Function templates cannot be specializations; there are
2189 no partial specializations of functions. Therefore, if
2190 the type of DECL does not match FN, there is no
2193 Note that it should never be the case that we have both
2194 candidates added here, and for regular member functions
2196 if (tsk
== tsk_template
)
2198 if (compparms (fn_arg_types
, decl_arg_types
))
2199 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2203 /* See whether this function might be a specialization of this
2204 template. Suppress access control because we might be trying
2205 to make this specialization a friend, and we have already done
2206 access control for the declaration of the specialization. */
2207 push_deferring_access_checks (dk_no_check
);
2208 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2209 pop_deferring_access_checks ();
2212 /* We cannot deduce template arguments that when used to
2213 specialize TMPL will produce DECL. */
2216 /* Remove, from the set of candidates, all those functions
2217 whose constraints are not satisfied. */
2218 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2221 // Then, try to form the new function type.
2222 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
2223 if (insttype
== error_mark_node
)
2226 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2227 if (!compparms (fn_arg_types
, decl_arg_types
))
2230 /* Save this template, and the arguments deduced. */
2231 templates
= tree_cons (targs
, fn
, templates
);
2233 else if (need_member_template
)
2234 /* FN is an ordinary member function, and we need a
2235 specialization of a member template. */
2237 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2238 /* We can get IDENTIFIER_NODEs here in certain erroneous
2241 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2242 /* This is just an ordinary non-member function. Nothing can
2243 be a specialization of that. */
2245 else if (DECL_ARTIFICIAL (fn
))
2246 /* Cannot specialize functions that are created implicitly. */
2250 tree decl_arg_types
;
2252 /* This is an ordinary member function. However, since
2253 we're here, we can assume its enclosing class is a
2254 template class. For example,
2256 template <typename T> struct S { void f(); };
2257 template <> void S<int>::f() {}
2259 Here, S<int>::f is a non-template, but S<int> is a
2260 template class. If FN has the same type as DECL, we
2261 might be in business. */
2263 if (!DECL_TEMPLATE_INFO (fn
))
2264 /* Its enclosing class is an explicit specialization
2265 of a template class. This is not a candidate. */
2269 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2270 TREE_TYPE (TREE_TYPE (fn
))))
2271 /* The return types differ. */
2274 /* Adjust the type of DECL in case FN is a static member. */
2275 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2276 if (DECL_STATIC_FUNCTION_P (fn
)
2277 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2278 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2280 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2284 // If the deduced arguments do not satisfy the constraints,
2285 // this is not a candidate. If it fails, record the
2286 // rejected candidate.
2287 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2289 rejections
= tree_cons (NULL_TREE
, fn
, rejections
);
2293 // Add the candidate.
2294 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2298 if (templates
&& TREE_CHAIN (templates
))
2304 It is possible for a specialization with a given function
2305 signature to be instantiated from more than one function
2306 template. In such cases, explicit specification of the
2307 template arguments must be used to uniquely identify the
2308 function template specialization being specialized.
2310 Note that here, there's no suggestion that we're supposed to
2311 determine which of the candidate templates is most
2312 specialized. However, we, also have:
2316 Partial ordering of overloaded function template
2317 declarations is used in the following contexts to select
2318 the function template to which a function template
2319 specialization refers:
2321 -- when an explicit specialization refers to a function
2324 So, we do use the partial ordering rules, at least for now.
2325 This extension can only serve to make invalid programs valid,
2326 so it's safe. And, there is strong anecdotal evidence that
2327 the committee intended the partial ordering rules to apply;
2328 the EDG front end has that behavior, and John Spicer claims
2329 that the committee simply forgot to delete the wording in
2330 [temp.expl.spec]. */
2331 tree tmpl
= most_specialized_instantiation (templates
);
2332 if (tmpl
!= error_mark_node
)
2335 TREE_CHAIN (templates
) = NULL_TREE
;
2339 // Concepts allows multiple declarations of member functions
2340 // with the same signature. Like above, we need to rely on
2341 // on the partial ordering of those candidates to determine which
2343 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2345 if (tree cand
= most_constrained_function (candidates
))
2348 TREE_CHAIN (cand
) = NULL_TREE
;
2352 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2354 error ("template-id %qD for %q+D does not match any template "
2355 "declaration", template_id
, decl
);
2357 print_candidates (rejections
);
2358 else if (header_count
&& header_count
!= template_count
+ 1)
2359 inform (input_location
, "saw %d %<template<>%>, need %d for "
2360 "specializing a member function template",
2361 header_count
, template_count
+ 1);
2362 return error_mark_node
;
2364 else if ((templates
&& TREE_CHAIN (templates
))
2365 || (candidates
&& TREE_CHAIN (candidates
))
2366 || (templates
&& candidates
))
2368 error ("ambiguous template specialization %qD for %q+D",
2370 candidates
= chainon (candidates
, templates
);
2371 print_candidates (candidates
);
2372 return error_mark_node
;
2375 /* We have one, and exactly one, match. */
2378 tree fn
= TREE_VALUE (candidates
);
2379 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2381 // Propagate the candidate's constraints to the declaration.
2382 set_constraints (decl
, get_constraints (fn
));
2384 /* DECL is a re-declaration or partial instantiation of a template
2386 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2389 /* It was a specialization of an ordinary member function in a
2391 return DECL_TI_TEMPLATE (fn
);
2394 /* It was a specialization of a template. */
2395 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2396 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2398 *targs_out
= copy_node (targs
);
2399 SET_TMPL_ARGS_LEVEL (*targs_out
,
2400 TMPL_ARGS_DEPTH (*targs_out
),
2401 TREE_PURPOSE (templates
));
2404 *targs_out
= TREE_PURPOSE (templates
);
2406 return TREE_VALUE (templates
);
2409 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2410 but with the default argument values filled in from those in the
2414 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2417 tree new_spec_types
;
2422 if (spec_types
== void_list_node
)
2423 return void_list_node
;
2425 /* Substitute into the rest of the list. */
2427 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2428 TREE_CHAIN (tmpl_types
));
2430 /* Add the default argument for this parameter. */
2431 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2432 TREE_VALUE (spec_types
),
2436 /* DECL is an explicit specialization. Replicate default arguments
2437 from the template it specializes. (That way, code like:
2439 template <class T> void f(T = 3);
2440 template <> void f(double);
2443 works, as required.) An alternative approach would be to look up
2444 the correct default arguments at the call-site, but this approach
2445 is consistent with how implicit instantiations are handled. */
2448 copy_default_args_to_explicit_spec (tree decl
)
2453 tree new_spec_types
;
2457 tree object_type
= NULL_TREE
;
2458 tree in_charge
= NULL_TREE
;
2459 tree vtt
= NULL_TREE
;
2461 /* See if there's anything we need to do. */
2462 tmpl
= DECL_TI_TEMPLATE (decl
);
2463 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2464 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2465 if (TREE_PURPOSE (t
))
2470 old_type
= TREE_TYPE (decl
);
2471 spec_types
= TYPE_ARG_TYPES (old_type
);
2473 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2475 /* Remove the this pointer, but remember the object's type for
2477 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2478 spec_types
= TREE_CHAIN (spec_types
);
2479 tmpl_types
= TREE_CHAIN (tmpl_types
);
2481 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2483 /* DECL may contain more parameters than TMPL due to the extra
2484 in-charge parameter in constructors and destructors. */
2485 in_charge
= spec_types
;
2486 spec_types
= TREE_CHAIN (spec_types
);
2488 if (DECL_HAS_VTT_PARM_P (decl
))
2491 spec_types
= TREE_CHAIN (spec_types
);
2495 /* Compute the merged default arguments. */
2497 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2499 /* Compute the new FUNCTION_TYPE. */
2503 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2508 /* Put the in-charge parameter back. */
2509 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2510 TREE_VALUE (in_charge
),
2513 new_type
= build_method_type_directly (object_type
,
2514 TREE_TYPE (old_type
),
2518 new_type
= build_function_type (TREE_TYPE (old_type
),
2520 new_type
= cp_build_type_attribute_variant (new_type
,
2521 TYPE_ATTRIBUTES (old_type
));
2522 new_type
= build_exception_variant (new_type
,
2523 TYPE_RAISES_EXCEPTIONS (old_type
));
2525 if (TYPE_HAS_LATE_RETURN_TYPE (old_type
))
2526 TYPE_HAS_LATE_RETURN_TYPE (new_type
) = 1;
2528 TREE_TYPE (decl
) = new_type
;
2531 /* Return the number of template headers we expect to see for a definition
2532 or specialization of CTYPE or one of its non-template members. */
2535 num_template_headers_for_class (tree ctype
)
2537 int num_templates
= 0;
2539 while (ctype
&& CLASS_TYPE_P (ctype
))
2541 /* You're supposed to have one `template <...>' for every
2542 template class, but you don't need one for a full
2543 specialization. For example:
2545 template <class T> struct S{};
2546 template <> struct S<int> { void f(); };
2547 void S<int>::f () {}
2549 is correct; there shouldn't be a `template <>' for the
2550 definition of `S<int>::f'. */
2551 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2552 /* If CTYPE does not have template information of any
2553 kind, then it is not a template, nor is it nested
2554 within a template. */
2556 if (explicit_class_specialization_p (ctype
))
2558 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2561 ctype
= TYPE_CONTEXT (ctype
);
2564 return num_templates
;
2567 /* Do a simple sanity check on the template headers that precede the
2568 variable declaration DECL. */
2571 check_template_variable (tree decl
)
2573 tree ctx
= CP_DECL_CONTEXT (decl
);
2574 int wanted
= num_template_headers_for_class (ctx
);
2575 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2576 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2578 if (cxx_dialect
< cxx14
)
2579 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2580 "variable templates only available with "
2581 "-std=c++14 or -std=gnu++14");
2583 // Namespace-scope variable templates should have a template header.
2586 if (template_header_count
> wanted
)
2588 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2589 "too many template headers for %D (should be %d)",
2591 if (warned
&& CLASS_TYPE_P (ctx
)
2592 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2593 inform (DECL_SOURCE_LOCATION (decl
),
2594 "members of an explicitly specialized class are defined "
2595 "without a template header");
2599 /* Check to see if the function just declared, as indicated in
2600 DECLARATOR, and in DECL, is a specialization of a function
2601 template. We may also discover that the declaration is an explicit
2602 instantiation at this point.
2604 Returns DECL, or an equivalent declaration that should be used
2605 instead if all goes well. Issues an error message if something is
2606 amiss. Returns error_mark_node if the error is not easily
2609 FLAGS is a bitmask consisting of the following flags:
2611 2: The function has a definition.
2612 4: The function is a friend.
2614 The TEMPLATE_COUNT is the number of references to qualifying
2615 template classes that appeared in the name of the function. For
2618 template <class T> struct S { void f(); };
2621 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2622 classes are not counted in the TEMPLATE_COUNT, so that in
2624 template <class T> struct S {};
2625 template <> struct S<int> { void f(); }
2626 template <> void S<int>::f();
2628 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2629 invalid; there should be no template <>.)
2631 If the function is a specialization, it is marked as such via
2632 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2633 is set up correctly, and it is added to the list of specializations
2634 for that template. */
2637 check_explicit_specialization (tree declarator
,
2642 int have_def
= flags
& 2;
2643 int is_friend
= flags
& 4;
2644 bool is_concept
= flags
& 8;
2645 int specialization
= 0;
2646 int explicit_instantiation
= 0;
2647 int member_specialization
= 0;
2648 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2649 tree dname
= DECL_NAME (decl
);
2654 if (!processing_specialization
)
2657 tsk
= tsk_excessive_parms
;
2660 tsk
= current_tmpl_spec_kind (template_count
);
2665 if (processing_specialization
)
2668 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2670 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2673 /* This could be something like:
2675 template <class T> void f(T);
2676 class S { friend void f<>(int); } */
2680 /* This case handles bogus declarations like template <>
2681 template <class T> void f<int>(); */
2683 error ("template-id %qD in declaration of primary template",
2690 case tsk_invalid_member_spec
:
2691 /* The error has already been reported in
2692 check_specialization_scope. */
2693 return error_mark_node
;
2695 case tsk_invalid_expl_inst
:
2696 error ("template parameter list used in explicit instantiation");
2702 error ("definition provided for explicit instantiation");
2704 explicit_instantiation
= 1;
2707 case tsk_excessive_parms
:
2708 case tsk_insufficient_parms
:
2709 if (tsk
== tsk_excessive_parms
)
2710 error ("too many template parameter lists in declaration of %qD",
2712 else if (template_header_count
)
2713 error("too few template parameter lists in declaration of %qD", decl
);
2715 error("explicit specialization of %qD must be introduced by "
2716 "%<template <>%>", decl
);
2721 error ("explicit specialization declared %<concept%>");
2723 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2724 /* In cases like template<> constexpr bool v = true;
2725 We'll give an error in check_template_variable. */
2728 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2730 member_specialization
= 1;
2736 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2738 /* This case handles bogus declarations like template <>
2739 template <class T> void f<int>(); */
2741 if (!uses_template_parms (declarator
))
2742 error ("template-id %qD in declaration of primary template",
2744 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2746 /* Partial specialization of variable template. */
2747 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2751 else if (cxx_dialect
< cxx14
)
2752 error ("non-type partial specialization %qD "
2753 "is not allowed", declarator
);
2755 error ("non-class, non-variable partial specialization %qD "
2756 "is not allowed", declarator
);
2761 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2762 /* This is a specialization of a member template, without
2763 specialization the containing class. Something like:
2765 template <class T> struct S {
2766 template <class U> void f (U);
2768 template <> template <class U> void S<int>::f(U) {}
2770 That's a specialization -- but of the entire template. */
2778 if ((specialization
|| member_specialization
)
2779 /* This doesn't apply to variable templates. */
2780 && (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
2781 || TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
))
2783 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2784 for (; t
; t
= TREE_CHAIN (t
))
2785 if (TREE_PURPOSE (t
))
2787 permerror (input_location
,
2788 "default argument specified in explicit specialization");
2793 if (specialization
|| member_specialization
|| explicit_instantiation
)
2795 tree tmpl
= NULL_TREE
;
2796 tree targs
= NULL_TREE
;
2797 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2799 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2800 if (!was_template_id
)
2804 gcc_assert (identifier_p (declarator
));
2809 /* If there is no class context, the explicit instantiation
2810 must be at namespace scope. */
2811 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2813 /* Find the namespace binding, using the declaration
2815 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2817 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2819 error ("%qD is not a template function", dname
);
2820 fns
= error_mark_node
;
2824 tree fn
= OVL_CURRENT (fns
);
2825 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2826 CP_DECL_CONTEXT (fn
)))
2827 error ("%qD is not declared in %qD",
2828 decl
, current_namespace
);
2832 declarator
= lookup_template_function (fns
, NULL_TREE
);
2835 if (declarator
== error_mark_node
)
2836 return error_mark_node
;
2838 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2840 if (!explicit_instantiation
)
2841 /* A specialization in class scope. This is invalid,
2842 but the error will already have been flagged by
2843 check_specialization_scope. */
2844 return error_mark_node
;
2847 /* It's not valid to write an explicit instantiation in
2850 class C { template void f(); }
2852 This case is caught by the parser. However, on
2855 template class C { void f(); };
2857 (which is invalid) we can get here. The error will be
2864 else if (ctype
!= NULL_TREE
2865 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2867 // We'll match variable templates in start_decl.
2871 /* Find the list of functions in ctype that have the same
2872 name as the declared function. */
2873 tree name
= TREE_OPERAND (declarator
, 0);
2874 tree fns
= NULL_TREE
;
2877 if (constructor_name_p (name
, ctype
))
2879 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2881 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2882 : !CLASSTYPE_DESTRUCTORS (ctype
))
2884 /* From [temp.expl.spec]:
2886 If such an explicit specialization for the member
2887 of a class template names an implicitly-declared
2888 special member function (clause _special_), the
2889 program is ill-formed.
2891 Similar language is found in [temp.explicit]. */
2892 error ("specialization of implicitly-declared special member function");
2893 return error_mark_node
;
2896 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2899 if (!DECL_CONV_FN_P (decl
))
2901 idx
= lookup_fnfields_1 (ctype
, name
);
2903 fns
= (*CLASSTYPE_METHOD_VEC (ctype
))[idx
];
2907 vec
<tree
, va_gc
> *methods
;
2910 /* For a type-conversion operator, we cannot do a
2911 name-based lookup. We might be looking for `operator
2912 int' which will be a specialization of `operator T'.
2913 So, we find *all* the conversion operators, and then
2914 select from them. */
2917 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2919 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2920 methods
->iterate (idx
, &ovl
);
2923 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2924 /* There are no more conversion functions. */
2927 /* Glue all these conversion functions together
2928 with those we already have. */
2929 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2930 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2934 if (fns
== NULL_TREE
)
2936 error ("no member function %qD declared in %qT", name
, ctype
);
2937 return error_mark_node
;
2940 TREE_OPERAND (declarator
, 0) = fns
;
2943 /* Figure out what exactly is being specialized at this point.
2944 Note that for an explicit instantiation, even one for a
2945 member function, we cannot tell apriori whether the
2946 instantiation is for a member template, or just a member
2947 function of a template class. Even if a member template is
2948 being instantiated, the member template arguments may be
2949 elided if they can be deduced from the rest of the
2951 tmpl
= determine_specialization (declarator
, decl
,
2953 member_specialization
,
2957 if (!tmpl
|| tmpl
== error_mark_node
)
2958 /* We couldn't figure out what this declaration was
2960 return error_mark_node
;
2963 tree gen_tmpl
= most_general_template (tmpl
);
2965 if (explicit_instantiation
)
2967 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2968 is done by do_decl_instantiation later. */
2970 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2971 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2973 if (arg_depth
> parm_depth
)
2975 /* If TMPL is not the most general template (for
2976 example, if TMPL is a friend template that is
2977 injected into namespace scope), then there will
2978 be too many levels of TARGS. Remove some of them
2983 new_targs
= make_tree_vec (parm_depth
);
2984 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2985 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2986 = TREE_VEC_ELT (targs
, i
);
2990 return instantiate_template (tmpl
, targs
, tf_error
);
2993 /* If we thought that the DECL was a member function, but it
2994 turns out to be specializing a static member function,
2995 make DECL a static member function as well. */
2996 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
2997 && DECL_STATIC_FUNCTION_P (tmpl
)
2998 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2999 revert_static_member_fn (decl
);
3001 /* If this is a specialization of a member template of a
3002 template class, we want to return the TEMPLATE_DECL, not
3003 the specialization of it. */
3004 if (tsk
== tsk_template
&& !was_template_id
)
3006 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3007 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3008 DECL_INITIAL (result
) = NULL_TREE
;
3012 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3013 DECL_SOURCE_LOCATION (result
)
3014 = DECL_SOURCE_LOCATION (decl
);
3015 /* We want to use the argument list specified in the
3016 definition, not in the original declaration. */
3017 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3018 for (parm
= DECL_ARGUMENTS (result
); parm
;
3019 parm
= DECL_CHAIN (parm
))
3020 DECL_CONTEXT (parm
) = result
;
3022 return register_specialization (tmpl
, gen_tmpl
, targs
,
3026 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3027 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3029 if (was_template_id
)
3030 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3032 /* Inherit default function arguments from the template
3033 DECL is specializing. */
3034 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3035 copy_default_args_to_explicit_spec (decl
);
3037 /* This specialization has the same protection as the
3038 template it specializes. */
3039 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3040 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3042 /* 7.1.1-1 [dcl.stc]
3044 A storage-class-specifier shall not be specified in an
3045 explicit specialization...
3047 The parser rejects these, so unless action is taken here,
3048 explicit function specializations will always appear with
3051 The action recommended by the C++ CWG in response to C++
3052 defect report 605 is to make the storage class and linkage
3053 of the explicit specialization match the templated function:
3055 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3057 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3059 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3060 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3062 // A concept cannot be specialized.
3063 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3065 error ("explicit specialization of concept %qD", gen_tmpl
);
3066 return error_mark_node
;
3069 /* This specialization has the same linkage and visibility as
3070 the function template it specializes. */
3071 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3072 if (! TREE_PUBLIC (decl
))
3074 DECL_INTERFACE_KNOWN (decl
) = 1;
3075 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3077 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3078 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3080 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3081 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3085 /* If DECL is a friend declaration, declared using an
3086 unqualified name, the namespace associated with DECL may
3087 have been set incorrectly. For example, in:
3089 template <typename T> void f(T);
3091 struct S { friend void f<int>(int); }
3094 we will have set the DECL_CONTEXT for the friend
3095 declaration to N, rather than to the global namespace. */
3096 if (DECL_NAMESPACE_SCOPE_P (decl
))
3097 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3099 if (is_friend
&& !have_def
)
3100 /* This is not really a declaration of a specialization.
3101 It's just the name of an instantiation. But, it's not
3102 a request for an instantiation, either. */
3103 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3104 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3105 /* A specialization is not necessarily COMDAT. */
3106 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3107 && DECL_DECLARED_INLINE_P (decl
));
3108 else if (TREE_CODE (decl
) == VAR_DECL
)
3109 DECL_COMDAT (decl
) = false;
3111 /* Register this specialization so that we can find it
3113 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
3115 /* A 'structor should already have clones. */
3116 gcc_assert (decl
== error_mark_node
3117 || variable_template_p (tmpl
)
3118 || !(DECL_CONSTRUCTOR_P (decl
)
3119 || DECL_DESTRUCTOR_P (decl
))
3120 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3127 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3128 parameters. These are represented in the same format used for
3129 DECL_TEMPLATE_PARMS. */
3132 comp_template_parms (const_tree parms1
, const_tree parms2
)
3137 if (parms1
== parms2
)
3140 for (p1
= parms1
, p2
= parms2
;
3141 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3142 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3144 tree t1
= TREE_VALUE (p1
);
3145 tree t2
= TREE_VALUE (p2
);
3148 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3149 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3151 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3154 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3156 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3157 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3159 /* If either of the template parameters are invalid, assume
3160 they match for the sake of error recovery. */
3161 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3164 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3167 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3168 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3169 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3171 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3176 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3177 /* One set of parameters has more parameters lists than the
3184 /* Determine whether PARM is a parameter pack. */
3187 template_parameter_pack_p (const_tree parm
)
3189 /* Determine if we have a non-type template parameter pack. */
3190 if (TREE_CODE (parm
) == PARM_DECL
)
3191 return (DECL_TEMPLATE_PARM_P (parm
)
3192 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3193 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3194 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3196 /* If this is a list of template parameters, we could get a
3197 TYPE_DECL or a TEMPLATE_DECL. */
3198 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3199 parm
= TREE_TYPE (parm
);
3201 /* Otherwise it must be a type template parameter. */
3202 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3203 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3204 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3207 /* Determine if T is a function parameter pack. */
3210 function_parameter_pack_p (const_tree t
)
3212 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3213 return DECL_PACK_P (t
);
3217 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3218 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3221 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3223 if (! primary_func_tmpl_inst
3224 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3225 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
3228 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3231 /* Return true iff the function parameter PARAM_DECL was expanded
3232 from the function parameter pack PACK. */
3235 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3237 if (DECL_ARTIFICIAL (param_decl
)
3238 || !function_parameter_pack_p (pack
))
3241 /* The parameter pack and its pack arguments have the same
3243 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3246 /* Determine whether ARGS describes a variadic template args list,
3247 i.e., one that is terminated by a template argument pack. */
3250 template_args_variadic_p (tree args
)
3255 if (args
== NULL_TREE
)
3258 args
= INNERMOST_TEMPLATE_ARGS (args
);
3259 nargs
= TREE_VEC_LENGTH (args
);
3264 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3266 return ARGUMENT_PACK_P (last_parm
);
3269 /* Generate a new name for the parameter pack name NAME (an
3270 IDENTIFIER_NODE) that incorporates its */
3273 make_ith_pack_parameter_name (tree name
, int i
)
3275 /* Munge the name to include the parameter index. */
3276 #define NUMBUF_LEN 128
3277 char numbuf
[NUMBUF_LEN
];
3281 if (name
== NULL_TREE
)
3283 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3284 newname_len
= IDENTIFIER_LENGTH (name
)
3285 + strlen (numbuf
) + 2;
3286 newname
= (char*)alloca (newname_len
);
3287 snprintf (newname
, newname_len
,
3288 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3289 return get_identifier (newname
);
3292 /* Return true if T is a primary function, class or alias template
3296 primary_template_instantiation_p (const_tree t
)
3301 if (TREE_CODE (t
) == FUNCTION_DECL
)
3302 return DECL_LANG_SPECIFIC (t
)
3303 && DECL_TEMPLATE_INSTANTIATION (t
)
3304 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
3305 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3306 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
3307 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
3308 else if (alias_template_specialization_p (t
))
3313 /* Return true if PARM is a template template parameter. */
3316 template_template_parameter_p (const_tree parm
)
3318 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3321 /* Return true iff PARM is a DECL representing a type template
3325 template_type_parameter_p (const_tree parm
)
3328 && (TREE_CODE (parm
) == TYPE_DECL
3329 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3330 && DECL_TEMPLATE_PARM_P (parm
));
3333 /* Return the template parameters of T if T is a
3334 primary template instantiation, NULL otherwise. */
3337 get_primary_template_innermost_parameters (const_tree t
)
3339 tree parms
= NULL
, template_info
= NULL
;
3341 if ((template_info
= get_template_info (t
))
3342 && primary_template_instantiation_p (t
))
3343 parms
= INNERMOST_TEMPLATE_PARMS
3344 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3349 /* Return the template parameters of the LEVELth level from the full list
3350 of template parameters PARMS. */
3353 get_template_parms_at_level (tree parms
, int level
)
3357 || TREE_CODE (parms
) != TREE_LIST
3358 || level
> TMPL_PARMS_DEPTH (parms
))
3361 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3362 if (TMPL_PARMS_DEPTH (p
) == level
)
3368 /* Returns the template arguments of T if T is a template instantiation,
3372 get_template_innermost_arguments (const_tree t
)
3374 tree args
= NULL
, template_info
= NULL
;
3376 if ((template_info
= get_template_info (t
))
3377 && TI_ARGS (template_info
))
3378 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3383 /* Return the argument pack elements of T if T is a template argument pack,
3387 get_template_argument_pack_elems (const_tree t
)
3389 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3390 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3393 return ARGUMENT_PACK_ARGS (t
);
3396 /* Structure used to track the progress of find_parameter_packs_r. */
3397 struct find_parameter_pack_data
3399 /* TREE_LIST that will contain all of the parameter packs found by
3401 tree
* parameter_packs
;
3403 /* Set of AST nodes that have been visited by the traversal. */
3404 hash_set
<tree
> *visited
;
3407 /* Identifies all of the argument packs that occur in a template
3408 argument and appends them to the TREE_LIST inside DATA, which is a
3409 find_parameter_pack_data structure. This is a subroutine of
3410 make_pack_expansion and uses_parameter_packs. */
3412 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3415 struct find_parameter_pack_data
* ppd
=
3416 (struct find_parameter_pack_data
*)data
;
3417 bool parameter_pack_p
= false;
3419 /* Handle type aliases/typedefs. */
3420 if (TYPE_ALIAS_P (t
))
3422 if (TYPE_TEMPLATE_INFO (t
))
3423 cp_walk_tree (&TYPE_TI_ARGS (t
),
3424 &find_parameter_packs_r
,
3430 /* Identify whether this is a parameter pack or not. */
3431 switch (TREE_CODE (t
))
3433 case TEMPLATE_PARM_INDEX
:
3434 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3435 parameter_pack_p
= true;
3438 case TEMPLATE_TYPE_PARM
:
3439 t
= TYPE_MAIN_VARIANT (t
);
3440 case TEMPLATE_TEMPLATE_PARM
:
3441 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3442 parameter_pack_p
= true;
3447 if (DECL_PACK_P (t
))
3449 /* We don't want to walk into the type of a PARM_DECL,
3450 because we don't want to see the type parameter pack. */
3452 parameter_pack_p
= true;
3456 /* Look through a lambda capture proxy to the field pack. */
3458 if (DECL_HAS_VALUE_EXPR_P (t
))
3460 tree v
= DECL_VALUE_EXPR (t
);
3462 &find_parameter_packs_r
,
3469 parameter_pack_p
= true;
3472 /* Not a parameter pack. */
3476 if (parameter_pack_p
)
3478 /* Add this parameter pack to the list. */
3479 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3483 cp_walk_tree (&TYPE_CONTEXT (t
),
3484 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3486 /* This switch statement will return immediately if we don't find a
3488 switch (TREE_CODE (t
))
3490 case TEMPLATE_PARM_INDEX
:
3493 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3494 /* Check the template itself. */
3495 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3496 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3497 /* Check the template arguments. */
3498 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3503 case TEMPLATE_TYPE_PARM
:
3504 case TEMPLATE_TEMPLATE_PARM
:
3511 if (TYPE_PTRMEMFUNC_P (t
))
3517 if (TYPE_TEMPLATE_INFO (t
))
3518 cp_walk_tree (&TYPE_TI_ARGS (t
),
3519 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3526 cp_walk_tree (&TREE_TYPE (t
),
3527 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3531 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3536 case TYPE_PACK_EXPANSION
:
3537 case EXPR_PACK_EXPANSION
:
3542 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3547 case IDENTIFIER_NODE
:
3548 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3560 /* Determines if the expression or type T uses any parameter packs. */
3562 uses_parameter_packs (tree t
)
3564 tree parameter_packs
= NULL_TREE
;
3565 struct find_parameter_pack_data ppd
;
3566 ppd
.parameter_packs
= ¶meter_packs
;
3567 ppd
.visited
= new hash_set
<tree
>;
3568 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3570 return parameter_packs
!= NULL_TREE
;
3573 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3574 representation a base-class initializer into a parameter pack
3575 expansion. If all goes well, the resulting node will be an
3576 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3579 make_pack_expansion (tree arg
)
3582 tree parameter_packs
= NULL_TREE
;
3583 bool for_types
= false;
3584 struct find_parameter_pack_data ppd
;
3586 if (!arg
|| arg
== error_mark_node
)
3589 if (TREE_CODE (arg
) == TREE_LIST
)
3591 /* The only time we will see a TREE_LIST here is for a base
3592 class initializer. In this case, the TREE_PURPOSE will be a
3593 _TYPE node (representing the base class expansion we're
3594 initializing) and the TREE_VALUE will be a TREE_LIST
3595 containing the initialization arguments.
3597 The resulting expansion looks somewhat different from most
3598 expansions. Rather than returning just one _EXPANSION, we
3599 return a TREE_LIST whose TREE_PURPOSE is a
3600 TYPE_PACK_EXPANSION containing the bases that will be
3601 initialized. The TREE_VALUE will be identical to the
3602 original TREE_VALUE, which is a list of arguments that will
3603 be passed to each base. We do not introduce any new pack
3604 expansion nodes into the TREE_VALUE (although it is possible
3605 that some already exist), because the TREE_PURPOSE and
3606 TREE_VALUE all need to be expanded together with the same
3607 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3608 resulting TREE_PURPOSE will mention the parameter packs in
3609 both the bases and the arguments to the bases. */
3612 tree parameter_packs
= NULL_TREE
;
3614 /* Determine which parameter packs will be used by the base
3616 ppd
.visited
= new hash_set
<tree
>;
3617 ppd
.parameter_packs
= ¶meter_packs
;
3618 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3621 if (parameter_packs
== NULL_TREE
)
3623 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3625 return error_mark_node
;
3628 if (TREE_VALUE (arg
) != void_type_node
)
3630 /* Collect the sets of parameter packs used in each of the
3631 initialization arguments. */
3632 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3634 /* Determine which parameter packs will be expanded in this
3636 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3643 /* Create the pack expansion type for the base type. */
3644 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3645 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3646 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3648 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3649 they will rarely be compared to anything. */
3650 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3652 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3655 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3658 /* Build the PACK_EXPANSION_* node. */
3660 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3661 : make_node (EXPR_PACK_EXPANSION
);
3662 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3663 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3665 /* Propagate type and const-expression information. */
3666 TREE_TYPE (result
) = TREE_TYPE (arg
);
3667 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3670 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3671 they will rarely be compared to anything. */
3672 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3674 /* Determine which parameter packs will be expanded. */
3675 ppd
.parameter_packs
= ¶meter_packs
;
3676 ppd
.visited
= new hash_set
<tree
>;
3677 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3680 /* Make sure we found some parameter packs. */
3681 if (parameter_packs
== NULL_TREE
)
3684 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3686 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3687 return error_mark_node
;
3689 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3691 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3696 /* Checks T for any "bare" parameter packs, which have not yet been
3697 expanded, and issues an error if any are found. This operation can
3698 only be done on full expressions or types (e.g., an expression
3699 statement, "if" condition, etc.), because we could have expressions like:
3701 foo(f(g(h(args)))...)
3703 where "args" is a parameter pack. check_for_bare_parameter_packs
3704 should not be called for the subexpressions args, h(args),
3705 g(h(args)), or f(g(h(args))), because we would produce erroneous
3708 Returns TRUE and emits an error if there were bare parameter packs,
3709 returns FALSE otherwise. */
3711 check_for_bare_parameter_packs (tree t
)
3713 tree parameter_packs
= NULL_TREE
;
3714 struct find_parameter_pack_data ppd
;
3716 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3719 if (TREE_CODE (t
) == TYPE_DECL
)
3722 ppd
.parameter_packs
= ¶meter_packs
;
3723 ppd
.visited
= new hash_set
<tree
>;
3724 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3727 if (parameter_packs
)
3729 error ("parameter packs not expanded with %<...%>:");
3730 while (parameter_packs
)
3732 tree pack
= TREE_VALUE (parameter_packs
);
3733 tree name
= NULL_TREE
;
3735 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3736 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3737 name
= TYPE_NAME (pack
);
3738 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3739 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3741 name
= DECL_NAME (pack
);
3744 inform (input_location
, " %qD", name
);
3746 inform (input_location
, " <anonymous>");
3748 parameter_packs
= TREE_CHAIN (parameter_packs
);
3757 /* Expand any parameter packs that occur in the template arguments in
3760 expand_template_argument_pack (tree args
)
3762 tree result_args
= NULL_TREE
;
3763 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3764 int num_result_args
= -1;
3765 int non_default_args_count
= -1;
3767 /* First, determine if we need to expand anything, and the number of
3768 slots we'll need. */
3769 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3771 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3772 if (arg
== NULL_TREE
)
3774 if (ARGUMENT_PACK_P (arg
))
3776 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3777 if (num_result_args
< 0)
3778 num_result_args
= in_arg
+ num_packed
;
3780 num_result_args
+= num_packed
;
3784 if (num_result_args
>= 0)
3789 /* If no expansion is necessary, we're done. */
3790 if (num_result_args
< 0)
3793 /* Expand arguments. */
3794 result_args
= make_tree_vec (num_result_args
);
3795 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3796 non_default_args_count
=
3797 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3798 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3800 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3801 if (ARGUMENT_PACK_P (arg
))
3803 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3804 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3805 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3806 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3807 if (non_default_args_count
> 0)
3808 non_default_args_count
+= num_packed
- 1;
3812 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3816 if (non_default_args_count
>= 0)
3817 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3821 /* Checks if DECL shadows a template parameter.
3823 [temp.local]: A template-parameter shall not be redeclared within its
3824 scope (including nested scopes).
3826 Emits an error and returns TRUE if the DECL shadows a parameter,
3827 returns FALSE otherwise. */
3830 check_template_shadow (tree decl
)
3834 /* If we're not in a template, we can't possibly shadow a template
3836 if (!current_template_parms
)
3839 /* Figure out what we're shadowing. */
3840 if (TREE_CODE (decl
) == OVERLOAD
)
3841 decl
= OVL_CURRENT (decl
);
3842 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3844 /* If there's no previous binding for this name, we're not shadowing
3845 anything, let alone a template parameter. */
3849 /* If we're not shadowing a template parameter, we're done. Note
3850 that OLDDECL might be an OVERLOAD (or perhaps even an
3851 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3853 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3856 /* We check for decl != olddecl to avoid bogus errors for using a
3857 name inside a class. We check TPFI to avoid duplicate errors for
3858 inline member templates. */
3860 || (DECL_TEMPLATE_PARM_P (decl
)
3861 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
3864 /* Don't complain about the injected class name, as we've already
3865 complained about the class itself. */
3866 if (DECL_SELF_REFERENCE_P (decl
))
3869 error ("declaration of %q+#D", decl
);
3870 error (" shadows template parm %q+#D", olddecl
);
3874 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3875 ORIG_LEVEL, DECL, and TYPE. */
3878 build_template_parm_index (int index
,
3884 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3885 TEMPLATE_PARM_IDX (t
) = index
;
3886 TEMPLATE_PARM_LEVEL (t
) = level
;
3887 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3888 TEMPLATE_PARM_DECL (t
) = decl
;
3889 TREE_TYPE (t
) = type
;
3890 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3891 TREE_READONLY (t
) = TREE_READONLY (decl
);
3896 /* Find the canonical type parameter for the given template type
3897 parameter. Returns the canonical type parameter, which may be TYPE
3898 if no such parameter existed. */
3901 canonical_type_parameter (tree type
)
3904 int idx
= TEMPLATE_TYPE_IDX (type
);
3905 if (!canonical_template_parms
)
3906 vec_alloc (canonical_template_parms
, idx
+1);
3908 while (canonical_template_parms
->length () <= (unsigned)idx
)
3909 vec_safe_push (canonical_template_parms
, NULL_TREE
);
3911 list
= (*canonical_template_parms
)[idx
];
3912 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3913 list
= TREE_CHAIN (list
);
3916 return TREE_VALUE (list
);
3919 (*canonical_template_parms
)[idx
]
3920 = tree_cons (NULL_TREE
, type
,
3921 (*canonical_template_parms
)[idx
]);
3926 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3927 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3928 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3929 new one is created. */
3932 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3933 tsubst_flags_t complain
)
3935 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3936 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3937 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3938 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3940 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3943 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3944 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3945 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3946 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3947 DECL_ARTIFICIAL (decl
) = 1;
3948 SET_DECL_TEMPLATE_PARM_P (decl
);
3950 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3951 TEMPLATE_PARM_LEVEL (index
) - levels
,
3952 TEMPLATE_PARM_ORIG_LEVEL (index
),
3954 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3955 TEMPLATE_PARM_PARAMETER_PACK (t
)
3956 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3958 /* Template template parameters need this. */
3959 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3960 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3961 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3965 return TEMPLATE_PARM_DESCENDANTS (index
);
3968 /* Process information from new template parameter PARM and append it
3969 to the LIST being built. This new parameter is a non-type
3970 parameter iff IS_NON_TYPE is true. This new parameter is a
3971 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3975 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
3976 bool is_non_type
, bool is_parameter_pack
)
3981 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3982 tree defval
= TREE_PURPOSE (parm
);
3983 tree constr
= TREE_TYPE (parm
);
3984 tree reqs
= NULL_TREE
;
3988 tree p
= tree_last (list
);
3990 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3993 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3994 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3996 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4004 parm
= TREE_VALUE (parm
);
4006 SET_DECL_TEMPLATE_PARM_P (parm
);
4008 if (TREE_TYPE (parm
) != error_mark_node
)
4012 The top-level cv-qualifiers on the template-parameter are
4013 ignored when determining its type. */
4014 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4015 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4016 TREE_TYPE (parm
) = error_mark_node
;
4017 else if (uses_parameter_packs (TREE_TYPE (parm
))
4018 && !is_parameter_pack
4019 /* If we're in a nested template parameter list, the template
4020 template parameter could be a parameter pack. */
4021 && processing_template_parmlist
== 1)
4023 /* This template parameter is not a parameter pack, but it
4024 should be. Complain about "bare" parameter packs. */
4025 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4027 /* Recover by calling this a parameter pack. */
4028 is_parameter_pack
= true;
4032 /* A template parameter is not modifiable. */
4033 TREE_CONSTANT (parm
) = 1;
4034 TREE_READONLY (parm
) = 1;
4035 decl
= build_decl (parm_loc
,
4036 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4037 TREE_CONSTANT (decl
) = 1;
4038 TREE_READONLY (decl
) = 1;
4039 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4040 = build_template_parm_index (idx
, processing_template_decl
,
4041 processing_template_decl
,
4042 decl
, TREE_TYPE (parm
));
4044 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4045 = is_parameter_pack
;
4050 parm
= TREE_VALUE (TREE_VALUE (parm
));
4052 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4054 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4055 /* This is for distinguishing between real templates and template
4056 template parameters */
4057 TREE_TYPE (parm
) = t
;
4058 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4063 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4064 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4065 decl
= build_decl (parm_loc
,
4066 TYPE_DECL
, parm
, t
);
4069 TYPE_NAME (t
) = decl
;
4070 TYPE_STUB_DECL (t
) = decl
;
4072 TEMPLATE_TYPE_PARM_INDEX (t
)
4073 = build_template_parm_index (idx
, processing_template_decl
,
4074 processing_template_decl
,
4075 decl
, TREE_TYPE (parm
));
4076 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4077 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4079 DECL_ARTIFICIAL (decl
) = 1;
4080 SET_DECL_TEMPLATE_PARM_P (decl
);
4082 // Build requirements for the type/template parameter. This must be done
4083 // after SET_DECL_TEMPLATE_PARM_P or process_template_parm could fail.
4084 reqs
= finish_shorthand_constraint (parm
, constr
);
4088 // Build the parameter node linking the parameter declaration, its
4089 // default argument (if any), and its constraints (if any).
4090 parm
= build_tree_list (defval
, parm
);
4091 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4093 return chainon (list
, parm
);
4096 /* The end of a template parameter list has been reached. Process the
4097 tree list into a parameter vector, converting each parameter into a more
4098 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4102 end_template_parm_list (tree parms
)
4106 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4108 current_template_parms
4109 = tree_cons (size_int (processing_template_decl
),
4110 saved_parmlist
, current_template_parms
);
4112 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4114 next
= TREE_CHAIN (parm
);
4115 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4116 TREE_CHAIN (parm
) = NULL_TREE
;
4119 --processing_template_parmlist
;
4121 return saved_parmlist
;
4124 // Explicitly indicate the end of the template parameter list. We assume
4125 // that the current template parameters have been constructed and/or
4126 // managed explicitly, as when creating new template template parameters
4127 // from a shorthand constraint.
4129 end_template_parm_list ()
4131 --processing_template_parmlist
;
4134 /* end_template_decl is called after a template declaration is seen. */
4137 end_template_decl (void)
4139 reset_specialization ();
4141 if (! processing_template_decl
)
4144 /* This matches the pushlevel in begin_template_parm_list. */
4147 --processing_template_decl
;
4148 current_template_parms
= TREE_CHAIN (current_template_parms
);
4151 /* Takes a TREE_LIST representing a template parameter and convert it
4152 into an argument suitable to be passed to the type substitution
4153 functions. Note that If the TREE_LIST contains an error_mark
4154 node, the returned argument is error_mark_node. */
4157 template_parm_to_arg (tree t
)
4161 || TREE_CODE (t
) != TREE_LIST
)
4164 if (error_operand_p (TREE_VALUE (t
)))
4165 return error_mark_node
;
4169 if (TREE_CODE (t
) == TYPE_DECL
4170 || TREE_CODE (t
) == TEMPLATE_DECL
)
4174 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4176 /* Turn this argument into a TYPE_ARGUMENT_PACK
4177 with a single element, which expands T. */
4178 tree vec
= make_tree_vec (1);
4179 #ifdef ENABLE_CHECKING
4180 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
4181 (vec
, TREE_VEC_LENGTH (vec
));
4183 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4185 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4186 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4191 t
= DECL_INITIAL (t
);
4193 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4195 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4196 with a single element, which expands T. */
4197 tree vec
= make_tree_vec (1);
4198 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
4199 #ifdef ENABLE_CHECKING
4200 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
4201 (vec
, TREE_VEC_LENGTH (vec
));
4203 t
= convert_from_reference (t
);
4204 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4206 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4207 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4208 TREE_TYPE (t
) = type
;
4211 t
= convert_from_reference (t
);
4216 /* Given a set of template parameters, return them as a set of template
4217 arguments. The template parameters are represented as a TREE_VEC, in
4218 the form documented in cp-tree.h for template arguments. */
4221 template_parms_to_args (tree parms
)
4224 tree args
= NULL_TREE
;
4225 int length
= TMPL_PARMS_DEPTH (parms
);
4228 /* If there is only one level of template parameters, we do not
4229 create a TREE_VEC of TREE_VECs. Instead, we return a single
4230 TREE_VEC containing the arguments. */
4232 args
= make_tree_vec (length
);
4234 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4236 tree a
= copy_node (TREE_VALUE (header
));
4239 TREE_TYPE (a
) = NULL_TREE
;
4240 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4241 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4243 #ifdef ENABLE_CHECKING
4244 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4248 TREE_VEC_ELT (args
, --l
) = a
;
4253 if (length
> 1 && TREE_VEC_ELT (args
, 0) == NULL_TREE
)
4254 /* This can happen for template parms of a template template
4257 template<template<class T, class U> class TT> struct S;
4259 Consider the level of the parms of TT; T and U both have
4260 level 2; TT has no template parm of level 1. So in this case
4261 the first element of full_template_args is NULL_TREE. If we
4262 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
4263 of 2. This will make tsubst wrongly consider that T and U
4264 have level 1. Instead, let's create a dummy vector as the
4265 first element of full_template_args so that TMPL_ARGS_DEPTH
4266 returns the correct depth for args. */
4267 TREE_VEC_ELT (args
, 0) = make_tree_vec (1);
4271 /* Within the declaration of a template, return the currently active
4272 template parameters as an argument TREE_VEC. */
4275 current_template_args (void)
4277 return template_parms_to_args (current_template_parms
);
4280 /* Update the declared TYPE by doing any lookups which were thought to be
4281 dependent, but are not now that we know the SCOPE of the declarator. */
4284 maybe_update_decl_type (tree orig_type
, tree scope
)
4286 tree type
= orig_type
;
4288 if (type
== NULL_TREE
)
4291 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4292 type
= TREE_TYPE (type
);
4294 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4295 && dependent_type_p (type
)
4296 /* Don't bother building up the args in this case. */
4297 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4299 /* tsubst in the args corresponding to the template parameters,
4300 including auto if present. Most things will be unchanged, but
4301 make_typename_type and tsubst_qualified_id will resolve
4302 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4303 tree args
= current_template_args ();
4304 tree auto_node
= type_uses_auto (type
);
4308 tree auto_vec
= make_tree_vec (1);
4309 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4310 args
= add_to_template_args (args
, auto_vec
);
4312 pushed
= push_scope (scope
);
4313 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4318 if (type
== error_mark_node
)
4321 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4323 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4326 type
= TYPE_NAME (type
);
4331 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4332 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4333 the new template is a member template. */
4336 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4338 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4339 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4340 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4341 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4342 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4347 struct template_parm_data
4349 /* The level of the template parameters we are currently
4353 /* The index of the specialization argument we are currently
4357 /* An array whose size is the number of template parameters. The
4358 elements are nonzero if the parameter has been used in any one
4359 of the arguments processed so far. */
4362 /* An array whose size is the number of template arguments. The
4363 elements are nonzero if the argument makes use of template
4364 parameters of this level. */
4365 int* arg_uses_template_parms
;
4368 /* Subroutine of push_template_decl used to see if each template
4369 parameter in a partial specialization is used in the explicit
4370 argument list. If T is of the LEVEL given in DATA (which is
4371 treated as a template_parm_data*), then DATA->PARMS is marked
4375 mark_template_parm (tree t
, void* data
)
4379 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4381 template_parm_level_and_index (t
, &level
, &idx
);
4383 if (level
== tpd
->level
)
4385 tpd
->parms
[idx
] = 1;
4386 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4389 /* Return zero so that for_each_template_parm will continue the
4390 traversal of the tree; we want to mark *every* template parm. */
4394 /* Process the partial specialization DECL. */
4397 process_partial_specialization (tree decl
)
4399 tree type
= TREE_TYPE (decl
);
4400 tree tinfo
= get_template_info (decl
);
4401 tree maintmpl
= TI_TEMPLATE (tinfo
);
4402 tree specargs
= TI_ARGS (tinfo
);
4403 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4404 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4407 int nargs
= TREE_VEC_LENGTH (inner_args
);
4410 bool did_error_intro
= false;
4411 struct template_parm_data tpd
;
4412 struct template_parm_data tpd2
;
4414 gcc_assert (current_template_parms
);
4416 /* A concept cannot be specialized. */
4417 if (flag_concepts
&& variable_concept_p (maintmpl
))
4419 error ("specialization of variable concept %q#D", maintmpl
);
4420 return error_mark_node
;
4423 /* When defining a constrained partial specialization, DECL's type
4424 will have been assigned to the canonical type of the primary.
4427 template<typename T>
4428 struct S; // Has canonical type S<T>
4431 struct S<T>; // Binds to the primary template.
4433 However, the constraints differ, so we should be constructing
4434 a new type instead of simply referring to the old one. We
4435 do this by erasing the canonical type of the partial
4436 specialization and always comparing these types structurally.
4438 TODO: Do we need to compare the current requirements to make
4439 sure this isn't a redeclaration? */
4440 if (TEMPLATE_PARM_CONSTRAINTS (current_template_parms
))
4441 SET_TYPE_STRUCTURAL_EQUALITY (type
);
4443 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4444 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4446 /* We check that each of the template parameters given in the
4447 partial specialization is used in the argument list to the
4448 specialization. For example:
4450 template <class T> struct S;
4451 template <class T> struct S<T*>;
4453 The second declaration is OK because `T*' uses the template
4454 parameter T, whereas
4456 template <class T> struct S<int>;
4458 is no good. Even trickier is:
4469 The S2<T> declaration is actually invalid; it is a
4470 full-specialization. Of course,
4473 struct S2<T (*)(U)>;
4475 or some such would have been OK. */
4476 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4477 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4478 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4480 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4481 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4482 for (i
= 0; i
< nargs
; ++i
)
4484 tpd
.current_arg
= i
;
4485 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4486 &mark_template_parm
,
4489 /*include_nondeduced_p=*/false);
4491 for (i
= 0; i
< ntparms
; ++i
)
4492 if (tpd
.parms
[i
] == 0)
4494 /* One of the template parms was not used in a deduced context in the
4496 if (!did_error_intro
)
4498 error ("template parameters not deducible in "
4499 "partial specialization:");
4500 did_error_intro
= true;
4503 inform (input_location
, " %qD",
4504 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4507 if (did_error_intro
)
4508 return error_mark_node
;
4510 /* Build the associated constraints now so we can compare
4511 and assign them below. */
4515 tree reqs
= TEMPLATE_PARM_CONSTRAINTS (current_template_parms
);
4516 tmpl_constr
= build_constraints (reqs
, NULL_TREE
);
4519 tmpl_constr
= NULL_TREE
;
4521 /* [temp.class.spec]
4523 The argument list of the specialization shall not be identical to
4524 the implicit argument list of the primary template. */
4526 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
4527 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
)))
4530 error ("partial specialization %qD does not specialize "
4531 "any template arguments", decl
);
4534 /* If the arguments compare equal but are not not more
4535 constrained, then this will not be more specialized. */
4536 if (!subsumes_constraints (tmpl_constr
, get_constraints (maintmpl
)))
4538 error ("partial specialization is not more specialized than the "
4539 "primary template because it is not more constrained");
4540 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4546 /* A partial specialization that replaces multiple parameters of the
4547 primary template with a pack expansion is less specialized for those
4549 if (nargs
< DECL_NTPARMS (maintmpl
))
4551 error ("partial specialization is not more specialized than the "
4552 "primary template because it replaces multiple parameters "
4553 "with a pack expansion");
4554 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4558 /* [temp.class.spec]
4560 A partially specialized non-type argument expression shall not
4561 involve template parameters of the partial specialization except
4562 when the argument expression is a simple identifier.
4564 The type of a template parameter corresponding to a specialized
4565 non-type argument shall not be dependent on a parameter of the
4568 Also, we verify that pack expansions only occur at the
4569 end of the argument list. */
4570 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4572 for (i
= 0; i
< nargs
; ++i
)
4574 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4575 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4576 tree packed_args
= NULL_TREE
;
4579 if (ARGUMENT_PACK_P (arg
))
4581 /* Extract the arguments from the argument pack. We'll be
4582 iterating over these in the following loop. */
4583 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4584 len
= TREE_VEC_LENGTH (packed_args
);
4587 for (j
= 0; j
< len
; j
++)
4590 /* Get the Jth argument in the parameter pack. */
4591 arg
= TREE_VEC_ELT (packed_args
, j
);
4593 if (PACK_EXPANSION_P (arg
))
4595 /* Pack expansions must come at the end of the
4597 if ((packed_args
&& j
< len
- 1)
4598 || (!packed_args
&& i
< nargs
- 1))
4600 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4601 error ("parameter pack argument %qE must be at the "
4602 "end of the template argument list", arg
);
4604 error ("parameter pack argument %qT must be at the "
4605 "end of the template argument list", arg
);
4609 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4610 /* We only care about the pattern. */
4611 arg
= PACK_EXPANSION_PATTERN (arg
);
4613 if (/* These first two lines are the `non-type' bit. */
4615 && TREE_CODE (arg
) != TEMPLATE_DECL
4616 /* This next two lines are the `argument expression is not just a
4617 simple identifier' condition and also the `specialized
4618 non-type argument' bit. */
4619 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4620 && !(REFERENCE_REF_P (arg
)
4621 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4623 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4624 || (packed_args
&& uses_template_parms (arg
)))
4625 error ("template argument %qE involves template parameter(s)",
4629 /* Look at the corresponding template parameter,
4630 marking which template parameters its type depends
4632 tree type
= TREE_TYPE (parm
);
4636 /* We haven't yet initialized TPD2. Do so now. */
4637 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4638 /* The number of parameters here is the number in the
4639 main template, which, as checked in the assertion
4641 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4643 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4646 /* Mark the template parameters. But this time, we're
4647 looking for the template parameters of the main
4648 template, not in the specialization. */
4649 tpd2
.current_arg
= i
;
4650 tpd2
.arg_uses_template_parms
[i
] = 0;
4651 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4652 for_each_template_parm (type
,
4653 &mark_template_parm
,
4656 /*include_nondeduced_p=*/false);
4658 if (tpd2
.arg_uses_template_parms
[i
])
4660 /* The type depended on some template parameters.
4661 If they are fully specialized in the
4662 specialization, that's OK. */
4665 for (j
= 0; j
< nargs
; ++j
)
4666 if (tpd2
.parms
[j
] != 0
4667 && tpd
.arg_uses_template_parms
[j
])
4670 error_n (input_location
, count
,
4671 "type %qT of template argument %qE depends "
4672 "on a template parameter",
4673 "type %qT of template argument %qE depends "
4674 "on template parameters",
4683 /* We should only get here once. */
4684 if (TREE_CODE (decl
) == TYPE_DECL
)
4685 gcc_assert (!COMPLETE_TYPE_P (type
));
4687 // Build the template decl.
4688 tree tmpl
= build_template_decl (decl
,
4689 current_template_parms
,
4690 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4691 TREE_TYPE (tmpl
) = type
;
4692 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4693 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4694 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4695 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4696 set_constraints (tmpl
, tmpl_constr
);
4698 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4699 = tree_cons (specargs
, tmpl
,
4700 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4701 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4703 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4704 inst
= TREE_CHAIN (inst
))
4706 tree instance
= TREE_VALUE (inst
);
4707 if (TYPE_P (instance
)
4708 ? (COMPLETE_TYPE_P (instance
)
4709 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
4710 : DECL_TEMPLATE_INSTANTIATION (instance
))
4712 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
4713 if (spec
&& TREE_VALUE (spec
) == tmpl
)
4715 tree inst_decl
= (DECL_P (instance
)
4716 ? instance
: TYPE_NAME (instance
));
4717 permerror (input_location
,
4718 "partial specialization of %qD after instantiation "
4719 "of %qD", decl
, inst_decl
);
4727 /* PARM is a template parameter of some form; return the corresponding
4728 TEMPLATE_PARM_INDEX. */
4731 get_template_parm_index (tree parm
)
4733 if (TREE_CODE (parm
) == PARM_DECL
4734 || TREE_CODE (parm
) == CONST_DECL
)
4735 parm
= DECL_INITIAL (parm
);
4736 else if (TREE_CODE (parm
) == TYPE_DECL
4737 || TREE_CODE (parm
) == TEMPLATE_DECL
)
4738 parm
= TREE_TYPE (parm
);
4739 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
4740 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
4741 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
4742 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
4746 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4747 parameter packs used by the template parameter PARM. */
4750 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
4752 /* A type parm can't refer to another parm. */
4753 if (TREE_CODE (parm
) == TYPE_DECL
)
4755 else if (TREE_CODE (parm
) == PARM_DECL
)
4757 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
4762 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
4764 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
4765 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
4766 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
4769 /* PARM is a template parameter pack. Return any parameter packs used in
4770 its type or the type of any of its template parameters. If there are
4771 any such packs, it will be instantiated into a fixed template parameter
4772 list by partial instantiation rather than be fully deduced. */
4775 fixed_parameter_pack_p (tree parm
)
4777 /* This can only be true in a member template. */
4778 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
4780 /* This can only be true for a parameter pack. */
4781 if (!template_parameter_pack_p (parm
))
4783 /* A type parm can't refer to another parm. */
4784 if (TREE_CODE (parm
) == TYPE_DECL
)
4787 tree parameter_packs
= NULL_TREE
;
4788 struct find_parameter_pack_data ppd
;
4789 ppd
.parameter_packs
= ¶meter_packs
;
4790 ppd
.visited
= new hash_set
<tree
>;
4792 fixed_parameter_pack_p_1 (parm
, &ppd
);
4795 return parameter_packs
;
4798 /* Check that a template declaration's use of default arguments and
4799 parameter packs is not invalid. Here, PARMS are the template
4800 parameters. IS_PRIMARY is true if DECL is the thing declared by
4801 a primary template. IS_PARTIAL is true if DECL is a partial
4804 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4805 declaration (but not a definition); 1 indicates a declaration, 2
4806 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4807 emitted for extraneous default arguments.
4809 Returns TRUE if there were no errors found, FALSE otherwise. */
4812 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4813 bool is_partial
, int is_friend_decl
)
4816 int last_level_to_check
;
4818 bool no_errors
= true;
4822 A default template-argument shall not be specified in a
4823 function template declaration or a function template definition, nor
4824 in the template-parameter-list of the definition of a member of a
4827 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
4828 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
4829 /* You can't have a function template declaration in a local
4830 scope, nor you can you define a member of a class template in a
4834 if ((TREE_CODE (decl
) == TYPE_DECL
4836 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4837 || (TREE_CODE (decl
) == FUNCTION_DECL
4838 && LAMBDA_FUNCTION_P (decl
)))
4839 /* A lambda doesn't have an explicit declaration; don't complain
4840 about the parms of the enclosing class. */
4843 if (current_class_type
4844 && !TYPE_BEING_DEFINED (current_class_type
)
4845 && DECL_LANG_SPECIFIC (decl
)
4846 && DECL_DECLARES_FUNCTION_P (decl
)
4847 /* If this is either a friend defined in the scope of the class
4848 or a member function. */
4849 && (DECL_FUNCTION_MEMBER_P (decl
)
4850 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4851 : DECL_FRIEND_CONTEXT (decl
)
4852 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4854 /* And, if it was a member function, it really was defined in
4855 the scope of the class. */
4856 && (!DECL_FUNCTION_MEMBER_P (decl
)
4857 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4858 /* We already checked these parameters when the template was
4859 declared, so there's no need to do it again now. This function
4860 was defined in class scope, but we're processing its body now
4861 that the class is complete. */
4864 /* Core issue 226 (C++0x only): the following only applies to class
4867 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
4871 If a template-parameter has a default template-argument, all
4872 subsequent template-parameters shall have a default
4873 template-argument supplied. */
4874 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4876 tree inner_parms
= TREE_VALUE (parm_level
);
4877 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4878 int seen_def_arg_p
= 0;
4881 for (i
= 0; i
< ntparms
; ++i
)
4883 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4885 if (parm
== error_mark_node
)
4888 if (TREE_PURPOSE (parm
))
4890 else if (seen_def_arg_p
4891 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4893 error ("no default argument for %qD", TREE_VALUE (parm
));
4894 /* For better subsequent error-recovery, we indicate that
4895 there should have been a default argument. */
4896 TREE_PURPOSE (parm
) = error_mark_node
;
4899 else if (!is_partial
4901 /* Don't complain about an enclosing partial
4903 && parm_level
== parms
4904 && TREE_CODE (decl
) == TYPE_DECL
4906 && template_parameter_pack_p (TREE_VALUE (parm
))
4907 /* A fixed parameter pack will be partially
4908 instantiated into a fixed length list. */
4909 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
4911 /* A primary class template can only have one
4912 parameter pack, at the end of the template
4915 error ("parameter pack %q+D must be at the end of the"
4916 " template parameter list", TREE_VALUE (parm
));
4918 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4926 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4930 /* For an ordinary class template, default template arguments are
4931 allowed at the innermost level, e.g.:
4932 template <class T = int>
4934 but, in a partial specialization, they're not allowed even
4935 there, as we have in [temp.class.spec]:
4937 The template parameter list of a specialization shall not
4938 contain default template argument values.
4940 So, for a partial specialization, or for a function template
4941 (in C++98/C++03), we look at all of them. */
4944 /* But, for a primary class template that is not a partial
4945 specialization we look at all template parameters except the
4947 parms
= TREE_CHAIN (parms
);
4949 /* Figure out what error message to issue. */
4950 if (is_friend_decl
== 2)
4951 msg
= G_("default template arguments may not be used in function template "
4952 "friend re-declaration");
4953 else if (is_friend_decl
)
4954 msg
= G_("default template arguments may not be used in function template "
4955 "friend declarations");
4956 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4957 msg
= G_("default template arguments may not be used in function templates "
4958 "without -std=c++11 or -std=gnu++11");
4959 else if (is_partial
)
4960 msg
= G_("default template arguments may not be used in "
4961 "partial specializations");
4963 msg
= G_("default argument for template parameter for class enclosing %qD");
4965 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4966 /* If we're inside a class definition, there's no need to
4967 examine the parameters to the class itself. On the one
4968 hand, they will be checked when the class is defined, and,
4969 on the other, default arguments are valid in things like:
4970 template <class T = double>
4971 struct S { template <class U> void f(U); };
4972 Here the default argument for `S' has no bearing on the
4973 declaration of `f'. */
4974 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4976 /* Check everything. */
4977 last_level_to_check
= 0;
4979 for (parm_level
= parms
;
4980 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4981 parm_level
= TREE_CHAIN (parm_level
))
4983 tree inner_parms
= TREE_VALUE (parm_level
);
4987 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4988 for (i
= 0; i
< ntparms
; ++i
)
4990 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4993 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4998 if (is_friend_decl
== 2)
5005 /* Clear out the default argument so that we are not
5007 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5011 /* At this point, if we're still interested in issuing messages,
5012 they must apply to classes surrounding the object declared. */
5014 msg
= G_("default argument for template parameter for class "
5021 /* Worker for push_template_decl_real, called via
5022 for_each_template_parm. DATA is really an int, indicating the
5023 level of the parameters we are interested in. If T is a template
5024 parameter of that level, return nonzero. */
5027 template_parm_this_level_p (tree t
, void* data
)
5029 int this_level
= *(int *)data
;
5032 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5033 level
= TEMPLATE_PARM_LEVEL (t
);
5035 level
= TEMPLATE_TYPE_LEVEL (t
);
5036 return level
== this_level
;
5039 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5040 parameters given by current_template_args, or reuses a
5041 previously existing one, if appropriate. Returns the DECL, or an
5042 equivalent one, if it is replaced via a call to duplicate_decls.
5044 If IS_FRIEND is true, DECL is a friend declaration. */
5047 push_template_decl_real (tree decl
, bool is_friend
)
5055 int new_template_p
= 0;
5056 /* True if the template is a member template, in the sense of
5058 bool member_template_p
= false;
5060 if (decl
== error_mark_node
|| !current_template_parms
)
5061 return error_mark_node
;
5063 /* See if this is a partial specialization. */
5064 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5065 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5066 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5067 || (TREE_CODE (decl
) == VAR_DECL
5068 && DECL_LANG_SPECIFIC (decl
)
5069 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5070 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5072 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5076 /* For a friend, we want the context of the friend function, not
5077 the type of which it is a friend. */
5078 ctx
= CP_DECL_CONTEXT (decl
);
5079 else if (CP_DECL_CONTEXT (decl
)
5080 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5081 /* In the case of a virtual function, we want the class in which
5083 ctx
= CP_DECL_CONTEXT (decl
);
5085 /* Otherwise, if we're currently defining some class, the DECL
5086 is assumed to be a member of the class. */
5087 ctx
= current_scope ();
5089 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5092 if (!DECL_CONTEXT (decl
))
5093 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5095 /* See if this is a primary template. */
5096 if (is_friend
&& ctx
5097 && uses_template_parms_level (ctx
, processing_template_decl
))
5098 /* A friend template that specifies a class context, i.e.
5099 template <typename T> friend void A<T>::f();
5102 else if (TREE_CODE (decl
) == TYPE_DECL
5103 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5106 is_primary
= template_parm_scope_p ();
5110 if (DECL_CLASS_SCOPE_P (decl
))
5111 member_template_p
= true;
5112 if (TREE_CODE (decl
) == TYPE_DECL
5113 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
5115 error ("template class without a name");
5116 return error_mark_node
;
5118 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5120 if (member_template_p
)
5122 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5123 error ("member template %qD may not have virt-specifiers", decl
);
5125 if (DECL_DESTRUCTOR_P (decl
))
5129 A destructor shall not be a member template. */
5130 error ("destructor %qD declared as member template", decl
);
5131 return error_mark_node
;
5133 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
5134 && (!prototype_p (TREE_TYPE (decl
))
5135 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5136 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5137 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
5138 == void_list_node
)))
5140 /* [basic.stc.dynamic.allocation]
5142 An allocation function can be a function
5143 template. ... Template allocation functions shall
5144 have two or more parameters. */
5145 error ("invalid template declaration of %qD", decl
);
5146 return error_mark_node
;
5149 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5150 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5152 else if (TREE_CODE (decl
) == TYPE_DECL
5153 && TYPE_DECL_ALIAS_P (decl
))
5154 /* alias-declaration */
5155 gcc_assert (!DECL_ARTIFICIAL (decl
));
5156 else if (VAR_P (decl
))
5157 /* C++14 variable template. */;
5160 error ("template declaration of %q#D", decl
);
5161 return error_mark_node
;
5165 /* Check to see that the rules regarding the use of default
5166 arguments are not being violated. */
5167 check_default_tmpl_args (decl
, current_template_parms
,
5168 is_primary
, is_partial
, /*is_friend_decl=*/0);
5170 /* Ensure that there are no parameter packs in the type of this
5171 declaration that have not been expanded. */
5172 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5174 /* Check each of the arguments individually to see if there are
5175 any bare parameter packs. */
5176 tree type
= TREE_TYPE (decl
);
5177 tree arg
= DECL_ARGUMENTS (decl
);
5178 tree argtype
= TYPE_ARG_TYPES (type
);
5180 while (arg
&& argtype
)
5182 if (!DECL_PACK_P (arg
)
5183 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5185 /* This is a PARM_DECL that contains unexpanded parameter
5186 packs. We have already complained about this in the
5187 check_for_bare_parameter_packs call, so just replace
5188 these types with ERROR_MARK_NODE. */
5189 TREE_TYPE (arg
) = error_mark_node
;
5190 TREE_VALUE (argtype
) = error_mark_node
;
5193 arg
= DECL_CHAIN (arg
);
5194 argtype
= TREE_CHAIN (argtype
);
5197 /* Check for bare parameter packs in the return type and the
5198 exception specifiers. */
5199 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5200 /* Errors were already issued, set return type to int
5201 as the frontend doesn't expect error_mark_node as
5203 TREE_TYPE (type
) = integer_type_node
;
5204 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5205 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5207 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
5208 && TYPE_DECL_ALIAS_P (decl
))
5209 ? DECL_ORIGINAL_TYPE (decl
)
5210 : TREE_TYPE (decl
)))
5212 TREE_TYPE (decl
) = error_mark_node
;
5213 return error_mark_node
;
5217 return process_partial_specialization (decl
);
5219 args
= current_template_args ();
5222 || TREE_CODE (ctx
) == FUNCTION_DECL
5223 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5224 || (TREE_CODE (decl
) == TYPE_DECL
5225 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5226 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5228 if (DECL_LANG_SPECIFIC (decl
)
5229 && DECL_TEMPLATE_INFO (decl
)
5230 && DECL_TI_TEMPLATE (decl
))
5231 tmpl
= DECL_TI_TEMPLATE (decl
);
5232 /* If DECL is a TYPE_DECL for a class-template, then there won't
5233 be DECL_LANG_SPECIFIC. The information equivalent to
5234 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5235 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5236 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5237 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5239 /* Since a template declaration already existed for this
5240 class-type, we must be redeclaring it here. Make sure
5241 that the redeclaration is valid. */
5242 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
5243 tree constr
= build_constraints (reqs
, NULL_TREE
);
5244 redeclare_class_template (TREE_TYPE (decl
),
5245 current_template_parms
,
5247 /* We don't need to create a new TEMPLATE_DECL; just use the
5248 one we already had. */
5249 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5253 tmpl
= build_template_decl (decl
,
5254 current_template_parms
,
5258 if (DECL_LANG_SPECIFIC (decl
)
5259 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5261 /* A specialization of a member template of a template
5263 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5264 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5265 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5271 tree a
, t
, current
, parms
;
5273 tree tinfo
= get_template_info (decl
);
5277 error ("template definition of non-template %q#D", decl
);
5278 return error_mark_node
;
5281 tmpl
= TI_TEMPLATE (tinfo
);
5283 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5284 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5285 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5286 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5290 /* The declaration is a specialization of a member
5291 template, declared outside the class. Therefore, the
5292 innermost template arguments will be NULL, so we
5293 replace them with the arguments determined by the
5294 earlier call to check_explicit_specialization. */
5295 args
= DECL_TI_ARGS (decl
);
5297 new_tmpl
= build_template_decl (decl
,
5298 current_template_parms
,
5300 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5301 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5302 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5303 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5304 DECL_TEMPLATE_INFO (new_tmpl
) = build_template_info (tmpl
, args
);
5306 register_specialization (new_tmpl
,
5307 most_general_template (tmpl
),
5313 /* Make sure the template headers we got make sense. */
5315 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5316 i
= TMPL_PARMS_DEPTH (parms
);
5317 if (TMPL_ARGS_DEPTH (args
) != i
)
5319 error ("expected %d levels of template parms for %q#D, got %d",
5320 i
, decl
, TMPL_ARGS_DEPTH (args
));
5321 DECL_INTERFACE_KNOWN (decl
) = 1;
5322 return error_mark_node
;
5325 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5327 a
= TMPL_ARGS_LEVEL (args
, i
);
5328 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5330 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5332 if (current
== decl
)
5333 error ("got %d template parameters for %q#D",
5334 TREE_VEC_LENGTH (a
), decl
);
5336 error ("got %d template parameters for %q#T",
5337 TREE_VEC_LENGTH (a
), current
);
5338 error (" but %d required", TREE_VEC_LENGTH (t
));
5339 /* Avoid crash in import_export_decl. */
5340 DECL_INTERFACE_KNOWN (decl
) = 1;
5341 return error_mark_node
;
5344 if (current
== decl
)
5346 else if (current
== NULL_TREE
)
5347 /* Can happen in erroneous input. */
5350 current
= get_containing_scope (current
);
5353 /* Check that the parms are used in the appropriate qualifying scopes
5354 in the declarator. */
5355 if (!comp_template_args
5357 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5360 template arguments to %qD do not match original template %qD",
5361 decl
, DECL_TEMPLATE_RESULT (tmpl
));
5362 if (!uses_template_parms (TI_ARGS (tinfo
)))
5363 inform (input_location
, "use template<> for an explicit specialization");
5364 /* Avoid crash in import_export_decl. */
5365 DECL_INTERFACE_KNOWN (decl
) = 1;
5366 return error_mark_node
;
5370 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5371 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5373 /* Push template declarations for global functions and types. Note
5374 that we do not try to push a global template friend declared in a
5375 template class; such a thing may well depend on the template
5376 parameters of the class. */
5377 if (new_template_p
&& !ctx
5378 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5380 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5381 if (tmpl
== error_mark_node
)
5382 return error_mark_node
;
5384 /* Hide template friend classes that haven't been declared yet. */
5385 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5387 DECL_ANTICIPATED (tmpl
) = 1;
5388 DECL_FRIEND_P (tmpl
) = 1;
5394 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5397 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5398 if (DECL_CONV_FN_P (tmpl
))
5400 int depth
= TMPL_PARMS_DEPTH (parms
);
5402 /* It is a conversion operator. See if the type converted to
5403 depends on innermost template operands. */
5405 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
5407 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
5410 /* Give template template parms a DECL_CONTEXT of the template
5411 for which they are a parameter. */
5412 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5413 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5415 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5416 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5417 DECL_CONTEXT (parm
) = tmpl
;
5421 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5422 back to its most general template. If TMPL is a specialization,
5423 ARGS may only have the innermost set of arguments. Add the missing
5424 argument levels if necessary. */
5425 if (DECL_TEMPLATE_INFO (tmpl
))
5426 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5428 info
= build_template_info (tmpl
, args
);
5430 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5431 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5434 if (is_primary
&& !DECL_LANG_SPECIFIC (decl
))
5435 retrofit_lang_decl (decl
);
5436 if (DECL_LANG_SPECIFIC (decl
))
5437 DECL_TEMPLATE_INFO (decl
) = info
;
5440 if (flag_implicit_templates
5442 && TREE_PUBLIC (decl
)
5443 && VAR_OR_FUNCTION_DECL_P (decl
))
5444 /* Set DECL_COMDAT on template instantiations; if we force
5445 them to be emitted by explicit instantiation or -frepo,
5446 mark_needed will tell cgraph to do the right thing. */
5447 DECL_COMDAT (decl
) = true;
5449 return DECL_TEMPLATE_RESULT (tmpl
);
5453 push_template_decl (tree decl
)
5455 return push_template_decl_real (decl
, false);
5458 /* FN is an inheriting constructor that inherits from the constructor
5459 template INHERITED; turn FN into a constructor template with a matching
5463 add_inherited_template_parms (tree fn
, tree inherited
)
5466 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5467 inner_parms
= copy_node (inner_parms
);
5469 = tree_cons (size_int (processing_template_decl
+ 1),
5470 inner_parms
, current_template_parms
);
5471 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5472 tree args
= template_parms_to_args (parms
);
5474 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5475 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5476 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5477 DECL_ARTIFICIAL (tmpl
) = true;
5478 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5483 /* Called when a class template TYPE is redeclared with the indicated
5484 template PARMS, e.g.:
5486 template <class T> struct S;
5487 template <class T> struct S {}; */
5490 redeclare_class_template (tree type
, tree parms
, tree cons
)
5496 if (!TYPE_TEMPLATE_INFO (type
))
5498 error ("%qT is not a template type", type
);
5502 tmpl
= TYPE_TI_TEMPLATE (type
);
5503 if (!PRIMARY_TEMPLATE_P (tmpl
))
5504 /* The type is nested in some template class. Nothing to worry
5505 about here; there are no new template parameters for the nested
5511 error ("template specifiers not specified in declaration of %qD",
5516 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5517 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5519 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5521 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5522 "redeclared with %d template parameter",
5523 "redeclared with %d template parameters",
5524 TREE_VEC_LENGTH (parms
));
5525 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
5526 "previous declaration %q+D used %d template parameter",
5527 "previous declaration %q+D used %d template parameters",
5528 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5532 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5539 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5540 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5543 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5544 if (error_operand_p (tmpl_parm
))
5547 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5548 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5549 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5551 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5553 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5554 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5555 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5556 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5557 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5558 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5559 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5560 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5561 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5563 error ("template parameter %q+#D", tmpl_parm
);
5564 error ("redeclared here as %q#D", parm
);
5568 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5570 /* We have in [temp.param]:
5572 A template-parameter may not be given default arguments
5573 by two different declarations in the same scope. */
5574 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5575 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5576 "original definition appeared here");
5580 if (parm_default
!= NULL_TREE
)
5581 /* Update the previous template parameters (which are the ones
5582 that will really count) with the new default value. */
5583 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5584 else if (tmpl_default
!= NULL_TREE
)
5585 /* Update the new parameters, too; they'll be used as the
5586 parameters for any members. */
5587 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5590 // Cannot redeclare a class template with a different set of constraints.
5591 if (!equivalent_constraints (get_constraints (tmpl
), cons
))
5593 error_at (input_location
, "redeclaration %q#D with different "
5594 "constraints", tmpl
);
5595 inform (DECL_SOURCE_LOCATION (tmpl
),
5596 "original declaration appeared here");
5602 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5603 to be used when the caller has already checked
5604 (processing_template_decl
5605 && !instantiation_dependent_expression_p (expr)
5606 && potential_constant_expression (expr))
5607 and cleared processing_template_decl. */
5610 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
5612 return tsubst_copy_and_build (expr
,
5615 /*in_decl=*/NULL_TREE
,
5616 /*function_p=*/false,
5617 /*integral_constant_expression_p=*/true);
5620 /* Simplify EXPR if it is a non-dependent expression. Returns the
5621 (possibly simplified) expression. */
5624 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5626 if (expr
== NULL_TREE
)
5629 /* If we're in a template, but EXPR isn't value dependent, simplify
5630 it. We're supposed to treat:
5632 template <typename T> void f(T[1 + 1]);
5633 template <typename T> void f(T[2]);
5635 as two declarations of the same function, for example. */
5636 if (processing_template_decl
5637 && !instantiation_dependent_expression_p (expr
)
5638 && potential_constant_expression (expr
))
5640 processing_template_decl_sentinel s
;
5641 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
5647 instantiate_non_dependent_expr (tree expr
)
5649 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
5652 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5653 template declaration, or a TYPE_DECL for an alias declaration. */
5656 alias_type_or_template_p (tree t
)
5660 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5663 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5664 || DECL_ALIAS_TEMPLATE_P (t
));
5667 /* Return TRUE iff T is a specialization of an alias template. */
5670 alias_template_specialization_p (const_tree t
)
5672 /* It's an alias template specialization if it's an alias and its
5673 TYPE_NAME is a specialization of a primary template. */
5674 if (TYPE_ALIAS_P (t
))
5676 tree name
= TYPE_NAME (t
);
5677 if (DECL_LANG_SPECIFIC (name
))
5678 if (tree ti
= DECL_TEMPLATE_INFO (name
))
5680 tree tmpl
= TI_TEMPLATE (ti
);
5681 return PRIMARY_TEMPLATE_P (tmpl
);
5687 /* Return TRUE iff T is a specialization of an alias template with
5688 dependent template-arguments. */
5691 dependent_alias_template_spec_p (const_tree t
)
5693 return (alias_template_specialization_p (t
)
5694 && (any_dependent_template_arguments_p
5695 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (t
)))));
5698 /* Return the number of innermost template parameters in TMPL. */
5701 num_innermost_template_parms (tree tmpl
)
5703 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
5704 return TREE_VEC_LENGTH (parms
);
5707 /* Return either TMPL or another template that it is equivalent to under DR
5708 1286: An alias that just changes the name of a template is equivalent to
5709 the other template. */
5712 get_underlying_template (tree tmpl
)
5714 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
5715 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
5717 tree result
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5718 if (TYPE_TEMPLATE_INFO (result
))
5720 tree sub
= TYPE_TI_TEMPLATE (result
);
5721 if (PRIMARY_TEMPLATE_P (sub
)
5722 && (num_innermost_template_parms (tmpl
)
5723 == num_innermost_template_parms (sub
)))
5725 tree alias_args
= INNERMOST_TEMPLATE_ARGS
5726 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
5727 if (!comp_template_args (TYPE_TI_ARGS (result
), alias_args
))
5729 /* The alias type is equivalent to the pattern of the
5730 underlying template, so strip the alias. */
5740 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5741 must be a function or a pointer-to-function type, as specified
5742 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5743 and check that the resulting function has external linkage. */
5746 convert_nontype_argument_function (tree type
, tree expr
,
5747 tsubst_flags_t complain
)
5751 linkage_kind linkage
;
5753 fn
= instantiate_type (type
, fns
, tf_none
);
5754 if (fn
== error_mark_node
)
5755 return error_mark_node
;
5758 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5759 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5760 if (BASELINK_P (fn_no_ptr
))
5761 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5763 /* [temp.arg.nontype]/1
5765 A template-argument for a non-type, non-template template-parameter
5768 -- the address of an object or function with external [C++11: or
5769 internal] linkage. */
5771 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
5773 if (complain
& tf_error
)
5775 error ("%qE is not a valid template argument for type %qT",
5777 if (TYPE_PTR_P (type
))
5778 error ("it must be the address of a function with "
5779 "external linkage");
5781 error ("it must be the name of a function with "
5782 "external linkage");
5787 linkage
= decl_linkage (fn_no_ptr
);
5788 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
5790 if (complain
& tf_error
)
5792 if (cxx_dialect
>= cxx11
)
5793 error ("%qE is not a valid template argument for type %qT "
5794 "because %qD has no linkage",
5795 expr
, type
, fn_no_ptr
);
5797 error ("%qE is not a valid template argument for type %qT "
5798 "because %qD does not have external linkage",
5799 expr
, type
, fn_no_ptr
);
5807 /* Subroutine of convert_nontype_argument.
5808 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5809 Emit an error otherwise. */
5812 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5813 tsubst_flags_t complain
)
5816 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5818 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
5820 if (processing_template_decl
5821 && TREE_CODE (expr
) == ADDR_EXPR
5822 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
5824 if (complain
& tf_error
)
5826 error ("%qE is not a valid template argument for type %qT",
5828 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5833 /* Returns TRUE iff the address of OP is value-dependent.
5835 14.6.2.4 [temp.dep.temp]:
5836 A non-integral non-type template-argument is dependent if its type is
5837 dependent or it has either of the following forms
5840 and contains a nested-name-specifier which specifies a class-name that
5841 names a dependent type.
5843 We generalize this to just say that the address of a member of a
5844 dependent class is value-dependent; the above doesn't cover the
5845 address of a static data member named with an unqualified-id. */
5848 has_value_dependent_address (tree op
)
5850 /* We could use get_inner_reference here, but there's no need;
5851 this is only relevant for template non-type arguments, which
5852 can only be expressed as &id-expression. */
5855 tree ctx
= CP_DECL_CONTEXT (op
);
5856 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5863 /* The next set of functions are used for providing helpful explanatory
5864 diagnostics for failed overload resolution. Their messages should be
5865 indented by two spaces for consistency with the messages in
5869 unify_success (bool /*explain_p*/)
5875 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5878 inform (input_location
,
5879 " couldn't deduce template parameter %qD", parm
);
5884 unify_invalid (bool /*explain_p*/)
5890 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5893 inform (input_location
,
5894 " types %qT and %qT have incompatible cv-qualifiers",
5900 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5903 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5908 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
5911 inform (input_location
,
5912 " template parameter %qD is not a parameter pack, but "
5919 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
5922 inform (input_location
,
5923 " template argument %qE does not match "
5924 "pointer-to-member constant %qE",
5930 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
5933 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
5938 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
5941 inform (input_location
,
5942 " inconsistent parameter pack deduction with %qT and %qT",
5948 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
5953 inform (input_location
,
5954 " deduced conflicting types for parameter %qT (%qT and %qT)",
5955 parm
, first
, second
);
5957 inform (input_location
,
5958 " deduced conflicting values for non-type parameter "
5959 "%qE (%qE and %qE)", parm
, first
, second
);
5965 unify_vla_arg (bool explain_p
, tree arg
)
5968 inform (input_location
,
5969 " variable-sized array type %qT is not "
5970 "a valid template argument",
5976 unify_method_type_error (bool explain_p
, tree arg
)
5979 inform (input_location
,
5980 " member function type %qT is not a valid template argument",
5986 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
5991 inform_n (input_location
, wanted
,
5992 " candidate expects at least %d argument, %d provided",
5993 " candidate expects at least %d arguments, %d provided",
5996 inform_n (input_location
, wanted
,
5997 " candidate expects %d argument, %d provided",
5998 " candidate expects %d arguments, %d provided",
6005 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6007 return unify_arity (explain_p
, have
, wanted
);
6011 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6012 bool least_p
= false)
6014 return unify_arity (explain_p
, have
, wanted
, least_p
);
6018 unify_arg_conversion (bool explain_p
, tree to_type
,
6019 tree from_type
, tree arg
)
6022 inform (EXPR_LOC_OR_LOC (arg
, input_location
),
6023 " cannot convert %qE (type %qT) to type %qT",
6024 arg
, from_type
, to_type
);
6029 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6030 tree parm
, tree arg
)
6035 case tbr_ambiguous_baseclass
:
6036 inform (input_location
, " %qT is an ambiguous base class of %qT",
6040 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6047 unify_inconsistent_template_template_parameters (bool explain_p
)
6050 inform (input_location
,
6051 " template parameters of a template template argument are "
6052 "inconsistent with other deduced template arguments");
6057 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6060 inform (input_location
,
6061 " can't deduce a template for %qT from non-template type %qT",
6067 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6070 inform (input_location
,
6071 " template argument %qE does not match %qD", arg
, parm
);
6076 unify_overload_resolution_failure (bool explain_p
, tree arg
)
6079 inform (input_location
,
6080 " could not resolve address from overloaded function %qE",
6085 /* Attempt to convert the non-type template parameter EXPR to the
6086 indicated TYPE. If the conversion is successful, return the
6087 converted value. If the conversion is unsuccessful, return
6088 NULL_TREE if we issued an error message, or error_mark_node if we
6089 did not. We issue error messages for out-and-out bad template
6090 parameters, but not simply because the conversion failed, since we
6091 might be just trying to do argument deduction. Both TYPE and EXPR
6092 must be non-dependent.
6094 The conversion follows the special rules described in
6095 [temp.arg.nontype], and it is much more strict than an implicit
6098 This function is called twice for each template argument (see
6099 lookup_template_class for a more accurate description of this
6100 problem). This means that we need to handle expressions which
6101 are not valid in a C++ source, but can be created from the
6102 first call (for instance, casts to perform conversions). These
6103 hacks can go away after we fix the double coercion problem. */
6106 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
6110 /* Detect immediately string literals as invalid non-type argument.
6111 This special-case is not needed for correctness (we would easily
6112 catch this later), but only to provide better diagnostic for this
6113 common user mistake. As suggested by DR 100, we do not mention
6114 linkage issues in the diagnostic as this is not the point. */
6115 /* FIXME we're making this OK. */
6116 if (TREE_CODE (expr
) == STRING_CST
)
6118 if (complain
& tf_error
)
6119 error ("%qE is not a valid template argument for type %qT "
6120 "because string literals can never be used in this context",
6125 /* Add the ADDR_EXPR now for the benefit of
6126 value_dependent_expression_p. */
6127 if (TYPE_PTROBV_P (type
)
6128 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
6130 expr
= decay_conversion (expr
, complain
);
6131 if (expr
== error_mark_node
)
6132 return error_mark_node
;
6135 /* If we are in a template, EXPR may be non-dependent, but still
6136 have a syntactic, rather than semantic, form. For example, EXPR
6137 might be a SCOPE_REF, rather than the VAR_DECL to which the
6138 SCOPE_REF refers. Preserving the qualifying scope is necessary
6139 so that access checking can be performed when the template is
6140 instantiated -- but here we need the resolved form so that we can
6141 convert the argument. */
6142 bool non_dep
= false;
6143 if (TYPE_REF_OBJ_P (type
)
6144 && has_value_dependent_address (expr
))
6145 /* If we want the address and it's value-dependent, don't fold. */;
6146 else if (!type_unknown_p (expr
)
6147 && processing_template_decl
6148 && !instantiation_dependent_expression_p (expr
)
6149 && potential_constant_expression (expr
))
6151 if (error_operand_p (expr
))
6152 return error_mark_node
;
6153 expr_type
= TREE_TYPE (expr
);
6154 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6155 expr
= mark_lvalue_use (expr
);
6157 expr
= mark_rvalue_use (expr
);
6159 /* If the argument is non-dependent, perform any conversions in
6160 non-dependent context as well. */
6161 processing_template_decl_sentinel
s (non_dep
);
6163 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6165 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6166 to a non-type argument of "nullptr". */
6167 if (expr
== nullptr_node
&& TYPE_PTR_OR_PTRMEM_P (type
))
6168 expr
= convert (type
, expr
);
6170 /* In C++11, integral or enumeration non-type template arguments can be
6171 arbitrary constant expressions. Pointer and pointer to
6172 member arguments can be general constant expressions that evaluate
6173 to a null value, but otherwise still need to be of a specific form. */
6174 if (cxx_dialect
>= cxx11
)
6176 if (TREE_CODE (expr
) == PTRMEM_CST
)
6177 /* A PTRMEM_CST is already constant, and a valid template
6178 argument for a parameter of pointer to member type, we just want
6179 to leave it in that form rather than lower it to a
6181 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6182 expr
= maybe_constant_value (expr
);
6183 else if (TYPE_PTR_OR_PTRMEM_P (type
))
6185 tree folded
= maybe_constant_value (expr
);
6186 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
6187 : null_member_pointer_value_p (folded
))
6192 /* HACK: Due to double coercion, we can get a
6193 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6194 which is the tree that we built on the first call (see
6195 below when coercing to reference to object or to reference to
6196 function). We just strip everything and get to the arg.
6197 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6199 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
6201 tree probe_type
, probe
= expr
;
6202 if (REFERENCE_REF_P (probe
))
6203 probe
= TREE_OPERAND (probe
, 0);
6204 probe_type
= TREE_TYPE (probe
);
6205 if (TREE_CODE (probe
) == NOP_EXPR
)
6207 /* ??? Maybe we could use convert_from_reference here, but we
6208 would need to relax its constraints because the NOP_EXPR
6209 could actually change the type to something more cv-qualified,
6210 and this is not folded by convert_from_reference. */
6211 tree addr
= TREE_OPERAND (probe
, 0);
6212 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
6213 && TREE_CODE (addr
) == ADDR_EXPR
6214 && TYPE_PTR_P (TREE_TYPE (addr
))
6215 && (same_type_ignoring_top_level_qualifiers_p
6216 (TREE_TYPE (probe_type
),
6217 TREE_TYPE (TREE_TYPE (addr
)))))
6219 expr
= TREE_OPERAND (addr
, 0);
6220 expr_type
= TREE_TYPE (probe_type
);
6225 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
6226 parameter is a pointer to object, through decay and
6227 qualification conversion. Let's strip everything. */
6228 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
6232 if (TREE_CODE (probe
) == ADDR_EXPR
6233 && TYPE_PTR_P (TREE_TYPE (probe
)))
6235 /* Skip the ADDR_EXPR only if it is part of the decay for
6236 an array. Otherwise, it is part of the original argument
6237 in the source code. */
6238 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe
, 0))) == ARRAY_TYPE
)
6239 probe
= TREE_OPERAND (probe
, 0);
6241 expr_type
= TREE_TYPE (expr
);
6245 /* [temp.arg.nontype]/5, bullet 1
6247 For a non-type template-parameter of integral or enumeration type,
6248 integral promotions (_conv.prom_) and integral conversions
6249 (_conv.integral_) are applied. */
6250 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6252 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
6253 t
= maybe_constant_value (t
);
6254 if (t
!= error_mark_node
)
6257 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
6258 return error_mark_node
;
6260 /* Notice that there are constant expressions like '4 % 0' which
6261 do not fold into integer constants. */
6262 if (TREE_CODE (expr
) != INTEGER_CST
)
6264 if (complain
& tf_error
)
6266 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
6267 if (processing_template_decl
6268 && !require_potential_constant_expression (expr
))
6270 expr
= cxx_constant_value (expr
);
6271 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
6272 inform (EXPR_LOC_OR_LOC (expr
, input_location
),
6273 "in template argument for type %qT ", type
);
6274 if (expr
== error_mark_node
)
6276 /* else cxx_constant_value complained but gave us
6277 a real constant, so go ahead. */
6278 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
6284 /* Avoid typedef problems. */
6285 if (TREE_TYPE (expr
) != type
)
6286 expr
= fold_convert (type
, expr
);
6288 /* [temp.arg.nontype]/5, bullet 2
6290 For a non-type template-parameter of type pointer to object,
6291 qualification conversions (_conv.qual_) and the array-to-pointer
6292 conversion (_conv.array_) are applied. */
6293 else if (TYPE_PTROBV_P (type
))
6295 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6297 A template-argument for a non-type, non-template template-parameter
6298 shall be one of: [...]
6300 -- the name of a non-type template-parameter;
6301 -- the address of an object or function with external linkage, [...]
6302 expressed as "& id-expression" where the & is optional if the name
6303 refers to a function or array, or if the corresponding
6304 template-parameter is a reference.
6306 Here, we do not care about functions, as they are invalid anyway
6307 for a parameter of type pointer-to-object. */
6309 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
6310 /* Non-type template parameters are OK. */
6312 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6313 /* Null pointer values are OK in C++11. */;
6314 else if (TREE_CODE (expr
) != ADDR_EXPR
6315 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
6319 if (complain
& tf_error
)
6320 error ("%qD is not a valid template argument "
6321 "because %qD is a variable, not the address of "
6322 "a variable", expr
, expr
);
6325 if (POINTER_TYPE_P (expr_type
))
6327 if (complain
& tf_error
)
6328 error ("%qE is not a valid template argument for %qT "
6329 "because it is not the address of a variable",
6333 /* Other values, like integer constants, might be valid
6334 non-type arguments of some other type. */
6335 return error_mark_node
;
6341 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
6342 ? TREE_OPERAND (expr
, 0) : expr
);
6345 if (complain
& tf_error
)
6346 error ("%qE is not a valid template argument of type %qT "
6347 "because %qE is not a variable", expr
, type
, decl
);
6350 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6352 if (complain
& tf_error
)
6353 error ("%qE is not a valid template argument of type %qT "
6354 "because %qD does not have external linkage",
6358 else if (cxx_dialect
>= cxx11
&& decl_linkage (decl
) == lk_none
)
6360 if (complain
& tf_error
)
6361 error ("%qE is not a valid template argument of type %qT "
6362 "because %qD has no linkage", expr
, type
, decl
);
6367 expr
= decay_conversion (expr
, complain
);
6368 if (expr
== error_mark_node
)
6369 return error_mark_node
;
6371 expr
= perform_qualification_conversions (type
, expr
);
6372 if (expr
== error_mark_node
)
6373 return error_mark_node
;
6375 /* [temp.arg.nontype]/5, bullet 3
6377 For a non-type template-parameter of type reference to object, no
6378 conversions apply. The type referred to by the reference may be more
6379 cv-qualified than the (otherwise identical) type of the
6380 template-argument. The template-parameter is bound directly to the
6381 template-argument, which must be an lvalue. */
6382 else if (TYPE_REF_OBJ_P (type
))
6384 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
6386 return error_mark_node
;
6388 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
6390 if (complain
& tf_error
)
6391 error ("%qE is not a valid template argument for type %qT "
6392 "because of conflicts in cv-qualification", expr
, type
);
6396 if (!real_lvalue_p (expr
))
6398 if (complain
& tf_error
)
6399 error ("%qE is not a valid template argument for type %qT "
6400 "because it is not an lvalue", expr
, type
);
6404 /* [temp.arg.nontype]/1
6406 A template-argument for a non-type, non-template template-parameter
6407 shall be one of: [...]
6409 -- the address of an object or function with external linkage. */
6410 if (INDIRECT_REF_P (expr
)
6411 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
6413 expr
= TREE_OPERAND (expr
, 0);
6416 if (complain
& tf_error
)
6417 error ("%q#D is not a valid template argument for type %qT "
6418 "because a reference variable does not have a constant "
6419 "address", expr
, type
);
6426 if (complain
& tf_error
)
6427 error ("%qE is not a valid template argument for type %qT "
6428 "because it is not an object with external linkage",
6433 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
6435 if (complain
& tf_error
)
6436 error ("%qE is not a valid template argument for type %qT "
6437 "because object %qD has not external linkage",
6442 expr
= build_nop (type
, build_address (expr
));
6444 /* [temp.arg.nontype]/5, bullet 4
6446 For a non-type template-parameter of type pointer to function, only
6447 the function-to-pointer conversion (_conv.func_) is applied. If the
6448 template-argument represents a set of overloaded functions (or a
6449 pointer to such), the matching function is selected from the set
6451 else if (TYPE_PTRFN_P (type
))
6453 /* If the argument is a template-id, we might not have enough
6454 context information to decay the pointer. */
6455 if (!type_unknown_p (expr_type
))
6457 expr
= decay_conversion (expr
, complain
);
6458 if (expr
== error_mark_node
)
6459 return error_mark_node
;
6462 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6463 /* Null pointer values are OK in C++11. */
6464 return perform_qualification_conversions (type
, expr
);
6466 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6467 if (!expr
|| expr
== error_mark_node
)
6470 /* [temp.arg.nontype]/5, bullet 5
6472 For a non-type template-parameter of type reference to function, no
6473 conversions apply. If the template-argument represents a set of
6474 overloaded functions, the matching function is selected from the set
6476 else if (TYPE_REFFN_P (type
))
6478 if (TREE_CODE (expr
) == ADDR_EXPR
)
6480 if (complain
& tf_error
)
6482 error ("%qE is not a valid template argument for type %qT "
6483 "because it is a pointer", expr
, type
);
6484 inform (input_location
, "try using %qE instead",
6485 TREE_OPERAND (expr
, 0));
6490 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6491 if (!expr
|| expr
== error_mark_node
)
6494 expr
= build_nop (type
, build_address (expr
));
6496 /* [temp.arg.nontype]/5, bullet 6
6498 For a non-type template-parameter of type pointer to member function,
6499 no conversions apply. If the template-argument represents a set of
6500 overloaded member functions, the matching member function is selected
6501 from the set (_over.over_). */
6502 else if (TYPE_PTRMEMFUNC_P (type
))
6504 expr
= instantiate_type (type
, expr
, tf_none
);
6505 if (expr
== error_mark_node
)
6506 return error_mark_node
;
6508 /* [temp.arg.nontype] bullet 1 says the pointer to member
6509 expression must be a pointer-to-member constant. */
6510 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6511 return error_mark_node
;
6513 /* There is no way to disable standard conversions in
6514 resolve_address_of_overloaded_function (called by
6515 instantiate_type). It is possible that the call succeeded by
6516 converting &B::I to &D::I (where B is a base of D), so we need
6517 to reject this conversion here.
6519 Actually, even if there was a way to disable standard conversions,
6520 it would still be better to reject them here so that we can
6521 provide a superior diagnostic. */
6522 if (!same_type_p (TREE_TYPE (expr
), type
))
6524 if (complain
& tf_error
)
6526 error ("%qE is not a valid template argument for type %qT "
6527 "because it is of type %qT", expr
, type
,
6529 /* If we are just one standard conversion off, explain. */
6530 if (can_convert_standard (type
, TREE_TYPE (expr
), complain
))
6531 inform (input_location
,
6532 "standard conversions are not allowed in this context");
6537 /* [temp.arg.nontype]/5, bullet 7
6539 For a non-type template-parameter of type pointer to data member,
6540 qualification conversions (_conv.qual_) are applied. */
6541 else if (TYPE_PTRDATAMEM_P (type
))
6543 /* [temp.arg.nontype] bullet 1 says the pointer to member
6544 expression must be a pointer-to-member constant. */
6545 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6546 return error_mark_node
;
6548 expr
= perform_qualification_conversions (type
, expr
);
6549 if (expr
== error_mark_node
)
6552 else if (NULLPTR_TYPE_P (type
))
6554 if (expr
!= nullptr_node
)
6556 if (complain
& tf_error
)
6557 error ("%qE is not a valid template argument for type %qT "
6558 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
6563 /* A template non-type parameter must be one of the above. */
6567 /* Sanity check: did we actually convert the argument to the
6569 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6570 (type
, TREE_TYPE (expr
)));
6571 return convert_from_reference (expr
);
6574 /* Subroutine of coerce_template_template_parms, which returns 1 if
6575 PARM_PARM and ARG_PARM match using the rule for the template
6576 parameters of template template parameters. Both PARM and ARG are
6577 template parameters; the rest of the arguments are the same as for
6578 coerce_template_template_parms.
6581 coerce_template_template_parm (tree parm
,
6583 tsubst_flags_t complain
,
6587 if (arg
== NULL_TREE
|| error_operand_p (arg
)
6588 || parm
== NULL_TREE
|| error_operand_p (parm
))
6591 if (TREE_CODE (arg
) != TREE_CODE (parm
))
6594 switch (TREE_CODE (parm
))
6597 /* We encounter instantiations of templates like
6598 template <template <template <class> class> class TT>
6601 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6602 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6604 if (!coerce_template_template_parms
6605 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
6611 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
6612 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6613 /* Argument is a parameter pack but parameter is not. */
6618 /* The tsubst call is used to handle cases such as
6620 template <int> class C {};
6621 template <class T, template <T> class TT> class D {};
6624 i.e. the parameter list of TT depends on earlier parameters. */
6625 if (!uses_template_parms (TREE_TYPE (arg
))
6627 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
6631 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6632 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6633 /* Argument is a parameter pack but parameter is not. */
6646 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6647 template template parameters. Both PARM_PARMS and ARG_PARMS are
6648 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6651 Consider the example:
6652 template <class T> class A;
6653 template<template <class U> class TT> class B;
6655 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6656 the parameters to A, and OUTER_ARGS contains A. */
6659 coerce_template_template_parms (tree parm_parms
,
6661 tsubst_flags_t complain
,
6665 int nparms
, nargs
, i
;
6669 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
6670 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
6672 nparms
= TREE_VEC_LENGTH (parm_parms
);
6673 nargs
= TREE_VEC_LENGTH (arg_parms
);
6675 /* Determine whether we have a parameter pack at the end of the
6676 template template parameter's template parameter list. */
6677 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
6679 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
6681 if (error_operand_p (parm
))
6684 switch (TREE_CODE (parm
))
6688 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6693 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6703 && !(variadic_p
&& nargs
>= nparms
- 1))
6706 /* Check all of the template parameters except the parameter pack at
6707 the end (if any). */
6708 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6710 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6711 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6714 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6715 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6717 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6725 /* Check each of the template parameters in the template
6726 argument against the template parameter pack at the end of
6727 the template template parameter. */
6728 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6731 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6733 for (; i
< nargs
; ++i
)
6735 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6738 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6740 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6749 /* Verifies that the deduced template arguments (in TARGS) for the
6750 template template parameters (in TPARMS) represent valid bindings,
6751 by comparing the template parameter list of each template argument
6752 to the template parameter list of its corresponding template
6753 template parameter, in accordance with DR150. This
6754 routine can only be called after all template arguments have been
6755 deduced. It will return TRUE if all of the template template
6756 parameter bindings are okay, FALSE otherwise. */
6758 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6760 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6763 /* We're dealing with template parms in this process. */
6764 ++processing_template_decl
;
6766 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6768 for (i
= 0; i
< ntparms
; ++i
)
6770 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6771 tree targ
= TREE_VEC_ELT (targs
, i
);
6773 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6775 tree packed_args
= NULL_TREE
;
6778 if (ARGUMENT_PACK_P (targ
))
6780 /* Look inside the argument pack. */
6781 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6782 len
= TREE_VEC_LENGTH (packed_args
);
6785 for (idx
= 0; idx
< len
; ++idx
)
6787 tree targ_parms
= NULL_TREE
;
6790 /* Extract the next argument from the argument
6792 targ
= TREE_VEC_ELT (packed_args
, idx
);
6794 if (PACK_EXPANSION_P (targ
))
6795 /* Look at the pattern of the pack expansion. */
6796 targ
= PACK_EXPANSION_PATTERN (targ
);
6798 /* Extract the template parameters from the template
6800 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6801 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6802 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6803 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6805 /* Verify that we can coerce the template template
6806 parameters from the template argument to the template
6807 parameter. This requires an exact match. */
6809 && !coerce_template_template_parms
6810 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6825 --processing_template_decl
;
6829 /* Since type attributes aren't mangled, we need to strip them from
6830 template type arguments. */
6833 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6836 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6838 mv
= TYPE_MAIN_VARIANT (arg
);
6839 arg
= strip_typedefs (arg
);
6840 if (TYPE_ALIGN (arg
) != TYPE_ALIGN (mv
)
6841 || TYPE_ATTRIBUTES (arg
) != TYPE_ATTRIBUTES (mv
))
6843 if (complain
& tf_warning
)
6844 warning (0, "ignoring attributes on template argument %qT", arg
);
6845 arg
= build_aligned_type (arg
, TYPE_ALIGN (mv
));
6846 arg
= cp_build_type_attribute_variant (arg
, TYPE_ATTRIBUTES (mv
));
6851 // A template declaration can be substituted for a constrained
6852 // template template parameter only when the argument is more
6853 // constrained than the parameter.
6855 is_compatible_template_arg (tree parm
, tree arg
)
6857 // TODO: The argument may not have a decl result. This seems to
6858 // happen in some classes with nested template template
6859 // parameters (e.g., a rebind struct in a class taking a template
6860 // template parameter. If this is the case, just return true and
6861 // allow things to happen as they always did.
6862 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
6864 if (!DECL_TEMPLATE_RESULT (arg
))
6868 tree parm_cons
= get_constraints (parm
);
6869 tree arg_cons
= get_constraints (arg
);
6871 // If the template parameter is constrained, we need to rewrite its
6872 // constraints in terms of the ARG's template parameters. This ensures
6873 // that all of the template parameter types will have the same depth.
6875 // Note that this is only valid when coerce_template_template_parm is
6876 // true for the innermost template parameters of PARM and ARG. In other
6877 // words, because coercion is successful, this conversion will be valid.
6880 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (arg
));
6881 parm_cons
= tsubst_constraint_info (parm_cons
, args
, tf_none
, NULL_TREE
);
6882 if (parm_cons
== error_mark_node
)
6886 return subsumes (parm_cons
, arg_cons
);
6889 // Convert a placeholder argument into a binding to the original
6890 // parameter. The original parameter is saved as the TREE_TYPE of
6893 convert_placeholder_argument (tree parm
, tree arg
)
6895 TREE_TYPE (arg
) = parm
;
6899 /* Convert the indicated template ARG as necessary to match the
6900 indicated template PARM. Returns the converted ARG, or
6901 error_mark_node if the conversion was unsuccessful. Error and
6902 warning messages are issued under control of COMPLAIN. This
6903 conversion is for the Ith parameter in the parameter list. ARGS is
6904 the full set of template arguments deduced so far. */
6907 convert_template_argument (tree parm
,
6910 tsubst_flags_t complain
,
6916 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
6918 if (parm
== error_mark_node
)
6919 return error_mark_node
;
6921 /* Trivially convert placeholders. */
6922 if (TREE_CODE (arg
) == INTRODUCED_PARM_DECL
)
6923 return convert_placeholder_argument (parm
, arg
);
6925 if (TREE_CODE (arg
) == TREE_LIST
6926 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
6928 /* The template argument was the name of some
6929 member function. That's usually
6930 invalid, but static members are OK. In any
6931 case, grab the underlying fields/functions
6932 and issue an error later if required. */
6933 orig_arg
= TREE_VALUE (arg
);
6934 TREE_TYPE (arg
) = unknown_type_node
;
6939 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
6940 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
6941 || requires_tmpl_type
);
6943 /* When determining whether an argument pack expansion is a template,
6944 look at the pattern. */
6945 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
6946 arg
= PACK_EXPANSION_PATTERN (arg
);
6948 /* Deal with an injected-class-name used as a template template arg. */
6949 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
6951 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
6952 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6954 if (cxx_dialect
>= cxx11
)
6955 /* OK under DR 1004. */;
6956 else if (complain
& tf_warning_or_error
)
6957 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
6958 " used as template template argument", TYPE_NAME (arg
));
6959 else if (flag_pedantic_errors
)
6967 ((TREE_CODE (arg
) == TEMPLATE_DECL
6968 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
6969 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
6970 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6971 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
6974 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6975 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
6976 arg
= TYPE_STUB_DECL (arg
);
6978 is_type
= TYPE_P (arg
) || is_tmpl_type
;
6980 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
6981 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
6983 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
6985 if (complain
& tf_error
)
6986 error ("invalid use of destructor %qE as a type", orig_arg
);
6987 return error_mark_node
;
6990 permerror (input_location
,
6991 "to refer to a type member of a template parameter, "
6992 "use %<typename %E%>", orig_arg
);
6994 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
6995 TREE_OPERAND (arg
, 1),
7001 if (is_type
!= requires_type
)
7005 if (complain
& tf_error
)
7007 error ("type/value mismatch at argument %d in template "
7008 "parameter list for %qD",
7011 inform (input_location
,
7012 " expected a constant of type %qT, got %qT",
7014 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
7015 else if (requires_tmpl_type
)
7016 inform (input_location
,
7017 " expected a class template, got %qE", orig_arg
);
7019 inform (input_location
,
7020 " expected a type, got %qE", orig_arg
);
7023 return error_mark_node
;
7025 if (is_tmpl_type
^ requires_tmpl_type
)
7027 if (in_decl
&& (complain
& tf_error
))
7029 error ("type/value mismatch at argument %d in template "
7030 "parameter list for %qD",
7033 inform (input_location
,
7034 " expected a type, got %qT", DECL_NAME (arg
));
7036 inform (input_location
,
7037 " expected a class template, got %qT", orig_arg
);
7039 return error_mark_node
;
7044 if (requires_tmpl_type
)
7046 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7048 else if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
7049 /* The number of argument required is not known yet.
7050 Just accept it for now. */
7051 val
= TREE_TYPE (arg
);
7054 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7055 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7057 /* Strip alias templates that are equivalent to another
7059 arg
= get_underlying_template (arg
);
7060 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7062 if (coerce_template_template_parms (parmparm
, argparm
,
7068 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7070 if (val
!= error_mark_node
)
7072 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
7073 val
= TREE_TYPE (val
);
7074 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
7075 val
= make_pack_expansion (val
);
7080 if (in_decl
&& (complain
& tf_error
))
7082 error ("type/value mismatch at argument %d in "
7083 "template parameter list for %qD",
7085 inform (input_location
,
7086 " expected a template of type %qD, got %qT",
7090 val
= error_mark_node
;
7093 // Check that the constraints are compatible before allowing the
7095 if (val
!= error_mark_node
)
7096 if (!is_compatible_template_arg (parm
, arg
))
7098 if (in_decl
&& (complain
& tf_error
))
7100 error ("constraint mismatch at argument %d in "
7101 "template parameter list for %qD",
7103 inform (input_location
, " expected %qD but got %qD",
7106 val
= error_mark_node
;
7112 /* We only form one instance of each template specialization.
7113 Therefore, if we use a non-canonical variant (i.e., a
7114 typedef), any future messages referring to the type will use
7115 the typedef, which is confusing if those future uses do not
7116 themselves also use the typedef. */
7118 val
= canonicalize_type_argument (val
, complain
);
7122 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
7124 if (invalid_nontype_parm_type_p (t
, complain
))
7125 return error_mark_node
;
7127 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7129 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
7133 /* Not sure if this is reachable, but it doesn't hurt
7135 error ("type mismatch in nontype parameter pack");
7136 val
= error_mark_node
;
7139 else if (!dependent_template_arg_p (orig_arg
)
7140 && !uses_template_parms (t
))
7141 /* We used to call digest_init here. However, digest_init
7142 will report errors, which we don't want when complain
7143 is zero. More importantly, digest_init will try too
7144 hard to convert things: for example, `0' should not be
7145 converted to pointer type at this point according to
7146 the standard. Accepting this is not merely an
7147 extension, since deciding whether or not these
7148 conversions can occur is part of determining which
7149 function template to call, or whether a given explicit
7150 argument specification is valid. */
7151 val
= convert_nontype_argument (t
, orig_arg
, complain
);
7153 val
= strip_typedefs_expr (orig_arg
);
7155 if (val
== NULL_TREE
)
7156 val
= error_mark_node
;
7157 else if (val
== error_mark_node
&& (complain
& tf_error
))
7158 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
7160 if (TREE_CODE (val
) == SCOPE_REF
)
7162 /* Strip typedefs from the SCOPE_REF. */
7163 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
7164 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
7166 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
7167 QUALIFIED_NAME_IS_TEMPLATE (val
));
7174 /* Coerces the remaining template arguments in INNER_ARGS (from
7175 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7176 Returns the coerced argument pack. PARM_IDX is the position of this
7177 parameter in the template parameter list. ARGS is the original
7178 template argument list. */
7180 coerce_template_parameter_pack (tree parms
,
7188 tsubst_flags_t complain
)
7190 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
7191 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
7194 tree packed_parms
= NULL_TREE
;
7196 if (arg_idx
> nargs
)
7199 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
7201 /* When the template parameter is a non-type template parameter pack
7202 or template template parameter pack whose type or template
7203 parameters use parameter packs, we know exactly how many arguments
7204 we are looking for. Build a vector of the instantiated decls for
7205 these template parameters in PACKED_PARMS. */
7206 /* We can't use make_pack_expansion here because it would interpret a
7207 _DECL as a use rather than a declaration. */
7208 tree decl
= TREE_VALUE (parm
);
7209 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
7210 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
7211 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
7212 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
7214 TREE_VEC_LENGTH (args
)--;
7215 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
7216 TREE_VEC_LENGTH (args
)++;
7218 if (packed_parms
== error_mark_node
)
7219 return error_mark_node
;
7221 /* If we're doing a partial instantiation of a member template,
7222 verify that all of the types used for the non-type
7223 template parameter pack are, in fact, valid for non-type
7224 template parameters. */
7226 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7228 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
7229 for (j
= 0; j
< len
; ++j
)
7231 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
7232 if (invalid_nontype_parm_type_p (t
, complain
))
7233 return error_mark_node
;
7235 /* We don't know how many args we have yet, just
7236 use the unconverted ones for now. */
7240 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
7242 /* Check if we have a placeholder pack, which indicates we're in the context
7243 of a introduction list. In that case we want to simply match this pack to
7244 the single placeholder. */
7245 else if (arg_idx
< nargs
7246 && IS_INTRODUCED_PACK (TREE_VEC_ELT (inner_args
, arg_idx
)))
7248 nargs
= arg_idx
+ 1;
7249 packed_args
= make_tree_vec (1);
7252 packed_args
= make_tree_vec (nargs
- arg_idx
);
7254 /* Convert the remaining arguments, which will be a part of the
7255 parameter pack "parm". */
7256 for (; arg_idx
< nargs
; ++arg_idx
)
7258 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
7259 tree actual_parm
= TREE_VALUE (parm
);
7260 int pack_idx
= arg_idx
- parm_idx
;
7264 /* Once we've packed as many args as we have types, stop. */
7265 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
7267 else if (PACK_EXPANSION_P (arg
))
7268 /* We don't know how many args we have yet, just
7269 use the unconverted ones for now. */
7272 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
7275 if (arg
== error_mark_node
)
7277 if (complain
& tf_error
)
7278 error ("template argument %d is invalid", arg_idx
+ 1);
7281 arg
= convert_template_argument (actual_parm
,
7282 arg
, new_args
, complain
, parm_idx
,
7284 if (arg
== error_mark_node
)
7286 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
7289 if (arg_idx
- parm_idx
< TREE_VEC_LENGTH (packed_args
)
7290 && TREE_VEC_LENGTH (packed_args
) > 0)
7292 if (complain
& tf_error
)
7293 error ("wrong number of template arguments (%d, should be %d)",
7294 arg_idx
- parm_idx
, TREE_VEC_LENGTH (packed_args
));
7295 return error_mark_node
;
7298 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
7299 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
7300 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
7303 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
7304 TREE_TYPE (argument_pack
)
7305 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
7306 TREE_CONSTANT (argument_pack
) = 1;
7309 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
7310 #ifdef ENABLE_CHECKING
7311 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
7312 TREE_VEC_LENGTH (packed_args
));
7314 return argument_pack
;
7317 /* Returns the number of pack expansions in the template argument vector
7321 pack_expansion_args_count (tree args
)
7326 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
7328 tree elt
= TREE_VEC_ELT (args
, i
);
7329 if (elt
&& PACK_EXPANSION_P (elt
))
7335 /* Convert all template arguments to their appropriate types, and
7336 return a vector containing the innermost resulting template
7337 arguments. If any error occurs, return error_mark_node. Error and
7338 warning messages are issued under control of COMPLAIN.
7340 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7341 for arguments not specified in ARGS. Otherwise, if
7342 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7343 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7344 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7348 coerce_template_parms (tree parms
,
7351 tsubst_flags_t complain
,
7352 bool require_all_args
,
7353 bool use_default_args
)
7355 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
7356 tree orig_inner_args
;
7359 tree new_inner_args
;
7360 int saved_unevaluated_operand
;
7361 int saved_inhibit_evaluation_warnings
;
7363 /* When used as a boolean value, indicates whether this is a
7364 variadic template parameter list. Since it's an int, we can also
7365 subtract it from nparms to get the number of non-variadic
7368 int variadic_args_p
= 0;
7369 int post_variadic_parms
= 0;
7371 /* Likewise for parameters with default arguments. */
7374 if (args
== error_mark_node
)
7375 return error_mark_node
;
7377 nparms
= TREE_VEC_LENGTH (parms
);
7379 /* Determine if there are any parameter packs or default arguments. */
7380 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
7382 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
7384 ++post_variadic_parms
;
7385 if (template_parameter_pack_p (TREE_VALUE (parm
)))
7387 if (TREE_PURPOSE (parm
))
7391 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
7392 /* If there are no parameters that follow a parameter pack, we need to
7393 expand any argument packs so that we can deduce a parameter pack from
7394 some non-packed args followed by an argument pack, as in variadic85.C.
7395 If there are such parameters, we need to leave argument packs intact
7396 so the arguments are assigned properly. This can happen when dealing
7397 with a nested class inside a partial specialization of a class
7398 template, as in variadic92.C, or when deducing a template parameter pack
7399 from a sub-declarator, as in variadic114.C. */
7400 if (!post_variadic_parms
)
7401 inner_args
= expand_template_argument_pack (inner_args
);
7403 /* Count any pack expansion args. */
7404 variadic_args_p
= pack_expansion_args_count (inner_args
);
7406 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
7407 if ((nargs
> nparms
&& !variadic_p
)
7408 || (nargs
< nparms
- variadic_p
7411 && (!use_default_args
7412 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
7413 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
7415 if (complain
& tf_error
)
7417 if (variadic_p
|| default_p
)
7419 nparms
-= variadic_p
+ default_p
;
7420 error ("wrong number of template arguments "
7421 "(%d, should be at least %d)", nargs
, nparms
);
7424 error ("wrong number of template arguments "
7425 "(%d, should be %d)", nargs
, nparms
);
7428 inform (input_location
, "provided for %q+D", in_decl
);
7431 return error_mark_node
;
7433 /* We can't pass a pack expansion to a non-pack parameter of an alias
7434 template (DR 1430). */
7435 else if (in_decl
&& DECL_ALIAS_TEMPLATE_P (in_decl
)
7437 && nargs
- variadic_args_p
< nparms
- variadic_p
)
7439 if (complain
& tf_error
)
7441 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
7443 tree arg
= TREE_VEC_ELT (inner_args
, i
);
7444 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
7446 if (PACK_EXPANSION_P (arg
)
7447 && !template_parameter_pack_p (parm
))
7449 error ("pack expansion argument for non-pack parameter "
7450 "%qD of alias template %qD", parm
, in_decl
);
7451 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
7458 return error_mark_node
;
7461 /* We need to evaluate the template arguments, even though this
7462 template-id may be nested within a "sizeof". */
7463 saved_unevaluated_operand
= cp_unevaluated_operand
;
7464 cp_unevaluated_operand
= 0;
7465 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
7466 c_inhibit_evaluation_warnings
= 0;
7467 new_inner_args
= make_tree_vec (nparms
);
7468 new_args
= add_outermost_template_args (args
, new_inner_args
);
7469 int pack_adjust
= 0;
7470 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
7475 /* Get the Ith template parameter. */
7476 parm
= TREE_VEC_ELT (parms
, parm_idx
);
7478 if (parm
== error_mark_node
)
7480 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
7484 /* Calculate the next argument. */
7485 if (arg_idx
< nargs
)
7486 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
7490 if (template_parameter_pack_p (TREE_VALUE (parm
))
7491 && !(arg
&& ARGUMENT_PACK_P (arg
)))
7493 /* Some arguments will be placed in the
7494 template parameter pack PARM. */
7495 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
7496 inner_args
, arg_idx
,
7500 if (arg
== NULL_TREE
)
7502 /* We don't know how many args we have yet, just use the
7503 unconverted (and still packed) ones for now. */
7504 new_inner_args
= orig_inner_args
;
7509 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
7511 /* Store this argument. */
7512 if (arg
== error_mark_node
)
7515 /* We are done with all of the arguments. */
7520 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
7521 arg_idx
+= pack_adjust
;
7528 if (PACK_EXPANSION_P (arg
))
7530 /* "If every valid specialization of a variadic template
7531 requires an empty template parameter pack, the template is
7532 ill-formed, no diagnostic required." So check that the
7533 pattern works with this parameter. */
7534 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
7535 tree conv
= convert_template_argument (TREE_VALUE (parm
),
7539 if (conv
== error_mark_node
)
7541 inform (input_location
, "so any instantiation with a "
7542 "non-empty parameter pack would be ill-formed");
7545 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
7546 /* Recover from missing typename. */
7547 TREE_VEC_ELT (inner_args
, arg_idx
)
7548 = make_pack_expansion (conv
);
7550 /* We don't know how many args we have yet, just
7551 use the unconverted ones for now. */
7552 new_inner_args
= inner_args
;
7557 else if (require_all_args
)
7559 /* There must be a default arg in this case. */
7560 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
7562 /* The position of the first default template argument,
7563 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
7565 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
7566 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
7567 arg_idx
- pack_adjust
);
7572 if (arg
== error_mark_node
)
7574 if (complain
& tf_error
)
7575 error ("template argument %d is invalid", arg_idx
+ 1);
7578 /* This only occurs if there was an error in the template
7579 parameter list itself (which we would already have
7580 reported) that we are trying to recover from, e.g., a class
7581 template with a parameter list such as
7582 template<typename..., typename>. */
7585 arg
= convert_template_argument (TREE_VALUE (parm
),
7586 arg
, new_args
, complain
,
7589 if (arg
== error_mark_node
)
7591 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
7593 cp_unevaluated_operand
= saved_unevaluated_operand
;
7594 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
7596 if (variadic_p
&& arg_idx
< nargs
)
7598 if (complain
& tf_error
)
7600 error ("wrong number of template arguments "
7601 "(%d, should be %d)", nargs
, arg_idx
);
7603 error ("provided for %q+D", in_decl
);
7605 return error_mark_node
;
7609 return error_mark_node
;
7611 #ifdef ENABLE_CHECKING
7612 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
7613 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
7614 TREE_VEC_LENGTH (new_inner_args
));
7617 return new_inner_args
;
7620 /* Convert all template arguments to their appropriate types, and
7621 return a vector containing the innermost resulting template
7622 arguments. If any error occurs, return error_mark_node. Error and
7623 warning messages are not issued.
7625 Note that no function argument deduction is performed, and default
7626 arguments are used to fill in unspecified arguments. */
7628 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
7630 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
7633 /* Convert all template arguments to their appropriate type, and
7634 instantiate default arguments as needed. This returns a vector
7635 containing the innermost resulting template arguments, or
7636 error_mark_node if unsuccessful. */
7638 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
7639 tsubst_flags_t complain
)
7641 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
7644 /* Like coerce_template_parms. If PARMS represents all template
7645 parameters levels, this function returns a vector of vectors
7646 representing all the resulting argument levels. Note that in this
7647 case, only the innermost arguments are coerced because the
7648 outermost ones are supposed to have been coerced already.
7650 Otherwise, if PARMS represents only (the innermost) vector of
7651 parameters, this function returns a vector containing just the
7652 innermost resulting arguments. */
7655 coerce_innermost_template_parms (tree parms
,
7658 tsubst_flags_t complain
,
7659 bool require_all_args
,
7660 bool use_default_args
)
7662 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
7663 int args_depth
= TMPL_ARGS_DEPTH (args
);
7666 if (parms_depth
> 1)
7668 coerced_args
= make_tree_vec (parms_depth
);
7672 for (level
= parms
, cur_depth
= parms_depth
;
7673 parms_depth
> 0 && level
!= NULL_TREE
;
7674 level
= TREE_CHAIN (level
), --cur_depth
)
7677 if (cur_depth
== args_depth
)
7678 l
= coerce_template_parms (TREE_VALUE (level
),
7679 args
, in_decl
, complain
,
7683 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
7685 if (l
== error_mark_node
)
7686 return error_mark_node
;
7688 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
7692 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
7693 args
, in_decl
, complain
,
7696 return coerced_args
;
7699 /* Returns 1 if template args OT and NT are equivalent. */
7702 template_args_equal (tree ot
, tree nt
)
7706 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
7709 if (TREE_CODE (nt
) == TREE_VEC
)
7710 /* For member templates */
7711 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
7712 else if (PACK_EXPANSION_P (ot
))
7713 return (PACK_EXPANSION_P (nt
)
7714 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
7715 PACK_EXPANSION_PATTERN (nt
))
7716 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
7717 PACK_EXPANSION_EXTRA_ARGS (nt
)));
7718 else if (ARGUMENT_PACK_P (ot
))
7723 if (!ARGUMENT_PACK_P (nt
))
7726 opack
= ARGUMENT_PACK_ARGS (ot
);
7727 npack
= ARGUMENT_PACK_ARGS (nt
);
7728 len
= TREE_VEC_LENGTH (opack
);
7729 if (TREE_VEC_LENGTH (npack
) != len
)
7731 for (i
= 0; i
< len
; ++i
)
7732 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
7733 TREE_VEC_ELT (npack
, i
)))
7737 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
7739 /* We get here probably because we are in the middle of substituting
7740 into the pattern of a pack expansion. In that case the
7741 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
7742 interested in. So we want to use the initial pack argument for
7744 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
7745 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
7746 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
7747 return template_args_equal (ot
, nt
);
7749 else if (TYPE_P (nt
))
7753 /* Don't treat an alias template specialization with dependent
7754 arguments as equivalent to its underlying type when used as a
7755 template argument; we need them to be distinct so that we
7756 substitute into the specialization arguments at instantiation
7757 time. And aliases can't be equivalent without being ==, so
7758 we don't need to look any deeper. */
7759 if (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
))
7762 return same_type_p (ot
, nt
);
7764 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
7768 /* Try to treat a template non-type argument that has been converted
7769 to the parameter type as equivalent to one that hasn't yet. */
7770 for (enum tree_code code1
= TREE_CODE (ot
);
7771 CONVERT_EXPR_CODE_P (code1
)
7772 || code1
== NON_LVALUE_EXPR
;
7773 code1
= TREE_CODE (ot
))
7774 ot
= TREE_OPERAND (ot
, 0);
7775 for (enum tree_code code2
= TREE_CODE (nt
);
7776 CONVERT_EXPR_CODE_P (code2
)
7777 || code2
== NON_LVALUE_EXPR
;
7778 code2
= TREE_CODE (nt
))
7779 nt
= TREE_OPERAND (nt
, 0);
7781 return cp_tree_equal (ot
, nt
);
7785 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7786 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7787 NEWARG_PTR with the offending arguments if they are non-NULL. */
7790 comp_template_args_with_info (tree oldargs
, tree newargs
,
7791 tree
*oldarg_ptr
, tree
*newarg_ptr
)
7795 if (oldargs
== newargs
)
7798 if (!oldargs
|| !newargs
)
7801 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
7804 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
7806 tree nt
= TREE_VEC_ELT (newargs
, i
);
7807 tree ot
= TREE_VEC_ELT (oldargs
, i
);
7809 if (! template_args_equal (ot
, nt
))
7811 if (oldarg_ptr
!= NULL
)
7813 if (newarg_ptr
!= NULL
)
7821 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7822 of template arguments. Returns 0 otherwise. */
7825 comp_template_args (tree oldargs
, tree newargs
)
7827 return comp_template_args_with_info (oldargs
, newargs
, NULL
, NULL
);
7831 add_pending_template (tree d
)
7833 tree ti
= (TYPE_P (d
)
7834 ? CLASSTYPE_TEMPLATE_INFO (d
)
7835 : DECL_TEMPLATE_INFO (d
));
7836 struct pending_template
*pt
;
7839 if (TI_PENDING_TEMPLATE_FLAG (ti
))
7842 /* We are called both from instantiate_decl, where we've already had a
7843 tinst_level pushed, and instantiate_template, where we haven't.
7845 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
7848 push_tinst_level (d
);
7850 pt
= ggc_alloc
<pending_template
> ();
7852 pt
->tinst
= current_tinst_level
;
7853 if (last_pending_template
)
7854 last_pending_template
->next
= pt
;
7856 pending_templates
= pt
;
7858 last_pending_template
= pt
;
7860 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
7867 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7868 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7869 documentation for TEMPLATE_ID_EXPR. */
7872 lookup_template_function (tree fns
, tree arglist
)
7876 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
7877 return error_mark_node
;
7879 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
7881 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
7883 error ("%q#D is not a function template", fns
);
7884 return error_mark_node
;
7887 if (BASELINK_P (fns
))
7889 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
7891 BASELINK_FUNCTIONS (fns
),
7896 type
= TREE_TYPE (fns
);
7897 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
7898 type
= unknown_type_node
;
7900 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
7903 /* Within the scope of a template class S<T>, the name S gets bound
7904 (in build_self_reference) to a TYPE_DECL for the class, not a
7905 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7906 or one of its enclosing classes, and that type is a template,
7907 return the associated TEMPLATE_DECL. Otherwise, the original
7910 Also handle the case when DECL is a TREE_LIST of ambiguous
7911 injected-class-names from different bases. */
7914 maybe_get_template_decl_from_type_decl (tree decl
)
7916 if (decl
== NULL_TREE
)
7919 /* DR 176: A lookup that finds an injected-class-name (10.2
7920 [class.member.lookup]) can result in an ambiguity in certain cases
7921 (for example, if it is found in more than one base class). If all of
7922 the injected-class-names that are found refer to specializations of
7923 the same class template, and if the name is followed by a
7924 template-argument-list, the reference refers to the class template
7925 itself and not a specialization thereof, and is not ambiguous. */
7926 if (TREE_CODE (decl
) == TREE_LIST
)
7928 tree t
, tmpl
= NULL_TREE
;
7929 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
7931 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
7934 else if (tmpl
!= elt
)
7937 if (tmpl
&& t
== NULL_TREE
)
7943 return (decl
!= NULL_TREE
7944 && DECL_SELF_REFERENCE_P (decl
)
7945 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
7946 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
7949 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7950 parameters, find the desired type.
7952 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7954 IN_DECL, if non-NULL, is the template declaration we are trying to
7957 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7958 the class we are looking up.
7960 Issue error and warning messages under control of COMPLAIN.
7962 If the template class is really a local class in a template
7963 function, then the FUNCTION_CONTEXT is the function in which it is
7966 ??? Note that this function is currently called *twice* for each
7967 template-id: the first time from the parser, while creating the
7968 incomplete type (finish_template_type), and the second type during the
7969 real instantiation (instantiate_template_class). This is surely something
7970 that we want to avoid. It also causes some problems with argument
7971 coercion (see convert_nontype_argument for more information on this). */
7974 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
7975 int entering_scope
, tsubst_flags_t complain
)
7977 tree templ
= NULL_TREE
, parmlist
;
7984 if (identifier_p (d1
))
7986 tree value
= innermost_non_namespace_value (d1
);
7987 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
7992 push_decl_namespace (context
);
7993 templ
= lookup_name (d1
);
7994 templ
= maybe_get_template_decl_from_type_decl (templ
);
7996 pop_decl_namespace ();
7999 context
= DECL_CONTEXT (templ
);
8001 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
8003 tree type
= TREE_TYPE (d1
);
8005 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8006 an implicit typename for the second A. Deal with it. */
8007 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
8008 type
= TREE_TYPE (type
);
8010 if (CLASSTYPE_TEMPLATE_INFO (type
))
8012 templ
= CLASSTYPE_TI_TEMPLATE (type
);
8013 d1
= DECL_NAME (templ
);
8016 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
8017 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
8019 templ
= TYPE_TI_TEMPLATE (d1
);
8020 d1
= DECL_NAME (templ
);
8022 else if (DECL_TYPE_TEMPLATE_P (d1
))
8025 d1
= DECL_NAME (templ
);
8026 context
= DECL_CONTEXT (templ
);
8028 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
8031 d1
= DECL_NAME (templ
);
8034 /* Issue an error message if we didn't find a template. */
8037 if (complain
& tf_error
)
8038 error ("%qT is not a template", d1
);
8039 return error_mark_node
;
8042 if (TREE_CODE (templ
) != TEMPLATE_DECL
8043 /* Make sure it's a user visible template, if it was named by
8045 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
8046 && !PRIMARY_TEMPLATE_P (templ
)))
8048 if (complain
& tf_error
)
8050 error ("non-template type %qT used as a template", d1
);
8052 error ("for template declaration %q+D", in_decl
);
8054 return error_mark_node
;
8057 complain
&= ~tf_user
;
8059 /* An alias that just changes the name of a template is equivalent to the
8060 other template, so if any of the arguments are pack expansions, strip
8061 the alias to avoid problems with a pack expansion passed to a non-pack
8062 alias template parameter (DR 1430). */
8063 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
8064 templ
= get_underlying_template (templ
);
8066 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
8068 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
8069 template arguments */
8075 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
8077 /* Consider an example where a template template parameter declared as
8079 template <class T, class U = std::allocator<T> > class TT
8081 The template parameter level of T and U are one level larger than
8082 of TT. To proper process the default argument of U, say when an
8083 instantiation `TT<int>' is seen, we need to build the full
8084 arguments containing {int} as the innermost level. Outer levels,
8085 available when not appearing as default template argument, can be
8086 obtained from the arguments of the enclosing template.
8088 Suppose that TT is later substituted with std::vector. The above
8089 instantiation is `TT<int, std::allocator<T> >' with TT at
8090 level 1, and T at level 2, while the template arguments at level 1
8091 becomes {std::vector} and the inner level 2 is {int}. */
8093 outer
= DECL_CONTEXT (templ
);
8095 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
8096 else if (current_template_parms
)
8097 /* This is an argument of the current template, so we haven't set
8098 DECL_CONTEXT yet. */
8099 outer
= current_template_args ();
8102 arglist
= add_to_template_args (outer
, arglist
);
8104 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
8106 /*require_all_args=*/true,
8107 /*use_default_args=*/true);
8108 if (arglist2
== error_mark_node
8109 || (!uses_template_parms (arglist2
)
8110 && check_instantiated_args (templ
, arglist2
, complain
)))
8111 return error_mark_node
;
8113 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
8118 tree template_type
= TREE_TYPE (templ
);
8121 tree found
= NULL_TREE
;
8124 int is_dependent_type
;
8125 int use_partial_inst_tmpl
= false;
8127 if (template_type
== error_mark_node
)
8128 /* An error occurred while building the template TEMPL, and a
8129 diagnostic has most certainly been emitted for that
8130 already. Let's propagate that error. */
8131 return error_mark_node
;
8133 gen_tmpl
= most_general_template (templ
);
8134 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
8135 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
8136 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8138 if (arg_depth
== 1 && parm_depth
> 1)
8140 /* We've been given an incomplete set of template arguments.
8143 template <class T> struct S1 {
8144 template <class U> struct S2 {};
8145 template <class U> struct S2<U*> {};
8148 we will be called with an ARGLIST of `U*', but the
8149 TEMPLATE will be `template <class T> template
8150 <class U> struct S1<T>::S2'. We must fill in the missing
8153 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
8155 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8158 /* Now we should have enough arguments. */
8159 gcc_assert (parm_depth
== arg_depth
);
8161 /* From here on, we're only interested in the most general
8164 /* Calculate the BOUND_ARGS. These will be the args that are
8165 actually tsubst'd into the definition to create the
8169 /* We have multiple levels of arguments to coerce, at once. */
8171 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
8173 tree bound_args
= make_tree_vec (parm_depth
);
8175 for (i
= saved_depth
,
8176 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
8177 i
> 0 && t
!= NULL_TREE
;
8178 --i
, t
= TREE_CHAIN (t
))
8181 if (i
== saved_depth
)
8182 a
= coerce_template_parms (TREE_VALUE (t
),
8185 /*require_all_args=*/true,
8186 /*use_default_args=*/true);
8188 /* Outer levels should have already been coerced. */
8189 a
= TMPL_ARGS_LEVEL (arglist
, i
);
8191 /* Don't process further if one of the levels fails. */
8192 if (a
== error_mark_node
)
8194 /* Restore the ARGLIST to its full size. */
8195 TREE_VEC_LENGTH (arglist
) = saved_depth
;
8196 return error_mark_node
;
8199 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
8201 /* We temporarily reduce the length of the ARGLIST so
8202 that coerce_template_parms will see only the arguments
8203 corresponding to the template parameters it is
8205 TREE_VEC_LENGTH (arglist
)--;
8208 /* Restore the ARGLIST to its full size. */
8209 TREE_VEC_LENGTH (arglist
) = saved_depth
;
8211 arglist
= bound_args
;
8215 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
8216 INNERMOST_TEMPLATE_ARGS (arglist
),
8219 /*require_all_args=*/true,
8220 /*use_default_args=*/true);
8222 if (arglist
== error_mark_node
)
8223 /* We were unable to bind the arguments. */
8224 return error_mark_node
;
8226 /* In the scope of a template class, explicit references to the
8227 template class refer to the type of the template, not any
8228 instantiation of it. For example, in:
8230 template <class T> class C { void f(C<T>); }
8232 the `C<T>' is just the same as `C'. Outside of the
8233 class, however, such a reference is an instantiation. */
8235 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
8236 || currently_open_class (template_type
))
8237 /* comp_template_args is expensive, check it last. */
8238 && comp_template_args (TYPE_TI_ARGS (template_type
),
8240 return template_type
;
8242 /* If we already have this specialization, return it. */
8243 elt
.tmpl
= gen_tmpl
;
8245 hash
= spec_hasher::hash (&elt
);
8246 entry
= type_specializations
->find_with_hash (&elt
, hash
);
8251 /* If the the template's constraints are not satisfied,
8252 then we cannot form a valid type.
8254 Note that the check is deferred until after the hash
8255 lookup. This prevents redundant checks on previously
8256 instantiated specializations. */
8257 if (flag_concepts
&& !constraints_satisfied_p (gen_tmpl
, arglist
))
8259 if (complain
& tf_error
)
8261 error ("template constraint failure");
8262 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
8264 return error_mark_node
;
8267 is_dependent_type
= uses_template_parms (arglist
);
8269 /* If the deduced arguments are invalid, then the binding
8271 if (!is_dependent_type
8272 && check_instantiated_args (gen_tmpl
,
8273 INNERMOST_TEMPLATE_ARGS (arglist
),
8275 return error_mark_node
;
8277 if (!is_dependent_type
8278 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
8279 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
8280 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
8282 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
8283 DECL_NAME (gen_tmpl
),
8284 /*tag_scope=*/ts_global
);
8288 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
8290 if (context
== error_mark_node
)
8291 return error_mark_node
;
8294 context
= global_namespace
;
8296 /* Create the type. */
8297 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8299 /* The user referred to a specialization of an alias
8300 template represented by GEN_TMPL.
8302 [temp.alias]/2 says:
8304 When a template-id refers to the specialization of an
8305 alias template, it is equivalent to the associated
8306 type obtained by substitution of its
8307 template-arguments for the template-parameters in the
8308 type-id of the alias template. */
8310 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
8311 /* Note that the call above (by indirectly calling
8312 register_specialization in tsubst_decl) registers the
8313 TYPE_DECL representing the specialization of the alias
8314 template. So next time someone substitutes ARGLIST for
8315 the template parms into the alias template (GEN_TMPL),
8316 she'll get that TYPE_DECL back. */
8318 if (t
== error_mark_node
)
8321 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
8323 if (!is_dependent_type
)
8325 set_current_access_from_decl (TYPE_NAME (template_type
));
8326 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
8327 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
8328 arglist
, complain
, in_decl
),
8329 SCOPED_ENUM_P (template_type
), NULL
);
8331 if (t
== error_mark_node
)
8336 /* We don't want to call start_enum for this type, since
8337 the values for the enumeration constants may involve
8338 template parameters. And, no one should be interested
8339 in the enumeration constants for such a type. */
8340 t
= cxx_make_type (ENUMERAL_TYPE
);
8341 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
8343 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
8344 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
8345 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
8347 else if (CLASS_TYPE_P (template_type
))
8349 t
= make_class_type (TREE_CODE (template_type
));
8350 CLASSTYPE_DECLARED_CLASS (t
)
8351 = CLASSTYPE_DECLARED_CLASS (template_type
);
8352 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
8353 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
8355 /* A local class. Make sure the decl gets registered properly. */
8356 if (context
== current_function_decl
)
8357 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
8359 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
8360 /* This instantiation is another name for the primary
8361 template type. Set the TYPE_CANONICAL field
8363 TYPE_CANONICAL (t
) = template_type
;
8364 else if (any_template_arguments_need_structural_equality_p (arglist
))
8365 /* Some of the template arguments require structural
8366 equality testing, so this template class requires
8367 structural equality testing. */
8368 SET_TYPE_STRUCTURAL_EQUALITY (t
);
8373 /* If we called start_enum or pushtag above, this information
8374 will already be set up. */
8377 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
8379 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
8380 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
8381 DECL_SOURCE_LOCATION (type_decl
)
8382 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
8385 type_decl
= TYPE_NAME (t
);
8387 if (CLASS_TYPE_P (template_type
))
8389 TREE_PRIVATE (type_decl
)
8390 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
8391 TREE_PROTECTED (type_decl
)
8392 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
8393 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
8395 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
8396 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
8400 if (OVERLOAD_TYPE_P (t
)
8401 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8404 = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (template_type
)))
8406 if (!TREE_CHAIN (attributes
))
8407 TYPE_ATTRIBUTES (t
) = attributes
;
8410 = build_tree_list (TREE_PURPOSE (attributes
),
8411 TREE_VALUE (attributes
));
8415 /* Let's consider the explicit specialization of a member
8416 of a class template specialization that is implicitly instantiated,
8421 template<class U> struct M {}; //#0
8426 struct S<int>::M<char> //#1
8430 [temp.expl.spec]/4 says this is valid.
8432 In this case, when we write:
8435 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8438 When we encounter #1, we want to store the partial instantiation
8439 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8441 For all cases other than this "explicit specialization of member of a
8442 class template", we just want to store the most general template into
8443 the CLASSTYPE_TI_TEMPLATE of M.
8445 This case of "explicit specialization of member of a class template"
8447 1/ the enclosing class is an instantiation of, and therefore not
8448 the same as, the context of the most general template, and
8449 2/ we aren't looking at the partial instantiation itself, i.e.
8450 the innermost arguments are not the same as the innermost parms of
8451 the most general template.
8453 So it's only when 1/ and 2/ happens that we want to use the partial
8454 instantiation of the member template in lieu of its most general
8457 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
8458 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
8459 /* the enclosing class must be an instantiation... */
8460 && CLASS_TYPE_P (context
)
8461 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
8463 tree partial_inst_args
;
8464 TREE_VEC_LENGTH (arglist
)--;
8465 ++processing_template_decl
;
8467 tsubst (INNERMOST_TEMPLATE_ARGS
8468 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
8469 arglist
, complain
, NULL_TREE
);
8470 --processing_template_decl
;
8471 TREE_VEC_LENGTH (arglist
)++;
8472 use_partial_inst_tmpl
=
8473 /*...and we must not be looking at the partial instantiation
8475 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
8479 if (!use_partial_inst_tmpl
)
8480 /* This case is easy; there are no member templates involved. */
8484 /* This is a full instantiation of a member template. Find
8485 the partial instantiation of which this is an instance. */
8487 /* Temporarily reduce by one the number of levels in the ARGLIST
8488 so as to avoid comparing the last set of arguments. */
8489 TREE_VEC_LENGTH (arglist
)--;
8490 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
8491 TREE_VEC_LENGTH (arglist
)++;
8492 /* FOUND is either a proper class type, or an alias
8493 template specialization. In the later case, it's a
8494 TYPE_DECL, resulting from the substituting of arguments
8495 for parameters in the TYPE_DECL of the alias template
8496 done earlier. So be careful while getting the template
8498 found
= TREE_CODE (found
) == TYPE_DECL
8499 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
8500 : CLASSTYPE_TI_TEMPLATE (found
);
8503 // Build template info for the new specialization.
8504 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
8507 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
8508 entry
= ggc_alloc
<spec_entry
> ();
8512 /* Note this use of the partial instantiation so we can check it
8513 later in maybe_process_partial_specialization. */
8514 DECL_TEMPLATE_INSTANTIATIONS (found
)
8515 = tree_cons (arglist
, t
,
8516 DECL_TEMPLATE_INSTANTIATIONS (found
));
8518 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
8519 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8520 /* Now that the type has been registered on the instantiations
8521 list, we set up the enumerators. Because the enumeration
8522 constants may involve the enumeration type itself, we make
8523 sure to register the type first, and then create the
8524 constants. That way, doing tsubst_expr for the enumeration
8525 constants won't result in recursive calls here; we'll find
8526 the instantiation and exit above. */
8527 tsubst_enum (template_type
, t
, arglist
);
8529 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
8530 /* If the type makes use of template parameters, the
8531 code that generates debugging information will crash. */
8532 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
8534 /* Possibly limit visibility based on template args. */
8535 TREE_PUBLIC (type_decl
) = 1;
8536 determine_visibility (type_decl
);
8538 inherit_targ_abi_tags (t
);
8544 /* Wrapper for lookup_template_class_1. */
8547 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
8548 int entering_scope
, tsubst_flags_t complain
)
8551 timevar_push (TV_TEMPLATE_INST
);
8552 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
8553 entering_scope
, complain
);
8554 timevar_pop (TV_TEMPLATE_INST
);
8558 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST.
8559 The type of the expression is the unknown_type_node since the
8560 template-id could refer to an explicit or partial specialization. */
8563 lookup_template_variable (tree templ
, tree arglist
)
8565 tree type
= unknown_type_node
;
8566 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
8569 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
8572 finish_template_variable (tree var
)
8574 tree templ
= TREE_OPERAND (var
, 0);
8576 tree arglist
= TREE_OPERAND (var
, 1);
8577 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
8578 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
8580 tree parms
= DECL_TEMPLATE_PARMS (templ
);
8581 tsubst_flags_t complain
= tf_warning_or_error
;
8582 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
8584 /*use_default*/true);
8586 /* If a template-id refers to a specialization of a variable
8587 concept, then the expression is true if and only if the
8588 concept's constraints are satisfied by the given template
8591 NOTE: This is an extension of Concepts Lite TS that
8592 allows constraints to be used in expressions. */
8594 if (variable_concept_p (templ
) && !uses_template_parms (arglist
))
8596 tree decl
= DECL_TEMPLATE_RESULT (templ
);
8597 return evaluate_variable_concept (decl
, arglist
);
8600 return instantiate_template (templ
, arglist
, complain
);
8607 /* True when we should also visit template parameters that occur in
8608 non-deduced contexts. */
8609 bool include_nondeduced_p
;
8610 hash_set
<tree
> *visited
;
8613 /* Called from for_each_template_parm via walk_tree. */
8616 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
8619 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
8620 tree_fn_t fn
= pfd
->fn
;
8621 void *data
= pfd
->data
;
8624 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
8625 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
8626 pfd
->include_nondeduced_p
))
8627 return error_mark_node
;
8629 switch (TREE_CODE (t
))
8632 if (TYPE_PTRMEMFUNC_P (t
))
8638 if (!TYPE_TEMPLATE_INFO (t
))
8640 else if (for_each_template_parm (TYPE_TI_ARGS (t
),
8641 fn
, data
, pfd
->visited
,
8642 pfd
->include_nondeduced_p
))
8643 return error_mark_node
;
8647 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
8648 fn
, data
, pfd
->visited
,
8649 pfd
->include_nondeduced_p
)
8650 || for_each_template_parm (TYPE_MAX_VALUE (t
),
8651 fn
, data
, pfd
->visited
,
8652 pfd
->include_nondeduced_p
))
8653 return error_mark_node
;
8657 /* Since we're not going to walk subtrees, we have to do this
8659 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
8660 pfd
->visited
, pfd
->include_nondeduced_p
))
8661 return error_mark_node
;
8665 /* Check the return type. */
8666 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
8667 pfd
->include_nondeduced_p
))
8668 return error_mark_node
;
8670 /* Check the parameter types. Since default arguments are not
8671 instantiated until they are needed, the TYPE_ARG_TYPES may
8672 contain expressions that involve template parameters. But,
8673 no-one should be looking at them yet. And, once they're
8674 instantiated, they don't contain template parameters, so
8675 there's no point in looking at them then, either. */
8679 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
8680 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
8681 pfd
->visited
, pfd
->include_nondeduced_p
))
8682 return error_mark_node
;
8684 /* Since we've already handled the TYPE_ARG_TYPES, we don't
8685 want walk_tree walking into them itself. */
8691 case UNDERLYING_TYPE
:
8692 if (pfd
->include_nondeduced_p
8693 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
8695 pfd
->include_nondeduced_p
))
8696 return error_mark_node
;
8701 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
8702 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
8703 pfd
->visited
, pfd
->include_nondeduced_p
))
8704 return error_mark_node
;
8709 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
8710 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
8711 pfd
->visited
, pfd
->include_nondeduced_p
))
8712 return error_mark_node
;
8713 if (DECL_CONTEXT (t
)
8714 && pfd
->include_nondeduced_p
8715 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
8716 pfd
->visited
, pfd
->include_nondeduced_p
))
8717 return error_mark_node
;
8720 case BOUND_TEMPLATE_TEMPLATE_PARM
:
8721 /* Record template parameters such as `T' inside `TT<T>'. */
8722 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
8723 pfd
->include_nondeduced_p
))
8724 return error_mark_node
;
8727 case TEMPLATE_TEMPLATE_PARM
:
8728 case TEMPLATE_TYPE_PARM
:
8729 case TEMPLATE_PARM_INDEX
:
8730 if (fn
&& (*fn
)(t
, data
))
8731 return error_mark_node
;
8733 return error_mark_node
;
8737 /* A template template parameter is encountered. */
8738 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
8739 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
8740 pfd
->include_nondeduced_p
))
8741 return error_mark_node
;
8743 /* Already substituted template template parameter */
8749 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
8751 pfd
->include_nondeduced_p
))
8752 return error_mark_node
;
8756 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
8757 && pfd
->include_nondeduced_p
8758 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
8759 (TREE_TYPE (t
)), fn
, data
,
8760 pfd
->visited
, pfd
->include_nondeduced_p
))
8761 return error_mark_node
;
8766 /* If there's no type, then this thing must be some expression
8767 involving template parameters. */
8768 if (!fn
&& !TREE_TYPE (t
))
8769 return error_mark_node
;
8774 case IMPLICIT_CONV_EXPR
:
8775 case REINTERPRET_CAST_EXPR
:
8776 case CONST_CAST_EXPR
:
8777 case STATIC_CAST_EXPR
:
8778 case DYNAMIC_CAST_EXPR
:
8782 case PSEUDO_DTOR_EXPR
:
8784 return error_mark_node
;
8791 /* We didn't find any template parameters we liked. */
8795 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8796 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8797 call FN with the parameter and the DATA.
8798 If FN returns nonzero, the iteration is terminated, and
8799 for_each_template_parm returns 1. Otherwise, the iteration
8800 continues. If FN never returns a nonzero value, the value
8801 returned by for_each_template_parm is 0. If FN is NULL, it is
8802 considered to be the function which always returns 1.
8804 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8805 parameters that occur in non-deduced contexts. When false, only
8806 visits those template parameters that can be deduced. */
8809 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
8810 hash_set
<tree
> *visited
,
8811 bool include_nondeduced_p
)
8813 struct pair_fn_data pfd
;
8819 pfd
.include_nondeduced_p
= include_nondeduced_p
;
8821 /* Walk the tree. (Conceptually, we would like to walk without
8822 duplicates, but for_each_template_parm_r recursively calls
8823 for_each_template_parm, so we would need to reorganize a fair
8824 bit to use walk_tree_without_duplicates, so we keep our own
8827 pfd
.visited
= visited
;
8829 pfd
.visited
= new hash_set
<tree
>;
8830 result
= cp_walk_tree (&t
,
8831 for_each_template_parm_r
,
8833 pfd
.visited
) != NULL_TREE
;
8845 /* Returns true if T depends on any template parameter. */
8848 uses_template_parms (tree t
)
8854 int saved_processing_template_decl
;
8856 saved_processing_template_decl
= processing_template_decl
;
8857 if (!saved_processing_template_decl
)
8858 processing_template_decl
= 1;
8860 dependent_p
= dependent_type_p (t
);
8861 else if (TREE_CODE (t
) == TREE_VEC
)
8862 dependent_p
= any_dependent_template_arguments_p (t
);
8863 else if (TREE_CODE (t
) == TREE_LIST
)
8864 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
8865 || uses_template_parms (TREE_CHAIN (t
)));
8866 else if (TREE_CODE (t
) == TYPE_DECL
)
8867 dependent_p
= dependent_type_p (TREE_TYPE (t
));
8870 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
8871 || TREE_CODE (t
) == OVERLOAD
8874 || TREE_CODE (t
) == TRAIT_EXPR
8875 || TREE_CODE (t
) == CONSTRUCTOR
8876 || CONSTANT_CLASS_P (t
))
8877 dependent_p
= (type_dependent_expression_p (t
)
8878 || value_dependent_expression_p (t
));
8881 gcc_assert (t
== error_mark_node
);
8882 dependent_p
= false;
8885 processing_template_decl
= saved_processing_template_decl
;
8890 /* Returns true iff current_function_decl is an incompletely instantiated
8891 template. Useful instead of processing_template_decl because the latter
8892 is set to 0 during instantiate_non_dependent_expr. */
8895 in_template_function (void)
8897 tree fn
= current_function_decl
;
8899 ++processing_template_decl
;
8900 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
8901 && DECL_TEMPLATE_INFO (fn
)
8902 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
8903 --processing_template_decl
;
8907 /* Returns true if T depends on any template parameter with level LEVEL. */
8910 uses_template_parms_level (tree t
, int level
)
8912 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
8913 /*include_nondeduced_p=*/true);
8916 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8917 ill-formed translation unit, i.e. a variable or function that isn't
8918 usable in a constant expression. */
8921 neglectable_inst_p (tree d
)
8924 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
8925 : decl_maybe_constant_var_p (d
)));
8928 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8929 neglectable and instantiated from within an erroneous instantiation. */
8932 limit_bad_template_recursion (tree decl
)
8934 struct tinst_level
*lev
= current_tinst_level
;
8935 int errs
= errorcount
+ sorrycount
;
8936 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
8939 for (; lev
; lev
= lev
->next
)
8940 if (neglectable_inst_p (lev
->decl
))
8943 return (lev
&& errs
> lev
->errors
);
8946 static int tinst_depth
;
8947 extern int max_tinst_depth
;
8950 static GTY(()) struct tinst_level
*last_error_tinst_level
;
8952 /* We're starting to instantiate D; record the template instantiation context
8953 for diagnostics and to restore it later. */
8956 push_tinst_level (tree d
)
8958 return push_tinst_level_loc (d
, input_location
);
8961 /* We're starting to instantiate D; record the template instantiation context
8962 at LOC for diagnostics and to restore it later. */
8965 push_tinst_level_loc (tree d
, location_t loc
)
8967 struct tinst_level
*new_level
;
8969 if (tinst_depth
>= max_tinst_depth
)
8971 fatal_error (input_location
,
8972 "template instantiation depth exceeds maximum of %d"
8973 " (use -ftemplate-depth= to increase the maximum)",
8978 /* If the current instantiation caused problems, don't let it instantiate
8979 anything else. Do allow deduction substitution and decls usable in
8980 constant expressions. */
8981 if (limit_bad_template_recursion (d
))
8984 new_level
= ggc_alloc
<tinst_level
> ();
8985 new_level
->decl
= d
;
8986 new_level
->locus
= loc
;
8987 new_level
->errors
= errorcount
+sorrycount
;
8988 new_level
->in_system_header_p
= in_system_header_at (input_location
);
8989 new_level
->next
= current_tinst_level
;
8990 current_tinst_level
= new_level
;
8993 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
8994 depth_reached
= tinst_depth
;
8999 /* We're done instantiating this template; return to the instantiation
9003 pop_tinst_level (void)
9005 /* Restore the filename and line number stashed away when we started
9006 this instantiation. */
9007 input_location
= current_tinst_level
->locus
;
9008 current_tinst_level
= current_tinst_level
->next
;
9012 /* We're instantiating a deferred template; restore the template
9013 instantiation context in which the instantiation was requested, which
9014 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9017 reopen_tinst_level (struct tinst_level
*level
)
9019 struct tinst_level
*t
;
9022 for (t
= level
; t
; t
= t
->next
)
9025 current_tinst_level
= level
;
9027 if (current_tinst_level
)
9028 current_tinst_level
->errors
= errorcount
+sorrycount
;
9032 /* Returns the TINST_LEVEL which gives the original instantiation
9035 struct tinst_level
*
9036 outermost_tinst_level (void)
9038 struct tinst_level
*level
= current_tinst_level
;
9041 level
= level
->next
;
9045 /* Returns TRUE if the t is a parameter that will be
9046 expanded into a sequence of arguments, when an argument
9047 pack is substituted into it.
9049 These arise from requires-expressions, where substitution
9050 during normalization produces unexpanded parameters.
9053 template<typename T, typename... Args>
9055 return requires (T t, Args... args) {
9060 tempate<typename X, typename... As>
9061 requires C<X, As...>
9064 When S's constraint is normalized the parameterized expression
9065 is a parameter args whose type is the type pack expansion
9066 As... When we instantiate S and provide a concrete argument
9067 pack, we need to make sure that references to args... are
9068 expanded correctly. */
9071 pending_expansion_p (tree t
)
9073 return (TREE_CODE (t
) == PARM_DECL
&& CONSTRAINT_VAR_P (t
)
9074 && PACK_EXPANSION_P (TREE_TYPE (t
)));
9077 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9078 vector of template arguments, as for tsubst.
9080 Returns an appropriate tsubst'd friend declaration. */
9083 tsubst_friend_function (tree decl
, tree args
)
9087 if (TREE_CODE (decl
) == FUNCTION_DECL
9088 && DECL_TEMPLATE_INSTANTIATION (decl
)
9089 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
9090 /* This was a friend declared with an explicit template
9091 argument list, e.g.:
9095 to indicate that f was a template instantiation, not a new
9096 function declaration. Now, we have to figure out what
9097 instantiation of what template. */
9099 tree template_id
, arglist
, fns
;
9102 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
9104 /* Friend functions are looked up in the containing namespace scope.
9105 We must enter that scope, to avoid finding member functions of the
9106 current class with same name. */
9107 push_nested_namespace (ns
);
9108 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
9109 tf_warning_or_error
, NULL_TREE
,
9110 /*integral_constant_expression_p=*/false);
9111 pop_nested_namespace (ns
);
9112 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
9113 tf_warning_or_error
, NULL_TREE
);
9114 template_id
= lookup_template_function (fns
, arglist
);
9116 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9117 tmpl
= determine_specialization (template_id
, new_friend
,
9119 /*need_member_template=*/0,
9120 TREE_VEC_LENGTH (args
),
9122 return instantiate_template (tmpl
, new_args
, tf_error
);
9125 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9127 /* The NEW_FRIEND will look like an instantiation, to the
9128 compiler, but is not an instantiation from the point of view of
9129 the language. For example, we might have had:
9131 template <class T> struct S {
9132 template <class U> friend void f(T, U);
9135 Then, in S<int>, template <class U> void f(int, U) is not an
9136 instantiation of anything. */
9137 if (new_friend
== error_mark_node
)
9138 return error_mark_node
;
9140 DECL_USE_TEMPLATE (new_friend
) = 0;
9141 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9143 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
9144 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
9145 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
9148 /* The mangled name for the NEW_FRIEND is incorrect. The function
9149 is not a template instantiation and should not be mangled like
9150 one. Therefore, we forget the mangling here; we'll recompute it
9151 later if we need it. */
9152 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
9154 SET_DECL_RTL (new_friend
, NULL
);
9155 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
9158 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
9161 tree new_friend_template_info
;
9162 tree new_friend_result_template_info
;
9164 int new_friend_is_defn
;
9166 /* We must save some information from NEW_FRIEND before calling
9167 duplicate decls since that function will free NEW_FRIEND if
9169 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
9170 new_friend_is_defn
=
9171 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9172 (template_for_substitution (new_friend
)))
9174 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
9176 /* This declaration is a `primary' template. */
9177 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
9179 new_friend_result_template_info
9180 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
9183 new_friend_result_template_info
= NULL_TREE
;
9185 /* Make the init_value nonzero so pushdecl knows this is a defn. */
9186 if (new_friend_is_defn
)
9187 DECL_INITIAL (new_friend
) = error_mark_node
;
9189 /* Inside pushdecl_namespace_level, we will push into the
9190 current namespace. However, the friend function should go
9191 into the namespace of the template. */
9192 ns
= decl_namespace_context (new_friend
);
9193 push_nested_namespace (ns
);
9194 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
9195 pop_nested_namespace (ns
);
9197 if (old_decl
== error_mark_node
)
9198 return error_mark_node
;
9200 if (old_decl
!= new_friend
)
9202 /* This new friend declaration matched an existing
9203 declaration. For example, given:
9205 template <class T> void f(T);
9206 template <class U> class C {
9207 template <class T> friend void f(T) {}
9210 the friend declaration actually provides the definition
9211 of `f', once C has been instantiated for some type. So,
9212 old_decl will be the out-of-class template declaration,
9213 while new_friend is the in-class definition.
9215 But, if `f' was called before this point, the
9216 instantiation of `f' will have DECL_TI_ARGS corresponding
9217 to `T' but not to `U', references to which might appear
9218 in the definition of `f'. Previously, the most general
9219 template for an instantiation of `f' was the out-of-class
9220 version; now it is the in-class version. Therefore, we
9221 run through all specialization of `f', adding to their
9222 DECL_TI_ARGS appropriately. In particular, they need a
9223 new set of outer arguments, corresponding to the
9224 arguments for this class instantiation.
9226 The same situation can arise with something like this:
9229 template <class T> class C {
9233 when `C<int>' is instantiated. Now, `f(int)' is defined
9236 if (!new_friend_is_defn
)
9237 /* On the other hand, if the in-class declaration does
9238 *not* provide a definition, then we don't want to alter
9239 existing definitions. We can just leave everything
9244 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
9245 tree new_args
= TI_ARGS (new_friend_template_info
);
9247 /* Overwrite whatever template info was there before, if
9248 any, with the new template information pertaining to
9250 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
9252 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
9254 /* We should have called reregister_specialization in
9256 gcc_assert (retrieve_specialization (new_template
,
9260 /* Instantiate it if the global has already been used. */
9261 if (DECL_ODR_USED (old_decl
))
9262 instantiate_decl (old_decl
, /*defer_ok=*/true,
9263 /*expl_inst_class_mem_p=*/false);
9269 /* Indicate that the old function template is a partial
9271 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
9272 = new_friend_result_template_info
;
9274 gcc_assert (new_template
9275 == most_general_template (new_template
));
9276 gcc_assert (new_template
!= old_decl
);
9278 /* Reassign any specializations already in the hash table
9279 to the new more general template, and add the
9280 additional template args. */
9281 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
9285 tree spec
= TREE_VALUE (t
);
9288 elt
.tmpl
= old_decl
;
9289 elt
.args
= DECL_TI_ARGS (spec
);
9290 elt
.spec
= NULL_TREE
;
9292 decl_specializations
->remove_elt (&elt
);
9295 = add_outermost_template_args (new_args
,
9296 DECL_TI_ARGS (spec
));
9298 register_specialization
9299 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
9302 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
9306 /* The information from NEW_FRIEND has been merged into OLD_DECL
9307 by duplicate_decls. */
9308 new_friend
= old_decl
;
9313 tree context
= DECL_CONTEXT (new_friend
);
9317 template <class T> class C {
9318 template <class U> friend void C1<U>::f (); // case 1
9319 friend void C2<T>::f (); // case 2
9321 we only need to make sure CONTEXT is a complete type for
9322 case 2. To distinguish between the two cases, we note that
9323 CONTEXT of case 1 remains dependent type after tsubst while
9324 this isn't true for case 2. */
9325 ++processing_template_decl
;
9326 dependent_p
= dependent_type_p (context
);
9327 --processing_template_decl
;
9330 && !complete_type_or_else (context
, NULL_TREE
))
9331 return error_mark_node
;
9333 if (COMPLETE_TYPE_P (context
))
9335 tree fn
= new_friend
;
9336 /* do_friend adds the TEMPLATE_DECL for any member friend
9337 template even if it isn't a member template, i.e.
9338 template <class T> friend A<T>::f();
9339 Look through it in that case. */
9340 if (TREE_CODE (fn
) == TEMPLATE_DECL
9341 && !PRIMARY_TEMPLATE_P (fn
))
9342 fn
= DECL_TEMPLATE_RESULT (fn
);
9343 /* Check to see that the declaration is really present, and,
9344 possibly obtain an improved declaration. */
9345 fn
= check_classfn (context
, fn
, NULL_TREE
);
9355 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9356 template arguments, as for tsubst.
9358 Returns an appropriate tsubst'd friend type or error_mark_node on
9362 tsubst_friend_class (tree friend_tmpl
, tree args
)
9368 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
9370 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
9371 return TREE_TYPE (t
);
9374 context
= CP_DECL_CONTEXT (friend_tmpl
);
9376 if (context
!= global_namespace
)
9378 if (TREE_CODE (context
) == NAMESPACE_DECL
)
9379 push_nested_namespace (context
);
9381 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
9384 /* Look for a class template declaration. We look for hidden names
9385 because two friend declarations of the same template are the
9386 same. For example, in:
9389 template <typename> friend class F;
9391 template <typename> struct B {
9392 template <typename> friend class F;
9395 both F templates are the same. */
9396 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
9397 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
9399 /* But, if we don't find one, it might be because we're in a
9400 situation like this:
9408 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9409 for `S<int>', not the TEMPLATE_DECL. */
9410 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
9412 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
9413 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
9416 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
9418 /* The friend template has already been declared. Just
9419 check to see that the declarations match, and install any new
9420 default parameters. We must tsubst the default parameters,
9421 of course. We only need the innermost template parameters
9422 because that is all that redeclare_class_template will look
9424 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
9425 > TMPL_ARGS_DEPTH (args
))
9428 location_t saved_input_location
;
9429 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
9430 args
, tf_warning_or_error
);
9432 saved_input_location
= input_location
;
9433 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
9434 tree cons
= get_constraints (tmpl
);
9435 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
9436 input_location
= saved_input_location
;
9440 friend_type
= TREE_TYPE (tmpl
);
9444 /* The friend template has not already been declared. In this
9445 case, the instantiation of the template class will cause the
9446 injection of this template into the global scope. */
9447 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
9448 if (tmpl
== error_mark_node
)
9449 return error_mark_node
;
9451 /* The new TMPL is not an instantiation of anything, so we
9452 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
9453 the new type because that is supposed to be the corresponding
9454 template decl, i.e., TMPL. */
9455 DECL_USE_TEMPLATE (tmpl
) = 0;
9456 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
9457 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
9458 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
9459 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
9461 /* Inject this template into the global scope. */
9462 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
9465 if (context
!= global_namespace
)
9467 if (TREE_CODE (context
) == NAMESPACE_DECL
)
9468 pop_nested_namespace (context
);
9470 pop_nested_class ();
9476 /* Returns zero if TYPE cannot be completed later due to circularity.
9477 Otherwise returns one. */
9480 can_complete_type_without_circularity (tree type
)
9482 if (type
== NULL_TREE
|| type
== error_mark_node
)
9484 else if (COMPLETE_TYPE_P (type
))
9486 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
9487 return can_complete_type_without_circularity (TREE_TYPE (type
));
9488 else if (CLASS_TYPE_P (type
)
9489 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
9495 static tree
tsubst_omp_clauses (tree
, bool, tree
, tsubst_flags_t
, tree
);
9497 /* Apply any attributes which had to be deferred until instantiation
9498 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
9499 ARGS, COMPLAIN, IN_DECL are as tsubst. */
9502 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
9503 tree args
, tsubst_flags_t complain
, tree in_decl
)
9505 tree last_dep
= NULL_TREE
;
9509 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
9510 if (ATTR_IS_DEPENDENT (t
))
9513 attributes
= copy_list (attributes
);
9517 if (DECL_P (*decl_p
))
9519 if (TREE_TYPE (*decl_p
) == error_mark_node
)
9521 p
= &DECL_ATTRIBUTES (*decl_p
);
9524 p
= &TYPE_ATTRIBUTES (*decl_p
);
9528 tree late_attrs
= NULL_TREE
;
9529 tree
*q
= &late_attrs
;
9531 for (*p
= attributes
; *p
; )
9534 if (ATTR_IS_DEPENDENT (t
))
9536 *p
= TREE_CHAIN (t
);
9537 TREE_CHAIN (t
) = NULL_TREE
;
9538 if ((flag_openmp
|| flag_cilkplus
)
9539 && is_attribute_p ("omp declare simd",
9540 get_attribute_name (t
))
9543 tree clauses
= TREE_VALUE (TREE_VALUE (t
));
9544 clauses
= tsubst_omp_clauses (clauses
, true, args
,
9546 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
9547 clauses
= finish_omp_clauses (clauses
);
9548 tree parms
= DECL_ARGUMENTS (*decl_p
);
9550 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
9552 TREE_VALUE (TREE_VALUE (t
)) = clauses
;
9554 TREE_VALUE (t
) = NULL_TREE
;
9556 /* If the first attribute argument is an identifier, don't
9557 pass it through tsubst. Attributes like mode, format,
9558 cleanup and several target specific attributes expect it
9560 else if (attribute_takes_identifier_p (get_attribute_name (t
))
9564 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
9566 /*integral_constant_expression_p=*/false);
9567 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
9569 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
9574 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
9575 /*integral_constant_expression_p=*/false);
9577 q
= &TREE_CHAIN (t
);
9580 p
= &TREE_CHAIN (t
);
9583 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
9587 /* Perform (or defer) access check for typedefs that were referenced
9588 from within the template TMPL code.
9589 This is a subroutine of instantiate_decl and instantiate_class_template.
9590 TMPL is the template to consider and TARGS is the list of arguments of
9594 perform_typedefs_access_check (tree tmpl
, tree targs
)
9596 location_t saved_location
;
9598 qualified_typedef_usage_t
*iter
;
9601 || (!CLASS_TYPE_P (tmpl
)
9602 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
9605 saved_location
= input_location
;
9606 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
9608 tree type_decl
= iter
->typedef_decl
;
9609 tree type_scope
= iter
->context
;
9611 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
9614 if (uses_template_parms (type_decl
))
9615 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
9616 if (uses_template_parms (type_scope
))
9617 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
9619 /* Make access check error messages point to the location
9620 of the use of the typedef. */
9621 input_location
= iter
->locus
;
9622 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
9623 type_decl
, type_decl
,
9624 tf_warning_or_error
);
9626 input_location
= saved_location
;
9630 instantiate_class_template_1 (tree type
)
9632 tree templ
, args
, pattern
, t
, member
;
9636 unsigned int saved_maximum_field_alignment
;
9639 if (type
== error_mark_node
)
9640 return error_mark_node
;
9642 if (COMPLETE_OR_OPEN_TYPE_P (type
)
9643 || uses_template_parms (type
))
9646 /* Figure out which template is being instantiated. */
9647 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
9648 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
9650 /* Determine what specialization of the original template to
9652 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
9653 if (t
== error_mark_node
)
9655 TYPE_BEING_DEFINED (type
) = 1;
9656 return error_mark_node
;
9660 /* This TYPE is actually an instantiation of a partial
9661 specialization. We replace the innermost set of ARGS with
9662 the arguments appropriate for substitution. For example,
9665 template <class T> struct S {};
9666 template <class T> struct S<T*> {};
9668 and supposing that we are instantiating S<int*>, ARGS will
9669 presently be {int*} -- but we need {int}. */
9670 pattern
= TREE_TYPE (t
);
9671 args
= TREE_PURPOSE (t
);
9675 pattern
= TREE_TYPE (templ
);
9676 args
= CLASSTYPE_TI_ARGS (type
);
9679 /* If the template we're instantiating is incomplete, then clearly
9680 there's nothing we can do. */
9681 if (!COMPLETE_TYPE_P (pattern
))
9684 /* If we've recursively instantiated too many templates, stop. */
9685 if (! push_tinst_level (type
))
9688 /* Now we're really doing the instantiation. Mark the type as in
9689 the process of being defined. */
9690 TYPE_BEING_DEFINED (type
) = 1;
9692 /* We may be in the middle of deferred access check. Disable
9694 push_deferring_access_checks (dk_no_deferred
);
9696 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
9697 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
9698 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
9699 fn_context
= error_mark_node
;
9701 push_to_top_level ();
9702 /* Use #pragma pack from the template context. */
9703 saved_maximum_field_alignment
= maximum_field_alignment
;
9704 maximum_field_alignment
= TYPE_PRECISION (pattern
);
9706 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
9708 /* Set the input location to the most specialized template definition.
9709 This is needed if tsubsting causes an error. */
9710 typedecl
= TYPE_MAIN_DECL (pattern
);
9711 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
9712 DECL_SOURCE_LOCATION (typedecl
);
9714 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
9715 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
9716 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
9717 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
9718 if (ANON_AGGR_TYPE_P (pattern
))
9719 SET_ANON_AGGR_TYPE_P (type
);
9720 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
9722 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
9723 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
9724 /* Adjust visibility for template arguments. */
9725 determine_visibility (TYPE_MAIN_DECL (type
));
9727 if (CLASS_TYPE_P (type
))
9728 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
9730 pbinfo
= TYPE_BINFO (pattern
);
9732 /* We should never instantiate a nested class before its enclosing
9733 class; we need to look up the nested class by name before we can
9734 instantiate it, and that lookup should instantiate the enclosing
9736 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
9737 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
9739 base_list
= NULL_TREE
;
9740 if (BINFO_N_BASE_BINFOS (pbinfo
))
9746 /* We must enter the scope containing the type, as that is where
9747 the accessibility of types named in dependent bases are
9749 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
9751 /* Substitute into each of the bases to determine the actual
9753 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
9756 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
9757 tree expanded_bases
= NULL_TREE
;
9760 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
9763 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
9764 args
, tf_error
, NULL_TREE
);
9765 if (expanded_bases
== error_mark_node
)
9768 len
= TREE_VEC_LENGTH (expanded_bases
);
9771 for (idx
= 0; idx
< len
; idx
++)
9774 /* Extract the already-expanded base class. */
9775 base
= TREE_VEC_ELT (expanded_bases
, idx
);
9777 /* Substitute to figure out the base class. */
9778 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
9781 if (base
== error_mark_node
)
9784 base_list
= tree_cons (access
, base
, base_list
);
9785 if (BINFO_VIRTUAL_P (pbase_binfo
))
9786 TREE_TYPE (base_list
) = integer_type_node
;
9790 /* The list is now in reverse order; correct that. */
9791 base_list
= nreverse (base_list
);
9794 pop_scope (pushed_scope
);
9796 /* Now call xref_basetypes to set up all the base-class
9798 xref_basetypes (type
, base_list
);
9800 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
9801 (int) ATTR_FLAG_TYPE_IN_PLACE
,
9802 args
, tf_error
, NULL_TREE
);
9803 fixup_attribute_variants (type
);
9805 /* Now that our base classes are set up, enter the scope of the
9806 class, so that name lookups into base classes, etc. will work
9807 correctly. This is precisely analogous to what we do in
9808 begin_class_definition when defining an ordinary non-template
9809 class, except we also need to push the enclosing classes. */
9810 push_nested_class (type
);
9812 /* Now members are processed in the order of declaration. */
9813 for (member
= CLASSTYPE_DECL_LIST (pattern
);
9814 member
; member
= TREE_CHAIN (member
))
9816 tree t
= TREE_VALUE (member
);
9818 if (TREE_PURPOSE (member
))
9822 /* Build new CLASSTYPE_NESTED_UTDS. */
9825 bool class_template_p
;
9827 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
9828 && TYPE_LANG_SPECIFIC (t
)
9829 && CLASSTYPE_IS_TEMPLATE (t
));
9830 /* If the member is a class template, then -- even after
9831 substitution -- there may be dependent types in the
9832 template argument list for the class. We increment
9833 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
9834 that function will assume that no types are dependent
9835 when outside of a template. */
9836 if (class_template_p
)
9837 ++processing_template_decl
;
9838 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
9839 if (class_template_p
)
9840 --processing_template_decl
;
9841 if (newtag
== error_mark_node
)
9844 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
9846 tree name
= TYPE_IDENTIFIER (t
);
9848 if (class_template_p
)
9849 /* Unfortunately, lookup_template_class sets
9850 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
9851 instantiation (i.e., for the type of a member
9852 template class nested within a template class.)
9853 This behavior is required for
9854 maybe_process_partial_specialization to work
9855 correctly, but is not accurate in this case;
9856 the TAG is not an instantiation of anything.
9857 (The corresponding TEMPLATE_DECL is an
9858 instantiation, but the TYPE is not.) */
9859 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
9861 /* Now, we call pushtag to put this NEWTAG into the scope of
9862 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
9863 pushtag calling push_template_decl. We don't have to do
9864 this for enums because it will already have been done in
9867 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
9868 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
9871 else if (DECL_DECLARES_FUNCTION_P (t
))
9873 /* Build new TYPE_METHODS. */
9876 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9877 ++processing_template_decl
;
9878 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
9879 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9880 --processing_template_decl
;
9881 set_current_access_from_decl (r
);
9882 finish_member_declaration (r
);
9883 /* Instantiate members marked with attribute used. */
9884 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
9886 if (TREE_CODE (r
) == FUNCTION_DECL
9887 && DECL_OMP_DECLARE_REDUCTION_P (r
))
9888 cp_check_omp_declare_reduction (r
);
9890 else if (DECL_CLASS_TEMPLATE_P (t
)
9891 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
9892 /* A closure type for a lambda in a default argument for a
9893 member template. Ignore it; it will be instantiated with
9894 the default argument. */;
9897 /* Build new TYPE_FIELDS. */
9898 if (TREE_CODE (t
) == STATIC_ASSERT
)
9902 ++c_inhibit_evaluation_warnings
;
9904 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
9905 tf_warning_or_error
, NULL_TREE
,
9906 /*integral_constant_expression_p=*/true);
9907 --c_inhibit_evaluation_warnings
;
9909 finish_static_assert (condition
,
9910 STATIC_ASSERT_MESSAGE (t
),
9911 STATIC_ASSERT_SOURCE_LOCATION (t
),
9914 else if (TREE_CODE (t
) != CONST_DECL
)
9917 tree vec
= NULL_TREE
;
9920 /* The file and line for this declaration, to
9921 assist in error message reporting. Since we
9922 called push_tinst_level above, we don't need to
9924 input_location
= DECL_SOURCE_LOCATION (t
);
9926 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9927 ++processing_template_decl
;
9928 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
9929 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9930 --processing_template_decl
;
9932 if (TREE_CODE (r
) == TREE_VEC
)
9934 /* A capture pack became multiple fields. */
9936 len
= TREE_VEC_LENGTH (vec
);
9939 for (int i
= 0; i
< len
; ++i
)
9942 r
= TREE_VEC_ELT (vec
, i
);
9947 [t]he initialization (and any associated
9948 side-effects) of a static data member does
9949 not occur unless the static data member is
9950 itself used in a way that requires the
9951 definition of the static data member to
9954 Therefore, we do not substitute into the
9955 initialized for the static data member here. */
9956 finish_static_data_member_decl
9959 /*init_const_expr_p=*/false,
9960 /*asmspec_tree=*/NULL_TREE
,
9962 /* Instantiate members marked with attribute used. */
9963 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
9966 else if (TREE_CODE (r
) == FIELD_DECL
)
9968 /* Determine whether R has a valid type and can be
9969 completed later. If R is invalid, then its type
9970 is replaced by error_mark_node. */
9971 tree rtype
= TREE_TYPE (r
);
9972 if (can_complete_type_without_circularity (rtype
))
9973 complete_type (rtype
);
9975 if (!COMPLETE_TYPE_P (rtype
))
9977 cxx_incomplete_type_error (r
, rtype
);
9978 TREE_TYPE (r
) = error_mark_node
;
9982 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9983 such a thing will already have been added to the field
9984 list by tsubst_enum in finish_member_declaration in the
9985 CLASSTYPE_NESTED_UTDS case above. */
9986 if (!(TREE_CODE (r
) == TYPE_DECL
9987 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
9988 && DECL_ARTIFICIAL (r
)))
9990 set_current_access_from_decl (r
);
9991 finish_member_declaration (r
);
9999 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
10000 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10002 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10004 tree friend_type
= t
;
10005 bool adjust_processing_template_decl
= false;
10007 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10009 /* template <class T> friend class C; */
10010 friend_type
= tsubst_friend_class (friend_type
, args
);
10011 adjust_processing_template_decl
= true;
10013 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
10015 /* template <class T> friend class C::D; */
10016 friend_type
= tsubst (friend_type
, args
,
10017 tf_warning_or_error
, NULL_TREE
);
10018 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10019 friend_type
= TREE_TYPE (friend_type
);
10020 adjust_processing_template_decl
= true;
10022 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
10023 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
10025 /* This could be either
10029 when dependent_type_p is false or
10031 template <class U> friend class T::C;
10034 friend_type
= tsubst (friend_type
, args
,
10035 tf_warning_or_error
, NULL_TREE
);
10036 /* Bump processing_template_decl for correct
10037 dependent_type_p calculation. */
10038 ++processing_template_decl
;
10039 if (dependent_type_p (friend_type
))
10040 adjust_processing_template_decl
= true;
10041 --processing_template_decl
;
10043 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
10044 && hidden_name_p (TYPE_NAME (friend_type
)))
10048 where C hasn't been declared yet. Let's lookup name
10049 from namespace scope directly, bypassing any name that
10050 come from dependent base class. */
10051 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
10053 /* The call to xref_tag_from_type does injection for friend
10055 push_nested_namespace (ns
);
10057 xref_tag_from_type (friend_type
, NULL_TREE
,
10058 /*tag_scope=*/ts_current
);
10059 pop_nested_namespace (ns
);
10061 else if (uses_template_parms (friend_type
))
10062 /* friend class C<T>; */
10063 friend_type
= tsubst (friend_type
, args
,
10064 tf_warning_or_error
, NULL_TREE
);
10069 where C is already declared or
10071 friend class C<int>;
10073 We don't have to do anything in these cases. */
10075 if (adjust_processing_template_decl
)
10076 /* Trick make_friend_class into realizing that the friend
10077 we're adding is a template, not an ordinary class. It's
10078 important that we use make_friend_class since it will
10079 perform some error-checking and output cross-reference
10081 ++processing_template_decl
;
10083 if (friend_type
!= error_mark_node
)
10084 make_friend_class (type
, friend_type
, /*complain=*/false);
10086 if (adjust_processing_template_decl
)
10087 --processing_template_decl
;
10091 /* Build new DECL_FRIENDLIST. */
10094 /* The file and line for this declaration, to
10095 assist in error message reporting. Since we
10096 called push_tinst_level above, we don't need to
10098 input_location
= DECL_SOURCE_LOCATION (t
);
10100 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10102 ++processing_template_decl
;
10103 push_deferring_access_checks (dk_no_check
);
10106 r
= tsubst_friend_function (t
, args
);
10107 add_friend (type
, r
, /*complain=*/false);
10108 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10110 pop_deferring_access_checks ();
10111 --processing_template_decl
;
10117 if (tree expr
= CLASSTYPE_LAMBDA_EXPR (type
))
10119 tree decl
= lambda_function (type
);
10122 if (!DECL_TEMPLATE_INFO (decl
)
10123 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl
)) != decl
)
10124 instantiate_decl (decl
, false, false);
10126 /* We need to instantiate the capture list from the template
10127 after we've instantiated the closure members, but before we
10128 consider adding the conversion op. Also keep any captures
10129 that may have been added during instantiation of the op(). */
10130 tree tmpl_expr
= CLASSTYPE_LAMBDA_EXPR (pattern
);
10132 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr
),
10133 args
, tf_warning_or_error
, NULL_TREE
,
10136 LAMBDA_EXPR_CAPTURE_LIST (expr
)
10137 = chainon (tmpl_cap
, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr
)));
10139 maybe_add_lambda_conv_op (type
);
10142 gcc_assert (errorcount
);
10145 /* Set the file and line number information to whatever is given for
10146 the class itself. This puts error messages involving generated
10147 implicit functions at a predictable point, and the same point
10148 that would be used for non-template classes. */
10149 input_location
= DECL_SOURCE_LOCATION (typedecl
);
10151 unreverse_member_declarations (type
);
10152 finish_struct_1 (type
);
10153 TYPE_BEING_DEFINED (type
) = 0;
10155 /* We don't instantiate default arguments for member functions. 14.7.1:
10157 The implicit instantiation of a class template specialization causes
10158 the implicit instantiation of the declarations, but not of the
10159 definitions or default arguments, of the class member functions,
10160 member classes, static data members and member templates.... */
10162 /* Some typedefs referenced from within the template code need to be access
10163 checked at template instantiation time, i.e now. These types were
10164 added to the template at parsing time. Let's get those and perform
10165 the access checks then. */
10166 perform_typedefs_access_check (pattern
, args
);
10167 perform_deferred_access_checks (tf_warning_or_error
);
10168 pop_nested_class ();
10169 maximum_field_alignment
= saved_maximum_field_alignment
;
10171 pop_from_top_level ();
10172 pop_deferring_access_checks ();
10173 pop_tinst_level ();
10175 /* The vtable for a template class can be emitted in any translation
10176 unit in which the class is instantiated. When there is no key
10177 method, however, finish_struct_1 will already have added TYPE to
10178 the keyed_classes list. */
10179 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
10180 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
10185 /* Wrapper for instantiate_class_template_1. */
10188 instantiate_class_template (tree type
)
10191 timevar_push (TV_TEMPLATE_INST
);
10192 ret
= instantiate_class_template_1 (type
);
10193 timevar_pop (TV_TEMPLATE_INST
);
10198 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10204 else if (TYPE_P (t
))
10205 r
= tsubst (t
, args
, complain
, in_decl
);
10208 if (!(complain
& tf_warning
))
10209 ++c_inhibit_evaluation_warnings
;
10210 r
= tsubst_expr (t
, args
, complain
, in_decl
,
10211 /*integral_constant_expression_p=*/true);
10212 if (!(complain
& tf_warning
))
10213 --c_inhibit_evaluation_warnings
;
10218 /* Given a function parameter pack TMPL_PARM and some function parameters
10219 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10220 and set *SPEC_P to point at the next point in the list. */
10223 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
10225 /* Collect all of the extra "packed" parameters into an
10229 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
10230 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
10231 tree spec_parm
= *spec_p
;
10234 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
10236 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
10239 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10240 parmvec
= make_tree_vec (len
);
10241 parmtypevec
= make_tree_vec (len
);
10242 spec_parm
= *spec_p
;
10243 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
10245 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
10246 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
10249 /* Build the argument packs. */
10250 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
10251 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
10252 TREE_TYPE (argpack
) = argtypepack
;
10253 *spec_p
= spec_parm
;
10258 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10259 NONTYPE_ARGUMENT_PACK. */
10262 make_fnparm_pack (tree spec_parm
)
10264 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
10267 /* Return true iff the Ith element of the argument pack ARG_PACK is a
10271 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
10273 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
10274 if (i
>= TREE_VEC_LENGTH (vec
))
10276 return PACK_EXPANSION_P (TREE_VEC_ELT (vec
, i
));
10280 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10283 make_argument_pack_select (tree arg_pack
, unsigned index
)
10285 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
10287 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
10288 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
10293 /* This is a subroutine of tsubst_pack_expansion.
10295 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10296 mechanism to store the (non complete list of) arguments of the
10297 substitution and return a non substituted pack expansion, in order
10298 to wait for when we have enough arguments to really perform the
10302 use_pack_expansion_extra_args_p (tree parm_packs
,
10304 bool has_empty_arg
)
10306 /* If one pack has an expansion and another pack has a normal
10307 argument or if one pack has an empty argument and an another
10308 one hasn't then tsubst_pack_expansion cannot perform the
10309 substitution and need to fall back on the
10310 PACK_EXPANSION_EXTRA mechanism. */
10311 if (parm_packs
== NULL_TREE
)
10313 else if (has_empty_arg
)
10316 bool has_expansion_arg
= false;
10317 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
10319 bool has_non_expansion_arg
= false;
10320 for (tree parm_pack
= parm_packs
;
10322 parm_pack
= TREE_CHAIN (parm_pack
))
10324 tree arg
= TREE_VALUE (parm_pack
);
10326 if (argument_pack_element_is_expansion_p (arg
, i
))
10327 has_expansion_arg
= true;
10329 has_non_expansion_arg
= true;
10332 if (has_expansion_arg
&& has_non_expansion_arg
)
10338 /* [temp.variadic]/6 says that:
10340 The instantiation of a pack expansion [...]
10341 produces a list E1,E2, ..., En, where N is the number of elements
10342 in the pack expansion parameters.
10344 This subroutine of tsubst_pack_expansion produces one of these Ei.
10346 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
10347 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
10348 PATTERN, and each TREE_VALUE is its corresponding argument pack.
10349 INDEX is the index 'i' of the element Ei to produce. ARGS,
10350 COMPLAIN, and IN_DECL are the same parameters as for the
10351 tsubst_pack_expansion function.
10353 The function returns the resulting Ei upon successful completion,
10354 or error_mark_node.
10356 Note that this function possibly modifies the ARGS parameter, so
10357 it's the responsibility of the caller to restore it. */
10360 gen_elem_of_pack_expansion_instantiation (tree pattern
,
10363 tree args
/* This parm gets
10365 tsubst_flags_t complain
,
10369 bool ith_elem_is_expansion
= false;
10371 /* For each parameter pack, change the substitution of the parameter
10372 pack to the ith argument in its argument pack, then expand the
10374 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
10376 tree parm
= TREE_PURPOSE (pack
);
10377 tree arg_pack
= TREE_VALUE (pack
);
10378 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
10380 ith_elem_is_expansion
|=
10381 argument_pack_element_is_expansion_p (arg_pack
, index
);
10383 /* Select the Ith argument from the pack. */
10384 if (TREE_CODE (parm
) == PARM_DECL
10385 || TREE_CODE (parm
) == FIELD_DECL
)
10389 aps
= make_argument_pack_select (arg_pack
, index
);
10391 register_local_specialization (aps
, parm
);
10394 aps
= retrieve_local_specialization (parm
);
10399 template_parm_level_and_index (parm
, &level
, &idx
);
10403 aps
= make_argument_pack_select (arg_pack
, index
);
10404 /* Update the corresponding argument. */
10405 TMPL_ARG (args
, level
, idx
) = aps
;
10408 /* Re-use the ARGUMENT_PACK_SELECT. */
10409 aps
= TMPL_ARG (args
, level
, idx
);
10411 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
10414 /* Substitute into the PATTERN with the (possibly altered)
10416 if (pattern
== in_decl
)
10417 /* Expanding a fixed parameter pack from
10418 coerce_template_parameter_pack. */
10419 t
= tsubst_decl (pattern
, args
, complain
);
10420 else if (!TYPE_P (pattern
))
10421 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
10422 /*integral_constant_expression_p=*/false);
10424 t
= tsubst (pattern
, args
, complain
, in_decl
);
10426 /* If the Ith argument pack element is a pack expansion, then
10427 the Ith element resulting from the substituting is going to
10428 be a pack expansion as well. */
10429 if (ith_elem_is_expansion
)
10430 t
= make_pack_expansion (t
);
10435 /* Substitute args into the pack expansion T, and rewrite the resulting
10436 list as a conjunction of the specified terms. If the result is an empty
10437 expression, return boolean_true_node.
10439 FIXME: This goes away with fold expressions. */
10441 tsubst_pack_conjunction (tree t
, tree args
, tsubst_flags_t complain
,
10444 tree terms
= tsubst_pack_expansion (t
, args
, complain
, in_decl
);
10446 /* If the resulting expression is type- or value-dependent, then
10447 return it after setting the result type to bool (so it can be
10448 expanded as a conjunction). This happens when instantiating
10449 constrained variadic member function templates. Just rebuild
10450 the dependent pack expansion. */
10451 if (instantiation_dependent_expression_p (terms
))
10453 terms
= TREE_VEC_ELT (terms
, 0);
10454 TREE_TYPE (terms
) = boolean_type_node
;
10458 /* Empty expansions are equivalent to 'true'. */
10459 if (TREE_VEC_LENGTH (terms
) == 0)
10460 return boolean_true_node
;
10462 /* Otherwise, and the terms together. These are transformed
10463 into constraints later. */
10464 tree result
= TREE_VEC_ELT (terms
, 0);
10465 for (int i
= 1; i
< TREE_VEC_LENGTH (terms
); ++i
) {
10466 tree t
= TREE_VEC_ELT (terms
, i
);
10467 result
= build_min (TRUTH_ANDIF_EXPR
, boolean_type_node
, result
, t
);
10473 /* Substitute ARGS into T, which is an pack expansion
10474 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
10475 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
10476 (if only a partial substitution could be performed) or
10477 ERROR_MARK_NODE if there was an error. */
10479 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
10483 tree pack
, packs
= NULL_TREE
;
10484 bool unsubstituted_packs
= false;
10487 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
10488 bool need_local_specializations
= false;
10491 /* If T is a parameter declaration pending expansion by
10492 ARGS, substitute through and form an argument pack
10493 for the resulting expression. The sequence of
10494 expressions is contained in the argument pack. */
10495 if (pending_expansion_p (t
))
10497 tree pack
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
10498 /*function_p=*/false,
10499 /*constexpr_p=*/false);
10500 gcc_assert (TREE_CODE (pack
) == NONTYPE_ARGUMENT_PACK
);
10501 return TREE_OPERAND (pack
, 0);
10504 gcc_assert (PACK_EXPANSION_P (t
));
10505 pattern
= PACK_EXPANSION_PATTERN (t
);
10507 /* Add in any args remembered from an earlier partial instantiation. */
10508 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
10510 levels
= TMPL_ARGS_DEPTH (args
);
10512 /* Determine the argument packs that will instantiate the parameter
10513 packs used in the expansion expression. While we're at it,
10514 compute the number of arguments to be expanded and make sure it
10516 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
10517 pack
= TREE_CHAIN (pack
))
10519 tree parm_pack
= TREE_VALUE (pack
);
10520 tree arg_pack
= NULL_TREE
;
10521 tree orig_arg
= NULL_TREE
;
10524 if (TREE_CODE (parm_pack
) == BASES
)
10526 if (BASES_DIRECT (parm_pack
))
10527 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
10528 args
, complain
, in_decl
, false));
10530 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
10531 args
, complain
, in_decl
, false));
10533 if (TREE_CODE (parm_pack
) == PARM_DECL
)
10535 if (PACK_EXPANSION_LOCAL_P (t
))
10536 arg_pack
= retrieve_local_specialization (parm_pack
);
10539 /* We can't rely on local_specializations for a parameter
10540 name used later in a function declaration (such as in a
10541 late-specified return type). Even if it exists, it might
10542 have the wrong value for a recursive call. Just make a
10543 dummy decl, since it's only used for its type. */
10544 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
10545 if (arg_pack
&& DECL_PACK_P (arg_pack
))
10546 /* Partial instantiation of the parm_pack, we can't build
10547 up an argument pack yet. */
10548 arg_pack
= NULL_TREE
;
10550 arg_pack
= make_fnparm_pack (arg_pack
);
10551 need_local_specializations
= true;
10554 else if (TREE_CODE (parm_pack
) == FIELD_DECL
)
10555 arg_pack
= tsubst_copy (parm_pack
, args
, complain
, in_decl
);
10559 template_parm_level_and_index (parm_pack
, &level
, &idx
);
10561 if (level
<= levels
)
10562 arg_pack
= TMPL_ARG (args
, level
, idx
);
10565 orig_arg
= arg_pack
;
10566 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
10567 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
10569 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
10570 /* This can only happen if we forget to expand an argument
10571 pack somewhere else. Just return an error, silently. */
10573 result
= make_tree_vec (1);
10574 TREE_VEC_ELT (result
, 0) = error_mark_node
;
10581 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
10583 /* Don't bother trying to do a partial substitution with
10584 incomplete packs; we'll try again after deduction. */
10585 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
10590 else if (len
!= my_len
)
10592 if (!(complain
& tf_error
))
10593 /* Fail quietly. */;
10594 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
10595 error ("mismatched argument pack lengths while expanding "
10599 error ("mismatched argument pack lengths while expanding "
10602 return error_mark_node
;
10605 /* Keep track of the parameter packs and their corresponding
10607 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
10608 TREE_TYPE (packs
) = orig_arg
;
10612 /* We can't substitute for this parameter pack. We use a flag as
10613 well as the missing_level counter because function parameter
10614 packs don't have a level. */
10615 unsubstituted_packs
= true;
10619 /* If the expansion is just T..., return the matching argument pack. */
10620 if (!unsubstituted_packs
10621 && TREE_PURPOSE (packs
) == pattern
)
10623 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
10624 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
10625 || pack_expansion_args_count (args
))
10627 /* Otherwise use the normal path so we get convert_from_reference. */
10630 /* We cannot expand this expansion expression, because we don't have
10631 all of the argument packs we need. */
10632 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
10634 /* We got some full packs, but we can't substitute them in until we
10635 have values for all the packs. So remember these until then. */
10637 t
= make_pack_expansion (pattern
);
10638 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
10641 else if (unsubstituted_packs
)
10643 /* There were no real arguments, we're just replacing a parameter
10644 pack with another version of itself. Substitute into the
10645 pattern and return a PACK_EXPANSION_*. The caller will need to
10647 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
10648 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
10649 /*integral_constant_expression_p=*/false);
10651 t
= tsubst (pattern
, args
, complain
, in_decl
);
10652 t
= make_pack_expansion (t
);
10656 gcc_assert (len
>= 0);
10658 if (need_local_specializations
)
10660 /* We're in a late-specified return type, so create our own local
10661 specializations map; the current map is either NULL or (in the
10662 case of recursive unification) might have bindings that we don't
10663 want to use or alter. */
10664 saved_local_specializations
= local_specializations
;
10665 local_specializations
= new hash_map
<tree
, tree
>;
10668 /* For each argument in each argument pack, substitute into the
10670 result
= make_tree_vec (len
);
10671 for (i
= 0; i
< len
; ++i
)
10673 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
10677 TREE_VEC_ELT (result
, i
) = t
;
10678 if (t
== error_mark_node
)
10680 result
= error_mark_node
;
10685 /* Update ARGS to restore the substitution from parameter packs to
10686 their argument packs. */
10687 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
10689 tree parm
= TREE_PURPOSE (pack
);
10691 if (TREE_CODE (parm
) == PARM_DECL
10692 || TREE_CODE (parm
) == FIELD_DECL
)
10693 register_local_specialization (TREE_TYPE (pack
), parm
);
10698 if (TREE_VALUE (pack
) == NULL_TREE
)
10701 template_parm_level_and_index (parm
, &level
, &idx
);
10703 /* Update the corresponding argument. */
10704 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
10705 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
10708 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
10712 if (need_local_specializations
)
10714 delete local_specializations
;
10715 local_specializations
= saved_local_specializations
;
10721 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
10722 TMPL. We do this using DECL_PARM_INDEX, which should work even with
10723 parameter packs; all parms generated from a function parameter pack will
10724 have the same DECL_PARM_INDEX. */
10727 get_pattern_parm (tree parm
, tree tmpl
)
10729 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
10732 if (DECL_ARTIFICIAL (parm
))
10734 for (patparm
= DECL_ARGUMENTS (pattern
);
10735 patparm
; patparm
= DECL_CHAIN (patparm
))
10736 if (DECL_ARTIFICIAL (patparm
)
10737 && DECL_NAME (parm
) == DECL_NAME (patparm
))
10742 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
10743 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
10744 gcc_assert (DECL_PARM_INDEX (patparm
)
10745 == DECL_PARM_INDEX (parm
));
10751 /* Substitute ARGS into the vector or list of template arguments T. */
10754 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10757 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
10760 if (t
== error_mark_node
)
10761 return error_mark_node
;
10763 len
= TREE_VEC_LENGTH (t
);
10764 elts
= XALLOCAVEC (tree
, len
);
10766 for (i
= 0; i
< len
; i
++)
10768 tree orig_arg
= TREE_VEC_ELT (t
, i
);
10771 if (TREE_CODE (orig_arg
) == TREE_VEC
)
10772 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
10773 else if (PACK_EXPANSION_P (orig_arg
))
10775 /* Substitute into an expansion expression. */
10776 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
10778 if (TREE_CODE (new_arg
) == TREE_VEC
)
10779 /* Add to the expanded length adjustment the number of
10780 expanded arguments. We subtract one from this
10781 measurement, because the argument pack expression
10782 itself is already counted as 1 in
10783 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
10784 the argument pack is empty. */
10785 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
10787 else if (ARGUMENT_PACK_P (orig_arg
))
10789 /* Substitute into each of the arguments. */
10790 new_arg
= TYPE_P (orig_arg
)
10791 ? cxx_make_type (TREE_CODE (orig_arg
))
10792 : make_node (TREE_CODE (orig_arg
));
10794 SET_ARGUMENT_PACK_ARGS (
10796 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
10797 args
, complain
, in_decl
));
10799 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
10800 new_arg
= error_mark_node
;
10802 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
10803 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
10804 complain
, in_decl
);
10805 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
10807 if (TREE_TYPE (new_arg
) == error_mark_node
)
10808 new_arg
= error_mark_node
;
10812 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
10814 if (new_arg
== error_mark_node
)
10815 return error_mark_node
;
10818 if (new_arg
!= orig_arg
)
10825 /* Make space for the expanded arguments coming from template
10827 t
= make_tree_vec (len
+ expanded_len_adjust
);
10828 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
10829 arguments for a member template.
10830 In that case each TREE_VEC in ORIG_T represents a level of template
10831 arguments, and ORIG_T won't carry any non defaulted argument count.
10832 It will rather be the nested TREE_VECs that will carry one.
10833 In other words, ORIG_T carries a non defaulted argument count only
10834 if it doesn't contain any nested TREE_VEC. */
10835 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
10837 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
10838 count
+= expanded_len_adjust
;
10839 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
10841 for (i
= 0, out
= 0; i
< len
; i
++)
10843 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
10844 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
10845 && TREE_CODE (elts
[i
]) == TREE_VEC
)
10849 /* Now expand the template argument pack "in place". */
10850 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
10851 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
10855 TREE_VEC_ELT (t
, out
) = elts
[i
];
10863 /* Return the result of substituting ARGS into the template parameters
10864 given by PARMS. If there are m levels of ARGS and m + n levels of
10865 PARMS, then the result will contain n levels of PARMS. For
10866 example, if PARMS is `template <class T> template <class U>
10867 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
10868 result will be `template <int*, double, class V>'. */
10871 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
10873 tree r
= NULL_TREE
;
10876 /* When substituting into a template, we must set
10877 PROCESSING_TEMPLATE_DECL as the template parameters may be
10878 dependent if they are based on one-another, and the dependency
10879 predicates are short-circuit outside of templates. */
10880 ++processing_template_decl
;
10882 for (new_parms
= &r
;
10883 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
10884 new_parms
= &(TREE_CHAIN (*new_parms
)),
10885 parms
= TREE_CHAIN (parms
))
10888 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
10891 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
10895 if (parms
== error_mark_node
)
10898 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
10900 if (tuple
== error_mark_node
)
10903 TREE_VEC_ELT (new_vec
, i
) =
10904 tsubst_template_parm (tuple
, args
, complain
);
10908 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
10909 - TMPL_ARGS_DEPTH (args
)),
10910 new_vec
, NULL_TREE
);
10913 --processing_template_decl
;
10918 /* Return the result of substituting ARGS into one template parameter
10919 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
10920 parameter and which TREE_PURPOSE is the default argument of the
10921 template parameter. */
10924 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
10926 tree default_value
, parm_decl
;
10928 if (args
== NULL_TREE
10930 || t
== error_mark_node
)
10933 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
10935 default_value
= TREE_PURPOSE (t
);
10936 parm_decl
= TREE_VALUE (t
);
10938 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
10939 if (TREE_CODE (parm_decl
) == PARM_DECL
10940 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
10941 parm_decl
= error_mark_node
;
10942 default_value
= tsubst_template_arg (default_value
, args
,
10943 complain
, NULL_TREE
);
10945 return build_tree_list (default_value
, parm_decl
);
10948 /* Substitute the ARGS into the indicated aggregate (or enumeration)
10949 type T. If T is not an aggregate or enumeration type, it is
10950 handled as if by tsubst. IN_DECL is as for tsubst. If
10951 ENTERING_SCOPE is nonzero, T is the context for a template which
10952 we are presently tsubst'ing. Return the substituted value. */
10955 tsubst_aggr_type (tree t
,
10957 tsubst_flags_t complain
,
10959 int entering_scope
)
10961 if (t
== NULL_TREE
)
10964 switch (TREE_CODE (t
))
10967 if (TYPE_PTRMEMFUNC_P (t
))
10968 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
10970 /* Else fall through. */
10971 case ENUMERAL_TYPE
:
10973 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
10978 int saved_unevaluated_operand
;
10979 int saved_inhibit_evaluation_warnings
;
10981 /* In "sizeof(X<I>)" we need to evaluate "I". */
10982 saved_unevaluated_operand
= cp_unevaluated_operand
;
10983 cp_unevaluated_operand
= 0;
10984 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
10985 c_inhibit_evaluation_warnings
= 0;
10987 /* First, determine the context for the type we are looking
10989 context
= TYPE_CONTEXT (t
);
10990 if (context
&& TYPE_P (context
))
10992 context
= tsubst_aggr_type (context
, args
, complain
,
10993 in_decl
, /*entering_scope=*/1);
10994 /* If context is a nested class inside a class template,
10995 it may still need to be instantiated (c++/33959). */
10996 context
= complete_type (context
);
10999 /* Then, figure out what arguments are appropriate for the
11000 type we are trying to find. For example, given:
11002 template <class T> struct S;
11003 template <class T, class U> void f(T, U) { S<U> su; }
11005 and supposing that we are instantiating f<int, double>,
11006 then our ARGS will be {int, double}, but, when looking up
11007 S we only want {double}. */
11008 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
11009 complain
, in_decl
);
11010 if (argvec
== error_mark_node
)
11011 r
= error_mark_node
;
11014 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
11015 entering_scope
, complain
);
11016 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11019 cp_unevaluated_operand
= saved_unevaluated_operand
;
11020 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
11025 /* This is not a template type, so there's nothing to do. */
11029 return tsubst (t
, args
, complain
, in_decl
);
11033 /* Substitute into the default argument ARG (a default argument for
11034 FN), which has the indicated TYPE. */
11037 tsubst_default_argument (tree fn
, tree type
, tree arg
, tsubst_flags_t complain
)
11039 tree saved_class_ptr
= NULL_TREE
;
11040 tree saved_class_ref
= NULL_TREE
;
11041 int errs
= errorcount
+ sorrycount
;
11043 /* This can happen in invalid code. */
11044 if (TREE_CODE (arg
) == DEFAULT_ARG
)
11047 /* This default argument came from a template. Instantiate the
11048 default argument here, not in tsubst. In the case of
11057 we must be careful to do name lookup in the scope of S<T>,
11058 rather than in the current class. */
11059 push_access_scope (fn
);
11060 /* The "this" pointer is not valid in a default argument. */
11063 saved_class_ptr
= current_class_ptr
;
11064 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
11065 saved_class_ref
= current_class_ref
;
11066 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
11069 push_deferring_access_checks(dk_no_deferred
);
11070 /* The default argument expression may cause implicitly defined
11071 member functions to be synthesized, which will result in garbage
11072 collection. We must treat this situation as if we were within
11073 the body of function so as to avoid collecting live data on the
11076 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
11077 complain
, NULL_TREE
,
11078 /*integral_constant_expression_p=*/false);
11080 pop_deferring_access_checks();
11082 /* Restore the "this" pointer. */
11085 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
11086 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
11089 if (errorcount
+sorrycount
> errs
11090 && (complain
& tf_warning_or_error
))
11091 inform (input_location
,
11092 " when instantiating default argument for call to %D", fn
);
11094 /* Make sure the default argument is reasonable. */
11095 arg
= check_default_argument (type
, arg
, complain
);
11097 pop_access_scope (fn
);
11102 /* Substitute into all the default arguments for FN. */
11105 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
11110 tmpl_args
= DECL_TI_ARGS (fn
);
11112 /* If this function is not yet instantiated, we certainly don't need
11113 its default arguments. */
11114 if (uses_template_parms (tmpl_args
))
11116 /* Don't do this again for clones. */
11117 if (DECL_CLONED_FUNCTION_P (fn
))
11120 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
11122 arg
= TREE_CHAIN (arg
))
11123 if (TREE_PURPOSE (arg
))
11124 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
11126 TREE_PURPOSE (arg
),
11130 /* Substitute the ARGS into the T, which is a _DECL. Return the
11131 result of the substitution. Issue error and warning messages under
11132 control of COMPLAIN. */
11135 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
11137 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
11138 location_t saved_loc
;
11139 tree r
= NULL_TREE
;
11141 hashval_t hash
= 0;
11143 /* Set the filename and linenumber to improve error-reporting. */
11144 saved_loc
= input_location
;
11145 input_location
= DECL_SOURCE_LOCATION (t
);
11147 switch (TREE_CODE (t
))
11149 case TEMPLATE_DECL
:
11151 /* We can get here when processing a member function template,
11152 member class template, or template template parameter. */
11153 tree decl
= DECL_TEMPLATE_RESULT (t
);
11154 tree type
= TREE_TYPE (t
);
11159 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11161 /* Template template parameter is treated here. */
11162 tree new_type
= tsubst (type
, args
, complain
, in_decl
);
11163 if (new_type
== error_mark_node
)
11164 RETURN (error_mark_node
);
11165 /* If we get a real template back, return it. This can happen in
11166 the context of most_specialized_partial_spec. */
11167 if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
11171 DECL_CHAIN (r
) = NULL_TREE
;
11172 TREE_TYPE (r
) = new_type
;
11173 DECL_TEMPLATE_RESULT (r
)
11174 = build_decl (DECL_SOURCE_LOCATION (decl
),
11175 TYPE_DECL
, DECL_NAME (decl
), new_type
);
11176 DECL_TEMPLATE_PARMS (r
)
11177 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
11179 TYPE_NAME (new_type
) = r
;
11183 /* We might already have an instance of this template.
11184 The ARGS are for the surrounding class type, so the
11185 full args contain the tsubst'd args for the context,
11186 plus the innermost args from the template decl. */
11187 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
11188 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
11189 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
11190 /* Because this is a template, the arguments will still be
11191 dependent, even after substitution. If
11192 PROCESSING_TEMPLATE_DECL is not set, the dependency
11193 predicates will short-circuit. */
11194 ++processing_template_decl
;
11195 full_args
= tsubst_template_args (tmpl_args
, args
,
11196 complain
, in_decl
);
11197 --processing_template_decl
;
11198 if (full_args
== error_mark_node
)
11199 RETURN (error_mark_node
);
11201 /* If this is a default template template argument,
11202 tsubst might not have changed anything. */
11203 if (full_args
== tmpl_args
)
11206 hash
= hash_tmpl_and_args (t
, full_args
);
11207 spec
= retrieve_specialization (t
, full_args
, hash
);
11208 if (spec
!= NULL_TREE
)
11214 /* Make a new template decl. It will be similar to the
11215 original, but will record the current template arguments.
11216 We also create a new function declaration, which is just
11217 like the old one, but points to this new template, rather
11218 than the old one. */
11220 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
11221 DECL_CHAIN (r
) = NULL_TREE
;
11223 // Build new template info linking to the original template decl.
11224 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
11226 if (TREE_CODE (decl
) == TYPE_DECL
11227 && !TYPE_DECL_ALIAS_P (decl
))
11230 ++processing_template_decl
;
11231 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11232 --processing_template_decl
;
11233 if (new_type
== error_mark_node
)
11234 RETURN (error_mark_node
);
11236 TREE_TYPE (r
) = new_type
;
11237 /* For a partial specialization, we need to keep pointing to
11238 the primary template. */
11239 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
11240 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
11241 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
11242 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
11243 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
11248 ++processing_template_decl
;
11249 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
11250 --processing_template_decl
;
11251 if (new_decl
== error_mark_node
)
11252 RETURN (error_mark_node
);
11254 DECL_TEMPLATE_RESULT (r
) = new_decl
;
11255 DECL_TI_TEMPLATE (new_decl
) = r
;
11256 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
11257 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
11258 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
11261 SET_DECL_IMPLICIT_INSTANTIATION (r
);
11262 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
11263 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
11265 /* The template parameters for this new template are all the
11266 template parameters for the old template, except the
11267 outermost level of parameters. */
11268 DECL_TEMPLATE_PARMS (r
)
11269 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
11272 if (PRIMARY_TEMPLATE_P (t
))
11273 DECL_PRIMARY_TEMPLATE (r
) = r
;
11275 if (TREE_CODE (decl
) != TYPE_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
11276 /* Record this non-type partial instantiation. */
11277 register_specialization (r
, t
,
11278 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
11283 case FUNCTION_DECL
:
11286 tree argvec
= NULL_TREE
;
11294 /* Nobody should be tsubst'ing into non-template functions. */
11295 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
11297 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
11302 /* If T is not dependent, just return it. We have to
11303 increment PROCESSING_TEMPLATE_DECL because
11304 value_dependent_expression_p assumes that nothing is
11305 dependent when PROCESSING_TEMPLATE_DECL is zero. */
11306 ++processing_template_decl
;
11307 dependent_p
= value_dependent_expression_p (t
);
11308 --processing_template_decl
;
11312 /* Calculate the most general template of which R is a
11313 specialization, and the complete set of arguments used to
11315 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
11316 argvec
= tsubst_template_args (DECL_TI_ARGS
11317 (DECL_TEMPLATE_RESULT
11318 (DECL_TI_TEMPLATE (t
))),
11319 args
, complain
, in_decl
);
11320 if (argvec
== error_mark_node
)
11321 RETURN (error_mark_node
);
11323 /* Check to see if we already have this specialization. */
11324 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
11325 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
11333 /* We can see more levels of arguments than parameters if
11334 there was a specialization of a member template, like
11337 template <class T> struct S { template <class U> void f(); }
11338 template <> template <class U> void S<int>::f(U);
11340 Here, we'll be substituting into the specialization,
11341 because that's where we can find the code we actually
11342 want to generate, but we'll have enough arguments for
11343 the most general template.
11345 We also deal with the peculiar case:
11347 template <class T> struct S {
11348 template <class U> friend void f();
11350 template <class U> void f() {}
11352 template void f<double>();
11354 Here, the ARGS for the instantiation of will be {int,
11355 double}. But, we only need as many ARGS as there are
11356 levels of template parameters in CODE_PATTERN. We are
11357 careful not to get fooled into reducing the ARGS in
11360 template <class T> struct S { template <class U> void f(U); }
11361 template <class T> template <> void S<T>::f(int) {}
11363 which we can spot because the pattern will be a
11364 specialization in this case. */
11365 args_depth
= TMPL_ARGS_DEPTH (args
);
11367 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
11368 if (args_depth
> parms_depth
11369 && !DECL_TEMPLATE_SPECIALIZATION (t
))
11370 args
= get_innermost_template_args (args
, parms_depth
);
11374 /* This special case arises when we have something like this:
11376 template <class T> struct S {
11377 friend void f<int>(int, double);
11380 Here, the DECL_TI_TEMPLATE for the friend declaration
11381 will be an IDENTIFIER_NODE. We are being called from
11382 tsubst_friend_function, and we want only to create a
11383 new decl (R) with appropriate types so that we can call
11384 determine_specialization. */
11385 gen_tmpl
= NULL_TREE
;
11388 if (DECL_CLASS_SCOPE_P (t
))
11390 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
11394 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
11395 complain
, t
, /*entering_scope=*/1);
11400 ctx
= DECL_CONTEXT (t
);
11402 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11403 if (type
== error_mark_node
)
11404 RETURN (error_mark_node
);
11406 /* If we hit excessive deduction depth, the type is bogus even if
11407 it isn't error_mark_node, so don't build a decl. */
11408 if (excessive_deduction_depth
)
11409 RETURN (error_mark_node
);
11411 /* We do NOT check for matching decls pushed separately at this
11412 point, as they may not represent instantiations of this
11413 template, and in any case are considered separate under the
11416 DECL_USE_TEMPLATE (r
) = 0;
11417 TREE_TYPE (r
) = type
;
11418 /* Clear out the mangled name and RTL for the instantiation. */
11419 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
11420 SET_DECL_RTL (r
, NULL
);
11421 /* Leave DECL_INITIAL set on deleted instantiations. */
11422 if (!DECL_DELETED_FN (r
))
11423 DECL_INITIAL (r
) = NULL_TREE
;
11424 DECL_CONTEXT (r
) = ctx
;
11426 /* OpenMP UDRs have the only argument a reference to the declared
11427 type. We want to diagnose if the declared type is a reference,
11428 which is invalid, but as references to references are usually
11429 quietly merged, diagnose it here. */
11430 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
11433 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
11434 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
11435 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
11436 error_at (DECL_SOURCE_LOCATION (t
),
11437 "reference type %qT in "
11438 "%<#pragma omp declare reduction%>", argtype
);
11439 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
11440 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
11444 if (member
&& DECL_CONV_FN_P (r
))
11445 /* Type-conversion operator. Reconstruct the name, in
11446 case it's the name of one of the template's parameters. */
11447 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
11449 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
11451 DECL_RESULT (r
) = NULL_TREE
;
11453 TREE_STATIC (r
) = 0;
11454 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
11455 DECL_EXTERNAL (r
) = 1;
11456 /* If this is an instantiation of a function with internal
11457 linkage, we already know what object file linkage will be
11458 assigned to the instantiation. */
11459 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
11460 DECL_DEFER_OUTPUT (r
) = 0;
11461 DECL_CHAIN (r
) = NULL_TREE
;
11462 DECL_PENDING_INLINE_INFO (r
) = 0;
11463 DECL_PENDING_INLINE_P (r
) = 0;
11464 DECL_SAVED_TREE (r
) = NULL_TREE
;
11465 DECL_STRUCT_FUNCTION (r
) = NULL
;
11467 /* We'll re-clone as appropriate in instantiate_template. */
11468 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
11470 /* If we aren't complaining now, return on error before we register
11471 the specialization so that we'll complain eventually. */
11472 if ((complain
& tf_error
) == 0
11473 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
11474 && !grok_op_properties (r
, /*complain=*/false))
11475 RETURN (error_mark_node
);
11477 /* When instantiating a constrained member, substitute
11478 into the constraints to create a new constraint into
11480 if (tree ci
= get_constraints (t
)) {
11483 ci
= tsubst_constraint_info (ci
, argvec
, complain
, NULL_TREE
);
11484 set_constraints (r
, ci
);
11488 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
11489 this in the special friend case mentioned above where
11490 GEN_TMPL is NULL. */
11493 DECL_TEMPLATE_INFO (r
) = build_template_info (gen_tmpl
, argvec
);
11494 SET_DECL_IMPLICIT_INSTANTIATION (r
);
11497 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
11499 /* We instantiated this while substituting into
11500 the type earlier (template/friend54.C). */
11503 /* We're not supposed to instantiate default arguments
11504 until they are called, for a template. But, for a
11507 template <class T> void f ()
11508 { extern void g(int i = T()); }
11510 we should do the substitution when the template is
11511 instantiated. We handle the member function case in
11512 instantiate_class_template since the default arguments
11513 might refer to other members of the class. */
11515 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
11516 && !uses_template_parms (argvec
))
11517 tsubst_default_arguments (r
, complain
);
11520 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
11522 /* Copy the list of befriending classes. */
11523 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
11525 friends
= &TREE_CHAIN (*friends
))
11527 *friends
= copy_node (*friends
);
11528 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
11533 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
11535 maybe_retrofit_in_chrg (r
);
11536 if (DECL_CONSTRUCTOR_P (r
))
11537 grok_ctor_properties (ctx
, r
);
11539 if (DECL_INHERITED_CTOR_BASE (r
))
11540 deduce_inheriting_ctor (r
);
11542 /* If this is an instantiation of a member template, clone it.
11543 If it isn't, that'll be handled by
11544 clone_constructors_and_destructors. */
11545 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
11546 clone_function_decl (r
, /*update_method_vec_p=*/0);
11548 else if ((complain
& tf_error
) != 0
11549 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
11550 && !grok_op_properties (r
, /*complain=*/true))
11551 RETURN (error_mark_node
);
11553 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
11554 SET_DECL_FRIEND_CONTEXT (r
,
11555 tsubst (DECL_FRIEND_CONTEXT (t
),
11556 args
, complain
, in_decl
));
11558 /* Possibly limit visibility based on template args. */
11559 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
11560 if (DECL_VISIBILITY_SPECIFIED (t
))
11562 DECL_VISIBILITY_SPECIFIED (r
) = 0;
11563 DECL_ATTRIBUTES (r
)
11564 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
11566 determine_visibility (r
);
11567 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
11568 && !processing_template_decl
)
11569 defaulted_late_check (r
);
11571 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
11572 args
, complain
, in_decl
);
11578 tree type
= NULL_TREE
;
11580 tree expanded_types
= NULL_TREE
;
11581 tree prev_r
= NULL_TREE
;
11582 tree first_r
= NULL_TREE
;
11584 if (DECL_PACK_P (t
))
11586 /* If there is a local specialization that isn't a
11587 parameter pack, it means that we're doing a "simple"
11588 substitution from inside tsubst_pack_expansion. Just
11589 return the local specialization (which will be a single
11591 tree spec
= retrieve_local_specialization (t
);
11593 && TREE_CODE (spec
) == PARM_DECL
11594 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
11597 /* Expand the TYPE_PACK_EXPANSION that provides the types for
11598 the parameters in this function parameter pack. */
11599 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
11600 complain
, in_decl
);
11601 if (TREE_CODE (expanded_types
) == TREE_VEC
)
11603 len
= TREE_VEC_LENGTH (expanded_types
);
11605 /* Zero-length parameter packs are boring. Just substitute
11608 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
11613 /* All we did was update the type. Make a note of that. */
11614 type
= expanded_types
;
11615 expanded_types
= NULL_TREE
;
11619 /* Loop through all of the parameters we'll build. When T is
11620 a function parameter pack, LEN is the number of expanded
11621 types in EXPANDED_TYPES; otherwise, LEN is 1. */
11623 for (i
= 0; i
< len
; ++i
)
11627 if (DECL_TEMPLATE_PARM_P (t
))
11628 SET_DECL_TEMPLATE_PARM_P (r
);
11630 if (expanded_types
)
11631 /* We're on the Ith parameter of the function parameter
11634 /* Get the Ith type. */
11635 type
= TREE_VEC_ELT (expanded_types
, i
);
11637 /* Rename the parameter to include the index. */
11639 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
11642 /* We're dealing with a normal parameter. */
11643 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11645 type
= type_decays_to (type
);
11646 TREE_TYPE (r
) = type
;
11647 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
11649 if (DECL_INITIAL (r
))
11651 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
11652 DECL_INITIAL (r
) = TREE_TYPE (r
);
11654 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
11655 complain
, in_decl
);
11658 DECL_CONTEXT (r
) = NULL_TREE
;
11660 if (!DECL_TEMPLATE_PARM_P (r
))
11661 DECL_ARG_TYPE (r
) = type_passed_as (type
);
11663 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
11664 args
, complain
, in_decl
);
11666 /* Keep track of the first new parameter we
11667 generate. That's what will be returned to the
11672 /* Build a proper chain of parameters when substituting
11673 into a function parameter pack. */
11675 DECL_CHAIN (prev_r
) = r
;
11678 /* If cp_unevaluated_operand is set, we're just looking for a
11679 single dummy parameter, so don't keep going. */
11680 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
11681 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
11682 complain
, DECL_CHAIN (t
));
11684 /* FIRST_R contains the start of the chain we've built. */
11691 tree type
= NULL_TREE
;
11692 tree vec
= NULL_TREE
;
11693 tree expanded_types
= NULL_TREE
;
11696 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
11698 /* This field is a lambda capture pack. Return a TREE_VEC of
11699 the expanded fields to instantiate_class_template_1 and
11700 store them in the specializations hash table as a
11701 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
11702 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
11703 complain
, in_decl
);
11704 if (TREE_CODE (expanded_types
) == TREE_VEC
)
11706 len
= TREE_VEC_LENGTH (expanded_types
);
11707 vec
= make_tree_vec (len
);
11711 /* All we did was update the type. Make a note of that. */
11712 type
= expanded_types
;
11713 expanded_types
= NULL_TREE
;
11717 for (int i
= 0; i
< len
; ++i
)
11720 if (expanded_types
)
11722 type
= TREE_VEC_ELT (expanded_types
, i
);
11724 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
11727 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11729 if (type
== error_mark_node
)
11730 RETURN (error_mark_node
);
11731 TREE_TYPE (r
) = type
;
11732 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
11734 if (DECL_C_BIT_FIELD (r
))
11735 /* For bit-fields, DECL_INITIAL gives the number of bits. For
11736 non-bit-fields DECL_INITIAL is a non-static data member
11737 initializer, which gets deferred instantiation. */
11739 = tsubst_expr (DECL_INITIAL (t
), args
,
11741 /*integral_constant_expression_p=*/true);
11742 else if (DECL_INITIAL (t
))
11744 /* Set up DECL_TEMPLATE_INFO so that we can get at the
11745 NSDMI in perform_member_init. Still set DECL_INITIAL
11746 so that we know there is one. */
11747 DECL_INITIAL (r
) = void_node
;
11748 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
11749 retrofit_lang_decl (r
);
11750 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
11752 /* We don't have to set DECL_CONTEXT here; it is set by
11753 finish_member_declaration. */
11754 DECL_CHAIN (r
) = NULL_TREE
;
11756 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
11757 args
, complain
, in_decl
);
11760 TREE_VEC_ELT (vec
, i
) = r
;
11766 tree pack
= make_node (NONTYPE_ARGUMENT_PACK
);
11767 tree tpack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
11768 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
11769 SET_ARGUMENT_PACK_ARGS (tpack
, expanded_types
);
11770 TREE_TYPE (pack
) = tpack
;
11771 register_specialization (pack
, t
, args
, false, 0);
11777 /* We reach here only for member using decls. We also need to check
11778 uses_template_parms because DECL_DEPENDENT_P is not set for a
11779 using-declaration that designates a member of the current
11780 instantiation (c++/53549). */
11781 if (DECL_DEPENDENT_P (t
)
11782 || uses_template_parms (USING_DECL_SCOPE (t
)))
11784 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
11785 complain
, in_decl
);
11786 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
11787 r
= do_class_using_decl (inst_scope
, name
);
11789 r
= error_mark_node
;
11792 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
11793 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
11799 DECL_CHAIN (r
) = NULL_TREE
;
11806 tree argvec
= NULL_TREE
;
11807 tree gen_tmpl
= NULL_TREE
;
11809 tree tmpl
= NULL_TREE
;
11811 tree type
= NULL_TREE
;
11814 if (TREE_TYPE (t
) == error_mark_node
)
11815 RETURN (error_mark_node
);
11817 if (TREE_CODE (t
) == TYPE_DECL
11818 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
11820 /* If this is the canonical decl, we don't have to
11821 mess with instantiations, and often we can't (for
11822 typename, template type parms and such). Note that
11823 TYPE_NAME is not correct for the above test if
11824 we've copied the type for a typedef. */
11825 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11826 if (type
== error_mark_node
)
11827 RETURN (error_mark_node
);
11828 r
= TYPE_NAME (type
);
11832 /* Check to see if we already have the specialization we
11835 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
11837 /* T is a static data member or namespace-scope entity.
11838 We have to substitute into namespace-scope variables
11839 (even though such entities are never templates) because
11842 template <class T> void f() { extern T t; }
11844 where the entity referenced is not known until
11845 instantiation time. */
11847 ctx
= DECL_CONTEXT (t
);
11848 if (DECL_CLASS_SCOPE_P (t
))
11850 ctx
= tsubst_aggr_type (ctx
, args
,
11852 in_decl
, /*entering_scope=*/1);
11853 /* If CTX is unchanged, then T is in fact the
11854 specialization we want. That situation occurs when
11855 referencing a static data member within in its own
11856 class. We can use pointer equality, rather than
11857 same_type_p, because DECL_CONTEXT is always
11859 if (ctx
== DECL_CONTEXT (t
)
11860 /* ... unless T is a member template; in which
11861 case our caller can be willing to create a
11862 specialization of that template represented
11865 NOTE: The extra check for DECL_TEMPLATE_INFO
11866 is apparenly needed when checking constraints
11867 for associated type names when the requirement
11868 is satisfied by an alias declaration. */
11869 && !(DECL_TEMPLATE_INFO (t
)
11870 && DECL_TI_TEMPLATE (t
)
11871 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
11875 if (!spec
&& DECL_LANG_SPECIFIC (t
))
11877 tmpl
= DECL_TI_TEMPLATE (t
);
11878 gen_tmpl
= most_general_template (tmpl
);
11879 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
11880 if (argvec
== error_mark_node
)
11881 RETURN (error_mark_node
);
11882 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
11883 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
11888 /* A local variable. */
11890 /* Subsequent calls to pushdecl will fill this in. */
11892 spec
= retrieve_local_specialization (t
);
11894 /* If we already have the specialization we need, there is
11895 nothing more to do. */
11902 /* Create a new node for the specialization we need. */
11904 if (type
== NULL_TREE
)
11906 if (is_typedef_decl (t
))
11907 type
= DECL_ORIGINAL_TYPE (t
);
11909 type
= TREE_TYPE (t
);
11911 && VAR_HAD_UNKNOWN_BOUND (t
)
11912 && type
!= error_mark_node
)
11913 type
= strip_array_domain (type
);
11914 type
= tsubst (type
, args
, complain
, in_decl
);
11918 /* Even if the original location is out of scope, the
11919 newly substituted one is not. */
11920 DECL_DEAD_FOR_LOCAL (r
) = 0;
11921 DECL_INITIALIZED_P (r
) = 0;
11922 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
11923 if (type
== error_mark_node
)
11924 RETURN (error_mark_node
);
11925 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11927 /* It may seem that this case cannot occur, since:
11932 declares a function, not a variable. However:
11935 template <typename T> void g() { T t; }
11936 template void g<f>();
11938 is an attempt to declare a variable with function
11940 error ("variable %qD has function type",
11941 /* R is not yet sufficiently initialized, so we
11942 just use its name. */
11944 RETURN (error_mark_node
);
11946 type
= complete_type (type
);
11947 /* Wait until cp_finish_decl to set this again, to handle
11948 circular dependency (template/instantiate6.C). */
11949 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
11950 type
= check_var_type (DECL_NAME (r
), type
);
11952 if (DECL_HAS_VALUE_EXPR_P (t
))
11954 tree ve
= DECL_VALUE_EXPR (t
);
11955 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
11956 /*constant_expression_p=*/false);
11957 if (REFERENCE_REF_P (ve
))
11959 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
11960 ve
= TREE_OPERAND (ve
, 0);
11962 SET_DECL_VALUE_EXPR (r
, ve
);
11964 if (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
11965 set_decl_tls_model (r
, decl_tls_model (t
));
11967 else if (DECL_SELF_REFERENCE_P (t
))
11968 SET_DECL_SELF_REFERENCE_P (r
);
11969 TREE_TYPE (r
) = type
;
11970 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
11971 DECL_CONTEXT (r
) = ctx
;
11972 /* Clear out the mangled name and RTL for the instantiation. */
11973 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
11974 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
11975 SET_DECL_RTL (r
, NULL
);
11976 /* The initializer must not be expanded until it is required;
11977 see [temp.inst]. */
11978 DECL_INITIAL (r
) = NULL_TREE
;
11979 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
11980 SET_DECL_RTL (r
, NULL
);
11981 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
11984 /* Possibly limit visibility based on template args. */
11985 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
11986 if (DECL_VISIBILITY_SPECIFIED (t
))
11988 DECL_VISIBILITY_SPECIFIED (r
) = 0;
11989 DECL_ATTRIBUTES (r
)
11990 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
11992 determine_visibility (r
);
11995 if (!local_p
&& DECL_LANG_SPECIFIC (r
))
11997 /* A static data member declaration is always marked
11998 external when it is declared in-class, even if an
11999 initializer is present. We mimic the non-template
12000 processing here. */
12001 DECL_EXTERNAL (r
) = 1;
12003 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
12005 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
12006 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12008 else if (!cp_unevaluated_operand
)
12009 register_local_specialization (r
, t
);
12011 DECL_CHAIN (r
) = NULL_TREE
;
12013 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
12015 args
, complain
, in_decl
);
12017 /* Preserve a typedef that names a type. */
12018 if (is_typedef_decl (r
))
12020 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
12021 set_underlying_type (r
);
12024 layout_decl (r
, 0);
12029 gcc_unreachable ();
12034 /* Restore the file and line information. */
12035 input_location
= saved_loc
;
12040 /* Substitute into the ARG_TYPES of a function type.
12041 If END is a TREE_CHAIN, leave it and any following types
12045 tsubst_arg_types (tree arg_types
,
12048 tsubst_flags_t complain
,
12051 tree remaining_arg_types
;
12052 tree type
= NULL_TREE
;
12054 tree expanded_args
= NULL_TREE
;
12057 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
12060 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
12061 args
, end
, complain
, in_decl
);
12062 if (remaining_arg_types
== error_mark_node
)
12063 return error_mark_node
;
12065 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
12067 /* For a pack expansion, perform substitution on the
12068 entire expression. Later on, we'll handle the arguments
12070 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
12071 args
, complain
, in_decl
);
12073 if (TREE_CODE (expanded_args
) == TREE_VEC
)
12074 /* So that we'll spin through the parameters, one by one. */
12075 i
= TREE_VEC_LENGTH (expanded_args
);
12078 /* We only partially substituted into the parameter
12079 pack. Our type is TYPE_PACK_EXPANSION. */
12080 type
= expanded_args
;
12081 expanded_args
= NULL_TREE
;
12089 type
= TREE_VEC_ELT (expanded_args
, i
);
12091 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
12093 if (type
== error_mark_node
)
12094 return error_mark_node
;
12095 if (VOID_TYPE_P (type
))
12097 if (complain
& tf_error
)
12099 error ("invalid parameter type %qT", type
);
12101 error ("in declaration %q+D", in_decl
);
12103 return error_mark_node
;
12106 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
12107 return error_mark_node
;
12109 /* Do array-to-pointer, function-to-pointer conversion, and ignore
12110 top-level qualifiers as required. */
12111 type
= cv_unqualified (type_decays_to (type
));
12113 /* We do not substitute into default arguments here. The standard
12114 mandates that they be instantiated only when needed, which is
12115 done in build_over_call. */
12116 default_arg
= TREE_PURPOSE (arg_types
);
12118 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
12120 /* We've instantiated a template before its default arguments
12121 have been parsed. This can happen for a nested template
12122 class, and is not an error unless we require the default
12123 argument in a call of this function. */
12124 remaining_arg_types
=
12125 tree_cons (default_arg
, type
, remaining_arg_types
);
12126 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
12129 remaining_arg_types
=
12130 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
12133 return remaining_arg_types
;
12136 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
12137 *not* handle the exception-specification for FNTYPE, because the
12138 initial substitution of explicitly provided template parameters
12139 during argument deduction forbids substitution into the
12140 exception-specification:
12144 All references in the function type of the function template to the
12145 corresponding template parameters are replaced by the specified tem-
12146 plate argument values. If a substitution in a template parameter or
12147 in the function type of the function template results in an invalid
12148 type, type deduction fails. [Note: The equivalent substitution in
12149 exception specifications is done only when the function is instanti-
12150 ated, at which point a program is ill-formed if the substitution
12151 results in an invalid type.] */
12154 tsubst_function_type (tree t
,
12156 tsubst_flags_t complain
,
12160 tree arg_types
= NULL_TREE
;
12163 /* The TYPE_CONTEXT is not used for function/method types. */
12164 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
12166 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
12168 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
12170 if (late_return_type_p
)
12172 /* Substitute the argument types. */
12173 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
12174 complain
, in_decl
);
12175 if (arg_types
== error_mark_node
)
12176 return error_mark_node
;
12178 tree save_ccp
= current_class_ptr
;
12179 tree save_ccr
= current_class_ref
;
12180 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
12181 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
12182 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
12185 /* DR 1207: 'this' is in scope in the trailing return type. */
12186 inject_this_parameter (this_type
, cp_type_quals (this_type
));
12189 /* Substitute the return type. */
12190 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12194 current_class_ptr
= save_ccp
;
12195 current_class_ref
= save_ccr
;
12199 /* Substitute the return type. */
12200 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12202 if (return_type
== error_mark_node
)
12203 return error_mark_node
;
12204 /* DR 486 clarifies that creation of a function type with an
12205 invalid return type is a deduction failure. */
12206 if (TREE_CODE (return_type
) == ARRAY_TYPE
12207 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
12209 if (complain
& tf_error
)
12211 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
12212 error ("function returning an array");
12214 error ("function returning a function");
12216 return error_mark_node
;
12219 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
12220 return error_mark_node
;
12222 if (!late_return_type_p
)
12224 /* Substitute the argument types. */
12225 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
12226 complain
, in_decl
);
12227 if (arg_types
== error_mark_node
)
12228 return error_mark_node
;
12231 /* Construct a new type node and return it. */
12232 if (TREE_CODE (t
) == FUNCTION_TYPE
)
12234 fntype
= build_function_type (return_type
, arg_types
);
12235 fntype
= apply_memfn_quals (fntype
,
12236 type_memfn_quals (t
),
12237 type_memfn_rqual (t
));
12241 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
12242 /* Don't pick up extra function qualifiers from the basetype. */
12243 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
12244 if (! MAYBE_CLASS_TYPE_P (r
))
12248 Type deduction may fail for any of the following
12251 -- Attempting to create "pointer to member of T" when T
12252 is not a class type. */
12253 if (complain
& tf_error
)
12254 error ("creating pointer to member function of non-class type %qT",
12256 return error_mark_node
;
12259 fntype
= build_method_type_directly (r
, return_type
,
12260 TREE_CHAIN (arg_types
));
12261 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
12263 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
12265 if (late_return_type_p
)
12266 TYPE_HAS_LATE_RETURN_TYPE (fntype
) = 1;
12271 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
12272 ARGS into that specification, and return the substituted
12273 specification. If there is no specification, return NULL_TREE. */
12276 tsubst_exception_specification (tree fntype
,
12278 tsubst_flags_t complain
,
12285 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
12286 new_specs
= NULL_TREE
;
12287 if (specs
&& TREE_PURPOSE (specs
))
12289 /* A noexcept-specifier. */
12290 tree expr
= TREE_PURPOSE (specs
);
12291 if (TREE_CODE (expr
) == INTEGER_CST
)
12295 /* Defer instantiation of noexcept-specifiers to avoid
12296 excessive instantiations (c++/49107). */
12297 new_specs
= make_node (DEFERRED_NOEXCEPT
);
12298 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
12300 /* We already partially instantiated this member template,
12301 so combine the new args with the old. */
12302 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
12303 = DEFERRED_NOEXCEPT_PATTERN (expr
);
12304 DEFERRED_NOEXCEPT_ARGS (new_specs
)
12305 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
12309 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
12310 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
12314 new_specs
= tsubst_copy_and_build
12315 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
12316 /*integral_constant_expression_p=*/true);
12317 new_specs
= build_noexcept_spec (new_specs
, complain
);
12321 if (! TREE_VALUE (specs
))
12328 tree expanded_specs
= NULL_TREE
;
12330 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
12332 /* Expand the pack expansion type. */
12333 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
12337 if (expanded_specs
== error_mark_node
)
12338 return error_mark_node
;
12339 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
12340 len
= TREE_VEC_LENGTH (expanded_specs
);
12343 /* We're substituting into a member template, so
12344 we got a TYPE_PACK_EXPANSION back. Add that
12345 expansion and move on. */
12346 gcc_assert (TREE_CODE (expanded_specs
)
12347 == TYPE_PACK_EXPANSION
);
12348 new_specs
= add_exception_specifier (new_specs
,
12351 specs
= TREE_CHAIN (specs
);
12356 for (i
= 0; i
< len
; ++i
)
12358 if (expanded_specs
)
12359 spec
= TREE_VEC_ELT (expanded_specs
, i
);
12361 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
12362 if (spec
== error_mark_node
)
12364 new_specs
= add_exception_specifier (new_specs
, spec
,
12368 specs
= TREE_CHAIN (specs
);
12374 /* Take the tree structure T and replace template parameters used
12375 therein with the argument vector ARGS. IN_DECL is an associated
12376 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
12377 Issue error and warning messages under control of COMPLAIN. Note
12378 that we must be relatively non-tolerant of extensions here, in
12379 order to preserve conformance; if we allow substitutions that
12380 should not be allowed, we may allow argument deductions that should
12381 not succeed, and therefore report ambiguous overload situations
12382 where there are none. In theory, we could allow the substitution,
12383 but indicate that it should have failed, and allow our caller to
12384 make sure that the right thing happens, but we don't try to do this
12387 This function is used for dealing with types, decls and the like;
12388 for expressions, use tsubst_expr or tsubst_copy. */
12391 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12393 enum tree_code code
;
12394 tree type
, r
= NULL_TREE
;
12396 if (t
== NULL_TREE
|| t
== error_mark_node
12397 || t
== integer_type_node
12398 || t
== void_type_node
12399 || t
== char_type_node
12400 || t
== unknown_type_node
12401 || TREE_CODE (t
) == NAMESPACE_DECL
12402 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
12406 return tsubst_decl (t
, args
, complain
);
12408 if (args
== NULL_TREE
)
12411 code
= TREE_CODE (t
);
12413 if (code
== IDENTIFIER_NODE
)
12414 type
= IDENTIFIER_TYPE_VALUE (t
);
12416 type
= TREE_TYPE (t
);
12418 gcc_assert (type
!= unknown_type_node
);
12420 /* Reuse typedefs. We need to do this to handle dependent attributes,
12421 such as attribute aligned. */
12423 && typedef_variant_p (t
))
12425 tree decl
= TYPE_NAME (t
);
12427 if (alias_template_specialization_p (t
))
12429 /* DECL represents an alias template and we want to
12431 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
12432 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
12433 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
12435 else if (DECL_CLASS_SCOPE_P (decl
)
12436 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
12437 && uses_template_parms (DECL_CONTEXT (decl
)))
12439 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
12440 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
12441 r
= retrieve_specialization (tmpl
, gen_args
, 0);
12443 else if (DECL_FUNCTION_SCOPE_P (decl
)
12444 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
12445 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
12446 r
= retrieve_local_specialization (decl
);
12448 /* The typedef is from a non-template context. */
12454 r
= cp_build_qualified_type_real
12455 (r
, cp_type_quals (t
) | cp_type_quals (r
),
12456 complain
| tf_ignore_bad_quals
);
12461 /* We don't have an instantiation yet, so drop the typedef. */
12462 int quals
= cp_type_quals (t
);
12463 t
= DECL_ORIGINAL_TYPE (decl
);
12464 t
= cp_build_qualified_type_real (t
, quals
,
12465 complain
| tf_ignore_bad_quals
);
12470 && code
!= TYPENAME_TYPE
12471 && code
!= TEMPLATE_TYPE_PARM
12472 && code
!= IDENTIFIER_NODE
12473 && code
!= FUNCTION_TYPE
12474 && code
!= METHOD_TYPE
)
12475 type
= tsubst (type
, args
, complain
, in_decl
);
12476 if (type
== error_mark_node
)
12477 return error_mark_node
;
12483 case ENUMERAL_TYPE
:
12484 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
12485 /*entering_scope=*/0);
12488 case IDENTIFIER_NODE
:
12499 if (t
== integer_type_node
)
12502 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
12503 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
12507 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
12509 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
12510 /*integral_constant_expression_p=*/false);
12512 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
12514 if (TREE_CODE (max
) == NOP_EXPR
12515 && TREE_SIDE_EFFECTS (omax
)
12516 && !TREE_TYPE (max
))
12517 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
12519 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
12520 with TREE_SIDE_EFFECTS that indicates this is not an integral
12521 constant expression. */
12522 if (processing_template_decl
12523 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
12525 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
12526 TREE_SIDE_EFFECTS (max
) = 1;
12529 return compute_array_index_type (NULL_TREE
, max
, complain
);
12532 case TEMPLATE_TYPE_PARM
:
12533 case TEMPLATE_TEMPLATE_PARM
:
12534 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12535 case TEMPLATE_PARM_INDEX
:
12540 tree arg
= NULL_TREE
;
12547 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
12548 template_parm_level_and_index (t
, &level
, &idx
);
12550 levels
= TMPL_ARGS_DEPTH (args
);
12551 if (level
<= levels
)
12553 arg
= TMPL_ARG (args
, level
, idx
);
12554 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
12556 /* See through ARGUMENT_PACK_SELECT arguments. */
12557 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
12558 /* If the selected argument is an expansion E, that most
12559 likely means we were called from
12560 gen_elem_of_pack_expansion_instantiation during the
12561 substituting of pack an argument pack (which Ith
12562 element is a pack expansion, where I is
12563 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
12564 In this case, the Ith element resulting from this
12565 substituting is going to be a pack expansion, which
12566 pattern is the pattern of E. Let's return the
12568 gen_elem_of_pack_expansion_instantiation will
12569 build the resulting pack expansion from it. */
12570 if (PACK_EXPANSION_P (arg
))
12572 /* Make sure we aren't throwing away arg info. */
12573 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
12574 arg
= PACK_EXPANSION_PATTERN (arg
);
12579 if (arg
== error_mark_node
)
12580 return error_mark_node
;
12581 else if (arg
!= NULL_TREE
)
12583 if (ARGUMENT_PACK_P (arg
))
12584 /* If ARG is an argument pack, we don't actually want to
12585 perform a substitution here, because substitutions
12586 for argument packs are only done
12587 element-by-element. We can get to this point when
12588 substituting the type of a non-type template
12589 parameter pack, when that type actually contains
12590 template parameter packs from an outer template, e.g.,
12592 template<typename... Types> struct A {
12593 template<Types... Values> struct B { };
12597 if (code
== TEMPLATE_TYPE_PARM
)
12600 gcc_assert (TYPE_P (arg
));
12602 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
12604 return cp_build_qualified_type_real
12605 (arg
, quals
, complain
| tf_ignore_bad_quals
);
12607 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
12609 /* We are processing a type constructed from a
12610 template template parameter. */
12611 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
12612 args
, complain
, in_decl
);
12613 if (argvec
== error_mark_node
)
12614 return error_mark_node
;
12616 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
12617 || TREE_CODE (arg
) == TEMPLATE_DECL
12618 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
12620 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
12621 /* Consider this code:
12623 template <template <class> class Template>
12625 template <class Arg> using Bind = Template<Arg>;
12628 template <template <class> class Template, class Arg>
12629 using Instantiate = Template<Arg>; //#0
12631 template <template <class> class Template,
12634 Instantiate<Internal<Template>::template Bind,
12637 When #1 is parsed, the
12638 BOUND_TEMPLATE_TEMPLATE_PARM representing the
12639 parameter `Template' in #0 matches the
12640 UNBOUND_CLASS_TEMPLATE representing the argument
12641 `Internal<Template>::template Bind'; We then want
12642 to assemble the type `Bind<Argument>' that can't
12643 be fully created right now, because
12644 `Internal<Template>' not being complete, the Bind
12645 template cannot be looked up in that context. So
12646 we need to "store" `Bind<Argument>' for later
12647 when the context of Bind becomes complete. Let's
12648 store that in a TYPENAME_TYPE. */
12649 return make_typename_type (TYPE_CONTEXT (arg
),
12650 build_nt (TEMPLATE_ID_EXPR
,
12651 TYPE_IDENTIFIER (arg
),
12656 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
12657 are resolving nested-types in the signature of a
12658 member function templates. Otherwise ARG is a
12659 TEMPLATE_DECL and is the real template to be
12661 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
12662 arg
= TYPE_NAME (arg
);
12664 r
= lookup_template_class (arg
,
12666 DECL_CONTEXT (arg
),
12667 /*entering_scope=*/0,
12669 return cp_build_qualified_type_real
12670 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
12673 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
12674 return convert_from_reference (unshare_expr (arg
));
12678 /* This can happen during the attempted tsubst'ing in
12679 unify. This means that we don't yet have any information
12680 about the template parameter in question. */
12683 /* Early in template argument deduction substitution, we don't
12684 want to reduce the level of 'auto', or it will be confused
12685 with a normal template parm in subsequent deduction. */
12686 if (is_auto (t
) && (complain
& tf_partial
))
12689 /* If we get here, we must have been looking at a parm for a
12690 more deeply nested template. Make a new version of this
12691 template parameter, but with a lower level. */
12694 case TEMPLATE_TYPE_PARM
:
12695 case TEMPLATE_TEMPLATE_PARM
:
12696 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12697 if (cp_type_quals (t
))
12699 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
12700 r
= cp_build_qualified_type_real
12701 (r
, cp_type_quals (t
),
12702 complain
| (code
== TEMPLATE_TYPE_PARM
12703 ? tf_ignore_bad_quals
: 0));
12708 TEMPLATE_TYPE_PARM_INDEX (r
)
12709 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
12710 r
, levels
, args
, complain
);
12711 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
12712 TYPE_MAIN_VARIANT (r
) = r
;
12713 TYPE_POINTER_TO (r
) = NULL_TREE
;
12714 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
12716 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
12717 /* We have reduced the level of the template
12718 template parameter, but not the levels of its
12719 template parameters, so canonical_type_parameter
12720 will not be able to find the canonical template
12721 template parameter for this level. Thus, we
12722 require structural equality checking to compare
12723 TEMPLATE_TEMPLATE_PARMs. */
12724 SET_TYPE_STRUCTURAL_EQUALITY (r
);
12725 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
12726 SET_TYPE_STRUCTURAL_EQUALITY (r
);
12728 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
12730 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
12732 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
12733 complain
, in_decl
);
12734 if (argvec
== error_mark_node
)
12735 return error_mark_node
;
12737 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
12738 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
12743 case TEMPLATE_PARM_INDEX
:
12744 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
12748 gcc_unreachable ();
12756 tree purpose
, value
, chain
;
12758 if (t
== void_list_node
)
12761 purpose
= TREE_PURPOSE (t
);
12764 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
12765 if (purpose
== error_mark_node
)
12766 return error_mark_node
;
12768 value
= TREE_VALUE (t
);
12771 value
= tsubst (value
, args
, complain
, in_decl
);
12772 if (value
== error_mark_node
)
12773 return error_mark_node
;
12775 chain
= TREE_CHAIN (t
);
12776 if (chain
&& chain
!= void_type_node
)
12778 chain
= tsubst (chain
, args
, complain
, in_decl
);
12779 if (chain
== error_mark_node
)
12780 return error_mark_node
;
12782 if (purpose
== TREE_PURPOSE (t
)
12783 && value
== TREE_VALUE (t
)
12784 && chain
== TREE_CHAIN (t
))
12786 return hash_tree_cons (purpose
, value
, chain
);
12790 /* We should never be tsubsting a binfo. */
12791 gcc_unreachable ();
12794 /* A vector of template arguments. */
12795 gcc_assert (!type
);
12796 return tsubst_template_args (t
, args
, complain
, in_decl
);
12799 case REFERENCE_TYPE
:
12801 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
12806 Type deduction may fail for any of the following
12809 -- Attempting to create a pointer to reference type.
12810 -- Attempting to create a reference to a reference type or
12811 a reference to void.
12813 Core issue 106 says that creating a reference to a reference
12814 during instantiation is no longer a cause for failure. We
12815 only enforce this check in strict C++98 mode. */
12816 if ((TREE_CODE (type
) == REFERENCE_TYPE
12817 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
12818 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
12820 static location_t last_loc
;
12822 /* We keep track of the last time we issued this error
12823 message to avoid spewing a ton of messages during a
12824 single bad template instantiation. */
12825 if (complain
& tf_error
12826 && last_loc
!= input_location
)
12828 if (VOID_TYPE_P (type
))
12829 error ("forming reference to void");
12830 else if (code
== POINTER_TYPE
)
12831 error ("forming pointer to reference type %qT", type
);
12833 error ("forming reference to reference type %qT", type
);
12834 last_loc
= input_location
;
12837 return error_mark_node
;
12839 else if (TREE_CODE (type
) == FUNCTION_TYPE
12840 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
12841 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
12843 if (complain
& tf_error
)
12845 if (code
== POINTER_TYPE
)
12846 error ("forming pointer to qualified function type %qT",
12849 error ("forming reference to qualified function type %qT",
12852 return error_mark_node
;
12854 else if (code
== POINTER_TYPE
)
12856 r
= build_pointer_type (type
);
12857 if (TREE_CODE (type
) == METHOD_TYPE
)
12858 r
= build_ptrmemfunc_type (r
);
12860 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
12861 /* In C++0x, during template argument substitution, when there is an
12862 attempt to create a reference to a reference type, reference
12863 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
12865 "If a template-argument for a template-parameter T names a type
12866 that is a reference to a type A, an attempt to create the type
12867 'lvalue reference to cv T' creates the type 'lvalue reference to
12868 A,' while an attempt to create the type type rvalue reference to
12869 cv T' creates the type T"
12871 r
= cp_build_reference_type
12873 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
12875 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
12876 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
12878 if (r
!= error_mark_node
)
12879 /* Will this ever be needed for TYPE_..._TO values? */
12886 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
12887 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
12891 Type deduction may fail for any of the following
12894 -- Attempting to create "pointer to member of T" when T
12895 is not a class type. */
12896 if (complain
& tf_error
)
12897 error ("creating pointer to member of non-class type %qT", r
);
12898 return error_mark_node
;
12900 if (TREE_CODE (type
) == REFERENCE_TYPE
)
12902 if (complain
& tf_error
)
12903 error ("creating pointer to member reference type %qT", type
);
12904 return error_mark_node
;
12906 if (VOID_TYPE_P (type
))
12908 if (complain
& tf_error
)
12909 error ("creating pointer to member of type void");
12910 return error_mark_node
;
12912 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
12913 if (TREE_CODE (type
) == FUNCTION_TYPE
)
12915 /* The type of the implicit object parameter gets its
12916 cv-qualifiers from the FUNCTION_TYPE. */
12919 = build_memfn_type (type
, r
, type_memfn_quals (type
),
12920 type_memfn_rqual (type
));
12921 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
12922 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
12926 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
12930 case FUNCTION_TYPE
:
12935 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
12936 if (fntype
== error_mark_node
)
12937 return error_mark_node
;
12939 /* Substitute the exception specification. */
12940 specs
= tsubst_exception_specification (t
, args
, complain
,
12941 in_decl
, /*defer_ok*/true);
12942 if (specs
== error_mark_node
)
12943 return error_mark_node
;
12945 fntype
= build_exception_variant (fntype
, specs
);
12950 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
12951 if (domain
== error_mark_node
)
12952 return error_mark_node
;
12954 /* As an optimization, we avoid regenerating the array type if
12955 it will obviously be the same as T. */
12956 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
12959 /* These checks should match the ones in create_array_type_for_decl.
12963 The deduction may fail for any of the following reasons:
12965 -- Attempting to create an array with an element type that
12966 is void, a function type, or a reference type, or [DR337]
12967 an abstract class type. */
12968 if (VOID_TYPE_P (type
)
12969 || TREE_CODE (type
) == FUNCTION_TYPE
12970 || (TREE_CODE (type
) == ARRAY_TYPE
12971 && TYPE_DOMAIN (type
) == NULL_TREE
)
12972 || TREE_CODE (type
) == REFERENCE_TYPE
)
12974 if (complain
& tf_error
)
12975 error ("creating array of %qT", type
);
12976 return error_mark_node
;
12979 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
12980 return error_mark_node
;
12982 r
= build_cplus_array_type (type
, domain
);
12984 if (TYPE_USER_ALIGN (t
))
12986 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
12987 TYPE_USER_ALIGN (r
) = 1;
12993 case TYPENAME_TYPE
:
12995 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
12996 in_decl
, /*entering_scope=*/1);
12997 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
12998 complain
, in_decl
);
13000 if (ctx
== error_mark_node
|| f
== error_mark_node
)
13001 return error_mark_node
;
13003 if (!MAYBE_CLASS_TYPE_P (ctx
))
13005 if (complain
& tf_error
)
13006 error ("%qT is not a class, struct, or union type", ctx
);
13007 return error_mark_node
;
13009 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
13011 /* Normally, make_typename_type does not require that the CTX
13012 have complete type in order to allow things like:
13014 template <class T> struct S { typename S<T>::X Y; };
13016 But, such constructs have already been resolved by this
13017 point, so here CTX really should have complete type, unless
13018 it's a partial instantiation. */
13019 ctx
= complete_type (ctx
);
13020 if (!COMPLETE_TYPE_P (ctx
))
13022 if (complain
& tf_error
)
13023 cxx_incomplete_type_error (NULL_TREE
, ctx
);
13024 return error_mark_node
;
13028 f
= make_typename_type (ctx
, f
, typename_type
,
13029 complain
| tf_keep_type_decl
);
13030 if (f
== error_mark_node
)
13032 if (TREE_CODE (f
) == TYPE_DECL
)
13034 complain
|= tf_ignore_bad_quals
;
13038 if (TREE_CODE (f
) != TYPENAME_TYPE
)
13040 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
13042 if (complain
& tf_error
)
13043 error ("%qT resolves to %qT, which is not an enumeration type",
13046 return error_mark_node
;
13048 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
13050 if (complain
& tf_error
)
13051 error ("%qT resolves to %qT, which is is not a class type",
13054 return error_mark_node
;
13058 return cp_build_qualified_type_real
13059 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
13062 case UNBOUND_CLASS_TEMPLATE
:
13064 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
13065 in_decl
, /*entering_scope=*/1);
13066 tree name
= TYPE_IDENTIFIER (t
);
13067 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
13069 if (ctx
== error_mark_node
|| name
== error_mark_node
)
13070 return error_mark_node
;
13073 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
13074 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
13081 ++cp_unevaluated_operand
;
13082 ++c_inhibit_evaluation_warnings
;
13084 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
13086 /*integral_constant_expression_p=*/false);
13088 --cp_unevaluated_operand
;
13089 --c_inhibit_evaluation_warnings
;
13091 type
= finish_typeof (type
);
13092 return cp_build_qualified_type_real (type
,
13094 | cp_type_quals (type
),
13098 case DECLTYPE_TYPE
:
13102 ++cp_unevaluated_operand
;
13103 ++c_inhibit_evaluation_warnings
;
13105 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
13106 complain
|tf_decltype
, in_decl
,
13107 /*function_p*/false,
13108 /*integral_constant_expression*/false);
13110 --cp_unevaluated_operand
;
13111 --c_inhibit_evaluation_warnings
;
13113 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
13114 type
= lambda_capture_field_type (type
,
13115 DECLTYPE_FOR_INIT_CAPTURE (t
));
13116 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
13117 type
= lambda_proxy_type (type
);
13120 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
13121 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
13123 /* In a template ~id could be either a complement expression
13124 or an unqualified-id naming a destructor; if instantiating
13125 it produces an expression, it's not an id-expression or
13128 type
= finish_decltype_type (type
, id
, complain
);
13130 return cp_build_qualified_type_real (type
,
13132 | cp_type_quals (type
),
13133 complain
| tf_ignore_bad_quals
);
13136 case UNDERLYING_TYPE
:
13138 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
13139 complain
, in_decl
);
13140 return finish_underlying_type (type
);
13143 case TYPE_ARGUMENT_PACK
:
13144 case NONTYPE_ARGUMENT_PACK
:
13146 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
13148 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
13152 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
13154 /* For template nontype argument packs, also substitute into
13156 if (code
== NONTYPE_ARGUMENT_PACK
)
13157 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13176 /* We should use one of the expression tsubsts for these codes. */
13177 gcc_unreachable ();
13180 sorry ("use of %qs in template", get_tree_code_name (code
));
13181 return error_mark_node
;
13185 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
13186 type of the expression on the left-hand side of the "." or "->"
13190 tsubst_baselink (tree baselink
, tree object_type
,
13191 tree args
, tsubst_flags_t complain
, tree in_decl
)
13194 tree qualifying_scope
;
13197 tree template_args
= 0;
13198 bool template_id_p
= false;
13199 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
13201 /* A baselink indicates a function from a base class. Both the
13202 BASELINK_ACCESS_BINFO and the base class referenced may
13203 indicate bases of the template class, rather than the
13204 instantiated class. In addition, lookups that were not
13205 ambiguous before may be ambiguous now. Therefore, we perform
13206 the lookup again. */
13207 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
13208 qualifying_scope
= tsubst (qualifying_scope
, args
,
13209 complain
, in_decl
);
13210 fns
= BASELINK_FUNCTIONS (baselink
);
13211 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
13212 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
13214 template_id_p
= true;
13215 template_args
= TREE_OPERAND (fns
, 1);
13216 fns
= TREE_OPERAND (fns
, 0);
13218 template_args
= tsubst_template_args (template_args
, args
,
13219 complain
, in_decl
);
13221 name
= DECL_NAME (get_first_fn (fns
));
13222 if (IDENTIFIER_TYPENAME_P (name
))
13223 name
= mangle_conv_op_name_for_type (optype
);
13224 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
13226 return error_mark_node
;
13228 /* If lookup found a single function, mark it as used at this
13229 point. (If it lookup found multiple functions the one selected
13230 later by overload resolution will be marked as used at that
13232 if (BASELINK_P (baselink
))
13233 fns
= BASELINK_FUNCTIONS (baselink
);
13234 if (!template_id_p
&& !really_overloaded_fn (fns
))
13235 mark_used (OVL_CURRENT (fns
), complain
);
13237 /* Add back the template arguments, if present. */
13238 if (BASELINK_P (baselink
) && template_id_p
)
13239 BASELINK_FUNCTIONS (baselink
)
13240 = build_nt (TEMPLATE_ID_EXPR
,
13241 BASELINK_FUNCTIONS (baselink
),
13243 /* Update the conversion operator type. */
13244 BASELINK_OPTYPE (baselink
) = optype
;
13247 object_type
= current_class_type
;
13250 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
13256 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
13257 true if the qualified-id will be a postfix-expression in-and-of
13258 itself; false if more of the postfix-expression follows the
13259 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
13263 tsubst_qualified_id (tree qualified_id
, tree args
,
13264 tsubst_flags_t complain
, tree in_decl
,
13265 bool done
, bool address_p
)
13271 tree template_args
;
13272 location_t loc
= UNKNOWN_LOCATION
;
13274 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
13276 /* Figure out what name to look up. */
13277 name
= TREE_OPERAND (qualified_id
, 1);
13278 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
13280 is_template
= true;
13281 loc
= EXPR_LOCATION (name
);
13282 template_args
= TREE_OPERAND (name
, 1);
13284 template_args
= tsubst_template_args (template_args
, args
,
13285 complain
, in_decl
);
13286 name
= TREE_OPERAND (name
, 0);
13290 is_template
= false;
13291 template_args
= NULL_TREE
;
13294 /* Substitute into the qualifying scope. When there are no ARGS, we
13295 are just trying to simplify a non-dependent expression. In that
13296 case the qualifying scope may be dependent, and, in any case,
13297 substituting will not help. */
13298 scope
= TREE_OPERAND (qualified_id
, 0);
13301 scope
= tsubst (scope
, args
, complain
, in_decl
);
13302 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
13307 if (dependent_scope_p (scope
))
13310 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
13311 return build_qualified_name (NULL_TREE
, scope
, expr
,
13312 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
13315 if (!BASELINK_P (name
) && !DECL_P (expr
))
13317 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
13319 /* A BIT_NOT_EXPR is used to represent a destructor. */
13320 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
13322 error ("qualifying type %qT does not match destructor name ~%qT",
13323 scope
, TREE_OPERAND (expr
, 0));
13324 expr
= error_mark_node
;
13327 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
13328 /*is_type_p=*/0, false);
13331 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
13332 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
13333 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
13335 if (complain
& tf_error
)
13337 error ("dependent-name %qE is parsed as a non-type, but "
13338 "instantiation yields a type", qualified_id
);
13339 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
13341 return error_mark_node
;
13347 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
13349 /* Remember that there was a reference to this entity. */
13353 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
13355 if (complain
& tf_error
)
13356 qualified_name_lookup_error (scope
,
13357 TREE_OPERAND (qualified_id
, 1),
13358 expr
, input_location
);
13359 return error_mark_node
;
13363 expr
= lookup_template_function (expr
, template_args
);
13365 if (expr
== error_mark_node
&& complain
& tf_error
)
13366 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
13367 expr
, input_location
);
13368 else if (TYPE_P (scope
))
13370 expr
= (adjust_result_of_qualified_name_lookup
13371 (expr
, scope
, current_nonlambda_class_type ()));
13372 expr
= (finish_qualified_id_expr
13373 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
13374 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
13375 /*template_arg_p=*/false, complain
));
13378 /* Expressions do not generally have reference type. */
13379 if (TREE_CODE (expr
) != SCOPE_REF
13380 /* However, if we're about to form a pointer-to-member, we just
13381 want the referenced member referenced. */
13382 && TREE_CODE (expr
) != OFFSET_REF
)
13383 expr
= convert_from_reference (expr
);
13388 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
13389 initializer, DECL is the substituted VAR_DECL. Other arguments are as
13393 tsubst_init (tree init
, tree decl
, tree args
,
13394 tsubst_flags_t complain
, tree in_decl
)
13399 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
13403 /* If we had an initializer but it
13404 instantiated to nothing,
13405 value-initialize the object. This will
13406 only occur when the initializer was a
13407 pack expansion where the parameter packs
13408 used in that expansion were of length
13410 init
= build_value_init (TREE_TYPE (decl
),
13412 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
13413 init
= get_target_expr_sfinae (init
, complain
);
13419 /* Like tsubst, but deals with expressions. This function just replaces
13420 template parms; to finish processing the resultant expression, use
13421 tsubst_copy_and_build or tsubst_expr. */
13424 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13426 enum tree_code code
;
13429 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
13432 code
= TREE_CODE (t
);
13437 r
= retrieve_local_specialization (t
);
13439 if (r
== NULL_TREE
)
13441 /* We get here for a use of 'this' in an NSDMI. */
13442 if (DECL_NAME (t
) == this_identifier
13443 && current_function_decl
13444 && DECL_CONSTRUCTOR_P (current_function_decl
))
13445 return current_class_ptr
;
13447 /* This can happen for a parameter name used later in a function
13448 declaration (such as in a late-specified return type). Just
13449 make a dummy decl, since it's only used for its type. */
13450 gcc_assert (cp_unevaluated_operand
!= 0);
13451 r
= tsubst_decl (t
, args
, complain
);
13452 /* Give it the template pattern as its context; its true context
13453 hasn't been instantiated yet and this is good enough for
13455 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
13458 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
13459 r
= ARGUMENT_PACK_SELECT_ARG (r
);
13468 if (DECL_TEMPLATE_PARM_P (t
))
13469 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
13470 /* There is no need to substitute into namespace-scope
13472 if (DECL_NAMESPACE_SCOPE_P (t
))
13474 /* If ARGS is NULL, then T is known to be non-dependent. */
13475 if (args
== NULL_TREE
)
13476 return scalar_constant_value (t
);
13478 /* Unfortunately, we cannot just call lookup_name here.
13481 template <int I> int f() {
13483 struct S { void g() { E e = a; } };
13486 When we instantiate f<7>::S::g(), say, lookup_name is not
13487 clever enough to find f<7>::a. */
13489 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
13490 /*entering_scope=*/0);
13492 for (v
= TYPE_VALUES (enum_type
);
13494 v
= TREE_CHAIN (v
))
13495 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
13496 return TREE_VALUE (v
);
13498 /* We didn't find the name. That should never happen; if
13499 name-lookup found it during preliminary parsing, we
13500 should find it again here during instantiation. */
13501 gcc_unreachable ();
13506 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
13508 /* Check for a local specialization set up by
13509 tsubst_pack_expansion. */
13510 if (tree r
= retrieve_local_specialization (t
))
13512 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
13513 r
= ARGUMENT_PACK_SELECT_ARG (r
);
13517 /* When retrieving a capture pack from a generic lambda, remove the
13518 lambda call op's own template argument list from ARGS. Only the
13519 template arguments active for the closure type should be used to
13520 retrieve the pack specialization. */
13521 if (LAMBDA_FUNCTION_P (current_function_decl
)
13522 && (template_class_depth (DECL_CONTEXT (t
))
13523 != TMPL_ARGS_DEPTH (args
)))
13524 args
= strip_innermost_template_args (args
, 1);
13526 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
13527 tsubst_decl put in the hash table. */
13528 return retrieve_specialization (t
, args
, 0);
13531 if (DECL_CONTEXT (t
))
13535 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
13536 /*entering_scope=*/1);
13537 if (ctx
!= DECL_CONTEXT (t
))
13539 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
13542 if (complain
& tf_error
)
13543 error ("using invalid field %qD", t
);
13544 return error_mark_node
;
13553 case FUNCTION_DECL
:
13554 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
13555 r
= tsubst (t
, args
, complain
, in_decl
);
13556 else if (local_variable_p (t
))
13558 r
= retrieve_local_specialization (t
);
13559 if (r
== NULL_TREE
)
13561 /* First try name lookup to find the instantiation. */
13562 r
= lookup_name (DECL_NAME (t
));
13565 /* Make sure that the one we found is the one we want. */
13566 tree ctx
= tsubst (DECL_CONTEXT (t
), args
,
13567 complain
, in_decl
);
13568 if (ctx
!= DECL_CONTEXT (r
))
13576 /* This can happen for a variable used in a
13577 late-specified return type of a local lambda, or for a
13578 local static or constant. Building a new VAR_DECL
13579 should be OK in all those cases. */
13580 r
= tsubst_decl (t
, args
, complain
);
13581 if (decl_maybe_constant_var_p (r
))
13583 /* We can't call cp_finish_decl, so handle the
13584 initializer by hand. */
13585 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
13586 complain
, in_decl
);
13587 if (!processing_template_decl
)
13588 init
= maybe_constant_init (init
);
13589 if (processing_template_decl
13590 ? potential_constant_expression (init
)
13591 : reduced_constant_expression_p (init
))
13592 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
13593 = TREE_CONSTANT (r
) = true;
13594 DECL_INITIAL (r
) = init
;
13596 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
13597 || decl_constant_var_p (r
)
13598 || errorcount
|| sorrycount
);
13599 if (!processing_template_decl
)
13601 if (TREE_STATIC (r
))
13602 rest_of_decl_compilation (r
, toplevel_bindings_p (),
13604 else if (decl_constant_var_p (r
))
13605 /* A use of a local constant decays to its value.
13606 FIXME update for core DR 696. */
13607 r
= scalar_constant_value (r
);
13610 /* Remember this for subsequent uses. */
13611 if (local_specializations
)
13612 register_local_specialization (r
, t
);
13620 case NAMESPACE_DECL
:
13624 /* An OVERLOAD will always be a non-dependent overload set; an
13625 overload set from function scope will just be represented with an
13626 IDENTIFIER_NODE, and from class scope with a BASELINK. */
13627 gcc_assert (!uses_template_parms (t
));
13631 return tsubst_baselink (t
, current_nonlambda_class_type (),
13632 args
, complain
, in_decl
);
13634 case TEMPLATE_DECL
:
13635 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
13636 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
13637 args
, complain
, in_decl
);
13638 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
13639 return tsubst (t
, args
, complain
, in_decl
);
13640 else if (DECL_CLASS_SCOPE_P (t
)
13641 && uses_template_parms (DECL_CONTEXT (t
)))
13643 /* Template template argument like the following example need
13646 template <template <class> class TT> struct C {};
13647 template <class T> struct D {
13648 template <class U> struct E {};
13653 We are processing the template argument `E' in #1 for
13654 the template instantiation #2. Originally, `E' is a
13655 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
13656 have to substitute this with one having context `D<int>'. */
13658 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
13659 return lookup_field (context
, DECL_NAME(t
), 0, false);
13662 /* Ordinary template template argument. */
13666 case REINTERPRET_CAST_EXPR
:
13667 case CONST_CAST_EXPR
:
13668 case STATIC_CAST_EXPR
:
13669 case DYNAMIC_CAST_EXPR
:
13670 case IMPLICIT_CONV_EXPR
:
13674 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13675 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13676 return build1 (code
, type
, op0
);
13680 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
13683 tree expanded
, op
= TREE_OPERAND (t
, 0);
13686 if (SIZEOF_EXPR_TYPE_P (t
))
13687 op
= TREE_TYPE (op
);
13689 ++cp_unevaluated_operand
;
13690 ++c_inhibit_evaluation_warnings
;
13691 /* We only want to compute the number of arguments. */
13692 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
13693 --cp_unevaluated_operand
;
13694 --c_inhibit_evaluation_warnings
;
13696 if (TREE_CODE (expanded
) == TREE_VEC
)
13697 len
= TREE_VEC_LENGTH (expanded
);
13699 if (expanded
== error_mark_node
)
13700 return error_mark_node
;
13701 else if (PACK_EXPANSION_P (expanded
)
13702 || (TREE_CODE (expanded
) == TREE_VEC
13704 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
13706 if (TREE_CODE (expanded
) == TREE_VEC
)
13707 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
13709 if (TYPE_P (expanded
))
13710 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
13711 complain
& tf_error
);
13713 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
13714 complain
& tf_error
);
13717 return build_int_cst (size_type_node
, len
);
13719 if (SIZEOF_EXPR_TYPE_P (t
))
13721 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
13722 args
, complain
, in_decl
);
13723 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
13724 r
= build1 (SIZEOF_EXPR
,
13725 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
13726 SIZEOF_EXPR_TYPE_P (r
) = 1;
13733 case TRUTH_NOT_EXPR
:
13736 case UNARY_PLUS_EXPR
: /* Unary + */
13738 case AT_ENCODE_EXPR
:
13742 case REALPART_EXPR
:
13743 case IMAGPART_EXPR
:
13746 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13747 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13748 return build1 (code
, type
, op0
);
13751 case COMPONENT_REF
:
13756 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13757 name
= TREE_OPERAND (t
, 1);
13758 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
13760 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
13761 complain
, in_decl
);
13762 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
13764 else if (TREE_CODE (name
) == SCOPE_REF
13765 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
13767 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
13768 complain
, in_decl
);
13769 name
= TREE_OPERAND (name
, 1);
13770 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
13771 complain
, in_decl
);
13772 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
13773 name
= build_qualified_name (/*type=*/NULL_TREE
,
13775 /*template_p=*/false);
13777 else if (BASELINK_P (name
))
13778 name
= tsubst_baselink (name
,
13779 non_reference (TREE_TYPE (object
)),
13783 name
= tsubst_copy (name
, args
, complain
, in_decl
);
13784 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
13790 case TRUNC_DIV_EXPR
:
13791 case CEIL_DIV_EXPR
:
13792 case FLOOR_DIV_EXPR
:
13793 case ROUND_DIV_EXPR
:
13794 case EXACT_DIV_EXPR
:
13798 case TRUNC_MOD_EXPR
:
13799 case FLOOR_MOD_EXPR
:
13800 case TRUTH_ANDIF_EXPR
:
13801 case TRUTH_ORIF_EXPR
:
13802 case TRUTH_AND_EXPR
:
13803 case TRUTH_OR_EXPR
:
13816 case COMPOUND_EXPR
:
13819 case PREDECREMENT_EXPR
:
13820 case PREINCREMENT_EXPR
:
13821 case POSTDECREMENT_EXPR
:
13822 case POSTINCREMENT_EXPR
:
13824 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13825 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13826 return build_nt (code
, op0
, op1
);
13831 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13832 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13833 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
13834 QUALIFIED_NAME_IS_TEMPLATE (t
));
13839 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13840 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13841 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
13846 int n
= VL_EXP_OPERAND_LENGTH (t
);
13847 tree result
= build_vl_exp (CALL_EXPR
, n
);
13849 for (i
= 0; i
< n
; i
++)
13850 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
13851 complain
, in_decl
);
13857 case PSEUDO_DTOR_EXPR
:
13858 case VEC_PERM_EXPR
:
13860 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13861 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13862 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
13863 r
= build_nt (code
, op0
, op1
, op2
);
13864 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13870 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13871 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13872 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
13873 r
= build_nt (code
, op0
, op1
, op2
);
13874 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
13880 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13881 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13882 r
= build_nt (code
, op0
, op1
);
13883 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
13884 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
13888 case TEMPLATE_ID_EXPR
:
13890 /* Substituted template arguments */
13891 tree fn
= TREE_OPERAND (t
, 0);
13892 tree targs
= TREE_OPERAND (t
, 1);
13894 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
13896 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
13898 return lookup_template_function (fn
, targs
);
13903 tree purpose
, value
, chain
;
13905 if (t
== void_list_node
)
13908 purpose
= TREE_PURPOSE (t
);
13910 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
13911 value
= TREE_VALUE (t
);
13913 value
= tsubst_copy (value
, args
, complain
, in_decl
);
13914 chain
= TREE_CHAIN (t
);
13915 if (chain
&& chain
!= void_type_node
)
13916 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
13917 if (purpose
== TREE_PURPOSE (t
)
13918 && value
== TREE_VALUE (t
)
13919 && chain
== TREE_CHAIN (t
))
13921 return tree_cons (purpose
, value
, chain
);
13926 case ENUMERAL_TYPE
:
13930 case TEMPLATE_TYPE_PARM
:
13931 case TEMPLATE_TEMPLATE_PARM
:
13932 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13933 case TEMPLATE_PARM_INDEX
:
13935 case REFERENCE_TYPE
:
13937 case FUNCTION_TYPE
:
13940 case TYPENAME_TYPE
:
13941 case UNBOUND_CLASS_TEMPLATE
:
13943 case DECLTYPE_TYPE
:
13945 return tsubst (t
, args
, complain
, in_decl
);
13949 /* Fall through. */
13950 case IDENTIFIER_NODE
:
13951 if (IDENTIFIER_TYPENAME_P (t
))
13953 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13954 return mangle_conv_op_name_for_type (new_type
);
13960 /* This is handled by tsubst_copy_and_build. */
13961 gcc_unreachable ();
13965 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13966 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13967 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
13970 case CLEANUP_POINT_EXPR
:
13971 /* We shouldn't have built any of these during initial template
13972 generation. Instead, they should be built during instantiation
13973 in response to the saved STMT_IS_FULL_EXPR_P setting. */
13974 gcc_unreachable ();
13978 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13979 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13980 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13981 r
= build2 (code
, type
, op0
, op1
);
13982 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
13983 mark_used (TREE_OPERAND (r
, 1));
13987 case EXPR_PACK_EXPANSION
:
13988 // Expansions of variadic constraints are viable, expanding
13989 // to a conunction of the expanded terms.
13990 if (TREE_TYPE (t
) == boolean_type_node
)
13991 return tsubst_pack_conjunction (t
, args
, complain
, in_decl
);
13993 error ("invalid use of pack expansion expression");
13994 return error_mark_node
;
13996 case NONTYPE_ARGUMENT_PACK
:
13997 error ("use %<...%> to expand argument pack");
13998 return error_mark_node
;
14001 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
14009 /* Instantiate any typedefs in the type. */
14010 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14011 r
= fold_convert (type
, t
);
14012 gcc_assert (TREE_CODE (r
) == code
);
14017 /* These can sometimes show up in a partial instantiation, but never
14018 involve template parms. */
14019 gcc_assert (!uses_template_parms (t
));
14023 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
14024 gcc_checking_assert (false);
14029 /* Like tsubst_copy, but specifically for OpenMP clauses. */
14032 tsubst_omp_clauses (tree clauses
, bool declare_simd
,
14033 tree args
, tsubst_flags_t complain
, tree in_decl
)
14035 tree new_clauses
= NULL
, nc
, oc
;
14037 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
14039 nc
= copy_node (oc
);
14040 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
14043 switch (OMP_CLAUSE_CODE (nc
))
14045 case OMP_CLAUSE_LASTPRIVATE
:
14046 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
14048 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
14049 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
14050 in_decl
, /*integral_constant_expression_p=*/false);
14051 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
14052 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
14055 case OMP_CLAUSE_PRIVATE
:
14056 case OMP_CLAUSE_SHARED
:
14057 case OMP_CLAUSE_FIRSTPRIVATE
:
14058 case OMP_CLAUSE_COPYIN
:
14059 case OMP_CLAUSE_COPYPRIVATE
:
14060 case OMP_CLAUSE_IF
:
14061 case OMP_CLAUSE_NUM_THREADS
:
14062 case OMP_CLAUSE_SCHEDULE
:
14063 case OMP_CLAUSE_COLLAPSE
:
14064 case OMP_CLAUSE_FINAL
:
14065 case OMP_CLAUSE_DEPEND
:
14066 case OMP_CLAUSE_FROM
:
14067 case OMP_CLAUSE_TO
:
14068 case OMP_CLAUSE_UNIFORM
:
14069 case OMP_CLAUSE_MAP
:
14070 case OMP_CLAUSE_DEVICE
:
14071 case OMP_CLAUSE_DIST_SCHEDULE
:
14072 case OMP_CLAUSE_NUM_TEAMS
:
14073 case OMP_CLAUSE_THREAD_LIMIT
:
14074 case OMP_CLAUSE_SAFELEN
:
14075 case OMP_CLAUSE_SIMDLEN
:
14076 OMP_CLAUSE_OPERAND (nc
, 0)
14077 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
14078 in_decl
, /*integral_constant_expression_p=*/false);
14080 case OMP_CLAUSE_REDUCTION
:
14081 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
14083 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
14084 if (TREE_CODE (placeholder
) == SCOPE_REF
)
14086 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
14087 complain
, in_decl
);
14088 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
14089 = build_qualified_name (NULL_TREE
, scope
,
14090 TREE_OPERAND (placeholder
, 1),
14094 gcc_assert (identifier_p (placeholder
));
14096 OMP_CLAUSE_OPERAND (nc
, 0)
14097 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
14098 in_decl
, /*integral_constant_expression_p=*/false);
14100 case OMP_CLAUSE_LINEAR
:
14101 case OMP_CLAUSE_ALIGNED
:
14102 OMP_CLAUSE_OPERAND (nc
, 0)
14103 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
14104 in_decl
, /*integral_constant_expression_p=*/false);
14105 OMP_CLAUSE_OPERAND (nc
, 1)
14106 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
14107 in_decl
, /*integral_constant_expression_p=*/false);
14110 case OMP_CLAUSE_NOWAIT
:
14111 case OMP_CLAUSE_ORDERED
:
14112 case OMP_CLAUSE_DEFAULT
:
14113 case OMP_CLAUSE_UNTIED
:
14114 case OMP_CLAUSE_MERGEABLE
:
14115 case OMP_CLAUSE_INBRANCH
:
14116 case OMP_CLAUSE_NOTINBRANCH
:
14117 case OMP_CLAUSE_PROC_BIND
:
14118 case OMP_CLAUSE_FOR
:
14119 case OMP_CLAUSE_PARALLEL
:
14120 case OMP_CLAUSE_SECTIONS
:
14121 case OMP_CLAUSE_TASKGROUP
:
14124 gcc_unreachable ();
14128 new_clauses
= nreverse (new_clauses
);
14130 new_clauses
= finish_omp_clauses (new_clauses
);
14131 return new_clauses
;
14134 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
14137 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
14140 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
14142 tree purpose
, value
, chain
;
14147 if (TREE_CODE (t
) != TREE_LIST
)
14148 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
14149 /*function_p=*/false,
14150 /*integral_constant_expression_p=*/false);
14152 if (t
== void_list_node
)
14155 purpose
= TREE_PURPOSE (t
);
14157 purpose
= RECUR (purpose
);
14158 value
= TREE_VALUE (t
);
14161 if (TREE_CODE (value
) != LABEL_DECL
)
14162 value
= RECUR (value
);
14165 value
= lookup_label (DECL_NAME (value
));
14166 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
14167 TREE_USED (value
) = 1;
14170 chain
= TREE_CHAIN (t
);
14171 if (chain
&& chain
!= void_type_node
)
14172 chain
= RECUR (chain
);
14173 return tree_cons (purpose
, value
, chain
);
14177 /* Substitute one OMP_FOR iterator. */
14180 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
14181 tree condv
, tree incrv
, tree
*clauses
,
14182 tree args
, tsubst_flags_t complain
, tree in_decl
,
14183 bool integral_constant_expression_p
)
14185 #define RECUR(NODE) \
14186 tsubst_expr ((NODE), args, complain, in_decl, \
14187 integral_constant_expression_p)
14188 tree decl
, init
, cond
, incr
;
14190 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
14191 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
14192 decl
= TREE_OPERAND (init
, 0);
14193 init
= TREE_OPERAND (init
, 1);
14194 tree decl_expr
= NULL_TREE
;
14195 if (init
&& TREE_CODE (init
) == DECL_EXPR
)
14197 /* We need to jump through some hoops to handle declarations in the
14198 for-init-statement, since we might need to handle auto deduction,
14199 but we need to keep control of initialization. */
14201 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
14202 decl
= tsubst_decl (decl
, args
, complain
);
14205 decl
= RECUR (decl
);
14206 init
= RECUR (init
);
14208 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
14209 if (auto_node
&& init
)
14211 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
);
14213 gcc_assert (!type_dependent_expression_p (decl
));
14215 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
14219 /* Declare the variable, but don't let that initialize it. */
14220 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
14221 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
14223 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
14226 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
14227 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
14228 if (TREE_CODE (incr
) == MODIFY_EXPR
)
14230 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
14231 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
14232 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
14233 NOP_EXPR
, rhs
, complain
);
14236 incr
= RECUR (incr
);
14237 TREE_VEC_ELT (declv
, i
) = decl
;
14238 TREE_VEC_ELT (initv
, i
) = init
;
14239 TREE_VEC_ELT (condv
, i
) = cond
;
14240 TREE_VEC_ELT (incrv
, i
) = incr
;
14246 /* Declare and initialize the variable. */
14253 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
14255 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
14256 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
14257 && OMP_CLAUSE_DECL (c
) == decl
)
14259 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
14260 && OMP_CLAUSE_DECL (c
) == decl
)
14261 error ("iteration variable %qD should not be firstprivate", decl
);
14262 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
14263 && OMP_CLAUSE_DECL (c
) == decl
)
14264 error ("iteration variable %qD should not be reduction", decl
);
14268 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
14269 OMP_CLAUSE_DECL (c
) = decl
;
14270 c
= finish_omp_clauses (c
);
14273 OMP_CLAUSE_CHAIN (c
) = *clauses
;
14278 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
14279 if (COMPARISON_CLASS_P (cond
))
14281 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
14282 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
14283 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
14286 cond
= RECUR (cond
);
14287 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
14288 switch (TREE_CODE (incr
))
14290 case PREINCREMENT_EXPR
:
14291 case PREDECREMENT_EXPR
:
14292 case POSTINCREMENT_EXPR
:
14293 case POSTDECREMENT_EXPR
:
14294 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
14295 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
14298 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
14299 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
14301 tree rhs
= TREE_OPERAND (incr
, 1);
14302 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
14303 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
14304 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
14305 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
14306 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
14310 incr
= RECUR (incr
);
14313 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
14314 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
14316 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
14317 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
14318 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
14319 TREE_TYPE (decl
), lhs
,
14320 RECUR (TREE_OPERAND (incr
, 2))));
14322 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
14323 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
14324 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
14326 tree rhs
= TREE_OPERAND (incr
, 2);
14327 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
14328 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
14329 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
14330 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
14331 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
14335 incr
= RECUR (incr
);
14338 incr
= RECUR (incr
);
14342 TREE_VEC_ELT (declv
, i
) = decl
;
14343 TREE_VEC_ELT (initv
, i
) = init
;
14344 TREE_VEC_ELT (condv
, i
) = cond
;
14345 TREE_VEC_ELT (incrv
, i
) = incr
;
14349 /* Like tsubst_copy for expressions, etc. but also does semantic
14353 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
14354 bool integral_constant_expression_p
)
14356 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14357 #define RECUR(NODE) \
14358 tsubst_expr ((NODE), args, complain, in_decl, \
14359 integral_constant_expression_p)
14365 if (t
== NULL_TREE
|| t
== error_mark_node
)
14368 loc
= input_location
;
14369 if (EXPR_HAS_LOCATION (t
))
14370 input_location
= EXPR_LOCATION (t
);
14371 if (STATEMENT_CODE_P (TREE_CODE (t
)))
14372 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
14374 switch (TREE_CODE (t
))
14376 case STATEMENT_LIST
:
14378 tree_stmt_iterator i
;
14379 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
14380 RECUR (tsi_stmt (i
));
14384 case CTOR_INITIALIZER
:
14385 finish_mem_initializers (tsubst_initializer_list
14386 (TREE_OPERAND (t
, 0), args
));
14390 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
14394 tmp
= RECUR (EXPR_STMT_EXPR (t
));
14395 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
14396 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
14398 finish_expr_stmt (tmp
);
14402 do_using_directive (USING_STMT_NAMESPACE (t
));
14407 tree decl
, pattern_decl
;
14410 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
14411 if (TREE_CODE (decl
) == LABEL_DECL
)
14412 finish_label_decl (DECL_NAME (decl
));
14413 else if (TREE_CODE (decl
) == USING_DECL
)
14415 tree scope
= USING_DECL_SCOPE (decl
);
14416 tree name
= DECL_NAME (decl
);
14419 scope
= tsubst (scope
, args
, complain
, in_decl
);
14420 decl
= lookup_qualified_name (scope
, name
,
14421 /*is_type_p=*/false,
14422 /*complain=*/false);
14423 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
14424 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
14426 do_local_using_decl (decl
, scope
, name
);
14428 else if (DECL_PACK_P (decl
))
14430 /* Don't build up decls for a variadic capture proxy, we'll
14431 instantiate the elements directly as needed. */
14436 init
= DECL_INITIAL (decl
);
14437 decl
= tsubst (decl
, args
, complain
, in_decl
);
14438 if (decl
!= error_mark_node
)
14440 /* By marking the declaration as instantiated, we avoid
14441 trying to instantiate it. Since instantiate_decl can't
14442 handle local variables, and since we've already done
14443 all that needs to be done, that's the right thing to
14446 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
14448 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
14449 /* Anonymous aggregates are a special case. */
14450 finish_anon_union (decl
);
14451 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
14453 DECL_CONTEXT (decl
) = current_function_decl
;
14454 if (DECL_NAME (decl
) == this_identifier
)
14456 tree lam
= DECL_CONTEXT (current_function_decl
);
14457 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
14458 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
14460 insert_capture_proxy (decl
);
14462 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
14463 /* We already did a pushtag. */;
14464 else if (TREE_CODE (decl
) == FUNCTION_DECL
14465 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
14466 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
14468 DECL_CONTEXT (decl
) = NULL_TREE
;
14470 DECL_CONTEXT (decl
) = current_function_decl
;
14471 cp_check_omp_declare_reduction (decl
);
14475 int const_init
= false;
14476 maybe_push_decl (decl
);
14478 && DECL_PRETTY_FUNCTION_P (decl
))
14480 /* For __PRETTY_FUNCTION__ we have to adjust the
14482 const char *const name
14483 = cxx_printable_name (current_function_decl
, 2);
14484 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
14487 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
14490 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
14492 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
14501 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
14502 RECUR (FOR_INIT_STMT (t
));
14503 finish_for_init_stmt (stmt
);
14504 tmp
= RECUR (FOR_COND (t
));
14505 finish_for_cond (tmp
, stmt
, false);
14506 tmp
= RECUR (FOR_EXPR (t
));
14507 finish_for_expr (tmp
, stmt
);
14508 RECUR (FOR_BODY (t
));
14509 finish_for_stmt (stmt
);
14512 case RANGE_FOR_STMT
:
14515 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
14516 decl
= RANGE_FOR_DECL (t
);
14517 decl
= tsubst (decl
, args
, complain
, in_decl
);
14518 maybe_push_decl (decl
);
14519 expr
= RECUR (RANGE_FOR_EXPR (t
));
14520 stmt
= cp_convert_range_for (stmt
, decl
, expr
, RANGE_FOR_IVDEP (t
));
14521 RECUR (RANGE_FOR_BODY (t
));
14522 finish_for_stmt (stmt
);
14527 stmt
= begin_while_stmt ();
14528 tmp
= RECUR (WHILE_COND (t
));
14529 finish_while_stmt_cond (tmp
, stmt
, false);
14530 RECUR (WHILE_BODY (t
));
14531 finish_while_stmt (stmt
);
14535 stmt
= begin_do_stmt ();
14536 RECUR (DO_BODY (t
));
14537 finish_do_body (stmt
);
14538 tmp
= RECUR (DO_COND (t
));
14539 finish_do_stmt (tmp
, stmt
, false);
14543 stmt
= begin_if_stmt ();
14544 tmp
= RECUR (IF_COND (t
));
14545 finish_if_stmt_cond (tmp
, stmt
);
14546 RECUR (THEN_CLAUSE (t
));
14547 finish_then_clause (stmt
);
14549 if (ELSE_CLAUSE (t
))
14551 begin_else_clause (stmt
);
14552 RECUR (ELSE_CLAUSE (t
));
14553 finish_else_clause (stmt
);
14556 finish_if_stmt (stmt
);
14560 if (BIND_EXPR_BODY_BLOCK (t
))
14561 stmt
= begin_function_body ();
14563 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
14564 ? BCS_TRY_BLOCK
: 0);
14566 RECUR (BIND_EXPR_BODY (t
));
14568 if (BIND_EXPR_BODY_BLOCK (t
))
14569 finish_function_body (stmt
);
14571 finish_compound_stmt (stmt
);
14575 finish_break_stmt ();
14578 case CONTINUE_STMT
:
14579 finish_continue_stmt ();
14583 stmt
= begin_switch_stmt ();
14584 tmp
= RECUR (SWITCH_STMT_COND (t
));
14585 finish_switch_cond (tmp
, stmt
);
14586 RECUR (SWITCH_STMT_BODY (t
));
14587 finish_switch_stmt (stmt
);
14590 case CASE_LABEL_EXPR
:
14592 tree low
= RECUR (CASE_LOW (t
));
14593 tree high
= RECUR (CASE_HIGH (t
));
14594 finish_case_label (EXPR_LOCATION (t
), low
, high
);
14600 tree decl
= LABEL_EXPR_LABEL (t
);
14603 label
= finish_label_stmt (DECL_NAME (decl
));
14604 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
14605 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
14610 tmp
= GOTO_DESTINATION (t
);
14611 if (TREE_CODE (tmp
) != LABEL_DECL
)
14612 /* Computed goto's must be tsubst'd into. On the other hand,
14613 non-computed gotos must not be; the identifier in question
14614 will have no binding. */
14617 tmp
= DECL_NAME (tmp
);
14618 finish_goto_stmt (tmp
);
14623 tree string
= RECUR (ASM_STRING (t
));
14624 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
14625 complain
, in_decl
);
14626 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
14627 complain
, in_decl
);
14628 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
14629 complain
, in_decl
);
14630 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
14631 complain
, in_decl
);
14632 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
14634 tree asm_expr
= tmp
;
14635 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
14636 asm_expr
= TREE_OPERAND (asm_expr
, 0);
14637 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
14644 stmt
= begin_try_block ();
14645 RECUR (TRY_STMTS (t
));
14646 finish_cleanup_try_block (stmt
);
14647 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
14651 tree compound_stmt
= NULL_TREE
;
14653 if (FN_TRY_BLOCK_P (t
))
14654 stmt
= begin_function_try_block (&compound_stmt
);
14656 stmt
= begin_try_block ();
14658 RECUR (TRY_STMTS (t
));
14660 if (FN_TRY_BLOCK_P (t
))
14661 finish_function_try_block (stmt
);
14663 finish_try_block (stmt
);
14665 RECUR (TRY_HANDLERS (t
));
14666 if (FN_TRY_BLOCK_P (t
))
14667 finish_function_handler_sequence (stmt
, compound_stmt
);
14669 finish_handler_sequence (stmt
);
14675 tree decl
= HANDLER_PARMS (t
);
14679 decl
= tsubst (decl
, args
, complain
, in_decl
);
14680 /* Prevent instantiate_decl from trying to instantiate
14681 this variable. We've already done all that needs to be
14683 if (decl
!= error_mark_node
)
14684 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
14686 stmt
= begin_handler ();
14687 finish_handler_parms (decl
, stmt
);
14688 RECUR (HANDLER_BODY (t
));
14689 finish_handler (stmt
);
14694 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
14695 if (CLASS_TYPE_P (tmp
))
14697 /* Local classes are not independent templates; they are
14698 instantiated along with their containing function. And this
14699 way we don't have to deal with pushing out of one local class
14700 to instantiate a member of another local class. */
14702 /* Closures are handled by the LAMBDA_EXPR. */
14703 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
14704 complete_type (tmp
);
14705 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
14706 if (!DECL_ARTIFICIAL (fn
))
14707 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
14711 case STATIC_ASSERT
:
14715 ++c_inhibit_evaluation_warnings
;
14717 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
14720 /*integral_constant_expression_p=*/true);
14721 --c_inhibit_evaluation_warnings
;
14723 finish_static_assert (condition
,
14724 STATIC_ASSERT_MESSAGE (t
),
14725 STATIC_ASSERT_SOURCE_LOCATION (t
),
14726 /*member_p=*/false);
14731 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), false,
14732 args
, complain
, in_decl
);
14733 stmt
= begin_omp_parallel ();
14734 RECUR (OMP_PARALLEL_BODY (t
));
14735 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
14736 = OMP_PARALLEL_COMBINED (t
);
14740 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), false,
14741 args
, complain
, in_decl
);
14742 stmt
= begin_omp_task ();
14743 RECUR (OMP_TASK_BODY (t
));
14744 finish_omp_task (tmp
, stmt
);
14751 case OMP_DISTRIBUTE
:
14753 tree clauses
, body
, pre_body
;
14754 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
14755 tree incrv
= NULL_TREE
;
14758 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), false,
14759 args
, complain
, in_decl
);
14760 if (OMP_FOR_INIT (t
) != NULL_TREE
)
14762 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
14763 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
14764 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
14765 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
14768 stmt
= begin_omp_structured_block ();
14770 pre_body
= push_stmt_list ();
14771 RECUR (OMP_FOR_PRE_BODY (t
));
14772 pre_body
= pop_stmt_list (pre_body
);
14774 if (OMP_FOR_INIT (t
) != NULL_TREE
)
14775 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
14776 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
14777 &clauses
, args
, complain
, in_decl
,
14778 integral_constant_expression_p
);
14780 body
= push_stmt_list ();
14781 RECUR (OMP_FOR_BODY (t
));
14782 body
= pop_stmt_list (body
);
14784 if (OMP_FOR_INIT (t
) != NULL_TREE
)
14785 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
, initv
,
14786 condv
, incrv
, body
, pre_body
, clauses
);
14789 t
= make_node (TREE_CODE (t
));
14790 TREE_TYPE (t
) = void_type_node
;
14791 OMP_FOR_BODY (t
) = body
;
14792 OMP_FOR_PRE_BODY (t
) = pre_body
;
14793 OMP_FOR_CLAUSES (t
) = clauses
;
14794 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
14798 add_stmt (finish_omp_structured_block (stmt
));
14805 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false,
14806 args
, complain
, in_decl
);
14807 stmt
= push_stmt_list ();
14808 RECUR (OMP_BODY (t
));
14809 stmt
= pop_stmt_list (stmt
);
14812 OMP_BODY (t
) = stmt
;
14813 OMP_CLAUSES (t
) = tmp
;
14817 case OMP_TARGET_DATA
:
14819 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false,
14820 args
, complain
, in_decl
);
14821 keep_next_level (true);
14822 stmt
= begin_omp_structured_block ();
14824 RECUR (OMP_BODY (t
));
14825 stmt
= finish_omp_structured_block (stmt
);
14828 OMP_BODY (t
) = stmt
;
14829 OMP_CLAUSES (t
) = tmp
;
14833 case OMP_TARGET_UPDATE
:
14834 tmp
= tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t
), false,
14835 args
, complain
, in_decl
);
14837 OMP_CLAUSES (t
) = tmp
;
14844 case OMP_TASKGROUP
:
14846 stmt
= push_stmt_list ();
14847 RECUR (OMP_BODY (t
));
14848 stmt
= pop_stmt_list (stmt
);
14851 OMP_BODY (t
) = stmt
;
14856 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
14857 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
14859 tree op1
= TREE_OPERAND (t
, 1);
14860 tree rhs1
= NULL_TREE
;
14862 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
14864 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
14865 op1
= TREE_OPERAND (op1
, 1);
14867 lhs
= RECUR (TREE_OPERAND (op1
, 0));
14868 rhs
= RECUR (TREE_OPERAND (op1
, 1));
14869 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
14870 NULL_TREE
, NULL_TREE
, rhs1
,
14871 OMP_ATOMIC_SEQ_CST (t
));
14875 tree op1
= TREE_OPERAND (t
, 1);
14876 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
14877 tree rhs1
= NULL_TREE
;
14878 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
14879 enum tree_code opcode
= NOP_EXPR
;
14880 if (code
== OMP_ATOMIC_READ
)
14882 v
= RECUR (TREE_OPERAND (op1
, 0));
14883 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
14885 else if (code
== OMP_ATOMIC_CAPTURE_OLD
14886 || code
== OMP_ATOMIC_CAPTURE_NEW
)
14888 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
14889 v
= RECUR (TREE_OPERAND (op1
, 0));
14890 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
14891 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
14893 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
14894 op11
= TREE_OPERAND (op11
, 1);
14896 lhs
= RECUR (TREE_OPERAND (op11
, 0));
14897 rhs
= RECUR (TREE_OPERAND (op11
, 1));
14898 opcode
= TREE_CODE (op11
);
14899 if (opcode
== MODIFY_EXPR
)
14905 lhs
= RECUR (TREE_OPERAND (op1
, 0));
14906 rhs
= RECUR (TREE_OPERAND (op1
, 1));
14908 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
14909 OMP_ATOMIC_SEQ_CST (t
));
14913 case TRANSACTION_EXPR
:
14916 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
14917 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
14919 if (TRANSACTION_EXPR_IS_STMT (t
))
14921 tree body
= TRANSACTION_EXPR_BODY (t
);
14922 tree noex
= NULL_TREE
;
14923 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
14925 noex
= MUST_NOT_THROW_COND (body
);
14926 if (noex
== NULL_TREE
)
14927 noex
= boolean_true_node
;
14928 body
= TREE_OPERAND (body
, 0);
14930 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
14932 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
14936 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
14937 RECUR (TRANSACTION_EXPR_BODY (t
)),
14944 case MUST_NOT_THROW_EXPR
:
14946 tree op0
= RECUR (TREE_OPERAND (t
, 0));
14947 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
14948 RETURN (build_must_not_throw_expr (op0
, cond
));
14951 case EXPR_PACK_EXPANSION
:
14952 // Expansions of variadic constraints are viable, expanding
14953 // to a conunction of the expanded terms.
14954 if (TREE_TYPE (t
) == boolean_type_node
)
14955 return tsubst_pack_conjunction (t
, args
, complain
, in_decl
);
14957 error ("invalid use of pack expansion expression");
14958 RETURN (error_mark_node
);
14960 case NONTYPE_ARGUMENT_PACK
:
14961 error ("use %<...%> to expand argument pack");
14962 RETURN (error_mark_node
);
14964 case CILK_SPAWN_STMT
:
14965 cfun
->calls_cilk_spawn
= 1;
14966 RETURN (build_cilk_spawn (EXPR_LOCATION (t
), RECUR (CILK_SPAWN_FN (t
))));
14968 case CILK_SYNC_STMT
:
14969 RETURN (build_cilk_sync ());
14971 case COMPOUND_EXPR
:
14972 tmp
= RECUR (TREE_OPERAND (t
, 0));
14973 if (tmp
== NULL_TREE
)
14974 /* If the first operand was a statement, we're done with it. */
14975 RETURN (RECUR (TREE_OPERAND (t
, 1)));
14976 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
14977 RECUR (TREE_OPERAND (t
, 1)),
14980 case ANNOTATE_EXPR
:
14981 tmp
= RECUR (TREE_OPERAND (t
, 0));
14982 RETURN (build2_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
14983 TREE_TYPE (tmp
), tmp
, RECUR (TREE_OPERAND (t
, 1))));
14986 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
14988 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
14989 /*function_p=*/false,
14990 integral_constant_expression_p
));
14993 RETURN (NULL_TREE
);
14995 input_location
= loc
;
15001 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
15002 function. For description of the body see comment above
15003 cp_parser_omp_declare_reduction_exprs. */
15006 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15008 if (t
== NULL_TREE
|| t
== error_mark_node
)
15011 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
15013 tree_stmt_iterator tsi
;
15016 memset (stmts
, 0, sizeof stmts
);
15017 for (i
= 0, tsi
= tsi_start (t
);
15018 i
< 7 && !tsi_end_p (tsi
);
15019 i
++, tsi_next (&tsi
))
15020 stmts
[i
] = tsi_stmt (tsi
);
15021 gcc_assert (tsi_end_p (tsi
));
15025 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
15026 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
15027 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
15028 args
, complain
, in_decl
);
15029 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
15030 args
, complain
, in_decl
);
15031 DECL_CONTEXT (omp_out
) = current_function_decl
;
15032 DECL_CONTEXT (omp_in
) = current_function_decl
;
15033 keep_next_level (true);
15034 tree block
= begin_omp_structured_block ();
15035 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
15036 block
= finish_omp_structured_block (block
);
15037 block
= maybe_cleanup_point_expr_void (block
);
15038 add_decl_expr (omp_out
);
15039 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
15040 TREE_NO_WARNING (omp_out
) = 1;
15041 add_decl_expr (omp_in
);
15042 finish_expr_stmt (block
);
15046 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
15047 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
15048 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
15049 args
, complain
, in_decl
);
15050 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
15051 args
, complain
, in_decl
);
15052 DECL_CONTEXT (omp_priv
) = current_function_decl
;
15053 DECL_CONTEXT (omp_orig
) = current_function_decl
;
15054 keep_next_level (true);
15055 tree block
= begin_omp_structured_block ();
15056 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
15057 block
= finish_omp_structured_block (block
);
15058 block
= maybe_cleanup_point_expr_void (block
);
15059 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
15060 add_decl_expr (omp_priv
);
15061 add_decl_expr (omp_orig
);
15062 finish_expr_stmt (block
);
15064 add_decl_expr (omp_orig
);
15068 /* T is a postfix-expression that is not being used in a function
15069 call. Return the substituted version of T. */
15072 tsubst_non_call_postfix_expression (tree t
, tree args
,
15073 tsubst_flags_t complain
,
15076 if (TREE_CODE (t
) == SCOPE_REF
)
15077 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
15078 /*done=*/false, /*address_p=*/false);
15080 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
15081 /*function_p=*/false,
15082 /*integral_constant_expression_p=*/false);
15087 /* Like tsubst but deals with expressions and performs semantic
15088 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
15091 tsubst_copy_and_build (tree t
,
15093 tsubst_flags_t complain
,
15096 bool integral_constant_expression_p
)
15098 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
15099 #define RECUR(NODE) \
15100 tsubst_copy_and_build (NODE, args, complain, in_decl, \
15101 /*function_p=*/false, \
15102 integral_constant_expression_p)
15107 if (t
== NULL_TREE
|| t
== error_mark_node
)
15110 loc
= input_location
;
15111 if (EXPR_HAS_LOCATION (t
))
15112 input_location
= EXPR_LOCATION (t
);
15114 /* N3276 decltype magic only applies to calls at the top level or on the
15115 right side of a comma. */
15116 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
15117 complain
&= ~tf_decltype
;
15119 switch (TREE_CODE (t
))
15123 /* Fall through. */
15124 case IDENTIFIER_NODE
:
15128 bool non_integral_constant_expression_p
;
15129 const char *error_msg
;
15131 if (IDENTIFIER_TYPENAME_P (t
))
15133 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15134 t
= mangle_conv_op_name_for_type (new_type
);
15137 /* Look up the name. */
15138 decl
= lookup_name (t
);
15140 /* By convention, expressions use ERROR_MARK_NODE to indicate
15141 failure, not NULL_TREE. */
15142 if (decl
== NULL_TREE
)
15143 decl
= error_mark_node
;
15145 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
15147 integral_constant_expression_p
,
15148 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
15149 &non_integral_constant_expression_p
,
15150 /*template_p=*/false,
15152 /*address_p=*/false,
15153 /*template_arg_p=*/false,
15158 if (!function_p
&& identifier_p (decl
))
15160 if (complain
& tf_error
)
15161 unqualified_name_lookup_error (decl
);
15162 decl
= error_mark_node
;
15167 case TEMPLATE_ID_EXPR
:
15170 tree templ
= RECUR (TREE_OPERAND (t
, 0));
15171 tree targs
= TREE_OPERAND (t
, 1);
15174 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
15176 if (TREE_CODE (templ
) == COMPONENT_REF
)
15178 object
= TREE_OPERAND (templ
, 0);
15179 templ
= TREE_OPERAND (templ
, 1);
15182 object
= NULL_TREE
;
15183 if (variable_template_p (templ
))
15185 templ
= lookup_template_variable (templ
, targs
);
15186 RETURN (finish_template_variable (templ
));
15189 templ
= lookup_template_function (templ
, targs
);
15192 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
15193 object
, templ
, NULL_TREE
));
15195 RETURN (baselink_for_fns (templ
));
15200 tree r
= RECUR (TREE_OPERAND (t
, 0));
15202 if (REFERENCE_REF_P (t
))
15204 /* A type conversion to reference type will be enclosed in
15205 such an indirect ref, but the substitution of the cast
15206 will have also added such an indirect ref. */
15207 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
15208 r
= convert_from_reference (r
);
15211 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
15212 complain
|decltype_flag
);
15218 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15219 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15220 RETURN (build_nop (type
, op0
));
15223 case IMPLICIT_CONV_EXPR
:
15225 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15226 tree expr
= RECUR (TREE_OPERAND (t
, 0));
15227 int flags
= LOOKUP_IMPLICIT
;
15228 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
15229 flags
= LOOKUP_NORMAL
;
15230 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
15236 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15237 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15238 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
15242 case REINTERPRET_CAST_EXPR
:
15243 case CONST_CAST_EXPR
:
15244 case DYNAMIC_CAST_EXPR
:
15245 case STATIC_CAST_EXPR
:
15248 tree op
, r
= NULL_TREE
;
15250 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15251 if (integral_constant_expression_p
15252 && !cast_valid_in_integral_constant_expression_p (type
))
15254 if (complain
& tf_error
)
15255 error ("a cast to a type other than an integral or "
15256 "enumeration type cannot appear in a constant-expression");
15257 RETURN (error_mark_node
);
15260 op
= RECUR (TREE_OPERAND (t
, 0));
15262 warning_sentinel
s(warn_useless_cast
);
15263 switch (TREE_CODE (t
))
15266 r
= build_functional_cast (type
, op
, complain
);
15268 case REINTERPRET_CAST_EXPR
:
15269 r
= build_reinterpret_cast (type
, op
, complain
);
15271 case CONST_CAST_EXPR
:
15272 r
= build_const_cast (type
, op
, complain
);
15274 case DYNAMIC_CAST_EXPR
:
15275 r
= build_dynamic_cast (type
, op
, complain
);
15277 case STATIC_CAST_EXPR
:
15278 r
= build_static_cast (type
, op
, complain
);
15281 gcc_unreachable ();
15287 case POSTDECREMENT_EXPR
:
15288 case POSTINCREMENT_EXPR
:
15289 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
15290 args
, complain
, in_decl
);
15291 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
15292 complain
|decltype_flag
));
15294 case PREDECREMENT_EXPR
:
15295 case PREINCREMENT_EXPR
:
15299 case TRUTH_NOT_EXPR
:
15300 case UNARY_PLUS_EXPR
: /* Unary + */
15301 case REALPART_EXPR
:
15302 case IMAGPART_EXPR
:
15303 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
15304 RECUR (TREE_OPERAND (t
, 0)),
15305 complain
|decltype_flag
));
15307 case FIX_TRUNC_EXPR
:
15308 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
15312 op1
= TREE_OPERAND (t
, 0);
15313 if (TREE_CODE (op1
) == LABEL_DECL
)
15314 RETURN (finish_label_address_expr (DECL_NAME (op1
),
15315 EXPR_LOCATION (op1
)));
15316 if (TREE_CODE (op1
) == SCOPE_REF
)
15317 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
15318 /*done=*/true, /*address_p=*/true);
15320 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
15322 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
15323 complain
|decltype_flag
));
15328 case TRUNC_DIV_EXPR
:
15329 case CEIL_DIV_EXPR
:
15330 case FLOOR_DIV_EXPR
:
15331 case ROUND_DIV_EXPR
:
15332 case EXACT_DIV_EXPR
:
15336 case TRUNC_MOD_EXPR
:
15337 case FLOOR_MOD_EXPR
:
15338 case TRUTH_ANDIF_EXPR
:
15339 case TRUTH_ORIF_EXPR
:
15340 case TRUTH_AND_EXPR
:
15341 case TRUTH_OR_EXPR
:
15357 warning_sentinel
s1(warn_type_limits
);
15358 warning_sentinel
s2(warn_div_by_zero
);
15359 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15360 tree op1
= RECUR (TREE_OPERAND (t
, 1));
15361 tree r
= build_x_binary_op
15362 (input_location
, TREE_CODE (t
),
15364 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
15366 : TREE_CODE (TREE_OPERAND (t
, 0))),
15368 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
15370 : TREE_CODE (TREE_OPERAND (t
, 1))),
15372 complain
|decltype_flag
);
15373 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
15374 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
15379 case POINTER_PLUS_EXPR
:
15381 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15382 tree op1
= RECUR (TREE_OPERAND (t
, 1));
15383 return fold_build_pointer_plus (op0
, op1
);
15387 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
15388 /*address_p=*/false));
15390 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
15391 args
, complain
, in_decl
);
15392 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
15393 RECUR (TREE_OPERAND (t
, 1)),
15394 complain
|decltype_flag
));
15396 case ARRAY_NOTATION_REF
:
15398 tree start_index
, length
, stride
;
15399 op1
= tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t
),
15400 args
, complain
, in_decl
);
15401 start_index
= RECUR (ARRAY_NOTATION_START (t
));
15402 length
= RECUR (ARRAY_NOTATION_LENGTH (t
));
15403 stride
= RECUR (ARRAY_NOTATION_STRIDE (t
));
15404 RETURN (build_array_notation_ref (EXPR_LOCATION (t
), op1
, start_index
,
15405 length
, stride
, TREE_TYPE (op1
)));
15408 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
15409 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
15416 op1
= TREE_OPERAND (t
, 0);
15417 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
15418 op1
= TREE_TYPE (op1
);
15421 /* When there are no ARGS, we are trying to evaluate a
15422 non-dependent expression from the parser. Trying to do
15423 the substitutions may not work. */
15425 op1
= TREE_TYPE (op1
);
15429 ++cp_unevaluated_operand
;
15430 ++c_inhibit_evaluation_warnings
;
15432 op1
= tsubst (op1
, args
, complain
, in_decl
);
15434 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
15435 /*function_p=*/false,
15436 /*integral_constant_expression_p=*/
15438 --cp_unevaluated_operand
;
15439 --c_inhibit_evaluation_warnings
;
15442 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
15443 complain
& tf_error
);
15445 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
15446 complain
& tf_error
);
15447 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
15449 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
15451 if (!processing_template_decl
&& TYPE_P (op1
))
15453 r
= build_min (SIZEOF_EXPR
, size_type_node
,
15454 build1 (NOP_EXPR
, op1
, error_mark_node
));
15455 SIZEOF_EXPR_TYPE_P (r
) = 1;
15458 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
15459 TREE_SIDE_EFFECTS (r
) = 0;
15460 TREE_READONLY (r
) = 1;
15462 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
15467 case AT_ENCODE_EXPR
:
15469 op1
= TREE_OPERAND (t
, 0);
15470 ++cp_unevaluated_operand
;
15471 ++c_inhibit_evaluation_warnings
;
15472 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
15473 /*function_p=*/false,
15474 /*integral_constant_expression_p=*/false);
15475 --cp_unevaluated_operand
;
15476 --c_inhibit_evaluation_warnings
;
15477 RETURN (objc_build_encode_expr (op1
));
15480 case NOEXCEPT_EXPR
:
15481 op1
= TREE_OPERAND (t
, 0);
15482 ++cp_unevaluated_operand
;
15483 ++c_inhibit_evaluation_warnings
;
15484 ++cp_noexcept_operand
;
15485 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
15486 /*function_p=*/false,
15487 /*integral_constant_expression_p=*/false);
15488 --cp_unevaluated_operand
;
15489 --c_inhibit_evaluation_warnings
;
15490 --cp_noexcept_operand
;
15491 RETURN (finish_noexcept_expr (op1
, complain
));
15495 warning_sentinel
s(warn_div_by_zero
);
15496 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
15497 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
15498 tree r
= build_x_modify_expr
15499 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
15500 complain
|decltype_flag
);
15501 /* TREE_NO_WARNING must be set if either the expression was
15502 parenthesized or it uses an operator such as >>= rather
15503 than plain assignment. In the former case, it was already
15504 set and must be copied. In the latter case,
15505 build_x_modify_expr sets it and it must not be reset
15507 if (TREE_NO_WARNING (t
))
15508 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
15514 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
15515 args
, complain
, in_decl
);
15516 /* Remember that there was a reference to this entity. */
15519 RETURN (build_x_arrow (input_location
, op1
, complain
));
15523 tree placement
= RECUR (TREE_OPERAND (t
, 0));
15524 tree init
= RECUR (TREE_OPERAND (t
, 3));
15525 vec
<tree
, va_gc
> *placement_vec
;
15526 vec
<tree
, va_gc
> *init_vec
;
15529 if (placement
== NULL_TREE
)
15530 placement_vec
= NULL
;
15533 placement_vec
= make_tree_vector ();
15534 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
15535 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
15538 /* If there was an initializer in the original tree, but it
15539 instantiated to an empty list, then we should pass a
15540 non-NULL empty vector to tell build_new that it was an
15541 empty initializer() rather than no initializer. This can
15542 only happen when the initializer is a pack expansion whose
15543 parameter packs are of length zero. */
15544 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
15548 init_vec
= make_tree_vector ();
15549 if (init
== void_node
)
15550 gcc_assert (init_vec
!= NULL
);
15553 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
15554 vec_safe_push (init_vec
, TREE_VALUE (init
));
15558 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15559 tree op2
= RECUR (TREE_OPERAND (t
, 2));
15560 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
15561 NEW_EXPR_USE_GLOBAL (t
),
15564 if (placement_vec
!= NULL
)
15565 release_tree_vector (placement_vec
);
15566 if (init_vec
!= NULL
)
15567 release_tree_vector (init_vec
);
15574 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15575 tree op1
= RECUR (TREE_OPERAND (t
, 1));
15576 RETURN (delete_sanity (op0
, op1
,
15577 DELETE_EXPR_USE_VEC (t
),
15578 DELETE_EXPR_USE_GLOBAL (t
),
15582 case COMPOUND_EXPR
:
15584 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
15585 complain
& ~tf_decltype
, in_decl
,
15586 /*function_p=*/false,
15587 integral_constant_expression_p
);
15588 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
15590 RECUR (TREE_OPERAND (t
, 1)),
15591 complain
|decltype_flag
));
15597 vec
<tree
, va_gc
> *call_args
;
15598 unsigned int nargs
, i
;
15603 function
= CALL_EXPR_FN (t
);
15604 /* When we parsed the expression, we determined whether or
15605 not Koenig lookup should be performed. */
15606 koenig_p
= KOENIG_LOOKUP_P (t
);
15607 if (TREE_CODE (function
) == SCOPE_REF
)
15609 qualified_p
= true;
15610 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
15612 /*address_p=*/false);
15614 else if (koenig_p
&& identifier_p (function
))
15616 /* Do nothing; calling tsubst_copy_and_build on an identifier
15617 would incorrectly perform unqualified lookup again.
15619 Note that we can also have an IDENTIFIER_NODE if the earlier
15620 unqualified lookup found a member function; in that case
15621 koenig_p will be false and we do want to do the lookup
15622 again to find the instantiated member function.
15624 FIXME but doing that causes c++/15272, so we need to stop
15625 using IDENTIFIER_NODE in that situation. */
15626 qualified_p
= false;
15630 if (TREE_CODE (function
) == COMPONENT_REF
)
15632 tree op
= TREE_OPERAND (function
, 1);
15634 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
15635 || (BASELINK_P (op
)
15636 && BASELINK_QUALIFIED_P (op
)));
15639 qualified_p
= false;
15641 if (TREE_CODE (function
) == ADDR_EXPR
15642 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
15643 /* Avoid error about taking the address of a constructor. */
15644 function
= TREE_OPERAND (function
, 0);
15646 function
= tsubst_copy_and_build (function
, args
, complain
,
15649 integral_constant_expression_p
);
15651 if (BASELINK_P (function
))
15652 qualified_p
= true;
15655 nargs
= call_expr_nargs (t
);
15656 call_args
= make_tree_vector ();
15657 for (i
= 0; i
< nargs
; ++i
)
15659 tree arg
= CALL_EXPR_ARG (t
, i
);
15661 if (!PACK_EXPANSION_P (arg
) && !pending_expansion_p (arg
))
15662 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
15665 /* Expand the pack expansion and push each entry onto
15667 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
15668 if (TREE_CODE (arg
) == TREE_VEC
)
15670 unsigned int len
, j
;
15672 len
= TREE_VEC_LENGTH (arg
);
15673 for (j
= 0; j
< len
; ++j
)
15675 tree value
= TREE_VEC_ELT (arg
, j
);
15676 if (value
!= NULL_TREE
)
15677 value
= convert_from_reference (value
);
15678 vec_safe_push (call_args
, value
);
15683 /* A partial substitution. Add one entry. */
15684 vec_safe_push (call_args
, arg
);
15689 /* We do not perform argument-dependent lookup if normal
15690 lookup finds a non-function, in accordance with the
15691 expected resolution of DR 218. */
15693 && ((is_overloaded_fn (function
)
15694 /* If lookup found a member function, the Koenig lookup is
15695 not appropriate, even if an unqualified-name was used
15696 to denote the function. */
15697 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
15698 || identifier_p (function
))
15699 /* Only do this when substitution turns a dependent call
15700 into a non-dependent call. */
15701 && type_dependent_expression_p_push (t
)
15702 && !any_type_dependent_arguments_p (call_args
))
15703 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
15705 if (identifier_p (function
)
15706 && !any_type_dependent_arguments_p (call_args
))
15708 if (koenig_p
&& (complain
& tf_warning_or_error
))
15710 /* For backwards compatibility and good diagnostics, try
15711 the unqualified lookup again if we aren't in SFINAE
15713 tree unq
= (tsubst_copy_and_build
15714 (function
, args
, complain
, in_decl
, true,
15715 integral_constant_expression_p
));
15716 if (unq
== error_mark_node
)
15717 RETURN (error_mark_node
);
15719 if (unq
!= function
)
15722 if (INDIRECT_REF_P (fn
))
15723 fn
= TREE_OPERAND (fn
, 0);
15724 if (TREE_CODE (fn
) == COMPONENT_REF
)
15725 fn
= TREE_OPERAND (fn
, 1);
15726 if (is_overloaded_fn (fn
))
15727 fn
= get_first_fn (fn
);
15728 if (permerror (EXPR_LOC_OR_LOC (t
, input_location
),
15729 "%qD was not declared in this scope, "
15730 "and no declarations were found by "
15731 "argument-dependent lookup at the point "
15732 "of instantiation", function
))
15735 /* Can't say anything more. */;
15736 else if (DECL_CLASS_SCOPE_P (fn
))
15738 location_t loc
= EXPR_LOC_OR_LOC (t
,
15741 "declarations in dependent base %qT are "
15742 "not found by unqualified lookup",
15743 DECL_CLASS_CONTEXT (fn
));
15744 if (current_class_ptr
)
15746 "use %<this->%D%> instead", function
);
15749 "use %<%T::%D%> instead",
15750 current_class_name
, function
);
15753 inform (0, "%q+D declared here, later in the "
15754 "translation unit", fn
);
15759 if (identifier_p (function
))
15761 if (complain
& tf_error
)
15762 unqualified_name_lookup_error (function
);
15763 release_tree_vector (call_args
);
15764 RETURN (error_mark_node
);
15768 /* Remember that there was a reference to this entity. */
15769 if (DECL_P (function
))
15770 mark_used (function
, complain
);
15772 /* Put back tf_decltype for the actual call. */
15773 complain
|= decltype_flag
;
15775 if (TREE_CODE (function
) == OFFSET_REF
)
15776 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
15778 else if (TREE_CODE (function
) == COMPONENT_REF
)
15780 tree instance
= TREE_OPERAND (function
, 0);
15781 tree fn
= TREE_OPERAND (function
, 1);
15783 if (processing_template_decl
15784 && (type_dependent_expression_p (instance
)
15785 || (!BASELINK_P (fn
)
15786 && TREE_CODE (fn
) != FIELD_DECL
)
15787 || type_dependent_expression_p (fn
)
15788 || any_type_dependent_arguments_p (call_args
)))
15789 ret
= build_nt_call_vec (function
, call_args
);
15790 else if (!BASELINK_P (fn
))
15791 ret
= finish_call_expr (function
, &call_args
,
15792 /*disallow_virtual=*/false,
15793 /*koenig_p=*/false,
15796 ret
= (build_new_method_call
15798 &call_args
, NULL_TREE
,
15799 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
15804 ret
= finish_call_expr (function
, &call_args
,
15805 /*disallow_virtual=*/qualified_p
,
15809 release_tree_vector (call_args
);
15816 tree cond
= RECUR (TREE_OPERAND (t
, 0));
15817 tree folded_cond
= fold_non_dependent_expr (cond
);
15820 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
15822 if (integer_zerop (folded_cond
))
15824 ++c_inhibit_evaluation_warnings
;
15825 exp1
= RECUR (TREE_OPERAND (t
, 1));
15826 --c_inhibit_evaluation_warnings
;
15827 exp2
= RECUR (TREE_OPERAND (t
, 2));
15831 exp1
= RECUR (TREE_OPERAND (t
, 1));
15832 ++c_inhibit_evaluation_warnings
;
15833 exp2
= RECUR (TREE_OPERAND (t
, 2));
15834 --c_inhibit_evaluation_warnings
;
15836 cond
= folded_cond
;
15840 exp1
= RECUR (TREE_OPERAND (t
, 1));
15841 exp2
= RECUR (TREE_OPERAND (t
, 2));
15844 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
15845 cond
, exp1
, exp2
, complain
));
15848 case PSEUDO_DTOR_EXPR
:
15850 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15851 tree op1
= RECUR (TREE_OPERAND (t
, 1));
15852 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15853 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
15859 tree purpose
, value
, chain
;
15861 if (t
== void_list_node
)
15864 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
15865 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
15867 /* We have pack expansions, so expand those and
15868 create a new list out of it. */
15869 tree purposevec
= NULL_TREE
;
15870 tree valuevec
= NULL_TREE
;
15874 /* Expand the argument expressions. */
15875 if (TREE_PURPOSE (t
))
15876 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
15877 complain
, in_decl
);
15878 if (TREE_VALUE (t
))
15879 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
15880 complain
, in_decl
);
15882 /* Build the rest of the list. */
15883 chain
= TREE_CHAIN (t
);
15884 if (chain
&& chain
!= void_type_node
)
15885 chain
= RECUR (chain
);
15887 /* Determine the number of arguments. */
15888 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
15890 len
= TREE_VEC_LENGTH (purposevec
);
15891 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
15893 else if (TREE_CODE (valuevec
) == TREE_VEC
)
15894 len
= TREE_VEC_LENGTH (valuevec
);
15897 /* Since we only performed a partial substitution into
15898 the argument pack, we only RETURN (a single list
15900 if (purposevec
== TREE_PURPOSE (t
)
15901 && valuevec
== TREE_VALUE (t
)
15902 && chain
== TREE_CHAIN (t
))
15905 RETURN (tree_cons (purposevec
, valuevec
, chain
));
15908 /* Convert the argument vectors into a TREE_LIST */
15912 /* Grab the Ith values. */
15914 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
15917 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
15920 /* Build the list (backwards). */
15921 chain
= tree_cons (purpose
, value
, chain
);
15927 purpose
= TREE_PURPOSE (t
);
15929 purpose
= RECUR (purpose
);
15930 value
= TREE_VALUE (t
);
15932 value
= RECUR (value
);
15933 chain
= TREE_CHAIN (t
);
15934 if (chain
&& chain
!= void_type_node
)
15935 chain
= RECUR (chain
);
15936 if (purpose
== TREE_PURPOSE (t
)
15937 && value
== TREE_VALUE (t
)
15938 && chain
== TREE_CHAIN (t
))
15940 RETURN (tree_cons (purpose
, value
, chain
));
15943 case COMPONENT_REF
:
15950 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
15951 args
, complain
, in_decl
);
15952 /* Remember that there was a reference to this entity. */
15953 if (DECL_P (object
))
15954 mark_used (object
);
15955 object_type
= TREE_TYPE (object
);
15957 member
= TREE_OPERAND (t
, 1);
15958 if (BASELINK_P (member
))
15959 member
= tsubst_baselink (member
,
15960 non_reference (TREE_TYPE (object
)),
15961 args
, complain
, in_decl
);
15963 member
= tsubst_copy (member
, args
, complain
, in_decl
);
15964 if (member
== error_mark_node
)
15965 RETURN (error_mark_node
);
15967 if (type_dependent_expression_p (object
))
15968 /* We can't do much here. */;
15969 else if (!CLASS_TYPE_P (object_type
))
15971 if (scalarish_type_p (object_type
))
15973 tree s
= NULL_TREE
;
15974 tree dtor
= member
;
15976 if (TREE_CODE (dtor
) == SCOPE_REF
)
15978 s
= TREE_OPERAND (dtor
, 0);
15979 dtor
= TREE_OPERAND (dtor
, 1);
15981 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
15983 dtor
= TREE_OPERAND (dtor
, 0);
15985 RETURN (finish_pseudo_destructor_expr
15986 (object
, s
, dtor
, input_location
));
15990 else if (TREE_CODE (member
) == SCOPE_REF
15991 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
15993 /* Lookup the template functions now that we know what the
15995 tree scope
= TREE_OPERAND (member
, 0);
15996 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
15997 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
15998 member
= lookup_qualified_name (scope
, tmpl
,
15999 /*is_type_p=*/false,
16000 /*complain=*/false);
16001 if (BASELINK_P (member
))
16003 BASELINK_FUNCTIONS (member
)
16004 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
16006 member
= (adjust_result_of_qualified_name_lookup
16007 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
16012 qualified_name_lookup_error (scope
, tmpl
, member
,
16014 RETURN (error_mark_node
);
16017 else if (TREE_CODE (member
) == SCOPE_REF
16018 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
16019 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
16021 if (complain
& tf_error
)
16023 if (TYPE_P (TREE_OPERAND (member
, 0)))
16024 error ("%qT is not a class or namespace",
16025 TREE_OPERAND (member
, 0));
16027 error ("%qD is not a class or namespace",
16028 TREE_OPERAND (member
, 0));
16030 RETURN (error_mark_node
);
16032 else if (TREE_CODE (member
) == FIELD_DECL
)
16034 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
16035 if (TREE_CODE (r
) == COMPONENT_REF
)
16036 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
16040 r
= finish_class_member_access_expr (object
, member
,
16041 /*template_p=*/false,
16043 if (TREE_CODE (r
) == COMPONENT_REF
)
16044 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
16049 RETURN (build_throw
16050 (RECUR (TREE_OPERAND (t
, 0))));
16054 vec
<constructor_elt
, va_gc
> *n
;
16055 constructor_elt
*ce
;
16056 unsigned HOST_WIDE_INT idx
;
16057 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16058 bool process_index_p
;
16060 bool need_copy_p
= false;
16063 if (type
== error_mark_node
)
16064 RETURN (error_mark_node
);
16066 /* digest_init will do the wrong thing if we let it. */
16067 if (type
&& TYPE_PTRMEMFUNC_P (type
))
16070 /* We do not want to process the index of aggregate
16071 initializers as they are identifier nodes which will be
16072 looked up by digest_init. */
16073 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
16075 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
16076 newlen
= vec_safe_length (n
);
16077 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
16079 if (ce
->index
&& process_index_p
16080 /* An identifier index is looked up in the type
16081 being initialized, not the current scope. */
16082 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
16083 ce
->index
= RECUR (ce
->index
);
16085 if (PACK_EXPANSION_P (ce
->value
))
16087 /* Substitute into the pack expansion. */
16088 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
16091 if (ce
->value
== error_mark_node
16092 || PACK_EXPANSION_P (ce
->value
))
16094 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
16095 /* Just move the argument into place. */
16096 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
16099 /* Update the length of the final CONSTRUCTOR
16100 arguments vector, and note that we will need to
16102 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
16103 need_copy_p
= true;
16107 ce
->value
= RECUR (ce
->value
);
16112 vec
<constructor_elt
, va_gc
> *old_n
= n
;
16114 vec_alloc (n
, newlen
);
16115 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
16117 if (TREE_CODE (ce
->value
) == TREE_VEC
)
16119 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
16120 for (i
= 0; i
< len
; ++i
)
16121 CONSTRUCTOR_APPEND_ELT (n
, 0,
16122 TREE_VEC_ELT (ce
->value
, i
));
16125 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
16129 r
= build_constructor (init_list_type_node
, n
);
16130 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
16132 if (TREE_HAS_CONSTRUCTOR (t
))
16133 RETURN (finish_compound_literal (type
, r
, complain
));
16135 TREE_TYPE (r
) = type
;
16141 tree operand_0
= TREE_OPERAND (t
, 0);
16142 if (TYPE_P (operand_0
))
16144 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
16145 RETURN (get_typeid (operand_0
, complain
));
16149 operand_0
= RECUR (operand_0
);
16150 RETURN (build_typeid (operand_0
, complain
));
16157 else if (DECL_PACK_P (t
))
16159 /* We don't build decls for an instantiation of a
16160 variadic capture proxy, we instantiate the elements
16162 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
16163 return RECUR (DECL_VALUE_EXPR (t
));
16169 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
16170 /* ??? We're doing a subset of finish_id_expression here. */
16172 && !processing_template_decl
16173 && !cp_unevaluated_operand
16174 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
16175 && DECL_THREAD_LOCAL_P (r
))
16177 if (tree wrap
= get_tls_wrapper_fn (r
))
16178 /* Replace an evaluated use of the thread_local variable with
16179 a call to its wrapper. */
16180 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
16182 else if (outer_automatic_var_p (r
))
16183 r
= process_outer_var_ref (r
, complain
);
16185 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
16186 /* If the original type was a reference, we'll be wrapped in
16187 the appropriate INDIRECT_REF. */
16188 r
= convert_from_reference (r
);
16195 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16196 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16197 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
16200 case OFFSETOF_EXPR
:
16201 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t
, 0)),
16202 EXPR_LOCATION (t
)));
16206 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
16207 complain
, in_decl
);
16209 tree type2
= TRAIT_EXPR_TYPE2 (t
);
16210 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
16211 type2
= RECUR (type2
);
16213 type2
= tsubst (type2
, args
, complain
, in_decl
);
16215 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
16220 tree old_stmt_expr
= cur_stmt_expr
;
16221 tree stmt_expr
= begin_stmt_expr ();
16223 cur_stmt_expr
= stmt_expr
;
16224 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
16225 integral_constant_expression_p
);
16226 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
16227 cur_stmt_expr
= old_stmt_expr
;
16229 /* If the resulting list of expression statement is empty,
16230 fold it further into void_node. */
16231 if (empty_expr_stmt_p (stmt_expr
))
16232 stmt_expr
= void_node
;
16234 RETURN (stmt_expr
);
16239 tree r
= build_lambda_expr ();
16241 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
16242 LAMBDA_EXPR_CLOSURE (r
) = type
;
16243 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
16245 LAMBDA_EXPR_LOCATION (r
)
16246 = LAMBDA_EXPR_LOCATION (t
);
16247 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
16248 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
16249 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
16250 LAMBDA_EXPR_DISCRIMINATOR (r
)
16251 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
16252 /* For a function scope, we want to use tsubst so that we don't
16253 complain about referring to an auto function before its return
16254 type has been deduced. Otherwise, we want to use tsubst_copy so
16255 that we look up the existing field/parameter/variable rather
16256 than build a new one. */
16257 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
16258 if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
16259 scope
= tsubst (scope
, args
, complain
, in_decl
);
16260 else if (scope
&& TREE_CODE (scope
) == PARM_DECL
)
16262 /* Look up the parameter we want directly, as tsubst_copy
16263 doesn't do what we need. */
16264 tree fn
= tsubst (DECL_CONTEXT (scope
), args
, complain
, in_decl
);
16265 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
16266 while (DECL_PARM_INDEX (parm
) != DECL_PARM_INDEX (scope
))
16267 parm
= DECL_CHAIN (parm
);
16269 /* FIXME Work around the parm not having DECL_CONTEXT set. */
16270 if (DECL_CONTEXT (scope
) == NULL_TREE
)
16271 DECL_CONTEXT (scope
) = fn
;
16274 scope
= RECUR (scope
);
16275 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
16276 LAMBDA_EXPR_RETURN_TYPE (r
)
16277 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
16279 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
16280 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
16282 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16283 determine_visibility (TYPE_NAME (type
));
16284 /* Now that we know visibility, instantiate the type so we have a
16285 declaration of the op() for later calls to lambda_function. */
16286 complete_type (type
);
16288 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
16290 RETURN (build_lambda_object (r
));
16294 /* We can get here for a constant initializer of non-dependent type.
16295 FIXME stop folding in cp_parser_initializer_clause. */
16297 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
16302 case TRANSACTION_EXPR
:
16303 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
16304 integral_constant_expression_p
));
16307 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
16309 case VEC_PERM_EXPR
:
16311 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16312 tree op1
= RECUR (TREE_OPERAND (t
, 1));
16313 tree op2
= RECUR (TREE_OPERAND (t
, 2));
16314 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
16318 case REQUIRES_EXPR
:
16319 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
16322 /* Handle Objective-C++ constructs, if appropriate. */
16325 = objcp_tsubst_copy_and_build (t
, args
, complain
,
16326 in_decl
, /*function_p=*/false);
16330 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
16336 input_location
= loc
;
16340 /* Verify that the instantiated ARGS are valid. For type arguments,
16341 make sure that the type's linkage is ok. For non-type arguments,
16342 make sure they are constants if they are integral or enumerations.
16343 Emit an error under control of COMPLAIN, and return TRUE on error. */
16346 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
16348 if (dependent_template_arg_p (t
))
16350 if (ARGUMENT_PACK_P (t
))
16352 tree vec
= ARGUMENT_PACK_ARGS (t
);
16353 int len
= TREE_VEC_LENGTH (vec
);
16354 bool result
= false;
16357 for (i
= 0; i
< len
; ++i
)
16358 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
16362 else if (TYPE_P (t
))
16364 /* [basic.link]: A name with no linkage (notably, the name
16365 of a class or enumeration declared in a local scope)
16366 shall not be used to declare an entity with linkage.
16367 This implies that names with no linkage cannot be used as
16370 DR 757 relaxes this restriction for C++0x. */
16371 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
16372 : no_linkage_check (t
, /*relaxed_p=*/false));
16376 /* DR 488 makes use of a type with no linkage cause
16377 type deduction to fail. */
16378 if (complain
& tf_error
)
16380 if (TYPE_ANONYMOUS_P (nt
))
16381 error ("%qT is/uses anonymous type", t
);
16383 error ("template argument for %qD uses local type %qT",
16388 /* In order to avoid all sorts of complications, we do not
16389 allow variably-modified types as template arguments. */
16390 else if (variably_modified_type_p (t
, NULL_TREE
))
16392 if (complain
& tf_error
)
16393 error ("%qT is a variably modified type", t
);
16397 /* Class template and alias template arguments should be OK. */
16398 else if (DECL_TYPE_TEMPLATE_P (t
))
16400 /* A non-type argument of integral or enumerated type must be a
16402 else if (TREE_TYPE (t
)
16403 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
16404 && !REFERENCE_REF_P (t
)
16405 && !TREE_CONSTANT (t
))
16407 if (complain
& tf_error
)
16408 error ("integral expression %qE is not constant", t
);
16415 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
16417 int ix
, len
= DECL_NTPARMS (tmpl
);
16418 bool result
= false;
16420 for (ix
= 0; ix
!= len
; ix
++)
16422 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
16425 if (result
&& (complain
& tf_error
))
16426 error (" trying to instantiate %qD", tmpl
);
16430 /* We're out of SFINAE context now, so generate diagnostics for the access
16431 errors we saw earlier when instantiating D from TMPL and ARGS. */
16434 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
16436 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
16437 tree type
= TREE_TYPE (pattern
);
16438 location_t loc
= input_location
;
16440 push_access_scope (d
);
16441 push_deferring_access_checks (dk_no_deferred
);
16442 input_location
= DECL_SOURCE_LOCATION (pattern
);
16443 tsubst (type
, args
, tf_warning_or_error
, d
);
16444 input_location
= loc
;
16445 pop_deferring_access_checks ();
16446 pop_access_scope (d
);
16449 /* Instantiate the indicated variable, function, or alias template TMPL with
16450 the template arguments in TARG_PTR. */
16453 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
16455 tree targ_ptr
= orig_args
;
16459 bool access_ok
= true;
16461 if (tmpl
== error_mark_node
)
16462 return error_mark_node
;
16464 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
16466 /* If this function is a clone, handle it specially. */
16467 if (DECL_CLONED_FUNCTION_P (tmpl
))
16472 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
16473 DECL_CLONED_FUNCTION. */
16474 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
16475 targ_ptr
, complain
);
16476 if (spec
== error_mark_node
)
16477 return error_mark_node
;
16479 /* Look for the clone. */
16480 FOR_EACH_CLONE (clone
, spec
)
16481 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
16483 /* We should always have found the clone by now. */
16484 gcc_unreachable ();
16488 if (targ_ptr
== error_mark_node
)
16489 return error_mark_node
;
16491 /* Check to see if we already have this specialization. */
16492 gen_tmpl
= most_general_template (tmpl
);
16493 if (tmpl
!= gen_tmpl
)
16494 /* The TMPL is a partial instantiation. To get a full set of
16495 arguments we must add the arguments used to perform the
16496 partial instantiation. */
16497 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
16500 /* It would be nice to avoid hashing here and then again in tsubst_decl,
16501 but it doesn't seem to be on the hot path. */
16502 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
16504 gcc_assert (tmpl
== gen_tmpl
16505 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
16507 || fndecl
== NULL_TREE
);
16509 if (spec
!= NULL_TREE
)
16511 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
16513 if (complain
& tf_error
)
16514 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
16515 return error_mark_node
;
16520 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
16522 return error_mark_node
;
16524 /* We are building a FUNCTION_DECL, during which the access of its
16525 parameters and return types have to be checked. However this
16526 FUNCTION_DECL which is the desired context for access checking
16527 is not built yet. We solve this chicken-and-egg problem by
16528 deferring all checks until we have the FUNCTION_DECL. */
16529 push_deferring_access_checks (dk_deferred
);
16531 /* Instantiation of the function happens in the context of the function
16532 template, not the context of the overload resolution we're doing. */
16533 push_to_top_level ();
16534 /* If there are dependent arguments, e.g. because we're doing partial
16535 ordering, make sure processing_template_decl stays set. */
16536 if (uses_template_parms (targ_ptr
))
16537 ++processing_template_decl
;
16538 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
16540 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
16541 complain
, gen_tmpl
, true);
16542 push_nested_class (ctx
);
16545 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
16547 if (VAR_P (pattern
))
16549 /* We need to determine if we're using a partial or explicit
16550 specialization now, because the type of the variable could be
16552 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
16553 tree elt
= most_specialized_partial_spec (tid
, complain
);
16554 if (elt
== error_mark_node
)
16555 pattern
= error_mark_node
;
16558 tmpl
= TREE_VALUE (elt
);
16559 pattern
= DECL_TEMPLATE_RESULT (tmpl
);
16560 targ_ptr
= TREE_PURPOSE (elt
);
16564 /* Substitute template parameters to obtain the specialization. */
16565 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
16566 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
16567 pop_nested_class ();
16568 pop_from_top_level ();
16570 if (fndecl
== error_mark_node
)
16572 pop_deferring_access_checks ();
16573 return error_mark_node
;
16576 /* The DECL_TI_TEMPLATE should always be the immediate parent
16577 template, not the most general template. */
16578 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
16580 /* Now we know the specialization, compute access previously
16582 push_access_scope (fndecl
);
16583 if (!perform_deferred_access_checks (complain
))
16585 pop_access_scope (fndecl
);
16586 pop_deferring_access_checks ();
16588 /* If we've just instantiated the main entry point for a function,
16589 instantiate all the alternate entry points as well. We do this
16590 by cloning the instantiation of the main entry point, not by
16591 instantiating the template clones. */
16592 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
16593 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
16597 if (!(complain
& tf_error
))
16599 /* Remember to reinstantiate when we're out of SFINAE so the user
16600 can see the errors. */
16601 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
16603 return error_mark_node
;
16608 /* Wrapper for instantiate_template_1. */
16611 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
16614 timevar_push (TV_TEMPLATE_INST
);
16615 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
16616 timevar_pop (TV_TEMPLATE_INST
);
16620 /* Instantiate the alias template TMPL with ARGS. Also push a template
16621 instantiation level, which instantiate_template doesn't do because
16622 functions and variables have sufficient context established by the
16626 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
16628 struct pending_template
*old_last_pend
= last_pending_template
;
16629 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
16630 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
16631 return error_mark_node
;
16632 tree tinst
= build_tree_list (tmpl
, args
);
16633 if (!push_tinst_level (tinst
))
16636 return error_mark_node
;
16640 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
16641 args
, tmpl
, complain
,
16642 /*require_all_args=*/true,
16643 /*use_default_args=*/true);
16645 tree r
= instantiate_template (tmpl
, args
, complain
);
16646 pop_tinst_level ();
16647 /* We can't free this if a pending_template entry or last_error_tinst_level
16648 is pointing at it. */
16649 if (last_pending_template
== old_last_pend
16650 && last_error_tinst_level
== old_error_tinst
) {
16657 /* PARM is a template parameter pack for FN. Returns true iff
16658 PARM is used in a deducible way in the argument list of FN. */
16661 pack_deducible_p (tree parm
, tree fn
)
16663 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
16664 for (; t
; t
= TREE_CHAIN (t
))
16666 tree type
= TREE_VALUE (t
);
16668 if (!PACK_EXPANSION_P (type
))
16670 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
16671 packs
; packs
= TREE_CHAIN (packs
))
16672 if (template_args_equal (TREE_VALUE (packs
), parm
))
16674 /* The template parameter pack is used in a function parameter
16675 pack. If this is the end of the parameter list, the
16676 template parameter pack is deducible. */
16677 if (TREE_CHAIN (t
) == void_list_node
)
16680 /* Otherwise, not. Well, it could be deduced from
16681 a non-pack parameter, but doing so would end up with
16682 a deduction mismatch, so don't bother. */
16686 /* The template parameter pack isn't used in any function parameter
16687 packs, but it might be used deeper, e.g. tuple<Args...>. */
16691 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
16692 NARGS elements of the arguments that are being used when calling
16693 it. TARGS is a vector into which the deduced template arguments
16696 Returns either a FUNCTION_DECL for the matching specialization of FN or
16697 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
16698 true, diagnostics will be printed to explain why it failed.
16700 If FN is a conversion operator, or we are trying to produce a specific
16701 specialization, RETURN_TYPE is the return type desired.
16703 The EXPLICIT_TARGS are explicit template arguments provided via a
16706 The parameter STRICT is one of:
16709 We are deducing arguments for a function call, as in
16710 [temp.deduct.call].
16713 We are deducing arguments for a conversion function, as in
16714 [temp.deduct.conv].
16717 We are deducing arguments when doing an explicit instantiation
16718 as in [temp.explicit], when determining an explicit specialization
16719 as in [temp.expl.spec], or when taking the address of a function
16720 template, as in [temp.deduct.funcaddr]. */
16723 fn_type_unification (tree fn
,
16724 tree explicit_targs
,
16727 unsigned int nargs
,
16729 unification_kind_t strict
,
16736 tree decl
= NULL_TREE
;
16737 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
16739 static int deduction_depth
;
16740 struct pending_template
*old_last_pend
= last_pending_template
;
16741 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
16742 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
16744 tree r
= error_mark_node
;
16747 complain
|= tf_decltype
;
16749 /* In C++0x, it's possible to have a function template whose type depends
16750 on itself recursively. This is most obvious with decltype, but can also
16751 occur with enumeration scope (c++/48969). So we need to catch infinite
16752 recursion and reject the substitution at deduction time; this function
16753 will return error_mark_node for any repeated substitution.
16755 This also catches excessive recursion such as when f<N> depends on
16756 f<N-1> across all integers, and returns error_mark_node for all the
16757 substitutions back up to the initial one.
16759 This is, of course, not reentrant. */
16760 if (excessive_deduction_depth
)
16761 return error_mark_node
;
16762 tinst
= build_tree_list (fn
, NULL_TREE
);
16765 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
16767 fntype
= TREE_TYPE (fn
);
16768 if (explicit_targs
)
16772 The specified template arguments must match the template
16773 parameters in kind (i.e., type, nontype, template), and there
16774 must not be more arguments than there are parameters;
16775 otherwise type deduction fails.
16777 Nontype arguments must match the types of the corresponding
16778 nontype template parameters, or must be convertible to the
16779 types of the corresponding nontype parameters as specified in
16780 _temp.arg.nontype_, otherwise type deduction fails.
16782 All references in the function type of the function template
16783 to the corresponding template parameters are replaced by the
16784 specified template argument values. If a substitution in a
16785 template parameter or in the function type of the function
16786 template results in an invalid type, type deduction fails. */
16787 int i
, len
= TREE_VEC_LENGTH (tparms
);
16788 location_t loc
= input_location
;
16789 bool incomplete
= false;
16791 /* Adjust any explicit template arguments before entering the
16792 substitution context. */
16794 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
16796 /*require_all_args=*/false,
16797 /*use_default_args=*/false));
16798 if (explicit_targs
== error_mark_node
)
16801 /* Substitute the explicit args into the function type. This is
16802 necessary so that, for instance, explicitly declared function
16803 arguments can match null pointed constants. If we were given
16804 an incomplete set of explicit args, we must not do semantic
16805 processing during substitution as we could create partial
16807 for (i
= 0; i
< len
; i
++)
16809 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
16810 bool parameter_pack
= false;
16811 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
16813 /* Dig out the actual parm. */
16814 if (TREE_CODE (parm
) == TYPE_DECL
16815 || TREE_CODE (parm
) == TEMPLATE_DECL
)
16817 parm
= TREE_TYPE (parm
);
16818 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
16820 else if (TREE_CODE (parm
) == PARM_DECL
)
16822 parm
= DECL_INITIAL (parm
);
16823 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
16826 if (!parameter_pack
&& targ
== NULL_TREE
)
16827 /* No explicit argument for this template parameter. */
16830 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
16832 /* Mark the argument pack as "incomplete". We could
16833 still deduce more arguments during unification.
16834 We remove this mark in type_unification_real. */
16837 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
16838 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
16839 = ARGUMENT_PACK_ARGS (targ
);
16842 /* We have some incomplete argument packs. */
16847 TREE_VALUE (tinst
) = explicit_targs
;
16848 if (!push_tinst_level (tinst
))
16850 excessive_deduction_depth
= true;
16853 processing_template_decl
+= incomplete
;
16854 input_location
= DECL_SOURCE_LOCATION (fn
);
16855 /* Ignore any access checks; we'll see them again in
16856 instantiate_template and they might have the wrong
16857 access path at this point. */
16858 push_deferring_access_checks (dk_deferred
);
16859 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
16860 complain
| tf_partial
, NULL_TREE
);
16861 pop_deferring_access_checks ();
16862 input_location
= loc
;
16863 processing_template_decl
-= incomplete
;
16864 pop_tinst_level ();
16866 if (fntype
== error_mark_node
)
16869 /* Place the explicitly specified arguments in TARGS. */
16870 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
16871 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
16874 /* Never do unification on the 'this' parameter. */
16875 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
16881 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
16882 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
16883 new_args
[0] = return_type
;
16884 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
16889 /* We allow incomplete unification without an error message here
16890 because the standard doesn't seem to explicitly prohibit it. Our
16891 callers must be ready to deal with unification failures in any
16894 TREE_VALUE (tinst
) = targs
;
16895 /* If we aren't explaining yet, push tinst context so we can see where
16896 any errors (e.g. from class instantiations triggered by instantiation
16897 of default template arguments) come from. If we are explaining, this
16898 context is redundant. */
16899 if (!explain_p
&& !push_tinst_level (tinst
))
16901 excessive_deduction_depth
= true;
16905 /* type_unification_real will pass back any access checks from default
16906 template argument substitution. */
16907 vec
<deferred_access_check
, va_gc
> *checks
;
16910 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
16911 targs
, parms
, args
, nargs
, /*subr=*/0,
16912 strict
, flags
, &checks
, explain_p
);
16914 pop_tinst_level ();
16918 /* Now that we have bindings for all of the template arguments,
16919 ensure that the arguments deduced for the template template
16920 parameters have compatible template parameter lists. We cannot
16921 check this property before we have deduced all template
16922 arguments, because the template parameter types of a template
16923 template parameter might depend on prior template parameters
16924 deduced after the template template parameter. The following
16925 ill-formed example illustrates this issue:
16927 template<typename T, template<T> class C> void f(C<5>, T);
16929 template<int N> struct X {};
16932 f(X<5>(), 5l); // error: template argument deduction fails
16935 The template parameter list of 'C' depends on the template type
16936 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
16937 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
16938 time that we deduce 'C'. */
16939 if (!template_template_parm_bindings_ok_p
16940 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
16942 unify_inconsistent_template_template_parameters (explain_p
);
16946 /* All is well so far. Now, check:
16950 When all template arguments have been deduced, all uses of
16951 template parameters in nondeduced contexts are replaced with
16952 the corresponding deduced argument values. If the
16953 substitution results in an invalid type, as described above,
16954 type deduction fails. */
16955 TREE_VALUE (tinst
) = targs
;
16956 if (!push_tinst_level (tinst
))
16958 excessive_deduction_depth
= true;
16962 /* Also collect access checks from the instantiation. */
16963 reopen_deferring_access_checks (checks
);
16965 decl
= instantiate_template (fn
, targs
, complain
);
16967 checks
= get_deferred_access_checks ();
16968 pop_deferring_access_checks ();
16970 pop_tinst_level ();
16972 if (decl
== error_mark_node
)
16975 /* Now perform any access checks encountered during substitution. */
16976 push_access_scope (decl
);
16977 ok
= perform_access_checks (checks
, complain
);
16978 pop_access_scope (decl
);
16982 /* If we're looking for an exact match, check that what we got
16983 is indeed an exact match. It might not be if some template
16984 parameters are used in non-deduced contexts. But don't check
16985 for an exact match if we have dependent template arguments;
16986 in that case we're doing partial ordering, and we already know
16987 that we have two candidates that will provide the actual type. */
16988 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
16990 tree substed
= TREE_TYPE (decl
);
16994 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
16996 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
16997 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
16998 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
17000 unify_type_mismatch (explain_p
, args
[i
],
17001 TREE_VALUE (sarg
));
17010 if (excessive_deduction_depth
)
17012 if (deduction_depth
== 0)
17013 /* Reset once we're all the way out. */
17014 excessive_deduction_depth
= false;
17017 /* We can't free this if a pending_template entry or last_error_tinst_level
17018 is pointing at it. */
17019 if (last_pending_template
== old_last_pend
17020 && last_error_tinst_level
== old_error_tinst
) {
17027 /* Adjust types before performing type deduction, as described in
17028 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
17029 sections are symmetric. PARM is the type of a function parameter
17030 or the return type of the conversion function. ARG is the type of
17031 the argument passed to the call, or the type of the value
17032 initialized with the result of the conversion function.
17033 ARG_EXPR is the original argument expression, which may be null. */
17036 maybe_adjust_types_for_deduction (unification_kind_t strict
,
17050 /* Swap PARM and ARG throughout the remainder of this
17051 function; the handling is precisely symmetric since PARM
17052 will initialize ARG rather than vice versa. */
17060 /* Core issue #873: Do the DR606 thing (see below) for these cases,
17061 too, but here handle it by stripping the reference from PARM
17062 rather than by adding it to ARG. */
17063 if (TREE_CODE (*parm
) == REFERENCE_TYPE
17064 && TYPE_REF_IS_RVALUE (*parm
)
17065 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
17066 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
17067 && TREE_CODE (*arg
) == REFERENCE_TYPE
17068 && !TYPE_REF_IS_RVALUE (*arg
))
17069 *parm
= TREE_TYPE (*parm
);
17070 /* Nothing else to do in this case. */
17074 gcc_unreachable ();
17077 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
17079 /* [temp.deduct.call]
17081 If P is not a reference type:
17083 --If A is an array type, the pointer type produced by the
17084 array-to-pointer standard conversion (_conv.array_) is
17085 used in place of A for type deduction; otherwise,
17087 --If A is a function type, the pointer type produced by
17088 the function-to-pointer standard conversion
17089 (_conv.func_) is used in place of A for type deduction;
17092 --If A is a cv-qualified type, the top level
17093 cv-qualifiers of A's type are ignored for type
17095 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
17096 *arg
= build_pointer_type (TREE_TYPE (*arg
));
17097 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
17098 *arg
= build_pointer_type (*arg
);
17100 *arg
= TYPE_MAIN_VARIANT (*arg
);
17103 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
17104 of the form T&&, where T is a template parameter, and the argument
17105 is an lvalue, T is deduced as A& */
17106 if (TREE_CODE (*parm
) == REFERENCE_TYPE
17107 && TYPE_REF_IS_RVALUE (*parm
)
17108 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
17109 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
17110 && (arg_expr
? real_lvalue_p (arg_expr
)
17111 /* try_one_overload doesn't provide an arg_expr, but
17112 functions are always lvalues. */
17113 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
17114 *arg
= build_reference_type (*arg
);
17116 /* [temp.deduct.call]
17118 If P is a cv-qualified type, the top level cv-qualifiers
17119 of P's type are ignored for type deduction. If P is a
17120 reference type, the type referred to by P is used for
17122 *parm
= TYPE_MAIN_VARIANT (*parm
);
17123 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
17125 *parm
= TREE_TYPE (*parm
);
17126 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
17129 /* DR 322. For conversion deduction, remove a reference type on parm
17130 too (which has been swapped into ARG). */
17131 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
17132 *arg
= TREE_TYPE (*arg
);
17137 /* Subroutine of unify_one_argument. PARM is a function parameter of a
17138 template which does contain any deducible template parameters; check if
17139 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
17140 unify_one_argument. */
17143 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
17144 int flags
, bool explain_p
)
17149 type
= TREE_TYPE (arg
);
17153 if (same_type_p (parm
, type
))
17154 return unify_success (explain_p
);
17156 if (strict
== DEDUCE_CONV
)
17158 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
17159 explain_p
? tf_warning_or_error
: tf_none
))
17160 return unify_success (explain_p
);
17162 else if (strict
!= DEDUCE_EXACT
)
17164 if (can_convert_arg (parm
, type
,
17165 TYPE_P (arg
) ? NULL_TREE
: arg
,
17166 flags
, explain_p
? tf_warning_or_error
: tf_none
))
17167 return unify_success (explain_p
);
17170 if (strict
== DEDUCE_EXACT
)
17171 return unify_type_mismatch (explain_p
, parm
, arg
);
17173 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
17176 static bool uses_deducible_template_parms (tree type
);
17178 /* Returns true iff the expression EXPR is one from which a template
17179 argument can be deduced. In other words, if it's an undecorated
17180 use of a template non-type parameter. */
17183 deducible_expression (tree expr
)
17185 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
17188 /* Returns true iff the array domain DOMAIN uses a template parameter in a
17189 deducible way; that is, if it has a max value of <PARM> - 1. */
17192 deducible_array_bound (tree domain
)
17194 if (domain
== NULL_TREE
)
17197 tree max
= TYPE_MAX_VALUE (domain
);
17198 if (TREE_CODE (max
) != MINUS_EXPR
)
17201 return deducible_expression (TREE_OPERAND (max
, 0));
17204 /* Returns true iff the template arguments ARGS use a template parameter
17205 in a deducible way. */
17208 deducible_template_args (tree args
)
17210 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
17213 tree elt
= TREE_VEC_ELT (args
, i
);
17214 if (ARGUMENT_PACK_P (elt
))
17215 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
17218 if (PACK_EXPANSION_P (elt
))
17219 elt
= PACK_EXPANSION_PATTERN (elt
);
17220 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
17222 else if (TYPE_P (elt
))
17223 deducible
= uses_deducible_template_parms (elt
);
17225 deducible
= deducible_expression (elt
);
17233 /* Returns true iff TYPE contains any deducible references to template
17234 parameters, as per 14.8.2.5. */
17237 uses_deducible_template_parms (tree type
)
17239 if (PACK_EXPANSION_P (type
))
17240 type
= PACK_EXPANSION_PATTERN (type
);
17247 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
17248 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17254 if (POINTER_TYPE_P (type
))
17255 return uses_deducible_template_parms (TREE_TYPE (type
));
17257 /* T[integer-constant ]
17259 if (TREE_CODE (type
) == ARRAY_TYPE
)
17260 return (uses_deducible_template_parms (TREE_TYPE (type
))
17261 || deducible_array_bound (TYPE_DOMAIN (type
)));
17273 if (TYPE_PTRMEM_P (type
))
17274 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
17275 || (uses_deducible_template_parms
17276 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
17278 /* template-name <T> (where template-name refers to a class template)
17279 template-name <i> (where template-name refers to a class template) */
17280 if (CLASS_TYPE_P (type
)
17281 && CLASSTYPE_TEMPLATE_INFO (type
)
17282 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
17283 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
17284 (CLASSTYPE_TI_ARGS (type
)));
17289 if (TREE_CODE (type
) == FUNCTION_TYPE
17290 || TREE_CODE (type
) == METHOD_TYPE
)
17292 if (uses_deducible_template_parms (TREE_TYPE (type
)))
17294 tree parm
= TYPE_ARG_TYPES (type
);
17295 if (TREE_CODE (type
) == METHOD_TYPE
)
17296 parm
= TREE_CHAIN (parm
);
17297 for (; parm
; parm
= TREE_CHAIN (parm
))
17298 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
17305 /* Subroutine of type_unification_real and unify_pack_expansion to
17306 handle unification of a single P/A pair. Parameters are as
17307 for those functions. */
17310 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
17311 int subr
, unification_kind_t strict
, int flags
,
17314 tree arg_expr
= NULL_TREE
;
17317 if (arg
== error_mark_node
|| parm
== error_mark_node
)
17318 return unify_invalid (explain_p
);
17319 if (arg
== unknown_type_node
)
17320 /* We can't deduce anything from this, but we might get all the
17321 template args from other function args. */
17322 return unify_success (explain_p
);
17324 /* Implicit conversions (Clause 4) will be performed on a function
17325 argument to convert it to the type of the corresponding function
17326 parameter if the parameter type contains no template-parameters that
17327 participate in template argument deduction. */
17328 if (TYPE_P (parm
) && !uses_template_parms (parm
))
17329 /* For function parameters that contain no template-parameters at all,
17330 we have historically checked for convertibility in order to shortcut
17331 consideration of this candidate. */
17332 return check_non_deducible_conversion (parm
, arg
, strict
, flags
,
17334 else if (strict
== DEDUCE_CALL
17335 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
17336 /* For function parameters with only non-deducible template parameters,
17338 return unify_success (explain_p
);
17343 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
17344 | UNIFY_ALLOW_MORE_CV_QUAL
17345 | UNIFY_ALLOW_DERIVED
);
17349 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
17353 arg_strict
= UNIFY_ALLOW_NONE
;
17357 gcc_unreachable ();
17360 /* We only do these transformations if this is the top-level
17361 parameter_type_list in a call or declaration matching; in other
17362 situations (nested function declarators, template argument lists) we
17363 won't be comparing a type to an expression, and we don't do any type
17369 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
17370 if (type_unknown_p (arg
))
17372 /* [temp.deduct.type] A template-argument can be
17373 deduced from a pointer to function or pointer
17374 to member function argument if the set of
17375 overloaded functions does not contain function
17376 templates and at most one of a set of
17377 overloaded functions provides a unique
17380 if (resolve_overloaded_unification
17381 (tparms
, targs
, parm
, arg
, strict
,
17382 arg_strict
, explain_p
))
17383 return unify_success (explain_p
);
17384 return unify_overload_resolution_failure (explain_p
, arg
);
17388 arg
= unlowered_expr_type (arg
);
17389 if (arg
== error_mark_node
)
17390 return unify_invalid (explain_p
);
17394 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
17397 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
17398 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
17399 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17401 /* For deduction from an init-list we need the actual list. */
17402 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
17404 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
17407 /* Most parms like fn_type_unification.
17409 If SUBR is 1, we're being called recursively (to unify the
17410 arguments of a function or method parameter of a function
17413 CHECKS is a pointer to a vector of access checks encountered while
17414 substituting default template arguments. */
17417 type_unification_real (tree tparms
,
17421 unsigned int xnargs
,
17423 unification_kind_t strict
,
17425 vec
<deferred_access_check
, va_gc
> **checks
,
17430 int ntparms
= TREE_VEC_LENGTH (tparms
);
17431 int saw_undeduced
= 0;
17434 unsigned int nargs
;
17437 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
17438 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
17439 gcc_assert (ntparms
> 0);
17441 /* Reset the number of non-defaulted template arguments contained
17443 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
17451 while (parms
&& parms
!= void_list_node
17454 parm
= TREE_VALUE (parms
);
17456 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
17457 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
17458 /* For a function parameter pack that occurs at the end of the
17459 parameter-declaration-list, the type A of each remaining
17460 argument of the call is compared with the type P of the
17461 declarator-id of the function parameter pack. */
17464 parms
= TREE_CHAIN (parms
);
17466 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
17467 /* For a function parameter pack that does not occur at the
17468 end of the parameter-declaration-list, the type of the
17469 parameter pack is a non-deduced context. */
17475 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
17481 && parms
!= void_list_node
17482 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
17484 /* Unify the remaining arguments with the pack expansion type. */
17486 tree parmvec
= make_tree_vec (1);
17488 /* Allocate a TREE_VEC and copy in all of the arguments */
17489 argvec
= make_tree_vec (nargs
- ia
);
17490 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
17491 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
17493 /* Copy the parameter into parmvec. */
17494 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
17495 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
17496 /*subr=*/subr
, explain_p
))
17499 /* Advance to the end of the list of parameters. */
17500 parms
= TREE_CHAIN (parms
);
17503 /* Fail if we've reached the end of the parm list, and more args
17504 are present, and the parm list isn't variadic. */
17505 if (ia
< nargs
&& parms
== void_list_node
)
17506 return unify_too_many_arguments (explain_p
, nargs
, ia
);
17507 /* Fail if parms are left and they don't have default values and
17508 they aren't all deduced as empty packs (c++/57397). This is
17509 consistent with sufficient_parms_p. */
17510 if (parms
&& parms
!= void_list_node
17511 && TREE_PURPOSE (parms
) == NULL_TREE
)
17513 unsigned int count
= nargs
;
17518 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
17521 p
= TREE_CHAIN (p
);
17523 while (p
&& p
!= void_list_node
);
17524 if (count
!= nargs
)
17525 return unify_too_few_arguments (explain_p
, ia
, count
,
17531 tsubst_flags_t complain
= (explain_p
17532 ? tf_warning_or_error
17535 for (i
= 0; i
< ntparms
; i
++)
17537 tree targ
= TREE_VEC_ELT (targs
, i
);
17538 tree tparm
= TREE_VEC_ELT (tparms
, i
);
17540 /* Clear the "incomplete" flags on all argument packs now so that
17541 substituting them into later default arguments works. */
17542 if (targ
&& ARGUMENT_PACK_P (targ
))
17544 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
17545 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
17548 if (targ
|| tparm
== error_mark_node
)
17550 tparm
= TREE_VALUE (tparm
);
17552 /* If this is an undeduced nontype parameter that depends on
17553 a type parameter, try another pass; its type may have been
17554 deduced from a later argument than the one from which
17555 this parameter can be deduced. */
17556 if (TREE_CODE (tparm
) == PARM_DECL
17557 && uses_template_parms (TREE_TYPE (tparm
))
17558 && !saw_undeduced
++)
17561 /* Core issue #226 (C++0x) [temp.deduct]:
17563 If a template argument has not been deduced, its
17564 default template argument, if any, is used.
17566 When we are in C++98 mode, TREE_PURPOSE will either
17567 be NULL_TREE or ERROR_MARK_NODE, so we do not need
17568 to explicitly check cxx_dialect here. */
17569 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
17571 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
17572 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
17573 reopen_deferring_access_checks (*checks
);
17574 location_t save_loc
= input_location
;
17576 input_location
= DECL_SOURCE_LOCATION (parm
);
17577 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
17578 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
17580 input_location
= save_loc
;
17581 *checks
= get_deferred_access_checks ();
17582 pop_deferring_access_checks ();
17583 if (arg
== error_mark_node
)
17587 TREE_VEC_ELT (targs
, i
) = arg
;
17588 /* The position of the first default template argument,
17589 is also the number of non-defaulted arguments in TARGS.
17591 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
17592 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
17597 /* If the type parameter is a parameter pack, then it will
17598 be deduced to an empty parameter pack. */
17599 if (template_parameter_pack_p (tparm
))
17603 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
17605 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
17606 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
17607 TREE_CONSTANT (arg
) = 1;
17610 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
17612 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
17614 TREE_VEC_ELT (targs
, i
) = arg
;
17618 return unify_parameter_deduction_failure (explain_p
, tparm
);
17621 #ifdef ENABLE_CHECKING
17622 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
17623 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
17626 return unify_success (explain_p
);
17629 /* Subroutine of type_unification_real. Args are like the variables
17630 at the call site. ARG is an overloaded function (or template-id);
17631 we try deducing template args from each of the overloads, and if
17632 only one succeeds, we go with that. Modifies TARGS and returns
17633 true on success. */
17636 resolve_overloaded_unification (tree tparms
,
17640 unification_kind_t strict
,
17644 tree tempargs
= copy_node (targs
);
17646 tree goodfn
= NULL_TREE
;
17649 if (TREE_CODE (arg
) == ADDR_EXPR
)
17651 arg
= TREE_OPERAND (arg
, 0);
17657 if (TREE_CODE (arg
) == COMPONENT_REF
)
17658 /* Handle `&x' where `x' is some static or non-static member
17660 arg
= TREE_OPERAND (arg
, 1);
17662 if (TREE_CODE (arg
) == OFFSET_REF
)
17663 arg
= TREE_OPERAND (arg
, 1);
17665 /* Strip baselink information. */
17666 if (BASELINK_P (arg
))
17667 arg
= BASELINK_FUNCTIONS (arg
);
17669 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
17671 /* If we got some explicit template args, we need to plug them into
17672 the affected templates before we try to unify, in case the
17673 explicit args will completely resolve the templates in question. */
17676 tree expl_subargs
= TREE_OPERAND (arg
, 1);
17677 arg
= TREE_OPERAND (arg
, 0);
17679 for (; arg
; arg
= OVL_NEXT (arg
))
17681 tree fn
= OVL_CURRENT (arg
);
17682 tree subargs
, elem
;
17684 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
17687 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
17688 expl_subargs
, NULL_TREE
, tf_none
,
17689 /*require_all_args=*/true,
17690 /*use_default_args=*/true);
17691 if (subargs
!= error_mark_node
17692 && !any_dependent_template_arguments_p (subargs
))
17694 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
17695 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
17696 elem
, strict
, sub_strict
, addr_p
, explain_p
)
17697 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
17706 /* If no templates (or more than one) are fully resolved by the
17707 explicit arguments, this template-id is a non-deduced context; it
17708 could still be OK if we deduce all template arguments for the
17709 enclosing call through other arguments. */
17713 else if (TREE_CODE (arg
) != OVERLOAD
17714 && TREE_CODE (arg
) != FUNCTION_DECL
)
17715 /* If ARG is, for example, "(0, &f)" then its type will be unknown
17716 -- but the deduction does not succeed because the expression is
17717 not just the function on its own. */
17720 for (; arg
; arg
= OVL_NEXT (arg
))
17721 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
17722 TREE_TYPE (OVL_CURRENT (arg
)),
17723 strict
, sub_strict
, addr_p
, explain_p
)
17724 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
17726 goodfn
= OVL_CURRENT (arg
);
17730 /* [temp.deduct.type] A template-argument can be deduced from a pointer
17731 to function or pointer to member function argument if the set of
17732 overloaded functions does not contain function templates and at most
17733 one of a set of overloaded functions provides a unique match.
17735 So if we found multiple possibilities, we return success but don't
17736 deduce anything. */
17740 int i
= TREE_VEC_LENGTH (targs
);
17742 if (TREE_VEC_ELT (tempargs
, i
))
17744 tree old
= TREE_VEC_ELT (targs
, i
);
17745 tree new_
= TREE_VEC_ELT (tempargs
, i
);
17746 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
17747 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
17748 /* Don't forget explicit template arguments in a pack. */
17749 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
17750 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
17751 TREE_VEC_ELT (targs
, i
) = new_
;
17760 /* Core DR 115: In contexts where deduction is done and fails, or in
17761 contexts where deduction is not done, if a template argument list is
17762 specified and it, along with any default template arguments, identifies
17763 a single function template specialization, then the template-id is an
17764 lvalue for the function template specialization. */
17767 resolve_nondeduced_context (tree orig_expr
)
17769 tree expr
, offset
, baselink
;
17772 if (!type_unknown_p (orig_expr
))
17777 offset
= NULL_TREE
;
17778 baselink
= NULL_TREE
;
17780 if (TREE_CODE (expr
) == ADDR_EXPR
)
17782 expr
= TREE_OPERAND (expr
, 0);
17785 if (TREE_CODE (expr
) == OFFSET_REF
)
17788 expr
= TREE_OPERAND (expr
, 1);
17790 if (BASELINK_P (expr
))
17793 expr
= BASELINK_FUNCTIONS (expr
);
17796 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
17799 tree goodfn
= NULL_TREE
;
17801 /* If we got some explicit template args, we need to plug them into
17802 the affected templates before we try to unify, in case the
17803 explicit args will completely resolve the templates in question. */
17805 tree expl_subargs
= TREE_OPERAND (expr
, 1);
17806 tree arg
= TREE_OPERAND (expr
, 0);
17807 tree badfn
= NULL_TREE
;
17808 tree badargs
= NULL_TREE
;
17810 for (; arg
; arg
= OVL_NEXT (arg
))
17812 tree fn
= OVL_CURRENT (arg
);
17813 tree subargs
, elem
;
17815 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
17818 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
17819 expl_subargs
, NULL_TREE
, tf_none
,
17820 /*require_all_args=*/true,
17821 /*use_default_args=*/true);
17822 if (subargs
!= error_mark_node
17823 && !any_dependent_template_arguments_p (subargs
))
17825 elem
= instantiate_template (fn
, subargs
, tf_none
);
17826 if (elem
== error_mark_node
)
17831 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
17840 mark_used (goodfn
);
17843 expr
= build_baselink (BASELINK_BINFO (baselink
),
17844 BASELINK_ACCESS_BINFO (baselink
),
17845 expr
, BASELINK_OPTYPE (baselink
));
17849 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
17850 expr
= build_offset_ref (base
, expr
, addr
, tf_warning_or_error
);
17853 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
17856 else if (good
== 0 && badargs
)
17857 /* There were no good options and at least one bad one, so let the
17858 user know what the problem is. */
17859 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
17864 /* Subroutine of resolve_overloaded_unification; does deduction for a single
17865 overload. Fills TARGS with any deduced arguments, or error_mark_node if
17866 different overloads deduce different arguments for a given parm.
17867 ADDR_P is true if the expression for which deduction is being
17868 performed was of the form "& fn" rather than simply "fn".
17870 Returns 1 on success. */
17873 try_one_overload (tree tparms
,
17878 unification_kind_t strict
,
17887 if (arg
== error_mark_node
)
17890 /* [temp.deduct.type] A template-argument can be deduced from a pointer
17891 to function or pointer to member function argument if the set of
17892 overloaded functions does not contain function templates and at most
17893 one of a set of overloaded functions provides a unique match.
17895 So if this is a template, just return success. */
17897 if (uses_template_parms (arg
))
17900 if (TREE_CODE (arg
) == METHOD_TYPE
)
17901 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
17903 arg
= build_pointer_type (arg
);
17905 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
17907 /* We don't copy orig_targs for this because if we have already deduced
17908 some template args from previous args, unify would complain when we
17909 try to deduce a template parameter for the same argument, even though
17910 there isn't really a conflict. */
17911 nargs
= TREE_VEC_LENGTH (targs
);
17912 tempargs
= make_tree_vec (nargs
);
17914 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
17917 /* First make sure we didn't deduce anything that conflicts with
17918 explicitly specified args. */
17919 for (i
= nargs
; i
--; )
17921 tree elt
= TREE_VEC_ELT (tempargs
, i
);
17922 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
17926 else if (uses_template_parms (elt
))
17927 /* Since we're unifying against ourselves, we will fill in
17928 template args used in the function parm list with our own
17929 template parms. Discard them. */
17930 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
17931 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
17935 for (i
= nargs
; i
--; )
17937 tree elt
= TREE_VEC_ELT (tempargs
, i
);
17940 TREE_VEC_ELT (targs
, i
) = elt
;
17946 /* PARM is a template class (perhaps with unbound template
17947 parameters). ARG is a fully instantiated type. If ARG can be
17948 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
17949 TARGS are as for unify. */
17952 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
17955 tree copy_of_targs
;
17957 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
17958 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
17959 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
17962 /* We need to make a new template argument vector for the call to
17963 unify. If we used TARGS, we'd clutter it up with the result of
17964 the attempted unification, even if this class didn't work out.
17965 We also don't want to commit ourselves to all the unifications
17966 we've already done, since unification is supposed to be done on
17967 an argument-by-argument basis. In other words, consider the
17968 following pathological case:
17970 template <int I, int J, int K>
17973 template <int I, int J>
17974 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
17976 template <int I, int J, int K>
17977 void f(S<I, J, K>, S<I, I, I>);
17986 Now, by the time we consider the unification involving `s2', we
17987 already know that we must have `f<0, 0, 0>'. But, even though
17988 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
17989 because there are two ways to unify base classes of S<0, 1, 2>
17990 with S<I, I, I>. If we kept the already deduced knowledge, we
17991 would reject the possibility I=1. */
17992 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
17994 /* If unification failed, we're done. */
17995 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
17996 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
18002 /* Given a template type PARM and a class type ARG, find the unique
18003 base type in ARG that is an instance of PARM. We do not examine
18004 ARG itself; only its base-classes. If there is not exactly one
18005 appropriate base class, return NULL_TREE. PARM may be the type of
18006 a partial specialization, as well as a plain template type. Used
18009 static enum template_base_result
18010 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
18011 bool explain_p
, tree
*result
)
18013 tree rval
= NULL_TREE
;
18016 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
18018 binfo
= TYPE_BINFO (complete_type (arg
));
18021 /* The type could not be completed. */
18022 *result
= NULL_TREE
;
18023 return tbr_incomplete_type
;
18026 /* Walk in inheritance graph order. The search order is not
18027 important, and this avoids multiple walks of virtual bases. */
18028 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
18030 tree r
= try_class_unification (tparms
, targs
, parm
,
18031 BINFO_TYPE (binfo
), explain_p
);
18035 /* If there is more than one satisfactory baseclass, then:
18039 If they yield more than one possible deduced A, the type
18043 if (rval
&& !same_type_p (r
, rval
))
18045 *result
= NULL_TREE
;
18046 return tbr_ambiguous_baseclass
;
18054 return tbr_success
;
18057 /* Returns the level of DECL, which declares a template parameter. */
18060 template_decl_level (tree decl
)
18062 switch (TREE_CODE (decl
))
18065 case TEMPLATE_DECL
:
18066 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
18069 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
18072 gcc_unreachable ();
18077 /* Decide whether ARG can be unified with PARM, considering only the
18078 cv-qualifiers of each type, given STRICT as documented for unify.
18079 Returns nonzero iff the unification is OK on that basis. */
18082 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
18084 int arg_quals
= cp_type_quals (arg
);
18085 int parm_quals
= cp_type_quals (parm
);
18087 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
18088 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
18090 /* Although a CVR qualifier is ignored when being applied to a
18091 substituted template parameter ([8.3.2]/1 for example), that
18092 does not allow us to unify "const T" with "int&" because both
18093 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
18094 It is ok when we're allowing additional CV qualifiers
18095 at the outer level [14.8.2.1]/3,1st bullet. */
18096 if ((TREE_CODE (arg
) == REFERENCE_TYPE
18097 || TREE_CODE (arg
) == FUNCTION_TYPE
18098 || TREE_CODE (arg
) == METHOD_TYPE
)
18099 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
18102 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
18103 && (parm_quals
& TYPE_QUAL_RESTRICT
))
18107 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
18108 && (arg_quals
& parm_quals
) != parm_quals
)
18111 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
18112 && (parm_quals
& arg_quals
) != arg_quals
)
18118 /* Determines the LEVEL and INDEX for the template parameter PARM. */
18120 template_parm_level_and_index (tree parm
, int* level
, int* index
)
18122 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
18123 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
18124 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
18126 *index
= TEMPLATE_TYPE_IDX (parm
);
18127 *level
= TEMPLATE_TYPE_LEVEL (parm
);
18131 *index
= TEMPLATE_PARM_IDX (parm
);
18132 *level
= TEMPLATE_PARM_LEVEL (parm
);
18136 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
18138 if (unify (TP, TA, P, A, S, EP)) \
18142 /* Unifies the remaining arguments in PACKED_ARGS with the pack
18143 expansion at the end of PACKED_PARMS. Returns 0 if the type
18144 deduction succeeds, 1 otherwise. STRICT is the same as in
18145 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
18146 call argument list. We'll need to adjust the arguments to make them
18147 types. SUBR tells us if this is from a recursive call to
18148 type_unification_real, or for comparing two template argument
18152 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
18153 tree packed_args
, unification_kind_t strict
,
18154 bool subr
, bool explain_p
)
18157 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
18158 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
18159 tree pack
, packs
= NULL_TREE
;
18160 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
18162 packed_args
= expand_template_argument_pack (packed_args
);
18164 int len
= TREE_VEC_LENGTH (packed_args
);
18166 /* Determine the parameter packs we will be deducing from the
18167 pattern, and record their current deductions. */
18168 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
18169 pack
; pack
= TREE_CHAIN (pack
))
18171 tree parm_pack
= TREE_VALUE (pack
);
18174 /* Determine the index and level of this parameter pack. */
18175 template_parm_level_and_index (parm_pack
, &level
, &idx
);
18177 /* Keep track of the parameter packs and their corresponding
18179 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
18180 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
18183 /* Loop through all of the arguments that have not yet been
18184 unified and unify each with the pattern. */
18185 for (i
= start
; i
< len
; i
++)
18188 bool any_explicit
= false;
18189 tree arg
= TREE_VEC_ELT (packed_args
, i
);
18191 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
18192 or the element of its argument pack at the current index if
18193 this argument was explicitly specified. */
18194 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
18198 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
18201 if (TREE_VALUE (pack
)
18202 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
18203 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
18205 any_explicit
= true;
18206 arg
= TREE_VEC_ELT (pargs
, i
- start
);
18208 TMPL_ARG (targs
, level
, idx
) = arg
;
18211 /* If we had explicit template arguments, substitute them into the
18212 pattern before deduction. */
18215 /* Some arguments might still be unspecified or dependent. */
18217 ++processing_template_decl
;
18218 dependent
= any_dependent_template_arguments_p (targs
);
18220 --processing_template_decl
;
18221 parm
= tsubst (pattern
, targs
,
18222 explain_p
? tf_warning_or_error
: tf_none
,
18225 --processing_template_decl
;
18226 if (parm
== error_mark_node
)
18232 /* Unify the pattern with the current argument. */
18233 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
18234 LOOKUP_IMPLICIT
, explain_p
))
18237 /* For each parameter pack, collect the deduced value. */
18238 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
18241 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
18243 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
18244 TMPL_ARG (targs
, level
, idx
);
18248 /* Verify that the results of unification with the parameter packs
18249 produce results consistent with what we've seen before, and make
18250 the deduced argument packs available. */
18251 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
18253 tree old_pack
= TREE_VALUE (pack
);
18254 tree new_args
= TREE_TYPE (pack
);
18255 int i
, len
= TREE_VEC_LENGTH (new_args
);
18257 bool nondeduced_p
= false;
18259 /* By default keep the original deduced argument pack.
18260 If necessary, more specific code is going to update the
18261 resulting deduced argument later down in this function. */
18262 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
18263 TMPL_ARG (targs
, level
, idx
) = old_pack
;
18265 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
18266 actually deduce anything. */
18267 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
18268 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
18269 nondeduced_p
= true;
18273 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
18275 /* If we had fewer function args than explicit template args,
18276 just use the explicits. */
18277 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
18278 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
18279 if (len
< explicit_len
)
18280 new_args
= explicit_args
;
18286 /* Build the deduced *_ARGUMENT_PACK. */
18287 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
18289 result
= make_node (NONTYPE_ARGUMENT_PACK
);
18290 TREE_TYPE (result
) =
18291 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
18292 TREE_CONSTANT (result
) = 1;
18295 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
18297 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
18299 /* Note the deduced argument packs for this parameter
18301 TMPL_ARG (targs
, level
, idx
) = result
;
18303 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
18304 && (ARGUMENT_PACK_ARGS (old_pack
)
18305 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
18307 /* We only had the explicitly-provided arguments before, but
18308 now we have a complete set of arguments. */
18309 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
18311 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
18312 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
18313 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
18317 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
18318 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
18320 if (!comp_template_args_with_info (old_args
, new_args
,
18321 &bad_old_arg
, &bad_new_arg
))
18322 /* Inconsistent unification of this parameter pack. */
18323 return unify_parameter_pack_inconsistent (explain_p
,
18329 return unify_success (explain_p
);
18332 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
18333 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
18334 parameters and return value are as for unify. */
18337 unify_array_domain (tree tparms
, tree targs
,
18338 tree parm_dom
, tree arg_dom
,
18346 /* Our representation of array types uses "N - 1" as the
18347 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
18348 not an integer constant. We cannot unify arbitrarily
18349 complex expressions, so we eliminate the MINUS_EXPRs
18351 parm_max
= TYPE_MAX_VALUE (parm_dom
);
18352 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
18355 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
18356 parm_max
= TREE_OPERAND (parm_max
, 0);
18358 arg_max
= TYPE_MAX_VALUE (arg_dom
);
18359 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
18362 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
18363 trying to unify the type of a variable with the type
18364 of a template parameter. For example:
18366 template <unsigned int N>
18367 void f (char (&) [N]);
18374 Here, the type of the ARG will be "int [g(i)]", and
18375 may be a SAVE_EXPR, etc. */
18376 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
18377 return unify_vla_arg (explain_p
, arg_dom
);
18378 arg_max
= TREE_OPERAND (arg_max
, 0);
18381 /* If only one of the bounds used a MINUS_EXPR, compensate
18382 by adding one to the other bound. */
18383 if (parm_cst
&& !arg_cst
)
18384 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
18388 else if (arg_cst
&& !parm_cst
)
18389 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
18394 return unify (tparms
, targs
, parm_max
, arg_max
,
18395 UNIFY_ALLOW_INTEGER
, explain_p
);
18398 /* Deduce the value of template parameters. TPARMS is the (innermost)
18399 set of template parameters to a template. TARGS is the bindings
18400 for those template parameters, as determined thus far; TARGS may
18401 include template arguments for outer levels of template parameters
18402 as well. PARM is a parameter to a template function, or a
18403 subcomponent of that parameter; ARG is the corresponding argument.
18404 This function attempts to match PARM with ARG in a manner
18405 consistent with the existing assignments in TARGS. If more values
18406 are deduced, then TARGS is updated.
18408 Returns 0 if the type deduction succeeds, 1 otherwise. The
18409 parameter STRICT is a bitwise or of the following flags:
18412 Require an exact match between PARM and ARG.
18413 UNIFY_ALLOW_MORE_CV_QUAL:
18414 Allow the deduced ARG to be more cv-qualified (by qualification
18415 conversion) than ARG.
18416 UNIFY_ALLOW_LESS_CV_QUAL:
18417 Allow the deduced ARG to be less cv-qualified than ARG.
18418 UNIFY_ALLOW_DERIVED:
18419 Allow the deduced ARG to be a template base class of ARG,
18420 or a pointer to a template base class of the type pointed to by
18422 UNIFY_ALLOW_INTEGER:
18423 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
18424 case for more information.
18425 UNIFY_ALLOW_OUTER_LEVEL:
18426 This is the outermost level of a deduction. Used to determine validity
18427 of qualification conversions. A valid qualification conversion must
18428 have const qualified pointers leading up to the inner type which
18429 requires additional CV quals, except at the outer level, where const
18430 is not required [conv.qual]. It would be normal to set this flag in
18431 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
18432 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
18433 This is the outermost level of a deduction, and PARM can be more CV
18434 qualified at this point.
18435 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
18436 This is the outermost level of a deduction, and PARM can be less CV
18437 qualified at this point. */
18440 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
18446 int strict_in
= strict
;
18448 /* I don't think this will do the right thing with respect to types.
18449 But the only case I've seen it in so far has been array bounds, where
18450 signedness is the only information lost, and I think that will be
18452 while (TREE_CODE (parm
) == NOP_EXPR
)
18453 parm
= TREE_OPERAND (parm
, 0);
18455 if (arg
== error_mark_node
)
18456 return unify_invalid (explain_p
);
18457 if (arg
== unknown_type_node
18458 || arg
== init_list_type_node
)
18459 /* We can't deduce anything from this, but we might get all the
18460 template args from other function args. */
18461 return unify_success (explain_p
);
18463 /* If PARM uses template parameters, then we can't bail out here,
18464 even if ARG == PARM, since we won't record unifications for the
18465 template parameters. We might need them if we're trying to
18466 figure out which of two things is more specialized. */
18467 if (arg
== parm
&& !uses_template_parms (parm
))
18468 return unify_success (explain_p
);
18470 /* Handle init lists early, so the rest of the function can assume
18471 we're dealing with a type. */
18472 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
18476 tree orig_parm
= parm
;
18478 /* Replace T with std::initializer_list<T> for deduction. */
18479 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
18480 && flag_deduce_init_list
)
18481 parm
= listify (parm
);
18483 if (!is_std_init_list (parm
)
18484 && TREE_CODE (parm
) != ARRAY_TYPE
)
18485 /* We can only deduce from an initializer list argument if the
18486 parameter is std::initializer_list or an array; otherwise this
18487 is a non-deduced context. */
18488 return unify_success (explain_p
);
18490 if (TREE_CODE (parm
) == ARRAY_TYPE
)
18491 elttype
= TREE_TYPE (parm
);
18494 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
18495 /* Deduction is defined in terms of a single type, so just punt
18496 on the (bizarre) std::initializer_list<T...>. */
18497 if (PACK_EXPANSION_P (elttype
))
18498 return unify_success (explain_p
);
18501 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
18503 int elt_strict
= strict
;
18505 if (elt
== error_mark_node
)
18506 return unify_invalid (explain_p
);
18508 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
18510 tree type
= TREE_TYPE (elt
);
18511 if (type
== error_mark_node
)
18512 return unify_invalid (explain_p
);
18513 /* It should only be possible to get here for a call. */
18514 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
18515 elt_strict
|= maybe_adjust_types_for_deduction
18516 (DEDUCE_CALL
, &elttype
, &type
, elt
);
18520 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
18524 if (TREE_CODE (parm
) == ARRAY_TYPE
18525 && deducible_array_bound (TYPE_DOMAIN (parm
)))
18527 /* Also deduce from the length of the initializer list. */
18528 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
18529 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
18530 if (idx
== error_mark_node
)
18531 return unify_invalid (explain_p
);
18532 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
18536 /* If the std::initializer_list<T> deduction worked, replace the
18537 deduced A with std::initializer_list<A>. */
18538 if (orig_parm
!= parm
)
18540 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
18541 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
18542 targ
= listify (targ
);
18543 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
18545 return unify_success (explain_p
);
18548 /* Immediately reject some pairs that won't unify because of
18549 cv-qualification mismatches. */
18550 if (TREE_CODE (arg
) == TREE_CODE (parm
)
18552 /* It is the elements of the array which hold the cv quals of an array
18553 type, and the elements might be template type parms. We'll check
18554 when we recurse. */
18555 && TREE_CODE (arg
) != ARRAY_TYPE
18556 /* We check the cv-qualifiers when unifying with template type
18557 parameters below. We want to allow ARG `const T' to unify with
18558 PARM `T' for example, when computing which of two templates
18559 is more specialized, for example. */
18560 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
18561 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
18562 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
18564 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
18565 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
18566 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
18567 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
18568 strict
&= ~UNIFY_ALLOW_DERIVED
;
18569 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
18570 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
18572 switch (TREE_CODE (parm
))
18574 case TYPENAME_TYPE
:
18576 case UNBOUND_CLASS_TEMPLATE
:
18577 /* In a type which contains a nested-name-specifier, template
18578 argument values cannot be deduced for template parameters used
18579 within the nested-name-specifier. */
18580 return unify_success (explain_p
);
18582 case TEMPLATE_TYPE_PARM
:
18583 case TEMPLATE_TEMPLATE_PARM
:
18584 case BOUND_TEMPLATE_TEMPLATE_PARM
:
18585 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
18586 if (error_operand_p (tparm
))
18587 return unify_invalid (explain_p
);
18589 if (TEMPLATE_TYPE_LEVEL (parm
)
18590 != template_decl_level (tparm
))
18591 /* The PARM is not one we're trying to unify. Just check
18592 to see if it matches ARG. */
18594 if (TREE_CODE (arg
) == TREE_CODE (parm
)
18595 && (is_auto (parm
) ? is_auto (arg
)
18596 : same_type_p (parm
, arg
)))
18597 return unify_success (explain_p
);
18599 return unify_type_mismatch (explain_p
, parm
, arg
);
18601 idx
= TEMPLATE_TYPE_IDX (parm
);
18602 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
18603 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
18604 if (error_operand_p (tparm
))
18605 return unify_invalid (explain_p
);
18607 /* Check for mixed types and values. */
18608 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
18609 && TREE_CODE (tparm
) != TYPE_DECL
)
18610 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
18611 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
18612 gcc_unreachable ();
18614 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
18616 /* ARG must be constructed from a template class or a template
18617 template parameter. */
18618 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
18619 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
18620 return unify_template_deduction_failure (explain_p
, parm
, arg
);
18622 tree parmvec
= TYPE_TI_ARGS (parm
);
18623 /* An alias template name is never deduced. */
18624 if (TYPE_ALIAS_P (arg
))
18625 arg
= strip_typedefs (arg
);
18626 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
18627 tree full_argvec
= add_to_template_args (targs
, argvec
);
18629 = DECL_INNERMOST_TEMPLATE_PARMS
18630 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
18632 int parm_variadic_p
= 0;
18634 /* The resolution to DR150 makes clear that default
18635 arguments for an N-argument may not be used to bind T
18636 to a template template parameter with fewer than N
18637 parameters. It is not safe to permit the binding of
18638 default arguments as an extension, as that may change
18639 the meaning of a conforming program. Consider:
18641 struct Dense { static const unsigned int dim = 1; };
18643 template <template <typename> class View,
18645 void operator+(float, View<Block> const&);
18647 template <typename Block,
18648 unsigned int Dim = Block::dim>
18649 struct Lvalue_proxy { operator float() const; };
18653 Lvalue_proxy<Dense> p;
18658 Here, if Lvalue_proxy is permitted to bind to View, then
18659 the global operator+ will be used; if they are not, the
18660 Lvalue_proxy will be converted to float. */
18661 if (coerce_template_parms (parm_parms
,
18663 TYPE_TI_TEMPLATE (parm
),
18665 ? tf_warning_or_error
18667 /*require_all_args=*/true,
18668 /*use_default_args=*/false)
18669 == error_mark_node
)
18672 /* Deduce arguments T, i from TT<T> or TT<i>.
18673 We check each element of PARMVEC and ARGVEC individually
18674 rather than the whole TREE_VEC since they can have
18675 different number of elements. */
18677 parmvec
= expand_template_argument_pack (parmvec
);
18678 argvec
= expand_template_argument_pack (argvec
);
18680 len
= TREE_VEC_LENGTH (parmvec
);
18682 /* Check if the parameters end in a pack, making them
18685 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
18686 parm_variadic_p
= 1;
18688 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
18689 /* If the template argument list of P contains a pack
18690 expansion that is not the last template argument, the
18691 entire template argument list is a non-deduced
18693 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
18694 return unify_success (explain_p
);
18696 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
18697 return unify_too_few_arguments (explain_p
,
18698 TREE_VEC_LENGTH (argvec
), len
);
18700 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
18702 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
18703 TREE_VEC_ELT (parmvec
, i
),
18704 TREE_VEC_ELT (argvec
, i
),
18705 UNIFY_ALLOW_NONE
, explain_p
);
18708 if (parm_variadic_p
18709 && unify_pack_expansion (tparms
, targs
,
18712 /*subr=*/true, explain_p
))
18715 arg
= TYPE_TI_TEMPLATE (arg
);
18717 /* Fall through to deduce template name. */
18720 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
18721 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
18723 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
18725 /* Simple cases: Value already set, does match or doesn't. */
18726 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
18727 return unify_success (explain_p
);
18729 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
18733 /* If PARM is `const T' and ARG is only `int', we don't have
18734 a match unless we are allowing additional qualification.
18735 If ARG is `const int' and PARM is just `T' that's OK;
18736 that binds `const int' to `T'. */
18737 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
18739 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
18741 /* Consider the case where ARG is `const volatile int' and
18742 PARM is `const T'. Then, T should be `volatile int'. */
18743 arg
= cp_build_qualified_type_real
18744 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
18745 if (arg
== error_mark_node
)
18746 return unify_invalid (explain_p
);
18748 /* Simple cases: Value already set, does match or doesn't. */
18749 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
18750 return unify_success (explain_p
);
18752 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
18754 /* Make sure that ARG is not a variable-sized array. (Note
18755 that were talking about variable-sized arrays (like
18756 `int[n]'), rather than arrays of unknown size (like
18757 `int[]').) We'll get very confused by such a type since
18758 the bound of the array is not constant, and therefore
18759 not mangleable. Besides, such types are not allowed in
18760 ISO C++, so we can do as we please here. We do allow
18761 them for 'auto' deduction, since that isn't ABI-exposed. */
18762 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
18763 return unify_vla_arg (explain_p
, arg
);
18765 /* Strip typedefs as in convert_template_argument. */
18766 arg
= canonicalize_type_argument (arg
, tf_none
);
18769 /* If ARG is a parameter pack or an expansion, we cannot unify
18770 against it unless PARM is also a parameter pack. */
18771 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
18772 && !template_parameter_pack_p (parm
))
18773 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
18775 /* If the argument deduction results is a METHOD_TYPE,
18776 then there is a problem.
18777 METHOD_TYPE doesn't map to any real C++ type the result of
18778 the deduction can not be of that type. */
18779 if (TREE_CODE (arg
) == METHOD_TYPE
)
18780 return unify_method_type_error (explain_p
, arg
);
18782 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
18783 return unify_success (explain_p
);
18785 case TEMPLATE_PARM_INDEX
:
18786 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
18787 if (error_operand_p (tparm
))
18788 return unify_invalid (explain_p
);
18790 if (TEMPLATE_PARM_LEVEL (parm
)
18791 != template_decl_level (tparm
))
18793 /* The PARM is not one we're trying to unify. Just check
18794 to see if it matches ARG. */
18795 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
18796 && cp_tree_equal (parm
, arg
));
18798 unify_expression_unequal (explain_p
, parm
, arg
);
18802 idx
= TEMPLATE_PARM_IDX (parm
);
18803 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
18807 int x
= !cp_tree_equal (targ
, arg
);
18809 unify_inconsistency (explain_p
, parm
, targ
, arg
);
18813 /* [temp.deduct.type] If, in the declaration of a function template
18814 with a non-type template-parameter, the non-type
18815 template-parameter is used in an expression in the function
18816 parameter-list and, if the corresponding template-argument is
18817 deduced, the template-argument type shall match the type of the
18818 template-parameter exactly, except that a template-argument
18819 deduced from an array bound may be of any integral type.
18820 The non-type parameter might use already deduced type parameters. */
18821 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
18822 if (!TREE_TYPE (arg
))
18823 /* Template-parameter dependent expression. Just accept it for now.
18824 It will later be processed in convert_template_argument. */
18826 else if (same_type_p (TREE_TYPE (arg
), tparm
))
18828 else if ((strict
& UNIFY_ALLOW_INTEGER
)
18829 && CP_INTEGRAL_TYPE_P (tparm
))
18830 /* Convert the ARG to the type of PARM; the deduced non-type
18831 template argument must exactly match the types of the
18832 corresponding parameter. */
18833 arg
= fold (build_nop (tparm
, arg
));
18834 else if (uses_template_parms (tparm
))
18835 /* We haven't deduced the type of this parameter yet. Try again
18837 return unify_success (explain_p
);
18839 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
18841 /* If ARG is a parameter pack or an expansion, we cannot unify
18842 against it unless PARM is also a parameter pack. */
18843 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
18844 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
18845 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
18847 arg
= strip_typedefs_expr (arg
);
18848 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
18849 return unify_success (explain_p
);
18853 /* A pointer-to-member constant can be unified only with
18854 another constant. */
18855 if (TREE_CODE (arg
) != PTRMEM_CST
)
18856 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
18858 /* Just unify the class member. It would be useless (and possibly
18859 wrong, depending on the strict flags) to unify also
18860 PTRMEM_CST_CLASS, because we want to be sure that both parm and
18861 arg refer to the same variable, even if through different
18862 classes. For instance:
18864 struct A { int x; };
18867 Unification of &A::x and &B::x must succeed. */
18868 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
18869 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
18874 if (!TYPE_PTR_P (arg
))
18875 return unify_type_mismatch (explain_p
, parm
, arg
);
18877 /* [temp.deduct.call]
18879 A can be another pointer or pointer to member type that can
18880 be converted to the deduced A via a qualification
18881 conversion (_conv.qual_).
18883 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
18884 This will allow for additional cv-qualification of the
18885 pointed-to types if appropriate. */
18887 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
18888 /* The derived-to-base conversion only persists through one
18889 level of pointers. */
18890 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
18892 return unify (tparms
, targs
, TREE_TYPE (parm
),
18893 TREE_TYPE (arg
), strict
, explain_p
);
18896 case REFERENCE_TYPE
:
18897 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
18898 return unify_type_mismatch (explain_p
, parm
, arg
);
18899 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
18900 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
18903 if (TREE_CODE (arg
) != ARRAY_TYPE
)
18904 return unify_type_mismatch (explain_p
, parm
, arg
);
18905 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
18906 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
18907 return unify_type_mismatch (explain_p
, parm
, arg
);
18908 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
18909 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
18910 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
18911 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
18912 TYPE_DOMAIN (arg
), explain_p
);
18913 return unify_success (explain_p
);
18920 case ENUMERAL_TYPE
:
18923 if (TREE_CODE (arg
) != TREE_CODE (parm
))
18924 return unify_type_mismatch (explain_p
, parm
, arg
);
18926 /* We have already checked cv-qualification at the top of the
18928 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
18929 return unify_type_mismatch (explain_p
, parm
, arg
);
18931 /* As far as unification is concerned, this wins. Later checks
18932 will invalidate it if necessary. */
18933 return unify_success (explain_p
);
18935 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
18936 /* Type INTEGER_CST can come from ordinary constant template args. */
18938 while (TREE_CODE (arg
) == NOP_EXPR
)
18939 arg
= TREE_OPERAND (arg
, 0);
18941 if (TREE_CODE (arg
) != INTEGER_CST
)
18942 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
18943 return (tree_int_cst_equal (parm
, arg
)
18944 ? unify_success (explain_p
)
18945 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
18949 int i
, len
, argslen
;
18950 int parm_variadic_p
= 0;
18952 if (TREE_CODE (arg
) != TREE_VEC
)
18953 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
18955 len
= TREE_VEC_LENGTH (parm
);
18956 argslen
= TREE_VEC_LENGTH (arg
);
18958 /* Check for pack expansions in the parameters. */
18959 for (i
= 0; i
< len
; ++i
)
18961 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
18964 /* We can unify against something with a trailing
18966 parm_variadic_p
= 1;
18968 /* [temp.deduct.type]/9: If the template argument list of
18969 P contains a pack expansion that is not the last
18970 template argument, the entire template argument list
18971 is a non-deduced context. */
18972 return unify_success (explain_p
);
18976 /* If we don't have enough arguments to satisfy the parameters
18977 (not counting the pack expression at the end), or we have
18978 too many arguments for a parameter list that doesn't end in
18979 a pack expression, we can't unify. */
18980 if (parm_variadic_p
18981 ? argslen
< len
- parm_variadic_p
18983 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
18985 /* Unify all of the parameters that precede the (optional)
18986 pack expression. */
18987 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
18989 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
18990 TREE_VEC_ELT (parm
, i
),
18991 TREE_VEC_ELT (arg
, i
),
18992 UNIFY_ALLOW_NONE
, explain_p
);
18994 if (parm_variadic_p
)
18995 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
18997 /*subr=*/true, explain_p
);
18998 return unify_success (explain_p
);
19003 if (TREE_CODE (arg
) != TREE_CODE (parm
))
19004 return unify_type_mismatch (explain_p
, parm
, arg
);
19006 if (TYPE_PTRMEMFUNC_P (parm
))
19008 if (!TYPE_PTRMEMFUNC_P (arg
))
19009 return unify_type_mismatch (explain_p
, parm
, arg
);
19011 return unify (tparms
, targs
,
19012 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
19013 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
19014 strict
, explain_p
);
19016 else if (TYPE_PTRMEMFUNC_P (arg
))
19017 return unify_type_mismatch (explain_p
, parm
, arg
);
19019 if (CLASSTYPE_TEMPLATE_INFO (parm
))
19021 tree t
= NULL_TREE
;
19023 if (strict_in
& UNIFY_ALLOW_DERIVED
)
19025 /* First, we try to unify the PARM and ARG directly. */
19026 t
= try_class_unification (tparms
, targs
,
19027 parm
, arg
, explain_p
);
19031 /* Fallback to the special case allowed in
19032 [temp.deduct.call]:
19034 If P is a class, and P has the form
19035 template-id, then A can be a derived class of
19036 the deduced A. Likewise, if P is a pointer to
19037 a class of the form template-id, A can be a
19038 pointer to a derived class pointed to by the
19040 enum template_base_result r
;
19041 r
= get_template_base (tparms
, targs
, parm
, arg
,
19045 return unify_no_common_base (explain_p
, r
, parm
, arg
);
19048 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
19049 && (CLASSTYPE_TI_TEMPLATE (parm
)
19050 == CLASSTYPE_TI_TEMPLATE (arg
)))
19051 /* Perhaps PARM is something like S<U> and ARG is S<int>.
19052 Then, we should unify `int' and `U'. */
19055 /* There's no chance of unification succeeding. */
19056 return unify_type_mismatch (explain_p
, parm
, arg
);
19058 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
19059 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
19061 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
19062 return unify_type_mismatch (explain_p
, parm
, arg
);
19063 return unify_success (explain_p
);
19066 case FUNCTION_TYPE
:
19068 unsigned int nargs
;
19073 if (TREE_CODE (arg
) != TREE_CODE (parm
))
19074 return unify_type_mismatch (explain_p
, parm
, arg
);
19076 /* CV qualifications for methods can never be deduced, they must
19077 match exactly. We need to check them explicitly here,
19078 because type_unification_real treats them as any other
19079 cv-qualified parameter. */
19080 if (TREE_CODE (parm
) == METHOD_TYPE
19081 && (!check_cv_quals_for_unify
19083 class_of_this_parm (arg
),
19084 class_of_this_parm (parm
))))
19085 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
19087 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
19088 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
19090 nargs
= list_length (TYPE_ARG_TYPES (arg
));
19091 args
= XALLOCAVEC (tree
, nargs
);
19092 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
19093 a
!= NULL_TREE
&& a
!= void_list_node
;
19094 a
= TREE_CHAIN (a
), ++i
)
19095 args
[i
] = TREE_VALUE (a
);
19098 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
19099 args
, nargs
, 1, DEDUCE_EXACT
,
19100 LOOKUP_NORMAL
, NULL
, explain_p
);
19104 /* Unify a pointer to member with a pointer to member function, which
19105 deduces the type of the member as a function type. */
19106 if (TYPE_PTRMEMFUNC_P (arg
))
19108 /* Check top-level cv qualifiers */
19109 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
19110 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
19112 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
19113 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
19114 UNIFY_ALLOW_NONE
, explain_p
);
19116 /* Determine the type of the function we are unifying against. */
19117 tree fntype
= static_fn_type (arg
);
19119 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
19122 if (TREE_CODE (arg
) != OFFSET_TYPE
)
19123 return unify_type_mismatch (explain_p
, parm
, arg
);
19124 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
19125 TYPE_OFFSET_BASETYPE (arg
),
19126 UNIFY_ALLOW_NONE
, explain_p
);
19127 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
19128 strict
, explain_p
);
19131 if (DECL_TEMPLATE_PARM_P (parm
))
19132 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
19133 if (arg
!= scalar_constant_value (parm
))
19134 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19135 return unify_success (explain_p
);
19138 case TEMPLATE_DECL
:
19139 /* Matched cases are handled by the ARG == PARM test above. */
19140 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19143 /* A non-type template parameter that is a variable should be a
19144 an integral constant, in which case, it whould have been
19145 folded into its (constant) value. So we should not be getting
19146 a variable here. */
19147 gcc_unreachable ();
19149 case TYPE_ARGUMENT_PACK
:
19150 case NONTYPE_ARGUMENT_PACK
:
19151 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
19152 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
19155 case DECLTYPE_TYPE
:
19156 case UNDERLYING_TYPE
:
19157 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
19158 or UNDERLYING_TYPE nodes. */
19159 return unify_success (explain_p
);
19162 /* Unification fails if we hit an error node. */
19163 return unify_invalid (explain_p
);
19166 if (REFERENCE_REF_P (parm
))
19168 if (REFERENCE_REF_P (arg
))
19169 arg
= TREE_OPERAND (arg
, 0);
19170 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
19171 strict
, explain_p
);
19176 /* An unresolved overload is a nondeduced context. */
19177 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
19178 return unify_success (explain_p
);
19179 gcc_assert (EXPR_P (parm
));
19181 /* We must be looking at an expression. This can happen with
19185 void foo(S<I>, S<I + 2>);
19187 This is a "nondeduced context":
19191 The nondeduced contexts are:
19193 --A type that is a template-id in which one or more of
19194 the template-arguments is an expression that references
19195 a template-parameter.
19197 In these cases, we assume deduction succeeded, but don't
19198 actually infer any unifications. */
19200 if (!uses_template_parms (parm
)
19201 && !template_args_equal (parm
, arg
))
19202 return unify_expression_unequal (explain_p
, parm
, arg
);
19204 return unify_success (explain_p
);
19207 #undef RECUR_AND_CHECK_FAILURE
19209 /* Note that DECL can be defined in this translation unit, if
19213 mark_definable (tree decl
)
19216 DECL_NOT_REALLY_EXTERN (decl
) = 1;
19217 FOR_EACH_CLONE (clone
, decl
)
19218 DECL_NOT_REALLY_EXTERN (clone
) = 1;
19221 /* Called if RESULT is explicitly instantiated, or is a member of an
19222 explicitly instantiated class. */
19225 mark_decl_instantiated (tree result
, int extern_p
)
19227 SET_DECL_EXPLICIT_INSTANTIATION (result
);
19229 /* If this entity has already been written out, it's too late to
19230 make any modifications. */
19231 if (TREE_ASM_WRITTEN (result
))
19234 /* For anonymous namespace we don't need to do anything. */
19235 if (decl_anon_ns_mem_p (result
))
19237 gcc_assert (!TREE_PUBLIC (result
));
19241 if (TREE_CODE (result
) != FUNCTION_DECL
)
19242 /* The TREE_PUBLIC flag for function declarations will have been
19243 set correctly by tsubst. */
19244 TREE_PUBLIC (result
) = 1;
19246 /* This might have been set by an earlier implicit instantiation. */
19247 DECL_COMDAT (result
) = 0;
19250 DECL_NOT_REALLY_EXTERN (result
) = 0;
19253 mark_definable (result
);
19254 mark_needed (result
);
19255 /* Always make artificials weak. */
19256 if (DECL_ARTIFICIAL (result
) && flag_weak
)
19257 comdat_linkage (result
);
19258 /* For WIN32 we also want to put explicit instantiations in
19259 linkonce sections. */
19260 else if (TREE_PUBLIC (result
))
19261 maybe_make_one_only (result
);
19264 /* If EXTERN_P, then this function will not be emitted -- unless
19265 followed by an explicit instantiation, at which point its linkage
19266 will be adjusted. If !EXTERN_P, then this function will be
19267 emitted here. In neither circumstance do we want
19268 import_export_decl to adjust the linkage. */
19269 DECL_INTERFACE_KNOWN (result
) = 1;
19272 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
19273 important template arguments. If any are missing, we check whether
19274 they're important by using error_mark_node for substituting into any
19275 args that were used for partial ordering (the ones between ARGS and END)
19276 and seeing if it bubbles up. */
19279 check_undeduced_parms (tree targs
, tree args
, tree end
)
19281 bool found
= false;
19283 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
19284 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
19287 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
19291 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
19292 if (substed
== error_mark_node
)
19298 /* Given two function templates PAT1 and PAT2, return:
19300 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
19301 -1 if PAT2 is more specialized than PAT1.
19302 0 if neither is more specialized.
19304 LEN indicates the number of parameters we should consider
19305 (defaulted parameters should not be considered).
19307 The 1998 std underspecified function template partial ordering, and
19308 DR214 addresses the issue. We take pairs of arguments, one from
19309 each of the templates, and deduce them against each other. One of
19310 the templates will be more specialized if all the *other*
19311 template's arguments deduce against its arguments and at least one
19312 of its arguments *does* *not* deduce against the other template's
19313 corresponding argument. Deduction is done as for class templates.
19314 The arguments used in deduction have reference and top level cv
19315 qualifiers removed. Iff both arguments were originally reference
19316 types *and* deduction succeeds in both directions, an lvalue reference
19317 wins against an rvalue reference and otherwise the template
19318 with the more cv-qualified argument wins for that pairing (if
19319 neither is more cv-qualified, they both are equal). Unlike regular
19320 deduction, after all the arguments have been deduced in this way,
19321 we do *not* verify the deduced template argument values can be
19322 substituted into non-deduced contexts.
19324 The logic can be a bit confusing here, because we look at deduce1 and
19325 targs1 to see if pat2 is at least as specialized, and vice versa; if we
19326 can find template arguments for pat1 to make arg1 look like arg2, that
19327 means that arg2 is at least as specialized as arg1. */
19330 more_specialized_fn (tree pat1
, tree pat2
, int len
)
19332 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
19333 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
19334 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
19335 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
19336 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
19337 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
19338 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
19339 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
19340 tree origs1
, origs2
;
19341 bool lose1
= false;
19342 bool lose2
= false;
19344 /* Remove the this parameter from non-static member functions. If
19345 one is a non-static member function and the other is not a static
19346 member function, remove the first parameter from that function
19347 also. This situation occurs for operator functions where we
19348 locate both a member function (with this pointer) and non-member
19349 operator (with explicit first operand). */
19350 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
19352 len
--; /* LEN is the number of significant arguments for DECL1 */
19353 args1
= TREE_CHAIN (args1
);
19354 if (!DECL_STATIC_FUNCTION_P (decl2
))
19355 args2
= TREE_CHAIN (args2
);
19357 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
19359 args2
= TREE_CHAIN (args2
);
19360 if (!DECL_STATIC_FUNCTION_P (decl1
))
19363 args1
= TREE_CHAIN (args1
);
19367 /* If only one is a conversion operator, they are unordered. */
19368 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
19371 /* Consider the return type for a conversion function */
19372 if (DECL_CONV_FN_P (decl1
))
19374 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
19375 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
19379 processing_template_decl
++;
19385 /* Stop when an ellipsis is seen. */
19386 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
19388 tree arg1
= TREE_VALUE (args1
);
19389 tree arg2
= TREE_VALUE (args2
);
19390 int deduce1
, deduce2
;
19396 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
19397 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
19399 /* When both arguments are pack expansions, we need only
19400 unify the patterns themselves. */
19401 arg1
= PACK_EXPANSION_PATTERN (arg1
);
19402 arg2
= PACK_EXPANSION_PATTERN (arg2
);
19404 /* This is the last comparison we need to do. */
19408 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
19410 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
19411 arg1
= TREE_TYPE (arg1
);
19412 quals1
= cp_type_quals (arg1
);
19415 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
19417 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
19418 arg2
= TREE_TYPE (arg2
);
19419 quals2
= cp_type_quals (arg2
);
19422 arg1
= TYPE_MAIN_VARIANT (arg1
);
19423 arg2
= TYPE_MAIN_VARIANT (arg2
);
19425 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
19427 int i
, len2
= list_length (args2
);
19428 tree parmvec
= make_tree_vec (1);
19429 tree argvec
= make_tree_vec (len2
);
19432 /* Setup the parameter vector, which contains only ARG1. */
19433 TREE_VEC_ELT (parmvec
, 0) = arg1
;
19435 /* Setup the argument vector, which contains the remaining
19437 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
19438 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
19440 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
19441 argvec
, DEDUCE_EXACT
,
19442 /*subr=*/true, /*explain_p=*/false)
19445 /* We cannot deduce in the other direction, because ARG1 is
19446 a pack expansion but ARG2 is not. */
19449 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
19451 int i
, len1
= list_length (args1
);
19452 tree parmvec
= make_tree_vec (1);
19453 tree argvec
= make_tree_vec (len1
);
19456 /* Setup the parameter vector, which contains only ARG1. */
19457 TREE_VEC_ELT (parmvec
, 0) = arg2
;
19459 /* Setup the argument vector, which contains the remaining
19461 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
19462 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
19464 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
19465 argvec
, DEDUCE_EXACT
,
19466 /*subr=*/true, /*explain_p=*/false)
19469 /* We cannot deduce in the other direction, because ARG2 is
19470 a pack expansion but ARG1 is not.*/
19476 /* The normal case, where neither argument is a pack
19478 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
19479 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
19481 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
19482 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
19486 /* If we couldn't deduce arguments for tparms1 to make arg1 match
19487 arg2, then arg2 is not as specialized as arg1. */
19493 /* "If, for a given type, deduction succeeds in both directions
19494 (i.e., the types are identical after the transformations above)
19495 and both P and A were reference types (before being replaced with
19496 the type referred to above):
19497 - if the type from the argument template was an lvalue reference and
19498 the type from the parameter template was not, the argument type is
19499 considered to be more specialized than the other; otherwise,
19500 - if the type from the argument template is more cv-qualified
19501 than the type from the parameter template (as described above),
19502 the argument type is considered to be more specialized than the other;
19504 - neither type is more specialized than the other." */
19506 if (deduce1
&& deduce2
)
19508 if (ref1
&& ref2
&& ref1
!= ref2
)
19515 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
19517 if ((quals1
& quals2
) == quals2
)
19519 if ((quals1
& quals2
) == quals1
)
19524 if (lose1
&& lose2
)
19525 /* We've failed to deduce something in either direction.
19526 These must be unordered. */
19529 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
19530 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
19531 /* We have already processed all of the arguments in our
19532 handing of the pack expansion type. */
19535 args1
= TREE_CHAIN (args1
);
19536 args2
= TREE_CHAIN (args2
);
19539 /* "In most cases, all template parameters must have values in order for
19540 deduction to succeed, but for partial ordering purposes a template
19541 parameter may remain without a value provided it is not used in the
19542 types being used for partial ordering."
19544 Thus, if we are missing any of the targs1 we need to substitute into
19545 origs1, then pat2 is not as specialized as pat1. This can happen when
19546 there is a nondeduced context. */
19547 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
19549 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
19552 processing_template_decl
--;
19554 /* All things being equal, if the next argument is a pack expansion
19555 for one function but not for the other, prefer the
19556 non-variadic function. FIXME this is bogus; see c++/41958. */
19558 && args1
&& TREE_VALUE (args1
)
19559 && args2
&& TREE_VALUE (args2
))
19561 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
19562 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
19565 // All things still being equal, determine if one is more constrained.
19566 if (lose1
== lose2
)
19568 tree c1
= get_constraints (DECL_TEMPLATE_RESULT (pat1
));
19569 tree c2
= get_constraints (DECL_TEMPLATE_RESULT (pat2
));
19570 lose1
= !subsumes_constraints (c1
, c2
);
19571 lose2
= !subsumes_constraints (c2
, c1
);
19574 if (lose1
== lose2
)
19582 /* Determine which of two partial specializations of TMPL is more
19585 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
19586 to the first partial specialization. The TREE_PURPOSE is the
19587 innermost set of template parameters for the partial
19588 specialization. PAT2 is similar, but for the second template.
19590 Return 1 if the first partial specialization is more specialized;
19591 -1 if the second is more specialized; 0 if neither is more
19594 See [temp.class.order] for information about determining which of
19595 two templates is more specialized. */
19598 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
19602 bool any_deductions
= false;
19604 tree tmpl1
= TREE_VALUE (pat1
);
19605 tree tmpl2
= TREE_VALUE (pat2
);
19606 tree parms1
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl1
);
19607 tree parms2
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl2
);
19608 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
19609 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
19611 /* Just like what happens for functions, if we are ordering between
19612 different template specializations, we may encounter dependent
19613 types in the arguments, and we need our dependency check functions
19614 to behave correctly. */
19615 ++processing_template_decl
;
19616 targs
= get_partial_spec_bindings (tmpl
, parms1
, specargs1
, specargs2
);
19620 any_deductions
= true;
19623 targs
= get_partial_spec_bindings (tmpl
, parms2
, specargs2
, specargs1
);
19627 any_deductions
= true;
19629 --processing_template_decl
;
19631 /* In the case of a tie where at least one of the templates
19632 has a parameter pack at the end, the template with the most
19633 non-packed parameters wins. */
19636 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
19637 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
19639 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
19640 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
19641 int len1
= TREE_VEC_LENGTH (args1
);
19642 int len2
= TREE_VEC_LENGTH (args2
);
19644 /* We don't count the pack expansion at the end. */
19645 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
19647 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
19652 else if (len1
< len2
)
19657 // If still tied at this point, the most specialized is also
19658 // the most constrained.
19660 return more_constrained (tmpl1
, tmpl2
);
19665 /* Return the template arguments that will produce the function signature
19666 DECL from the function template FN, with the explicit template
19667 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
19668 also match. Return NULL_TREE if no satisfactory arguments could be
19672 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
19674 int ntparms
= DECL_NTPARMS (fn
);
19675 tree targs
= make_tree_vec (ntparms
);
19676 tree decl_type
= TREE_TYPE (decl
);
19677 tree decl_arg_types
;
19679 unsigned int nargs
, ix
;
19682 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
19684 /* Never do unification on the 'this' parameter. */
19685 decl_arg_types
= skip_artificial_parms_for (decl
,
19686 TYPE_ARG_TYPES (decl_type
));
19688 nargs
= list_length (decl_arg_types
);
19689 args
= XALLOCAVEC (tree
, nargs
);
19690 for (arg
= decl_arg_types
, ix
= 0;
19691 arg
!= NULL_TREE
&& arg
!= void_list_node
;
19692 arg
= TREE_CHAIN (arg
), ++ix
)
19693 args
[ix
] = TREE_VALUE (arg
);
19695 if (fn_type_unification (fn
, explicit_args
, targs
,
19697 (check_rettype
|| DECL_CONV_FN_P (fn
)
19698 ? TREE_TYPE (decl_type
) : NULL_TREE
),
19699 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
19701 == error_mark_node
)
19707 /* Return the innermost template arguments that, when applied to a partial
19708 specialization of TMPL whose innermost template parameters are
19709 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
19712 For example, suppose we have:
19714 template <class T, class U> struct S {};
19715 template <class T> struct S<T*, int> {};
19717 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
19718 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
19719 int}. The resulting vector will be {double}, indicating that `T'
19720 is bound to `double'. */
19723 get_partial_spec_bindings (tree tmpl
, tree tparms
, tree spec_args
, tree args
)
19725 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
19727 tree innermost_deduced_args
;
19729 innermost_deduced_args
= make_tree_vec (ntparms
);
19730 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
19732 deduced_args
= copy_node (args
);
19733 SET_TMPL_ARGS_LEVEL (deduced_args
,
19734 TMPL_ARGS_DEPTH (deduced_args
),
19735 innermost_deduced_args
);
19738 deduced_args
= innermost_deduced_args
;
19740 if (unify (tparms
, deduced_args
,
19741 INNERMOST_TEMPLATE_ARGS (spec_args
),
19742 INNERMOST_TEMPLATE_ARGS (args
),
19743 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
19746 for (i
= 0; i
< ntparms
; ++i
)
19747 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
19750 /* Verify that nondeduced template arguments agree with the type
19751 obtained from argument deduction.
19755 struct A { typedef int X; };
19756 template <class T, class U> struct C {};
19757 template <class T> struct C<T, typename T::X> {};
19759 Then with the instantiation `C<A, int>', we can deduce that
19760 `T' is `A' but unify () does not check whether `typename T::X'
19762 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
19763 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
19765 tf_none
, false, false);
19766 if (spec_args
== error_mark_node
19767 /* We only need to check the innermost arguments; the other
19768 arguments will always agree. */
19769 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
19770 INNERMOST_TEMPLATE_ARGS (args
)))
19773 /* Now that we have bindings for all of the template arguments,
19774 ensure that the arguments deduced for the template template
19775 parameters have compatible template parameter lists. See the use
19776 of template_template_parm_bindings_ok_p in fn_type_unification
19777 for more information. */
19778 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
19781 return deduced_args
;
19784 // Compare two function templates T1 and T2 by deducing bindings
19785 // from one against the other. If both deductions succeed, compare
19786 // constraints to see which is more constrained.
19788 more_specialized_inst (tree t1
, tree t2
)
19793 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
19799 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
19805 // If both deductions succeed, then one may be more constrained.
19806 if (count
== 2 && fate
== 0)
19807 fate
= more_constrained (t1
, t2
);
19812 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
19813 Return the TREE_LIST node with the most specialized template, if
19814 any. If there is no most specialized template, the error_mark_node
19817 Note that this function does not look at, or modify, the
19818 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
19819 returned is one of the elements of INSTANTIATIONS, callers may
19820 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
19821 and retrieve it from the value returned. */
19824 most_specialized_instantiation (tree templates
)
19828 ++processing_template_decl
;
19831 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
19833 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
19838 /* Equally specialized, move to next function. If there
19839 is no next function, nothing's most specialized. */
19840 fn
= TREE_CHAIN (fn
);
19848 /* Now verify that champ is better than everything earlier in the
19849 instantiation list. */
19850 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
19851 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
19858 processing_template_decl
--;
19861 return error_mark_node
;
19866 /* If DECL is a specialization of some template, return the most
19867 general such template. Otherwise, returns NULL_TREE.
19869 For example, given:
19871 template <class T> struct S { template <class U> void f(U); };
19873 if TMPL is `template <class U> void S<int>::f(U)' this will return
19874 the full template. This function will not trace past partial
19875 specializations, however. For example, given in addition:
19877 template <class T> struct S<T*> { template <class U> void f(U); };
19879 if TMPL is `template <class U> void S<int*>::f(U)' this will return
19880 `template <class T> template <class U> S<T*>::f(U)'. */
19883 most_general_template (tree decl
)
19885 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
19887 if (tree tinfo
= get_template_info (decl
))
19888 decl
= TI_TEMPLATE (tinfo
);
19889 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
19890 template friend, or a FIELD_DECL for a capture pack. */
19891 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
19895 /* Look for more and more general templates. */
19896 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
19898 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
19899 (See cp-tree.h for details.) */
19900 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
19903 if (CLASS_TYPE_P (TREE_TYPE (decl
))
19904 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
19905 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
19908 /* Stop if we run into an explicitly specialized class template. */
19909 if (!DECL_NAMESPACE_SCOPE_P (decl
)
19910 && DECL_CONTEXT (decl
)
19911 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
19914 decl
= DECL_TI_TEMPLATE (decl
);
19920 /* Return the most specialized of the template partial specializations
19921 which can produce TARGET, a specialization of some class or variable
19922 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
19923 a TEMPLATE_DECL node corresponding to the partial specialization, while
19924 the TREE_PURPOSE is the set of template arguments that must be
19925 substituted into the template pattern in order to generate TARGET.
19927 If the choice of partial specialization is ambiguous, a diagnostic
19928 is issued, and the error_mark_node is returned. If there are no
19929 partial specializations matching TARGET, then NULL_TREE is
19930 returned, indicating that the primary template should be used. */
19933 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
19935 tree list
= NULL_TREE
;
19940 tree outer_args
= NULL_TREE
;
19943 if (TYPE_P (target
))
19945 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
19946 tmpl
= TI_TEMPLATE (tinfo
);
19947 args
= TI_ARGS (tinfo
);
19949 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
19951 tmpl
= TREE_OPERAND (target
, 0);
19952 args
= TREE_OPERAND (target
, 1);
19954 else if (VAR_P (target
))
19956 tree tinfo
= DECL_TEMPLATE_INFO (target
);
19957 tmpl
= TI_TEMPLATE (tinfo
);
19958 args
= TI_ARGS (tinfo
);
19961 gcc_unreachable ();
19963 tree main_tmpl
= most_general_template (tmpl
);
19965 /* For determining which partial specialization to use, only the
19966 innermost args are interesting. */
19967 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
19969 outer_args
= strip_innermost_template_args (args
, 1);
19970 args
= INNERMOST_TEMPLATE_ARGS (args
);
19973 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
19975 tree partial_spec_args
;
19977 tree spec_tmpl
= TREE_VALUE (t
);
19979 partial_spec_args
= TREE_PURPOSE (t
);
19981 ++processing_template_decl
;
19985 /* Discard the outer levels of args, and then substitute in the
19986 template args from the enclosing class. */
19987 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
19988 partial_spec_args
= tsubst_template_args
19989 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
19991 /* And the same for the partial specialization TEMPLATE_DECL. */
19992 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
19995 partial_spec_args
=
19996 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
19999 /*require_all_args=*/true,
20000 /*use_default_args=*/true);
20002 --processing_template_decl
;
20004 if (partial_spec_args
== error_mark_node
)
20005 return error_mark_node
;
20006 if (spec_tmpl
== error_mark_node
)
20007 return error_mark_node
;
20009 tree parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
20010 spec_args
= get_partial_spec_bindings (tmpl
, parms
,
20016 spec_args
= add_to_template_args (outer_args
, spec_args
);
20018 /* Keep the candidate only if the constraints are satisfied,
20019 or if we're not compiling with concepts. */
20020 if (!flag_concepts
|| constraints_satisfied_p (spec_tmpl
, spec_args
))
20022 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
20023 TREE_TYPE (list
) = TREE_TYPE (t
);
20031 ambiguous_p
= false;
20034 t
= TREE_CHAIN (t
);
20035 for (; t
; t
= TREE_CHAIN (t
))
20037 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
20044 t
= TREE_CHAIN (t
);
20047 ambiguous_p
= true;
20056 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
20058 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
20061 ambiguous_p
= true;
20069 char *spaces
= NULL
;
20070 if (!(complain
& tf_error
))
20071 return error_mark_node
;
20072 if (TYPE_P (target
))
20073 error ("ambiguous template instantiation for %q#T", target
);
20075 error ("ambiguous template instantiation for %q#D", target
);
20076 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
20077 for (t
= list
; t
; t
= TREE_CHAIN (t
))
20079 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
20080 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
20081 "%s %#S", spaces
? spaces
: str
, subst
);
20082 spaces
= spaces
? spaces
: get_spaces (str
);
20085 return error_mark_node
;
20091 /* Explicitly instantiate DECL. */
20094 do_decl_instantiation (tree decl
, tree storage
)
20096 tree result
= NULL_TREE
;
20099 if (!decl
|| decl
== error_mark_node
)
20100 /* An error occurred, for which grokdeclarator has already issued
20101 an appropriate message. */
20103 else if (! DECL_LANG_SPECIFIC (decl
))
20105 error ("explicit instantiation of non-template %q#D", decl
);
20109 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
20110 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
20112 if (VAR_P (decl
) && !var_templ
)
20114 /* There is an asymmetry here in the way VAR_DECLs and
20115 FUNCTION_DECLs are handled by grokdeclarator. In the case of
20116 the latter, the DECL we get back will be marked as a
20117 template instantiation, and the appropriate
20118 DECL_TEMPLATE_INFO will be set up. This does not happen for
20119 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
20120 should handle VAR_DECLs as it currently handles
20122 if (!DECL_CLASS_SCOPE_P (decl
))
20124 error ("%qD is not a static data member of a class template", decl
);
20127 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
20128 if (!result
|| !VAR_P (result
))
20130 error ("no matching template for %qD found", decl
);
20133 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
20135 error ("type %qT for explicit instantiation %qD does not match "
20136 "declared type %qT", TREE_TYPE (result
), decl
,
20141 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
20143 error ("explicit instantiation of %q#D", decl
);
20149 /* Check for various error cases. Note that if the explicit
20150 instantiation is valid the RESULT will currently be marked as an
20151 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
20152 until we get here. */
20154 if (DECL_TEMPLATE_SPECIALIZATION (result
))
20156 /* DR 259 [temp.spec].
20158 Both an explicit instantiation and a declaration of an explicit
20159 specialization shall not appear in a program unless the explicit
20160 instantiation follows a declaration of the explicit specialization.
20162 For a given set of template parameters, if an explicit
20163 instantiation of a template appears after a declaration of an
20164 explicit specialization for that template, the explicit
20165 instantiation has no effect. */
20168 else if (DECL_EXPLICIT_INSTANTIATION (result
))
20172 No program shall explicitly instantiate any template more
20175 We check DECL_NOT_REALLY_EXTERN so as not to complain when
20176 the first instantiation was `extern' and the second is not,
20177 and EXTERN_P for the opposite case. */
20178 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
20179 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
20180 /* If an "extern" explicit instantiation follows an ordinary
20181 explicit instantiation, the template is instantiated. */
20185 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
20187 error ("no matching template for %qD found", result
);
20190 else if (!DECL_TEMPLATE_INFO (result
))
20192 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
20196 if (storage
== NULL_TREE
)
20198 else if (storage
== ridpointers
[(int) RID_EXTERN
])
20200 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
20201 pedwarn (input_location
, OPT_Wpedantic
,
20202 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
20207 error ("storage class %qD applied to template instantiation", storage
);
20209 check_explicit_instantiation_namespace (result
);
20210 mark_decl_instantiated (result
, extern_p
);
20212 instantiate_decl (result
, /*defer_ok=*/1,
20213 /*expl_inst_class_mem_p=*/false);
20217 mark_class_instantiated (tree t
, int extern_p
)
20219 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
20220 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
20221 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
20222 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
20225 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
20226 rest_of_type_compilation (t
, 1);
20230 /* Called from do_type_instantiation through binding_table_foreach to
20231 do recursive instantiation for the type bound in ENTRY. */
20233 bt_instantiate_type_proc (binding_entry entry
, void *data
)
20235 tree storage
= *(tree
*) data
;
20237 if (MAYBE_CLASS_TYPE_P (entry
->type
)
20238 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
20239 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
20242 /* Called from do_type_instantiation to instantiate a member
20243 (a member function or a static member variable) of an
20244 explicitly instantiated class template. */
20246 instantiate_class_member (tree decl
, int extern_p
)
20248 mark_decl_instantiated (decl
, extern_p
);
20250 instantiate_decl (decl
, /*defer_ok=*/1,
20251 /*expl_inst_class_mem_p=*/true);
20254 /* Perform an explicit instantiation of template class T. STORAGE, if
20255 non-null, is the RID for extern, inline or static. COMPLAIN is
20256 nonzero if this is called from the parser, zero if called recursively,
20257 since the standard is unclear (as detailed below). */
20260 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
20265 int previous_instantiation_extern_p
= 0;
20267 if (TREE_CODE (t
) == TYPE_DECL
)
20270 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
20273 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
20275 error ("explicit instantiation of non-class template %qD", tmpl
);
20277 error ("explicit instantiation of non-template type %qT", t
);
20283 if (!COMPLETE_TYPE_P (t
))
20285 if (complain
& tf_error
)
20286 error ("explicit instantiation of %q#T before definition of template",
20291 if (storage
!= NULL_TREE
)
20293 if (!in_system_header_at (input_location
))
20295 if (storage
== ridpointers
[(int) RID_EXTERN
])
20297 if (cxx_dialect
== cxx98
)
20298 pedwarn (input_location
, OPT_Wpedantic
,
20299 "ISO C++ 1998 forbids the use of %<extern%> on "
20300 "explicit instantiations");
20303 pedwarn (input_location
, OPT_Wpedantic
,
20304 "ISO C++ forbids the use of %qE"
20305 " on explicit instantiations", storage
);
20308 if (storage
== ridpointers
[(int) RID_INLINE
])
20310 else if (storage
== ridpointers
[(int) RID_EXTERN
])
20312 else if (storage
== ridpointers
[(int) RID_STATIC
])
20316 error ("storage class %qD applied to template instantiation",
20322 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
20324 /* DR 259 [temp.spec].
20326 Both an explicit instantiation and a declaration of an explicit
20327 specialization shall not appear in a program unless the explicit
20328 instantiation follows a declaration of the explicit specialization.
20330 For a given set of template parameters, if an explicit
20331 instantiation of a template appears after a declaration of an
20332 explicit specialization for that template, the explicit
20333 instantiation has no effect. */
20336 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
20340 No program shall explicitly instantiate any template more
20343 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
20344 instantiation was `extern'. If EXTERN_P then the second is.
20345 These cases are OK. */
20346 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
20348 if (!previous_instantiation_extern_p
&& !extern_p
20349 && (complain
& tf_error
))
20350 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
20352 /* If we've already instantiated the template, just return now. */
20353 if (!CLASSTYPE_INTERFACE_ONLY (t
))
20357 check_explicit_instantiation_namespace (TYPE_NAME (t
));
20358 mark_class_instantiated (t
, extern_p
);
20366 /* In contrast to implicit instantiation, where only the
20367 declarations, and not the definitions, of members are
20368 instantiated, we have here:
20372 The explicit instantiation of a class template specialization
20373 implies the instantiation of all of its members not
20374 previously explicitly specialized in the translation unit
20375 containing the explicit instantiation.
20377 Of course, we can't instantiate member template classes, since
20378 we don't have any arguments for them. Note that the standard
20379 is unclear on whether the instantiation of the members are
20380 *explicit* instantiations or not. However, the most natural
20381 interpretation is that it should be an explicit instantiation. */
20384 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
20385 if (TREE_CODE (tmp
) == FUNCTION_DECL
20386 && DECL_TEMPLATE_INSTANTIATION (tmp
))
20387 instantiate_class_member (tmp
, extern_p
);
20389 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
20390 if (VAR_P (tmp
) && DECL_TEMPLATE_INSTANTIATION (tmp
))
20391 instantiate_class_member (tmp
, extern_p
);
20393 if (CLASSTYPE_NESTED_UTDS (t
))
20394 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
20395 bt_instantiate_type_proc
, &storage
);
20399 /* Given a function DECL, which is a specialization of TMPL, modify
20400 DECL to be a re-instantiation of TMPL with the same template
20401 arguments. TMPL should be the template into which tsubst'ing
20402 should occur for DECL, not the most general template.
20404 One reason for doing this is a scenario like this:
20407 void f(const T&, int i);
20409 void g() { f(3, 7); }
20412 void f(const T& t, const int i) { }
20414 Note that when the template is first instantiated, with
20415 instantiate_template, the resulting DECL will have no name for the
20416 first parameter, and the wrong type for the second. So, when we go
20417 to instantiate the DECL, we regenerate it. */
20420 regenerate_decl_from_template (tree decl
, tree tmpl
)
20422 /* The arguments used to instantiate DECL, from the most general
20427 args
= DECL_TI_ARGS (decl
);
20428 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
20430 /* Make sure that we can see identifiers, and compute access
20432 push_access_scope (decl
);
20434 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20442 args_depth
= TMPL_ARGS_DEPTH (args
);
20443 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
20444 if (args_depth
> parms_depth
)
20445 args
= get_innermost_template_args (args
, parms_depth
);
20447 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
20448 args
, tf_error
, NULL_TREE
,
20449 /*defer_ok*/false);
20450 if (specs
&& specs
!= error_mark_node
)
20451 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
20454 /* Merge parameter declarations. */
20455 decl_parm
= skip_artificial_parms_for (decl
,
20456 DECL_ARGUMENTS (decl
));
20458 = skip_artificial_parms_for (code_pattern
,
20459 DECL_ARGUMENTS (code_pattern
));
20460 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
20465 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
20466 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
20467 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
20469 parm_type
= type_decays_to (parm_type
);
20470 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
20471 TREE_TYPE (decl_parm
) = parm_type
;
20472 attributes
= DECL_ATTRIBUTES (pattern_parm
);
20473 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
20475 DECL_ATTRIBUTES (decl_parm
) = attributes
;
20476 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
20478 decl_parm
= DECL_CHAIN (decl_parm
);
20479 pattern_parm
= DECL_CHAIN (pattern_parm
);
20481 /* Merge any parameters that match with the function parameter
20483 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
20486 tree expanded_types
;
20487 /* Expand the TYPE_PACK_EXPANSION that provides the types for
20488 the parameters in this function parameter pack. */
20489 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
20490 args
, tf_error
, NULL_TREE
);
20491 len
= TREE_VEC_LENGTH (expanded_types
);
20492 for (i
= 0; i
< len
; i
++)
20497 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
20498 /* Rename the parameter to include the index. */
20499 DECL_NAME (decl_parm
) =
20500 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
20501 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
20502 parm_type
= type_decays_to (parm_type
);
20503 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
20504 TREE_TYPE (decl_parm
) = parm_type
;
20505 attributes
= DECL_ATTRIBUTES (pattern_parm
);
20506 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
20508 DECL_ATTRIBUTES (decl_parm
) = attributes
;
20509 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
20511 decl_parm
= DECL_CHAIN (decl_parm
);
20514 /* Merge additional specifiers from the CODE_PATTERN. */
20515 if (DECL_DECLARED_INLINE_P (code_pattern
)
20516 && !DECL_DECLARED_INLINE_P (decl
))
20517 DECL_DECLARED_INLINE_P (decl
) = 1;
20519 else if (VAR_P (decl
))
20521 DECL_INITIAL (decl
) =
20522 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
20523 tf_error
, DECL_TI_TEMPLATE (decl
),
20524 /*integral_constant_expression_p=*/false);
20525 if (VAR_HAD_UNKNOWN_BOUND (decl
))
20526 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
20527 tf_error
, DECL_TI_TEMPLATE (decl
));
20530 gcc_unreachable ();
20532 pop_access_scope (decl
);
20535 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
20536 substituted to get DECL. */
20539 template_for_substitution (tree decl
)
20541 tree tmpl
= DECL_TI_TEMPLATE (decl
);
20543 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
20544 for the instantiation. This is not always the most general
20545 template. Consider, for example:
20548 struct S { template <class U> void f();
20549 template <> void f<int>(); };
20551 and an instantiation of S<double>::f<int>. We want TD to be the
20552 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
20553 while (/* An instantiation cannot have a definition, so we need a
20554 more general template. */
20555 DECL_TEMPLATE_INSTANTIATION (tmpl
)
20556 /* We must also deal with friend templates. Given:
20558 template <class T> struct S {
20559 template <class U> friend void f() {};
20562 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
20563 so far as the language is concerned, but that's still
20564 where we get the pattern for the instantiation from. On
20565 other hand, if the definition comes outside the class, say:
20567 template <class T> struct S {
20568 template <class U> friend void f();
20570 template <class U> friend void f() {}
20572 we don't need to look any further. That's what the check for
20573 DECL_INITIAL is for. */
20574 || (TREE_CODE (decl
) == FUNCTION_DECL
20575 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
20576 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
20578 /* The present template, TD, should not be a definition. If it
20579 were a definition, we should be using it! Note that we
20580 cannot restructure the loop to just keep going until we find
20581 a template with a definition, since that might go too far if
20582 a specialization was declared, but not defined. */
20584 /* Fetch the more general template. */
20585 tmpl
= DECL_TI_TEMPLATE (tmpl
);
20591 /* Returns true if we need to instantiate this template instance even if we
20592 know we aren't going to emit it. */
20595 always_instantiate_p (tree decl
)
20597 /* We always instantiate inline functions so that we can inline them. An
20598 explicit instantiation declaration prohibits implicit instantiation of
20599 non-inline functions. With high levels of optimization, we would
20600 normally inline non-inline functions -- but we're not allowed to do
20601 that for "extern template" functions. Therefore, we check
20602 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
20603 return ((TREE_CODE (decl
) == FUNCTION_DECL
20604 && (DECL_DECLARED_INLINE_P (decl
)
20605 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
20606 /* And we need to instantiate static data members so that
20607 their initializers are available in integral constant
20610 && decl_maybe_constant_var_p (decl
)));
20613 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
20614 instantiate it now, modifying TREE_TYPE (fn). */
20617 maybe_instantiate_noexcept (tree fn
)
20619 tree fntype
, spec
, noex
, clone
;
20621 /* Don't instantiate a noexcept-specification from template context. */
20622 if (processing_template_decl
)
20625 if (DECL_CLONED_FUNCTION_P (fn
))
20626 fn
= DECL_CLONED_FUNCTION (fn
);
20627 fntype
= TREE_TYPE (fn
);
20628 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
20630 if (!spec
|| !TREE_PURPOSE (spec
))
20633 noex
= TREE_PURPOSE (spec
);
20635 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
20637 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
20638 spec
= get_defaulted_eh_spec (fn
);
20639 else if (push_tinst_level (fn
))
20641 push_access_scope (fn
);
20642 push_deferring_access_checks (dk_no_deferred
);
20643 input_location
= DECL_SOURCE_LOCATION (fn
);
20644 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
20645 DEFERRED_NOEXCEPT_ARGS (noex
),
20646 tf_warning_or_error
, fn
,
20647 /*function_p=*/false,
20648 /*integral_constant_expression_p=*/true);
20649 pop_deferring_access_checks ();
20650 pop_access_scope (fn
);
20651 pop_tinst_level ();
20652 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
20653 if (spec
== error_mark_node
)
20654 spec
= noexcept_false_spec
;
20657 spec
= noexcept_false_spec
;
20659 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
20662 FOR_EACH_CLONE (clone
, fn
)
20664 if (TREE_TYPE (clone
) == fntype
)
20665 TREE_TYPE (clone
) = TREE_TYPE (fn
);
20667 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
20671 /* Produce the definition of D, a _DECL generated from a template. If
20672 DEFER_OK is nonzero, then we don't have to actually do the
20673 instantiation now; we just have to do it sometime. Normally it is
20674 an error if this is an explicit instantiation but D is undefined.
20675 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
20676 explicitly instantiated class template. */
20679 instantiate_decl (tree d
, int defer_ok
,
20680 bool expl_inst_class_mem_p
)
20682 tree tmpl
= DECL_TI_TEMPLATE (d
);
20689 bool pattern_defined
;
20690 location_t saved_loc
= input_location
;
20691 int saved_unevaluated_operand
= cp_unevaluated_operand
;
20692 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
20698 /* This function should only be used to instantiate templates for
20699 functions and static member variables. */
20700 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
20702 /* A concept is never instantiated. */
20703 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
20705 /* Variables are never deferred; if instantiation is required, they
20706 are instantiated right away. That allows for better code in the
20707 case that an expression refers to the value of the variable --
20708 if the variable has a constant value the referring expression can
20709 take advantage of that fact. */
20711 || DECL_DECLARED_CONSTEXPR_P (d
))
20714 /* Don't instantiate cloned functions. Instead, instantiate the
20715 functions they cloned. */
20716 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
20717 d
= DECL_CLONED_FUNCTION (d
);
20719 if (DECL_TEMPLATE_INSTANTIATED (d
)
20720 || (TREE_CODE (d
) == FUNCTION_DECL
20721 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
20722 || DECL_TEMPLATE_SPECIALIZATION (d
))
20723 /* D has already been instantiated or explicitly specialized, so
20724 there's nothing for us to do here.
20726 It might seem reasonable to check whether or not D is an explicit
20727 instantiation, and, if so, stop here. But when an explicit
20728 instantiation is deferred until the end of the compilation,
20729 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
20730 the instantiation. */
20733 /* Check to see whether we know that this template will be
20734 instantiated in some other file, as with "extern template"
20736 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
20738 /* In general, we do not instantiate such templates. */
20739 if (external_p
&& !always_instantiate_p (d
))
20742 gen_tmpl
= most_general_template (tmpl
);
20743 gen_args
= DECL_TI_ARGS (d
);
20745 if (tmpl
!= gen_tmpl
)
20746 /* We should already have the extra args. */
20747 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
20748 == TMPL_ARGS_DEPTH (gen_args
));
20749 /* And what's in the hash table should match D. */
20750 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
20751 || spec
== NULL_TREE
);
20753 /* This needs to happen before any tsubsting. */
20754 if (! push_tinst_level (d
))
20757 timevar_push (TV_TEMPLATE_INST
);
20759 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
20760 for the instantiation. */
20761 td
= template_for_substitution (d
);
20762 code_pattern
= DECL_TEMPLATE_RESULT (td
);
20764 /* We should never be trying to instantiate a member of a class
20765 template or partial specialization. */
20766 gcc_assert (d
!= code_pattern
);
20768 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
20769 || DECL_TEMPLATE_SPECIALIZATION (td
))
20770 /* In the case of a friend template whose definition is provided
20771 outside the class, we may have too many arguments. Drop the
20772 ones we don't need. The same is true for specializations. */
20773 args
= get_innermost_template_args
20774 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
20778 if (TREE_CODE (d
) == FUNCTION_DECL
)
20780 deleted_p
= DECL_DELETED_FN (code_pattern
);
20781 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
20782 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
)
20788 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
20791 /* We may be in the middle of deferred access check. Disable it now. */
20792 push_deferring_access_checks (dk_no_deferred
);
20794 /* Unless an explicit instantiation directive has already determined
20795 the linkage of D, remember that a definition is available for
20797 if (pattern_defined
20798 && !DECL_INTERFACE_KNOWN (d
)
20799 && !DECL_NOT_REALLY_EXTERN (d
))
20800 mark_definable (d
);
20802 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
20803 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
20804 input_location
= DECL_SOURCE_LOCATION (d
);
20806 /* If D is a member of an explicitly instantiated class template,
20807 and no definition is available, treat it like an implicit
20809 if (!pattern_defined
&& expl_inst_class_mem_p
20810 && DECL_EXPLICIT_INSTANTIATION (d
))
20812 /* Leave linkage flags alone on instantiations with anonymous
20814 if (TREE_PUBLIC (d
))
20816 DECL_NOT_REALLY_EXTERN (d
) = 0;
20817 DECL_INTERFACE_KNOWN (d
) = 0;
20819 SET_DECL_IMPLICIT_INSTANTIATION (d
);
20822 /* Defer all other templates, unless we have been explicitly
20823 forbidden from doing so. */
20824 if (/* If there is no definition, we cannot instantiate the
20827 /* If it's OK to postpone instantiation, do so. */
20829 /* If this is a static data member that will be defined
20830 elsewhere, we don't want to instantiate the entire data
20831 member, but we do want to instantiate the initializer so that
20832 we can substitute that elsewhere. */
20833 || (external_p
&& VAR_P (d
))
20834 /* Handle here a deleted function too, avoid generating
20835 its body (c++/61080). */
20838 /* The definition of the static data member is now required so
20839 we must substitute the initializer. */
20841 && !DECL_INITIAL (d
)
20842 && DECL_INITIAL (code_pattern
))
20846 bool const_init
= false;
20847 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
20849 ns
= decl_namespace_context (d
);
20850 push_nested_namespace (ns
);
20852 push_nested_class (DECL_CONTEXT (d
));
20853 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
20855 tf_warning_or_error
, NULL_TREE
,
20856 /*integral_constant_expression_p=*/false);
20857 /* If instantiating the initializer involved instantiating this
20858 again, don't call cp_finish_decl twice. */
20859 if (!DECL_INITIAL (d
))
20861 /* Make sure the initializer is still constant, in case of
20862 circular dependency (template/instantiate6.C). */
20864 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
20865 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
20866 /*asmspec_tree=*/NULL_TREE
,
20867 LOOKUP_ONLYCONVERTING
);
20870 pop_nested_class ();
20871 pop_nested_namespace (ns
);
20874 /* We restore the source position here because it's used by
20875 add_pending_template. */
20876 input_location
= saved_loc
;
20878 if (at_eof
&& !pattern_defined
20879 && DECL_EXPLICIT_INSTANTIATION (d
)
20880 && DECL_NOT_REALLY_EXTERN (d
))
20883 The definition of a non-exported function template, a
20884 non-exported member function template, or a non-exported
20885 member function or static data member of a class template
20886 shall be present in every translation unit in which it is
20887 explicitly instantiated. */
20888 permerror (input_location
, "explicit instantiation of %qD "
20889 "but no definition available", d
);
20891 /* If we're in unevaluated context, we just wanted to get the
20892 constant value; this isn't an odr use, so don't queue
20893 a full instantiation. */
20894 if (cp_unevaluated_operand
!= 0)
20896 /* ??? Historically, we have instantiated inline functions, even
20897 when marked as "extern template". */
20898 if (!(external_p
&& VAR_P (d
)))
20899 add_pending_template (d
);
20902 /* Tell the repository that D is available in this translation unit
20903 -- and see if it is supposed to be instantiated here. */
20904 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
20906 /* In a PCH file, despite the fact that the repository hasn't
20907 requested instantiation in the PCH it is still possible that
20908 an instantiation will be required in a file that includes the
20911 add_pending_template (d
);
20912 /* Instantiate inline functions so that the inliner can do its
20913 job, even though we'll not be emitting a copy of this
20915 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
20919 fn_context
= decl_function_context (d
);
20920 nested
= (current_function_decl
!= NULL_TREE
);
20922 push_to_top_level ();
20926 push_function_context ();
20927 cp_unevaluated_operand
= 0;
20928 c_inhibit_evaluation_warnings
= 0;
20931 /* Mark D as instantiated so that recursive calls to
20932 instantiate_decl do not try to instantiate it again. */
20933 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
20935 /* Regenerate the declaration in case the template has been modified
20936 by a subsequent redeclaration. */
20937 regenerate_decl_from_template (d
, td
);
20939 /* We already set the file and line above. Reset them now in case
20940 they changed as a result of calling regenerate_decl_from_template. */
20941 input_location
= DECL_SOURCE_LOCATION (d
);
20946 bool const_init
= false;
20948 /* Clear out DECL_RTL; whatever was there before may not be right
20949 since we've reset the type of the declaration. */
20950 SET_DECL_RTL (d
, NULL
);
20951 DECL_IN_AGGR_P (d
) = 0;
20953 /* The initializer is placed in DECL_INITIAL by
20954 regenerate_decl_from_template so we don't need to
20955 push/pop_access_scope again here. Pull it out so that
20956 cp_finish_decl can process it. */
20957 init
= DECL_INITIAL (d
);
20958 DECL_INITIAL (d
) = NULL_TREE
;
20959 DECL_INITIALIZED_P (d
) = 0;
20961 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
20962 initializer. That function will defer actual emission until
20963 we have a chance to determine linkage. */
20964 DECL_EXTERNAL (d
) = 0;
20966 /* Enter the scope of D so that access-checking works correctly. */
20967 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
20969 push_nested_class (DECL_CONTEXT (d
));
20971 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
20972 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
20975 pop_nested_class ();
20977 if (variable_template_p (td
))
20978 note_variable_template_instantiation (d
);
20980 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
20981 synthesize_method (d
);
20982 else if (TREE_CODE (d
) == FUNCTION_DECL
)
20984 hash_map
<tree
, tree
> *saved_local_specializations
;
20988 tree block
= NULL_TREE
;
20990 /* Save away the current list, in case we are instantiating one
20991 template from within the body of another. */
20992 saved_local_specializations
= local_specializations
;
20994 /* Set up the list of local specializations. */
20995 local_specializations
= new hash_map
<tree
, tree
>;
20997 /* Set up context. */
20998 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
20999 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
21000 block
= push_stmt_list ();
21002 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
21004 /* Some typedefs referenced from within the template code need to be
21005 access checked at template instantiation time, i.e now. These
21006 types were added to the template at parsing time. Let's get those
21007 and perform the access checks then. */
21008 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl
),
21011 /* Create substitution entries for the parameters. */
21012 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
21013 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
21014 spec_parm
= DECL_ARGUMENTS (d
);
21015 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
21017 register_local_specialization (spec_parm
, tmpl_parm
);
21018 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
21019 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
21021 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
21023 if (!DECL_PACK_P (tmpl_parm
))
21025 register_local_specialization (spec_parm
, tmpl_parm
);
21026 spec_parm
= DECL_CHAIN (spec_parm
);
21030 /* Register the (value) argument pack as a specialization of
21031 TMPL_PARM, then move on. */
21032 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
21033 register_local_specialization (argpack
, tmpl_parm
);
21036 gcc_assert (!spec_parm
);
21038 /* Substitute into the body of the function. */
21039 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
21040 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
21041 tf_warning_or_error
, tmpl
);
21044 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
21045 tf_warning_or_error
, tmpl
,
21046 /*integral_constant_expression_p=*/false);
21048 /* Set the current input_location to the end of the function
21049 so that finish_function knows where we are. */
21051 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
21053 /* Remember if we saw an infinite loop in the template. */
21054 current_function_infinite_loop
21055 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
21058 /* We don't need the local specializations any more. */
21059 delete local_specializations
;
21060 local_specializations
= saved_local_specializations
;
21062 /* Finish the function. */
21063 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
21064 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
21065 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
21068 d
= finish_function (0);
21069 expand_or_defer_fn (d
);
21072 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
21073 cp_check_omp_declare_reduction (d
);
21076 /* We're not deferring instantiation any more. */
21077 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
21080 pop_from_top_level ();
21082 pop_function_context ();
21085 input_location
= saved_loc
;
21086 cp_unevaluated_operand
= saved_unevaluated_operand
;
21087 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
21088 pop_deferring_access_checks ();
21089 pop_tinst_level ();
21091 timevar_pop (TV_TEMPLATE_INST
);
21096 /* Run through the list of templates that we wish we could
21097 instantiate, and instantiate any we can. RETRIES is the
21098 number of times we retry pending template instantiation. */
21101 instantiate_pending_templates (int retries
)
21104 location_t saved_loc
= input_location
;
21106 /* Instantiating templates may trigger vtable generation. This in turn
21107 may require further template instantiations. We place a limit here
21108 to avoid infinite loop. */
21109 if (pending_templates
&& retries
>= max_tinst_depth
)
21111 tree decl
= pending_templates
->tinst
->decl
;
21113 fatal_error (input_location
,
21114 "template instantiation depth exceeds maximum of %d"
21115 " instantiating %q+D, possibly from virtual table generation"
21116 " (use -ftemplate-depth= to increase the maximum)",
21117 max_tinst_depth
, decl
);
21118 if (TREE_CODE (decl
) == FUNCTION_DECL
)
21119 /* Pretend that we defined it. */
21120 DECL_INITIAL (decl
) = error_mark_node
;
21126 struct pending_template
**t
= &pending_templates
;
21127 struct pending_template
*last
= NULL
;
21131 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
21132 bool complete
= false;
21134 if (TYPE_P (instantiation
))
21138 if (!COMPLETE_TYPE_P (instantiation
))
21140 instantiate_class_template (instantiation
);
21141 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
21142 for (fn
= TYPE_METHODS (instantiation
);
21144 fn
= TREE_CHAIN (fn
))
21145 if (! DECL_ARTIFICIAL (fn
))
21146 instantiate_decl (fn
,
21148 /*expl_inst_class_mem_p=*/false);
21149 if (COMPLETE_TYPE_P (instantiation
))
21153 complete
= COMPLETE_TYPE_P (instantiation
);
21157 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
21158 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
21161 = instantiate_decl (instantiation
,
21163 /*expl_inst_class_mem_p=*/false);
21164 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
21168 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
21169 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
21173 /* If INSTANTIATION has been instantiated, then we don't
21174 need to consider it again in the future. */
21182 current_tinst_level
= NULL
;
21184 last_pending_template
= last
;
21186 while (reconsider
);
21188 input_location
= saved_loc
;
21191 /* Substitute ARGVEC into T, which is a list of initializers for
21192 either base class or a non-static data member. The TREE_PURPOSEs
21193 are DECLs, and the TREE_VALUEs are the initializer values. Used by
21194 instantiate_decl. */
21197 tsubst_initializer_list (tree t
, tree argvec
)
21199 tree inits
= NULL_TREE
;
21201 for (; t
; t
= TREE_CHAIN (t
))
21205 tree expanded_bases
= NULL_TREE
;
21206 tree expanded_arguments
= NULL_TREE
;
21209 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
21214 /* Expand the base class expansion type into separate base
21216 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
21217 tf_warning_or_error
,
21219 if (expanded_bases
== error_mark_node
)
21222 /* We'll be building separate TREE_LISTs of arguments for
21224 len
= TREE_VEC_LENGTH (expanded_bases
);
21225 expanded_arguments
= make_tree_vec (len
);
21226 for (i
= 0; i
< len
; i
++)
21227 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
21229 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
21230 expand each argument in the TREE_VALUE of t. */
21231 expr
= make_node (EXPR_PACK_EXPANSION
);
21232 PACK_EXPANSION_LOCAL_P (expr
) = true;
21233 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
21234 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
21236 if (TREE_VALUE (t
) == void_type_node
)
21237 /* VOID_TYPE_NODE is used to indicate
21238 value-initialization. */
21240 for (i
= 0; i
< len
; i
++)
21241 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
21245 /* Substitute parameter packs into each argument in the
21247 in_base_initializer
= 1;
21248 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
21250 tree expanded_exprs
;
21252 /* Expand the argument. */
21253 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
21255 = tsubst_pack_expansion (expr
, argvec
,
21256 tf_warning_or_error
,
21258 if (expanded_exprs
== error_mark_node
)
21261 /* Prepend each of the expanded expressions to the
21262 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
21263 for (i
= 0; i
< len
; i
++)
21265 TREE_VEC_ELT (expanded_arguments
, i
) =
21266 tree_cons (NULL_TREE
,
21267 TREE_VEC_ELT (expanded_exprs
, i
),
21268 TREE_VEC_ELT (expanded_arguments
, i
));
21271 in_base_initializer
= 0;
21273 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
21274 since we built them backwards. */
21275 for (i
= 0; i
< len
; i
++)
21277 TREE_VEC_ELT (expanded_arguments
, i
) =
21278 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
21283 for (i
= 0; i
< len
; ++i
)
21285 if (expanded_bases
)
21287 decl
= TREE_VEC_ELT (expanded_bases
, i
);
21288 decl
= expand_member_init (decl
);
21289 init
= TREE_VEC_ELT (expanded_arguments
, i
);
21294 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
21295 tf_warning_or_error
, NULL_TREE
);
21297 decl
= expand_member_init (decl
);
21298 if (decl
&& !DECL_P (decl
))
21299 in_base_initializer
= 1;
21301 init
= TREE_VALUE (t
);
21303 if (init
!= void_type_node
)
21304 init
= tsubst_expr (init
, argvec
,
21305 tf_warning_or_error
, NULL_TREE
,
21306 /*integral_constant_expression_p=*/false);
21307 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
21308 /* If we had an initializer but it instantiated to nothing,
21309 value-initialize the object. This will only occur when
21310 the initializer was a pack expansion where the parameter
21311 packs used in that expansion were of length zero. */
21312 init
= void_type_node
;
21313 in_base_initializer
= 0;
21318 init
= build_tree_list (decl
, init
);
21319 TREE_CHAIN (init
) = inits
;
21327 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
21330 set_current_access_from_decl (tree decl
)
21332 if (TREE_PRIVATE (decl
))
21333 current_access_specifier
= access_private_node
;
21334 else if (TREE_PROTECTED (decl
))
21335 current_access_specifier
= access_protected_node
;
21337 current_access_specifier
= access_public_node
;
21340 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
21341 is the instantiation (which should have been created with
21342 start_enum) and ARGS are the template arguments to use. */
21345 tsubst_enum (tree tag
, tree newtag
, tree args
)
21349 if (SCOPED_ENUM_P (newtag
))
21350 begin_scope (sk_scoped_enum
, newtag
);
21352 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
21357 decl
= TREE_VALUE (e
);
21358 /* Note that in a template enum, the TREE_VALUE is the
21359 CONST_DECL, not the corresponding INTEGER_CST. */
21360 value
= tsubst_expr (DECL_INITIAL (decl
),
21361 args
, tf_warning_or_error
, NULL_TREE
,
21362 /*integral_constant_expression_p=*/true);
21364 /* Give this enumeration constant the correct access. */
21365 set_current_access_from_decl (decl
);
21367 /* Actually build the enumerator itself. */
21369 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
21372 if (SCOPED_ENUM_P (newtag
))
21375 finish_enum_value_list (newtag
);
21376 finish_enum (newtag
);
21378 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
21379 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
21382 /* DECL is a FUNCTION_DECL that is a template specialization. Return
21383 its type -- but without substituting the innermost set of template
21384 arguments. So, innermost set of template parameters will appear in
21388 get_mostly_instantiated_function_type (tree decl
)
21390 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
21391 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
21394 /* Return truthvalue if we're processing a template different from
21395 the last one involved in diagnostics. */
21397 problematic_instantiation_changed (void)
21399 return current_tinst_level
!= last_error_tinst_level
;
21402 /* Remember current template involved in diagnostics. */
21404 record_last_problematic_instantiation (void)
21406 last_error_tinst_level
= current_tinst_level
;
21409 struct tinst_level
*
21410 current_instantiation (void)
21412 return current_tinst_level
;
21415 /* Return TRUE if current_function_decl is being instantiated, false
21419 instantiating_current_function_p (void)
21421 return (current_instantiation ()
21422 && current_instantiation ()->decl
== current_function_decl
);
21425 /* [temp.param] Check that template non-type parm TYPE is of an allowable
21426 type. Return zero for ok, nonzero for disallowed. Issue error and
21427 warning messages under control of COMPLAIN. */
21430 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
21432 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
21434 else if (POINTER_TYPE_P (type
))
21436 else if (TYPE_PTRMEM_P (type
))
21438 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
21440 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
21442 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
21444 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
21447 if (complain
& tf_error
)
21449 if (type
== error_mark_node
)
21450 inform (input_location
, "invalid template non-type parameter");
21452 error ("%q#T is not a valid type for a template non-type parameter",
21458 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
21459 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
21462 dependent_type_p_r (tree type
)
21468 A type is dependent if it is:
21470 -- a template parameter. Template template parameters are types
21471 for us (since TYPE_P holds true for them) so we handle
21473 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21474 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
21476 /* -- a qualified-id with a nested-name-specifier which contains a
21477 class-name that names a dependent type or whose unqualified-id
21478 names a dependent type. */
21479 if (TREE_CODE (type
) == TYPENAME_TYPE
)
21481 /* -- a cv-qualified type where the cv-unqualified type is
21483 No code is necessary for this bullet; the code below handles
21484 cv-qualified types, and we don't want to strip aliases with
21485 TYPE_MAIN_VARIANT because of DR 1558. */
21486 /* -- a compound type constructed from any dependent type. */
21487 if (TYPE_PTRMEM_P (type
))
21488 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
21489 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
21491 else if (TYPE_PTR_P (type
)
21492 || TREE_CODE (type
) == REFERENCE_TYPE
)
21493 return dependent_type_p (TREE_TYPE (type
));
21494 else if (TREE_CODE (type
) == FUNCTION_TYPE
21495 || TREE_CODE (type
) == METHOD_TYPE
)
21499 if (dependent_type_p (TREE_TYPE (type
)))
21501 for (arg_type
= TYPE_ARG_TYPES (type
);
21503 arg_type
= TREE_CHAIN (arg_type
))
21504 if (dependent_type_p (TREE_VALUE (arg_type
)))
21508 /* -- an array type constructed from any dependent type or whose
21509 size is specified by a constant expression that is
21512 We checked for type- and value-dependence of the bounds in
21513 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
21514 if (TREE_CODE (type
) == ARRAY_TYPE
)
21516 if (TYPE_DOMAIN (type
)
21517 && dependent_type_p (TYPE_DOMAIN (type
)))
21519 return dependent_type_p (TREE_TYPE (type
));
21522 /* -- a template-id in which either the template name is a template
21524 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21526 /* ... or any of the template arguments is a dependent type or
21527 an expression that is type-dependent or value-dependent. */
21528 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
21529 && (any_dependent_template_arguments_p
21530 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
21532 /* For an alias template specialization, check the arguments both to the
21533 class template and the alias template. */
21534 else if (alias_template_specialization_p (type
)
21535 && (any_dependent_template_arguments_p
21536 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (type
)))))
21539 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
21540 dependent; if the argument of the `typeof' expression is not
21541 type-dependent, then it should already been have resolved. */
21542 if (TREE_CODE (type
) == TYPEOF_TYPE
21543 || TREE_CODE (type
) == DECLTYPE_TYPE
21544 || TREE_CODE (type
) == UNDERLYING_TYPE
)
21547 /* A template argument pack is dependent if any of its packed
21549 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
21551 tree args
= ARGUMENT_PACK_ARGS (type
);
21552 int i
, len
= TREE_VEC_LENGTH (args
);
21553 for (i
= 0; i
< len
; ++i
)
21554 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
21558 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
21559 be template parameters. */
21560 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
21563 /* The standard does not specifically mention types that are local
21564 to template functions or local classes, but they should be
21565 considered dependent too. For example:
21567 template <int I> void f() {
21572 The size of `E' cannot be known until the value of `I' has been
21573 determined. Therefore, `E' must be considered dependent. */
21574 scope
= TYPE_CONTEXT (type
);
21575 if (scope
&& TYPE_P (scope
))
21576 return dependent_type_p (scope
);
21577 /* Don't use type_dependent_expression_p here, as it can lead
21578 to infinite recursion trying to determine whether a lambda
21579 nested in a lambda is dependent (c++/47687). */
21580 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
21581 && DECL_LANG_SPECIFIC (scope
)
21582 && DECL_TEMPLATE_INFO (scope
)
21583 && (any_dependent_template_arguments_p
21584 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
21587 /* Other types are non-dependent. */
21591 /* Returns TRUE if TYPE is dependent, in the sense of
21592 [temp.dep.type]. Note that a NULL type is considered dependent. */
21595 dependent_type_p (tree type
)
21597 /* If there are no template parameters in scope, then there can't be
21598 any dependent types. */
21599 if (!processing_template_decl
)
21601 /* If we are not processing a template, then nobody should be
21602 providing us with a dependent type. */
21604 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
21608 /* If the type is NULL, we have not computed a type for the entity
21609 in question; in that case, the type is dependent. */
21613 /* Erroneous types can be considered non-dependent. */
21614 if (type
== error_mark_node
)
21617 /* If we have not already computed the appropriate value for TYPE,
21619 if (!TYPE_DEPENDENT_P_VALID (type
))
21621 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
21622 TYPE_DEPENDENT_P_VALID (type
) = 1;
21625 return TYPE_DEPENDENT_P (type
);
21628 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
21629 lookup. In other words, a dependent type that is not the current
21633 dependent_scope_p (tree scope
)
21635 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
21636 && !currently_open_class (scope
));
21639 /* T is a SCOPE_REF; return whether we need to consider it
21640 instantiation-dependent so that we can check access at instantiation
21641 time even though we know which member it resolves to. */
21644 instantiation_dependent_scope_ref_p (tree t
)
21646 if (DECL_P (TREE_OPERAND (t
, 1))
21647 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
21648 && accessible_in_template_p (TREE_OPERAND (t
, 0),
21649 TREE_OPERAND (t
, 1)))
21655 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
21656 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
21659 /* Note that this predicate is not appropriate for general expressions;
21660 only constant expressions (that satisfy potential_constant_expression)
21661 can be tested for value dependence. */
21664 value_dependent_expression_p (tree expression
)
21666 if (!processing_template_decl
)
21669 /* A name declared with a dependent type. */
21670 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
21673 switch (TREE_CODE (expression
))
21675 case IDENTIFIER_NODE
:
21676 /* A name that has not been looked up -- must be dependent. */
21679 case TEMPLATE_PARM_INDEX
:
21680 /* A non-type template parm. */
21684 /* A non-type template parm. */
21685 if (DECL_TEMPLATE_PARM_P (expression
))
21687 return value_dependent_expression_p (DECL_INITIAL (expression
));
21690 /* A constant with literal type and is initialized
21691 with an expression that is value-dependent.
21693 Note that a non-dependent parenthesized initializer will have
21694 already been replaced with its constant value, so if we see
21695 a TREE_LIST it must be dependent. */
21696 if (DECL_INITIAL (expression
)
21697 && decl_constant_var_p (expression
)
21698 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
21699 /* cp_finish_decl doesn't fold reference initializers. */
21700 || TREE_CODE (TREE_TYPE (expression
)) == REFERENCE_TYPE
21701 || value_dependent_expression_p (DECL_INITIAL (expression
))))
21705 case DYNAMIC_CAST_EXPR
:
21706 case STATIC_CAST_EXPR
:
21707 case CONST_CAST_EXPR
:
21708 case REINTERPRET_CAST_EXPR
:
21710 /* These expressions are value-dependent if the type to which
21711 the cast occurs is dependent or the expression being casted
21712 is value-dependent. */
21714 tree type
= TREE_TYPE (expression
);
21716 if (dependent_type_p (type
))
21719 /* A functional cast has a list of operands. */
21720 expression
= TREE_OPERAND (expression
, 0);
21723 /* If there are no operands, it must be an expression such
21724 as "int()". This should not happen for aggregate types
21725 because it would form non-constant expressions. */
21726 gcc_assert (cxx_dialect
>= cxx11
21727 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
21732 if (TREE_CODE (expression
) == TREE_LIST
)
21733 return any_value_dependent_elements_p (expression
);
21735 return value_dependent_expression_p (expression
);
21739 if (SIZEOF_EXPR_TYPE_P (expression
))
21740 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
21744 /* A `sizeof' expression is value-dependent if the operand is
21745 type-dependent or is a pack expansion. */
21746 expression
= TREE_OPERAND (expression
, 0);
21747 if (PACK_EXPANSION_P (expression
))
21749 else if (TYPE_P (expression
))
21750 return dependent_type_p (expression
);
21751 return instantiation_dependent_expression_p (expression
);
21753 case AT_ENCODE_EXPR
:
21754 /* An 'encode' expression is value-dependent if the operand is
21756 expression
= TREE_OPERAND (expression
, 0);
21757 return dependent_type_p (expression
);
21759 case NOEXCEPT_EXPR
:
21760 expression
= TREE_OPERAND (expression
, 0);
21761 return instantiation_dependent_expression_p (expression
);
21764 /* All instantiation-dependent expressions should also be considered
21765 value-dependent. */
21766 return instantiation_dependent_scope_ref_p (expression
);
21768 case COMPONENT_REF
:
21769 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
21770 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
21772 case NONTYPE_ARGUMENT_PACK
:
21773 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
21774 is value-dependent. */
21776 tree values
= ARGUMENT_PACK_ARGS (expression
);
21777 int i
, len
= TREE_VEC_LENGTH (values
);
21779 for (i
= 0; i
< len
; ++i
)
21780 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
21788 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
21789 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
21790 || (type2
? dependent_type_p (type2
) : false));
21794 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
21795 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
21798 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
21799 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
21803 tree op
= TREE_OPERAND (expression
, 0);
21804 return (value_dependent_expression_p (op
)
21805 || has_value_dependent_address (op
));
21808 case REQUIRES_EXPR
:
21809 /* Treat all requires-expressions as value-dependent so
21810 we don't try to fold them. */
21814 case VALIDEXPR_EXPR
:
21815 case VALIDTYPE_EXPR
:
21816 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
21820 tree fn
= get_callee_fndecl (expression
);
21822 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
21824 nargs
= call_expr_nargs (expression
);
21825 for (i
= 0; i
< nargs
; ++i
)
21827 tree op
= CALL_EXPR_ARG (expression
, i
);
21828 /* In a call to a constexpr member function, look through the
21829 implicit ADDR_EXPR on the object argument so that it doesn't
21830 cause the call to be considered value-dependent. We also
21831 look through it in potential_constant_expression. */
21832 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
21833 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
21834 && TREE_CODE (op
) == ADDR_EXPR
)
21835 op
= TREE_OPERAND (op
, 0);
21836 if (value_dependent_expression_p (op
))
21842 case TEMPLATE_ID_EXPR
:
21843 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
21845 return type_dependent_expression_p (expression
);
21851 if (dependent_type_p (TREE_TYPE (expression
)))
21853 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
21854 if (value_dependent_expression_p (val
))
21860 /* Treat a GNU statement expression as dependent to avoid crashing
21861 under instantiate_non_dependent_expr; it can't be constant. */
21865 /* A constant expression is value-dependent if any subexpression is
21866 value-dependent. */
21867 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
21869 case tcc_reference
:
21871 case tcc_comparison
:
21873 case tcc_expression
:
21876 int i
, len
= cp_tree_operand_length (expression
);
21878 for (i
= 0; i
< len
; i
++)
21880 tree t
= TREE_OPERAND (expression
, i
);
21882 /* In some cases, some of the operands may be missing.l
21883 (For example, in the case of PREDECREMENT_EXPR, the
21884 amount to increment by may be missing.) That doesn't
21885 make the expression dependent. */
21886 if (t
&& value_dependent_expression_p (t
))
21897 /* The expression is not value-dependent. */
21901 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
21902 [temp.dep.expr]. Note that an expression with no type is
21903 considered dependent. Other parts of the compiler arrange for an
21904 expression with type-dependent subexpressions to have no type, so
21905 this function doesn't have to be fully recursive. */
21908 type_dependent_expression_p (tree expression
)
21910 if (!processing_template_decl
)
21913 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
21916 /* An unresolved name is always dependent. */
21917 if (identifier_p (expression
)
21918 || TREE_CODE (expression
) == USING_DECL
21919 || TREE_CODE (expression
) == INTRODUCED_PARM_DECL
)
21922 /* Some expression forms are never type-dependent. */
21923 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
21924 || TREE_CODE (expression
) == SIZEOF_EXPR
21925 || TREE_CODE (expression
) == ALIGNOF_EXPR
21926 || TREE_CODE (expression
) == AT_ENCODE_EXPR
21927 || TREE_CODE (expression
) == NOEXCEPT_EXPR
21928 || TREE_CODE (expression
) == TRAIT_EXPR
21929 || TREE_CODE (expression
) == TYPEID_EXPR
21930 || TREE_CODE (expression
) == DELETE_EXPR
21931 || TREE_CODE (expression
) == VEC_DELETE_EXPR
21932 || TREE_CODE (expression
) == THROW_EXPR
21933 || TREE_CODE (expression
) == REQUIRES_EXPR
)
21936 /* The types of these expressions depends only on the type to which
21937 the cast occurs. */
21938 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
21939 || TREE_CODE (expression
) == STATIC_CAST_EXPR
21940 || TREE_CODE (expression
) == CONST_CAST_EXPR
21941 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
21942 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
21943 || TREE_CODE (expression
) == CAST_EXPR
)
21944 return dependent_type_p (TREE_TYPE (expression
));
21946 /* The types of these expressions depends only on the type created
21947 by the expression. */
21948 if (TREE_CODE (expression
) == NEW_EXPR
21949 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
21951 /* For NEW_EXPR tree nodes created inside a template, either
21952 the object type itself or a TREE_LIST may appear as the
21954 tree type
= TREE_OPERAND (expression
, 1);
21955 if (TREE_CODE (type
) == TREE_LIST
)
21956 /* This is an array type. We need to check array dimensions
21958 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
21959 || value_dependent_expression_p
21960 (TREE_OPERAND (TREE_VALUE (type
), 1));
21962 return dependent_type_p (type
);
21965 if (TREE_CODE (expression
) == SCOPE_REF
)
21967 tree scope
= TREE_OPERAND (expression
, 0);
21968 tree name
= TREE_OPERAND (expression
, 1);
21970 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
21971 contains an identifier associated by name lookup with one or more
21972 declarations declared with a dependent type, or...a
21973 nested-name-specifier or qualified-id that names a member of an
21974 unknown specialization. */
21975 return (type_dependent_expression_p (name
)
21976 || dependent_scope_p (scope
));
21979 if (TREE_CODE (expression
) == FUNCTION_DECL
21980 && DECL_LANG_SPECIFIC (expression
)
21981 && DECL_TEMPLATE_INFO (expression
)
21982 && (any_dependent_template_arguments_p
21983 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
21986 if (TREE_CODE (expression
) == TEMPLATE_DECL
21987 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
21990 if (TREE_CODE (expression
) == STMT_EXPR
)
21991 expression
= stmt_expr_value_expr (expression
);
21993 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
21998 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
22000 if (type_dependent_expression_p (elt
))
22006 /* A static data member of the current instantiation with incomplete
22007 array type is type-dependent, as the definition and specializations
22008 can have different bounds. */
22009 if (VAR_P (expression
)
22010 && DECL_CLASS_SCOPE_P (expression
)
22011 && dependent_type_p (DECL_CONTEXT (expression
))
22012 && VAR_HAD_UNKNOWN_BOUND (expression
))
22015 /* An array of unknown bound depending on a variadic parameter, eg:
22017 template<typename... Args>
22018 void foo (Args... args)
22020 int arr[] = { args... };
22023 template<int... vals>
22026 int arr[] = { vals... };
22029 If the array has no length and has an initializer, it must be that
22030 we couldn't determine its length in cp_complete_array_type because
22031 it is dependent. */
22032 if (VAR_P (expression
)
22033 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
22034 && !TYPE_DOMAIN (TREE_TYPE (expression
))
22035 && DECL_INITIAL (expression
))
22038 /* A variable template specialization is type-dependent if it has any
22039 dependent template arguments. */
22040 if (VAR_P (expression
)
22041 && DECL_LANG_SPECIFIC (expression
)
22042 && DECL_TEMPLATE_INFO (expression
)
22043 && variable_template_p (DECL_TI_TEMPLATE (expression
)))
22044 return any_dependent_template_arguments_p (DECL_TI_ARGS (expression
));
22046 if (TREE_TYPE (expression
) == unknown_type_node
)
22048 if (TREE_CODE (expression
) == ADDR_EXPR
)
22049 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
22050 if (TREE_CODE (expression
) == COMPONENT_REF
22051 || TREE_CODE (expression
) == OFFSET_REF
)
22053 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
22055 expression
= TREE_OPERAND (expression
, 1);
22056 if (identifier_p (expression
))
22059 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
22060 if (TREE_CODE (expression
) == SCOPE_REF
)
22063 /* Always dependent, on the number of arguments if nothing else. */
22064 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
22067 if (BASELINK_P (expression
))
22069 if (BASELINK_OPTYPE (expression
)
22070 && dependent_type_p (BASELINK_OPTYPE (expression
)))
22072 expression
= BASELINK_FUNCTIONS (expression
);
22075 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
22077 if (any_dependent_template_arguments_p
22078 (TREE_OPERAND (expression
, 1)))
22080 expression
= TREE_OPERAND (expression
, 0);
22082 gcc_assert (TREE_CODE (expression
) == OVERLOAD
22083 || TREE_CODE (expression
) == FUNCTION_DECL
);
22087 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
22089 expression
= OVL_NEXT (expression
);
22094 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
22096 return (dependent_type_p (TREE_TYPE (expression
)));
22099 /* walk_tree callback function for instantiation_dependent_expression_p,
22100 below. Returns non-zero if a dependent subexpression is found. */
22103 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
22108 /* We don't have to worry about decltype currently because decltype
22109 of an instantiation-dependent expr is a dependent type. This
22110 might change depending on the resolution of DR 1172. */
22111 *walk_subtrees
= false;
22114 enum tree_code code
= TREE_CODE (*tp
);
22117 /* Don't treat an argument list as dependent just because it has no
22125 /* A constant with a dependent initializer is dependent. */
22126 if (value_dependent_expression_p (*tp
))
22130 case TEMPLATE_PARM_INDEX
:
22133 /* Handle expressions with type operands. */
22137 case AT_ENCODE_EXPR
:
22139 tree op
= TREE_OPERAND (*tp
, 0);
22140 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
22141 op
= TREE_TYPE (op
);
22144 if (dependent_type_p (op
))
22148 *walk_subtrees
= false;
22157 bool l
= dependent_type_p (TRAIT_EXPR_TYPE1 (*tp
));
22158 bool r
= is_binary_trait (TRAIT_EXPR_KIND (*tp
))
22159 ? dependent_type_p (TRAIT_EXPR_TYPE1 (*tp
)) : false;
22162 *walk_subtrees
= false;
22166 case COMPONENT_REF
:
22167 if (identifier_p (TREE_OPERAND (*tp
, 1)))
22168 /* In a template, finish_class_member_access_expr creates a
22169 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
22170 type-dependent, so that we can check access control at
22171 instantiation time (PR 42277). See also Core issue 1273. */
22176 if (instantiation_dependent_scope_ref_p (*tp
))
22181 /* Treat statement-expressions as dependent. */
22185 case REQUIRES_EXPR
:
22186 /* Treat requires-expressions as dependent. */
22193 if (type_dependent_expression_p (*tp
))
22199 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
22200 sense defined by the ABI:
22202 "An expression is instantiation-dependent if it is type-dependent
22203 or value-dependent, or it has a subexpression that is type-dependent
22204 or value-dependent." */
22207 instantiation_dependent_expression_p (tree expression
)
22211 if (!processing_template_decl
)
22214 if (expression
== error_mark_node
)
22217 result
= cp_walk_tree_without_duplicates (&expression
,
22218 instantiation_dependent_r
, NULL
);
22219 return result
!= NULL_TREE
;
22222 /* Like type_dependent_expression_p, but it also works while not processing
22223 a template definition, i.e. during substitution or mangling. */
22226 type_dependent_expression_p_push (tree expr
)
22229 ++processing_template_decl
;
22230 b
= type_dependent_expression_p (expr
);
22231 --processing_template_decl
;
22235 /* Returns TRUE if ARGS contains a type-dependent expression. */
22238 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
22243 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
22245 if (type_dependent_expression_p (arg
))
22251 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
22252 expressions) contains any type-dependent expressions. */
22255 any_type_dependent_elements_p (const_tree list
)
22257 for (; list
; list
= TREE_CHAIN (list
))
22258 if (type_dependent_expression_p (TREE_VALUE (list
)))
22264 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
22265 expressions) contains any value-dependent expressions. */
22268 any_value_dependent_elements_p (const_tree list
)
22270 for (; list
; list
= TREE_CHAIN (list
))
22271 if (value_dependent_expression_p (TREE_VALUE (list
)))
22277 /* Returns TRUE if the ARG (a template argument) is dependent. */
22280 dependent_template_arg_p (tree arg
)
22282 if (!processing_template_decl
)
22285 /* Assume a template argument that was wrongly written by the user
22286 is dependent. This is consistent with what
22287 any_dependent_template_arguments_p [that calls this function]
22289 if (!arg
|| arg
== error_mark_node
)
22292 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
22293 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
22295 if (TREE_CODE (arg
) == TEMPLATE_DECL
22296 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
22297 return dependent_template_p (arg
);
22298 else if (ARGUMENT_PACK_P (arg
))
22300 tree args
= ARGUMENT_PACK_ARGS (arg
);
22301 int i
, len
= TREE_VEC_LENGTH (args
);
22302 for (i
= 0; i
< len
; ++i
)
22304 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
22310 else if (TYPE_P (arg
))
22311 return dependent_type_p (arg
);
22313 return (type_dependent_expression_p (arg
)
22314 || value_dependent_expression_p (arg
));
22317 /* Returns true if ARGS (a collection of template arguments) contains
22318 any types that require structural equality testing. */
22321 any_template_arguments_need_structural_equality_p (tree args
)
22328 if (args
== error_mark_node
)
22331 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
22333 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
22334 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
22336 tree arg
= TREE_VEC_ELT (level
, j
);
22337 tree packed_args
= NULL_TREE
;
22340 if (ARGUMENT_PACK_P (arg
))
22342 /* Look inside the argument pack. */
22343 packed_args
= ARGUMENT_PACK_ARGS (arg
);
22344 len
= TREE_VEC_LENGTH (packed_args
);
22347 for (k
= 0; k
< len
; ++k
)
22350 arg
= TREE_VEC_ELT (packed_args
, k
);
22352 if (error_operand_p (arg
))
22354 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
22356 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
22358 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
22359 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
22368 /* Returns true if ARGS (a collection of template arguments) contains
22369 any dependent arguments. */
22372 any_dependent_template_arguments_p (const_tree args
)
22379 if (args
== error_mark_node
)
22382 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
22384 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
22385 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
22386 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
22393 /* Returns TRUE if the template TMPL is dependent. */
22396 dependent_template_p (tree tmpl
)
22398 if (TREE_CODE (tmpl
) == OVERLOAD
)
22402 if (dependent_template_p (OVL_CURRENT (tmpl
)))
22404 tmpl
= OVL_NEXT (tmpl
);
22409 /* Template template parameters are dependent. */
22410 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
22411 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
22413 /* So are names that have not been looked up. */
22414 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
22416 /* So are member templates of dependent classes. */
22417 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
22418 return dependent_type_p (DECL_CONTEXT (tmpl
));
22422 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
22425 dependent_template_id_p (tree tmpl
, tree args
)
22427 return (dependent_template_p (tmpl
)
22428 || any_dependent_template_arguments_p (args
));
22431 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
22435 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
22439 if (!processing_template_decl
)
22442 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
22444 tree decl
= TREE_VEC_ELT (declv
, i
);
22445 tree init
= TREE_VEC_ELT (initv
, i
);
22446 tree cond
= TREE_VEC_ELT (condv
, i
);
22447 tree incr
= TREE_VEC_ELT (incrv
, i
);
22449 if (type_dependent_expression_p (decl
))
22452 if (init
&& type_dependent_expression_p (init
))
22455 if (type_dependent_expression_p (cond
))
22458 if (COMPARISON_CLASS_P (cond
)
22459 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
22460 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
22463 if (TREE_CODE (incr
) == MODOP_EXPR
)
22465 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
22466 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
22469 else if (type_dependent_expression_p (incr
))
22471 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
22473 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
22475 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
22477 tree t
= TREE_OPERAND (incr
, 1);
22478 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
22479 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
22488 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
22489 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
22490 no such TYPE can be found. Note that this function peers inside
22491 uninstantiated templates and therefore should be used only in
22492 extremely limited situations. ONLY_CURRENT_P restricts this
22493 peering to the currently open classes hierarchy (which is required
22494 when comparing types). */
22497 resolve_typename_type (tree type
, bool only_current_p
)
22506 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
22508 scope
= TYPE_CONTEXT (type
);
22509 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
22510 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
22511 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
22512 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
22513 identifier of the TYPENAME_TYPE anymore.
22514 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
22515 TYPENAME_TYPE instead, we avoid messing up with a possible
22516 typedef variant case. */
22517 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
22519 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
22520 it first before we can figure out what NAME refers to. */
22521 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
22523 if (TYPENAME_IS_RESOLVING_P (scope
))
22524 /* Given a class template A with a dependent base with nested type C,
22525 typedef typename A::C::C C will land us here, as trying to resolve
22526 the initial A::C leads to the local C typedef, which leads back to
22527 A::C::C. So we break the recursion now. */
22530 scope
= resolve_typename_type (scope
, only_current_p
);
22532 /* If we don't know what SCOPE refers to, then we cannot resolve the
22534 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
22536 /* If the SCOPE is a template type parameter, we have no way of
22537 resolving the name. */
22538 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
22540 /* If the SCOPE is not the current instantiation, there's no reason
22541 to look inside it. */
22542 if (only_current_p
&& !currently_open_class (scope
))
22544 /* If this is a typedef, we don't want to look inside (c++/11987). */
22545 if (typedef_variant_p (type
))
22547 /* If SCOPE isn't the template itself, it will not have a valid
22548 TYPE_FIELDS list. */
22549 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
22550 /* scope is either the template itself or a compatible instantiation
22551 like X<T>, so look up the name in the original template. */
22552 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
22554 /* scope is a partial instantiation, so we can't do the lookup or we
22555 will lose the template arguments. */
22557 /* Enter the SCOPE so that name lookup will be resolved as if we
22558 were in the class definition. In particular, SCOPE will no
22559 longer be considered a dependent type. */
22560 pushed_scope
= push_scope (scope
);
22561 /* Look up the declaration. */
22562 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
22563 tf_warning_or_error
);
22565 result
= NULL_TREE
;
22567 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
22568 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
22571 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type
))
22572 && TREE_CODE (decl
) == TYPE_DECL
)
22574 result
= TREE_TYPE (decl
);
22575 if (result
== error_mark_node
)
22576 result
= NULL_TREE
;
22578 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
22579 && DECL_CLASS_TEMPLATE_P (decl
))
22583 /* Obtain the template and the arguments. */
22584 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
22585 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
22586 /* Instantiate the template. */
22587 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
22588 /*entering_scope=*/0,
22589 tf_error
| tf_user
);
22590 if (result
== error_mark_node
)
22591 result
= NULL_TREE
;
22594 /* Leave the SCOPE. */
22596 pop_scope (pushed_scope
);
22598 /* If we failed to resolve it, return the original typename. */
22602 /* If lookup found a typename type, resolve that too. */
22603 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
22605 /* Ill-formed programs can cause infinite recursion here, so we
22606 must catch that. */
22607 TYPENAME_IS_RESOLVING_P (type
) = 1;
22608 result
= resolve_typename_type (result
, only_current_p
);
22609 TYPENAME_IS_RESOLVING_P (type
) = 0;
22612 /* Qualify the resulting type. */
22613 quals
= cp_type_quals (type
);
22615 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
22620 /* EXPR is an expression which is not type-dependent. Return a proxy
22621 for EXPR that can be used to compute the types of larger
22622 expressions containing EXPR. */
22625 build_non_dependent_expr (tree expr
)
22629 #ifdef ENABLE_CHECKING
22630 /* Try to get a constant value for all non-dependent expressions in
22631 order to expose bugs in *_dependent_expression_p and constexpr. */
22632 if (cxx_dialect
>= cxx11
)
22633 fold_non_dependent_expr (expr
);
22636 /* Preserve OVERLOADs; the functions must be available to resolve
22639 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
22640 inner_expr
= stmt_expr_value_expr (inner_expr
);
22641 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
22642 inner_expr
= TREE_OPERAND (inner_expr
, 0);
22643 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
22644 inner_expr
= TREE_OPERAND (inner_expr
, 1);
22645 if (is_overloaded_fn (inner_expr
)
22646 || TREE_CODE (inner_expr
) == OFFSET_REF
)
22648 /* There is no need to return a proxy for a variable. */
22651 /* Preserve string constants; conversions from string constants to
22652 "char *" are allowed, even though normally a "const char *"
22653 cannot be used to initialize a "char *". */
22654 if (TREE_CODE (expr
) == STRING_CST
)
22656 /* Preserve void and arithmetic constants, as an optimization -- there is no
22657 reason to create a new node. */
22658 if (TREE_CODE (expr
) == VOID_CST
22659 || TREE_CODE (expr
) == INTEGER_CST
22660 || TREE_CODE (expr
) == REAL_CST
)
22662 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
22663 There is at least one place where we want to know that a
22664 particular expression is a throw-expression: when checking a ?:
22665 expression, there are special rules if the second or third
22666 argument is a throw-expression. */
22667 if (TREE_CODE (expr
) == THROW_EXPR
)
22670 /* Don't wrap an initializer list, we need to be able to look inside. */
22671 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
22674 /* Don't wrap a dummy object, we need to be able to test for it. */
22675 if (is_dummy_object (expr
))
22678 if (TREE_CODE (expr
) == COND_EXPR
)
22679 return build3 (COND_EXPR
,
22681 TREE_OPERAND (expr
, 0),
22682 (TREE_OPERAND (expr
, 1)
22683 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
22684 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
22685 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
22686 if (TREE_CODE (expr
) == COMPOUND_EXPR
22687 && !COMPOUND_EXPR_OVERLOADED (expr
))
22688 return build2 (COMPOUND_EXPR
,
22690 TREE_OPERAND (expr
, 0),
22691 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
22693 /* If the type is unknown, it can't really be non-dependent */
22694 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
22696 /* Otherwise, build a NON_DEPENDENT_EXPR. */
22697 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
22700 /* ARGS is a vector of expressions as arguments to a function call.
22701 Replace the arguments with equivalent non-dependent expressions.
22702 This modifies ARGS in place. */
22705 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
22710 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
22712 tree newarg
= build_non_dependent_expr (arg
);
22714 (*args
)[ix
] = newarg
;
22718 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
22719 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
22723 make_auto_1 (tree name
)
22725 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
22726 TYPE_NAME (au
) = build_decl (input_location
,
22727 TYPE_DECL
, name
, au
);
22728 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
22729 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
22730 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
22731 TYPE_NAME (au
), NULL_TREE
);
22732 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
22733 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
22734 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
22740 make_decltype_auto (void)
22742 return make_auto_1 (get_identifier ("decltype(auto)"));
22748 return make_auto_1 (get_identifier ("auto"));
22751 /* Given type ARG, return std::initializer_list<ARG>. */
22756 tree std_init_list
= namespace_binding
22757 (get_identifier ("initializer_list"), std_node
);
22759 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
22761 error ("deducing from brace-enclosed initializer list requires "
22762 "#include <initializer_list>");
22763 return error_mark_node
;
22765 argvec
= make_tree_vec (1);
22766 TREE_VEC_ELT (argvec
, 0) = arg
;
22767 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
22768 NULL_TREE
, 0, tf_warning_or_error
);
22771 /* Replace auto in TYPE with std::initializer_list<auto>. */
22774 listify_autos (tree type
, tree auto_node
)
22776 tree init_auto
= listify (auto_node
);
22777 tree argvec
= make_tree_vec (1);
22778 TREE_VEC_ELT (argvec
, 0) = init_auto
;
22779 if (processing_template_decl
)
22780 argvec
= add_to_template_args (current_template_args (), argvec
);
22781 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
22784 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
22785 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
22788 do_auto_deduction (tree type
, tree init
, tree auto_node
)
22792 if (init
== error_mark_node
)
22793 return error_mark_node
;
22795 if (type_dependent_expression_p (init
))
22796 /* Defining a subset of type-dependent expressions that we can deduce
22797 from ahead of time isn't worth the trouble. */
22800 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
22801 with either a new invented type template parameter U or, if the
22802 initializer is a braced-init-list (8.5.4), with
22803 std::initializer_list<U>. */
22804 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
22806 if (!DIRECT_LIST_INIT_P (init
))
22807 type
= listify_autos (type
, auto_node
);
22808 else if (CONSTRUCTOR_NELTS (init
) == 1)
22809 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
22812 if (permerror (input_location
, "direct-list-initialization of "
22813 "%<auto%> requires exactly one element"))
22814 inform (input_location
,
22815 "for deduction to %<std::initializer_list%>, use copy-"
22816 "list-initialization (i.e. add %<=%> before the %<{%>)");
22817 type
= listify_autos (type
, auto_node
);
22821 init
= resolve_nondeduced_context (init
);
22823 targs
= make_tree_vec (1);
22824 if (AUTO_IS_DECLTYPE (auto_node
))
22826 bool id
= (DECL_P (init
) || (TREE_CODE (init
) == COMPONENT_REF
22827 && !REF_PARENTHESIZED_P (init
)));
22828 TREE_VEC_ELT (targs
, 0)
22829 = finish_decltype_type (init
, id
, tf_warning_or_error
);
22830 if (type
!= auto_node
)
22832 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
22833 return error_mark_node
;
22838 tree parms
= build_tree_list (NULL_TREE
, type
);
22839 tree tparms
= make_tree_vec (1);
22842 TREE_VEC_ELT (tparms
, 0)
22843 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
22844 val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
22845 DEDUCE_CALL
, LOOKUP_NORMAL
,
22846 NULL
, /*explain_p=*/false);
22849 if (processing_template_decl
)
22850 /* Try again at instantiation time. */
22852 if (type
&& type
!= error_mark_node
)
22853 /* If type is error_mark_node a diagnostic must have been
22854 emitted by now. Also, having a mention to '<type error>'
22855 in the diagnostic is not really useful to the user. */
22857 if (cfun
&& auto_node
== current_function_auto_return_pattern
22858 && LAMBDA_FUNCTION_P (current_function_decl
))
22859 error ("unable to deduce lambda return type from %qE", init
);
22861 error ("unable to deduce %qT from %qE", type
, init
);
22863 return error_mark_node
;
22867 /* If the list of declarators contains more than one declarator, the type
22868 of each declared variable is determined as described above. If the
22869 type deduced for the template parameter U is not the same in each
22870 deduction, the program is ill-formed. */
22871 if (TREE_TYPE (auto_node
)
22872 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
22874 if (cfun
&& auto_node
== current_function_auto_return_pattern
22875 && LAMBDA_FUNCTION_P (current_function_decl
))
22876 error ("inconsistent types %qT and %qT deduced for "
22877 "lambda return type", TREE_TYPE (auto_node
),
22878 TREE_VEC_ELT (targs
, 0));
22880 error ("inconsistent deduction for %qT: %qT and then %qT",
22881 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
22882 return error_mark_node
;
22884 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
22886 if (processing_template_decl
)
22887 targs
= add_to_template_args (current_template_args (), targs
);
22888 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
22891 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
22895 splice_late_return_type (tree type
, tree late_return_type
)
22899 if (late_return_type
== NULL_TREE
)
22901 argvec
= make_tree_vec (1);
22902 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
22903 if (processing_template_parmlist
)
22904 /* For a late-specified return type in a template type-parameter, we
22905 need to add a dummy argument level for its parmlist. */
22906 argvec
= add_to_template_args
22907 (make_tree_vec (processing_template_parmlist
), argvec
);
22908 if (current_template_parms
)
22909 argvec
= add_to_template_args (current_template_args (), argvec
);
22910 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
22913 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
22914 'decltype(auto)'. */
22917 is_auto (const_tree type
)
22919 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
22920 && (TYPE_IDENTIFIER (type
) == get_identifier ("auto")
22921 || TYPE_IDENTIFIER (type
) == get_identifier ("decltype(auto)")))
22927 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
22928 a use of `auto'. Returns NULL_TREE otherwise. */
22931 type_uses_auto (tree type
)
22933 return find_type_usage (type
, is_auto
);
22936 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
22937 'decltype(auto)' or a concept. */
22940 is_auto_or_concept (const_tree type
)
22942 return is_auto (type
); // or concept
22945 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
22946 a concept identifier) iff TYPE contains a use of a generic type. Returns
22947 NULL_TREE otherwise. */
22950 type_uses_auto_or_concept (tree type
)
22952 return find_type_usage (type
, is_auto_or_concept
);
22956 /* For a given template T, return the vector of typedefs referenced
22957 in T for which access check is needed at T instantiation time.
22958 T is either a FUNCTION_DECL or a RECORD_TYPE.
22959 Those typedefs were added to T by the function
22960 append_type_to_template_for_access_check. */
22962 vec
<qualified_typedef_usage_t
, va_gc
> *
22963 get_types_needing_access_check (tree t
)
22966 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
22968 if (!t
|| t
== error_mark_node
)
22971 if (!(ti
= get_template_info (t
)))
22974 if (CLASS_TYPE_P (t
)
22975 || TREE_CODE (t
) == FUNCTION_DECL
)
22977 if (!TI_TEMPLATE (ti
))
22980 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
22986 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
22987 tied to T. That list of typedefs will be access checked at
22988 T instantiation time.
22989 T is either a FUNCTION_DECL or a RECORD_TYPE.
22990 TYPE_DECL is a TYPE_DECL node representing a typedef.
22991 SCOPE is the scope through which TYPE_DECL is accessed.
22992 LOCATION is the location of the usage point of TYPE_DECL.
22994 This function is a subroutine of
22995 append_type_to_template_for_access_check. */
22998 append_type_to_template_for_access_check_1 (tree t
,
23001 location_t location
)
23003 qualified_typedef_usage_t typedef_usage
;
23006 if (!t
|| t
== error_mark_node
)
23009 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
23010 || CLASS_TYPE_P (t
))
23012 && TREE_CODE (type_decl
) == TYPE_DECL
23015 if (!(ti
= get_template_info (t
)))
23018 gcc_assert (TI_TEMPLATE (ti
));
23020 typedef_usage
.typedef_decl
= type_decl
;
23021 typedef_usage
.context
= scope
;
23022 typedef_usage
.locus
= location
;
23024 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
23027 /* Append TYPE_DECL to the template TEMPL.
23028 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
23029 At TEMPL instanciation time, TYPE_DECL will be checked to see
23030 if it can be accessed through SCOPE.
23031 LOCATION is the location of the usage point of TYPE_DECL.
23033 e.g. consider the following code snippet:
23040 template<class U> struct S
23042 C::myint mi; // <-- usage point of the typedef C::myint
23047 At S<char> instantiation time, we need to check the access of C::myint
23048 In other words, we need to check the access of the myint typedef through
23049 the C scope. For that purpose, this function will add the myint typedef
23050 and the scope C through which its being accessed to a list of typedefs
23051 tied to the template S. That list will be walked at template instantiation
23052 time and access check performed on each typedefs it contains.
23053 Note that this particular code snippet should yield an error because
23054 myint is private to C. */
23057 append_type_to_template_for_access_check (tree templ
,
23060 location_t location
)
23062 qualified_typedef_usage_t
*iter
;
23065 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
23067 /* Make sure we don't append the type to the template twice. */
23068 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
23069 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
23072 append_type_to_template_for_access_check_1 (templ
, type_decl
,
23076 /* Convert the generic type parameters in PARM that match the types given in the
23077 range [START_IDX, END_IDX) from the current_template_parms into generic type
23081 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
23083 tree current
= current_template_parms
;
23084 int depth
= TMPL_PARMS_DEPTH (current
);
23085 current
= INNERMOST_TEMPLATE_PARMS (current
);
23086 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
23088 for (int i
= 0; i
< start_idx
; ++i
)
23089 TREE_VEC_ELT (replacement
, i
)
23090 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
23092 for (int i
= start_idx
; i
< end_idx
; ++i
)
23094 /* Create a distinct parameter pack type from the current parm and add it
23095 to the replacement args to tsubst below into the generic function
23098 tree o
= TREE_TYPE (TREE_VALUE
23099 (TREE_VEC_ELT (current
, i
)));
23100 tree t
= copy_type (o
);
23101 TEMPLATE_TYPE_PARM_INDEX (t
)
23102 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
23104 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
23105 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
23106 TYPE_MAIN_VARIANT (t
) = t
;
23107 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
23108 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
23109 TREE_VEC_ELT (replacement
, i
) = t
;
23110 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
23113 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
23114 TREE_VEC_ELT (replacement
, i
)
23115 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
23117 /* If there are more levels then build up the replacement with the outer
23120 replacement
= add_to_template_args (template_parms_to_args
23121 (TREE_CHAIN (current_template_parms
)),
23124 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
23127 /* Entries in the decl_constraint hash table. */
23128 struct GTY((for_user
)) constr_entry
23134 /* Hashing function and equality fo constraint entries.. */
23135 struct constr_hasher
: ggc_hasher
<constr_entry
*>
23137 static hashval_t
hash (constr_entry
*e
)
23139 return (hashval_t
)DECL_UID (e
->decl
);
23142 static bool equal (constr_entry
*e1
, constr_entry
*e2
)
23144 return e1
->decl
== e2
->decl
;
23149 /* A mapping from declarations to constraint information. Note that
23150 both templates and their underlying declarations are mapped to the
23151 same constraint information.
23153 FIXME: This is defined in pt.c because it's garbage collection
23154 code is not being generated for constraint.cc. */
23155 static GTY (()) hash_table
<constr_hasher
> *decl_constraints
;
23157 /* Returns true iff cinfo contains a valid set of constraints.
23158 This is the case when the associated requirements have been
23159 successfully decomposed into lists of atomic constraints.
23160 That is, when the saved assumptions are not error_mark_node. */
23162 valid_constraints_p (tree cinfo
)
23164 gcc_assert (cinfo
);
23165 return CI_ASSUMPTIONS (cinfo
) != error_mark_node
;
23168 /* Returns the template constraints of declaration T. If T is not
23169 constrained, return NULL_TREE. Note that T must be non-null. */
23171 get_constraints (tree t
)
23173 gcc_assert (DECL_P (t
));
23174 if (TREE_CODE (t
) == TEMPLATE_DECL
)
23175 t
= DECL_TEMPLATE_RESULT (t
);
23176 constr_entry elt
= { t
, NULL_TREE
};
23177 constr_entry
* found
= decl_constraints
->find (&elt
);
23184 /* Associate the given constraint information CI with the declaration
23185 T. If T is a template, then the constraints are associated with
23186 its underlying declaration. Don't build associations if CI is
23189 set_constraints (tree t
, tree ci
)
23194 if (TREE_CODE (t
) == TEMPLATE_DECL
)
23195 t
= DECL_TEMPLATE_RESULT (t
);
23196 gcc_assert (!get_constraints (t
));
23197 constr_entry elt
= {t
, ci
};
23198 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, INSERT
);
23199 constr_entry
* entry
= ggc_alloc
<constr_entry
> ();
23204 /* Remove the associated constraints of the declaration T.
23205 If there are no constraints associated with T, then
23206 return NULL_TREE. */
23208 remove_constraints (tree t
)
23210 gcc_assert (DECL_P (t
));
23211 if (TREE_CODE (t
) == TEMPLATE_DECL
)
23212 t
= DECL_TEMPLATE_RESULT (t
);
23214 constr_entry elt
= {t
, NULL_TREE
};
23215 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, NO_INSERT
);
23217 decl_constraints
->clear_slot(slot
);
23220 /* Set up the hash table for constraint association. */
23223 init_constraint_processing (void)
23225 decl_constraints
= hash_table
<constr_hasher
>::create_ggc(37);
23228 /* Set up the hash tables for template instantiations. */
23231 init_template_processing (void)
23233 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
23234 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
23237 /* Print stats about the template hash tables for -fstats. */
23240 print_template_statistics (void)
23242 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
23243 "%f collisions\n", (long) decl_specializations
->size (),
23244 (long) decl_specializations
->elements (),
23245 decl_specializations
->collisions ());
23246 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
23247 "%f collisions\n", (long) type_specializations
->size (),
23248 (long) type_specializations
->elements (),
23249 type_specializations
->collisions ());
23252 #include "gt-cp-pt.h"