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"
33 #include "stringpool.h"
36 #include "stor-layout.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "cp-objcp-common.h"
43 #include "tree-inline.h"
47 #include "tree-iterator.h"
48 #include "type-utils.h"
51 /* The type of functions taking a tree, and some additional data, and
53 typedef int (*tree_fn_t
) (tree
, void*);
55 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
56 instantiations have been deferred, either because their definitions
57 were not yet available, or because we were putting off doing the work. */
58 struct GTY ((chain_next ("%h.next"))) pending_template
{
59 struct pending_template
*next
;
60 struct tinst_level
*tinst
;
63 static GTY(()) struct pending_template
*pending_templates
;
64 static GTY(()) struct pending_template
*last_pending_template
;
66 int processing_template_parmlist
;
67 static int template_header_count
;
69 static GTY(()) tree saved_trees
;
70 static vec
<int> inline_parm_levels
;
72 static GTY(()) struct tinst_level
*current_tinst_level
;
74 static GTY(()) tree saved_access_scope
;
76 /* Live only within one (recursive) call to tsubst_expr. We use
77 this to pass the statement expression node from the STMT_EXPR
78 to the EXPR_STMT that is its result. */
79 static tree cur_stmt_expr
;
81 /* True if we've recursed into fn_type_unification too many times. */
82 static bool excessive_deduction_depth
;
84 struct GTY((for_user
)) spec_entry
91 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
93 static hashval_t
hash (spec_entry
*);
94 static bool equal (spec_entry
*, spec_entry
*);
97 static GTY (()) hash_table
<spec_hasher
> *decl_specializations
;
99 static GTY (()) hash_table
<spec_hasher
> *type_specializations
;
101 /* Contains canonical template parameter types. The vector is indexed by
102 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
103 TREE_LIST, whose TREE_VALUEs contain the canonical template
104 parameters of various types and levels. */
105 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
107 #define UNIFY_ALLOW_NONE 0
108 #define UNIFY_ALLOW_MORE_CV_QUAL 1
109 #define UNIFY_ALLOW_LESS_CV_QUAL 2
110 #define UNIFY_ALLOW_DERIVED 4
111 #define UNIFY_ALLOW_INTEGER 8
112 #define UNIFY_ALLOW_OUTER_LEVEL 16
113 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
114 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
116 enum template_base_result
{
118 tbr_ambiguous_baseclass
,
122 static void push_access_scope (tree
);
123 static void pop_access_scope (tree
);
124 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
125 unification_kind_t
, int,
127 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
128 unification_kind_t
, int, bool, bool);
129 static int unify (tree
, tree
, tree
, tree
, int, bool);
130 static void add_pending_template (tree
);
131 static tree
reopen_tinst_level (struct tinst_level
*);
132 static tree
tsubst_initializer_list (tree
, tree
);
133 static tree
get_partial_spec_bindings (tree
, tree
, tree
, tree
);
134 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
136 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
138 static void tsubst_enum (tree
, tree
, tree
);
139 static tree
add_to_template_args (tree
, tree
);
140 static tree
add_outermost_template_args (tree
, tree
);
141 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
142 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
144 static int type_unification_real (tree
, tree
, tree
, const tree
*,
145 unsigned int, int, unification_kind_t
, int,
146 vec
<deferred_access_check
, va_gc
> **,
148 static void note_template_header (int);
149 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
150 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
151 static tree
convert_template_argument (tree
, tree
, tree
,
152 tsubst_flags_t
, int, tree
);
153 static int for_each_template_parm (tree
, tree_fn_t
, void*,
154 hash_set
<tree
> *, bool);
155 static tree
expand_template_argument_pack (tree
);
156 static tree
build_template_parm_index (int, int, int, tree
, tree
);
157 static bool inline_needs_template_parms (tree
, bool);
158 static void push_inline_template_parms_recursive (tree
, int);
159 static tree
retrieve_local_specialization (tree
);
160 static void register_local_specialization (tree
, tree
);
161 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
162 static int mark_template_parm (tree
, void *);
163 static int template_parm_this_level_p (tree
, void *);
164 static tree
tsubst_friend_function (tree
, tree
);
165 static tree
tsubst_friend_class (tree
, tree
);
166 static int can_complete_type_without_circularity (tree
);
167 static tree
get_bindings (tree
, tree
, tree
, bool);
168 static int template_decl_level (tree
);
169 static int check_cv_quals_for_unify (int, tree
, tree
);
170 static void template_parm_level_and_index (tree
, int*, int*);
171 static int unify_pack_expansion (tree
, tree
, tree
,
172 tree
, unification_kind_t
, bool, bool);
173 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
174 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
175 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
176 static void regenerate_decl_from_template (tree
, tree
);
177 static tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
178 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
179 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
180 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
181 static bool check_specialization_scope (void);
182 static tree
process_partial_specialization (tree
);
183 static void set_current_access_from_decl (tree
);
184 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
186 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
187 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
189 static bool template_template_parm_bindings_ok_p (tree
, tree
);
190 static int template_args_equal (tree
, tree
);
191 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
192 static tree
for_each_template_parm_r (tree
*, int *, void *);
193 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
194 static void copy_default_args_to_explicit_spec (tree
);
195 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
196 static bool dependent_template_arg_p (tree
);
197 static bool any_template_arguments_need_structural_equality_p (tree
);
198 static bool dependent_type_p_r (tree
);
199 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
, bool);
200 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
201 static tree
tsubst_pack_expansion (tree
, tree
, tsubst_flags_t
, tree
);
202 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
203 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
204 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
206 static tree
listify (tree
);
207 static tree
listify_autos (tree
, tree
);
208 static tree
template_parm_to_arg (tree t
);
209 static tree
current_template_args (void);
210 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
211 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
212 static bool complex_alias_template_p (const_tree tmpl
);
214 /* Make the current scope suitable for access checking when we are
215 processing T. T can be FUNCTION_DECL for instantiated function
216 template, VAR_DECL for static member variable, or TYPE_DECL for
217 alias template (needed by instantiate_decl). */
220 push_access_scope (tree t
)
222 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
223 || TREE_CODE (t
) == TYPE_DECL
);
225 if (DECL_FRIEND_CONTEXT (t
))
226 push_nested_class (DECL_FRIEND_CONTEXT (t
));
227 else if (DECL_CLASS_SCOPE_P (t
))
228 push_nested_class (DECL_CONTEXT (t
));
230 push_to_top_level ();
232 if (TREE_CODE (t
) == FUNCTION_DECL
)
234 saved_access_scope
= tree_cons
235 (NULL_TREE
, current_function_decl
, saved_access_scope
);
236 current_function_decl
= t
;
240 /* Restore the scope set up by push_access_scope. T is the node we
244 pop_access_scope (tree t
)
246 if (TREE_CODE (t
) == FUNCTION_DECL
)
248 current_function_decl
= TREE_VALUE (saved_access_scope
);
249 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
252 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
255 pop_from_top_level ();
258 /* Do any processing required when DECL (a member template
259 declaration) is finished. Returns the TEMPLATE_DECL corresponding
260 to DECL, unless it is a specialization, in which case the DECL
261 itself is returned. */
264 finish_member_template_decl (tree decl
)
266 if (decl
== error_mark_node
)
267 return error_mark_node
;
269 gcc_assert (DECL_P (decl
));
271 if (TREE_CODE (decl
) == TYPE_DECL
)
275 type
= TREE_TYPE (decl
);
276 if (type
== error_mark_node
)
277 return error_mark_node
;
278 if (MAYBE_CLASS_TYPE_P (type
)
279 && CLASSTYPE_TEMPLATE_INFO (type
)
280 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
282 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
283 check_member_template (tmpl
);
288 else if (TREE_CODE (decl
) == FIELD_DECL
)
289 error ("data member %qD cannot be a member template", decl
);
290 else if (DECL_TEMPLATE_INFO (decl
))
292 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
294 check_member_template (DECL_TI_TEMPLATE (decl
));
295 return DECL_TI_TEMPLATE (decl
);
301 error ("invalid member template declaration %qD", decl
);
303 return error_mark_node
;
306 /* Create a template info node. */
309 build_template_info (tree template_decl
, tree template_args
)
311 tree result
= make_node (TEMPLATE_INFO
);
312 TI_TEMPLATE (result
) = template_decl
;
313 TI_ARGS (result
) = template_args
;
317 /* Return the template info node corresponding to T, whatever T is. */
320 get_template_info (const_tree t
)
322 tree tinfo
= NULL_TREE
;
324 if (!t
|| t
== error_mark_node
)
327 if (TREE_CODE (t
) == NAMESPACE_DECL
)
330 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
331 tinfo
= DECL_TEMPLATE_INFO (t
);
333 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
336 if (OVERLOAD_TYPE_P (t
))
337 tinfo
= TYPE_TEMPLATE_INFO (t
);
338 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
339 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
344 /* Returns the template nesting level of the indicated class TYPE.
354 A<T>::B<U> has depth two, while A<T> has depth one.
355 Both A<T>::B<int> and A<int>::B<U> have depth one, if
356 they are instantiations, not specializations.
358 This function is guaranteed to return 0 if passed NULL_TREE so
359 that, for example, `template_class_depth (current_class_type)' is
363 template_class_depth (tree type
)
368 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
369 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
370 ? CP_DECL_CONTEXT (type
) : CP_TYPE_CONTEXT (type
))
372 tree tinfo
= get_template_info (type
);
374 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
375 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
382 /* Subroutine of maybe_begin_member_template_processing.
383 Returns true if processing DECL needs us to push template parms. */
386 inline_needs_template_parms (tree decl
, bool nsdmi
)
388 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
391 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
392 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
395 /* Subroutine of maybe_begin_member_template_processing.
396 Push the template parms in PARMS, starting from LEVELS steps into the
397 chain, and ending at the beginning, since template parms are listed
401 push_inline_template_parms_recursive (tree parmlist
, int levels
)
403 tree parms
= TREE_VALUE (parmlist
);
407 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
409 ++processing_template_decl
;
410 current_template_parms
411 = tree_cons (size_int (processing_template_decl
),
412 parms
, current_template_parms
);
413 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
415 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
417 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
419 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
421 if (error_operand_p (parm
))
424 gcc_assert (DECL_P (parm
));
426 switch (TREE_CODE (parm
))
435 /* Make a CONST_DECL as is done in process_template_parm.
436 It is ugly that we recreate this here; the original
437 version built in process_template_parm is no longer
439 tree decl
= build_decl (DECL_SOURCE_LOCATION (parm
),
440 CONST_DECL
, DECL_NAME (parm
),
442 DECL_ARTIFICIAL (decl
) = 1;
443 TREE_CONSTANT (decl
) = 1;
444 TREE_READONLY (decl
) = 1;
445 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
446 SET_DECL_TEMPLATE_PARM_P (decl
);
457 /* Restore the template parameter context for a member template, a
458 friend template defined in a class definition, or a non-template
459 member of template class. */
462 maybe_begin_member_template_processing (tree decl
)
466 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
470 tree ctx
= DECL_CONTEXT (decl
);
471 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
472 /* Disregard full specializations (c++/60999). */
473 && uses_template_parms (ctx
)
474 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
477 if (inline_needs_template_parms (decl
, nsdmi
))
479 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
480 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
482 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
485 parms
= TREE_CHAIN (parms
);
488 push_inline_template_parms_recursive (parms
, levels
);
491 /* Remember how many levels of template parameters we pushed so that
492 we can pop them later. */
493 inline_parm_levels
.safe_push (levels
);
496 /* Undo the effects of maybe_begin_member_template_processing. */
499 maybe_end_member_template_processing (void)
504 if (inline_parm_levels
.length () == 0)
507 last
= inline_parm_levels
.pop ();
508 for (i
= 0; i
< last
; ++i
)
510 --processing_template_decl
;
511 current_template_parms
= TREE_CHAIN (current_template_parms
);
516 /* Return a new template argument vector which contains all of ARGS,
517 but has as its innermost set of arguments the EXTRA_ARGS. */
520 add_to_template_args (tree args
, tree extra_args
)
527 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
530 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
531 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
533 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
534 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
536 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
537 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
542 /* Like add_to_template_args, but only the outermost ARGS are added to
543 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
544 (EXTRA_ARGS) levels are added. This function is used to combine
545 the template arguments from a partial instantiation with the
546 template arguments used to attain the full instantiation from the
547 partial instantiation. */
550 add_outermost_template_args (tree args
, tree extra_args
)
554 /* If there are more levels of EXTRA_ARGS than there are ARGS,
555 something very fishy is going on. */
556 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
558 /* If *all* the new arguments will be the EXTRA_ARGS, just return
560 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
563 /* For the moment, we make ARGS look like it contains fewer levels. */
564 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
566 new_args
= add_to_template_args (args
, extra_args
);
568 /* Now, we restore ARGS to its full dimensions. */
569 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
574 /* Return the N levels of innermost template arguments from the ARGS. */
577 get_innermost_template_args (tree args
, int n
)
585 /* If N is 1, just return the innermost set of template arguments. */
587 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
589 /* If we're not removing anything, just return the arguments we were
591 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
592 gcc_assert (extra_levels
>= 0);
593 if (extra_levels
== 0)
596 /* Make a new set of arguments, not containing the outer arguments. */
597 new_args
= make_tree_vec (n
);
598 for (i
= 1; i
<= n
; ++i
)
599 SET_TMPL_ARGS_LEVEL (new_args
, i
,
600 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
605 /* The inverse of get_innermost_template_args: Return all but the innermost
606 EXTRA_LEVELS levels of template arguments from the ARGS. */
609 strip_innermost_template_args (tree args
, int extra_levels
)
612 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
617 /* If N is 1, just return the outermost set of template arguments. */
619 return TMPL_ARGS_LEVEL (args
, 1);
621 /* If we're not removing anything, just return the arguments we were
623 gcc_assert (extra_levels
>= 0);
624 if (extra_levels
== 0)
627 /* Make a new set of arguments, not containing the inner arguments. */
628 new_args
= make_tree_vec (n
);
629 for (i
= 1; i
<= n
; ++i
)
630 SET_TMPL_ARGS_LEVEL (new_args
, i
,
631 TMPL_ARGS_LEVEL (args
, i
));
636 /* We've got a template header coming up; push to a new level for storing
640 begin_template_parm_list (void)
642 /* We use a non-tag-transparent scope here, which causes pushtag to
643 put tags in this scope, rather than in the enclosing class or
644 namespace scope. This is the right thing, since we want
645 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
646 global template class, push_template_decl handles putting the
647 TEMPLATE_DECL into top-level scope. For a nested template class,
650 template <class T> struct S1 {
651 template <class T> struct S2 {};
654 pushtag contains special code to call pushdecl_with_scope on the
655 TEMPLATE_DECL for S2. */
656 begin_scope (sk_template_parms
, NULL
);
657 ++processing_template_decl
;
658 ++processing_template_parmlist
;
659 note_template_header (0);
661 /* Add a dummy parameter level while we process the parameter list. */
662 current_template_parms
663 = tree_cons (size_int (processing_template_decl
),
665 current_template_parms
);
668 /* This routine is called when a specialization is declared. If it is
669 invalid to declare a specialization here, an error is reported and
670 false is returned, otherwise this routine will return true. */
673 check_specialization_scope (void)
675 tree scope
= current_scope ();
679 An explicit specialization shall be declared in the namespace of
680 which the template is a member, or, for member templates, in the
681 namespace of which the enclosing class or enclosing class
682 template is a member. An explicit specialization of a member
683 function, member class or static data member of a class template
684 shall be declared in the namespace of which the class template
686 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
688 error ("explicit specialization in non-namespace scope %qD", scope
);
694 In an explicit specialization declaration for a member of a class
695 template or a member template that appears in namespace scope,
696 the member template and some of its enclosing class templates may
697 remain unspecialized, except that the declaration shall not
698 explicitly specialize a class member template if its enclosing
699 class templates are not explicitly specialized as well. */
700 if (current_template_parms
)
702 error ("enclosing class templates are not explicitly specialized");
709 /* We've just seen template <>. */
712 begin_specialization (void)
714 begin_scope (sk_template_spec
, NULL
);
715 note_template_header (1);
716 return check_specialization_scope ();
719 /* Called at then end of processing a declaration preceded by
723 end_specialization (void)
726 reset_specialization ();
729 /* Any template <>'s that we have seen thus far are not referring to a
730 function specialization. */
733 reset_specialization (void)
735 processing_specialization
= 0;
736 template_header_count
= 0;
739 /* We've just seen a template header. If SPECIALIZATION is nonzero,
740 it was of the form template <>. */
743 note_template_header (int specialization
)
745 processing_specialization
= specialization
;
746 template_header_count
++;
749 /* We're beginning an explicit instantiation. */
752 begin_explicit_instantiation (void)
754 gcc_assert (!processing_explicit_instantiation
);
755 processing_explicit_instantiation
= true;
760 end_explicit_instantiation (void)
762 gcc_assert (processing_explicit_instantiation
);
763 processing_explicit_instantiation
= false;
766 /* An explicit specialization or partial specialization of TMPL is being
767 declared. Check that the namespace in which the specialization is
768 occurring is permissible. Returns false iff it is invalid to
769 specialize TMPL in the current namespace. */
772 check_specialization_namespace (tree tmpl
)
774 tree tpl_ns
= decl_namespace_context (tmpl
);
778 An explicit specialization shall be declared in the namespace of
779 which the template is a member, or, for member templates, in the
780 namespace of which the enclosing class or enclosing class
781 template is a member. An explicit specialization of a member
782 function, member class or static data member of a class template
783 shall be declared in the namespace of which the class template is
785 if (current_scope() != DECL_CONTEXT (tmpl
)
786 && !at_namespace_scope_p ())
788 error ("specialization of %qD must appear at namespace scope", tmpl
);
791 if (is_associated_namespace (current_namespace
, tpl_ns
))
792 /* Same or super-using namespace. */
796 permerror (input_location
,
797 "specialization of %qD in different namespace", tmpl
);
798 permerror (DECL_SOURCE_LOCATION (tmpl
),
799 " from definition of %q#D", tmpl
);
804 /* SPEC is an explicit instantiation. Check that it is valid to
805 perform this explicit instantiation in the current namespace. */
808 check_explicit_instantiation_namespace (tree spec
)
812 /* DR 275: An explicit instantiation shall appear in an enclosing
813 namespace of its template. */
814 ns
= decl_namespace_context (spec
);
815 if (!is_ancestor (current_namespace
, ns
))
816 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
817 "(which does not enclose namespace %qD)",
818 spec
, current_namespace
, ns
);
821 /* The TYPE is being declared. If it is a template type, that means it
822 is a partial specialization. Do appropriate error-checking. */
825 maybe_process_partial_specialization (tree type
)
829 if (type
== error_mark_node
)
830 return error_mark_node
;
832 /* A lambda that appears in specialization context is not itself a
834 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
837 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
839 error ("name of class shadows template template parameter %qD",
841 return error_mark_node
;
844 context
= TYPE_CONTEXT (type
);
846 if (TYPE_ALIAS_P (type
))
848 if (TYPE_TEMPLATE_INFO (type
)
849 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type
)))
850 error ("specialization of alias template %qD",
851 TYPE_TI_TEMPLATE (type
));
853 error ("explicit specialization of non-template %qT", type
);
854 return error_mark_node
;
856 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
858 /* This is for ordinary explicit specialization and partial
859 specialization of a template class such as:
861 template <> class C<int>;
865 template <class T> class C<T*>;
867 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
869 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
870 && !COMPLETE_TYPE_P (type
))
872 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
))
873 && !at_namespace_scope_p ())
874 return error_mark_node
;
875 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
876 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
877 if (processing_template_decl
)
879 if (push_template_decl (TYPE_MAIN_DECL (type
))
881 return error_mark_node
;
884 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
885 error ("specialization of %qT after instantiation", type
);
886 else if (errorcount
&& !processing_specialization
887 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
888 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
889 /* Trying to define a specialization either without a template<> header
890 or in an inappropriate place. We've already given an error, so just
891 bail now so we don't actually define the specialization. */
892 return error_mark_node
;
894 else if (CLASS_TYPE_P (type
)
895 && !CLASSTYPE_USE_TEMPLATE (type
)
896 && CLASSTYPE_TEMPLATE_INFO (type
)
897 && context
&& CLASS_TYPE_P (context
)
898 && CLASSTYPE_TEMPLATE_INFO (context
))
900 /* This is for an explicit specialization of member class
901 template according to [temp.expl.spec/18]:
903 template <> template <class U> class C<int>::D;
905 The context `C<int>' must be an implicit instantiation.
906 Otherwise this is just a member class template declared
909 template <> class C<int> { template <class U> class D; };
910 template <> template <class U> class C<int>::D;
912 In the first case, `C<int>::D' is a specialization of `C<T>::D'
913 while in the second case, `C<int>::D' is a primary template
914 and `C<T>::D' may not exist. */
916 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
917 && !COMPLETE_TYPE_P (type
))
920 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
922 if (current_namespace
923 != decl_namespace_context (tmpl
))
925 permerror (input_location
,
926 "specializing %q#T in different namespace", type
);
927 permerror (DECL_SOURCE_LOCATION (tmpl
),
928 " from definition of %q#D", tmpl
);
931 /* Check for invalid specialization after instantiation:
933 template <> template <> class C<int>::D<int>;
934 template <> template <class U> class C<int>::D; */
936 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
937 t
; t
= TREE_CHAIN (t
))
939 tree inst
= TREE_VALUE (t
);
940 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
941 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
943 /* We already have a full specialization of this partial
944 instantiation, or a full specialization has been
945 looked up but not instantiated. Reassign it to the
946 new member specialization template. */
950 elt
.tmpl
= most_general_template (tmpl
);
951 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
954 type_specializations
->remove_elt (&elt
);
957 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
960 = type_specializations
->find_slot (&elt
, INSERT
);
961 entry
= ggc_alloc
<spec_entry
> ();
966 /* But if we've had an implicit instantiation, that's a
967 problem ([temp.expl.spec]/6). */
968 error ("specialization %qT after instantiation %qT",
972 /* Mark TYPE as a specialization. And as a result, we only
973 have one level of template argument for the innermost
975 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
976 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
977 CLASSTYPE_TI_ARGS (type
)
978 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
981 else if (processing_specialization
)
983 /* Someday C++0x may allow for enum template specialization. */
984 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
985 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
986 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
987 "of %qD not allowed by ISO C++", type
);
990 error ("explicit specialization of non-template %qT", type
);
991 return error_mark_node
;
998 /* Returns nonzero if we can optimize the retrieval of specializations
999 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1000 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1003 optimize_specialization_lookup_p (tree tmpl
)
1005 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1006 && DECL_CLASS_SCOPE_P (tmpl
)
1007 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1009 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1010 /* The optimized lookup depends on the fact that the
1011 template arguments for the member function template apply
1012 purely to the containing class, which is not true if the
1013 containing class is an explicit or partial
1015 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1016 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1017 && !DECL_CONV_FN_P (tmpl
)
1018 /* It is possible to have a template that is not a member
1019 template and is not a member of a template class:
1021 template <typename T>
1022 struct S { friend A::f(); };
1024 Here, the friend function is a template, but the context does
1025 not have template information. The optimized lookup relies
1026 on having ARGS be the template arguments for both the class
1027 and the function template. */
1028 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1031 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1032 gone through coerce_template_parms by now. */
1035 check_unstripped_args (tree args ATTRIBUTE_UNUSED
)
1037 #ifdef ENABLE_CHECKING
1038 ++processing_template_decl
;
1039 if (!any_dependent_template_arguments_p (args
))
1041 tree inner
= INNERMOST_TEMPLATE_ARGS (args
);
1042 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1044 tree arg
= TREE_VEC_ELT (inner
, i
);
1045 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1047 else if (TYPE_P (arg
))
1048 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1049 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1050 /* Allow typedefs on the type of a non-type argument, since a
1051 parameter can have them. */;
1053 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1056 --processing_template_decl
;
1060 /* Retrieve the specialization (in the sense of [temp.spec] - a
1061 specialization is either an instantiation or an explicit
1062 specialization) of TMPL for the given template ARGS. If there is
1063 no such specialization, return NULL_TREE. The ARGS are a vector of
1064 arguments, or a vector of vectors of arguments, in the case of
1065 templates with more than one level of parameters.
1067 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1068 then we search for a partial specialization matching ARGS. This
1069 parameter is ignored if TMPL is not a class template.
1071 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1072 result is a NONTYPE_ARGUMENT_PACK. */
1075 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1077 if (tmpl
== NULL_TREE
)
1080 if (args
== error_mark_node
)
1083 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1084 || TREE_CODE (tmpl
) == FIELD_DECL
);
1086 /* There should be as many levels of arguments as there are
1087 levels of parameters. */
1088 gcc_assert (TMPL_ARGS_DEPTH (args
)
1089 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1090 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1091 : template_class_depth (DECL_CONTEXT (tmpl
))));
1093 check_unstripped_args (args
);
1095 if (optimize_specialization_lookup_p (tmpl
))
1097 tree class_template
;
1098 tree class_specialization
;
1099 vec
<tree
, va_gc
> *methods
;
1103 /* The template arguments actually apply to the containing
1104 class. Find the class specialization with those
1106 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1107 class_specialization
1108 = retrieve_specialization (class_template
, args
, 0);
1109 if (!class_specialization
)
1111 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1112 for the specialization. */
1113 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1116 /* Iterate through the methods with the indicated name, looking
1117 for the one that has an instance of TMPL. */
1118 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1119 for (fns
= (*methods
)[idx
]; fns
; fns
= OVL_NEXT (fns
))
1121 tree fn
= OVL_CURRENT (fns
);
1122 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1123 /* using-declarations can add base methods to the method vec,
1124 and we don't want those here. */
1125 && DECL_CONTEXT (fn
) == class_specialization
)
1134 hash_table
<spec_hasher
> *specializations
;
1138 elt
.spec
= NULL_TREE
;
1140 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1141 specializations
= type_specializations
;
1143 specializations
= decl_specializations
;
1146 hash
= spec_hasher::hash (&elt
);
1147 found
= specializations
->find_with_hash (&elt
, hash
);
1155 /* Like retrieve_specialization, but for local declarations. */
1158 retrieve_local_specialization (tree tmpl
)
1160 if (local_specializations
== NULL
)
1163 tree
*slot
= local_specializations
->get (tmpl
);
1164 return slot
? *slot
: NULL_TREE
;
1167 /* Returns nonzero iff DECL is a specialization of TMPL. */
1170 is_specialization_of (tree decl
, tree tmpl
)
1174 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1178 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1184 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1186 for (t
= TREE_TYPE (decl
);
1188 t
= CLASSTYPE_USE_TEMPLATE (t
)
1189 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1190 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1197 /* Returns nonzero iff DECL is a specialization of friend declaration
1198 FRIEND_DECL according to [temp.friend]. */
1201 is_specialization_of_friend (tree decl
, tree friend_decl
)
1203 bool need_template
= true;
1206 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1207 || TREE_CODE (decl
) == TYPE_DECL
);
1209 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1210 of a template class, we want to check if DECL is a specialization
1212 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1213 && DECL_TEMPLATE_INFO (friend_decl
)
1214 && !DECL_USE_TEMPLATE (friend_decl
))
1216 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1217 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1218 need_template
= false;
1220 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1221 && !PRIMARY_TEMPLATE_P (friend_decl
))
1222 need_template
= false;
1224 /* There is nothing to do if this is not a template friend. */
1225 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1228 if (is_specialization_of (decl
, friend_decl
))
1232 A member of a class template may be declared to be a friend of a
1233 non-template class. In this case, the corresponding member of
1234 every specialization of the class template is a friend of the
1235 class granting friendship.
1237 For example, given a template friend declaration
1239 template <class T> friend void A<T>::f();
1241 the member function below is considered a friend
1243 template <> struct A<int> {
1247 For this type of template friend, TEMPLATE_DEPTH below will be
1248 nonzero. To determine if DECL is a friend of FRIEND, we first
1249 check if the enclosing class is a specialization of another. */
1251 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1253 && DECL_CLASS_SCOPE_P (decl
)
1254 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1255 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1257 /* Next, we check the members themselves. In order to handle
1258 a few tricky cases, such as when FRIEND_DECL's are
1260 template <class T> friend void A<T>::g(T t);
1261 template <class T> template <T t> friend void A<T>::h();
1265 void A<int>::g(int);
1266 template <int> void A<int>::h();
1268 we need to figure out ARGS, the template arguments from
1269 the context of DECL. This is required for template substitution
1270 of `T' in the function parameter of `g' and template parameter
1271 of `h' in the above examples. Here ARGS corresponds to `int'. */
1273 tree context
= DECL_CONTEXT (decl
);
1274 tree args
= NULL_TREE
;
1275 int current_depth
= 0;
1277 while (current_depth
< template_depth
)
1279 if (CLASSTYPE_TEMPLATE_INFO (context
))
1281 if (current_depth
== 0)
1282 args
= TYPE_TI_ARGS (context
);
1284 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1287 context
= TYPE_CONTEXT (context
);
1290 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1295 tree friend_args_type
;
1296 tree decl_args_type
;
1298 /* Make sure that both DECL and FRIEND_DECL are templates or
1300 is_template
= DECL_TEMPLATE_INFO (decl
)
1301 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1302 if (need_template
^ is_template
)
1304 else if (is_template
)
1306 /* If both are templates, check template parameter list. */
1308 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1310 if (!comp_template_parms
1311 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1315 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1318 decl_type
= TREE_TYPE (decl
);
1320 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1321 tf_none
, NULL_TREE
);
1322 if (friend_type
== error_mark_node
)
1325 /* Check if return types match. */
1326 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1329 /* Check if function parameter types match, ignoring the
1330 `this' parameter. */
1331 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1332 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1333 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1334 friend_args_type
= TREE_CHAIN (friend_args_type
);
1335 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1336 decl_args_type
= TREE_CHAIN (decl_args_type
);
1338 return compparms (decl_args_type
, friend_args_type
);
1342 /* DECL is a TYPE_DECL */
1344 tree decl_type
= TREE_TYPE (decl
);
1346 /* Make sure that both DECL and FRIEND_DECL are templates or
1349 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1350 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1352 if (need_template
^ is_template
)
1354 else if (is_template
)
1357 /* If both are templates, check the name of the two
1358 TEMPLATE_DECL's first because is_friend didn't. */
1359 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1360 != DECL_NAME (friend_decl
))
1363 /* Now check template parameter list. */
1365 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1367 return comp_template_parms
1368 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1372 return (DECL_NAME (decl
)
1373 == DECL_NAME (friend_decl
));
1379 /* Register the specialization SPEC as a specialization of TMPL with
1380 the indicated ARGS. IS_FRIEND indicates whether the specialization
1381 is actually just a friend declaration. Returns SPEC, or an
1382 equivalent prior declaration, if available.
1384 We also store instantiations of field packs in the hash table, even
1385 though they are not themselves templates, to make lookup easier. */
1388 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1392 spec_entry
**slot
= NULL
;
1395 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1396 || (TREE_CODE (tmpl
) == FIELD_DECL
1397 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1399 if (TREE_CODE (spec
) == FUNCTION_DECL
1400 && uses_template_parms (DECL_TI_ARGS (spec
)))
1401 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1402 register it; we want the corresponding TEMPLATE_DECL instead.
1403 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1404 the more obvious `uses_template_parms (spec)' to avoid problems
1405 with default function arguments. In particular, given
1406 something like this:
1408 template <class T> void f(T t1, T t = T())
1410 the default argument expression is not substituted for in an
1411 instantiation unless and until it is actually needed. */
1414 if (optimize_specialization_lookup_p (tmpl
))
1415 /* We don't put these specializations in the hash table, but we might
1416 want to give an error about a mismatch. */
1417 fn
= retrieve_specialization (tmpl
, args
, 0);
1425 hash
= spec_hasher::hash (&elt
);
1428 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1430 fn
= ((spec_entry
*) *slot
)->spec
;
1435 /* We can sometimes try to re-register a specialization that we've
1436 already got. In particular, regenerate_decl_from_template calls
1437 duplicate_decls which will update the specialization list. But,
1438 we'll still get called again here anyhow. It's more convenient
1439 to simply allow this than to try to prevent it. */
1442 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1444 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1446 if (DECL_ODR_USED (fn
)
1447 || DECL_EXPLICIT_INSTANTIATION (fn
))
1449 error ("specialization of %qD after instantiation",
1451 return error_mark_node
;
1456 /* This situation should occur only if the first
1457 specialization is an implicit instantiation, the
1458 second is an explicit specialization, and the
1459 implicit instantiation has not yet been used. That
1460 situation can occur if we have implicitly
1461 instantiated a member function and then specialized
1464 We can also wind up here if a friend declaration that
1465 looked like an instantiation turns out to be a
1468 template <class T> void foo(T);
1469 class S { friend void foo<>(int) };
1470 template <> void foo(int);
1472 We transform the existing DECL in place so that any
1473 pointers to it become pointers to the updated
1476 If there was a definition for the template, but not
1477 for the specialization, we want this to look as if
1478 there were no definition, and vice versa. */
1479 DECL_INITIAL (fn
) = NULL_TREE
;
1480 duplicate_decls (spec
, fn
, is_friend
);
1481 /* The call to duplicate_decls will have applied
1484 An explicit specialization of a function template
1485 is inline only if it is explicitly declared to be,
1486 and independently of whether its function template
1489 to the primary function; now copy the inline bits to
1490 the various clones. */
1491 FOR_EACH_CLONE (clone
, fn
)
1493 DECL_DECLARED_INLINE_P (clone
)
1494 = DECL_DECLARED_INLINE_P (fn
);
1495 DECL_SOURCE_LOCATION (clone
)
1496 = DECL_SOURCE_LOCATION (fn
);
1497 DECL_DELETED_FN (clone
)
1498 = DECL_DELETED_FN (fn
);
1500 check_specialization_namespace (tmpl
);
1505 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1507 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1508 /* Dup decl failed, but this is a new definition. Set the
1509 line number so any errors match this new
1511 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1517 return duplicate_decls (spec
, fn
, is_friend
);
1519 /* A specialization must be declared in the same namespace as the
1520 template it is specializing. */
1521 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1522 && !check_specialization_namespace (tmpl
))
1523 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1525 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1527 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1528 gcc_assert (tmpl
&& args
&& spec
);
1531 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1532 && PRIMARY_TEMPLATE_P (tmpl
)
1533 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1534 || variable_template_p (tmpl
))
1535 /* If TMPL is a forward declaration of a template function, keep a list
1536 of all specializations in case we need to reassign them to a friend
1537 template later in tsubst_friend_function.
1539 Also keep a list of all variable template instantiations so that
1540 process_partial_specialization can check whether a later partial
1541 specialization would have used it. */
1542 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1543 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1549 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1550 TMPL and ARGS members, ignores SPEC. */
1552 int comparing_specializations
;
1555 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1559 ++comparing_specializations
;
1560 equal
= (e1
->tmpl
== e2
->tmpl
1561 && comp_template_args (e1
->args
, e2
->args
));
1562 --comparing_specializations
;
1567 /* Returns a hash for a template TMPL and template arguments ARGS. */
1570 hash_tmpl_and_args (tree tmpl
, tree args
)
1572 hashval_t val
= DECL_UID (tmpl
);
1573 return iterative_hash_template_arg (args
, val
);
1576 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1580 spec_hasher::hash (spec_entry
*e
)
1582 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1585 /* Recursively calculate a hash value for a template argument ARG, for use
1586 in the hash tables of template specializations. */
1589 iterative_hash_template_arg (tree arg
, hashval_t val
)
1591 unsigned HOST_WIDE_INT i
;
1592 enum tree_code code
;
1595 if (arg
== NULL_TREE
)
1596 return iterative_hash_object (arg
, val
);
1601 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1602 /* We can get one of these when re-hashing a previous entry in the middle
1603 of substituting into a pack expansion. Just look through it. */
1604 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1606 code
= TREE_CODE (arg
);
1607 tclass
= TREE_CODE_CLASS (code
);
1609 val
= iterative_hash_object (code
, val
);
1616 case IDENTIFIER_NODE
:
1617 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1621 int i
, len
= TREE_VEC_LENGTH (arg
);
1622 for (i
= 0; i
< len
; ++i
)
1623 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1627 case TYPE_PACK_EXPANSION
:
1628 case EXPR_PACK_EXPANSION
:
1629 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1630 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1632 case TYPE_ARGUMENT_PACK
:
1633 case NONTYPE_ARGUMENT_PACK
:
1634 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1637 for (; arg
; arg
= TREE_CHAIN (arg
))
1638 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1642 for (; arg
; arg
= OVL_NEXT (arg
))
1643 val
= iterative_hash_template_arg (OVL_CURRENT (arg
), val
);
1649 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1650 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1652 val
= iterative_hash_template_arg (field
, val
);
1653 val
= iterative_hash_template_arg (value
, val
);
1659 if (!DECL_ARTIFICIAL (arg
))
1661 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1662 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1664 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1667 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1670 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1671 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1673 case TEMPLATE_PARM_INDEX
:
1674 val
= iterative_hash_template_arg
1675 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1676 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1677 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1680 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1681 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1682 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1685 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1687 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1691 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1692 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1693 val
= iterative_hash_object (code
, val
);
1694 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1697 /* A lambda can't appear in a template arg, but don't crash on
1699 gcc_assert (seen_error ());
1703 case IMPLICIT_CONV_EXPR
:
1704 case STATIC_CAST_EXPR
:
1705 case REINTERPRET_CAST_EXPR
:
1706 case CONST_CAST_EXPR
:
1707 case DYNAMIC_CAST_EXPR
:
1709 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1710 /* Now hash operands as usual. */
1720 if (alias_template_specialization_p (arg
))
1722 // We want an alias specialization that survived strip_typedefs
1723 // to hash differently from its TYPE_CANONICAL, to avoid hash
1724 // collisions that compare as different in template_args_equal.
1725 // These could be dependent specializations that strip_typedefs
1726 // left alone, or untouched specializations because
1727 // coerce_template_parms returns the unconverted template
1728 // arguments if it sees incomplete argument packs.
1729 tree ti
= TYPE_TEMPLATE_INFO (arg
);
1730 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1732 if (TYPE_CANONICAL (arg
))
1733 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1735 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1736 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1737 /* Otherwise just compare the types during lookup. */
1740 case tcc_declaration
:
1742 return iterative_hash_expr (arg
, val
);
1745 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1747 unsigned n
= cp_tree_operand_length (arg
);
1748 for (i
= 0; i
< n
; ++i
)
1749 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1757 /* Unregister the specialization SPEC as a specialization of TMPL.
1758 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1759 if the SPEC was listed as a specialization of TMPL.
1761 Note that SPEC has been ggc_freed, so we can't look inside it. */
1764 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1769 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1770 elt
.args
= TI_ARGS (tinfo
);
1771 elt
.spec
= NULL_TREE
;
1773 entry
= decl_specializations
->find (&elt
);
1776 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1777 gcc_assert (new_spec
!= NULL_TREE
);
1778 entry
->spec
= new_spec
;
1785 /* Like register_specialization, but for local declarations. We are
1786 registering SPEC, an instantiation of TMPL. */
1789 register_local_specialization (tree spec
, tree tmpl
)
1791 local_specializations
->put (tmpl
, spec
);
1794 /* TYPE is a class type. Returns true if TYPE is an explicitly
1795 specialized class. */
1798 explicit_class_specialization_p (tree type
)
1800 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1802 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1805 /* Print the list of functions at FNS, going through all the overloads
1806 for each element of the list. Alternatively, FNS can not be a
1807 TREE_LIST, in which case it will be printed together with all the
1810 MORE and *STR should respectively be FALSE and NULL when the function
1811 is called from the outside. They are used internally on recursive
1812 calls. print_candidates manages the two parameters and leaves NULL
1813 in *STR when it ends. */
1816 print_candidates_1 (tree fns
, bool more
, const char **str
)
1819 char *spaces
= NULL
;
1821 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1822 if (TREE_CODE (fn
) == TREE_LIST
)
1824 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1825 print_candidates_1 (TREE_VALUE (fn2
),
1826 TREE_CHAIN (fn2
) || more
, str
);
1830 tree cand
= OVL_CURRENT (fn
);
1833 /* Pick the prefix string. */
1834 if (!more
&& !OVL_NEXT (fns
))
1836 inform (DECL_SOURCE_LOCATION (cand
),
1837 "candidate is: %#D", cand
);
1841 *str
= _("candidates are:");
1842 spaces
= get_spaces (*str
);
1844 inform (DECL_SOURCE_LOCATION (cand
), "%s %#D", *str
, cand
);
1845 *str
= spaces
? spaces
: *str
;
1855 /* Print the list of candidate FNS in an error message. FNS can also
1856 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1859 print_candidates (tree fns
)
1861 const char *str
= NULL
;
1862 print_candidates_1 (fns
, false, &str
);
1863 gcc_assert (str
== NULL
);
1866 /* Returns the template (one of the functions given by TEMPLATE_ID)
1867 which can be specialized to match the indicated DECL with the
1868 explicit template args given in TEMPLATE_ID. The DECL may be
1869 NULL_TREE if none is available. In that case, the functions in
1870 TEMPLATE_ID are non-members.
1872 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1873 specialization of a member template.
1875 The TEMPLATE_COUNT is the number of references to qualifying
1876 template classes that appeared in the name of the function. See
1877 check_explicit_specialization for a more accurate description.
1879 TSK indicates what kind of template declaration (if any) is being
1880 declared. TSK_TEMPLATE indicates that the declaration given by
1881 DECL, though a FUNCTION_DECL, has template parameters, and is
1882 therefore a template function.
1884 The template args (those explicitly specified and those deduced)
1885 are output in a newly created vector *TARGS_OUT.
1887 If it is impossible to determine the result, an error message is
1888 issued. The error_mark_node is returned to indicate failure. */
1891 determine_specialization (tree template_id
,
1894 int need_member_template
,
1900 tree explicit_targs
;
1901 tree candidates
= NULL_TREE
;
1902 /* A TREE_LIST of templates of which DECL may be a specialization.
1903 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1904 corresponding TREE_PURPOSE is the set of template arguments that,
1905 when used to instantiate the template, would produce a function
1906 with the signature of DECL. */
1907 tree templates
= NULL_TREE
;
1909 cp_binding_level
*b
;
1911 *targs_out
= NULL_TREE
;
1913 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1914 return error_mark_node
;
1916 /* We shouldn't be specializing a member template of an
1917 unspecialized class template; we already gave an error in
1918 check_specialization_scope, now avoid crashing. */
1919 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
1920 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
1922 gcc_assert (errorcount
);
1923 return error_mark_node
;
1926 fns
= TREE_OPERAND (template_id
, 0);
1927 explicit_targs
= TREE_OPERAND (template_id
, 1);
1929 if (fns
== error_mark_node
)
1930 return error_mark_node
;
1932 /* Check for baselinks. */
1933 if (BASELINK_P (fns
))
1934 fns
= BASELINK_FUNCTIONS (fns
);
1936 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
1938 error ("%qD is not a function template", fns
);
1939 return error_mark_node
;
1941 else if (VAR_P (decl
) && !variable_template_p (fns
))
1943 error ("%qD is not a variable template", fns
);
1944 return error_mark_node
;
1947 /* Count the number of template headers specified for this
1950 for (b
= current_binding_level
;
1951 b
->kind
== sk_template_parms
;
1955 tree orig_fns
= fns
;
1957 if (variable_template_p (fns
))
1959 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
1960 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
1961 tf_warning_or_error
,
1962 /*req_all*/true, /*use_defarg*/true);
1963 templates
= tree_cons (targs
, fns
, templates
);
1965 else for (; fns
; fns
= OVL_NEXT (fns
))
1967 tree fn
= OVL_CURRENT (fns
);
1969 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1971 tree decl_arg_types
;
1975 /* In case of explicit specialization, we need to check if
1976 the number of template headers appearing in the specialization
1977 is correct. This is usually done in check_explicit_specialization,
1978 but the check done there cannot be exhaustive when specializing
1979 member functions. Consider the following code:
1981 template <> void A<int>::f(int);
1982 template <> template <> void A<int>::f(int);
1984 Assuming that A<int> is not itself an explicit specialization
1985 already, the first line specializes "f" which is a non-template
1986 member function, whilst the second line specializes "f" which
1987 is a template member function. So both lines are syntactically
1988 correct, and check_explicit_specialization does not reject
1991 Here, we can do better, as we are matching the specialization
1992 against the declarations. We count the number of template
1993 headers, and we check if they match TEMPLATE_COUNT + 1
1994 (TEMPLATE_COUNT is the number of qualifying template classes,
1995 plus there must be another header for the member template
1998 Notice that if header_count is zero, this is not a
1999 specialization but rather a template instantiation, so there
2000 is no check we can perform here. */
2001 if (header_count
&& header_count
!= template_count
+ 1)
2004 /* Check that the number of template arguments at the
2005 innermost level for DECL is the same as for FN. */
2006 if (current_binding_level
->kind
== sk_template_parms
2007 && !current_binding_level
->explicit_spec_p
2008 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2009 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2010 (current_template_parms
))))
2013 /* DECL might be a specialization of FN. */
2014 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2015 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2017 /* For a non-static member function, we need to make sure
2018 that the const qualification is the same. Since
2019 get_bindings does not try to merge the "this" parameter,
2020 we must do the comparison explicitly. */
2021 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2022 && !same_type_p (TREE_VALUE (fn_arg_types
),
2023 TREE_VALUE (decl_arg_types
)))
2026 /* Skip the "this" parameter and, for constructors of
2027 classes with virtual bases, the VTT parameter. A
2028 full specialization of a constructor will have a VTT
2029 parameter, but a template never will. */
2031 = skip_artificial_parms_for (decl
, decl_arg_types
);
2033 = skip_artificial_parms_for (fn
, fn_arg_types
);
2035 /* Function templates cannot be specializations; there are
2036 no partial specializations of functions. Therefore, if
2037 the type of DECL does not match FN, there is no
2039 if (tsk
== tsk_template
)
2041 if (compparms (fn_arg_types
, decl_arg_types
))
2042 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2046 /* See whether this function might be a specialization of this
2047 template. Suppress access control because we might be trying
2048 to make this specialization a friend, and we have already done
2049 access control for the declaration of the specialization. */
2050 push_deferring_access_checks (dk_no_check
);
2051 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2052 pop_deferring_access_checks ();
2055 /* We cannot deduce template arguments that when used to
2056 specialize TMPL will produce DECL. */
2059 /* Make sure that the deduced arguments actually work. */
2060 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
2061 if (insttype
== error_mark_node
)
2064 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2065 if (!compparms (fn_arg_types
, decl_arg_types
))
2068 /* Save this template, and the arguments deduced. */
2069 templates
= tree_cons (targs
, fn
, templates
);
2071 else if (need_member_template
)
2072 /* FN is an ordinary member function, and we need a
2073 specialization of a member template. */
2075 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2076 /* We can get IDENTIFIER_NODEs here in certain erroneous
2079 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2080 /* This is just an ordinary non-member function. Nothing can
2081 be a specialization of that. */
2083 else if (DECL_ARTIFICIAL (fn
))
2084 /* Cannot specialize functions that are created implicitly. */
2088 tree decl_arg_types
;
2090 /* This is an ordinary member function. However, since
2091 we're here, we can assume its enclosing class is a
2092 template class. For example,
2094 template <typename T> struct S { void f(); };
2095 template <> void S<int>::f() {}
2097 Here, S<int>::f is a non-template, but S<int> is a
2098 template class. If FN has the same type as DECL, we
2099 might be in business. */
2101 if (!DECL_TEMPLATE_INFO (fn
))
2102 /* Its enclosing class is an explicit specialization
2103 of a template class. This is not a candidate. */
2106 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2107 TREE_TYPE (TREE_TYPE (fn
))))
2108 /* The return types differ. */
2111 /* Adjust the type of DECL in case FN is a static member. */
2112 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2113 if (DECL_STATIC_FUNCTION_P (fn
)
2114 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2115 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2117 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2120 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2124 if (templates
&& TREE_CHAIN (templates
))
2130 It is possible for a specialization with a given function
2131 signature to be instantiated from more than one function
2132 template. In such cases, explicit specification of the
2133 template arguments must be used to uniquely identify the
2134 function template specialization being specialized.
2136 Note that here, there's no suggestion that we're supposed to
2137 determine which of the candidate templates is most
2138 specialized. However, we, also have:
2142 Partial ordering of overloaded function template
2143 declarations is used in the following contexts to select
2144 the function template to which a function template
2145 specialization refers:
2147 -- when an explicit specialization refers to a function
2150 So, we do use the partial ordering rules, at least for now.
2151 This extension can only serve to make invalid programs valid,
2152 so it's safe. And, there is strong anecdotal evidence that
2153 the committee intended the partial ordering rules to apply;
2154 the EDG front end has that behavior, and John Spicer claims
2155 that the committee simply forgot to delete the wording in
2156 [temp.expl.spec]. */
2157 tree tmpl
= most_specialized_instantiation (templates
);
2158 if (tmpl
!= error_mark_node
)
2161 TREE_CHAIN (templates
) = NULL_TREE
;
2165 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2167 error ("template-id %qD for %q+D does not match any template "
2168 "declaration", template_id
, decl
);
2169 if (header_count
&& header_count
!= template_count
+ 1)
2170 inform (input_location
, "saw %d %<template<>%>, need %d for "
2171 "specializing a member function template",
2172 header_count
, template_count
+ 1);
2174 print_candidates (orig_fns
);
2175 return error_mark_node
;
2177 else if ((templates
&& TREE_CHAIN (templates
))
2178 || (candidates
&& TREE_CHAIN (candidates
))
2179 || (templates
&& candidates
))
2181 error ("ambiguous template specialization %qD for %q+D",
2183 candidates
= chainon (candidates
, templates
);
2184 print_candidates (candidates
);
2185 return error_mark_node
;
2188 /* We have one, and exactly one, match. */
2191 tree fn
= TREE_VALUE (candidates
);
2192 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2193 /* DECL is a re-declaration or partial instantiation of a template
2195 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2197 /* It was a specialization of an ordinary member function in a
2199 return DECL_TI_TEMPLATE (fn
);
2202 /* It was a specialization of a template. */
2203 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2204 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2206 *targs_out
= copy_node (targs
);
2207 SET_TMPL_ARGS_LEVEL (*targs_out
,
2208 TMPL_ARGS_DEPTH (*targs_out
),
2209 TREE_PURPOSE (templates
));
2212 *targs_out
= TREE_PURPOSE (templates
);
2213 return TREE_VALUE (templates
);
2216 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2217 but with the default argument values filled in from those in the
2221 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2224 tree new_spec_types
;
2229 if (spec_types
== void_list_node
)
2230 return void_list_node
;
2232 /* Substitute into the rest of the list. */
2234 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2235 TREE_CHAIN (tmpl_types
));
2237 /* Add the default argument for this parameter. */
2238 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2239 TREE_VALUE (spec_types
),
2243 /* DECL is an explicit specialization. Replicate default arguments
2244 from the template it specializes. (That way, code like:
2246 template <class T> void f(T = 3);
2247 template <> void f(double);
2250 works, as required.) An alternative approach would be to look up
2251 the correct default arguments at the call-site, but this approach
2252 is consistent with how implicit instantiations are handled. */
2255 copy_default_args_to_explicit_spec (tree decl
)
2260 tree new_spec_types
;
2264 tree object_type
= NULL_TREE
;
2265 tree in_charge
= NULL_TREE
;
2266 tree vtt
= NULL_TREE
;
2268 /* See if there's anything we need to do. */
2269 tmpl
= DECL_TI_TEMPLATE (decl
);
2270 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2271 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2272 if (TREE_PURPOSE (t
))
2277 old_type
= TREE_TYPE (decl
);
2278 spec_types
= TYPE_ARG_TYPES (old_type
);
2280 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2282 /* Remove the this pointer, but remember the object's type for
2284 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2285 spec_types
= TREE_CHAIN (spec_types
);
2286 tmpl_types
= TREE_CHAIN (tmpl_types
);
2288 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2290 /* DECL may contain more parameters than TMPL due to the extra
2291 in-charge parameter in constructors and destructors. */
2292 in_charge
= spec_types
;
2293 spec_types
= TREE_CHAIN (spec_types
);
2295 if (DECL_HAS_VTT_PARM_P (decl
))
2298 spec_types
= TREE_CHAIN (spec_types
);
2302 /* Compute the merged default arguments. */
2304 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2306 /* Compute the new FUNCTION_TYPE. */
2310 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2315 /* Put the in-charge parameter back. */
2316 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2317 TREE_VALUE (in_charge
),
2320 new_type
= build_method_type_directly (object_type
,
2321 TREE_TYPE (old_type
),
2325 new_type
= build_function_type (TREE_TYPE (old_type
),
2327 new_type
= cp_build_type_attribute_variant (new_type
,
2328 TYPE_ATTRIBUTES (old_type
));
2329 new_type
= build_exception_variant (new_type
,
2330 TYPE_RAISES_EXCEPTIONS (old_type
));
2332 if (TYPE_HAS_LATE_RETURN_TYPE (old_type
))
2333 TYPE_HAS_LATE_RETURN_TYPE (new_type
) = 1;
2335 TREE_TYPE (decl
) = new_type
;
2338 /* Return the number of template headers we expect to see for a definition
2339 or specialization of CTYPE or one of its non-template members. */
2342 num_template_headers_for_class (tree ctype
)
2344 int num_templates
= 0;
2346 while (ctype
&& CLASS_TYPE_P (ctype
))
2348 /* You're supposed to have one `template <...>' for every
2349 template class, but you don't need one for a full
2350 specialization. For example:
2352 template <class T> struct S{};
2353 template <> struct S<int> { void f(); };
2354 void S<int>::f () {}
2356 is correct; there shouldn't be a `template <>' for the
2357 definition of `S<int>::f'. */
2358 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2359 /* If CTYPE does not have template information of any
2360 kind, then it is not a template, nor is it nested
2361 within a template. */
2363 if (explicit_class_specialization_p (ctype
))
2365 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2368 ctype
= TYPE_CONTEXT (ctype
);
2371 return num_templates
;
2374 /* Do a simple sanity check on the template headers that precede the
2375 variable declaration DECL. */
2378 check_template_variable (tree decl
)
2380 tree ctx
= CP_DECL_CONTEXT (decl
);
2381 int wanted
= num_template_headers_for_class (ctx
);
2382 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2383 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2385 if (cxx_dialect
< cxx14
)
2386 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2387 "variable templates only available with "
2388 "-std=c++14 or -std=gnu++14");
2390 // Namespace-scope variable templates should have a template header.
2393 if (template_header_count
> wanted
)
2395 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2396 "too many template headers for %D (should be %d)",
2398 if (warned
&& CLASS_TYPE_P (ctx
)
2399 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2400 inform (DECL_SOURCE_LOCATION (decl
),
2401 "members of an explicitly specialized class are defined "
2402 "without a template header");
2406 /* Check to see if the function just declared, as indicated in
2407 DECLARATOR, and in DECL, is a specialization of a function
2408 template. We may also discover that the declaration is an explicit
2409 instantiation at this point.
2411 Returns DECL, or an equivalent declaration that should be used
2412 instead if all goes well. Issues an error message if something is
2413 amiss. Returns error_mark_node if the error is not easily
2416 FLAGS is a bitmask consisting of the following flags:
2418 2: The function has a definition.
2419 4: The function is a friend.
2421 The TEMPLATE_COUNT is the number of references to qualifying
2422 template classes that appeared in the name of the function. For
2425 template <class T> struct S { void f(); };
2428 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2429 classes are not counted in the TEMPLATE_COUNT, so that in
2431 template <class T> struct S {};
2432 template <> struct S<int> { void f(); }
2433 template <> void S<int>::f();
2435 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2436 invalid; there should be no template <>.)
2438 If the function is a specialization, it is marked as such via
2439 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2440 is set up correctly, and it is added to the list of specializations
2441 for that template. */
2444 check_explicit_specialization (tree declarator
,
2449 int have_def
= flags
& 2;
2450 int is_friend
= flags
& 4;
2451 int specialization
= 0;
2452 int explicit_instantiation
= 0;
2453 int member_specialization
= 0;
2454 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2455 tree dname
= DECL_NAME (decl
);
2460 if (!processing_specialization
)
2463 tsk
= tsk_excessive_parms
;
2466 tsk
= current_tmpl_spec_kind (template_count
);
2471 if (processing_specialization
&& !VAR_P (decl
))
2474 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2476 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2479 /* This could be something like:
2481 template <class T> void f(T);
2482 class S { friend void f<>(int); } */
2486 /* This case handles bogus declarations like template <>
2487 template <class T> void f<int>(); */
2489 error ("template-id %qD in declaration of primary template",
2496 case tsk_invalid_member_spec
:
2497 /* The error has already been reported in
2498 check_specialization_scope. */
2499 return error_mark_node
;
2501 case tsk_invalid_expl_inst
:
2502 error ("template parameter list used in explicit instantiation");
2508 error ("definition provided for explicit instantiation");
2510 explicit_instantiation
= 1;
2513 case tsk_excessive_parms
:
2514 case tsk_insufficient_parms
:
2515 if (tsk
== tsk_excessive_parms
)
2516 error ("too many template parameter lists in declaration of %qD",
2518 else if (template_header_count
)
2519 error("too few template parameter lists in declaration of %qD", decl
);
2521 error("explicit specialization of %qD must be introduced by "
2522 "%<template <>%>", decl
);
2526 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2527 /* In cases like template<> constexpr bool v = true;
2528 We'll give an error in check_template_variable. */
2531 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2533 member_specialization
= 1;
2539 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2541 /* This case handles bogus declarations like template <>
2542 template <class T> void f<int>(); */
2544 if (!uses_template_parms (declarator
))
2545 error ("template-id %qD in declaration of primary template",
2547 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2549 /* Partial specialization of variable template. */
2550 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2554 else if (cxx_dialect
< cxx14
)
2555 error ("non-type partial specialization %qD "
2556 "is not allowed", declarator
);
2558 error ("non-class, non-variable partial specialization %qD "
2559 "is not allowed", declarator
);
2564 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2565 /* This is a specialization of a member template, without
2566 specialization the containing class. Something like:
2568 template <class T> struct S {
2569 template <class U> void f (U);
2571 template <> template <class U> void S<int>::f(U) {}
2573 That's a specialization -- but of the entire template. */
2581 if ((specialization
|| member_specialization
)
2582 /* This doesn't apply to variable templates. */
2583 && (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
2584 || TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
))
2586 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2587 for (; t
; t
= TREE_CHAIN (t
))
2588 if (TREE_PURPOSE (t
))
2590 permerror (input_location
,
2591 "default argument specified in explicit specialization");
2596 if (specialization
|| member_specialization
|| explicit_instantiation
)
2598 tree tmpl
= NULL_TREE
;
2599 tree targs
= NULL_TREE
;
2600 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2602 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2603 if (!was_template_id
)
2607 gcc_assert (identifier_p (declarator
));
2612 /* If there is no class context, the explicit instantiation
2613 must be at namespace scope. */
2614 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2616 /* Find the namespace binding, using the declaration
2618 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2620 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2622 error ("%qD is not a template function", dname
);
2623 fns
= error_mark_node
;
2627 tree fn
= OVL_CURRENT (fns
);
2628 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2629 CP_DECL_CONTEXT (fn
)))
2630 error ("%qD is not declared in %qD",
2631 decl
, current_namespace
);
2635 declarator
= lookup_template_function (fns
, NULL_TREE
);
2638 if (declarator
== error_mark_node
)
2639 return error_mark_node
;
2641 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2643 if (!explicit_instantiation
)
2644 /* A specialization in class scope. This is invalid,
2645 but the error will already have been flagged by
2646 check_specialization_scope. */
2647 return error_mark_node
;
2650 /* It's not valid to write an explicit instantiation in
2653 class C { template void f(); }
2655 This case is caught by the parser. However, on
2658 template class C { void f(); };
2660 (which is invalid) we can get here. The error will be
2667 else if (ctype
!= NULL_TREE
2668 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2670 // We'll match variable templates in start_decl.
2674 /* Find the list of functions in ctype that have the same
2675 name as the declared function. */
2676 tree name
= TREE_OPERAND (declarator
, 0);
2677 tree fns
= NULL_TREE
;
2680 if (constructor_name_p (name
, ctype
))
2682 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2684 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2685 : !CLASSTYPE_DESTRUCTORS (ctype
))
2687 /* From [temp.expl.spec]:
2689 If such an explicit specialization for the member
2690 of a class template names an implicitly-declared
2691 special member function (clause _special_), the
2692 program is ill-formed.
2694 Similar language is found in [temp.explicit]. */
2695 error ("specialization of implicitly-declared special member function");
2696 return error_mark_node
;
2699 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2702 if (!DECL_CONV_FN_P (decl
))
2704 idx
= lookup_fnfields_1 (ctype
, name
);
2706 fns
= (*CLASSTYPE_METHOD_VEC (ctype
))[idx
];
2710 vec
<tree
, va_gc
> *methods
;
2713 /* For a type-conversion operator, we cannot do a
2714 name-based lookup. We might be looking for `operator
2715 int' which will be a specialization of `operator T'.
2716 So, we find *all* the conversion operators, and then
2717 select from them. */
2720 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2722 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2723 methods
->iterate (idx
, &ovl
);
2726 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2727 /* There are no more conversion functions. */
2730 /* Glue all these conversion functions together
2731 with those we already have. */
2732 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2733 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2737 if (fns
== NULL_TREE
)
2739 error ("no member function %qD declared in %qT", name
, ctype
);
2740 return error_mark_node
;
2743 TREE_OPERAND (declarator
, 0) = fns
;
2746 /* Figure out what exactly is being specialized at this point.
2747 Note that for an explicit instantiation, even one for a
2748 member function, we cannot tell apriori whether the
2749 instantiation is for a member template, or just a member
2750 function of a template class. Even if a member template is
2751 being instantiated, the member template arguments may be
2752 elided if they can be deduced from the rest of the
2754 tmpl
= determine_specialization (declarator
, decl
,
2756 member_specialization
,
2760 if (!tmpl
|| tmpl
== error_mark_node
)
2761 /* We couldn't figure out what this declaration was
2763 return error_mark_node
;
2766 tree gen_tmpl
= most_general_template (tmpl
);
2768 if (explicit_instantiation
)
2770 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2771 is done by do_decl_instantiation later. */
2773 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2774 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2776 if (arg_depth
> parm_depth
)
2778 /* If TMPL is not the most general template (for
2779 example, if TMPL is a friend template that is
2780 injected into namespace scope), then there will
2781 be too many levels of TARGS. Remove some of them
2786 new_targs
= make_tree_vec (parm_depth
);
2787 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2788 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2789 = TREE_VEC_ELT (targs
, i
);
2793 return instantiate_template (tmpl
, targs
, tf_error
);
2796 /* If we thought that the DECL was a member function, but it
2797 turns out to be specializing a static member function,
2798 make DECL a static member function as well. */
2799 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
2800 && DECL_STATIC_FUNCTION_P (tmpl
)
2801 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2802 revert_static_member_fn (decl
);
2804 /* If this is a specialization of a member template of a
2805 template class, we want to return the TEMPLATE_DECL, not
2806 the specialization of it. */
2807 if (tsk
== tsk_template
&& !was_template_id
)
2809 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2810 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2811 DECL_INITIAL (result
) = NULL_TREE
;
2815 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2816 DECL_SOURCE_LOCATION (result
)
2817 = DECL_SOURCE_LOCATION (decl
);
2818 /* We want to use the argument list specified in the
2819 definition, not in the original declaration. */
2820 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2821 for (parm
= DECL_ARGUMENTS (result
); parm
;
2822 parm
= DECL_CHAIN (parm
))
2823 DECL_CONTEXT (parm
) = result
;
2825 return register_specialization (tmpl
, gen_tmpl
, targs
,
2829 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2830 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2832 if (was_template_id
)
2833 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
2835 /* Inherit default function arguments from the template
2836 DECL is specializing. */
2837 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2838 copy_default_args_to_explicit_spec (decl
);
2840 /* This specialization has the same protection as the
2841 template it specializes. */
2842 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2843 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2845 /* 7.1.1-1 [dcl.stc]
2847 A storage-class-specifier shall not be specified in an
2848 explicit specialization...
2850 The parser rejects these, so unless action is taken here,
2851 explicit function specializations will always appear with
2854 The action recommended by the C++ CWG in response to C++
2855 defect report 605 is to make the storage class and linkage
2856 of the explicit specialization match the templated function:
2858 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2860 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2862 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2863 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2865 /* This specialization has the same linkage and visibility as
2866 the function template it specializes. */
2867 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2868 if (! TREE_PUBLIC (decl
))
2870 DECL_INTERFACE_KNOWN (decl
) = 1;
2871 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2873 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2874 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2876 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2877 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2881 /* If DECL is a friend declaration, declared using an
2882 unqualified name, the namespace associated with DECL may
2883 have been set incorrectly. For example, in:
2885 template <typename T> void f(T);
2887 struct S { friend void f<int>(int); }
2890 we will have set the DECL_CONTEXT for the friend
2891 declaration to N, rather than to the global namespace. */
2892 if (DECL_NAMESPACE_SCOPE_P (decl
))
2893 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2895 if (is_friend
&& !have_def
)
2896 /* This is not really a declaration of a specialization.
2897 It's just the name of an instantiation. But, it's not
2898 a request for an instantiation, either. */
2899 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2900 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
2901 /* A specialization is not necessarily COMDAT. */
2902 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
2903 && DECL_DECLARED_INLINE_P (decl
));
2904 else if (VAR_P (decl
))
2905 DECL_COMDAT (decl
) = false;
2907 /* Register this specialization so that we can find it
2909 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2911 /* A 'structor should already have clones. */
2912 gcc_assert (decl
== error_mark_node
2913 || variable_template_p (tmpl
)
2914 || !(DECL_CONSTRUCTOR_P (decl
)
2915 || DECL_DESTRUCTOR_P (decl
))
2916 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
2923 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2924 parameters. These are represented in the same format used for
2925 DECL_TEMPLATE_PARMS. */
2928 comp_template_parms (const_tree parms1
, const_tree parms2
)
2933 if (parms1
== parms2
)
2936 for (p1
= parms1
, p2
= parms2
;
2937 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2938 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2940 tree t1
= TREE_VALUE (p1
);
2941 tree t2
= TREE_VALUE (p2
);
2944 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2945 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2947 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2950 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2952 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2953 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2955 /* If either of the template parameters are invalid, assume
2956 they match for the sake of error recovery. */
2957 if (error_operand_p (parm1
) || error_operand_p (parm2
))
2960 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2963 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2964 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2965 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2967 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2972 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2973 /* One set of parameters has more parameters lists than the
2980 /* Determine whether PARM is a parameter pack. */
2983 template_parameter_pack_p (const_tree parm
)
2985 /* Determine if we have a non-type template parameter pack. */
2986 if (TREE_CODE (parm
) == PARM_DECL
)
2987 return (DECL_TEMPLATE_PARM_P (parm
)
2988 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2989 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
2990 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
2992 /* If this is a list of template parameters, we could get a
2993 TYPE_DECL or a TEMPLATE_DECL. */
2994 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2995 parm
= TREE_TYPE (parm
);
2997 /* Otherwise it must be a type template parameter. */
2998 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2999 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3000 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3003 /* Determine if T is a function parameter pack. */
3006 function_parameter_pack_p (const_tree t
)
3008 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3009 return DECL_PACK_P (t
);
3013 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3014 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3017 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3019 if (! primary_func_tmpl_inst
3020 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3021 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
3024 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3027 /* Return true iff the function parameter PARAM_DECL was expanded
3028 from the function parameter pack PACK. */
3031 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3033 if (DECL_ARTIFICIAL (param_decl
)
3034 || !function_parameter_pack_p (pack
))
3037 /* The parameter pack and its pack arguments have the same
3039 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3042 /* Determine whether ARGS describes a variadic template args list,
3043 i.e., one that is terminated by a template argument pack. */
3046 template_args_variadic_p (tree args
)
3051 if (args
== NULL_TREE
)
3054 args
= INNERMOST_TEMPLATE_ARGS (args
);
3055 nargs
= TREE_VEC_LENGTH (args
);
3060 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3062 return ARGUMENT_PACK_P (last_parm
);
3065 /* Generate a new name for the parameter pack name NAME (an
3066 IDENTIFIER_NODE) that incorporates its */
3069 make_ith_pack_parameter_name (tree name
, int i
)
3071 /* Munge the name to include the parameter index. */
3072 #define NUMBUF_LEN 128
3073 char numbuf
[NUMBUF_LEN
];
3077 if (name
== NULL_TREE
)
3079 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3080 newname_len
= IDENTIFIER_LENGTH (name
)
3081 + strlen (numbuf
) + 2;
3082 newname
= (char*)alloca (newname_len
);
3083 snprintf (newname
, newname_len
,
3084 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3085 return get_identifier (newname
);
3088 /* Return true if T is a primary function, class or alias template
3092 primary_template_instantiation_p (const_tree t
)
3097 if (TREE_CODE (t
) == FUNCTION_DECL
)
3098 return DECL_LANG_SPECIFIC (t
)
3099 && DECL_TEMPLATE_INSTANTIATION (t
)
3100 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
3101 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3102 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
3103 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
3104 else if (alias_template_specialization_p (t
))
3109 /* Return true if PARM is a template template parameter. */
3112 template_template_parameter_p (const_tree parm
)
3114 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3117 /* Return true iff PARM is a DECL representing a type template
3121 template_type_parameter_p (const_tree parm
)
3124 && (TREE_CODE (parm
) == TYPE_DECL
3125 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3126 && DECL_TEMPLATE_PARM_P (parm
));
3129 /* Return the template parameters of T if T is a
3130 primary template instantiation, NULL otherwise. */
3133 get_primary_template_innermost_parameters (const_tree t
)
3135 tree parms
= NULL
, template_info
= NULL
;
3137 if ((template_info
= get_template_info (t
))
3138 && primary_template_instantiation_p (t
))
3139 parms
= INNERMOST_TEMPLATE_PARMS
3140 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3145 /* Return the template parameters of the LEVELth level from the full list
3146 of template parameters PARMS. */
3149 get_template_parms_at_level (tree parms
, int level
)
3153 || TREE_CODE (parms
) != TREE_LIST
3154 || level
> TMPL_PARMS_DEPTH (parms
))
3157 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3158 if (TMPL_PARMS_DEPTH (p
) == level
)
3164 /* Returns the template arguments of T if T is a template instantiation,
3168 get_template_innermost_arguments (const_tree t
)
3170 tree args
= NULL
, template_info
= NULL
;
3172 if ((template_info
= get_template_info (t
))
3173 && TI_ARGS (template_info
))
3174 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3179 /* Return the argument pack elements of T if T is a template argument pack,
3183 get_template_argument_pack_elems (const_tree t
)
3185 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3186 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3189 return ARGUMENT_PACK_ARGS (t
);
3192 /* Structure used to track the progress of find_parameter_packs_r. */
3193 struct find_parameter_pack_data
3195 /* TREE_LIST that will contain all of the parameter packs found by
3197 tree
* parameter_packs
;
3199 /* Set of AST nodes that have been visited by the traversal. */
3200 hash_set
<tree
> *visited
;
3203 /* Identifies all of the argument packs that occur in a template
3204 argument and appends them to the TREE_LIST inside DATA, which is a
3205 find_parameter_pack_data structure. This is a subroutine of
3206 make_pack_expansion and uses_parameter_packs. */
3208 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3211 struct find_parameter_pack_data
* ppd
=
3212 (struct find_parameter_pack_data
*)data
;
3213 bool parameter_pack_p
= false;
3215 /* Handle type aliases/typedefs. */
3216 if (TYPE_ALIAS_P (t
))
3218 if (TYPE_TEMPLATE_INFO (t
))
3219 cp_walk_tree (&TYPE_TI_ARGS (t
),
3220 &find_parameter_packs_r
,
3226 /* Identify whether this is a parameter pack or not. */
3227 switch (TREE_CODE (t
))
3229 case TEMPLATE_PARM_INDEX
:
3230 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3231 parameter_pack_p
= true;
3234 case TEMPLATE_TYPE_PARM
:
3235 t
= TYPE_MAIN_VARIANT (t
);
3236 case TEMPLATE_TEMPLATE_PARM
:
3237 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3238 parameter_pack_p
= true;
3243 if (DECL_PACK_P (t
))
3245 /* We don't want to walk into the type of a PARM_DECL,
3246 because we don't want to see the type parameter pack. */
3248 parameter_pack_p
= true;
3252 /* Look through a lambda capture proxy to the field pack. */
3254 if (DECL_HAS_VALUE_EXPR_P (t
))
3256 tree v
= DECL_VALUE_EXPR (t
);
3258 &find_parameter_packs_r
,
3262 else if (variable_template_specialization_p (t
))
3264 cp_walk_tree (&DECL_TI_ARGS (t
),
3265 find_parameter_packs_r
,
3272 parameter_pack_p
= true;
3275 /* Not a parameter pack. */
3279 if (parameter_pack_p
)
3281 /* Add this parameter pack to the list. */
3282 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3286 cp_walk_tree (&TYPE_CONTEXT (t
),
3287 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3289 /* This switch statement will return immediately if we don't find a
3291 switch (TREE_CODE (t
))
3293 case TEMPLATE_PARM_INDEX
:
3296 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3297 /* Check the template itself. */
3298 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3299 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3300 /* Check the template arguments. */
3301 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3306 case TEMPLATE_TYPE_PARM
:
3307 case TEMPLATE_TEMPLATE_PARM
:
3314 if (TYPE_PTRMEMFUNC_P (t
))
3320 if (TYPE_TEMPLATE_INFO (t
))
3321 cp_walk_tree (&TYPE_TI_ARGS (t
),
3322 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3329 cp_walk_tree (&TREE_TYPE (t
),
3330 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3334 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3339 case TYPE_PACK_EXPANSION
:
3340 case EXPR_PACK_EXPANSION
:
3345 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3350 case IDENTIFIER_NODE
:
3351 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3363 /* Determines if the expression or type T uses any parameter packs. */
3365 uses_parameter_packs (tree t
)
3367 tree parameter_packs
= NULL_TREE
;
3368 struct find_parameter_pack_data ppd
;
3369 ppd
.parameter_packs
= ¶meter_packs
;
3370 ppd
.visited
= new hash_set
<tree
>;
3371 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3373 return parameter_packs
!= NULL_TREE
;
3376 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3377 representation a base-class initializer into a parameter pack
3378 expansion. If all goes well, the resulting node will be an
3379 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3382 make_pack_expansion (tree arg
)
3385 tree parameter_packs
= NULL_TREE
;
3386 bool for_types
= false;
3387 struct find_parameter_pack_data ppd
;
3389 if (!arg
|| arg
== error_mark_node
)
3392 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
3394 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3395 class initializer. In this case, the TREE_PURPOSE will be a
3396 _TYPE node (representing the base class expansion we're
3397 initializing) and the TREE_VALUE will be a TREE_LIST
3398 containing the initialization arguments.
3400 The resulting expansion looks somewhat different from most
3401 expansions. Rather than returning just one _EXPANSION, we
3402 return a TREE_LIST whose TREE_PURPOSE is a
3403 TYPE_PACK_EXPANSION containing the bases that will be
3404 initialized. The TREE_VALUE will be identical to the
3405 original TREE_VALUE, which is a list of arguments that will
3406 be passed to each base. We do not introduce any new pack
3407 expansion nodes into the TREE_VALUE (although it is possible
3408 that some already exist), because the TREE_PURPOSE and
3409 TREE_VALUE all need to be expanded together with the same
3410 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3411 resulting TREE_PURPOSE will mention the parameter packs in
3412 both the bases and the arguments to the bases. */
3415 tree parameter_packs
= NULL_TREE
;
3417 /* Determine which parameter packs will be used by the base
3419 ppd
.visited
= new hash_set
<tree
>;
3420 ppd
.parameter_packs
= ¶meter_packs
;
3421 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3424 if (parameter_packs
== NULL_TREE
)
3426 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3428 return error_mark_node
;
3431 if (TREE_VALUE (arg
) != void_type_node
)
3433 /* Collect the sets of parameter packs used in each of the
3434 initialization arguments. */
3435 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3437 /* Determine which parameter packs will be expanded in this
3439 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3446 /* Create the pack expansion type for the base type. */
3447 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3448 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3449 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3451 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3452 they will rarely be compared to anything. */
3453 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3455 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3458 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3461 /* Build the PACK_EXPANSION_* node. */
3463 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3464 : make_node (EXPR_PACK_EXPANSION
);
3465 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3466 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3468 /* Propagate type and const-expression information. */
3469 TREE_TYPE (result
) = TREE_TYPE (arg
);
3470 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3473 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3474 they will rarely be compared to anything. */
3475 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3477 /* Determine which parameter packs will be expanded. */
3478 ppd
.parameter_packs
= ¶meter_packs
;
3479 ppd
.visited
= new hash_set
<tree
>;
3480 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3483 /* Make sure we found some parameter packs. */
3484 if (parameter_packs
== NULL_TREE
)
3487 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3489 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3490 return error_mark_node
;
3492 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3494 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3499 /* Checks T for any "bare" parameter packs, which have not yet been
3500 expanded, and issues an error if any are found. This operation can
3501 only be done on full expressions or types (e.g., an expression
3502 statement, "if" condition, etc.), because we could have expressions like:
3504 foo(f(g(h(args)))...)
3506 where "args" is a parameter pack. check_for_bare_parameter_packs
3507 should not be called for the subexpressions args, h(args),
3508 g(h(args)), or f(g(h(args))), because we would produce erroneous
3511 Returns TRUE and emits an error if there were bare parameter packs,
3512 returns FALSE otherwise. */
3514 check_for_bare_parameter_packs (tree t
)
3516 tree parameter_packs
= NULL_TREE
;
3517 struct find_parameter_pack_data ppd
;
3519 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3522 if (TREE_CODE (t
) == TYPE_DECL
)
3525 ppd
.parameter_packs
= ¶meter_packs
;
3526 ppd
.visited
= new hash_set
<tree
>;
3527 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3530 if (parameter_packs
)
3532 error ("parameter packs not expanded with %<...%>:");
3533 while (parameter_packs
)
3535 tree pack
= TREE_VALUE (parameter_packs
);
3536 tree name
= NULL_TREE
;
3538 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3539 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3540 name
= TYPE_NAME (pack
);
3541 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3542 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3544 name
= DECL_NAME (pack
);
3547 inform (input_location
, " %qD", name
);
3549 inform (input_location
, " <anonymous>");
3551 parameter_packs
= TREE_CHAIN (parameter_packs
);
3560 /* Expand any parameter packs that occur in the template arguments in
3563 expand_template_argument_pack (tree args
)
3565 tree result_args
= NULL_TREE
;
3566 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3567 int num_result_args
= -1;
3568 int non_default_args_count
= -1;
3570 /* First, determine if we need to expand anything, and the number of
3571 slots we'll need. */
3572 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3574 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3575 if (arg
== NULL_TREE
)
3577 if (ARGUMENT_PACK_P (arg
))
3579 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3580 if (num_result_args
< 0)
3581 num_result_args
= in_arg
+ num_packed
;
3583 num_result_args
+= num_packed
;
3587 if (num_result_args
>= 0)
3592 /* If no expansion is necessary, we're done. */
3593 if (num_result_args
< 0)
3596 /* Expand arguments. */
3597 result_args
= make_tree_vec (num_result_args
);
3598 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3599 non_default_args_count
=
3600 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3601 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3603 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3604 if (ARGUMENT_PACK_P (arg
))
3606 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3607 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3608 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3609 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3610 if (non_default_args_count
> 0)
3611 non_default_args_count
+= num_packed
- 1;
3615 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3619 if (non_default_args_count
>= 0)
3620 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3624 /* Checks if DECL shadows a template parameter.
3626 [temp.local]: A template-parameter shall not be redeclared within its
3627 scope (including nested scopes).
3629 Emits an error and returns TRUE if the DECL shadows a parameter,
3630 returns FALSE otherwise. */
3633 check_template_shadow (tree decl
)
3637 /* If we're not in a template, we can't possibly shadow a template
3639 if (!current_template_parms
)
3642 /* Figure out what we're shadowing. */
3643 if (TREE_CODE (decl
) == OVERLOAD
)
3644 decl
= OVL_CURRENT (decl
);
3645 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3647 /* If there's no previous binding for this name, we're not shadowing
3648 anything, let alone a template parameter. */
3652 /* If we're not shadowing a template parameter, we're done. Note
3653 that OLDDECL might be an OVERLOAD (or perhaps even an
3654 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3656 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3659 /* We check for decl != olddecl to avoid bogus errors for using a
3660 name inside a class. We check TPFI to avoid duplicate errors for
3661 inline member templates. */
3663 || (DECL_TEMPLATE_PARM_P (decl
)
3664 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
3667 /* Don't complain about the injected class name, as we've already
3668 complained about the class itself. */
3669 if (DECL_SELF_REFERENCE_P (decl
))
3672 if (DECL_TEMPLATE_PARM_P (decl
))
3673 error ("declaration of template parameter %q+D shadows "
3674 "template parameter", decl
);
3676 error ("declaration of %q+#D shadows template parameter", decl
);
3677 inform (DECL_SOURCE_LOCATION (olddecl
),
3678 "template parameter %qD declared here", olddecl
);
3682 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3683 ORIG_LEVEL, DECL, and TYPE. */
3686 build_template_parm_index (int index
,
3692 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3693 TEMPLATE_PARM_IDX (t
) = index
;
3694 TEMPLATE_PARM_LEVEL (t
) = level
;
3695 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3696 TEMPLATE_PARM_DECL (t
) = decl
;
3697 TREE_TYPE (t
) = type
;
3698 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3699 TREE_READONLY (t
) = TREE_READONLY (decl
);
3704 /* Find the canonical type parameter for the given template type
3705 parameter. Returns the canonical type parameter, which may be TYPE
3706 if no such parameter existed. */
3709 canonical_type_parameter (tree type
)
3712 int idx
= TEMPLATE_TYPE_IDX (type
);
3713 if (!canonical_template_parms
)
3714 vec_alloc (canonical_template_parms
, idx
+1);
3716 while (canonical_template_parms
->length () <= (unsigned)idx
)
3717 vec_safe_push (canonical_template_parms
, NULL_TREE
);
3719 list
= (*canonical_template_parms
)[idx
];
3720 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3721 list
= TREE_CHAIN (list
);
3724 return TREE_VALUE (list
);
3727 (*canonical_template_parms
)[idx
]
3728 = tree_cons (NULL_TREE
, type
,
3729 (*canonical_template_parms
)[idx
]);
3734 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3735 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3736 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3737 new one is created. */
3740 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3741 tsubst_flags_t complain
)
3743 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3744 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3745 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3746 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3748 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3751 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3752 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3753 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3754 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3755 DECL_ARTIFICIAL (decl
) = 1;
3756 SET_DECL_TEMPLATE_PARM_P (decl
);
3758 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3759 TEMPLATE_PARM_LEVEL (index
) - levels
,
3760 TEMPLATE_PARM_ORIG_LEVEL (index
),
3762 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3763 TEMPLATE_PARM_PARAMETER_PACK (t
)
3764 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3766 /* Template template parameters need this. */
3767 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3769 DECL_TEMPLATE_RESULT (decl
)
3770 = build_decl (DECL_SOURCE_LOCATION (decl
),
3771 TYPE_DECL
, DECL_NAME (decl
), type
);
3772 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl
)) = true;
3773 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3774 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
3778 return TEMPLATE_PARM_DESCENDANTS (index
);
3781 /* Process information from new template parameter PARM and append it
3782 to the LIST being built. This new parameter is a non-type
3783 parameter iff IS_NON_TYPE is true. This new parameter is a
3784 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3788 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
3789 bool is_non_type
, bool is_parameter_pack
)
3795 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3796 defval
= TREE_PURPOSE (parm
);
3800 tree p
= tree_last (list
);
3802 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3805 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3806 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3808 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3816 parm
= TREE_VALUE (parm
);
3818 SET_DECL_TEMPLATE_PARM_P (parm
);
3820 if (TREE_TYPE (parm
) != error_mark_node
)
3824 The top-level cv-qualifiers on the template-parameter are
3825 ignored when determining its type. */
3826 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3827 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3828 TREE_TYPE (parm
) = error_mark_node
;
3829 else if (uses_parameter_packs (TREE_TYPE (parm
))
3830 && !is_parameter_pack
3831 /* If we're in a nested template parameter list, the template
3832 template parameter could be a parameter pack. */
3833 && processing_template_parmlist
== 1)
3835 /* This template parameter is not a parameter pack, but it
3836 should be. Complain about "bare" parameter packs. */
3837 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3839 /* Recover by calling this a parameter pack. */
3840 is_parameter_pack
= true;
3844 /* A template parameter is not modifiable. */
3845 TREE_CONSTANT (parm
) = 1;
3846 TREE_READONLY (parm
) = 1;
3847 decl
= build_decl (parm_loc
,
3848 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3849 TREE_CONSTANT (decl
) = 1;
3850 TREE_READONLY (decl
) = 1;
3851 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3852 = build_template_parm_index (idx
, processing_template_decl
,
3853 processing_template_decl
,
3854 decl
, TREE_TYPE (parm
));
3856 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3857 = is_parameter_pack
;
3862 parm
= TREE_VALUE (TREE_VALUE (parm
));
3864 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3866 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3867 /* This is for distinguishing between real templates and template
3868 template parameters */
3869 TREE_TYPE (parm
) = t
;
3870 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3875 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3876 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3877 decl
= build_decl (parm_loc
,
3878 TYPE_DECL
, parm
, t
);
3881 TYPE_NAME (t
) = decl
;
3882 TYPE_STUB_DECL (t
) = decl
;
3884 TEMPLATE_TYPE_PARM_INDEX (t
)
3885 = build_template_parm_index (idx
, processing_template_decl
,
3886 processing_template_decl
,
3887 decl
, TREE_TYPE (parm
));
3888 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3889 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3891 DECL_ARTIFICIAL (decl
) = 1;
3892 SET_DECL_TEMPLATE_PARM_P (decl
);
3894 parm
= build_tree_list (defval
, parm
);
3895 return chainon (list
, parm
);
3898 /* The end of a template parameter list has been reached. Process the
3899 tree list into a parameter vector, converting each parameter into a more
3900 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3904 end_template_parm_list (tree parms
)
3908 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3910 /* Pop the dummy parameter level and add the real one. */
3911 current_template_parms
= TREE_CHAIN (current_template_parms
);
3913 current_template_parms
3914 = tree_cons (size_int (processing_template_decl
),
3915 saved_parmlist
, current_template_parms
);
3917 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3919 next
= TREE_CHAIN (parm
);
3920 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3921 TREE_CHAIN (parm
) = NULL_TREE
;
3924 --processing_template_parmlist
;
3926 return saved_parmlist
;
3929 /* end_template_decl is called after a template declaration is seen. */
3932 end_template_decl (void)
3934 reset_specialization ();
3936 if (! processing_template_decl
)
3939 /* This matches the pushlevel in begin_template_parm_list. */
3942 --processing_template_decl
;
3943 current_template_parms
= TREE_CHAIN (current_template_parms
);
3946 /* Takes a TREE_LIST representing a template parameter and convert it
3947 into an argument suitable to be passed to the type substitution
3948 functions. Note that If the TREE_LIST contains an error_mark
3949 node, the returned argument is error_mark_node. */
3952 template_parm_to_arg (tree t
)
3956 || TREE_CODE (t
) != TREE_LIST
)
3959 if (error_operand_p (TREE_VALUE (t
)))
3960 return error_mark_node
;
3964 if (TREE_CODE (t
) == TYPE_DECL
3965 || TREE_CODE (t
) == TEMPLATE_DECL
)
3969 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3971 /* Turn this argument into a TYPE_ARGUMENT_PACK
3972 with a single element, which expands T. */
3973 tree vec
= make_tree_vec (1);
3974 #ifdef ENABLE_CHECKING
3975 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3976 (vec
, TREE_VEC_LENGTH (vec
));
3978 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3980 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
3981 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3986 t
= DECL_INITIAL (t
);
3988 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3990 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3991 with a single element, which expands T. */
3992 tree vec
= make_tree_vec (1);
3993 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3994 #ifdef ENABLE_CHECKING
3995 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3996 (vec
, TREE_VEC_LENGTH (vec
));
3998 t
= convert_from_reference (t
);
3999 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4001 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4002 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4003 TREE_TYPE (t
) = type
;
4006 t
= convert_from_reference (t
);
4011 /* Given a set of template parameters, return them as a set of template
4012 arguments. The template parameters are represented as a TREE_VEC, in
4013 the form documented in cp-tree.h for template arguments. */
4016 template_parms_to_args (tree parms
)
4019 tree args
= NULL_TREE
;
4020 int length
= TMPL_PARMS_DEPTH (parms
);
4023 /* If there is only one level of template parameters, we do not
4024 create a TREE_VEC of TREE_VECs. Instead, we return a single
4025 TREE_VEC containing the arguments. */
4027 args
= make_tree_vec (length
);
4029 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4031 tree a
= copy_node (TREE_VALUE (header
));
4034 TREE_TYPE (a
) = NULL_TREE
;
4035 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4036 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4038 #ifdef ENABLE_CHECKING
4039 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4043 TREE_VEC_ELT (args
, --l
) = a
;
4051 /* Within the declaration of a template, return the currently active
4052 template parameters as an argument TREE_VEC. */
4055 current_template_args (void)
4057 return template_parms_to_args (current_template_parms
);
4060 /* Update the declared TYPE by doing any lookups which were thought to be
4061 dependent, but are not now that we know the SCOPE of the declarator. */
4064 maybe_update_decl_type (tree orig_type
, tree scope
)
4066 tree type
= orig_type
;
4068 if (type
== NULL_TREE
)
4071 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4072 type
= TREE_TYPE (type
);
4074 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4075 && dependent_type_p (type
)
4076 /* Don't bother building up the args in this case. */
4077 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4079 /* tsubst in the args corresponding to the template parameters,
4080 including auto if present. Most things will be unchanged, but
4081 make_typename_type and tsubst_qualified_id will resolve
4082 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4083 tree args
= current_template_args ();
4084 tree auto_node
= type_uses_auto (type
);
4088 tree auto_vec
= make_tree_vec (1);
4089 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4090 args
= add_to_template_args (args
, auto_vec
);
4092 pushed
= push_scope (scope
);
4093 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4098 if (type
== error_mark_node
)
4101 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4103 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4106 type
= TYPE_NAME (type
);
4111 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4112 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
4113 a member template. Used by push_template_decl below. */
4116 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4118 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4119 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4120 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4121 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4122 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4127 struct template_parm_data
4129 /* The level of the template parameters we are currently
4133 /* The index of the specialization argument we are currently
4137 /* An array whose size is the number of template parameters. The
4138 elements are nonzero if the parameter has been used in any one
4139 of the arguments processed so far. */
4142 /* An array whose size is the number of template arguments. The
4143 elements are nonzero if the argument makes use of template
4144 parameters of this level. */
4145 int* arg_uses_template_parms
;
4148 /* Subroutine of push_template_decl used to see if each template
4149 parameter in a partial specialization is used in the explicit
4150 argument list. If T is of the LEVEL given in DATA (which is
4151 treated as a template_parm_data*), then DATA->PARMS is marked
4155 mark_template_parm (tree t
, void* data
)
4159 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4161 template_parm_level_and_index (t
, &level
, &idx
);
4163 if (level
== tpd
->level
)
4165 tpd
->parms
[idx
] = 1;
4166 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4169 /* Return zero so that for_each_template_parm will continue the
4170 traversal of the tree; we want to mark *every* template parm. */
4174 /* Process the partial specialization DECL. */
4177 process_partial_specialization (tree decl
)
4179 tree type
= TREE_TYPE (decl
);
4180 tree tinfo
= get_template_info (decl
);
4181 tree maintmpl
= TI_TEMPLATE (tinfo
);
4182 tree specargs
= TI_ARGS (tinfo
);
4183 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4184 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4187 int nargs
= TREE_VEC_LENGTH (inner_args
);
4190 bool did_error_intro
= false;
4191 struct template_parm_data tpd
;
4192 struct template_parm_data tpd2
;
4194 gcc_assert (current_template_parms
);
4196 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4197 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4199 /* We check that each of the template parameters given in the
4200 partial specialization is used in the argument list to the
4201 specialization. For example:
4203 template <class T> struct S;
4204 template <class T> struct S<T*>;
4206 The second declaration is OK because `T*' uses the template
4207 parameter T, whereas
4209 template <class T> struct S<int>;
4211 is no good. Even trickier is:
4222 The S2<T> declaration is actually invalid; it is a
4223 full-specialization. Of course,
4226 struct S2<T (*)(U)>;
4228 or some such would have been OK. */
4229 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4230 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4231 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4233 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4234 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4235 for (i
= 0; i
< nargs
; ++i
)
4237 tpd
.current_arg
= i
;
4238 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4239 &mark_template_parm
,
4242 /*include_nondeduced_p=*/false);
4244 for (i
= 0; i
< ntparms
; ++i
)
4245 if (tpd
.parms
[i
] == 0)
4247 /* One of the template parms was not used in a deduced context in the
4249 if (!did_error_intro
)
4251 error ("template parameters not deducible in "
4252 "partial specialization:");
4253 did_error_intro
= true;
4256 inform (input_location
, " %qD",
4257 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4260 if (did_error_intro
)
4261 return error_mark_node
;
4263 /* [temp.class.spec]
4265 The argument list of the specialization shall not be identical to
4266 the implicit argument list of the primary template. */
4268 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
4269 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
)))
4270 error ("partial specialization %qD does not specialize "
4271 "any template arguments", decl
);
4273 /* A partial specialization that replaces multiple parameters of the
4274 primary template with a pack expansion is less specialized for those
4276 if (nargs
< DECL_NTPARMS (maintmpl
))
4278 error ("partial specialization is not more specialized than the "
4279 "primary template because it replaces multiple parameters "
4280 "with a pack expansion");
4281 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4285 /* [temp.class.spec]
4287 A partially specialized non-type argument expression shall not
4288 involve template parameters of the partial specialization except
4289 when the argument expression is a simple identifier.
4291 The type of a template parameter corresponding to a specialized
4292 non-type argument shall not be dependent on a parameter of the
4295 Also, we verify that pack expansions only occur at the
4296 end of the argument list. */
4297 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4299 for (i
= 0; i
< nargs
; ++i
)
4301 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4302 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4303 tree packed_args
= NULL_TREE
;
4306 if (ARGUMENT_PACK_P (arg
))
4308 /* Extract the arguments from the argument pack. We'll be
4309 iterating over these in the following loop. */
4310 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4311 len
= TREE_VEC_LENGTH (packed_args
);
4314 for (j
= 0; j
< len
; j
++)
4317 /* Get the Jth argument in the parameter pack. */
4318 arg
= TREE_VEC_ELT (packed_args
, j
);
4320 if (PACK_EXPANSION_P (arg
))
4322 /* Pack expansions must come at the end of the
4324 if ((packed_args
&& j
< len
- 1)
4325 || (!packed_args
&& i
< nargs
- 1))
4327 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4328 error ("parameter pack argument %qE must be at the "
4329 "end of the template argument list", arg
);
4331 error ("parameter pack argument %qT must be at the "
4332 "end of the template argument list", arg
);
4336 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4337 /* We only care about the pattern. */
4338 arg
= PACK_EXPANSION_PATTERN (arg
);
4340 if (/* These first two lines are the `non-type' bit. */
4342 && TREE_CODE (arg
) != TEMPLATE_DECL
4343 /* This next two lines are the `argument expression is not just a
4344 simple identifier' condition and also the `specialized
4345 non-type argument' bit. */
4346 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4347 && !(REFERENCE_REF_P (arg
)
4348 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4350 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4351 || (packed_args
&& uses_template_parms (arg
)))
4352 error ("template argument %qE involves template parameter(s)",
4356 /* Look at the corresponding template parameter,
4357 marking which template parameters its type depends
4359 tree type
= TREE_TYPE (parm
);
4363 /* We haven't yet initialized TPD2. Do so now. */
4364 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4365 /* The number of parameters here is the number in the
4366 main template, which, as checked in the assertion
4368 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4370 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4373 /* Mark the template parameters. But this time, we're
4374 looking for the template parameters of the main
4375 template, not in the specialization. */
4376 tpd2
.current_arg
= i
;
4377 tpd2
.arg_uses_template_parms
[i
] = 0;
4378 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4379 for_each_template_parm (type
,
4380 &mark_template_parm
,
4383 /*include_nondeduced_p=*/false);
4385 if (tpd2
.arg_uses_template_parms
[i
])
4387 /* The type depended on some template parameters.
4388 If they are fully specialized in the
4389 specialization, that's OK. */
4392 for (j
= 0; j
< nargs
; ++j
)
4393 if (tpd2
.parms
[j
] != 0
4394 && tpd
.arg_uses_template_parms
[j
])
4397 error_n (input_location
, count
,
4398 "type %qT of template argument %qE depends "
4399 "on a template parameter",
4400 "type %qT of template argument %qE depends "
4401 "on template parameters",
4410 /* We should only get here once. */
4411 if (TREE_CODE (decl
) == TYPE_DECL
)
4412 gcc_assert (!COMPLETE_TYPE_P (type
));
4414 tree tmpl
= build_template_decl (decl
, current_template_parms
,
4415 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4416 TREE_TYPE (tmpl
) = type
;
4417 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4418 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4419 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4420 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4422 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4423 = tree_cons (specargs
, tmpl
,
4424 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4425 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4427 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4428 inst
= TREE_CHAIN (inst
))
4430 tree instance
= TREE_VALUE (inst
);
4431 if (TYPE_P (instance
)
4432 ? (COMPLETE_TYPE_P (instance
)
4433 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
4434 : DECL_TEMPLATE_INSTANTIATION (instance
))
4436 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
4437 if (spec
&& TREE_VALUE (spec
) == tmpl
)
4439 tree inst_decl
= (DECL_P (instance
)
4440 ? instance
: TYPE_NAME (instance
));
4441 permerror (input_location
,
4442 "partial specialization of %qD after instantiation "
4443 "of %qD", decl
, inst_decl
);
4451 /* PARM is a template parameter of some form; return the corresponding
4452 TEMPLATE_PARM_INDEX. */
4455 get_template_parm_index (tree parm
)
4457 if (TREE_CODE (parm
) == PARM_DECL
4458 || TREE_CODE (parm
) == CONST_DECL
)
4459 parm
= DECL_INITIAL (parm
);
4460 else if (TREE_CODE (parm
) == TYPE_DECL
4461 || TREE_CODE (parm
) == TEMPLATE_DECL
)
4462 parm
= TREE_TYPE (parm
);
4463 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
4464 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
4465 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
4466 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
4467 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
4471 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4472 parameter packs used by the template parameter PARM. */
4475 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
4477 /* A type parm can't refer to another parm. */
4478 if (TREE_CODE (parm
) == TYPE_DECL
)
4480 else if (TREE_CODE (parm
) == PARM_DECL
)
4482 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
4487 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
4489 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
4490 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
4491 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
4494 /* PARM is a template parameter pack. Return any parameter packs used in
4495 its type or the type of any of its template parameters. If there are
4496 any such packs, it will be instantiated into a fixed template parameter
4497 list by partial instantiation rather than be fully deduced. */
4500 fixed_parameter_pack_p (tree parm
)
4502 /* This can only be true in a member template. */
4503 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
4505 /* This can only be true for a parameter pack. */
4506 if (!template_parameter_pack_p (parm
))
4508 /* A type parm can't refer to another parm. */
4509 if (TREE_CODE (parm
) == TYPE_DECL
)
4512 tree parameter_packs
= NULL_TREE
;
4513 struct find_parameter_pack_data ppd
;
4514 ppd
.parameter_packs
= ¶meter_packs
;
4515 ppd
.visited
= new hash_set
<tree
>;
4517 fixed_parameter_pack_p_1 (parm
, &ppd
);
4520 return parameter_packs
;
4523 /* Check that a template declaration's use of default arguments and
4524 parameter packs is not invalid. Here, PARMS are the template
4525 parameters. IS_PRIMARY is true if DECL is the thing declared by
4526 a primary template. IS_PARTIAL is true if DECL is a partial
4529 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4530 declaration (but not a definition); 1 indicates a declaration, 2
4531 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4532 emitted for extraneous default arguments.
4534 Returns TRUE if there were no errors found, FALSE otherwise. */
4537 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4538 bool is_partial
, int is_friend_decl
)
4541 int last_level_to_check
;
4543 bool no_errors
= true;
4547 A default template-argument shall not be specified in a
4548 function template declaration or a function template definition, nor
4549 in the template-parameter-list of the definition of a member of a
4552 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
4553 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
4554 /* You can't have a function template declaration in a local
4555 scope, nor you can you define a member of a class template in a
4559 if ((TREE_CODE (decl
) == TYPE_DECL
4561 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4562 || (TREE_CODE (decl
) == FUNCTION_DECL
4563 && LAMBDA_FUNCTION_P (decl
)))
4564 /* A lambda doesn't have an explicit declaration; don't complain
4565 about the parms of the enclosing class. */
4568 if (current_class_type
4569 && !TYPE_BEING_DEFINED (current_class_type
)
4570 && DECL_LANG_SPECIFIC (decl
)
4571 && DECL_DECLARES_FUNCTION_P (decl
)
4572 /* If this is either a friend defined in the scope of the class
4573 or a member function. */
4574 && (DECL_FUNCTION_MEMBER_P (decl
)
4575 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4576 : DECL_FRIEND_CONTEXT (decl
)
4577 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4579 /* And, if it was a member function, it really was defined in
4580 the scope of the class. */
4581 && (!DECL_FUNCTION_MEMBER_P (decl
)
4582 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4583 /* We already checked these parameters when the template was
4584 declared, so there's no need to do it again now. This function
4585 was defined in class scope, but we're processing its body now
4586 that the class is complete. */
4589 /* Core issue 226 (C++0x only): the following only applies to class
4592 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
4596 If a template-parameter has a default template-argument, all
4597 subsequent template-parameters shall have a default
4598 template-argument supplied. */
4599 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4601 tree inner_parms
= TREE_VALUE (parm_level
);
4602 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4603 int seen_def_arg_p
= 0;
4606 for (i
= 0; i
< ntparms
; ++i
)
4608 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4610 if (parm
== error_mark_node
)
4613 if (TREE_PURPOSE (parm
))
4615 else if (seen_def_arg_p
4616 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4618 error ("no default argument for %qD", TREE_VALUE (parm
));
4619 /* For better subsequent error-recovery, we indicate that
4620 there should have been a default argument. */
4621 TREE_PURPOSE (parm
) = error_mark_node
;
4624 else if (!is_partial
4626 /* Don't complain about an enclosing partial
4628 && parm_level
== parms
4629 && TREE_CODE (decl
) == TYPE_DECL
4631 && template_parameter_pack_p (TREE_VALUE (parm
))
4632 /* A fixed parameter pack will be partially
4633 instantiated into a fixed length list. */
4634 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
4636 /* A primary class template can only have one
4637 parameter pack, at the end of the template
4640 error ("parameter pack %q+D must be at the end of the"
4641 " template parameter list", TREE_VALUE (parm
));
4643 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4651 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4655 /* For an ordinary class template, default template arguments are
4656 allowed at the innermost level, e.g.:
4657 template <class T = int>
4659 but, in a partial specialization, they're not allowed even
4660 there, as we have in [temp.class.spec]:
4662 The template parameter list of a specialization shall not
4663 contain default template argument values.
4665 So, for a partial specialization, or for a function template
4666 (in C++98/C++03), we look at all of them. */
4669 /* But, for a primary class template that is not a partial
4670 specialization we look at all template parameters except the
4672 parms
= TREE_CHAIN (parms
);
4674 /* Figure out what error message to issue. */
4675 if (is_friend_decl
== 2)
4676 msg
= G_("default template arguments may not be used in function template "
4677 "friend re-declaration");
4678 else if (is_friend_decl
)
4679 msg
= G_("default template arguments may not be used in function template "
4680 "friend declarations");
4681 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4682 msg
= G_("default template arguments may not be used in function templates "
4683 "without -std=c++11 or -std=gnu++11");
4684 else if (is_partial
)
4685 msg
= G_("default template arguments may not be used in "
4686 "partial specializations");
4688 msg
= G_("default argument for template parameter for class enclosing %qD");
4690 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4691 /* If we're inside a class definition, there's no need to
4692 examine the parameters to the class itself. On the one
4693 hand, they will be checked when the class is defined, and,
4694 on the other, default arguments are valid in things like:
4695 template <class T = double>
4696 struct S { template <class U> void f(U); };
4697 Here the default argument for `S' has no bearing on the
4698 declaration of `f'. */
4699 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4701 /* Check everything. */
4702 last_level_to_check
= 0;
4704 for (parm_level
= parms
;
4705 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4706 parm_level
= TREE_CHAIN (parm_level
))
4708 tree inner_parms
= TREE_VALUE (parm_level
);
4712 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4713 for (i
= 0; i
< ntparms
; ++i
)
4715 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4718 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4723 if (is_friend_decl
== 2)
4730 /* Clear out the default argument so that we are not
4732 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4736 /* At this point, if we're still interested in issuing messages,
4737 they must apply to classes surrounding the object declared. */
4739 msg
= G_("default argument for template parameter for class "
4746 /* Worker for push_template_decl_real, called via
4747 for_each_template_parm. DATA is really an int, indicating the
4748 level of the parameters we are interested in. If T is a template
4749 parameter of that level, return nonzero. */
4752 template_parm_this_level_p (tree t
, void* data
)
4754 int this_level
= *(int *)data
;
4757 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4758 level
= TEMPLATE_PARM_LEVEL (t
);
4760 level
= TEMPLATE_TYPE_LEVEL (t
);
4761 return level
== this_level
;
4764 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4765 parameters given by current_template_args, or reuses a
4766 previously existing one, if appropriate. Returns the DECL, or an
4767 equivalent one, if it is replaced via a call to duplicate_decls.
4769 If IS_FRIEND is true, DECL is a friend declaration. */
4772 push_template_decl_real (tree decl
, bool is_friend
)
4780 int new_template_p
= 0;
4781 /* True if the template is a member template, in the sense of
4783 bool member_template_p
= false;
4785 if (decl
== error_mark_node
|| !current_template_parms
)
4786 return error_mark_node
;
4788 /* See if this is a partial specialization. */
4789 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
4790 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4791 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
4793 && DECL_LANG_SPECIFIC (decl
)
4794 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4795 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
4797 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4801 /* For a friend, we want the context of the friend function, not
4802 the type of which it is a friend. */
4803 ctx
= CP_DECL_CONTEXT (decl
);
4804 else if (CP_DECL_CONTEXT (decl
)
4805 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4806 /* In the case of a virtual function, we want the class in which
4808 ctx
= CP_DECL_CONTEXT (decl
);
4810 /* Otherwise, if we're currently defining some class, the DECL
4811 is assumed to be a member of the class. */
4812 ctx
= current_scope ();
4814 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4817 if (!DECL_CONTEXT (decl
))
4818 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4820 /* See if this is a primary template. */
4821 if (is_friend
&& ctx
4822 && uses_template_parms_level (ctx
, processing_template_decl
))
4823 /* A friend template that specifies a class context, i.e.
4824 template <typename T> friend void A<T>::f();
4827 else if (TREE_CODE (decl
) == TYPE_DECL
4828 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4831 is_primary
= template_parm_scope_p ();
4835 if (DECL_CLASS_SCOPE_P (decl
))
4836 member_template_p
= true;
4837 if (TREE_CODE (decl
) == TYPE_DECL
4838 && anon_aggrname_p (DECL_NAME (decl
)))
4840 error ("template class without a name");
4841 return error_mark_node
;
4843 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4845 if (member_template_p
)
4847 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
4848 error ("member template %qD may not have virt-specifiers", decl
);
4850 if (DECL_DESTRUCTOR_P (decl
))
4854 A destructor shall not be a member template. */
4855 error ("destructor %qD declared as member template", decl
);
4856 return error_mark_node
;
4858 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4859 && (!prototype_p (TREE_TYPE (decl
))
4860 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4861 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4862 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4863 == void_list_node
)))
4865 /* [basic.stc.dynamic.allocation]
4867 An allocation function can be a function
4868 template. ... Template allocation functions shall
4869 have two or more parameters. */
4870 error ("invalid template declaration of %qD", decl
);
4871 return error_mark_node
;
4874 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4875 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4877 else if (TREE_CODE (decl
) == TYPE_DECL
4878 && TYPE_DECL_ALIAS_P (decl
))
4879 /* alias-declaration */
4880 gcc_assert (!DECL_ARTIFICIAL (decl
));
4881 else if (VAR_P (decl
))
4882 /* C++14 variable template. */;
4885 error ("template declaration of %q#D", decl
);
4886 return error_mark_node
;
4890 /* Check to see that the rules regarding the use of default
4891 arguments are not being violated. */
4892 check_default_tmpl_args (decl
, current_template_parms
,
4893 is_primary
, is_partial
, /*is_friend_decl=*/0);
4895 /* Ensure that there are no parameter packs in the type of this
4896 declaration that have not been expanded. */
4897 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4899 /* Check each of the arguments individually to see if there are
4900 any bare parameter packs. */
4901 tree type
= TREE_TYPE (decl
);
4902 tree arg
= DECL_ARGUMENTS (decl
);
4903 tree argtype
= TYPE_ARG_TYPES (type
);
4905 while (arg
&& argtype
)
4907 if (!DECL_PACK_P (arg
)
4908 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4910 /* This is a PARM_DECL that contains unexpanded parameter
4911 packs. We have already complained about this in the
4912 check_for_bare_parameter_packs call, so just replace
4913 these types with ERROR_MARK_NODE. */
4914 TREE_TYPE (arg
) = error_mark_node
;
4915 TREE_VALUE (argtype
) = error_mark_node
;
4918 arg
= DECL_CHAIN (arg
);
4919 argtype
= TREE_CHAIN (argtype
);
4922 /* Check for bare parameter packs in the return type and the
4923 exception specifiers. */
4924 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4925 /* Errors were already issued, set return type to int
4926 as the frontend doesn't expect error_mark_node as
4928 TREE_TYPE (type
) = integer_type_node
;
4929 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4930 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4932 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
4933 && TYPE_DECL_ALIAS_P (decl
))
4934 ? DECL_ORIGINAL_TYPE (decl
)
4935 : TREE_TYPE (decl
)))
4937 TREE_TYPE (decl
) = error_mark_node
;
4938 return error_mark_node
;
4942 return process_partial_specialization (decl
);
4944 args
= current_template_args ();
4947 || TREE_CODE (ctx
) == FUNCTION_DECL
4948 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4949 || (TREE_CODE (decl
) == TYPE_DECL
4950 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4951 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4953 if (DECL_LANG_SPECIFIC (decl
)
4954 && DECL_TEMPLATE_INFO (decl
)
4955 && DECL_TI_TEMPLATE (decl
))
4956 tmpl
= DECL_TI_TEMPLATE (decl
);
4957 /* If DECL is a TYPE_DECL for a class-template, then there won't
4958 be DECL_LANG_SPECIFIC. The information equivalent to
4959 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4960 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4961 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4962 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4964 /* Since a template declaration already existed for this
4965 class-type, we must be redeclaring it here. Make sure
4966 that the redeclaration is valid. */
4967 redeclare_class_template (TREE_TYPE (decl
),
4968 current_template_parms
);
4969 /* We don't need to create a new TEMPLATE_DECL; just use the
4970 one we already had. */
4971 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4975 tmpl
= build_template_decl (decl
, current_template_parms
,
4979 if (DECL_LANG_SPECIFIC (decl
)
4980 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4982 /* A specialization of a member template of a template
4984 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4985 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4986 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4992 tree a
, t
, current
, parms
;
4994 tree tinfo
= get_template_info (decl
);
4998 error ("template definition of non-template %q#D", decl
);
4999 return error_mark_node
;
5002 tmpl
= TI_TEMPLATE (tinfo
);
5004 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5005 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5006 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5007 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5011 /* The declaration is a specialization of a member
5012 template, declared outside the class. Therefore, the
5013 innermost template arguments will be NULL, so we
5014 replace them with the arguments determined by the
5015 earlier call to check_explicit_specialization. */
5016 args
= DECL_TI_ARGS (decl
);
5019 = build_template_decl (decl
, current_template_parms
,
5021 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5022 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5023 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5024 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5025 DECL_TEMPLATE_INFO (new_tmpl
)
5026 = build_template_info (tmpl
, args
);
5028 register_specialization (new_tmpl
,
5029 most_general_template (tmpl
),
5035 /* Make sure the template headers we got make sense. */
5037 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5038 i
= TMPL_PARMS_DEPTH (parms
);
5039 if (TMPL_ARGS_DEPTH (args
) != i
)
5041 error ("expected %d levels of template parms for %q#D, got %d",
5042 i
, decl
, TMPL_ARGS_DEPTH (args
));
5043 DECL_INTERFACE_KNOWN (decl
) = 1;
5044 return error_mark_node
;
5047 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5049 a
= TMPL_ARGS_LEVEL (args
, i
);
5050 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5052 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5054 if (current
== decl
)
5055 error ("got %d template parameters for %q#D",
5056 TREE_VEC_LENGTH (a
), decl
);
5058 error ("got %d template parameters for %q#T",
5059 TREE_VEC_LENGTH (a
), current
);
5060 error (" but %d required", TREE_VEC_LENGTH (t
));
5061 /* Avoid crash in import_export_decl. */
5062 DECL_INTERFACE_KNOWN (decl
) = 1;
5063 return error_mark_node
;
5066 if (current
== decl
)
5068 else if (current
== NULL_TREE
)
5069 /* Can happen in erroneous input. */
5072 current
= get_containing_scope (current
);
5075 /* Check that the parms are used in the appropriate qualifying scopes
5076 in the declarator. */
5077 if (!comp_template_args
5079 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5082 template arguments to %qD do not match original template %qD",
5083 decl
, DECL_TEMPLATE_RESULT (tmpl
));
5084 if (!uses_template_parms (TI_ARGS (tinfo
)))
5085 inform (input_location
, "use template<> for an explicit specialization");
5086 /* Avoid crash in import_export_decl. */
5087 DECL_INTERFACE_KNOWN (decl
) = 1;
5088 return error_mark_node
;
5092 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5093 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5095 /* Push template declarations for global functions and types. Note
5096 that we do not try to push a global template friend declared in a
5097 template class; such a thing may well depend on the template
5098 parameters of the class. */
5099 if (new_template_p
&& !ctx
5100 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5102 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5103 if (tmpl
== error_mark_node
)
5104 return error_mark_node
;
5106 /* Hide template friend classes that haven't been declared yet. */
5107 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5109 DECL_ANTICIPATED (tmpl
) = 1;
5110 DECL_FRIEND_P (tmpl
) = 1;
5116 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5119 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5120 if (DECL_CONV_FN_P (tmpl
))
5122 int depth
= TMPL_PARMS_DEPTH (parms
);
5124 /* It is a conversion operator. See if the type converted to
5125 depends on innermost template operands. */
5127 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
5129 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
5132 /* Give template template parms a DECL_CONTEXT of the template
5133 for which they are a parameter. */
5134 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5135 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5137 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5138 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5139 DECL_CONTEXT (parm
) = tmpl
;
5142 if (TREE_CODE (decl
) == TYPE_DECL
5143 && TYPE_DECL_ALIAS_P (decl
)
5144 && complex_alias_template_p (tmpl
))
5145 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5148 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5149 back to its most general template. If TMPL is a specialization,
5150 ARGS may only have the innermost set of arguments. Add the missing
5151 argument levels if necessary. */
5152 if (DECL_TEMPLATE_INFO (tmpl
))
5153 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5155 info
= build_template_info (tmpl
, args
);
5157 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5158 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5161 if (is_primary
&& !DECL_LANG_SPECIFIC (decl
))
5162 retrofit_lang_decl (decl
);
5163 if (DECL_LANG_SPECIFIC (decl
))
5164 DECL_TEMPLATE_INFO (decl
) = info
;
5167 if (flag_implicit_templates
5169 && TREE_PUBLIC (decl
)
5170 && VAR_OR_FUNCTION_DECL_P (decl
))
5171 /* Set DECL_COMDAT on template instantiations; if we force
5172 them to be emitted by explicit instantiation or -frepo,
5173 mark_needed will tell cgraph to do the right thing. */
5174 DECL_COMDAT (decl
) = true;
5176 return DECL_TEMPLATE_RESULT (tmpl
);
5180 push_template_decl (tree decl
)
5182 return push_template_decl_real (decl
, false);
5185 /* FN is an inheriting constructor that inherits from the constructor
5186 template INHERITED; turn FN into a constructor template with a matching
5190 add_inherited_template_parms (tree fn
, tree inherited
)
5193 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5194 inner_parms
= copy_node (inner_parms
);
5196 = tree_cons (size_int (processing_template_decl
+ 1),
5197 inner_parms
, current_template_parms
);
5198 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5199 tree args
= template_parms_to_args (parms
);
5200 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5201 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5202 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5203 DECL_ARTIFICIAL (tmpl
) = true;
5204 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5208 /* Called when a class template TYPE is redeclared with the indicated
5209 template PARMS, e.g.:
5211 template <class T> struct S;
5212 template <class T> struct S {}; */
5215 redeclare_class_template (tree type
, tree parms
)
5221 if (!TYPE_TEMPLATE_INFO (type
))
5223 error ("%qT is not a template type", type
);
5227 tmpl
= TYPE_TI_TEMPLATE (type
);
5228 if (!PRIMARY_TEMPLATE_P (tmpl
))
5229 /* The type is nested in some template class. Nothing to worry
5230 about here; there are no new template parameters for the nested
5236 error ("template specifiers not specified in declaration of %qD",
5241 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5242 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5244 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5246 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5247 "redeclared with %d template parameter",
5248 "redeclared with %d template parameters",
5249 TREE_VEC_LENGTH (parms
));
5250 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
5251 "previous declaration %qD used %d template parameter",
5252 "previous declaration %qD used %d template parameters",
5253 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5257 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5264 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5265 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5268 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5269 if (error_operand_p (tmpl_parm
))
5272 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5273 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5274 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5276 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5278 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5279 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5280 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5281 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5282 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5283 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5284 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5285 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5286 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5288 error ("template parameter %q+#D", tmpl_parm
);
5289 error ("redeclared here as %q#D", parm
);
5293 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5295 /* We have in [temp.param]:
5297 A template-parameter may not be given default arguments
5298 by two different declarations in the same scope. */
5299 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5300 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5301 "original definition appeared here");
5305 if (parm_default
!= NULL_TREE
)
5306 /* Update the previous template parameters (which are the ones
5307 that will really count) with the new default value. */
5308 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5309 else if (tmpl_default
!= NULL_TREE
)
5310 /* Update the new parameters, too; they'll be used as the
5311 parameters for any members. */
5312 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5314 /* Give each template template parm in this redeclaration a
5315 DECL_CONTEXT of the template for which they are a parameter. */
5316 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5318 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
5319 DECL_CONTEXT (parm
) = tmpl
;
5326 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5327 to be used when the caller has already checked
5328 (processing_template_decl
5329 && !instantiation_dependent_expression_p (expr)
5330 && potential_constant_expression (expr))
5331 and cleared processing_template_decl. */
5334 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
5336 return tsubst_copy_and_build (expr
,
5339 /*in_decl=*/NULL_TREE
,
5340 /*function_p=*/false,
5341 /*integral_constant_expression_p=*/true);
5344 /* Simplify EXPR if it is a non-dependent expression. Returns the
5345 (possibly simplified) expression. */
5348 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5350 if (expr
== NULL_TREE
)
5353 /* If we're in a template, but EXPR isn't value dependent, simplify
5354 it. We're supposed to treat:
5356 template <typename T> void f(T[1 + 1]);
5357 template <typename T> void f(T[2]);
5359 as two declarations of the same function, for example. */
5360 if (processing_template_decl
5361 && !instantiation_dependent_expression_p (expr
)
5362 && potential_constant_expression (expr
))
5364 processing_template_decl_sentinel s
;
5365 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
5371 instantiate_non_dependent_expr (tree expr
)
5373 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
5376 /* True iff T is a specialization of a variable template. */
5379 variable_template_specialization_p (tree t
)
5381 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
5383 tree tmpl
= DECL_TI_TEMPLATE (t
);
5384 return variable_template_p (tmpl
);
5387 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5388 template declaration, or a TYPE_DECL for an alias declaration. */
5391 alias_type_or_template_p (tree t
)
5395 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5398 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5399 || DECL_ALIAS_TEMPLATE_P (t
));
5402 /* Return TRUE iff T is a specialization of an alias template. */
5405 alias_template_specialization_p (const_tree t
)
5407 /* It's an alias template specialization if it's an alias and its
5408 TYPE_NAME is a specialization of a primary template. */
5409 if (TYPE_ALIAS_P (t
))
5411 tree name
= TYPE_NAME (t
);
5412 if (DECL_LANG_SPECIFIC (name
))
5413 if (tree ti
= DECL_TEMPLATE_INFO (name
))
5415 tree tmpl
= TI_TEMPLATE (ti
);
5416 return PRIMARY_TEMPLATE_P (tmpl
);
5422 /* An alias template is complex from a SFINAE perspective if a template-id
5423 using that alias can be ill-formed when the expansion is not, as with
5424 the void_t template. We determine this by checking whether the
5425 expansion for the alias template uses all its template parameters. */
5427 struct uses_all_template_parms_data
5434 uses_all_template_parms_r (tree t
, void *data_
)
5436 struct uses_all_template_parms_data
&data
5437 = *(struct uses_all_template_parms_data
*)data_
;
5438 tree idx
= get_template_parm_index (t
);
5440 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
5441 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
5446 complex_alias_template_p (const_tree tmpl
)
5448 struct uses_all_template_parms_data data
;
5449 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5450 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5451 data
.level
= TMPL_PARMS_DEPTH (parms
);
5452 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
5453 data
.seen
= XALLOCAVEC (bool, len
);
5454 for (int i
= 0; i
< len
; ++i
)
5455 data
.seen
[i
] = false;
5457 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
5458 for (int i
= 0; i
< len
; ++i
)
5464 /* Return TRUE iff T is a specialization of a complex alias template with
5465 dependent template-arguments. */
5468 dependent_alias_template_spec_p (const_tree t
)
5470 return (alias_template_specialization_p (t
)
5471 && TEMPLATE_DECL_COMPLEX_ALIAS_P (DECL_TI_TEMPLATE (TYPE_NAME (t
)))
5472 && (any_dependent_template_arguments_p
5473 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (t
)))));
5476 /* Return the number of innermost template parameters in TMPL. */
5479 num_innermost_template_parms (tree tmpl
)
5481 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
5482 return TREE_VEC_LENGTH (parms
);
5485 /* Return either TMPL or another template that it is equivalent to under DR
5486 1286: An alias that just changes the name of a template is equivalent to
5487 the other template. */
5490 get_underlying_template (tree tmpl
)
5492 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
5493 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
5495 tree result
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5496 if (TYPE_TEMPLATE_INFO (result
))
5498 tree sub
= TYPE_TI_TEMPLATE (result
);
5499 if (PRIMARY_TEMPLATE_P (sub
)
5500 && (num_innermost_template_parms (tmpl
)
5501 == num_innermost_template_parms (sub
)))
5503 tree alias_args
= INNERMOST_TEMPLATE_ARGS
5504 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
5505 if (!comp_template_args (TYPE_TI_ARGS (result
), alias_args
))
5507 /* The alias type is equivalent to the pattern of the
5508 underlying template, so strip the alias. */
5518 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5519 must be a function or a pointer-to-function type, as specified
5520 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5521 and check that the resulting function has external linkage. */
5524 convert_nontype_argument_function (tree type
, tree expr
,
5525 tsubst_flags_t complain
)
5529 linkage_kind linkage
;
5531 fn
= instantiate_type (type
, fns
, tf_none
);
5532 if (fn
== error_mark_node
)
5533 return error_mark_node
;
5536 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5537 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5538 if (BASELINK_P (fn_no_ptr
))
5539 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5541 /* [temp.arg.nontype]/1
5543 A template-argument for a non-type, non-template template-parameter
5546 -- the address of an object or function with external [C++11: or
5547 internal] linkage. */
5549 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
5551 if (complain
& tf_error
)
5553 error ("%qE is not a valid template argument for type %qT",
5555 if (TYPE_PTR_P (type
))
5556 error ("it must be the address of a function with "
5557 "external linkage");
5559 error ("it must be the name of a function with "
5560 "external linkage");
5565 linkage
= decl_linkage (fn_no_ptr
);
5566 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
5568 if (complain
& tf_error
)
5570 if (cxx_dialect
>= cxx11
)
5571 error ("%qE is not a valid template argument for type %qT "
5572 "because %qD has no linkage",
5573 expr
, type
, fn_no_ptr
);
5575 error ("%qE is not a valid template argument for type %qT "
5576 "because %qD does not have external linkage",
5577 expr
, type
, fn_no_ptr
);
5585 /* Subroutine of convert_nontype_argument.
5586 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5587 Emit an error otherwise. */
5590 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5591 tsubst_flags_t complain
)
5594 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5596 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
5598 if (processing_template_decl
5599 && TREE_CODE (expr
) == ADDR_EXPR
5600 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
5602 if (complain
& tf_error
)
5604 error ("%qE is not a valid template argument for type %qT",
5606 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5611 /* Returns TRUE iff the address of OP is value-dependent.
5613 14.6.2.4 [temp.dep.temp]:
5614 A non-integral non-type template-argument is dependent if its type is
5615 dependent or it has either of the following forms
5618 and contains a nested-name-specifier which specifies a class-name that
5619 names a dependent type.
5621 We generalize this to just say that the address of a member of a
5622 dependent class is value-dependent; the above doesn't cover the
5623 address of a static data member named with an unqualified-id. */
5626 has_value_dependent_address (tree op
)
5628 /* We could use get_inner_reference here, but there's no need;
5629 this is only relevant for template non-type arguments, which
5630 can only be expressed as &id-expression. */
5633 tree ctx
= CP_DECL_CONTEXT (op
);
5634 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5641 /* The next set of functions are used for providing helpful explanatory
5642 diagnostics for failed overload resolution. Their messages should be
5643 indented by two spaces for consistency with the messages in
5647 unify_success (bool /*explain_p*/)
5653 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5656 inform (input_location
,
5657 " couldn't deduce template parameter %qD", parm
);
5662 unify_invalid (bool /*explain_p*/)
5668 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5671 inform (input_location
,
5672 " types %qT and %qT have incompatible cv-qualifiers",
5678 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5681 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5686 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
5689 inform (input_location
,
5690 " template parameter %qD is not a parameter pack, but "
5697 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
5700 inform (input_location
,
5701 " template argument %qE does not match "
5702 "pointer-to-member constant %qE",
5708 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
5711 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
5716 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
5719 inform (input_location
,
5720 " inconsistent parameter pack deduction with %qT and %qT",
5726 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
5731 inform (input_location
,
5732 " deduced conflicting types for parameter %qT (%qT and %qT)",
5733 parm
, first
, second
);
5735 inform (input_location
,
5736 " deduced conflicting values for non-type parameter "
5737 "%qE (%qE and %qE)", parm
, first
, second
);
5743 unify_vla_arg (bool explain_p
, tree arg
)
5746 inform (input_location
,
5747 " variable-sized array type %qT is not "
5748 "a valid template argument",
5754 unify_method_type_error (bool explain_p
, tree arg
)
5757 inform (input_location
,
5758 " member function type %qT is not a valid template argument",
5764 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
5769 inform_n (input_location
, wanted
,
5770 " candidate expects at least %d argument, %d provided",
5771 " candidate expects at least %d arguments, %d provided",
5774 inform_n (input_location
, wanted
,
5775 " candidate expects %d argument, %d provided",
5776 " candidate expects %d arguments, %d provided",
5783 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
5785 return unify_arity (explain_p
, have
, wanted
);
5789 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
5790 bool least_p
= false)
5792 return unify_arity (explain_p
, have
, wanted
, least_p
);
5796 unify_arg_conversion (bool explain_p
, tree to_type
,
5797 tree from_type
, tree arg
)
5800 inform (EXPR_LOC_OR_LOC (arg
, input_location
),
5801 " cannot convert %qE (type %qT) to type %qT",
5802 arg
, from_type
, to_type
);
5807 unify_no_common_base (bool explain_p
, enum template_base_result r
,
5808 tree parm
, tree arg
)
5813 case tbr_ambiguous_baseclass
:
5814 inform (input_location
, " %qT is an ambiguous base class of %qT",
5818 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
5825 unify_inconsistent_template_template_parameters (bool explain_p
)
5828 inform (input_location
,
5829 " template parameters of a template template argument are "
5830 "inconsistent with other deduced template arguments");
5835 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
5838 inform (input_location
,
5839 " can't deduce a template for %qT from non-template type %qT",
5845 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
5848 inform (input_location
,
5849 " template argument %qE does not match %qD", arg
, parm
);
5854 unify_overload_resolution_failure (bool explain_p
, tree arg
)
5857 inform (input_location
,
5858 " could not resolve address from overloaded function %qE",
5863 /* Attempt to convert the non-type template parameter EXPR to the
5864 indicated TYPE. If the conversion is successful, return the
5865 converted value. If the conversion is unsuccessful, return
5866 NULL_TREE if we issued an error message, or error_mark_node if we
5867 did not. We issue error messages for out-and-out bad template
5868 parameters, but not simply because the conversion failed, since we
5869 might be just trying to do argument deduction. Both TYPE and EXPR
5870 must be non-dependent.
5872 The conversion follows the special rules described in
5873 [temp.arg.nontype], and it is much more strict than an implicit
5876 This function is called twice for each template argument (see
5877 lookup_template_class for a more accurate description of this
5878 problem). This means that we need to handle expressions which
5879 are not valid in a C++ source, but can be created from the
5880 first call (for instance, casts to perform conversions). These
5881 hacks can go away after we fix the double coercion problem. */
5884 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5888 /* Detect immediately string literals as invalid non-type argument.
5889 This special-case is not needed for correctness (we would easily
5890 catch this later), but only to provide better diagnostic for this
5891 common user mistake. As suggested by DR 100, we do not mention
5892 linkage issues in the diagnostic as this is not the point. */
5893 /* FIXME we're making this OK. */
5894 if (TREE_CODE (expr
) == STRING_CST
)
5896 if (complain
& tf_error
)
5897 error ("%qE is not a valid template argument for type %qT "
5898 "because string literals can never be used in this context",
5903 /* Add the ADDR_EXPR now for the benefit of
5904 value_dependent_expression_p. */
5905 if (TYPE_PTROBV_P (type
)
5906 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
5908 expr
= decay_conversion (expr
, complain
);
5909 if (expr
== error_mark_node
)
5910 return error_mark_node
;
5913 /* If we are in a template, EXPR may be non-dependent, but still
5914 have a syntactic, rather than semantic, form. For example, EXPR
5915 might be a SCOPE_REF, rather than the VAR_DECL to which the
5916 SCOPE_REF refers. Preserving the qualifying scope is necessary
5917 so that access checking can be performed when the template is
5918 instantiated -- but here we need the resolved form so that we can
5919 convert the argument. */
5920 bool non_dep
= false;
5921 if (TYPE_REF_OBJ_P (type
)
5922 && has_value_dependent_address (expr
))
5923 /* If we want the address and it's value-dependent, don't fold. */;
5924 else if (!type_unknown_p (expr
)
5925 && processing_template_decl
5926 && !instantiation_dependent_expression_p (expr
)
5927 && potential_constant_expression (expr
))
5929 if (error_operand_p (expr
))
5930 return error_mark_node
;
5931 expr_type
= TREE_TYPE (expr
);
5932 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5933 expr
= mark_lvalue_use (expr
);
5935 expr
= mark_rvalue_use (expr
);
5937 /* If the argument is non-dependent, perform any conversions in
5938 non-dependent context as well. */
5939 processing_template_decl_sentinel
s (non_dep
);
5941 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
5943 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5944 to a non-type argument of "nullptr". */
5945 if (expr
== nullptr_node
&& TYPE_PTR_OR_PTRMEM_P (type
))
5946 expr
= convert (type
, expr
);
5948 /* In C++11, integral or enumeration non-type template arguments can be
5949 arbitrary constant expressions. Pointer and pointer to
5950 member arguments can be general constant expressions that evaluate
5951 to a null value, but otherwise still need to be of a specific form. */
5952 if (cxx_dialect
>= cxx11
)
5954 if (TREE_CODE (expr
) == PTRMEM_CST
)
5955 /* A PTRMEM_CST is already constant, and a valid template
5956 argument for a parameter of pointer to member type, we just want
5957 to leave it in that form rather than lower it to a
5959 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5960 expr
= maybe_constant_value (expr
);
5961 else if (TYPE_PTR_OR_PTRMEM_P (type
))
5963 tree folded
= maybe_constant_value (expr
);
5964 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
5965 : null_member_pointer_value_p (folded
))
5970 /* HACK: Due to double coercion, we can get a
5971 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5972 which is the tree that we built on the first call (see
5973 below when coercing to reference to object or to reference to
5974 function). We just strip everything and get to the arg.
5975 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5977 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5979 tree probe_type
, probe
= expr
;
5980 if (REFERENCE_REF_P (probe
))
5981 probe
= TREE_OPERAND (probe
, 0);
5982 probe_type
= TREE_TYPE (probe
);
5983 if (TREE_CODE (probe
) == NOP_EXPR
)
5985 /* ??? Maybe we could use convert_from_reference here, but we
5986 would need to relax its constraints because the NOP_EXPR
5987 could actually change the type to something more cv-qualified,
5988 and this is not folded by convert_from_reference. */
5989 tree addr
= TREE_OPERAND (probe
, 0);
5990 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
5991 && TREE_CODE (addr
) == ADDR_EXPR
5992 && TYPE_PTR_P (TREE_TYPE (addr
))
5993 && (same_type_ignoring_top_level_qualifiers_p
5994 (TREE_TYPE (probe_type
),
5995 TREE_TYPE (TREE_TYPE (addr
)))))
5997 expr
= TREE_OPERAND (addr
, 0);
5998 expr_type
= TREE_TYPE (probe_type
);
6003 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
6004 parameter is a pointer to object, through decay and
6005 qualification conversion. Let's strip everything. */
6006 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
6010 if (TREE_CODE (probe
) == ADDR_EXPR
6011 && TYPE_PTR_P (TREE_TYPE (probe
)))
6013 /* Skip the ADDR_EXPR only if it is part of the decay for
6014 an array. Otherwise, it is part of the original argument
6015 in the source code. */
6016 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe
, 0))) == ARRAY_TYPE
)
6017 probe
= TREE_OPERAND (probe
, 0);
6019 expr_type
= TREE_TYPE (expr
);
6023 /* [temp.arg.nontype]/5, bullet 1
6025 For a non-type template-parameter of integral or enumeration type,
6026 integral promotions (_conv.prom_) and integral conversions
6027 (_conv.integral_) are applied. */
6028 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6030 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
6031 t
= maybe_constant_value (t
);
6032 if (t
!= error_mark_node
)
6035 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
6036 return error_mark_node
;
6038 /* Notice that there are constant expressions like '4 % 0' which
6039 do not fold into integer constants. */
6040 if (TREE_CODE (expr
) != INTEGER_CST
)
6042 if (complain
& tf_error
)
6044 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
6045 if (processing_template_decl
6046 && !require_potential_constant_expression (expr
))
6048 expr
= cxx_constant_value (expr
);
6049 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
6050 inform (EXPR_LOC_OR_LOC (expr
, input_location
),
6051 "in template argument for type %qT ", type
);
6052 if (expr
== error_mark_node
)
6054 /* else cxx_constant_value complained but gave us
6055 a real constant, so go ahead. */
6056 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
6062 /* Avoid typedef problems. */
6063 if (TREE_TYPE (expr
) != type
)
6064 expr
= fold_convert (type
, expr
);
6066 /* [temp.arg.nontype]/5, bullet 2
6068 For a non-type template-parameter of type pointer to object,
6069 qualification conversions (_conv.qual_) and the array-to-pointer
6070 conversion (_conv.array_) are applied. */
6071 else if (TYPE_PTROBV_P (type
))
6073 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6075 A template-argument for a non-type, non-template template-parameter
6076 shall be one of: [...]
6078 -- the name of a non-type template-parameter;
6079 -- the address of an object or function with external linkage, [...]
6080 expressed as "& id-expression" where the & is optional if the name
6081 refers to a function or array, or if the corresponding
6082 template-parameter is a reference.
6084 Here, we do not care about functions, as they are invalid anyway
6085 for a parameter of type pointer-to-object. */
6087 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
6088 /* Non-type template parameters are OK. */
6090 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6091 /* Null pointer values are OK in C++11. */;
6092 else if (TREE_CODE (expr
) != ADDR_EXPR
6093 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
6097 if (complain
& tf_error
)
6098 error ("%qD is not a valid template argument "
6099 "because %qD is a variable, not the address of "
6100 "a variable", expr
, expr
);
6103 if (POINTER_TYPE_P (expr_type
))
6105 if (complain
& tf_error
)
6106 error ("%qE is not a valid template argument for %qT "
6107 "because it is not the address of a variable",
6111 /* Other values, like integer constants, might be valid
6112 non-type arguments of some other type. */
6113 return error_mark_node
;
6119 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
6120 ? TREE_OPERAND (expr
, 0) : expr
);
6123 if (complain
& tf_error
)
6124 error ("%qE is not a valid template argument of type %qT "
6125 "because %qE is not a variable", expr
, type
, decl
);
6128 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6130 if (complain
& tf_error
)
6131 error ("%qE is not a valid template argument of type %qT "
6132 "because %qD does not have external linkage",
6136 else if (cxx_dialect
>= cxx11
&& decl_linkage (decl
) == lk_none
)
6138 if (complain
& tf_error
)
6139 error ("%qE is not a valid template argument of type %qT "
6140 "because %qD has no linkage", expr
, type
, decl
);
6145 expr
= decay_conversion (expr
, complain
);
6146 if (expr
== error_mark_node
)
6147 return error_mark_node
;
6149 expr
= perform_qualification_conversions (type
, expr
);
6150 if (expr
== error_mark_node
)
6151 return error_mark_node
;
6153 /* [temp.arg.nontype]/5, bullet 3
6155 For a non-type template-parameter of type reference to object, no
6156 conversions apply. The type referred to by the reference may be more
6157 cv-qualified than the (otherwise identical) type of the
6158 template-argument. The template-parameter is bound directly to the
6159 template-argument, which must be an lvalue. */
6160 else if (TYPE_REF_OBJ_P (type
))
6162 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
6164 return error_mark_node
;
6166 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
6168 if (complain
& tf_error
)
6169 error ("%qE is not a valid template argument for type %qT "
6170 "because of conflicts in cv-qualification", expr
, type
);
6174 if (!real_lvalue_p (expr
))
6176 if (complain
& tf_error
)
6177 error ("%qE is not a valid template argument for type %qT "
6178 "because it is not an lvalue", expr
, type
);
6182 /* [temp.arg.nontype]/1
6184 A template-argument for a non-type, non-template template-parameter
6185 shall be one of: [...]
6187 -- the address of an object or function with external linkage. */
6188 if (INDIRECT_REF_P (expr
)
6189 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
6191 expr
= TREE_OPERAND (expr
, 0);
6194 if (complain
& tf_error
)
6195 error ("%q#D is not a valid template argument for type %qT "
6196 "because a reference variable does not have a constant "
6197 "address", expr
, type
);
6204 if (complain
& tf_error
)
6205 error ("%qE is not a valid template argument for type %qT "
6206 "because it is not an object with external linkage",
6211 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
6213 if (complain
& tf_error
)
6214 error ("%qE is not a valid template argument for type %qT "
6215 "because object %qD has not external linkage",
6220 expr
= build_nop (type
, build_address (expr
));
6222 /* [temp.arg.nontype]/5, bullet 4
6224 For a non-type template-parameter of type pointer to function, only
6225 the function-to-pointer conversion (_conv.func_) is applied. If the
6226 template-argument represents a set of overloaded functions (or a
6227 pointer to such), the matching function is selected from the set
6229 else if (TYPE_PTRFN_P (type
))
6231 /* If the argument is a template-id, we might not have enough
6232 context information to decay the pointer. */
6233 if (!type_unknown_p (expr_type
))
6235 expr
= decay_conversion (expr
, complain
);
6236 if (expr
== error_mark_node
)
6237 return error_mark_node
;
6240 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6241 /* Null pointer values are OK in C++11. */
6242 return perform_qualification_conversions (type
, expr
);
6244 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6245 if (!expr
|| expr
== error_mark_node
)
6248 /* [temp.arg.nontype]/5, bullet 5
6250 For a non-type template-parameter of type reference to function, no
6251 conversions apply. If the template-argument represents a set of
6252 overloaded functions, the matching function is selected from the set
6254 else if (TYPE_REFFN_P (type
))
6256 if (TREE_CODE (expr
) == ADDR_EXPR
)
6258 if (complain
& tf_error
)
6260 error ("%qE is not a valid template argument for type %qT "
6261 "because it is a pointer", expr
, type
);
6262 inform (input_location
, "try using %qE instead",
6263 TREE_OPERAND (expr
, 0));
6268 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6269 if (!expr
|| expr
== error_mark_node
)
6272 expr
= build_nop (type
, build_address (expr
));
6274 /* [temp.arg.nontype]/5, bullet 6
6276 For a non-type template-parameter of type pointer to member function,
6277 no conversions apply. If the template-argument represents a set of
6278 overloaded member functions, the matching member function is selected
6279 from the set (_over.over_). */
6280 else if (TYPE_PTRMEMFUNC_P (type
))
6282 expr
= instantiate_type (type
, expr
, tf_none
);
6283 if (expr
== error_mark_node
)
6284 return error_mark_node
;
6286 /* [temp.arg.nontype] bullet 1 says the pointer to member
6287 expression must be a pointer-to-member constant. */
6288 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6289 return error_mark_node
;
6291 /* There is no way to disable standard conversions in
6292 resolve_address_of_overloaded_function (called by
6293 instantiate_type). It is possible that the call succeeded by
6294 converting &B::I to &D::I (where B is a base of D), so we need
6295 to reject this conversion here.
6297 Actually, even if there was a way to disable standard conversions,
6298 it would still be better to reject them here so that we can
6299 provide a superior diagnostic. */
6300 if (!same_type_p (TREE_TYPE (expr
), type
))
6302 if (complain
& tf_error
)
6304 error ("%qE is not a valid template argument for type %qT "
6305 "because it is of type %qT", expr
, type
,
6307 /* If we are just one standard conversion off, explain. */
6308 if (can_convert_standard (type
, TREE_TYPE (expr
), complain
))
6309 inform (input_location
,
6310 "standard conversions are not allowed in this context");
6315 /* [temp.arg.nontype]/5, bullet 7
6317 For a non-type template-parameter of type pointer to data member,
6318 qualification conversions (_conv.qual_) are applied. */
6319 else if (TYPE_PTRDATAMEM_P (type
))
6321 /* [temp.arg.nontype] bullet 1 says the pointer to member
6322 expression must be a pointer-to-member constant. */
6323 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6324 return error_mark_node
;
6326 expr
= perform_qualification_conversions (type
, expr
);
6327 if (expr
== error_mark_node
)
6330 else if (NULLPTR_TYPE_P (type
))
6332 if (expr
!= nullptr_node
)
6334 if (complain
& tf_error
)
6335 error ("%qE is not a valid template argument for type %qT "
6336 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
6341 /* A template non-type parameter must be one of the above. */
6345 /* Sanity check: did we actually convert the argument to the
6347 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6348 (type
, TREE_TYPE (expr
)));
6349 return convert_from_reference (expr
);
6352 /* Subroutine of coerce_template_template_parms, which returns 1 if
6353 PARM_PARM and ARG_PARM match using the rule for the template
6354 parameters of template template parameters. Both PARM and ARG are
6355 template parameters; the rest of the arguments are the same as for
6356 coerce_template_template_parms.
6359 coerce_template_template_parm (tree parm
,
6361 tsubst_flags_t complain
,
6365 if (arg
== NULL_TREE
|| error_operand_p (arg
)
6366 || parm
== NULL_TREE
|| error_operand_p (parm
))
6369 if (TREE_CODE (arg
) != TREE_CODE (parm
))
6372 switch (TREE_CODE (parm
))
6375 /* We encounter instantiations of templates like
6376 template <template <template <class> class> class TT>
6379 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6380 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6382 if (!coerce_template_template_parms
6383 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
6389 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
6390 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6391 /* Argument is a parameter pack but parameter is not. */
6396 /* The tsubst call is used to handle cases such as
6398 template <int> class C {};
6399 template <class T, template <T> class TT> class D {};
6402 i.e. the parameter list of TT depends on earlier parameters. */
6403 if (!uses_template_parms (TREE_TYPE (arg
)))
6405 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
6406 if (!uses_template_parms (t
)
6407 && !same_type_p (t
, TREE_TYPE (arg
)))
6411 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6412 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6413 /* Argument is a parameter pack but parameter is not. */
6426 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6427 template template parameters. Both PARM_PARMS and ARG_PARMS are
6428 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6431 Consider the example:
6432 template <class T> class A;
6433 template<template <class U> class TT> class B;
6435 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6436 the parameters to A, and OUTER_ARGS contains A. */
6439 coerce_template_template_parms (tree parm_parms
,
6441 tsubst_flags_t complain
,
6445 int nparms
, nargs
, i
;
6449 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
6450 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
6452 nparms
= TREE_VEC_LENGTH (parm_parms
);
6453 nargs
= TREE_VEC_LENGTH (arg_parms
);
6455 /* Determine whether we have a parameter pack at the end of the
6456 template template parameter's template parameter list. */
6457 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
6459 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
6461 if (error_operand_p (parm
))
6464 switch (TREE_CODE (parm
))
6468 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6473 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6483 && !(variadic_p
&& nargs
>= nparms
- 1))
6486 /* Check all of the template parameters except the parameter pack at
6487 the end (if any). */
6488 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6490 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6491 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6494 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6495 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6497 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6505 /* Check each of the template parameters in the template
6506 argument against the template parameter pack at the end of
6507 the template template parameter. */
6508 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6511 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6513 for (; i
< nargs
; ++i
)
6515 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6518 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6520 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6529 /* Verifies that the deduced template arguments (in TARGS) for the
6530 template template parameters (in TPARMS) represent valid bindings,
6531 by comparing the template parameter list of each template argument
6532 to the template parameter list of its corresponding template
6533 template parameter, in accordance with DR150. This
6534 routine can only be called after all template arguments have been
6535 deduced. It will return TRUE if all of the template template
6536 parameter bindings are okay, FALSE otherwise. */
6538 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6540 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6543 /* We're dealing with template parms in this process. */
6544 ++processing_template_decl
;
6546 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6548 for (i
= 0; i
< ntparms
; ++i
)
6550 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6551 tree targ
= TREE_VEC_ELT (targs
, i
);
6553 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6555 tree packed_args
= NULL_TREE
;
6558 if (ARGUMENT_PACK_P (targ
))
6560 /* Look inside the argument pack. */
6561 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6562 len
= TREE_VEC_LENGTH (packed_args
);
6565 for (idx
= 0; idx
< len
; ++idx
)
6567 tree targ_parms
= NULL_TREE
;
6570 /* Extract the next argument from the argument
6572 targ
= TREE_VEC_ELT (packed_args
, idx
);
6574 if (PACK_EXPANSION_P (targ
))
6575 /* Look at the pattern of the pack expansion. */
6576 targ
= PACK_EXPANSION_PATTERN (targ
);
6578 /* Extract the template parameters from the template
6580 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6581 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6582 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6583 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6585 /* Verify that we can coerce the template template
6586 parameters from the template argument to the template
6587 parameter. This requires an exact match. */
6589 && !coerce_template_template_parms
6590 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6605 --processing_template_decl
;
6609 /* Since type attributes aren't mangled, we need to strip them from
6610 template type arguments. */
6613 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6615 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6617 bool removed_attributes
= false;
6618 tree canon
= strip_typedefs (arg
, &removed_attributes
);
6619 if (removed_attributes
6620 && (complain
& tf_warning
))
6621 warning (0, "ignoring attributes on template argument %qT", arg
);
6625 /* Convert the indicated template ARG as necessary to match the
6626 indicated template PARM. Returns the converted ARG, or
6627 error_mark_node if the conversion was unsuccessful. Error and
6628 warning messages are issued under control of COMPLAIN. This
6629 conversion is for the Ith parameter in the parameter list. ARGS is
6630 the full set of template arguments deduced so far. */
6633 convert_template_argument (tree parm
,
6636 tsubst_flags_t complain
,
6642 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
6644 if (parm
== error_mark_node
)
6645 return error_mark_node
;
6647 if (TREE_CODE (arg
) == TREE_LIST
6648 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
6650 /* The template argument was the name of some
6651 member function. That's usually
6652 invalid, but static members are OK. In any
6653 case, grab the underlying fields/functions
6654 and issue an error later if required. */
6655 orig_arg
= TREE_VALUE (arg
);
6656 TREE_TYPE (arg
) = unknown_type_node
;
6661 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
6662 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
6663 || requires_tmpl_type
);
6665 /* When determining whether an argument pack expansion is a template,
6666 look at the pattern. */
6667 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
6668 arg
= PACK_EXPANSION_PATTERN (arg
);
6670 /* Deal with an injected-class-name used as a template template arg. */
6671 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
6673 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
6674 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6676 if (cxx_dialect
>= cxx11
)
6677 /* OK under DR 1004. */;
6678 else if (complain
& tf_warning_or_error
)
6679 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
6680 " used as template template argument", TYPE_NAME (arg
));
6681 else if (flag_pedantic_errors
)
6689 ((TREE_CODE (arg
) == TEMPLATE_DECL
6690 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
6691 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
6692 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6693 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
6696 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6697 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
6698 arg
= TYPE_STUB_DECL (arg
);
6700 is_type
= TYPE_P (arg
) || is_tmpl_type
;
6702 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
6703 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
6705 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
6707 if (complain
& tf_error
)
6708 error ("invalid use of destructor %qE as a type", orig_arg
);
6709 return error_mark_node
;
6712 permerror (input_location
,
6713 "to refer to a type member of a template parameter, "
6714 "use %<typename %E%>", orig_arg
);
6716 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
6717 TREE_OPERAND (arg
, 1),
6723 if (is_type
!= requires_type
)
6727 if (complain
& tf_error
)
6729 error ("type/value mismatch at argument %d in template "
6730 "parameter list for %qD",
6733 inform (input_location
,
6734 " expected a constant of type %qT, got %qT",
6736 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6737 else if (requires_tmpl_type
)
6738 inform (input_location
,
6739 " expected a class template, got %qE", orig_arg
);
6741 inform (input_location
,
6742 " expected a type, got %qE", orig_arg
);
6745 return error_mark_node
;
6747 if (is_tmpl_type
^ requires_tmpl_type
)
6749 if (in_decl
&& (complain
& tf_error
))
6751 error ("type/value mismatch at argument %d in template "
6752 "parameter list for %qD",
6755 inform (input_location
,
6756 " expected a type, got %qT", DECL_NAME (arg
));
6758 inform (input_location
,
6759 " expected a class template, got %qT", orig_arg
);
6761 return error_mark_node
;
6766 if (requires_tmpl_type
)
6768 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6770 else if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6771 /* The number of argument required is not known yet.
6772 Just accept it for now. */
6773 val
= TREE_TYPE (arg
);
6776 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6779 /* Strip alias templates that are equivalent to another
6781 arg
= get_underlying_template (arg
);
6782 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6784 if (coerce_template_template_parms (parmparm
, argparm
,
6790 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6792 if (val
!= error_mark_node
)
6794 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6795 val
= TREE_TYPE (val
);
6796 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6797 val
= make_pack_expansion (val
);
6802 if (in_decl
&& (complain
& tf_error
))
6804 error ("type/value mismatch at argument %d in "
6805 "template parameter list for %qD",
6807 inform (input_location
,
6808 " expected a template of type %qD, got %qT",
6812 val
= error_mark_node
;
6818 /* We only form one instance of each template specialization.
6819 Therefore, if we use a non-canonical variant (i.e., a
6820 typedef), any future messages referring to the type will use
6821 the typedef, which is confusing if those future uses do not
6822 themselves also use the typedef. */
6824 val
= canonicalize_type_argument (val
, complain
);
6828 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6830 if (invalid_nontype_parm_type_p (t
, complain
))
6831 return error_mark_node
;
6833 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6835 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6839 /* Not sure if this is reachable, but it doesn't hurt
6841 error ("type mismatch in nontype parameter pack");
6842 val
= error_mark_node
;
6845 else if (!dependent_template_arg_p (orig_arg
)
6846 && !uses_template_parms (t
))
6847 /* We used to call digest_init here. However, digest_init
6848 will report errors, which we don't want when complain
6849 is zero. More importantly, digest_init will try too
6850 hard to convert things: for example, `0' should not be
6851 converted to pointer type at this point according to
6852 the standard. Accepting this is not merely an
6853 extension, since deciding whether or not these
6854 conversions can occur is part of determining which
6855 function template to call, or whether a given explicit
6856 argument specification is valid. */
6857 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6860 bool removed_attr
= false;
6861 val
= strip_typedefs_expr (orig_arg
, &removed_attr
);
6864 if (val
== NULL_TREE
)
6865 val
= error_mark_node
;
6866 else if (val
== error_mark_node
&& (complain
& tf_error
))
6867 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6869 if (TREE_CODE (val
) == SCOPE_REF
)
6871 /* Strip typedefs from the SCOPE_REF. */
6872 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
6873 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
6875 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6876 QUALIFIED_NAME_IS_TEMPLATE (val
));
6883 /* Coerces the remaining template arguments in INNER_ARGS (from
6884 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6885 Returns the coerced argument pack. PARM_IDX is the position of this
6886 parameter in the template parameter list. ARGS is the original
6887 template argument list. */
6889 coerce_template_parameter_pack (tree parms
,
6897 tsubst_flags_t complain
)
6899 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6900 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6903 tree packed_parms
= NULL_TREE
;
6905 if (arg_idx
> nargs
)
6908 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
6910 /* When the template parameter is a non-type template parameter pack
6911 or template template parameter pack whose type or template
6912 parameters use parameter packs, we know exactly how many arguments
6913 we are looking for. Build a vector of the instantiated decls for
6914 these template parameters in PACKED_PARMS. */
6915 /* We can't use make_pack_expansion here because it would interpret a
6916 _DECL as a use rather than a declaration. */
6917 tree decl
= TREE_VALUE (parm
);
6918 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
6919 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
6920 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
6921 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
6923 TREE_VEC_LENGTH (args
)--;
6924 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
6925 TREE_VEC_LENGTH (args
)++;
6927 if (packed_parms
== error_mark_node
)
6928 return error_mark_node
;
6930 /* If we're doing a partial instantiation of a member template,
6931 verify that all of the types used for the non-type
6932 template parameter pack are, in fact, valid for non-type
6933 template parameters. */
6935 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6937 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
6938 for (j
= 0; j
< len
; ++j
)
6940 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
6941 if (invalid_nontype_parm_type_p (t
, complain
))
6942 return error_mark_node
;
6944 /* We don't know how many args we have yet, just
6945 use the unconverted ones for now. */
6949 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
6952 packed_args
= make_tree_vec (nargs
- arg_idx
);
6954 /* Convert the remaining arguments, which will be a part of the
6955 parameter pack "parm". */
6956 for (; arg_idx
< nargs
; ++arg_idx
)
6958 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6959 tree actual_parm
= TREE_VALUE (parm
);
6960 int pack_idx
= arg_idx
- parm_idx
;
6964 /* Once we've packed as many args as we have types, stop. */
6965 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
6967 else if (PACK_EXPANSION_P (arg
))
6968 /* We don't know how many args we have yet, just
6969 use the unconverted ones for now. */
6972 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
6975 if (arg
== error_mark_node
)
6977 if (complain
& tf_error
)
6978 error ("template argument %d is invalid", arg_idx
+ 1);
6981 arg
= convert_template_argument (actual_parm
,
6982 arg
, new_args
, complain
, parm_idx
,
6984 if (arg
== error_mark_node
)
6986 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
6989 if (arg_idx
- parm_idx
< TREE_VEC_LENGTH (packed_args
)
6990 && TREE_VEC_LENGTH (packed_args
) > 0)
6992 if (complain
& tf_error
)
6993 error ("wrong number of template arguments (%d, should be %d)",
6994 arg_idx
- parm_idx
, TREE_VEC_LENGTH (packed_args
));
6995 return error_mark_node
;
6998 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6999 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
7000 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
7003 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
7004 TREE_TYPE (argument_pack
)
7005 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
7006 TREE_CONSTANT (argument_pack
) = 1;
7009 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
7010 #ifdef ENABLE_CHECKING
7011 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
7012 TREE_VEC_LENGTH (packed_args
));
7014 return argument_pack
;
7017 /* Returns the number of pack expansions in the template argument vector
7021 pack_expansion_args_count (tree args
)
7026 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
7028 tree elt
= TREE_VEC_ELT (args
, i
);
7029 if (elt
&& PACK_EXPANSION_P (elt
))
7035 /* Convert all template arguments to their appropriate types, and
7036 return a vector containing the innermost resulting template
7037 arguments. If any error occurs, return error_mark_node. Error and
7038 warning messages are issued under control of COMPLAIN.
7040 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7041 for arguments not specified in ARGS. Otherwise, if
7042 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7043 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7044 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7048 coerce_template_parms (tree parms
,
7051 tsubst_flags_t complain
,
7052 bool require_all_args
,
7053 bool use_default_args
)
7055 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
7056 tree orig_inner_args
;
7059 tree new_inner_args
;
7060 int saved_unevaluated_operand
;
7061 int saved_inhibit_evaluation_warnings
;
7063 /* When used as a boolean value, indicates whether this is a
7064 variadic template parameter list. Since it's an int, we can also
7065 subtract it from nparms to get the number of non-variadic
7068 int variadic_args_p
= 0;
7069 int post_variadic_parms
= 0;
7071 /* Likewise for parameters with default arguments. */
7074 if (args
== error_mark_node
)
7075 return error_mark_node
;
7077 nparms
= TREE_VEC_LENGTH (parms
);
7079 /* Determine if there are any parameter packs or default arguments. */
7080 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
7082 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
7084 ++post_variadic_parms
;
7085 if (template_parameter_pack_p (TREE_VALUE (parm
)))
7087 if (TREE_PURPOSE (parm
))
7091 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
7092 /* If there are no parameters that follow a parameter pack, we need to
7093 expand any argument packs so that we can deduce a parameter pack from
7094 some non-packed args followed by an argument pack, as in variadic85.C.
7095 If there are such parameters, we need to leave argument packs intact
7096 so the arguments are assigned properly. This can happen when dealing
7097 with a nested class inside a partial specialization of a class
7098 template, as in variadic92.C, or when deducing a template parameter pack
7099 from a sub-declarator, as in variadic114.C. */
7100 if (!post_variadic_parms
)
7101 inner_args
= expand_template_argument_pack (inner_args
);
7103 /* Count any pack expansion args. */
7104 variadic_args_p
= pack_expansion_args_count (inner_args
);
7106 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
7107 if ((nargs
> nparms
&& !variadic_p
)
7108 || (nargs
< nparms
- variadic_p
7111 && (!use_default_args
7112 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
7113 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
7115 if (complain
& tf_error
)
7117 if (variadic_p
|| default_p
)
7119 nparms
-= variadic_p
+ default_p
;
7120 error ("wrong number of template arguments "
7121 "(%d, should be at least %d)", nargs
, nparms
);
7124 error ("wrong number of template arguments "
7125 "(%d, should be %d)", nargs
, nparms
);
7128 inform (DECL_SOURCE_LOCATION (in_decl
),
7129 "provided for %qD", in_decl
);
7132 return error_mark_node
;
7134 /* We can't pass a pack expansion to a non-pack parameter of an alias
7135 template (DR 1430). */
7136 else if (in_decl
&& DECL_ALIAS_TEMPLATE_P (in_decl
)
7138 && nargs
- variadic_args_p
< nparms
- variadic_p
)
7140 if (complain
& tf_error
)
7142 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
7144 tree arg
= TREE_VEC_ELT (inner_args
, i
);
7145 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
7147 if (PACK_EXPANSION_P (arg
)
7148 && !template_parameter_pack_p (parm
))
7150 error ("pack expansion argument for non-pack parameter "
7151 "%qD of alias template %qD", parm
, in_decl
);
7152 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
7159 return error_mark_node
;
7162 /* We need to evaluate the template arguments, even though this
7163 template-id may be nested within a "sizeof". */
7164 saved_unevaluated_operand
= cp_unevaluated_operand
;
7165 cp_unevaluated_operand
= 0;
7166 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
7167 c_inhibit_evaluation_warnings
= 0;
7168 new_inner_args
= make_tree_vec (nparms
);
7169 new_args
= add_outermost_template_args (args
, new_inner_args
);
7170 int pack_adjust
= 0;
7171 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
7176 /* Get the Ith template parameter. */
7177 parm
= TREE_VEC_ELT (parms
, parm_idx
);
7179 if (parm
== error_mark_node
)
7181 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
7185 /* Calculate the next argument. */
7186 if (arg_idx
< nargs
)
7187 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
7191 if (template_parameter_pack_p (TREE_VALUE (parm
))
7192 && !(arg
&& ARGUMENT_PACK_P (arg
)))
7194 /* Some arguments will be placed in the
7195 template parameter pack PARM. */
7196 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
7197 inner_args
, arg_idx
,
7201 if (arg
== NULL_TREE
)
7203 /* We don't know how many args we have yet, just use the
7204 unconverted (and still packed) ones for now. */
7205 new_inner_args
= orig_inner_args
;
7210 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
7212 /* Store this argument. */
7213 if (arg
== error_mark_node
)
7216 /* We are done with all of the arguments. */
7221 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
7222 arg_idx
+= pack_adjust
;
7229 if (PACK_EXPANSION_P (arg
))
7231 /* "If every valid specialization of a variadic template
7232 requires an empty template parameter pack, the template is
7233 ill-formed, no diagnostic required." So check that the
7234 pattern works with this parameter. */
7235 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
7236 tree conv
= convert_template_argument (TREE_VALUE (parm
),
7240 if (conv
== error_mark_node
)
7242 inform (input_location
, "so any instantiation with a "
7243 "non-empty parameter pack would be ill-formed");
7246 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
7247 /* Recover from missing typename. */
7248 TREE_VEC_ELT (inner_args
, arg_idx
)
7249 = make_pack_expansion (conv
);
7251 /* We don't know how many args we have yet, just
7252 use the unconverted ones for now. */
7253 new_inner_args
= inner_args
;
7258 else if (require_all_args
)
7260 /* There must be a default arg in this case. */
7261 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
7263 /* The position of the first default template argument,
7264 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
7266 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
7267 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
7268 arg_idx
- pack_adjust
);
7273 if (arg
== error_mark_node
)
7275 if (complain
& tf_error
)
7276 error ("template argument %d is invalid", arg_idx
+ 1);
7279 /* This only occurs if there was an error in the template
7280 parameter list itself (which we would already have
7281 reported) that we are trying to recover from, e.g., a class
7282 template with a parameter list such as
7283 template<typename..., typename>. */
7286 arg
= convert_template_argument (TREE_VALUE (parm
),
7287 arg
, new_args
, complain
,
7290 if (arg
== error_mark_node
)
7292 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
7294 cp_unevaluated_operand
= saved_unevaluated_operand
;
7295 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
7297 if (variadic_p
&& arg_idx
< nargs
)
7299 if (complain
& tf_error
)
7301 error ("wrong number of template arguments "
7302 "(%d, should be %d)", nargs
, arg_idx
);
7304 error ("provided for %q+D", in_decl
);
7306 return error_mark_node
;
7310 return error_mark_node
;
7312 #ifdef ENABLE_CHECKING
7313 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
7314 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
7315 TREE_VEC_LENGTH (new_inner_args
));
7318 return new_inner_args
;
7321 /* Like coerce_template_parms. If PARMS represents all template
7322 parameters levels, this function returns a vector of vectors
7323 representing all the resulting argument levels. Note that in this
7324 case, only the innermost arguments are coerced because the
7325 outermost ones are supposed to have been coerced already.
7327 Otherwise, if PARMS represents only (the innermost) vector of
7328 parameters, this function returns a vector containing just the
7329 innermost resulting arguments. */
7332 coerce_innermost_template_parms (tree parms
,
7335 tsubst_flags_t complain
,
7336 bool require_all_args
,
7337 bool use_default_args
)
7339 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
7340 int args_depth
= TMPL_ARGS_DEPTH (args
);
7343 if (parms_depth
> 1)
7345 coerced_args
= make_tree_vec (parms_depth
);
7349 for (level
= parms
, cur_depth
= parms_depth
;
7350 parms_depth
> 0 && level
!= NULL_TREE
;
7351 level
= TREE_CHAIN (level
), --cur_depth
)
7354 if (cur_depth
== args_depth
)
7355 l
= coerce_template_parms (TREE_VALUE (level
),
7356 args
, in_decl
, complain
,
7360 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
7362 if (l
== error_mark_node
)
7363 return error_mark_node
;
7365 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
7369 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
7370 args
, in_decl
, complain
,
7373 return coerced_args
;
7376 /* Returns 1 if template args OT and NT are equivalent. */
7379 template_args_equal (tree ot
, tree nt
)
7383 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
7386 if (TREE_CODE (nt
) == TREE_VEC
)
7387 /* For member templates */
7388 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
7389 else if (PACK_EXPANSION_P (ot
))
7390 return (PACK_EXPANSION_P (nt
)
7391 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
7392 PACK_EXPANSION_PATTERN (nt
))
7393 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
7394 PACK_EXPANSION_EXTRA_ARGS (nt
)));
7395 else if (ARGUMENT_PACK_P (ot
))
7400 if (!ARGUMENT_PACK_P (nt
))
7403 opack
= ARGUMENT_PACK_ARGS (ot
);
7404 npack
= ARGUMENT_PACK_ARGS (nt
);
7405 len
= TREE_VEC_LENGTH (opack
);
7406 if (TREE_VEC_LENGTH (npack
) != len
)
7408 for (i
= 0; i
< len
; ++i
)
7409 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
7410 TREE_VEC_ELT (npack
, i
)))
7414 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
7416 /* We get here probably because we are in the middle of substituting
7417 into the pattern of a pack expansion. In that case the
7418 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
7419 interested in. So we want to use the initial pack argument for
7421 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
7422 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
7423 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
7424 return template_args_equal (ot
, nt
);
7426 else if (TYPE_P (nt
))
7430 /* Don't treat an alias template specialization with dependent
7431 arguments as equivalent to its underlying type when used as a
7432 template argument; we need them to be distinct so that we
7433 substitute into the specialization arguments at instantiation
7434 time. And aliases can't be equivalent without being ==, so
7435 we don't need to look any deeper. */
7436 if (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
))
7439 return same_type_p (ot
, nt
);
7441 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
7445 /* Try to treat a template non-type argument that has been converted
7446 to the parameter type as equivalent to one that hasn't yet. */
7447 for (enum tree_code code1
= TREE_CODE (ot
);
7448 CONVERT_EXPR_CODE_P (code1
)
7449 || code1
== NON_LVALUE_EXPR
;
7450 code1
= TREE_CODE (ot
))
7451 ot
= TREE_OPERAND (ot
, 0);
7452 for (enum tree_code code2
= TREE_CODE (nt
);
7453 CONVERT_EXPR_CODE_P (code2
)
7454 || code2
== NON_LVALUE_EXPR
;
7455 code2
= TREE_CODE (nt
))
7456 nt
= TREE_OPERAND (nt
, 0);
7458 return cp_tree_equal (ot
, nt
);
7462 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7463 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7464 NEWARG_PTR with the offending arguments if they are non-NULL. */
7467 comp_template_args_with_info (tree oldargs
, tree newargs
,
7468 tree
*oldarg_ptr
, tree
*newarg_ptr
)
7472 if (oldargs
== newargs
)
7475 if (!oldargs
|| !newargs
)
7478 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
7481 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
7483 tree nt
= TREE_VEC_ELT (newargs
, i
);
7484 tree ot
= TREE_VEC_ELT (oldargs
, i
);
7486 if (! template_args_equal (ot
, nt
))
7488 if (oldarg_ptr
!= NULL
)
7490 if (newarg_ptr
!= NULL
)
7498 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7499 of template arguments. Returns 0 otherwise. */
7502 comp_template_args (tree oldargs
, tree newargs
)
7504 return comp_template_args_with_info (oldargs
, newargs
, NULL
, NULL
);
7508 add_pending_template (tree d
)
7510 tree ti
= (TYPE_P (d
)
7511 ? CLASSTYPE_TEMPLATE_INFO (d
)
7512 : DECL_TEMPLATE_INFO (d
));
7513 struct pending_template
*pt
;
7516 if (TI_PENDING_TEMPLATE_FLAG (ti
))
7519 /* We are called both from instantiate_decl, where we've already had a
7520 tinst_level pushed, and instantiate_template, where we haven't.
7522 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
7525 push_tinst_level (d
);
7527 pt
= ggc_alloc
<pending_template
> ();
7529 pt
->tinst
= current_tinst_level
;
7530 if (last_pending_template
)
7531 last_pending_template
->next
= pt
;
7533 pending_templates
= pt
;
7535 last_pending_template
= pt
;
7537 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
7544 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7545 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7546 documentation for TEMPLATE_ID_EXPR. */
7549 lookup_template_function (tree fns
, tree arglist
)
7553 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
7554 return error_mark_node
;
7556 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
7558 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
7560 error ("%q#D is not a function template", fns
);
7561 return error_mark_node
;
7564 if (BASELINK_P (fns
))
7566 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
7568 BASELINK_FUNCTIONS (fns
),
7573 type
= TREE_TYPE (fns
);
7574 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
7575 type
= unknown_type_node
;
7577 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
7580 /* Within the scope of a template class S<T>, the name S gets bound
7581 (in build_self_reference) to a TYPE_DECL for the class, not a
7582 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7583 or one of its enclosing classes, and that type is a template,
7584 return the associated TEMPLATE_DECL. Otherwise, the original
7587 Also handle the case when DECL is a TREE_LIST of ambiguous
7588 injected-class-names from different bases. */
7591 maybe_get_template_decl_from_type_decl (tree decl
)
7593 if (decl
== NULL_TREE
)
7596 /* DR 176: A lookup that finds an injected-class-name (10.2
7597 [class.member.lookup]) can result in an ambiguity in certain cases
7598 (for example, if it is found in more than one base class). If all of
7599 the injected-class-names that are found refer to specializations of
7600 the same class template, and if the name is followed by a
7601 template-argument-list, the reference refers to the class template
7602 itself and not a specialization thereof, and is not ambiguous. */
7603 if (TREE_CODE (decl
) == TREE_LIST
)
7605 tree t
, tmpl
= NULL_TREE
;
7606 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
7608 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
7611 else if (tmpl
!= elt
)
7614 if (tmpl
&& t
== NULL_TREE
)
7620 return (decl
!= NULL_TREE
7621 && DECL_SELF_REFERENCE_P (decl
)
7622 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
7623 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
7626 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7627 parameters, find the desired type.
7629 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7631 IN_DECL, if non-NULL, is the template declaration we are trying to
7634 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7635 the class we are looking up.
7637 Issue error and warning messages under control of COMPLAIN.
7639 If the template class is really a local class in a template
7640 function, then the FUNCTION_CONTEXT is the function in which it is
7643 ??? Note that this function is currently called *twice* for each
7644 template-id: the first time from the parser, while creating the
7645 incomplete type (finish_template_type), and the second type during the
7646 real instantiation (instantiate_template_class). This is surely something
7647 that we want to avoid. It also causes some problems with argument
7648 coercion (see convert_nontype_argument for more information on this). */
7651 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
7652 int entering_scope
, tsubst_flags_t complain
)
7654 tree templ
= NULL_TREE
, parmlist
;
7661 if (identifier_p (d1
))
7663 tree value
= innermost_non_namespace_value (d1
);
7664 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
7669 push_decl_namespace (context
);
7670 templ
= lookup_name (d1
);
7671 templ
= maybe_get_template_decl_from_type_decl (templ
);
7673 pop_decl_namespace ();
7676 context
= DECL_CONTEXT (templ
);
7678 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
7680 tree type
= TREE_TYPE (d1
);
7682 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7683 an implicit typename for the second A. Deal with it. */
7684 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
7685 type
= TREE_TYPE (type
);
7687 if (CLASSTYPE_TEMPLATE_INFO (type
))
7689 templ
= CLASSTYPE_TI_TEMPLATE (type
);
7690 d1
= DECL_NAME (templ
);
7693 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
7694 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
7696 templ
= TYPE_TI_TEMPLATE (d1
);
7697 d1
= DECL_NAME (templ
);
7699 else if (DECL_TYPE_TEMPLATE_P (d1
))
7702 d1
= DECL_NAME (templ
);
7703 context
= DECL_CONTEXT (templ
);
7705 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
7708 d1
= DECL_NAME (templ
);
7711 /* Issue an error message if we didn't find a template. */
7714 if (complain
& tf_error
)
7715 error ("%qT is not a template", d1
);
7716 return error_mark_node
;
7719 if (TREE_CODE (templ
) != TEMPLATE_DECL
7720 /* Make sure it's a user visible template, if it was named by
7722 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
7723 && !PRIMARY_TEMPLATE_P (templ
)))
7725 if (complain
& tf_error
)
7727 error ("non-template type %qT used as a template", d1
);
7729 error ("for template declaration %q+D", in_decl
);
7731 return error_mark_node
;
7734 complain
&= ~tf_user
;
7736 /* An alias that just changes the name of a template is equivalent to the
7737 other template, so if any of the arguments are pack expansions, strip
7738 the alias to avoid problems with a pack expansion passed to a non-pack
7739 alias template parameter (DR 1430). */
7740 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
7741 templ
= get_underlying_template (templ
);
7743 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
7745 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7746 template arguments */
7752 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7754 /* Consider an example where a template template parameter declared as
7756 template <class T, class U = std::allocator<T> > class TT
7758 The template parameter level of T and U are one level larger than
7759 of TT. To proper process the default argument of U, say when an
7760 instantiation `TT<int>' is seen, we need to build the full
7761 arguments containing {int} as the innermost level. Outer levels,
7762 available when not appearing as default template argument, can be
7763 obtained from the arguments of the enclosing template.
7765 Suppose that TT is later substituted with std::vector. The above
7766 instantiation is `TT<int, std::allocator<T> >' with TT at
7767 level 1, and T at level 2, while the template arguments at level 1
7768 becomes {std::vector} and the inner level 2 is {int}. */
7770 outer
= DECL_CONTEXT (templ
);
7772 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7773 else if (current_template_parms
)
7775 /* This is an argument of the current template, so we haven't set
7776 DECL_CONTEXT yet. */
7777 tree relevant_template_parms
;
7779 /* Parameter levels that are greater than the level of the given
7780 template template parm are irrelevant. */
7781 relevant_template_parms
= current_template_parms
;
7782 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7783 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7784 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7786 outer
= template_parms_to_args (relevant_template_parms
);
7790 arglist
= add_to_template_args (outer
, arglist
);
7792 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
7794 /*require_all_args=*/true,
7795 /*use_default_args=*/true);
7796 if (arglist2
== error_mark_node
7797 || (!uses_template_parms (arglist2
)
7798 && check_instantiated_args (templ
, arglist2
, complain
)))
7799 return error_mark_node
;
7801 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
7806 tree template_type
= TREE_TYPE (templ
);
7809 tree found
= NULL_TREE
;
7812 int is_dependent_type
;
7813 int use_partial_inst_tmpl
= false;
7815 if (template_type
== error_mark_node
)
7816 /* An error occurred while building the template TEMPL, and a
7817 diagnostic has most certainly been emitted for that
7818 already. Let's propagate that error. */
7819 return error_mark_node
;
7821 gen_tmpl
= most_general_template (templ
);
7822 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7823 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
7824 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7826 if (arg_depth
== 1 && parm_depth
> 1)
7828 /* We've been given an incomplete set of template arguments.
7831 template <class T> struct S1 {
7832 template <class U> struct S2 {};
7833 template <class U> struct S2<U*> {};
7836 we will be called with an ARGLIST of `U*', but the
7837 TEMPLATE will be `template <class T> template
7838 <class U> struct S1<T>::S2'. We must fill in the missing
7841 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
7843 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7846 /* Now we should have enough arguments. */
7847 gcc_assert (parm_depth
== arg_depth
);
7849 /* From here on, we're only interested in the most general
7852 /* Calculate the BOUND_ARGS. These will be the args that are
7853 actually tsubst'd into the definition to create the
7855 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
7857 /*require_all_args=*/true,
7858 /*use_default_args=*/true);
7860 if (arglist
== error_mark_node
)
7861 /* We were unable to bind the arguments. */
7862 return error_mark_node
;
7864 /* In the scope of a template class, explicit references to the
7865 template class refer to the type of the template, not any
7866 instantiation of it. For example, in:
7868 template <class T> class C { void f(C<T>); }
7870 the `C<T>' is just the same as `C'. Outside of the
7871 class, however, such a reference is an instantiation. */
7873 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
7874 || currently_open_class (template_type
))
7875 /* comp_template_args is expensive, check it last. */
7876 && comp_template_args (TYPE_TI_ARGS (template_type
),
7878 return template_type
;
7880 /* If we already have this specialization, return it. */
7881 elt
.tmpl
= gen_tmpl
;
7883 elt
.spec
= NULL_TREE
;
7884 hash
= spec_hasher::hash (&elt
);
7885 entry
= type_specializations
->find_with_hash (&elt
, hash
);
7890 is_dependent_type
= uses_template_parms (arglist
);
7892 /* If the deduced arguments are invalid, then the binding
7894 if (!is_dependent_type
7895 && check_instantiated_args (gen_tmpl
,
7896 INNERMOST_TEMPLATE_ARGS (arglist
),
7898 return error_mark_node
;
7900 if (!is_dependent_type
7901 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
7902 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
7903 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
7905 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
7906 DECL_NAME (gen_tmpl
),
7907 /*tag_scope=*/ts_global
);
7911 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
7913 if (context
== error_mark_node
)
7914 return error_mark_node
;
7917 context
= global_namespace
;
7919 /* Create the type. */
7920 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7922 /* The user referred to a specialization of an alias
7923 template represented by GEN_TMPL.
7925 [temp.alias]/2 says:
7927 When a template-id refers to the specialization of an
7928 alias template, it is equivalent to the associated
7929 type obtained by substitution of its
7930 template-arguments for the template-parameters in the
7931 type-id of the alias template. */
7933 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
7934 /* Note that the call above (by indirectly calling
7935 register_specialization in tsubst_decl) registers the
7936 TYPE_DECL representing the specialization of the alias
7937 template. So next time someone substitutes ARGLIST for
7938 the template parms into the alias template (GEN_TMPL),
7939 she'll get that TYPE_DECL back. */
7941 if (t
== error_mark_node
)
7944 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
7946 if (!is_dependent_type
)
7948 set_current_access_from_decl (TYPE_NAME (template_type
));
7949 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
7950 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
7951 arglist
, complain
, in_decl
),
7952 SCOPED_ENUM_P (template_type
), NULL
);
7954 if (t
== error_mark_node
)
7959 /* We don't want to call start_enum for this type, since
7960 the values for the enumeration constants may involve
7961 template parameters. And, no one should be interested
7962 in the enumeration constants for such a type. */
7963 t
= cxx_make_type (ENUMERAL_TYPE
);
7964 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
7966 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
7967 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
7968 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7970 else if (CLASS_TYPE_P (template_type
))
7972 t
= make_class_type (TREE_CODE (template_type
));
7973 CLASSTYPE_DECLARED_CLASS (t
)
7974 = CLASSTYPE_DECLARED_CLASS (template_type
);
7975 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7976 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7978 /* A local class. Make sure the decl gets registered properly. */
7979 if (context
== current_function_decl
)
7980 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7982 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7983 /* This instantiation is another name for the primary
7984 template type. Set the TYPE_CANONICAL field
7986 TYPE_CANONICAL (t
) = template_type
;
7987 else if (any_template_arguments_need_structural_equality_p (arglist
))
7988 /* Some of the template arguments require structural
7989 equality testing, so this template class requires
7990 structural equality testing. */
7991 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7996 /* If we called start_enum or pushtag above, this information
7997 will already be set up. */
8000 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
8002 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
8003 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
8004 DECL_SOURCE_LOCATION (type_decl
)
8005 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
8008 type_decl
= TYPE_NAME (t
);
8010 if (CLASS_TYPE_P (template_type
))
8012 TREE_PRIVATE (type_decl
)
8013 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
8014 TREE_PROTECTED (type_decl
)
8015 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
8016 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
8018 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
8019 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
8023 if (OVERLOAD_TYPE_P (t
)
8024 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8026 static const char *tags
[] = {"abi_tag", "may_alias"};
8028 for (unsigned ix
= 0; ix
!= 2; ix
++)
8031 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
8035 else if (!TREE_CHAIN (attributes
) && !TYPE_ATTRIBUTES (t
))
8036 TYPE_ATTRIBUTES (t
) = attributes
;
8039 = tree_cons (TREE_PURPOSE (attributes
),
8040 TREE_VALUE (attributes
),
8041 TYPE_ATTRIBUTES (t
));
8045 /* Let's consider the explicit specialization of a member
8046 of a class template specialization that is implicitly instantiated,
8051 template<class U> struct M {}; //#0
8056 struct S<int>::M<char> //#1
8060 [temp.expl.spec]/4 says this is valid.
8062 In this case, when we write:
8065 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8068 When we encounter #1, we want to store the partial instantiation
8069 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8071 For all cases other than this "explicit specialization of member of a
8072 class template", we just want to store the most general template into
8073 the CLASSTYPE_TI_TEMPLATE of M.
8075 This case of "explicit specialization of member of a class template"
8077 1/ the enclosing class is an instantiation of, and therefore not
8078 the same as, the context of the most general template, and
8079 2/ we aren't looking at the partial instantiation itself, i.e.
8080 the innermost arguments are not the same as the innermost parms of
8081 the most general template.
8083 So it's only when 1/ and 2/ happens that we want to use the partial
8084 instantiation of the member template in lieu of its most general
8087 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
8088 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
8089 /* the enclosing class must be an instantiation... */
8090 && CLASS_TYPE_P (context
)
8091 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
8093 tree partial_inst_args
;
8094 TREE_VEC_LENGTH (arglist
)--;
8095 ++processing_template_decl
;
8097 tsubst (INNERMOST_TEMPLATE_ARGS
8098 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
8099 arglist
, complain
, NULL_TREE
);
8100 --processing_template_decl
;
8101 TREE_VEC_LENGTH (arglist
)++;
8102 use_partial_inst_tmpl
=
8103 /*...and we must not be looking at the partial instantiation
8105 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
8109 if (!use_partial_inst_tmpl
)
8110 /* This case is easy; there are no member templates involved. */
8114 /* This is a full instantiation of a member template. Find
8115 the partial instantiation of which this is an instance. */
8117 /* Temporarily reduce by one the number of levels in the ARGLIST
8118 so as to avoid comparing the last set of arguments. */
8119 TREE_VEC_LENGTH (arglist
)--;
8120 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
8121 TREE_VEC_LENGTH (arglist
)++;
8122 /* FOUND is either a proper class type, or an alias
8123 template specialization. In the later case, it's a
8124 TYPE_DECL, resulting from the substituting of arguments
8125 for parameters in the TYPE_DECL of the alias template
8126 done earlier. So be careful while getting the template
8128 found
= TREE_CODE (found
) == TYPE_DECL
8129 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
8130 : CLASSTYPE_TI_TEMPLATE (found
);
8133 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
8136 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
8137 entry
= ggc_alloc
<spec_entry
> ();
8141 /* Note this use of the partial instantiation so we can check it
8142 later in maybe_process_partial_specialization. */
8143 DECL_TEMPLATE_INSTANTIATIONS (found
)
8144 = tree_cons (arglist
, t
,
8145 DECL_TEMPLATE_INSTANTIATIONS (found
));
8147 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
8148 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8149 /* Now that the type has been registered on the instantiations
8150 list, we set up the enumerators. Because the enumeration
8151 constants may involve the enumeration type itself, we make
8152 sure to register the type first, and then create the
8153 constants. That way, doing tsubst_expr for the enumeration
8154 constants won't result in recursive calls here; we'll find
8155 the instantiation and exit above. */
8156 tsubst_enum (template_type
, t
, arglist
);
8158 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
8159 /* If the type makes use of template parameters, the
8160 code that generates debugging information will crash. */
8161 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
8163 /* Possibly limit visibility based on template args. */
8164 TREE_PUBLIC (type_decl
) = 1;
8165 determine_visibility (type_decl
);
8167 inherit_targ_abi_tags (t
);
8173 /* Wrapper for lookup_template_class_1. */
8176 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
8177 int entering_scope
, tsubst_flags_t complain
)
8180 timevar_push (TV_TEMPLATE_INST
);
8181 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
8182 entering_scope
, complain
);
8183 timevar_pop (TV_TEMPLATE_INST
);
8187 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST.
8188 The type of the expression is the unknown_type_node since the
8189 template-id could refer to an explicit or partial specialization. */
8192 lookup_template_variable (tree templ
, tree arglist
)
8194 tree type
= NULL_TREE
;
8195 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
8198 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
8201 finish_template_variable (tree var
, tsubst_flags_t complain
)
8203 tree templ
= TREE_OPERAND (var
, 0);
8205 tree arglist
= TREE_OPERAND (var
, 1);
8206 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
8207 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
8209 tree parms
= DECL_TEMPLATE_PARMS (templ
);
8210 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
8212 /*use_default*/true);
8214 return instantiate_template (templ
, arglist
, complain
);
8221 /* True when we should also visit template parameters that occur in
8222 non-deduced contexts. */
8223 bool include_nondeduced_p
;
8224 hash_set
<tree
> *visited
;
8227 /* Called from for_each_template_parm via walk_tree. */
8230 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
8233 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
8234 tree_fn_t fn
= pfd
->fn
;
8235 void *data
= pfd
->data
;
8238 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
8239 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
8240 pfd
->include_nondeduced_p
))
8241 return error_mark_node
;
8243 switch (TREE_CODE (t
))
8246 if (TYPE_PTRMEMFUNC_P (t
))
8252 if (!TYPE_TEMPLATE_INFO (t
))
8254 else if (for_each_template_parm (TYPE_TI_ARGS (t
),
8255 fn
, data
, pfd
->visited
,
8256 pfd
->include_nondeduced_p
))
8257 return error_mark_node
;
8261 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
8262 fn
, data
, pfd
->visited
,
8263 pfd
->include_nondeduced_p
)
8264 || for_each_template_parm (TYPE_MAX_VALUE (t
),
8265 fn
, data
, pfd
->visited
,
8266 pfd
->include_nondeduced_p
))
8267 return error_mark_node
;
8271 /* Since we're not going to walk subtrees, we have to do this
8273 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
8274 pfd
->visited
, pfd
->include_nondeduced_p
))
8275 return error_mark_node
;
8279 /* Check the return type. */
8280 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
8281 pfd
->include_nondeduced_p
))
8282 return error_mark_node
;
8284 /* Check the parameter types. Since default arguments are not
8285 instantiated until they are needed, the TYPE_ARG_TYPES may
8286 contain expressions that involve template parameters. But,
8287 no-one should be looking at them yet. And, once they're
8288 instantiated, they don't contain template parameters, so
8289 there's no point in looking at them then, either. */
8293 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
8294 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
8295 pfd
->visited
, pfd
->include_nondeduced_p
))
8296 return error_mark_node
;
8298 /* Since we've already handled the TYPE_ARG_TYPES, we don't
8299 want walk_tree walking into them itself. */
8305 case UNDERLYING_TYPE
:
8306 if (pfd
->include_nondeduced_p
8307 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
8309 pfd
->include_nondeduced_p
))
8310 return error_mark_node
;
8315 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
8316 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
8317 pfd
->visited
, pfd
->include_nondeduced_p
))
8318 return error_mark_node
;
8323 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
8324 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
8325 pfd
->visited
, pfd
->include_nondeduced_p
))
8326 return error_mark_node
;
8327 if (DECL_CONTEXT (t
)
8328 && pfd
->include_nondeduced_p
8329 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
8330 pfd
->visited
, pfd
->include_nondeduced_p
))
8331 return error_mark_node
;
8334 case BOUND_TEMPLATE_TEMPLATE_PARM
:
8335 /* Record template parameters such as `T' inside `TT<T>'. */
8336 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
8337 pfd
->include_nondeduced_p
))
8338 return error_mark_node
;
8341 case TEMPLATE_TEMPLATE_PARM
:
8342 case TEMPLATE_TYPE_PARM
:
8343 case TEMPLATE_PARM_INDEX
:
8344 if (fn
&& (*fn
)(t
, data
))
8345 return error_mark_node
;
8347 return error_mark_node
;
8351 /* A template template parameter is encountered. */
8352 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
8353 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
8354 pfd
->include_nondeduced_p
))
8355 return error_mark_node
;
8357 /* Already substituted template template parameter */
8363 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
8365 pfd
->include_nondeduced_p
))
8366 return error_mark_node
;
8370 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
8371 && pfd
->include_nondeduced_p
8372 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
8373 (TREE_TYPE (t
)), fn
, data
,
8374 pfd
->visited
, pfd
->include_nondeduced_p
))
8375 return error_mark_node
;
8380 /* If there's no type, then this thing must be some expression
8381 involving template parameters. */
8382 if (!fn
&& !TREE_TYPE (t
))
8383 return error_mark_node
;
8388 case IMPLICIT_CONV_EXPR
:
8389 case REINTERPRET_CAST_EXPR
:
8390 case CONST_CAST_EXPR
:
8391 case STATIC_CAST_EXPR
:
8392 case DYNAMIC_CAST_EXPR
:
8396 case PSEUDO_DTOR_EXPR
:
8398 return error_mark_node
;
8405 /* We didn't find any template parameters we liked. */
8409 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8410 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8411 call FN with the parameter and the DATA.
8412 If FN returns nonzero, the iteration is terminated, and
8413 for_each_template_parm returns 1. Otherwise, the iteration
8414 continues. If FN never returns a nonzero value, the value
8415 returned by for_each_template_parm is 0. If FN is NULL, it is
8416 considered to be the function which always returns 1.
8418 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8419 parameters that occur in non-deduced contexts. When false, only
8420 visits those template parameters that can be deduced. */
8423 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
8424 hash_set
<tree
> *visited
,
8425 bool include_nondeduced_p
)
8427 struct pair_fn_data pfd
;
8433 pfd
.include_nondeduced_p
= include_nondeduced_p
;
8435 /* Walk the tree. (Conceptually, we would like to walk without
8436 duplicates, but for_each_template_parm_r recursively calls
8437 for_each_template_parm, so we would need to reorganize a fair
8438 bit to use walk_tree_without_duplicates, so we keep our own
8441 pfd
.visited
= visited
;
8443 pfd
.visited
= new hash_set
<tree
>;
8444 result
= cp_walk_tree (&t
,
8445 for_each_template_parm_r
,
8447 pfd
.visited
) != NULL_TREE
;
8459 /* Returns true if T depends on any template parameter. */
8462 uses_template_parms (tree t
)
8468 int saved_processing_template_decl
;
8470 saved_processing_template_decl
= processing_template_decl
;
8471 if (!saved_processing_template_decl
)
8472 processing_template_decl
= 1;
8474 dependent_p
= dependent_type_p (t
);
8475 else if (TREE_CODE (t
) == TREE_VEC
)
8476 dependent_p
= any_dependent_template_arguments_p (t
);
8477 else if (TREE_CODE (t
) == TREE_LIST
)
8478 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
8479 || uses_template_parms (TREE_CHAIN (t
)));
8480 else if (TREE_CODE (t
) == TYPE_DECL
)
8481 dependent_p
= dependent_type_p (TREE_TYPE (t
));
8484 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
8485 || TREE_CODE (t
) == OVERLOAD
8488 || TREE_CODE (t
) == TRAIT_EXPR
8489 || TREE_CODE (t
) == CONSTRUCTOR
8490 || CONSTANT_CLASS_P (t
))
8491 dependent_p
= (type_dependent_expression_p (t
)
8492 || value_dependent_expression_p (t
));
8495 gcc_assert (t
== error_mark_node
);
8496 dependent_p
= false;
8499 processing_template_decl
= saved_processing_template_decl
;
8504 /* Returns true iff current_function_decl is an incompletely instantiated
8505 template. Useful instead of processing_template_decl because the latter
8506 is set to 0 during instantiate_non_dependent_expr. */
8509 in_template_function (void)
8511 tree fn
= current_function_decl
;
8513 ++processing_template_decl
;
8514 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
8515 && DECL_TEMPLATE_INFO (fn
)
8516 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
8517 --processing_template_decl
;
8521 /* Returns true if T depends on any template parameter with level LEVEL. */
8524 uses_template_parms_level (tree t
, int level
)
8526 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
8527 /*include_nondeduced_p=*/true);
8530 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8531 ill-formed translation unit, i.e. a variable or function that isn't
8532 usable in a constant expression. */
8535 neglectable_inst_p (tree d
)
8538 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
8539 : decl_maybe_constant_var_p (d
)));
8542 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8543 neglectable and instantiated from within an erroneous instantiation. */
8546 limit_bad_template_recursion (tree decl
)
8548 struct tinst_level
*lev
= current_tinst_level
;
8549 int errs
= errorcount
+ sorrycount
;
8550 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
8553 for (; lev
; lev
= lev
->next
)
8554 if (neglectable_inst_p (lev
->decl
))
8557 return (lev
&& errs
> lev
->errors
);
8560 static int tinst_depth
;
8561 extern int max_tinst_depth
;
8564 static GTY(()) struct tinst_level
*last_error_tinst_level
;
8566 /* We're starting to instantiate D; record the template instantiation context
8567 for diagnostics and to restore it later. */
8570 push_tinst_level (tree d
)
8572 return push_tinst_level_loc (d
, input_location
);
8575 /* We're starting to instantiate D; record the template instantiation context
8576 at LOC for diagnostics and to restore it later. */
8579 push_tinst_level_loc (tree d
, location_t loc
)
8581 struct tinst_level
*new_level
;
8583 if (tinst_depth
>= max_tinst_depth
)
8585 fatal_error (input_location
,
8586 "template instantiation depth exceeds maximum of %d"
8587 " (use -ftemplate-depth= to increase the maximum)",
8592 /* If the current instantiation caused problems, don't let it instantiate
8593 anything else. Do allow deduction substitution and decls usable in
8594 constant expressions. */
8595 if (limit_bad_template_recursion (d
))
8598 new_level
= ggc_alloc
<tinst_level
> ();
8599 new_level
->decl
= d
;
8600 new_level
->locus
= loc
;
8601 new_level
->errors
= errorcount
+sorrycount
;
8602 new_level
->in_system_header_p
= in_system_header_at (input_location
);
8603 new_level
->next
= current_tinst_level
;
8604 current_tinst_level
= new_level
;
8607 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
8608 depth_reached
= tinst_depth
;
8613 /* We're done instantiating this template; return to the instantiation
8617 pop_tinst_level (void)
8619 /* Restore the filename and line number stashed away when we started
8620 this instantiation. */
8621 input_location
= current_tinst_level
->locus
;
8622 current_tinst_level
= current_tinst_level
->next
;
8626 /* We're instantiating a deferred template; restore the template
8627 instantiation context in which the instantiation was requested, which
8628 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8631 reopen_tinst_level (struct tinst_level
*level
)
8633 struct tinst_level
*t
;
8636 for (t
= level
; t
; t
= t
->next
)
8639 current_tinst_level
= level
;
8641 if (current_tinst_level
)
8642 current_tinst_level
->errors
= errorcount
+sorrycount
;
8646 /* Returns the TINST_LEVEL which gives the original instantiation
8649 struct tinst_level
*
8650 outermost_tinst_level (void)
8652 struct tinst_level
*level
= current_tinst_level
;
8655 level
= level
->next
;
8659 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8660 vector of template arguments, as for tsubst.
8662 Returns an appropriate tsubst'd friend declaration. */
8665 tsubst_friend_function (tree decl
, tree args
)
8669 if (TREE_CODE (decl
) == FUNCTION_DECL
8670 && DECL_TEMPLATE_INSTANTIATION (decl
)
8671 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
8672 /* This was a friend declared with an explicit template
8673 argument list, e.g.:
8677 to indicate that f was a template instantiation, not a new
8678 function declaration. Now, we have to figure out what
8679 instantiation of what template. */
8681 tree template_id
, arglist
, fns
;
8684 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
8686 /* Friend functions are looked up in the containing namespace scope.
8687 We must enter that scope, to avoid finding member functions of the
8688 current class with same name. */
8689 push_nested_namespace (ns
);
8690 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
8691 tf_warning_or_error
, NULL_TREE
,
8692 /*integral_constant_expression_p=*/false);
8693 pop_nested_namespace (ns
);
8694 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
8695 tf_warning_or_error
, NULL_TREE
);
8696 template_id
= lookup_template_function (fns
, arglist
);
8698 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8699 tmpl
= determine_specialization (template_id
, new_friend
,
8701 /*need_member_template=*/0,
8702 TREE_VEC_LENGTH (args
),
8704 return instantiate_template (tmpl
, new_args
, tf_error
);
8707 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8709 /* The NEW_FRIEND will look like an instantiation, to the
8710 compiler, but is not an instantiation from the point of view of
8711 the language. For example, we might have had:
8713 template <class T> struct S {
8714 template <class U> friend void f(T, U);
8717 Then, in S<int>, template <class U> void f(int, U) is not an
8718 instantiation of anything. */
8719 if (new_friend
== error_mark_node
)
8720 return error_mark_node
;
8722 DECL_USE_TEMPLATE (new_friend
) = 0;
8723 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8725 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
8726 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
8727 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
8730 /* The mangled name for the NEW_FRIEND is incorrect. The function
8731 is not a template instantiation and should not be mangled like
8732 one. Therefore, we forget the mangling here; we'll recompute it
8733 later if we need it. */
8734 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
8736 SET_DECL_RTL (new_friend
, NULL
);
8737 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
8740 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
8743 tree new_friend_template_info
;
8744 tree new_friend_result_template_info
;
8746 int new_friend_is_defn
;
8748 /* We must save some information from NEW_FRIEND before calling
8749 duplicate decls since that function will free NEW_FRIEND if
8751 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
8752 new_friend_is_defn
=
8753 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8754 (template_for_substitution (new_friend
)))
8756 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
8758 /* This declaration is a `primary' template. */
8759 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
8761 new_friend_result_template_info
8762 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
8765 new_friend_result_template_info
= NULL_TREE
;
8767 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8768 if (new_friend_is_defn
)
8769 DECL_INITIAL (new_friend
) = error_mark_node
;
8771 /* Inside pushdecl_namespace_level, we will push into the
8772 current namespace. However, the friend function should go
8773 into the namespace of the template. */
8774 ns
= decl_namespace_context (new_friend
);
8775 push_nested_namespace (ns
);
8776 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
8777 pop_nested_namespace (ns
);
8779 if (old_decl
== error_mark_node
)
8780 return error_mark_node
;
8782 if (old_decl
!= new_friend
)
8784 /* This new friend declaration matched an existing
8785 declaration. For example, given:
8787 template <class T> void f(T);
8788 template <class U> class C {
8789 template <class T> friend void f(T) {}
8792 the friend declaration actually provides the definition
8793 of `f', once C has been instantiated for some type. So,
8794 old_decl will be the out-of-class template declaration,
8795 while new_friend is the in-class definition.
8797 But, if `f' was called before this point, the
8798 instantiation of `f' will have DECL_TI_ARGS corresponding
8799 to `T' but not to `U', references to which might appear
8800 in the definition of `f'. Previously, the most general
8801 template for an instantiation of `f' was the out-of-class
8802 version; now it is the in-class version. Therefore, we
8803 run through all specialization of `f', adding to their
8804 DECL_TI_ARGS appropriately. In particular, they need a
8805 new set of outer arguments, corresponding to the
8806 arguments for this class instantiation.
8808 The same situation can arise with something like this:
8811 template <class T> class C {
8815 when `C<int>' is instantiated. Now, `f(int)' is defined
8818 if (!new_friend_is_defn
)
8819 /* On the other hand, if the in-class declaration does
8820 *not* provide a definition, then we don't want to alter
8821 existing definitions. We can just leave everything
8826 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
8827 tree new_args
= TI_ARGS (new_friend_template_info
);
8829 /* Overwrite whatever template info was there before, if
8830 any, with the new template information pertaining to
8832 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
8834 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
8836 /* We should have called reregister_specialization in
8838 gcc_assert (retrieve_specialization (new_template
,
8842 /* Instantiate it if the global has already been used. */
8843 if (DECL_ODR_USED (old_decl
))
8844 instantiate_decl (old_decl
, /*defer_ok=*/true,
8845 /*expl_inst_class_mem_p=*/false);
8851 /* Indicate that the old function template is a partial
8853 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
8854 = new_friend_result_template_info
;
8856 gcc_assert (new_template
8857 == most_general_template (new_template
));
8858 gcc_assert (new_template
!= old_decl
);
8860 /* Reassign any specializations already in the hash table
8861 to the new more general template, and add the
8862 additional template args. */
8863 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
8867 tree spec
= TREE_VALUE (t
);
8870 elt
.tmpl
= old_decl
;
8871 elt
.args
= DECL_TI_ARGS (spec
);
8872 elt
.spec
= NULL_TREE
;
8874 decl_specializations
->remove_elt (&elt
);
8877 = add_outermost_template_args (new_args
,
8878 DECL_TI_ARGS (spec
));
8880 register_specialization
8881 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
8884 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
8888 /* The information from NEW_FRIEND has been merged into OLD_DECL
8889 by duplicate_decls. */
8890 new_friend
= old_decl
;
8895 tree context
= DECL_CONTEXT (new_friend
);
8899 template <class T> class C {
8900 template <class U> friend void C1<U>::f (); // case 1
8901 friend void C2<T>::f (); // case 2
8903 we only need to make sure CONTEXT is a complete type for
8904 case 2. To distinguish between the two cases, we note that
8905 CONTEXT of case 1 remains dependent type after tsubst while
8906 this isn't true for case 2. */
8907 ++processing_template_decl
;
8908 dependent_p
= dependent_type_p (context
);
8909 --processing_template_decl
;
8912 && !complete_type_or_else (context
, NULL_TREE
))
8913 return error_mark_node
;
8915 if (COMPLETE_TYPE_P (context
))
8917 tree fn
= new_friend
;
8918 /* do_friend adds the TEMPLATE_DECL for any member friend
8919 template even if it isn't a member template, i.e.
8920 template <class T> friend A<T>::f();
8921 Look through it in that case. */
8922 if (TREE_CODE (fn
) == TEMPLATE_DECL
8923 && !PRIMARY_TEMPLATE_P (fn
))
8924 fn
= DECL_TEMPLATE_RESULT (fn
);
8925 /* Check to see that the declaration is really present, and,
8926 possibly obtain an improved declaration. */
8927 fn
= check_classfn (context
, fn
, NULL_TREE
);
8937 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8938 template arguments, as for tsubst.
8940 Returns an appropriate tsubst'd friend type or error_mark_node on
8944 tsubst_friend_class (tree friend_tmpl
, tree args
)
8950 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
8952 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
8953 return TREE_TYPE (t
);
8956 context
= CP_DECL_CONTEXT (friend_tmpl
);
8958 if (context
!= global_namespace
)
8960 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8961 push_nested_namespace (context
);
8963 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
8966 /* Look for a class template declaration. We look for hidden names
8967 because two friend declarations of the same template are the
8968 same. For example, in:
8971 template <typename> friend class F;
8973 template <typename> struct B {
8974 template <typename> friend class F;
8977 both F templates are the same. */
8978 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
8979 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
8981 /* But, if we don't find one, it might be because we're in a
8982 situation like this:
8990 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8991 for `S<int>', not the TEMPLATE_DECL. */
8992 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
8994 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
8995 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
8998 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
9000 /* The friend template has already been declared. Just
9001 check to see that the declarations match, and install any new
9002 default parameters. We must tsubst the default parameters,
9003 of course. We only need the innermost template parameters
9004 because that is all that redeclare_class_template will look
9006 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
9007 > TMPL_ARGS_DEPTH (args
))
9010 location_t saved_input_location
;
9011 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
9012 args
, tf_warning_or_error
);
9014 saved_input_location
= input_location
;
9015 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
9016 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
9017 input_location
= saved_input_location
;
9021 friend_type
= TREE_TYPE (tmpl
);
9025 /* The friend template has not already been declared. In this
9026 case, the instantiation of the template class will cause the
9027 injection of this template into the global scope. */
9028 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
9029 if (tmpl
== error_mark_node
)
9030 return error_mark_node
;
9032 /* The new TMPL is not an instantiation of anything, so we
9033 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
9034 the new type because that is supposed to be the corresponding
9035 template decl, i.e., TMPL. */
9036 DECL_USE_TEMPLATE (tmpl
) = 0;
9037 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
9038 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
9039 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
9040 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
9042 /* Inject this template into the global scope. */
9043 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
9046 if (context
!= global_namespace
)
9048 if (TREE_CODE (context
) == NAMESPACE_DECL
)
9049 pop_nested_namespace (context
);
9051 pop_nested_class ();
9057 /* Returns zero if TYPE cannot be completed later due to circularity.
9058 Otherwise returns one. */
9061 can_complete_type_without_circularity (tree type
)
9063 if (type
== NULL_TREE
|| type
== error_mark_node
)
9065 else if (COMPLETE_TYPE_P (type
))
9067 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
9068 return can_complete_type_without_circularity (TREE_TYPE (type
));
9069 else if (CLASS_TYPE_P (type
)
9070 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
9076 static tree
tsubst_omp_clauses (tree
, bool, tree
, tsubst_flags_t
, tree
);
9078 /* Apply any attributes which had to be deferred until instantiation
9079 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
9080 ARGS, COMPLAIN, IN_DECL are as tsubst. */
9083 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
9084 tree args
, tsubst_flags_t complain
, tree in_decl
)
9086 tree last_dep
= NULL_TREE
;
9090 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
9091 if (ATTR_IS_DEPENDENT (t
))
9094 attributes
= copy_list (attributes
);
9098 if (DECL_P (*decl_p
))
9100 if (TREE_TYPE (*decl_p
) == error_mark_node
)
9102 p
= &DECL_ATTRIBUTES (*decl_p
);
9105 p
= &TYPE_ATTRIBUTES (*decl_p
);
9109 tree late_attrs
= NULL_TREE
;
9110 tree
*q
= &late_attrs
;
9112 for (*p
= attributes
; *p
; )
9115 if (ATTR_IS_DEPENDENT (t
))
9117 *p
= TREE_CHAIN (t
);
9118 TREE_CHAIN (t
) = NULL_TREE
;
9119 if ((flag_openmp
|| flag_openmp_simd
|| flag_cilkplus
)
9120 && is_attribute_p ("omp declare simd",
9121 get_attribute_name (t
))
9124 tree clauses
= TREE_VALUE (TREE_VALUE (t
));
9125 clauses
= tsubst_omp_clauses (clauses
, true, args
,
9127 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
9128 clauses
= finish_omp_clauses (clauses
);
9129 tree parms
= DECL_ARGUMENTS (*decl_p
);
9131 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
9133 TREE_VALUE (TREE_VALUE (t
)) = clauses
;
9135 TREE_VALUE (t
) = NULL_TREE
;
9137 /* If the first attribute argument is an identifier, don't
9138 pass it through tsubst. Attributes like mode, format,
9139 cleanup and several target specific attributes expect it
9141 else if (attribute_takes_identifier_p (get_attribute_name (t
))
9145 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
9147 /*integral_constant_expression_p=*/false);
9148 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
9150 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
9153 else if (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
)))
9155 /* An attribute pack expansion. */
9156 tree purp
= TREE_PURPOSE (t
);
9157 tree pack
= (tsubst_pack_expansion
9158 (TREE_VALUE (t
), args
, complain
, in_decl
));
9159 int len
= TREE_VEC_LENGTH (pack
);
9160 for (int i
= 0; i
< len
; ++i
)
9162 tree elt
= TREE_VEC_ELT (pack
, i
);
9163 *q
= build_tree_list (purp
, elt
);
9164 q
= &TREE_CHAIN (*q
);
9170 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
9171 /*integral_constant_expression_p=*/false);
9173 q
= &TREE_CHAIN (t
);
9176 p
= &TREE_CHAIN (t
);
9179 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
9183 /* Perform (or defer) access check for typedefs that were referenced
9184 from within the template TMPL code.
9185 This is a subroutine of instantiate_decl and instantiate_class_template.
9186 TMPL is the template to consider and TARGS is the list of arguments of
9190 perform_typedefs_access_check (tree tmpl
, tree targs
)
9192 location_t saved_location
;
9194 qualified_typedef_usage_t
*iter
;
9197 || (!CLASS_TYPE_P (tmpl
)
9198 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
9201 saved_location
= input_location
;
9202 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
9204 tree type_decl
= iter
->typedef_decl
;
9205 tree type_scope
= iter
->context
;
9207 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
9210 if (uses_template_parms (type_decl
))
9211 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
9212 if (uses_template_parms (type_scope
))
9213 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
9215 /* Make access check error messages point to the location
9216 of the use of the typedef. */
9217 input_location
= iter
->locus
;
9218 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
9219 type_decl
, type_decl
,
9220 tf_warning_or_error
);
9222 input_location
= saved_location
;
9226 instantiate_class_template_1 (tree type
)
9228 tree templ
, args
, pattern
, t
, member
;
9232 unsigned int saved_maximum_field_alignment
;
9235 if (type
== error_mark_node
)
9236 return error_mark_node
;
9238 if (COMPLETE_OR_OPEN_TYPE_P (type
)
9239 || uses_template_parms (type
))
9242 /* Figure out which template is being instantiated. */
9243 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
9244 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
9246 /* Determine what specialization of the original template to
9248 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
9249 if (t
== error_mark_node
)
9251 TYPE_BEING_DEFINED (type
) = 1;
9252 return error_mark_node
;
9256 /* This TYPE is actually an instantiation of a partial
9257 specialization. We replace the innermost set of ARGS with
9258 the arguments appropriate for substitution. For example,
9261 template <class T> struct S {};
9262 template <class T> struct S<T*> {};
9264 and supposing that we are instantiating S<int*>, ARGS will
9265 presently be {int*} -- but we need {int}. */
9266 pattern
= TREE_TYPE (t
);
9267 args
= TREE_PURPOSE (t
);
9271 pattern
= TREE_TYPE (templ
);
9272 args
= CLASSTYPE_TI_ARGS (type
);
9275 /* If the template we're instantiating is incomplete, then clearly
9276 there's nothing we can do. */
9277 if (!COMPLETE_TYPE_P (pattern
))
9280 /* If we've recursively instantiated too many templates, stop. */
9281 if (! push_tinst_level (type
))
9284 /* Now we're really doing the instantiation. Mark the type as in
9285 the process of being defined. */
9286 TYPE_BEING_DEFINED (type
) = 1;
9288 /* We may be in the middle of deferred access check. Disable
9290 push_deferring_access_checks (dk_no_deferred
);
9292 int saved_unevaluated_operand
= cp_unevaluated_operand
;
9293 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9295 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
9296 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
9297 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
9298 fn_context
= error_mark_node
;
9300 push_to_top_level ();
9303 cp_unevaluated_operand
= 0;
9304 c_inhibit_evaluation_warnings
= 0;
9306 /* Use #pragma pack from the template context. */
9307 saved_maximum_field_alignment
= maximum_field_alignment
;
9308 maximum_field_alignment
= TYPE_PRECISION (pattern
);
9310 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
9312 /* Set the input location to the most specialized template definition.
9313 This is needed if tsubsting causes an error. */
9314 typedecl
= TYPE_MAIN_DECL (pattern
);
9315 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
9316 DECL_SOURCE_LOCATION (typedecl
);
9318 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
9319 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
9320 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
9321 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
9322 if (ANON_AGGR_TYPE_P (pattern
))
9323 SET_ANON_AGGR_TYPE_P (type
);
9324 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
9326 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
9327 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
9328 /* Adjust visibility for template arguments. */
9329 determine_visibility (TYPE_MAIN_DECL (type
));
9331 if (CLASS_TYPE_P (type
))
9332 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
9334 pbinfo
= TYPE_BINFO (pattern
);
9336 /* We should never instantiate a nested class before its enclosing
9337 class; we need to look up the nested class by name before we can
9338 instantiate it, and that lookup should instantiate the enclosing
9340 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
9341 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
9343 base_list
= NULL_TREE
;
9344 if (BINFO_N_BASE_BINFOS (pbinfo
))
9350 /* We must enter the scope containing the type, as that is where
9351 the accessibility of types named in dependent bases are
9353 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
9355 /* Substitute into each of the bases to determine the actual
9357 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
9360 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
9361 tree expanded_bases
= NULL_TREE
;
9364 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
9367 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
9368 args
, tf_error
, NULL_TREE
);
9369 if (expanded_bases
== error_mark_node
)
9372 len
= TREE_VEC_LENGTH (expanded_bases
);
9375 for (idx
= 0; idx
< len
; idx
++)
9378 /* Extract the already-expanded base class. */
9379 base
= TREE_VEC_ELT (expanded_bases
, idx
);
9381 /* Substitute to figure out the base class. */
9382 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
9385 if (base
== error_mark_node
)
9388 base_list
= tree_cons (access
, base
, base_list
);
9389 if (BINFO_VIRTUAL_P (pbase_binfo
))
9390 TREE_TYPE (base_list
) = integer_type_node
;
9394 /* The list is now in reverse order; correct that. */
9395 base_list
= nreverse (base_list
);
9398 pop_scope (pushed_scope
);
9400 /* Now call xref_basetypes to set up all the base-class
9402 xref_basetypes (type
, base_list
);
9404 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
9405 (int) ATTR_FLAG_TYPE_IN_PLACE
,
9406 args
, tf_error
, NULL_TREE
);
9407 fixup_attribute_variants (type
);
9409 /* Now that our base classes are set up, enter the scope of the
9410 class, so that name lookups into base classes, etc. will work
9411 correctly. This is precisely analogous to what we do in
9412 begin_class_definition when defining an ordinary non-template
9413 class, except we also need to push the enclosing classes. */
9414 push_nested_class (type
);
9416 /* Now members are processed in the order of declaration. */
9417 for (member
= CLASSTYPE_DECL_LIST (pattern
);
9418 member
; member
= TREE_CHAIN (member
))
9420 tree t
= TREE_VALUE (member
);
9422 if (TREE_PURPOSE (member
))
9426 /* Build new CLASSTYPE_NESTED_UTDS. */
9429 bool class_template_p
;
9431 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
9432 && TYPE_LANG_SPECIFIC (t
)
9433 && CLASSTYPE_IS_TEMPLATE (t
));
9434 /* If the member is a class template, then -- even after
9435 substitution -- there may be dependent types in the
9436 template argument list for the class. We increment
9437 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
9438 that function will assume that no types are dependent
9439 when outside of a template. */
9440 if (class_template_p
)
9441 ++processing_template_decl
;
9442 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
9443 if (class_template_p
)
9444 --processing_template_decl
;
9445 if (newtag
== error_mark_node
)
9448 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
9450 tree name
= TYPE_IDENTIFIER (t
);
9452 if (class_template_p
)
9453 /* Unfortunately, lookup_template_class sets
9454 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
9455 instantiation (i.e., for the type of a member
9456 template class nested within a template class.)
9457 This behavior is required for
9458 maybe_process_partial_specialization to work
9459 correctly, but is not accurate in this case;
9460 the TAG is not an instantiation of anything.
9461 (The corresponding TEMPLATE_DECL is an
9462 instantiation, but the TYPE is not.) */
9463 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
9465 /* Now, we call pushtag to put this NEWTAG into the scope of
9466 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
9467 pushtag calling push_template_decl. We don't have to do
9468 this for enums because it will already have been done in
9471 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
9472 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
9475 else if (DECL_DECLARES_FUNCTION_P (t
))
9477 /* Build new TYPE_METHODS. */
9480 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9481 ++processing_template_decl
;
9482 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
9483 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9484 --processing_template_decl
;
9485 set_current_access_from_decl (r
);
9486 finish_member_declaration (r
);
9487 /* Instantiate members marked with attribute used. */
9488 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
9490 if (TREE_CODE (r
) == FUNCTION_DECL
9491 && DECL_OMP_DECLARE_REDUCTION_P (r
))
9492 cp_check_omp_declare_reduction (r
);
9494 else if (DECL_CLASS_TEMPLATE_P (t
)
9495 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
9496 /* A closure type for a lambda in a default argument for a
9497 member template. Ignore it; it will be instantiated with
9498 the default argument. */;
9501 /* Build new TYPE_FIELDS. */
9502 if (TREE_CODE (t
) == STATIC_ASSERT
)
9506 ++c_inhibit_evaluation_warnings
;
9508 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
9509 tf_warning_or_error
, NULL_TREE
,
9510 /*integral_constant_expression_p=*/true);
9511 --c_inhibit_evaluation_warnings
;
9513 finish_static_assert (condition
,
9514 STATIC_ASSERT_MESSAGE (t
),
9515 STATIC_ASSERT_SOURCE_LOCATION (t
),
9518 else if (TREE_CODE (t
) != CONST_DECL
)
9521 tree vec
= NULL_TREE
;
9524 /* The file and line for this declaration, to
9525 assist in error message reporting. Since we
9526 called push_tinst_level above, we don't need to
9528 input_location
= DECL_SOURCE_LOCATION (t
);
9530 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9531 ++processing_template_decl
;
9532 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
9533 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9534 --processing_template_decl
;
9536 if (TREE_CODE (r
) == TREE_VEC
)
9538 /* A capture pack became multiple fields. */
9540 len
= TREE_VEC_LENGTH (vec
);
9543 for (int i
= 0; i
< len
; ++i
)
9546 r
= TREE_VEC_ELT (vec
, i
);
9551 [t]he initialization (and any associated
9552 side-effects) of a static data member does
9553 not occur unless the static data member is
9554 itself used in a way that requires the
9555 definition of the static data member to
9558 Therefore, we do not substitute into the
9559 initialized for the static data member here. */
9560 finish_static_data_member_decl
9563 /*init_const_expr_p=*/false,
9564 /*asmspec_tree=*/NULL_TREE
,
9566 /* Instantiate members marked with attribute used. */
9567 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
9570 else if (TREE_CODE (r
) == FIELD_DECL
)
9572 /* Determine whether R has a valid type and can be
9573 completed later. If R is invalid, then its type
9574 is replaced by error_mark_node. */
9575 tree rtype
= TREE_TYPE (r
);
9576 if (can_complete_type_without_circularity (rtype
))
9577 complete_type (rtype
);
9579 if (!COMPLETE_TYPE_P (rtype
))
9581 cxx_incomplete_type_error (r
, rtype
);
9582 TREE_TYPE (r
) = error_mark_node
;
9586 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9587 such a thing will already have been added to the field
9588 list by tsubst_enum in finish_member_declaration in the
9589 CLASSTYPE_NESTED_UTDS case above. */
9590 if (!(TREE_CODE (r
) == TYPE_DECL
9591 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
9592 && DECL_ARTIFICIAL (r
)))
9594 set_current_access_from_decl (r
);
9595 finish_member_declaration (r
);
9603 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
9604 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9606 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9608 tree friend_type
= t
;
9609 bool adjust_processing_template_decl
= false;
9611 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9613 /* template <class T> friend class C; */
9614 friend_type
= tsubst_friend_class (friend_type
, args
);
9615 adjust_processing_template_decl
= true;
9617 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
9619 /* template <class T> friend class C::D; */
9620 friend_type
= tsubst (friend_type
, args
,
9621 tf_warning_or_error
, NULL_TREE
);
9622 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9623 friend_type
= TREE_TYPE (friend_type
);
9624 adjust_processing_template_decl
= true;
9626 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
9627 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
9629 /* This could be either
9633 when dependent_type_p is false or
9635 template <class U> friend class T::C;
9638 friend_type
= tsubst (friend_type
, args
,
9639 tf_warning_or_error
, NULL_TREE
);
9640 /* Bump processing_template_decl for correct
9641 dependent_type_p calculation. */
9642 ++processing_template_decl
;
9643 if (dependent_type_p (friend_type
))
9644 adjust_processing_template_decl
= true;
9645 --processing_template_decl
;
9647 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
9648 && hidden_name_p (TYPE_NAME (friend_type
)))
9652 where C hasn't been declared yet. Let's lookup name
9653 from namespace scope directly, bypassing any name that
9654 come from dependent base class. */
9655 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
9657 /* The call to xref_tag_from_type does injection for friend
9659 push_nested_namespace (ns
);
9661 xref_tag_from_type (friend_type
, NULL_TREE
,
9662 /*tag_scope=*/ts_current
);
9663 pop_nested_namespace (ns
);
9665 else if (uses_template_parms (friend_type
))
9666 /* friend class C<T>; */
9667 friend_type
= tsubst (friend_type
, args
,
9668 tf_warning_or_error
, NULL_TREE
);
9673 where C is already declared or
9675 friend class C<int>;
9677 We don't have to do anything in these cases. */
9679 if (adjust_processing_template_decl
)
9680 /* Trick make_friend_class into realizing that the friend
9681 we're adding is a template, not an ordinary class. It's
9682 important that we use make_friend_class since it will
9683 perform some error-checking and output cross-reference
9685 ++processing_template_decl
;
9687 if (friend_type
!= error_mark_node
)
9688 make_friend_class (type
, friend_type
, /*complain=*/false);
9690 if (adjust_processing_template_decl
)
9691 --processing_template_decl
;
9695 /* Build new DECL_FRIENDLIST. */
9698 /* The file and line for this declaration, to
9699 assist in error message reporting. Since we
9700 called push_tinst_level above, we don't need to
9702 input_location
= DECL_SOURCE_LOCATION (t
);
9704 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9706 ++processing_template_decl
;
9707 push_deferring_access_checks (dk_no_check
);
9710 r
= tsubst_friend_function (t
, args
);
9711 add_friend (type
, r
, /*complain=*/false);
9712 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9714 pop_deferring_access_checks ();
9715 --processing_template_decl
;
9723 /* Restore these before substituting into the lambda capture
9725 cp_unevaluated_operand
= saved_unevaluated_operand
;
9726 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
9729 if (tree expr
= CLASSTYPE_LAMBDA_EXPR (type
))
9731 tree decl
= lambda_function (type
);
9734 if (!DECL_TEMPLATE_INFO (decl
)
9735 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl
)) != decl
)
9736 instantiate_decl (decl
, false, false);
9738 /* We need to instantiate the capture list from the template
9739 after we've instantiated the closure members, but before we
9740 consider adding the conversion op. Also keep any captures
9741 that may have been added during instantiation of the op(). */
9742 tree tmpl_expr
= CLASSTYPE_LAMBDA_EXPR (pattern
);
9744 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr
),
9745 args
, tf_warning_or_error
, NULL_TREE
,
9748 LAMBDA_EXPR_CAPTURE_LIST (expr
)
9749 = chainon (tmpl_cap
, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr
)));
9751 maybe_add_lambda_conv_op (type
);
9754 gcc_assert (errorcount
);
9757 /* Set the file and line number information to whatever is given for
9758 the class itself. This puts error messages involving generated
9759 implicit functions at a predictable point, and the same point
9760 that would be used for non-template classes. */
9761 input_location
= DECL_SOURCE_LOCATION (typedecl
);
9763 unreverse_member_declarations (type
);
9764 finish_struct_1 (type
);
9765 TYPE_BEING_DEFINED (type
) = 0;
9767 /* We don't instantiate default arguments for member functions. 14.7.1:
9769 The implicit instantiation of a class template specialization causes
9770 the implicit instantiation of the declarations, but not of the
9771 definitions or default arguments, of the class member functions,
9772 member classes, static data members and member templates.... */
9774 /* Some typedefs referenced from within the template code need to be access
9775 checked at template instantiation time, i.e now. These types were
9776 added to the template at parsing time. Let's get those and perform
9777 the access checks then. */
9778 perform_typedefs_access_check (pattern
, args
);
9779 perform_deferred_access_checks (tf_warning_or_error
);
9780 pop_nested_class ();
9781 maximum_field_alignment
= saved_maximum_field_alignment
;
9783 pop_from_top_level ();
9784 pop_deferring_access_checks ();
9787 /* The vtable for a template class can be emitted in any translation
9788 unit in which the class is instantiated. When there is no key
9789 method, however, finish_struct_1 will already have added TYPE to
9790 the keyed_classes list. */
9791 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
9792 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
9797 /* Wrapper for instantiate_class_template_1. */
9800 instantiate_class_template (tree type
)
9803 timevar_push (TV_TEMPLATE_INST
);
9804 ret
= instantiate_class_template_1 (type
);
9805 timevar_pop (TV_TEMPLATE_INST
);
9810 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9816 else if (TYPE_P (t
))
9817 r
= tsubst (t
, args
, complain
, in_decl
);
9820 if (!(complain
& tf_warning
))
9821 ++c_inhibit_evaluation_warnings
;
9822 r
= tsubst_expr (t
, args
, complain
, in_decl
,
9823 /*integral_constant_expression_p=*/true);
9824 if (!(complain
& tf_warning
))
9825 --c_inhibit_evaluation_warnings
;
9830 /* Given a function parameter pack TMPL_PARM and some function parameters
9831 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9832 and set *SPEC_P to point at the next point in the list. */
9835 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
9837 /* Collect all of the extra "packed" parameters into an
9841 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
9842 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
9843 tree spec_parm
= *spec_p
;
9846 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
9848 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
9851 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9852 parmvec
= make_tree_vec (len
);
9853 parmtypevec
= make_tree_vec (len
);
9854 spec_parm
= *spec_p
;
9855 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
9857 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
9858 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
9861 /* Build the argument packs. */
9862 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
9863 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
9864 TREE_TYPE (argpack
) = argtypepack
;
9865 *spec_p
= spec_parm
;
9870 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9871 NONTYPE_ARGUMENT_PACK. */
9874 make_fnparm_pack (tree spec_parm
)
9876 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
9879 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
9880 pack expansion with no extra args, 2 if it has extra args, or 0
9881 if it is not a pack expansion. */
9884 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
9886 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
9887 if (i
>= TREE_VEC_LENGTH (vec
))
9889 tree elt
= TREE_VEC_ELT (vec
, i
);
9891 /* A decl pack is itself an expansion. */
9892 elt
= TREE_TYPE (elt
);
9893 if (!PACK_EXPANSION_P (elt
))
9895 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
9901 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9904 make_argument_pack_select (tree arg_pack
, unsigned index
)
9906 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
9908 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
9909 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9914 /* This is a subroutine of tsubst_pack_expansion.
9916 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9917 mechanism to store the (non complete list of) arguments of the
9918 substitution and return a non substituted pack expansion, in order
9919 to wait for when we have enough arguments to really perform the
9923 use_pack_expansion_extra_args_p (tree parm_packs
,
9927 /* If one pack has an expansion and another pack has a normal
9928 argument or if one pack has an empty argument and an another
9929 one hasn't then tsubst_pack_expansion cannot perform the
9930 substitution and need to fall back on the
9931 PACK_EXPANSION_EXTRA mechanism. */
9932 if (parm_packs
== NULL_TREE
)
9934 else if (has_empty_arg
)
9937 bool has_expansion_arg
= false;
9938 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
9940 bool has_non_expansion_arg
= false;
9941 for (tree parm_pack
= parm_packs
;
9943 parm_pack
= TREE_CHAIN (parm_pack
))
9945 tree arg
= TREE_VALUE (parm_pack
);
9947 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
9949 /* We can't substitute a pack expansion with extra args into
9953 has_expansion_arg
= true;
9955 has_non_expansion_arg
= true;
9958 if (has_expansion_arg
&& has_non_expansion_arg
)
9964 /* [temp.variadic]/6 says that:
9966 The instantiation of a pack expansion [...]
9967 produces a list E1,E2, ..., En, where N is the number of elements
9968 in the pack expansion parameters.
9970 This subroutine of tsubst_pack_expansion produces one of these Ei.
9972 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9973 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9974 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9975 INDEX is the index 'i' of the element Ei to produce. ARGS,
9976 COMPLAIN, and IN_DECL are the same parameters as for the
9977 tsubst_pack_expansion function.
9979 The function returns the resulting Ei upon successful completion,
9982 Note that this function possibly modifies the ARGS parameter, so
9983 it's the responsibility of the caller to restore it. */
9986 gen_elem_of_pack_expansion_instantiation (tree pattern
,
9989 tree args
/* This parm gets
9991 tsubst_flags_t complain
,
9995 bool ith_elem_is_expansion
= false;
9997 /* For each parameter pack, change the substitution of the parameter
9998 pack to the ith argument in its argument pack, then expand the
10000 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
10002 tree parm
= TREE_PURPOSE (pack
);
10003 tree arg_pack
= TREE_VALUE (pack
);
10004 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
10006 ith_elem_is_expansion
|=
10007 argument_pack_element_is_expansion_p (arg_pack
, index
);
10009 /* Select the Ith argument from the pack. */
10010 if (TREE_CODE (parm
) == PARM_DECL
10011 || TREE_CODE (parm
) == FIELD_DECL
)
10015 aps
= make_argument_pack_select (arg_pack
, index
);
10016 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
10017 return error_mark_node
;
10018 register_local_specialization (aps
, parm
);
10021 aps
= retrieve_local_specialization (parm
);
10026 template_parm_level_and_index (parm
, &level
, &idx
);
10030 aps
= make_argument_pack_select (arg_pack
, index
);
10031 /* Update the corresponding argument. */
10032 TMPL_ARG (args
, level
, idx
) = aps
;
10035 /* Re-use the ARGUMENT_PACK_SELECT. */
10036 aps
= TMPL_ARG (args
, level
, idx
);
10038 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
10041 /* Substitute into the PATTERN with the (possibly altered)
10043 if (pattern
== in_decl
)
10044 /* Expanding a fixed parameter pack from
10045 coerce_template_parameter_pack. */
10046 t
= tsubst_decl (pattern
, args
, complain
);
10047 else if (!TYPE_P (pattern
))
10048 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
10049 /*integral_constant_expression_p=*/false);
10051 t
= tsubst (pattern
, args
, complain
, in_decl
);
10053 /* If the Ith argument pack element is a pack expansion, then
10054 the Ith element resulting from the substituting is going to
10055 be a pack expansion as well. */
10056 if (ith_elem_is_expansion
)
10057 t
= make_pack_expansion (t
);
10062 /* Substitute ARGS into T, which is an pack expansion
10063 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
10064 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
10065 (if only a partial substitution could be performed) or
10066 ERROR_MARK_NODE if there was an error. */
10068 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
10072 tree pack
, packs
= NULL_TREE
;
10073 bool unsubstituted_packs
= false;
10076 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
10077 bool need_local_specializations
= false;
10080 gcc_assert (PACK_EXPANSION_P (t
));
10081 pattern
= PACK_EXPANSION_PATTERN (t
);
10083 /* Add in any args remembered from an earlier partial instantiation. */
10084 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
10086 levels
= TMPL_ARGS_DEPTH (args
);
10088 /* Determine the argument packs that will instantiate the parameter
10089 packs used in the expansion expression. While we're at it,
10090 compute the number of arguments to be expanded and make sure it
10092 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
10093 pack
= TREE_CHAIN (pack
))
10095 tree parm_pack
= TREE_VALUE (pack
);
10096 tree arg_pack
= NULL_TREE
;
10097 tree orig_arg
= NULL_TREE
;
10100 if (TREE_CODE (parm_pack
) == BASES
)
10102 if (BASES_DIRECT (parm_pack
))
10103 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
10104 args
, complain
, in_decl
, false));
10106 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
10107 args
, complain
, in_decl
, false));
10109 if (TREE_CODE (parm_pack
) == PARM_DECL
)
10111 if (PACK_EXPANSION_LOCAL_P (t
))
10112 arg_pack
= retrieve_local_specialization (parm_pack
);
10115 /* We can't rely on local_specializations for a parameter
10116 name used later in a function declaration (such as in a
10117 late-specified return type). Even if it exists, it might
10118 have the wrong value for a recursive call. Just make a
10119 dummy decl, since it's only used for its type. */
10120 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
10121 if (arg_pack
&& DECL_PACK_P (arg_pack
))
10122 /* Partial instantiation of the parm_pack, we can't build
10123 up an argument pack yet. */
10124 arg_pack
= NULL_TREE
;
10126 arg_pack
= make_fnparm_pack (arg_pack
);
10127 need_local_specializations
= true;
10130 else if (TREE_CODE (parm_pack
) == FIELD_DECL
)
10131 arg_pack
= tsubst_copy (parm_pack
, args
, complain
, in_decl
);
10135 template_parm_level_and_index (parm_pack
, &level
, &idx
);
10137 if (level
<= levels
)
10138 arg_pack
= TMPL_ARG (args
, level
, idx
);
10141 orig_arg
= arg_pack
;
10142 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
10143 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
10145 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
10146 /* This can only happen if we forget to expand an argument
10147 pack somewhere else. Just return an error, silently. */
10149 result
= make_tree_vec (1);
10150 TREE_VEC_ELT (result
, 0) = error_mark_node
;
10157 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
10159 /* Don't bother trying to do a partial substitution with
10160 incomplete packs; we'll try again after deduction. */
10161 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
10166 else if (len
!= my_len
)
10168 if (!(complain
& tf_error
))
10169 /* Fail quietly. */;
10170 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
10171 error ("mismatched argument pack lengths while expanding "
10175 error ("mismatched argument pack lengths while expanding "
10178 return error_mark_node
;
10181 /* Keep track of the parameter packs and their corresponding
10183 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
10184 TREE_TYPE (packs
) = orig_arg
;
10188 /* We can't substitute for this parameter pack. We use a flag as
10189 well as the missing_level counter because function parameter
10190 packs don't have a level. */
10191 unsubstituted_packs
= true;
10195 /* If the expansion is just T..., return the matching argument pack. */
10196 if (!unsubstituted_packs
10197 && TREE_PURPOSE (packs
) == pattern
)
10199 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
10200 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
10201 || pack_expansion_args_count (args
))
10203 /* Otherwise use the normal path so we get convert_from_reference. */
10206 /* We cannot expand this expansion expression, because we don't have
10207 all of the argument packs we need. */
10208 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
10210 /* We got some full packs, but we can't substitute them in until we
10211 have values for all the packs. So remember these until then. */
10213 t
= make_pack_expansion (pattern
);
10214 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
10217 else if (unsubstituted_packs
)
10219 /* There were no real arguments, we're just replacing a parameter
10220 pack with another version of itself. Substitute into the
10221 pattern and return a PACK_EXPANSION_*. The caller will need to
10223 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
10224 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
10225 /*integral_constant_expression_p=*/false);
10227 t
= tsubst (pattern
, args
, complain
, in_decl
);
10228 t
= make_pack_expansion (t
);
10232 gcc_assert (len
>= 0);
10234 if (need_local_specializations
)
10236 /* We're in a late-specified return type, so create our own local
10237 specializations map; the current map is either NULL or (in the
10238 case of recursive unification) might have bindings that we don't
10239 want to use or alter. */
10240 saved_local_specializations
= local_specializations
;
10241 local_specializations
= new hash_map
<tree
, tree
>;
10244 /* For each argument in each argument pack, substitute into the
10246 result
= make_tree_vec (len
);
10247 for (i
= 0; i
< len
; ++i
)
10249 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
10253 TREE_VEC_ELT (result
, i
) = t
;
10254 if (t
== error_mark_node
)
10256 result
= error_mark_node
;
10261 /* Update ARGS to restore the substitution from parameter packs to
10262 their argument packs. */
10263 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
10265 tree parm
= TREE_PURPOSE (pack
);
10267 if (TREE_CODE (parm
) == PARM_DECL
10268 || TREE_CODE (parm
) == FIELD_DECL
)
10269 register_local_specialization (TREE_TYPE (pack
), parm
);
10274 if (TREE_VALUE (pack
) == NULL_TREE
)
10277 template_parm_level_and_index (parm
, &level
, &idx
);
10279 /* Update the corresponding argument. */
10280 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
10281 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
10284 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
10288 if (need_local_specializations
)
10290 delete local_specializations
;
10291 local_specializations
= saved_local_specializations
;
10297 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
10298 TMPL. We do this using DECL_PARM_INDEX, which should work even with
10299 parameter packs; all parms generated from a function parameter pack will
10300 have the same DECL_PARM_INDEX. */
10303 get_pattern_parm (tree parm
, tree tmpl
)
10305 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
10308 if (DECL_ARTIFICIAL (parm
))
10310 for (patparm
= DECL_ARGUMENTS (pattern
);
10311 patparm
; patparm
= DECL_CHAIN (patparm
))
10312 if (DECL_ARTIFICIAL (patparm
)
10313 && DECL_NAME (parm
) == DECL_NAME (patparm
))
10318 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
10319 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
10320 gcc_assert (DECL_PARM_INDEX (patparm
)
10321 == DECL_PARM_INDEX (parm
));
10327 /* Substitute ARGS into the vector or list of template arguments T. */
10330 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10333 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
10336 if (t
== error_mark_node
)
10337 return error_mark_node
;
10339 len
= TREE_VEC_LENGTH (t
);
10340 elts
= XALLOCAVEC (tree
, len
);
10342 for (i
= 0; i
< len
; i
++)
10344 tree orig_arg
= TREE_VEC_ELT (t
, i
);
10347 if (TREE_CODE (orig_arg
) == TREE_VEC
)
10348 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
10349 else if (PACK_EXPANSION_P (orig_arg
))
10351 /* Substitute into an expansion expression. */
10352 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
10354 if (TREE_CODE (new_arg
) == TREE_VEC
)
10355 /* Add to the expanded length adjustment the number of
10356 expanded arguments. We subtract one from this
10357 measurement, because the argument pack expression
10358 itself is already counted as 1 in
10359 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
10360 the argument pack is empty. */
10361 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
10363 else if (ARGUMENT_PACK_P (orig_arg
))
10365 /* Substitute into each of the arguments. */
10366 new_arg
= TYPE_P (orig_arg
)
10367 ? cxx_make_type (TREE_CODE (orig_arg
))
10368 : make_node (TREE_CODE (orig_arg
));
10370 SET_ARGUMENT_PACK_ARGS (
10372 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
10373 args
, complain
, in_decl
));
10375 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
10376 new_arg
= error_mark_node
;
10378 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
10379 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
10380 complain
, in_decl
);
10381 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
10383 if (TREE_TYPE (new_arg
) == error_mark_node
)
10384 new_arg
= error_mark_node
;
10388 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
10390 if (new_arg
== error_mark_node
)
10391 return error_mark_node
;
10394 if (new_arg
!= orig_arg
)
10401 /* Make space for the expanded arguments coming from template
10403 t
= make_tree_vec (len
+ expanded_len_adjust
);
10404 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
10405 arguments for a member template.
10406 In that case each TREE_VEC in ORIG_T represents a level of template
10407 arguments, and ORIG_T won't carry any non defaulted argument count.
10408 It will rather be the nested TREE_VECs that will carry one.
10409 In other words, ORIG_T carries a non defaulted argument count only
10410 if it doesn't contain any nested TREE_VEC. */
10411 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
10413 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
10414 count
+= expanded_len_adjust
;
10415 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
10417 for (i
= 0, out
= 0; i
< len
; i
++)
10419 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
10420 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
10421 && TREE_CODE (elts
[i
]) == TREE_VEC
)
10425 /* Now expand the template argument pack "in place". */
10426 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
10427 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
10431 TREE_VEC_ELT (t
, out
) = elts
[i
];
10439 /* Return the result of substituting ARGS into the template parameters
10440 given by PARMS. If there are m levels of ARGS and m + n levels of
10441 PARMS, then the result will contain n levels of PARMS. For
10442 example, if PARMS is `template <class T> template <class U>
10443 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
10444 result will be `template <int*, double, class V>'. */
10447 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
10449 tree r
= NULL_TREE
;
10452 /* When substituting into a template, we must set
10453 PROCESSING_TEMPLATE_DECL as the template parameters may be
10454 dependent if they are based on one-another, and the dependency
10455 predicates are short-circuit outside of templates. */
10456 ++processing_template_decl
;
10458 for (new_parms
= &r
;
10459 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
10460 new_parms
= &(TREE_CHAIN (*new_parms
)),
10461 parms
= TREE_CHAIN (parms
))
10464 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
10467 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
10471 if (parms
== error_mark_node
)
10474 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
10476 if (tuple
== error_mark_node
)
10479 TREE_VEC_ELT (new_vec
, i
) =
10480 tsubst_template_parm (tuple
, args
, complain
);
10484 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
10485 - TMPL_ARGS_DEPTH (args
)),
10486 new_vec
, NULL_TREE
);
10489 --processing_template_decl
;
10494 /* Return the result of substituting ARGS into one template parameter
10495 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
10496 parameter and which TREE_PURPOSE is the default argument of the
10497 template parameter. */
10500 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
10502 tree default_value
, parm_decl
;
10504 if (args
== NULL_TREE
10506 || t
== error_mark_node
)
10509 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
10511 default_value
= TREE_PURPOSE (t
);
10512 parm_decl
= TREE_VALUE (t
);
10514 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
10515 if (TREE_CODE (parm_decl
) == PARM_DECL
10516 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
10517 parm_decl
= error_mark_node
;
10518 default_value
= tsubst_template_arg (default_value
, args
,
10519 complain
, NULL_TREE
);
10521 return build_tree_list (default_value
, parm_decl
);
10524 /* Substitute the ARGS into the indicated aggregate (or enumeration)
10525 type T. If T is not an aggregate or enumeration type, it is
10526 handled as if by tsubst. IN_DECL is as for tsubst. If
10527 ENTERING_SCOPE is nonzero, T is the context for a template which
10528 we are presently tsubst'ing. Return the substituted value. */
10531 tsubst_aggr_type (tree t
,
10533 tsubst_flags_t complain
,
10535 int entering_scope
)
10537 if (t
== NULL_TREE
)
10540 switch (TREE_CODE (t
))
10543 if (TYPE_PTRMEMFUNC_P (t
))
10544 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
10546 /* Else fall through. */
10547 case ENUMERAL_TYPE
:
10549 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
10554 int saved_unevaluated_operand
;
10555 int saved_inhibit_evaluation_warnings
;
10557 /* In "sizeof(X<I>)" we need to evaluate "I". */
10558 saved_unevaluated_operand
= cp_unevaluated_operand
;
10559 cp_unevaluated_operand
= 0;
10560 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
10561 c_inhibit_evaluation_warnings
= 0;
10563 /* First, determine the context for the type we are looking
10565 context
= TYPE_CONTEXT (t
);
10566 if (context
&& TYPE_P (context
))
10568 context
= tsubst_aggr_type (context
, args
, complain
,
10569 in_decl
, /*entering_scope=*/1);
10570 /* If context is a nested class inside a class template,
10571 it may still need to be instantiated (c++/33959). */
10572 context
= complete_type (context
);
10575 /* Then, figure out what arguments are appropriate for the
10576 type we are trying to find. For example, given:
10578 template <class T> struct S;
10579 template <class T, class U> void f(T, U) { S<U> su; }
10581 and supposing that we are instantiating f<int, double>,
10582 then our ARGS will be {int, double}, but, when looking up
10583 S we only want {double}. */
10584 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
10585 complain
, in_decl
);
10586 if (argvec
== error_mark_node
)
10587 r
= error_mark_node
;
10590 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
10591 entering_scope
, complain
);
10592 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
10595 cp_unevaluated_operand
= saved_unevaluated_operand
;
10596 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
10601 /* This is not a template type, so there's nothing to do. */
10605 return tsubst (t
, args
, complain
, in_decl
);
10609 /* Substitute into the default argument ARG (a default argument for
10610 FN), which has the indicated TYPE. */
10613 tsubst_default_argument (tree fn
, tree type
, tree arg
, tsubst_flags_t complain
)
10615 tree saved_class_ptr
= NULL_TREE
;
10616 tree saved_class_ref
= NULL_TREE
;
10617 int errs
= errorcount
+ sorrycount
;
10619 /* This can happen in invalid code. */
10620 if (TREE_CODE (arg
) == DEFAULT_ARG
)
10623 /* This default argument came from a template. Instantiate the
10624 default argument here, not in tsubst. In the case of
10633 we must be careful to do name lookup in the scope of S<T>,
10634 rather than in the current class. */
10635 push_access_scope (fn
);
10636 /* The "this" pointer is not valid in a default argument. */
10639 saved_class_ptr
= current_class_ptr
;
10640 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
10641 saved_class_ref
= current_class_ref
;
10642 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
10645 push_deferring_access_checks(dk_no_deferred
);
10646 /* The default argument expression may cause implicitly defined
10647 member functions to be synthesized, which will result in garbage
10648 collection. We must treat this situation as if we were within
10649 the body of function so as to avoid collecting live data on the
10652 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
10653 complain
, NULL_TREE
,
10654 /*integral_constant_expression_p=*/false);
10656 pop_deferring_access_checks();
10658 /* Restore the "this" pointer. */
10661 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
10662 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
10665 if (errorcount
+sorrycount
> errs
10666 && (complain
& tf_warning_or_error
))
10667 inform (input_location
,
10668 " when instantiating default argument for call to %D", fn
);
10670 /* Make sure the default argument is reasonable. */
10671 arg
= check_default_argument (type
, arg
, complain
);
10673 pop_access_scope (fn
);
10678 /* Substitute into all the default arguments for FN. */
10681 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
10686 tmpl_args
= DECL_TI_ARGS (fn
);
10688 /* If this function is not yet instantiated, we certainly don't need
10689 its default arguments. */
10690 if (uses_template_parms (tmpl_args
))
10692 /* Don't do this again for clones. */
10693 if (DECL_CLONED_FUNCTION_P (fn
))
10696 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
10698 arg
= TREE_CHAIN (arg
))
10699 if (TREE_PURPOSE (arg
))
10700 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
10702 TREE_PURPOSE (arg
),
10706 /* Substitute the ARGS into the T, which is a _DECL. Return the
10707 result of the substitution. Issue error and warning messages under
10708 control of COMPLAIN. */
10711 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
10713 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10714 location_t saved_loc
;
10715 tree r
= NULL_TREE
;
10717 hashval_t hash
= 0;
10719 /* Set the filename and linenumber to improve error-reporting. */
10720 saved_loc
= input_location
;
10721 input_location
= DECL_SOURCE_LOCATION (t
);
10723 switch (TREE_CODE (t
))
10725 case TEMPLATE_DECL
:
10727 /* We can get here when processing a member function template,
10728 member class template, or template template parameter. */
10729 tree decl
= DECL_TEMPLATE_RESULT (t
);
10734 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10736 /* Template template parameter is treated here. */
10737 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10738 if (new_type
== error_mark_node
)
10739 r
= error_mark_node
;
10740 /* If we get a real template back, return it. This can happen in
10741 the context of most_specialized_partial_spec. */
10742 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
10745 /* The new TEMPLATE_DECL was built in
10746 reduce_template_parm_level. */
10747 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
10751 /* We might already have an instance of this template.
10752 The ARGS are for the surrounding class type, so the
10753 full args contain the tsubst'd args for the context,
10754 plus the innermost args from the template decl. */
10755 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
10756 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
10757 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
10758 /* Because this is a template, the arguments will still be
10759 dependent, even after substitution. If
10760 PROCESSING_TEMPLATE_DECL is not set, the dependency
10761 predicates will short-circuit. */
10762 ++processing_template_decl
;
10763 full_args
= tsubst_template_args (tmpl_args
, args
,
10764 complain
, in_decl
);
10765 --processing_template_decl
;
10766 if (full_args
== error_mark_node
)
10767 RETURN (error_mark_node
);
10769 /* If this is a default template template argument,
10770 tsubst might not have changed anything. */
10771 if (full_args
== tmpl_args
)
10774 hash
= hash_tmpl_and_args (t
, full_args
);
10775 spec
= retrieve_specialization (t
, full_args
, hash
);
10776 if (spec
!= NULL_TREE
)
10782 /* Make a new template decl. It will be similar to the
10783 original, but will record the current template arguments.
10784 We also create a new function declaration, which is just
10785 like the old one, but points to this new template, rather
10786 than the old one. */
10788 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
10789 DECL_CHAIN (r
) = NULL_TREE
;
10791 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10793 if (TREE_CODE (decl
) == TYPE_DECL
10794 && !TYPE_DECL_ALIAS_P (decl
))
10797 ++processing_template_decl
;
10798 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10799 --processing_template_decl
;
10800 if (new_type
== error_mark_node
)
10801 RETURN (error_mark_node
);
10803 TREE_TYPE (r
) = new_type
;
10804 /* For a partial specialization, we need to keep pointing to
10805 the primary template. */
10806 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
10807 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
10808 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
10809 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
10810 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
10815 ++processing_template_decl
;
10816 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
10817 --processing_template_decl
;
10818 if (new_decl
== error_mark_node
)
10819 RETURN (error_mark_node
);
10821 DECL_TEMPLATE_RESULT (r
) = new_decl
;
10822 DECL_TI_TEMPLATE (new_decl
) = r
;
10823 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
10824 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
10825 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
10828 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10829 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
10830 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
10832 /* The template parameters for this new template are all the
10833 template parameters for the old template, except the
10834 outermost level of parameters. */
10835 DECL_TEMPLATE_PARMS (r
)
10836 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10839 if (PRIMARY_TEMPLATE_P (t
))
10840 DECL_PRIMARY_TEMPLATE (r
) = r
;
10842 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
))
10843 /* Record this non-type partial instantiation. */
10844 register_specialization (r
, t
,
10845 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
10850 case FUNCTION_DECL
:
10853 tree argvec
= NULL_TREE
;
10861 /* Nobody should be tsubst'ing into non-template functions. */
10862 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
10864 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
10869 /* If T is not dependent, just return it. We have to
10870 increment PROCESSING_TEMPLATE_DECL because
10871 value_dependent_expression_p assumes that nothing is
10872 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10873 ++processing_template_decl
;
10874 dependent_p
= value_dependent_expression_p (t
);
10875 --processing_template_decl
;
10879 /* Calculate the most general template of which R is a
10880 specialization, and the complete set of arguments used to
10882 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
10883 argvec
= tsubst_template_args (DECL_TI_ARGS
10884 (DECL_TEMPLATE_RESULT
10885 (DECL_TI_TEMPLATE (t
))),
10886 args
, complain
, in_decl
);
10887 if (argvec
== error_mark_node
)
10888 RETURN (error_mark_node
);
10890 /* Check to see if we already have this specialization. */
10891 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10892 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10900 /* We can see more levels of arguments than parameters if
10901 there was a specialization of a member template, like
10904 template <class T> struct S { template <class U> void f(); }
10905 template <> template <class U> void S<int>::f(U);
10907 Here, we'll be substituting into the specialization,
10908 because that's where we can find the code we actually
10909 want to generate, but we'll have enough arguments for
10910 the most general template.
10912 We also deal with the peculiar case:
10914 template <class T> struct S {
10915 template <class U> friend void f();
10917 template <class U> void f() {}
10919 template void f<double>();
10921 Here, the ARGS for the instantiation of will be {int,
10922 double}. But, we only need as many ARGS as there are
10923 levels of template parameters in CODE_PATTERN. We are
10924 careful not to get fooled into reducing the ARGS in
10927 template <class T> struct S { template <class U> void f(U); }
10928 template <class T> template <> void S<T>::f(int) {}
10930 which we can spot because the pattern will be a
10931 specialization in this case. */
10932 args_depth
= TMPL_ARGS_DEPTH (args
);
10934 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
10935 if (args_depth
> parms_depth
10936 && !DECL_TEMPLATE_SPECIALIZATION (t
))
10937 args
= get_innermost_template_args (args
, parms_depth
);
10941 /* This special case arises when we have something like this:
10943 template <class T> struct S {
10944 friend void f<int>(int, double);
10947 Here, the DECL_TI_TEMPLATE for the friend declaration
10948 will be an IDENTIFIER_NODE. We are being called from
10949 tsubst_friend_function, and we want only to create a
10950 new decl (R) with appropriate types so that we can call
10951 determine_specialization. */
10952 gen_tmpl
= NULL_TREE
;
10955 if (DECL_CLASS_SCOPE_P (t
))
10957 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
10961 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
10962 complain
, t
, /*entering_scope=*/1);
10967 ctx
= DECL_CONTEXT (t
);
10969 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10970 if (type
== error_mark_node
)
10971 RETURN (error_mark_node
);
10973 /* If we hit excessive deduction depth, the type is bogus even if
10974 it isn't error_mark_node, so don't build a decl. */
10975 if (excessive_deduction_depth
)
10976 RETURN (error_mark_node
);
10978 /* We do NOT check for matching decls pushed separately at this
10979 point, as they may not represent instantiations of this
10980 template, and in any case are considered separate under the
10983 DECL_USE_TEMPLATE (r
) = 0;
10984 TREE_TYPE (r
) = type
;
10985 /* Clear out the mangled name and RTL for the instantiation. */
10986 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10987 SET_DECL_RTL (r
, NULL
);
10988 /* Leave DECL_INITIAL set on deleted instantiations. */
10989 if (!DECL_DELETED_FN (r
))
10990 DECL_INITIAL (r
) = NULL_TREE
;
10991 DECL_CONTEXT (r
) = ctx
;
10993 /* OpenMP UDRs have the only argument a reference to the declared
10994 type. We want to diagnose if the declared type is a reference,
10995 which is invalid, but as references to references are usually
10996 quietly merged, diagnose it here. */
10997 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
11000 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
11001 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
11002 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
11003 error_at (DECL_SOURCE_LOCATION (t
),
11004 "reference type %qT in "
11005 "%<#pragma omp declare reduction%>", argtype
);
11006 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
11007 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
11011 if (member
&& DECL_CONV_FN_P (r
))
11012 /* Type-conversion operator. Reconstruct the name, in
11013 case it's the name of one of the template's parameters. */
11014 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
11016 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
11018 DECL_RESULT (r
) = NULL_TREE
;
11020 TREE_STATIC (r
) = 0;
11021 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
11022 DECL_EXTERNAL (r
) = 1;
11023 /* If this is an instantiation of a function with internal
11024 linkage, we already know what object file linkage will be
11025 assigned to the instantiation. */
11026 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
11027 DECL_DEFER_OUTPUT (r
) = 0;
11028 DECL_CHAIN (r
) = NULL_TREE
;
11029 DECL_PENDING_INLINE_INFO (r
) = 0;
11030 DECL_PENDING_INLINE_P (r
) = 0;
11031 DECL_SAVED_TREE (r
) = NULL_TREE
;
11032 DECL_STRUCT_FUNCTION (r
) = NULL
;
11034 /* We'll re-clone as appropriate in instantiate_template. */
11035 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
11037 /* If we aren't complaining now, return on error before we register
11038 the specialization so that we'll complain eventually. */
11039 if ((complain
& tf_error
) == 0
11040 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
11041 && !grok_op_properties (r
, /*complain=*/false))
11042 RETURN (error_mark_node
);
11044 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
11045 this in the special friend case mentioned above where
11046 GEN_TMPL is NULL. */
11049 DECL_TEMPLATE_INFO (r
)
11050 = build_template_info (gen_tmpl
, argvec
);
11051 SET_DECL_IMPLICIT_INSTANTIATION (r
);
11054 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
11056 /* We instantiated this while substituting into
11057 the type earlier (template/friend54.C). */
11060 /* We're not supposed to instantiate default arguments
11061 until they are called, for a template. But, for a
11064 template <class T> void f ()
11065 { extern void g(int i = T()); }
11067 we should do the substitution when the template is
11068 instantiated. We handle the member function case in
11069 instantiate_class_template since the default arguments
11070 might refer to other members of the class. */
11072 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
11073 && !uses_template_parms (argvec
))
11074 tsubst_default_arguments (r
, complain
);
11077 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
11079 /* Copy the list of befriending classes. */
11080 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
11082 friends
= &TREE_CHAIN (*friends
))
11084 *friends
= copy_node (*friends
);
11085 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
11090 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
11092 maybe_retrofit_in_chrg (r
);
11093 if (DECL_CONSTRUCTOR_P (r
))
11094 grok_ctor_properties (ctx
, r
);
11095 if (DECL_INHERITED_CTOR_BASE (r
))
11096 deduce_inheriting_ctor (r
);
11097 /* If this is an instantiation of a member template, clone it.
11098 If it isn't, that'll be handled by
11099 clone_constructors_and_destructors. */
11100 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
11101 clone_function_decl (r
, /*update_method_vec_p=*/0);
11103 else if ((complain
& tf_error
) != 0
11104 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
11105 && !grok_op_properties (r
, /*complain=*/true))
11106 RETURN (error_mark_node
);
11108 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
11109 SET_DECL_FRIEND_CONTEXT (r
,
11110 tsubst (DECL_FRIEND_CONTEXT (t
),
11111 args
, complain
, in_decl
));
11113 /* Possibly limit visibility based on template args. */
11114 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
11115 if (DECL_VISIBILITY_SPECIFIED (t
))
11117 DECL_VISIBILITY_SPECIFIED (r
) = 0;
11118 DECL_ATTRIBUTES (r
)
11119 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
11121 determine_visibility (r
);
11122 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
11123 && !processing_template_decl
)
11124 defaulted_late_check (r
);
11126 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
11127 args
, complain
, in_decl
);
11133 tree type
= NULL_TREE
;
11135 tree expanded_types
= NULL_TREE
;
11136 tree prev_r
= NULL_TREE
;
11137 tree first_r
= NULL_TREE
;
11139 if (DECL_PACK_P (t
))
11141 /* If there is a local specialization that isn't a
11142 parameter pack, it means that we're doing a "simple"
11143 substitution from inside tsubst_pack_expansion. Just
11144 return the local specialization (which will be a single
11146 tree spec
= retrieve_local_specialization (t
);
11148 && TREE_CODE (spec
) == PARM_DECL
11149 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
11152 /* Expand the TYPE_PACK_EXPANSION that provides the types for
11153 the parameters in this function parameter pack. */
11154 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
11155 complain
, in_decl
);
11156 if (TREE_CODE (expanded_types
) == TREE_VEC
)
11158 len
= TREE_VEC_LENGTH (expanded_types
);
11160 /* Zero-length parameter packs are boring. Just substitute
11163 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
11168 /* All we did was update the type. Make a note of that. */
11169 type
= expanded_types
;
11170 expanded_types
= NULL_TREE
;
11174 /* Loop through all of the parameters we'll build. When T is
11175 a function parameter pack, LEN is the number of expanded
11176 types in EXPANDED_TYPES; otherwise, LEN is 1. */
11178 for (i
= 0; i
< len
; ++i
)
11182 if (DECL_TEMPLATE_PARM_P (t
))
11183 SET_DECL_TEMPLATE_PARM_P (r
);
11185 if (expanded_types
)
11186 /* We're on the Ith parameter of the function parameter
11189 /* Get the Ith type. */
11190 type
= TREE_VEC_ELT (expanded_types
, i
);
11192 /* Rename the parameter to include the index. */
11194 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
11197 /* We're dealing with a normal parameter. */
11198 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11200 type
= type_decays_to (type
);
11201 TREE_TYPE (r
) = type
;
11202 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
11204 if (DECL_INITIAL (r
))
11206 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
11207 DECL_INITIAL (r
) = TREE_TYPE (r
);
11209 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
11210 complain
, in_decl
);
11213 DECL_CONTEXT (r
) = NULL_TREE
;
11215 if (!DECL_TEMPLATE_PARM_P (r
))
11216 DECL_ARG_TYPE (r
) = type_passed_as (type
);
11218 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
11219 args
, complain
, in_decl
);
11221 /* Keep track of the first new parameter we
11222 generate. That's what will be returned to the
11227 /* Build a proper chain of parameters when substituting
11228 into a function parameter pack. */
11230 DECL_CHAIN (prev_r
) = r
;
11233 /* If cp_unevaluated_operand is set, we're just looking for a
11234 single dummy parameter, so don't keep going. */
11235 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
11236 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
11237 complain
, DECL_CHAIN (t
));
11239 /* FIRST_R contains the start of the chain we've built. */
11246 tree type
= NULL_TREE
;
11247 tree vec
= NULL_TREE
;
11248 tree expanded_types
= NULL_TREE
;
11251 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
11253 /* This field is a lambda capture pack. Return a TREE_VEC of
11254 the expanded fields to instantiate_class_template_1 and
11255 store them in the specializations hash table as a
11256 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
11257 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
11258 complain
, in_decl
);
11259 if (TREE_CODE (expanded_types
) == TREE_VEC
)
11261 len
= TREE_VEC_LENGTH (expanded_types
);
11262 vec
= make_tree_vec (len
);
11266 /* All we did was update the type. Make a note of that. */
11267 type
= expanded_types
;
11268 expanded_types
= NULL_TREE
;
11272 for (int i
= 0; i
< len
; ++i
)
11275 if (expanded_types
)
11277 type
= TREE_VEC_ELT (expanded_types
, i
);
11279 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
11282 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11284 if (type
== error_mark_node
)
11285 RETURN (error_mark_node
);
11286 TREE_TYPE (r
) = type
;
11287 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
11289 if (DECL_C_BIT_FIELD (r
))
11290 /* For bit-fields, DECL_INITIAL gives the number of bits. For
11291 non-bit-fields DECL_INITIAL is a non-static data member
11292 initializer, which gets deferred instantiation. */
11294 = tsubst_expr (DECL_INITIAL (t
), args
,
11296 /*integral_constant_expression_p=*/true);
11297 else if (DECL_INITIAL (t
))
11299 /* Set up DECL_TEMPLATE_INFO so that we can get at the
11300 NSDMI in perform_member_init. Still set DECL_INITIAL
11301 so that we know there is one. */
11302 DECL_INITIAL (r
) = void_node
;
11303 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
11304 retrofit_lang_decl (r
);
11305 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
11307 /* We don't have to set DECL_CONTEXT here; it is set by
11308 finish_member_declaration. */
11309 DECL_CHAIN (r
) = NULL_TREE
;
11311 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
11312 args
, complain
, in_decl
);
11315 TREE_VEC_ELT (vec
, i
) = r
;
11321 tree pack
= make_node (NONTYPE_ARGUMENT_PACK
);
11322 tree tpack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
11323 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
11324 SET_ARGUMENT_PACK_ARGS (tpack
, expanded_types
);
11325 TREE_TYPE (pack
) = tpack
;
11326 register_specialization (pack
, t
, args
, false, 0);
11332 /* We reach here only for member using decls. We also need to check
11333 uses_template_parms because DECL_DEPENDENT_P is not set for a
11334 using-declaration that designates a member of the current
11335 instantiation (c++/53549). */
11336 if (DECL_DEPENDENT_P (t
)
11337 || uses_template_parms (USING_DECL_SCOPE (t
)))
11339 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
11340 complain
, in_decl
);
11341 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
11342 r
= do_class_using_decl (inst_scope
, name
);
11344 r
= error_mark_node
;
11347 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
11348 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
11354 DECL_CHAIN (r
) = NULL_TREE
;
11361 tree argvec
= NULL_TREE
;
11362 tree gen_tmpl
= NULL_TREE
;
11364 tree tmpl
= NULL_TREE
;
11366 tree type
= NULL_TREE
;
11369 if (TREE_TYPE (t
) == error_mark_node
)
11370 RETURN (error_mark_node
);
11372 if (TREE_CODE (t
) == TYPE_DECL
11373 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
11375 /* If this is the canonical decl, we don't have to
11376 mess with instantiations, and often we can't (for
11377 typename, template type parms and such). Note that
11378 TYPE_NAME is not correct for the above test if
11379 we've copied the type for a typedef. */
11380 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11381 if (type
== error_mark_node
)
11382 RETURN (error_mark_node
);
11383 r
= TYPE_NAME (type
);
11387 /* Check to see if we already have the specialization we
11390 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
11392 /* T is a static data member or namespace-scope entity.
11393 We have to substitute into namespace-scope variables
11394 (not just variable templates) because of cases like:
11396 template <class T> void f() { extern T t; }
11398 where the entity referenced is not known until
11399 instantiation time. */
11401 ctx
= DECL_CONTEXT (t
);
11402 if (DECL_CLASS_SCOPE_P (t
))
11404 ctx
= tsubst_aggr_type (ctx
, args
,
11406 in_decl
, /*entering_scope=*/1);
11407 /* If CTX is unchanged, then T is in fact the
11408 specialization we want. That situation occurs when
11409 referencing a static data member within in its own
11410 class. We can use pointer equality, rather than
11411 same_type_p, because DECL_CONTEXT is always
11413 if (ctx
== DECL_CONTEXT (t
)
11414 /* ... unless T is a member template; in which
11415 case our caller can be willing to create a
11416 specialization of that template represented
11418 && !(DECL_TI_TEMPLATE (t
)
11419 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
11425 tmpl
= DECL_TI_TEMPLATE (t
);
11426 gen_tmpl
= most_general_template (tmpl
);
11427 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
11428 if (argvec
!= error_mark_node
)
11429 argvec
= (coerce_innermost_template_parms
11430 (DECL_TEMPLATE_PARMS (gen_tmpl
),
11431 argvec
, t
, complain
,
11432 /*all*/true, /*defarg*/true));
11433 if (argvec
== error_mark_node
)
11434 RETURN (error_mark_node
);
11435 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
11436 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
11441 /* A local variable. */
11443 /* Subsequent calls to pushdecl will fill this in. */
11445 spec
= retrieve_local_specialization (t
);
11447 /* If we already have the specialization we need, there is
11448 nothing more to do. */
11455 /* Create a new node for the specialization we need. */
11457 if (type
== NULL_TREE
)
11459 if (is_typedef_decl (t
))
11460 type
= DECL_ORIGINAL_TYPE (t
);
11462 type
= TREE_TYPE (t
);
11464 && VAR_HAD_UNKNOWN_BOUND (t
)
11465 && type
!= error_mark_node
)
11466 type
= strip_array_domain (type
);
11467 type
= tsubst (type
, args
, complain
, in_decl
);
11471 /* Even if the original location is out of scope, the
11472 newly substituted one is not. */
11473 DECL_DEAD_FOR_LOCAL (r
) = 0;
11474 DECL_INITIALIZED_P (r
) = 0;
11475 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
11476 if (type
== error_mark_node
)
11477 RETURN (error_mark_node
);
11478 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11480 /* It may seem that this case cannot occur, since:
11485 declares a function, not a variable. However:
11488 template <typename T> void g() { T t; }
11489 template void g<f>();
11491 is an attempt to declare a variable with function
11493 error ("variable %qD has function type",
11494 /* R is not yet sufficiently initialized, so we
11495 just use its name. */
11497 RETURN (error_mark_node
);
11499 type
= complete_type (type
);
11500 /* Wait until cp_finish_decl to set this again, to handle
11501 circular dependency (template/instantiate6.C). */
11502 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
11503 type
= check_var_type (DECL_NAME (r
), type
);
11505 if (DECL_HAS_VALUE_EXPR_P (t
))
11507 tree ve
= DECL_VALUE_EXPR (t
);
11508 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
11509 /*constant_expression_p=*/false);
11510 if (REFERENCE_REF_P (ve
))
11512 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
11513 ve
= TREE_OPERAND (ve
, 0);
11515 SET_DECL_VALUE_EXPR (r
, ve
);
11517 if (CP_DECL_THREAD_LOCAL_P (r
)
11518 && !processing_template_decl
)
11519 set_decl_tls_model (r
, decl_default_tls_model (r
));
11521 else if (DECL_SELF_REFERENCE_P (t
))
11522 SET_DECL_SELF_REFERENCE_P (r
);
11523 TREE_TYPE (r
) = type
;
11524 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
11525 DECL_CONTEXT (r
) = ctx
;
11526 /* Clear out the mangled name and RTL for the instantiation. */
11527 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
11528 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
11529 SET_DECL_RTL (r
, NULL
);
11530 /* The initializer must not be expanded until it is required;
11531 see [temp.inst]. */
11532 DECL_INITIAL (r
) = NULL_TREE
;
11533 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
11534 SET_DECL_RTL (r
, NULL
);
11535 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
11538 /* Possibly limit visibility based on template args. */
11539 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
11540 if (DECL_VISIBILITY_SPECIFIED (t
))
11542 DECL_VISIBILITY_SPECIFIED (r
) = 0;
11543 DECL_ATTRIBUTES (r
)
11544 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
11546 determine_visibility (r
);
11551 /* A static data member declaration is always marked
11552 external when it is declared in-class, even if an
11553 initializer is present. We mimic the non-template
11554 processing here. */
11555 DECL_EXTERNAL (r
) = 1;
11556 if (DECL_NAMESPACE_SCOPE_P (t
))
11557 DECL_NOT_REALLY_EXTERN (r
) = 1;
11559 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
11560 SET_DECL_IMPLICIT_INSTANTIATION (r
);
11561 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
11563 else if (!cp_unevaluated_operand
)
11564 register_local_specialization (r
, t
);
11566 DECL_CHAIN (r
) = NULL_TREE
;
11568 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
11570 args
, complain
, in_decl
);
11572 /* Preserve a typedef that names a type. */
11573 if (is_typedef_decl (r
))
11575 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
11576 set_underlying_type (r
);
11577 if (TYPE_DECL_ALIAS_P (r
) && type
!= error_mark_node
)
11578 /* An alias template specialization can be dependent
11579 even if its underlying type is not. */
11580 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
11583 layout_decl (r
, 0);
11588 gcc_unreachable ();
11593 /* Restore the file and line information. */
11594 input_location
= saved_loc
;
11599 /* Substitute into the ARG_TYPES of a function type.
11600 If END is a TREE_CHAIN, leave it and any following types
11604 tsubst_arg_types (tree arg_types
,
11607 tsubst_flags_t complain
,
11610 tree remaining_arg_types
;
11611 tree type
= NULL_TREE
;
11613 tree expanded_args
= NULL_TREE
;
11616 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
11619 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
11620 args
, end
, complain
, in_decl
);
11621 if (remaining_arg_types
== error_mark_node
)
11622 return error_mark_node
;
11624 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
11626 /* For a pack expansion, perform substitution on the
11627 entire expression. Later on, we'll handle the arguments
11629 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
11630 args
, complain
, in_decl
);
11632 if (TREE_CODE (expanded_args
) == TREE_VEC
)
11633 /* So that we'll spin through the parameters, one by one. */
11634 i
= TREE_VEC_LENGTH (expanded_args
);
11637 /* We only partially substituted into the parameter
11638 pack. Our type is TYPE_PACK_EXPANSION. */
11639 type
= expanded_args
;
11640 expanded_args
= NULL_TREE
;
11648 type
= TREE_VEC_ELT (expanded_args
, i
);
11650 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
11652 if (type
== error_mark_node
)
11653 return error_mark_node
;
11654 if (VOID_TYPE_P (type
))
11656 if (complain
& tf_error
)
11658 error ("invalid parameter type %qT", type
);
11660 error ("in declaration %q+D", in_decl
);
11662 return error_mark_node
;
11665 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
11666 return error_mark_node
;
11668 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11669 top-level qualifiers as required. */
11670 type
= cv_unqualified (type_decays_to (type
));
11672 /* We do not substitute into default arguments here. The standard
11673 mandates that they be instantiated only when needed, which is
11674 done in build_over_call. */
11675 default_arg
= TREE_PURPOSE (arg_types
);
11677 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
11679 /* We've instantiated a template before its default arguments
11680 have been parsed. This can happen for a nested template
11681 class, and is not an error unless we require the default
11682 argument in a call of this function. */
11683 remaining_arg_types
=
11684 tree_cons (default_arg
, type
, remaining_arg_types
);
11685 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
11688 remaining_arg_types
=
11689 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
11692 return remaining_arg_types
;
11695 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11696 *not* handle the exception-specification for FNTYPE, because the
11697 initial substitution of explicitly provided template parameters
11698 during argument deduction forbids substitution into the
11699 exception-specification:
11703 All references in the function type of the function template to the
11704 corresponding template parameters are replaced by the specified tem-
11705 plate argument values. If a substitution in a template parameter or
11706 in the function type of the function template results in an invalid
11707 type, type deduction fails. [Note: The equivalent substitution in
11708 exception specifications is done only when the function is instanti-
11709 ated, at which point a program is ill-formed if the substitution
11710 results in an invalid type.] */
11713 tsubst_function_type (tree t
,
11715 tsubst_flags_t complain
,
11719 tree arg_types
= NULL_TREE
;
11722 /* The TYPE_CONTEXT is not used for function/method types. */
11723 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
11725 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
11727 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
11729 if (late_return_type_p
)
11731 /* Substitute the argument types. */
11732 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
11733 complain
, in_decl
);
11734 if (arg_types
== error_mark_node
)
11735 return error_mark_node
;
11737 tree save_ccp
= current_class_ptr
;
11738 tree save_ccr
= current_class_ref
;
11739 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
11740 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
11741 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
11744 /* DR 1207: 'this' is in scope in the trailing return type. */
11745 inject_this_parameter (this_type
, cp_type_quals (this_type
));
11748 /* Substitute the return type. */
11749 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11753 current_class_ptr
= save_ccp
;
11754 current_class_ref
= save_ccr
;
11758 /* Substitute the return type. */
11759 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11761 if (return_type
== error_mark_node
)
11762 return error_mark_node
;
11763 /* DR 486 clarifies that creation of a function type with an
11764 invalid return type is a deduction failure. */
11765 if (TREE_CODE (return_type
) == ARRAY_TYPE
11766 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
11768 if (complain
& tf_error
)
11770 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
11771 error ("function returning an array");
11773 error ("function returning a function");
11775 return error_mark_node
;
11778 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
11779 return error_mark_node
;
11781 if (!late_return_type_p
)
11783 /* Substitute the argument types. */
11784 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
11785 complain
, in_decl
);
11786 if (arg_types
== error_mark_node
)
11787 return error_mark_node
;
11790 /* Construct a new type node and return it. */
11791 if (TREE_CODE (t
) == FUNCTION_TYPE
)
11793 fntype
= build_function_type (return_type
, arg_types
);
11794 fntype
= apply_memfn_quals (fntype
,
11795 type_memfn_quals (t
),
11796 type_memfn_rqual (t
));
11800 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
11801 /* Don't pick up extra function qualifiers from the basetype. */
11802 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
11803 if (! MAYBE_CLASS_TYPE_P (r
))
11807 Type deduction may fail for any of the following
11810 -- Attempting to create "pointer to member of T" when T
11811 is not a class type. */
11812 if (complain
& tf_error
)
11813 error ("creating pointer to member function of non-class type %qT",
11815 return error_mark_node
;
11818 fntype
= build_method_type_directly (r
, return_type
,
11819 TREE_CHAIN (arg_types
));
11820 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
11822 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
11824 if (late_return_type_p
)
11825 TYPE_HAS_LATE_RETURN_TYPE (fntype
) = 1;
11830 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11831 ARGS into that specification, and return the substituted
11832 specification. If there is no specification, return NULL_TREE. */
11835 tsubst_exception_specification (tree fntype
,
11837 tsubst_flags_t complain
,
11844 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
11845 new_specs
= NULL_TREE
;
11846 if (specs
&& TREE_PURPOSE (specs
))
11848 /* A noexcept-specifier. */
11849 tree expr
= TREE_PURPOSE (specs
);
11850 if (TREE_CODE (expr
) == INTEGER_CST
)
11854 /* Defer instantiation of noexcept-specifiers to avoid
11855 excessive instantiations (c++/49107). */
11856 new_specs
= make_node (DEFERRED_NOEXCEPT
);
11857 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
11859 /* We already partially instantiated this member template,
11860 so combine the new args with the old. */
11861 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
11862 = DEFERRED_NOEXCEPT_PATTERN (expr
);
11863 DEFERRED_NOEXCEPT_ARGS (new_specs
)
11864 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
11868 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
11869 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
11873 new_specs
= tsubst_copy_and_build
11874 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
11875 /*integral_constant_expression_p=*/true);
11876 new_specs
= build_noexcept_spec (new_specs
, complain
);
11880 if (! TREE_VALUE (specs
))
11887 tree expanded_specs
= NULL_TREE
;
11889 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
11891 /* Expand the pack expansion type. */
11892 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
11896 if (expanded_specs
== error_mark_node
)
11897 return error_mark_node
;
11898 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
11899 len
= TREE_VEC_LENGTH (expanded_specs
);
11902 /* We're substituting into a member template, so
11903 we got a TYPE_PACK_EXPANSION back. Add that
11904 expansion and move on. */
11905 gcc_assert (TREE_CODE (expanded_specs
)
11906 == TYPE_PACK_EXPANSION
);
11907 new_specs
= add_exception_specifier (new_specs
,
11910 specs
= TREE_CHAIN (specs
);
11915 for (i
= 0; i
< len
; ++i
)
11917 if (expanded_specs
)
11918 spec
= TREE_VEC_ELT (expanded_specs
, i
);
11920 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
11921 if (spec
== error_mark_node
)
11923 new_specs
= add_exception_specifier (new_specs
, spec
,
11927 specs
= TREE_CHAIN (specs
);
11933 /* Take the tree structure T and replace template parameters used
11934 therein with the argument vector ARGS. IN_DECL is an associated
11935 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11936 Issue error and warning messages under control of COMPLAIN. Note
11937 that we must be relatively non-tolerant of extensions here, in
11938 order to preserve conformance; if we allow substitutions that
11939 should not be allowed, we may allow argument deductions that should
11940 not succeed, and therefore report ambiguous overload situations
11941 where there are none. In theory, we could allow the substitution,
11942 but indicate that it should have failed, and allow our caller to
11943 make sure that the right thing happens, but we don't try to do this
11946 This function is used for dealing with types, decls and the like;
11947 for expressions, use tsubst_expr or tsubst_copy. */
11950 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11952 enum tree_code code
;
11953 tree type
, r
= NULL_TREE
;
11955 if (t
== NULL_TREE
|| t
== error_mark_node
11956 || t
== integer_type_node
11957 || t
== void_type_node
11958 || t
== char_type_node
11959 || t
== unknown_type_node
11960 || TREE_CODE (t
) == NAMESPACE_DECL
11961 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
11965 return tsubst_decl (t
, args
, complain
);
11967 if (args
== NULL_TREE
)
11970 code
= TREE_CODE (t
);
11972 if (code
== IDENTIFIER_NODE
)
11973 type
= IDENTIFIER_TYPE_VALUE (t
);
11975 type
= TREE_TYPE (t
);
11977 gcc_assert (type
!= unknown_type_node
);
11979 /* Reuse typedefs. We need to do this to handle dependent attributes,
11980 such as attribute aligned. */
11982 && typedef_variant_p (t
))
11984 tree decl
= TYPE_NAME (t
);
11986 if (alias_template_specialization_p (t
))
11988 /* DECL represents an alias template and we want to
11990 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11991 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11992 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
11994 else if (DECL_CLASS_SCOPE_P (decl
)
11995 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11996 && uses_template_parms (DECL_CONTEXT (decl
)))
11998 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11999 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
12000 r
= retrieve_specialization (tmpl
, gen_args
, 0);
12002 else if (DECL_FUNCTION_SCOPE_P (decl
)
12003 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
12004 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
12005 r
= retrieve_local_specialization (decl
);
12007 /* The typedef is from a non-template context. */
12013 r
= cp_build_qualified_type_real
12014 (r
, cp_type_quals (t
) | cp_type_quals (r
),
12015 complain
| tf_ignore_bad_quals
);
12020 /* We don't have an instantiation yet, so drop the typedef. */
12021 int quals
= cp_type_quals (t
);
12022 t
= DECL_ORIGINAL_TYPE (decl
);
12023 t
= cp_build_qualified_type_real (t
, quals
,
12024 complain
| tf_ignore_bad_quals
);
12029 && code
!= TYPENAME_TYPE
12030 && code
!= TEMPLATE_TYPE_PARM
12031 && code
!= IDENTIFIER_NODE
12032 && code
!= FUNCTION_TYPE
12033 && code
!= METHOD_TYPE
)
12034 type
= tsubst (type
, args
, complain
, in_decl
);
12035 if (type
== error_mark_node
)
12036 return error_mark_node
;
12042 case ENUMERAL_TYPE
:
12043 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
12044 /*entering_scope=*/0);
12047 case IDENTIFIER_NODE
:
12058 if (t
== integer_type_node
)
12061 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
12062 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
12066 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
12068 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
12069 /*integral_constant_expression_p=*/false);
12071 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
12073 if (TREE_CODE (max
) == NOP_EXPR
12074 && TREE_SIDE_EFFECTS (omax
)
12075 && !TREE_TYPE (max
))
12076 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
12078 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
12079 with TREE_SIDE_EFFECTS that indicates this is not an integral
12080 constant expression. */
12081 if (processing_template_decl
12082 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
12084 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
12085 TREE_SIDE_EFFECTS (max
) = 1;
12088 return compute_array_index_type (NULL_TREE
, max
, complain
);
12091 case TEMPLATE_TYPE_PARM
:
12092 case TEMPLATE_TEMPLATE_PARM
:
12093 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12094 case TEMPLATE_PARM_INDEX
:
12099 tree arg
= NULL_TREE
;
12103 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
12104 template_parm_level_and_index (t
, &level
, &idx
);
12106 levels
= TMPL_ARGS_DEPTH (args
);
12107 if (level
<= levels
12108 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
12110 arg
= TMPL_ARG (args
, level
, idx
);
12112 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
12114 /* See through ARGUMENT_PACK_SELECT arguments. */
12115 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
12116 /* If the selected argument is an expansion E, that most
12117 likely means we were called from
12118 gen_elem_of_pack_expansion_instantiation during the
12119 substituting of pack an argument pack (which Ith
12120 element is a pack expansion, where I is
12121 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
12122 In this case, the Ith element resulting from this
12123 substituting is going to be a pack expansion, which
12124 pattern is the pattern of E. Let's return the
12126 gen_elem_of_pack_expansion_instantiation will
12127 build the resulting pack expansion from it. */
12128 if (PACK_EXPANSION_P (arg
))
12130 /* Make sure we aren't throwing away arg info. */
12131 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
12132 arg
= PACK_EXPANSION_PATTERN (arg
);
12137 if (arg
== error_mark_node
)
12138 return error_mark_node
;
12139 else if (arg
!= NULL_TREE
)
12141 if (ARGUMENT_PACK_P (arg
))
12142 /* If ARG is an argument pack, we don't actually want to
12143 perform a substitution here, because substitutions
12144 for argument packs are only done
12145 element-by-element. We can get to this point when
12146 substituting the type of a non-type template
12147 parameter pack, when that type actually contains
12148 template parameter packs from an outer template, e.g.,
12150 template<typename... Types> struct A {
12151 template<Types... Values> struct B { };
12155 if (code
== TEMPLATE_TYPE_PARM
)
12158 gcc_assert (TYPE_P (arg
));
12160 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
12162 return cp_build_qualified_type_real
12163 (arg
, quals
, complain
| tf_ignore_bad_quals
);
12165 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
12167 /* We are processing a type constructed from a
12168 template template parameter. */
12169 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
12170 args
, complain
, in_decl
);
12171 if (argvec
== error_mark_node
)
12172 return error_mark_node
;
12174 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
12175 || TREE_CODE (arg
) == TEMPLATE_DECL
12176 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
12178 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
12179 /* Consider this code:
12181 template <template <class> class Template>
12183 template <class Arg> using Bind = Template<Arg>;
12186 template <template <class> class Template, class Arg>
12187 using Instantiate = Template<Arg>; //#0
12189 template <template <class> class Template,
12192 Instantiate<Internal<Template>::template Bind,
12195 When #1 is parsed, the
12196 BOUND_TEMPLATE_TEMPLATE_PARM representing the
12197 parameter `Template' in #0 matches the
12198 UNBOUND_CLASS_TEMPLATE representing the argument
12199 `Internal<Template>::template Bind'; We then want
12200 to assemble the type `Bind<Argument>' that can't
12201 be fully created right now, because
12202 `Internal<Template>' not being complete, the Bind
12203 template cannot be looked up in that context. So
12204 we need to "store" `Bind<Argument>' for later
12205 when the context of Bind becomes complete. Let's
12206 store that in a TYPENAME_TYPE. */
12207 return make_typename_type (TYPE_CONTEXT (arg
),
12208 build_nt (TEMPLATE_ID_EXPR
,
12209 TYPE_IDENTIFIER (arg
),
12214 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
12215 are resolving nested-types in the signature of a
12216 member function templates. Otherwise ARG is a
12217 TEMPLATE_DECL and is the real template to be
12219 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
12220 arg
= TYPE_NAME (arg
);
12222 r
= lookup_template_class (arg
,
12224 DECL_CONTEXT (arg
),
12225 /*entering_scope=*/0,
12227 return cp_build_qualified_type_real
12228 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
12231 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
12232 return convert_from_reference (unshare_expr (arg
));
12236 /* This can happen during the attempted tsubst'ing in
12237 unify. This means that we don't yet have any information
12238 about the template parameter in question. */
12241 /* Early in template argument deduction substitution, we don't
12242 want to reduce the level of 'auto', or it will be confused
12243 with a normal template parm in subsequent deduction. */
12244 if (is_auto (t
) && (complain
& tf_partial
))
12247 /* If we get here, we must have been looking at a parm for a
12248 more deeply nested template. Make a new version of this
12249 template parameter, but with a lower level. */
12252 case TEMPLATE_TYPE_PARM
:
12253 case TEMPLATE_TEMPLATE_PARM
:
12254 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12255 if (cp_type_quals (t
))
12257 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
12258 r
= cp_build_qualified_type_real
12259 (r
, cp_type_quals (t
),
12260 complain
| (code
== TEMPLATE_TYPE_PARM
12261 ? tf_ignore_bad_quals
: 0));
12266 TEMPLATE_TYPE_PARM_INDEX (r
)
12267 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
12268 r
, levels
, args
, complain
);
12269 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
12270 TYPE_MAIN_VARIANT (r
) = r
;
12271 TYPE_POINTER_TO (r
) = NULL_TREE
;
12272 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
12274 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
12275 /* We have reduced the level of the template
12276 template parameter, but not the levels of its
12277 template parameters, so canonical_type_parameter
12278 will not be able to find the canonical template
12279 template parameter for this level. Thus, we
12280 require structural equality checking to compare
12281 TEMPLATE_TEMPLATE_PARMs. */
12282 SET_TYPE_STRUCTURAL_EQUALITY (r
);
12283 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
12284 SET_TYPE_STRUCTURAL_EQUALITY (r
);
12286 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
12288 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
12290 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
12291 complain
, in_decl
);
12292 if (argvec
== error_mark_node
)
12293 return error_mark_node
;
12295 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
12296 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
12301 case TEMPLATE_PARM_INDEX
:
12302 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
12306 gcc_unreachable ();
12314 tree purpose
, value
, chain
;
12316 if (t
== void_list_node
)
12319 purpose
= TREE_PURPOSE (t
);
12322 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
12323 if (purpose
== error_mark_node
)
12324 return error_mark_node
;
12326 value
= TREE_VALUE (t
);
12329 value
= tsubst (value
, args
, complain
, in_decl
);
12330 if (value
== error_mark_node
)
12331 return error_mark_node
;
12333 chain
= TREE_CHAIN (t
);
12334 if (chain
&& chain
!= void_type_node
)
12336 chain
= tsubst (chain
, args
, complain
, in_decl
);
12337 if (chain
== error_mark_node
)
12338 return error_mark_node
;
12340 if (purpose
== TREE_PURPOSE (t
)
12341 && value
== TREE_VALUE (t
)
12342 && chain
== TREE_CHAIN (t
))
12344 return hash_tree_cons (purpose
, value
, chain
);
12348 /* We should never be tsubsting a binfo. */
12349 gcc_unreachable ();
12352 /* A vector of template arguments. */
12353 gcc_assert (!type
);
12354 return tsubst_template_args (t
, args
, complain
, in_decl
);
12357 case REFERENCE_TYPE
:
12359 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
12364 Type deduction may fail for any of the following
12367 -- Attempting to create a pointer to reference type.
12368 -- Attempting to create a reference to a reference type or
12369 a reference to void.
12371 Core issue 106 says that creating a reference to a reference
12372 during instantiation is no longer a cause for failure. We
12373 only enforce this check in strict C++98 mode. */
12374 if ((TREE_CODE (type
) == REFERENCE_TYPE
12375 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
12376 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
12378 static location_t last_loc
;
12380 /* We keep track of the last time we issued this error
12381 message to avoid spewing a ton of messages during a
12382 single bad template instantiation. */
12383 if (complain
& tf_error
12384 && last_loc
!= input_location
)
12386 if (VOID_TYPE_P (type
))
12387 error ("forming reference to void");
12388 else if (code
== POINTER_TYPE
)
12389 error ("forming pointer to reference type %qT", type
);
12391 error ("forming reference to reference type %qT", type
);
12392 last_loc
= input_location
;
12395 return error_mark_node
;
12397 else if (TREE_CODE (type
) == FUNCTION_TYPE
12398 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
12399 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
12401 if (complain
& tf_error
)
12403 if (code
== POINTER_TYPE
)
12404 error ("forming pointer to qualified function type %qT",
12407 error ("forming reference to qualified function type %qT",
12410 return error_mark_node
;
12412 else if (code
== POINTER_TYPE
)
12414 r
= build_pointer_type (type
);
12415 if (TREE_CODE (type
) == METHOD_TYPE
)
12416 r
= build_ptrmemfunc_type (r
);
12418 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
12419 /* In C++0x, during template argument substitution, when there is an
12420 attempt to create a reference to a reference type, reference
12421 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
12423 "If a template-argument for a template-parameter T names a type
12424 that is a reference to a type A, an attempt to create the type
12425 'lvalue reference to cv T' creates the type 'lvalue reference to
12426 A,' while an attempt to create the type type rvalue reference to
12427 cv T' creates the type T"
12429 r
= cp_build_reference_type
12431 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
12433 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
12434 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
12436 if (r
!= error_mark_node
)
12437 /* Will this ever be needed for TYPE_..._TO values? */
12444 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
12445 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
12449 Type deduction may fail for any of the following
12452 -- Attempting to create "pointer to member of T" when T
12453 is not a class type. */
12454 if (complain
& tf_error
)
12455 error ("creating pointer to member of non-class type %qT", r
);
12456 return error_mark_node
;
12458 if (TREE_CODE (type
) == REFERENCE_TYPE
)
12460 if (complain
& tf_error
)
12461 error ("creating pointer to member reference type %qT", type
);
12462 return error_mark_node
;
12464 if (VOID_TYPE_P (type
))
12466 if (complain
& tf_error
)
12467 error ("creating pointer to member of type void");
12468 return error_mark_node
;
12470 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
12471 if (TREE_CODE (type
) == FUNCTION_TYPE
)
12473 /* The type of the implicit object parameter gets its
12474 cv-qualifiers from the FUNCTION_TYPE. */
12477 = build_memfn_type (type
, r
, type_memfn_quals (type
),
12478 type_memfn_rqual (type
));
12479 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
12480 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
12484 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
12488 case FUNCTION_TYPE
:
12493 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
12494 if (fntype
== error_mark_node
)
12495 return error_mark_node
;
12497 /* Substitute the exception specification. */
12498 specs
= tsubst_exception_specification (t
, args
, complain
,
12499 in_decl
, /*defer_ok*/true);
12500 if (specs
== error_mark_node
)
12501 return error_mark_node
;
12503 fntype
= build_exception_variant (fntype
, specs
);
12508 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
12509 if (domain
== error_mark_node
)
12510 return error_mark_node
;
12512 /* As an optimization, we avoid regenerating the array type if
12513 it will obviously be the same as T. */
12514 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
12517 /* These checks should match the ones in create_array_type_for_decl.
12521 The deduction may fail for any of the following reasons:
12523 -- Attempting to create an array with an element type that
12524 is void, a function type, or a reference type, or [DR337]
12525 an abstract class type. */
12526 if (VOID_TYPE_P (type
)
12527 || TREE_CODE (type
) == FUNCTION_TYPE
12528 || (TREE_CODE (type
) == ARRAY_TYPE
12529 && TYPE_DOMAIN (type
) == NULL_TREE
)
12530 || TREE_CODE (type
) == REFERENCE_TYPE
)
12532 if (complain
& tf_error
)
12533 error ("creating array of %qT", type
);
12534 return error_mark_node
;
12537 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
12538 return error_mark_node
;
12540 r
= build_cplus_array_type (type
, domain
);
12542 if (TYPE_USER_ALIGN (t
))
12544 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
12545 TYPE_USER_ALIGN (r
) = 1;
12551 case TYPENAME_TYPE
:
12553 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
12554 in_decl
, /*entering_scope=*/1);
12555 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
12556 complain
, in_decl
);
12558 if (ctx
== error_mark_node
|| f
== error_mark_node
)
12559 return error_mark_node
;
12561 if (!MAYBE_CLASS_TYPE_P (ctx
))
12563 if (complain
& tf_error
)
12564 error ("%qT is not a class, struct, or union type", ctx
);
12565 return error_mark_node
;
12567 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
12569 /* Normally, make_typename_type does not require that the CTX
12570 have complete type in order to allow things like:
12572 template <class T> struct S { typename S<T>::X Y; };
12574 But, such constructs have already been resolved by this
12575 point, so here CTX really should have complete type, unless
12576 it's a partial instantiation. */
12577 ctx
= complete_type (ctx
);
12578 if (!COMPLETE_TYPE_P (ctx
))
12580 if (complain
& tf_error
)
12581 cxx_incomplete_type_error (NULL_TREE
, ctx
);
12582 return error_mark_node
;
12586 f
= make_typename_type (ctx
, f
, typename_type
,
12587 complain
| tf_keep_type_decl
);
12588 if (f
== error_mark_node
)
12590 if (TREE_CODE (f
) == TYPE_DECL
)
12592 complain
|= tf_ignore_bad_quals
;
12596 if (TREE_CODE (f
) != TYPENAME_TYPE
)
12598 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
12600 if (complain
& tf_error
)
12601 error ("%qT resolves to %qT, which is not an enumeration type",
12604 return error_mark_node
;
12606 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
12608 if (complain
& tf_error
)
12609 error ("%qT resolves to %qT, which is is not a class type",
12612 return error_mark_node
;
12616 return cp_build_qualified_type_real
12617 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
12620 case UNBOUND_CLASS_TEMPLATE
:
12622 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
12623 in_decl
, /*entering_scope=*/1);
12624 tree name
= TYPE_IDENTIFIER (t
);
12625 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
12627 if (ctx
== error_mark_node
|| name
== error_mark_node
)
12628 return error_mark_node
;
12631 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
12632 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
12639 ++cp_unevaluated_operand
;
12640 ++c_inhibit_evaluation_warnings
;
12642 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
12644 /*integral_constant_expression_p=*/false);
12646 --cp_unevaluated_operand
;
12647 --c_inhibit_evaluation_warnings
;
12649 type
= finish_typeof (type
);
12650 return cp_build_qualified_type_real (type
,
12652 | cp_type_quals (type
),
12656 case DECLTYPE_TYPE
:
12660 ++cp_unevaluated_operand
;
12661 ++c_inhibit_evaluation_warnings
;
12663 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
12664 complain
|tf_decltype
, in_decl
,
12665 /*function_p*/false,
12666 /*integral_constant_expression*/false);
12668 --cp_unevaluated_operand
;
12669 --c_inhibit_evaluation_warnings
;
12671 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
12672 type
= lambda_capture_field_type (type
,
12673 DECLTYPE_FOR_INIT_CAPTURE (t
));
12674 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
12675 type
= lambda_proxy_type (type
);
12678 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
12679 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
12681 /* In a template ~id could be either a complement expression
12682 or an unqualified-id naming a destructor; if instantiating
12683 it produces an expression, it's not an id-expression or
12686 type
= finish_decltype_type (type
, id
, complain
);
12688 return cp_build_qualified_type_real (type
,
12690 | cp_type_quals (type
),
12691 complain
| tf_ignore_bad_quals
);
12694 case UNDERLYING_TYPE
:
12696 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
12697 complain
, in_decl
);
12698 return finish_underlying_type (type
);
12701 case TYPE_ARGUMENT_PACK
:
12702 case NONTYPE_ARGUMENT_PACK
:
12704 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
12706 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
12710 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
12712 /* For template nontype argument packs, also substitute into
12714 if (code
== NONTYPE_ARGUMENT_PACK
)
12715 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12734 /* We should use one of the expression tsubsts for these codes. */
12735 gcc_unreachable ();
12738 sorry ("use of %qs in template", get_tree_code_name (code
));
12739 return error_mark_node
;
12743 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12744 type of the expression on the left-hand side of the "." or "->"
12748 tsubst_baselink (tree baselink
, tree object_type
,
12749 tree args
, tsubst_flags_t complain
, tree in_decl
)
12752 tree qualifying_scope
;
12755 tree template_args
= 0;
12756 bool template_id_p
= false;
12757 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
12759 /* A baselink indicates a function from a base class. Both the
12760 BASELINK_ACCESS_BINFO and the base class referenced may
12761 indicate bases of the template class, rather than the
12762 instantiated class. In addition, lookups that were not
12763 ambiguous before may be ambiguous now. Therefore, we perform
12764 the lookup again. */
12765 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
12766 qualifying_scope
= tsubst (qualifying_scope
, args
,
12767 complain
, in_decl
);
12768 fns
= BASELINK_FUNCTIONS (baselink
);
12769 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
12770 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
12772 template_id_p
= true;
12773 template_args
= TREE_OPERAND (fns
, 1);
12774 fns
= TREE_OPERAND (fns
, 0);
12776 template_args
= tsubst_template_args (template_args
, args
,
12777 complain
, in_decl
);
12779 name
= DECL_NAME (get_first_fn (fns
));
12780 if (IDENTIFIER_TYPENAME_P (name
))
12781 name
= mangle_conv_op_name_for_type (optype
);
12782 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
12784 return error_mark_node
;
12786 /* If lookup found a single function, mark it as used at this
12787 point. (If it lookup found multiple functions the one selected
12788 later by overload resolution will be marked as used at that
12790 if (BASELINK_P (baselink
))
12791 fns
= BASELINK_FUNCTIONS (baselink
);
12792 if (!template_id_p
&& !really_overloaded_fn (fns
)
12793 && !mark_used (OVL_CURRENT (fns
), complain
) && !(complain
& tf_error
))
12794 return error_mark_node
;
12796 /* Add back the template arguments, if present. */
12797 if (BASELINK_P (baselink
) && template_id_p
)
12798 BASELINK_FUNCTIONS (baselink
)
12799 = build_nt (TEMPLATE_ID_EXPR
,
12800 BASELINK_FUNCTIONS (baselink
),
12802 /* Update the conversion operator type. */
12803 BASELINK_OPTYPE (baselink
) = optype
;
12806 object_type
= current_class_type
;
12809 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
12815 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12816 true if the qualified-id will be a postfix-expression in-and-of
12817 itself; false if more of the postfix-expression follows the
12818 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12822 tsubst_qualified_id (tree qualified_id
, tree args
,
12823 tsubst_flags_t complain
, tree in_decl
,
12824 bool done
, bool address_p
)
12830 tree template_args
;
12831 location_t loc
= UNKNOWN_LOCATION
;
12833 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
12835 /* Figure out what name to look up. */
12836 name
= TREE_OPERAND (qualified_id
, 1);
12837 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
12839 is_template
= true;
12840 loc
= EXPR_LOCATION (name
);
12841 template_args
= TREE_OPERAND (name
, 1);
12843 template_args
= tsubst_template_args (template_args
, args
,
12844 complain
, in_decl
);
12845 name
= TREE_OPERAND (name
, 0);
12849 is_template
= false;
12850 template_args
= NULL_TREE
;
12853 /* Substitute into the qualifying scope. When there are no ARGS, we
12854 are just trying to simplify a non-dependent expression. In that
12855 case the qualifying scope may be dependent, and, in any case,
12856 substituting will not help. */
12857 scope
= TREE_OPERAND (qualified_id
, 0);
12860 scope
= tsubst (scope
, args
, complain
, in_decl
);
12861 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
12866 if (dependent_scope_p (scope
))
12869 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
12870 return build_qualified_name (NULL_TREE
, scope
, expr
,
12871 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
12874 if (!BASELINK_P (name
) && !DECL_P (expr
))
12876 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
12878 /* A BIT_NOT_EXPR is used to represent a destructor. */
12879 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
12881 error ("qualifying type %qT does not match destructor name ~%qT",
12882 scope
, TREE_OPERAND (expr
, 0));
12883 expr
= error_mark_node
;
12886 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
12887 /*is_type_p=*/0, false);
12890 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
12891 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
12892 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
12894 if (complain
& tf_error
)
12896 error ("dependent-name %qE is parsed as a non-type, but "
12897 "instantiation yields a type", qualified_id
);
12898 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
12900 return error_mark_node
;
12906 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
12908 /* Remember that there was a reference to this entity. */
12909 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
12910 return error_mark_node
;
12913 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
12915 if (complain
& tf_error
)
12916 qualified_name_lookup_error (scope
,
12917 TREE_OPERAND (qualified_id
, 1),
12918 expr
, input_location
);
12919 return error_mark_node
;
12923 expr
= lookup_template_function (expr
, template_args
);
12925 if (expr
== error_mark_node
&& complain
& tf_error
)
12926 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
12927 expr
, input_location
);
12928 else if (TYPE_P (scope
))
12930 expr
= (adjust_result_of_qualified_name_lookup
12931 (expr
, scope
, current_nonlambda_class_type ()));
12932 expr
= (finish_qualified_id_expr
12933 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
12934 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
12935 /*template_arg_p=*/false, complain
));
12938 /* Expressions do not generally have reference type. */
12939 if (TREE_CODE (expr
) != SCOPE_REF
12940 /* However, if we're about to form a pointer-to-member, we just
12941 want the referenced member referenced. */
12942 && TREE_CODE (expr
) != OFFSET_REF
)
12943 expr
= convert_from_reference (expr
);
12948 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
12949 initializer, DECL is the substituted VAR_DECL. Other arguments are as
12953 tsubst_init (tree init
, tree decl
, tree args
,
12954 tsubst_flags_t complain
, tree in_decl
)
12959 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
12963 /* If we had an initializer but it
12964 instantiated to nothing,
12965 value-initialize the object. This will
12966 only occur when the initializer was a
12967 pack expansion where the parameter packs
12968 used in that expansion were of length
12970 init
= build_value_init (TREE_TYPE (decl
),
12972 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
12973 init
= get_target_expr_sfinae (init
, complain
);
12979 /* Like tsubst, but deals with expressions. This function just replaces
12980 template parms; to finish processing the resultant expression, use
12981 tsubst_copy_and_build or tsubst_expr. */
12984 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12986 enum tree_code code
;
12989 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
12992 code
= TREE_CODE (t
);
12997 r
= retrieve_local_specialization (t
);
12999 if (r
== NULL_TREE
)
13001 /* We get here for a use of 'this' in an NSDMI. */
13002 if (DECL_NAME (t
) == this_identifier
13003 && current_function_decl
13004 && DECL_CONSTRUCTOR_P (current_function_decl
))
13005 return current_class_ptr
;
13007 /* This can happen for a parameter name used later in a function
13008 declaration (such as in a late-specified return type). Just
13009 make a dummy decl, since it's only used for its type. */
13010 gcc_assert (cp_unevaluated_operand
!= 0);
13011 r
= tsubst_decl (t
, args
, complain
);
13012 /* Give it the template pattern as its context; its true context
13013 hasn't been instantiated yet and this is good enough for
13015 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
13018 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
13019 r
= ARGUMENT_PACK_SELECT_ARG (r
);
13020 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
13021 return error_mark_node
;
13029 if (DECL_TEMPLATE_PARM_P (t
))
13030 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
13031 /* There is no need to substitute into namespace-scope
13033 if (DECL_NAMESPACE_SCOPE_P (t
))
13035 /* If ARGS is NULL, then T is known to be non-dependent. */
13036 if (args
== NULL_TREE
)
13037 return scalar_constant_value (t
);
13039 /* Unfortunately, we cannot just call lookup_name here.
13042 template <int I> int f() {
13044 struct S { void g() { E e = a; } };
13047 When we instantiate f<7>::S::g(), say, lookup_name is not
13048 clever enough to find f<7>::a. */
13050 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
13051 /*entering_scope=*/0);
13053 for (v
= TYPE_VALUES (enum_type
);
13055 v
= TREE_CHAIN (v
))
13056 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
13057 return TREE_VALUE (v
);
13059 /* We didn't find the name. That should never happen; if
13060 name-lookup found it during preliminary parsing, we
13061 should find it again here during instantiation. */
13062 gcc_unreachable ();
13067 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
13069 /* Check for a local specialization set up by
13070 tsubst_pack_expansion. */
13071 if (tree r
= retrieve_local_specialization (t
))
13073 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
13074 r
= ARGUMENT_PACK_SELECT_ARG (r
);
13078 /* When retrieving a capture pack from a generic lambda, remove the
13079 lambda call op's own template argument list from ARGS. Only the
13080 template arguments active for the closure type should be used to
13081 retrieve the pack specialization. */
13082 if (LAMBDA_FUNCTION_P (current_function_decl
)
13083 && (template_class_depth (DECL_CONTEXT (t
))
13084 != TMPL_ARGS_DEPTH (args
)))
13085 args
= strip_innermost_template_args (args
, 1);
13087 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
13088 tsubst_decl put in the hash table. */
13089 return retrieve_specialization (t
, args
, 0);
13092 if (DECL_CONTEXT (t
))
13096 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
13097 /*entering_scope=*/1);
13098 if (ctx
!= DECL_CONTEXT (t
))
13100 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
13103 if (complain
& tf_error
)
13104 error ("using invalid field %qD", t
);
13105 return error_mark_node
;
13114 case FUNCTION_DECL
:
13115 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
13116 r
= tsubst (t
, args
, complain
, in_decl
);
13117 else if (local_variable_p (t
))
13119 r
= retrieve_local_specialization (t
);
13120 if (r
== NULL_TREE
)
13122 /* First try name lookup to find the instantiation. */
13123 r
= lookup_name (DECL_NAME (t
));
13126 /* Make sure that the one we found is the one we want. */
13127 tree ctx
= tsubst (DECL_CONTEXT (t
), args
,
13128 complain
, in_decl
);
13129 if (ctx
!= DECL_CONTEXT (r
))
13137 /* This can happen for a variable used in a
13138 late-specified return type of a local lambda, or for a
13139 local static or constant. Building a new VAR_DECL
13140 should be OK in all those cases. */
13141 r
= tsubst_decl (t
, args
, complain
);
13142 if (decl_maybe_constant_var_p (r
))
13144 /* We can't call cp_finish_decl, so handle the
13145 initializer by hand. */
13146 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
13147 complain
, in_decl
);
13148 if (!processing_template_decl
)
13149 init
= maybe_constant_init (init
);
13150 if (processing_template_decl
13151 ? potential_constant_expression (init
)
13152 : reduced_constant_expression_p (init
))
13153 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
13154 = TREE_CONSTANT (r
) = true;
13155 DECL_INITIAL (r
) = init
;
13157 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
13158 || decl_constant_var_p (r
)
13159 || errorcount
|| sorrycount
);
13160 if (!processing_template_decl
)
13162 if (TREE_STATIC (r
))
13163 rest_of_decl_compilation (r
, toplevel_bindings_p (),
13166 r
= process_outer_var_ref (r
, complain
);
13169 /* Remember this for subsequent uses. */
13170 if (local_specializations
)
13171 register_local_specialization (r
, t
);
13176 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
13177 return error_mark_node
;
13180 case NAMESPACE_DECL
:
13184 /* An OVERLOAD will always be a non-dependent overload set; an
13185 overload set from function scope will just be represented with an
13186 IDENTIFIER_NODE, and from class scope with a BASELINK. */
13187 gcc_assert (!uses_template_parms (t
));
13191 return tsubst_baselink (t
, current_nonlambda_class_type (),
13192 args
, complain
, in_decl
);
13194 case TEMPLATE_DECL
:
13195 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
13196 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
13197 args
, complain
, in_decl
);
13198 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
13199 return tsubst (t
, args
, complain
, in_decl
);
13200 else if (DECL_CLASS_SCOPE_P (t
)
13201 && uses_template_parms (DECL_CONTEXT (t
)))
13203 /* Template template argument like the following example need
13206 template <template <class> class TT> struct C {};
13207 template <class T> struct D {
13208 template <class U> struct E {};
13213 We are processing the template argument `E' in #1 for
13214 the template instantiation #2. Originally, `E' is a
13215 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
13216 have to substitute this with one having context `D<int>'. */
13218 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
13219 return lookup_field (context
, DECL_NAME(t
), 0, false);
13222 /* Ordinary template template argument. */
13226 case REINTERPRET_CAST_EXPR
:
13227 case CONST_CAST_EXPR
:
13228 case STATIC_CAST_EXPR
:
13229 case DYNAMIC_CAST_EXPR
:
13230 case IMPLICIT_CONV_EXPR
:
13234 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13235 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13236 return build1 (code
, type
, op0
);
13240 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
13243 tree expanded
, op
= TREE_OPERAND (t
, 0);
13246 if (SIZEOF_EXPR_TYPE_P (t
))
13247 op
= TREE_TYPE (op
);
13249 ++cp_unevaluated_operand
;
13250 ++c_inhibit_evaluation_warnings
;
13251 /* We only want to compute the number of arguments. */
13252 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
13253 --cp_unevaluated_operand
;
13254 --c_inhibit_evaluation_warnings
;
13256 if (TREE_CODE (expanded
) == TREE_VEC
)
13257 len
= TREE_VEC_LENGTH (expanded
);
13259 if (expanded
== error_mark_node
)
13260 return error_mark_node
;
13261 else if (PACK_EXPANSION_P (expanded
)
13262 || (TREE_CODE (expanded
) == TREE_VEC
13264 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
13266 if (TREE_CODE (expanded
) == TREE_VEC
)
13267 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
13269 if (TYPE_P (expanded
))
13270 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
13271 complain
& tf_error
);
13273 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
13274 complain
& tf_error
);
13277 return build_int_cst (size_type_node
, len
);
13279 if (SIZEOF_EXPR_TYPE_P (t
))
13281 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
13282 args
, complain
, in_decl
);
13283 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
13284 r
= build1 (SIZEOF_EXPR
,
13285 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
13286 SIZEOF_EXPR_TYPE_P (r
) = 1;
13293 case TRUTH_NOT_EXPR
:
13296 case UNARY_PLUS_EXPR
: /* Unary + */
13298 case AT_ENCODE_EXPR
:
13302 case REALPART_EXPR
:
13303 case IMAGPART_EXPR
:
13306 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13307 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13308 return build1 (code
, type
, op0
);
13311 case COMPONENT_REF
:
13316 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13317 name
= TREE_OPERAND (t
, 1);
13318 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
13320 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
13321 complain
, in_decl
);
13322 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
13324 else if (TREE_CODE (name
) == SCOPE_REF
13325 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
13327 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
13328 complain
, in_decl
);
13329 name
= TREE_OPERAND (name
, 1);
13330 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
13331 complain
, in_decl
);
13332 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
13333 name
= build_qualified_name (/*type=*/NULL_TREE
,
13335 /*template_p=*/false);
13337 else if (BASELINK_P (name
))
13338 name
= tsubst_baselink (name
,
13339 non_reference (TREE_TYPE (object
)),
13343 name
= tsubst_copy (name
, args
, complain
, in_decl
);
13344 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
13350 case TRUNC_DIV_EXPR
:
13351 case CEIL_DIV_EXPR
:
13352 case FLOOR_DIV_EXPR
:
13353 case ROUND_DIV_EXPR
:
13354 case EXACT_DIV_EXPR
:
13358 case TRUNC_MOD_EXPR
:
13359 case FLOOR_MOD_EXPR
:
13360 case TRUTH_ANDIF_EXPR
:
13361 case TRUTH_ORIF_EXPR
:
13362 case TRUTH_AND_EXPR
:
13363 case TRUTH_OR_EXPR
:
13376 case COMPOUND_EXPR
:
13379 case PREDECREMENT_EXPR
:
13380 case PREINCREMENT_EXPR
:
13381 case POSTDECREMENT_EXPR
:
13382 case POSTINCREMENT_EXPR
:
13384 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13385 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13386 return build_nt (code
, op0
, op1
);
13391 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13392 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13393 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
13394 QUALIFIED_NAME_IS_TEMPLATE (t
));
13399 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13400 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13401 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
13406 int n
= VL_EXP_OPERAND_LENGTH (t
);
13407 tree result
= build_vl_exp (CALL_EXPR
, n
);
13409 for (i
= 0; i
< n
; i
++)
13410 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
13411 complain
, in_decl
);
13417 case PSEUDO_DTOR_EXPR
:
13418 case VEC_PERM_EXPR
:
13420 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13421 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13422 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
13423 r
= build_nt (code
, op0
, op1
, op2
);
13424 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13430 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13431 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13432 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
13433 r
= build_nt (code
, op0
, op1
, op2
);
13434 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
13440 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13441 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13442 r
= build_nt (code
, op0
, op1
);
13443 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
13444 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
13448 case TEMPLATE_ID_EXPR
:
13450 /* Substituted template arguments */
13451 tree fn
= TREE_OPERAND (t
, 0);
13452 tree targs
= TREE_OPERAND (t
, 1);
13454 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
13456 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
13458 return lookup_template_function (fn
, targs
);
13463 tree purpose
, value
, chain
;
13465 if (t
== void_list_node
)
13468 purpose
= TREE_PURPOSE (t
);
13470 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
13471 value
= TREE_VALUE (t
);
13473 value
= tsubst_copy (value
, args
, complain
, in_decl
);
13474 chain
= TREE_CHAIN (t
);
13475 if (chain
&& chain
!= void_type_node
)
13476 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
13477 if (purpose
== TREE_PURPOSE (t
)
13478 && value
== TREE_VALUE (t
)
13479 && chain
== TREE_CHAIN (t
))
13481 return tree_cons (purpose
, value
, chain
);
13486 case ENUMERAL_TYPE
:
13488 case TEMPLATE_TYPE_PARM
:
13489 case TEMPLATE_TEMPLATE_PARM
:
13490 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13491 case TEMPLATE_PARM_INDEX
:
13493 case REFERENCE_TYPE
:
13495 case FUNCTION_TYPE
:
13498 case TYPENAME_TYPE
:
13499 case UNBOUND_CLASS_TEMPLATE
:
13501 case DECLTYPE_TYPE
:
13503 return tsubst (t
, args
, complain
, in_decl
);
13507 /* Fall through. */
13508 case IDENTIFIER_NODE
:
13509 if (IDENTIFIER_TYPENAME_P (t
))
13511 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13512 return mangle_conv_op_name_for_type (new_type
);
13518 /* This is handled by tsubst_copy_and_build. */
13519 gcc_unreachable ();
13523 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13524 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13525 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
13528 case CLEANUP_POINT_EXPR
:
13529 /* We shouldn't have built any of these during initial template
13530 generation. Instead, they should be built during instantiation
13531 in response to the saved STMT_IS_FULL_EXPR_P setting. */
13532 gcc_unreachable ();
13536 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13537 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13538 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13539 r
= build2 (code
, type
, op0
, op1
);
13540 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
13541 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
13542 && !(complain
& tf_error
))
13543 return error_mark_node
;
13547 case EXPR_PACK_EXPANSION
:
13548 error ("invalid use of pack expansion expression");
13549 return error_mark_node
;
13551 case NONTYPE_ARGUMENT_PACK
:
13552 error ("use %<...%> to expand argument pack");
13553 return error_mark_node
;
13556 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
13564 /* Instantiate any typedefs in the type. */
13565 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13566 r
= fold_convert (type
, t
);
13567 gcc_assert (TREE_CODE (r
) == code
);
13572 /* These can sometimes show up in a partial instantiation, but never
13573 involve template parms. */
13574 gcc_assert (!uses_template_parms (t
));
13578 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
13579 gcc_checking_assert (false);
13584 /* Helper function for tsubst_omp_clauses, used for instantiation of
13585 OMP_CLAUSE_DECL of clauses that handles also OpenMP array sections
13586 represented with TREE_LIST. */
13589 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
13592 if (TREE_CODE (decl
) == TREE_LIST
)
13595 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
13596 /*integral_constant_expression_p=*/false);
13597 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
13598 /*integral_constant_expression_p=*/false);
13599 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
13601 if (TREE_PURPOSE (decl
) == low_bound
13602 && TREE_VALUE (decl
) == length
13603 && TREE_CHAIN (decl
) == chain
)
13605 return tree_cons (low_bound
, length
, chain
);
13607 return tsubst_copy (decl
, args
, complain
, in_decl
);
13610 /* Like tsubst_copy, but specifically for OpenMP clauses. */
13613 tsubst_omp_clauses (tree clauses
, bool declare_simd
,
13614 tree args
, tsubst_flags_t complain
, tree in_decl
)
13616 tree new_clauses
= NULL
, nc
, oc
;
13618 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
13620 nc
= copy_node (oc
);
13621 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
13624 switch (OMP_CLAUSE_CODE (nc
))
13626 case OMP_CLAUSE_LASTPRIVATE
:
13627 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
13629 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
13630 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
13631 in_decl
, /*integral_constant_expression_p=*/false);
13632 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
13633 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
13636 case OMP_CLAUSE_PRIVATE
:
13637 case OMP_CLAUSE_SHARED
:
13638 case OMP_CLAUSE_FIRSTPRIVATE
:
13639 case OMP_CLAUSE_COPYIN
:
13640 case OMP_CLAUSE_COPYPRIVATE
:
13641 case OMP_CLAUSE_UNIFORM
:
13642 OMP_CLAUSE_DECL (nc
) = tsubst_copy (OMP_CLAUSE_DECL (oc
), args
,
13643 complain
, in_decl
);
13645 case OMP_CLAUSE_DEPEND
:
13646 case OMP_CLAUSE_FROM
:
13647 case OMP_CLAUSE_TO
:
13648 case OMP_CLAUSE_MAP
:
13649 OMP_CLAUSE_DECL (nc
)
13650 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
13653 case OMP_CLAUSE_IF
:
13654 case OMP_CLAUSE_NUM_THREADS
:
13655 case OMP_CLAUSE_SCHEDULE
:
13656 case OMP_CLAUSE_COLLAPSE
:
13657 case OMP_CLAUSE_FINAL
:
13658 case OMP_CLAUSE_DEVICE
:
13659 case OMP_CLAUSE_DIST_SCHEDULE
:
13660 case OMP_CLAUSE_NUM_TEAMS
:
13661 case OMP_CLAUSE_THREAD_LIMIT
:
13662 case OMP_CLAUSE_SAFELEN
:
13663 case OMP_CLAUSE_SIMDLEN
:
13664 OMP_CLAUSE_OPERAND (nc
, 0)
13665 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
13666 in_decl
, /*integral_constant_expression_p=*/false);
13668 case OMP_CLAUSE_REDUCTION
:
13669 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
13671 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
13672 if (TREE_CODE (placeholder
) == SCOPE_REF
)
13674 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
13675 complain
, in_decl
);
13676 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
13677 = build_qualified_name (NULL_TREE
, scope
,
13678 TREE_OPERAND (placeholder
, 1),
13682 gcc_assert (identifier_p (placeholder
));
13684 OMP_CLAUSE_DECL (nc
) = tsubst_copy (OMP_CLAUSE_DECL (oc
), args
,
13685 complain
, in_decl
);
13687 case OMP_CLAUSE_LINEAR
:
13688 case OMP_CLAUSE_ALIGNED
:
13689 OMP_CLAUSE_DECL (nc
) = tsubst_copy (OMP_CLAUSE_DECL (oc
), args
,
13690 complain
, in_decl
);
13691 OMP_CLAUSE_OPERAND (nc
, 1)
13692 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
13693 in_decl
, /*integral_constant_expression_p=*/false);
13695 case OMP_CLAUSE_NOWAIT
:
13696 case OMP_CLAUSE_ORDERED
:
13697 case OMP_CLAUSE_DEFAULT
:
13698 case OMP_CLAUSE_UNTIED
:
13699 case OMP_CLAUSE_MERGEABLE
:
13700 case OMP_CLAUSE_INBRANCH
:
13701 case OMP_CLAUSE_NOTINBRANCH
:
13702 case OMP_CLAUSE_PROC_BIND
:
13703 case OMP_CLAUSE_FOR
:
13704 case OMP_CLAUSE_PARALLEL
:
13705 case OMP_CLAUSE_SECTIONS
:
13706 case OMP_CLAUSE_TASKGROUP
:
13709 gcc_unreachable ();
13713 new_clauses
= nreverse (new_clauses
);
13715 new_clauses
= finish_omp_clauses (new_clauses
);
13716 return new_clauses
;
13719 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
13722 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
13725 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
13727 tree purpose
, value
, chain
;
13732 if (TREE_CODE (t
) != TREE_LIST
)
13733 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13734 /*function_p=*/false,
13735 /*integral_constant_expression_p=*/false);
13737 if (t
== void_list_node
)
13740 purpose
= TREE_PURPOSE (t
);
13742 purpose
= RECUR (purpose
);
13743 value
= TREE_VALUE (t
);
13746 if (TREE_CODE (value
) != LABEL_DECL
)
13747 value
= RECUR (value
);
13750 value
= lookup_label (DECL_NAME (value
));
13751 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
13752 TREE_USED (value
) = 1;
13755 chain
= TREE_CHAIN (t
);
13756 if (chain
&& chain
!= void_type_node
)
13757 chain
= RECUR (chain
);
13758 return tree_cons (purpose
, value
, chain
);
13762 /* Substitute one OMP_FOR iterator. */
13765 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
13766 tree condv
, tree incrv
, tree
*clauses
,
13767 tree args
, tsubst_flags_t complain
, tree in_decl
,
13768 bool integral_constant_expression_p
)
13770 #define RECUR(NODE) \
13771 tsubst_expr ((NODE), args, complain, in_decl, \
13772 integral_constant_expression_p)
13773 tree decl
, init
, cond
, incr
;
13775 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
13776 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
13777 decl
= TREE_OPERAND (init
, 0);
13778 init
= TREE_OPERAND (init
, 1);
13779 tree decl_expr
= NULL_TREE
;
13780 if (init
&& TREE_CODE (init
) == DECL_EXPR
)
13782 /* We need to jump through some hoops to handle declarations in the
13783 for-init-statement, since we might need to handle auto deduction,
13784 but we need to keep control of initialization. */
13786 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
13787 decl
= tsubst_decl (decl
, args
, complain
);
13790 decl
= RECUR (decl
);
13791 init
= RECUR (init
);
13793 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
13794 if (auto_node
&& init
)
13796 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
);
13798 gcc_assert (!type_dependent_expression_p (decl
));
13800 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
13804 /* Declare the variable, but don't let that initialize it. */
13805 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
13806 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
13808 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
13811 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
13812 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
13813 if (TREE_CODE (incr
) == MODIFY_EXPR
)
13815 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
13816 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
13817 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
13818 NOP_EXPR
, rhs
, complain
);
13821 incr
= RECUR (incr
);
13822 TREE_VEC_ELT (declv
, i
) = decl
;
13823 TREE_VEC_ELT (initv
, i
) = init
;
13824 TREE_VEC_ELT (condv
, i
) = cond
;
13825 TREE_VEC_ELT (incrv
, i
) = incr
;
13831 /* Declare and initialize the variable. */
13838 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
13840 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
13841 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
13842 && OMP_CLAUSE_DECL (c
) == decl
)
13844 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
13845 && OMP_CLAUSE_DECL (c
) == decl
)
13846 error ("iteration variable %qD should not be firstprivate", decl
);
13847 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
13848 && OMP_CLAUSE_DECL (c
) == decl
)
13849 error ("iteration variable %qD should not be reduction", decl
);
13853 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
13854 OMP_CLAUSE_DECL (c
) = decl
;
13855 c
= finish_omp_clauses (c
);
13858 OMP_CLAUSE_CHAIN (c
) = *clauses
;
13863 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
13864 if (COMPARISON_CLASS_P (cond
))
13866 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
13867 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
13868 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
13871 cond
= RECUR (cond
);
13872 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
13873 switch (TREE_CODE (incr
))
13875 case PREINCREMENT_EXPR
:
13876 case PREDECREMENT_EXPR
:
13877 case POSTINCREMENT_EXPR
:
13878 case POSTDECREMENT_EXPR
:
13879 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
13880 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
13883 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
13884 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
13886 tree rhs
= TREE_OPERAND (incr
, 1);
13887 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
13888 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
13889 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
13890 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
13891 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
13895 incr
= RECUR (incr
);
13898 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
13899 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
13901 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
13902 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
13903 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
13904 TREE_TYPE (decl
), lhs
,
13905 RECUR (TREE_OPERAND (incr
, 2))));
13907 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
13908 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
13909 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
13911 tree rhs
= TREE_OPERAND (incr
, 2);
13912 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
13913 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
13914 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
13915 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
13916 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
13920 incr
= RECUR (incr
);
13923 incr
= RECUR (incr
);
13927 TREE_VEC_ELT (declv
, i
) = decl
;
13928 TREE_VEC_ELT (initv
, i
) = init
;
13929 TREE_VEC_ELT (condv
, i
) = cond
;
13930 TREE_VEC_ELT (incrv
, i
) = incr
;
13934 /* Like tsubst_copy for expressions, etc. but also does semantic
13938 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
13939 bool integral_constant_expression_p
)
13941 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13942 #define RECUR(NODE) \
13943 tsubst_expr ((NODE), args, complain, in_decl, \
13944 integral_constant_expression_p)
13950 if (t
== NULL_TREE
|| t
== error_mark_node
)
13953 loc
= input_location
;
13954 if (EXPR_HAS_LOCATION (t
))
13955 input_location
= EXPR_LOCATION (t
);
13956 if (STATEMENT_CODE_P (TREE_CODE (t
)))
13957 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
13959 switch (TREE_CODE (t
))
13961 case STATEMENT_LIST
:
13963 tree_stmt_iterator i
;
13964 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
13965 RECUR (tsi_stmt (i
));
13969 case CTOR_INITIALIZER
:
13970 finish_mem_initializers (tsubst_initializer_list
13971 (TREE_OPERAND (t
, 0), args
));
13975 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
13979 tmp
= RECUR (EXPR_STMT_EXPR (t
));
13980 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
13981 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
13983 finish_expr_stmt (tmp
);
13987 do_using_directive (USING_STMT_NAMESPACE (t
));
13992 tree decl
, pattern_decl
;
13995 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
13996 if (TREE_CODE (decl
) == LABEL_DECL
)
13997 finish_label_decl (DECL_NAME (decl
));
13998 else if (TREE_CODE (decl
) == USING_DECL
)
14000 tree scope
= USING_DECL_SCOPE (decl
);
14001 tree name
= DECL_NAME (decl
);
14004 scope
= tsubst (scope
, args
, complain
, in_decl
);
14005 decl
= lookup_qualified_name (scope
, name
,
14006 /*is_type_p=*/false,
14007 /*complain=*/false);
14008 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
14009 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
14011 do_local_using_decl (decl
, scope
, name
);
14013 else if (DECL_PACK_P (decl
))
14015 /* Don't build up decls for a variadic capture proxy, we'll
14016 instantiate the elements directly as needed. */
14021 init
= DECL_INITIAL (decl
);
14022 decl
= tsubst (decl
, args
, complain
, in_decl
);
14023 if (decl
!= error_mark_node
)
14025 /* By marking the declaration as instantiated, we avoid
14026 trying to instantiate it. Since instantiate_decl can't
14027 handle local variables, and since we've already done
14028 all that needs to be done, that's the right thing to
14031 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
14033 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
14034 /* Anonymous aggregates are a special case. */
14035 finish_anon_union (decl
);
14036 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
14038 DECL_CONTEXT (decl
) = current_function_decl
;
14039 if (DECL_NAME (decl
) == this_identifier
)
14041 tree lam
= DECL_CONTEXT (current_function_decl
);
14042 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
14043 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
14045 insert_capture_proxy (decl
);
14047 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
14048 /* We already did a pushtag. */;
14049 else if (TREE_CODE (decl
) == FUNCTION_DECL
14050 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
14051 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
14053 DECL_CONTEXT (decl
) = NULL_TREE
;
14055 DECL_CONTEXT (decl
) = current_function_decl
;
14056 cp_check_omp_declare_reduction (decl
);
14060 int const_init
= false;
14061 maybe_push_decl (decl
);
14063 && DECL_PRETTY_FUNCTION_P (decl
))
14065 /* For __PRETTY_FUNCTION__ we have to adjust the
14067 const char *const name
14068 = cxx_printable_name (current_function_decl
, 2);
14069 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
14072 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
14075 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
14077 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
14086 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
14087 RECUR (FOR_INIT_STMT (t
));
14088 finish_for_init_stmt (stmt
);
14089 tmp
= RECUR (FOR_COND (t
));
14090 finish_for_cond (tmp
, stmt
, false);
14091 tmp
= RECUR (FOR_EXPR (t
));
14092 finish_for_expr (tmp
, stmt
);
14093 RECUR (FOR_BODY (t
));
14094 finish_for_stmt (stmt
);
14097 case RANGE_FOR_STMT
:
14100 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
14101 decl
= RANGE_FOR_DECL (t
);
14102 decl
= tsubst (decl
, args
, complain
, in_decl
);
14103 maybe_push_decl (decl
);
14104 expr
= RECUR (RANGE_FOR_EXPR (t
));
14105 stmt
= cp_convert_range_for (stmt
, decl
, expr
, RANGE_FOR_IVDEP (t
));
14106 RECUR (RANGE_FOR_BODY (t
));
14107 finish_for_stmt (stmt
);
14112 stmt
= begin_while_stmt ();
14113 tmp
= RECUR (WHILE_COND (t
));
14114 finish_while_stmt_cond (tmp
, stmt
, false);
14115 RECUR (WHILE_BODY (t
));
14116 finish_while_stmt (stmt
);
14120 stmt
= begin_do_stmt ();
14121 RECUR (DO_BODY (t
));
14122 finish_do_body (stmt
);
14123 tmp
= RECUR (DO_COND (t
));
14124 finish_do_stmt (tmp
, stmt
, false);
14128 stmt
= begin_if_stmt ();
14129 tmp
= RECUR (IF_COND (t
));
14130 finish_if_stmt_cond (tmp
, stmt
);
14131 RECUR (THEN_CLAUSE (t
));
14132 finish_then_clause (stmt
);
14134 if (ELSE_CLAUSE (t
))
14136 begin_else_clause (stmt
);
14137 RECUR (ELSE_CLAUSE (t
));
14138 finish_else_clause (stmt
);
14141 finish_if_stmt (stmt
);
14145 if (BIND_EXPR_BODY_BLOCK (t
))
14146 stmt
= begin_function_body ();
14148 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
14149 ? BCS_TRY_BLOCK
: 0);
14151 RECUR (BIND_EXPR_BODY (t
));
14153 if (BIND_EXPR_BODY_BLOCK (t
))
14154 finish_function_body (stmt
);
14156 finish_compound_stmt (stmt
);
14160 finish_break_stmt ();
14163 case CONTINUE_STMT
:
14164 finish_continue_stmt ();
14168 stmt
= begin_switch_stmt ();
14169 tmp
= RECUR (SWITCH_STMT_COND (t
));
14170 finish_switch_cond (tmp
, stmt
);
14171 RECUR (SWITCH_STMT_BODY (t
));
14172 finish_switch_stmt (stmt
);
14175 case CASE_LABEL_EXPR
:
14177 tree low
= RECUR (CASE_LOW (t
));
14178 tree high
= RECUR (CASE_HIGH (t
));
14179 finish_case_label (EXPR_LOCATION (t
), low
, high
);
14185 tree decl
= LABEL_EXPR_LABEL (t
);
14188 label
= finish_label_stmt (DECL_NAME (decl
));
14189 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
14190 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
14195 tmp
= GOTO_DESTINATION (t
);
14196 if (TREE_CODE (tmp
) != LABEL_DECL
)
14197 /* Computed goto's must be tsubst'd into. On the other hand,
14198 non-computed gotos must not be; the identifier in question
14199 will have no binding. */
14202 tmp
= DECL_NAME (tmp
);
14203 finish_goto_stmt (tmp
);
14208 tree string
= RECUR (ASM_STRING (t
));
14209 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
14210 complain
, in_decl
);
14211 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
14212 complain
, in_decl
);
14213 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
14214 complain
, in_decl
);
14215 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
14216 complain
, in_decl
);
14217 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
14219 tree asm_expr
= tmp
;
14220 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
14221 asm_expr
= TREE_OPERAND (asm_expr
, 0);
14222 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
14229 stmt
= begin_try_block ();
14230 RECUR (TRY_STMTS (t
));
14231 finish_cleanup_try_block (stmt
);
14232 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
14236 tree compound_stmt
= NULL_TREE
;
14238 if (FN_TRY_BLOCK_P (t
))
14239 stmt
= begin_function_try_block (&compound_stmt
);
14241 stmt
= begin_try_block ();
14243 RECUR (TRY_STMTS (t
));
14245 if (FN_TRY_BLOCK_P (t
))
14246 finish_function_try_block (stmt
);
14248 finish_try_block (stmt
);
14250 RECUR (TRY_HANDLERS (t
));
14251 if (FN_TRY_BLOCK_P (t
))
14252 finish_function_handler_sequence (stmt
, compound_stmt
);
14254 finish_handler_sequence (stmt
);
14260 tree decl
= HANDLER_PARMS (t
);
14264 decl
= tsubst (decl
, args
, complain
, in_decl
);
14265 /* Prevent instantiate_decl from trying to instantiate
14266 this variable. We've already done all that needs to be
14268 if (decl
!= error_mark_node
)
14269 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
14271 stmt
= begin_handler ();
14272 finish_handler_parms (decl
, stmt
);
14273 RECUR (HANDLER_BODY (t
));
14274 finish_handler (stmt
);
14279 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
14280 if (CLASS_TYPE_P (tmp
))
14282 /* Local classes are not independent templates; they are
14283 instantiated along with their containing function. And this
14284 way we don't have to deal with pushing out of one local class
14285 to instantiate a member of another local class. */
14287 /* Closures are handled by the LAMBDA_EXPR. */
14288 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
14289 complete_type (tmp
);
14290 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
14291 if (!DECL_ARTIFICIAL (fn
))
14292 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
14296 case STATIC_ASSERT
:
14300 ++c_inhibit_evaluation_warnings
;
14302 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
14305 /*integral_constant_expression_p=*/true);
14306 --c_inhibit_evaluation_warnings
;
14308 finish_static_assert (condition
,
14309 STATIC_ASSERT_MESSAGE (t
),
14310 STATIC_ASSERT_SOURCE_LOCATION (t
),
14311 /*member_p=*/false);
14316 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), false,
14317 args
, complain
, in_decl
);
14318 stmt
= begin_omp_parallel ();
14319 RECUR (OMP_PARALLEL_BODY (t
));
14320 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
14321 = OMP_PARALLEL_COMBINED (t
);
14325 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), false,
14326 args
, complain
, in_decl
);
14327 stmt
= begin_omp_task ();
14328 RECUR (OMP_TASK_BODY (t
));
14329 finish_omp_task (tmp
, stmt
);
14336 case OMP_DISTRIBUTE
:
14338 tree clauses
, body
, pre_body
;
14339 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
14340 tree incrv
= NULL_TREE
;
14343 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), false,
14344 args
, complain
, in_decl
);
14345 if (OMP_FOR_INIT (t
) != NULL_TREE
)
14347 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
14348 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
14349 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
14350 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
14353 stmt
= begin_omp_structured_block ();
14355 pre_body
= push_stmt_list ();
14356 RECUR (OMP_FOR_PRE_BODY (t
));
14357 pre_body
= pop_stmt_list (pre_body
);
14359 if (OMP_FOR_INIT (t
) != NULL_TREE
)
14360 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
14361 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
14362 &clauses
, args
, complain
, in_decl
,
14363 integral_constant_expression_p
);
14365 body
= push_stmt_list ();
14366 RECUR (OMP_FOR_BODY (t
));
14367 body
= pop_stmt_list (body
);
14369 if (OMP_FOR_INIT (t
) != NULL_TREE
)
14370 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
, initv
,
14371 condv
, incrv
, body
, pre_body
, clauses
);
14374 t
= make_node (TREE_CODE (t
));
14375 TREE_TYPE (t
) = void_type_node
;
14376 OMP_FOR_BODY (t
) = body
;
14377 OMP_FOR_PRE_BODY (t
) = pre_body
;
14378 OMP_FOR_CLAUSES (t
) = clauses
;
14379 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
14383 add_stmt (finish_omp_structured_block (stmt
));
14390 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false,
14391 args
, complain
, in_decl
);
14392 stmt
= push_stmt_list ();
14393 RECUR (OMP_BODY (t
));
14394 stmt
= pop_stmt_list (stmt
);
14397 OMP_BODY (t
) = stmt
;
14398 OMP_CLAUSES (t
) = tmp
;
14402 case OMP_TARGET_DATA
:
14404 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false,
14405 args
, complain
, in_decl
);
14406 keep_next_level (true);
14407 stmt
= begin_omp_structured_block ();
14409 RECUR (OMP_BODY (t
));
14410 stmt
= finish_omp_structured_block (stmt
);
14413 OMP_BODY (t
) = stmt
;
14414 OMP_CLAUSES (t
) = tmp
;
14418 case OMP_TARGET_UPDATE
:
14419 tmp
= tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t
), false,
14420 args
, complain
, in_decl
);
14422 OMP_TARGET_UPDATE_CLAUSES (t
) = tmp
;
14429 case OMP_TASKGROUP
:
14431 stmt
= push_stmt_list ();
14432 RECUR (OMP_BODY (t
));
14433 stmt
= pop_stmt_list (stmt
);
14436 OMP_BODY (t
) = stmt
;
14441 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
14442 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
14444 tree op1
= TREE_OPERAND (t
, 1);
14445 tree rhs1
= NULL_TREE
;
14447 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
14449 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
14450 op1
= TREE_OPERAND (op1
, 1);
14452 lhs
= RECUR (TREE_OPERAND (op1
, 0));
14453 rhs
= RECUR (TREE_OPERAND (op1
, 1));
14454 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
14455 NULL_TREE
, NULL_TREE
, rhs1
,
14456 OMP_ATOMIC_SEQ_CST (t
));
14460 tree op1
= TREE_OPERAND (t
, 1);
14461 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
14462 tree rhs1
= NULL_TREE
;
14463 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
14464 enum tree_code opcode
= NOP_EXPR
;
14465 if (code
== OMP_ATOMIC_READ
)
14467 v
= RECUR (TREE_OPERAND (op1
, 0));
14468 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
14470 else if (code
== OMP_ATOMIC_CAPTURE_OLD
14471 || code
== OMP_ATOMIC_CAPTURE_NEW
)
14473 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
14474 v
= RECUR (TREE_OPERAND (op1
, 0));
14475 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
14476 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
14478 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
14479 op11
= TREE_OPERAND (op11
, 1);
14481 lhs
= RECUR (TREE_OPERAND (op11
, 0));
14482 rhs
= RECUR (TREE_OPERAND (op11
, 1));
14483 opcode
= TREE_CODE (op11
);
14484 if (opcode
== MODIFY_EXPR
)
14490 lhs
= RECUR (TREE_OPERAND (op1
, 0));
14491 rhs
= RECUR (TREE_OPERAND (op1
, 1));
14493 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
14494 OMP_ATOMIC_SEQ_CST (t
));
14498 case TRANSACTION_EXPR
:
14501 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
14502 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
14504 if (TRANSACTION_EXPR_IS_STMT (t
))
14506 tree body
= TRANSACTION_EXPR_BODY (t
);
14507 tree noex
= NULL_TREE
;
14508 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
14510 noex
= MUST_NOT_THROW_COND (body
);
14511 if (noex
== NULL_TREE
)
14512 noex
= boolean_true_node
;
14513 body
= TREE_OPERAND (body
, 0);
14515 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
14517 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
14521 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
14522 RECUR (TRANSACTION_EXPR_BODY (t
)),
14529 case MUST_NOT_THROW_EXPR
:
14531 tree op0
= RECUR (TREE_OPERAND (t
, 0));
14532 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
14533 RETURN (build_must_not_throw_expr (op0
, cond
));
14536 case EXPR_PACK_EXPANSION
:
14537 error ("invalid use of pack expansion expression");
14538 RETURN (error_mark_node
);
14540 case NONTYPE_ARGUMENT_PACK
:
14541 error ("use %<...%> to expand argument pack");
14542 RETURN (error_mark_node
);
14544 case CILK_SPAWN_STMT
:
14545 cfun
->calls_cilk_spawn
= 1;
14546 RETURN (build_cilk_spawn (EXPR_LOCATION (t
), RECUR (CILK_SPAWN_FN (t
))));
14548 case CILK_SYNC_STMT
:
14549 RETURN (build_cilk_sync ());
14551 case COMPOUND_EXPR
:
14552 tmp
= RECUR (TREE_OPERAND (t
, 0));
14553 if (tmp
== NULL_TREE
)
14554 /* If the first operand was a statement, we're done with it. */
14555 RETURN (RECUR (TREE_OPERAND (t
, 1)));
14556 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
14557 RECUR (TREE_OPERAND (t
, 1)),
14560 case ANNOTATE_EXPR
:
14561 tmp
= RECUR (TREE_OPERAND (t
, 0));
14562 RETURN (build2_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
14563 TREE_TYPE (tmp
), tmp
, RECUR (TREE_OPERAND (t
, 1))));
14566 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
14568 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
14569 /*function_p=*/false,
14570 integral_constant_expression_p
));
14573 RETURN (NULL_TREE
);
14575 input_location
= loc
;
14581 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
14582 function. For description of the body see comment above
14583 cp_parser_omp_declare_reduction_exprs. */
14586 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14588 if (t
== NULL_TREE
|| t
== error_mark_node
)
14591 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
14593 tree_stmt_iterator tsi
;
14596 memset (stmts
, 0, sizeof stmts
);
14597 for (i
= 0, tsi
= tsi_start (t
);
14598 i
< 7 && !tsi_end_p (tsi
);
14599 i
++, tsi_next (&tsi
))
14600 stmts
[i
] = tsi_stmt (tsi
);
14601 gcc_assert (tsi_end_p (tsi
));
14605 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
14606 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
14607 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
14608 args
, complain
, in_decl
);
14609 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
14610 args
, complain
, in_decl
);
14611 DECL_CONTEXT (omp_out
) = current_function_decl
;
14612 DECL_CONTEXT (omp_in
) = current_function_decl
;
14613 keep_next_level (true);
14614 tree block
= begin_omp_structured_block ();
14615 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
14616 block
= finish_omp_structured_block (block
);
14617 block
= maybe_cleanup_point_expr_void (block
);
14618 add_decl_expr (omp_out
);
14619 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
14620 TREE_NO_WARNING (omp_out
) = 1;
14621 add_decl_expr (omp_in
);
14622 finish_expr_stmt (block
);
14626 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
14627 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
14628 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
14629 args
, complain
, in_decl
);
14630 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
14631 args
, complain
, in_decl
);
14632 DECL_CONTEXT (omp_priv
) = current_function_decl
;
14633 DECL_CONTEXT (omp_orig
) = current_function_decl
;
14634 keep_next_level (true);
14635 tree block
= begin_omp_structured_block ();
14636 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
14637 block
= finish_omp_structured_block (block
);
14638 block
= maybe_cleanup_point_expr_void (block
);
14639 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
14640 add_decl_expr (omp_priv
);
14641 add_decl_expr (omp_orig
);
14642 finish_expr_stmt (block
);
14644 add_decl_expr (omp_orig
);
14648 /* T is a postfix-expression that is not being used in a function
14649 call. Return the substituted version of T. */
14652 tsubst_non_call_postfix_expression (tree t
, tree args
,
14653 tsubst_flags_t complain
,
14656 if (TREE_CODE (t
) == SCOPE_REF
)
14657 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
14658 /*done=*/false, /*address_p=*/false);
14660 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
14661 /*function_p=*/false,
14662 /*integral_constant_expression_p=*/false);
14667 /* Like tsubst but deals with expressions and performs semantic
14668 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
14671 tsubst_copy_and_build (tree t
,
14673 tsubst_flags_t complain
,
14676 bool integral_constant_expression_p
)
14678 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
14679 #define RECUR(NODE) \
14680 tsubst_copy_and_build (NODE, args, complain, in_decl, \
14681 /*function_p=*/false, \
14682 integral_constant_expression_p)
14687 if (t
== NULL_TREE
|| t
== error_mark_node
)
14690 loc
= input_location
;
14691 if (EXPR_HAS_LOCATION (t
))
14692 input_location
= EXPR_LOCATION (t
);
14694 /* N3276 decltype magic only applies to calls at the top level or on the
14695 right side of a comma. */
14696 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
14697 complain
&= ~tf_decltype
;
14699 switch (TREE_CODE (t
))
14703 /* Fall through. */
14704 case IDENTIFIER_NODE
:
14708 bool non_integral_constant_expression_p
;
14709 const char *error_msg
;
14711 if (IDENTIFIER_TYPENAME_P (t
))
14713 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14714 t
= mangle_conv_op_name_for_type (new_type
);
14717 /* Look up the name. */
14718 decl
= lookup_name (t
);
14720 /* By convention, expressions use ERROR_MARK_NODE to indicate
14721 failure, not NULL_TREE. */
14722 if (decl
== NULL_TREE
)
14723 decl
= error_mark_node
;
14725 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
14727 integral_constant_expression_p
,
14728 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
14729 &non_integral_constant_expression_p
,
14730 /*template_p=*/false,
14732 /*address_p=*/false,
14733 /*template_arg_p=*/false,
14738 if (!function_p
&& identifier_p (decl
))
14740 if (complain
& tf_error
)
14741 unqualified_name_lookup_error (decl
);
14742 decl
= error_mark_node
;
14747 case TEMPLATE_ID_EXPR
:
14750 tree templ
= RECUR (TREE_OPERAND (t
, 0));
14751 tree targs
= TREE_OPERAND (t
, 1);
14754 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
14755 if (targs
== error_mark_node
)
14756 return error_mark_node
;
14758 if (variable_template_p (templ
))
14760 templ
= lookup_template_variable (templ
, targs
);
14761 if (!any_dependent_template_arguments_p (targs
))
14763 templ
= finish_template_variable (templ
, complain
);
14766 RETURN (convert_from_reference (templ
));
14769 if (TREE_CODE (templ
) == COMPONENT_REF
)
14771 object
= TREE_OPERAND (templ
, 0);
14772 templ
= TREE_OPERAND (templ
, 1);
14775 object
= NULL_TREE
;
14776 templ
= lookup_template_function (templ
, targs
);
14779 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
14780 object
, templ
, NULL_TREE
));
14782 RETURN (baselink_for_fns (templ
));
14787 tree r
= RECUR (TREE_OPERAND (t
, 0));
14789 if (REFERENCE_REF_P (t
))
14791 /* A type conversion to reference type will be enclosed in
14792 such an indirect ref, but the substitution of the cast
14793 will have also added such an indirect ref. */
14794 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
14795 r
= convert_from_reference (r
);
14798 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
14799 complain
|decltype_flag
);
14805 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14806 tree op0
= RECUR (TREE_OPERAND (t
, 0));
14807 RETURN (build_nop (type
, op0
));
14810 case IMPLICIT_CONV_EXPR
:
14812 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14813 tree expr
= RECUR (TREE_OPERAND (t
, 0));
14814 int flags
= LOOKUP_IMPLICIT
;
14815 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
14816 flags
= LOOKUP_NORMAL
;
14817 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
14823 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14824 tree op0
= RECUR (TREE_OPERAND (t
, 0));
14825 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
14829 case REINTERPRET_CAST_EXPR
:
14830 case CONST_CAST_EXPR
:
14831 case DYNAMIC_CAST_EXPR
:
14832 case STATIC_CAST_EXPR
:
14835 tree op
, r
= NULL_TREE
;
14837 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14838 if (integral_constant_expression_p
14839 && !cast_valid_in_integral_constant_expression_p (type
))
14841 if (complain
& tf_error
)
14842 error ("a cast to a type other than an integral or "
14843 "enumeration type cannot appear in a constant-expression");
14844 RETURN (error_mark_node
);
14847 op
= RECUR (TREE_OPERAND (t
, 0));
14849 warning_sentinel
s(warn_useless_cast
);
14850 switch (TREE_CODE (t
))
14853 r
= build_functional_cast (type
, op
, complain
);
14855 case REINTERPRET_CAST_EXPR
:
14856 r
= build_reinterpret_cast (type
, op
, complain
);
14858 case CONST_CAST_EXPR
:
14859 r
= build_const_cast (type
, op
, complain
);
14861 case DYNAMIC_CAST_EXPR
:
14862 r
= build_dynamic_cast (type
, op
, complain
);
14864 case STATIC_CAST_EXPR
:
14865 r
= build_static_cast (type
, op
, complain
);
14868 gcc_unreachable ();
14874 case POSTDECREMENT_EXPR
:
14875 case POSTINCREMENT_EXPR
:
14876 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14877 args
, complain
, in_decl
);
14878 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
14879 complain
|decltype_flag
));
14881 case PREDECREMENT_EXPR
:
14882 case PREINCREMENT_EXPR
:
14886 case TRUTH_NOT_EXPR
:
14887 case UNARY_PLUS_EXPR
: /* Unary + */
14888 case REALPART_EXPR
:
14889 case IMAGPART_EXPR
:
14890 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
14891 RECUR (TREE_OPERAND (t
, 0)),
14892 complain
|decltype_flag
));
14894 case FIX_TRUNC_EXPR
:
14895 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
14899 op1
= TREE_OPERAND (t
, 0);
14900 if (TREE_CODE (op1
) == LABEL_DECL
)
14901 RETURN (finish_label_address_expr (DECL_NAME (op1
),
14902 EXPR_LOCATION (op1
)));
14903 if (TREE_CODE (op1
) == SCOPE_REF
)
14904 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
14905 /*done=*/true, /*address_p=*/true);
14907 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
14909 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
14910 complain
|decltype_flag
));
14915 case TRUNC_DIV_EXPR
:
14916 case CEIL_DIV_EXPR
:
14917 case FLOOR_DIV_EXPR
:
14918 case ROUND_DIV_EXPR
:
14919 case EXACT_DIV_EXPR
:
14923 case TRUNC_MOD_EXPR
:
14924 case FLOOR_MOD_EXPR
:
14925 case TRUTH_ANDIF_EXPR
:
14926 case TRUTH_ORIF_EXPR
:
14927 case TRUTH_AND_EXPR
:
14928 case TRUTH_OR_EXPR
:
14944 warning_sentinel
s1(warn_type_limits
);
14945 warning_sentinel
s2(warn_div_by_zero
);
14946 warning_sentinel
s3(warn_logical_op
);
14947 warning_sentinel
s4(warn_tautological_compare
);
14948 tree op0
= RECUR (TREE_OPERAND (t
, 0));
14949 tree op1
= RECUR (TREE_OPERAND (t
, 1));
14950 tree r
= build_x_binary_op
14951 (input_location
, TREE_CODE (t
),
14953 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
14955 : TREE_CODE (TREE_OPERAND (t
, 0))),
14957 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
14959 : TREE_CODE (TREE_OPERAND (t
, 1))),
14961 complain
|decltype_flag
);
14962 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
14963 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14968 case POINTER_PLUS_EXPR
:
14970 tree op0
= RECUR (TREE_OPERAND (t
, 0));
14971 tree op1
= RECUR (TREE_OPERAND (t
, 1));
14972 return fold_build_pointer_plus (op0
, op1
);
14976 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
14977 /*address_p=*/false));
14979 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14980 args
, complain
, in_decl
);
14981 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
14982 RECUR (TREE_OPERAND (t
, 1)),
14983 complain
|decltype_flag
));
14985 case ARRAY_NOTATION_REF
:
14987 tree start_index
, length
, stride
;
14988 op1
= tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t
),
14989 args
, complain
, in_decl
);
14990 start_index
= RECUR (ARRAY_NOTATION_START (t
));
14991 length
= RECUR (ARRAY_NOTATION_LENGTH (t
));
14992 stride
= RECUR (ARRAY_NOTATION_STRIDE (t
));
14993 RETURN (build_array_notation_ref (EXPR_LOCATION (t
), op1
, start_index
,
14994 length
, stride
, TREE_TYPE (op1
)));
14997 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
14998 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
15005 op1
= TREE_OPERAND (t
, 0);
15006 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
15007 op1
= TREE_TYPE (op1
);
15010 /* When there are no ARGS, we are trying to evaluate a
15011 non-dependent expression from the parser. Trying to do
15012 the substitutions may not work. */
15014 op1
= TREE_TYPE (op1
);
15018 ++cp_unevaluated_operand
;
15019 ++c_inhibit_evaluation_warnings
;
15021 op1
= tsubst (op1
, args
, complain
, in_decl
);
15023 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
15024 /*function_p=*/false,
15025 /*integral_constant_expression_p=*/
15027 --cp_unevaluated_operand
;
15028 --c_inhibit_evaluation_warnings
;
15031 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
15032 complain
& tf_error
);
15034 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
15035 complain
& tf_error
);
15036 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
15038 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
15040 if (!processing_template_decl
&& TYPE_P (op1
))
15042 r
= build_min (SIZEOF_EXPR
, size_type_node
,
15043 build1 (NOP_EXPR
, op1
, error_mark_node
));
15044 SIZEOF_EXPR_TYPE_P (r
) = 1;
15047 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
15048 TREE_SIDE_EFFECTS (r
) = 0;
15049 TREE_READONLY (r
) = 1;
15051 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
15056 case AT_ENCODE_EXPR
:
15058 op1
= TREE_OPERAND (t
, 0);
15059 ++cp_unevaluated_operand
;
15060 ++c_inhibit_evaluation_warnings
;
15061 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
15062 /*function_p=*/false,
15063 /*integral_constant_expression_p=*/false);
15064 --cp_unevaluated_operand
;
15065 --c_inhibit_evaluation_warnings
;
15066 RETURN (objc_build_encode_expr (op1
));
15069 case NOEXCEPT_EXPR
:
15070 op1
= TREE_OPERAND (t
, 0);
15071 ++cp_unevaluated_operand
;
15072 ++c_inhibit_evaluation_warnings
;
15073 ++cp_noexcept_operand
;
15074 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
15075 /*function_p=*/false,
15076 /*integral_constant_expression_p=*/false);
15077 --cp_unevaluated_operand
;
15078 --c_inhibit_evaluation_warnings
;
15079 --cp_noexcept_operand
;
15080 RETURN (finish_noexcept_expr (op1
, complain
));
15084 warning_sentinel
s(warn_div_by_zero
);
15085 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
15086 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
15087 tree r
= build_x_modify_expr
15088 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
15089 complain
|decltype_flag
);
15090 /* TREE_NO_WARNING must be set if either the expression was
15091 parenthesized or it uses an operator such as >>= rather
15092 than plain assignment. In the former case, it was already
15093 set and must be copied. In the latter case,
15094 build_x_modify_expr sets it and it must not be reset
15096 if (TREE_NO_WARNING (t
))
15097 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
15103 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
15104 args
, complain
, in_decl
);
15105 /* Remember that there was a reference to this entity. */
15107 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
15108 RETURN (error_mark_node
);
15109 RETURN (build_x_arrow (input_location
, op1
, complain
));
15113 tree placement
= RECUR (TREE_OPERAND (t
, 0));
15114 tree init
= RECUR (TREE_OPERAND (t
, 3));
15115 vec
<tree
, va_gc
> *placement_vec
;
15116 vec
<tree
, va_gc
> *init_vec
;
15119 if (placement
== NULL_TREE
)
15120 placement_vec
= NULL
;
15123 placement_vec
= make_tree_vector ();
15124 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
15125 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
15128 /* If there was an initializer in the original tree, but it
15129 instantiated to an empty list, then we should pass a
15130 non-NULL empty vector to tell build_new that it was an
15131 empty initializer() rather than no initializer. This can
15132 only happen when the initializer is a pack expansion whose
15133 parameter packs are of length zero. */
15134 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
15138 init_vec
= make_tree_vector ();
15139 if (init
== void_node
)
15140 gcc_assert (init_vec
!= NULL
);
15143 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
15144 vec_safe_push (init_vec
, TREE_VALUE (init
));
15148 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15149 tree op2
= RECUR (TREE_OPERAND (t
, 2));
15150 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
15151 NEW_EXPR_USE_GLOBAL (t
),
15154 if (placement_vec
!= NULL
)
15155 release_tree_vector (placement_vec
);
15156 if (init_vec
!= NULL
)
15157 release_tree_vector (init_vec
);
15164 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15165 tree op1
= RECUR (TREE_OPERAND (t
, 1));
15166 RETURN (delete_sanity (op0
, op1
,
15167 DELETE_EXPR_USE_VEC (t
),
15168 DELETE_EXPR_USE_GLOBAL (t
),
15172 case COMPOUND_EXPR
:
15174 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
15175 complain
& ~tf_decltype
, in_decl
,
15176 /*function_p=*/false,
15177 integral_constant_expression_p
);
15178 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
15180 RECUR (TREE_OPERAND (t
, 1)),
15181 complain
|decltype_flag
));
15187 vec
<tree
, va_gc
> *call_args
;
15188 unsigned int nargs
, i
;
15193 function
= CALL_EXPR_FN (t
);
15194 /* When we parsed the expression, we determined whether or
15195 not Koenig lookup should be performed. */
15196 koenig_p
= KOENIG_LOOKUP_P (t
);
15197 if (TREE_CODE (function
) == SCOPE_REF
)
15199 qualified_p
= true;
15200 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
15202 /*address_p=*/false);
15204 else if (koenig_p
&& identifier_p (function
))
15206 /* Do nothing; calling tsubst_copy_and_build on an identifier
15207 would incorrectly perform unqualified lookup again.
15209 Note that we can also have an IDENTIFIER_NODE if the earlier
15210 unqualified lookup found a member function; in that case
15211 koenig_p will be false and we do want to do the lookup
15212 again to find the instantiated member function.
15214 FIXME but doing that causes c++/15272, so we need to stop
15215 using IDENTIFIER_NODE in that situation. */
15216 qualified_p
= false;
15220 if (TREE_CODE (function
) == COMPONENT_REF
)
15222 tree op
= TREE_OPERAND (function
, 1);
15224 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
15225 || (BASELINK_P (op
)
15226 && BASELINK_QUALIFIED_P (op
)));
15229 qualified_p
= false;
15231 if (TREE_CODE (function
) == ADDR_EXPR
15232 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
15233 /* Avoid error about taking the address of a constructor. */
15234 function
= TREE_OPERAND (function
, 0);
15236 function
= tsubst_copy_and_build (function
, args
, complain
,
15239 integral_constant_expression_p
);
15241 if (BASELINK_P (function
))
15242 qualified_p
= true;
15245 nargs
= call_expr_nargs (t
);
15246 call_args
= make_tree_vector ();
15247 for (i
= 0; i
< nargs
; ++i
)
15249 tree arg
= CALL_EXPR_ARG (t
, i
);
15251 if (!PACK_EXPANSION_P (arg
))
15252 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
15255 /* Expand the pack expansion and push each entry onto
15257 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
15258 if (TREE_CODE (arg
) == TREE_VEC
)
15260 unsigned int len
, j
;
15262 len
= TREE_VEC_LENGTH (arg
);
15263 for (j
= 0; j
< len
; ++j
)
15265 tree value
= TREE_VEC_ELT (arg
, j
);
15266 if (value
!= NULL_TREE
)
15267 value
= convert_from_reference (value
);
15268 vec_safe_push (call_args
, value
);
15273 /* A partial substitution. Add one entry. */
15274 vec_safe_push (call_args
, arg
);
15279 /* We do not perform argument-dependent lookup if normal
15280 lookup finds a non-function, in accordance with the
15281 expected resolution of DR 218. */
15283 && ((is_overloaded_fn (function
)
15284 /* If lookup found a member function, the Koenig lookup is
15285 not appropriate, even if an unqualified-name was used
15286 to denote the function. */
15287 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
15288 || identifier_p (function
))
15289 /* Only do this when substitution turns a dependent call
15290 into a non-dependent call. */
15291 && type_dependent_expression_p_push (t
)
15292 && !any_type_dependent_arguments_p (call_args
))
15293 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
15295 if (identifier_p (function
)
15296 && !any_type_dependent_arguments_p (call_args
))
15298 if (koenig_p
&& (complain
& tf_warning_or_error
))
15300 /* For backwards compatibility and good diagnostics, try
15301 the unqualified lookup again if we aren't in SFINAE
15303 tree unq
= (tsubst_copy_and_build
15304 (function
, args
, complain
, in_decl
, true,
15305 integral_constant_expression_p
));
15306 if (unq
== error_mark_node
)
15307 RETURN (error_mark_node
);
15309 if (unq
!= function
)
15312 if (INDIRECT_REF_P (fn
))
15313 fn
= TREE_OPERAND (fn
, 0);
15314 if (TREE_CODE (fn
) == COMPONENT_REF
)
15315 fn
= TREE_OPERAND (fn
, 1);
15316 if (is_overloaded_fn (fn
))
15317 fn
= get_first_fn (fn
);
15318 if (permerror (EXPR_LOC_OR_LOC (t
, input_location
),
15319 "%qD was not declared in this scope, "
15320 "and no declarations were found by "
15321 "argument-dependent lookup at the point "
15322 "of instantiation", function
))
15325 /* Can't say anything more. */;
15326 else if (DECL_CLASS_SCOPE_P (fn
))
15328 location_t loc
= EXPR_LOC_OR_LOC (t
,
15331 "declarations in dependent base %qT are "
15332 "not found by unqualified lookup",
15333 DECL_CLASS_CONTEXT (fn
));
15334 if (current_class_ptr
)
15336 "use %<this->%D%> instead", function
);
15339 "use %<%T::%D%> instead",
15340 current_class_name
, function
);
15343 inform (DECL_SOURCE_LOCATION (fn
),
15344 "%qD declared here, later in the "
15345 "translation unit", fn
);
15350 if (identifier_p (function
))
15352 if (complain
& tf_error
)
15353 unqualified_name_lookup_error (function
);
15354 release_tree_vector (call_args
);
15355 RETURN (error_mark_node
);
15359 /* Remember that there was a reference to this entity. */
15360 if (DECL_P (function
)
15361 && !mark_used (function
, complain
) && !(complain
& tf_error
))
15362 RETURN (error_mark_node
);
15364 /* Put back tf_decltype for the actual call. */
15365 complain
|= decltype_flag
;
15367 if (TREE_CODE (function
) == OFFSET_REF
)
15368 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
15370 else if (TREE_CODE (function
) == COMPONENT_REF
)
15372 tree instance
= TREE_OPERAND (function
, 0);
15373 tree fn
= TREE_OPERAND (function
, 1);
15375 if (processing_template_decl
15376 && (type_dependent_expression_p (instance
)
15377 || (!BASELINK_P (fn
)
15378 && TREE_CODE (fn
) != FIELD_DECL
)
15379 || type_dependent_expression_p (fn
)
15380 || any_type_dependent_arguments_p (call_args
)))
15381 ret
= build_nt_call_vec (function
, call_args
);
15382 else if (!BASELINK_P (fn
))
15383 ret
= finish_call_expr (function
, &call_args
,
15384 /*disallow_virtual=*/false,
15385 /*koenig_p=*/false,
15388 ret
= (build_new_method_call
15390 &call_args
, NULL_TREE
,
15391 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
15396 ret
= finish_call_expr (function
, &call_args
,
15397 /*disallow_virtual=*/qualified_p
,
15401 release_tree_vector (call_args
);
15408 tree cond
= RECUR (TREE_OPERAND (t
, 0));
15409 tree folded_cond
= fold_non_dependent_expr (cond
);
15412 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
15414 if (integer_zerop (folded_cond
))
15416 ++c_inhibit_evaluation_warnings
;
15417 exp1
= RECUR (TREE_OPERAND (t
, 1));
15418 --c_inhibit_evaluation_warnings
;
15419 exp2
= RECUR (TREE_OPERAND (t
, 2));
15423 exp1
= RECUR (TREE_OPERAND (t
, 1));
15424 ++c_inhibit_evaluation_warnings
;
15425 exp2
= RECUR (TREE_OPERAND (t
, 2));
15426 --c_inhibit_evaluation_warnings
;
15428 cond
= folded_cond
;
15432 exp1
= RECUR (TREE_OPERAND (t
, 1));
15433 exp2
= RECUR (TREE_OPERAND (t
, 2));
15436 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
15437 cond
, exp1
, exp2
, complain
));
15440 case PSEUDO_DTOR_EXPR
:
15442 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15443 tree op1
= RECUR (TREE_OPERAND (t
, 1));
15444 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15445 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
15451 tree purpose
, value
, chain
;
15453 if (t
== void_list_node
)
15456 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
15457 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
15459 /* We have pack expansions, so expand those and
15460 create a new list out of it. */
15461 tree purposevec
= NULL_TREE
;
15462 tree valuevec
= NULL_TREE
;
15466 /* Expand the argument expressions. */
15467 if (TREE_PURPOSE (t
))
15468 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
15469 complain
, in_decl
);
15470 if (TREE_VALUE (t
))
15471 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
15472 complain
, in_decl
);
15474 /* Build the rest of the list. */
15475 chain
= TREE_CHAIN (t
);
15476 if (chain
&& chain
!= void_type_node
)
15477 chain
= RECUR (chain
);
15479 /* Determine the number of arguments. */
15480 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
15482 len
= TREE_VEC_LENGTH (purposevec
);
15483 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
15485 else if (TREE_CODE (valuevec
) == TREE_VEC
)
15486 len
= TREE_VEC_LENGTH (valuevec
);
15489 /* Since we only performed a partial substitution into
15490 the argument pack, we only RETURN (a single list
15492 if (purposevec
== TREE_PURPOSE (t
)
15493 && valuevec
== TREE_VALUE (t
)
15494 && chain
== TREE_CHAIN (t
))
15497 RETURN (tree_cons (purposevec
, valuevec
, chain
));
15500 /* Convert the argument vectors into a TREE_LIST */
15504 /* Grab the Ith values. */
15506 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
15509 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
15512 /* Build the list (backwards). */
15513 chain
= tree_cons (purpose
, value
, chain
);
15519 purpose
= TREE_PURPOSE (t
);
15521 purpose
= RECUR (purpose
);
15522 value
= TREE_VALUE (t
);
15524 value
= RECUR (value
);
15525 chain
= TREE_CHAIN (t
);
15526 if (chain
&& chain
!= void_type_node
)
15527 chain
= RECUR (chain
);
15528 if (purpose
== TREE_PURPOSE (t
)
15529 && value
== TREE_VALUE (t
)
15530 && chain
== TREE_CHAIN (t
))
15532 RETURN (tree_cons (purpose
, value
, chain
));
15535 case COMPONENT_REF
:
15542 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
15543 args
, complain
, in_decl
);
15544 /* Remember that there was a reference to this entity. */
15545 if (DECL_P (object
)
15546 && !mark_used (object
, complain
) && !(complain
& tf_error
))
15547 RETURN (error_mark_node
);
15548 object_type
= TREE_TYPE (object
);
15550 member
= TREE_OPERAND (t
, 1);
15551 if (BASELINK_P (member
))
15552 member
= tsubst_baselink (member
,
15553 non_reference (TREE_TYPE (object
)),
15554 args
, complain
, in_decl
);
15556 member
= tsubst_copy (member
, args
, complain
, in_decl
);
15557 if (member
== error_mark_node
)
15558 RETURN (error_mark_node
);
15560 if (type_dependent_expression_p (object
))
15561 /* We can't do much here. */;
15562 else if (!CLASS_TYPE_P (object_type
))
15564 if (scalarish_type_p (object_type
))
15566 tree s
= NULL_TREE
;
15567 tree dtor
= member
;
15569 if (TREE_CODE (dtor
) == SCOPE_REF
)
15571 s
= TREE_OPERAND (dtor
, 0);
15572 dtor
= TREE_OPERAND (dtor
, 1);
15574 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
15576 dtor
= TREE_OPERAND (dtor
, 0);
15578 RETURN (finish_pseudo_destructor_expr
15579 (object
, s
, dtor
, input_location
));
15583 else if (TREE_CODE (member
) == SCOPE_REF
15584 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
15586 /* Lookup the template functions now that we know what the
15588 tree scope
= TREE_OPERAND (member
, 0);
15589 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
15590 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
15591 member
= lookup_qualified_name (scope
, tmpl
,
15592 /*is_type_p=*/false,
15593 /*complain=*/false);
15594 if (BASELINK_P (member
))
15596 BASELINK_FUNCTIONS (member
)
15597 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
15599 member
= (adjust_result_of_qualified_name_lookup
15600 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
15605 qualified_name_lookup_error (scope
, tmpl
, member
,
15607 RETURN (error_mark_node
);
15610 else if (TREE_CODE (member
) == SCOPE_REF
15611 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
15612 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
15614 if (complain
& tf_error
)
15616 if (TYPE_P (TREE_OPERAND (member
, 0)))
15617 error ("%qT is not a class or namespace",
15618 TREE_OPERAND (member
, 0));
15620 error ("%qD is not a class or namespace",
15621 TREE_OPERAND (member
, 0));
15623 RETURN (error_mark_node
);
15625 else if (TREE_CODE (member
) == FIELD_DECL
)
15627 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
15628 if (TREE_CODE (r
) == COMPONENT_REF
)
15629 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
15633 r
= finish_class_member_access_expr (object
, member
,
15634 /*template_p=*/false,
15636 if (TREE_CODE (r
) == COMPONENT_REF
)
15637 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
15642 RETURN (build_throw
15643 (RECUR (TREE_OPERAND (t
, 0))));
15647 vec
<constructor_elt
, va_gc
> *n
;
15648 constructor_elt
*ce
;
15649 unsigned HOST_WIDE_INT idx
;
15650 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15651 bool process_index_p
;
15653 bool need_copy_p
= false;
15656 if (type
== error_mark_node
)
15657 RETURN (error_mark_node
);
15659 /* digest_init will do the wrong thing if we let it. */
15660 if (type
&& TYPE_PTRMEMFUNC_P (type
))
15663 /* We do not want to process the index of aggregate
15664 initializers as they are identifier nodes which will be
15665 looked up by digest_init. */
15666 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
15668 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
15669 newlen
= vec_safe_length (n
);
15670 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
15672 if (ce
->index
&& process_index_p
15673 /* An identifier index is looked up in the type
15674 being initialized, not the current scope. */
15675 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
15676 ce
->index
= RECUR (ce
->index
);
15678 if (PACK_EXPANSION_P (ce
->value
))
15680 /* Substitute into the pack expansion. */
15681 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
15684 if (ce
->value
== error_mark_node
15685 || PACK_EXPANSION_P (ce
->value
))
15687 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
15688 /* Just move the argument into place. */
15689 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
15692 /* Update the length of the final CONSTRUCTOR
15693 arguments vector, and note that we will need to
15695 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
15696 need_copy_p
= true;
15700 ce
->value
= RECUR (ce
->value
);
15705 vec
<constructor_elt
, va_gc
> *old_n
= n
;
15707 vec_alloc (n
, newlen
);
15708 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
15710 if (TREE_CODE (ce
->value
) == TREE_VEC
)
15712 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
15713 for (i
= 0; i
< len
; ++i
)
15714 CONSTRUCTOR_APPEND_ELT (n
, 0,
15715 TREE_VEC_ELT (ce
->value
, i
));
15718 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
15722 r
= build_constructor (init_list_type_node
, n
);
15723 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
15725 if (TREE_HAS_CONSTRUCTOR (t
))
15726 RETURN (finish_compound_literal (type
, r
, complain
));
15728 TREE_TYPE (r
) = type
;
15734 tree operand_0
= TREE_OPERAND (t
, 0);
15735 if (TYPE_P (operand_0
))
15737 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
15738 RETURN (get_typeid (operand_0
, complain
));
15742 operand_0
= RECUR (operand_0
);
15743 RETURN (build_typeid (operand_0
, complain
));
15750 else if (DECL_PACK_P (t
))
15752 /* We don't build decls for an instantiation of a
15753 variadic capture proxy, we instantiate the elements
15755 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
15756 return RECUR (DECL_VALUE_EXPR (t
));
15762 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
15763 /* ??? We're doing a subset of finish_id_expression here. */
15765 && !processing_template_decl
15766 && !cp_unevaluated_operand
15767 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
15768 && CP_DECL_THREAD_LOCAL_P (r
))
15770 if (tree wrap
= get_tls_wrapper_fn (r
))
15771 /* Replace an evaluated use of the thread_local variable with
15772 a call to its wrapper. */
15773 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
15775 else if (outer_automatic_var_p (r
))
15777 r
= process_outer_var_ref (r
, complain
);
15778 if (is_capture_proxy (r
))
15779 register_local_specialization (r
, t
);
15782 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
15783 /* If the original type was a reference, we'll be wrapped in
15784 the appropriate INDIRECT_REF. */
15785 r
= convert_from_reference (r
);
15791 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15792 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15793 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
15796 case OFFSETOF_EXPR
:
15797 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t
, 0)),
15798 EXPR_LOCATION (t
)));
15802 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
15803 complain
, in_decl
);
15805 tree type2
= TRAIT_EXPR_TYPE2 (t
);
15806 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
15807 type2
= RECUR (type2
);
15809 type2
= tsubst (type2
, args
, complain
, in_decl
);
15811 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
15816 tree old_stmt_expr
= cur_stmt_expr
;
15817 tree stmt_expr
= begin_stmt_expr ();
15819 cur_stmt_expr
= stmt_expr
;
15820 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
15821 integral_constant_expression_p
);
15822 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
15823 cur_stmt_expr
= old_stmt_expr
;
15825 /* If the resulting list of expression statement is empty,
15826 fold it further into void_node. */
15827 if (empty_expr_stmt_p (stmt_expr
))
15828 stmt_expr
= void_node
;
15830 RETURN (stmt_expr
);
15835 tree r
= build_lambda_expr ();
15837 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
15838 LAMBDA_EXPR_CLOSURE (r
) = type
;
15839 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
15841 LAMBDA_EXPR_LOCATION (r
)
15842 = LAMBDA_EXPR_LOCATION (t
);
15843 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
15844 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
15845 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
15846 LAMBDA_EXPR_DISCRIMINATOR (r
)
15847 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
15848 /* For a function scope, we want to use tsubst so that we don't
15849 complain about referring to an auto function before its return
15850 type has been deduced. Otherwise, we want to use tsubst_copy so
15851 that we look up the existing field/parameter/variable rather
15852 than build a new one. */
15853 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
15854 if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
15855 scope
= tsubst (scope
, args
, complain
, in_decl
);
15856 else if (scope
&& TREE_CODE (scope
) == PARM_DECL
)
15858 /* Look up the parameter we want directly, as tsubst_copy
15859 doesn't do what we need. */
15860 tree fn
= tsubst (DECL_CONTEXT (scope
), args
, complain
, in_decl
);
15861 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
15862 while (DECL_PARM_INDEX (parm
) != DECL_PARM_INDEX (scope
))
15863 parm
= DECL_CHAIN (parm
);
15865 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15866 if (DECL_CONTEXT (scope
) == NULL_TREE
)
15867 DECL_CONTEXT (scope
) = fn
;
15870 scope
= RECUR (scope
);
15871 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
15872 LAMBDA_EXPR_RETURN_TYPE (r
)
15873 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
15875 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
15876 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
15878 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15879 determine_visibility (TYPE_NAME (type
));
15880 /* Now that we know visibility, instantiate the type so we have a
15881 declaration of the op() for later calls to lambda_function. */
15882 complete_type (type
);
15884 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
15886 RETURN (build_lambda_object (r
));
15890 /* We can get here for a constant initializer of non-dependent type.
15891 FIXME stop folding in cp_parser_initializer_clause. */
15893 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
15898 case TRANSACTION_EXPR
:
15899 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
15900 integral_constant_expression_p
));
15903 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
15905 case VEC_PERM_EXPR
:
15907 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15908 tree op1
= RECUR (TREE_OPERAND (t
, 1));
15909 tree op2
= RECUR (TREE_OPERAND (t
, 2));
15910 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
15915 /* Handle Objective-C++ constructs, if appropriate. */
15918 = objcp_tsubst_copy_and_build (t
, args
, complain
,
15919 in_decl
, /*function_p=*/false);
15923 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
15929 input_location
= loc
;
15933 /* Verify that the instantiated ARGS are valid. For type arguments,
15934 make sure that the type's linkage is ok. For non-type arguments,
15935 make sure they are constants if they are integral or enumerations.
15936 Emit an error under control of COMPLAIN, and return TRUE on error. */
15939 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
15941 if (dependent_template_arg_p (t
))
15943 if (ARGUMENT_PACK_P (t
))
15945 tree vec
= ARGUMENT_PACK_ARGS (t
);
15946 int len
= TREE_VEC_LENGTH (vec
);
15947 bool result
= false;
15950 for (i
= 0; i
< len
; ++i
)
15951 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
15955 else if (TYPE_P (t
))
15957 /* [basic.link]: A name with no linkage (notably, the name
15958 of a class or enumeration declared in a local scope)
15959 shall not be used to declare an entity with linkage.
15960 This implies that names with no linkage cannot be used as
15963 DR 757 relaxes this restriction for C++0x. */
15964 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
15965 : no_linkage_check (t
, /*relaxed_p=*/false));
15969 /* DR 488 makes use of a type with no linkage cause
15970 type deduction to fail. */
15971 if (complain
& tf_error
)
15973 if (TYPE_ANONYMOUS_P (nt
))
15974 error ("%qT is/uses anonymous type", t
);
15976 error ("template argument for %qD uses local type %qT",
15981 /* In order to avoid all sorts of complications, we do not
15982 allow variably-modified types as template arguments. */
15983 else if (variably_modified_type_p (t
, NULL_TREE
))
15985 if (complain
& tf_error
)
15986 error ("%qT is a variably modified type", t
);
15990 /* Class template and alias template arguments should be OK. */
15991 else if (DECL_TYPE_TEMPLATE_P (t
))
15993 /* A non-type argument of integral or enumerated type must be a
15995 else if (TREE_TYPE (t
)
15996 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
15997 && !REFERENCE_REF_P (t
)
15998 && !TREE_CONSTANT (t
))
16000 if (complain
& tf_error
)
16001 error ("integral expression %qE is not constant", t
);
16008 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
16010 int ix
, len
= DECL_NTPARMS (tmpl
);
16011 bool result
= false;
16013 for (ix
= 0; ix
!= len
; ix
++)
16015 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
16018 if (result
&& (complain
& tf_error
))
16019 error (" trying to instantiate %qD", tmpl
);
16023 /* We're out of SFINAE context now, so generate diagnostics for the access
16024 errors we saw earlier when instantiating D from TMPL and ARGS. */
16027 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
16029 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
16030 tree type
= TREE_TYPE (pattern
);
16031 location_t loc
= input_location
;
16033 push_access_scope (d
);
16034 push_deferring_access_checks (dk_no_deferred
);
16035 input_location
= DECL_SOURCE_LOCATION (pattern
);
16036 tsubst (type
, args
, tf_warning_or_error
, d
);
16037 input_location
= loc
;
16038 pop_deferring_access_checks ();
16039 pop_access_scope (d
);
16042 /* Instantiate the indicated variable, function, or alias template TMPL with
16043 the template arguments in TARG_PTR. */
16046 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
16048 tree targ_ptr
= orig_args
;
16052 bool access_ok
= true;
16054 if (tmpl
== error_mark_node
)
16055 return error_mark_node
;
16057 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
16059 /* If this function is a clone, handle it specially. */
16060 if (DECL_CLONED_FUNCTION_P (tmpl
))
16065 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
16066 DECL_CLONED_FUNCTION. */
16067 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
16068 targ_ptr
, complain
);
16069 if (spec
== error_mark_node
)
16070 return error_mark_node
;
16072 /* Look for the clone. */
16073 FOR_EACH_CLONE (clone
, spec
)
16074 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
16076 /* We should always have found the clone by now. */
16077 gcc_unreachable ();
16081 if (targ_ptr
== error_mark_node
)
16082 return error_mark_node
;
16084 /* Check to see if we already have this specialization. */
16085 gen_tmpl
= most_general_template (tmpl
);
16086 if (tmpl
!= gen_tmpl
)
16087 /* The TMPL is a partial instantiation. To get a full set of
16088 arguments we must add the arguments used to perform the
16089 partial instantiation. */
16090 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
16093 /* It would be nice to avoid hashing here and then again in tsubst_decl,
16094 but it doesn't seem to be on the hot path. */
16095 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
16097 gcc_assert (tmpl
== gen_tmpl
16098 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
16100 || fndecl
== NULL_TREE
);
16102 if (spec
!= NULL_TREE
)
16104 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
16106 if (complain
& tf_error
)
16107 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
16108 return error_mark_node
;
16113 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
16115 return error_mark_node
;
16117 /* We are building a FUNCTION_DECL, during which the access of its
16118 parameters and return types have to be checked. However this
16119 FUNCTION_DECL which is the desired context for access checking
16120 is not built yet. We solve this chicken-and-egg problem by
16121 deferring all checks until we have the FUNCTION_DECL. */
16122 push_deferring_access_checks (dk_deferred
);
16124 /* Instantiation of the function happens in the context of the function
16125 template, not the context of the overload resolution we're doing. */
16126 push_to_top_level ();
16127 /* If there are dependent arguments, e.g. because we're doing partial
16128 ordering, make sure processing_template_decl stays set. */
16129 if (uses_template_parms (targ_ptr
))
16130 ++processing_template_decl
;
16131 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
16133 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
16134 complain
, gen_tmpl
, true);
16135 push_nested_class (ctx
);
16138 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
16140 if (VAR_P (pattern
))
16142 /* We need to determine if we're using a partial or explicit
16143 specialization now, because the type of the variable could be
16145 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
16146 tree elt
= most_specialized_partial_spec (tid
, complain
);
16147 if (elt
== error_mark_node
)
16148 pattern
= error_mark_node
;
16151 tmpl
= TREE_VALUE (elt
);
16152 pattern
= DECL_TEMPLATE_RESULT (tmpl
);
16153 targ_ptr
= TREE_PURPOSE (elt
);
16157 /* Substitute template parameters to obtain the specialization. */
16158 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
16159 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
16160 pop_nested_class ();
16161 pop_from_top_level ();
16163 if (fndecl
== error_mark_node
)
16165 pop_deferring_access_checks ();
16166 return error_mark_node
;
16169 /* The DECL_TI_TEMPLATE should always be the immediate parent
16170 template, not the most general template. */
16171 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
16172 DECL_TI_ARGS (fndecl
) = targ_ptr
;
16174 /* Now we know the specialization, compute access previously
16176 push_access_scope (fndecl
);
16177 if (!perform_deferred_access_checks (complain
))
16179 pop_access_scope (fndecl
);
16180 pop_deferring_access_checks ();
16182 /* If we've just instantiated the main entry point for a function,
16183 instantiate all the alternate entry points as well. We do this
16184 by cloning the instantiation of the main entry point, not by
16185 instantiating the template clones. */
16186 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
16187 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
16191 if (!(complain
& tf_error
))
16193 /* Remember to reinstantiate when we're out of SFINAE so the user
16194 can see the errors. */
16195 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
16197 return error_mark_node
;
16202 /* Wrapper for instantiate_template_1. */
16205 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
16208 timevar_push (TV_TEMPLATE_INST
);
16209 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
16210 timevar_pop (TV_TEMPLATE_INST
);
16214 /* Instantiate the alias template TMPL with ARGS. Also push a template
16215 instantiation level, which instantiate_template doesn't do because
16216 functions and variables have sufficient context established by the
16220 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
16222 struct pending_template
*old_last_pend
= last_pending_template
;
16223 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
16224 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
16225 return error_mark_node
;
16226 tree tinst
= build_tree_list (tmpl
, args
);
16227 if (!push_tinst_level (tinst
))
16230 return error_mark_node
;
16234 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
16235 args
, tmpl
, complain
,
16236 /*require_all_args=*/true,
16237 /*use_default_args=*/true);
16239 tree r
= instantiate_template (tmpl
, args
, complain
);
16240 pop_tinst_level ();
16241 /* We can't free this if a pending_template entry or last_error_tinst_level
16242 is pointing at it. */
16243 if (last_pending_template
== old_last_pend
16244 && last_error_tinst_level
== old_error_tinst
)
16250 /* PARM is a template parameter pack for FN. Returns true iff
16251 PARM is used in a deducible way in the argument list of FN. */
16254 pack_deducible_p (tree parm
, tree fn
)
16256 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
16257 for (; t
; t
= TREE_CHAIN (t
))
16259 tree type
= TREE_VALUE (t
);
16261 if (!PACK_EXPANSION_P (type
))
16263 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
16264 packs
; packs
= TREE_CHAIN (packs
))
16265 if (template_args_equal (TREE_VALUE (packs
), parm
))
16267 /* The template parameter pack is used in a function parameter
16268 pack. If this is the end of the parameter list, the
16269 template parameter pack is deducible. */
16270 if (TREE_CHAIN (t
) == void_list_node
)
16273 /* Otherwise, not. Well, it could be deduced from
16274 a non-pack parameter, but doing so would end up with
16275 a deduction mismatch, so don't bother. */
16279 /* The template parameter pack isn't used in any function parameter
16280 packs, but it might be used deeper, e.g. tuple<Args...>. */
16284 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
16285 NARGS elements of the arguments that are being used when calling
16286 it. TARGS is a vector into which the deduced template arguments
16289 Returns either a FUNCTION_DECL for the matching specialization of FN or
16290 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
16291 true, diagnostics will be printed to explain why it failed.
16293 If FN is a conversion operator, or we are trying to produce a specific
16294 specialization, RETURN_TYPE is the return type desired.
16296 The EXPLICIT_TARGS are explicit template arguments provided via a
16299 The parameter STRICT is one of:
16302 We are deducing arguments for a function call, as in
16303 [temp.deduct.call].
16306 We are deducing arguments for a conversion function, as in
16307 [temp.deduct.conv].
16310 We are deducing arguments when doing an explicit instantiation
16311 as in [temp.explicit], when determining an explicit specialization
16312 as in [temp.expl.spec], or when taking the address of a function
16313 template, as in [temp.deduct.funcaddr]. */
16316 fn_type_unification (tree fn
,
16317 tree explicit_targs
,
16320 unsigned int nargs
,
16322 unification_kind_t strict
,
16329 tree decl
= NULL_TREE
;
16330 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
16332 static int deduction_depth
;
16333 struct pending_template
*old_last_pend
= last_pending_template
;
16334 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
16335 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
16337 tree r
= error_mark_node
;
16340 complain
|= tf_decltype
;
16342 /* In C++0x, it's possible to have a function template whose type depends
16343 on itself recursively. This is most obvious with decltype, but can also
16344 occur with enumeration scope (c++/48969). So we need to catch infinite
16345 recursion and reject the substitution at deduction time; this function
16346 will return error_mark_node for any repeated substitution.
16348 This also catches excessive recursion such as when f<N> depends on
16349 f<N-1> across all integers, and returns error_mark_node for all the
16350 substitutions back up to the initial one.
16352 This is, of course, not reentrant. */
16353 if (excessive_deduction_depth
)
16354 return error_mark_node
;
16355 tinst
= build_tree_list (fn
, NULL_TREE
);
16358 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
16360 fntype
= TREE_TYPE (fn
);
16361 if (explicit_targs
)
16365 The specified template arguments must match the template
16366 parameters in kind (i.e., type, nontype, template), and there
16367 must not be more arguments than there are parameters;
16368 otherwise type deduction fails.
16370 Nontype arguments must match the types of the corresponding
16371 nontype template parameters, or must be convertible to the
16372 types of the corresponding nontype parameters as specified in
16373 _temp.arg.nontype_, otherwise type deduction fails.
16375 All references in the function type of the function template
16376 to the corresponding template parameters are replaced by the
16377 specified template argument values. If a substitution in a
16378 template parameter or in the function type of the function
16379 template results in an invalid type, type deduction fails. */
16380 int i
, len
= TREE_VEC_LENGTH (tparms
);
16381 location_t loc
= input_location
;
16382 bool incomplete
= false;
16384 /* Adjust any explicit template arguments before entering the
16385 substitution context. */
16387 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
16389 /*require_all_args=*/false,
16390 /*use_default_args=*/false));
16391 if (explicit_targs
== error_mark_node
)
16394 /* Substitute the explicit args into the function type. This is
16395 necessary so that, for instance, explicitly declared function
16396 arguments can match null pointed constants. If we were given
16397 an incomplete set of explicit args, we must not do semantic
16398 processing during substitution as we could create partial
16400 for (i
= 0; i
< len
; i
++)
16402 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
16403 bool parameter_pack
= false;
16404 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
16406 /* Dig out the actual parm. */
16407 if (TREE_CODE (parm
) == TYPE_DECL
16408 || TREE_CODE (parm
) == TEMPLATE_DECL
)
16410 parm
= TREE_TYPE (parm
);
16411 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
16413 else if (TREE_CODE (parm
) == PARM_DECL
)
16415 parm
= DECL_INITIAL (parm
);
16416 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
16419 if (!parameter_pack
&& targ
== NULL_TREE
)
16420 /* No explicit argument for this template parameter. */
16423 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
16425 /* Mark the argument pack as "incomplete". We could
16426 still deduce more arguments during unification.
16427 We remove this mark in type_unification_real. */
16430 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
16431 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
16432 = ARGUMENT_PACK_ARGS (targ
);
16435 /* We have some incomplete argument packs. */
16440 TREE_VALUE (tinst
) = explicit_targs
;
16441 if (!push_tinst_level (tinst
))
16443 excessive_deduction_depth
= true;
16446 processing_template_decl
+= incomplete
;
16447 input_location
= DECL_SOURCE_LOCATION (fn
);
16448 /* Ignore any access checks; we'll see them again in
16449 instantiate_template and they might have the wrong
16450 access path at this point. */
16451 push_deferring_access_checks (dk_deferred
);
16452 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
16453 complain
| tf_partial
, NULL_TREE
);
16454 pop_deferring_access_checks ();
16455 input_location
= loc
;
16456 processing_template_decl
-= incomplete
;
16457 pop_tinst_level ();
16459 if (fntype
== error_mark_node
)
16462 /* Place the explicitly specified arguments in TARGS. */
16463 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
16464 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
16467 /* Never do unification on the 'this' parameter. */
16468 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
16474 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
16475 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
16476 new_args
[0] = return_type
;
16477 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
16482 /* We allow incomplete unification without an error message here
16483 because the standard doesn't seem to explicitly prohibit it. Our
16484 callers must be ready to deal with unification failures in any
16487 TREE_VALUE (tinst
) = targs
;
16488 /* If we aren't explaining yet, push tinst context so we can see where
16489 any errors (e.g. from class instantiations triggered by instantiation
16490 of default template arguments) come from. If we are explaining, this
16491 context is redundant. */
16492 if (!explain_p
&& !push_tinst_level (tinst
))
16494 excessive_deduction_depth
= true;
16498 /* type_unification_real will pass back any access checks from default
16499 template argument substitution. */
16500 vec
<deferred_access_check
, va_gc
> *checks
;
16503 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
16504 targs
, parms
, args
, nargs
, /*subr=*/0,
16505 strict
, flags
, &checks
, explain_p
);
16507 pop_tinst_level ();
16511 /* Now that we have bindings for all of the template arguments,
16512 ensure that the arguments deduced for the template template
16513 parameters have compatible template parameter lists. We cannot
16514 check this property before we have deduced all template
16515 arguments, because the template parameter types of a template
16516 template parameter might depend on prior template parameters
16517 deduced after the template template parameter. The following
16518 ill-formed example illustrates this issue:
16520 template<typename T, template<T> class C> void f(C<5>, T);
16522 template<int N> struct X {};
16525 f(X<5>(), 5l); // error: template argument deduction fails
16528 The template parameter list of 'C' depends on the template type
16529 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
16530 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
16531 time that we deduce 'C'. */
16532 if (!template_template_parm_bindings_ok_p
16533 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
16535 unify_inconsistent_template_template_parameters (explain_p
);
16539 /* All is well so far. Now, check:
16543 When all template arguments have been deduced, all uses of
16544 template parameters in nondeduced contexts are replaced with
16545 the corresponding deduced argument values. If the
16546 substitution results in an invalid type, as described above,
16547 type deduction fails. */
16548 TREE_VALUE (tinst
) = targs
;
16549 if (!push_tinst_level (tinst
))
16551 excessive_deduction_depth
= true;
16555 /* Also collect access checks from the instantiation. */
16556 reopen_deferring_access_checks (checks
);
16558 decl
= instantiate_template (fn
, targs
, complain
);
16560 checks
= get_deferred_access_checks ();
16561 pop_deferring_access_checks ();
16563 pop_tinst_level ();
16565 if (decl
== error_mark_node
)
16568 /* Now perform any access checks encountered during substitution. */
16569 push_access_scope (decl
);
16570 ok
= perform_access_checks (checks
, complain
);
16571 pop_access_scope (decl
);
16575 /* If we're looking for an exact match, check that what we got
16576 is indeed an exact match. It might not be if some template
16577 parameters are used in non-deduced contexts. But don't check
16578 for an exact match if we have dependent template arguments;
16579 in that case we're doing partial ordering, and we already know
16580 that we have two candidates that will provide the actual type. */
16581 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
16583 tree substed
= TREE_TYPE (decl
);
16587 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
16589 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
16590 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
16591 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
16593 unify_type_mismatch (explain_p
, args
[i
],
16594 TREE_VALUE (sarg
));
16603 if (excessive_deduction_depth
)
16605 if (deduction_depth
== 0)
16606 /* Reset once we're all the way out. */
16607 excessive_deduction_depth
= false;
16610 /* We can't free this if a pending_template entry or last_error_tinst_level
16611 is pointing at it. */
16612 if (last_pending_template
== old_last_pend
16613 && last_error_tinst_level
== old_error_tinst
)
16619 /* Adjust types before performing type deduction, as described in
16620 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
16621 sections are symmetric. PARM is the type of a function parameter
16622 or the return type of the conversion function. ARG is the type of
16623 the argument passed to the call, or the type of the value
16624 initialized with the result of the conversion function.
16625 ARG_EXPR is the original argument expression, which may be null. */
16628 maybe_adjust_types_for_deduction (unification_kind_t strict
,
16641 /* Swap PARM and ARG throughout the remainder of this
16642 function; the handling is precisely symmetric since PARM
16643 will initialize ARG rather than vice versa. */
16644 std::swap (parm
, arg
);
16648 /* Core issue #873: Do the DR606 thing (see below) for these cases,
16649 too, but here handle it by stripping the reference from PARM
16650 rather than by adding it to ARG. */
16651 if (TREE_CODE (*parm
) == REFERENCE_TYPE
16652 && TYPE_REF_IS_RVALUE (*parm
)
16653 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
16654 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
16655 && TREE_CODE (*arg
) == REFERENCE_TYPE
16656 && !TYPE_REF_IS_RVALUE (*arg
))
16657 *parm
= TREE_TYPE (*parm
);
16658 /* Nothing else to do in this case. */
16662 gcc_unreachable ();
16665 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
16667 /* [temp.deduct.call]
16669 If P is not a reference type:
16671 --If A is an array type, the pointer type produced by the
16672 array-to-pointer standard conversion (_conv.array_) is
16673 used in place of A for type deduction; otherwise,
16675 --If A is a function type, the pointer type produced by
16676 the function-to-pointer standard conversion
16677 (_conv.func_) is used in place of A for type deduction;
16680 --If A is a cv-qualified type, the top level
16681 cv-qualifiers of A's type are ignored for type
16683 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
16684 *arg
= build_pointer_type (TREE_TYPE (*arg
));
16685 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
16686 *arg
= build_pointer_type (*arg
);
16688 *arg
= TYPE_MAIN_VARIANT (*arg
);
16691 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
16692 of the form T&&, where T is a template parameter, and the argument
16693 is an lvalue, T is deduced as A& */
16694 if (TREE_CODE (*parm
) == REFERENCE_TYPE
16695 && TYPE_REF_IS_RVALUE (*parm
)
16696 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
16697 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
16698 && (arg_expr
? real_lvalue_p (arg_expr
)
16699 /* try_one_overload doesn't provide an arg_expr, but
16700 functions are always lvalues. */
16701 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
16702 *arg
= build_reference_type (*arg
);
16704 /* [temp.deduct.call]
16706 If P is a cv-qualified type, the top level cv-qualifiers
16707 of P's type are ignored for type deduction. If P is a
16708 reference type, the type referred to by P is used for
16710 *parm
= TYPE_MAIN_VARIANT (*parm
);
16711 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
16713 *parm
= TREE_TYPE (*parm
);
16714 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
16717 /* DR 322. For conversion deduction, remove a reference type on parm
16718 too (which has been swapped into ARG). */
16719 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
16720 *arg
= TREE_TYPE (*arg
);
16725 /* Subroutine of unify_one_argument. PARM is a function parameter of a
16726 template which does contain any deducible template parameters; check if
16727 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
16728 unify_one_argument. */
16731 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
16732 int flags
, bool explain_p
)
16737 type
= TREE_TYPE (arg
);
16741 if (same_type_p (parm
, type
))
16742 return unify_success (explain_p
);
16744 if (strict
== DEDUCE_CONV
)
16746 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
16747 explain_p
? tf_warning_or_error
: tf_none
))
16748 return unify_success (explain_p
);
16750 else if (strict
!= DEDUCE_EXACT
)
16752 if (can_convert_arg (parm
, type
,
16753 TYPE_P (arg
) ? NULL_TREE
: arg
,
16754 flags
, explain_p
? tf_warning_or_error
: tf_none
))
16755 return unify_success (explain_p
);
16758 if (strict
== DEDUCE_EXACT
)
16759 return unify_type_mismatch (explain_p
, parm
, arg
);
16761 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
16764 static bool uses_deducible_template_parms (tree type
);
16766 /* Returns true iff the expression EXPR is one from which a template
16767 argument can be deduced. In other words, if it's an undecorated
16768 use of a template non-type parameter. */
16771 deducible_expression (tree expr
)
16773 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
16776 /* Returns true iff the array domain DOMAIN uses a template parameter in a
16777 deducible way; that is, if it has a max value of <PARM> - 1. */
16780 deducible_array_bound (tree domain
)
16782 if (domain
== NULL_TREE
)
16785 tree max
= TYPE_MAX_VALUE (domain
);
16786 if (TREE_CODE (max
) != MINUS_EXPR
)
16789 return deducible_expression (TREE_OPERAND (max
, 0));
16792 /* Returns true iff the template arguments ARGS use a template parameter
16793 in a deducible way. */
16796 deducible_template_args (tree args
)
16798 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
16801 tree elt
= TREE_VEC_ELT (args
, i
);
16802 if (ARGUMENT_PACK_P (elt
))
16803 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
16806 if (PACK_EXPANSION_P (elt
))
16807 elt
= PACK_EXPANSION_PATTERN (elt
);
16808 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
16810 else if (TYPE_P (elt
))
16811 deducible
= uses_deducible_template_parms (elt
);
16813 deducible
= deducible_expression (elt
);
16821 /* Returns true iff TYPE contains any deducible references to template
16822 parameters, as per 14.8.2.5. */
16825 uses_deducible_template_parms (tree type
)
16827 if (PACK_EXPANSION_P (type
))
16828 type
= PACK_EXPANSION_PATTERN (type
);
16835 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
16836 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16842 if (POINTER_TYPE_P (type
))
16843 return uses_deducible_template_parms (TREE_TYPE (type
));
16845 /* T[integer-constant ]
16847 if (TREE_CODE (type
) == ARRAY_TYPE
)
16848 return (uses_deducible_template_parms (TREE_TYPE (type
))
16849 || deducible_array_bound (TYPE_DOMAIN (type
)));
16861 if (TYPE_PTRMEM_P (type
))
16862 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
16863 || (uses_deducible_template_parms
16864 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
16866 /* template-name <T> (where template-name refers to a class template)
16867 template-name <i> (where template-name refers to a class template) */
16868 if (CLASS_TYPE_P (type
)
16869 && CLASSTYPE_TEMPLATE_INFO (type
)
16870 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
16871 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
16872 (CLASSTYPE_TI_ARGS (type
)));
16877 if (TREE_CODE (type
) == FUNCTION_TYPE
16878 || TREE_CODE (type
) == METHOD_TYPE
)
16880 if (uses_deducible_template_parms (TREE_TYPE (type
)))
16882 tree parm
= TYPE_ARG_TYPES (type
);
16883 if (TREE_CODE (type
) == METHOD_TYPE
)
16884 parm
= TREE_CHAIN (parm
);
16885 for (; parm
; parm
= TREE_CHAIN (parm
))
16886 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
16893 /* Subroutine of type_unification_real and unify_pack_expansion to
16894 handle unification of a single P/A pair. Parameters are as
16895 for those functions. */
16898 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
16899 int subr
, unification_kind_t strict
,
16902 tree arg_expr
= NULL_TREE
;
16905 if (arg
== error_mark_node
|| parm
== error_mark_node
)
16906 return unify_invalid (explain_p
);
16907 if (arg
== unknown_type_node
)
16908 /* We can't deduce anything from this, but we might get all the
16909 template args from other function args. */
16910 return unify_success (explain_p
);
16912 /* Implicit conversions (Clause 4) will be performed on a function
16913 argument to convert it to the type of the corresponding function
16914 parameter if the parameter type contains no template-parameters that
16915 participate in template argument deduction. */
16916 if (strict
!= DEDUCE_EXACT
16917 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
16918 /* For function parameters with no deducible template parameters,
16919 just return. We'll check non-dependent conversions later. */
16920 return unify_success (explain_p
);
16925 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
16926 | UNIFY_ALLOW_MORE_CV_QUAL
16927 | UNIFY_ALLOW_DERIVED
);
16931 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
16935 arg_strict
= UNIFY_ALLOW_NONE
;
16939 gcc_unreachable ();
16942 /* We only do these transformations if this is the top-level
16943 parameter_type_list in a call or declaration matching; in other
16944 situations (nested function declarators, template argument lists) we
16945 won't be comparing a type to an expression, and we don't do any type
16951 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
16952 if (type_unknown_p (arg
))
16954 /* [temp.deduct.type] A template-argument can be
16955 deduced from a pointer to function or pointer
16956 to member function argument if the set of
16957 overloaded functions does not contain function
16958 templates and at most one of a set of
16959 overloaded functions provides a unique
16962 if (resolve_overloaded_unification
16963 (tparms
, targs
, parm
, arg
, strict
,
16964 arg_strict
, explain_p
))
16965 return unify_success (explain_p
);
16966 return unify_overload_resolution_failure (explain_p
, arg
);
16970 arg
= unlowered_expr_type (arg
);
16971 if (arg
== error_mark_node
)
16972 return unify_invalid (explain_p
);
16976 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
16979 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
16980 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
16981 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16983 /* For deduction from an init-list we need the actual list. */
16984 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
16986 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
16989 /* Most parms like fn_type_unification.
16991 If SUBR is 1, we're being called recursively (to unify the
16992 arguments of a function or method parameter of a function
16995 CHECKS is a pointer to a vector of access checks encountered while
16996 substituting default template arguments. */
16999 type_unification_real (tree tparms
,
17003 unsigned int xnargs
,
17005 unification_kind_t strict
,
17007 vec
<deferred_access_check
, va_gc
> **checks
,
17012 int ntparms
= TREE_VEC_LENGTH (tparms
);
17013 int saw_undeduced
= 0;
17016 unsigned int nargs
;
17019 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
17020 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
17021 gcc_assert (ntparms
> 0);
17023 /* Reset the number of non-defaulted template arguments contained
17025 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
17033 while (parms
&& parms
!= void_list_node
17036 parm
= TREE_VALUE (parms
);
17038 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
17039 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
17040 /* For a function parameter pack that occurs at the end of the
17041 parameter-declaration-list, the type A of each remaining
17042 argument of the call is compared with the type P of the
17043 declarator-id of the function parameter pack. */
17046 parms
= TREE_CHAIN (parms
);
17048 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
17049 /* For a function parameter pack that does not occur at the
17050 end of the parameter-declaration-list, the type of the
17051 parameter pack is a non-deduced context. */
17057 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
17063 && parms
!= void_list_node
17064 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
17066 /* Unify the remaining arguments with the pack expansion type. */
17068 tree parmvec
= make_tree_vec (1);
17070 /* Allocate a TREE_VEC and copy in all of the arguments */
17071 argvec
= make_tree_vec (nargs
- ia
);
17072 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
17073 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
17075 /* Copy the parameter into parmvec. */
17076 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
17077 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
17078 /*subr=*/subr
, explain_p
))
17081 /* Advance to the end of the list of parameters. */
17082 parms
= TREE_CHAIN (parms
);
17085 /* Fail if we've reached the end of the parm list, and more args
17086 are present, and the parm list isn't variadic. */
17087 if (ia
< nargs
&& parms
== void_list_node
)
17088 return unify_too_many_arguments (explain_p
, nargs
, ia
);
17089 /* Fail if parms are left and they don't have default values and
17090 they aren't all deduced as empty packs (c++/57397). This is
17091 consistent with sufficient_parms_p. */
17092 if (parms
&& parms
!= void_list_node
17093 && TREE_PURPOSE (parms
) == NULL_TREE
)
17095 unsigned int count
= nargs
;
17100 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
17103 p
= TREE_CHAIN (p
);
17105 while (p
&& p
!= void_list_node
);
17106 if (count
!= nargs
)
17107 return unify_too_few_arguments (explain_p
, ia
, count
,
17113 tsubst_flags_t complain
= (explain_p
17114 ? tf_warning_or_error
17117 for (i
= 0; i
< ntparms
; i
++)
17119 tree targ
= TREE_VEC_ELT (targs
, i
);
17120 tree tparm
= TREE_VEC_ELT (tparms
, i
);
17122 /* Clear the "incomplete" flags on all argument packs now so that
17123 substituting them into later default arguments works. */
17124 if (targ
&& ARGUMENT_PACK_P (targ
))
17126 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
17127 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
17130 if (targ
|| tparm
== error_mark_node
)
17132 tparm
= TREE_VALUE (tparm
);
17134 /* If this is an undeduced nontype parameter that depends on
17135 a type parameter, try another pass; its type may have been
17136 deduced from a later argument than the one from which
17137 this parameter can be deduced. */
17138 if (TREE_CODE (tparm
) == PARM_DECL
17139 && uses_template_parms (TREE_TYPE (tparm
))
17140 && saw_undeduced
< 2)
17146 /* Core issue #226 (C++0x) [temp.deduct]:
17148 If a template argument has not been deduced, its
17149 default template argument, if any, is used.
17151 When we are in C++98 mode, TREE_PURPOSE will either
17152 be NULL_TREE or ERROR_MARK_NODE, so we do not need
17153 to explicitly check cxx_dialect here. */
17154 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
17155 /* OK, there is a default argument. Wait until after the
17156 conversion check to do substitution. */
17159 /* If the type parameter is a parameter pack, then it will
17160 be deduced to an empty parameter pack. */
17161 if (template_parameter_pack_p (tparm
))
17165 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
17167 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
17168 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
17169 TREE_CONSTANT (arg
) = 1;
17172 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
17174 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
17176 TREE_VEC_ELT (targs
, i
) = arg
;
17180 return unify_parameter_deduction_failure (explain_p
, tparm
);
17183 /* DR 1391: All parameters have args, now check non-dependent parms for
17185 if (saw_undeduced
< 2)
17186 for (ia
= 0, parms
= xparms
, args
= xargs
, nargs
= xnargs
;
17187 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
17189 parm
= TREE_VALUE (parms
);
17191 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
17192 && (!TREE_CHAIN (parms
)
17193 || TREE_CHAIN (parms
) == void_list_node
))
17194 /* For a function parameter pack that occurs at the end of the
17195 parameter-declaration-list, the type A of each remaining
17196 argument of the call is compared with the type P of the
17197 declarator-id of the function parameter pack. */
17200 parms
= TREE_CHAIN (parms
);
17202 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
17203 /* For a function parameter pack that does not occur at the
17204 end of the parameter-declaration-list, the type of the
17205 parameter pack is a non-deduced context. */
17211 if (uses_template_parms (parm
))
17213 if (check_non_deducible_conversion (parm
, arg
, strict
, flags
,
17218 /* Now substitute into the default template arguments. */
17219 for (i
= 0; i
< ntparms
; i
++)
17221 tree targ
= TREE_VEC_ELT (targs
, i
);
17222 tree tparm
= TREE_VEC_ELT (tparms
, i
);
17224 if (targ
|| tparm
== error_mark_node
)
17226 tree parm
= TREE_VALUE (tparm
);
17228 if (TREE_CODE (parm
) == PARM_DECL
17229 && uses_template_parms (TREE_TYPE (parm
))
17230 && saw_undeduced
< 2)
17233 tree arg
= TREE_PURPOSE (tparm
);
17234 reopen_deferring_access_checks (*checks
);
17235 location_t save_loc
= input_location
;
17237 input_location
= DECL_SOURCE_LOCATION (parm
);
17238 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
17239 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
17241 input_location
= save_loc
;
17242 *checks
= get_deferred_access_checks ();
17243 pop_deferring_access_checks ();
17244 if (arg
== error_mark_node
)
17248 TREE_VEC_ELT (targs
, i
) = arg
;
17249 /* The position of the first default template argument,
17250 is also the number of non-defaulted arguments in TARGS.
17252 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
17253 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
17258 if (saw_undeduced
++ == 1)
17261 #ifdef ENABLE_CHECKING
17262 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
17263 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
17266 return unify_success (explain_p
);
17269 /* Subroutine of type_unification_real. Args are like the variables
17270 at the call site. ARG is an overloaded function (or template-id);
17271 we try deducing template args from each of the overloads, and if
17272 only one succeeds, we go with that. Modifies TARGS and returns
17273 true on success. */
17276 resolve_overloaded_unification (tree tparms
,
17280 unification_kind_t strict
,
17284 tree tempargs
= copy_node (targs
);
17286 tree goodfn
= NULL_TREE
;
17289 if (TREE_CODE (arg
) == ADDR_EXPR
)
17291 arg
= TREE_OPERAND (arg
, 0);
17297 if (TREE_CODE (arg
) == COMPONENT_REF
)
17298 /* Handle `&x' where `x' is some static or non-static member
17300 arg
= TREE_OPERAND (arg
, 1);
17302 if (TREE_CODE (arg
) == OFFSET_REF
)
17303 arg
= TREE_OPERAND (arg
, 1);
17305 /* Strip baselink information. */
17306 if (BASELINK_P (arg
))
17307 arg
= BASELINK_FUNCTIONS (arg
);
17309 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
17311 /* If we got some explicit template args, we need to plug them into
17312 the affected templates before we try to unify, in case the
17313 explicit args will completely resolve the templates in question. */
17316 tree expl_subargs
= TREE_OPERAND (arg
, 1);
17317 arg
= TREE_OPERAND (arg
, 0);
17319 for (; arg
; arg
= OVL_NEXT (arg
))
17321 tree fn
= OVL_CURRENT (arg
);
17322 tree subargs
, elem
;
17324 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
17327 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
17328 expl_subargs
, NULL_TREE
, tf_none
,
17329 /*require_all_args=*/true,
17330 /*use_default_args=*/true);
17331 if (subargs
!= error_mark_node
17332 && !any_dependent_template_arguments_p (subargs
))
17334 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
17335 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
17336 elem
, strict
, sub_strict
, addr_p
, explain_p
)
17337 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
17346 /* If no templates (or more than one) are fully resolved by the
17347 explicit arguments, this template-id is a non-deduced context; it
17348 could still be OK if we deduce all template arguments for the
17349 enclosing call through other arguments. */
17353 else if (TREE_CODE (arg
) != OVERLOAD
17354 && TREE_CODE (arg
) != FUNCTION_DECL
)
17355 /* If ARG is, for example, "(0, &f)" then its type will be unknown
17356 -- but the deduction does not succeed because the expression is
17357 not just the function on its own. */
17360 for (; arg
; arg
= OVL_NEXT (arg
))
17361 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
17362 TREE_TYPE (OVL_CURRENT (arg
)),
17363 strict
, sub_strict
, addr_p
, explain_p
)
17364 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
17366 goodfn
= OVL_CURRENT (arg
);
17370 /* [temp.deduct.type] A template-argument can be deduced from a pointer
17371 to function or pointer to member function argument if the set of
17372 overloaded functions does not contain function templates and at most
17373 one of a set of overloaded functions provides a unique match.
17375 So if we found multiple possibilities, we return success but don't
17376 deduce anything. */
17380 int i
= TREE_VEC_LENGTH (targs
);
17382 if (TREE_VEC_ELT (tempargs
, i
))
17384 tree old
= TREE_VEC_ELT (targs
, i
);
17385 tree new_
= TREE_VEC_ELT (tempargs
, i
);
17386 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
17387 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
17388 /* Don't forget explicit template arguments in a pack. */
17389 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
17390 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
17391 TREE_VEC_ELT (targs
, i
) = new_
;
17400 /* Core DR 115: In contexts where deduction is done and fails, or in
17401 contexts where deduction is not done, if a template argument list is
17402 specified and it, along with any default template arguments, identifies
17403 a single function template specialization, then the template-id is an
17404 lvalue for the function template specialization. */
17407 resolve_nondeduced_context (tree orig_expr
)
17409 tree expr
, offset
, baselink
;
17412 if (!type_unknown_p (orig_expr
))
17417 offset
= NULL_TREE
;
17418 baselink
= NULL_TREE
;
17420 if (TREE_CODE (expr
) == ADDR_EXPR
)
17422 expr
= TREE_OPERAND (expr
, 0);
17425 if (TREE_CODE (expr
) == OFFSET_REF
)
17428 expr
= TREE_OPERAND (expr
, 1);
17430 if (BASELINK_P (expr
))
17433 expr
= BASELINK_FUNCTIONS (expr
);
17436 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
17439 tree goodfn
= NULL_TREE
;
17441 /* If we got some explicit template args, we need to plug them into
17442 the affected templates before we try to unify, in case the
17443 explicit args will completely resolve the templates in question. */
17445 tree expl_subargs
= TREE_OPERAND (expr
, 1);
17446 tree arg
= TREE_OPERAND (expr
, 0);
17447 tree badfn
= NULL_TREE
;
17448 tree badargs
= NULL_TREE
;
17450 for (; arg
; arg
= OVL_NEXT (arg
))
17452 tree fn
= OVL_CURRENT (arg
);
17453 tree subargs
, elem
;
17455 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
17458 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
17459 expl_subargs
, NULL_TREE
, tf_none
,
17460 /*require_all_args=*/true,
17461 /*use_default_args=*/true);
17462 if (subargs
!= error_mark_node
17463 && !any_dependent_template_arguments_p (subargs
))
17465 elem
= instantiate_template (fn
, subargs
, tf_none
);
17466 if (elem
== error_mark_node
)
17471 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
17480 mark_used (goodfn
);
17483 expr
= build_baselink (BASELINK_BINFO (baselink
),
17484 BASELINK_ACCESS_BINFO (baselink
),
17485 expr
, BASELINK_OPTYPE (baselink
));
17489 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
17490 expr
= build_offset_ref (base
, expr
, addr
, tf_warning_or_error
);
17493 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
17496 else if (good
== 0 && badargs
)
17497 /* There were no good options and at least one bad one, so let the
17498 user know what the problem is. */
17499 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
17504 /* Subroutine of resolve_overloaded_unification; does deduction for a single
17505 overload. Fills TARGS with any deduced arguments, or error_mark_node if
17506 different overloads deduce different arguments for a given parm.
17507 ADDR_P is true if the expression for which deduction is being
17508 performed was of the form "& fn" rather than simply "fn".
17510 Returns 1 on success. */
17513 try_one_overload (tree tparms
,
17518 unification_kind_t strict
,
17527 if (arg
== error_mark_node
)
17530 /* [temp.deduct.type] A template-argument can be deduced from a pointer
17531 to function or pointer to member function argument if the set of
17532 overloaded functions does not contain function templates and at most
17533 one of a set of overloaded functions provides a unique match.
17535 So if this is a template, just return success. */
17537 if (uses_template_parms (arg
))
17540 if (TREE_CODE (arg
) == METHOD_TYPE
)
17541 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
17543 arg
= build_pointer_type (arg
);
17545 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
17547 /* We don't copy orig_targs for this because if we have already deduced
17548 some template args from previous args, unify would complain when we
17549 try to deduce a template parameter for the same argument, even though
17550 there isn't really a conflict. */
17551 nargs
= TREE_VEC_LENGTH (targs
);
17552 tempargs
= make_tree_vec (nargs
);
17554 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
17557 /* First make sure we didn't deduce anything that conflicts with
17558 explicitly specified args. */
17559 for (i
= nargs
; i
--; )
17561 tree elt
= TREE_VEC_ELT (tempargs
, i
);
17562 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
17566 else if (uses_template_parms (elt
))
17567 /* Since we're unifying against ourselves, we will fill in
17568 template args used in the function parm list with our own
17569 template parms. Discard them. */
17570 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
17571 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
17575 for (i
= nargs
; i
--; )
17577 tree elt
= TREE_VEC_ELT (tempargs
, i
);
17580 TREE_VEC_ELT (targs
, i
) = elt
;
17586 /* PARM is a template class (perhaps with unbound template
17587 parameters). ARG is a fully instantiated type. If ARG can be
17588 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
17589 TARGS are as for unify. */
17592 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
17595 tree copy_of_targs
;
17597 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
17598 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
17599 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
17602 /* We need to make a new template argument vector for the call to
17603 unify. If we used TARGS, we'd clutter it up with the result of
17604 the attempted unification, even if this class didn't work out.
17605 We also don't want to commit ourselves to all the unifications
17606 we've already done, since unification is supposed to be done on
17607 an argument-by-argument basis. In other words, consider the
17608 following pathological case:
17610 template <int I, int J, int K>
17613 template <int I, int J>
17614 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
17616 template <int I, int J, int K>
17617 void f(S<I, J, K>, S<I, I, I>);
17626 Now, by the time we consider the unification involving `s2', we
17627 already know that we must have `f<0, 0, 0>'. But, even though
17628 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
17629 because there are two ways to unify base classes of S<0, 1, 2>
17630 with S<I, I, I>. If we kept the already deduced knowledge, we
17631 would reject the possibility I=1. */
17632 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
17634 /* If unification failed, we're done. */
17635 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
17636 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
17642 /* Given a template type PARM and a class type ARG, find the unique
17643 base type in ARG that is an instance of PARM. We do not examine
17644 ARG itself; only its base-classes. If there is not exactly one
17645 appropriate base class, return NULL_TREE. PARM may be the type of
17646 a partial specialization, as well as a plain template type. Used
17649 static enum template_base_result
17650 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
17651 bool explain_p
, tree
*result
)
17653 tree rval
= NULL_TREE
;
17656 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
17658 binfo
= TYPE_BINFO (complete_type (arg
));
17661 /* The type could not be completed. */
17662 *result
= NULL_TREE
;
17663 return tbr_incomplete_type
;
17666 /* Walk in inheritance graph order. The search order is not
17667 important, and this avoids multiple walks of virtual bases. */
17668 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
17670 tree r
= try_class_unification (tparms
, targs
, parm
,
17671 BINFO_TYPE (binfo
), explain_p
);
17675 /* If there is more than one satisfactory baseclass, then:
17679 If they yield more than one possible deduced A, the type
17683 if (rval
&& !same_type_p (r
, rval
))
17685 *result
= NULL_TREE
;
17686 return tbr_ambiguous_baseclass
;
17694 return tbr_success
;
17697 /* Returns the level of DECL, which declares a template parameter. */
17700 template_decl_level (tree decl
)
17702 switch (TREE_CODE (decl
))
17705 case TEMPLATE_DECL
:
17706 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
17709 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
17712 gcc_unreachable ();
17717 /* Decide whether ARG can be unified with PARM, considering only the
17718 cv-qualifiers of each type, given STRICT as documented for unify.
17719 Returns nonzero iff the unification is OK on that basis. */
17722 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
17724 int arg_quals
= cp_type_quals (arg
);
17725 int parm_quals
= cp_type_quals (parm
);
17727 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17728 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
17730 /* Although a CVR qualifier is ignored when being applied to a
17731 substituted template parameter ([8.3.2]/1 for example), that
17732 does not allow us to unify "const T" with "int&" because both
17733 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
17734 It is ok when we're allowing additional CV qualifiers
17735 at the outer level [14.8.2.1]/3,1st bullet. */
17736 if ((TREE_CODE (arg
) == REFERENCE_TYPE
17737 || TREE_CODE (arg
) == FUNCTION_TYPE
17738 || TREE_CODE (arg
) == METHOD_TYPE
)
17739 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
17742 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
17743 && (parm_quals
& TYPE_QUAL_RESTRICT
))
17747 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
17748 && (arg_quals
& parm_quals
) != parm_quals
)
17751 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
17752 && (parm_quals
& arg_quals
) != arg_quals
)
17758 /* Determines the LEVEL and INDEX for the template parameter PARM. */
17760 template_parm_level_and_index (tree parm
, int* level
, int* index
)
17762 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17763 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
17764 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17766 *index
= TEMPLATE_TYPE_IDX (parm
);
17767 *level
= TEMPLATE_TYPE_LEVEL (parm
);
17771 *index
= TEMPLATE_PARM_IDX (parm
);
17772 *level
= TEMPLATE_PARM_LEVEL (parm
);
17776 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
17778 if (unify (TP, TA, P, A, S, EP)) \
17782 /* Unifies the remaining arguments in PACKED_ARGS with the pack
17783 expansion at the end of PACKED_PARMS. Returns 0 if the type
17784 deduction succeeds, 1 otherwise. STRICT is the same as in
17785 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
17786 call argument list. We'll need to adjust the arguments to make them
17787 types. SUBR tells us if this is from a recursive call to
17788 type_unification_real, or for comparing two template argument
17792 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
17793 tree packed_args
, unification_kind_t strict
,
17794 bool subr
, bool explain_p
)
17797 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
17798 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
17799 tree pack
, packs
= NULL_TREE
;
17800 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
17802 packed_args
= expand_template_argument_pack (packed_args
);
17804 int len
= TREE_VEC_LENGTH (packed_args
);
17806 /* Determine the parameter packs we will be deducing from the
17807 pattern, and record their current deductions. */
17808 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
17809 pack
; pack
= TREE_CHAIN (pack
))
17811 tree parm_pack
= TREE_VALUE (pack
);
17814 /* Determine the index and level of this parameter pack. */
17815 template_parm_level_and_index (parm_pack
, &level
, &idx
);
17817 /* Keep track of the parameter packs and their corresponding
17819 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
17820 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
17823 /* Loop through all of the arguments that have not yet been
17824 unified and unify each with the pattern. */
17825 for (i
= start
; i
< len
; i
++)
17828 bool any_explicit
= false;
17829 tree arg
= TREE_VEC_ELT (packed_args
, i
);
17831 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
17832 or the element of its argument pack at the current index if
17833 this argument was explicitly specified. */
17834 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
17838 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
17841 if (TREE_VALUE (pack
)
17842 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
17843 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
17845 any_explicit
= true;
17846 arg
= TREE_VEC_ELT (pargs
, i
- start
);
17848 TMPL_ARG (targs
, level
, idx
) = arg
;
17851 /* If we had explicit template arguments, substitute them into the
17852 pattern before deduction. */
17855 /* Some arguments might still be unspecified or dependent. */
17857 ++processing_template_decl
;
17858 dependent
= any_dependent_template_arguments_p (targs
);
17860 --processing_template_decl
;
17861 parm
= tsubst (pattern
, targs
,
17862 explain_p
? tf_warning_or_error
: tf_none
,
17865 --processing_template_decl
;
17866 if (parm
== error_mark_node
)
17872 /* Unify the pattern with the current argument. */
17873 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
17877 /* For each parameter pack, collect the deduced value. */
17878 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
17881 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
17883 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
17884 TMPL_ARG (targs
, level
, idx
);
17888 /* Verify that the results of unification with the parameter packs
17889 produce results consistent with what we've seen before, and make
17890 the deduced argument packs available. */
17891 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
17893 tree old_pack
= TREE_VALUE (pack
);
17894 tree new_args
= TREE_TYPE (pack
);
17895 int i
, len
= TREE_VEC_LENGTH (new_args
);
17897 bool nondeduced_p
= false;
17899 /* By default keep the original deduced argument pack.
17900 If necessary, more specific code is going to update the
17901 resulting deduced argument later down in this function. */
17902 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
17903 TMPL_ARG (targs
, level
, idx
) = old_pack
;
17905 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
17906 actually deduce anything. */
17907 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
17908 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
17909 nondeduced_p
= true;
17913 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
17915 /* If we had fewer function args than explicit template args,
17916 just use the explicits. */
17917 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
17918 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
17919 if (len
< explicit_len
)
17920 new_args
= explicit_args
;
17926 /* Build the deduced *_ARGUMENT_PACK. */
17927 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
17929 result
= make_node (NONTYPE_ARGUMENT_PACK
);
17930 TREE_TYPE (result
) =
17931 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
17932 TREE_CONSTANT (result
) = 1;
17935 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
17937 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
17939 /* Note the deduced argument packs for this parameter
17941 TMPL_ARG (targs
, level
, idx
) = result
;
17943 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
17944 && (ARGUMENT_PACK_ARGS (old_pack
)
17945 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
17947 /* We only had the explicitly-provided arguments before, but
17948 now we have a complete set of arguments. */
17949 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
17951 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
17952 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
17953 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
17957 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
17958 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
17960 if (!comp_template_args_with_info (old_args
, new_args
,
17961 &bad_old_arg
, &bad_new_arg
))
17962 /* Inconsistent unification of this parameter pack. */
17963 return unify_parameter_pack_inconsistent (explain_p
,
17969 return unify_success (explain_p
);
17972 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17973 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17974 parameters and return value are as for unify. */
17977 unify_array_domain (tree tparms
, tree targs
,
17978 tree parm_dom
, tree arg_dom
,
17986 /* Our representation of array types uses "N - 1" as the
17987 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17988 not an integer constant. We cannot unify arbitrarily
17989 complex expressions, so we eliminate the MINUS_EXPRs
17991 parm_max
= TYPE_MAX_VALUE (parm_dom
);
17992 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
17995 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
17996 parm_max
= TREE_OPERAND (parm_max
, 0);
17998 arg_max
= TYPE_MAX_VALUE (arg_dom
);
17999 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
18002 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
18003 trying to unify the type of a variable with the type
18004 of a template parameter. For example:
18006 template <unsigned int N>
18007 void f (char (&) [N]);
18014 Here, the type of the ARG will be "int [g(i)]", and
18015 may be a SAVE_EXPR, etc. */
18016 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
18017 return unify_vla_arg (explain_p
, arg_dom
);
18018 arg_max
= TREE_OPERAND (arg_max
, 0);
18021 /* If only one of the bounds used a MINUS_EXPR, compensate
18022 by adding one to the other bound. */
18023 if (parm_cst
&& !arg_cst
)
18024 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
18028 else if (arg_cst
&& !parm_cst
)
18029 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
18034 return unify (tparms
, targs
, parm_max
, arg_max
,
18035 UNIFY_ALLOW_INTEGER
, explain_p
);
18038 /* Deduce the value of template parameters. TPARMS is the (innermost)
18039 set of template parameters to a template. TARGS is the bindings
18040 for those template parameters, as determined thus far; TARGS may
18041 include template arguments for outer levels of template parameters
18042 as well. PARM is a parameter to a template function, or a
18043 subcomponent of that parameter; ARG is the corresponding argument.
18044 This function attempts to match PARM with ARG in a manner
18045 consistent with the existing assignments in TARGS. If more values
18046 are deduced, then TARGS is updated.
18048 Returns 0 if the type deduction succeeds, 1 otherwise. The
18049 parameter STRICT is a bitwise or of the following flags:
18052 Require an exact match between PARM and ARG.
18053 UNIFY_ALLOW_MORE_CV_QUAL:
18054 Allow the deduced ARG to be more cv-qualified (by qualification
18055 conversion) than ARG.
18056 UNIFY_ALLOW_LESS_CV_QUAL:
18057 Allow the deduced ARG to be less cv-qualified than ARG.
18058 UNIFY_ALLOW_DERIVED:
18059 Allow the deduced ARG to be a template base class of ARG,
18060 or a pointer to a template base class of the type pointed to by
18062 UNIFY_ALLOW_INTEGER:
18063 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
18064 case for more information.
18065 UNIFY_ALLOW_OUTER_LEVEL:
18066 This is the outermost level of a deduction. Used to determine validity
18067 of qualification conversions. A valid qualification conversion must
18068 have const qualified pointers leading up to the inner type which
18069 requires additional CV quals, except at the outer level, where const
18070 is not required [conv.qual]. It would be normal to set this flag in
18071 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
18072 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
18073 This is the outermost level of a deduction, and PARM can be more CV
18074 qualified at this point.
18075 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
18076 This is the outermost level of a deduction, and PARM can be less CV
18077 qualified at this point. */
18080 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
18086 int strict_in
= strict
;
18088 /* I don't think this will do the right thing with respect to types.
18089 But the only case I've seen it in so far has been array bounds, where
18090 signedness is the only information lost, and I think that will be
18092 while (TREE_CODE (parm
) == NOP_EXPR
)
18093 parm
= TREE_OPERAND (parm
, 0);
18095 if (arg
== error_mark_node
)
18096 return unify_invalid (explain_p
);
18097 if (arg
== unknown_type_node
18098 || arg
== init_list_type_node
)
18099 /* We can't deduce anything from this, but we might get all the
18100 template args from other function args. */
18101 return unify_success (explain_p
);
18103 /* If PARM uses template parameters, then we can't bail out here,
18104 even if ARG == PARM, since we won't record unifications for the
18105 template parameters. We might need them if we're trying to
18106 figure out which of two things is more specialized. */
18107 if (arg
== parm
&& !uses_template_parms (parm
))
18108 return unify_success (explain_p
);
18110 /* Handle init lists early, so the rest of the function can assume
18111 we're dealing with a type. */
18112 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
18116 tree orig_parm
= parm
;
18118 /* Replace T with std::initializer_list<T> for deduction. */
18119 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
18120 && flag_deduce_init_list
)
18121 parm
= listify (parm
);
18123 if (!is_std_init_list (parm
)
18124 && TREE_CODE (parm
) != ARRAY_TYPE
)
18125 /* We can only deduce from an initializer list argument if the
18126 parameter is std::initializer_list or an array; otherwise this
18127 is a non-deduced context. */
18128 return unify_success (explain_p
);
18130 if (TREE_CODE (parm
) == ARRAY_TYPE
)
18131 elttype
= TREE_TYPE (parm
);
18134 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
18135 /* Deduction is defined in terms of a single type, so just punt
18136 on the (bizarre) std::initializer_list<T...>. */
18137 if (PACK_EXPANSION_P (elttype
))
18138 return unify_success (explain_p
);
18141 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
18143 int elt_strict
= strict
;
18145 if (elt
== error_mark_node
)
18146 return unify_invalid (explain_p
);
18148 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
18150 tree type
= TREE_TYPE (elt
);
18151 if (type
== error_mark_node
)
18152 return unify_invalid (explain_p
);
18153 /* It should only be possible to get here for a call. */
18154 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
18155 elt_strict
|= maybe_adjust_types_for_deduction
18156 (DEDUCE_CALL
, &elttype
, &type
, elt
);
18160 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
18164 if (TREE_CODE (parm
) == ARRAY_TYPE
18165 && deducible_array_bound (TYPE_DOMAIN (parm
)))
18167 /* Also deduce from the length of the initializer list. */
18168 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
18169 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
18170 if (idx
== error_mark_node
)
18171 return unify_invalid (explain_p
);
18172 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
18176 /* If the std::initializer_list<T> deduction worked, replace the
18177 deduced A with std::initializer_list<A>. */
18178 if (orig_parm
!= parm
)
18180 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
18181 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
18182 targ
= listify (targ
);
18183 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
18185 return unify_success (explain_p
);
18188 /* Immediately reject some pairs that won't unify because of
18189 cv-qualification mismatches. */
18190 if (TREE_CODE (arg
) == TREE_CODE (parm
)
18192 /* It is the elements of the array which hold the cv quals of an array
18193 type, and the elements might be template type parms. We'll check
18194 when we recurse. */
18195 && TREE_CODE (arg
) != ARRAY_TYPE
18196 /* We check the cv-qualifiers when unifying with template type
18197 parameters below. We want to allow ARG `const T' to unify with
18198 PARM `T' for example, when computing which of two templates
18199 is more specialized, for example. */
18200 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
18201 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
18202 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
18204 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
18205 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
18206 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
18207 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
18208 strict
&= ~UNIFY_ALLOW_DERIVED
;
18209 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
18210 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
18212 switch (TREE_CODE (parm
))
18214 case TYPENAME_TYPE
:
18216 case UNBOUND_CLASS_TEMPLATE
:
18217 /* In a type which contains a nested-name-specifier, template
18218 argument values cannot be deduced for template parameters used
18219 within the nested-name-specifier. */
18220 return unify_success (explain_p
);
18222 case TEMPLATE_TYPE_PARM
:
18223 case TEMPLATE_TEMPLATE_PARM
:
18224 case BOUND_TEMPLATE_TEMPLATE_PARM
:
18225 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
18226 if (error_operand_p (tparm
))
18227 return unify_invalid (explain_p
);
18229 if (TEMPLATE_TYPE_LEVEL (parm
)
18230 != template_decl_level (tparm
))
18231 /* The PARM is not one we're trying to unify. Just check
18232 to see if it matches ARG. */
18234 if (TREE_CODE (arg
) == TREE_CODE (parm
)
18235 && (is_auto (parm
) ? is_auto (arg
)
18236 : same_type_p (parm
, arg
)))
18237 return unify_success (explain_p
);
18239 return unify_type_mismatch (explain_p
, parm
, arg
);
18241 idx
= TEMPLATE_TYPE_IDX (parm
);
18242 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
18243 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
18244 if (error_operand_p (tparm
))
18245 return unify_invalid (explain_p
);
18247 /* Check for mixed types and values. */
18248 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
18249 && TREE_CODE (tparm
) != TYPE_DECL
)
18250 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
18251 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
18252 gcc_unreachable ();
18254 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
18256 /* ARG must be constructed from a template class or a template
18257 template parameter. */
18258 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
18259 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
18260 return unify_template_deduction_failure (explain_p
, parm
, arg
);
18262 tree parmvec
= TYPE_TI_ARGS (parm
);
18263 /* An alias template name is never deduced. */
18264 if (TYPE_ALIAS_P (arg
))
18265 arg
= strip_typedefs (arg
);
18266 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
18267 tree full_argvec
= add_to_template_args (targs
, argvec
);
18269 = DECL_INNERMOST_TEMPLATE_PARMS
18270 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
18272 int parm_variadic_p
= 0;
18274 /* The resolution to DR150 makes clear that default
18275 arguments for an N-argument may not be used to bind T
18276 to a template template parameter with fewer than N
18277 parameters. It is not safe to permit the binding of
18278 default arguments as an extension, as that may change
18279 the meaning of a conforming program. Consider:
18281 struct Dense { static const unsigned int dim = 1; };
18283 template <template <typename> class View,
18285 void operator+(float, View<Block> const&);
18287 template <typename Block,
18288 unsigned int Dim = Block::dim>
18289 struct Lvalue_proxy { operator float() const; };
18293 Lvalue_proxy<Dense> p;
18298 Here, if Lvalue_proxy is permitted to bind to View, then
18299 the global operator+ will be used; if they are not, the
18300 Lvalue_proxy will be converted to float. */
18301 if (coerce_template_parms (parm_parms
,
18303 TYPE_TI_TEMPLATE (parm
),
18305 ? tf_warning_or_error
18307 /*require_all_args=*/true,
18308 /*use_default_args=*/false)
18309 == error_mark_node
)
18312 /* Deduce arguments T, i from TT<T> or TT<i>.
18313 We check each element of PARMVEC and ARGVEC individually
18314 rather than the whole TREE_VEC since they can have
18315 different number of elements. */
18317 parmvec
= expand_template_argument_pack (parmvec
);
18318 argvec
= expand_template_argument_pack (argvec
);
18320 len
= TREE_VEC_LENGTH (parmvec
);
18322 /* Check if the parameters end in a pack, making them
18325 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
18326 parm_variadic_p
= 1;
18328 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
18329 /* If the template argument list of P contains a pack
18330 expansion that is not the last template argument, the
18331 entire template argument list is a non-deduced
18333 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
18334 return unify_success (explain_p
);
18336 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
18337 return unify_too_few_arguments (explain_p
,
18338 TREE_VEC_LENGTH (argvec
), len
);
18340 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
18342 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
18343 TREE_VEC_ELT (parmvec
, i
),
18344 TREE_VEC_ELT (argvec
, i
),
18345 UNIFY_ALLOW_NONE
, explain_p
);
18348 if (parm_variadic_p
18349 && unify_pack_expansion (tparms
, targs
,
18352 /*subr=*/true, explain_p
))
18355 arg
= TYPE_TI_TEMPLATE (arg
);
18357 /* Fall through to deduce template name. */
18360 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
18361 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
18363 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
18365 /* Simple cases: Value already set, does match or doesn't. */
18366 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
18367 return unify_success (explain_p
);
18369 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
18373 /* If PARM is `const T' and ARG is only `int', we don't have
18374 a match unless we are allowing additional qualification.
18375 If ARG is `const int' and PARM is just `T' that's OK;
18376 that binds `const int' to `T'. */
18377 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
18379 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
18381 /* Consider the case where ARG is `const volatile int' and
18382 PARM is `const T'. Then, T should be `volatile int'. */
18383 arg
= cp_build_qualified_type_real
18384 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
18385 if (arg
== error_mark_node
)
18386 return unify_invalid (explain_p
);
18388 /* Simple cases: Value already set, does match or doesn't. */
18389 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
18390 return unify_success (explain_p
);
18392 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
18394 /* Make sure that ARG is not a variable-sized array. (Note
18395 that were talking about variable-sized arrays (like
18396 `int[n]'), rather than arrays of unknown size (like
18397 `int[]').) We'll get very confused by such a type since
18398 the bound of the array is not constant, and therefore
18399 not mangleable. Besides, such types are not allowed in
18400 ISO C++, so we can do as we please here. We do allow
18401 them for 'auto' deduction, since that isn't ABI-exposed. */
18402 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
18403 return unify_vla_arg (explain_p
, arg
);
18405 /* Strip typedefs as in convert_template_argument. */
18406 arg
= canonicalize_type_argument (arg
, tf_none
);
18409 /* If ARG is a parameter pack or an expansion, we cannot unify
18410 against it unless PARM is also a parameter pack. */
18411 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
18412 && !template_parameter_pack_p (parm
))
18413 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
18415 /* If the argument deduction results is a METHOD_TYPE,
18416 then there is a problem.
18417 METHOD_TYPE doesn't map to any real C++ type the result of
18418 the deduction can not be of that type. */
18419 if (TREE_CODE (arg
) == METHOD_TYPE
)
18420 return unify_method_type_error (explain_p
, arg
);
18422 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
18423 return unify_success (explain_p
);
18425 case TEMPLATE_PARM_INDEX
:
18426 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
18427 if (error_operand_p (tparm
))
18428 return unify_invalid (explain_p
);
18430 if (TEMPLATE_PARM_LEVEL (parm
)
18431 != template_decl_level (tparm
))
18433 /* The PARM is not one we're trying to unify. Just check
18434 to see if it matches ARG. */
18435 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
18436 && cp_tree_equal (parm
, arg
));
18438 unify_expression_unequal (explain_p
, parm
, arg
);
18442 idx
= TEMPLATE_PARM_IDX (parm
);
18443 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
18447 int x
= !cp_tree_equal (targ
, arg
);
18449 unify_inconsistency (explain_p
, parm
, targ
, arg
);
18453 /* [temp.deduct.type] If, in the declaration of a function template
18454 with a non-type template-parameter, the non-type
18455 template-parameter is used in an expression in the function
18456 parameter-list and, if the corresponding template-argument is
18457 deduced, the template-argument type shall match the type of the
18458 template-parameter exactly, except that a template-argument
18459 deduced from an array bound may be of any integral type.
18460 The non-type parameter might use already deduced type parameters. */
18461 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
18462 if (!TREE_TYPE (arg
))
18463 /* Template-parameter dependent expression. Just accept it for now.
18464 It will later be processed in convert_template_argument. */
18466 else if (same_type_p (TREE_TYPE (arg
), tparm
))
18468 else if ((strict
& UNIFY_ALLOW_INTEGER
)
18469 && CP_INTEGRAL_TYPE_P (tparm
))
18470 /* Convert the ARG to the type of PARM; the deduced non-type
18471 template argument must exactly match the types of the
18472 corresponding parameter. */
18473 arg
= fold (build_nop (tparm
, arg
));
18474 else if (uses_template_parms (tparm
))
18475 /* We haven't deduced the type of this parameter yet. Try again
18477 return unify_success (explain_p
);
18479 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
18481 /* If ARG is a parameter pack or an expansion, we cannot unify
18482 against it unless PARM is also a parameter pack. */
18483 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
18484 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
18485 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
18488 bool removed_attr
= false;
18489 arg
= strip_typedefs_expr (arg
, &removed_attr
);
18491 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
18492 return unify_success (explain_p
);
18496 /* A pointer-to-member constant can be unified only with
18497 another constant. */
18498 if (TREE_CODE (arg
) != PTRMEM_CST
)
18499 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
18501 /* Just unify the class member. It would be useless (and possibly
18502 wrong, depending on the strict flags) to unify also
18503 PTRMEM_CST_CLASS, because we want to be sure that both parm and
18504 arg refer to the same variable, even if through different
18505 classes. For instance:
18507 struct A { int x; };
18510 Unification of &A::x and &B::x must succeed. */
18511 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
18512 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
18517 if (!TYPE_PTR_P (arg
))
18518 return unify_type_mismatch (explain_p
, parm
, arg
);
18520 /* [temp.deduct.call]
18522 A can be another pointer or pointer to member type that can
18523 be converted to the deduced A via a qualification
18524 conversion (_conv.qual_).
18526 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
18527 This will allow for additional cv-qualification of the
18528 pointed-to types if appropriate. */
18530 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
18531 /* The derived-to-base conversion only persists through one
18532 level of pointers. */
18533 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
18535 return unify (tparms
, targs
, TREE_TYPE (parm
),
18536 TREE_TYPE (arg
), strict
, explain_p
);
18539 case REFERENCE_TYPE
:
18540 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
18541 return unify_type_mismatch (explain_p
, parm
, arg
);
18542 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
18543 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
18546 if (TREE_CODE (arg
) != ARRAY_TYPE
)
18547 return unify_type_mismatch (explain_p
, parm
, arg
);
18548 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
18549 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
18550 return unify_type_mismatch (explain_p
, parm
, arg
);
18551 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
18552 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
18553 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
18554 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
18555 TYPE_DOMAIN (arg
), explain_p
);
18556 return unify_success (explain_p
);
18563 case ENUMERAL_TYPE
:
18566 if (TREE_CODE (arg
) != TREE_CODE (parm
))
18567 return unify_type_mismatch (explain_p
, parm
, arg
);
18569 /* We have already checked cv-qualification at the top of the
18571 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
18572 return unify_type_mismatch (explain_p
, parm
, arg
);
18574 /* As far as unification is concerned, this wins. Later checks
18575 will invalidate it if necessary. */
18576 return unify_success (explain_p
);
18578 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
18579 /* Type INTEGER_CST can come from ordinary constant template args. */
18581 while (TREE_CODE (arg
) == NOP_EXPR
)
18582 arg
= TREE_OPERAND (arg
, 0);
18584 if (TREE_CODE (arg
) != INTEGER_CST
)
18585 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
18586 return (tree_int_cst_equal (parm
, arg
)
18587 ? unify_success (explain_p
)
18588 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
18592 int i
, len
, argslen
;
18593 int parm_variadic_p
= 0;
18595 if (TREE_CODE (arg
) != TREE_VEC
)
18596 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
18598 len
= TREE_VEC_LENGTH (parm
);
18599 argslen
= TREE_VEC_LENGTH (arg
);
18601 /* Check for pack expansions in the parameters. */
18602 for (i
= 0; i
< len
; ++i
)
18604 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
18607 /* We can unify against something with a trailing
18609 parm_variadic_p
= 1;
18611 /* [temp.deduct.type]/9: If the template argument list of
18612 P contains a pack expansion that is not the last
18613 template argument, the entire template argument list
18614 is a non-deduced context. */
18615 return unify_success (explain_p
);
18619 /* If we don't have enough arguments to satisfy the parameters
18620 (not counting the pack expression at the end), or we have
18621 too many arguments for a parameter list that doesn't end in
18622 a pack expression, we can't unify. */
18623 if (parm_variadic_p
18624 ? argslen
< len
- parm_variadic_p
18626 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
18628 /* Unify all of the parameters that precede the (optional)
18629 pack expression. */
18630 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
18632 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
18633 TREE_VEC_ELT (parm
, i
),
18634 TREE_VEC_ELT (arg
, i
),
18635 UNIFY_ALLOW_NONE
, explain_p
);
18637 if (parm_variadic_p
)
18638 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
18640 /*subr=*/true, explain_p
);
18641 return unify_success (explain_p
);
18646 if (TREE_CODE (arg
) != TREE_CODE (parm
))
18647 return unify_type_mismatch (explain_p
, parm
, arg
);
18649 if (TYPE_PTRMEMFUNC_P (parm
))
18651 if (!TYPE_PTRMEMFUNC_P (arg
))
18652 return unify_type_mismatch (explain_p
, parm
, arg
);
18654 return unify (tparms
, targs
,
18655 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
18656 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
18657 strict
, explain_p
);
18659 else if (TYPE_PTRMEMFUNC_P (arg
))
18660 return unify_type_mismatch (explain_p
, parm
, arg
);
18662 if (CLASSTYPE_TEMPLATE_INFO (parm
))
18664 tree t
= NULL_TREE
;
18666 if (strict_in
& UNIFY_ALLOW_DERIVED
)
18668 /* First, we try to unify the PARM and ARG directly. */
18669 t
= try_class_unification (tparms
, targs
,
18670 parm
, arg
, explain_p
);
18674 /* Fallback to the special case allowed in
18675 [temp.deduct.call]:
18677 If P is a class, and P has the form
18678 template-id, then A can be a derived class of
18679 the deduced A. Likewise, if P is a pointer to
18680 a class of the form template-id, A can be a
18681 pointer to a derived class pointed to by the
18683 enum template_base_result r
;
18684 r
= get_template_base (tparms
, targs
, parm
, arg
,
18688 return unify_no_common_base (explain_p
, r
, parm
, arg
);
18691 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
18692 && (CLASSTYPE_TI_TEMPLATE (parm
)
18693 == CLASSTYPE_TI_TEMPLATE (arg
)))
18694 /* Perhaps PARM is something like S<U> and ARG is S<int>.
18695 Then, we should unify `int' and `U'. */
18698 /* There's no chance of unification succeeding. */
18699 return unify_type_mismatch (explain_p
, parm
, arg
);
18701 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
18702 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
18704 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
18705 return unify_type_mismatch (explain_p
, parm
, arg
);
18706 return unify_success (explain_p
);
18709 case FUNCTION_TYPE
:
18711 unsigned int nargs
;
18716 if (TREE_CODE (arg
) != TREE_CODE (parm
))
18717 return unify_type_mismatch (explain_p
, parm
, arg
);
18719 /* CV qualifications for methods can never be deduced, they must
18720 match exactly. We need to check them explicitly here,
18721 because type_unification_real treats them as any other
18722 cv-qualified parameter. */
18723 if (TREE_CODE (parm
) == METHOD_TYPE
18724 && (!check_cv_quals_for_unify
18726 class_of_this_parm (arg
),
18727 class_of_this_parm (parm
))))
18728 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
18730 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
18731 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
18733 nargs
= list_length (TYPE_ARG_TYPES (arg
));
18734 args
= XALLOCAVEC (tree
, nargs
);
18735 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
18736 a
!= NULL_TREE
&& a
!= void_list_node
;
18737 a
= TREE_CHAIN (a
), ++i
)
18738 args
[i
] = TREE_VALUE (a
);
18741 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
18742 args
, nargs
, 1, DEDUCE_EXACT
,
18743 LOOKUP_NORMAL
, NULL
, explain_p
);
18747 /* Unify a pointer to member with a pointer to member function, which
18748 deduces the type of the member as a function type. */
18749 if (TYPE_PTRMEMFUNC_P (arg
))
18751 /* Check top-level cv qualifiers */
18752 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
18753 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
18755 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
18756 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
18757 UNIFY_ALLOW_NONE
, explain_p
);
18759 /* Determine the type of the function we are unifying against. */
18760 tree fntype
= static_fn_type (arg
);
18762 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
18765 if (TREE_CODE (arg
) != OFFSET_TYPE
)
18766 return unify_type_mismatch (explain_p
, parm
, arg
);
18767 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
18768 TYPE_OFFSET_BASETYPE (arg
),
18769 UNIFY_ALLOW_NONE
, explain_p
);
18770 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
18771 strict
, explain_p
);
18774 if (DECL_TEMPLATE_PARM_P (parm
))
18775 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
18776 if (arg
!= scalar_constant_value (parm
))
18777 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
18778 return unify_success (explain_p
);
18781 case TEMPLATE_DECL
:
18782 /* Matched cases are handled by the ARG == PARM test above. */
18783 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
18786 /* A non-type template parameter that is a variable should be a
18787 an integral constant, in which case, it whould have been
18788 folded into its (constant) value. So we should not be getting
18789 a variable here. */
18790 gcc_unreachable ();
18792 case TYPE_ARGUMENT_PACK
:
18793 case NONTYPE_ARGUMENT_PACK
:
18794 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
18795 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
18798 case DECLTYPE_TYPE
:
18799 case UNDERLYING_TYPE
:
18800 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
18801 or UNDERLYING_TYPE nodes. */
18802 return unify_success (explain_p
);
18805 /* Unification fails if we hit an error node. */
18806 return unify_invalid (explain_p
);
18809 if (REFERENCE_REF_P (parm
))
18811 if (REFERENCE_REF_P (arg
))
18812 arg
= TREE_OPERAND (arg
, 0);
18813 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
18814 strict
, explain_p
);
18819 /* An unresolved overload is a nondeduced context. */
18820 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
18821 return unify_success (explain_p
);
18822 gcc_assert (EXPR_P (parm
));
18824 /* We must be looking at an expression. This can happen with
18828 void foo(S<I>, S<I + 2>);
18830 This is a "nondeduced context":
18834 The nondeduced contexts are:
18836 --A type that is a template-id in which one or more of
18837 the template-arguments is an expression that references
18838 a template-parameter.
18840 In these cases, we assume deduction succeeded, but don't
18841 actually infer any unifications. */
18843 if (!uses_template_parms (parm
)
18844 && !template_args_equal (parm
, arg
))
18845 return unify_expression_unequal (explain_p
, parm
, arg
);
18847 return unify_success (explain_p
);
18850 #undef RECUR_AND_CHECK_FAILURE
18852 /* Note that DECL can be defined in this translation unit, if
18856 mark_definable (tree decl
)
18859 DECL_NOT_REALLY_EXTERN (decl
) = 1;
18860 FOR_EACH_CLONE (clone
, decl
)
18861 DECL_NOT_REALLY_EXTERN (clone
) = 1;
18864 /* Called if RESULT is explicitly instantiated, or is a member of an
18865 explicitly instantiated class. */
18868 mark_decl_instantiated (tree result
, int extern_p
)
18870 SET_DECL_EXPLICIT_INSTANTIATION (result
);
18872 /* If this entity has already been written out, it's too late to
18873 make any modifications. */
18874 if (TREE_ASM_WRITTEN (result
))
18877 /* For anonymous namespace we don't need to do anything. */
18878 if (decl_anon_ns_mem_p (result
))
18880 gcc_assert (!TREE_PUBLIC (result
));
18884 if (TREE_CODE (result
) != FUNCTION_DECL
)
18885 /* The TREE_PUBLIC flag for function declarations will have been
18886 set correctly by tsubst. */
18887 TREE_PUBLIC (result
) = 1;
18889 /* This might have been set by an earlier implicit instantiation. */
18890 DECL_COMDAT (result
) = 0;
18893 DECL_NOT_REALLY_EXTERN (result
) = 0;
18896 mark_definable (result
);
18897 mark_needed (result
);
18898 /* Always make artificials weak. */
18899 if (DECL_ARTIFICIAL (result
) && flag_weak
)
18900 comdat_linkage (result
);
18901 /* For WIN32 we also want to put explicit instantiations in
18902 linkonce sections. */
18903 else if (TREE_PUBLIC (result
))
18904 maybe_make_one_only (result
);
18907 /* If EXTERN_P, then this function will not be emitted -- unless
18908 followed by an explicit instantiation, at which point its linkage
18909 will be adjusted. If !EXTERN_P, then this function will be
18910 emitted here. In neither circumstance do we want
18911 import_export_decl to adjust the linkage. */
18912 DECL_INTERFACE_KNOWN (result
) = 1;
18915 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
18916 important template arguments. If any are missing, we check whether
18917 they're important by using error_mark_node for substituting into any
18918 args that were used for partial ordering (the ones between ARGS and END)
18919 and seeing if it bubbles up. */
18922 check_undeduced_parms (tree targs
, tree args
, tree end
)
18924 bool found
= false;
18926 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
18927 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
18930 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
18934 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
18935 if (substed
== error_mark_node
)
18941 /* Given two function templates PAT1 and PAT2, return:
18943 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
18944 -1 if PAT2 is more specialized than PAT1.
18945 0 if neither is more specialized.
18947 LEN indicates the number of parameters we should consider
18948 (defaulted parameters should not be considered).
18950 The 1998 std underspecified function template partial ordering, and
18951 DR214 addresses the issue. We take pairs of arguments, one from
18952 each of the templates, and deduce them against each other. One of
18953 the templates will be more specialized if all the *other*
18954 template's arguments deduce against its arguments and at least one
18955 of its arguments *does* *not* deduce against the other template's
18956 corresponding argument. Deduction is done as for class templates.
18957 The arguments used in deduction have reference and top level cv
18958 qualifiers removed. Iff both arguments were originally reference
18959 types *and* deduction succeeds in both directions, an lvalue reference
18960 wins against an rvalue reference and otherwise the template
18961 with the more cv-qualified argument wins for that pairing (if
18962 neither is more cv-qualified, they both are equal). Unlike regular
18963 deduction, after all the arguments have been deduced in this way,
18964 we do *not* verify the deduced template argument values can be
18965 substituted into non-deduced contexts.
18967 The logic can be a bit confusing here, because we look at deduce1 and
18968 targs1 to see if pat2 is at least as specialized, and vice versa; if we
18969 can find template arguments for pat1 to make arg1 look like arg2, that
18970 means that arg2 is at least as specialized as arg1. */
18973 more_specialized_fn (tree pat1
, tree pat2
, int len
)
18975 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
18976 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
18977 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
18978 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
18979 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
18980 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
18981 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
18982 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
18983 tree origs1
, origs2
;
18984 bool lose1
= false;
18985 bool lose2
= false;
18987 /* Remove the this parameter from non-static member functions. If
18988 one is a non-static member function and the other is not a static
18989 member function, remove the first parameter from that function
18990 also. This situation occurs for operator functions where we
18991 locate both a member function (with this pointer) and non-member
18992 operator (with explicit first operand). */
18993 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
18995 len
--; /* LEN is the number of significant arguments for DECL1 */
18996 args1
= TREE_CHAIN (args1
);
18997 if (!DECL_STATIC_FUNCTION_P (decl2
))
18998 args2
= TREE_CHAIN (args2
);
19000 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
19002 args2
= TREE_CHAIN (args2
);
19003 if (!DECL_STATIC_FUNCTION_P (decl1
))
19006 args1
= TREE_CHAIN (args1
);
19010 /* If only one is a conversion operator, they are unordered. */
19011 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
19014 /* Consider the return type for a conversion function */
19015 if (DECL_CONV_FN_P (decl1
))
19017 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
19018 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
19022 processing_template_decl
++;
19028 /* Stop when an ellipsis is seen. */
19029 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
19031 tree arg1
= TREE_VALUE (args1
);
19032 tree arg2
= TREE_VALUE (args2
);
19033 int deduce1
, deduce2
;
19039 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
19040 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
19042 /* When both arguments are pack expansions, we need only
19043 unify the patterns themselves. */
19044 arg1
= PACK_EXPANSION_PATTERN (arg1
);
19045 arg2
= PACK_EXPANSION_PATTERN (arg2
);
19047 /* This is the last comparison we need to do. */
19051 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
19053 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
19054 arg1
= TREE_TYPE (arg1
);
19055 quals1
= cp_type_quals (arg1
);
19058 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
19060 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
19061 arg2
= TREE_TYPE (arg2
);
19062 quals2
= cp_type_quals (arg2
);
19065 arg1
= TYPE_MAIN_VARIANT (arg1
);
19066 arg2
= TYPE_MAIN_VARIANT (arg2
);
19068 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
19070 int i
, len2
= list_length (args2
);
19071 tree parmvec
= make_tree_vec (1);
19072 tree argvec
= make_tree_vec (len2
);
19075 /* Setup the parameter vector, which contains only ARG1. */
19076 TREE_VEC_ELT (parmvec
, 0) = arg1
;
19078 /* Setup the argument vector, which contains the remaining
19080 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
19081 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
19083 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
19084 argvec
, DEDUCE_EXACT
,
19085 /*subr=*/true, /*explain_p=*/false)
19088 /* We cannot deduce in the other direction, because ARG1 is
19089 a pack expansion but ARG2 is not. */
19092 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
19094 int i
, len1
= list_length (args1
);
19095 tree parmvec
= make_tree_vec (1);
19096 tree argvec
= make_tree_vec (len1
);
19099 /* Setup the parameter vector, which contains only ARG1. */
19100 TREE_VEC_ELT (parmvec
, 0) = arg2
;
19102 /* Setup the argument vector, which contains the remaining
19104 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
19105 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
19107 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
19108 argvec
, DEDUCE_EXACT
,
19109 /*subr=*/true, /*explain_p=*/false)
19112 /* We cannot deduce in the other direction, because ARG2 is
19113 a pack expansion but ARG1 is not.*/
19119 /* The normal case, where neither argument is a pack
19121 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
19122 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
19124 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
19125 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
19129 /* If we couldn't deduce arguments for tparms1 to make arg1 match
19130 arg2, then arg2 is not as specialized as arg1. */
19136 /* "If, for a given type, deduction succeeds in both directions
19137 (i.e., the types are identical after the transformations above)
19138 and both P and A were reference types (before being replaced with
19139 the type referred to above):
19140 - if the type from the argument template was an lvalue reference and
19141 the type from the parameter template was not, the argument type is
19142 considered to be more specialized than the other; otherwise,
19143 - if the type from the argument template is more cv-qualified
19144 than the type from the parameter template (as described above),
19145 the argument type is considered to be more specialized than the other;
19147 - neither type is more specialized than the other." */
19149 if (deduce1
&& deduce2
)
19151 if (ref1
&& ref2
&& ref1
!= ref2
)
19158 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
19160 if ((quals1
& quals2
) == quals2
)
19162 if ((quals1
& quals2
) == quals1
)
19167 if (lose1
&& lose2
)
19168 /* We've failed to deduce something in either direction.
19169 These must be unordered. */
19172 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
19173 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
19174 /* We have already processed all of the arguments in our
19175 handing of the pack expansion type. */
19178 args1
= TREE_CHAIN (args1
);
19179 args2
= TREE_CHAIN (args2
);
19182 /* "In most cases, all template parameters must have values in order for
19183 deduction to succeed, but for partial ordering purposes a template
19184 parameter may remain without a value provided it is not used in the
19185 types being used for partial ordering."
19187 Thus, if we are missing any of the targs1 we need to substitute into
19188 origs1, then pat2 is not as specialized as pat1. This can happen when
19189 there is a nondeduced context. */
19190 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
19192 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
19195 processing_template_decl
--;
19197 /* All things being equal, if the next argument is a pack expansion
19198 for one function but not for the other, prefer the
19199 non-variadic function. FIXME this is bogus; see c++/41958. */
19201 && args1
&& TREE_VALUE (args1
)
19202 && args2
&& TREE_VALUE (args2
))
19204 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
19205 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
19208 if (lose1
== lose2
)
19216 /* Determine which of two partial specializations of TMPL is more
19219 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
19220 to the first partial specialization. The TREE_PURPOSE is the
19221 innermost set of template parameters for the partial
19222 specialization. PAT2 is similar, but for the second template.
19224 Return 1 if the first partial specialization is more specialized;
19225 -1 if the second is more specialized; 0 if neither is more
19228 See [temp.class.order] for information about determining which of
19229 two templates is more specialized. */
19232 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
19236 bool any_deductions
= false;
19238 tree tmpl1
= TREE_VALUE (pat1
);
19239 tree tmpl2
= TREE_VALUE (pat2
);
19240 tree parms1
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl1
);
19241 tree parms2
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl2
);
19242 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
19243 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
19245 /* Just like what happens for functions, if we are ordering between
19246 different template specializations, we may encounter dependent
19247 types in the arguments, and we need our dependency check functions
19248 to behave correctly. */
19249 ++processing_template_decl
;
19250 targs
= get_partial_spec_bindings (tmpl
, parms1
, specargs1
, specargs2
);
19254 any_deductions
= true;
19257 targs
= get_partial_spec_bindings (tmpl
, parms2
, specargs2
, specargs1
);
19261 any_deductions
= true;
19263 --processing_template_decl
;
19265 /* In the case of a tie where at least one of the templates
19266 has a parameter pack at the end, the template with the most
19267 non-packed parameters wins. */
19270 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
19271 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
19273 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
19274 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
19275 int len1
= TREE_VEC_LENGTH (args1
);
19276 int len2
= TREE_VEC_LENGTH (args2
);
19278 /* We don't count the pack expansion at the end. */
19279 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
19281 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
19286 else if (len1
< len2
)
19293 /* Return the template arguments that will produce the function signature
19294 DECL from the function template FN, with the explicit template
19295 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
19296 also match. Return NULL_TREE if no satisfactory arguments could be
19300 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
19302 int ntparms
= DECL_NTPARMS (fn
);
19303 tree targs
= make_tree_vec (ntparms
);
19304 tree decl_type
= TREE_TYPE (decl
);
19305 tree decl_arg_types
;
19307 unsigned int nargs
, ix
;
19310 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
19312 /* Never do unification on the 'this' parameter. */
19313 decl_arg_types
= skip_artificial_parms_for (decl
,
19314 TYPE_ARG_TYPES (decl_type
));
19316 nargs
= list_length (decl_arg_types
);
19317 args
= XALLOCAVEC (tree
, nargs
);
19318 for (arg
= decl_arg_types
, ix
= 0;
19319 arg
!= NULL_TREE
&& arg
!= void_list_node
;
19320 arg
= TREE_CHAIN (arg
), ++ix
)
19321 args
[ix
] = TREE_VALUE (arg
);
19323 if (fn_type_unification (fn
, explicit_args
, targs
,
19325 (check_rettype
|| DECL_CONV_FN_P (fn
)
19326 ? TREE_TYPE (decl_type
) : NULL_TREE
),
19327 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
19329 == error_mark_node
)
19335 /* Return the innermost template arguments that, when applied to a partial
19336 specialization of TMPL whose innermost template parameters are
19337 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
19340 For example, suppose we have:
19342 template <class T, class U> struct S {};
19343 template <class T> struct S<T*, int> {};
19345 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
19346 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
19347 int}. The resulting vector will be {double}, indicating that `T'
19348 is bound to `double'. */
19351 get_partial_spec_bindings (tree tmpl
, tree tparms
, tree spec_args
, tree args
)
19353 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
19355 tree innermost_deduced_args
;
19357 innermost_deduced_args
= make_tree_vec (ntparms
);
19358 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
19360 deduced_args
= copy_node (args
);
19361 SET_TMPL_ARGS_LEVEL (deduced_args
,
19362 TMPL_ARGS_DEPTH (deduced_args
),
19363 innermost_deduced_args
);
19366 deduced_args
= innermost_deduced_args
;
19368 if (unify (tparms
, deduced_args
,
19369 INNERMOST_TEMPLATE_ARGS (spec_args
),
19370 INNERMOST_TEMPLATE_ARGS (args
),
19371 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
19374 for (i
= 0; i
< ntparms
; ++i
)
19375 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
19378 /* Verify that nondeduced template arguments agree with the type
19379 obtained from argument deduction.
19383 struct A { typedef int X; };
19384 template <class T, class U> struct C {};
19385 template <class T> struct C<T, typename T::X> {};
19387 Then with the instantiation `C<A, int>', we can deduce that
19388 `T' is `A' but unify () does not check whether `typename T::X'
19390 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
19391 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
19393 tf_none
, false, false);
19394 if (spec_args
== error_mark_node
19395 /* We only need to check the innermost arguments; the other
19396 arguments will always agree. */
19397 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
19398 INNERMOST_TEMPLATE_ARGS (args
)))
19401 /* Now that we have bindings for all of the template arguments,
19402 ensure that the arguments deduced for the template template
19403 parameters have compatible template parameter lists. See the use
19404 of template_template_parm_bindings_ok_p in fn_type_unification
19405 for more information. */
19406 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
19409 return deduced_args
;
19412 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
19413 Return the TREE_LIST node with the most specialized template, if
19414 any. If there is no most specialized template, the error_mark_node
19417 Note that this function does not look at, or modify, the
19418 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
19419 returned is one of the elements of INSTANTIATIONS, callers may
19420 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
19421 and retrieve it from the value returned. */
19424 most_specialized_instantiation (tree templates
)
19428 ++processing_template_decl
;
19431 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
19435 if (get_bindings (TREE_VALUE (champ
),
19436 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
19437 NULL_TREE
, /*check_ret=*/true))
19440 if (get_bindings (TREE_VALUE (fn
),
19441 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
19442 NULL_TREE
, /*check_ret=*/true))
19449 /* Equally specialized, move to next function. If there
19450 is no next function, nothing's most specialized. */
19451 fn
= TREE_CHAIN (fn
);
19459 /* Now verify that champ is better than everything earlier in the
19460 instantiation list. */
19461 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
19462 if (get_bindings (TREE_VALUE (champ
),
19463 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
19464 NULL_TREE
, /*check_ret=*/true)
19465 || !get_bindings (TREE_VALUE (fn
),
19466 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
19467 NULL_TREE
, /*check_ret=*/true))
19473 processing_template_decl
--;
19476 return error_mark_node
;
19481 /* If DECL is a specialization of some template, return the most
19482 general such template. Otherwise, returns NULL_TREE.
19484 For example, given:
19486 template <class T> struct S { template <class U> void f(U); };
19488 if TMPL is `template <class U> void S<int>::f(U)' this will return
19489 the full template. This function will not trace past partial
19490 specializations, however. For example, given in addition:
19492 template <class T> struct S<T*> { template <class U> void f(U); };
19494 if TMPL is `template <class U> void S<int*>::f(U)' this will return
19495 `template <class T> template <class U> S<T*>::f(U)'. */
19498 most_general_template (tree decl
)
19500 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
19502 if (tree tinfo
= get_template_info (decl
))
19503 decl
= TI_TEMPLATE (tinfo
);
19504 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
19505 template friend, or a FIELD_DECL for a capture pack. */
19506 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
19510 /* Look for more and more general templates. */
19511 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
19513 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
19514 (See cp-tree.h for details.) */
19515 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
19518 if (CLASS_TYPE_P (TREE_TYPE (decl
))
19519 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
19520 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
19523 /* Stop if we run into an explicitly specialized class template. */
19524 if (!DECL_NAMESPACE_SCOPE_P (decl
)
19525 && DECL_CONTEXT (decl
)
19526 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
19529 decl
= DECL_TI_TEMPLATE (decl
);
19535 /* Return the most specialized of the template partial specializations
19536 which can produce TARGET, a specialization of some class or variable
19537 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
19538 a TEMPLATE_DECL node corresponding to the partial specialization, while
19539 the TREE_PURPOSE is the set of template arguments that must be
19540 substituted into the template pattern in order to generate TARGET.
19542 If the choice of partial specialization is ambiguous, a diagnostic
19543 is issued, and the error_mark_node is returned. If there are no
19544 partial specializations matching TARGET, then NULL_TREE is
19545 returned, indicating that the primary template should be used. */
19548 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
19550 tree list
= NULL_TREE
;
19555 tree outer_args
= NULL_TREE
;
19558 if (TYPE_P (target
))
19560 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
19561 tmpl
= TI_TEMPLATE (tinfo
);
19562 args
= TI_ARGS (tinfo
);
19564 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
19566 tmpl
= TREE_OPERAND (target
, 0);
19567 args
= TREE_OPERAND (target
, 1);
19569 else if (VAR_P (target
))
19571 tree tinfo
= DECL_TEMPLATE_INFO (target
);
19572 tmpl
= TI_TEMPLATE (tinfo
);
19573 args
= TI_ARGS (tinfo
);
19576 gcc_unreachable ();
19578 tree main_tmpl
= most_general_template (tmpl
);
19580 /* For determining which partial specialization to use, only the
19581 innermost args are interesting. */
19582 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
19584 outer_args
= strip_innermost_template_args (args
, 1);
19585 args
= INNERMOST_TEMPLATE_ARGS (args
);
19588 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
19590 tree partial_spec_args
;
19592 tree spec_tmpl
= TREE_VALUE (t
);
19594 partial_spec_args
= TREE_PURPOSE (t
);
19596 ++processing_template_decl
;
19600 /* Discard the outer levels of args, and then substitute in the
19601 template args from the enclosing class. */
19602 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
19603 partial_spec_args
= tsubst_template_args
19604 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
19606 /* And the same for the partial specialization TEMPLATE_DECL. */
19607 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
19610 partial_spec_args
=
19611 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
19614 /*require_all_args=*/true,
19615 /*use_default_args=*/true);
19617 --processing_template_decl
;
19619 if (partial_spec_args
== error_mark_node
)
19620 return error_mark_node
;
19621 if (spec_tmpl
== error_mark_node
)
19622 return error_mark_node
;
19624 tree parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
19625 spec_args
= get_partial_spec_bindings (tmpl
, parms
,
19631 spec_args
= add_to_template_args (outer_args
, spec_args
);
19632 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
19633 TREE_TYPE (list
) = TREE_TYPE (t
);
19640 ambiguous_p
= false;
19643 t
= TREE_CHAIN (t
);
19644 for (; t
; t
= TREE_CHAIN (t
))
19646 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
19653 t
= TREE_CHAIN (t
);
19656 ambiguous_p
= true;
19665 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
19667 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
19670 ambiguous_p
= true;
19678 char *spaces
= NULL
;
19679 if (!(complain
& tf_error
))
19680 return error_mark_node
;
19681 if (TYPE_P (target
))
19682 error ("ambiguous template instantiation for %q#T", target
);
19684 error ("ambiguous template instantiation for %q#D", target
);
19685 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
19686 for (t
= list
; t
; t
= TREE_CHAIN (t
))
19688 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
19689 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
19690 "%s %#S", spaces
? spaces
: str
, subst
);
19691 spaces
= spaces
? spaces
: get_spaces (str
);
19694 return error_mark_node
;
19700 /* Explicitly instantiate DECL. */
19703 do_decl_instantiation (tree decl
, tree storage
)
19705 tree result
= NULL_TREE
;
19708 if (!decl
|| decl
== error_mark_node
)
19709 /* An error occurred, for which grokdeclarator has already issued
19710 an appropriate message. */
19712 else if (! DECL_LANG_SPECIFIC (decl
))
19714 error ("explicit instantiation of non-template %q#D", decl
);
19718 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
19719 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
19721 if (VAR_P (decl
) && !var_templ
)
19723 /* There is an asymmetry here in the way VAR_DECLs and
19724 FUNCTION_DECLs are handled by grokdeclarator. In the case of
19725 the latter, the DECL we get back will be marked as a
19726 template instantiation, and the appropriate
19727 DECL_TEMPLATE_INFO will be set up. This does not happen for
19728 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
19729 should handle VAR_DECLs as it currently handles
19731 if (!DECL_CLASS_SCOPE_P (decl
))
19733 error ("%qD is not a static data member of a class template", decl
);
19736 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
19737 if (!result
|| !VAR_P (result
))
19739 error ("no matching template for %qD found", decl
);
19742 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
19744 error ("type %qT for explicit instantiation %qD does not match "
19745 "declared type %qT", TREE_TYPE (result
), decl
,
19750 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
19752 error ("explicit instantiation of %q#D", decl
);
19758 /* Check for various error cases. Note that if the explicit
19759 instantiation is valid the RESULT will currently be marked as an
19760 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
19761 until we get here. */
19763 if (DECL_TEMPLATE_SPECIALIZATION (result
))
19765 /* DR 259 [temp.spec].
19767 Both an explicit instantiation and a declaration of an explicit
19768 specialization shall not appear in a program unless the explicit
19769 instantiation follows a declaration of the explicit specialization.
19771 For a given set of template parameters, if an explicit
19772 instantiation of a template appears after a declaration of an
19773 explicit specialization for that template, the explicit
19774 instantiation has no effect. */
19777 else if (DECL_EXPLICIT_INSTANTIATION (result
))
19781 No program shall explicitly instantiate any template more
19784 We check DECL_NOT_REALLY_EXTERN so as not to complain when
19785 the first instantiation was `extern' and the second is not,
19786 and EXTERN_P for the opposite case. */
19787 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
19788 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
19789 /* If an "extern" explicit instantiation follows an ordinary
19790 explicit instantiation, the template is instantiated. */
19794 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
19796 error ("no matching template for %qD found", result
);
19799 else if (!DECL_TEMPLATE_INFO (result
))
19801 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
19805 if (storage
== NULL_TREE
)
19807 else if (storage
== ridpointers
[(int) RID_EXTERN
])
19809 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
19810 pedwarn (input_location
, OPT_Wpedantic
,
19811 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
19816 error ("storage class %qD applied to template instantiation", storage
);
19818 check_explicit_instantiation_namespace (result
);
19819 mark_decl_instantiated (result
, extern_p
);
19821 instantiate_decl (result
, /*defer_ok=*/1,
19822 /*expl_inst_class_mem_p=*/false);
19826 mark_class_instantiated (tree t
, int extern_p
)
19828 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
19829 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
19830 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
19831 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
19834 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
19835 rest_of_type_compilation (t
, 1);
19839 /* Called from do_type_instantiation through binding_table_foreach to
19840 do recursive instantiation for the type bound in ENTRY. */
19842 bt_instantiate_type_proc (binding_entry entry
, void *data
)
19844 tree storage
= *(tree
*) data
;
19846 if (MAYBE_CLASS_TYPE_P (entry
->type
)
19847 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
19848 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
19851 /* Called from do_type_instantiation to instantiate a member
19852 (a member function or a static member variable) of an
19853 explicitly instantiated class template. */
19855 instantiate_class_member (tree decl
, int extern_p
)
19857 mark_decl_instantiated (decl
, extern_p
);
19859 instantiate_decl (decl
, /*defer_ok=*/1,
19860 /*expl_inst_class_mem_p=*/true);
19863 /* Perform an explicit instantiation of template class T. STORAGE, if
19864 non-null, is the RID for extern, inline or static. COMPLAIN is
19865 nonzero if this is called from the parser, zero if called recursively,
19866 since the standard is unclear (as detailed below). */
19869 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
19874 int previous_instantiation_extern_p
= 0;
19876 if (TREE_CODE (t
) == TYPE_DECL
)
19879 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
19882 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
19884 error ("explicit instantiation of non-class template %qD", tmpl
);
19886 error ("explicit instantiation of non-template type %qT", t
);
19892 if (!COMPLETE_TYPE_P (t
))
19894 if (complain
& tf_error
)
19895 error ("explicit instantiation of %q#T before definition of template",
19900 if (storage
!= NULL_TREE
)
19902 if (!in_system_header_at (input_location
))
19904 if (storage
== ridpointers
[(int) RID_EXTERN
])
19906 if (cxx_dialect
== cxx98
)
19907 pedwarn (input_location
, OPT_Wpedantic
,
19908 "ISO C++ 1998 forbids the use of %<extern%> on "
19909 "explicit instantiations");
19912 pedwarn (input_location
, OPT_Wpedantic
,
19913 "ISO C++ forbids the use of %qE"
19914 " on explicit instantiations", storage
);
19917 if (storage
== ridpointers
[(int) RID_INLINE
])
19919 else if (storage
== ridpointers
[(int) RID_EXTERN
])
19921 else if (storage
== ridpointers
[(int) RID_STATIC
])
19925 error ("storage class %qD applied to template instantiation",
19931 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
19933 /* DR 259 [temp.spec].
19935 Both an explicit instantiation and a declaration of an explicit
19936 specialization shall not appear in a program unless the explicit
19937 instantiation follows a declaration of the explicit specialization.
19939 For a given set of template parameters, if an explicit
19940 instantiation of a template appears after a declaration of an
19941 explicit specialization for that template, the explicit
19942 instantiation has no effect. */
19945 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
19949 No program shall explicitly instantiate any template more
19952 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
19953 instantiation was `extern'. If EXTERN_P then the second is.
19954 These cases are OK. */
19955 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
19957 if (!previous_instantiation_extern_p
&& !extern_p
19958 && (complain
& tf_error
))
19959 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
19961 /* If we've already instantiated the template, just return now. */
19962 if (!CLASSTYPE_INTERFACE_ONLY (t
))
19966 check_explicit_instantiation_namespace (TYPE_NAME (t
));
19967 mark_class_instantiated (t
, extern_p
);
19975 /* In contrast to implicit instantiation, where only the
19976 declarations, and not the definitions, of members are
19977 instantiated, we have here:
19981 The explicit instantiation of a class template specialization
19982 implies the instantiation of all of its members not
19983 previously explicitly specialized in the translation unit
19984 containing the explicit instantiation.
19986 Of course, we can't instantiate member template classes, since
19987 we don't have any arguments for them. Note that the standard
19988 is unclear on whether the instantiation of the members are
19989 *explicit* instantiations or not. However, the most natural
19990 interpretation is that it should be an explicit instantiation. */
19993 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
19994 if (TREE_CODE (tmp
) == FUNCTION_DECL
19995 && DECL_TEMPLATE_INSTANTIATION (tmp
))
19996 instantiate_class_member (tmp
, extern_p
);
19998 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
19999 if (VAR_P (tmp
) && DECL_TEMPLATE_INSTANTIATION (tmp
))
20000 instantiate_class_member (tmp
, extern_p
);
20002 if (CLASSTYPE_NESTED_UTDS (t
))
20003 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
20004 bt_instantiate_type_proc
, &storage
);
20008 /* Given a function DECL, which is a specialization of TMPL, modify
20009 DECL to be a re-instantiation of TMPL with the same template
20010 arguments. TMPL should be the template into which tsubst'ing
20011 should occur for DECL, not the most general template.
20013 One reason for doing this is a scenario like this:
20016 void f(const T&, int i);
20018 void g() { f(3, 7); }
20021 void f(const T& t, const int i) { }
20023 Note that when the template is first instantiated, with
20024 instantiate_template, the resulting DECL will have no name for the
20025 first parameter, and the wrong type for the second. So, when we go
20026 to instantiate the DECL, we regenerate it. */
20029 regenerate_decl_from_template (tree decl
, tree tmpl
)
20031 /* The arguments used to instantiate DECL, from the most general
20036 args
= DECL_TI_ARGS (decl
);
20037 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
20039 /* Make sure that we can see identifiers, and compute access
20041 push_access_scope (decl
);
20043 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20051 args_depth
= TMPL_ARGS_DEPTH (args
);
20052 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
20053 if (args_depth
> parms_depth
)
20054 args
= get_innermost_template_args (args
, parms_depth
);
20056 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
20057 args
, tf_error
, NULL_TREE
,
20058 /*defer_ok*/false);
20059 if (specs
&& specs
!= error_mark_node
)
20060 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
20063 /* Merge parameter declarations. */
20064 decl_parm
= skip_artificial_parms_for (decl
,
20065 DECL_ARGUMENTS (decl
));
20067 = skip_artificial_parms_for (code_pattern
,
20068 DECL_ARGUMENTS (code_pattern
));
20069 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
20074 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
20075 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
20076 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
20078 parm_type
= type_decays_to (parm_type
);
20079 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
20080 TREE_TYPE (decl_parm
) = parm_type
;
20081 attributes
= DECL_ATTRIBUTES (pattern_parm
);
20082 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
20084 DECL_ATTRIBUTES (decl_parm
) = attributes
;
20085 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
20087 decl_parm
= DECL_CHAIN (decl_parm
);
20088 pattern_parm
= DECL_CHAIN (pattern_parm
);
20090 /* Merge any parameters that match with the function parameter
20092 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
20095 tree expanded_types
;
20096 /* Expand the TYPE_PACK_EXPANSION that provides the types for
20097 the parameters in this function parameter pack. */
20098 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
20099 args
, tf_error
, NULL_TREE
);
20100 len
= TREE_VEC_LENGTH (expanded_types
);
20101 for (i
= 0; i
< len
; i
++)
20106 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
20107 /* Rename the parameter to include the index. */
20108 DECL_NAME (decl_parm
) =
20109 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
20110 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
20111 parm_type
= type_decays_to (parm_type
);
20112 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
20113 TREE_TYPE (decl_parm
) = parm_type
;
20114 attributes
= DECL_ATTRIBUTES (pattern_parm
);
20115 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
20117 DECL_ATTRIBUTES (decl_parm
) = attributes
;
20118 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
20120 decl_parm
= DECL_CHAIN (decl_parm
);
20123 /* Merge additional specifiers from the CODE_PATTERN. */
20124 if (DECL_DECLARED_INLINE_P (code_pattern
)
20125 && !DECL_DECLARED_INLINE_P (decl
))
20126 DECL_DECLARED_INLINE_P (decl
) = 1;
20128 else if (VAR_P (decl
))
20130 DECL_INITIAL (decl
) =
20131 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
20132 tf_error
, DECL_TI_TEMPLATE (decl
),
20133 /*integral_constant_expression_p=*/false);
20134 if (VAR_HAD_UNKNOWN_BOUND (decl
))
20135 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
20136 tf_error
, DECL_TI_TEMPLATE (decl
));
20139 gcc_unreachable ();
20141 pop_access_scope (decl
);
20144 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
20145 substituted to get DECL. */
20148 template_for_substitution (tree decl
)
20150 tree tmpl
= DECL_TI_TEMPLATE (decl
);
20152 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
20153 for the instantiation. This is not always the most general
20154 template. Consider, for example:
20157 struct S { template <class U> void f();
20158 template <> void f<int>(); };
20160 and an instantiation of S<double>::f<int>. We want TD to be the
20161 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
20162 while (/* An instantiation cannot have a definition, so we need a
20163 more general template. */
20164 DECL_TEMPLATE_INSTANTIATION (tmpl
)
20165 /* We must also deal with friend templates. Given:
20167 template <class T> struct S {
20168 template <class U> friend void f() {};
20171 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
20172 so far as the language is concerned, but that's still
20173 where we get the pattern for the instantiation from. On
20174 other hand, if the definition comes outside the class, say:
20176 template <class T> struct S {
20177 template <class U> friend void f();
20179 template <class U> friend void f() {}
20181 we don't need to look any further. That's what the check for
20182 DECL_INITIAL is for. */
20183 || (TREE_CODE (decl
) == FUNCTION_DECL
20184 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
20185 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
20187 /* The present template, TD, should not be a definition. If it
20188 were a definition, we should be using it! Note that we
20189 cannot restructure the loop to just keep going until we find
20190 a template with a definition, since that might go too far if
20191 a specialization was declared, but not defined. */
20193 /* Fetch the more general template. */
20194 tmpl
= DECL_TI_TEMPLATE (tmpl
);
20200 /* Returns true if we need to instantiate this template instance even if we
20201 know we aren't going to emit it. */
20204 always_instantiate_p (tree decl
)
20206 /* We always instantiate inline functions so that we can inline them. An
20207 explicit instantiation declaration prohibits implicit instantiation of
20208 non-inline functions. With high levels of optimization, we would
20209 normally inline non-inline functions -- but we're not allowed to do
20210 that for "extern template" functions. Therefore, we check
20211 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
20212 return ((TREE_CODE (decl
) == FUNCTION_DECL
20213 && (DECL_DECLARED_INLINE_P (decl
)
20214 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
20215 /* And we need to instantiate static data members so that
20216 their initializers are available in integral constant
20219 && decl_maybe_constant_var_p (decl
)));
20222 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
20223 instantiate it now, modifying TREE_TYPE (fn). */
20226 maybe_instantiate_noexcept (tree fn
)
20228 tree fntype
, spec
, noex
, clone
;
20230 /* Don't instantiate a noexcept-specification from template context. */
20231 if (processing_template_decl
)
20234 if (DECL_CLONED_FUNCTION_P (fn
))
20235 fn
= DECL_CLONED_FUNCTION (fn
);
20236 fntype
= TREE_TYPE (fn
);
20237 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
20239 if (!spec
|| !TREE_PURPOSE (spec
))
20242 noex
= TREE_PURPOSE (spec
);
20244 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
20246 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
20247 spec
= get_defaulted_eh_spec (fn
);
20248 else if (push_tinst_level (fn
))
20250 push_access_scope (fn
);
20251 push_deferring_access_checks (dk_no_deferred
);
20252 input_location
= DECL_SOURCE_LOCATION (fn
);
20253 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
20254 DEFERRED_NOEXCEPT_ARGS (noex
),
20255 tf_warning_or_error
, fn
,
20256 /*function_p=*/false,
20257 /*integral_constant_expression_p=*/true);
20258 pop_deferring_access_checks ();
20259 pop_access_scope (fn
);
20260 pop_tinst_level ();
20261 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
20262 if (spec
== error_mark_node
)
20263 spec
= noexcept_false_spec
;
20266 spec
= noexcept_false_spec
;
20268 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
20271 FOR_EACH_CLONE (clone
, fn
)
20273 if (TREE_TYPE (clone
) == fntype
)
20274 TREE_TYPE (clone
) = TREE_TYPE (fn
);
20276 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
20280 /* Produce the definition of D, a _DECL generated from a template. If
20281 DEFER_OK is nonzero, then we don't have to actually do the
20282 instantiation now; we just have to do it sometime. Normally it is
20283 an error if this is an explicit instantiation but D is undefined.
20284 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
20285 explicitly instantiated class template. */
20288 instantiate_decl (tree d
, int defer_ok
,
20289 bool expl_inst_class_mem_p
)
20291 tree tmpl
= DECL_TI_TEMPLATE (d
);
20298 bool pattern_defined
;
20299 location_t saved_loc
= input_location
;
20300 int saved_unevaluated_operand
= cp_unevaluated_operand
;
20301 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
20307 /* This function should only be used to instantiate templates for
20308 functions and static member variables. */
20309 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
20311 /* Variables are never deferred; if instantiation is required, they
20312 are instantiated right away. That allows for better code in the
20313 case that an expression refers to the value of the variable --
20314 if the variable has a constant value the referring expression can
20315 take advantage of that fact. */
20317 || DECL_DECLARED_CONSTEXPR_P (d
))
20320 /* Don't instantiate cloned functions. Instead, instantiate the
20321 functions they cloned. */
20322 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
20323 d
= DECL_CLONED_FUNCTION (d
);
20325 if (DECL_TEMPLATE_INSTANTIATED (d
)
20326 || (TREE_CODE (d
) == FUNCTION_DECL
20327 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
20328 || DECL_TEMPLATE_SPECIALIZATION (d
))
20329 /* D has already been instantiated or explicitly specialized, so
20330 there's nothing for us to do here.
20332 It might seem reasonable to check whether or not D is an explicit
20333 instantiation, and, if so, stop here. But when an explicit
20334 instantiation is deferred until the end of the compilation,
20335 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
20336 the instantiation. */
20339 /* Check to see whether we know that this template will be
20340 instantiated in some other file, as with "extern template"
20342 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
20344 /* In general, we do not instantiate such templates. */
20345 if (external_p
&& !always_instantiate_p (d
))
20348 gen_tmpl
= most_general_template (tmpl
);
20349 gen_args
= DECL_TI_ARGS (d
);
20351 if (tmpl
!= gen_tmpl
)
20352 /* We should already have the extra args. */
20353 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
20354 == TMPL_ARGS_DEPTH (gen_args
));
20355 /* And what's in the hash table should match D. */
20356 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
20357 || spec
== NULL_TREE
);
20359 /* This needs to happen before any tsubsting. */
20360 if (! push_tinst_level (d
))
20363 timevar_push (TV_TEMPLATE_INST
);
20365 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
20366 for the instantiation. */
20367 td
= template_for_substitution (d
);
20368 code_pattern
= DECL_TEMPLATE_RESULT (td
);
20370 /* We should never be trying to instantiate a member of a class
20371 template or partial specialization. */
20372 gcc_assert (d
!= code_pattern
);
20374 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
20375 || DECL_TEMPLATE_SPECIALIZATION (td
))
20376 /* In the case of a friend template whose definition is provided
20377 outside the class, we may have too many arguments. Drop the
20378 ones we don't need. The same is true for specializations. */
20379 args
= get_innermost_template_args
20380 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
20384 if (TREE_CODE (d
) == FUNCTION_DECL
)
20386 deleted_p
= DECL_DELETED_FN (code_pattern
);
20387 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
20388 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
)
20394 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
20397 /* We may be in the middle of deferred access check. Disable it now. */
20398 push_deferring_access_checks (dk_no_deferred
);
20400 /* Unless an explicit instantiation directive has already determined
20401 the linkage of D, remember that a definition is available for
20403 if (pattern_defined
20404 && !DECL_INTERFACE_KNOWN (d
)
20405 && !DECL_NOT_REALLY_EXTERN (d
))
20406 mark_definable (d
);
20408 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
20409 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
20410 input_location
= DECL_SOURCE_LOCATION (d
);
20412 /* If D is a member of an explicitly instantiated class template,
20413 and no definition is available, treat it like an implicit
20415 if (!pattern_defined
&& expl_inst_class_mem_p
20416 && DECL_EXPLICIT_INSTANTIATION (d
))
20418 /* Leave linkage flags alone on instantiations with anonymous
20420 if (TREE_PUBLIC (d
))
20422 DECL_NOT_REALLY_EXTERN (d
) = 0;
20423 DECL_INTERFACE_KNOWN (d
) = 0;
20425 SET_DECL_IMPLICIT_INSTANTIATION (d
);
20428 /* Defer all other templates, unless we have been explicitly
20429 forbidden from doing so. */
20430 if (/* If there is no definition, we cannot instantiate the
20433 /* If it's OK to postpone instantiation, do so. */
20435 /* If this is a static data member that will be defined
20436 elsewhere, we don't want to instantiate the entire data
20437 member, but we do want to instantiate the initializer so that
20438 we can substitute that elsewhere. */
20439 || (external_p
&& VAR_P (d
))
20440 /* Handle here a deleted function too, avoid generating
20441 its body (c++/61080). */
20444 /* The definition of the static data member is now required so
20445 we must substitute the initializer. */
20447 && !DECL_INITIAL (d
)
20448 && DECL_INITIAL (code_pattern
))
20452 bool const_init
= false;
20453 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
20455 ns
= decl_namespace_context (d
);
20456 push_nested_namespace (ns
);
20458 push_nested_class (DECL_CONTEXT (d
));
20459 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
20461 tf_warning_or_error
, NULL_TREE
,
20462 /*integral_constant_expression_p=*/false);
20463 /* If instantiating the initializer involved instantiating this
20464 again, don't call cp_finish_decl twice. */
20465 if (!DECL_INITIAL (d
))
20467 /* Make sure the initializer is still constant, in case of
20468 circular dependency (template/instantiate6.C). */
20470 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
20471 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
20472 /*asmspec_tree=*/NULL_TREE
,
20473 LOOKUP_ONLYCONVERTING
);
20476 pop_nested_class ();
20477 pop_nested_namespace (ns
);
20480 /* We restore the source position here because it's used by
20481 add_pending_template. */
20482 input_location
= saved_loc
;
20484 if (at_eof
&& !pattern_defined
20485 && DECL_EXPLICIT_INSTANTIATION (d
)
20486 && DECL_NOT_REALLY_EXTERN (d
))
20489 The definition of a non-exported function template, a
20490 non-exported member function template, or a non-exported
20491 member function or static data member of a class template
20492 shall be present in every translation unit in which it is
20493 explicitly instantiated. */
20494 permerror (input_location
, "explicit instantiation of %qD "
20495 "but no definition available", d
);
20497 /* If we're in unevaluated context, we just wanted to get the
20498 constant value; this isn't an odr use, so don't queue
20499 a full instantiation. */
20500 if (cp_unevaluated_operand
!= 0)
20502 /* ??? Historically, we have instantiated inline functions, even
20503 when marked as "extern template". */
20504 if (!(external_p
&& VAR_P (d
)))
20505 add_pending_template (d
);
20508 /* Tell the repository that D is available in this translation unit
20509 -- and see if it is supposed to be instantiated here. */
20510 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
20512 /* In a PCH file, despite the fact that the repository hasn't
20513 requested instantiation in the PCH it is still possible that
20514 an instantiation will be required in a file that includes the
20517 add_pending_template (d
);
20518 /* Instantiate inline functions so that the inliner can do its
20519 job, even though we'll not be emitting a copy of this
20521 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
20525 fn_context
= decl_function_context (d
);
20526 nested
= (current_function_decl
!= NULL_TREE
);
20528 push_to_top_level ();
20532 push_function_context ();
20533 cp_unevaluated_operand
= 0;
20534 c_inhibit_evaluation_warnings
= 0;
20537 /* Mark D as instantiated so that recursive calls to
20538 instantiate_decl do not try to instantiate it again. */
20539 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
20541 /* Regenerate the declaration in case the template has been modified
20542 by a subsequent redeclaration. */
20543 regenerate_decl_from_template (d
, td
);
20545 /* We already set the file and line above. Reset them now in case
20546 they changed as a result of calling regenerate_decl_from_template. */
20547 input_location
= DECL_SOURCE_LOCATION (d
);
20552 bool const_init
= false;
20554 /* Clear out DECL_RTL; whatever was there before may not be right
20555 since we've reset the type of the declaration. */
20556 SET_DECL_RTL (d
, NULL
);
20557 DECL_IN_AGGR_P (d
) = 0;
20559 /* The initializer is placed in DECL_INITIAL by
20560 regenerate_decl_from_template so we don't need to
20561 push/pop_access_scope again here. Pull it out so that
20562 cp_finish_decl can process it. */
20563 init
= DECL_INITIAL (d
);
20564 DECL_INITIAL (d
) = NULL_TREE
;
20565 DECL_INITIALIZED_P (d
) = 0;
20567 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
20568 initializer. That function will defer actual emission until
20569 we have a chance to determine linkage. */
20570 DECL_EXTERNAL (d
) = 0;
20572 /* Enter the scope of D so that access-checking works correctly. */
20573 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
20575 push_nested_class (DECL_CONTEXT (d
));
20577 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
20578 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
20581 pop_nested_class ();
20583 if (variable_template_p (td
))
20584 note_variable_template_instantiation (d
);
20586 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
20587 synthesize_method (d
);
20588 else if (TREE_CODE (d
) == FUNCTION_DECL
)
20590 hash_map
<tree
, tree
> *saved_local_specializations
;
20594 tree block
= NULL_TREE
;
20596 /* Save away the current list, in case we are instantiating one
20597 template from within the body of another. */
20598 saved_local_specializations
= local_specializations
;
20600 /* Set up the list of local specializations. */
20601 local_specializations
= new hash_map
<tree
, tree
>;
20603 /* Set up context. */
20604 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
20605 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
20606 block
= push_stmt_list ();
20608 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
20610 /* Some typedefs referenced from within the template code need to be
20611 access checked at template instantiation time, i.e now. These
20612 types were added to the template at parsing time. Let's get those
20613 and perform the access checks then. */
20614 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl
),
20617 /* Create substitution entries for the parameters. */
20618 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
20619 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
20620 spec_parm
= DECL_ARGUMENTS (d
);
20621 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
20623 register_local_specialization (spec_parm
, tmpl_parm
);
20624 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
20625 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
20627 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
20629 if (!DECL_PACK_P (tmpl_parm
))
20631 register_local_specialization (spec_parm
, tmpl_parm
);
20632 spec_parm
= DECL_CHAIN (spec_parm
);
20636 /* Register the (value) argument pack as a specialization of
20637 TMPL_PARM, then move on. */
20638 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
20639 register_local_specialization (argpack
, tmpl_parm
);
20642 gcc_assert (!spec_parm
);
20644 /* Substitute into the body of the function. */
20645 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
20646 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
20647 tf_warning_or_error
, tmpl
);
20650 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
20651 tf_warning_or_error
, tmpl
,
20652 /*integral_constant_expression_p=*/false);
20654 /* Set the current input_location to the end of the function
20655 so that finish_function knows where we are. */
20657 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
20659 /* Remember if we saw an infinite loop in the template. */
20660 current_function_infinite_loop
20661 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
20664 /* We don't need the local specializations any more. */
20665 delete local_specializations
;
20666 local_specializations
= saved_local_specializations
;
20668 /* Finish the function. */
20669 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
20670 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
20671 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
20674 d
= finish_function (0);
20675 expand_or_defer_fn (d
);
20678 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
20679 cp_check_omp_declare_reduction (d
);
20682 /* We're not deferring instantiation any more. */
20683 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
20686 pop_from_top_level ();
20688 pop_function_context ();
20691 input_location
= saved_loc
;
20692 cp_unevaluated_operand
= saved_unevaluated_operand
;
20693 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
20694 pop_deferring_access_checks ();
20695 pop_tinst_level ();
20697 timevar_pop (TV_TEMPLATE_INST
);
20702 /* Run through the list of templates that we wish we could
20703 instantiate, and instantiate any we can. RETRIES is the
20704 number of times we retry pending template instantiation. */
20707 instantiate_pending_templates (int retries
)
20710 location_t saved_loc
= input_location
;
20712 /* Instantiating templates may trigger vtable generation. This in turn
20713 may require further template instantiations. We place a limit here
20714 to avoid infinite loop. */
20715 if (pending_templates
&& retries
>= max_tinst_depth
)
20717 tree decl
= pending_templates
->tinst
->decl
;
20719 fatal_error (input_location
,
20720 "template instantiation depth exceeds maximum of %d"
20721 " instantiating %q+D, possibly from virtual table generation"
20722 " (use -ftemplate-depth= to increase the maximum)",
20723 max_tinst_depth
, decl
);
20724 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20725 /* Pretend that we defined it. */
20726 DECL_INITIAL (decl
) = error_mark_node
;
20732 struct pending_template
**t
= &pending_templates
;
20733 struct pending_template
*last
= NULL
;
20737 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
20738 bool complete
= false;
20740 if (TYPE_P (instantiation
))
20744 if (!COMPLETE_TYPE_P (instantiation
))
20746 instantiate_class_template (instantiation
);
20747 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
20748 for (fn
= TYPE_METHODS (instantiation
);
20750 fn
= TREE_CHAIN (fn
))
20751 if (! DECL_ARTIFICIAL (fn
))
20752 instantiate_decl (fn
,
20754 /*expl_inst_class_mem_p=*/false);
20755 if (COMPLETE_TYPE_P (instantiation
))
20759 complete
= COMPLETE_TYPE_P (instantiation
);
20763 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
20764 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
20767 = instantiate_decl (instantiation
,
20769 /*expl_inst_class_mem_p=*/false);
20770 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
20774 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
20775 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
20779 /* If INSTANTIATION has been instantiated, then we don't
20780 need to consider it again in the future. */
20788 current_tinst_level
= NULL
;
20790 last_pending_template
= last
;
20792 while (reconsider
);
20794 input_location
= saved_loc
;
20797 /* Substitute ARGVEC into T, which is a list of initializers for
20798 either base class or a non-static data member. The TREE_PURPOSEs
20799 are DECLs, and the TREE_VALUEs are the initializer values. Used by
20800 instantiate_decl. */
20803 tsubst_initializer_list (tree t
, tree argvec
)
20805 tree inits
= NULL_TREE
;
20807 for (; t
; t
= TREE_CHAIN (t
))
20811 tree expanded_bases
= NULL_TREE
;
20812 tree expanded_arguments
= NULL_TREE
;
20815 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
20820 /* Expand the base class expansion type into separate base
20822 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
20823 tf_warning_or_error
,
20825 if (expanded_bases
== error_mark_node
)
20828 /* We'll be building separate TREE_LISTs of arguments for
20830 len
= TREE_VEC_LENGTH (expanded_bases
);
20831 expanded_arguments
= make_tree_vec (len
);
20832 for (i
= 0; i
< len
; i
++)
20833 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
20835 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
20836 expand each argument in the TREE_VALUE of t. */
20837 expr
= make_node (EXPR_PACK_EXPANSION
);
20838 PACK_EXPANSION_LOCAL_P (expr
) = true;
20839 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
20840 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
20842 if (TREE_VALUE (t
) == void_type_node
)
20843 /* VOID_TYPE_NODE is used to indicate
20844 value-initialization. */
20846 for (i
= 0; i
< len
; i
++)
20847 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
20851 /* Substitute parameter packs into each argument in the
20853 in_base_initializer
= 1;
20854 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
20856 tree expanded_exprs
;
20858 /* Expand the argument. */
20859 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
20861 = tsubst_pack_expansion (expr
, argvec
,
20862 tf_warning_or_error
,
20864 if (expanded_exprs
== error_mark_node
)
20867 /* Prepend each of the expanded expressions to the
20868 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
20869 for (i
= 0; i
< len
; i
++)
20871 TREE_VEC_ELT (expanded_arguments
, i
) =
20872 tree_cons (NULL_TREE
,
20873 TREE_VEC_ELT (expanded_exprs
, i
),
20874 TREE_VEC_ELT (expanded_arguments
, i
));
20877 in_base_initializer
= 0;
20879 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
20880 since we built them backwards. */
20881 for (i
= 0; i
< len
; i
++)
20883 TREE_VEC_ELT (expanded_arguments
, i
) =
20884 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
20889 for (i
= 0; i
< len
; ++i
)
20891 if (expanded_bases
)
20893 decl
= TREE_VEC_ELT (expanded_bases
, i
);
20894 decl
= expand_member_init (decl
);
20895 init
= TREE_VEC_ELT (expanded_arguments
, i
);
20900 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
20901 tf_warning_or_error
, NULL_TREE
);
20903 decl
= expand_member_init (decl
);
20904 if (decl
&& !DECL_P (decl
))
20905 in_base_initializer
= 1;
20907 init
= TREE_VALUE (t
);
20909 if (init
!= void_type_node
)
20910 init
= tsubst_expr (init
, argvec
,
20911 tf_warning_or_error
, NULL_TREE
,
20912 /*integral_constant_expression_p=*/false);
20913 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
20914 /* If we had an initializer but it instantiated to nothing,
20915 value-initialize the object. This will only occur when
20916 the initializer was a pack expansion where the parameter
20917 packs used in that expansion were of length zero. */
20918 init
= void_type_node
;
20919 in_base_initializer
= 0;
20924 init
= build_tree_list (decl
, init
);
20925 TREE_CHAIN (init
) = inits
;
20933 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
20936 set_current_access_from_decl (tree decl
)
20938 if (TREE_PRIVATE (decl
))
20939 current_access_specifier
= access_private_node
;
20940 else if (TREE_PROTECTED (decl
))
20941 current_access_specifier
= access_protected_node
;
20943 current_access_specifier
= access_public_node
;
20946 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
20947 is the instantiation (which should have been created with
20948 start_enum) and ARGS are the template arguments to use. */
20951 tsubst_enum (tree tag
, tree newtag
, tree args
)
20955 if (SCOPED_ENUM_P (newtag
))
20956 begin_scope (sk_scoped_enum
, newtag
);
20958 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
20963 decl
= TREE_VALUE (e
);
20964 /* Note that in a template enum, the TREE_VALUE is the
20965 CONST_DECL, not the corresponding INTEGER_CST. */
20966 value
= tsubst_expr (DECL_INITIAL (decl
),
20967 args
, tf_warning_or_error
, NULL_TREE
,
20968 /*integral_constant_expression_p=*/true);
20970 /* Give this enumeration constant the correct access. */
20971 set_current_access_from_decl (decl
);
20973 /* Actually build the enumerator itself. Here we're assuming that
20974 enumerators can't have dependent attributes. */
20975 build_enumerator (DECL_NAME (decl
), value
, newtag
,
20976 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
20979 if (SCOPED_ENUM_P (newtag
))
20982 finish_enum_value_list (newtag
);
20983 finish_enum (newtag
);
20985 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
20986 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
20989 /* DECL is a FUNCTION_DECL that is a template specialization. Return
20990 its type -- but without substituting the innermost set of template
20991 arguments. So, innermost set of template parameters will appear in
20995 get_mostly_instantiated_function_type (tree decl
)
20997 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
20998 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
21001 /* Return truthvalue if we're processing a template different from
21002 the last one involved in diagnostics. */
21004 problematic_instantiation_changed (void)
21006 return current_tinst_level
!= last_error_tinst_level
;
21009 /* Remember current template involved in diagnostics. */
21011 record_last_problematic_instantiation (void)
21013 last_error_tinst_level
= current_tinst_level
;
21016 struct tinst_level
*
21017 current_instantiation (void)
21019 return current_tinst_level
;
21022 /* Return TRUE if current_function_decl is being instantiated, false
21026 instantiating_current_function_p (void)
21028 return (current_instantiation ()
21029 && current_instantiation ()->decl
== current_function_decl
);
21032 /* [temp.param] Check that template non-type parm TYPE is of an allowable
21033 type. Return zero for ok, nonzero for disallowed. Issue error and
21034 warning messages under control of COMPLAIN. */
21037 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
21039 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
21041 else if (POINTER_TYPE_P (type
))
21043 else if (TYPE_PTRMEM_P (type
))
21045 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
21047 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
21049 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
21051 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
21053 /* A bound template template parm could later be instantiated to have a valid
21054 nontype parm type via an alias template. */
21055 else if (cxx_dialect
>= cxx11
21056 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21059 if (complain
& tf_error
)
21061 if (type
== error_mark_node
)
21062 inform (input_location
, "invalid template non-type parameter");
21064 error ("%q#T is not a valid type for a template non-type parameter",
21070 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
21071 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
21074 dependent_type_p_r (tree type
)
21080 A type is dependent if it is:
21082 -- a template parameter. Template template parameters are types
21083 for us (since TYPE_P holds true for them) so we handle
21085 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21086 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
21088 /* -- a qualified-id with a nested-name-specifier which contains a
21089 class-name that names a dependent type or whose unqualified-id
21090 names a dependent type. */
21091 if (TREE_CODE (type
) == TYPENAME_TYPE
)
21094 /* An alias template specialization can be dependent even if the
21095 resulting type is not. */
21096 if (dependent_alias_template_spec_p (type
))
21099 /* -- a cv-qualified type where the cv-unqualified type is
21101 No code is necessary for this bullet; the code below handles
21102 cv-qualified types, and we don't want to strip aliases with
21103 TYPE_MAIN_VARIANT because of DR 1558. */
21104 /* -- a compound type constructed from any dependent type. */
21105 if (TYPE_PTRMEM_P (type
))
21106 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
21107 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
21109 else if (TYPE_PTR_P (type
)
21110 || TREE_CODE (type
) == REFERENCE_TYPE
)
21111 return dependent_type_p (TREE_TYPE (type
));
21112 else if (TREE_CODE (type
) == FUNCTION_TYPE
21113 || TREE_CODE (type
) == METHOD_TYPE
)
21117 if (dependent_type_p (TREE_TYPE (type
)))
21119 for (arg_type
= TYPE_ARG_TYPES (type
);
21121 arg_type
= TREE_CHAIN (arg_type
))
21122 if (dependent_type_p (TREE_VALUE (arg_type
)))
21126 /* -- an array type constructed from any dependent type or whose
21127 size is specified by a constant expression that is
21130 We checked for type- and value-dependence of the bounds in
21131 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
21132 if (TREE_CODE (type
) == ARRAY_TYPE
)
21134 if (TYPE_DOMAIN (type
)
21135 && dependent_type_p (TYPE_DOMAIN (type
)))
21137 return dependent_type_p (TREE_TYPE (type
));
21140 /* -- a template-id in which either the template name is a template
21142 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21144 /* ... or any of the template arguments is a dependent type or
21145 an expression that is type-dependent or value-dependent. */
21146 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
21147 && (any_dependent_template_arguments_p
21148 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
21151 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
21152 dependent; if the argument of the `typeof' expression is not
21153 type-dependent, then it should already been have resolved. */
21154 if (TREE_CODE (type
) == TYPEOF_TYPE
21155 || TREE_CODE (type
) == DECLTYPE_TYPE
21156 || TREE_CODE (type
) == UNDERLYING_TYPE
)
21159 /* A template argument pack is dependent if any of its packed
21161 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
21163 tree args
= ARGUMENT_PACK_ARGS (type
);
21164 int i
, len
= TREE_VEC_LENGTH (args
);
21165 for (i
= 0; i
< len
; ++i
)
21166 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
21170 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
21171 be template parameters. */
21172 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
21175 /* The standard does not specifically mention types that are local
21176 to template functions or local classes, but they should be
21177 considered dependent too. For example:
21179 template <int I> void f() {
21184 The size of `E' cannot be known until the value of `I' has been
21185 determined. Therefore, `E' must be considered dependent. */
21186 scope
= TYPE_CONTEXT (type
);
21187 if (scope
&& TYPE_P (scope
))
21188 return dependent_type_p (scope
);
21189 /* Don't use type_dependent_expression_p here, as it can lead
21190 to infinite recursion trying to determine whether a lambda
21191 nested in a lambda is dependent (c++/47687). */
21192 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
21193 && DECL_LANG_SPECIFIC (scope
)
21194 && DECL_TEMPLATE_INFO (scope
)
21195 && (any_dependent_template_arguments_p
21196 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
21199 /* Other types are non-dependent. */
21203 /* Returns TRUE if TYPE is dependent, in the sense of
21204 [temp.dep.type]. Note that a NULL type is considered dependent. */
21207 dependent_type_p (tree type
)
21209 /* If there are no template parameters in scope, then there can't be
21210 any dependent types. */
21211 if (!processing_template_decl
)
21213 /* If we are not processing a template, then nobody should be
21214 providing us with a dependent type. */
21216 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
21220 /* If the type is NULL, we have not computed a type for the entity
21221 in question; in that case, the type is dependent. */
21225 /* Erroneous types can be considered non-dependent. */
21226 if (type
== error_mark_node
)
21229 /* If we have not already computed the appropriate value for TYPE,
21231 if (!TYPE_DEPENDENT_P_VALID (type
))
21233 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
21234 TYPE_DEPENDENT_P_VALID (type
) = 1;
21237 return TYPE_DEPENDENT_P (type
);
21240 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
21241 lookup. In other words, a dependent type that is not the current
21245 dependent_scope_p (tree scope
)
21247 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
21248 && !currently_open_class (scope
));
21251 /* T is a SCOPE_REF; return whether we need to consider it
21252 instantiation-dependent so that we can check access at instantiation
21253 time even though we know which member it resolves to. */
21256 instantiation_dependent_scope_ref_p (tree t
)
21258 if (DECL_P (TREE_OPERAND (t
, 1))
21259 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
21260 && accessible_in_template_p (TREE_OPERAND (t
, 0),
21261 TREE_OPERAND (t
, 1)))
21267 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
21268 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
21271 /* Note that this predicate is not appropriate for general expressions;
21272 only constant expressions (that satisfy potential_constant_expression)
21273 can be tested for value dependence. */
21276 value_dependent_expression_p (tree expression
)
21278 if (!processing_template_decl
)
21281 /* A name declared with a dependent type. */
21282 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
21285 switch (TREE_CODE (expression
))
21287 case IDENTIFIER_NODE
:
21288 /* A name that has not been looked up -- must be dependent. */
21291 case TEMPLATE_PARM_INDEX
:
21292 /* A non-type template parm. */
21296 /* A non-type template parm. */
21297 if (DECL_TEMPLATE_PARM_P (expression
))
21299 return value_dependent_expression_p (DECL_INITIAL (expression
));
21302 /* A constant with literal type and is initialized
21303 with an expression that is value-dependent.
21305 Note that a non-dependent parenthesized initializer will have
21306 already been replaced with its constant value, so if we see
21307 a TREE_LIST it must be dependent. */
21308 if (DECL_INITIAL (expression
)
21309 && decl_constant_var_p (expression
)
21310 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
21311 /* cp_finish_decl doesn't fold reference initializers. */
21312 || TREE_CODE (TREE_TYPE (expression
)) == REFERENCE_TYPE
21313 || value_dependent_expression_p (DECL_INITIAL (expression
))))
21317 case DYNAMIC_CAST_EXPR
:
21318 case STATIC_CAST_EXPR
:
21319 case CONST_CAST_EXPR
:
21320 case REINTERPRET_CAST_EXPR
:
21322 /* These expressions are value-dependent if the type to which
21323 the cast occurs is dependent or the expression being casted
21324 is value-dependent. */
21326 tree type
= TREE_TYPE (expression
);
21328 if (dependent_type_p (type
))
21331 /* A functional cast has a list of operands. */
21332 expression
= TREE_OPERAND (expression
, 0);
21335 /* If there are no operands, it must be an expression such
21336 as "int()". This should not happen for aggregate types
21337 because it would form non-constant expressions. */
21338 gcc_assert (cxx_dialect
>= cxx11
21339 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
21344 if (TREE_CODE (expression
) == TREE_LIST
)
21345 return any_value_dependent_elements_p (expression
);
21347 return value_dependent_expression_p (expression
);
21351 if (SIZEOF_EXPR_TYPE_P (expression
))
21352 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
21356 /* A `sizeof' expression is value-dependent if the operand is
21357 type-dependent or is a pack expansion. */
21358 expression
= TREE_OPERAND (expression
, 0);
21359 if (PACK_EXPANSION_P (expression
))
21361 else if (TYPE_P (expression
))
21362 return dependent_type_p (expression
);
21363 return instantiation_dependent_expression_p (expression
);
21365 case AT_ENCODE_EXPR
:
21366 /* An 'encode' expression is value-dependent if the operand is
21368 expression
= TREE_OPERAND (expression
, 0);
21369 return dependent_type_p (expression
);
21371 case NOEXCEPT_EXPR
:
21372 expression
= TREE_OPERAND (expression
, 0);
21373 return instantiation_dependent_expression_p (expression
);
21376 /* All instantiation-dependent expressions should also be considered
21377 value-dependent. */
21378 return instantiation_dependent_scope_ref_p (expression
);
21380 case COMPONENT_REF
:
21381 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
21382 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
21384 case NONTYPE_ARGUMENT_PACK
:
21385 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
21386 is value-dependent. */
21388 tree values
= ARGUMENT_PACK_ARGS (expression
);
21389 int i
, len
= TREE_VEC_LENGTH (values
);
21391 for (i
= 0; i
< len
; ++i
)
21392 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
21400 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
21401 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
21402 || (type2
? dependent_type_p (type2
) : false));
21406 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
21407 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
21410 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
21411 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
21415 tree op
= TREE_OPERAND (expression
, 0);
21416 return (value_dependent_expression_p (op
)
21417 || has_value_dependent_address (op
));
21422 tree fn
= get_callee_fndecl (expression
);
21424 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
21426 nargs
= call_expr_nargs (expression
);
21427 for (i
= 0; i
< nargs
; ++i
)
21429 tree op
= CALL_EXPR_ARG (expression
, i
);
21430 /* In a call to a constexpr member function, look through the
21431 implicit ADDR_EXPR on the object argument so that it doesn't
21432 cause the call to be considered value-dependent. We also
21433 look through it in potential_constant_expression. */
21434 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
21435 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
21436 && TREE_CODE (op
) == ADDR_EXPR
)
21437 op
= TREE_OPERAND (op
, 0);
21438 if (value_dependent_expression_p (op
))
21444 case TEMPLATE_ID_EXPR
:
21445 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
21447 return type_dependent_expression_p (expression
);
21453 if (dependent_type_p (TREE_TYPE (expression
)))
21455 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
21456 if (value_dependent_expression_p (val
))
21462 /* Treat a GNU statement expression as dependent to avoid crashing
21463 under instantiate_non_dependent_expr; it can't be constant. */
21467 /* A constant expression is value-dependent if any subexpression is
21468 value-dependent. */
21469 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
21471 case tcc_reference
:
21473 case tcc_comparison
:
21475 case tcc_expression
:
21478 int i
, len
= cp_tree_operand_length (expression
);
21480 for (i
= 0; i
< len
; i
++)
21482 tree t
= TREE_OPERAND (expression
, i
);
21484 /* In some cases, some of the operands may be missing.l
21485 (For example, in the case of PREDECREMENT_EXPR, the
21486 amount to increment by may be missing.) That doesn't
21487 make the expression dependent. */
21488 if (t
&& value_dependent_expression_p (t
))
21499 /* The expression is not value-dependent. */
21503 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
21504 [temp.dep.expr]. Note that an expression with no type is
21505 considered dependent. Other parts of the compiler arrange for an
21506 expression with type-dependent subexpressions to have no type, so
21507 this function doesn't have to be fully recursive. */
21510 type_dependent_expression_p (tree expression
)
21512 if (!processing_template_decl
)
21515 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
21518 /* An unresolved name is always dependent. */
21519 if (identifier_p (expression
) || TREE_CODE (expression
) == USING_DECL
)
21522 /* Some expression forms are never type-dependent. */
21523 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
21524 || TREE_CODE (expression
) == SIZEOF_EXPR
21525 || TREE_CODE (expression
) == ALIGNOF_EXPR
21526 || TREE_CODE (expression
) == AT_ENCODE_EXPR
21527 || TREE_CODE (expression
) == NOEXCEPT_EXPR
21528 || TREE_CODE (expression
) == TRAIT_EXPR
21529 || TREE_CODE (expression
) == TYPEID_EXPR
21530 || TREE_CODE (expression
) == DELETE_EXPR
21531 || TREE_CODE (expression
) == VEC_DELETE_EXPR
21532 || TREE_CODE (expression
) == THROW_EXPR
)
21535 /* The types of these expressions depends only on the type to which
21536 the cast occurs. */
21537 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
21538 || TREE_CODE (expression
) == STATIC_CAST_EXPR
21539 || TREE_CODE (expression
) == CONST_CAST_EXPR
21540 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
21541 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
21542 || TREE_CODE (expression
) == CAST_EXPR
)
21543 return dependent_type_p (TREE_TYPE (expression
));
21545 /* The types of these expressions depends only on the type created
21546 by the expression. */
21547 if (TREE_CODE (expression
) == NEW_EXPR
21548 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
21550 /* For NEW_EXPR tree nodes created inside a template, either
21551 the object type itself or a TREE_LIST may appear as the
21553 tree type
= TREE_OPERAND (expression
, 1);
21554 if (TREE_CODE (type
) == TREE_LIST
)
21555 /* This is an array type. We need to check array dimensions
21557 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
21558 || value_dependent_expression_p
21559 (TREE_OPERAND (TREE_VALUE (type
), 1));
21561 return dependent_type_p (type
);
21564 if (TREE_CODE (expression
) == SCOPE_REF
)
21566 tree scope
= TREE_OPERAND (expression
, 0);
21567 tree name
= TREE_OPERAND (expression
, 1);
21569 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
21570 contains an identifier associated by name lookup with one or more
21571 declarations declared with a dependent type, or...a
21572 nested-name-specifier or qualified-id that names a member of an
21573 unknown specialization. */
21574 return (type_dependent_expression_p (name
)
21575 || dependent_scope_p (scope
));
21578 if (TREE_CODE (expression
) == FUNCTION_DECL
21579 && DECL_LANG_SPECIFIC (expression
)
21580 && DECL_TEMPLATE_INFO (expression
)
21581 && (any_dependent_template_arguments_p
21582 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
21585 if (TREE_CODE (expression
) == TEMPLATE_DECL
21586 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
21589 if (TREE_CODE (expression
) == STMT_EXPR
)
21590 expression
= stmt_expr_value_expr (expression
);
21592 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
21597 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
21599 if (type_dependent_expression_p (elt
))
21605 /* A static data member of the current instantiation with incomplete
21606 array type is type-dependent, as the definition and specializations
21607 can have different bounds. */
21608 if (VAR_P (expression
)
21609 && DECL_CLASS_SCOPE_P (expression
)
21610 && dependent_type_p (DECL_CONTEXT (expression
))
21611 && VAR_HAD_UNKNOWN_BOUND (expression
))
21614 /* An array of unknown bound depending on a variadic parameter, eg:
21616 template<typename... Args>
21617 void foo (Args... args)
21619 int arr[] = { args... };
21622 template<int... vals>
21625 int arr[] = { vals... };
21628 If the array has no length and has an initializer, it must be that
21629 we couldn't determine its length in cp_complete_array_type because
21630 it is dependent. */
21631 if (VAR_P (expression
)
21632 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
21633 && !TYPE_DOMAIN (TREE_TYPE (expression
))
21634 && DECL_INITIAL (expression
))
21637 /* A variable template specialization is type-dependent if it has any
21638 dependent template arguments. */
21639 if (VAR_P (expression
)
21640 && DECL_LANG_SPECIFIC (expression
)
21641 && DECL_TEMPLATE_INFO (expression
)
21642 && variable_template_p (DECL_TI_TEMPLATE (expression
)))
21643 return any_dependent_template_arguments_p (DECL_TI_ARGS (expression
));
21645 /* Always dependent, on the number of arguments if nothing else. */
21646 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
21649 if (TREE_TYPE (expression
) == unknown_type_node
)
21651 if (TREE_CODE (expression
) == ADDR_EXPR
)
21652 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
21653 if (TREE_CODE (expression
) == COMPONENT_REF
21654 || TREE_CODE (expression
) == OFFSET_REF
)
21656 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
21658 expression
= TREE_OPERAND (expression
, 1);
21659 if (identifier_p (expression
))
21662 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
21663 if (TREE_CODE (expression
) == SCOPE_REF
)
21666 if (BASELINK_P (expression
))
21668 if (BASELINK_OPTYPE (expression
)
21669 && dependent_type_p (BASELINK_OPTYPE (expression
)))
21671 expression
= BASELINK_FUNCTIONS (expression
);
21674 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
21676 if (any_dependent_template_arguments_p
21677 (TREE_OPERAND (expression
, 1)))
21679 expression
= TREE_OPERAND (expression
, 0);
21680 if (identifier_p (expression
))
21684 gcc_assert (TREE_CODE (expression
) == OVERLOAD
21685 || TREE_CODE (expression
) == FUNCTION_DECL
);
21689 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
21691 expression
= OVL_NEXT (expression
);
21696 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
21698 return (dependent_type_p (TREE_TYPE (expression
)));
21701 /* walk_tree callback function for instantiation_dependent_expression_p,
21702 below. Returns non-zero if a dependent subexpression is found. */
21705 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
21710 /* We don't have to worry about decltype currently because decltype
21711 of an instantiation-dependent expr is a dependent type. This
21712 might change depending on the resolution of DR 1172. */
21713 *walk_subtrees
= false;
21716 enum tree_code code
= TREE_CODE (*tp
);
21719 /* Don't treat an argument list as dependent just because it has no
21727 /* A constant with a dependent initializer is dependent. */
21728 if (value_dependent_expression_p (*tp
))
21732 case TEMPLATE_PARM_INDEX
:
21735 /* Handle expressions with type operands. */
21739 case AT_ENCODE_EXPR
:
21741 tree op
= TREE_OPERAND (*tp
, 0);
21742 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
21743 op
= TREE_TYPE (op
);
21746 if (dependent_type_p (op
))
21750 *walk_subtrees
= false;
21758 if (value_dependent_expression_p (*tp
))
21760 *walk_subtrees
= false;
21763 case COMPONENT_REF
:
21764 if (identifier_p (TREE_OPERAND (*tp
, 1)))
21765 /* In a template, finish_class_member_access_expr creates a
21766 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
21767 type-dependent, so that we can check access control at
21768 instantiation time (PR 42277). See also Core issue 1273. */
21773 if (instantiation_dependent_scope_ref_p (*tp
))
21778 /* Treat statement-expressions as dependent. */
21786 if (type_dependent_expression_p (*tp
))
21792 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
21793 sense defined by the ABI:
21795 "An expression is instantiation-dependent if it is type-dependent
21796 or value-dependent, or it has a subexpression that is type-dependent
21797 or value-dependent." */
21800 instantiation_dependent_expression_p (tree expression
)
21804 if (!processing_template_decl
)
21807 if (expression
== error_mark_node
)
21810 result
= cp_walk_tree_without_duplicates (&expression
,
21811 instantiation_dependent_r
, NULL
);
21812 return result
!= NULL_TREE
;
21815 /* Like type_dependent_expression_p, but it also works while not processing
21816 a template definition, i.e. during substitution or mangling. */
21819 type_dependent_expression_p_push (tree expr
)
21822 ++processing_template_decl
;
21823 b
= type_dependent_expression_p (expr
);
21824 --processing_template_decl
;
21828 /* Returns TRUE if ARGS contains a type-dependent expression. */
21831 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
21836 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
21838 if (type_dependent_expression_p (arg
))
21844 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21845 expressions) contains any type-dependent expressions. */
21848 any_type_dependent_elements_p (const_tree list
)
21850 for (; list
; list
= TREE_CHAIN (list
))
21851 if (type_dependent_expression_p (TREE_VALUE (list
)))
21857 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21858 expressions) contains any value-dependent expressions. */
21861 any_value_dependent_elements_p (const_tree list
)
21863 for (; list
; list
= TREE_CHAIN (list
))
21864 if (value_dependent_expression_p (TREE_VALUE (list
)))
21870 /* Returns TRUE if the ARG (a template argument) is dependent. */
21873 dependent_template_arg_p (tree arg
)
21875 if (!processing_template_decl
)
21878 /* Assume a template argument that was wrongly written by the user
21879 is dependent. This is consistent with what
21880 any_dependent_template_arguments_p [that calls this function]
21882 if (!arg
|| arg
== error_mark_node
)
21885 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
21886 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
21888 if (TREE_CODE (arg
) == TEMPLATE_DECL
21889 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
21890 return dependent_template_p (arg
);
21891 else if (ARGUMENT_PACK_P (arg
))
21893 tree args
= ARGUMENT_PACK_ARGS (arg
);
21894 int i
, len
= TREE_VEC_LENGTH (args
);
21895 for (i
= 0; i
< len
; ++i
)
21897 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
21903 else if (TYPE_P (arg
))
21904 return dependent_type_p (arg
);
21906 return (type_dependent_expression_p (arg
)
21907 || value_dependent_expression_p (arg
));
21910 /* Returns true if ARGS (a collection of template arguments) contains
21911 any types that require structural equality testing. */
21914 any_template_arguments_need_structural_equality_p (tree args
)
21921 if (args
== error_mark_node
)
21924 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
21926 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
21927 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
21929 tree arg
= TREE_VEC_ELT (level
, j
);
21930 tree packed_args
= NULL_TREE
;
21933 if (ARGUMENT_PACK_P (arg
))
21935 /* Look inside the argument pack. */
21936 packed_args
= ARGUMENT_PACK_ARGS (arg
);
21937 len
= TREE_VEC_LENGTH (packed_args
);
21940 for (k
= 0; k
< len
; ++k
)
21943 arg
= TREE_VEC_ELT (packed_args
, k
);
21945 if (error_operand_p (arg
))
21947 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
21949 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
21951 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
21952 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
21961 /* Returns true if ARGS (a collection of template arguments) contains
21962 any dependent arguments. */
21965 any_dependent_template_arguments_p (const_tree args
)
21972 if (args
== error_mark_node
)
21975 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
21977 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
21978 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
21979 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
21986 /* Returns TRUE if the template TMPL is dependent. */
21989 dependent_template_p (tree tmpl
)
21991 if (TREE_CODE (tmpl
) == OVERLOAD
)
21995 if (dependent_template_p (OVL_CURRENT (tmpl
)))
21997 tmpl
= OVL_NEXT (tmpl
);
22002 /* Template template parameters are dependent. */
22003 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
22004 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
22006 /* So are names that have not been looked up. */
22007 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
22009 /* So are member templates of dependent classes. */
22010 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
22011 return dependent_type_p (DECL_CONTEXT (tmpl
));
22015 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
22018 dependent_template_id_p (tree tmpl
, tree args
)
22020 return (dependent_template_p (tmpl
)
22021 || any_dependent_template_arguments_p (args
));
22024 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
22028 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
22032 if (!processing_template_decl
)
22035 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
22037 tree decl
= TREE_VEC_ELT (declv
, i
);
22038 tree init
= TREE_VEC_ELT (initv
, i
);
22039 tree cond
= TREE_VEC_ELT (condv
, i
);
22040 tree incr
= TREE_VEC_ELT (incrv
, i
);
22042 if (type_dependent_expression_p (decl
))
22045 if (init
&& type_dependent_expression_p (init
))
22048 if (type_dependent_expression_p (cond
))
22051 if (COMPARISON_CLASS_P (cond
)
22052 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
22053 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
22056 if (TREE_CODE (incr
) == MODOP_EXPR
)
22058 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
22059 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
22062 else if (type_dependent_expression_p (incr
))
22064 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
22066 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
22068 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
22070 tree t
= TREE_OPERAND (incr
, 1);
22071 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
22072 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
22081 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
22082 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
22083 no such TYPE can be found. Note that this function peers inside
22084 uninstantiated templates and therefore should be used only in
22085 extremely limited situations. ONLY_CURRENT_P restricts this
22086 peering to the currently open classes hierarchy (which is required
22087 when comparing types). */
22090 resolve_typename_type (tree type
, bool only_current_p
)
22099 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
22101 scope
= TYPE_CONTEXT (type
);
22102 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
22103 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
22104 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
22105 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
22106 identifier of the TYPENAME_TYPE anymore.
22107 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
22108 TYPENAME_TYPE instead, we avoid messing up with a possible
22109 typedef variant case. */
22110 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
22112 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
22113 it first before we can figure out what NAME refers to. */
22114 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
22116 if (TYPENAME_IS_RESOLVING_P (scope
))
22117 /* Given a class template A with a dependent base with nested type C,
22118 typedef typename A::C::C C will land us here, as trying to resolve
22119 the initial A::C leads to the local C typedef, which leads back to
22120 A::C::C. So we break the recursion now. */
22123 scope
= resolve_typename_type (scope
, only_current_p
);
22125 /* If we don't know what SCOPE refers to, then we cannot resolve the
22127 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
22129 /* If the SCOPE is a template type parameter, we have no way of
22130 resolving the name. */
22131 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
22133 /* If the SCOPE is not the current instantiation, there's no reason
22134 to look inside it. */
22135 if (only_current_p
&& !currently_open_class (scope
))
22137 /* If this is a typedef, we don't want to look inside (c++/11987). */
22138 if (typedef_variant_p (type
))
22140 /* If SCOPE isn't the template itself, it will not have a valid
22141 TYPE_FIELDS list. */
22142 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
22143 /* scope is either the template itself or a compatible instantiation
22144 like X<T>, so look up the name in the original template. */
22145 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
22147 /* scope is a partial instantiation, so we can't do the lookup or we
22148 will lose the template arguments. */
22150 /* Enter the SCOPE so that name lookup will be resolved as if we
22151 were in the class definition. In particular, SCOPE will no
22152 longer be considered a dependent type. */
22153 pushed_scope
= push_scope (scope
);
22154 /* Look up the declaration. */
22155 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
22156 tf_warning_or_error
);
22158 result
= NULL_TREE
;
22160 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
22161 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
22164 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type
))
22165 && TREE_CODE (decl
) == TYPE_DECL
)
22167 result
= TREE_TYPE (decl
);
22168 if (result
== error_mark_node
)
22169 result
= NULL_TREE
;
22171 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
22172 && DECL_CLASS_TEMPLATE_P (decl
))
22176 /* Obtain the template and the arguments. */
22177 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
22178 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
22179 /* Instantiate the template. */
22180 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
22181 /*entering_scope=*/0,
22182 tf_error
| tf_user
);
22183 if (result
== error_mark_node
)
22184 result
= NULL_TREE
;
22187 /* Leave the SCOPE. */
22189 pop_scope (pushed_scope
);
22191 /* If we failed to resolve it, return the original typename. */
22195 /* If lookup found a typename type, resolve that too. */
22196 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
22198 /* Ill-formed programs can cause infinite recursion here, so we
22199 must catch that. */
22200 TYPENAME_IS_RESOLVING_P (type
) = 1;
22201 result
= resolve_typename_type (result
, only_current_p
);
22202 TYPENAME_IS_RESOLVING_P (type
) = 0;
22205 /* Qualify the resulting type. */
22206 quals
= cp_type_quals (type
);
22208 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
22213 /* EXPR is an expression which is not type-dependent. Return a proxy
22214 for EXPR that can be used to compute the types of larger
22215 expressions containing EXPR. */
22218 build_non_dependent_expr (tree expr
)
22222 #ifdef ENABLE_CHECKING
22223 /* Try to get a constant value for all non-dependent expressions in
22224 order to expose bugs in *_dependent_expression_p and constexpr. */
22225 if (cxx_dialect
>= cxx11
)
22226 fold_non_dependent_expr (expr
);
22229 /* Preserve OVERLOADs; the functions must be available to resolve
22232 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
22233 inner_expr
= stmt_expr_value_expr (inner_expr
);
22234 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
22235 inner_expr
= TREE_OPERAND (inner_expr
, 0);
22236 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
22237 inner_expr
= TREE_OPERAND (inner_expr
, 1);
22238 if (is_overloaded_fn (inner_expr
)
22239 || TREE_CODE (inner_expr
) == OFFSET_REF
)
22241 /* There is no need to return a proxy for a variable. */
22244 /* Preserve string constants; conversions from string constants to
22245 "char *" are allowed, even though normally a "const char *"
22246 cannot be used to initialize a "char *". */
22247 if (TREE_CODE (expr
) == STRING_CST
)
22249 /* Preserve void and arithmetic constants, as an optimization -- there is no
22250 reason to create a new node. */
22251 if (TREE_CODE (expr
) == VOID_CST
22252 || TREE_CODE (expr
) == INTEGER_CST
22253 || TREE_CODE (expr
) == REAL_CST
)
22255 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
22256 There is at least one place where we want to know that a
22257 particular expression is a throw-expression: when checking a ?:
22258 expression, there are special rules if the second or third
22259 argument is a throw-expression. */
22260 if (TREE_CODE (expr
) == THROW_EXPR
)
22263 /* Don't wrap an initializer list, we need to be able to look inside. */
22264 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
22267 /* Don't wrap a dummy object, we need to be able to test for it. */
22268 if (is_dummy_object (expr
))
22271 if (TREE_CODE (expr
) == COND_EXPR
)
22272 return build3 (COND_EXPR
,
22274 TREE_OPERAND (expr
, 0),
22275 (TREE_OPERAND (expr
, 1)
22276 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
22277 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
22278 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
22279 if (TREE_CODE (expr
) == COMPOUND_EXPR
22280 && !COMPOUND_EXPR_OVERLOADED (expr
))
22281 return build2 (COMPOUND_EXPR
,
22283 TREE_OPERAND (expr
, 0),
22284 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
22286 /* If the type is unknown, it can't really be non-dependent */
22287 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
22289 /* Otherwise, build a NON_DEPENDENT_EXPR. */
22290 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
22293 /* ARGS is a vector of expressions as arguments to a function call.
22294 Replace the arguments with equivalent non-dependent expressions.
22295 This modifies ARGS in place. */
22298 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
22303 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
22305 tree newarg
= build_non_dependent_expr (arg
);
22307 (*args
)[ix
] = newarg
;
22311 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
22312 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
22316 make_auto_1 (tree name
)
22318 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
22319 TYPE_NAME (au
) = build_decl (input_location
,
22320 TYPE_DECL
, name
, au
);
22321 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
22322 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
22323 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
22324 TYPE_NAME (au
), NULL_TREE
);
22325 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
22326 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
22327 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
22333 make_decltype_auto (void)
22335 return make_auto_1 (get_identifier ("decltype(auto)"));
22341 return make_auto_1 (get_identifier ("auto"));
22344 /* Given type ARG, return std::initializer_list<ARG>. */
22349 tree std_init_list
= namespace_binding
22350 (get_identifier ("initializer_list"), std_node
);
22352 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
22354 error ("deducing from brace-enclosed initializer list requires "
22355 "#include <initializer_list>");
22356 return error_mark_node
;
22358 argvec
= make_tree_vec (1);
22359 TREE_VEC_ELT (argvec
, 0) = arg
;
22360 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
22361 NULL_TREE
, 0, tf_warning_or_error
);
22364 /* Replace auto in TYPE with std::initializer_list<auto>. */
22367 listify_autos (tree type
, tree auto_node
)
22369 tree init_auto
= listify (auto_node
);
22370 tree argvec
= make_tree_vec (1);
22371 TREE_VEC_ELT (argvec
, 0) = init_auto
;
22372 if (processing_template_decl
)
22373 argvec
= add_to_template_args (current_template_args (), argvec
);
22374 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
22377 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
22378 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
22381 do_auto_deduction (tree type
, tree init
, tree auto_node
)
22385 if (init
== error_mark_node
)
22386 return error_mark_node
;
22388 if (type_dependent_expression_p (init
))
22389 /* Defining a subset of type-dependent expressions that we can deduce
22390 from ahead of time isn't worth the trouble. */
22393 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
22394 with either a new invented type template parameter U or, if the
22395 initializer is a braced-init-list (8.5.4), with
22396 std::initializer_list<U>. */
22397 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
22399 if (!DIRECT_LIST_INIT_P (init
))
22400 type
= listify_autos (type
, auto_node
);
22401 else if (CONSTRUCTOR_NELTS (init
) == 1)
22402 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
22405 if (permerror (input_location
, "direct-list-initialization of "
22406 "%<auto%> requires exactly one element"))
22407 inform (input_location
,
22408 "for deduction to %<std::initializer_list%>, use copy-"
22409 "list-initialization (i.e. add %<=%> before the %<{%>)");
22410 type
= listify_autos (type
, auto_node
);
22414 init
= resolve_nondeduced_context (init
);
22416 targs
= make_tree_vec (1);
22417 if (AUTO_IS_DECLTYPE (auto_node
))
22419 bool id
= (DECL_P (init
) || (TREE_CODE (init
) == COMPONENT_REF
22420 && !REF_PARENTHESIZED_P (init
)));
22421 TREE_VEC_ELT (targs
, 0)
22422 = finish_decltype_type (init
, id
, tf_warning_or_error
);
22423 if (type
!= auto_node
)
22425 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
22426 return error_mark_node
;
22431 tree parms
= build_tree_list (NULL_TREE
, type
);
22432 tree tparms
= make_tree_vec (1);
22435 TREE_VEC_ELT (tparms
, 0)
22436 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
22437 val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
22438 DEDUCE_CALL
, LOOKUP_NORMAL
,
22439 NULL
, /*explain_p=*/false);
22442 if (processing_template_decl
)
22443 /* Try again at instantiation time. */
22445 if (type
&& type
!= error_mark_node
)
22446 /* If type is error_mark_node a diagnostic must have been
22447 emitted by now. Also, having a mention to '<type error>'
22448 in the diagnostic is not really useful to the user. */
22450 if (cfun
&& auto_node
== current_function_auto_return_pattern
22451 && LAMBDA_FUNCTION_P (current_function_decl
))
22452 error ("unable to deduce lambda return type from %qE", init
);
22454 error ("unable to deduce %qT from %qE", type
, init
);
22456 return error_mark_node
;
22460 /* If the list of declarators contains more than one declarator, the type
22461 of each declared variable is determined as described above. If the
22462 type deduced for the template parameter U is not the same in each
22463 deduction, the program is ill-formed. */
22464 if (TREE_TYPE (auto_node
)
22465 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
22467 if (cfun
&& auto_node
== current_function_auto_return_pattern
22468 && LAMBDA_FUNCTION_P (current_function_decl
))
22469 error ("inconsistent types %qT and %qT deduced for "
22470 "lambda return type", TREE_TYPE (auto_node
),
22471 TREE_VEC_ELT (targs
, 0));
22473 error ("inconsistent deduction for %qT: %qT and then %qT",
22474 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
22475 return error_mark_node
;
22477 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
22479 if (processing_template_decl
)
22480 targs
= add_to_template_args (current_template_args (), targs
);
22481 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
22484 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
22488 splice_late_return_type (tree type
, tree late_return_type
)
22490 if (is_auto (type
))
22492 if (late_return_type
)
22493 return late_return_type
;
22495 tree idx
= get_template_parm_index (type
);
22496 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
22497 /* In an abbreviated function template we didn't know we were dealing
22498 with a function template when we saw the auto return type, so update
22499 it to have the correct level. */
22500 return make_auto_1 (TYPE_IDENTIFIER (type
));
22505 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
22506 'decltype(auto)'. */
22509 is_auto (const_tree type
)
22511 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
22512 && (TYPE_IDENTIFIER (type
) == get_identifier ("auto")
22513 || TYPE_IDENTIFIER (type
) == get_identifier ("decltype(auto)")))
22519 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
22520 a use of `auto'. Returns NULL_TREE otherwise. */
22523 type_uses_auto (tree type
)
22525 return find_type_usage (type
, is_auto
);
22528 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
22529 'decltype(auto)' or a concept. */
22532 is_auto_or_concept (const_tree type
)
22534 return is_auto (type
); // or concept
22537 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
22538 a concept identifier) iff TYPE contains a use of a generic type. Returns
22539 NULL_TREE otherwise. */
22542 type_uses_auto_or_concept (tree type
)
22544 return find_type_usage (type
, is_auto_or_concept
);
22548 /* For a given template T, return the vector of typedefs referenced
22549 in T for which access check is needed at T instantiation time.
22550 T is either a FUNCTION_DECL or a RECORD_TYPE.
22551 Those typedefs were added to T by the function
22552 append_type_to_template_for_access_check. */
22554 vec
<qualified_typedef_usage_t
, va_gc
> *
22555 get_types_needing_access_check (tree t
)
22558 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
22560 if (!t
|| t
== error_mark_node
)
22563 if (!(ti
= get_template_info (t
)))
22566 if (CLASS_TYPE_P (t
)
22567 || TREE_CODE (t
) == FUNCTION_DECL
)
22569 if (!TI_TEMPLATE (ti
))
22572 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
22578 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
22579 tied to T. That list of typedefs will be access checked at
22580 T instantiation time.
22581 T is either a FUNCTION_DECL or a RECORD_TYPE.
22582 TYPE_DECL is a TYPE_DECL node representing a typedef.
22583 SCOPE is the scope through which TYPE_DECL is accessed.
22584 LOCATION is the location of the usage point of TYPE_DECL.
22586 This function is a subroutine of
22587 append_type_to_template_for_access_check. */
22590 append_type_to_template_for_access_check_1 (tree t
,
22593 location_t location
)
22595 qualified_typedef_usage_t typedef_usage
;
22598 if (!t
|| t
== error_mark_node
)
22601 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
22602 || CLASS_TYPE_P (t
))
22604 && TREE_CODE (type_decl
) == TYPE_DECL
22607 if (!(ti
= get_template_info (t
)))
22610 gcc_assert (TI_TEMPLATE (ti
));
22612 typedef_usage
.typedef_decl
= type_decl
;
22613 typedef_usage
.context
= scope
;
22614 typedef_usage
.locus
= location
;
22616 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
22619 /* Append TYPE_DECL to the template TEMPL.
22620 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
22621 At TEMPL instanciation time, TYPE_DECL will be checked to see
22622 if it can be accessed through SCOPE.
22623 LOCATION is the location of the usage point of TYPE_DECL.
22625 e.g. consider the following code snippet:
22632 template<class U> struct S
22634 C::myint mi; // <-- usage point of the typedef C::myint
22639 At S<char> instantiation time, we need to check the access of C::myint
22640 In other words, we need to check the access of the myint typedef through
22641 the C scope. For that purpose, this function will add the myint typedef
22642 and the scope C through which its being accessed to a list of typedefs
22643 tied to the template S. That list will be walked at template instantiation
22644 time and access check performed on each typedefs it contains.
22645 Note that this particular code snippet should yield an error because
22646 myint is private to C. */
22649 append_type_to_template_for_access_check (tree templ
,
22652 location_t location
)
22654 qualified_typedef_usage_t
*iter
;
22657 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
22659 /* Make sure we don't append the type to the template twice. */
22660 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
22661 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
22664 append_type_to_template_for_access_check_1 (templ
, type_decl
,
22668 /* Convert the generic type parameters in PARM that match the types given in the
22669 range [START_IDX, END_IDX) from the current_template_parms into generic type
22673 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
22675 tree current
= current_template_parms
;
22676 int depth
= TMPL_PARMS_DEPTH (current
);
22677 current
= INNERMOST_TEMPLATE_PARMS (current
);
22678 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
22680 for (int i
= 0; i
< start_idx
; ++i
)
22681 TREE_VEC_ELT (replacement
, i
)
22682 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
22684 for (int i
= start_idx
; i
< end_idx
; ++i
)
22686 /* Create a distinct parameter pack type from the current parm and add it
22687 to the replacement args to tsubst below into the generic function
22690 tree o
= TREE_TYPE (TREE_VALUE
22691 (TREE_VEC_ELT (current
, i
)));
22692 tree t
= copy_type (o
);
22693 TEMPLATE_TYPE_PARM_INDEX (t
)
22694 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
22696 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
22697 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
22698 TYPE_MAIN_VARIANT (t
) = t
;
22699 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
22700 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
22701 TREE_VEC_ELT (replacement
, i
) = t
;
22702 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
22705 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
22706 TREE_VEC_ELT (replacement
, i
)
22707 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
22709 /* If there are more levels then build up the replacement with the outer
22712 replacement
= add_to_template_args (template_parms_to_args
22713 (TREE_CHAIN (current_template_parms
)),
22716 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
22720 /* Set up the hash tables for template instantiations. */
22723 init_template_processing (void)
22725 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
22726 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
22729 /* Print stats about the template hash tables for -fstats. */
22732 print_template_statistics (void)
22734 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
22735 "%f collisions\n", (long) decl_specializations
->size (),
22736 (long) decl_specializations
->elements (),
22737 decl_specializations
->collisions ());
22738 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
22739 "%f collisions\n", (long) type_specializations
->size (),
22740 (long) type_specializations
->elements (),
22741 type_specializations
->collisions ());
22744 #include "gt-cp-pt.h"