1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
29 #include "coretypes.h"
33 #include "pointer-set.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "tree-inline.h"
43 #include "tree-iterator.h"
44 #include "type-utils.h"
46 /* The type of functions taking a tree, and some additional data, and
48 typedef int (*tree_fn_t
) (tree
, void*);
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template
{
54 struct pending_template
*next
;
55 struct tinst_level
*tinst
;
58 static GTY(()) struct pending_template
*pending_templates
;
59 static GTY(()) struct pending_template
*last_pending_template
;
61 int processing_template_parmlist
;
62 static int template_header_count
;
64 static GTY(()) tree saved_trees
;
65 static vec
<int> inline_parm_levels
;
67 static GTY(()) struct tinst_level
*current_tinst_level
;
69 static GTY(()) tree saved_access_scope
;
71 /* Live only within one (recursive) call to tsubst_expr. We use
72 this to pass the statement expression node from the STMT_EXPR
73 to the EXPR_STMT that is its result. */
74 static tree cur_stmt_expr
;
76 /* A map from local variable declarations in the body of the template
77 presently being instantiated to the corresponding instantiated
79 static struct pointer_map_t
*local_specializations
;
81 /* True if we've recursed into fn_type_unification too many times. */
82 static bool excessive_deduction_depth
;
84 typedef struct GTY(()) spec_entry
91 static GTY ((param_is (spec_entry
)))
92 htab_t decl_specializations
;
94 static GTY ((param_is (spec_entry
)))
95 htab_t type_specializations
;
97 /* Contains canonical template parameter types. The vector is indexed by
98 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
99 TREE_LIST, whose TREE_VALUEs contain the canonical template
100 parameters of various types and levels. */
101 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
103 #define UNIFY_ALLOW_NONE 0
104 #define UNIFY_ALLOW_MORE_CV_QUAL 1
105 #define UNIFY_ALLOW_LESS_CV_QUAL 2
106 #define UNIFY_ALLOW_DERIVED 4
107 #define UNIFY_ALLOW_INTEGER 8
108 #define UNIFY_ALLOW_OUTER_LEVEL 16
109 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
110 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
112 enum template_base_result
{
114 tbr_ambiguous_baseclass
,
118 static void push_access_scope (tree
);
119 static void pop_access_scope (tree
);
120 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
121 unification_kind_t
, int,
123 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
124 unification_kind_t
, int, bool, bool);
125 static int unify (tree
, tree
, tree
, tree
, int, bool);
126 static void add_pending_template (tree
);
127 static tree
reopen_tinst_level (struct tinst_level
*);
128 static tree
tsubst_initializer_list (tree
, tree
);
129 static tree
get_class_bindings (tree
, tree
, tree
, tree
);
130 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
132 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
134 static void tsubst_enum (tree
, tree
, tree
);
135 static tree
add_to_template_args (tree
, tree
);
136 static tree
add_outermost_template_args (tree
, tree
);
137 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
138 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
140 static int type_unification_real (tree
, tree
, tree
, const tree
*,
141 unsigned int, int, unification_kind_t
, int,
142 vec
<deferred_access_check
, va_gc
> **,
144 static void note_template_header (int);
145 static tree
convert_nontype_argument_function (tree
, tree
);
146 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
147 static tree
convert_template_argument (tree
, tree
, tree
,
148 tsubst_flags_t
, int, tree
);
149 static int for_each_template_parm (tree
, tree_fn_t
, void*,
150 struct pointer_set_t
*, bool);
151 static tree
expand_template_argument_pack (tree
);
152 static tree
build_template_parm_index (int, int, int, tree
, tree
);
153 static bool inline_needs_template_parms (tree
);
154 static void push_inline_template_parms_recursive (tree
, int);
155 static tree
retrieve_local_specialization (tree
);
156 static void register_local_specialization (tree
, tree
);
157 static hashval_t
hash_specialization (const void *p
);
158 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
159 static int mark_template_parm (tree
, void *);
160 static int template_parm_this_level_p (tree
, void *);
161 static tree
tsubst_friend_function (tree
, tree
);
162 static tree
tsubst_friend_class (tree
, tree
);
163 static int can_complete_type_without_circularity (tree
);
164 static tree
get_bindings (tree
, tree
, tree
, bool);
165 static int template_decl_level (tree
);
166 static int check_cv_quals_for_unify (int, tree
, tree
);
167 static void template_parm_level_and_index (tree
, int*, int*);
168 static int unify_pack_expansion (tree
, tree
, tree
,
169 tree
, unification_kind_t
, bool, bool);
170 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
171 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
172 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
173 static void regenerate_decl_from_template (tree
, tree
);
174 static tree
most_specialized_class (tree
, tree
, tsubst_flags_t
);
175 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
176 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
177 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
178 static bool check_specialization_scope (void);
179 static tree
process_partial_specialization (tree
);
180 static void set_current_access_from_decl (tree
);
181 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
183 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
184 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
186 static bool template_template_parm_bindings_ok_p (tree
, tree
);
187 static int template_args_equal (tree
, tree
);
188 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
189 static tree
for_each_template_parm_r (tree
*, int *, void *);
190 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
191 static void copy_default_args_to_explicit_spec (tree
);
192 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
193 static bool dependent_template_arg_p (tree
);
194 static bool any_template_arguments_need_structural_equality_p (tree
);
195 static bool dependent_type_p_r (tree
);
196 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
, bool);
197 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
198 static tree
tsubst_pack_expansion (tree
, tree
, tsubst_flags_t
, tree
);
199 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
200 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
201 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
203 static tree
listify (tree
);
204 static tree
listify_autos (tree
, tree
);
205 static tree
template_parm_to_arg (tree t
);
206 static tree
current_template_args (void);
207 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
208 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
210 /* Make the current scope suitable for access checking when we are
211 processing T. T can be FUNCTION_DECL for instantiated function
212 template, VAR_DECL for static member variable, or TYPE_DECL for
213 alias template (needed by instantiate_decl). */
216 push_access_scope (tree t
)
218 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
219 || TREE_CODE (t
) == TYPE_DECL
);
221 if (DECL_FRIEND_CONTEXT (t
))
222 push_nested_class (DECL_FRIEND_CONTEXT (t
));
223 else if (DECL_CLASS_SCOPE_P (t
))
224 push_nested_class (DECL_CONTEXT (t
));
226 push_to_top_level ();
228 if (TREE_CODE (t
) == FUNCTION_DECL
)
230 saved_access_scope
= tree_cons
231 (NULL_TREE
, current_function_decl
, saved_access_scope
);
232 current_function_decl
= t
;
236 /* Restore the scope set up by push_access_scope. T is the node we
240 pop_access_scope (tree t
)
242 if (TREE_CODE (t
) == FUNCTION_DECL
)
244 current_function_decl
= TREE_VALUE (saved_access_scope
);
245 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
248 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
251 pop_from_top_level ();
254 /* Do any processing required when DECL (a member template
255 declaration) is finished. Returns the TEMPLATE_DECL corresponding
256 to DECL, unless it is a specialization, in which case the DECL
257 itself is returned. */
260 finish_member_template_decl (tree decl
)
262 if (decl
== error_mark_node
)
263 return error_mark_node
;
265 gcc_assert (DECL_P (decl
));
267 if (TREE_CODE (decl
) == TYPE_DECL
)
271 type
= TREE_TYPE (decl
);
272 if (type
== error_mark_node
)
273 return error_mark_node
;
274 if (MAYBE_CLASS_TYPE_P (type
)
275 && CLASSTYPE_TEMPLATE_INFO (type
)
276 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
278 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
279 check_member_template (tmpl
);
284 else if (TREE_CODE (decl
) == FIELD_DECL
)
285 error ("data member %qD cannot be a member template", decl
);
286 else if (DECL_TEMPLATE_INFO (decl
))
288 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
290 check_member_template (DECL_TI_TEMPLATE (decl
));
291 return DECL_TI_TEMPLATE (decl
);
297 error ("invalid member template declaration %qD", decl
);
299 return error_mark_node
;
302 /* Create a template info node. */
305 build_template_info (tree template_decl
, tree template_args
)
307 tree result
= make_node (TEMPLATE_INFO
);
308 TI_TEMPLATE (result
) = template_decl
;
309 TI_ARGS (result
) = template_args
;
313 /* Return the template info node corresponding to T, whatever T is. */
316 get_template_info (const_tree t
)
318 tree tinfo
= NULL_TREE
;
320 if (!t
|| t
== error_mark_node
)
323 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
324 tinfo
= DECL_TEMPLATE_INFO (t
);
326 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
329 if (OVERLOAD_TYPE_P (t
))
330 tinfo
= TYPE_TEMPLATE_INFO (t
);
331 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
332 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
337 /* Returns the template nesting level of the indicated class TYPE.
347 A<T>::B<U> has depth two, while A<T> has depth one.
348 Both A<T>::B<int> and A<int>::B<U> have depth one, if
349 they are instantiations, not specializations.
351 This function is guaranteed to return 0 if passed NULL_TREE so
352 that, for example, `template_class_depth (current_class_type)' is
356 template_class_depth (tree type
)
361 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
362 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
363 ? CP_DECL_CONTEXT (type
) : CP_TYPE_CONTEXT (type
))
365 tree tinfo
= get_template_info (type
);
367 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
368 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
375 /* Subroutine of maybe_begin_member_template_processing.
376 Returns true if processing DECL needs us to push template parms. */
379 inline_needs_template_parms (tree decl
)
381 if (! DECL_TEMPLATE_INFO (decl
))
384 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
385 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
388 /* Subroutine of maybe_begin_member_template_processing.
389 Push the template parms in PARMS, starting from LEVELS steps into the
390 chain, and ending at the beginning, since template parms are listed
394 push_inline_template_parms_recursive (tree parmlist
, int levels
)
396 tree parms
= TREE_VALUE (parmlist
);
400 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
402 ++processing_template_decl
;
403 current_template_parms
404 = tree_cons (size_int (processing_template_decl
),
405 parms
, current_template_parms
);
406 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
408 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
410 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
412 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
414 if (parm
== error_mark_node
)
417 gcc_assert (DECL_P (parm
));
419 switch (TREE_CODE (parm
))
428 /* Make a CONST_DECL as is done in process_template_parm.
429 It is ugly that we recreate this here; the original
430 version built in process_template_parm is no longer
432 tree decl
= build_decl (DECL_SOURCE_LOCATION (parm
),
433 CONST_DECL
, DECL_NAME (parm
),
435 DECL_ARTIFICIAL (decl
) = 1;
436 TREE_CONSTANT (decl
) = 1;
437 TREE_READONLY (decl
) = 1;
438 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
439 SET_DECL_TEMPLATE_PARM_P (decl
);
450 /* Restore the template parameter context for a member template or
451 a friend template defined in a class definition. */
454 maybe_begin_member_template_processing (tree decl
)
459 if (inline_needs_template_parms (decl
))
461 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
462 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
464 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
467 parms
= TREE_CHAIN (parms
);
470 push_inline_template_parms_recursive (parms
, levels
);
473 /* Remember how many levels of template parameters we pushed so that
474 we can pop them later. */
475 inline_parm_levels
.safe_push (levels
);
478 /* Undo the effects of maybe_begin_member_template_processing. */
481 maybe_end_member_template_processing (void)
486 if (inline_parm_levels
.length () == 0)
489 last
= inline_parm_levels
.pop ();
490 for (i
= 0; i
< last
; ++i
)
492 --processing_template_decl
;
493 current_template_parms
= TREE_CHAIN (current_template_parms
);
498 /* Return a new template argument vector which contains all of ARGS,
499 but has as its innermost set of arguments the EXTRA_ARGS. */
502 add_to_template_args (tree args
, tree extra_args
)
509 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
512 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
513 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
515 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
516 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
518 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
519 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
524 /* Like add_to_template_args, but only the outermost ARGS are added to
525 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
526 (EXTRA_ARGS) levels are added. This function is used to combine
527 the template arguments from a partial instantiation with the
528 template arguments used to attain the full instantiation from the
529 partial instantiation. */
532 add_outermost_template_args (tree args
, tree extra_args
)
536 /* If there are more levels of EXTRA_ARGS than there are ARGS,
537 something very fishy is going on. */
538 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
540 /* If *all* the new arguments will be the EXTRA_ARGS, just return
542 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
545 /* For the moment, we make ARGS look like it contains fewer levels. */
546 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
548 new_args
= add_to_template_args (args
, extra_args
);
550 /* Now, we restore ARGS to its full dimensions. */
551 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
556 /* Return the N levels of innermost template arguments from the ARGS. */
559 get_innermost_template_args (tree args
, int n
)
567 /* If N is 1, just return the innermost set of template arguments. */
569 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
571 /* If we're not removing anything, just return the arguments we were
573 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
574 gcc_assert (extra_levels
>= 0);
575 if (extra_levels
== 0)
578 /* Make a new set of arguments, not containing the outer arguments. */
579 new_args
= make_tree_vec (n
);
580 for (i
= 1; i
<= n
; ++i
)
581 SET_TMPL_ARGS_LEVEL (new_args
, i
,
582 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
587 /* The inverse of get_innermost_template_args: Return all but the innermost
588 EXTRA_LEVELS levels of template arguments from the ARGS. */
591 strip_innermost_template_args (tree args
, int extra_levels
)
594 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
599 /* If N is 1, just return the outermost set of template arguments. */
601 return TMPL_ARGS_LEVEL (args
, 1);
603 /* If we're not removing anything, just return the arguments we were
605 gcc_assert (extra_levels
>= 0);
606 if (extra_levels
== 0)
609 /* Make a new set of arguments, not containing the inner arguments. */
610 new_args
= make_tree_vec (n
);
611 for (i
= 1; i
<= n
; ++i
)
612 SET_TMPL_ARGS_LEVEL (new_args
, i
,
613 TMPL_ARGS_LEVEL (args
, i
));
618 /* We've got a template header coming up; push to a new level for storing
622 begin_template_parm_list (void)
624 /* We use a non-tag-transparent scope here, which causes pushtag to
625 put tags in this scope, rather than in the enclosing class or
626 namespace scope. This is the right thing, since we want
627 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
628 global template class, push_template_decl handles putting the
629 TEMPLATE_DECL into top-level scope. For a nested template class,
632 template <class T> struct S1 {
633 template <class T> struct S2 {};
636 pushtag contains special code to call pushdecl_with_scope on the
637 TEMPLATE_DECL for S2. */
638 begin_scope (sk_template_parms
, NULL
);
639 ++processing_template_decl
;
640 ++processing_template_parmlist
;
641 note_template_header (0);
644 /* This routine is called when a specialization is declared. If it is
645 invalid to declare a specialization here, an error is reported and
646 false is returned, otherwise this routine will return true. */
649 check_specialization_scope (void)
651 tree scope
= current_scope ();
655 An explicit specialization shall be declared in the namespace of
656 which the template is a member, or, for member templates, in the
657 namespace of which the enclosing class or enclosing class
658 template is a member. An explicit specialization of a member
659 function, member class or static data member of a class template
660 shall be declared in the namespace of which the class template
662 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
664 error ("explicit specialization in non-namespace scope %qD", scope
);
670 In an explicit specialization declaration for a member of a class
671 template or a member template that appears in namespace scope,
672 the member template and some of its enclosing class templates may
673 remain unspecialized, except that the declaration shall not
674 explicitly specialize a class member template if its enclosing
675 class templates are not explicitly specialized as well. */
676 if (current_template_parms
)
678 error ("enclosing class templates are not explicitly specialized");
685 /* We've just seen template <>. */
688 begin_specialization (void)
690 begin_scope (sk_template_spec
, NULL
);
691 note_template_header (1);
692 return check_specialization_scope ();
695 /* Called at then end of processing a declaration preceded by
699 end_specialization (void)
702 reset_specialization ();
705 /* Any template <>'s that we have seen thus far are not referring to a
706 function specialization. */
709 reset_specialization (void)
711 processing_specialization
= 0;
712 template_header_count
= 0;
715 /* We've just seen a template header. If SPECIALIZATION is nonzero,
716 it was of the form template <>. */
719 note_template_header (int specialization
)
721 processing_specialization
= specialization
;
722 template_header_count
++;
725 /* We're beginning an explicit instantiation. */
728 begin_explicit_instantiation (void)
730 gcc_assert (!processing_explicit_instantiation
);
731 processing_explicit_instantiation
= true;
736 end_explicit_instantiation (void)
738 gcc_assert (processing_explicit_instantiation
);
739 processing_explicit_instantiation
= false;
742 /* An explicit specialization or partial specialization of TMPL is being
743 declared. Check that the namespace in which the specialization is
744 occurring is permissible. Returns false iff it is invalid to
745 specialize TMPL in the current namespace. */
748 check_specialization_namespace (tree tmpl
)
750 tree tpl_ns
= decl_namespace_context (tmpl
);
754 An explicit specialization shall be declared in the namespace of
755 which the template is a member, or, for member templates, in the
756 namespace of which the enclosing class or enclosing class
757 template is a member. An explicit specialization of a member
758 function, member class or static data member of a class template
759 shall be declared in the namespace of which the class template is
761 if (current_scope() != DECL_CONTEXT (tmpl
)
762 && !at_namespace_scope_p ())
764 error ("specialization of %qD must appear at namespace scope", tmpl
);
767 if (is_associated_namespace (current_namespace
, tpl_ns
))
768 /* Same or super-using namespace. */
772 permerror (input_location
, "specialization of %qD in different namespace", tmpl
);
773 permerror (input_location
, " from definition of %q+#D", tmpl
);
778 /* SPEC is an explicit instantiation. Check that it is valid to
779 perform this explicit instantiation in the current namespace. */
782 check_explicit_instantiation_namespace (tree spec
)
786 /* DR 275: An explicit instantiation shall appear in an enclosing
787 namespace of its template. */
788 ns
= decl_namespace_context (spec
);
789 if (!is_ancestor (current_namespace
, ns
))
790 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
791 "(which does not enclose namespace %qD)",
792 spec
, current_namespace
, ns
);
795 /* The TYPE is being declared. If it is a template type, that means it
796 is a partial specialization. Do appropriate error-checking. */
799 maybe_process_partial_specialization (tree type
)
803 if (type
== error_mark_node
)
804 return error_mark_node
;
806 /* A lambda that appears in specialization context is not itself a
808 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
811 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
813 error ("name of class shadows template template parameter %qD",
815 return error_mark_node
;
818 context
= TYPE_CONTEXT (type
);
820 if (TYPE_ALIAS_P (type
))
822 if (TYPE_TEMPLATE_INFO (type
)
823 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type
)))
824 error ("specialization of alias template %qD",
825 TYPE_TI_TEMPLATE (type
));
827 error ("explicit specialization of non-template %qT", type
);
828 return error_mark_node
;
830 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
832 /* This is for ordinary explicit specialization and partial
833 specialization of a template class such as:
835 template <> class C<int>;
839 template <class T> class C<T*>;
841 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
843 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
844 && !COMPLETE_TYPE_P (type
))
846 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
));
847 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
848 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
849 if (processing_template_decl
)
851 if (push_template_decl (TYPE_MAIN_DECL (type
))
853 return error_mark_node
;
856 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
857 error ("specialization of %qT after instantiation", type
);
858 else if (errorcount
&& !processing_specialization
859 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
860 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
861 /* Trying to define a specialization either without a template<> header
862 or in an inappropriate place. We've already given an error, so just
863 bail now so we don't actually define the specialization. */
864 return error_mark_node
;
866 else if (CLASS_TYPE_P (type
)
867 && !CLASSTYPE_USE_TEMPLATE (type
)
868 && CLASSTYPE_TEMPLATE_INFO (type
)
869 && context
&& CLASS_TYPE_P (context
)
870 && CLASSTYPE_TEMPLATE_INFO (context
))
872 /* This is for an explicit specialization of member class
873 template according to [temp.expl.spec/18]:
875 template <> template <class U> class C<int>::D;
877 The context `C<int>' must be an implicit instantiation.
878 Otherwise this is just a member class template declared
881 template <> class C<int> { template <class U> class D; };
882 template <> template <class U> class C<int>::D;
884 In the first case, `C<int>::D' is a specialization of `C<T>::D'
885 while in the second case, `C<int>::D' is a primary template
886 and `C<T>::D' may not exist. */
888 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
889 && !COMPLETE_TYPE_P (type
))
892 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
894 if (current_namespace
895 != decl_namespace_context (tmpl
))
897 permerror (input_location
, "specializing %q#T in different namespace", type
);
898 permerror (input_location
, " from definition of %q+#D", tmpl
);
901 /* Check for invalid specialization after instantiation:
903 template <> template <> class C<int>::D<int>;
904 template <> template <class U> class C<int>::D; */
906 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
907 t
; t
= TREE_CHAIN (t
))
909 tree inst
= TREE_VALUE (t
);
910 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
))
912 /* We already have a full specialization of this partial
913 instantiation. Reassign it to the new member
914 specialization template. */
919 elt
.tmpl
= most_general_template (tmpl
);
920 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
923 htab_remove_elt (type_specializations
, &elt
);
926 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
928 slot
= htab_find_slot (type_specializations
, &elt
, INSERT
);
929 entry
= ggc_alloc_spec_entry ();
933 else if (COMPLETE_OR_OPEN_TYPE_P (inst
))
934 /* But if we've had an implicit instantiation, that's a
935 problem ([temp.expl.spec]/6). */
936 error ("specialization %qT after instantiation %qT",
940 /* Mark TYPE as a specialization. And as a result, we only
941 have one level of template argument for the innermost
943 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
944 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
945 CLASSTYPE_TI_ARGS (type
)
946 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
949 else if (processing_specialization
)
951 /* Someday C++0x may allow for enum template specialization. */
952 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
953 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
954 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
955 "of %qD not allowed by ISO C++", type
);
958 error ("explicit specialization of non-template %qT", type
);
959 return error_mark_node
;
966 /* Returns nonzero if we can optimize the retrieval of specializations
967 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
968 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
971 optimize_specialization_lookup_p (tree tmpl
)
973 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
974 && DECL_CLASS_SCOPE_P (tmpl
)
975 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
977 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
978 /* The optimized lookup depends on the fact that the
979 template arguments for the member function template apply
980 purely to the containing class, which is not true if the
981 containing class is an explicit or partial
983 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
984 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
985 && !DECL_CONV_FN_P (tmpl
)
986 /* It is possible to have a template that is not a member
987 template and is not a member of a template class:
989 template <typename T>
990 struct S { friend A::f(); };
992 Here, the friend function is a template, but the context does
993 not have template information. The optimized lookup relies
994 on having ARGS be the template arguments for both the class
995 and the function template. */
996 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
999 /* Retrieve the specialization (in the sense of [temp.spec] - a
1000 specialization is either an instantiation or an explicit
1001 specialization) of TMPL for the given template ARGS. If there is
1002 no such specialization, return NULL_TREE. The ARGS are a vector of
1003 arguments, or a vector of vectors of arguments, in the case of
1004 templates with more than one level of parameters.
1006 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1007 then we search for a partial specialization matching ARGS. This
1008 parameter is ignored if TMPL is not a class template.
1010 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1011 result is a NONTYPE_ARGUMENT_PACK. */
1014 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1016 if (tmpl
== NULL_TREE
)
1019 if (args
== error_mark_node
)
1022 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1023 || TREE_CODE (tmpl
) == FIELD_DECL
);
1025 /* There should be as many levels of arguments as there are
1026 levels of parameters. */
1027 gcc_assert (TMPL_ARGS_DEPTH (args
)
1028 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1029 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1030 : template_class_depth (DECL_CONTEXT (tmpl
))));
1032 if (optimize_specialization_lookup_p (tmpl
))
1034 tree class_template
;
1035 tree class_specialization
;
1036 vec
<tree
, va_gc
> *methods
;
1040 /* The template arguments actually apply to the containing
1041 class. Find the class specialization with those
1043 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1044 class_specialization
1045 = retrieve_specialization (class_template
, args
, 0);
1046 if (!class_specialization
)
1048 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1049 for the specialization. */
1050 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1053 /* Iterate through the methods with the indicated name, looking
1054 for the one that has an instance of TMPL. */
1055 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1056 for (fns
= (*methods
)[idx
]; fns
; fns
= OVL_NEXT (fns
))
1058 tree fn
= OVL_CURRENT (fns
);
1059 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1060 /* using-declarations can add base methods to the method vec,
1061 and we don't want those here. */
1062 && DECL_CONTEXT (fn
) == class_specialization
)
1071 htab_t specializations
;
1075 elt
.spec
= NULL_TREE
;
1077 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1078 specializations
= type_specializations
;
1080 specializations
= decl_specializations
;
1083 hash
= hash_specialization (&elt
);
1084 found
= (spec_entry
*) htab_find_with_hash (specializations
, &elt
, hash
);
1092 /* Like retrieve_specialization, but for local declarations. */
1095 retrieve_local_specialization (tree tmpl
)
1099 if (local_specializations
== NULL
)
1102 slot
= pointer_map_contains (local_specializations
, tmpl
);
1103 return slot
? (tree
) *slot
: NULL_TREE
;
1106 /* Returns nonzero iff DECL is a specialization of TMPL. */
1109 is_specialization_of (tree decl
, tree tmpl
)
1113 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1117 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1123 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1125 for (t
= TREE_TYPE (decl
);
1127 t
= CLASSTYPE_USE_TEMPLATE (t
)
1128 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1129 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1136 /* Returns nonzero iff DECL is a specialization of friend declaration
1137 FRIEND_DECL according to [temp.friend]. */
1140 is_specialization_of_friend (tree decl
, tree friend_decl
)
1142 bool need_template
= true;
1145 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1146 || TREE_CODE (decl
) == TYPE_DECL
);
1148 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1149 of a template class, we want to check if DECL is a specialization
1151 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1152 && DECL_TEMPLATE_INFO (friend_decl
)
1153 && !DECL_USE_TEMPLATE (friend_decl
))
1155 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1156 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1157 need_template
= false;
1159 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1160 && !PRIMARY_TEMPLATE_P (friend_decl
))
1161 need_template
= false;
1163 /* There is nothing to do if this is not a template friend. */
1164 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1167 if (is_specialization_of (decl
, friend_decl
))
1171 A member of a class template may be declared to be a friend of a
1172 non-template class. In this case, the corresponding member of
1173 every specialization of the class template is a friend of the
1174 class granting friendship.
1176 For example, given a template friend declaration
1178 template <class T> friend void A<T>::f();
1180 the member function below is considered a friend
1182 template <> struct A<int> {
1186 For this type of template friend, TEMPLATE_DEPTH below will be
1187 nonzero. To determine if DECL is a friend of FRIEND, we first
1188 check if the enclosing class is a specialization of another. */
1190 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1192 && DECL_CLASS_SCOPE_P (decl
)
1193 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1194 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1196 /* Next, we check the members themselves. In order to handle
1197 a few tricky cases, such as when FRIEND_DECL's are
1199 template <class T> friend void A<T>::g(T t);
1200 template <class T> template <T t> friend void A<T>::h();
1204 void A<int>::g(int);
1205 template <int> void A<int>::h();
1207 we need to figure out ARGS, the template arguments from
1208 the context of DECL. This is required for template substitution
1209 of `T' in the function parameter of `g' and template parameter
1210 of `h' in the above examples. Here ARGS corresponds to `int'. */
1212 tree context
= DECL_CONTEXT (decl
);
1213 tree args
= NULL_TREE
;
1214 int current_depth
= 0;
1216 while (current_depth
< template_depth
)
1218 if (CLASSTYPE_TEMPLATE_INFO (context
))
1220 if (current_depth
== 0)
1221 args
= TYPE_TI_ARGS (context
);
1223 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1226 context
= TYPE_CONTEXT (context
);
1229 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1234 tree friend_args_type
;
1235 tree decl_args_type
;
1237 /* Make sure that both DECL and FRIEND_DECL are templates or
1239 is_template
= DECL_TEMPLATE_INFO (decl
)
1240 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1241 if (need_template
^ is_template
)
1243 else if (is_template
)
1245 /* If both are templates, check template parameter list. */
1247 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1249 if (!comp_template_parms
1250 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1254 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1257 decl_type
= TREE_TYPE (decl
);
1259 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1260 tf_none
, NULL_TREE
);
1261 if (friend_type
== error_mark_node
)
1264 /* Check if return types match. */
1265 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1268 /* Check if function parameter types match, ignoring the
1269 `this' parameter. */
1270 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1271 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1272 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1273 friend_args_type
= TREE_CHAIN (friend_args_type
);
1274 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1275 decl_args_type
= TREE_CHAIN (decl_args_type
);
1277 return compparms (decl_args_type
, friend_args_type
);
1281 /* DECL is a TYPE_DECL */
1283 tree decl_type
= TREE_TYPE (decl
);
1285 /* Make sure that both DECL and FRIEND_DECL are templates or
1288 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1289 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1291 if (need_template
^ is_template
)
1293 else if (is_template
)
1296 /* If both are templates, check the name of the two
1297 TEMPLATE_DECL's first because is_friend didn't. */
1298 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1299 != DECL_NAME (friend_decl
))
1302 /* Now check template parameter list. */
1304 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1306 return comp_template_parms
1307 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1311 return (DECL_NAME (decl
)
1312 == DECL_NAME (friend_decl
));
1318 /* Register the specialization SPEC as a specialization of TMPL with
1319 the indicated ARGS. IS_FRIEND indicates whether the specialization
1320 is actually just a friend declaration. Returns SPEC, or an
1321 equivalent prior declaration, if available.
1323 We also store instantiations of field packs in the hash table, even
1324 though they are not themselves templates, to make lookup easier. */
1327 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1334 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1335 || (TREE_CODE (tmpl
) == FIELD_DECL
1336 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1338 if (TREE_CODE (spec
) == FUNCTION_DECL
1339 && uses_template_parms (DECL_TI_ARGS (spec
)))
1340 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1341 register it; we want the corresponding TEMPLATE_DECL instead.
1342 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1343 the more obvious `uses_template_parms (spec)' to avoid problems
1344 with default function arguments. In particular, given
1345 something like this:
1347 template <class T> void f(T t1, T t = T())
1349 the default argument expression is not substituted for in an
1350 instantiation unless and until it is actually needed. */
1353 if (optimize_specialization_lookup_p (tmpl
))
1354 /* We don't put these specializations in the hash table, but we might
1355 want to give an error about a mismatch. */
1356 fn
= retrieve_specialization (tmpl
, args
, 0);
1364 hash
= hash_specialization (&elt
);
1367 htab_find_slot_with_hash (decl_specializations
, &elt
, hash
, INSERT
);
1369 fn
= ((spec_entry
*) *slot
)->spec
;
1374 /* We can sometimes try to re-register a specialization that we've
1375 already got. In particular, regenerate_decl_from_template calls
1376 duplicate_decls which will update the specialization list. But,
1377 we'll still get called again here anyhow. It's more convenient
1378 to simply allow this than to try to prevent it. */
1381 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1383 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1385 if (DECL_ODR_USED (fn
)
1386 || DECL_EXPLICIT_INSTANTIATION (fn
))
1388 error ("specialization of %qD after instantiation",
1390 return error_mark_node
;
1395 /* This situation should occur only if the first
1396 specialization is an implicit instantiation, the
1397 second is an explicit specialization, and the
1398 implicit instantiation has not yet been used. That
1399 situation can occur if we have implicitly
1400 instantiated a member function and then specialized
1403 We can also wind up here if a friend declaration that
1404 looked like an instantiation turns out to be a
1407 template <class T> void foo(T);
1408 class S { friend void foo<>(int) };
1409 template <> void foo(int);
1411 We transform the existing DECL in place so that any
1412 pointers to it become pointers to the updated
1415 If there was a definition for the template, but not
1416 for the specialization, we want this to look as if
1417 there were no definition, and vice versa. */
1418 DECL_INITIAL (fn
) = NULL_TREE
;
1419 duplicate_decls (spec
, fn
, is_friend
);
1420 /* The call to duplicate_decls will have applied
1423 An explicit specialization of a function template
1424 is inline only if it is explicitly declared to be,
1425 and independently of whether its function template
1428 to the primary function; now copy the inline bits to
1429 the various clones. */
1430 FOR_EACH_CLONE (clone
, fn
)
1432 DECL_DECLARED_INLINE_P (clone
)
1433 = DECL_DECLARED_INLINE_P (fn
);
1434 DECL_SOURCE_LOCATION (clone
)
1435 = DECL_SOURCE_LOCATION (fn
);
1437 check_specialization_namespace (tmpl
);
1442 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1444 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1445 /* Dup decl failed, but this is a new definition. Set the
1446 line number so any errors match this new
1448 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1454 return duplicate_decls (spec
, fn
, is_friend
);
1456 /* A specialization must be declared in the same namespace as the
1457 template it is specializing. */
1458 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1459 && !check_specialization_namespace (tmpl
))
1460 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1462 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1464 spec_entry
*entry
= ggc_alloc_spec_entry ();
1465 gcc_assert (tmpl
&& args
&& spec
);
1468 if (TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1469 && PRIMARY_TEMPLATE_P (tmpl
)
1470 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1471 /* TMPL is a forward declaration of a template function; keep a list
1472 of all specializations in case we need to reassign them to a friend
1473 template later in tsubst_friend_function. */
1474 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1475 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1481 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1482 TMPL and ARGS members, ignores SPEC. */
1484 int comparing_specializations
;
1487 eq_specializations (const void *p1
, const void *p2
)
1489 const spec_entry
*e1
= (const spec_entry
*)p1
;
1490 const spec_entry
*e2
= (const spec_entry
*)p2
;
1493 ++comparing_specializations
;
1494 equal
= (e1
->tmpl
== e2
->tmpl
1495 && comp_template_args (e1
->args
, e2
->args
));
1496 --comparing_specializations
;
1501 /* Returns a hash for a template TMPL and template arguments ARGS. */
1504 hash_tmpl_and_args (tree tmpl
, tree args
)
1506 hashval_t val
= DECL_UID (tmpl
);
1507 return iterative_hash_template_arg (args
, val
);
1510 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1514 hash_specialization (const void *p
)
1516 const spec_entry
*e
= (const spec_entry
*)p
;
1517 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1520 /* Recursively calculate a hash value for a template argument ARG, for use
1521 in the hash tables of template specializations. */
1524 iterative_hash_template_arg (tree arg
, hashval_t val
)
1526 unsigned HOST_WIDE_INT i
;
1527 enum tree_code code
;
1530 if (arg
== NULL_TREE
)
1531 return iterative_hash_object (arg
, val
);
1536 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1537 /* We can get one of these when re-hashing a previous entry in the middle
1538 of substituting into a pack expansion. Just look through it. */
1539 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1541 code
= TREE_CODE (arg
);
1542 tclass
= TREE_CODE_CLASS (code
);
1544 val
= iterative_hash_object (code
, val
);
1551 case IDENTIFIER_NODE
:
1552 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1556 int i
, len
= TREE_VEC_LENGTH (arg
);
1557 for (i
= 0; i
< len
; ++i
)
1558 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1562 case TYPE_PACK_EXPANSION
:
1563 case EXPR_PACK_EXPANSION
:
1564 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1565 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1567 case TYPE_ARGUMENT_PACK
:
1568 case NONTYPE_ARGUMENT_PACK
:
1569 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1572 for (; arg
; arg
= TREE_CHAIN (arg
))
1573 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1577 for (; arg
; arg
= OVL_NEXT (arg
))
1578 val
= iterative_hash_template_arg (OVL_CURRENT (arg
), val
);
1584 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1586 val
= iterative_hash_template_arg (field
, val
);
1587 val
= iterative_hash_template_arg (value
, val
);
1593 if (!DECL_ARTIFICIAL (arg
))
1595 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1596 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1598 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1601 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1604 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1605 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1607 case TEMPLATE_PARM_INDEX
:
1608 val
= iterative_hash_template_arg
1609 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1610 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1611 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1614 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1615 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1616 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1619 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1621 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1625 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1626 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1627 val
= iterative_hash_object (code
, val
);
1628 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1631 /* A lambda can't appear in a template arg, but don't crash on
1633 gcc_assert (seen_error ());
1637 case IMPLICIT_CONV_EXPR
:
1638 case STATIC_CAST_EXPR
:
1639 case REINTERPRET_CAST_EXPR
:
1640 case CONST_CAST_EXPR
:
1641 case DYNAMIC_CAST_EXPR
:
1643 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1644 /* Now hash operands as usual. */
1654 if (TYPE_CANONICAL (arg
))
1655 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1657 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1658 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1659 /* Otherwise just compare the types during lookup. */
1662 case tcc_declaration
:
1664 return iterative_hash_expr (arg
, val
);
1667 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1669 unsigned n
= cp_tree_operand_length (arg
);
1670 for (i
= 0; i
< n
; ++i
)
1671 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1679 /* Unregister the specialization SPEC as a specialization of TMPL.
1680 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1681 if the SPEC was listed as a specialization of TMPL.
1683 Note that SPEC has been ggc_freed, so we can't look inside it. */
1686 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1691 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1692 elt
.args
= TI_ARGS (tinfo
);
1693 elt
.spec
= NULL_TREE
;
1695 entry
= (spec_entry
*) htab_find (decl_specializations
, &elt
);
1698 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1699 gcc_assert (new_spec
!= NULL_TREE
);
1700 entry
->spec
= new_spec
;
1707 /* Like register_specialization, but for local declarations. We are
1708 registering SPEC, an instantiation of TMPL. */
1711 register_local_specialization (tree spec
, tree tmpl
)
1715 slot
= pointer_map_insert (local_specializations
, tmpl
);
1719 /* TYPE is a class type. Returns true if TYPE is an explicitly
1720 specialized class. */
1723 explicit_class_specialization_p (tree type
)
1725 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1727 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1730 /* Print the list of functions at FNS, going through all the overloads
1731 for each element of the list. Alternatively, FNS can not be a
1732 TREE_LIST, in which case it will be printed together with all the
1735 MORE and *STR should respectively be FALSE and NULL when the function
1736 is called from the outside. They are used internally on recursive
1737 calls. print_candidates manages the two parameters and leaves NULL
1738 in *STR when it ends. */
1741 print_candidates_1 (tree fns
, bool more
, const char **str
)
1744 char *spaces
= NULL
;
1746 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1747 if (TREE_CODE (fn
) == TREE_LIST
)
1749 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1750 print_candidates_1 (TREE_VALUE (fn2
),
1751 TREE_CHAIN (fn2
) || more
, str
);
1755 tree cand
= OVL_CURRENT (fn
);
1758 /* Pick the prefix string. */
1759 if (!more
&& !OVL_NEXT (fns
))
1761 inform (DECL_SOURCE_LOCATION (cand
),
1762 "candidate is: %#D", cand
);
1766 *str
= _("candidates are:");
1767 spaces
= get_spaces (*str
);
1769 inform (DECL_SOURCE_LOCATION (cand
), "%s %#D", *str
, cand
);
1770 *str
= spaces
? spaces
: *str
;
1780 /* Print the list of candidate FNS in an error message. FNS can also
1781 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1784 print_candidates (tree fns
)
1786 const char *str
= NULL
;
1787 print_candidates_1 (fns
, false, &str
);
1788 gcc_assert (str
== NULL
);
1791 /* Returns the template (one of the functions given by TEMPLATE_ID)
1792 which can be specialized to match the indicated DECL with the
1793 explicit template args given in TEMPLATE_ID. The DECL may be
1794 NULL_TREE if none is available. In that case, the functions in
1795 TEMPLATE_ID are non-members.
1797 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1798 specialization of a member template.
1800 The TEMPLATE_COUNT is the number of references to qualifying
1801 template classes that appeared in the name of the function. See
1802 check_explicit_specialization for a more accurate description.
1804 TSK indicates what kind of template declaration (if any) is being
1805 declared. TSK_TEMPLATE indicates that the declaration given by
1806 DECL, though a FUNCTION_DECL, has template parameters, and is
1807 therefore a template function.
1809 The template args (those explicitly specified and those deduced)
1810 are output in a newly created vector *TARGS_OUT.
1812 If it is impossible to determine the result, an error message is
1813 issued. The error_mark_node is returned to indicate failure. */
1816 determine_specialization (tree template_id
,
1819 int need_member_template
,
1825 tree explicit_targs
;
1826 tree candidates
= NULL_TREE
;
1827 /* A TREE_LIST of templates of which DECL may be a specialization.
1828 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1829 corresponding TREE_PURPOSE is the set of template arguments that,
1830 when used to instantiate the template, would produce a function
1831 with the signature of DECL. */
1832 tree templates
= NULL_TREE
;
1834 cp_binding_level
*b
;
1836 *targs_out
= NULL_TREE
;
1838 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1839 return error_mark_node
;
1841 /* We shouldn't be specializing a member template of an
1842 unspecialized class template; we already gave an error in
1843 check_specialization_scope, now avoid crashing. */
1844 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
1845 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
1847 gcc_assert (errorcount
);
1848 return error_mark_node
;
1851 fns
= TREE_OPERAND (template_id
, 0);
1852 explicit_targs
= TREE_OPERAND (template_id
, 1);
1854 if (fns
== error_mark_node
)
1855 return error_mark_node
;
1857 /* Check for baselinks. */
1858 if (BASELINK_P (fns
))
1859 fns
= BASELINK_FUNCTIONS (fns
);
1861 if (!is_overloaded_fn (fns
))
1863 error ("%qD is not a function template", fns
);
1864 return error_mark_node
;
1867 /* Count the number of template headers specified for this
1870 for (b
= current_binding_level
;
1871 b
->kind
== sk_template_parms
;
1875 for (; fns
; fns
= OVL_NEXT (fns
))
1877 tree fn
= OVL_CURRENT (fns
);
1879 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1881 tree decl_arg_types
;
1885 /* In case of explicit specialization, we need to check if
1886 the number of template headers appearing in the specialization
1887 is correct. This is usually done in check_explicit_specialization,
1888 but the check done there cannot be exhaustive when specializing
1889 member functions. Consider the following code:
1891 template <> void A<int>::f(int);
1892 template <> template <> void A<int>::f(int);
1894 Assuming that A<int> is not itself an explicit specialization
1895 already, the first line specializes "f" which is a non-template
1896 member function, whilst the second line specializes "f" which
1897 is a template member function. So both lines are syntactically
1898 correct, and check_explicit_specialization does not reject
1901 Here, we can do better, as we are matching the specialization
1902 against the declarations. We count the number of template
1903 headers, and we check if they match TEMPLATE_COUNT + 1
1904 (TEMPLATE_COUNT is the number of qualifying template classes,
1905 plus there must be another header for the member template
1908 Notice that if header_count is zero, this is not a
1909 specialization but rather a template instantiation, so there
1910 is no check we can perform here. */
1911 if (header_count
&& header_count
!= template_count
+ 1)
1914 /* Check that the number of template arguments at the
1915 innermost level for DECL is the same as for FN. */
1916 if (current_binding_level
->kind
== sk_template_parms
1917 && !current_binding_level
->explicit_spec_p
1918 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1919 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1920 (current_template_parms
))))
1923 /* DECL might be a specialization of FN. */
1924 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1925 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1927 /* For a non-static member function, we need to make sure
1928 that the const qualification is the same. Since
1929 get_bindings does not try to merge the "this" parameter,
1930 we must do the comparison explicitly. */
1931 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1932 && !same_type_p (TREE_VALUE (fn_arg_types
),
1933 TREE_VALUE (decl_arg_types
)))
1936 /* Skip the "this" parameter and, for constructors of
1937 classes with virtual bases, the VTT parameter. A
1938 full specialization of a constructor will have a VTT
1939 parameter, but a template never will. */
1941 = skip_artificial_parms_for (decl
, decl_arg_types
);
1943 = skip_artificial_parms_for (fn
, fn_arg_types
);
1945 /* Function templates cannot be specializations; there are
1946 no partial specializations of functions. Therefore, if
1947 the type of DECL does not match FN, there is no
1949 if (tsk
== tsk_template
)
1951 if (compparms (fn_arg_types
, decl_arg_types
))
1952 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1956 /* See whether this function might be a specialization of this
1957 template. Suppress access control because we might be trying
1958 to make this specialization a friend, and we have already done
1959 access control for the declaration of the specialization. */
1960 push_deferring_access_checks (dk_no_check
);
1961 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1962 pop_deferring_access_checks ();
1965 /* We cannot deduce template arguments that when used to
1966 specialize TMPL will produce DECL. */
1969 /* Make sure that the deduced arguments actually work. */
1970 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
1971 if (insttype
== error_mark_node
)
1974 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
1975 if (!compparms (fn_arg_types
, decl_arg_types
))
1978 /* Save this template, and the arguments deduced. */
1979 templates
= tree_cons (targs
, fn
, templates
);
1981 else if (need_member_template
)
1982 /* FN is an ordinary member function, and we need a
1983 specialization of a member template. */
1985 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1986 /* We can get IDENTIFIER_NODEs here in certain erroneous
1989 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1990 /* This is just an ordinary non-member function. Nothing can
1991 be a specialization of that. */
1993 else if (DECL_ARTIFICIAL (fn
))
1994 /* Cannot specialize functions that are created implicitly. */
1998 tree decl_arg_types
;
2000 /* This is an ordinary member function. However, since
2001 we're here, we can assume its enclosing class is a
2002 template class. For example,
2004 template <typename T> struct S { void f(); };
2005 template <> void S<int>::f() {}
2007 Here, S<int>::f is a non-template, but S<int> is a
2008 template class. If FN has the same type as DECL, we
2009 might be in business. */
2011 if (!DECL_TEMPLATE_INFO (fn
))
2012 /* Its enclosing class is an explicit specialization
2013 of a template class. This is not a candidate. */
2016 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2017 TREE_TYPE (TREE_TYPE (fn
))))
2018 /* The return types differ. */
2021 /* Adjust the type of DECL in case FN is a static member. */
2022 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2023 if (DECL_STATIC_FUNCTION_P (fn
)
2024 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2025 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2027 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2030 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2034 if (templates
&& TREE_CHAIN (templates
))
2040 It is possible for a specialization with a given function
2041 signature to be instantiated from more than one function
2042 template. In such cases, explicit specification of the
2043 template arguments must be used to uniquely identify the
2044 function template specialization being specialized.
2046 Note that here, there's no suggestion that we're supposed to
2047 determine which of the candidate templates is most
2048 specialized. However, we, also have:
2052 Partial ordering of overloaded function template
2053 declarations is used in the following contexts to select
2054 the function template to which a function template
2055 specialization refers:
2057 -- when an explicit specialization refers to a function
2060 So, we do use the partial ordering rules, at least for now.
2061 This extension can only serve to make invalid programs valid,
2062 so it's safe. And, there is strong anecdotal evidence that
2063 the committee intended the partial ordering rules to apply;
2064 the EDG front end has that behavior, and John Spicer claims
2065 that the committee simply forgot to delete the wording in
2066 [temp.expl.spec]. */
2067 tree tmpl
= most_specialized_instantiation (templates
);
2068 if (tmpl
!= error_mark_node
)
2071 TREE_CHAIN (templates
) = NULL_TREE
;
2075 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2077 error ("template-id %qD for %q+D does not match any template "
2078 "declaration", template_id
, decl
);
2079 if (header_count
&& header_count
!= template_count
+ 1)
2080 inform (input_location
, "saw %d %<template<>%>, need %d for "
2081 "specializing a member function template",
2082 header_count
, template_count
+ 1);
2083 return error_mark_node
;
2085 else if ((templates
&& TREE_CHAIN (templates
))
2086 || (candidates
&& TREE_CHAIN (candidates
))
2087 || (templates
&& candidates
))
2089 error ("ambiguous template specialization %qD for %q+D",
2091 candidates
= chainon (candidates
, templates
);
2092 print_candidates (candidates
);
2093 return error_mark_node
;
2096 /* We have one, and exactly one, match. */
2099 tree fn
= TREE_VALUE (candidates
);
2100 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2101 /* DECL is a re-declaration or partial instantiation of a template
2103 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2105 /* It was a specialization of an ordinary member function in a
2107 return DECL_TI_TEMPLATE (fn
);
2110 /* It was a specialization of a template. */
2111 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2112 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2114 *targs_out
= copy_node (targs
);
2115 SET_TMPL_ARGS_LEVEL (*targs_out
,
2116 TMPL_ARGS_DEPTH (*targs_out
),
2117 TREE_PURPOSE (templates
));
2120 *targs_out
= TREE_PURPOSE (templates
);
2121 return TREE_VALUE (templates
);
2124 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2125 but with the default argument values filled in from those in the
2129 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2132 tree new_spec_types
;
2137 if (spec_types
== void_list_node
)
2138 return void_list_node
;
2140 /* Substitute into the rest of the list. */
2142 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2143 TREE_CHAIN (tmpl_types
));
2145 /* Add the default argument for this parameter. */
2146 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2147 TREE_VALUE (spec_types
),
2151 /* DECL is an explicit specialization. Replicate default arguments
2152 from the template it specializes. (That way, code like:
2154 template <class T> void f(T = 3);
2155 template <> void f(double);
2158 works, as required.) An alternative approach would be to look up
2159 the correct default arguments at the call-site, but this approach
2160 is consistent with how implicit instantiations are handled. */
2163 copy_default_args_to_explicit_spec (tree decl
)
2168 tree new_spec_types
;
2172 tree object_type
= NULL_TREE
;
2173 tree in_charge
= NULL_TREE
;
2174 tree vtt
= NULL_TREE
;
2176 /* See if there's anything we need to do. */
2177 tmpl
= DECL_TI_TEMPLATE (decl
);
2178 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2179 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2180 if (TREE_PURPOSE (t
))
2185 old_type
= TREE_TYPE (decl
);
2186 spec_types
= TYPE_ARG_TYPES (old_type
);
2188 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2190 /* Remove the this pointer, but remember the object's type for
2192 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2193 spec_types
= TREE_CHAIN (spec_types
);
2194 tmpl_types
= TREE_CHAIN (tmpl_types
);
2196 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2198 /* DECL may contain more parameters than TMPL due to the extra
2199 in-charge parameter in constructors and destructors. */
2200 in_charge
= spec_types
;
2201 spec_types
= TREE_CHAIN (spec_types
);
2203 if (DECL_HAS_VTT_PARM_P (decl
))
2206 spec_types
= TREE_CHAIN (spec_types
);
2210 /* Compute the merged default arguments. */
2212 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2214 /* Compute the new FUNCTION_TYPE. */
2218 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2223 /* Put the in-charge parameter back. */
2224 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2225 TREE_VALUE (in_charge
),
2228 new_type
= build_method_type_directly (object_type
,
2229 TREE_TYPE (old_type
),
2233 new_type
= build_function_type (TREE_TYPE (old_type
),
2235 new_type
= cp_build_type_attribute_variant (new_type
,
2236 TYPE_ATTRIBUTES (old_type
));
2237 new_type
= build_exception_variant (new_type
,
2238 TYPE_RAISES_EXCEPTIONS (old_type
));
2239 TREE_TYPE (decl
) = new_type
;
2242 /* Return the number of template headers we expect to see for a definition
2243 or specialization of CTYPE or one of its non-template members. */
2246 num_template_headers_for_class (tree ctype
)
2248 int num_templates
= 0;
2250 while (ctype
&& CLASS_TYPE_P (ctype
))
2252 /* You're supposed to have one `template <...>' for every
2253 template class, but you don't need one for a full
2254 specialization. For example:
2256 template <class T> struct S{};
2257 template <> struct S<int> { void f(); };
2258 void S<int>::f () {}
2260 is correct; there shouldn't be a `template <>' for the
2261 definition of `S<int>::f'. */
2262 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2263 /* If CTYPE does not have template information of any
2264 kind, then it is not a template, nor is it nested
2265 within a template. */
2267 if (explicit_class_specialization_p (ctype
))
2269 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2272 ctype
= TYPE_CONTEXT (ctype
);
2275 return num_templates
;
2278 /* Do a simple sanity check on the template headers that precede the
2279 variable declaration DECL. */
2282 check_template_variable (tree decl
)
2284 tree ctx
= CP_DECL_CONTEXT (decl
);
2285 int wanted
= num_template_headers_for_class (ctx
);
2286 if (!TYPE_P (ctx
) || !CLASSTYPE_TEMPLATE_INFO (ctx
))
2287 permerror (DECL_SOURCE_LOCATION (decl
),
2288 "%qD is not a static data member of a class template", decl
);
2289 else if (template_header_count
> wanted
)
2291 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2292 "too many template headers for %D (should be %d)",
2294 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2295 inform (DECL_SOURCE_LOCATION (decl
),
2296 "members of an explicitly specialized class are defined "
2297 "without a template header");
2301 /* Check to see if the function just declared, as indicated in
2302 DECLARATOR, and in DECL, is a specialization of a function
2303 template. We may also discover that the declaration is an explicit
2304 instantiation at this point.
2306 Returns DECL, or an equivalent declaration that should be used
2307 instead if all goes well. Issues an error message if something is
2308 amiss. Returns error_mark_node if the error is not easily
2311 FLAGS is a bitmask consisting of the following flags:
2313 2: The function has a definition.
2314 4: The function is a friend.
2316 The TEMPLATE_COUNT is the number of references to qualifying
2317 template classes that appeared in the name of the function. For
2320 template <class T> struct S { void f(); };
2323 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2324 classes are not counted in the TEMPLATE_COUNT, so that in
2326 template <class T> struct S {};
2327 template <> struct S<int> { void f(); }
2328 template <> void S<int>::f();
2330 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2331 invalid; there should be no template <>.)
2333 If the function is a specialization, it is marked as such via
2334 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2335 is set up correctly, and it is added to the list of specializations
2336 for that template. */
2339 check_explicit_specialization (tree declarator
,
2344 int have_def
= flags
& 2;
2345 int is_friend
= flags
& 4;
2346 int specialization
= 0;
2347 int explicit_instantiation
= 0;
2348 int member_specialization
= 0;
2349 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2350 tree dname
= DECL_NAME (decl
);
2355 if (!processing_specialization
)
2358 tsk
= tsk_excessive_parms
;
2361 tsk
= current_tmpl_spec_kind (template_count
);
2366 if (processing_specialization
)
2369 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2371 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2374 /* This could be something like:
2376 template <class T> void f(T);
2377 class S { friend void f<>(int); } */
2381 /* This case handles bogus declarations like template <>
2382 template <class T> void f<int>(); */
2384 error ("template-id %qD in declaration of primary template",
2391 case tsk_invalid_member_spec
:
2392 /* The error has already been reported in
2393 check_specialization_scope. */
2394 return error_mark_node
;
2396 case tsk_invalid_expl_inst
:
2397 error ("template parameter list used in explicit instantiation");
2403 error ("definition provided for explicit instantiation");
2405 explicit_instantiation
= 1;
2408 case tsk_excessive_parms
:
2409 case tsk_insufficient_parms
:
2410 if (tsk
== tsk_excessive_parms
)
2411 error ("too many template parameter lists in declaration of %qD",
2413 else if (template_header_count
)
2414 error("too few template parameter lists in declaration of %qD", decl
);
2416 error("explicit specialization of %qD must be introduced by "
2417 "%<template <>%>", decl
);
2421 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2423 member_specialization
= 1;
2429 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2431 /* This case handles bogus declarations like template <>
2432 template <class T> void f<int>(); */
2434 if (uses_template_parms (declarator
))
2435 error ("function template partial specialization %qD "
2436 "is not allowed", declarator
);
2438 error ("template-id %qD in declaration of primary template",
2443 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2444 /* This is a specialization of a member template, without
2445 specialization the containing class. Something like:
2447 template <class T> struct S {
2448 template <class U> void f (U);
2450 template <> template <class U> void S<int>::f(U) {}
2452 That's a specialization -- but of the entire template. */
2460 if (specialization
|| member_specialization
)
2462 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2463 for (; t
; t
= TREE_CHAIN (t
))
2464 if (TREE_PURPOSE (t
))
2466 permerror (input_location
,
2467 "default argument specified in explicit specialization");
2472 if (specialization
|| member_specialization
|| explicit_instantiation
)
2474 tree tmpl
= NULL_TREE
;
2475 tree targs
= NULL_TREE
;
2477 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2478 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2482 gcc_assert (identifier_p (declarator
));
2487 /* If there is no class context, the explicit instantiation
2488 must be at namespace scope. */
2489 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2491 /* Find the namespace binding, using the declaration
2493 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2495 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2497 error ("%qD is not a template function", dname
);
2498 fns
= error_mark_node
;
2502 tree fn
= OVL_CURRENT (fns
);
2503 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2504 CP_DECL_CONTEXT (fn
)))
2505 error ("%qD is not declared in %qD",
2506 decl
, current_namespace
);
2510 declarator
= lookup_template_function (fns
, NULL_TREE
);
2513 if (declarator
== error_mark_node
)
2514 return error_mark_node
;
2516 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2518 if (!explicit_instantiation
)
2519 /* A specialization in class scope. This is invalid,
2520 but the error will already have been flagged by
2521 check_specialization_scope. */
2522 return error_mark_node
;
2525 /* It's not valid to write an explicit instantiation in
2528 class C { template void f(); }
2530 This case is caught by the parser. However, on
2533 template class C { void f(); };
2535 (which is invalid) we can get here. The error will be
2542 else if (ctype
!= NULL_TREE
2543 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2545 /* Find the list of functions in ctype that have the same
2546 name as the declared function. */
2547 tree name
= TREE_OPERAND (declarator
, 0);
2548 tree fns
= NULL_TREE
;
2551 if (constructor_name_p (name
, ctype
))
2553 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2555 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2556 : !CLASSTYPE_DESTRUCTORS (ctype
))
2558 /* From [temp.expl.spec]:
2560 If such an explicit specialization for the member
2561 of a class template names an implicitly-declared
2562 special member function (clause _special_), the
2563 program is ill-formed.
2565 Similar language is found in [temp.explicit]. */
2566 error ("specialization of implicitly-declared special member function");
2567 return error_mark_node
;
2570 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2573 if (!DECL_CONV_FN_P (decl
))
2575 idx
= lookup_fnfields_1 (ctype
, name
);
2577 fns
= (*CLASSTYPE_METHOD_VEC (ctype
))[idx
];
2581 vec
<tree
, va_gc
> *methods
;
2584 /* For a type-conversion operator, we cannot do a
2585 name-based lookup. We might be looking for `operator
2586 int' which will be a specialization of `operator T'.
2587 So, we find *all* the conversion operators, and then
2588 select from them. */
2591 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2593 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2594 methods
->iterate (idx
, &ovl
);
2597 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2598 /* There are no more conversion functions. */
2601 /* Glue all these conversion functions together
2602 with those we already have. */
2603 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2604 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2608 if (fns
== NULL_TREE
)
2610 error ("no member function %qD declared in %qT", name
, ctype
);
2611 return error_mark_node
;
2614 TREE_OPERAND (declarator
, 0) = fns
;
2617 /* Figure out what exactly is being specialized at this point.
2618 Note that for an explicit instantiation, even one for a
2619 member function, we cannot tell apriori whether the
2620 instantiation is for a member template, or just a member
2621 function of a template class. Even if a member template is
2622 being instantiated, the member template arguments may be
2623 elided if they can be deduced from the rest of the
2625 tmpl
= determine_specialization (declarator
, decl
,
2627 member_specialization
,
2631 if (!tmpl
|| tmpl
== error_mark_node
)
2632 /* We couldn't figure out what this declaration was
2634 return error_mark_node
;
2637 tree gen_tmpl
= most_general_template (tmpl
);
2639 if (explicit_instantiation
)
2641 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2642 is done by do_decl_instantiation later. */
2644 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2645 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2647 if (arg_depth
> parm_depth
)
2649 /* If TMPL is not the most general template (for
2650 example, if TMPL is a friend template that is
2651 injected into namespace scope), then there will
2652 be too many levels of TARGS. Remove some of them
2657 new_targs
= make_tree_vec (parm_depth
);
2658 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2659 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2660 = TREE_VEC_ELT (targs
, i
);
2664 return instantiate_template (tmpl
, targs
, tf_error
);
2667 /* If we thought that the DECL was a member function, but it
2668 turns out to be specializing a static member function,
2669 make DECL a static member function as well. */
2670 if (DECL_STATIC_FUNCTION_P (tmpl
)
2671 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2672 revert_static_member_fn (decl
);
2674 /* If this is a specialization of a member template of a
2675 template class, we want to return the TEMPLATE_DECL, not
2676 the specialization of it. */
2677 if (tsk
== tsk_template
)
2679 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2680 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2681 DECL_INITIAL (result
) = NULL_TREE
;
2685 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2686 DECL_SOURCE_LOCATION (result
)
2687 = DECL_SOURCE_LOCATION (decl
);
2688 /* We want to use the argument list specified in the
2689 definition, not in the original declaration. */
2690 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2691 for (parm
= DECL_ARGUMENTS (result
); parm
;
2692 parm
= DECL_CHAIN (parm
))
2693 DECL_CONTEXT (parm
) = result
;
2695 return register_specialization (tmpl
, gen_tmpl
, targs
,
2699 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2700 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2702 /* Inherit default function arguments from the template
2703 DECL is specializing. */
2704 copy_default_args_to_explicit_spec (decl
);
2706 /* This specialization has the same protection as the
2707 template it specializes. */
2708 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2709 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2711 /* 7.1.1-1 [dcl.stc]
2713 A storage-class-specifier shall not be specified in an
2714 explicit specialization...
2716 The parser rejects these, so unless action is taken here,
2717 explicit function specializations will always appear with
2720 The action recommended by the C++ CWG in response to C++
2721 defect report 605 is to make the storage class and linkage
2722 of the explicit specialization match the templated function:
2724 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2726 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2728 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2729 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2731 /* This specialization has the same linkage and visibility as
2732 the function template it specializes. */
2733 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2734 if (! TREE_PUBLIC (decl
))
2736 DECL_INTERFACE_KNOWN (decl
) = 1;
2737 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2739 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2740 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2742 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2743 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2747 /* If DECL is a friend declaration, declared using an
2748 unqualified name, the namespace associated with DECL may
2749 have been set incorrectly. For example, in:
2751 template <typename T> void f(T);
2753 struct S { friend void f<int>(int); }
2756 we will have set the DECL_CONTEXT for the friend
2757 declaration to N, rather than to the global namespace. */
2758 if (DECL_NAMESPACE_SCOPE_P (decl
))
2759 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2761 if (is_friend
&& !have_def
)
2762 /* This is not really a declaration of a specialization.
2763 It's just the name of an instantiation. But, it's not
2764 a request for an instantiation, either. */
2765 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2766 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2767 /* This is indeed a specialization. In case of constructors
2768 and destructors, we need in-charge and not-in-charge
2769 versions in V3 ABI. */
2770 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2772 /* Register this specialization so that we can find it
2774 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2781 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2782 parameters. These are represented in the same format used for
2783 DECL_TEMPLATE_PARMS. */
2786 comp_template_parms (const_tree parms1
, const_tree parms2
)
2791 if (parms1
== parms2
)
2794 for (p1
= parms1
, p2
= parms2
;
2795 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2796 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2798 tree t1
= TREE_VALUE (p1
);
2799 tree t2
= TREE_VALUE (p2
);
2802 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2803 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2805 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2808 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2810 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2811 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2813 /* If either of the template parameters are invalid, assume
2814 they match for the sake of error recovery. */
2815 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2818 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2821 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2822 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2823 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2825 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2830 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2831 /* One set of parameters has more parameters lists than the
2838 /* Determine whether PARM is a parameter pack. */
2841 template_parameter_pack_p (const_tree parm
)
2843 /* Determine if we have a non-type template parameter pack. */
2844 if (TREE_CODE (parm
) == PARM_DECL
)
2845 return (DECL_TEMPLATE_PARM_P (parm
)
2846 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2847 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
2848 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
2850 /* If this is a list of template parameters, we could get a
2851 TYPE_DECL or a TEMPLATE_DECL. */
2852 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2853 parm
= TREE_TYPE (parm
);
2855 /* Otherwise it must be a type template parameter. */
2856 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2857 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2858 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2861 /* Determine if T is a function parameter pack. */
2864 function_parameter_pack_p (const_tree t
)
2866 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2867 return DECL_PACK_P (t
);
2871 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2872 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2875 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2877 if (! primary_func_tmpl_inst
2878 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2879 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2882 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2885 /* Return true iff the function parameter PARAM_DECL was expanded
2886 from the function parameter pack PACK. */
2889 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
2891 if (DECL_ARTIFICIAL (param_decl
)
2892 || !function_parameter_pack_p (pack
))
2895 /* The parameter pack and its pack arguments have the same
2897 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
2900 /* Determine whether ARGS describes a variadic template args list,
2901 i.e., one that is terminated by a template argument pack. */
2904 template_args_variadic_p (tree args
)
2909 if (args
== NULL_TREE
)
2912 args
= INNERMOST_TEMPLATE_ARGS (args
);
2913 nargs
= TREE_VEC_LENGTH (args
);
2918 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2920 return ARGUMENT_PACK_P (last_parm
);
2923 /* Generate a new name for the parameter pack name NAME (an
2924 IDENTIFIER_NODE) that incorporates its */
2927 make_ith_pack_parameter_name (tree name
, int i
)
2929 /* Munge the name to include the parameter index. */
2930 #define NUMBUF_LEN 128
2931 char numbuf
[NUMBUF_LEN
];
2935 if (name
== NULL_TREE
)
2937 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
2938 newname_len
= IDENTIFIER_LENGTH (name
)
2939 + strlen (numbuf
) + 2;
2940 newname
= (char*)alloca (newname_len
);
2941 snprintf (newname
, newname_len
,
2942 "%s#%i", IDENTIFIER_POINTER (name
), i
);
2943 return get_identifier (newname
);
2946 /* Return true if T is a primary function, class or alias template
2950 primary_template_instantiation_p (const_tree t
)
2955 if (TREE_CODE (t
) == FUNCTION_DECL
)
2956 return DECL_LANG_SPECIFIC (t
)
2957 && DECL_TEMPLATE_INSTANTIATION (t
)
2958 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
2959 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
2960 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
2961 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
2962 else if (alias_template_specialization_p (t
))
2967 /* Return true if PARM is a template template parameter. */
2970 template_template_parameter_p (const_tree parm
)
2972 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
2975 /* Return true iff PARM is a DECL representing a type template
2979 template_type_parameter_p (const_tree parm
)
2982 && (TREE_CODE (parm
) == TYPE_DECL
2983 || TREE_CODE (parm
) == TEMPLATE_DECL
)
2984 && DECL_TEMPLATE_PARM_P (parm
));
2987 /* Return the template parameters of T if T is a
2988 primary template instantiation, NULL otherwise. */
2991 get_primary_template_innermost_parameters (const_tree t
)
2993 tree parms
= NULL
, template_info
= NULL
;
2995 if ((template_info
= get_template_info (t
))
2996 && primary_template_instantiation_p (t
))
2997 parms
= INNERMOST_TEMPLATE_PARMS
2998 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3003 /* Return the template parameters of the LEVELth level from the full list
3004 of template parameters PARMS. */
3007 get_template_parms_at_level (tree parms
, int level
)
3011 || TREE_CODE (parms
) != TREE_LIST
3012 || level
> TMPL_PARMS_DEPTH (parms
))
3015 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3016 if (TMPL_PARMS_DEPTH (p
) == level
)
3022 /* Returns the template arguments of T if T is a template instantiation,
3026 get_template_innermost_arguments (const_tree t
)
3028 tree args
= NULL
, template_info
= NULL
;
3030 if ((template_info
= get_template_info (t
))
3031 && TI_ARGS (template_info
))
3032 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3037 /* Return the argument pack elements of T if T is a template argument pack,
3041 get_template_argument_pack_elems (const_tree t
)
3043 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3044 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3047 return ARGUMENT_PACK_ARGS (t
);
3050 /* Structure used to track the progress of find_parameter_packs_r. */
3051 struct find_parameter_pack_data
3053 /* TREE_LIST that will contain all of the parameter packs found by
3055 tree
* parameter_packs
;
3057 /* Set of AST nodes that have been visited by the traversal. */
3058 struct pointer_set_t
*visited
;
3061 /* Identifies all of the argument packs that occur in a template
3062 argument and appends them to the TREE_LIST inside DATA, which is a
3063 find_parameter_pack_data structure. This is a subroutine of
3064 make_pack_expansion and uses_parameter_packs. */
3066 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3069 struct find_parameter_pack_data
* ppd
=
3070 (struct find_parameter_pack_data
*)data
;
3071 bool parameter_pack_p
= false;
3073 /* Handle type aliases/typedefs. */
3074 if (TYPE_ALIAS_P (t
))
3076 if (TYPE_TEMPLATE_INFO (t
))
3077 cp_walk_tree (&TYPE_TI_ARGS (t
),
3078 &find_parameter_packs_r
,
3084 /* Identify whether this is a parameter pack or not. */
3085 switch (TREE_CODE (t
))
3087 case TEMPLATE_PARM_INDEX
:
3088 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3089 parameter_pack_p
= true;
3092 case TEMPLATE_TYPE_PARM
:
3093 t
= TYPE_MAIN_VARIANT (t
);
3094 case TEMPLATE_TEMPLATE_PARM
:
3095 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3096 parameter_pack_p
= true;
3101 if (DECL_PACK_P (t
))
3103 /* We don't want to walk into the type of a PARM_DECL,
3104 because we don't want to see the type parameter pack. */
3106 parameter_pack_p
= true;
3110 /* Look through a lambda capture proxy to the field pack. */
3112 if (DECL_HAS_VALUE_EXPR_P (t
))
3114 tree v
= DECL_VALUE_EXPR (t
);
3116 &find_parameter_packs_r
,
3123 parameter_pack_p
= true;
3126 /* Not a parameter pack. */
3130 if (parameter_pack_p
)
3132 /* Add this parameter pack to the list. */
3133 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3137 cp_walk_tree (&TYPE_CONTEXT (t
),
3138 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3140 /* This switch statement will return immediately if we don't find a
3142 switch (TREE_CODE (t
))
3144 case TEMPLATE_PARM_INDEX
:
3147 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3148 /* Check the template itself. */
3149 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3150 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3151 /* Check the template arguments. */
3152 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3157 case TEMPLATE_TYPE_PARM
:
3158 case TEMPLATE_TEMPLATE_PARM
:
3165 if (TYPE_PTRMEMFUNC_P (t
))
3171 if (TYPE_TEMPLATE_INFO (t
))
3172 cp_walk_tree (&TYPE_TI_ARGS (t
),
3173 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3180 cp_walk_tree (&TREE_TYPE (t
),
3181 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3185 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3190 case TYPE_PACK_EXPANSION
:
3191 case EXPR_PACK_EXPANSION
:
3196 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3201 case IDENTIFIER_NODE
:
3202 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3214 /* Determines if the expression or type T uses any parameter packs. */
3216 uses_parameter_packs (tree t
)
3218 tree parameter_packs
= NULL_TREE
;
3219 struct find_parameter_pack_data ppd
;
3220 ppd
.parameter_packs
= ¶meter_packs
;
3221 ppd
.visited
= pointer_set_create ();
3222 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3223 pointer_set_destroy (ppd
.visited
);
3224 return parameter_packs
!= NULL_TREE
;
3227 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3228 representation a base-class initializer into a parameter pack
3229 expansion. If all goes well, the resulting node will be an
3230 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3233 make_pack_expansion (tree arg
)
3236 tree parameter_packs
= NULL_TREE
;
3237 bool for_types
= false;
3238 struct find_parameter_pack_data ppd
;
3240 if (!arg
|| arg
== error_mark_node
)
3243 if (TREE_CODE (arg
) == TREE_LIST
)
3245 /* The only time we will see a TREE_LIST here is for a base
3246 class initializer. In this case, the TREE_PURPOSE will be a
3247 _TYPE node (representing the base class expansion we're
3248 initializing) and the TREE_VALUE will be a TREE_LIST
3249 containing the initialization arguments.
3251 The resulting expansion looks somewhat different from most
3252 expansions. Rather than returning just one _EXPANSION, we
3253 return a TREE_LIST whose TREE_PURPOSE is a
3254 TYPE_PACK_EXPANSION containing the bases that will be
3255 initialized. The TREE_VALUE will be identical to the
3256 original TREE_VALUE, which is a list of arguments that will
3257 be passed to each base. We do not introduce any new pack
3258 expansion nodes into the TREE_VALUE (although it is possible
3259 that some already exist), because the TREE_PURPOSE and
3260 TREE_VALUE all need to be expanded together with the same
3261 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3262 resulting TREE_PURPOSE will mention the parameter packs in
3263 both the bases and the arguments to the bases. */
3266 tree parameter_packs
= NULL_TREE
;
3268 /* Determine which parameter packs will be used by the base
3270 ppd
.visited
= pointer_set_create ();
3271 ppd
.parameter_packs
= ¶meter_packs
;
3272 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3275 if (parameter_packs
== NULL_TREE
)
3277 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3278 pointer_set_destroy (ppd
.visited
);
3279 return error_mark_node
;
3282 if (TREE_VALUE (arg
) != void_type_node
)
3284 /* Collect the sets of parameter packs used in each of the
3285 initialization arguments. */
3286 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3288 /* Determine which parameter packs will be expanded in this
3290 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3295 pointer_set_destroy (ppd
.visited
);
3297 /* Create the pack expansion type for the base type. */
3298 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3299 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3300 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3302 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3303 they will rarely be compared to anything. */
3304 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3306 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3309 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3312 /* Build the PACK_EXPANSION_* node. */
3314 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3315 : make_node (EXPR_PACK_EXPANSION
);
3316 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3317 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3319 /* Propagate type and const-expression information. */
3320 TREE_TYPE (result
) = TREE_TYPE (arg
);
3321 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3324 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3325 they will rarely be compared to anything. */
3326 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3328 /* Determine which parameter packs will be expanded. */
3329 ppd
.parameter_packs
= ¶meter_packs
;
3330 ppd
.visited
= pointer_set_create ();
3331 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3332 pointer_set_destroy (ppd
.visited
);
3334 /* Make sure we found some parameter packs. */
3335 if (parameter_packs
== NULL_TREE
)
3338 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3340 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3341 return error_mark_node
;
3343 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3345 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3350 /* Checks T for any "bare" parameter packs, which have not yet been
3351 expanded, and issues an error if any are found. This operation can
3352 only be done on full expressions or types (e.g., an expression
3353 statement, "if" condition, etc.), because we could have expressions like:
3355 foo(f(g(h(args)))...)
3357 where "args" is a parameter pack. check_for_bare_parameter_packs
3358 should not be called for the subexpressions args, h(args),
3359 g(h(args)), or f(g(h(args))), because we would produce erroneous
3362 Returns TRUE and emits an error if there were bare parameter packs,
3363 returns FALSE otherwise. */
3365 check_for_bare_parameter_packs (tree t
)
3367 tree parameter_packs
= NULL_TREE
;
3368 struct find_parameter_pack_data ppd
;
3370 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3373 if (TREE_CODE (t
) == TYPE_DECL
)
3376 ppd
.parameter_packs
= ¶meter_packs
;
3377 ppd
.visited
= pointer_set_create ();
3378 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3379 pointer_set_destroy (ppd
.visited
);
3381 if (parameter_packs
)
3383 error ("parameter packs not expanded with %<...%>:");
3384 while (parameter_packs
)
3386 tree pack
= TREE_VALUE (parameter_packs
);
3387 tree name
= NULL_TREE
;
3389 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3390 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3391 name
= TYPE_NAME (pack
);
3392 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3393 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3395 name
= DECL_NAME (pack
);
3398 inform (input_location
, " %qD", name
);
3400 inform (input_location
, " <anonymous>");
3402 parameter_packs
= TREE_CHAIN (parameter_packs
);
3411 /* Expand any parameter packs that occur in the template arguments in
3414 expand_template_argument_pack (tree args
)
3416 tree result_args
= NULL_TREE
;
3417 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3418 int num_result_args
= -1;
3419 int non_default_args_count
= -1;
3421 /* First, determine if we need to expand anything, and the number of
3422 slots we'll need. */
3423 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3425 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3426 if (arg
== NULL_TREE
)
3428 if (ARGUMENT_PACK_P (arg
))
3430 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3431 if (num_result_args
< 0)
3432 num_result_args
= in_arg
+ num_packed
;
3434 num_result_args
+= num_packed
;
3438 if (num_result_args
>= 0)
3443 /* If no expansion is necessary, we're done. */
3444 if (num_result_args
< 0)
3447 /* Expand arguments. */
3448 result_args
= make_tree_vec (num_result_args
);
3449 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3450 non_default_args_count
=
3451 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3452 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3454 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3455 if (ARGUMENT_PACK_P (arg
))
3457 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3458 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3459 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3460 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3461 if (non_default_args_count
> 0)
3462 non_default_args_count
+= num_packed
;
3466 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3470 if (non_default_args_count
>= 0)
3471 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3475 /* Checks if DECL shadows a template parameter.
3477 [temp.local]: A template-parameter shall not be redeclared within its
3478 scope (including nested scopes).
3480 Emits an error and returns TRUE if the DECL shadows a parameter,
3481 returns FALSE otherwise. */
3484 check_template_shadow (tree decl
)
3488 /* If we're not in a template, we can't possibly shadow a template
3490 if (!current_template_parms
)
3493 /* Figure out what we're shadowing. */
3494 if (TREE_CODE (decl
) == OVERLOAD
)
3495 decl
= OVL_CURRENT (decl
);
3496 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3498 /* If there's no previous binding for this name, we're not shadowing
3499 anything, let alone a template parameter. */
3503 /* If we're not shadowing a template parameter, we're done. Note
3504 that OLDDECL might be an OVERLOAD (or perhaps even an
3505 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3507 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3510 /* We check for decl != olddecl to avoid bogus errors for using a
3511 name inside a class. We check TPFI to avoid duplicate errors for
3512 inline member templates. */
3514 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
3517 error ("declaration of %q+#D", decl
);
3518 error (" shadows template parm %q+#D", olddecl
);
3522 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3523 ORIG_LEVEL, DECL, and TYPE. */
3526 build_template_parm_index (int index
,
3532 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3533 TEMPLATE_PARM_IDX (t
) = index
;
3534 TEMPLATE_PARM_LEVEL (t
) = level
;
3535 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3536 TEMPLATE_PARM_DECL (t
) = decl
;
3537 TREE_TYPE (t
) = type
;
3538 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3539 TREE_READONLY (t
) = TREE_READONLY (decl
);
3544 /* Find the canonical type parameter for the given template type
3545 parameter. Returns the canonical type parameter, which may be TYPE
3546 if no such parameter existed. */
3549 canonical_type_parameter (tree type
)
3552 int idx
= TEMPLATE_TYPE_IDX (type
);
3553 if (!canonical_template_parms
)
3554 vec_alloc (canonical_template_parms
, idx
+1);
3556 while (canonical_template_parms
->length () <= (unsigned)idx
)
3557 vec_safe_push (canonical_template_parms
, NULL_TREE
);
3559 list
= (*canonical_template_parms
)[idx
];
3560 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3561 list
= TREE_CHAIN (list
);
3564 return TREE_VALUE (list
);
3567 (*canonical_template_parms
)[idx
]
3568 = tree_cons (NULL_TREE
, type
,
3569 (*canonical_template_parms
)[idx
]);
3574 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3575 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3576 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3577 new one is created. */
3580 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3581 tsubst_flags_t complain
)
3583 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3584 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3585 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3586 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3588 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3591 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3592 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3593 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3594 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3595 DECL_ARTIFICIAL (decl
) = 1;
3596 SET_DECL_TEMPLATE_PARM_P (decl
);
3598 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3599 TEMPLATE_PARM_LEVEL (index
) - levels
,
3600 TEMPLATE_PARM_ORIG_LEVEL (index
),
3602 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3603 TEMPLATE_PARM_PARAMETER_PACK (t
)
3604 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3606 /* Template template parameters need this. */
3607 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3608 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3609 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3613 return TEMPLATE_PARM_DESCENDANTS (index
);
3616 /* Process information from new template parameter PARM and append it
3617 to the LIST being built. This new parameter is a non-type
3618 parameter iff IS_NON_TYPE is true. This new parameter is a
3619 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3620 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3621 parameter list PARM belongs to. This is used used to create a
3622 proper canonical type for the type of PARM that is to be created,
3623 iff PARM is a type. If the size is not known, this parameter shall
3627 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
3628 bool is_non_type
, bool is_parameter_pack
)
3635 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3636 defval
= TREE_PURPOSE (parm
);
3640 tree p
= tree_last (list
);
3642 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3645 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3646 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3648 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3658 parm
= TREE_VALUE (parm
);
3660 SET_DECL_TEMPLATE_PARM_P (parm
);
3662 if (TREE_TYPE (parm
) == error_mark_node
)
3664 err_parm_list
= build_tree_list (defval
, parm
);
3665 TREE_VALUE (err_parm_list
) = error_mark_node
;
3666 return chainon (list
, err_parm_list
);
3672 The top-level cv-qualifiers on the template-parameter are
3673 ignored when determining its type. */
3674 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3675 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3677 err_parm_list
= build_tree_list (defval
, parm
);
3678 TREE_VALUE (err_parm_list
) = error_mark_node
;
3679 return chainon (list
, err_parm_list
);
3682 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
)
3684 /* This template parameter is not a parameter pack, but it
3685 should be. Complain about "bare" parameter packs. */
3686 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3688 /* Recover by calling this a parameter pack. */
3689 is_parameter_pack
= true;
3693 /* A template parameter is not modifiable. */
3694 TREE_CONSTANT (parm
) = 1;
3695 TREE_READONLY (parm
) = 1;
3696 decl
= build_decl (parm_loc
,
3697 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3698 TREE_CONSTANT (decl
) = 1;
3699 TREE_READONLY (decl
) = 1;
3700 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3701 = build_template_parm_index (idx
, processing_template_decl
,
3702 processing_template_decl
,
3703 decl
, TREE_TYPE (parm
));
3705 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3706 = is_parameter_pack
;
3711 parm
= TREE_VALUE (TREE_VALUE (parm
));
3713 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3715 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3716 /* This is for distinguishing between real templates and template
3717 template parameters */
3718 TREE_TYPE (parm
) = t
;
3719 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3724 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3725 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3726 decl
= build_decl (parm_loc
,
3727 TYPE_DECL
, parm
, t
);
3730 TYPE_NAME (t
) = decl
;
3731 TYPE_STUB_DECL (t
) = decl
;
3733 TEMPLATE_TYPE_PARM_INDEX (t
)
3734 = build_template_parm_index (idx
, processing_template_decl
,
3735 processing_template_decl
,
3736 decl
, TREE_TYPE (parm
));
3737 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3738 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3740 DECL_ARTIFICIAL (decl
) = 1;
3741 SET_DECL_TEMPLATE_PARM_P (decl
);
3743 parm
= build_tree_list (defval
, parm
);
3744 return chainon (list
, parm
);
3747 /* The end of a template parameter list has been reached. Process the
3748 tree list into a parameter vector, converting each parameter into a more
3749 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3753 end_template_parm_list (tree parms
)
3757 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3759 current_template_parms
3760 = tree_cons (size_int (processing_template_decl
),
3761 saved_parmlist
, current_template_parms
);
3763 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3765 next
= TREE_CHAIN (parm
);
3766 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3767 TREE_CHAIN (parm
) = NULL_TREE
;
3770 --processing_template_parmlist
;
3772 return saved_parmlist
;
3775 /* end_template_decl is called after a template declaration is seen. */
3778 end_template_decl (void)
3780 reset_specialization ();
3782 if (! processing_template_decl
)
3785 /* This matches the pushlevel in begin_template_parm_list. */
3788 --processing_template_decl
;
3789 current_template_parms
= TREE_CHAIN (current_template_parms
);
3792 /* Takes a TREE_LIST representing a template parameter and convert it
3793 into an argument suitable to be passed to the type substitution
3794 functions. Note that If the TREE_LIST contains an error_mark
3795 node, the returned argument is error_mark_node. */
3798 template_parm_to_arg (tree t
)
3802 || TREE_CODE (t
) != TREE_LIST
)
3805 if (error_operand_p (TREE_VALUE (t
)))
3806 return error_mark_node
;
3810 if (TREE_CODE (t
) == TYPE_DECL
3811 || TREE_CODE (t
) == TEMPLATE_DECL
)
3815 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3817 /* Turn this argument into a TYPE_ARGUMENT_PACK
3818 with a single element, which expands T. */
3819 tree vec
= make_tree_vec (1);
3820 #ifdef ENABLE_CHECKING
3821 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3822 (vec
, TREE_VEC_LENGTH (vec
));
3824 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3826 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
3827 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3832 t
= DECL_INITIAL (t
);
3834 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3836 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3837 with a single element, which expands T. */
3838 tree vec
= make_tree_vec (1);
3839 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3840 #ifdef ENABLE_CHECKING
3841 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3842 (vec
, TREE_VEC_LENGTH (vec
));
3844 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3846 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3847 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3848 TREE_TYPE (t
) = type
;
3854 /* Given a set of template parameters, return them as a set of template
3855 arguments. The template parameters are represented as a TREE_VEC, in
3856 the form documented in cp-tree.h for template arguments. */
3859 template_parms_to_args (tree parms
)
3862 tree args
= NULL_TREE
;
3863 int length
= TMPL_PARMS_DEPTH (parms
);
3866 /* If there is only one level of template parameters, we do not
3867 create a TREE_VEC of TREE_VECs. Instead, we return a single
3868 TREE_VEC containing the arguments. */
3870 args
= make_tree_vec (length
);
3872 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
3874 tree a
= copy_node (TREE_VALUE (header
));
3877 TREE_TYPE (a
) = NULL_TREE
;
3878 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
3879 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
3881 #ifdef ENABLE_CHECKING
3882 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
3886 TREE_VEC_ELT (args
, --l
) = a
;
3891 if (length
> 1 && TREE_VEC_ELT (args
, 0) == NULL_TREE
)
3892 /* This can happen for template parms of a template template
3895 template<template<class T, class U> class TT> struct S;
3897 Consider the level of the parms of TT; T and U both have
3898 level 2; TT has no template parm of level 1. So in this case
3899 the first element of full_template_args is NULL_TREE. If we
3900 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
3901 of 2. This will make tsubst wrongly consider that T and U
3902 have level 1. Instead, let's create a dummy vector as the
3903 first element of full_template_args so that TMPL_ARGS_DEPTH
3904 returns the correct depth for args. */
3905 TREE_VEC_ELT (args
, 0) = make_tree_vec (1);
3909 /* Within the declaration of a template, return the currently active
3910 template parameters as an argument TREE_VEC. */
3913 current_template_args (void)
3915 return template_parms_to_args (current_template_parms
);
3918 /* Update the declared TYPE by doing any lookups which were thought to be
3919 dependent, but are not now that we know the SCOPE of the declarator. */
3922 maybe_update_decl_type (tree orig_type
, tree scope
)
3924 tree type
= orig_type
;
3926 if (type
== NULL_TREE
)
3929 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3930 type
= TREE_TYPE (type
);
3932 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
3933 && dependent_type_p (type
)
3934 /* Don't bother building up the args in this case. */
3935 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
3937 /* tsubst in the args corresponding to the template parameters,
3938 including auto if present. Most things will be unchanged, but
3939 make_typename_type and tsubst_qualified_id will resolve
3940 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3941 tree args
= current_template_args ();
3942 tree auto_node
= type_uses_auto (type
);
3946 tree auto_vec
= make_tree_vec (1);
3947 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
3948 args
= add_to_template_args (args
, auto_vec
);
3950 pushed
= push_scope (scope
);
3951 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
3956 if (type
== error_mark_node
)
3959 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3961 if (same_type_p (type
, TREE_TYPE (orig_type
)))
3964 type
= TYPE_NAME (type
);
3969 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3970 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3971 a member template. Used by push_template_decl below. */
3974 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
3976 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
3977 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
3978 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
3979 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3980 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
3985 struct template_parm_data
3987 /* The level of the template parameters we are currently
3991 /* The index of the specialization argument we are currently
3995 /* An array whose size is the number of template parameters. The
3996 elements are nonzero if the parameter has been used in any one
3997 of the arguments processed so far. */
4000 /* An array whose size is the number of template arguments. The
4001 elements are nonzero if the argument makes use of template
4002 parameters of this level. */
4003 int* arg_uses_template_parms
;
4006 /* Subroutine of push_template_decl used to see if each template
4007 parameter in a partial specialization is used in the explicit
4008 argument list. If T is of the LEVEL given in DATA (which is
4009 treated as a template_parm_data*), then DATA->PARMS is marked
4013 mark_template_parm (tree t
, void* data
)
4017 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4019 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4021 level
= TEMPLATE_PARM_LEVEL (t
);
4022 idx
= TEMPLATE_PARM_IDX (t
);
4026 level
= TEMPLATE_TYPE_LEVEL (t
);
4027 idx
= TEMPLATE_TYPE_IDX (t
);
4030 if (level
== tpd
->level
)
4032 tpd
->parms
[idx
] = 1;
4033 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4036 /* Return zero so that for_each_template_parm will continue the
4037 traversal of the tree; we want to mark *every* template parm. */
4041 /* Process the partial specialization DECL. */
4044 process_partial_specialization (tree decl
)
4046 tree type
= TREE_TYPE (decl
);
4047 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
4048 tree specargs
= CLASSTYPE_TI_ARGS (type
);
4049 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4050 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4053 int nargs
= TREE_VEC_LENGTH (inner_args
);
4056 bool did_error_intro
= false;
4057 struct template_parm_data tpd
;
4058 struct template_parm_data tpd2
;
4060 gcc_assert (current_template_parms
);
4062 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4063 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4065 /* We check that each of the template parameters given in the
4066 partial specialization is used in the argument list to the
4067 specialization. For example:
4069 template <class T> struct S;
4070 template <class T> struct S<T*>;
4072 The second declaration is OK because `T*' uses the template
4073 parameter T, whereas
4075 template <class T> struct S<int>;
4077 is no good. Even trickier is:
4088 The S2<T> declaration is actually invalid; it is a
4089 full-specialization. Of course,
4092 struct S2<T (*)(U)>;
4094 or some such would have been OK. */
4095 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4096 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4097 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4099 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4100 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4101 for (i
= 0; i
< nargs
; ++i
)
4103 tpd
.current_arg
= i
;
4104 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4105 &mark_template_parm
,
4108 /*include_nondeduced_p=*/false);
4110 for (i
= 0; i
< ntparms
; ++i
)
4111 if (tpd
.parms
[i
] == 0)
4113 /* One of the template parms was not used in the
4115 if (!did_error_intro
)
4117 error ("template parameters not used in partial specialization:");
4118 did_error_intro
= true;
4121 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4124 if (did_error_intro
)
4125 return error_mark_node
;
4127 /* [temp.class.spec]
4129 The argument list of the specialization shall not be identical to
4130 the implicit argument list of the primary template. */
4131 if (comp_template_args
4133 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4135 error ("partial specialization %qT does not specialize any template arguments", type
);
4137 /* A partial specialization that replaces multiple parameters of the
4138 primary template with a pack expansion is less specialized for those
4140 if (nargs
< DECL_NTPARMS (maintmpl
))
4142 error ("partial specialization is not more specialized than the "
4143 "primary template because it replaces multiple parameters "
4144 "with a pack expansion");
4145 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4149 /* [temp.class.spec]
4151 A partially specialized non-type argument expression shall not
4152 involve template parameters of the partial specialization except
4153 when the argument expression is a simple identifier.
4155 The type of a template parameter corresponding to a specialized
4156 non-type argument shall not be dependent on a parameter of the
4159 Also, we verify that pack expansions only occur at the
4160 end of the argument list. */
4161 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4163 for (i
= 0; i
< nargs
; ++i
)
4165 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4166 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4167 tree packed_args
= NULL_TREE
;
4170 if (ARGUMENT_PACK_P (arg
))
4172 /* Extract the arguments from the argument pack. We'll be
4173 iterating over these in the following loop. */
4174 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4175 len
= TREE_VEC_LENGTH (packed_args
);
4178 for (j
= 0; j
< len
; j
++)
4181 /* Get the Jth argument in the parameter pack. */
4182 arg
= TREE_VEC_ELT (packed_args
, j
);
4184 if (PACK_EXPANSION_P (arg
))
4186 /* Pack expansions must come at the end of the
4188 if ((packed_args
&& j
< len
- 1)
4189 || (!packed_args
&& i
< nargs
- 1))
4191 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4192 error ("parameter pack argument %qE must be at the "
4193 "end of the template argument list", arg
);
4195 error ("parameter pack argument %qT must be at the "
4196 "end of the template argument list", arg
);
4200 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4201 /* We only care about the pattern. */
4202 arg
= PACK_EXPANSION_PATTERN (arg
);
4204 if (/* These first two lines are the `non-type' bit. */
4206 && TREE_CODE (arg
) != TEMPLATE_DECL
4207 /* This next line is the `argument expression is not just a
4208 simple identifier' condition and also the `specialized
4209 non-type argument' bit. */
4210 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
4212 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4213 || (packed_args
&& uses_template_parms (arg
)))
4214 error ("template argument %qE involves template parameter(s)",
4218 /* Look at the corresponding template parameter,
4219 marking which template parameters its type depends
4221 tree type
= TREE_TYPE (parm
);
4225 /* We haven't yet initialized TPD2. Do so now. */
4226 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4227 /* The number of parameters here is the number in the
4228 main template, which, as checked in the assertion
4230 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4232 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4235 /* Mark the template parameters. But this time, we're
4236 looking for the template parameters of the main
4237 template, not in the specialization. */
4238 tpd2
.current_arg
= i
;
4239 tpd2
.arg_uses_template_parms
[i
] = 0;
4240 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4241 for_each_template_parm (type
,
4242 &mark_template_parm
,
4245 /*include_nondeduced_p=*/false);
4247 if (tpd2
.arg_uses_template_parms
[i
])
4249 /* The type depended on some template parameters.
4250 If they are fully specialized in the
4251 specialization, that's OK. */
4254 for (j
= 0; j
< nargs
; ++j
)
4255 if (tpd2
.parms
[j
] != 0
4256 && tpd
.arg_uses_template_parms
[j
])
4259 error_n (input_location
, count
,
4260 "type %qT of template argument %qE depends "
4261 "on a template parameter",
4262 "type %qT of template argument %qE depends "
4263 "on template parameters",
4272 /* We should only get here once. */
4273 gcc_assert (!COMPLETE_TYPE_P (type
));
4275 tree tmpl
= build_template_decl (decl
, current_template_parms
,
4276 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4277 TREE_TYPE (tmpl
) = type
;
4278 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4279 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4280 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4281 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4283 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4284 = tree_cons (specargs
, tmpl
,
4285 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4286 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4288 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4289 inst
= TREE_CHAIN (inst
))
4291 tree inst_type
= TREE_VALUE (inst
);
4292 if (COMPLETE_TYPE_P (inst_type
)
4293 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type
))
4295 tree spec
= most_specialized_class (inst_type
, maintmpl
, tf_none
);
4296 if (spec
&& TREE_TYPE (spec
) == type
)
4297 permerror (input_location
,
4298 "partial specialization of %qT after instantiation "
4299 "of %qT", type
, inst_type
);
4306 /* Check that a template declaration's use of default arguments and
4307 parameter packs is not invalid. Here, PARMS are the template
4308 parameters. IS_PRIMARY is true if DECL is the thing declared by
4309 a primary template. IS_PARTIAL is true if DECL is a partial
4312 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4313 declaration (but not a definition); 1 indicates a declaration, 2
4314 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4315 emitted for extraneous default arguments.
4317 Returns TRUE if there were no errors found, FALSE otherwise. */
4320 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4321 bool is_partial
, int is_friend_decl
)
4324 int last_level_to_check
;
4326 bool no_errors
= true;
4330 A default template-argument shall not be specified in a
4331 function template declaration or a function template definition, nor
4332 in the template-parameter-list of the definition of a member of a
4335 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
4336 /* You can't have a function template declaration in a local
4337 scope, nor you can you define a member of a class template in a
4341 if (TREE_CODE (decl
) == TYPE_DECL
4343 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4344 /* A lambda doesn't have an explicit declaration; don't complain
4345 about the parms of the enclosing class. */
4348 if (current_class_type
4349 && !TYPE_BEING_DEFINED (current_class_type
)
4350 && DECL_LANG_SPECIFIC (decl
)
4351 && DECL_DECLARES_FUNCTION_P (decl
)
4352 /* If this is either a friend defined in the scope of the class
4353 or a member function. */
4354 && (DECL_FUNCTION_MEMBER_P (decl
)
4355 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4356 : DECL_FRIEND_CONTEXT (decl
)
4357 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4359 /* And, if it was a member function, it really was defined in
4360 the scope of the class. */
4361 && (!DECL_FUNCTION_MEMBER_P (decl
)
4362 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4363 /* We already checked these parameters when the template was
4364 declared, so there's no need to do it again now. This function
4365 was defined in class scope, but we're processing its body now
4366 that the class is complete. */
4369 /* Core issue 226 (C++0x only): the following only applies to class
4372 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
4376 If a template-parameter has a default template-argument, all
4377 subsequent template-parameters shall have a default
4378 template-argument supplied. */
4379 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4381 tree inner_parms
= TREE_VALUE (parm_level
);
4382 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4383 int seen_def_arg_p
= 0;
4386 for (i
= 0; i
< ntparms
; ++i
)
4388 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4390 if (parm
== error_mark_node
)
4393 if (TREE_PURPOSE (parm
))
4395 else if (seen_def_arg_p
4396 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4398 error ("no default argument for %qD", TREE_VALUE (parm
));
4399 /* For better subsequent error-recovery, we indicate that
4400 there should have been a default argument. */
4401 TREE_PURPOSE (parm
) = error_mark_node
;
4404 else if (!is_partial
4406 /* Don't complain about an enclosing partial
4408 && parm_level
== parms
4409 && TREE_CODE (decl
) == TYPE_DECL
4411 && template_parameter_pack_p (TREE_VALUE (parm
)))
4413 /* A primary class template can only have one
4414 parameter pack, at the end of the template
4417 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
4418 error ("parameter pack %qE must be at the end of the"
4419 " template parameter list", TREE_VALUE (parm
));
4421 error ("parameter pack %qT must be at the end of the"
4422 " template parameter list",
4423 TREE_TYPE (TREE_VALUE (parm
)));
4425 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4433 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4437 /* For an ordinary class template, default template arguments are
4438 allowed at the innermost level, e.g.:
4439 template <class T = int>
4441 but, in a partial specialization, they're not allowed even
4442 there, as we have in [temp.class.spec]:
4444 The template parameter list of a specialization shall not
4445 contain default template argument values.
4447 So, for a partial specialization, or for a function template
4448 (in C++98/C++03), we look at all of them. */
4451 /* But, for a primary class template that is not a partial
4452 specialization we look at all template parameters except the
4454 parms
= TREE_CHAIN (parms
);
4456 /* Figure out what error message to issue. */
4457 if (is_friend_decl
== 2)
4458 msg
= G_("default template arguments may not be used in function template "
4459 "friend re-declaration");
4460 else if (is_friend_decl
)
4461 msg
= G_("default template arguments may not be used in function template "
4462 "friend declarations");
4463 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4464 msg
= G_("default template arguments may not be used in function templates "
4465 "without -std=c++11 or -std=gnu++11");
4466 else if (is_partial
)
4467 msg
= G_("default template arguments may not be used in "
4468 "partial specializations");
4470 msg
= G_("default argument for template parameter for class enclosing %qD");
4472 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4473 /* If we're inside a class definition, there's no need to
4474 examine the parameters to the class itself. On the one
4475 hand, they will be checked when the class is defined, and,
4476 on the other, default arguments are valid in things like:
4477 template <class T = double>
4478 struct S { template <class U> void f(U); };
4479 Here the default argument for `S' has no bearing on the
4480 declaration of `f'. */
4481 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4483 /* Check everything. */
4484 last_level_to_check
= 0;
4486 for (parm_level
= parms
;
4487 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4488 parm_level
= TREE_CHAIN (parm_level
))
4490 tree inner_parms
= TREE_VALUE (parm_level
);
4494 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4495 for (i
= 0; i
< ntparms
; ++i
)
4497 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4500 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4505 if (is_friend_decl
== 2)
4512 /* Clear out the default argument so that we are not
4514 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4518 /* At this point, if we're still interested in issuing messages,
4519 they must apply to classes surrounding the object declared. */
4521 msg
= G_("default argument for template parameter for class "
4528 /* Worker for push_template_decl_real, called via
4529 for_each_template_parm. DATA is really an int, indicating the
4530 level of the parameters we are interested in. If T is a template
4531 parameter of that level, return nonzero. */
4534 template_parm_this_level_p (tree t
, void* data
)
4536 int this_level
= *(int *)data
;
4539 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4540 level
= TEMPLATE_PARM_LEVEL (t
);
4542 level
= TEMPLATE_TYPE_LEVEL (t
);
4543 return level
== this_level
;
4546 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4547 parameters given by current_template_args, or reuses a
4548 previously existing one, if appropriate. Returns the DECL, or an
4549 equivalent one, if it is replaced via a call to duplicate_decls.
4551 If IS_FRIEND is true, DECL is a friend declaration. */
4554 push_template_decl_real (tree decl
, bool is_friend
)
4562 int new_template_p
= 0;
4563 /* True if the template is a member template, in the sense of
4565 bool member_template_p
= false;
4567 if (decl
== error_mark_node
|| !current_template_parms
)
4568 return error_mark_node
;
4570 /* See if this is a partial specialization. */
4571 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
4572 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4573 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
4575 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4579 /* For a friend, we want the context of the friend function, not
4580 the type of which it is a friend. */
4581 ctx
= CP_DECL_CONTEXT (decl
);
4582 else if (CP_DECL_CONTEXT (decl
)
4583 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4584 /* In the case of a virtual function, we want the class in which
4586 ctx
= CP_DECL_CONTEXT (decl
);
4588 /* Otherwise, if we're currently defining some class, the DECL
4589 is assumed to be a member of the class. */
4590 ctx
= current_scope ();
4592 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4595 if (!DECL_CONTEXT (decl
))
4596 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4598 /* See if this is a primary template. */
4599 if (is_friend
&& ctx
)
4600 /* A friend template that specifies a class context, i.e.
4601 template <typename T> friend void A<T>::f();
4605 is_primary
= template_parm_scope_p ();
4609 if (DECL_CLASS_SCOPE_P (decl
))
4610 member_template_p
= true;
4611 if (TREE_CODE (decl
) == TYPE_DECL
4612 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4614 error ("template class without a name");
4615 return error_mark_node
;
4617 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4619 if (DECL_DESTRUCTOR_P (decl
))
4623 A destructor shall not be a member template. */
4624 error ("destructor %qD declared as member template", decl
);
4625 return error_mark_node
;
4627 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4628 && (!prototype_p (TREE_TYPE (decl
))
4629 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4630 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4631 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4632 == void_list_node
)))
4634 /* [basic.stc.dynamic.allocation]
4636 An allocation function can be a function
4637 template. ... Template allocation functions shall
4638 have two or more parameters. */
4639 error ("invalid template declaration of %qD", decl
);
4640 return error_mark_node
;
4643 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4644 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4646 else if (TREE_CODE (decl
) == TYPE_DECL
4647 && TYPE_DECL_ALIAS_P (decl
))
4648 /* alias-declaration */
4649 gcc_assert (!DECL_ARTIFICIAL (decl
));
4652 error ("template declaration of %q#D", decl
);
4653 return error_mark_node
;
4657 /* Check to see that the rules regarding the use of default
4658 arguments are not being violated. */
4659 check_default_tmpl_args (decl
, current_template_parms
,
4660 is_primary
, is_partial
, /*is_friend_decl=*/0);
4662 /* Ensure that there are no parameter packs in the type of this
4663 declaration that have not been expanded. */
4664 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4666 /* Check each of the arguments individually to see if there are
4667 any bare parameter packs. */
4668 tree type
= TREE_TYPE (decl
);
4669 tree arg
= DECL_ARGUMENTS (decl
);
4670 tree argtype
= TYPE_ARG_TYPES (type
);
4672 while (arg
&& argtype
)
4674 if (!DECL_PACK_P (arg
)
4675 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4677 /* This is a PARM_DECL that contains unexpanded parameter
4678 packs. We have already complained about this in the
4679 check_for_bare_parameter_packs call, so just replace
4680 these types with ERROR_MARK_NODE. */
4681 TREE_TYPE (arg
) = error_mark_node
;
4682 TREE_VALUE (argtype
) = error_mark_node
;
4685 arg
= DECL_CHAIN (arg
);
4686 argtype
= TREE_CHAIN (argtype
);
4689 /* Check for bare parameter packs in the return type and the
4690 exception specifiers. */
4691 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4692 /* Errors were already issued, set return type to int
4693 as the frontend doesn't expect error_mark_node as
4695 TREE_TYPE (type
) = integer_type_node
;
4696 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4697 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4699 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
4700 && TYPE_DECL_ALIAS_P (decl
))
4701 ? DECL_ORIGINAL_TYPE (decl
)
4702 : TREE_TYPE (decl
)))
4704 TREE_TYPE (decl
) = error_mark_node
;
4705 return error_mark_node
;
4709 return process_partial_specialization (decl
);
4711 args
= current_template_args ();
4714 || TREE_CODE (ctx
) == FUNCTION_DECL
4715 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4716 || (TREE_CODE (decl
) == TYPE_DECL
4717 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4718 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4720 if (DECL_LANG_SPECIFIC (decl
)
4721 && DECL_TEMPLATE_INFO (decl
)
4722 && DECL_TI_TEMPLATE (decl
))
4723 tmpl
= DECL_TI_TEMPLATE (decl
);
4724 /* If DECL is a TYPE_DECL for a class-template, then there won't
4725 be DECL_LANG_SPECIFIC. The information equivalent to
4726 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4727 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4728 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4729 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4731 /* Since a template declaration already existed for this
4732 class-type, we must be redeclaring it here. Make sure
4733 that the redeclaration is valid. */
4734 redeclare_class_template (TREE_TYPE (decl
),
4735 current_template_parms
);
4736 /* We don't need to create a new TEMPLATE_DECL; just use the
4737 one we already had. */
4738 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4742 tmpl
= build_template_decl (decl
, current_template_parms
,
4746 if (DECL_LANG_SPECIFIC (decl
)
4747 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4749 /* A specialization of a member template of a template
4751 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4752 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4753 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4759 tree a
, t
, current
, parms
;
4761 tree tinfo
= get_template_info (decl
);
4765 error ("template definition of non-template %q#D", decl
);
4766 return error_mark_node
;
4769 tmpl
= TI_TEMPLATE (tinfo
);
4771 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4772 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4773 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4774 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4778 /* The declaration is a specialization of a member
4779 template, declared outside the class. Therefore, the
4780 innermost template arguments will be NULL, so we
4781 replace them with the arguments determined by the
4782 earlier call to check_explicit_specialization. */
4783 args
= DECL_TI_ARGS (decl
);
4786 = build_template_decl (decl
, current_template_parms
,
4788 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4789 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4790 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4791 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4792 DECL_TEMPLATE_INFO (new_tmpl
)
4793 = build_template_info (tmpl
, args
);
4795 register_specialization (new_tmpl
,
4796 most_general_template (tmpl
),
4802 /* Make sure the template headers we got make sense. */
4804 parms
= DECL_TEMPLATE_PARMS (tmpl
);
4805 i
= TMPL_PARMS_DEPTH (parms
);
4806 if (TMPL_ARGS_DEPTH (args
) != i
)
4808 error ("expected %d levels of template parms for %q#D, got %d",
4809 i
, decl
, TMPL_ARGS_DEPTH (args
));
4812 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
4814 a
= TMPL_ARGS_LEVEL (args
, i
);
4815 t
= INNERMOST_TEMPLATE_PARMS (parms
);
4817 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
4819 if (current
== decl
)
4820 error ("got %d template parameters for %q#D",
4821 TREE_VEC_LENGTH (a
), decl
);
4823 error ("got %d template parameters for %q#T",
4824 TREE_VEC_LENGTH (a
), current
);
4825 error (" but %d required", TREE_VEC_LENGTH (t
));
4826 /* Avoid crash in import_export_decl. */
4827 DECL_INTERFACE_KNOWN (decl
) = 1;
4828 return error_mark_node
;
4831 if (current
== decl
)
4833 else if (current
== NULL_TREE
)
4834 /* Can happen in erroneous input. */
4837 current
= get_containing_scope (current
);
4840 /* Check that the parms are used in the appropriate qualifying scopes
4841 in the declarator. */
4842 if (!comp_template_args
4844 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
4847 template arguments to %qD do not match original template %qD",
4848 decl
, DECL_TEMPLATE_RESULT (tmpl
));
4849 if (!uses_template_parms (TI_ARGS (tinfo
)))
4850 inform (input_location
, "use template<> for an explicit specialization");
4851 /* Avoid crash in import_export_decl. */
4852 DECL_INTERFACE_KNOWN (decl
) = 1;
4853 return error_mark_node
;
4857 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4858 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4860 /* Push template declarations for global functions and types. Note
4861 that we do not try to push a global template friend declared in a
4862 template class; such a thing may well depend on the template
4863 parameters of the class. */
4864 if (new_template_p
&& !ctx
4865 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
4867 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
4868 if (tmpl
== error_mark_node
)
4869 return error_mark_node
;
4871 /* Hide template friend classes that haven't been declared yet. */
4872 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
4874 DECL_ANTICIPATED (tmpl
) = 1;
4875 DECL_FRIEND_P (tmpl
) = 1;
4881 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
4884 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4885 if (DECL_CONV_FN_P (tmpl
))
4887 int depth
= TMPL_PARMS_DEPTH (parms
);
4889 /* It is a conversion operator. See if the type converted to
4890 depends on innermost template operands. */
4892 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
4894 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
4897 /* Give template template parms a DECL_CONTEXT of the template
4898 for which they are a parameter. */
4899 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4900 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
4902 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4903 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4904 DECL_CONTEXT (parm
) = tmpl
;
4908 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4909 back to its most general template. If TMPL is a specialization,
4910 ARGS may only have the innermost set of arguments. Add the missing
4911 argument levels if necessary. */
4912 if (DECL_TEMPLATE_INFO (tmpl
))
4913 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
4915 info
= build_template_info (tmpl
, args
);
4917 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
4918 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
4921 if (is_primary
&& !DECL_LANG_SPECIFIC (decl
))
4922 retrofit_lang_decl (decl
);
4923 if (DECL_LANG_SPECIFIC (decl
))
4924 DECL_TEMPLATE_INFO (decl
) = info
;
4927 return DECL_TEMPLATE_RESULT (tmpl
);
4931 push_template_decl (tree decl
)
4933 return push_template_decl_real (decl
, false);
4936 /* FN is an inheriting constructor that inherits from the constructor
4937 template INHERITED; turn FN into a constructor template with a matching
4941 add_inherited_template_parms (tree fn
, tree inherited
)
4944 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
4945 inner_parms
= copy_node (inner_parms
);
4947 = tree_cons (size_int (processing_template_decl
+ 1),
4948 inner_parms
, current_template_parms
);
4949 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
4950 tree args
= template_parms_to_args (parms
);
4951 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
4952 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
4953 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
4954 DECL_ARTIFICIAL (tmpl
) = true;
4955 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4959 /* Called when a class template TYPE is redeclared with the indicated
4960 template PARMS, e.g.:
4962 template <class T> struct S;
4963 template <class T> struct S {}; */
4966 redeclare_class_template (tree type
, tree parms
)
4972 if (!TYPE_TEMPLATE_INFO (type
))
4974 error ("%qT is not a template type", type
);
4978 tmpl
= TYPE_TI_TEMPLATE (type
);
4979 if (!PRIMARY_TEMPLATE_P (tmpl
))
4980 /* The type is nested in some template class. Nothing to worry
4981 about here; there are no new template parameters for the nested
4987 error ("template specifiers not specified in declaration of %qD",
4992 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4993 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
4995 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
4997 error_n (input_location
, TREE_VEC_LENGTH (parms
),
4998 "redeclared with %d template parameter",
4999 "redeclared with %d template parameters",
5000 TREE_VEC_LENGTH (parms
));
5001 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
5002 "previous declaration %q+D used %d template parameter",
5003 "previous declaration %q+D used %d template parameters",
5004 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5008 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5015 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5016 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5019 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5020 if (tmpl_parm
== error_mark_node
)
5023 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5024 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5025 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5027 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5029 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5030 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5031 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5032 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5033 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5034 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5035 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5036 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5037 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5039 error ("template parameter %q+#D", tmpl_parm
);
5040 error ("redeclared here as %q#D", parm
);
5044 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5046 /* We have in [temp.param]:
5048 A template-parameter may not be given default arguments
5049 by two different declarations in the same scope. */
5050 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5051 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5052 "original definition appeared here");
5056 if (parm_default
!= NULL_TREE
)
5057 /* Update the previous template parameters (which are the ones
5058 that will really count) with the new default value. */
5059 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5060 else if (tmpl_default
!= NULL_TREE
)
5061 /* Update the new parameters, too; they'll be used as the
5062 parameters for any members. */
5063 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5069 /* Simplify EXPR if it is a non-dependent expression. Returns the
5070 (possibly simplified) expression. */
5073 fold_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5075 if (expr
== NULL_TREE
)
5078 /* If we're in a template, but EXPR isn't value dependent, simplify
5079 it. We're supposed to treat:
5081 template <typename T> void f(T[1 + 1]);
5082 template <typename T> void f(T[2]);
5084 as two declarations of the same function, for example. */
5085 if (processing_template_decl
5086 && !instantiation_dependent_expression_p (expr
)
5087 && potential_constant_expression (expr
))
5089 HOST_WIDE_INT saved_processing_template_decl
;
5091 saved_processing_template_decl
= processing_template_decl
;
5092 processing_template_decl
= 0;
5093 expr
= tsubst_copy_and_build (expr
,
5096 /*in_decl=*/NULL_TREE
,
5097 /*function_p=*/false,
5098 /*integral_constant_expression_p=*/true);
5099 processing_template_decl
= saved_processing_template_decl
;
5105 fold_non_dependent_expr (tree expr
)
5107 return fold_non_dependent_expr_sfinae (expr
, tf_error
);
5110 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5111 template declaration, or a TYPE_DECL for an alias declaration. */
5114 alias_type_or_template_p (tree t
)
5118 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5121 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5122 || DECL_ALIAS_TEMPLATE_P (t
));
5125 /* Return TRUE iff is a specialization of an alias template. */
5128 alias_template_specialization_p (const_tree t
)
5134 && TYPE_TEMPLATE_INFO (t
)
5135 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t
))
5136 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t
)));
5139 /* Return either TMPL or another template that it is equivalent to under DR
5140 1286: An alias that just changes the name of a template is equivalent to
5141 the other template. */
5144 get_underlying_template (tree tmpl
)
5146 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
5147 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
5149 tree result
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5150 if (TYPE_TEMPLATE_INFO (result
))
5152 tree sub
= TYPE_TI_TEMPLATE (result
);
5153 if (PRIMARY_TEMPLATE_P (sub
)
5154 && same_type_p (result
, TREE_TYPE (sub
)))
5156 /* The alias type is equivalent to the pattern of the
5157 underlying template, so strip the alias. */
5167 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5168 must be a function or a pointer-to-function type, as specified
5169 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5170 and check that the resulting function has external linkage. */
5173 convert_nontype_argument_function (tree type
, tree expr
)
5177 linkage_kind linkage
;
5179 fn
= instantiate_type (type
, fns
, tf_none
);
5180 if (fn
== error_mark_node
)
5181 return error_mark_node
;
5184 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5185 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5186 if (BASELINK_P (fn_no_ptr
))
5187 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5189 /* [temp.arg.nontype]/1
5191 A template-argument for a non-type, non-template template-parameter
5194 -- the address of an object or function with external [C++11: or
5195 internal] linkage. */
5197 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
5199 error ("%qE is not a valid template argument for type %qT", expr
, type
);
5200 if (TYPE_PTR_P (type
))
5201 error ("it must be the address of a function with external linkage");
5203 error ("it must be the name of a function with external linkage");
5207 linkage
= decl_linkage (fn_no_ptr
);
5208 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
5210 if (cxx_dialect
>= cxx11
)
5211 error ("%qE is not a valid template argument for type %qT "
5212 "because %qD has no linkage",
5213 expr
, type
, fn_no_ptr
);
5215 error ("%qE is not a valid template argument for type %qT "
5216 "because %qD does not have external linkage",
5217 expr
, type
, fn_no_ptr
);
5224 /* Subroutine of convert_nontype_argument.
5225 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5226 Emit an error otherwise. */
5229 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5230 tsubst_flags_t complain
)
5233 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5235 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
5237 if (complain
& tf_error
)
5239 error ("%qE is not a valid template argument for type %qT",
5241 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5246 /* Returns TRUE iff the address of OP is value-dependent.
5248 14.6.2.4 [temp.dep.temp]:
5249 A non-integral non-type template-argument is dependent if its type is
5250 dependent or it has either of the following forms
5253 and contains a nested-name-specifier which specifies a class-name that
5254 names a dependent type.
5256 We generalize this to just say that the address of a member of a
5257 dependent class is value-dependent; the above doesn't cover the
5258 address of a static data member named with an unqualified-id. */
5261 has_value_dependent_address (tree op
)
5263 /* We could use get_inner_reference here, but there's no need;
5264 this is only relevant for template non-type arguments, which
5265 can only be expressed as &id-expression. */
5268 tree ctx
= CP_DECL_CONTEXT (op
);
5269 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5276 /* The next set of functions are used for providing helpful explanatory
5277 diagnostics for failed overload resolution. Their messages should be
5278 indented by two spaces for consistency with the messages in
5282 unify_success (bool /*explain_p*/)
5288 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5291 inform (input_location
,
5292 " couldn't deduce template parameter %qD", parm
);
5297 unify_invalid (bool /*explain_p*/)
5303 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5306 inform (input_location
,
5307 " types %qT and %qT have incompatible cv-qualifiers",
5313 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5316 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5321 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
5324 inform (input_location
,
5325 " template parameter %qD is not a parameter pack, but "
5332 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
5335 inform (input_location
,
5336 " template argument %qE does not match "
5337 "pointer-to-member constant %qE",
5343 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
5346 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
5351 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
5354 inform (input_location
,
5355 " inconsistent parameter pack deduction with %qT and %qT",
5361 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
5366 inform (input_location
,
5367 " deduced conflicting types for parameter %qT (%qT and %qT)",
5368 parm
, first
, second
);
5370 inform (input_location
,
5371 " deduced conflicting values for non-type parameter "
5372 "%qE (%qE and %qE)", parm
, first
, second
);
5378 unify_vla_arg (bool explain_p
, tree arg
)
5381 inform (input_location
,
5382 " variable-sized array type %qT is not "
5383 "a valid template argument",
5389 unify_method_type_error (bool explain_p
, tree arg
)
5392 inform (input_location
,
5393 " member function type %qT is not a valid template argument",
5399 unify_arity (bool explain_p
, int have
, int wanted
)
5402 inform_n (input_location
, wanted
,
5403 " candidate expects %d argument, %d provided",
5404 " candidate expects %d arguments, %d provided",
5410 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
5412 return unify_arity (explain_p
, have
, wanted
);
5416 unify_too_few_arguments (bool explain_p
, int have
, int wanted
)
5418 return unify_arity (explain_p
, have
, wanted
);
5422 unify_arg_conversion (bool explain_p
, tree to_type
,
5423 tree from_type
, tree arg
)
5426 inform (EXPR_LOC_OR_HERE (arg
),
5427 " cannot convert %qE (type %qT) to type %qT",
5428 arg
, from_type
, to_type
);
5433 unify_no_common_base (bool explain_p
, enum template_base_result r
,
5434 tree parm
, tree arg
)
5439 case tbr_ambiguous_baseclass
:
5440 inform (input_location
, " %qT is an ambiguous base class of %qT",
5444 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
5451 unify_inconsistent_template_template_parameters (bool explain_p
)
5454 inform (input_location
,
5455 " template parameters of a template template argument are "
5456 "inconsistent with other deduced template arguments");
5461 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
5464 inform (input_location
,
5465 " can't deduce a template for %qT from non-template type %qT",
5471 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
5474 inform (input_location
,
5475 " template argument %qE does not match %qD", arg
, parm
);
5480 unify_overload_resolution_failure (bool explain_p
, tree arg
)
5483 inform (input_location
,
5484 " could not resolve address from overloaded function %qE",
5489 /* Attempt to convert the non-type template parameter EXPR to the
5490 indicated TYPE. If the conversion is successful, return the
5491 converted value. If the conversion is unsuccessful, return
5492 NULL_TREE if we issued an error message, or error_mark_node if we
5493 did not. We issue error messages for out-and-out bad template
5494 parameters, but not simply because the conversion failed, since we
5495 might be just trying to do argument deduction. Both TYPE and EXPR
5496 must be non-dependent.
5498 The conversion follows the special rules described in
5499 [temp.arg.nontype], and it is much more strict than an implicit
5502 This function is called twice for each template argument (see
5503 lookup_template_class for a more accurate description of this
5504 problem). This means that we need to handle expressions which
5505 are not valid in a C++ source, but can be created from the
5506 first call (for instance, casts to perform conversions). These
5507 hacks can go away after we fix the double coercion problem. */
5510 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5514 /* Detect immediately string literals as invalid non-type argument.
5515 This special-case is not needed for correctness (we would easily
5516 catch this later), but only to provide better diagnostic for this
5517 common user mistake. As suggested by DR 100, we do not mention
5518 linkage issues in the diagnostic as this is not the point. */
5519 /* FIXME we're making this OK. */
5520 if (TREE_CODE (expr
) == STRING_CST
)
5522 if (complain
& tf_error
)
5523 error ("%qE is not a valid template argument for type %qT "
5524 "because string literals can never be used in this context",
5529 /* Add the ADDR_EXPR now for the benefit of
5530 value_dependent_expression_p. */
5531 if (TYPE_PTROBV_P (type
)
5532 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
5534 expr
= decay_conversion (expr
, complain
);
5535 if (expr
== error_mark_node
)
5536 return error_mark_node
;
5539 /* If we are in a template, EXPR may be non-dependent, but still
5540 have a syntactic, rather than semantic, form. For example, EXPR
5541 might be a SCOPE_REF, rather than the VAR_DECL to which the
5542 SCOPE_REF refers. Preserving the qualifying scope is necessary
5543 so that access checking can be performed when the template is
5544 instantiated -- but here we need the resolved form so that we can
5545 convert the argument. */
5546 if (TYPE_REF_OBJ_P (type
)
5547 && has_value_dependent_address (expr
))
5548 /* If we want the address and it's value-dependent, don't fold. */;
5549 else if (!type_unknown_p (expr
))
5550 expr
= fold_non_dependent_expr_sfinae (expr
, complain
);
5551 if (error_operand_p (expr
))
5552 return error_mark_node
;
5553 expr_type
= TREE_TYPE (expr
);
5554 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5555 expr
= mark_lvalue_use (expr
);
5557 expr
= mark_rvalue_use (expr
);
5559 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5560 to a non-type argument of "nullptr". */
5561 if (expr
== nullptr_node
&& TYPE_PTR_OR_PTRMEM_P (type
))
5562 expr
= convert (type
, expr
);
5564 /* In C++11, integral or enumeration non-type template arguments can be
5565 arbitrary constant expressions. Pointer and pointer to
5566 member arguments can be general constant expressions that evaluate
5567 to a null value, but otherwise still need to be of a specific form. */
5568 if (cxx_dialect
>= cxx11
)
5570 if (TREE_CODE (expr
) == PTRMEM_CST
)
5571 /* A PTRMEM_CST is already constant, and a valid template
5572 argument for a parameter of pointer to member type, we just want
5573 to leave it in that form rather than lower it to a
5575 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5576 expr
= maybe_constant_value (expr
);
5577 else if (TYPE_PTR_OR_PTRMEM_P (type
))
5579 tree folded
= maybe_constant_value (expr
);
5580 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
5581 : null_member_pointer_value_p (folded
))
5586 /* HACK: Due to double coercion, we can get a
5587 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5588 which is the tree that we built on the first call (see
5589 below when coercing to reference to object or to reference to
5590 function). We just strip everything and get to the arg.
5591 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5593 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5595 tree probe_type
, probe
= expr
;
5596 if (REFERENCE_REF_P (probe
))
5597 probe
= TREE_OPERAND (probe
, 0);
5598 probe_type
= TREE_TYPE (probe
);
5599 if (TREE_CODE (probe
) == NOP_EXPR
)
5601 /* ??? Maybe we could use convert_from_reference here, but we
5602 would need to relax its constraints because the NOP_EXPR
5603 could actually change the type to something more cv-qualified,
5604 and this is not folded by convert_from_reference. */
5605 tree addr
= TREE_OPERAND (probe
, 0);
5606 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
5607 && TREE_CODE (addr
) == ADDR_EXPR
5608 && TYPE_PTR_P (TREE_TYPE (addr
))
5609 && (same_type_ignoring_top_level_qualifiers_p
5610 (TREE_TYPE (probe_type
),
5611 TREE_TYPE (TREE_TYPE (addr
)))))
5613 expr
= TREE_OPERAND (addr
, 0);
5614 expr_type
= TREE_TYPE (probe_type
);
5619 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5620 parameter is a pointer to object, through decay and
5621 qualification conversion. Let's strip everything. */
5622 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
5626 if (TREE_CODE (probe
) == ADDR_EXPR
5627 && TYPE_PTR_P (TREE_TYPE (probe
)))
5629 /* Skip the ADDR_EXPR only if it is part of the decay for
5630 an array. Otherwise, it is part of the original argument
5631 in the source code. */
5632 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe
, 0))) == ARRAY_TYPE
)
5633 probe
= TREE_OPERAND (probe
, 0);
5635 expr_type
= TREE_TYPE (expr
);
5639 /* [temp.arg.nontype]/5, bullet 1
5641 For a non-type template-parameter of integral or enumeration type,
5642 integral promotions (_conv.prom_) and integral conversions
5643 (_conv.integral_) are applied. */
5644 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5646 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
5647 t
= maybe_constant_value (t
);
5648 if (t
!= error_mark_node
)
5651 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
5652 return error_mark_node
;
5654 /* Notice that there are constant expressions like '4 % 0' which
5655 do not fold into integer constants. */
5656 if (TREE_CODE (expr
) != INTEGER_CST
)
5658 if (complain
& tf_error
)
5660 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
5661 if (processing_template_decl
5662 && !require_potential_constant_expression (expr
))
5664 expr
= cxx_constant_value (expr
);
5665 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
5666 inform (EXPR_LOC_OR_HERE (expr
),
5667 "in template argument for type %qT ", type
);
5668 if (expr
== error_mark_node
)
5670 /* else cxx_constant_value complained but gave us
5671 a real constant, so go ahead. */
5672 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
5678 /* Avoid typedef problems. */
5679 if (TREE_TYPE (expr
) != type
)
5680 expr
= fold_convert (type
, expr
);
5682 /* [temp.arg.nontype]/5, bullet 2
5684 For a non-type template-parameter of type pointer to object,
5685 qualification conversions (_conv.qual_) and the array-to-pointer
5686 conversion (_conv.array_) are applied. */
5687 else if (TYPE_PTROBV_P (type
))
5689 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5691 A template-argument for a non-type, non-template template-parameter
5692 shall be one of: [...]
5694 -- the name of a non-type template-parameter;
5695 -- the address of an object or function with external linkage, [...]
5696 expressed as "& id-expression" where the & is optional if the name
5697 refers to a function or array, or if the corresponding
5698 template-parameter is a reference.
5700 Here, we do not care about functions, as they are invalid anyway
5701 for a parameter of type pointer-to-object. */
5703 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5704 /* Non-type template parameters are OK. */
5706 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
5707 /* Null pointer values are OK in C++11. */;
5708 else if (TREE_CODE (expr
) != ADDR_EXPR
5709 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5713 error ("%qD is not a valid template argument "
5714 "because %qD is a variable, not the address of "
5719 if (POINTER_TYPE_P (expr_type
))
5721 error ("%qE is not a valid template argument for %qT "
5722 "because it is not the address of a variable",
5726 /* Other values, like integer constants, might be valid
5727 non-type arguments of some other type. */
5728 return error_mark_node
;
5734 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
5735 ? TREE_OPERAND (expr
, 0) : expr
);
5738 error ("%qE is not a valid template argument of type %qT "
5739 "because %qE is not a variable",
5743 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
5745 error ("%qE is not a valid template argument of type %qT "
5746 "because %qD does not have external linkage",
5750 else if (cxx_dialect
>= cxx11
&& decl_linkage (decl
) == lk_none
)
5752 error ("%qE is not a valid template argument of type %qT "
5753 "because %qD has no linkage",
5759 expr
= decay_conversion (expr
, complain
);
5760 if (expr
== error_mark_node
)
5761 return error_mark_node
;
5763 expr
= perform_qualification_conversions (type
, expr
);
5764 if (expr
== error_mark_node
)
5765 return error_mark_node
;
5767 /* [temp.arg.nontype]/5, bullet 3
5769 For a non-type template-parameter of type reference to object, no
5770 conversions apply. The type referred to by the reference may be more
5771 cv-qualified than the (otherwise identical) type of the
5772 template-argument. The template-parameter is bound directly to the
5773 template-argument, which must be an lvalue. */
5774 else if (TYPE_REF_OBJ_P (type
))
5776 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
5778 return error_mark_node
;
5780 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5782 error ("%qE is not a valid template argument for type %qT "
5783 "because of conflicts in cv-qualification", expr
, type
);
5787 if (!real_lvalue_p (expr
))
5789 error ("%qE is not a valid template argument for type %qT "
5790 "because it is not an lvalue", expr
, type
);
5794 /* [temp.arg.nontype]/1
5796 A template-argument for a non-type, non-template template-parameter
5797 shall be one of: [...]
5799 -- the address of an object or function with external linkage. */
5800 if (INDIRECT_REF_P (expr
)
5801 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5803 expr
= TREE_OPERAND (expr
, 0);
5806 error ("%q#D is not a valid template argument for type %qT "
5807 "because a reference variable does not have a constant "
5808 "address", expr
, type
);
5815 error ("%qE is not a valid template argument for type %qT "
5816 "because it is not an object with external linkage",
5821 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5823 error ("%qE is not a valid template argument for type %qT "
5824 "because object %qD has not external linkage",
5829 expr
= build_nop (type
, build_address (expr
));
5831 /* [temp.arg.nontype]/5, bullet 4
5833 For a non-type template-parameter of type pointer to function, only
5834 the function-to-pointer conversion (_conv.func_) is applied. If the
5835 template-argument represents a set of overloaded functions (or a
5836 pointer to such), the matching function is selected from the set
5838 else if (TYPE_PTRFN_P (type
))
5840 /* If the argument is a template-id, we might not have enough
5841 context information to decay the pointer. */
5842 if (!type_unknown_p (expr_type
))
5844 expr
= decay_conversion (expr
, complain
);
5845 if (expr
== error_mark_node
)
5846 return error_mark_node
;
5849 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
5850 /* Null pointer values are OK in C++11. */
5851 return perform_qualification_conversions (type
, expr
);
5853 expr
= convert_nontype_argument_function (type
, expr
);
5854 if (!expr
|| expr
== error_mark_node
)
5857 /* [temp.arg.nontype]/5, bullet 5
5859 For a non-type template-parameter of type reference to function, no
5860 conversions apply. If the template-argument represents a set of
5861 overloaded functions, the matching function is selected from the set
5863 else if (TYPE_REFFN_P (type
))
5865 if (TREE_CODE (expr
) == ADDR_EXPR
)
5867 error ("%qE is not a valid template argument for type %qT "
5868 "because it is a pointer", expr
, type
);
5869 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
5873 expr
= convert_nontype_argument_function (type
, expr
);
5874 if (!expr
|| expr
== error_mark_node
)
5877 expr
= build_nop (type
, build_address (expr
));
5879 /* [temp.arg.nontype]/5, bullet 6
5881 For a non-type template-parameter of type pointer to member function,
5882 no conversions apply. If the template-argument represents a set of
5883 overloaded member functions, the matching member function is selected
5884 from the set (_over.over_). */
5885 else if (TYPE_PTRMEMFUNC_P (type
))
5887 expr
= instantiate_type (type
, expr
, tf_none
);
5888 if (expr
== error_mark_node
)
5889 return error_mark_node
;
5891 /* [temp.arg.nontype] bullet 1 says the pointer to member
5892 expression must be a pointer-to-member constant. */
5893 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5894 return error_mark_node
;
5896 /* There is no way to disable standard conversions in
5897 resolve_address_of_overloaded_function (called by
5898 instantiate_type). It is possible that the call succeeded by
5899 converting &B::I to &D::I (where B is a base of D), so we need
5900 to reject this conversion here.
5902 Actually, even if there was a way to disable standard conversions,
5903 it would still be better to reject them here so that we can
5904 provide a superior diagnostic. */
5905 if (!same_type_p (TREE_TYPE (expr
), type
))
5907 error ("%qE is not a valid template argument for type %qT "
5908 "because it is of type %qT", expr
, type
,
5910 /* If we are just one standard conversion off, explain. */
5911 if (can_convert_standard (type
, TREE_TYPE (expr
), complain
))
5912 inform (input_location
,
5913 "standard conversions are not allowed in this context");
5917 /* [temp.arg.nontype]/5, bullet 7
5919 For a non-type template-parameter of type pointer to data member,
5920 qualification conversions (_conv.qual_) are applied. */
5921 else if (TYPE_PTRDATAMEM_P (type
))
5923 /* [temp.arg.nontype] bullet 1 says the pointer to member
5924 expression must be a pointer-to-member constant. */
5925 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5926 return error_mark_node
;
5928 expr
= perform_qualification_conversions (type
, expr
);
5929 if (expr
== error_mark_node
)
5932 else if (NULLPTR_TYPE_P (type
))
5934 if (expr
!= nullptr_node
)
5936 error ("%qE is not a valid template argument for type %qT "
5937 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
5942 /* A template non-type parameter must be one of the above. */
5946 /* Sanity check: did we actually convert the argument to the
5948 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5949 (type
, TREE_TYPE (expr
)));
5953 /* Subroutine of coerce_template_template_parms, which returns 1 if
5954 PARM_PARM and ARG_PARM match using the rule for the template
5955 parameters of template template parameters. Both PARM and ARG are
5956 template parameters; the rest of the arguments are the same as for
5957 coerce_template_template_parms.
5960 coerce_template_template_parm (tree parm
,
5962 tsubst_flags_t complain
,
5966 if (arg
== NULL_TREE
|| arg
== error_mark_node
5967 || parm
== NULL_TREE
|| parm
== error_mark_node
)
5970 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5973 switch (TREE_CODE (parm
))
5976 /* We encounter instantiations of templates like
5977 template <template <template <class> class> class TT>
5980 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
5981 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
5983 if (!coerce_template_template_parms
5984 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
5990 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
5991 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5992 /* Argument is a parameter pack but parameter is not. */
5997 /* The tsubst call is used to handle cases such as
5999 template <int> class C {};
6000 template <class T, template <T> class TT> class D {};
6003 i.e. the parameter list of TT depends on earlier parameters. */
6004 if (!uses_template_parms (TREE_TYPE (arg
))
6006 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
6010 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6011 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6012 /* Argument is a parameter pack but parameter is not. */
6025 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6026 template template parameters. Both PARM_PARMS and ARG_PARMS are
6027 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6030 Consider the example:
6031 template <class T> class A;
6032 template<template <class U> class TT> class B;
6034 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6035 the parameters to A, and OUTER_ARGS contains A. */
6038 coerce_template_template_parms (tree parm_parms
,
6040 tsubst_flags_t complain
,
6044 int nparms
, nargs
, i
;
6048 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
6049 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
6051 nparms
= TREE_VEC_LENGTH (parm_parms
);
6052 nargs
= TREE_VEC_LENGTH (arg_parms
);
6054 /* Determine whether we have a parameter pack at the end of the
6055 template template parameter's template parameter list. */
6056 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
6058 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
6060 if (parm
== error_mark_node
)
6063 switch (TREE_CODE (parm
))
6067 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6072 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6082 && !(variadic_p
&& nargs
>= nparms
- 1))
6085 /* Check all of the template parameters except the parameter pack at
6086 the end (if any). */
6087 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6089 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6090 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6093 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6094 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6096 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6104 /* Check each of the template parameters in the template
6105 argument against the template parameter pack at the end of
6106 the template template parameter. */
6107 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6110 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6112 for (; i
< nargs
; ++i
)
6114 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6117 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6119 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6128 /* Verifies that the deduced template arguments (in TARGS) for the
6129 template template parameters (in TPARMS) represent valid bindings,
6130 by comparing the template parameter list of each template argument
6131 to the template parameter list of its corresponding template
6132 template parameter, in accordance with DR150. This
6133 routine can only be called after all template arguments have been
6134 deduced. It will return TRUE if all of the template template
6135 parameter bindings are okay, FALSE otherwise. */
6137 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6139 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6142 /* We're dealing with template parms in this process. */
6143 ++processing_template_decl
;
6145 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6147 for (i
= 0; i
< ntparms
; ++i
)
6149 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6150 tree targ
= TREE_VEC_ELT (targs
, i
);
6152 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6154 tree packed_args
= NULL_TREE
;
6157 if (ARGUMENT_PACK_P (targ
))
6159 /* Look inside the argument pack. */
6160 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6161 len
= TREE_VEC_LENGTH (packed_args
);
6164 for (idx
= 0; idx
< len
; ++idx
)
6166 tree targ_parms
= NULL_TREE
;
6169 /* Extract the next argument from the argument
6171 targ
= TREE_VEC_ELT (packed_args
, idx
);
6173 if (PACK_EXPANSION_P (targ
))
6174 /* Look at the pattern of the pack expansion. */
6175 targ
= PACK_EXPANSION_PATTERN (targ
);
6177 /* Extract the template parameters from the template
6179 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6180 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6181 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6182 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6184 /* Verify that we can coerce the template template
6185 parameters from the template argument to the template
6186 parameter. This requires an exact match. */
6188 && !coerce_template_template_parms
6189 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6204 --processing_template_decl
;
6208 /* Since type attributes aren't mangled, we need to strip them from
6209 template type arguments. */
6212 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6215 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6217 mv
= TYPE_MAIN_VARIANT (arg
);
6218 arg
= strip_typedefs (arg
);
6219 if (TYPE_ALIGN (arg
) != TYPE_ALIGN (mv
)
6220 || TYPE_ATTRIBUTES (arg
) != TYPE_ATTRIBUTES (mv
))
6222 if (complain
& tf_warning
)
6223 warning (0, "ignoring attributes on template argument %qT", arg
);
6224 arg
= build_aligned_type (arg
, TYPE_ALIGN (mv
));
6225 arg
= cp_build_type_attribute_variant (arg
, TYPE_ATTRIBUTES (mv
));
6230 /* Convert the indicated template ARG as necessary to match the
6231 indicated template PARM. Returns the converted ARG, or
6232 error_mark_node if the conversion was unsuccessful. Error and
6233 warning messages are issued under control of COMPLAIN. This
6234 conversion is for the Ith parameter in the parameter list. ARGS is
6235 the full set of template arguments deduced so far. */
6238 convert_template_argument (tree parm
,
6241 tsubst_flags_t complain
,
6247 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
6249 if (TREE_CODE (arg
) == TREE_LIST
6250 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
6252 /* The template argument was the name of some
6253 member function. That's usually
6254 invalid, but static members are OK. In any
6255 case, grab the underlying fields/functions
6256 and issue an error later if required. */
6257 orig_arg
= TREE_VALUE (arg
);
6258 TREE_TYPE (arg
) = unknown_type_node
;
6263 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
6264 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
6265 || requires_tmpl_type
);
6267 /* When determining whether an argument pack expansion is a template,
6268 look at the pattern. */
6269 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
6270 arg
= PACK_EXPANSION_PATTERN (arg
);
6272 /* Deal with an injected-class-name used as a template template arg. */
6273 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
6275 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
6276 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6278 if (cxx_dialect
>= cxx11
)
6279 /* OK under DR 1004. */;
6280 else if (complain
& tf_warning_or_error
)
6281 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
6282 " used as template template argument", TYPE_NAME (arg
));
6283 else if (flag_pedantic_errors
)
6291 ((TREE_CODE (arg
) == TEMPLATE_DECL
6292 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
6293 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
6294 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6295 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
6298 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6299 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
6300 arg
= TYPE_STUB_DECL (arg
);
6302 is_type
= TYPE_P (arg
) || is_tmpl_type
;
6304 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
6305 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
6307 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
6309 if (complain
& tf_error
)
6310 error ("invalid use of destructor %qE as a type", orig_arg
);
6311 return error_mark_node
;
6314 permerror (input_location
,
6315 "to refer to a type member of a template parameter, "
6316 "use %<typename %E%>", orig_arg
);
6318 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
6319 TREE_OPERAND (arg
, 1),
6325 if (is_type
!= requires_type
)
6329 if (complain
& tf_error
)
6331 error ("type/value mismatch at argument %d in template "
6332 "parameter list for %qD",
6335 error (" expected a constant of type %qT, got %qT",
6337 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6338 else if (requires_tmpl_type
)
6339 error (" expected a class template, got %qE", orig_arg
);
6341 error (" expected a type, got %qE", orig_arg
);
6344 return error_mark_node
;
6346 if (is_tmpl_type
^ requires_tmpl_type
)
6348 if (in_decl
&& (complain
& tf_error
))
6350 error ("type/value mismatch at argument %d in template "
6351 "parameter list for %qD",
6354 error (" expected a type, got %qT", DECL_NAME (arg
));
6356 error (" expected a class template, got %qT", orig_arg
);
6358 return error_mark_node
;
6363 if (requires_tmpl_type
)
6365 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6367 else if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6368 /* The number of argument required is not known yet.
6369 Just accept it for now. */
6370 val
= TREE_TYPE (arg
);
6373 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6376 /* Strip alias templates that are equivalent to another
6378 arg
= get_underlying_template (arg
);
6379 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6381 if (coerce_template_template_parms (parmparm
, argparm
,
6387 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6389 if (val
!= error_mark_node
)
6391 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6392 val
= TREE_TYPE (val
);
6393 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6394 val
= make_pack_expansion (val
);
6399 if (in_decl
&& (complain
& tf_error
))
6401 error ("type/value mismatch at argument %d in "
6402 "template parameter list for %qD",
6404 error (" expected a template of type %qD, got %qT",
6408 val
= error_mark_node
;
6414 /* We only form one instance of each template specialization.
6415 Therefore, if we use a non-canonical variant (i.e., a
6416 typedef), any future messages referring to the type will use
6417 the typedef, which is confusing if those future uses do not
6418 themselves also use the typedef. */
6420 val
= canonicalize_type_argument (val
, complain
);
6424 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6426 if (invalid_nontype_parm_type_p (t
, complain
))
6427 return error_mark_node
;
6429 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6431 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6435 /* Not sure if this is reachable, but it doesn't hurt
6437 error ("type mismatch in nontype parameter pack");
6438 val
= error_mark_node
;
6441 else if (!dependent_template_arg_p (orig_arg
)
6442 && !uses_template_parms (t
))
6443 /* We used to call digest_init here. However, digest_init
6444 will report errors, which we don't want when complain
6445 is zero. More importantly, digest_init will try too
6446 hard to convert things: for example, `0' should not be
6447 converted to pointer type at this point according to
6448 the standard. Accepting this is not merely an
6449 extension, since deciding whether or not these
6450 conversions can occur is part of determining which
6451 function template to call, or whether a given explicit
6452 argument specification is valid. */
6453 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6455 val
= strip_typedefs_expr (orig_arg
);
6457 if (val
== NULL_TREE
)
6458 val
= error_mark_node
;
6459 else if (val
== error_mark_node
&& (complain
& tf_error
))
6460 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6462 if (TREE_CODE (val
) == SCOPE_REF
)
6464 /* Strip typedefs from the SCOPE_REF. */
6465 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
6466 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
6468 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6469 QUALIFIED_NAME_IS_TEMPLATE (val
));
6476 /* Coerces the remaining template arguments in INNER_ARGS (from
6477 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6478 Returns the coerced argument pack. PARM_IDX is the position of this
6479 parameter in the template parameter list. ARGS is the original
6480 template argument list. */
6482 coerce_template_parameter_pack (tree parms
,
6490 tsubst_flags_t complain
)
6492 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6493 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6496 tree packed_types
= NULL_TREE
;
6498 if (arg_idx
> nargs
)
6501 packed_args
= make_tree_vec (nargs
- arg_idx
);
6503 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
6504 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
6506 /* When the template parameter is a non-type template
6507 parameter pack whose type uses parameter packs, we need
6508 to look at each of the template arguments
6509 separately. Build a vector of the types for these
6510 non-type template parameters in PACKED_TYPES. */
6512 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
6513 packed_types
= tsubst_pack_expansion (expansion
, args
,
6516 if (packed_types
== error_mark_node
)
6517 return error_mark_node
;
6519 /* Check that we have the right number of arguments. */
6521 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
6522 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
6525 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
6526 error ("wrong number of template arguments (%d, should be %d)",
6527 nargs
, needed_parms
);
6528 return error_mark_node
;
6531 /* If we aren't able to check the actual arguments now
6532 (because they haven't been expanded yet), we can at least
6533 verify that all of the types used for the non-type
6534 template parameter pack are, in fact, valid for non-type
6535 template parameters. */
6537 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6539 int j
, len
= TREE_VEC_LENGTH (packed_types
);
6540 for (j
= 0; j
< len
; ++j
)
6542 tree t
= TREE_VEC_ELT (packed_types
, j
);
6543 if (invalid_nontype_parm_type_p (t
, complain
))
6544 return error_mark_node
;
6549 /* Convert the remaining arguments, which will be a part of the
6550 parameter pack "parm". */
6551 for (; arg_idx
< nargs
; ++arg_idx
)
6553 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6554 tree actual_parm
= TREE_VALUE (parm
);
6556 if (packed_types
&& !PACK_EXPANSION_P (arg
))
6558 /* When we have a vector of types (corresponding to the
6559 non-type template parameter pack that uses parameter
6560 packs in its type, as mention above), and the
6561 argument is not an expansion (which expands to a
6562 currently unknown number of arguments), clone the
6563 parm and give it the next type in PACKED_TYPES. */
6564 actual_parm
= copy_node (actual_parm
);
6565 TREE_TYPE (actual_parm
) =
6566 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
6569 if (arg
== error_mark_node
)
6571 if (complain
& tf_error
)
6572 error ("template argument %d is invalid", arg_idx
+ 1);
6575 arg
= convert_template_argument (actual_parm
,
6576 arg
, new_args
, complain
, parm_idx
,
6578 if (arg
== error_mark_node
)
6580 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
6583 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6584 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
6585 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
6588 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
6589 TREE_TYPE (argument_pack
)
6590 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
6591 TREE_CONSTANT (argument_pack
) = 1;
6594 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
6595 #ifdef ENABLE_CHECKING
6596 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
6597 TREE_VEC_LENGTH (packed_args
));
6599 return argument_pack
;
6602 /* Returns the number of pack expansions in the template argument vector
6606 pack_expansion_args_count (tree args
)
6611 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
6613 tree elt
= TREE_VEC_ELT (args
, i
);
6614 if (elt
&& PACK_EXPANSION_P (elt
))
6620 /* Convert all template arguments to their appropriate types, and
6621 return a vector containing the innermost resulting template
6622 arguments. If any error occurs, return error_mark_node. Error and
6623 warning messages are issued under control of COMPLAIN.
6625 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6626 for arguments not specified in ARGS. Otherwise, if
6627 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6628 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6629 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6633 coerce_template_parms (tree parms
,
6636 tsubst_flags_t complain
,
6637 bool require_all_args
,
6638 bool use_default_args
)
6640 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
6643 tree new_inner_args
;
6644 int saved_unevaluated_operand
;
6645 int saved_inhibit_evaluation_warnings
;
6647 /* When used as a boolean value, indicates whether this is a
6648 variadic template parameter list. Since it's an int, we can also
6649 subtract it from nparms to get the number of non-variadic
6652 int variadic_args_p
= 0;
6653 int post_variadic_parms
= 0;
6655 if (args
== error_mark_node
)
6656 return error_mark_node
;
6658 nparms
= TREE_VEC_LENGTH (parms
);
6660 /* Determine if there are any parameter packs. */
6661 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
6663 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
6665 ++post_variadic_parms
;
6666 if (template_parameter_pack_p (tparm
))
6670 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
6671 /* If there are no parameters that follow a parameter pack, we need to
6672 expand any argument packs so that we can deduce a parameter pack from
6673 some non-packed args followed by an argument pack, as in variadic85.C.
6674 If there are such parameters, we need to leave argument packs intact
6675 so the arguments are assigned properly. This can happen when dealing
6676 with a nested class inside a partial specialization of a class
6677 template, as in variadic92.C, or when deducing a template parameter pack
6678 from a sub-declarator, as in variadic114.C. */
6679 if (!post_variadic_parms
)
6680 inner_args
= expand_template_argument_pack (inner_args
);
6682 /* Count any pack expansion args. */
6683 variadic_args_p
= pack_expansion_args_count (inner_args
);
6685 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6686 if ((nargs
> nparms
&& !variadic_p
)
6687 || (nargs
< nparms
- variadic_p
6690 && (!use_default_args
6691 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
6692 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
6694 if (complain
& tf_error
)
6698 nparms
-= variadic_p
;
6699 error ("wrong number of template arguments "
6700 "(%d, should be %d or more)", nargs
, nparms
);
6703 error ("wrong number of template arguments "
6704 "(%d, should be %d)", nargs
, nparms
);
6707 error ("provided for %q+D", in_decl
);
6710 return error_mark_node
;
6712 /* We can't pass a pack expansion to a non-pack parameter of an alias
6713 template (DR 1430). */
6714 else if (in_decl
&& DECL_ALIAS_TEMPLATE_P (in_decl
)
6716 && nargs
- variadic_args_p
< nparms
- variadic_p
)
6718 if (complain
& tf_error
)
6720 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
6722 tree arg
= TREE_VEC_ELT (inner_args
, i
);
6723 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6725 if (PACK_EXPANSION_P (arg
)
6726 && !template_parameter_pack_p (parm
))
6728 error ("pack expansion argument for non-pack parameter "
6729 "%qD of alias template %qD", parm
, in_decl
);
6730 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
6737 return error_mark_node
;
6740 /* We need to evaluate the template arguments, even though this
6741 template-id may be nested within a "sizeof". */
6742 saved_unevaluated_operand
= cp_unevaluated_operand
;
6743 cp_unevaluated_operand
= 0;
6744 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6745 c_inhibit_evaluation_warnings
= 0;
6746 new_inner_args
= make_tree_vec (nparms
);
6747 new_args
= add_outermost_template_args (args
, new_inner_args
);
6748 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
6753 /* Get the Ith template parameter. */
6754 parm
= TREE_VEC_ELT (parms
, parm_idx
);
6756 if (parm
== error_mark_node
)
6758 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
6762 /* Calculate the next argument. */
6763 if (arg_idx
< nargs
)
6764 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6768 if (template_parameter_pack_p (TREE_VALUE (parm
))
6769 && !(arg
&& ARGUMENT_PACK_P (arg
)))
6771 /* All remaining arguments will be placed in the
6772 template parameter pack PARM. */
6773 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
6774 inner_args
, arg_idx
,
6778 /* Store this argument. */
6779 if (arg
== error_mark_node
)
6781 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
6783 /* We are done with all of the arguments. */
6790 if (PACK_EXPANSION_P (arg
))
6792 /* We don't know how many args we have yet, just
6793 use the unconverted ones for now. */
6794 new_inner_args
= inner_args
;
6798 else if (require_all_args
)
6800 /* There must be a default arg in this case. */
6801 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
6803 /* The position of the first default template argument,
6804 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6806 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6807 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
, arg_idx
);
6812 if (arg
== error_mark_node
)
6814 if (complain
& tf_error
)
6815 error ("template argument %d is invalid", arg_idx
+ 1);
6818 /* This only occurs if there was an error in the template
6819 parameter list itself (which we would already have
6820 reported) that we are trying to recover from, e.g., a class
6821 template with a parameter list such as
6822 template<typename..., typename>. */
6825 arg
= convert_template_argument (TREE_VALUE (parm
),
6826 arg
, new_args
, complain
,
6829 if (arg
== error_mark_node
)
6831 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
6833 cp_unevaluated_operand
= saved_unevaluated_operand
;
6834 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
6837 return error_mark_node
;
6839 #ifdef ENABLE_CHECKING
6840 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6841 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6842 TREE_VEC_LENGTH (new_inner_args
));
6845 return new_inner_args
;
6848 /* Like coerce_template_parms. If PARMS represents all template
6849 parameters levels, this function returns a vector of vectors
6850 representing all the resulting argument levels. Note that in this
6851 case, only the innermost arguments are coerced because the
6852 outermost ones are supposed to have been coerced already.
6854 Otherwise, if PARMS represents only (the innermost) vector of
6855 parameters, this function returns a vector containing just the
6856 innermost resulting arguments. */
6859 coerce_innermost_template_parms (tree parms
,
6862 tsubst_flags_t complain
,
6863 bool require_all_args
,
6864 bool use_default_args
)
6866 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
6867 int args_depth
= TMPL_ARGS_DEPTH (args
);
6870 if (parms_depth
> 1)
6872 coerced_args
= make_tree_vec (parms_depth
);
6876 for (level
= parms
, cur_depth
= parms_depth
;
6877 parms_depth
> 0 && level
!= NULL_TREE
;
6878 level
= TREE_CHAIN (level
), --cur_depth
)
6881 if (cur_depth
== args_depth
)
6882 l
= coerce_template_parms (TREE_VALUE (level
),
6883 args
, in_decl
, complain
,
6887 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
6889 if (l
== error_mark_node
)
6890 return error_mark_node
;
6892 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
6896 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
6897 args
, in_decl
, complain
,
6900 return coerced_args
;
6903 /* Returns 1 if template args OT and NT are equivalent. */
6906 template_args_equal (tree ot
, tree nt
)
6910 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
6913 if (TREE_CODE (nt
) == TREE_VEC
)
6914 /* For member templates */
6915 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
6916 else if (PACK_EXPANSION_P (ot
))
6917 return (PACK_EXPANSION_P (nt
)
6918 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
6919 PACK_EXPANSION_PATTERN (nt
))
6920 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
6921 PACK_EXPANSION_EXTRA_ARGS (nt
)));
6922 else if (ARGUMENT_PACK_P (ot
))
6927 if (!ARGUMENT_PACK_P (nt
))
6930 opack
= ARGUMENT_PACK_ARGS (ot
);
6931 npack
= ARGUMENT_PACK_ARGS (nt
);
6932 len
= TREE_VEC_LENGTH (opack
);
6933 if (TREE_VEC_LENGTH (npack
) != len
)
6935 for (i
= 0; i
< len
; ++i
)
6936 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
6937 TREE_VEC_ELT (npack
, i
)))
6941 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
6943 /* We get here probably because we are in the middle of substituting
6944 into the pattern of a pack expansion. In that case the
6945 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6946 interested in. So we want to use the initial pack argument for
6948 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
6949 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
6950 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
6951 return template_args_equal (ot
, nt
);
6953 else if (TYPE_P (nt
))
6954 return TYPE_P (ot
) && same_type_p (ot
, nt
);
6955 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
6958 return cp_tree_equal (ot
, nt
);
6961 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6962 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6963 NEWARG_PTR with the offending arguments if they are non-NULL. */
6966 comp_template_args_with_info (tree oldargs
, tree newargs
,
6967 tree
*oldarg_ptr
, tree
*newarg_ptr
)
6971 if (oldargs
== newargs
)
6974 if (!oldargs
|| !newargs
)
6977 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
6980 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
6982 tree nt
= TREE_VEC_ELT (newargs
, i
);
6983 tree ot
= TREE_VEC_ELT (oldargs
, i
);
6985 if (! template_args_equal (ot
, nt
))
6987 if (oldarg_ptr
!= NULL
)
6989 if (newarg_ptr
!= NULL
)
6997 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6998 of template arguments. Returns 0 otherwise. */
7001 comp_template_args (tree oldargs
, tree newargs
)
7003 return comp_template_args_with_info (oldargs
, newargs
, NULL
, NULL
);
7007 add_pending_template (tree d
)
7009 tree ti
= (TYPE_P (d
)
7010 ? CLASSTYPE_TEMPLATE_INFO (d
)
7011 : DECL_TEMPLATE_INFO (d
));
7012 struct pending_template
*pt
;
7015 if (TI_PENDING_TEMPLATE_FLAG (ti
))
7018 /* We are called both from instantiate_decl, where we've already had a
7019 tinst_level pushed, and instantiate_template, where we haven't.
7021 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
7024 push_tinst_level (d
);
7026 pt
= ggc_alloc_pending_template ();
7028 pt
->tinst
= current_tinst_level
;
7029 if (last_pending_template
)
7030 last_pending_template
->next
= pt
;
7032 pending_templates
= pt
;
7034 last_pending_template
= pt
;
7036 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
7043 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7044 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7045 documentation for TEMPLATE_ID_EXPR. */
7048 lookup_template_function (tree fns
, tree arglist
)
7052 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
7053 return error_mark_node
;
7055 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
7057 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
7059 error ("%q#D is not a function template", fns
);
7060 return error_mark_node
;
7063 if (BASELINK_P (fns
))
7065 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
7067 BASELINK_FUNCTIONS (fns
),
7072 type
= TREE_TYPE (fns
);
7073 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
7074 type
= unknown_type_node
;
7076 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
7079 /* Within the scope of a template class S<T>, the name S gets bound
7080 (in build_self_reference) to a TYPE_DECL for the class, not a
7081 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7082 or one of its enclosing classes, and that type is a template,
7083 return the associated TEMPLATE_DECL. Otherwise, the original
7086 Also handle the case when DECL is a TREE_LIST of ambiguous
7087 injected-class-names from different bases. */
7090 maybe_get_template_decl_from_type_decl (tree decl
)
7092 if (decl
== NULL_TREE
)
7095 /* DR 176: A lookup that finds an injected-class-name (10.2
7096 [class.member.lookup]) can result in an ambiguity in certain cases
7097 (for example, if it is found in more than one base class). If all of
7098 the injected-class-names that are found refer to specializations of
7099 the same class template, and if the name is followed by a
7100 template-argument-list, the reference refers to the class template
7101 itself and not a specialization thereof, and is not ambiguous. */
7102 if (TREE_CODE (decl
) == TREE_LIST
)
7104 tree t
, tmpl
= NULL_TREE
;
7105 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
7107 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
7110 else if (tmpl
!= elt
)
7113 if (tmpl
&& t
== NULL_TREE
)
7119 return (decl
!= NULL_TREE
7120 && DECL_SELF_REFERENCE_P (decl
)
7121 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
7122 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
7125 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7126 parameters, find the desired type.
7128 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7130 IN_DECL, if non-NULL, is the template declaration we are trying to
7133 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7134 the class we are looking up.
7136 Issue error and warning messages under control of COMPLAIN.
7138 If the template class is really a local class in a template
7139 function, then the FUNCTION_CONTEXT is the function in which it is
7142 ??? Note that this function is currently called *twice* for each
7143 template-id: the first time from the parser, while creating the
7144 incomplete type (finish_template_type), and the second type during the
7145 real instantiation (instantiate_template_class). This is surely something
7146 that we want to avoid. It also causes some problems with argument
7147 coercion (see convert_nontype_argument for more information on this). */
7150 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
7151 int entering_scope
, tsubst_flags_t complain
)
7153 tree templ
= NULL_TREE
, parmlist
;
7160 if (identifier_p (d1
))
7162 tree value
= innermost_non_namespace_value (d1
);
7163 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
7168 push_decl_namespace (context
);
7169 templ
= lookup_name (d1
);
7170 templ
= maybe_get_template_decl_from_type_decl (templ
);
7172 pop_decl_namespace ();
7175 context
= DECL_CONTEXT (templ
);
7177 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
7179 tree type
= TREE_TYPE (d1
);
7181 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7182 an implicit typename for the second A. Deal with it. */
7183 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
7184 type
= TREE_TYPE (type
);
7186 if (CLASSTYPE_TEMPLATE_INFO (type
))
7188 templ
= CLASSTYPE_TI_TEMPLATE (type
);
7189 d1
= DECL_NAME (templ
);
7192 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
7193 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
7195 templ
= TYPE_TI_TEMPLATE (d1
);
7196 d1
= DECL_NAME (templ
);
7198 else if (TREE_CODE (d1
) == TEMPLATE_DECL
7199 && DECL_TEMPLATE_RESULT (d1
)
7200 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
7203 d1
= DECL_NAME (templ
);
7204 context
= DECL_CONTEXT (templ
);
7206 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
7209 d1
= DECL_NAME (templ
);
7212 /* Issue an error message if we didn't find a template. */
7215 if (complain
& tf_error
)
7216 error ("%qT is not a template", d1
);
7217 return error_mark_node
;
7220 if (TREE_CODE (templ
) != TEMPLATE_DECL
7221 /* Make sure it's a user visible template, if it was named by
7223 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
7224 && !PRIMARY_TEMPLATE_P (templ
)))
7226 if (complain
& tf_error
)
7228 error ("non-template type %qT used as a template", d1
);
7230 error ("for template declaration %q+D", in_decl
);
7232 return error_mark_node
;
7235 complain
&= ~tf_user
;
7237 /* An alias that just changes the name of a template is equivalent to the
7238 other template, so if any of the arguments are pack expansions, strip
7239 the alias to avoid problems with a pack expansion passed to a non-pack
7240 alias template parameter (DR 1430). */
7241 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
7242 templ
= get_underlying_template (templ
);
7244 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
7246 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7247 template arguments */
7253 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7255 /* Consider an example where a template template parameter declared as
7257 template <class T, class U = std::allocator<T> > class TT
7259 The template parameter level of T and U are one level larger than
7260 of TT. To proper process the default argument of U, say when an
7261 instantiation `TT<int>' is seen, we need to build the full
7262 arguments containing {int} as the innermost level. Outer levels,
7263 available when not appearing as default template argument, can be
7264 obtained from the arguments of the enclosing template.
7266 Suppose that TT is later substituted with std::vector. The above
7267 instantiation is `TT<int, std::allocator<T> >' with TT at
7268 level 1, and T at level 2, while the template arguments at level 1
7269 becomes {std::vector} and the inner level 2 is {int}. */
7271 outer
= DECL_CONTEXT (templ
);
7273 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7274 else if (current_template_parms
)
7275 /* This is an argument of the current template, so we haven't set
7276 DECL_CONTEXT yet. */
7277 outer
= current_template_args ();
7280 arglist
= add_to_template_args (outer
, arglist
);
7282 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
7284 /*require_all_args=*/true,
7285 /*use_default_args=*/true);
7286 if (arglist2
== error_mark_node
7287 || (!uses_template_parms (arglist2
)
7288 && check_instantiated_args (templ
, arglist2
, complain
)))
7289 return error_mark_node
;
7291 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
7296 tree template_type
= TREE_TYPE (templ
);
7299 tree found
= NULL_TREE
;
7302 int is_dependent_type
;
7303 int use_partial_inst_tmpl
= false;
7305 if (template_type
== error_mark_node
)
7306 /* An error occurred while building the template TEMPL, and a
7307 diagnostic has most certainly been emitted for that
7308 already. Let's propagate that error. */
7309 return error_mark_node
;
7311 gen_tmpl
= most_general_template (templ
);
7312 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7313 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
7314 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7316 if (arg_depth
== 1 && parm_depth
> 1)
7318 /* We've been given an incomplete set of template arguments.
7321 template <class T> struct S1 {
7322 template <class U> struct S2 {};
7323 template <class U> struct S2<U*> {};
7326 we will be called with an ARGLIST of `U*', but the
7327 TEMPLATE will be `template <class T> template
7328 <class U> struct S1<T>::S2'. We must fill in the missing
7331 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
7333 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7336 /* Now we should have enough arguments. */
7337 gcc_assert (parm_depth
== arg_depth
);
7339 /* From here on, we're only interested in the most general
7342 /* Calculate the BOUND_ARGS. These will be the args that are
7343 actually tsubst'd into the definition to create the
7347 /* We have multiple levels of arguments to coerce, at once. */
7349 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
7351 tree bound_args
= make_tree_vec (parm_depth
);
7353 for (i
= saved_depth
,
7354 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7355 i
> 0 && t
!= NULL_TREE
;
7356 --i
, t
= TREE_CHAIN (t
))
7359 if (i
== saved_depth
)
7360 a
= coerce_template_parms (TREE_VALUE (t
),
7363 /*require_all_args=*/true,
7364 /*use_default_args=*/true);
7366 /* Outer levels should have already been coerced. */
7367 a
= TMPL_ARGS_LEVEL (arglist
, i
);
7369 /* Don't process further if one of the levels fails. */
7370 if (a
== error_mark_node
)
7372 /* Restore the ARGLIST to its full size. */
7373 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7374 return error_mark_node
;
7377 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
7379 /* We temporarily reduce the length of the ARGLIST so
7380 that coerce_template_parms will see only the arguments
7381 corresponding to the template parameters it is
7383 TREE_VEC_LENGTH (arglist
)--;
7386 /* Restore the ARGLIST to its full size. */
7387 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7389 arglist
= bound_args
;
7393 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
7394 INNERMOST_TEMPLATE_ARGS (arglist
),
7397 /*require_all_args=*/true,
7398 /*use_default_args=*/true);
7400 if (arglist
== error_mark_node
)
7401 /* We were unable to bind the arguments. */
7402 return error_mark_node
;
7404 /* In the scope of a template class, explicit references to the
7405 template class refer to the type of the template, not any
7406 instantiation of it. For example, in:
7408 template <class T> class C { void f(C<T>); }
7410 the `C<T>' is just the same as `C'. Outside of the
7411 class, however, such a reference is an instantiation. */
7413 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
7414 || currently_open_class (template_type
))
7415 /* comp_template_args is expensive, check it last. */
7416 && comp_template_args (TYPE_TI_ARGS (template_type
),
7418 return template_type
;
7420 /* If we already have this specialization, return it. */
7421 elt
.tmpl
= gen_tmpl
;
7423 hash
= hash_specialization (&elt
);
7424 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
7430 is_dependent_type
= uses_template_parms (arglist
);
7432 /* If the deduced arguments are invalid, then the binding
7434 if (!is_dependent_type
7435 && check_instantiated_args (gen_tmpl
,
7436 INNERMOST_TEMPLATE_ARGS (arglist
),
7438 return error_mark_node
;
7440 if (!is_dependent_type
7441 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
7442 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
7443 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
7445 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
7446 DECL_NAME (gen_tmpl
),
7447 /*tag_scope=*/ts_global
);
7451 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
7453 if (context
== error_mark_node
)
7454 return error_mark_node
;
7457 context
= global_namespace
;
7459 /* Create the type. */
7460 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
7462 if (!is_dependent_type
)
7464 set_current_access_from_decl (TYPE_NAME (template_type
));
7465 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
7466 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
7467 arglist
, complain
, in_decl
),
7468 SCOPED_ENUM_P (template_type
), NULL
);
7472 /* We don't want to call start_enum for this type, since
7473 the values for the enumeration constants may involve
7474 template parameters. And, no one should be interested
7475 in the enumeration constants for such a type. */
7476 t
= cxx_make_type (ENUMERAL_TYPE
);
7477 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
7479 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
7480 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
7481 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7483 else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7485 /* The user referred to a specialization of an alias
7486 template represented by GEN_TMPL.
7488 [temp.alias]/2 says:
7490 When a template-id refers to the specialization of an
7491 alias template, it is equivalent to the associated
7492 type obtained by substitution of its
7493 template-arguments for the template-parameters in the
7494 type-id of the alias template. */
7496 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
7497 /* Note that the call above (by indirectly calling
7498 register_specialization in tsubst_decl) registers the
7499 TYPE_DECL representing the specialization of the alias
7500 template. So next time someone substitutes ARGLIST for
7501 the template parms into the alias template (GEN_TMPL),
7502 she'll get that TYPE_DECL back. */
7504 if (t
== error_mark_node
)
7507 else if (CLASS_TYPE_P (template_type
))
7509 t
= make_class_type (TREE_CODE (template_type
));
7510 CLASSTYPE_DECLARED_CLASS (t
)
7511 = CLASSTYPE_DECLARED_CLASS (template_type
);
7512 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7513 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7515 /* A local class. Make sure the decl gets registered properly. */
7516 if (context
== current_function_decl
)
7517 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7519 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7520 /* This instantiation is another name for the primary
7521 template type. Set the TYPE_CANONICAL field
7523 TYPE_CANONICAL (t
) = template_type
;
7524 else if (any_template_arguments_need_structural_equality_p (arglist
))
7525 /* Some of the template arguments require structural
7526 equality testing, so this template class requires
7527 structural equality testing. */
7528 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7533 /* If we called start_enum or pushtag above, this information
7534 will already be set up. */
7537 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
7539 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
7540 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
7541 DECL_SOURCE_LOCATION (type_decl
)
7542 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
7545 type_decl
= TYPE_NAME (t
);
7547 if (CLASS_TYPE_P (template_type
))
7549 TREE_PRIVATE (type_decl
)
7550 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
7551 TREE_PROTECTED (type_decl
)
7552 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
7553 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
7555 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
7556 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
7560 /* Let's consider the explicit specialization of a member
7561 of a class template specialization that is implicitely instantiated,
7566 template<class U> struct M {}; //#0
7571 struct S<int>::M<char> //#1
7575 [temp.expl.spec]/4 says this is valid.
7577 In this case, when we write:
7580 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7583 When we encounter #1, we want to store the partial instantiation
7584 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7586 For all cases other than this "explicit specialization of member of a
7587 class template", we just want to store the most general template into
7588 the CLASSTYPE_TI_TEMPLATE of M.
7590 This case of "explicit specialization of member of a class template"
7592 1/ the enclosing class is an instantiation of, and therefore not
7593 the same as, the context of the most general template, and
7594 2/ we aren't looking at the partial instantiation itself, i.e.
7595 the innermost arguments are not the same as the innermost parms of
7596 the most general template.
7598 So it's only when 1/ and 2/ happens that we want to use the partial
7599 instantiation of the member template in lieu of its most general
7602 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
7603 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
7604 /* the enclosing class must be an instantiation... */
7605 && CLASS_TYPE_P (context
)
7606 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
7608 tree partial_inst_args
;
7609 TREE_VEC_LENGTH (arglist
)--;
7610 ++processing_template_decl
;
7612 tsubst (INNERMOST_TEMPLATE_ARGS
7613 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
7614 arglist
, complain
, NULL_TREE
);
7615 --processing_template_decl
;
7616 TREE_VEC_LENGTH (arglist
)++;
7617 use_partial_inst_tmpl
=
7618 /*...and we must not be looking at the partial instantiation
7620 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
7624 if (!use_partial_inst_tmpl
)
7625 /* This case is easy; there are no member templates involved. */
7629 /* This is a full instantiation of a member template. Find
7630 the partial instantiation of which this is an instance. */
7632 /* Temporarily reduce by one the number of levels in the ARGLIST
7633 so as to avoid comparing the last set of arguments. */
7634 TREE_VEC_LENGTH (arglist
)--;
7635 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
7636 TREE_VEC_LENGTH (arglist
)++;
7637 /* FOUND is either a proper class type, or an alias
7638 template specialization. In the later case, it's a
7639 TYPE_DECL, resulting from the substituting of arguments
7640 for parameters in the TYPE_DECL of the alias template
7641 done earlier. So be careful while getting the template
7643 found
= TREE_CODE (found
) == TYPE_DECL
7644 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
7645 : CLASSTYPE_TI_TEMPLATE (found
);
7648 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
7651 slot
= htab_find_slot_with_hash (type_specializations
,
7652 &elt
, hash
, INSERT
);
7653 entry
= ggc_alloc_spec_entry ();
7657 /* Note this use of the partial instantiation so we can check it
7658 later in maybe_process_partial_specialization. */
7659 DECL_TEMPLATE_INSTANTIATIONS (templ
)
7660 = tree_cons (arglist
, t
,
7661 DECL_TEMPLATE_INSTANTIATIONS (templ
));
7663 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
)
7664 /* Now that the type has been registered on the instantiations
7665 list, we set up the enumerators. Because the enumeration
7666 constants may involve the enumeration type itself, we make
7667 sure to register the type first, and then create the
7668 constants. That way, doing tsubst_expr for the enumeration
7669 constants won't result in recursive calls here; we'll find
7670 the instantiation and exit above. */
7671 tsubst_enum (template_type
, t
, arglist
);
7673 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
7674 /* If the type makes use of template parameters, the
7675 code that generates debugging information will crash. */
7676 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
7678 /* Possibly limit visibility based on template args. */
7679 TREE_PUBLIC (type_decl
) = 1;
7680 determine_visibility (type_decl
);
7686 /* Wrapper for lookup_template_class_1. */
7689 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
7690 int entering_scope
, tsubst_flags_t complain
)
7693 timevar_push (TV_TEMPLATE_INST
);
7694 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
7695 entering_scope
, complain
);
7696 timevar_pop (TV_TEMPLATE_INST
);
7704 /* True when we should also visit template parameters that occur in
7705 non-deduced contexts. */
7706 bool include_nondeduced_p
;
7707 struct pointer_set_t
*visited
;
7710 /* Called from for_each_template_parm via walk_tree. */
7713 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
7716 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
7717 tree_fn_t fn
= pfd
->fn
;
7718 void *data
= pfd
->data
;
7721 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
7722 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
7723 pfd
->include_nondeduced_p
))
7724 return error_mark_node
;
7726 switch (TREE_CODE (t
))
7729 if (TYPE_PTRMEMFUNC_P (t
))
7735 if (!TYPE_TEMPLATE_INFO (t
))
7737 else if (for_each_template_parm (TYPE_TI_ARGS (t
),
7738 fn
, data
, pfd
->visited
,
7739 pfd
->include_nondeduced_p
))
7740 return error_mark_node
;
7744 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
7745 fn
, data
, pfd
->visited
,
7746 pfd
->include_nondeduced_p
)
7747 || for_each_template_parm (TYPE_MAX_VALUE (t
),
7748 fn
, data
, pfd
->visited
,
7749 pfd
->include_nondeduced_p
))
7750 return error_mark_node
;
7754 /* Since we're not going to walk subtrees, we have to do this
7756 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
7757 pfd
->visited
, pfd
->include_nondeduced_p
))
7758 return error_mark_node
;
7762 /* Check the return type. */
7763 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7764 pfd
->include_nondeduced_p
))
7765 return error_mark_node
;
7767 /* Check the parameter types. Since default arguments are not
7768 instantiated until they are needed, the TYPE_ARG_TYPES may
7769 contain expressions that involve template parameters. But,
7770 no-one should be looking at them yet. And, once they're
7771 instantiated, they don't contain template parameters, so
7772 there's no point in looking at them then, either. */
7776 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
7777 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
7778 pfd
->visited
, pfd
->include_nondeduced_p
))
7779 return error_mark_node
;
7781 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7782 want walk_tree walking into them itself. */
7788 case UNDERLYING_TYPE
:
7789 if (pfd
->include_nondeduced_p
7790 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
7792 pfd
->include_nondeduced_p
))
7793 return error_mark_node
;
7798 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
7799 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
7800 pfd
->visited
, pfd
->include_nondeduced_p
))
7801 return error_mark_node
;
7806 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
7807 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
7808 pfd
->visited
, pfd
->include_nondeduced_p
))
7809 return error_mark_node
;
7810 if (DECL_CONTEXT (t
)
7811 && pfd
->include_nondeduced_p
7812 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
7813 pfd
->visited
, pfd
->include_nondeduced_p
))
7814 return error_mark_node
;
7817 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7818 /* Record template parameters such as `T' inside `TT<T>'. */
7819 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
7820 pfd
->include_nondeduced_p
))
7821 return error_mark_node
;
7824 case TEMPLATE_TEMPLATE_PARM
:
7825 case TEMPLATE_TYPE_PARM
:
7826 case TEMPLATE_PARM_INDEX
:
7827 if (fn
&& (*fn
)(t
, data
))
7828 return error_mark_node
;
7830 return error_mark_node
;
7834 /* A template template parameter is encountered. */
7835 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
7836 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7837 pfd
->include_nondeduced_p
))
7838 return error_mark_node
;
7840 /* Already substituted template template parameter */
7846 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
7848 pfd
->include_nondeduced_p
))
7849 return error_mark_node
;
7853 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
7854 && pfd
->include_nondeduced_p
7855 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7856 (TREE_TYPE (t
)), fn
, data
,
7857 pfd
->visited
, pfd
->include_nondeduced_p
))
7858 return error_mark_node
;
7863 /* If there's no type, then this thing must be some expression
7864 involving template parameters. */
7865 if (!fn
&& !TREE_TYPE (t
))
7866 return error_mark_node
;
7871 case IMPLICIT_CONV_EXPR
:
7872 case REINTERPRET_CAST_EXPR
:
7873 case CONST_CAST_EXPR
:
7874 case STATIC_CAST_EXPR
:
7875 case DYNAMIC_CAST_EXPR
:
7879 case PSEUDO_DTOR_EXPR
:
7881 return error_mark_node
;
7888 /* We didn't find any template parameters we liked. */
7892 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7893 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7894 call FN with the parameter and the DATA.
7895 If FN returns nonzero, the iteration is terminated, and
7896 for_each_template_parm returns 1. Otherwise, the iteration
7897 continues. If FN never returns a nonzero value, the value
7898 returned by for_each_template_parm is 0. If FN is NULL, it is
7899 considered to be the function which always returns 1.
7901 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7902 parameters that occur in non-deduced contexts. When false, only
7903 visits those template parameters that can be deduced. */
7906 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
7907 struct pointer_set_t
*visited
,
7908 bool include_nondeduced_p
)
7910 struct pair_fn_data pfd
;
7916 pfd
.include_nondeduced_p
= include_nondeduced_p
;
7918 /* Walk the tree. (Conceptually, we would like to walk without
7919 duplicates, but for_each_template_parm_r recursively calls
7920 for_each_template_parm, so we would need to reorganize a fair
7921 bit to use walk_tree_without_duplicates, so we keep our own
7924 pfd
.visited
= visited
;
7926 pfd
.visited
= pointer_set_create ();
7927 result
= cp_walk_tree (&t
,
7928 for_each_template_parm_r
,
7930 pfd
.visited
) != NULL_TREE
;
7935 pointer_set_destroy (pfd
.visited
);
7942 /* Returns true if T depends on any template parameter. */
7945 uses_template_parms (tree t
)
7948 int saved_processing_template_decl
;
7950 saved_processing_template_decl
= processing_template_decl
;
7951 if (!saved_processing_template_decl
)
7952 processing_template_decl
= 1;
7954 dependent_p
= dependent_type_p (t
);
7955 else if (TREE_CODE (t
) == TREE_VEC
)
7956 dependent_p
= any_dependent_template_arguments_p (t
);
7957 else if (TREE_CODE (t
) == TREE_LIST
)
7958 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
7959 || uses_template_parms (TREE_CHAIN (t
)));
7960 else if (TREE_CODE (t
) == TYPE_DECL
)
7961 dependent_p
= dependent_type_p (TREE_TYPE (t
));
7964 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
7965 || TREE_CODE (t
) == OVERLOAD
7968 || TREE_CODE (t
) == TRAIT_EXPR
7969 || TREE_CODE (t
) == CONSTRUCTOR
7970 || CONSTANT_CLASS_P (t
))
7971 dependent_p
= (type_dependent_expression_p (t
)
7972 || value_dependent_expression_p (t
));
7975 gcc_assert (t
== error_mark_node
);
7976 dependent_p
= false;
7979 processing_template_decl
= saved_processing_template_decl
;
7984 /* Returns true iff current_function_decl is an incompletely instantiated
7985 template. Useful instead of processing_template_decl because the latter
7986 is set to 0 during fold_non_dependent_expr. */
7989 in_template_function (void)
7991 tree fn
= current_function_decl
;
7993 ++processing_template_decl
;
7994 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
7995 && DECL_TEMPLATE_INFO (fn
)
7996 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
7997 --processing_template_decl
;
8001 /* Returns true if T depends on any template parameter with level LEVEL. */
8004 uses_template_parms_level (tree t
, int level
)
8006 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
8007 /*include_nondeduced_p=*/true);
8010 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8011 ill-formed translation unit, i.e. a variable or function that isn't
8012 usable in a constant expression. */
8015 neglectable_inst_p (tree d
)
8018 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
8019 : decl_maybe_constant_var_p (d
)));
8022 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8023 neglectable and instantiated from within an erroneous instantiation. */
8026 limit_bad_template_recursion (tree decl
)
8028 struct tinst_level
*lev
= current_tinst_level
;
8029 int errs
= errorcount
+ sorrycount
;
8030 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
8033 for (; lev
; lev
= lev
->next
)
8034 if (neglectable_inst_p (lev
->decl
))
8037 return (lev
&& errs
> lev
->errors
);
8040 static int tinst_depth
;
8041 extern int max_tinst_depth
;
8044 static GTY(()) struct tinst_level
*last_error_tinst_level
;
8046 /* We're starting to instantiate D; record the template instantiation context
8047 for diagnostics and to restore it later. */
8050 push_tinst_level (tree d
)
8052 struct tinst_level
*new_level
;
8054 if (tinst_depth
>= max_tinst_depth
)
8056 last_error_tinst_level
= current_tinst_level
;
8057 if (TREE_CODE (d
) == TREE_LIST
)
8058 error ("template instantiation depth exceeds maximum of %d (use "
8059 "-ftemplate-depth= to increase the maximum) substituting %qS",
8060 max_tinst_depth
, d
);
8062 error ("template instantiation depth exceeds maximum of %d (use "
8063 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8064 max_tinst_depth
, d
);
8066 print_instantiation_context ();
8071 /* If the current instantiation caused problems, don't let it instantiate
8072 anything else. Do allow deduction substitution and decls usable in
8073 constant expressions. */
8074 if (limit_bad_template_recursion (d
))
8077 new_level
= ggc_alloc_tinst_level ();
8078 new_level
->decl
= d
;
8079 new_level
->locus
= input_location
;
8080 new_level
->errors
= errorcount
+sorrycount
;
8081 new_level
->in_system_header_p
= in_system_header
;
8082 new_level
->next
= current_tinst_level
;
8083 current_tinst_level
= new_level
;
8086 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
8087 depth_reached
= tinst_depth
;
8092 /* We're done instantiating this template; return to the instantiation
8096 pop_tinst_level (void)
8098 /* Restore the filename and line number stashed away when we started
8099 this instantiation. */
8100 input_location
= current_tinst_level
->locus
;
8101 current_tinst_level
= current_tinst_level
->next
;
8105 /* We're instantiating a deferred template; restore the template
8106 instantiation context in which the instantiation was requested, which
8107 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8110 reopen_tinst_level (struct tinst_level
*level
)
8112 struct tinst_level
*t
;
8115 for (t
= level
; t
; t
= t
->next
)
8118 current_tinst_level
= level
;
8120 if (current_tinst_level
)
8121 current_tinst_level
->errors
= errorcount
+sorrycount
;
8125 /* Returns the TINST_LEVEL which gives the original instantiation
8128 struct tinst_level
*
8129 outermost_tinst_level (void)
8131 struct tinst_level
*level
= current_tinst_level
;
8134 level
= level
->next
;
8138 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8139 vector of template arguments, as for tsubst.
8141 Returns an appropriate tsubst'd friend declaration. */
8144 tsubst_friend_function (tree decl
, tree args
)
8148 if (TREE_CODE (decl
) == FUNCTION_DECL
8149 && DECL_TEMPLATE_INSTANTIATION (decl
)
8150 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
8151 /* This was a friend declared with an explicit template
8152 argument list, e.g.:
8156 to indicate that f was a template instantiation, not a new
8157 function declaration. Now, we have to figure out what
8158 instantiation of what template. */
8160 tree template_id
, arglist
, fns
;
8163 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
8165 /* Friend functions are looked up in the containing namespace scope.
8166 We must enter that scope, to avoid finding member functions of the
8167 current class with same name. */
8168 push_nested_namespace (ns
);
8169 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
8170 tf_warning_or_error
, NULL_TREE
,
8171 /*integral_constant_expression_p=*/false);
8172 pop_nested_namespace (ns
);
8173 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
8174 tf_warning_or_error
, NULL_TREE
);
8175 template_id
= lookup_template_function (fns
, arglist
);
8177 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8178 tmpl
= determine_specialization (template_id
, new_friend
,
8180 /*need_member_template=*/0,
8181 TREE_VEC_LENGTH (args
),
8183 return instantiate_template (tmpl
, new_args
, tf_error
);
8186 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8188 /* The NEW_FRIEND will look like an instantiation, to the
8189 compiler, but is not an instantiation from the point of view of
8190 the language. For example, we might have had:
8192 template <class T> struct S {
8193 template <class U> friend void f(T, U);
8196 Then, in S<int>, template <class U> void f(int, U) is not an
8197 instantiation of anything. */
8198 if (new_friend
== error_mark_node
)
8199 return error_mark_node
;
8201 DECL_USE_TEMPLATE (new_friend
) = 0;
8202 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8204 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
8205 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
8206 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
8209 /* The mangled name for the NEW_FRIEND is incorrect. The function
8210 is not a template instantiation and should not be mangled like
8211 one. Therefore, we forget the mangling here; we'll recompute it
8212 later if we need it. */
8213 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
8215 SET_DECL_RTL (new_friend
, NULL
);
8216 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
8219 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
8222 tree new_friend_template_info
;
8223 tree new_friend_result_template_info
;
8225 int new_friend_is_defn
;
8227 /* We must save some information from NEW_FRIEND before calling
8228 duplicate decls since that function will free NEW_FRIEND if
8230 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
8231 new_friend_is_defn
=
8232 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8233 (template_for_substitution (new_friend
)))
8235 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
8237 /* This declaration is a `primary' template. */
8238 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
8240 new_friend_result_template_info
8241 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
8244 new_friend_result_template_info
= NULL_TREE
;
8246 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8247 if (new_friend_is_defn
)
8248 DECL_INITIAL (new_friend
) = error_mark_node
;
8250 /* Inside pushdecl_namespace_level, we will push into the
8251 current namespace. However, the friend function should go
8252 into the namespace of the template. */
8253 ns
= decl_namespace_context (new_friend
);
8254 push_nested_namespace (ns
);
8255 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
8256 pop_nested_namespace (ns
);
8258 if (old_decl
== error_mark_node
)
8259 return error_mark_node
;
8261 if (old_decl
!= new_friend
)
8263 /* This new friend declaration matched an existing
8264 declaration. For example, given:
8266 template <class T> void f(T);
8267 template <class U> class C {
8268 template <class T> friend void f(T) {}
8271 the friend declaration actually provides the definition
8272 of `f', once C has been instantiated for some type. So,
8273 old_decl will be the out-of-class template declaration,
8274 while new_friend is the in-class definition.
8276 But, if `f' was called before this point, the
8277 instantiation of `f' will have DECL_TI_ARGS corresponding
8278 to `T' but not to `U', references to which might appear
8279 in the definition of `f'. Previously, the most general
8280 template for an instantiation of `f' was the out-of-class
8281 version; now it is the in-class version. Therefore, we
8282 run through all specialization of `f', adding to their
8283 DECL_TI_ARGS appropriately. In particular, they need a
8284 new set of outer arguments, corresponding to the
8285 arguments for this class instantiation.
8287 The same situation can arise with something like this:
8290 template <class T> class C {
8294 when `C<int>' is instantiated. Now, `f(int)' is defined
8297 if (!new_friend_is_defn
)
8298 /* On the other hand, if the in-class declaration does
8299 *not* provide a definition, then we don't want to alter
8300 existing definitions. We can just leave everything
8305 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
8306 tree new_args
= TI_ARGS (new_friend_template_info
);
8308 /* Overwrite whatever template info was there before, if
8309 any, with the new template information pertaining to
8311 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
8313 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
8315 /* We should have called reregister_specialization in
8317 gcc_assert (retrieve_specialization (new_template
,
8321 /* Instantiate it if the global has already been used. */
8322 if (DECL_ODR_USED (old_decl
))
8323 instantiate_decl (old_decl
, /*defer_ok=*/true,
8324 /*expl_inst_class_mem_p=*/false);
8330 /* Indicate that the old function template is a partial
8332 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
8333 = new_friend_result_template_info
;
8335 gcc_assert (new_template
8336 == most_general_template (new_template
));
8337 gcc_assert (new_template
!= old_decl
);
8339 /* Reassign any specializations already in the hash table
8340 to the new more general template, and add the
8341 additional template args. */
8342 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
8346 tree spec
= TREE_VALUE (t
);
8349 elt
.tmpl
= old_decl
;
8350 elt
.args
= DECL_TI_ARGS (spec
);
8351 elt
.spec
= NULL_TREE
;
8353 htab_remove_elt (decl_specializations
, &elt
);
8356 = add_outermost_template_args (new_args
,
8357 DECL_TI_ARGS (spec
));
8359 register_specialization
8360 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
8363 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
8367 /* The information from NEW_FRIEND has been merged into OLD_DECL
8368 by duplicate_decls. */
8369 new_friend
= old_decl
;
8374 tree context
= DECL_CONTEXT (new_friend
);
8378 template <class T> class C {
8379 template <class U> friend void C1<U>::f (); // case 1
8380 friend void C2<T>::f (); // case 2
8382 we only need to make sure CONTEXT is a complete type for
8383 case 2. To distinguish between the two cases, we note that
8384 CONTEXT of case 1 remains dependent type after tsubst while
8385 this isn't true for case 2. */
8386 ++processing_template_decl
;
8387 dependent_p
= dependent_type_p (context
);
8388 --processing_template_decl
;
8391 && !complete_type_or_else (context
, NULL_TREE
))
8392 return error_mark_node
;
8394 if (COMPLETE_TYPE_P (context
))
8396 /* Check to see that the declaration is really present, and,
8397 possibly obtain an improved declaration. */
8398 tree fn
= check_classfn (context
,
8399 new_friend
, NULL_TREE
);
8409 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8410 template arguments, as for tsubst.
8412 Returns an appropriate tsubst'd friend type or error_mark_node on
8416 tsubst_friend_class (tree friend_tmpl
, tree args
)
8422 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
8424 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
8425 return TREE_TYPE (t
);
8428 context
= CP_DECL_CONTEXT (friend_tmpl
);
8430 if (context
!= global_namespace
)
8432 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8433 push_nested_namespace (context
);
8435 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
8438 /* Look for a class template declaration. We look for hidden names
8439 because two friend declarations of the same template are the
8440 same. For example, in:
8443 template <typename> friend class F;
8445 template <typename> struct B {
8446 template <typename> friend class F;
8449 both F templates are the same. */
8450 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
8451 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
8453 /* But, if we don't find one, it might be because we're in a
8454 situation like this:
8462 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8463 for `S<int>', not the TEMPLATE_DECL. */
8464 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
8466 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
8467 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
8470 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
8472 /* The friend template has already been declared. Just
8473 check to see that the declarations match, and install any new
8474 default parameters. We must tsubst the default parameters,
8475 of course. We only need the innermost template parameters
8476 because that is all that redeclare_class_template will look
8478 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
8479 > TMPL_ARGS_DEPTH (args
))
8482 location_t saved_input_location
;
8483 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
8484 args
, tf_warning_or_error
);
8486 saved_input_location
= input_location
;
8487 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
8488 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
8489 input_location
= saved_input_location
;
8493 friend_type
= TREE_TYPE (tmpl
);
8497 /* The friend template has not already been declared. In this
8498 case, the instantiation of the template class will cause the
8499 injection of this template into the global scope. */
8500 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
8501 if (tmpl
== error_mark_node
)
8502 return error_mark_node
;
8504 /* The new TMPL is not an instantiation of anything, so we
8505 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8506 the new type because that is supposed to be the corresponding
8507 template decl, i.e., TMPL. */
8508 DECL_USE_TEMPLATE (tmpl
) = 0;
8509 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
8510 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
8511 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
8512 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
8514 /* Inject this template into the global scope. */
8515 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
8518 if (context
!= global_namespace
)
8520 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8521 pop_nested_namespace (context
);
8523 pop_nested_class ();
8529 /* Returns zero if TYPE cannot be completed later due to circularity.
8530 Otherwise returns one. */
8533 can_complete_type_without_circularity (tree type
)
8535 if (type
== NULL_TREE
|| type
== error_mark_node
)
8537 else if (COMPLETE_TYPE_P (type
))
8539 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
8540 return can_complete_type_without_circularity (TREE_TYPE (type
));
8541 else if (CLASS_TYPE_P (type
)
8542 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
8548 static tree
tsubst_omp_clauses (tree
, bool, tree
, tsubst_flags_t
, tree
);
8550 /* Apply any attributes which had to be deferred until instantiation
8551 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8552 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8555 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
8556 tree args
, tsubst_flags_t complain
, tree in_decl
)
8558 tree last_dep
= NULL_TREE
;
8562 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
8563 if (ATTR_IS_DEPENDENT (t
))
8566 attributes
= copy_list (attributes
);
8570 if (DECL_P (*decl_p
))
8572 if (TREE_TYPE (*decl_p
) == error_mark_node
)
8574 p
= &DECL_ATTRIBUTES (*decl_p
);
8577 p
= &TYPE_ATTRIBUTES (*decl_p
);
8581 tree late_attrs
= NULL_TREE
;
8582 tree
*q
= &late_attrs
;
8584 for (*p
= attributes
; *p
; )
8587 if (ATTR_IS_DEPENDENT (t
))
8589 *p
= TREE_CHAIN (t
);
8590 TREE_CHAIN (t
) = NULL_TREE
;
8592 && is_attribute_p ("omp declare simd",
8593 get_attribute_name (t
))
8596 tree clauses
= TREE_VALUE (TREE_VALUE (t
));
8597 clauses
= tsubst_omp_clauses (clauses
, true, args
,
8599 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
8600 clauses
= finish_omp_clauses (clauses
);
8601 tree parms
= DECL_ARGUMENTS (*decl_p
);
8603 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
8605 TREE_VALUE (TREE_VALUE (t
)) = clauses
;
8607 TREE_VALUE (t
) = NULL_TREE
;
8609 /* If the first attribute argument is an identifier, don't
8610 pass it through tsubst. Attributes like mode, format,
8611 cleanup and several target specific attributes expect it
8613 else if (attribute_takes_identifier_p (TREE_PURPOSE (t
)))
8616 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
8618 /*integral_constant_expression_p=*/false);
8619 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
8621 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
8626 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
8627 /*integral_constant_expression_p=*/false);
8629 q
= &TREE_CHAIN (t
);
8632 p
= &TREE_CHAIN (t
);
8635 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
8639 /* Perform (or defer) access check for typedefs that were referenced
8640 from within the template TMPL code.
8641 This is a subroutine of instantiate_decl and instantiate_class_template.
8642 TMPL is the template to consider and TARGS is the list of arguments of
8646 perform_typedefs_access_check (tree tmpl
, tree targs
)
8648 location_t saved_location
;
8650 qualified_typedef_usage_t
*iter
;
8653 || (!CLASS_TYPE_P (tmpl
)
8654 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
8657 saved_location
= input_location
;
8658 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
8660 tree type_decl
= iter
->typedef_decl
;
8661 tree type_scope
= iter
->context
;
8663 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
8666 if (uses_template_parms (type_decl
))
8667 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
8668 if (uses_template_parms (type_scope
))
8669 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
8671 /* Make access check error messages point to the location
8672 of the use of the typedef. */
8673 input_location
= iter
->locus
;
8674 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
8675 type_decl
, type_decl
,
8676 tf_warning_or_error
);
8678 input_location
= saved_location
;
8682 instantiate_class_template_1 (tree type
)
8684 tree templ
, args
, pattern
, t
, member
;
8688 unsigned int saved_maximum_field_alignment
;
8691 if (type
== error_mark_node
)
8692 return error_mark_node
;
8694 if (COMPLETE_OR_OPEN_TYPE_P (type
)
8695 || uses_template_parms (type
))
8698 /* Figure out which template is being instantiated. */
8699 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
8700 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
8702 /* Determine what specialization of the original template to
8704 t
= most_specialized_class (type
, templ
, tf_warning_or_error
);
8705 if (t
== error_mark_node
)
8707 TYPE_BEING_DEFINED (type
) = 1;
8708 return error_mark_node
;
8712 /* This TYPE is actually an instantiation of a partial
8713 specialization. We replace the innermost set of ARGS with
8714 the arguments appropriate for substitution. For example,
8717 template <class T> struct S {};
8718 template <class T> struct S<T*> {};
8720 and supposing that we are instantiating S<int*>, ARGS will
8721 presently be {int*} -- but we need {int}. */
8722 pattern
= TREE_TYPE (t
);
8723 args
= TREE_PURPOSE (t
);
8727 pattern
= TREE_TYPE (templ
);
8728 args
= CLASSTYPE_TI_ARGS (type
);
8731 /* If the template we're instantiating is incomplete, then clearly
8732 there's nothing we can do. */
8733 if (!COMPLETE_TYPE_P (pattern
))
8736 /* If we've recursively instantiated too many templates, stop. */
8737 if (! push_tinst_level (type
))
8740 /* Now we're really doing the instantiation. Mark the type as in
8741 the process of being defined. */
8742 TYPE_BEING_DEFINED (type
) = 1;
8744 /* We may be in the middle of deferred access check. Disable
8746 push_deferring_access_checks (dk_no_deferred
);
8748 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
8750 push_to_top_level ();
8751 /* Use #pragma pack from the template context. */
8752 saved_maximum_field_alignment
= maximum_field_alignment
;
8753 maximum_field_alignment
= TYPE_PRECISION (pattern
);
8755 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
8757 /* Set the input location to the most specialized template definition.
8758 This is needed if tsubsting causes an error. */
8759 typedecl
= TYPE_MAIN_DECL (pattern
);
8760 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
8761 DECL_SOURCE_LOCATION (typedecl
);
8763 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
8764 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
8765 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
8766 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
8767 if (ANON_AGGR_TYPE_P (pattern
))
8768 SET_ANON_AGGR_TYPE_P (type
);
8769 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
8771 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
8772 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
8773 /* Adjust visibility for template arguments. */
8774 determine_visibility (TYPE_MAIN_DECL (type
));
8776 if (CLASS_TYPE_P (type
))
8777 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
8779 pbinfo
= TYPE_BINFO (pattern
);
8781 /* We should never instantiate a nested class before its enclosing
8782 class; we need to look up the nested class by name before we can
8783 instantiate it, and that lookup should instantiate the enclosing
8785 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
8786 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
8788 base_list
= NULL_TREE
;
8789 if (BINFO_N_BASE_BINFOS (pbinfo
))
8795 /* We must enter the scope containing the type, as that is where
8796 the accessibility of types named in dependent bases are
8798 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
8800 /* Substitute into each of the bases to determine the actual
8802 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
8805 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
8806 tree expanded_bases
= NULL_TREE
;
8809 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
8812 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
8813 args
, tf_error
, NULL_TREE
);
8814 if (expanded_bases
== error_mark_node
)
8817 len
= TREE_VEC_LENGTH (expanded_bases
);
8820 for (idx
= 0; idx
< len
; idx
++)
8823 /* Extract the already-expanded base class. */
8824 base
= TREE_VEC_ELT (expanded_bases
, idx
);
8826 /* Substitute to figure out the base class. */
8827 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
8830 if (base
== error_mark_node
)
8833 base_list
= tree_cons (access
, base
, base_list
);
8834 if (BINFO_VIRTUAL_P (pbase_binfo
))
8835 TREE_TYPE (base_list
) = integer_type_node
;
8839 /* The list is now in reverse order; correct that. */
8840 base_list
= nreverse (base_list
);
8843 pop_scope (pushed_scope
);
8845 /* Now call xref_basetypes to set up all the base-class
8847 xref_basetypes (type
, base_list
);
8849 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
8850 (int) ATTR_FLAG_TYPE_IN_PLACE
,
8851 args
, tf_error
, NULL_TREE
);
8852 fixup_attribute_variants (type
);
8854 /* Now that our base classes are set up, enter the scope of the
8855 class, so that name lookups into base classes, etc. will work
8856 correctly. This is precisely analogous to what we do in
8857 begin_class_definition when defining an ordinary non-template
8858 class, except we also need to push the enclosing classes. */
8859 push_nested_class (type
);
8861 /* Now members are processed in the order of declaration. */
8862 for (member
= CLASSTYPE_DECL_LIST (pattern
);
8863 member
; member
= TREE_CHAIN (member
))
8865 tree t
= TREE_VALUE (member
);
8867 if (TREE_PURPOSE (member
))
8871 /* Build new CLASSTYPE_NESTED_UTDS. */
8874 bool class_template_p
;
8876 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
8877 && TYPE_LANG_SPECIFIC (t
)
8878 && CLASSTYPE_IS_TEMPLATE (t
));
8879 /* If the member is a class template, then -- even after
8880 substitution -- there may be dependent types in the
8881 template argument list for the class. We increment
8882 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8883 that function will assume that no types are dependent
8884 when outside of a template. */
8885 if (class_template_p
)
8886 ++processing_template_decl
;
8887 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8888 if (class_template_p
)
8889 --processing_template_decl
;
8890 if (newtag
== error_mark_node
)
8893 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
8895 tree name
= TYPE_IDENTIFIER (t
);
8897 if (class_template_p
)
8898 /* Unfortunately, lookup_template_class sets
8899 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8900 instantiation (i.e., for the type of a member
8901 template class nested within a template class.)
8902 This behavior is required for
8903 maybe_process_partial_specialization to work
8904 correctly, but is not accurate in this case;
8905 the TAG is not an instantiation of anything.
8906 (The corresponding TEMPLATE_DECL is an
8907 instantiation, but the TYPE is not.) */
8908 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
8910 /* Now, we call pushtag to put this NEWTAG into the scope of
8911 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8912 pushtag calling push_template_decl. We don't have to do
8913 this for enums because it will already have been done in
8916 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
8917 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
8920 else if (DECL_DECLARES_FUNCTION_P (t
))
8922 /* Build new TYPE_METHODS. */
8925 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8926 ++processing_template_decl
;
8927 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8928 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8929 --processing_template_decl
;
8930 set_current_access_from_decl (r
);
8931 finish_member_declaration (r
);
8932 /* Instantiate members marked with attribute used. */
8933 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8935 if (TREE_CODE (r
) == FUNCTION_DECL
8936 && DECL_OMP_DECLARE_REDUCTION_P (r
))
8937 cp_check_omp_declare_reduction (r
);
8941 /* Build new TYPE_FIELDS. */
8942 if (TREE_CODE (t
) == STATIC_ASSERT
)
8946 ++c_inhibit_evaluation_warnings
;
8948 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
8949 tf_warning_or_error
, NULL_TREE
,
8950 /*integral_constant_expression_p=*/true);
8951 --c_inhibit_evaluation_warnings
;
8953 finish_static_assert (condition
,
8954 STATIC_ASSERT_MESSAGE (t
),
8955 STATIC_ASSERT_SOURCE_LOCATION (t
),
8958 else if (TREE_CODE (t
) != CONST_DECL
)
8961 tree vec
= NULL_TREE
;
8964 /* The file and line for this declaration, to
8965 assist in error message reporting. Since we
8966 called push_tinst_level above, we don't need to
8968 input_location
= DECL_SOURCE_LOCATION (t
);
8970 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8971 ++processing_template_decl
;
8972 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
8973 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8974 --processing_template_decl
;
8976 if (TREE_CODE (r
) == TREE_VEC
)
8978 /* A capture pack became multiple fields. */
8980 len
= TREE_VEC_LENGTH (vec
);
8983 for (int i
= 0; i
< len
; ++i
)
8986 r
= TREE_VEC_ELT (vec
, i
);
8991 [t]he initialization (and any associated
8992 side-effects) of a static data member does
8993 not occur unless the static data member is
8994 itself used in a way that requires the
8995 definition of the static data member to
8998 Therefore, we do not substitute into the
8999 initialized for the static data member here. */
9000 finish_static_data_member_decl
9003 /*init_const_expr_p=*/false,
9004 /*asmspec_tree=*/NULL_TREE
,
9006 /* Instantiate members marked with attribute used. */
9007 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
9010 else if (TREE_CODE (r
) == FIELD_DECL
)
9012 /* Determine whether R has a valid type and can be
9013 completed later. If R is invalid, then its type
9014 is replaced by error_mark_node. */
9015 tree rtype
= TREE_TYPE (r
);
9016 if (can_complete_type_without_circularity (rtype
))
9017 complete_type (rtype
);
9019 if (!COMPLETE_TYPE_P (rtype
))
9021 cxx_incomplete_type_error (r
, rtype
);
9022 TREE_TYPE (r
) = error_mark_node
;
9026 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9027 such a thing will already have been added to the field
9028 list by tsubst_enum in finish_member_declaration in the
9029 CLASSTYPE_NESTED_UTDS case above. */
9030 if (!(TREE_CODE (r
) == TYPE_DECL
9031 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
9032 && DECL_ARTIFICIAL (r
)))
9034 set_current_access_from_decl (r
);
9035 finish_member_declaration (r
);
9043 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
9044 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9046 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9048 tree friend_type
= t
;
9049 bool adjust_processing_template_decl
= false;
9051 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9053 /* template <class T> friend class C; */
9054 friend_type
= tsubst_friend_class (friend_type
, args
);
9055 adjust_processing_template_decl
= true;
9057 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
9059 /* template <class T> friend class C::D; */
9060 friend_type
= tsubst (friend_type
, args
,
9061 tf_warning_or_error
, NULL_TREE
);
9062 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9063 friend_type
= TREE_TYPE (friend_type
);
9064 adjust_processing_template_decl
= true;
9066 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
9067 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
9069 /* This could be either
9073 when dependent_type_p is false or
9075 template <class U> friend class T::C;
9078 friend_type
= tsubst (friend_type
, args
,
9079 tf_warning_or_error
, NULL_TREE
);
9080 /* Bump processing_template_decl for correct
9081 dependent_type_p calculation. */
9082 ++processing_template_decl
;
9083 if (dependent_type_p (friend_type
))
9084 adjust_processing_template_decl
= true;
9085 --processing_template_decl
;
9087 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
9088 && hidden_name_p (TYPE_NAME (friend_type
)))
9092 where C hasn't been declared yet. Let's lookup name
9093 from namespace scope directly, bypassing any name that
9094 come from dependent base class. */
9095 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
9097 /* The call to xref_tag_from_type does injection for friend
9099 push_nested_namespace (ns
);
9101 xref_tag_from_type (friend_type
, NULL_TREE
,
9102 /*tag_scope=*/ts_current
);
9103 pop_nested_namespace (ns
);
9105 else if (uses_template_parms (friend_type
))
9106 /* friend class C<T>; */
9107 friend_type
= tsubst (friend_type
, args
,
9108 tf_warning_or_error
, NULL_TREE
);
9113 where C is already declared or
9115 friend class C<int>;
9117 We don't have to do anything in these cases. */
9119 if (adjust_processing_template_decl
)
9120 /* Trick make_friend_class into realizing that the friend
9121 we're adding is a template, not an ordinary class. It's
9122 important that we use make_friend_class since it will
9123 perform some error-checking and output cross-reference
9125 ++processing_template_decl
;
9127 if (friend_type
!= error_mark_node
)
9128 make_friend_class (type
, friend_type
, /*complain=*/false);
9130 if (adjust_processing_template_decl
)
9131 --processing_template_decl
;
9135 /* Build new DECL_FRIENDLIST. */
9138 /* The file and line for this declaration, to
9139 assist in error message reporting. Since we
9140 called push_tinst_level above, we don't need to
9142 input_location
= DECL_SOURCE_LOCATION (t
);
9144 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9146 ++processing_template_decl
;
9147 push_deferring_access_checks (dk_no_check
);
9150 r
= tsubst_friend_function (t
, args
);
9151 add_friend (type
, r
, /*complain=*/false);
9152 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9154 pop_deferring_access_checks ();
9155 --processing_template_decl
;
9161 if (tree expr
= CLASSTYPE_LAMBDA_EXPR (type
))
9163 tree decl
= lambda_function (type
);
9166 if (!DECL_TEMPLATE_INFO (decl
)
9167 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl
)) != decl
)
9168 instantiate_decl (decl
, false, false);
9170 /* We need to instantiate the capture list from the template
9171 after we've instantiated the closure members, but before we
9172 consider adding the conversion op. Also keep any captures
9173 that may have been added during instantiation of the op(). */
9174 tree tmpl_expr
= CLASSTYPE_LAMBDA_EXPR (pattern
);
9176 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr
),
9177 args
, tf_warning_or_error
, NULL_TREE
,
9180 LAMBDA_EXPR_CAPTURE_LIST (expr
)
9181 = chainon (tmpl_cap
, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr
)));
9183 maybe_add_lambda_conv_op (type
);
9186 gcc_assert (errorcount
);
9189 /* Set the file and line number information to whatever is given for
9190 the class itself. This puts error messages involving generated
9191 implicit functions at a predictable point, and the same point
9192 that would be used for non-template classes. */
9193 input_location
= DECL_SOURCE_LOCATION (typedecl
);
9195 unreverse_member_declarations (type
);
9196 finish_struct_1 (type
);
9197 TYPE_BEING_DEFINED (type
) = 0;
9199 /* We don't instantiate default arguments for member functions. 14.7.1:
9201 The implicit instantiation of a class template specialization causes
9202 the implicit instantiation of the declarations, but not of the
9203 definitions or default arguments, of the class member functions,
9204 member classes, static data members and member templates.... */
9206 /* Some typedefs referenced from within the template code need to be access
9207 checked at template instantiation time, i.e now. These types were
9208 added to the template at parsing time. Let's get those and perform
9209 the access checks then. */
9210 perform_typedefs_access_check (pattern
, args
);
9211 perform_deferred_access_checks (tf_warning_or_error
);
9212 pop_nested_class ();
9213 maximum_field_alignment
= saved_maximum_field_alignment
;
9215 pop_from_top_level ();
9216 pop_deferring_access_checks ();
9219 /* The vtable for a template class can be emitted in any translation
9220 unit in which the class is instantiated. When there is no key
9221 method, however, finish_struct_1 will already have added TYPE to
9222 the keyed_classes list. */
9223 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
9224 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
9229 /* Wrapper for instantiate_class_template_1. */
9232 instantiate_class_template (tree type
)
9235 timevar_push (TV_TEMPLATE_INST
);
9236 ret
= instantiate_class_template_1 (type
);
9237 timevar_pop (TV_TEMPLATE_INST
);
9242 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9248 else if (TYPE_P (t
))
9249 r
= tsubst (t
, args
, complain
, in_decl
);
9252 if (!(complain
& tf_warning
))
9253 ++c_inhibit_evaluation_warnings
;
9254 r
= tsubst_expr (t
, args
, complain
, in_decl
,
9255 /*integral_constant_expression_p=*/true);
9256 if (!(complain
& tf_warning
))
9257 --c_inhibit_evaluation_warnings
;
9258 /* Preserve the raw-reference nature of T. */
9259 if (TREE_TYPE (t
) && TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
9260 && REFERENCE_REF_P (r
))
9261 r
= TREE_OPERAND (r
, 0);
9266 /* Given a function parameter pack TMPL_PARM and some function parameters
9267 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9268 and set *SPEC_P to point at the next point in the list. */
9271 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
9273 /* Collect all of the extra "packed" parameters into an
9277 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
9278 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
9279 tree spec_parm
= *spec_p
;
9282 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
9284 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
9287 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9288 parmvec
= make_tree_vec (len
);
9289 parmtypevec
= make_tree_vec (len
);
9290 spec_parm
= *spec_p
;
9291 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
9293 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
9294 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
9297 /* Build the argument packs. */
9298 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
9299 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
9300 TREE_TYPE (argpack
) = argtypepack
;
9301 *spec_p
= spec_parm
;
9306 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9307 NONTYPE_ARGUMENT_PACK. */
9310 make_fnparm_pack (tree spec_parm
)
9312 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
9315 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9319 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
9321 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
9322 if (i
>= TREE_VEC_LENGTH (vec
))
9324 return PACK_EXPANSION_P (TREE_VEC_ELT (vec
, i
));
9328 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9331 make_argument_pack_select (tree arg_pack
, unsigned index
)
9333 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
9335 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
9336 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9341 /* This is a subroutine of tsubst_pack_expansion.
9343 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9344 mechanism to store the (non complete list of) arguments of the
9345 substitution and return a non substituted pack expansion, in order
9346 to wait for when we have enough arguments to really perform the
9350 use_pack_expansion_extra_args_p (tree parm_packs
,
9354 /* If one pack has an expansion and another pack has a normal
9355 argument or if one pack has an empty argument and an another
9356 one hasn't then tsubst_pack_expansion cannot perform the
9357 substitution and need to fall back on the
9358 PACK_EXPANSION_EXTRA mechanism. */
9359 if (parm_packs
== NULL_TREE
)
9361 else if (has_empty_arg
)
9364 bool has_expansion_arg
= false;
9365 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
9367 bool has_non_expansion_arg
= false;
9368 for (tree parm_pack
= parm_packs
;
9370 parm_pack
= TREE_CHAIN (parm_pack
))
9372 tree arg
= TREE_VALUE (parm_pack
);
9374 if (argument_pack_element_is_expansion_p (arg
, i
))
9375 has_expansion_arg
= true;
9377 has_non_expansion_arg
= true;
9380 if (has_expansion_arg
&& has_non_expansion_arg
)
9386 /* [temp.variadic]/6 says that:
9388 The instantiation of a pack expansion [...]
9389 produces a list E1,E2, ..., En, where N is the number of elements
9390 in the pack expansion parameters.
9392 This subroutine of tsubst_pack_expansion produces one of these Ei.
9394 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9395 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9396 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9397 INDEX is the index 'i' of the element Ei to produce. ARGS,
9398 COMPLAIN, and IN_DECL are the same parameters as for the
9399 tsubst_pack_expansion function.
9401 The function returns the resulting Ei upon successful completion,
9404 Note that this function possibly modifies the ARGS parameter, so
9405 it's the responsibility of the caller to restore it. */
9408 gen_elem_of_pack_expansion_instantiation (tree pattern
,
9411 tree args
/* This parm gets
9413 tsubst_flags_t complain
,
9417 bool ith_elem_is_expansion
= false;
9419 /* For each parameter pack, change the substitution of the parameter
9420 pack to the ith argument in its argument pack, then expand the
9422 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
9424 tree parm
= TREE_PURPOSE (pack
);
9425 tree arg_pack
= TREE_VALUE (pack
);
9426 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
9428 ith_elem_is_expansion
|=
9429 argument_pack_element_is_expansion_p (arg_pack
, index
);
9431 /* Select the Ith argument from the pack. */
9432 if (TREE_CODE (parm
) == PARM_DECL
9433 || TREE_CODE (parm
) == FIELD_DECL
)
9437 aps
= make_argument_pack_select (arg_pack
, index
);
9439 register_local_specialization (aps
, parm
);
9442 aps
= retrieve_local_specialization (parm
);
9447 template_parm_level_and_index (parm
, &level
, &idx
);
9451 aps
= make_argument_pack_select (arg_pack
, index
);
9452 /* Update the corresponding argument. */
9453 TMPL_ARG (args
, level
, idx
) = aps
;
9456 /* Re-use the ARGUMENT_PACK_SELECT. */
9457 aps
= TMPL_ARG (args
, level
, idx
);
9459 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9462 /* Substitute into the PATTERN with the (possibly altered)
9464 if (!TYPE_P (pattern
))
9465 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9466 /*integral_constant_expression_p=*/false);
9468 t
= tsubst (pattern
, args
, complain
, in_decl
);
9470 /* If the Ith argument pack element is a pack expansion, then
9471 the Ith element resulting from the substituting is going to
9472 be a pack expansion as well. */
9473 if (ith_elem_is_expansion
)
9474 t
= make_pack_expansion (t
);
9479 /* Substitute ARGS into T, which is an pack expansion
9480 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9481 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9482 (if only a partial substitution could be performed) or
9483 ERROR_MARK_NODE if there was an error. */
9485 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
9489 tree pack
, packs
= NULL_TREE
;
9490 bool unsubstituted_packs
= false;
9493 struct pointer_map_t
*saved_local_specializations
= NULL
;
9494 bool need_local_specializations
= false;
9497 gcc_assert (PACK_EXPANSION_P (t
));
9498 pattern
= PACK_EXPANSION_PATTERN (t
);
9500 /* Add in any args remembered from an earlier partial instantiation. */
9501 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
9503 levels
= TMPL_ARGS_DEPTH (args
);
9505 /* Determine the argument packs that will instantiate the parameter
9506 packs used in the expansion expression. While we're at it,
9507 compute the number of arguments to be expanded and make sure it
9509 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
9510 pack
= TREE_CHAIN (pack
))
9512 tree parm_pack
= TREE_VALUE (pack
);
9513 tree arg_pack
= NULL_TREE
;
9514 tree orig_arg
= NULL_TREE
;
9517 if (TREE_CODE (parm_pack
) == BASES
)
9519 if (BASES_DIRECT (parm_pack
))
9520 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9521 args
, complain
, in_decl
, false));
9523 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9524 args
, complain
, in_decl
, false));
9526 if (TREE_CODE (parm_pack
) == PARM_DECL
)
9528 if (PACK_EXPANSION_LOCAL_P (t
))
9529 arg_pack
= retrieve_local_specialization (parm_pack
);
9532 /* We can't rely on local_specializations for a parameter
9533 name used later in a function declaration (such as in a
9534 late-specified return type). Even if it exists, it might
9535 have the wrong value for a recursive call. Just make a
9536 dummy decl, since it's only used for its type. */
9537 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
9538 if (arg_pack
&& DECL_PACK_P (arg_pack
))
9539 /* Partial instantiation of the parm_pack, we can't build
9540 up an argument pack yet. */
9541 arg_pack
= NULL_TREE
;
9543 arg_pack
= make_fnparm_pack (arg_pack
);
9544 need_local_specializations
= true;
9547 else if (TREE_CODE (parm_pack
) == FIELD_DECL
)
9548 arg_pack
= tsubst_copy (parm_pack
, args
, complain
, in_decl
);
9552 template_parm_level_and_index (parm_pack
, &level
, &idx
);
9554 if (level
<= levels
)
9555 arg_pack
= TMPL_ARG (args
, level
, idx
);
9558 orig_arg
= arg_pack
;
9559 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
9560 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
9562 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
9563 /* This can only happen if we forget to expand an argument
9564 pack somewhere else. Just return an error, silently. */
9566 result
= make_tree_vec (1);
9567 TREE_VEC_ELT (result
, 0) = error_mark_node
;
9574 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
9576 /* Don't bother trying to do a partial substitution with
9577 incomplete packs; we'll try again after deduction. */
9578 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
9583 else if (len
!= my_len
)
9585 if (!(complain
& tf_error
))
9586 /* Fail quietly. */;
9587 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
9588 error ("mismatched argument pack lengths while expanding "
9592 error ("mismatched argument pack lengths while expanding "
9595 return error_mark_node
;
9598 /* Keep track of the parameter packs and their corresponding
9600 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
9601 TREE_TYPE (packs
) = orig_arg
;
9605 /* We can't substitute for this parameter pack. We use a flag as
9606 well as the missing_level counter because function parameter
9607 packs don't have a level. */
9608 unsubstituted_packs
= true;
9612 /* We cannot expand this expansion expression, because we don't have
9613 all of the argument packs we need. */
9614 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
9616 /* We got some full packs, but we can't substitute them in until we
9617 have values for all the packs. So remember these until then. */
9619 t
= make_pack_expansion (pattern
);
9620 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
9623 else if (unsubstituted_packs
)
9625 /* There were no real arguments, we're just replacing a parameter
9626 pack with another version of itself. Substitute into the
9627 pattern and return a PACK_EXPANSION_*. The caller will need to
9629 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
9630 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9631 /*integral_constant_expression_p=*/false);
9633 t
= tsubst (pattern
, args
, complain
, in_decl
);
9634 t
= make_pack_expansion (t
);
9638 gcc_assert (len
>= 0);
9640 if (need_local_specializations
)
9642 /* We're in a late-specified return type, so create our own local
9643 specializations map; the current map is either NULL or (in the
9644 case of recursive unification) might have bindings that we don't
9645 want to use or alter. */
9646 saved_local_specializations
= local_specializations
;
9647 local_specializations
= pointer_map_create ();
9650 /* For each argument in each argument pack, substitute into the
9652 result
= make_tree_vec (len
);
9653 for (i
= 0; i
< len
; ++i
)
9655 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
9659 TREE_VEC_ELT (result
, i
) = t
;
9660 if (t
== error_mark_node
)
9662 result
= error_mark_node
;
9667 /* Update ARGS to restore the substitution from parameter packs to
9668 their argument packs. */
9669 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9671 tree parm
= TREE_PURPOSE (pack
);
9673 if (TREE_CODE (parm
) == PARM_DECL
9674 || TREE_CODE (parm
) == FIELD_DECL
)
9675 register_local_specialization (TREE_TYPE (pack
), parm
);
9680 if (TREE_VALUE (pack
) == NULL_TREE
)
9683 template_parm_level_and_index (parm
, &level
, &idx
);
9685 /* Update the corresponding argument. */
9686 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
9687 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
9690 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
9694 if (need_local_specializations
)
9696 pointer_map_destroy (local_specializations
);
9697 local_specializations
= saved_local_specializations
;
9703 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9704 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9705 parameter packs; all parms generated from a function parameter pack will
9706 have the same DECL_PARM_INDEX. */
9709 get_pattern_parm (tree parm
, tree tmpl
)
9711 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9714 if (DECL_ARTIFICIAL (parm
))
9716 for (patparm
= DECL_ARGUMENTS (pattern
);
9717 patparm
; patparm
= DECL_CHAIN (patparm
))
9718 if (DECL_ARTIFICIAL (patparm
)
9719 && DECL_NAME (parm
) == DECL_NAME (patparm
))
9724 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
9725 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
9726 gcc_assert (DECL_PARM_INDEX (patparm
)
9727 == DECL_PARM_INDEX (parm
));
9733 /* Substitute ARGS into the vector or list of template arguments T. */
9736 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9739 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
9742 if (t
== error_mark_node
)
9743 return error_mark_node
;
9745 len
= TREE_VEC_LENGTH (t
);
9746 elts
= XALLOCAVEC (tree
, len
);
9748 for (i
= 0; i
< len
; i
++)
9750 tree orig_arg
= TREE_VEC_ELT (t
, i
);
9753 if (TREE_CODE (orig_arg
) == TREE_VEC
)
9754 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
9755 else if (PACK_EXPANSION_P (orig_arg
))
9757 /* Substitute into an expansion expression. */
9758 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
9760 if (TREE_CODE (new_arg
) == TREE_VEC
)
9761 /* Add to the expanded length adjustment the number of
9762 expanded arguments. We subtract one from this
9763 measurement, because the argument pack expression
9764 itself is already counted as 1 in
9765 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9766 the argument pack is empty. */
9767 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
9769 else if (ARGUMENT_PACK_P (orig_arg
))
9771 /* Substitute into each of the arguments. */
9772 new_arg
= TYPE_P (orig_arg
)
9773 ? cxx_make_type (TREE_CODE (orig_arg
))
9774 : make_node (TREE_CODE (orig_arg
));
9776 SET_ARGUMENT_PACK_ARGS (
9778 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
9779 args
, complain
, in_decl
));
9781 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
9782 new_arg
= error_mark_node
;
9784 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
9785 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
9787 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
9789 if (TREE_TYPE (new_arg
) == error_mark_node
)
9790 new_arg
= error_mark_node
;
9794 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
9796 if (new_arg
== error_mark_node
)
9797 return error_mark_node
;
9800 if (new_arg
!= orig_arg
)
9807 /* Make space for the expanded arguments coming from template
9809 t
= make_tree_vec (len
+ expanded_len_adjust
);
9810 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9811 arguments for a member template.
9812 In that case each TREE_VEC in ORIG_T represents a level of template
9813 arguments, and ORIG_T won't carry any non defaulted argument count.
9814 It will rather be the nested TREE_VECs that will carry one.
9815 In other words, ORIG_T carries a non defaulted argument count only
9816 if it doesn't contain any nested TREE_VEC. */
9817 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
9819 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
9820 count
+= expanded_len_adjust
;
9821 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
9823 for (i
= 0, out
= 0; i
< len
; i
++)
9825 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
9826 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
9827 && TREE_CODE (elts
[i
]) == TREE_VEC
)
9831 /* Now expand the template argument pack "in place". */
9832 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
9833 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
9837 TREE_VEC_ELT (t
, out
) = elts
[i
];
9845 /* Return the result of substituting ARGS into the template parameters
9846 given by PARMS. If there are m levels of ARGS and m + n levels of
9847 PARMS, then the result will contain n levels of PARMS. For
9848 example, if PARMS is `template <class T> template <class U>
9849 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9850 result will be `template <int*, double, class V>'. */
9853 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
9858 /* When substituting into a template, we must set
9859 PROCESSING_TEMPLATE_DECL as the template parameters may be
9860 dependent if they are based on one-another, and the dependency
9861 predicates are short-circuit outside of templates. */
9862 ++processing_template_decl
;
9864 for (new_parms
= &r
;
9865 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
9866 new_parms
= &(TREE_CHAIN (*new_parms
)),
9867 parms
= TREE_CHAIN (parms
))
9870 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
9873 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
9877 if (parms
== error_mark_node
)
9880 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
9882 if (tuple
== error_mark_node
)
9885 TREE_VEC_ELT (new_vec
, i
) =
9886 tsubst_template_parm (tuple
, args
, complain
);
9890 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
9891 - TMPL_ARGS_DEPTH (args
)),
9892 new_vec
, NULL_TREE
);
9895 --processing_template_decl
;
9900 /* Return the result of substituting ARGS into one template parameter
9901 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9902 parameter and which TREE_PURPOSE is the default argument of the
9903 template parameter. */
9906 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
9908 tree default_value
, parm_decl
;
9910 if (args
== NULL_TREE
9912 || t
== error_mark_node
)
9915 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
9917 default_value
= TREE_PURPOSE (t
);
9918 parm_decl
= TREE_VALUE (t
);
9920 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
9921 if (TREE_CODE (parm_decl
) == PARM_DECL
9922 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
9923 parm_decl
= error_mark_node
;
9924 default_value
= tsubst_template_arg (default_value
, args
,
9925 complain
, NULL_TREE
);
9927 return build_tree_list (default_value
, parm_decl
);
9930 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9931 type T. If T is not an aggregate or enumeration type, it is
9932 handled as if by tsubst. IN_DECL is as for tsubst. If
9933 ENTERING_SCOPE is nonzero, T is the context for a template which
9934 we are presently tsubst'ing. Return the substituted value. */
9937 tsubst_aggr_type (tree t
,
9939 tsubst_flags_t complain
,
9946 switch (TREE_CODE (t
))
9949 if (TYPE_PTRMEMFUNC_P (t
))
9950 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
9952 /* Else fall through. */
9955 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
9960 int saved_unevaluated_operand
;
9961 int saved_inhibit_evaluation_warnings
;
9963 /* In "sizeof(X<I>)" we need to evaluate "I". */
9964 saved_unevaluated_operand
= cp_unevaluated_operand
;
9965 cp_unevaluated_operand
= 0;
9966 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9967 c_inhibit_evaluation_warnings
= 0;
9969 /* First, determine the context for the type we are looking
9971 context
= TYPE_CONTEXT (t
);
9972 if (context
&& TYPE_P (context
))
9974 context
= tsubst_aggr_type (context
, args
, complain
,
9975 in_decl
, /*entering_scope=*/1);
9976 /* If context is a nested class inside a class template,
9977 it may still need to be instantiated (c++/33959). */
9978 context
= complete_type (context
);
9981 /* Then, figure out what arguments are appropriate for the
9982 type we are trying to find. For example, given:
9984 template <class T> struct S;
9985 template <class T, class U> void f(T, U) { S<U> su; }
9987 and supposing that we are instantiating f<int, double>,
9988 then our ARGS will be {int, double}, but, when looking up
9989 S we only want {double}. */
9990 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
9992 if (argvec
== error_mark_node
)
9993 r
= error_mark_node
;
9996 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
9997 entering_scope
, complain
);
9998 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
10001 cp_unevaluated_operand
= saved_unevaluated_operand
;
10002 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
10007 /* This is not a template type, so there's nothing to do. */
10011 return tsubst (t
, args
, complain
, in_decl
);
10015 /* Substitute into the default argument ARG (a default argument for
10016 FN), which has the indicated TYPE. */
10019 tsubst_default_argument (tree fn
, tree type
, tree arg
, tsubst_flags_t complain
)
10021 tree saved_class_ptr
= NULL_TREE
;
10022 tree saved_class_ref
= NULL_TREE
;
10023 int errs
= errorcount
+ sorrycount
;
10025 /* This can happen in invalid code. */
10026 if (TREE_CODE (arg
) == DEFAULT_ARG
)
10029 /* This default argument came from a template. Instantiate the
10030 default argument here, not in tsubst. In the case of
10039 we must be careful to do name lookup in the scope of S<T>,
10040 rather than in the current class. */
10041 push_access_scope (fn
);
10042 /* The "this" pointer is not valid in a default argument. */
10045 saved_class_ptr
= current_class_ptr
;
10046 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
10047 saved_class_ref
= current_class_ref
;
10048 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
10051 push_deferring_access_checks(dk_no_deferred
);
10052 /* The default argument expression may cause implicitly defined
10053 member functions to be synthesized, which will result in garbage
10054 collection. We must treat this situation as if we were within
10055 the body of function so as to avoid collecting live data on the
10058 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
10059 complain
, NULL_TREE
,
10060 /*integral_constant_expression_p=*/false);
10062 pop_deferring_access_checks();
10064 /* Restore the "this" pointer. */
10067 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
10068 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
10071 if (errorcount
+sorrycount
> errs
10072 && (complain
& tf_warning_or_error
))
10073 inform (input_location
,
10074 " when instantiating default argument for call to %D", fn
);
10076 /* Make sure the default argument is reasonable. */
10077 arg
= check_default_argument (type
, arg
, complain
);
10079 pop_access_scope (fn
);
10084 /* Substitute into all the default arguments for FN. */
10087 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
10092 tmpl_args
= DECL_TI_ARGS (fn
);
10094 /* If this function is not yet instantiated, we certainly don't need
10095 its default arguments. */
10096 if (uses_template_parms (tmpl_args
))
10098 /* Don't do this again for clones. */
10099 if (DECL_CLONED_FUNCTION_P (fn
))
10102 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
10104 arg
= TREE_CHAIN (arg
))
10105 if (TREE_PURPOSE (arg
))
10106 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
10108 TREE_PURPOSE (arg
),
10112 /* Substitute the ARGS into the T, which is a _DECL. Return the
10113 result of the substitution. Issue error and warning messages under
10114 control of COMPLAIN. */
10117 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
10119 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10120 location_t saved_loc
;
10121 tree r
= NULL_TREE
;
10123 hashval_t hash
= 0;
10125 /* Set the filename and linenumber to improve error-reporting. */
10126 saved_loc
= input_location
;
10127 input_location
= DECL_SOURCE_LOCATION (t
);
10129 switch (TREE_CODE (t
))
10131 case TEMPLATE_DECL
:
10133 /* We can get here when processing a member function template,
10134 member class template, or template template parameter. */
10135 tree decl
= DECL_TEMPLATE_RESULT (t
);
10140 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10142 /* Template template parameter is treated here. */
10143 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10144 if (new_type
== error_mark_node
)
10145 RETURN (error_mark_node
);
10146 /* If we get a real template back, return it. This can happen in
10147 the context of most_specialized_class. */
10148 if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
10152 DECL_CHAIN (r
) = NULL_TREE
;
10153 TREE_TYPE (r
) = new_type
;
10154 DECL_TEMPLATE_RESULT (r
)
10155 = build_decl (DECL_SOURCE_LOCATION (decl
),
10156 TYPE_DECL
, DECL_NAME (decl
), new_type
);
10157 DECL_TEMPLATE_PARMS (r
)
10158 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10160 TYPE_NAME (new_type
) = r
;
10164 /* We might already have an instance of this template.
10165 The ARGS are for the surrounding class type, so the
10166 full args contain the tsubst'd args for the context,
10167 plus the innermost args from the template decl. */
10168 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
10169 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
10170 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
10171 /* Because this is a template, the arguments will still be
10172 dependent, even after substitution. If
10173 PROCESSING_TEMPLATE_DECL is not set, the dependency
10174 predicates will short-circuit. */
10175 ++processing_template_decl
;
10176 full_args
= tsubst_template_args (tmpl_args
, args
,
10177 complain
, in_decl
);
10178 --processing_template_decl
;
10179 if (full_args
== error_mark_node
)
10180 RETURN (error_mark_node
);
10182 /* If this is a default template template argument,
10183 tsubst might not have changed anything. */
10184 if (full_args
== tmpl_args
)
10187 hash
= hash_tmpl_and_args (t
, full_args
);
10188 spec
= retrieve_specialization (t
, full_args
, hash
);
10189 if (spec
!= NULL_TREE
)
10195 /* Make a new template decl. It will be similar to the
10196 original, but will record the current template arguments.
10197 We also create a new function declaration, which is just
10198 like the old one, but points to this new template, rather
10199 than the old one. */
10201 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
10202 DECL_CHAIN (r
) = NULL_TREE
;
10204 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10206 if (TREE_CODE (decl
) == TYPE_DECL
10207 && !TYPE_DECL_ALIAS_P (decl
))
10210 ++processing_template_decl
;
10211 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10212 --processing_template_decl
;
10213 if (new_type
== error_mark_node
)
10214 RETURN (error_mark_node
);
10216 TREE_TYPE (r
) = new_type
;
10217 /* For a partial specialization, we need to keep pointing to
10218 the primary template. */
10219 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
10220 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
10221 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
10222 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
10223 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
10228 ++processing_template_decl
;
10229 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
10230 --processing_template_decl
;
10231 if (new_decl
== error_mark_node
)
10232 RETURN (error_mark_node
);
10234 DECL_TEMPLATE_RESULT (r
) = new_decl
;
10235 DECL_TI_TEMPLATE (new_decl
) = r
;
10236 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
10237 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
10238 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
10241 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10242 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
10243 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
10245 /* The template parameters for this new template are all the
10246 template parameters for the old template, except the
10247 outermost level of parameters. */
10248 DECL_TEMPLATE_PARMS (r
)
10249 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10252 if (PRIMARY_TEMPLATE_P (t
))
10253 DECL_PRIMARY_TEMPLATE (r
) = r
;
10255 if (TREE_CODE (decl
) != TYPE_DECL
)
10256 /* Record this non-type partial instantiation. */
10257 register_specialization (r
, t
,
10258 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
10263 case FUNCTION_DECL
:
10266 tree argvec
= NULL_TREE
;
10274 /* Nobody should be tsubst'ing into non-template functions. */
10275 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
10277 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
10282 /* If T is not dependent, just return it. We have to
10283 increment PROCESSING_TEMPLATE_DECL because
10284 value_dependent_expression_p assumes that nothing is
10285 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10286 ++processing_template_decl
;
10287 dependent_p
= value_dependent_expression_p (t
);
10288 --processing_template_decl
;
10292 /* Calculate the most general template of which R is a
10293 specialization, and the complete set of arguments used to
10295 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
10296 argvec
= tsubst_template_args (DECL_TI_ARGS
10297 (DECL_TEMPLATE_RESULT
10298 (DECL_TI_TEMPLATE (t
))),
10299 args
, complain
, in_decl
);
10300 if (argvec
== error_mark_node
)
10301 RETURN (error_mark_node
);
10303 /* Check to see if we already have this specialization. */
10304 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10305 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10313 /* We can see more levels of arguments than parameters if
10314 there was a specialization of a member template, like
10317 template <class T> struct S { template <class U> void f(); }
10318 template <> template <class U> void S<int>::f(U);
10320 Here, we'll be substituting into the specialization,
10321 because that's where we can find the code we actually
10322 want to generate, but we'll have enough arguments for
10323 the most general template.
10325 We also deal with the peculiar case:
10327 template <class T> struct S {
10328 template <class U> friend void f();
10330 template <class U> void f() {}
10332 template void f<double>();
10334 Here, the ARGS for the instantiation of will be {int,
10335 double}. But, we only need as many ARGS as there are
10336 levels of template parameters in CODE_PATTERN. We are
10337 careful not to get fooled into reducing the ARGS in
10340 template <class T> struct S { template <class U> void f(U); }
10341 template <class T> template <> void S<T>::f(int) {}
10343 which we can spot because the pattern will be a
10344 specialization in this case. */
10345 args_depth
= TMPL_ARGS_DEPTH (args
);
10347 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
10348 if (args_depth
> parms_depth
10349 && !DECL_TEMPLATE_SPECIALIZATION (t
))
10350 args
= get_innermost_template_args (args
, parms_depth
);
10354 /* This special case arises when we have something like this:
10356 template <class T> struct S {
10357 friend void f<int>(int, double);
10360 Here, the DECL_TI_TEMPLATE for the friend declaration
10361 will be an IDENTIFIER_NODE. We are being called from
10362 tsubst_friend_function, and we want only to create a
10363 new decl (R) with appropriate types so that we can call
10364 determine_specialization. */
10365 gen_tmpl
= NULL_TREE
;
10368 if (DECL_CLASS_SCOPE_P (t
))
10370 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
10374 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
10375 complain
, t
, /*entering_scope=*/1);
10380 ctx
= DECL_CONTEXT (t
);
10382 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10383 if (type
== error_mark_node
)
10384 RETURN (error_mark_node
);
10386 /* If we hit excessive deduction depth, the type is bogus even if
10387 it isn't error_mark_node, so don't build a decl. */
10388 if (excessive_deduction_depth
)
10389 RETURN (error_mark_node
);
10391 /* We do NOT check for matching decls pushed separately at this
10392 point, as they may not represent instantiations of this
10393 template, and in any case are considered separate under the
10396 DECL_USE_TEMPLATE (r
) = 0;
10397 TREE_TYPE (r
) = type
;
10398 /* Clear out the mangled name and RTL for the instantiation. */
10399 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10400 SET_DECL_RTL (r
, NULL
);
10401 /* Leave DECL_INITIAL set on deleted instantiations. */
10402 if (!DECL_DELETED_FN (r
))
10403 DECL_INITIAL (r
) = NULL_TREE
;
10404 DECL_CONTEXT (r
) = ctx
;
10406 /* OpenMP UDRs have the only argument a reference to the declared
10407 type. We want to diagnose if the declared type is a reference,
10408 which is invalid, but as references to references are usually
10409 quietly merged, diagnose it here. */
10410 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
10413 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
10414 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
10415 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
10416 error_at (DECL_SOURCE_LOCATION (t
),
10417 "reference type %qT in "
10418 "%<#pragma omp declare reduction%>", argtype
);
10419 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
10420 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
10424 if (member
&& DECL_CONV_FN_P (r
))
10425 /* Type-conversion operator. Reconstruct the name, in
10426 case it's the name of one of the template's parameters. */
10427 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
10429 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
10431 DECL_RESULT (r
) = NULL_TREE
;
10433 TREE_STATIC (r
) = 0;
10434 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
10435 DECL_EXTERNAL (r
) = 1;
10436 /* If this is an instantiation of a function with internal
10437 linkage, we already know what object file linkage will be
10438 assigned to the instantiation. */
10439 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
10440 DECL_DEFER_OUTPUT (r
) = 0;
10441 DECL_CHAIN (r
) = NULL_TREE
;
10442 DECL_PENDING_INLINE_INFO (r
) = 0;
10443 DECL_PENDING_INLINE_P (r
) = 0;
10444 DECL_SAVED_TREE (r
) = NULL_TREE
;
10445 DECL_STRUCT_FUNCTION (r
) = NULL
;
10447 /* We'll re-clone as appropriate in instantiate_template. */
10448 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
10450 /* If we aren't complaining now, return on error before we register
10451 the specialization so that we'll complain eventually. */
10452 if ((complain
& tf_error
) == 0
10453 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10454 && !grok_op_properties (r
, /*complain=*/false))
10455 RETURN (error_mark_node
);
10457 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10458 this in the special friend case mentioned above where
10459 GEN_TMPL is NULL. */
10462 DECL_TEMPLATE_INFO (r
)
10463 = build_template_info (gen_tmpl
, argvec
);
10464 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10467 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10469 /* We instantiated this while substituting into
10470 the type earlier (template/friend54.C). */
10473 /* We're not supposed to instantiate default arguments
10474 until they are called, for a template. But, for a
10477 template <class T> void f ()
10478 { extern void g(int i = T()); }
10480 we should do the substitution when the template is
10481 instantiated. We handle the member function case in
10482 instantiate_class_template since the default arguments
10483 might refer to other members of the class. */
10485 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
10486 && !uses_template_parms (argvec
))
10487 tsubst_default_arguments (r
, complain
);
10490 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
10492 /* Copy the list of befriending classes. */
10493 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
10495 friends
= &TREE_CHAIN (*friends
))
10497 *friends
= copy_node (*friends
);
10498 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
10503 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
10505 maybe_retrofit_in_chrg (r
);
10506 if (DECL_CONSTRUCTOR_P (r
))
10507 grok_ctor_properties (ctx
, r
);
10508 if (DECL_INHERITED_CTOR_BASE (r
))
10509 deduce_inheriting_ctor (r
);
10510 /* If this is an instantiation of a member template, clone it.
10511 If it isn't, that'll be handled by
10512 clone_constructors_and_destructors. */
10513 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
10514 clone_function_decl (r
, /*update_method_vec_p=*/0);
10516 else if ((complain
& tf_error
) != 0
10517 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10518 && !grok_op_properties (r
, /*complain=*/true))
10519 RETURN (error_mark_node
);
10521 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
10522 SET_DECL_FRIEND_CONTEXT (r
,
10523 tsubst (DECL_FRIEND_CONTEXT (t
),
10524 args
, complain
, in_decl
));
10526 /* Possibly limit visibility based on template args. */
10527 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10528 if (DECL_VISIBILITY_SPECIFIED (t
))
10530 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10531 DECL_ATTRIBUTES (r
)
10532 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10534 determine_visibility (r
);
10535 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
10536 && !processing_template_decl
)
10537 defaulted_late_check (r
);
10539 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10540 args
, complain
, in_decl
);
10546 tree type
= NULL_TREE
;
10548 tree expanded_types
= NULL_TREE
;
10549 tree prev_r
= NULL_TREE
;
10550 tree first_r
= NULL_TREE
;
10552 if (DECL_PACK_P (t
))
10554 /* If there is a local specialization that isn't a
10555 parameter pack, it means that we're doing a "simple"
10556 substitution from inside tsubst_pack_expansion. Just
10557 return the local specialization (which will be a single
10559 tree spec
= retrieve_local_specialization (t
);
10561 && TREE_CODE (spec
) == PARM_DECL
10562 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
10565 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10566 the parameters in this function parameter pack. */
10567 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10568 complain
, in_decl
);
10569 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10571 len
= TREE_VEC_LENGTH (expanded_types
);
10573 /* Zero-length parameter packs are boring. Just substitute
10576 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
10581 /* All we did was update the type. Make a note of that. */
10582 type
= expanded_types
;
10583 expanded_types
= NULL_TREE
;
10587 /* Loop through all of the parameters we'll build. When T is
10588 a function parameter pack, LEN is the number of expanded
10589 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10591 for (i
= 0; i
< len
; ++i
)
10595 if (DECL_TEMPLATE_PARM_P (t
))
10596 SET_DECL_TEMPLATE_PARM_P (r
);
10598 if (expanded_types
)
10599 /* We're on the Ith parameter of the function parameter
10602 /* Get the Ith type. */
10603 type
= TREE_VEC_ELT (expanded_types
, i
);
10605 /* Rename the parameter to include the index. */
10607 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10610 /* We're dealing with a normal parameter. */
10611 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10613 type
= type_decays_to (type
);
10614 TREE_TYPE (r
) = type
;
10615 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10617 if (DECL_INITIAL (r
))
10619 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
10620 DECL_INITIAL (r
) = TREE_TYPE (r
);
10622 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
10623 complain
, in_decl
);
10626 DECL_CONTEXT (r
) = NULL_TREE
;
10628 if (!DECL_TEMPLATE_PARM_P (r
))
10629 DECL_ARG_TYPE (r
) = type_passed_as (type
);
10631 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10632 args
, complain
, in_decl
);
10634 /* Keep track of the first new parameter we
10635 generate. That's what will be returned to the
10640 /* Build a proper chain of parameters when substituting
10641 into a function parameter pack. */
10643 DECL_CHAIN (prev_r
) = r
;
10646 /* If cp_unevaluated_operand is set, we're just looking for a
10647 single dummy parameter, so don't keep going. */
10648 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
10649 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
10650 complain
, DECL_CHAIN (t
));
10652 /* FIRST_R contains the start of the chain we've built. */
10659 tree type
= NULL_TREE
;
10660 tree vec
= NULL_TREE
;
10661 tree expanded_types
= NULL_TREE
;
10664 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
10666 /* This field is a lambda capture pack. Return a TREE_VEC of
10667 the expanded fields to instantiate_class_template_1 and
10668 store them in the specializations hash table as a
10669 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
10670 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10671 complain
, in_decl
);
10672 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10674 len
= TREE_VEC_LENGTH (expanded_types
);
10675 vec
= make_tree_vec (len
);
10679 /* All we did was update the type. Make a note of that. */
10680 type
= expanded_types
;
10681 expanded_types
= NULL_TREE
;
10685 for (int i
= 0; i
< len
; ++i
)
10688 if (expanded_types
)
10690 type
= TREE_VEC_ELT (expanded_types
, i
);
10692 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10695 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10697 if (type
== error_mark_node
)
10698 RETURN (error_mark_node
);
10699 TREE_TYPE (r
) = type
;
10700 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10702 if (DECL_C_BIT_FIELD (r
))
10703 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10704 non-bit-fields DECL_INITIAL is a non-static data member
10705 initializer, which gets deferred instantiation. */
10707 = tsubst_expr (DECL_INITIAL (t
), args
,
10709 /*integral_constant_expression_p=*/true);
10710 else if (DECL_INITIAL (t
))
10712 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10713 NSDMI in perform_member_init. Still set DECL_INITIAL
10714 so that we know there is one. */
10715 DECL_INITIAL (r
) = void_zero_node
;
10716 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
10717 retrofit_lang_decl (r
);
10718 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10720 /* We don't have to set DECL_CONTEXT here; it is set by
10721 finish_member_declaration. */
10722 DECL_CHAIN (r
) = NULL_TREE
;
10724 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10725 args
, complain
, in_decl
);
10728 TREE_VEC_ELT (vec
, i
) = r
;
10734 tree pack
= make_node (NONTYPE_ARGUMENT_PACK
);
10735 tree tpack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
10736 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
10737 SET_ARGUMENT_PACK_ARGS (tpack
, expanded_types
);
10738 TREE_TYPE (pack
) = tpack
;
10739 register_specialization (pack
, t
, args
, false, 0);
10745 /* We reach here only for member using decls. We also need to check
10746 uses_template_parms because DECL_DEPENDENT_P is not set for a
10747 using-declaration that designates a member of the current
10748 instantiation (c++/53549). */
10749 if (DECL_DEPENDENT_P (t
)
10750 || uses_template_parms (USING_DECL_SCOPE (t
)))
10752 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
10753 complain
, in_decl
);
10754 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
10755 r
= do_class_using_decl (inst_scope
, name
);
10757 r
= error_mark_node
;
10760 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
10761 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
10767 DECL_CHAIN (r
) = NULL_TREE
;
10774 tree argvec
= NULL_TREE
;
10775 tree gen_tmpl
= NULL_TREE
;
10777 tree tmpl
= NULL_TREE
;
10779 tree type
= NULL_TREE
;
10782 if (TREE_CODE (t
) == TYPE_DECL
10783 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
10785 /* If this is the canonical decl, we don't have to
10786 mess with instantiations, and often we can't (for
10787 typename, template type parms and such). Note that
10788 TYPE_NAME is not correct for the above test if
10789 we've copied the type for a typedef. */
10790 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10791 if (type
== error_mark_node
)
10792 RETURN (error_mark_node
);
10793 r
= TYPE_NAME (type
);
10797 /* Check to see if we already have the specialization we
10800 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
10802 /* T is a static data member or namespace-scope entity.
10803 We have to substitute into namespace-scope variables
10804 (even though such entities are never templates) because
10807 template <class T> void f() { extern T t; }
10809 where the entity referenced is not known until
10810 instantiation time. */
10812 ctx
= DECL_CONTEXT (t
);
10813 if (DECL_CLASS_SCOPE_P (t
))
10815 ctx
= tsubst_aggr_type (ctx
, args
,
10817 in_decl
, /*entering_scope=*/1);
10818 /* If CTX is unchanged, then T is in fact the
10819 specialization we want. That situation occurs when
10820 referencing a static data member within in its own
10821 class. We can use pointer equality, rather than
10822 same_type_p, because DECL_CONTEXT is always
10824 if (ctx
== DECL_CONTEXT (t
)
10825 && (TREE_CODE (t
) != TYPE_DECL
10826 /* ... unless T is a member template; in which
10827 case our caller can be willing to create a
10828 specialization of that template represented
10830 || !(DECL_TI_TEMPLATE (t
)
10831 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))))
10837 tmpl
= DECL_TI_TEMPLATE (t
);
10838 gen_tmpl
= most_general_template (tmpl
);
10839 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
10840 if (argvec
== error_mark_node
)
10841 RETURN (error_mark_node
);
10842 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10843 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10848 /* A local variable. */
10850 /* Subsequent calls to pushdecl will fill this in. */
10852 spec
= retrieve_local_specialization (t
);
10854 /* If we already have the specialization we need, there is
10855 nothing more to do. */
10862 /* Create a new node for the specialization we need. */
10864 if (type
== NULL_TREE
)
10866 if (is_typedef_decl (t
))
10867 type
= DECL_ORIGINAL_TYPE (t
);
10869 type
= TREE_TYPE (t
);
10871 && VAR_HAD_UNKNOWN_BOUND (t
)
10872 && type
!= error_mark_node
)
10873 type
= strip_array_domain (type
);
10874 type
= tsubst (type
, args
, complain
, in_decl
);
10878 /* Even if the original location is out of scope, the
10879 newly substituted one is not. */
10880 DECL_DEAD_FOR_LOCAL (r
) = 0;
10881 DECL_INITIALIZED_P (r
) = 0;
10882 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
10883 if (type
== error_mark_node
)
10884 RETURN (error_mark_node
);
10885 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10887 /* It may seem that this case cannot occur, since:
10892 declares a function, not a variable. However:
10895 template <typename T> void g() { T t; }
10896 template void g<f>();
10898 is an attempt to declare a variable with function
10900 error ("variable %qD has function type",
10901 /* R is not yet sufficiently initialized, so we
10902 just use its name. */
10904 RETURN (error_mark_node
);
10906 type
= complete_type (type
);
10907 /* Wait until cp_finish_decl to set this again, to handle
10908 circular dependency (template/instantiate6.C). */
10909 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
10910 type
= check_var_type (DECL_NAME (r
), type
);
10912 if (DECL_HAS_VALUE_EXPR_P (t
))
10914 tree ve
= DECL_VALUE_EXPR (t
);
10915 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
10916 /*constant_expression_p=*/false);
10917 if (REFERENCE_REF_P (ve
))
10919 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
10920 ve
= TREE_OPERAND (ve
, 0);
10922 SET_DECL_VALUE_EXPR (r
, ve
);
10925 else if (DECL_SELF_REFERENCE_P (t
))
10926 SET_DECL_SELF_REFERENCE_P (r
);
10927 TREE_TYPE (r
) = type
;
10928 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10929 DECL_CONTEXT (r
) = ctx
;
10930 /* Clear out the mangled name and RTL for the instantiation. */
10931 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10932 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10933 SET_DECL_RTL (r
, NULL
);
10934 /* The initializer must not be expanded until it is required;
10935 see [temp.inst]. */
10936 DECL_INITIAL (r
) = NULL_TREE
;
10937 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10938 SET_DECL_RTL (r
, NULL
);
10939 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
10942 /* Possibly limit visibility based on template args. */
10943 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10944 if (DECL_VISIBILITY_SPECIFIED (t
))
10946 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10947 DECL_ATTRIBUTES (r
)
10948 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10950 determine_visibility (r
);
10955 /* A static data member declaration is always marked
10956 external when it is declared in-class, even if an
10957 initializer is present. We mimic the non-template
10958 processing here. */
10959 DECL_EXTERNAL (r
) = 1;
10961 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10962 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
10963 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10965 else if (cp_unevaluated_operand
)
10966 gcc_unreachable ();
10968 register_local_specialization (r
, t
);
10970 DECL_CHAIN (r
) = NULL_TREE
;
10972 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
10974 args
, complain
, in_decl
);
10976 /* Preserve a typedef that names a type. */
10977 if (is_typedef_decl (r
))
10979 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
10980 set_underlying_type (r
);
10983 layout_decl (r
, 0);
10988 gcc_unreachable ();
10993 /* Restore the file and line information. */
10994 input_location
= saved_loc
;
10999 /* Substitute into the ARG_TYPES of a function type.
11000 If END is a TREE_CHAIN, leave it and any following types
11004 tsubst_arg_types (tree arg_types
,
11007 tsubst_flags_t complain
,
11010 tree remaining_arg_types
;
11011 tree type
= NULL_TREE
;
11013 tree expanded_args
= NULL_TREE
;
11016 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
11019 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
11020 args
, end
, complain
, in_decl
);
11021 if (remaining_arg_types
== error_mark_node
)
11022 return error_mark_node
;
11024 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
11026 /* For a pack expansion, perform substitution on the
11027 entire expression. Later on, we'll handle the arguments
11029 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
11030 args
, complain
, in_decl
);
11032 if (TREE_CODE (expanded_args
) == TREE_VEC
)
11033 /* So that we'll spin through the parameters, one by one. */
11034 i
= TREE_VEC_LENGTH (expanded_args
);
11037 /* We only partially substituted into the parameter
11038 pack. Our type is TYPE_PACK_EXPANSION. */
11039 type
= expanded_args
;
11040 expanded_args
= NULL_TREE
;
11048 type
= TREE_VEC_ELT (expanded_args
, i
);
11050 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
11052 if (type
== error_mark_node
)
11053 return error_mark_node
;
11054 if (VOID_TYPE_P (type
))
11056 if (complain
& tf_error
)
11058 error ("invalid parameter type %qT", type
);
11060 error ("in declaration %q+D", in_decl
);
11062 return error_mark_node
;
11065 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
11066 return error_mark_node
;
11068 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11069 top-level qualifiers as required. */
11070 type
= cv_unqualified (type_decays_to (type
));
11072 /* We do not substitute into default arguments here. The standard
11073 mandates that they be instantiated only when needed, which is
11074 done in build_over_call. */
11075 default_arg
= TREE_PURPOSE (arg_types
);
11077 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
11079 /* We've instantiated a template before its default arguments
11080 have been parsed. This can happen for a nested template
11081 class, and is not an error unless we require the default
11082 argument in a call of this function. */
11083 remaining_arg_types
=
11084 tree_cons (default_arg
, type
, remaining_arg_types
);
11085 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
11088 remaining_arg_types
=
11089 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
11092 return remaining_arg_types
;
11095 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11096 *not* handle the exception-specification for FNTYPE, because the
11097 initial substitution of explicitly provided template parameters
11098 during argument deduction forbids substitution into the
11099 exception-specification:
11103 All references in the function type of the function template to the
11104 corresponding template parameters are replaced by the specified tem-
11105 plate argument values. If a substitution in a template parameter or
11106 in the function type of the function template results in an invalid
11107 type, type deduction fails. [Note: The equivalent substitution in
11108 exception specifications is done only when the function is instanti-
11109 ated, at which point a program is ill-formed if the substitution
11110 results in an invalid type.] */
11113 tsubst_function_type (tree t
,
11115 tsubst_flags_t complain
,
11122 /* The TYPE_CONTEXT is not used for function/method types. */
11123 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
11125 /* Substitute the return type. */
11126 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11127 if (return_type
== error_mark_node
)
11128 return error_mark_node
;
11129 /* DR 486 clarifies that creation of a function type with an
11130 invalid return type is a deduction failure. */
11131 if (TREE_CODE (return_type
) == ARRAY_TYPE
11132 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
11134 if (complain
& tf_error
)
11136 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
11137 error ("function returning an array");
11139 error ("function returning a function");
11141 return error_mark_node
;
11144 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
11145 return error_mark_node
;
11147 /* Substitute the argument types. */
11148 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
11149 complain
, in_decl
);
11150 if (arg_types
== error_mark_node
)
11151 return error_mark_node
;
11153 /* Construct a new type node and return it. */
11154 if (TREE_CODE (t
) == FUNCTION_TYPE
)
11156 fntype
= build_function_type (return_type
, arg_types
);
11157 fntype
= apply_memfn_quals (fntype
,
11158 type_memfn_quals (t
),
11159 type_memfn_rqual (t
));
11163 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
11164 if (! MAYBE_CLASS_TYPE_P (r
))
11168 Type deduction may fail for any of the following
11171 -- Attempting to create "pointer to member of T" when T
11172 is not a class type. */
11173 if (complain
& tf_error
)
11174 error ("creating pointer to member function of non-class type %qT",
11176 return error_mark_node
;
11179 fntype
= build_method_type_directly (r
, return_type
,
11180 TREE_CHAIN (arg_types
));
11181 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
11183 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
11188 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11189 ARGS into that specification, and return the substituted
11190 specification. If there is no specification, return NULL_TREE. */
11193 tsubst_exception_specification (tree fntype
,
11195 tsubst_flags_t complain
,
11202 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
11203 new_specs
= NULL_TREE
;
11204 if (specs
&& TREE_PURPOSE (specs
))
11206 /* A noexcept-specifier. */
11207 tree expr
= TREE_PURPOSE (specs
);
11208 if (TREE_CODE (expr
) == INTEGER_CST
)
11212 /* Defer instantiation of noexcept-specifiers to avoid
11213 excessive instantiations (c++/49107). */
11214 new_specs
= make_node (DEFERRED_NOEXCEPT
);
11215 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
11217 /* We already partially instantiated this member template,
11218 so combine the new args with the old. */
11219 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
11220 = DEFERRED_NOEXCEPT_PATTERN (expr
);
11221 DEFERRED_NOEXCEPT_ARGS (new_specs
)
11222 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
11226 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
11227 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
11231 new_specs
= tsubst_copy_and_build
11232 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
11233 /*integral_constant_expression_p=*/true);
11234 new_specs
= build_noexcept_spec (new_specs
, complain
);
11238 if (! TREE_VALUE (specs
))
11245 tree expanded_specs
= NULL_TREE
;
11247 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
11249 /* Expand the pack expansion type. */
11250 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
11254 if (expanded_specs
== error_mark_node
)
11255 return error_mark_node
;
11256 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
11257 len
= TREE_VEC_LENGTH (expanded_specs
);
11260 /* We're substituting into a member template, so
11261 we got a TYPE_PACK_EXPANSION back. Add that
11262 expansion and move on. */
11263 gcc_assert (TREE_CODE (expanded_specs
)
11264 == TYPE_PACK_EXPANSION
);
11265 new_specs
= add_exception_specifier (new_specs
,
11268 specs
= TREE_CHAIN (specs
);
11273 for (i
= 0; i
< len
; ++i
)
11275 if (expanded_specs
)
11276 spec
= TREE_VEC_ELT (expanded_specs
, i
);
11278 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
11279 if (spec
== error_mark_node
)
11281 new_specs
= add_exception_specifier (new_specs
, spec
,
11285 specs
= TREE_CHAIN (specs
);
11291 /* Take the tree structure T and replace template parameters used
11292 therein with the argument vector ARGS. IN_DECL is an associated
11293 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11294 Issue error and warning messages under control of COMPLAIN. Note
11295 that we must be relatively non-tolerant of extensions here, in
11296 order to preserve conformance; if we allow substitutions that
11297 should not be allowed, we may allow argument deductions that should
11298 not succeed, and therefore report ambiguous overload situations
11299 where there are none. In theory, we could allow the substitution,
11300 but indicate that it should have failed, and allow our caller to
11301 make sure that the right thing happens, but we don't try to do this
11304 This function is used for dealing with types, decls and the like;
11305 for expressions, use tsubst_expr or tsubst_copy. */
11308 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11310 enum tree_code code
;
11311 tree type
, r
= NULL_TREE
;
11313 if (t
== NULL_TREE
|| t
== error_mark_node
11314 || t
== integer_type_node
11315 || t
== void_type_node
11316 || t
== char_type_node
11317 || t
== unknown_type_node
11318 || TREE_CODE (t
) == NAMESPACE_DECL
11319 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
11323 return tsubst_decl (t
, args
, complain
);
11325 if (args
== NULL_TREE
)
11328 code
= TREE_CODE (t
);
11330 if (code
== IDENTIFIER_NODE
)
11331 type
= IDENTIFIER_TYPE_VALUE (t
);
11333 type
= TREE_TYPE (t
);
11335 gcc_assert (type
!= unknown_type_node
);
11337 /* Reuse typedefs. We need to do this to handle dependent attributes,
11338 such as attribute aligned. */
11340 && typedef_variant_p (t
))
11342 tree decl
= TYPE_NAME (t
);
11344 if (alias_template_specialization_p (t
))
11346 /* DECL represents an alias template and we want to
11348 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11349 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11350 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
11352 else if (DECL_CLASS_SCOPE_P (decl
)
11353 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11354 && uses_template_parms (DECL_CONTEXT (decl
)))
11356 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11357 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11358 r
= retrieve_specialization (tmpl
, gen_args
, 0);
11360 else if (DECL_FUNCTION_SCOPE_P (decl
)
11361 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11362 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
11363 r
= retrieve_local_specialization (decl
);
11365 /* The typedef is from a non-template context. */
11371 r
= cp_build_qualified_type_real
11372 (r
, cp_type_quals (t
) | cp_type_quals (r
),
11373 complain
| tf_ignore_bad_quals
);
11378 /* We don't have an instantiation yet, so drop the typedef. */
11379 int quals
= cp_type_quals (t
);
11380 t
= DECL_ORIGINAL_TYPE (decl
);
11381 t
= cp_build_qualified_type_real (t
, quals
,
11382 complain
| tf_ignore_bad_quals
);
11387 && code
!= TYPENAME_TYPE
11388 && code
!= TEMPLATE_TYPE_PARM
11389 && code
!= IDENTIFIER_NODE
11390 && code
!= FUNCTION_TYPE
11391 && code
!= METHOD_TYPE
)
11392 type
= tsubst (type
, args
, complain
, in_decl
);
11393 if (type
== error_mark_node
)
11394 return error_mark_node
;
11400 case ENUMERAL_TYPE
:
11401 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
11402 /*entering_scope=*/0);
11405 case IDENTIFIER_NODE
:
11416 if (t
== integer_type_node
)
11419 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
11420 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
11424 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
11426 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
11427 /*integral_constant_expression_p=*/false);
11429 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11431 if (TREE_CODE (max
) == NOP_EXPR
11432 && TREE_SIDE_EFFECTS (omax
)
11433 && !TREE_TYPE (max
))
11434 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
11436 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11437 with TREE_SIDE_EFFECTS that indicates this is not an integral
11438 constant expression. */
11439 if (processing_template_decl
11440 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
11442 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
11443 TREE_SIDE_EFFECTS (max
) = 1;
11446 return compute_array_index_type (NULL_TREE
, max
, complain
);
11449 case TEMPLATE_TYPE_PARM
:
11450 case TEMPLATE_TEMPLATE_PARM
:
11451 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11452 case TEMPLATE_PARM_INDEX
:
11457 tree arg
= NULL_TREE
;
11461 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
11462 template_parm_level_and_index (t
, &level
, &idx
);
11464 levels
= TMPL_ARGS_DEPTH (args
);
11465 if (level
<= levels
)
11467 arg
= TMPL_ARG (args
, level
, idx
);
11469 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
11471 /* See through ARGUMENT_PACK_SELECT arguments. */
11472 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
11473 /* If the selected argument is an expansion E, that most
11474 likely means we were called from
11475 gen_elem_of_pack_expansion_instantiation during the
11476 substituting of pack an argument pack (which Ith
11477 element is a pack expansion, where I is
11478 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11479 In this case, the Ith element resulting from this
11480 substituting is going to be a pack expansion, which
11481 pattern is the pattern of E. Let's return the
11483 gen_elem_of_pack_expansion_instantiation will
11484 build the resulting pack expansion from it. */
11485 if (PACK_EXPANSION_P (arg
))
11486 arg
= PACK_EXPANSION_PATTERN (arg
);
11490 if (arg
== error_mark_node
)
11491 return error_mark_node
;
11492 else if (arg
!= NULL_TREE
)
11494 if (ARGUMENT_PACK_P (arg
))
11495 /* If ARG is an argument pack, we don't actually want to
11496 perform a substitution here, because substitutions
11497 for argument packs are only done
11498 element-by-element. We can get to this point when
11499 substituting the type of a non-type template
11500 parameter pack, when that type actually contains
11501 template parameter packs from an outer template, e.g.,
11503 template<typename... Types> struct A {
11504 template<Types... Values> struct B { };
11508 if (code
== TEMPLATE_TYPE_PARM
)
11511 gcc_assert (TYPE_P (arg
));
11513 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
11515 return cp_build_qualified_type_real
11516 (arg
, quals
, complain
| tf_ignore_bad_quals
);
11518 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11520 /* We are processing a type constructed from a
11521 template template parameter. */
11522 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
11523 args
, complain
, in_decl
);
11524 if (argvec
== error_mark_node
)
11525 return error_mark_node
;
11527 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
11528 || TREE_CODE (arg
) == TEMPLATE_DECL
11529 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
11531 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
11532 /* Consider this code:
11534 template <template <class> class Template>
11536 template <class Arg> using Bind = Template<Arg>;
11539 template <template <class> class Template, class Arg>
11540 using Instantiate = Template<Arg>; //#0
11542 template <template <class> class Template,
11545 Instantiate<Internal<Template>::template Bind,
11548 When #1 is parsed, the
11549 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11550 parameter `Template' in #0 matches the
11551 UNBOUND_CLASS_TEMPLATE representing the argument
11552 `Internal<Template>::template Bind'; We then want
11553 to assemble the type `Bind<Argument>' that can't
11554 be fully created right now, because
11555 `Internal<Template>' not being complete, the Bind
11556 template cannot be looked up in that context. So
11557 we need to "store" `Bind<Argument>' for later
11558 when the context of Bind becomes complete. Let's
11559 store that in a TYPENAME_TYPE. */
11560 return make_typename_type (TYPE_CONTEXT (arg
),
11561 build_nt (TEMPLATE_ID_EXPR
,
11562 TYPE_IDENTIFIER (arg
),
11567 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11568 are resolving nested-types in the signature of a
11569 member function templates. Otherwise ARG is a
11570 TEMPLATE_DECL and is the real template to be
11572 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
11573 arg
= TYPE_NAME (arg
);
11575 r
= lookup_template_class (arg
,
11577 DECL_CONTEXT (arg
),
11578 /*entering_scope=*/0,
11580 return cp_build_qualified_type_real
11581 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
11584 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11585 return convert_from_reference (unshare_expr (arg
));
11589 /* This can happen during the attempted tsubst'ing in
11590 unify. This means that we don't yet have any information
11591 about the template parameter in question. */
11594 /* Early in template argument deduction substitution, we don't
11595 want to reduce the level of 'auto', or it will be confused
11596 with a normal template parm in subsequent deduction. */
11597 if (is_auto (t
) && (complain
& tf_partial
))
11600 /* If we get here, we must have been looking at a parm for a
11601 more deeply nested template. Make a new version of this
11602 template parameter, but with a lower level. */
11605 case TEMPLATE_TYPE_PARM
:
11606 case TEMPLATE_TEMPLATE_PARM
:
11607 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11608 if (cp_type_quals (t
))
11610 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
11611 r
= cp_build_qualified_type_real
11612 (r
, cp_type_quals (t
),
11613 complain
| (code
== TEMPLATE_TYPE_PARM
11614 ? tf_ignore_bad_quals
: 0));
11619 TEMPLATE_TYPE_PARM_INDEX (r
)
11620 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
11621 r
, levels
, args
, complain
);
11622 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
11623 TYPE_MAIN_VARIANT (r
) = r
;
11624 TYPE_POINTER_TO (r
) = NULL_TREE
;
11625 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
11627 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
11628 /* We have reduced the level of the template
11629 template parameter, but not the levels of its
11630 template parameters, so canonical_type_parameter
11631 will not be able to find the canonical template
11632 template parameter for this level. Thus, we
11633 require structural equality checking to compare
11634 TEMPLATE_TEMPLATE_PARMs. */
11635 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11636 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
11637 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11639 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
11641 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11643 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
11644 complain
, in_decl
);
11645 if (argvec
== error_mark_node
)
11646 return error_mark_node
;
11648 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
11649 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
11654 case TEMPLATE_PARM_INDEX
:
11655 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
11659 gcc_unreachable ();
11667 tree purpose
, value
, chain
;
11669 if (t
== void_list_node
)
11672 purpose
= TREE_PURPOSE (t
);
11675 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
11676 if (purpose
== error_mark_node
)
11677 return error_mark_node
;
11679 value
= TREE_VALUE (t
);
11682 value
= tsubst (value
, args
, complain
, in_decl
);
11683 if (value
== error_mark_node
)
11684 return error_mark_node
;
11686 chain
= TREE_CHAIN (t
);
11687 if (chain
&& chain
!= void_type_node
)
11689 chain
= tsubst (chain
, args
, complain
, in_decl
);
11690 if (chain
== error_mark_node
)
11691 return error_mark_node
;
11693 if (purpose
== TREE_PURPOSE (t
)
11694 && value
== TREE_VALUE (t
)
11695 && chain
== TREE_CHAIN (t
))
11697 return hash_tree_cons (purpose
, value
, chain
);
11701 /* We should never be tsubsting a binfo. */
11702 gcc_unreachable ();
11705 /* A vector of template arguments. */
11706 gcc_assert (!type
);
11707 return tsubst_template_args (t
, args
, complain
, in_decl
);
11710 case REFERENCE_TYPE
:
11712 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
11717 Type deduction may fail for any of the following
11720 -- Attempting to create a pointer to reference type.
11721 -- Attempting to create a reference to a reference type or
11722 a reference to void.
11724 Core issue 106 says that creating a reference to a reference
11725 during instantiation is no longer a cause for failure. We
11726 only enforce this check in strict C++98 mode. */
11727 if ((TREE_CODE (type
) == REFERENCE_TYPE
11728 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
11729 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
11731 static location_t last_loc
;
11733 /* We keep track of the last time we issued this error
11734 message to avoid spewing a ton of messages during a
11735 single bad template instantiation. */
11736 if (complain
& tf_error
11737 && last_loc
!= input_location
)
11739 if (VOID_TYPE_P (type
))
11740 error ("forming reference to void");
11741 else if (code
== POINTER_TYPE
)
11742 error ("forming pointer to reference type %qT", type
);
11744 error ("forming reference to reference type %qT", type
);
11745 last_loc
= input_location
;
11748 return error_mark_node
;
11750 else if (TREE_CODE (type
) == FUNCTION_TYPE
11751 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
11752 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
11754 if (complain
& tf_error
)
11756 if (code
== POINTER_TYPE
)
11757 error ("forming pointer to qualified function type %qT",
11760 error ("forming reference to qualified function type %qT",
11763 return error_mark_node
;
11765 else if (code
== POINTER_TYPE
)
11767 r
= build_pointer_type (type
);
11768 if (TREE_CODE (type
) == METHOD_TYPE
)
11769 r
= build_ptrmemfunc_type (r
);
11771 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
11772 /* In C++0x, during template argument substitution, when there is an
11773 attempt to create a reference to a reference type, reference
11774 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11776 "If a template-argument for a template-parameter T names a type
11777 that is a reference to a type A, an attempt to create the type
11778 'lvalue reference to cv T' creates the type 'lvalue reference to
11779 A,' while an attempt to create the type type rvalue reference to
11780 cv T' creates the type T"
11782 r
= cp_build_reference_type
11784 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
11786 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
11787 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11789 if (cxx_dialect
>= cxx1y
11790 && !(TREE_CODE (t
) == REFERENCE_TYPE
&& REFERENCE_VLA_OK (t
))
11791 && array_of_runtime_bound_p (type
))
11793 if (complain
& tf_warning_or_error
)
11795 (input_location
, OPT_Wvla
,
11796 code
== REFERENCE_TYPE
11797 ? G_("cannot declare reference to array of runtime bound")
11798 : G_("cannot declare pointer to array of runtime bound"));
11800 r
= error_mark_node
;
11803 if (r
!= error_mark_node
)
11804 /* Will this ever be needed for TYPE_..._TO values? */
11811 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
11812 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
11816 Type deduction may fail for any of the following
11819 -- Attempting to create "pointer to member of T" when T
11820 is not a class type. */
11821 if (complain
& tf_error
)
11822 error ("creating pointer to member of non-class type %qT", r
);
11823 return error_mark_node
;
11825 if (TREE_CODE (type
) == REFERENCE_TYPE
)
11827 if (complain
& tf_error
)
11828 error ("creating pointer to member reference type %qT", type
);
11829 return error_mark_node
;
11831 if (VOID_TYPE_P (type
))
11833 if (complain
& tf_error
)
11834 error ("creating pointer to member of type void");
11835 return error_mark_node
;
11837 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
11838 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11840 /* The type of the implicit object parameter gets its
11841 cv-qualifiers from the FUNCTION_TYPE. */
11844 = build_memfn_type (type
, r
, type_memfn_quals (type
),
11845 type_memfn_rqual (type
));
11846 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
11847 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
11851 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
11855 case FUNCTION_TYPE
:
11860 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
11861 if (fntype
== error_mark_node
)
11862 return error_mark_node
;
11864 /* Substitute the exception specification. */
11865 specs
= tsubst_exception_specification (t
, args
, complain
,
11866 in_decl
, /*defer_ok*/true);
11867 if (specs
== error_mark_node
)
11868 return error_mark_node
;
11870 fntype
= build_exception_variant (fntype
, specs
);
11875 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
11876 if (domain
== error_mark_node
)
11877 return error_mark_node
;
11879 /* As an optimization, we avoid regenerating the array type if
11880 it will obviously be the same as T. */
11881 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
11884 /* These checks should match the ones in grokdeclarator.
11888 The deduction may fail for any of the following reasons:
11890 -- Attempting to create an array with an element type that
11891 is void, a function type, or a reference type, or [DR337]
11892 an abstract class type. */
11893 if (VOID_TYPE_P (type
)
11894 || TREE_CODE (type
) == FUNCTION_TYPE
11895 || TREE_CODE (type
) == REFERENCE_TYPE
)
11897 if (complain
& tf_error
)
11898 error ("creating array of %qT", type
);
11899 return error_mark_node
;
11902 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
11903 return error_mark_node
;
11905 r
= build_cplus_array_type (type
, domain
);
11907 if (TYPE_USER_ALIGN (t
))
11909 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
11910 TYPE_USER_ALIGN (r
) = 1;
11916 case TYPENAME_TYPE
:
11918 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11919 in_decl
, /*entering_scope=*/1);
11920 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
11921 complain
, in_decl
);
11923 if (ctx
== error_mark_node
|| f
== error_mark_node
)
11924 return error_mark_node
;
11926 if (!MAYBE_CLASS_TYPE_P (ctx
))
11928 if (complain
& tf_error
)
11929 error ("%qT is not a class, struct, or union type", ctx
);
11930 return error_mark_node
;
11932 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
11934 /* Normally, make_typename_type does not require that the CTX
11935 have complete type in order to allow things like:
11937 template <class T> struct S { typename S<T>::X Y; };
11939 But, such constructs have already been resolved by this
11940 point, so here CTX really should have complete type, unless
11941 it's a partial instantiation. */
11942 ctx
= complete_type (ctx
);
11943 if (!COMPLETE_TYPE_P (ctx
))
11945 if (complain
& tf_error
)
11946 cxx_incomplete_type_error (NULL_TREE
, ctx
);
11947 return error_mark_node
;
11951 f
= make_typename_type (ctx
, f
, typename_type
,
11952 complain
| tf_keep_type_decl
);
11953 if (f
== error_mark_node
)
11955 if (TREE_CODE (f
) == TYPE_DECL
)
11957 complain
|= tf_ignore_bad_quals
;
11961 if (TREE_CODE (f
) != TYPENAME_TYPE
)
11963 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
11965 if (complain
& tf_error
)
11966 error ("%qT resolves to %qT, which is not an enumeration type",
11969 return error_mark_node
;
11971 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
11973 if (complain
& tf_error
)
11974 error ("%qT resolves to %qT, which is is not a class type",
11977 return error_mark_node
;
11981 return cp_build_qualified_type_real
11982 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
11985 case UNBOUND_CLASS_TEMPLATE
:
11987 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11988 in_decl
, /*entering_scope=*/1);
11989 tree name
= TYPE_IDENTIFIER (t
);
11990 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
11992 if (ctx
== error_mark_node
|| name
== error_mark_node
)
11993 return error_mark_node
;
11996 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
11997 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
12004 ++cp_unevaluated_operand
;
12005 ++c_inhibit_evaluation_warnings
;
12007 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
12009 /*integral_constant_expression_p=*/false);
12011 --cp_unevaluated_operand
;
12012 --c_inhibit_evaluation_warnings
;
12014 type
= finish_typeof (type
);
12015 return cp_build_qualified_type_real (type
,
12017 | cp_type_quals (type
),
12021 case DECLTYPE_TYPE
:
12025 ++cp_unevaluated_operand
;
12026 ++c_inhibit_evaluation_warnings
;
12028 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
12029 complain
|tf_decltype
, in_decl
,
12030 /*function_p*/false,
12031 /*integral_constant_expression*/false);
12033 --cp_unevaluated_operand
;
12034 --c_inhibit_evaluation_warnings
;
12036 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
12037 type
= lambda_capture_field_type (type
,
12038 DECLTYPE_FOR_INIT_CAPTURE (t
));
12039 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
12040 type
= lambda_proxy_type (type
);
12043 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
12044 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
12046 /* In a template ~id could be either a complement expression
12047 or an unqualified-id naming a destructor; if instantiating
12048 it produces an expression, it's not an id-expression or
12051 type
= finish_decltype_type (type
, id
, complain
);
12053 return cp_build_qualified_type_real (type
,
12055 | cp_type_quals (type
),
12059 case UNDERLYING_TYPE
:
12061 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
12062 complain
, in_decl
);
12063 return finish_underlying_type (type
);
12066 case TYPE_ARGUMENT_PACK
:
12067 case NONTYPE_ARGUMENT_PACK
:
12069 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
12071 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
12075 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
12077 /* For template nontype argument packs, also substitute into
12079 if (code
== NONTYPE_ARGUMENT_PACK
)
12080 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12098 /* We should use one of the expression tsubsts for these codes. */
12099 gcc_unreachable ();
12102 sorry ("use of %qs in template", get_tree_code_name (code
));
12103 return error_mark_node
;
12107 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12108 type of the expression on the left-hand side of the "." or "->"
12112 tsubst_baselink (tree baselink
, tree object_type
,
12113 tree args
, tsubst_flags_t complain
, tree in_decl
)
12116 tree qualifying_scope
;
12119 tree template_args
= 0;
12120 bool template_id_p
= false;
12121 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
12123 /* A baselink indicates a function from a base class. Both the
12124 BASELINK_ACCESS_BINFO and the base class referenced may
12125 indicate bases of the template class, rather than the
12126 instantiated class. In addition, lookups that were not
12127 ambiguous before may be ambiguous now. Therefore, we perform
12128 the lookup again. */
12129 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
12130 qualifying_scope
= tsubst (qualifying_scope
, args
,
12131 complain
, in_decl
);
12132 fns
= BASELINK_FUNCTIONS (baselink
);
12133 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
12134 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
12136 template_id_p
= true;
12137 template_args
= TREE_OPERAND (fns
, 1);
12138 fns
= TREE_OPERAND (fns
, 0);
12140 template_args
= tsubst_template_args (template_args
, args
,
12141 complain
, in_decl
);
12143 name
= DECL_NAME (get_first_fn (fns
));
12144 if (IDENTIFIER_TYPENAME_P (name
))
12145 name
= mangle_conv_op_name_for_type (optype
);
12146 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
12148 return error_mark_node
;
12150 /* If lookup found a single function, mark it as used at this
12151 point. (If it lookup found multiple functions the one selected
12152 later by overload resolution will be marked as used at that
12154 if (BASELINK_P (baselink
))
12155 fns
= BASELINK_FUNCTIONS (baselink
);
12156 if (!template_id_p
&& !really_overloaded_fn (fns
))
12157 mark_used (OVL_CURRENT (fns
));
12159 /* Add back the template arguments, if present. */
12160 if (BASELINK_P (baselink
) && template_id_p
)
12161 BASELINK_FUNCTIONS (baselink
)
12162 = build_nt (TEMPLATE_ID_EXPR
,
12163 BASELINK_FUNCTIONS (baselink
),
12165 /* Update the conversion operator type. */
12166 BASELINK_OPTYPE (baselink
) = optype
;
12169 object_type
= current_class_type
;
12172 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
12178 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12179 true if the qualified-id will be a postfix-expression in-and-of
12180 itself; false if more of the postfix-expression follows the
12181 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12185 tsubst_qualified_id (tree qualified_id
, tree args
,
12186 tsubst_flags_t complain
, tree in_decl
,
12187 bool done
, bool address_p
)
12193 tree template_args
;
12194 location_t loc
= UNKNOWN_LOCATION
;
12196 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
12198 /* Figure out what name to look up. */
12199 name
= TREE_OPERAND (qualified_id
, 1);
12200 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
12202 is_template
= true;
12203 loc
= EXPR_LOCATION (name
);
12204 template_args
= TREE_OPERAND (name
, 1);
12206 template_args
= tsubst_template_args (template_args
, args
,
12207 complain
, in_decl
);
12208 name
= TREE_OPERAND (name
, 0);
12212 is_template
= false;
12213 template_args
= NULL_TREE
;
12216 /* Substitute into the qualifying scope. When there are no ARGS, we
12217 are just trying to simplify a non-dependent expression. In that
12218 case the qualifying scope may be dependent, and, in any case,
12219 substituting will not help. */
12220 scope
= TREE_OPERAND (qualified_id
, 0);
12223 scope
= tsubst (scope
, args
, complain
, in_decl
);
12224 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
12229 if (dependent_scope_p (scope
))
12232 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
12233 return build_qualified_name (NULL_TREE
, scope
, expr
,
12234 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
12237 if (!BASELINK_P (name
) && !DECL_P (expr
))
12239 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
12241 /* A BIT_NOT_EXPR is used to represent a destructor. */
12242 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
12244 error ("qualifying type %qT does not match destructor name ~%qT",
12245 scope
, TREE_OPERAND (expr
, 0));
12246 expr
= error_mark_node
;
12249 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
12250 /*is_type_p=*/0, false);
12253 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
12254 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
12255 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
12257 if (complain
& tf_error
)
12259 error ("dependent-name %qE is parsed as a non-type, but "
12260 "instantiation yields a type", qualified_id
);
12261 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
12263 return error_mark_node
;
12269 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
12271 /* Remember that there was a reference to this entity. */
12275 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
12277 if (complain
& tf_error
)
12278 qualified_name_lookup_error (scope
,
12279 TREE_OPERAND (qualified_id
, 1),
12280 expr
, input_location
);
12281 return error_mark_node
;
12285 expr
= lookup_template_function (expr
, template_args
);
12287 if (expr
== error_mark_node
&& complain
& tf_error
)
12288 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
12289 expr
, input_location
);
12290 else if (TYPE_P (scope
))
12292 expr
= (adjust_result_of_qualified_name_lookup
12293 (expr
, scope
, current_nonlambda_class_type ()));
12294 expr
= (finish_qualified_id_expr
12295 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
12296 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
12297 /*template_arg_p=*/false, complain
));
12300 /* Expressions do not generally have reference type. */
12301 if (TREE_CODE (expr
) != SCOPE_REF
12302 /* However, if we're about to form a pointer-to-member, we just
12303 want the referenced member referenced. */
12304 && TREE_CODE (expr
) != OFFSET_REF
)
12305 expr
= convert_from_reference (expr
);
12310 /* Like tsubst, but deals with expressions. This function just replaces
12311 template parms; to finish processing the resultant expression, use
12312 tsubst_copy_and_build or tsubst_expr. */
12315 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12317 enum tree_code code
;
12320 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
12323 code
= TREE_CODE (t
);
12328 r
= retrieve_local_specialization (t
);
12330 if (r
== NULL_TREE
)
12332 /* We get here for a use of 'this' in an NSDMI. */
12333 if (DECL_NAME (t
) == this_identifier
12334 && at_function_scope_p ()
12335 && DECL_CONSTRUCTOR_P (current_function_decl
))
12336 return current_class_ptr
;
12338 /* This can happen for a parameter name used later in a function
12339 declaration (such as in a late-specified return type). Just
12340 make a dummy decl, since it's only used for its type. */
12341 gcc_assert (cp_unevaluated_operand
!= 0);
12342 r
= tsubst_decl (t
, args
, complain
);
12343 /* Give it the template pattern as its context; its true context
12344 hasn't been instantiated yet and this is good enough for
12346 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
12349 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12350 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12359 if (DECL_TEMPLATE_PARM_P (t
))
12360 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
12361 /* There is no need to substitute into namespace-scope
12363 if (DECL_NAMESPACE_SCOPE_P (t
))
12365 /* If ARGS is NULL, then T is known to be non-dependent. */
12366 if (args
== NULL_TREE
)
12367 return integral_constant_value (t
);
12369 /* Unfortunately, we cannot just call lookup_name here.
12372 template <int I> int f() {
12374 struct S { void g() { E e = a; } };
12377 When we instantiate f<7>::S::g(), say, lookup_name is not
12378 clever enough to find f<7>::a. */
12380 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12381 /*entering_scope=*/0);
12383 for (v
= TYPE_VALUES (enum_type
);
12385 v
= TREE_CHAIN (v
))
12386 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
12387 return TREE_VALUE (v
);
12389 /* We didn't find the name. That should never happen; if
12390 name-lookup found it during preliminary parsing, we
12391 should find it again here during instantiation. */
12392 gcc_unreachable ();
12397 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
12399 /* Check for a local specialization set up by
12400 tsubst_pack_expansion. */
12401 tree r
= retrieve_local_specialization (t
);
12404 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12405 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12409 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12410 tsubst_decl put in the hash table. */
12411 return retrieve_specialization (t
, args
, 0);
12414 if (DECL_CONTEXT (t
))
12418 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12419 /*entering_scope=*/1);
12420 if (ctx
!= DECL_CONTEXT (t
))
12422 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
12425 if (complain
& tf_error
)
12426 error ("using invalid field %qD", t
);
12427 return error_mark_node
;
12436 case FUNCTION_DECL
:
12437 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
12438 r
= tsubst (t
, args
, complain
, in_decl
);
12439 else if (local_variable_p (t
))
12441 r
= retrieve_local_specialization (t
);
12442 if (r
== NULL_TREE
)
12444 if (DECL_ANON_UNION_VAR_P (t
))
12446 /* Just use name lookup to find a member alias for an
12447 anonymous union, but then add it to the hash table. */
12448 r
= lookup_name (DECL_NAME (t
));
12449 gcc_assert (DECL_ANON_UNION_VAR_P (r
));
12450 register_local_specialization (r
, t
);
12454 gcc_assert (errorcount
|| sorrycount
);
12455 return error_mark_node
;
12464 case NAMESPACE_DECL
:
12468 /* An OVERLOAD will always be a non-dependent overload set; an
12469 overload set from function scope will just be represented with an
12470 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12471 gcc_assert (!uses_template_parms (t
));
12475 return tsubst_baselink (t
, current_nonlambda_class_type (),
12476 args
, complain
, in_decl
);
12478 case TEMPLATE_DECL
:
12479 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12480 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
12481 args
, complain
, in_decl
);
12482 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
12483 return tsubst (t
, args
, complain
, in_decl
);
12484 else if (DECL_CLASS_SCOPE_P (t
)
12485 && uses_template_parms (DECL_CONTEXT (t
)))
12487 /* Template template argument like the following example need
12490 template <template <class> class TT> struct C {};
12491 template <class T> struct D {
12492 template <class U> struct E {};
12497 We are processing the template argument `E' in #1 for
12498 the template instantiation #2. Originally, `E' is a
12499 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12500 have to substitute this with one having context `D<int>'. */
12502 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
12503 return lookup_field (context
, DECL_NAME(t
), 0, false);
12506 /* Ordinary template template argument. */
12510 case REINTERPRET_CAST_EXPR
:
12511 case CONST_CAST_EXPR
:
12512 case STATIC_CAST_EXPR
:
12513 case DYNAMIC_CAST_EXPR
:
12514 case IMPLICIT_CONV_EXPR
:
12518 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12519 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12522 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
12525 tree expanded
, op
= TREE_OPERAND (t
, 0);
12528 if (SIZEOF_EXPR_TYPE_P (t
))
12529 op
= TREE_TYPE (op
);
12531 ++cp_unevaluated_operand
;
12532 ++c_inhibit_evaluation_warnings
;
12533 /* We only want to compute the number of arguments. */
12534 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
12535 --cp_unevaluated_operand
;
12536 --c_inhibit_evaluation_warnings
;
12538 if (TREE_CODE (expanded
) == TREE_VEC
)
12539 len
= TREE_VEC_LENGTH (expanded
);
12541 if (expanded
== error_mark_node
)
12542 return error_mark_node
;
12543 else if (PACK_EXPANSION_P (expanded
)
12544 || (TREE_CODE (expanded
) == TREE_VEC
12546 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
12548 if (TREE_CODE (expanded
) == TREE_VEC
)
12549 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
12551 if (TYPE_P (expanded
))
12552 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
12553 complain
& tf_error
);
12555 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
12556 complain
& tf_error
);
12559 return build_int_cst (size_type_node
, len
);
12561 if (SIZEOF_EXPR_TYPE_P (t
))
12563 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
12564 args
, complain
, in_decl
);
12565 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
12566 r
= build1 (SIZEOF_EXPR
,
12567 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
12568 SIZEOF_EXPR_TYPE_P (r
) = 1;
12575 case TRUTH_NOT_EXPR
:
12578 case UNARY_PLUS_EXPR
: /* Unary + */
12580 case AT_ENCODE_EXPR
:
12584 case REALPART_EXPR
:
12585 case IMAGPART_EXPR
:
12588 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12589 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12591 case COMPONENT_REF
:
12596 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
12597 name
= TREE_OPERAND (t
, 1);
12598 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
12600 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12601 complain
, in_decl
);
12602 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12604 else if (TREE_CODE (name
) == SCOPE_REF
12605 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
12607 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12608 complain
, in_decl
);
12609 name
= TREE_OPERAND (name
, 1);
12610 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12611 complain
, in_decl
);
12612 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12613 name
= build_qualified_name (/*type=*/NULL_TREE
,
12615 /*template_p=*/false);
12617 else if (BASELINK_P (name
))
12618 name
= tsubst_baselink (name
,
12619 non_reference (TREE_TYPE (object
)),
12623 name
= tsubst_copy (name
, args
, complain
, in_decl
);
12624 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
12630 case TRUNC_DIV_EXPR
:
12631 case CEIL_DIV_EXPR
:
12632 case FLOOR_DIV_EXPR
:
12633 case ROUND_DIV_EXPR
:
12634 case EXACT_DIV_EXPR
:
12638 case TRUNC_MOD_EXPR
:
12639 case FLOOR_MOD_EXPR
:
12640 case TRUTH_ANDIF_EXPR
:
12641 case TRUTH_ORIF_EXPR
:
12642 case TRUTH_AND_EXPR
:
12643 case TRUTH_OR_EXPR
:
12656 case COMPOUND_EXPR
:
12659 case PREDECREMENT_EXPR
:
12660 case PREINCREMENT_EXPR
:
12661 case POSTDECREMENT_EXPR
:
12662 case POSTINCREMENT_EXPR
:
12664 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12665 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12668 return build_qualified_name (/*type=*/NULL_TREE
,
12669 tsubst_copy (TREE_OPERAND (t
, 0),
12670 args
, complain
, in_decl
),
12671 tsubst_copy (TREE_OPERAND (t
, 1),
12672 args
, complain
, in_decl
),
12673 QUALIFIED_NAME_IS_TEMPLATE (t
));
12678 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12679 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12680 NULL_TREE
, NULL_TREE
);
12684 int n
= VL_EXP_OPERAND_LENGTH (t
);
12685 tree result
= build_vl_exp (CALL_EXPR
, n
);
12687 for (i
= 0; i
< n
; i
++)
12688 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
12689 complain
, in_decl
);
12695 case PSEUDO_DTOR_EXPR
:
12696 case VEC_PERM_EXPR
:
12699 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12700 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12701 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12702 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12709 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12710 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12711 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12712 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
12719 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12720 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12721 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
12722 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
12726 case TEMPLATE_ID_EXPR
:
12728 /* Substituted template arguments */
12729 tree fn
= TREE_OPERAND (t
, 0);
12730 tree targs
= TREE_OPERAND (t
, 1);
12732 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
12734 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12736 return lookup_template_function (fn
, targs
);
12741 tree purpose
, value
, chain
;
12743 if (t
== void_list_node
)
12746 purpose
= TREE_PURPOSE (t
);
12748 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
12749 value
= TREE_VALUE (t
);
12751 value
= tsubst_copy (value
, args
, complain
, in_decl
);
12752 chain
= TREE_CHAIN (t
);
12753 if (chain
&& chain
!= void_type_node
)
12754 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
12755 if (purpose
== TREE_PURPOSE (t
)
12756 && value
== TREE_VALUE (t
)
12757 && chain
== TREE_CHAIN (t
))
12759 return tree_cons (purpose
, value
, chain
);
12764 case ENUMERAL_TYPE
:
12766 case TEMPLATE_TYPE_PARM
:
12767 case TEMPLATE_TEMPLATE_PARM
:
12768 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12769 case TEMPLATE_PARM_INDEX
:
12771 case REFERENCE_TYPE
:
12773 case FUNCTION_TYPE
:
12776 case TYPENAME_TYPE
:
12777 case UNBOUND_CLASS_TEMPLATE
:
12779 case DECLTYPE_TYPE
:
12781 return tsubst (t
, args
, complain
, in_decl
);
12785 /* Fall through. */
12786 case IDENTIFIER_NODE
:
12787 if (IDENTIFIER_TYPENAME_P (t
))
12789 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12790 return mangle_conv_op_name_for_type (new_type
);
12796 /* This is handled by tsubst_copy_and_build. */
12797 gcc_unreachable ();
12800 return build_x_va_arg (EXPR_LOCATION (t
),
12801 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
12803 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
12805 case CLEANUP_POINT_EXPR
:
12806 /* We shouldn't have built any of these during initial template
12807 generation. Instead, they should be built during instantiation
12808 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12809 gcc_unreachable ();
12813 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12814 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12815 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12816 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
12817 mark_used (TREE_OPERAND (r
, 1));
12820 case EXPR_PACK_EXPANSION
:
12821 error ("invalid use of pack expansion expression");
12822 return error_mark_node
;
12824 case NONTYPE_ARGUMENT_PACK
:
12825 error ("use %<...%> to expand argument pack");
12826 return error_mark_node
;
12833 /* Instantiate any typedefs in the type. */
12834 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12835 r
= fold_convert (type
, t
);
12836 gcc_assert (TREE_CODE (r
) == code
);
12841 /* These can sometimes show up in a partial instantiation, but never
12842 involve template parms. */
12843 gcc_assert (!uses_template_parms (t
));
12847 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12848 gcc_checking_assert (false);
12853 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12856 tsubst_omp_clauses (tree clauses
, bool declare_simd
,
12857 tree args
, tsubst_flags_t complain
, tree in_decl
)
12859 tree new_clauses
= NULL
, nc
, oc
;
12861 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
12863 nc
= copy_node (oc
);
12864 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
12867 switch (OMP_CLAUSE_CODE (nc
))
12869 case OMP_CLAUSE_LASTPRIVATE
:
12870 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
12872 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
12873 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
12874 in_decl
, /*integral_constant_expression_p=*/false);
12875 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
12876 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
12879 case OMP_CLAUSE_PRIVATE
:
12880 case OMP_CLAUSE_SHARED
:
12881 case OMP_CLAUSE_FIRSTPRIVATE
:
12882 case OMP_CLAUSE_COPYIN
:
12883 case OMP_CLAUSE_COPYPRIVATE
:
12884 case OMP_CLAUSE_IF
:
12885 case OMP_CLAUSE_NUM_THREADS
:
12886 case OMP_CLAUSE_SCHEDULE
:
12887 case OMP_CLAUSE_COLLAPSE
:
12888 case OMP_CLAUSE_FINAL
:
12889 case OMP_CLAUSE_DEPEND
:
12890 case OMP_CLAUSE_FROM
:
12891 case OMP_CLAUSE_TO
:
12892 case OMP_CLAUSE_UNIFORM
:
12893 case OMP_CLAUSE_MAP
:
12894 case OMP_CLAUSE_DEVICE
:
12895 case OMP_CLAUSE_DIST_SCHEDULE
:
12896 case OMP_CLAUSE_NUM_TEAMS
:
12897 case OMP_CLAUSE_THREAD_LIMIT
:
12898 case OMP_CLAUSE_SAFELEN
:
12899 case OMP_CLAUSE_SIMDLEN
:
12900 OMP_CLAUSE_OPERAND (nc
, 0)
12901 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12902 in_decl
, /*integral_constant_expression_p=*/false);
12904 case OMP_CLAUSE_REDUCTION
:
12905 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
12907 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
12908 if (TREE_CODE (placeholder
) == SCOPE_REF
)
12910 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
12911 complain
, in_decl
);
12912 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
12913 = build_qualified_name (NULL_TREE
, scope
,
12914 TREE_OPERAND (placeholder
, 1),
12918 gcc_assert (identifier_p (placeholder
));
12920 OMP_CLAUSE_OPERAND (nc
, 0)
12921 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12922 in_decl
, /*integral_constant_expression_p=*/false);
12924 case OMP_CLAUSE_LINEAR
:
12925 case OMP_CLAUSE_ALIGNED
:
12926 OMP_CLAUSE_OPERAND (nc
, 0)
12927 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12928 in_decl
, /*integral_constant_expression_p=*/false);
12929 OMP_CLAUSE_OPERAND (nc
, 1)
12930 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
12931 in_decl
, /*integral_constant_expression_p=*/false);
12934 case OMP_CLAUSE_NOWAIT
:
12935 case OMP_CLAUSE_ORDERED
:
12936 case OMP_CLAUSE_DEFAULT
:
12937 case OMP_CLAUSE_UNTIED
:
12938 case OMP_CLAUSE_MERGEABLE
:
12939 case OMP_CLAUSE_INBRANCH
:
12940 case OMP_CLAUSE_NOTINBRANCH
:
12941 case OMP_CLAUSE_PROC_BIND
:
12942 case OMP_CLAUSE_FOR
:
12943 case OMP_CLAUSE_PARALLEL
:
12944 case OMP_CLAUSE_SECTIONS
:
12945 case OMP_CLAUSE_TASKGROUP
:
12948 gcc_unreachable ();
12952 new_clauses
= nreverse (new_clauses
);
12954 new_clauses
= finish_omp_clauses (new_clauses
);
12955 return new_clauses
;
12958 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12961 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
12964 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12966 tree purpose
, value
, chain
;
12971 if (TREE_CODE (t
) != TREE_LIST
)
12972 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12973 /*function_p=*/false,
12974 /*integral_constant_expression_p=*/false);
12976 if (t
== void_list_node
)
12979 purpose
= TREE_PURPOSE (t
);
12981 purpose
= RECUR (purpose
);
12982 value
= TREE_VALUE (t
);
12985 if (TREE_CODE (value
) != LABEL_DECL
)
12986 value
= RECUR (value
);
12989 value
= lookup_label (DECL_NAME (value
));
12990 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
12991 TREE_USED (value
) = 1;
12994 chain
= TREE_CHAIN (t
);
12995 if (chain
&& chain
!= void_type_node
)
12996 chain
= RECUR (chain
);
12997 return tree_cons (purpose
, value
, chain
);
13001 /* Substitute one OMP_FOR iterator. */
13004 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
13005 tree condv
, tree incrv
, tree
*clauses
,
13006 tree args
, tsubst_flags_t complain
, tree in_decl
,
13007 bool integral_constant_expression_p
)
13009 #define RECUR(NODE) \
13010 tsubst_expr ((NODE), args, complain, in_decl, \
13011 integral_constant_expression_p)
13012 tree decl
, init
, cond
, incr
;
13015 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
13016 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
13017 decl
= TREE_OPERAND (init
, 0);
13018 init
= TREE_OPERAND (init
, 1);
13019 /* Do this before substituting into decl to handle 'auto'. */
13020 init_decl
= (init
&& TREE_CODE (init
) == DECL_EXPR
);
13021 init
= RECUR (init
);
13022 decl
= RECUR (decl
);
13025 gcc_assert (!processing_template_decl
);
13026 init
= DECL_INITIAL (decl
);
13027 DECL_INITIAL (decl
) = NULL_TREE
;
13030 gcc_assert (!type_dependent_expression_p (decl
));
13032 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
13034 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
13035 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
13036 if (TREE_CODE (incr
) == MODIFY_EXPR
)
13037 incr
= build_x_modify_expr (EXPR_LOCATION (incr
),
13038 RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
13039 RECUR (TREE_OPERAND (incr
, 1)),
13042 incr
= RECUR (incr
);
13043 TREE_VEC_ELT (declv
, i
) = decl
;
13044 TREE_VEC_ELT (initv
, i
) = init
;
13045 TREE_VEC_ELT (condv
, i
) = cond
;
13046 TREE_VEC_ELT (incrv
, i
) = incr
;
13050 if (init
&& !init_decl
)
13053 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
13055 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
13056 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
13057 && OMP_CLAUSE_DECL (c
) == decl
)
13059 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
13060 && OMP_CLAUSE_DECL (c
) == decl
)
13061 error ("iteration variable %qD should not be firstprivate", decl
);
13062 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
13063 && OMP_CLAUSE_DECL (c
) == decl
)
13064 error ("iteration variable %qD should not be reduction", decl
);
13068 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
13069 OMP_CLAUSE_DECL (c
) = decl
;
13070 c
= finish_omp_clauses (c
);
13073 OMP_CLAUSE_CHAIN (c
) = *clauses
;
13078 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
13079 if (COMPARISON_CLASS_P (cond
))
13080 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
13081 RECUR (TREE_OPERAND (cond
, 0)),
13082 RECUR (TREE_OPERAND (cond
, 1)));
13084 cond
= RECUR (cond
);
13085 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
13086 switch (TREE_CODE (incr
))
13088 case PREINCREMENT_EXPR
:
13089 case PREDECREMENT_EXPR
:
13090 case POSTINCREMENT_EXPR
:
13091 case POSTDECREMENT_EXPR
:
13092 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
13093 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
13096 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
13097 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
13099 tree rhs
= TREE_OPERAND (incr
, 1);
13100 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
13101 RECUR (TREE_OPERAND (incr
, 0)),
13102 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
13103 RECUR (TREE_OPERAND (rhs
, 0)),
13104 RECUR (TREE_OPERAND (rhs
, 1))));
13107 incr
= RECUR (incr
);
13110 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
13111 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
13113 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
13114 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
13115 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
13116 TREE_TYPE (decl
), lhs
,
13117 RECUR (TREE_OPERAND (incr
, 2))));
13119 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
13120 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
13121 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
13123 tree rhs
= TREE_OPERAND (incr
, 2);
13124 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
13125 RECUR (TREE_OPERAND (incr
, 0)),
13126 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
13127 RECUR (TREE_OPERAND (rhs
, 0)),
13128 RECUR (TREE_OPERAND (rhs
, 1))));
13131 incr
= RECUR (incr
);
13134 incr
= RECUR (incr
);
13138 TREE_VEC_ELT (declv
, i
) = decl
;
13139 TREE_VEC_ELT (initv
, i
) = init
;
13140 TREE_VEC_ELT (condv
, i
) = cond
;
13141 TREE_VEC_ELT (incrv
, i
) = incr
;
13145 /* Like tsubst_copy for expressions, etc. but also does semantic
13149 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
13150 bool integral_constant_expression_p
)
13152 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13153 #define RECUR(NODE) \
13154 tsubst_expr ((NODE), args, complain, in_decl, \
13155 integral_constant_expression_p)
13161 if (t
== NULL_TREE
|| t
== error_mark_node
)
13164 loc
= input_location
;
13165 if (EXPR_HAS_LOCATION (t
))
13166 input_location
= EXPR_LOCATION (t
);
13167 if (STATEMENT_CODE_P (TREE_CODE (t
)))
13168 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
13170 switch (TREE_CODE (t
))
13172 case STATEMENT_LIST
:
13174 tree_stmt_iterator i
;
13175 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
13176 RECUR (tsi_stmt (i
));
13180 case CTOR_INITIALIZER
:
13181 finish_mem_initializers (tsubst_initializer_list
13182 (TREE_OPERAND (t
, 0), args
));
13186 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
13190 tmp
= RECUR (EXPR_STMT_EXPR (t
));
13191 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
13192 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
13194 finish_expr_stmt (tmp
);
13198 do_using_directive (USING_STMT_NAMESPACE (t
));
13203 tree decl
, pattern_decl
;
13206 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
13207 if (TREE_CODE (decl
) == LABEL_DECL
)
13208 finish_label_decl (DECL_NAME (decl
));
13209 else if (TREE_CODE (decl
) == USING_DECL
)
13211 tree scope
= USING_DECL_SCOPE (decl
);
13212 tree name
= DECL_NAME (decl
);
13215 scope
= tsubst (scope
, args
, complain
, in_decl
);
13216 decl
= lookup_qualified_name (scope
, name
,
13217 /*is_type_p=*/false,
13218 /*complain=*/false);
13219 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
13220 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
13222 do_local_using_decl (decl
, scope
, name
);
13224 else if (DECL_PACK_P (decl
))
13226 /* Don't build up decls for a variadic capture proxy, we'll
13227 instantiate the elements directly as needed. */
13232 init
= DECL_INITIAL (decl
);
13233 decl
= tsubst (decl
, args
, complain
, in_decl
);
13234 if (decl
!= error_mark_node
)
13236 /* By marking the declaration as instantiated, we avoid
13237 trying to instantiate it. Since instantiate_decl can't
13238 handle local variables, and since we've already done
13239 all that needs to be done, that's the right thing to
13242 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13244 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
13245 /* Anonymous aggregates are a special case. */
13246 finish_anon_union (decl
);
13247 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
13249 DECL_CONTEXT (decl
) = current_function_decl
;
13250 if (DECL_NAME (decl
) == this_identifier
)
13252 tree lam
= DECL_CONTEXT (current_function_decl
);
13253 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
13254 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
13256 insert_capture_proxy (decl
);
13258 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
13259 /* We already did a pushtag. */;
13260 else if (TREE_CODE (decl
) == FUNCTION_DECL
13261 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
13262 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
13264 DECL_CONTEXT (decl
) = NULL_TREE
;
13266 DECL_CONTEXT (decl
) = current_function_decl
;
13267 cp_check_omp_declare_reduction (decl
);
13271 int const_init
= false;
13272 maybe_push_decl (decl
);
13274 && DECL_PRETTY_FUNCTION_P (decl
))
13276 /* For __PRETTY_FUNCTION__ we have to adjust the
13278 const char *const name
13279 = cxx_printable_name (current_function_decl
, 2);
13280 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
13284 tree t
= RECUR (init
);
13288 /* If we had an initializer but it
13289 instantiated to nothing,
13290 value-initialize the object. This will
13291 only occur when the initializer was a
13292 pack expansion where the parameter packs
13293 used in that expansion were of length
13295 init
= build_value_init (TREE_TYPE (decl
),
13297 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
13298 init
= get_target_expr_sfinae (init
, complain
);
13305 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13307 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
13316 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
13317 RECUR (FOR_INIT_STMT (t
));
13318 finish_for_init_stmt (stmt
);
13319 tmp
= RECUR (FOR_COND (t
));
13320 finish_for_cond (tmp
, stmt
);
13321 tmp
= RECUR (FOR_EXPR (t
));
13322 finish_for_expr (tmp
, stmt
);
13323 RECUR (FOR_BODY (t
));
13324 finish_for_stmt (stmt
);
13327 case RANGE_FOR_STMT
:
13330 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
13331 decl
= RANGE_FOR_DECL (t
);
13332 decl
= tsubst (decl
, args
, complain
, in_decl
);
13333 maybe_push_decl (decl
);
13334 expr
= RECUR (RANGE_FOR_EXPR (t
));
13335 stmt
= cp_convert_range_for (stmt
, decl
, expr
);
13336 RECUR (RANGE_FOR_BODY (t
));
13337 finish_for_stmt (stmt
);
13342 stmt
= begin_while_stmt ();
13343 tmp
= RECUR (WHILE_COND (t
));
13344 finish_while_stmt_cond (tmp
, stmt
);
13345 RECUR (WHILE_BODY (t
));
13346 finish_while_stmt (stmt
);
13350 stmt
= begin_do_stmt ();
13351 RECUR (DO_BODY (t
));
13352 finish_do_body (stmt
);
13353 tmp
= RECUR (DO_COND (t
));
13354 finish_do_stmt (tmp
, stmt
);
13358 stmt
= begin_if_stmt ();
13359 tmp
= RECUR (IF_COND (t
));
13360 finish_if_stmt_cond (tmp
, stmt
);
13361 RECUR (THEN_CLAUSE (t
));
13362 finish_then_clause (stmt
);
13364 if (ELSE_CLAUSE (t
))
13366 begin_else_clause (stmt
);
13367 RECUR (ELSE_CLAUSE (t
));
13368 finish_else_clause (stmt
);
13371 finish_if_stmt (stmt
);
13375 if (BIND_EXPR_BODY_BLOCK (t
))
13376 stmt
= begin_function_body ();
13378 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
13379 ? BCS_TRY_BLOCK
: 0);
13381 RECUR (BIND_EXPR_BODY (t
));
13383 if (BIND_EXPR_BODY_BLOCK (t
))
13384 finish_function_body (stmt
);
13386 finish_compound_stmt (stmt
);
13390 finish_break_stmt ();
13393 case CONTINUE_STMT
:
13394 finish_continue_stmt ();
13398 stmt
= begin_switch_stmt ();
13399 tmp
= RECUR (SWITCH_STMT_COND (t
));
13400 finish_switch_cond (tmp
, stmt
);
13401 RECUR (SWITCH_STMT_BODY (t
));
13402 finish_switch_stmt (stmt
);
13405 case CASE_LABEL_EXPR
:
13406 finish_case_label (EXPR_LOCATION (t
),
13407 RECUR (CASE_LOW (t
)),
13408 RECUR (CASE_HIGH (t
)));
13413 tree decl
= LABEL_EXPR_LABEL (t
);
13416 label
= finish_label_stmt (DECL_NAME (decl
));
13417 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
13418 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
13423 tmp
= GOTO_DESTINATION (t
);
13424 if (TREE_CODE (tmp
) != LABEL_DECL
)
13425 /* Computed goto's must be tsubst'd into. On the other hand,
13426 non-computed gotos must not be; the identifier in question
13427 will have no binding. */
13430 tmp
= DECL_NAME (tmp
);
13431 finish_goto_stmt (tmp
);
13435 tmp
= finish_asm_stmt
13436 (ASM_VOLATILE_P (t
),
13437 RECUR (ASM_STRING (t
)),
13438 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
13439 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
13440 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
13441 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
13443 tree asm_expr
= tmp
;
13444 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
13445 asm_expr
= TREE_OPERAND (asm_expr
, 0);
13446 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
13453 stmt
= begin_try_block ();
13454 RECUR (TRY_STMTS (t
));
13455 finish_cleanup_try_block (stmt
);
13456 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
13460 tree compound_stmt
= NULL_TREE
;
13462 if (FN_TRY_BLOCK_P (t
))
13463 stmt
= begin_function_try_block (&compound_stmt
);
13465 stmt
= begin_try_block ();
13467 RECUR (TRY_STMTS (t
));
13469 if (FN_TRY_BLOCK_P (t
))
13470 finish_function_try_block (stmt
);
13472 finish_try_block (stmt
);
13474 RECUR (TRY_HANDLERS (t
));
13475 if (FN_TRY_BLOCK_P (t
))
13476 finish_function_handler_sequence (stmt
, compound_stmt
);
13478 finish_handler_sequence (stmt
);
13484 tree decl
= HANDLER_PARMS (t
);
13488 decl
= tsubst (decl
, args
, complain
, in_decl
);
13489 /* Prevent instantiate_decl from trying to instantiate
13490 this variable. We've already done all that needs to be
13492 if (decl
!= error_mark_node
)
13493 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13495 stmt
= begin_handler ();
13496 finish_handler_parms (decl
, stmt
);
13497 RECUR (HANDLER_BODY (t
));
13498 finish_handler (stmt
);
13503 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
13504 if (CLASS_TYPE_P (tmp
))
13506 /* Local classes are not independent templates; they are
13507 instantiated along with their containing function. And this
13508 way we don't have to deal with pushing out of one local class
13509 to instantiate a member of another local class. */
13511 /* Closures are handled by the LAMBDA_EXPR. */
13512 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
13513 complete_type (tmp
);
13514 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
13515 if (!DECL_ARTIFICIAL (fn
))
13516 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
13520 case STATIC_ASSERT
:
13524 ++c_inhibit_evaluation_warnings
;
13526 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
13529 /*integral_constant_expression_p=*/true);
13530 --c_inhibit_evaluation_warnings
;
13532 finish_static_assert (condition
,
13533 STATIC_ASSERT_MESSAGE (t
),
13534 STATIC_ASSERT_SOURCE_LOCATION (t
),
13535 /*member_p=*/false);
13540 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), false,
13541 args
, complain
, in_decl
);
13542 stmt
= begin_omp_parallel ();
13543 RECUR (OMP_PARALLEL_BODY (t
));
13544 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
13545 = OMP_PARALLEL_COMBINED (t
);
13549 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), false,
13550 args
, complain
, in_decl
);
13551 stmt
= begin_omp_task ();
13552 RECUR (OMP_TASK_BODY (t
));
13553 finish_omp_task (tmp
, stmt
);
13558 case OMP_DISTRIBUTE
:
13560 tree clauses
, body
, pre_body
;
13561 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
13562 tree incrv
= NULL_TREE
;
13565 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), false,
13566 args
, complain
, in_decl
);
13567 if (OMP_FOR_INIT (t
) != NULL_TREE
)
13569 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13570 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13571 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13572 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13575 stmt
= begin_omp_structured_block ();
13577 pre_body
= push_stmt_list ();
13578 RECUR (OMP_FOR_PRE_BODY (t
));
13579 pre_body
= pop_stmt_list (pre_body
);
13581 if (OMP_FOR_INIT (t
) != NULL_TREE
)
13582 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
13583 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
13584 &clauses
, args
, complain
, in_decl
,
13585 integral_constant_expression_p
);
13587 body
= push_stmt_list ();
13588 RECUR (OMP_FOR_BODY (t
));
13589 body
= pop_stmt_list (body
);
13591 if (OMP_FOR_INIT (t
) != NULL_TREE
)
13592 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
, initv
,
13593 condv
, incrv
, body
, pre_body
, clauses
);
13596 t
= make_node (TREE_CODE (t
));
13597 TREE_TYPE (t
) = void_type_node
;
13598 OMP_FOR_BODY (t
) = body
;
13599 OMP_FOR_PRE_BODY (t
) = pre_body
;
13600 OMP_FOR_CLAUSES (t
) = clauses
;
13601 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
13605 add_stmt (finish_omp_structured_block (stmt
));
13612 case OMP_TARGET_DATA
:
13614 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), false,
13615 args
, complain
, in_decl
);
13616 stmt
= push_stmt_list ();
13617 RECUR (OMP_BODY (t
));
13618 stmt
= pop_stmt_list (stmt
);
13621 OMP_BODY (t
) = stmt
;
13622 OMP_CLAUSES (t
) = tmp
;
13626 case OMP_TARGET_UPDATE
:
13627 tmp
= tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t
), false,
13628 args
, complain
, in_decl
);
13630 OMP_CLAUSES (t
) = tmp
;
13637 case OMP_TASKGROUP
:
13639 stmt
= push_stmt_list ();
13640 RECUR (OMP_BODY (t
));
13641 stmt
= pop_stmt_list (stmt
);
13644 OMP_BODY (t
) = stmt
;
13649 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
13650 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
13652 tree op1
= TREE_OPERAND (t
, 1);
13653 tree rhs1
= NULL_TREE
;
13655 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
13657 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
13658 op1
= TREE_OPERAND (op1
, 1);
13660 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13661 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13662 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
13663 NULL_TREE
, NULL_TREE
, rhs1
,
13664 OMP_ATOMIC_SEQ_CST (t
));
13668 tree op1
= TREE_OPERAND (t
, 1);
13669 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
13670 tree rhs1
= NULL_TREE
;
13671 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
13672 enum tree_code opcode
= NOP_EXPR
;
13673 if (code
== OMP_ATOMIC_READ
)
13675 v
= RECUR (TREE_OPERAND (op1
, 0));
13676 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13678 else if (code
== OMP_ATOMIC_CAPTURE_OLD
13679 || code
== OMP_ATOMIC_CAPTURE_NEW
)
13681 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
13682 v
= RECUR (TREE_OPERAND (op1
, 0));
13683 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13684 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
13686 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
13687 op11
= TREE_OPERAND (op11
, 1);
13689 lhs
= RECUR (TREE_OPERAND (op11
, 0));
13690 rhs
= RECUR (TREE_OPERAND (op11
, 1));
13691 opcode
= TREE_CODE (op11
);
13692 if (opcode
== MODIFY_EXPR
)
13698 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13699 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13701 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
13702 OMP_ATOMIC_SEQ_CST (t
));
13706 case TRANSACTION_EXPR
:
13709 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
13710 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
13712 if (TRANSACTION_EXPR_IS_STMT (t
))
13714 tree body
= TRANSACTION_EXPR_BODY (t
);
13715 tree noex
= NULL_TREE
;
13716 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
13718 noex
= MUST_NOT_THROW_COND (body
);
13719 if (noex
== NULL_TREE
)
13720 noex
= boolean_true_node
;
13721 body
= TREE_OPERAND (body
, 0);
13723 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
13725 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
13729 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
13730 RECUR (TRANSACTION_EXPR_BODY (t
)),
13737 case MUST_NOT_THROW_EXPR
:
13738 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t
, 0)),
13739 RECUR (MUST_NOT_THROW_COND (t
))));
13741 case EXPR_PACK_EXPANSION
:
13742 error ("invalid use of pack expansion expression");
13743 RETURN (error_mark_node
);
13745 case NONTYPE_ARGUMENT_PACK
:
13746 error ("use %<...%> to expand argument pack");
13747 RETURN (error_mark_node
);
13749 case COMPOUND_EXPR
:
13750 tmp
= RECUR (TREE_OPERAND (t
, 0));
13751 if (tmp
== NULL_TREE
)
13752 /* If the first operand was a statement, we're done with it. */
13753 RETURN (RECUR (TREE_OPERAND (t
, 1)));
13754 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
13755 RECUR (TREE_OPERAND (t
, 1)),
13759 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
13761 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13762 /*function_p=*/false,
13763 integral_constant_expression_p
));
13766 RETURN (NULL_TREE
);
13768 input_location
= loc
;
13774 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
13775 function. For description of the body see comment above
13776 cp_parser_omp_declare_reduction_exprs. */
13779 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13781 if (t
== NULL_TREE
|| t
== error_mark_node
)
13784 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
13786 tree_stmt_iterator tsi
;
13789 memset (stmts
, 0, sizeof stmts
);
13790 for (i
= 0, tsi
= tsi_start (t
);
13791 i
< 7 && !tsi_end_p (tsi
);
13792 i
++, tsi_next (&tsi
))
13793 stmts
[i
] = tsi_stmt (tsi
);
13794 gcc_assert (tsi_end_p (tsi
));
13798 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
13799 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
13800 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
13801 args
, complain
, in_decl
);
13802 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
13803 args
, complain
, in_decl
);
13804 DECL_CONTEXT (omp_out
) = current_function_decl
;
13805 DECL_CONTEXT (omp_in
) = current_function_decl
;
13806 keep_next_level (true);
13807 tree block
= begin_omp_structured_block ();
13808 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
13809 block
= finish_omp_structured_block (block
);
13810 block
= maybe_cleanup_point_expr_void (block
);
13811 add_decl_expr (omp_out
);
13812 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
13813 TREE_NO_WARNING (omp_out
) = 1;
13814 add_decl_expr (omp_in
);
13815 finish_expr_stmt (block
);
13819 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
13820 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
13821 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
13822 args
, complain
, in_decl
);
13823 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
13824 args
, complain
, in_decl
);
13825 DECL_CONTEXT (omp_priv
) = current_function_decl
;
13826 DECL_CONTEXT (omp_orig
) = current_function_decl
;
13827 keep_next_level (true);
13828 tree block
= begin_omp_structured_block ();
13829 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
13830 block
= finish_omp_structured_block (block
);
13831 block
= maybe_cleanup_point_expr_void (block
);
13832 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
13833 add_decl_expr (omp_priv
);
13834 add_decl_expr (omp_orig
);
13835 finish_expr_stmt (block
);
13837 add_decl_expr (omp_orig
);
13841 /* T is a postfix-expression that is not being used in a function
13842 call. Return the substituted version of T. */
13845 tsubst_non_call_postfix_expression (tree t
, tree args
,
13846 tsubst_flags_t complain
,
13849 if (TREE_CODE (t
) == SCOPE_REF
)
13850 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
13851 /*done=*/false, /*address_p=*/false);
13853 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13854 /*function_p=*/false,
13855 /*integral_constant_expression_p=*/false);
13860 /* Like tsubst but deals with expressions and performs semantic
13861 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13864 tsubst_copy_and_build (tree t
,
13866 tsubst_flags_t complain
,
13869 bool integral_constant_expression_p
)
13871 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13872 #define RECUR(NODE) \
13873 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13874 /*function_p=*/false, \
13875 integral_constant_expression_p)
13880 if (t
== NULL_TREE
|| t
== error_mark_node
)
13883 loc
= input_location
;
13884 if (EXPR_HAS_LOCATION (t
))
13885 input_location
= EXPR_LOCATION (t
);
13887 /* N3276 decltype magic only applies to calls at the top level or on the
13888 right side of a comma. */
13889 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
13890 complain
&= ~tf_decltype
;
13892 switch (TREE_CODE (t
))
13896 /* Fall through. */
13897 case IDENTIFIER_NODE
:
13901 bool non_integral_constant_expression_p
;
13902 const char *error_msg
;
13904 if (IDENTIFIER_TYPENAME_P (t
))
13906 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13907 t
= mangle_conv_op_name_for_type (new_type
);
13910 /* Look up the name. */
13911 decl
= lookup_name (t
);
13913 /* By convention, expressions use ERROR_MARK_NODE to indicate
13914 failure, not NULL_TREE. */
13915 if (decl
== NULL_TREE
)
13916 decl
= error_mark_node
;
13918 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
13920 integral_constant_expression_p
,
13921 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
13922 &non_integral_constant_expression_p
,
13923 /*template_p=*/false,
13925 /*address_p=*/false,
13926 /*template_arg_p=*/false,
13931 if (!function_p
&& identifier_p (decl
))
13933 if (complain
& tf_error
)
13934 unqualified_name_lookup_error (decl
);
13935 decl
= error_mark_node
;
13940 case TEMPLATE_ID_EXPR
:
13943 tree templ
= RECUR (TREE_OPERAND (t
, 0));
13944 tree targs
= TREE_OPERAND (t
, 1);
13947 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
13949 if (TREE_CODE (templ
) == COMPONENT_REF
)
13951 object
= TREE_OPERAND (templ
, 0);
13952 templ
= TREE_OPERAND (templ
, 1);
13955 object
= NULL_TREE
;
13956 templ
= lookup_template_function (templ
, targs
);
13959 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
13960 object
, templ
, NULL_TREE
));
13962 RETURN (baselink_for_fns (templ
));
13967 tree r
= RECUR (TREE_OPERAND (t
, 0));
13969 if (REFERENCE_REF_P (t
))
13971 /* A type conversion to reference type will be enclosed in
13972 such an indirect ref, but the substitution of the cast
13973 will have also added such an indirect ref. */
13974 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
13975 r
= convert_from_reference (r
);
13978 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
13979 complain
|decltype_flag
);
13985 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13986 RECUR (TREE_OPERAND (t
, 0))));
13988 case IMPLICIT_CONV_EXPR
:
13990 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13991 tree expr
= RECUR (TREE_OPERAND (t
, 0));
13992 int flags
= LOOKUP_IMPLICIT
;
13993 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
13994 flags
= LOOKUP_NORMAL
;
13995 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
14002 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
14003 RECUR (TREE_OPERAND (t
, 0))));
14006 case REINTERPRET_CAST_EXPR
:
14007 case CONST_CAST_EXPR
:
14008 case DYNAMIC_CAST_EXPR
:
14009 case STATIC_CAST_EXPR
:
14012 tree op
, r
= NULL_TREE
;
14014 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14015 if (integral_constant_expression_p
14016 && !cast_valid_in_integral_constant_expression_p (type
))
14018 if (complain
& tf_error
)
14019 error ("a cast to a type other than an integral or "
14020 "enumeration type cannot appear in a constant-expression");
14021 RETURN (error_mark_node
);
14024 op
= RECUR (TREE_OPERAND (t
, 0));
14026 ++c_inhibit_evaluation_warnings
;
14027 switch (TREE_CODE (t
))
14030 r
= build_functional_cast (type
, op
, complain
);
14032 case REINTERPRET_CAST_EXPR
:
14033 r
= build_reinterpret_cast (type
, op
, complain
);
14035 case CONST_CAST_EXPR
:
14036 r
= build_const_cast (type
, op
, complain
);
14038 case DYNAMIC_CAST_EXPR
:
14039 r
= build_dynamic_cast (type
, op
, complain
);
14041 case STATIC_CAST_EXPR
:
14042 r
= build_static_cast (type
, op
, complain
);
14045 gcc_unreachable ();
14047 --c_inhibit_evaluation_warnings
;
14052 case POSTDECREMENT_EXPR
:
14053 case POSTINCREMENT_EXPR
:
14054 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14055 args
, complain
, in_decl
);
14056 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
14057 complain
|decltype_flag
));
14059 case PREDECREMENT_EXPR
:
14060 case PREINCREMENT_EXPR
:
14064 case TRUTH_NOT_EXPR
:
14065 case UNARY_PLUS_EXPR
: /* Unary + */
14066 case REALPART_EXPR
:
14067 case IMAGPART_EXPR
:
14068 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
14069 RECUR (TREE_OPERAND (t
, 0)),
14070 complain
|decltype_flag
));
14072 case FIX_TRUNC_EXPR
:
14073 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
14077 op1
= TREE_OPERAND (t
, 0);
14078 if (TREE_CODE (op1
) == LABEL_DECL
)
14079 RETURN (finish_label_address_expr (DECL_NAME (op1
),
14080 EXPR_LOCATION (op1
)));
14081 if (TREE_CODE (op1
) == SCOPE_REF
)
14082 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
14083 /*done=*/true, /*address_p=*/true);
14085 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
14087 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
14088 complain
|decltype_flag
));
14093 case TRUNC_DIV_EXPR
:
14094 case CEIL_DIV_EXPR
:
14095 case FLOOR_DIV_EXPR
:
14096 case ROUND_DIV_EXPR
:
14097 case EXACT_DIV_EXPR
:
14101 case TRUNC_MOD_EXPR
:
14102 case FLOOR_MOD_EXPR
:
14103 case TRUTH_ANDIF_EXPR
:
14104 case TRUTH_ORIF_EXPR
:
14105 case TRUTH_AND_EXPR
:
14106 case TRUTH_OR_EXPR
:
14124 ++c_inhibit_evaluation_warnings
;
14126 r
= build_x_binary_op
14127 (input_location
, TREE_CODE (t
),
14128 RECUR (TREE_OPERAND (t
, 0)),
14129 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
14131 : TREE_CODE (TREE_OPERAND (t
, 0))),
14132 RECUR (TREE_OPERAND (t
, 1)),
14133 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
14135 : TREE_CODE (TREE_OPERAND (t
, 1))),
14137 complain
|decltype_flag
);
14138 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
14139 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14141 --c_inhibit_evaluation_warnings
;
14147 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
14148 /*address_p=*/false));
14150 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14151 args
, complain
, in_decl
);
14152 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
14153 RECUR (TREE_OPERAND (t
, 1)),
14154 complain
|decltype_flag
));
14156 case ARRAY_NOTATION_REF
:
14158 tree start_index
, length
, stride
;
14159 op1
= tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t
),
14160 args
, complain
, in_decl
);
14161 start_index
= RECUR (ARRAY_NOTATION_START (t
));
14162 length
= RECUR (ARRAY_NOTATION_LENGTH (t
));
14163 stride
= RECUR (ARRAY_NOTATION_STRIDE (t
));
14164 RETURN (build_array_notation_ref (EXPR_LOCATION (t
), op1
, start_index
,
14165 length
, stride
, TREE_TYPE (op1
)));
14168 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
14169 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
14176 op1
= TREE_OPERAND (t
, 0);
14177 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
14178 op1
= TREE_TYPE (op1
);
14181 /* When there are no ARGS, we are trying to evaluate a
14182 non-dependent expression from the parser. Trying to do
14183 the substitutions may not work. */
14185 op1
= TREE_TYPE (op1
);
14189 ++cp_unevaluated_operand
;
14190 ++c_inhibit_evaluation_warnings
;
14192 op1
= tsubst (op1
, args
, complain
, in_decl
);
14194 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14195 /*function_p=*/false,
14196 /*integral_constant_expression_p=*/
14198 --cp_unevaluated_operand
;
14199 --c_inhibit_evaluation_warnings
;
14202 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
14203 complain
& tf_error
);
14205 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
14206 complain
& tf_error
);
14207 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
14209 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
14211 if (!processing_template_decl
&& TYPE_P (op1
))
14213 r
= build_min (SIZEOF_EXPR
, size_type_node
,
14214 build1 (NOP_EXPR
, op1
, error_mark_node
));
14215 SIZEOF_EXPR_TYPE_P (r
) = 1;
14218 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
14219 TREE_SIDE_EFFECTS (r
) = 0;
14220 TREE_READONLY (r
) = 1;
14222 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
14227 case AT_ENCODE_EXPR
:
14229 op1
= TREE_OPERAND (t
, 0);
14230 ++cp_unevaluated_operand
;
14231 ++c_inhibit_evaluation_warnings
;
14232 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14233 /*function_p=*/false,
14234 /*integral_constant_expression_p=*/false);
14235 --cp_unevaluated_operand
;
14236 --c_inhibit_evaluation_warnings
;
14237 RETURN (objc_build_encode_expr (op1
));
14240 case NOEXCEPT_EXPR
:
14241 op1
= TREE_OPERAND (t
, 0);
14242 ++cp_unevaluated_operand
;
14243 ++c_inhibit_evaluation_warnings
;
14244 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14245 /*function_p=*/false,
14246 /*integral_constant_expression_p=*/false);
14247 --cp_unevaluated_operand
;
14248 --c_inhibit_evaluation_warnings
;
14249 RETURN (finish_noexcept_expr (op1
, complain
));
14255 ++c_inhibit_evaluation_warnings
;
14257 r
= build_x_modify_expr
14258 (EXPR_LOCATION (t
),
14259 RECUR (TREE_OPERAND (t
, 0)),
14260 TREE_CODE (TREE_OPERAND (t
, 1)),
14261 RECUR (TREE_OPERAND (t
, 2)),
14262 complain
|decltype_flag
);
14263 /* TREE_NO_WARNING must be set if either the expression was
14264 parenthesized or it uses an operator such as >>= rather
14265 than plain assignment. In the former case, it was already
14266 set and must be copied. In the latter case,
14267 build_x_modify_expr sets it and it must not be reset
14269 if (TREE_NO_WARNING (t
))
14270 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14272 --c_inhibit_evaluation_warnings
;
14278 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14279 args
, complain
, in_decl
);
14280 /* Remember that there was a reference to this entity. */
14283 RETURN (build_x_arrow (input_location
, op1
, complain
));
14287 tree placement
= RECUR (TREE_OPERAND (t
, 0));
14288 tree init
= RECUR (TREE_OPERAND (t
, 3));
14289 vec
<tree
, va_gc
> *placement_vec
;
14290 vec
<tree
, va_gc
> *init_vec
;
14293 if (placement
== NULL_TREE
)
14294 placement_vec
= NULL
;
14297 placement_vec
= make_tree_vector ();
14298 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
14299 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
14302 /* If there was an initializer in the original tree, but it
14303 instantiated to an empty list, then we should pass a
14304 non-NULL empty vector to tell build_new that it was an
14305 empty initializer() rather than no initializer. This can
14306 only happen when the initializer is a pack expansion whose
14307 parameter packs are of length zero. */
14308 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
14312 init_vec
= make_tree_vector ();
14313 if (init
== void_zero_node
)
14314 gcc_assert (init_vec
!= NULL
);
14317 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
14318 vec_safe_push (init_vec
, TREE_VALUE (init
));
14322 ret
= build_new (&placement_vec
,
14323 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
14324 RECUR (TREE_OPERAND (t
, 2)),
14326 NEW_EXPR_USE_GLOBAL (t
),
14329 if (placement_vec
!= NULL
)
14330 release_tree_vector (placement_vec
);
14331 if (init_vec
!= NULL
)
14332 release_tree_vector (init_vec
);
14338 RETURN (delete_sanity
14339 (RECUR (TREE_OPERAND (t
, 0)),
14340 RECUR (TREE_OPERAND (t
, 1)),
14341 DELETE_EXPR_USE_VEC (t
),
14342 DELETE_EXPR_USE_GLOBAL (t
),
14345 case COMPOUND_EXPR
:
14347 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
14348 complain
& ~tf_decltype
, in_decl
,
14349 /*function_p=*/false,
14350 integral_constant_expression_p
);
14351 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
14353 RECUR (TREE_OPERAND (t
, 1)),
14354 complain
|decltype_flag
));
14360 vec
<tree
, va_gc
> *call_args
;
14361 unsigned int nargs
, i
;
14366 function
= CALL_EXPR_FN (t
);
14367 /* When we parsed the expression, we determined whether or
14368 not Koenig lookup should be performed. */
14369 koenig_p
= KOENIG_LOOKUP_P (t
);
14370 if (TREE_CODE (function
) == SCOPE_REF
)
14372 qualified_p
= true;
14373 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
14375 /*address_p=*/false);
14377 else if (koenig_p
&& identifier_p (function
))
14379 /* Do nothing; calling tsubst_copy_and_build on an identifier
14380 would incorrectly perform unqualified lookup again.
14382 Note that we can also have an IDENTIFIER_NODE if the earlier
14383 unqualified lookup found a member function; in that case
14384 koenig_p will be false and we do want to do the lookup
14385 again to find the instantiated member function.
14387 FIXME but doing that causes c++/15272, so we need to stop
14388 using IDENTIFIER_NODE in that situation. */
14389 qualified_p
= false;
14393 if (TREE_CODE (function
) == COMPONENT_REF
)
14395 tree op
= TREE_OPERAND (function
, 1);
14397 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
14398 || (BASELINK_P (op
)
14399 && BASELINK_QUALIFIED_P (op
)));
14402 qualified_p
= false;
14404 if (TREE_CODE (function
) == ADDR_EXPR
14405 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
14406 /* Avoid error about taking the address of a constructor. */
14407 function
= TREE_OPERAND (function
, 0);
14409 function
= tsubst_copy_and_build (function
, args
, complain
,
14412 integral_constant_expression_p
);
14414 if (BASELINK_P (function
))
14415 qualified_p
= true;
14418 nargs
= call_expr_nargs (t
);
14419 call_args
= make_tree_vector ();
14420 for (i
= 0; i
< nargs
; ++i
)
14422 tree arg
= CALL_EXPR_ARG (t
, i
);
14424 if (!PACK_EXPANSION_P (arg
))
14425 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
14428 /* Expand the pack expansion and push each entry onto
14430 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
14431 if (TREE_CODE (arg
) == TREE_VEC
)
14433 unsigned int len
, j
;
14435 len
= TREE_VEC_LENGTH (arg
);
14436 for (j
= 0; j
< len
; ++j
)
14438 tree value
= TREE_VEC_ELT (arg
, j
);
14439 if (value
!= NULL_TREE
)
14440 value
= convert_from_reference (value
);
14441 vec_safe_push (call_args
, value
);
14446 /* A partial substitution. Add one entry. */
14447 vec_safe_push (call_args
, arg
);
14452 /* We do not perform argument-dependent lookup if normal
14453 lookup finds a non-function, in accordance with the
14454 expected resolution of DR 218. */
14456 && ((is_overloaded_fn (function
)
14457 /* If lookup found a member function, the Koenig lookup is
14458 not appropriate, even if an unqualified-name was used
14459 to denote the function. */
14460 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
14461 || identifier_p (function
))
14462 /* Only do this when substitution turns a dependent call
14463 into a non-dependent call. */
14464 && type_dependent_expression_p_push (t
)
14465 && !any_type_dependent_arguments_p (call_args
))
14466 function
= perform_koenig_lookup (function
, call_args
, false,
14469 if (identifier_p (function
)
14470 && !any_type_dependent_arguments_p (call_args
))
14472 if (koenig_p
&& (complain
& tf_warning_or_error
))
14474 /* For backwards compatibility and good diagnostics, try
14475 the unqualified lookup again if we aren't in SFINAE
14477 tree unq
= (tsubst_copy_and_build
14478 (function
, args
, complain
, in_decl
, true,
14479 integral_constant_expression_p
));
14480 if (unq
== error_mark_node
)
14481 RETURN (error_mark_node
);
14483 if (unq
!= function
)
14486 if (INDIRECT_REF_P (fn
))
14487 fn
= TREE_OPERAND (fn
, 0);
14488 if (TREE_CODE (fn
) == COMPONENT_REF
)
14489 fn
= TREE_OPERAND (fn
, 1);
14490 if (is_overloaded_fn (fn
))
14491 fn
= get_first_fn (fn
);
14492 if (permerror (EXPR_LOC_OR_HERE (t
),
14493 "%qD was not declared in this scope, "
14494 "and no declarations were found by "
14495 "argument-dependent lookup at the point "
14496 "of instantiation", function
))
14499 /* Can't say anything more. */;
14500 else if (DECL_CLASS_SCOPE_P (fn
))
14502 inform (EXPR_LOC_OR_HERE (t
),
14503 "declarations in dependent base %qT are "
14504 "not found by unqualified lookup",
14505 DECL_CLASS_CONTEXT (fn
));
14506 if (current_class_ptr
)
14507 inform (EXPR_LOC_OR_HERE (t
),
14508 "use %<this->%D%> instead", function
);
14510 inform (EXPR_LOC_OR_HERE (t
),
14511 "use %<%T::%D%> instead",
14512 current_class_name
, function
);
14515 inform (0, "%q+D declared here, later in the "
14516 "translation unit", fn
);
14521 if (identifier_p (function
))
14523 if (complain
& tf_error
)
14524 unqualified_name_lookup_error (function
);
14525 release_tree_vector (call_args
);
14526 RETURN (error_mark_node
);
14530 /* Remember that there was a reference to this entity. */
14531 if (DECL_P (function
))
14532 mark_used (function
);
14534 /* Put back tf_decltype for the actual call. */
14535 complain
|= decltype_flag
;
14537 if (TREE_CODE (function
) == OFFSET_REF
)
14538 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
14540 else if (TREE_CODE (function
) == COMPONENT_REF
)
14542 tree instance
= TREE_OPERAND (function
, 0);
14543 tree fn
= TREE_OPERAND (function
, 1);
14545 if (processing_template_decl
14546 && (type_dependent_expression_p (instance
)
14547 || (!BASELINK_P (fn
)
14548 && TREE_CODE (fn
) != FIELD_DECL
)
14549 || type_dependent_expression_p (fn
)
14550 || any_type_dependent_arguments_p (call_args
)))
14551 ret
= build_nt_call_vec (function
, call_args
);
14552 else if (!BASELINK_P (fn
))
14553 ret
= finish_call_expr (function
, &call_args
,
14554 /*disallow_virtual=*/false,
14555 /*koenig_p=*/false,
14558 ret
= (build_new_method_call
14560 &call_args
, NULL_TREE
,
14561 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
14566 ret
= finish_call_expr (function
, &call_args
,
14567 /*disallow_virtual=*/qualified_p
,
14571 release_tree_vector (call_args
);
14578 tree cond
= RECUR (TREE_OPERAND (t
, 0));
14581 if (TREE_CODE (cond
) == INTEGER_CST
)
14583 if (integer_zerop (cond
))
14585 ++c_inhibit_evaluation_warnings
;
14586 exp1
= RECUR (TREE_OPERAND (t
, 1));
14587 --c_inhibit_evaluation_warnings
;
14588 exp2
= RECUR (TREE_OPERAND (t
, 2));
14592 exp1
= RECUR (TREE_OPERAND (t
, 1));
14593 ++c_inhibit_evaluation_warnings
;
14594 exp2
= RECUR (TREE_OPERAND (t
, 2));
14595 --c_inhibit_evaluation_warnings
;
14600 exp1
= RECUR (TREE_OPERAND (t
, 1));
14601 exp2
= RECUR (TREE_OPERAND (t
, 2));
14604 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
14605 cond
, exp1
, exp2
, complain
));
14608 case PSEUDO_DTOR_EXPR
:
14609 RETURN (finish_pseudo_destructor_expr
14610 (RECUR (TREE_OPERAND (t
, 0)),
14611 RECUR (TREE_OPERAND (t
, 1)),
14612 tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
),
14617 tree purpose
, value
, chain
;
14619 if (t
== void_list_node
)
14622 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
14623 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
14625 /* We have pack expansions, so expand those and
14626 create a new list out of it. */
14627 tree purposevec
= NULL_TREE
;
14628 tree valuevec
= NULL_TREE
;
14632 /* Expand the argument expressions. */
14633 if (TREE_PURPOSE (t
))
14634 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
14635 complain
, in_decl
);
14636 if (TREE_VALUE (t
))
14637 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
14638 complain
, in_decl
);
14640 /* Build the rest of the list. */
14641 chain
= TREE_CHAIN (t
);
14642 if (chain
&& chain
!= void_type_node
)
14643 chain
= RECUR (chain
);
14645 /* Determine the number of arguments. */
14646 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
14648 len
= TREE_VEC_LENGTH (purposevec
);
14649 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
14651 else if (TREE_CODE (valuevec
) == TREE_VEC
)
14652 len
= TREE_VEC_LENGTH (valuevec
);
14655 /* Since we only performed a partial substitution into
14656 the argument pack, we only RETURN (a single list
14658 if (purposevec
== TREE_PURPOSE (t
)
14659 && valuevec
== TREE_VALUE (t
)
14660 && chain
== TREE_CHAIN (t
))
14663 RETURN (tree_cons (purposevec
, valuevec
, chain
));
14666 /* Convert the argument vectors into a TREE_LIST */
14670 /* Grab the Ith values. */
14672 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
14675 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
14678 /* Build the list (backwards). */
14679 chain
= tree_cons (purpose
, value
, chain
);
14685 purpose
= TREE_PURPOSE (t
);
14687 purpose
= RECUR (purpose
);
14688 value
= TREE_VALUE (t
);
14690 value
= RECUR (value
);
14691 chain
= TREE_CHAIN (t
);
14692 if (chain
&& chain
!= void_type_node
)
14693 chain
= RECUR (chain
);
14694 if (purpose
== TREE_PURPOSE (t
)
14695 && value
== TREE_VALUE (t
)
14696 && chain
== TREE_CHAIN (t
))
14698 RETURN (tree_cons (purpose
, value
, chain
));
14701 case COMPONENT_REF
:
14707 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14708 args
, complain
, in_decl
);
14709 /* Remember that there was a reference to this entity. */
14710 if (DECL_P (object
))
14711 mark_used (object
);
14712 object_type
= TREE_TYPE (object
);
14714 member
= TREE_OPERAND (t
, 1);
14715 if (BASELINK_P (member
))
14716 member
= tsubst_baselink (member
,
14717 non_reference (TREE_TYPE (object
)),
14718 args
, complain
, in_decl
);
14720 member
= tsubst_copy (member
, args
, complain
, in_decl
);
14721 if (member
== error_mark_node
)
14722 RETURN (error_mark_node
);
14724 if (type_dependent_expression_p (object
))
14725 /* We can't do much here. */;
14726 else if (!CLASS_TYPE_P (object_type
))
14728 if (scalarish_type_p (object_type
))
14730 tree s
= NULL_TREE
;
14731 tree dtor
= member
;
14733 if (TREE_CODE (dtor
) == SCOPE_REF
)
14735 s
= TREE_OPERAND (dtor
, 0);
14736 dtor
= TREE_OPERAND (dtor
, 1);
14738 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
14740 dtor
= TREE_OPERAND (dtor
, 0);
14742 RETURN (finish_pseudo_destructor_expr
14743 (object
, s
, dtor
, input_location
));
14747 else if (TREE_CODE (member
) == SCOPE_REF
14748 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
14750 /* Lookup the template functions now that we know what the
14752 tree scope
= TREE_OPERAND (member
, 0);
14753 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
14754 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
14755 member
= lookup_qualified_name (scope
, tmpl
,
14756 /*is_type_p=*/false,
14757 /*complain=*/false);
14758 if (BASELINK_P (member
))
14760 BASELINK_FUNCTIONS (member
)
14761 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
14763 member
= (adjust_result_of_qualified_name_lookup
14764 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
14769 qualified_name_lookup_error (scope
, tmpl
, member
,
14771 RETURN (error_mark_node
);
14774 else if (TREE_CODE (member
) == SCOPE_REF
14775 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
14776 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
14778 if (complain
& tf_error
)
14780 if (TYPE_P (TREE_OPERAND (member
, 0)))
14781 error ("%qT is not a class or namespace",
14782 TREE_OPERAND (member
, 0));
14784 error ("%qD is not a class or namespace",
14785 TREE_OPERAND (member
, 0));
14787 RETURN (error_mark_node
);
14789 else if (TREE_CODE (member
) == FIELD_DECL
)
14790 RETURN (finish_non_static_data_member (member
, object
, NULL_TREE
));
14792 RETURN (finish_class_member_access_expr (object
, member
,
14793 /*template_p=*/false,
14798 RETURN (build_throw
14799 (RECUR (TREE_OPERAND (t
, 0))));
14803 vec
<constructor_elt
, va_gc
> *n
;
14804 constructor_elt
*ce
;
14805 unsigned HOST_WIDE_INT idx
;
14806 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14807 bool process_index_p
;
14809 bool need_copy_p
= false;
14812 if (type
== error_mark_node
)
14813 RETURN (error_mark_node
);
14815 /* digest_init will do the wrong thing if we let it. */
14816 if (type
&& TYPE_PTRMEMFUNC_P (type
))
14819 /* We do not want to process the index of aggregate
14820 initializers as they are identifier nodes which will be
14821 looked up by digest_init. */
14822 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
14824 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
14825 newlen
= vec_safe_length (n
);
14826 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
14828 if (ce
->index
&& process_index_p
14829 /* An identifier index is looked up in the type
14830 being initialized, not the current scope. */
14831 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
14832 ce
->index
= RECUR (ce
->index
);
14834 if (PACK_EXPANSION_P (ce
->value
))
14836 /* Substitute into the pack expansion. */
14837 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
14840 if (ce
->value
== error_mark_node
14841 || PACK_EXPANSION_P (ce
->value
))
14843 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
14844 /* Just move the argument into place. */
14845 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
14848 /* Update the length of the final CONSTRUCTOR
14849 arguments vector, and note that we will need to
14851 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
14852 need_copy_p
= true;
14856 ce
->value
= RECUR (ce
->value
);
14861 vec
<constructor_elt
, va_gc
> *old_n
= n
;
14863 vec_alloc (n
, newlen
);
14864 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
14866 if (TREE_CODE (ce
->value
) == TREE_VEC
)
14868 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
14869 for (i
= 0; i
< len
; ++i
)
14870 CONSTRUCTOR_APPEND_ELT (n
, 0,
14871 TREE_VEC_ELT (ce
->value
, i
));
14874 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
14878 r
= build_constructor (init_list_type_node
, n
);
14879 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
14881 if (TREE_HAS_CONSTRUCTOR (t
))
14882 RETURN (finish_compound_literal (type
, r
, complain
));
14884 TREE_TYPE (r
) = type
;
14890 tree operand_0
= TREE_OPERAND (t
, 0);
14891 if (TYPE_P (operand_0
))
14893 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
14894 RETURN (get_typeid (operand_0
, complain
));
14898 operand_0
= RECUR (operand_0
);
14899 RETURN (build_typeid (operand_0
, complain
));
14906 else if (DECL_PACK_P (t
))
14908 /* We don't build decls for an instantiation of a
14909 variadic capture proxy, we instantiate the elements
14911 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
14912 return RECUR (DECL_VALUE_EXPR (t
));
14918 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
14920 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
14921 /* If the original type was a reference, we'll be wrapped in
14922 the appropriate INDIRECT_REF. */
14923 r
= convert_from_reference (r
);
14928 RETURN (build_x_va_arg (EXPR_LOCATION (t
),
14929 RECUR (TREE_OPERAND (t
, 0)),
14930 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
)));
14932 case OFFSETOF_EXPR
:
14933 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t
, 0))));
14937 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
14938 complain
, in_decl
);
14940 tree type2
= TRAIT_EXPR_TYPE2 (t
);
14942 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
14944 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
14949 tree old_stmt_expr
= cur_stmt_expr
;
14950 tree stmt_expr
= begin_stmt_expr ();
14952 cur_stmt_expr
= stmt_expr
;
14953 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
14954 integral_constant_expression_p
);
14955 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
14956 cur_stmt_expr
= old_stmt_expr
;
14958 /* If the resulting list of expression statement is empty,
14959 fold it further into void_zero_node. */
14960 if (empty_expr_stmt_p (stmt_expr
))
14961 stmt_expr
= void_zero_node
;
14963 RETURN (stmt_expr
);
14968 tree r
= build_lambda_expr ();
14970 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
14971 LAMBDA_EXPR_CLOSURE (r
) = type
;
14972 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
14974 LAMBDA_EXPR_LOCATION (r
)
14975 = LAMBDA_EXPR_LOCATION (t
);
14976 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
14977 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
14978 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
14979 LAMBDA_EXPR_DISCRIMINATOR (r
)
14980 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
14981 /* For a function scope, we want to use tsubst so that we don't
14982 complain about referring to an auto function before its return
14983 type has been deduced. Otherwise, we want to use tsubst_copy so
14984 that we look up the existing field/parameter/variable rather
14985 than build a new one. */
14986 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
14987 if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
14988 scope
= tsubst (scope
, args
, complain
, in_decl
);
14989 else if (scope
&& TREE_CODE (scope
) == PARM_DECL
)
14991 /* Look up the parameter we want directly, as tsubst_copy
14992 doesn't do what we need. */
14993 tree fn
= tsubst (DECL_CONTEXT (scope
), args
, complain
, in_decl
);
14994 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
14995 while (DECL_PARM_INDEX (parm
) != DECL_PARM_INDEX (scope
))
14996 parm
= DECL_CHAIN (parm
);
14998 /* FIXME Work around the parm not having DECL_CONTEXT set. */
14999 if (DECL_CONTEXT (scope
) == NULL_TREE
)
15000 DECL_CONTEXT (scope
) = fn
;
15003 scope
= RECUR (scope
);
15004 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
15005 LAMBDA_EXPR_RETURN_TYPE (r
)
15006 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
15008 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
15009 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
15011 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15012 determine_visibility (TYPE_NAME (type
));
15013 /* Now that we know visibility, instantiate the type so we have a
15014 declaration of the op() for later calls to lambda_function. */
15015 complete_type (type
);
15017 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
15019 RETURN (build_lambda_object (r
));
15023 /* We can get here for a constant initializer of non-dependent type.
15024 FIXME stop folding in cp_parser_initializer_clause. */
15026 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
15031 case TRANSACTION_EXPR
:
15032 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
15033 integral_constant_expression_p
));
15036 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
15038 case VEC_PERM_EXPR
:
15039 RETURN (build_x_vec_perm_expr (input_location
,
15040 RECUR (TREE_OPERAND (t
, 0)),
15041 RECUR (TREE_OPERAND (t
, 1)),
15042 RECUR (TREE_OPERAND (t
, 2)),
15046 /* Handle Objective-C++ constructs, if appropriate. */
15049 = objcp_tsubst_copy_and_build (t
, args
, complain
,
15050 in_decl
, /*function_p=*/false);
15054 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
15060 input_location
= loc
;
15064 /* Verify that the instantiated ARGS are valid. For type arguments,
15065 make sure that the type's linkage is ok. For non-type arguments,
15066 make sure they are constants if they are integral or enumerations.
15067 Emit an error under control of COMPLAIN, and return TRUE on error. */
15070 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
15072 if (dependent_template_arg_p (t
))
15074 if (ARGUMENT_PACK_P (t
))
15076 tree vec
= ARGUMENT_PACK_ARGS (t
);
15077 int len
= TREE_VEC_LENGTH (vec
);
15078 bool result
= false;
15081 for (i
= 0; i
< len
; ++i
)
15082 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
15086 else if (TYPE_P (t
))
15088 /* [basic.link]: A name with no linkage (notably, the name
15089 of a class or enumeration declared in a local scope)
15090 shall not be used to declare an entity with linkage.
15091 This implies that names with no linkage cannot be used as
15094 DR 757 relaxes this restriction for C++0x. */
15095 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
15096 : no_linkage_check (t
, /*relaxed_p=*/false));
15100 /* DR 488 makes use of a type with no linkage cause
15101 type deduction to fail. */
15102 if (complain
& tf_error
)
15104 if (TYPE_ANONYMOUS_P (nt
))
15105 error ("%qT is/uses anonymous type", t
);
15107 error ("template argument for %qD uses local type %qT",
15112 /* In order to avoid all sorts of complications, we do not
15113 allow variably-modified types as template arguments. */
15114 else if (variably_modified_type_p (t
, NULL_TREE
))
15116 if (complain
& tf_error
)
15117 error ("%qT is a variably modified type", t
);
15121 /* Class template and alias template arguments should be OK. */
15122 else if (DECL_TYPE_TEMPLATE_P (t
))
15124 /* A non-type argument of integral or enumerated type must be a
15126 else if (TREE_TYPE (t
)
15127 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
15128 && !TREE_CONSTANT (t
))
15130 if (complain
& tf_error
)
15131 error ("integral expression %qE is not constant", t
);
15138 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
15140 int ix
, len
= DECL_NTPARMS (tmpl
);
15141 bool result
= false;
15143 for (ix
= 0; ix
!= len
; ix
++)
15145 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
15148 if (result
&& (complain
& tf_error
))
15149 error (" trying to instantiate %qD", tmpl
);
15153 /* We're out of SFINAE context now, so generate diagnostics for the access
15154 errors we saw earlier when instantiating D from TMPL and ARGS. */
15157 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
15159 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
15160 tree type
= TREE_TYPE (pattern
);
15161 location_t loc
= input_location
;
15163 push_access_scope (d
);
15164 push_deferring_access_checks (dk_no_deferred
);
15165 input_location
= DECL_SOURCE_LOCATION (pattern
);
15166 tsubst (type
, args
, tf_warning_or_error
, d
);
15167 input_location
= loc
;
15168 pop_deferring_access_checks ();
15169 pop_access_scope (d
);
15172 /* Instantiate the indicated variable, function, or alias template TMPL with
15173 the template arguments in TARG_PTR. */
15176 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
15178 tree targ_ptr
= orig_args
;
15182 bool access_ok
= true;
15184 if (tmpl
== error_mark_node
)
15185 return error_mark_node
;
15187 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
15189 /* If this function is a clone, handle it specially. */
15190 if (DECL_CLONED_FUNCTION_P (tmpl
))
15195 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15196 DECL_CLONED_FUNCTION. */
15197 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
15198 targ_ptr
, complain
);
15199 if (spec
== error_mark_node
)
15200 return error_mark_node
;
15202 /* Look for the clone. */
15203 FOR_EACH_CLONE (clone
, spec
)
15204 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
15206 /* We should always have found the clone by now. */
15207 gcc_unreachable ();
15211 /* Check to see if we already have this specialization. */
15212 gen_tmpl
= most_general_template (tmpl
);
15213 if (tmpl
!= gen_tmpl
)
15214 /* The TMPL is a partial instantiation. To get a full set of
15215 arguments we must add the arguments used to perform the
15216 partial instantiation. */
15217 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
15220 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15221 but it doesn't seem to be on the hot path. */
15222 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
15224 gcc_assert (tmpl
== gen_tmpl
15225 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
15227 || fndecl
== NULL_TREE
);
15229 if (spec
!= NULL_TREE
)
15231 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
15233 if (complain
& tf_error
)
15234 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
15235 return error_mark_node
;
15240 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
15242 return error_mark_node
;
15244 /* We are building a FUNCTION_DECL, during which the access of its
15245 parameters and return types have to be checked. However this
15246 FUNCTION_DECL which is the desired context for access checking
15247 is not built yet. We solve this chicken-and-egg problem by
15248 deferring all checks until we have the FUNCTION_DECL. */
15249 push_deferring_access_checks (dk_deferred
);
15251 /* Instantiation of the function happens in the context of the function
15252 template, not the context of the overload resolution we're doing. */
15253 push_to_top_level ();
15254 /* If there are dependent arguments, e.g. because we're doing partial
15255 ordering, make sure processing_template_decl stays set. */
15256 if (uses_template_parms (targ_ptr
))
15257 ++processing_template_decl
;
15258 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
15260 tree ctx
= tsubst (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
15261 complain
, gen_tmpl
);
15262 push_nested_class (ctx
);
15264 /* Substitute template parameters to obtain the specialization. */
15265 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
15266 targ_ptr
, complain
, gen_tmpl
);
15267 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
15268 pop_nested_class ();
15269 pop_from_top_level ();
15271 if (fndecl
== error_mark_node
)
15273 pop_deferring_access_checks ();
15274 return error_mark_node
;
15277 /* The DECL_TI_TEMPLATE should always be the immediate parent
15278 template, not the most general template. */
15279 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
15281 /* Now we know the specialization, compute access previously
15283 push_access_scope (fndecl
);
15284 if (!perform_deferred_access_checks (complain
))
15286 pop_access_scope (fndecl
);
15287 pop_deferring_access_checks ();
15289 /* If we've just instantiated the main entry point for a function,
15290 instantiate all the alternate entry points as well. We do this
15291 by cloning the instantiation of the main entry point, not by
15292 instantiating the template clones. */
15293 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
15294 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
15298 if (!(complain
& tf_error
))
15300 /* Remember to reinstantiate when we're out of SFINAE so the user
15301 can see the errors. */
15302 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
15304 return error_mark_node
;
15309 /* Wrapper for instantiate_template_1. */
15312 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
15315 timevar_push (TV_TEMPLATE_INST
);
15316 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
15317 timevar_pop (TV_TEMPLATE_INST
);
15321 /* Instantiate the alias template TMPL with ARGS. Also push a template
15322 instantiation level, which instantiate_template doesn't do because
15323 functions and variables have sufficient context established by the
15327 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
15329 struct pending_template
*old_last_pend
= last_pending_template
;
15330 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
15331 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
15332 return error_mark_node
;
15333 tree tinst
= build_tree_list (tmpl
, args
);
15334 if (!push_tinst_level (tinst
))
15337 return error_mark_node
;
15341 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
15342 args
, tmpl
, complain
,
15343 /*require_all_args=*/true,
15344 /*use_default_args=*/true);
15346 tree r
= instantiate_template (tmpl
, args
, complain
);
15347 pop_tinst_level ();
15348 /* We can't free this if a pending_template entry or last_error_tinst_level
15349 is pointing at it. */
15350 if (last_pending_template
== old_last_pend
15351 && last_error_tinst_level
== old_error_tinst
)
15357 /* PARM is a template parameter pack for FN. Returns true iff
15358 PARM is used in a deducible way in the argument list of FN. */
15361 pack_deducible_p (tree parm
, tree fn
)
15363 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
15364 for (; t
; t
= TREE_CHAIN (t
))
15366 tree type
= TREE_VALUE (t
);
15368 if (!PACK_EXPANSION_P (type
))
15370 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
15371 packs
; packs
= TREE_CHAIN (packs
))
15372 if (TREE_VALUE (packs
) == parm
)
15374 /* The template parameter pack is used in a function parameter
15375 pack. If this is the end of the parameter list, the
15376 template parameter pack is deducible. */
15377 if (TREE_CHAIN (t
) == void_list_node
)
15380 /* Otherwise, not. Well, it could be deduced from
15381 a non-pack parameter, but doing so would end up with
15382 a deduction mismatch, so don't bother. */
15386 /* The template parameter pack isn't used in any function parameter
15387 packs, but it might be used deeper, e.g. tuple<Args...>. */
15391 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
15392 NARGS elements of the arguments that are being used when calling
15393 it. TARGS is a vector into which the deduced template arguments
15396 Return zero for success, 2 for an incomplete match that doesn't resolve
15397 all the types, and 1 for complete failure. An error message will be
15398 printed only for an incomplete match.
15400 If FN is a conversion operator, or we are trying to produce a specific
15401 specialization, RETURN_TYPE is the return type desired.
15403 The EXPLICIT_TARGS are explicit template arguments provided via a
15406 The parameter STRICT is one of:
15409 We are deducing arguments for a function call, as in
15410 [temp.deduct.call].
15413 We are deducing arguments for a conversion function, as in
15414 [temp.deduct.conv].
15417 We are deducing arguments when doing an explicit instantiation
15418 as in [temp.explicit], when determining an explicit specialization
15419 as in [temp.expl.spec], or when taking the address of a function
15420 template, as in [temp.deduct.funcaddr]. */
15423 fn_type_unification (tree fn
,
15424 tree explicit_targs
,
15427 unsigned int nargs
,
15429 unification_kind_t strict
,
15436 tree decl
= NULL_TREE
;
15437 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
15439 static int deduction_depth
;
15440 struct pending_template
*old_last_pend
= last_pending_template
;
15441 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
15442 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
15444 tree r
= error_mark_node
;
15447 complain
|= tf_decltype
;
15449 /* In C++0x, it's possible to have a function template whose type depends
15450 on itself recursively. This is most obvious with decltype, but can also
15451 occur with enumeration scope (c++/48969). So we need to catch infinite
15452 recursion and reject the substitution at deduction time; this function
15453 will return error_mark_node for any repeated substitution.
15455 This also catches excessive recursion such as when f<N> depends on
15456 f<N-1> across all integers, and returns error_mark_node for all the
15457 substitutions back up to the initial one.
15459 This is, of course, not reentrant. */
15460 if (excessive_deduction_depth
)
15461 return error_mark_node
;
15462 tinst
= build_tree_list (fn
, NULL_TREE
);
15465 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
15467 fntype
= TREE_TYPE (fn
);
15468 if (explicit_targs
)
15472 The specified template arguments must match the template
15473 parameters in kind (i.e., type, nontype, template), and there
15474 must not be more arguments than there are parameters;
15475 otherwise type deduction fails.
15477 Nontype arguments must match the types of the corresponding
15478 nontype template parameters, or must be convertible to the
15479 types of the corresponding nontype parameters as specified in
15480 _temp.arg.nontype_, otherwise type deduction fails.
15482 All references in the function type of the function template
15483 to the corresponding template parameters are replaced by the
15484 specified template argument values. If a substitution in a
15485 template parameter or in the function type of the function
15486 template results in an invalid type, type deduction fails. */
15487 int i
, len
= TREE_VEC_LENGTH (tparms
);
15488 location_t loc
= input_location
;
15489 bool incomplete
= false;
15491 /* Adjust any explicit template arguments before entering the
15492 substitution context. */
15494 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
15496 /*require_all_args=*/false,
15497 /*use_default_args=*/false));
15498 if (explicit_targs
== error_mark_node
)
15501 /* Substitute the explicit args into the function type. This is
15502 necessary so that, for instance, explicitly declared function
15503 arguments can match null pointed constants. If we were given
15504 an incomplete set of explicit args, we must not do semantic
15505 processing during substitution as we could create partial
15507 for (i
= 0; i
< len
; i
++)
15509 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
15510 bool parameter_pack
= false;
15511 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
15513 /* Dig out the actual parm. */
15514 if (TREE_CODE (parm
) == TYPE_DECL
15515 || TREE_CODE (parm
) == TEMPLATE_DECL
)
15517 parm
= TREE_TYPE (parm
);
15518 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
15520 else if (TREE_CODE (parm
) == PARM_DECL
)
15522 parm
= DECL_INITIAL (parm
);
15523 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
15526 if (!parameter_pack
&& targ
== NULL_TREE
)
15527 /* No explicit argument for this template parameter. */
15530 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
15532 /* Mark the argument pack as "incomplete". We could
15533 still deduce more arguments during unification.
15534 We remove this mark in type_unification_real. */
15537 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
15538 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
15539 = ARGUMENT_PACK_ARGS (targ
);
15542 /* We have some incomplete argument packs. */
15547 TREE_VALUE (tinst
) = explicit_targs
;
15548 if (!push_tinst_level (tinst
))
15550 excessive_deduction_depth
= true;
15553 processing_template_decl
+= incomplete
;
15554 input_location
= DECL_SOURCE_LOCATION (fn
);
15555 /* Ignore any access checks; we'll see them again in
15556 instantiate_template and they might have the wrong
15557 access path at this point. */
15558 push_deferring_access_checks (dk_deferred
);
15559 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
15560 complain
| tf_partial
, NULL_TREE
);
15561 pop_deferring_access_checks ();
15562 input_location
= loc
;
15563 processing_template_decl
-= incomplete
;
15564 pop_tinst_level ();
15566 if (fntype
== error_mark_node
)
15569 /* Place the explicitly specified arguments in TARGS. */
15570 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
15571 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
15574 /* Never do unification on the 'this' parameter. */
15575 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
15581 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
15582 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
15583 new_args
[0] = return_type
;
15584 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
15589 /* We allow incomplete unification without an error message here
15590 because the standard doesn't seem to explicitly prohibit it. Our
15591 callers must be ready to deal with unification failures in any
15594 TREE_VALUE (tinst
) = targs
;
15595 /* If we aren't explaining yet, push tinst context so we can see where
15596 any errors (e.g. from class instantiations triggered by instantiation
15597 of default template arguments) come from. If we are explaining, this
15598 context is redundant. */
15599 if (!explain_p
&& !push_tinst_level (tinst
))
15601 excessive_deduction_depth
= true;
15605 /* type_unification_real will pass back any access checks from default
15606 template argument substitution. */
15607 vec
<deferred_access_check
, va_gc
> *checks
;
15610 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15611 targs
, parms
, args
, nargs
, /*subr=*/0,
15612 strict
, flags
, &checks
, explain_p
);
15614 pop_tinst_level ();
15618 /* Now that we have bindings for all of the template arguments,
15619 ensure that the arguments deduced for the template template
15620 parameters have compatible template parameter lists. We cannot
15621 check this property before we have deduced all template
15622 arguments, because the template parameter types of a template
15623 template parameter might depend on prior template parameters
15624 deduced after the template template parameter. The following
15625 ill-formed example illustrates this issue:
15627 template<typename T, template<T> class C> void f(C<5>, T);
15629 template<int N> struct X {};
15632 f(X<5>(), 5l); // error: template argument deduction fails
15635 The template parameter list of 'C' depends on the template type
15636 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15637 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15638 time that we deduce 'C'. */
15639 if (!template_template_parm_bindings_ok_p
15640 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
15642 unify_inconsistent_template_template_parameters (explain_p
);
15646 /* All is well so far. Now, check:
15650 When all template arguments have been deduced, all uses of
15651 template parameters in nondeduced contexts are replaced with
15652 the corresponding deduced argument values. If the
15653 substitution results in an invalid type, as described above,
15654 type deduction fails. */
15655 TREE_VALUE (tinst
) = targs
;
15656 if (!push_tinst_level (tinst
))
15658 excessive_deduction_depth
= true;
15662 /* Also collect access checks from the instantiation. */
15663 reopen_deferring_access_checks (checks
);
15665 decl
= instantiate_template (fn
, targs
, complain
);
15667 checks
= get_deferred_access_checks ();
15668 pop_deferring_access_checks ();
15670 pop_tinst_level ();
15672 if (decl
== error_mark_node
)
15675 /* Now perform any access checks encountered during substitution. */
15676 push_access_scope (decl
);
15677 ok
= perform_access_checks (checks
, complain
);
15678 pop_access_scope (decl
);
15682 /* If we're looking for an exact match, check that what we got
15683 is indeed an exact match. It might not be if some template
15684 parameters are used in non-deduced contexts. */
15685 if (strict
== DEDUCE_EXACT
)
15687 tree substed
= TREE_TYPE (decl
);
15691 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
15693 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
15694 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
15695 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
15697 unify_type_mismatch (explain_p
, args
[i
],
15698 TREE_VALUE (sarg
));
15707 if (excessive_deduction_depth
)
15709 if (deduction_depth
== 0)
15710 /* Reset once we're all the way out. */
15711 excessive_deduction_depth
= false;
15714 /* We can't free this if a pending_template entry or last_error_tinst_level
15715 is pointing at it. */
15716 if (last_pending_template
== old_last_pend
15717 && last_error_tinst_level
== old_error_tinst
)
15723 /* Adjust types before performing type deduction, as described in
15724 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15725 sections are symmetric. PARM is the type of a function parameter
15726 or the return type of the conversion function. ARG is the type of
15727 the argument passed to the call, or the type of the value
15728 initialized with the result of the conversion function.
15729 ARG_EXPR is the original argument expression, which may be null. */
15732 maybe_adjust_types_for_deduction (unification_kind_t strict
,
15746 /* Swap PARM and ARG throughout the remainder of this
15747 function; the handling is precisely symmetric since PARM
15748 will initialize ARG rather than vice versa. */
15756 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15757 too, but here handle it by stripping the reference from PARM
15758 rather than by adding it to ARG. */
15759 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15760 && TYPE_REF_IS_RVALUE (*parm
)
15761 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15762 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15763 && TREE_CODE (*arg
) == REFERENCE_TYPE
15764 && !TYPE_REF_IS_RVALUE (*arg
))
15765 *parm
= TREE_TYPE (*parm
);
15766 /* Nothing else to do in this case. */
15770 gcc_unreachable ();
15773 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
15775 /* [temp.deduct.call]
15777 If P is not a reference type:
15779 --If A is an array type, the pointer type produced by the
15780 array-to-pointer standard conversion (_conv.array_) is
15781 used in place of A for type deduction; otherwise,
15783 --If A is a function type, the pointer type produced by
15784 the function-to-pointer standard conversion
15785 (_conv.func_) is used in place of A for type deduction;
15788 --If A is a cv-qualified type, the top level
15789 cv-qualifiers of A's type are ignored for type
15791 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
15792 *arg
= build_pointer_type (TREE_TYPE (*arg
));
15793 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
15794 *arg
= build_pointer_type (*arg
);
15796 *arg
= TYPE_MAIN_VARIANT (*arg
);
15799 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15800 of the form T&&, where T is a template parameter, and the argument
15801 is an lvalue, T is deduced as A& */
15802 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15803 && TYPE_REF_IS_RVALUE (*parm
)
15804 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15805 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15806 && (arg_expr
? real_lvalue_p (arg_expr
)
15807 /* try_one_overload doesn't provide an arg_expr, but
15808 functions are always lvalues. */
15809 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
15810 *arg
= build_reference_type (*arg
);
15812 /* [temp.deduct.call]
15814 If P is a cv-qualified type, the top level cv-qualifiers
15815 of P's type are ignored for type deduction. If P is a
15816 reference type, the type referred to by P is used for
15818 *parm
= TYPE_MAIN_VARIANT (*parm
);
15819 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
15821 *parm
= TREE_TYPE (*parm
);
15822 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
15825 /* DR 322. For conversion deduction, remove a reference type on parm
15826 too (which has been swapped into ARG). */
15827 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
15828 *arg
= TREE_TYPE (*arg
);
15833 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15834 template which does contain any deducible template parameters; check if
15835 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15836 unify_one_argument. */
15839 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
15840 int flags
, bool explain_p
)
15845 type
= TREE_TYPE (arg
);
15849 if (same_type_p (parm
, type
))
15850 return unify_success (explain_p
);
15852 if (strict
== DEDUCE_CONV
)
15854 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
15855 explain_p
? tf_warning_or_error
: tf_none
))
15856 return unify_success (explain_p
);
15858 else if (strict
!= DEDUCE_EXACT
)
15860 if (can_convert_arg (parm
, type
,
15861 TYPE_P (arg
) ? NULL_TREE
: arg
,
15862 flags
, explain_p
? tf_warning_or_error
: tf_none
))
15863 return unify_success (explain_p
);
15866 if (strict
== DEDUCE_EXACT
)
15867 return unify_type_mismatch (explain_p
, parm
, arg
);
15869 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
15872 static bool uses_deducible_template_parms (tree type
);
15874 /* Returns true iff the expression EXPR is one from which a template
15875 argument can be deduced. In other words, if it's an undecorated
15876 use of a template non-type parameter. */
15879 deducible_expression (tree expr
)
15881 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
15884 /* Returns true iff the array domain DOMAIN uses a template parameter in a
15885 deducible way; that is, if it has a max value of <PARM> - 1. */
15888 deducible_array_bound (tree domain
)
15890 if (domain
== NULL_TREE
)
15893 tree max
= TYPE_MAX_VALUE (domain
);
15894 if (TREE_CODE (max
) != MINUS_EXPR
)
15897 return deducible_expression (TREE_OPERAND (max
, 0));
15900 /* Returns true iff the template arguments ARGS use a template parameter
15901 in a deducible way. */
15904 deducible_template_args (tree args
)
15906 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
15909 tree elt
= TREE_VEC_ELT (args
, i
);
15910 if (ARGUMENT_PACK_P (elt
))
15911 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
15914 if (PACK_EXPANSION_P (elt
))
15915 elt
= PACK_EXPANSION_PATTERN (elt
);
15916 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
15918 else if (TYPE_P (elt
))
15919 deducible
= uses_deducible_template_parms (elt
);
15921 deducible
= deducible_expression (elt
);
15929 /* Returns true iff TYPE contains any deducible references to template
15930 parameters, as per 14.8.2.5. */
15933 uses_deducible_template_parms (tree type
)
15935 if (PACK_EXPANSION_P (type
))
15936 type
= PACK_EXPANSION_PATTERN (type
);
15943 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
15944 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
15950 if (POINTER_TYPE_P (type
))
15951 return uses_deducible_template_parms (TREE_TYPE (type
));
15953 /* T[integer-constant ]
15955 if (TREE_CODE (type
) == ARRAY_TYPE
)
15956 return (uses_deducible_template_parms (TREE_TYPE (type
))
15957 || deducible_array_bound (TYPE_DOMAIN (type
)));
15969 if (TYPE_PTRMEM_P (type
))
15970 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
15971 || (uses_deducible_template_parms
15972 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
15974 /* template-name <T> (where template-name refers to a class template)
15975 template-name <i> (where template-name refers to a class template) */
15976 if (CLASS_TYPE_P (type
)
15977 && CLASSTYPE_TEMPLATE_INFO (type
)
15978 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
15979 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
15980 (CLASSTYPE_TI_ARGS (type
)));
15985 if (TREE_CODE (type
) == FUNCTION_TYPE
15986 || TREE_CODE (type
) == METHOD_TYPE
)
15988 if (uses_deducible_template_parms (TREE_TYPE (type
)))
15990 tree parm
= TYPE_ARG_TYPES (type
);
15991 if (TREE_CODE (type
) == METHOD_TYPE
)
15992 parm
= TREE_CHAIN (parm
);
15993 for (; parm
; parm
= TREE_CHAIN (parm
))
15994 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
16001 /* Subroutine of type_unification_real and unify_pack_expansion to
16002 handle unification of a single P/A pair. Parameters are as
16003 for those functions. */
16006 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
16007 int subr
, unification_kind_t strict
, int flags
,
16010 tree arg_expr
= NULL_TREE
;
16013 if (arg
== error_mark_node
|| parm
== error_mark_node
)
16014 return unify_invalid (explain_p
);
16015 if (arg
== unknown_type_node
)
16016 /* We can't deduce anything from this, but we might get all the
16017 template args from other function args. */
16018 return unify_success (explain_p
);
16020 /* Implicit conversions (Clause 4) will be performed on a function
16021 argument to convert it to the type of the corresponding function
16022 parameter if the parameter type contains no template-parameters that
16023 participate in template argument deduction. */
16024 if (TYPE_P (parm
) && !uses_template_parms (parm
))
16025 /* For function parameters that contain no template-parameters at all,
16026 we have historically checked for convertibility in order to shortcut
16027 consideration of this candidate. */
16028 return check_non_deducible_conversion (parm
, arg
, strict
, flags
,
16030 else if (strict
== DEDUCE_CALL
16031 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
16032 /* For function parameters with only non-deducible template parameters,
16034 return unify_success (explain_p
);
16039 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
16040 | UNIFY_ALLOW_MORE_CV_QUAL
16041 | UNIFY_ALLOW_DERIVED
);
16045 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
16049 arg_strict
= UNIFY_ALLOW_NONE
;
16053 gcc_unreachable ();
16056 /* We only do these transformations if this is the top-level
16057 parameter_type_list in a call or declaration matching; in other
16058 situations (nested function declarators, template argument lists) we
16059 won't be comparing a type to an expression, and we don't do any type
16065 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
16066 if (type_unknown_p (arg
))
16068 /* [temp.deduct.type] A template-argument can be
16069 deduced from a pointer to function or pointer
16070 to member function argument if the set of
16071 overloaded functions does not contain function
16072 templates and at most one of a set of
16073 overloaded functions provides a unique
16076 if (resolve_overloaded_unification
16077 (tparms
, targs
, parm
, arg
, strict
,
16078 arg_strict
, explain_p
))
16079 return unify_success (explain_p
);
16080 return unify_overload_resolution_failure (explain_p
, arg
);
16084 arg
= unlowered_expr_type (arg
);
16085 if (arg
== error_mark_node
)
16086 return unify_invalid (explain_p
);
16090 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
16093 gcc_assert ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
16094 == (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
));
16096 /* For deduction from an init-list we need the actual list. */
16097 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
16099 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
16102 /* Most parms like fn_type_unification.
16104 If SUBR is 1, we're being called recursively (to unify the
16105 arguments of a function or method parameter of a function
16108 CHECKS is a pointer to a vector of access checks encountered while
16109 substituting default template arguments. */
16112 type_unification_real (tree tparms
,
16116 unsigned int xnargs
,
16118 unification_kind_t strict
,
16120 vec
<deferred_access_check
, va_gc
> **checks
,
16125 int ntparms
= TREE_VEC_LENGTH (tparms
);
16126 int saw_undeduced
= 0;
16129 unsigned int nargs
;
16132 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
16133 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
16134 gcc_assert (ntparms
> 0);
16136 /* Reset the number of non-defaulted template arguments contained
16138 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
16146 while (parms
&& parms
!= void_list_node
16149 parm
= TREE_VALUE (parms
);
16151 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
16152 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
16153 /* For a function parameter pack that occurs at the end of the
16154 parameter-declaration-list, the type A of each remaining
16155 argument of the call is compared with the type P of the
16156 declarator-id of the function parameter pack. */
16159 parms
= TREE_CHAIN (parms
);
16161 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
16162 /* For a function parameter pack that does not occur at the
16163 end of the parameter-declaration-list, the type of the
16164 parameter pack is a non-deduced context. */
16170 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
16176 && parms
!= void_list_node
16177 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
16179 /* Unify the remaining arguments with the pack expansion type. */
16181 tree parmvec
= make_tree_vec (1);
16183 /* Allocate a TREE_VEC and copy in all of the arguments */
16184 argvec
= make_tree_vec (nargs
- ia
);
16185 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
16186 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
16188 /* Copy the parameter into parmvec. */
16189 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
16190 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
16191 /*subr=*/subr
, explain_p
))
16194 /* Advance to the end of the list of parameters. */
16195 parms
= TREE_CHAIN (parms
);
16198 /* Fail if we've reached the end of the parm list, and more args
16199 are present, and the parm list isn't variadic. */
16200 if (ia
< nargs
&& parms
== void_list_node
)
16201 return unify_too_many_arguments (explain_p
, nargs
, ia
);
16202 /* Fail if parms are left and they don't have default values. */
16203 if (parms
&& parms
!= void_list_node
16204 && TREE_PURPOSE (parms
) == NULL_TREE
)
16206 unsigned int count
= nargs
;
16208 while (p
&& p
!= void_list_node
)
16211 p
= TREE_CHAIN (p
);
16213 return unify_too_few_arguments (explain_p
, ia
, count
);
16218 tsubst_flags_t complain
= (explain_p
16219 ? tf_warning_or_error
16222 for (i
= 0; i
< ntparms
; i
++)
16224 tree targ
= TREE_VEC_ELT (targs
, i
);
16225 tree tparm
= TREE_VEC_ELT (tparms
, i
);
16227 /* Clear the "incomplete" flags on all argument packs now so that
16228 substituting them into later default arguments works. */
16229 if (targ
&& ARGUMENT_PACK_P (targ
))
16231 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
16232 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
16235 if (targ
|| tparm
== error_mark_node
)
16237 tparm
= TREE_VALUE (tparm
);
16239 /* If this is an undeduced nontype parameter that depends on
16240 a type parameter, try another pass; its type may have been
16241 deduced from a later argument than the one from which
16242 this parameter can be deduced. */
16243 if (TREE_CODE (tparm
) == PARM_DECL
16244 && uses_template_parms (TREE_TYPE (tparm
))
16245 && !saw_undeduced
++)
16248 /* Core issue #226 (C++0x) [temp.deduct]:
16250 If a template argument has not been deduced, its
16251 default template argument, if any, is used.
16253 When we are in C++98 mode, TREE_PURPOSE will either
16254 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16255 to explicitly check cxx_dialect here. */
16256 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
16258 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
16259 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
16260 reopen_deferring_access_checks (*checks
);
16261 location_t save_loc
= input_location
;
16263 input_location
= DECL_SOURCE_LOCATION (parm
);
16264 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
16265 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
16267 input_location
= save_loc
;
16268 *checks
= get_deferred_access_checks ();
16269 pop_deferring_access_checks ();
16270 if (arg
== error_mark_node
)
16274 TREE_VEC_ELT (targs
, i
) = arg
;
16275 /* The position of the first default template argument,
16276 is also the number of non-defaulted arguments in TARGS.
16278 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
16279 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
16284 /* If the type parameter is a parameter pack, then it will
16285 be deduced to an empty parameter pack. */
16286 if (template_parameter_pack_p (tparm
))
16290 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
16292 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
16293 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
16294 TREE_CONSTANT (arg
) = 1;
16297 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
16299 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
16301 TREE_VEC_ELT (targs
, i
) = arg
;
16305 return unify_parameter_deduction_failure (explain_p
, tparm
);
16308 #ifdef ENABLE_CHECKING
16309 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
16310 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
16313 return unify_success (explain_p
);
16316 /* Subroutine of type_unification_real. Args are like the variables
16317 at the call site. ARG is an overloaded function (or template-id);
16318 we try deducing template args from each of the overloads, and if
16319 only one succeeds, we go with that. Modifies TARGS and returns
16320 true on success. */
16323 resolve_overloaded_unification (tree tparms
,
16327 unification_kind_t strict
,
16331 tree tempargs
= copy_node (targs
);
16333 tree goodfn
= NULL_TREE
;
16336 if (TREE_CODE (arg
) == ADDR_EXPR
)
16338 arg
= TREE_OPERAND (arg
, 0);
16344 if (TREE_CODE (arg
) == COMPONENT_REF
)
16345 /* Handle `&x' where `x' is some static or non-static member
16347 arg
= TREE_OPERAND (arg
, 1);
16349 if (TREE_CODE (arg
) == OFFSET_REF
)
16350 arg
= TREE_OPERAND (arg
, 1);
16352 /* Strip baselink information. */
16353 if (BASELINK_P (arg
))
16354 arg
= BASELINK_FUNCTIONS (arg
);
16356 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
16358 /* If we got some explicit template args, we need to plug them into
16359 the affected templates before we try to unify, in case the
16360 explicit args will completely resolve the templates in question. */
16363 tree expl_subargs
= TREE_OPERAND (arg
, 1);
16364 arg
= TREE_OPERAND (arg
, 0);
16366 for (; arg
; arg
= OVL_NEXT (arg
))
16368 tree fn
= OVL_CURRENT (arg
);
16369 tree subargs
, elem
;
16371 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
16374 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
16375 expl_subargs
, NULL_TREE
, tf_none
,
16376 /*require_all_args=*/true,
16377 /*use_default_args=*/true);
16378 if (subargs
!= error_mark_node
16379 && !any_dependent_template_arguments_p (subargs
))
16381 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
16382 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
16383 elem
, strict
, sub_strict
, addr_p
, explain_p
)
16384 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
16393 /* If no templates (or more than one) are fully resolved by the
16394 explicit arguments, this template-id is a non-deduced context; it
16395 could still be OK if we deduce all template arguments for the
16396 enclosing call through other arguments. */
16400 else if (TREE_CODE (arg
) != OVERLOAD
16401 && TREE_CODE (arg
) != FUNCTION_DECL
)
16402 /* If ARG is, for example, "(0, &f)" then its type will be unknown
16403 -- but the deduction does not succeed because the expression is
16404 not just the function on its own. */
16407 for (; arg
; arg
= OVL_NEXT (arg
))
16408 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
16409 TREE_TYPE (OVL_CURRENT (arg
)),
16410 strict
, sub_strict
, addr_p
, explain_p
)
16411 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
16413 goodfn
= OVL_CURRENT (arg
);
16417 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16418 to function or pointer to member function argument if the set of
16419 overloaded functions does not contain function templates and at most
16420 one of a set of overloaded functions provides a unique match.
16422 So if we found multiple possibilities, we return success but don't
16423 deduce anything. */
16427 int i
= TREE_VEC_LENGTH (targs
);
16429 if (TREE_VEC_ELT (tempargs
, i
))
16430 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
16438 /* Core DR 115: In contexts where deduction is done and fails, or in
16439 contexts where deduction is not done, if a template argument list is
16440 specified and it, along with any default template arguments, identifies
16441 a single function template specialization, then the template-id is an
16442 lvalue for the function template specialization. */
16445 resolve_nondeduced_context (tree orig_expr
)
16447 tree expr
, offset
, baselink
;
16450 if (!type_unknown_p (orig_expr
))
16455 offset
= NULL_TREE
;
16456 baselink
= NULL_TREE
;
16458 if (TREE_CODE (expr
) == ADDR_EXPR
)
16460 expr
= TREE_OPERAND (expr
, 0);
16463 if (TREE_CODE (expr
) == OFFSET_REF
)
16466 expr
= TREE_OPERAND (expr
, 1);
16468 if (BASELINK_P (expr
))
16471 expr
= BASELINK_FUNCTIONS (expr
);
16474 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
16477 tree goodfn
= NULL_TREE
;
16479 /* If we got some explicit template args, we need to plug them into
16480 the affected templates before we try to unify, in case the
16481 explicit args will completely resolve the templates in question. */
16483 tree expl_subargs
= TREE_OPERAND (expr
, 1);
16484 tree arg
= TREE_OPERAND (expr
, 0);
16485 tree badfn
= NULL_TREE
;
16486 tree badargs
= NULL_TREE
;
16488 for (; arg
; arg
= OVL_NEXT (arg
))
16490 tree fn
= OVL_CURRENT (arg
);
16491 tree subargs
, elem
;
16493 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
16496 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
16497 expl_subargs
, NULL_TREE
, tf_none
,
16498 /*require_all_args=*/true,
16499 /*use_default_args=*/true);
16500 if (subargs
!= error_mark_node
16501 && !any_dependent_template_arguments_p (subargs
))
16503 elem
= instantiate_template (fn
, subargs
, tf_none
);
16504 if (elem
== error_mark_node
)
16509 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
16518 mark_used (goodfn
);
16521 expr
= build_baselink (BASELINK_BINFO (baselink
),
16522 BASELINK_ACCESS_BINFO (baselink
),
16523 expr
, BASELINK_OPTYPE (baselink
));
16527 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
16528 expr
= build_offset_ref (base
, expr
, addr
, tf_warning_or_error
);
16531 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
16534 else if (good
== 0 && badargs
)
16535 /* There were no good options and at least one bad one, so let the
16536 user know what the problem is. */
16537 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
16542 /* Subroutine of resolve_overloaded_unification; does deduction for a single
16543 overload. Fills TARGS with any deduced arguments, or error_mark_node if
16544 different overloads deduce different arguments for a given parm.
16545 ADDR_P is true if the expression for which deduction is being
16546 performed was of the form "& fn" rather than simply "fn".
16548 Returns 1 on success. */
16551 try_one_overload (tree tparms
,
16556 unification_kind_t strict
,
16565 if (arg
== error_mark_node
)
16568 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16569 to function or pointer to member function argument if the set of
16570 overloaded functions does not contain function templates and at most
16571 one of a set of overloaded functions provides a unique match.
16573 So if this is a template, just return success. */
16575 if (uses_template_parms (arg
))
16578 if (TREE_CODE (arg
) == METHOD_TYPE
)
16579 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
16581 arg
= build_pointer_type (arg
);
16583 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
16585 /* We don't copy orig_targs for this because if we have already deduced
16586 some template args from previous args, unify would complain when we
16587 try to deduce a template parameter for the same argument, even though
16588 there isn't really a conflict. */
16589 nargs
= TREE_VEC_LENGTH (targs
);
16590 tempargs
= make_tree_vec (nargs
);
16592 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
16595 /* First make sure we didn't deduce anything that conflicts with
16596 explicitly specified args. */
16597 for (i
= nargs
; i
--; )
16599 tree elt
= TREE_VEC_ELT (tempargs
, i
);
16600 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
16604 else if (uses_template_parms (elt
))
16605 /* Since we're unifying against ourselves, we will fill in
16606 template args used in the function parm list with our own
16607 template parms. Discard them. */
16608 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
16609 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
16613 for (i
= nargs
; i
--; )
16615 tree elt
= TREE_VEC_ELT (tempargs
, i
);
16618 TREE_VEC_ELT (targs
, i
) = elt
;
16624 /* PARM is a template class (perhaps with unbound template
16625 parameters). ARG is a fully instantiated type. If ARG can be
16626 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
16627 TARGS are as for unify. */
16630 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
16633 tree copy_of_targs
;
16635 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
16636 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
16637 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
16640 /* We need to make a new template argument vector for the call to
16641 unify. If we used TARGS, we'd clutter it up with the result of
16642 the attempted unification, even if this class didn't work out.
16643 We also don't want to commit ourselves to all the unifications
16644 we've already done, since unification is supposed to be done on
16645 an argument-by-argument basis. In other words, consider the
16646 following pathological case:
16648 template <int I, int J, int K>
16651 template <int I, int J>
16652 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
16654 template <int I, int J, int K>
16655 void f(S<I, J, K>, S<I, I, I>);
16664 Now, by the time we consider the unification involving `s2', we
16665 already know that we must have `f<0, 0, 0>'. But, even though
16666 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16667 because there are two ways to unify base classes of S<0, 1, 2>
16668 with S<I, I, I>. If we kept the already deduced knowledge, we
16669 would reject the possibility I=1. */
16670 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
16672 /* If unification failed, we're done. */
16673 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
16674 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
16680 /* Given a template type PARM and a class type ARG, find the unique
16681 base type in ARG that is an instance of PARM. We do not examine
16682 ARG itself; only its base-classes. If there is not exactly one
16683 appropriate base class, return NULL_TREE. PARM may be the type of
16684 a partial specialization, as well as a plain template type. Used
16687 static enum template_base_result
16688 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
16689 bool explain_p
, tree
*result
)
16691 tree rval
= NULL_TREE
;
16694 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
16696 binfo
= TYPE_BINFO (complete_type (arg
));
16699 /* The type could not be completed. */
16700 *result
= NULL_TREE
;
16701 return tbr_incomplete_type
;
16704 /* Walk in inheritance graph order. The search order is not
16705 important, and this avoids multiple walks of virtual bases. */
16706 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
16708 tree r
= try_class_unification (tparms
, targs
, parm
,
16709 BINFO_TYPE (binfo
), explain_p
);
16713 /* If there is more than one satisfactory baseclass, then:
16717 If they yield more than one possible deduced A, the type
16721 if (rval
&& !same_type_p (r
, rval
))
16723 *result
= NULL_TREE
;
16724 return tbr_ambiguous_baseclass
;
16732 return tbr_success
;
16735 /* Returns the level of DECL, which declares a template parameter. */
16738 template_decl_level (tree decl
)
16740 switch (TREE_CODE (decl
))
16743 case TEMPLATE_DECL
:
16744 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
16747 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
16750 gcc_unreachable ();
16755 /* Decide whether ARG can be unified with PARM, considering only the
16756 cv-qualifiers of each type, given STRICT as documented for unify.
16757 Returns nonzero iff the unification is OK on that basis. */
16760 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
16762 int arg_quals
= cp_type_quals (arg
);
16763 int parm_quals
= cp_type_quals (parm
);
16765 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16766 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
16768 /* Although a CVR qualifier is ignored when being applied to a
16769 substituted template parameter ([8.3.2]/1 for example), that
16770 does not allow us to unify "const T" with "int&" because both
16771 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16772 It is ok when we're allowing additional CV qualifiers
16773 at the outer level [14.8.2.1]/3,1st bullet. */
16774 if ((TREE_CODE (arg
) == REFERENCE_TYPE
16775 || TREE_CODE (arg
) == FUNCTION_TYPE
16776 || TREE_CODE (arg
) == METHOD_TYPE
)
16777 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
16780 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
16781 && (parm_quals
& TYPE_QUAL_RESTRICT
))
16785 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
16786 && (arg_quals
& parm_quals
) != parm_quals
)
16789 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
16790 && (parm_quals
& arg_quals
) != arg_quals
)
16796 /* Determines the LEVEL and INDEX for the template parameter PARM. */
16798 template_parm_level_and_index (tree parm
, int* level
, int* index
)
16800 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16801 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16802 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16804 *index
= TEMPLATE_TYPE_IDX (parm
);
16805 *level
= TEMPLATE_TYPE_LEVEL (parm
);
16809 *index
= TEMPLATE_PARM_IDX (parm
);
16810 *level
= TEMPLATE_PARM_LEVEL (parm
);
16814 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
16816 if (unify (TP, TA, P, A, S, EP)) \
16820 /* Unifies the remaining arguments in PACKED_ARGS with the pack
16821 expansion at the end of PACKED_PARMS. Returns 0 if the type
16822 deduction succeeds, 1 otherwise. STRICT is the same as in
16823 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
16824 call argument list. We'll need to adjust the arguments to make them
16825 types. SUBR tells us if this is from a recursive call to
16826 type_unification_real, or for comparing two template argument
16830 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
16831 tree packed_args
, unification_kind_t strict
,
16832 bool subr
, bool explain_p
)
16835 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
16836 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
16837 tree pack
, packs
= NULL_TREE
;
16838 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
16839 int len
= TREE_VEC_LENGTH (packed_args
);
16841 /* Determine the parameter packs we will be deducing from the
16842 pattern, and record their current deductions. */
16843 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
16844 pack
; pack
= TREE_CHAIN (pack
))
16846 tree parm_pack
= TREE_VALUE (pack
);
16849 /* Determine the index and level of this parameter pack. */
16850 template_parm_level_and_index (parm_pack
, &level
, &idx
);
16852 /* Keep track of the parameter packs and their corresponding
16854 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
16855 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
16858 /* Loop through all of the arguments that have not yet been
16859 unified and unify each with the pattern. */
16860 for (i
= start
; i
< len
; i
++)
16863 bool any_explicit
= false;
16864 tree arg
= TREE_VEC_ELT (packed_args
, i
);
16866 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16867 or the element of its argument pack at the current index if
16868 this argument was explicitly specified. */
16869 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16873 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16876 if (TREE_VALUE (pack
)
16877 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
16878 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
16880 any_explicit
= true;
16881 arg
= TREE_VEC_ELT (pargs
, i
- start
);
16883 TMPL_ARG (targs
, level
, idx
) = arg
;
16886 /* If we had explicit template arguments, substitute them into the
16887 pattern before deduction. */
16890 /* Some arguments might still be unspecified or dependent. */
16892 ++processing_template_decl
;
16893 dependent
= any_dependent_template_arguments_p (targs
);
16895 --processing_template_decl
;
16896 parm
= tsubst (pattern
, targs
,
16897 explain_p
? tf_warning_or_error
: tf_none
,
16900 --processing_template_decl
;
16901 if (parm
== error_mark_node
)
16907 /* Unify the pattern with the current argument. */
16908 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
16909 LOOKUP_IMPLICIT
, explain_p
))
16912 /* For each parameter pack, collect the deduced value. */
16913 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16916 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16918 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
16919 TMPL_ARG (targs
, level
, idx
);
16923 /* Verify that the results of unification with the parameter packs
16924 produce results consistent with what we've seen before, and make
16925 the deduced argument packs available. */
16926 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16928 tree old_pack
= TREE_VALUE (pack
);
16929 tree new_args
= TREE_TYPE (pack
);
16930 int i
, len
= TREE_VEC_LENGTH (new_args
);
16932 bool nondeduced_p
= false;
16934 /* By default keep the original deduced argument pack.
16935 If necessary, more specific code is going to update the
16936 resulting deduced argument later down in this function. */
16937 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16938 TMPL_ARG (targs
, level
, idx
) = old_pack
;
16940 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16941 actually deduce anything. */
16942 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
16943 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
16944 nondeduced_p
= true;
16948 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
16950 /* If we had fewer function args than explicit template args,
16951 just use the explicits. */
16952 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16953 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
16954 if (len
< explicit_len
)
16955 new_args
= explicit_args
;
16961 /* Build the deduced *_ARGUMENT_PACK. */
16962 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
16964 result
= make_node (NONTYPE_ARGUMENT_PACK
);
16965 TREE_TYPE (result
) =
16966 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
16967 TREE_CONSTANT (result
) = 1;
16970 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
16972 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
16974 /* Note the deduced argument packs for this parameter
16976 TMPL_ARG (targs
, level
, idx
) = result
;
16978 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
16979 && (ARGUMENT_PACK_ARGS (old_pack
)
16980 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
16982 /* We only had the explicitly-provided arguments before, but
16983 now we have a complete set of arguments. */
16984 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16986 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
16987 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
16988 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
16992 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
16993 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
16995 if (!comp_template_args_with_info (old_args
, new_args
,
16996 &bad_old_arg
, &bad_new_arg
))
16997 /* Inconsistent unification of this parameter pack. */
16998 return unify_parameter_pack_inconsistent (explain_p
,
17004 return unify_success (explain_p
);
17007 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17008 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17009 parameters and return value are as for unify. */
17012 unify_array_domain (tree tparms
, tree targs
,
17013 tree parm_dom
, tree arg_dom
,
17021 /* Our representation of array types uses "N - 1" as the
17022 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17023 not an integer constant. We cannot unify arbitrarily
17024 complex expressions, so we eliminate the MINUS_EXPRs
17026 parm_max
= TYPE_MAX_VALUE (parm_dom
);
17027 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
17030 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
17031 parm_max
= TREE_OPERAND (parm_max
, 0);
17033 arg_max
= TYPE_MAX_VALUE (arg_dom
);
17034 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
17037 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17038 trying to unify the type of a variable with the type
17039 of a template parameter. For example:
17041 template <unsigned int N>
17042 void f (char (&) [N]);
17049 Here, the type of the ARG will be "int [g(i)]", and
17050 may be a SAVE_EXPR, etc. */
17051 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
17052 return unify_vla_arg (explain_p
, arg_dom
);
17053 arg_max
= TREE_OPERAND (arg_max
, 0);
17056 /* If only one of the bounds used a MINUS_EXPR, compensate
17057 by adding one to the other bound. */
17058 if (parm_cst
&& !arg_cst
)
17059 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
17063 else if (arg_cst
&& !parm_cst
)
17064 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
17069 return unify (tparms
, targs
, parm_max
, arg_max
,
17070 UNIFY_ALLOW_INTEGER
, explain_p
);
17073 /* Deduce the value of template parameters. TPARMS is the (innermost)
17074 set of template parameters to a template. TARGS is the bindings
17075 for those template parameters, as determined thus far; TARGS may
17076 include template arguments for outer levels of template parameters
17077 as well. PARM is a parameter to a template function, or a
17078 subcomponent of that parameter; ARG is the corresponding argument.
17079 This function attempts to match PARM with ARG in a manner
17080 consistent with the existing assignments in TARGS. If more values
17081 are deduced, then TARGS is updated.
17083 Returns 0 if the type deduction succeeds, 1 otherwise. The
17084 parameter STRICT is a bitwise or of the following flags:
17087 Require an exact match between PARM and ARG.
17088 UNIFY_ALLOW_MORE_CV_QUAL:
17089 Allow the deduced ARG to be more cv-qualified (by qualification
17090 conversion) than ARG.
17091 UNIFY_ALLOW_LESS_CV_QUAL:
17092 Allow the deduced ARG to be less cv-qualified than ARG.
17093 UNIFY_ALLOW_DERIVED:
17094 Allow the deduced ARG to be a template base class of ARG,
17095 or a pointer to a template base class of the type pointed to by
17097 UNIFY_ALLOW_INTEGER:
17098 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17099 case for more information.
17100 UNIFY_ALLOW_OUTER_LEVEL:
17101 This is the outermost level of a deduction. Used to determine validity
17102 of qualification conversions. A valid qualification conversion must
17103 have const qualified pointers leading up to the inner type which
17104 requires additional CV quals, except at the outer level, where const
17105 is not required [conv.qual]. It would be normal to set this flag in
17106 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17107 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17108 This is the outermost level of a deduction, and PARM can be more CV
17109 qualified at this point.
17110 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17111 This is the outermost level of a deduction, and PARM can be less CV
17112 qualified at this point. */
17115 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
17121 int strict_in
= strict
;
17123 /* I don't think this will do the right thing with respect to types.
17124 But the only case I've seen it in so far has been array bounds, where
17125 signedness is the only information lost, and I think that will be
17127 while (TREE_CODE (parm
) == NOP_EXPR
)
17128 parm
= TREE_OPERAND (parm
, 0);
17130 if (arg
== error_mark_node
)
17131 return unify_invalid (explain_p
);
17132 if (arg
== unknown_type_node
17133 || arg
== init_list_type_node
)
17134 /* We can't deduce anything from this, but we might get all the
17135 template args from other function args. */
17136 return unify_success (explain_p
);
17138 /* If PARM uses template parameters, then we can't bail out here,
17139 even if ARG == PARM, since we won't record unifications for the
17140 template parameters. We might need them if we're trying to
17141 figure out which of two things is more specialized. */
17142 if (arg
== parm
&& !uses_template_parms (parm
))
17143 return unify_success (explain_p
);
17145 /* Handle init lists early, so the rest of the function can assume
17146 we're dealing with a type. */
17147 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
17151 tree orig_parm
= parm
;
17153 /* Replace T with std::initializer_list<T> for deduction. */
17154 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17155 && flag_deduce_init_list
)
17156 parm
= listify (parm
);
17158 if (!is_std_init_list (parm
)
17159 && TREE_CODE (parm
) != ARRAY_TYPE
)
17160 /* We can only deduce from an initializer list argument if the
17161 parameter is std::initializer_list or an array; otherwise this
17162 is a non-deduced context. */
17163 return unify_success (explain_p
);
17165 if (TREE_CODE (parm
) == ARRAY_TYPE
)
17166 elttype
= TREE_TYPE (parm
);
17168 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
17170 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
17172 int elt_strict
= strict
;
17174 if (elt
== error_mark_node
)
17175 return unify_invalid (explain_p
);
17177 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
17179 tree type
= TREE_TYPE (elt
);
17180 /* It should only be possible to get here for a call. */
17181 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
17182 elt_strict
|= maybe_adjust_types_for_deduction
17183 (DEDUCE_CALL
, &elttype
, &type
, elt
);
17187 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
17191 if (TREE_CODE (parm
) == ARRAY_TYPE
)
17193 /* Also deduce from the length of the initializer list. */
17194 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
17195 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
17196 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
17200 /* If the std::initializer_list<T> deduction worked, replace the
17201 deduced A with std::initializer_list<A>. */
17202 if (orig_parm
!= parm
)
17204 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
17205 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17206 targ
= listify (targ
);
17207 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
17209 return unify_success (explain_p
);
17212 /* Immediately reject some pairs that won't unify because of
17213 cv-qualification mismatches. */
17214 if (TREE_CODE (arg
) == TREE_CODE (parm
)
17216 /* It is the elements of the array which hold the cv quals of an array
17217 type, and the elements might be template type parms. We'll check
17218 when we recurse. */
17219 && TREE_CODE (arg
) != ARRAY_TYPE
17220 /* We check the cv-qualifiers when unifying with template type
17221 parameters below. We want to allow ARG `const T' to unify with
17222 PARM `T' for example, when computing which of two templates
17223 is more specialized, for example. */
17224 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
17225 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
17226 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17228 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
17229 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
17230 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
17231 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
17232 strict
&= ~UNIFY_ALLOW_DERIVED
;
17233 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
17234 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
17236 switch (TREE_CODE (parm
))
17238 case TYPENAME_TYPE
:
17240 case UNBOUND_CLASS_TEMPLATE
:
17241 /* In a type which contains a nested-name-specifier, template
17242 argument values cannot be deduced for template parameters used
17243 within the nested-name-specifier. */
17244 return unify_success (explain_p
);
17246 case TEMPLATE_TYPE_PARM
:
17247 case TEMPLATE_TEMPLATE_PARM
:
17248 case BOUND_TEMPLATE_TEMPLATE_PARM
:
17249 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
17250 if (tparm
== error_mark_node
)
17251 return unify_invalid (explain_p
);
17253 if (TEMPLATE_TYPE_LEVEL (parm
)
17254 != template_decl_level (tparm
))
17255 /* The PARM is not one we're trying to unify. Just check
17256 to see if it matches ARG. */
17258 if (TREE_CODE (arg
) == TREE_CODE (parm
)
17259 && (is_auto (parm
) ? is_auto (arg
)
17260 : same_type_p (parm
, arg
)))
17261 return unify_success (explain_p
);
17263 return unify_type_mismatch (explain_p
, parm
, arg
);
17265 idx
= TEMPLATE_TYPE_IDX (parm
);
17266 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17267 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
17268 if (tparm
== error_mark_node
)
17269 return unify_invalid (explain_p
);
17271 /* Check for mixed types and values. */
17272 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
17273 && TREE_CODE (tparm
) != TYPE_DECL
)
17274 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
17275 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
17276 gcc_unreachable ();
17278 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17280 /* ARG must be constructed from a template class or a template
17281 template parameter. */
17282 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
17283 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
17284 return unify_template_deduction_failure (explain_p
, parm
, arg
);
17287 tree parmvec
= TYPE_TI_ARGS (parm
);
17288 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
17289 tree full_argvec
= add_to_template_args (targs
, argvec
);
17291 = DECL_INNERMOST_TEMPLATE_PARMS
17292 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
17294 int parm_variadic_p
= 0;
17296 /* The resolution to DR150 makes clear that default
17297 arguments for an N-argument may not be used to bind T
17298 to a template template parameter with fewer than N
17299 parameters. It is not safe to permit the binding of
17300 default arguments as an extension, as that may change
17301 the meaning of a conforming program. Consider:
17303 struct Dense { static const unsigned int dim = 1; };
17305 template <template <typename> class View,
17307 void operator+(float, View<Block> const&);
17309 template <typename Block,
17310 unsigned int Dim = Block::dim>
17311 struct Lvalue_proxy { operator float() const; };
17315 Lvalue_proxy<Dense> p;
17320 Here, if Lvalue_proxy is permitted to bind to View, then
17321 the global operator+ will be used; if they are not, the
17322 Lvalue_proxy will be converted to float. */
17323 if (coerce_template_parms (parm_parms
,
17325 TYPE_TI_TEMPLATE (parm
),
17327 ? tf_warning_or_error
17329 /*require_all_args=*/true,
17330 /*use_default_args=*/false)
17331 == error_mark_node
)
17334 /* Deduce arguments T, i from TT<T> or TT<i>.
17335 We check each element of PARMVEC and ARGVEC individually
17336 rather than the whole TREE_VEC since they can have
17337 different number of elements. */
17339 parmvec
= expand_template_argument_pack (parmvec
);
17340 argvec
= expand_template_argument_pack (argvec
);
17342 len
= TREE_VEC_LENGTH (parmvec
);
17344 /* Check if the parameters end in a pack, making them
17347 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
17348 parm_variadic_p
= 1;
17350 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17351 /* If the template argument list of P contains a pack
17352 expansion that is not the last template argument, the
17353 entire template argument list is a non-deduced
17355 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
17356 return unify_success (explain_p
);
17358 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
17359 return unify_too_few_arguments (explain_p
,
17360 TREE_VEC_LENGTH (argvec
), len
);
17362 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17364 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
17365 TREE_VEC_ELT (parmvec
, i
),
17366 TREE_VEC_ELT (argvec
, i
),
17367 UNIFY_ALLOW_NONE
, explain_p
);
17370 if (parm_variadic_p
17371 && unify_pack_expansion (tparms
, targs
,
17374 /*subr=*/true, explain_p
))
17377 arg
= TYPE_TI_TEMPLATE (arg
);
17379 /* Fall through to deduce template name. */
17382 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
17383 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17385 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
17387 /* Simple cases: Value already set, does match or doesn't. */
17388 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
17389 return unify_success (explain_p
);
17391 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
17395 /* If PARM is `const T' and ARG is only `int', we don't have
17396 a match unless we are allowing additional qualification.
17397 If ARG is `const int' and PARM is just `T' that's OK;
17398 that binds `const int' to `T'. */
17399 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
17401 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17403 /* Consider the case where ARG is `const volatile int' and
17404 PARM is `const T'. Then, T should be `volatile int'. */
17405 arg
= cp_build_qualified_type_real
17406 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
17407 if (arg
== error_mark_node
)
17408 return unify_invalid (explain_p
);
17410 /* Simple cases: Value already set, does match or doesn't. */
17411 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
17412 return unify_success (explain_p
);
17414 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
17416 /* Make sure that ARG is not a variable-sized array. (Note
17417 that were talking about variable-sized arrays (like
17418 `int[n]'), rather than arrays of unknown size (like
17419 `int[]').) We'll get very confused by such a type since
17420 the bound of the array is not constant, and therefore
17421 not mangleable. Besides, such types are not allowed in
17422 ISO C++, so we can do as we please here. We do allow
17423 them for 'auto' deduction, since that isn't ABI-exposed. */
17424 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
17425 return unify_vla_arg (explain_p
, arg
);
17427 /* Strip typedefs as in convert_template_argument. */
17428 arg
= canonicalize_type_argument (arg
, tf_none
);
17431 /* If ARG is a parameter pack or an expansion, we cannot unify
17432 against it unless PARM is also a parameter pack. */
17433 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
17434 && !template_parameter_pack_p (parm
))
17435 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
17437 /* If the argument deduction results is a METHOD_TYPE,
17438 then there is a problem.
17439 METHOD_TYPE doesn't map to any real C++ type the result of
17440 the deduction can not be of that type. */
17441 if (TREE_CODE (arg
) == METHOD_TYPE
)
17442 return unify_method_type_error (explain_p
, arg
);
17444 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
17445 return unify_success (explain_p
);
17447 case TEMPLATE_PARM_INDEX
:
17448 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
17449 if (tparm
== error_mark_node
)
17450 return unify_invalid (explain_p
);
17452 if (TEMPLATE_PARM_LEVEL (parm
)
17453 != template_decl_level (tparm
))
17455 /* The PARM is not one we're trying to unify. Just check
17456 to see if it matches ARG. */
17457 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
17458 && cp_tree_equal (parm
, arg
));
17460 unify_expression_unequal (explain_p
, parm
, arg
);
17464 idx
= TEMPLATE_PARM_IDX (parm
);
17465 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17469 int x
= !cp_tree_equal (targ
, arg
);
17471 unify_inconsistency (explain_p
, parm
, targ
, arg
);
17475 /* [temp.deduct.type] If, in the declaration of a function template
17476 with a non-type template-parameter, the non-type
17477 template-parameter is used in an expression in the function
17478 parameter-list and, if the corresponding template-argument is
17479 deduced, the template-argument type shall match the type of the
17480 template-parameter exactly, except that a template-argument
17481 deduced from an array bound may be of any integral type.
17482 The non-type parameter might use already deduced type parameters. */
17483 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
17484 if (!TREE_TYPE (arg
))
17485 /* Template-parameter dependent expression. Just accept it for now.
17486 It will later be processed in convert_template_argument. */
17488 else if (same_type_p (TREE_TYPE (arg
), tparm
))
17490 else if ((strict
& UNIFY_ALLOW_INTEGER
)
17491 && CP_INTEGRAL_TYPE_P (tparm
))
17492 /* Convert the ARG to the type of PARM; the deduced non-type
17493 template argument must exactly match the types of the
17494 corresponding parameter. */
17495 arg
= fold (build_nop (tparm
, arg
));
17496 else if (uses_template_parms (tparm
))
17497 /* We haven't deduced the type of this parameter yet. Try again
17499 return unify_success (explain_p
);
17501 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
17503 /* If ARG is a parameter pack or an expansion, we cannot unify
17504 against it unless PARM is also a parameter pack. */
17505 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
17506 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
17507 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
17509 arg
= strip_typedefs_expr (arg
);
17510 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
17511 return unify_success (explain_p
);
17515 /* A pointer-to-member constant can be unified only with
17516 another constant. */
17517 if (TREE_CODE (arg
) != PTRMEM_CST
)
17518 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
17520 /* Just unify the class member. It would be useless (and possibly
17521 wrong, depending on the strict flags) to unify also
17522 PTRMEM_CST_CLASS, because we want to be sure that both parm and
17523 arg refer to the same variable, even if through different
17524 classes. For instance:
17526 struct A { int x; };
17529 Unification of &A::x and &B::x must succeed. */
17530 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
17531 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
17536 if (!TYPE_PTR_P (arg
))
17537 return unify_type_mismatch (explain_p
, parm
, arg
);
17539 /* [temp.deduct.call]
17541 A can be another pointer or pointer to member type that can
17542 be converted to the deduced A via a qualification
17543 conversion (_conv.qual_).
17545 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
17546 This will allow for additional cv-qualification of the
17547 pointed-to types if appropriate. */
17549 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
17550 /* The derived-to-base conversion only persists through one
17551 level of pointers. */
17552 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
17554 return unify (tparms
, targs
, TREE_TYPE (parm
),
17555 TREE_TYPE (arg
), strict
, explain_p
);
17558 case REFERENCE_TYPE
:
17559 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
17560 return unify_type_mismatch (explain_p
, parm
, arg
);
17561 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17562 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
17565 if (TREE_CODE (arg
) != ARRAY_TYPE
)
17566 return unify_type_mismatch (explain_p
, parm
, arg
);
17567 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
17568 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
17569 return unify_type_mismatch (explain_p
, parm
, arg
);
17570 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17571 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
17572 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
17573 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
17574 TYPE_DOMAIN (arg
), explain_p
);
17575 return unify_success (explain_p
);
17582 case ENUMERAL_TYPE
:
17585 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17586 return unify_type_mismatch (explain_p
, parm
, arg
);
17588 /* We have already checked cv-qualification at the top of the
17590 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
17591 return unify_type_mismatch (explain_p
, parm
, arg
);
17593 /* As far as unification is concerned, this wins. Later checks
17594 will invalidate it if necessary. */
17595 return unify_success (explain_p
);
17597 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
17598 /* Type INTEGER_CST can come from ordinary constant template args. */
17600 while (TREE_CODE (arg
) == NOP_EXPR
)
17601 arg
= TREE_OPERAND (arg
, 0);
17603 if (TREE_CODE (arg
) != INTEGER_CST
)
17604 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17605 return (tree_int_cst_equal (parm
, arg
)
17606 ? unify_success (explain_p
)
17607 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
17611 int i
, len
, argslen
;
17612 int parm_variadic_p
= 0;
17614 if (TREE_CODE (arg
) != TREE_VEC
)
17615 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17617 len
= TREE_VEC_LENGTH (parm
);
17618 argslen
= TREE_VEC_LENGTH (arg
);
17620 /* Check for pack expansions in the parameters. */
17621 for (i
= 0; i
< len
; ++i
)
17623 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
17626 /* We can unify against something with a trailing
17628 parm_variadic_p
= 1;
17630 /* [temp.deduct.type]/9: If the template argument list of
17631 P contains a pack expansion that is not the last
17632 template argument, the entire template argument list
17633 is a non-deduced context. */
17634 return unify_success (explain_p
);
17638 /* If we don't have enough arguments to satisfy the parameters
17639 (not counting the pack expression at the end), or we have
17640 too many arguments for a parameter list that doesn't end in
17641 a pack expression, we can't unify. */
17642 if (parm_variadic_p
17643 ? argslen
< len
- parm_variadic_p
17645 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
17647 /* Unify all of the parameters that precede the (optional)
17648 pack expression. */
17649 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17651 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
17652 TREE_VEC_ELT (parm
, i
),
17653 TREE_VEC_ELT (arg
, i
),
17654 UNIFY_ALLOW_NONE
, explain_p
);
17656 if (parm_variadic_p
)
17657 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
17659 /*subr=*/true, explain_p
);
17660 return unify_success (explain_p
);
17665 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17666 return unify_type_mismatch (explain_p
, parm
, arg
);
17668 if (TYPE_PTRMEMFUNC_P (parm
))
17670 if (!TYPE_PTRMEMFUNC_P (arg
))
17671 return unify_type_mismatch (explain_p
, parm
, arg
);
17673 return unify (tparms
, targs
,
17674 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
17675 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
17676 strict
, explain_p
);
17679 if (CLASSTYPE_TEMPLATE_INFO (parm
))
17681 tree t
= NULL_TREE
;
17683 if (strict_in
& UNIFY_ALLOW_DERIVED
)
17685 /* First, we try to unify the PARM and ARG directly. */
17686 t
= try_class_unification (tparms
, targs
,
17687 parm
, arg
, explain_p
);
17691 /* Fallback to the special case allowed in
17692 [temp.deduct.call]:
17694 If P is a class, and P has the form
17695 template-id, then A can be a derived class of
17696 the deduced A. Likewise, if P is a pointer to
17697 a class of the form template-id, A can be a
17698 pointer to a derived class pointed to by the
17700 enum template_base_result r
;
17701 r
= get_template_base (tparms
, targs
, parm
, arg
,
17705 return unify_no_common_base (explain_p
, r
, parm
, arg
);
17708 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
17709 && (CLASSTYPE_TI_TEMPLATE (parm
)
17710 == CLASSTYPE_TI_TEMPLATE (arg
)))
17711 /* Perhaps PARM is something like S<U> and ARG is S<int>.
17712 Then, we should unify `int' and `U'. */
17715 /* There's no chance of unification succeeding. */
17716 return unify_type_mismatch (explain_p
, parm
, arg
);
17718 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
17719 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
17721 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
17722 return unify_type_mismatch (explain_p
, parm
, arg
);
17723 return unify_success (explain_p
);
17726 case FUNCTION_TYPE
:
17728 unsigned int nargs
;
17733 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17734 return unify_type_mismatch (explain_p
, parm
, arg
);
17736 /* CV qualifications for methods can never be deduced, they must
17737 match exactly. We need to check them explicitly here,
17738 because type_unification_real treats them as any other
17739 cv-qualified parameter. */
17740 if (TREE_CODE (parm
) == METHOD_TYPE
17741 && (!check_cv_quals_for_unify
17743 class_of_this_parm (arg
),
17744 class_of_this_parm (parm
))))
17745 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17747 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
17748 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
17750 nargs
= list_length (TYPE_ARG_TYPES (arg
));
17751 args
= XALLOCAVEC (tree
, nargs
);
17752 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
17753 a
!= NULL_TREE
&& a
!= void_list_node
;
17754 a
= TREE_CHAIN (a
), ++i
)
17755 args
[i
] = TREE_VALUE (a
);
17758 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
17759 args
, nargs
, 1, DEDUCE_EXACT
,
17760 LOOKUP_NORMAL
, NULL
, explain_p
);
17764 /* Unify a pointer to member with a pointer to member function, which
17765 deduces the type of the member as a function type. */
17766 if (TYPE_PTRMEMFUNC_P (arg
))
17768 /* Check top-level cv qualifiers */
17769 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
17770 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17772 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
17773 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
17774 UNIFY_ALLOW_NONE
, explain_p
);
17776 /* Determine the type of the function we are unifying against. */
17777 tree fntype
= static_fn_type (arg
);
17779 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
17782 if (TREE_CODE (arg
) != OFFSET_TYPE
)
17783 return unify_type_mismatch (explain_p
, parm
, arg
);
17784 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
17785 TYPE_OFFSET_BASETYPE (arg
),
17786 UNIFY_ALLOW_NONE
, explain_p
);
17787 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17788 strict
, explain_p
);
17791 if (DECL_TEMPLATE_PARM_P (parm
))
17792 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
17793 if (arg
!= integral_constant_value (parm
))
17794 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17795 return unify_success (explain_p
);
17798 case TEMPLATE_DECL
:
17799 /* Matched cases are handled by the ARG == PARM test above. */
17800 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17803 /* A non-type template parameter that is a variable should be a
17804 an integral constant, in which case, it whould have been
17805 folded into its (constant) value. So we should not be getting
17806 a variable here. */
17807 gcc_unreachable ();
17809 case TYPE_ARGUMENT_PACK
:
17810 case NONTYPE_ARGUMENT_PACK
:
17811 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
17812 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
17815 case DECLTYPE_TYPE
:
17816 case UNDERLYING_TYPE
:
17817 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
17818 or UNDERLYING_TYPE nodes. */
17819 return unify_success (explain_p
);
17822 /* Unification fails if we hit an error node. */
17823 return unify_invalid (explain_p
);
17826 /* An unresolved overload is a nondeduced context. */
17827 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
17828 return unify_success (explain_p
);
17829 gcc_assert (EXPR_P (parm
));
17831 /* We must be looking at an expression. This can happen with
17835 void foo(S<I>, S<I + 2>);
17837 This is a "nondeduced context":
17841 The nondeduced contexts are:
17843 --A type that is a template-id in which one or more of
17844 the template-arguments is an expression that references
17845 a template-parameter.
17847 In these cases, we assume deduction succeeded, but don't
17848 actually infer any unifications. */
17850 if (!uses_template_parms (parm
)
17851 && !template_args_equal (parm
, arg
))
17852 return unify_expression_unequal (explain_p
, parm
, arg
);
17854 return unify_success (explain_p
);
17857 #undef RECUR_AND_CHECK_FAILURE
17859 /* Note that DECL can be defined in this translation unit, if
17863 mark_definable (tree decl
)
17866 DECL_NOT_REALLY_EXTERN (decl
) = 1;
17867 FOR_EACH_CLONE (clone
, decl
)
17868 DECL_NOT_REALLY_EXTERN (clone
) = 1;
17871 /* Called if RESULT is explicitly instantiated, or is a member of an
17872 explicitly instantiated class. */
17875 mark_decl_instantiated (tree result
, int extern_p
)
17877 SET_DECL_EXPLICIT_INSTANTIATION (result
);
17879 /* If this entity has already been written out, it's too late to
17880 make any modifications. */
17881 if (TREE_ASM_WRITTEN (result
))
17884 /* For anonymous namespace we don't need to do anything. */
17885 if (decl_anon_ns_mem_p (result
))
17887 gcc_assert (!TREE_PUBLIC (result
));
17891 if (TREE_CODE (result
) != FUNCTION_DECL
)
17892 /* The TREE_PUBLIC flag for function declarations will have been
17893 set correctly by tsubst. */
17894 TREE_PUBLIC (result
) = 1;
17896 /* This might have been set by an earlier implicit instantiation. */
17897 DECL_COMDAT (result
) = 0;
17900 DECL_NOT_REALLY_EXTERN (result
) = 0;
17903 mark_definable (result
);
17904 /* Always make artificials weak. */
17905 if (DECL_ARTIFICIAL (result
) && flag_weak
)
17906 comdat_linkage (result
);
17907 /* For WIN32 we also want to put explicit instantiations in
17908 linkonce sections. */
17909 else if (TREE_PUBLIC (result
))
17910 maybe_make_one_only (result
);
17913 /* If EXTERN_P, then this function will not be emitted -- unless
17914 followed by an explicit instantiation, at which point its linkage
17915 will be adjusted. If !EXTERN_P, then this function will be
17916 emitted here. In neither circumstance do we want
17917 import_export_decl to adjust the linkage. */
17918 DECL_INTERFACE_KNOWN (result
) = 1;
17921 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17922 important template arguments. If any are missing, we check whether
17923 they're important by using error_mark_node for substituting into any
17924 args that were used for partial ordering (the ones between ARGS and END)
17925 and seeing if it bubbles up. */
17928 check_undeduced_parms (tree targs
, tree args
, tree end
)
17930 bool found
= false;
17932 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
17933 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
17936 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
17940 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
17941 if (substed
== error_mark_node
)
17947 /* Given two function templates PAT1 and PAT2, return:
17949 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17950 -1 if PAT2 is more specialized than PAT1.
17951 0 if neither is more specialized.
17953 LEN indicates the number of parameters we should consider
17954 (defaulted parameters should not be considered).
17956 The 1998 std underspecified function template partial ordering, and
17957 DR214 addresses the issue. We take pairs of arguments, one from
17958 each of the templates, and deduce them against each other. One of
17959 the templates will be more specialized if all the *other*
17960 template's arguments deduce against its arguments and at least one
17961 of its arguments *does* *not* deduce against the other template's
17962 corresponding argument. Deduction is done as for class templates.
17963 The arguments used in deduction have reference and top level cv
17964 qualifiers removed. Iff both arguments were originally reference
17965 types *and* deduction succeeds in both directions, an lvalue reference
17966 wins against an rvalue reference and otherwise the template
17967 with the more cv-qualified argument wins for that pairing (if
17968 neither is more cv-qualified, they both are equal). Unlike regular
17969 deduction, after all the arguments have been deduced in this way,
17970 we do *not* verify the deduced template argument values can be
17971 substituted into non-deduced contexts.
17973 The logic can be a bit confusing here, because we look at deduce1 and
17974 targs1 to see if pat2 is at least as specialized, and vice versa; if we
17975 can find template arguments for pat1 to make arg1 look like arg2, that
17976 means that arg2 is at least as specialized as arg1. */
17979 more_specialized_fn (tree pat1
, tree pat2
, int len
)
17981 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
17982 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
17983 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
17984 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
17985 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
17986 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
17987 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
17988 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
17989 tree origs1
, origs2
;
17990 bool lose1
= false;
17991 bool lose2
= false;
17993 /* Remove the this parameter from non-static member functions. If
17994 one is a non-static member function and the other is not a static
17995 member function, remove the first parameter from that function
17996 also. This situation occurs for operator functions where we
17997 locate both a member function (with this pointer) and non-member
17998 operator (with explicit first operand). */
17999 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
18001 len
--; /* LEN is the number of significant arguments for DECL1 */
18002 args1
= TREE_CHAIN (args1
);
18003 if (!DECL_STATIC_FUNCTION_P (decl2
))
18004 args2
= TREE_CHAIN (args2
);
18006 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
18008 args2
= TREE_CHAIN (args2
);
18009 if (!DECL_STATIC_FUNCTION_P (decl1
))
18012 args1
= TREE_CHAIN (args1
);
18016 /* If only one is a conversion operator, they are unordered. */
18017 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
18020 /* Consider the return type for a conversion function */
18021 if (DECL_CONV_FN_P (decl1
))
18023 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
18024 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
18028 processing_template_decl
++;
18034 /* Stop when an ellipsis is seen. */
18035 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
18037 tree arg1
= TREE_VALUE (args1
);
18038 tree arg2
= TREE_VALUE (args2
);
18039 int deduce1
, deduce2
;
18045 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
18046 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18048 /* When both arguments are pack expansions, we need only
18049 unify the patterns themselves. */
18050 arg1
= PACK_EXPANSION_PATTERN (arg1
);
18051 arg2
= PACK_EXPANSION_PATTERN (arg2
);
18053 /* This is the last comparison we need to do. */
18057 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
18059 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
18060 arg1
= TREE_TYPE (arg1
);
18061 quals1
= cp_type_quals (arg1
);
18064 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
18066 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
18067 arg2
= TREE_TYPE (arg2
);
18068 quals2
= cp_type_quals (arg2
);
18071 arg1
= TYPE_MAIN_VARIANT (arg1
);
18072 arg2
= TYPE_MAIN_VARIANT (arg2
);
18074 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
18076 int i
, len2
= list_length (args2
);
18077 tree parmvec
= make_tree_vec (1);
18078 tree argvec
= make_tree_vec (len2
);
18081 /* Setup the parameter vector, which contains only ARG1. */
18082 TREE_VEC_ELT (parmvec
, 0) = arg1
;
18084 /* Setup the argument vector, which contains the remaining
18086 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
18087 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
18089 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
18090 argvec
, DEDUCE_EXACT
,
18091 /*subr=*/true, /*explain_p=*/false)
18094 /* We cannot deduce in the other direction, because ARG1 is
18095 a pack expansion but ARG2 is not. */
18098 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18100 int i
, len1
= list_length (args1
);
18101 tree parmvec
= make_tree_vec (1);
18102 tree argvec
= make_tree_vec (len1
);
18105 /* Setup the parameter vector, which contains only ARG1. */
18106 TREE_VEC_ELT (parmvec
, 0) = arg2
;
18108 /* Setup the argument vector, which contains the remaining
18110 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
18111 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
18113 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
18114 argvec
, DEDUCE_EXACT
,
18115 /*subr=*/true, /*explain_p=*/false)
18118 /* We cannot deduce in the other direction, because ARG2 is
18119 a pack expansion but ARG1 is not.*/
18125 /* The normal case, where neither argument is a pack
18127 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
18128 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
18130 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
18131 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
18135 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18136 arg2, then arg2 is not as specialized as arg1. */
18142 /* "If, for a given type, deduction succeeds in both directions
18143 (i.e., the types are identical after the transformations above)
18144 and both P and A were reference types (before being replaced with
18145 the type referred to above):
18146 - if the type from the argument template was an lvalue reference and
18147 the type from the parameter template was not, the argument type is
18148 considered to be more specialized than the other; otherwise,
18149 - if the type from the argument template is more cv-qualified
18150 than the type from the parameter template (as described above),
18151 the argument type is considered to be more specialized than the other;
18153 - neither type is more specialized than the other." */
18155 if (deduce1
&& deduce2
)
18157 if (ref1
&& ref2
&& ref1
!= ref2
)
18164 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
18166 if ((quals1
& quals2
) == quals2
)
18168 if ((quals1
& quals2
) == quals1
)
18173 if (lose1
&& lose2
)
18174 /* We've failed to deduce something in either direction.
18175 These must be unordered. */
18178 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
18179 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
18180 /* We have already processed all of the arguments in our
18181 handing of the pack expansion type. */
18184 args1
= TREE_CHAIN (args1
);
18185 args2
= TREE_CHAIN (args2
);
18188 /* "In most cases, all template parameters must have values in order for
18189 deduction to succeed, but for partial ordering purposes a template
18190 parameter may remain without a value provided it is not used in the
18191 types being used for partial ordering."
18193 Thus, if we are missing any of the targs1 we need to substitute into
18194 origs1, then pat2 is not as specialized as pat1. This can happen when
18195 there is a nondeduced context. */
18196 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
18198 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
18201 processing_template_decl
--;
18203 /* All things being equal, if the next argument is a pack expansion
18204 for one function but not for the other, prefer the
18205 non-variadic function. FIXME this is bogus; see c++/41958. */
18207 && args1
&& TREE_VALUE (args1
)
18208 && args2
&& TREE_VALUE (args2
))
18210 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
18211 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
18214 if (lose1
== lose2
)
18222 /* Determine which of two partial specializations of MAIN_TMPL is more
18225 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18226 to the first partial specialization. The TREE_VALUE is the
18227 innermost set of template parameters for the partial
18228 specialization. PAT2 is similar, but for the second template.
18230 Return 1 if the first partial specialization is more specialized;
18231 -1 if the second is more specialized; 0 if neither is more
18234 See [temp.class.order] for information about determining which of
18235 two templates is more specialized. */
18238 more_specialized_class (tree main_tmpl
, tree pat1
, tree pat2
)
18243 bool any_deductions
= false;
18245 tmpl1
= TREE_TYPE (pat1
);
18246 tmpl2
= TREE_TYPE (pat2
);
18248 /* Just like what happens for functions, if we are ordering between
18249 different class template specializations, we may encounter dependent
18250 types in the arguments, and we need our dependency check functions
18251 to behave correctly. */
18252 ++processing_template_decl
;
18253 targs
= get_class_bindings (main_tmpl
, TREE_VALUE (pat1
),
18254 CLASSTYPE_TI_ARGS (tmpl1
),
18255 CLASSTYPE_TI_ARGS (tmpl2
));
18259 any_deductions
= true;
18262 targs
= get_class_bindings (main_tmpl
, TREE_VALUE (pat2
),
18263 CLASSTYPE_TI_ARGS (tmpl2
),
18264 CLASSTYPE_TI_ARGS (tmpl1
));
18268 any_deductions
= true;
18270 --processing_template_decl
;
18272 /* In the case of a tie where at least one of the class templates
18273 has a parameter pack at the end, the template with the most
18274 non-packed parameters wins. */
18277 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
18278 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
18280 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
18281 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
18282 int len1
= TREE_VEC_LENGTH (args1
);
18283 int len2
= TREE_VEC_LENGTH (args2
);
18285 /* We don't count the pack expansion at the end. */
18286 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
18288 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
18293 else if (len1
< len2
)
18300 /* Return the template arguments that will produce the function signature
18301 DECL from the function template FN, with the explicit template
18302 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18303 also match. Return NULL_TREE if no satisfactory arguments could be
18307 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
18309 int ntparms
= DECL_NTPARMS (fn
);
18310 tree targs
= make_tree_vec (ntparms
);
18311 tree decl_type
= TREE_TYPE (decl
);
18312 tree decl_arg_types
;
18314 unsigned int nargs
, ix
;
18317 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
18319 /* Never do unification on the 'this' parameter. */
18320 decl_arg_types
= skip_artificial_parms_for (decl
,
18321 TYPE_ARG_TYPES (decl_type
));
18323 nargs
= list_length (decl_arg_types
);
18324 args
= XALLOCAVEC (tree
, nargs
);
18325 for (arg
= decl_arg_types
, ix
= 0;
18326 arg
!= NULL_TREE
&& arg
!= void_list_node
;
18327 arg
= TREE_CHAIN (arg
), ++ix
)
18328 args
[ix
] = TREE_VALUE (arg
);
18330 if (fn_type_unification (fn
, explicit_args
, targs
,
18332 (check_rettype
|| DECL_CONV_FN_P (fn
)
18333 ? TREE_TYPE (decl_type
) : NULL_TREE
),
18334 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
18336 == error_mark_node
)
18342 /* Return the innermost template arguments that, when applied to a partial
18343 specialization of MAIN_TMPL whose innermost template parameters are
18344 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
18347 For example, suppose we have:
18349 template <class T, class U> struct S {};
18350 template <class T> struct S<T*, int> {};
18352 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
18353 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
18354 int}. The resulting vector will be {double}, indicating that `T'
18355 is bound to `double'. */
18358 get_class_bindings (tree main_tmpl
, tree tparms
, tree spec_args
, tree args
)
18360 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
18362 tree innermost_deduced_args
;
18364 innermost_deduced_args
= make_tree_vec (ntparms
);
18365 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
18367 deduced_args
= copy_node (args
);
18368 SET_TMPL_ARGS_LEVEL (deduced_args
,
18369 TMPL_ARGS_DEPTH (deduced_args
),
18370 innermost_deduced_args
);
18373 deduced_args
= innermost_deduced_args
;
18375 if (unify (tparms
, deduced_args
,
18376 INNERMOST_TEMPLATE_ARGS (spec_args
),
18377 INNERMOST_TEMPLATE_ARGS (args
),
18378 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
18381 for (i
= 0; i
< ntparms
; ++i
)
18382 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
18385 /* Verify that nondeduced template arguments agree with the type
18386 obtained from argument deduction.
18390 struct A { typedef int X; };
18391 template <class T, class U> struct C {};
18392 template <class T> struct C<T, typename T::X> {};
18394 Then with the instantiation `C<A, int>', we can deduce that
18395 `T' is `A' but unify () does not check whether `typename T::X'
18397 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
18398 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (main_tmpl
),
18399 spec_args
, main_tmpl
,
18400 tf_none
, false, false);
18401 if (spec_args
== error_mark_node
18402 /* We only need to check the innermost arguments; the other
18403 arguments will always agree. */
18404 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
18405 INNERMOST_TEMPLATE_ARGS (args
)))
18408 /* Now that we have bindings for all of the template arguments,
18409 ensure that the arguments deduced for the template template
18410 parameters have compatible template parameter lists. See the use
18411 of template_template_parm_bindings_ok_p in fn_type_unification
18412 for more information. */
18413 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
18416 return deduced_args
;
18419 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
18420 Return the TREE_LIST node with the most specialized template, if
18421 any. If there is no most specialized template, the error_mark_node
18424 Note that this function does not look at, or modify, the
18425 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
18426 returned is one of the elements of INSTANTIATIONS, callers may
18427 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
18428 and retrieve it from the value returned. */
18431 most_specialized_instantiation (tree templates
)
18435 ++processing_template_decl
;
18438 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
18442 if (get_bindings (TREE_VALUE (champ
),
18443 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
18444 NULL_TREE
, /*check_ret=*/true))
18447 if (get_bindings (TREE_VALUE (fn
),
18448 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
18449 NULL_TREE
, /*check_ret=*/true))
18456 /* Equally specialized, move to next function. If there
18457 is no next function, nothing's most specialized. */
18458 fn
= TREE_CHAIN (fn
);
18466 /* Now verify that champ is better than everything earlier in the
18467 instantiation list. */
18468 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
18469 if (get_bindings (TREE_VALUE (champ
),
18470 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
18471 NULL_TREE
, /*check_ret=*/true)
18472 || !get_bindings (TREE_VALUE (fn
),
18473 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
18474 NULL_TREE
, /*check_ret=*/true))
18480 processing_template_decl
--;
18483 return error_mark_node
;
18488 /* If DECL is a specialization of some template, return the most
18489 general such template. Otherwise, returns NULL_TREE.
18491 For example, given:
18493 template <class T> struct S { template <class U> void f(U); };
18495 if TMPL is `template <class U> void S<int>::f(U)' this will return
18496 the full template. This function will not trace past partial
18497 specializations, however. For example, given in addition:
18499 template <class T> struct S<T*> { template <class U> void f(U); };
18501 if TMPL is `template <class U> void S<int*>::f(U)' this will return
18502 `template <class T> template <class U> S<T*>::f(U)'. */
18505 most_general_template (tree decl
)
18507 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
18508 an immediate specialization. */
18509 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18511 if (DECL_TEMPLATE_INFO (decl
)) {
18512 decl
= DECL_TI_TEMPLATE (decl
);
18514 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
18515 template friend. */
18516 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
18522 /* Look for more and more general templates. */
18523 while (DECL_TEMPLATE_INFO (decl
))
18525 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
18526 (See cp-tree.h for details.) */
18527 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
18530 if (CLASS_TYPE_P (TREE_TYPE (decl
))
18531 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
18534 /* Stop if we run into an explicitly specialized class template. */
18535 if (!DECL_NAMESPACE_SCOPE_P (decl
)
18536 && DECL_CONTEXT (decl
)
18537 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
18540 decl
= DECL_TI_TEMPLATE (decl
);
18546 /* Return the most specialized of the class template partial
18547 specializations of TMPL which can produce TYPE, a specialization of
18548 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
18549 a _TYPE node corresponding to the partial specialization, while the
18550 TREE_PURPOSE is the set of template arguments that must be
18551 substituted into the TREE_TYPE in order to generate TYPE.
18553 If the choice of partial specialization is ambiguous, a diagnostic
18554 is issued, and the error_mark_node is returned. If there are no
18555 partial specializations of TMPL matching TYPE, then NULL_TREE is
18559 most_specialized_class (tree type
, tree tmpl
, tsubst_flags_t complain
)
18561 tree list
= NULL_TREE
;
18567 tree outer_args
= NULL_TREE
;
18569 tmpl
= most_general_template (tmpl
);
18570 args
= CLASSTYPE_TI_ARGS (type
);
18572 /* For determining which partial specialization to use, only the
18573 innermost args are interesting. */
18574 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
18576 outer_args
= strip_innermost_template_args (args
, 1);
18577 args
= INNERMOST_TEMPLATE_ARGS (args
);
18580 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
18582 tree partial_spec_args
;
18584 tree spec_tmpl
= TREE_VALUE (t
);
18585 tree orig_parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
18587 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
18589 ++processing_template_decl
;
18593 /* Discard the outer levels of args, and then substitute in the
18594 template args from the enclosing class. */
18595 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
18596 partial_spec_args
= tsubst_template_args
18597 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
18599 /* And the same for the partial specialization TEMPLATE_DECL. */
18600 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
18603 partial_spec_args
=
18604 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
18605 add_to_template_args (outer_args
,
18606 partial_spec_args
),
18608 /*require_all_args=*/true,
18609 /*use_default_args=*/true);
18611 --processing_template_decl
;
18613 if (partial_spec_args
== error_mark_node
)
18614 return error_mark_node
;
18615 if (spec_tmpl
== error_mark_node
)
18616 return error_mark_node
;
18618 tree parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
18619 spec_args
= get_class_bindings (tmpl
, parms
,
18625 spec_args
= add_to_template_args (outer_args
, spec_args
);
18626 list
= tree_cons (spec_args
, orig_parms
, list
);
18627 TREE_TYPE (list
) = TREE_TYPE (t
);
18634 ambiguous_p
= false;
18637 t
= TREE_CHAIN (t
);
18638 for (; t
; t
= TREE_CHAIN (t
))
18640 fate
= more_specialized_class (tmpl
, champ
, t
);
18647 t
= TREE_CHAIN (t
);
18650 ambiguous_p
= true;
18659 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
18661 fate
= more_specialized_class (tmpl
, champ
, t
);
18664 ambiguous_p
= true;
18672 char *spaces
= NULL
;
18673 if (!(complain
& tf_error
))
18674 return error_mark_node
;
18675 error ("ambiguous class template instantiation for %q#T", type
);
18676 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
18677 for (t
= list
; t
; t
= TREE_CHAIN (t
))
18679 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
18680 spaces
= spaces
? spaces
: get_spaces (str
);
18683 return error_mark_node
;
18689 /* Explicitly instantiate DECL. */
18692 do_decl_instantiation (tree decl
, tree storage
)
18694 tree result
= NULL_TREE
;
18697 if (!decl
|| decl
== error_mark_node
)
18698 /* An error occurred, for which grokdeclarator has already issued
18699 an appropriate message. */
18701 else if (! DECL_LANG_SPECIFIC (decl
))
18703 error ("explicit instantiation of non-template %q#D", decl
);
18706 else if (VAR_P (decl
))
18708 /* There is an asymmetry here in the way VAR_DECLs and
18709 FUNCTION_DECLs are handled by grokdeclarator. In the case of
18710 the latter, the DECL we get back will be marked as a
18711 template instantiation, and the appropriate
18712 DECL_TEMPLATE_INFO will be set up. This does not happen for
18713 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
18714 should handle VAR_DECLs as it currently handles
18716 if (!DECL_CLASS_SCOPE_P (decl
))
18718 error ("%qD is not a static data member of a class template", decl
);
18721 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
18722 if (!result
|| !VAR_P (result
))
18724 error ("no matching template for %qD found", decl
);
18727 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
18729 error ("type %qT for explicit instantiation %qD does not match "
18730 "declared type %qT", TREE_TYPE (result
), decl
,
18735 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
18737 error ("explicit instantiation of %q#D", decl
);
18743 /* Check for various error cases. Note that if the explicit
18744 instantiation is valid the RESULT will currently be marked as an
18745 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
18746 until we get here. */
18748 if (DECL_TEMPLATE_SPECIALIZATION (result
))
18750 /* DR 259 [temp.spec].
18752 Both an explicit instantiation and a declaration of an explicit
18753 specialization shall not appear in a program unless the explicit
18754 instantiation follows a declaration of the explicit specialization.
18756 For a given set of template parameters, if an explicit
18757 instantiation of a template appears after a declaration of an
18758 explicit specialization for that template, the explicit
18759 instantiation has no effect. */
18762 else if (DECL_EXPLICIT_INSTANTIATION (result
))
18766 No program shall explicitly instantiate any template more
18769 We check DECL_NOT_REALLY_EXTERN so as not to complain when
18770 the first instantiation was `extern' and the second is not,
18771 and EXTERN_P for the opposite case. */
18772 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
18773 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
18774 /* If an "extern" explicit instantiation follows an ordinary
18775 explicit instantiation, the template is instantiated. */
18779 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
18781 error ("no matching template for %qD found", result
);
18784 else if (!DECL_TEMPLATE_INFO (result
))
18786 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
18790 if (storage
== NULL_TREE
)
18792 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18794 if (!in_system_header
&& (cxx_dialect
== cxx98
))
18795 pedwarn (input_location
, OPT_Wpedantic
,
18796 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
18801 error ("storage class %qD applied to template instantiation", storage
);
18803 check_explicit_instantiation_namespace (result
);
18804 mark_decl_instantiated (result
, extern_p
);
18806 instantiate_decl (result
, /*defer_ok=*/1,
18807 /*expl_inst_class_mem_p=*/false);
18811 mark_class_instantiated (tree t
, int extern_p
)
18813 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
18814 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
18815 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
18816 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
18819 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
18820 rest_of_type_compilation (t
, 1);
18824 /* Called from do_type_instantiation through binding_table_foreach to
18825 do recursive instantiation for the type bound in ENTRY. */
18827 bt_instantiate_type_proc (binding_entry entry
, void *data
)
18829 tree storage
= *(tree
*) data
;
18831 if (MAYBE_CLASS_TYPE_P (entry
->type
)
18832 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
18833 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
18836 /* Called from do_type_instantiation to instantiate a member
18837 (a member function or a static member variable) of an
18838 explicitly instantiated class template. */
18840 instantiate_class_member (tree decl
, int extern_p
)
18842 mark_decl_instantiated (decl
, extern_p
);
18844 instantiate_decl (decl
, /*defer_ok=*/1,
18845 /*expl_inst_class_mem_p=*/true);
18848 /* Perform an explicit instantiation of template class T. STORAGE, if
18849 non-null, is the RID for extern, inline or static. COMPLAIN is
18850 nonzero if this is called from the parser, zero if called recursively,
18851 since the standard is unclear (as detailed below). */
18854 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
18859 int previous_instantiation_extern_p
= 0;
18861 if (TREE_CODE (t
) == TYPE_DECL
)
18864 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
18867 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
18869 error ("explicit instantiation of non-class template %qD", tmpl
);
18871 error ("explicit instantiation of non-template type %qT", t
);
18877 if (!COMPLETE_TYPE_P (t
))
18879 if (complain
& tf_error
)
18880 error ("explicit instantiation of %q#T before definition of template",
18885 if (storage
!= NULL_TREE
)
18887 if (!in_system_header
)
18889 if (storage
== ridpointers
[(int) RID_EXTERN
])
18891 if (cxx_dialect
== cxx98
)
18892 pedwarn (input_location
, OPT_Wpedantic
,
18893 "ISO C++ 1998 forbids the use of %<extern%> on "
18894 "explicit instantiations");
18897 pedwarn (input_location
, OPT_Wpedantic
,
18898 "ISO C++ forbids the use of %qE"
18899 " on explicit instantiations", storage
);
18902 if (storage
== ridpointers
[(int) RID_INLINE
])
18904 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18906 else if (storage
== ridpointers
[(int) RID_STATIC
])
18910 error ("storage class %qD applied to template instantiation",
18916 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
18918 /* DR 259 [temp.spec].
18920 Both an explicit instantiation and a declaration of an explicit
18921 specialization shall not appear in a program unless the explicit
18922 instantiation follows a declaration of the explicit specialization.
18924 For a given set of template parameters, if an explicit
18925 instantiation of a template appears after a declaration of an
18926 explicit specialization for that template, the explicit
18927 instantiation has no effect. */
18930 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
18934 No program shall explicitly instantiate any template more
18937 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18938 instantiation was `extern'. If EXTERN_P then the second is.
18939 These cases are OK. */
18940 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
18942 if (!previous_instantiation_extern_p
&& !extern_p
18943 && (complain
& tf_error
))
18944 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
18946 /* If we've already instantiated the template, just return now. */
18947 if (!CLASSTYPE_INTERFACE_ONLY (t
))
18951 check_explicit_instantiation_namespace (TYPE_NAME (t
));
18952 mark_class_instantiated (t
, extern_p
);
18960 /* In contrast to implicit instantiation, where only the
18961 declarations, and not the definitions, of members are
18962 instantiated, we have here:
18966 The explicit instantiation of a class template specialization
18967 implies the instantiation of all of its members not
18968 previously explicitly specialized in the translation unit
18969 containing the explicit instantiation.
18971 Of course, we can't instantiate member template classes, since
18972 we don't have any arguments for them. Note that the standard
18973 is unclear on whether the instantiation of the members are
18974 *explicit* instantiations or not. However, the most natural
18975 interpretation is that it should be an explicit instantiation. */
18978 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18979 if (TREE_CODE (tmp
) == FUNCTION_DECL
18980 && DECL_TEMPLATE_INSTANTIATION (tmp
))
18981 instantiate_class_member (tmp
, extern_p
);
18983 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18984 if (VAR_P (tmp
) && DECL_TEMPLATE_INSTANTIATION (tmp
))
18985 instantiate_class_member (tmp
, extern_p
);
18987 if (CLASSTYPE_NESTED_UTDS (t
))
18988 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
18989 bt_instantiate_type_proc
, &storage
);
18993 /* Given a function DECL, which is a specialization of TMPL, modify
18994 DECL to be a re-instantiation of TMPL with the same template
18995 arguments. TMPL should be the template into which tsubst'ing
18996 should occur for DECL, not the most general template.
18998 One reason for doing this is a scenario like this:
19001 void f(const T&, int i);
19003 void g() { f(3, 7); }
19006 void f(const T& t, const int i) { }
19008 Note that when the template is first instantiated, with
19009 instantiate_template, the resulting DECL will have no name for the
19010 first parameter, and the wrong type for the second. So, when we go
19011 to instantiate the DECL, we regenerate it. */
19014 regenerate_decl_from_template (tree decl
, tree tmpl
)
19016 /* The arguments used to instantiate DECL, from the most general
19021 args
= DECL_TI_ARGS (decl
);
19022 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
19024 /* Make sure that we can see identifiers, and compute access
19026 push_access_scope (decl
);
19028 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19036 args_depth
= TMPL_ARGS_DEPTH (args
);
19037 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
19038 if (args_depth
> parms_depth
)
19039 args
= get_innermost_template_args (args
, parms_depth
);
19041 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
19042 args
, tf_error
, NULL_TREE
,
19043 /*defer_ok*/false);
19044 if (specs
&& specs
!= error_mark_node
)
19045 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
19048 /* Merge parameter declarations. */
19049 decl_parm
= skip_artificial_parms_for (decl
,
19050 DECL_ARGUMENTS (decl
));
19052 = skip_artificial_parms_for (code_pattern
,
19053 DECL_ARGUMENTS (code_pattern
));
19054 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
19059 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
19060 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
19061 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
19063 parm_type
= type_decays_to (parm_type
);
19064 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
19065 TREE_TYPE (decl_parm
) = parm_type
;
19066 attributes
= DECL_ATTRIBUTES (pattern_parm
);
19067 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
19069 DECL_ATTRIBUTES (decl_parm
) = attributes
;
19070 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
19072 decl_parm
= DECL_CHAIN (decl_parm
);
19073 pattern_parm
= DECL_CHAIN (pattern_parm
);
19075 /* Merge any parameters that match with the function parameter
19077 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
19080 tree expanded_types
;
19081 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19082 the parameters in this function parameter pack. */
19083 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
19084 args
, tf_error
, NULL_TREE
);
19085 len
= TREE_VEC_LENGTH (expanded_types
);
19086 for (i
= 0; i
< len
; i
++)
19091 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
19092 /* Rename the parameter to include the index. */
19093 DECL_NAME (decl_parm
) =
19094 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
19095 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
19096 parm_type
= type_decays_to (parm_type
);
19097 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
19098 TREE_TYPE (decl_parm
) = parm_type
;
19099 attributes
= DECL_ATTRIBUTES (pattern_parm
);
19100 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
19102 DECL_ATTRIBUTES (decl_parm
) = attributes
;
19103 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
19105 decl_parm
= DECL_CHAIN (decl_parm
);
19108 /* Merge additional specifiers from the CODE_PATTERN. */
19109 if (DECL_DECLARED_INLINE_P (code_pattern
)
19110 && !DECL_DECLARED_INLINE_P (decl
))
19111 DECL_DECLARED_INLINE_P (decl
) = 1;
19113 else if (VAR_P (decl
))
19115 DECL_INITIAL (decl
) =
19116 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
19117 tf_error
, DECL_TI_TEMPLATE (decl
),
19118 /*integral_constant_expression_p=*/false);
19119 if (VAR_HAD_UNKNOWN_BOUND (decl
))
19120 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
19121 tf_error
, DECL_TI_TEMPLATE (decl
));
19124 gcc_unreachable ();
19126 pop_access_scope (decl
);
19129 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19130 substituted to get DECL. */
19133 template_for_substitution (tree decl
)
19135 tree tmpl
= DECL_TI_TEMPLATE (decl
);
19137 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19138 for the instantiation. This is not always the most general
19139 template. Consider, for example:
19142 struct S { template <class U> void f();
19143 template <> void f<int>(); };
19145 and an instantiation of S<double>::f<int>. We want TD to be the
19146 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19147 while (/* An instantiation cannot have a definition, so we need a
19148 more general template. */
19149 DECL_TEMPLATE_INSTANTIATION (tmpl
)
19150 /* We must also deal with friend templates. Given:
19152 template <class T> struct S {
19153 template <class U> friend void f() {};
19156 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19157 so far as the language is concerned, but that's still
19158 where we get the pattern for the instantiation from. On
19159 other hand, if the definition comes outside the class, say:
19161 template <class T> struct S {
19162 template <class U> friend void f();
19164 template <class U> friend void f() {}
19166 we don't need to look any further. That's what the check for
19167 DECL_INITIAL is for. */
19168 || (TREE_CODE (decl
) == FUNCTION_DECL
19169 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
19170 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
19172 /* The present template, TD, should not be a definition. If it
19173 were a definition, we should be using it! Note that we
19174 cannot restructure the loop to just keep going until we find
19175 a template with a definition, since that might go too far if
19176 a specialization was declared, but not defined. */
19177 gcc_assert (!VAR_P (decl
)
19178 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
19180 /* Fetch the more general template. */
19181 tmpl
= DECL_TI_TEMPLATE (tmpl
);
19187 /* Returns true if we need to instantiate this template instance even if we
19188 know we aren't going to emit it.. */
19191 always_instantiate_p (tree decl
)
19193 /* We always instantiate inline functions so that we can inline them. An
19194 explicit instantiation declaration prohibits implicit instantiation of
19195 non-inline functions. With high levels of optimization, we would
19196 normally inline non-inline functions -- but we're not allowed to do
19197 that for "extern template" functions. Therefore, we check
19198 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19199 return ((TREE_CODE (decl
) == FUNCTION_DECL
19200 && (DECL_DECLARED_INLINE_P (decl
)
19201 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
19202 /* And we need to instantiate static data members so that
19203 their initializers are available in integral constant
19206 && decl_maybe_constant_var_p (decl
)));
19209 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19210 instantiate it now, modifying TREE_TYPE (fn). */
19213 maybe_instantiate_noexcept (tree fn
)
19215 tree fntype
, spec
, noex
, clone
;
19217 if (DECL_CLONED_FUNCTION_P (fn
))
19218 fn
= DECL_CLONED_FUNCTION (fn
);
19219 fntype
= TREE_TYPE (fn
);
19220 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
19222 if (!DEFERRED_NOEXCEPT_SPEC_P (spec
))
19225 noex
= TREE_PURPOSE (spec
);
19227 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
19229 if (push_tinst_level (fn
))
19231 push_access_scope (fn
);
19232 push_deferring_access_checks (dk_no_deferred
);
19233 input_location
= DECL_SOURCE_LOCATION (fn
);
19234 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
19235 DEFERRED_NOEXCEPT_ARGS (noex
),
19236 tf_warning_or_error
, fn
,
19237 /*function_p=*/false,
19238 /*integral_constant_expression_p=*/true);
19239 pop_deferring_access_checks ();
19240 pop_access_scope (fn
);
19241 pop_tinst_level ();
19242 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
19243 if (spec
== error_mark_node
)
19244 spec
= noexcept_false_spec
;
19247 spec
= noexcept_false_spec
;
19251 /* This is an implicitly declared function, so NOEX is a list of
19252 other functions to evaluate and merge. */
19254 spec
= noexcept_true_spec
;
19255 for (elt
= noex
; elt
; elt
= OVL_NEXT (elt
))
19257 tree fn
= OVL_CURRENT (elt
);
19259 maybe_instantiate_noexcept (fn
);
19260 subspec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
19261 spec
= merge_exception_specifiers (spec
, subspec
, NULL_TREE
);
19265 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
19267 FOR_EACH_CLONE (clone
, fn
)
19269 if (TREE_TYPE (clone
) == fntype
)
19270 TREE_TYPE (clone
) = TREE_TYPE (fn
);
19272 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
19276 /* Produce the definition of D, a _DECL generated from a template. If
19277 DEFER_OK is nonzero, then we don't have to actually do the
19278 instantiation now; we just have to do it sometime. Normally it is
19279 an error if this is an explicit instantiation but D is undefined.
19280 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19281 explicitly instantiated class template. */
19284 instantiate_decl (tree d
, int defer_ok
,
19285 bool expl_inst_class_mem_p
)
19287 tree tmpl
= DECL_TI_TEMPLATE (d
);
19294 bool pattern_defined
;
19295 location_t saved_loc
= input_location
;
19296 int saved_unevaluated_operand
= cp_unevaluated_operand
;
19297 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
19302 /* This function should only be used to instantiate templates for
19303 functions and static member variables. */
19304 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
19306 /* Variables are never deferred; if instantiation is required, they
19307 are instantiated right away. That allows for better code in the
19308 case that an expression refers to the value of the variable --
19309 if the variable has a constant value the referring expression can
19310 take advantage of that fact. */
19312 || DECL_DECLARED_CONSTEXPR_P (d
))
19315 /* Don't instantiate cloned functions. Instead, instantiate the
19316 functions they cloned. */
19317 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
19318 d
= DECL_CLONED_FUNCTION (d
);
19320 if (DECL_TEMPLATE_INSTANTIATED (d
)
19321 || (TREE_CODE (d
) == FUNCTION_DECL
19322 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
19323 || DECL_TEMPLATE_SPECIALIZATION (d
))
19324 /* D has already been instantiated or explicitly specialized, so
19325 there's nothing for us to do here.
19327 It might seem reasonable to check whether or not D is an explicit
19328 instantiation, and, if so, stop here. But when an explicit
19329 instantiation is deferred until the end of the compilation,
19330 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
19331 the instantiation. */
19334 /* Check to see whether we know that this template will be
19335 instantiated in some other file, as with "extern template"
19337 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
19339 /* In general, we do not instantiate such templates. */
19340 if (external_p
&& !always_instantiate_p (d
))
19343 gen_tmpl
= most_general_template (tmpl
);
19344 gen_args
= DECL_TI_ARGS (d
);
19346 if (tmpl
!= gen_tmpl
)
19347 /* We should already have the extra args. */
19348 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
19349 == TMPL_ARGS_DEPTH (gen_args
));
19350 /* And what's in the hash table should match D. */
19351 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
19352 || spec
== NULL_TREE
);
19354 /* This needs to happen before any tsubsting. */
19355 if (! push_tinst_level (d
))
19358 timevar_push (TV_TEMPLATE_INST
);
19360 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
19361 for the instantiation. */
19362 td
= template_for_substitution (d
);
19363 code_pattern
= DECL_TEMPLATE_RESULT (td
);
19365 /* We should never be trying to instantiate a member of a class
19366 template or partial specialization. */
19367 gcc_assert (d
!= code_pattern
);
19369 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
19370 || DECL_TEMPLATE_SPECIALIZATION (td
))
19371 /* In the case of a friend template whose definition is provided
19372 outside the class, we may have too many arguments. Drop the
19373 ones we don't need. The same is true for specializations. */
19374 args
= get_innermost_template_args
19375 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
19379 if (TREE_CODE (d
) == FUNCTION_DECL
)
19380 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
19381 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
));
19383 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
19385 /* We may be in the middle of deferred access check. Disable it now. */
19386 push_deferring_access_checks (dk_no_deferred
);
19388 /* Unless an explicit instantiation directive has already determined
19389 the linkage of D, remember that a definition is available for
19391 if (pattern_defined
19392 && !DECL_INTERFACE_KNOWN (d
)
19393 && !DECL_NOT_REALLY_EXTERN (d
))
19394 mark_definable (d
);
19396 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
19397 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
19398 input_location
= DECL_SOURCE_LOCATION (d
);
19400 /* If D is a member of an explicitly instantiated class template,
19401 and no definition is available, treat it like an implicit
19403 if (!pattern_defined
&& expl_inst_class_mem_p
19404 && DECL_EXPLICIT_INSTANTIATION (d
))
19406 /* Leave linkage flags alone on instantiations with anonymous
19408 if (TREE_PUBLIC (d
))
19410 DECL_NOT_REALLY_EXTERN (d
) = 0;
19411 DECL_INTERFACE_KNOWN (d
) = 0;
19413 SET_DECL_IMPLICIT_INSTANTIATION (d
);
19416 if (TREE_CODE (d
) == FUNCTION_DECL
)
19417 maybe_instantiate_noexcept (d
);
19419 /* Defer all other templates, unless we have been explicitly
19420 forbidden from doing so. */
19421 if (/* If there is no definition, we cannot instantiate the
19424 /* If it's OK to postpone instantiation, do so. */
19426 /* If this is a static data member that will be defined
19427 elsewhere, we don't want to instantiate the entire data
19428 member, but we do want to instantiate the initializer so that
19429 we can substitute that elsewhere. */
19430 || (external_p
&& VAR_P (d
)))
19432 /* The definition of the static data member is now required so
19433 we must substitute the initializer. */
19435 && !DECL_INITIAL (d
)
19436 && DECL_INITIAL (code_pattern
))
19440 bool const_init
= false;
19442 ns
= decl_namespace_context (d
);
19443 push_nested_namespace (ns
);
19444 push_nested_class (DECL_CONTEXT (d
));
19445 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
19447 tf_warning_or_error
, NULL_TREE
,
19448 /*integral_constant_expression_p=*/false);
19449 /* Make sure the initializer is still constant, in case of
19450 circular dependency (template/instantiate6.C). */
19452 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
19453 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
19454 /*asmspec_tree=*/NULL_TREE
,
19455 LOOKUP_ONLYCONVERTING
);
19456 pop_nested_class ();
19457 pop_nested_namespace (ns
);
19460 /* We restore the source position here because it's used by
19461 add_pending_template. */
19462 input_location
= saved_loc
;
19464 if (at_eof
&& !pattern_defined
19465 && DECL_EXPLICIT_INSTANTIATION (d
)
19466 && DECL_NOT_REALLY_EXTERN (d
))
19469 The definition of a non-exported function template, a
19470 non-exported member function template, or a non-exported
19471 member function or static data member of a class template
19472 shall be present in every translation unit in which it is
19473 explicitly instantiated. */
19474 permerror (input_location
, "explicit instantiation of %qD "
19475 "but no definition available", d
);
19477 /* If we're in unevaluated context, we just wanted to get the
19478 constant value; this isn't an odr use, so don't queue
19479 a full instantiation. */
19480 if (cp_unevaluated_operand
!= 0)
19482 /* ??? Historically, we have instantiated inline functions, even
19483 when marked as "extern template". */
19484 if (!(external_p
&& VAR_P (d
)))
19485 add_pending_template (d
);
19488 /* Tell the repository that D is available in this translation unit
19489 -- and see if it is supposed to be instantiated here. */
19490 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
19492 /* In a PCH file, despite the fact that the repository hasn't
19493 requested instantiation in the PCH it is still possible that
19494 an instantiation will be required in a file that includes the
19497 add_pending_template (d
);
19498 /* Instantiate inline functions so that the inliner can do its
19499 job, even though we'll not be emitting a copy of this
19501 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
19505 fn_context
= decl_function_context (d
);
19506 nested
= (current_function_decl
!= NULL_TREE
);
19508 push_to_top_level ();
19512 push_function_context ();
19513 cp_unevaluated_operand
= 0;
19514 c_inhibit_evaluation_warnings
= 0;
19517 /* Mark D as instantiated so that recursive calls to
19518 instantiate_decl do not try to instantiate it again. */
19519 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
19521 /* Regenerate the declaration in case the template has been modified
19522 by a subsequent redeclaration. */
19523 regenerate_decl_from_template (d
, td
);
19525 /* We already set the file and line above. Reset them now in case
19526 they changed as a result of calling regenerate_decl_from_template. */
19527 input_location
= DECL_SOURCE_LOCATION (d
);
19532 bool const_init
= false;
19534 /* Clear out DECL_RTL; whatever was there before may not be right
19535 since we've reset the type of the declaration. */
19536 SET_DECL_RTL (d
, NULL
);
19537 DECL_IN_AGGR_P (d
) = 0;
19539 /* The initializer is placed in DECL_INITIAL by
19540 regenerate_decl_from_template so we don't need to
19541 push/pop_access_scope again here. Pull it out so that
19542 cp_finish_decl can process it. */
19543 init
= DECL_INITIAL (d
);
19544 DECL_INITIAL (d
) = NULL_TREE
;
19545 DECL_INITIALIZED_P (d
) = 0;
19547 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
19548 initializer. That function will defer actual emission until
19549 we have a chance to determine linkage. */
19550 DECL_EXTERNAL (d
) = 0;
19552 /* Enter the scope of D so that access-checking works correctly. */
19553 push_nested_class (DECL_CONTEXT (d
));
19554 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
19555 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
19556 pop_nested_class ();
19558 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
19559 synthesize_method (d
);
19560 else if (TREE_CODE (d
) == FUNCTION_DECL
)
19562 struct pointer_map_t
*saved_local_specializations
;
19566 tree block
= NULL_TREE
;
19568 /* Save away the current list, in case we are instantiating one
19569 template from within the body of another. */
19570 saved_local_specializations
= local_specializations
;
19572 /* Set up the list of local specializations. */
19573 local_specializations
= pointer_map_create ();
19575 /* Set up context. */
19576 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
19577 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
19578 block
= push_stmt_list ();
19580 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
19582 /* Some typedefs referenced from within the template code need to be
19583 access checked at template instantiation time, i.e now. These
19584 types were added to the template at parsing time. Let's get those
19585 and perform the access checks then. */
19586 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl
),
19589 /* Create substitution entries for the parameters. */
19590 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
19591 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
19592 spec_parm
= DECL_ARGUMENTS (d
);
19593 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
19595 register_local_specialization (spec_parm
, tmpl_parm
);
19596 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
19597 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
19599 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
19601 if (!DECL_PACK_P (tmpl_parm
))
19603 register_local_specialization (spec_parm
, tmpl_parm
);
19604 spec_parm
= DECL_CHAIN (spec_parm
);
19608 /* Register the (value) argument pack as a specialization of
19609 TMPL_PARM, then move on. */
19610 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
19611 register_local_specialization (argpack
, tmpl_parm
);
19614 gcc_assert (!spec_parm
);
19616 /* Substitute into the body of the function. */
19617 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
19618 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
19619 tf_warning_or_error
, tmpl
);
19622 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
19623 tf_warning_or_error
, tmpl
,
19624 /*integral_constant_expression_p=*/false);
19626 /* Set the current input_location to the end of the function
19627 so that finish_function knows where we are. */
19629 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
19632 /* We don't need the local specializations any more. */
19633 pointer_map_destroy (local_specializations
);
19634 local_specializations
= saved_local_specializations
;
19636 /* Finish the function. */
19637 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
19638 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
19639 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
19642 d
= finish_function (0);
19643 expand_or_defer_fn (d
);
19646 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
19647 cp_check_omp_declare_reduction (d
);
19650 /* We're not deferring instantiation any more. */
19651 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
19654 pop_from_top_level ();
19656 pop_function_context ();
19659 input_location
= saved_loc
;
19660 cp_unevaluated_operand
= saved_unevaluated_operand
;
19661 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
19662 pop_deferring_access_checks ();
19663 pop_tinst_level ();
19665 timevar_pop (TV_TEMPLATE_INST
);
19670 /* Run through the list of templates that we wish we could
19671 instantiate, and instantiate any we can. RETRIES is the
19672 number of times we retry pending template instantiation. */
19675 instantiate_pending_templates (int retries
)
19678 location_t saved_loc
= input_location
;
19680 /* Instantiating templates may trigger vtable generation. This in turn
19681 may require further template instantiations. We place a limit here
19682 to avoid infinite loop. */
19683 if (pending_templates
&& retries
>= max_tinst_depth
)
19685 tree decl
= pending_templates
->tinst
->decl
;
19687 error ("template instantiation depth exceeds maximum of %d"
19688 " instantiating %q+D, possibly from virtual table generation"
19689 " (use -ftemplate-depth= to increase the maximum)",
19690 max_tinst_depth
, decl
);
19691 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19692 /* Pretend that we defined it. */
19693 DECL_INITIAL (decl
) = error_mark_node
;
19699 struct pending_template
**t
= &pending_templates
;
19700 struct pending_template
*last
= NULL
;
19704 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
19705 bool complete
= false;
19707 if (TYPE_P (instantiation
))
19711 if (!COMPLETE_TYPE_P (instantiation
))
19713 instantiate_class_template (instantiation
);
19714 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
19715 for (fn
= TYPE_METHODS (instantiation
);
19717 fn
= TREE_CHAIN (fn
))
19718 if (! DECL_ARTIFICIAL (fn
))
19719 instantiate_decl (fn
,
19721 /*expl_inst_class_mem_p=*/false);
19722 if (COMPLETE_TYPE_P (instantiation
))
19726 complete
= COMPLETE_TYPE_P (instantiation
);
19730 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
19731 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
19734 = instantiate_decl (instantiation
,
19736 /*expl_inst_class_mem_p=*/false);
19737 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
19741 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
19742 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
19746 /* If INSTANTIATION has been instantiated, then we don't
19747 need to consider it again in the future. */
19755 current_tinst_level
= NULL
;
19757 last_pending_template
= last
;
19759 while (reconsider
);
19761 input_location
= saved_loc
;
19764 /* Substitute ARGVEC into T, which is a list of initializers for
19765 either base class or a non-static data member. The TREE_PURPOSEs
19766 are DECLs, and the TREE_VALUEs are the initializer values. Used by
19767 instantiate_decl. */
19770 tsubst_initializer_list (tree t
, tree argvec
)
19772 tree inits
= NULL_TREE
;
19774 for (; t
; t
= TREE_CHAIN (t
))
19778 tree expanded_bases
= NULL_TREE
;
19779 tree expanded_arguments
= NULL_TREE
;
19782 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
19787 /* Expand the base class expansion type into separate base
19789 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
19790 tf_warning_or_error
,
19792 if (expanded_bases
== error_mark_node
)
19795 /* We'll be building separate TREE_LISTs of arguments for
19797 len
= TREE_VEC_LENGTH (expanded_bases
);
19798 expanded_arguments
= make_tree_vec (len
);
19799 for (i
= 0; i
< len
; i
++)
19800 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
19802 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
19803 expand each argument in the TREE_VALUE of t. */
19804 expr
= make_node (EXPR_PACK_EXPANSION
);
19805 PACK_EXPANSION_LOCAL_P (expr
) = true;
19806 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
19807 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
19809 if (TREE_VALUE (t
) == void_type_node
)
19810 /* VOID_TYPE_NODE is used to indicate
19811 value-initialization. */
19813 for (i
= 0; i
< len
; i
++)
19814 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
19818 /* Substitute parameter packs into each argument in the
19820 in_base_initializer
= 1;
19821 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
19823 tree expanded_exprs
;
19825 /* Expand the argument. */
19826 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
19828 = tsubst_pack_expansion (expr
, argvec
,
19829 tf_warning_or_error
,
19831 if (expanded_exprs
== error_mark_node
)
19834 /* Prepend each of the expanded expressions to the
19835 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
19836 for (i
= 0; i
< len
; i
++)
19838 TREE_VEC_ELT (expanded_arguments
, i
) =
19839 tree_cons (NULL_TREE
,
19840 TREE_VEC_ELT (expanded_exprs
, i
),
19841 TREE_VEC_ELT (expanded_arguments
, i
));
19844 in_base_initializer
= 0;
19846 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
19847 since we built them backwards. */
19848 for (i
= 0; i
< len
; i
++)
19850 TREE_VEC_ELT (expanded_arguments
, i
) =
19851 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
19856 for (i
= 0; i
< len
; ++i
)
19858 if (expanded_bases
)
19860 decl
= TREE_VEC_ELT (expanded_bases
, i
);
19861 decl
= expand_member_init (decl
);
19862 init
= TREE_VEC_ELT (expanded_arguments
, i
);
19867 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
19868 tf_warning_or_error
, NULL_TREE
);
19870 decl
= expand_member_init (decl
);
19871 if (decl
&& !DECL_P (decl
))
19872 in_base_initializer
= 1;
19874 init
= TREE_VALUE (t
);
19876 if (init
!= void_type_node
)
19877 init
= tsubst_expr (init
, argvec
,
19878 tf_warning_or_error
, NULL_TREE
,
19879 /*integral_constant_expression_p=*/false);
19880 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
19881 /* If we had an initializer but it instantiated to nothing,
19882 value-initialize the object. This will only occur when
19883 the initializer was a pack expansion where the parameter
19884 packs used in that expansion were of length zero. */
19885 init
= void_type_node
;
19886 in_base_initializer
= 0;
19891 init
= build_tree_list (decl
, init
);
19892 TREE_CHAIN (init
) = inits
;
19900 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19903 set_current_access_from_decl (tree decl
)
19905 if (TREE_PRIVATE (decl
))
19906 current_access_specifier
= access_private_node
;
19907 else if (TREE_PROTECTED (decl
))
19908 current_access_specifier
= access_protected_node
;
19910 current_access_specifier
= access_public_node
;
19913 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19914 is the instantiation (which should have been created with
19915 start_enum) and ARGS are the template arguments to use. */
19918 tsubst_enum (tree tag
, tree newtag
, tree args
)
19922 if (SCOPED_ENUM_P (newtag
))
19923 begin_scope (sk_scoped_enum
, newtag
);
19925 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
19930 decl
= TREE_VALUE (e
);
19931 /* Note that in a template enum, the TREE_VALUE is the
19932 CONST_DECL, not the corresponding INTEGER_CST. */
19933 value
= tsubst_expr (DECL_INITIAL (decl
),
19934 args
, tf_warning_or_error
, NULL_TREE
,
19935 /*integral_constant_expression_p=*/true);
19937 /* Give this enumeration constant the correct access. */
19938 set_current_access_from_decl (decl
);
19940 /* Actually build the enumerator itself. */
19942 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
19945 if (SCOPED_ENUM_P (newtag
))
19948 finish_enum_value_list (newtag
);
19949 finish_enum (newtag
);
19951 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
19952 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
19955 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19956 its type -- but without substituting the innermost set of template
19957 arguments. So, innermost set of template parameters will appear in
19961 get_mostly_instantiated_function_type (tree decl
)
19969 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
19970 targs
= DECL_TI_ARGS (decl
);
19971 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
19972 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
19974 /* There should be as many levels of arguments as there are levels
19976 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
19978 fn_type
= TREE_TYPE (tmpl
);
19980 if (parm_depth
== 1)
19981 /* No substitution is necessary. */
19988 /* Replace the innermost level of the TARGS with NULL_TREEs to
19989 let tsubst know not to substitute for those parameters. */
19990 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
19991 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
19992 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
19993 TMPL_ARGS_LEVEL (targs
, i
));
19994 SET_TMPL_ARGS_LEVEL (partial_args
,
19995 TMPL_ARGS_DEPTH (targs
),
19996 make_tree_vec (DECL_NTPARMS (tmpl
)));
19998 /* Make sure that we can see identifiers, and compute access
20000 push_access_scope (decl
);
20002 ++processing_template_decl
;
20003 /* Now, do the (partial) substitution to figure out the
20004 appropriate function type. */
20005 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
20006 --processing_template_decl
;
20008 /* Substitute into the template parameters to obtain the real
20009 innermost set of parameters. This step is important if the
20010 innermost set of template parameters contains value
20011 parameters whose types depend on outer template parameters. */
20012 TREE_VEC_LENGTH (partial_args
)--;
20013 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
20015 pop_access_scope (decl
);
20021 /* Return truthvalue if we're processing a template different from
20022 the last one involved in diagnostics. */
20024 problematic_instantiation_changed (void)
20026 return current_tinst_level
!= last_error_tinst_level
;
20029 /* Remember current template involved in diagnostics. */
20031 record_last_problematic_instantiation (void)
20033 last_error_tinst_level
= current_tinst_level
;
20036 struct tinst_level
*
20037 current_instantiation (void)
20039 return current_tinst_level
;
20042 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20043 type. Return zero for ok, nonzero for disallowed. Issue error and
20044 warning messages under control of COMPLAIN. */
20047 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
20049 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
20051 else if (POINTER_TYPE_P (type
))
20053 else if (TYPE_PTRMEM_P (type
))
20055 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
20057 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
20059 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
20061 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
20064 if (complain
& tf_error
)
20066 if (type
== error_mark_node
)
20067 inform (input_location
, "invalid template non-type parameter");
20069 error ("%q#T is not a valid type for a template non-type parameter",
20075 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20076 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20079 dependent_type_p_r (tree type
)
20085 A type is dependent if it is:
20087 -- a template parameter. Template template parameters are types
20088 for us (since TYPE_P holds true for them) so we handle
20090 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20091 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
20093 /* -- a qualified-id with a nested-name-specifier which contains a
20094 class-name that names a dependent type or whose unqualified-id
20095 names a dependent type. */
20096 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20098 /* -- a cv-qualified type where the cv-unqualified type is
20100 type
= TYPE_MAIN_VARIANT (type
);
20101 /* -- a compound type constructed from any dependent type. */
20102 if (TYPE_PTRMEM_P (type
))
20103 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
20104 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20106 else if (TYPE_PTR_P (type
)
20107 || TREE_CODE (type
) == REFERENCE_TYPE
)
20108 return dependent_type_p (TREE_TYPE (type
));
20109 else if (TREE_CODE (type
) == FUNCTION_TYPE
20110 || TREE_CODE (type
) == METHOD_TYPE
)
20114 if (dependent_type_p (TREE_TYPE (type
)))
20116 for (arg_type
= TYPE_ARG_TYPES (type
);
20118 arg_type
= TREE_CHAIN (arg_type
))
20119 if (dependent_type_p (TREE_VALUE (arg_type
)))
20123 /* -- an array type constructed from any dependent type or whose
20124 size is specified by a constant expression that is
20127 We checked for type- and value-dependence of the bounds in
20128 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20129 if (TREE_CODE (type
) == ARRAY_TYPE
)
20131 if (TYPE_DOMAIN (type
)
20132 && dependent_type_p (TYPE_DOMAIN (type
)))
20134 return dependent_type_p (TREE_TYPE (type
));
20137 /* -- a template-id in which either the template name is a template
20139 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20141 /* ... or any of the template arguments is a dependent type or
20142 an expression that is type-dependent or value-dependent. */
20143 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
20144 && (any_dependent_template_arguments_p
20145 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
20148 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20149 dependent; if the argument of the `typeof' expression is not
20150 type-dependent, then it should already been have resolved. */
20151 if (TREE_CODE (type
) == TYPEOF_TYPE
20152 || TREE_CODE (type
) == DECLTYPE_TYPE
20153 || TREE_CODE (type
) == UNDERLYING_TYPE
)
20156 /* A template argument pack is dependent if any of its packed
20158 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
20160 tree args
= ARGUMENT_PACK_ARGS (type
);
20161 int i
, len
= TREE_VEC_LENGTH (args
);
20162 for (i
= 0; i
< len
; ++i
)
20163 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
20167 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20168 be template parameters. */
20169 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
20172 /* The standard does not specifically mention types that are local
20173 to template functions or local classes, but they should be
20174 considered dependent too. For example:
20176 template <int I> void f() {
20181 The size of `E' cannot be known until the value of `I' has been
20182 determined. Therefore, `E' must be considered dependent. */
20183 scope
= TYPE_CONTEXT (type
);
20184 if (scope
&& TYPE_P (scope
))
20185 return dependent_type_p (scope
);
20186 /* Don't use type_dependent_expression_p here, as it can lead
20187 to infinite recursion trying to determine whether a lambda
20188 nested in a lambda is dependent (c++/47687). */
20189 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
20190 && DECL_LANG_SPECIFIC (scope
)
20191 && DECL_TEMPLATE_INFO (scope
)
20192 && (any_dependent_template_arguments_p
20193 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
20196 /* Other types are non-dependent. */
20200 /* Returns TRUE if TYPE is dependent, in the sense of
20201 [temp.dep.type]. Note that a NULL type is considered dependent. */
20204 dependent_type_p (tree type
)
20206 /* If there are no template parameters in scope, then there can't be
20207 any dependent types. */
20208 if (!processing_template_decl
)
20210 /* If we are not processing a template, then nobody should be
20211 providing us with a dependent type. */
20213 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
20217 /* If the type is NULL, we have not computed a type for the entity
20218 in question; in that case, the type is dependent. */
20222 /* Erroneous types can be considered non-dependent. */
20223 if (type
== error_mark_node
)
20226 /* If we have not already computed the appropriate value for TYPE,
20228 if (!TYPE_DEPENDENT_P_VALID (type
))
20230 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
20231 TYPE_DEPENDENT_P_VALID (type
) = 1;
20234 return TYPE_DEPENDENT_P (type
);
20237 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20238 lookup. In other words, a dependent type that is not the current
20242 dependent_scope_p (tree scope
)
20244 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
20245 && !currently_open_class (scope
));
20248 /* T is a SCOPE_REF; return whether we need to consider it
20249 instantiation-dependent so that we can check access at instantiation
20250 time even though we know which member it resolves to. */
20253 instantiation_dependent_scope_ref_p (tree t
)
20255 if (DECL_P (TREE_OPERAND (t
, 1))
20256 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
20257 && accessible_in_template_p (TREE_OPERAND (t
, 0),
20258 TREE_OPERAND (t
, 1)))
20264 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20265 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20268 /* Note that this predicate is not appropriate for general expressions;
20269 only constant expressions (that satisfy potential_constant_expression)
20270 can be tested for value dependence. */
20273 value_dependent_expression_p (tree expression
)
20275 if (!processing_template_decl
)
20278 /* A name declared with a dependent type. */
20279 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
20282 switch (TREE_CODE (expression
))
20284 case IDENTIFIER_NODE
:
20285 /* A name that has not been looked up -- must be dependent. */
20288 case TEMPLATE_PARM_INDEX
:
20289 /* A non-type template parm. */
20293 /* A non-type template parm. */
20294 if (DECL_TEMPLATE_PARM_P (expression
))
20296 return value_dependent_expression_p (DECL_INITIAL (expression
));
20299 /* A constant with literal type and is initialized
20300 with an expression that is value-dependent.
20302 Note that a non-dependent parenthesized initializer will have
20303 already been replaced with its constant value, so if we see
20304 a TREE_LIST it must be dependent. */
20305 if (DECL_INITIAL (expression
)
20306 && decl_constant_var_p (expression
)
20307 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
20308 || value_dependent_expression_p (DECL_INITIAL (expression
))))
20312 case DYNAMIC_CAST_EXPR
:
20313 case STATIC_CAST_EXPR
:
20314 case CONST_CAST_EXPR
:
20315 case REINTERPRET_CAST_EXPR
:
20317 /* These expressions are value-dependent if the type to which
20318 the cast occurs is dependent or the expression being casted
20319 is value-dependent. */
20321 tree type
= TREE_TYPE (expression
);
20323 if (dependent_type_p (type
))
20326 /* A functional cast has a list of operands. */
20327 expression
= TREE_OPERAND (expression
, 0);
20330 /* If there are no operands, it must be an expression such
20331 as "int()". This should not happen for aggregate types
20332 because it would form non-constant expressions. */
20333 gcc_assert (cxx_dialect
>= cxx11
20334 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
20339 if (TREE_CODE (expression
) == TREE_LIST
)
20340 return any_value_dependent_elements_p (expression
);
20342 return value_dependent_expression_p (expression
);
20346 if (SIZEOF_EXPR_TYPE_P (expression
))
20347 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
20351 /* A `sizeof' expression is value-dependent if the operand is
20352 type-dependent or is a pack expansion. */
20353 expression
= TREE_OPERAND (expression
, 0);
20354 if (PACK_EXPANSION_P (expression
))
20356 else if (TYPE_P (expression
))
20357 return dependent_type_p (expression
);
20358 return instantiation_dependent_expression_p (expression
);
20360 case AT_ENCODE_EXPR
:
20361 /* An 'encode' expression is value-dependent if the operand is
20363 expression
= TREE_OPERAND (expression
, 0);
20364 return dependent_type_p (expression
);
20366 case NOEXCEPT_EXPR
:
20367 expression
= TREE_OPERAND (expression
, 0);
20368 return instantiation_dependent_expression_p (expression
);
20371 /* All instantiation-dependent expressions should also be considered
20372 value-dependent. */
20373 return instantiation_dependent_scope_ref_p (expression
);
20375 case COMPONENT_REF
:
20376 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
20377 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
20379 case NONTYPE_ARGUMENT_PACK
:
20380 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
20381 is value-dependent. */
20383 tree values
= ARGUMENT_PACK_ARGS (expression
);
20384 int i
, len
= TREE_VEC_LENGTH (values
);
20386 for (i
= 0; i
< len
; ++i
)
20387 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
20395 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
20396 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
20397 || (type2
? dependent_type_p (type2
) : false));
20401 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20402 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
20405 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20406 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
20410 tree op
= TREE_OPERAND (expression
, 0);
20411 return (value_dependent_expression_p (op
)
20412 || has_value_dependent_address (op
));
20417 tree fn
= get_callee_fndecl (expression
);
20419 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
20421 nargs
= call_expr_nargs (expression
);
20422 for (i
= 0; i
< nargs
; ++i
)
20424 tree op
= CALL_EXPR_ARG (expression
, i
);
20425 /* In a call to a constexpr member function, look through the
20426 implicit ADDR_EXPR on the object argument so that it doesn't
20427 cause the call to be considered value-dependent. We also
20428 look through it in potential_constant_expression. */
20429 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
20430 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
20431 && TREE_CODE (op
) == ADDR_EXPR
)
20432 op
= TREE_OPERAND (op
, 0);
20433 if (value_dependent_expression_p (op
))
20439 case TEMPLATE_ID_EXPR
:
20440 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
20442 return type_dependent_expression_p (expression
);
20448 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
20449 if (value_dependent_expression_p (val
))
20455 /* Treat a GNU statement expression as dependent to avoid crashing
20456 under fold_non_dependent_expr; it can't be constant. */
20460 /* A constant expression is value-dependent if any subexpression is
20461 value-dependent. */
20462 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
20464 case tcc_reference
:
20466 case tcc_comparison
:
20468 case tcc_expression
:
20471 int i
, len
= cp_tree_operand_length (expression
);
20473 for (i
= 0; i
< len
; i
++)
20475 tree t
= TREE_OPERAND (expression
, i
);
20477 /* In some cases, some of the operands may be missing.l
20478 (For example, in the case of PREDECREMENT_EXPR, the
20479 amount to increment by may be missing.) That doesn't
20480 make the expression dependent. */
20481 if (t
&& value_dependent_expression_p (t
))
20492 /* The expression is not value-dependent. */
20496 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
20497 [temp.dep.expr]. Note that an expression with no type is
20498 considered dependent. Other parts of the compiler arrange for an
20499 expression with type-dependent subexpressions to have no type, so
20500 this function doesn't have to be fully recursive. */
20503 type_dependent_expression_p (tree expression
)
20505 if (!processing_template_decl
)
20508 if (expression
== error_mark_node
)
20511 /* An unresolved name is always dependent. */
20512 if (identifier_p (expression
) || TREE_CODE (expression
) == USING_DECL
)
20515 /* Some expression forms are never type-dependent. */
20516 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
20517 || TREE_CODE (expression
) == SIZEOF_EXPR
20518 || TREE_CODE (expression
) == ALIGNOF_EXPR
20519 || TREE_CODE (expression
) == AT_ENCODE_EXPR
20520 || TREE_CODE (expression
) == NOEXCEPT_EXPR
20521 || TREE_CODE (expression
) == TRAIT_EXPR
20522 || TREE_CODE (expression
) == TYPEID_EXPR
20523 || TREE_CODE (expression
) == DELETE_EXPR
20524 || TREE_CODE (expression
) == VEC_DELETE_EXPR
20525 || TREE_CODE (expression
) == THROW_EXPR
)
20528 /* The types of these expressions depends only on the type to which
20529 the cast occurs. */
20530 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
20531 || TREE_CODE (expression
) == STATIC_CAST_EXPR
20532 || TREE_CODE (expression
) == CONST_CAST_EXPR
20533 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
20534 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
20535 || TREE_CODE (expression
) == CAST_EXPR
)
20536 return dependent_type_p (TREE_TYPE (expression
));
20538 /* The types of these expressions depends only on the type created
20539 by the expression. */
20540 if (TREE_CODE (expression
) == NEW_EXPR
20541 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
20543 /* For NEW_EXPR tree nodes created inside a template, either
20544 the object type itself or a TREE_LIST may appear as the
20546 tree type
= TREE_OPERAND (expression
, 1);
20547 if (TREE_CODE (type
) == TREE_LIST
)
20548 /* This is an array type. We need to check array dimensions
20550 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
20551 || value_dependent_expression_p
20552 (TREE_OPERAND (TREE_VALUE (type
), 1));
20554 return dependent_type_p (type
);
20557 if (TREE_CODE (expression
) == SCOPE_REF
)
20559 tree scope
= TREE_OPERAND (expression
, 0);
20560 tree name
= TREE_OPERAND (expression
, 1);
20562 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
20563 contains an identifier associated by name lookup with one or more
20564 declarations declared with a dependent type, or...a
20565 nested-name-specifier or qualified-id that names a member of an
20566 unknown specialization. */
20567 return (type_dependent_expression_p (name
)
20568 || dependent_scope_p (scope
));
20571 if (TREE_CODE (expression
) == FUNCTION_DECL
20572 && DECL_LANG_SPECIFIC (expression
)
20573 && DECL_TEMPLATE_INFO (expression
)
20574 && (any_dependent_template_arguments_p
20575 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
20578 if (TREE_CODE (expression
) == TEMPLATE_DECL
20579 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
20582 if (TREE_CODE (expression
) == STMT_EXPR
)
20583 expression
= stmt_expr_value_expr (expression
);
20585 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
20590 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
20592 if (type_dependent_expression_p (elt
))
20598 /* A static data member of the current instantiation with incomplete
20599 array type is type-dependent, as the definition and specializations
20600 can have different bounds. */
20601 if (VAR_P (expression
)
20602 && DECL_CLASS_SCOPE_P (expression
)
20603 && dependent_type_p (DECL_CONTEXT (expression
))
20604 && VAR_HAD_UNKNOWN_BOUND (expression
))
20607 /* An array of unknown bound depending on a variadic parameter, eg:
20609 template<typename... Args>
20610 void foo (Args... args)
20612 int arr[] = { args... };
20615 template<int... vals>
20618 int arr[] = { vals... };
20621 If the array has no length and has an initializer, it must be that
20622 we couldn't determine its length in cp_complete_array_type because
20623 it is dependent. */
20624 if (VAR_P (expression
)
20625 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
20626 && !TYPE_DOMAIN (TREE_TYPE (expression
))
20627 && DECL_INITIAL (expression
))
20630 if (TREE_TYPE (expression
) == unknown_type_node
)
20632 if (TREE_CODE (expression
) == ADDR_EXPR
)
20633 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
20634 if (TREE_CODE (expression
) == COMPONENT_REF
20635 || TREE_CODE (expression
) == OFFSET_REF
)
20637 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20639 expression
= TREE_OPERAND (expression
, 1);
20640 if (identifier_p (expression
))
20643 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
20644 if (TREE_CODE (expression
) == SCOPE_REF
)
20647 /* Always dependent, on the number of arguments if nothing else. */
20648 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
20651 if (BASELINK_P (expression
))
20652 expression
= BASELINK_FUNCTIONS (expression
);
20654 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
20656 if (any_dependent_template_arguments_p
20657 (TREE_OPERAND (expression
, 1)))
20659 expression
= TREE_OPERAND (expression
, 0);
20661 gcc_assert (TREE_CODE (expression
) == OVERLOAD
20662 || TREE_CODE (expression
) == FUNCTION_DECL
);
20666 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
20668 expression
= OVL_NEXT (expression
);
20673 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
20675 return (dependent_type_p (TREE_TYPE (expression
)));
20678 /* walk_tree callback function for instantiation_dependent_expression_p,
20679 below. Returns non-zero if a dependent subexpression is found. */
20682 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
20687 /* We don't have to worry about decltype currently because decltype
20688 of an instantiation-dependent expr is a dependent type. This
20689 might change depending on the resolution of DR 1172. */
20690 *walk_subtrees
= false;
20693 enum tree_code code
= TREE_CODE (*tp
);
20696 /* Don't treat an argument list as dependent just because it has no
20704 /* A constant with a dependent initializer is dependent. */
20705 if (value_dependent_expression_p (*tp
))
20709 case TEMPLATE_PARM_INDEX
:
20712 /* Handle expressions with type operands. */
20716 case AT_ENCODE_EXPR
:
20718 tree op
= TREE_OPERAND (*tp
, 0);
20719 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
20720 op
= TREE_TYPE (op
);
20723 if (dependent_type_p (op
))
20727 *walk_subtrees
= false;
20735 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp
))
20736 || (TRAIT_EXPR_TYPE2 (*tp
)
20737 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp
))))
20739 *walk_subtrees
= false;
20742 case COMPONENT_REF
:
20743 if (identifier_p (TREE_OPERAND (*tp
, 1)))
20744 /* In a template, finish_class_member_access_expr creates a
20745 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
20746 type-dependent, so that we can check access control at
20747 instantiation time (PR 42277). See also Core issue 1273. */
20752 if (instantiation_dependent_scope_ref_p (*tp
))
20757 /* Treat statement-expressions as dependent. */
20765 if (type_dependent_expression_p (*tp
))
20771 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
20772 sense defined by the ABI:
20774 "An expression is instantiation-dependent if it is type-dependent
20775 or value-dependent, or it has a subexpression that is type-dependent
20776 or value-dependent." */
20779 instantiation_dependent_expression_p (tree expression
)
20783 if (!processing_template_decl
)
20786 if (expression
== error_mark_node
)
20789 result
= cp_walk_tree_without_duplicates (&expression
,
20790 instantiation_dependent_r
, NULL
);
20791 return result
!= NULL_TREE
;
20794 /* Like type_dependent_expression_p, but it also works while not processing
20795 a template definition, i.e. during substitution or mangling. */
20798 type_dependent_expression_p_push (tree expr
)
20801 ++processing_template_decl
;
20802 b
= type_dependent_expression_p (expr
);
20803 --processing_template_decl
;
20807 /* Returns TRUE if ARGS contains a type-dependent expression. */
20810 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
20815 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
20817 if (type_dependent_expression_p (arg
))
20823 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20824 expressions) contains any type-dependent expressions. */
20827 any_type_dependent_elements_p (const_tree list
)
20829 for (; list
; list
= TREE_CHAIN (list
))
20830 if (type_dependent_expression_p (TREE_VALUE (list
)))
20836 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20837 expressions) contains any value-dependent expressions. */
20840 any_value_dependent_elements_p (const_tree list
)
20842 for (; list
; list
= TREE_CHAIN (list
))
20843 if (value_dependent_expression_p (TREE_VALUE (list
)))
20849 /* Returns TRUE if the ARG (a template argument) is dependent. */
20852 dependent_template_arg_p (tree arg
)
20854 if (!processing_template_decl
)
20857 /* Assume a template argument that was wrongly written by the user
20858 is dependent. This is consistent with what
20859 any_dependent_template_arguments_p [that calls this function]
20861 if (!arg
|| arg
== error_mark_node
)
20864 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
20865 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
20867 if (TREE_CODE (arg
) == TEMPLATE_DECL
20868 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
20869 return dependent_template_p (arg
);
20870 else if (ARGUMENT_PACK_P (arg
))
20872 tree args
= ARGUMENT_PACK_ARGS (arg
);
20873 int i
, len
= TREE_VEC_LENGTH (args
);
20874 for (i
= 0; i
< len
; ++i
)
20876 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
20882 else if (TYPE_P (arg
))
20883 return dependent_type_p (arg
);
20885 return (type_dependent_expression_p (arg
)
20886 || value_dependent_expression_p (arg
));
20889 /* Returns true if ARGS (a collection of template arguments) contains
20890 any types that require structural equality testing. */
20893 any_template_arguments_need_structural_equality_p (tree args
)
20900 if (args
== error_mark_node
)
20903 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
20905 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
20906 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
20908 tree arg
= TREE_VEC_ELT (level
, j
);
20909 tree packed_args
= NULL_TREE
;
20912 if (ARGUMENT_PACK_P (arg
))
20914 /* Look inside the argument pack. */
20915 packed_args
= ARGUMENT_PACK_ARGS (arg
);
20916 len
= TREE_VEC_LENGTH (packed_args
);
20919 for (k
= 0; k
< len
; ++k
)
20922 arg
= TREE_VEC_ELT (packed_args
, k
);
20924 if (error_operand_p (arg
))
20926 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
20928 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
20930 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
20931 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
20940 /* Returns true if ARGS (a collection of template arguments) contains
20941 any dependent arguments. */
20944 any_dependent_template_arguments_p (const_tree args
)
20951 if (args
== error_mark_node
)
20954 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
20956 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
20957 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
20958 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
20965 /* Returns TRUE if the template TMPL is dependent. */
20968 dependent_template_p (tree tmpl
)
20970 if (TREE_CODE (tmpl
) == OVERLOAD
)
20974 if (dependent_template_p (OVL_CURRENT (tmpl
)))
20976 tmpl
= OVL_NEXT (tmpl
);
20981 /* Template template parameters are dependent. */
20982 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
20983 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
20985 /* So are names that have not been looked up. */
20986 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
20988 /* So are member templates of dependent classes. */
20989 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
20990 return dependent_type_p (DECL_CONTEXT (tmpl
));
20994 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
20997 dependent_template_id_p (tree tmpl
, tree args
)
20999 return (dependent_template_p (tmpl
)
21000 || any_dependent_template_arguments_p (args
));
21003 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21007 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
21011 if (!processing_template_decl
)
21014 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
21016 tree decl
= TREE_VEC_ELT (declv
, i
);
21017 tree init
= TREE_VEC_ELT (initv
, i
);
21018 tree cond
= TREE_VEC_ELT (condv
, i
);
21019 tree incr
= TREE_VEC_ELT (incrv
, i
);
21021 if (type_dependent_expression_p (decl
))
21024 if (init
&& type_dependent_expression_p (init
))
21027 if (type_dependent_expression_p (cond
))
21030 if (COMPARISON_CLASS_P (cond
)
21031 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
21032 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
21035 if (TREE_CODE (incr
) == MODOP_EXPR
)
21037 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
21038 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
21041 else if (type_dependent_expression_p (incr
))
21043 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
21045 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
21047 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
21049 tree t
= TREE_OPERAND (incr
, 1);
21050 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
21051 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
21060 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21061 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21062 no such TYPE can be found. Note that this function peers inside
21063 uninstantiated templates and therefore should be used only in
21064 extremely limited situations. ONLY_CURRENT_P restricts this
21065 peering to the currently open classes hierarchy (which is required
21066 when comparing types). */
21069 resolve_typename_type (tree type
, bool only_current_p
)
21078 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
21080 scope
= TYPE_CONTEXT (type
);
21081 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21082 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21083 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21084 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21085 identifier of the TYPENAME_TYPE anymore.
21086 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21087 TYPENAME_TYPE instead, we avoid messing up with a possible
21088 typedef variant case. */
21089 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
21091 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21092 it first before we can figure out what NAME refers to. */
21093 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
21095 if (TYPENAME_IS_RESOLVING_P (scope
))
21096 /* Given a class template A with a dependent base with nested type C,
21097 typedef typename A::C::C C will land us here, as trying to resolve
21098 the initial A::C leads to the local C typedef, which leads back to
21099 A::C::C. So we break the recursion now. */
21102 scope
= resolve_typename_type (scope
, only_current_p
);
21104 /* If we don't know what SCOPE refers to, then we cannot resolve the
21106 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
21108 /* If the SCOPE is a template type parameter, we have no way of
21109 resolving the name. */
21110 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
21112 /* If the SCOPE is not the current instantiation, there's no reason
21113 to look inside it. */
21114 if (only_current_p
&& !currently_open_class (scope
))
21116 /* If this is a typedef, we don't want to look inside (c++/11987). */
21117 if (typedef_variant_p (type
))
21119 /* If SCOPE isn't the template itself, it will not have a valid
21120 TYPE_FIELDS list. */
21121 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
21122 /* scope is either the template itself or a compatible instantiation
21123 like X<T>, so look up the name in the original template. */
21124 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
21126 /* scope is a partial instantiation, so we can't do the lookup or we
21127 will lose the template arguments. */
21129 /* Enter the SCOPE so that name lookup will be resolved as if we
21130 were in the class definition. In particular, SCOPE will no
21131 longer be considered a dependent type. */
21132 pushed_scope
= push_scope (scope
);
21133 /* Look up the declaration. */
21134 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
21135 tf_warning_or_error
);
21137 result
= NULL_TREE
;
21139 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21140 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21143 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type
))
21144 && TREE_CODE (decl
) == TYPE_DECL
)
21146 result
= TREE_TYPE (decl
);
21147 if (result
== error_mark_node
)
21148 result
= NULL_TREE
;
21150 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
21151 && DECL_CLASS_TEMPLATE_P (decl
))
21155 /* Obtain the template and the arguments. */
21156 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
21157 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
21158 /* Instantiate the template. */
21159 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
21160 /*entering_scope=*/0,
21161 tf_error
| tf_user
);
21162 if (result
== error_mark_node
)
21163 result
= NULL_TREE
;
21166 /* Leave the SCOPE. */
21168 pop_scope (pushed_scope
);
21170 /* If we failed to resolve it, return the original typename. */
21174 /* If lookup found a typename type, resolve that too. */
21175 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
21177 /* Ill-formed programs can cause infinite recursion here, so we
21178 must catch that. */
21179 TYPENAME_IS_RESOLVING_P (type
) = 1;
21180 result
= resolve_typename_type (result
, only_current_p
);
21181 TYPENAME_IS_RESOLVING_P (type
) = 0;
21184 /* Qualify the resulting type. */
21185 quals
= cp_type_quals (type
);
21187 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
21192 /* EXPR is an expression which is not type-dependent. Return a proxy
21193 for EXPR that can be used to compute the types of larger
21194 expressions containing EXPR. */
21197 build_non_dependent_expr (tree expr
)
21201 #ifdef ENABLE_CHECKING
21202 /* Try to get a constant value for all non-dependent expressions in
21203 order to expose bugs in *_dependent_expression_p and constexpr. */
21204 if (cxx_dialect
>= cxx11
)
21205 maybe_constant_value (fold_non_dependent_expr_sfinae (expr
, tf_none
));
21208 /* Preserve OVERLOADs; the functions must be available to resolve
21211 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
21212 inner_expr
= stmt_expr_value_expr (inner_expr
);
21213 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
21214 inner_expr
= TREE_OPERAND (inner_expr
, 0);
21215 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
21216 inner_expr
= TREE_OPERAND (inner_expr
, 1);
21217 if (is_overloaded_fn (inner_expr
)
21218 || TREE_CODE (inner_expr
) == OFFSET_REF
)
21220 /* There is no need to return a proxy for a variable. */
21223 /* Preserve string constants; conversions from string constants to
21224 "char *" are allowed, even though normally a "const char *"
21225 cannot be used to initialize a "char *". */
21226 if (TREE_CODE (expr
) == STRING_CST
)
21228 /* Preserve arithmetic constants, as an optimization -- there is no
21229 reason to create a new node. */
21230 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
21232 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21233 There is at least one place where we want to know that a
21234 particular expression is a throw-expression: when checking a ?:
21235 expression, there are special rules if the second or third
21236 argument is a throw-expression. */
21237 if (TREE_CODE (expr
) == THROW_EXPR
)
21240 /* Don't wrap an initializer list, we need to be able to look inside. */
21241 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
21244 /* Don't wrap a dummy object, we need to be able to test for it. */
21245 if (is_dummy_object (expr
))
21248 if (TREE_CODE (expr
) == COND_EXPR
)
21249 return build3 (COND_EXPR
,
21251 TREE_OPERAND (expr
, 0),
21252 (TREE_OPERAND (expr
, 1)
21253 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
21254 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
21255 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
21256 if (TREE_CODE (expr
) == COMPOUND_EXPR
21257 && !COMPOUND_EXPR_OVERLOADED (expr
))
21258 return build2 (COMPOUND_EXPR
,
21260 TREE_OPERAND (expr
, 0),
21261 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
21263 /* If the type is unknown, it can't really be non-dependent */
21264 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
21266 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21267 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
21270 /* ARGS is a vector of expressions as arguments to a function call.
21271 Replace the arguments with equivalent non-dependent expressions.
21272 This modifies ARGS in place. */
21275 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
21280 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
21282 tree newarg
= build_non_dependent_expr (arg
);
21284 (*args
)[ix
] = newarg
;
21288 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21289 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21293 make_auto_1 (tree name
)
21295 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
21296 TYPE_NAME (au
) = build_decl (input_location
,
21297 TYPE_DECL
, name
, au
);
21298 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
21299 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
21300 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
21301 TYPE_NAME (au
), NULL_TREE
);
21302 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
21303 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
21304 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
21310 make_decltype_auto (void)
21312 return make_auto_1 (get_identifier ("decltype(auto)"));
21318 return make_auto_1 (get_identifier ("auto"));
21321 /* Given type ARG, return std::initializer_list<ARG>. */
21326 tree std_init_list
= namespace_binding
21327 (get_identifier ("initializer_list"), std_node
);
21329 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
21331 error ("deducing from brace-enclosed initializer list requires "
21332 "#include <initializer_list>");
21333 return error_mark_node
;
21335 argvec
= make_tree_vec (1);
21336 TREE_VEC_ELT (argvec
, 0) = arg
;
21337 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
21338 NULL_TREE
, 0, tf_warning_or_error
);
21341 /* Replace auto in TYPE with std::initializer_list<auto>. */
21344 listify_autos (tree type
, tree auto_node
)
21346 tree init_auto
= listify (auto_node
);
21347 tree argvec
= make_tree_vec (1);
21348 TREE_VEC_ELT (argvec
, 0) = init_auto
;
21349 if (processing_template_decl
)
21350 argvec
= add_to_template_args (current_template_args (), argvec
);
21351 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
21354 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
21355 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
21358 do_auto_deduction (tree type
, tree init
, tree auto_node
)
21362 if (init
== error_mark_node
)
21363 return error_mark_node
;
21365 if (type_dependent_expression_p (init
))
21366 /* Defining a subset of type-dependent expressions that we can deduce
21367 from ahead of time isn't worth the trouble. */
21370 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
21371 with either a new invented type template parameter U or, if the
21372 initializer is a braced-init-list (8.5.4), with
21373 std::initializer_list<U>. */
21374 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
21375 type
= listify_autos (type
, auto_node
);
21377 init
= resolve_nondeduced_context (init
);
21379 targs
= make_tree_vec (1);
21380 if (AUTO_IS_DECLTYPE (auto_node
))
21382 bool id
= (DECL_P (init
) || TREE_CODE (init
) == COMPONENT_REF
);
21383 TREE_VEC_ELT (targs
, 0)
21384 = finish_decltype_type (init
, id
, tf_warning_or_error
);
21385 if (type
!= auto_node
)
21387 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
21388 return error_mark_node
;
21393 tree parms
= build_tree_list (NULL_TREE
, type
);
21394 tree tparms
= make_tree_vec (1);
21397 TREE_VEC_ELT (tparms
, 0)
21398 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
21399 val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
21400 DEDUCE_CALL
, LOOKUP_NORMAL
,
21401 NULL
, /*explain_p=*/false);
21404 if (processing_template_decl
)
21405 /* Try again at instantiation time. */
21407 if (type
&& type
!= error_mark_node
)
21408 /* If type is error_mark_node a diagnostic must have been
21409 emitted by now. Also, having a mention to '<type error>'
21410 in the diagnostic is not really useful to the user. */
21412 if (cfun
&& auto_node
== current_function_auto_return_pattern
21413 && LAMBDA_FUNCTION_P (current_function_decl
))
21414 error ("unable to deduce lambda return type from %qE", init
);
21416 error ("unable to deduce %qT from %qE", type
, init
);
21418 return error_mark_node
;
21422 /* If the list of declarators contains more than one declarator, the type
21423 of each declared variable is determined as described above. If the
21424 type deduced for the template parameter U is not the same in each
21425 deduction, the program is ill-formed. */
21426 if (TREE_TYPE (auto_node
)
21427 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
21429 if (cfun
&& auto_node
== current_function_auto_return_pattern
21430 && LAMBDA_FUNCTION_P (current_function_decl
))
21431 error ("inconsistent types %qT and %qT deduced for "
21432 "lambda return type", TREE_TYPE (auto_node
),
21433 TREE_VEC_ELT (targs
, 0));
21435 error ("inconsistent deduction for %qT: %qT and then %qT",
21436 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
21437 return error_mark_node
;
21439 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
21441 if (processing_template_decl
)
21442 targs
= add_to_template_args (current_template_args (), targs
);
21443 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
21446 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
21450 splice_late_return_type (tree type
, tree late_return_type
)
21454 if (late_return_type
== NULL_TREE
)
21456 argvec
= make_tree_vec (1);
21457 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
21458 if (processing_template_parmlist
)
21459 /* For a late-specified return type in a template type-parameter, we
21460 need to add a dummy argument level for its parmlist. */
21461 argvec
= add_to_template_args
21462 (make_tree_vec (processing_template_parmlist
), argvec
);
21463 if (current_template_parms
)
21464 argvec
= add_to_template_args (current_template_args (), argvec
);
21465 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
21468 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
21469 'decltype(auto)'. */
21472 is_auto (const_tree type
)
21474 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21475 && (TYPE_IDENTIFIER (type
) == get_identifier ("auto")
21476 || TYPE_IDENTIFIER (type
) == get_identifier ("decltype(auto)")))
21482 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
21483 a use of `auto'. Returns NULL_TREE otherwise. */
21486 type_uses_auto (tree type
)
21488 return find_type_usage (type
, is_auto
);
21491 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
21492 'decltype(auto)' or a concept. */
21495 is_auto_or_concept (const_tree type
)
21497 return is_auto (type
); // or concept
21500 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
21501 a concept identifier) iff TYPE contains a use of a generic type. Returns
21502 NULL_TREE otherwise. */
21505 type_uses_auto_or_concept (tree type
)
21507 return find_type_usage (type
, is_auto_or_concept
);
21511 /* For a given template T, return the vector of typedefs referenced
21512 in T for which access check is needed at T instantiation time.
21513 T is either a FUNCTION_DECL or a RECORD_TYPE.
21514 Those typedefs were added to T by the function
21515 append_type_to_template_for_access_check. */
21517 vec
<qualified_typedef_usage_t
, va_gc
> *
21518 get_types_needing_access_check (tree t
)
21521 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
21523 if (!t
|| t
== error_mark_node
)
21526 if (!(ti
= get_template_info (t
)))
21529 if (CLASS_TYPE_P (t
)
21530 || TREE_CODE (t
) == FUNCTION_DECL
)
21532 if (!TI_TEMPLATE (ti
))
21535 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
21541 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
21542 tied to T. That list of typedefs will be access checked at
21543 T instantiation time.
21544 T is either a FUNCTION_DECL or a RECORD_TYPE.
21545 TYPE_DECL is a TYPE_DECL node representing a typedef.
21546 SCOPE is the scope through which TYPE_DECL is accessed.
21547 LOCATION is the location of the usage point of TYPE_DECL.
21549 This function is a subroutine of
21550 append_type_to_template_for_access_check. */
21553 append_type_to_template_for_access_check_1 (tree t
,
21556 location_t location
)
21558 qualified_typedef_usage_t typedef_usage
;
21561 if (!t
|| t
== error_mark_node
)
21564 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
21565 || CLASS_TYPE_P (t
))
21567 && TREE_CODE (type_decl
) == TYPE_DECL
21570 if (!(ti
= get_template_info (t
)))
21573 gcc_assert (TI_TEMPLATE (ti
));
21575 typedef_usage
.typedef_decl
= type_decl
;
21576 typedef_usage
.context
= scope
;
21577 typedef_usage
.locus
= location
;
21579 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
21582 /* Append TYPE_DECL to the template TEMPL.
21583 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
21584 At TEMPL instanciation time, TYPE_DECL will be checked to see
21585 if it can be accessed through SCOPE.
21586 LOCATION is the location of the usage point of TYPE_DECL.
21588 e.g. consider the following code snippet:
21595 template<class U> struct S
21597 C::myint mi; // <-- usage point of the typedef C::myint
21602 At S<char> instantiation time, we need to check the access of C::myint
21603 In other words, we need to check the access of the myint typedef through
21604 the C scope. For that purpose, this function will add the myint typedef
21605 and the scope C through which its being accessed to a list of typedefs
21606 tied to the template S. That list will be walked at template instantiation
21607 time and access check performed on each typedefs it contains.
21608 Note that this particular code snippet should yield an error because
21609 myint is private to C. */
21612 append_type_to_template_for_access_check (tree templ
,
21615 location_t location
)
21617 qualified_typedef_usage_t
*iter
;
21620 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
21622 /* Make sure we don't append the type to the template twice. */
21623 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
21624 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
21627 append_type_to_template_for_access_check_1 (templ
, type_decl
,
21631 /* Set up the hash tables for template instantiations. */
21634 init_template_processing (void)
21636 decl_specializations
= htab_create_ggc (37,
21637 hash_specialization
,
21638 eq_specializations
,
21640 type_specializations
= htab_create_ggc (37,
21641 hash_specialization
,
21642 eq_specializations
,
21646 /* Print stats about the template hash tables for -fstats. */
21649 print_template_statistics (void)
21651 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
21652 "%f collisions\n", (long) htab_size (decl_specializations
),
21653 (long) htab_elements (decl_specializations
),
21654 htab_collisions (decl_specializations
));
21655 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
21656 "%f collisions\n", (long) htab_size (type_specializations
),
21657 (long) htab_elements (type_specializations
),
21658 htab_collisions (type_specializations
));
21661 #include "gt-cp-pt.h"