1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
5 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
6 Rewritten by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* Known bugs or deficiencies include:
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
31 #include "coretypes.h"
35 #include "pointer-set.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "cp-objcp-common.h"
41 #include "tree-inline.h"
45 #include "tree-iterator.h"
47 /* The type of functions taking a tree, and some additional data, and
49 typedef int (*tree_fn_t
) (tree
, void*);
51 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
52 instantiations have been deferred, either because their definitions
53 were not yet available, or because we were putting off doing the work. */
54 struct GTY ((chain_next ("%h.next"))) pending_template
{
55 struct pending_template
*next
;
56 struct tinst_level
*tinst
;
59 static GTY(()) struct pending_template
*pending_templates
;
60 static GTY(()) struct pending_template
*last_pending_template
;
62 int processing_template_parmlist
;
63 static int template_header_count
;
65 static GTY(()) tree saved_trees
;
66 static vec
<int> inline_parm_levels
;
68 static GTY(()) struct tinst_level
*current_tinst_level
;
70 static GTY(()) tree saved_access_scope
;
72 /* Live only within one (recursive) call to tsubst_expr. We use
73 this to pass the statement expression node from the STMT_EXPR
74 to the EXPR_STMT that is its result. */
75 static tree cur_stmt_expr
;
77 /* A map from local variable declarations in the body of the template
78 presently being instantiated to the corresponding instantiated
80 static struct pointer_map_t
*local_specializations
;
82 /* True if we've recursed into fn_type_unification too many times. */
83 static bool excessive_deduction_depth
;
85 typedef struct GTY(()) spec_entry
92 static GTY ((param_is (spec_entry
)))
93 htab_t decl_specializations
;
95 static GTY ((param_is (spec_entry
)))
96 htab_t type_specializations
;
98 /* Contains canonical template parameter types. The vector is indexed by
99 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
100 TREE_LIST, whose TREE_VALUEs contain the canonical template
101 parameters of various types and levels. */
102 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
104 #define UNIFY_ALLOW_NONE 0
105 #define UNIFY_ALLOW_MORE_CV_QUAL 1
106 #define UNIFY_ALLOW_LESS_CV_QUAL 2
107 #define UNIFY_ALLOW_DERIVED 4
108 #define UNIFY_ALLOW_INTEGER 8
109 #define UNIFY_ALLOW_OUTER_LEVEL 16
110 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
111 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
113 enum template_base_result
{
115 tbr_ambiguous_baseclass
,
119 static void push_access_scope (tree
);
120 static void pop_access_scope (tree
);
121 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
122 unification_kind_t
, int,
124 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
125 unification_kind_t
, int, bool, bool);
126 static int unify (tree
, tree
, tree
, tree
, int, bool);
127 static void add_pending_template (tree
);
128 static tree
reopen_tinst_level (struct tinst_level
*);
129 static tree
tsubst_initializer_list (tree
, tree
);
130 static tree
get_class_bindings (tree
, tree
, tree
, tree
);
131 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
133 static void tsubst_enum (tree
, tree
, tree
);
134 static tree
add_to_template_args (tree
, tree
);
135 static tree
add_outermost_template_args (tree
, tree
);
136 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
137 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
139 static int type_unification_real (tree
, tree
, tree
, const tree
*,
140 unsigned int, int, unification_kind_t
, int,
142 static void note_template_header (int);
143 static tree
convert_nontype_argument_function (tree
, tree
);
144 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
145 static tree
convert_template_argument (tree
, tree
, tree
,
146 tsubst_flags_t
, int, tree
);
147 static int for_each_template_parm (tree
, tree_fn_t
, void*,
148 struct pointer_set_t
*, bool);
149 static tree
expand_template_argument_pack (tree
);
150 static tree
build_template_parm_index (int, int, int, tree
, tree
);
151 static bool inline_needs_template_parms (tree
);
152 static void push_inline_template_parms_recursive (tree
, int);
153 static tree
retrieve_local_specialization (tree
);
154 static void register_local_specialization (tree
, tree
);
155 static hashval_t
hash_specialization (const void *p
);
156 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
157 static int mark_template_parm (tree
, void *);
158 static int template_parm_this_level_p (tree
, void *);
159 static tree
tsubst_friend_function (tree
, tree
);
160 static tree
tsubst_friend_class (tree
, tree
);
161 static int can_complete_type_without_circularity (tree
);
162 static tree
get_bindings (tree
, tree
, tree
, bool);
163 static int template_decl_level (tree
);
164 static int check_cv_quals_for_unify (int, tree
, tree
);
165 static void template_parm_level_and_index (tree
, int*, int*);
166 static int unify_pack_expansion (tree
, tree
, tree
,
167 tree
, unification_kind_t
, bool, bool);
168 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
169 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
170 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
171 static void regenerate_decl_from_template (tree
, tree
);
172 static tree
most_specialized_class (tree
, tree
, tsubst_flags_t
);
173 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
174 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
175 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
176 static bool check_specialization_scope (void);
177 static tree
process_partial_specialization (tree
);
178 static void set_current_access_from_decl (tree
);
179 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
181 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
182 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
184 static bool template_template_parm_bindings_ok_p (tree
, tree
);
185 static int template_args_equal (tree
, tree
);
186 static void tsubst_default_arguments (tree
);
187 static tree
for_each_template_parm_r (tree
*, int *, void *);
188 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
189 static void copy_default_args_to_explicit_spec (tree
);
190 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
191 static bool dependent_template_arg_p (tree
);
192 static bool any_template_arguments_need_structural_equality_p (tree
);
193 static bool dependent_type_p_r (tree
);
194 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
, bool);
195 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
196 static tree
tsubst_pack_expansion (tree
, tree
, tsubst_flags_t
, tree
);
197 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
198 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
199 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
201 static tree
listify (tree
);
202 static tree
listify_autos (tree
, tree
);
203 static tree
template_parm_to_arg (tree t
);
204 static bool arg_from_parm_pack_p (tree
, tree
);
205 static tree
current_template_args (void);
206 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
207 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
209 /* Make the current scope suitable for access checking when we are
210 processing T. T can be FUNCTION_DECL for instantiated function
211 template, VAR_DECL for static member variable, or TYPE_DECL for
212 alias template (needed by instantiate_decl). */
215 push_access_scope (tree t
)
217 gcc_assert (TREE_CODE (t
) == FUNCTION_DECL
218 || TREE_CODE (t
) == TYPE_DECL
219 || TREE_CODE (t
) == VAR_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 (TAGGED_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 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
808 error ("name of class shadows template template parameter %qD",
810 return error_mark_node
;
813 context
= TYPE_CONTEXT (type
);
815 if (TYPE_ALIAS_P (type
))
817 if (TYPE_TEMPLATE_INFO (type
)
818 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type
)))
819 error ("specialization of alias template %qD",
820 TYPE_TI_TEMPLATE (type
));
822 error ("explicit specialization of non-template %qT", type
);
823 return error_mark_node
;
825 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
827 /* This is for ordinary explicit specialization and partial
828 specialization of a template class such as:
830 template <> class C<int>;
834 template <class T> class C<T*>;
836 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
838 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
839 && !COMPLETE_TYPE_P (type
))
841 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
));
842 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
843 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
844 if (processing_template_decl
)
846 if (push_template_decl (TYPE_MAIN_DECL (type
))
848 return error_mark_node
;
851 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
852 error ("specialization of %qT after instantiation", type
);
855 else if (CLASS_TYPE_P (type
)
856 && !CLASSTYPE_USE_TEMPLATE (type
)
857 && CLASSTYPE_TEMPLATE_INFO (type
)
858 && context
&& CLASS_TYPE_P (context
)
859 && CLASSTYPE_TEMPLATE_INFO (context
))
861 /* This is for an explicit specialization of member class
862 template according to [temp.expl.spec/18]:
864 template <> template <class U> class C<int>::D;
866 The context `C<int>' must be an implicit instantiation.
867 Otherwise this is just a member class template declared
870 template <> class C<int> { template <class U> class D; };
871 template <> template <class U> class C<int>::D;
873 In the first case, `C<int>::D' is a specialization of `C<T>::D'
874 while in the second case, `C<int>::D' is a primary template
875 and `C<T>::D' may not exist. */
877 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
878 && !COMPLETE_TYPE_P (type
))
881 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
883 if (current_namespace
884 != decl_namespace_context (tmpl
))
886 permerror (input_location
, "specializing %q#T in different namespace", type
);
887 permerror (input_location
, " from definition of %q+#D", tmpl
);
890 /* Check for invalid specialization after instantiation:
892 template <> template <> class C<int>::D<int>;
893 template <> template <class U> class C<int>::D; */
895 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
896 t
; t
= TREE_CHAIN (t
))
898 tree inst
= TREE_VALUE (t
);
899 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
))
901 /* We already have a full specialization of this partial
902 instantiation. Reassign it to the new member
903 specialization template. */
908 elt
.tmpl
= most_general_template (tmpl
);
909 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
912 htab_remove_elt (type_specializations
, &elt
);
915 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
917 slot
= htab_find_slot (type_specializations
, &elt
, INSERT
);
918 entry
= ggc_alloc_spec_entry ();
922 else if (COMPLETE_OR_OPEN_TYPE_P (inst
))
923 /* But if we've had an implicit instantiation, that's a
924 problem ([temp.expl.spec]/6). */
925 error ("specialization %qT after instantiation %qT",
929 /* Mark TYPE as a specialization. And as a result, we only
930 have one level of template argument for the innermost
932 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
933 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
934 CLASSTYPE_TI_ARGS (type
)
935 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
938 else if (processing_specialization
)
940 /* Someday C++0x may allow for enum template specialization. */
941 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
942 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
943 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
944 "of %qD not allowed by ISO C++", type
);
947 error ("explicit specialization of non-template %qT", type
);
948 return error_mark_node
;
955 /* Returns nonzero if we can optimize the retrieval of specializations
956 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
957 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
960 optimize_specialization_lookup_p (tree tmpl
)
962 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
963 && DECL_CLASS_SCOPE_P (tmpl
)
964 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
966 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
967 /* The optimized lookup depends on the fact that the
968 template arguments for the member function template apply
969 purely to the containing class, which is not true if the
970 containing class is an explicit or partial
972 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
973 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
974 && !DECL_CONV_FN_P (tmpl
)
975 /* It is possible to have a template that is not a member
976 template and is not a member of a template class:
978 template <typename T>
979 struct S { friend A::f(); };
981 Here, the friend function is a template, but the context does
982 not have template information. The optimized lookup relies
983 on having ARGS be the template arguments for both the class
984 and the function template. */
985 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
988 /* Retrieve the specialization (in the sense of [temp.spec] - a
989 specialization is either an instantiation or an explicit
990 specialization) of TMPL for the given template ARGS. If there is
991 no such specialization, return NULL_TREE. The ARGS are a vector of
992 arguments, or a vector of vectors of arguments, in the case of
993 templates with more than one level of parameters.
995 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
996 then we search for a partial specialization matching ARGS. This
997 parameter is ignored if TMPL is not a class template. */
1000 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1002 if (args
== error_mark_node
)
1005 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
1007 /* There should be as many levels of arguments as there are
1008 levels of parameters. */
1009 gcc_assert (TMPL_ARGS_DEPTH (args
)
1010 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
1012 if (optimize_specialization_lookup_p (tmpl
))
1014 tree class_template
;
1015 tree class_specialization
;
1016 vec
<tree
, va_gc
> *methods
;
1020 /* The template arguments actually apply to the containing
1021 class. Find the class specialization with those
1023 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1024 class_specialization
1025 = retrieve_specialization (class_template
, args
, 0);
1026 if (!class_specialization
)
1028 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1029 for the specialization. */
1030 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1033 /* Iterate through the methods with the indicated name, looking
1034 for the one that has an instance of TMPL. */
1035 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1036 for (fns
= (*methods
)[idx
]; fns
; fns
= OVL_NEXT (fns
))
1038 tree fn
= OVL_CURRENT (fns
);
1039 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1040 /* using-declarations can add base methods to the method vec,
1041 and we don't want those here. */
1042 && DECL_CONTEXT (fn
) == class_specialization
)
1051 htab_t specializations
;
1055 elt
.spec
= NULL_TREE
;
1057 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1058 specializations
= type_specializations
;
1060 specializations
= decl_specializations
;
1063 hash
= hash_specialization (&elt
);
1064 found
= (spec_entry
*) htab_find_with_hash (specializations
, &elt
, hash
);
1072 /* Like retrieve_specialization, but for local declarations. */
1075 retrieve_local_specialization (tree tmpl
)
1079 if (local_specializations
== NULL
)
1082 slot
= pointer_map_contains (local_specializations
, tmpl
);
1083 return slot
? (tree
) *slot
: NULL_TREE
;
1086 /* Returns nonzero iff DECL is a specialization of TMPL. */
1089 is_specialization_of (tree decl
, tree tmpl
)
1093 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1097 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1103 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1105 for (t
= TREE_TYPE (decl
);
1107 t
= CLASSTYPE_USE_TEMPLATE (t
)
1108 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1109 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1116 /* Returns nonzero iff DECL is a specialization of friend declaration
1117 FRIEND_DECL according to [temp.friend]. */
1120 is_specialization_of_friend (tree decl
, tree friend_decl
)
1122 bool need_template
= true;
1125 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1126 || TREE_CODE (decl
) == TYPE_DECL
);
1128 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1129 of a template class, we want to check if DECL is a specialization
1131 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1132 && DECL_TEMPLATE_INFO (friend_decl
)
1133 && !DECL_USE_TEMPLATE (friend_decl
))
1135 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1136 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1137 need_template
= false;
1139 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1140 && !PRIMARY_TEMPLATE_P (friend_decl
))
1141 need_template
= false;
1143 /* There is nothing to do if this is not a template friend. */
1144 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1147 if (is_specialization_of (decl
, friend_decl
))
1151 A member of a class template may be declared to be a friend of a
1152 non-template class. In this case, the corresponding member of
1153 every specialization of the class template is a friend of the
1154 class granting friendship.
1156 For example, given a template friend declaration
1158 template <class T> friend void A<T>::f();
1160 the member function below is considered a friend
1162 template <> struct A<int> {
1166 For this type of template friend, TEMPLATE_DEPTH below will be
1167 nonzero. To determine if DECL is a friend of FRIEND, we first
1168 check if the enclosing class is a specialization of another. */
1170 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1172 && DECL_CLASS_SCOPE_P (decl
)
1173 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1174 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1176 /* Next, we check the members themselves. In order to handle
1177 a few tricky cases, such as when FRIEND_DECL's are
1179 template <class T> friend void A<T>::g(T t);
1180 template <class T> template <T t> friend void A<T>::h();
1184 void A<int>::g(int);
1185 template <int> void A<int>::h();
1187 we need to figure out ARGS, the template arguments from
1188 the context of DECL. This is required for template substitution
1189 of `T' in the function parameter of `g' and template parameter
1190 of `h' in the above examples. Here ARGS corresponds to `int'. */
1192 tree context
= DECL_CONTEXT (decl
);
1193 tree args
= NULL_TREE
;
1194 int current_depth
= 0;
1196 while (current_depth
< template_depth
)
1198 if (CLASSTYPE_TEMPLATE_INFO (context
))
1200 if (current_depth
== 0)
1201 args
= TYPE_TI_ARGS (context
);
1203 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1206 context
= TYPE_CONTEXT (context
);
1209 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1214 tree friend_args_type
;
1215 tree decl_args_type
;
1217 /* Make sure that both DECL and FRIEND_DECL are templates or
1219 is_template
= DECL_TEMPLATE_INFO (decl
)
1220 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1221 if (need_template
^ is_template
)
1223 else if (is_template
)
1225 /* If both are templates, check template parameter list. */
1227 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1229 if (!comp_template_parms
1230 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1234 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1237 decl_type
= TREE_TYPE (decl
);
1239 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1240 tf_none
, NULL_TREE
);
1241 if (friend_type
== error_mark_node
)
1244 /* Check if return types match. */
1245 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1248 /* Check if function parameter types match, ignoring the
1249 `this' parameter. */
1250 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1251 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1252 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1253 friend_args_type
= TREE_CHAIN (friend_args_type
);
1254 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1255 decl_args_type
= TREE_CHAIN (decl_args_type
);
1257 return compparms (decl_args_type
, friend_args_type
);
1261 /* DECL is a TYPE_DECL */
1263 tree decl_type
= TREE_TYPE (decl
);
1265 /* Make sure that both DECL and FRIEND_DECL are templates or
1268 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1269 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1271 if (need_template
^ is_template
)
1273 else if (is_template
)
1276 /* If both are templates, check the name of the two
1277 TEMPLATE_DECL's first because is_friend didn't. */
1278 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1279 != DECL_NAME (friend_decl
))
1282 /* Now check template parameter list. */
1284 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1286 return comp_template_parms
1287 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1291 return (DECL_NAME (decl
)
1292 == DECL_NAME (friend_decl
));
1298 /* Register the specialization SPEC as a specialization of TMPL with
1299 the indicated ARGS. IS_FRIEND indicates whether the specialization
1300 is actually just a friend declaration. Returns SPEC, or an
1301 equivalent prior declaration, if available. */
1304 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1311 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
));
1313 if (TREE_CODE (spec
) == FUNCTION_DECL
1314 && uses_template_parms (DECL_TI_ARGS (spec
)))
1315 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1316 register it; we want the corresponding TEMPLATE_DECL instead.
1317 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1318 the more obvious `uses_template_parms (spec)' to avoid problems
1319 with default function arguments. In particular, given
1320 something like this:
1322 template <class T> void f(T t1, T t = T())
1324 the default argument expression is not substituted for in an
1325 instantiation unless and until it is actually needed. */
1328 if (optimize_specialization_lookup_p (tmpl
))
1329 /* We don't put these specializations in the hash table, but we might
1330 want to give an error about a mismatch. */
1331 fn
= retrieve_specialization (tmpl
, args
, 0);
1339 hash
= hash_specialization (&elt
);
1342 htab_find_slot_with_hash (decl_specializations
, &elt
, hash
, INSERT
);
1344 fn
= ((spec_entry
*) *slot
)->spec
;
1349 /* We can sometimes try to re-register a specialization that we've
1350 already got. In particular, regenerate_decl_from_template calls
1351 duplicate_decls which will update the specialization list. But,
1352 we'll still get called again here anyhow. It's more convenient
1353 to simply allow this than to try to prevent it. */
1356 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1358 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1360 if (DECL_ODR_USED (fn
)
1361 || DECL_EXPLICIT_INSTANTIATION (fn
))
1363 error ("specialization of %qD after instantiation",
1365 return error_mark_node
;
1370 /* This situation should occur only if the first
1371 specialization is an implicit instantiation, the
1372 second is an explicit specialization, and the
1373 implicit instantiation has not yet been used. That
1374 situation can occur if we have implicitly
1375 instantiated a member function and then specialized
1378 We can also wind up here if a friend declaration that
1379 looked like an instantiation turns out to be a
1382 template <class T> void foo(T);
1383 class S { friend void foo<>(int) };
1384 template <> void foo(int);
1386 We transform the existing DECL in place so that any
1387 pointers to it become pointers to the updated
1390 If there was a definition for the template, but not
1391 for the specialization, we want this to look as if
1392 there were no definition, and vice versa. */
1393 DECL_INITIAL (fn
) = NULL_TREE
;
1394 duplicate_decls (spec
, fn
, is_friend
);
1395 /* The call to duplicate_decls will have applied
1398 An explicit specialization of a function template
1399 is inline only if it is explicitly declared to be,
1400 and independently of whether its function template
1403 to the primary function; now copy the inline bits to
1404 the various clones. */
1405 FOR_EACH_CLONE (clone
, fn
)
1407 DECL_DECLARED_INLINE_P (clone
)
1408 = DECL_DECLARED_INLINE_P (fn
);
1409 DECL_SOURCE_LOCATION (clone
)
1410 = DECL_SOURCE_LOCATION (fn
);
1412 check_specialization_namespace (tmpl
);
1417 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1419 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1420 /* Dup decl failed, but this is a new definition. Set the
1421 line number so any errors match this new
1423 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1429 return duplicate_decls (spec
, fn
, is_friend
);
1431 /* A specialization must be declared in the same namespace as the
1432 template it is specializing. */
1433 if (DECL_TEMPLATE_SPECIALIZATION (spec
)
1434 && !check_specialization_namespace (tmpl
))
1435 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1437 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1439 spec_entry
*entry
= ggc_alloc_spec_entry ();
1440 gcc_assert (tmpl
&& args
&& spec
);
1443 if (TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1444 && PRIMARY_TEMPLATE_P (tmpl
)
1445 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1446 /* TMPL is a forward declaration of a template function; keep a list
1447 of all specializations in case we need to reassign them to a friend
1448 template later in tsubst_friend_function. */
1449 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1450 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1456 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1457 TMPL and ARGS members, ignores SPEC. */
1460 eq_specializations (const void *p1
, const void *p2
)
1462 const spec_entry
*e1
= (const spec_entry
*)p1
;
1463 const spec_entry
*e2
= (const spec_entry
*)p2
;
1465 return (e1
->tmpl
== e2
->tmpl
1466 && comp_template_args (e1
->args
, e2
->args
));
1469 /* Returns a hash for a template TMPL and template arguments ARGS. */
1472 hash_tmpl_and_args (tree tmpl
, tree args
)
1474 hashval_t val
= DECL_UID (tmpl
);
1475 return iterative_hash_template_arg (args
, val
);
1478 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1482 hash_specialization (const void *p
)
1484 const spec_entry
*e
= (const spec_entry
*)p
;
1485 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1488 /* Recursively calculate a hash value for a template argument ARG, for use
1489 in the hash tables of template specializations. */
1492 iterative_hash_template_arg (tree arg
, hashval_t val
)
1494 unsigned HOST_WIDE_INT i
;
1495 enum tree_code code
;
1498 if (arg
== NULL_TREE
)
1499 return iterative_hash_object (arg
, val
);
1504 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1505 /* We can get one of these when re-hashing a previous entry in the middle
1506 of substituting into a pack expansion. Just look through it. */
1507 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1509 code
= TREE_CODE (arg
);
1510 tclass
= TREE_CODE_CLASS (code
);
1512 val
= iterative_hash_object (code
, val
);
1519 case IDENTIFIER_NODE
:
1520 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1524 int i
, len
= TREE_VEC_LENGTH (arg
);
1525 for (i
= 0; i
< len
; ++i
)
1526 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1530 case TYPE_PACK_EXPANSION
:
1531 case EXPR_PACK_EXPANSION
:
1532 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1533 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1535 case TYPE_ARGUMENT_PACK
:
1536 case NONTYPE_ARGUMENT_PACK
:
1537 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1540 for (; arg
; arg
= TREE_CHAIN (arg
))
1541 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1545 for (; arg
; arg
= OVL_NEXT (arg
))
1546 val
= iterative_hash_template_arg (OVL_CURRENT (arg
), val
);
1552 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1554 val
= iterative_hash_template_arg (field
, val
);
1555 val
= iterative_hash_template_arg (value
, val
);
1561 if (!DECL_ARTIFICIAL (arg
))
1563 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1564 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1566 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1569 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1572 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1573 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1575 case TEMPLATE_PARM_INDEX
:
1576 val
= iterative_hash_template_arg
1577 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1578 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1579 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1582 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1583 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1584 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1587 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1589 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1593 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1594 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1595 val
= iterative_hash_object (code
, val
);
1596 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1599 /* A lambda can't appear in a template arg, but don't crash on
1601 gcc_assert (seen_error ());
1605 case IMPLICIT_CONV_EXPR
:
1606 case STATIC_CAST_EXPR
:
1607 case REINTERPRET_CAST_EXPR
:
1608 case CONST_CAST_EXPR
:
1609 case DYNAMIC_CAST_EXPR
:
1611 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1612 /* Now hash operands as usual. */
1622 if (TYPE_CANONICAL (arg
))
1623 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1625 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1626 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1627 /* Otherwise just compare the types during lookup. */
1630 case tcc_declaration
:
1632 return iterative_hash_expr (arg
, val
);
1635 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1637 unsigned n
= cp_tree_operand_length (arg
);
1638 for (i
= 0; i
< n
; ++i
)
1639 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1647 /* Unregister the specialization SPEC as a specialization of TMPL.
1648 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1649 if the SPEC was listed as a specialization of TMPL.
1651 Note that SPEC has been ggc_freed, so we can't look inside it. */
1654 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1659 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1660 elt
.args
= TI_ARGS (tinfo
);
1661 elt
.spec
= NULL_TREE
;
1663 entry
= (spec_entry
*) htab_find (decl_specializations
, &elt
);
1666 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1667 gcc_assert (new_spec
!= NULL_TREE
);
1668 entry
->spec
= new_spec
;
1675 /* Like register_specialization, but for local declarations. We are
1676 registering SPEC, an instantiation of TMPL. */
1679 register_local_specialization (tree spec
, tree tmpl
)
1683 slot
= pointer_map_insert (local_specializations
, tmpl
);
1687 /* TYPE is a class type. Returns true if TYPE is an explicitly
1688 specialized class. */
1691 explicit_class_specialization_p (tree type
)
1693 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1695 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1698 /* Print the list of functions at FNS, going through all the overloads
1699 for each element of the list. Alternatively, FNS can not be a
1700 TREE_LIST, in which case it will be printed together with all the
1703 MORE and *STR should respectively be FALSE and NULL when the function
1704 is called from the outside. They are used internally on recursive
1705 calls. print_candidates manages the two parameters and leaves NULL
1706 in *STR when it ends. */
1709 print_candidates_1 (tree fns
, bool more
, const char **str
)
1712 char *spaces
= NULL
;
1714 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1715 if (TREE_CODE (fn
) == TREE_LIST
)
1717 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1718 print_candidates_1 (TREE_VALUE (fn2
),
1719 TREE_CHAIN (fn2
) || more
, str
);
1723 tree cand
= OVL_CURRENT (fn
);
1726 /* Pick the prefix string. */
1727 if (!more
&& !OVL_NEXT (fns
))
1729 inform (DECL_SOURCE_LOCATION (cand
),
1730 "candidate is: %#D", cand
);
1734 *str
= _("candidates are:");
1735 spaces
= get_spaces (*str
);
1737 inform (DECL_SOURCE_LOCATION (cand
), "%s %#D", *str
, cand
);
1738 *str
= spaces
? spaces
: *str
;
1748 /* Print the list of candidate FNS in an error message. FNS can also
1749 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1752 print_candidates (tree fns
)
1754 const char *str
= NULL
;
1755 print_candidates_1 (fns
, false, &str
);
1756 gcc_assert (str
== NULL
);
1759 /* Returns the template (one of the functions given by TEMPLATE_ID)
1760 which can be specialized to match the indicated DECL with the
1761 explicit template args given in TEMPLATE_ID. The DECL may be
1762 NULL_TREE if none is available. In that case, the functions in
1763 TEMPLATE_ID are non-members.
1765 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1766 specialization of a member template.
1768 The TEMPLATE_COUNT is the number of references to qualifying
1769 template classes that appeared in the name of the function. See
1770 check_explicit_specialization for a more accurate description.
1772 TSK indicates what kind of template declaration (if any) is being
1773 declared. TSK_TEMPLATE indicates that the declaration given by
1774 DECL, though a FUNCTION_DECL, has template parameters, and is
1775 therefore a template function.
1777 The template args (those explicitly specified and those deduced)
1778 are output in a newly created vector *TARGS_OUT.
1780 If it is impossible to determine the result, an error message is
1781 issued. The error_mark_node is returned to indicate failure. */
1784 determine_specialization (tree template_id
,
1787 int need_member_template
,
1793 tree explicit_targs
;
1794 tree candidates
= NULL_TREE
;
1795 /* A TREE_LIST of templates of which DECL may be a specialization.
1796 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1797 corresponding TREE_PURPOSE is the set of template arguments that,
1798 when used to instantiate the template, would produce a function
1799 with the signature of DECL. */
1800 tree templates
= NULL_TREE
;
1802 cp_binding_level
*b
;
1804 *targs_out
= NULL_TREE
;
1806 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1807 return error_mark_node
;
1809 /* We shouldn't be specializing a member template of an
1810 unspecialized class template; we already gave an error in
1811 check_specialization_scope, now avoid crashing. */
1812 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
1813 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
1815 gcc_assert (errorcount
);
1816 return error_mark_node
;
1819 fns
= TREE_OPERAND (template_id
, 0);
1820 explicit_targs
= TREE_OPERAND (template_id
, 1);
1822 if (fns
== error_mark_node
)
1823 return error_mark_node
;
1825 /* Check for baselinks. */
1826 if (BASELINK_P (fns
))
1827 fns
= BASELINK_FUNCTIONS (fns
);
1829 if (!is_overloaded_fn (fns
))
1831 error ("%qD is not a function template", fns
);
1832 return error_mark_node
;
1835 /* Count the number of template headers specified for this
1838 for (b
= current_binding_level
;
1839 b
->kind
== sk_template_parms
;
1843 for (; fns
; fns
= OVL_NEXT (fns
))
1845 tree fn
= OVL_CURRENT (fns
);
1847 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1849 tree decl_arg_types
;
1853 /* In case of explicit specialization, we need to check if
1854 the number of template headers appearing in the specialization
1855 is correct. This is usually done in check_explicit_specialization,
1856 but the check done there cannot be exhaustive when specializing
1857 member functions. Consider the following code:
1859 template <> void A<int>::f(int);
1860 template <> template <> void A<int>::f(int);
1862 Assuming that A<int> is not itself an explicit specialization
1863 already, the first line specializes "f" which is a non-template
1864 member function, whilst the second line specializes "f" which
1865 is a template member function. So both lines are syntactically
1866 correct, and check_explicit_specialization does not reject
1869 Here, we can do better, as we are matching the specialization
1870 against the declarations. We count the number of template
1871 headers, and we check if they match TEMPLATE_COUNT + 1
1872 (TEMPLATE_COUNT is the number of qualifying template classes,
1873 plus there must be another header for the member template
1876 Notice that if header_count is zero, this is not a
1877 specialization but rather a template instantiation, so there
1878 is no check we can perform here. */
1879 if (header_count
&& header_count
!= template_count
+ 1)
1882 /* Check that the number of template arguments at the
1883 innermost level for DECL is the same as for FN. */
1884 if (current_binding_level
->kind
== sk_template_parms
1885 && !current_binding_level
->explicit_spec_p
1886 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1887 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1888 (current_template_parms
))))
1891 /* DECL might be a specialization of FN. */
1892 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1893 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1895 /* For a non-static member function, we need to make sure
1896 that the const qualification is the same. Since
1897 get_bindings does not try to merge the "this" parameter,
1898 we must do the comparison explicitly. */
1899 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1900 && !same_type_p (TREE_VALUE (fn_arg_types
),
1901 TREE_VALUE (decl_arg_types
)))
1904 /* Skip the "this" parameter and, for constructors of
1905 classes with virtual bases, the VTT parameter. A
1906 full specialization of a constructor will have a VTT
1907 parameter, but a template never will. */
1909 = skip_artificial_parms_for (decl
, decl_arg_types
);
1911 = skip_artificial_parms_for (fn
, fn_arg_types
);
1913 /* Function templates cannot be specializations; there are
1914 no partial specializations of functions. Therefore, if
1915 the type of DECL does not match FN, there is no
1917 if (tsk
== tsk_template
)
1919 if (compparms (fn_arg_types
, decl_arg_types
))
1920 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1924 /* See whether this function might be a specialization of this
1925 template. Suppress access control because we might be trying
1926 to make this specialization a friend, and we have already done
1927 access control for the declaration of the specialization. */
1928 push_deferring_access_checks (dk_no_check
);
1929 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1930 pop_deferring_access_checks ();
1933 /* We cannot deduce template arguments that when used to
1934 specialize TMPL will produce DECL. */
1937 /* Make sure that the deduced arguments actually work. */
1938 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
1939 if (insttype
== error_mark_node
)
1942 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
1943 if (!compparms (fn_arg_types
, decl_arg_types
))
1946 /* Save this template, and the arguments deduced. */
1947 templates
= tree_cons (targs
, fn
, templates
);
1949 else if (need_member_template
)
1950 /* FN is an ordinary member function, and we need a
1951 specialization of a member template. */
1953 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1954 /* We can get IDENTIFIER_NODEs here in certain erroneous
1957 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1958 /* This is just an ordinary non-member function. Nothing can
1959 be a specialization of that. */
1961 else if (DECL_ARTIFICIAL (fn
))
1962 /* Cannot specialize functions that are created implicitly. */
1966 tree decl_arg_types
;
1968 /* This is an ordinary member function. However, since
1969 we're here, we can assume it's enclosing class is a
1970 template class. For example,
1972 template <typename T> struct S { void f(); };
1973 template <> void S<int>::f() {}
1975 Here, S<int>::f is a non-template, but S<int> is a
1976 template class. If FN has the same type as DECL, we
1977 might be in business. */
1979 if (!DECL_TEMPLATE_INFO (fn
))
1980 /* Its enclosing class is an explicit specialization
1981 of a template class. This is not a candidate. */
1984 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
1985 TREE_TYPE (TREE_TYPE (fn
))))
1986 /* The return types differ. */
1989 /* Adjust the type of DECL in case FN is a static member. */
1990 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1991 if (DECL_STATIC_FUNCTION_P (fn
)
1992 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1993 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1995 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1998 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2002 if (templates
&& TREE_CHAIN (templates
))
2008 It is possible for a specialization with a given function
2009 signature to be instantiated from more than one function
2010 template. In such cases, explicit specification of the
2011 template arguments must be used to uniquely identify the
2012 function template specialization being specialized.
2014 Note that here, there's no suggestion that we're supposed to
2015 determine which of the candidate templates is most
2016 specialized. However, we, also have:
2020 Partial ordering of overloaded function template
2021 declarations is used in the following contexts to select
2022 the function template to which a function template
2023 specialization refers:
2025 -- when an explicit specialization refers to a function
2028 So, we do use the partial ordering rules, at least for now.
2029 This extension can only serve to make invalid programs valid,
2030 so it's safe. And, there is strong anecdotal evidence that
2031 the committee intended the partial ordering rules to apply;
2032 the EDG front end has that behavior, and John Spicer claims
2033 that the committee simply forgot to delete the wording in
2034 [temp.expl.spec]. */
2035 tree tmpl
= most_specialized_instantiation (templates
);
2036 if (tmpl
!= error_mark_node
)
2039 TREE_CHAIN (templates
) = NULL_TREE
;
2043 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2045 error ("template-id %qD for %q+D does not match any template "
2046 "declaration", template_id
, decl
);
2047 if (header_count
&& header_count
!= template_count
+ 1)
2048 inform (input_location
, "saw %d %<template<>%>, need %d for "
2049 "specializing a member function template",
2050 header_count
, template_count
+ 1);
2051 return error_mark_node
;
2053 else if ((templates
&& TREE_CHAIN (templates
))
2054 || (candidates
&& TREE_CHAIN (candidates
))
2055 || (templates
&& candidates
))
2057 error ("ambiguous template specialization %qD for %q+D",
2059 candidates
= chainon (candidates
, templates
);
2060 print_candidates (candidates
);
2061 return error_mark_node
;
2064 /* We have one, and exactly one, match. */
2067 tree fn
= TREE_VALUE (candidates
);
2068 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2069 /* DECL is a re-declaration or partial instantiation of a template
2071 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2073 /* It was a specialization of an ordinary member function in a
2075 return DECL_TI_TEMPLATE (fn
);
2078 /* It was a specialization of a template. */
2079 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2080 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2082 *targs_out
= copy_node (targs
);
2083 SET_TMPL_ARGS_LEVEL (*targs_out
,
2084 TMPL_ARGS_DEPTH (*targs_out
),
2085 TREE_PURPOSE (templates
));
2088 *targs_out
= TREE_PURPOSE (templates
);
2089 return TREE_VALUE (templates
);
2092 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2093 but with the default argument values filled in from those in the
2097 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2100 tree new_spec_types
;
2105 if (spec_types
== void_list_node
)
2106 return void_list_node
;
2108 /* Substitute into the rest of the list. */
2110 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2111 TREE_CHAIN (tmpl_types
));
2113 /* Add the default argument for this parameter. */
2114 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2115 TREE_VALUE (spec_types
),
2119 /* DECL is an explicit specialization. Replicate default arguments
2120 from the template it specializes. (That way, code like:
2122 template <class T> void f(T = 3);
2123 template <> void f(double);
2126 works, as required.) An alternative approach would be to look up
2127 the correct default arguments at the call-site, but this approach
2128 is consistent with how implicit instantiations are handled. */
2131 copy_default_args_to_explicit_spec (tree decl
)
2136 tree new_spec_types
;
2140 tree object_type
= NULL_TREE
;
2141 tree in_charge
= NULL_TREE
;
2142 tree vtt
= NULL_TREE
;
2144 /* See if there's anything we need to do. */
2145 tmpl
= DECL_TI_TEMPLATE (decl
);
2146 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2147 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2148 if (TREE_PURPOSE (t
))
2153 old_type
= TREE_TYPE (decl
);
2154 spec_types
= TYPE_ARG_TYPES (old_type
);
2156 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2158 /* Remove the this pointer, but remember the object's type for
2160 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2161 spec_types
= TREE_CHAIN (spec_types
);
2162 tmpl_types
= TREE_CHAIN (tmpl_types
);
2164 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2166 /* DECL may contain more parameters than TMPL due to the extra
2167 in-charge parameter in constructors and destructors. */
2168 in_charge
= spec_types
;
2169 spec_types
= TREE_CHAIN (spec_types
);
2171 if (DECL_HAS_VTT_PARM_P (decl
))
2174 spec_types
= TREE_CHAIN (spec_types
);
2178 /* Compute the merged default arguments. */
2180 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2182 /* Compute the new FUNCTION_TYPE. */
2186 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2191 /* Put the in-charge parameter back. */
2192 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2193 TREE_VALUE (in_charge
),
2196 new_type
= build_method_type_directly (object_type
,
2197 TREE_TYPE (old_type
),
2201 new_type
= build_function_type (TREE_TYPE (old_type
),
2203 new_type
= cp_build_type_attribute_variant (new_type
,
2204 TYPE_ATTRIBUTES (old_type
));
2205 new_type
= build_exception_variant (new_type
,
2206 TYPE_RAISES_EXCEPTIONS (old_type
));
2207 TREE_TYPE (decl
) = new_type
;
2210 /* Return the number of template headers we expect to see for a definition
2211 or specialization of CTYPE or one of its non-template members. */
2214 num_template_headers_for_class (tree ctype
)
2216 int num_templates
= 0;
2218 while (ctype
&& CLASS_TYPE_P (ctype
))
2220 /* You're supposed to have one `template <...>' for every
2221 template class, but you don't need one for a full
2222 specialization. For example:
2224 template <class T> struct S{};
2225 template <> struct S<int> { void f(); };
2226 void S<int>::f () {}
2228 is correct; there shouldn't be a `template <>' for the
2229 definition of `S<int>::f'. */
2230 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2231 /* If CTYPE does not have template information of any
2232 kind, then it is not a template, nor is it nested
2233 within a template. */
2235 if (explicit_class_specialization_p (ctype
))
2237 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2240 ctype
= TYPE_CONTEXT (ctype
);
2243 return num_templates
;
2246 /* Do a simple sanity check on the template headers that precede the
2247 variable declaration DECL. */
2250 check_template_variable (tree decl
)
2252 tree ctx
= CP_DECL_CONTEXT (decl
);
2253 int wanted
= num_template_headers_for_class (ctx
);
2254 if (!TYPE_P (ctx
) || !CLASSTYPE_TEMPLATE_INFO (ctx
))
2255 permerror (DECL_SOURCE_LOCATION (decl
),
2256 "%qD is not a static data member of a class template", decl
);
2257 else if (template_header_count
> wanted
)
2259 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2260 "too many template headers for %D (should be %d)",
2262 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2263 inform (DECL_SOURCE_LOCATION (decl
),
2264 "members of an explicitly specialized class are defined "
2265 "without a template header");
2269 /* Check to see if the function just declared, as indicated in
2270 DECLARATOR, and in DECL, is a specialization of a function
2271 template. We may also discover that the declaration is an explicit
2272 instantiation at this point.
2274 Returns DECL, or an equivalent declaration that should be used
2275 instead if all goes well. Issues an error message if something is
2276 amiss. Returns error_mark_node if the error is not easily
2279 FLAGS is a bitmask consisting of the following flags:
2281 2: The function has a definition.
2282 4: The function is a friend.
2284 The TEMPLATE_COUNT is the number of references to qualifying
2285 template classes that appeared in the name of the function. For
2288 template <class T> struct S { void f(); };
2291 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2292 classes are not counted in the TEMPLATE_COUNT, so that in
2294 template <class T> struct S {};
2295 template <> struct S<int> { void f(); }
2296 template <> void S<int>::f();
2298 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2299 invalid; there should be no template <>.)
2301 If the function is a specialization, it is marked as such via
2302 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2303 is set up correctly, and it is added to the list of specializations
2304 for that template. */
2307 check_explicit_specialization (tree declarator
,
2312 int have_def
= flags
& 2;
2313 int is_friend
= flags
& 4;
2314 int specialization
= 0;
2315 int explicit_instantiation
= 0;
2316 int member_specialization
= 0;
2317 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2318 tree dname
= DECL_NAME (decl
);
2323 if (!processing_specialization
)
2326 tsk
= tsk_excessive_parms
;
2329 tsk
= current_tmpl_spec_kind (template_count
);
2334 if (processing_specialization
)
2337 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2339 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2342 /* This could be something like:
2344 template <class T> void f(T);
2345 class S { friend void f<>(int); } */
2349 /* This case handles bogus declarations like template <>
2350 template <class T> void f<int>(); */
2352 error ("template-id %qD in declaration of primary template",
2359 case tsk_invalid_member_spec
:
2360 /* The error has already been reported in
2361 check_specialization_scope. */
2362 return error_mark_node
;
2364 case tsk_invalid_expl_inst
:
2365 error ("template parameter list used in explicit instantiation");
2371 error ("definition provided for explicit instantiation");
2373 explicit_instantiation
= 1;
2376 case tsk_excessive_parms
:
2377 case tsk_insufficient_parms
:
2378 if (tsk
== tsk_excessive_parms
)
2379 error ("too many template parameter lists in declaration of %qD",
2381 else if (template_header_count
)
2382 error("too few template parameter lists in declaration of %qD", decl
);
2384 error("explicit specialization of %qD must be introduced by "
2385 "%<template <>%>", decl
);
2389 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2391 member_specialization
= 1;
2397 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2399 /* This case handles bogus declarations like template <>
2400 template <class T> void f<int>(); */
2402 if (uses_template_parms (declarator
))
2403 error ("function template partial specialization %qD "
2404 "is not allowed", declarator
);
2406 error ("template-id %qD in declaration of primary template",
2411 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2412 /* This is a specialization of a member template, without
2413 specialization the containing class. Something like:
2415 template <class T> struct S {
2416 template <class U> void f (U);
2418 template <> template <class U> void S<int>::f(U) {}
2420 That's a specialization -- but of the entire template. */
2428 if (specialization
|| member_specialization
)
2430 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2431 for (; t
; t
= TREE_CHAIN (t
))
2432 if (TREE_PURPOSE (t
))
2434 permerror (input_location
,
2435 "default argument specified in explicit specialization");
2440 if (specialization
|| member_specialization
|| explicit_instantiation
)
2442 tree tmpl
= NULL_TREE
;
2443 tree targs
= NULL_TREE
;
2445 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2446 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2450 gcc_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
);
2455 /* If there is no class context, the explicit instantiation
2456 must be at namespace scope. */
2457 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2459 /* Find the namespace binding, using the declaration
2461 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2463 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2465 error ("%qD is not a template function", dname
);
2466 fns
= error_mark_node
;
2470 tree fn
= OVL_CURRENT (fns
);
2471 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2472 CP_DECL_CONTEXT (fn
)))
2473 error ("%qD is not declared in %qD",
2474 decl
, current_namespace
);
2478 declarator
= lookup_template_function (fns
, NULL_TREE
);
2481 if (declarator
== error_mark_node
)
2482 return error_mark_node
;
2484 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2486 if (!explicit_instantiation
)
2487 /* A specialization in class scope. This is invalid,
2488 but the error will already have been flagged by
2489 check_specialization_scope. */
2490 return error_mark_node
;
2493 /* It's not valid to write an explicit instantiation in
2496 class C { template void f(); }
2498 This case is caught by the parser. However, on
2501 template class C { void f(); };
2503 (which is invalid) we can get here. The error will be
2510 else if (ctype
!= NULL_TREE
2511 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
2514 /* Find the list of functions in ctype that have the same
2515 name as the declared function. */
2516 tree name
= TREE_OPERAND (declarator
, 0);
2517 tree fns
= NULL_TREE
;
2520 if (constructor_name_p (name
, ctype
))
2522 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2524 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2525 : !CLASSTYPE_DESTRUCTORS (ctype
))
2527 /* From [temp.expl.spec]:
2529 If such an explicit specialization for the member
2530 of a class template names an implicitly-declared
2531 special member function (clause _special_), the
2532 program is ill-formed.
2534 Similar language is found in [temp.explicit]. */
2535 error ("specialization of implicitly-declared special member function");
2536 return error_mark_node
;
2539 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2542 if (!DECL_CONV_FN_P (decl
))
2544 idx
= lookup_fnfields_1 (ctype
, name
);
2546 fns
= (*CLASSTYPE_METHOD_VEC (ctype
))[idx
];
2550 vec
<tree
, va_gc
> *methods
;
2553 /* For a type-conversion operator, we cannot do a
2554 name-based lookup. We might be looking for `operator
2555 int' which will be a specialization of `operator T'.
2556 So, we find *all* the conversion operators, and then
2557 select from them. */
2560 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2562 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2563 methods
->iterate (idx
, &ovl
);
2566 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2567 /* There are no more conversion functions. */
2570 /* Glue all these conversion functions together
2571 with those we already have. */
2572 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2573 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2577 if (fns
== NULL_TREE
)
2579 error ("no member function %qD declared in %qT", name
, ctype
);
2580 return error_mark_node
;
2583 TREE_OPERAND (declarator
, 0) = fns
;
2586 /* Figure out what exactly is being specialized at this point.
2587 Note that for an explicit instantiation, even one for a
2588 member function, we cannot tell apriori whether the
2589 instantiation is for a member template, or just a member
2590 function of a template class. Even if a member template is
2591 being instantiated, the member template arguments may be
2592 elided if they can be deduced from the rest of the
2594 tmpl
= determine_specialization (declarator
, decl
,
2596 member_specialization
,
2600 if (!tmpl
|| tmpl
== error_mark_node
)
2601 /* We couldn't figure out what this declaration was
2603 return error_mark_node
;
2606 tree gen_tmpl
= most_general_template (tmpl
);
2608 if (explicit_instantiation
)
2610 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2611 is done by do_decl_instantiation later. */
2613 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2614 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2616 if (arg_depth
> parm_depth
)
2618 /* If TMPL is not the most general template (for
2619 example, if TMPL is a friend template that is
2620 injected into namespace scope), then there will
2621 be too many levels of TARGS. Remove some of them
2626 new_targs
= make_tree_vec (parm_depth
);
2627 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2628 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2629 = TREE_VEC_ELT (targs
, i
);
2633 return instantiate_template (tmpl
, targs
, tf_error
);
2636 /* If we thought that the DECL was a member function, but it
2637 turns out to be specializing a static member function,
2638 make DECL a static member function as well. */
2639 if (DECL_STATIC_FUNCTION_P (tmpl
)
2640 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2641 revert_static_member_fn (decl
);
2643 /* If this is a specialization of a member template of a
2644 template class, we want to return the TEMPLATE_DECL, not
2645 the specialization of it. */
2646 if (tsk
== tsk_template
)
2648 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2649 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2650 DECL_INITIAL (result
) = NULL_TREE
;
2654 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2655 DECL_SOURCE_LOCATION (result
)
2656 = DECL_SOURCE_LOCATION (decl
);
2657 /* We want to use the argument list specified in the
2658 definition, not in the original declaration. */
2659 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2660 for (parm
= DECL_ARGUMENTS (result
); parm
;
2661 parm
= DECL_CHAIN (parm
))
2662 DECL_CONTEXT (parm
) = result
;
2664 return register_specialization (tmpl
, gen_tmpl
, targs
,
2668 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2669 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2671 /* Inherit default function arguments from the template
2672 DECL is specializing. */
2673 copy_default_args_to_explicit_spec (decl
);
2675 /* This specialization has the same protection as the
2676 template it specializes. */
2677 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2678 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2680 /* 7.1.1-1 [dcl.stc]
2682 A storage-class-specifier shall not be specified in an
2683 explicit specialization...
2685 The parser rejects these, so unless action is taken here,
2686 explicit function specializations will always appear with
2689 The action recommended by the C++ CWG in response to C++
2690 defect report 605 is to make the storage class and linkage
2691 of the explicit specialization match the templated function:
2693 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2695 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2697 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2698 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2700 /* This specialization has the same linkage and visibility as
2701 the function template it specializes. */
2702 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2703 if (! TREE_PUBLIC (decl
))
2705 DECL_INTERFACE_KNOWN (decl
) = 1;
2706 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2708 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2709 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2711 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2712 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2716 /* If DECL is a friend declaration, declared using an
2717 unqualified name, the namespace associated with DECL may
2718 have been set incorrectly. For example, in:
2720 template <typename T> void f(T);
2722 struct S { friend void f<int>(int); }
2725 we will have set the DECL_CONTEXT for the friend
2726 declaration to N, rather than to the global namespace. */
2727 if (DECL_NAMESPACE_SCOPE_P (decl
))
2728 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2730 if (is_friend
&& !have_def
)
2731 /* This is not really a declaration of a specialization.
2732 It's just the name of an instantiation. But, it's not
2733 a request for an instantiation, either. */
2734 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2735 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2736 /* This is indeed a specialization. In case of constructors
2737 and destructors, we need in-charge and not-in-charge
2738 versions in V3 ABI. */
2739 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2741 /* Register this specialization so that we can find it
2743 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2750 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2751 parameters. These are represented in the same format used for
2752 DECL_TEMPLATE_PARMS. */
2755 comp_template_parms (const_tree parms1
, const_tree parms2
)
2760 if (parms1
== parms2
)
2763 for (p1
= parms1
, p2
= parms2
;
2764 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2765 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2767 tree t1
= TREE_VALUE (p1
);
2768 tree t2
= TREE_VALUE (p2
);
2771 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2772 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2774 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2777 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2779 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2780 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2782 /* If either of the template parameters are invalid, assume
2783 they match for the sake of error recovery. */
2784 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2787 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2790 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2791 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2792 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2794 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2799 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2800 /* One set of parameters has more parameters lists than the
2807 /* Determine whether PARM is a parameter pack. */
2810 template_parameter_pack_p (const_tree parm
)
2812 /* Determine if we have a non-type template parameter pack. */
2813 if (TREE_CODE (parm
) == PARM_DECL
)
2814 return (DECL_TEMPLATE_PARM_P (parm
)
2815 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2816 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
2817 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
2819 /* If this is a list of template parameters, we could get a
2820 TYPE_DECL or a TEMPLATE_DECL. */
2821 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2822 parm
= TREE_TYPE (parm
);
2824 /* Otherwise it must be a type template parameter. */
2825 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2826 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2827 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2830 /* Determine if T is a function parameter pack. */
2833 function_parameter_pack_p (const_tree t
)
2835 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2836 return FUNCTION_PARAMETER_PACK_P (t
);
2840 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2841 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2844 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2846 if (! primary_func_tmpl_inst
2847 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2848 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2851 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2854 /* Return true iff the function parameter PARAM_DECL was expanded
2855 from the function parameter pack PACK. */
2858 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
2860 if (DECL_ARTIFICIAL (param_decl
)
2861 || !function_parameter_pack_p (pack
))
2864 /* The parameter pack and its pack arguments have the same
2866 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
2869 /* Determine whether ARGS describes a variadic template args list,
2870 i.e., one that is terminated by a template argument pack. */
2873 template_args_variadic_p (tree args
)
2878 if (args
== NULL_TREE
)
2881 args
= INNERMOST_TEMPLATE_ARGS (args
);
2882 nargs
= TREE_VEC_LENGTH (args
);
2887 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2889 return ARGUMENT_PACK_P (last_parm
);
2892 /* Generate a new name for the parameter pack name NAME (an
2893 IDENTIFIER_NODE) that incorporates its */
2896 make_ith_pack_parameter_name (tree name
, int i
)
2898 /* Munge the name to include the parameter index. */
2899 #define NUMBUF_LEN 128
2900 char numbuf
[NUMBUF_LEN
];
2904 if (name
== NULL_TREE
)
2906 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
2907 newname_len
= IDENTIFIER_LENGTH (name
)
2908 + strlen (numbuf
) + 2;
2909 newname
= (char*)alloca (newname_len
);
2910 snprintf (newname
, newname_len
,
2911 "%s#%i", IDENTIFIER_POINTER (name
), i
);
2912 return get_identifier (newname
);
2915 /* Return true if T is a primary function, class or alias template
2919 primary_template_instantiation_p (const_tree t
)
2924 if (TREE_CODE (t
) == FUNCTION_DECL
)
2925 return DECL_LANG_SPECIFIC (t
)
2926 && DECL_TEMPLATE_INSTANTIATION (t
)
2927 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
2928 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
2929 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
2930 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
2931 else if (alias_template_specialization_p (t
))
2936 /* Return true if PARM is a template template parameter. */
2939 template_template_parameter_p (const_tree parm
)
2941 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
2944 /* Return true iff PARM is a DECL representing a type template
2948 template_type_parameter_p (const_tree parm
)
2951 && (TREE_CODE (parm
) == TYPE_DECL
2952 || TREE_CODE (parm
) == TEMPLATE_DECL
)
2953 && DECL_TEMPLATE_PARM_P (parm
));
2956 /* Return the template parameters of T if T is a
2957 primary template instantiation, NULL otherwise. */
2960 get_primary_template_innermost_parameters (const_tree t
)
2962 tree parms
= NULL
, template_info
= NULL
;
2964 if ((template_info
= get_template_info (t
))
2965 && primary_template_instantiation_p (t
))
2966 parms
= INNERMOST_TEMPLATE_PARMS
2967 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
2972 /* Return the template parameters of the LEVELth level from the full list
2973 of template parameters PARMS. */
2976 get_template_parms_at_level (tree parms
, int level
)
2980 || TREE_CODE (parms
) != TREE_LIST
2981 || level
> TMPL_PARMS_DEPTH (parms
))
2984 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
2985 if (TMPL_PARMS_DEPTH (p
) == level
)
2991 /* Returns the template arguments of T if T is a template instantiation,
2995 get_template_innermost_arguments (const_tree t
)
2997 tree args
= NULL
, template_info
= NULL
;
2999 if ((template_info
= get_template_info (t
))
3000 && TI_ARGS (template_info
))
3001 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3006 /* Return the argument pack elements of T if T is a template argument pack,
3010 get_template_argument_pack_elems (const_tree t
)
3012 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3013 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3016 return ARGUMENT_PACK_ARGS (t
);
3019 /* Structure used to track the progress of find_parameter_packs_r. */
3020 struct find_parameter_pack_data
3022 /* TREE_LIST that will contain all of the parameter packs found by
3024 tree
* parameter_packs
;
3026 /* Set of AST nodes that have been visited by the traversal. */
3027 struct pointer_set_t
*visited
;
3030 /* Identifies all of the argument packs that occur in a template
3031 argument and appends them to the TREE_LIST inside DATA, which is a
3032 find_parameter_pack_data structure. This is a subroutine of
3033 make_pack_expansion and uses_parameter_packs. */
3035 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3038 struct find_parameter_pack_data
* ppd
=
3039 (struct find_parameter_pack_data
*)data
;
3040 bool parameter_pack_p
= false;
3042 /* Handle type aliases/typedefs. */
3045 && TREE_CODE (TYPE_NAME (t
)) == TYPE_DECL
3046 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3048 if (TYPE_TEMPLATE_INFO (t
))
3049 cp_walk_tree (&TYPE_TI_ARGS (t
),
3050 &find_parameter_packs_r
,
3056 /* Identify whether this is a parameter pack or not. */
3057 switch (TREE_CODE (t
))
3059 case TEMPLATE_PARM_INDEX
:
3060 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3061 parameter_pack_p
= true;
3064 case TEMPLATE_TYPE_PARM
:
3065 t
= TYPE_MAIN_VARIANT (t
);
3066 case TEMPLATE_TEMPLATE_PARM
:
3067 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3068 parameter_pack_p
= true;
3072 if (FUNCTION_PARAMETER_PACK_P (t
))
3074 /* We don't want to walk into the type of a PARM_DECL,
3075 because we don't want to see the type parameter pack. */
3077 parameter_pack_p
= true;
3082 parameter_pack_p
= true;
3085 /* Not a parameter pack. */
3089 if (parameter_pack_p
)
3091 /* Add this parameter pack to the list. */
3092 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3096 cp_walk_tree (&TYPE_CONTEXT (t
),
3097 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3099 /* This switch statement will return immediately if we don't find a
3101 switch (TREE_CODE (t
))
3103 case TEMPLATE_PARM_INDEX
:
3106 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3107 /* Check the template itself. */
3108 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3109 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3110 /* Check the template arguments. */
3111 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3116 case TEMPLATE_TYPE_PARM
:
3117 case TEMPLATE_TEMPLATE_PARM
:
3124 if (TYPE_PTRMEMFUNC_P (t
))
3130 if (TYPE_TEMPLATE_INFO (t
))
3131 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
3132 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3139 cp_walk_tree (&TREE_TYPE (t
),
3140 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3144 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3149 case TYPE_PACK_EXPANSION
:
3150 case EXPR_PACK_EXPANSION
:
3155 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3160 case IDENTIFIER_NODE
:
3161 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3173 /* Determines if the expression or type T uses any parameter packs. */
3175 uses_parameter_packs (tree t
)
3177 tree parameter_packs
= NULL_TREE
;
3178 struct find_parameter_pack_data ppd
;
3179 ppd
.parameter_packs
= ¶meter_packs
;
3180 ppd
.visited
= pointer_set_create ();
3181 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3182 pointer_set_destroy (ppd
.visited
);
3183 return parameter_packs
!= NULL_TREE
;
3186 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3187 representation a base-class initializer into a parameter pack
3188 expansion. If all goes well, the resulting node will be an
3189 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3192 make_pack_expansion (tree arg
)
3195 tree parameter_packs
= NULL_TREE
;
3196 bool for_types
= false;
3197 struct find_parameter_pack_data ppd
;
3199 if (!arg
|| arg
== error_mark_node
)
3202 if (TREE_CODE (arg
) == TREE_LIST
)
3204 /* The only time we will see a TREE_LIST here is for a base
3205 class initializer. In this case, the TREE_PURPOSE will be a
3206 _TYPE node (representing the base class expansion we're
3207 initializing) and the TREE_VALUE will be a TREE_LIST
3208 containing the initialization arguments.
3210 The resulting expansion looks somewhat different from most
3211 expansions. Rather than returning just one _EXPANSION, we
3212 return a TREE_LIST whose TREE_PURPOSE is a
3213 TYPE_PACK_EXPANSION containing the bases that will be
3214 initialized. The TREE_VALUE will be identical to the
3215 original TREE_VALUE, which is a list of arguments that will
3216 be passed to each base. We do not introduce any new pack
3217 expansion nodes into the TREE_VALUE (although it is possible
3218 that some already exist), because the TREE_PURPOSE and
3219 TREE_VALUE all need to be expanded together with the same
3220 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3221 resulting TREE_PURPOSE will mention the parameter packs in
3222 both the bases and the arguments to the bases. */
3225 tree parameter_packs
= NULL_TREE
;
3227 /* Determine which parameter packs will be used by the base
3229 ppd
.visited
= pointer_set_create ();
3230 ppd
.parameter_packs
= ¶meter_packs
;
3231 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3234 if (parameter_packs
== NULL_TREE
)
3236 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3237 pointer_set_destroy (ppd
.visited
);
3238 return error_mark_node
;
3241 if (TREE_VALUE (arg
) != void_type_node
)
3243 /* Collect the sets of parameter packs used in each of the
3244 initialization arguments. */
3245 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3247 /* Determine which parameter packs will be expanded in this
3249 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3254 pointer_set_destroy (ppd
.visited
);
3256 /* Create the pack expansion type for the base type. */
3257 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3258 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3259 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3261 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3262 they will rarely be compared to anything. */
3263 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3265 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3268 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3271 /* Build the PACK_EXPANSION_* node. */
3273 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3274 : make_node (EXPR_PACK_EXPANSION
);
3275 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3276 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3278 /* Propagate type and const-expression information. */
3279 TREE_TYPE (result
) = TREE_TYPE (arg
);
3280 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3283 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3284 they will rarely be compared to anything. */
3285 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3287 /* Determine which parameter packs will be expanded. */
3288 ppd
.parameter_packs
= ¶meter_packs
;
3289 ppd
.visited
= pointer_set_create ();
3290 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3291 pointer_set_destroy (ppd
.visited
);
3293 /* Make sure we found some parameter packs. */
3294 if (parameter_packs
== NULL_TREE
)
3297 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3299 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3300 return error_mark_node
;
3302 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3304 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3309 /* Checks T for any "bare" parameter packs, which have not yet been
3310 expanded, and issues an error if any are found. This operation can
3311 only be done on full expressions or types (e.g., an expression
3312 statement, "if" condition, etc.), because we could have expressions like:
3314 foo(f(g(h(args)))...)
3316 where "args" is a parameter pack. check_for_bare_parameter_packs
3317 should not be called for the subexpressions args, h(args),
3318 g(h(args)), or f(g(h(args))), because we would produce erroneous
3321 Returns TRUE and emits an error if there were bare parameter packs,
3322 returns FALSE otherwise. */
3324 check_for_bare_parameter_packs (tree t
)
3326 tree parameter_packs
= NULL_TREE
;
3327 struct find_parameter_pack_data ppd
;
3329 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3332 if (TREE_CODE (t
) == TYPE_DECL
)
3335 ppd
.parameter_packs
= ¶meter_packs
;
3336 ppd
.visited
= pointer_set_create ();
3337 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3338 pointer_set_destroy (ppd
.visited
);
3340 if (parameter_packs
)
3342 error ("parameter packs not expanded with %<...%>:");
3343 while (parameter_packs
)
3345 tree pack
= TREE_VALUE (parameter_packs
);
3346 tree name
= NULL_TREE
;
3348 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3349 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3350 name
= TYPE_NAME (pack
);
3351 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3352 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3354 name
= DECL_NAME (pack
);
3357 inform (input_location
, " %qD", name
);
3359 inform (input_location
, " <anonymous>");
3361 parameter_packs
= TREE_CHAIN (parameter_packs
);
3370 /* Expand any parameter packs that occur in the template arguments in
3373 expand_template_argument_pack (tree args
)
3375 tree result_args
= NULL_TREE
;
3376 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3377 int num_result_args
= -1;
3378 int non_default_args_count
= -1;
3380 /* First, determine if we need to expand anything, and the number of
3381 slots we'll need. */
3382 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3384 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3385 if (arg
== NULL_TREE
)
3387 if (ARGUMENT_PACK_P (arg
))
3389 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3390 if (num_result_args
< 0)
3391 num_result_args
= in_arg
+ num_packed
;
3393 num_result_args
+= num_packed
;
3397 if (num_result_args
>= 0)
3402 /* If no expansion is necessary, we're done. */
3403 if (num_result_args
< 0)
3406 /* Expand arguments. */
3407 result_args
= make_tree_vec (num_result_args
);
3408 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3409 non_default_args_count
=
3410 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3411 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3413 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3414 if (ARGUMENT_PACK_P (arg
))
3416 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3417 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3418 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3419 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3420 if (non_default_args_count
> 0)
3421 non_default_args_count
+= num_packed
;
3425 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3429 if (non_default_args_count
>= 0)
3430 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3434 /* Checks if DECL shadows a template parameter.
3436 [temp.local]: A template-parameter shall not be redeclared within its
3437 scope (including nested scopes).
3439 Emits an error and returns TRUE if the DECL shadows a parameter,
3440 returns FALSE otherwise. */
3443 check_template_shadow (tree decl
)
3447 /* If we're not in a template, we can't possibly shadow a template
3449 if (!current_template_parms
)
3452 /* Figure out what we're shadowing. */
3453 if (TREE_CODE (decl
) == OVERLOAD
)
3454 decl
= OVL_CURRENT (decl
);
3455 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3457 /* If there's no previous binding for this name, we're not shadowing
3458 anything, let alone a template parameter. */
3462 /* If we're not shadowing a template parameter, we're done. Note
3463 that OLDDECL might be an OVERLOAD (or perhaps even an
3464 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3466 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3469 /* We check for decl != olddecl to avoid bogus errors for using a
3470 name inside a class. We check TPFI to avoid duplicate errors for
3471 inline member templates. */
3473 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
3476 error ("declaration of %q+#D", decl
);
3477 error (" shadows template parm %q+#D", olddecl
);
3481 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3482 ORIG_LEVEL, DECL, and TYPE. */
3485 build_template_parm_index (int index
,
3491 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3492 TEMPLATE_PARM_IDX (t
) = index
;
3493 TEMPLATE_PARM_LEVEL (t
) = level
;
3494 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3495 TEMPLATE_PARM_DECL (t
) = decl
;
3496 TREE_TYPE (t
) = type
;
3497 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3498 TREE_READONLY (t
) = TREE_READONLY (decl
);
3503 /* Find the canonical type parameter for the given template type
3504 parameter. Returns the canonical type parameter, which may be TYPE
3505 if no such parameter existed. */
3508 canonical_type_parameter (tree type
)
3511 int idx
= TEMPLATE_TYPE_IDX (type
);
3512 if (!canonical_template_parms
)
3513 vec_alloc (canonical_template_parms
, idx
+1);
3515 while (canonical_template_parms
->length () <= (unsigned)idx
)
3516 vec_safe_push (canonical_template_parms
, NULL_TREE
);
3518 list
= (*canonical_template_parms
)[idx
];
3519 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3520 list
= TREE_CHAIN (list
);
3523 return TREE_VALUE (list
);
3526 (*canonical_template_parms
)[idx
]
3527 = tree_cons (NULL_TREE
, type
,
3528 (*canonical_template_parms
)[idx
]);
3533 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3534 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3535 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3536 new one is created. */
3539 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3540 tsubst_flags_t complain
)
3542 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3543 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3544 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3545 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3547 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3550 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3551 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3552 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3553 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3554 DECL_ARTIFICIAL (decl
) = 1;
3555 SET_DECL_TEMPLATE_PARM_P (decl
);
3557 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3558 TEMPLATE_PARM_LEVEL (index
) - levels
,
3559 TEMPLATE_PARM_ORIG_LEVEL (index
),
3561 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3562 TEMPLATE_PARM_PARAMETER_PACK (t
)
3563 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3565 /* Template template parameters need this. */
3566 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3567 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3568 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3572 return TEMPLATE_PARM_DESCENDANTS (index
);
3575 /* Process information from new template parameter PARM and append it
3576 to the LIST being built. This new parameter is a non-type
3577 parameter iff IS_NON_TYPE is true. This new parameter is a
3578 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3579 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3580 parameter list PARM belongs to. This is used used to create a
3581 proper canonical type for the type of PARM that is to be created,
3582 iff PARM is a type. If the size is not known, this parameter shall
3586 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
3587 bool is_non_type
, bool is_parameter_pack
)
3594 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3595 defval
= TREE_PURPOSE (parm
);
3599 tree p
= tree_last (list
);
3601 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3604 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3605 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3607 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3617 parm
= TREE_VALUE (parm
);
3619 SET_DECL_TEMPLATE_PARM_P (parm
);
3621 if (TREE_TYPE (parm
) == error_mark_node
)
3623 err_parm_list
= build_tree_list (defval
, parm
);
3624 TREE_VALUE (err_parm_list
) = error_mark_node
;
3625 return chainon (list
, err_parm_list
);
3631 The top-level cv-qualifiers on the template-parameter are
3632 ignored when determining its type. */
3633 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3634 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3636 err_parm_list
= build_tree_list (defval
, parm
);
3637 TREE_VALUE (err_parm_list
) = error_mark_node
;
3638 return chainon (list
, err_parm_list
);
3641 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
)
3643 /* This template parameter is not a parameter pack, but it
3644 should be. Complain about "bare" parameter packs. */
3645 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3647 /* Recover by calling this a parameter pack. */
3648 is_parameter_pack
= true;
3652 /* A template parameter is not modifiable. */
3653 TREE_CONSTANT (parm
) = 1;
3654 TREE_READONLY (parm
) = 1;
3655 decl
= build_decl (parm_loc
,
3656 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3657 TREE_CONSTANT (decl
) = 1;
3658 TREE_READONLY (decl
) = 1;
3659 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3660 = build_template_parm_index (idx
, processing_template_decl
,
3661 processing_template_decl
,
3662 decl
, TREE_TYPE (parm
));
3664 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3665 = is_parameter_pack
;
3670 parm
= TREE_VALUE (TREE_VALUE (parm
));
3672 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3674 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3675 /* This is for distinguishing between real templates and template
3676 template parameters */
3677 TREE_TYPE (parm
) = t
;
3678 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3683 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3684 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3685 decl
= build_decl (parm_loc
,
3686 TYPE_DECL
, parm
, t
);
3689 TYPE_NAME (t
) = decl
;
3690 TYPE_STUB_DECL (t
) = decl
;
3692 TEMPLATE_TYPE_PARM_INDEX (t
)
3693 = build_template_parm_index (idx
, processing_template_decl
,
3694 processing_template_decl
,
3695 decl
, TREE_TYPE (parm
));
3696 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3697 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3699 DECL_ARTIFICIAL (decl
) = 1;
3700 SET_DECL_TEMPLATE_PARM_P (decl
);
3702 parm
= build_tree_list (defval
, parm
);
3703 return chainon (list
, parm
);
3706 /* The end of a template parameter list has been reached. Process the
3707 tree list into a parameter vector, converting each parameter into a more
3708 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3712 end_template_parm_list (tree parms
)
3716 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3718 current_template_parms
3719 = tree_cons (size_int (processing_template_decl
),
3720 saved_parmlist
, current_template_parms
);
3722 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3724 next
= TREE_CHAIN (parm
);
3725 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3726 TREE_CHAIN (parm
) = NULL_TREE
;
3729 --processing_template_parmlist
;
3731 return saved_parmlist
;
3734 /* end_template_decl is called after a template declaration is seen. */
3737 end_template_decl (void)
3739 reset_specialization ();
3741 if (! processing_template_decl
)
3744 /* This matches the pushlevel in begin_template_parm_list. */
3747 --processing_template_decl
;
3748 current_template_parms
= TREE_CHAIN (current_template_parms
);
3751 /* Takes a TREE_LIST representing a template parameter and convert it
3752 into an argument suitable to be passed to the type substitution
3753 functions. Note that If the TREE_LIST contains an error_mark
3754 node, the returned argument is error_mark_node. */
3757 template_parm_to_arg (tree t
)
3761 || TREE_CODE (t
) != TREE_LIST
)
3764 if (error_operand_p (TREE_VALUE (t
)))
3765 return error_mark_node
;
3769 if (TREE_CODE (t
) == TYPE_DECL
3770 || TREE_CODE (t
) == TEMPLATE_DECL
)
3774 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3776 /* Turn this argument into a TYPE_ARGUMENT_PACK
3777 with a single element, which expands T. */
3778 tree vec
= make_tree_vec (1);
3779 #ifdef ENABLE_CHECKING
3780 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3781 (vec
, TREE_VEC_LENGTH (vec
));
3783 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3785 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
3786 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3791 t
= DECL_INITIAL (t
);
3793 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3795 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3796 with a single element, which expands T. */
3797 tree vec
= make_tree_vec (1);
3798 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3799 #ifdef ENABLE_CHECKING
3800 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3801 (vec
, TREE_VEC_LENGTH (vec
));
3803 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3805 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3806 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3807 TREE_TYPE (t
) = type
;
3813 /* This function returns TRUE if PARM_PACK is a template parameter
3814 pack and if ARG_PACK is what template_parm_to_arg returned when
3815 passed PARM_PACK. */
3818 arg_from_parm_pack_p (tree arg_pack
, tree parm_pack
)
3820 /* For clarity in the comments below let's use the representation
3821 argument_pack<elements>' to denote an argument pack and its
3824 In the 'if' block below, we want to detect cases where
3825 ARG_PACK is argument_pack<PARM_PACK...>. I.e, we want to
3826 check if ARG_PACK is an argument pack which sole element is
3827 the expansion of PARM_PACK. That argument pack is typically
3828 created by template_parm_to_arg when passed a parameter
3832 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
)) == 1
3833 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0)))
3835 tree expansion
= TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0);
3836 tree pattern
= PACK_EXPANSION_PATTERN (expansion
);
3837 if ((TYPE_P (pattern
) && same_type_p (pattern
, parm_pack
))
3838 || (!TYPE_P (pattern
) && cp_tree_equal (parm_pack
, pattern
)))
3839 /* The argument pack that the parameter maps to is just an
3840 expansion of the parameter itself, such as one would
3841 find in the implicit typedef of a class inside the
3842 class itself. Consider this parameter "unsubstituted",
3843 so that we will maintain the outer pack expansion. */
3849 /* Given a set of template parameters, return them as a set of template
3850 arguments. The template parameters are represented as a TREE_VEC, in
3851 the form documented in cp-tree.h for template arguments. */
3854 template_parms_to_args (tree parms
)
3857 tree args
= NULL_TREE
;
3858 int length
= TMPL_PARMS_DEPTH (parms
);
3861 /* If there is only one level of template parameters, we do not
3862 create a TREE_VEC of TREE_VECs. Instead, we return a single
3863 TREE_VEC containing the arguments. */
3865 args
= make_tree_vec (length
);
3867 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
3869 tree a
= copy_node (TREE_VALUE (header
));
3872 TREE_TYPE (a
) = NULL_TREE
;
3873 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
3874 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
3876 #ifdef ENABLE_CHECKING
3877 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
3881 TREE_VEC_ELT (args
, --l
) = a
;
3886 if (length
> 1 && TREE_VEC_ELT (args
, 0) == NULL_TREE
)
3887 /* This can happen for template parms of a template template
3890 template<template<class T, class U> class TT> struct S;
3892 Consider the level of the parms of TT; T and U both have
3893 level 2; TT has no template parm of level 1. So in this case
3894 the first element of full_template_args is NULL_TREE. If we
3895 leave it like this TMPL_ARG_DEPTH on args returns 1 instead
3896 of 2. This will make tsubst wrongly consider that T and U
3897 have level 1. Instead, let's create a dummy vector as the
3898 first element of full_template_args so that TMPL_ARG_DEPTH
3899 returns the correct depth for args. */
3900 TREE_VEC_ELT (args
, 0) = make_tree_vec (1);
3904 /* Within the declaration of a template, return the currently active
3905 template parameters as an argument TREE_VEC. */
3908 current_template_args (void)
3910 return template_parms_to_args (current_template_parms
);
3913 /* Update the declared TYPE by doing any lookups which were thought to be
3914 dependent, but are not now that we know the SCOPE of the declarator. */
3917 maybe_update_decl_type (tree orig_type
, tree scope
)
3919 tree type
= orig_type
;
3921 if (type
== NULL_TREE
)
3924 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3925 type
= TREE_TYPE (type
);
3927 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
3928 && dependent_type_p (type
)
3929 /* Don't bother building up the args in this case. */
3930 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
3932 /* tsubst in the args corresponding to the template parameters,
3933 including auto if present. Most things will be unchanged, but
3934 make_typename_type and tsubst_qualified_id will resolve
3935 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3936 tree args
= current_template_args ();
3937 tree auto_node
= type_uses_auto (type
);
3941 tree auto_vec
= make_tree_vec (1);
3942 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
3943 args
= add_to_template_args (args
, auto_vec
);
3945 pushed
= push_scope (scope
);
3946 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
3951 if (type
== error_mark_node
)
3954 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3956 if (same_type_p (type
, TREE_TYPE (orig_type
)))
3959 type
= TYPE_NAME (type
);
3964 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3965 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3966 a member template. Used by push_template_decl below. */
3969 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
3971 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
3972 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
3973 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
3974 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3975 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
3980 struct template_parm_data
3982 /* The level of the template parameters we are currently
3986 /* The index of the specialization argument we are currently
3990 /* An array whose size is the number of template parameters. The
3991 elements are nonzero if the parameter has been used in any one
3992 of the arguments processed so far. */
3995 /* An array whose size is the number of template arguments. The
3996 elements are nonzero if the argument makes use of template
3997 parameters of this level. */
3998 int* arg_uses_template_parms
;
4001 /* Subroutine of push_template_decl used to see if each template
4002 parameter in a partial specialization is used in the explicit
4003 argument list. If T is of the LEVEL given in DATA (which is
4004 treated as a template_parm_data*), then DATA->PARMS is marked
4008 mark_template_parm (tree t
, void* data
)
4012 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4014 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4016 level
= TEMPLATE_PARM_LEVEL (t
);
4017 idx
= TEMPLATE_PARM_IDX (t
);
4021 level
= TEMPLATE_TYPE_LEVEL (t
);
4022 idx
= TEMPLATE_TYPE_IDX (t
);
4025 if (level
== tpd
->level
)
4027 tpd
->parms
[idx
] = 1;
4028 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4031 /* Return zero so that for_each_template_parm will continue the
4032 traversal of the tree; we want to mark *every* template parm. */
4036 /* Process the partial specialization DECL. */
4039 process_partial_specialization (tree decl
)
4041 tree type
= TREE_TYPE (decl
);
4042 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
4043 tree specargs
= CLASSTYPE_TI_ARGS (type
);
4044 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4045 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4048 int nargs
= TREE_VEC_LENGTH (inner_args
);
4051 bool did_error_intro
= false;
4052 struct template_parm_data tpd
;
4053 struct template_parm_data tpd2
;
4055 gcc_assert (current_template_parms
);
4057 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4058 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4060 /* We check that each of the template parameters given in the
4061 partial specialization is used in the argument list to the
4062 specialization. For example:
4064 template <class T> struct S;
4065 template <class T> struct S<T*>;
4067 The second declaration is OK because `T*' uses the template
4068 parameter T, whereas
4070 template <class T> struct S<int>;
4072 is no good. Even trickier is:
4083 The S2<T> declaration is actually invalid; it is a
4084 full-specialization. Of course,
4087 struct S2<T (*)(U)>;
4089 or some such would have been OK. */
4090 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4091 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4092 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4094 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4095 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4096 for (i
= 0; i
< nargs
; ++i
)
4098 tpd
.current_arg
= i
;
4099 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4100 &mark_template_parm
,
4103 /*include_nondeduced_p=*/false);
4105 for (i
= 0; i
< ntparms
; ++i
)
4106 if (tpd
.parms
[i
] == 0)
4108 /* One of the template parms was not used in the
4110 if (!did_error_intro
)
4112 error ("template parameters not used in partial specialization:");
4113 did_error_intro
= true;
4116 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4119 if (did_error_intro
)
4120 return error_mark_node
;
4122 /* [temp.class.spec]
4124 The argument list of the specialization shall not be identical to
4125 the implicit argument list of the primary template. */
4126 if (comp_template_args
4128 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4130 error ("partial specialization %qT does not specialize any template arguments", type
);
4132 /* A partial specialization that replaces multiple parameters of the
4133 primary template with a pack expansion is less specialized for those
4135 if (nargs
< DECL_NTPARMS (maintmpl
))
4137 error ("partial specialization is not more specialized than the "
4138 "primary template because it replaces multiple parameters "
4139 "with a pack expansion");
4140 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4144 /* [temp.class.spec]
4146 A partially specialized non-type argument expression shall not
4147 involve template parameters of the partial specialization except
4148 when the argument expression is a simple identifier.
4150 The type of a template parameter corresponding to a specialized
4151 non-type argument shall not be dependent on a parameter of the
4154 Also, we verify that pack expansions only occur at the
4155 end of the argument list. */
4156 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4158 for (i
= 0; i
< nargs
; ++i
)
4160 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4161 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4162 tree packed_args
= NULL_TREE
;
4165 if (ARGUMENT_PACK_P (arg
))
4167 /* Extract the arguments from the argument pack. We'll be
4168 iterating over these in the following loop. */
4169 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4170 len
= TREE_VEC_LENGTH (packed_args
);
4173 for (j
= 0; j
< len
; j
++)
4176 /* Get the Jth argument in the parameter pack. */
4177 arg
= TREE_VEC_ELT (packed_args
, j
);
4179 if (PACK_EXPANSION_P (arg
))
4181 /* Pack expansions must come at the end of the
4183 if ((packed_args
&& j
< len
- 1)
4184 || (!packed_args
&& i
< nargs
- 1))
4186 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4187 error ("parameter pack argument %qE must be at the "
4188 "end of the template argument list", arg
);
4190 error ("parameter pack argument %qT must be at the "
4191 "end of the template argument list", arg
);
4195 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4196 /* We only care about the pattern. */
4197 arg
= PACK_EXPANSION_PATTERN (arg
);
4199 if (/* These first two lines are the `non-type' bit. */
4201 && TREE_CODE (arg
) != TEMPLATE_DECL
4202 /* This next line is the `argument expression is not just a
4203 simple identifier' condition and also the `specialized
4204 non-type argument' bit. */
4205 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
4207 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4208 || (packed_args
&& uses_template_parms (arg
)))
4209 error ("template argument %qE involves template parameter(s)",
4213 /* Look at the corresponding template parameter,
4214 marking which template parameters its type depends
4216 tree type
= TREE_TYPE (parm
);
4220 /* We haven't yet initialized TPD2. Do so now. */
4221 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4222 /* The number of parameters here is the number in the
4223 main template, which, as checked in the assertion
4225 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4227 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4230 /* Mark the template parameters. But this time, we're
4231 looking for the template parameters of the main
4232 template, not in the specialization. */
4233 tpd2
.current_arg
= i
;
4234 tpd2
.arg_uses_template_parms
[i
] = 0;
4235 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4236 for_each_template_parm (type
,
4237 &mark_template_parm
,
4240 /*include_nondeduced_p=*/false);
4242 if (tpd2
.arg_uses_template_parms
[i
])
4244 /* The type depended on some template parameters.
4245 If they are fully specialized in the
4246 specialization, that's OK. */
4249 for (j
= 0; j
< nargs
; ++j
)
4250 if (tpd2
.parms
[j
] != 0
4251 && tpd
.arg_uses_template_parms
[j
])
4254 error_n (input_location
, count
,
4255 "type %qT of template argument %qE depends "
4256 "on a template parameter",
4257 "type %qT of template argument %qE depends "
4258 "on template parameters",
4267 /* We should only get here once. */
4268 gcc_assert (!COMPLETE_TYPE_P (type
));
4270 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4271 = tree_cons (specargs
, inner_parms
,
4272 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4273 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4275 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4276 inst
= TREE_CHAIN (inst
))
4278 tree inst_type
= TREE_VALUE (inst
);
4279 if (COMPLETE_TYPE_P (inst_type
)
4280 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type
))
4282 tree spec
= most_specialized_class (inst_type
, maintmpl
, tf_none
);
4283 if (spec
&& TREE_TYPE (spec
) == type
)
4284 permerror (input_location
,
4285 "partial specialization of %qT after instantiation "
4286 "of %qT", type
, inst_type
);
4293 /* Check that a template declaration's use of default arguments and
4294 parameter packs is not invalid. Here, PARMS are the template
4295 parameters. IS_PRIMARY is true if DECL is the thing declared by
4296 a primary template. IS_PARTIAL is true if DECL is a partial
4299 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4300 declaration (but not a definition); 1 indicates a declaration, 2
4301 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4302 emitted for extraneous default arguments.
4304 Returns TRUE if there were no errors found, FALSE otherwise. */
4307 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4308 bool is_partial
, int is_friend_decl
)
4311 int last_level_to_check
;
4313 bool no_errors
= true;
4317 A default template-argument shall not be specified in a
4318 function template declaration or a function template definition, nor
4319 in the template-parameter-list of the definition of a member of a
4322 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
4323 /* You can't have a function template declaration in a local
4324 scope, nor you can you define a member of a class template in a
4328 if (current_class_type
4329 && !TYPE_BEING_DEFINED (current_class_type
)
4330 && DECL_LANG_SPECIFIC (decl
)
4331 && DECL_DECLARES_FUNCTION_P (decl
)
4332 /* If this is either a friend defined in the scope of the class
4333 or a member function. */
4334 && (DECL_FUNCTION_MEMBER_P (decl
)
4335 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4336 : DECL_FRIEND_CONTEXT (decl
)
4337 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4339 /* And, if it was a member function, it really was defined in
4340 the scope of the class. */
4341 && (!DECL_FUNCTION_MEMBER_P (decl
)
4342 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4343 /* We already checked these parameters when the template was
4344 declared, so there's no need to do it again now. This function
4345 was defined in class scope, but we're processing it's body now
4346 that the class is complete. */
4349 /* Core issue 226 (C++0x only): the following only applies to class
4352 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
4356 If a template-parameter has a default template-argument, all
4357 subsequent template-parameters shall have a default
4358 template-argument supplied. */
4359 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4361 tree inner_parms
= TREE_VALUE (parm_level
);
4362 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4363 int seen_def_arg_p
= 0;
4366 for (i
= 0; i
< ntparms
; ++i
)
4368 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4370 if (parm
== error_mark_node
)
4373 if (TREE_PURPOSE (parm
))
4375 else if (seen_def_arg_p
4376 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4378 error ("no default argument for %qD", TREE_VALUE (parm
));
4379 /* For better subsequent error-recovery, we indicate that
4380 there should have been a default argument. */
4381 TREE_PURPOSE (parm
) = error_mark_node
;
4384 else if (!is_partial
4386 /* Don't complain about an enclosing partial
4388 && parm_level
== parms
4389 && TREE_CODE (decl
) == TYPE_DECL
4391 && template_parameter_pack_p (TREE_VALUE (parm
)))
4393 /* A primary class template can only have one
4394 parameter pack, at the end of the template
4397 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
4398 error ("parameter pack %qE must be at the end of the"
4399 " template parameter list", TREE_VALUE (parm
));
4401 error ("parameter pack %qT must be at the end of the"
4402 " template parameter list",
4403 TREE_TYPE (TREE_VALUE (parm
)));
4405 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4413 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4417 /* For an ordinary class template, default template arguments are
4418 allowed at the innermost level, e.g.:
4419 template <class T = int>
4421 but, in a partial specialization, they're not allowed even
4422 there, as we have in [temp.class.spec]:
4424 The template parameter list of a specialization shall not
4425 contain default template argument values.
4427 So, for a partial specialization, or for a function template
4428 (in C++98/C++03), we look at all of them. */
4431 /* But, for a primary class template that is not a partial
4432 specialization we look at all template parameters except the
4434 parms
= TREE_CHAIN (parms
);
4436 /* Figure out what error message to issue. */
4437 if (is_friend_decl
== 2)
4438 msg
= G_("default template arguments may not be used in function template "
4439 "friend re-declaration");
4440 else if (is_friend_decl
)
4441 msg
= G_("default template arguments may not be used in function template "
4442 "friend declarations");
4443 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4444 msg
= G_("default template arguments may not be used in function templates "
4445 "without -std=c++11 or -std=gnu++11");
4446 else if (is_partial
)
4447 msg
= G_("default template arguments may not be used in "
4448 "partial specializations");
4450 msg
= G_("default argument for template parameter for class enclosing %qD");
4452 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4453 /* If we're inside a class definition, there's no need to
4454 examine the parameters to the class itself. On the one
4455 hand, they will be checked when the class is defined, and,
4456 on the other, default arguments are valid in things like:
4457 template <class T = double>
4458 struct S { template <class U> void f(U); };
4459 Here the default argument for `S' has no bearing on the
4460 declaration of `f'. */
4461 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4463 /* Check everything. */
4464 last_level_to_check
= 0;
4466 for (parm_level
= parms
;
4467 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4468 parm_level
= TREE_CHAIN (parm_level
))
4470 tree inner_parms
= TREE_VALUE (parm_level
);
4474 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4475 for (i
= 0; i
< ntparms
; ++i
)
4477 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4480 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4485 if (is_friend_decl
== 2)
4492 /* Clear out the default argument so that we are not
4494 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4498 /* At this point, if we're still interested in issuing messages,
4499 they must apply to classes surrounding the object declared. */
4501 msg
= G_("default argument for template parameter for class "
4508 /* Worker for push_template_decl_real, called via
4509 for_each_template_parm. DATA is really an int, indicating the
4510 level of the parameters we are interested in. If T is a template
4511 parameter of that level, return nonzero. */
4514 template_parm_this_level_p (tree t
, void* data
)
4516 int this_level
= *(int *)data
;
4519 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4520 level
= TEMPLATE_PARM_LEVEL (t
);
4522 level
= TEMPLATE_TYPE_LEVEL (t
);
4523 return level
== this_level
;
4526 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4527 parameters given by current_template_args, or reuses a
4528 previously existing one, if appropriate. Returns the DECL, or an
4529 equivalent one, if it is replaced via a call to duplicate_decls.
4531 If IS_FRIEND is true, DECL is a friend declaration. */
4534 push_template_decl_real (tree decl
, bool is_friend
)
4542 int new_template_p
= 0;
4543 /* True if the template is a member template, in the sense of
4545 bool member_template_p
= false;
4547 if (decl
== error_mark_node
|| !current_template_parms
)
4548 return error_mark_node
;
4550 /* See if this is a partial specialization. */
4551 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
4552 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4553 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
4555 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4559 /* For a friend, we want the context of the friend function, not
4560 the type of which it is a friend. */
4561 ctx
= CP_DECL_CONTEXT (decl
);
4562 else if (CP_DECL_CONTEXT (decl
)
4563 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4564 /* In the case of a virtual function, we want the class in which
4566 ctx
= CP_DECL_CONTEXT (decl
);
4568 /* Otherwise, if we're currently defining some class, the DECL
4569 is assumed to be a member of the class. */
4570 ctx
= current_scope ();
4572 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4575 if (!DECL_CONTEXT (decl
))
4576 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4578 /* See if this is a primary template. */
4579 if (is_friend
&& ctx
)
4580 /* A friend template that specifies a class context, i.e.
4581 template <typename T> friend void A<T>::f();
4585 is_primary
= template_parm_scope_p ();
4589 if (DECL_CLASS_SCOPE_P (decl
))
4590 member_template_p
= true;
4591 if (TREE_CODE (decl
) == TYPE_DECL
4592 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4594 error ("template class without a name");
4595 return error_mark_node
;
4597 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4599 if (DECL_DESTRUCTOR_P (decl
))
4603 A destructor shall not be a member template. */
4604 error ("destructor %qD declared as member template", decl
);
4605 return error_mark_node
;
4607 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4608 && (!prototype_p (TREE_TYPE (decl
))
4609 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4610 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4611 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4612 == void_list_node
)))
4614 /* [basic.stc.dynamic.allocation]
4616 An allocation function can be a function
4617 template. ... Template allocation functions shall
4618 have two or more parameters. */
4619 error ("invalid template declaration of %qD", decl
);
4620 return error_mark_node
;
4623 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4624 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4626 else if (TREE_CODE (decl
) == TYPE_DECL
4627 && TYPE_DECL_ALIAS_P (decl
))
4628 /* alias-declaration */
4629 gcc_assert (!DECL_ARTIFICIAL (decl
));
4632 error ("template declaration of %q#D", decl
);
4633 return error_mark_node
;
4637 /* Check to see that the rules regarding the use of default
4638 arguments are not being violated. */
4639 check_default_tmpl_args (decl
, current_template_parms
,
4640 is_primary
, is_partial
, /*is_friend_decl=*/0);
4642 /* Ensure that there are no parameter packs in the type of this
4643 declaration that have not been expanded. */
4644 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4646 /* Check each of the arguments individually to see if there are
4647 any bare parameter packs. */
4648 tree type
= TREE_TYPE (decl
);
4649 tree arg
= DECL_ARGUMENTS (decl
);
4650 tree argtype
= TYPE_ARG_TYPES (type
);
4652 while (arg
&& argtype
)
4654 if (!FUNCTION_PARAMETER_PACK_P (arg
)
4655 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4657 /* This is a PARM_DECL that contains unexpanded parameter
4658 packs. We have already complained about this in the
4659 check_for_bare_parameter_packs call, so just replace
4660 these types with ERROR_MARK_NODE. */
4661 TREE_TYPE (arg
) = error_mark_node
;
4662 TREE_VALUE (argtype
) = error_mark_node
;
4665 arg
= DECL_CHAIN (arg
);
4666 argtype
= TREE_CHAIN (argtype
);
4669 /* Check for bare parameter packs in the return type and the
4670 exception specifiers. */
4671 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4672 /* Errors were already issued, set return type to int
4673 as the frontend doesn't expect error_mark_node as
4675 TREE_TYPE (type
) = integer_type_node
;
4676 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4677 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4679 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
4680 && TYPE_DECL_ALIAS_P (decl
))
4681 ? DECL_ORIGINAL_TYPE (decl
)
4682 : TREE_TYPE (decl
)))
4684 TREE_TYPE (decl
) = error_mark_node
;
4685 return error_mark_node
;
4689 return process_partial_specialization (decl
);
4691 args
= current_template_args ();
4694 || TREE_CODE (ctx
) == FUNCTION_DECL
4695 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4696 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4698 if (DECL_LANG_SPECIFIC (decl
)
4699 && DECL_TEMPLATE_INFO (decl
)
4700 && DECL_TI_TEMPLATE (decl
))
4701 tmpl
= DECL_TI_TEMPLATE (decl
);
4702 /* If DECL is a TYPE_DECL for a class-template, then there won't
4703 be DECL_LANG_SPECIFIC. The information equivalent to
4704 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4705 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4706 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4707 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4709 /* Since a template declaration already existed for this
4710 class-type, we must be redeclaring it here. Make sure
4711 that the redeclaration is valid. */
4712 redeclare_class_template (TREE_TYPE (decl
),
4713 current_template_parms
);
4714 /* We don't need to create a new TEMPLATE_DECL; just use the
4715 one we already had. */
4716 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4720 tmpl
= build_template_decl (decl
, current_template_parms
,
4724 if (DECL_LANG_SPECIFIC (decl
)
4725 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4727 /* A specialization of a member template of a template
4729 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4730 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4731 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4737 tree a
, t
, current
, parms
;
4739 tree tinfo
= get_template_info (decl
);
4743 error ("template definition of non-template %q#D", decl
);
4744 return error_mark_node
;
4747 tmpl
= TI_TEMPLATE (tinfo
);
4749 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4750 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4751 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4752 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4756 /* The declaration is a specialization of a member
4757 template, declared outside the class. Therefore, the
4758 innermost template arguments will be NULL, so we
4759 replace them with the arguments determined by the
4760 earlier call to check_explicit_specialization. */
4761 args
= DECL_TI_ARGS (decl
);
4764 = build_template_decl (decl
, current_template_parms
,
4766 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4767 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4768 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4769 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4770 DECL_TEMPLATE_INFO (new_tmpl
)
4771 = build_template_info (tmpl
, args
);
4773 register_specialization (new_tmpl
,
4774 most_general_template (tmpl
),
4780 /* Make sure the template headers we got make sense. */
4782 parms
= DECL_TEMPLATE_PARMS (tmpl
);
4783 i
= TMPL_PARMS_DEPTH (parms
);
4784 if (TMPL_ARGS_DEPTH (args
) != i
)
4786 error ("expected %d levels of template parms for %q#D, got %d",
4787 i
, decl
, TMPL_ARGS_DEPTH (args
));
4790 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
4792 a
= TMPL_ARGS_LEVEL (args
, i
);
4793 t
= INNERMOST_TEMPLATE_PARMS (parms
);
4795 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
4797 if (current
== decl
)
4798 error ("got %d template parameters for %q#D",
4799 TREE_VEC_LENGTH (a
), decl
);
4801 error ("got %d template parameters for %q#T",
4802 TREE_VEC_LENGTH (a
), current
);
4803 error (" but %d required", TREE_VEC_LENGTH (t
));
4804 return error_mark_node
;
4807 if (current
== decl
)
4809 else if (current
== NULL_TREE
)
4810 /* Can happen in erroneous input. */
4813 current
= (TYPE_P (current
)
4814 ? TYPE_CONTEXT (current
)
4815 : DECL_CONTEXT (current
));
4818 /* Check that the parms are used in the appropriate qualifying scopes
4819 in the declarator. */
4820 if (!comp_template_args
4822 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
4825 template arguments to %qD do not match original template %qD",
4826 decl
, DECL_TEMPLATE_RESULT (tmpl
));
4827 if (!uses_template_parms (TI_ARGS (tinfo
)))
4828 inform (input_location
, "use template<> for an explicit specialization");
4829 /* Avoid crash in import_export_decl. */
4830 DECL_INTERFACE_KNOWN (decl
) = 1;
4831 return error_mark_node
;
4835 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4836 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4838 /* Push template declarations for global functions and types. Note
4839 that we do not try to push a global template friend declared in a
4840 template class; such a thing may well depend on the template
4841 parameters of the class. */
4842 if (new_template_p
&& !ctx
4843 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
4845 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
4846 if (tmpl
== error_mark_node
)
4847 return error_mark_node
;
4849 /* Hide template friend classes that haven't been declared yet. */
4850 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
4852 DECL_ANTICIPATED (tmpl
) = 1;
4853 DECL_FRIEND_P (tmpl
) = 1;
4859 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
4862 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4863 if (DECL_CONV_FN_P (tmpl
))
4865 int depth
= TMPL_PARMS_DEPTH (parms
);
4867 /* It is a conversion operator. See if the type converted to
4868 depends on innermost template operands. */
4870 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
4872 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
4875 /* Give template template parms a DECL_CONTEXT of the template
4876 for which they are a parameter. */
4877 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4878 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
4880 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4881 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4882 DECL_CONTEXT (parm
) = tmpl
;
4886 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4887 back to its most general template. If TMPL is a specialization,
4888 ARGS may only have the innermost set of arguments. Add the missing
4889 argument levels if necessary. */
4890 if (DECL_TEMPLATE_INFO (tmpl
))
4891 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
4893 info
= build_template_info (tmpl
, args
);
4895 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
4896 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
4899 if (is_primary
&& !DECL_LANG_SPECIFIC (decl
))
4900 retrofit_lang_decl (decl
);
4901 if (DECL_LANG_SPECIFIC (decl
))
4902 DECL_TEMPLATE_INFO (decl
) = info
;
4905 return DECL_TEMPLATE_RESULT (tmpl
);
4909 push_template_decl (tree decl
)
4911 return push_template_decl_real (decl
, false);
4914 /* FN is an inheriting constructor that inherits from the constructor
4915 template INHERITED; turn FN into a constructor template with a matching
4919 add_inherited_template_parms (tree fn
, tree inherited
)
4922 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
4923 inner_parms
= copy_node (inner_parms
);
4925 = tree_cons (size_int (processing_template_decl
+ 1),
4926 inner_parms
, current_template_parms
);
4927 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
4928 tree args
= template_parms_to_args (parms
);
4929 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
4930 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
4931 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
4932 DECL_ARTIFICIAL (tmpl
) = true;
4933 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4937 /* Called when a class template TYPE is redeclared with the indicated
4938 template PARMS, e.g.:
4940 template <class T> struct S;
4941 template <class T> struct S {}; */
4944 redeclare_class_template (tree type
, tree parms
)
4950 if (!TYPE_TEMPLATE_INFO (type
))
4952 error ("%qT is not a template type", type
);
4956 tmpl
= TYPE_TI_TEMPLATE (type
);
4957 if (!PRIMARY_TEMPLATE_P (tmpl
))
4958 /* The type is nested in some template class. Nothing to worry
4959 about here; there are no new template parameters for the nested
4965 error ("template specifiers not specified in declaration of %qD",
4970 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4971 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
4973 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
4975 error_n (input_location
, TREE_VEC_LENGTH (parms
),
4976 "redeclared with %d template parameter",
4977 "redeclared with %d template parameters",
4978 TREE_VEC_LENGTH (parms
));
4979 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
4980 "previous declaration %q+D used %d template parameter",
4981 "previous declaration %q+D used %d template parameters",
4982 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
4986 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
4993 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
4994 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
4997 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
4998 if (tmpl_parm
== error_mark_node
)
5001 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5002 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5003 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5005 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5007 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5008 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5009 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5010 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5011 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5012 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5013 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5014 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5015 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5017 error ("template parameter %q+#D", tmpl_parm
);
5018 error ("redeclared here as %q#D", parm
);
5022 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5024 /* We have in [temp.param]:
5026 A template-parameter may not be given default arguments
5027 by two different declarations in the same scope. */
5028 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5029 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5030 "original definition appeared here");
5034 if (parm_default
!= NULL_TREE
)
5035 /* Update the previous template parameters (which are the ones
5036 that will really count) with the new default value. */
5037 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5038 else if (tmpl_default
!= NULL_TREE
)
5039 /* Update the new parameters, too; they'll be used as the
5040 parameters for any members. */
5041 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5047 /* Simplify EXPR if it is a non-dependent expression. Returns the
5048 (possibly simplified) expression. */
5051 fold_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5053 if (expr
== NULL_TREE
)
5056 /* If we're in a template, but EXPR isn't value dependent, simplify
5057 it. We're supposed to treat:
5059 template <typename T> void f(T[1 + 1]);
5060 template <typename T> void f(T[2]);
5062 as two declarations of the same function, for example. */
5063 if (processing_template_decl
5064 && !type_dependent_expression_p (expr
)
5065 && potential_constant_expression (expr
)
5066 && !value_dependent_expression_p (expr
))
5068 HOST_WIDE_INT saved_processing_template_decl
;
5070 saved_processing_template_decl
= processing_template_decl
;
5071 processing_template_decl
= 0;
5072 expr
= tsubst_copy_and_build (expr
,
5075 /*in_decl=*/NULL_TREE
,
5076 /*function_p=*/false,
5077 /*integral_constant_expression_p=*/true);
5078 processing_template_decl
= saved_processing_template_decl
;
5084 fold_non_dependent_expr (tree expr
)
5086 return fold_non_dependent_expr_sfinae (expr
, tf_error
);
5089 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5090 template declaration, or a TYPE_DECL for an alias declaration. */
5093 alias_type_or_template_p (tree t
)
5097 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5100 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5101 || DECL_ALIAS_TEMPLATE_P (t
));
5104 /* Return TRUE iff is a specialization of an alias template. */
5107 alias_template_specialization_p (const_tree t
)
5113 && TYPE_TEMPLATE_INFO (t
)
5114 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t
))
5115 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t
)));
5118 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5119 must be a function or a pointer-to-function type, as specified
5120 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5121 and check that the resulting function has external linkage. */
5124 convert_nontype_argument_function (tree type
, tree expr
)
5128 linkage_kind linkage
;
5130 fn
= instantiate_type (type
, fns
, tf_none
);
5131 if (fn
== error_mark_node
)
5132 return error_mark_node
;
5135 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5136 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5137 if (BASELINK_P (fn_no_ptr
))
5138 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5140 /* [temp.arg.nontype]/1
5142 A template-argument for a non-type, non-template template-parameter
5145 -- the address of an object or function with external [C++11: or
5146 internal] linkage. */
5147 linkage
= decl_linkage (fn_no_ptr
);
5148 if (cxx_dialect
>= cxx0x
? linkage
== lk_none
: linkage
!= lk_external
)
5150 if (cxx_dialect
>= cxx0x
)
5151 error ("%qE is not a valid template argument for type %qT "
5152 "because %qD has no linkage",
5153 expr
, type
, fn_no_ptr
);
5155 error ("%qE is not a valid template argument for type %qT "
5156 "because %qD does not have external linkage",
5157 expr
, type
, fn_no_ptr
);
5164 /* Subroutine of convert_nontype_argument.
5165 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5166 Emit an error otherwise. */
5169 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5170 tsubst_flags_t complain
)
5173 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5175 if (cxx_dialect
>= cxx0x
&& null_member_pointer_value_p (expr
))
5177 if (complain
& tf_error
)
5179 error ("%qE is not a valid template argument for type %qT",
5181 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5186 /* Returns TRUE iff the address of OP is value-dependent.
5188 14.6.2.4 [temp.dep.temp]:
5189 A non-integral non-type template-argument is dependent if its type is
5190 dependent or it has either of the following forms
5193 and contains a nested-name-specifier which specifies a class-name that
5194 names a dependent type.
5196 We generalize this to just say that the address of a member of a
5197 dependent class is value-dependent; the above doesn't cover the
5198 address of a static data member named with an unqualified-id. */
5201 has_value_dependent_address (tree op
)
5203 /* We could use get_inner_reference here, but there's no need;
5204 this is only relevant for template non-type arguments, which
5205 can only be expressed as &id-expression. */
5208 tree ctx
= CP_DECL_CONTEXT (op
);
5209 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5216 /* The next set of functions are used for providing helpful explanatory
5217 diagnostics for failed overload resolution. Their messages should be
5218 indented by two spaces for consistency with the messages in
5222 unify_success (bool /*explain_p*/)
5228 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5231 inform (input_location
,
5232 " couldn't deduce template parameter %qD", parm
);
5237 unify_invalid (bool /*explain_p*/)
5243 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5246 inform (input_location
,
5247 " types %qT and %qT have incompatible cv-qualifiers",
5253 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5256 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5261 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
5264 inform (input_location
,
5265 " template parameter %qD is not a parameter pack, but "
5272 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
5275 inform (input_location
,
5276 " template argument %qE does not match "
5277 "pointer-to-member constant %qE",
5283 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
5286 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
5291 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
5294 inform (input_location
,
5295 " inconsistent parameter pack deduction with %qT and %qT",
5301 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
5306 inform (input_location
,
5307 " deduced conflicting types for parameter %qT (%qT and %qT)",
5308 parm
, first
, second
);
5310 inform (input_location
,
5311 " deduced conflicting values for non-type parameter "
5312 "%qE (%qE and %qE)", parm
, first
, second
);
5318 unify_vla_arg (bool explain_p
, tree arg
)
5321 inform (input_location
,
5322 " variable-sized array type %qT is not "
5323 "a valid template argument",
5329 unify_method_type_error (bool explain_p
, tree arg
)
5332 inform (input_location
,
5333 " member function type %qT is not a valid template argument",
5339 unify_arity (bool explain_p
, int have
, int wanted
)
5342 inform_n (input_location
, wanted
,
5343 " candidate expects %d argument, %d provided",
5344 " candidate expects %d arguments, %d provided",
5350 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
5352 return unify_arity (explain_p
, have
, wanted
);
5356 unify_too_few_arguments (bool explain_p
, int have
, int wanted
)
5358 return unify_arity (explain_p
, have
, wanted
);
5362 unify_arg_conversion (bool explain_p
, tree to_type
,
5363 tree from_type
, tree arg
)
5366 inform (input_location
, " cannot convert %qE (type %qT) to type %qT",
5367 arg
, from_type
, to_type
);
5372 unify_no_common_base (bool explain_p
, enum template_base_result r
,
5373 tree parm
, tree arg
)
5378 case tbr_ambiguous_baseclass
:
5379 inform (input_location
, " %qT is an ambiguous base class of %qT",
5383 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
5390 unify_inconsistent_template_template_parameters (bool explain_p
)
5393 inform (input_location
,
5394 " template parameters of a template template argument are "
5395 "inconsistent with other deduced template arguments");
5400 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
5403 inform (input_location
,
5404 " can't deduce a template for %qT from non-template type %qT",
5410 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
5413 inform (input_location
,
5414 " template argument %qE does not match %qD", arg
, parm
);
5419 unify_overload_resolution_failure (bool explain_p
, tree arg
)
5422 inform (input_location
,
5423 " could not resolve address from overloaded function %qE",
5428 /* Attempt to convert the non-type template parameter EXPR to the
5429 indicated TYPE. If the conversion is successful, return the
5430 converted value. If the conversion is unsuccessful, return
5431 NULL_TREE if we issued an error message, or error_mark_node if we
5432 did not. We issue error messages for out-and-out bad template
5433 parameters, but not simply because the conversion failed, since we
5434 might be just trying to do argument deduction. Both TYPE and EXPR
5435 must be non-dependent.
5437 The conversion follows the special rules described in
5438 [temp.arg.nontype], and it is much more strict than an implicit
5441 This function is called twice for each template argument (see
5442 lookup_template_class for a more accurate description of this
5443 problem). This means that we need to handle expressions which
5444 are not valid in a C++ source, but can be created from the
5445 first call (for instance, casts to perform conversions). These
5446 hacks can go away after we fix the double coercion problem. */
5449 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5453 /* Detect immediately string literals as invalid non-type argument.
5454 This special-case is not needed for correctness (we would easily
5455 catch this later), but only to provide better diagnostic for this
5456 common user mistake. As suggested by DR 100, we do not mention
5457 linkage issues in the diagnostic as this is not the point. */
5458 /* FIXME we're making this OK. */
5459 if (TREE_CODE (expr
) == STRING_CST
)
5461 if (complain
& tf_error
)
5462 error ("%qE is not a valid template argument for type %qT "
5463 "because string literals can never be used in this context",
5468 /* Add the ADDR_EXPR now for the benefit of
5469 value_dependent_expression_p. */
5470 if (TYPE_PTROBV_P (type
)
5471 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
5473 expr
= decay_conversion (expr
, complain
);
5474 if (expr
== error_mark_node
)
5475 return error_mark_node
;
5478 /* If we are in a template, EXPR may be non-dependent, but still
5479 have a syntactic, rather than semantic, form. For example, EXPR
5480 might be a SCOPE_REF, rather than the VAR_DECL to which the
5481 SCOPE_REF refers. Preserving the qualifying scope is necessary
5482 so that access checking can be performed when the template is
5483 instantiated -- but here we need the resolved form so that we can
5484 convert the argument. */
5485 if (TYPE_REF_OBJ_P (type
)
5486 && has_value_dependent_address (expr
))
5487 /* If we want the address and it's value-dependent, don't fold. */;
5488 else if (!type_unknown_p (expr
))
5489 expr
= fold_non_dependent_expr_sfinae (expr
, complain
);
5490 if (error_operand_p (expr
))
5491 return error_mark_node
;
5492 expr_type
= TREE_TYPE (expr
);
5493 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5494 expr
= mark_lvalue_use (expr
);
5496 expr
= mark_rvalue_use (expr
);
5498 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5499 to a non-type argument of "nullptr". */
5500 if (expr
== nullptr_node
&& TYPE_PTR_OR_PTRMEM_P (type
))
5501 expr
= convert (type
, expr
);
5503 /* In C++11, integral or enumeration non-type template arguments can be
5504 arbitrary constant expressions. Pointer and pointer to
5505 member arguments can be general constant expressions that evaluate
5506 to a null value, but otherwise still need to be of a specific form. */
5507 if (cxx_dialect
>= cxx0x
)
5509 if (TREE_CODE (expr
) == PTRMEM_CST
)
5510 /* A PTRMEM_CST is already constant, and a valid template
5511 argument for a parameter of pointer to member type, we just want
5512 to leave it in that form rather than lower it to a
5514 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5515 expr
= maybe_constant_value (expr
);
5516 else if (TYPE_PTR_OR_PTRMEM_P (type
))
5518 tree folded
= maybe_constant_value (expr
);
5519 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
5520 : null_member_pointer_value_p (folded
))
5525 /* HACK: Due to double coercion, we can get a
5526 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5527 which is the tree that we built on the first call (see
5528 below when coercing to reference to object or to reference to
5529 function). We just strip everything and get to the arg.
5530 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5532 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5534 tree probe_type
, probe
= expr
;
5535 if (REFERENCE_REF_P (probe
))
5536 probe
= TREE_OPERAND (probe
, 0);
5537 probe_type
= TREE_TYPE (probe
);
5538 if (TREE_CODE (probe
) == NOP_EXPR
)
5540 /* ??? Maybe we could use convert_from_reference here, but we
5541 would need to relax its constraints because the NOP_EXPR
5542 could actually change the type to something more cv-qualified,
5543 and this is not folded by convert_from_reference. */
5544 tree addr
= TREE_OPERAND (probe
, 0);
5545 gcc_assert (TREE_CODE (probe_type
) == REFERENCE_TYPE
);
5546 gcc_assert (TREE_CODE (addr
) == ADDR_EXPR
);
5547 gcc_assert (TREE_CODE (TREE_TYPE (addr
)) == POINTER_TYPE
);
5548 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5549 (TREE_TYPE (probe_type
),
5550 TREE_TYPE (TREE_TYPE (addr
))));
5552 expr
= TREE_OPERAND (addr
, 0);
5553 expr_type
= TREE_TYPE (expr
);
5557 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5558 parameter is a pointer to object, through decay and
5559 qualification conversion. Let's strip everything. */
5560 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
5563 gcc_assert (TREE_CODE (expr
) == ADDR_EXPR
);
5564 gcc_assert (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
);
5565 /* Skip the ADDR_EXPR only if it is part of the decay for
5566 an array. Otherwise, it is part of the original argument
5567 in the source code. */
5568 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == ARRAY_TYPE
)
5569 expr
= TREE_OPERAND (expr
, 0);
5570 expr_type
= TREE_TYPE (expr
);
5573 /* [temp.arg.nontype]/5, bullet 1
5575 For a non-type template-parameter of integral or enumeration type,
5576 integral promotions (_conv.prom_) and integral conversions
5577 (_conv.integral_) are applied. */
5578 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5580 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
5581 t
= maybe_constant_value (t
);
5582 if (t
!= error_mark_node
)
5585 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
5586 return error_mark_node
;
5588 /* Notice that there are constant expressions like '4 % 0' which
5589 do not fold into integer constants. */
5590 if (TREE_CODE (expr
) != INTEGER_CST
)
5592 if (complain
& tf_error
)
5594 int errs
= errorcount
, warns
= warningcount
;
5595 if (processing_template_decl
5596 && !require_potential_constant_expression (expr
))
5598 expr
= cxx_constant_value (expr
);
5599 if (errorcount
> errs
|| warningcount
> warns
)
5600 inform (EXPR_LOC_OR_HERE (expr
),
5601 "in template argument for type %qT ", type
);
5602 if (expr
== error_mark_node
)
5604 /* else cxx_constant_value complained but gave us
5605 a real constant, so go ahead. */
5606 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
5612 /* [temp.arg.nontype]/5, bullet 2
5614 For a non-type template-parameter of type pointer to object,
5615 qualification conversions (_conv.qual_) and the array-to-pointer
5616 conversion (_conv.array_) are applied. */
5617 else if (TYPE_PTROBV_P (type
))
5619 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5621 A template-argument for a non-type, non-template template-parameter
5622 shall be one of: [...]
5624 -- the name of a non-type template-parameter;
5625 -- the address of an object or function with external linkage, [...]
5626 expressed as "& id-expression" where the & is optional if the name
5627 refers to a function or array, or if the corresponding
5628 template-parameter is a reference.
5630 Here, we do not care about functions, as they are invalid anyway
5631 for a parameter of type pointer-to-object. */
5633 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5634 /* Non-type template parameters are OK. */
5636 else if (cxx_dialect
>= cxx0x
&& integer_zerop (expr
))
5637 /* Null pointer values are OK in C++11. */;
5638 else if (TREE_CODE (expr
) != ADDR_EXPR
5639 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5641 if (TREE_CODE (expr
) == VAR_DECL
)
5643 error ("%qD is not a valid template argument "
5644 "because %qD is a variable, not the address of "
5649 /* Other values, like integer constants, might be valid
5650 non-type arguments of some other type. */
5651 return error_mark_node
;
5657 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
5658 ? TREE_OPERAND (expr
, 0) : expr
);
5659 if (TREE_CODE (decl
) != VAR_DECL
)
5661 error ("%qE is not a valid template argument of type %qT "
5662 "because %qE is not a variable",
5666 else if (cxx_dialect
< cxx0x
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
5668 error ("%qE is not a valid template argument of type %qT "
5669 "because %qD does not have external linkage",
5673 else if (cxx_dialect
>= cxx0x
&& decl_linkage (decl
) == lk_none
)
5675 error ("%qE is not a valid template argument of type %qT "
5676 "because %qD has no linkage",
5682 expr
= decay_conversion (expr
, complain
);
5683 if (expr
== error_mark_node
)
5684 return error_mark_node
;
5686 expr
= perform_qualification_conversions (type
, expr
);
5687 if (expr
== error_mark_node
)
5688 return error_mark_node
;
5690 /* [temp.arg.nontype]/5, bullet 3
5692 For a non-type template-parameter of type reference to object, no
5693 conversions apply. The type referred to by the reference may be more
5694 cv-qualified than the (otherwise identical) type of the
5695 template-argument. The template-parameter is bound directly to the
5696 template-argument, which must be an lvalue. */
5697 else if (TYPE_REF_OBJ_P (type
))
5699 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
5701 return error_mark_node
;
5703 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5705 error ("%qE is not a valid template argument for type %qT "
5706 "because of conflicts in cv-qualification", expr
, type
);
5710 if (!real_lvalue_p (expr
))
5712 error ("%qE is not a valid template argument for type %qT "
5713 "because it is not an lvalue", expr
, type
);
5717 /* [temp.arg.nontype]/1
5719 A template-argument for a non-type, non-template template-parameter
5720 shall be one of: [...]
5722 -- the address of an object or function with external linkage. */
5723 if (TREE_CODE (expr
) == INDIRECT_REF
5724 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5726 expr
= TREE_OPERAND (expr
, 0);
5729 error ("%q#D is not a valid template argument for type %qT "
5730 "because a reference variable does not have a constant "
5731 "address", expr
, type
);
5738 error ("%qE is not a valid template argument for type %qT "
5739 "because it is not an object with external linkage",
5744 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5746 error ("%qE is not a valid template argument for type %qT "
5747 "because object %qD has not external linkage",
5752 expr
= build_nop (type
, build_address (expr
));
5754 /* [temp.arg.nontype]/5, bullet 4
5756 For a non-type template-parameter of type pointer to function, only
5757 the function-to-pointer conversion (_conv.func_) is applied. If the
5758 template-argument represents a set of overloaded functions (or a
5759 pointer to such), the matching function is selected from the set
5761 else if (TYPE_PTRFN_P (type
))
5763 /* If the argument is a template-id, we might not have enough
5764 context information to decay the pointer. */
5765 if (!type_unknown_p (expr_type
))
5767 expr
= decay_conversion (expr
, complain
);
5768 if (expr
== error_mark_node
)
5769 return error_mark_node
;
5772 if (cxx_dialect
>= cxx0x
&& integer_zerop (expr
))
5773 /* Null pointer values are OK in C++11. */
5774 return perform_qualification_conversions (type
, expr
);
5776 expr
= convert_nontype_argument_function (type
, expr
);
5777 if (!expr
|| expr
== error_mark_node
)
5780 if (TREE_CODE (expr
) != ADDR_EXPR
)
5782 error ("%qE is not a valid template argument for type %qT", expr
, type
);
5783 error ("it must be the address of a function with external linkage");
5787 /* [temp.arg.nontype]/5, bullet 5
5789 For a non-type template-parameter of type reference to function, no
5790 conversions apply. If the template-argument represents a set of
5791 overloaded functions, the matching function is selected from the set
5793 else if (TYPE_REFFN_P (type
))
5795 if (TREE_CODE (expr
) == ADDR_EXPR
)
5797 error ("%qE is not a valid template argument for type %qT "
5798 "because it is a pointer", expr
, type
);
5799 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
5803 expr
= convert_nontype_argument_function (TREE_TYPE (type
), expr
);
5804 if (!expr
|| expr
== error_mark_node
)
5807 expr
= build_nop (type
, build_address (expr
));
5809 /* [temp.arg.nontype]/5, bullet 6
5811 For a non-type template-parameter of type pointer to member function,
5812 no conversions apply. If the template-argument represents a set of
5813 overloaded member functions, the matching member function is selected
5814 from the set (_over.over_). */
5815 else if (TYPE_PTRMEMFUNC_P (type
))
5817 expr
= instantiate_type (type
, expr
, tf_none
);
5818 if (expr
== error_mark_node
)
5819 return error_mark_node
;
5821 /* [temp.arg.nontype] bullet 1 says the pointer to member
5822 expression must be a pointer-to-member constant. */
5823 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5824 return error_mark_node
;
5826 /* There is no way to disable standard conversions in
5827 resolve_address_of_overloaded_function (called by
5828 instantiate_type). It is possible that the call succeeded by
5829 converting &B::I to &D::I (where B is a base of D), so we need
5830 to reject this conversion here.
5832 Actually, even if there was a way to disable standard conversions,
5833 it would still be better to reject them here so that we can
5834 provide a superior diagnostic. */
5835 if (!same_type_p (TREE_TYPE (expr
), type
))
5837 error ("%qE is not a valid template argument for type %qT "
5838 "because it is of type %qT", expr
, type
,
5840 /* If we are just one standard conversion off, explain. */
5841 if (can_convert (type
, TREE_TYPE (expr
), complain
))
5842 inform (input_location
,
5843 "standard conversions are not allowed in this context");
5847 /* [temp.arg.nontype]/5, bullet 7
5849 For a non-type template-parameter of type pointer to data member,
5850 qualification conversions (_conv.qual_) are applied. */
5851 else if (TYPE_PTRDATAMEM_P (type
))
5853 /* [temp.arg.nontype] bullet 1 says the pointer to member
5854 expression must be a pointer-to-member constant. */
5855 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5856 return error_mark_node
;
5858 expr
= perform_qualification_conversions (type
, expr
);
5859 if (expr
== error_mark_node
)
5862 else if (NULLPTR_TYPE_P (type
))
5864 if (expr
!= nullptr_node
)
5866 error ("%qE is not a valid template argument for type %qT "
5867 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
5872 /* A template non-type parameter must be one of the above. */
5876 /* Sanity check: did we actually convert the argument to the
5878 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5879 (type
, TREE_TYPE (expr
)));
5883 /* Subroutine of coerce_template_template_parms, which returns 1 if
5884 PARM_PARM and ARG_PARM match using the rule for the template
5885 parameters of template template parameters. Both PARM and ARG are
5886 template parameters; the rest of the arguments are the same as for
5887 coerce_template_template_parms.
5890 coerce_template_template_parm (tree parm
,
5892 tsubst_flags_t complain
,
5896 if (arg
== NULL_TREE
|| arg
== error_mark_node
5897 || parm
== NULL_TREE
|| parm
== error_mark_node
)
5900 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5903 switch (TREE_CODE (parm
))
5906 /* We encounter instantiations of templates like
5907 template <template <template <class> class> class TT>
5910 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
5911 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
5913 if (!coerce_template_template_parms
5914 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
5920 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
5921 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5922 /* Argument is a parameter pack but parameter is not. */
5927 /* The tsubst call is used to handle cases such as
5929 template <int> class C {};
5930 template <class T, template <T> class TT> class D {};
5933 i.e. the parameter list of TT depends on earlier parameters. */
5934 if (!uses_template_parms (TREE_TYPE (arg
))
5936 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
5940 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
5941 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
5942 /* Argument is a parameter pack but parameter is not. */
5955 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5956 template template parameters. Both PARM_PARMS and ARG_PARMS are
5957 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5960 Consider the example:
5961 template <class T> class A;
5962 template<template <class U> class TT> class B;
5964 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5965 the parameters to A, and OUTER_ARGS contains A. */
5968 coerce_template_template_parms (tree parm_parms
,
5970 tsubst_flags_t complain
,
5974 int nparms
, nargs
, i
;
5978 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
5979 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
5981 nparms
= TREE_VEC_LENGTH (parm_parms
);
5982 nargs
= TREE_VEC_LENGTH (arg_parms
);
5984 /* Determine whether we have a parameter pack at the end of the
5985 template template parameter's template parameter list. */
5986 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
5988 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
5990 if (parm
== error_mark_node
)
5993 switch (TREE_CODE (parm
))
5997 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6002 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6012 && !(variadic_p
&& nargs
>= nparms
- 1))
6015 /* Check all of the template parameters except the parameter pack at
6016 the end (if any). */
6017 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6019 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6020 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6023 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6024 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6026 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6034 /* Check each of the template parameters in the template
6035 argument against the template parameter pack at the end of
6036 the template template parameter. */
6037 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6040 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6042 for (; i
< nargs
; ++i
)
6044 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6047 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6049 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6058 /* Verifies that the deduced template arguments (in TARGS) for the
6059 template template parameters (in TPARMS) represent valid bindings,
6060 by comparing the template parameter list of each template argument
6061 to the template parameter list of its corresponding template
6062 template parameter, in accordance with DR150. This
6063 routine can only be called after all template arguments have been
6064 deduced. It will return TRUE if all of the template template
6065 parameter bindings are okay, FALSE otherwise. */
6067 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6069 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6072 /* We're dealing with template parms in this process. */
6073 ++processing_template_decl
;
6075 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6077 for (i
= 0; i
< ntparms
; ++i
)
6079 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6080 tree targ
= TREE_VEC_ELT (targs
, i
);
6082 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6084 tree packed_args
= NULL_TREE
;
6087 if (ARGUMENT_PACK_P (targ
))
6089 /* Look inside the argument pack. */
6090 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6091 len
= TREE_VEC_LENGTH (packed_args
);
6094 for (idx
= 0; idx
< len
; ++idx
)
6096 tree targ_parms
= NULL_TREE
;
6099 /* Extract the next argument from the argument
6101 targ
= TREE_VEC_ELT (packed_args
, idx
);
6103 if (PACK_EXPANSION_P (targ
))
6104 /* Look at the pattern of the pack expansion. */
6105 targ
= PACK_EXPANSION_PATTERN (targ
);
6107 /* Extract the template parameters from the template
6109 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6110 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6111 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6112 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6114 /* Verify that we can coerce the template template
6115 parameters from the template argument to the template
6116 parameter. This requires an exact match. */
6118 && !coerce_template_template_parms
6119 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6134 --processing_template_decl
;
6138 /* Since type attributes aren't mangled, we need to strip them from
6139 template type arguments. */
6142 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6145 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6147 mv
= TYPE_MAIN_VARIANT (arg
);
6148 arg
= strip_typedefs (arg
);
6149 if (TYPE_ALIGN (arg
) != TYPE_ALIGN (mv
)
6150 || TYPE_ATTRIBUTES (arg
) != TYPE_ATTRIBUTES (mv
))
6152 if (complain
& tf_warning
)
6153 warning (0, "ignoring attributes on template argument %qT", arg
);
6154 arg
= build_aligned_type (arg
, TYPE_ALIGN (mv
));
6155 arg
= cp_build_type_attribute_variant (arg
, TYPE_ATTRIBUTES (mv
));
6160 /* Convert the indicated template ARG as necessary to match the
6161 indicated template PARM. Returns the converted ARG, or
6162 error_mark_node if the conversion was unsuccessful. Error and
6163 warning messages are issued under control of COMPLAIN. This
6164 conversion is for the Ith parameter in the parameter list. ARGS is
6165 the full set of template arguments deduced so far. */
6168 convert_template_argument (tree parm
,
6171 tsubst_flags_t complain
,
6177 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
6179 if (TREE_CODE (arg
) == TREE_LIST
6180 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
6182 /* The template argument was the name of some
6183 member function. That's usually
6184 invalid, but static members are OK. In any
6185 case, grab the underlying fields/functions
6186 and issue an error later if required. */
6187 orig_arg
= TREE_VALUE (arg
);
6188 TREE_TYPE (arg
) = unknown_type_node
;
6193 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
6194 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
6195 || requires_tmpl_type
);
6197 /* When determining whether an argument pack expansion is a template,
6198 look at the pattern. */
6199 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
6200 arg
= PACK_EXPANSION_PATTERN (arg
);
6202 /* Deal with an injected-class-name used as a template template arg. */
6203 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
6205 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
6206 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6208 if (cxx_dialect
>= cxx0x
)
6209 /* OK under DR 1004. */;
6210 else if (complain
& tf_warning_or_error
)
6211 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
6212 " used as template template argument", TYPE_NAME (arg
));
6213 else if (flag_pedantic_errors
)
6221 ((TREE_CODE (arg
) == TEMPLATE_DECL
6222 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
6223 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
6224 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6225 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
6228 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6229 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
6230 arg
= TYPE_STUB_DECL (arg
);
6232 is_type
= TYPE_P (arg
) || is_tmpl_type
;
6234 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
6235 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
6237 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
6239 if (complain
& tf_error
)
6240 error ("invalid use of destructor %qE as a type", orig_arg
);
6241 return error_mark_node
;
6244 permerror (input_location
,
6245 "to refer to a type member of a template parameter, "
6246 "use %<typename %E%>", orig_arg
);
6248 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
6249 TREE_OPERAND (arg
, 1),
6255 if (is_type
!= requires_type
)
6259 if (complain
& tf_error
)
6261 error ("type/value mismatch at argument %d in template "
6262 "parameter list for %qD",
6265 error (" expected a constant of type %qT, got %qT",
6267 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6268 else if (requires_tmpl_type
)
6269 error (" expected a class template, got %qE", orig_arg
);
6271 error (" expected a type, got %qE", orig_arg
);
6274 return error_mark_node
;
6276 if (is_tmpl_type
^ requires_tmpl_type
)
6278 if (in_decl
&& (complain
& tf_error
))
6280 error ("type/value mismatch at argument %d in template "
6281 "parameter list for %qD",
6284 error (" expected a type, got %qT", DECL_NAME (arg
));
6286 error (" expected a class template, got %qT", orig_arg
);
6288 return error_mark_node
;
6293 if (requires_tmpl_type
)
6295 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6297 else if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6298 /* The number of argument required is not known yet.
6299 Just accept it for now. */
6300 val
= TREE_TYPE (arg
);
6303 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6306 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6308 if (coerce_template_template_parms (parmparm
, argparm
,
6314 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6316 if (val
!= error_mark_node
)
6318 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6319 val
= TREE_TYPE (val
);
6320 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6321 val
= make_pack_expansion (val
);
6326 if (in_decl
&& (complain
& tf_error
))
6328 error ("type/value mismatch at argument %d in "
6329 "template parameter list for %qD",
6331 error (" expected a template of type %qD, got %qT",
6335 val
= error_mark_node
;
6341 /* We only form one instance of each template specialization.
6342 Therefore, if we use a non-canonical variant (i.e., a
6343 typedef), any future messages referring to the type will use
6344 the typedef, which is confusing if those future uses do not
6345 themselves also use the typedef. */
6347 val
= canonicalize_type_argument (val
, complain
);
6351 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6353 if (invalid_nontype_parm_type_p (t
, complain
))
6354 return error_mark_node
;
6356 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6358 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6362 /* Not sure if this is reachable, but it doesn't hurt
6364 error ("type mismatch in nontype parameter pack");
6365 val
= error_mark_node
;
6368 else if (!uses_template_parms (orig_arg
) && !uses_template_parms (t
))
6369 /* We used to call digest_init here. However, digest_init
6370 will report errors, which we don't want when complain
6371 is zero. More importantly, digest_init will try too
6372 hard to convert things: for example, `0' should not be
6373 converted to pointer type at this point according to
6374 the standard. Accepting this is not merely an
6375 extension, since deciding whether or not these
6376 conversions can occur is part of determining which
6377 function template to call, or whether a given explicit
6378 argument specification is valid. */
6379 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6381 val
= strip_typedefs_expr (orig_arg
);
6383 if (val
== NULL_TREE
)
6384 val
= error_mark_node
;
6385 else if (val
== error_mark_node
&& (complain
& tf_error
))
6386 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6388 if (TREE_CODE (val
) == SCOPE_REF
)
6390 /* Strip typedefs from the SCOPE_REF. */
6391 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
6392 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
6394 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6395 QUALIFIED_NAME_IS_TEMPLATE (val
));
6402 /* Coerces the remaining template arguments in INNER_ARGS (from
6403 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6404 Returns the coerced argument pack. PARM_IDX is the position of this
6405 parameter in the template parameter list. ARGS is the original
6406 template argument list. */
6408 coerce_template_parameter_pack (tree parms
,
6416 tsubst_flags_t complain
)
6418 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6419 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6422 tree packed_types
= NULL_TREE
;
6424 if (arg_idx
> nargs
)
6427 packed_args
= make_tree_vec (nargs
- arg_idx
);
6429 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
6430 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
6432 /* When the template parameter is a non-type template
6433 parameter pack whose type uses parameter packs, we need
6434 to look at each of the template arguments
6435 separately. Build a vector of the types for these
6436 non-type template parameters in PACKED_TYPES. */
6438 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
6439 packed_types
= tsubst_pack_expansion (expansion
, args
,
6442 if (packed_types
== error_mark_node
)
6443 return error_mark_node
;
6445 /* Check that we have the right number of arguments. */
6447 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
6448 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
6451 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
6452 error ("wrong number of template arguments (%d, should be %d)",
6453 nargs
, needed_parms
);
6454 return error_mark_node
;
6457 /* If we aren't able to check the actual arguments now
6458 (because they haven't been expanded yet), we can at least
6459 verify that all of the types used for the non-type
6460 template parameter pack are, in fact, valid for non-type
6461 template parameters. */
6463 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6465 int j
, len
= TREE_VEC_LENGTH (packed_types
);
6466 for (j
= 0; j
< len
; ++j
)
6468 tree t
= TREE_VEC_ELT (packed_types
, j
);
6469 if (invalid_nontype_parm_type_p (t
, complain
))
6470 return error_mark_node
;
6475 /* Convert the remaining arguments, which will be a part of the
6476 parameter pack "parm". */
6477 for (; arg_idx
< nargs
; ++arg_idx
)
6479 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6480 tree actual_parm
= TREE_VALUE (parm
);
6482 if (packed_types
&& !PACK_EXPANSION_P (arg
))
6484 /* When we have a vector of types (corresponding to the
6485 non-type template parameter pack that uses parameter
6486 packs in its type, as mention above), and the
6487 argument is not an expansion (which expands to a
6488 currently unknown number of arguments), clone the
6489 parm and give it the next type in PACKED_TYPES. */
6490 actual_parm
= copy_node (actual_parm
);
6491 TREE_TYPE (actual_parm
) =
6492 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
6495 if (arg
== error_mark_node
)
6497 if (complain
& tf_error
)
6498 error ("template argument %d is invalid", arg_idx
+ 1);
6501 arg
= convert_template_argument (actual_parm
,
6502 arg
, new_args
, complain
, parm_idx
,
6504 if (arg
== error_mark_node
)
6506 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
6509 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6510 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
6511 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
6514 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
6515 TREE_TYPE (argument_pack
)
6516 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
6517 TREE_CONSTANT (argument_pack
) = 1;
6520 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
6521 #ifdef ENABLE_CHECKING
6522 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
6523 TREE_VEC_LENGTH (packed_args
));
6525 return argument_pack
;
6528 /* Returns true if the template argument vector ARGS contains
6529 any pack expansions, false otherwise. */
6532 any_pack_expanson_args_p (tree args
)
6536 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
6537 if (PACK_EXPANSION_P (TREE_VEC_ELT (args
, i
)))
6542 /* Convert all template arguments to their appropriate types, and
6543 return a vector containing the innermost resulting template
6544 arguments. If any error occurs, return error_mark_node. Error and
6545 warning messages are issued under control of COMPLAIN.
6547 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6548 for arguments not specified in ARGS. Otherwise, if
6549 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6550 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6551 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6555 coerce_template_parms (tree parms
,
6558 tsubst_flags_t complain
,
6559 bool require_all_args
,
6560 bool use_default_args
)
6562 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
6565 tree new_inner_args
;
6566 int saved_unevaluated_operand
;
6567 int saved_inhibit_evaluation_warnings
;
6569 /* When used as a boolean value, indicates whether this is a
6570 variadic template parameter list. Since it's an int, we can also
6571 subtract it from nparms to get the number of non-variadic
6574 int post_variadic_parms
= 0;
6576 if (args
== error_mark_node
)
6577 return error_mark_node
;
6579 nparms
= TREE_VEC_LENGTH (parms
);
6581 /* Determine if there are any parameter packs. */
6582 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
6584 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
6586 ++post_variadic_parms
;
6587 if (template_parameter_pack_p (tparm
))
6591 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
6592 /* If there are no parameters that follow a parameter pack, we need to
6593 expand any argument packs so that we can deduce a parameter pack from
6594 some non-packed args followed by an argument pack, as in variadic85.C.
6595 If there are such parameters, we need to leave argument packs intact
6596 so the arguments are assigned properly. This can happen when dealing
6597 with a nested class inside a partial specialization of a class
6598 template, as in variadic92.C, or when deducing a template parameter pack
6599 from a sub-declarator, as in variadic114.C. */
6600 if (!post_variadic_parms
)
6601 inner_args
= expand_template_argument_pack (inner_args
);
6603 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6604 if ((nargs
> nparms
&& !variadic_p
)
6605 || (nargs
< nparms
- variadic_p
6607 && !any_pack_expanson_args_p (inner_args
)
6608 && (!use_default_args
6609 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
6610 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
6612 if (complain
& tf_error
)
6616 nparms
-= variadic_p
;
6617 error ("wrong number of template arguments "
6618 "(%d, should be %d or more)", nargs
, nparms
);
6621 error ("wrong number of template arguments "
6622 "(%d, should be %d)", nargs
, nparms
);
6625 error ("provided for %q+D", in_decl
);
6628 return error_mark_node
;
6631 /* We need to evaluate the template arguments, even though this
6632 template-id may be nested within a "sizeof". */
6633 saved_unevaluated_operand
= cp_unevaluated_operand
;
6634 cp_unevaluated_operand
= 0;
6635 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6636 c_inhibit_evaluation_warnings
= 0;
6637 new_inner_args
= make_tree_vec (nparms
);
6638 new_args
= add_outermost_template_args (args
, new_inner_args
);
6639 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
6644 /* Get the Ith template parameter. */
6645 parm
= TREE_VEC_ELT (parms
, parm_idx
);
6647 if (parm
== error_mark_node
)
6649 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
6653 /* Calculate the next argument. */
6654 if (arg_idx
< nargs
)
6655 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6659 if (template_parameter_pack_p (TREE_VALUE (parm
))
6660 && !(arg
&& ARGUMENT_PACK_P (arg
)))
6662 /* All remaining arguments will be placed in the
6663 template parameter pack PARM. */
6664 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
6665 inner_args
, arg_idx
,
6669 /* Store this argument. */
6670 if (arg
== error_mark_node
)
6672 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
6674 /* We are done with all of the arguments. */
6681 if (PACK_EXPANSION_P (arg
))
6683 /* We don't know how many args we have yet, just
6684 use the unconverted ones for now. */
6685 new_inner_args
= inner_args
;
6689 else if (require_all_args
)
6691 /* There must be a default arg in this case. */
6692 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
6694 /* The position of the first default template argument,
6695 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6697 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6698 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
, arg_idx
);
6703 if (arg
== error_mark_node
)
6705 if (complain
& tf_error
)
6706 error ("template argument %d is invalid", arg_idx
+ 1);
6709 /* This only occurs if there was an error in the template
6710 parameter list itself (which we would already have
6711 reported) that we are trying to recover from, e.g., a class
6712 template with a parameter list such as
6713 template<typename..., typename>. */
6716 arg
= convert_template_argument (TREE_VALUE (parm
),
6717 arg
, new_args
, complain
,
6720 if (arg
== error_mark_node
)
6722 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
6724 cp_unevaluated_operand
= saved_unevaluated_operand
;
6725 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
6728 return error_mark_node
;
6730 #ifdef ENABLE_CHECKING
6731 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6732 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6733 TREE_VEC_LENGTH (new_inner_args
));
6736 return new_inner_args
;
6739 /* Returns 1 if template args OT and NT are equivalent. */
6742 template_args_equal (tree ot
, tree nt
)
6746 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
6749 if (TREE_CODE (nt
) == TREE_VEC
)
6750 /* For member templates */
6751 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
6752 else if (PACK_EXPANSION_P (ot
))
6753 return (PACK_EXPANSION_P (nt
)
6754 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
6755 PACK_EXPANSION_PATTERN (nt
))
6756 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
6757 PACK_EXPANSION_EXTRA_ARGS (nt
)));
6758 else if (ARGUMENT_PACK_P (ot
))
6763 if (!ARGUMENT_PACK_P (nt
))
6766 opack
= ARGUMENT_PACK_ARGS (ot
);
6767 npack
= ARGUMENT_PACK_ARGS (nt
);
6768 len
= TREE_VEC_LENGTH (opack
);
6769 if (TREE_VEC_LENGTH (npack
) != len
)
6771 for (i
= 0; i
< len
; ++i
)
6772 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
6773 TREE_VEC_ELT (npack
, i
)))
6777 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
6779 /* We get here probably because we are in the middle of substituting
6780 into the pattern of a pack expansion. In that case the
6781 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6782 interested in. So we want to use the initial pack argument for
6784 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
6785 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
6786 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
6787 return template_args_equal (ot
, nt
);
6789 else if (TYPE_P (nt
))
6790 return TYPE_P (ot
) && same_type_p (ot
, nt
);
6791 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
6794 return cp_tree_equal (ot
, nt
);
6797 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6798 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6799 NEWARG_PTR with the offending arguments if they are non-NULL. */
6802 comp_template_args_with_info (tree oldargs
, tree newargs
,
6803 tree
*oldarg_ptr
, tree
*newarg_ptr
)
6807 if (oldargs
== newargs
)
6810 if (!oldargs
|| !newargs
)
6813 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
6816 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
6818 tree nt
= TREE_VEC_ELT (newargs
, i
);
6819 tree ot
= TREE_VEC_ELT (oldargs
, i
);
6821 if (! template_args_equal (ot
, nt
))
6823 if (oldarg_ptr
!= NULL
)
6825 if (newarg_ptr
!= NULL
)
6833 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6834 of template arguments. Returns 0 otherwise. */
6837 comp_template_args (tree oldargs
, tree newargs
)
6839 return comp_template_args_with_info (oldargs
, newargs
, NULL
, NULL
);
6843 add_pending_template (tree d
)
6845 tree ti
= (TYPE_P (d
)
6846 ? CLASSTYPE_TEMPLATE_INFO (d
)
6847 : DECL_TEMPLATE_INFO (d
));
6848 struct pending_template
*pt
;
6851 if (TI_PENDING_TEMPLATE_FLAG (ti
))
6854 /* We are called both from instantiate_decl, where we've already had a
6855 tinst_level pushed, and instantiate_template, where we haven't.
6857 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
6860 push_tinst_level (d
);
6862 pt
= ggc_alloc_pending_template ();
6864 pt
->tinst
= current_tinst_level
;
6865 if (last_pending_template
)
6866 last_pending_template
->next
= pt
;
6868 pending_templates
= pt
;
6870 last_pending_template
= pt
;
6872 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
6879 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6880 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6881 documentation for TEMPLATE_ID_EXPR. */
6884 lookup_template_function (tree fns
, tree arglist
)
6888 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
6889 return error_mark_node
;
6891 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
6893 if (!is_overloaded_fn (fns
) && TREE_CODE (fns
) != IDENTIFIER_NODE
)
6895 error ("%q#D is not a function template", fns
);
6896 return error_mark_node
;
6899 if (BASELINK_P (fns
))
6901 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
6903 BASELINK_FUNCTIONS (fns
),
6908 type
= TREE_TYPE (fns
);
6909 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
6910 type
= unknown_type_node
;
6912 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
6915 /* Within the scope of a template class S<T>, the name S gets bound
6916 (in build_self_reference) to a TYPE_DECL for the class, not a
6917 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6918 or one of its enclosing classes, and that type is a template,
6919 return the associated TEMPLATE_DECL. Otherwise, the original
6922 Also handle the case when DECL is a TREE_LIST of ambiguous
6923 injected-class-names from different bases. */
6926 maybe_get_template_decl_from_type_decl (tree decl
)
6928 if (decl
== NULL_TREE
)
6931 /* DR 176: A lookup that finds an injected-class-name (10.2
6932 [class.member.lookup]) can result in an ambiguity in certain cases
6933 (for example, if it is found in more than one base class). If all of
6934 the injected-class-names that are found refer to specializations of
6935 the same class template, and if the name is followed by a
6936 template-argument-list, the reference refers to the class template
6937 itself and not a specialization thereof, and is not ambiguous. */
6938 if (TREE_CODE (decl
) == TREE_LIST
)
6940 tree t
, tmpl
= NULL_TREE
;
6941 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
6943 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
6946 else if (tmpl
!= elt
)
6949 if (tmpl
&& t
== NULL_TREE
)
6955 return (decl
!= NULL_TREE
6956 && DECL_SELF_REFERENCE_P (decl
)
6957 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
6958 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
6961 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6962 parameters, find the desired type.
6964 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6966 IN_DECL, if non-NULL, is the template declaration we are trying to
6969 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6970 the class we are looking up.
6972 Issue error and warning messages under control of COMPLAIN.
6974 If the template class is really a local class in a template
6975 function, then the FUNCTION_CONTEXT is the function in which it is
6978 ??? Note that this function is currently called *twice* for each
6979 template-id: the first time from the parser, while creating the
6980 incomplete type (finish_template_type), and the second type during the
6981 real instantiation (instantiate_template_class). This is surely something
6982 that we want to avoid. It also causes some problems with argument
6983 coercion (see convert_nontype_argument for more information on this). */
6986 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
6987 int entering_scope
, tsubst_flags_t complain
)
6989 tree templ
= NULL_TREE
, parmlist
;
6996 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
6998 tree value
= innermost_non_namespace_value (d1
);
6999 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
7004 push_decl_namespace (context
);
7005 templ
= lookup_name (d1
);
7006 templ
= maybe_get_template_decl_from_type_decl (templ
);
7008 pop_decl_namespace ();
7011 context
= DECL_CONTEXT (templ
);
7013 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
7015 tree type
= TREE_TYPE (d1
);
7017 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7018 an implicit typename for the second A. Deal with it. */
7019 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
7020 type
= TREE_TYPE (type
);
7022 if (CLASSTYPE_TEMPLATE_INFO (type
))
7024 templ
= CLASSTYPE_TI_TEMPLATE (type
);
7025 d1
= DECL_NAME (templ
);
7028 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
7029 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
7031 templ
= TYPE_TI_TEMPLATE (d1
);
7032 d1
= DECL_NAME (templ
);
7034 else if (TREE_CODE (d1
) == TEMPLATE_DECL
7035 && DECL_TEMPLATE_RESULT (d1
)
7036 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
7039 d1
= DECL_NAME (templ
);
7040 context
= DECL_CONTEXT (templ
);
7043 /* Issue an error message if we didn't find a template. */
7046 if (complain
& tf_error
)
7047 error ("%qT is not a template", d1
);
7048 return error_mark_node
;
7051 if (TREE_CODE (templ
) != TEMPLATE_DECL
7052 /* Make sure it's a user visible template, if it was named by
7054 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
7055 && !PRIMARY_TEMPLATE_P (templ
)))
7057 if (complain
& tf_error
)
7059 error ("non-template type %qT used as a template", d1
);
7061 error ("for template declaration %q+D", in_decl
);
7063 return error_mark_node
;
7066 complain
&= ~tf_user
;
7068 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
7070 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7071 template arguments */
7077 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7079 /* Consider an example where a template template parameter declared as
7081 template <class T, class U = std::allocator<T> > class TT
7083 The template parameter level of T and U are one level larger than
7084 of TT. To proper process the default argument of U, say when an
7085 instantiation `TT<int>' is seen, we need to build the full
7086 arguments containing {int} as the innermost level. Outer levels,
7087 available when not appearing as default template argument, can be
7088 obtained from the arguments of the enclosing template.
7090 Suppose that TT is later substituted with std::vector. The above
7091 instantiation is `TT<int, std::allocator<T> >' with TT at
7092 level 1, and T at level 2, while the template arguments at level 1
7093 becomes {std::vector} and the inner level 2 is {int}. */
7095 outer
= DECL_CONTEXT (templ
);
7097 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7098 else if (current_template_parms
)
7099 /* This is an argument of the current template, so we haven't set
7100 DECL_CONTEXT yet. */
7101 outer
= current_template_args ();
7104 arglist
= add_to_template_args (outer
, arglist
);
7106 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
7108 /*require_all_args=*/true,
7109 /*use_default_args=*/true);
7110 if (arglist2
== error_mark_node
7111 || (!uses_template_parms (arglist2
)
7112 && check_instantiated_args (templ
, arglist2
, complain
)))
7113 return error_mark_node
;
7115 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
7120 tree template_type
= TREE_TYPE (templ
);
7123 tree found
= NULL_TREE
;
7126 int is_dependent_type
;
7127 int use_partial_inst_tmpl
= false;
7129 if (template_type
== error_mark_node
)
7130 /* An error occured while building the template TEMPL, and a
7131 diagnostic has most certainly been emitted for that
7132 already. Let's propagate that error. */
7133 return error_mark_node
;
7135 gen_tmpl
= most_general_template (templ
);
7136 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7137 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
7138 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7140 if (arg_depth
== 1 && parm_depth
> 1)
7142 /* We've been given an incomplete set of template arguments.
7145 template <class T> struct S1 {
7146 template <class U> struct S2 {};
7147 template <class U> struct S2<U*> {};
7150 we will be called with an ARGLIST of `U*', but the
7151 TEMPLATE will be `template <class T> template
7152 <class U> struct S1<T>::S2'. We must fill in the missing
7155 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
7157 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7160 /* Now we should have enough arguments. */
7161 gcc_assert (parm_depth
== arg_depth
);
7163 /* From here on, we're only interested in the most general
7166 /* Calculate the BOUND_ARGS. These will be the args that are
7167 actually tsubst'd into the definition to create the
7171 /* We have multiple levels of arguments to coerce, at once. */
7173 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
7175 tree bound_args
= make_tree_vec (parm_depth
);
7177 for (i
= saved_depth
,
7178 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7179 i
> 0 && t
!= NULL_TREE
;
7180 --i
, t
= TREE_CHAIN (t
))
7183 if (i
== saved_depth
)
7184 a
= coerce_template_parms (TREE_VALUE (t
),
7187 /*require_all_args=*/true,
7188 /*use_default_args=*/true);
7190 /* Outer levels should have already been coerced. */
7191 a
= TMPL_ARGS_LEVEL (arglist
, i
);
7193 /* Don't process further if one of the levels fails. */
7194 if (a
== error_mark_node
)
7196 /* Restore the ARGLIST to its full size. */
7197 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7198 return error_mark_node
;
7201 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
7203 /* We temporarily reduce the length of the ARGLIST so
7204 that coerce_template_parms will see only the arguments
7205 corresponding to the template parameters it is
7207 TREE_VEC_LENGTH (arglist
)--;
7210 /* Restore the ARGLIST to its full size. */
7211 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7213 arglist
= bound_args
;
7217 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
7218 INNERMOST_TEMPLATE_ARGS (arglist
),
7221 /*require_all_args=*/true,
7222 /*use_default_args=*/true);
7224 if (arglist
== error_mark_node
)
7225 /* We were unable to bind the arguments. */
7226 return error_mark_node
;
7228 /* In the scope of a template class, explicit references to the
7229 template class refer to the type of the template, not any
7230 instantiation of it. For example, in:
7232 template <class T> class C { void f(C<T>); }
7234 the `C<T>' is just the same as `C'. Outside of the
7235 class, however, such a reference is an instantiation. */
7237 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
7238 || currently_open_class (template_type
))
7239 /* comp_template_args is expensive, check it last. */
7240 && comp_template_args (TYPE_TI_ARGS (template_type
),
7242 return template_type
;
7244 /* If we already have this specialization, return it. */
7245 elt
.tmpl
= gen_tmpl
;
7247 hash
= hash_specialization (&elt
);
7248 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
7254 is_dependent_type
= uses_template_parms (arglist
);
7256 /* If the deduced arguments are invalid, then the binding
7258 if (!is_dependent_type
7259 && check_instantiated_args (gen_tmpl
,
7260 INNERMOST_TEMPLATE_ARGS (arglist
),
7262 return error_mark_node
;
7264 if (!is_dependent_type
7265 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
7266 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
7267 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
7269 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
7270 DECL_NAME (gen_tmpl
),
7271 /*tag_scope=*/ts_global
);
7275 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
7277 if (context
== error_mark_node
)
7278 return error_mark_node
;
7281 context
= global_namespace
;
7283 /* Create the type. */
7284 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
7286 if (!is_dependent_type
)
7288 set_current_access_from_decl (TYPE_NAME (template_type
));
7289 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
7290 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
7291 arglist
, complain
, in_decl
),
7292 SCOPED_ENUM_P (template_type
), NULL
);
7296 /* We don't want to call start_enum for this type, since
7297 the values for the enumeration constants may involve
7298 template parameters. And, no one should be interested
7299 in the enumeration constants for such a type. */
7300 t
= cxx_make_type (ENUMERAL_TYPE
);
7301 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
7303 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
7304 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
7305 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7307 else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7309 /* The user referred to a specialization of an alias
7310 template represented by GEN_TMPL.
7312 [temp.alias]/2 says:
7314 When a template-id refers to the specialization of an
7315 alias template, it is equivalent to the associated
7316 type obtained by substitution of its
7317 template-arguments for the template-parameters in the
7318 type-id of the alias template. */
7320 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
7321 /* Note that the call above (by indirectly calling
7322 register_specialization in tsubst_decl) registers the
7323 TYPE_DECL representing the specialization of the alias
7324 template. So next time someone substitutes ARGLIST for
7325 the template parms into the alias template (GEN_TMPL),
7326 she'll get that TYPE_DECL back. */
7328 if (t
== error_mark_node
)
7331 else if (CLASS_TYPE_P (template_type
))
7333 t
= make_class_type (TREE_CODE (template_type
));
7334 CLASSTYPE_DECLARED_CLASS (t
)
7335 = CLASSTYPE_DECLARED_CLASS (template_type
);
7336 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7337 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7339 /* A local class. Make sure the decl gets registered properly. */
7340 if (context
== current_function_decl
)
7341 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7343 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7344 /* This instantiation is another name for the primary
7345 template type. Set the TYPE_CANONICAL field
7347 TYPE_CANONICAL (t
) = template_type
;
7348 else if (any_template_arguments_need_structural_equality_p (arglist
))
7349 /* Some of the template arguments require structural
7350 equality testing, so this template class requires
7351 structural equality testing. */
7352 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7357 /* If we called start_enum or pushtag above, this information
7358 will already be set up. */
7361 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
7363 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
7364 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
7365 DECL_SOURCE_LOCATION (type_decl
)
7366 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
7369 type_decl
= TYPE_NAME (t
);
7371 if (CLASS_TYPE_P (template_type
))
7373 TREE_PRIVATE (type_decl
)
7374 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
7375 TREE_PROTECTED (type_decl
)
7376 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
7377 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
7379 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
7380 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
7384 /* Let's consider the explicit specialization of a member
7385 of a class template specialization that is implicitely instantiated,
7390 template<class U> struct M {}; //#0
7395 struct S<int>::M<char> //#1
7399 [temp.expl.spec]/4 says this is valid.
7401 In this case, when we write:
7404 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7407 When we encounter #1, we want to store the partial instantiation
7408 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
7410 For all cases other than this "explicit specialization of member of a
7411 class template", we just want to store the most general template into
7412 the CLASSTYPE_TI_TEMPLATE of M.
7414 This case of "explicit specialization of member of a class template"
7416 1/ the enclosing class is an instantiation of, and therefore not
7417 the same as, the context of the most general template, and
7418 2/ we aren't looking at the partial instantiation itself, i.e.
7419 the innermost arguments are not the same as the innermost parms of
7420 the most general template.
7422 So it's only when 1/ and 2/ happens that we want to use the partial
7423 instantiation of the member template in lieu of its most general
7426 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
7427 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
7428 /* the enclosing class must be an instantiation... */
7429 && CLASS_TYPE_P (context
)
7430 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
7432 tree partial_inst_args
;
7433 TREE_VEC_LENGTH (arglist
)--;
7434 ++processing_template_decl
;
7436 tsubst (INNERMOST_TEMPLATE_ARGS
7437 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
7438 arglist
, complain
, NULL_TREE
);
7439 --processing_template_decl
;
7440 TREE_VEC_LENGTH (arglist
)++;
7441 use_partial_inst_tmpl
=
7442 /*...and we must not be looking at the partial instantiation
7444 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
7448 if (!use_partial_inst_tmpl
)
7449 /* This case is easy; there are no member templates involved. */
7453 /* This is a full instantiation of a member template. Find
7454 the partial instantiation of which this is an instance. */
7456 /* Temporarily reduce by one the number of levels in the ARGLIST
7457 so as to avoid comparing the last set of arguments. */
7458 TREE_VEC_LENGTH (arglist
)--;
7459 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
7460 TREE_VEC_LENGTH (arglist
)++;
7461 /* FOUND is either a proper class type, or an alias
7462 template specialization. In the later case, it's a
7463 TYPE_DECL, resulting from the substituting of arguments
7464 for parameters in the TYPE_DECL of the alias template
7465 done earlier. So be careful while getting the template
7467 found
= TREE_CODE (found
) == TYPE_DECL
7468 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
7469 : CLASSTYPE_TI_TEMPLATE (found
);
7472 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
7475 slot
= htab_find_slot_with_hash (type_specializations
,
7476 &elt
, hash
, INSERT
);
7477 entry
= ggc_alloc_spec_entry ();
7481 /* Note this use of the partial instantiation so we can check it
7482 later in maybe_process_partial_specialization. */
7483 DECL_TEMPLATE_INSTANTIATIONS (templ
)
7484 = tree_cons (arglist
, t
,
7485 DECL_TEMPLATE_INSTANTIATIONS (templ
));
7487 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
)
7488 /* Now that the type has been registered on the instantiations
7489 list, we set up the enumerators. Because the enumeration
7490 constants may involve the enumeration type itself, we make
7491 sure to register the type first, and then create the
7492 constants. That way, doing tsubst_expr for the enumeration
7493 constants won't result in recursive calls here; we'll find
7494 the instantiation and exit above. */
7495 tsubst_enum (template_type
, t
, arglist
);
7497 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
7498 /* If the type makes use of template parameters, the
7499 code that generates debugging information will crash. */
7500 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
7502 /* Possibly limit visibility based on template args. */
7503 TREE_PUBLIC (type_decl
) = 1;
7504 determine_visibility (type_decl
);
7510 /* Wrapper for lookup_template_class_1. */
7513 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
7514 int entering_scope
, tsubst_flags_t complain
)
7517 timevar_push (TV_TEMPLATE_INST
);
7518 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
7519 entering_scope
, complain
);
7520 timevar_pop (TV_TEMPLATE_INST
);
7528 /* True when we should also visit template parameters that occur in
7529 non-deduced contexts. */
7530 bool include_nondeduced_p
;
7531 struct pointer_set_t
*visited
;
7534 /* Called from for_each_template_parm via walk_tree. */
7537 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
7540 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
7541 tree_fn_t fn
= pfd
->fn
;
7542 void *data
= pfd
->data
;
7545 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
7546 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
7547 pfd
->include_nondeduced_p
))
7548 return error_mark_node
;
7550 switch (TREE_CODE (t
))
7553 if (TYPE_PTRMEMFUNC_P (t
))
7559 if (!TYPE_TEMPLATE_INFO (t
))
7561 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
7562 fn
, data
, pfd
->visited
,
7563 pfd
->include_nondeduced_p
))
7564 return error_mark_node
;
7568 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
7569 fn
, data
, pfd
->visited
,
7570 pfd
->include_nondeduced_p
)
7571 || for_each_template_parm (TYPE_MAX_VALUE (t
),
7572 fn
, data
, pfd
->visited
,
7573 pfd
->include_nondeduced_p
))
7574 return error_mark_node
;
7578 /* Since we're not going to walk subtrees, we have to do this
7580 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
7581 pfd
->visited
, pfd
->include_nondeduced_p
))
7582 return error_mark_node
;
7586 /* Check the return type. */
7587 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7588 pfd
->include_nondeduced_p
))
7589 return error_mark_node
;
7591 /* Check the parameter types. Since default arguments are not
7592 instantiated until they are needed, the TYPE_ARG_TYPES may
7593 contain expressions that involve template parameters. But,
7594 no-one should be looking at them yet. And, once they're
7595 instantiated, they don't contain template parameters, so
7596 there's no point in looking at them then, either. */
7600 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
7601 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
7602 pfd
->visited
, pfd
->include_nondeduced_p
))
7603 return error_mark_node
;
7605 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7606 want walk_tree walking into them itself. */
7612 case UNDERLYING_TYPE
:
7613 if (pfd
->include_nondeduced_p
7614 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
7616 pfd
->include_nondeduced_p
))
7617 return error_mark_node
;
7622 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
7623 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
7624 pfd
->visited
, pfd
->include_nondeduced_p
))
7625 return error_mark_node
;
7630 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
7631 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
7632 pfd
->visited
, pfd
->include_nondeduced_p
))
7633 return error_mark_node
;
7634 if (DECL_CONTEXT (t
)
7635 && pfd
->include_nondeduced_p
7636 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
7637 pfd
->visited
, pfd
->include_nondeduced_p
))
7638 return error_mark_node
;
7641 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7642 /* Record template parameters such as `T' inside `TT<T>'. */
7643 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
7644 pfd
->include_nondeduced_p
))
7645 return error_mark_node
;
7648 case TEMPLATE_TEMPLATE_PARM
:
7649 case TEMPLATE_TYPE_PARM
:
7650 case TEMPLATE_PARM_INDEX
:
7651 if (fn
&& (*fn
)(t
, data
))
7652 return error_mark_node
;
7654 return error_mark_node
;
7658 /* A template template parameter is encountered. */
7659 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
7660 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7661 pfd
->include_nondeduced_p
))
7662 return error_mark_node
;
7664 /* Already substituted template template parameter */
7670 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
7672 pfd
->include_nondeduced_p
))
7673 return error_mark_node
;
7677 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
7678 && pfd
->include_nondeduced_p
7679 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7680 (TREE_TYPE (t
)), fn
, data
,
7681 pfd
->visited
, pfd
->include_nondeduced_p
))
7682 return error_mark_node
;
7687 /* If there's no type, then this thing must be some expression
7688 involving template parameters. */
7689 if (!fn
&& !TREE_TYPE (t
))
7690 return error_mark_node
;
7695 case IMPLICIT_CONV_EXPR
:
7696 case REINTERPRET_CAST_EXPR
:
7697 case CONST_CAST_EXPR
:
7698 case STATIC_CAST_EXPR
:
7699 case DYNAMIC_CAST_EXPR
:
7703 case PSEUDO_DTOR_EXPR
:
7705 return error_mark_node
;
7712 /* We didn't find any template parameters we liked. */
7716 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7717 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7718 call FN with the parameter and the DATA.
7719 If FN returns nonzero, the iteration is terminated, and
7720 for_each_template_parm returns 1. Otherwise, the iteration
7721 continues. If FN never returns a nonzero value, the value
7722 returned by for_each_template_parm is 0. If FN is NULL, it is
7723 considered to be the function which always returns 1.
7725 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7726 parameters that occur in non-deduced contexts. When false, only
7727 visits those template parameters that can be deduced. */
7730 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
7731 struct pointer_set_t
*visited
,
7732 bool include_nondeduced_p
)
7734 struct pair_fn_data pfd
;
7740 pfd
.include_nondeduced_p
= include_nondeduced_p
;
7742 /* Walk the tree. (Conceptually, we would like to walk without
7743 duplicates, but for_each_template_parm_r recursively calls
7744 for_each_template_parm, so we would need to reorganize a fair
7745 bit to use walk_tree_without_duplicates, so we keep our own
7748 pfd
.visited
= visited
;
7750 pfd
.visited
= pointer_set_create ();
7751 result
= cp_walk_tree (&t
,
7752 for_each_template_parm_r
,
7754 pfd
.visited
) != NULL_TREE
;
7759 pointer_set_destroy (pfd
.visited
);
7766 /* Returns true if T depends on any template parameter. */
7769 uses_template_parms (tree t
)
7772 int saved_processing_template_decl
;
7774 saved_processing_template_decl
= processing_template_decl
;
7775 if (!saved_processing_template_decl
)
7776 processing_template_decl
= 1;
7778 dependent_p
= dependent_type_p (t
);
7779 else if (TREE_CODE (t
) == TREE_VEC
)
7780 dependent_p
= any_dependent_template_arguments_p (t
);
7781 else if (TREE_CODE (t
) == TREE_LIST
)
7782 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
7783 || uses_template_parms (TREE_CHAIN (t
)));
7784 else if (TREE_CODE (t
) == TYPE_DECL
)
7785 dependent_p
= dependent_type_p (TREE_TYPE (t
));
7788 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
7789 || TREE_CODE (t
) == OVERLOAD
7791 || TREE_CODE (t
) == IDENTIFIER_NODE
7792 || TREE_CODE (t
) == TRAIT_EXPR
7793 || TREE_CODE (t
) == CONSTRUCTOR
7794 || CONSTANT_CLASS_P (t
))
7795 dependent_p
= (type_dependent_expression_p (t
)
7796 || value_dependent_expression_p (t
));
7799 gcc_assert (t
== error_mark_node
);
7800 dependent_p
= false;
7803 processing_template_decl
= saved_processing_template_decl
;
7808 /* Returns true iff current_function_decl is an incompletely instantiated
7809 template. Useful instead of processing_template_decl because the latter
7810 is set to 0 during fold_non_dependent_expr. */
7813 in_template_function (void)
7815 tree fn
= current_function_decl
;
7817 ++processing_template_decl
;
7818 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
7819 && DECL_TEMPLATE_INFO (fn
)
7820 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
7821 --processing_template_decl
;
7825 /* Returns true if T depends on any template parameter with level LEVEL. */
7828 uses_template_parms_level (tree t
, int level
)
7830 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
7831 /*include_nondeduced_p=*/true);
7834 /* Returns TRUE iff INST is an instantiation we don't need to do in an
7835 ill-formed translation unit, i.e. a variable or function that isn't
7836 usable in a constant expression. */
7839 neglectable_inst_p (tree d
)
7842 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
7843 : decl_maybe_constant_var_p (d
)));
7846 /* Returns TRUE iff we should refuse to instantiate DECL because it's
7847 neglectable and instantiated from within an erroneous instantiation. */
7850 limit_bad_template_recursion (tree decl
)
7852 struct tinst_level
*lev
= current_tinst_level
;
7853 int errs
= errorcount
+ sorrycount
;
7854 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
7857 for (; lev
; lev
= lev
->next
)
7858 if (neglectable_inst_p (lev
->decl
))
7861 return (lev
&& errs
> lev
->errors
);
7864 static int tinst_depth
;
7865 extern int max_tinst_depth
;
7868 static GTY(()) struct tinst_level
*last_error_tinst_level
;
7870 /* We're starting to instantiate D; record the template instantiation context
7871 for diagnostics and to restore it later. */
7874 push_tinst_level (tree d
)
7876 struct tinst_level
*new_level
;
7878 if (tinst_depth
>= max_tinst_depth
)
7880 last_error_tinst_level
= current_tinst_level
;
7881 if (TREE_CODE (d
) == TREE_LIST
)
7882 error ("template instantiation depth exceeds maximum of %d (use "
7883 "-ftemplate-depth= to increase the maximum) substituting %qS",
7884 max_tinst_depth
, d
);
7886 error ("template instantiation depth exceeds maximum of %d (use "
7887 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7888 max_tinst_depth
, d
);
7890 print_instantiation_context ();
7895 /* If the current instantiation caused problems, don't let it instantiate
7896 anything else. Do allow deduction substitution and decls usable in
7897 constant expressions. */
7898 if (limit_bad_template_recursion (d
))
7901 new_level
= ggc_alloc_tinst_level ();
7902 new_level
->decl
= d
;
7903 new_level
->locus
= input_location
;
7904 new_level
->errors
= errorcount
+sorrycount
;
7905 new_level
->in_system_header_p
= in_system_header
;
7906 new_level
->next
= current_tinst_level
;
7907 current_tinst_level
= new_level
;
7910 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
7911 depth_reached
= tinst_depth
;
7916 /* We're done instantiating this template; return to the instantiation
7920 pop_tinst_level (void)
7922 /* Restore the filename and line number stashed away when we started
7923 this instantiation. */
7924 input_location
= current_tinst_level
->locus
;
7925 current_tinst_level
= current_tinst_level
->next
;
7929 /* We're instantiating a deferred template; restore the template
7930 instantiation context in which the instantiation was requested, which
7931 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7934 reopen_tinst_level (struct tinst_level
*level
)
7936 struct tinst_level
*t
;
7939 for (t
= level
; t
; t
= t
->next
)
7942 current_tinst_level
= level
;
7944 if (current_tinst_level
)
7945 current_tinst_level
->errors
= errorcount
+sorrycount
;
7949 /* Returns the TINST_LEVEL which gives the original instantiation
7952 struct tinst_level
*
7953 outermost_tinst_level (void)
7955 struct tinst_level
*level
= current_tinst_level
;
7958 level
= level
->next
;
7962 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
7963 vector of template arguments, as for tsubst.
7965 Returns an appropriate tsubst'd friend declaration. */
7968 tsubst_friend_function (tree decl
, tree args
)
7972 if (TREE_CODE (decl
) == FUNCTION_DECL
7973 && DECL_TEMPLATE_INSTANTIATION (decl
)
7974 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
7975 /* This was a friend declared with an explicit template
7976 argument list, e.g.:
7980 to indicate that f was a template instantiation, not a new
7981 function declaration. Now, we have to figure out what
7982 instantiation of what template. */
7984 tree template_id
, arglist
, fns
;
7987 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
7989 /* Friend functions are looked up in the containing namespace scope.
7990 We must enter that scope, to avoid finding member functions of the
7991 current class with same name. */
7992 push_nested_namespace (ns
);
7993 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
7994 tf_warning_or_error
, NULL_TREE
,
7995 /*integral_constant_expression_p=*/false);
7996 pop_nested_namespace (ns
);
7997 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
7998 tf_warning_or_error
, NULL_TREE
);
7999 template_id
= lookup_template_function (fns
, arglist
);
8001 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8002 tmpl
= determine_specialization (template_id
, new_friend
,
8004 /*need_member_template=*/0,
8005 TREE_VEC_LENGTH (args
),
8007 return instantiate_template (tmpl
, new_args
, tf_error
);
8010 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8012 /* The NEW_FRIEND will look like an instantiation, to the
8013 compiler, but is not an instantiation from the point of view of
8014 the language. For example, we might have had:
8016 template <class T> struct S {
8017 template <class U> friend void f(T, U);
8020 Then, in S<int>, template <class U> void f(int, U) is not an
8021 instantiation of anything. */
8022 if (new_friend
== error_mark_node
)
8023 return error_mark_node
;
8025 DECL_USE_TEMPLATE (new_friend
) = 0;
8026 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8028 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
8029 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
8030 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
8033 /* The mangled name for the NEW_FRIEND is incorrect. The function
8034 is not a template instantiation and should not be mangled like
8035 one. Therefore, we forget the mangling here; we'll recompute it
8036 later if we need it. */
8037 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
8039 SET_DECL_RTL (new_friend
, NULL
);
8040 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
8043 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
8046 tree new_friend_template_info
;
8047 tree new_friend_result_template_info
;
8049 int new_friend_is_defn
;
8051 /* We must save some information from NEW_FRIEND before calling
8052 duplicate decls since that function will free NEW_FRIEND if
8054 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
8055 new_friend_is_defn
=
8056 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8057 (template_for_substitution (new_friend
)))
8059 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
8061 /* This declaration is a `primary' template. */
8062 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
8064 new_friend_result_template_info
8065 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
8068 new_friend_result_template_info
= NULL_TREE
;
8070 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8071 if (new_friend_is_defn
)
8072 DECL_INITIAL (new_friend
) = error_mark_node
;
8074 /* Inside pushdecl_namespace_level, we will push into the
8075 current namespace. However, the friend function should go
8076 into the namespace of the template. */
8077 ns
= decl_namespace_context (new_friend
);
8078 push_nested_namespace (ns
);
8079 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
8080 pop_nested_namespace (ns
);
8082 if (old_decl
== error_mark_node
)
8083 return error_mark_node
;
8085 if (old_decl
!= new_friend
)
8087 /* This new friend declaration matched an existing
8088 declaration. For example, given:
8090 template <class T> void f(T);
8091 template <class U> class C {
8092 template <class T> friend void f(T) {}
8095 the friend declaration actually provides the definition
8096 of `f', once C has been instantiated for some type. So,
8097 old_decl will be the out-of-class template declaration,
8098 while new_friend is the in-class definition.
8100 But, if `f' was called before this point, the
8101 instantiation of `f' will have DECL_TI_ARGS corresponding
8102 to `T' but not to `U', references to which might appear
8103 in the definition of `f'. Previously, the most general
8104 template for an instantiation of `f' was the out-of-class
8105 version; now it is the in-class version. Therefore, we
8106 run through all specialization of `f', adding to their
8107 DECL_TI_ARGS appropriately. In particular, they need a
8108 new set of outer arguments, corresponding to the
8109 arguments for this class instantiation.
8111 The same situation can arise with something like this:
8114 template <class T> class C {
8118 when `C<int>' is instantiated. Now, `f(int)' is defined
8121 if (!new_friend_is_defn
)
8122 /* On the other hand, if the in-class declaration does
8123 *not* provide a definition, then we don't want to alter
8124 existing definitions. We can just leave everything
8129 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
8130 tree new_args
= TI_ARGS (new_friend_template_info
);
8132 /* Overwrite whatever template info was there before, if
8133 any, with the new template information pertaining to
8135 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
8137 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
8139 /* We should have called reregister_specialization in
8141 gcc_assert (retrieve_specialization (new_template
,
8145 /* Instantiate it if the global has already been used. */
8146 if (DECL_ODR_USED (old_decl
))
8147 instantiate_decl (old_decl
, /*defer_ok=*/true,
8148 /*expl_inst_class_mem_p=*/false);
8154 /* Indicate that the old function template is a partial
8156 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
8157 = new_friend_result_template_info
;
8159 gcc_assert (new_template
8160 == most_general_template (new_template
));
8161 gcc_assert (new_template
!= old_decl
);
8163 /* Reassign any specializations already in the hash table
8164 to the new more general template, and add the
8165 additional template args. */
8166 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
8170 tree spec
= TREE_VALUE (t
);
8173 elt
.tmpl
= old_decl
;
8174 elt
.args
= DECL_TI_ARGS (spec
);
8175 elt
.spec
= NULL_TREE
;
8177 htab_remove_elt (decl_specializations
, &elt
);
8180 = add_outermost_template_args (new_args
,
8181 DECL_TI_ARGS (spec
));
8183 register_specialization
8184 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
8187 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
8191 /* The information from NEW_FRIEND has been merged into OLD_DECL
8192 by duplicate_decls. */
8193 new_friend
= old_decl
;
8198 tree context
= DECL_CONTEXT (new_friend
);
8202 template <class T> class C {
8203 template <class U> friend void C1<U>::f (); // case 1
8204 friend void C2<T>::f (); // case 2
8206 we only need to make sure CONTEXT is a complete type for
8207 case 2. To distinguish between the two cases, we note that
8208 CONTEXT of case 1 remains dependent type after tsubst while
8209 this isn't true for case 2. */
8210 ++processing_template_decl
;
8211 dependent_p
= dependent_type_p (context
);
8212 --processing_template_decl
;
8215 && !complete_type_or_else (context
, NULL_TREE
))
8216 return error_mark_node
;
8218 if (COMPLETE_TYPE_P (context
))
8220 /* Check to see that the declaration is really present, and,
8221 possibly obtain an improved declaration. */
8222 tree fn
= check_classfn (context
,
8223 new_friend
, NULL_TREE
);
8233 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8234 template arguments, as for tsubst.
8236 Returns an appropriate tsubst'd friend type or error_mark_node on
8240 tsubst_friend_class (tree friend_tmpl
, tree args
)
8246 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
8248 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
8249 return TREE_TYPE (t
);
8252 context
= CP_DECL_CONTEXT (friend_tmpl
);
8254 if (context
!= global_namespace
)
8256 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8257 push_nested_namespace (context
);
8259 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
8262 /* Look for a class template declaration. We look for hidden names
8263 because two friend declarations of the same template are the
8264 same. For example, in:
8267 template <typename> friend class F;
8269 template <typename> struct B {
8270 template <typename> friend class F;
8273 both F templates are the same. */
8274 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
8275 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
8277 /* But, if we don't find one, it might be because we're in a
8278 situation like this:
8286 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8287 for `S<int>', not the TEMPLATE_DECL. */
8288 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
8290 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
8291 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
8294 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
8296 /* The friend template has already been declared. Just
8297 check to see that the declarations match, and install any new
8298 default parameters. We must tsubst the default parameters,
8299 of course. We only need the innermost template parameters
8300 because that is all that redeclare_class_template will look
8302 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
8303 > TMPL_ARGS_DEPTH (args
))
8306 location_t saved_input_location
;
8307 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
8308 args
, tf_warning_or_error
);
8310 saved_input_location
= input_location
;
8311 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
8312 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
8313 input_location
= saved_input_location
;
8317 friend_type
= TREE_TYPE (tmpl
);
8321 /* The friend template has not already been declared. In this
8322 case, the instantiation of the template class will cause the
8323 injection of this template into the global scope. */
8324 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
8325 if (tmpl
== error_mark_node
)
8326 return error_mark_node
;
8328 /* The new TMPL is not an instantiation of anything, so we
8329 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8330 the new type because that is supposed to be the corresponding
8331 template decl, i.e., TMPL. */
8332 DECL_USE_TEMPLATE (tmpl
) = 0;
8333 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
8334 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
8335 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
8336 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
8338 /* Inject this template into the global scope. */
8339 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
8342 if (context
!= global_namespace
)
8344 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8345 pop_nested_namespace (context
);
8347 pop_nested_class ();
8353 /* Returns zero if TYPE cannot be completed later due to circularity.
8354 Otherwise returns one. */
8357 can_complete_type_without_circularity (tree type
)
8359 if (type
== NULL_TREE
|| type
== error_mark_node
)
8361 else if (COMPLETE_TYPE_P (type
))
8363 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
8364 return can_complete_type_without_circularity (TREE_TYPE (type
));
8365 else if (CLASS_TYPE_P (type
)
8366 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
8372 /* Apply any attributes which had to be deferred until instantiation
8373 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8374 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8377 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
8378 tree args
, tsubst_flags_t complain
, tree in_decl
)
8380 tree last_dep
= NULL_TREE
;
8384 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
8385 if (ATTR_IS_DEPENDENT (t
))
8388 attributes
= copy_list (attributes
);
8392 if (DECL_P (*decl_p
))
8394 if (TREE_TYPE (*decl_p
) == error_mark_node
)
8396 p
= &DECL_ATTRIBUTES (*decl_p
);
8399 p
= &TYPE_ATTRIBUTES (*decl_p
);
8403 tree late_attrs
= NULL_TREE
;
8404 tree
*q
= &late_attrs
;
8406 for (*p
= attributes
; *p
; )
8409 if (ATTR_IS_DEPENDENT (t
))
8411 *p
= TREE_CHAIN (t
);
8412 TREE_CHAIN (t
) = NULL_TREE
;
8413 /* If the first attribute argument is an identifier, don't
8414 pass it through tsubst. Attributes like mode, format,
8415 cleanup and several target specific attributes expect it
8418 && TREE_CODE (TREE_VALUE (t
)) == TREE_LIST
8419 && TREE_VALUE (TREE_VALUE (t
))
8420 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t
)))
8421 == IDENTIFIER_NODE
))
8424 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
8426 /*integral_constant_expression_p=*/false);
8427 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
8429 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
8434 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
8435 /*integral_constant_expression_p=*/false);
8437 q
= &TREE_CHAIN (t
);
8440 p
= &TREE_CHAIN (t
);
8443 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
8447 /* Perform (or defer) access check for typedefs that were referenced
8448 from within the template TMPL code.
8449 This is a subroutine of instantiate_decl and instantiate_class_template.
8450 TMPL is the template to consider and TARGS is the list of arguments of
8454 perform_typedefs_access_check (tree tmpl
, tree targs
)
8456 location_t saved_location
;
8458 qualified_typedef_usage_t
*iter
;
8461 || (!CLASS_TYPE_P (tmpl
)
8462 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
8465 saved_location
= input_location
;
8466 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
8468 tree type_decl
= iter
->typedef_decl
;
8469 tree type_scope
= iter
->context
;
8471 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
8474 if (uses_template_parms (type_decl
))
8475 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
8476 if (uses_template_parms (type_scope
))
8477 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
8479 /* Make access check error messages point to the location
8480 of the use of the typedef. */
8481 input_location
= iter
->locus
;
8482 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
8483 type_decl
, type_decl
,
8484 tf_warning_or_error
);
8486 input_location
= saved_location
;
8490 instantiate_class_template_1 (tree type
)
8492 tree templ
, args
, pattern
, t
, member
;
8496 unsigned int saved_maximum_field_alignment
;
8499 if (type
== error_mark_node
)
8500 return error_mark_node
;
8502 if (COMPLETE_OR_OPEN_TYPE_P (type
)
8503 || uses_template_parms (type
))
8506 /* Figure out which template is being instantiated. */
8507 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
8508 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
8510 /* Determine what specialization of the original template to
8512 t
= most_specialized_class (type
, templ
, tf_warning_or_error
);
8513 if (t
== error_mark_node
)
8515 TYPE_BEING_DEFINED (type
) = 1;
8516 return error_mark_node
;
8520 /* This TYPE is actually an instantiation of a partial
8521 specialization. We replace the innermost set of ARGS with
8522 the arguments appropriate for substitution. For example,
8525 template <class T> struct S {};
8526 template <class T> struct S<T*> {};
8528 and supposing that we are instantiating S<int*>, ARGS will
8529 presently be {int*} -- but we need {int}. */
8530 pattern
= TREE_TYPE (t
);
8531 args
= TREE_PURPOSE (t
);
8535 pattern
= TREE_TYPE (templ
);
8536 args
= CLASSTYPE_TI_ARGS (type
);
8539 /* If the template we're instantiating is incomplete, then clearly
8540 there's nothing we can do. */
8541 if (!COMPLETE_TYPE_P (pattern
))
8544 /* If we've recursively instantiated too many templates, stop. */
8545 if (! push_tinst_level (type
))
8548 /* Now we're really doing the instantiation. Mark the type as in
8549 the process of being defined. */
8550 TYPE_BEING_DEFINED (type
) = 1;
8552 /* We may be in the middle of deferred access check. Disable
8554 push_deferring_access_checks (dk_no_deferred
);
8556 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
8558 push_to_top_level ();
8559 /* Use #pragma pack from the template context. */
8560 saved_maximum_field_alignment
= maximum_field_alignment
;
8561 maximum_field_alignment
= TYPE_PRECISION (pattern
);
8563 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
8565 /* Set the input location to the most specialized template definition.
8566 This is needed if tsubsting causes an error. */
8567 typedecl
= TYPE_MAIN_DECL (pattern
);
8568 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
8569 DECL_SOURCE_LOCATION (typedecl
);
8571 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
8572 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
8573 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
8574 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
8575 if (ANON_AGGR_TYPE_P (pattern
))
8576 SET_ANON_AGGR_TYPE_P (type
);
8577 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
8579 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
8580 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
8581 /* Adjust visibility for template arguments. */
8582 determine_visibility (TYPE_MAIN_DECL (type
));
8584 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
8586 pbinfo
= TYPE_BINFO (pattern
);
8588 /* We should never instantiate a nested class before its enclosing
8589 class; we need to look up the nested class by name before we can
8590 instantiate it, and that lookup should instantiate the enclosing
8592 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
8593 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
8595 base_list
= NULL_TREE
;
8596 if (BINFO_N_BASE_BINFOS (pbinfo
))
8602 /* We must enter the scope containing the type, as that is where
8603 the accessibility of types named in dependent bases are
8605 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
8607 /* Substitute into each of the bases to determine the actual
8609 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
8612 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
8613 tree expanded_bases
= NULL_TREE
;
8616 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
8619 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
8620 args
, tf_error
, NULL_TREE
);
8621 if (expanded_bases
== error_mark_node
)
8624 len
= TREE_VEC_LENGTH (expanded_bases
);
8627 for (idx
= 0; idx
< len
; idx
++)
8630 /* Extract the already-expanded base class. */
8631 base
= TREE_VEC_ELT (expanded_bases
, idx
);
8633 /* Substitute to figure out the base class. */
8634 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
8637 if (base
== error_mark_node
)
8640 base_list
= tree_cons (access
, base
, base_list
);
8641 if (BINFO_VIRTUAL_P (pbase_binfo
))
8642 TREE_TYPE (base_list
) = integer_type_node
;
8646 /* The list is now in reverse order; correct that. */
8647 base_list
= nreverse (base_list
);
8650 pop_scope (pushed_scope
);
8652 /* Now call xref_basetypes to set up all the base-class
8654 xref_basetypes (type
, base_list
);
8656 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
8657 (int) ATTR_FLAG_TYPE_IN_PLACE
,
8658 args
, tf_error
, NULL_TREE
);
8659 fixup_attribute_variants (type
);
8661 /* Now that our base classes are set up, enter the scope of the
8662 class, so that name lookups into base classes, etc. will work
8663 correctly. This is precisely analogous to what we do in
8664 begin_class_definition when defining an ordinary non-template
8665 class, except we also need to push the enclosing classes. */
8666 push_nested_class (type
);
8668 /* Now members are processed in the order of declaration. */
8669 for (member
= CLASSTYPE_DECL_LIST (pattern
);
8670 member
; member
= TREE_CHAIN (member
))
8672 tree t
= TREE_VALUE (member
);
8674 if (TREE_PURPOSE (member
))
8678 /* Build new CLASSTYPE_NESTED_UTDS. */
8681 bool class_template_p
;
8683 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
8684 && TYPE_LANG_SPECIFIC (t
)
8685 && CLASSTYPE_IS_TEMPLATE (t
));
8686 /* If the member is a class template, then -- even after
8687 substitution -- there may be dependent types in the
8688 template argument list for the class. We increment
8689 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8690 that function will assume that no types are dependent
8691 when outside of a template. */
8692 if (class_template_p
)
8693 ++processing_template_decl
;
8694 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8695 if (class_template_p
)
8696 --processing_template_decl
;
8697 if (newtag
== error_mark_node
)
8700 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
8702 tree name
= TYPE_IDENTIFIER (t
);
8704 if (class_template_p
)
8705 /* Unfortunately, lookup_template_class sets
8706 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8707 instantiation (i.e., for the type of a member
8708 template class nested within a template class.)
8709 This behavior is required for
8710 maybe_process_partial_specialization to work
8711 correctly, but is not accurate in this case;
8712 the TAG is not an instantiation of anything.
8713 (The corresponding TEMPLATE_DECL is an
8714 instantiation, but the TYPE is not.) */
8715 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
8717 /* Now, we call pushtag to put this NEWTAG into the scope of
8718 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8719 pushtag calling push_template_decl. We don't have to do
8720 this for enums because it will already have been done in
8723 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
8724 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
8727 else if (TREE_CODE (t
) == FUNCTION_DECL
8728 || DECL_FUNCTION_TEMPLATE_P (t
))
8730 /* Build new TYPE_METHODS. */
8733 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8734 ++processing_template_decl
;
8735 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8736 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8737 --processing_template_decl
;
8738 set_current_access_from_decl (r
);
8739 finish_member_declaration (r
);
8740 /* Instantiate members marked with attribute used. */
8741 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8746 /* Build new TYPE_FIELDS. */
8747 if (TREE_CODE (t
) == STATIC_ASSERT
)
8751 ++c_inhibit_evaluation_warnings
;
8753 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
8754 tf_warning_or_error
, NULL_TREE
,
8755 /*integral_constant_expression_p=*/true);
8756 --c_inhibit_evaluation_warnings
;
8758 finish_static_assert (condition
,
8759 STATIC_ASSERT_MESSAGE (t
),
8760 STATIC_ASSERT_SOURCE_LOCATION (t
),
8763 else if (TREE_CODE (t
) != CONST_DECL
)
8767 /* The file and line for this declaration, to
8768 assist in error message reporting. Since we
8769 called push_tinst_level above, we don't need to
8771 input_location
= DECL_SOURCE_LOCATION (t
);
8773 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8774 ++processing_template_decl
;
8775 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
8776 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8777 --processing_template_decl
;
8778 if (TREE_CODE (r
) == VAR_DECL
)
8782 [t]he initialization (and any associated
8783 side-effects) of a static data member does
8784 not occur unless the static data member is
8785 itself used in a way that requires the
8786 definition of the static data member to
8789 Therefore, we do not substitute into the
8790 initialized for the static data member here. */
8791 finish_static_data_member_decl
8794 /*init_const_expr_p=*/false,
8795 /*asmspec_tree=*/NULL_TREE
,
8797 /* Instantiate members marked with attribute used. */
8798 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8801 else if (TREE_CODE (r
) == FIELD_DECL
)
8803 /* Determine whether R has a valid type and can be
8804 completed later. If R is invalid, then it is
8805 replaced by error_mark_node so that it will not be
8806 added to TYPE_FIELDS. */
8807 tree rtype
= TREE_TYPE (r
);
8808 if (can_complete_type_without_circularity (rtype
))
8809 complete_type (rtype
);
8811 if (!COMPLETE_TYPE_P (rtype
))
8813 cxx_incomplete_type_error (r
, rtype
);
8814 r
= error_mark_node
;
8818 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8819 such a thing will already have been added to the field
8820 list by tsubst_enum in finish_member_declaration in the
8821 CLASSTYPE_NESTED_UTDS case above. */
8822 if (!(TREE_CODE (r
) == TYPE_DECL
8823 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
8824 && DECL_ARTIFICIAL (r
)))
8826 set_current_access_from_decl (r
);
8827 finish_member_declaration (r
);
8834 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
8835 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
8837 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8839 tree friend_type
= t
;
8840 bool adjust_processing_template_decl
= false;
8842 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
8844 /* template <class T> friend class C; */
8845 friend_type
= tsubst_friend_class (friend_type
, args
);
8846 adjust_processing_template_decl
= true;
8848 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
8850 /* template <class T> friend class C::D; */
8851 friend_type
= tsubst (friend_type
, args
,
8852 tf_warning_or_error
, NULL_TREE
);
8853 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
8854 friend_type
= TREE_TYPE (friend_type
);
8855 adjust_processing_template_decl
= true;
8857 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
8858 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
8860 /* This could be either
8864 when dependent_type_p is false or
8866 template <class U> friend class T::C;
8869 friend_type
= tsubst (friend_type
, args
,
8870 tf_warning_or_error
, NULL_TREE
);
8871 /* Bump processing_template_decl for correct
8872 dependent_type_p calculation. */
8873 ++processing_template_decl
;
8874 if (dependent_type_p (friend_type
))
8875 adjust_processing_template_decl
= true;
8876 --processing_template_decl
;
8878 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
8879 && hidden_name_p (TYPE_NAME (friend_type
)))
8883 where C hasn't been declared yet. Let's lookup name
8884 from namespace scope directly, bypassing any name that
8885 come from dependent base class. */
8886 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
8888 /* The call to xref_tag_from_type does injection for friend
8890 push_nested_namespace (ns
);
8892 xref_tag_from_type (friend_type
, NULL_TREE
,
8893 /*tag_scope=*/ts_current
);
8894 pop_nested_namespace (ns
);
8896 else if (uses_template_parms (friend_type
))
8897 /* friend class C<T>; */
8898 friend_type
= tsubst (friend_type
, args
,
8899 tf_warning_or_error
, NULL_TREE
);
8904 where C is already declared or
8906 friend class C<int>;
8908 We don't have to do anything in these cases. */
8910 if (adjust_processing_template_decl
)
8911 /* Trick make_friend_class into realizing that the friend
8912 we're adding is a template, not an ordinary class. It's
8913 important that we use make_friend_class since it will
8914 perform some error-checking and output cross-reference
8916 ++processing_template_decl
;
8918 if (friend_type
!= error_mark_node
)
8919 make_friend_class (type
, friend_type
, /*complain=*/false);
8921 if (adjust_processing_template_decl
)
8922 --processing_template_decl
;
8926 /* Build new DECL_FRIENDLIST. */
8929 /* The file and line for this declaration, to
8930 assist in error message reporting. Since we
8931 called push_tinst_level above, we don't need to
8933 input_location
= DECL_SOURCE_LOCATION (t
);
8935 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8937 ++processing_template_decl
;
8938 push_deferring_access_checks (dk_no_check
);
8941 r
= tsubst_friend_function (t
, args
);
8942 add_friend (type
, r
, /*complain=*/false);
8943 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8945 pop_deferring_access_checks ();
8946 --processing_template_decl
;
8952 if (CLASSTYPE_LAMBDA_EXPR (type
))
8954 tree decl
= lambda_function (type
);
8957 instantiate_decl (decl
, false, false);
8958 maybe_add_lambda_conv_op (type
);
8961 gcc_assert (errorcount
);
8964 /* Set the file and line number information to whatever is given for
8965 the class itself. This puts error messages involving generated
8966 implicit functions at a predictable point, and the same point
8967 that would be used for non-template classes. */
8968 input_location
= DECL_SOURCE_LOCATION (typedecl
);
8970 unreverse_member_declarations (type
);
8971 finish_struct_1 (type
);
8972 TYPE_BEING_DEFINED (type
) = 0;
8974 /* We don't instantiate default arguments for member functions. 14.7.1:
8976 The implicit instantiation of a class template specialization causes
8977 the implicit instantiation of the declarations, but not of the
8978 definitions or default arguments, of the class member functions,
8979 member classes, static data members and member templates.... */
8981 /* Some typedefs referenced from within the template code need to be access
8982 checked at template instantiation time, i.e now. These types were
8983 added to the template at parsing time. Let's get those and perform
8984 the access checks then. */
8985 perform_typedefs_access_check (pattern
, args
);
8986 perform_deferred_access_checks (tf_warning_or_error
);
8987 pop_nested_class ();
8988 maximum_field_alignment
= saved_maximum_field_alignment
;
8990 pop_from_top_level ();
8991 pop_deferring_access_checks ();
8994 /* The vtable for a template class can be emitted in any translation
8995 unit in which the class is instantiated. When there is no key
8996 method, however, finish_struct_1 will already have added TYPE to
8997 the keyed_classes list. */
8998 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
8999 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
9004 /* Wrapper for instantiate_class_template_1. */
9007 instantiate_class_template (tree type
)
9010 timevar_push (TV_TEMPLATE_INST
);
9011 ret
= instantiate_class_template_1 (type
);
9012 timevar_pop (TV_TEMPLATE_INST
);
9017 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9023 else if (TYPE_P (t
))
9024 r
= tsubst (t
, args
, complain
, in_decl
);
9027 if (!(complain
& tf_warning
))
9028 ++c_inhibit_evaluation_warnings
;
9029 r
= tsubst_expr (t
, args
, complain
, in_decl
,
9030 /*integral_constant_expression_p=*/true);
9031 if (!(complain
& tf_warning
))
9032 --c_inhibit_evaluation_warnings
;
9033 /* Preserve the raw-reference nature of T. */
9034 if (TREE_TYPE (t
) && TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
9035 && REFERENCE_REF_P (r
))
9036 r
= TREE_OPERAND (r
, 0);
9041 /* Given a function parameter pack TMPL_PARM and some function parameters
9042 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9043 and set *SPEC_P to point at the next point in the list. */
9046 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
9048 /* Collect all of the extra "packed" parameters into an
9052 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
9053 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
9054 tree spec_parm
= *spec_p
;
9057 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
9059 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
9062 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9063 parmvec
= make_tree_vec (len
);
9064 parmtypevec
= make_tree_vec (len
);
9065 spec_parm
= *spec_p
;
9066 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
9068 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
9069 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
9072 /* Build the argument packs. */
9073 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
9074 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
9075 TREE_TYPE (argpack
) = argtypepack
;
9076 *spec_p
= spec_parm
;
9081 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9082 NONTYPE_ARGUMENT_PACK. */
9085 make_fnparm_pack (tree spec_parm
)
9087 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
9090 /* Substitute ARGS into T, which is an pack expansion
9091 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9092 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9093 (if only a partial substitution could be performed) or
9094 ERROR_MARK_NODE if there was an error. */
9096 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
9100 tree pack
, packs
= NULL_TREE
;
9101 bool unsubstituted_packs
= false;
9102 bool real_packs
= false;
9103 int missing_level
= 0;
9106 struct pointer_map_t
*saved_local_specializations
= NULL
;
9107 bool need_local_specializations
= false;
9110 gcc_assert (PACK_EXPANSION_P (t
));
9111 pattern
= PACK_EXPANSION_PATTERN (t
);
9113 /* Add in any args remembered from an earlier partial instantiation. */
9114 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
9116 levels
= TMPL_ARGS_DEPTH (args
);
9118 /* Determine the argument packs that will instantiate the parameter
9119 packs used in the expansion expression. While we're at it,
9120 compute the number of arguments to be expanded and make sure it
9122 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
9123 pack
= TREE_CHAIN (pack
))
9125 tree parm_pack
= TREE_VALUE (pack
);
9126 tree arg_pack
= NULL_TREE
;
9127 tree orig_arg
= NULL_TREE
;
9130 if (TREE_CODE (parm_pack
) == BASES
)
9132 if (BASES_DIRECT (parm_pack
))
9133 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9134 args
, complain
, in_decl
, false));
9136 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9137 args
, complain
, in_decl
, false));
9139 if (TREE_CODE (parm_pack
) == PARM_DECL
)
9141 if (PACK_EXPANSION_LOCAL_P (t
))
9142 arg_pack
= retrieve_local_specialization (parm_pack
);
9145 /* We can't rely on local_specializations for a parameter
9146 name used later in a function declaration (such as in a
9147 late-specified return type). Even if it exists, it might
9148 have the wrong value for a recursive call. Just make a
9149 dummy decl, since it's only used for its type. */
9150 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
9151 if (arg_pack
&& FUNCTION_PARAMETER_PACK_P (arg_pack
))
9152 /* Partial instantiation of the parm_pack, we can't build
9153 up an argument pack yet. */
9154 arg_pack
= NULL_TREE
;
9156 arg_pack
= make_fnparm_pack (arg_pack
);
9157 need_local_specializations
= true;
9163 template_parm_level_and_index (parm_pack
, &level
, &idx
);
9165 if (level
<= levels
)
9166 arg_pack
= TMPL_ARG (args
, level
, idx
);
9169 orig_arg
= arg_pack
;
9170 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
9171 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
9173 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
9174 /* This can only happen if we forget to expand an argument
9175 pack somewhere else. Just return an error, silently. */
9177 result
= make_tree_vec (1);
9178 TREE_VEC_ELT (result
, 0) = error_mark_node
;
9182 if (arg_from_parm_pack_p (arg_pack
, parm_pack
))
9183 /* The argument pack that the parameter maps to is just an
9184 expansion of the parameter itself, such as one would find
9185 in the implicit typedef of a class inside the class itself.
9186 Consider this parameter "unsubstituted", so that we will
9187 maintain the outer pack expansion. */
9188 arg_pack
= NULL_TREE
;
9193 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
9195 /* Don't bother trying to do a partial substitution with
9196 incomplete packs; we'll try again after deduction. */
9197 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
9202 else if (len
!= my_len
)
9204 if (!(complain
& tf_error
))
9205 /* Fail quietly. */;
9206 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
9207 error ("mismatched argument pack lengths while expanding "
9211 error ("mismatched argument pack lengths while expanding "
9214 return error_mark_node
;
9217 if (TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
)) == 1
9218 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
),
9220 /* This isn't a real argument pack yet. */;
9224 /* Keep track of the parameter packs and their corresponding
9226 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
9227 TREE_TYPE (packs
) = orig_arg
;
9231 /* We can't substitute for this parameter pack. We use a flag as
9232 well as the missing_level counter because function parameter
9233 packs don't have a level. */
9234 unsubstituted_packs
= true;
9235 if (!missing_level
|| missing_level
> level
)
9236 missing_level
= level
;
9240 /* We cannot expand this expansion expression, because we don't have
9241 all of the argument packs we need. */
9242 if (unsubstituted_packs
)
9246 /* We got some full packs, but we can't substitute them in until we
9247 have values for all the packs. So remember these until then. */
9250 t
= make_pack_expansion (pattern
);
9252 /* The call to add_to_template_args above assumes no overlap
9253 between saved args and new args, so prune away any fake
9254 args, i.e. those that satisfied arg_from_parm_pack_p above. */
9255 if (missing_level
&& levels
>= missing_level
)
9257 gcc_assert (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
)
9258 && missing_level
> 1);
9259 TREE_VEC_LENGTH (args
) = missing_level
- 1;
9260 save_args
= copy_node (args
);
9261 TREE_VEC_LENGTH (args
) = levels
;
9266 PACK_EXPANSION_EXTRA_ARGS (t
) = save_args
;
9270 /* There were no real arguments, we're just replacing a parameter
9271 pack with another version of itself. Substitute into the
9272 pattern and return a PACK_EXPANSION_*. The caller will need to
9274 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
9275 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9276 /*integral_constant_expression_p=*/false);
9278 t
= tsubst (pattern
, args
, complain
, in_decl
);
9279 t
= make_pack_expansion (t
);
9284 /* We could not find any argument packs that work. */
9286 return error_mark_node
;
9288 if (need_local_specializations
)
9290 /* We're in a late-specified return type, so create our own local
9291 specializations map; the current map is either NULL or (in the
9292 case of recursive unification) might have bindings that we don't
9293 want to use or alter. */
9294 saved_local_specializations
= local_specializations
;
9295 local_specializations
= pointer_map_create ();
9298 /* For each argument in each argument pack, substitute into the
9300 result
= make_tree_vec (len
);
9301 for (i
= 0; i
< len
; ++i
)
9303 /* For parameter pack, change the substitution of the parameter
9304 pack to the ith argument in its argument pack, then expand
9306 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9308 tree parm
= TREE_PURPOSE (pack
);
9311 /* Select the Ith argument from the pack. */
9312 if (TREE_CODE (parm
) == PARM_DECL
)
9316 arg
= make_node (ARGUMENT_PACK_SELECT
);
9317 ARGUMENT_PACK_SELECT_FROM_PACK (arg
) = TREE_VALUE (pack
);
9319 register_local_specialization (arg
, parm
);
9322 arg
= retrieve_local_specialization (parm
);
9327 template_parm_level_and_index (parm
, &level
, &idx
);
9331 arg
= make_node (ARGUMENT_PACK_SELECT
);
9332 ARGUMENT_PACK_SELECT_FROM_PACK (arg
) = TREE_VALUE (pack
);
9333 /* Update the corresponding argument. */
9334 TMPL_ARG (args
, level
, idx
) = arg
;
9337 /* Re-use the ARGUMENT_PACK_SELECT. */
9338 arg
= TMPL_ARG (args
, level
, idx
);
9340 ARGUMENT_PACK_SELECT_INDEX (arg
) = i
;
9343 /* Substitute into the PATTERN with the altered arguments. */
9344 if (!TYPE_P (pattern
))
9345 TREE_VEC_ELT (result
, i
) =
9346 tsubst_expr (pattern
, args
, complain
, in_decl
,
9347 /*integral_constant_expression_p=*/false);
9349 TREE_VEC_ELT (result
, i
) = tsubst (pattern
, args
, complain
, in_decl
);
9351 if (TREE_VEC_ELT (result
, i
) == error_mark_node
)
9353 result
= error_mark_node
;
9358 /* Update ARGS to restore the substitution from parameter packs to
9359 their argument packs. */
9360 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9362 tree parm
= TREE_PURPOSE (pack
);
9364 if (TREE_CODE (parm
) == PARM_DECL
)
9365 register_local_specialization (TREE_TYPE (pack
), parm
);
9369 template_parm_level_and_index (parm
, &level
, &idx
);
9371 /* Update the corresponding argument. */
9372 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
9373 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
9376 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
9380 if (need_local_specializations
)
9382 pointer_map_destroy (local_specializations
);
9383 local_specializations
= saved_local_specializations
;
9389 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9390 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9391 parameter packs; all parms generated from a function parameter pack will
9392 have the same DECL_PARM_INDEX. */
9395 get_pattern_parm (tree parm
, tree tmpl
)
9397 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9400 if (DECL_ARTIFICIAL (parm
))
9402 for (patparm
= DECL_ARGUMENTS (pattern
);
9403 patparm
; patparm
= DECL_CHAIN (patparm
))
9404 if (DECL_ARTIFICIAL (patparm
)
9405 && DECL_NAME (parm
) == DECL_NAME (patparm
))
9410 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
9411 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
9412 gcc_assert (DECL_PARM_INDEX (patparm
)
9413 == DECL_PARM_INDEX (parm
));
9419 /* Substitute ARGS into the vector or list of template arguments T. */
9422 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9425 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
9428 if (t
== error_mark_node
)
9429 return error_mark_node
;
9431 len
= TREE_VEC_LENGTH (t
);
9432 elts
= XALLOCAVEC (tree
, len
);
9434 for (i
= 0; i
< len
; i
++)
9436 tree orig_arg
= TREE_VEC_ELT (t
, i
);
9439 if (TREE_CODE (orig_arg
) == TREE_VEC
)
9440 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
9441 else if (PACK_EXPANSION_P (orig_arg
))
9443 /* Substitute into an expansion expression. */
9444 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
9446 if (TREE_CODE (new_arg
) == TREE_VEC
)
9447 /* Add to the expanded length adjustment the number of
9448 expanded arguments. We subtract one from this
9449 measurement, because the argument pack expression
9450 itself is already counted as 1 in
9451 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9452 the argument pack is empty. */
9453 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
9455 else if (ARGUMENT_PACK_P (orig_arg
))
9457 /* Substitute into each of the arguments. */
9458 new_arg
= TYPE_P (orig_arg
)
9459 ? cxx_make_type (TREE_CODE (orig_arg
))
9460 : make_node (TREE_CODE (orig_arg
));
9462 SET_ARGUMENT_PACK_ARGS (
9464 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
9465 args
, complain
, in_decl
));
9467 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
9468 new_arg
= error_mark_node
;
9470 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
9471 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
9473 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
9475 if (TREE_TYPE (new_arg
) == error_mark_node
)
9476 new_arg
= error_mark_node
;
9480 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
9482 if (new_arg
== error_mark_node
)
9483 return error_mark_node
;
9486 if (new_arg
!= orig_arg
)
9493 /* Make space for the expanded arguments coming from template
9495 t
= make_tree_vec (len
+ expanded_len_adjust
);
9496 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9497 arguments for a member template.
9498 In that case each TREE_VEC in ORIG_T represents a level of template
9499 arguments, and ORIG_T won't carry any non defaulted argument count.
9500 It will rather be the nested TREE_VECs that will carry one.
9501 In other words, ORIG_T carries a non defaulted argument count only
9502 if it doesn't contain any nested TREE_VEC. */
9503 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
9505 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
9506 count
+= expanded_len_adjust
;
9507 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
9509 for (i
= 0, out
= 0; i
< len
; i
++)
9511 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
9512 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
9513 && TREE_CODE (elts
[i
]) == TREE_VEC
)
9517 /* Now expand the template argument pack "in place". */
9518 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
9519 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
9523 TREE_VEC_ELT (t
, out
) = elts
[i
];
9531 /* Return the result of substituting ARGS into the template parameters
9532 given by PARMS. If there are m levels of ARGS and m + n levels of
9533 PARMS, then the result will contain n levels of PARMS. For
9534 example, if PARMS is `template <class T> template <class U>
9535 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9536 result will be `template <int*, double, class V>'. */
9539 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
9544 /* When substituting into a template, we must set
9545 PROCESSING_TEMPLATE_DECL as the template parameters may be
9546 dependent if they are based on one-another, and the dependency
9547 predicates are short-circuit outside of templates. */
9548 ++processing_template_decl
;
9550 for (new_parms
= &r
;
9551 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
9552 new_parms
= &(TREE_CHAIN (*new_parms
)),
9553 parms
= TREE_CHAIN (parms
))
9556 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
9559 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
9563 if (parms
== error_mark_node
)
9566 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
9568 if (tuple
== error_mark_node
)
9571 TREE_VEC_ELT (new_vec
, i
) =
9572 tsubst_template_parm (tuple
, args
, complain
);
9576 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
9577 - TMPL_ARGS_DEPTH (args
)),
9578 new_vec
, NULL_TREE
);
9581 --processing_template_decl
;
9586 /* Return the result of substituting ARGS into one template parameter
9587 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9588 parameter and which TREE_PURPOSE is the default argument of the
9589 template parameter. */
9592 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
9594 tree default_value
, parm_decl
;
9596 if (args
== NULL_TREE
9598 || t
== error_mark_node
)
9601 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
9603 default_value
= TREE_PURPOSE (t
);
9604 parm_decl
= TREE_VALUE (t
);
9606 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
9607 if (TREE_CODE (parm_decl
) == PARM_DECL
9608 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
9609 parm_decl
= error_mark_node
;
9610 default_value
= tsubst_template_arg (default_value
, args
,
9611 complain
, NULL_TREE
);
9613 return build_tree_list (default_value
, parm_decl
);
9616 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9617 type T. If T is not an aggregate or enumeration type, it is
9618 handled as if by tsubst. IN_DECL is as for tsubst. If
9619 ENTERING_SCOPE is nonzero, T is the context for a template which
9620 we are presently tsubst'ing. Return the substituted value. */
9623 tsubst_aggr_type (tree t
,
9625 tsubst_flags_t complain
,
9632 switch (TREE_CODE (t
))
9635 if (TYPE_PTRMEMFUNC_P (t
))
9636 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
9638 /* Else fall through. */
9641 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
9646 int saved_unevaluated_operand
;
9647 int saved_inhibit_evaluation_warnings
;
9649 /* In "sizeof(X<I>)" we need to evaluate "I". */
9650 saved_unevaluated_operand
= cp_unevaluated_operand
;
9651 cp_unevaluated_operand
= 0;
9652 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9653 c_inhibit_evaluation_warnings
= 0;
9655 /* First, determine the context for the type we are looking
9657 context
= TYPE_CONTEXT (t
);
9658 if (context
&& TYPE_P (context
))
9660 context
= tsubst_aggr_type (context
, args
, complain
,
9661 in_decl
, /*entering_scope=*/1);
9662 /* If context is a nested class inside a class template,
9663 it may still need to be instantiated (c++/33959). */
9664 context
= complete_type (context
);
9667 /* Then, figure out what arguments are appropriate for the
9668 type we are trying to find. For example, given:
9670 template <class T> struct S;
9671 template <class T, class U> void f(T, U) { S<U> su; }
9673 and supposing that we are instantiating f<int, double>,
9674 then our ARGS will be {int, double}, but, when looking up
9675 S we only want {double}. */
9676 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
9678 if (argvec
== error_mark_node
)
9679 r
= error_mark_node
;
9682 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
9683 entering_scope
, complain
);
9684 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
9687 cp_unevaluated_operand
= saved_unevaluated_operand
;
9688 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
9693 /* This is not a template type, so there's nothing to do. */
9697 return tsubst (t
, args
, complain
, in_decl
);
9701 /* Substitute into the default argument ARG (a default argument for
9702 FN), which has the indicated TYPE. */
9705 tsubst_default_argument (tree fn
, tree type
, tree arg
)
9707 tree saved_class_ptr
= NULL_TREE
;
9708 tree saved_class_ref
= NULL_TREE
;
9709 int errs
= errorcount
+ sorrycount
;
9711 /* This can happen in invalid code. */
9712 if (TREE_CODE (arg
) == DEFAULT_ARG
)
9715 /* This default argument came from a template. Instantiate the
9716 default argument here, not in tsubst. In the case of
9725 we must be careful to do name lookup in the scope of S<T>,
9726 rather than in the current class. */
9727 push_access_scope (fn
);
9728 /* The "this" pointer is not valid in a default argument. */
9731 saved_class_ptr
= current_class_ptr
;
9732 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
9733 saved_class_ref
= current_class_ref
;
9734 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
9737 push_deferring_access_checks(dk_no_deferred
);
9738 /* The default argument expression may cause implicitly defined
9739 member functions to be synthesized, which will result in garbage
9740 collection. We must treat this situation as if we were within
9741 the body of function so as to avoid collecting live data on the
9744 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
9745 tf_warning_or_error
, NULL_TREE
,
9746 /*integral_constant_expression_p=*/false);
9748 pop_deferring_access_checks();
9750 /* Restore the "this" pointer. */
9753 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
9754 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
9757 if (errorcount
+sorrycount
> errs
)
9758 inform (input_location
,
9759 " when instantiating default argument for call to %D", fn
);
9761 /* Make sure the default argument is reasonable. */
9762 arg
= check_default_argument (type
, arg
);
9764 pop_access_scope (fn
);
9769 /* Substitute into all the default arguments for FN. */
9772 tsubst_default_arguments (tree fn
)
9777 tmpl_args
= DECL_TI_ARGS (fn
);
9779 /* If this function is not yet instantiated, we certainly don't need
9780 its default arguments. */
9781 if (uses_template_parms (tmpl_args
))
9783 /* Don't do this again for clones. */
9784 if (DECL_CLONED_FUNCTION_P (fn
))
9787 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
9789 arg
= TREE_CHAIN (arg
))
9790 if (TREE_PURPOSE (arg
))
9791 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
9793 TREE_PURPOSE (arg
));
9796 /* Substitute the ARGS into the T, which is a _DECL. Return the
9797 result of the substitution. Issue error and warning messages under
9798 control of COMPLAIN. */
9801 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
9803 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
9804 location_t saved_loc
;
9809 /* Set the filename and linenumber to improve error-reporting. */
9810 saved_loc
= input_location
;
9811 input_location
= DECL_SOURCE_LOCATION (t
);
9813 switch (TREE_CODE (t
))
9817 /* We can get here when processing a member function template,
9818 member class template, or template template parameter. */
9819 tree decl
= DECL_TEMPLATE_RESULT (t
);
9824 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9826 /* Template template parameter is treated here. */
9827 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9828 if (new_type
== error_mark_node
)
9829 RETURN (error_mark_node
);
9830 /* If we get a real template back, return it. This can happen in
9831 the context of most_specialized_class. */
9832 if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
9836 DECL_CHAIN (r
) = NULL_TREE
;
9837 TREE_TYPE (r
) = new_type
;
9838 DECL_TEMPLATE_RESULT (r
)
9839 = build_decl (DECL_SOURCE_LOCATION (decl
),
9840 TYPE_DECL
, DECL_NAME (decl
), new_type
);
9841 DECL_TEMPLATE_PARMS (r
)
9842 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
9844 TYPE_NAME (new_type
) = r
;
9848 /* We might already have an instance of this template.
9849 The ARGS are for the surrounding class type, so the
9850 full args contain the tsubst'd args for the context,
9851 plus the innermost args from the template decl. */
9852 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
9853 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
9854 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
9855 /* Because this is a template, the arguments will still be
9856 dependent, even after substitution. If
9857 PROCESSING_TEMPLATE_DECL is not set, the dependency
9858 predicates will short-circuit. */
9859 ++processing_template_decl
;
9860 full_args
= tsubst_template_args (tmpl_args
, args
,
9862 --processing_template_decl
;
9863 if (full_args
== error_mark_node
)
9864 RETURN (error_mark_node
);
9866 /* If this is a default template template argument,
9867 tsubst might not have changed anything. */
9868 if (full_args
== tmpl_args
)
9871 hash
= hash_tmpl_and_args (t
, full_args
);
9872 spec
= retrieve_specialization (t
, full_args
, hash
);
9873 if (spec
!= NULL_TREE
)
9879 /* Make a new template decl. It will be similar to the
9880 original, but will record the current template arguments.
9881 We also create a new function declaration, which is just
9882 like the old one, but points to this new template, rather
9883 than the old one. */
9885 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
9886 DECL_CHAIN (r
) = NULL_TREE
;
9888 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
9890 if (TREE_CODE (decl
) == TYPE_DECL
9891 && !TYPE_DECL_ALIAS_P (decl
))
9894 ++processing_template_decl
;
9895 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9896 --processing_template_decl
;
9897 if (new_type
== error_mark_node
)
9898 RETURN (error_mark_node
);
9900 TREE_TYPE (r
) = new_type
;
9901 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
9902 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
9903 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
9904 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
9909 ++processing_template_decl
;
9910 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
9911 --processing_template_decl
;
9912 if (new_decl
== error_mark_node
)
9913 RETURN (error_mark_node
);
9915 DECL_TEMPLATE_RESULT (r
) = new_decl
;
9916 DECL_TI_TEMPLATE (new_decl
) = r
;
9917 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
9918 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
9919 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
9922 SET_DECL_IMPLICIT_INSTANTIATION (r
);
9923 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
9924 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
9926 /* The template parameters for this new template are all the
9927 template parameters for the old template, except the
9928 outermost level of parameters. */
9929 DECL_TEMPLATE_PARMS (r
)
9930 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
9933 if (PRIMARY_TEMPLATE_P (t
))
9934 DECL_PRIMARY_TEMPLATE (r
) = r
;
9936 if (TREE_CODE (decl
) != TYPE_DECL
)
9937 /* Record this non-type partial instantiation. */
9938 register_specialization (r
, t
,
9939 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
9947 tree argvec
= NULL_TREE
;
9955 /* Nobody should be tsubst'ing into non-template functions. */
9956 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
9958 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
9963 /* If T is not dependent, just return it. We have to
9964 increment PROCESSING_TEMPLATE_DECL because
9965 value_dependent_expression_p assumes that nothing is
9966 dependent when PROCESSING_TEMPLATE_DECL is zero. */
9967 ++processing_template_decl
;
9968 dependent_p
= value_dependent_expression_p (t
);
9969 --processing_template_decl
;
9973 /* Calculate the most general template of which R is a
9974 specialization, and the complete set of arguments used to
9976 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
9977 argvec
= tsubst_template_args (DECL_TI_ARGS
9978 (DECL_TEMPLATE_RESULT
9979 (DECL_TI_TEMPLATE (t
))),
9980 args
, complain
, in_decl
);
9981 if (argvec
== error_mark_node
)
9982 RETURN (error_mark_node
);
9984 /* Check to see if we already have this specialization. */
9985 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
9986 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
9994 /* We can see more levels of arguments than parameters if
9995 there was a specialization of a member template, like
9998 template <class T> struct S { template <class U> void f(); }
9999 template <> template <class U> void S<int>::f(U);
10001 Here, we'll be substituting into the specialization,
10002 because that's where we can find the code we actually
10003 want to generate, but we'll have enough arguments for
10004 the most general template.
10006 We also deal with the peculiar case:
10008 template <class T> struct S {
10009 template <class U> friend void f();
10011 template <class U> void f() {}
10013 template void f<double>();
10015 Here, the ARGS for the instantiation of will be {int,
10016 double}. But, we only need as many ARGS as there are
10017 levels of template parameters in CODE_PATTERN. We are
10018 careful not to get fooled into reducing the ARGS in
10021 template <class T> struct S { template <class U> void f(U); }
10022 template <class T> template <> void S<T>::f(int) {}
10024 which we can spot because the pattern will be a
10025 specialization in this case. */
10026 args_depth
= TMPL_ARGS_DEPTH (args
);
10028 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
10029 if (args_depth
> parms_depth
10030 && !DECL_TEMPLATE_SPECIALIZATION (t
))
10031 args
= get_innermost_template_args (args
, parms_depth
);
10035 /* This special case arises when we have something like this:
10037 template <class T> struct S {
10038 friend void f<int>(int, double);
10041 Here, the DECL_TI_TEMPLATE for the friend declaration
10042 will be an IDENTIFIER_NODE. We are being called from
10043 tsubst_friend_function, and we want only to create a
10044 new decl (R) with appropriate types so that we can call
10045 determine_specialization. */
10046 gen_tmpl
= NULL_TREE
;
10049 if (DECL_CLASS_SCOPE_P (t
))
10051 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
10055 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
10056 complain
, t
, /*entering_scope=*/1);
10061 ctx
= DECL_CONTEXT (t
);
10063 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10064 if (type
== error_mark_node
)
10065 RETURN (error_mark_node
);
10067 /* If we hit excessive deduction depth, the type is bogus even if
10068 it isn't error_mark_node, so don't build a decl. */
10069 if (excessive_deduction_depth
)
10070 RETURN (error_mark_node
);
10072 /* We do NOT check for matching decls pushed separately at this
10073 point, as they may not represent instantiations of this
10074 template, and in any case are considered separate under the
10077 DECL_USE_TEMPLATE (r
) = 0;
10078 TREE_TYPE (r
) = type
;
10079 /* Clear out the mangled name and RTL for the instantiation. */
10080 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10081 SET_DECL_RTL (r
, NULL
);
10082 /* Leave DECL_INITIAL set on deleted instantiations. */
10083 if (!DECL_DELETED_FN (r
))
10084 DECL_INITIAL (r
) = NULL_TREE
;
10085 DECL_CONTEXT (r
) = ctx
;
10087 if (member
&& DECL_CONV_FN_P (r
))
10088 /* Type-conversion operator. Reconstruct the name, in
10089 case it's the name of one of the template's parameters. */
10090 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
10092 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
10094 DECL_RESULT (r
) = NULL_TREE
;
10096 TREE_STATIC (r
) = 0;
10097 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
10098 DECL_EXTERNAL (r
) = 1;
10099 /* If this is an instantiation of a function with internal
10100 linkage, we already know what object file linkage will be
10101 assigned to the instantiation. */
10102 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
10103 DECL_DEFER_OUTPUT (r
) = 0;
10104 DECL_CHAIN (r
) = NULL_TREE
;
10105 DECL_PENDING_INLINE_INFO (r
) = 0;
10106 DECL_PENDING_INLINE_P (r
) = 0;
10107 DECL_SAVED_TREE (r
) = NULL_TREE
;
10108 DECL_STRUCT_FUNCTION (r
) = NULL
;
10110 /* We'll re-clone as appropriate in instantiate_template. */
10111 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
10113 /* If we aren't complaining now, return on error before we register
10114 the specialization so that we'll complain eventually. */
10115 if ((complain
& tf_error
) == 0
10116 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10117 && !grok_op_properties (r
, /*complain=*/false))
10118 RETURN (error_mark_node
);
10120 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10121 this in the special friend case mentioned above where
10122 GEN_TMPL is NULL. */
10125 DECL_TEMPLATE_INFO (r
)
10126 = build_template_info (gen_tmpl
, argvec
);
10127 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10128 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10130 /* We're not supposed to instantiate default arguments
10131 until they are called, for a template. But, for a
10134 template <class T> void f ()
10135 { extern void g(int i = T()); }
10137 we should do the substitution when the template is
10138 instantiated. We handle the member function case in
10139 instantiate_class_template since the default arguments
10140 might refer to other members of the class. */
10142 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
10143 && !uses_template_parms (argvec
))
10144 tsubst_default_arguments (r
);
10147 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
10149 /* Copy the list of befriending classes. */
10150 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
10152 friends
= &TREE_CHAIN (*friends
))
10154 *friends
= copy_node (*friends
);
10155 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
10160 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
10162 maybe_retrofit_in_chrg (r
);
10163 if (DECL_CONSTRUCTOR_P (r
))
10164 grok_ctor_properties (ctx
, r
);
10165 if (DECL_INHERITED_CTOR_BASE (r
))
10166 deduce_inheriting_ctor (r
);
10167 /* If this is an instantiation of a member template, clone it.
10168 If it isn't, that'll be handled by
10169 clone_constructors_and_destructors. */
10170 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
10171 clone_function_decl (r
, /*update_method_vec_p=*/0);
10173 else if ((complain
& tf_error
) != 0
10174 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10175 && !grok_op_properties (r
, /*complain=*/true))
10176 RETURN (error_mark_node
);
10178 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
10179 SET_DECL_FRIEND_CONTEXT (r
,
10180 tsubst (DECL_FRIEND_CONTEXT (t
),
10181 args
, complain
, in_decl
));
10183 /* Possibly limit visibility based on template args. */
10184 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10185 if (DECL_VISIBILITY_SPECIFIED (t
))
10187 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10188 DECL_ATTRIBUTES (r
)
10189 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10191 determine_visibility (r
);
10192 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
10193 && !processing_template_decl
)
10194 defaulted_late_check (r
);
10196 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10197 args
, complain
, in_decl
);
10203 tree type
= NULL_TREE
;
10205 tree expanded_types
= NULL_TREE
;
10206 tree prev_r
= NULL_TREE
;
10207 tree first_r
= NULL_TREE
;
10209 if (FUNCTION_PARAMETER_PACK_P (t
))
10211 /* If there is a local specialization that isn't a
10212 parameter pack, it means that we're doing a "simple"
10213 substitution from inside tsubst_pack_expansion. Just
10214 return the local specialization (which will be a single
10216 tree spec
= retrieve_local_specialization (t
);
10218 && TREE_CODE (spec
) == PARM_DECL
10219 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
10222 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10223 the parameters in this function parameter pack. */
10224 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10225 complain
, in_decl
);
10226 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10228 len
= TREE_VEC_LENGTH (expanded_types
);
10230 /* Zero-length parameter packs are boring. Just substitute
10233 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
10238 /* All we did was update the type. Make a note of that. */
10239 type
= expanded_types
;
10240 expanded_types
= NULL_TREE
;
10244 /* Loop through all of the parameter's we'll build. When T is
10245 a function parameter pack, LEN is the number of expanded
10246 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10248 for (i
= 0; i
< len
; ++i
)
10252 if (DECL_TEMPLATE_PARM_P (t
))
10253 SET_DECL_TEMPLATE_PARM_P (r
);
10255 if (expanded_types
)
10256 /* We're on the Ith parameter of the function parameter
10259 /* An argument of a function parameter pack is not a parameter
10261 FUNCTION_PARAMETER_PACK_P (r
) = false;
10263 /* Get the Ith type. */
10264 type
= TREE_VEC_ELT (expanded_types
, i
);
10266 /* Rename the parameter to include the index. */
10268 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10271 /* We're dealing with a normal parameter. */
10272 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10274 type
= type_decays_to (type
);
10275 TREE_TYPE (r
) = type
;
10276 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10278 if (DECL_INITIAL (r
))
10280 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
10281 DECL_INITIAL (r
) = TREE_TYPE (r
);
10283 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
10284 complain
, in_decl
);
10287 DECL_CONTEXT (r
) = NULL_TREE
;
10289 if (!DECL_TEMPLATE_PARM_P (r
))
10290 DECL_ARG_TYPE (r
) = type_passed_as (type
);
10292 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10293 args
, complain
, in_decl
);
10295 /* Keep track of the first new parameter we
10296 generate. That's what will be returned to the
10301 /* Build a proper chain of parameters when substituting
10302 into a function parameter pack. */
10304 DECL_CHAIN (prev_r
) = r
;
10307 /* If cp_unevaluated_operand is set, we're just looking for a
10308 single dummy parameter, so don't keep going. */
10309 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
10310 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
10311 complain
, DECL_CHAIN (t
));
10313 /* FIRST_R contains the start of the chain we've built. */
10323 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10324 if (type
== error_mark_node
)
10325 RETURN (error_mark_node
);
10326 TREE_TYPE (r
) = type
;
10327 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10329 if (DECL_C_BIT_FIELD (r
))
10330 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10331 non-bit-fields DECL_INITIAL is a non-static data member
10332 initializer, which gets deferred instantiation. */
10334 = tsubst_expr (DECL_INITIAL (t
), args
,
10336 /*integral_constant_expression_p=*/true);
10337 else if (DECL_INITIAL (t
))
10339 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10340 NSDMI in perform_member_init. Still set DECL_INITIAL
10341 so that we know there is one. */
10342 DECL_INITIAL (r
) = void_zero_node
;
10343 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
10344 retrofit_lang_decl (r
);
10345 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10347 /* We don't have to set DECL_CONTEXT here; it is set by
10348 finish_member_declaration. */
10349 DECL_CHAIN (r
) = NULL_TREE
;
10350 if (VOID_TYPE_P (type
))
10351 error ("instantiation of %q+D as type %qT", r
, type
);
10353 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10354 args
, complain
, in_decl
);
10359 /* We reach here only for member using decls. We also need to check
10360 uses_template_parms because DECL_DEPENDENT_P is not set for a
10361 using-declaration that designates a member of the current
10362 instantiation (c++/53549). */
10363 if (DECL_DEPENDENT_P (t
)
10364 || uses_template_parms (USING_DECL_SCOPE (t
)))
10366 tree scope
= USING_DECL_SCOPE (t
);
10367 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
10368 complain
, in_decl
);
10369 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
10370 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
10371 && name
== TYPE_IDENTIFIER (scope
))
10372 name
= TYPE_IDENTIFIER (inst_scope
);
10373 r
= do_class_using_decl (inst_scope
, name
);
10375 r
= error_mark_node
;
10378 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
10379 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
10385 DECL_CHAIN (r
) = NULL_TREE
;
10392 tree argvec
= NULL_TREE
;
10393 tree gen_tmpl
= NULL_TREE
;
10395 tree tmpl
= NULL_TREE
;
10397 tree type
= NULL_TREE
;
10400 if (TREE_CODE (t
) == TYPE_DECL
10401 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
10403 /* If this is the canonical decl, we don't have to
10404 mess with instantiations, and often we can't (for
10405 typename, template type parms and such). Note that
10406 TYPE_NAME is not correct for the above test if
10407 we've copied the type for a typedef. */
10408 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10409 if (type
== error_mark_node
)
10410 RETURN (error_mark_node
);
10411 r
= TYPE_NAME (type
);
10415 /* Check to see if we already have the specialization we
10418 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
10420 /* T is a static data member or namespace-scope entity.
10421 We have to substitute into namespace-scope variables
10422 (even though such entities are never templates) because
10425 template <class T> void f() { extern T t; }
10427 where the entity referenced is not known until
10428 instantiation time. */
10430 ctx
= DECL_CONTEXT (t
);
10431 if (DECL_CLASS_SCOPE_P (t
))
10433 ctx
= tsubst_aggr_type (ctx
, args
,
10435 in_decl
, /*entering_scope=*/1);
10436 /* If CTX is unchanged, then T is in fact the
10437 specialization we want. That situation occurs when
10438 referencing a static data member within in its own
10439 class. We can use pointer equality, rather than
10440 same_type_p, because DECL_CONTEXT is always
10442 if (ctx
== DECL_CONTEXT (t
)
10443 && (TREE_CODE (t
) != TYPE_DECL
10444 /* ... unless T is a member template; in which
10445 case our caller can be willing to create a
10446 specialization of that template represented
10448 || !(DECL_TI_TEMPLATE (t
)
10449 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))))
10455 tmpl
= DECL_TI_TEMPLATE (t
);
10456 gen_tmpl
= most_general_template (tmpl
);
10457 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
10458 if (argvec
== error_mark_node
)
10459 RETURN (error_mark_node
);
10460 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10461 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10466 /* A local variable. */
10468 /* Subsequent calls to pushdecl will fill this in. */
10470 spec
= retrieve_local_specialization (t
);
10472 /* If we already have the specialization we need, there is
10473 nothing more to do. */
10480 if (TREE_CODE (t
) == VAR_DECL
&& DECL_ANON_UNION_VAR_P (t
))
10482 /* Just use name lookup to find a member alias for an anonymous
10483 union, but then add it to the hash table. */
10484 r
= lookup_name (DECL_NAME (t
));
10485 gcc_assert (DECL_ANON_UNION_VAR_P (r
));
10486 register_local_specialization (r
, t
);
10490 /* Create a new node for the specialization we need. */
10492 if (type
== NULL_TREE
)
10494 if (is_typedef_decl (t
))
10495 type
= DECL_ORIGINAL_TYPE (t
);
10497 type
= TREE_TYPE (t
);
10498 if (TREE_CODE (t
) == VAR_DECL
10499 && VAR_HAD_UNKNOWN_BOUND (t
)
10500 && type
!= error_mark_node
)
10501 type
= strip_array_domain (type
);
10502 type
= tsubst (type
, args
, complain
, in_decl
);
10504 if (TREE_CODE (r
) == VAR_DECL
)
10506 /* Even if the original location is out of scope, the
10507 newly substituted one is not. */
10508 DECL_DEAD_FOR_LOCAL (r
) = 0;
10509 DECL_INITIALIZED_P (r
) = 0;
10510 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
10511 if (type
== error_mark_node
)
10512 RETURN (error_mark_node
);
10513 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10515 /* It may seem that this case cannot occur, since:
10520 declares a function, not a variable. However:
10523 template <typename T> void g() { T t; }
10524 template void g<f>();
10526 is an attempt to declare a variable with function
10528 error ("variable %qD has function type",
10529 /* R is not yet sufficiently initialized, so we
10530 just use its name. */
10532 RETURN (error_mark_node
);
10534 type
= complete_type (type
);
10535 /* Wait until cp_finish_decl to set this again, to handle
10536 circular dependency (template/instantiate6.C). */
10537 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
10538 type
= check_var_type (DECL_NAME (r
), type
);
10540 if (DECL_HAS_VALUE_EXPR_P (t
))
10542 tree ve
= DECL_VALUE_EXPR (t
);
10543 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
10544 /*constant_expression_p=*/false);
10545 if (REFERENCE_REF_P (ve
))
10547 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
10548 ve
= TREE_OPERAND (ve
, 0);
10550 SET_DECL_VALUE_EXPR (r
, ve
);
10553 else if (DECL_SELF_REFERENCE_P (t
))
10554 SET_DECL_SELF_REFERENCE_P (r
);
10555 TREE_TYPE (r
) = type
;
10556 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10557 DECL_CONTEXT (r
) = ctx
;
10558 /* Clear out the mangled name and RTL for the instantiation. */
10559 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10560 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10561 SET_DECL_RTL (r
, NULL
);
10562 /* The initializer must not be expanded until it is required;
10563 see [temp.inst]. */
10564 DECL_INITIAL (r
) = NULL_TREE
;
10565 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10566 SET_DECL_RTL (r
, NULL
);
10567 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
10568 if (TREE_CODE (r
) == VAR_DECL
)
10570 /* Possibly limit visibility based on template args. */
10571 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10572 if (DECL_VISIBILITY_SPECIFIED (t
))
10574 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10575 DECL_ATTRIBUTES (r
)
10576 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10578 determine_visibility (r
);
10583 /* A static data member declaration is always marked
10584 external when it is declared in-class, even if an
10585 initializer is present. We mimic the non-template
10586 processing here. */
10587 DECL_EXTERNAL (r
) = 1;
10589 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10590 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
10591 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10593 else if (cp_unevaluated_operand
)
10595 /* We're substituting this var in a decltype outside of its
10596 scope, such as for a lambda return type. Don't add it to
10597 local_specializations, do perform auto deduction. */
10598 tree auto_node
= type_uses_auto (type
);
10602 = tsubst_expr (DECL_INITIAL (t
), args
, complain
, in_decl
,
10603 /*constant_expression_p=*/false);
10604 init
= resolve_nondeduced_context (init
);
10605 TREE_TYPE (r
) = type
10606 = do_auto_deduction (type
, init
, auto_node
);
10610 register_local_specialization (r
, t
);
10612 DECL_CHAIN (r
) = NULL_TREE
;
10614 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
10616 args
, complain
, in_decl
);
10618 /* Preserve a typedef that names a type. */
10619 if (is_typedef_decl (r
))
10621 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
10622 set_underlying_type (r
);
10625 layout_decl (r
, 0);
10630 gcc_unreachable ();
10635 /* Restore the file and line information. */
10636 input_location
= saved_loc
;
10641 /* Substitute into the ARG_TYPES of a function type.
10642 If END is a TREE_CHAIN, leave it and any following types
10646 tsubst_arg_types (tree arg_types
,
10649 tsubst_flags_t complain
,
10652 tree remaining_arg_types
;
10653 tree type
= NULL_TREE
;
10655 tree expanded_args
= NULL_TREE
;
10658 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
10661 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
10662 args
, end
, complain
, in_decl
);
10663 if (remaining_arg_types
== error_mark_node
)
10664 return error_mark_node
;
10666 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
10668 /* For a pack expansion, perform substitution on the
10669 entire expression. Later on, we'll handle the arguments
10671 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
10672 args
, complain
, in_decl
);
10674 if (TREE_CODE (expanded_args
) == TREE_VEC
)
10675 /* So that we'll spin through the parameters, one by one. */
10676 i
= TREE_VEC_LENGTH (expanded_args
);
10679 /* We only partially substituted into the parameter
10680 pack. Our type is TYPE_PACK_EXPANSION. */
10681 type
= expanded_args
;
10682 expanded_args
= NULL_TREE
;
10690 type
= TREE_VEC_ELT (expanded_args
, i
);
10692 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
10694 if (type
== error_mark_node
)
10695 return error_mark_node
;
10696 if (VOID_TYPE_P (type
))
10698 if (complain
& tf_error
)
10700 error ("invalid parameter type %qT", type
);
10702 error ("in declaration %q+D", in_decl
);
10704 return error_mark_node
;
10707 /* Do array-to-pointer, function-to-pointer conversion, and ignore
10708 top-level qualifiers as required. */
10709 type
= cv_unqualified (type_decays_to (type
));
10711 /* We do not substitute into default arguments here. The standard
10712 mandates that they be instantiated only when needed, which is
10713 done in build_over_call. */
10714 default_arg
= TREE_PURPOSE (arg_types
);
10716 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
10718 /* We've instantiated a template before its default arguments
10719 have been parsed. This can happen for a nested template
10720 class, and is not an error unless we require the default
10721 argument in a call of this function. */
10722 remaining_arg_types
=
10723 tree_cons (default_arg
, type
, remaining_arg_types
);
10724 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
10727 remaining_arg_types
=
10728 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
10731 return remaining_arg_types
;
10734 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
10735 *not* handle the exception-specification for FNTYPE, because the
10736 initial substitution of explicitly provided template parameters
10737 during argument deduction forbids substitution into the
10738 exception-specification:
10742 All references in the function type of the function template to the
10743 corresponding template parameters are replaced by the specified tem-
10744 plate argument values. If a substitution in a template parameter or
10745 in the function type of the function template results in an invalid
10746 type, type deduction fails. [Note: The equivalent substitution in
10747 exception specifications is done only when the function is instanti-
10748 ated, at which point a program is ill-formed if the substitution
10749 results in an invalid type.] */
10752 tsubst_function_type (tree t
,
10754 tsubst_flags_t complain
,
10761 /* The TYPE_CONTEXT is not used for function/method types. */
10762 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
10764 /* Substitute the return type. */
10765 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10766 if (return_type
== error_mark_node
)
10767 return error_mark_node
;
10768 /* The standard does not presently indicate that creation of a
10769 function type with an invalid return type is a deduction failure.
10770 However, that is clearly analogous to creating an array of "void"
10771 or a reference to a reference. This is core issue #486. */
10772 if (TREE_CODE (return_type
) == ARRAY_TYPE
10773 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
10775 if (complain
& tf_error
)
10777 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
10778 error ("function returning an array");
10780 error ("function returning a function");
10782 return error_mark_node
;
10785 /* Substitute the argument types. */
10786 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
10787 complain
, in_decl
);
10788 if (arg_types
== error_mark_node
)
10789 return error_mark_node
;
10791 /* Construct a new type node and return it. */
10792 if (TREE_CODE (t
) == FUNCTION_TYPE
)
10794 fntype
= build_function_type (return_type
, arg_types
);
10795 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
10799 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
10800 if (! MAYBE_CLASS_TYPE_P (r
))
10804 Type deduction may fail for any of the following
10807 -- Attempting to create "pointer to member of T" when T
10808 is not a class type. */
10809 if (complain
& tf_error
)
10810 error ("creating pointer to member function of non-class type %qT",
10812 return error_mark_node
;
10815 fntype
= build_method_type_directly (r
, return_type
,
10816 TREE_CHAIN (arg_types
));
10818 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
10823 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
10824 ARGS into that specification, and return the substituted
10825 specification. If there is no specification, return NULL_TREE. */
10828 tsubst_exception_specification (tree fntype
,
10830 tsubst_flags_t complain
,
10837 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
10838 new_specs
= NULL_TREE
;
10839 if (specs
&& TREE_PURPOSE (specs
))
10841 /* A noexcept-specifier. */
10842 tree expr
= TREE_PURPOSE (specs
);
10843 if (TREE_CODE (expr
) == INTEGER_CST
)
10847 /* Defer instantiation of noexcept-specifiers to avoid
10848 excessive instantiations (c++/49107). */
10849 new_specs
= make_node (DEFERRED_NOEXCEPT
);
10850 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
10852 /* We already partially instantiated this member template,
10853 so combine the new args with the old. */
10854 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
10855 = DEFERRED_NOEXCEPT_PATTERN (expr
);
10856 DEFERRED_NOEXCEPT_ARGS (new_specs
)
10857 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
10861 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
10862 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
10866 new_specs
= tsubst_copy_and_build
10867 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
10868 /*integral_constant_expression_p=*/true);
10869 new_specs
= build_noexcept_spec (new_specs
, complain
);
10873 if (! TREE_VALUE (specs
))
10880 tree expanded_specs
= NULL_TREE
;
10882 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
10884 /* Expand the pack expansion type. */
10885 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
10889 if (expanded_specs
== error_mark_node
)
10890 return error_mark_node
;
10891 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
10892 len
= TREE_VEC_LENGTH (expanded_specs
);
10895 /* We're substituting into a member template, so
10896 we got a TYPE_PACK_EXPANSION back. Add that
10897 expansion and move on. */
10898 gcc_assert (TREE_CODE (expanded_specs
)
10899 == TYPE_PACK_EXPANSION
);
10900 new_specs
= add_exception_specifier (new_specs
,
10903 specs
= TREE_CHAIN (specs
);
10908 for (i
= 0; i
< len
; ++i
)
10910 if (expanded_specs
)
10911 spec
= TREE_VEC_ELT (expanded_specs
, i
);
10913 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
10914 if (spec
== error_mark_node
)
10916 new_specs
= add_exception_specifier (new_specs
, spec
,
10920 specs
= TREE_CHAIN (specs
);
10926 /* Take the tree structure T and replace template parameters used
10927 therein with the argument vector ARGS. IN_DECL is an associated
10928 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
10929 Issue error and warning messages under control of COMPLAIN. Note
10930 that we must be relatively non-tolerant of extensions here, in
10931 order to preserve conformance; if we allow substitutions that
10932 should not be allowed, we may allow argument deductions that should
10933 not succeed, and therefore report ambiguous overload situations
10934 where there are none. In theory, we could allow the substitution,
10935 but indicate that it should have failed, and allow our caller to
10936 make sure that the right thing happens, but we don't try to do this
10939 This function is used for dealing with types, decls and the like;
10940 for expressions, use tsubst_expr or tsubst_copy. */
10943 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10945 enum tree_code code
;
10946 tree type
, r
= NULL_TREE
;
10948 if (t
== NULL_TREE
|| t
== error_mark_node
10949 || t
== integer_type_node
10950 || t
== void_type_node
10951 || t
== char_type_node
10952 || t
== unknown_type_node
10953 || TREE_CODE (t
) == NAMESPACE_DECL
10954 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
10958 return tsubst_decl (t
, args
, complain
);
10960 if (args
== NULL_TREE
)
10963 code
= TREE_CODE (t
);
10965 if (code
== IDENTIFIER_NODE
)
10966 type
= IDENTIFIER_TYPE_VALUE (t
);
10968 type
= TREE_TYPE (t
);
10970 gcc_assert (type
!= unknown_type_node
);
10972 /* Reuse typedefs. We need to do this to handle dependent attributes,
10973 such as attribute aligned. */
10975 && typedef_variant_p (t
))
10977 tree decl
= TYPE_NAME (t
);
10979 if (alias_template_specialization_p (t
))
10981 /* DECL represents an alias template and we want to
10983 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
10984 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
10985 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
10987 else if (DECL_CLASS_SCOPE_P (decl
)
10988 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
10989 && uses_template_parms (DECL_CONTEXT (decl
)))
10991 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
10992 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
10993 r
= retrieve_specialization (tmpl
, gen_args
, 0);
10995 else if (DECL_FUNCTION_SCOPE_P (decl
)
10996 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
10997 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
10998 r
= retrieve_local_specialization (decl
);
11000 /* The typedef is from a non-template context. */
11006 r
= cp_build_qualified_type_real
11007 (r
, cp_type_quals (t
) | cp_type_quals (r
),
11008 complain
| tf_ignore_bad_quals
);
11013 /* We don't have an instantiation yet, so drop the typedef. */
11014 int quals
= cp_type_quals (t
);
11015 t
= DECL_ORIGINAL_TYPE (decl
);
11016 t
= cp_build_qualified_type_real (t
, quals
,
11017 complain
| tf_ignore_bad_quals
);
11022 && code
!= TYPENAME_TYPE
11023 && code
!= TEMPLATE_TYPE_PARM
11024 && code
!= IDENTIFIER_NODE
11025 && code
!= FUNCTION_TYPE
11026 && code
!= METHOD_TYPE
)
11027 type
= tsubst (type
, args
, complain
, in_decl
);
11028 if (type
== error_mark_node
)
11029 return error_mark_node
;
11035 case ENUMERAL_TYPE
:
11036 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
11037 /*entering_scope=*/0);
11040 case IDENTIFIER_NODE
:
11051 if (t
== integer_type_node
)
11054 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
11055 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
11059 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
11061 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
11062 /*integral_constant_expression_p=*/false);
11064 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11066 if (TREE_CODE (max
) == NOP_EXPR
11067 && TREE_SIDE_EFFECTS (omax
)
11068 && !TREE_TYPE (max
))
11069 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
11071 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11072 with TREE_SIDE_EFFECTS that indicates this is not an integral
11073 constant expression. */
11074 if (processing_template_decl
11075 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
11077 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
11078 TREE_SIDE_EFFECTS (max
) = 1;
11081 return compute_array_index_type (NULL_TREE
, max
, complain
);
11084 case TEMPLATE_TYPE_PARM
:
11085 case TEMPLATE_TEMPLATE_PARM
:
11086 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11087 case TEMPLATE_PARM_INDEX
:
11092 tree arg
= NULL_TREE
;
11096 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
11097 template_parm_level_and_index (t
, &level
, &idx
);
11099 levels
= TMPL_ARGS_DEPTH (args
);
11100 if (level
<= levels
)
11102 arg
= TMPL_ARG (args
, level
, idx
);
11104 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
11105 /* See through ARGUMENT_PACK_SELECT arguments. */
11106 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
11109 if (arg
== error_mark_node
)
11110 return error_mark_node
;
11111 else if (arg
!= NULL_TREE
)
11113 if (ARGUMENT_PACK_P (arg
))
11114 /* If ARG is an argument pack, we don't actually want to
11115 perform a substitution here, because substitutions
11116 for argument packs are only done
11117 element-by-element. We can get to this point when
11118 substituting the type of a non-type template
11119 parameter pack, when that type actually contains
11120 template parameter packs from an outer template, e.g.,
11122 template<typename... Types> struct A {
11123 template<Types... Values> struct B { };
11127 if (code
== TEMPLATE_TYPE_PARM
)
11130 gcc_assert (TYPE_P (arg
));
11132 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
11134 return cp_build_qualified_type_real
11135 (arg
, quals
, complain
| tf_ignore_bad_quals
);
11137 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11139 /* We are processing a type constructed from a
11140 template template parameter. */
11141 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
11142 args
, complain
, in_decl
);
11143 if (argvec
== error_mark_node
)
11144 return error_mark_node
;
11146 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
11147 || TREE_CODE (arg
) == TEMPLATE_DECL
11148 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
11150 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
11151 /* Consider this code:
11153 template <template <class> class Template>
11155 template <class Arg> using Bind = Template<Arg>;
11158 template <template <class> class Template, class Arg>
11159 using Instantiate = Template<Arg>; //#0
11161 template <template <class> class Template,
11164 Instantiate<Internal<Template>::template Bind,
11167 When #1 is parsed, the
11168 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11169 parameter `Template' in #0 matches the
11170 UNBOUND_CLASS_TEMPLATE representing the argument
11171 `Internal<Template>::template Bind'; We then want
11172 to assemble the type `Bind<Argument>' that can't
11173 be fully created right now, because
11174 `Internal<Template>' not being complete, the Bind
11175 template cannot be looked up in that context. So
11176 we need to "store" `Bind<Argument>' for later
11177 when the context of Bind becomes complete. Let's
11178 store that in a TYPENAME_TYPE. */
11179 return make_typename_type (TYPE_CONTEXT (arg
),
11180 build_nt (TEMPLATE_ID_EXPR
,
11181 TYPE_IDENTIFIER (arg
),
11186 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11187 are resolving nested-types in the signature of a
11188 member function templates. Otherwise ARG is a
11189 TEMPLATE_DECL and is the real template to be
11191 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
11192 arg
= TYPE_NAME (arg
);
11194 r
= lookup_template_class (arg
,
11196 DECL_CONTEXT (arg
),
11197 /*entering_scope=*/0,
11199 return cp_build_qualified_type_real
11200 (r
, cp_type_quals (t
), complain
);
11203 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11204 return convert_from_reference (unshare_expr (arg
));
11208 /* This can happen during the attempted tsubst'ing in
11209 unify. This means that we don't yet have any information
11210 about the template parameter in question. */
11213 /* Early in template argument deduction substitution, we don't
11214 want to reduce the level of 'auto', or it will be confused
11215 with a normal template parm in subsequent deduction. */
11216 if (is_auto (t
) && (complain
& tf_partial
))
11219 /* If we get here, we must have been looking at a parm for a
11220 more deeply nested template. Make a new version of this
11221 template parameter, but with a lower level. */
11224 case TEMPLATE_TYPE_PARM
:
11225 case TEMPLATE_TEMPLATE_PARM
:
11226 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11227 if (cp_type_quals (t
))
11229 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
11230 r
= cp_build_qualified_type_real
11231 (r
, cp_type_quals (t
),
11232 complain
| (code
== TEMPLATE_TYPE_PARM
11233 ? tf_ignore_bad_quals
: 0));
11238 TEMPLATE_TYPE_PARM_INDEX (r
)
11239 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
11240 r
, levels
, args
, complain
);
11241 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
11242 TYPE_MAIN_VARIANT (r
) = r
;
11243 TYPE_POINTER_TO (r
) = NULL_TREE
;
11244 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
11246 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
11247 /* We have reduced the level of the template
11248 template parameter, but not the levels of its
11249 template parameters, so canonical_type_parameter
11250 will not be able to find the canonical template
11251 template parameter for this level. Thus, we
11252 require structural equality checking to compare
11253 TEMPLATE_TEMPLATE_PARMs. */
11254 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11255 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
11256 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11258 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
11260 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11262 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
11263 complain
, in_decl
);
11264 if (argvec
== error_mark_node
)
11265 return error_mark_node
;
11267 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
11268 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
11273 case TEMPLATE_PARM_INDEX
:
11274 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
11278 gcc_unreachable ();
11286 tree purpose
, value
, chain
;
11288 if (t
== void_list_node
)
11291 purpose
= TREE_PURPOSE (t
);
11294 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
11295 if (purpose
== error_mark_node
)
11296 return error_mark_node
;
11298 value
= TREE_VALUE (t
);
11301 value
= tsubst (value
, args
, complain
, in_decl
);
11302 if (value
== error_mark_node
)
11303 return error_mark_node
;
11305 chain
= TREE_CHAIN (t
);
11306 if (chain
&& chain
!= void_type_node
)
11308 chain
= tsubst (chain
, args
, complain
, in_decl
);
11309 if (chain
== error_mark_node
)
11310 return error_mark_node
;
11312 if (purpose
== TREE_PURPOSE (t
)
11313 && value
== TREE_VALUE (t
)
11314 && chain
== TREE_CHAIN (t
))
11316 return hash_tree_cons (purpose
, value
, chain
);
11320 /* We should never be tsubsting a binfo. */
11321 gcc_unreachable ();
11324 /* A vector of template arguments. */
11325 gcc_assert (!type
);
11326 return tsubst_template_args (t
, args
, complain
, in_decl
);
11329 case REFERENCE_TYPE
:
11331 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
11336 Type deduction may fail for any of the following
11339 -- Attempting to create a pointer to reference type.
11340 -- Attempting to create a reference to a reference type or
11341 a reference to void.
11343 Core issue 106 says that creating a reference to a reference
11344 during instantiation is no longer a cause for failure. We
11345 only enforce this check in strict C++98 mode. */
11346 if ((TREE_CODE (type
) == REFERENCE_TYPE
11347 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
11348 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
11350 static location_t last_loc
;
11352 /* We keep track of the last time we issued this error
11353 message to avoid spewing a ton of messages during a
11354 single bad template instantiation. */
11355 if (complain
& tf_error
11356 && last_loc
!= input_location
)
11358 if (TREE_CODE (type
) == VOID_TYPE
)
11359 error ("forming reference to void");
11360 else if (code
== POINTER_TYPE
)
11361 error ("forming pointer to reference type %qT", type
);
11363 error ("forming reference to reference type %qT", type
);
11364 last_loc
= input_location
;
11367 return error_mark_node
;
11369 else if (code
== POINTER_TYPE
)
11371 r
= build_pointer_type (type
);
11372 if (TREE_CODE (type
) == METHOD_TYPE
)
11373 r
= build_ptrmemfunc_type (r
);
11375 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
11376 /* In C++0x, during template argument substitution, when there is an
11377 attempt to create a reference to a reference type, reference
11378 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11380 "If a template-argument for a template-parameter T names a type
11381 that is a reference to a type A, an attempt to create the type
11382 'lvalue reference to cv T' creates the type 'lvalue reference to
11383 A,' while an attempt to create the type type rvalue reference to
11384 cv T' creates the type T"
11386 r
= cp_build_reference_type
11388 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
11390 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
11391 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11393 if (r
!= error_mark_node
)
11394 /* Will this ever be needed for TYPE_..._TO values? */
11401 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
11402 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
11406 Type deduction may fail for any of the following
11409 -- Attempting to create "pointer to member of T" when T
11410 is not a class type. */
11411 if (complain
& tf_error
)
11412 error ("creating pointer to member of non-class type %qT", r
);
11413 return error_mark_node
;
11415 if (TREE_CODE (type
) == REFERENCE_TYPE
)
11417 if (complain
& tf_error
)
11418 error ("creating pointer to member reference type %qT", type
);
11419 return error_mark_node
;
11421 if (TREE_CODE (type
) == VOID_TYPE
)
11423 if (complain
& tf_error
)
11424 error ("creating pointer to member of type void");
11425 return error_mark_node
;
11427 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
11428 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11430 /* The type of the implicit object parameter gets its
11431 cv-qualifiers from the FUNCTION_TYPE. */
11433 tree method_type
= build_memfn_type (type
, r
, type_memfn_quals (type
));
11434 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
11435 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
11439 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
11443 case FUNCTION_TYPE
:
11448 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
11449 if (fntype
== error_mark_node
)
11450 return error_mark_node
;
11452 /* Substitute the exception specification. */
11453 specs
= tsubst_exception_specification (t
, args
, complain
,
11454 in_decl
, /*defer_ok*/true);
11455 if (specs
== error_mark_node
)
11456 return error_mark_node
;
11458 fntype
= build_exception_variant (fntype
, specs
);
11463 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
11464 if (domain
== error_mark_node
)
11465 return error_mark_node
;
11467 /* As an optimization, we avoid regenerating the array type if
11468 it will obviously be the same as T. */
11469 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
11472 /* These checks should match the ones in grokdeclarator.
11476 The deduction may fail for any of the following reasons:
11478 -- Attempting to create an array with an element type that
11479 is void, a function type, or a reference type, or [DR337]
11480 an abstract class type. */
11481 if (TREE_CODE (type
) == VOID_TYPE
11482 || TREE_CODE (type
) == FUNCTION_TYPE
11483 || TREE_CODE (type
) == REFERENCE_TYPE
)
11485 if (complain
& tf_error
)
11486 error ("creating array of %qT", type
);
11487 return error_mark_node
;
11489 if (ABSTRACT_CLASS_TYPE_P (type
))
11491 if (complain
& tf_error
)
11492 error ("creating array of %qT, which is an abstract class type",
11494 return error_mark_node
;
11497 r
= build_cplus_array_type (type
, domain
);
11499 if (TYPE_USER_ALIGN (t
))
11501 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
11502 TYPE_USER_ALIGN (r
) = 1;
11508 case TYPENAME_TYPE
:
11510 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11511 in_decl
, /*entering_scope=*/1);
11512 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
11513 complain
, in_decl
);
11515 if (ctx
== error_mark_node
|| f
== error_mark_node
)
11516 return error_mark_node
;
11518 if (!MAYBE_CLASS_TYPE_P (ctx
))
11520 if (complain
& tf_error
)
11521 error ("%qT is not a class, struct, or union type", ctx
);
11522 return error_mark_node
;
11524 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
11526 /* Normally, make_typename_type does not require that the CTX
11527 have complete type in order to allow things like:
11529 template <class T> struct S { typename S<T>::X Y; };
11531 But, such constructs have already been resolved by this
11532 point, so here CTX really should have complete type, unless
11533 it's a partial instantiation. */
11534 ctx
= complete_type (ctx
);
11535 if (!COMPLETE_TYPE_P (ctx
))
11537 if (complain
& tf_error
)
11538 cxx_incomplete_type_error (NULL_TREE
, ctx
);
11539 return error_mark_node
;
11543 f
= make_typename_type (ctx
, f
, typename_type
,
11544 complain
| tf_keep_type_decl
);
11545 if (f
== error_mark_node
)
11547 if (TREE_CODE (f
) == TYPE_DECL
)
11549 complain
|= tf_ignore_bad_quals
;
11553 if (TREE_CODE (f
) != TYPENAME_TYPE
)
11555 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
11557 if (complain
& tf_error
)
11558 error ("%qT resolves to %qT, which is not an enumeration type",
11561 return error_mark_node
;
11563 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
11565 if (complain
& tf_error
)
11566 error ("%qT resolves to %qT, which is is not a class type",
11569 return error_mark_node
;
11573 return cp_build_qualified_type_real
11574 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
11577 case UNBOUND_CLASS_TEMPLATE
:
11579 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11580 in_decl
, /*entering_scope=*/1);
11581 tree name
= TYPE_IDENTIFIER (t
);
11582 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
11584 if (ctx
== error_mark_node
|| name
== error_mark_node
)
11585 return error_mark_node
;
11588 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
11589 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
11596 ++cp_unevaluated_operand
;
11597 ++c_inhibit_evaluation_warnings
;
11599 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
11601 /*integral_constant_expression_p=*/false);
11603 --cp_unevaluated_operand
;
11604 --c_inhibit_evaluation_warnings
;
11606 type
= finish_typeof (type
);
11607 return cp_build_qualified_type_real (type
,
11609 | cp_type_quals (type
),
11613 case DECLTYPE_TYPE
:
11617 ++cp_unevaluated_operand
;
11618 ++c_inhibit_evaluation_warnings
;
11620 type
= tsubst_expr (DECLTYPE_TYPE_EXPR (t
), args
,
11622 /*integral_constant_expression_p=*/false);
11624 --cp_unevaluated_operand
;
11625 --c_inhibit_evaluation_warnings
;
11627 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
11628 type
= lambda_capture_field_type (type
);
11629 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
11630 type
= lambda_proxy_type (type
);
11632 type
= finish_decltype_type
11633 (type
, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
), complain
);
11634 return cp_build_qualified_type_real (type
,
11636 | cp_type_quals (type
),
11640 case UNDERLYING_TYPE
:
11642 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
11643 complain
, in_decl
);
11644 return finish_underlying_type (type
);
11647 case TYPE_ARGUMENT_PACK
:
11648 case NONTYPE_ARGUMENT_PACK
:
11650 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
11652 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
11656 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
11658 /* For template nontype argument packs, also substitute into
11660 if (code
== NONTYPE_ARGUMENT_PACK
)
11661 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11679 /* We should use one of the expression tsubsts for these codes. */
11680 gcc_unreachable ();
11683 sorry ("use of %qs in template", tree_code_name
[(int) code
]);
11684 return error_mark_node
;
11688 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
11689 type of the expression on the left-hand side of the "." or "->"
11693 tsubst_baselink (tree baselink
, tree object_type
,
11694 tree args
, tsubst_flags_t complain
, tree in_decl
)
11697 tree qualifying_scope
;
11700 tree template_args
= 0;
11701 bool template_id_p
= false;
11702 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
11704 /* A baselink indicates a function from a base class. Both the
11705 BASELINK_ACCESS_BINFO and the base class referenced may
11706 indicate bases of the template class, rather than the
11707 instantiated class. In addition, lookups that were not
11708 ambiguous before may be ambiguous now. Therefore, we perform
11709 the lookup again. */
11710 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
11711 qualifying_scope
= tsubst (qualifying_scope
, args
,
11712 complain
, in_decl
);
11713 fns
= BASELINK_FUNCTIONS (baselink
);
11714 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
11715 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
11717 template_id_p
= true;
11718 template_args
= TREE_OPERAND (fns
, 1);
11719 fns
= TREE_OPERAND (fns
, 0);
11721 template_args
= tsubst_template_args (template_args
, args
,
11722 complain
, in_decl
);
11724 name
= DECL_NAME (get_first_fn (fns
));
11725 if (IDENTIFIER_TYPENAME_P (name
))
11726 name
= mangle_conv_op_name_for_type (optype
);
11727 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
11729 return error_mark_node
;
11731 /* If lookup found a single function, mark it as used at this
11732 point. (If it lookup found multiple functions the one selected
11733 later by overload resolution will be marked as used at that
11735 if (BASELINK_P (baselink
))
11736 fns
= BASELINK_FUNCTIONS (baselink
);
11737 if (!template_id_p
&& !really_overloaded_fn (fns
))
11738 mark_used (OVL_CURRENT (fns
));
11740 /* Add back the template arguments, if present. */
11741 if (BASELINK_P (baselink
) && template_id_p
)
11742 BASELINK_FUNCTIONS (baselink
)
11743 = build_nt (TEMPLATE_ID_EXPR
,
11744 BASELINK_FUNCTIONS (baselink
),
11746 /* Update the conversion operator type. */
11747 BASELINK_OPTYPE (baselink
) = optype
;
11750 object_type
= current_class_type
;
11753 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
11759 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
11760 true if the qualified-id will be a postfix-expression in-and-of
11761 itself; false if more of the postfix-expression follows the
11762 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
11766 tsubst_qualified_id (tree qualified_id
, tree args
,
11767 tsubst_flags_t complain
, tree in_decl
,
11768 bool done
, bool address_p
)
11774 tree template_args
;
11775 location_t loc
= UNKNOWN_LOCATION
;
11777 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
11779 /* Figure out what name to look up. */
11780 name
= TREE_OPERAND (qualified_id
, 1);
11781 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
11783 is_template
= true;
11784 loc
= EXPR_LOCATION (name
);
11785 template_args
= TREE_OPERAND (name
, 1);
11787 template_args
= tsubst_template_args (template_args
, args
,
11788 complain
, in_decl
);
11789 name
= TREE_OPERAND (name
, 0);
11793 is_template
= false;
11794 template_args
= NULL_TREE
;
11797 /* Substitute into the qualifying scope. When there are no ARGS, we
11798 are just trying to simplify a non-dependent expression. In that
11799 case the qualifying scope may be dependent, and, in any case,
11800 substituting will not help. */
11801 scope
= TREE_OPERAND (qualified_id
, 0);
11804 scope
= tsubst (scope
, args
, complain
, in_decl
);
11805 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
11810 if (dependent_scope_p (scope
))
11813 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
11814 return build_qualified_name (NULL_TREE
, scope
, expr
,
11815 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
11818 if (!BASELINK_P (name
) && !DECL_P (expr
))
11820 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
11822 /* A BIT_NOT_EXPR is used to represent a destructor. */
11823 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
11825 error ("qualifying type %qT does not match destructor name ~%qT",
11826 scope
, TREE_OPERAND (expr
, 0));
11827 expr
= error_mark_node
;
11830 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
11831 /*is_type_p=*/0, false);
11834 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
11835 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
11836 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
11838 if (complain
& tf_error
)
11840 error ("dependent-name %qE is parsed as a non-type, but "
11841 "instantiation yields a type", qualified_id
);
11842 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
11844 return error_mark_node
;
11850 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
11852 /* Remember that there was a reference to this entity. */
11856 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
11858 if (complain
& tf_error
)
11859 qualified_name_lookup_error (scope
,
11860 TREE_OPERAND (qualified_id
, 1),
11861 expr
, input_location
);
11862 return error_mark_node
;
11866 expr
= lookup_template_function (expr
, template_args
);
11868 if (expr
== error_mark_node
&& complain
& tf_error
)
11869 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
11870 expr
, input_location
);
11871 else if (TYPE_P (scope
))
11873 expr
= (adjust_result_of_qualified_name_lookup
11874 (expr
, scope
, current_class_type
));
11875 expr
= (finish_qualified_id_expr
11876 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
11877 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
11878 /*template_arg_p=*/false));
11881 /* Expressions do not generally have reference type. */
11882 if (TREE_CODE (expr
) != SCOPE_REF
11883 /* However, if we're about to form a pointer-to-member, we just
11884 want the referenced member referenced. */
11885 && TREE_CODE (expr
) != OFFSET_REF
)
11886 expr
= convert_from_reference (expr
);
11891 /* Like tsubst, but deals with expressions. This function just replaces
11892 template parms; to finish processing the resultant expression, use
11893 tsubst_copy_and_build or tsubst_expr. */
11896 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11898 enum tree_code code
;
11901 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
11904 code
= TREE_CODE (t
);
11909 r
= retrieve_local_specialization (t
);
11911 if (r
== NULL_TREE
)
11913 /* We get here for a use of 'this' in an NSDMI. */
11914 if (DECL_NAME (t
) == this_identifier
11915 && at_function_scope_p ()
11916 && DECL_CONSTRUCTOR_P (current_function_decl
))
11917 return current_class_ptr
;
11919 /* This can happen for a parameter name used later in a function
11920 declaration (such as in a late-specified return type). Just
11921 make a dummy decl, since it's only used for its type. */
11922 gcc_assert (cp_unevaluated_operand
!= 0);
11923 r
= tsubst_decl (t
, args
, complain
);
11924 /* Give it the template pattern as its context; its true context
11925 hasn't been instantiated yet and this is good enough for
11927 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
11930 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
11931 r
= ARGUMENT_PACK_SELECT_ARG (r
);
11940 if (DECL_TEMPLATE_PARM_P (t
))
11941 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
11942 /* There is no need to substitute into namespace-scope
11944 if (DECL_NAMESPACE_SCOPE_P (t
))
11946 /* If ARGS is NULL, then T is known to be non-dependent. */
11947 if (args
== NULL_TREE
)
11948 return integral_constant_value (t
);
11950 /* Unfortunately, we cannot just call lookup_name here.
11953 template <int I> int f() {
11955 struct S { void g() { E e = a; } };
11958 When we instantiate f<7>::S::g(), say, lookup_name is not
11959 clever enough to find f<7>::a. */
11961 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
11962 /*entering_scope=*/0);
11964 for (v
= TYPE_VALUES (enum_type
);
11966 v
= TREE_CHAIN (v
))
11967 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
11968 return TREE_VALUE (v
);
11970 /* We didn't find the name. That should never happen; if
11971 name-lookup found it during preliminary parsing, we
11972 should find it again here during instantiation. */
11973 gcc_unreachable ();
11978 if (DECL_CONTEXT (t
))
11982 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
11983 /*entering_scope=*/1);
11984 if (ctx
!= DECL_CONTEXT (t
))
11986 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
11989 if (complain
& tf_error
)
11990 error ("using invalid field %qD", t
);
11991 return error_mark_node
;
12000 case FUNCTION_DECL
:
12001 if ((DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
12002 || local_variable_p (t
))
12003 t
= tsubst (t
, args
, complain
, in_decl
);
12007 case NAMESPACE_DECL
:
12011 /* An OVERLOAD will always be a non-dependent overload set; an
12012 overload set from function scope will just be represented with an
12013 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12014 gcc_assert (!uses_template_parms (t
));
12018 return tsubst_baselink (t
, current_class_type
, args
, complain
, in_decl
);
12020 case TEMPLATE_DECL
:
12021 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12022 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
12023 args
, complain
, in_decl
);
12024 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
12025 return tsubst (t
, args
, complain
, in_decl
);
12026 else if (DECL_CLASS_SCOPE_P (t
)
12027 && uses_template_parms (DECL_CONTEXT (t
)))
12029 /* Template template argument like the following example need
12032 template <template <class> class TT> struct C {};
12033 template <class T> struct D {
12034 template <class U> struct E {};
12039 We are processing the template argument `E' in #1 for
12040 the template instantiation #2. Originally, `E' is a
12041 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12042 have to substitute this with one having context `D<int>'. */
12044 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
12045 return lookup_field (context
, DECL_NAME(t
), 0, false);
12048 /* Ordinary template template argument. */
12052 case REINTERPRET_CAST_EXPR
:
12053 case CONST_CAST_EXPR
:
12054 case STATIC_CAST_EXPR
:
12055 case DYNAMIC_CAST_EXPR
:
12056 case IMPLICIT_CONV_EXPR
:
12060 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12061 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12064 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
12067 tree expanded
, op
= TREE_OPERAND (t
, 0);
12070 if (SIZEOF_EXPR_TYPE_P (t
))
12071 op
= TREE_TYPE (op
);
12073 ++cp_unevaluated_operand
;
12074 ++c_inhibit_evaluation_warnings
;
12075 /* We only want to compute the number of arguments. */
12076 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
12077 --cp_unevaluated_operand
;
12078 --c_inhibit_evaluation_warnings
;
12080 if (TREE_CODE (expanded
) == TREE_VEC
)
12081 len
= TREE_VEC_LENGTH (expanded
);
12083 if (expanded
== error_mark_node
)
12084 return error_mark_node
;
12085 else if (PACK_EXPANSION_P (expanded
)
12086 || (TREE_CODE (expanded
) == TREE_VEC
12088 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
12090 if (TREE_CODE (expanded
) == TREE_VEC
)
12091 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
12093 if (TYPE_P (expanded
))
12094 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
12095 complain
& tf_error
);
12097 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
12098 complain
& tf_error
);
12101 return build_int_cst (size_type_node
, len
);
12103 if (SIZEOF_EXPR_TYPE_P (t
))
12105 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
12106 args
, complain
, in_decl
);
12107 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
12108 r
= build1 (SIZEOF_EXPR
,
12109 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
12110 SIZEOF_EXPR_TYPE_P (r
) = 1;
12117 case TRUTH_NOT_EXPR
:
12120 case UNARY_PLUS_EXPR
: /* Unary + */
12122 case AT_ENCODE_EXPR
:
12126 case REALPART_EXPR
:
12127 case IMAGPART_EXPR
:
12129 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12130 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12132 case COMPONENT_REF
:
12137 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
12138 name
= TREE_OPERAND (t
, 1);
12139 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
12141 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12142 complain
, in_decl
);
12143 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12145 else if (TREE_CODE (name
) == SCOPE_REF
12146 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
12148 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12149 complain
, in_decl
);
12150 name
= TREE_OPERAND (name
, 1);
12151 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12152 complain
, in_decl
);
12153 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12154 name
= build_qualified_name (/*type=*/NULL_TREE
,
12156 /*template_p=*/false);
12158 else if (BASELINK_P (name
))
12159 name
= tsubst_baselink (name
,
12160 non_reference (TREE_TYPE (object
)),
12164 name
= tsubst_copy (name
, args
, complain
, in_decl
);
12165 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
12171 case TRUNC_DIV_EXPR
:
12172 case CEIL_DIV_EXPR
:
12173 case FLOOR_DIV_EXPR
:
12174 case ROUND_DIV_EXPR
:
12175 case EXACT_DIV_EXPR
:
12179 case TRUNC_MOD_EXPR
:
12180 case FLOOR_MOD_EXPR
:
12181 case TRUTH_ANDIF_EXPR
:
12182 case TRUTH_ORIF_EXPR
:
12183 case TRUTH_AND_EXPR
:
12184 case TRUTH_OR_EXPR
:
12197 case COMPOUND_EXPR
:
12200 case PREDECREMENT_EXPR
:
12201 case PREINCREMENT_EXPR
:
12202 case POSTDECREMENT_EXPR
:
12203 case POSTINCREMENT_EXPR
:
12205 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12206 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12209 return build_qualified_name (/*type=*/NULL_TREE
,
12210 tsubst_copy (TREE_OPERAND (t
, 0),
12211 args
, complain
, in_decl
),
12212 tsubst_copy (TREE_OPERAND (t
, 1),
12213 args
, complain
, in_decl
),
12214 QUALIFIED_NAME_IS_TEMPLATE (t
));
12219 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12220 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12221 NULL_TREE
, NULL_TREE
);
12225 int n
= VL_EXP_OPERAND_LENGTH (t
);
12226 tree result
= build_vl_exp (CALL_EXPR
, n
);
12228 for (i
= 0; i
< n
; i
++)
12229 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
12230 complain
, in_decl
);
12236 case PSEUDO_DTOR_EXPR
:
12239 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12240 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12241 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12242 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12249 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12250 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12251 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12252 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
12259 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12260 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12261 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
12262 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
12266 case TEMPLATE_ID_EXPR
:
12268 /* Substituted template arguments */
12269 tree fn
= TREE_OPERAND (t
, 0);
12270 tree targs
= TREE_OPERAND (t
, 1);
12272 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
12274 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12276 return lookup_template_function (fn
, targs
);
12281 tree purpose
, value
, chain
;
12283 if (t
== void_list_node
)
12286 purpose
= TREE_PURPOSE (t
);
12288 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
12289 value
= TREE_VALUE (t
);
12291 value
= tsubst_copy (value
, args
, complain
, in_decl
);
12292 chain
= TREE_CHAIN (t
);
12293 if (chain
&& chain
!= void_type_node
)
12294 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
12295 if (purpose
== TREE_PURPOSE (t
)
12296 && value
== TREE_VALUE (t
)
12297 && chain
== TREE_CHAIN (t
))
12299 return tree_cons (purpose
, value
, chain
);
12304 case ENUMERAL_TYPE
:
12306 case TEMPLATE_TYPE_PARM
:
12307 case TEMPLATE_TEMPLATE_PARM
:
12308 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12309 case TEMPLATE_PARM_INDEX
:
12311 case REFERENCE_TYPE
:
12313 case FUNCTION_TYPE
:
12316 case TYPENAME_TYPE
:
12317 case UNBOUND_CLASS_TEMPLATE
:
12319 case DECLTYPE_TYPE
:
12321 return tsubst (t
, args
, complain
, in_decl
);
12323 case IDENTIFIER_NODE
:
12324 if (IDENTIFIER_TYPENAME_P (t
))
12326 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12327 return mangle_conv_op_name_for_type (new_type
);
12333 /* This is handled by tsubst_copy_and_build. */
12334 gcc_unreachable ();
12337 return build_x_va_arg (EXPR_LOCATION (t
),
12338 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
12340 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
12342 case CLEANUP_POINT_EXPR
:
12343 /* We shouldn't have built any of these during initial template
12344 generation. Instead, they should be built during instantiation
12345 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12346 gcc_unreachable ();
12350 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12351 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12352 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12353 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
12354 mark_used (TREE_OPERAND (r
, 1));
12357 case EXPR_PACK_EXPANSION
:
12358 error ("invalid use of pack expansion expression");
12359 return error_mark_node
;
12361 case NONTYPE_ARGUMENT_PACK
:
12362 error ("use %<...%> to expand argument pack");
12363 return error_mark_node
;
12370 /* Instantiate any typedefs in the type. */
12371 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12372 r
= fold_convert (type
, t
);
12373 gcc_assert (TREE_CODE (r
) == code
);
12378 /* These can sometimes show up in a partial instantiation, but never
12379 involve template parms. */
12380 gcc_assert (!uses_template_parms (t
));
12384 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12385 gcc_checking_assert (false);
12390 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12393 tsubst_omp_clauses (tree clauses
, tree args
, tsubst_flags_t complain
,
12396 tree new_clauses
= NULL
, nc
, oc
;
12398 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
12400 nc
= copy_node (oc
);
12401 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
12404 switch (OMP_CLAUSE_CODE (nc
))
12406 case OMP_CLAUSE_LASTPRIVATE
:
12407 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
12409 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
12410 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
12411 in_decl
, /*integral_constant_expression_p=*/false);
12412 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
12413 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
12416 case OMP_CLAUSE_PRIVATE
:
12417 case OMP_CLAUSE_SHARED
:
12418 case OMP_CLAUSE_FIRSTPRIVATE
:
12419 case OMP_CLAUSE_REDUCTION
:
12420 case OMP_CLAUSE_COPYIN
:
12421 case OMP_CLAUSE_COPYPRIVATE
:
12422 case OMP_CLAUSE_IF
:
12423 case OMP_CLAUSE_NUM_THREADS
:
12424 case OMP_CLAUSE_SCHEDULE
:
12425 case OMP_CLAUSE_COLLAPSE
:
12426 case OMP_CLAUSE_FINAL
:
12427 OMP_CLAUSE_OPERAND (nc
, 0)
12428 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12429 in_decl
, /*integral_constant_expression_p=*/false);
12431 case OMP_CLAUSE_NOWAIT
:
12432 case OMP_CLAUSE_ORDERED
:
12433 case OMP_CLAUSE_DEFAULT
:
12434 case OMP_CLAUSE_UNTIED
:
12435 case OMP_CLAUSE_MERGEABLE
:
12438 gcc_unreachable ();
12442 return finish_omp_clauses (nreverse (new_clauses
));
12445 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12448 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
12451 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12453 tree purpose
, value
, chain
;
12458 if (TREE_CODE (t
) != TREE_LIST
)
12459 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12460 /*function_p=*/false,
12461 /*integral_constant_expression_p=*/false);
12463 if (t
== void_list_node
)
12466 purpose
= TREE_PURPOSE (t
);
12468 purpose
= RECUR (purpose
);
12469 value
= TREE_VALUE (t
);
12472 if (TREE_CODE (value
) != LABEL_DECL
)
12473 value
= RECUR (value
);
12476 value
= lookup_label (DECL_NAME (value
));
12477 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
12478 TREE_USED (value
) = 1;
12481 chain
= TREE_CHAIN (t
);
12482 if (chain
&& chain
!= void_type_node
)
12483 chain
= RECUR (chain
);
12484 return tree_cons (purpose
, value
, chain
);
12488 /* Substitute one OMP_FOR iterator. */
12491 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
12492 tree condv
, tree incrv
, tree
*clauses
,
12493 tree args
, tsubst_flags_t complain
, tree in_decl
,
12494 bool integral_constant_expression_p
)
12496 #define RECUR(NODE) \
12497 tsubst_expr ((NODE), args, complain, in_decl, \
12498 integral_constant_expression_p)
12499 tree decl
, init
, cond
, incr
;
12502 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
12503 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
12504 decl
= TREE_OPERAND (init
, 0);
12505 init
= TREE_OPERAND (init
, 1);
12506 /* Do this before substituting into decl to handle 'auto'. */
12507 init_decl
= (init
&& TREE_CODE (init
) == DECL_EXPR
);
12508 init
= RECUR (init
);
12509 decl
= RECUR (decl
);
12512 gcc_assert (!processing_template_decl
);
12513 init
= DECL_INITIAL (decl
);
12514 DECL_INITIAL (decl
) = NULL_TREE
;
12517 gcc_assert (!type_dependent_expression_p (decl
));
12519 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
12521 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
12522 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
12523 if (TREE_CODE (incr
) == MODIFY_EXPR
)
12524 incr
= build_x_modify_expr (EXPR_LOCATION (incr
),
12525 RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
12526 RECUR (TREE_OPERAND (incr
, 1)),
12529 incr
= RECUR (incr
);
12530 TREE_VEC_ELT (declv
, i
) = decl
;
12531 TREE_VEC_ELT (initv
, i
) = init
;
12532 TREE_VEC_ELT (condv
, i
) = cond
;
12533 TREE_VEC_ELT (incrv
, i
) = incr
;
12537 if (init
&& !init_decl
)
12540 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
12542 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
12543 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
12544 && OMP_CLAUSE_DECL (c
) == decl
)
12546 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
12547 && OMP_CLAUSE_DECL (c
) == decl
)
12548 error ("iteration variable %qD should not be firstprivate", decl
);
12549 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
12550 && OMP_CLAUSE_DECL (c
) == decl
)
12551 error ("iteration variable %qD should not be reduction", decl
);
12555 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
12556 OMP_CLAUSE_DECL (c
) = decl
;
12557 c
= finish_omp_clauses (c
);
12560 OMP_CLAUSE_CHAIN (c
) = *clauses
;
12565 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
12566 if (COMPARISON_CLASS_P (cond
))
12567 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
12568 RECUR (TREE_OPERAND (cond
, 0)),
12569 RECUR (TREE_OPERAND (cond
, 1)));
12571 cond
= RECUR (cond
);
12572 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
12573 switch (TREE_CODE (incr
))
12575 case PREINCREMENT_EXPR
:
12576 case PREDECREMENT_EXPR
:
12577 case POSTINCREMENT_EXPR
:
12578 case POSTDECREMENT_EXPR
:
12579 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
12580 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
12583 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
12584 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
12586 tree rhs
= TREE_OPERAND (incr
, 1);
12587 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
12588 RECUR (TREE_OPERAND (incr
, 0)),
12589 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
12590 RECUR (TREE_OPERAND (rhs
, 0)),
12591 RECUR (TREE_OPERAND (rhs
, 1))));
12594 incr
= RECUR (incr
);
12597 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
12598 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
12600 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
12601 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
12602 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
12603 TREE_TYPE (decl
), lhs
,
12604 RECUR (TREE_OPERAND (incr
, 2))));
12606 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
12607 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
12608 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
12610 tree rhs
= TREE_OPERAND (incr
, 2);
12611 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
12612 RECUR (TREE_OPERAND (incr
, 0)),
12613 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
12614 RECUR (TREE_OPERAND (rhs
, 0)),
12615 RECUR (TREE_OPERAND (rhs
, 1))));
12618 incr
= RECUR (incr
);
12621 incr
= RECUR (incr
);
12625 TREE_VEC_ELT (declv
, i
) = decl
;
12626 TREE_VEC_ELT (initv
, i
) = init
;
12627 TREE_VEC_ELT (condv
, i
) = cond
;
12628 TREE_VEC_ELT (incrv
, i
) = incr
;
12632 /* Like tsubst_copy for expressions, etc. but also does semantic
12636 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
12637 bool integral_constant_expression_p
)
12639 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12640 #define RECUR(NODE) \
12641 tsubst_expr ((NODE), args, complain, in_decl, \
12642 integral_constant_expression_p)
12648 if (t
== NULL_TREE
|| t
== error_mark_node
)
12651 loc
= input_location
;
12652 if (EXPR_HAS_LOCATION (t
))
12653 input_location
= EXPR_LOCATION (t
);
12654 if (STATEMENT_CODE_P (TREE_CODE (t
)))
12655 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
12657 switch (TREE_CODE (t
))
12659 case STATEMENT_LIST
:
12661 tree_stmt_iterator i
;
12662 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
12663 RECUR (tsi_stmt (i
));
12667 case CTOR_INITIALIZER
:
12668 finish_mem_initializers (tsubst_initializer_list
12669 (TREE_OPERAND (t
, 0), args
));
12673 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
12677 tmp
= RECUR (EXPR_STMT_EXPR (t
));
12678 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
12679 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
12681 finish_expr_stmt (tmp
);
12685 do_using_directive (USING_STMT_NAMESPACE (t
));
12690 tree decl
, pattern_decl
;
12693 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
12694 if (TREE_CODE (decl
) == LABEL_DECL
)
12695 finish_label_decl (DECL_NAME (decl
));
12696 else if (TREE_CODE (decl
) == USING_DECL
)
12698 tree scope
= USING_DECL_SCOPE (decl
);
12699 tree name
= DECL_NAME (decl
);
12702 scope
= tsubst (scope
, args
, complain
, in_decl
);
12703 decl
= lookup_qualified_name (scope
, name
,
12704 /*is_type_p=*/false,
12705 /*complain=*/false);
12706 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
12707 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
12709 do_local_using_decl (decl
, scope
, name
);
12713 init
= DECL_INITIAL (decl
);
12714 decl
= tsubst (decl
, args
, complain
, in_decl
);
12715 if (decl
!= error_mark_node
)
12717 /* By marking the declaration as instantiated, we avoid
12718 trying to instantiate it. Since instantiate_decl can't
12719 handle local variables, and since we've already done
12720 all that needs to be done, that's the right thing to
12722 if (TREE_CODE (decl
) == VAR_DECL
)
12723 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
12724 if (TREE_CODE (decl
) == VAR_DECL
12725 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
12726 /* Anonymous aggregates are a special case. */
12727 finish_anon_union (decl
);
12728 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
12730 DECL_CONTEXT (decl
) = current_function_decl
;
12731 if (DECL_NAME (decl
) == this_identifier
)
12733 tree lam
= DECL_CONTEXT (current_function_decl
);
12734 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
12735 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
12737 insert_capture_proxy (decl
);
12739 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
12740 /* We already did a pushtag. */;
12743 int const_init
= false;
12744 maybe_push_decl (decl
);
12745 if (TREE_CODE (decl
) == VAR_DECL
12746 && DECL_PRETTY_FUNCTION_P (decl
))
12748 /* For __PRETTY_FUNCTION__ we have to adjust the
12750 const char *const name
12751 = cxx_printable_name (current_function_decl
, 2);
12752 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
12756 tree t
= RECUR (init
);
12760 /* If we had an initializer but it
12761 instantiated to nothing,
12762 value-initialize the object. This will
12763 only occur when the initializer was a
12764 pack expansion where the parameter packs
12765 used in that expansion were of length
12767 init
= build_value_init (TREE_TYPE (decl
),
12769 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
12770 init
= get_target_expr_sfinae (init
, complain
);
12776 if (TREE_CODE (decl
) == VAR_DECL
)
12777 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
12779 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
12788 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12789 RECUR (FOR_INIT_STMT (t
));
12790 finish_for_init_stmt (stmt
);
12791 tmp
= RECUR (FOR_COND (t
));
12792 finish_for_cond (tmp
, stmt
);
12793 tmp
= RECUR (FOR_EXPR (t
));
12794 finish_for_expr (tmp
, stmt
);
12795 RECUR (FOR_BODY (t
));
12796 finish_for_stmt (stmt
);
12799 case RANGE_FOR_STMT
:
12802 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12803 decl
= RANGE_FOR_DECL (t
);
12804 decl
= tsubst (decl
, args
, complain
, in_decl
);
12805 maybe_push_decl (decl
);
12806 expr
= RECUR (RANGE_FOR_EXPR (t
));
12807 stmt
= cp_convert_range_for (stmt
, decl
, expr
);
12808 RECUR (RANGE_FOR_BODY (t
));
12809 finish_for_stmt (stmt
);
12814 stmt
= begin_while_stmt ();
12815 tmp
= RECUR (WHILE_COND (t
));
12816 finish_while_stmt_cond (tmp
, stmt
);
12817 RECUR (WHILE_BODY (t
));
12818 finish_while_stmt (stmt
);
12822 stmt
= begin_do_stmt ();
12823 RECUR (DO_BODY (t
));
12824 finish_do_body (stmt
);
12825 tmp
= RECUR (DO_COND (t
));
12826 finish_do_stmt (tmp
, stmt
);
12830 stmt
= begin_if_stmt ();
12831 tmp
= RECUR (IF_COND (t
));
12832 finish_if_stmt_cond (tmp
, stmt
);
12833 RECUR (THEN_CLAUSE (t
));
12834 finish_then_clause (stmt
);
12836 if (ELSE_CLAUSE (t
))
12838 begin_else_clause (stmt
);
12839 RECUR (ELSE_CLAUSE (t
));
12840 finish_else_clause (stmt
);
12843 finish_if_stmt (stmt
);
12847 if (BIND_EXPR_BODY_BLOCK (t
))
12848 stmt
= begin_function_body ();
12850 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
12851 ? BCS_TRY_BLOCK
: 0);
12853 RECUR (BIND_EXPR_BODY (t
));
12855 if (BIND_EXPR_BODY_BLOCK (t
))
12856 finish_function_body (stmt
);
12858 finish_compound_stmt (stmt
);
12862 finish_break_stmt ();
12865 case CONTINUE_STMT
:
12866 finish_continue_stmt ();
12870 stmt
= begin_switch_stmt ();
12871 tmp
= RECUR (SWITCH_STMT_COND (t
));
12872 finish_switch_cond (tmp
, stmt
);
12873 RECUR (SWITCH_STMT_BODY (t
));
12874 finish_switch_stmt (stmt
);
12877 case CASE_LABEL_EXPR
:
12878 finish_case_label (EXPR_LOCATION (t
),
12879 RECUR (CASE_LOW (t
)),
12880 RECUR (CASE_HIGH (t
)));
12885 tree decl
= LABEL_EXPR_LABEL (t
);
12888 label
= finish_label_stmt (DECL_NAME (decl
));
12889 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
12890 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
12895 tmp
= GOTO_DESTINATION (t
);
12896 if (TREE_CODE (tmp
) != LABEL_DECL
)
12897 /* Computed goto's must be tsubst'd into. On the other hand,
12898 non-computed gotos must not be; the identifier in question
12899 will have no binding. */
12902 tmp
= DECL_NAME (tmp
);
12903 finish_goto_stmt (tmp
);
12907 tmp
= finish_asm_stmt
12908 (ASM_VOLATILE_P (t
),
12909 RECUR (ASM_STRING (t
)),
12910 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
12911 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
12912 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
12913 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
12915 tree asm_expr
= tmp
;
12916 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
12917 asm_expr
= TREE_OPERAND (asm_expr
, 0);
12918 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
12925 stmt
= begin_try_block ();
12926 RECUR (TRY_STMTS (t
));
12927 finish_cleanup_try_block (stmt
);
12928 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
12932 tree compound_stmt
= NULL_TREE
;
12934 if (FN_TRY_BLOCK_P (t
))
12935 stmt
= begin_function_try_block (&compound_stmt
);
12937 stmt
= begin_try_block ();
12939 RECUR (TRY_STMTS (t
));
12941 if (FN_TRY_BLOCK_P (t
))
12942 finish_function_try_block (stmt
);
12944 finish_try_block (stmt
);
12946 RECUR (TRY_HANDLERS (t
));
12947 if (FN_TRY_BLOCK_P (t
))
12948 finish_function_handler_sequence (stmt
, compound_stmt
);
12950 finish_handler_sequence (stmt
);
12956 tree decl
= HANDLER_PARMS (t
);
12960 decl
= tsubst (decl
, args
, complain
, in_decl
);
12961 /* Prevent instantiate_decl from trying to instantiate
12962 this variable. We've already done all that needs to be
12964 if (decl
!= error_mark_node
)
12965 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
12967 stmt
= begin_handler ();
12968 finish_handler_parms (decl
, stmt
);
12969 RECUR (HANDLER_BODY (t
));
12970 finish_handler (stmt
);
12975 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
12976 if (CLASS_TYPE_P (tmp
))
12978 /* Local classes are not independent templates; they are
12979 instantiated along with their containing function. And this
12980 way we don't have to deal with pushing out of one local class
12981 to instantiate a member of another local class. */
12983 /* Closures are handled by the LAMBDA_EXPR. */
12984 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
12985 complete_type (tmp
);
12986 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
12987 if (!DECL_ARTIFICIAL (fn
))
12988 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
12992 case STATIC_ASSERT
:
12996 ++c_inhibit_evaluation_warnings
;
12998 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
13001 /*integral_constant_expression_p=*/true);
13002 --c_inhibit_evaluation_warnings
;
13004 finish_static_assert (condition
,
13005 STATIC_ASSERT_MESSAGE (t
),
13006 STATIC_ASSERT_SOURCE_LOCATION (t
),
13007 /*member_p=*/false);
13012 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
),
13013 args
, complain
, in_decl
);
13014 stmt
= begin_omp_parallel ();
13015 RECUR (OMP_PARALLEL_BODY (t
));
13016 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
13017 = OMP_PARALLEL_COMBINED (t
);
13021 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
),
13022 args
, complain
, in_decl
);
13023 stmt
= begin_omp_task ();
13024 RECUR (OMP_TASK_BODY (t
));
13025 finish_omp_task (tmp
, stmt
);
13030 tree clauses
, body
, pre_body
;
13031 tree declv
, initv
, condv
, incrv
;
13034 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
),
13035 args
, complain
, in_decl
);
13036 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13037 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13038 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13039 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13041 stmt
= begin_omp_structured_block ();
13043 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
13044 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
13045 &clauses
, args
, complain
, in_decl
,
13046 integral_constant_expression_p
);
13048 pre_body
= push_stmt_list ();
13049 RECUR (OMP_FOR_PRE_BODY (t
));
13050 pre_body
= pop_stmt_list (pre_body
);
13052 body
= push_stmt_list ();
13053 RECUR (OMP_FOR_BODY (t
));
13054 body
= pop_stmt_list (body
);
13056 t
= finish_omp_for (EXPR_LOCATION (t
), declv
, initv
, condv
, incrv
,
13057 body
, pre_body
, clauses
);
13059 add_stmt (finish_omp_structured_block (stmt
));
13065 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), args
, complain
, in_decl
);
13066 stmt
= push_stmt_list ();
13067 RECUR (OMP_BODY (t
));
13068 stmt
= pop_stmt_list (stmt
);
13071 OMP_BODY (t
) = stmt
;
13072 OMP_CLAUSES (t
) = tmp
;
13080 stmt
= push_stmt_list ();
13081 RECUR (OMP_BODY (t
));
13082 stmt
= pop_stmt_list (stmt
);
13085 OMP_BODY (t
) = stmt
;
13090 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
13091 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
13093 tree op1
= TREE_OPERAND (t
, 1);
13094 tree rhs1
= NULL_TREE
;
13096 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
13098 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
13099 op1
= TREE_OPERAND (op1
, 1);
13101 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13102 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13103 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
13104 NULL_TREE
, NULL_TREE
, rhs1
);
13108 tree op1
= TREE_OPERAND (t
, 1);
13109 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
13110 tree rhs1
= NULL_TREE
;
13111 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
13112 enum tree_code opcode
= NOP_EXPR
;
13113 if (code
== OMP_ATOMIC_READ
)
13115 v
= RECUR (TREE_OPERAND (op1
, 0));
13116 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13118 else if (code
== OMP_ATOMIC_CAPTURE_OLD
13119 || code
== OMP_ATOMIC_CAPTURE_NEW
)
13121 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
13122 v
= RECUR (TREE_OPERAND (op1
, 0));
13123 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13124 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
13126 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
13127 op11
= TREE_OPERAND (op11
, 1);
13129 lhs
= RECUR (TREE_OPERAND (op11
, 0));
13130 rhs
= RECUR (TREE_OPERAND (op11
, 1));
13131 opcode
= TREE_CODE (op11
);
13136 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13137 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13139 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
);
13143 case TRANSACTION_EXPR
:
13146 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
13147 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
13149 if (TRANSACTION_EXPR_IS_STMT (t
))
13151 tree body
= TRANSACTION_EXPR_BODY (t
);
13152 tree noex
= NULL_TREE
;
13153 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
13155 noex
= MUST_NOT_THROW_COND (body
);
13156 if (noex
== NULL_TREE
)
13157 noex
= boolean_true_node
;
13158 body
= TREE_OPERAND (body
, 0);
13160 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
13162 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
13166 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
13167 RECUR (TRANSACTION_EXPR_BODY (t
)),
13174 case MUST_NOT_THROW_EXPR
:
13175 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t
, 0)),
13176 RECUR (MUST_NOT_THROW_COND (t
))));
13178 case EXPR_PACK_EXPANSION
:
13179 error ("invalid use of pack expansion expression");
13180 RETURN (error_mark_node
);
13182 case NONTYPE_ARGUMENT_PACK
:
13183 error ("use %<...%> to expand argument pack");
13184 RETURN (error_mark_node
);
13186 case COMPOUND_EXPR
:
13187 tmp
= RECUR (TREE_OPERAND (t
, 0));
13188 if (tmp
== NULL_TREE
)
13189 /* If the first operand was a statement, we're done with it. */
13190 RETURN (RECUR (TREE_OPERAND (t
, 1)));
13191 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
13192 RECUR (TREE_OPERAND (t
, 1)),
13196 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
13198 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13199 /*function_p=*/false,
13200 integral_constant_expression_p
));
13203 RETURN (NULL_TREE
);
13205 input_location
= loc
;
13211 /* T is a postfix-expression that is not being used in a function
13212 call. Return the substituted version of T. */
13215 tsubst_non_call_postfix_expression (tree t
, tree args
,
13216 tsubst_flags_t complain
,
13219 if (TREE_CODE (t
) == SCOPE_REF
)
13220 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
13221 /*done=*/false, /*address_p=*/false);
13223 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13224 /*function_p=*/false,
13225 /*integral_constant_expression_p=*/false);
13230 /* Like tsubst but deals with expressions and performs semantic
13231 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13234 tsubst_copy_and_build (tree t
,
13236 tsubst_flags_t complain
,
13239 bool integral_constant_expression_p
)
13241 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13242 #define RECUR(NODE) \
13243 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13244 /*function_p=*/false, \
13245 integral_constant_expression_p)
13250 if (t
== NULL_TREE
|| t
== error_mark_node
)
13253 loc
= input_location
;
13254 if (EXPR_HAS_LOCATION (t
))
13255 input_location
= EXPR_LOCATION (t
);
13257 switch (TREE_CODE (t
))
13261 /* Fall through. */
13262 case IDENTIFIER_NODE
:
13266 bool non_integral_constant_expression_p
;
13267 const char *error_msg
;
13269 if (IDENTIFIER_TYPENAME_P (t
))
13271 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13272 t
= mangle_conv_op_name_for_type (new_type
);
13275 /* Look up the name. */
13276 decl
= lookup_name (t
);
13278 /* By convention, expressions use ERROR_MARK_NODE to indicate
13279 failure, not NULL_TREE. */
13280 if (decl
== NULL_TREE
)
13281 decl
= error_mark_node
;
13283 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
13285 integral_constant_expression_p
,
13286 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx0x
),
13287 &non_integral_constant_expression_p
,
13288 /*template_p=*/false,
13290 /*address_p=*/false,
13291 /*template_arg_p=*/false,
13296 if (!function_p
&& TREE_CODE (decl
) == IDENTIFIER_NODE
)
13298 if (complain
& tf_error
)
13299 unqualified_name_lookup_error (decl
);
13300 decl
= error_mark_node
;
13305 case TEMPLATE_ID_EXPR
:
13308 tree templ
= RECUR (TREE_OPERAND (t
, 0));
13309 tree targs
= TREE_OPERAND (t
, 1);
13312 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
13314 if (TREE_CODE (templ
) == COMPONENT_REF
)
13316 object
= TREE_OPERAND (templ
, 0);
13317 templ
= TREE_OPERAND (templ
, 1);
13320 object
= NULL_TREE
;
13321 templ
= lookup_template_function (templ
, targs
);
13324 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
13325 object
, templ
, NULL_TREE
));
13327 RETURN (baselink_for_fns (templ
));
13332 tree r
= RECUR (TREE_OPERAND (t
, 0));
13334 if (REFERENCE_REF_P (t
))
13336 /* A type conversion to reference type will be enclosed in
13337 such an indirect ref, but the substitution of the cast
13338 will have also added such an indirect ref. */
13339 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
13340 r
= convert_from_reference (r
);
13343 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
, complain
);
13349 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13350 RECUR (TREE_OPERAND (t
, 0))));
13352 case IMPLICIT_CONV_EXPR
:
13354 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13355 tree expr
= RECUR (TREE_OPERAND (t
, 0));
13356 int flags
= LOOKUP_IMPLICIT
;
13357 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
13358 flags
= LOOKUP_NORMAL
;
13359 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
13366 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13367 RECUR (TREE_OPERAND (t
, 0))));
13370 case REINTERPRET_CAST_EXPR
:
13371 case CONST_CAST_EXPR
:
13372 case DYNAMIC_CAST_EXPR
:
13373 case STATIC_CAST_EXPR
:
13376 tree op
, r
= NULL_TREE
;
13378 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13379 if (integral_constant_expression_p
13380 && !cast_valid_in_integral_constant_expression_p (type
))
13382 if (complain
& tf_error
)
13383 error ("a cast to a type other than an integral or "
13384 "enumeration type cannot appear in a constant-expression");
13385 RETURN (error_mark_node
);
13388 op
= RECUR (TREE_OPERAND (t
, 0));
13390 ++c_inhibit_evaluation_warnings
;
13391 switch (TREE_CODE (t
))
13394 r
= build_functional_cast (type
, op
, complain
);
13396 case REINTERPRET_CAST_EXPR
:
13397 r
= build_reinterpret_cast (type
, op
, complain
);
13399 case CONST_CAST_EXPR
:
13400 r
= build_const_cast (type
, op
, complain
);
13402 case DYNAMIC_CAST_EXPR
:
13403 r
= build_dynamic_cast (type
, op
, complain
);
13405 case STATIC_CAST_EXPR
:
13406 r
= build_static_cast (type
, op
, complain
);
13409 gcc_unreachable ();
13411 --c_inhibit_evaluation_warnings
;
13416 case POSTDECREMENT_EXPR
:
13417 case POSTINCREMENT_EXPR
:
13418 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13419 args
, complain
, in_decl
);
13420 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
, complain
));
13422 case PREDECREMENT_EXPR
:
13423 case PREINCREMENT_EXPR
:
13427 case TRUTH_NOT_EXPR
:
13428 case UNARY_PLUS_EXPR
: /* Unary + */
13429 case REALPART_EXPR
:
13430 case IMAGPART_EXPR
:
13431 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
13432 RECUR (TREE_OPERAND (t
, 0)), complain
));
13434 case FIX_TRUNC_EXPR
:
13435 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
13439 op1
= TREE_OPERAND (t
, 0);
13440 if (TREE_CODE (op1
) == LABEL_DECL
)
13441 RETURN (finish_label_address_expr (DECL_NAME (op1
),
13442 EXPR_LOCATION (op1
)));
13443 if (TREE_CODE (op1
) == SCOPE_REF
)
13444 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
13445 /*done=*/true, /*address_p=*/true);
13447 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
13449 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
, complain
));
13454 case TRUNC_DIV_EXPR
:
13455 case CEIL_DIV_EXPR
:
13456 case FLOOR_DIV_EXPR
:
13457 case ROUND_DIV_EXPR
:
13458 case EXACT_DIV_EXPR
:
13462 case TRUNC_MOD_EXPR
:
13463 case FLOOR_MOD_EXPR
:
13464 case TRUTH_ANDIF_EXPR
:
13465 case TRUTH_ORIF_EXPR
:
13466 case TRUTH_AND_EXPR
:
13467 case TRUTH_OR_EXPR
:
13485 ++c_inhibit_evaluation_warnings
;
13487 r
= build_x_binary_op
13488 (input_location
, TREE_CODE (t
),
13489 RECUR (TREE_OPERAND (t
, 0)),
13490 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
13492 : TREE_CODE (TREE_OPERAND (t
, 0))),
13493 RECUR (TREE_OPERAND (t
, 1)),
13494 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
13496 : TREE_CODE (TREE_OPERAND (t
, 1))),
13499 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
13500 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13502 --c_inhibit_evaluation_warnings
;
13508 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
13509 /*address_p=*/false));
13511 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13512 args
, complain
, in_decl
);
13513 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
13514 RECUR (TREE_OPERAND (t
, 1)), complain
));
13517 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
13518 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
13525 op1
= TREE_OPERAND (t
, 0);
13526 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
13527 op1
= TREE_TYPE (op1
);
13530 /* When there are no ARGS, we are trying to evaluate a
13531 non-dependent expression from the parser. Trying to do
13532 the substitutions may not work. */
13534 op1
= TREE_TYPE (op1
);
13538 ++cp_unevaluated_operand
;
13539 ++c_inhibit_evaluation_warnings
;
13541 op1
= tsubst (op1
, args
, complain
, in_decl
);
13543 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13544 /*function_p=*/false,
13545 /*integral_constant_expression_p=*/
13547 --cp_unevaluated_operand
;
13548 --c_inhibit_evaluation_warnings
;
13551 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
13552 complain
& tf_error
);
13554 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
13555 complain
& tf_error
);
13556 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
13558 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
13562 r
= build_min (SIZEOF_EXPR
, size_type_node
,
13563 build1 (NOP_EXPR
, op1
, error_mark_node
));
13564 SIZEOF_EXPR_TYPE_P (r
) = 1;
13567 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
13568 TREE_SIDE_EFFECTS (r
) = 0;
13569 TREE_READONLY (r
) = 1;
13571 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
13576 case AT_ENCODE_EXPR
:
13578 op1
= TREE_OPERAND (t
, 0);
13579 ++cp_unevaluated_operand
;
13580 ++c_inhibit_evaluation_warnings
;
13581 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13582 /*function_p=*/false,
13583 /*integral_constant_expression_p=*/false);
13584 --cp_unevaluated_operand
;
13585 --c_inhibit_evaluation_warnings
;
13586 RETURN (objc_build_encode_expr (op1
));
13589 case NOEXCEPT_EXPR
:
13590 op1
= TREE_OPERAND (t
, 0);
13591 ++cp_unevaluated_operand
;
13592 ++c_inhibit_evaluation_warnings
;
13593 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13594 /*function_p=*/false,
13595 /*integral_constant_expression_p=*/false);
13596 --cp_unevaluated_operand
;
13597 --c_inhibit_evaluation_warnings
;
13598 RETURN (finish_noexcept_expr (op1
, complain
));
13602 tree r
= build_x_modify_expr
13603 (EXPR_LOCATION (t
),
13604 RECUR (TREE_OPERAND (t
, 0)),
13605 TREE_CODE (TREE_OPERAND (t
, 1)),
13606 RECUR (TREE_OPERAND (t
, 2)),
13608 /* TREE_NO_WARNING must be set if either the expression was
13609 parenthesized or it uses an operator such as >>= rather
13610 than plain assignment. In the former case, it was already
13611 set and must be copied. In the latter case,
13612 build_x_modify_expr sets it and it must not be reset
13614 if (TREE_NO_WARNING (t
))
13615 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13620 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13621 args
, complain
, in_decl
);
13622 /* Remember that there was a reference to this entity. */
13625 RETURN (build_x_arrow (input_location
, op1
, complain
));
13629 tree placement
= RECUR (TREE_OPERAND (t
, 0));
13630 tree init
= RECUR (TREE_OPERAND (t
, 3));
13631 vec
<tree
, va_gc
> *placement_vec
;
13632 vec
<tree
, va_gc
> *init_vec
;
13635 if (placement
== NULL_TREE
)
13636 placement_vec
= NULL
;
13639 placement_vec
= make_tree_vector ();
13640 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
13641 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
13644 /* If there was an initializer in the original tree, but it
13645 instantiated to an empty list, then we should pass a
13646 non-NULL empty vector to tell build_new that it was an
13647 empty initializer() rather than no initializer. This can
13648 only happen when the initializer is a pack expansion whose
13649 parameter packs are of length zero. */
13650 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
13654 init_vec
= make_tree_vector ();
13655 if (init
== void_zero_node
)
13656 gcc_assert (init_vec
!= NULL
);
13659 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
13660 vec_safe_push (init_vec
, TREE_VALUE (init
));
13664 ret
= build_new (&placement_vec
,
13665 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
13666 RECUR (TREE_OPERAND (t
, 2)),
13668 NEW_EXPR_USE_GLOBAL (t
),
13671 if (placement_vec
!= NULL
)
13672 release_tree_vector (placement_vec
);
13673 if (init_vec
!= NULL
)
13674 release_tree_vector (init_vec
);
13680 RETURN (delete_sanity
13681 (RECUR (TREE_OPERAND (t
, 0)),
13682 RECUR (TREE_OPERAND (t
, 1)),
13683 DELETE_EXPR_USE_VEC (t
),
13684 DELETE_EXPR_USE_GLOBAL (t
),
13687 case COMPOUND_EXPR
:
13688 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
13689 RECUR (TREE_OPERAND (t
, 0)),
13690 RECUR (TREE_OPERAND (t
, 1)),
13696 vec
<tree
, va_gc
> *call_args
;
13697 unsigned int nargs
, i
;
13702 function
= CALL_EXPR_FN (t
);
13703 /* When we parsed the expression, we determined whether or
13704 not Koenig lookup should be performed. */
13705 koenig_p
= KOENIG_LOOKUP_P (t
);
13706 if (TREE_CODE (function
) == SCOPE_REF
)
13708 qualified_p
= true;
13709 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
13711 /*address_p=*/false);
13713 else if (koenig_p
&& TREE_CODE (function
) == IDENTIFIER_NODE
)
13715 /* Do nothing; calling tsubst_copy_and_build on an identifier
13716 would incorrectly perform unqualified lookup again.
13718 Note that we can also have an IDENTIFIER_NODE if the earlier
13719 unqualified lookup found a member function; in that case
13720 koenig_p will be false and we do want to do the lookup
13721 again to find the instantiated member function.
13723 FIXME but doing that causes c++/15272, so we need to stop
13724 using IDENTIFIER_NODE in that situation. */
13725 qualified_p
= false;
13729 if (TREE_CODE (function
) == COMPONENT_REF
)
13731 tree op
= TREE_OPERAND (function
, 1);
13733 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
13734 || (BASELINK_P (op
)
13735 && BASELINK_QUALIFIED_P (op
)));
13738 qualified_p
= false;
13740 function
= tsubst_copy_and_build (function
, args
, complain
,
13743 integral_constant_expression_p
);
13745 if (BASELINK_P (function
))
13746 qualified_p
= true;
13749 nargs
= call_expr_nargs (t
);
13750 call_args
= make_tree_vector ();
13751 for (i
= 0; i
< nargs
; ++i
)
13753 tree arg
= CALL_EXPR_ARG (t
, i
);
13755 if (!PACK_EXPANSION_P (arg
))
13756 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
13759 /* Expand the pack expansion and push each entry onto
13761 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
13762 if (TREE_CODE (arg
) == TREE_VEC
)
13764 unsigned int len
, j
;
13766 len
= TREE_VEC_LENGTH (arg
);
13767 for (j
= 0; j
< len
; ++j
)
13769 tree value
= TREE_VEC_ELT (arg
, j
);
13770 if (value
!= NULL_TREE
)
13771 value
= convert_from_reference (value
);
13772 vec_safe_push (call_args
, value
);
13777 /* A partial substitution. Add one entry. */
13778 vec_safe_push (call_args
, arg
);
13783 /* We do not perform argument-dependent lookup if normal
13784 lookup finds a non-function, in accordance with the
13785 expected resolution of DR 218. */
13787 && ((is_overloaded_fn (function
)
13788 /* If lookup found a member function, the Koenig lookup is
13789 not appropriate, even if an unqualified-name was used
13790 to denote the function. */
13791 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
13792 || TREE_CODE (function
) == IDENTIFIER_NODE
)
13793 /* Only do this when substitution turns a dependent call
13794 into a non-dependent call. */
13795 && type_dependent_expression_p_push (t
)
13796 && !any_type_dependent_arguments_p (call_args
))
13797 function
= perform_koenig_lookup (function
, call_args
, false,
13800 if (TREE_CODE (function
) == IDENTIFIER_NODE
13801 && !any_type_dependent_arguments_p (call_args
))
13803 if (koenig_p
&& (complain
& tf_warning_or_error
))
13805 /* For backwards compatibility and good diagnostics, try
13806 the unqualified lookup again if we aren't in SFINAE
13808 tree unq
= (tsubst_copy_and_build
13809 (function
, args
, complain
, in_decl
, true,
13810 integral_constant_expression_p
));
13811 if (unq
== error_mark_node
)
13812 RETURN (error_mark_node
);
13814 if (unq
!= function
)
13817 if (TREE_CODE (fn
) == INDIRECT_REF
)
13818 fn
= TREE_OPERAND (fn
, 0);
13819 if (TREE_CODE (fn
) == COMPONENT_REF
)
13820 fn
= TREE_OPERAND (fn
, 1);
13821 if (is_overloaded_fn (fn
))
13822 fn
= get_first_fn (fn
);
13823 permerror (EXPR_LOC_OR_HERE (t
),
13824 "%qD was not declared in this scope, "
13825 "and no declarations were found by "
13826 "argument-dependent lookup at the point "
13827 "of instantiation", function
);
13829 /* Can't say anything more. */;
13830 else if (DECL_CLASS_SCOPE_P (fn
))
13832 inform (EXPR_LOC_OR_HERE (t
),
13833 "declarations in dependent base %qT are "
13834 "not found by unqualified lookup",
13835 DECL_CLASS_CONTEXT (fn
));
13836 if (current_class_ptr
)
13837 inform (EXPR_LOC_OR_HERE (t
),
13838 "use %<this->%D%> instead", function
);
13840 inform (EXPR_LOC_OR_HERE (t
),
13841 "use %<%T::%D%> instead",
13842 current_class_name
, function
);
13845 inform (0, "%q+D declared here, later in the "
13846 "translation unit", fn
);
13850 if (TREE_CODE (function
) == IDENTIFIER_NODE
)
13852 if (complain
& tf_error
)
13853 unqualified_name_lookup_error (function
);
13854 release_tree_vector (call_args
);
13855 RETURN (error_mark_node
);
13859 /* Remember that there was a reference to this entity. */
13860 if (DECL_P (function
))
13861 mark_used (function
);
13863 if (TREE_CODE (function
) == OFFSET_REF
)
13864 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
13866 else if (TREE_CODE (function
) == COMPONENT_REF
)
13868 tree instance
= TREE_OPERAND (function
, 0);
13869 tree fn
= TREE_OPERAND (function
, 1);
13871 if (processing_template_decl
13872 && (type_dependent_expression_p (instance
)
13873 || (!BASELINK_P (fn
)
13874 && TREE_CODE (fn
) != FIELD_DECL
)
13875 || type_dependent_expression_p (fn
)
13876 || any_type_dependent_arguments_p (call_args
)))
13877 ret
= build_nt_call_vec (function
, call_args
);
13878 else if (!BASELINK_P (fn
))
13879 ret
= finish_call_expr (function
, &call_args
,
13880 /*disallow_virtual=*/false,
13881 /*koenig_p=*/false,
13884 ret
= (build_new_method_call
13886 &call_args
, NULL_TREE
,
13887 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
13892 ret
= finish_call_expr (function
, &call_args
,
13893 /*disallow_virtual=*/qualified_p
,
13897 release_tree_vector (call_args
);
13904 tree cond
= RECUR (TREE_OPERAND (t
, 0));
13907 if (TREE_CODE (cond
) == INTEGER_CST
)
13909 if (integer_zerop (cond
))
13911 ++c_inhibit_evaluation_warnings
;
13912 exp1
= RECUR (TREE_OPERAND (t
, 1));
13913 --c_inhibit_evaluation_warnings
;
13914 exp2
= RECUR (TREE_OPERAND (t
, 2));
13918 exp1
= RECUR (TREE_OPERAND (t
, 1));
13919 ++c_inhibit_evaluation_warnings
;
13920 exp2
= RECUR (TREE_OPERAND (t
, 2));
13921 --c_inhibit_evaluation_warnings
;
13926 exp1
= RECUR (TREE_OPERAND (t
, 1));
13927 exp2
= RECUR (TREE_OPERAND (t
, 2));
13930 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
13931 cond
, exp1
, exp2
, complain
));
13934 case PSEUDO_DTOR_EXPR
:
13935 RETURN (finish_pseudo_destructor_expr
13936 (RECUR (TREE_OPERAND (t
, 0)),
13937 RECUR (TREE_OPERAND (t
, 1)),
13938 tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
)));
13942 tree purpose
, value
, chain
;
13944 if (t
== void_list_node
)
13947 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
13948 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
13950 /* We have pack expansions, so expand those and
13951 create a new list out of it. */
13952 tree purposevec
= NULL_TREE
;
13953 tree valuevec
= NULL_TREE
;
13957 /* Expand the argument expressions. */
13958 if (TREE_PURPOSE (t
))
13959 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
13960 complain
, in_decl
);
13961 if (TREE_VALUE (t
))
13962 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
13963 complain
, in_decl
);
13965 /* Build the rest of the list. */
13966 chain
= TREE_CHAIN (t
);
13967 if (chain
&& chain
!= void_type_node
)
13968 chain
= RECUR (chain
);
13970 /* Determine the number of arguments. */
13971 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
13973 len
= TREE_VEC_LENGTH (purposevec
);
13974 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
13976 else if (TREE_CODE (valuevec
) == TREE_VEC
)
13977 len
= TREE_VEC_LENGTH (valuevec
);
13980 /* Since we only performed a partial substitution into
13981 the argument pack, we only RETURN (a single list
13983 if (purposevec
== TREE_PURPOSE (t
)
13984 && valuevec
== TREE_VALUE (t
)
13985 && chain
== TREE_CHAIN (t
))
13988 RETURN (tree_cons (purposevec
, valuevec
, chain
));
13991 /* Convert the argument vectors into a TREE_LIST */
13995 /* Grab the Ith values. */
13997 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
14000 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
14003 /* Build the list (backwards). */
14004 chain
= tree_cons (purpose
, value
, chain
);
14010 purpose
= TREE_PURPOSE (t
);
14012 purpose
= RECUR (purpose
);
14013 value
= TREE_VALUE (t
);
14015 value
= RECUR (value
);
14016 chain
= TREE_CHAIN (t
);
14017 if (chain
&& chain
!= void_type_node
)
14018 chain
= RECUR (chain
);
14019 if (purpose
== TREE_PURPOSE (t
)
14020 && value
== TREE_VALUE (t
)
14021 && chain
== TREE_CHAIN (t
))
14023 RETURN (tree_cons (purpose
, value
, chain
));
14026 case COMPONENT_REF
:
14032 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14033 args
, complain
, in_decl
);
14034 /* Remember that there was a reference to this entity. */
14035 if (DECL_P (object
))
14036 mark_used (object
);
14037 object_type
= TREE_TYPE (object
);
14039 member
= TREE_OPERAND (t
, 1);
14040 if (BASELINK_P (member
))
14041 member
= tsubst_baselink (member
,
14042 non_reference (TREE_TYPE (object
)),
14043 args
, complain
, in_decl
);
14045 member
= tsubst_copy (member
, args
, complain
, in_decl
);
14046 if (member
== error_mark_node
)
14047 RETURN (error_mark_node
);
14049 if (type_dependent_expression_p (object
))
14050 /* We can't do much here. */;
14051 else if (!CLASS_TYPE_P (object_type
))
14053 if (scalarish_type_p (object_type
))
14055 tree s
= NULL_TREE
;
14056 tree dtor
= member
;
14058 if (TREE_CODE (dtor
) == SCOPE_REF
)
14060 s
= TREE_OPERAND (dtor
, 0);
14061 dtor
= TREE_OPERAND (dtor
, 1);
14063 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
14065 dtor
= TREE_OPERAND (dtor
, 0);
14067 RETURN (finish_pseudo_destructor_expr (object
, s
, dtor
));
14071 else if (TREE_CODE (member
) == SCOPE_REF
14072 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
14074 /* Lookup the template functions now that we know what the
14076 tree scope
= TREE_OPERAND (member
, 0);
14077 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
14078 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
14079 member
= lookup_qualified_name (scope
, tmpl
,
14080 /*is_type_p=*/false,
14081 /*complain=*/false);
14082 if (BASELINK_P (member
))
14084 BASELINK_FUNCTIONS (member
)
14085 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
14087 member
= (adjust_result_of_qualified_name_lookup
14088 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
14093 qualified_name_lookup_error (scope
, tmpl
, member
,
14095 RETURN (error_mark_node
);
14098 else if (TREE_CODE (member
) == SCOPE_REF
14099 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
14100 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
14102 if (complain
& tf_error
)
14104 if (TYPE_P (TREE_OPERAND (member
, 0)))
14105 error ("%qT is not a class or namespace",
14106 TREE_OPERAND (member
, 0));
14108 error ("%qD is not a class or namespace",
14109 TREE_OPERAND (member
, 0));
14111 RETURN (error_mark_node
);
14113 else if (TREE_CODE (member
) == FIELD_DECL
)
14114 RETURN (finish_non_static_data_member (member
, object
, NULL_TREE
));
14116 RETURN (finish_class_member_access_expr (object
, member
,
14117 /*template_p=*/false,
14122 RETURN (build_throw
14123 (RECUR (TREE_OPERAND (t
, 0))));
14127 vec
<constructor_elt
, va_gc
> *n
;
14128 constructor_elt
*ce
;
14129 unsigned HOST_WIDE_INT idx
;
14130 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14131 bool process_index_p
;
14133 bool need_copy_p
= false;
14136 if (type
== error_mark_node
)
14137 RETURN (error_mark_node
);
14139 /* digest_init will do the wrong thing if we let it. */
14140 if (type
&& TYPE_PTRMEMFUNC_P (type
))
14143 /* We do not want to process the index of aggregate
14144 initializers as they are identifier nodes which will be
14145 looked up by digest_init. */
14146 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
14148 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
14149 newlen
= vec_safe_length (n
);
14150 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
14152 if (ce
->index
&& process_index_p
)
14153 ce
->index
= RECUR (ce
->index
);
14155 if (PACK_EXPANSION_P (ce
->value
))
14157 /* Substitute into the pack expansion. */
14158 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
14161 if (ce
->value
== error_mark_node
14162 || PACK_EXPANSION_P (ce
->value
))
14164 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
14165 /* Just move the argument into place. */
14166 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
14169 /* Update the length of the final CONSTRUCTOR
14170 arguments vector, and note that we will need to
14172 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
14173 need_copy_p
= true;
14177 ce
->value
= RECUR (ce
->value
);
14182 vec
<constructor_elt
, va_gc
> *old_n
= n
;
14184 vec_alloc (n
, newlen
);
14185 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
14187 if (TREE_CODE (ce
->value
) == TREE_VEC
)
14189 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
14190 for (i
= 0; i
< len
; ++i
)
14191 CONSTRUCTOR_APPEND_ELT (n
, 0,
14192 TREE_VEC_ELT (ce
->value
, i
));
14195 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
14199 r
= build_constructor (init_list_type_node
, n
);
14200 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
14202 if (TREE_HAS_CONSTRUCTOR (t
))
14203 RETURN (finish_compound_literal (type
, r
, complain
));
14205 TREE_TYPE (r
) = type
;
14211 tree operand_0
= TREE_OPERAND (t
, 0);
14212 if (TYPE_P (operand_0
))
14214 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
14215 RETURN (get_typeid (operand_0
));
14219 operand_0
= RECUR (operand_0
);
14220 RETURN (build_typeid (operand_0
));
14231 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
14233 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
14234 /* If the original type was a reference, we'll be wrapped in
14235 the appropriate INDIRECT_REF. */
14236 r
= convert_from_reference (r
);
14241 RETURN (build_x_va_arg (EXPR_LOCATION (t
),
14242 RECUR (TREE_OPERAND (t
, 0)),
14243 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
)));
14245 case OFFSETOF_EXPR
:
14246 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t
, 0))));
14250 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
14251 complain
, in_decl
);
14253 tree type2
= TRAIT_EXPR_TYPE2 (t
);
14255 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
14257 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
14262 tree old_stmt_expr
= cur_stmt_expr
;
14263 tree stmt_expr
= begin_stmt_expr ();
14265 cur_stmt_expr
= stmt_expr
;
14266 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
14267 integral_constant_expression_p
);
14268 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
14269 cur_stmt_expr
= old_stmt_expr
;
14271 /* If the resulting list of expression statement is empty,
14272 fold it further into void_zero_node. */
14273 if (empty_expr_stmt_p (stmt_expr
))
14274 stmt_expr
= void_zero_node
;
14276 RETURN (stmt_expr
);
14281 tree r
= build_lambda_expr ();
14283 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
14284 LAMBDA_EXPR_CLOSURE (r
) = type
;
14285 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
14287 LAMBDA_EXPR_LOCATION (r
)
14288 = LAMBDA_EXPR_LOCATION (t
);
14289 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
14290 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
14291 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
14292 LAMBDA_EXPR_DISCRIMINATOR (r
)
14293 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
14294 /* For a function scope, we want to use tsubst so that we don't
14295 complain about referring to an auto function before its return
14296 type has been deduced. Otherwise, we want to use tsubst_copy so
14297 that we look up the existing field/parameter/variable rather
14298 than build a new one. */
14299 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
14300 if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
14301 scope
= tsubst (LAMBDA_EXPR_EXTRA_SCOPE (t
), args
,
14302 complain
, in_decl
);
14304 scope
= RECUR (scope
);
14305 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
14306 LAMBDA_EXPR_RETURN_TYPE (r
)
14307 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
14309 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
14310 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
14312 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
14313 determine_visibility (TYPE_NAME (type
));
14314 /* Now that we know visibility, instantiate the type so we have a
14315 declaration of the op() for later calls to lambda_function. */
14316 complete_type (type
);
14318 /* The capture list refers to closure members, so this needs to
14319 wait until after we finish instantiating the type. */
14320 LAMBDA_EXPR_CAPTURE_LIST (r
)
14321 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t
));
14322 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
14324 RETURN (build_lambda_object (r
));
14328 /* We can get here for a constant initializer of non-dependent type.
14329 FIXME stop folding in cp_parser_initializer_clause. */
14330 gcc_assert (TREE_CONSTANT (t
));
14332 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
14334 TREE_CONSTANT (r
) = true;
14338 case TRANSACTION_EXPR
:
14339 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
14340 integral_constant_expression_p
));
14343 /* Handle Objective-C++ constructs, if appropriate. */
14346 = objcp_tsubst_copy_and_build (t
, args
, complain
,
14347 in_decl
, /*function_p=*/false);
14351 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
14357 input_location
= loc
;
14361 /* Verify that the instantiated ARGS are valid. For type arguments,
14362 make sure that the type's linkage is ok. For non-type arguments,
14363 make sure they are constants if they are integral or enumerations.
14364 Emit an error under control of COMPLAIN, and return TRUE on error. */
14367 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
14369 if (dependent_template_arg_p (t
))
14371 if (ARGUMENT_PACK_P (t
))
14373 tree vec
= ARGUMENT_PACK_ARGS (t
);
14374 int len
= TREE_VEC_LENGTH (vec
);
14375 bool result
= false;
14378 for (i
= 0; i
< len
; ++i
)
14379 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
14383 else if (TYPE_P (t
))
14385 /* [basic.link]: A name with no linkage (notably, the name
14386 of a class or enumeration declared in a local scope)
14387 shall not be used to declare an entity with linkage.
14388 This implies that names with no linkage cannot be used as
14391 DR 757 relaxes this restriction for C++0x. */
14392 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
14393 : no_linkage_check (t
, /*relaxed_p=*/false));
14397 /* DR 488 makes use of a type with no linkage cause
14398 type deduction to fail. */
14399 if (complain
& tf_error
)
14401 if (TYPE_ANONYMOUS_P (nt
))
14402 error ("%qT is/uses anonymous type", t
);
14404 error ("template argument for %qD uses local type %qT",
14409 /* In order to avoid all sorts of complications, we do not
14410 allow variably-modified types as template arguments. */
14411 else if (variably_modified_type_p (t
, NULL_TREE
))
14413 if (complain
& tf_error
)
14414 error ("%qT is a variably modified type", t
);
14418 /* A non-type argument of integral or enumerated type must be a
14420 else if (TREE_TYPE (t
)
14421 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
14422 && !TREE_CONSTANT (t
))
14424 if (complain
& tf_error
)
14425 error ("integral expression %qE is not constant", t
);
14432 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
14434 int ix
, len
= DECL_NTPARMS (tmpl
);
14435 bool result
= false;
14437 for (ix
= 0; ix
!= len
; ix
++)
14439 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
14442 if (result
&& (complain
& tf_error
))
14443 error (" trying to instantiate %qD", tmpl
);
14447 /* We're out of SFINAE context now, so generate diagnostics for the access
14448 errors we saw earlier when instantiating D from TMPL and ARGS. */
14451 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
14453 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
14454 tree type
= TREE_TYPE (pattern
);
14455 location_t loc
= input_location
;
14457 push_access_scope (d
);
14458 push_deferring_access_checks (dk_no_deferred
);
14459 input_location
= DECL_SOURCE_LOCATION (pattern
);
14460 tsubst (type
, args
, tf_warning_or_error
, d
);
14461 input_location
= loc
;
14462 pop_deferring_access_checks ();
14463 pop_access_scope (d
);
14466 /* Instantiate the indicated variable, function, or alias template TMPL with
14467 the template arguments in TARG_PTR. */
14470 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
14472 tree targ_ptr
= orig_args
;
14476 bool access_ok
= true;
14478 if (tmpl
== error_mark_node
)
14479 return error_mark_node
;
14481 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
14483 /* If this function is a clone, handle it specially. */
14484 if (DECL_CLONED_FUNCTION_P (tmpl
))
14489 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
14490 DECL_CLONED_FUNCTION. */
14491 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
14492 targ_ptr
, complain
);
14493 if (spec
== error_mark_node
)
14494 return error_mark_node
;
14496 /* Look for the clone. */
14497 FOR_EACH_CLONE (clone
, spec
)
14498 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
14500 /* We should always have found the clone by now. */
14501 gcc_unreachable ();
14505 /* Check to see if we already have this specialization. */
14506 gen_tmpl
= most_general_template (tmpl
);
14507 if (tmpl
!= gen_tmpl
)
14508 /* The TMPL is a partial instantiation. To get a full set of
14509 arguments we must add the arguments used to perform the
14510 partial instantiation. */
14511 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
14514 /* It would be nice to avoid hashing here and then again in tsubst_decl,
14515 but it doesn't seem to be on the hot path. */
14516 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
14518 gcc_assert (tmpl
== gen_tmpl
14519 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
14521 || fndecl
== NULL_TREE
);
14523 if (spec
!= NULL_TREE
)
14525 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
14527 if (complain
& tf_error
)
14528 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
14529 return error_mark_node
;
14534 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
14536 return error_mark_node
;
14538 /* We are building a FUNCTION_DECL, during which the access of its
14539 parameters and return types have to be checked. However this
14540 FUNCTION_DECL which is the desired context for access checking
14541 is not built yet. We solve this chicken-and-egg problem by
14542 deferring all checks until we have the FUNCTION_DECL. */
14543 push_deferring_access_checks (dk_deferred
);
14545 /* Instantiation of the function happens in the context of the function
14546 template, not the context of the overload resolution we're doing. */
14547 push_to_top_level ();
14548 /* If there are dependent arguments, e.g. because we're doing partial
14549 ordering, make sure processing_template_decl stays set. */
14550 if (uses_template_parms (targ_ptr
))
14551 ++processing_template_decl
;
14552 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
14554 tree ctx
= tsubst (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
14555 complain
, gen_tmpl
);
14556 push_nested_class (ctx
);
14558 /* Substitute template parameters to obtain the specialization. */
14559 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
14560 targ_ptr
, complain
, gen_tmpl
);
14561 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
14562 pop_nested_class ();
14563 pop_from_top_level ();
14565 if (fndecl
== error_mark_node
)
14567 pop_deferring_access_checks ();
14568 return error_mark_node
;
14571 /* The DECL_TI_TEMPLATE should always be the immediate parent
14572 template, not the most general template. */
14573 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
14575 /* Now we know the specialization, compute access previously
14577 push_access_scope (fndecl
);
14578 if (!perform_deferred_access_checks (complain
))
14580 pop_access_scope (fndecl
);
14581 pop_deferring_access_checks ();
14583 /* If we've just instantiated the main entry point for a function,
14584 instantiate all the alternate entry points as well. We do this
14585 by cloning the instantiation of the main entry point, not by
14586 instantiating the template clones. */
14587 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
14588 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
14592 if (!(complain
& tf_error
))
14594 /* Remember to reinstantiate when we're out of SFINAE so the user
14595 can see the errors. */
14596 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
14598 return error_mark_node
;
14603 /* Wrapper for instantiate_template_1. */
14606 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
14609 timevar_push (TV_TEMPLATE_INST
);
14610 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
14611 timevar_pop (TV_TEMPLATE_INST
);
14615 /* Instantiate the alias template TMPL with ARGS. Also push a template
14616 instantiation level, which instantiate_template doesn't do because
14617 functions and variables have sufficient context established by the
14621 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
14623 struct pending_template
*old_last_pend
= last_pending_template
;
14624 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
14625 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
14626 return error_mark_node
;
14627 tree tinst
= build_tree_list (tmpl
, args
);
14628 if (!push_tinst_level (tinst
))
14631 return error_mark_node
;
14633 tree r
= instantiate_template (tmpl
, args
, complain
);
14634 pop_tinst_level ();
14635 /* We can't free this if a pending_template entry or last_error_tinst_level
14636 is pointing at it. */
14637 if (last_pending_template
== old_last_pend
14638 && last_error_tinst_level
== old_error_tinst
)
14644 /* PARM is a template parameter pack for FN. Returns true iff
14645 PARM is used in a deducible way in the argument list of FN. */
14648 pack_deducible_p (tree parm
, tree fn
)
14650 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
14651 for (; t
; t
= TREE_CHAIN (t
))
14653 tree type
= TREE_VALUE (t
);
14655 if (!PACK_EXPANSION_P (type
))
14657 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
14658 packs
; packs
= TREE_CHAIN (packs
))
14659 if (TREE_VALUE (packs
) == parm
)
14661 /* The template parameter pack is used in a function parameter
14662 pack. If this is the end of the parameter list, the
14663 template parameter pack is deducible. */
14664 if (TREE_CHAIN (t
) == void_list_node
)
14667 /* Otherwise, not. Well, it could be deduced from
14668 a non-pack parameter, but doing so would end up with
14669 a deduction mismatch, so don't bother. */
14673 /* The template parameter pack isn't used in any function parameter
14674 packs, but it might be used deeper, e.g. tuple<Args...>. */
14678 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
14679 NARGS elements of the arguments that are being used when calling
14680 it. TARGS is a vector into which the deduced template arguments
14683 Return zero for success, 2 for an incomplete match that doesn't resolve
14684 all the types, and 1 for complete failure. An error message will be
14685 printed only for an incomplete match.
14687 If FN is a conversion operator, or we are trying to produce a specific
14688 specialization, RETURN_TYPE is the return type desired.
14690 The EXPLICIT_TARGS are explicit template arguments provided via a
14693 The parameter STRICT is one of:
14696 We are deducing arguments for a function call, as in
14697 [temp.deduct.call].
14700 We are deducing arguments for a conversion function, as in
14701 [temp.deduct.conv].
14704 We are deducing arguments when doing an explicit instantiation
14705 as in [temp.explicit], when determining an explicit specialization
14706 as in [temp.expl.spec], or when taking the address of a function
14707 template, as in [temp.deduct.funcaddr]. */
14710 fn_type_unification (tree fn
,
14711 tree explicit_targs
,
14714 unsigned int nargs
,
14716 unification_kind_t strict
,
14722 tree decl
= NULL_TREE
;
14723 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
14725 static int deduction_depth
;
14726 struct pending_template
*old_last_pend
= last_pending_template
;
14727 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
14728 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
14730 tree r
= error_mark_node
;
14732 /* Adjust any explicit template arguments before entering the
14733 substitution context. */
14734 if (explicit_targs
)
14737 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
14739 /*require_all_args=*/false,
14740 /*use_default_args=*/false));
14741 if (explicit_targs
== error_mark_node
)
14742 return error_mark_node
;
14745 /* In C++0x, it's possible to have a function template whose type depends
14746 on itself recursively. This is most obvious with decltype, but can also
14747 occur with enumeration scope (c++/48969). So we need to catch infinite
14748 recursion and reject the substitution at deduction time; this function
14749 will return error_mark_node for any repeated substitution.
14751 This also catches excessive recursion such as when f<N> depends on
14752 f<N-1> across all integers, and returns error_mark_node for all the
14753 substitutions back up to the initial one.
14755 This is, of course, not reentrant. */
14756 if (excessive_deduction_depth
)
14757 return error_mark_node
;
14758 tinst
= build_tree_list (fn
, targs
);
14759 if (!push_tinst_level (tinst
))
14761 excessive_deduction_depth
= true;
14763 return error_mark_node
;
14766 push_deferring_access_checks (dk_deferred
);
14768 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
14770 fntype
= TREE_TYPE (fn
);
14771 if (explicit_targs
)
14775 The specified template arguments must match the template
14776 parameters in kind (i.e., type, nontype, template), and there
14777 must not be more arguments than there are parameters;
14778 otherwise type deduction fails.
14780 Nontype arguments must match the types of the corresponding
14781 nontype template parameters, or must be convertible to the
14782 types of the corresponding nontype parameters as specified in
14783 _temp.arg.nontype_, otherwise type deduction fails.
14785 All references in the function type of the function template
14786 to the corresponding template parameters are replaced by the
14787 specified template argument values. If a substitution in a
14788 template parameter or in the function type of the function
14789 template results in an invalid type, type deduction fails. */
14790 int i
, len
= TREE_VEC_LENGTH (tparms
);
14791 location_t loc
= input_location
;
14792 bool incomplete
= false;
14794 /* Substitute the explicit args into the function type. This is
14795 necessary so that, for instance, explicitly declared function
14796 arguments can match null pointed constants. If we were given
14797 an incomplete set of explicit args, we must not do semantic
14798 processing during substitution as we could create partial
14800 for (i
= 0; i
< len
; i
++)
14802 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
14803 bool parameter_pack
= false;
14804 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
14806 /* Dig out the actual parm. */
14807 if (TREE_CODE (parm
) == TYPE_DECL
14808 || TREE_CODE (parm
) == TEMPLATE_DECL
)
14810 parm
= TREE_TYPE (parm
);
14811 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
14813 else if (TREE_CODE (parm
) == PARM_DECL
)
14815 parm
= DECL_INITIAL (parm
);
14816 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
14819 if (!parameter_pack
&& targ
== NULL_TREE
)
14820 /* No explicit argument for this template parameter. */
14823 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
14825 /* Mark the argument pack as "incomplete". We could
14826 still deduce more arguments during unification.
14827 We remove this mark in type_unification_real. */
14830 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
14831 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
14832 = ARGUMENT_PACK_ARGS (targ
);
14835 /* We have some incomplete argument packs. */
14840 processing_template_decl
+= incomplete
;
14841 input_location
= DECL_SOURCE_LOCATION (fn
);
14842 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
14843 complain
| tf_partial
, NULL_TREE
);
14844 input_location
= loc
;
14845 processing_template_decl
-= incomplete
;
14847 if (fntype
== error_mark_node
)
14850 /* Place the explicitly specified arguments in TARGS. */
14851 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
14852 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
14855 /* Never do unification on the 'this' parameter. */
14856 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
14862 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
14863 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
14864 new_args
[0] = return_type
;
14865 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
14870 /* We allow incomplete unification without an error message here
14871 because the standard doesn't seem to explicitly prohibit it. Our
14872 callers must be ready to deal with unification failures in any
14875 pop_tinst_level ();
14876 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
14877 targs
, parms
, args
, nargs
, /*subr=*/0,
14878 strict
, flags
, explain_p
);
14879 push_tinst_level (tinst
);
14883 /* Now that we have bindings for all of the template arguments,
14884 ensure that the arguments deduced for the template template
14885 parameters have compatible template parameter lists. We cannot
14886 check this property before we have deduced all template
14887 arguments, because the template parameter types of a template
14888 template parameter might depend on prior template parameters
14889 deduced after the template template parameter. The following
14890 ill-formed example illustrates this issue:
14892 template<typename T, template<T> class C> void f(C<5>, T);
14894 template<int N> struct X {};
14897 f(X<5>(), 5l); // error: template argument deduction fails
14900 The template parameter list of 'C' depends on the template type
14901 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
14902 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
14903 time that we deduce 'C'. */
14904 if (!template_template_parm_bindings_ok_p
14905 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
14907 unify_inconsistent_template_template_parameters (explain_p
);
14911 /* All is well so far. Now, check:
14915 When all template arguments have been deduced, all uses of
14916 template parameters in nondeduced contexts are replaced with
14917 the corresponding deduced argument values. If the
14918 substitution results in an invalid type, as described above,
14919 type deduction fails. */
14920 decl
= instantiate_template (fn
, targs
, complain
);
14921 if (decl
== error_mark_node
)
14924 /* Now perform any access checks encountered during deduction, such as
14925 for default template arguments. */
14926 push_access_scope (decl
);
14927 ok
= perform_deferred_access_checks (complain
);
14928 pop_access_scope (decl
);
14932 /* If we're looking for an exact match, check that what we got
14933 is indeed an exact match. It might not be if some template
14934 parameters are used in non-deduced contexts. */
14935 if (strict
== DEDUCE_EXACT
)
14937 tree substed
= TREE_TYPE (decl
);
14941 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
14943 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
14944 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
14945 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
14947 unify_type_mismatch (explain_p
, args
[i
],
14948 TREE_VALUE (sarg
));
14956 pop_deferring_access_checks ();
14958 if (excessive_deduction_depth
)
14960 if (deduction_depth
== 0)
14961 /* Reset once we're all the way out. */
14962 excessive_deduction_depth
= false;
14965 pop_tinst_level ();
14966 /* We can't free this if a pending_template entry or last_error_tinst_level
14967 is pointing at it. */
14968 if (last_pending_template
== old_last_pend
14969 && last_error_tinst_level
== old_error_tinst
)
14975 /* Adjust types before performing type deduction, as described in
14976 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
14977 sections are symmetric. PARM is the type of a function parameter
14978 or the return type of the conversion function. ARG is the type of
14979 the argument passed to the call, or the type of the value
14980 initialized with the result of the conversion function.
14981 ARG_EXPR is the original argument expression, which may be null. */
14984 maybe_adjust_types_for_deduction (unification_kind_t strict
,
14998 /* Swap PARM and ARG throughout the remainder of this
14999 function; the handling is precisely symmetric since PARM
15000 will initialize ARG rather than vice versa. */
15008 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15009 too, but here handle it by stripping the reference from PARM
15010 rather than by adding it to ARG. */
15011 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15012 && TYPE_REF_IS_RVALUE (*parm
)
15013 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15014 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15015 && TREE_CODE (*arg
) == REFERENCE_TYPE
15016 && !TYPE_REF_IS_RVALUE (*arg
))
15017 *parm
= TREE_TYPE (*parm
);
15018 /* Nothing else to do in this case. */
15022 gcc_unreachable ();
15025 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
15027 /* [temp.deduct.call]
15029 If P is not a reference type:
15031 --If A is an array type, the pointer type produced by the
15032 array-to-pointer standard conversion (_conv.array_) is
15033 used in place of A for type deduction; otherwise,
15035 --If A is a function type, the pointer type produced by
15036 the function-to-pointer standard conversion
15037 (_conv.func_) is used in place of A for type deduction;
15040 --If A is a cv-qualified type, the top level
15041 cv-qualifiers of A's type are ignored for type
15043 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
15044 *arg
= build_pointer_type (TREE_TYPE (*arg
));
15045 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
15046 *arg
= build_pointer_type (*arg
);
15048 *arg
= TYPE_MAIN_VARIANT (*arg
);
15051 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15052 of the form T&&, where T is a template parameter, and the argument
15053 is an lvalue, T is deduced as A& */
15054 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15055 && TYPE_REF_IS_RVALUE (*parm
)
15056 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15057 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15058 && (arg_expr
? real_lvalue_p (arg_expr
)
15059 /* try_one_overload doesn't provide an arg_expr, but
15060 functions are always lvalues. */
15061 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
15062 *arg
= build_reference_type (*arg
);
15064 /* [temp.deduct.call]
15066 If P is a cv-qualified type, the top level cv-qualifiers
15067 of P's type are ignored for type deduction. If P is a
15068 reference type, the type referred to by P is used for
15070 *parm
= TYPE_MAIN_VARIANT (*parm
);
15071 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
15073 *parm
= TREE_TYPE (*parm
);
15074 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
15077 /* DR 322. For conversion deduction, remove a reference type on parm
15078 too (which has been swapped into ARG). */
15079 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
15080 *arg
= TREE_TYPE (*arg
);
15085 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15086 template which does contain any deducible template parameters; check if
15087 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15088 unify_one_argument. */
15091 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
15092 int flags
, bool explain_p
)
15097 type
= TREE_TYPE (arg
);
15101 if (same_type_p (parm
, type
))
15102 return unify_success (explain_p
);
15104 if (strict
== DEDUCE_CONV
)
15106 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
15107 explain_p
? tf_warning_or_error
: tf_none
))
15108 return unify_success (explain_p
);
15110 else if (strict
!= DEDUCE_EXACT
)
15112 if (can_convert_arg (parm
, type
,
15113 TYPE_P (arg
) ? NULL_TREE
: arg
,
15114 flags
, explain_p
? tf_warning_or_error
: tf_none
))
15115 return unify_success (explain_p
);
15118 if (strict
== DEDUCE_EXACT
)
15119 return unify_type_mismatch (explain_p
, parm
, arg
);
15121 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
15124 /* Subroutine of type_unification_real and unify_pack_expansion to
15125 handle unification of a single P/A pair. Parameters are as
15126 for those functions. */
15129 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
15130 int subr
, unification_kind_t strict
, int flags
,
15133 tree arg_expr
= NULL_TREE
;
15136 if (arg
== error_mark_node
|| parm
== error_mark_node
)
15137 return unify_invalid (explain_p
);
15138 if (arg
== unknown_type_node
)
15139 /* We can't deduce anything from this, but we might get all the
15140 template args from other function args. */
15141 return unify_success (explain_p
);
15143 /* FIXME uses_deducible_template_parms */
15144 if (TYPE_P (parm
) && !uses_template_parms (parm
))
15145 return check_non_deducible_conversion (parm
, arg
, strict
, flags
,
15151 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
15152 | UNIFY_ALLOW_MORE_CV_QUAL
15153 | UNIFY_ALLOW_DERIVED
);
15157 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
15161 arg_strict
= UNIFY_ALLOW_NONE
;
15165 gcc_unreachable ();
15168 /* We only do these transformations if this is the top-level
15169 parameter_type_list in a call or declaration matching; in other
15170 situations (nested function declarators, template argument lists) we
15171 won't be comparing a type to an expression, and we don't do any type
15177 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
15178 if (type_unknown_p (arg
))
15180 /* [temp.deduct.type] A template-argument can be
15181 deduced from a pointer to function or pointer
15182 to member function argument if the set of
15183 overloaded functions does not contain function
15184 templates and at most one of a set of
15185 overloaded functions provides a unique
15188 if (resolve_overloaded_unification
15189 (tparms
, targs
, parm
, arg
, strict
,
15190 arg_strict
, explain_p
))
15191 return unify_success (explain_p
);
15192 return unify_overload_resolution_failure (explain_p
, arg
);
15196 arg
= unlowered_expr_type (arg
);
15197 if (arg
== error_mark_node
)
15198 return unify_invalid (explain_p
);
15202 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
15205 gcc_assert ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
15206 == (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
));
15208 /* For deduction from an init-list we need the actual list. */
15209 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
15211 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
15214 /* Most parms like fn_type_unification.
15216 If SUBR is 1, we're being called recursively (to unify the
15217 arguments of a function or method parameter of a function
15221 type_unification_real (tree tparms
,
15225 unsigned int xnargs
,
15227 unification_kind_t strict
,
15233 int ntparms
= TREE_VEC_LENGTH (tparms
);
15234 int saw_undeduced
= 0;
15237 unsigned int nargs
;
15240 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
15241 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
15242 gcc_assert (ntparms
> 0);
15244 /* Reset the number of non-defaulted template arguments contained
15246 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
15254 while (parms
&& parms
!= void_list_node
15257 parm
= TREE_VALUE (parms
);
15259 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
15260 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
15261 /* For a function parameter pack that occurs at the end of the
15262 parameter-declaration-list, the type A of each remaining
15263 argument of the call is compared with the type P of the
15264 declarator-id of the function parameter pack. */
15267 parms
= TREE_CHAIN (parms
);
15269 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
15270 /* For a function parameter pack that does not occur at the
15271 end of the parameter-declaration-list, the type of the
15272 parameter pack is a non-deduced context. */
15278 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
15284 && parms
!= void_list_node
15285 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
15287 /* Unify the remaining arguments with the pack expansion type. */
15289 tree parmvec
= make_tree_vec (1);
15291 /* Allocate a TREE_VEC and copy in all of the arguments */
15292 argvec
= make_tree_vec (nargs
- ia
);
15293 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
15294 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
15296 /* Copy the parameter into parmvec. */
15297 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
15298 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
15299 /*subr=*/subr
, explain_p
))
15302 /* Advance to the end of the list of parameters. */
15303 parms
= TREE_CHAIN (parms
);
15306 /* Fail if we've reached the end of the parm list, and more args
15307 are present, and the parm list isn't variadic. */
15308 if (ia
< nargs
&& parms
== void_list_node
)
15309 return unify_too_many_arguments (explain_p
, nargs
, ia
);
15310 /* Fail if parms are left and they don't have default values. */
15311 if (parms
&& parms
!= void_list_node
15312 && TREE_PURPOSE (parms
) == NULL_TREE
)
15314 unsigned int count
= nargs
;
15316 while (p
&& p
!= void_list_node
)
15319 p
= TREE_CHAIN (p
);
15321 return unify_too_few_arguments (explain_p
, ia
, count
);
15326 tsubst_flags_t complain
= (explain_p
15327 ? tf_warning_or_error
15330 /* Check to see if we need another pass before we start clearing
15331 ARGUMENT_PACK_INCOMPLETE_P. */
15332 for (i
= 0; i
< ntparms
; i
++)
15334 tree targ
= TREE_VEC_ELT (targs
, i
);
15335 tree tparm
= TREE_VEC_ELT (tparms
, i
);
15337 if (targ
|| tparm
== error_mark_node
)
15339 tparm
= TREE_VALUE (tparm
);
15341 /* If this is an undeduced nontype parameter that depends on
15342 a type parameter, try another pass; its type may have been
15343 deduced from a later argument than the one from which
15344 this parameter can be deduced. */
15345 if (TREE_CODE (tparm
) == PARM_DECL
15346 && uses_template_parms (TREE_TYPE (tparm
))
15347 && !saw_undeduced
++)
15351 for (i
= 0; i
< ntparms
; i
++)
15353 tree targ
= TREE_VEC_ELT (targs
, i
);
15354 tree tparm
= TREE_VEC_ELT (tparms
, i
);
15356 /* Clear the "incomplete" flags on all argument packs now so that
15357 substituting them into later default arguments works. */
15358 if (targ
&& ARGUMENT_PACK_P (targ
))
15360 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
15361 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
15364 if (targ
|| tparm
== error_mark_node
)
15366 tparm
= TREE_VALUE (tparm
);
15368 /* Core issue #226 (C++0x) [temp.deduct]:
15370 If a template argument has not been deduced, its
15371 default template argument, if any, is used.
15373 When we are in C++98 mode, TREE_PURPOSE will either
15374 be NULL_TREE or ERROR_MARK_NODE, so we do not need
15375 to explicitly check cxx_dialect here. */
15376 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
15378 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
15379 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
15380 location_t save_loc
= input_location
;
15382 input_location
= DECL_SOURCE_LOCATION (parm
);
15383 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
15384 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
15386 input_location
= save_loc
;
15387 if (arg
== error_mark_node
)
15391 TREE_VEC_ELT (targs
, i
) = arg
;
15392 /* The position of the first default template argument,
15393 is also the number of non-defaulted arguments in TARGS.
15395 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
15396 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
15401 /* If the type parameter is a parameter pack, then it will
15402 be deduced to an empty parameter pack. */
15403 if (template_parameter_pack_p (tparm
))
15407 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
15409 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
15410 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
15411 TREE_CONSTANT (arg
) = 1;
15414 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
15416 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
15418 TREE_VEC_ELT (targs
, i
) = arg
;
15422 return unify_parameter_deduction_failure (explain_p
, tparm
);
15425 #ifdef ENABLE_CHECKING
15426 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
15427 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
15430 return unify_success (explain_p
);
15433 /* Subroutine of type_unification_real. Args are like the variables
15434 at the call site. ARG is an overloaded function (or template-id);
15435 we try deducing template args from each of the overloads, and if
15436 only one succeeds, we go with that. Modifies TARGS and returns
15437 true on success. */
15440 resolve_overloaded_unification (tree tparms
,
15444 unification_kind_t strict
,
15448 tree tempargs
= copy_node (targs
);
15450 tree goodfn
= NULL_TREE
;
15453 if (TREE_CODE (arg
) == ADDR_EXPR
)
15455 arg
= TREE_OPERAND (arg
, 0);
15461 if (TREE_CODE (arg
) == COMPONENT_REF
)
15462 /* Handle `&x' where `x' is some static or non-static member
15464 arg
= TREE_OPERAND (arg
, 1);
15466 if (TREE_CODE (arg
) == OFFSET_REF
)
15467 arg
= TREE_OPERAND (arg
, 1);
15469 /* Strip baselink information. */
15470 if (BASELINK_P (arg
))
15471 arg
= BASELINK_FUNCTIONS (arg
);
15473 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
15475 /* If we got some explicit template args, we need to plug them into
15476 the affected templates before we try to unify, in case the
15477 explicit args will completely resolve the templates in question. */
15480 tree expl_subargs
= TREE_OPERAND (arg
, 1);
15481 arg
= TREE_OPERAND (arg
, 0);
15483 for (; arg
; arg
= OVL_NEXT (arg
))
15485 tree fn
= OVL_CURRENT (arg
);
15486 tree subargs
, elem
;
15488 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
15491 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15492 expl_subargs
, NULL_TREE
, tf_none
,
15493 /*require_all_args=*/true,
15494 /*use_default_args=*/true);
15495 if (subargs
!= error_mark_node
15496 && !any_dependent_template_arguments_p (subargs
))
15498 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
15499 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
15500 elem
, strict
, sub_strict
, addr_p
, explain_p
)
15501 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
15510 /* If no templates (or more than one) are fully resolved by the
15511 explicit arguments, this template-id is a non-deduced context; it
15512 could still be OK if we deduce all template arguments for the
15513 enclosing call through other arguments. */
15517 else if (TREE_CODE (arg
) != OVERLOAD
15518 && TREE_CODE (arg
) != FUNCTION_DECL
)
15519 /* If ARG is, for example, "(0, &f)" then its type will be unknown
15520 -- but the deduction does not succeed because the expression is
15521 not just the function on its own. */
15524 for (; arg
; arg
= OVL_NEXT (arg
))
15525 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
15526 TREE_TYPE (OVL_CURRENT (arg
)),
15527 strict
, sub_strict
, addr_p
, explain_p
)
15528 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
15530 goodfn
= OVL_CURRENT (arg
);
15534 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15535 to function or pointer to member function argument if the set of
15536 overloaded functions does not contain function templates and at most
15537 one of a set of overloaded functions provides a unique match.
15539 So if we found multiple possibilities, we return success but don't
15540 deduce anything. */
15544 int i
= TREE_VEC_LENGTH (targs
);
15546 if (TREE_VEC_ELT (tempargs
, i
))
15547 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
15555 /* Core DR 115: In contexts where deduction is done and fails, or in
15556 contexts where deduction is not done, if a template argument list is
15557 specified and it, along with any default template arguments, identifies
15558 a single function template specialization, then the template-id is an
15559 lvalue for the function template specialization. */
15562 resolve_nondeduced_context (tree orig_expr
)
15564 tree expr
, offset
, baselink
;
15567 if (!type_unknown_p (orig_expr
))
15572 offset
= NULL_TREE
;
15573 baselink
= NULL_TREE
;
15575 if (TREE_CODE (expr
) == ADDR_EXPR
)
15577 expr
= TREE_OPERAND (expr
, 0);
15580 if (TREE_CODE (expr
) == OFFSET_REF
)
15583 expr
= TREE_OPERAND (expr
, 1);
15585 if (BASELINK_P (expr
))
15588 expr
= BASELINK_FUNCTIONS (expr
);
15591 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
15594 tree goodfn
= NULL_TREE
;
15596 /* If we got some explicit template args, we need to plug them into
15597 the affected templates before we try to unify, in case the
15598 explicit args will completely resolve the templates in question. */
15600 tree expl_subargs
= TREE_OPERAND (expr
, 1);
15601 tree arg
= TREE_OPERAND (expr
, 0);
15602 tree badfn
= NULL_TREE
;
15603 tree badargs
= NULL_TREE
;
15605 for (; arg
; arg
= OVL_NEXT (arg
))
15607 tree fn
= OVL_CURRENT (arg
);
15608 tree subargs
, elem
;
15610 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
15613 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15614 expl_subargs
, NULL_TREE
, tf_none
,
15615 /*require_all_args=*/true,
15616 /*use_default_args=*/true);
15617 if (subargs
!= error_mark_node
15618 && !any_dependent_template_arguments_p (subargs
))
15620 elem
= instantiate_template (fn
, subargs
, tf_none
);
15621 if (elem
== error_mark_node
)
15626 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
15635 mark_used (goodfn
);
15638 expr
= build_baselink (BASELINK_BINFO (baselink
),
15639 BASELINK_ACCESS_BINFO (baselink
),
15640 expr
, BASELINK_OPTYPE (baselink
));
15644 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
15645 expr
= build_offset_ref (base
, expr
, addr
);
15648 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
15651 else if (good
== 0 && badargs
)
15652 /* There were no good options and at least one bad one, so let the
15653 user know what the problem is. */
15654 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
15659 /* Subroutine of resolve_overloaded_unification; does deduction for a single
15660 overload. Fills TARGS with any deduced arguments, or error_mark_node if
15661 different overloads deduce different arguments for a given parm.
15662 ADDR_P is true if the expression for which deduction is being
15663 performed was of the form "& fn" rather than simply "fn".
15665 Returns 1 on success. */
15668 try_one_overload (tree tparms
,
15673 unification_kind_t strict
,
15682 if (arg
== error_mark_node
)
15685 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15686 to function or pointer to member function argument if the set of
15687 overloaded functions does not contain function templates and at most
15688 one of a set of overloaded functions provides a unique match.
15690 So if this is a template, just return success. */
15692 if (uses_template_parms (arg
))
15695 if (TREE_CODE (arg
) == METHOD_TYPE
)
15696 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
15698 arg
= build_pointer_type (arg
);
15700 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
15702 /* We don't copy orig_targs for this because if we have already deduced
15703 some template args from previous args, unify would complain when we
15704 try to deduce a template parameter for the same argument, even though
15705 there isn't really a conflict. */
15706 nargs
= TREE_VEC_LENGTH (targs
);
15707 tempargs
= make_tree_vec (nargs
);
15709 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
15712 /* First make sure we didn't deduce anything that conflicts with
15713 explicitly specified args. */
15714 for (i
= nargs
; i
--; )
15716 tree elt
= TREE_VEC_ELT (tempargs
, i
);
15717 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
15721 else if (uses_template_parms (elt
))
15722 /* Since we're unifying against ourselves, we will fill in
15723 template args used in the function parm list with our own
15724 template parms. Discard them. */
15725 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
15726 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
15730 for (i
= nargs
; i
--; )
15732 tree elt
= TREE_VEC_ELT (tempargs
, i
);
15735 TREE_VEC_ELT (targs
, i
) = elt
;
15741 /* PARM is a template class (perhaps with unbound template
15742 parameters). ARG is a fully instantiated type. If ARG can be
15743 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
15744 TARGS are as for unify. */
15747 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
15750 tree copy_of_targs
;
15752 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
15753 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
15754 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
15757 /* We need to make a new template argument vector for the call to
15758 unify. If we used TARGS, we'd clutter it up with the result of
15759 the attempted unification, even if this class didn't work out.
15760 We also don't want to commit ourselves to all the unifications
15761 we've already done, since unification is supposed to be done on
15762 an argument-by-argument basis. In other words, consider the
15763 following pathological case:
15765 template <int I, int J, int K>
15768 template <int I, int J>
15769 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
15771 template <int I, int J, int K>
15772 void f(S<I, J, K>, S<I, I, I>);
15781 Now, by the time we consider the unification involving `s2', we
15782 already know that we must have `f<0, 0, 0>'. But, even though
15783 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
15784 because there are two ways to unify base classes of S<0, 1, 2>
15785 with S<I, I, I>. If we kept the already deduced knowledge, we
15786 would reject the possibility I=1. */
15787 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
15789 /* If unification failed, we're done. */
15790 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
15791 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
15797 /* Given a template type PARM and a class type ARG, find the unique
15798 base type in ARG that is an instance of PARM. We do not examine
15799 ARG itself; only its base-classes. If there is not exactly one
15800 appropriate base class, return NULL_TREE. PARM may be the type of
15801 a partial specialization, as well as a plain template type. Used
15804 static enum template_base_result
15805 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
15806 bool explain_p
, tree
*result
)
15808 tree rval
= NULL_TREE
;
15811 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
15813 binfo
= TYPE_BINFO (complete_type (arg
));
15816 /* The type could not be completed. */
15817 *result
= NULL_TREE
;
15818 return tbr_incomplete_type
;
15821 /* Walk in inheritance graph order. The search order is not
15822 important, and this avoids multiple walks of virtual bases. */
15823 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
15825 tree r
= try_class_unification (tparms
, targs
, parm
,
15826 BINFO_TYPE (binfo
), explain_p
);
15830 /* If there is more than one satisfactory baseclass, then:
15834 If they yield more than one possible deduced A, the type
15838 if (rval
&& !same_type_p (r
, rval
))
15840 *result
= NULL_TREE
;
15841 return tbr_ambiguous_baseclass
;
15849 return tbr_success
;
15852 /* Returns the level of DECL, which declares a template parameter. */
15855 template_decl_level (tree decl
)
15857 switch (TREE_CODE (decl
))
15860 case TEMPLATE_DECL
:
15861 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
15864 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
15867 gcc_unreachable ();
15872 /* Decide whether ARG can be unified with PARM, considering only the
15873 cv-qualifiers of each type, given STRICT as documented for unify.
15874 Returns nonzero iff the unification is OK on that basis. */
15877 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
15879 int arg_quals
= cp_type_quals (arg
);
15880 int parm_quals
= cp_type_quals (parm
);
15882 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
15883 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
15885 /* Although a CVR qualifier is ignored when being applied to a
15886 substituted template parameter ([8.3.2]/1 for example), that
15887 does not allow us to unify "const T" with "int&" because both
15888 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
15889 It is ok when we're allowing additional CV qualifiers
15890 at the outer level [14.8.2.1]/3,1st bullet. */
15891 if ((TREE_CODE (arg
) == REFERENCE_TYPE
15892 || TREE_CODE (arg
) == FUNCTION_TYPE
15893 || TREE_CODE (arg
) == METHOD_TYPE
)
15894 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
15897 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
15898 && (parm_quals
& TYPE_QUAL_RESTRICT
))
15902 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
15903 && (arg_quals
& parm_quals
) != parm_quals
)
15906 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
15907 && (parm_quals
& arg_quals
) != arg_quals
)
15913 /* Determines the LEVEL and INDEX for the template parameter PARM. */
15915 template_parm_level_and_index (tree parm
, int* level
, int* index
)
15917 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
15918 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
15919 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
15921 *index
= TEMPLATE_TYPE_IDX (parm
);
15922 *level
= TEMPLATE_TYPE_LEVEL (parm
);
15926 *index
= TEMPLATE_PARM_IDX (parm
);
15927 *level
= TEMPLATE_PARM_LEVEL (parm
);
15931 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
15933 if (unify (TP, TA, P, A, S, EP)) \
15937 /* Unifies the remaining arguments in PACKED_ARGS with the pack
15938 expansion at the end of PACKED_PARMS. Returns 0 if the type
15939 deduction succeeds, 1 otherwise. STRICT is the same as in
15940 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
15941 call argument list. We'll need to adjust the arguments to make them
15942 types. SUBR tells us if this is from a recursive call to
15943 type_unification_real, or for comparing two template argument
15947 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
15948 tree packed_args
, unification_kind_t strict
,
15949 bool subr
, bool explain_p
)
15952 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
15953 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
15954 tree pack
, packs
= NULL_TREE
;
15955 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
15956 int len
= TREE_VEC_LENGTH (packed_args
);
15958 /* Determine the parameter packs we will be deducing from the
15959 pattern, and record their current deductions. */
15960 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
15961 pack
; pack
= TREE_CHAIN (pack
))
15963 tree parm_pack
= TREE_VALUE (pack
);
15966 /* Determine the index and level of this parameter pack. */
15967 template_parm_level_and_index (parm_pack
, &level
, &idx
);
15969 /* Keep track of the parameter packs and their corresponding
15971 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
15972 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
15975 /* Loop through all of the arguments that have not yet been
15976 unified and unify each with the pattern. */
15977 for (i
= start
; i
< len
; i
++)
15980 bool any_explicit
= false;
15981 tree arg
= TREE_VEC_ELT (packed_args
, i
);
15983 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
15984 or the element of its argument pack at the current index if
15985 this argument was explicitly specified. */
15986 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
15990 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
15993 if (TREE_VALUE (pack
)
15994 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
15995 && (i
< TREE_VEC_LENGTH (pargs
)))
15997 any_explicit
= true;
15998 arg
= TREE_VEC_ELT (pargs
, i
);
16000 TMPL_ARG (targs
, level
, idx
) = arg
;
16003 /* If we had explicit template arguments, substitute them into the
16004 pattern before deduction. */
16007 /* Some arguments might still be unspecified or dependent. */
16009 ++processing_template_decl
;
16010 dependent
= any_dependent_template_arguments_p (targs
);
16012 --processing_template_decl
;
16013 parm
= tsubst (pattern
, targs
,
16014 explain_p
? tf_warning_or_error
: tf_none
,
16017 --processing_template_decl
;
16018 if (parm
== error_mark_node
)
16024 /* Unify the pattern with the current argument. */
16025 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
16026 LOOKUP_IMPLICIT
, explain_p
))
16029 /* For each parameter pack, collect the deduced value. */
16030 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16033 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16035 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
16036 TMPL_ARG (targs
, level
, idx
);
16040 /* Verify that the results of unification with the parameter packs
16041 produce results consistent with what we've seen before, and make
16042 the deduced argument packs available. */
16043 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16045 tree old_pack
= TREE_VALUE (pack
);
16046 tree new_args
= TREE_TYPE (pack
);
16047 int i
, len
= TREE_VEC_LENGTH (new_args
);
16049 bool nondeduced_p
= false;
16051 /* By default keep the original deduced argument pack.
16052 If necessary, more specific code is going to update the
16053 resulting deduced argument later down in this function. */
16054 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16055 TMPL_ARG (targs
, level
, idx
) = old_pack
;
16057 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16058 actually deduce anything. */
16059 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
16060 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
16061 nondeduced_p
= true;
16065 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
16067 /* If we had fewer function args than explicit template args,
16068 just use the explicits. */
16069 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16070 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
16071 if (len
< explicit_len
)
16072 new_args
= explicit_args
;
16078 /* Build the deduced *_ARGUMENT_PACK. */
16079 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
16081 result
= make_node (NONTYPE_ARGUMENT_PACK
);
16082 TREE_TYPE (result
) =
16083 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
16084 TREE_CONSTANT (result
) = 1;
16087 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
16089 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
16091 /* Note the deduced argument packs for this parameter
16093 TMPL_ARG (targs
, level
, idx
) = result
;
16095 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
16096 && (ARGUMENT_PACK_ARGS (old_pack
)
16097 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
16099 /* We only had the explicitly-provided arguments before, but
16100 now we have a complete set of arguments. */
16101 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16103 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
16104 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
16105 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
16109 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
16110 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
16112 if (!comp_template_args_with_info (old_args
, new_args
,
16113 &bad_old_arg
, &bad_new_arg
))
16114 /* Inconsistent unification of this parameter pack. */
16115 return unify_parameter_pack_inconsistent (explain_p
,
16121 return unify_success (explain_p
);
16124 /* Deduce the value of template parameters. TPARMS is the (innermost)
16125 set of template parameters to a template. TARGS is the bindings
16126 for those template parameters, as determined thus far; TARGS may
16127 include template arguments for outer levels of template parameters
16128 as well. PARM is a parameter to a template function, or a
16129 subcomponent of that parameter; ARG is the corresponding argument.
16130 This function attempts to match PARM with ARG in a manner
16131 consistent with the existing assignments in TARGS. If more values
16132 are deduced, then TARGS is updated.
16134 Returns 0 if the type deduction succeeds, 1 otherwise. The
16135 parameter STRICT is a bitwise or of the following flags:
16138 Require an exact match between PARM and ARG.
16139 UNIFY_ALLOW_MORE_CV_QUAL:
16140 Allow the deduced ARG to be more cv-qualified (by qualification
16141 conversion) than ARG.
16142 UNIFY_ALLOW_LESS_CV_QUAL:
16143 Allow the deduced ARG to be less cv-qualified than ARG.
16144 UNIFY_ALLOW_DERIVED:
16145 Allow the deduced ARG to be a template base class of ARG,
16146 or a pointer to a template base class of the type pointed to by
16148 UNIFY_ALLOW_INTEGER:
16149 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
16150 case for more information.
16151 UNIFY_ALLOW_OUTER_LEVEL:
16152 This is the outermost level of a deduction. Used to determine validity
16153 of qualification conversions. A valid qualification conversion must
16154 have const qualified pointers leading up to the inner type which
16155 requires additional CV quals, except at the outer level, where const
16156 is not required [conv.qual]. It would be normal to set this flag in
16157 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
16158 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
16159 This is the outermost level of a deduction, and PARM can be more CV
16160 qualified at this point.
16161 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
16162 This is the outermost level of a deduction, and PARM can be less CV
16163 qualified at this point. */
16166 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
16172 int strict_in
= strict
;
16174 /* I don't think this will do the right thing with respect to types.
16175 But the only case I've seen it in so far has been array bounds, where
16176 signedness is the only information lost, and I think that will be
16178 while (TREE_CODE (parm
) == NOP_EXPR
)
16179 parm
= TREE_OPERAND (parm
, 0);
16181 if (arg
== error_mark_node
)
16182 return unify_invalid (explain_p
);
16183 if (arg
== unknown_type_node
16184 || arg
== init_list_type_node
)
16185 /* We can't deduce anything from this, but we might get all the
16186 template args from other function args. */
16187 return unify_success (explain_p
);
16189 /* If PARM uses template parameters, then we can't bail out here,
16190 even if ARG == PARM, since we won't record unifications for the
16191 template parameters. We might need them if we're trying to
16192 figure out which of two things is more specialized. */
16193 if (arg
== parm
&& !uses_template_parms (parm
))
16194 return unify_success (explain_p
);
16196 /* Handle init lists early, so the rest of the function can assume
16197 we're dealing with a type. */
16198 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
16202 tree orig_parm
= parm
;
16204 /* Replace T with std::initializer_list<T> for deduction. */
16205 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16206 && flag_deduce_init_list
)
16207 parm
= listify (parm
);
16209 if (!is_std_init_list (parm
))
16210 /* We can only deduce from an initializer list argument if the
16211 parameter is std::initializer_list; otherwise this is a
16212 non-deduced context. */
16213 return unify_success (explain_p
);
16215 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
16217 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
16219 int elt_strict
= strict
;
16221 if (elt
== error_mark_node
)
16222 return unify_invalid (explain_p
);
16224 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
16226 tree type
= TREE_TYPE (elt
);
16227 /* It should only be possible to get here for a call. */
16228 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
16229 elt_strict
|= maybe_adjust_types_for_deduction
16230 (DEDUCE_CALL
, &elttype
, &type
, elt
);
16234 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
16238 /* If the std::initializer_list<T> deduction worked, replace the
16239 deduced A with std::initializer_list<A>. */
16240 if (orig_parm
!= parm
)
16242 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
16243 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16244 targ
= listify (targ
);
16245 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
16247 return unify_success (explain_p
);
16250 /* Immediately reject some pairs that won't unify because of
16251 cv-qualification mismatches. */
16252 if (TREE_CODE (arg
) == TREE_CODE (parm
)
16254 /* It is the elements of the array which hold the cv quals of an array
16255 type, and the elements might be template type parms. We'll check
16256 when we recurse. */
16257 && TREE_CODE (arg
) != ARRAY_TYPE
16258 /* We check the cv-qualifiers when unifying with template type
16259 parameters below. We want to allow ARG `const T' to unify with
16260 PARM `T' for example, when computing which of two templates
16261 is more specialized, for example. */
16262 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
16263 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
16264 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16266 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
16267 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
16268 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
16269 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
16270 strict
&= ~UNIFY_ALLOW_DERIVED
;
16271 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
16272 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
16274 switch (TREE_CODE (parm
))
16276 case TYPENAME_TYPE
:
16278 case UNBOUND_CLASS_TEMPLATE
:
16279 /* In a type which contains a nested-name-specifier, template
16280 argument values cannot be deduced for template parameters used
16281 within the nested-name-specifier. */
16282 return unify_success (explain_p
);
16284 case TEMPLATE_TYPE_PARM
:
16285 case TEMPLATE_TEMPLATE_PARM
:
16286 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16287 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
16288 if (tparm
== error_mark_node
)
16289 return unify_invalid (explain_p
);
16291 if (TEMPLATE_TYPE_LEVEL (parm
)
16292 != template_decl_level (tparm
))
16293 /* The PARM is not one we're trying to unify. Just check
16294 to see if it matches ARG. */
16296 if (TREE_CODE (arg
) == TREE_CODE (parm
)
16297 && (is_auto (parm
) ? is_auto (arg
)
16298 : same_type_p (parm
, arg
)))
16299 return unify_success (explain_p
);
16301 return unify_type_mismatch (explain_p
, parm
, arg
);
16303 idx
= TEMPLATE_TYPE_IDX (parm
);
16304 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16305 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
16306 if (tparm
== error_mark_node
)
16307 return unify_invalid (explain_p
);
16309 /* Check for mixed types and values. */
16310 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16311 && TREE_CODE (tparm
) != TYPE_DECL
)
16312 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16313 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
16314 gcc_unreachable ();
16316 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16318 /* ARG must be constructed from a template class or a template
16319 template parameter. */
16320 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
16321 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
16322 return unify_template_deduction_failure (explain_p
, parm
, arg
);
16325 tree parmvec
= TYPE_TI_ARGS (parm
);
16326 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
16327 tree full_argvec
= add_to_template_args (targs
, argvec
);
16329 = DECL_INNERMOST_TEMPLATE_PARMS
16330 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
16332 int parm_variadic_p
= 0;
16334 /* The resolution to DR150 makes clear that default
16335 arguments for an N-argument may not be used to bind T
16336 to a template template parameter with fewer than N
16337 parameters. It is not safe to permit the binding of
16338 default arguments as an extension, as that may change
16339 the meaning of a conforming program. Consider:
16341 struct Dense { static const unsigned int dim = 1; };
16343 template <template <typename> class View,
16345 void operator+(float, View<Block> const&);
16347 template <typename Block,
16348 unsigned int Dim = Block::dim>
16349 struct Lvalue_proxy { operator float() const; };
16353 Lvalue_proxy<Dense> p;
16358 Here, if Lvalue_proxy is permitted to bind to View, then
16359 the global operator+ will be used; if they are not, the
16360 Lvalue_proxy will be converted to float. */
16361 if (coerce_template_parms (parm_parms
,
16363 TYPE_TI_TEMPLATE (parm
),
16365 ? tf_warning_or_error
16367 /*require_all_args=*/true,
16368 /*use_default_args=*/false)
16369 == error_mark_node
)
16372 /* Deduce arguments T, i from TT<T> or TT<i>.
16373 We check each element of PARMVEC and ARGVEC individually
16374 rather than the whole TREE_VEC since they can have
16375 different number of elements. */
16377 parmvec
= expand_template_argument_pack (parmvec
);
16378 argvec
= expand_template_argument_pack (argvec
);
16380 len
= TREE_VEC_LENGTH (parmvec
);
16382 /* Check if the parameters end in a pack, making them
16385 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
16386 parm_variadic_p
= 1;
16388 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
16389 return unify_too_few_arguments (explain_p
,
16390 TREE_VEC_LENGTH (argvec
), len
);
16392 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16394 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
16395 TREE_VEC_ELT (parmvec
, i
),
16396 TREE_VEC_ELT (argvec
, i
),
16397 UNIFY_ALLOW_NONE
, explain_p
);
16400 if (parm_variadic_p
16401 && unify_pack_expansion (tparms
, targs
,
16404 /*subr=*/true, explain_p
))
16407 arg
= TYPE_TI_TEMPLATE (arg
);
16409 /* Fall through to deduce template name. */
16412 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16413 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16415 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
16417 /* Simple cases: Value already set, does match or doesn't. */
16418 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
16419 return unify_success (explain_p
);
16421 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
16425 /* If PARM is `const T' and ARG is only `int', we don't have
16426 a match unless we are allowing additional qualification.
16427 If ARG is `const int' and PARM is just `T' that's OK;
16428 that binds `const int' to `T'. */
16429 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
16431 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16433 /* Consider the case where ARG is `const volatile int' and
16434 PARM is `const T'. Then, T should be `volatile int'. */
16435 arg
= cp_build_qualified_type_real
16436 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
16437 if (arg
== error_mark_node
)
16438 return unify_invalid (explain_p
);
16440 /* Simple cases: Value already set, does match or doesn't. */
16441 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
16442 return unify_success (explain_p
);
16444 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
16446 /* Make sure that ARG is not a variable-sized array. (Note
16447 that were talking about variable-sized arrays (like
16448 `int[n]'), rather than arrays of unknown size (like
16449 `int[]').) We'll get very confused by such a type since
16450 the bound of the array is not constant, and therefore
16451 not mangleable. Besides, such types are not allowed in
16452 ISO C++, so we can do as we please here. We do allow
16453 them for 'auto' deduction, since that isn't ABI-exposed. */
16454 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
16455 return unify_vla_arg (explain_p
, arg
);
16457 /* Strip typedefs as in convert_template_argument. */
16458 arg
= canonicalize_type_argument (arg
, tf_none
);
16461 /* If ARG is a parameter pack or an expansion, we cannot unify
16462 against it unless PARM is also a parameter pack. */
16463 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
16464 && !template_parameter_pack_p (parm
))
16465 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
16467 /* If the argument deduction results is a METHOD_TYPE,
16468 then there is a problem.
16469 METHOD_TYPE doesn't map to any real C++ type the result of
16470 the deduction can not be of that type. */
16471 if (TREE_CODE (arg
) == METHOD_TYPE
)
16472 return unify_method_type_error (explain_p
, arg
);
16474 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
16475 return unify_success (explain_p
);
16477 case TEMPLATE_PARM_INDEX
:
16478 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
16479 if (tparm
== error_mark_node
)
16480 return unify_invalid (explain_p
);
16482 if (TEMPLATE_PARM_LEVEL (parm
)
16483 != template_decl_level (tparm
))
16485 /* The PARM is not one we're trying to unify. Just check
16486 to see if it matches ARG. */
16487 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
16488 && cp_tree_equal (parm
, arg
));
16490 unify_expression_unequal (explain_p
, parm
, arg
);
16494 idx
= TEMPLATE_PARM_IDX (parm
);
16495 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16499 int x
= !cp_tree_equal (targ
, arg
);
16501 unify_inconsistency (explain_p
, parm
, targ
, arg
);
16505 /* [temp.deduct.type] If, in the declaration of a function template
16506 with a non-type template-parameter, the non-type
16507 template-parameter is used in an expression in the function
16508 parameter-list and, if the corresponding template-argument is
16509 deduced, the template-argument type shall match the type of the
16510 template-parameter exactly, except that a template-argument
16511 deduced from an array bound may be of any integral type.
16512 The non-type parameter might use already deduced type parameters. */
16513 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
16514 if (!TREE_TYPE (arg
))
16515 /* Template-parameter dependent expression. Just accept it for now.
16516 It will later be processed in convert_template_argument. */
16518 else if (same_type_p (TREE_TYPE (arg
), tparm
))
16520 else if ((strict
& UNIFY_ALLOW_INTEGER
)
16521 && (TREE_CODE (tparm
) == INTEGER_TYPE
16522 || TREE_CODE (tparm
) == BOOLEAN_TYPE
))
16523 /* Convert the ARG to the type of PARM; the deduced non-type
16524 template argument must exactly match the types of the
16525 corresponding parameter. */
16526 arg
= fold (build_nop (tparm
, arg
));
16527 else if (uses_template_parms (tparm
))
16528 /* We haven't deduced the type of this parameter yet. Try again
16530 return unify_success (explain_p
);
16532 return unify_type_mismatch (explain_p
, tparm
, arg
);
16534 /* If ARG is a parameter pack or an expansion, we cannot unify
16535 against it unless PARM is also a parameter pack. */
16536 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
16537 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
16538 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
16540 arg
= strip_typedefs_expr (arg
);
16541 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
16542 return unify_success (explain_p
);
16546 /* A pointer-to-member constant can be unified only with
16547 another constant. */
16548 if (TREE_CODE (arg
) != PTRMEM_CST
)
16549 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
16551 /* Just unify the class member. It would be useless (and possibly
16552 wrong, depending on the strict flags) to unify also
16553 PTRMEM_CST_CLASS, because we want to be sure that both parm and
16554 arg refer to the same variable, even if through different
16555 classes. For instance:
16557 struct A { int x; };
16560 Unification of &A::x and &B::x must succeed. */
16561 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
16562 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
16567 if (TREE_CODE (arg
) != POINTER_TYPE
)
16568 return unify_type_mismatch (explain_p
, parm
, arg
);
16570 /* [temp.deduct.call]
16572 A can be another pointer or pointer to member type that can
16573 be converted to the deduced A via a qualification
16574 conversion (_conv.qual_).
16576 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
16577 This will allow for additional cv-qualification of the
16578 pointed-to types if appropriate. */
16580 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
16581 /* The derived-to-base conversion only persists through one
16582 level of pointers. */
16583 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
16585 return unify (tparms
, targs
, TREE_TYPE (parm
),
16586 TREE_TYPE (arg
), strict
, explain_p
);
16589 case REFERENCE_TYPE
:
16590 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
16591 return unify_type_mismatch (explain_p
, parm
, arg
);
16592 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16593 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
16596 if (TREE_CODE (arg
) != ARRAY_TYPE
)
16597 return unify_type_mismatch (explain_p
, parm
, arg
);
16598 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
16599 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
16600 return unify_type_mismatch (explain_p
, parm
, arg
);
16601 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16602 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
16603 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
16610 /* Our representation of array types uses "N - 1" as the
16611 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
16612 not an integer constant. We cannot unify arbitrarily
16613 complex expressions, so we eliminate the MINUS_EXPRs
16615 parm_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (parm
));
16616 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
16619 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
16620 parm_max
= TREE_OPERAND (parm_max
, 0);
16622 arg_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (arg
));
16623 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
16626 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
16627 trying to unify the type of a variable with the type
16628 of a template parameter. For example:
16630 template <unsigned int N>
16631 void f (char (&) [N]);
16638 Here, the type of the ARG will be "int [g(i)]", and
16639 may be a SAVE_EXPR, etc. */
16640 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
16641 return unify_vla_arg (explain_p
, arg
);
16642 arg_max
= TREE_OPERAND (arg_max
, 0);
16645 /* If only one of the bounds used a MINUS_EXPR, compensate
16646 by adding one to the other bound. */
16647 if (parm_cst
&& !arg_cst
)
16648 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
16652 else if (arg_cst
&& !parm_cst
)
16653 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
16658 RECUR_AND_CHECK_FAILURE (tparms
, targs
, parm_max
, arg_max
,
16659 UNIFY_ALLOW_INTEGER
, explain_p
);
16661 return unify_success (explain_p
);
16668 case ENUMERAL_TYPE
:
16671 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16672 return unify_type_mismatch (explain_p
, parm
, arg
);
16674 /* We have already checked cv-qualification at the top of the
16676 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
16677 return unify_type_mismatch (explain_p
, parm
, arg
);
16679 /* As far as unification is concerned, this wins. Later checks
16680 will invalidate it if necessary. */
16681 return unify_success (explain_p
);
16683 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
16684 /* Type INTEGER_CST can come from ordinary constant template args. */
16686 while (TREE_CODE (arg
) == NOP_EXPR
)
16687 arg
= TREE_OPERAND (arg
, 0);
16689 if (TREE_CODE (arg
) != INTEGER_CST
)
16690 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16691 return (tree_int_cst_equal (parm
, arg
)
16692 ? unify_success (explain_p
)
16693 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
16697 int i
, len
, argslen
;
16698 int parm_variadic_p
= 0;
16700 if (TREE_CODE (arg
) != TREE_VEC
)
16701 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16703 len
= TREE_VEC_LENGTH (parm
);
16704 argslen
= TREE_VEC_LENGTH (arg
);
16706 /* Check for pack expansions in the parameters. */
16707 for (i
= 0; i
< len
; ++i
)
16709 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
16712 /* We can unify against something with a trailing
16714 parm_variadic_p
= 1;
16716 /* [temp.deduct.type]/9: If the template argument list of
16717 P contains a pack expansion that is not the last
16718 template argument, the entire template argument list
16719 is a non-deduced context. */
16720 return unify_success (explain_p
);
16724 /* If we don't have enough arguments to satisfy the parameters
16725 (not counting the pack expression at the end), or we have
16726 too many arguments for a parameter list that doesn't end in
16727 a pack expression, we can't unify. */
16728 if (parm_variadic_p
16729 ? argslen
< len
- parm_variadic_p
16731 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
16733 /* Unify all of the parameters that precede the (optional)
16734 pack expression. */
16735 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16737 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
16738 TREE_VEC_ELT (parm
, i
),
16739 TREE_VEC_ELT (arg
, i
),
16740 UNIFY_ALLOW_NONE
, explain_p
);
16742 if (parm_variadic_p
)
16743 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
16745 /*subr=*/true, explain_p
);
16746 return unify_success (explain_p
);
16751 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16752 return unify_type_mismatch (explain_p
, parm
, arg
);
16754 if (TYPE_PTRMEMFUNC_P (parm
))
16756 if (!TYPE_PTRMEMFUNC_P (arg
))
16757 return unify_type_mismatch (explain_p
, parm
, arg
);
16759 return unify (tparms
, targs
,
16760 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
16761 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
16762 strict
, explain_p
);
16765 if (CLASSTYPE_TEMPLATE_INFO (parm
))
16767 tree t
= NULL_TREE
;
16769 if (strict_in
& UNIFY_ALLOW_DERIVED
)
16771 /* First, we try to unify the PARM and ARG directly. */
16772 t
= try_class_unification (tparms
, targs
,
16773 parm
, arg
, explain_p
);
16777 /* Fallback to the special case allowed in
16778 [temp.deduct.call]:
16780 If P is a class, and P has the form
16781 template-id, then A can be a derived class of
16782 the deduced A. Likewise, if P is a pointer to
16783 a class of the form template-id, A can be a
16784 pointer to a derived class pointed to by the
16786 enum template_base_result r
;
16787 r
= get_template_base (tparms
, targs
, parm
, arg
,
16791 return unify_no_common_base (explain_p
, r
, parm
, arg
);
16794 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
16795 && (CLASSTYPE_TI_TEMPLATE (parm
)
16796 == CLASSTYPE_TI_TEMPLATE (arg
)))
16797 /* Perhaps PARM is something like S<U> and ARG is S<int>.
16798 Then, we should unify `int' and `U'. */
16801 /* There's no chance of unification succeeding. */
16802 return unify_type_mismatch (explain_p
, parm
, arg
);
16804 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
16805 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
16807 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
16808 return unify_type_mismatch (explain_p
, parm
, arg
);
16809 return unify_success (explain_p
);
16812 case FUNCTION_TYPE
:
16814 unsigned int nargs
;
16819 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16820 return unify_type_mismatch (explain_p
, parm
, arg
);
16822 /* CV qualifications for methods can never be deduced, they must
16823 match exactly. We need to check them explicitly here,
16824 because type_unification_real treats them as any other
16825 cv-qualified parameter. */
16826 if (TREE_CODE (parm
) == METHOD_TYPE
16827 && (!check_cv_quals_for_unify
16829 class_of_this_parm (arg
),
16830 class_of_this_parm (parm
))))
16831 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16833 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
16834 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
16836 nargs
= list_length (TYPE_ARG_TYPES (arg
));
16837 args
= XALLOCAVEC (tree
, nargs
);
16838 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
16839 a
!= NULL_TREE
&& a
!= void_list_node
;
16840 a
= TREE_CHAIN (a
), ++i
)
16841 args
[i
] = TREE_VALUE (a
);
16844 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
16845 args
, nargs
, 1, DEDUCE_EXACT
,
16846 LOOKUP_NORMAL
, explain_p
);
16850 /* Unify a pointer to member with a pointer to member function, which
16851 deduces the type of the member as a function type. */
16852 if (TYPE_PTRMEMFUNC_P (arg
))
16857 /* Check top-level cv qualifiers */
16858 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
16859 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16861 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
16862 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
16863 UNIFY_ALLOW_NONE
, explain_p
);
16865 /* Determine the type of the function we are unifying against. */
16866 method_type
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg
));
16868 build_function_type (TREE_TYPE (method_type
),
16869 TREE_CHAIN (TYPE_ARG_TYPES (method_type
)));
16871 /* Extract the cv-qualifiers of the member function from the
16872 implicit object parameter and place them on the function
16873 type to be restored later. */
16874 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (method_type
));
16875 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
16878 if (TREE_CODE (arg
) != OFFSET_TYPE
)
16879 return unify_type_mismatch (explain_p
, parm
, arg
);
16880 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
16881 TYPE_OFFSET_BASETYPE (arg
),
16882 UNIFY_ALLOW_NONE
, explain_p
);
16883 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16884 strict
, explain_p
);
16887 if (DECL_TEMPLATE_PARM_P (parm
))
16888 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
16889 if (arg
!= integral_constant_value (parm
))
16890 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16891 return unify_success (explain_p
);
16894 case TEMPLATE_DECL
:
16895 /* Matched cases are handled by the ARG == PARM test above. */
16896 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16899 /* A non-type template parameter that is a variable should be a
16900 an integral constant, in which case, it whould have been
16901 folded into its (constant) value. So we should not be getting
16902 a variable here. */
16903 gcc_unreachable ();
16905 case TYPE_ARGUMENT_PACK
:
16906 case NONTYPE_ARGUMENT_PACK
:
16907 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
16908 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
16911 case DECLTYPE_TYPE
:
16912 case UNDERLYING_TYPE
:
16913 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
16914 or UNDERLYING_TYPE nodes. */
16915 return unify_success (explain_p
);
16918 /* Unification fails if we hit an error node. */
16919 return unify_invalid (explain_p
);
16922 /* An unresolved overload is a nondeduced context. */
16923 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
16924 return unify_success (explain_p
);
16925 gcc_assert (EXPR_P (parm
));
16927 /* We must be looking at an expression. This can happen with
16931 void foo(S<I>, S<I + 2>);
16933 This is a "nondeduced context":
16937 The nondeduced contexts are:
16939 --A type that is a template-id in which one or more of
16940 the template-arguments is an expression that references
16941 a template-parameter.
16943 In these cases, we assume deduction succeeded, but don't
16944 actually infer any unifications. */
16946 if (!uses_template_parms (parm
)
16947 && !template_args_equal (parm
, arg
))
16948 return unify_expression_unequal (explain_p
, parm
, arg
);
16950 return unify_success (explain_p
);
16953 #undef RECUR_AND_CHECK_FAILURE
16955 /* Note that DECL can be defined in this translation unit, if
16959 mark_definable (tree decl
)
16962 DECL_NOT_REALLY_EXTERN (decl
) = 1;
16963 FOR_EACH_CLONE (clone
, decl
)
16964 DECL_NOT_REALLY_EXTERN (clone
) = 1;
16967 /* Called if RESULT is explicitly instantiated, or is a member of an
16968 explicitly instantiated class. */
16971 mark_decl_instantiated (tree result
, int extern_p
)
16973 SET_DECL_EXPLICIT_INSTANTIATION (result
);
16975 /* If this entity has already been written out, it's too late to
16976 make any modifications. */
16977 if (TREE_ASM_WRITTEN (result
))
16980 if (TREE_CODE (result
) != FUNCTION_DECL
)
16981 /* The TREE_PUBLIC flag for function declarations will have been
16982 set correctly by tsubst. */
16983 TREE_PUBLIC (result
) = 1;
16985 /* This might have been set by an earlier implicit instantiation. */
16986 DECL_COMDAT (result
) = 0;
16989 DECL_NOT_REALLY_EXTERN (result
) = 0;
16992 mark_definable (result
);
16993 /* Always make artificials weak. */
16994 if (DECL_ARTIFICIAL (result
) && flag_weak
)
16995 comdat_linkage (result
);
16996 /* For WIN32 we also want to put explicit instantiations in
16997 linkonce sections. */
16998 else if (TREE_PUBLIC (result
))
16999 maybe_make_one_only (result
);
17002 /* If EXTERN_P, then this function will not be emitted -- unless
17003 followed by an explicit instantiation, at which point its linkage
17004 will be adjusted. If !EXTERN_P, then this function will be
17005 emitted here. In neither circumstance do we want
17006 import_export_decl to adjust the linkage. */
17007 DECL_INTERFACE_KNOWN (result
) = 1;
17010 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17011 important template arguments. If any are missing, we check whether
17012 they're important by using error_mark_node for substituting into any
17013 args that were used for partial ordering (the ones between ARGS and END)
17014 and seeing if it bubbles up. */
17017 check_undeduced_parms (tree targs
, tree args
, tree end
)
17019 bool found
= false;
17021 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
17022 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
17025 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
17029 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
17030 if (substed
== error_mark_node
)
17036 /* Given two function templates PAT1 and PAT2, return:
17038 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17039 -1 if PAT2 is more specialized than PAT1.
17040 0 if neither is more specialized.
17042 LEN indicates the number of parameters we should consider
17043 (defaulted parameters should not be considered).
17045 The 1998 std underspecified function template partial ordering, and
17046 DR214 addresses the issue. We take pairs of arguments, one from
17047 each of the templates, and deduce them against each other. One of
17048 the templates will be more specialized if all the *other*
17049 template's arguments deduce against its arguments and at least one
17050 of its arguments *does* *not* deduce against the other template's
17051 corresponding argument. Deduction is done as for class templates.
17052 The arguments used in deduction have reference and top level cv
17053 qualifiers removed. Iff both arguments were originally reference
17054 types *and* deduction succeeds in both directions, the template
17055 with the more cv-qualified argument wins for that pairing (if
17056 neither is more cv-qualified, they both are equal). Unlike regular
17057 deduction, after all the arguments have been deduced in this way,
17058 we do *not* verify the deduced template argument values can be
17059 substituted into non-deduced contexts.
17061 The logic can be a bit confusing here, because we look at deduce1 and
17062 targs1 to see if pat2 is at least as specialized, and vice versa; if we
17063 can find template arguments for pat1 to make arg1 look like arg2, that
17064 means that arg2 is at least as specialized as arg1. */
17067 more_specialized_fn (tree pat1
, tree pat2
, int len
)
17069 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
17070 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
17071 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
17072 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
17073 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
17074 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
17075 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
17076 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
17077 tree origs1
, origs2
;
17078 bool lose1
= false;
17079 bool lose2
= false;
17081 /* Remove the this parameter from non-static member functions. If
17082 one is a non-static member function and the other is not a static
17083 member function, remove the first parameter from that function
17084 also. This situation occurs for operator functions where we
17085 locate both a member function (with this pointer) and non-member
17086 operator (with explicit first operand). */
17087 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
17089 len
--; /* LEN is the number of significant arguments for DECL1 */
17090 args1
= TREE_CHAIN (args1
);
17091 if (!DECL_STATIC_FUNCTION_P (decl2
))
17092 args2
= TREE_CHAIN (args2
);
17094 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
17096 args2
= TREE_CHAIN (args2
);
17097 if (!DECL_STATIC_FUNCTION_P (decl1
))
17100 args1
= TREE_CHAIN (args1
);
17104 /* If only one is a conversion operator, they are unordered. */
17105 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
17108 /* Consider the return type for a conversion function */
17109 if (DECL_CONV_FN_P (decl1
))
17111 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
17112 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
17116 processing_template_decl
++;
17122 /* Stop when an ellipsis is seen. */
17123 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
17125 tree arg1
= TREE_VALUE (args1
);
17126 tree arg2
= TREE_VALUE (args2
);
17127 int deduce1
, deduce2
;
17131 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
17132 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17134 /* When both arguments are pack expansions, we need only
17135 unify the patterns themselves. */
17136 arg1
= PACK_EXPANSION_PATTERN (arg1
);
17137 arg2
= PACK_EXPANSION_PATTERN (arg2
);
17139 /* This is the last comparison we need to do. */
17143 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
17145 arg1
= TREE_TYPE (arg1
);
17146 quals1
= cp_type_quals (arg1
);
17149 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
17151 arg2
= TREE_TYPE (arg2
);
17152 quals2
= cp_type_quals (arg2
);
17155 arg1
= TYPE_MAIN_VARIANT (arg1
);
17156 arg2
= TYPE_MAIN_VARIANT (arg2
);
17158 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
17160 int i
, len2
= list_length (args2
);
17161 tree parmvec
= make_tree_vec (1);
17162 tree argvec
= make_tree_vec (len2
);
17165 /* Setup the parameter vector, which contains only ARG1. */
17166 TREE_VEC_ELT (parmvec
, 0) = arg1
;
17168 /* Setup the argument vector, which contains the remaining
17170 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
17171 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
17173 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
17174 argvec
, DEDUCE_EXACT
,
17175 /*subr=*/true, /*explain_p=*/false)
17178 /* We cannot deduce in the other direction, because ARG1 is
17179 a pack expansion but ARG2 is not. */
17182 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17184 int i
, len1
= list_length (args1
);
17185 tree parmvec
= make_tree_vec (1);
17186 tree argvec
= make_tree_vec (len1
);
17189 /* Setup the parameter vector, which contains only ARG1. */
17190 TREE_VEC_ELT (parmvec
, 0) = arg2
;
17192 /* Setup the argument vector, which contains the remaining
17194 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
17195 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
17197 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
17198 argvec
, DEDUCE_EXACT
,
17199 /*subr=*/true, /*explain_p=*/false)
17202 /* We cannot deduce in the other direction, because ARG2 is
17203 a pack expansion but ARG1 is not.*/
17209 /* The normal case, where neither argument is a pack
17211 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
17212 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
17214 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
17215 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
17219 /* If we couldn't deduce arguments for tparms1 to make arg1 match
17220 arg2, then arg2 is not as specialized as arg1. */
17226 /* "If, for a given type, deduction succeeds in both directions
17227 (i.e., the types are identical after the transformations above)
17228 and if the type from the argument template is more cv-qualified
17229 than the type from the parameter template (as described above)
17230 that type is considered to be more specialized than the other. If
17231 neither type is more cv-qualified than the other then neither type
17232 is more specialized than the other." */
17234 if (deduce1
&& deduce2
17235 && quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
17237 if ((quals1
& quals2
) == quals2
)
17239 if ((quals1
& quals2
) == quals1
)
17243 if (lose1
&& lose2
)
17244 /* We've failed to deduce something in either direction.
17245 These must be unordered. */
17248 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
17249 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17250 /* We have already processed all of the arguments in our
17251 handing of the pack expansion type. */
17254 args1
= TREE_CHAIN (args1
);
17255 args2
= TREE_CHAIN (args2
);
17258 /* "In most cases, all template parameters must have values in order for
17259 deduction to succeed, but for partial ordering purposes a template
17260 parameter may remain without a value provided it is not used in the
17261 types being used for partial ordering."
17263 Thus, if we are missing any of the targs1 we need to substitute into
17264 origs1, then pat2 is not as specialized as pat1. This can happen when
17265 there is a nondeduced context. */
17266 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
17268 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
17271 processing_template_decl
--;
17273 /* All things being equal, if the next argument is a pack expansion
17274 for one function but not for the other, prefer the
17275 non-variadic function. FIXME this is bogus; see c++/41958. */
17277 && args1
&& TREE_VALUE (args1
)
17278 && args2
&& TREE_VALUE (args2
))
17280 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
17281 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
17284 if (lose1
== lose2
)
17292 /* Determine which of two partial specializations of MAIN_TMPL is more
17295 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
17296 to the first partial specialization. The TREE_VALUE is the
17297 innermost set of template parameters for the partial
17298 specialization. PAT2 is similar, but for the second template.
17300 Return 1 if the first partial specialization is more specialized;
17301 -1 if the second is more specialized; 0 if neither is more
17304 See [temp.class.order] for information about determining which of
17305 two templates is more specialized. */
17308 more_specialized_class (tree main_tmpl
, tree pat1
, tree pat2
)
17313 bool any_deductions
= false;
17315 tmpl1
= TREE_TYPE (pat1
);
17316 tmpl2
= TREE_TYPE (pat2
);
17318 /* Just like what happens for functions, if we are ordering between
17319 different class template specializations, we may encounter dependent
17320 types in the arguments, and we need our dependency check functions
17321 to behave correctly. */
17322 ++processing_template_decl
;
17323 targs
= get_class_bindings (main_tmpl
, TREE_VALUE (pat1
),
17324 CLASSTYPE_TI_ARGS (tmpl1
),
17325 CLASSTYPE_TI_ARGS (tmpl2
));
17329 any_deductions
= true;
17332 targs
= get_class_bindings (main_tmpl
, TREE_VALUE (pat2
),
17333 CLASSTYPE_TI_ARGS (tmpl2
),
17334 CLASSTYPE_TI_ARGS (tmpl1
));
17338 any_deductions
= true;
17340 --processing_template_decl
;
17342 /* In the case of a tie where at least one of the class templates
17343 has a parameter pack at the end, the template with the most
17344 non-packed parameters wins. */
17347 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
17348 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
17350 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
17351 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
17352 int len1
= TREE_VEC_LENGTH (args1
);
17353 int len2
= TREE_VEC_LENGTH (args2
);
17355 /* We don't count the pack expansion at the end. */
17356 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
17358 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
17363 else if (len1
< len2
)
17370 /* Return the template arguments that will produce the function signature
17371 DECL from the function template FN, with the explicit template
17372 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
17373 also match. Return NULL_TREE if no satisfactory arguments could be
17377 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
17379 int ntparms
= DECL_NTPARMS (fn
);
17380 tree targs
= make_tree_vec (ntparms
);
17381 tree decl_type
= TREE_TYPE (decl
);
17382 tree decl_arg_types
;
17384 unsigned int nargs
, ix
;
17387 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
17389 /* Never do unification on the 'this' parameter. */
17390 decl_arg_types
= skip_artificial_parms_for (decl
,
17391 TYPE_ARG_TYPES (decl_type
));
17393 nargs
= list_length (decl_arg_types
);
17394 args
= XALLOCAVEC (tree
, nargs
);
17395 for (arg
= decl_arg_types
, ix
= 0;
17396 arg
!= NULL_TREE
&& arg
!= void_list_node
;
17397 arg
= TREE_CHAIN (arg
), ++ix
)
17398 args
[ix
] = TREE_VALUE (arg
);
17400 if (fn_type_unification (fn
, explicit_args
, targs
,
17402 (check_rettype
|| DECL_CONV_FN_P (fn
)
17403 ? TREE_TYPE (decl_type
) : NULL_TREE
),
17404 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false)
17405 == error_mark_node
)
17411 /* Return the innermost template arguments that, when applied to a partial
17412 specialization of MAIN_TMPL whose innermost template parameters are
17413 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
17416 For example, suppose we have:
17418 template <class T, class U> struct S {};
17419 template <class T> struct S<T*, int> {};
17421 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
17422 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
17423 int}. The resulting vector will be {double}, indicating that `T'
17424 is bound to `double'. */
17427 get_class_bindings (tree main_tmpl
, tree tparms
, tree spec_args
, tree args
)
17429 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
17431 tree innermost_deduced_args
;
17433 innermost_deduced_args
= make_tree_vec (ntparms
);
17434 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
17436 deduced_args
= copy_node (args
);
17437 SET_TMPL_ARGS_LEVEL (deduced_args
,
17438 TMPL_ARGS_DEPTH (deduced_args
),
17439 innermost_deduced_args
);
17442 deduced_args
= innermost_deduced_args
;
17444 if (unify (tparms
, deduced_args
,
17445 INNERMOST_TEMPLATE_ARGS (spec_args
),
17446 INNERMOST_TEMPLATE_ARGS (args
),
17447 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
17450 for (i
= 0; i
< ntparms
; ++i
)
17451 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
17454 /* Verify that nondeduced template arguments agree with the type
17455 obtained from argument deduction.
17459 struct A { typedef int X; };
17460 template <class T, class U> struct C {};
17461 template <class T> struct C<T, typename T::X> {};
17463 Then with the instantiation `C<A, int>', we can deduce that
17464 `T' is `A' but unify () does not check whether `typename T::X'
17466 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
17467 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (main_tmpl
),
17468 spec_args
, main_tmpl
,
17469 tf_none
, false, false);
17470 if (spec_args
== error_mark_node
17471 /* We only need to check the innermost arguments; the other
17472 arguments will always agree. */
17473 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
17474 INNERMOST_TEMPLATE_ARGS (args
)))
17477 /* Now that we have bindings for all of the template arguments,
17478 ensure that the arguments deduced for the template template
17479 parameters have compatible template parameter lists. See the use
17480 of template_template_parm_bindings_ok_p in fn_type_unification
17481 for more information. */
17482 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
17485 return deduced_args
;
17488 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
17489 Return the TREE_LIST node with the most specialized template, if
17490 any. If there is no most specialized template, the error_mark_node
17493 Note that this function does not look at, or modify, the
17494 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
17495 returned is one of the elements of INSTANTIATIONS, callers may
17496 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
17497 and retrieve it from the value returned. */
17500 most_specialized_instantiation (tree templates
)
17504 ++processing_template_decl
;
17507 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
17511 if (get_bindings (TREE_VALUE (champ
),
17512 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
17513 NULL_TREE
, /*check_ret=*/true))
17516 if (get_bindings (TREE_VALUE (fn
),
17517 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
17518 NULL_TREE
, /*check_ret=*/true))
17525 /* Equally specialized, move to next function. If there
17526 is no next function, nothing's most specialized. */
17527 fn
= TREE_CHAIN (fn
);
17535 /* Now verify that champ is better than everything earlier in the
17536 instantiation list. */
17537 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
17538 if (get_bindings (TREE_VALUE (champ
),
17539 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
17540 NULL_TREE
, /*check_ret=*/true)
17541 || !get_bindings (TREE_VALUE (fn
),
17542 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
17543 NULL_TREE
, /*check_ret=*/true))
17549 processing_template_decl
--;
17552 return error_mark_node
;
17557 /* If DECL is a specialization of some template, return the most
17558 general such template. Otherwise, returns NULL_TREE.
17560 For example, given:
17562 template <class T> struct S { template <class U> void f(U); };
17564 if TMPL is `template <class U> void S<int>::f(U)' this will return
17565 the full template. This function will not trace past partial
17566 specializations, however. For example, given in addition:
17568 template <class T> struct S<T*> { template <class U> void f(U); };
17570 if TMPL is `template <class U> void S<int*>::f(U)' this will return
17571 `template <class T> template <class U> S<T*>::f(U)'. */
17574 most_general_template (tree decl
)
17576 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
17577 an immediate specialization. */
17578 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17580 if (DECL_TEMPLATE_INFO (decl
)) {
17581 decl
= DECL_TI_TEMPLATE (decl
);
17583 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
17584 template friend. */
17585 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
17591 /* Look for more and more general templates. */
17592 while (DECL_TEMPLATE_INFO (decl
))
17594 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
17595 (See cp-tree.h for details.) */
17596 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
17599 if (CLASS_TYPE_P (TREE_TYPE (decl
))
17600 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
17603 /* Stop if we run into an explicitly specialized class template. */
17604 if (!DECL_NAMESPACE_SCOPE_P (decl
)
17605 && DECL_CONTEXT (decl
)
17606 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
17609 decl
= DECL_TI_TEMPLATE (decl
);
17615 /* Return the most specialized of the class template partial
17616 specializations of TMPL which can produce TYPE, a specialization of
17617 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
17618 a _TYPE node corresponding to the partial specialization, while the
17619 TREE_PURPOSE is the set of template arguments that must be
17620 substituted into the TREE_TYPE in order to generate TYPE.
17622 If the choice of partial specialization is ambiguous, a diagnostic
17623 is issued, and the error_mark_node is returned. If there are no
17624 partial specializations of TMPL matching TYPE, then NULL_TREE is
17628 most_specialized_class (tree type
, tree tmpl
, tsubst_flags_t complain
)
17630 tree list
= NULL_TREE
;
17636 tree outer_args
= NULL_TREE
;
17638 tmpl
= most_general_template (tmpl
);
17639 args
= CLASSTYPE_TI_ARGS (type
);
17641 /* For determining which partial specialization to use, only the
17642 innermost args are interesting. */
17643 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
17645 outer_args
= strip_innermost_template_args (args
, 1);
17646 args
= INNERMOST_TEMPLATE_ARGS (args
);
17649 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
17651 tree partial_spec_args
;
17653 tree parms
= TREE_VALUE (t
);
17655 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
17657 ++processing_template_decl
;
17663 /* Discard the outer levels of args, and then substitute in the
17664 template args from the enclosing class. */
17665 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
17666 partial_spec_args
= tsubst_template_args
17667 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
17669 /* PARMS already refers to just the innermost parms, but the
17670 template parms in partial_spec_args had their levels lowered
17671 by tsubst, so we need to do the same for the parm list. We
17672 can't just tsubst the TREE_VEC itself, as tsubst wants to
17673 treat a TREE_VEC as an argument vector. */
17674 parms
= copy_node (parms
);
17675 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
17676 TREE_VEC_ELT (parms
, i
) =
17677 tsubst (TREE_VEC_ELT (parms
, i
), outer_args
, tf_none
, NULL_TREE
);
17681 partial_spec_args
=
17682 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
17683 add_to_template_args (outer_args
,
17684 partial_spec_args
),
17686 /*require_all_args=*/true,
17687 /*use_default_args=*/true);
17689 --processing_template_decl
;
17691 if (partial_spec_args
== error_mark_node
)
17692 return error_mark_node
;
17694 spec_args
= get_class_bindings (tmpl
, parms
,
17700 spec_args
= add_to_template_args (outer_args
, spec_args
);
17701 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
17702 TREE_TYPE (list
) = TREE_TYPE (t
);
17709 ambiguous_p
= false;
17712 t
= TREE_CHAIN (t
);
17713 for (; t
; t
= TREE_CHAIN (t
))
17715 fate
= more_specialized_class (tmpl
, champ
, t
);
17722 t
= TREE_CHAIN (t
);
17725 ambiguous_p
= true;
17734 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
17736 fate
= more_specialized_class (tmpl
, champ
, t
);
17739 ambiguous_p
= true;
17747 char *spaces
= NULL
;
17748 if (!(complain
& tf_error
))
17749 return error_mark_node
;
17750 error ("ambiguous class template instantiation for %q#T", type
);
17751 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
17752 for (t
= list
; t
; t
= TREE_CHAIN (t
))
17754 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
17755 spaces
= spaces
? spaces
: get_spaces (str
);
17758 return error_mark_node
;
17764 /* Explicitly instantiate DECL. */
17767 do_decl_instantiation (tree decl
, tree storage
)
17769 tree result
= NULL_TREE
;
17772 if (!decl
|| decl
== error_mark_node
)
17773 /* An error occurred, for which grokdeclarator has already issued
17774 an appropriate message. */
17776 else if (! DECL_LANG_SPECIFIC (decl
))
17778 error ("explicit instantiation of non-template %q#D", decl
);
17781 else if (TREE_CODE (decl
) == VAR_DECL
)
17783 /* There is an asymmetry here in the way VAR_DECLs and
17784 FUNCTION_DECLs are handled by grokdeclarator. In the case of
17785 the latter, the DECL we get back will be marked as a
17786 template instantiation, and the appropriate
17787 DECL_TEMPLATE_INFO will be set up. This does not happen for
17788 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
17789 should handle VAR_DECLs as it currently handles
17791 if (!DECL_CLASS_SCOPE_P (decl
))
17793 error ("%qD is not a static data member of a class template", decl
);
17796 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
17797 if (!result
|| TREE_CODE (result
) != VAR_DECL
)
17799 error ("no matching template for %qD found", decl
);
17802 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
17804 error ("type %qT for explicit instantiation %qD does not match "
17805 "declared type %qT", TREE_TYPE (result
), decl
,
17810 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
17812 error ("explicit instantiation of %q#D", decl
);
17818 /* Check for various error cases. Note that if the explicit
17819 instantiation is valid the RESULT will currently be marked as an
17820 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
17821 until we get here. */
17823 if (DECL_TEMPLATE_SPECIALIZATION (result
))
17825 /* DR 259 [temp.spec].
17827 Both an explicit instantiation and a declaration of an explicit
17828 specialization shall not appear in a program unless the explicit
17829 instantiation follows a declaration of the explicit specialization.
17831 For a given set of template parameters, if an explicit
17832 instantiation of a template appears after a declaration of an
17833 explicit specialization for that template, the explicit
17834 instantiation has no effect. */
17837 else if (DECL_EXPLICIT_INSTANTIATION (result
))
17841 No program shall explicitly instantiate any template more
17844 We check DECL_NOT_REALLY_EXTERN so as not to complain when
17845 the first instantiation was `extern' and the second is not,
17846 and EXTERN_P for the opposite case. */
17847 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
17848 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
17849 /* If an "extern" explicit instantiation follows an ordinary
17850 explicit instantiation, the template is instantiated. */
17854 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
17856 error ("no matching template for %qD found", result
);
17859 else if (!DECL_TEMPLATE_INFO (result
))
17861 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
17865 if (storage
== NULL_TREE
)
17867 else if (storage
== ridpointers
[(int) RID_EXTERN
])
17869 if (!in_system_header
&& (cxx_dialect
== cxx98
))
17870 pedwarn (input_location
, OPT_Wpedantic
,
17871 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
17876 error ("storage class %qD applied to template instantiation", storage
);
17878 check_explicit_instantiation_namespace (result
);
17879 mark_decl_instantiated (result
, extern_p
);
17881 instantiate_decl (result
, /*defer_ok=*/1,
17882 /*expl_inst_class_mem_p=*/false);
17886 mark_class_instantiated (tree t
, int extern_p
)
17888 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
17889 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
17890 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
17891 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
17894 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
17895 rest_of_type_compilation (t
, 1);
17899 /* Called from do_type_instantiation through binding_table_foreach to
17900 do recursive instantiation for the type bound in ENTRY. */
17902 bt_instantiate_type_proc (binding_entry entry
, void *data
)
17904 tree storage
= *(tree
*) data
;
17906 if (MAYBE_CLASS_TYPE_P (entry
->type
)
17907 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
17908 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
17911 /* Called from do_type_instantiation to instantiate a member
17912 (a member function or a static member variable) of an
17913 explicitly instantiated class template. */
17915 instantiate_class_member (tree decl
, int extern_p
)
17917 mark_decl_instantiated (decl
, extern_p
);
17919 instantiate_decl (decl
, /*defer_ok=*/1,
17920 /*expl_inst_class_mem_p=*/true);
17923 /* Perform an explicit instantiation of template class T. STORAGE, if
17924 non-null, is the RID for extern, inline or static. COMPLAIN is
17925 nonzero if this is called from the parser, zero if called recursively,
17926 since the standard is unclear (as detailed below). */
17929 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
17934 int previous_instantiation_extern_p
= 0;
17936 if (TREE_CODE (t
) == TYPE_DECL
)
17939 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
17942 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
17944 error ("explicit instantiation of non-class template %qD", tmpl
);
17946 error ("explicit instantiation of non-template type %qT", t
);
17952 if (!COMPLETE_TYPE_P (t
))
17954 if (complain
& tf_error
)
17955 error ("explicit instantiation of %q#T before definition of template",
17960 if (storage
!= NULL_TREE
)
17962 if (!in_system_header
)
17964 if (storage
== ridpointers
[(int) RID_EXTERN
])
17966 if (cxx_dialect
== cxx98
)
17967 pedwarn (input_location
, OPT_Wpedantic
,
17968 "ISO C++ 1998 forbids the use of %<extern%> on "
17969 "explicit instantiations");
17972 pedwarn (input_location
, OPT_Wpedantic
,
17973 "ISO C++ forbids the use of %qE"
17974 " on explicit instantiations", storage
);
17977 if (storage
== ridpointers
[(int) RID_INLINE
])
17979 else if (storage
== ridpointers
[(int) RID_EXTERN
])
17981 else if (storage
== ridpointers
[(int) RID_STATIC
])
17985 error ("storage class %qD applied to template instantiation",
17991 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
17993 /* DR 259 [temp.spec].
17995 Both an explicit instantiation and a declaration of an explicit
17996 specialization shall not appear in a program unless the explicit
17997 instantiation follows a declaration of the explicit specialization.
17999 For a given set of template parameters, if an explicit
18000 instantiation of a template appears after a declaration of an
18001 explicit specialization for that template, the explicit
18002 instantiation has no effect. */
18005 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
18009 No program shall explicitly instantiate any template more
18012 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18013 instantiation was `extern'. If EXTERN_P then the second is.
18014 These cases are OK. */
18015 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
18017 if (!previous_instantiation_extern_p
&& !extern_p
18018 && (complain
& tf_error
))
18019 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
18021 /* If we've already instantiated the template, just return now. */
18022 if (!CLASSTYPE_INTERFACE_ONLY (t
))
18026 check_explicit_instantiation_namespace (TYPE_NAME (t
));
18027 mark_class_instantiated (t
, extern_p
);
18035 /* In contrast to implicit instantiation, where only the
18036 declarations, and not the definitions, of members are
18037 instantiated, we have here:
18041 The explicit instantiation of a class template specialization
18042 implies the instantiation of all of its members not
18043 previously explicitly specialized in the translation unit
18044 containing the explicit instantiation.
18046 Of course, we can't instantiate member template classes, since
18047 we don't have any arguments for them. Note that the standard
18048 is unclear on whether the instantiation of the members are
18049 *explicit* instantiations or not. However, the most natural
18050 interpretation is that it should be an explicit instantiation. */
18053 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18054 if (TREE_CODE (tmp
) == FUNCTION_DECL
18055 && DECL_TEMPLATE_INSTANTIATION (tmp
))
18056 instantiate_class_member (tmp
, extern_p
);
18058 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18059 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
18060 instantiate_class_member (tmp
, extern_p
);
18062 if (CLASSTYPE_NESTED_UTDS (t
))
18063 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
18064 bt_instantiate_type_proc
, &storage
);
18068 /* Given a function DECL, which is a specialization of TMPL, modify
18069 DECL to be a re-instantiation of TMPL with the same template
18070 arguments. TMPL should be the template into which tsubst'ing
18071 should occur for DECL, not the most general template.
18073 One reason for doing this is a scenario like this:
18076 void f(const T&, int i);
18078 void g() { f(3, 7); }
18081 void f(const T& t, const int i) { }
18083 Note that when the template is first instantiated, with
18084 instantiate_template, the resulting DECL will have no name for the
18085 first parameter, and the wrong type for the second. So, when we go
18086 to instantiate the DECL, we regenerate it. */
18089 regenerate_decl_from_template (tree decl
, tree tmpl
)
18091 /* The arguments used to instantiate DECL, from the most general
18096 args
= DECL_TI_ARGS (decl
);
18097 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
18099 /* Make sure that we can see identifiers, and compute access
18101 push_access_scope (decl
);
18103 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18111 args_depth
= TMPL_ARGS_DEPTH (args
);
18112 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
18113 if (args_depth
> parms_depth
)
18114 args
= get_innermost_template_args (args
, parms_depth
);
18116 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
18117 args
, tf_error
, NULL_TREE
,
18118 /*defer_ok*/false);
18119 if (specs
&& specs
!= error_mark_node
)
18120 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
18123 /* Merge parameter declarations. */
18124 decl_parm
= skip_artificial_parms_for (decl
,
18125 DECL_ARGUMENTS (decl
));
18127 = skip_artificial_parms_for (code_pattern
,
18128 DECL_ARGUMENTS (code_pattern
));
18129 while (decl_parm
&& !FUNCTION_PARAMETER_PACK_P (pattern_parm
))
18134 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
18135 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
18136 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
18138 parm_type
= type_decays_to (parm_type
);
18139 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
18140 TREE_TYPE (decl_parm
) = parm_type
;
18141 attributes
= DECL_ATTRIBUTES (pattern_parm
);
18142 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
18144 DECL_ATTRIBUTES (decl_parm
) = attributes
;
18145 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
18147 decl_parm
= DECL_CHAIN (decl_parm
);
18148 pattern_parm
= DECL_CHAIN (pattern_parm
);
18150 /* Merge any parameters that match with the function parameter
18152 if (pattern_parm
&& FUNCTION_PARAMETER_PACK_P (pattern_parm
))
18155 tree expanded_types
;
18156 /* Expand the TYPE_PACK_EXPANSION that provides the types for
18157 the parameters in this function parameter pack. */
18158 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
18159 args
, tf_error
, NULL_TREE
);
18160 len
= TREE_VEC_LENGTH (expanded_types
);
18161 for (i
= 0; i
< len
; i
++)
18166 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
18167 /* Rename the parameter to include the index. */
18168 DECL_NAME (decl_parm
) =
18169 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
18170 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
18171 parm_type
= type_decays_to (parm_type
);
18172 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
18173 TREE_TYPE (decl_parm
) = parm_type
;
18174 attributes
= DECL_ATTRIBUTES (pattern_parm
);
18175 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
18177 DECL_ATTRIBUTES (decl_parm
) = attributes
;
18178 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
18180 decl_parm
= DECL_CHAIN (decl_parm
);
18183 /* Merge additional specifiers from the CODE_PATTERN. */
18184 if (DECL_DECLARED_INLINE_P (code_pattern
)
18185 && !DECL_DECLARED_INLINE_P (decl
))
18186 DECL_DECLARED_INLINE_P (decl
) = 1;
18188 else if (TREE_CODE (decl
) == VAR_DECL
)
18190 DECL_INITIAL (decl
) =
18191 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
18192 tf_error
, DECL_TI_TEMPLATE (decl
),
18193 /*integral_constant_expression_p=*/false);
18194 if (VAR_HAD_UNKNOWN_BOUND (decl
))
18195 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
18196 tf_error
, DECL_TI_TEMPLATE (decl
));
18199 gcc_unreachable ();
18201 pop_access_scope (decl
);
18204 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
18205 substituted to get DECL. */
18208 template_for_substitution (tree decl
)
18210 tree tmpl
= DECL_TI_TEMPLATE (decl
);
18212 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
18213 for the instantiation. This is not always the most general
18214 template. Consider, for example:
18217 struct S { template <class U> void f();
18218 template <> void f<int>(); };
18220 and an instantiation of S<double>::f<int>. We want TD to be the
18221 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
18222 while (/* An instantiation cannot have a definition, so we need a
18223 more general template. */
18224 DECL_TEMPLATE_INSTANTIATION (tmpl
)
18225 /* We must also deal with friend templates. Given:
18227 template <class T> struct S {
18228 template <class U> friend void f() {};
18231 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
18232 so far as the language is concerned, but that's still
18233 where we get the pattern for the instantiation from. On
18234 other hand, if the definition comes outside the class, say:
18236 template <class T> struct S {
18237 template <class U> friend void f();
18239 template <class U> friend void f() {}
18241 we don't need to look any further. That's what the check for
18242 DECL_INITIAL is for. */
18243 || (TREE_CODE (decl
) == FUNCTION_DECL
18244 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
18245 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
18247 /* The present template, TD, should not be a definition. If it
18248 were a definition, we should be using it! Note that we
18249 cannot restructure the loop to just keep going until we find
18250 a template with a definition, since that might go too far if
18251 a specialization was declared, but not defined. */
18252 gcc_assert (TREE_CODE (decl
) != VAR_DECL
18253 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
18255 /* Fetch the more general template. */
18256 tmpl
= DECL_TI_TEMPLATE (tmpl
);
18262 /* Returns true if we need to instantiate this template instance even if we
18263 know we aren't going to emit it.. */
18266 always_instantiate_p (tree decl
)
18268 /* We always instantiate inline functions so that we can inline them. An
18269 explicit instantiation declaration prohibits implicit instantiation of
18270 non-inline functions. With high levels of optimization, we would
18271 normally inline non-inline functions -- but we're not allowed to do
18272 that for "extern template" functions. Therefore, we check
18273 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
18274 return ((TREE_CODE (decl
) == FUNCTION_DECL
18275 && (DECL_DECLARED_INLINE_P (decl
)
18276 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
18277 /* And we need to instantiate static data members so that
18278 their initializers are available in integral constant
18280 || (TREE_CODE (decl
) == VAR_DECL
18281 && decl_maybe_constant_var_p (decl
)));
18284 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
18285 instantiate it now, modifying TREE_TYPE (fn). */
18288 maybe_instantiate_noexcept (tree fn
)
18290 tree fntype
, spec
, noex
, clone
;
18292 if (DECL_CLONED_FUNCTION_P (fn
))
18293 fn
= DECL_CLONED_FUNCTION (fn
);
18294 fntype
= TREE_TYPE (fn
);
18295 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
18297 if (!DEFERRED_NOEXCEPT_SPEC_P (spec
))
18300 noex
= TREE_PURPOSE (spec
);
18302 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
18304 if (push_tinst_level (fn
))
18306 push_access_scope (fn
);
18307 input_location
= DECL_SOURCE_LOCATION (fn
);
18308 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
18309 DEFERRED_NOEXCEPT_ARGS (noex
),
18310 tf_warning_or_error
, fn
,
18311 /*function_p=*/false,
18312 /*integral_constant_expression_p=*/true);
18313 pop_access_scope (fn
);
18314 pop_tinst_level ();
18315 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
18316 if (spec
== error_mark_node
)
18317 spec
= noexcept_false_spec
;
18320 spec
= noexcept_false_spec
;
18324 /* This is an implicitly declared function, so NOEX is a list of
18325 other functions to evaluate and merge. */
18327 spec
= noexcept_true_spec
;
18328 for (elt
= noex
; elt
; elt
= OVL_NEXT (elt
))
18330 tree fn
= OVL_CURRENT (elt
);
18332 maybe_instantiate_noexcept (fn
);
18333 subspec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
18334 spec
= merge_exception_specifiers (spec
, subspec
, NULL_TREE
);
18338 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
18340 FOR_EACH_CLONE (clone
, fn
)
18342 if (TREE_TYPE (clone
) == fntype
)
18343 TREE_TYPE (clone
) = TREE_TYPE (fn
);
18345 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
18349 /* Produce the definition of D, a _DECL generated from a template. If
18350 DEFER_OK is nonzero, then we don't have to actually do the
18351 instantiation now; we just have to do it sometime. Normally it is
18352 an error if this is an explicit instantiation but D is undefined.
18353 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
18354 explicitly instantiated class template. */
18357 instantiate_decl (tree d
, int defer_ok
,
18358 bool expl_inst_class_mem_p
)
18360 tree tmpl
= DECL_TI_TEMPLATE (d
);
18367 bool pattern_defined
;
18368 location_t saved_loc
= input_location
;
18373 /* This function should only be used to instantiate templates for
18374 functions and static member variables. */
18375 gcc_assert (TREE_CODE (d
) == FUNCTION_DECL
18376 || TREE_CODE (d
) == VAR_DECL
);
18378 /* Variables are never deferred; if instantiation is required, they
18379 are instantiated right away. That allows for better code in the
18380 case that an expression refers to the value of the variable --
18381 if the variable has a constant value the referring expression can
18382 take advantage of that fact. */
18383 if (TREE_CODE (d
) == VAR_DECL
18384 || DECL_DECLARED_CONSTEXPR_P (d
))
18387 /* Don't instantiate cloned functions. Instead, instantiate the
18388 functions they cloned. */
18389 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
18390 d
= DECL_CLONED_FUNCTION (d
);
18392 if (DECL_TEMPLATE_INSTANTIATED (d
)
18393 || (TREE_CODE (d
) == FUNCTION_DECL
18394 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
18395 || DECL_TEMPLATE_SPECIALIZATION (d
))
18396 /* D has already been instantiated or explicitly specialized, so
18397 there's nothing for us to do here.
18399 It might seem reasonable to check whether or not D is an explicit
18400 instantiation, and, if so, stop here. But when an explicit
18401 instantiation is deferred until the end of the compilation,
18402 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
18403 the instantiation. */
18406 /* Check to see whether we know that this template will be
18407 instantiated in some other file, as with "extern template"
18409 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
18411 /* In general, we do not instantiate such templates. */
18412 if (external_p
&& !always_instantiate_p (d
))
18415 gen_tmpl
= most_general_template (tmpl
);
18416 gen_args
= DECL_TI_ARGS (d
);
18418 if (tmpl
!= gen_tmpl
)
18419 /* We should already have the extra args. */
18420 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
18421 == TMPL_ARGS_DEPTH (gen_args
));
18422 /* And what's in the hash table should match D. */
18423 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
18424 || spec
== NULL_TREE
);
18426 /* This needs to happen before any tsubsting. */
18427 if (! push_tinst_level (d
))
18430 timevar_push (TV_TEMPLATE_INST
);
18432 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
18433 for the instantiation. */
18434 td
= template_for_substitution (d
);
18435 code_pattern
= DECL_TEMPLATE_RESULT (td
);
18437 /* We should never be trying to instantiate a member of a class
18438 template or partial specialization. */
18439 gcc_assert (d
!= code_pattern
);
18441 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
18442 || DECL_TEMPLATE_SPECIALIZATION (td
))
18443 /* In the case of a friend template whose definition is provided
18444 outside the class, we may have too many arguments. Drop the
18445 ones we don't need. The same is true for specializations. */
18446 args
= get_innermost_template_args
18447 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
18451 if (TREE_CODE (d
) == FUNCTION_DECL
)
18452 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
18453 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
));
18455 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
18457 /* We may be in the middle of deferred access check. Disable it now. */
18458 push_deferring_access_checks (dk_no_deferred
);
18460 /* Unless an explicit instantiation directive has already determined
18461 the linkage of D, remember that a definition is available for
18463 if (pattern_defined
18464 && !DECL_INTERFACE_KNOWN (d
)
18465 && !DECL_NOT_REALLY_EXTERN (d
))
18466 mark_definable (d
);
18468 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
18469 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
18470 input_location
= DECL_SOURCE_LOCATION (d
);
18472 /* If D is a member of an explicitly instantiated class template,
18473 and no definition is available, treat it like an implicit
18475 if (!pattern_defined
&& expl_inst_class_mem_p
18476 && DECL_EXPLICIT_INSTANTIATION (d
))
18478 /* Leave linkage flags alone on instantiations with anonymous
18480 if (TREE_PUBLIC (d
))
18482 DECL_NOT_REALLY_EXTERN (d
) = 0;
18483 DECL_INTERFACE_KNOWN (d
) = 0;
18485 SET_DECL_IMPLICIT_INSTANTIATION (d
);
18488 if (TREE_CODE (d
) == FUNCTION_DECL
)
18489 maybe_instantiate_noexcept (d
);
18491 /* Defer all other templates, unless we have been explicitly
18492 forbidden from doing so. */
18493 if (/* If there is no definition, we cannot instantiate the
18496 /* If it's OK to postpone instantiation, do so. */
18498 /* If this is a static data member that will be defined
18499 elsewhere, we don't want to instantiate the entire data
18500 member, but we do want to instantiate the initializer so that
18501 we can substitute that elsewhere. */
18502 || (external_p
&& TREE_CODE (d
) == VAR_DECL
))
18504 /* The definition of the static data member is now required so
18505 we must substitute the initializer. */
18506 if (TREE_CODE (d
) == VAR_DECL
18507 && !DECL_INITIAL (d
)
18508 && DECL_INITIAL (code_pattern
))
18512 bool const_init
= false;
18514 ns
= decl_namespace_context (d
);
18515 push_nested_namespace (ns
);
18516 push_nested_class (DECL_CONTEXT (d
));
18517 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
18519 tf_warning_or_error
, NULL_TREE
,
18520 /*integral_constant_expression_p=*/false);
18521 /* Make sure the initializer is still constant, in case of
18522 circular dependency (template/instantiate6.C). */
18524 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
18525 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
18526 /*asmspec_tree=*/NULL_TREE
,
18527 LOOKUP_ONLYCONVERTING
);
18528 pop_nested_class ();
18529 pop_nested_namespace (ns
);
18532 /* We restore the source position here because it's used by
18533 add_pending_template. */
18534 input_location
= saved_loc
;
18536 if (at_eof
&& !pattern_defined
18537 && DECL_EXPLICIT_INSTANTIATION (d
)
18538 && DECL_NOT_REALLY_EXTERN (d
))
18541 The definition of a non-exported function template, a
18542 non-exported member function template, or a non-exported
18543 member function or static data member of a class template
18544 shall be present in every translation unit in which it is
18545 explicitly instantiated. */
18546 permerror (input_location
, "explicit instantiation of %qD "
18547 "but no definition available", d
);
18549 /* If we're in unevaluated context, we just wanted to get the
18550 constant value; this isn't an odr use, so don't queue
18551 a full instantiation. */
18552 if (cp_unevaluated_operand
!= 0)
18554 /* ??? Historically, we have instantiated inline functions, even
18555 when marked as "extern template". */
18556 if (!(external_p
&& TREE_CODE (d
) == VAR_DECL
))
18557 add_pending_template (d
);
18560 /* Tell the repository that D is available in this translation unit
18561 -- and see if it is supposed to be instantiated here. */
18562 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
18564 /* In a PCH file, despite the fact that the repository hasn't
18565 requested instantiation in the PCH it is still possible that
18566 an instantiation will be required in a file that includes the
18569 add_pending_template (d
);
18570 /* Instantiate inline functions so that the inliner can do its
18571 job, even though we'll not be emitting a copy of this
18573 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
18577 fn_context
= decl_function_context (d
);
18578 nested
= (current_function_decl
!= NULL_TREE
);
18580 push_to_top_level ();
18582 push_function_context ();
18584 /* Mark D as instantiated so that recursive calls to
18585 instantiate_decl do not try to instantiate it again. */
18586 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
18588 /* Regenerate the declaration in case the template has been modified
18589 by a subsequent redeclaration. */
18590 regenerate_decl_from_template (d
, td
);
18592 /* We already set the file and line above. Reset them now in case
18593 they changed as a result of calling regenerate_decl_from_template. */
18594 input_location
= DECL_SOURCE_LOCATION (d
);
18596 if (TREE_CODE (d
) == VAR_DECL
)
18599 bool const_init
= false;
18601 /* Clear out DECL_RTL; whatever was there before may not be right
18602 since we've reset the type of the declaration. */
18603 SET_DECL_RTL (d
, NULL
);
18604 DECL_IN_AGGR_P (d
) = 0;
18606 /* The initializer is placed in DECL_INITIAL by
18607 regenerate_decl_from_template so we don't need to
18608 push/pop_access_scope again here. Pull it out so that
18609 cp_finish_decl can process it. */
18610 init
= DECL_INITIAL (d
);
18611 DECL_INITIAL (d
) = NULL_TREE
;
18612 DECL_INITIALIZED_P (d
) = 0;
18614 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
18615 initializer. That function will defer actual emission until
18616 we have a chance to determine linkage. */
18617 DECL_EXTERNAL (d
) = 0;
18619 /* Enter the scope of D so that access-checking works correctly. */
18620 push_nested_class (DECL_CONTEXT (d
));
18621 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
18622 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
18623 pop_nested_class ();
18625 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
18626 synthesize_method (d
);
18627 else if (TREE_CODE (d
) == FUNCTION_DECL
)
18629 struct pointer_map_t
*saved_local_specializations
;
18634 /* Save away the current list, in case we are instantiating one
18635 template from within the body of another. */
18636 saved_local_specializations
= local_specializations
;
18638 /* Set up the list of local specializations. */
18639 local_specializations
= pointer_map_create ();
18641 /* Set up context. */
18642 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
18644 /* Some typedefs referenced from within the template code need to be
18645 access checked at template instantiation time, i.e now. These
18646 types were added to the template at parsing time. Let's get those
18647 and perform the access checks then. */
18648 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl
),
18651 /* Create substitution entries for the parameters. */
18652 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
18653 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
18654 spec_parm
= DECL_ARGUMENTS (d
);
18655 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
18657 register_local_specialization (spec_parm
, tmpl_parm
);
18658 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
18659 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
18661 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
18663 if (!FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
18665 register_local_specialization (spec_parm
, tmpl_parm
);
18666 spec_parm
= DECL_CHAIN (spec_parm
);
18670 /* Register the (value) argument pack as a specialization of
18671 TMPL_PARM, then move on. */
18672 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
18673 register_local_specialization (argpack
, tmpl_parm
);
18676 gcc_assert (!spec_parm
);
18678 /* Substitute into the body of the function. */
18679 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
18680 tf_warning_or_error
, tmpl
,
18681 /*integral_constant_expression_p=*/false);
18683 /* Set the current input_location to the end of the function
18684 so that finish_function knows where we are. */
18685 input_location
= DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
18687 /* We don't need the local specializations any more. */
18688 pointer_map_destroy (local_specializations
);
18689 local_specializations
= saved_local_specializations
;
18691 /* Finish the function. */
18692 d
= finish_function (0);
18693 expand_or_defer_fn (d
);
18696 /* We're not deferring instantiation any more. */
18697 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
18700 pop_from_top_level ();
18702 pop_function_context ();
18705 input_location
= saved_loc
;
18706 pop_deferring_access_checks ();
18707 pop_tinst_level ();
18709 timevar_pop (TV_TEMPLATE_INST
);
18714 /* Run through the list of templates that we wish we could
18715 instantiate, and instantiate any we can. RETRIES is the
18716 number of times we retry pending template instantiation. */
18719 instantiate_pending_templates (int retries
)
18722 location_t saved_loc
= input_location
;
18724 /* Instantiating templates may trigger vtable generation. This in turn
18725 may require further template instantiations. We place a limit here
18726 to avoid infinite loop. */
18727 if (pending_templates
&& retries
>= max_tinst_depth
)
18729 tree decl
= pending_templates
->tinst
->decl
;
18731 error ("template instantiation depth exceeds maximum of %d"
18732 " instantiating %q+D, possibly from virtual table generation"
18733 " (use -ftemplate-depth= to increase the maximum)",
18734 max_tinst_depth
, decl
);
18735 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18736 /* Pretend that we defined it. */
18737 DECL_INITIAL (decl
) = error_mark_node
;
18743 struct pending_template
**t
= &pending_templates
;
18744 struct pending_template
*last
= NULL
;
18748 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
18749 bool complete
= false;
18751 if (TYPE_P (instantiation
))
18755 if (!COMPLETE_TYPE_P (instantiation
))
18757 instantiate_class_template (instantiation
);
18758 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
18759 for (fn
= TYPE_METHODS (instantiation
);
18761 fn
= TREE_CHAIN (fn
))
18762 if (! DECL_ARTIFICIAL (fn
))
18763 instantiate_decl (fn
,
18765 /*expl_inst_class_mem_p=*/false);
18766 if (COMPLETE_TYPE_P (instantiation
))
18770 complete
= COMPLETE_TYPE_P (instantiation
);
18774 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
18775 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
18778 = instantiate_decl (instantiation
,
18780 /*expl_inst_class_mem_p=*/false);
18781 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
18785 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
18786 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
18790 /* If INSTANTIATION has been instantiated, then we don't
18791 need to consider it again in the future. */
18799 current_tinst_level
= NULL
;
18801 last_pending_template
= last
;
18803 while (reconsider
);
18805 input_location
= saved_loc
;
18808 /* Substitute ARGVEC into T, which is a list of initializers for
18809 either base class or a non-static data member. The TREE_PURPOSEs
18810 are DECLs, and the TREE_VALUEs are the initializer values. Used by
18811 instantiate_decl. */
18814 tsubst_initializer_list (tree t
, tree argvec
)
18816 tree inits
= NULL_TREE
;
18818 for (; t
; t
= TREE_CHAIN (t
))
18822 tree expanded_bases
= NULL_TREE
;
18823 tree expanded_arguments
= NULL_TREE
;
18826 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
18831 /* Expand the base class expansion type into separate base
18833 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
18834 tf_warning_or_error
,
18836 if (expanded_bases
== error_mark_node
)
18839 /* We'll be building separate TREE_LISTs of arguments for
18841 len
= TREE_VEC_LENGTH (expanded_bases
);
18842 expanded_arguments
= make_tree_vec (len
);
18843 for (i
= 0; i
< len
; i
++)
18844 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
18846 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
18847 expand each argument in the TREE_VALUE of t. */
18848 expr
= make_node (EXPR_PACK_EXPANSION
);
18849 PACK_EXPANSION_LOCAL_P (expr
) = true;
18850 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
18851 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
18853 if (TREE_VALUE (t
) == void_type_node
)
18854 /* VOID_TYPE_NODE is used to indicate
18855 value-initialization. */
18857 for (i
= 0; i
< len
; i
++)
18858 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
18862 /* Substitute parameter packs into each argument in the
18864 in_base_initializer
= 1;
18865 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
18867 tree expanded_exprs
;
18869 /* Expand the argument. */
18870 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
18872 = tsubst_pack_expansion (expr
, argvec
,
18873 tf_warning_or_error
,
18875 if (expanded_exprs
== error_mark_node
)
18878 /* Prepend each of the expanded expressions to the
18879 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
18880 for (i
= 0; i
< len
; i
++)
18882 TREE_VEC_ELT (expanded_arguments
, i
) =
18883 tree_cons (NULL_TREE
,
18884 TREE_VEC_ELT (expanded_exprs
, i
),
18885 TREE_VEC_ELT (expanded_arguments
, i
));
18888 in_base_initializer
= 0;
18890 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
18891 since we built them backwards. */
18892 for (i
= 0; i
< len
; i
++)
18894 TREE_VEC_ELT (expanded_arguments
, i
) =
18895 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
18900 for (i
= 0; i
< len
; ++i
)
18902 if (expanded_bases
)
18904 decl
= TREE_VEC_ELT (expanded_bases
, i
);
18905 decl
= expand_member_init (decl
);
18906 init
= TREE_VEC_ELT (expanded_arguments
, i
);
18911 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
18912 tf_warning_or_error
, NULL_TREE
);
18914 decl
= expand_member_init (decl
);
18915 if (decl
&& !DECL_P (decl
))
18916 in_base_initializer
= 1;
18918 init
= TREE_VALUE (t
);
18920 if (init
!= void_type_node
)
18921 init
= tsubst_expr (init
, argvec
,
18922 tf_warning_or_error
, NULL_TREE
,
18923 /*integral_constant_expression_p=*/false);
18924 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
18925 /* If we had an initializer but it instantiated to nothing,
18926 value-initialize the object. This will only occur when
18927 the initializer was a pack expansion where the parameter
18928 packs used in that expansion were of length zero. */
18929 init
= void_type_node
;
18930 in_base_initializer
= 0;
18935 init
= build_tree_list (decl
, init
);
18936 TREE_CHAIN (init
) = inits
;
18944 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
18947 set_current_access_from_decl (tree decl
)
18949 if (TREE_PRIVATE (decl
))
18950 current_access_specifier
= access_private_node
;
18951 else if (TREE_PROTECTED (decl
))
18952 current_access_specifier
= access_protected_node
;
18954 current_access_specifier
= access_public_node
;
18957 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
18958 is the instantiation (which should have been created with
18959 start_enum) and ARGS are the template arguments to use. */
18962 tsubst_enum (tree tag
, tree newtag
, tree args
)
18966 if (SCOPED_ENUM_P (newtag
))
18967 begin_scope (sk_scoped_enum
, newtag
);
18969 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
18974 decl
= TREE_VALUE (e
);
18975 /* Note that in a template enum, the TREE_VALUE is the
18976 CONST_DECL, not the corresponding INTEGER_CST. */
18977 value
= tsubst_expr (DECL_INITIAL (decl
),
18978 args
, tf_warning_or_error
, NULL_TREE
,
18979 /*integral_constant_expression_p=*/true);
18981 /* Give this enumeration constant the correct access. */
18982 set_current_access_from_decl (decl
);
18984 /* Actually build the enumerator itself. */
18986 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
18989 if (SCOPED_ENUM_P (newtag
))
18992 finish_enum_value_list (newtag
);
18993 finish_enum (newtag
);
18995 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
18996 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
18999 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19000 its type -- but without substituting the innermost set of template
19001 arguments. So, innermost set of template parameters will appear in
19005 get_mostly_instantiated_function_type (tree decl
)
19013 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
19014 targs
= DECL_TI_ARGS (decl
);
19015 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
19016 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
19018 /* There should be as many levels of arguments as there are levels
19020 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
19022 fn_type
= TREE_TYPE (tmpl
);
19024 if (parm_depth
== 1)
19025 /* No substitution is necessary. */
19032 /* Replace the innermost level of the TARGS with NULL_TREEs to
19033 let tsubst know not to substitute for those parameters. */
19034 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
19035 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
19036 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
19037 TMPL_ARGS_LEVEL (targs
, i
));
19038 SET_TMPL_ARGS_LEVEL (partial_args
,
19039 TMPL_ARGS_DEPTH (targs
),
19040 make_tree_vec (DECL_NTPARMS (tmpl
)));
19042 /* Make sure that we can see identifiers, and compute access
19044 push_access_scope (decl
);
19046 ++processing_template_decl
;
19047 /* Now, do the (partial) substitution to figure out the
19048 appropriate function type. */
19049 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
19050 --processing_template_decl
;
19052 /* Substitute into the template parameters to obtain the real
19053 innermost set of parameters. This step is important if the
19054 innermost set of template parameters contains value
19055 parameters whose types depend on outer template parameters. */
19056 TREE_VEC_LENGTH (partial_args
)--;
19057 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
19059 pop_access_scope (decl
);
19065 /* Return truthvalue if we're processing a template different from
19066 the last one involved in diagnostics. */
19068 problematic_instantiation_changed (void)
19070 return current_tinst_level
!= last_error_tinst_level
;
19073 /* Remember current template involved in diagnostics. */
19075 record_last_problematic_instantiation (void)
19077 last_error_tinst_level
= current_tinst_level
;
19080 struct tinst_level
*
19081 current_instantiation (void)
19083 return current_tinst_level
;
19086 /* [temp.param] Check that template non-type parm TYPE is of an allowable
19087 type. Return zero for ok, nonzero for disallowed. Issue error and
19088 warning messages under control of COMPLAIN. */
19091 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
19093 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
19095 else if (POINTER_TYPE_P (type
))
19097 else if (TYPE_PTRMEM_P (type
))
19099 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
19101 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
19103 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
19105 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
19108 if (complain
& tf_error
)
19110 if (type
== error_mark_node
)
19111 inform (input_location
, "invalid template non-type parameter");
19113 error ("%q#T is not a valid type for a template non-type parameter",
19119 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
19120 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
19123 dependent_type_p_r (tree type
)
19129 A type is dependent if it is:
19131 -- a template parameter. Template template parameters are types
19132 for us (since TYPE_P holds true for them) so we handle
19134 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
19135 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
19137 /* -- a qualified-id with a nested-name-specifier which contains a
19138 class-name that names a dependent type or whose unqualified-id
19139 names a dependent type. */
19140 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19142 /* -- a cv-qualified type where the cv-unqualified type is
19144 type
= TYPE_MAIN_VARIANT (type
);
19145 /* -- a compound type constructed from any dependent type. */
19146 if (TYPE_PTRMEM_P (type
))
19147 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
19148 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
19150 else if (TREE_CODE (type
) == POINTER_TYPE
19151 || TREE_CODE (type
) == REFERENCE_TYPE
)
19152 return dependent_type_p (TREE_TYPE (type
));
19153 else if (TREE_CODE (type
) == FUNCTION_TYPE
19154 || TREE_CODE (type
) == METHOD_TYPE
)
19158 if (dependent_type_p (TREE_TYPE (type
)))
19160 for (arg_type
= TYPE_ARG_TYPES (type
);
19162 arg_type
= TREE_CHAIN (arg_type
))
19163 if (dependent_type_p (TREE_VALUE (arg_type
)))
19167 /* -- an array type constructed from any dependent type or whose
19168 size is specified by a constant expression that is
19171 We checked for type- and value-dependence of the bounds in
19172 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
19173 if (TREE_CODE (type
) == ARRAY_TYPE
)
19175 if (TYPE_DOMAIN (type
)
19176 && dependent_type_p (TYPE_DOMAIN (type
)))
19178 return dependent_type_p (TREE_TYPE (type
));
19181 /* -- a template-id in which either the template name is a template
19183 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19185 /* ... or any of the template arguments is a dependent type or
19186 an expression that is type-dependent or value-dependent. */
19187 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
19188 && (any_dependent_template_arguments_p
19189 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
19192 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
19193 dependent; if the argument of the `typeof' expression is not
19194 type-dependent, then it should already been have resolved. */
19195 if (TREE_CODE (type
) == TYPEOF_TYPE
19196 || TREE_CODE (type
) == DECLTYPE_TYPE
19197 || TREE_CODE (type
) == UNDERLYING_TYPE
)
19200 /* A template argument pack is dependent if any of its packed
19202 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
19204 tree args
= ARGUMENT_PACK_ARGS (type
);
19205 int i
, len
= TREE_VEC_LENGTH (args
);
19206 for (i
= 0; i
< len
; ++i
)
19207 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
19211 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
19212 be template parameters. */
19213 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
19216 /* The standard does not specifically mention types that are local
19217 to template functions or local classes, but they should be
19218 considered dependent too. For example:
19220 template <int I> void f() {
19225 The size of `E' cannot be known until the value of `I' has been
19226 determined. Therefore, `E' must be considered dependent. */
19227 scope
= TYPE_CONTEXT (type
);
19228 if (scope
&& TYPE_P (scope
))
19229 return dependent_type_p (scope
);
19230 /* Don't use type_dependent_expression_p here, as it can lead
19231 to infinite recursion trying to determine whether a lambda
19232 nested in a lambda is dependent (c++/47687). */
19233 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
19234 && DECL_LANG_SPECIFIC (scope
)
19235 && DECL_TEMPLATE_INFO (scope
)
19236 && (any_dependent_template_arguments_p
19237 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
19240 /* Other types are non-dependent. */
19244 /* Returns TRUE if TYPE is dependent, in the sense of
19245 [temp.dep.type]. Note that a NULL type is considered dependent. */
19248 dependent_type_p (tree type
)
19250 /* If there are no template parameters in scope, then there can't be
19251 any dependent types. */
19252 if (!processing_template_decl
)
19254 /* If we are not processing a template, then nobody should be
19255 providing us with a dependent type. */
19257 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
19261 /* If the type is NULL, we have not computed a type for the entity
19262 in question; in that case, the type is dependent. */
19266 /* Erroneous types can be considered non-dependent. */
19267 if (type
== error_mark_node
)
19270 /* If we have not already computed the appropriate value for TYPE,
19272 if (!TYPE_DEPENDENT_P_VALID (type
))
19274 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
19275 TYPE_DEPENDENT_P_VALID (type
) = 1;
19278 return TYPE_DEPENDENT_P (type
);
19281 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
19282 lookup. In other words, a dependent type that is not the current
19286 dependent_scope_p (tree scope
)
19288 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
19289 && !currently_open_class (scope
));
19292 /* T is a SCOPE_REF; return whether we need to consider it
19293 instantiation-dependent so that we can check access at instantiation
19294 time even though we know which member it resolves to. */
19297 instantiation_dependent_scope_ref_p (tree t
)
19299 if (DECL_P (TREE_OPERAND (t
, 1))
19300 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
19301 && accessible_in_template_p (TREE_OPERAND (t
, 0),
19302 TREE_OPERAND (t
, 1)))
19308 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
19309 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
19312 /* Note that this predicate is not appropriate for general expressions;
19313 only constant expressions (that satisfy potential_constant_expression)
19314 can be tested for value dependence. */
19317 value_dependent_expression_p (tree expression
)
19319 if (!processing_template_decl
)
19322 /* A name declared with a dependent type. */
19323 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
19326 switch (TREE_CODE (expression
))
19328 case IDENTIFIER_NODE
:
19329 /* A name that has not been looked up -- must be dependent. */
19332 case TEMPLATE_PARM_INDEX
:
19333 /* A non-type template parm. */
19337 /* A non-type template parm. */
19338 if (DECL_TEMPLATE_PARM_P (expression
))
19340 return value_dependent_expression_p (DECL_INITIAL (expression
));
19343 /* A constant with literal type and is initialized
19344 with an expression that is value-dependent.
19346 Note that a non-dependent parenthesized initializer will have
19347 already been replaced with its constant value, so if we see
19348 a TREE_LIST it must be dependent. */
19349 if (DECL_INITIAL (expression
)
19350 && decl_constant_var_p (expression
)
19351 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
19352 || value_dependent_expression_p (DECL_INITIAL (expression
))))
19356 case DYNAMIC_CAST_EXPR
:
19357 case STATIC_CAST_EXPR
:
19358 case CONST_CAST_EXPR
:
19359 case REINTERPRET_CAST_EXPR
:
19361 /* These expressions are value-dependent if the type to which
19362 the cast occurs is dependent or the expression being casted
19363 is value-dependent. */
19365 tree type
= TREE_TYPE (expression
);
19367 if (dependent_type_p (type
))
19370 /* A functional cast has a list of operands. */
19371 expression
= TREE_OPERAND (expression
, 0);
19374 /* If there are no operands, it must be an expression such
19375 as "int()". This should not happen for aggregate types
19376 because it would form non-constant expressions. */
19377 gcc_assert (cxx_dialect
>= cxx0x
19378 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
19383 if (TREE_CODE (expression
) == TREE_LIST
)
19384 return any_value_dependent_elements_p (expression
);
19386 return value_dependent_expression_p (expression
);
19390 if (SIZEOF_EXPR_TYPE_P (expression
))
19391 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
19395 /* A `sizeof' expression is value-dependent if the operand is
19396 type-dependent or is a pack expansion. */
19397 expression
= TREE_OPERAND (expression
, 0);
19398 if (PACK_EXPANSION_P (expression
))
19400 else if (TYPE_P (expression
))
19401 return dependent_type_p (expression
);
19402 return instantiation_dependent_expression_p (expression
);
19404 case AT_ENCODE_EXPR
:
19405 /* An 'encode' expression is value-dependent if the operand is
19407 expression
= TREE_OPERAND (expression
, 0);
19408 return dependent_type_p (expression
);
19410 case NOEXCEPT_EXPR
:
19411 expression
= TREE_OPERAND (expression
, 0);
19412 return instantiation_dependent_expression_p (expression
);
19415 /* All instantiation-dependent expressions should also be considered
19416 value-dependent. */
19417 return instantiation_dependent_scope_ref_p (expression
);
19419 case COMPONENT_REF
:
19420 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
19421 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
19423 case NONTYPE_ARGUMENT_PACK
:
19424 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
19425 is value-dependent. */
19427 tree values
= ARGUMENT_PACK_ARGS (expression
);
19428 int i
, len
= TREE_VEC_LENGTH (values
);
19430 for (i
= 0; i
< len
; ++i
)
19431 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
19439 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
19440 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
19441 || (type2
? dependent_type_p (type2
) : false));
19445 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19446 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
19449 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19450 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
19454 tree op
= TREE_OPERAND (expression
, 0);
19455 return (value_dependent_expression_p (op
)
19456 || has_value_dependent_address (op
));
19461 tree fn
= get_callee_fndecl (expression
);
19463 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
19465 nargs
= call_expr_nargs (expression
);
19466 for (i
= 0; i
< nargs
; ++i
)
19468 tree op
= CALL_EXPR_ARG (expression
, i
);
19469 /* In a call to a constexpr member function, look through the
19470 implicit ADDR_EXPR on the object argument so that it doesn't
19471 cause the call to be considered value-dependent. We also
19472 look through it in potential_constant_expression. */
19473 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
19474 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
19475 && TREE_CODE (op
) == ADDR_EXPR
)
19476 op
= TREE_OPERAND (op
, 0);
19477 if (value_dependent_expression_p (op
))
19483 case TEMPLATE_ID_EXPR
:
19484 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
19486 return type_dependent_expression_p (expression
);
19492 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
19493 if (value_dependent_expression_p (val
))
19499 /* Treat a GNU statement expression as dependent to avoid crashing
19500 under fold_non_dependent_expr; it can't be constant. */
19504 /* A constant expression is value-dependent if any subexpression is
19505 value-dependent. */
19506 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
19508 case tcc_reference
:
19510 case tcc_comparison
:
19512 case tcc_expression
:
19515 int i
, len
= cp_tree_operand_length (expression
);
19517 for (i
= 0; i
< len
; i
++)
19519 tree t
= TREE_OPERAND (expression
, i
);
19521 /* In some cases, some of the operands may be missing.l
19522 (For example, in the case of PREDECREMENT_EXPR, the
19523 amount to increment by may be missing.) That doesn't
19524 make the expression dependent. */
19525 if (t
&& value_dependent_expression_p (t
))
19536 /* The expression is not value-dependent. */
19540 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
19541 [temp.dep.expr]. Note that an expression with no type is
19542 considered dependent. Other parts of the compiler arrange for an
19543 expression with type-dependent subexpressions to have no type, so
19544 this function doesn't have to be fully recursive. */
19547 type_dependent_expression_p (tree expression
)
19549 if (!processing_template_decl
)
19552 if (expression
== error_mark_node
)
19555 /* An unresolved name is always dependent. */
19556 if (TREE_CODE (expression
) == IDENTIFIER_NODE
19557 || TREE_CODE (expression
) == USING_DECL
)
19560 /* Some expression forms are never type-dependent. */
19561 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
19562 || TREE_CODE (expression
) == SIZEOF_EXPR
19563 || TREE_CODE (expression
) == ALIGNOF_EXPR
19564 || TREE_CODE (expression
) == AT_ENCODE_EXPR
19565 || TREE_CODE (expression
) == NOEXCEPT_EXPR
19566 || TREE_CODE (expression
) == TRAIT_EXPR
19567 || TREE_CODE (expression
) == TYPEID_EXPR
19568 || TREE_CODE (expression
) == DELETE_EXPR
19569 || TREE_CODE (expression
) == VEC_DELETE_EXPR
19570 || TREE_CODE (expression
) == THROW_EXPR
)
19573 /* The types of these expressions depends only on the type to which
19574 the cast occurs. */
19575 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
19576 || TREE_CODE (expression
) == STATIC_CAST_EXPR
19577 || TREE_CODE (expression
) == CONST_CAST_EXPR
19578 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
19579 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
19580 || TREE_CODE (expression
) == CAST_EXPR
)
19581 return dependent_type_p (TREE_TYPE (expression
));
19583 /* The types of these expressions depends only on the type created
19584 by the expression. */
19585 if (TREE_CODE (expression
) == NEW_EXPR
19586 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
19588 /* For NEW_EXPR tree nodes created inside a template, either
19589 the object type itself or a TREE_LIST may appear as the
19591 tree type
= TREE_OPERAND (expression
, 1);
19592 if (TREE_CODE (type
) == TREE_LIST
)
19593 /* This is an array type. We need to check array dimensions
19595 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
19596 || value_dependent_expression_p
19597 (TREE_OPERAND (TREE_VALUE (type
), 1));
19599 return dependent_type_p (type
);
19602 if (TREE_CODE (expression
) == SCOPE_REF
)
19604 tree scope
= TREE_OPERAND (expression
, 0);
19605 tree name
= TREE_OPERAND (expression
, 1);
19607 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
19608 contains an identifier associated by name lookup with one or more
19609 declarations declared with a dependent type, or...a
19610 nested-name-specifier or qualified-id that names a member of an
19611 unknown specialization. */
19612 return (type_dependent_expression_p (name
)
19613 || dependent_scope_p (scope
));
19616 if (TREE_CODE (expression
) == FUNCTION_DECL
19617 && DECL_LANG_SPECIFIC (expression
)
19618 && DECL_TEMPLATE_INFO (expression
)
19619 && (any_dependent_template_arguments_p
19620 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
19623 if (TREE_CODE (expression
) == TEMPLATE_DECL
19624 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
19627 if (TREE_CODE (expression
) == STMT_EXPR
)
19628 expression
= stmt_expr_value_expr (expression
);
19630 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
19635 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
19637 if (type_dependent_expression_p (elt
))
19643 /* A static data member of the current instantiation with incomplete
19644 array type is type-dependent, as the definition and specializations
19645 can have different bounds. */
19646 if (TREE_CODE (expression
) == VAR_DECL
19647 && DECL_CLASS_SCOPE_P (expression
)
19648 && dependent_type_p (DECL_CONTEXT (expression
))
19649 && VAR_HAD_UNKNOWN_BOUND (expression
))
19652 if (TREE_TYPE (expression
) == unknown_type_node
)
19654 if (TREE_CODE (expression
) == ADDR_EXPR
)
19655 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
19656 if (TREE_CODE (expression
) == COMPONENT_REF
19657 || TREE_CODE (expression
) == OFFSET_REF
)
19659 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19661 expression
= TREE_OPERAND (expression
, 1);
19662 if (TREE_CODE (expression
) == IDENTIFIER_NODE
)
19665 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
19666 if (TREE_CODE (expression
) == SCOPE_REF
)
19669 if (BASELINK_P (expression
))
19670 expression
= BASELINK_FUNCTIONS (expression
);
19672 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
19674 if (any_dependent_template_arguments_p
19675 (TREE_OPERAND (expression
, 1)))
19677 expression
= TREE_OPERAND (expression
, 0);
19679 gcc_assert (TREE_CODE (expression
) == OVERLOAD
19680 || TREE_CODE (expression
) == FUNCTION_DECL
);
19684 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
19686 expression
= OVL_NEXT (expression
);
19691 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
19693 return (dependent_type_p (TREE_TYPE (expression
)));
19696 /* walk_tree callback function for instantiation_dependent_expression_p,
19697 below. Returns non-zero if a dependent subexpression is found. */
19700 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
19705 /* We don't have to worry about decltype currently because decltype
19706 of an instantiation-dependent expr is a dependent type. This
19707 might change depending on the resolution of DR 1172. */
19708 *walk_subtrees
= false;
19711 enum tree_code code
= TREE_CODE (*tp
);
19714 /* Don't treat an argument list as dependent just because it has no
19720 case TEMPLATE_PARM_INDEX
:
19723 /* Handle expressions with type operands. */
19727 case AT_ENCODE_EXPR
:
19730 tree op
= TREE_OPERAND (*tp
, 0);
19731 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
19732 op
= TREE_TYPE (op
);
19735 if (dependent_type_p (op
)
19736 || (code
== TRAIT_EXPR
19737 && dependent_type_p (TREE_OPERAND (*tp
, 1))))
19741 *walk_subtrees
= false;
19748 case COMPONENT_REF
:
19749 if (TREE_CODE (TREE_OPERAND (*tp
, 1)) == IDENTIFIER_NODE
)
19750 /* In a template, finish_class_member_access_expr creates a
19751 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
19752 type-dependent, so that we can check access control at
19753 instantiation time (PR 42277). See also Core issue 1273. */
19758 if (instantiation_dependent_scope_ref_p (*tp
))
19767 if (type_dependent_expression_p (*tp
))
19773 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
19774 sense defined by the ABI:
19776 "An expression is instantiation-dependent if it is type-dependent
19777 or value-dependent, or it has a subexpression that is type-dependent
19778 or value-dependent." */
19781 instantiation_dependent_expression_p (tree expression
)
19785 if (!processing_template_decl
)
19788 if (expression
== error_mark_node
)
19791 result
= cp_walk_tree_without_duplicates (&expression
,
19792 instantiation_dependent_r
, NULL
);
19793 return result
!= NULL_TREE
;
19796 /* Like type_dependent_expression_p, but it also works while not processing
19797 a template definition, i.e. during substitution or mangling. */
19800 type_dependent_expression_p_push (tree expr
)
19803 ++processing_template_decl
;
19804 b
= type_dependent_expression_p (expr
);
19805 --processing_template_decl
;
19809 /* Returns TRUE if ARGS contains a type-dependent expression. */
19812 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
19817 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
19819 if (type_dependent_expression_p (arg
))
19825 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
19826 expressions) contains any type-dependent expressions. */
19829 any_type_dependent_elements_p (const_tree list
)
19831 for (; list
; list
= TREE_CHAIN (list
))
19832 if (value_dependent_expression_p (TREE_VALUE (list
)))
19838 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
19839 expressions) contains any value-dependent expressions. */
19842 any_value_dependent_elements_p (const_tree list
)
19844 for (; list
; list
= TREE_CHAIN (list
))
19845 if (value_dependent_expression_p (TREE_VALUE (list
)))
19851 /* Returns TRUE if the ARG (a template argument) is dependent. */
19854 dependent_template_arg_p (tree arg
)
19856 if (!processing_template_decl
)
19859 /* Assume a template argument that was wrongly written by the user
19860 is dependent. This is consistent with what
19861 any_dependent_template_arguments_p [that calls this function]
19863 if (!arg
|| arg
== error_mark_node
)
19866 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
19867 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
19869 if (TREE_CODE (arg
) == TEMPLATE_DECL
19870 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
19871 return dependent_template_p (arg
);
19872 else if (ARGUMENT_PACK_P (arg
))
19874 tree args
= ARGUMENT_PACK_ARGS (arg
);
19875 int i
, len
= TREE_VEC_LENGTH (args
);
19876 for (i
= 0; i
< len
; ++i
)
19878 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
19884 else if (TYPE_P (arg
))
19885 return dependent_type_p (arg
);
19887 return (type_dependent_expression_p (arg
)
19888 || value_dependent_expression_p (arg
));
19891 /* Returns true if ARGS (a collection of template arguments) contains
19892 any types that require structural equality testing. */
19895 any_template_arguments_need_structural_equality_p (tree args
)
19902 if (args
== error_mark_node
)
19905 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
19907 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
19908 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
19910 tree arg
= TREE_VEC_ELT (level
, j
);
19911 tree packed_args
= NULL_TREE
;
19914 if (ARGUMENT_PACK_P (arg
))
19916 /* Look inside the argument pack. */
19917 packed_args
= ARGUMENT_PACK_ARGS (arg
);
19918 len
= TREE_VEC_LENGTH (packed_args
);
19921 for (k
= 0; k
< len
; ++k
)
19924 arg
= TREE_VEC_ELT (packed_args
, k
);
19926 if (error_operand_p (arg
))
19928 else if (TREE_CODE (arg
) == TEMPLATE_DECL
19929 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
19931 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
19933 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
19934 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
19943 /* Returns true if ARGS (a collection of template arguments) contains
19944 any dependent arguments. */
19947 any_dependent_template_arguments_p (const_tree args
)
19954 if (args
== error_mark_node
)
19957 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
19959 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
19960 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
19961 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
19968 /* Returns TRUE if the template TMPL is dependent. */
19971 dependent_template_p (tree tmpl
)
19973 if (TREE_CODE (tmpl
) == OVERLOAD
)
19977 if (dependent_template_p (OVL_CURRENT (tmpl
)))
19979 tmpl
= OVL_NEXT (tmpl
);
19984 /* Template template parameters are dependent. */
19985 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
19986 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
19988 /* So are names that have not been looked up. */
19989 if (TREE_CODE (tmpl
) == SCOPE_REF
19990 || TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
19992 /* So are member templates of dependent classes. */
19993 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
19994 return dependent_type_p (DECL_CONTEXT (tmpl
));
19998 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
20001 dependent_template_id_p (tree tmpl
, tree args
)
20003 return (dependent_template_p (tmpl
)
20004 || any_dependent_template_arguments_p (args
));
20007 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
20011 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
20015 if (!processing_template_decl
)
20018 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
20020 tree decl
= TREE_VEC_ELT (declv
, i
);
20021 tree init
= TREE_VEC_ELT (initv
, i
);
20022 tree cond
= TREE_VEC_ELT (condv
, i
);
20023 tree incr
= TREE_VEC_ELT (incrv
, i
);
20025 if (type_dependent_expression_p (decl
))
20028 if (init
&& type_dependent_expression_p (init
))
20031 if (type_dependent_expression_p (cond
))
20034 if (COMPARISON_CLASS_P (cond
)
20035 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
20036 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
20039 if (TREE_CODE (incr
) == MODOP_EXPR
)
20041 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
20042 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
20045 else if (type_dependent_expression_p (incr
))
20047 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
20049 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
20051 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
20053 tree t
= TREE_OPERAND (incr
, 1);
20054 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
20055 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
20064 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
20065 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
20066 no such TYPE can be found. Note that this function peers inside
20067 uninstantiated templates and therefore should be used only in
20068 extremely limited situations. ONLY_CURRENT_P restricts this
20069 peering to the currently open classes hierarchy (which is required
20070 when comparing types). */
20073 resolve_typename_type (tree type
, bool only_current_p
)
20082 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
20084 scope
= TYPE_CONTEXT (type
);
20085 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
20086 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
20087 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
20088 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
20089 identifier of the TYPENAME_TYPE anymore.
20090 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
20091 TYPENAME_TYPE instead, we avoid messing up with a possible
20092 typedef variant case. */
20093 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
20095 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
20096 it first before we can figure out what NAME refers to. */
20097 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
20099 if (TYPENAME_IS_RESOLVING_P (scope
))
20100 /* Given a class template A with a dependent base with nested type C,
20101 typedef typename A::C::C C will land us here, as trying to resolve
20102 the initial A::C leads to the local C typedef, which leads back to
20103 A::C::C. So we break the recursion now. */
20106 scope
= resolve_typename_type (scope
, only_current_p
);
20108 /* If we don't know what SCOPE refers to, then we cannot resolve the
20110 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
20112 /* If the SCOPE is a template type parameter, we have no way of
20113 resolving the name. */
20114 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
20116 /* If the SCOPE is not the current instantiation, there's no reason
20117 to look inside it. */
20118 if (only_current_p
&& !currently_open_class (scope
))
20120 /* If this is a typedef, we don't want to look inside (c++/11987). */
20121 if (typedef_variant_p (type
))
20123 /* If SCOPE isn't the template itself, it will not have a valid
20124 TYPE_FIELDS list. */
20125 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
20126 /* scope is either the template itself or a compatible instantiation
20127 like X<T>, so look up the name in the original template. */
20128 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
20130 /* scope is a partial instantiation, so we can't do the lookup or we
20131 will lose the template arguments. */
20133 /* Enter the SCOPE so that name lookup will be resolved as if we
20134 were in the class definition. In particular, SCOPE will no
20135 longer be considered a dependent type. */
20136 pushed_scope
= push_scope (scope
);
20137 /* Look up the declaration. */
20138 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
20139 tf_warning_or_error
);
20141 result
= NULL_TREE
;
20143 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
20144 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
20147 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == IDENTIFIER_NODE
20148 && TREE_CODE (decl
) == TYPE_DECL
)
20150 result
= TREE_TYPE (decl
);
20151 if (result
== error_mark_node
)
20152 result
= NULL_TREE
;
20154 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
20155 && DECL_CLASS_TEMPLATE_P (decl
))
20159 /* Obtain the template and the arguments. */
20160 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
20161 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
20162 /* Instantiate the template. */
20163 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
20164 /*entering_scope=*/0,
20165 tf_error
| tf_user
);
20166 if (result
== error_mark_node
)
20167 result
= NULL_TREE
;
20170 /* Leave the SCOPE. */
20172 pop_scope (pushed_scope
);
20174 /* If we failed to resolve it, return the original typename. */
20178 /* If lookup found a typename type, resolve that too. */
20179 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
20181 /* Ill-formed programs can cause infinite recursion here, so we
20182 must catch that. */
20183 TYPENAME_IS_RESOLVING_P (type
) = 1;
20184 result
= resolve_typename_type (result
, only_current_p
);
20185 TYPENAME_IS_RESOLVING_P (type
) = 0;
20188 /* Qualify the resulting type. */
20189 quals
= cp_type_quals (type
);
20191 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
20196 /* EXPR is an expression which is not type-dependent. Return a proxy
20197 for EXPR that can be used to compute the types of larger
20198 expressions containing EXPR. */
20201 build_non_dependent_expr (tree expr
)
20205 #ifdef ENABLE_CHECKING
20206 /* Try to get a constant value for all non-type-dependent expressions in
20207 order to expose bugs in *_dependent_expression_p and constexpr. */
20208 if (cxx_dialect
>= cxx0x
)
20209 maybe_constant_value (fold_non_dependent_expr_sfinae (expr
, tf_none
));
20212 /* Preserve OVERLOADs; the functions must be available to resolve
20215 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
20216 inner_expr
= stmt_expr_value_expr (inner_expr
);
20217 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
20218 inner_expr
= TREE_OPERAND (inner_expr
, 0);
20219 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
20220 inner_expr
= TREE_OPERAND (inner_expr
, 1);
20221 if (is_overloaded_fn (inner_expr
)
20222 || TREE_CODE (inner_expr
) == OFFSET_REF
)
20224 /* There is no need to return a proxy for a variable. */
20225 if (TREE_CODE (expr
) == VAR_DECL
)
20227 /* Preserve string constants; conversions from string constants to
20228 "char *" are allowed, even though normally a "const char *"
20229 cannot be used to initialize a "char *". */
20230 if (TREE_CODE (expr
) == STRING_CST
)
20232 /* Preserve arithmetic constants, as an optimization -- there is no
20233 reason to create a new node. */
20234 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
20236 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
20237 There is at least one place where we want to know that a
20238 particular expression is a throw-expression: when checking a ?:
20239 expression, there are special rules if the second or third
20240 argument is a throw-expression. */
20241 if (TREE_CODE (expr
) == THROW_EXPR
)
20244 /* Don't wrap an initializer list, we need to be able to look inside. */
20245 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
20248 /* Don't wrap a dummy object, we need to be able to test for it. */
20249 if (is_dummy_object (expr
))
20252 if (TREE_CODE (expr
) == COND_EXPR
)
20253 return build3 (COND_EXPR
,
20255 TREE_OPERAND (expr
, 0),
20256 (TREE_OPERAND (expr
, 1)
20257 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
20258 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
20259 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
20260 if (TREE_CODE (expr
) == COMPOUND_EXPR
20261 && !COMPOUND_EXPR_OVERLOADED (expr
))
20262 return build2 (COMPOUND_EXPR
,
20264 TREE_OPERAND (expr
, 0),
20265 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
20267 /* If the type is unknown, it can't really be non-dependent */
20268 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
20270 /* Otherwise, build a NON_DEPENDENT_EXPR. */
20271 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
20274 /* ARGS is a vector of expressions as arguments to a function call.
20275 Replace the arguments with equivalent non-dependent expressions.
20276 This modifies ARGS in place. */
20279 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
20284 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
20286 tree newarg
= build_non_dependent_expr (arg
);
20288 (*args
)[ix
] = newarg
;
20292 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
20293 with a level one deeper than the actual template parms. */
20298 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
20299 TYPE_NAME (au
) = build_decl (BUILTINS_LOCATION
,
20300 TYPE_DECL
, get_identifier ("auto"), au
);
20301 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
20302 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
20303 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
20304 TYPE_NAME (au
), NULL_TREE
);
20305 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
20306 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
20307 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
20312 /* Given type ARG, return std::initializer_list<ARG>. */
20317 tree std_init_list
= namespace_binding
20318 (get_identifier ("initializer_list"), std_node
);
20320 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
20322 error ("deducing from brace-enclosed initializer list requires "
20323 "#include <initializer_list>");
20324 return error_mark_node
;
20326 argvec
= make_tree_vec (1);
20327 TREE_VEC_ELT (argvec
, 0) = arg
;
20328 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
20329 NULL_TREE
, 0, tf_warning_or_error
);
20332 /* Replace auto in TYPE with std::initializer_list<auto>. */
20335 listify_autos (tree type
, tree auto_node
)
20337 tree init_auto
= listify (auto_node
);
20338 tree argvec
= make_tree_vec (1);
20339 TREE_VEC_ELT (argvec
, 0) = init_auto
;
20340 if (processing_template_decl
)
20341 argvec
= add_to_template_args (current_template_args (), argvec
);
20342 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
20345 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
20346 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
20349 do_auto_deduction (tree type
, tree init
, tree auto_node
)
20351 tree parms
, tparms
, targs
;
20355 if (init
== error_mark_node
)
20356 return error_mark_node
;
20358 if (type_dependent_expression_p (init
))
20359 /* Defining a subset of type-dependent expressions that we can deduce
20360 from ahead of time isn't worth the trouble. */
20363 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
20364 with either a new invented type template parameter U or, if the
20365 initializer is a braced-init-list (8.5.4), with
20366 std::initializer_list<U>. */
20367 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
20368 type
= listify_autos (type
, auto_node
);
20370 init
= resolve_nondeduced_context (init
);
20372 parms
= build_tree_list (NULL_TREE
, type
);
20374 tparms
= make_tree_vec (1);
20375 targs
= make_tree_vec (1);
20376 TREE_VEC_ELT (tparms
, 0)
20377 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
20378 val
= type_unification_real (tparms
, targs
, parms
, args
, 1, 0,
20379 DEDUCE_CALL
, LOOKUP_NORMAL
,
20380 /*explain_p=*/false);
20383 if (processing_template_decl
)
20384 /* Try again at instantiation time. */
20386 if (type
&& type
!= error_mark_node
)
20387 /* If type is error_mark_node a diagnostic must have been
20388 emitted by now. Also, having a mention to '<type error>'
20389 in the diagnostic is not really useful to the user. */
20391 if (cfun
&& auto_node
== current_function_auto_return_pattern
20392 && LAMBDA_FUNCTION_P (current_function_decl
))
20393 error ("unable to deduce lambda return type from %qE", init
);
20395 error ("unable to deduce %qT from %qE", type
, init
);
20397 return error_mark_node
;
20400 /* If the list of declarators contains more than one declarator, the type
20401 of each declared variable is determined as described above. If the
20402 type deduced for the template parameter U is not the same in each
20403 deduction, the program is ill-formed. */
20404 if (TREE_TYPE (auto_node
)
20405 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
20407 if (cfun
&& auto_node
== current_function_auto_return_pattern
20408 && LAMBDA_FUNCTION_P (current_function_decl
))
20409 error ("inconsistent types %qT and %qT deduced for "
20410 "lambda return type", TREE_TYPE (auto_node
),
20411 TREE_VEC_ELT (targs
, 0));
20413 error ("inconsistent deduction for %qT: %qT and then %qT",
20414 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
20415 return error_mark_node
;
20417 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
20419 if (processing_template_decl
)
20420 targs
= add_to_template_args (current_template_args (), targs
);
20421 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
20424 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
20428 splice_late_return_type (tree type
, tree late_return_type
)
20432 if (late_return_type
== NULL_TREE
)
20434 argvec
= make_tree_vec (1);
20435 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
20436 if (processing_template_parmlist
)
20437 /* For a late-specified return type in a template type-parameter, we
20438 need to add a dummy argument level for its parmlist. */
20439 argvec
= add_to_template_args
20440 (make_tree_vec (processing_template_parmlist
), argvec
);
20441 if (current_template_parms
)
20442 argvec
= add_to_template_args (current_template_args (), argvec
);
20443 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
20446 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
20449 is_auto (const_tree type
)
20451 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20452 && TYPE_IDENTIFIER (type
) == get_identifier ("auto"))
20458 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
20459 appear as a type-specifier for the declaration in question, we don't
20460 have to look through the whole type. */
20463 type_uses_auto (tree type
)
20465 enum tree_code code
;
20466 if (is_auto (type
))
20469 code
= TREE_CODE (type
);
20471 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
20472 || code
== OFFSET_TYPE
|| code
== FUNCTION_TYPE
20473 || code
== METHOD_TYPE
|| code
== ARRAY_TYPE
)
20474 return type_uses_auto (TREE_TYPE (type
));
20476 if (TYPE_PTRMEMFUNC_P (type
))
20477 return type_uses_auto (TREE_TYPE (TREE_TYPE
20478 (TYPE_PTRMEMFUNC_FN_TYPE (type
))));
20483 /* For a given template T, return the vector of typedefs referenced
20484 in T for which access check is needed at T instantiation time.
20485 T is either a FUNCTION_DECL or a RECORD_TYPE.
20486 Those typedefs were added to T by the function
20487 append_type_to_template_for_access_check. */
20489 vec
<qualified_typedef_usage_t
, va_gc
> *
20490 get_types_needing_access_check (tree t
)
20493 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
20495 if (!t
|| t
== error_mark_node
)
20498 if (!(ti
= get_template_info (t
)))
20501 if (CLASS_TYPE_P (t
)
20502 || TREE_CODE (t
) == FUNCTION_DECL
)
20504 if (!TI_TEMPLATE (ti
))
20507 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
20513 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
20514 tied to T. That list of typedefs will be access checked at
20515 T instantiation time.
20516 T is either a FUNCTION_DECL or a RECORD_TYPE.
20517 TYPE_DECL is a TYPE_DECL node representing a typedef.
20518 SCOPE is the scope through which TYPE_DECL is accessed.
20519 LOCATION is the location of the usage point of TYPE_DECL.
20521 This function is a subroutine of
20522 append_type_to_template_for_access_check. */
20525 append_type_to_template_for_access_check_1 (tree t
,
20528 location_t location
)
20530 qualified_typedef_usage_t typedef_usage
;
20533 if (!t
|| t
== error_mark_node
)
20536 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
20537 || CLASS_TYPE_P (t
))
20539 && TREE_CODE (type_decl
) == TYPE_DECL
20542 if (!(ti
= get_template_info (t
)))
20545 gcc_assert (TI_TEMPLATE (ti
));
20547 typedef_usage
.typedef_decl
= type_decl
;
20548 typedef_usage
.context
= scope
;
20549 typedef_usage
.locus
= location
;
20551 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
20554 /* Append TYPE_DECL to the template TEMPL.
20555 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
20556 At TEMPL instanciation time, TYPE_DECL will be checked to see
20557 if it can be accessed through SCOPE.
20558 LOCATION is the location of the usage point of TYPE_DECL.
20560 e.g. consider the following code snippet:
20567 template<class U> struct S
20569 C::myint mi; // <-- usage point of the typedef C::myint
20574 At S<char> instantiation time, we need to check the access of C::myint
20575 In other words, we need to check the access of the myint typedef through
20576 the C scope. For that purpose, this function will add the myint typedef
20577 and the scope C through which its being accessed to a list of typedefs
20578 tied to the template S. That list will be walked at template instantiation
20579 time and access check performed on each typedefs it contains.
20580 Note that this particular code snippet should yield an error because
20581 myint is private to C. */
20584 append_type_to_template_for_access_check (tree templ
,
20587 location_t location
)
20589 qualified_typedef_usage_t
*iter
;
20592 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
20594 /* Make sure we don't append the type to the template twice. */
20595 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
20596 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
20599 append_type_to_template_for_access_check_1 (templ
, type_decl
,
20603 /* Set up the hash tables for template instantiations. */
20606 init_template_processing (void)
20608 decl_specializations
= htab_create_ggc (37,
20609 hash_specialization
,
20610 eq_specializations
,
20612 type_specializations
= htab_create_ggc (37,
20613 hash_specialization
,
20614 eq_specializations
,
20618 /* Print stats about the template hash tables for -fstats. */
20621 print_template_statistics (void)
20623 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
20624 "%f collisions\n", (long) htab_size (decl_specializations
),
20625 (long) htab_elements (decl_specializations
),
20626 htab_collisions (decl_specializations
));
20627 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
20628 "%f collisions\n", (long) htab_size (type_specializations
),
20629 (long) htab_elements (type_specializations
),
20630 htab_collisions (type_specializations
));
20633 #include "gt-cp-pt.h"