1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
29 #include "coretypes.h"
34 #include "double-int.h"
41 #include "stringpool.h"
44 #include "stor-layout.h"
48 #include "c-family/c-common.h"
49 #include "c-family/c-objc.h"
50 #include "cp-objcp-common.h"
51 #include "tree-inline.h"
55 #include "tree-iterator.h"
56 #include "type-utils.h"
59 /* The type of functions taking a tree, and some additional data, and
61 typedef int (*tree_fn_t
) (tree
, void*);
63 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
64 instantiations have been deferred, either because their definitions
65 were not yet available, or because we were putting off doing the work. */
66 struct GTY ((chain_next ("%h.next"))) pending_template
{
67 struct pending_template
*next
;
68 struct tinst_level
*tinst
;
71 static GTY(()) struct pending_template
*pending_templates
;
72 static GTY(()) struct pending_template
*last_pending_template
;
74 int processing_template_parmlist
;
75 static int template_header_count
;
77 static GTY(()) tree saved_trees
;
78 static vec
<int> inline_parm_levels
;
80 static GTY(()) struct tinst_level
*current_tinst_level
;
82 static GTY(()) tree saved_access_scope
;
84 /* Live only within one (recursive) call to tsubst_expr. We use
85 this to pass the statement expression node from the STMT_EXPR
86 to the EXPR_STMT that is its result. */
87 static tree cur_stmt_expr
;
89 /* True if we've recursed into fn_type_unification too many times. */
90 static bool excessive_deduction_depth
;
92 struct GTY((for_user
)) spec_entry
99 struct spec_hasher
: ggc_hasher
<spec_entry
*>
101 static hashval_t
hash (spec_entry
*);
102 static bool equal (spec_entry
*, spec_entry
*);
105 static GTY (()) hash_table
<spec_hasher
> *decl_specializations
;
107 static GTY (()) hash_table
<spec_hasher
> *type_specializations
;
109 /* Contains canonical template parameter types. The vector is indexed by
110 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
111 TREE_LIST, whose TREE_VALUEs contain the canonical template
112 parameters of various types and levels. */
113 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
115 #define UNIFY_ALLOW_NONE 0
116 #define UNIFY_ALLOW_MORE_CV_QUAL 1
117 #define UNIFY_ALLOW_LESS_CV_QUAL 2
118 #define UNIFY_ALLOW_DERIVED 4
119 #define UNIFY_ALLOW_INTEGER 8
120 #define UNIFY_ALLOW_OUTER_LEVEL 16
121 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
122 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
124 enum template_base_result
{
126 tbr_ambiguous_baseclass
,
130 static void push_access_scope (tree
);
131 static void pop_access_scope (tree
);
132 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
133 unification_kind_t
, int,
135 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
136 unification_kind_t
, int, bool, bool);
137 static int unify (tree
, tree
, tree
, tree
, int, bool);
138 static void add_pending_template (tree
);
139 static tree
reopen_tinst_level (struct tinst_level
*);
140 static tree
tsubst_initializer_list (tree
, tree
);
141 static tree
get_partial_spec_bindings (tree
, tree
, tree
, tree
);
142 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
144 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
146 static void tsubst_enum (tree
, tree
, tree
);
147 static tree
add_to_template_args (tree
, tree
);
148 static tree
add_outermost_template_args (tree
, tree
);
149 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
150 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
152 static int type_unification_real (tree
, tree
, tree
, const tree
*,
153 unsigned int, int, unification_kind_t
, int,
154 vec
<deferred_access_check
, va_gc
> **,
156 static void note_template_header (int);
157 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
158 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
159 static tree
convert_template_argument (tree
, tree
, tree
,
160 tsubst_flags_t
, int, tree
);
161 static int for_each_template_parm (tree
, tree_fn_t
, void*,
162 hash_set
<tree
> *, bool);
163 static tree
expand_template_argument_pack (tree
);
164 static tree
build_template_parm_index (int, int, int, tree
, tree
);
165 static bool inline_needs_template_parms (tree
, bool);
166 static void push_inline_template_parms_recursive (tree
, int);
167 static tree
retrieve_local_specialization (tree
);
168 static void register_local_specialization (tree
, tree
);
169 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
170 static int mark_template_parm (tree
, void *);
171 static int template_parm_this_level_p (tree
, void *);
172 static tree
tsubst_friend_function (tree
, tree
);
173 static tree
tsubst_friend_class (tree
, tree
);
174 static int can_complete_type_without_circularity (tree
);
175 static tree
get_bindings (tree
, tree
, tree
, bool);
176 static int template_decl_level (tree
);
177 static int check_cv_quals_for_unify (int, tree
, tree
);
178 static void template_parm_level_and_index (tree
, int*, int*);
179 static int unify_pack_expansion (tree
, tree
, tree
,
180 tree
, unification_kind_t
, bool, bool);
181 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
182 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
183 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
184 static void regenerate_decl_from_template (tree
, tree
);
185 static tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
186 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
187 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
188 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
189 static bool check_specialization_scope (void);
190 static tree
process_partial_specialization (tree
);
191 static void set_current_access_from_decl (tree
);
192 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
194 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
195 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
197 static bool template_template_parm_bindings_ok_p (tree
, tree
);
198 static int template_args_equal (tree
, tree
);
199 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
200 static tree
for_each_template_parm_r (tree
*, int *, void *);
201 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
202 static void copy_default_args_to_explicit_spec (tree
);
203 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
204 static bool dependent_template_arg_p (tree
);
205 static bool any_template_arguments_need_structural_equality_p (tree
);
206 static bool dependent_type_p_r (tree
);
207 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
, bool);
208 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
209 static tree
tsubst_pack_expansion (tree
, tree
, tsubst_flags_t
, tree
);
210 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
211 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
212 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
214 static tree
listify (tree
);
215 static tree
listify_autos (tree
, tree
);
216 static tree
template_parm_to_arg (tree t
);
217 static tree
current_template_args (void);
218 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
219 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
220 static bool complex_alias_template_p (const_tree tmpl
);
222 /* Make the current scope suitable for access checking when we are
223 processing T. T can be FUNCTION_DECL for instantiated function
224 template, VAR_DECL for static member variable, or TYPE_DECL for
225 alias template (needed by instantiate_decl). */
228 push_access_scope (tree t
)
230 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
231 || TREE_CODE (t
) == TYPE_DECL
);
233 if (DECL_FRIEND_CONTEXT (t
))
234 push_nested_class (DECL_FRIEND_CONTEXT (t
));
235 else if (DECL_CLASS_SCOPE_P (t
))
236 push_nested_class (DECL_CONTEXT (t
));
238 push_to_top_level ();
240 if (TREE_CODE (t
) == FUNCTION_DECL
)
242 saved_access_scope
= tree_cons
243 (NULL_TREE
, current_function_decl
, saved_access_scope
);
244 current_function_decl
= t
;
248 /* Restore the scope set up by push_access_scope. T is the node we
252 pop_access_scope (tree t
)
254 if (TREE_CODE (t
) == FUNCTION_DECL
)
256 current_function_decl
= TREE_VALUE (saved_access_scope
);
257 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
260 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
263 pop_from_top_level ();
266 /* Do any processing required when DECL (a member template
267 declaration) is finished. Returns the TEMPLATE_DECL corresponding
268 to DECL, unless it is a specialization, in which case the DECL
269 itself is returned. */
272 finish_member_template_decl (tree decl
)
274 if (decl
== error_mark_node
)
275 return error_mark_node
;
277 gcc_assert (DECL_P (decl
));
279 if (TREE_CODE (decl
) == TYPE_DECL
)
283 type
= TREE_TYPE (decl
);
284 if (type
== error_mark_node
)
285 return error_mark_node
;
286 if (MAYBE_CLASS_TYPE_P (type
)
287 && CLASSTYPE_TEMPLATE_INFO (type
)
288 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
290 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
291 check_member_template (tmpl
);
296 else if (TREE_CODE (decl
) == FIELD_DECL
)
297 error ("data member %qD cannot be a member template", decl
);
298 else if (DECL_TEMPLATE_INFO (decl
))
300 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
302 check_member_template (DECL_TI_TEMPLATE (decl
));
303 return DECL_TI_TEMPLATE (decl
);
309 error ("invalid member template declaration %qD", decl
);
311 return error_mark_node
;
314 /* Create a template info node. */
317 build_template_info (tree template_decl
, tree template_args
)
319 tree result
= make_node (TEMPLATE_INFO
);
320 TI_TEMPLATE (result
) = template_decl
;
321 TI_ARGS (result
) = template_args
;
325 /* Return the template info node corresponding to T, whatever T is. */
328 get_template_info (const_tree t
)
330 tree tinfo
= NULL_TREE
;
332 if (!t
|| t
== error_mark_node
)
335 if (TREE_CODE (t
) == NAMESPACE_DECL
)
338 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
339 tinfo
= DECL_TEMPLATE_INFO (t
);
341 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
344 if (OVERLOAD_TYPE_P (t
))
345 tinfo
= TYPE_TEMPLATE_INFO (t
);
346 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
347 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
352 /* Returns the template nesting level of the indicated class TYPE.
362 A<T>::B<U> has depth two, while A<T> has depth one.
363 Both A<T>::B<int> and A<int>::B<U> have depth one, if
364 they are instantiations, not specializations.
366 This function is guaranteed to return 0 if passed NULL_TREE so
367 that, for example, `template_class_depth (current_class_type)' is
371 template_class_depth (tree type
)
376 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
377 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
378 ? CP_DECL_CONTEXT (type
) : CP_TYPE_CONTEXT (type
))
380 tree tinfo
= get_template_info (type
);
382 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
383 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
390 /* Subroutine of maybe_begin_member_template_processing.
391 Returns true if processing DECL needs us to push template parms. */
394 inline_needs_template_parms (tree decl
, bool nsdmi
)
396 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
399 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
400 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
403 /* Subroutine of maybe_begin_member_template_processing.
404 Push the template parms in PARMS, starting from LEVELS steps into the
405 chain, and ending at the beginning, since template parms are listed
409 push_inline_template_parms_recursive (tree parmlist
, int levels
)
411 tree parms
= TREE_VALUE (parmlist
);
415 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
417 ++processing_template_decl
;
418 current_template_parms
419 = tree_cons (size_int (processing_template_decl
),
420 parms
, current_template_parms
);
421 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
423 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
425 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
427 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
429 if (error_operand_p (parm
))
432 gcc_assert (DECL_P (parm
));
434 switch (TREE_CODE (parm
))
443 /* Make a CONST_DECL as is done in process_template_parm.
444 It is ugly that we recreate this here; the original
445 version built in process_template_parm is no longer
447 tree decl
= build_decl (DECL_SOURCE_LOCATION (parm
),
448 CONST_DECL
, DECL_NAME (parm
),
450 DECL_ARTIFICIAL (decl
) = 1;
451 TREE_CONSTANT (decl
) = 1;
452 TREE_READONLY (decl
) = 1;
453 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
454 SET_DECL_TEMPLATE_PARM_P (decl
);
465 /* Restore the template parameter context for a member template, a
466 friend template defined in a class definition, or a non-template
467 member of template class. */
470 maybe_begin_member_template_processing (tree decl
)
474 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
478 tree ctx
= DECL_CONTEXT (decl
);
479 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
480 /* Disregard full specializations (c++/60999). */
481 && uses_template_parms (ctx
)
482 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
485 if (inline_needs_template_parms (decl
, nsdmi
))
487 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
488 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
490 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
493 parms
= TREE_CHAIN (parms
);
496 push_inline_template_parms_recursive (parms
, levels
);
499 /* Remember how many levels of template parameters we pushed so that
500 we can pop them later. */
501 inline_parm_levels
.safe_push (levels
);
504 /* Undo the effects of maybe_begin_member_template_processing. */
507 maybe_end_member_template_processing (void)
512 if (inline_parm_levels
.length () == 0)
515 last
= inline_parm_levels
.pop ();
516 for (i
= 0; i
< last
; ++i
)
518 --processing_template_decl
;
519 current_template_parms
= TREE_CHAIN (current_template_parms
);
524 /* Return a new template argument vector which contains all of ARGS,
525 but has as its innermost set of arguments the EXTRA_ARGS. */
528 add_to_template_args (tree args
, tree extra_args
)
535 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
538 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
539 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
541 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
542 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
544 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
545 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
550 /* Like add_to_template_args, but only the outermost ARGS are added to
551 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
552 (EXTRA_ARGS) levels are added. This function is used to combine
553 the template arguments from a partial instantiation with the
554 template arguments used to attain the full instantiation from the
555 partial instantiation. */
558 add_outermost_template_args (tree args
, tree extra_args
)
562 /* If there are more levels of EXTRA_ARGS than there are ARGS,
563 something very fishy is going on. */
564 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
566 /* If *all* the new arguments will be the EXTRA_ARGS, just return
568 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
571 /* For the moment, we make ARGS look like it contains fewer levels. */
572 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
574 new_args
= add_to_template_args (args
, extra_args
);
576 /* Now, we restore ARGS to its full dimensions. */
577 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
582 /* Return the N levels of innermost template arguments from the ARGS. */
585 get_innermost_template_args (tree args
, int n
)
593 /* If N is 1, just return the innermost set of template arguments. */
595 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
597 /* If we're not removing anything, just return the arguments we were
599 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
600 gcc_assert (extra_levels
>= 0);
601 if (extra_levels
== 0)
604 /* Make a new set of arguments, not containing the outer arguments. */
605 new_args
= make_tree_vec (n
);
606 for (i
= 1; i
<= n
; ++i
)
607 SET_TMPL_ARGS_LEVEL (new_args
, i
,
608 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
613 /* The inverse of get_innermost_template_args: Return all but the innermost
614 EXTRA_LEVELS levels of template arguments from the ARGS. */
617 strip_innermost_template_args (tree args
, int extra_levels
)
620 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
625 /* If N is 1, just return the outermost set of template arguments. */
627 return TMPL_ARGS_LEVEL (args
, 1);
629 /* If we're not removing anything, just return the arguments we were
631 gcc_assert (extra_levels
>= 0);
632 if (extra_levels
== 0)
635 /* Make a new set of arguments, not containing the inner arguments. */
636 new_args
= make_tree_vec (n
);
637 for (i
= 1; i
<= n
; ++i
)
638 SET_TMPL_ARGS_LEVEL (new_args
, i
,
639 TMPL_ARGS_LEVEL (args
, i
));
644 /* We've got a template header coming up; push to a new level for storing
648 begin_template_parm_list (void)
650 /* We use a non-tag-transparent scope here, which causes pushtag to
651 put tags in this scope, rather than in the enclosing class or
652 namespace scope. This is the right thing, since we want
653 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
654 global template class, push_template_decl handles putting the
655 TEMPLATE_DECL into top-level scope. For a nested template class,
658 template <class T> struct S1 {
659 template <class T> struct S2 {};
662 pushtag contains special code to call pushdecl_with_scope on the
663 TEMPLATE_DECL for S2. */
664 begin_scope (sk_template_parms
, NULL
);
665 ++processing_template_decl
;
666 ++processing_template_parmlist
;
667 note_template_header (0);
670 /* This routine is called when a specialization is declared. If it is
671 invalid to declare a specialization here, an error is reported and
672 false is returned, otherwise this routine will return true. */
675 check_specialization_scope (void)
677 tree scope
= current_scope ();
681 An explicit specialization shall be declared in the namespace of
682 which the template is a member, or, for member templates, in the
683 namespace of which the enclosing class or enclosing class
684 template is a member. An explicit specialization of a member
685 function, member class or static data member of a class template
686 shall be declared in the namespace of which the class template
688 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
690 error ("explicit specialization in non-namespace scope %qD", scope
);
696 In an explicit specialization declaration for a member of a class
697 template or a member template that appears in namespace scope,
698 the member template and some of its enclosing class templates may
699 remain unspecialized, except that the declaration shall not
700 explicitly specialize a class member template if its enclosing
701 class templates are not explicitly specialized as well. */
702 if (current_template_parms
)
704 error ("enclosing class templates are not explicitly specialized");
711 /* We've just seen template <>. */
714 begin_specialization (void)
716 begin_scope (sk_template_spec
, NULL
);
717 note_template_header (1);
718 return check_specialization_scope ();
721 /* Called at then end of processing a declaration preceded by
725 end_specialization (void)
728 reset_specialization ();
731 /* Any template <>'s that we have seen thus far are not referring to a
732 function specialization. */
735 reset_specialization (void)
737 processing_specialization
= 0;
738 template_header_count
= 0;
741 /* We've just seen a template header. If SPECIALIZATION is nonzero,
742 it was of the form template <>. */
745 note_template_header (int specialization
)
747 processing_specialization
= specialization
;
748 template_header_count
++;
751 /* We're beginning an explicit instantiation. */
754 begin_explicit_instantiation (void)
756 gcc_assert (!processing_explicit_instantiation
);
757 processing_explicit_instantiation
= true;
762 end_explicit_instantiation (void)
764 gcc_assert (processing_explicit_instantiation
);
765 processing_explicit_instantiation
= false;
768 /* An explicit specialization or partial specialization of TMPL is being
769 declared. Check that the namespace in which the specialization is
770 occurring is permissible. Returns false iff it is invalid to
771 specialize TMPL in the current namespace. */
774 check_specialization_namespace (tree tmpl
)
776 tree tpl_ns
= decl_namespace_context (tmpl
);
780 An explicit specialization shall be declared in the namespace of
781 which the template is a member, or, for member templates, in the
782 namespace of which the enclosing class or enclosing class
783 template is a member. An explicit specialization of a member
784 function, member class or static data member of a class template
785 shall be declared in the namespace of which the class template is
787 if (current_scope() != DECL_CONTEXT (tmpl
)
788 && !at_namespace_scope_p ())
790 error ("specialization of %qD must appear at namespace scope", tmpl
);
793 if (is_associated_namespace (current_namespace
, tpl_ns
))
794 /* Same or super-using namespace. */
798 permerror (input_location
, "specialization of %qD in different namespace", tmpl
);
799 permerror (input_location
, " 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
, "specializing %q#T in different namespace", type
);
926 permerror (input_location
, " from definition of %q+#D", tmpl
);
929 /* Check for invalid specialization after instantiation:
931 template <> template <> class C<int>::D<int>;
932 template <> template <class U> class C<int>::D; */
934 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
935 t
; t
= TREE_CHAIN (t
))
937 tree inst
= TREE_VALUE (t
);
938 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
939 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
941 /* We already have a full specialization of this partial
942 instantiation, or a full specialization has been
943 looked up but not instantiated. Reassign it to the
944 new member specialization template. */
948 elt
.tmpl
= most_general_template (tmpl
);
949 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
952 type_specializations
->remove_elt (&elt
);
955 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
958 = type_specializations
->find_slot (&elt
, INSERT
);
959 entry
= ggc_alloc
<spec_entry
> ();
964 /* But if we've had an implicit instantiation, that's a
965 problem ([temp.expl.spec]/6). */
966 error ("specialization %qT after instantiation %qT",
970 /* Mark TYPE as a specialization. And as a result, we only
971 have one level of template argument for the innermost
973 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
974 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
975 CLASSTYPE_TI_ARGS (type
)
976 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
979 else if (processing_specialization
)
981 /* Someday C++0x may allow for enum template specialization. */
982 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
983 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
984 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
985 "of %qD not allowed by ISO C++", type
);
988 error ("explicit specialization of non-template %qT", type
);
989 return error_mark_node
;
996 /* Returns nonzero if we can optimize the retrieval of specializations
997 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
998 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1001 optimize_specialization_lookup_p (tree tmpl
)
1003 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1004 && DECL_CLASS_SCOPE_P (tmpl
)
1005 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1007 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1008 /* The optimized lookup depends on the fact that the
1009 template arguments for the member function template apply
1010 purely to the containing class, which is not true if the
1011 containing class is an explicit or partial
1013 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1014 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1015 && !DECL_CONV_FN_P (tmpl
)
1016 /* It is possible to have a template that is not a member
1017 template and is not a member of a template class:
1019 template <typename T>
1020 struct S { friend A::f(); };
1022 Here, the friend function is a template, but the context does
1023 not have template information. The optimized lookup relies
1024 on having ARGS be the template arguments for both the class
1025 and the function template. */
1026 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1029 /* Retrieve the specialization (in the sense of [temp.spec] - a
1030 specialization is either an instantiation or an explicit
1031 specialization) of TMPL for the given template ARGS. If there is
1032 no such specialization, return NULL_TREE. The ARGS are a vector of
1033 arguments, or a vector of vectors of arguments, in the case of
1034 templates with more than one level of parameters.
1036 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1037 then we search for a partial specialization matching ARGS. This
1038 parameter is ignored if TMPL is not a class template.
1040 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1041 result is a NONTYPE_ARGUMENT_PACK. */
1044 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1046 if (tmpl
== NULL_TREE
)
1049 if (args
== error_mark_node
)
1052 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1053 || TREE_CODE (tmpl
) == FIELD_DECL
);
1055 /* There should be as many levels of arguments as there are
1056 levels of parameters. */
1057 gcc_assert (TMPL_ARGS_DEPTH (args
)
1058 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1059 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1060 : template_class_depth (DECL_CONTEXT (tmpl
))));
1062 if (optimize_specialization_lookup_p (tmpl
))
1064 tree class_template
;
1065 tree class_specialization
;
1066 vec
<tree
, va_gc
> *methods
;
1070 /* The template arguments actually apply to the containing
1071 class. Find the class specialization with those
1073 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1074 class_specialization
1075 = retrieve_specialization (class_template
, args
, 0);
1076 if (!class_specialization
)
1078 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1079 for the specialization. */
1080 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1083 /* Iterate through the methods with the indicated name, looking
1084 for the one that has an instance of TMPL. */
1085 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1086 for (fns
= (*methods
)[idx
]; fns
; fns
= OVL_NEXT (fns
))
1088 tree fn
= OVL_CURRENT (fns
);
1089 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1090 /* using-declarations can add base methods to the method vec,
1091 and we don't want those here. */
1092 && DECL_CONTEXT (fn
) == class_specialization
)
1101 hash_table
<spec_hasher
> *specializations
;
1105 elt
.spec
= NULL_TREE
;
1107 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1108 specializations
= type_specializations
;
1110 specializations
= decl_specializations
;
1113 hash
= spec_hasher::hash (&elt
);
1114 found
= specializations
->find_with_hash (&elt
, hash
);
1122 /* Like retrieve_specialization, but for local declarations. */
1125 retrieve_local_specialization (tree tmpl
)
1127 if (local_specializations
== NULL
)
1130 tree
*slot
= local_specializations
->get (tmpl
);
1131 return slot
? *slot
: NULL_TREE
;
1134 /* Returns nonzero iff DECL is a specialization of TMPL. */
1137 is_specialization_of (tree decl
, tree tmpl
)
1141 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1145 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1151 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1153 for (t
= TREE_TYPE (decl
);
1155 t
= CLASSTYPE_USE_TEMPLATE (t
)
1156 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1157 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1164 /* Returns nonzero iff DECL is a specialization of friend declaration
1165 FRIEND_DECL according to [temp.friend]. */
1168 is_specialization_of_friend (tree decl
, tree friend_decl
)
1170 bool need_template
= true;
1173 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1174 || TREE_CODE (decl
) == TYPE_DECL
);
1176 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1177 of a template class, we want to check if DECL is a specialization
1179 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1180 && DECL_TEMPLATE_INFO (friend_decl
)
1181 && !DECL_USE_TEMPLATE (friend_decl
))
1183 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1184 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1185 need_template
= false;
1187 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1188 && !PRIMARY_TEMPLATE_P (friend_decl
))
1189 need_template
= false;
1191 /* There is nothing to do if this is not a template friend. */
1192 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1195 if (is_specialization_of (decl
, friend_decl
))
1199 A member of a class template may be declared to be a friend of a
1200 non-template class. In this case, the corresponding member of
1201 every specialization of the class template is a friend of the
1202 class granting friendship.
1204 For example, given a template friend declaration
1206 template <class T> friend void A<T>::f();
1208 the member function below is considered a friend
1210 template <> struct A<int> {
1214 For this type of template friend, TEMPLATE_DEPTH below will be
1215 nonzero. To determine if DECL is a friend of FRIEND, we first
1216 check if the enclosing class is a specialization of another. */
1218 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1220 && DECL_CLASS_SCOPE_P (decl
)
1221 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1222 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1224 /* Next, we check the members themselves. In order to handle
1225 a few tricky cases, such as when FRIEND_DECL's are
1227 template <class T> friend void A<T>::g(T t);
1228 template <class T> template <T t> friend void A<T>::h();
1232 void A<int>::g(int);
1233 template <int> void A<int>::h();
1235 we need to figure out ARGS, the template arguments from
1236 the context of DECL. This is required for template substitution
1237 of `T' in the function parameter of `g' and template parameter
1238 of `h' in the above examples. Here ARGS corresponds to `int'. */
1240 tree context
= DECL_CONTEXT (decl
);
1241 tree args
= NULL_TREE
;
1242 int current_depth
= 0;
1244 while (current_depth
< template_depth
)
1246 if (CLASSTYPE_TEMPLATE_INFO (context
))
1248 if (current_depth
== 0)
1249 args
= TYPE_TI_ARGS (context
);
1251 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1254 context
= TYPE_CONTEXT (context
);
1257 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1262 tree friend_args_type
;
1263 tree decl_args_type
;
1265 /* Make sure that both DECL and FRIEND_DECL are templates or
1267 is_template
= DECL_TEMPLATE_INFO (decl
)
1268 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1269 if (need_template
^ is_template
)
1271 else if (is_template
)
1273 /* If both are templates, check template parameter list. */
1275 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1277 if (!comp_template_parms
1278 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1282 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1285 decl_type
= TREE_TYPE (decl
);
1287 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1288 tf_none
, NULL_TREE
);
1289 if (friend_type
== error_mark_node
)
1292 /* Check if return types match. */
1293 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1296 /* Check if function parameter types match, ignoring the
1297 `this' parameter. */
1298 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1299 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1300 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1301 friend_args_type
= TREE_CHAIN (friend_args_type
);
1302 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1303 decl_args_type
= TREE_CHAIN (decl_args_type
);
1305 return compparms (decl_args_type
, friend_args_type
);
1309 /* DECL is a TYPE_DECL */
1311 tree decl_type
= TREE_TYPE (decl
);
1313 /* Make sure that both DECL and FRIEND_DECL are templates or
1316 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1317 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1319 if (need_template
^ is_template
)
1321 else if (is_template
)
1324 /* If both are templates, check the name of the two
1325 TEMPLATE_DECL's first because is_friend didn't. */
1326 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1327 != DECL_NAME (friend_decl
))
1330 /* Now check template parameter list. */
1332 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1334 return comp_template_parms
1335 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1339 return (DECL_NAME (decl
)
1340 == DECL_NAME (friend_decl
));
1346 /* Register the specialization SPEC as a specialization of TMPL with
1347 the indicated ARGS. IS_FRIEND indicates whether the specialization
1348 is actually just a friend declaration. Returns SPEC, or an
1349 equivalent prior declaration, if available.
1351 We also store instantiations of field packs in the hash table, even
1352 though they are not themselves templates, to make lookup easier. */
1355 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1359 spec_entry
**slot
= NULL
;
1362 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1363 || (TREE_CODE (tmpl
) == FIELD_DECL
1364 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1366 if (TREE_CODE (spec
) == FUNCTION_DECL
1367 && uses_template_parms (DECL_TI_ARGS (spec
)))
1368 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1369 register it; we want the corresponding TEMPLATE_DECL instead.
1370 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1371 the more obvious `uses_template_parms (spec)' to avoid problems
1372 with default function arguments. In particular, given
1373 something like this:
1375 template <class T> void f(T t1, T t = T())
1377 the default argument expression is not substituted for in an
1378 instantiation unless and until it is actually needed. */
1381 if (optimize_specialization_lookup_p (tmpl
))
1382 /* We don't put these specializations in the hash table, but we might
1383 want to give an error about a mismatch. */
1384 fn
= retrieve_specialization (tmpl
, args
, 0);
1392 hash
= spec_hasher::hash (&elt
);
1395 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1397 fn
= ((spec_entry
*) *slot
)->spec
;
1402 /* We can sometimes try to re-register a specialization that we've
1403 already got. In particular, regenerate_decl_from_template calls
1404 duplicate_decls which will update the specialization list. But,
1405 we'll still get called again here anyhow. It's more convenient
1406 to simply allow this than to try to prevent it. */
1409 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1411 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1413 if (DECL_ODR_USED (fn
)
1414 || DECL_EXPLICIT_INSTANTIATION (fn
))
1416 error ("specialization of %qD after instantiation",
1418 return error_mark_node
;
1423 /* This situation should occur only if the first
1424 specialization is an implicit instantiation, the
1425 second is an explicit specialization, and the
1426 implicit instantiation has not yet been used. That
1427 situation can occur if we have implicitly
1428 instantiated a member function and then specialized
1431 We can also wind up here if a friend declaration that
1432 looked like an instantiation turns out to be a
1435 template <class T> void foo(T);
1436 class S { friend void foo<>(int) };
1437 template <> void foo(int);
1439 We transform the existing DECL in place so that any
1440 pointers to it become pointers to the updated
1443 If there was a definition for the template, but not
1444 for the specialization, we want this to look as if
1445 there were no definition, and vice versa. */
1446 DECL_INITIAL (fn
) = NULL_TREE
;
1447 duplicate_decls (spec
, fn
, is_friend
);
1448 /* The call to duplicate_decls will have applied
1451 An explicit specialization of a function template
1452 is inline only if it is explicitly declared to be,
1453 and independently of whether its function template
1456 to the primary function; now copy the inline bits to
1457 the various clones. */
1458 FOR_EACH_CLONE (clone
, fn
)
1460 DECL_DECLARED_INLINE_P (clone
)
1461 = DECL_DECLARED_INLINE_P (fn
);
1462 DECL_SOURCE_LOCATION (clone
)
1463 = DECL_SOURCE_LOCATION (fn
);
1464 DECL_DELETED_FN (clone
)
1465 = DECL_DELETED_FN (fn
);
1467 check_specialization_namespace (tmpl
);
1472 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1474 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1475 /* Dup decl failed, but this is a new definition. Set the
1476 line number so any errors match this new
1478 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1484 return duplicate_decls (spec
, fn
, is_friend
);
1486 /* A specialization must be declared in the same namespace as the
1487 template it is specializing. */
1488 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1489 && !check_specialization_namespace (tmpl
))
1490 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1492 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1494 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1495 gcc_assert (tmpl
&& args
&& spec
);
1498 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1499 && PRIMARY_TEMPLATE_P (tmpl
)
1500 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1501 || variable_template_p (tmpl
))
1502 /* If TMPL is a forward declaration of a template function, keep a list
1503 of all specializations in case we need to reassign them to a friend
1504 template later in tsubst_friend_function.
1506 Also keep a list of all variable template instantiations so that
1507 process_partial_specialization can check whether a later partial
1508 specialization would have used it. */
1509 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1510 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1516 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1517 TMPL and ARGS members, ignores SPEC. */
1519 int comparing_specializations
;
1522 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1526 ++comparing_specializations
;
1527 equal
= (e1
->tmpl
== e2
->tmpl
1528 && comp_template_args (e1
->args
, e2
->args
));
1529 --comparing_specializations
;
1534 /* Returns a hash for a template TMPL and template arguments ARGS. */
1537 hash_tmpl_and_args (tree tmpl
, tree args
)
1539 hashval_t val
= DECL_UID (tmpl
);
1540 return iterative_hash_template_arg (args
, val
);
1543 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1547 spec_hasher::hash (spec_entry
*e
)
1549 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1552 /* Recursively calculate a hash value for a template argument ARG, for use
1553 in the hash tables of template specializations. */
1556 iterative_hash_template_arg (tree arg
, hashval_t val
)
1558 unsigned HOST_WIDE_INT i
;
1559 enum tree_code code
;
1562 if (arg
== NULL_TREE
)
1563 return iterative_hash_object (arg
, val
);
1568 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1569 /* We can get one of these when re-hashing a previous entry in the middle
1570 of substituting into a pack expansion. Just look through it. */
1571 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1573 code
= TREE_CODE (arg
);
1574 tclass
= TREE_CODE_CLASS (code
);
1576 val
= iterative_hash_object (code
, val
);
1583 case IDENTIFIER_NODE
:
1584 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1588 int i
, len
= TREE_VEC_LENGTH (arg
);
1589 for (i
= 0; i
< len
; ++i
)
1590 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1594 case TYPE_PACK_EXPANSION
:
1595 case EXPR_PACK_EXPANSION
:
1596 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1597 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1599 case TYPE_ARGUMENT_PACK
:
1600 case NONTYPE_ARGUMENT_PACK
:
1601 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1604 for (; arg
; arg
= TREE_CHAIN (arg
))
1605 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1609 for (; arg
; arg
= OVL_NEXT (arg
))
1610 val
= iterative_hash_template_arg (OVL_CURRENT (arg
), val
);
1616 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1617 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1619 val
= iterative_hash_template_arg (field
, val
);
1620 val
= iterative_hash_template_arg (value
, val
);
1626 if (!DECL_ARTIFICIAL (arg
))
1628 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1629 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1631 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1634 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1637 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1638 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1640 case TEMPLATE_PARM_INDEX
:
1641 val
= iterative_hash_template_arg
1642 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1643 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1644 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1647 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1648 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1649 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1652 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1654 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1658 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1659 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1660 val
= iterative_hash_object (code
, val
);
1661 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1664 /* A lambda can't appear in a template arg, but don't crash on
1666 gcc_assert (seen_error ());
1670 case IMPLICIT_CONV_EXPR
:
1671 case STATIC_CAST_EXPR
:
1672 case REINTERPRET_CAST_EXPR
:
1673 case CONST_CAST_EXPR
:
1674 case DYNAMIC_CAST_EXPR
:
1676 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1677 /* Now hash operands as usual. */
1687 if (alias_template_specialization_p (arg
))
1689 // We want an alias specialization that survived strip_typedefs
1690 // to hash differently from its TYPE_CANONICAL, to avoid hash
1691 // collisions that compare as different in template_args_equal.
1692 // These could be dependent specializations that strip_typedefs
1693 // left alone, or untouched specializations because
1694 // coerce_template_parms returns the unconverted template
1695 // arguments if it sees incomplete argument packs.
1696 tree ti
= TYPE_TEMPLATE_INFO (arg
);
1697 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1699 if (TYPE_CANONICAL (arg
))
1700 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1702 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1703 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1704 /* Otherwise just compare the types during lookup. */
1707 case tcc_declaration
:
1709 return iterative_hash_expr (arg
, val
);
1712 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1714 unsigned n
= cp_tree_operand_length (arg
);
1715 for (i
= 0; i
< n
; ++i
)
1716 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1724 /* Unregister the specialization SPEC as a specialization of TMPL.
1725 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1726 if the SPEC was listed as a specialization of TMPL.
1728 Note that SPEC has been ggc_freed, so we can't look inside it. */
1731 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1736 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1737 elt
.args
= TI_ARGS (tinfo
);
1738 elt
.spec
= NULL_TREE
;
1740 entry
= decl_specializations
->find (&elt
);
1743 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1744 gcc_assert (new_spec
!= NULL_TREE
);
1745 entry
->spec
= new_spec
;
1752 /* Like register_specialization, but for local declarations. We are
1753 registering SPEC, an instantiation of TMPL. */
1756 register_local_specialization (tree spec
, tree tmpl
)
1758 local_specializations
->put (tmpl
, spec
);
1761 /* TYPE is a class type. Returns true if TYPE is an explicitly
1762 specialized class. */
1765 explicit_class_specialization_p (tree type
)
1767 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1769 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1772 /* Print the list of functions at FNS, going through all the overloads
1773 for each element of the list. Alternatively, FNS can not be a
1774 TREE_LIST, in which case it will be printed together with all the
1777 MORE and *STR should respectively be FALSE and NULL when the function
1778 is called from the outside. They are used internally on recursive
1779 calls. print_candidates manages the two parameters and leaves NULL
1780 in *STR when it ends. */
1783 print_candidates_1 (tree fns
, bool more
, const char **str
)
1786 char *spaces
= NULL
;
1788 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1789 if (TREE_CODE (fn
) == TREE_LIST
)
1791 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1792 print_candidates_1 (TREE_VALUE (fn2
),
1793 TREE_CHAIN (fn2
) || more
, str
);
1797 tree cand
= OVL_CURRENT (fn
);
1800 /* Pick the prefix string. */
1801 if (!more
&& !OVL_NEXT (fns
))
1803 inform (DECL_SOURCE_LOCATION (cand
),
1804 "candidate is: %#D", cand
);
1808 *str
= _("candidates are:");
1809 spaces
= get_spaces (*str
);
1811 inform (DECL_SOURCE_LOCATION (cand
), "%s %#D", *str
, cand
);
1812 *str
= spaces
? spaces
: *str
;
1822 /* Print the list of candidate FNS in an error message. FNS can also
1823 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1826 print_candidates (tree fns
)
1828 const char *str
= NULL
;
1829 print_candidates_1 (fns
, false, &str
);
1830 gcc_assert (str
== NULL
);
1833 /* Returns the template (one of the functions given by TEMPLATE_ID)
1834 which can be specialized to match the indicated DECL with the
1835 explicit template args given in TEMPLATE_ID. The DECL may be
1836 NULL_TREE if none is available. In that case, the functions in
1837 TEMPLATE_ID are non-members.
1839 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1840 specialization of a member template.
1842 The TEMPLATE_COUNT is the number of references to qualifying
1843 template classes that appeared in the name of the function. See
1844 check_explicit_specialization for a more accurate description.
1846 TSK indicates what kind of template declaration (if any) is being
1847 declared. TSK_TEMPLATE indicates that the declaration given by
1848 DECL, though a FUNCTION_DECL, has template parameters, and is
1849 therefore a template function.
1851 The template args (those explicitly specified and those deduced)
1852 are output in a newly created vector *TARGS_OUT.
1854 If it is impossible to determine the result, an error message is
1855 issued. The error_mark_node is returned to indicate failure. */
1858 determine_specialization (tree template_id
,
1861 int need_member_template
,
1867 tree explicit_targs
;
1868 tree candidates
= NULL_TREE
;
1869 /* A TREE_LIST of templates of which DECL may be a specialization.
1870 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1871 corresponding TREE_PURPOSE is the set of template arguments that,
1872 when used to instantiate the template, would produce a function
1873 with the signature of DECL. */
1874 tree templates
= NULL_TREE
;
1876 cp_binding_level
*b
;
1878 *targs_out
= NULL_TREE
;
1880 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1881 return error_mark_node
;
1883 /* We shouldn't be specializing a member template of an
1884 unspecialized class template; we already gave an error in
1885 check_specialization_scope, now avoid crashing. */
1886 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
1887 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
1889 gcc_assert (errorcount
);
1890 return error_mark_node
;
1893 fns
= TREE_OPERAND (template_id
, 0);
1894 explicit_targs
= TREE_OPERAND (template_id
, 1);
1896 if (fns
== error_mark_node
)
1897 return error_mark_node
;
1899 /* Check for baselinks. */
1900 if (BASELINK_P (fns
))
1901 fns
= BASELINK_FUNCTIONS (fns
);
1903 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
1905 error ("%qD is not a function template", fns
);
1906 return error_mark_node
;
1908 else if (VAR_P (decl
) && !variable_template_p (fns
))
1910 error ("%qD is not a variable template", fns
);
1911 return error_mark_node
;
1914 /* Count the number of template headers specified for this
1917 for (b
= current_binding_level
;
1918 b
->kind
== sk_template_parms
;
1922 if (variable_template_p (fns
))
1924 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
1925 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
1926 tf_warning_or_error
,
1927 /*req_all*/true, /*use_defarg*/true);
1928 templates
= tree_cons (targs
, fns
, templates
);
1930 else for (; fns
; fns
= OVL_NEXT (fns
))
1932 tree fn
= OVL_CURRENT (fns
);
1934 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1936 tree decl_arg_types
;
1940 /* In case of explicit specialization, we need to check if
1941 the number of template headers appearing in the specialization
1942 is correct. This is usually done in check_explicit_specialization,
1943 but the check done there cannot be exhaustive when specializing
1944 member functions. Consider the following code:
1946 template <> void A<int>::f(int);
1947 template <> template <> void A<int>::f(int);
1949 Assuming that A<int> is not itself an explicit specialization
1950 already, the first line specializes "f" which is a non-template
1951 member function, whilst the second line specializes "f" which
1952 is a template member function. So both lines are syntactically
1953 correct, and check_explicit_specialization does not reject
1956 Here, we can do better, as we are matching the specialization
1957 against the declarations. We count the number of template
1958 headers, and we check if they match TEMPLATE_COUNT + 1
1959 (TEMPLATE_COUNT is the number of qualifying template classes,
1960 plus there must be another header for the member template
1963 Notice that if header_count is zero, this is not a
1964 specialization but rather a template instantiation, so there
1965 is no check we can perform here. */
1966 if (header_count
&& header_count
!= template_count
+ 1)
1969 /* Check that the number of template arguments at the
1970 innermost level for DECL is the same as for FN. */
1971 if (current_binding_level
->kind
== sk_template_parms
1972 && !current_binding_level
->explicit_spec_p
1973 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1974 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1975 (current_template_parms
))))
1978 /* DECL might be a specialization of FN. */
1979 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1980 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1982 /* For a non-static member function, we need to make sure
1983 that the const qualification is the same. Since
1984 get_bindings does not try to merge the "this" parameter,
1985 we must do the comparison explicitly. */
1986 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1987 && !same_type_p (TREE_VALUE (fn_arg_types
),
1988 TREE_VALUE (decl_arg_types
)))
1991 /* Skip the "this" parameter and, for constructors of
1992 classes with virtual bases, the VTT parameter. A
1993 full specialization of a constructor will have a VTT
1994 parameter, but a template never will. */
1996 = skip_artificial_parms_for (decl
, decl_arg_types
);
1998 = skip_artificial_parms_for (fn
, fn_arg_types
);
2000 /* Function templates cannot be specializations; there are
2001 no partial specializations of functions. Therefore, if
2002 the type of DECL does not match FN, there is no
2004 if (tsk
== tsk_template
)
2006 if (compparms (fn_arg_types
, decl_arg_types
))
2007 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2011 /* See whether this function might be a specialization of this
2012 template. Suppress access control because we might be trying
2013 to make this specialization a friend, and we have already done
2014 access control for the declaration of the specialization. */
2015 push_deferring_access_checks (dk_no_check
);
2016 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2017 pop_deferring_access_checks ();
2020 /* We cannot deduce template arguments that when used to
2021 specialize TMPL will produce DECL. */
2024 /* Make sure that the deduced arguments actually work. */
2025 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
2026 if (insttype
== error_mark_node
)
2029 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2030 if (!compparms (fn_arg_types
, decl_arg_types
))
2033 /* Save this template, and the arguments deduced. */
2034 templates
= tree_cons (targs
, fn
, templates
);
2036 else if (need_member_template
)
2037 /* FN is an ordinary member function, and we need a
2038 specialization of a member template. */
2040 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2041 /* We can get IDENTIFIER_NODEs here in certain erroneous
2044 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2045 /* This is just an ordinary non-member function. Nothing can
2046 be a specialization of that. */
2048 else if (DECL_ARTIFICIAL (fn
))
2049 /* Cannot specialize functions that are created implicitly. */
2053 tree decl_arg_types
;
2055 /* This is an ordinary member function. However, since
2056 we're here, we can assume its enclosing class is a
2057 template class. For example,
2059 template <typename T> struct S { void f(); };
2060 template <> void S<int>::f() {}
2062 Here, S<int>::f is a non-template, but S<int> is a
2063 template class. If FN has the same type as DECL, we
2064 might be in business. */
2066 if (!DECL_TEMPLATE_INFO (fn
))
2067 /* Its enclosing class is an explicit specialization
2068 of a template class. This is not a candidate. */
2071 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2072 TREE_TYPE (TREE_TYPE (fn
))))
2073 /* The return types differ. */
2076 /* Adjust the type of DECL in case FN is a static member. */
2077 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2078 if (DECL_STATIC_FUNCTION_P (fn
)
2079 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2080 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2082 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2085 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2089 if (templates
&& TREE_CHAIN (templates
))
2095 It is possible for a specialization with a given function
2096 signature to be instantiated from more than one function
2097 template. In such cases, explicit specification of the
2098 template arguments must be used to uniquely identify the
2099 function template specialization being specialized.
2101 Note that here, there's no suggestion that we're supposed to
2102 determine which of the candidate templates is most
2103 specialized. However, we, also have:
2107 Partial ordering of overloaded function template
2108 declarations is used in the following contexts to select
2109 the function template to which a function template
2110 specialization refers:
2112 -- when an explicit specialization refers to a function
2115 So, we do use the partial ordering rules, at least for now.
2116 This extension can only serve to make invalid programs valid,
2117 so it's safe. And, there is strong anecdotal evidence that
2118 the committee intended the partial ordering rules to apply;
2119 the EDG front end has that behavior, and John Spicer claims
2120 that the committee simply forgot to delete the wording in
2121 [temp.expl.spec]. */
2122 tree tmpl
= most_specialized_instantiation (templates
);
2123 if (tmpl
!= error_mark_node
)
2126 TREE_CHAIN (templates
) = NULL_TREE
;
2130 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2132 error ("template-id %qD for %q+D does not match any template "
2133 "declaration", template_id
, decl
);
2134 if (header_count
&& header_count
!= template_count
+ 1)
2135 inform (input_location
, "saw %d %<template<>%>, need %d for "
2136 "specializing a member function template",
2137 header_count
, template_count
+ 1);
2138 return error_mark_node
;
2140 else if ((templates
&& TREE_CHAIN (templates
))
2141 || (candidates
&& TREE_CHAIN (candidates
))
2142 || (templates
&& candidates
))
2144 error ("ambiguous template specialization %qD for %q+D",
2146 candidates
= chainon (candidates
, templates
);
2147 print_candidates (candidates
);
2148 return error_mark_node
;
2151 /* We have one, and exactly one, match. */
2154 tree fn
= TREE_VALUE (candidates
);
2155 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2156 /* DECL is a re-declaration or partial instantiation of a template
2158 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2160 /* It was a specialization of an ordinary member function in a
2162 return DECL_TI_TEMPLATE (fn
);
2165 /* It was a specialization of a template. */
2166 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2167 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2169 *targs_out
= copy_node (targs
);
2170 SET_TMPL_ARGS_LEVEL (*targs_out
,
2171 TMPL_ARGS_DEPTH (*targs_out
),
2172 TREE_PURPOSE (templates
));
2175 *targs_out
= TREE_PURPOSE (templates
);
2176 return TREE_VALUE (templates
);
2179 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2180 but with the default argument values filled in from those in the
2184 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2187 tree new_spec_types
;
2192 if (spec_types
== void_list_node
)
2193 return void_list_node
;
2195 /* Substitute into the rest of the list. */
2197 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2198 TREE_CHAIN (tmpl_types
));
2200 /* Add the default argument for this parameter. */
2201 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2202 TREE_VALUE (spec_types
),
2206 /* DECL is an explicit specialization. Replicate default arguments
2207 from the template it specializes. (That way, code like:
2209 template <class T> void f(T = 3);
2210 template <> void f(double);
2213 works, as required.) An alternative approach would be to look up
2214 the correct default arguments at the call-site, but this approach
2215 is consistent with how implicit instantiations are handled. */
2218 copy_default_args_to_explicit_spec (tree decl
)
2223 tree new_spec_types
;
2227 tree object_type
= NULL_TREE
;
2228 tree in_charge
= NULL_TREE
;
2229 tree vtt
= NULL_TREE
;
2231 /* See if there's anything we need to do. */
2232 tmpl
= DECL_TI_TEMPLATE (decl
);
2233 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2234 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2235 if (TREE_PURPOSE (t
))
2240 old_type
= TREE_TYPE (decl
);
2241 spec_types
= TYPE_ARG_TYPES (old_type
);
2243 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2245 /* Remove the this pointer, but remember the object's type for
2247 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2248 spec_types
= TREE_CHAIN (spec_types
);
2249 tmpl_types
= TREE_CHAIN (tmpl_types
);
2251 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2253 /* DECL may contain more parameters than TMPL due to the extra
2254 in-charge parameter in constructors and destructors. */
2255 in_charge
= spec_types
;
2256 spec_types
= TREE_CHAIN (spec_types
);
2258 if (DECL_HAS_VTT_PARM_P (decl
))
2261 spec_types
= TREE_CHAIN (spec_types
);
2265 /* Compute the merged default arguments. */
2267 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2269 /* Compute the new FUNCTION_TYPE. */
2273 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2278 /* Put the in-charge parameter back. */
2279 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2280 TREE_VALUE (in_charge
),
2283 new_type
= build_method_type_directly (object_type
,
2284 TREE_TYPE (old_type
),
2288 new_type
= build_function_type (TREE_TYPE (old_type
),
2290 new_type
= cp_build_type_attribute_variant (new_type
,
2291 TYPE_ATTRIBUTES (old_type
));
2292 new_type
= build_exception_variant (new_type
,
2293 TYPE_RAISES_EXCEPTIONS (old_type
));
2295 if (TYPE_HAS_LATE_RETURN_TYPE (old_type
))
2296 TYPE_HAS_LATE_RETURN_TYPE (new_type
) = 1;
2298 TREE_TYPE (decl
) = new_type
;
2301 /* Return the number of template headers we expect to see for a definition
2302 or specialization of CTYPE or one of its non-template members. */
2305 num_template_headers_for_class (tree ctype
)
2307 int num_templates
= 0;
2309 while (ctype
&& CLASS_TYPE_P (ctype
))
2311 /* You're supposed to have one `template <...>' for every
2312 template class, but you don't need one for a full
2313 specialization. For example:
2315 template <class T> struct S{};
2316 template <> struct S<int> { void f(); };
2317 void S<int>::f () {}
2319 is correct; there shouldn't be a `template <>' for the
2320 definition of `S<int>::f'. */
2321 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2322 /* If CTYPE does not have template information of any
2323 kind, then it is not a template, nor is it nested
2324 within a template. */
2326 if (explicit_class_specialization_p (ctype
))
2328 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2331 ctype
= TYPE_CONTEXT (ctype
);
2334 return num_templates
;
2337 /* Do a simple sanity check on the template headers that precede the
2338 variable declaration DECL. */
2341 check_template_variable (tree decl
)
2343 tree ctx
= CP_DECL_CONTEXT (decl
);
2344 int wanted
= num_template_headers_for_class (ctx
);
2345 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2346 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2348 if (cxx_dialect
< cxx14
)
2349 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2350 "variable templates only available with "
2351 "-std=c++14 or -std=gnu++14");
2353 // Namespace-scope variable templates should have a template header.
2356 if (template_header_count
> wanted
)
2358 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2359 "too many template headers for %D (should be %d)",
2361 if (warned
&& CLASS_TYPE_P (ctx
)
2362 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2363 inform (DECL_SOURCE_LOCATION (decl
),
2364 "members of an explicitly specialized class are defined "
2365 "without a template header");
2369 /* Check to see if the function just declared, as indicated in
2370 DECLARATOR, and in DECL, is a specialization of a function
2371 template. We may also discover that the declaration is an explicit
2372 instantiation at this point.
2374 Returns DECL, or an equivalent declaration that should be used
2375 instead if all goes well. Issues an error message if something is
2376 amiss. Returns error_mark_node if the error is not easily
2379 FLAGS is a bitmask consisting of the following flags:
2381 2: The function has a definition.
2382 4: The function is a friend.
2384 The TEMPLATE_COUNT is the number of references to qualifying
2385 template classes that appeared in the name of the function. For
2388 template <class T> struct S { void f(); };
2391 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2392 classes are not counted in the TEMPLATE_COUNT, so that in
2394 template <class T> struct S {};
2395 template <> struct S<int> { void f(); }
2396 template <> void S<int>::f();
2398 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2399 invalid; there should be no template <>.)
2401 If the function is a specialization, it is marked as such via
2402 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2403 is set up correctly, and it is added to the list of specializations
2404 for that template. */
2407 check_explicit_specialization (tree declarator
,
2412 int have_def
= flags
& 2;
2413 int is_friend
= flags
& 4;
2414 int specialization
= 0;
2415 int explicit_instantiation
= 0;
2416 int member_specialization
= 0;
2417 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2418 tree dname
= DECL_NAME (decl
);
2423 if (!processing_specialization
)
2426 tsk
= tsk_excessive_parms
;
2429 tsk
= current_tmpl_spec_kind (template_count
);
2434 if (processing_specialization
)
2437 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2439 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2442 /* This could be something like:
2444 template <class T> void f(T);
2445 class S { friend void f<>(int); } */
2449 /* This case handles bogus declarations like template <>
2450 template <class T> void f<int>(); */
2452 error ("template-id %qD in declaration of primary template",
2459 case tsk_invalid_member_spec
:
2460 /* The error has already been reported in
2461 check_specialization_scope. */
2462 return error_mark_node
;
2464 case tsk_invalid_expl_inst
:
2465 error ("template parameter list used in explicit instantiation");
2471 error ("definition provided for explicit instantiation");
2473 explicit_instantiation
= 1;
2476 case tsk_excessive_parms
:
2477 case tsk_insufficient_parms
:
2478 if (tsk
== tsk_excessive_parms
)
2479 error ("too many template parameter lists in declaration of %qD",
2481 else if (template_header_count
)
2482 error("too few template parameter lists in declaration of %qD", decl
);
2484 error("explicit specialization of %qD must be introduced by "
2485 "%<template <>%>", decl
);
2489 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2490 /* In cases like template<> constexpr bool v = true;
2491 We'll give an error in check_template_variable. */
2494 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2496 member_specialization
= 1;
2502 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2504 /* This case handles bogus declarations like template <>
2505 template <class T> void f<int>(); */
2507 if (!uses_template_parms (declarator
))
2508 error ("template-id %qD in declaration of primary template",
2510 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2512 /* Partial specialization of variable template. */
2513 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2517 else if (cxx_dialect
< cxx14
)
2518 error ("non-type partial specialization %qD "
2519 "is not allowed", declarator
);
2521 error ("non-class, non-variable partial specialization %qD "
2522 "is not allowed", declarator
);
2527 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2528 /* This is a specialization of a member template, without
2529 specialization the containing class. Something like:
2531 template <class T> struct S {
2532 template <class U> void f (U);
2534 template <> template <class U> void S<int>::f(U) {}
2536 That's a specialization -- but of the entire template. */
2544 if ((specialization
|| member_specialization
)
2545 /* This doesn't apply to variable templates. */
2546 && (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
2547 || TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
))
2549 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2550 for (; t
; t
= TREE_CHAIN (t
))
2551 if (TREE_PURPOSE (t
))
2553 permerror (input_location
,
2554 "default argument specified in explicit specialization");
2559 if (specialization
|| member_specialization
|| explicit_instantiation
)
2561 tree tmpl
= NULL_TREE
;
2562 tree targs
= NULL_TREE
;
2563 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2565 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2566 if (!was_template_id
)
2570 gcc_assert (identifier_p (declarator
));
2575 /* If there is no class context, the explicit instantiation
2576 must be at namespace scope. */
2577 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2579 /* Find the namespace binding, using the declaration
2581 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2583 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2585 error ("%qD is not a template function", dname
);
2586 fns
= error_mark_node
;
2590 tree fn
= OVL_CURRENT (fns
);
2591 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2592 CP_DECL_CONTEXT (fn
)))
2593 error ("%qD is not declared in %qD",
2594 decl
, current_namespace
);
2598 declarator
= lookup_template_function (fns
, NULL_TREE
);
2601 if (declarator
== error_mark_node
)
2602 return error_mark_node
;
2604 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2606 if (!explicit_instantiation
)
2607 /* A specialization in class scope. This is invalid,
2608 but the error will already have been flagged by
2609 check_specialization_scope. */
2610 return error_mark_node
;
2613 /* It's not valid to write an explicit instantiation in
2616 class C { template void f(); }
2618 This case is caught by the parser. However, on
2621 template class C { void f(); };
2623 (which is invalid) we can get here. The error will be
2630 else if (ctype
!= NULL_TREE
2631 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2633 // We'll match variable templates in start_decl.
2637 /* Find the list of functions in ctype that have the same
2638 name as the declared function. */
2639 tree name
= TREE_OPERAND (declarator
, 0);
2640 tree fns
= NULL_TREE
;
2643 if (constructor_name_p (name
, ctype
))
2645 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2647 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2648 : !CLASSTYPE_DESTRUCTORS (ctype
))
2650 /* From [temp.expl.spec]:
2652 If such an explicit specialization for the member
2653 of a class template names an implicitly-declared
2654 special member function (clause _special_), the
2655 program is ill-formed.
2657 Similar language is found in [temp.explicit]. */
2658 error ("specialization of implicitly-declared special member function");
2659 return error_mark_node
;
2662 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2665 if (!DECL_CONV_FN_P (decl
))
2667 idx
= lookup_fnfields_1 (ctype
, name
);
2669 fns
= (*CLASSTYPE_METHOD_VEC (ctype
))[idx
];
2673 vec
<tree
, va_gc
> *methods
;
2676 /* For a type-conversion operator, we cannot do a
2677 name-based lookup. We might be looking for `operator
2678 int' which will be a specialization of `operator T'.
2679 So, we find *all* the conversion operators, and then
2680 select from them. */
2683 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2685 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2686 methods
->iterate (idx
, &ovl
);
2689 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2690 /* There are no more conversion functions. */
2693 /* Glue all these conversion functions together
2694 with those we already have. */
2695 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2696 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2700 if (fns
== NULL_TREE
)
2702 error ("no member function %qD declared in %qT", name
, ctype
);
2703 return error_mark_node
;
2706 TREE_OPERAND (declarator
, 0) = fns
;
2709 /* Figure out what exactly is being specialized at this point.
2710 Note that for an explicit instantiation, even one for a
2711 member function, we cannot tell apriori whether the
2712 instantiation is for a member template, or just a member
2713 function of a template class. Even if a member template is
2714 being instantiated, the member template arguments may be
2715 elided if they can be deduced from the rest of the
2717 tmpl
= determine_specialization (declarator
, decl
,
2719 member_specialization
,
2723 if (!tmpl
|| tmpl
== error_mark_node
)
2724 /* We couldn't figure out what this declaration was
2726 return error_mark_node
;
2729 tree gen_tmpl
= most_general_template (tmpl
);
2731 if (explicit_instantiation
)
2733 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2734 is done by do_decl_instantiation later. */
2736 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2737 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2739 if (arg_depth
> parm_depth
)
2741 /* If TMPL is not the most general template (for
2742 example, if TMPL is a friend template that is
2743 injected into namespace scope), then there will
2744 be too many levels of TARGS. Remove some of them
2749 new_targs
= make_tree_vec (parm_depth
);
2750 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2751 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2752 = TREE_VEC_ELT (targs
, i
);
2756 return instantiate_template (tmpl
, targs
, tf_error
);
2759 /* If we thought that the DECL was a member function, but it
2760 turns out to be specializing a static member function,
2761 make DECL a static member function as well. */
2762 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
2763 && DECL_STATIC_FUNCTION_P (tmpl
)
2764 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2765 revert_static_member_fn (decl
);
2767 /* If this is a specialization of a member template of a
2768 template class, we want to return the TEMPLATE_DECL, not
2769 the specialization of it. */
2770 if (tsk
== tsk_template
&& !was_template_id
)
2772 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2773 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2774 DECL_INITIAL (result
) = NULL_TREE
;
2778 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2779 DECL_SOURCE_LOCATION (result
)
2780 = DECL_SOURCE_LOCATION (decl
);
2781 /* We want to use the argument list specified in the
2782 definition, not in the original declaration. */
2783 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2784 for (parm
= DECL_ARGUMENTS (result
); parm
;
2785 parm
= DECL_CHAIN (parm
))
2786 DECL_CONTEXT (parm
) = result
;
2788 return register_specialization (tmpl
, gen_tmpl
, targs
,
2792 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2793 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2795 if (was_template_id
)
2796 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
2798 /* Inherit default function arguments from the template
2799 DECL is specializing. */
2800 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2801 copy_default_args_to_explicit_spec (decl
);
2803 /* This specialization has the same protection as the
2804 template it specializes. */
2805 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2806 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2808 /* 7.1.1-1 [dcl.stc]
2810 A storage-class-specifier shall not be specified in an
2811 explicit specialization...
2813 The parser rejects these, so unless action is taken here,
2814 explicit function specializations will always appear with
2817 The action recommended by the C++ CWG in response to C++
2818 defect report 605 is to make the storage class and linkage
2819 of the explicit specialization match the templated function:
2821 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2823 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2825 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2826 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2828 /* This specialization has the same linkage and visibility as
2829 the function template it specializes. */
2830 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2831 if (! TREE_PUBLIC (decl
))
2833 DECL_INTERFACE_KNOWN (decl
) = 1;
2834 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2836 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2837 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2839 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2840 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2844 /* If DECL is a friend declaration, declared using an
2845 unqualified name, the namespace associated with DECL may
2846 have been set incorrectly. For example, in:
2848 template <typename T> void f(T);
2850 struct S { friend void f<int>(int); }
2853 we will have set the DECL_CONTEXT for the friend
2854 declaration to N, rather than to the global namespace. */
2855 if (DECL_NAMESPACE_SCOPE_P (decl
))
2856 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2858 if (is_friend
&& !have_def
)
2859 /* This is not really a declaration of a specialization.
2860 It's just the name of an instantiation. But, it's not
2861 a request for an instantiation, either. */
2862 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2863 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
2864 /* A specialization is not necessarily COMDAT. */
2865 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
2866 && DECL_DECLARED_INLINE_P (decl
));
2867 else if (TREE_CODE (decl
) == VAR_DECL
)
2868 DECL_COMDAT (decl
) = false;
2870 /* Register this specialization so that we can find it
2872 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2874 /* A 'structor should already have clones. */
2875 gcc_assert (decl
== error_mark_node
2876 || variable_template_p (tmpl
)
2877 || !(DECL_CONSTRUCTOR_P (decl
)
2878 || DECL_DESTRUCTOR_P (decl
))
2879 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
2886 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2887 parameters. These are represented in the same format used for
2888 DECL_TEMPLATE_PARMS. */
2891 comp_template_parms (const_tree parms1
, const_tree parms2
)
2896 if (parms1
== parms2
)
2899 for (p1
= parms1
, p2
= parms2
;
2900 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2901 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2903 tree t1
= TREE_VALUE (p1
);
2904 tree t2
= TREE_VALUE (p2
);
2907 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2908 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2910 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2913 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2915 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2916 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2918 /* If either of the template parameters are invalid, assume
2919 they match for the sake of error recovery. */
2920 if (error_operand_p (parm1
) || error_operand_p (parm2
))
2923 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2926 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2927 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2928 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2930 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2935 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2936 /* One set of parameters has more parameters lists than the
2943 /* Determine whether PARM is a parameter pack. */
2946 template_parameter_pack_p (const_tree parm
)
2948 /* Determine if we have a non-type template parameter pack. */
2949 if (TREE_CODE (parm
) == PARM_DECL
)
2950 return (DECL_TEMPLATE_PARM_P (parm
)
2951 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2952 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
2953 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
2955 /* If this is a list of template parameters, we could get a
2956 TYPE_DECL or a TEMPLATE_DECL. */
2957 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2958 parm
= TREE_TYPE (parm
);
2960 /* Otherwise it must be a type template parameter. */
2961 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2962 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2963 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2966 /* Determine if T is a function parameter pack. */
2969 function_parameter_pack_p (const_tree t
)
2971 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2972 return DECL_PACK_P (t
);
2976 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2977 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2980 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2982 if (! primary_func_tmpl_inst
2983 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2984 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2987 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2990 /* Return true iff the function parameter PARAM_DECL was expanded
2991 from the function parameter pack PACK. */
2994 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
2996 if (DECL_ARTIFICIAL (param_decl
)
2997 || !function_parameter_pack_p (pack
))
3000 /* The parameter pack and its pack arguments have the same
3002 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3005 /* Determine whether ARGS describes a variadic template args list,
3006 i.e., one that is terminated by a template argument pack. */
3009 template_args_variadic_p (tree args
)
3014 if (args
== NULL_TREE
)
3017 args
= INNERMOST_TEMPLATE_ARGS (args
);
3018 nargs
= TREE_VEC_LENGTH (args
);
3023 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3025 return ARGUMENT_PACK_P (last_parm
);
3028 /* Generate a new name for the parameter pack name NAME (an
3029 IDENTIFIER_NODE) that incorporates its */
3032 make_ith_pack_parameter_name (tree name
, int i
)
3034 /* Munge the name to include the parameter index. */
3035 #define NUMBUF_LEN 128
3036 char numbuf
[NUMBUF_LEN
];
3040 if (name
== NULL_TREE
)
3042 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3043 newname_len
= IDENTIFIER_LENGTH (name
)
3044 + strlen (numbuf
) + 2;
3045 newname
= (char*)alloca (newname_len
);
3046 snprintf (newname
, newname_len
,
3047 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3048 return get_identifier (newname
);
3051 /* Return true if T is a primary function, class or alias template
3055 primary_template_instantiation_p (const_tree t
)
3060 if (TREE_CODE (t
) == FUNCTION_DECL
)
3061 return DECL_LANG_SPECIFIC (t
)
3062 && DECL_TEMPLATE_INSTANTIATION (t
)
3063 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
3064 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3065 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
3066 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
3067 else if (alias_template_specialization_p (t
))
3072 /* Return true if PARM is a template template parameter. */
3075 template_template_parameter_p (const_tree parm
)
3077 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3080 /* Return true iff PARM is a DECL representing a type template
3084 template_type_parameter_p (const_tree parm
)
3087 && (TREE_CODE (parm
) == TYPE_DECL
3088 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3089 && DECL_TEMPLATE_PARM_P (parm
));
3092 /* Return the template parameters of T if T is a
3093 primary template instantiation, NULL otherwise. */
3096 get_primary_template_innermost_parameters (const_tree t
)
3098 tree parms
= NULL
, template_info
= NULL
;
3100 if ((template_info
= get_template_info (t
))
3101 && primary_template_instantiation_p (t
))
3102 parms
= INNERMOST_TEMPLATE_PARMS
3103 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3108 /* Return the template parameters of the LEVELth level from the full list
3109 of template parameters PARMS. */
3112 get_template_parms_at_level (tree parms
, int level
)
3116 || TREE_CODE (parms
) != TREE_LIST
3117 || level
> TMPL_PARMS_DEPTH (parms
))
3120 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3121 if (TMPL_PARMS_DEPTH (p
) == level
)
3127 /* Returns the template arguments of T if T is a template instantiation,
3131 get_template_innermost_arguments (const_tree t
)
3133 tree args
= NULL
, template_info
= NULL
;
3135 if ((template_info
= get_template_info (t
))
3136 && TI_ARGS (template_info
))
3137 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3142 /* Return the argument pack elements of T if T is a template argument pack,
3146 get_template_argument_pack_elems (const_tree t
)
3148 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3149 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3152 return ARGUMENT_PACK_ARGS (t
);
3155 /* Structure used to track the progress of find_parameter_packs_r. */
3156 struct find_parameter_pack_data
3158 /* TREE_LIST that will contain all of the parameter packs found by
3160 tree
* parameter_packs
;
3162 /* Set of AST nodes that have been visited by the traversal. */
3163 hash_set
<tree
> *visited
;
3166 /* Identifies all of the argument packs that occur in a template
3167 argument and appends them to the TREE_LIST inside DATA, which is a
3168 find_parameter_pack_data structure. This is a subroutine of
3169 make_pack_expansion and uses_parameter_packs. */
3171 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3174 struct find_parameter_pack_data
* ppd
=
3175 (struct find_parameter_pack_data
*)data
;
3176 bool parameter_pack_p
= false;
3178 /* Handle type aliases/typedefs. */
3179 if (TYPE_ALIAS_P (t
))
3181 if (TYPE_TEMPLATE_INFO (t
))
3182 cp_walk_tree (&TYPE_TI_ARGS (t
),
3183 &find_parameter_packs_r
,
3189 /* Identify whether this is a parameter pack or not. */
3190 switch (TREE_CODE (t
))
3192 case TEMPLATE_PARM_INDEX
:
3193 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3194 parameter_pack_p
= true;
3197 case TEMPLATE_TYPE_PARM
:
3198 t
= TYPE_MAIN_VARIANT (t
);
3199 case TEMPLATE_TEMPLATE_PARM
:
3200 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3201 parameter_pack_p
= true;
3206 if (DECL_PACK_P (t
))
3208 /* We don't want to walk into the type of a PARM_DECL,
3209 because we don't want to see the type parameter pack. */
3211 parameter_pack_p
= true;
3215 /* Look through a lambda capture proxy to the field pack. */
3217 if (DECL_HAS_VALUE_EXPR_P (t
))
3219 tree v
= DECL_VALUE_EXPR (t
);
3221 &find_parameter_packs_r
,
3225 else if (variable_template_specialization_p (t
))
3227 cp_walk_tree (&DECL_TI_ARGS (t
),
3228 find_parameter_packs_r
,
3235 parameter_pack_p
= true;
3238 /* Not a parameter pack. */
3242 if (parameter_pack_p
)
3244 /* Add this parameter pack to the list. */
3245 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3249 cp_walk_tree (&TYPE_CONTEXT (t
),
3250 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3252 /* This switch statement will return immediately if we don't find a
3254 switch (TREE_CODE (t
))
3256 case TEMPLATE_PARM_INDEX
:
3259 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3260 /* Check the template itself. */
3261 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3262 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3263 /* Check the template arguments. */
3264 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3269 case TEMPLATE_TYPE_PARM
:
3270 case TEMPLATE_TEMPLATE_PARM
:
3277 if (TYPE_PTRMEMFUNC_P (t
))
3283 if (TYPE_TEMPLATE_INFO (t
))
3284 cp_walk_tree (&TYPE_TI_ARGS (t
),
3285 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3292 cp_walk_tree (&TREE_TYPE (t
),
3293 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3297 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3302 case TYPE_PACK_EXPANSION
:
3303 case EXPR_PACK_EXPANSION
:
3308 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3313 case IDENTIFIER_NODE
:
3314 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3326 /* Determines if the expression or type T uses any parameter packs. */
3328 uses_parameter_packs (tree t
)
3330 tree parameter_packs
= NULL_TREE
;
3331 struct find_parameter_pack_data ppd
;
3332 ppd
.parameter_packs
= ¶meter_packs
;
3333 ppd
.visited
= new hash_set
<tree
>;
3334 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3336 return parameter_packs
!= NULL_TREE
;
3339 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3340 representation a base-class initializer into a parameter pack
3341 expansion. If all goes well, the resulting node will be an
3342 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3345 make_pack_expansion (tree arg
)
3348 tree parameter_packs
= NULL_TREE
;
3349 bool for_types
= false;
3350 struct find_parameter_pack_data ppd
;
3352 if (!arg
|| arg
== error_mark_node
)
3355 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
3357 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3358 class initializer. In this case, the TREE_PURPOSE will be a
3359 _TYPE node (representing the base class expansion we're
3360 initializing) and the TREE_VALUE will be a TREE_LIST
3361 containing the initialization arguments.
3363 The resulting expansion looks somewhat different from most
3364 expansions. Rather than returning just one _EXPANSION, we
3365 return a TREE_LIST whose TREE_PURPOSE is a
3366 TYPE_PACK_EXPANSION containing the bases that will be
3367 initialized. The TREE_VALUE will be identical to the
3368 original TREE_VALUE, which is a list of arguments that will
3369 be passed to each base. We do not introduce any new pack
3370 expansion nodes into the TREE_VALUE (although it is possible
3371 that some already exist), because the TREE_PURPOSE and
3372 TREE_VALUE all need to be expanded together with the same
3373 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3374 resulting TREE_PURPOSE will mention the parameter packs in
3375 both the bases and the arguments to the bases. */
3378 tree parameter_packs
= NULL_TREE
;
3380 /* Determine which parameter packs will be used by the base
3382 ppd
.visited
= new hash_set
<tree
>;
3383 ppd
.parameter_packs
= ¶meter_packs
;
3384 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3387 if (parameter_packs
== NULL_TREE
)
3389 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3391 return error_mark_node
;
3394 if (TREE_VALUE (arg
) != void_type_node
)
3396 /* Collect the sets of parameter packs used in each of the
3397 initialization arguments. */
3398 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3400 /* Determine which parameter packs will be expanded in this
3402 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3409 /* Create the pack expansion type for the base type. */
3410 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3411 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3412 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3414 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3415 they will rarely be compared to anything. */
3416 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3418 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3421 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3424 /* Build the PACK_EXPANSION_* node. */
3426 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3427 : make_node (EXPR_PACK_EXPANSION
);
3428 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3429 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3431 /* Propagate type and const-expression information. */
3432 TREE_TYPE (result
) = TREE_TYPE (arg
);
3433 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3436 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3437 they will rarely be compared to anything. */
3438 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3440 /* Determine which parameter packs will be expanded. */
3441 ppd
.parameter_packs
= ¶meter_packs
;
3442 ppd
.visited
= new hash_set
<tree
>;
3443 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3446 /* Make sure we found some parameter packs. */
3447 if (parameter_packs
== NULL_TREE
)
3450 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3452 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3453 return error_mark_node
;
3455 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3457 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3462 /* Checks T for any "bare" parameter packs, which have not yet been
3463 expanded, and issues an error if any are found. This operation can
3464 only be done on full expressions or types (e.g., an expression
3465 statement, "if" condition, etc.), because we could have expressions like:
3467 foo(f(g(h(args)))...)
3469 where "args" is a parameter pack. check_for_bare_parameter_packs
3470 should not be called for the subexpressions args, h(args),
3471 g(h(args)), or f(g(h(args))), because we would produce erroneous
3474 Returns TRUE and emits an error if there were bare parameter packs,
3475 returns FALSE otherwise. */
3477 check_for_bare_parameter_packs (tree t
)
3479 tree parameter_packs
= NULL_TREE
;
3480 struct find_parameter_pack_data ppd
;
3482 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3485 if (TREE_CODE (t
) == TYPE_DECL
)
3488 ppd
.parameter_packs
= ¶meter_packs
;
3489 ppd
.visited
= new hash_set
<tree
>;
3490 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3493 if (parameter_packs
)
3495 error ("parameter packs not expanded with %<...%>:");
3496 while (parameter_packs
)
3498 tree pack
= TREE_VALUE (parameter_packs
);
3499 tree name
= NULL_TREE
;
3501 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3502 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3503 name
= TYPE_NAME (pack
);
3504 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3505 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3507 name
= DECL_NAME (pack
);
3510 inform (input_location
, " %qD", name
);
3512 inform (input_location
, " <anonymous>");
3514 parameter_packs
= TREE_CHAIN (parameter_packs
);
3523 /* Expand any parameter packs that occur in the template arguments in
3526 expand_template_argument_pack (tree args
)
3528 tree result_args
= NULL_TREE
;
3529 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3530 int num_result_args
= -1;
3531 int non_default_args_count
= -1;
3533 /* First, determine if we need to expand anything, and the number of
3534 slots we'll need. */
3535 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3537 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3538 if (arg
== NULL_TREE
)
3540 if (ARGUMENT_PACK_P (arg
))
3542 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3543 if (num_result_args
< 0)
3544 num_result_args
= in_arg
+ num_packed
;
3546 num_result_args
+= num_packed
;
3550 if (num_result_args
>= 0)
3555 /* If no expansion is necessary, we're done. */
3556 if (num_result_args
< 0)
3559 /* Expand arguments. */
3560 result_args
= make_tree_vec (num_result_args
);
3561 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3562 non_default_args_count
=
3563 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3564 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3566 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3567 if (ARGUMENT_PACK_P (arg
))
3569 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3570 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3571 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3572 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3573 if (non_default_args_count
> 0)
3574 non_default_args_count
+= num_packed
- 1;
3578 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3582 if (non_default_args_count
>= 0)
3583 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3587 /* Checks if DECL shadows a template parameter.
3589 [temp.local]: A template-parameter shall not be redeclared within its
3590 scope (including nested scopes).
3592 Emits an error and returns TRUE if the DECL shadows a parameter,
3593 returns FALSE otherwise. */
3596 check_template_shadow (tree decl
)
3600 /* If we're not in a template, we can't possibly shadow a template
3602 if (!current_template_parms
)
3605 /* Figure out what we're shadowing. */
3606 if (TREE_CODE (decl
) == OVERLOAD
)
3607 decl
= OVL_CURRENT (decl
);
3608 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3610 /* If there's no previous binding for this name, we're not shadowing
3611 anything, let alone a template parameter. */
3615 /* If we're not shadowing a template parameter, we're done. Note
3616 that OLDDECL might be an OVERLOAD (or perhaps even an
3617 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3619 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3622 /* We check for decl != olddecl to avoid bogus errors for using a
3623 name inside a class. We check TPFI to avoid duplicate errors for
3624 inline member templates. */
3626 || (DECL_TEMPLATE_PARM_P (decl
)
3627 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
3630 /* Don't complain about the injected class name, as we've already
3631 complained about the class itself. */
3632 if (DECL_SELF_REFERENCE_P (decl
))
3635 error ("declaration of %q+#D", decl
);
3636 error (" shadows template parm %q+#D", olddecl
);
3640 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3641 ORIG_LEVEL, DECL, and TYPE. */
3644 build_template_parm_index (int index
,
3650 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3651 TEMPLATE_PARM_IDX (t
) = index
;
3652 TEMPLATE_PARM_LEVEL (t
) = level
;
3653 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3654 TEMPLATE_PARM_DECL (t
) = decl
;
3655 TREE_TYPE (t
) = type
;
3656 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3657 TREE_READONLY (t
) = TREE_READONLY (decl
);
3662 /* Find the canonical type parameter for the given template type
3663 parameter. Returns the canonical type parameter, which may be TYPE
3664 if no such parameter existed. */
3667 canonical_type_parameter (tree type
)
3670 int idx
= TEMPLATE_TYPE_IDX (type
);
3671 if (!canonical_template_parms
)
3672 vec_alloc (canonical_template_parms
, idx
+1);
3674 while (canonical_template_parms
->length () <= (unsigned)idx
)
3675 vec_safe_push (canonical_template_parms
, NULL_TREE
);
3677 list
= (*canonical_template_parms
)[idx
];
3678 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3679 list
= TREE_CHAIN (list
);
3682 return TREE_VALUE (list
);
3685 (*canonical_template_parms
)[idx
]
3686 = tree_cons (NULL_TREE
, type
,
3687 (*canonical_template_parms
)[idx
]);
3692 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3693 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3694 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3695 new one is created. */
3698 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3699 tsubst_flags_t complain
)
3701 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3702 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3703 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3704 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3706 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3709 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3710 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3711 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3712 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3713 DECL_ARTIFICIAL (decl
) = 1;
3714 SET_DECL_TEMPLATE_PARM_P (decl
);
3716 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3717 TEMPLATE_PARM_LEVEL (index
) - levels
,
3718 TEMPLATE_PARM_ORIG_LEVEL (index
),
3720 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3721 TEMPLATE_PARM_PARAMETER_PACK (t
)
3722 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3724 /* Template template parameters need this. */
3725 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3726 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3727 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3731 return TEMPLATE_PARM_DESCENDANTS (index
);
3734 /* Process information from new template parameter PARM and append it
3735 to the LIST being built. This new parameter is a non-type
3736 parameter iff IS_NON_TYPE is true. This new parameter is a
3737 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3741 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
3742 bool is_non_type
, bool is_parameter_pack
)
3748 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3749 defval
= TREE_PURPOSE (parm
);
3753 tree p
= tree_last (list
);
3755 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3758 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3759 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3761 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3769 parm
= TREE_VALUE (parm
);
3771 SET_DECL_TEMPLATE_PARM_P (parm
);
3773 if (TREE_TYPE (parm
) != error_mark_node
)
3777 The top-level cv-qualifiers on the template-parameter are
3778 ignored when determining its type. */
3779 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3780 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3781 TREE_TYPE (parm
) = error_mark_node
;
3782 else if (uses_parameter_packs (TREE_TYPE (parm
))
3783 && !is_parameter_pack
3784 /* If we're in a nested template parameter list, the template
3785 template parameter could be a parameter pack. */
3786 && processing_template_parmlist
== 1)
3788 /* This template parameter is not a parameter pack, but it
3789 should be. Complain about "bare" parameter packs. */
3790 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3792 /* Recover by calling this a parameter pack. */
3793 is_parameter_pack
= true;
3797 /* A template parameter is not modifiable. */
3798 TREE_CONSTANT (parm
) = 1;
3799 TREE_READONLY (parm
) = 1;
3800 decl
= build_decl (parm_loc
,
3801 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3802 TREE_CONSTANT (decl
) = 1;
3803 TREE_READONLY (decl
) = 1;
3804 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3805 = build_template_parm_index (idx
, processing_template_decl
,
3806 processing_template_decl
,
3807 decl
, TREE_TYPE (parm
));
3809 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3810 = is_parameter_pack
;
3815 parm
= TREE_VALUE (TREE_VALUE (parm
));
3817 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3819 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3820 /* This is for distinguishing between real templates and template
3821 template parameters */
3822 TREE_TYPE (parm
) = t
;
3823 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3828 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3829 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3830 decl
= build_decl (parm_loc
,
3831 TYPE_DECL
, parm
, t
);
3834 TYPE_NAME (t
) = decl
;
3835 TYPE_STUB_DECL (t
) = decl
;
3837 TEMPLATE_TYPE_PARM_INDEX (t
)
3838 = build_template_parm_index (idx
, processing_template_decl
,
3839 processing_template_decl
,
3840 decl
, TREE_TYPE (parm
));
3841 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3842 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3844 DECL_ARTIFICIAL (decl
) = 1;
3845 SET_DECL_TEMPLATE_PARM_P (decl
);
3847 parm
= build_tree_list (defval
, parm
);
3848 return chainon (list
, parm
);
3851 /* The end of a template parameter list has been reached. Process the
3852 tree list into a parameter vector, converting each parameter into a more
3853 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3857 end_template_parm_list (tree parms
)
3861 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3863 current_template_parms
3864 = tree_cons (size_int (processing_template_decl
),
3865 saved_parmlist
, current_template_parms
);
3867 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3869 next
= TREE_CHAIN (parm
);
3870 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3871 TREE_CHAIN (parm
) = NULL_TREE
;
3874 --processing_template_parmlist
;
3876 return saved_parmlist
;
3879 /* end_template_decl is called after a template declaration is seen. */
3882 end_template_decl (void)
3884 reset_specialization ();
3886 if (! processing_template_decl
)
3889 /* This matches the pushlevel in begin_template_parm_list. */
3892 --processing_template_decl
;
3893 current_template_parms
= TREE_CHAIN (current_template_parms
);
3896 /* Takes a TREE_LIST representing a template parameter and convert it
3897 into an argument suitable to be passed to the type substitution
3898 functions. Note that If the TREE_LIST contains an error_mark
3899 node, the returned argument is error_mark_node. */
3902 template_parm_to_arg (tree t
)
3906 || TREE_CODE (t
) != TREE_LIST
)
3909 if (error_operand_p (TREE_VALUE (t
)))
3910 return error_mark_node
;
3914 if (TREE_CODE (t
) == TYPE_DECL
3915 || TREE_CODE (t
) == TEMPLATE_DECL
)
3919 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3921 /* Turn this argument into a TYPE_ARGUMENT_PACK
3922 with a single element, which expands T. */
3923 tree vec
= make_tree_vec (1);
3924 #ifdef ENABLE_CHECKING
3925 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3926 (vec
, TREE_VEC_LENGTH (vec
));
3928 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3930 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
3931 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3936 t
= DECL_INITIAL (t
);
3938 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3940 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3941 with a single element, which expands T. */
3942 tree vec
= make_tree_vec (1);
3943 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3944 #ifdef ENABLE_CHECKING
3945 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3946 (vec
, TREE_VEC_LENGTH (vec
));
3948 t
= convert_from_reference (t
);
3949 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3951 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3952 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3953 TREE_TYPE (t
) = type
;
3956 t
= convert_from_reference (t
);
3961 /* Given a set of template parameters, return them as a set of template
3962 arguments. The template parameters are represented as a TREE_VEC, in
3963 the form documented in cp-tree.h for template arguments. */
3966 template_parms_to_args (tree parms
)
3969 tree args
= NULL_TREE
;
3970 int length
= TMPL_PARMS_DEPTH (parms
);
3973 /* If there is only one level of template parameters, we do not
3974 create a TREE_VEC of TREE_VECs. Instead, we return a single
3975 TREE_VEC containing the arguments. */
3977 args
= make_tree_vec (length
);
3979 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
3981 tree a
= copy_node (TREE_VALUE (header
));
3984 TREE_TYPE (a
) = NULL_TREE
;
3985 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
3986 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
3988 #ifdef ENABLE_CHECKING
3989 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
3993 TREE_VEC_ELT (args
, --l
) = a
;
3998 if (length
> 1 && TREE_VEC_ELT (args
, 0) == NULL_TREE
)
3999 /* This can happen for template parms of a template template
4002 template<template<class T, class U> class TT> struct S;
4004 Consider the level of the parms of TT; T and U both have
4005 level 2; TT has no template parm of level 1. So in this case
4006 the first element of full_template_args is NULL_TREE. If we
4007 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
4008 of 2. This will make tsubst wrongly consider that T and U
4009 have level 1. Instead, let's create a dummy vector as the
4010 first element of full_template_args so that TMPL_ARGS_DEPTH
4011 returns the correct depth for args. */
4012 TREE_VEC_ELT (args
, 0) = make_tree_vec (1);
4016 /* Within the declaration of a template, return the currently active
4017 template parameters as an argument TREE_VEC. */
4020 current_template_args (void)
4022 return template_parms_to_args (current_template_parms
);
4025 /* Update the declared TYPE by doing any lookups which were thought to be
4026 dependent, but are not now that we know the SCOPE of the declarator. */
4029 maybe_update_decl_type (tree orig_type
, tree scope
)
4031 tree type
= orig_type
;
4033 if (type
== NULL_TREE
)
4036 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4037 type
= TREE_TYPE (type
);
4039 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4040 && dependent_type_p (type
)
4041 /* Don't bother building up the args in this case. */
4042 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4044 /* tsubst in the args corresponding to the template parameters,
4045 including auto if present. Most things will be unchanged, but
4046 make_typename_type and tsubst_qualified_id will resolve
4047 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4048 tree args
= current_template_args ();
4049 tree auto_node
= type_uses_auto (type
);
4053 tree auto_vec
= make_tree_vec (1);
4054 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4055 args
= add_to_template_args (args
, auto_vec
);
4057 pushed
= push_scope (scope
);
4058 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4063 if (type
== error_mark_node
)
4066 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4068 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4071 type
= TYPE_NAME (type
);
4076 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4077 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
4078 a member template. Used by push_template_decl below. */
4081 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4083 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4084 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4085 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4086 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4087 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4092 struct template_parm_data
4094 /* The level of the template parameters we are currently
4098 /* The index of the specialization argument we are currently
4102 /* An array whose size is the number of template parameters. The
4103 elements are nonzero if the parameter has been used in any one
4104 of the arguments processed so far. */
4107 /* An array whose size is the number of template arguments. The
4108 elements are nonzero if the argument makes use of template
4109 parameters of this level. */
4110 int* arg_uses_template_parms
;
4113 /* Subroutine of push_template_decl used to see if each template
4114 parameter in a partial specialization is used in the explicit
4115 argument list. If T is of the LEVEL given in DATA (which is
4116 treated as a template_parm_data*), then DATA->PARMS is marked
4120 mark_template_parm (tree t
, void* data
)
4124 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4126 template_parm_level_and_index (t
, &level
, &idx
);
4128 if (level
== tpd
->level
)
4130 tpd
->parms
[idx
] = 1;
4131 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4134 /* Return zero so that for_each_template_parm will continue the
4135 traversal of the tree; we want to mark *every* template parm. */
4139 /* Process the partial specialization DECL. */
4142 process_partial_specialization (tree decl
)
4144 tree type
= TREE_TYPE (decl
);
4145 tree tinfo
= get_template_info (decl
);
4146 tree maintmpl
= TI_TEMPLATE (tinfo
);
4147 tree specargs
= TI_ARGS (tinfo
);
4148 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4149 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4152 int nargs
= TREE_VEC_LENGTH (inner_args
);
4155 bool did_error_intro
= false;
4156 struct template_parm_data tpd
;
4157 struct template_parm_data tpd2
;
4159 gcc_assert (current_template_parms
);
4161 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4162 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4164 /* We check that each of the template parameters given in the
4165 partial specialization is used in the argument list to the
4166 specialization. For example:
4168 template <class T> struct S;
4169 template <class T> struct S<T*>;
4171 The second declaration is OK because `T*' uses the template
4172 parameter T, whereas
4174 template <class T> struct S<int>;
4176 is no good. Even trickier is:
4187 The S2<T> declaration is actually invalid; it is a
4188 full-specialization. Of course,
4191 struct S2<T (*)(U)>;
4193 or some such would have been OK. */
4194 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4195 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4196 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4198 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4199 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4200 for (i
= 0; i
< nargs
; ++i
)
4202 tpd
.current_arg
= i
;
4203 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4204 &mark_template_parm
,
4207 /*include_nondeduced_p=*/false);
4209 for (i
= 0; i
< ntparms
; ++i
)
4210 if (tpd
.parms
[i
] == 0)
4212 /* One of the template parms was not used in a deduced context in the
4214 if (!did_error_intro
)
4216 error ("template parameters not deducible in "
4217 "partial specialization:");
4218 did_error_intro
= true;
4221 inform (input_location
, " %qD",
4222 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4225 if (did_error_intro
)
4226 return error_mark_node
;
4228 /* [temp.class.spec]
4230 The argument list of the specialization shall not be identical to
4231 the implicit argument list of the primary template. */
4233 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
4234 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
)))
4235 error ("partial specialization %qD does not specialize "
4236 "any template arguments", decl
);
4238 /* A partial specialization that replaces multiple parameters of the
4239 primary template with a pack expansion is less specialized for those
4241 if (nargs
< DECL_NTPARMS (maintmpl
))
4243 error ("partial specialization is not more specialized than the "
4244 "primary template because it replaces multiple parameters "
4245 "with a pack expansion");
4246 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4250 /* [temp.class.spec]
4252 A partially specialized non-type argument expression shall not
4253 involve template parameters of the partial specialization except
4254 when the argument expression is a simple identifier.
4256 The type of a template parameter corresponding to a specialized
4257 non-type argument shall not be dependent on a parameter of the
4260 Also, we verify that pack expansions only occur at the
4261 end of the argument list. */
4262 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4264 for (i
= 0; i
< nargs
; ++i
)
4266 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4267 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4268 tree packed_args
= NULL_TREE
;
4271 if (ARGUMENT_PACK_P (arg
))
4273 /* Extract the arguments from the argument pack. We'll be
4274 iterating over these in the following loop. */
4275 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4276 len
= TREE_VEC_LENGTH (packed_args
);
4279 for (j
= 0; j
< len
; j
++)
4282 /* Get the Jth argument in the parameter pack. */
4283 arg
= TREE_VEC_ELT (packed_args
, j
);
4285 if (PACK_EXPANSION_P (arg
))
4287 /* Pack expansions must come at the end of the
4289 if ((packed_args
&& j
< len
- 1)
4290 || (!packed_args
&& i
< nargs
- 1))
4292 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4293 error ("parameter pack argument %qE must be at the "
4294 "end of the template argument list", arg
);
4296 error ("parameter pack argument %qT must be at the "
4297 "end of the template argument list", arg
);
4301 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4302 /* We only care about the pattern. */
4303 arg
= PACK_EXPANSION_PATTERN (arg
);
4305 if (/* These first two lines are the `non-type' bit. */
4307 && TREE_CODE (arg
) != TEMPLATE_DECL
4308 /* This next two lines are the `argument expression is not just a
4309 simple identifier' condition and also the `specialized
4310 non-type argument' bit. */
4311 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4312 && !(REFERENCE_REF_P (arg
)
4313 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4315 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4316 || (packed_args
&& uses_template_parms (arg
)))
4317 error ("template argument %qE involves template parameter(s)",
4321 /* Look at the corresponding template parameter,
4322 marking which template parameters its type depends
4324 tree type
= TREE_TYPE (parm
);
4328 /* We haven't yet initialized TPD2. Do so now. */
4329 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4330 /* The number of parameters here is the number in the
4331 main template, which, as checked in the assertion
4333 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4335 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4338 /* Mark the template parameters. But this time, we're
4339 looking for the template parameters of the main
4340 template, not in the specialization. */
4341 tpd2
.current_arg
= i
;
4342 tpd2
.arg_uses_template_parms
[i
] = 0;
4343 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4344 for_each_template_parm (type
,
4345 &mark_template_parm
,
4348 /*include_nondeduced_p=*/false);
4350 if (tpd2
.arg_uses_template_parms
[i
])
4352 /* The type depended on some template parameters.
4353 If they are fully specialized in the
4354 specialization, that's OK. */
4357 for (j
= 0; j
< nargs
; ++j
)
4358 if (tpd2
.parms
[j
] != 0
4359 && tpd
.arg_uses_template_parms
[j
])
4362 error_n (input_location
, count
,
4363 "type %qT of template argument %qE depends "
4364 "on a template parameter",
4365 "type %qT of template argument %qE depends "
4366 "on template parameters",
4375 /* We should only get here once. */
4376 if (TREE_CODE (decl
) == TYPE_DECL
)
4377 gcc_assert (!COMPLETE_TYPE_P (type
));
4379 tree tmpl
= build_template_decl (decl
, current_template_parms
,
4380 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4381 TREE_TYPE (tmpl
) = type
;
4382 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4383 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4384 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4385 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4387 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4388 = tree_cons (specargs
, tmpl
,
4389 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4390 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4392 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4393 inst
= TREE_CHAIN (inst
))
4395 tree instance
= TREE_VALUE (inst
);
4396 if (TYPE_P (instance
)
4397 ? (COMPLETE_TYPE_P (instance
)
4398 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
4399 : DECL_TEMPLATE_INSTANTIATION (instance
))
4401 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
4402 if (spec
&& TREE_VALUE (spec
) == tmpl
)
4404 tree inst_decl
= (DECL_P (instance
)
4405 ? instance
: TYPE_NAME (instance
));
4406 permerror (input_location
,
4407 "partial specialization of %qD after instantiation "
4408 "of %qD", decl
, inst_decl
);
4416 /* PARM is a template parameter of some form; return the corresponding
4417 TEMPLATE_PARM_INDEX. */
4420 get_template_parm_index (tree parm
)
4422 if (TREE_CODE (parm
) == PARM_DECL
4423 || TREE_CODE (parm
) == CONST_DECL
)
4424 parm
= DECL_INITIAL (parm
);
4425 else if (TREE_CODE (parm
) == TYPE_DECL
4426 || TREE_CODE (parm
) == TEMPLATE_DECL
)
4427 parm
= TREE_TYPE (parm
);
4428 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
4429 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
4430 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
4431 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
4432 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
4436 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4437 parameter packs used by the template parameter PARM. */
4440 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
4442 /* A type parm can't refer to another parm. */
4443 if (TREE_CODE (parm
) == TYPE_DECL
)
4445 else if (TREE_CODE (parm
) == PARM_DECL
)
4447 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
4452 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
4454 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
4455 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
4456 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
4459 /* PARM is a template parameter pack. Return any parameter packs used in
4460 its type or the type of any of its template parameters. If there are
4461 any such packs, it will be instantiated into a fixed template parameter
4462 list by partial instantiation rather than be fully deduced. */
4465 fixed_parameter_pack_p (tree parm
)
4467 /* This can only be true in a member template. */
4468 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
4470 /* This can only be true for a parameter pack. */
4471 if (!template_parameter_pack_p (parm
))
4473 /* A type parm can't refer to another parm. */
4474 if (TREE_CODE (parm
) == TYPE_DECL
)
4477 tree parameter_packs
= NULL_TREE
;
4478 struct find_parameter_pack_data ppd
;
4479 ppd
.parameter_packs
= ¶meter_packs
;
4480 ppd
.visited
= new hash_set
<tree
>;
4482 fixed_parameter_pack_p_1 (parm
, &ppd
);
4485 return parameter_packs
;
4488 /* Check that a template declaration's use of default arguments and
4489 parameter packs is not invalid. Here, PARMS are the template
4490 parameters. IS_PRIMARY is true if DECL is the thing declared by
4491 a primary template. IS_PARTIAL is true if DECL is a partial
4494 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4495 declaration (but not a definition); 1 indicates a declaration, 2
4496 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4497 emitted for extraneous default arguments.
4499 Returns TRUE if there were no errors found, FALSE otherwise. */
4502 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4503 bool is_partial
, int is_friend_decl
)
4506 int last_level_to_check
;
4508 bool no_errors
= true;
4512 A default template-argument shall not be specified in a
4513 function template declaration or a function template definition, nor
4514 in the template-parameter-list of the definition of a member of a
4517 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
4518 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
4519 /* You can't have a function template declaration in a local
4520 scope, nor you can you define a member of a class template in a
4524 if ((TREE_CODE (decl
) == TYPE_DECL
4526 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4527 || (TREE_CODE (decl
) == FUNCTION_DECL
4528 && LAMBDA_FUNCTION_P (decl
)))
4529 /* A lambda doesn't have an explicit declaration; don't complain
4530 about the parms of the enclosing class. */
4533 if (current_class_type
4534 && !TYPE_BEING_DEFINED (current_class_type
)
4535 && DECL_LANG_SPECIFIC (decl
)
4536 && DECL_DECLARES_FUNCTION_P (decl
)
4537 /* If this is either a friend defined in the scope of the class
4538 or a member function. */
4539 && (DECL_FUNCTION_MEMBER_P (decl
)
4540 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4541 : DECL_FRIEND_CONTEXT (decl
)
4542 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4544 /* And, if it was a member function, it really was defined in
4545 the scope of the class. */
4546 && (!DECL_FUNCTION_MEMBER_P (decl
)
4547 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4548 /* We already checked these parameters when the template was
4549 declared, so there's no need to do it again now. This function
4550 was defined in class scope, but we're processing its body now
4551 that the class is complete. */
4554 /* Core issue 226 (C++0x only): the following only applies to class
4557 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
4561 If a template-parameter has a default template-argument, all
4562 subsequent template-parameters shall have a default
4563 template-argument supplied. */
4564 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4566 tree inner_parms
= TREE_VALUE (parm_level
);
4567 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4568 int seen_def_arg_p
= 0;
4571 for (i
= 0; i
< ntparms
; ++i
)
4573 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4575 if (parm
== error_mark_node
)
4578 if (TREE_PURPOSE (parm
))
4580 else if (seen_def_arg_p
4581 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4583 error ("no default argument for %qD", TREE_VALUE (parm
));
4584 /* For better subsequent error-recovery, we indicate that
4585 there should have been a default argument. */
4586 TREE_PURPOSE (parm
) = error_mark_node
;
4589 else if (!is_partial
4591 /* Don't complain about an enclosing partial
4593 && parm_level
== parms
4594 && TREE_CODE (decl
) == TYPE_DECL
4596 && template_parameter_pack_p (TREE_VALUE (parm
))
4597 /* A fixed parameter pack will be partially
4598 instantiated into a fixed length list. */
4599 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
4601 /* A primary class template can only have one
4602 parameter pack, at the end of the template
4605 error ("parameter pack %q+D must be at the end of the"
4606 " template parameter list", TREE_VALUE (parm
));
4608 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4616 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4620 /* For an ordinary class template, default template arguments are
4621 allowed at the innermost level, e.g.:
4622 template <class T = int>
4624 but, in a partial specialization, they're not allowed even
4625 there, as we have in [temp.class.spec]:
4627 The template parameter list of a specialization shall not
4628 contain default template argument values.
4630 So, for a partial specialization, or for a function template
4631 (in C++98/C++03), we look at all of them. */
4634 /* But, for a primary class template that is not a partial
4635 specialization we look at all template parameters except the
4637 parms
= TREE_CHAIN (parms
);
4639 /* Figure out what error message to issue. */
4640 if (is_friend_decl
== 2)
4641 msg
= G_("default template arguments may not be used in function template "
4642 "friend re-declaration");
4643 else if (is_friend_decl
)
4644 msg
= G_("default template arguments may not be used in function template "
4645 "friend declarations");
4646 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4647 msg
= G_("default template arguments may not be used in function templates "
4648 "without -std=c++11 or -std=gnu++11");
4649 else if (is_partial
)
4650 msg
= G_("default template arguments may not be used in "
4651 "partial specializations");
4653 msg
= G_("default argument for template parameter for class enclosing %qD");
4655 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4656 /* If we're inside a class definition, there's no need to
4657 examine the parameters to the class itself. On the one
4658 hand, they will be checked when the class is defined, and,
4659 on the other, default arguments are valid in things like:
4660 template <class T = double>
4661 struct S { template <class U> void f(U); };
4662 Here the default argument for `S' has no bearing on the
4663 declaration of `f'. */
4664 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4666 /* Check everything. */
4667 last_level_to_check
= 0;
4669 for (parm_level
= parms
;
4670 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4671 parm_level
= TREE_CHAIN (parm_level
))
4673 tree inner_parms
= TREE_VALUE (parm_level
);
4677 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4678 for (i
= 0; i
< ntparms
; ++i
)
4680 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4683 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4688 if (is_friend_decl
== 2)
4695 /* Clear out the default argument so that we are not
4697 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4701 /* At this point, if we're still interested in issuing messages,
4702 they must apply to classes surrounding the object declared. */
4704 msg
= G_("default argument for template parameter for class "
4711 /* Worker for push_template_decl_real, called via
4712 for_each_template_parm. DATA is really an int, indicating the
4713 level of the parameters we are interested in. If T is a template
4714 parameter of that level, return nonzero. */
4717 template_parm_this_level_p (tree t
, void* data
)
4719 int this_level
= *(int *)data
;
4722 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4723 level
= TEMPLATE_PARM_LEVEL (t
);
4725 level
= TEMPLATE_TYPE_LEVEL (t
);
4726 return level
== this_level
;
4729 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4730 parameters given by current_template_args, or reuses a
4731 previously existing one, if appropriate. Returns the DECL, or an
4732 equivalent one, if it is replaced via a call to duplicate_decls.
4734 If IS_FRIEND is true, DECL is a friend declaration. */
4737 push_template_decl_real (tree decl
, bool is_friend
)
4745 int new_template_p
= 0;
4746 /* True if the template is a member template, in the sense of
4748 bool member_template_p
= false;
4750 if (decl
== error_mark_node
|| !current_template_parms
)
4751 return error_mark_node
;
4753 /* See if this is a partial specialization. */
4754 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
4755 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4756 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
4757 || (TREE_CODE (decl
) == VAR_DECL
4758 && DECL_LANG_SPECIFIC (decl
)
4759 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4760 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
4762 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4766 /* For a friend, we want the context of the friend function, not
4767 the type of which it is a friend. */
4768 ctx
= CP_DECL_CONTEXT (decl
);
4769 else if (CP_DECL_CONTEXT (decl
)
4770 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4771 /* In the case of a virtual function, we want the class in which
4773 ctx
= CP_DECL_CONTEXT (decl
);
4775 /* Otherwise, if we're currently defining some class, the DECL
4776 is assumed to be a member of the class. */
4777 ctx
= current_scope ();
4779 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4782 if (!DECL_CONTEXT (decl
))
4783 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4785 /* See if this is a primary template. */
4786 if (is_friend
&& ctx
4787 && uses_template_parms_level (ctx
, processing_template_decl
))
4788 /* A friend template that specifies a class context, i.e.
4789 template <typename T> friend void A<T>::f();
4792 else if (TREE_CODE (decl
) == TYPE_DECL
4793 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4796 is_primary
= template_parm_scope_p ();
4800 if (DECL_CLASS_SCOPE_P (decl
))
4801 member_template_p
= true;
4802 if (TREE_CODE (decl
) == TYPE_DECL
4803 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4805 error ("template class without a name");
4806 return error_mark_node
;
4808 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4810 if (member_template_p
)
4812 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
4813 error ("member template %qD may not have virt-specifiers", decl
);
4815 if (DECL_DESTRUCTOR_P (decl
))
4819 A destructor shall not be a member template. */
4820 error ("destructor %qD declared as member template", decl
);
4821 return error_mark_node
;
4823 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4824 && (!prototype_p (TREE_TYPE (decl
))
4825 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4826 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4827 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4828 == void_list_node
)))
4830 /* [basic.stc.dynamic.allocation]
4832 An allocation function can be a function
4833 template. ... Template allocation functions shall
4834 have two or more parameters. */
4835 error ("invalid template declaration of %qD", decl
);
4836 return error_mark_node
;
4839 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4840 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4842 else if (TREE_CODE (decl
) == TYPE_DECL
4843 && TYPE_DECL_ALIAS_P (decl
))
4844 /* alias-declaration */
4845 gcc_assert (!DECL_ARTIFICIAL (decl
));
4846 else if (VAR_P (decl
))
4847 /* C++14 variable template. */;
4850 error ("template declaration of %q#D", decl
);
4851 return error_mark_node
;
4855 /* Check to see that the rules regarding the use of default
4856 arguments are not being violated. */
4857 check_default_tmpl_args (decl
, current_template_parms
,
4858 is_primary
, is_partial
, /*is_friend_decl=*/0);
4860 /* Ensure that there are no parameter packs in the type of this
4861 declaration that have not been expanded. */
4862 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4864 /* Check each of the arguments individually to see if there are
4865 any bare parameter packs. */
4866 tree type
= TREE_TYPE (decl
);
4867 tree arg
= DECL_ARGUMENTS (decl
);
4868 tree argtype
= TYPE_ARG_TYPES (type
);
4870 while (arg
&& argtype
)
4872 if (!DECL_PACK_P (arg
)
4873 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4875 /* This is a PARM_DECL that contains unexpanded parameter
4876 packs. We have already complained about this in the
4877 check_for_bare_parameter_packs call, so just replace
4878 these types with ERROR_MARK_NODE. */
4879 TREE_TYPE (arg
) = error_mark_node
;
4880 TREE_VALUE (argtype
) = error_mark_node
;
4883 arg
= DECL_CHAIN (arg
);
4884 argtype
= TREE_CHAIN (argtype
);
4887 /* Check for bare parameter packs in the return type and the
4888 exception specifiers. */
4889 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4890 /* Errors were already issued, set return type to int
4891 as the frontend doesn't expect error_mark_node as
4893 TREE_TYPE (type
) = integer_type_node
;
4894 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4895 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4897 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
4898 && TYPE_DECL_ALIAS_P (decl
))
4899 ? DECL_ORIGINAL_TYPE (decl
)
4900 : TREE_TYPE (decl
)))
4902 TREE_TYPE (decl
) = error_mark_node
;
4903 return error_mark_node
;
4907 return process_partial_specialization (decl
);
4909 args
= current_template_args ();
4912 || TREE_CODE (ctx
) == FUNCTION_DECL
4913 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4914 || (TREE_CODE (decl
) == TYPE_DECL
4915 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4916 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4918 if (DECL_LANG_SPECIFIC (decl
)
4919 && DECL_TEMPLATE_INFO (decl
)
4920 && DECL_TI_TEMPLATE (decl
))
4921 tmpl
= DECL_TI_TEMPLATE (decl
);
4922 /* If DECL is a TYPE_DECL for a class-template, then there won't
4923 be DECL_LANG_SPECIFIC. The information equivalent to
4924 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4925 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4926 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4927 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4929 /* Since a template declaration already existed for this
4930 class-type, we must be redeclaring it here. Make sure
4931 that the redeclaration is valid. */
4932 redeclare_class_template (TREE_TYPE (decl
),
4933 current_template_parms
);
4934 /* We don't need to create a new TEMPLATE_DECL; just use the
4935 one we already had. */
4936 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4940 tmpl
= build_template_decl (decl
, current_template_parms
,
4944 if (DECL_LANG_SPECIFIC (decl
)
4945 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4947 /* A specialization of a member template of a template
4949 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4950 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4951 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4957 tree a
, t
, current
, parms
;
4959 tree tinfo
= get_template_info (decl
);
4963 error ("template definition of non-template %q#D", decl
);
4964 return error_mark_node
;
4967 tmpl
= TI_TEMPLATE (tinfo
);
4969 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4970 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4971 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4972 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4976 /* The declaration is a specialization of a member
4977 template, declared outside the class. Therefore, the
4978 innermost template arguments will be NULL, so we
4979 replace them with the arguments determined by the
4980 earlier call to check_explicit_specialization. */
4981 args
= DECL_TI_ARGS (decl
);
4984 = build_template_decl (decl
, current_template_parms
,
4986 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4987 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4988 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4989 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4990 DECL_TEMPLATE_INFO (new_tmpl
)
4991 = build_template_info (tmpl
, args
);
4993 register_specialization (new_tmpl
,
4994 most_general_template (tmpl
),
5000 /* Make sure the template headers we got make sense. */
5002 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5003 i
= TMPL_PARMS_DEPTH (parms
);
5004 if (TMPL_ARGS_DEPTH (args
) != i
)
5006 error ("expected %d levels of template parms for %q#D, got %d",
5007 i
, decl
, TMPL_ARGS_DEPTH (args
));
5008 DECL_INTERFACE_KNOWN (decl
) = 1;
5009 return error_mark_node
;
5012 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5014 a
= TMPL_ARGS_LEVEL (args
, i
);
5015 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5017 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5019 if (current
== decl
)
5020 error ("got %d template parameters for %q#D",
5021 TREE_VEC_LENGTH (a
), decl
);
5023 error ("got %d template parameters for %q#T",
5024 TREE_VEC_LENGTH (a
), current
);
5025 error (" but %d required", TREE_VEC_LENGTH (t
));
5026 /* Avoid crash in import_export_decl. */
5027 DECL_INTERFACE_KNOWN (decl
) = 1;
5028 return error_mark_node
;
5031 if (current
== decl
)
5033 else if (current
== NULL_TREE
)
5034 /* Can happen in erroneous input. */
5037 current
= get_containing_scope (current
);
5040 /* Check that the parms are used in the appropriate qualifying scopes
5041 in the declarator. */
5042 if (!comp_template_args
5044 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5047 template arguments to %qD do not match original template %qD",
5048 decl
, DECL_TEMPLATE_RESULT (tmpl
));
5049 if (!uses_template_parms (TI_ARGS (tinfo
)))
5050 inform (input_location
, "use template<> for an explicit specialization");
5051 /* Avoid crash in import_export_decl. */
5052 DECL_INTERFACE_KNOWN (decl
) = 1;
5053 return error_mark_node
;
5057 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5058 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5060 /* Push template declarations for global functions and types. Note
5061 that we do not try to push a global template friend declared in a
5062 template class; such a thing may well depend on the template
5063 parameters of the class. */
5064 if (new_template_p
&& !ctx
5065 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5067 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5068 if (tmpl
== error_mark_node
)
5069 return error_mark_node
;
5071 /* Hide template friend classes that haven't been declared yet. */
5072 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5074 DECL_ANTICIPATED (tmpl
) = 1;
5075 DECL_FRIEND_P (tmpl
) = 1;
5081 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5084 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5085 if (DECL_CONV_FN_P (tmpl
))
5087 int depth
= TMPL_PARMS_DEPTH (parms
);
5089 /* It is a conversion operator. See if the type converted to
5090 depends on innermost template operands. */
5092 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
5094 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
5097 /* Give template template parms a DECL_CONTEXT of the template
5098 for which they are a parameter. */
5099 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5100 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5102 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5103 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5104 DECL_CONTEXT (parm
) = tmpl
;
5107 if (TREE_CODE (decl
) == TYPE_DECL
5108 && TYPE_DECL_ALIAS_P (decl
)
5109 && complex_alias_template_p (tmpl
))
5110 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5113 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5114 back to its most general template. If TMPL is a specialization,
5115 ARGS may only have the innermost set of arguments. Add the missing
5116 argument levels if necessary. */
5117 if (DECL_TEMPLATE_INFO (tmpl
))
5118 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5120 info
= build_template_info (tmpl
, args
);
5122 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5123 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5126 if (is_primary
&& !DECL_LANG_SPECIFIC (decl
))
5127 retrofit_lang_decl (decl
);
5128 if (DECL_LANG_SPECIFIC (decl
))
5129 DECL_TEMPLATE_INFO (decl
) = info
;
5132 if (flag_implicit_templates
5134 && TREE_PUBLIC (decl
)
5135 && VAR_OR_FUNCTION_DECL_P (decl
))
5136 /* Set DECL_COMDAT on template instantiations; if we force
5137 them to be emitted by explicit instantiation or -frepo,
5138 mark_needed will tell cgraph to do the right thing. */
5139 DECL_COMDAT (decl
) = true;
5141 return DECL_TEMPLATE_RESULT (tmpl
);
5145 push_template_decl (tree decl
)
5147 return push_template_decl_real (decl
, false);
5150 /* FN is an inheriting constructor that inherits from the constructor
5151 template INHERITED; turn FN into a constructor template with a matching
5155 add_inherited_template_parms (tree fn
, tree inherited
)
5158 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5159 inner_parms
= copy_node (inner_parms
);
5161 = tree_cons (size_int (processing_template_decl
+ 1),
5162 inner_parms
, current_template_parms
);
5163 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5164 tree args
= template_parms_to_args (parms
);
5165 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5166 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5167 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5168 DECL_ARTIFICIAL (tmpl
) = true;
5169 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5173 /* Called when a class template TYPE is redeclared with the indicated
5174 template PARMS, e.g.:
5176 template <class T> struct S;
5177 template <class T> struct S {}; */
5180 redeclare_class_template (tree type
, tree parms
)
5186 if (!TYPE_TEMPLATE_INFO (type
))
5188 error ("%qT is not a template type", type
);
5192 tmpl
= TYPE_TI_TEMPLATE (type
);
5193 if (!PRIMARY_TEMPLATE_P (tmpl
))
5194 /* The type is nested in some template class. Nothing to worry
5195 about here; there are no new template parameters for the nested
5201 error ("template specifiers not specified in declaration of %qD",
5206 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5207 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5209 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5211 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5212 "redeclared with %d template parameter",
5213 "redeclared with %d template parameters",
5214 TREE_VEC_LENGTH (parms
));
5215 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
5216 "previous declaration %q+D used %d template parameter",
5217 "previous declaration %q+D used %d template parameters",
5218 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5222 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5229 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5230 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5233 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5234 if (error_operand_p (tmpl_parm
))
5237 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5238 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5239 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5241 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5243 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5244 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5245 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5246 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5247 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5248 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5249 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5250 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5251 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5253 error ("template parameter %q+#D", tmpl_parm
);
5254 error ("redeclared here as %q#D", parm
);
5258 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5260 /* We have in [temp.param]:
5262 A template-parameter may not be given default arguments
5263 by two different declarations in the same scope. */
5264 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5265 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5266 "original definition appeared here");
5270 if (parm_default
!= NULL_TREE
)
5271 /* Update the previous template parameters (which are the ones
5272 that will really count) with the new default value. */
5273 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5274 else if (tmpl_default
!= NULL_TREE
)
5275 /* Update the new parameters, too; they'll be used as the
5276 parameters for any members. */
5277 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5283 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5284 to be used when the caller has already checked
5285 (processing_template_decl
5286 && !instantiation_dependent_expression_p (expr)
5287 && potential_constant_expression (expr))
5288 and cleared processing_template_decl. */
5291 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
5293 return tsubst_copy_and_build (expr
,
5296 /*in_decl=*/NULL_TREE
,
5297 /*function_p=*/false,
5298 /*integral_constant_expression_p=*/true);
5301 /* Simplify EXPR if it is a non-dependent expression. Returns the
5302 (possibly simplified) expression. */
5305 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5307 if (expr
== NULL_TREE
)
5310 /* If we're in a template, but EXPR isn't value dependent, simplify
5311 it. We're supposed to treat:
5313 template <typename T> void f(T[1 + 1]);
5314 template <typename T> void f(T[2]);
5316 as two declarations of the same function, for example. */
5317 if (processing_template_decl
5318 && !instantiation_dependent_expression_p (expr
)
5319 && potential_constant_expression (expr
))
5321 processing_template_decl_sentinel s
;
5322 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
5328 instantiate_non_dependent_expr (tree expr
)
5330 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
5333 /* True iff T is a specialization of a variable template. */
5336 variable_template_specialization_p (tree t
)
5338 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
5340 tree tmpl
= DECL_TI_TEMPLATE (t
);
5341 return variable_template_p (tmpl
);
5344 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5345 template declaration, or a TYPE_DECL for an alias declaration. */
5348 alias_type_or_template_p (tree t
)
5352 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5355 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5356 || DECL_ALIAS_TEMPLATE_P (t
));
5359 /* Return TRUE iff T is a specialization of an alias template. */
5362 alias_template_specialization_p (const_tree t
)
5364 /* It's an alias template specialization if it's an alias and its
5365 TYPE_NAME is a specialization of a primary template. */
5366 if (TYPE_ALIAS_P (t
))
5368 tree name
= TYPE_NAME (t
);
5369 if (DECL_LANG_SPECIFIC (name
))
5370 if (tree ti
= DECL_TEMPLATE_INFO (name
))
5372 tree tmpl
= TI_TEMPLATE (ti
);
5373 return PRIMARY_TEMPLATE_P (tmpl
);
5379 /* An alias template is complex from a SFINAE perspective if a template-id
5380 using that alias can be ill-formed when the expansion is not, as with
5381 the void_t template. We determine this by checking whether the
5382 expansion for the alias template uses all its template parameters. */
5384 struct uses_all_template_parms_data
5391 uses_all_template_parms_r (tree t
, void *data_
)
5393 struct uses_all_template_parms_data
&data
5394 = *(struct uses_all_template_parms_data
*)data_
;
5395 tree idx
= get_template_parm_index (t
);
5397 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
5398 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
5403 complex_alias_template_p (const_tree tmpl
)
5405 struct uses_all_template_parms_data data
;
5406 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5407 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5408 data
.level
= TMPL_PARMS_DEPTH (parms
);
5409 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
5410 data
.seen
= XALLOCAVEC (bool, len
);
5411 for (int i
= 0; i
< len
; ++i
)
5412 data
.seen
[i
] = false;
5414 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
5415 for (int i
= 0; i
< len
; ++i
)
5421 /* Return TRUE iff T is a specialization of a complex alias template with
5422 dependent template-arguments. */
5425 dependent_alias_template_spec_p (const_tree t
)
5427 return (alias_template_specialization_p (t
)
5428 && TEMPLATE_DECL_COMPLEX_ALIAS_P (DECL_TI_TEMPLATE (TYPE_NAME (t
)))
5429 && (any_dependent_template_arguments_p
5430 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (t
)))));
5433 /* Return the number of innermost template parameters in TMPL. */
5436 num_innermost_template_parms (tree tmpl
)
5438 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
5439 return TREE_VEC_LENGTH (parms
);
5442 /* Return either TMPL or another template that it is equivalent to under DR
5443 1286: An alias that just changes the name of a template is equivalent to
5444 the other template. */
5447 get_underlying_template (tree tmpl
)
5449 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
5450 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
5452 tree result
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5453 if (TYPE_TEMPLATE_INFO (result
))
5455 tree sub
= TYPE_TI_TEMPLATE (result
);
5456 if (PRIMARY_TEMPLATE_P (sub
)
5457 && (num_innermost_template_parms (tmpl
)
5458 == num_innermost_template_parms (sub
)))
5460 tree alias_args
= INNERMOST_TEMPLATE_ARGS
5461 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
5462 if (!comp_template_args (TYPE_TI_ARGS (result
), alias_args
))
5464 /* The alias type is equivalent to the pattern of the
5465 underlying template, so strip the alias. */
5475 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5476 must be a function or a pointer-to-function type, as specified
5477 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5478 and check that the resulting function has external linkage. */
5481 convert_nontype_argument_function (tree type
, tree expr
,
5482 tsubst_flags_t complain
)
5486 linkage_kind linkage
;
5488 fn
= instantiate_type (type
, fns
, tf_none
);
5489 if (fn
== error_mark_node
)
5490 return error_mark_node
;
5493 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5494 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5495 if (BASELINK_P (fn_no_ptr
))
5496 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5498 /* [temp.arg.nontype]/1
5500 A template-argument for a non-type, non-template template-parameter
5503 -- the address of an object or function with external [C++11: or
5504 internal] linkage. */
5506 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
5508 if (complain
& tf_error
)
5510 error ("%qE is not a valid template argument for type %qT",
5512 if (TYPE_PTR_P (type
))
5513 error ("it must be the address of a function with "
5514 "external linkage");
5516 error ("it must be the name of a function with "
5517 "external linkage");
5522 linkage
= decl_linkage (fn_no_ptr
);
5523 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
5525 if (complain
& tf_error
)
5527 if (cxx_dialect
>= cxx11
)
5528 error ("%qE is not a valid template argument for type %qT "
5529 "because %qD has no linkage",
5530 expr
, type
, fn_no_ptr
);
5532 error ("%qE is not a valid template argument for type %qT "
5533 "because %qD does not have external linkage",
5534 expr
, type
, fn_no_ptr
);
5542 /* Subroutine of convert_nontype_argument.
5543 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5544 Emit an error otherwise. */
5547 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5548 tsubst_flags_t complain
)
5551 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5553 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
5555 if (processing_template_decl
5556 && TREE_CODE (expr
) == ADDR_EXPR
5557 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
5559 if (complain
& tf_error
)
5561 error ("%qE is not a valid template argument for type %qT",
5563 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5568 /* Returns TRUE iff the address of OP is value-dependent.
5570 14.6.2.4 [temp.dep.temp]:
5571 A non-integral non-type template-argument is dependent if its type is
5572 dependent or it has either of the following forms
5575 and contains a nested-name-specifier which specifies a class-name that
5576 names a dependent type.
5578 We generalize this to just say that the address of a member of a
5579 dependent class is value-dependent; the above doesn't cover the
5580 address of a static data member named with an unqualified-id. */
5583 has_value_dependent_address (tree op
)
5585 /* We could use get_inner_reference here, but there's no need;
5586 this is only relevant for template non-type arguments, which
5587 can only be expressed as &id-expression. */
5590 tree ctx
= CP_DECL_CONTEXT (op
);
5591 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5598 /* The next set of functions are used for providing helpful explanatory
5599 diagnostics for failed overload resolution. Their messages should be
5600 indented by two spaces for consistency with the messages in
5604 unify_success (bool /*explain_p*/)
5610 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5613 inform (input_location
,
5614 " couldn't deduce template parameter %qD", parm
);
5619 unify_invalid (bool /*explain_p*/)
5625 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5628 inform (input_location
,
5629 " types %qT and %qT have incompatible cv-qualifiers",
5635 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5638 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5643 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
5646 inform (input_location
,
5647 " template parameter %qD is not a parameter pack, but "
5654 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
5657 inform (input_location
,
5658 " template argument %qE does not match "
5659 "pointer-to-member constant %qE",
5665 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
5668 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
5673 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
5676 inform (input_location
,
5677 " inconsistent parameter pack deduction with %qT and %qT",
5683 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
5688 inform (input_location
,
5689 " deduced conflicting types for parameter %qT (%qT and %qT)",
5690 parm
, first
, second
);
5692 inform (input_location
,
5693 " deduced conflicting values for non-type parameter "
5694 "%qE (%qE and %qE)", parm
, first
, second
);
5700 unify_vla_arg (bool explain_p
, tree arg
)
5703 inform (input_location
,
5704 " variable-sized array type %qT is not "
5705 "a valid template argument",
5711 unify_method_type_error (bool explain_p
, tree arg
)
5714 inform (input_location
,
5715 " member function type %qT is not a valid template argument",
5721 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
5726 inform_n (input_location
, wanted
,
5727 " candidate expects at least %d argument, %d provided",
5728 " candidate expects at least %d arguments, %d provided",
5731 inform_n (input_location
, wanted
,
5732 " candidate expects %d argument, %d provided",
5733 " candidate expects %d arguments, %d provided",
5740 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
5742 return unify_arity (explain_p
, have
, wanted
);
5746 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
5747 bool least_p
= false)
5749 return unify_arity (explain_p
, have
, wanted
, least_p
);
5753 unify_arg_conversion (bool explain_p
, tree to_type
,
5754 tree from_type
, tree arg
)
5757 inform (EXPR_LOC_OR_LOC (arg
, input_location
),
5758 " cannot convert %qE (type %qT) to type %qT",
5759 arg
, from_type
, to_type
);
5764 unify_no_common_base (bool explain_p
, enum template_base_result r
,
5765 tree parm
, tree arg
)
5770 case tbr_ambiguous_baseclass
:
5771 inform (input_location
, " %qT is an ambiguous base class of %qT",
5775 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
5782 unify_inconsistent_template_template_parameters (bool explain_p
)
5785 inform (input_location
,
5786 " template parameters of a template template argument are "
5787 "inconsistent with other deduced template arguments");
5792 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
5795 inform (input_location
,
5796 " can't deduce a template for %qT from non-template type %qT",
5802 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
5805 inform (input_location
,
5806 " template argument %qE does not match %qD", arg
, parm
);
5811 unify_overload_resolution_failure (bool explain_p
, tree arg
)
5814 inform (input_location
,
5815 " could not resolve address from overloaded function %qE",
5820 /* Attempt to convert the non-type template parameter EXPR to the
5821 indicated TYPE. If the conversion is successful, return the
5822 converted value. If the conversion is unsuccessful, return
5823 NULL_TREE if we issued an error message, or error_mark_node if we
5824 did not. We issue error messages for out-and-out bad template
5825 parameters, but not simply because the conversion failed, since we
5826 might be just trying to do argument deduction. Both TYPE and EXPR
5827 must be non-dependent.
5829 The conversion follows the special rules described in
5830 [temp.arg.nontype], and it is much more strict than an implicit
5833 This function is called twice for each template argument (see
5834 lookup_template_class for a more accurate description of this
5835 problem). This means that we need to handle expressions which
5836 are not valid in a C++ source, but can be created from the
5837 first call (for instance, casts to perform conversions). These
5838 hacks can go away after we fix the double coercion problem. */
5841 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5845 /* Detect immediately string literals as invalid non-type argument.
5846 This special-case is not needed for correctness (we would easily
5847 catch this later), but only to provide better diagnostic for this
5848 common user mistake. As suggested by DR 100, we do not mention
5849 linkage issues in the diagnostic as this is not the point. */
5850 /* FIXME we're making this OK. */
5851 if (TREE_CODE (expr
) == STRING_CST
)
5853 if (complain
& tf_error
)
5854 error ("%qE is not a valid template argument for type %qT "
5855 "because string literals can never be used in this context",
5860 /* Add the ADDR_EXPR now for the benefit of
5861 value_dependent_expression_p. */
5862 if (TYPE_PTROBV_P (type
)
5863 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
5865 expr
= decay_conversion (expr
, complain
);
5866 if (expr
== error_mark_node
)
5867 return error_mark_node
;
5870 /* If we are in a template, EXPR may be non-dependent, but still
5871 have a syntactic, rather than semantic, form. For example, EXPR
5872 might be a SCOPE_REF, rather than the VAR_DECL to which the
5873 SCOPE_REF refers. Preserving the qualifying scope is necessary
5874 so that access checking can be performed when the template is
5875 instantiated -- but here we need the resolved form so that we can
5876 convert the argument. */
5877 bool non_dep
= false;
5878 if (TYPE_REF_OBJ_P (type
)
5879 && has_value_dependent_address (expr
))
5880 /* If we want the address and it's value-dependent, don't fold. */;
5881 else if (!type_unknown_p (expr
)
5882 && processing_template_decl
5883 && !instantiation_dependent_expression_p (expr
)
5884 && potential_constant_expression (expr
))
5886 if (error_operand_p (expr
))
5887 return error_mark_node
;
5888 expr_type
= TREE_TYPE (expr
);
5889 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5890 expr
= mark_lvalue_use (expr
);
5892 expr
= mark_rvalue_use (expr
);
5894 /* If the argument is non-dependent, perform any conversions in
5895 non-dependent context as well. */
5896 processing_template_decl_sentinel
s (non_dep
);
5898 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
5900 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5901 to a non-type argument of "nullptr". */
5902 if (expr
== nullptr_node
&& TYPE_PTR_OR_PTRMEM_P (type
))
5903 expr
= convert (type
, expr
);
5905 /* In C++11, integral or enumeration non-type template arguments can be
5906 arbitrary constant expressions. Pointer and pointer to
5907 member arguments can be general constant expressions that evaluate
5908 to a null value, but otherwise still need to be of a specific form. */
5909 if (cxx_dialect
>= cxx11
)
5911 if (TREE_CODE (expr
) == PTRMEM_CST
)
5912 /* A PTRMEM_CST is already constant, and a valid template
5913 argument for a parameter of pointer to member type, we just want
5914 to leave it in that form rather than lower it to a
5916 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5917 expr
= maybe_constant_value (expr
);
5918 else if (TYPE_PTR_OR_PTRMEM_P (type
))
5920 tree folded
= maybe_constant_value (expr
);
5921 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
5922 : null_member_pointer_value_p (folded
))
5927 /* HACK: Due to double coercion, we can get a
5928 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5929 which is the tree that we built on the first call (see
5930 below when coercing to reference to object or to reference to
5931 function). We just strip everything and get to the arg.
5932 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5934 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5936 tree probe_type
, probe
= expr
;
5937 if (REFERENCE_REF_P (probe
))
5938 probe
= TREE_OPERAND (probe
, 0);
5939 probe_type
= TREE_TYPE (probe
);
5940 if (TREE_CODE (probe
) == NOP_EXPR
)
5942 /* ??? Maybe we could use convert_from_reference here, but we
5943 would need to relax its constraints because the NOP_EXPR
5944 could actually change the type to something more cv-qualified,
5945 and this is not folded by convert_from_reference. */
5946 tree addr
= TREE_OPERAND (probe
, 0);
5947 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
5948 && TREE_CODE (addr
) == ADDR_EXPR
5949 && TYPE_PTR_P (TREE_TYPE (addr
))
5950 && (same_type_ignoring_top_level_qualifiers_p
5951 (TREE_TYPE (probe_type
),
5952 TREE_TYPE (TREE_TYPE (addr
)))))
5954 expr
= TREE_OPERAND (addr
, 0);
5955 expr_type
= TREE_TYPE (probe_type
);
5960 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5961 parameter is a pointer to object, through decay and
5962 qualification conversion. Let's strip everything. */
5963 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
5967 if (TREE_CODE (probe
) == ADDR_EXPR
5968 && TYPE_PTR_P (TREE_TYPE (probe
)))
5970 /* Skip the ADDR_EXPR only if it is part of the decay for
5971 an array. Otherwise, it is part of the original argument
5972 in the source code. */
5973 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe
, 0))) == ARRAY_TYPE
)
5974 probe
= TREE_OPERAND (probe
, 0);
5976 expr_type
= TREE_TYPE (expr
);
5980 /* [temp.arg.nontype]/5, bullet 1
5982 For a non-type template-parameter of integral or enumeration type,
5983 integral promotions (_conv.prom_) and integral conversions
5984 (_conv.integral_) are applied. */
5985 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5987 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
5988 t
= maybe_constant_value (t
);
5989 if (t
!= error_mark_node
)
5992 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
5993 return error_mark_node
;
5995 /* Notice that there are constant expressions like '4 % 0' which
5996 do not fold into integer constants. */
5997 if (TREE_CODE (expr
) != INTEGER_CST
)
5999 if (complain
& tf_error
)
6001 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
6002 if (processing_template_decl
6003 && !require_potential_constant_expression (expr
))
6005 expr
= cxx_constant_value (expr
);
6006 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
6007 inform (EXPR_LOC_OR_LOC (expr
, input_location
),
6008 "in template argument for type %qT ", type
);
6009 if (expr
== error_mark_node
)
6011 /* else cxx_constant_value complained but gave us
6012 a real constant, so go ahead. */
6013 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
6019 /* Avoid typedef problems. */
6020 if (TREE_TYPE (expr
) != type
)
6021 expr
= fold_convert (type
, expr
);
6023 /* [temp.arg.nontype]/5, bullet 2
6025 For a non-type template-parameter of type pointer to object,
6026 qualification conversions (_conv.qual_) and the array-to-pointer
6027 conversion (_conv.array_) are applied. */
6028 else if (TYPE_PTROBV_P (type
))
6030 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6032 A template-argument for a non-type, non-template template-parameter
6033 shall be one of: [...]
6035 -- the name of a non-type template-parameter;
6036 -- the address of an object or function with external linkage, [...]
6037 expressed as "& id-expression" where the & is optional if the name
6038 refers to a function or array, or if the corresponding
6039 template-parameter is a reference.
6041 Here, we do not care about functions, as they are invalid anyway
6042 for a parameter of type pointer-to-object. */
6044 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
6045 /* Non-type template parameters are OK. */
6047 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6048 /* Null pointer values are OK in C++11. */;
6049 else if (TREE_CODE (expr
) != ADDR_EXPR
6050 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
6054 if (complain
& tf_error
)
6055 error ("%qD is not a valid template argument "
6056 "because %qD is a variable, not the address of "
6057 "a variable", expr
, expr
);
6060 if (POINTER_TYPE_P (expr_type
))
6062 if (complain
& tf_error
)
6063 error ("%qE is not a valid template argument for %qT "
6064 "because it is not the address of a variable",
6068 /* Other values, like integer constants, might be valid
6069 non-type arguments of some other type. */
6070 return error_mark_node
;
6076 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
6077 ? TREE_OPERAND (expr
, 0) : expr
);
6080 if (complain
& tf_error
)
6081 error ("%qE is not a valid template argument of type %qT "
6082 "because %qE is not a variable", expr
, type
, decl
);
6085 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6087 if (complain
& tf_error
)
6088 error ("%qE is not a valid template argument of type %qT "
6089 "because %qD does not have external linkage",
6093 else if (cxx_dialect
>= cxx11
&& decl_linkage (decl
) == lk_none
)
6095 if (complain
& tf_error
)
6096 error ("%qE is not a valid template argument of type %qT "
6097 "because %qD has no linkage", expr
, type
, decl
);
6102 expr
= decay_conversion (expr
, complain
);
6103 if (expr
== error_mark_node
)
6104 return error_mark_node
;
6106 expr
= perform_qualification_conversions (type
, expr
);
6107 if (expr
== error_mark_node
)
6108 return error_mark_node
;
6110 /* [temp.arg.nontype]/5, bullet 3
6112 For a non-type template-parameter of type reference to object, no
6113 conversions apply. The type referred to by the reference may be more
6114 cv-qualified than the (otherwise identical) type of the
6115 template-argument. The template-parameter is bound directly to the
6116 template-argument, which must be an lvalue. */
6117 else if (TYPE_REF_OBJ_P (type
))
6119 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
6121 return error_mark_node
;
6123 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
6125 if (complain
& tf_error
)
6126 error ("%qE is not a valid template argument for type %qT "
6127 "because of conflicts in cv-qualification", expr
, type
);
6131 if (!real_lvalue_p (expr
))
6133 if (complain
& tf_error
)
6134 error ("%qE is not a valid template argument for type %qT "
6135 "because it is not an lvalue", expr
, type
);
6139 /* [temp.arg.nontype]/1
6141 A template-argument for a non-type, non-template template-parameter
6142 shall be one of: [...]
6144 -- the address of an object or function with external linkage. */
6145 if (INDIRECT_REF_P (expr
)
6146 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
6148 expr
= TREE_OPERAND (expr
, 0);
6151 if (complain
& tf_error
)
6152 error ("%q#D is not a valid template argument for type %qT "
6153 "because a reference variable does not have a constant "
6154 "address", expr
, type
);
6161 if (complain
& tf_error
)
6162 error ("%qE is not a valid template argument for type %qT "
6163 "because it is not an object with external linkage",
6168 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
6170 if (complain
& tf_error
)
6171 error ("%qE is not a valid template argument for type %qT "
6172 "because object %qD has not external linkage",
6177 expr
= build_nop (type
, build_address (expr
));
6179 /* [temp.arg.nontype]/5, bullet 4
6181 For a non-type template-parameter of type pointer to function, only
6182 the function-to-pointer conversion (_conv.func_) is applied. If the
6183 template-argument represents a set of overloaded functions (or a
6184 pointer to such), the matching function is selected from the set
6186 else if (TYPE_PTRFN_P (type
))
6188 /* If the argument is a template-id, we might not have enough
6189 context information to decay the pointer. */
6190 if (!type_unknown_p (expr_type
))
6192 expr
= decay_conversion (expr
, complain
);
6193 if (expr
== error_mark_node
)
6194 return error_mark_node
;
6197 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6198 /* Null pointer values are OK in C++11. */
6199 return perform_qualification_conversions (type
, expr
);
6201 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6202 if (!expr
|| expr
== error_mark_node
)
6205 /* [temp.arg.nontype]/5, bullet 5
6207 For a non-type template-parameter of type reference to function, no
6208 conversions apply. If the template-argument represents a set of
6209 overloaded functions, the matching function is selected from the set
6211 else if (TYPE_REFFN_P (type
))
6213 if (TREE_CODE (expr
) == ADDR_EXPR
)
6215 if (complain
& tf_error
)
6217 error ("%qE is not a valid template argument for type %qT "
6218 "because it is a pointer", expr
, type
);
6219 inform (input_location
, "try using %qE instead",
6220 TREE_OPERAND (expr
, 0));
6225 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6226 if (!expr
|| expr
== error_mark_node
)
6229 expr
= build_nop (type
, build_address (expr
));
6231 /* [temp.arg.nontype]/5, bullet 6
6233 For a non-type template-parameter of type pointer to member function,
6234 no conversions apply. If the template-argument represents a set of
6235 overloaded member functions, the matching member function is selected
6236 from the set (_over.over_). */
6237 else if (TYPE_PTRMEMFUNC_P (type
))
6239 expr
= instantiate_type (type
, expr
, tf_none
);
6240 if (expr
== error_mark_node
)
6241 return error_mark_node
;
6243 /* [temp.arg.nontype] bullet 1 says the pointer to member
6244 expression must be a pointer-to-member constant. */
6245 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6246 return error_mark_node
;
6248 /* There is no way to disable standard conversions in
6249 resolve_address_of_overloaded_function (called by
6250 instantiate_type). It is possible that the call succeeded by
6251 converting &B::I to &D::I (where B is a base of D), so we need
6252 to reject this conversion here.
6254 Actually, even if there was a way to disable standard conversions,
6255 it would still be better to reject them here so that we can
6256 provide a superior diagnostic. */
6257 if (!same_type_p (TREE_TYPE (expr
), type
))
6259 if (complain
& tf_error
)
6261 error ("%qE is not a valid template argument for type %qT "
6262 "because it is of type %qT", expr
, type
,
6264 /* If we are just one standard conversion off, explain. */
6265 if (can_convert_standard (type
, TREE_TYPE (expr
), complain
))
6266 inform (input_location
,
6267 "standard conversions are not allowed in this context");
6272 /* [temp.arg.nontype]/5, bullet 7
6274 For a non-type template-parameter of type pointer to data member,
6275 qualification conversions (_conv.qual_) are applied. */
6276 else if (TYPE_PTRDATAMEM_P (type
))
6278 /* [temp.arg.nontype] bullet 1 says the pointer to member
6279 expression must be a pointer-to-member constant. */
6280 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6281 return error_mark_node
;
6283 expr
= perform_qualification_conversions (type
, expr
);
6284 if (expr
== error_mark_node
)
6287 else if (NULLPTR_TYPE_P (type
))
6289 if (expr
!= nullptr_node
)
6291 if (complain
& tf_error
)
6292 error ("%qE is not a valid template argument for type %qT "
6293 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
6298 /* A template non-type parameter must be one of the above. */
6302 /* Sanity check: did we actually convert the argument to the
6304 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6305 (type
, TREE_TYPE (expr
)));
6306 return convert_from_reference (expr
);
6309 /* Subroutine of coerce_template_template_parms, which returns 1 if
6310 PARM_PARM and ARG_PARM match using the rule for the template
6311 parameters of template template parameters. Both PARM and ARG are
6312 template parameters; the rest of the arguments are the same as for
6313 coerce_template_template_parms.
6316 coerce_template_template_parm (tree parm
,
6318 tsubst_flags_t complain
,
6322 if (arg
== NULL_TREE
|| error_operand_p (arg
)
6323 || parm
== NULL_TREE
|| error_operand_p (parm
))
6326 if (TREE_CODE (arg
) != TREE_CODE (parm
))
6329 switch (TREE_CODE (parm
))
6332 /* We encounter instantiations of templates like
6333 template <template <template <class> class> class TT>
6336 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6337 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6339 if (!coerce_template_template_parms
6340 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
6346 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
6347 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6348 /* Argument is a parameter pack but parameter is not. */
6353 /* The tsubst call is used to handle cases such as
6355 template <int> class C {};
6356 template <class T, template <T> class TT> class D {};
6359 i.e. the parameter list of TT depends on earlier parameters. */
6360 if (!uses_template_parms (TREE_TYPE (arg
))
6362 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
6366 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6367 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6368 /* Argument is a parameter pack but parameter is not. */
6381 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6382 template template parameters. Both PARM_PARMS and ARG_PARMS are
6383 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6386 Consider the example:
6387 template <class T> class A;
6388 template<template <class U> class TT> class B;
6390 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6391 the parameters to A, and OUTER_ARGS contains A. */
6394 coerce_template_template_parms (tree parm_parms
,
6396 tsubst_flags_t complain
,
6400 int nparms
, nargs
, i
;
6404 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
6405 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
6407 nparms
= TREE_VEC_LENGTH (parm_parms
);
6408 nargs
= TREE_VEC_LENGTH (arg_parms
);
6410 /* Determine whether we have a parameter pack at the end of the
6411 template template parameter's template parameter list. */
6412 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
6414 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
6416 if (error_operand_p (parm
))
6419 switch (TREE_CODE (parm
))
6423 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6428 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6438 && !(variadic_p
&& nargs
>= nparms
- 1))
6441 /* Check all of the template parameters except the parameter pack at
6442 the end (if any). */
6443 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6445 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6446 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6449 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6450 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6452 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6460 /* Check each of the template parameters in the template
6461 argument against the template parameter pack at the end of
6462 the template template parameter. */
6463 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6466 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6468 for (; i
< nargs
; ++i
)
6470 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6473 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6475 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6484 /* Verifies that the deduced template arguments (in TARGS) for the
6485 template template parameters (in TPARMS) represent valid bindings,
6486 by comparing the template parameter list of each template argument
6487 to the template parameter list of its corresponding template
6488 template parameter, in accordance with DR150. This
6489 routine can only be called after all template arguments have been
6490 deduced. It will return TRUE if all of the template template
6491 parameter bindings are okay, FALSE otherwise. */
6493 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6495 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6498 /* We're dealing with template parms in this process. */
6499 ++processing_template_decl
;
6501 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6503 for (i
= 0; i
< ntparms
; ++i
)
6505 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6506 tree targ
= TREE_VEC_ELT (targs
, i
);
6508 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6510 tree packed_args
= NULL_TREE
;
6513 if (ARGUMENT_PACK_P (targ
))
6515 /* Look inside the argument pack. */
6516 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6517 len
= TREE_VEC_LENGTH (packed_args
);
6520 for (idx
= 0; idx
< len
; ++idx
)
6522 tree targ_parms
= NULL_TREE
;
6525 /* Extract the next argument from the argument
6527 targ
= TREE_VEC_ELT (packed_args
, idx
);
6529 if (PACK_EXPANSION_P (targ
))
6530 /* Look at the pattern of the pack expansion. */
6531 targ
= PACK_EXPANSION_PATTERN (targ
);
6533 /* Extract the template parameters from the template
6535 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6536 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6537 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6538 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6540 /* Verify that we can coerce the template template
6541 parameters from the template argument to the template
6542 parameter. This requires an exact match. */
6544 && !coerce_template_template_parms
6545 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6560 --processing_template_decl
;
6564 /* Since type attributes aren't mangled, we need to strip them from
6565 template type arguments. */
6568 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6571 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6573 mv
= TYPE_MAIN_VARIANT (arg
);
6574 arg
= strip_typedefs (arg
);
6575 if (TYPE_ALIGN (arg
) != TYPE_ALIGN (mv
)
6576 || TYPE_ATTRIBUTES (arg
) != TYPE_ATTRIBUTES (mv
))
6578 if (complain
& tf_warning
)
6579 warning (0, "ignoring attributes on template argument %qT", arg
);
6580 arg
= build_aligned_type (arg
, TYPE_ALIGN (mv
));
6581 arg
= cp_build_type_attribute_variant (arg
, TYPE_ATTRIBUTES (mv
));
6586 /* Convert the indicated template ARG as necessary to match the
6587 indicated template PARM. Returns the converted ARG, or
6588 error_mark_node if the conversion was unsuccessful. Error and
6589 warning messages are issued under control of COMPLAIN. This
6590 conversion is for the Ith parameter in the parameter list. ARGS is
6591 the full set of template arguments deduced so far. */
6594 convert_template_argument (tree parm
,
6597 tsubst_flags_t complain
,
6603 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
6605 if (parm
== error_mark_node
)
6606 return error_mark_node
;
6608 if (TREE_CODE (arg
) == TREE_LIST
6609 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
6611 /* The template argument was the name of some
6612 member function. That's usually
6613 invalid, but static members are OK. In any
6614 case, grab the underlying fields/functions
6615 and issue an error later if required. */
6616 orig_arg
= TREE_VALUE (arg
);
6617 TREE_TYPE (arg
) = unknown_type_node
;
6622 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
6623 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
6624 || requires_tmpl_type
);
6626 /* When determining whether an argument pack expansion is a template,
6627 look at the pattern. */
6628 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
6629 arg
= PACK_EXPANSION_PATTERN (arg
);
6631 /* Deal with an injected-class-name used as a template template arg. */
6632 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
6634 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
6635 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6637 if (cxx_dialect
>= cxx11
)
6638 /* OK under DR 1004. */;
6639 else if (complain
& tf_warning_or_error
)
6640 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
6641 " used as template template argument", TYPE_NAME (arg
));
6642 else if (flag_pedantic_errors
)
6650 ((TREE_CODE (arg
) == TEMPLATE_DECL
6651 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
6652 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
6653 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6654 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
6657 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6658 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
6659 arg
= TYPE_STUB_DECL (arg
);
6661 is_type
= TYPE_P (arg
) || is_tmpl_type
;
6663 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
6664 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
6666 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
6668 if (complain
& tf_error
)
6669 error ("invalid use of destructor %qE as a type", orig_arg
);
6670 return error_mark_node
;
6673 permerror (input_location
,
6674 "to refer to a type member of a template parameter, "
6675 "use %<typename %E%>", orig_arg
);
6677 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
6678 TREE_OPERAND (arg
, 1),
6684 if (is_type
!= requires_type
)
6688 if (complain
& tf_error
)
6690 error ("type/value mismatch at argument %d in template "
6691 "parameter list for %qD",
6694 inform (input_location
,
6695 " expected a constant of type %qT, got %qT",
6697 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6698 else if (requires_tmpl_type
)
6699 inform (input_location
,
6700 " expected a class template, got %qE", orig_arg
);
6702 inform (input_location
,
6703 " expected a type, got %qE", orig_arg
);
6706 return error_mark_node
;
6708 if (is_tmpl_type
^ requires_tmpl_type
)
6710 if (in_decl
&& (complain
& tf_error
))
6712 error ("type/value mismatch at argument %d in template "
6713 "parameter list for %qD",
6716 inform (input_location
,
6717 " expected a type, got %qT", DECL_NAME (arg
));
6719 inform (input_location
,
6720 " expected a class template, got %qT", orig_arg
);
6722 return error_mark_node
;
6727 if (requires_tmpl_type
)
6729 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6731 else if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6732 /* The number of argument required is not known yet.
6733 Just accept it for now. */
6734 val
= TREE_TYPE (arg
);
6737 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6740 /* Strip alias templates that are equivalent to another
6742 arg
= get_underlying_template (arg
);
6743 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6745 if (coerce_template_template_parms (parmparm
, argparm
,
6751 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6753 if (val
!= error_mark_node
)
6755 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6756 val
= TREE_TYPE (val
);
6757 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6758 val
= make_pack_expansion (val
);
6763 if (in_decl
&& (complain
& tf_error
))
6765 error ("type/value mismatch at argument %d in "
6766 "template parameter list for %qD",
6768 inform (input_location
,
6769 " expected a template of type %qD, got %qT",
6773 val
= error_mark_node
;
6779 /* We only form one instance of each template specialization.
6780 Therefore, if we use a non-canonical variant (i.e., a
6781 typedef), any future messages referring to the type will use
6782 the typedef, which is confusing if those future uses do not
6783 themselves also use the typedef. */
6785 val
= canonicalize_type_argument (val
, complain
);
6789 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6791 if (invalid_nontype_parm_type_p (t
, complain
))
6792 return error_mark_node
;
6794 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6796 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6800 /* Not sure if this is reachable, but it doesn't hurt
6802 error ("type mismatch in nontype parameter pack");
6803 val
= error_mark_node
;
6806 else if (!dependent_template_arg_p (orig_arg
)
6807 && !uses_template_parms (t
))
6808 /* We used to call digest_init here. However, digest_init
6809 will report errors, which we don't want when complain
6810 is zero. More importantly, digest_init will try too
6811 hard to convert things: for example, `0' should not be
6812 converted to pointer type at this point according to
6813 the standard. Accepting this is not merely an
6814 extension, since deciding whether or not these
6815 conversions can occur is part of determining which
6816 function template to call, or whether a given explicit
6817 argument specification is valid. */
6818 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6820 val
= strip_typedefs_expr (orig_arg
);
6822 if (val
== NULL_TREE
)
6823 val
= error_mark_node
;
6824 else if (val
== error_mark_node
&& (complain
& tf_error
))
6825 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6827 if (TREE_CODE (val
) == SCOPE_REF
)
6829 /* Strip typedefs from the SCOPE_REF. */
6830 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
6831 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
6833 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6834 QUALIFIED_NAME_IS_TEMPLATE (val
));
6841 /* Coerces the remaining template arguments in INNER_ARGS (from
6842 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6843 Returns the coerced argument pack. PARM_IDX is the position of this
6844 parameter in the template parameter list. ARGS is the original
6845 template argument list. */
6847 coerce_template_parameter_pack (tree parms
,
6855 tsubst_flags_t complain
)
6857 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6858 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6861 tree packed_parms
= NULL_TREE
;
6863 if (arg_idx
> nargs
)
6866 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
6868 /* When the template parameter is a non-type template parameter pack
6869 or template template parameter pack whose type or template
6870 parameters use parameter packs, we know exactly how many arguments
6871 we are looking for. Build a vector of the instantiated decls for
6872 these template parameters in PACKED_PARMS. */
6873 /* We can't use make_pack_expansion here because it would interpret a
6874 _DECL as a use rather than a declaration. */
6875 tree decl
= TREE_VALUE (parm
);
6876 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
6877 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
6878 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
6879 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
6881 TREE_VEC_LENGTH (args
)--;
6882 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
6883 TREE_VEC_LENGTH (args
)++;
6885 if (packed_parms
== error_mark_node
)
6886 return error_mark_node
;
6888 /* If we're doing a partial instantiation of a member template,
6889 verify that all of the types used for the non-type
6890 template parameter pack are, in fact, valid for non-type
6891 template parameters. */
6893 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6895 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
6896 for (j
= 0; j
< len
; ++j
)
6898 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
6899 if (invalid_nontype_parm_type_p (t
, complain
))
6900 return error_mark_node
;
6902 /* We don't know how many args we have yet, just
6903 use the unconverted ones for now. */
6907 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
6910 packed_args
= make_tree_vec (nargs
- arg_idx
);
6912 /* Convert the remaining arguments, which will be a part of the
6913 parameter pack "parm". */
6914 for (; arg_idx
< nargs
; ++arg_idx
)
6916 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6917 tree actual_parm
= TREE_VALUE (parm
);
6918 int pack_idx
= arg_idx
- parm_idx
;
6922 /* Once we've packed as many args as we have types, stop. */
6923 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
6925 else if (PACK_EXPANSION_P (arg
))
6926 /* We don't know how many args we have yet, just
6927 use the unconverted ones for now. */
6930 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
6933 if (arg
== error_mark_node
)
6935 if (complain
& tf_error
)
6936 error ("template argument %d is invalid", arg_idx
+ 1);
6939 arg
= convert_template_argument (actual_parm
,
6940 arg
, new_args
, complain
, parm_idx
,
6942 if (arg
== error_mark_node
)
6944 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
6947 if (arg_idx
- parm_idx
< TREE_VEC_LENGTH (packed_args
)
6948 && TREE_VEC_LENGTH (packed_args
) > 0)
6950 if (complain
& tf_error
)
6951 error ("wrong number of template arguments (%d, should be %d)",
6952 arg_idx
- parm_idx
, TREE_VEC_LENGTH (packed_args
));
6953 return error_mark_node
;
6956 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6957 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
6958 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
6961 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
6962 TREE_TYPE (argument_pack
)
6963 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
6964 TREE_CONSTANT (argument_pack
) = 1;
6967 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
6968 #ifdef ENABLE_CHECKING
6969 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
6970 TREE_VEC_LENGTH (packed_args
));
6972 return argument_pack
;
6975 /* Returns the number of pack expansions in the template argument vector
6979 pack_expansion_args_count (tree args
)
6984 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
6986 tree elt
= TREE_VEC_ELT (args
, i
);
6987 if (elt
&& PACK_EXPANSION_P (elt
))
6993 /* Convert all template arguments to their appropriate types, and
6994 return a vector containing the innermost resulting template
6995 arguments. If any error occurs, return error_mark_node. Error and
6996 warning messages are issued under control of COMPLAIN.
6998 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6999 for arguments not specified in ARGS. Otherwise, if
7000 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7001 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7002 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7006 coerce_template_parms (tree parms
,
7009 tsubst_flags_t complain
,
7010 bool require_all_args
,
7011 bool use_default_args
)
7013 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
7014 tree orig_inner_args
;
7017 tree new_inner_args
;
7018 int saved_unevaluated_operand
;
7019 int saved_inhibit_evaluation_warnings
;
7021 /* When used as a boolean value, indicates whether this is a
7022 variadic template parameter list. Since it's an int, we can also
7023 subtract it from nparms to get the number of non-variadic
7026 int variadic_args_p
= 0;
7027 int post_variadic_parms
= 0;
7029 /* Likewise for parameters with default arguments. */
7032 if (args
== error_mark_node
)
7033 return error_mark_node
;
7035 nparms
= TREE_VEC_LENGTH (parms
);
7037 /* Determine if there are any parameter packs or default arguments. */
7038 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
7040 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
7042 ++post_variadic_parms
;
7043 if (template_parameter_pack_p (TREE_VALUE (parm
)))
7045 if (TREE_PURPOSE (parm
))
7049 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
7050 /* If there are no parameters that follow a parameter pack, we need to
7051 expand any argument packs so that we can deduce a parameter pack from
7052 some non-packed args followed by an argument pack, as in variadic85.C.
7053 If there are such parameters, we need to leave argument packs intact
7054 so the arguments are assigned properly. This can happen when dealing
7055 with a nested class inside a partial specialization of a class
7056 template, as in variadic92.C, or when deducing a template parameter pack
7057 from a sub-declarator, as in variadic114.C. */
7058 if (!post_variadic_parms
)
7059 inner_args
= expand_template_argument_pack (inner_args
);
7061 /* Count any pack expansion args. */
7062 variadic_args_p
= pack_expansion_args_count (inner_args
);
7064 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
7065 if ((nargs
> nparms
&& !variadic_p
)
7066 || (nargs
< nparms
- variadic_p
7069 && (!use_default_args
7070 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
7071 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
7073 if (complain
& tf_error
)
7075 if (variadic_p
|| default_p
)
7077 nparms
-= variadic_p
+ default_p
;
7078 error ("wrong number of template arguments "
7079 "(%d, should be at least %d)", nargs
, nparms
);
7082 error ("wrong number of template arguments "
7083 "(%d, should be %d)", nargs
, nparms
);
7086 inform (input_location
, "provided for %q+D", in_decl
);
7089 return error_mark_node
;
7091 /* We can't pass a pack expansion to a non-pack parameter of an alias
7092 template (DR 1430). */
7093 else if (in_decl
&& DECL_ALIAS_TEMPLATE_P (in_decl
)
7095 && nargs
- variadic_args_p
< nparms
- variadic_p
)
7097 if (complain
& tf_error
)
7099 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
7101 tree arg
= TREE_VEC_ELT (inner_args
, i
);
7102 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
7104 if (PACK_EXPANSION_P (arg
)
7105 && !template_parameter_pack_p (parm
))
7107 error ("pack expansion argument for non-pack parameter "
7108 "%qD of alias template %qD", parm
, in_decl
);
7109 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
7116 return error_mark_node
;
7119 /* We need to evaluate the template arguments, even though this
7120 template-id may be nested within a "sizeof". */
7121 saved_unevaluated_operand
= cp_unevaluated_operand
;
7122 cp_unevaluated_operand
= 0;
7123 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
7124 c_inhibit_evaluation_warnings
= 0;
7125 new_inner_args
= make_tree_vec (nparms
);
7126 new_args
= add_outermost_template_args (args
, new_inner_args
);
7127 int pack_adjust
= 0;
7128 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
7133 /* Get the Ith template parameter. */
7134 parm
= TREE_VEC_ELT (parms
, parm_idx
);
7136 if (parm
== error_mark_node
)
7138 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
7142 /* Calculate the next argument. */
7143 if (arg_idx
< nargs
)
7144 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
7148 if (template_parameter_pack_p (TREE_VALUE (parm
))
7149 && !(arg
&& ARGUMENT_PACK_P (arg
)))
7151 /* Some arguments will be placed in the
7152 template parameter pack PARM. */
7153 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
7154 inner_args
, arg_idx
,
7158 if (arg
== NULL_TREE
)
7160 /* We don't know how many args we have yet, just use the
7161 unconverted (and still packed) ones for now. */
7162 new_inner_args
= orig_inner_args
;
7167 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
7169 /* Store this argument. */
7170 if (arg
== error_mark_node
)
7173 /* We are done with all of the arguments. */
7178 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
7179 arg_idx
+= pack_adjust
;
7186 if (PACK_EXPANSION_P (arg
))
7188 /* "If every valid specialization of a variadic template
7189 requires an empty template parameter pack, the template is
7190 ill-formed, no diagnostic required." So check that the
7191 pattern works with this parameter. */
7192 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
7193 tree conv
= convert_template_argument (TREE_VALUE (parm
),
7197 if (conv
== error_mark_node
)
7199 inform (input_location
, "so any instantiation with a "
7200 "non-empty parameter pack would be ill-formed");
7203 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
7204 /* Recover from missing typename. */
7205 TREE_VEC_ELT (inner_args
, arg_idx
)
7206 = make_pack_expansion (conv
);
7208 /* We don't know how many args we have yet, just
7209 use the unconverted ones for now. */
7210 new_inner_args
= inner_args
;
7215 else if (require_all_args
)
7217 /* There must be a default arg in this case. */
7218 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
7220 /* The position of the first default template argument,
7221 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
7223 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
7224 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
7225 arg_idx
- pack_adjust
);
7230 if (arg
== error_mark_node
)
7232 if (complain
& tf_error
)
7233 error ("template argument %d is invalid", arg_idx
+ 1);
7236 /* This only occurs if there was an error in the template
7237 parameter list itself (which we would already have
7238 reported) that we are trying to recover from, e.g., a class
7239 template with a parameter list such as
7240 template<typename..., typename>. */
7243 arg
= convert_template_argument (TREE_VALUE (parm
),
7244 arg
, new_args
, complain
,
7247 if (arg
== error_mark_node
)
7249 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
7251 cp_unevaluated_operand
= saved_unevaluated_operand
;
7252 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
7254 if (variadic_p
&& arg_idx
< nargs
)
7256 if (complain
& tf_error
)
7258 error ("wrong number of template arguments "
7259 "(%d, should be %d)", nargs
, arg_idx
);
7261 error ("provided for %q+D", in_decl
);
7263 return error_mark_node
;
7267 return error_mark_node
;
7269 #ifdef ENABLE_CHECKING
7270 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
7271 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
7272 TREE_VEC_LENGTH (new_inner_args
));
7275 return new_inner_args
;
7278 /* Like coerce_template_parms. If PARMS represents all template
7279 parameters levels, this function returns a vector of vectors
7280 representing all the resulting argument levels. Note that in this
7281 case, only the innermost arguments are coerced because the
7282 outermost ones are supposed to have been coerced already.
7284 Otherwise, if PARMS represents only (the innermost) vector of
7285 parameters, this function returns a vector containing just the
7286 innermost resulting arguments. */
7289 coerce_innermost_template_parms (tree parms
,
7292 tsubst_flags_t complain
,
7293 bool require_all_args
,
7294 bool use_default_args
)
7296 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
7297 int args_depth
= TMPL_ARGS_DEPTH (args
);
7300 if (parms_depth
> 1)
7302 coerced_args
= make_tree_vec (parms_depth
);
7306 for (level
= parms
, cur_depth
= parms_depth
;
7307 parms_depth
> 0 && level
!= NULL_TREE
;
7308 level
= TREE_CHAIN (level
), --cur_depth
)
7311 if (cur_depth
== args_depth
)
7312 l
= coerce_template_parms (TREE_VALUE (level
),
7313 args
, in_decl
, complain
,
7317 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
7319 if (l
== error_mark_node
)
7320 return error_mark_node
;
7322 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
7326 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
7327 args
, in_decl
, complain
,
7330 return coerced_args
;
7333 /* Returns 1 if template args OT and NT are equivalent. */
7336 template_args_equal (tree ot
, tree nt
)
7340 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
7343 if (TREE_CODE (nt
) == TREE_VEC
)
7344 /* For member templates */
7345 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
7346 else if (PACK_EXPANSION_P (ot
))
7347 return (PACK_EXPANSION_P (nt
)
7348 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
7349 PACK_EXPANSION_PATTERN (nt
))
7350 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
7351 PACK_EXPANSION_EXTRA_ARGS (nt
)));
7352 else if (ARGUMENT_PACK_P (ot
))
7357 if (!ARGUMENT_PACK_P (nt
))
7360 opack
= ARGUMENT_PACK_ARGS (ot
);
7361 npack
= ARGUMENT_PACK_ARGS (nt
);
7362 len
= TREE_VEC_LENGTH (opack
);
7363 if (TREE_VEC_LENGTH (npack
) != len
)
7365 for (i
= 0; i
< len
; ++i
)
7366 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
7367 TREE_VEC_ELT (npack
, i
)))
7371 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
7373 /* We get here probably because we are in the middle of substituting
7374 into the pattern of a pack expansion. In that case the
7375 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
7376 interested in. So we want to use the initial pack argument for
7378 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
7379 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
7380 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
7381 return template_args_equal (ot
, nt
);
7383 else if (TYPE_P (nt
))
7387 /* Don't treat an alias template specialization with dependent
7388 arguments as equivalent to its underlying type when used as a
7389 template argument; we need them to be distinct so that we
7390 substitute into the specialization arguments at instantiation
7391 time. And aliases can't be equivalent without being ==, so
7392 we don't need to look any deeper. */
7393 if (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
))
7396 return same_type_p (ot
, nt
);
7398 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
7402 /* Try to treat a template non-type argument that has been converted
7403 to the parameter type as equivalent to one that hasn't yet. */
7404 for (enum tree_code code1
= TREE_CODE (ot
);
7405 CONVERT_EXPR_CODE_P (code1
)
7406 || code1
== NON_LVALUE_EXPR
;
7407 code1
= TREE_CODE (ot
))
7408 ot
= TREE_OPERAND (ot
, 0);
7409 for (enum tree_code code2
= TREE_CODE (nt
);
7410 CONVERT_EXPR_CODE_P (code2
)
7411 || code2
== NON_LVALUE_EXPR
;
7412 code2
= TREE_CODE (nt
))
7413 nt
= TREE_OPERAND (nt
, 0);
7415 return cp_tree_equal (ot
, nt
);
7419 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7420 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7421 NEWARG_PTR with the offending arguments if they are non-NULL. */
7424 comp_template_args_with_info (tree oldargs
, tree newargs
,
7425 tree
*oldarg_ptr
, tree
*newarg_ptr
)
7429 if (oldargs
== newargs
)
7432 if (!oldargs
|| !newargs
)
7435 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
7438 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
7440 tree nt
= TREE_VEC_ELT (newargs
, i
);
7441 tree ot
= TREE_VEC_ELT (oldargs
, i
);
7443 if (! template_args_equal (ot
, nt
))
7445 if (oldarg_ptr
!= NULL
)
7447 if (newarg_ptr
!= NULL
)
7455 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7456 of template arguments. Returns 0 otherwise. */
7459 comp_template_args (tree oldargs
, tree newargs
)
7461 return comp_template_args_with_info (oldargs
, newargs
, NULL
, NULL
);
7465 add_pending_template (tree d
)
7467 tree ti
= (TYPE_P (d
)
7468 ? CLASSTYPE_TEMPLATE_INFO (d
)
7469 : DECL_TEMPLATE_INFO (d
));
7470 struct pending_template
*pt
;
7473 if (TI_PENDING_TEMPLATE_FLAG (ti
))
7476 /* We are called both from instantiate_decl, where we've already had a
7477 tinst_level pushed, and instantiate_template, where we haven't.
7479 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
7482 push_tinst_level (d
);
7484 pt
= ggc_alloc
<pending_template
> ();
7486 pt
->tinst
= current_tinst_level
;
7487 if (last_pending_template
)
7488 last_pending_template
->next
= pt
;
7490 pending_templates
= pt
;
7492 last_pending_template
= pt
;
7494 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
7501 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7502 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7503 documentation for TEMPLATE_ID_EXPR. */
7506 lookup_template_function (tree fns
, tree arglist
)
7510 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
7511 return error_mark_node
;
7513 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
7515 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
7517 error ("%q#D is not a function template", fns
);
7518 return error_mark_node
;
7521 if (BASELINK_P (fns
))
7523 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
7525 BASELINK_FUNCTIONS (fns
),
7530 type
= TREE_TYPE (fns
);
7531 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
7532 type
= unknown_type_node
;
7534 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
7537 /* Within the scope of a template class S<T>, the name S gets bound
7538 (in build_self_reference) to a TYPE_DECL for the class, not a
7539 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7540 or one of its enclosing classes, and that type is a template,
7541 return the associated TEMPLATE_DECL. Otherwise, the original
7544 Also handle the case when DECL is a TREE_LIST of ambiguous
7545 injected-class-names from different bases. */
7548 maybe_get_template_decl_from_type_decl (tree decl
)
7550 if (decl
== NULL_TREE
)
7553 /* DR 176: A lookup that finds an injected-class-name (10.2
7554 [class.member.lookup]) can result in an ambiguity in certain cases
7555 (for example, if it is found in more than one base class). If all of
7556 the injected-class-names that are found refer to specializations of
7557 the same class template, and if the name is followed by a
7558 template-argument-list, the reference refers to the class template
7559 itself and not a specialization thereof, and is not ambiguous. */
7560 if (TREE_CODE (decl
) == TREE_LIST
)
7562 tree t
, tmpl
= NULL_TREE
;
7563 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
7565 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
7568 else if (tmpl
!= elt
)
7571 if (tmpl
&& t
== NULL_TREE
)
7577 return (decl
!= NULL_TREE
7578 && DECL_SELF_REFERENCE_P (decl
)
7579 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
7580 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
7583 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7584 parameters, find the desired type.
7586 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7588 IN_DECL, if non-NULL, is the template declaration we are trying to
7591 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7592 the class we are looking up.
7594 Issue error and warning messages under control of COMPLAIN.
7596 If the template class is really a local class in a template
7597 function, then the FUNCTION_CONTEXT is the function in which it is
7600 ??? Note that this function is currently called *twice* for each
7601 template-id: the first time from the parser, while creating the
7602 incomplete type (finish_template_type), and the second type during the
7603 real instantiation (instantiate_template_class). This is surely something
7604 that we want to avoid. It also causes some problems with argument
7605 coercion (see convert_nontype_argument for more information on this). */
7608 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
7609 int entering_scope
, tsubst_flags_t complain
)
7611 tree templ
= NULL_TREE
, parmlist
;
7618 if (identifier_p (d1
))
7620 tree value
= innermost_non_namespace_value (d1
);
7621 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
7626 push_decl_namespace (context
);
7627 templ
= lookup_name (d1
);
7628 templ
= maybe_get_template_decl_from_type_decl (templ
);
7630 pop_decl_namespace ();
7633 context
= DECL_CONTEXT (templ
);
7635 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
7637 tree type
= TREE_TYPE (d1
);
7639 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7640 an implicit typename for the second A. Deal with it. */
7641 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
7642 type
= TREE_TYPE (type
);
7644 if (CLASSTYPE_TEMPLATE_INFO (type
))
7646 templ
= CLASSTYPE_TI_TEMPLATE (type
);
7647 d1
= DECL_NAME (templ
);
7650 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
7651 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
7653 templ
= TYPE_TI_TEMPLATE (d1
);
7654 d1
= DECL_NAME (templ
);
7656 else if (DECL_TYPE_TEMPLATE_P (d1
))
7659 d1
= DECL_NAME (templ
);
7660 context
= DECL_CONTEXT (templ
);
7662 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
7665 d1
= DECL_NAME (templ
);
7668 /* Issue an error message if we didn't find a template. */
7671 if (complain
& tf_error
)
7672 error ("%qT is not a template", d1
);
7673 return error_mark_node
;
7676 if (TREE_CODE (templ
) != TEMPLATE_DECL
7677 /* Make sure it's a user visible template, if it was named by
7679 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
7680 && !PRIMARY_TEMPLATE_P (templ
)))
7682 if (complain
& tf_error
)
7684 error ("non-template type %qT used as a template", d1
);
7686 error ("for template declaration %q+D", in_decl
);
7688 return error_mark_node
;
7691 complain
&= ~tf_user
;
7693 /* An alias that just changes the name of a template is equivalent to the
7694 other template, so if any of the arguments are pack expansions, strip
7695 the alias to avoid problems with a pack expansion passed to a non-pack
7696 alias template parameter (DR 1430). */
7697 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
7698 templ
= get_underlying_template (templ
);
7700 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
7702 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7703 template arguments */
7709 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7711 /* Consider an example where a template template parameter declared as
7713 template <class T, class U = std::allocator<T> > class TT
7715 The template parameter level of T and U are one level larger than
7716 of TT. To proper process the default argument of U, say when an
7717 instantiation `TT<int>' is seen, we need to build the full
7718 arguments containing {int} as the innermost level. Outer levels,
7719 available when not appearing as default template argument, can be
7720 obtained from the arguments of the enclosing template.
7722 Suppose that TT is later substituted with std::vector. The above
7723 instantiation is `TT<int, std::allocator<T> >' with TT at
7724 level 1, and T at level 2, while the template arguments at level 1
7725 becomes {std::vector} and the inner level 2 is {int}. */
7727 outer
= DECL_CONTEXT (templ
);
7729 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7730 else if (current_template_parms
)
7731 /* This is an argument of the current template, so we haven't set
7732 DECL_CONTEXT yet. */
7733 outer
= current_template_args ();
7736 arglist
= add_to_template_args (outer
, arglist
);
7738 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
7740 /*require_all_args=*/true,
7741 /*use_default_args=*/true);
7742 if (arglist2
== error_mark_node
7743 || (!uses_template_parms (arglist2
)
7744 && check_instantiated_args (templ
, arglist2
, complain
)))
7745 return error_mark_node
;
7747 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
7752 tree template_type
= TREE_TYPE (templ
);
7755 tree found
= NULL_TREE
;
7758 int is_dependent_type
;
7759 int use_partial_inst_tmpl
= false;
7761 if (template_type
== error_mark_node
)
7762 /* An error occurred while building the template TEMPL, and a
7763 diagnostic has most certainly been emitted for that
7764 already. Let's propagate that error. */
7765 return error_mark_node
;
7767 gen_tmpl
= most_general_template (templ
);
7768 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7769 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
7770 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7772 if (arg_depth
== 1 && parm_depth
> 1)
7774 /* We've been given an incomplete set of template arguments.
7777 template <class T> struct S1 {
7778 template <class U> struct S2 {};
7779 template <class U> struct S2<U*> {};
7782 we will be called with an ARGLIST of `U*', but the
7783 TEMPLATE will be `template <class T> template
7784 <class U> struct S1<T>::S2'. We must fill in the missing
7787 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
7789 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7792 /* Now we should have enough arguments. */
7793 gcc_assert (parm_depth
== arg_depth
);
7795 /* From here on, we're only interested in the most general
7798 /* Calculate the BOUND_ARGS. These will be the args that are
7799 actually tsubst'd into the definition to create the
7803 /* We have multiple levels of arguments to coerce, at once. */
7805 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
7807 tree bound_args
= make_tree_vec (parm_depth
);
7809 for (i
= saved_depth
,
7810 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7811 i
> 0 && t
!= NULL_TREE
;
7812 --i
, t
= TREE_CHAIN (t
))
7815 if (i
== saved_depth
)
7816 a
= coerce_template_parms (TREE_VALUE (t
),
7819 /*require_all_args=*/true,
7820 /*use_default_args=*/true);
7822 /* Outer levels should have already been coerced. */
7823 a
= TMPL_ARGS_LEVEL (arglist
, i
);
7825 /* Don't process further if one of the levels fails. */
7826 if (a
== error_mark_node
)
7828 /* Restore the ARGLIST to its full size. */
7829 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7830 return error_mark_node
;
7833 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
7835 /* We temporarily reduce the length of the ARGLIST so
7836 that coerce_template_parms will see only the arguments
7837 corresponding to the template parameters it is
7839 TREE_VEC_LENGTH (arglist
)--;
7842 /* Restore the ARGLIST to its full size. */
7843 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7845 arglist
= bound_args
;
7849 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
7850 INNERMOST_TEMPLATE_ARGS (arglist
),
7853 /*require_all_args=*/true,
7854 /*use_default_args=*/true);
7856 if (arglist
== error_mark_node
)
7857 /* We were unable to bind the arguments. */
7858 return error_mark_node
;
7860 /* In the scope of a template class, explicit references to the
7861 template class refer to the type of the template, not any
7862 instantiation of it. For example, in:
7864 template <class T> class C { void f(C<T>); }
7866 the `C<T>' is just the same as `C'. Outside of the
7867 class, however, such a reference is an instantiation. */
7869 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
7870 || currently_open_class (template_type
))
7871 /* comp_template_args is expensive, check it last. */
7872 && comp_template_args (TYPE_TI_ARGS (template_type
),
7874 return template_type
;
7876 /* If we already have this specialization, return it. */
7877 elt
.tmpl
= gen_tmpl
;
7879 hash
= spec_hasher::hash (&elt
);
7880 entry
= type_specializations
->find_with_hash (&elt
, hash
);
7885 is_dependent_type
= uses_template_parms (arglist
);
7887 /* If the deduced arguments are invalid, then the binding
7889 if (!is_dependent_type
7890 && check_instantiated_args (gen_tmpl
,
7891 INNERMOST_TEMPLATE_ARGS (arglist
),
7893 return error_mark_node
;
7895 if (!is_dependent_type
7896 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
7897 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
7898 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
7900 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
7901 DECL_NAME (gen_tmpl
),
7902 /*tag_scope=*/ts_global
);
7906 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
7908 if (context
== error_mark_node
)
7909 return error_mark_node
;
7912 context
= global_namespace
;
7914 /* Create the type. */
7915 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7917 /* The user referred to a specialization of an alias
7918 template represented by GEN_TMPL.
7920 [temp.alias]/2 says:
7922 When a template-id refers to the specialization of an
7923 alias template, it is equivalent to the associated
7924 type obtained by substitution of its
7925 template-arguments for the template-parameters in the
7926 type-id of the alias template. */
7928 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
7929 /* Note that the call above (by indirectly calling
7930 register_specialization in tsubst_decl) registers the
7931 TYPE_DECL representing the specialization of the alias
7932 template. So next time someone substitutes ARGLIST for
7933 the template parms into the alias template (GEN_TMPL),
7934 she'll get that TYPE_DECL back. */
7936 if (t
== error_mark_node
)
7939 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
7941 if (!is_dependent_type
)
7943 set_current_access_from_decl (TYPE_NAME (template_type
));
7944 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
7945 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
7946 arglist
, complain
, in_decl
),
7947 SCOPED_ENUM_P (template_type
), NULL
);
7949 if (t
== error_mark_node
)
7954 /* We don't want to call start_enum for this type, since
7955 the values for the enumeration constants may involve
7956 template parameters. And, no one should be interested
7957 in the enumeration constants for such a type. */
7958 t
= cxx_make_type (ENUMERAL_TYPE
);
7959 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
7961 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
7962 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
7963 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7965 else if (CLASS_TYPE_P (template_type
))
7967 t
= make_class_type (TREE_CODE (template_type
));
7968 CLASSTYPE_DECLARED_CLASS (t
)
7969 = CLASSTYPE_DECLARED_CLASS (template_type
);
7970 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7971 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7973 /* A local class. Make sure the decl gets registered properly. */
7974 if (context
== current_function_decl
)
7975 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7977 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7978 /* This instantiation is another name for the primary
7979 template type. Set the TYPE_CANONICAL field
7981 TYPE_CANONICAL (t
) = template_type
;
7982 else if (any_template_arguments_need_structural_equality_p (arglist
))
7983 /* Some of the template arguments require structural
7984 equality testing, so this template class requires
7985 structural equality testing. */
7986 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7991 /* If we called start_enum or pushtag above, this information
7992 will already be set up. */
7995 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
7997 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
7998 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
7999 DECL_SOURCE_LOCATION (type_decl
)
8000 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
8003 type_decl
= TYPE_NAME (t
);
8005 if (CLASS_TYPE_P (template_type
))
8007 TREE_PRIVATE (type_decl
)
8008 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
8009 TREE_PROTECTED (type_decl
)
8010 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
8011 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
8013 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
8014 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
8018 if (OVERLOAD_TYPE_P (t
)
8019 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8022 = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (template_type
)))
8024 if (!TREE_CHAIN (attributes
))
8025 TYPE_ATTRIBUTES (t
) = attributes
;
8028 = build_tree_list (TREE_PURPOSE (attributes
),
8029 TREE_VALUE (attributes
));
8033 /* Let's consider the explicit specialization of a member
8034 of a class template specialization that is implicitly instantiated,
8039 template<class U> struct M {}; //#0
8044 struct S<int>::M<char> //#1
8048 [temp.expl.spec]/4 says this is valid.
8050 In this case, when we write:
8053 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8056 When we encounter #1, we want to store the partial instantiation
8057 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8059 For all cases other than this "explicit specialization of member of a
8060 class template", we just want to store the most general template into
8061 the CLASSTYPE_TI_TEMPLATE of M.
8063 This case of "explicit specialization of member of a class template"
8065 1/ the enclosing class is an instantiation of, and therefore not
8066 the same as, the context of the most general template, and
8067 2/ we aren't looking at the partial instantiation itself, i.e.
8068 the innermost arguments are not the same as the innermost parms of
8069 the most general template.
8071 So it's only when 1/ and 2/ happens that we want to use the partial
8072 instantiation of the member template in lieu of its most general
8075 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
8076 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
8077 /* the enclosing class must be an instantiation... */
8078 && CLASS_TYPE_P (context
)
8079 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
8081 tree partial_inst_args
;
8082 TREE_VEC_LENGTH (arglist
)--;
8083 ++processing_template_decl
;
8085 tsubst (INNERMOST_TEMPLATE_ARGS
8086 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
8087 arglist
, complain
, NULL_TREE
);
8088 --processing_template_decl
;
8089 TREE_VEC_LENGTH (arglist
)++;
8090 use_partial_inst_tmpl
=
8091 /*...and we must not be looking at the partial instantiation
8093 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
8097 if (!use_partial_inst_tmpl
)
8098 /* This case is easy; there are no member templates involved. */
8102 /* This is a full instantiation of a member template. Find
8103 the partial instantiation of which this is an instance. */
8105 /* Temporarily reduce by one the number of levels in the ARGLIST
8106 so as to avoid comparing the last set of arguments. */
8107 TREE_VEC_LENGTH (arglist
)--;
8108 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
8109 TREE_VEC_LENGTH (arglist
)++;
8110 /* FOUND is either a proper class type, or an alias
8111 template specialization. In the later case, it's a
8112 TYPE_DECL, resulting from the substituting of arguments
8113 for parameters in the TYPE_DECL of the alias template
8114 done earlier. So be careful while getting the template
8116 found
= TREE_CODE (found
) == TYPE_DECL
8117 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
8118 : CLASSTYPE_TI_TEMPLATE (found
);
8121 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
8124 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
8125 entry
= ggc_alloc
<spec_entry
> ();
8129 /* Note this use of the partial instantiation so we can check it
8130 later in maybe_process_partial_specialization. */
8131 DECL_TEMPLATE_INSTANTIATIONS (found
)
8132 = tree_cons (arglist
, t
,
8133 DECL_TEMPLATE_INSTANTIATIONS (found
));
8135 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
8136 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8137 /* Now that the type has been registered on the instantiations
8138 list, we set up the enumerators. Because the enumeration
8139 constants may involve the enumeration type itself, we make
8140 sure to register the type first, and then create the
8141 constants. That way, doing tsubst_expr for the enumeration
8142 constants won't result in recursive calls here; we'll find
8143 the instantiation and exit above. */
8144 tsubst_enum (template_type
, t
, arglist
);
8146 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
8147 /* If the type makes use of template parameters, the
8148 code that generates debugging information will crash. */
8149 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
8151 /* Possibly limit visibility based on template args. */
8152 TREE_PUBLIC (type_decl
) = 1;
8153 determine_visibility (type_decl
);
8155 inherit_targ_abi_tags (t
);
8161 /* Wrapper for lookup_template_class_1. */
8164 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
8165 int entering_scope
, tsubst_flags_t complain
)
8168 timevar_push (TV_TEMPLATE_INST
);
8169 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
8170 entering_scope
, complain
);
8171 timevar_pop (TV_TEMPLATE_INST
);
8175 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST.
8176 The type of the expression is the unknown_type_node since the
8177 template-id could refer to an explicit or partial specialization. */
8180 lookup_template_variable (tree templ
, tree arglist
)
8182 tree type
= NULL_TREE
;
8183 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
8186 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
8189 finish_template_variable (tree var
, tsubst_flags_t complain
)
8191 tree templ
= TREE_OPERAND (var
, 0);
8193 tree arglist
= TREE_OPERAND (var
, 1);
8194 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
8195 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
8197 tree parms
= DECL_TEMPLATE_PARMS (templ
);
8198 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
8200 /*use_default*/true);
8202 return instantiate_template (templ
, arglist
, complain
);
8209 /* True when we should also visit template parameters that occur in
8210 non-deduced contexts. */
8211 bool include_nondeduced_p
;
8212 hash_set
<tree
> *visited
;
8215 /* Called from for_each_template_parm via walk_tree. */
8218 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
8221 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
8222 tree_fn_t fn
= pfd
->fn
;
8223 void *data
= pfd
->data
;
8226 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
8227 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
8228 pfd
->include_nondeduced_p
))
8229 return error_mark_node
;
8231 switch (TREE_CODE (t
))
8234 if (TYPE_PTRMEMFUNC_P (t
))
8240 if (!TYPE_TEMPLATE_INFO (t
))
8242 else if (for_each_template_parm (TYPE_TI_ARGS (t
),
8243 fn
, data
, pfd
->visited
,
8244 pfd
->include_nondeduced_p
))
8245 return error_mark_node
;
8249 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
8250 fn
, data
, pfd
->visited
,
8251 pfd
->include_nondeduced_p
)
8252 || for_each_template_parm (TYPE_MAX_VALUE (t
),
8253 fn
, data
, pfd
->visited
,
8254 pfd
->include_nondeduced_p
))
8255 return error_mark_node
;
8259 /* Since we're not going to walk subtrees, we have to do this
8261 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
8262 pfd
->visited
, pfd
->include_nondeduced_p
))
8263 return error_mark_node
;
8267 /* Check the return type. */
8268 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
8269 pfd
->include_nondeduced_p
))
8270 return error_mark_node
;
8272 /* Check the parameter types. Since default arguments are not
8273 instantiated until they are needed, the TYPE_ARG_TYPES may
8274 contain expressions that involve template parameters. But,
8275 no-one should be looking at them yet. And, once they're
8276 instantiated, they don't contain template parameters, so
8277 there's no point in looking at them then, either. */
8281 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
8282 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
8283 pfd
->visited
, pfd
->include_nondeduced_p
))
8284 return error_mark_node
;
8286 /* Since we've already handled the TYPE_ARG_TYPES, we don't
8287 want walk_tree walking into them itself. */
8293 case UNDERLYING_TYPE
:
8294 if (pfd
->include_nondeduced_p
8295 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
8297 pfd
->include_nondeduced_p
))
8298 return error_mark_node
;
8303 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
8304 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
8305 pfd
->visited
, pfd
->include_nondeduced_p
))
8306 return error_mark_node
;
8311 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
8312 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
8313 pfd
->visited
, pfd
->include_nondeduced_p
))
8314 return error_mark_node
;
8315 if (DECL_CONTEXT (t
)
8316 && pfd
->include_nondeduced_p
8317 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
8318 pfd
->visited
, pfd
->include_nondeduced_p
))
8319 return error_mark_node
;
8322 case BOUND_TEMPLATE_TEMPLATE_PARM
:
8323 /* Record template parameters such as `T' inside `TT<T>'. */
8324 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
8325 pfd
->include_nondeduced_p
))
8326 return error_mark_node
;
8329 case TEMPLATE_TEMPLATE_PARM
:
8330 case TEMPLATE_TYPE_PARM
:
8331 case TEMPLATE_PARM_INDEX
:
8332 if (fn
&& (*fn
)(t
, data
))
8333 return error_mark_node
;
8335 return error_mark_node
;
8339 /* A template template parameter is encountered. */
8340 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
8341 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
8342 pfd
->include_nondeduced_p
))
8343 return error_mark_node
;
8345 /* Already substituted template template parameter */
8351 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
8353 pfd
->include_nondeduced_p
))
8354 return error_mark_node
;
8358 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
8359 && pfd
->include_nondeduced_p
8360 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
8361 (TREE_TYPE (t
)), fn
, data
,
8362 pfd
->visited
, pfd
->include_nondeduced_p
))
8363 return error_mark_node
;
8368 /* If there's no type, then this thing must be some expression
8369 involving template parameters. */
8370 if (!fn
&& !TREE_TYPE (t
))
8371 return error_mark_node
;
8376 case IMPLICIT_CONV_EXPR
:
8377 case REINTERPRET_CAST_EXPR
:
8378 case CONST_CAST_EXPR
:
8379 case STATIC_CAST_EXPR
:
8380 case DYNAMIC_CAST_EXPR
:
8384 case PSEUDO_DTOR_EXPR
:
8386 return error_mark_node
;
8393 /* We didn't find any template parameters we liked. */
8397 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8398 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8399 call FN with the parameter and the DATA.
8400 If FN returns nonzero, the iteration is terminated, and
8401 for_each_template_parm returns 1. Otherwise, the iteration
8402 continues. If FN never returns a nonzero value, the value
8403 returned by for_each_template_parm is 0. If FN is NULL, it is
8404 considered to be the function which always returns 1.
8406 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8407 parameters that occur in non-deduced contexts. When false, only
8408 visits those template parameters that can be deduced. */
8411 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
8412 hash_set
<tree
> *visited
,
8413 bool include_nondeduced_p
)
8415 struct pair_fn_data pfd
;
8421 pfd
.include_nondeduced_p
= include_nondeduced_p
;
8423 /* Walk the tree. (Conceptually, we would like to walk without
8424 duplicates, but for_each_template_parm_r recursively calls
8425 for_each_template_parm, so we would need to reorganize a fair
8426 bit to use walk_tree_without_duplicates, so we keep our own
8429 pfd
.visited
= visited
;
8431 pfd
.visited
= new hash_set
<tree
>;
8432 result
= cp_walk_tree (&t
,
8433 for_each_template_parm_r
,
8435 pfd
.visited
) != NULL_TREE
;
8447 /* Returns true if T depends on any template parameter. */
8450 uses_template_parms (tree t
)
8456 int saved_processing_template_decl
;
8458 saved_processing_template_decl
= processing_template_decl
;
8459 if (!saved_processing_template_decl
)
8460 processing_template_decl
= 1;
8462 dependent_p
= dependent_type_p (t
);
8463 else if (TREE_CODE (t
) == TREE_VEC
)
8464 dependent_p
= any_dependent_template_arguments_p (t
);
8465 else if (TREE_CODE (t
) == TREE_LIST
)
8466 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
8467 || uses_template_parms (TREE_CHAIN (t
)));
8468 else if (TREE_CODE (t
) == TYPE_DECL
)
8469 dependent_p
= dependent_type_p (TREE_TYPE (t
));
8472 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
8473 || TREE_CODE (t
) == OVERLOAD
8476 || TREE_CODE (t
) == TRAIT_EXPR
8477 || TREE_CODE (t
) == CONSTRUCTOR
8478 || CONSTANT_CLASS_P (t
))
8479 dependent_p
= (type_dependent_expression_p (t
)
8480 || value_dependent_expression_p (t
));
8483 gcc_assert (t
== error_mark_node
);
8484 dependent_p
= false;
8487 processing_template_decl
= saved_processing_template_decl
;
8492 /* Returns true iff current_function_decl is an incompletely instantiated
8493 template. Useful instead of processing_template_decl because the latter
8494 is set to 0 during instantiate_non_dependent_expr. */
8497 in_template_function (void)
8499 tree fn
= current_function_decl
;
8501 ++processing_template_decl
;
8502 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
8503 && DECL_TEMPLATE_INFO (fn
)
8504 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
8505 --processing_template_decl
;
8509 /* Returns true if T depends on any template parameter with level LEVEL. */
8512 uses_template_parms_level (tree t
, int level
)
8514 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
8515 /*include_nondeduced_p=*/true);
8518 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8519 ill-formed translation unit, i.e. a variable or function that isn't
8520 usable in a constant expression. */
8523 neglectable_inst_p (tree d
)
8526 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
8527 : decl_maybe_constant_var_p (d
)));
8530 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8531 neglectable and instantiated from within an erroneous instantiation. */
8534 limit_bad_template_recursion (tree decl
)
8536 struct tinst_level
*lev
= current_tinst_level
;
8537 int errs
= errorcount
+ sorrycount
;
8538 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
8541 for (; lev
; lev
= lev
->next
)
8542 if (neglectable_inst_p (lev
->decl
))
8545 return (lev
&& errs
> lev
->errors
);
8548 static int tinst_depth
;
8549 extern int max_tinst_depth
;
8552 static GTY(()) struct tinst_level
*last_error_tinst_level
;
8554 /* We're starting to instantiate D; record the template instantiation context
8555 for diagnostics and to restore it later. */
8558 push_tinst_level (tree d
)
8560 return push_tinst_level_loc (d
, input_location
);
8563 /* We're starting to instantiate D; record the template instantiation context
8564 at LOC for diagnostics and to restore it later. */
8567 push_tinst_level_loc (tree d
, location_t loc
)
8569 struct tinst_level
*new_level
;
8571 if (tinst_depth
>= max_tinst_depth
)
8573 fatal_error (input_location
,
8574 "template instantiation depth exceeds maximum of %d"
8575 " (use -ftemplate-depth= to increase the maximum)",
8580 /* If the current instantiation caused problems, don't let it instantiate
8581 anything else. Do allow deduction substitution and decls usable in
8582 constant expressions. */
8583 if (limit_bad_template_recursion (d
))
8586 new_level
= ggc_alloc
<tinst_level
> ();
8587 new_level
->decl
= d
;
8588 new_level
->locus
= loc
;
8589 new_level
->errors
= errorcount
+sorrycount
;
8590 new_level
->in_system_header_p
= in_system_header_at (input_location
);
8591 new_level
->next
= current_tinst_level
;
8592 current_tinst_level
= new_level
;
8595 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
8596 depth_reached
= tinst_depth
;
8601 /* We're done instantiating this template; return to the instantiation
8605 pop_tinst_level (void)
8607 /* Restore the filename and line number stashed away when we started
8608 this instantiation. */
8609 input_location
= current_tinst_level
->locus
;
8610 current_tinst_level
= current_tinst_level
->next
;
8614 /* We're instantiating a deferred template; restore the template
8615 instantiation context in which the instantiation was requested, which
8616 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8619 reopen_tinst_level (struct tinst_level
*level
)
8621 struct tinst_level
*t
;
8624 for (t
= level
; t
; t
= t
->next
)
8627 current_tinst_level
= level
;
8629 if (current_tinst_level
)
8630 current_tinst_level
->errors
= errorcount
+sorrycount
;
8634 /* Returns the TINST_LEVEL which gives the original instantiation
8637 struct tinst_level
*
8638 outermost_tinst_level (void)
8640 struct tinst_level
*level
= current_tinst_level
;
8643 level
= level
->next
;
8647 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8648 vector of template arguments, as for tsubst.
8650 Returns an appropriate tsubst'd friend declaration. */
8653 tsubst_friend_function (tree decl
, tree args
)
8657 if (TREE_CODE (decl
) == FUNCTION_DECL
8658 && DECL_TEMPLATE_INSTANTIATION (decl
)
8659 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
8660 /* This was a friend declared with an explicit template
8661 argument list, e.g.:
8665 to indicate that f was a template instantiation, not a new
8666 function declaration. Now, we have to figure out what
8667 instantiation of what template. */
8669 tree template_id
, arglist
, fns
;
8672 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
8674 /* Friend functions are looked up in the containing namespace scope.
8675 We must enter that scope, to avoid finding member functions of the
8676 current class with same name. */
8677 push_nested_namespace (ns
);
8678 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
8679 tf_warning_or_error
, NULL_TREE
,
8680 /*integral_constant_expression_p=*/false);
8681 pop_nested_namespace (ns
);
8682 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
8683 tf_warning_or_error
, NULL_TREE
);
8684 template_id
= lookup_template_function (fns
, arglist
);
8686 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8687 tmpl
= determine_specialization (template_id
, new_friend
,
8689 /*need_member_template=*/0,
8690 TREE_VEC_LENGTH (args
),
8692 return instantiate_template (tmpl
, new_args
, tf_error
);
8695 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8697 /* The NEW_FRIEND will look like an instantiation, to the
8698 compiler, but is not an instantiation from the point of view of
8699 the language. For example, we might have had:
8701 template <class T> struct S {
8702 template <class U> friend void f(T, U);
8705 Then, in S<int>, template <class U> void f(int, U) is not an
8706 instantiation of anything. */
8707 if (new_friend
== error_mark_node
)
8708 return error_mark_node
;
8710 DECL_USE_TEMPLATE (new_friend
) = 0;
8711 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8713 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
8714 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
8715 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
8718 /* The mangled name for the NEW_FRIEND is incorrect. The function
8719 is not a template instantiation and should not be mangled like
8720 one. Therefore, we forget the mangling here; we'll recompute it
8721 later if we need it. */
8722 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
8724 SET_DECL_RTL (new_friend
, NULL
);
8725 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
8728 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
8731 tree new_friend_template_info
;
8732 tree new_friend_result_template_info
;
8734 int new_friend_is_defn
;
8736 /* We must save some information from NEW_FRIEND before calling
8737 duplicate decls since that function will free NEW_FRIEND if
8739 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
8740 new_friend_is_defn
=
8741 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8742 (template_for_substitution (new_friend
)))
8744 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
8746 /* This declaration is a `primary' template. */
8747 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
8749 new_friend_result_template_info
8750 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
8753 new_friend_result_template_info
= NULL_TREE
;
8755 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8756 if (new_friend_is_defn
)
8757 DECL_INITIAL (new_friend
) = error_mark_node
;
8759 /* Inside pushdecl_namespace_level, we will push into the
8760 current namespace. However, the friend function should go
8761 into the namespace of the template. */
8762 ns
= decl_namespace_context (new_friend
);
8763 push_nested_namespace (ns
);
8764 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
8765 pop_nested_namespace (ns
);
8767 if (old_decl
== error_mark_node
)
8768 return error_mark_node
;
8770 if (old_decl
!= new_friend
)
8772 /* This new friend declaration matched an existing
8773 declaration. For example, given:
8775 template <class T> void f(T);
8776 template <class U> class C {
8777 template <class T> friend void f(T) {}
8780 the friend declaration actually provides the definition
8781 of `f', once C has been instantiated for some type. So,
8782 old_decl will be the out-of-class template declaration,
8783 while new_friend is the in-class definition.
8785 But, if `f' was called before this point, the
8786 instantiation of `f' will have DECL_TI_ARGS corresponding
8787 to `T' but not to `U', references to which might appear
8788 in the definition of `f'. Previously, the most general
8789 template for an instantiation of `f' was the out-of-class
8790 version; now it is the in-class version. Therefore, we
8791 run through all specialization of `f', adding to their
8792 DECL_TI_ARGS appropriately. In particular, they need a
8793 new set of outer arguments, corresponding to the
8794 arguments for this class instantiation.
8796 The same situation can arise with something like this:
8799 template <class T> class C {
8803 when `C<int>' is instantiated. Now, `f(int)' is defined
8806 if (!new_friend_is_defn
)
8807 /* On the other hand, if the in-class declaration does
8808 *not* provide a definition, then we don't want to alter
8809 existing definitions. We can just leave everything
8814 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
8815 tree new_args
= TI_ARGS (new_friend_template_info
);
8817 /* Overwrite whatever template info was there before, if
8818 any, with the new template information pertaining to
8820 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
8822 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
8824 /* We should have called reregister_specialization in
8826 gcc_assert (retrieve_specialization (new_template
,
8830 /* Instantiate it if the global has already been used. */
8831 if (DECL_ODR_USED (old_decl
))
8832 instantiate_decl (old_decl
, /*defer_ok=*/true,
8833 /*expl_inst_class_mem_p=*/false);
8839 /* Indicate that the old function template is a partial
8841 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
8842 = new_friend_result_template_info
;
8844 gcc_assert (new_template
8845 == most_general_template (new_template
));
8846 gcc_assert (new_template
!= old_decl
);
8848 /* Reassign any specializations already in the hash table
8849 to the new more general template, and add the
8850 additional template args. */
8851 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
8855 tree spec
= TREE_VALUE (t
);
8858 elt
.tmpl
= old_decl
;
8859 elt
.args
= DECL_TI_ARGS (spec
);
8860 elt
.spec
= NULL_TREE
;
8862 decl_specializations
->remove_elt (&elt
);
8865 = add_outermost_template_args (new_args
,
8866 DECL_TI_ARGS (spec
));
8868 register_specialization
8869 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
8872 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
8876 /* The information from NEW_FRIEND has been merged into OLD_DECL
8877 by duplicate_decls. */
8878 new_friend
= old_decl
;
8883 tree context
= DECL_CONTEXT (new_friend
);
8887 template <class T> class C {
8888 template <class U> friend void C1<U>::f (); // case 1
8889 friend void C2<T>::f (); // case 2
8891 we only need to make sure CONTEXT is a complete type for
8892 case 2. To distinguish between the two cases, we note that
8893 CONTEXT of case 1 remains dependent type after tsubst while
8894 this isn't true for case 2. */
8895 ++processing_template_decl
;
8896 dependent_p
= dependent_type_p (context
);
8897 --processing_template_decl
;
8900 && !complete_type_or_else (context
, NULL_TREE
))
8901 return error_mark_node
;
8903 if (COMPLETE_TYPE_P (context
))
8905 tree fn
= new_friend
;
8906 /* do_friend adds the TEMPLATE_DECL for any member friend
8907 template even if it isn't a member template, i.e.
8908 template <class T> friend A<T>::f();
8909 Look through it in that case. */
8910 if (TREE_CODE (fn
) == TEMPLATE_DECL
8911 && !PRIMARY_TEMPLATE_P (fn
))
8912 fn
= DECL_TEMPLATE_RESULT (fn
);
8913 /* Check to see that the declaration is really present, and,
8914 possibly obtain an improved declaration. */
8915 fn
= check_classfn (context
, fn
, NULL_TREE
);
8925 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8926 template arguments, as for tsubst.
8928 Returns an appropriate tsubst'd friend type or error_mark_node on
8932 tsubst_friend_class (tree friend_tmpl
, tree args
)
8938 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
8940 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
8941 return TREE_TYPE (t
);
8944 context
= CP_DECL_CONTEXT (friend_tmpl
);
8946 if (context
!= global_namespace
)
8948 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8949 push_nested_namespace (context
);
8951 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
8954 /* Look for a class template declaration. We look for hidden names
8955 because two friend declarations of the same template are the
8956 same. For example, in:
8959 template <typename> friend class F;
8961 template <typename> struct B {
8962 template <typename> friend class F;
8965 both F templates are the same. */
8966 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
8967 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
8969 /* But, if we don't find one, it might be because we're in a
8970 situation like this:
8978 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8979 for `S<int>', not the TEMPLATE_DECL. */
8980 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
8982 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
8983 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
8986 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
8988 /* The friend template has already been declared. Just
8989 check to see that the declarations match, and install any new
8990 default parameters. We must tsubst the default parameters,
8991 of course. We only need the innermost template parameters
8992 because that is all that redeclare_class_template will look
8994 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
8995 > TMPL_ARGS_DEPTH (args
))
8998 location_t saved_input_location
;
8999 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
9000 args
, tf_warning_or_error
);
9002 saved_input_location
= input_location
;
9003 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
9004 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
9005 input_location
= saved_input_location
;
9009 friend_type
= TREE_TYPE (tmpl
);
9013 /* The friend template has not already been declared. In this
9014 case, the instantiation of the template class will cause the
9015 injection of this template into the global scope. */
9016 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
9017 if (tmpl
== error_mark_node
)
9018 return error_mark_node
;
9020 /* The new TMPL is not an instantiation of anything, so we
9021 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
9022 the new type because that is supposed to be the corresponding
9023 template decl, i.e., TMPL. */
9024 DECL_USE_TEMPLATE (tmpl
) = 0;
9025 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
9026 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
9027 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
9028 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
9030 /* Inject this template into the global scope. */
9031 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
9034 if (context
!= global_namespace
)
9036 if (TREE_CODE (context
) == NAMESPACE_DECL
)
9037 pop_nested_namespace (context
);
9039 pop_nested_class ();
9045 /* Returns zero if TYPE cannot be completed later due to circularity.
9046 Otherwise returns one. */
9049 can_complete_type_without_circularity (tree type
)
9051 if (type
== NULL_TREE
|| type
== error_mark_node
)
9053 else if (COMPLETE_TYPE_P (type
))
9055 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
9056 return can_complete_type_without_circularity (TREE_TYPE (type
));
9057 else if (CLASS_TYPE_P (type
)
9058 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
9064 static tree
tsubst_omp_clauses (tree
, bool, tree
, tsubst_flags_t
, tree
);
9066 /* Apply any attributes which had to be deferred until instantiation
9067 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
9068 ARGS, COMPLAIN, IN_DECL are as tsubst. */
9071 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
9072 tree args
, tsubst_flags_t complain
, tree in_decl
)
9074 tree last_dep
= NULL_TREE
;
9078 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
9079 if (ATTR_IS_DEPENDENT (t
))
9082 attributes
= copy_list (attributes
);
9086 if (DECL_P (*decl_p
))
9088 if (TREE_TYPE (*decl_p
) == error_mark_node
)
9090 p
= &DECL_ATTRIBUTES (*decl_p
);
9093 p
= &TYPE_ATTRIBUTES (*decl_p
);
9097 tree late_attrs
= NULL_TREE
;
9098 tree
*q
= &late_attrs
;
9100 for (*p
= attributes
; *p
; )
9103 if (ATTR_IS_DEPENDENT (t
))
9105 *p
= TREE_CHAIN (t
);
9106 TREE_CHAIN (t
) = NULL_TREE
;
9107 if ((flag_openmp
|| flag_openmp_simd
|| flag_cilkplus
)
9108 && is_attribute_p ("omp declare simd",
9109 get_attribute_name (t
))
9112 tree clauses
= TREE_VALUE (TREE_VALUE (t
));
9113 clauses
= tsubst_omp_clauses (clauses
, true, args
,
9115 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
9116 clauses
= finish_omp_clauses (clauses
);
9117 tree parms
= DECL_ARGUMENTS (*decl_p
);
9119 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
9121 TREE_VALUE (TREE_VALUE (t
)) = clauses
;
9123 TREE_VALUE (t
) = NULL_TREE
;
9125 /* If the first attribute argument is an identifier, don't
9126 pass it through tsubst. Attributes like mode, format,
9127 cleanup and several target specific attributes expect it
9129 else if (attribute_takes_identifier_p (get_attribute_name (t
))
9133 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
9135 /*integral_constant_expression_p=*/false);
9136 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
9138 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
9141 else if (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
)))
9143 /* An attribute pack expansion. */
9144 tree purp
= TREE_PURPOSE (t
);
9145 tree pack
= (tsubst_pack_expansion
9146 (TREE_VALUE (t
), args
, complain
, in_decl
));
9147 int len
= TREE_VEC_LENGTH (pack
);
9148 for (int i
= 0; i
< len
; ++i
)
9150 tree elt
= TREE_VEC_ELT (pack
, i
);
9151 *q
= build_tree_list (purp
, elt
);
9152 q
= &TREE_CHAIN (*q
);
9158 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
9159 /*integral_constant_expression_p=*/false);
9161 q
= &TREE_CHAIN (t
);
9164 p
= &TREE_CHAIN (t
);
9167 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
9171 /* Perform (or defer) access check for typedefs that were referenced
9172 from within the template TMPL code.
9173 This is a subroutine of instantiate_decl and instantiate_class_template.
9174 TMPL is the template to consider and TARGS is the list of arguments of
9178 perform_typedefs_access_check (tree tmpl
, tree targs
)
9180 location_t saved_location
;
9182 qualified_typedef_usage_t
*iter
;
9185 || (!CLASS_TYPE_P (tmpl
)
9186 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
9189 saved_location
= input_location
;
9190 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
9192 tree type_decl
= iter
->typedef_decl
;
9193 tree type_scope
= iter
->context
;
9195 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
9198 if (uses_template_parms (type_decl
))
9199 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
9200 if (uses_template_parms (type_scope
))
9201 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
9203 /* Make access check error messages point to the location
9204 of the use of the typedef. */
9205 input_location
= iter
->locus
;
9206 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
9207 type_decl
, type_decl
,
9208 tf_warning_or_error
);
9210 input_location
= saved_location
;
9214 instantiate_class_template_1 (tree type
)
9216 tree templ
, args
, pattern
, t
, member
;
9220 unsigned int saved_maximum_field_alignment
;
9223 if (type
== error_mark_node
)
9224 return error_mark_node
;
9226 if (COMPLETE_OR_OPEN_TYPE_P (type
)
9227 || uses_template_parms (type
))
9230 /* Figure out which template is being instantiated. */
9231 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
9232 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
9234 /* Determine what specialization of the original template to
9236 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
9237 if (t
== error_mark_node
)
9239 TYPE_BEING_DEFINED (type
) = 1;
9240 return error_mark_node
;
9244 /* This TYPE is actually an instantiation of a partial
9245 specialization. We replace the innermost set of ARGS with
9246 the arguments appropriate for substitution. For example,
9249 template <class T> struct S {};
9250 template <class T> struct S<T*> {};
9252 and supposing that we are instantiating S<int*>, ARGS will
9253 presently be {int*} -- but we need {int}. */
9254 pattern
= TREE_TYPE (t
);
9255 args
= TREE_PURPOSE (t
);
9259 pattern
= TREE_TYPE (templ
);
9260 args
= CLASSTYPE_TI_ARGS (type
);
9263 /* If the template we're instantiating is incomplete, then clearly
9264 there's nothing we can do. */
9265 if (!COMPLETE_TYPE_P (pattern
))
9268 /* If we've recursively instantiated too many templates, stop. */
9269 if (! push_tinst_level (type
))
9272 /* Now we're really doing the instantiation. Mark the type as in
9273 the process of being defined. */
9274 TYPE_BEING_DEFINED (type
) = 1;
9276 /* We may be in the middle of deferred access check. Disable
9278 push_deferring_access_checks (dk_no_deferred
);
9280 int saved_unevaluated_operand
= cp_unevaluated_operand
;
9281 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9283 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
9284 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
9285 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
9286 fn_context
= error_mark_node
;
9288 push_to_top_level ();
9291 cp_unevaluated_operand
= 0;
9292 c_inhibit_evaluation_warnings
= 0;
9294 /* Use #pragma pack from the template context. */
9295 saved_maximum_field_alignment
= maximum_field_alignment
;
9296 maximum_field_alignment
= TYPE_PRECISION (pattern
);
9298 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
9300 /* Set the input location to the most specialized template definition.
9301 This is needed if tsubsting causes an error. */
9302 typedecl
= TYPE_MAIN_DECL (pattern
);
9303 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
9304 DECL_SOURCE_LOCATION (typedecl
);
9306 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
9307 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
9308 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
9309 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
9310 if (ANON_AGGR_TYPE_P (pattern
))
9311 SET_ANON_AGGR_TYPE_P (type
);
9312 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
9314 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
9315 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
9316 /* Adjust visibility for template arguments. */
9317 determine_visibility (TYPE_MAIN_DECL (type
));
9319 if (CLASS_TYPE_P (type
))
9320 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
9322 pbinfo
= TYPE_BINFO (pattern
);
9324 /* We should never instantiate a nested class before its enclosing
9325 class; we need to look up the nested class by name before we can
9326 instantiate it, and that lookup should instantiate the enclosing
9328 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
9329 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
9331 base_list
= NULL_TREE
;
9332 if (BINFO_N_BASE_BINFOS (pbinfo
))
9338 /* We must enter the scope containing the type, as that is where
9339 the accessibility of types named in dependent bases are
9341 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
9343 /* Substitute into each of the bases to determine the actual
9345 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
9348 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
9349 tree expanded_bases
= NULL_TREE
;
9352 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
9355 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
9356 args
, tf_error
, NULL_TREE
);
9357 if (expanded_bases
== error_mark_node
)
9360 len
= TREE_VEC_LENGTH (expanded_bases
);
9363 for (idx
= 0; idx
< len
; idx
++)
9366 /* Extract the already-expanded base class. */
9367 base
= TREE_VEC_ELT (expanded_bases
, idx
);
9369 /* Substitute to figure out the base class. */
9370 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
9373 if (base
== error_mark_node
)
9376 base_list
= tree_cons (access
, base
, base_list
);
9377 if (BINFO_VIRTUAL_P (pbase_binfo
))
9378 TREE_TYPE (base_list
) = integer_type_node
;
9382 /* The list is now in reverse order; correct that. */
9383 base_list
= nreverse (base_list
);
9386 pop_scope (pushed_scope
);
9388 /* Now call xref_basetypes to set up all the base-class
9390 xref_basetypes (type
, base_list
);
9392 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
9393 (int) ATTR_FLAG_TYPE_IN_PLACE
,
9394 args
, tf_error
, NULL_TREE
);
9395 fixup_attribute_variants (type
);
9397 /* Now that our base classes are set up, enter the scope of the
9398 class, so that name lookups into base classes, etc. will work
9399 correctly. This is precisely analogous to what we do in
9400 begin_class_definition when defining an ordinary non-template
9401 class, except we also need to push the enclosing classes. */
9402 push_nested_class (type
);
9404 /* Now members are processed in the order of declaration. */
9405 for (member
= CLASSTYPE_DECL_LIST (pattern
);
9406 member
; member
= TREE_CHAIN (member
))
9408 tree t
= TREE_VALUE (member
);
9410 if (TREE_PURPOSE (member
))
9414 /* Build new CLASSTYPE_NESTED_UTDS. */
9417 bool class_template_p
;
9419 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
9420 && TYPE_LANG_SPECIFIC (t
)
9421 && CLASSTYPE_IS_TEMPLATE (t
));
9422 /* If the member is a class template, then -- even after
9423 substitution -- there may be dependent types in the
9424 template argument list for the class. We increment
9425 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
9426 that function will assume that no types are dependent
9427 when outside of a template. */
9428 if (class_template_p
)
9429 ++processing_template_decl
;
9430 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
9431 if (class_template_p
)
9432 --processing_template_decl
;
9433 if (newtag
== error_mark_node
)
9436 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
9438 tree name
= TYPE_IDENTIFIER (t
);
9440 if (class_template_p
)
9441 /* Unfortunately, lookup_template_class sets
9442 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
9443 instantiation (i.e., for the type of a member
9444 template class nested within a template class.)
9445 This behavior is required for
9446 maybe_process_partial_specialization to work
9447 correctly, but is not accurate in this case;
9448 the TAG is not an instantiation of anything.
9449 (The corresponding TEMPLATE_DECL is an
9450 instantiation, but the TYPE is not.) */
9451 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
9453 /* Now, we call pushtag to put this NEWTAG into the scope of
9454 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
9455 pushtag calling push_template_decl. We don't have to do
9456 this for enums because it will already have been done in
9459 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
9460 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
9463 else if (DECL_DECLARES_FUNCTION_P (t
))
9465 /* Build new TYPE_METHODS. */
9468 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9469 ++processing_template_decl
;
9470 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
9471 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9472 --processing_template_decl
;
9473 set_current_access_from_decl (r
);
9474 finish_member_declaration (r
);
9475 /* Instantiate members marked with attribute used. */
9476 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
9478 if (TREE_CODE (r
) == FUNCTION_DECL
9479 && DECL_OMP_DECLARE_REDUCTION_P (r
))
9480 cp_check_omp_declare_reduction (r
);
9482 else if (DECL_CLASS_TEMPLATE_P (t
)
9483 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
9484 /* A closure type for a lambda in a default argument for a
9485 member template. Ignore it; it will be instantiated with
9486 the default argument. */;
9489 /* Build new TYPE_FIELDS. */
9490 if (TREE_CODE (t
) == STATIC_ASSERT
)
9494 ++c_inhibit_evaluation_warnings
;
9496 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
9497 tf_warning_or_error
, NULL_TREE
,
9498 /*integral_constant_expression_p=*/true);
9499 --c_inhibit_evaluation_warnings
;
9501 finish_static_assert (condition
,
9502 STATIC_ASSERT_MESSAGE (t
),
9503 STATIC_ASSERT_SOURCE_LOCATION (t
),
9506 else if (TREE_CODE (t
) != CONST_DECL
)
9509 tree vec
= NULL_TREE
;
9512 /* The file and line for this declaration, to
9513 assist in error message reporting. Since we
9514 called push_tinst_level above, we don't need to
9516 input_location
= DECL_SOURCE_LOCATION (t
);
9518 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9519 ++processing_template_decl
;
9520 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
9521 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9522 --processing_template_decl
;
9524 if (TREE_CODE (r
) == TREE_VEC
)
9526 /* A capture pack became multiple fields. */
9528 len
= TREE_VEC_LENGTH (vec
);
9531 for (int i
= 0; i
< len
; ++i
)
9534 r
= TREE_VEC_ELT (vec
, i
);
9539 [t]he initialization (and any associated
9540 side-effects) of a static data member does
9541 not occur unless the static data member is
9542 itself used in a way that requires the
9543 definition of the static data member to
9546 Therefore, we do not substitute into the
9547 initialized for the static data member here. */
9548 finish_static_data_member_decl
9551 /*init_const_expr_p=*/false,
9552 /*asmspec_tree=*/NULL_TREE
,
9554 /* Instantiate members marked with attribute used. */
9555 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
9558 else if (TREE_CODE (r
) == FIELD_DECL
)
9560 /* Determine whether R has a valid type and can be
9561 completed later. If R is invalid, then its type
9562 is replaced by error_mark_node. */
9563 tree rtype
= TREE_TYPE (r
);
9564 if (can_complete_type_without_circularity (rtype
))
9565 complete_type (rtype
);
9567 if (!COMPLETE_TYPE_P (rtype
))
9569 cxx_incomplete_type_error (r
, rtype
);
9570 TREE_TYPE (r
) = error_mark_node
;
9574 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9575 such a thing will already have been added to the field
9576 list by tsubst_enum in finish_member_declaration in the
9577 CLASSTYPE_NESTED_UTDS case above. */
9578 if (!(TREE_CODE (r
) == TYPE_DECL
9579 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
9580 && DECL_ARTIFICIAL (r
)))
9582 set_current_access_from_decl (r
);
9583 finish_member_declaration (r
);
9591 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
9592 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9594 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9596 tree friend_type
= t
;
9597 bool adjust_processing_template_decl
= false;
9599 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9601 /* template <class T> friend class C; */
9602 friend_type
= tsubst_friend_class (friend_type
, args
);
9603 adjust_processing_template_decl
= true;
9605 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
9607 /* template <class T> friend class C::D; */
9608 friend_type
= tsubst (friend_type
, args
,
9609 tf_warning_or_error
, NULL_TREE
);
9610 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9611 friend_type
= TREE_TYPE (friend_type
);
9612 adjust_processing_template_decl
= true;
9614 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
9615 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
9617 /* This could be either
9621 when dependent_type_p is false or
9623 template <class U> friend class T::C;
9626 friend_type
= tsubst (friend_type
, args
,
9627 tf_warning_or_error
, NULL_TREE
);
9628 /* Bump processing_template_decl for correct
9629 dependent_type_p calculation. */
9630 ++processing_template_decl
;
9631 if (dependent_type_p (friend_type
))
9632 adjust_processing_template_decl
= true;
9633 --processing_template_decl
;
9635 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
9636 && hidden_name_p (TYPE_NAME (friend_type
)))
9640 where C hasn't been declared yet. Let's lookup name
9641 from namespace scope directly, bypassing any name that
9642 come from dependent base class. */
9643 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
9645 /* The call to xref_tag_from_type does injection for friend
9647 push_nested_namespace (ns
);
9649 xref_tag_from_type (friend_type
, NULL_TREE
,
9650 /*tag_scope=*/ts_current
);
9651 pop_nested_namespace (ns
);
9653 else if (uses_template_parms (friend_type
))
9654 /* friend class C<T>; */
9655 friend_type
= tsubst (friend_type
, args
,
9656 tf_warning_or_error
, NULL_TREE
);
9661 where C is already declared or
9663 friend class C<int>;
9665 We don't have to do anything in these cases. */
9667 if (adjust_processing_template_decl
)
9668 /* Trick make_friend_class into realizing that the friend
9669 we're adding is a template, not an ordinary class. It's
9670 important that we use make_friend_class since it will
9671 perform some error-checking and output cross-reference
9673 ++processing_template_decl
;
9675 if (friend_type
!= error_mark_node
)
9676 make_friend_class (type
, friend_type
, /*complain=*/false);
9678 if (adjust_processing_template_decl
)
9679 --processing_template_decl
;
9683 /* Build new DECL_FRIENDLIST. */
9686 /* The file and line for this declaration, to
9687 assist in error message reporting. Since we
9688 called push_tinst_level above, we don't need to
9690 input_location
= DECL_SOURCE_LOCATION (t
);
9692 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9694 ++processing_template_decl
;
9695 push_deferring_access_checks (dk_no_check
);
9698 r
= tsubst_friend_function (t
, args
);
9699 add_friend (type
, r
, /*complain=*/false);
9700 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9702 pop_deferring_access_checks ();
9703 --processing_template_decl
;
9711 /* Restore these before substituting into the lambda capture
9713 cp_unevaluated_operand
= saved_unevaluated_operand
;
9714 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
9717 if (tree expr
= CLASSTYPE_LAMBDA_EXPR (type
))
9719 tree decl
= lambda_function (type
);
9722 if (!DECL_TEMPLATE_INFO (decl
)
9723 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl
)) != decl
)
9724 instantiate_decl (decl
, false, false);
9726 /* We need to instantiate the capture list from the template
9727 after we've instantiated the closure members, but before we
9728 consider adding the conversion op. Also keep any captures
9729 that may have been added during instantiation of the op(). */
9730 tree tmpl_expr
= CLASSTYPE_LAMBDA_EXPR (pattern
);
9732 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr
),
9733 args
, tf_warning_or_error
, NULL_TREE
,
9736 LAMBDA_EXPR_CAPTURE_LIST (expr
)
9737 = chainon (tmpl_cap
, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr
)));
9739 maybe_add_lambda_conv_op (type
);
9742 gcc_assert (errorcount
);
9745 /* Set the file and line number information to whatever is given for
9746 the class itself. This puts error messages involving generated
9747 implicit functions at a predictable point, and the same point
9748 that would be used for non-template classes. */
9749 input_location
= DECL_SOURCE_LOCATION (typedecl
);
9751 unreverse_member_declarations (type
);
9752 finish_struct_1 (type
);
9753 TYPE_BEING_DEFINED (type
) = 0;
9755 /* We don't instantiate default arguments for member functions. 14.7.1:
9757 The implicit instantiation of a class template specialization causes
9758 the implicit instantiation of the declarations, but not of the
9759 definitions or default arguments, of the class member functions,
9760 member classes, static data members and member templates.... */
9762 /* Some typedefs referenced from within the template code need to be access
9763 checked at template instantiation time, i.e now. These types were
9764 added to the template at parsing time. Let's get those and perform
9765 the access checks then. */
9766 perform_typedefs_access_check (pattern
, args
);
9767 perform_deferred_access_checks (tf_warning_or_error
);
9768 pop_nested_class ();
9769 maximum_field_alignment
= saved_maximum_field_alignment
;
9771 pop_from_top_level ();
9772 pop_deferring_access_checks ();
9775 /* The vtable for a template class can be emitted in any translation
9776 unit in which the class is instantiated. When there is no key
9777 method, however, finish_struct_1 will already have added TYPE to
9778 the keyed_classes list. */
9779 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
9780 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
9785 /* Wrapper for instantiate_class_template_1. */
9788 instantiate_class_template (tree type
)
9791 timevar_push (TV_TEMPLATE_INST
);
9792 ret
= instantiate_class_template_1 (type
);
9793 timevar_pop (TV_TEMPLATE_INST
);
9798 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9804 else if (TYPE_P (t
))
9805 r
= tsubst (t
, args
, complain
, in_decl
);
9808 if (!(complain
& tf_warning
))
9809 ++c_inhibit_evaluation_warnings
;
9810 r
= tsubst_expr (t
, args
, complain
, in_decl
,
9811 /*integral_constant_expression_p=*/true);
9812 if (!(complain
& tf_warning
))
9813 --c_inhibit_evaluation_warnings
;
9818 /* Given a function parameter pack TMPL_PARM and some function parameters
9819 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9820 and set *SPEC_P to point at the next point in the list. */
9823 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
9825 /* Collect all of the extra "packed" parameters into an
9829 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
9830 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
9831 tree spec_parm
= *spec_p
;
9834 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
9836 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
9839 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9840 parmvec
= make_tree_vec (len
);
9841 parmtypevec
= make_tree_vec (len
);
9842 spec_parm
= *spec_p
;
9843 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
9845 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
9846 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
9849 /* Build the argument packs. */
9850 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
9851 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
9852 TREE_TYPE (argpack
) = argtypepack
;
9853 *spec_p
= spec_parm
;
9858 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9859 NONTYPE_ARGUMENT_PACK. */
9862 make_fnparm_pack (tree spec_parm
)
9864 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
9867 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
9868 pack expansion with no extra args, 2 if it has extra args, or 0
9869 if it is not a pack expansion. */
9872 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
9874 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
9875 if (i
>= TREE_VEC_LENGTH (vec
))
9877 tree elt
= TREE_VEC_ELT (vec
, i
);
9878 if (!PACK_EXPANSION_P (elt
))
9880 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
9886 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9889 make_argument_pack_select (tree arg_pack
, unsigned index
)
9891 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
9893 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
9894 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9899 /* This is a subroutine of tsubst_pack_expansion.
9901 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9902 mechanism to store the (non complete list of) arguments of the
9903 substitution and return a non substituted pack expansion, in order
9904 to wait for when we have enough arguments to really perform the
9908 use_pack_expansion_extra_args_p (tree parm_packs
,
9912 /* If one pack has an expansion and another pack has a normal
9913 argument or if one pack has an empty argument and an another
9914 one hasn't then tsubst_pack_expansion cannot perform the
9915 substitution and need to fall back on the
9916 PACK_EXPANSION_EXTRA mechanism. */
9917 if (parm_packs
== NULL_TREE
)
9919 else if (has_empty_arg
)
9922 bool has_expansion_arg
= false;
9923 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
9925 bool has_non_expansion_arg
= false;
9926 for (tree parm_pack
= parm_packs
;
9928 parm_pack
= TREE_CHAIN (parm_pack
))
9930 tree arg
= TREE_VALUE (parm_pack
);
9932 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
9934 /* We can't substitute a pack expansion with extra args into
9938 has_expansion_arg
= true;
9940 has_non_expansion_arg
= true;
9943 if (has_expansion_arg
&& has_non_expansion_arg
)
9949 /* [temp.variadic]/6 says that:
9951 The instantiation of a pack expansion [...]
9952 produces a list E1,E2, ..., En, where N is the number of elements
9953 in the pack expansion parameters.
9955 This subroutine of tsubst_pack_expansion produces one of these Ei.
9957 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9958 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9959 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9960 INDEX is the index 'i' of the element Ei to produce. ARGS,
9961 COMPLAIN, and IN_DECL are the same parameters as for the
9962 tsubst_pack_expansion function.
9964 The function returns the resulting Ei upon successful completion,
9967 Note that this function possibly modifies the ARGS parameter, so
9968 it's the responsibility of the caller to restore it. */
9971 gen_elem_of_pack_expansion_instantiation (tree pattern
,
9974 tree args
/* This parm gets
9976 tsubst_flags_t complain
,
9980 bool ith_elem_is_expansion
= false;
9982 /* For each parameter pack, change the substitution of the parameter
9983 pack to the ith argument in its argument pack, then expand the
9985 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
9987 tree parm
= TREE_PURPOSE (pack
);
9988 tree arg_pack
= TREE_VALUE (pack
);
9989 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
9991 ith_elem_is_expansion
|=
9992 argument_pack_element_is_expansion_p (arg_pack
, index
);
9994 /* Select the Ith argument from the pack. */
9995 if (TREE_CODE (parm
) == PARM_DECL
9996 || TREE_CODE (parm
) == FIELD_DECL
)
10000 aps
= make_argument_pack_select (arg_pack
, index
);
10002 register_local_specialization (aps
, parm
);
10005 aps
= retrieve_local_specialization (parm
);
10010 template_parm_level_and_index (parm
, &level
, &idx
);
10014 aps
= make_argument_pack_select (arg_pack
, index
);
10015 /* Update the corresponding argument. */
10016 TMPL_ARG (args
, level
, idx
) = aps
;
10019 /* Re-use the ARGUMENT_PACK_SELECT. */
10020 aps
= TMPL_ARG (args
, level
, idx
);
10022 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
10025 /* Substitute into the PATTERN with the (possibly altered)
10027 if (pattern
== in_decl
)
10028 /* Expanding a fixed parameter pack from
10029 coerce_template_parameter_pack. */
10030 t
= tsubst_decl (pattern
, args
, complain
);
10031 else if (!TYPE_P (pattern
))
10032 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
10033 /*integral_constant_expression_p=*/false);
10035 t
= tsubst (pattern
, args
, complain
, in_decl
);
10037 /* If the Ith argument pack element is a pack expansion, then
10038 the Ith element resulting from the substituting is going to
10039 be a pack expansion as well. */
10040 if (ith_elem_is_expansion
)
10041 t
= make_pack_expansion (t
);
10046 /* Substitute ARGS into T, which is an pack expansion
10047 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
10048 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
10049 (if only a partial substitution could be performed) or
10050 ERROR_MARK_NODE if there was an error. */
10052 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
10056 tree pack
, packs
= NULL_TREE
;
10057 bool unsubstituted_packs
= false;
10060 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
10061 bool need_local_specializations
= false;
10064 gcc_assert (PACK_EXPANSION_P (t
));
10065 pattern
= PACK_EXPANSION_PATTERN (t
);
10067 /* Add in any args remembered from an earlier partial instantiation. */
10068 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
10070 levels
= TMPL_ARGS_DEPTH (args
);
10072 /* Determine the argument packs that will instantiate the parameter
10073 packs used in the expansion expression. While we're at it,
10074 compute the number of arguments to be expanded and make sure it
10076 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
10077 pack
= TREE_CHAIN (pack
))
10079 tree parm_pack
= TREE_VALUE (pack
);
10080 tree arg_pack
= NULL_TREE
;
10081 tree orig_arg
= NULL_TREE
;
10084 if (TREE_CODE (parm_pack
) == BASES
)
10086 if (BASES_DIRECT (parm_pack
))
10087 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
10088 args
, complain
, in_decl
, false));
10090 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
10091 args
, complain
, in_decl
, false));
10093 if (TREE_CODE (parm_pack
) == PARM_DECL
)
10095 if (PACK_EXPANSION_LOCAL_P (t
))
10096 arg_pack
= retrieve_local_specialization (parm_pack
);
10099 /* We can't rely on local_specializations for a parameter
10100 name used later in a function declaration (such as in a
10101 late-specified return type). Even if it exists, it might
10102 have the wrong value for a recursive call. Just make a
10103 dummy decl, since it's only used for its type. */
10104 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
10105 if (arg_pack
&& DECL_PACK_P (arg_pack
))
10106 /* Partial instantiation of the parm_pack, we can't build
10107 up an argument pack yet. */
10108 arg_pack
= NULL_TREE
;
10110 arg_pack
= make_fnparm_pack (arg_pack
);
10111 need_local_specializations
= true;
10114 else if (TREE_CODE (parm_pack
) == FIELD_DECL
)
10115 arg_pack
= tsubst_copy (parm_pack
, args
, complain
, in_decl
);
10119 template_parm_level_and_index (parm_pack
, &level
, &idx
);
10121 if (level
<= levels
)
10122 arg_pack
= TMPL_ARG (args
, level
, idx
);
10125 orig_arg
= arg_pack
;
10126 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
10127 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
10129 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
10130 /* This can only happen if we forget to expand an argument
10131 pack somewhere else. Just return an error, silently. */
10133 result
= make_tree_vec (1);
10134 TREE_VEC_ELT (result
, 0) = error_mark_node
;
10141 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
10143 /* Don't bother trying to do a partial substitution with
10144 incomplete packs; we'll try again after deduction. */
10145 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
10150 else if (len
!= my_len
)
10152 if (!(complain
& tf_error
))
10153 /* Fail quietly. */;
10154 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
10155 error ("mismatched argument pack lengths while expanding "
10159 error ("mismatched argument pack lengths while expanding "
10162 return error_mark_node
;
10165 /* Keep track of the parameter packs and their corresponding
10167 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
10168 TREE_TYPE (packs
) = orig_arg
;
10172 /* We can't substitute for this parameter pack. We use a flag as
10173 well as the missing_level counter because function parameter
10174 packs don't have a level. */
10175 unsubstituted_packs
= true;
10179 /* If the expansion is just T..., return the matching argument pack, unless
10180 we need to call convert_from_reference on all the elements. This is an
10181 important optimization; see c++/68422. */
10182 if (!unsubstituted_packs
10183 && TREE_PURPOSE (packs
) == pattern
)
10185 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
10186 /* Types need no adjustment, nor does sizeof..., and if we still have
10187 some pack expansion args we won't do anything yet. */
10188 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
10189 || PACK_EXPANSION_SIZEOF_P (t
)
10190 || pack_expansion_args_count (args
))
10192 /* Otherwise use the normal path so we get convert_from_reference. */
10195 /* We cannot expand this expansion expression, because we don't have
10196 all of the argument packs we need. */
10197 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
10199 /* We got some full packs, but we can't substitute them in until we
10200 have values for all the packs. So remember these until then. */
10202 t
= make_pack_expansion (pattern
);
10203 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
10206 else if (unsubstituted_packs
)
10208 /* There were no real arguments, we're just replacing a parameter
10209 pack with another version of itself. Substitute into the
10210 pattern and return a PACK_EXPANSION_*. The caller will need to
10212 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
10213 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
10214 /*integral_constant_expression_p=*/false);
10216 t
= tsubst (pattern
, args
, complain
, in_decl
);
10217 t
= make_pack_expansion (t
);
10221 gcc_assert (len
>= 0);
10223 if (need_local_specializations
)
10225 /* We're in a late-specified return type, so create our own local
10226 specializations map; the current map is either NULL or (in the
10227 case of recursive unification) might have bindings that we don't
10228 want to use or alter. */
10229 saved_local_specializations
= local_specializations
;
10230 local_specializations
= new hash_map
<tree
, tree
>;
10233 /* For each argument in each argument pack, substitute into the
10235 result
= make_tree_vec (len
);
10236 for (i
= 0; i
< len
; ++i
)
10238 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
10242 TREE_VEC_ELT (result
, i
) = t
;
10243 if (t
== error_mark_node
)
10245 result
= error_mark_node
;
10250 /* Update ARGS to restore the substitution from parameter packs to
10251 their argument packs. */
10252 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
10254 tree parm
= TREE_PURPOSE (pack
);
10256 if (TREE_CODE (parm
) == PARM_DECL
10257 || TREE_CODE (parm
) == FIELD_DECL
)
10258 register_local_specialization (TREE_TYPE (pack
), parm
);
10263 if (TREE_VALUE (pack
) == NULL_TREE
)
10266 template_parm_level_and_index (parm
, &level
, &idx
);
10268 /* Update the corresponding argument. */
10269 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
10270 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
10273 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
10277 if (need_local_specializations
)
10279 delete local_specializations
;
10280 local_specializations
= saved_local_specializations
;
10286 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
10287 TMPL. We do this using DECL_PARM_INDEX, which should work even with
10288 parameter packs; all parms generated from a function parameter pack will
10289 have the same DECL_PARM_INDEX. */
10292 get_pattern_parm (tree parm
, tree tmpl
)
10294 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
10297 if (DECL_ARTIFICIAL (parm
))
10299 for (patparm
= DECL_ARGUMENTS (pattern
);
10300 patparm
; patparm
= DECL_CHAIN (patparm
))
10301 if (DECL_ARTIFICIAL (patparm
)
10302 && DECL_NAME (parm
) == DECL_NAME (patparm
))
10307 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
10308 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
10309 gcc_assert (DECL_PARM_INDEX (patparm
)
10310 == DECL_PARM_INDEX (parm
));
10316 /* Substitute ARGS into the vector or list of template arguments T. */
10319 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10322 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
10325 if (t
== error_mark_node
)
10326 return error_mark_node
;
10328 len
= TREE_VEC_LENGTH (t
);
10329 elts
= XALLOCAVEC (tree
, len
);
10331 for (i
= 0; i
< len
; i
++)
10333 tree orig_arg
= TREE_VEC_ELT (t
, i
);
10336 if (TREE_CODE (orig_arg
) == TREE_VEC
)
10337 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
10338 else if (PACK_EXPANSION_P (orig_arg
))
10340 /* Substitute into an expansion expression. */
10341 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
10343 if (TREE_CODE (new_arg
) == TREE_VEC
)
10344 /* Add to the expanded length adjustment the number of
10345 expanded arguments. We subtract one from this
10346 measurement, because the argument pack expression
10347 itself is already counted as 1 in
10348 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
10349 the argument pack is empty. */
10350 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
10352 else if (ARGUMENT_PACK_P (orig_arg
))
10354 /* Substitute into each of the arguments. */
10355 new_arg
= TYPE_P (orig_arg
)
10356 ? cxx_make_type (TREE_CODE (orig_arg
))
10357 : make_node (TREE_CODE (orig_arg
));
10359 SET_ARGUMENT_PACK_ARGS (
10361 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
10362 args
, complain
, in_decl
));
10364 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
10365 new_arg
= error_mark_node
;
10367 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
10368 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
10369 complain
, in_decl
);
10370 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
10372 if (TREE_TYPE (new_arg
) == error_mark_node
)
10373 new_arg
= error_mark_node
;
10377 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
10379 if (new_arg
== error_mark_node
)
10380 return error_mark_node
;
10383 if (new_arg
!= orig_arg
)
10390 /* Make space for the expanded arguments coming from template
10392 t
= make_tree_vec (len
+ expanded_len_adjust
);
10393 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
10394 arguments for a member template.
10395 In that case each TREE_VEC in ORIG_T represents a level of template
10396 arguments, and ORIG_T won't carry any non defaulted argument count.
10397 It will rather be the nested TREE_VECs that will carry one.
10398 In other words, ORIG_T carries a non defaulted argument count only
10399 if it doesn't contain any nested TREE_VEC. */
10400 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
10402 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
10403 count
+= expanded_len_adjust
;
10404 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
10406 for (i
= 0, out
= 0; i
< len
; i
++)
10408 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
10409 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
10410 && TREE_CODE (elts
[i
]) == TREE_VEC
)
10414 /* Now expand the template argument pack "in place". */
10415 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
10416 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
10420 TREE_VEC_ELT (t
, out
) = elts
[i
];
10428 /* Return the result of substituting ARGS into the template parameters
10429 given by PARMS. If there are m levels of ARGS and m + n levels of
10430 PARMS, then the result will contain n levels of PARMS. For
10431 example, if PARMS is `template <class T> template <class U>
10432 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
10433 result will be `template <int*, double, class V>'. */
10436 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
10438 tree r
= NULL_TREE
;
10441 /* When substituting into a template, we must set
10442 PROCESSING_TEMPLATE_DECL as the template parameters may be
10443 dependent if they are based on one-another, and the dependency
10444 predicates are short-circuit outside of templates. */
10445 ++processing_template_decl
;
10447 for (new_parms
= &r
;
10448 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
10449 new_parms
= &(TREE_CHAIN (*new_parms
)),
10450 parms
= TREE_CHAIN (parms
))
10453 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
10456 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
10460 if (parms
== error_mark_node
)
10463 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
10465 if (tuple
== error_mark_node
)
10468 TREE_VEC_ELT (new_vec
, i
) =
10469 tsubst_template_parm (tuple
, args
, complain
);
10473 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
10474 - TMPL_ARGS_DEPTH (args
)),
10475 new_vec
, NULL_TREE
);
10478 --processing_template_decl
;
10483 /* Return the result of substituting ARGS into one template parameter
10484 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
10485 parameter and which TREE_PURPOSE is the default argument of the
10486 template parameter. */
10489 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
10491 tree default_value
, parm_decl
;
10493 if (args
== NULL_TREE
10495 || t
== error_mark_node
)
10498 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
10500 default_value
= TREE_PURPOSE (t
);
10501 parm_decl
= TREE_VALUE (t
);
10503 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
10504 if (TREE_CODE (parm_decl
) == PARM_DECL
10505 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
10506 parm_decl
= error_mark_node
;
10507 default_value
= tsubst_template_arg (default_value
, args
,
10508 complain
, NULL_TREE
);
10510 return build_tree_list (default_value
, parm_decl
);
10513 /* Substitute the ARGS into the indicated aggregate (or enumeration)
10514 type T. If T is not an aggregate or enumeration type, it is
10515 handled as if by tsubst. IN_DECL is as for tsubst. If
10516 ENTERING_SCOPE is nonzero, T is the context for a template which
10517 we are presently tsubst'ing. Return the substituted value. */
10520 tsubst_aggr_type (tree t
,
10522 tsubst_flags_t complain
,
10524 int entering_scope
)
10526 if (t
== NULL_TREE
)
10529 switch (TREE_CODE (t
))
10532 if (TYPE_PTRMEMFUNC_P (t
))
10533 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
10535 /* Else fall through. */
10536 case ENUMERAL_TYPE
:
10538 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
10543 int saved_unevaluated_operand
;
10544 int saved_inhibit_evaluation_warnings
;
10546 /* In "sizeof(X<I>)" we need to evaluate "I". */
10547 saved_unevaluated_operand
= cp_unevaluated_operand
;
10548 cp_unevaluated_operand
= 0;
10549 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
10550 c_inhibit_evaluation_warnings
= 0;
10552 /* First, determine the context for the type we are looking
10554 context
= TYPE_CONTEXT (t
);
10555 if (context
&& TYPE_P (context
))
10557 context
= tsubst_aggr_type (context
, args
, complain
,
10558 in_decl
, /*entering_scope=*/1);
10559 /* If context is a nested class inside a class template,
10560 it may still need to be instantiated (c++/33959). */
10561 context
= complete_type (context
);
10564 /* Then, figure out what arguments are appropriate for the
10565 type we are trying to find. For example, given:
10567 template <class T> struct S;
10568 template <class T, class U> void f(T, U) { S<U> su; }
10570 and supposing that we are instantiating f<int, double>,
10571 then our ARGS will be {int, double}, but, when looking up
10572 S we only want {double}. */
10573 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
10574 complain
, in_decl
);
10575 if (argvec
== error_mark_node
)
10576 r
= error_mark_node
;
10579 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
10580 entering_scope
, complain
);
10581 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
10584 cp_unevaluated_operand
= saved_unevaluated_operand
;
10585 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
10590 /* This is not a template type, so there's nothing to do. */
10594 return tsubst (t
, args
, complain
, in_decl
);
10598 /* Substitute into the default argument ARG (a default argument for
10599 FN), which has the indicated TYPE. */
10602 tsubst_default_argument (tree fn
, tree type
, tree arg
, tsubst_flags_t complain
)
10604 tree saved_class_ptr
= NULL_TREE
;
10605 tree saved_class_ref
= NULL_TREE
;
10606 int errs
= errorcount
+ sorrycount
;
10608 /* This can happen in invalid code. */
10609 if (TREE_CODE (arg
) == DEFAULT_ARG
)
10612 /* This default argument came from a template. Instantiate the
10613 default argument here, not in tsubst. In the case of
10622 we must be careful to do name lookup in the scope of S<T>,
10623 rather than in the current class. */
10624 push_access_scope (fn
);
10625 /* The "this" pointer is not valid in a default argument. */
10628 saved_class_ptr
= current_class_ptr
;
10629 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
10630 saved_class_ref
= current_class_ref
;
10631 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
10634 push_deferring_access_checks(dk_no_deferred
);
10635 /* The default argument expression may cause implicitly defined
10636 member functions to be synthesized, which will result in garbage
10637 collection. We must treat this situation as if we were within
10638 the body of function so as to avoid collecting live data on the
10641 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
10642 complain
, NULL_TREE
,
10643 /*integral_constant_expression_p=*/false);
10645 pop_deferring_access_checks();
10647 /* Restore the "this" pointer. */
10650 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
10651 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
10654 if (errorcount
+sorrycount
> errs
10655 && (complain
& tf_warning_or_error
))
10656 inform (input_location
,
10657 " when instantiating default argument for call to %D", fn
);
10659 /* Make sure the default argument is reasonable. */
10660 arg
= check_default_argument (type
, arg
, complain
);
10662 pop_access_scope (fn
);
10667 /* Substitute into all the default arguments for FN. */
10670 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
10675 tmpl_args
= DECL_TI_ARGS (fn
);
10677 /* If this function is not yet instantiated, we certainly don't need
10678 its default arguments. */
10679 if (uses_template_parms (tmpl_args
))
10681 /* Don't do this again for clones. */
10682 if (DECL_CLONED_FUNCTION_P (fn
))
10685 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
10687 arg
= TREE_CHAIN (arg
))
10688 if (TREE_PURPOSE (arg
))
10689 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
10691 TREE_PURPOSE (arg
),
10695 /* Substitute the ARGS into the T, which is a _DECL. Return the
10696 result of the substitution. Issue error and warning messages under
10697 control of COMPLAIN. */
10700 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
10702 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10703 location_t saved_loc
;
10704 tree r
= NULL_TREE
;
10706 hashval_t hash
= 0;
10708 /* Set the filename and linenumber to improve error-reporting. */
10709 saved_loc
= input_location
;
10710 input_location
= DECL_SOURCE_LOCATION (t
);
10712 switch (TREE_CODE (t
))
10714 case TEMPLATE_DECL
:
10716 /* We can get here when processing a member function template,
10717 member class template, or template template parameter. */
10718 tree decl
= DECL_TEMPLATE_RESULT (t
);
10723 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10725 /* Template template parameter is treated here. */
10726 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10727 if (new_type
== error_mark_node
)
10728 RETURN (error_mark_node
);
10729 /* If we get a real template back, return it. This can happen in
10730 the context of most_specialized_partial_spec. */
10731 if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
10735 DECL_CHAIN (r
) = NULL_TREE
;
10736 TREE_TYPE (r
) = new_type
;
10737 DECL_TEMPLATE_RESULT (r
)
10738 = build_decl (DECL_SOURCE_LOCATION (decl
),
10739 TYPE_DECL
, DECL_NAME (decl
), new_type
);
10740 DECL_TEMPLATE_PARMS (r
)
10741 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10743 TYPE_NAME (new_type
) = r
;
10747 /* We might already have an instance of this template.
10748 The ARGS are for the surrounding class type, so the
10749 full args contain the tsubst'd args for the context,
10750 plus the innermost args from the template decl. */
10751 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
10752 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
10753 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
10754 /* Because this is a template, the arguments will still be
10755 dependent, even after substitution. If
10756 PROCESSING_TEMPLATE_DECL is not set, the dependency
10757 predicates will short-circuit. */
10758 ++processing_template_decl
;
10759 full_args
= tsubst_template_args (tmpl_args
, args
,
10760 complain
, in_decl
);
10761 --processing_template_decl
;
10762 if (full_args
== error_mark_node
)
10763 RETURN (error_mark_node
);
10765 /* If this is a default template template argument,
10766 tsubst might not have changed anything. */
10767 if (full_args
== tmpl_args
)
10770 hash
= hash_tmpl_and_args (t
, full_args
);
10771 spec
= retrieve_specialization (t
, full_args
, hash
);
10772 if (spec
!= NULL_TREE
)
10778 /* Make a new template decl. It will be similar to the
10779 original, but will record the current template arguments.
10780 We also create a new function declaration, which is just
10781 like the old one, but points to this new template, rather
10782 than the old one. */
10784 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
10785 DECL_CHAIN (r
) = NULL_TREE
;
10787 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10789 if (TREE_CODE (decl
) == TYPE_DECL
10790 && !TYPE_DECL_ALIAS_P (decl
))
10793 ++processing_template_decl
;
10794 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10795 --processing_template_decl
;
10796 if (new_type
== error_mark_node
)
10797 RETURN (error_mark_node
);
10799 TREE_TYPE (r
) = new_type
;
10800 /* For a partial specialization, we need to keep pointing to
10801 the primary template. */
10802 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
10803 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
10804 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
10805 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
10806 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
10811 ++processing_template_decl
;
10812 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
10813 --processing_template_decl
;
10814 if (new_decl
== error_mark_node
)
10815 RETURN (error_mark_node
);
10817 DECL_TEMPLATE_RESULT (r
) = new_decl
;
10818 DECL_TI_TEMPLATE (new_decl
) = r
;
10819 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
10820 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
10821 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
10824 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10825 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
10826 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
10828 /* The template parameters for this new template are all the
10829 template parameters for the old template, except the
10830 outermost level of parameters. */
10831 DECL_TEMPLATE_PARMS (r
)
10832 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10835 if (PRIMARY_TEMPLATE_P (t
))
10836 DECL_PRIMARY_TEMPLATE (r
) = r
;
10838 if (TREE_CODE (decl
) != TYPE_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
10839 /* Record this non-type partial instantiation. */
10840 register_specialization (r
, t
,
10841 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
10846 case FUNCTION_DECL
:
10849 tree argvec
= NULL_TREE
;
10857 /* Nobody should be tsubst'ing into non-template functions. */
10858 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
10860 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
10865 /* If T is not dependent, just return it. We have to
10866 increment PROCESSING_TEMPLATE_DECL because
10867 value_dependent_expression_p assumes that nothing is
10868 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10869 ++processing_template_decl
;
10870 dependent_p
= value_dependent_expression_p (t
);
10871 --processing_template_decl
;
10875 /* Calculate the most general template of which R is a
10876 specialization, and the complete set of arguments used to
10878 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
10879 argvec
= tsubst_template_args (DECL_TI_ARGS
10880 (DECL_TEMPLATE_RESULT
10881 (DECL_TI_TEMPLATE (t
))),
10882 args
, complain
, in_decl
);
10883 if (argvec
== error_mark_node
)
10884 RETURN (error_mark_node
);
10886 /* Check to see if we already have this specialization. */
10887 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10888 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10896 /* We can see more levels of arguments than parameters if
10897 there was a specialization of a member template, like
10900 template <class T> struct S { template <class U> void f(); }
10901 template <> template <class U> void S<int>::f(U);
10903 Here, we'll be substituting into the specialization,
10904 because that's where we can find the code we actually
10905 want to generate, but we'll have enough arguments for
10906 the most general template.
10908 We also deal with the peculiar case:
10910 template <class T> struct S {
10911 template <class U> friend void f();
10913 template <class U> void f() {}
10915 template void f<double>();
10917 Here, the ARGS for the instantiation of will be {int,
10918 double}. But, we only need as many ARGS as there are
10919 levels of template parameters in CODE_PATTERN. We are
10920 careful not to get fooled into reducing the ARGS in
10923 template <class T> struct S { template <class U> void f(U); }
10924 template <class T> template <> void S<T>::f(int) {}
10926 which we can spot because the pattern will be a
10927 specialization in this case. */
10928 args_depth
= TMPL_ARGS_DEPTH (args
);
10930 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
10931 if (args_depth
> parms_depth
10932 && !DECL_TEMPLATE_SPECIALIZATION (t
))
10933 args
= get_innermost_template_args (args
, parms_depth
);
10937 /* This special case arises when we have something like this:
10939 template <class T> struct S {
10940 friend void f<int>(int, double);
10943 Here, the DECL_TI_TEMPLATE for the friend declaration
10944 will be an IDENTIFIER_NODE. We are being called from
10945 tsubst_friend_function, and we want only to create a
10946 new decl (R) with appropriate types so that we can call
10947 determine_specialization. */
10948 gen_tmpl
= NULL_TREE
;
10951 if (DECL_CLASS_SCOPE_P (t
))
10953 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
10957 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
10958 complain
, t
, /*entering_scope=*/1);
10963 ctx
= DECL_CONTEXT (t
);
10965 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10966 if (type
== error_mark_node
)
10967 RETURN (error_mark_node
);
10969 /* If we hit excessive deduction depth, the type is bogus even if
10970 it isn't error_mark_node, so don't build a decl. */
10971 if (excessive_deduction_depth
)
10972 RETURN (error_mark_node
);
10974 /* We do NOT check for matching decls pushed separately at this
10975 point, as they may not represent instantiations of this
10976 template, and in any case are considered separate under the
10979 DECL_USE_TEMPLATE (r
) = 0;
10980 TREE_TYPE (r
) = type
;
10981 /* Clear out the mangled name and RTL for the instantiation. */
10982 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10983 SET_DECL_RTL (r
, NULL
);
10984 /* Leave DECL_INITIAL set on deleted instantiations. */
10985 if (!DECL_DELETED_FN (r
))
10986 DECL_INITIAL (r
) = NULL_TREE
;
10987 DECL_CONTEXT (r
) = ctx
;
10989 /* OpenMP UDRs have the only argument a reference to the declared
10990 type. We want to diagnose if the declared type is a reference,
10991 which is invalid, but as references to references are usually
10992 quietly merged, diagnose it here. */
10993 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
10996 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
10997 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
10998 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
10999 error_at (DECL_SOURCE_LOCATION (t
),
11000 "reference type %qT in "
11001 "%<#pragma omp declare reduction%>", argtype
);
11002 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
11003 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
11007 if (member
&& DECL_CONV_FN_P (r
))
11008 /* Type-conversion operator. Reconstruct the name, in
11009 case it's the name of one of the template's parameters. */
11010 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
11012 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
11014 DECL_RESULT (r
) = NULL_TREE
;
11016 TREE_STATIC (r
) = 0;
11017 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
11018 DECL_EXTERNAL (r
) = 1;
11019 /* If this is an instantiation of a function with internal
11020 linkage, we already know what object file linkage will be
11021 assigned to the instantiation. */
11022 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
11023 DECL_DEFER_OUTPUT (r
) = 0;
11024 DECL_CHAIN (r
) = NULL_TREE
;
11025 DECL_PENDING_INLINE_INFO (r
) = 0;
11026 DECL_PENDING_INLINE_P (r
) = 0;
11027 DECL_SAVED_TREE (r
) = NULL_TREE
;
11028 DECL_STRUCT_FUNCTION (r
) = NULL
;
11030 /* We'll re-clone as appropriate in instantiate_template. */
11031 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
11033 /* If we aren't complaining now, return on error before we register
11034 the specialization so that we'll complain eventually. */
11035 if ((complain
& tf_error
) == 0
11036 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
11037 && !grok_op_properties (r
, /*complain=*/false))
11038 RETURN (error_mark_node
);
11040 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
11041 this in the special friend case mentioned above where
11042 GEN_TMPL is NULL. */
11045 DECL_TEMPLATE_INFO (r
)
11046 = build_template_info (gen_tmpl
, argvec
);
11047 SET_DECL_IMPLICIT_INSTANTIATION (r
);
11050 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
11052 /* We instantiated this while substituting into
11053 the type earlier (template/friend54.C). */
11056 /* We're not supposed to instantiate default arguments
11057 until they are called, for a template. But, for a
11060 template <class T> void f ()
11061 { extern void g(int i = T()); }
11063 we should do the substitution when the template is
11064 instantiated. We handle the member function case in
11065 instantiate_class_template since the default arguments
11066 might refer to other members of the class. */
11068 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
11069 && !uses_template_parms (argvec
))
11070 tsubst_default_arguments (r
, complain
);
11073 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
11075 /* Copy the list of befriending classes. */
11076 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
11078 friends
= &TREE_CHAIN (*friends
))
11080 *friends
= copy_node (*friends
);
11081 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
11086 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
11088 maybe_retrofit_in_chrg (r
);
11089 if (DECL_CONSTRUCTOR_P (r
))
11090 grok_ctor_properties (ctx
, r
);
11091 if (DECL_INHERITED_CTOR_BASE (r
))
11092 deduce_inheriting_ctor (r
);
11093 /* If this is an instantiation of a member template, clone it.
11094 If it isn't, that'll be handled by
11095 clone_constructors_and_destructors. */
11096 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
11097 clone_function_decl (r
, /*update_method_vec_p=*/0);
11099 else if ((complain
& tf_error
) != 0
11100 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
11101 && !grok_op_properties (r
, /*complain=*/true))
11102 RETURN (error_mark_node
);
11104 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
11105 SET_DECL_FRIEND_CONTEXT (r
,
11106 tsubst (DECL_FRIEND_CONTEXT (t
),
11107 args
, complain
, in_decl
));
11109 /* Possibly limit visibility based on template args. */
11110 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
11111 if (DECL_VISIBILITY_SPECIFIED (t
))
11113 DECL_VISIBILITY_SPECIFIED (r
) = 0;
11114 DECL_ATTRIBUTES (r
)
11115 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
11117 determine_visibility (r
);
11118 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
11119 && !processing_template_decl
)
11120 defaulted_late_check (r
);
11122 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
11123 args
, complain
, in_decl
);
11129 tree type
= NULL_TREE
;
11131 tree expanded_types
= NULL_TREE
;
11132 tree prev_r
= NULL_TREE
;
11133 tree first_r
= NULL_TREE
;
11135 if (DECL_PACK_P (t
))
11137 /* If there is a local specialization that isn't a
11138 parameter pack, it means that we're doing a "simple"
11139 substitution from inside tsubst_pack_expansion. Just
11140 return the local specialization (which will be a single
11142 tree spec
= retrieve_local_specialization (t
);
11144 && TREE_CODE (spec
) == PARM_DECL
11145 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
11148 /* Expand the TYPE_PACK_EXPANSION that provides the types for
11149 the parameters in this function parameter pack. */
11150 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
11151 complain
, in_decl
);
11152 if (TREE_CODE (expanded_types
) == TREE_VEC
)
11154 len
= TREE_VEC_LENGTH (expanded_types
);
11156 /* Zero-length parameter packs are boring. Just substitute
11159 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
11164 /* All we did was update the type. Make a note of that. */
11165 type
= expanded_types
;
11166 expanded_types
= NULL_TREE
;
11170 /* Loop through all of the parameters we'll build. When T is
11171 a function parameter pack, LEN is the number of expanded
11172 types in EXPANDED_TYPES; otherwise, LEN is 1. */
11174 for (i
= 0; i
< len
; ++i
)
11178 if (DECL_TEMPLATE_PARM_P (t
))
11179 SET_DECL_TEMPLATE_PARM_P (r
);
11181 if (expanded_types
)
11182 /* We're on the Ith parameter of the function parameter
11185 /* Get the Ith type. */
11186 type
= TREE_VEC_ELT (expanded_types
, i
);
11188 /* Rename the parameter to include the index. */
11190 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
11193 /* We're dealing with a normal parameter. */
11194 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11196 type
= type_decays_to (type
);
11197 TREE_TYPE (r
) = type
;
11198 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
11200 if (DECL_INITIAL (r
))
11202 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
11203 DECL_INITIAL (r
) = TREE_TYPE (r
);
11205 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
11206 complain
, in_decl
);
11209 DECL_CONTEXT (r
) = NULL_TREE
;
11211 if (!DECL_TEMPLATE_PARM_P (r
))
11212 DECL_ARG_TYPE (r
) = type_passed_as (type
);
11214 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
11215 args
, complain
, in_decl
);
11217 /* Keep track of the first new parameter we
11218 generate. That's what will be returned to the
11223 /* Build a proper chain of parameters when substituting
11224 into a function parameter pack. */
11226 DECL_CHAIN (prev_r
) = r
;
11229 /* If cp_unevaluated_operand is set, we're just looking for a
11230 single dummy parameter, so don't keep going. */
11231 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
11232 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
11233 complain
, DECL_CHAIN (t
));
11235 /* FIRST_R contains the start of the chain we've built. */
11242 tree type
= NULL_TREE
;
11243 tree vec
= NULL_TREE
;
11244 tree expanded_types
= NULL_TREE
;
11247 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
11249 /* This field is a lambda capture pack. Return a TREE_VEC of
11250 the expanded fields to instantiate_class_template_1 and
11251 store them in the specializations hash table as a
11252 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
11253 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
11254 complain
, in_decl
);
11255 if (TREE_CODE (expanded_types
) == TREE_VEC
)
11257 len
= TREE_VEC_LENGTH (expanded_types
);
11258 vec
= make_tree_vec (len
);
11262 /* All we did was update the type. Make a note of that. */
11263 type
= expanded_types
;
11264 expanded_types
= NULL_TREE
;
11268 for (int i
= 0; i
< len
; ++i
)
11271 if (expanded_types
)
11273 type
= TREE_VEC_ELT (expanded_types
, i
);
11275 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
11278 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11280 if (type
== error_mark_node
)
11281 RETURN (error_mark_node
);
11282 TREE_TYPE (r
) = type
;
11283 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
11285 if (DECL_C_BIT_FIELD (r
))
11286 /* For bit-fields, DECL_INITIAL gives the number of bits. For
11287 non-bit-fields DECL_INITIAL is a non-static data member
11288 initializer, which gets deferred instantiation. */
11290 = tsubst_expr (DECL_INITIAL (t
), args
,
11292 /*integral_constant_expression_p=*/true);
11293 else if (DECL_INITIAL (t
))
11295 /* Set up DECL_TEMPLATE_INFO so that we can get at the
11296 NSDMI in perform_member_init. Still set DECL_INITIAL
11297 so that we know there is one. */
11298 DECL_INITIAL (r
) = void_node
;
11299 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
11300 retrofit_lang_decl (r
);
11301 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
11303 /* We don't have to set DECL_CONTEXT here; it is set by
11304 finish_member_declaration. */
11305 DECL_CHAIN (r
) = NULL_TREE
;
11307 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
11308 args
, complain
, in_decl
);
11311 TREE_VEC_ELT (vec
, i
) = r
;
11317 tree pack
= make_node (NONTYPE_ARGUMENT_PACK
);
11318 tree tpack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
11319 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
11320 SET_ARGUMENT_PACK_ARGS (tpack
, expanded_types
);
11321 TREE_TYPE (pack
) = tpack
;
11322 register_specialization (pack
, t
, args
, false, 0);
11328 /* We reach here only for member using decls. We also need to check
11329 uses_template_parms because DECL_DEPENDENT_P is not set for a
11330 using-declaration that designates a member of the current
11331 instantiation (c++/53549). */
11332 if (DECL_DEPENDENT_P (t
)
11333 || uses_template_parms (USING_DECL_SCOPE (t
)))
11335 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
11336 complain
, in_decl
);
11337 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
11338 r
= do_class_using_decl (inst_scope
, name
);
11340 r
= error_mark_node
;
11343 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
11344 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
11350 DECL_CHAIN (r
) = NULL_TREE
;
11357 tree argvec
= NULL_TREE
;
11358 tree gen_tmpl
= NULL_TREE
;
11360 tree tmpl
= NULL_TREE
;
11362 tree type
= NULL_TREE
;
11365 if (TREE_TYPE (t
) == error_mark_node
)
11366 RETURN (error_mark_node
);
11368 if (TREE_CODE (t
) == TYPE_DECL
11369 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
11371 /* If this is the canonical decl, we don't have to
11372 mess with instantiations, and often we can't (for
11373 typename, template type parms and such). Note that
11374 TYPE_NAME is not correct for the above test if
11375 we've copied the type for a typedef. */
11376 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11377 if (type
== error_mark_node
)
11378 RETURN (error_mark_node
);
11379 r
= TYPE_NAME (type
);
11383 /* Check to see if we already have the specialization we
11386 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
11388 /* T is a static data member or namespace-scope entity.
11389 We have to substitute into namespace-scope variables
11390 (not just variable templates) because of cases like:
11392 template <class T> void f() { extern T t; }
11394 where the entity referenced is not known until
11395 instantiation time. */
11397 ctx
= DECL_CONTEXT (t
);
11398 if (DECL_CLASS_SCOPE_P (t
))
11400 ctx
= tsubst_aggr_type (ctx
, args
,
11402 in_decl
, /*entering_scope=*/1);
11403 /* If CTX is unchanged, then T is in fact the
11404 specialization we want. That situation occurs when
11405 referencing a static data member within in its own
11406 class. We can use pointer equality, rather than
11407 same_type_p, because DECL_CONTEXT is always
11409 if (ctx
== DECL_CONTEXT (t
)
11410 /* ... unless T is a member template; in which
11411 case our caller can be willing to create a
11412 specialization of that template represented
11414 && !(DECL_TI_TEMPLATE (t
)
11415 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
11421 tmpl
= DECL_TI_TEMPLATE (t
);
11422 gen_tmpl
= most_general_template (tmpl
);
11423 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
11424 if (argvec
!= error_mark_node
)
11425 argvec
= (coerce_innermost_template_parms
11426 (DECL_TEMPLATE_PARMS (gen_tmpl
),
11427 argvec
, t
, complain
,
11428 /*all*/true, /*defarg*/true));
11429 if (argvec
== error_mark_node
)
11430 RETURN (error_mark_node
);
11431 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
11432 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
11437 /* A local variable. */
11439 /* Subsequent calls to pushdecl will fill this in. */
11441 spec
= retrieve_local_specialization (t
);
11443 /* If we already have the specialization we need, there is
11444 nothing more to do. */
11451 /* Create a new node for the specialization we need. */
11453 if (type
== NULL_TREE
)
11455 if (is_typedef_decl (t
))
11456 type
= DECL_ORIGINAL_TYPE (t
);
11458 type
= TREE_TYPE (t
);
11460 && VAR_HAD_UNKNOWN_BOUND (t
)
11461 && type
!= error_mark_node
)
11462 type
= strip_array_domain (type
);
11463 type
= tsubst (type
, args
, complain
, in_decl
);
11467 /* Even if the original location is out of scope, the
11468 newly substituted one is not. */
11469 DECL_DEAD_FOR_LOCAL (r
) = 0;
11470 DECL_INITIALIZED_P (r
) = 0;
11471 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
11472 if (type
== error_mark_node
)
11473 RETURN (error_mark_node
);
11474 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11476 /* It may seem that this case cannot occur, since:
11481 declares a function, not a variable. However:
11484 template <typename T> void g() { T t; }
11485 template void g<f>();
11487 is an attempt to declare a variable with function
11489 error ("variable %qD has function type",
11490 /* R is not yet sufficiently initialized, so we
11491 just use its name. */
11493 RETURN (error_mark_node
);
11495 type
= complete_type (type
);
11496 /* Wait until cp_finish_decl to set this again, to handle
11497 circular dependency (template/instantiate6.C). */
11498 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
11499 type
= check_var_type (DECL_NAME (r
), type
);
11501 if (DECL_HAS_VALUE_EXPR_P (t
))
11503 tree ve
= DECL_VALUE_EXPR (t
);
11504 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
11505 /*constant_expression_p=*/false);
11506 if (REFERENCE_REF_P (ve
))
11508 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
11509 ve
= TREE_OPERAND (ve
, 0);
11511 SET_DECL_VALUE_EXPR (r
, ve
);
11513 if (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
11514 set_decl_tls_model (r
, decl_tls_model (t
));
11516 else if (DECL_SELF_REFERENCE_P (t
))
11517 SET_DECL_SELF_REFERENCE_P (r
);
11518 TREE_TYPE (r
) = type
;
11519 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
11520 DECL_CONTEXT (r
) = ctx
;
11521 /* Clear out the mangled name and RTL for the instantiation. */
11522 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
11523 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
11524 SET_DECL_RTL (r
, NULL
);
11525 /* The initializer must not be expanded until it is required;
11526 see [temp.inst]. */
11527 DECL_INITIAL (r
) = NULL_TREE
;
11528 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
11529 SET_DECL_RTL (r
, NULL
);
11530 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
11533 /* Possibly limit visibility based on template args. */
11534 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
11535 if (DECL_VISIBILITY_SPECIFIED (t
))
11537 DECL_VISIBILITY_SPECIFIED (r
) = 0;
11538 DECL_ATTRIBUTES (r
)
11539 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
11541 determine_visibility (r
);
11546 /* A static data member declaration is always marked
11547 external when it is declared in-class, even if an
11548 initializer is present. We mimic the non-template
11549 processing here. */
11550 DECL_EXTERNAL (r
) = 1;
11551 if (DECL_NAMESPACE_SCOPE_P (t
))
11552 DECL_NOT_REALLY_EXTERN (r
) = 1;
11554 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
11555 SET_DECL_IMPLICIT_INSTANTIATION (r
);
11556 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
11558 else if (!cp_unevaluated_operand
)
11559 register_local_specialization (r
, t
);
11561 DECL_CHAIN (r
) = NULL_TREE
;
11563 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
11565 args
, complain
, in_decl
);
11567 /* Preserve a typedef that names a type. */
11568 if (is_typedef_decl (r
))
11570 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
11571 set_underlying_type (r
);
11572 if (TYPE_DECL_ALIAS_P (r
) && type
!= error_mark_node
)
11573 /* An alias template specialization can be dependent
11574 even if its underlying type is not. */
11575 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
11578 layout_decl (r
, 0);
11583 gcc_unreachable ();
11588 /* Restore the file and line information. */
11589 input_location
= saved_loc
;
11594 /* Substitute into the ARG_TYPES of a function type.
11595 If END is a TREE_CHAIN, leave it and any following types
11599 tsubst_arg_types (tree arg_types
,
11602 tsubst_flags_t complain
,
11605 tree remaining_arg_types
;
11606 tree type
= NULL_TREE
;
11608 tree expanded_args
= NULL_TREE
;
11611 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
11614 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
11615 args
, end
, complain
, in_decl
);
11616 if (remaining_arg_types
== error_mark_node
)
11617 return error_mark_node
;
11619 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
11621 /* For a pack expansion, perform substitution on the
11622 entire expression. Later on, we'll handle the arguments
11624 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
11625 args
, complain
, in_decl
);
11627 if (TREE_CODE (expanded_args
) == TREE_VEC
)
11628 /* So that we'll spin through the parameters, one by one. */
11629 i
= TREE_VEC_LENGTH (expanded_args
);
11632 /* We only partially substituted into the parameter
11633 pack. Our type is TYPE_PACK_EXPANSION. */
11634 type
= expanded_args
;
11635 expanded_args
= NULL_TREE
;
11643 type
= TREE_VEC_ELT (expanded_args
, i
);
11645 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
11647 if (type
== error_mark_node
)
11648 return error_mark_node
;
11649 if (VOID_TYPE_P (type
))
11651 if (complain
& tf_error
)
11653 error ("invalid parameter type %qT", type
);
11655 error ("in declaration %q+D", in_decl
);
11657 return error_mark_node
;
11660 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
11661 return error_mark_node
;
11663 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11664 top-level qualifiers as required. */
11665 type
= cv_unqualified (type_decays_to (type
));
11667 /* We do not substitute into default arguments here. The standard
11668 mandates that they be instantiated only when needed, which is
11669 done in build_over_call. */
11670 default_arg
= TREE_PURPOSE (arg_types
);
11672 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
11674 /* We've instantiated a template before its default arguments
11675 have been parsed. This can happen for a nested template
11676 class, and is not an error unless we require the default
11677 argument in a call of this function. */
11678 remaining_arg_types
=
11679 tree_cons (default_arg
, type
, remaining_arg_types
);
11680 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
11683 remaining_arg_types
=
11684 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
11687 return remaining_arg_types
;
11690 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11691 *not* handle the exception-specification for FNTYPE, because the
11692 initial substitution of explicitly provided template parameters
11693 during argument deduction forbids substitution into the
11694 exception-specification:
11698 All references in the function type of the function template to the
11699 corresponding template parameters are replaced by the specified tem-
11700 plate argument values. If a substitution in a template parameter or
11701 in the function type of the function template results in an invalid
11702 type, type deduction fails. [Note: The equivalent substitution in
11703 exception specifications is done only when the function is instanti-
11704 ated, at which point a program is ill-formed if the substitution
11705 results in an invalid type.] */
11708 tsubst_function_type (tree t
,
11710 tsubst_flags_t complain
,
11714 tree arg_types
= NULL_TREE
;
11717 /* The TYPE_CONTEXT is not used for function/method types. */
11718 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
11720 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
11722 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
11724 if (late_return_type_p
)
11726 /* Substitute the argument types. */
11727 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
11728 complain
, in_decl
);
11729 if (arg_types
== error_mark_node
)
11730 return error_mark_node
;
11732 tree save_ccp
= current_class_ptr
;
11733 tree save_ccr
= current_class_ref
;
11734 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
11735 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
11736 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
11739 /* DR 1207: 'this' is in scope in the trailing return type. */
11740 inject_this_parameter (this_type
, cp_type_quals (this_type
));
11743 /* Substitute the return type. */
11744 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11748 current_class_ptr
= save_ccp
;
11749 current_class_ref
= save_ccr
;
11753 /* Substitute the return type. */
11754 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11756 if (return_type
== error_mark_node
)
11757 return error_mark_node
;
11758 /* DR 486 clarifies that creation of a function type with an
11759 invalid return type is a deduction failure. */
11760 if (TREE_CODE (return_type
) == ARRAY_TYPE
11761 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
11763 if (complain
& tf_error
)
11765 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
11766 error ("function returning an array");
11768 error ("function returning a function");
11770 return error_mark_node
;
11773 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
11774 return error_mark_node
;
11776 if (!late_return_type_p
)
11778 /* Substitute the argument types. */
11779 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
11780 complain
, in_decl
);
11781 if (arg_types
== error_mark_node
)
11782 return error_mark_node
;
11785 /* Construct a new type node and return it. */
11786 if (TREE_CODE (t
) == FUNCTION_TYPE
)
11788 fntype
= build_function_type (return_type
, arg_types
);
11789 fntype
= apply_memfn_quals (fntype
,
11790 type_memfn_quals (t
),
11791 type_memfn_rqual (t
));
11795 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
11796 /* Don't pick up extra function qualifiers from the basetype. */
11797 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
11798 if (! MAYBE_CLASS_TYPE_P (r
))
11802 Type deduction may fail for any of the following
11805 -- Attempting to create "pointer to member of T" when T
11806 is not a class type. */
11807 if (complain
& tf_error
)
11808 error ("creating pointer to member function of non-class type %qT",
11810 return error_mark_node
;
11813 fntype
= build_method_type_directly (r
, return_type
,
11814 TREE_CHAIN (arg_types
));
11815 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
11817 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
11819 if (late_return_type_p
)
11820 TYPE_HAS_LATE_RETURN_TYPE (fntype
) = 1;
11825 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11826 ARGS into that specification, and return the substituted
11827 specification. If there is no specification, return NULL_TREE. */
11830 tsubst_exception_specification (tree fntype
,
11832 tsubst_flags_t complain
,
11839 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
11840 new_specs
= NULL_TREE
;
11841 if (specs
&& TREE_PURPOSE (specs
))
11843 /* A noexcept-specifier. */
11844 tree expr
= TREE_PURPOSE (specs
);
11845 if (TREE_CODE (expr
) == INTEGER_CST
)
11849 /* Defer instantiation of noexcept-specifiers to avoid
11850 excessive instantiations (c++/49107). */
11851 new_specs
= make_node (DEFERRED_NOEXCEPT
);
11852 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
11854 /* We already partially instantiated this member template,
11855 so combine the new args with the old. */
11856 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
11857 = DEFERRED_NOEXCEPT_PATTERN (expr
);
11858 DEFERRED_NOEXCEPT_ARGS (new_specs
)
11859 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
11863 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
11864 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
11868 new_specs
= tsubst_copy_and_build
11869 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
11870 /*integral_constant_expression_p=*/true);
11871 new_specs
= build_noexcept_spec (new_specs
, complain
);
11875 if (! TREE_VALUE (specs
))
11882 tree expanded_specs
= NULL_TREE
;
11884 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
11886 /* Expand the pack expansion type. */
11887 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
11891 if (expanded_specs
== error_mark_node
)
11892 return error_mark_node
;
11893 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
11894 len
= TREE_VEC_LENGTH (expanded_specs
);
11897 /* We're substituting into a member template, so
11898 we got a TYPE_PACK_EXPANSION back. Add that
11899 expansion and move on. */
11900 gcc_assert (TREE_CODE (expanded_specs
)
11901 == TYPE_PACK_EXPANSION
);
11902 new_specs
= add_exception_specifier (new_specs
,
11905 specs
= TREE_CHAIN (specs
);
11910 for (i
= 0; i
< len
; ++i
)
11912 if (expanded_specs
)
11913 spec
= TREE_VEC_ELT (expanded_specs
, i
);
11915 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
11916 if (spec
== error_mark_node
)
11918 new_specs
= add_exception_specifier (new_specs
, spec
,
11922 specs
= TREE_CHAIN (specs
);
11928 /* Take the tree structure T and replace template parameters used
11929 therein with the argument vector ARGS. IN_DECL is an associated
11930 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11931 Issue error and warning messages under control of COMPLAIN. Note
11932 that we must be relatively non-tolerant of extensions here, in
11933 order to preserve conformance; if we allow substitutions that
11934 should not be allowed, we may allow argument deductions that should
11935 not succeed, and therefore report ambiguous overload situations
11936 where there are none. In theory, we could allow the substitution,
11937 but indicate that it should have failed, and allow our caller to
11938 make sure that the right thing happens, but we don't try to do this
11941 This function is used for dealing with types, decls and the like;
11942 for expressions, use tsubst_expr or tsubst_copy. */
11945 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11947 enum tree_code code
;
11948 tree type
, r
= NULL_TREE
;
11950 if (t
== NULL_TREE
|| t
== error_mark_node
11951 || t
== integer_type_node
11952 || t
== void_type_node
11953 || t
== char_type_node
11954 || t
== unknown_type_node
11955 || TREE_CODE (t
) == NAMESPACE_DECL
11956 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
11960 return tsubst_decl (t
, args
, complain
);
11962 if (args
== NULL_TREE
)
11965 code
= TREE_CODE (t
);
11967 if (code
== IDENTIFIER_NODE
)
11968 type
= IDENTIFIER_TYPE_VALUE (t
);
11970 type
= TREE_TYPE (t
);
11972 gcc_assert (type
!= unknown_type_node
);
11974 /* Reuse typedefs. We need to do this to handle dependent attributes,
11975 such as attribute aligned. */
11977 && typedef_variant_p (t
))
11979 tree decl
= TYPE_NAME (t
);
11981 if (alias_template_specialization_p (t
))
11983 /* DECL represents an alias template and we want to
11985 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11986 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11987 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
11989 else if (DECL_CLASS_SCOPE_P (decl
)
11990 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11991 && uses_template_parms (DECL_CONTEXT (decl
)))
11993 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11994 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11995 r
= retrieve_specialization (tmpl
, gen_args
, 0);
11997 else if (DECL_FUNCTION_SCOPE_P (decl
)
11998 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11999 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
12000 r
= retrieve_local_specialization (decl
);
12002 /* The typedef is from a non-template context. */
12008 r
= cp_build_qualified_type_real
12009 (r
, cp_type_quals (t
) | cp_type_quals (r
),
12010 complain
| tf_ignore_bad_quals
);
12015 /* We don't have an instantiation yet, so drop the typedef. */
12016 int quals
= cp_type_quals (t
);
12017 t
= DECL_ORIGINAL_TYPE (decl
);
12018 t
= cp_build_qualified_type_real (t
, quals
,
12019 complain
| tf_ignore_bad_quals
);
12024 && code
!= TYPENAME_TYPE
12025 && code
!= TEMPLATE_TYPE_PARM
12026 && code
!= IDENTIFIER_NODE
12027 && code
!= FUNCTION_TYPE
12028 && code
!= METHOD_TYPE
)
12029 type
= tsubst (type
, args
, complain
, in_decl
);
12030 if (type
== error_mark_node
)
12031 return error_mark_node
;
12037 case ENUMERAL_TYPE
:
12038 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
12039 /*entering_scope=*/0);
12042 case IDENTIFIER_NODE
:
12053 if (t
== integer_type_node
)
12056 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
12057 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
12061 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
12063 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
12064 /*integral_constant_expression_p=*/false);
12066 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
12068 if (TREE_CODE (max
) == NOP_EXPR
12069 && TREE_SIDE_EFFECTS (omax
)
12070 && !TREE_TYPE (max
))
12071 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
12073 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
12074 with TREE_SIDE_EFFECTS that indicates this is not an integral
12075 constant expression. */
12076 if (processing_template_decl
12077 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
12079 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
12080 TREE_SIDE_EFFECTS (max
) = 1;
12083 return compute_array_index_type (NULL_TREE
, max
, complain
);
12086 case TEMPLATE_TYPE_PARM
:
12087 case TEMPLATE_TEMPLATE_PARM
:
12088 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12089 case TEMPLATE_PARM_INDEX
:
12094 tree arg
= NULL_TREE
;
12098 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
12099 template_parm_level_and_index (t
, &level
, &idx
);
12101 levels
= TMPL_ARGS_DEPTH (args
);
12102 if (level
<= levels
)
12104 arg
= TMPL_ARG (args
, level
, idx
);
12106 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
12108 /* See through ARGUMENT_PACK_SELECT arguments. */
12109 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
12110 /* If the selected argument is an expansion E, that most
12111 likely means we were called from
12112 gen_elem_of_pack_expansion_instantiation during the
12113 substituting of pack an argument pack (which Ith
12114 element is a pack expansion, where I is
12115 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
12116 In this case, the Ith element resulting from this
12117 substituting is going to be a pack expansion, which
12118 pattern is the pattern of E. Let's return the
12120 gen_elem_of_pack_expansion_instantiation will
12121 build the resulting pack expansion from it. */
12122 if (PACK_EXPANSION_P (arg
))
12124 /* Make sure we aren't throwing away arg info. */
12125 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
12126 arg
= PACK_EXPANSION_PATTERN (arg
);
12131 if (arg
== error_mark_node
)
12132 return error_mark_node
;
12133 else if (arg
!= NULL_TREE
)
12135 if (ARGUMENT_PACK_P (arg
))
12136 /* If ARG is an argument pack, we don't actually want to
12137 perform a substitution here, because substitutions
12138 for argument packs are only done
12139 element-by-element. We can get to this point when
12140 substituting the type of a non-type template
12141 parameter pack, when that type actually contains
12142 template parameter packs from an outer template, e.g.,
12144 template<typename... Types> struct A {
12145 template<Types... Values> struct B { };
12149 if (code
== TEMPLATE_TYPE_PARM
)
12152 gcc_assert (TYPE_P (arg
));
12154 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
12156 return cp_build_qualified_type_real
12157 (arg
, quals
, complain
| tf_ignore_bad_quals
);
12159 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
12161 /* We are processing a type constructed from a
12162 template template parameter. */
12163 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
12164 args
, complain
, in_decl
);
12165 if (argvec
== error_mark_node
)
12166 return error_mark_node
;
12168 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
12169 || TREE_CODE (arg
) == TEMPLATE_DECL
12170 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
12172 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
12173 /* Consider this code:
12175 template <template <class> class Template>
12177 template <class Arg> using Bind = Template<Arg>;
12180 template <template <class> class Template, class Arg>
12181 using Instantiate = Template<Arg>; //#0
12183 template <template <class> class Template,
12186 Instantiate<Internal<Template>::template Bind,
12189 When #1 is parsed, the
12190 BOUND_TEMPLATE_TEMPLATE_PARM representing the
12191 parameter `Template' in #0 matches the
12192 UNBOUND_CLASS_TEMPLATE representing the argument
12193 `Internal<Template>::template Bind'; We then want
12194 to assemble the type `Bind<Argument>' that can't
12195 be fully created right now, because
12196 `Internal<Template>' not being complete, the Bind
12197 template cannot be looked up in that context. So
12198 we need to "store" `Bind<Argument>' for later
12199 when the context of Bind becomes complete. Let's
12200 store that in a TYPENAME_TYPE. */
12201 return make_typename_type (TYPE_CONTEXT (arg
),
12202 build_nt (TEMPLATE_ID_EXPR
,
12203 TYPE_IDENTIFIER (arg
),
12208 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
12209 are resolving nested-types in the signature of a
12210 member function templates. Otherwise ARG is a
12211 TEMPLATE_DECL and is the real template to be
12213 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
12214 arg
= TYPE_NAME (arg
);
12216 r
= lookup_template_class (arg
,
12218 DECL_CONTEXT (arg
),
12219 /*entering_scope=*/0,
12221 return cp_build_qualified_type_real
12222 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
12225 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
12226 return convert_from_reference (unshare_expr (arg
));
12230 /* This can happen during the attempted tsubst'ing in
12231 unify. This means that we don't yet have any information
12232 about the template parameter in question. */
12235 /* Early in template argument deduction substitution, we don't
12236 want to reduce the level of 'auto', or it will be confused
12237 with a normal template parm in subsequent deduction. */
12238 if (is_auto (t
) && (complain
& tf_partial
))
12241 /* If we get here, we must have been looking at a parm for a
12242 more deeply nested template. Make a new version of this
12243 template parameter, but with a lower level. */
12246 case TEMPLATE_TYPE_PARM
:
12247 case TEMPLATE_TEMPLATE_PARM
:
12248 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12249 if (cp_type_quals (t
))
12251 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
12252 r
= cp_build_qualified_type_real
12253 (r
, cp_type_quals (t
),
12254 complain
| (code
== TEMPLATE_TYPE_PARM
12255 ? tf_ignore_bad_quals
: 0));
12260 TEMPLATE_TYPE_PARM_INDEX (r
)
12261 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
12262 r
, levels
, args
, complain
);
12263 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
12264 TYPE_MAIN_VARIANT (r
) = r
;
12265 TYPE_POINTER_TO (r
) = NULL_TREE
;
12266 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
12268 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
12269 /* We have reduced the level of the template
12270 template parameter, but not the levels of its
12271 template parameters, so canonical_type_parameter
12272 will not be able to find the canonical template
12273 template parameter for this level. Thus, we
12274 require structural equality checking to compare
12275 TEMPLATE_TEMPLATE_PARMs. */
12276 SET_TYPE_STRUCTURAL_EQUALITY (r
);
12277 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
12278 SET_TYPE_STRUCTURAL_EQUALITY (r
);
12280 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
12282 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
12284 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
12285 complain
, in_decl
);
12286 if (argvec
== error_mark_node
)
12287 return error_mark_node
;
12289 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
12290 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
12295 case TEMPLATE_PARM_INDEX
:
12296 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
12300 gcc_unreachable ();
12308 tree purpose
, value
, chain
;
12310 if (t
== void_list_node
)
12313 purpose
= TREE_PURPOSE (t
);
12316 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
12317 if (purpose
== error_mark_node
)
12318 return error_mark_node
;
12320 value
= TREE_VALUE (t
);
12323 value
= tsubst (value
, args
, complain
, in_decl
);
12324 if (value
== error_mark_node
)
12325 return error_mark_node
;
12327 chain
= TREE_CHAIN (t
);
12328 if (chain
&& chain
!= void_type_node
)
12330 chain
= tsubst (chain
, args
, complain
, in_decl
);
12331 if (chain
== error_mark_node
)
12332 return error_mark_node
;
12334 if (purpose
== TREE_PURPOSE (t
)
12335 && value
== TREE_VALUE (t
)
12336 && chain
== TREE_CHAIN (t
))
12338 return hash_tree_cons (purpose
, value
, chain
);
12342 /* We should never be tsubsting a binfo. */
12343 gcc_unreachable ();
12346 /* A vector of template arguments. */
12347 gcc_assert (!type
);
12348 return tsubst_template_args (t
, args
, complain
, in_decl
);
12351 case REFERENCE_TYPE
:
12353 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
12358 Type deduction may fail for any of the following
12361 -- Attempting to create a pointer to reference type.
12362 -- Attempting to create a reference to a reference type or
12363 a reference to void.
12365 Core issue 106 says that creating a reference to a reference
12366 during instantiation is no longer a cause for failure. We
12367 only enforce this check in strict C++98 mode. */
12368 if ((TREE_CODE (type
) == REFERENCE_TYPE
12369 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
12370 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
12372 static location_t last_loc
;
12374 /* We keep track of the last time we issued this error
12375 message to avoid spewing a ton of messages during a
12376 single bad template instantiation. */
12377 if (complain
& tf_error
12378 && last_loc
!= input_location
)
12380 if (VOID_TYPE_P (type
))
12381 error ("forming reference to void");
12382 else if (code
== POINTER_TYPE
)
12383 error ("forming pointer to reference type %qT", type
);
12385 error ("forming reference to reference type %qT", type
);
12386 last_loc
= input_location
;
12389 return error_mark_node
;
12391 else if (TREE_CODE (type
) == FUNCTION_TYPE
12392 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
12393 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
12395 if (complain
& tf_error
)
12397 if (code
== POINTER_TYPE
)
12398 error ("forming pointer to qualified function type %qT",
12401 error ("forming reference to qualified function type %qT",
12404 return error_mark_node
;
12406 else if (code
== POINTER_TYPE
)
12408 r
= build_pointer_type (type
);
12409 if (TREE_CODE (type
) == METHOD_TYPE
)
12410 r
= build_ptrmemfunc_type (r
);
12412 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
12413 /* In C++0x, during template argument substitution, when there is an
12414 attempt to create a reference to a reference type, reference
12415 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
12417 "If a template-argument for a template-parameter T names a type
12418 that is a reference to a type A, an attempt to create the type
12419 'lvalue reference to cv T' creates the type 'lvalue reference to
12420 A,' while an attempt to create the type type rvalue reference to
12421 cv T' creates the type T"
12423 r
= cp_build_reference_type
12425 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
12427 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
12428 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
12430 if (r
!= error_mark_node
)
12431 /* Will this ever be needed for TYPE_..._TO values? */
12438 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
12439 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
12443 Type deduction may fail for any of the following
12446 -- Attempting to create "pointer to member of T" when T
12447 is not a class type. */
12448 if (complain
& tf_error
)
12449 error ("creating pointer to member of non-class type %qT", r
);
12450 return error_mark_node
;
12452 if (TREE_CODE (type
) == REFERENCE_TYPE
)
12454 if (complain
& tf_error
)
12455 error ("creating pointer to member reference type %qT", type
);
12456 return error_mark_node
;
12458 if (VOID_TYPE_P (type
))
12460 if (complain
& tf_error
)
12461 error ("creating pointer to member of type void");
12462 return error_mark_node
;
12464 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
12465 if (TREE_CODE (type
) == FUNCTION_TYPE
)
12467 /* The type of the implicit object parameter gets its
12468 cv-qualifiers from the FUNCTION_TYPE. */
12471 = build_memfn_type (type
, r
, type_memfn_quals (type
),
12472 type_memfn_rqual (type
));
12473 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
12474 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
12478 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
12482 case FUNCTION_TYPE
:
12487 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
12488 if (fntype
== error_mark_node
)
12489 return error_mark_node
;
12491 /* Substitute the exception specification. */
12492 specs
= tsubst_exception_specification (t
, args
, complain
,
12493 in_decl
, /*defer_ok*/true);
12494 if (specs
== error_mark_node
)
12495 return error_mark_node
;
12497 fntype
= build_exception_variant (fntype
, specs
);
12502 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
12503 if (domain
== error_mark_node
)
12504 return error_mark_node
;
12506 /* As an optimization, we avoid regenerating the array type if
12507 it will obviously be the same as T. */
12508 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
12511 /* These checks should match the ones in create_array_type_for_decl.
12515 The deduction may fail for any of the following reasons:
12517 -- Attempting to create an array with an element type that
12518 is void, a function type, or a reference type, or [DR337]
12519 an abstract class type. */
12520 if (VOID_TYPE_P (type
)
12521 || TREE_CODE (type
) == FUNCTION_TYPE
12522 || (TREE_CODE (type
) == ARRAY_TYPE
12523 && TYPE_DOMAIN (type
) == NULL_TREE
)
12524 || TREE_CODE (type
) == REFERENCE_TYPE
)
12526 if (complain
& tf_error
)
12527 error ("creating array of %qT", type
);
12528 return error_mark_node
;
12531 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
12532 return error_mark_node
;
12534 r
= build_cplus_array_type (type
, domain
);
12536 if (TYPE_USER_ALIGN (t
))
12538 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
12539 TYPE_USER_ALIGN (r
) = 1;
12545 case TYPENAME_TYPE
:
12547 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
12548 in_decl
, /*entering_scope=*/1);
12549 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
12550 complain
, in_decl
);
12552 if (ctx
== error_mark_node
|| f
== error_mark_node
)
12553 return error_mark_node
;
12555 if (!MAYBE_CLASS_TYPE_P (ctx
))
12557 if (complain
& tf_error
)
12558 error ("%qT is not a class, struct, or union type", ctx
);
12559 return error_mark_node
;
12561 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
12563 /* Normally, make_typename_type does not require that the CTX
12564 have complete type in order to allow things like:
12566 template <class T> struct S { typename S<T>::X Y; };
12568 But, such constructs have already been resolved by this
12569 point, so here CTX really should have complete type, unless
12570 it's a partial instantiation. */
12571 ctx
= complete_type (ctx
);
12572 if (!COMPLETE_TYPE_P (ctx
))
12574 if (complain
& tf_error
)
12575 cxx_incomplete_type_error (NULL_TREE
, ctx
);
12576 return error_mark_node
;
12580 f
= make_typename_type (ctx
, f
, typename_type
,
12581 complain
| tf_keep_type_decl
);
12582 if (f
== error_mark_node
)
12584 if (TREE_CODE (f
) == TYPE_DECL
)
12586 complain
|= tf_ignore_bad_quals
;
12590 if (TREE_CODE (f
) != TYPENAME_TYPE
)
12592 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
12594 if (complain
& tf_error
)
12595 error ("%qT resolves to %qT, which is not an enumeration type",
12598 return error_mark_node
;
12600 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
12602 if (complain
& tf_error
)
12603 error ("%qT resolves to %qT, which is is not a class type",
12606 return error_mark_node
;
12610 return cp_build_qualified_type_real
12611 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
12614 case UNBOUND_CLASS_TEMPLATE
:
12616 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
12617 in_decl
, /*entering_scope=*/1);
12618 tree name
= TYPE_IDENTIFIER (t
);
12619 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
12621 if (ctx
== error_mark_node
|| name
== error_mark_node
)
12622 return error_mark_node
;
12625 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
12626 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
12633 ++cp_unevaluated_operand
;
12634 ++c_inhibit_evaluation_warnings
;
12636 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
12638 /*integral_constant_expression_p=*/false);
12640 --cp_unevaluated_operand
;
12641 --c_inhibit_evaluation_warnings
;
12643 type
= finish_typeof (type
);
12644 return cp_build_qualified_type_real (type
,
12646 | cp_type_quals (type
),
12650 case DECLTYPE_TYPE
:
12654 ++cp_unevaluated_operand
;
12655 ++c_inhibit_evaluation_warnings
;
12657 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
12658 complain
|tf_decltype
, in_decl
,
12659 /*function_p*/false,
12660 /*integral_constant_expression*/false);
12662 --cp_unevaluated_operand
;
12663 --c_inhibit_evaluation_warnings
;
12665 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
12666 type
= lambda_capture_field_type (type
,
12667 DECLTYPE_FOR_INIT_CAPTURE (t
));
12668 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
12669 type
= lambda_proxy_type (type
);
12672 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
12673 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
12675 /* In a template ~id could be either a complement expression
12676 or an unqualified-id naming a destructor; if instantiating
12677 it produces an expression, it's not an id-expression or
12680 type
= finish_decltype_type (type
, id
, complain
);
12682 return cp_build_qualified_type_real (type
,
12684 | cp_type_quals (type
),
12685 complain
| tf_ignore_bad_quals
);
12688 case UNDERLYING_TYPE
:
12690 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
12691 complain
, in_decl
);
12692 return finish_underlying_type (type
);
12695 case TYPE_ARGUMENT_PACK
:
12696 case NONTYPE_ARGUMENT_PACK
:
12698 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
12700 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
12704 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
12706 /* For template nontype argument packs, also substitute into
12708 if (code
== NONTYPE_ARGUMENT_PACK
)
12709 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12728 /* We should use one of the expression tsubsts for these codes. */
12729 gcc_unreachable ();
12732 sorry ("use of %qs in template", get_tree_code_name (code
));
12733 return error_mark_node
;
12737 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12738 type of the expression on the left-hand side of the "." or "->"
12742 tsubst_baselink (tree baselink
, tree object_type
,
12743 tree args
, tsubst_flags_t complain
, tree in_decl
)
12746 tree qualifying_scope
;
12749 tree template_args
= 0;
12750 bool template_id_p
= false;
12751 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
12753 /* A baselink indicates a function from a base class. Both the
12754 BASELINK_ACCESS_BINFO and the base class referenced may
12755 indicate bases of the template class, rather than the
12756 instantiated class. In addition, lookups that were not
12757 ambiguous before may be ambiguous now. Therefore, we perform
12758 the lookup again. */
12759 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
12760 qualifying_scope
= tsubst (qualifying_scope
, args
,
12761 complain
, in_decl
);
12762 fns
= BASELINK_FUNCTIONS (baselink
);
12763 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
12764 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
12766 template_id_p
= true;
12767 template_args
= TREE_OPERAND (fns
, 1);
12768 fns
= TREE_OPERAND (fns
, 0);
12770 template_args
= tsubst_template_args (template_args
, args
,
12771 complain
, in_decl
);
12773 name
= DECL_NAME (get_first_fn (fns
));
12774 if (IDENTIFIER_TYPENAME_P (name
))
12775 name
= mangle_conv_op_name_for_type (optype
);
12776 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
12778 return error_mark_node
;
12780 /* If lookup found a single function, mark it as used at this
12781 point. (If it lookup found multiple functions the one selected
12782 later by overload resolution will be marked as used at that
12784 if (BASELINK_P (baselink
))
12785 fns
= BASELINK_FUNCTIONS (baselink
);
12786 if (!template_id_p
&& !really_overloaded_fn (fns
))
12787 mark_used (OVL_CURRENT (fns
));
12789 /* Add back the template arguments, if present. */
12790 if (BASELINK_P (baselink
) && template_id_p
)
12791 BASELINK_FUNCTIONS (baselink
)
12792 = build_nt (TEMPLATE_ID_EXPR
,
12793 BASELINK_FUNCTIONS (baselink
),
12795 /* Update the conversion operator type. */
12796 BASELINK_OPTYPE (baselink
) = optype
;
12799 object_type
= current_class_type
;
12802 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
12808 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12809 true if the qualified-id will be a postfix-expression in-and-of
12810 itself; false if more of the postfix-expression follows the
12811 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12815 tsubst_qualified_id (tree qualified_id
, tree args
,
12816 tsubst_flags_t complain
, tree in_decl
,
12817 bool done
, bool address_p
)
12823 tree template_args
;
12824 location_t loc
= UNKNOWN_LOCATION
;
12826 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
12828 /* Figure out what name to look up. */
12829 name
= TREE_OPERAND (qualified_id
, 1);
12830 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
12832 is_template
= true;
12833 loc
= EXPR_LOCATION (name
);
12834 template_args
= TREE_OPERAND (name
, 1);
12836 template_args
= tsubst_template_args (template_args
, args
,
12837 complain
, in_decl
);
12838 name
= TREE_OPERAND (name
, 0);
12842 is_template
= false;
12843 template_args
= NULL_TREE
;
12846 /* Substitute into the qualifying scope. When there are no ARGS, we
12847 are just trying to simplify a non-dependent expression. In that
12848 case the qualifying scope may be dependent, and, in any case,
12849 substituting will not help. */
12850 scope
= TREE_OPERAND (qualified_id
, 0);
12853 scope
= tsubst (scope
, args
, complain
, in_decl
);
12854 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
12859 if (dependent_scope_p (scope
))
12862 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
12863 return build_qualified_name (NULL_TREE
, scope
, expr
,
12864 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
12867 if (!BASELINK_P (name
) && !DECL_P (expr
))
12869 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
12871 /* A BIT_NOT_EXPR is used to represent a destructor. */
12872 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
12874 error ("qualifying type %qT does not match destructor name ~%qT",
12875 scope
, TREE_OPERAND (expr
, 0));
12876 expr
= error_mark_node
;
12879 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
12880 /*is_type_p=*/0, false);
12883 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
12884 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
12885 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
12887 if (complain
& tf_error
)
12889 error ("dependent-name %qE is parsed as a non-type, but "
12890 "instantiation yields a type", qualified_id
);
12891 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
12893 return error_mark_node
;
12899 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
12901 /* Remember that there was a reference to this entity. */
12905 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
12907 if (complain
& tf_error
)
12908 qualified_name_lookup_error (scope
,
12909 TREE_OPERAND (qualified_id
, 1),
12910 expr
, input_location
);
12911 return error_mark_node
;
12915 expr
= lookup_template_function (expr
, template_args
);
12917 if (expr
== error_mark_node
&& complain
& tf_error
)
12918 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
12919 expr
, input_location
);
12920 else if (TYPE_P (scope
))
12922 expr
= (adjust_result_of_qualified_name_lookup
12923 (expr
, scope
, current_nonlambda_class_type ()));
12924 expr
= (finish_qualified_id_expr
12925 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
12926 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
12927 /*template_arg_p=*/false, complain
));
12930 /* Expressions do not generally have reference type. */
12931 if (TREE_CODE (expr
) != SCOPE_REF
12932 /* However, if we're about to form a pointer-to-member, we just
12933 want the referenced member referenced. */
12934 && TREE_CODE (expr
) != OFFSET_REF
)
12935 expr
= convert_from_reference (expr
);
12940 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
12941 initializer, DECL is the substituted VAR_DECL. Other arguments are as
12945 tsubst_init (tree init
, tree decl
, tree args
,
12946 tsubst_flags_t complain
, tree in_decl
)
12951 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
12955 /* If we had an initializer but it
12956 instantiated to nothing,
12957 value-initialize the object. This will
12958 only occur when the initializer was a
12959 pack expansion where the parameter packs
12960 used in that expansion were of length
12962 init
= build_value_init (TREE_TYPE (decl
),
12964 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
12965 init
= get_target_expr_sfinae (init
, complain
);
12971 /* Like tsubst, but deals with expressions. This function just replaces
12972 template parms; to finish processing the resultant expression, use
12973 tsubst_copy_and_build or tsubst_expr. */
12976 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12978 enum tree_code code
;
12981 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
12984 code
= TREE_CODE (t
);
12989 r
= retrieve_local_specialization (t
);
12991 if (r
== NULL_TREE
)
12993 /* We get here for a use of 'this' in an NSDMI. */
12994 if (DECL_NAME (t
) == this_identifier
12995 && current_function_decl
12996 && DECL_CONSTRUCTOR_P (current_function_decl
))
12997 return current_class_ptr
;
12999 /* This can happen for a parameter name used later in a function
13000 declaration (such as in a late-specified return type). Just
13001 make a dummy decl, since it's only used for its type. */
13002 gcc_assert (cp_unevaluated_operand
!= 0);
13003 r
= tsubst_decl (t
, args
, complain
);
13004 /* Give it the template pattern as its context; its true context
13005 hasn't been instantiated yet and this is good enough for
13007 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
13010 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
13011 r
= ARGUMENT_PACK_SELECT_ARG (r
);
13020 if (DECL_TEMPLATE_PARM_P (t
))
13021 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
13022 /* There is no need to substitute into namespace-scope
13024 if (DECL_NAMESPACE_SCOPE_P (t
))
13026 /* If ARGS is NULL, then T is known to be non-dependent. */
13027 if (args
== NULL_TREE
)
13028 return scalar_constant_value (t
);
13030 /* Unfortunately, we cannot just call lookup_name here.
13033 template <int I> int f() {
13035 struct S { void g() { E e = a; } };
13038 When we instantiate f<7>::S::g(), say, lookup_name is not
13039 clever enough to find f<7>::a. */
13041 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
13042 /*entering_scope=*/0);
13044 for (v
= TYPE_VALUES (enum_type
);
13046 v
= TREE_CHAIN (v
))
13047 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
13048 return TREE_VALUE (v
);
13050 /* We didn't find the name. That should never happen; if
13051 name-lookup found it during preliminary parsing, we
13052 should find it again here during instantiation. */
13053 gcc_unreachable ();
13058 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
13060 /* Check for a local specialization set up by
13061 tsubst_pack_expansion. */
13062 if (tree r
= retrieve_local_specialization (t
))
13064 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
13065 r
= ARGUMENT_PACK_SELECT_ARG (r
);
13069 /* When retrieving a capture pack from a generic lambda, remove the
13070 lambda call op's own template argument list from ARGS. Only the
13071 template arguments active for the closure type should be used to
13072 retrieve the pack specialization. */
13073 if (LAMBDA_FUNCTION_P (current_function_decl
)
13074 && (template_class_depth (DECL_CONTEXT (t
))
13075 != TMPL_ARGS_DEPTH (args
)))
13076 args
= strip_innermost_template_args (args
, 1);
13078 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
13079 tsubst_decl put in the hash table. */
13080 return retrieve_specialization (t
, args
, 0);
13083 if (DECL_CONTEXT (t
))
13087 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
13088 /*entering_scope=*/1);
13089 if (ctx
!= DECL_CONTEXT (t
))
13091 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
13094 if (complain
& tf_error
)
13095 error ("using invalid field %qD", t
);
13096 return error_mark_node
;
13105 case FUNCTION_DECL
:
13106 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
13107 r
= tsubst (t
, args
, complain
, in_decl
);
13108 else if (local_variable_p (t
))
13110 r
= retrieve_local_specialization (t
);
13111 if (r
== NULL_TREE
)
13113 /* First try name lookup to find the instantiation. */
13114 r
= lookup_name (DECL_NAME (t
));
13117 /* Make sure that the one we found is the one we want. */
13118 tree ctx
= DECL_CONTEXT (t
);
13119 if (DECL_LANG_SPECIFIC (ctx
) && DECL_TEMPLATE_INFO (ctx
))
13120 ctx
= tsubst (ctx
, args
, complain
, in_decl
);
13121 if (ctx
!= DECL_CONTEXT (r
))
13129 /* This can happen for a variable used in a
13130 late-specified return type of a local lambda, or for a
13131 local static or constant. Building a new VAR_DECL
13132 should be OK in all those cases. */
13133 r
= tsubst_decl (t
, args
, complain
);
13134 if (decl_maybe_constant_var_p (r
))
13136 /* We can't call cp_finish_decl, so handle the
13137 initializer by hand. */
13138 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
13139 complain
, in_decl
);
13140 if (!processing_template_decl
)
13141 init
= maybe_constant_init (init
);
13142 if (processing_template_decl
13143 ? potential_constant_expression (init
)
13144 : reduced_constant_expression_p (init
))
13145 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
13146 = TREE_CONSTANT (r
) = true;
13147 DECL_INITIAL (r
) = init
;
13149 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
13150 || decl_constant_var_p (r
)
13151 || errorcount
|| sorrycount
);
13152 if (!processing_template_decl
)
13154 if (TREE_STATIC (r
))
13155 rest_of_decl_compilation (r
, toplevel_bindings_p (),
13158 r
= process_outer_var_ref (r
, complain
);
13161 /* Remember this for subsequent uses. */
13162 if (local_specializations
)
13163 register_local_specialization (r
, t
);
13168 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
13169 return error_mark_node
;
13172 case NAMESPACE_DECL
:
13176 /* An OVERLOAD will always be a non-dependent overload set; an
13177 overload set from function scope will just be represented with an
13178 IDENTIFIER_NODE, and from class scope with a BASELINK. */
13179 gcc_assert (!uses_template_parms (t
));
13183 return tsubst_baselink (t
, current_nonlambda_class_type (),
13184 args
, complain
, in_decl
);
13186 case TEMPLATE_DECL
:
13187 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
13188 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
13189 args
, complain
, in_decl
);
13190 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
13191 return tsubst (t
, args
, complain
, in_decl
);
13192 else if (DECL_CLASS_SCOPE_P (t
)
13193 && uses_template_parms (DECL_CONTEXT (t
)))
13195 /* Template template argument like the following example need
13198 template <template <class> class TT> struct C {};
13199 template <class T> struct D {
13200 template <class U> struct E {};
13205 We are processing the template argument `E' in #1 for
13206 the template instantiation #2. Originally, `E' is a
13207 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
13208 have to substitute this with one having context `D<int>'. */
13210 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
13211 return lookup_field (context
, DECL_NAME(t
), 0, false);
13214 /* Ordinary template template argument. */
13218 case REINTERPRET_CAST_EXPR
:
13219 case CONST_CAST_EXPR
:
13220 case STATIC_CAST_EXPR
:
13221 case DYNAMIC_CAST_EXPR
:
13222 case IMPLICIT_CONV_EXPR
:
13226 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13227 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13228 return build1 (code
, type
, op0
);
13232 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
13234 tree expanded
, op
= TREE_OPERAND (t
, 0);
13237 if (SIZEOF_EXPR_TYPE_P (t
))
13238 op
= TREE_TYPE (op
);
13240 ++cp_unevaluated_operand
;
13241 ++c_inhibit_evaluation_warnings
;
13242 /* We only want to compute the number of arguments. */
13243 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
13244 --cp_unevaluated_operand
;
13245 --c_inhibit_evaluation_warnings
;
13247 if (TREE_CODE (expanded
) == TREE_VEC
)
13248 len
= TREE_VEC_LENGTH (expanded
);
13250 if (expanded
== error_mark_node
)
13251 return error_mark_node
;
13252 else if (PACK_EXPANSION_P (expanded
)
13253 || (TREE_CODE (expanded
) == TREE_VEC
13255 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
13257 if (TREE_CODE (expanded
) == TREE_VEC
)
13258 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
13260 PACK_EXPANSION_SIZEOF_P (expanded
) = true;
13262 if (TYPE_P (expanded
))
13263 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
13264 complain
& tf_error
);
13266 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
13267 complain
& tf_error
);
13270 return build_int_cst (size_type_node
, len
);
13272 if (SIZEOF_EXPR_TYPE_P (t
))
13274 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
13275 args
, complain
, in_decl
);
13276 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
13277 r
= build1 (SIZEOF_EXPR
,
13278 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
13279 SIZEOF_EXPR_TYPE_P (r
) = 1;
13286 case TRUTH_NOT_EXPR
:
13289 case UNARY_PLUS_EXPR
: /* Unary + */
13291 case AT_ENCODE_EXPR
:
13295 case REALPART_EXPR
:
13296 case IMAGPART_EXPR
:
13299 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13300 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13301 return build1 (code
, type
, op0
);
13304 case COMPONENT_REF
:
13309 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13310 name
= TREE_OPERAND (t
, 1);
13311 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
13313 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
13314 complain
, in_decl
);
13315 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
13317 else if (TREE_CODE (name
) == SCOPE_REF
13318 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
13320 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
13321 complain
, in_decl
);
13322 name
= TREE_OPERAND (name
, 1);
13323 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
13324 complain
, in_decl
);
13325 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
13326 name
= build_qualified_name (/*type=*/NULL_TREE
,
13328 /*template_p=*/false);
13330 else if (BASELINK_P (name
))
13331 name
= tsubst_baselink (name
,
13332 non_reference (TREE_TYPE (object
)),
13336 name
= tsubst_copy (name
, args
, complain
, in_decl
);
13337 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
13343 case TRUNC_DIV_EXPR
:
13344 case CEIL_DIV_EXPR
:
13345 case FLOOR_DIV_EXPR
:
13346 case ROUND_DIV_EXPR
:
13347 case EXACT_DIV_EXPR
:
13351 case TRUNC_MOD_EXPR
:
13352 case FLOOR_MOD_EXPR
:
13353 case TRUTH_ANDIF_EXPR
:
13354 case TRUTH_ORIF_EXPR
:
13355 case TRUTH_AND_EXPR
:
13356 case TRUTH_OR_EXPR
:
13369 case COMPOUND_EXPR
:
13372 case PREDECREMENT_EXPR
:
13373 case PREINCREMENT_EXPR
:
13374 case POSTDECREMENT_EXPR
:
13375 case POSTINCREMENT_EXPR
:
13377 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13378 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13379 return build_nt (code
, op0
, op1
);
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_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
13387 QUALIFIED_NAME_IS_TEMPLATE (t
));
13392 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13393 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13394 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
13399 int n
= VL_EXP_OPERAND_LENGTH (t
);
13400 tree result
= build_vl_exp (CALL_EXPR
, n
);
13402 for (i
= 0; i
< n
; i
++)
13403 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
13404 complain
, in_decl
);
13410 case PSEUDO_DTOR_EXPR
:
13411 case VEC_PERM_EXPR
:
13413 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13414 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13415 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
13416 r
= build_nt (code
, op0
, op1
, op2
);
13417 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13423 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13424 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13425 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
13426 r
= build_nt (code
, op0
, op1
, op2
);
13427 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
13433 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13434 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13435 r
= build_nt (code
, op0
, op1
);
13436 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
13437 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
13441 case TEMPLATE_ID_EXPR
:
13443 /* Substituted template arguments */
13444 tree fn
= TREE_OPERAND (t
, 0);
13445 tree targs
= TREE_OPERAND (t
, 1);
13447 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
13449 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
13451 return lookup_template_function (fn
, targs
);
13456 tree purpose
, value
, chain
;
13458 if (t
== void_list_node
)
13461 purpose
= TREE_PURPOSE (t
);
13463 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
13464 value
= TREE_VALUE (t
);
13466 value
= tsubst_copy (value
, args
, complain
, in_decl
);
13467 chain
= TREE_CHAIN (t
);
13468 if (chain
&& chain
!= void_type_node
)
13469 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
13470 if (purpose
== TREE_PURPOSE (t
)
13471 && value
== TREE_VALUE (t
)
13472 && chain
== TREE_CHAIN (t
))
13474 return tree_cons (purpose
, value
, chain
);
13479 case ENUMERAL_TYPE
:
13481 case TEMPLATE_TYPE_PARM
:
13482 case TEMPLATE_TEMPLATE_PARM
:
13483 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13484 case TEMPLATE_PARM_INDEX
:
13486 case REFERENCE_TYPE
:
13488 case FUNCTION_TYPE
:
13491 case TYPENAME_TYPE
:
13492 case UNBOUND_CLASS_TEMPLATE
:
13494 case DECLTYPE_TYPE
:
13496 return tsubst (t
, args
, complain
, in_decl
);
13500 /* Fall through. */
13501 case IDENTIFIER_NODE
:
13502 if (IDENTIFIER_TYPENAME_P (t
))
13504 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13505 return mangle_conv_op_name_for_type (new_type
);
13511 /* This is handled by tsubst_copy_and_build. */
13512 gcc_unreachable ();
13516 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13517 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13518 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
13521 case CLEANUP_POINT_EXPR
:
13522 /* We shouldn't have built any of these during initial template
13523 generation. Instead, they should be built during instantiation
13524 in response to the saved STMT_IS_FULL_EXPR_P setting. */
13525 gcc_unreachable ();
13529 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13530 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
13531 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
13532 r
= build2 (code
, type
, op0
, op1
);
13533 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
13534 mark_used (TREE_OPERAND (r
, 1));
13538 case EXPR_PACK_EXPANSION
:
13539 error ("invalid use of pack expansion expression");
13540 return error_mark_node
;
13542 case NONTYPE_ARGUMENT_PACK
:
13543 error ("use %<...%> to expand argument pack");
13544 return error_mark_node
;
13547 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
13555 /* Instantiate any typedefs in the type. */
13556 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13557 r
= fold_convert (type
, t
);
13558 gcc_assert (TREE_CODE (r
) == code
);
13563 /* These can sometimes show up in a partial instantiation, but never
13564 involve template parms. */
13565 gcc_assert (!uses_template_parms (t
));
13569 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
13570 gcc_checking_assert (false);
13575 /* Helper function for tsubst_omp_clauses, used for instantiation of
13576 OMP_CLAUSE_DECL of clauses that handles also OpenMP array sections
13577 represented with TREE_LIST. */
13580 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
13583 if (TREE_CODE (decl
) == TREE_LIST
)
13586 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
13587 /*integral_constant_expression_p=*/false);
13588 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
13589 /*integral_constant_expression_p=*/false);
13590 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
13592 if (TREE_PURPOSE (decl
) == low_bound
13593 && TREE_VALUE (decl
) == length
13594 && TREE_CHAIN (decl
) == chain
)
13596 return tree_cons (low_bound
, length
, chain
);
13598 return tsubst_copy (decl
, args
, complain
, in_decl
);
13601 /* Like tsubst_copy, but specifically for OpenMP clauses. */
13604 tsubst_omp_clauses (tree clauses
, bool declare_simd
,
13605 tree args
, tsubst_flags_t complain
, tree in_decl
)
13607 tree new_clauses
= NULL
, nc
, oc
;
13609 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
13611 nc
= copy_node (oc
);
13612 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
13615 switch (OMP_CLAUSE_CODE (nc
))
13617 case OMP_CLAUSE_LASTPRIVATE
:
13618 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
13620 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
13621 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
13622 in_decl
, /*integral_constant_expression_p=*/false);
13623 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
13624 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
13627 case OMP_CLAUSE_PRIVATE
:
13628 case OMP_CLAUSE_SHARED
:
13629 case OMP_CLAUSE_FIRSTPRIVATE
:
13630 case OMP_CLAUSE_COPYIN
:
13631 case OMP_CLAUSE_COPYPRIVATE
:
13632 case OMP_CLAUSE_UNIFORM
:
13633 OMP_CLAUSE_DECL (nc
) = tsubst_copy (OMP_CLAUSE_DECL (oc
), args
,
13634 complain
, in_decl
);
13636 case OMP_CLAUSE_DEPEND
:
13637 case OMP_CLAUSE_FROM
:
13638 case OMP_CLAUSE_TO
:
13639 case OMP_CLAUSE_MAP
:
13640 OMP_CLAUSE_DECL (nc
)
13641 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
13644 case OMP_CLAUSE_IF
:
13645 case OMP_CLAUSE_NUM_THREADS
:
13646 case OMP_CLAUSE_SCHEDULE
:
13647 case OMP_CLAUSE_COLLAPSE
:
13648 case OMP_CLAUSE_FINAL
:
13649 case OMP_CLAUSE_DEVICE
:
13650 case OMP_CLAUSE_DIST_SCHEDULE
:
13651 case OMP_CLAUSE_NUM_TEAMS
:
13652 case OMP_CLAUSE_THREAD_LIMIT
:
13653 case OMP_CLAUSE_SAFELEN
:
13654 case OMP_CLAUSE_SIMDLEN
:
13655 OMP_CLAUSE_OPERAND (nc
, 0)
13656 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
13657 in_decl
, /*integral_constant_expression_p=*/false);
13659 case OMP_CLAUSE_REDUCTION
:
13660 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
13662 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
13663 if (TREE_CODE (placeholder
) == SCOPE_REF
)
13665 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
13666 complain
, in_decl
);
13667 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
13668 = build_qualified_name (NULL_TREE
, scope
,
13669 TREE_OPERAND (placeholder
, 1),
13673 gcc_assert (identifier_p (placeholder
));
13675 OMP_CLAUSE_DECL (nc
) = tsubst_copy (OMP_CLAUSE_DECL (oc
), args
,
13676 complain
, in_decl
);
13678 case OMP_CLAUSE_LINEAR
:
13679 case OMP_CLAUSE_ALIGNED
:
13680 OMP_CLAUSE_DECL (nc
) = tsubst_copy (OMP_CLAUSE_DECL (oc
), args
,
13681 complain
, in_decl
);
13682 OMP_CLAUSE_OPERAND (nc
, 1)
13683 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
13684 in_decl
, /*integral_constant_expression_p=*/false);
13686 case OMP_CLAUSE_NOWAIT
:
13687 case OMP_CLAUSE_ORDERED
:
13688 case OMP_CLAUSE_DEFAULT
:
13689 case OMP_CLAUSE_UNTIED
:
13690 case OMP_CLAUSE_MERGEABLE
:
13691 case OMP_CLAUSE_INBRANCH
:
13692 case OMP_CLAUSE_NOTINBRANCH
:
13693 case OMP_CLAUSE_PROC_BIND
:
13694 case OMP_CLAUSE_FOR
:
13695 case OMP_CLAUSE_PARALLEL
:
13696 case OMP_CLAUSE_SECTIONS
:
13697 case OMP_CLAUSE_TASKGROUP
:
13700 gcc_unreachable ();
13704 new_clauses
= nreverse (new_clauses
);
13706 new_clauses
= finish_omp_clauses (new_clauses
);
13707 return new_clauses
;
13710 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
13713 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
13716 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
13718 tree purpose
, value
, chain
;
13723 if (TREE_CODE (t
) != TREE_LIST
)
13724 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13725 /*function_p=*/false,
13726 /*integral_constant_expression_p=*/false);
13728 if (t
== void_list_node
)
13731 purpose
= TREE_PURPOSE (t
);
13733 purpose
= RECUR (purpose
);
13734 value
= TREE_VALUE (t
);
13737 if (TREE_CODE (value
) != LABEL_DECL
)
13738 value
= RECUR (value
);
13741 value
= lookup_label (DECL_NAME (value
));
13742 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
13743 TREE_USED (value
) = 1;
13746 chain
= TREE_CHAIN (t
);
13747 if (chain
&& chain
!= void_type_node
)
13748 chain
= RECUR (chain
);
13749 return tree_cons (purpose
, value
, chain
);
13753 /* Substitute one OMP_FOR iterator. */
13756 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
13757 tree condv
, tree incrv
, tree
*clauses
,
13758 tree args
, tsubst_flags_t complain
, tree in_decl
,
13759 bool integral_constant_expression_p
)
13761 #define RECUR(NODE) \
13762 tsubst_expr ((NODE), args, complain, in_decl, \
13763 integral_constant_expression_p)
13764 tree decl
, init
, cond
, incr
;
13766 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
13767 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
13768 decl
= TREE_OPERAND (init
, 0);
13769 init
= TREE_OPERAND (init
, 1);
13770 tree decl_expr
= NULL_TREE
;
13771 if (init
&& TREE_CODE (init
) == DECL_EXPR
)
13773 /* We need to jump through some hoops to handle declarations in the
13774 for-init-statement, since we might need to handle auto deduction,
13775 but we need to keep control of initialization. */
13777 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
13778 decl
= tsubst_decl (decl
, args
, complain
);
13781 decl
= RECUR (decl
);
13782 init
= RECUR (init
);
13784 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
13785 if (auto_node
&& init
)
13787 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
);
13789 gcc_assert (!type_dependent_expression_p (decl
));
13791 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
13795 /* Declare the variable, but don't let that initialize it. */
13796 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
13797 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
13799 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
13802 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
13803 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
13804 if (TREE_CODE (incr
) == MODIFY_EXPR
)
13806 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
13807 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
13808 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
13809 NOP_EXPR
, rhs
, complain
);
13812 incr
= RECUR (incr
);
13813 TREE_VEC_ELT (declv
, i
) = decl
;
13814 TREE_VEC_ELT (initv
, i
) = init
;
13815 TREE_VEC_ELT (condv
, i
) = cond
;
13816 TREE_VEC_ELT (incrv
, i
) = incr
;
13822 /* Declare and initialize the variable. */
13829 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
13831 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
13832 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
13833 && OMP_CLAUSE_DECL (c
) == decl
)
13835 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
13836 && OMP_CLAUSE_DECL (c
) == decl
)
13837 error ("iteration variable %qD should not be firstprivate", decl
);
13838 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
13839 && OMP_CLAUSE_DECL (c
) == decl
)
13840 error ("iteration variable %qD should not be reduction", decl
);
13844 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
13845 OMP_CLAUSE_DECL (c
) = decl
;
13846 c
= finish_omp_clauses (c
);
13849 OMP_CLAUSE_CHAIN (c
) = *clauses
;
13854 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
13855 if (COMPARISON_CLASS_P (cond
))
13857 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
13858 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
13859 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
13862 cond
= RECUR (cond
);
13863 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
13864 switch (TREE_CODE (incr
))
13866 case PREINCREMENT_EXPR
:
13867 case PREDECREMENT_EXPR
:
13868 case POSTINCREMENT_EXPR
:
13869 case POSTDECREMENT_EXPR
:
13870 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
13871 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
13874 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
13875 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
13877 tree rhs
= TREE_OPERAND (incr
, 1);
13878 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
13879 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
13880 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
13881 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
13882 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
13886 incr
= RECUR (incr
);
13889 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
13890 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
13892 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
13893 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
13894 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
13895 TREE_TYPE (decl
), lhs
,
13896 RECUR (TREE_OPERAND (incr
, 2))));
13898 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
13899 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
13900 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
13902 tree rhs
= TREE_OPERAND (incr
, 2);
13903 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
13904 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
13905 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
13906 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
13907 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
13911 incr
= RECUR (incr
);
13914 incr
= RECUR (incr
);
13918 TREE_VEC_ELT (declv
, i
) = decl
;
13919 TREE_VEC_ELT (initv
, i
) = init
;
13920 TREE_VEC_ELT (condv
, i
) = cond
;
13921 TREE_VEC_ELT (incrv
, i
) = incr
;
13925 /* Like tsubst_copy for expressions, etc. but also does semantic
13929 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
13930 bool integral_constant_expression_p
)
13932 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13933 #define RECUR(NODE) \
13934 tsubst_expr ((NODE), args, complain, in_decl, \
13935 integral_constant_expression_p)
13941 if (t
== NULL_TREE
|| t
== error_mark_node
)
13944 loc
= input_location
;
13945 if (EXPR_HAS_LOCATION (t
))
13946 input_location
= EXPR_LOCATION (t
);
13947 if (STATEMENT_CODE_P (TREE_CODE (t
)))
13948 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
13950 switch (TREE_CODE (t
))
13952 case STATEMENT_LIST
:
13954 tree_stmt_iterator i
;
13955 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
13956 RECUR (tsi_stmt (i
));
13960 case CTOR_INITIALIZER
:
13961 finish_mem_initializers (tsubst_initializer_list
13962 (TREE_OPERAND (t
, 0), args
));
13966 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
13970 tmp
= RECUR (EXPR_STMT_EXPR (t
));
13971 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
13972 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
13974 finish_expr_stmt (tmp
);
13978 do_using_directive (USING_STMT_NAMESPACE (t
));
13983 tree decl
, pattern_decl
;
13986 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
13987 if (TREE_CODE (decl
) == LABEL_DECL
)
13988 finish_label_decl (DECL_NAME (decl
));
13989 else if (TREE_CODE (decl
) == USING_DECL
)
13991 tree scope
= USING_DECL_SCOPE (decl
);
13992 tree name
= DECL_NAME (decl
);
13995 scope
= tsubst (scope
, args
, complain
, in_decl
);
13996 decl
= lookup_qualified_name (scope
, name
,
13997 /*is_type_p=*/false,
13998 /*complain=*/false);
13999 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
14000 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
14002 do_local_using_decl (decl
, scope
, name
);
14004 else if (DECL_PACK_P (decl
))
14006 /* Don't build up decls for a variadic capture proxy, we'll
14007 instantiate the elements directly as needed. */
14012 init
= DECL_INITIAL (decl
);
14013 decl
= tsubst (decl
, args
, complain
, in_decl
);
14014 if (decl
!= error_mark_node
)
14016 /* By marking the declaration as instantiated, we avoid
14017 trying to instantiate it. Since instantiate_decl can't
14018 handle local variables, and since we've already done
14019 all that needs to be done, that's the right thing to
14022 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
14024 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
14025 /* Anonymous aggregates are a special case. */
14026 finish_anon_union (decl
);
14027 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
14029 DECL_CONTEXT (decl
) = current_function_decl
;
14030 if (DECL_NAME (decl
) == this_identifier
)
14032 tree lam
= DECL_CONTEXT (current_function_decl
);
14033 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
14034 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
14036 insert_capture_proxy (decl
);
14038 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
14039 /* We already did a pushtag. */;
14040 else if (TREE_CODE (decl
) == FUNCTION_DECL
14041 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
14042 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
14044 DECL_CONTEXT (decl
) = NULL_TREE
;
14046 DECL_CONTEXT (decl
) = current_function_decl
;
14047 cp_check_omp_declare_reduction (decl
);
14051 int const_init
= false;
14052 maybe_push_decl (decl
);
14054 && DECL_PRETTY_FUNCTION_P (decl
))
14056 /* For __PRETTY_FUNCTION__ we have to adjust the
14058 const char *const name
14059 = cxx_printable_name (current_function_decl
, 2);
14060 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
14063 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
14066 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
14068 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
14077 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
14078 RECUR (FOR_INIT_STMT (t
));
14079 finish_for_init_stmt (stmt
);
14080 tmp
= RECUR (FOR_COND (t
));
14081 finish_for_cond (tmp
, stmt
, false);
14082 tmp
= RECUR (FOR_EXPR (t
));
14083 finish_for_expr (tmp
, stmt
);
14084 RECUR (FOR_BODY (t
));
14085 finish_for_stmt (stmt
);
14088 case RANGE_FOR_STMT
:
14091 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
14092 decl
= RANGE_FOR_DECL (t
);
14093 decl
= tsubst (decl
, args
, complain
, in_decl
);
14094 maybe_push_decl (decl
);
14095 expr
= RECUR (RANGE_FOR_EXPR (t
));
14096 stmt
= cp_convert_range_for (stmt
, decl
, expr
, RANGE_FOR_IVDEP (t
));
14097 RECUR (RANGE_FOR_BODY (t
));
14098 finish_for_stmt (stmt
);
14103 stmt
= begin_while_stmt ();
14104 tmp
= RECUR (WHILE_COND (t
));
14105 finish_while_stmt_cond (tmp
, stmt
, false);
14106 RECUR (WHILE_BODY (t
));
14107 finish_while_stmt (stmt
);
14111 stmt
= begin_do_stmt ();
14112 RECUR (DO_BODY (t
));
14113 finish_do_body (stmt
);
14114 tmp
= RECUR (DO_COND (t
));
14115 finish_do_stmt (tmp
, stmt
, false);
14119 stmt
= begin_if_stmt ();
14120 tmp
= RECUR (IF_COND (t
));
14121 finish_if_stmt_cond (tmp
, stmt
);
14122 RECUR (THEN_CLAUSE (t
));
14123 finish_then_clause (stmt
);
14125 if (ELSE_CLAUSE (t
))
14127 begin_else_clause (stmt
);
14128 RECUR (ELSE_CLAUSE (t
));
14129 finish_else_clause (stmt
);
14132 finish_if_stmt (stmt
);
14136 if (BIND_EXPR_BODY_BLOCK (t
))
14137 stmt
= begin_function_body ();
14139 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
14140 ? BCS_TRY_BLOCK
: 0);
14142 RECUR (BIND_EXPR_BODY (t
));
14144 if (BIND_EXPR_BODY_BLOCK (t
))
14145 finish_function_body (stmt
);
14147 finish_compound_stmt (stmt
);
14151 finish_break_stmt ();
14154 case CONTINUE_STMT
:
14155 finish_continue_stmt ();
14159 stmt
= begin_switch_stmt ();
14160 tmp
= RECUR (SWITCH_STMT_COND (t
));
14161 finish_switch_cond (tmp
, stmt
);
14162 RECUR (SWITCH_STMT_BODY (t
));
14163 finish_switch_stmt (stmt
);
14166 case CASE_LABEL_EXPR
:
14168 tree low
= RECUR (CASE_LOW (t
));
14169 tree high
= RECUR (CASE_HIGH (t
));
14170 finish_case_label (EXPR_LOCATION (t
), low
, high
);
14176 tree decl
= LABEL_EXPR_LABEL (t
);
14179 label
= finish_label_stmt (DECL_NAME (decl
));
14180 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
14181 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
14186 tmp
= GOTO_DESTINATION (t
);
14187 if (TREE_CODE (tmp
) != LABEL_DECL
)
14188 /* Computed goto's must be tsubst'd into. On the other hand,
14189 non-computed gotos must not be; the identifier in question
14190 will have no binding. */
14193 tmp
= DECL_NAME (tmp
);
14194 finish_goto_stmt (tmp
);
14199 tree string
= RECUR (ASM_STRING (t
));
14200 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
14201 complain
, in_decl
);
14202 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
14203 complain
, in_decl
);
14204 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
14205 complain
, in_decl
);
14206 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
14207 complain
, in_decl
);
14208 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
14210 tree asm_expr
= tmp
;
14211 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
14212 asm_expr
= TREE_OPERAND (asm_expr
, 0);
14213 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
14220 stmt
= begin_try_block ();
14221 RECUR (TRY_STMTS (t
));
14222 finish_cleanup_try_block (stmt
);
14223 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
14227 tree compound_stmt
= NULL_TREE
;
14229 if (FN_TRY_BLOCK_P (t
))
14230 stmt
= begin_function_try_block (&compound_stmt
);
14232 stmt
= begin_try_block ();
14234 RECUR (TRY_STMTS (t
));
14236 if (FN_TRY_BLOCK_P (t
))
14237 finish_function_try_block (stmt
);
14239 finish_try_block (stmt
);
14241 RECUR (TRY_HANDLERS (t
));
14242 if (FN_TRY_BLOCK_P (t
))
14243 finish_function_handler_sequence (stmt
, compound_stmt
);
14245 finish_handler_sequence (stmt
);
14251 tree decl
= HANDLER_PARMS (t
);
14255 decl
= tsubst (decl
, args
, complain
, in_decl
);
14256 /* Prevent instantiate_decl from trying to instantiate
14257 this variable. We've already done all that needs to be
14259 if (decl
!= error_mark_node
)
14260 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
14262 stmt
= begin_handler ();
14263 finish_handler_parms (decl
, stmt
);
14264 RECUR (HANDLER_BODY (t
));
14265 finish_handler (stmt
);
14270 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
14271 if (CLASS_TYPE_P (tmp
))
14273 /* Local classes are not independent templates; they are
14274 instantiated along with their containing function. And this
14275 way we don't have to deal with pushing out of one local class
14276 to instantiate a member of another local class. */
14278 /* Closures are handled by the LAMBDA_EXPR. */
14279 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
14280 complete_type (tmp
);
14281 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
14282 if (!DECL_ARTIFICIAL (fn
))
14283 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
14287 case STATIC_ASSERT
:
14291 ++c_inhibit_evaluation_warnings
;
14293 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
14296 /*integral_constant_expression_p=*/true);
14297 --c_inhibit_evaluation_warnings
;
14299 finish_static_assert (condition
,
14300 STATIC_ASSERT_MESSAGE (t
),
14301 STATIC_ASSERT_SOURCE_LOCATION (t
),
14302 /*member_p=*/false);
14307 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), false,
14308 args
, complain
, in_decl
);
14309 stmt
= begin_omp_parallel ();
14310 RECUR (OMP_PARALLEL_BODY (t
));
14311 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
14312 = OMP_PARALLEL_COMBINED (t
);
14316 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), false,
14317 args
, complain
, in_decl
);
14318 stmt
= begin_omp_task ();
14319 RECUR (OMP_TASK_BODY (t
));
14320 finish_omp_task (tmp
, stmt
);
14327 case OMP_DISTRIBUTE
:
14329 tree clauses
, body
, pre_body
;
14330 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
14331 tree incrv
= NULL_TREE
;
14334 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), false,
14335 args
, complain
, in_decl
);
14336 if (OMP_FOR_INIT (t
) != NULL_TREE
)
14338 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
14339 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
14340 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
14341 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
14344 stmt
= begin_omp_structured_block ();
14346 pre_body
= push_stmt_list ();
14347 RECUR (OMP_FOR_PRE_BODY (t
));
14348 pre_body
= pop_stmt_list (pre_body
);
14350 if (OMP_FOR_INIT (t
) != NULL_TREE
)
14351 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
14352 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
14353 &clauses
, args
, complain
, in_decl
,
14354 integral_constant_expression_p
);
14356 body
= push_stmt_list ();
14357 RECUR (OMP_FOR_BODY (t
));
14358 body
= pop_stmt_list (body
);
14360 if (OMP_FOR_INIT (t
) != NULL_TREE
)
14361 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
, initv
,
14362 condv
, incrv
, body
, pre_body
, clauses
);
14365 t
= make_node (TREE_CODE (t
));
14366 TREE_TYPE (t
) = void_type_node
;
14367 OMP_FOR_BODY (t
) = body
;
14368 OMP_FOR_PRE_BODY (t
) = pre_body
;
14369 OMP_FOR_CLAUSES (t
) = clauses
;
14370 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
14374 add_stmt (finish_omp_structured_block (stmt
));
14381 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false,
14382 args
, complain
, in_decl
);
14383 stmt
= push_stmt_list ();
14384 RECUR (OMP_BODY (t
));
14385 stmt
= pop_stmt_list (stmt
);
14388 OMP_BODY (t
) = stmt
;
14389 OMP_CLAUSES (t
) = tmp
;
14393 case OMP_TARGET_DATA
:
14395 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false,
14396 args
, complain
, in_decl
);
14397 keep_next_level (true);
14398 stmt
= begin_omp_structured_block ();
14400 RECUR (OMP_BODY (t
));
14401 stmt
= finish_omp_structured_block (stmt
);
14404 OMP_BODY (t
) = stmt
;
14405 OMP_CLAUSES (t
) = tmp
;
14409 case OMP_TARGET_UPDATE
:
14410 tmp
= tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t
), false,
14411 args
, complain
, in_decl
);
14413 OMP_TARGET_UPDATE_CLAUSES (t
) = tmp
;
14420 case OMP_TASKGROUP
:
14422 stmt
= push_stmt_list ();
14423 RECUR (OMP_BODY (t
));
14424 stmt
= pop_stmt_list (stmt
);
14427 OMP_BODY (t
) = stmt
;
14432 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
14433 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
14435 tree op1
= TREE_OPERAND (t
, 1);
14436 tree rhs1
= NULL_TREE
;
14438 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
14440 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
14441 op1
= TREE_OPERAND (op1
, 1);
14443 lhs
= RECUR (TREE_OPERAND (op1
, 0));
14444 rhs
= RECUR (TREE_OPERAND (op1
, 1));
14445 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
14446 NULL_TREE
, NULL_TREE
, rhs1
,
14447 OMP_ATOMIC_SEQ_CST (t
));
14451 tree op1
= TREE_OPERAND (t
, 1);
14452 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
14453 tree rhs1
= NULL_TREE
;
14454 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
14455 enum tree_code opcode
= NOP_EXPR
;
14456 if (code
== OMP_ATOMIC_READ
)
14458 v
= RECUR (TREE_OPERAND (op1
, 0));
14459 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
14461 else if (code
== OMP_ATOMIC_CAPTURE_OLD
14462 || code
== OMP_ATOMIC_CAPTURE_NEW
)
14464 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
14465 v
= RECUR (TREE_OPERAND (op1
, 0));
14466 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
14467 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
14469 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
14470 op11
= TREE_OPERAND (op11
, 1);
14472 lhs
= RECUR (TREE_OPERAND (op11
, 0));
14473 rhs
= RECUR (TREE_OPERAND (op11
, 1));
14474 opcode
= TREE_CODE (op11
);
14475 if (opcode
== MODIFY_EXPR
)
14481 lhs
= RECUR (TREE_OPERAND (op1
, 0));
14482 rhs
= RECUR (TREE_OPERAND (op1
, 1));
14484 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
14485 OMP_ATOMIC_SEQ_CST (t
));
14489 case TRANSACTION_EXPR
:
14492 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
14493 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
14495 if (TRANSACTION_EXPR_IS_STMT (t
))
14497 tree body
= TRANSACTION_EXPR_BODY (t
);
14498 tree noex
= NULL_TREE
;
14499 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
14501 noex
= MUST_NOT_THROW_COND (body
);
14502 if (noex
== NULL_TREE
)
14503 noex
= boolean_true_node
;
14504 body
= TREE_OPERAND (body
, 0);
14506 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
14508 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
14512 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
14513 RECUR (TRANSACTION_EXPR_BODY (t
)),
14520 case MUST_NOT_THROW_EXPR
:
14522 tree op0
= RECUR (TREE_OPERAND (t
, 0));
14523 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
14524 RETURN (build_must_not_throw_expr (op0
, cond
));
14527 case EXPR_PACK_EXPANSION
:
14528 error ("invalid use of pack expansion expression");
14529 RETURN (error_mark_node
);
14531 case NONTYPE_ARGUMENT_PACK
:
14532 error ("use %<...%> to expand argument pack");
14533 RETURN (error_mark_node
);
14535 case CILK_SPAWN_STMT
:
14536 cfun
->calls_cilk_spawn
= 1;
14537 RETURN (build_cilk_spawn (EXPR_LOCATION (t
), RECUR (CILK_SPAWN_FN (t
))));
14539 case CILK_SYNC_STMT
:
14540 RETURN (build_cilk_sync ());
14542 case COMPOUND_EXPR
:
14543 tmp
= RECUR (TREE_OPERAND (t
, 0));
14544 if (tmp
== NULL_TREE
)
14545 /* If the first operand was a statement, we're done with it. */
14546 RETURN (RECUR (TREE_OPERAND (t
, 1)));
14547 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
14548 RECUR (TREE_OPERAND (t
, 1)),
14551 case ANNOTATE_EXPR
:
14552 tmp
= RECUR (TREE_OPERAND (t
, 0));
14553 RETURN (build2_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
14554 TREE_TYPE (tmp
), tmp
, RECUR (TREE_OPERAND (t
, 1))));
14557 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
14559 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
14560 /*function_p=*/false,
14561 integral_constant_expression_p
));
14564 RETURN (NULL_TREE
);
14566 input_location
= loc
;
14572 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
14573 function. For description of the body see comment above
14574 cp_parser_omp_declare_reduction_exprs. */
14577 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14579 if (t
== NULL_TREE
|| t
== error_mark_node
)
14582 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
14584 tree_stmt_iterator tsi
;
14587 memset (stmts
, 0, sizeof stmts
);
14588 for (i
= 0, tsi
= tsi_start (t
);
14589 i
< 7 && !tsi_end_p (tsi
);
14590 i
++, tsi_next (&tsi
))
14591 stmts
[i
] = tsi_stmt (tsi
);
14592 gcc_assert (tsi_end_p (tsi
));
14596 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
14597 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
14598 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
14599 args
, complain
, in_decl
);
14600 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
14601 args
, complain
, in_decl
);
14602 DECL_CONTEXT (omp_out
) = current_function_decl
;
14603 DECL_CONTEXT (omp_in
) = current_function_decl
;
14604 keep_next_level (true);
14605 tree block
= begin_omp_structured_block ();
14606 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
14607 block
= finish_omp_structured_block (block
);
14608 block
= maybe_cleanup_point_expr_void (block
);
14609 add_decl_expr (omp_out
);
14610 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
14611 TREE_NO_WARNING (omp_out
) = 1;
14612 add_decl_expr (omp_in
);
14613 finish_expr_stmt (block
);
14617 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
14618 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
14619 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
14620 args
, complain
, in_decl
);
14621 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
14622 args
, complain
, in_decl
);
14623 DECL_CONTEXT (omp_priv
) = current_function_decl
;
14624 DECL_CONTEXT (omp_orig
) = current_function_decl
;
14625 keep_next_level (true);
14626 tree block
= begin_omp_structured_block ();
14627 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
14628 block
= finish_omp_structured_block (block
);
14629 block
= maybe_cleanup_point_expr_void (block
);
14630 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
14631 add_decl_expr (omp_priv
);
14632 add_decl_expr (omp_orig
);
14633 finish_expr_stmt (block
);
14635 add_decl_expr (omp_orig
);
14639 /* T is a postfix-expression that is not being used in a function
14640 call. Return the substituted version of T. */
14643 tsubst_non_call_postfix_expression (tree t
, tree args
,
14644 tsubst_flags_t complain
,
14647 if (TREE_CODE (t
) == SCOPE_REF
)
14648 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
14649 /*done=*/false, /*address_p=*/false);
14651 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
14652 /*function_p=*/false,
14653 /*integral_constant_expression_p=*/false);
14658 /* Like tsubst but deals with expressions and performs semantic
14659 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
14662 tsubst_copy_and_build (tree t
,
14664 tsubst_flags_t complain
,
14667 bool integral_constant_expression_p
)
14669 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
14670 #define RECUR(NODE) \
14671 tsubst_copy_and_build (NODE, args, complain, in_decl, \
14672 /*function_p=*/false, \
14673 integral_constant_expression_p)
14678 if (t
== NULL_TREE
|| t
== error_mark_node
)
14681 loc
= input_location
;
14682 if (EXPR_HAS_LOCATION (t
))
14683 input_location
= EXPR_LOCATION (t
);
14685 /* N3276 decltype magic only applies to calls at the top level or on the
14686 right side of a comma. */
14687 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
14688 complain
&= ~tf_decltype
;
14690 switch (TREE_CODE (t
))
14694 /* Fall through. */
14695 case IDENTIFIER_NODE
:
14699 bool non_integral_constant_expression_p
;
14700 const char *error_msg
;
14702 if (IDENTIFIER_TYPENAME_P (t
))
14704 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14705 t
= mangle_conv_op_name_for_type (new_type
);
14708 /* Look up the name. */
14709 decl
= lookup_name (t
);
14711 /* By convention, expressions use ERROR_MARK_NODE to indicate
14712 failure, not NULL_TREE. */
14713 if (decl
== NULL_TREE
)
14714 decl
= error_mark_node
;
14716 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
14718 integral_constant_expression_p
,
14719 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
14720 &non_integral_constant_expression_p
,
14721 /*template_p=*/false,
14723 /*address_p=*/false,
14724 /*template_arg_p=*/false,
14729 if (!function_p
&& identifier_p (decl
))
14731 if (complain
& tf_error
)
14732 unqualified_name_lookup_error (decl
);
14733 decl
= error_mark_node
;
14738 case TEMPLATE_ID_EXPR
:
14741 tree templ
= RECUR (TREE_OPERAND (t
, 0));
14742 tree targs
= TREE_OPERAND (t
, 1);
14745 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
14746 if (targs
== error_mark_node
)
14747 return error_mark_node
;
14749 if (variable_template_p (templ
))
14751 templ
= lookup_template_variable (templ
, targs
);
14752 if (!any_dependent_template_arguments_p (targs
))
14754 templ
= finish_template_variable (templ
, complain
);
14757 RETURN (convert_from_reference (templ
));
14760 if (TREE_CODE (templ
) == COMPONENT_REF
)
14762 object
= TREE_OPERAND (templ
, 0);
14763 templ
= TREE_OPERAND (templ
, 1);
14766 object
= NULL_TREE
;
14767 templ
= lookup_template_function (templ
, targs
);
14770 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
14771 object
, templ
, NULL_TREE
));
14773 RETURN (baselink_for_fns (templ
));
14778 tree r
= RECUR (TREE_OPERAND (t
, 0));
14780 if (REFERENCE_REF_P (t
))
14782 /* A type conversion to reference type will be enclosed in
14783 such an indirect ref, but the substitution of the cast
14784 will have also added such an indirect ref. */
14785 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
14786 r
= convert_from_reference (r
);
14789 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
14790 complain
|decltype_flag
);
14796 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14797 tree op0
= RECUR (TREE_OPERAND (t
, 0));
14798 RETURN (build_nop (type
, op0
));
14801 case IMPLICIT_CONV_EXPR
:
14803 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14804 tree expr
= RECUR (TREE_OPERAND (t
, 0));
14805 int flags
= LOOKUP_IMPLICIT
;
14806 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
14807 flags
= LOOKUP_NORMAL
;
14808 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
14814 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14815 tree op0
= RECUR (TREE_OPERAND (t
, 0));
14816 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
14820 case REINTERPRET_CAST_EXPR
:
14821 case CONST_CAST_EXPR
:
14822 case DYNAMIC_CAST_EXPR
:
14823 case STATIC_CAST_EXPR
:
14826 tree op
, r
= NULL_TREE
;
14828 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14829 if (integral_constant_expression_p
14830 && !cast_valid_in_integral_constant_expression_p (type
))
14832 if (complain
& tf_error
)
14833 error ("a cast to a type other than an integral or "
14834 "enumeration type cannot appear in a constant-expression");
14835 RETURN (error_mark_node
);
14838 op
= RECUR (TREE_OPERAND (t
, 0));
14840 warning_sentinel
s(warn_useless_cast
);
14841 switch (TREE_CODE (t
))
14844 r
= build_functional_cast (type
, op
, complain
);
14846 case REINTERPRET_CAST_EXPR
:
14847 r
= build_reinterpret_cast (type
, op
, complain
);
14849 case CONST_CAST_EXPR
:
14850 r
= build_const_cast (type
, op
, complain
);
14852 case DYNAMIC_CAST_EXPR
:
14853 r
= build_dynamic_cast (type
, op
, complain
);
14855 case STATIC_CAST_EXPR
:
14856 r
= build_static_cast (type
, op
, complain
);
14859 gcc_unreachable ();
14865 case POSTDECREMENT_EXPR
:
14866 case POSTINCREMENT_EXPR
:
14867 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14868 args
, complain
, in_decl
);
14869 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
14870 complain
|decltype_flag
));
14872 case PREDECREMENT_EXPR
:
14873 case PREINCREMENT_EXPR
:
14877 case TRUTH_NOT_EXPR
:
14878 case UNARY_PLUS_EXPR
: /* Unary + */
14879 case REALPART_EXPR
:
14880 case IMAGPART_EXPR
:
14881 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
14882 RECUR (TREE_OPERAND (t
, 0)),
14883 complain
|decltype_flag
));
14885 case FIX_TRUNC_EXPR
:
14886 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
14890 op1
= TREE_OPERAND (t
, 0);
14891 if (TREE_CODE (op1
) == LABEL_DECL
)
14892 RETURN (finish_label_address_expr (DECL_NAME (op1
),
14893 EXPR_LOCATION (op1
)));
14894 if (TREE_CODE (op1
) == SCOPE_REF
)
14895 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
14896 /*done=*/true, /*address_p=*/true);
14898 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
14900 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
14901 complain
|decltype_flag
));
14906 case TRUNC_DIV_EXPR
:
14907 case CEIL_DIV_EXPR
:
14908 case FLOOR_DIV_EXPR
:
14909 case ROUND_DIV_EXPR
:
14910 case EXACT_DIV_EXPR
:
14914 case TRUNC_MOD_EXPR
:
14915 case FLOOR_MOD_EXPR
:
14916 case TRUTH_ANDIF_EXPR
:
14917 case TRUTH_ORIF_EXPR
:
14918 case TRUTH_AND_EXPR
:
14919 case TRUTH_OR_EXPR
:
14935 warning_sentinel
s1(warn_type_limits
);
14936 warning_sentinel
s2(warn_div_by_zero
);
14937 tree op0
= RECUR (TREE_OPERAND (t
, 0));
14938 tree op1
= RECUR (TREE_OPERAND (t
, 1));
14939 tree r
= build_x_binary_op
14940 (input_location
, TREE_CODE (t
),
14942 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
14944 : TREE_CODE (TREE_OPERAND (t
, 0))),
14946 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
14948 : TREE_CODE (TREE_OPERAND (t
, 1))),
14950 complain
|decltype_flag
);
14951 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
14952 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14957 case POINTER_PLUS_EXPR
:
14959 tree op0
= RECUR (TREE_OPERAND (t
, 0));
14960 tree op1
= RECUR (TREE_OPERAND (t
, 1));
14961 return fold_build_pointer_plus (op0
, op1
);
14965 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
14966 /*address_p=*/false));
14968 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14969 args
, complain
, in_decl
);
14970 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
14971 RECUR (TREE_OPERAND (t
, 1)),
14972 complain
|decltype_flag
));
14974 case ARRAY_NOTATION_REF
:
14976 tree start_index
, length
, stride
;
14977 op1
= tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t
),
14978 args
, complain
, in_decl
);
14979 start_index
= RECUR (ARRAY_NOTATION_START (t
));
14980 length
= RECUR (ARRAY_NOTATION_LENGTH (t
));
14981 stride
= RECUR (ARRAY_NOTATION_STRIDE (t
));
14982 RETURN (build_array_notation_ref (EXPR_LOCATION (t
), op1
, start_index
,
14983 length
, stride
, TREE_TYPE (op1
)));
14986 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
14987 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
14994 op1
= TREE_OPERAND (t
, 0);
14995 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
14996 op1
= TREE_TYPE (op1
);
14999 /* When there are no ARGS, we are trying to evaluate a
15000 non-dependent expression from the parser. Trying to do
15001 the substitutions may not work. */
15003 op1
= TREE_TYPE (op1
);
15007 ++cp_unevaluated_operand
;
15008 ++c_inhibit_evaluation_warnings
;
15010 op1
= tsubst (op1
, args
, complain
, in_decl
);
15012 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
15013 /*function_p=*/false,
15014 /*integral_constant_expression_p=*/
15016 --cp_unevaluated_operand
;
15017 --c_inhibit_evaluation_warnings
;
15020 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
15021 complain
& tf_error
);
15023 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
15024 complain
& tf_error
);
15025 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
15027 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
15029 if (!processing_template_decl
&& TYPE_P (op1
))
15031 r
= build_min (SIZEOF_EXPR
, size_type_node
,
15032 build1 (NOP_EXPR
, op1
, error_mark_node
));
15033 SIZEOF_EXPR_TYPE_P (r
) = 1;
15036 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
15037 TREE_SIDE_EFFECTS (r
) = 0;
15038 TREE_READONLY (r
) = 1;
15040 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
15045 case AT_ENCODE_EXPR
:
15047 op1
= TREE_OPERAND (t
, 0);
15048 ++cp_unevaluated_operand
;
15049 ++c_inhibit_evaluation_warnings
;
15050 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
15051 /*function_p=*/false,
15052 /*integral_constant_expression_p=*/false);
15053 --cp_unevaluated_operand
;
15054 --c_inhibit_evaluation_warnings
;
15055 RETURN (objc_build_encode_expr (op1
));
15058 case NOEXCEPT_EXPR
:
15059 op1
= TREE_OPERAND (t
, 0);
15060 ++cp_unevaluated_operand
;
15061 ++c_inhibit_evaluation_warnings
;
15062 ++cp_noexcept_operand
;
15063 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
15064 /*function_p=*/false,
15065 /*integral_constant_expression_p=*/false);
15066 --cp_unevaluated_operand
;
15067 --c_inhibit_evaluation_warnings
;
15068 --cp_noexcept_operand
;
15069 RETURN (finish_noexcept_expr (op1
, complain
));
15073 warning_sentinel
s(warn_div_by_zero
);
15074 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
15075 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
15076 tree r
= build_x_modify_expr
15077 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
15078 complain
|decltype_flag
);
15079 /* TREE_NO_WARNING must be set if either the expression was
15080 parenthesized or it uses an operator such as >>= rather
15081 than plain assignment. In the former case, it was already
15082 set and must be copied. In the latter case,
15083 build_x_modify_expr sets it and it must not be reset
15085 if (TREE_NO_WARNING (t
))
15086 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
15092 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
15093 args
, complain
, in_decl
);
15094 /* Remember that there was a reference to this entity. */
15097 RETURN (build_x_arrow (input_location
, op1
, complain
));
15101 tree placement
= RECUR (TREE_OPERAND (t
, 0));
15102 tree init
= RECUR (TREE_OPERAND (t
, 3));
15103 vec
<tree
, va_gc
> *placement_vec
;
15104 vec
<tree
, va_gc
> *init_vec
;
15107 if (placement
== NULL_TREE
)
15108 placement_vec
= NULL
;
15111 placement_vec
= make_tree_vector ();
15112 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
15113 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
15116 /* If there was an initializer in the original tree, but it
15117 instantiated to an empty list, then we should pass a
15118 non-NULL empty vector to tell build_new that it was an
15119 empty initializer() rather than no initializer. This can
15120 only happen when the initializer is a pack expansion whose
15121 parameter packs are of length zero. */
15122 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
15126 init_vec
= make_tree_vector ();
15127 if (init
== void_node
)
15128 gcc_assert (init_vec
!= NULL
);
15131 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
15132 vec_safe_push (init_vec
, TREE_VALUE (init
));
15136 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15137 tree op2
= RECUR (TREE_OPERAND (t
, 2));
15138 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
15139 NEW_EXPR_USE_GLOBAL (t
),
15142 if (placement_vec
!= NULL
)
15143 release_tree_vector (placement_vec
);
15144 if (init_vec
!= NULL
)
15145 release_tree_vector (init_vec
);
15152 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15153 tree op1
= RECUR (TREE_OPERAND (t
, 1));
15154 RETURN (delete_sanity (op0
, op1
,
15155 DELETE_EXPR_USE_VEC (t
),
15156 DELETE_EXPR_USE_GLOBAL (t
),
15160 case COMPOUND_EXPR
:
15162 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
15163 complain
& ~tf_decltype
, in_decl
,
15164 /*function_p=*/false,
15165 integral_constant_expression_p
);
15166 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
15168 RECUR (TREE_OPERAND (t
, 1)),
15169 complain
|decltype_flag
));
15175 vec
<tree
, va_gc
> *call_args
;
15176 unsigned int nargs
, i
;
15181 function
= CALL_EXPR_FN (t
);
15182 /* When we parsed the expression, we determined whether or
15183 not Koenig lookup should be performed. */
15184 koenig_p
= KOENIG_LOOKUP_P (t
);
15185 if (TREE_CODE (function
) == SCOPE_REF
)
15187 qualified_p
= true;
15188 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
15190 /*address_p=*/false);
15192 else if (koenig_p
&& identifier_p (function
))
15194 /* Do nothing; calling tsubst_copy_and_build on an identifier
15195 would incorrectly perform unqualified lookup again.
15197 Note that we can also have an IDENTIFIER_NODE if the earlier
15198 unqualified lookup found a member function; in that case
15199 koenig_p will be false and we do want to do the lookup
15200 again to find the instantiated member function.
15202 FIXME but doing that causes c++/15272, so we need to stop
15203 using IDENTIFIER_NODE in that situation. */
15204 qualified_p
= false;
15208 if (TREE_CODE (function
) == COMPONENT_REF
)
15210 tree op
= TREE_OPERAND (function
, 1);
15212 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
15213 || (BASELINK_P (op
)
15214 && BASELINK_QUALIFIED_P (op
)));
15217 qualified_p
= false;
15219 if (TREE_CODE (function
) == ADDR_EXPR
15220 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
15221 /* Avoid error about taking the address of a constructor. */
15222 function
= TREE_OPERAND (function
, 0);
15224 function
= tsubst_copy_and_build (function
, args
, complain
,
15227 integral_constant_expression_p
);
15229 if (BASELINK_P (function
))
15230 qualified_p
= true;
15233 nargs
= call_expr_nargs (t
);
15234 call_args
= make_tree_vector ();
15235 for (i
= 0; i
< nargs
; ++i
)
15237 tree arg
= CALL_EXPR_ARG (t
, i
);
15239 if (!PACK_EXPANSION_P (arg
))
15240 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
15243 /* Expand the pack expansion and push each entry onto
15245 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
15246 if (TREE_CODE (arg
) == TREE_VEC
)
15248 unsigned int len
, j
;
15250 len
= TREE_VEC_LENGTH (arg
);
15251 for (j
= 0; j
< len
; ++j
)
15253 tree value
= TREE_VEC_ELT (arg
, j
);
15254 if (value
!= NULL_TREE
)
15255 value
= convert_from_reference (value
);
15256 vec_safe_push (call_args
, value
);
15261 /* A partial substitution. Add one entry. */
15262 vec_safe_push (call_args
, arg
);
15267 /* We do not perform argument-dependent lookup if normal
15268 lookup finds a non-function, in accordance with the
15269 expected resolution of DR 218. */
15271 && ((is_overloaded_fn (function
)
15272 /* If lookup found a member function, the Koenig lookup is
15273 not appropriate, even if an unqualified-name was used
15274 to denote the function. */
15275 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
15276 || identifier_p (function
))
15277 /* Only do this when substitution turns a dependent call
15278 into a non-dependent call. */
15279 && type_dependent_expression_p_push (t
)
15280 && !any_type_dependent_arguments_p (call_args
))
15281 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
15283 if (identifier_p (function
)
15284 && !any_type_dependent_arguments_p (call_args
))
15286 if (koenig_p
&& (complain
& tf_warning_or_error
))
15288 /* For backwards compatibility and good diagnostics, try
15289 the unqualified lookup again if we aren't in SFINAE
15291 tree unq
= (tsubst_copy_and_build
15292 (function
, args
, complain
, in_decl
, true,
15293 integral_constant_expression_p
));
15294 if (unq
== error_mark_node
)
15295 RETURN (error_mark_node
);
15297 if (unq
!= function
)
15300 if (INDIRECT_REF_P (fn
))
15301 fn
= TREE_OPERAND (fn
, 0);
15302 if (TREE_CODE (fn
) == COMPONENT_REF
)
15303 fn
= TREE_OPERAND (fn
, 1);
15304 if (is_overloaded_fn (fn
))
15305 fn
= get_first_fn (fn
);
15306 if (permerror (EXPR_LOC_OR_LOC (t
, input_location
),
15307 "%qD was not declared in this scope, "
15308 "and no declarations were found by "
15309 "argument-dependent lookup at the point "
15310 "of instantiation", function
))
15313 /* Can't say anything more. */;
15314 else if (DECL_CLASS_SCOPE_P (fn
))
15316 location_t loc
= EXPR_LOC_OR_LOC (t
,
15319 "declarations in dependent base %qT are "
15320 "not found by unqualified lookup",
15321 DECL_CLASS_CONTEXT (fn
));
15322 if (current_class_ptr
)
15324 "use %<this->%D%> instead", function
);
15327 "use %<%T::%D%> instead",
15328 current_class_name
, function
);
15331 inform (0, "%q+D declared here, later in the "
15332 "translation unit", fn
);
15337 if (identifier_p (function
))
15339 if (complain
& tf_error
)
15340 unqualified_name_lookup_error (function
);
15341 release_tree_vector (call_args
);
15342 RETURN (error_mark_node
);
15346 /* Remember that there was a reference to this entity. */
15347 if (DECL_P (function
))
15348 mark_used (function
, complain
);
15350 /* Put back tf_decltype for the actual call. */
15351 complain
|= decltype_flag
;
15353 if (TREE_CODE (function
) == OFFSET_REF
)
15354 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
15356 else if (TREE_CODE (function
) == COMPONENT_REF
)
15358 tree instance
= TREE_OPERAND (function
, 0);
15359 tree fn
= TREE_OPERAND (function
, 1);
15361 if (processing_template_decl
15362 && (type_dependent_expression_p (instance
)
15363 || (!BASELINK_P (fn
)
15364 && TREE_CODE (fn
) != FIELD_DECL
)
15365 || type_dependent_expression_p (fn
)
15366 || any_type_dependent_arguments_p (call_args
)))
15367 ret
= build_nt_call_vec (function
, call_args
);
15368 else if (!BASELINK_P (fn
))
15369 ret
= finish_call_expr (function
, &call_args
,
15370 /*disallow_virtual=*/false,
15371 /*koenig_p=*/false,
15374 ret
= (build_new_method_call
15376 &call_args
, NULL_TREE
,
15377 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
15382 ret
= finish_call_expr (function
, &call_args
,
15383 /*disallow_virtual=*/qualified_p
,
15387 release_tree_vector (call_args
);
15394 tree cond
= RECUR (TREE_OPERAND (t
, 0));
15395 tree folded_cond
= fold_non_dependent_expr (cond
);
15398 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
15400 if (integer_zerop (folded_cond
))
15402 ++c_inhibit_evaluation_warnings
;
15403 exp1
= RECUR (TREE_OPERAND (t
, 1));
15404 --c_inhibit_evaluation_warnings
;
15405 exp2
= RECUR (TREE_OPERAND (t
, 2));
15409 exp1
= RECUR (TREE_OPERAND (t
, 1));
15410 ++c_inhibit_evaluation_warnings
;
15411 exp2
= RECUR (TREE_OPERAND (t
, 2));
15412 --c_inhibit_evaluation_warnings
;
15414 cond
= folded_cond
;
15418 exp1
= RECUR (TREE_OPERAND (t
, 1));
15419 exp2
= RECUR (TREE_OPERAND (t
, 2));
15422 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
15423 cond
, exp1
, exp2
, complain
));
15426 case PSEUDO_DTOR_EXPR
:
15428 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15429 tree op1
= RECUR (TREE_OPERAND (t
, 1));
15430 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15431 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
15437 tree purpose
, value
, chain
;
15439 if (t
== void_list_node
)
15442 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
15443 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
15445 /* We have pack expansions, so expand those and
15446 create a new list out of it. */
15447 tree purposevec
= NULL_TREE
;
15448 tree valuevec
= NULL_TREE
;
15452 /* Expand the argument expressions. */
15453 if (TREE_PURPOSE (t
))
15454 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
15455 complain
, in_decl
);
15456 if (TREE_VALUE (t
))
15457 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
15458 complain
, in_decl
);
15460 /* Build the rest of the list. */
15461 chain
= TREE_CHAIN (t
);
15462 if (chain
&& chain
!= void_type_node
)
15463 chain
= RECUR (chain
);
15465 /* Determine the number of arguments. */
15466 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
15468 len
= TREE_VEC_LENGTH (purposevec
);
15469 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
15471 else if (TREE_CODE (valuevec
) == TREE_VEC
)
15472 len
= TREE_VEC_LENGTH (valuevec
);
15475 /* Since we only performed a partial substitution into
15476 the argument pack, we only RETURN (a single list
15478 if (purposevec
== TREE_PURPOSE (t
)
15479 && valuevec
== TREE_VALUE (t
)
15480 && chain
== TREE_CHAIN (t
))
15483 RETURN (tree_cons (purposevec
, valuevec
, chain
));
15486 /* Convert the argument vectors into a TREE_LIST */
15490 /* Grab the Ith values. */
15492 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
15495 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
15498 /* Build the list (backwards). */
15499 chain
= tree_cons (purpose
, value
, chain
);
15505 purpose
= TREE_PURPOSE (t
);
15507 purpose
= RECUR (purpose
);
15508 value
= TREE_VALUE (t
);
15510 value
= RECUR (value
);
15511 chain
= TREE_CHAIN (t
);
15512 if (chain
&& chain
!= void_type_node
)
15513 chain
= RECUR (chain
);
15514 if (purpose
== TREE_PURPOSE (t
)
15515 && value
== TREE_VALUE (t
)
15516 && chain
== TREE_CHAIN (t
))
15518 RETURN (tree_cons (purpose
, value
, chain
));
15521 case COMPONENT_REF
:
15528 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
15529 args
, complain
, in_decl
);
15530 /* Remember that there was a reference to this entity. */
15531 if (DECL_P (object
))
15532 mark_used (object
);
15533 object_type
= TREE_TYPE (object
);
15535 member
= TREE_OPERAND (t
, 1);
15536 if (BASELINK_P (member
))
15537 member
= tsubst_baselink (member
,
15538 non_reference (TREE_TYPE (object
)),
15539 args
, complain
, in_decl
);
15541 member
= tsubst_copy (member
, args
, complain
, in_decl
);
15542 if (member
== error_mark_node
)
15543 RETURN (error_mark_node
);
15545 if (type_dependent_expression_p (object
))
15546 /* We can't do much here. */;
15547 else if (!CLASS_TYPE_P (object_type
))
15549 if (scalarish_type_p (object_type
))
15551 tree s
= NULL_TREE
;
15552 tree dtor
= member
;
15554 if (TREE_CODE (dtor
) == SCOPE_REF
)
15556 s
= TREE_OPERAND (dtor
, 0);
15557 dtor
= TREE_OPERAND (dtor
, 1);
15559 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
15561 dtor
= TREE_OPERAND (dtor
, 0);
15563 RETURN (finish_pseudo_destructor_expr
15564 (object
, s
, dtor
, input_location
));
15568 else if (TREE_CODE (member
) == SCOPE_REF
15569 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
15571 /* Lookup the template functions now that we know what the
15573 tree scope
= TREE_OPERAND (member
, 0);
15574 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
15575 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
15576 member
= lookup_qualified_name (scope
, tmpl
,
15577 /*is_type_p=*/false,
15578 /*complain=*/false);
15579 if (BASELINK_P (member
))
15581 BASELINK_FUNCTIONS (member
)
15582 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
15584 member
= (adjust_result_of_qualified_name_lookup
15585 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
15590 qualified_name_lookup_error (scope
, tmpl
, member
,
15592 RETURN (error_mark_node
);
15595 else if (TREE_CODE (member
) == SCOPE_REF
15596 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
15597 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
15599 if (complain
& tf_error
)
15601 if (TYPE_P (TREE_OPERAND (member
, 0)))
15602 error ("%qT is not a class or namespace",
15603 TREE_OPERAND (member
, 0));
15605 error ("%qD is not a class or namespace",
15606 TREE_OPERAND (member
, 0));
15608 RETURN (error_mark_node
);
15610 else if (TREE_CODE (member
) == FIELD_DECL
)
15612 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
15613 if (TREE_CODE (r
) == COMPONENT_REF
)
15614 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
15618 r
= finish_class_member_access_expr (object
, member
,
15619 /*template_p=*/false,
15621 if (TREE_CODE (r
) == COMPONENT_REF
)
15622 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
15627 RETURN (build_throw
15628 (RECUR (TREE_OPERAND (t
, 0))));
15632 vec
<constructor_elt
, va_gc
> *n
;
15633 constructor_elt
*ce
;
15634 unsigned HOST_WIDE_INT idx
;
15635 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15636 bool process_index_p
;
15638 bool need_copy_p
= false;
15641 if (type
== error_mark_node
)
15642 RETURN (error_mark_node
);
15644 /* digest_init will do the wrong thing if we let it. */
15645 if (type
&& TYPE_PTRMEMFUNC_P (type
))
15648 /* We do not want to process the index of aggregate
15649 initializers as they are identifier nodes which will be
15650 looked up by digest_init. */
15651 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
15653 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
15654 newlen
= vec_safe_length (n
);
15655 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
15657 if (ce
->index
&& process_index_p
15658 /* An identifier index is looked up in the type
15659 being initialized, not the current scope. */
15660 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
15661 ce
->index
= RECUR (ce
->index
);
15663 if (PACK_EXPANSION_P (ce
->value
))
15665 /* Substitute into the pack expansion. */
15666 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
15669 if (ce
->value
== error_mark_node
15670 || PACK_EXPANSION_P (ce
->value
))
15672 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
15673 /* Just move the argument into place. */
15674 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
15677 /* Update the length of the final CONSTRUCTOR
15678 arguments vector, and note that we will need to
15680 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
15681 need_copy_p
= true;
15685 ce
->value
= RECUR (ce
->value
);
15690 vec
<constructor_elt
, va_gc
> *old_n
= n
;
15692 vec_alloc (n
, newlen
);
15693 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
15695 if (TREE_CODE (ce
->value
) == TREE_VEC
)
15697 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
15698 for (i
= 0; i
< len
; ++i
)
15699 CONSTRUCTOR_APPEND_ELT (n
, 0,
15700 TREE_VEC_ELT (ce
->value
, i
));
15703 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
15707 r
= build_constructor (init_list_type_node
, n
);
15708 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
15710 if (TREE_HAS_CONSTRUCTOR (t
))
15711 RETURN (finish_compound_literal (type
, r
, complain
));
15713 TREE_TYPE (r
) = type
;
15719 tree operand_0
= TREE_OPERAND (t
, 0);
15720 if (TYPE_P (operand_0
))
15722 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
15723 RETURN (get_typeid (operand_0
, complain
));
15727 operand_0
= RECUR (operand_0
);
15728 RETURN (build_typeid (operand_0
, complain
));
15735 else if (DECL_PACK_P (t
))
15737 /* We don't build decls for an instantiation of a
15738 variadic capture proxy, we instantiate the elements
15740 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
15741 return RECUR (DECL_VALUE_EXPR (t
));
15747 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
15748 /* ??? We're doing a subset of finish_id_expression here. */
15750 && !processing_template_decl
15751 && !cp_unevaluated_operand
15752 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
15753 && DECL_THREAD_LOCAL_P (r
))
15755 if (tree wrap
= get_tls_wrapper_fn (r
))
15756 /* Replace an evaluated use of the thread_local variable with
15757 a call to its wrapper. */
15758 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
15760 else if (outer_automatic_var_p (r
))
15762 r
= process_outer_var_ref (r
, complain
);
15763 if (is_capture_proxy (r
))
15764 register_local_specialization (r
, t
);
15767 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
15768 /* If the original type was a reference, we'll be wrapped in
15769 the appropriate INDIRECT_REF. */
15770 r
= convert_from_reference (r
);
15776 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15777 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15778 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
15781 case OFFSETOF_EXPR
:
15782 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t
, 0)),
15783 EXPR_LOCATION (t
)));
15787 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
15788 complain
, in_decl
);
15790 tree type2
= TRAIT_EXPR_TYPE2 (t
);
15791 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
15792 type2
= RECUR (type2
);
15794 type2
= tsubst (type2
, args
, complain
, in_decl
);
15796 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
15801 tree old_stmt_expr
= cur_stmt_expr
;
15802 tree stmt_expr
= begin_stmt_expr ();
15804 cur_stmt_expr
= stmt_expr
;
15805 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
15806 integral_constant_expression_p
);
15807 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
15808 cur_stmt_expr
= old_stmt_expr
;
15810 /* If the resulting list of expression statement is empty,
15811 fold it further into void_node. */
15812 if (empty_expr_stmt_p (stmt_expr
))
15813 stmt_expr
= void_node
;
15815 RETURN (stmt_expr
);
15820 tree r
= build_lambda_expr ();
15822 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
15823 LAMBDA_EXPR_CLOSURE (r
) = type
;
15824 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
15826 LAMBDA_EXPR_LOCATION (r
)
15827 = LAMBDA_EXPR_LOCATION (t
);
15828 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
15829 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
15830 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
15831 LAMBDA_EXPR_DISCRIMINATOR (r
)
15832 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
15833 /* For a function scope, we want to use tsubst so that we don't
15834 complain about referring to an auto function before its return
15835 type has been deduced. Otherwise, we want to use tsubst_copy so
15836 that we look up the existing field/parameter/variable rather
15837 than build a new one. */
15838 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
15839 if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
15840 scope
= tsubst (scope
, args
, complain
, in_decl
);
15841 else if (scope
&& TREE_CODE (scope
) == PARM_DECL
)
15843 /* Look up the parameter we want directly, as tsubst_copy
15844 doesn't do what we need. */
15845 tree fn
= tsubst (DECL_CONTEXT (scope
), args
, complain
, in_decl
);
15846 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
15847 while (DECL_PARM_INDEX (parm
) != DECL_PARM_INDEX (scope
))
15848 parm
= DECL_CHAIN (parm
);
15850 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15851 if (DECL_CONTEXT (scope
) == NULL_TREE
)
15852 DECL_CONTEXT (scope
) = fn
;
15855 scope
= RECUR (scope
);
15856 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
15857 LAMBDA_EXPR_RETURN_TYPE (r
)
15858 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
15860 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
15861 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
15863 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15864 determine_visibility (TYPE_NAME (type
));
15865 /* Now that we know visibility, instantiate the type so we have a
15866 declaration of the op() for later calls to lambda_function. */
15867 complete_type (type
);
15869 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
15871 insert_pending_capture_proxies ();
15873 RETURN (build_lambda_object (r
));
15877 /* We can get here for a constant initializer of non-dependent type.
15878 FIXME stop folding in cp_parser_initializer_clause. */
15880 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
15885 case TRANSACTION_EXPR
:
15886 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
15887 integral_constant_expression_p
));
15890 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
15892 case VEC_PERM_EXPR
:
15894 tree op0
= RECUR (TREE_OPERAND (t
, 0));
15895 tree op1
= RECUR (TREE_OPERAND (t
, 1));
15896 tree op2
= RECUR (TREE_OPERAND (t
, 2));
15897 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
15902 /* Handle Objective-C++ constructs, if appropriate. */
15905 = objcp_tsubst_copy_and_build (t
, args
, complain
,
15906 in_decl
, /*function_p=*/false);
15910 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
15916 input_location
= loc
;
15920 /* Verify that the instantiated ARGS are valid. For type arguments,
15921 make sure that the type's linkage is ok. For non-type arguments,
15922 make sure they are constants if they are integral or enumerations.
15923 Emit an error under control of COMPLAIN, and return TRUE on error. */
15926 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
15928 if (dependent_template_arg_p (t
))
15930 if (ARGUMENT_PACK_P (t
))
15932 tree vec
= ARGUMENT_PACK_ARGS (t
);
15933 int len
= TREE_VEC_LENGTH (vec
);
15934 bool result
= false;
15937 for (i
= 0; i
< len
; ++i
)
15938 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
15942 else if (TYPE_P (t
))
15944 /* [basic.link]: A name with no linkage (notably, the name
15945 of a class or enumeration declared in a local scope)
15946 shall not be used to declare an entity with linkage.
15947 This implies that names with no linkage cannot be used as
15950 DR 757 relaxes this restriction for C++0x. */
15951 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
15952 : no_linkage_check (t
, /*relaxed_p=*/false));
15956 /* DR 488 makes use of a type with no linkage cause
15957 type deduction to fail. */
15958 if (complain
& tf_error
)
15960 if (TYPE_ANONYMOUS_P (nt
))
15961 error ("%qT is/uses anonymous type", t
);
15963 error ("template argument for %qD uses local type %qT",
15968 /* In order to avoid all sorts of complications, we do not
15969 allow variably-modified types as template arguments. */
15970 else if (variably_modified_type_p (t
, NULL_TREE
))
15972 if (complain
& tf_error
)
15973 error ("%qT is a variably modified type", t
);
15977 /* Class template and alias template arguments should be OK. */
15978 else if (DECL_TYPE_TEMPLATE_P (t
))
15980 /* A non-type argument of integral or enumerated type must be a
15982 else if (TREE_TYPE (t
)
15983 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
15984 && !REFERENCE_REF_P (t
)
15985 && !TREE_CONSTANT (t
))
15987 if (complain
& tf_error
)
15988 error ("integral expression %qE is not constant", t
);
15995 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
15997 int ix
, len
= DECL_NTPARMS (tmpl
);
15998 bool result
= false;
16000 for (ix
= 0; ix
!= len
; ix
++)
16002 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
16005 if (result
&& (complain
& tf_error
))
16006 error (" trying to instantiate %qD", tmpl
);
16010 /* We're out of SFINAE context now, so generate diagnostics for the access
16011 errors we saw earlier when instantiating D from TMPL and ARGS. */
16014 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
16016 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
16017 tree type
= TREE_TYPE (pattern
);
16018 location_t loc
= input_location
;
16020 push_access_scope (d
);
16021 push_deferring_access_checks (dk_no_deferred
);
16022 input_location
= DECL_SOURCE_LOCATION (pattern
);
16023 tsubst (type
, args
, tf_warning_or_error
, d
);
16024 input_location
= loc
;
16025 pop_deferring_access_checks ();
16026 pop_access_scope (d
);
16029 /* Instantiate the indicated variable, function, or alias template TMPL with
16030 the template arguments in TARG_PTR. */
16033 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
16035 tree targ_ptr
= orig_args
;
16039 bool access_ok
= true;
16041 if (tmpl
== error_mark_node
)
16042 return error_mark_node
;
16044 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
16046 /* If this function is a clone, handle it specially. */
16047 if (DECL_CLONED_FUNCTION_P (tmpl
))
16052 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
16053 DECL_CLONED_FUNCTION. */
16054 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
16055 targ_ptr
, complain
);
16056 if (spec
== error_mark_node
)
16057 return error_mark_node
;
16059 /* Look for the clone. */
16060 FOR_EACH_CLONE (clone
, spec
)
16061 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
16063 /* We should always have found the clone by now. */
16064 gcc_unreachable ();
16068 if (targ_ptr
== error_mark_node
)
16069 return error_mark_node
;
16071 /* Check to see if we already have this specialization. */
16072 gen_tmpl
= most_general_template (tmpl
);
16073 if (tmpl
!= gen_tmpl
)
16074 /* The TMPL is a partial instantiation. To get a full set of
16075 arguments we must add the arguments used to perform the
16076 partial instantiation. */
16077 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
16080 /* It would be nice to avoid hashing here and then again in tsubst_decl,
16081 but it doesn't seem to be on the hot path. */
16082 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
16084 gcc_assert (tmpl
== gen_tmpl
16085 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
16087 || fndecl
== NULL_TREE
);
16089 if (spec
!= NULL_TREE
)
16091 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
16093 if (complain
& tf_error
)
16094 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
16095 return error_mark_node
;
16100 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
16102 return error_mark_node
;
16104 /* We are building a FUNCTION_DECL, during which the access of its
16105 parameters and return types have to be checked. However this
16106 FUNCTION_DECL which is the desired context for access checking
16107 is not built yet. We solve this chicken-and-egg problem by
16108 deferring all checks until we have the FUNCTION_DECL. */
16109 push_deferring_access_checks (dk_deferred
);
16111 /* Instantiation of the function happens in the context of the function
16112 template, not the context of the overload resolution we're doing. */
16113 push_to_top_level ();
16114 /* If there are dependent arguments, e.g. because we're doing partial
16115 ordering, make sure processing_template_decl stays set. */
16116 if (uses_template_parms (targ_ptr
))
16117 ++processing_template_decl
;
16118 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
16120 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
16121 complain
, gen_tmpl
, true);
16122 push_nested_class (ctx
);
16125 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
16127 if (VAR_P (pattern
))
16129 /* We need to determine if we're using a partial or explicit
16130 specialization now, because the type of the variable could be
16132 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
16133 tree elt
= most_specialized_partial_spec (tid
, complain
);
16134 if (elt
== error_mark_node
)
16135 pattern
= error_mark_node
;
16138 tmpl
= TREE_VALUE (elt
);
16139 pattern
= DECL_TEMPLATE_RESULT (tmpl
);
16140 targ_ptr
= TREE_PURPOSE (elt
);
16144 /* Substitute template parameters to obtain the specialization. */
16145 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
16146 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
16147 pop_nested_class ();
16148 pop_from_top_level ();
16150 if (fndecl
== error_mark_node
)
16152 pop_deferring_access_checks ();
16153 return error_mark_node
;
16156 /* The DECL_TI_TEMPLATE should always be the immediate parent
16157 template, not the most general template. */
16158 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
16159 if (VAR_P (fndecl
))
16160 DECL_TI_ARGS (fndecl
) = targ_ptr
;
16162 /* Now we know the specialization, compute access previously
16164 push_access_scope (fndecl
);
16165 if (!perform_deferred_access_checks (complain
))
16167 pop_access_scope (fndecl
);
16168 pop_deferring_access_checks ();
16170 /* If we've just instantiated the main entry point for a function,
16171 instantiate all the alternate entry points as well. We do this
16172 by cloning the instantiation of the main entry point, not by
16173 instantiating the template clones. */
16174 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
16175 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
16179 if (!(complain
& tf_error
))
16181 /* Remember to reinstantiate when we're out of SFINAE so the user
16182 can see the errors. */
16183 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
16185 return error_mark_node
;
16190 /* Wrapper for instantiate_template_1. */
16193 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
16196 timevar_push (TV_TEMPLATE_INST
);
16197 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
16198 timevar_pop (TV_TEMPLATE_INST
);
16202 /* Instantiate the alias template TMPL with ARGS. Also push a template
16203 instantiation level, which instantiate_template doesn't do because
16204 functions and variables have sufficient context established by the
16208 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
16210 struct pending_template
*old_last_pend
= last_pending_template
;
16211 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
16212 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
16213 return error_mark_node
;
16214 tree tinst
= build_tree_list (tmpl
, args
);
16215 if (!push_tinst_level (tinst
))
16218 return error_mark_node
;
16222 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
16223 args
, tmpl
, complain
,
16224 /*require_all_args=*/true,
16225 /*use_default_args=*/true);
16227 tree r
= instantiate_template (tmpl
, args
, complain
);
16228 pop_tinst_level ();
16229 /* We can't free this if a pending_template entry or last_error_tinst_level
16230 is pointing at it. */
16231 if (last_pending_template
== old_last_pend
16232 && last_error_tinst_level
== old_error_tinst
)
16238 /* PARM is a template parameter pack for FN. Returns true iff
16239 PARM is used in a deducible way in the argument list of FN. */
16242 pack_deducible_p (tree parm
, tree fn
)
16244 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
16245 for (; t
; t
= TREE_CHAIN (t
))
16247 tree type
= TREE_VALUE (t
);
16249 if (!PACK_EXPANSION_P (type
))
16251 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
16252 packs
; packs
= TREE_CHAIN (packs
))
16253 if (template_args_equal (TREE_VALUE (packs
), parm
))
16255 /* The template parameter pack is used in a function parameter
16256 pack. If this is the end of the parameter list, the
16257 template parameter pack is deducible. */
16258 if (TREE_CHAIN (t
) == void_list_node
)
16261 /* Otherwise, not. Well, it could be deduced from
16262 a non-pack parameter, but doing so would end up with
16263 a deduction mismatch, so don't bother. */
16267 /* The template parameter pack isn't used in any function parameter
16268 packs, but it might be used deeper, e.g. tuple<Args...>. */
16272 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
16273 NARGS elements of the arguments that are being used when calling
16274 it. TARGS is a vector into which the deduced template arguments
16277 Returns either a FUNCTION_DECL for the matching specialization of FN or
16278 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
16279 true, diagnostics will be printed to explain why it failed.
16281 If FN is a conversion operator, or we are trying to produce a specific
16282 specialization, RETURN_TYPE is the return type desired.
16284 The EXPLICIT_TARGS are explicit template arguments provided via a
16287 The parameter STRICT is one of:
16290 We are deducing arguments for a function call, as in
16291 [temp.deduct.call].
16294 We are deducing arguments for a conversion function, as in
16295 [temp.deduct.conv].
16298 We are deducing arguments when doing an explicit instantiation
16299 as in [temp.explicit], when determining an explicit specialization
16300 as in [temp.expl.spec], or when taking the address of a function
16301 template, as in [temp.deduct.funcaddr]. */
16304 fn_type_unification (tree fn
,
16305 tree explicit_targs
,
16308 unsigned int nargs
,
16310 unification_kind_t strict
,
16317 tree decl
= NULL_TREE
;
16318 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
16320 static int deduction_depth
;
16321 struct pending_template
*old_last_pend
= last_pending_template
;
16322 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
16323 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
16325 tree r
= error_mark_node
;
16328 complain
|= tf_decltype
;
16330 /* In C++0x, it's possible to have a function template whose type depends
16331 on itself recursively. This is most obvious with decltype, but can also
16332 occur with enumeration scope (c++/48969). So we need to catch infinite
16333 recursion and reject the substitution at deduction time; this function
16334 will return error_mark_node for any repeated substitution.
16336 This also catches excessive recursion such as when f<N> depends on
16337 f<N-1> across all integers, and returns error_mark_node for all the
16338 substitutions back up to the initial one.
16340 This is, of course, not reentrant. */
16341 if (excessive_deduction_depth
)
16342 return error_mark_node
;
16343 tinst
= build_tree_list (fn
, NULL_TREE
);
16346 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
16348 fntype
= TREE_TYPE (fn
);
16349 if (explicit_targs
)
16353 The specified template arguments must match the template
16354 parameters in kind (i.e., type, nontype, template), and there
16355 must not be more arguments than there are parameters;
16356 otherwise type deduction fails.
16358 Nontype arguments must match the types of the corresponding
16359 nontype template parameters, or must be convertible to the
16360 types of the corresponding nontype parameters as specified in
16361 _temp.arg.nontype_, otherwise type deduction fails.
16363 All references in the function type of the function template
16364 to the corresponding template parameters are replaced by the
16365 specified template argument values. If a substitution in a
16366 template parameter or in the function type of the function
16367 template results in an invalid type, type deduction fails. */
16368 int i
, len
= TREE_VEC_LENGTH (tparms
);
16369 location_t loc
= input_location
;
16370 bool incomplete
= false;
16372 /* Adjust any explicit template arguments before entering the
16373 substitution context. */
16375 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
16377 /*require_all_args=*/false,
16378 /*use_default_args=*/false));
16379 if (explicit_targs
== error_mark_node
)
16382 /* Substitute the explicit args into the function type. This is
16383 necessary so that, for instance, explicitly declared function
16384 arguments can match null pointed constants. If we were given
16385 an incomplete set of explicit args, we must not do semantic
16386 processing during substitution as we could create partial
16388 for (i
= 0; i
< len
; i
++)
16390 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
16391 bool parameter_pack
= false;
16392 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
16394 /* Dig out the actual parm. */
16395 if (TREE_CODE (parm
) == TYPE_DECL
16396 || TREE_CODE (parm
) == TEMPLATE_DECL
)
16398 parm
= TREE_TYPE (parm
);
16399 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
16401 else if (TREE_CODE (parm
) == PARM_DECL
)
16403 parm
= DECL_INITIAL (parm
);
16404 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
16407 if (!parameter_pack
&& targ
== NULL_TREE
)
16408 /* No explicit argument for this template parameter. */
16411 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
16413 /* Mark the argument pack as "incomplete". We could
16414 still deduce more arguments during unification.
16415 We remove this mark in type_unification_real. */
16418 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
16419 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
16420 = ARGUMENT_PACK_ARGS (targ
);
16423 /* We have some incomplete argument packs. */
16428 TREE_VALUE (tinst
) = explicit_targs
;
16429 if (!push_tinst_level (tinst
))
16431 excessive_deduction_depth
= true;
16434 processing_template_decl
+= incomplete
;
16435 input_location
= DECL_SOURCE_LOCATION (fn
);
16436 /* Ignore any access checks; we'll see them again in
16437 instantiate_template and they might have the wrong
16438 access path at this point. */
16439 push_deferring_access_checks (dk_deferred
);
16440 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
16441 complain
| tf_partial
, NULL_TREE
);
16442 pop_deferring_access_checks ();
16443 input_location
= loc
;
16444 processing_template_decl
-= incomplete
;
16445 pop_tinst_level ();
16447 if (fntype
== error_mark_node
)
16450 /* Place the explicitly specified arguments in TARGS. */
16451 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
16452 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
16455 /* Never do unification on the 'this' parameter. */
16456 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
16462 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
16463 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
16464 new_args
[0] = return_type
;
16465 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
16470 /* We allow incomplete unification without an error message here
16471 because the standard doesn't seem to explicitly prohibit it. Our
16472 callers must be ready to deal with unification failures in any
16475 TREE_VALUE (tinst
) = targs
;
16476 /* If we aren't explaining yet, push tinst context so we can see where
16477 any errors (e.g. from class instantiations triggered by instantiation
16478 of default template arguments) come from. If we are explaining, this
16479 context is redundant. */
16480 if (!explain_p
&& !push_tinst_level (tinst
))
16482 excessive_deduction_depth
= true;
16486 /* type_unification_real will pass back any access checks from default
16487 template argument substitution. */
16488 vec
<deferred_access_check
, va_gc
> *checks
;
16491 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
16492 targs
, parms
, args
, nargs
, /*subr=*/0,
16493 strict
, flags
, &checks
, explain_p
);
16495 pop_tinst_level ();
16499 /* Now that we have bindings for all of the template arguments,
16500 ensure that the arguments deduced for the template template
16501 parameters have compatible template parameter lists. We cannot
16502 check this property before we have deduced all template
16503 arguments, because the template parameter types of a template
16504 template parameter might depend on prior template parameters
16505 deduced after the template template parameter. The following
16506 ill-formed example illustrates this issue:
16508 template<typename T, template<T> class C> void f(C<5>, T);
16510 template<int N> struct X {};
16513 f(X<5>(), 5l); // error: template argument deduction fails
16516 The template parameter list of 'C' depends on the template type
16517 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
16518 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
16519 time that we deduce 'C'. */
16520 if (!template_template_parm_bindings_ok_p
16521 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
16523 unify_inconsistent_template_template_parameters (explain_p
);
16527 /* All is well so far. Now, check:
16531 When all template arguments have been deduced, all uses of
16532 template parameters in nondeduced contexts are replaced with
16533 the corresponding deduced argument values. If the
16534 substitution results in an invalid type, as described above,
16535 type deduction fails. */
16536 TREE_VALUE (tinst
) = targs
;
16537 if (!push_tinst_level (tinst
))
16539 excessive_deduction_depth
= true;
16543 /* Also collect access checks from the instantiation. */
16544 reopen_deferring_access_checks (checks
);
16546 decl
= instantiate_template (fn
, targs
, complain
);
16548 checks
= get_deferred_access_checks ();
16549 pop_deferring_access_checks ();
16551 pop_tinst_level ();
16553 if (decl
== error_mark_node
)
16556 /* Now perform any access checks encountered during substitution. */
16557 push_access_scope (decl
);
16558 ok
= perform_access_checks (checks
, complain
);
16559 pop_access_scope (decl
);
16563 /* If we're looking for an exact match, check that what we got
16564 is indeed an exact match. It might not be if some template
16565 parameters are used in non-deduced contexts. But don't check
16566 for an exact match if we have dependent template arguments;
16567 in that case we're doing partial ordering, and we already know
16568 that we have two candidates that will provide the actual type. */
16569 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
16571 tree substed
= TREE_TYPE (decl
);
16575 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
16577 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
16578 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
16579 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
16581 unify_type_mismatch (explain_p
, args
[i
],
16582 TREE_VALUE (sarg
));
16591 if (excessive_deduction_depth
)
16593 if (deduction_depth
== 0)
16594 /* Reset once we're all the way out. */
16595 excessive_deduction_depth
= false;
16598 /* We can't free this if a pending_template entry or last_error_tinst_level
16599 is pointing at it. */
16600 if (last_pending_template
== old_last_pend
16601 && last_error_tinst_level
== old_error_tinst
)
16607 /* Adjust types before performing type deduction, as described in
16608 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
16609 sections are symmetric. PARM is the type of a function parameter
16610 or the return type of the conversion function. ARG is the type of
16611 the argument passed to the call, or the type of the value
16612 initialized with the result of the conversion function.
16613 ARG_EXPR is the original argument expression, which may be null. */
16616 maybe_adjust_types_for_deduction (unification_kind_t strict
,
16630 /* Swap PARM and ARG throughout the remainder of this
16631 function; the handling is precisely symmetric since PARM
16632 will initialize ARG rather than vice versa. */
16640 /* Core issue #873: Do the DR606 thing (see below) for these cases,
16641 too, but here handle it by stripping the reference from PARM
16642 rather than by adding it to ARG. */
16643 if (TREE_CODE (*parm
) == REFERENCE_TYPE
16644 && TYPE_REF_IS_RVALUE (*parm
)
16645 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
16646 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
16647 && TREE_CODE (*arg
) == REFERENCE_TYPE
16648 && !TYPE_REF_IS_RVALUE (*arg
))
16649 *parm
= TREE_TYPE (*parm
);
16650 /* Nothing else to do in this case. */
16654 gcc_unreachable ();
16657 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
16659 /* [temp.deduct.call]
16661 If P is not a reference type:
16663 --If A is an array type, the pointer type produced by the
16664 array-to-pointer standard conversion (_conv.array_) is
16665 used in place of A for type deduction; otherwise,
16667 --If A is a function type, the pointer type produced by
16668 the function-to-pointer standard conversion
16669 (_conv.func_) is used in place of A for type deduction;
16672 --If A is a cv-qualified type, the top level
16673 cv-qualifiers of A's type are ignored for type
16675 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
16676 *arg
= build_pointer_type (TREE_TYPE (*arg
));
16677 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
16678 *arg
= build_pointer_type (*arg
);
16680 *arg
= TYPE_MAIN_VARIANT (*arg
);
16683 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
16684 of the form T&&, where T is a template parameter, and the argument
16685 is an lvalue, T is deduced as A& */
16686 if (TREE_CODE (*parm
) == REFERENCE_TYPE
16687 && TYPE_REF_IS_RVALUE (*parm
)
16688 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
16689 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
16690 && (arg_expr
? real_lvalue_p (arg_expr
)
16691 /* try_one_overload doesn't provide an arg_expr, but
16692 functions are always lvalues. */
16693 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
16694 *arg
= build_reference_type (*arg
);
16696 /* [temp.deduct.call]
16698 If P is a cv-qualified type, the top level cv-qualifiers
16699 of P's type are ignored for type deduction. If P is a
16700 reference type, the type referred to by P is used for
16702 *parm
= TYPE_MAIN_VARIANT (*parm
);
16703 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
16705 *parm
= TREE_TYPE (*parm
);
16706 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
16709 /* DR 322. For conversion deduction, remove a reference type on parm
16710 too (which has been swapped into ARG). */
16711 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
16712 *arg
= TREE_TYPE (*arg
);
16717 /* Subroutine of unify_one_argument. PARM is a function parameter of a
16718 template which does contain any deducible template parameters; check if
16719 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
16720 unify_one_argument. */
16723 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
16724 int flags
, bool explain_p
)
16729 type
= TREE_TYPE (arg
);
16733 if (same_type_p (parm
, type
))
16734 return unify_success (explain_p
);
16736 if (strict
== DEDUCE_CONV
)
16738 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
16739 explain_p
? tf_warning_or_error
: tf_none
))
16740 return unify_success (explain_p
);
16742 else if (strict
!= DEDUCE_EXACT
)
16744 if (can_convert_arg (parm
, type
,
16745 TYPE_P (arg
) ? NULL_TREE
: arg
,
16746 flags
, explain_p
? tf_warning_or_error
: tf_none
))
16747 return unify_success (explain_p
);
16750 if (strict
== DEDUCE_EXACT
)
16751 return unify_type_mismatch (explain_p
, parm
, arg
);
16753 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
16756 static bool uses_deducible_template_parms (tree type
);
16758 /* Returns true iff the expression EXPR is one from which a template
16759 argument can be deduced. In other words, if it's an undecorated
16760 use of a template non-type parameter. */
16763 deducible_expression (tree expr
)
16765 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
16768 /* Returns true iff the array domain DOMAIN uses a template parameter in a
16769 deducible way; that is, if it has a max value of <PARM> - 1. */
16772 deducible_array_bound (tree domain
)
16774 if (domain
== NULL_TREE
)
16777 tree max
= TYPE_MAX_VALUE (domain
);
16778 if (TREE_CODE (max
) != MINUS_EXPR
)
16781 return deducible_expression (TREE_OPERAND (max
, 0));
16784 /* Returns true iff the template arguments ARGS use a template parameter
16785 in a deducible way. */
16788 deducible_template_args (tree args
)
16790 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
16793 tree elt
= TREE_VEC_ELT (args
, i
);
16794 if (ARGUMENT_PACK_P (elt
))
16795 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
16798 if (PACK_EXPANSION_P (elt
))
16799 elt
= PACK_EXPANSION_PATTERN (elt
);
16800 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
16802 else if (TYPE_P (elt
))
16803 deducible
= uses_deducible_template_parms (elt
);
16805 deducible
= deducible_expression (elt
);
16813 /* Returns true iff TYPE contains any deducible references to template
16814 parameters, as per 14.8.2.5. */
16817 uses_deducible_template_parms (tree type
)
16819 if (PACK_EXPANSION_P (type
))
16820 type
= PACK_EXPANSION_PATTERN (type
);
16827 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
16828 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16834 if (POINTER_TYPE_P (type
))
16835 return uses_deducible_template_parms (TREE_TYPE (type
));
16837 /* T[integer-constant ]
16839 if (TREE_CODE (type
) == ARRAY_TYPE
)
16840 return (uses_deducible_template_parms (TREE_TYPE (type
))
16841 || deducible_array_bound (TYPE_DOMAIN (type
)));
16853 if (TYPE_PTRMEM_P (type
))
16854 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
16855 || (uses_deducible_template_parms
16856 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
16858 /* template-name <T> (where template-name refers to a class template)
16859 template-name <i> (where template-name refers to a class template) */
16860 if (CLASS_TYPE_P (type
)
16861 && CLASSTYPE_TEMPLATE_INFO (type
)
16862 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
16863 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
16864 (CLASSTYPE_TI_ARGS (type
)));
16869 if (TREE_CODE (type
) == FUNCTION_TYPE
16870 || TREE_CODE (type
) == METHOD_TYPE
)
16872 if (uses_deducible_template_parms (TREE_TYPE (type
)))
16874 tree parm
= TYPE_ARG_TYPES (type
);
16875 if (TREE_CODE (type
) == METHOD_TYPE
)
16876 parm
= TREE_CHAIN (parm
);
16877 for (; parm
; parm
= TREE_CHAIN (parm
))
16878 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
16885 /* Subroutine of type_unification_real and unify_pack_expansion to
16886 handle unification of a single P/A pair. Parameters are as
16887 for those functions. */
16890 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
16891 int subr
, unification_kind_t strict
, int flags
,
16894 tree arg_expr
= NULL_TREE
;
16897 if (arg
== error_mark_node
|| parm
== error_mark_node
)
16898 return unify_invalid (explain_p
);
16899 if (arg
== unknown_type_node
)
16900 /* We can't deduce anything from this, but we might get all the
16901 template args from other function args. */
16902 return unify_success (explain_p
);
16904 /* Implicit conversions (Clause 4) will be performed on a function
16905 argument to convert it to the type of the corresponding function
16906 parameter if the parameter type contains no template-parameters that
16907 participate in template argument deduction. */
16908 if (TYPE_P (parm
) && !uses_template_parms (parm
))
16909 /* For function parameters that contain no template-parameters at all,
16910 we have historically checked for convertibility in order to shortcut
16911 consideration of this candidate. */
16912 return check_non_deducible_conversion (parm
, arg
, strict
, flags
,
16914 else if (strict
== DEDUCE_CALL
16915 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
16916 /* For function parameters with only non-deducible template parameters,
16918 return unify_success (explain_p
);
16923 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
16924 | UNIFY_ALLOW_MORE_CV_QUAL
16925 | UNIFY_ALLOW_DERIVED
);
16929 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
16933 arg_strict
= UNIFY_ALLOW_NONE
;
16937 gcc_unreachable ();
16940 /* We only do these transformations if this is the top-level
16941 parameter_type_list in a call or declaration matching; in other
16942 situations (nested function declarators, template argument lists) we
16943 won't be comparing a type to an expression, and we don't do any type
16949 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
16950 if (type_unknown_p (arg
))
16952 /* [temp.deduct.type] A template-argument can be
16953 deduced from a pointer to function or pointer
16954 to member function argument if the set of
16955 overloaded functions does not contain function
16956 templates and at most one of a set of
16957 overloaded functions provides a unique
16960 if (resolve_overloaded_unification
16961 (tparms
, targs
, parm
, arg
, strict
,
16962 arg_strict
, explain_p
))
16963 return unify_success (explain_p
);
16964 return unify_overload_resolution_failure (explain_p
, arg
);
16968 arg
= unlowered_expr_type (arg
);
16969 if (arg
== error_mark_node
)
16970 return unify_invalid (explain_p
);
16974 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
16977 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
16978 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
16979 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16981 /* For deduction from an init-list we need the actual list. */
16982 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
16984 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
16987 /* Most parms like fn_type_unification.
16989 If SUBR is 1, we're being called recursively (to unify the
16990 arguments of a function or method parameter of a function
16993 CHECKS is a pointer to a vector of access checks encountered while
16994 substituting default template arguments. */
16997 type_unification_real (tree tparms
,
17001 unsigned int xnargs
,
17003 unification_kind_t strict
,
17005 vec
<deferred_access_check
, va_gc
> **checks
,
17010 int ntparms
= TREE_VEC_LENGTH (tparms
);
17011 int saw_undeduced
= 0;
17014 unsigned int nargs
;
17017 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
17018 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
17019 gcc_assert (ntparms
> 0);
17021 /* Reset the number of non-defaulted template arguments contained
17023 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
17031 while (parms
&& parms
!= void_list_node
17034 parm
= TREE_VALUE (parms
);
17036 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
17037 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
17038 /* For a function parameter pack that occurs at the end of the
17039 parameter-declaration-list, the type A of each remaining
17040 argument of the call is compared with the type P of the
17041 declarator-id of the function parameter pack. */
17044 parms
= TREE_CHAIN (parms
);
17046 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
17047 /* For a function parameter pack that does not occur at the
17048 end of the parameter-declaration-list, the type of the
17049 parameter pack is a non-deduced context. */
17055 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
17061 && parms
!= void_list_node
17062 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
17064 /* Unify the remaining arguments with the pack expansion type. */
17066 tree parmvec
= make_tree_vec (1);
17068 /* Allocate a TREE_VEC and copy in all of the arguments */
17069 argvec
= make_tree_vec (nargs
- ia
);
17070 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
17071 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
17073 /* Copy the parameter into parmvec. */
17074 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
17075 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
17076 /*subr=*/subr
, explain_p
))
17079 /* Advance to the end of the list of parameters. */
17080 parms
= TREE_CHAIN (parms
);
17083 /* Fail if we've reached the end of the parm list, and more args
17084 are present, and the parm list isn't variadic. */
17085 if (ia
< nargs
&& parms
== void_list_node
)
17086 return unify_too_many_arguments (explain_p
, nargs
, ia
);
17087 /* Fail if parms are left and they don't have default values and
17088 they aren't all deduced as empty packs (c++/57397). This is
17089 consistent with sufficient_parms_p. */
17090 if (parms
&& parms
!= void_list_node
17091 && TREE_PURPOSE (parms
) == NULL_TREE
)
17093 unsigned int count
= nargs
;
17098 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
17101 p
= TREE_CHAIN (p
);
17103 while (p
&& p
!= void_list_node
);
17104 if (count
!= nargs
)
17105 return unify_too_few_arguments (explain_p
, ia
, count
,
17111 tsubst_flags_t complain
= (explain_p
17112 ? tf_warning_or_error
17115 for (i
= 0; i
< ntparms
; i
++)
17117 tree targ
= TREE_VEC_ELT (targs
, i
);
17118 tree tparm
= TREE_VEC_ELT (tparms
, i
);
17120 /* Clear the "incomplete" flags on all argument packs now so that
17121 substituting them into later default arguments works. */
17122 if (targ
&& ARGUMENT_PACK_P (targ
))
17124 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
17125 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
17128 if (targ
|| tparm
== error_mark_node
)
17130 tparm
= TREE_VALUE (tparm
);
17132 /* If this is an undeduced nontype parameter that depends on
17133 a type parameter, try another pass; its type may have been
17134 deduced from a later argument than the one from which
17135 this parameter can be deduced. */
17136 if (TREE_CODE (tparm
) == PARM_DECL
17137 && uses_template_parms (TREE_TYPE (tparm
))
17138 && !saw_undeduced
++)
17141 /* Core issue #226 (C++0x) [temp.deduct]:
17143 If a template argument has not been deduced, its
17144 default template argument, if any, is used.
17146 When we are in C++98 mode, TREE_PURPOSE will either
17147 be NULL_TREE or ERROR_MARK_NODE, so we do not need
17148 to explicitly check cxx_dialect here. */
17149 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
17151 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
17152 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
17153 reopen_deferring_access_checks (*checks
);
17154 location_t save_loc
= input_location
;
17156 input_location
= DECL_SOURCE_LOCATION (parm
);
17157 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
17158 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
17160 input_location
= save_loc
;
17161 *checks
= get_deferred_access_checks ();
17162 pop_deferring_access_checks ();
17163 if (arg
== error_mark_node
)
17167 TREE_VEC_ELT (targs
, i
) = arg
;
17168 /* The position of the first default template argument,
17169 is also the number of non-defaulted arguments in TARGS.
17171 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
17172 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
17177 /* If the type parameter is a parameter pack, then it will
17178 be deduced to an empty parameter pack. */
17179 if (template_parameter_pack_p (tparm
))
17183 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
17185 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
17186 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
17187 TREE_CONSTANT (arg
) = 1;
17190 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
17192 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
17194 TREE_VEC_ELT (targs
, i
) = arg
;
17198 return unify_parameter_deduction_failure (explain_p
, tparm
);
17201 #ifdef ENABLE_CHECKING
17202 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
17203 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
17206 return unify_success (explain_p
);
17209 /* Subroutine of type_unification_real. Args are like the variables
17210 at the call site. ARG is an overloaded function (or template-id);
17211 we try deducing template args from each of the overloads, and if
17212 only one succeeds, we go with that. Modifies TARGS and returns
17213 true on success. */
17216 resolve_overloaded_unification (tree tparms
,
17220 unification_kind_t strict
,
17224 tree tempargs
= copy_node (targs
);
17226 tree goodfn
= NULL_TREE
;
17229 if (TREE_CODE (arg
) == ADDR_EXPR
)
17231 arg
= TREE_OPERAND (arg
, 0);
17237 if (TREE_CODE (arg
) == COMPONENT_REF
)
17238 /* Handle `&x' where `x' is some static or non-static member
17240 arg
= TREE_OPERAND (arg
, 1);
17242 if (TREE_CODE (arg
) == OFFSET_REF
)
17243 arg
= TREE_OPERAND (arg
, 1);
17245 /* Strip baselink information. */
17246 if (BASELINK_P (arg
))
17247 arg
= BASELINK_FUNCTIONS (arg
);
17249 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
17251 /* If we got some explicit template args, we need to plug them into
17252 the affected templates before we try to unify, in case the
17253 explicit args will completely resolve the templates in question. */
17256 tree expl_subargs
= TREE_OPERAND (arg
, 1);
17257 arg
= TREE_OPERAND (arg
, 0);
17259 for (; arg
; arg
= OVL_NEXT (arg
))
17261 tree fn
= OVL_CURRENT (arg
);
17262 tree subargs
, elem
;
17264 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
17267 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
17268 expl_subargs
, NULL_TREE
, tf_none
,
17269 /*require_all_args=*/true,
17270 /*use_default_args=*/true);
17271 if (subargs
!= error_mark_node
17272 && !any_dependent_template_arguments_p (subargs
))
17274 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
17275 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
17276 elem
, strict
, sub_strict
, addr_p
, explain_p
)
17277 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
17286 /* If no templates (or more than one) are fully resolved by the
17287 explicit arguments, this template-id is a non-deduced context; it
17288 could still be OK if we deduce all template arguments for the
17289 enclosing call through other arguments. */
17293 else if (TREE_CODE (arg
) != OVERLOAD
17294 && TREE_CODE (arg
) != FUNCTION_DECL
)
17295 /* If ARG is, for example, "(0, &f)" then its type will be unknown
17296 -- but the deduction does not succeed because the expression is
17297 not just the function on its own. */
17300 for (; arg
; arg
= OVL_NEXT (arg
))
17301 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
17302 TREE_TYPE (OVL_CURRENT (arg
)),
17303 strict
, sub_strict
, addr_p
, explain_p
)
17304 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
17306 goodfn
= OVL_CURRENT (arg
);
17310 /* [temp.deduct.type] A template-argument can be deduced from a pointer
17311 to function or pointer to member function argument if the set of
17312 overloaded functions does not contain function templates and at most
17313 one of a set of overloaded functions provides a unique match.
17315 So if we found multiple possibilities, we return success but don't
17316 deduce anything. */
17320 int i
= TREE_VEC_LENGTH (targs
);
17322 if (TREE_VEC_ELT (tempargs
, i
))
17324 tree old
= TREE_VEC_ELT (targs
, i
);
17325 tree new_
= TREE_VEC_ELT (tempargs
, i
);
17326 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
17327 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
17328 /* Don't forget explicit template arguments in a pack. */
17329 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
17330 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
17331 TREE_VEC_ELT (targs
, i
) = new_
;
17340 /* Core DR 115: In contexts where deduction is done and fails, or in
17341 contexts where deduction is not done, if a template argument list is
17342 specified and it, along with any default template arguments, identifies
17343 a single function template specialization, then the template-id is an
17344 lvalue for the function template specialization. */
17347 resolve_nondeduced_context (tree orig_expr
)
17349 tree expr
, offset
, baselink
;
17352 if (!type_unknown_p (orig_expr
))
17357 offset
= NULL_TREE
;
17358 baselink
= NULL_TREE
;
17360 if (TREE_CODE (expr
) == ADDR_EXPR
)
17362 expr
= TREE_OPERAND (expr
, 0);
17365 if (TREE_CODE (expr
) == OFFSET_REF
)
17368 expr
= TREE_OPERAND (expr
, 1);
17370 if (BASELINK_P (expr
))
17373 expr
= BASELINK_FUNCTIONS (expr
);
17376 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
17379 tree goodfn
= NULL_TREE
;
17381 /* If we got some explicit template args, we need to plug them into
17382 the affected templates before we try to unify, in case the
17383 explicit args will completely resolve the templates in question. */
17385 tree expl_subargs
= TREE_OPERAND (expr
, 1);
17386 tree arg
= TREE_OPERAND (expr
, 0);
17387 tree badfn
= NULL_TREE
;
17388 tree badargs
= NULL_TREE
;
17390 for (; arg
; arg
= OVL_NEXT (arg
))
17392 tree fn
= OVL_CURRENT (arg
);
17393 tree subargs
, elem
;
17395 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
17398 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
17399 expl_subargs
, NULL_TREE
, tf_none
,
17400 /*require_all_args=*/true,
17401 /*use_default_args=*/true);
17402 if (subargs
!= error_mark_node
17403 && !any_dependent_template_arguments_p (subargs
))
17405 elem
= instantiate_template (fn
, subargs
, tf_none
);
17406 if (elem
== error_mark_node
)
17411 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
17420 mark_used (goodfn
);
17423 expr
= build_baselink (BASELINK_BINFO (baselink
),
17424 BASELINK_ACCESS_BINFO (baselink
),
17425 expr
, BASELINK_OPTYPE (baselink
));
17429 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
17430 expr
= build_offset_ref (base
, expr
, addr
, tf_warning_or_error
);
17433 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
17436 else if (good
== 0 && badargs
)
17437 /* There were no good options and at least one bad one, so let the
17438 user know what the problem is. */
17439 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
17444 /* Subroutine of resolve_overloaded_unification; does deduction for a single
17445 overload. Fills TARGS with any deduced arguments, or error_mark_node if
17446 different overloads deduce different arguments for a given parm.
17447 ADDR_P is true if the expression for which deduction is being
17448 performed was of the form "& fn" rather than simply "fn".
17450 Returns 1 on success. */
17453 try_one_overload (tree tparms
,
17458 unification_kind_t strict
,
17467 if (arg
== error_mark_node
)
17470 /* [temp.deduct.type] A template-argument can be deduced from a pointer
17471 to function or pointer to member function argument if the set of
17472 overloaded functions does not contain function templates and at most
17473 one of a set of overloaded functions provides a unique match.
17475 So if this is a template, just return success. */
17477 if (uses_template_parms (arg
))
17480 if (TREE_CODE (arg
) == METHOD_TYPE
)
17481 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
17483 arg
= build_pointer_type (arg
);
17485 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
17487 /* We don't copy orig_targs for this because if we have already deduced
17488 some template args from previous args, unify would complain when we
17489 try to deduce a template parameter for the same argument, even though
17490 there isn't really a conflict. */
17491 nargs
= TREE_VEC_LENGTH (targs
);
17492 tempargs
= make_tree_vec (nargs
);
17494 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
17497 /* First make sure we didn't deduce anything that conflicts with
17498 explicitly specified args. */
17499 for (i
= nargs
; i
--; )
17501 tree elt
= TREE_VEC_ELT (tempargs
, i
);
17502 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
17506 else if (uses_template_parms (elt
))
17507 /* Since we're unifying against ourselves, we will fill in
17508 template args used in the function parm list with our own
17509 template parms. Discard them. */
17510 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
17511 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
17515 for (i
= nargs
; i
--; )
17517 tree elt
= TREE_VEC_ELT (tempargs
, i
);
17520 TREE_VEC_ELT (targs
, i
) = elt
;
17526 /* PARM is a template class (perhaps with unbound template
17527 parameters). ARG is a fully instantiated type. If ARG can be
17528 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
17529 TARGS are as for unify. */
17532 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
17535 tree copy_of_targs
;
17537 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
17538 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
17539 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
17542 /* We need to make a new template argument vector for the call to
17543 unify. If we used TARGS, we'd clutter it up with the result of
17544 the attempted unification, even if this class didn't work out.
17545 We also don't want to commit ourselves to all the unifications
17546 we've already done, since unification is supposed to be done on
17547 an argument-by-argument basis. In other words, consider the
17548 following pathological case:
17550 template <int I, int J, int K>
17553 template <int I, int J>
17554 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
17556 template <int I, int J, int K>
17557 void f(S<I, J, K>, S<I, I, I>);
17566 Now, by the time we consider the unification involving `s2', we
17567 already know that we must have `f<0, 0, 0>'. But, even though
17568 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
17569 because there are two ways to unify base classes of S<0, 1, 2>
17570 with S<I, I, I>. If we kept the already deduced knowledge, we
17571 would reject the possibility I=1. */
17572 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
17574 /* If unification failed, we're done. */
17575 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
17576 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
17582 /* Given a template type PARM and a class type ARG, find the unique
17583 base type in ARG that is an instance of PARM. We do not examine
17584 ARG itself; only its base-classes. If there is not exactly one
17585 appropriate base class, return NULL_TREE. PARM may be the type of
17586 a partial specialization, as well as a plain template type. Used
17589 static enum template_base_result
17590 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
17591 bool explain_p
, tree
*result
)
17593 tree rval
= NULL_TREE
;
17596 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
17598 binfo
= TYPE_BINFO (complete_type (arg
));
17601 /* The type could not be completed. */
17602 *result
= NULL_TREE
;
17603 return tbr_incomplete_type
;
17606 /* Walk in inheritance graph order. The search order is not
17607 important, and this avoids multiple walks of virtual bases. */
17608 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
17610 tree r
= try_class_unification (tparms
, targs
, parm
,
17611 BINFO_TYPE (binfo
), explain_p
);
17615 /* If there is more than one satisfactory baseclass, then:
17619 If they yield more than one possible deduced A, the type
17623 if (rval
&& !same_type_p (r
, rval
))
17625 *result
= NULL_TREE
;
17626 return tbr_ambiguous_baseclass
;
17634 return tbr_success
;
17637 /* Returns the level of DECL, which declares a template parameter. */
17640 template_decl_level (tree decl
)
17642 switch (TREE_CODE (decl
))
17645 case TEMPLATE_DECL
:
17646 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
17649 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
17652 gcc_unreachable ();
17657 /* Decide whether ARG can be unified with PARM, considering only the
17658 cv-qualifiers of each type, given STRICT as documented for unify.
17659 Returns nonzero iff the unification is OK on that basis. */
17662 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
17664 int arg_quals
= cp_type_quals (arg
);
17665 int parm_quals
= cp_type_quals (parm
);
17667 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17668 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
17670 /* Although a CVR qualifier is ignored when being applied to a
17671 substituted template parameter ([8.3.2]/1 for example), that
17672 does not allow us to unify "const T" with "int&" because both
17673 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
17674 It is ok when we're allowing additional CV qualifiers
17675 at the outer level [14.8.2.1]/3,1st bullet. */
17676 if ((TREE_CODE (arg
) == REFERENCE_TYPE
17677 || TREE_CODE (arg
) == FUNCTION_TYPE
17678 || TREE_CODE (arg
) == METHOD_TYPE
)
17679 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
17682 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
17683 && (parm_quals
& TYPE_QUAL_RESTRICT
))
17687 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
17688 && (arg_quals
& parm_quals
) != parm_quals
)
17691 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
17692 && (parm_quals
& arg_quals
) != arg_quals
)
17698 /* Determines the LEVEL and INDEX for the template parameter PARM. */
17700 template_parm_level_and_index (tree parm
, int* level
, int* index
)
17702 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17703 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
17704 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17706 *index
= TEMPLATE_TYPE_IDX (parm
);
17707 *level
= TEMPLATE_TYPE_LEVEL (parm
);
17711 *index
= TEMPLATE_PARM_IDX (parm
);
17712 *level
= TEMPLATE_PARM_LEVEL (parm
);
17716 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
17718 if (unify (TP, TA, P, A, S, EP)) \
17722 /* Unifies the remaining arguments in PACKED_ARGS with the pack
17723 expansion at the end of PACKED_PARMS. Returns 0 if the type
17724 deduction succeeds, 1 otherwise. STRICT is the same as in
17725 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
17726 call argument list. We'll need to adjust the arguments to make them
17727 types. SUBR tells us if this is from a recursive call to
17728 type_unification_real, or for comparing two template argument
17732 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
17733 tree packed_args
, unification_kind_t strict
,
17734 bool subr
, bool explain_p
)
17737 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
17738 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
17739 tree pack
, packs
= NULL_TREE
;
17740 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
17742 packed_args
= expand_template_argument_pack (packed_args
);
17744 int len
= TREE_VEC_LENGTH (packed_args
);
17746 /* Determine the parameter packs we will be deducing from the
17747 pattern, and record their current deductions. */
17748 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
17749 pack
; pack
= TREE_CHAIN (pack
))
17751 tree parm_pack
= TREE_VALUE (pack
);
17754 /* Determine the index and level of this parameter pack. */
17755 template_parm_level_and_index (parm_pack
, &level
, &idx
);
17757 /* Keep track of the parameter packs and their corresponding
17759 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
17760 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
17763 /* Loop through all of the arguments that have not yet been
17764 unified and unify each with the pattern. */
17765 for (i
= start
; i
< len
; i
++)
17768 bool any_explicit
= false;
17769 tree arg
= TREE_VEC_ELT (packed_args
, i
);
17771 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
17772 or the element of its argument pack at the current index if
17773 this argument was explicitly specified. */
17774 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
17778 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
17781 if (TREE_VALUE (pack
)
17782 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
17783 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
17785 any_explicit
= true;
17786 arg
= TREE_VEC_ELT (pargs
, i
- start
);
17788 TMPL_ARG (targs
, level
, idx
) = arg
;
17791 /* If we had explicit template arguments, substitute them into the
17792 pattern before deduction. */
17795 /* Some arguments might still be unspecified or dependent. */
17797 ++processing_template_decl
;
17798 dependent
= any_dependent_template_arguments_p (targs
);
17800 --processing_template_decl
;
17801 parm
= tsubst (pattern
, targs
,
17802 explain_p
? tf_warning_or_error
: tf_none
,
17805 --processing_template_decl
;
17806 if (parm
== error_mark_node
)
17812 /* Unify the pattern with the current argument. */
17813 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
17814 LOOKUP_IMPLICIT
, explain_p
))
17817 /* For each parameter pack, collect the deduced value. */
17818 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
17821 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
17823 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
17824 TMPL_ARG (targs
, level
, idx
);
17828 /* Verify that the results of unification with the parameter packs
17829 produce results consistent with what we've seen before, and make
17830 the deduced argument packs available. */
17831 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
17833 tree old_pack
= TREE_VALUE (pack
);
17834 tree new_args
= TREE_TYPE (pack
);
17835 int i
, len
= TREE_VEC_LENGTH (new_args
);
17837 bool nondeduced_p
= false;
17839 /* By default keep the original deduced argument pack.
17840 If necessary, more specific code is going to update the
17841 resulting deduced argument later down in this function. */
17842 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
17843 TMPL_ARG (targs
, level
, idx
) = old_pack
;
17845 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
17846 actually deduce anything. */
17847 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
17848 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
17849 nondeduced_p
= true;
17853 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
17855 /* If we had fewer function args than explicit template args,
17856 just use the explicits. */
17857 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
17858 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
17859 if (len
< explicit_len
)
17860 new_args
= explicit_args
;
17866 /* Build the deduced *_ARGUMENT_PACK. */
17867 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
17869 result
= make_node (NONTYPE_ARGUMENT_PACK
);
17870 TREE_TYPE (result
) =
17871 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
17872 TREE_CONSTANT (result
) = 1;
17875 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
17877 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
17879 /* Note the deduced argument packs for this parameter
17881 TMPL_ARG (targs
, level
, idx
) = result
;
17883 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
17884 && (ARGUMENT_PACK_ARGS (old_pack
)
17885 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
17887 /* We only had the explicitly-provided arguments before, but
17888 now we have a complete set of arguments. */
17889 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
17891 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
17892 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
17893 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
17897 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
17898 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
17900 if (!comp_template_args_with_info (old_args
, new_args
,
17901 &bad_old_arg
, &bad_new_arg
))
17902 /* Inconsistent unification of this parameter pack. */
17903 return unify_parameter_pack_inconsistent (explain_p
,
17909 return unify_success (explain_p
);
17912 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17913 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17914 parameters and return value are as for unify. */
17917 unify_array_domain (tree tparms
, tree targs
,
17918 tree parm_dom
, tree arg_dom
,
17926 /* Our representation of array types uses "N - 1" as the
17927 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17928 not an integer constant. We cannot unify arbitrarily
17929 complex expressions, so we eliminate the MINUS_EXPRs
17931 parm_max
= TYPE_MAX_VALUE (parm_dom
);
17932 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
17935 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
17936 parm_max
= TREE_OPERAND (parm_max
, 0);
17938 arg_max
= TYPE_MAX_VALUE (arg_dom
);
17939 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
17942 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17943 trying to unify the type of a variable with the type
17944 of a template parameter. For example:
17946 template <unsigned int N>
17947 void f (char (&) [N]);
17954 Here, the type of the ARG will be "int [g(i)]", and
17955 may be a SAVE_EXPR, etc. */
17956 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
17957 return unify_vla_arg (explain_p
, arg_dom
);
17958 arg_max
= TREE_OPERAND (arg_max
, 0);
17961 /* If only one of the bounds used a MINUS_EXPR, compensate
17962 by adding one to the other bound. */
17963 if (parm_cst
&& !arg_cst
)
17964 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
17968 else if (arg_cst
&& !parm_cst
)
17969 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
17974 return unify (tparms
, targs
, parm_max
, arg_max
,
17975 UNIFY_ALLOW_INTEGER
, explain_p
);
17978 /* Deduce the value of template parameters. TPARMS is the (innermost)
17979 set of template parameters to a template. TARGS is the bindings
17980 for those template parameters, as determined thus far; TARGS may
17981 include template arguments for outer levels of template parameters
17982 as well. PARM is a parameter to a template function, or a
17983 subcomponent of that parameter; ARG is the corresponding argument.
17984 This function attempts to match PARM with ARG in a manner
17985 consistent with the existing assignments in TARGS. If more values
17986 are deduced, then TARGS is updated.
17988 Returns 0 if the type deduction succeeds, 1 otherwise. The
17989 parameter STRICT is a bitwise or of the following flags:
17992 Require an exact match between PARM and ARG.
17993 UNIFY_ALLOW_MORE_CV_QUAL:
17994 Allow the deduced ARG to be more cv-qualified (by qualification
17995 conversion) than ARG.
17996 UNIFY_ALLOW_LESS_CV_QUAL:
17997 Allow the deduced ARG to be less cv-qualified than ARG.
17998 UNIFY_ALLOW_DERIVED:
17999 Allow the deduced ARG to be a template base class of ARG,
18000 or a pointer to a template base class of the type pointed to by
18002 UNIFY_ALLOW_INTEGER:
18003 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
18004 case for more information.
18005 UNIFY_ALLOW_OUTER_LEVEL:
18006 This is the outermost level of a deduction. Used to determine validity
18007 of qualification conversions. A valid qualification conversion must
18008 have const qualified pointers leading up to the inner type which
18009 requires additional CV quals, except at the outer level, where const
18010 is not required [conv.qual]. It would be normal to set this flag in
18011 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
18012 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
18013 This is the outermost level of a deduction, and PARM can be more CV
18014 qualified at this point.
18015 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
18016 This is the outermost level of a deduction, and PARM can be less CV
18017 qualified at this point. */
18020 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
18026 int strict_in
= strict
;
18028 /* I don't think this will do the right thing with respect to types.
18029 But the only case I've seen it in so far has been array bounds, where
18030 signedness is the only information lost, and I think that will be
18032 while (TREE_CODE (parm
) == NOP_EXPR
)
18033 parm
= TREE_OPERAND (parm
, 0);
18035 if (arg
== error_mark_node
)
18036 return unify_invalid (explain_p
);
18037 if (arg
== unknown_type_node
18038 || arg
== init_list_type_node
)
18039 /* We can't deduce anything from this, but we might get all the
18040 template args from other function args. */
18041 return unify_success (explain_p
);
18043 /* If PARM uses template parameters, then we can't bail out here,
18044 even if ARG == PARM, since we won't record unifications for the
18045 template parameters. We might need them if we're trying to
18046 figure out which of two things is more specialized. */
18047 if (arg
== parm
&& !uses_template_parms (parm
))
18048 return unify_success (explain_p
);
18050 /* Handle init lists early, so the rest of the function can assume
18051 we're dealing with a type. */
18052 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
18056 tree orig_parm
= parm
;
18058 /* Replace T with std::initializer_list<T> for deduction. */
18059 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
18060 && flag_deduce_init_list
)
18061 parm
= listify (parm
);
18063 if (!is_std_init_list (parm
)
18064 && TREE_CODE (parm
) != ARRAY_TYPE
)
18065 /* We can only deduce from an initializer list argument if the
18066 parameter is std::initializer_list or an array; otherwise this
18067 is a non-deduced context. */
18068 return unify_success (explain_p
);
18070 if (TREE_CODE (parm
) == ARRAY_TYPE
)
18071 elttype
= TREE_TYPE (parm
);
18074 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
18075 /* Deduction is defined in terms of a single type, so just punt
18076 on the (bizarre) std::initializer_list<T...>. */
18077 if (PACK_EXPANSION_P (elttype
))
18078 return unify_success (explain_p
);
18081 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
18083 int elt_strict
= strict
;
18085 if (elt
== error_mark_node
)
18086 return unify_invalid (explain_p
);
18088 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
18090 tree type
= TREE_TYPE (elt
);
18091 if (type
== error_mark_node
)
18092 return unify_invalid (explain_p
);
18093 /* It should only be possible to get here for a call. */
18094 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
18095 elt_strict
|= maybe_adjust_types_for_deduction
18096 (DEDUCE_CALL
, &elttype
, &type
, elt
);
18100 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
18104 if (TREE_CODE (parm
) == ARRAY_TYPE
18105 && deducible_array_bound (TYPE_DOMAIN (parm
)))
18107 /* Also deduce from the length of the initializer list. */
18108 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
18109 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
18110 if (idx
== error_mark_node
)
18111 return unify_invalid (explain_p
);
18112 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
18116 /* If the std::initializer_list<T> deduction worked, replace the
18117 deduced A with std::initializer_list<A>. */
18118 if (orig_parm
!= parm
)
18120 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
18121 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
18122 targ
= listify (targ
);
18123 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
18125 return unify_success (explain_p
);
18128 /* Immediately reject some pairs that won't unify because of
18129 cv-qualification mismatches. */
18130 if (TREE_CODE (arg
) == TREE_CODE (parm
)
18132 /* It is the elements of the array which hold the cv quals of an array
18133 type, and the elements might be template type parms. We'll check
18134 when we recurse. */
18135 && TREE_CODE (arg
) != ARRAY_TYPE
18136 /* We check the cv-qualifiers when unifying with template type
18137 parameters below. We want to allow ARG `const T' to unify with
18138 PARM `T' for example, when computing which of two templates
18139 is more specialized, for example. */
18140 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
18141 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
18142 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
18144 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
18145 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
18146 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
18147 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
18148 strict
&= ~UNIFY_ALLOW_DERIVED
;
18149 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
18150 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
18152 switch (TREE_CODE (parm
))
18154 case TYPENAME_TYPE
:
18156 case UNBOUND_CLASS_TEMPLATE
:
18157 /* In a type which contains a nested-name-specifier, template
18158 argument values cannot be deduced for template parameters used
18159 within the nested-name-specifier. */
18160 return unify_success (explain_p
);
18162 case TEMPLATE_TYPE_PARM
:
18163 case TEMPLATE_TEMPLATE_PARM
:
18164 case BOUND_TEMPLATE_TEMPLATE_PARM
:
18165 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
18166 if (error_operand_p (tparm
))
18167 return unify_invalid (explain_p
);
18169 if (TEMPLATE_TYPE_LEVEL (parm
)
18170 != template_decl_level (tparm
))
18171 /* The PARM is not one we're trying to unify. Just check
18172 to see if it matches ARG. */
18174 if (TREE_CODE (arg
) == TREE_CODE (parm
)
18175 && (is_auto (parm
) ? is_auto (arg
)
18176 : same_type_p (parm
, arg
)))
18177 return unify_success (explain_p
);
18179 return unify_type_mismatch (explain_p
, parm
, arg
);
18181 idx
= TEMPLATE_TYPE_IDX (parm
);
18182 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
18183 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
18184 if (error_operand_p (tparm
))
18185 return unify_invalid (explain_p
);
18187 /* Check for mixed types and values. */
18188 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
18189 && TREE_CODE (tparm
) != TYPE_DECL
)
18190 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
18191 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
18192 gcc_unreachable ();
18194 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
18196 /* ARG must be constructed from a template class or a template
18197 template parameter. */
18198 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
18199 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
18200 return unify_template_deduction_failure (explain_p
, parm
, arg
);
18202 tree parmvec
= TYPE_TI_ARGS (parm
);
18203 /* An alias template name is never deduced. */
18204 if (TYPE_ALIAS_P (arg
))
18205 arg
= strip_typedefs (arg
);
18206 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
18207 tree full_argvec
= add_to_template_args (targs
, argvec
);
18209 = DECL_INNERMOST_TEMPLATE_PARMS
18210 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
18212 int parm_variadic_p
= 0;
18214 /* The resolution to DR150 makes clear that default
18215 arguments for an N-argument may not be used to bind T
18216 to a template template parameter with fewer than N
18217 parameters. It is not safe to permit the binding of
18218 default arguments as an extension, as that may change
18219 the meaning of a conforming program. Consider:
18221 struct Dense { static const unsigned int dim = 1; };
18223 template <template <typename> class View,
18225 void operator+(float, View<Block> const&);
18227 template <typename Block,
18228 unsigned int Dim = Block::dim>
18229 struct Lvalue_proxy { operator float() const; };
18233 Lvalue_proxy<Dense> p;
18238 Here, if Lvalue_proxy is permitted to bind to View, then
18239 the global operator+ will be used; if they are not, the
18240 Lvalue_proxy will be converted to float. */
18241 if (coerce_template_parms (parm_parms
,
18243 TYPE_TI_TEMPLATE (parm
),
18245 ? tf_warning_or_error
18247 /*require_all_args=*/true,
18248 /*use_default_args=*/false)
18249 == error_mark_node
)
18252 /* Deduce arguments T, i from TT<T> or TT<i>.
18253 We check each element of PARMVEC and ARGVEC individually
18254 rather than the whole TREE_VEC since they can have
18255 different number of elements. */
18257 parmvec
= expand_template_argument_pack (parmvec
);
18258 argvec
= expand_template_argument_pack (argvec
);
18260 len
= TREE_VEC_LENGTH (parmvec
);
18262 /* Check if the parameters end in a pack, making them
18265 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
18266 parm_variadic_p
= 1;
18268 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
18269 /* If the template argument list of P contains a pack
18270 expansion that is not the last template argument, the
18271 entire template argument list is a non-deduced
18273 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
18274 return unify_success (explain_p
);
18276 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
18277 return unify_too_few_arguments (explain_p
,
18278 TREE_VEC_LENGTH (argvec
), len
);
18280 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
18282 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
18283 TREE_VEC_ELT (parmvec
, i
),
18284 TREE_VEC_ELT (argvec
, i
),
18285 UNIFY_ALLOW_NONE
, explain_p
);
18288 if (parm_variadic_p
18289 && unify_pack_expansion (tparms
, targs
,
18292 /*subr=*/true, explain_p
))
18295 arg
= TYPE_TI_TEMPLATE (arg
);
18297 /* Fall through to deduce template name. */
18300 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
18301 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
18303 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
18305 /* Simple cases: Value already set, does match or doesn't. */
18306 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
18307 return unify_success (explain_p
);
18309 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
18313 /* If PARM is `const T' and ARG is only `int', we don't have
18314 a match unless we are allowing additional qualification.
18315 If ARG is `const int' and PARM is just `T' that's OK;
18316 that binds `const int' to `T'. */
18317 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
18319 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
18321 /* Consider the case where ARG is `const volatile int' and
18322 PARM is `const T'. Then, T should be `volatile int'. */
18323 arg
= cp_build_qualified_type_real
18324 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
18325 if (arg
== error_mark_node
)
18326 return unify_invalid (explain_p
);
18328 /* Simple cases: Value already set, does match or doesn't. */
18329 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
18330 return unify_success (explain_p
);
18332 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
18334 /* Make sure that ARG is not a variable-sized array. (Note
18335 that were talking about variable-sized arrays (like
18336 `int[n]'), rather than arrays of unknown size (like
18337 `int[]').) We'll get very confused by such a type since
18338 the bound of the array is not constant, and therefore
18339 not mangleable. Besides, such types are not allowed in
18340 ISO C++, so we can do as we please here. We do allow
18341 them for 'auto' deduction, since that isn't ABI-exposed. */
18342 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
18343 return unify_vla_arg (explain_p
, arg
);
18345 /* Strip typedefs as in convert_template_argument. */
18346 arg
= canonicalize_type_argument (arg
, tf_none
);
18349 /* If ARG is a parameter pack or an expansion, we cannot unify
18350 against it unless PARM is also a parameter pack. */
18351 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
18352 && !template_parameter_pack_p (parm
))
18353 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
18355 /* If the argument deduction results is a METHOD_TYPE,
18356 then there is a problem.
18357 METHOD_TYPE doesn't map to any real C++ type the result of
18358 the deduction can not be of that type. */
18359 if (TREE_CODE (arg
) == METHOD_TYPE
)
18360 return unify_method_type_error (explain_p
, arg
);
18362 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
18363 return unify_success (explain_p
);
18365 case TEMPLATE_PARM_INDEX
:
18366 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
18367 if (error_operand_p (tparm
))
18368 return unify_invalid (explain_p
);
18370 if (TEMPLATE_PARM_LEVEL (parm
)
18371 != template_decl_level (tparm
))
18373 /* The PARM is not one we're trying to unify. Just check
18374 to see if it matches ARG. */
18375 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
18376 && cp_tree_equal (parm
, arg
));
18378 unify_expression_unequal (explain_p
, parm
, arg
);
18382 idx
= TEMPLATE_PARM_IDX (parm
);
18383 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
18387 int x
= !cp_tree_equal (targ
, arg
);
18389 unify_inconsistency (explain_p
, parm
, targ
, arg
);
18393 /* [temp.deduct.type] If, in the declaration of a function template
18394 with a non-type template-parameter, the non-type
18395 template-parameter is used in an expression in the function
18396 parameter-list and, if the corresponding template-argument is
18397 deduced, the template-argument type shall match the type of the
18398 template-parameter exactly, except that a template-argument
18399 deduced from an array bound may be of any integral type.
18400 The non-type parameter might use already deduced type parameters. */
18401 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
18402 if (!TREE_TYPE (arg
))
18403 /* Template-parameter dependent expression. Just accept it for now.
18404 It will later be processed in convert_template_argument. */
18406 else if (same_type_p (TREE_TYPE (arg
), tparm
))
18408 else if ((strict
& UNIFY_ALLOW_INTEGER
)
18409 && CP_INTEGRAL_TYPE_P (tparm
))
18410 /* Convert the ARG to the type of PARM; the deduced non-type
18411 template argument must exactly match the types of the
18412 corresponding parameter. */
18413 arg
= fold (build_nop (tparm
, arg
));
18414 else if (uses_template_parms (tparm
))
18415 /* We haven't deduced the type of this parameter yet. Try again
18417 return unify_success (explain_p
);
18419 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
18421 /* If ARG is a parameter pack or an expansion, we cannot unify
18422 against it unless PARM is also a parameter pack. */
18423 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
18424 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
18425 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
18427 arg
= strip_typedefs_expr (arg
);
18428 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
18429 return unify_success (explain_p
);
18433 /* A pointer-to-member constant can be unified only with
18434 another constant. */
18435 if (TREE_CODE (arg
) != PTRMEM_CST
)
18436 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
18438 /* Just unify the class member. It would be useless (and possibly
18439 wrong, depending on the strict flags) to unify also
18440 PTRMEM_CST_CLASS, because we want to be sure that both parm and
18441 arg refer to the same variable, even if through different
18442 classes. For instance:
18444 struct A { int x; };
18447 Unification of &A::x and &B::x must succeed. */
18448 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
18449 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
18454 if (!TYPE_PTR_P (arg
))
18455 return unify_type_mismatch (explain_p
, parm
, arg
);
18457 /* [temp.deduct.call]
18459 A can be another pointer or pointer to member type that can
18460 be converted to the deduced A via a qualification
18461 conversion (_conv.qual_).
18463 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
18464 This will allow for additional cv-qualification of the
18465 pointed-to types if appropriate. */
18467 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
18468 /* The derived-to-base conversion only persists through one
18469 level of pointers. */
18470 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
18472 return unify (tparms
, targs
, TREE_TYPE (parm
),
18473 TREE_TYPE (arg
), strict
, explain_p
);
18476 case REFERENCE_TYPE
:
18477 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
18478 return unify_type_mismatch (explain_p
, parm
, arg
);
18479 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
18480 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
18483 if (TREE_CODE (arg
) != ARRAY_TYPE
)
18484 return unify_type_mismatch (explain_p
, parm
, arg
);
18485 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
18486 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
18487 return unify_type_mismatch (explain_p
, parm
, arg
);
18488 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
18489 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
18490 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
18491 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
18492 TYPE_DOMAIN (arg
), explain_p
);
18493 return unify_success (explain_p
);
18500 case ENUMERAL_TYPE
:
18503 if (TREE_CODE (arg
) != TREE_CODE (parm
))
18504 return unify_type_mismatch (explain_p
, parm
, arg
);
18506 /* We have already checked cv-qualification at the top of the
18508 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
18509 return unify_type_mismatch (explain_p
, parm
, arg
);
18511 /* As far as unification is concerned, this wins. Later checks
18512 will invalidate it if necessary. */
18513 return unify_success (explain_p
);
18515 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
18516 /* Type INTEGER_CST can come from ordinary constant template args. */
18518 while (TREE_CODE (arg
) == NOP_EXPR
)
18519 arg
= TREE_OPERAND (arg
, 0);
18521 if (TREE_CODE (arg
) != INTEGER_CST
)
18522 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
18523 return (tree_int_cst_equal (parm
, arg
)
18524 ? unify_success (explain_p
)
18525 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
18529 int i
, len
, argslen
;
18530 int parm_variadic_p
= 0;
18532 if (TREE_CODE (arg
) != TREE_VEC
)
18533 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
18535 len
= TREE_VEC_LENGTH (parm
);
18536 argslen
= TREE_VEC_LENGTH (arg
);
18538 /* Check for pack expansions in the parameters. */
18539 for (i
= 0; i
< len
; ++i
)
18541 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
18544 /* We can unify against something with a trailing
18546 parm_variadic_p
= 1;
18548 /* [temp.deduct.type]/9: If the template argument list of
18549 P contains a pack expansion that is not the last
18550 template argument, the entire template argument list
18551 is a non-deduced context. */
18552 return unify_success (explain_p
);
18556 /* If we don't have enough arguments to satisfy the parameters
18557 (not counting the pack expression at the end), or we have
18558 too many arguments for a parameter list that doesn't end in
18559 a pack expression, we can't unify. */
18560 if (parm_variadic_p
18561 ? argslen
< len
- parm_variadic_p
18563 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
18565 /* Unify all of the parameters that precede the (optional)
18566 pack expression. */
18567 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
18569 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
18570 TREE_VEC_ELT (parm
, i
),
18571 TREE_VEC_ELT (arg
, i
),
18572 UNIFY_ALLOW_NONE
, explain_p
);
18574 if (parm_variadic_p
)
18575 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
18577 /*subr=*/true, explain_p
);
18578 return unify_success (explain_p
);
18583 if (TREE_CODE (arg
) != TREE_CODE (parm
))
18584 return unify_type_mismatch (explain_p
, parm
, arg
);
18586 if (TYPE_PTRMEMFUNC_P (parm
))
18588 if (!TYPE_PTRMEMFUNC_P (arg
))
18589 return unify_type_mismatch (explain_p
, parm
, arg
);
18591 return unify (tparms
, targs
,
18592 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
18593 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
18594 strict
, explain_p
);
18596 else if (TYPE_PTRMEMFUNC_P (arg
))
18597 return unify_type_mismatch (explain_p
, parm
, arg
);
18599 if (CLASSTYPE_TEMPLATE_INFO (parm
))
18601 tree t
= NULL_TREE
;
18603 if (strict_in
& UNIFY_ALLOW_DERIVED
)
18605 /* First, we try to unify the PARM and ARG directly. */
18606 t
= try_class_unification (tparms
, targs
,
18607 parm
, arg
, explain_p
);
18611 /* Fallback to the special case allowed in
18612 [temp.deduct.call]:
18614 If P is a class, and P has the form
18615 template-id, then A can be a derived class of
18616 the deduced A. Likewise, if P is a pointer to
18617 a class of the form template-id, A can be a
18618 pointer to a derived class pointed to by the
18620 enum template_base_result r
;
18621 r
= get_template_base (tparms
, targs
, parm
, arg
,
18625 return unify_no_common_base (explain_p
, r
, parm
, arg
);
18628 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
18629 && (CLASSTYPE_TI_TEMPLATE (parm
)
18630 == CLASSTYPE_TI_TEMPLATE (arg
)))
18631 /* Perhaps PARM is something like S<U> and ARG is S<int>.
18632 Then, we should unify `int' and `U'. */
18635 /* There's no chance of unification succeeding. */
18636 return unify_type_mismatch (explain_p
, parm
, arg
);
18638 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
18639 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
18641 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
18642 return unify_type_mismatch (explain_p
, parm
, arg
);
18643 return unify_success (explain_p
);
18646 case FUNCTION_TYPE
:
18648 unsigned int nargs
;
18653 if (TREE_CODE (arg
) != TREE_CODE (parm
))
18654 return unify_type_mismatch (explain_p
, parm
, arg
);
18656 /* CV qualifications for methods can never be deduced, they must
18657 match exactly. We need to check them explicitly here,
18658 because type_unification_real treats them as any other
18659 cv-qualified parameter. */
18660 if (TREE_CODE (parm
) == METHOD_TYPE
18661 && (!check_cv_quals_for_unify
18663 class_of_this_parm (arg
),
18664 class_of_this_parm (parm
))))
18665 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
18667 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
18668 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
18670 nargs
= list_length (TYPE_ARG_TYPES (arg
));
18671 args
= XALLOCAVEC (tree
, nargs
);
18672 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
18673 a
!= NULL_TREE
&& a
!= void_list_node
;
18674 a
= TREE_CHAIN (a
), ++i
)
18675 args
[i
] = TREE_VALUE (a
);
18678 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
18679 args
, nargs
, 1, DEDUCE_EXACT
,
18680 LOOKUP_NORMAL
, NULL
, explain_p
);
18684 /* Unify a pointer to member with a pointer to member function, which
18685 deduces the type of the member as a function type. */
18686 if (TYPE_PTRMEMFUNC_P (arg
))
18688 /* Check top-level cv qualifiers */
18689 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
18690 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
18692 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
18693 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
18694 UNIFY_ALLOW_NONE
, explain_p
);
18696 /* Determine the type of the function we are unifying against. */
18697 tree fntype
= static_fn_type (arg
);
18699 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
18702 if (TREE_CODE (arg
) != OFFSET_TYPE
)
18703 return unify_type_mismatch (explain_p
, parm
, arg
);
18704 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
18705 TYPE_OFFSET_BASETYPE (arg
),
18706 UNIFY_ALLOW_NONE
, explain_p
);
18707 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
18708 strict
, explain_p
);
18711 if (DECL_TEMPLATE_PARM_P (parm
))
18712 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
18713 if (arg
!= scalar_constant_value (parm
))
18714 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
18715 return unify_success (explain_p
);
18718 case TEMPLATE_DECL
:
18719 /* Matched cases are handled by the ARG == PARM test above. */
18720 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
18723 /* A non-type template parameter that is a variable should be a
18724 an integral constant, in which case, it whould have been
18725 folded into its (constant) value. So we should not be getting
18726 a variable here. */
18727 gcc_unreachable ();
18729 case TYPE_ARGUMENT_PACK
:
18730 case NONTYPE_ARGUMENT_PACK
:
18731 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
18732 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
18735 case DECLTYPE_TYPE
:
18736 case UNDERLYING_TYPE
:
18737 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
18738 or UNDERLYING_TYPE nodes. */
18739 return unify_success (explain_p
);
18742 /* Unification fails if we hit an error node. */
18743 return unify_invalid (explain_p
);
18746 if (REFERENCE_REF_P (parm
))
18748 if (REFERENCE_REF_P (arg
))
18749 arg
= TREE_OPERAND (arg
, 0);
18750 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
18751 strict
, explain_p
);
18756 /* An unresolved overload is a nondeduced context. */
18757 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
18758 return unify_success (explain_p
);
18759 gcc_assert (EXPR_P (parm
));
18761 /* We must be looking at an expression. This can happen with
18765 void foo(S<I>, S<I + 2>);
18767 This is a "nondeduced context":
18771 The nondeduced contexts are:
18773 --A type that is a template-id in which one or more of
18774 the template-arguments is an expression that references
18775 a template-parameter.
18777 In these cases, we assume deduction succeeded, but don't
18778 actually infer any unifications. */
18780 if (!uses_template_parms (parm
)
18781 && !template_args_equal (parm
, arg
))
18782 return unify_expression_unequal (explain_p
, parm
, arg
);
18784 return unify_success (explain_p
);
18787 #undef RECUR_AND_CHECK_FAILURE
18789 /* Note that DECL can be defined in this translation unit, if
18793 mark_definable (tree decl
)
18796 DECL_NOT_REALLY_EXTERN (decl
) = 1;
18797 FOR_EACH_CLONE (clone
, decl
)
18798 DECL_NOT_REALLY_EXTERN (clone
) = 1;
18801 /* Called if RESULT is explicitly instantiated, or is a member of an
18802 explicitly instantiated class. */
18805 mark_decl_instantiated (tree result
, int extern_p
)
18807 SET_DECL_EXPLICIT_INSTANTIATION (result
);
18809 /* If this entity has already been written out, it's too late to
18810 make any modifications. */
18811 if (TREE_ASM_WRITTEN (result
))
18814 /* For anonymous namespace we don't need to do anything. */
18815 if (decl_anon_ns_mem_p (result
))
18817 gcc_assert (!TREE_PUBLIC (result
));
18821 if (TREE_CODE (result
) != FUNCTION_DECL
)
18822 /* The TREE_PUBLIC flag for function declarations will have been
18823 set correctly by tsubst. */
18824 TREE_PUBLIC (result
) = 1;
18826 /* This might have been set by an earlier implicit instantiation. */
18827 DECL_COMDAT (result
) = 0;
18830 DECL_NOT_REALLY_EXTERN (result
) = 0;
18833 mark_definable (result
);
18834 mark_needed (result
);
18835 /* Always make artificials weak. */
18836 if (DECL_ARTIFICIAL (result
) && flag_weak
)
18837 comdat_linkage (result
);
18838 /* For WIN32 we also want to put explicit instantiations in
18839 linkonce sections. */
18840 else if (TREE_PUBLIC (result
))
18841 maybe_make_one_only (result
);
18844 /* If EXTERN_P, then this function will not be emitted -- unless
18845 followed by an explicit instantiation, at which point its linkage
18846 will be adjusted. If !EXTERN_P, then this function will be
18847 emitted here. In neither circumstance do we want
18848 import_export_decl to adjust the linkage. */
18849 DECL_INTERFACE_KNOWN (result
) = 1;
18852 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
18853 important template arguments. If any are missing, we check whether
18854 they're important by using error_mark_node for substituting into any
18855 args that were used for partial ordering (the ones between ARGS and END)
18856 and seeing if it bubbles up. */
18859 check_undeduced_parms (tree targs
, tree args
, tree end
)
18861 bool found
= false;
18863 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
18864 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
18867 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
18871 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
18872 if (substed
== error_mark_node
)
18878 /* Given two function templates PAT1 and PAT2, return:
18880 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
18881 -1 if PAT2 is more specialized than PAT1.
18882 0 if neither is more specialized.
18884 LEN indicates the number of parameters we should consider
18885 (defaulted parameters should not be considered).
18887 The 1998 std underspecified function template partial ordering, and
18888 DR214 addresses the issue. We take pairs of arguments, one from
18889 each of the templates, and deduce them against each other. One of
18890 the templates will be more specialized if all the *other*
18891 template's arguments deduce against its arguments and at least one
18892 of its arguments *does* *not* deduce against the other template's
18893 corresponding argument. Deduction is done as for class templates.
18894 The arguments used in deduction have reference and top level cv
18895 qualifiers removed. Iff both arguments were originally reference
18896 types *and* deduction succeeds in both directions, an lvalue reference
18897 wins against an rvalue reference and otherwise the template
18898 with the more cv-qualified argument wins for that pairing (if
18899 neither is more cv-qualified, they both are equal). Unlike regular
18900 deduction, after all the arguments have been deduced in this way,
18901 we do *not* verify the deduced template argument values can be
18902 substituted into non-deduced contexts.
18904 The logic can be a bit confusing here, because we look at deduce1 and
18905 targs1 to see if pat2 is at least as specialized, and vice versa; if we
18906 can find template arguments for pat1 to make arg1 look like arg2, that
18907 means that arg2 is at least as specialized as arg1. */
18910 more_specialized_fn (tree pat1
, tree pat2
, int len
)
18912 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
18913 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
18914 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
18915 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
18916 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
18917 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
18918 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
18919 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
18920 tree origs1
, origs2
;
18921 bool lose1
= false;
18922 bool lose2
= false;
18924 /* Remove the this parameter from non-static member functions. If
18925 one is a non-static member function and the other is not a static
18926 member function, remove the first parameter from that function
18927 also. This situation occurs for operator functions where we
18928 locate both a member function (with this pointer) and non-member
18929 operator (with explicit first operand). */
18930 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
18932 len
--; /* LEN is the number of significant arguments for DECL1 */
18933 args1
= TREE_CHAIN (args1
);
18934 if (!DECL_STATIC_FUNCTION_P (decl2
))
18935 args2
= TREE_CHAIN (args2
);
18937 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
18939 args2
= TREE_CHAIN (args2
);
18940 if (!DECL_STATIC_FUNCTION_P (decl1
))
18943 args1
= TREE_CHAIN (args1
);
18947 /* If only one is a conversion operator, they are unordered. */
18948 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
18951 /* Consider the return type for a conversion function */
18952 if (DECL_CONV_FN_P (decl1
))
18954 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
18955 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
18959 processing_template_decl
++;
18965 /* Stop when an ellipsis is seen. */
18966 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
18968 tree arg1
= TREE_VALUE (args1
);
18969 tree arg2
= TREE_VALUE (args2
);
18970 int deduce1
, deduce2
;
18976 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
18977 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18979 /* When both arguments are pack expansions, we need only
18980 unify the patterns themselves. */
18981 arg1
= PACK_EXPANSION_PATTERN (arg1
);
18982 arg2
= PACK_EXPANSION_PATTERN (arg2
);
18984 /* This is the last comparison we need to do. */
18988 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
18990 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
18991 arg1
= TREE_TYPE (arg1
);
18992 quals1
= cp_type_quals (arg1
);
18995 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
18997 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
18998 arg2
= TREE_TYPE (arg2
);
18999 quals2
= cp_type_quals (arg2
);
19002 arg1
= TYPE_MAIN_VARIANT (arg1
);
19003 arg2
= TYPE_MAIN_VARIANT (arg2
);
19005 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
19007 int i
, len2
= list_length (args2
);
19008 tree parmvec
= make_tree_vec (1);
19009 tree argvec
= make_tree_vec (len2
);
19012 /* Setup the parameter vector, which contains only ARG1. */
19013 TREE_VEC_ELT (parmvec
, 0) = arg1
;
19015 /* Setup the argument vector, which contains the remaining
19017 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
19018 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
19020 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
19021 argvec
, DEDUCE_EXACT
,
19022 /*subr=*/true, /*explain_p=*/false)
19025 /* We cannot deduce in the other direction, because ARG1 is
19026 a pack expansion but ARG2 is not. */
19029 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
19031 int i
, len1
= list_length (args1
);
19032 tree parmvec
= make_tree_vec (1);
19033 tree argvec
= make_tree_vec (len1
);
19036 /* Setup the parameter vector, which contains only ARG1. */
19037 TREE_VEC_ELT (parmvec
, 0) = arg2
;
19039 /* Setup the argument vector, which contains the remaining
19041 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
19042 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
19044 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
19045 argvec
, DEDUCE_EXACT
,
19046 /*subr=*/true, /*explain_p=*/false)
19049 /* We cannot deduce in the other direction, because ARG2 is
19050 a pack expansion but ARG1 is not.*/
19056 /* The normal case, where neither argument is a pack
19058 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
19059 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
19061 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
19062 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
19066 /* If we couldn't deduce arguments for tparms1 to make arg1 match
19067 arg2, then arg2 is not as specialized as arg1. */
19073 /* "If, for a given type, deduction succeeds in both directions
19074 (i.e., the types are identical after the transformations above)
19075 and both P and A were reference types (before being replaced with
19076 the type referred to above):
19077 - if the type from the argument template was an lvalue reference and
19078 the type from the parameter template was not, the argument type is
19079 considered to be more specialized than the other; otherwise,
19080 - if the type from the argument template is more cv-qualified
19081 than the type from the parameter template (as described above),
19082 the argument type is considered to be more specialized than the other;
19084 - neither type is more specialized than the other." */
19086 if (deduce1
&& deduce2
)
19088 if (ref1
&& ref2
&& ref1
!= ref2
)
19095 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
19097 if ((quals1
& quals2
) == quals2
)
19099 if ((quals1
& quals2
) == quals1
)
19104 if (lose1
&& lose2
)
19105 /* We've failed to deduce something in either direction.
19106 These must be unordered. */
19109 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
19110 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
19111 /* We have already processed all of the arguments in our
19112 handing of the pack expansion type. */
19115 args1
= TREE_CHAIN (args1
);
19116 args2
= TREE_CHAIN (args2
);
19119 /* "In most cases, all template parameters must have values in order for
19120 deduction to succeed, but for partial ordering purposes a template
19121 parameter may remain without a value provided it is not used in the
19122 types being used for partial ordering."
19124 Thus, if we are missing any of the targs1 we need to substitute into
19125 origs1, then pat2 is not as specialized as pat1. This can happen when
19126 there is a nondeduced context. */
19127 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
19129 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
19132 processing_template_decl
--;
19134 /* All things being equal, if the next argument is a pack expansion
19135 for one function but not for the other, prefer the
19136 non-variadic function. FIXME this is bogus; see c++/41958. */
19138 && args1
&& TREE_VALUE (args1
)
19139 && args2
&& TREE_VALUE (args2
))
19141 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
19142 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
19145 if (lose1
== lose2
)
19153 /* Determine which of two partial specializations of TMPL is more
19156 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
19157 to the first partial specialization. The TREE_PURPOSE is the
19158 innermost set of template parameters for the partial
19159 specialization. PAT2 is similar, but for the second template.
19161 Return 1 if the first partial specialization is more specialized;
19162 -1 if the second is more specialized; 0 if neither is more
19165 See [temp.class.order] for information about determining which of
19166 two templates is more specialized. */
19169 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
19173 bool any_deductions
= false;
19175 tree tmpl1
= TREE_VALUE (pat1
);
19176 tree tmpl2
= TREE_VALUE (pat2
);
19177 tree parms1
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl1
);
19178 tree parms2
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl2
);
19179 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
19180 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
19182 /* Just like what happens for functions, if we are ordering between
19183 different template specializations, we may encounter dependent
19184 types in the arguments, and we need our dependency check functions
19185 to behave correctly. */
19186 ++processing_template_decl
;
19187 targs
= get_partial_spec_bindings (tmpl
, parms1
, specargs1
, specargs2
);
19191 any_deductions
= true;
19194 targs
= get_partial_spec_bindings (tmpl
, parms2
, specargs2
, specargs1
);
19198 any_deductions
= true;
19200 --processing_template_decl
;
19202 /* In the case of a tie where at least one of the templates
19203 has a parameter pack at the end, the template with the most
19204 non-packed parameters wins. */
19207 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
19208 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
19210 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
19211 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
19212 int len1
= TREE_VEC_LENGTH (args1
);
19213 int len2
= TREE_VEC_LENGTH (args2
);
19215 /* We don't count the pack expansion at the end. */
19216 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
19218 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
19223 else if (len1
< len2
)
19230 /* Return the template arguments that will produce the function signature
19231 DECL from the function template FN, with the explicit template
19232 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
19233 also match. Return NULL_TREE if no satisfactory arguments could be
19237 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
19239 int ntparms
= DECL_NTPARMS (fn
);
19240 tree targs
= make_tree_vec (ntparms
);
19241 tree decl_type
= TREE_TYPE (decl
);
19242 tree decl_arg_types
;
19244 unsigned int nargs
, ix
;
19247 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
19249 /* Never do unification on the 'this' parameter. */
19250 decl_arg_types
= skip_artificial_parms_for (decl
,
19251 TYPE_ARG_TYPES (decl_type
));
19253 nargs
= list_length (decl_arg_types
);
19254 args
= XALLOCAVEC (tree
, nargs
);
19255 for (arg
= decl_arg_types
, ix
= 0;
19256 arg
!= NULL_TREE
&& arg
!= void_list_node
;
19257 arg
= TREE_CHAIN (arg
), ++ix
)
19258 args
[ix
] = TREE_VALUE (arg
);
19260 if (fn_type_unification (fn
, explicit_args
, targs
,
19262 (check_rettype
|| DECL_CONV_FN_P (fn
)
19263 ? TREE_TYPE (decl_type
) : NULL_TREE
),
19264 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
19266 == error_mark_node
)
19272 /* Return the innermost template arguments that, when applied to a partial
19273 specialization of TMPL whose innermost template parameters are
19274 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
19277 For example, suppose we have:
19279 template <class T, class U> struct S {};
19280 template <class T> struct S<T*, int> {};
19282 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
19283 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
19284 int}. The resulting vector will be {double}, indicating that `T'
19285 is bound to `double'. */
19288 get_partial_spec_bindings (tree tmpl
, tree tparms
, tree spec_args
, tree args
)
19290 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
19292 tree innermost_deduced_args
;
19294 innermost_deduced_args
= make_tree_vec (ntparms
);
19295 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
19297 deduced_args
= copy_node (args
);
19298 SET_TMPL_ARGS_LEVEL (deduced_args
,
19299 TMPL_ARGS_DEPTH (deduced_args
),
19300 innermost_deduced_args
);
19303 deduced_args
= innermost_deduced_args
;
19305 if (unify (tparms
, deduced_args
,
19306 INNERMOST_TEMPLATE_ARGS (spec_args
),
19307 INNERMOST_TEMPLATE_ARGS (args
),
19308 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
19311 for (i
= 0; i
< ntparms
; ++i
)
19312 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
19315 /* Verify that nondeduced template arguments agree with the type
19316 obtained from argument deduction.
19320 struct A { typedef int X; };
19321 template <class T, class U> struct C {};
19322 template <class T> struct C<T, typename T::X> {};
19324 Then with the instantiation `C<A, int>', we can deduce that
19325 `T' is `A' but unify () does not check whether `typename T::X'
19327 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
19328 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
19330 tf_none
, false, false);
19331 if (spec_args
== error_mark_node
19332 /* We only need to check the innermost arguments; the other
19333 arguments will always agree. */
19334 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
19335 INNERMOST_TEMPLATE_ARGS (args
)))
19338 /* Now that we have bindings for all of the template arguments,
19339 ensure that the arguments deduced for the template template
19340 parameters have compatible template parameter lists. See the use
19341 of template_template_parm_bindings_ok_p in fn_type_unification
19342 for more information. */
19343 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
19346 return deduced_args
;
19349 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
19350 Return the TREE_LIST node with the most specialized template, if
19351 any. If there is no most specialized template, the error_mark_node
19354 Note that this function does not look at, or modify, the
19355 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
19356 returned is one of the elements of INSTANTIATIONS, callers may
19357 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
19358 and retrieve it from the value returned. */
19361 most_specialized_instantiation (tree templates
)
19365 ++processing_template_decl
;
19368 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
19372 if (get_bindings (TREE_VALUE (champ
),
19373 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
19374 NULL_TREE
, /*check_ret=*/true))
19377 if (get_bindings (TREE_VALUE (fn
),
19378 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
19379 NULL_TREE
, /*check_ret=*/true))
19386 /* Equally specialized, move to next function. If there
19387 is no next function, nothing's most specialized. */
19388 fn
= TREE_CHAIN (fn
);
19396 /* Now verify that champ is better than everything earlier in the
19397 instantiation list. */
19398 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
19399 if (get_bindings (TREE_VALUE (champ
),
19400 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
19401 NULL_TREE
, /*check_ret=*/true)
19402 || !get_bindings (TREE_VALUE (fn
),
19403 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
19404 NULL_TREE
, /*check_ret=*/true))
19410 processing_template_decl
--;
19413 return error_mark_node
;
19418 /* If DECL is a specialization of some template, return the most
19419 general such template. Otherwise, returns NULL_TREE.
19421 For example, given:
19423 template <class T> struct S { template <class U> void f(U); };
19425 if TMPL is `template <class U> void S<int>::f(U)' this will return
19426 the full template. This function will not trace past partial
19427 specializations, however. For example, given in addition:
19429 template <class T> struct S<T*> { template <class U> void f(U); };
19431 if TMPL is `template <class U> void S<int*>::f(U)' this will return
19432 `template <class T> template <class U> S<T*>::f(U)'. */
19435 most_general_template (tree decl
)
19437 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
19439 if (tree tinfo
= get_template_info (decl
))
19440 decl
= TI_TEMPLATE (tinfo
);
19441 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
19442 template friend, or a FIELD_DECL for a capture pack. */
19443 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
19447 /* Look for more and more general templates. */
19448 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
19450 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
19451 (See cp-tree.h for details.) */
19452 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
19455 if (CLASS_TYPE_P (TREE_TYPE (decl
))
19456 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
19457 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
19460 /* Stop if we run into an explicitly specialized class template. */
19461 if (!DECL_NAMESPACE_SCOPE_P (decl
)
19462 && DECL_CONTEXT (decl
)
19463 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
19466 decl
= DECL_TI_TEMPLATE (decl
);
19472 /* Return the most specialized of the template partial specializations
19473 which can produce TARGET, a specialization of some class or variable
19474 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
19475 a TEMPLATE_DECL node corresponding to the partial specialization, while
19476 the TREE_PURPOSE is the set of template arguments that must be
19477 substituted into the template pattern in order to generate TARGET.
19479 If the choice of partial specialization is ambiguous, a diagnostic
19480 is issued, and the error_mark_node is returned. If there are no
19481 partial specializations matching TARGET, then NULL_TREE is
19482 returned, indicating that the primary template should be used. */
19485 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
19487 tree list
= NULL_TREE
;
19492 tree outer_args
= NULL_TREE
;
19495 if (TYPE_P (target
))
19497 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
19498 tmpl
= TI_TEMPLATE (tinfo
);
19499 args
= TI_ARGS (tinfo
);
19501 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
19503 tmpl
= TREE_OPERAND (target
, 0);
19504 args
= TREE_OPERAND (target
, 1);
19506 else if (VAR_P (target
))
19508 tree tinfo
= DECL_TEMPLATE_INFO (target
);
19509 tmpl
= TI_TEMPLATE (tinfo
);
19510 args
= TI_ARGS (tinfo
);
19513 gcc_unreachable ();
19515 tree main_tmpl
= most_general_template (tmpl
);
19517 /* For determining which partial specialization to use, only the
19518 innermost args are interesting. */
19519 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
19521 outer_args
= strip_innermost_template_args (args
, 1);
19522 args
= INNERMOST_TEMPLATE_ARGS (args
);
19525 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
19527 tree partial_spec_args
;
19529 tree spec_tmpl
= TREE_VALUE (t
);
19531 partial_spec_args
= TREE_PURPOSE (t
);
19533 ++processing_template_decl
;
19537 /* Discard the outer levels of args, and then substitute in the
19538 template args from the enclosing class. */
19539 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
19540 partial_spec_args
= tsubst_template_args
19541 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
19543 /* And the same for the partial specialization TEMPLATE_DECL. */
19544 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
19547 partial_spec_args
=
19548 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
19551 /*require_all_args=*/true,
19552 /*use_default_args=*/true);
19554 --processing_template_decl
;
19556 if (partial_spec_args
== error_mark_node
)
19557 return error_mark_node
;
19558 if (spec_tmpl
== error_mark_node
)
19559 return error_mark_node
;
19561 tree parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
19562 spec_args
= get_partial_spec_bindings (tmpl
, parms
,
19568 spec_args
= add_to_template_args (outer_args
, spec_args
);
19569 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
19570 TREE_TYPE (list
) = TREE_TYPE (t
);
19577 ambiguous_p
= false;
19580 t
= TREE_CHAIN (t
);
19581 for (; t
; t
= TREE_CHAIN (t
))
19583 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
19590 t
= TREE_CHAIN (t
);
19593 ambiguous_p
= true;
19602 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
19604 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
19607 ambiguous_p
= true;
19615 char *spaces
= NULL
;
19616 if (!(complain
& tf_error
))
19617 return error_mark_node
;
19618 if (TYPE_P (target
))
19619 error ("ambiguous template instantiation for %q#T", target
);
19621 error ("ambiguous template instantiation for %q#D", target
);
19622 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
19623 for (t
= list
; t
; t
= TREE_CHAIN (t
))
19625 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
19626 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
19627 "%s %#S", spaces
? spaces
: str
, subst
);
19628 spaces
= spaces
? spaces
: get_spaces (str
);
19631 return error_mark_node
;
19637 /* Explicitly instantiate DECL. */
19640 do_decl_instantiation (tree decl
, tree storage
)
19642 tree result
= NULL_TREE
;
19645 if (!decl
|| decl
== error_mark_node
)
19646 /* An error occurred, for which grokdeclarator has already issued
19647 an appropriate message. */
19649 else if (! DECL_LANG_SPECIFIC (decl
))
19651 error ("explicit instantiation of non-template %q#D", decl
);
19655 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
19656 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
19658 if (VAR_P (decl
) && !var_templ
)
19660 /* There is an asymmetry here in the way VAR_DECLs and
19661 FUNCTION_DECLs are handled by grokdeclarator. In the case of
19662 the latter, the DECL we get back will be marked as a
19663 template instantiation, and the appropriate
19664 DECL_TEMPLATE_INFO will be set up. This does not happen for
19665 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
19666 should handle VAR_DECLs as it currently handles
19668 if (!DECL_CLASS_SCOPE_P (decl
))
19670 error ("%qD is not a static data member of a class template", decl
);
19673 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
19674 if (!result
|| !VAR_P (result
))
19676 error ("no matching template for %qD found", decl
);
19679 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
19681 error ("type %qT for explicit instantiation %qD does not match "
19682 "declared type %qT", TREE_TYPE (result
), decl
,
19687 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
19689 error ("explicit instantiation of %q#D", decl
);
19695 /* Check for various error cases. Note that if the explicit
19696 instantiation is valid the RESULT will currently be marked as an
19697 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
19698 until we get here. */
19700 if (DECL_TEMPLATE_SPECIALIZATION (result
))
19702 /* DR 259 [temp.spec].
19704 Both an explicit instantiation and a declaration of an explicit
19705 specialization shall not appear in a program unless the explicit
19706 instantiation follows a declaration of the explicit specialization.
19708 For a given set of template parameters, if an explicit
19709 instantiation of a template appears after a declaration of an
19710 explicit specialization for that template, the explicit
19711 instantiation has no effect. */
19714 else if (DECL_EXPLICIT_INSTANTIATION (result
))
19718 No program shall explicitly instantiate any template more
19721 We check DECL_NOT_REALLY_EXTERN so as not to complain when
19722 the first instantiation was `extern' and the second is not,
19723 and EXTERN_P for the opposite case. */
19724 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
19725 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
19726 /* If an "extern" explicit instantiation follows an ordinary
19727 explicit instantiation, the template is instantiated. */
19731 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
19733 error ("no matching template for %qD found", result
);
19736 else if (!DECL_TEMPLATE_INFO (result
))
19738 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
19742 if (storage
== NULL_TREE
)
19744 else if (storage
== ridpointers
[(int) RID_EXTERN
])
19746 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
19747 pedwarn (input_location
, OPT_Wpedantic
,
19748 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
19753 error ("storage class %qD applied to template instantiation", storage
);
19755 check_explicit_instantiation_namespace (result
);
19756 mark_decl_instantiated (result
, extern_p
);
19758 instantiate_decl (result
, /*defer_ok=*/1,
19759 /*expl_inst_class_mem_p=*/false);
19763 mark_class_instantiated (tree t
, int extern_p
)
19765 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
19766 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
19767 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
19768 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
19771 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
19772 rest_of_type_compilation (t
, 1);
19776 /* Called from do_type_instantiation through binding_table_foreach to
19777 do recursive instantiation for the type bound in ENTRY. */
19779 bt_instantiate_type_proc (binding_entry entry
, void *data
)
19781 tree storage
= *(tree
*) data
;
19783 if (MAYBE_CLASS_TYPE_P (entry
->type
)
19784 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
19785 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
19788 /* Called from do_type_instantiation to instantiate a member
19789 (a member function or a static member variable) of an
19790 explicitly instantiated class template. */
19792 instantiate_class_member (tree decl
, int extern_p
)
19794 mark_decl_instantiated (decl
, extern_p
);
19796 instantiate_decl (decl
, /*defer_ok=*/1,
19797 /*expl_inst_class_mem_p=*/true);
19800 /* Perform an explicit instantiation of template class T. STORAGE, if
19801 non-null, is the RID for extern, inline or static. COMPLAIN is
19802 nonzero if this is called from the parser, zero if called recursively,
19803 since the standard is unclear (as detailed below). */
19806 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
19811 int previous_instantiation_extern_p
= 0;
19813 if (TREE_CODE (t
) == TYPE_DECL
)
19816 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
19819 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
19821 error ("explicit instantiation of non-class template %qD", tmpl
);
19823 error ("explicit instantiation of non-template type %qT", t
);
19829 if (!COMPLETE_TYPE_P (t
))
19831 if (complain
& tf_error
)
19832 error ("explicit instantiation of %q#T before definition of template",
19837 if (storage
!= NULL_TREE
)
19839 if (!in_system_header_at (input_location
))
19841 if (storage
== ridpointers
[(int) RID_EXTERN
])
19843 if (cxx_dialect
== cxx98
)
19844 pedwarn (input_location
, OPT_Wpedantic
,
19845 "ISO C++ 1998 forbids the use of %<extern%> on "
19846 "explicit instantiations");
19849 pedwarn (input_location
, OPT_Wpedantic
,
19850 "ISO C++ forbids the use of %qE"
19851 " on explicit instantiations", storage
);
19854 if (storage
== ridpointers
[(int) RID_INLINE
])
19856 else if (storage
== ridpointers
[(int) RID_EXTERN
])
19858 else if (storage
== ridpointers
[(int) RID_STATIC
])
19862 error ("storage class %qD applied to template instantiation",
19868 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
19870 /* DR 259 [temp.spec].
19872 Both an explicit instantiation and a declaration of an explicit
19873 specialization shall not appear in a program unless the explicit
19874 instantiation follows a declaration of the explicit specialization.
19876 For a given set of template parameters, if an explicit
19877 instantiation of a template appears after a declaration of an
19878 explicit specialization for that template, the explicit
19879 instantiation has no effect. */
19882 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
19886 No program shall explicitly instantiate any template more
19889 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
19890 instantiation was `extern'. If EXTERN_P then the second is.
19891 These cases are OK. */
19892 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
19894 if (!previous_instantiation_extern_p
&& !extern_p
19895 && (complain
& tf_error
))
19896 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
19898 /* If we've already instantiated the template, just return now. */
19899 if (!CLASSTYPE_INTERFACE_ONLY (t
))
19903 check_explicit_instantiation_namespace (TYPE_NAME (t
));
19904 mark_class_instantiated (t
, extern_p
);
19912 /* In contrast to implicit instantiation, where only the
19913 declarations, and not the definitions, of members are
19914 instantiated, we have here:
19918 The explicit instantiation of a class template specialization
19919 implies the instantiation of all of its members not
19920 previously explicitly specialized in the translation unit
19921 containing the explicit instantiation.
19923 Of course, we can't instantiate member template classes, since
19924 we don't have any arguments for them. Note that the standard
19925 is unclear on whether the instantiation of the members are
19926 *explicit* instantiations or not. However, the most natural
19927 interpretation is that it should be an explicit instantiation. */
19930 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
19931 if (TREE_CODE (tmp
) == FUNCTION_DECL
19932 && DECL_TEMPLATE_INSTANTIATION (tmp
))
19933 instantiate_class_member (tmp
, extern_p
);
19935 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
19936 if (VAR_P (tmp
) && DECL_TEMPLATE_INSTANTIATION (tmp
))
19937 instantiate_class_member (tmp
, extern_p
);
19939 if (CLASSTYPE_NESTED_UTDS (t
))
19940 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
19941 bt_instantiate_type_proc
, &storage
);
19945 /* Given a function DECL, which is a specialization of TMPL, modify
19946 DECL to be a re-instantiation of TMPL with the same template
19947 arguments. TMPL should be the template into which tsubst'ing
19948 should occur for DECL, not the most general template.
19950 One reason for doing this is a scenario like this:
19953 void f(const T&, int i);
19955 void g() { f(3, 7); }
19958 void f(const T& t, const int i) { }
19960 Note that when the template is first instantiated, with
19961 instantiate_template, the resulting DECL will have no name for the
19962 first parameter, and the wrong type for the second. So, when we go
19963 to instantiate the DECL, we regenerate it. */
19966 regenerate_decl_from_template (tree decl
, tree tmpl
)
19968 /* The arguments used to instantiate DECL, from the most general
19973 args
= DECL_TI_ARGS (decl
);
19974 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
19976 /* Make sure that we can see identifiers, and compute access
19978 push_access_scope (decl
);
19980 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19988 args_depth
= TMPL_ARGS_DEPTH (args
);
19989 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
19990 if (args_depth
> parms_depth
)
19991 args
= get_innermost_template_args (args
, parms_depth
);
19993 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
19994 args
, tf_error
, NULL_TREE
,
19995 /*defer_ok*/false);
19996 if (specs
&& specs
!= error_mark_node
)
19997 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
20000 /* Merge parameter declarations. */
20001 decl_parm
= skip_artificial_parms_for (decl
,
20002 DECL_ARGUMENTS (decl
));
20004 = skip_artificial_parms_for (code_pattern
,
20005 DECL_ARGUMENTS (code_pattern
));
20006 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
20011 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
20012 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
20013 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
20015 parm_type
= type_decays_to (parm_type
);
20016 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
20017 TREE_TYPE (decl_parm
) = parm_type
;
20018 attributes
= DECL_ATTRIBUTES (pattern_parm
);
20019 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
20021 DECL_ATTRIBUTES (decl_parm
) = attributes
;
20022 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
20024 decl_parm
= DECL_CHAIN (decl_parm
);
20025 pattern_parm
= DECL_CHAIN (pattern_parm
);
20027 /* Merge any parameters that match with the function parameter
20029 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
20032 tree expanded_types
;
20033 /* Expand the TYPE_PACK_EXPANSION that provides the types for
20034 the parameters in this function parameter pack. */
20035 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
20036 args
, tf_error
, NULL_TREE
);
20037 len
= TREE_VEC_LENGTH (expanded_types
);
20038 for (i
= 0; i
< len
; i
++)
20043 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
20044 /* Rename the parameter to include the index. */
20045 DECL_NAME (decl_parm
) =
20046 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
20047 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
20048 parm_type
= type_decays_to (parm_type
);
20049 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
20050 TREE_TYPE (decl_parm
) = parm_type
;
20051 attributes
= DECL_ATTRIBUTES (pattern_parm
);
20052 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
20054 DECL_ATTRIBUTES (decl_parm
) = attributes
;
20055 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
20057 decl_parm
= DECL_CHAIN (decl_parm
);
20060 /* Merge additional specifiers from the CODE_PATTERN. */
20061 if (DECL_DECLARED_INLINE_P (code_pattern
)
20062 && !DECL_DECLARED_INLINE_P (decl
))
20063 DECL_DECLARED_INLINE_P (decl
) = 1;
20065 else if (VAR_P (decl
))
20067 DECL_INITIAL (decl
) =
20068 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
20069 tf_error
, DECL_TI_TEMPLATE (decl
),
20070 /*integral_constant_expression_p=*/false);
20071 if (VAR_HAD_UNKNOWN_BOUND (decl
))
20072 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
20073 tf_error
, DECL_TI_TEMPLATE (decl
));
20076 gcc_unreachable ();
20078 pop_access_scope (decl
);
20081 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
20082 substituted to get DECL. */
20085 template_for_substitution (tree decl
)
20087 tree tmpl
= DECL_TI_TEMPLATE (decl
);
20089 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
20090 for the instantiation. This is not always the most general
20091 template. Consider, for example:
20094 struct S { template <class U> void f();
20095 template <> void f<int>(); };
20097 and an instantiation of S<double>::f<int>. We want TD to be the
20098 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
20099 while (/* An instantiation cannot have a definition, so we need a
20100 more general template. */
20101 DECL_TEMPLATE_INSTANTIATION (tmpl
)
20102 /* We must also deal with friend templates. Given:
20104 template <class T> struct S {
20105 template <class U> friend void f() {};
20108 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
20109 so far as the language is concerned, but that's still
20110 where we get the pattern for the instantiation from. On
20111 other hand, if the definition comes outside the class, say:
20113 template <class T> struct S {
20114 template <class U> friend void f();
20116 template <class U> friend void f() {}
20118 we don't need to look any further. That's what the check for
20119 DECL_INITIAL is for. */
20120 || (TREE_CODE (decl
) == FUNCTION_DECL
20121 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
20122 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
20124 /* The present template, TD, should not be a definition. If it
20125 were a definition, we should be using it! Note that we
20126 cannot restructure the loop to just keep going until we find
20127 a template with a definition, since that might go too far if
20128 a specialization was declared, but not defined. */
20130 /* Fetch the more general template. */
20131 tmpl
= DECL_TI_TEMPLATE (tmpl
);
20137 /* Returns true if we need to instantiate this template instance even if we
20138 know we aren't going to emit it.. */
20141 always_instantiate_p (tree decl
)
20143 /* We always instantiate inline functions so that we can inline them. An
20144 explicit instantiation declaration prohibits implicit instantiation of
20145 non-inline functions. With high levels of optimization, we would
20146 normally inline non-inline functions -- but we're not allowed to do
20147 that for "extern template" functions. Therefore, we check
20148 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
20149 return ((TREE_CODE (decl
) == FUNCTION_DECL
20150 && (DECL_DECLARED_INLINE_P (decl
)
20151 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
20152 /* And we need to instantiate static data members so that
20153 their initializers are available in integral constant
20156 && decl_maybe_constant_var_p (decl
)));
20159 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
20160 instantiate it now, modifying TREE_TYPE (fn). */
20163 maybe_instantiate_noexcept (tree fn
)
20165 tree fntype
, spec
, noex
, clone
;
20167 /* Don't instantiate a noexcept-specification from template context. */
20168 if (processing_template_decl
)
20171 if (DECL_CLONED_FUNCTION_P (fn
))
20172 fn
= DECL_CLONED_FUNCTION (fn
);
20173 fntype
= TREE_TYPE (fn
);
20174 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
20176 if (!spec
|| !TREE_PURPOSE (spec
))
20179 noex
= TREE_PURPOSE (spec
);
20181 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
20183 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
20184 spec
= get_defaulted_eh_spec (fn
);
20185 else if (push_tinst_level (fn
))
20187 push_access_scope (fn
);
20188 push_deferring_access_checks (dk_no_deferred
);
20189 input_location
= DECL_SOURCE_LOCATION (fn
);
20190 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
20191 DEFERRED_NOEXCEPT_ARGS (noex
),
20192 tf_warning_or_error
, fn
,
20193 /*function_p=*/false,
20194 /*integral_constant_expression_p=*/true);
20195 pop_deferring_access_checks ();
20196 pop_access_scope (fn
);
20197 pop_tinst_level ();
20198 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
20199 if (spec
== error_mark_node
)
20200 spec
= noexcept_false_spec
;
20203 spec
= noexcept_false_spec
;
20205 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
20208 FOR_EACH_CLONE (clone
, fn
)
20210 if (TREE_TYPE (clone
) == fntype
)
20211 TREE_TYPE (clone
) = TREE_TYPE (fn
);
20213 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
20217 /* Produce the definition of D, a _DECL generated from a template. If
20218 DEFER_OK is nonzero, then we don't have to actually do the
20219 instantiation now; we just have to do it sometime. Normally it is
20220 an error if this is an explicit instantiation but D is undefined.
20221 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
20222 explicitly instantiated class template. */
20225 instantiate_decl (tree d
, int defer_ok
,
20226 bool expl_inst_class_mem_p
)
20228 tree tmpl
= DECL_TI_TEMPLATE (d
);
20235 bool pattern_defined
;
20236 location_t saved_loc
= input_location
;
20237 int saved_unevaluated_operand
= cp_unevaluated_operand
;
20238 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
20244 /* This function should only be used to instantiate templates for
20245 functions and static member variables. */
20246 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
20248 /* Variables are never deferred; if instantiation is required, they
20249 are instantiated right away. That allows for better code in the
20250 case that an expression refers to the value of the variable --
20251 if the variable has a constant value the referring expression can
20252 take advantage of that fact. */
20254 || DECL_DECLARED_CONSTEXPR_P (d
))
20257 /* Don't instantiate cloned functions. Instead, instantiate the
20258 functions they cloned. */
20259 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
20260 d
= DECL_CLONED_FUNCTION (d
);
20262 if (DECL_TEMPLATE_INSTANTIATED (d
)
20263 || (TREE_CODE (d
) == FUNCTION_DECL
20264 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
20265 || DECL_TEMPLATE_SPECIALIZATION (d
))
20266 /* D has already been instantiated or explicitly specialized, so
20267 there's nothing for us to do here.
20269 It might seem reasonable to check whether or not D is an explicit
20270 instantiation, and, if so, stop here. But when an explicit
20271 instantiation is deferred until the end of the compilation,
20272 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
20273 the instantiation. */
20276 /* Check to see whether we know that this template will be
20277 instantiated in some other file, as with "extern template"
20279 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
20281 /* In general, we do not instantiate such templates. */
20282 if (external_p
&& !always_instantiate_p (d
))
20285 gen_tmpl
= most_general_template (tmpl
);
20286 gen_args
= DECL_TI_ARGS (d
);
20288 if (tmpl
!= gen_tmpl
)
20289 /* We should already have the extra args. */
20290 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
20291 == TMPL_ARGS_DEPTH (gen_args
));
20292 /* And what's in the hash table should match D. */
20293 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
20294 || spec
== NULL_TREE
);
20296 /* This needs to happen before any tsubsting. */
20297 if (! push_tinst_level (d
))
20300 timevar_push (TV_TEMPLATE_INST
);
20302 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
20303 for the instantiation. */
20304 td
= template_for_substitution (d
);
20305 code_pattern
= DECL_TEMPLATE_RESULT (td
);
20307 /* We should never be trying to instantiate a member of a class
20308 template or partial specialization. */
20309 gcc_assert (d
!= code_pattern
);
20311 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
20312 || DECL_TEMPLATE_SPECIALIZATION (td
))
20313 /* In the case of a friend template whose definition is provided
20314 outside the class, we may have too many arguments. Drop the
20315 ones we don't need. The same is true for specializations. */
20316 args
= get_innermost_template_args
20317 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
20321 if (TREE_CODE (d
) == FUNCTION_DECL
)
20323 deleted_p
= DECL_DELETED_FN (code_pattern
);
20324 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
20325 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
)
20331 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
20334 /* We may be in the middle of deferred access check. Disable it now. */
20335 push_deferring_access_checks (dk_no_deferred
);
20337 /* Unless an explicit instantiation directive has already determined
20338 the linkage of D, remember that a definition is available for
20340 if (pattern_defined
20341 && !DECL_INTERFACE_KNOWN (d
)
20342 && !DECL_NOT_REALLY_EXTERN (d
))
20343 mark_definable (d
);
20345 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
20346 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
20347 input_location
= DECL_SOURCE_LOCATION (d
);
20349 /* If D is a member of an explicitly instantiated class template,
20350 and no definition is available, treat it like an implicit
20352 if (!pattern_defined
&& expl_inst_class_mem_p
20353 && DECL_EXPLICIT_INSTANTIATION (d
))
20355 /* Leave linkage flags alone on instantiations with anonymous
20357 if (TREE_PUBLIC (d
))
20359 DECL_NOT_REALLY_EXTERN (d
) = 0;
20360 DECL_INTERFACE_KNOWN (d
) = 0;
20362 SET_DECL_IMPLICIT_INSTANTIATION (d
);
20365 /* Defer all other templates, unless we have been explicitly
20366 forbidden from doing so. */
20367 if (/* If there is no definition, we cannot instantiate the
20370 /* If it's OK to postpone instantiation, do so. */
20372 /* If this is a static data member that will be defined
20373 elsewhere, we don't want to instantiate the entire data
20374 member, but we do want to instantiate the initializer so that
20375 we can substitute that elsewhere. */
20376 || (external_p
&& VAR_P (d
))
20377 /* Handle here a deleted function too, avoid generating
20378 its body (c++/61080). */
20381 /* The definition of the static data member is now required so
20382 we must substitute the initializer. */
20384 && !DECL_INITIAL (d
)
20385 && DECL_INITIAL (code_pattern
))
20389 bool const_init
= false;
20390 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
20392 ns
= decl_namespace_context (d
);
20393 push_nested_namespace (ns
);
20395 push_nested_class (DECL_CONTEXT (d
));
20396 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
20398 tf_warning_or_error
, NULL_TREE
,
20399 /*integral_constant_expression_p=*/false);
20400 /* If instantiating the initializer involved instantiating this
20401 again, don't call cp_finish_decl twice. */
20402 if (!DECL_INITIAL (d
))
20404 /* Make sure the initializer is still constant, in case of
20405 circular dependency (template/instantiate6.C). */
20407 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
20408 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
20409 /*asmspec_tree=*/NULL_TREE
,
20410 LOOKUP_ONLYCONVERTING
);
20413 pop_nested_class ();
20414 pop_nested_namespace (ns
);
20417 /* We restore the source position here because it's used by
20418 add_pending_template. */
20419 input_location
= saved_loc
;
20421 if (at_eof
&& !pattern_defined
20422 && DECL_EXPLICIT_INSTANTIATION (d
)
20423 && DECL_NOT_REALLY_EXTERN (d
))
20426 The definition of a non-exported function template, a
20427 non-exported member function template, or a non-exported
20428 member function or static data member of a class template
20429 shall be present in every translation unit in which it is
20430 explicitly instantiated. */
20431 permerror (input_location
, "explicit instantiation of %qD "
20432 "but no definition available", d
);
20434 /* If we're in unevaluated context, we just wanted to get the
20435 constant value; this isn't an odr use, so don't queue
20436 a full instantiation. */
20437 if (cp_unevaluated_operand
!= 0)
20439 /* ??? Historically, we have instantiated inline functions, even
20440 when marked as "extern template". */
20441 if (!(external_p
&& VAR_P (d
)))
20442 add_pending_template (d
);
20445 /* Tell the repository that D is available in this translation unit
20446 -- and see if it is supposed to be instantiated here. */
20447 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
20449 /* In a PCH file, despite the fact that the repository hasn't
20450 requested instantiation in the PCH it is still possible that
20451 an instantiation will be required in a file that includes the
20454 add_pending_template (d
);
20455 /* Instantiate inline functions so that the inliner can do its
20456 job, even though we'll not be emitting a copy of this
20458 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
20462 fn_context
= decl_function_context (d
);
20463 nested
= (current_function_decl
!= NULL_TREE
);
20465 push_to_top_level ();
20469 push_function_context ();
20470 cp_unevaluated_operand
= 0;
20471 c_inhibit_evaluation_warnings
= 0;
20474 /* Mark D as instantiated so that recursive calls to
20475 instantiate_decl do not try to instantiate it again. */
20476 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
20478 /* Regenerate the declaration in case the template has been modified
20479 by a subsequent redeclaration. */
20480 regenerate_decl_from_template (d
, td
);
20482 /* We already set the file and line above. Reset them now in case
20483 they changed as a result of calling regenerate_decl_from_template. */
20484 input_location
= DECL_SOURCE_LOCATION (d
);
20489 bool const_init
= false;
20491 /* Clear out DECL_RTL; whatever was there before may not be right
20492 since we've reset the type of the declaration. */
20493 SET_DECL_RTL (d
, NULL
);
20494 DECL_IN_AGGR_P (d
) = 0;
20496 /* The initializer is placed in DECL_INITIAL by
20497 regenerate_decl_from_template so we don't need to
20498 push/pop_access_scope again here. Pull it out so that
20499 cp_finish_decl can process it. */
20500 init
= DECL_INITIAL (d
);
20501 DECL_INITIAL (d
) = NULL_TREE
;
20502 DECL_INITIALIZED_P (d
) = 0;
20504 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
20505 initializer. That function will defer actual emission until
20506 we have a chance to determine linkage. */
20507 DECL_EXTERNAL (d
) = 0;
20509 /* Enter the scope of D so that access-checking works correctly. */
20510 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
20512 push_nested_class (DECL_CONTEXT (d
));
20514 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
20515 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
20518 pop_nested_class ();
20520 if (variable_template_p (td
))
20521 note_variable_template_instantiation (d
);
20523 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
20524 synthesize_method (d
);
20525 else if (TREE_CODE (d
) == FUNCTION_DECL
)
20527 hash_map
<tree
, tree
> *saved_local_specializations
;
20531 tree block
= NULL_TREE
;
20533 /* Save away the current list, in case we are instantiating one
20534 template from within the body of another. */
20535 saved_local_specializations
= local_specializations
;
20537 /* Set up the list of local specializations. */
20538 local_specializations
= new hash_map
<tree
, tree
>;
20540 /* Set up context. */
20541 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
20542 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
20543 block
= push_stmt_list ();
20545 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
20547 /* Some typedefs referenced from within the template code need to be
20548 access checked at template instantiation time, i.e now. These
20549 types were added to the template at parsing time. Let's get those
20550 and perform the access checks then. */
20551 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl
),
20554 /* Create substitution entries for the parameters. */
20555 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
20556 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
20557 spec_parm
= DECL_ARGUMENTS (d
);
20558 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
20560 register_local_specialization (spec_parm
, tmpl_parm
);
20561 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
20562 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
20564 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
20566 if (!DECL_PACK_P (tmpl_parm
))
20568 register_local_specialization (spec_parm
, tmpl_parm
);
20569 spec_parm
= DECL_CHAIN (spec_parm
);
20573 /* Register the (value) argument pack as a specialization of
20574 TMPL_PARM, then move on. */
20575 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
20576 register_local_specialization (argpack
, tmpl_parm
);
20579 gcc_assert (!spec_parm
);
20581 /* Substitute into the body of the function. */
20582 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
20583 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
20584 tf_warning_or_error
, tmpl
);
20587 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
20588 tf_warning_or_error
, tmpl
,
20589 /*integral_constant_expression_p=*/false);
20591 /* Set the current input_location to the end of the function
20592 so that finish_function knows where we are. */
20594 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
20596 /* Remember if we saw an infinite loop in the template. */
20597 current_function_infinite_loop
20598 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
20601 /* We don't need the local specializations any more. */
20602 delete local_specializations
;
20603 local_specializations
= saved_local_specializations
;
20605 /* Finish the function. */
20606 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
20607 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
20608 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
20611 d
= finish_function (0);
20612 expand_or_defer_fn (d
);
20615 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
20616 cp_check_omp_declare_reduction (d
);
20619 /* We're not deferring instantiation any more. */
20620 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
20623 pop_from_top_level ();
20625 pop_function_context ();
20628 input_location
= saved_loc
;
20629 cp_unevaluated_operand
= saved_unevaluated_operand
;
20630 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
20631 pop_deferring_access_checks ();
20632 pop_tinst_level ();
20634 timevar_pop (TV_TEMPLATE_INST
);
20639 /* Run through the list of templates that we wish we could
20640 instantiate, and instantiate any we can. RETRIES is the
20641 number of times we retry pending template instantiation. */
20644 instantiate_pending_templates (int retries
)
20647 location_t saved_loc
= input_location
;
20649 /* Instantiating templates may trigger vtable generation. This in turn
20650 may require further template instantiations. We place a limit here
20651 to avoid infinite loop. */
20652 if (pending_templates
&& retries
>= max_tinst_depth
)
20654 tree decl
= pending_templates
->tinst
->decl
;
20656 fatal_error (input_location
,
20657 "template instantiation depth exceeds maximum of %d"
20658 " instantiating %q+D, possibly from virtual table generation"
20659 " (use -ftemplate-depth= to increase the maximum)",
20660 max_tinst_depth
, decl
);
20661 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20662 /* Pretend that we defined it. */
20663 DECL_INITIAL (decl
) = error_mark_node
;
20669 struct pending_template
**t
= &pending_templates
;
20670 struct pending_template
*last
= NULL
;
20674 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
20675 bool complete
= false;
20677 if (TYPE_P (instantiation
))
20681 if (!COMPLETE_TYPE_P (instantiation
))
20683 instantiate_class_template (instantiation
);
20684 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
20685 for (fn
= TYPE_METHODS (instantiation
);
20687 fn
= TREE_CHAIN (fn
))
20688 if (! DECL_ARTIFICIAL (fn
))
20689 instantiate_decl (fn
,
20691 /*expl_inst_class_mem_p=*/false);
20692 if (COMPLETE_TYPE_P (instantiation
))
20696 complete
= COMPLETE_TYPE_P (instantiation
);
20700 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
20701 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
20704 = instantiate_decl (instantiation
,
20706 /*expl_inst_class_mem_p=*/false);
20707 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
20711 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
20712 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
20716 /* If INSTANTIATION has been instantiated, then we don't
20717 need to consider it again in the future. */
20725 current_tinst_level
= NULL
;
20727 last_pending_template
= last
;
20729 while (reconsider
);
20731 input_location
= saved_loc
;
20734 /* Substitute ARGVEC into T, which is a list of initializers for
20735 either base class or a non-static data member. The TREE_PURPOSEs
20736 are DECLs, and the TREE_VALUEs are the initializer values. Used by
20737 instantiate_decl. */
20740 tsubst_initializer_list (tree t
, tree argvec
)
20742 tree inits
= NULL_TREE
;
20744 for (; t
; t
= TREE_CHAIN (t
))
20748 tree expanded_bases
= NULL_TREE
;
20749 tree expanded_arguments
= NULL_TREE
;
20752 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
20757 /* Expand the base class expansion type into separate base
20759 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
20760 tf_warning_or_error
,
20762 if (expanded_bases
== error_mark_node
)
20765 /* We'll be building separate TREE_LISTs of arguments for
20767 len
= TREE_VEC_LENGTH (expanded_bases
);
20768 expanded_arguments
= make_tree_vec (len
);
20769 for (i
= 0; i
< len
; i
++)
20770 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
20772 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
20773 expand each argument in the TREE_VALUE of t. */
20774 expr
= make_node (EXPR_PACK_EXPANSION
);
20775 PACK_EXPANSION_LOCAL_P (expr
) = true;
20776 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
20777 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
20779 if (TREE_VALUE (t
) == void_type_node
)
20780 /* VOID_TYPE_NODE is used to indicate
20781 value-initialization. */
20783 for (i
= 0; i
< len
; i
++)
20784 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
20788 /* Substitute parameter packs into each argument in the
20790 in_base_initializer
= 1;
20791 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
20793 tree expanded_exprs
;
20795 /* Expand the argument. */
20796 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
20798 = tsubst_pack_expansion (expr
, argvec
,
20799 tf_warning_or_error
,
20801 if (expanded_exprs
== error_mark_node
)
20804 /* Prepend each of the expanded expressions to the
20805 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
20806 for (i
= 0; i
< len
; i
++)
20808 TREE_VEC_ELT (expanded_arguments
, i
) =
20809 tree_cons (NULL_TREE
,
20810 TREE_VEC_ELT (expanded_exprs
, i
),
20811 TREE_VEC_ELT (expanded_arguments
, i
));
20814 in_base_initializer
= 0;
20816 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
20817 since we built them backwards. */
20818 for (i
= 0; i
< len
; i
++)
20820 TREE_VEC_ELT (expanded_arguments
, i
) =
20821 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
20826 for (i
= 0; i
< len
; ++i
)
20828 if (expanded_bases
)
20830 decl
= TREE_VEC_ELT (expanded_bases
, i
);
20831 decl
= expand_member_init (decl
);
20832 init
= TREE_VEC_ELT (expanded_arguments
, i
);
20837 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
20838 tf_warning_or_error
, NULL_TREE
);
20840 decl
= expand_member_init (decl
);
20841 if (decl
&& !DECL_P (decl
))
20842 in_base_initializer
= 1;
20844 init
= TREE_VALUE (t
);
20846 if (init
!= void_type_node
)
20847 init
= tsubst_expr (init
, argvec
,
20848 tf_warning_or_error
, NULL_TREE
,
20849 /*integral_constant_expression_p=*/false);
20850 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
20851 /* If we had an initializer but it instantiated to nothing,
20852 value-initialize the object. This will only occur when
20853 the initializer was a pack expansion where the parameter
20854 packs used in that expansion were of length zero. */
20855 init
= void_type_node
;
20856 in_base_initializer
= 0;
20861 init
= build_tree_list (decl
, init
);
20862 TREE_CHAIN (init
) = inits
;
20870 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
20873 set_current_access_from_decl (tree decl
)
20875 if (TREE_PRIVATE (decl
))
20876 current_access_specifier
= access_private_node
;
20877 else if (TREE_PROTECTED (decl
))
20878 current_access_specifier
= access_protected_node
;
20880 current_access_specifier
= access_public_node
;
20883 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
20884 is the instantiation (which should have been created with
20885 start_enum) and ARGS are the template arguments to use. */
20888 tsubst_enum (tree tag
, tree newtag
, tree args
)
20892 if (SCOPED_ENUM_P (newtag
))
20893 begin_scope (sk_scoped_enum
, newtag
);
20895 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
20900 decl
= TREE_VALUE (e
);
20901 /* Note that in a template enum, the TREE_VALUE is the
20902 CONST_DECL, not the corresponding INTEGER_CST. */
20903 value
= tsubst_expr (DECL_INITIAL (decl
),
20904 args
, tf_warning_or_error
, NULL_TREE
,
20905 /*integral_constant_expression_p=*/true);
20907 /* Give this enumeration constant the correct access. */
20908 set_current_access_from_decl (decl
);
20910 /* Actually build the enumerator itself. */
20912 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
20915 if (SCOPED_ENUM_P (newtag
))
20918 finish_enum_value_list (newtag
);
20919 finish_enum (newtag
);
20921 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
20922 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
20925 /* DECL is a FUNCTION_DECL that is a template specialization. Return
20926 its type -- but without substituting the innermost set of template
20927 arguments. So, innermost set of template parameters will appear in
20931 get_mostly_instantiated_function_type (tree decl
)
20933 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
20934 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
20937 /* Return truthvalue if we're processing a template different from
20938 the last one involved in diagnostics. */
20940 problematic_instantiation_changed (void)
20942 return current_tinst_level
!= last_error_tinst_level
;
20945 /* Remember current template involved in diagnostics. */
20947 record_last_problematic_instantiation (void)
20949 last_error_tinst_level
= current_tinst_level
;
20952 struct tinst_level
*
20953 current_instantiation (void)
20955 return current_tinst_level
;
20958 /* Return TRUE if current_function_decl is being instantiated, false
20962 instantiating_current_function_p (void)
20964 return (current_instantiation ()
20965 && current_instantiation ()->decl
== current_function_decl
);
20968 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20969 type. Return zero for ok, nonzero for disallowed. Issue error and
20970 warning messages under control of COMPLAIN. */
20973 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
20975 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
20977 else if (POINTER_TYPE_P (type
))
20979 else if (TYPE_PTRMEM_P (type
))
20981 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
20983 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
20985 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
20987 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
20990 if (complain
& tf_error
)
20992 if (type
== error_mark_node
)
20993 inform (input_location
, "invalid template non-type parameter");
20995 error ("%q#T is not a valid type for a template non-type parameter",
21001 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
21002 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
21005 dependent_type_p_r (tree type
)
21011 A type is dependent if it is:
21013 -- a template parameter. Template template parameters are types
21014 for us (since TYPE_P holds true for them) so we handle
21016 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21017 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
21019 /* -- a qualified-id with a nested-name-specifier which contains a
21020 class-name that names a dependent type or whose unqualified-id
21021 names a dependent type. */
21022 if (TREE_CODE (type
) == TYPENAME_TYPE
)
21025 /* An alias template specialization can be dependent even if the
21026 resulting type is not. */
21027 if (dependent_alias_template_spec_p (type
))
21030 /* -- a cv-qualified type where the cv-unqualified type is
21032 No code is necessary for this bullet; the code below handles
21033 cv-qualified types, and we don't want to strip aliases with
21034 TYPE_MAIN_VARIANT because of DR 1558. */
21035 /* -- a compound type constructed from any dependent type. */
21036 if (TYPE_PTRMEM_P (type
))
21037 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
21038 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
21040 else if (TYPE_PTR_P (type
)
21041 || TREE_CODE (type
) == REFERENCE_TYPE
)
21042 return dependent_type_p (TREE_TYPE (type
));
21043 else if (TREE_CODE (type
) == FUNCTION_TYPE
21044 || TREE_CODE (type
) == METHOD_TYPE
)
21048 if (dependent_type_p (TREE_TYPE (type
)))
21050 for (arg_type
= TYPE_ARG_TYPES (type
);
21052 arg_type
= TREE_CHAIN (arg_type
))
21053 if (dependent_type_p (TREE_VALUE (arg_type
)))
21057 /* -- an array type constructed from any dependent type or whose
21058 size is specified by a constant expression that is
21061 We checked for type- and value-dependence of the bounds in
21062 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
21063 if (TREE_CODE (type
) == ARRAY_TYPE
)
21065 if (TYPE_DOMAIN (type
)
21066 && dependent_type_p (TYPE_DOMAIN (type
)))
21068 return dependent_type_p (TREE_TYPE (type
));
21071 /* -- a template-id in which either the template name is a template
21073 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21075 /* ... or any of the template arguments is a dependent type or
21076 an expression that is type-dependent or value-dependent. */
21077 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
21078 && (any_dependent_template_arguments_p
21079 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
21082 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
21083 dependent; if the argument of the `typeof' expression is not
21084 type-dependent, then it should already been have resolved. */
21085 if (TREE_CODE (type
) == TYPEOF_TYPE
21086 || TREE_CODE (type
) == DECLTYPE_TYPE
21087 || TREE_CODE (type
) == UNDERLYING_TYPE
)
21090 /* A template argument pack is dependent if any of its packed
21092 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
21094 tree args
= ARGUMENT_PACK_ARGS (type
);
21095 int i
, len
= TREE_VEC_LENGTH (args
);
21096 for (i
= 0; i
< len
; ++i
)
21097 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
21101 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
21102 be template parameters. */
21103 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
21106 /* The standard does not specifically mention types that are local
21107 to template functions or local classes, but they should be
21108 considered dependent too. For example:
21110 template <int I> void f() {
21115 The size of `E' cannot be known until the value of `I' has been
21116 determined. Therefore, `E' must be considered dependent. */
21117 scope
= TYPE_CONTEXT (type
);
21118 if (scope
&& TYPE_P (scope
))
21119 return dependent_type_p (scope
);
21120 /* Don't use type_dependent_expression_p here, as it can lead
21121 to infinite recursion trying to determine whether a lambda
21122 nested in a lambda is dependent (c++/47687). */
21123 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
21124 && DECL_LANG_SPECIFIC (scope
)
21125 && DECL_TEMPLATE_INFO (scope
)
21126 && (any_dependent_template_arguments_p
21127 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
21130 /* Other types are non-dependent. */
21134 /* Returns TRUE if TYPE is dependent, in the sense of
21135 [temp.dep.type]. Note that a NULL type is considered dependent. */
21138 dependent_type_p (tree type
)
21140 /* If there are no template parameters in scope, then there can't be
21141 any dependent types. */
21142 if (!processing_template_decl
)
21144 /* If we are not processing a template, then nobody should be
21145 providing us with a dependent type. */
21147 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
21151 /* If the type is NULL, we have not computed a type for the entity
21152 in question; in that case, the type is dependent. */
21156 /* Erroneous types can be considered non-dependent. */
21157 if (type
== error_mark_node
)
21160 /* If we have not already computed the appropriate value for TYPE,
21162 if (!TYPE_DEPENDENT_P_VALID (type
))
21164 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
21165 TYPE_DEPENDENT_P_VALID (type
) = 1;
21168 return TYPE_DEPENDENT_P (type
);
21171 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
21172 lookup. In other words, a dependent type that is not the current
21176 dependent_scope_p (tree scope
)
21178 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
21179 && !currently_open_class (scope
));
21182 /* T is a SCOPE_REF; return whether we need to consider it
21183 instantiation-dependent so that we can check access at instantiation
21184 time even though we know which member it resolves to. */
21187 instantiation_dependent_scope_ref_p (tree t
)
21189 if (DECL_P (TREE_OPERAND (t
, 1))
21190 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
21191 && accessible_in_template_p (TREE_OPERAND (t
, 0),
21192 TREE_OPERAND (t
, 1)))
21198 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
21199 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
21202 /* Note that this predicate is not appropriate for general expressions;
21203 only constant expressions (that satisfy potential_constant_expression)
21204 can be tested for value dependence. */
21207 value_dependent_expression_p (tree expression
)
21209 if (!processing_template_decl
)
21212 /* A name declared with a dependent type. */
21213 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
21216 switch (TREE_CODE (expression
))
21218 case IDENTIFIER_NODE
:
21219 /* A name that has not been looked up -- must be dependent. */
21222 case TEMPLATE_PARM_INDEX
:
21223 /* A non-type template parm. */
21227 /* A non-type template parm. */
21228 if (DECL_TEMPLATE_PARM_P (expression
))
21230 return value_dependent_expression_p (DECL_INITIAL (expression
));
21233 /* A constant with literal type and is initialized
21234 with an expression that is value-dependent.
21236 Note that a non-dependent parenthesized initializer will have
21237 already been replaced with its constant value, so if we see
21238 a TREE_LIST it must be dependent. */
21239 if (DECL_INITIAL (expression
)
21240 && decl_constant_var_p (expression
)
21241 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
21242 /* cp_finish_decl doesn't fold reference initializers. */
21243 || TREE_CODE (TREE_TYPE (expression
)) == REFERENCE_TYPE
21244 || value_dependent_expression_p (DECL_INITIAL (expression
))))
21248 case DYNAMIC_CAST_EXPR
:
21249 case STATIC_CAST_EXPR
:
21250 case CONST_CAST_EXPR
:
21251 case REINTERPRET_CAST_EXPR
:
21253 /* These expressions are value-dependent if the type to which
21254 the cast occurs is dependent or the expression being casted
21255 is value-dependent. */
21257 tree type
= TREE_TYPE (expression
);
21259 if (dependent_type_p (type
))
21262 /* A functional cast has a list of operands. */
21263 expression
= TREE_OPERAND (expression
, 0);
21266 /* If there are no operands, it must be an expression such
21267 as "int()". This should not happen for aggregate types
21268 because it would form non-constant expressions. */
21269 gcc_assert (cxx_dialect
>= cxx11
21270 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
21275 if (TREE_CODE (expression
) == TREE_LIST
)
21276 return any_value_dependent_elements_p (expression
);
21278 return value_dependent_expression_p (expression
);
21282 if (SIZEOF_EXPR_TYPE_P (expression
))
21283 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
21287 /* A `sizeof' expression is value-dependent if the operand is
21288 type-dependent or is a pack expansion. */
21289 expression
= TREE_OPERAND (expression
, 0);
21290 if (PACK_EXPANSION_P (expression
))
21292 else if (TYPE_P (expression
))
21293 return dependent_type_p (expression
);
21294 return instantiation_dependent_expression_p (expression
);
21296 case AT_ENCODE_EXPR
:
21297 /* An 'encode' expression is value-dependent if the operand is
21299 expression
= TREE_OPERAND (expression
, 0);
21300 return dependent_type_p (expression
);
21302 case NOEXCEPT_EXPR
:
21303 expression
= TREE_OPERAND (expression
, 0);
21304 return instantiation_dependent_expression_p (expression
);
21307 /* All instantiation-dependent expressions should also be considered
21308 value-dependent. */
21309 return instantiation_dependent_scope_ref_p (expression
);
21311 case COMPONENT_REF
:
21312 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
21313 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
21315 case NONTYPE_ARGUMENT_PACK
:
21316 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
21317 is value-dependent. */
21319 tree values
= ARGUMENT_PACK_ARGS (expression
);
21320 int i
, len
= TREE_VEC_LENGTH (values
);
21322 for (i
= 0; i
< len
; ++i
)
21323 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
21331 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
21332 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
21333 || (type2
? dependent_type_p (type2
) : false));
21337 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
21338 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
21341 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
21342 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
21346 tree op
= TREE_OPERAND (expression
, 0);
21347 return (value_dependent_expression_p (op
)
21348 || has_value_dependent_address (op
));
21353 tree fn
= get_callee_fndecl (expression
);
21355 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
21357 nargs
= call_expr_nargs (expression
);
21358 for (i
= 0; i
< nargs
; ++i
)
21360 tree op
= CALL_EXPR_ARG (expression
, i
);
21361 /* In a call to a constexpr member function, look through the
21362 implicit ADDR_EXPR on the object argument so that it doesn't
21363 cause the call to be considered value-dependent. We also
21364 look through it in potential_constant_expression. */
21365 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
21366 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
21367 && TREE_CODE (op
) == ADDR_EXPR
)
21368 op
= TREE_OPERAND (op
, 0);
21369 if (value_dependent_expression_p (op
))
21375 case TEMPLATE_ID_EXPR
:
21376 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
21378 return type_dependent_expression_p (expression
);
21384 if (dependent_type_p (TREE_TYPE (expression
)))
21386 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
21387 if (value_dependent_expression_p (val
))
21393 /* Treat a GNU statement expression as dependent to avoid crashing
21394 under instantiate_non_dependent_expr; it can't be constant. */
21398 /* A constant expression is value-dependent if any subexpression is
21399 value-dependent. */
21400 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
21402 case tcc_reference
:
21404 case tcc_comparison
:
21406 case tcc_expression
:
21409 int i
, len
= cp_tree_operand_length (expression
);
21411 for (i
= 0; i
< len
; i
++)
21413 tree t
= TREE_OPERAND (expression
, i
);
21415 /* In some cases, some of the operands may be missing.l
21416 (For example, in the case of PREDECREMENT_EXPR, the
21417 amount to increment by may be missing.) That doesn't
21418 make the expression dependent. */
21419 if (t
&& value_dependent_expression_p (t
))
21430 /* The expression is not value-dependent. */
21434 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
21435 [temp.dep.expr]. Note that an expression with no type is
21436 considered dependent. Other parts of the compiler arrange for an
21437 expression with type-dependent subexpressions to have no type, so
21438 this function doesn't have to be fully recursive. */
21441 type_dependent_expression_p (tree expression
)
21443 if (!processing_template_decl
)
21446 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
21449 /* An unresolved name is always dependent. */
21450 if (identifier_p (expression
) || TREE_CODE (expression
) == USING_DECL
)
21453 /* Some expression forms are never type-dependent. */
21454 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
21455 || TREE_CODE (expression
) == SIZEOF_EXPR
21456 || TREE_CODE (expression
) == ALIGNOF_EXPR
21457 || TREE_CODE (expression
) == AT_ENCODE_EXPR
21458 || TREE_CODE (expression
) == NOEXCEPT_EXPR
21459 || TREE_CODE (expression
) == TRAIT_EXPR
21460 || TREE_CODE (expression
) == TYPEID_EXPR
21461 || TREE_CODE (expression
) == DELETE_EXPR
21462 || TREE_CODE (expression
) == VEC_DELETE_EXPR
21463 || TREE_CODE (expression
) == THROW_EXPR
)
21466 /* The types of these expressions depends only on the type to which
21467 the cast occurs. */
21468 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
21469 || TREE_CODE (expression
) == STATIC_CAST_EXPR
21470 || TREE_CODE (expression
) == CONST_CAST_EXPR
21471 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
21472 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
21473 || TREE_CODE (expression
) == CAST_EXPR
)
21474 return dependent_type_p (TREE_TYPE (expression
));
21476 /* The types of these expressions depends only on the type created
21477 by the expression. */
21478 if (TREE_CODE (expression
) == NEW_EXPR
21479 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
21481 /* For NEW_EXPR tree nodes created inside a template, either
21482 the object type itself or a TREE_LIST may appear as the
21484 tree type
= TREE_OPERAND (expression
, 1);
21485 if (TREE_CODE (type
) == TREE_LIST
)
21486 /* This is an array type. We need to check array dimensions
21488 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
21489 || value_dependent_expression_p
21490 (TREE_OPERAND (TREE_VALUE (type
), 1));
21492 return dependent_type_p (type
);
21495 if (TREE_CODE (expression
) == SCOPE_REF
)
21497 tree scope
= TREE_OPERAND (expression
, 0);
21498 tree name
= TREE_OPERAND (expression
, 1);
21500 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
21501 contains an identifier associated by name lookup with one or more
21502 declarations declared with a dependent type, or...a
21503 nested-name-specifier or qualified-id that names a member of an
21504 unknown specialization. */
21505 return (type_dependent_expression_p (name
)
21506 || dependent_scope_p (scope
));
21509 if (TREE_CODE (expression
) == FUNCTION_DECL
21510 && DECL_LANG_SPECIFIC (expression
)
21511 && DECL_TEMPLATE_INFO (expression
)
21512 && (any_dependent_template_arguments_p
21513 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
21516 if (TREE_CODE (expression
) == TEMPLATE_DECL
21517 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
21520 if (TREE_CODE (expression
) == STMT_EXPR
)
21521 expression
= stmt_expr_value_expr (expression
);
21523 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
21528 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
21530 if (type_dependent_expression_p (elt
))
21536 /* A static data member of the current instantiation with incomplete
21537 array type is type-dependent, as the definition and specializations
21538 can have different bounds. */
21539 if (VAR_P (expression
)
21540 && DECL_CLASS_SCOPE_P (expression
)
21541 && dependent_type_p (DECL_CONTEXT (expression
))
21542 && VAR_HAD_UNKNOWN_BOUND (expression
))
21545 /* An array of unknown bound depending on a variadic parameter, eg:
21547 template<typename... Args>
21548 void foo (Args... args)
21550 int arr[] = { args... };
21553 template<int... vals>
21556 int arr[] = { vals... };
21559 If the array has no length and has an initializer, it must be that
21560 we couldn't determine its length in cp_complete_array_type because
21561 it is dependent. */
21562 if (VAR_P (expression
)
21563 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
21564 && !TYPE_DOMAIN (TREE_TYPE (expression
))
21565 && DECL_INITIAL (expression
))
21568 /* A variable template specialization is type-dependent if it has any
21569 dependent template arguments. */
21570 if (VAR_P (expression
)
21571 && DECL_LANG_SPECIFIC (expression
)
21572 && DECL_TEMPLATE_INFO (expression
)
21573 && variable_template_p (DECL_TI_TEMPLATE (expression
)))
21574 return any_dependent_template_arguments_p (DECL_TI_ARGS (expression
));
21576 /* Always dependent, on the number of arguments if nothing else. */
21577 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
21580 if (TREE_TYPE (expression
) == unknown_type_node
)
21582 if (TREE_CODE (expression
) == ADDR_EXPR
)
21583 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
21584 if (TREE_CODE (expression
) == COMPONENT_REF
21585 || TREE_CODE (expression
) == OFFSET_REF
)
21587 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
21589 expression
= TREE_OPERAND (expression
, 1);
21590 if (identifier_p (expression
))
21593 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
21594 if (TREE_CODE (expression
) == SCOPE_REF
)
21597 if (BASELINK_P (expression
))
21599 if (BASELINK_OPTYPE (expression
)
21600 && dependent_type_p (BASELINK_OPTYPE (expression
)))
21602 expression
= BASELINK_FUNCTIONS (expression
);
21605 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
21607 if (any_dependent_template_arguments_p
21608 (TREE_OPERAND (expression
, 1)))
21610 expression
= TREE_OPERAND (expression
, 0);
21612 gcc_assert (TREE_CODE (expression
) == OVERLOAD
21613 || TREE_CODE (expression
) == FUNCTION_DECL
);
21617 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
21619 expression
= OVL_NEXT (expression
);
21624 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
21626 return (dependent_type_p (TREE_TYPE (expression
)));
21629 /* walk_tree callback function for instantiation_dependent_expression_p,
21630 below. Returns non-zero if a dependent subexpression is found. */
21633 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
21638 /* We don't have to worry about decltype currently because decltype
21639 of an instantiation-dependent expr is a dependent type. This
21640 might change depending on the resolution of DR 1172. */
21641 *walk_subtrees
= false;
21644 enum tree_code code
= TREE_CODE (*tp
);
21647 /* Don't treat an argument list as dependent just because it has no
21655 /* A constant with a dependent initializer is dependent. */
21656 if (value_dependent_expression_p (*tp
))
21660 case TEMPLATE_PARM_INDEX
:
21663 /* Handle expressions with type operands. */
21667 case AT_ENCODE_EXPR
:
21669 tree op
= TREE_OPERAND (*tp
, 0);
21670 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
21671 op
= TREE_TYPE (op
);
21674 if (dependent_type_p (op
))
21678 *walk_subtrees
= false;
21686 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp
))
21687 || (TRAIT_EXPR_TYPE2 (*tp
)
21688 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp
))))
21690 *walk_subtrees
= false;
21693 case COMPONENT_REF
:
21694 if (identifier_p (TREE_OPERAND (*tp
, 1)))
21695 /* In a template, finish_class_member_access_expr creates a
21696 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
21697 type-dependent, so that we can check access control at
21698 instantiation time (PR 42277). See also Core issue 1273. */
21703 if (instantiation_dependent_scope_ref_p (*tp
))
21708 /* Treat statement-expressions as dependent. */
21716 if (type_dependent_expression_p (*tp
))
21722 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
21723 sense defined by the ABI:
21725 "An expression is instantiation-dependent if it is type-dependent
21726 or value-dependent, or it has a subexpression that is type-dependent
21727 or value-dependent." */
21730 instantiation_dependent_expression_p (tree expression
)
21734 if (!processing_template_decl
)
21737 if (expression
== error_mark_node
)
21740 result
= cp_walk_tree_without_duplicates (&expression
,
21741 instantiation_dependent_r
, NULL
);
21742 return result
!= NULL_TREE
;
21745 /* Like type_dependent_expression_p, but it also works while not processing
21746 a template definition, i.e. during substitution or mangling. */
21749 type_dependent_expression_p_push (tree expr
)
21752 ++processing_template_decl
;
21753 b
= type_dependent_expression_p (expr
);
21754 --processing_template_decl
;
21758 /* Returns TRUE if ARGS contains a type-dependent expression. */
21761 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
21766 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
21768 if (type_dependent_expression_p (arg
))
21774 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21775 expressions) contains any type-dependent expressions. */
21778 any_type_dependent_elements_p (const_tree list
)
21780 for (; list
; list
= TREE_CHAIN (list
))
21781 if (type_dependent_expression_p (TREE_VALUE (list
)))
21787 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21788 expressions) contains any value-dependent expressions. */
21791 any_value_dependent_elements_p (const_tree list
)
21793 for (; list
; list
= TREE_CHAIN (list
))
21794 if (value_dependent_expression_p (TREE_VALUE (list
)))
21800 /* Returns TRUE if the ARG (a template argument) is dependent. */
21803 dependent_template_arg_p (tree arg
)
21805 if (!processing_template_decl
)
21808 /* Assume a template argument that was wrongly written by the user
21809 is dependent. This is consistent with what
21810 any_dependent_template_arguments_p [that calls this function]
21812 if (!arg
|| arg
== error_mark_node
)
21815 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
21816 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
21818 if (TREE_CODE (arg
) == TEMPLATE_DECL
21819 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
21820 return dependent_template_p (arg
);
21821 else if (ARGUMENT_PACK_P (arg
))
21823 tree args
= ARGUMENT_PACK_ARGS (arg
);
21824 int i
, len
= TREE_VEC_LENGTH (args
);
21825 for (i
= 0; i
< len
; ++i
)
21827 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
21833 else if (TYPE_P (arg
))
21834 return dependent_type_p (arg
);
21836 return (type_dependent_expression_p (arg
)
21837 || value_dependent_expression_p (arg
));
21840 /* Returns true if ARGS (a collection of template arguments) contains
21841 any types that require structural equality testing. */
21844 any_template_arguments_need_structural_equality_p (tree args
)
21851 if (args
== error_mark_node
)
21854 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
21856 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
21857 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
21859 tree arg
= TREE_VEC_ELT (level
, j
);
21860 tree packed_args
= NULL_TREE
;
21863 if (ARGUMENT_PACK_P (arg
))
21865 /* Look inside the argument pack. */
21866 packed_args
= ARGUMENT_PACK_ARGS (arg
);
21867 len
= TREE_VEC_LENGTH (packed_args
);
21870 for (k
= 0; k
< len
; ++k
)
21873 arg
= TREE_VEC_ELT (packed_args
, k
);
21875 if (error_operand_p (arg
))
21877 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
21879 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
21881 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
21882 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
21891 /* Returns true if ARGS (a collection of template arguments) contains
21892 any dependent arguments. */
21895 any_dependent_template_arguments_p (const_tree args
)
21902 if (args
== error_mark_node
)
21905 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
21907 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
21908 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
21909 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
21916 /* Returns TRUE if the template TMPL is dependent. */
21919 dependent_template_p (tree tmpl
)
21921 if (TREE_CODE (tmpl
) == OVERLOAD
)
21925 if (dependent_template_p (OVL_CURRENT (tmpl
)))
21927 tmpl
= OVL_NEXT (tmpl
);
21932 /* Template template parameters are dependent. */
21933 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
21934 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
21936 /* So are names that have not been looked up. */
21937 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
21939 /* So are member templates of dependent classes. */
21940 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
21941 return dependent_type_p (DECL_CONTEXT (tmpl
));
21945 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21948 dependent_template_id_p (tree tmpl
, tree args
)
21950 return (dependent_template_p (tmpl
)
21951 || any_dependent_template_arguments_p (args
));
21954 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21958 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
21962 if (!processing_template_decl
)
21965 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
21967 tree decl
= TREE_VEC_ELT (declv
, i
);
21968 tree init
= TREE_VEC_ELT (initv
, i
);
21969 tree cond
= TREE_VEC_ELT (condv
, i
);
21970 tree incr
= TREE_VEC_ELT (incrv
, i
);
21972 if (type_dependent_expression_p (decl
))
21975 if (init
&& type_dependent_expression_p (init
))
21978 if (type_dependent_expression_p (cond
))
21981 if (COMPARISON_CLASS_P (cond
)
21982 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
21983 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
21986 if (TREE_CODE (incr
) == MODOP_EXPR
)
21988 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
21989 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
21992 else if (type_dependent_expression_p (incr
))
21994 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
21996 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
21998 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
22000 tree t
= TREE_OPERAND (incr
, 1);
22001 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
22002 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
22011 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
22012 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
22013 no such TYPE can be found. Note that this function peers inside
22014 uninstantiated templates and therefore should be used only in
22015 extremely limited situations. ONLY_CURRENT_P restricts this
22016 peering to the currently open classes hierarchy (which is required
22017 when comparing types). */
22020 resolve_typename_type (tree type
, bool only_current_p
)
22029 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
22031 scope
= TYPE_CONTEXT (type
);
22032 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
22033 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
22034 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
22035 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
22036 identifier of the TYPENAME_TYPE anymore.
22037 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
22038 TYPENAME_TYPE instead, we avoid messing up with a possible
22039 typedef variant case. */
22040 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
22042 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
22043 it first before we can figure out what NAME refers to. */
22044 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
22046 if (TYPENAME_IS_RESOLVING_P (scope
))
22047 /* Given a class template A with a dependent base with nested type C,
22048 typedef typename A::C::C C will land us here, as trying to resolve
22049 the initial A::C leads to the local C typedef, which leads back to
22050 A::C::C. So we break the recursion now. */
22053 scope
= resolve_typename_type (scope
, only_current_p
);
22055 /* If we don't know what SCOPE refers to, then we cannot resolve the
22057 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
22059 /* If the SCOPE is a template type parameter, we have no way of
22060 resolving the name. */
22061 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
22063 /* If the SCOPE is not the current instantiation, there's no reason
22064 to look inside it. */
22065 if (only_current_p
&& !currently_open_class (scope
))
22067 /* If this is a typedef, we don't want to look inside (c++/11987). */
22068 if (typedef_variant_p (type
))
22070 /* If SCOPE isn't the template itself, it will not have a valid
22071 TYPE_FIELDS list. */
22072 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
22073 /* scope is either the template itself or a compatible instantiation
22074 like X<T>, so look up the name in the original template. */
22075 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
22077 /* scope is a partial instantiation, so we can't do the lookup or we
22078 will lose the template arguments. */
22080 /* Enter the SCOPE so that name lookup will be resolved as if we
22081 were in the class definition. In particular, SCOPE will no
22082 longer be considered a dependent type. */
22083 pushed_scope
= push_scope (scope
);
22084 /* Look up the declaration. */
22085 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
22086 tf_warning_or_error
);
22088 result
= NULL_TREE
;
22090 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
22091 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
22094 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type
))
22095 && TREE_CODE (decl
) == TYPE_DECL
)
22097 result
= TREE_TYPE (decl
);
22098 if (result
== error_mark_node
)
22099 result
= NULL_TREE
;
22101 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
22102 && DECL_CLASS_TEMPLATE_P (decl
))
22106 /* Obtain the template and the arguments. */
22107 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
22108 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
22109 /* Instantiate the template. */
22110 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
22111 /*entering_scope=*/0,
22112 tf_error
| tf_user
);
22113 if (result
== error_mark_node
)
22114 result
= NULL_TREE
;
22117 /* Leave the SCOPE. */
22119 pop_scope (pushed_scope
);
22121 /* If we failed to resolve it, return the original typename. */
22125 /* If lookup found a typename type, resolve that too. */
22126 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
22128 /* Ill-formed programs can cause infinite recursion here, so we
22129 must catch that. */
22130 TYPENAME_IS_RESOLVING_P (type
) = 1;
22131 result
= resolve_typename_type (result
, only_current_p
);
22132 TYPENAME_IS_RESOLVING_P (type
) = 0;
22135 /* Qualify the resulting type. */
22136 quals
= cp_type_quals (type
);
22138 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
22143 /* EXPR is an expression which is not type-dependent. Return a proxy
22144 for EXPR that can be used to compute the types of larger
22145 expressions containing EXPR. */
22148 build_non_dependent_expr (tree expr
)
22152 #ifdef ENABLE_CHECKING
22153 /* Try to get a constant value for all non-dependent expressions in
22154 order to expose bugs in *_dependent_expression_p and constexpr. */
22155 if (cxx_dialect
>= cxx11
)
22156 fold_non_dependent_expr (expr
);
22159 /* Preserve OVERLOADs; the functions must be available to resolve
22162 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
22163 inner_expr
= stmt_expr_value_expr (inner_expr
);
22164 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
22165 inner_expr
= TREE_OPERAND (inner_expr
, 0);
22166 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
22167 inner_expr
= TREE_OPERAND (inner_expr
, 1);
22168 if (is_overloaded_fn (inner_expr
)
22169 || TREE_CODE (inner_expr
) == OFFSET_REF
)
22171 /* There is no need to return a proxy for a variable. */
22174 /* Preserve string constants; conversions from string constants to
22175 "char *" are allowed, even though normally a "const char *"
22176 cannot be used to initialize a "char *". */
22177 if (TREE_CODE (expr
) == STRING_CST
)
22179 /* Preserve void and arithmetic constants, as an optimization -- there is no
22180 reason to create a new node. */
22181 if (TREE_CODE (expr
) == VOID_CST
22182 || TREE_CODE (expr
) == INTEGER_CST
22183 || TREE_CODE (expr
) == REAL_CST
)
22185 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
22186 There is at least one place where we want to know that a
22187 particular expression is a throw-expression: when checking a ?:
22188 expression, there are special rules if the second or third
22189 argument is a throw-expression. */
22190 if (TREE_CODE (expr
) == THROW_EXPR
)
22193 /* Don't wrap an initializer list, we need to be able to look inside. */
22194 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
22197 /* Don't wrap a dummy object, we need to be able to test for it. */
22198 if (is_dummy_object (expr
))
22201 if (TREE_CODE (expr
) == COND_EXPR
)
22202 return build3 (COND_EXPR
,
22204 TREE_OPERAND (expr
, 0),
22205 (TREE_OPERAND (expr
, 1)
22206 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
22207 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
22208 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
22209 if (TREE_CODE (expr
) == COMPOUND_EXPR
22210 && !COMPOUND_EXPR_OVERLOADED (expr
))
22211 return build2 (COMPOUND_EXPR
,
22213 TREE_OPERAND (expr
, 0),
22214 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
22216 /* If the type is unknown, it can't really be non-dependent */
22217 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
22219 /* Otherwise, build a NON_DEPENDENT_EXPR. */
22220 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
22223 /* ARGS is a vector of expressions as arguments to a function call.
22224 Replace the arguments with equivalent non-dependent expressions.
22225 This modifies ARGS in place. */
22228 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
22233 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
22235 tree newarg
= build_non_dependent_expr (arg
);
22237 (*args
)[ix
] = newarg
;
22241 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
22242 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
22246 make_auto_1 (tree name
)
22248 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
22249 TYPE_NAME (au
) = build_decl (input_location
,
22250 TYPE_DECL
, name
, au
);
22251 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
22252 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
22253 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
22254 TYPE_NAME (au
), NULL_TREE
);
22255 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
22256 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
22257 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
22263 make_decltype_auto (void)
22265 return make_auto_1 (get_identifier ("decltype(auto)"));
22271 return make_auto_1 (get_identifier ("auto"));
22274 /* Given type ARG, return std::initializer_list<ARG>. */
22279 tree std_init_list
= namespace_binding
22280 (get_identifier ("initializer_list"), std_node
);
22282 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
22284 error ("deducing from brace-enclosed initializer list requires "
22285 "#include <initializer_list>");
22286 return error_mark_node
;
22288 argvec
= make_tree_vec (1);
22289 TREE_VEC_ELT (argvec
, 0) = arg
;
22290 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
22291 NULL_TREE
, 0, tf_warning_or_error
);
22294 /* Replace auto in TYPE with std::initializer_list<auto>. */
22297 listify_autos (tree type
, tree auto_node
)
22299 tree init_auto
= listify (auto_node
);
22300 tree argvec
= make_tree_vec (1);
22301 TREE_VEC_ELT (argvec
, 0) = init_auto
;
22302 if (processing_template_decl
)
22303 argvec
= add_to_template_args (current_template_args (), argvec
);
22304 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
22307 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
22308 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
22311 do_auto_deduction (tree type
, tree init
, tree auto_node
)
22315 if (init
== error_mark_node
)
22316 return error_mark_node
;
22318 if (type_dependent_expression_p (init
))
22319 /* Defining a subset of type-dependent expressions that we can deduce
22320 from ahead of time isn't worth the trouble. */
22323 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
22324 with either a new invented type template parameter U or, if the
22325 initializer is a braced-init-list (8.5.4), with
22326 std::initializer_list<U>. */
22327 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
22329 if (!DIRECT_LIST_INIT_P (init
))
22330 type
= listify_autos (type
, auto_node
);
22331 else if (CONSTRUCTOR_NELTS (init
) == 1)
22332 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
22335 if (permerror (input_location
, "direct-list-initialization of "
22336 "%<auto%> requires exactly one element"))
22337 inform (input_location
,
22338 "for deduction to %<std::initializer_list%>, use copy-"
22339 "list-initialization (i.e. add %<=%> before the %<{%>)");
22340 type
= listify_autos (type
, auto_node
);
22344 init
= resolve_nondeduced_context (init
);
22346 targs
= make_tree_vec (1);
22347 if (AUTO_IS_DECLTYPE (auto_node
))
22349 bool id
= (DECL_P (init
) || (TREE_CODE (init
) == COMPONENT_REF
22350 && !REF_PARENTHESIZED_P (init
)));
22351 TREE_VEC_ELT (targs
, 0)
22352 = finish_decltype_type (init
, id
, tf_warning_or_error
);
22353 if (type
!= auto_node
)
22355 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
22356 return error_mark_node
;
22361 tree parms
= build_tree_list (NULL_TREE
, type
);
22362 tree tparms
= make_tree_vec (1);
22365 TREE_VEC_ELT (tparms
, 0)
22366 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
22367 val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
22368 DEDUCE_CALL
, LOOKUP_NORMAL
,
22369 NULL
, /*explain_p=*/false);
22372 if (processing_template_decl
)
22373 /* Try again at instantiation time. */
22375 if (type
&& type
!= error_mark_node
)
22376 /* If type is error_mark_node a diagnostic must have been
22377 emitted by now. Also, having a mention to '<type error>'
22378 in the diagnostic is not really useful to the user. */
22380 if (cfun
&& auto_node
== current_function_auto_return_pattern
22381 && LAMBDA_FUNCTION_P (current_function_decl
))
22382 error ("unable to deduce lambda return type from %qE", init
);
22384 error ("unable to deduce %qT from %qE", type
, init
);
22386 return error_mark_node
;
22390 /* If the list of declarators contains more than one declarator, the type
22391 of each declared variable is determined as described above. If the
22392 type deduced for the template parameter U is not the same in each
22393 deduction, the program is ill-formed. */
22394 if (TREE_TYPE (auto_node
)
22395 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
22397 if (cfun
&& auto_node
== current_function_auto_return_pattern
22398 && LAMBDA_FUNCTION_P (current_function_decl
))
22399 error ("inconsistent types %qT and %qT deduced for "
22400 "lambda return type", TREE_TYPE (auto_node
),
22401 TREE_VEC_ELT (targs
, 0));
22403 error ("inconsistent deduction for %qT: %qT and then %qT",
22404 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
22405 return error_mark_node
;
22407 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
22409 if (processing_template_decl
)
22410 targs
= add_to_template_args (current_template_args (), targs
);
22411 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
22414 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
22418 splice_late_return_type (tree type
, tree late_return_type
)
22422 if (late_return_type
== NULL_TREE
)
22424 argvec
= make_tree_vec (1);
22425 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
22426 if (processing_template_parmlist
)
22427 /* For a late-specified return type in a template type-parameter, we
22428 need to add a dummy argument level for its parmlist. */
22429 argvec
= add_to_template_args
22430 (make_tree_vec (processing_template_parmlist
), argvec
);
22431 if (current_template_parms
)
22432 argvec
= add_to_template_args (current_template_args (), argvec
);
22433 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
22436 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
22437 'decltype(auto)'. */
22440 is_auto (const_tree type
)
22442 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
22443 && (TYPE_IDENTIFIER (type
) == get_identifier ("auto")
22444 || TYPE_IDENTIFIER (type
) == get_identifier ("decltype(auto)")))
22450 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
22451 a use of `auto'. Returns NULL_TREE otherwise. */
22454 type_uses_auto (tree type
)
22456 return find_type_usage (type
, is_auto
);
22459 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
22460 'decltype(auto)' or a concept. */
22463 is_auto_or_concept (const_tree type
)
22465 return is_auto (type
); // or concept
22468 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
22469 a concept identifier) iff TYPE contains a use of a generic type. Returns
22470 NULL_TREE otherwise. */
22473 type_uses_auto_or_concept (tree type
)
22475 return find_type_usage (type
, is_auto_or_concept
);
22479 /* For a given template T, return the vector of typedefs referenced
22480 in T for which access check is needed at T instantiation time.
22481 T is either a FUNCTION_DECL or a RECORD_TYPE.
22482 Those typedefs were added to T by the function
22483 append_type_to_template_for_access_check. */
22485 vec
<qualified_typedef_usage_t
, va_gc
> *
22486 get_types_needing_access_check (tree t
)
22489 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
22491 if (!t
|| t
== error_mark_node
)
22494 if (!(ti
= get_template_info (t
)))
22497 if (CLASS_TYPE_P (t
)
22498 || TREE_CODE (t
) == FUNCTION_DECL
)
22500 if (!TI_TEMPLATE (ti
))
22503 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
22509 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
22510 tied to T. That list of typedefs will be access checked at
22511 T instantiation time.
22512 T is either a FUNCTION_DECL or a RECORD_TYPE.
22513 TYPE_DECL is a TYPE_DECL node representing a typedef.
22514 SCOPE is the scope through which TYPE_DECL is accessed.
22515 LOCATION is the location of the usage point of TYPE_DECL.
22517 This function is a subroutine of
22518 append_type_to_template_for_access_check. */
22521 append_type_to_template_for_access_check_1 (tree t
,
22524 location_t location
)
22526 qualified_typedef_usage_t typedef_usage
;
22529 if (!t
|| t
== error_mark_node
)
22532 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
22533 || CLASS_TYPE_P (t
))
22535 && TREE_CODE (type_decl
) == TYPE_DECL
22538 if (!(ti
= get_template_info (t
)))
22541 gcc_assert (TI_TEMPLATE (ti
));
22543 typedef_usage
.typedef_decl
= type_decl
;
22544 typedef_usage
.context
= scope
;
22545 typedef_usage
.locus
= location
;
22547 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
22550 /* Append TYPE_DECL to the template TEMPL.
22551 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
22552 At TEMPL instanciation time, TYPE_DECL will be checked to see
22553 if it can be accessed through SCOPE.
22554 LOCATION is the location of the usage point of TYPE_DECL.
22556 e.g. consider the following code snippet:
22563 template<class U> struct S
22565 C::myint mi; // <-- usage point of the typedef C::myint
22570 At S<char> instantiation time, we need to check the access of C::myint
22571 In other words, we need to check the access of the myint typedef through
22572 the C scope. For that purpose, this function will add the myint typedef
22573 and the scope C through which its being accessed to a list of typedefs
22574 tied to the template S. That list will be walked at template instantiation
22575 time and access check performed on each typedefs it contains.
22576 Note that this particular code snippet should yield an error because
22577 myint is private to C. */
22580 append_type_to_template_for_access_check (tree templ
,
22583 location_t location
)
22585 qualified_typedef_usage_t
*iter
;
22588 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
22590 /* Make sure we don't append the type to the template twice. */
22591 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
22592 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
22595 append_type_to_template_for_access_check_1 (templ
, type_decl
,
22599 /* Convert the generic type parameters in PARM that match the types given in the
22600 range [START_IDX, END_IDX) from the current_template_parms into generic type
22604 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
22606 tree current
= current_template_parms
;
22607 int depth
= TMPL_PARMS_DEPTH (current
);
22608 current
= INNERMOST_TEMPLATE_PARMS (current
);
22609 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
22611 for (int i
= 0; i
< start_idx
; ++i
)
22612 TREE_VEC_ELT (replacement
, i
)
22613 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
22615 for (int i
= start_idx
; i
< end_idx
; ++i
)
22617 /* Create a distinct parameter pack type from the current parm and add it
22618 to the replacement args to tsubst below into the generic function
22621 tree o
= TREE_TYPE (TREE_VALUE
22622 (TREE_VEC_ELT (current
, i
)));
22623 tree t
= copy_type (o
);
22624 TEMPLATE_TYPE_PARM_INDEX (t
)
22625 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
22627 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
22628 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
22629 TYPE_MAIN_VARIANT (t
) = t
;
22630 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
22631 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
22632 TREE_VEC_ELT (replacement
, i
) = t
;
22633 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
22636 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
22637 TREE_VEC_ELT (replacement
, i
)
22638 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
22640 /* If there are more levels then build up the replacement with the outer
22643 replacement
= add_to_template_args (template_parms_to_args
22644 (TREE_CHAIN (current_template_parms
)),
22647 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
22651 /* Set up the hash tables for template instantiations. */
22654 init_template_processing (void)
22656 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
22657 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
22660 /* Print stats about the template hash tables for -fstats. */
22663 print_template_statistics (void)
22665 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
22666 "%f collisions\n", (long) decl_specializations
->size (),
22667 (long) decl_specializations
->elements (),
22668 decl_specializations
->collisions ());
22669 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
22670 "%f collisions\n", (long) type_specializations
->size (),
22671 (long) type_specializations
->elements (),
22672 type_specializations
->collisions ());
22675 #include "gt-cp-pt.h"