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
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"
36 #include "pointer-set.h"
40 #include "cp-objcp-common.h"
41 #include "tree-inline.h"
48 #include "tree-iterator.h"
51 /* The type of functions taking a tree, and some additional data, and
53 typedef int (*tree_fn_t
) (tree
, void*);
55 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
56 instantiations have been deferred, either because their definitions
57 were not yet available, or because we were putting off doing the work. */
58 struct GTY (()) pending_template
{
59 struct pending_template
*next
;
60 struct tinst_level
*tinst
;
63 static GTY(()) struct pending_template
*pending_templates
;
64 static GTY(()) struct pending_template
*last_pending_template
;
66 int processing_template_parmlist
;
67 static int template_header_count
;
69 static GTY(()) tree saved_trees
;
70 static VEC(int,heap
) *inline_parm_levels
;
72 static GTY(()) struct tinst_level
*current_tinst_level
;
74 static GTY(()) tree saved_access_scope
;
76 /* Live only within one (recursive) call to tsubst_expr. We use
77 this to pass the statement expression node from the STMT_EXPR
78 to the EXPR_STMT that is its result. */
79 static tree cur_stmt_expr
;
81 /* A map from local variable declarations in the body of the template
82 presently being instantiated to the corresponding instantiated
84 static htab_t local_specializations
;
86 typedef struct GTY(()) spec_entry
93 static GTY ((param_is (spec_entry
)))
94 htab_t decl_specializations
;
96 static GTY ((param_is (spec_entry
)))
97 htab_t type_specializations
;
99 /* Contains canonical template parameter types. The vector is indexed by
100 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
101 TREE_LIST, whose TREE_VALUEs contain the canonical template
102 parameters of various types and levels. */
103 static GTY(()) VEC(tree
,gc
) *canonical_template_parms
;
105 #define UNIFY_ALLOW_NONE 0
106 #define UNIFY_ALLOW_MORE_CV_QUAL 1
107 #define UNIFY_ALLOW_LESS_CV_QUAL 2
108 #define UNIFY_ALLOW_DERIVED 4
109 #define UNIFY_ALLOW_INTEGER 8
110 #define UNIFY_ALLOW_OUTER_LEVEL 16
111 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
112 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
114 static void push_access_scope (tree
);
115 static void pop_access_scope (tree
);
116 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
117 unification_kind_t
, int);
118 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
119 unification_kind_t
, int, bool);
120 static int unify (tree
, tree
, tree
, tree
, int);
121 static void add_pending_template (tree
);
122 static int push_tinst_level (tree
);
123 static void pop_tinst_level (void);
124 static tree
reopen_tinst_level (struct tinst_level
*);
125 static tree
tsubst_initializer_list (tree
, tree
);
126 static tree
get_class_bindings (tree
, tree
, tree
);
127 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
129 static void tsubst_enum (tree
, tree
, tree
);
130 static tree
add_to_template_args (tree
, tree
);
131 static tree
add_outermost_template_args (tree
, tree
);
132 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
133 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
135 static int type_unification_real (tree
, tree
, tree
, const tree
*,
136 unsigned int, int, unification_kind_t
, int);
137 static void note_template_header (int);
138 static tree
convert_nontype_argument_function (tree
, tree
);
139 static tree
convert_nontype_argument (tree
, tree
);
140 static tree
convert_template_argument (tree
, tree
, tree
,
141 tsubst_flags_t
, int, tree
);
142 static int for_each_template_parm (tree
, tree_fn_t
, void*,
143 struct pointer_set_t
*, bool);
144 static tree
expand_template_argument_pack (tree
);
145 static tree
build_template_parm_index (int, int, int, tree
, tree
);
146 static bool inline_needs_template_parms (tree
);
147 static void push_inline_template_parms_recursive (tree
, int);
148 static tree
retrieve_local_specialization (tree
);
149 static void register_local_specialization (tree
, tree
);
150 static hashval_t
hash_specialization (const void *p
);
151 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
152 static int mark_template_parm (tree
, void *);
153 static int template_parm_this_level_p (tree
, void *);
154 static tree
tsubst_friend_function (tree
, tree
);
155 static tree
tsubst_friend_class (tree
, tree
);
156 static int can_complete_type_without_circularity (tree
);
157 static tree
get_bindings (tree
, tree
, tree
, bool);
158 static int template_decl_level (tree
);
159 static int check_cv_quals_for_unify (int, tree
, tree
);
160 static void template_parm_level_and_index (tree
, int*, int*);
161 static int unify_pack_expansion (tree
, tree
, tree
, tree
, int, bool, bool);
162 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
163 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
164 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
165 static void regenerate_decl_from_template (tree
, tree
);
166 static tree
most_specialized_class (tree
, tree
);
167 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
168 static tree
tsubst_arg_types (tree
, tree
, tsubst_flags_t
, tree
);
169 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
170 static bool check_specialization_scope (void);
171 static tree
process_partial_specialization (tree
);
172 static void set_current_access_from_decl (tree
);
173 static tree
get_template_base (tree
, tree
, tree
, tree
);
174 static tree
try_class_unification (tree
, tree
, tree
, tree
);
175 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
177 static bool template_template_parm_bindings_ok_p (tree
, tree
);
178 static int template_args_equal (tree
, tree
);
179 static void tsubst_default_arguments (tree
);
180 static tree
for_each_template_parm_r (tree
*, int *, void *);
181 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
182 static void copy_default_args_to_explicit_spec (tree
);
183 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
184 static int eq_local_specializations (const void *, const void *);
185 static bool dependent_template_arg_p (tree
);
186 static bool any_template_arguments_need_structural_equality_p (tree
);
187 static bool dependent_type_p_r (tree
);
188 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
, bool);
189 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
190 static tree
tsubst_pack_expansion (tree
, tree
, tsubst_flags_t
, tree
);
191 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
192 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
193 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
195 static hashval_t
iterative_hash_template_arg (tree arg
, hashval_t val
);
196 static tree
listify (tree
);
197 static tree
listify_autos (tree
, tree
);
199 /* Make the current scope suitable for access checking when we are
200 processing T. T can be FUNCTION_DECL for instantiated function
201 template, or VAR_DECL for static member variable (need by
202 instantiate_decl). */
205 push_access_scope (tree t
)
207 gcc_assert (TREE_CODE (t
) == FUNCTION_DECL
208 || TREE_CODE (t
) == VAR_DECL
);
210 if (DECL_FRIEND_CONTEXT (t
))
211 push_nested_class (DECL_FRIEND_CONTEXT (t
));
212 else if (DECL_CLASS_SCOPE_P (t
))
213 push_nested_class (DECL_CONTEXT (t
));
215 push_to_top_level ();
217 if (TREE_CODE (t
) == FUNCTION_DECL
)
219 saved_access_scope
= tree_cons
220 (NULL_TREE
, current_function_decl
, saved_access_scope
);
221 current_function_decl
= t
;
225 /* Restore the scope set up by push_access_scope. T is the node we
229 pop_access_scope (tree t
)
231 if (TREE_CODE (t
) == FUNCTION_DECL
)
233 current_function_decl
= TREE_VALUE (saved_access_scope
);
234 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
237 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
240 pop_from_top_level ();
243 /* Do any processing required when DECL (a member template
244 declaration) is finished. Returns the TEMPLATE_DECL corresponding
245 to DECL, unless it is a specialization, in which case the DECL
246 itself is returned. */
249 finish_member_template_decl (tree decl
)
251 if (decl
== error_mark_node
)
252 return error_mark_node
;
254 gcc_assert (DECL_P (decl
));
256 if (TREE_CODE (decl
) == TYPE_DECL
)
260 type
= TREE_TYPE (decl
);
261 if (type
== error_mark_node
)
262 return error_mark_node
;
263 if (MAYBE_CLASS_TYPE_P (type
)
264 && CLASSTYPE_TEMPLATE_INFO (type
)
265 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
267 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
268 check_member_template (tmpl
);
273 else if (TREE_CODE (decl
) == FIELD_DECL
)
274 error ("data member %qD cannot be a member template", decl
);
275 else if (DECL_TEMPLATE_INFO (decl
))
277 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
279 check_member_template (DECL_TI_TEMPLATE (decl
));
280 return DECL_TI_TEMPLATE (decl
);
286 error ("invalid member template declaration %qD", decl
);
288 return error_mark_node
;
291 /* Create a template info node. */
294 build_template_info (tree template_decl
, tree template_args
)
296 tree result
= make_node (TEMPLATE_INFO
);
297 TI_TEMPLATE (result
) = template_decl
;
298 TI_ARGS (result
) = template_args
;
302 /* Return the template info node corresponding to T, whatever T is. */
305 get_template_info (const_tree t
)
307 tree tinfo
= NULL_TREE
;
309 if (!t
|| t
== error_mark_node
)
312 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
313 tinfo
= DECL_TEMPLATE_INFO (t
);
315 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
318 if (TAGGED_TYPE_P (t
))
319 tinfo
= TYPE_TEMPLATE_INFO (t
);
320 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
321 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
326 /* Returns the template nesting level of the indicated class TYPE.
336 A<T>::B<U> has depth two, while A<T> has depth one.
337 Both A<T>::B<int> and A<int>::B<U> have depth one, if
338 they are instantiations, not specializations.
340 This function is guaranteed to return 0 if passed NULL_TREE so
341 that, for example, `template_class_depth (current_class_type)' is
345 template_class_depth (tree type
)
350 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
351 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
352 ? CP_DECL_CONTEXT (type
) : TYPE_CONTEXT (type
))
354 tree tinfo
= get_template_info (type
);
356 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
357 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
364 /* Subroutine of maybe_begin_member_template_processing.
365 Returns true if processing DECL needs us to push template parms. */
368 inline_needs_template_parms (tree decl
)
370 if (! DECL_TEMPLATE_INFO (decl
))
373 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
374 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
377 /* Subroutine of maybe_begin_member_template_processing.
378 Push the template parms in PARMS, starting from LEVELS steps into the
379 chain, and ending at the beginning, since template parms are listed
383 push_inline_template_parms_recursive (tree parmlist
, int levels
)
385 tree parms
= TREE_VALUE (parmlist
);
389 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
391 ++processing_template_decl
;
392 current_template_parms
393 = tree_cons (size_int (processing_template_decl
),
394 parms
, current_template_parms
);
395 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
397 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
399 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
401 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
403 if (parm
== error_mark_node
)
406 gcc_assert (DECL_P (parm
));
408 switch (TREE_CODE (parm
))
417 /* Make a CONST_DECL as is done in process_template_parm.
418 It is ugly that we recreate this here; the original
419 version built in process_template_parm is no longer
421 tree decl
= build_decl (DECL_SOURCE_LOCATION (parm
),
422 CONST_DECL
, DECL_NAME (parm
),
424 DECL_ARTIFICIAL (decl
) = 1;
425 TREE_CONSTANT (decl
) = 1;
426 TREE_READONLY (decl
) = 1;
427 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
428 SET_DECL_TEMPLATE_PARM_P (decl
);
439 /* Restore the template parameter context for a member template or
440 a friend template defined in a class definition. */
443 maybe_begin_member_template_processing (tree decl
)
448 if (inline_needs_template_parms (decl
))
450 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
451 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
453 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
456 parms
= TREE_CHAIN (parms
);
459 push_inline_template_parms_recursive (parms
, levels
);
462 /* Remember how many levels of template parameters we pushed so that
463 we can pop them later. */
464 VEC_safe_push (int, heap
, inline_parm_levels
, levels
);
467 /* Undo the effects of maybe_begin_member_template_processing. */
470 maybe_end_member_template_processing (void)
475 if (VEC_length (int, inline_parm_levels
) == 0)
478 last
= VEC_pop (int, inline_parm_levels
);
479 for (i
= 0; i
< last
; ++i
)
481 --processing_template_decl
;
482 current_template_parms
= TREE_CHAIN (current_template_parms
);
487 /* Return a new template argument vector which contains all of ARGS,
488 but has as its innermost set of arguments the EXTRA_ARGS. */
491 add_to_template_args (tree args
, tree extra_args
)
498 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
499 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
501 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
502 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
504 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
505 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
510 /* Like add_to_template_args, but only the outermost ARGS are added to
511 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
512 (EXTRA_ARGS) levels are added. This function is used to combine
513 the template arguments from a partial instantiation with the
514 template arguments used to attain the full instantiation from the
515 partial instantiation. */
518 add_outermost_template_args (tree args
, tree extra_args
)
522 /* If there are more levels of EXTRA_ARGS than there are ARGS,
523 something very fishy is going on. */
524 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
526 /* If *all* the new arguments will be the EXTRA_ARGS, just return
528 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
531 /* For the moment, we make ARGS look like it contains fewer levels. */
532 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
534 new_args
= add_to_template_args (args
, extra_args
);
536 /* Now, we restore ARGS to its full dimensions. */
537 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
542 /* Return the N levels of innermost template arguments from the ARGS. */
545 get_innermost_template_args (tree args
, int n
)
553 /* If N is 1, just return the innermost set of template arguments. */
555 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
557 /* If we're not removing anything, just return the arguments we were
559 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
560 gcc_assert (extra_levels
>= 0);
561 if (extra_levels
== 0)
564 /* Make a new set of arguments, not containing the outer arguments. */
565 new_args
= make_tree_vec (n
);
566 for (i
= 1; i
<= n
; ++i
)
567 SET_TMPL_ARGS_LEVEL (new_args
, i
,
568 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
573 /* The inverse of get_innermost_template_args: Return all but the innermost
574 EXTRA_LEVELS levels of template arguments from the ARGS. */
577 strip_innermost_template_args (tree args
, int extra_levels
)
580 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
585 /* If N is 1, just return the outermost set of template arguments. */
587 return TMPL_ARGS_LEVEL (args
, 1);
589 /* If we're not removing anything, just return the arguments we were
591 gcc_assert (extra_levels
>= 0);
592 if (extra_levels
== 0)
595 /* Make a new set of arguments, not containing the inner arguments. */
596 new_args
= make_tree_vec (n
);
597 for (i
= 1; i
<= n
; ++i
)
598 SET_TMPL_ARGS_LEVEL (new_args
, i
,
599 TMPL_ARGS_LEVEL (args
, i
));
604 /* We've got a template header coming up; push to a new level for storing
608 begin_template_parm_list (void)
610 /* We use a non-tag-transparent scope here, which causes pushtag to
611 put tags in this scope, rather than in the enclosing class or
612 namespace scope. This is the right thing, since we want
613 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
614 global template class, push_template_decl handles putting the
615 TEMPLATE_DECL into top-level scope. For a nested template class,
618 template <class T> struct S1 {
619 template <class T> struct S2 {};
622 pushtag contains special code to call pushdecl_with_scope on the
623 TEMPLATE_DECL for S2. */
624 begin_scope (sk_template_parms
, NULL
);
625 ++processing_template_decl
;
626 ++processing_template_parmlist
;
627 note_template_header (0);
630 /* This routine is called when a specialization is declared. If it is
631 invalid to declare a specialization here, an error is reported and
632 false is returned, otherwise this routine will return true. */
635 check_specialization_scope (void)
637 tree scope
= current_scope ();
641 An explicit specialization shall be declared in the namespace of
642 which the template is a member, or, for member templates, in the
643 namespace of which the enclosing class or enclosing class
644 template is a member. An explicit specialization of a member
645 function, member class or static data member of a class template
646 shall be declared in the namespace of which the class template
648 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
650 error ("explicit specialization in non-namespace scope %qD", scope
);
656 In an explicit specialization declaration for a member of a class
657 template or a member template that appears in namespace scope,
658 the member template and some of its enclosing class templates may
659 remain unspecialized, except that the declaration shall not
660 explicitly specialize a class member template if its enclosing
661 class templates are not explicitly specialized as well. */
662 if (current_template_parms
)
664 error ("enclosing class templates are not explicitly specialized");
671 /* We've just seen template <>. */
674 begin_specialization (void)
676 begin_scope (sk_template_spec
, NULL
);
677 note_template_header (1);
678 return check_specialization_scope ();
681 /* Called at then end of processing a declaration preceded by
685 end_specialization (void)
688 reset_specialization ();
691 /* Any template <>'s that we have seen thus far are not referring to a
692 function specialization. */
695 reset_specialization (void)
697 processing_specialization
= 0;
698 template_header_count
= 0;
701 /* We've just seen a template header. If SPECIALIZATION is nonzero,
702 it was of the form template <>. */
705 note_template_header (int specialization
)
707 processing_specialization
= specialization
;
708 template_header_count
++;
711 /* We're beginning an explicit instantiation. */
714 begin_explicit_instantiation (void)
716 gcc_assert (!processing_explicit_instantiation
);
717 processing_explicit_instantiation
= true;
722 end_explicit_instantiation (void)
724 gcc_assert (processing_explicit_instantiation
);
725 processing_explicit_instantiation
= false;
728 /* An explicit specialization or partial specialization TMPL is being
729 declared. Check that the namespace in which the specialization is
730 occurring is permissible. Returns false iff it is invalid to
731 specialize TMPL in the current namespace. */
734 check_specialization_namespace (tree tmpl
)
736 tree tpl_ns
= decl_namespace_context (tmpl
);
740 An explicit specialization shall be declared in the namespace of
741 which the template is a member, or, for member templates, in the
742 namespace of which the enclosing class or enclosing class
743 template is a member. An explicit specialization of a member
744 function, member class or static data member of a class template
745 shall be declared in the namespace of which the class template is
747 if (current_scope() != DECL_CONTEXT (tmpl
)
748 && !at_namespace_scope_p ())
750 error ("specialization of %qD must appear at namespace scope", tmpl
);
753 if (is_associated_namespace (current_namespace
, tpl_ns
))
754 /* Same or super-using namespace. */
758 permerror (input_location
, "specialization of %qD in different namespace", tmpl
);
759 permerror (input_location
, " from definition of %q+#D", tmpl
);
764 /* SPEC is an explicit instantiation. Check that it is valid to
765 perform this explicit instantiation in the current namespace. */
768 check_explicit_instantiation_namespace (tree spec
)
772 /* DR 275: An explicit instantiation shall appear in an enclosing
773 namespace of its template. */
774 ns
= decl_namespace_context (spec
);
775 if (!is_ancestor (current_namespace
, ns
))
776 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
777 "(which does not enclose namespace %qD)",
778 spec
, current_namespace
, ns
);
781 /* The TYPE is being declared. If it is a template type, that means it
782 is a partial specialization. Do appropriate error-checking. */
785 maybe_process_partial_specialization (tree type
)
789 if (type
== error_mark_node
)
790 return error_mark_node
;
792 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
794 error ("name of class shadows template template parameter %qD",
796 return error_mark_node
;
799 context
= TYPE_CONTEXT (type
);
801 if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
803 /* This is for ordinary explicit specialization and partial
804 specialization of a template class such as:
806 template <> class C<int>;
810 template <class T> class C<T*>;
812 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
814 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
815 && !COMPLETE_TYPE_P (type
))
817 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
));
818 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
819 if (processing_template_decl
)
821 if (push_template_decl (TYPE_MAIN_DECL (type
))
823 return error_mark_node
;
826 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
827 error ("specialization of %qT after instantiation", type
);
829 else if (CLASS_TYPE_P (type
)
830 && !CLASSTYPE_USE_TEMPLATE (type
)
831 && CLASSTYPE_TEMPLATE_INFO (type
)
832 && context
&& CLASS_TYPE_P (context
)
833 && CLASSTYPE_TEMPLATE_INFO (context
))
835 /* This is for an explicit specialization of member class
836 template according to [temp.expl.spec/18]:
838 template <> template <class U> class C<int>::D;
840 The context `C<int>' must be an implicit instantiation.
841 Otherwise this is just a member class template declared
844 template <> class C<int> { template <class U> class D; };
845 template <> template <class U> class C<int>::D;
847 In the first case, `C<int>::D' is a specialization of `C<T>::D'
848 while in the second case, `C<int>::D' is a primary template
849 and `C<T>::D' may not exist. */
851 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
852 && !COMPLETE_TYPE_P (type
))
855 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
857 if (current_namespace
858 != decl_namespace_context (tmpl
))
860 permerror (input_location
, "specializing %q#T in different namespace", type
);
861 permerror (input_location
, " from definition of %q+#D", tmpl
);
864 /* Check for invalid specialization after instantiation:
866 template <> template <> class C<int>::D<int>;
867 template <> template <class U> class C<int>::D; */
869 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
870 t
; t
= TREE_CHAIN (t
))
872 tree inst
= TREE_VALUE (t
);
873 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
))
875 /* We already have a full specialization of this partial
876 instantiation. Reassign it to the new member
877 specialization template. */
881 elt
.tmpl
= most_general_template (tmpl
);
882 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
885 htab_remove_elt (type_specializations
, &elt
);
888 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
890 slot
= (spec_entry
**)
891 htab_find_slot (type_specializations
, &elt
, INSERT
);
892 *slot
= GGC_NEW (spec_entry
);
895 else if (COMPLETE_TYPE_P (inst
) || TYPE_BEING_DEFINED (inst
))
896 /* But if we've had an implicit instantiation, that's a
897 problem ([temp.expl.spec]/6). */
898 error ("specialization %qT after instantiation %qT",
902 /* Mark TYPE as a specialization. And as a result, we only
903 have one level of template argument for the innermost
905 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
906 CLASSTYPE_TI_ARGS (type
)
907 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
910 else if (processing_specialization
)
912 error ("explicit specialization of non-template %qT", type
);
913 return error_mark_node
;
919 /* Returns nonzero if we can optimize the retrieval of specializations
920 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
921 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
924 optimize_specialization_lookup_p (tree tmpl
)
926 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
927 && DECL_CLASS_SCOPE_P (tmpl
)
928 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
930 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
931 /* The optimized lookup depends on the fact that the
932 template arguments for the member function template apply
933 purely to the containing class, which is not true if the
934 containing class is an explicit or partial
936 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
937 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
938 && !DECL_CONV_FN_P (tmpl
)
939 /* It is possible to have a template that is not a member
940 template and is not a member of a template class:
942 template <typename T>
943 struct S { friend A::f(); };
945 Here, the friend function is a template, but the context does
946 not have template information. The optimized lookup relies
947 on having ARGS be the template arguments for both the class
948 and the function template. */
949 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
952 /* Retrieve the specialization (in the sense of [temp.spec] - a
953 specialization is either an instantiation or an explicit
954 specialization) of TMPL for the given template ARGS. If there is
955 no such specialization, return NULL_TREE. The ARGS are a vector of
956 arguments, or a vector of vectors of arguments, in the case of
957 templates with more than one level of parameters.
959 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
960 then we search for a partial specialization matching ARGS. This
961 parameter is ignored if TMPL is not a class template. */
964 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
966 if (args
== error_mark_node
)
969 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
971 /* There should be as many levels of arguments as there are
972 levels of parameters. */
973 gcc_assert (TMPL_ARGS_DEPTH (args
)
974 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
976 if (optimize_specialization_lookup_p (tmpl
))
979 tree class_specialization
;
980 VEC(tree
,gc
) *methods
;
984 /* The template arguments actually apply to the containing
985 class. Find the class specialization with those
987 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
989 = retrieve_specialization (class_template
, args
, 0);
990 if (!class_specialization
)
992 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
993 for the specialization. */
994 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
997 /* Iterate through the methods with the indicated name, looking
998 for the one that has an instance of TMPL. */
999 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1000 for (fns
= VEC_index (tree
, methods
, idx
); fns
; fns
= OVL_NEXT (fns
))
1002 tree fn
= OVL_CURRENT (fns
);
1003 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1004 /* using-declarations can add base methods to the method vec,
1005 and we don't want those here. */
1006 && DECL_CONTEXT (fn
) == class_specialization
)
1015 htab_t specializations
;
1019 elt
.spec
= NULL_TREE
;
1021 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1022 specializations
= type_specializations
;
1024 specializations
= decl_specializations
;
1027 hash
= hash_specialization (&elt
);
1028 found
= (spec_entry
*) htab_find_with_hash (specializations
, &elt
, hash
);
1036 /* Like retrieve_specialization, but for local declarations. */
1039 retrieve_local_specialization (tree tmpl
)
1043 if (local_specializations
== NULL
)
1046 spec
= (tree
) htab_find_with_hash (local_specializations
, tmpl
,
1047 htab_hash_pointer (tmpl
));
1048 return spec
? TREE_PURPOSE (spec
) : NULL_TREE
;
1051 /* Returns nonzero iff DECL is a specialization of TMPL. */
1054 is_specialization_of (tree decl
, tree tmpl
)
1058 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1062 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1068 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1070 for (t
= TREE_TYPE (decl
);
1072 t
= CLASSTYPE_USE_TEMPLATE (t
)
1073 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1074 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1081 /* Returns nonzero iff DECL is a specialization of friend declaration
1082 FRIEND_DECL according to [temp.friend]. */
1085 is_specialization_of_friend (tree decl
, tree friend_decl
)
1087 bool need_template
= true;
1090 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1091 || TREE_CODE (decl
) == TYPE_DECL
);
1093 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1094 of a template class, we want to check if DECL is a specialization
1096 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1097 && DECL_TEMPLATE_INFO (friend_decl
)
1098 && !DECL_USE_TEMPLATE (friend_decl
))
1100 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1101 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1102 need_template
= false;
1104 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1105 && !PRIMARY_TEMPLATE_P (friend_decl
))
1106 need_template
= false;
1108 /* There is nothing to do if this is not a template friend. */
1109 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1112 if (is_specialization_of (decl
, friend_decl
))
1116 A member of a class template may be declared to be a friend of a
1117 non-template class. In this case, the corresponding member of
1118 every specialization of the class template is a friend of the
1119 class granting friendship.
1121 For example, given a template friend declaration
1123 template <class T> friend void A<T>::f();
1125 the member function below is considered a friend
1127 template <> struct A<int> {
1131 For this type of template friend, TEMPLATE_DEPTH below will be
1132 nonzero. To determine if DECL is a friend of FRIEND, we first
1133 check if the enclosing class is a specialization of another. */
1135 template_depth
= template_class_depth (DECL_CONTEXT (friend_decl
));
1137 && DECL_CLASS_SCOPE_P (decl
)
1138 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1139 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1141 /* Next, we check the members themselves. In order to handle
1142 a few tricky cases, such as when FRIEND_DECL's are
1144 template <class T> friend void A<T>::g(T t);
1145 template <class T> template <T t> friend void A<T>::h();
1149 void A<int>::g(int);
1150 template <int> void A<int>::h();
1152 we need to figure out ARGS, the template arguments from
1153 the context of DECL. This is required for template substitution
1154 of `T' in the function parameter of `g' and template parameter
1155 of `h' in the above examples. Here ARGS corresponds to `int'. */
1157 tree context
= DECL_CONTEXT (decl
);
1158 tree args
= NULL_TREE
;
1159 int current_depth
= 0;
1161 while (current_depth
< template_depth
)
1163 if (CLASSTYPE_TEMPLATE_INFO (context
))
1165 if (current_depth
== 0)
1166 args
= TYPE_TI_ARGS (context
);
1168 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1171 context
= TYPE_CONTEXT (context
);
1174 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1179 tree friend_args_type
;
1180 tree decl_args_type
;
1182 /* Make sure that both DECL and FRIEND_DECL are templates or
1184 is_template
= DECL_TEMPLATE_INFO (decl
)
1185 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1186 if (need_template
^ is_template
)
1188 else if (is_template
)
1190 /* If both are templates, check template parameter list. */
1192 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1194 if (!comp_template_parms
1195 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1199 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1202 decl_type
= TREE_TYPE (decl
);
1204 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1205 tf_none
, NULL_TREE
);
1206 if (friend_type
== error_mark_node
)
1209 /* Check if return types match. */
1210 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1213 /* Check if function parameter types match, ignoring the
1214 `this' parameter. */
1215 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1216 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1217 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1218 friend_args_type
= TREE_CHAIN (friend_args_type
);
1219 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1220 decl_args_type
= TREE_CHAIN (decl_args_type
);
1222 return compparms (decl_args_type
, friend_args_type
);
1226 /* DECL is a TYPE_DECL */
1228 tree decl_type
= TREE_TYPE (decl
);
1230 /* Make sure that both DECL and FRIEND_DECL are templates or
1233 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1234 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1236 if (need_template
^ is_template
)
1238 else if (is_template
)
1241 /* If both are templates, check the name of the two
1242 TEMPLATE_DECL's first because is_friend didn't. */
1243 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1244 != DECL_NAME (friend_decl
))
1247 /* Now check template parameter list. */
1249 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1251 return comp_template_parms
1252 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1256 return (DECL_NAME (decl
)
1257 == DECL_NAME (friend_decl
));
1263 /* Register the specialization SPEC as a specialization of TMPL with
1264 the indicated ARGS. IS_FRIEND indicates whether the specialization
1265 is actually just a friend declaration. Returns SPEC, or an
1266 equivalent prior declaration, if available. */
1269 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1273 spec_entry
**slot
= NULL
;
1276 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
));
1278 if (TREE_CODE (spec
) == FUNCTION_DECL
1279 && uses_template_parms (DECL_TI_ARGS (spec
)))
1280 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1281 register it; we want the corresponding TEMPLATE_DECL instead.
1282 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1283 the more obvious `uses_template_parms (spec)' to avoid problems
1284 with default function arguments. In particular, given
1285 something like this:
1287 template <class T> void f(T t1, T t = T())
1289 the default argument expression is not substituted for in an
1290 instantiation unless and until it is actually needed. */
1293 if (optimize_specialization_lookup_p (tmpl
))
1294 /* We don't put these specializations in the hash table, but we might
1295 want to give an error about a mismatch. */
1296 fn
= retrieve_specialization (tmpl
, args
, 0);
1304 hash
= hash_specialization (&elt
);
1306 slot
= (spec_entry
**)
1307 htab_find_slot_with_hash (decl_specializations
, &elt
, hash
, INSERT
);
1314 /* We can sometimes try to re-register a specialization that we've
1315 already got. In particular, regenerate_decl_from_template calls
1316 duplicate_decls which will update the specialization list. But,
1317 we'll still get called again here anyhow. It's more convenient
1318 to simply allow this than to try to prevent it. */
1321 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1323 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1325 if (DECL_ODR_USED (fn
)
1326 || DECL_EXPLICIT_INSTANTIATION (fn
))
1328 error ("specialization of %qD after instantiation",
1330 return error_mark_node
;
1335 /* This situation should occur only if the first
1336 specialization is an implicit instantiation, the
1337 second is an explicit specialization, and the
1338 implicit instantiation has not yet been used. That
1339 situation can occur if we have implicitly
1340 instantiated a member function and then specialized
1343 We can also wind up here if a friend declaration that
1344 looked like an instantiation turns out to be a
1347 template <class T> void foo(T);
1348 class S { friend void foo<>(int) };
1349 template <> void foo(int);
1351 We transform the existing DECL in place so that any
1352 pointers to it become pointers to the updated
1355 If there was a definition for the template, but not
1356 for the specialization, we want this to look as if
1357 there were no definition, and vice versa. */
1358 DECL_INITIAL (fn
) = NULL_TREE
;
1359 duplicate_decls (spec
, fn
, is_friend
);
1360 /* The call to duplicate_decls will have applied
1363 An explicit specialization of a function template
1364 is inline only if it is explicitly declared to be,
1365 and independently of whether its function template
1368 to the primary function; now copy the inline bits to
1369 the various clones. */
1370 FOR_EACH_CLONE (clone
, fn
)
1372 DECL_DECLARED_INLINE_P (clone
)
1373 = DECL_DECLARED_INLINE_P (fn
);
1374 DECL_SOURCE_LOCATION (clone
)
1375 = DECL_SOURCE_LOCATION (fn
);
1377 check_specialization_namespace (fn
);
1382 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1384 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1385 /* Dup decl failed, but this is a new definition. Set the
1386 line number so any errors match this new
1388 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1394 return duplicate_decls (spec
, fn
, is_friend
);
1396 /* A specialization must be declared in the same namespace as the
1397 template it is specializing. */
1398 if (DECL_TEMPLATE_SPECIALIZATION (spec
)
1399 && !check_specialization_namespace (tmpl
))
1400 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1402 if (!optimize_specialization_lookup_p (tmpl
))
1404 gcc_assert (tmpl
&& args
&& spec
);
1405 *slot
= GGC_NEW (spec_entry
);
1407 if (TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1408 && PRIMARY_TEMPLATE_P (tmpl
)
1409 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1410 /* TMPL is a forward declaration of a template function; keep a list
1411 of all specializations in case we need to reassign them to a friend
1412 template later in tsubst_friend_function. */
1413 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1414 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1420 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1421 TMPL and ARGS members, ignores SPEC. */
1424 eq_specializations (const void *p1
, const void *p2
)
1426 const spec_entry
*e1
= (const spec_entry
*)p1
;
1427 const spec_entry
*e2
= (const spec_entry
*)p2
;
1429 return (e1
->tmpl
== e2
->tmpl
1430 && comp_template_args (e1
->args
, e2
->args
));
1433 /* Returns a hash for a template TMPL and template arguments ARGS. */
1436 hash_tmpl_and_args (tree tmpl
, tree args
)
1438 hashval_t val
= DECL_UID (tmpl
);
1439 return iterative_hash_template_arg (args
, val
);
1442 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1446 hash_specialization (const void *p
)
1448 const spec_entry
*e
= (const spec_entry
*)p
;
1449 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1452 /* Recursively calculate a hash value for a template argument ARG, for use
1453 in the hash tables of template specializations. */
1456 iterative_hash_template_arg (tree arg
, hashval_t val
)
1458 unsigned HOST_WIDE_INT i
;
1459 enum tree_code code
;
1462 if (arg
== NULL_TREE
)
1463 return iterative_hash_object (arg
, val
);
1468 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1469 /* We can get one of these when re-hashing a previous entry in the middle
1470 of substituting into a pack expansion. Just look through it. */
1471 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1473 code
= TREE_CODE (arg
);
1474 tclass
= TREE_CODE_CLASS (code
);
1476 val
= iterative_hash_object (code
, val
);
1483 case IDENTIFIER_NODE
:
1484 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1488 int i
, len
= TREE_VEC_LENGTH (arg
);
1489 for (i
= 0; i
< len
; ++i
)
1490 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1494 case TYPE_PACK_EXPANSION
:
1495 case EXPR_PACK_EXPANSION
:
1496 return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1498 case TYPE_ARGUMENT_PACK
:
1499 case NONTYPE_ARGUMENT_PACK
:
1500 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1503 for (; arg
; arg
= TREE_CHAIN (arg
))
1504 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1508 for (; arg
; arg
= OVL_CHAIN (arg
))
1509 val
= iterative_hash_template_arg (OVL_FUNCTION (arg
), val
);
1515 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1517 val
= iterative_hash_template_arg (field
, val
);
1518 val
= iterative_hash_template_arg (value
, val
);
1524 if (!DECL_ARTIFICIAL (arg
))
1525 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1526 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1529 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1532 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1533 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1535 case TEMPLATE_PARM_INDEX
:
1536 val
= iterative_hash_template_arg
1537 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1538 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1539 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1542 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1543 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1544 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1547 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1549 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1553 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1554 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1555 val
= iterative_hash_object (code
, val
);
1556 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1559 /* layout_type sets structural equality for arrays of
1560 incomplete type, so we can't rely on the canonical type
1562 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1563 return iterative_hash_template_arg (TYPE_DOMAIN (arg
), val
);
1569 if (TYPE_CANONICAL (arg
))
1570 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1572 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1573 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1574 /* Otherwise just compare the types during lookup. */
1577 case tcc_declaration
:
1579 return iterative_hash_expr (arg
, val
);
1582 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1584 unsigned n
= TREE_OPERAND_LENGTH (arg
);
1585 for (i
= 0; i
< n
; ++i
)
1586 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1595 /* Unregister the specialization SPEC as a specialization of TMPL.
1596 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1597 if the SPEC was listed as a specialization of TMPL.
1599 Note that SPEC has been ggc_freed, so we can't look inside it. */
1602 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1607 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1608 elt
.args
= TI_ARGS (tinfo
);
1609 elt
.spec
= NULL_TREE
;
1611 slot
= (spec_entry
**) htab_find_slot (decl_specializations
, &elt
, INSERT
);
1614 gcc_assert ((*slot
)->spec
== spec
|| (*slot
)->spec
== new_spec
);
1615 gcc_assert (new_spec
!= NULL_TREE
);
1616 (*slot
)->spec
= new_spec
;
1623 /* Compare an entry in the local specializations hash table P1 (which
1624 is really a pointer to a TREE_LIST) with P2 (which is really a
1628 eq_local_specializations (const void *p1
, const void *p2
)
1630 return TREE_VALUE ((const_tree
) p1
) == (const_tree
) p2
;
1633 /* Hash P1, an entry in the local specializations table. */
1636 hash_local_specialization (const void* p1
)
1638 return htab_hash_pointer (TREE_VALUE ((const_tree
) p1
));
1641 /* Like register_specialization, but for local declarations. We are
1642 registering SPEC, an instantiation of TMPL. */
1645 register_local_specialization (tree spec
, tree tmpl
)
1649 slot
= htab_find_slot_with_hash (local_specializations
, tmpl
,
1650 htab_hash_pointer (tmpl
), INSERT
);
1651 *slot
= build_tree_list (spec
, tmpl
);
1654 /* TYPE is a class type. Returns true if TYPE is an explicitly
1655 specialized class. */
1658 explicit_class_specialization_p (tree type
)
1660 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1662 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1665 /* Print the list of functions at FNS, going through all the overloads
1666 for each element of the list. Alternatively, FNS can not be a
1667 TREE_LIST, in which case it will be printed together with all the
1670 MORE and *STR should respectively be FALSE and NULL when the function
1671 is called from the outside. They are used internally on recursive
1672 calls. print_candidates manages the two parameters and leaves NULL
1673 in *STR when it ends. */
1676 print_candidates_1 (tree fns
, bool more
, const char **str
)
1679 char *spaces
= NULL
;
1681 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1682 if (TREE_CODE (fn
) == TREE_LIST
)
1684 gcc_assert (!OVL_NEXT (fn
) && !is_overloaded_fn (fn
));
1685 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1686 print_candidates_1 (TREE_VALUE (fn2
),
1687 TREE_CHAIN (fn2
) || more
, str
);
1693 /* Pick the prefix string. */
1694 if (!more
&& !OVL_NEXT (fns
))
1696 error ("candidate is: %+#D", OVL_CURRENT (fn
));
1700 *str
= _("candidates are:");
1701 spaces
= get_spaces (*str
);
1703 error ("%s %+#D", *str
, OVL_CURRENT (fn
));
1704 *str
= spaces
? spaces
: *str
;
1714 /* Print the list of candidate FNS in an error message. */
1717 print_candidates (tree fns
)
1719 const char *str
= NULL
;
1720 print_candidates_1 (fns
, false, &str
);
1721 gcc_assert (str
== NULL
);
1724 /* Returns the template (one of the functions given by TEMPLATE_ID)
1725 which can be specialized to match the indicated DECL with the
1726 explicit template args given in TEMPLATE_ID. The DECL may be
1727 NULL_TREE if none is available. In that case, the functions in
1728 TEMPLATE_ID are non-members.
1730 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1731 specialization of a member template.
1733 The TEMPLATE_COUNT is the number of references to qualifying
1734 template classes that appeared in the name of the function. See
1735 check_explicit_specialization for a more accurate description.
1737 TSK indicates what kind of template declaration (if any) is being
1738 declared. TSK_TEMPLATE indicates that the declaration given by
1739 DECL, though a FUNCTION_DECL, has template parameters, and is
1740 therefore a template function.
1742 The template args (those explicitly specified and those deduced)
1743 are output in a newly created vector *TARGS_OUT.
1745 If it is impossible to determine the result, an error message is
1746 issued. The error_mark_node is returned to indicate failure. */
1749 determine_specialization (tree template_id
,
1752 int need_member_template
,
1758 tree explicit_targs
;
1759 tree candidates
= NULL_TREE
;
1760 /* A TREE_LIST of templates of which DECL may be a specialization.
1761 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1762 corresponding TREE_PURPOSE is the set of template arguments that,
1763 when used to instantiate the template, would produce a function
1764 with the signature of DECL. */
1765 tree templates
= NULL_TREE
;
1767 struct cp_binding_level
*b
;
1769 *targs_out
= NULL_TREE
;
1771 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1772 return error_mark_node
;
1774 fns
= TREE_OPERAND (template_id
, 0);
1775 explicit_targs
= TREE_OPERAND (template_id
, 1);
1777 if (fns
== error_mark_node
)
1778 return error_mark_node
;
1780 /* Check for baselinks. */
1781 if (BASELINK_P (fns
))
1782 fns
= BASELINK_FUNCTIONS (fns
);
1784 if (!is_overloaded_fn (fns
))
1786 error ("%qD is not a function template", fns
);
1787 return error_mark_node
;
1790 /* Count the number of template headers specified for this
1793 for (b
= current_binding_level
;
1794 b
->kind
== sk_template_parms
;
1798 for (; fns
; fns
= OVL_NEXT (fns
))
1800 tree fn
= OVL_CURRENT (fns
);
1802 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1804 tree decl_arg_types
;
1807 /* In case of explicit specialization, we need to check if
1808 the number of template headers appearing in the specialization
1809 is correct. This is usually done in check_explicit_specialization,
1810 but the check done there cannot be exhaustive when specializing
1811 member functions. Consider the following code:
1813 template <> void A<int>::f(int);
1814 template <> template <> void A<int>::f(int);
1816 Assuming that A<int> is not itself an explicit specialization
1817 already, the first line specializes "f" which is a non-template
1818 member function, whilst the second line specializes "f" which
1819 is a template member function. So both lines are syntactically
1820 correct, and check_explicit_specialization does not reject
1823 Here, we can do better, as we are matching the specialization
1824 against the declarations. We count the number of template
1825 headers, and we check if they match TEMPLATE_COUNT + 1
1826 (TEMPLATE_COUNT is the number of qualifying template classes,
1827 plus there must be another header for the member template
1830 Notice that if header_count is zero, this is not a
1831 specialization but rather a template instantiation, so there
1832 is no check we can perform here. */
1833 if (header_count
&& header_count
!= template_count
+ 1)
1836 /* Check that the number of template arguments at the
1837 innermost level for DECL is the same as for FN. */
1838 if (current_binding_level
->kind
== sk_template_parms
1839 && !current_binding_level
->explicit_spec_p
1840 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1841 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1842 (current_template_parms
))))
1845 /* DECL might be a specialization of FN. */
1846 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1847 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1849 /* For a non-static member function, we need to make sure
1850 that the const qualification is the same. Since
1851 get_bindings does not try to merge the "this" parameter,
1852 we must do the comparison explicitly. */
1853 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1854 && !same_type_p (TREE_VALUE (fn_arg_types
),
1855 TREE_VALUE (decl_arg_types
)))
1858 /* Skip the "this" parameter and, for constructors of
1859 classes with virtual bases, the VTT parameter. A
1860 full specialization of a constructor will have a VTT
1861 parameter, but a template never will. */
1863 = skip_artificial_parms_for (decl
, decl_arg_types
);
1865 = skip_artificial_parms_for (fn
, fn_arg_types
);
1867 /* Check that the number of function parameters matches.
1869 template <class T> void f(int i = 0);
1870 template <> void f<int>();
1871 The specialization f<int> is invalid but is not caught
1872 by get_bindings below. */
1873 if (list_length (fn_arg_types
) != list_length (decl_arg_types
))
1876 /* Function templates cannot be specializations; there are
1877 no partial specializations of functions. Therefore, if
1878 the type of DECL does not match FN, there is no
1880 if (tsk
== tsk_template
)
1882 if (compparms (fn_arg_types
, decl_arg_types
))
1883 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1887 /* See whether this function might be a specialization of this
1889 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1892 /* We cannot deduce template arguments that when used to
1893 specialize TMPL will produce DECL. */
1896 /* Save this template, and the arguments deduced. */
1897 templates
= tree_cons (targs
, fn
, templates
);
1899 else if (need_member_template
)
1900 /* FN is an ordinary member function, and we need a
1901 specialization of a member template. */
1903 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1904 /* We can get IDENTIFIER_NODEs here in certain erroneous
1907 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1908 /* This is just an ordinary non-member function. Nothing can
1909 be a specialization of that. */
1911 else if (DECL_ARTIFICIAL (fn
))
1912 /* Cannot specialize functions that are created implicitly. */
1916 tree decl_arg_types
;
1918 /* This is an ordinary member function. However, since
1919 we're here, we can assume it's enclosing class is a
1920 template class. For example,
1922 template <typename T> struct S { void f(); };
1923 template <> void S<int>::f() {}
1925 Here, S<int>::f is a non-template, but S<int> is a
1926 template class. If FN has the same type as DECL, we
1927 might be in business. */
1929 if (!DECL_TEMPLATE_INFO (fn
))
1930 /* Its enclosing class is an explicit specialization
1931 of a template class. This is not a candidate. */
1934 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
1935 TREE_TYPE (TREE_TYPE (fn
))))
1936 /* The return types differ. */
1939 /* Adjust the type of DECL in case FN is a static member. */
1940 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1941 if (DECL_STATIC_FUNCTION_P (fn
)
1942 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1943 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1945 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1948 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1952 if (templates
&& TREE_CHAIN (templates
))
1958 It is possible for a specialization with a given function
1959 signature to be instantiated from more than one function
1960 template. In such cases, explicit specification of the
1961 template arguments must be used to uniquely identify the
1962 function template specialization being specialized.
1964 Note that here, there's no suggestion that we're supposed to
1965 determine which of the candidate templates is most
1966 specialized. However, we, also have:
1970 Partial ordering of overloaded function template
1971 declarations is used in the following contexts to select
1972 the function template to which a function template
1973 specialization refers:
1975 -- when an explicit specialization refers to a function
1978 So, we do use the partial ordering rules, at least for now.
1979 This extension can only serve to make invalid programs valid,
1980 so it's safe. And, there is strong anecdotal evidence that
1981 the committee intended the partial ordering rules to apply;
1982 the EDG front end has that behavior, and John Spicer claims
1983 that the committee simply forgot to delete the wording in
1984 [temp.expl.spec]. */
1985 tree tmpl
= most_specialized_instantiation (templates
);
1986 if (tmpl
!= error_mark_node
)
1989 TREE_CHAIN (templates
) = NULL_TREE
;
1993 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
1995 error ("template-id %qD for %q+D does not match any template "
1996 "declaration", template_id
, decl
);
1997 if (header_count
&& header_count
!= template_count
+ 1)
1998 inform (input_location
, "saw %d %<template<>%>, need %d for "
1999 "specializing a member function template",
2000 header_count
, template_count
+ 1);
2001 return error_mark_node
;
2003 else if ((templates
&& TREE_CHAIN (templates
))
2004 || (candidates
&& TREE_CHAIN (candidates
))
2005 || (templates
&& candidates
))
2007 error ("ambiguous template specialization %qD for %q+D",
2009 candidates
= chainon (candidates
, templates
);
2010 print_candidates (candidates
);
2011 return error_mark_node
;
2014 /* We have one, and exactly one, match. */
2017 tree fn
= TREE_VALUE (candidates
);
2018 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2019 /* DECL is a re-declaration or partial instantiation of a template
2021 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2023 /* It was a specialization of an ordinary member function in a
2025 return DECL_TI_TEMPLATE (fn
);
2028 /* It was a specialization of a template. */
2029 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2030 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2032 *targs_out
= copy_node (targs
);
2033 SET_TMPL_ARGS_LEVEL (*targs_out
,
2034 TMPL_ARGS_DEPTH (*targs_out
),
2035 TREE_PURPOSE (templates
));
2038 *targs_out
= TREE_PURPOSE (templates
);
2039 return TREE_VALUE (templates
);
2042 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2043 but with the default argument values filled in from those in the
2047 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2050 tree new_spec_types
;
2055 if (spec_types
== void_list_node
)
2056 return void_list_node
;
2058 /* Substitute into the rest of the list. */
2060 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2061 TREE_CHAIN (tmpl_types
));
2063 /* Add the default argument for this parameter. */
2064 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2065 TREE_VALUE (spec_types
),
2069 /* DECL is an explicit specialization. Replicate default arguments
2070 from the template it specializes. (That way, code like:
2072 template <class T> void f(T = 3);
2073 template <> void f(double);
2076 works, as required.) An alternative approach would be to look up
2077 the correct default arguments at the call-site, but this approach
2078 is consistent with how implicit instantiations are handled. */
2081 copy_default_args_to_explicit_spec (tree decl
)
2086 tree new_spec_types
;
2090 tree object_type
= NULL_TREE
;
2091 tree in_charge
= NULL_TREE
;
2092 tree vtt
= NULL_TREE
;
2094 /* See if there's anything we need to do. */
2095 tmpl
= DECL_TI_TEMPLATE (decl
);
2096 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2097 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2098 if (TREE_PURPOSE (t
))
2103 old_type
= TREE_TYPE (decl
);
2104 spec_types
= TYPE_ARG_TYPES (old_type
);
2106 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2108 /* Remove the this pointer, but remember the object's type for
2110 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2111 spec_types
= TREE_CHAIN (spec_types
);
2112 tmpl_types
= TREE_CHAIN (tmpl_types
);
2114 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2116 /* DECL may contain more parameters than TMPL due to the extra
2117 in-charge parameter in constructors and destructors. */
2118 in_charge
= spec_types
;
2119 spec_types
= TREE_CHAIN (spec_types
);
2121 if (DECL_HAS_VTT_PARM_P (decl
))
2124 spec_types
= TREE_CHAIN (spec_types
);
2128 /* Compute the merged default arguments. */
2130 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2132 /* Compute the new FUNCTION_TYPE. */
2136 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2141 /* Put the in-charge parameter back. */
2142 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2143 TREE_VALUE (in_charge
),
2146 new_type
= build_method_type_directly (object_type
,
2147 TREE_TYPE (old_type
),
2151 new_type
= build_function_type (TREE_TYPE (old_type
),
2153 new_type
= cp_build_type_attribute_variant (new_type
,
2154 TYPE_ATTRIBUTES (old_type
));
2155 new_type
= build_exception_variant (new_type
,
2156 TYPE_RAISES_EXCEPTIONS (old_type
));
2157 TREE_TYPE (decl
) = new_type
;
2160 /* Check to see if the function just declared, as indicated in
2161 DECLARATOR, and in DECL, is a specialization of a function
2162 template. We may also discover that the declaration is an explicit
2163 instantiation at this point.
2165 Returns DECL, or an equivalent declaration that should be used
2166 instead if all goes well. Issues an error message if something is
2167 amiss. Returns error_mark_node if the error is not easily
2170 FLAGS is a bitmask consisting of the following flags:
2172 2: The function has a definition.
2173 4: The function is a friend.
2175 The TEMPLATE_COUNT is the number of references to qualifying
2176 template classes that appeared in the name of the function. For
2179 template <class T> struct S { void f(); };
2182 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2183 classes are not counted in the TEMPLATE_COUNT, so that in
2185 template <class T> struct S {};
2186 template <> struct S<int> { void f(); }
2187 template <> void S<int>::f();
2189 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2190 invalid; there should be no template <>.)
2192 If the function is a specialization, it is marked as such via
2193 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2194 is set up correctly, and it is added to the list of specializations
2195 for that template. */
2198 check_explicit_specialization (tree declarator
,
2203 int have_def
= flags
& 2;
2204 int is_friend
= flags
& 4;
2205 int specialization
= 0;
2206 int explicit_instantiation
= 0;
2207 int member_specialization
= 0;
2208 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2209 tree dname
= DECL_NAME (decl
);
2214 if (!processing_specialization
)
2217 tsk
= tsk_excessive_parms
;
2220 tsk
= current_tmpl_spec_kind (template_count
);
2225 if (processing_specialization
)
2228 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2230 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2233 /* This could be something like:
2235 template <class T> void f(T);
2236 class S { friend void f<>(int); } */
2240 /* This case handles bogus declarations like template <>
2241 template <class T> void f<int>(); */
2243 error ("template-id %qD in declaration of primary template",
2250 case tsk_invalid_member_spec
:
2251 /* The error has already been reported in
2252 check_specialization_scope. */
2253 return error_mark_node
;
2255 case tsk_invalid_expl_inst
:
2256 error ("template parameter list used in explicit instantiation");
2262 error ("definition provided for explicit instantiation");
2264 explicit_instantiation
= 1;
2267 case tsk_excessive_parms
:
2268 case tsk_insufficient_parms
:
2269 if (tsk
== tsk_excessive_parms
)
2270 error ("too many template parameter lists in declaration of %qD",
2272 else if (template_header_count
)
2273 error("too few template parameter lists in declaration of %qD", decl
);
2275 error("explicit specialization of %qD must be introduced by "
2276 "%<template <>%>", decl
);
2280 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2282 member_specialization
= 1;
2288 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2290 /* This case handles bogus declarations like template <>
2291 template <class T> void f<int>(); */
2293 if (uses_template_parms (declarator
))
2294 error ("function template partial specialization %qD "
2295 "is not allowed", declarator
);
2297 error ("template-id %qD in declaration of primary template",
2302 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2303 /* This is a specialization of a member template, without
2304 specialization the containing class. Something like:
2306 template <class T> struct S {
2307 template <class U> void f (U);
2309 template <> template <class U> void S<int>::f(U) {}
2311 That's a specialization -- but of the entire template. */
2319 if (specialization
|| member_specialization
)
2321 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2322 for (; t
; t
= TREE_CHAIN (t
))
2323 if (TREE_PURPOSE (t
))
2325 permerror (input_location
,
2326 "default argument specified in explicit specialization");
2331 if (specialization
|| member_specialization
|| explicit_instantiation
)
2333 tree tmpl
= NULL_TREE
;
2334 tree targs
= NULL_TREE
;
2336 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2337 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2341 gcc_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
);
2346 /* If there is no class context, the explicit instantiation
2347 must be at namespace scope. */
2348 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2350 /* Find the namespace binding, using the declaration
2352 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2354 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2356 error ("%qD is not a template function", dname
);
2357 fns
= error_mark_node
;
2361 tree fn
= OVL_CURRENT (fns
);
2362 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2363 CP_DECL_CONTEXT (fn
)))
2364 error ("%qD is not declared in %qD",
2365 decl
, current_namespace
);
2369 declarator
= lookup_template_function (fns
, NULL_TREE
);
2372 if (declarator
== error_mark_node
)
2373 return error_mark_node
;
2375 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2377 if (!explicit_instantiation
)
2378 /* A specialization in class scope. This is invalid,
2379 but the error will already have been flagged by
2380 check_specialization_scope. */
2381 return error_mark_node
;
2384 /* It's not valid to write an explicit instantiation in
2387 class C { template void f(); }
2389 This case is caught by the parser. However, on
2392 template class C { void f(); };
2394 (which is invalid) we can get here. The error will be
2401 else if (ctype
!= NULL_TREE
2402 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
2405 /* Find the list of functions in ctype that have the same
2406 name as the declared function. */
2407 tree name
= TREE_OPERAND (declarator
, 0);
2408 tree fns
= NULL_TREE
;
2411 if (constructor_name_p (name
, ctype
))
2413 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2415 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2416 : !CLASSTYPE_DESTRUCTORS (ctype
))
2418 /* From [temp.expl.spec]:
2420 If such an explicit specialization for the member
2421 of a class template names an implicitly-declared
2422 special member function (clause _special_), the
2423 program is ill-formed.
2425 Similar language is found in [temp.explicit]. */
2426 error ("specialization of implicitly-declared special member function");
2427 return error_mark_node
;
2430 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2433 if (!DECL_CONV_FN_P (decl
))
2435 idx
= lookup_fnfields_1 (ctype
, name
);
2437 fns
= VEC_index (tree
, CLASSTYPE_METHOD_VEC (ctype
), idx
);
2441 VEC(tree
,gc
) *methods
;
2444 /* For a type-conversion operator, we cannot do a
2445 name-based lookup. We might be looking for `operator
2446 int' which will be a specialization of `operator T'.
2447 So, we find *all* the conversion operators, and then
2448 select from them. */
2451 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2453 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2454 VEC_iterate (tree
, methods
, idx
, ovl
);
2457 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2458 /* There are no more conversion functions. */
2461 /* Glue all these conversion functions together
2462 with those we already have. */
2463 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2464 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2468 if (fns
== NULL_TREE
)
2470 error ("no member function %qD declared in %qT", name
, ctype
);
2471 return error_mark_node
;
2474 TREE_OPERAND (declarator
, 0) = fns
;
2477 /* Figure out what exactly is being specialized at this point.
2478 Note that for an explicit instantiation, even one for a
2479 member function, we cannot tell apriori whether the
2480 instantiation is for a member template, or just a member
2481 function of a template class. Even if a member template is
2482 being instantiated, the member template arguments may be
2483 elided if they can be deduced from the rest of the
2485 tmpl
= determine_specialization (declarator
, decl
,
2487 member_specialization
,
2491 if (!tmpl
|| tmpl
== error_mark_node
)
2492 /* We couldn't figure out what this declaration was
2494 return error_mark_node
;
2497 tree gen_tmpl
= most_general_template (tmpl
);
2499 if (explicit_instantiation
)
2501 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2502 is done by do_decl_instantiation later. */
2504 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2505 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2507 if (arg_depth
> parm_depth
)
2509 /* If TMPL is not the most general template (for
2510 example, if TMPL is a friend template that is
2511 injected into namespace scope), then there will
2512 be too many levels of TARGS. Remove some of them
2517 new_targs
= make_tree_vec (parm_depth
);
2518 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2519 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2520 = TREE_VEC_ELT (targs
, i
);
2524 return instantiate_template (tmpl
, targs
, tf_error
);
2527 /* If we thought that the DECL was a member function, but it
2528 turns out to be specializing a static member function,
2529 make DECL a static member function as well. */
2530 if (DECL_STATIC_FUNCTION_P (tmpl
)
2531 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2532 revert_static_member_fn (decl
);
2534 /* If this is a specialization of a member template of a
2535 template class, we want to return the TEMPLATE_DECL, not
2536 the specialization of it. */
2537 if (tsk
== tsk_template
)
2539 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2540 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2541 DECL_INITIAL (result
) = NULL_TREE
;
2545 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2546 DECL_SOURCE_LOCATION (result
)
2547 = DECL_SOURCE_LOCATION (decl
);
2548 /* We want to use the argument list specified in the
2549 definition, not in the original declaration. */
2550 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2551 for (parm
= DECL_ARGUMENTS (result
); parm
;
2552 parm
= TREE_CHAIN (parm
))
2553 DECL_CONTEXT (parm
) = result
;
2555 return register_specialization (tmpl
, gen_tmpl
, targs
,
2559 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2560 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2562 /* Inherit default function arguments from the template
2563 DECL is specializing. */
2564 copy_default_args_to_explicit_spec (decl
);
2566 /* This specialization has the same protection as the
2567 template it specializes. */
2568 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2569 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2571 /* 7.1.1-1 [dcl.stc]
2573 A storage-class-specifier shall not be specified in an
2574 explicit specialization...
2576 The parser rejects these, so unless action is taken here,
2577 explicit function specializations will always appear with
2580 The action recommended by the C++ CWG in response to C++
2581 defect report 605 is to make the storage class and linkage
2582 of the explicit specialization match the templated function:
2584 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2586 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2588 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2589 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2591 /* This specialization has the same linkage and visibility as
2592 the function template it specializes. */
2593 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2594 if (! TREE_PUBLIC (decl
))
2596 DECL_INTERFACE_KNOWN (decl
) = 1;
2597 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2599 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2600 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2602 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2603 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2607 /* If DECL is a friend declaration, declared using an
2608 unqualified name, the namespace associated with DECL may
2609 have been set incorrectly. For example, in:
2611 template <typename T> void f(T);
2613 struct S { friend void f<int>(int); }
2616 we will have set the DECL_CONTEXT for the friend
2617 declaration to N, rather than to the global namespace. */
2618 if (DECL_NAMESPACE_SCOPE_P (decl
))
2619 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2621 if (is_friend
&& !have_def
)
2622 /* This is not really a declaration of a specialization.
2623 It's just the name of an instantiation. But, it's not
2624 a request for an instantiation, either. */
2625 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2626 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2627 /* This is indeed a specialization. In case of constructors
2628 and destructors, we need in-charge and not-in-charge
2629 versions in V3 ABI. */
2630 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2632 /* Register this specialization so that we can find it
2634 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2641 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2642 parameters. These are represented in the same format used for
2643 DECL_TEMPLATE_PARMS. */
2646 comp_template_parms (const_tree parms1
, const_tree parms2
)
2651 if (parms1
== parms2
)
2654 for (p1
= parms1
, p2
= parms2
;
2655 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2656 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2658 tree t1
= TREE_VALUE (p1
);
2659 tree t2
= TREE_VALUE (p2
);
2662 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2663 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2665 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2668 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2670 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2671 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2673 /* If either of the template parameters are invalid, assume
2674 they match for the sake of error recovery. */
2675 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2678 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2681 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2682 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2683 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2685 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2690 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2691 /* One set of parameters has more parameters lists than the
2698 /* Determine whether PARM is a parameter pack. */
2701 template_parameter_pack_p (const_tree parm
)
2703 /* Determine if we have a non-type template parameter pack. */
2704 if (TREE_CODE (parm
) == PARM_DECL
)
2705 return (DECL_TEMPLATE_PARM_P (parm
)
2706 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2708 /* If this is a list of template parameters, we could get a
2709 TYPE_DECL or a TEMPLATE_DECL. */
2710 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2711 parm
= TREE_TYPE (parm
);
2713 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2714 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2715 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2718 /* Determine if T is a function parameter pack. */
2721 function_parameter_pack_p (const_tree t
)
2723 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2724 return FUNCTION_PARAMETER_PACK_P (t
);
2728 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2729 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2732 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2734 if (! primary_func_tmpl_inst
2735 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2736 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2739 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2742 /* Return true iff the function parameter PARAM_DECL was expanded
2743 from the function parameter pack PACK. */
2746 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
2748 if (DECL_ARTIFICIAL (param_decl
)
2749 || !function_parameter_pack_p (pack
))
2752 /* The parameter pack and its pack arguments have the same
2754 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
2757 /* Determine whether ARGS describes a variadic template args list,
2758 i.e., one that is terminated by a template argument pack. */
2761 template_args_variadic_p (tree args
)
2766 if (args
== NULL_TREE
)
2769 args
= INNERMOST_TEMPLATE_ARGS (args
);
2770 nargs
= TREE_VEC_LENGTH (args
);
2775 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2777 return ARGUMENT_PACK_P (last_parm
);
2780 /* Generate a new name for the parameter pack name NAME (an
2781 IDENTIFIER_NODE) that incorporates its */
2784 make_ith_pack_parameter_name (tree name
, int i
)
2786 /* Munge the name to include the parameter index. */
2787 #define NUMBUF_LEN 128
2788 char numbuf
[NUMBUF_LEN
];
2792 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
2793 newname_len
= IDENTIFIER_LENGTH (name
)
2794 + strlen (numbuf
) + 2;
2795 newname
= (char*)alloca (newname_len
);
2796 snprintf (newname
, newname_len
,
2797 "%s#%i", IDENTIFIER_POINTER (name
), i
);
2798 return get_identifier (newname
);
2801 /* Return true if T is a primary function
2802 or class template instantiation. */
2805 primary_template_instantiation_p (const_tree t
)
2810 if (TREE_CODE (t
) == FUNCTION_DECL
)
2811 return DECL_LANG_SPECIFIC (t
)
2812 && DECL_TEMPLATE_INSTANTIATION (t
)
2813 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
2814 else if (CLASS_TYPE_P (t
))
2815 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
2816 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
2820 /* Return true if PARM is a template template parameter. */
2823 template_template_parameter_p (const_tree parm
)
2825 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
2828 /* Return the template parameters of T if T is a
2829 primary template instantiation, NULL otherwise. */
2832 get_primary_template_innermost_parameters (const_tree t
)
2834 tree parms
= NULL
, template_info
= NULL
;
2836 if ((template_info
= get_template_info (t
))
2837 && primary_template_instantiation_p (t
))
2838 parms
= INNERMOST_TEMPLATE_PARMS
2839 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
2844 /* Returns the template arguments of T if T is a template instantiation,
2848 get_template_innermost_arguments (const_tree t
)
2850 tree args
= NULL
, template_info
= NULL
;
2852 if ((template_info
= get_template_info (t
))
2853 && TI_ARGS (template_info
))
2854 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
2859 /* Return the argument pack elements of T if T is a template argument pack,
2863 get_template_argument_pack_elems (const_tree t
)
2865 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
2866 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
2869 return ARGUMENT_PACK_ARGS (t
);
2872 /* Structure used to track the progress of find_parameter_packs_r. */
2873 struct find_parameter_pack_data
2875 /* TREE_LIST that will contain all of the parameter packs found by
2877 tree
* parameter_packs
;
2879 /* Set of AST nodes that have been visited by the traversal. */
2880 struct pointer_set_t
*visited
;
2883 /* Identifies all of the argument packs that occur in a template
2884 argument and appends them to the TREE_LIST inside DATA, which is a
2885 find_parameter_pack_data structure. This is a subroutine of
2886 make_pack_expansion and uses_parameter_packs. */
2888 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
2891 struct find_parameter_pack_data
* ppd
=
2892 (struct find_parameter_pack_data
*)data
;
2893 bool parameter_pack_p
= false;
2895 /* Identify whether this is a parameter pack or not. */
2896 switch (TREE_CODE (t
))
2898 case TEMPLATE_PARM_INDEX
:
2899 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
2900 parameter_pack_p
= true;
2903 case TEMPLATE_TYPE_PARM
:
2904 case TEMPLATE_TEMPLATE_PARM
:
2905 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
2906 parameter_pack_p
= true;
2910 if (FUNCTION_PARAMETER_PACK_P (t
))
2912 /* We don't want to walk into the type of a PARM_DECL,
2913 because we don't want to see the type parameter pack. */
2915 parameter_pack_p
= true;
2920 /* Not a parameter pack. */
2924 if (parameter_pack_p
)
2926 /* Add this parameter pack to the list. */
2927 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
2931 cp_walk_tree (&TYPE_CONTEXT (t
),
2932 &find_parameter_packs_r
, ppd
, ppd
->visited
);
2934 /* This switch statement will return immediately if we don't find a
2936 switch (TREE_CODE (t
))
2938 case TEMPLATE_PARM_INDEX
:
2941 case BOUND_TEMPLATE_TEMPLATE_PARM
:
2942 /* Check the template itself. */
2943 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
2944 &find_parameter_packs_r
, ppd
, ppd
->visited
);
2945 /* Check the template arguments. */
2946 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
2951 case TEMPLATE_TYPE_PARM
:
2952 case TEMPLATE_TEMPLATE_PARM
:
2959 if (TYPE_PTRMEMFUNC_P (t
))
2965 if (TYPE_TEMPLATE_INFO (t
))
2966 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
2967 &find_parameter_packs_r
, ppd
, ppd
->visited
);
2973 cp_walk_tree (&TREE_TYPE (t
),
2974 &find_parameter_packs_r
, ppd
, ppd
->visited
);
2978 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
2983 case TYPE_PACK_EXPANSION
:
2984 case EXPR_PACK_EXPANSION
:
2989 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
2994 case IDENTIFIER_NODE
:
2995 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3007 /* Determines if the expression or type T uses any parameter packs. */
3009 uses_parameter_packs (tree t
)
3011 tree parameter_packs
= NULL_TREE
;
3012 struct find_parameter_pack_data ppd
;
3013 ppd
.parameter_packs
= ¶meter_packs
;
3014 ppd
.visited
= pointer_set_create ();
3015 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3016 pointer_set_destroy (ppd
.visited
);
3017 return parameter_packs
!= NULL_TREE
;
3020 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3021 representation a base-class initializer into a parameter pack
3022 expansion. If all goes well, the resulting node will be an
3023 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3026 make_pack_expansion (tree arg
)
3029 tree parameter_packs
= NULL_TREE
;
3030 bool for_types
= false;
3031 struct find_parameter_pack_data ppd
;
3033 if (!arg
|| arg
== error_mark_node
)
3036 if (TREE_CODE (arg
) == TREE_LIST
)
3038 /* The only time we will see a TREE_LIST here is for a base
3039 class initializer. In this case, the TREE_PURPOSE will be a
3040 _TYPE node (representing the base class expansion we're
3041 initializing) and the TREE_VALUE will be a TREE_LIST
3042 containing the initialization arguments.
3044 The resulting expansion looks somewhat different from most
3045 expansions. Rather than returning just one _EXPANSION, we
3046 return a TREE_LIST whose TREE_PURPOSE is a
3047 TYPE_PACK_EXPANSION containing the bases that will be
3048 initialized. The TREE_VALUE will be identical to the
3049 original TREE_VALUE, which is a list of arguments that will
3050 be passed to each base. We do not introduce any new pack
3051 expansion nodes into the TREE_VALUE (although it is possible
3052 that some already exist), because the TREE_PURPOSE and
3053 TREE_VALUE all need to be expanded together with the same
3054 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3055 resulting TREE_PURPOSE will mention the parameter packs in
3056 both the bases and the arguments to the bases. */
3059 tree parameter_packs
= NULL_TREE
;
3061 /* Determine which parameter packs will be used by the base
3063 ppd
.visited
= pointer_set_create ();
3064 ppd
.parameter_packs
= ¶meter_packs
;
3065 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3068 if (parameter_packs
== NULL_TREE
)
3070 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3071 pointer_set_destroy (ppd
.visited
);
3072 return error_mark_node
;
3075 if (TREE_VALUE (arg
) != void_type_node
)
3077 /* Collect the sets of parameter packs used in each of the
3078 initialization arguments. */
3079 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3081 /* Determine which parameter packs will be expanded in this
3083 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3088 pointer_set_destroy (ppd
.visited
);
3090 /* Create the pack expansion type for the base type. */
3091 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3092 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3093 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3095 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3096 they will rarely be compared to anything. */
3097 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3099 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3102 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3105 /* Build the PACK_EXPANSION_* node. */
3107 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3108 : make_node (EXPR_PACK_EXPANSION
);
3109 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3110 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3112 /* Propagate type and const-expression information. */
3113 TREE_TYPE (result
) = TREE_TYPE (arg
);
3114 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3117 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3118 they will rarely be compared to anything. */
3119 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3121 /* Determine which parameter packs will be expanded. */
3122 ppd
.parameter_packs
= ¶meter_packs
;
3123 ppd
.visited
= pointer_set_create ();
3124 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3125 pointer_set_destroy (ppd
.visited
);
3127 /* Make sure we found some parameter packs. */
3128 if (parameter_packs
== NULL_TREE
)
3131 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3133 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3134 return error_mark_node
;
3136 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3141 /* Checks T for any "bare" parameter packs, which have not yet been
3142 expanded, and issues an error if any are found. This operation can
3143 only be done on full expressions or types (e.g., an expression
3144 statement, "if" condition, etc.), because we could have expressions like:
3146 foo(f(g(h(args)))...)
3148 where "args" is a parameter pack. check_for_bare_parameter_packs
3149 should not be called for the subexpressions args, h(args),
3150 g(h(args)), or f(g(h(args))), because we would produce erroneous
3153 Returns TRUE and emits an error if there were bare parameter packs,
3154 returns FALSE otherwise. */
3156 check_for_bare_parameter_packs (tree t
)
3158 tree parameter_packs
= NULL_TREE
;
3159 struct find_parameter_pack_data ppd
;
3161 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3164 if (TREE_CODE (t
) == TYPE_DECL
)
3167 ppd
.parameter_packs
= ¶meter_packs
;
3168 ppd
.visited
= pointer_set_create ();
3169 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3170 pointer_set_destroy (ppd
.visited
);
3172 if (parameter_packs
)
3174 error ("parameter packs not expanded with %<...%>:");
3175 while (parameter_packs
)
3177 tree pack
= TREE_VALUE (parameter_packs
);
3178 tree name
= NULL_TREE
;
3180 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3181 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3182 name
= TYPE_NAME (pack
);
3183 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3184 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3186 name
= DECL_NAME (pack
);
3189 inform (input_location
, " %qD", name
);
3191 inform (input_location
, " <anonymous>");
3193 parameter_packs
= TREE_CHAIN (parameter_packs
);
3202 /* Expand any parameter packs that occur in the template arguments in
3205 expand_template_argument_pack (tree args
)
3207 tree result_args
= NULL_TREE
;
3208 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3209 int num_result_args
= -1;
3210 int non_default_args_count
= -1;
3212 /* First, determine if we need to expand anything, and the number of
3213 slots we'll need. */
3214 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3216 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3217 if (arg
== NULL_TREE
)
3219 if (ARGUMENT_PACK_P (arg
))
3221 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3222 if (num_result_args
< 0)
3223 num_result_args
= in_arg
+ num_packed
;
3225 num_result_args
+= num_packed
;
3229 if (num_result_args
>= 0)
3234 /* If no expansion is necessary, we're done. */
3235 if (num_result_args
< 0)
3238 /* Expand arguments. */
3239 result_args
= make_tree_vec (num_result_args
);
3240 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3241 non_default_args_count
=
3242 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3243 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3245 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3246 if (ARGUMENT_PACK_P (arg
))
3248 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3249 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3250 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3251 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3252 if (non_default_args_count
> 0)
3253 non_default_args_count
+= num_packed
;
3257 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3261 if (non_default_args_count
>= 0)
3262 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3266 /* Checks if DECL shadows a template parameter.
3268 [temp.local]: A template-parameter shall not be redeclared within its
3269 scope (including nested scopes).
3271 Emits an error and returns TRUE if the DECL shadows a parameter,
3272 returns FALSE otherwise. */
3275 check_template_shadow (tree decl
)
3279 /* If we're not in a template, we can't possibly shadow a template
3281 if (!current_template_parms
)
3284 /* Figure out what we're shadowing. */
3285 if (TREE_CODE (decl
) == OVERLOAD
)
3286 decl
= OVL_CURRENT (decl
);
3287 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3289 /* If there's no previous binding for this name, we're not shadowing
3290 anything, let alone a template parameter. */
3294 /* If we're not shadowing a template parameter, we're done. Note
3295 that OLDDECL might be an OVERLOAD (or perhaps even an
3296 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3298 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3301 /* We check for decl != olddecl to avoid bogus errors for using a
3302 name inside a class. We check TPFI to avoid duplicate errors for
3303 inline member templates. */
3305 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
3308 error ("declaration of %q+#D", decl
);
3309 error (" shadows template parm %q+#D", olddecl
);
3313 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3314 ORIG_LEVEL, DECL, and TYPE. */
3317 build_template_parm_index (int index
,
3323 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3324 TEMPLATE_PARM_IDX (t
) = index
;
3325 TEMPLATE_PARM_LEVEL (t
) = level
;
3326 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3327 TEMPLATE_PARM_DECL (t
) = decl
;
3328 TREE_TYPE (t
) = type
;
3329 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3330 TREE_READONLY (t
) = TREE_READONLY (decl
);
3335 /* Find the canonical type parameter for the given template type
3336 parameter. Returns the canonical type parameter, which may be TYPE
3337 if no such parameter existed. */
3339 canonical_type_parameter (tree type
)
3342 int idx
= TEMPLATE_TYPE_IDX (type
);
3343 if (!canonical_template_parms
)
3344 canonical_template_parms
= VEC_alloc (tree
, gc
, idx
+1);
3346 while (VEC_length (tree
, canonical_template_parms
) <= (unsigned)idx
)
3347 VEC_safe_push (tree
, gc
, canonical_template_parms
, NULL_TREE
);
3349 list
= VEC_index (tree
, canonical_template_parms
, idx
);
3350 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3351 list
= TREE_CHAIN (list
);
3354 return TREE_VALUE (list
);
3357 VEC_replace(tree
, canonical_template_parms
, idx
,
3358 tree_cons (NULL_TREE
, type
,
3359 VEC_index (tree
, canonical_template_parms
, idx
)));
3364 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3365 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3366 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3367 new one is created. */
3370 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3371 tsubst_flags_t complain
)
3373 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3374 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3375 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3376 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3378 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3381 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3382 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3383 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3384 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3385 DECL_ARTIFICIAL (decl
) = 1;
3386 SET_DECL_TEMPLATE_PARM_P (decl
);
3388 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3389 TEMPLATE_PARM_LEVEL (index
) - levels
,
3390 TEMPLATE_PARM_ORIG_LEVEL (index
),
3392 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3393 TEMPLATE_PARM_PARAMETER_PACK (t
)
3394 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3396 /* Template template parameters need this. */
3397 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3398 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3399 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3403 return TEMPLATE_PARM_DESCENDANTS (index
);
3406 /* Process information from new template parameter PARM and append it to the
3407 LIST being built. This new parameter is a non-type parameter iff
3408 IS_NON_TYPE is true. This new parameter is a parameter
3409 pack iff IS_PARAMETER_PACK is true. The location of PARM is in
3413 process_template_parm (tree list
, location_t parm_loc
, tree parm
, bool is_non_type
,
3414 bool is_parameter_pack
)
3421 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3422 defval
= TREE_PURPOSE (parm
);
3426 tree p
= tree_last (list
);
3428 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3431 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3432 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3434 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3444 parm
= TREE_VALUE (parm
);
3446 SET_DECL_TEMPLATE_PARM_P (parm
);
3448 if (TREE_TYPE (parm
) == error_mark_node
)
3450 err_parm_list
= build_tree_list (defval
, parm
);
3451 TREE_VALUE (err_parm_list
) = error_mark_node
;
3452 return chainon (list
, err_parm_list
);
3458 The top-level cv-qualifiers on the template-parameter are
3459 ignored when determining its type. */
3460 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3461 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3463 err_parm_list
= build_tree_list (defval
, parm
);
3464 TREE_VALUE (err_parm_list
) = error_mark_node
;
3465 return chainon (list
, err_parm_list
);
3468 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
)
3470 /* This template parameter is not a parameter pack, but it
3471 should be. Complain about "bare" parameter packs. */
3472 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3474 /* Recover by calling this a parameter pack. */
3475 is_parameter_pack
= true;
3479 /* A template parameter is not modifiable. */
3480 TREE_CONSTANT (parm
) = 1;
3481 TREE_READONLY (parm
) = 1;
3482 decl
= build_decl (parm_loc
,
3483 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3484 TREE_CONSTANT (decl
) = 1;
3485 TREE_READONLY (decl
) = 1;
3486 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3487 = build_template_parm_index (idx
, processing_template_decl
,
3488 processing_template_decl
,
3489 decl
, TREE_TYPE (parm
));
3491 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3492 = is_parameter_pack
;
3497 parm
= TREE_VALUE (TREE_VALUE (parm
));
3499 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3501 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3502 /* This is for distinguishing between real templates and template
3503 template parameters */
3504 TREE_TYPE (parm
) = t
;
3505 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3510 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3511 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3512 decl
= build_decl (parm_loc
,
3513 TYPE_DECL
, parm
, t
);
3516 TYPE_NAME (t
) = decl
;
3517 TYPE_STUB_DECL (t
) = decl
;
3519 TEMPLATE_TYPE_PARM_INDEX (t
)
3520 = build_template_parm_index (idx
, processing_template_decl
,
3521 processing_template_decl
,
3522 decl
, TREE_TYPE (parm
));
3523 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3524 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3526 DECL_ARTIFICIAL (decl
) = 1;
3527 SET_DECL_TEMPLATE_PARM_P (decl
);
3529 parm
= build_tree_list (defval
, parm
);
3530 return chainon (list
, parm
);
3533 /* The end of a template parameter list has been reached. Process the
3534 tree list into a parameter vector, converting each parameter into a more
3535 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3539 end_template_parm_list (tree parms
)
3543 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3545 current_template_parms
3546 = tree_cons (size_int (processing_template_decl
),
3547 saved_parmlist
, current_template_parms
);
3549 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3551 next
= TREE_CHAIN (parm
);
3552 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3553 TREE_CHAIN (parm
) = NULL_TREE
;
3556 --processing_template_parmlist
;
3558 return saved_parmlist
;
3561 /* end_template_decl is called after a template declaration is seen. */
3564 end_template_decl (void)
3566 reset_specialization ();
3568 if (! processing_template_decl
)
3571 /* This matches the pushlevel in begin_template_parm_list. */
3574 --processing_template_decl
;
3575 current_template_parms
= TREE_CHAIN (current_template_parms
);
3578 /* Within the declaration of a template, return all levels of template
3579 parameters that apply. The template parameters are represented as
3580 a TREE_VEC, in the form documented in cp-tree.h for template
3584 current_template_args (void)
3587 tree args
= NULL_TREE
;
3588 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
3591 /* If there is only one level of template parameters, we do not
3592 create a TREE_VEC of TREE_VECs. Instead, we return a single
3593 TREE_VEC containing the arguments. */
3595 args
= make_tree_vec (length
);
3597 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
3599 tree a
= copy_node (TREE_VALUE (header
));
3602 TREE_TYPE (a
) = NULL_TREE
;
3603 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
3605 tree t
= TREE_VEC_ELT (a
, i
);
3607 /* T will be a list if we are called from within a
3608 begin/end_template_parm_list pair, but a vector directly
3609 if within a begin/end_member_template_processing pair. */
3610 if (TREE_CODE (t
) == TREE_LIST
)
3614 if (!error_operand_p (t
))
3616 if (TREE_CODE (t
) == TYPE_DECL
3617 || TREE_CODE (t
) == TEMPLATE_DECL
)
3621 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3623 /* Turn this argument into a TYPE_ARGUMENT_PACK
3624 with a single element, which expands T. */
3625 tree vec
= make_tree_vec (1);
3626 #ifdef ENABLE_CHECKING
3627 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3628 (vec
, TREE_VEC_LENGTH (vec
));
3630 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3632 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
3633 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3638 t
= DECL_INITIAL (t
);
3640 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3642 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3643 with a single element, which expands T. */
3644 tree vec
= make_tree_vec (1);
3645 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3646 #ifdef ENABLE_CHECKING
3647 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3648 (vec
, TREE_VEC_LENGTH (vec
));
3650 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3652 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3653 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3654 TREE_TYPE (t
) = type
;
3657 TREE_VEC_ELT (a
, i
) = t
;
3662 #ifdef ENABLE_CHECKING
3663 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
3667 TREE_VEC_ELT (args
, --l
) = a
;
3675 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3676 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3677 a member template. Used by push_template_decl below. */
3680 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
3682 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
3683 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
3684 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
3685 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
3690 struct template_parm_data
3692 /* The level of the template parameters we are currently
3696 /* The index of the specialization argument we are currently
3700 /* An array whose size is the number of template parameters. The
3701 elements are nonzero if the parameter has been used in any one
3702 of the arguments processed so far. */
3705 /* An array whose size is the number of template arguments. The
3706 elements are nonzero if the argument makes use of template
3707 parameters of this level. */
3708 int* arg_uses_template_parms
;
3711 /* Subroutine of push_template_decl used to see if each template
3712 parameter in a partial specialization is used in the explicit
3713 argument list. If T is of the LEVEL given in DATA (which is
3714 treated as a template_parm_data*), then DATA->PARMS is marked
3718 mark_template_parm (tree t
, void* data
)
3722 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
3724 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
3726 level
= TEMPLATE_PARM_LEVEL (t
);
3727 idx
= TEMPLATE_PARM_IDX (t
);
3731 level
= TEMPLATE_TYPE_LEVEL (t
);
3732 idx
= TEMPLATE_TYPE_IDX (t
);
3735 if (level
== tpd
->level
)
3737 tpd
->parms
[idx
] = 1;
3738 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
3741 /* Return zero so that for_each_template_parm will continue the
3742 traversal of the tree; we want to mark *every* template parm. */
3746 /* Process the partial specialization DECL. */
3749 process_partial_specialization (tree decl
)
3751 tree type
= TREE_TYPE (decl
);
3752 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
3753 tree specargs
= CLASSTYPE_TI_ARGS (type
);
3754 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
3755 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
3757 int nargs
= TREE_VEC_LENGTH (inner_args
);
3760 int did_error_intro
= 0;
3761 struct template_parm_data tpd
;
3762 struct template_parm_data tpd2
;
3764 gcc_assert (current_template_parms
);
3766 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
3767 ntparms
= TREE_VEC_LENGTH (inner_parms
);
3769 /* We check that each of the template parameters given in the
3770 partial specialization is used in the argument list to the
3771 specialization. For example:
3773 template <class T> struct S;
3774 template <class T> struct S<T*>;
3776 The second declaration is OK because `T*' uses the template
3777 parameter T, whereas
3779 template <class T> struct S<int>;
3781 is no good. Even trickier is:
3792 The S2<T> declaration is actually invalid; it is a
3793 full-specialization. Of course,
3796 struct S2<T (*)(U)>;
3798 or some such would have been OK. */
3799 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
3800 tpd
.parms
= (int *) alloca (sizeof (int) * ntparms
);
3801 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
3803 tpd
.arg_uses_template_parms
= (int *) alloca (sizeof (int) * nargs
);
3804 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
3805 for (i
= 0; i
< nargs
; ++i
)
3807 tpd
.current_arg
= i
;
3808 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
3809 &mark_template_parm
,
3812 /*include_nondeduced_p=*/false);
3814 for (i
= 0; i
< ntparms
; ++i
)
3815 if (tpd
.parms
[i
] == 0)
3817 /* One of the template parms was not used in the
3819 if (!did_error_intro
)
3821 error ("template parameters not used in partial specialization:");
3822 did_error_intro
= 1;
3825 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
3828 /* [temp.class.spec]
3830 The argument list of the specialization shall not be identical to
3831 the implicit argument list of the primary template. */
3832 if (comp_template_args
3834 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3836 error ("partial specialization %qT does not specialize any template arguments", type
);
3838 /* [temp.class.spec]
3840 A partially specialized non-type argument expression shall not
3841 involve template parameters of the partial specialization except
3842 when the argument expression is a simple identifier.
3844 The type of a template parameter corresponding to a specialized
3845 non-type argument shall not be dependent on a parameter of the
3848 Also, we verify that pack expansions only occur at the
3849 end of the argument list. */
3850 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
3852 for (i
= 0; i
< nargs
; ++i
)
3854 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
3855 tree arg
= TREE_VEC_ELT (inner_args
, i
);
3856 tree packed_args
= NULL_TREE
;
3859 if (ARGUMENT_PACK_P (arg
))
3861 /* Extract the arguments from the argument pack. We'll be
3862 iterating over these in the following loop. */
3863 packed_args
= ARGUMENT_PACK_ARGS (arg
);
3864 len
= TREE_VEC_LENGTH (packed_args
);
3867 for (j
= 0; j
< len
; j
++)
3870 /* Get the Jth argument in the parameter pack. */
3871 arg
= TREE_VEC_ELT (packed_args
, j
);
3873 if (PACK_EXPANSION_P (arg
))
3875 /* Pack expansions must come at the end of the
3877 if ((packed_args
&& j
< len
- 1)
3878 || (!packed_args
&& i
< nargs
- 1))
3880 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
3881 error ("parameter pack argument %qE must be at the "
3882 "end of the template argument list", arg
);
3884 error ("parameter pack argument %qT must be at the "
3885 "end of the template argument list", arg
);
3889 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
3890 /* We only care about the pattern. */
3891 arg
= PACK_EXPANSION_PATTERN (arg
);
3893 if (/* These first two lines are the `non-type' bit. */
3895 && TREE_CODE (arg
) != TEMPLATE_DECL
3896 /* This next line is the `argument expression is not just a
3897 simple identifier' condition and also the `specialized
3898 non-type argument' bit. */
3899 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
3901 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
3902 || (packed_args
&& uses_template_parms (arg
)))
3903 error ("template argument %qE involves template parameter(s)",
3907 /* Look at the corresponding template parameter,
3908 marking which template parameters its type depends
3910 tree type
= TREE_TYPE (parm
);
3914 /* We haven't yet initialized TPD2. Do so now. */
3915 tpd2
.arg_uses_template_parms
3916 = (int *) alloca (sizeof (int) * nargs
);
3917 /* The number of parameters here is the number in the
3918 main template, which, as checked in the assertion
3920 tpd2
.parms
= (int *) alloca (sizeof (int) * nargs
);
3922 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
3925 /* Mark the template parameters. But this time, we're
3926 looking for the template parameters of the main
3927 template, not in the specialization. */
3928 tpd2
.current_arg
= i
;
3929 tpd2
.arg_uses_template_parms
[i
] = 0;
3930 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
3931 for_each_template_parm (type
,
3932 &mark_template_parm
,
3935 /*include_nondeduced_p=*/false);
3937 if (tpd2
.arg_uses_template_parms
[i
])
3939 /* The type depended on some template parameters.
3940 If they are fully specialized in the
3941 specialization, that's OK. */
3943 for (j
= 0; j
< nargs
; ++j
)
3944 if (tpd2
.parms
[j
] != 0
3945 && tpd
.arg_uses_template_parms
[j
])
3947 error ("type %qT of template argument %qE depends "
3948 "on template parameter(s)",
3959 /* We should only get here once. */
3960 gcc_assert (!COMPLETE_TYPE_P (type
));
3962 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
3963 = tree_cons (specargs
, inner_parms
,
3964 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
3965 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
3969 /* Check that a template declaration's use of default arguments and
3970 parameter packs is not invalid. Here, PARMS are the template
3971 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
3972 a primary template. IS_PARTIAL is nonzero if DECL is a partial
3976 IS_FRIEND_DECL is nonzero if DECL is a friend function template
3977 declaration (but not a definition); 1 indicates a declaration, 2
3978 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
3979 emitted for extraneous default arguments.
3981 Returns TRUE if there were no errors found, FALSE otherwise. */
3984 check_default_tmpl_args (tree decl
, tree parms
, int is_primary
,
3985 int is_partial
, int is_friend_decl
)
3988 int last_level_to_check
;
3990 bool no_errors
= true;
3994 A default template-argument shall not be specified in a
3995 function template declaration or a function template definition, nor
3996 in the template-parameter-list of the definition of a member of a
3999 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
4000 /* You can't have a function template declaration in a local
4001 scope, nor you can you define a member of a class template in a
4005 if (current_class_type
4006 && !TYPE_BEING_DEFINED (current_class_type
)
4007 && DECL_LANG_SPECIFIC (decl
)
4008 && DECL_DECLARES_FUNCTION_P (decl
)
4009 /* If this is either a friend defined in the scope of the class
4010 or a member function. */
4011 && (DECL_FUNCTION_MEMBER_P (decl
)
4012 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4013 : DECL_FRIEND_CONTEXT (decl
)
4014 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4016 /* And, if it was a member function, it really was defined in
4017 the scope of the class. */
4018 && (!DECL_FUNCTION_MEMBER_P (decl
)
4019 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4020 /* We already checked these parameters when the template was
4021 declared, so there's no need to do it again now. This function
4022 was defined in class scope, but we're processing it's body now
4023 that the class is complete. */
4026 /* Core issue 226 (C++0x only): the following only applies to class
4028 if ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
)
4032 If a template-parameter has a default template-argument, all
4033 subsequent template-parameters shall have a default
4034 template-argument supplied. */
4035 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4037 tree inner_parms
= TREE_VALUE (parm_level
);
4038 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4039 int seen_def_arg_p
= 0;
4042 for (i
= 0; i
< ntparms
; ++i
)
4044 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4046 if (parm
== error_mark_node
)
4049 if (TREE_PURPOSE (parm
))
4051 else if (seen_def_arg_p
4052 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4054 error ("no default argument for %qD", TREE_VALUE (parm
));
4055 /* For better subsequent error-recovery, we indicate that
4056 there should have been a default argument. */
4057 TREE_PURPOSE (parm
) = error_mark_node
;
4063 /* Don't complain about an enclosing partial
4065 && parm_level
== parms
4066 && TREE_CODE (decl
) == TYPE_DECL
4068 && template_parameter_pack_p (TREE_VALUE (parm
)))
4070 /* A primary class template can only have one
4071 parameter pack, at the end of the template
4074 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
4075 error ("parameter pack %qE must be at the end of the"
4076 " template parameter list", TREE_VALUE (parm
));
4078 error ("parameter pack %qT must be at the end of the"
4079 " template parameter list",
4080 TREE_TYPE (TREE_VALUE (parm
)));
4082 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4090 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4094 /* For an ordinary class template, default template arguments are
4095 allowed at the innermost level, e.g.:
4096 template <class T = int>
4098 but, in a partial specialization, they're not allowed even
4099 there, as we have in [temp.class.spec]:
4101 The template parameter list of a specialization shall not
4102 contain default template argument values.
4104 So, for a partial specialization, or for a function template
4105 (in C++98/C++03), we look at all of them. */
4108 /* But, for a primary class template that is not a partial
4109 specialization we look at all template parameters except the
4111 parms
= TREE_CHAIN (parms
);
4113 /* Figure out what error message to issue. */
4114 if (is_friend_decl
== 2)
4115 msg
= G_("default template arguments may not be used in function template "
4116 "friend re-declaration");
4117 else if (is_friend_decl
)
4118 msg
= G_("default template arguments may not be used in function template "
4119 "friend declarations");
4120 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4121 msg
= G_("default template arguments may not be used in function templates "
4122 "without -std=c++0x or -std=gnu++0x");
4123 else if (is_partial
)
4124 msg
= G_("default template arguments may not be used in "
4125 "partial specializations");
4127 msg
= G_("default argument for template parameter for class enclosing %qD");
4129 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4130 /* If we're inside a class definition, there's no need to
4131 examine the parameters to the class itself. On the one
4132 hand, they will be checked when the class is defined, and,
4133 on the other, default arguments are valid in things like:
4134 template <class T = double>
4135 struct S { template <class U> void f(U); };
4136 Here the default argument for `S' has no bearing on the
4137 declaration of `f'. */
4138 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4140 /* Check everything. */
4141 last_level_to_check
= 0;
4143 for (parm_level
= parms
;
4144 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4145 parm_level
= TREE_CHAIN (parm_level
))
4147 tree inner_parms
= TREE_VALUE (parm_level
);
4151 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4152 for (i
= 0; i
< ntparms
; ++i
)
4154 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4157 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4162 if (is_friend_decl
== 2)
4169 /* Clear out the default argument so that we are not
4171 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4175 /* At this point, if we're still interested in issuing messages,
4176 they must apply to classes surrounding the object declared. */
4178 msg
= G_("default argument for template parameter for class "
4185 /* Worker for push_template_decl_real, called via
4186 for_each_template_parm. DATA is really an int, indicating the
4187 level of the parameters we are interested in. If T is a template
4188 parameter of that level, return nonzero. */
4191 template_parm_this_level_p (tree t
, void* data
)
4193 int this_level
= *(int *)data
;
4196 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4197 level
= TEMPLATE_PARM_LEVEL (t
);
4199 level
= TEMPLATE_TYPE_LEVEL (t
);
4200 return level
== this_level
;
4203 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4204 parameters given by current_template_args, or reuses a
4205 previously existing one, if appropriate. Returns the DECL, or an
4206 equivalent one, if it is replaced via a call to duplicate_decls.
4208 If IS_FRIEND is true, DECL is a friend declaration. */
4211 push_template_decl_real (tree decl
, bool is_friend
)
4219 int new_template_p
= 0;
4220 /* True if the template is a member template, in the sense of
4222 bool member_template_p
= false;
4224 if (decl
== error_mark_node
|| !current_template_parms
)
4225 return error_mark_node
;
4227 /* See if this is a partial specialization. */
4228 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
4229 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4230 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
4232 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4236 /* For a friend, we want the context of the friend function, not
4237 the type of which it is a friend. */
4238 ctx
= DECL_CONTEXT (decl
);
4239 else if (CP_DECL_CONTEXT (decl
)
4240 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4241 /* In the case of a virtual function, we want the class in which
4243 ctx
= CP_DECL_CONTEXT (decl
);
4245 /* Otherwise, if we're currently defining some class, the DECL
4246 is assumed to be a member of the class. */
4247 ctx
= current_scope ();
4249 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4252 if (!DECL_CONTEXT (decl
))
4253 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4255 /* See if this is a primary template. */
4256 if (is_friend
&& ctx
)
4257 /* A friend template that specifies a class context, i.e.
4258 template <typename T> friend void A<T>::f();
4262 primary
= template_parm_scope_p ();
4266 if (DECL_CLASS_SCOPE_P (decl
))
4267 member_template_p
= true;
4268 if (TREE_CODE (decl
) == TYPE_DECL
4269 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4271 error ("template class without a name");
4272 return error_mark_node
;
4274 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4276 if (DECL_DESTRUCTOR_P (decl
))
4280 A destructor shall not be a member template. */
4281 error ("destructor %qD declared as member template", decl
);
4282 return error_mark_node
;
4284 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4285 && (!TYPE_ARG_TYPES (TREE_TYPE (decl
))
4286 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4287 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4288 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4289 == void_list_node
)))
4291 /* [basic.stc.dynamic.allocation]
4293 An allocation function can be a function
4294 template. ... Template allocation functions shall
4295 have two or more parameters. */
4296 error ("invalid template declaration of %qD", decl
);
4297 return error_mark_node
;
4300 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4301 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4305 error ("template declaration of %q#D", decl
);
4306 return error_mark_node
;
4310 /* Check to see that the rules regarding the use of default
4311 arguments are not being violated. */
4312 check_default_tmpl_args (decl
, current_template_parms
,
4313 primary
, is_partial
, /*is_friend_decl=*/0);
4315 /* Ensure that there are no parameter packs in the type of this
4316 declaration that have not been expanded. */
4317 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4319 /* Check each of the arguments individually to see if there are
4320 any bare parameter packs. */
4321 tree type
= TREE_TYPE (decl
);
4322 tree arg
= DECL_ARGUMENTS (decl
);
4323 tree argtype
= TYPE_ARG_TYPES (type
);
4325 while (arg
&& argtype
)
4327 if (!FUNCTION_PARAMETER_PACK_P (arg
)
4328 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4330 /* This is a PARM_DECL that contains unexpanded parameter
4331 packs. We have already complained about this in the
4332 check_for_bare_parameter_packs call, so just replace
4333 these types with ERROR_MARK_NODE. */
4334 TREE_TYPE (arg
) = error_mark_node
;
4335 TREE_VALUE (argtype
) = error_mark_node
;
4338 arg
= TREE_CHAIN (arg
);
4339 argtype
= TREE_CHAIN (argtype
);
4342 /* Check for bare parameter packs in the return type and the
4343 exception specifiers. */
4344 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4345 /* Errors were already issued, set return type to int
4346 as the frontend doesn't expect error_mark_node as
4348 TREE_TYPE (type
) = integer_type_node
;
4349 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4350 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4352 else if (check_for_bare_parameter_packs (TREE_TYPE (decl
)))
4354 TREE_TYPE (decl
) = error_mark_node
;
4355 return error_mark_node
;
4359 return process_partial_specialization (decl
);
4361 args
= current_template_args ();
4364 || TREE_CODE (ctx
) == FUNCTION_DECL
4365 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4366 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4368 if (DECL_LANG_SPECIFIC (decl
)
4369 && DECL_TEMPLATE_INFO (decl
)
4370 && DECL_TI_TEMPLATE (decl
))
4371 tmpl
= DECL_TI_TEMPLATE (decl
);
4372 /* If DECL is a TYPE_DECL for a class-template, then there won't
4373 be DECL_LANG_SPECIFIC. The information equivalent to
4374 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4375 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4376 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4377 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4379 /* Since a template declaration already existed for this
4380 class-type, we must be redeclaring it here. Make sure
4381 that the redeclaration is valid. */
4382 redeclare_class_template (TREE_TYPE (decl
),
4383 current_template_parms
);
4384 /* We don't need to create a new TEMPLATE_DECL; just use the
4385 one we already had. */
4386 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4390 tmpl
= build_template_decl (decl
, current_template_parms
,
4394 if (DECL_LANG_SPECIFIC (decl
)
4395 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4397 /* A specialization of a member template of a template
4399 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4400 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4401 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4407 tree a
, t
, current
, parms
;
4409 tree tinfo
= get_template_info (decl
);
4413 error ("template definition of non-template %q#D", decl
);
4414 return error_mark_node
;
4417 tmpl
= TI_TEMPLATE (tinfo
);
4419 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4420 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4421 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4422 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4426 /* The declaration is a specialization of a member
4427 template, declared outside the class. Therefore, the
4428 innermost template arguments will be NULL, so we
4429 replace them with the arguments determined by the
4430 earlier call to check_explicit_specialization. */
4431 args
= DECL_TI_ARGS (decl
);
4434 = build_template_decl (decl
, current_template_parms
,
4436 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4437 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4438 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4439 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4440 DECL_TEMPLATE_INFO (new_tmpl
)
4441 = build_template_info (tmpl
, args
);
4443 register_specialization (new_tmpl
,
4444 most_general_template (tmpl
),
4450 /* Make sure the template headers we got make sense. */
4452 parms
= DECL_TEMPLATE_PARMS (tmpl
);
4453 i
= TMPL_PARMS_DEPTH (parms
);
4454 if (TMPL_ARGS_DEPTH (args
) != i
)
4456 error ("expected %d levels of template parms for %q#D, got %d",
4457 i
, decl
, TMPL_ARGS_DEPTH (args
));
4460 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
4462 a
= TMPL_ARGS_LEVEL (args
, i
);
4463 t
= INNERMOST_TEMPLATE_PARMS (parms
);
4465 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
4467 if (current
== decl
)
4468 error ("got %d template parameters for %q#D",
4469 TREE_VEC_LENGTH (a
), decl
);
4471 error ("got %d template parameters for %q#T",
4472 TREE_VEC_LENGTH (a
), current
);
4473 error (" but %d required", TREE_VEC_LENGTH (t
));
4474 return error_mark_node
;
4477 if (current
== decl
)
4480 current
= (TYPE_P (current
)
4481 ? TYPE_CONTEXT (current
)
4482 : DECL_CONTEXT (current
));
4485 /* Check that the parms are used in the appropriate qualifying scopes
4486 in the declarator. */
4487 if (!comp_template_args
4489 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
4492 template arguments to %qD do not match original template %qD",
4493 decl
, DECL_TEMPLATE_RESULT (tmpl
));
4494 if (!uses_template_parms (TI_ARGS (tinfo
)))
4495 inform (input_location
, "use template<> for an explicit specialization");
4496 /* Avoid crash in import_export_decl. */
4497 DECL_INTERFACE_KNOWN (decl
) = 1;
4498 return error_mark_node
;
4502 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4503 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4505 /* Push template declarations for global functions and types. Note
4506 that we do not try to push a global template friend declared in a
4507 template class; such a thing may well depend on the template
4508 parameters of the class. */
4509 if (new_template_p
&& !ctx
4510 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
4512 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
4513 if (tmpl
== error_mark_node
)
4514 return error_mark_node
;
4516 /* Hide template friend classes that haven't been declared yet. */
4517 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
4519 DECL_ANTICIPATED (tmpl
) = 1;
4520 DECL_FRIEND_P (tmpl
) = 1;
4526 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
4529 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4530 if (DECL_CONV_FN_P (tmpl
))
4532 int depth
= TMPL_PARMS_DEPTH (parms
);
4534 /* It is a conversion operator. See if the type converted to
4535 depends on innermost template operands. */
4537 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
4539 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
4542 /* Give template template parms a DECL_CONTEXT of the template
4543 for which they are a parameter. */
4544 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4545 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
4547 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4548 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4549 DECL_CONTEXT (parm
) = tmpl
;
4551 if (TREE_CODE (TREE_TYPE (parm
)) == TEMPLATE_TYPE_PARM
)
4552 DECL_CONTEXT (TYPE_NAME (TREE_TYPE (parm
))) = tmpl
;
4556 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4557 back to its most general template. If TMPL is a specialization,
4558 ARGS may only have the innermost set of arguments. Add the missing
4559 argument levels if necessary. */
4560 if (DECL_TEMPLATE_INFO (tmpl
))
4561 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
4563 info
= build_template_info (tmpl
, args
);
4565 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
4566 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
4567 else if (DECL_LANG_SPECIFIC (decl
))
4568 DECL_TEMPLATE_INFO (decl
) = info
;
4570 return DECL_TEMPLATE_RESULT (tmpl
);
4574 push_template_decl (tree decl
)
4576 return push_template_decl_real (decl
, false);
4579 /* Called when a class template TYPE is redeclared with the indicated
4580 template PARMS, e.g.:
4582 template <class T> struct S;
4583 template <class T> struct S {}; */
4586 redeclare_class_template (tree type
, tree parms
)
4592 if (!TYPE_TEMPLATE_INFO (type
))
4594 error ("%qT is not a template type", type
);
4598 tmpl
= TYPE_TI_TEMPLATE (type
);
4599 if (!PRIMARY_TEMPLATE_P (tmpl
))
4600 /* The type is nested in some template class. Nothing to worry
4601 about here; there are no new template parameters for the nested
4607 error ("template specifiers not specified in declaration of %qD",
4612 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4613 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
4615 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
4617 error ("redeclared with %d template parameter(s)",
4618 TREE_VEC_LENGTH (parms
));
4619 inform (input_location
, "previous declaration %q+D used %d template parameter(s)",
4620 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
4624 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
4631 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
4632 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
4635 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
4636 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4637 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
4638 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
4640 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4642 if (tmpl_parm
!= error_mark_node
4643 && (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
4644 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
4645 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
4646 || (TREE_CODE (tmpl_parm
) != PARM_DECL
4647 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
4648 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
4649 || (TREE_CODE (tmpl_parm
) == PARM_DECL
4650 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
4651 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))))))
4653 error ("template parameter %q+#D", tmpl_parm
);
4654 error ("redeclared here as %q#D", parm
);
4658 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
4660 /* We have in [temp.param]:
4662 A template-parameter may not be given default arguments
4663 by two different declarations in the same scope. */
4664 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
4665 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
4666 "original definition appeared here");
4670 if (parm_default
!= NULL_TREE
)
4671 /* Update the previous template parameters (which are the ones
4672 that will really count) with the new default value. */
4673 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
4674 else if (tmpl_default
!= NULL_TREE
)
4675 /* Update the new parameters, too; they'll be used as the
4676 parameters for any members. */
4677 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
4683 /* Simplify EXPR if it is a non-dependent expression. Returns the
4684 (possibly simplified) expression. */
4687 fold_non_dependent_expr (tree expr
)
4689 if (expr
== NULL_TREE
)
4692 /* If we're in a template, but EXPR isn't value dependent, simplify
4693 it. We're supposed to treat:
4695 template <typename T> void f(T[1 + 1]);
4696 template <typename T> void f(T[2]);
4698 as two declarations of the same function, for example. */
4699 if (processing_template_decl
4700 && !type_dependent_expression_p (expr
)
4701 && !value_dependent_expression_p (expr
))
4703 HOST_WIDE_INT saved_processing_template_decl
;
4705 saved_processing_template_decl
= processing_template_decl
;
4706 processing_template_decl
= 0;
4707 expr
= tsubst_copy_and_build (expr
,
4710 /*in_decl=*/NULL_TREE
,
4711 /*function_p=*/false,
4712 /*integral_constant_expression_p=*/true);
4713 processing_template_decl
= saved_processing_template_decl
;
4718 /* EXPR is an expression which is used in a constant-expression context.
4719 For instance, it could be a VAR_DECL with a constant initializer.
4720 Extract the innermost constant expression.
4722 This is basically a more powerful version of
4723 integral_constant_value, which can be used also in templates where
4724 initializers can maintain a syntactic rather than semantic form
4725 (even if they are non-dependent, for access-checking purposes). */
4728 fold_decl_constant_value (tree expr
)
4730 tree const_expr
= expr
;
4733 expr
= fold_non_dependent_expr (const_expr
);
4734 const_expr
= integral_constant_value (expr
);
4736 while (expr
!= const_expr
);
4741 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4742 must be a function or a pointer-to-function type, as specified
4743 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4744 and check that the resulting function has external linkage. */
4747 convert_nontype_argument_function (tree type
, tree expr
)
4752 fn
= instantiate_type (type
, fns
, tf_none
);
4753 if (fn
== error_mark_node
)
4754 return error_mark_node
;
4757 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
4758 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
4759 if (TREE_CODE (fn_no_ptr
) == BASELINK
)
4760 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
4762 /* [temp.arg.nontype]/1
4764 A template-argument for a non-type, non-template template-parameter
4767 -- the address of an object or function with external linkage. */
4768 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr
))
4770 error ("%qE is not a valid template argument for type %qT "
4771 "because function %qD has not external linkage",
4772 expr
, type
, fn_no_ptr
);
4779 /* Subroutine of convert_nontype_argument.
4780 Check if EXPR of type TYPE is a valid pointer-to-member constant.
4781 Emit an error otherwise. */
4784 check_valid_ptrmem_cst_expr (tree type
, tree expr
)
4787 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
4789 error ("%qE is not a valid template argument for type %qT",
4791 error ("it must be a pointer-to-member of the form `&X::Y'");
4795 /* Attempt to convert the non-type template parameter EXPR to the
4796 indicated TYPE. If the conversion is successful, return the
4797 converted value. If the conversion is unsuccessful, return
4798 NULL_TREE if we issued an error message, or error_mark_node if we
4799 did not. We issue error messages for out-and-out bad template
4800 parameters, but not simply because the conversion failed, since we
4801 might be just trying to do argument deduction. Both TYPE and EXPR
4802 must be non-dependent.
4804 The conversion follows the special rules described in
4805 [temp.arg.nontype], and it is much more strict than an implicit
4808 This function is called twice for each template argument (see
4809 lookup_template_class for a more accurate description of this
4810 problem). This means that we need to handle expressions which
4811 are not valid in a C++ source, but can be created from the
4812 first call (for instance, casts to perform conversions). These
4813 hacks can go away after we fix the double coercion problem. */
4816 convert_nontype_argument (tree type
, tree expr
)
4820 /* Detect immediately string literals as invalid non-type argument.
4821 This special-case is not needed for correctness (we would easily
4822 catch this later), but only to provide better diagnostic for this
4823 common user mistake. As suggested by DR 100, we do not mention
4824 linkage issues in the diagnostic as this is not the point. */
4825 if (TREE_CODE (expr
) == STRING_CST
)
4827 error ("%qE is not a valid template argument for type %qT "
4828 "because string literals can never be used in this context",
4833 /* If we are in a template, EXPR may be non-dependent, but still
4834 have a syntactic, rather than semantic, form. For example, EXPR
4835 might be a SCOPE_REF, rather than the VAR_DECL to which the
4836 SCOPE_REF refers. Preserving the qualifying scope is necessary
4837 so that access checking can be performed when the template is
4838 instantiated -- but here we need the resolved form so that we can
4839 convert the argument. */
4840 expr
= fold_non_dependent_expr (expr
);
4841 if (error_operand_p (expr
))
4842 return error_mark_node
;
4843 expr_type
= TREE_TYPE (expr
);
4845 /* HACK: Due to double coercion, we can get a
4846 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
4847 which is the tree that we built on the first call (see
4848 below when coercing to reference to object or to reference to
4849 function). We just strip everything and get to the arg.
4850 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
4852 if (TREE_CODE (expr
) == NOP_EXPR
)
4854 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
4856 /* ??? Maybe we could use convert_from_reference here, but we
4857 would need to relax its constraints because the NOP_EXPR
4858 could actually change the type to something more cv-qualified,
4859 and this is not folded by convert_from_reference. */
4860 tree addr
= TREE_OPERAND (expr
, 0);
4861 gcc_assert (TREE_CODE (expr_type
) == REFERENCE_TYPE
);
4862 gcc_assert (TREE_CODE (addr
) == ADDR_EXPR
);
4863 gcc_assert (TREE_CODE (TREE_TYPE (addr
)) == POINTER_TYPE
);
4864 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4865 (TREE_TYPE (expr_type
),
4866 TREE_TYPE (TREE_TYPE (addr
))));
4868 expr
= TREE_OPERAND (addr
, 0);
4869 expr_type
= TREE_TYPE (expr
);
4872 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
4873 parameter is a pointer to object, through decay and
4874 qualification conversion. Let's strip everything. */
4875 else if (TYPE_PTROBV_P (type
))
4878 gcc_assert (TREE_CODE (expr
) == ADDR_EXPR
);
4879 gcc_assert (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
);
4880 /* Skip the ADDR_EXPR only if it is part of the decay for
4881 an array. Otherwise, it is part of the original argument
4882 in the source code. */
4883 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == ARRAY_TYPE
)
4884 expr
= TREE_OPERAND (expr
, 0);
4885 expr_type
= TREE_TYPE (expr
);
4889 /* [temp.arg.nontype]/5, bullet 1
4891 For a non-type template-parameter of integral or enumeration type,
4892 integral promotions (_conv.prom_) and integral conversions
4893 (_conv.integral_) are applied. */
4894 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
4896 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (expr_type
))
4897 return error_mark_node
;
4899 expr
= fold_decl_constant_value (expr
);
4900 /* Notice that there are constant expressions like '4 % 0' which
4901 do not fold into integer constants. */
4902 if (TREE_CODE (expr
) != INTEGER_CST
)
4904 error ("%qE is not a valid template argument for type %qT "
4905 "because it is a non-constant expression", expr
, type
);
4909 /* At this point, an implicit conversion does what we want,
4910 because we already know that the expression is of integral
4912 expr
= ocp_convert (type
, expr
, CONV_IMPLICIT
, LOOKUP_PROTECT
);
4913 if (expr
== error_mark_node
)
4914 return error_mark_node
;
4916 /* Conversion was allowed: fold it to a bare integer constant. */
4919 /* [temp.arg.nontype]/5, bullet 2
4921 For a non-type template-parameter of type pointer to object,
4922 qualification conversions (_conv.qual_) and the array-to-pointer
4923 conversion (_conv.array_) are applied. */
4924 else if (TYPE_PTROBV_P (type
))
4926 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
4928 A template-argument for a non-type, non-template template-parameter
4929 shall be one of: [...]
4931 -- the name of a non-type template-parameter;
4932 -- the address of an object or function with external linkage, [...]
4933 expressed as "& id-expression" where the & is optional if the name
4934 refers to a function or array, or if the corresponding
4935 template-parameter is a reference.
4937 Here, we do not care about functions, as they are invalid anyway
4938 for a parameter of type pointer-to-object. */
4940 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
4941 /* Non-type template parameters are OK. */
4943 else if (TREE_CODE (expr
) != ADDR_EXPR
4944 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
4946 if (TREE_CODE (expr
) == VAR_DECL
)
4948 error ("%qD is not a valid template argument "
4949 "because %qD is a variable, not the address of "
4954 /* Other values, like integer constants, might be valid
4955 non-type arguments of some other type. */
4956 return error_mark_node
;
4962 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
4963 ? TREE_OPERAND (expr
, 0) : expr
);
4964 if (TREE_CODE (decl
) != VAR_DECL
)
4966 error ("%qE is not a valid template argument of type %qT "
4967 "because %qE is not a variable",
4971 else if (!DECL_EXTERNAL_LINKAGE_P (decl
))
4973 error ("%qE is not a valid template argument of type %qT "
4974 "because %qD does not have external linkage",
4980 expr
= decay_conversion (expr
);
4981 if (expr
== error_mark_node
)
4982 return error_mark_node
;
4984 expr
= perform_qualification_conversions (type
, expr
);
4985 if (expr
== error_mark_node
)
4986 return error_mark_node
;
4988 /* [temp.arg.nontype]/5, bullet 3
4990 For a non-type template-parameter of type reference to object, no
4991 conversions apply. The type referred to by the reference may be more
4992 cv-qualified than the (otherwise identical) type of the
4993 template-argument. The template-parameter is bound directly to the
4994 template-argument, which must be an lvalue. */
4995 else if (TYPE_REF_OBJ_P (type
))
4997 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
4999 return error_mark_node
;
5001 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5003 error ("%qE is not a valid template argument for type %qT "
5004 "because of conflicts in cv-qualification", expr
, type
);
5008 if (!real_lvalue_p (expr
))
5010 error ("%qE is not a valid template argument for type %qT "
5011 "because it is not an lvalue", expr
, type
);
5015 /* [temp.arg.nontype]/1
5017 A template-argument for a non-type, non-template template-parameter
5018 shall be one of: [...]
5020 -- the address of an object or function with external linkage. */
5021 if (TREE_CODE (expr
) == INDIRECT_REF
5022 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5024 expr
= TREE_OPERAND (expr
, 0);
5027 error ("%q#D is not a valid template argument for type %qT "
5028 "because a reference variable does not have a constant "
5029 "address", expr
, type
);
5036 error ("%qE is not a valid template argument for type %qT "
5037 "because it is not an object with external linkage",
5042 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5044 error ("%qE is not a valid template argument for type %qT "
5045 "because object %qD has not external linkage",
5050 expr
= build_nop (type
, build_address (expr
));
5052 /* [temp.arg.nontype]/5, bullet 4
5054 For a non-type template-parameter of type pointer to function, only
5055 the function-to-pointer conversion (_conv.func_) is applied. If the
5056 template-argument represents a set of overloaded functions (or a
5057 pointer to such), the matching function is selected from the set
5059 else if (TYPE_PTRFN_P (type
))
5061 /* If the argument is a template-id, we might not have enough
5062 context information to decay the pointer. */
5063 if (!type_unknown_p (expr_type
))
5065 expr
= decay_conversion (expr
);
5066 if (expr
== error_mark_node
)
5067 return error_mark_node
;
5070 expr
= convert_nontype_argument_function (type
, expr
);
5071 if (!expr
|| expr
== error_mark_node
)
5074 if (TREE_CODE (expr
) != ADDR_EXPR
)
5076 error ("%qE is not a valid template argument for type %qT", expr
, type
);
5077 error ("it must be the address of a function with external linkage");
5081 /* [temp.arg.nontype]/5, bullet 5
5083 For a non-type template-parameter of type reference to function, no
5084 conversions apply. If the template-argument represents a set of
5085 overloaded functions, the matching function is selected from the set
5087 else if (TYPE_REFFN_P (type
))
5089 if (TREE_CODE (expr
) == ADDR_EXPR
)
5091 error ("%qE is not a valid template argument for type %qT "
5092 "because it is a pointer", expr
, type
);
5093 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
5097 expr
= convert_nontype_argument_function (TREE_TYPE (type
), expr
);
5098 if (!expr
|| expr
== error_mark_node
)
5101 expr
= build_nop (type
, build_address (expr
));
5103 /* [temp.arg.nontype]/5, bullet 6
5105 For a non-type template-parameter of type pointer to member function,
5106 no conversions apply. If the template-argument represents a set of
5107 overloaded member functions, the matching member function is selected
5108 from the set (_over.over_). */
5109 else if (TYPE_PTRMEMFUNC_P (type
))
5111 expr
= instantiate_type (type
, expr
, tf_none
);
5112 if (expr
== error_mark_node
)
5113 return error_mark_node
;
5115 /* [temp.arg.nontype] bullet 1 says the pointer to member
5116 expression must be a pointer-to-member constant. */
5117 if (!check_valid_ptrmem_cst_expr (type
, expr
))
5118 return error_mark_node
;
5120 /* There is no way to disable standard conversions in
5121 resolve_address_of_overloaded_function (called by
5122 instantiate_type). It is possible that the call succeeded by
5123 converting &B::I to &D::I (where B is a base of D), so we need
5124 to reject this conversion here.
5126 Actually, even if there was a way to disable standard conversions,
5127 it would still be better to reject them here so that we can
5128 provide a superior diagnostic. */
5129 if (!same_type_p (TREE_TYPE (expr
), type
))
5131 /* Make sure we are just one standard conversion off. */
5132 gcc_assert (can_convert (type
, TREE_TYPE (expr
)));
5133 error ("%qE is not a valid template argument for type %qT "
5134 "because it is of type %qT", expr
, type
,
5136 inform (input_location
, "standard conversions are not allowed in this context");
5140 /* [temp.arg.nontype]/5, bullet 7
5142 For a non-type template-parameter of type pointer to data member,
5143 qualification conversions (_conv.qual_) are applied. */
5144 else if (TYPE_PTRMEM_P (type
))
5146 /* [temp.arg.nontype] bullet 1 says the pointer to member
5147 expression must be a pointer-to-member constant. */
5148 if (!check_valid_ptrmem_cst_expr (type
, expr
))
5149 return error_mark_node
;
5151 expr
= perform_qualification_conversions (type
, expr
);
5152 if (expr
== error_mark_node
)
5155 /* A template non-type parameter must be one of the above. */
5159 /* Sanity check: did we actually convert the argument to the
5161 gcc_assert (same_type_p (type
, TREE_TYPE (expr
)));
5165 /* Subroutine of coerce_template_template_parms, which returns 1 if
5166 PARM_PARM and ARG_PARM match using the rule for the template
5167 parameters of template template parameters. Both PARM and ARG are
5168 template parameters; the rest of the arguments are the same as for
5169 coerce_template_template_parms.
5172 coerce_template_template_parm (tree parm
,
5174 tsubst_flags_t complain
,
5178 if (arg
== NULL_TREE
|| arg
== error_mark_node
5179 || parm
== NULL_TREE
|| parm
== error_mark_node
)
5182 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5185 switch (TREE_CODE (parm
))
5188 /* We encounter instantiations of templates like
5189 template <template <template <class> class> class TT>
5192 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
5193 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
5195 if (!coerce_template_template_parms
5196 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
5202 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
5203 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5204 /* Argument is a parameter pack but parameter is not. */
5209 /* The tsubst call is used to handle cases such as
5211 template <int> class C {};
5212 template <class T, template <T> class TT> class D {};
5215 i.e. the parameter list of TT depends on earlier parameters. */
5216 if (!uses_template_parms (TREE_TYPE (arg
))
5218 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
5222 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
5223 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
5224 /* Argument is a parameter pack but parameter is not. */
5237 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5238 template template parameters. Both PARM_PARMS and ARG_PARMS are
5239 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5242 Consider the example:
5243 template <class T> class A;
5244 template<template <class U> class TT> class B;
5246 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5247 the parameters to A, and OUTER_ARGS contains A. */
5250 coerce_template_template_parms (tree parm_parms
,
5252 tsubst_flags_t complain
,
5256 int nparms
, nargs
, i
;
5260 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
5261 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
5263 nparms
= TREE_VEC_LENGTH (parm_parms
);
5264 nargs
= TREE_VEC_LENGTH (arg_parms
);
5266 /* Determine whether we have a parameter pack at the end of the
5267 template template parameter's template parameter list. */
5268 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
5270 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
5272 if (parm
== error_mark_node
)
5275 switch (TREE_CODE (parm
))
5279 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5284 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
5294 && !(variadic_p
&& nargs
>= nparms
- 1))
5297 /* Check all of the template parameters except the parameter pack at
5298 the end (if any). */
5299 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
5301 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
5302 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
5305 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
5306 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
5308 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
5316 /* Check each of the template parameters in the template
5317 argument against the template parameter pack at the end of
5318 the template template parameter. */
5319 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
5322 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
5324 for (; i
< nargs
; ++i
)
5326 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
5329 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
5331 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
5340 /* Verifies that the deduced template arguments (in TARGS) for the
5341 template template parameters (in TPARMS) represent valid bindings,
5342 by comparing the template parameter list of each template argument
5343 to the template parameter list of its corresponding template
5344 template parameter, in accordance with DR150. This
5345 routine can only be called after all template arguments have been
5346 deduced. It will return TRUE if all of the template template
5347 parameter bindings are okay, FALSE otherwise. */
5349 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
5351 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
5354 /* We're dealing with template parms in this process. */
5355 ++processing_template_decl
;
5357 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
5359 for (i
= 0; i
< ntparms
; ++i
)
5361 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
5362 tree targ
= TREE_VEC_ELT (targs
, i
);
5364 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
5366 tree packed_args
= NULL_TREE
;
5369 if (ARGUMENT_PACK_P (targ
))
5371 /* Look inside the argument pack. */
5372 packed_args
= ARGUMENT_PACK_ARGS (targ
);
5373 len
= TREE_VEC_LENGTH (packed_args
);
5376 for (idx
= 0; idx
< len
; ++idx
)
5378 tree targ_parms
= NULL_TREE
;
5381 /* Extract the next argument from the argument
5383 targ
= TREE_VEC_ELT (packed_args
, idx
);
5385 if (PACK_EXPANSION_P (targ
))
5386 /* Look at the pattern of the pack expansion. */
5387 targ
= PACK_EXPANSION_PATTERN (targ
);
5389 /* Extract the template parameters from the template
5391 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
5392 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
5393 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
5394 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
5396 /* Verify that we can coerce the template template
5397 parameters from the template argument to the template
5398 parameter. This requires an exact match. */
5400 && !coerce_template_template_parms
5401 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
5416 --processing_template_decl
;
5420 /* Convert the indicated template ARG as necessary to match the
5421 indicated template PARM. Returns the converted ARG, or
5422 error_mark_node if the conversion was unsuccessful. Error and
5423 warning messages are issued under control of COMPLAIN. This
5424 conversion is for the Ith parameter in the parameter list. ARGS is
5425 the full set of template arguments deduced so far. */
5428 convert_template_argument (tree parm
,
5431 tsubst_flags_t complain
,
5437 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
5439 if (TREE_CODE (arg
) == TREE_LIST
5440 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
5442 /* The template argument was the name of some
5443 member function. That's usually
5444 invalid, but static members are OK. In any
5445 case, grab the underlying fields/functions
5446 and issue an error later if required. */
5447 orig_arg
= TREE_VALUE (arg
);
5448 TREE_TYPE (arg
) = unknown_type_node
;
5453 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
5454 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
5455 || requires_tmpl_type
);
5457 /* When determining whether an argument pack expansion is a template,
5458 look at the pattern. */
5459 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
5460 arg
= PACK_EXPANSION_PATTERN (arg
);
5462 /* Deal with an injected-class-name used as a template template arg. */
5463 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
5465 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
5466 if (TREE_CODE (t
) == TEMPLATE_DECL
)
5468 if (complain
& tf_warning_or_error
)
5469 pedwarn (input_location
, OPT_pedantic
, "injected-class-name %qD"
5470 " used as template template argument", TYPE_NAME (arg
));
5471 else if (flag_pedantic_errors
)
5479 ((TREE_CODE (arg
) == TEMPLATE_DECL
5480 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
5481 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
5482 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
5485 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
5486 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
5487 arg
= TYPE_STUB_DECL (arg
);
5489 is_type
= TYPE_P (arg
) || is_tmpl_type
;
5491 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
5492 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
5494 permerror (input_location
, "to refer to a type member of a template parameter, "
5495 "use %<typename %E%>", orig_arg
);
5497 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
5498 TREE_OPERAND (arg
, 1),
5500 complain
& tf_error
);
5504 if (is_type
!= requires_type
)
5508 if (complain
& tf_error
)
5510 error ("type/value mismatch at argument %d in template "
5511 "parameter list for %qD",
5514 error (" expected a constant of type %qT, got %qT",
5516 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
5517 else if (requires_tmpl_type
)
5518 error (" expected a class template, got %qE", orig_arg
);
5520 error (" expected a type, got %qE", orig_arg
);
5523 return error_mark_node
;
5525 if (is_tmpl_type
^ requires_tmpl_type
)
5527 if (in_decl
&& (complain
& tf_error
))
5529 error ("type/value mismatch at argument %d in template "
5530 "parameter list for %qD",
5533 error (" expected a type, got %qT", DECL_NAME (arg
));
5535 error (" expected a class template, got %qT", orig_arg
);
5537 return error_mark_node
;
5542 if (requires_tmpl_type
)
5544 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
5545 /* The number of argument required is not known yet.
5546 Just accept it for now. */
5547 val
= TREE_TYPE (arg
);
5550 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
5553 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
5555 if (coerce_template_template_parms (parmparm
, argparm
,
5561 /* TEMPLATE_TEMPLATE_PARM node is preferred over
5563 if (val
!= error_mark_node
)
5565 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
5566 val
= TREE_TYPE (val
);
5567 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
5568 val
= make_pack_expansion (val
);
5573 if (in_decl
&& (complain
& tf_error
))
5575 error ("type/value mismatch at argument %d in "
5576 "template parameter list for %qD",
5578 error (" expected a template of type %qD, got %qT",
5582 val
= error_mark_node
;
5588 /* We only form one instance of each template specialization.
5589 Therefore, if we use a non-canonical variant (i.e., a
5590 typedef), any future messages referring to the type will use
5591 the typedef, which is confusing if those future uses do not
5592 themselves also use the typedef. */
5594 val
= strip_typedefs (val
);
5598 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
5600 if (invalid_nontype_parm_type_p (t
, complain
))
5601 return error_mark_node
;
5603 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
5605 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
5609 /* Not sure if this is reachable, but it doesn't hurt
5611 error ("type mismatch in nontype parameter pack");
5612 val
= error_mark_node
;
5615 else if (!uses_template_parms (orig_arg
) && !uses_template_parms (t
))
5616 /* We used to call digest_init here. However, digest_init
5617 will report errors, which we don't want when complain
5618 is zero. More importantly, digest_init will try too
5619 hard to convert things: for example, `0' should not be
5620 converted to pointer type at this point according to
5621 the standard. Accepting this is not merely an
5622 extension, since deciding whether or not these
5623 conversions can occur is part of determining which
5624 function template to call, or whether a given explicit
5625 argument specification is valid. */
5626 val
= convert_nontype_argument (t
, orig_arg
);
5630 if (val
== NULL_TREE
)
5631 val
= error_mark_node
;
5632 else if (val
== error_mark_node
&& (complain
& tf_error
))
5633 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
5635 if (TREE_CODE (val
) == SCOPE_REF
)
5637 /* Strip typedefs from the SCOPE_REF. */
5638 tree type
= strip_typedefs (TREE_TYPE (val
));
5639 tree scope
= strip_typedefs (TREE_OPERAND (val
, 0));
5640 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
5641 QUALIFIED_NAME_IS_TEMPLATE (val
));
5648 /* Coerces the remaining template arguments in INNER_ARGS (from
5649 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
5650 Returns the coerced argument pack. PARM_IDX is the position of this
5651 parameter in the template parameter list. ARGS is the original
5652 template argument list. */
5654 coerce_template_parameter_pack (tree parms
,
5662 tsubst_flags_t complain
)
5664 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
5665 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
5668 tree packed_types
= NULL_TREE
;
5670 if (arg_idx
> nargs
)
5673 packed_args
= make_tree_vec (nargs
- arg_idx
);
5675 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
5676 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
5678 /* When the template parameter is a non-type template
5679 parameter pack whose type uses parameter packs, we need
5680 to look at each of the template arguments
5681 separately. Build a vector of the types for these
5682 non-type template parameters in PACKED_TYPES. */
5684 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
5685 packed_types
= tsubst_pack_expansion (expansion
, args
,
5688 if (packed_types
== error_mark_node
)
5689 return error_mark_node
;
5691 /* Check that we have the right number of arguments. */
5693 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
5694 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
5697 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
5698 error ("wrong number of template arguments (%d, should be %d)",
5699 nargs
, needed_parms
);
5700 return error_mark_node
;
5703 /* If we aren't able to check the actual arguments now
5704 (because they haven't been expanded yet), we can at least
5705 verify that all of the types used for the non-type
5706 template parameter pack are, in fact, valid for non-type
5707 template parameters. */
5709 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
5711 int j
, len
= TREE_VEC_LENGTH (packed_types
);
5712 for (j
= 0; j
< len
; ++j
)
5714 tree t
= TREE_VEC_ELT (packed_types
, j
);
5715 if (invalid_nontype_parm_type_p (t
, complain
))
5716 return error_mark_node
;
5721 /* Convert the remaining arguments, which will be a part of the
5722 parameter pack "parm". */
5723 for (; arg_idx
< nargs
; ++arg_idx
)
5725 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
5726 tree actual_parm
= TREE_VALUE (parm
);
5728 if (packed_types
&& !PACK_EXPANSION_P (arg
))
5730 /* When we have a vector of types (corresponding to the
5731 non-type template parameter pack that uses parameter
5732 packs in its type, as mention above), and the
5733 argument is not an expansion (which expands to a
5734 currently unknown number of arguments), clone the
5735 parm and give it the next type in PACKED_TYPES. */
5736 actual_parm
= copy_node (actual_parm
);
5737 TREE_TYPE (actual_parm
) =
5738 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
5741 if (arg
!= error_mark_node
)
5742 arg
= convert_template_argument (actual_parm
,
5743 arg
, new_args
, complain
, parm_idx
,
5745 if (arg
== error_mark_node
)
5747 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
5750 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
5751 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
5752 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
5755 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
5756 TREE_TYPE (argument_pack
)
5757 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
5758 TREE_CONSTANT (argument_pack
) = 1;
5761 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
5762 #ifdef ENABLE_CHECKING
5763 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
5764 TREE_VEC_LENGTH (packed_args
));
5766 return argument_pack
;
5769 /* Convert all template arguments to their appropriate types, and
5770 return a vector containing the innermost resulting template
5771 arguments. If any error occurs, return error_mark_node. Error and
5772 warning messages are issued under control of COMPLAIN.
5774 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
5775 for arguments not specified in ARGS. Otherwise, if
5776 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
5777 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
5778 USE_DEFAULT_ARGS is false, then all arguments must be specified in
5782 coerce_template_parms (tree parms
,
5785 tsubst_flags_t complain
,
5786 bool require_all_args
,
5787 bool use_default_args
)
5789 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
5792 tree new_inner_args
;
5793 int saved_unevaluated_operand
;
5794 int saved_inhibit_evaluation_warnings
;
5796 /* When used as a boolean value, indicates whether this is a
5797 variadic template parameter list. Since it's an int, we can also
5798 subtract it from nparms to get the number of non-variadic
5802 nparms
= TREE_VEC_LENGTH (parms
);
5804 /* Determine if there are any parameter packs. */
5805 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
5807 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
5808 if (template_parameter_pack_p (tparm
))
5812 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
5813 /* If there are 0 or 1 parameter packs, we need to expand any argument
5814 packs so that we can deduce a parameter pack from some non-packed args
5815 followed by an argument pack, as in variadic85.C. If there are more
5816 than that, we need to leave argument packs intact so the arguments are
5817 assigned to the right parameter packs. This should only happen when
5818 dealing with a nested class inside a partial specialization of a class
5819 template, as in variadic92.C. */
5820 if (variadic_p
<= 1)
5821 inner_args
= expand_template_argument_pack (inner_args
);
5823 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
5824 if ((nargs
> nparms
&& !variadic_p
)
5825 || (nargs
< nparms
- variadic_p
5827 && (!use_default_args
5828 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
5829 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
5831 if (complain
& tf_error
)
5833 const char *or_more
= "";
5836 or_more
= " or more";
5840 error ("wrong number of template arguments (%d, should be %d%s)",
5841 nargs
, nparms
, or_more
);
5844 error ("provided for %q+D", in_decl
);
5847 return error_mark_node
;
5850 /* We need to evaluate the template arguments, even though this
5851 template-id may be nested within a "sizeof". */
5852 saved_unevaluated_operand
= cp_unevaluated_operand
;
5853 cp_unevaluated_operand
= 0;
5854 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
5855 c_inhibit_evaluation_warnings
= 0;
5856 new_inner_args
= make_tree_vec (nparms
);
5857 new_args
= add_outermost_template_args (args
, new_inner_args
);
5858 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
5863 /* Get the Ith template parameter. */
5864 parm
= TREE_VEC_ELT (parms
, parm_idx
);
5866 if (parm
== error_mark_node
)
5868 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
5872 /* Calculate the next argument. */
5873 if (arg_idx
< nargs
)
5874 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
5878 if (template_parameter_pack_p (TREE_VALUE (parm
))
5879 && !(arg
&& ARGUMENT_PACK_P (arg
)))
5881 /* All remaining arguments will be placed in the
5882 template parameter pack PARM. */
5883 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
5884 inner_args
, arg_idx
,
5888 /* Store this argument. */
5889 if (arg
== error_mark_node
)
5891 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
5893 /* We are done with all of the arguments. */
5900 if (PACK_EXPANSION_P (arg
))
5902 if (complain
& tf_error
)
5904 /* FIXME this restriction was removed by N2555; see
5906 /* If ARG is a pack expansion, but PARM is not a
5907 template parameter pack (if it were, we would have
5908 handled it above), we're trying to expand into a
5909 fixed-length argument list. */
5910 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5911 sorry ("cannot expand %<%E%> into a fixed-length "
5912 "argument list", arg
);
5914 sorry ("cannot expand %<%T%> into a fixed-length "
5915 "argument list", arg
);
5917 return error_mark_node
;
5920 else if (require_all_args
)
5922 /* There must be a default arg in this case. */
5923 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
5925 /* The position of the first default template argument,
5926 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
5928 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
5929 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
, arg_idx
);
5934 if (arg
== error_mark_node
)
5936 if (complain
& tf_error
)
5937 error ("template argument %d is invalid", arg_idx
+ 1);
5940 /* This only occurs if there was an error in the template
5941 parameter list itself (which we would already have
5942 reported) that we are trying to recover from, e.g., a class
5943 template with a parameter list such as
5944 template<typename..., typename>. */
5945 return error_mark_node
;
5947 arg
= convert_template_argument (TREE_VALUE (parm
),
5948 arg
, new_args
, complain
,
5951 if (arg
== error_mark_node
)
5953 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
5955 cp_unevaluated_operand
= saved_unevaluated_operand
;
5956 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
5959 return error_mark_node
;
5961 #ifdef ENABLE_CHECKING
5962 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
5963 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
5964 TREE_VEC_LENGTH (new_inner_args
));
5967 return new_inner_args
;
5970 /* Returns 1 if template args OT and NT are equivalent. */
5973 template_args_equal (tree ot
, tree nt
)
5978 if (TREE_CODE (nt
) == TREE_VEC
)
5979 /* For member templates */
5980 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
5981 else if (PACK_EXPANSION_P (ot
))
5982 return PACK_EXPANSION_P (nt
)
5983 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
5984 PACK_EXPANSION_PATTERN (nt
));
5985 else if (ARGUMENT_PACK_P (ot
))
5990 if (!ARGUMENT_PACK_P (nt
))
5993 opack
= ARGUMENT_PACK_ARGS (ot
);
5994 npack
= ARGUMENT_PACK_ARGS (nt
);
5995 len
= TREE_VEC_LENGTH (opack
);
5996 if (TREE_VEC_LENGTH (npack
) != len
)
5998 for (i
= 0; i
< len
; ++i
)
5999 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
6000 TREE_VEC_ELT (npack
, i
)))
6004 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
6006 /* We get here probably because we are in the middle of substituting
6007 into the pattern of a pack expansion. In that case the
6008 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6009 interested in. So we want to use the initial pack argument for
6011 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
6012 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
6013 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
6014 return template_args_equal (ot
, nt
);
6016 else if (TYPE_P (nt
))
6017 return TYPE_P (ot
) && same_type_p (ot
, nt
);
6018 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
6021 return cp_tree_equal (ot
, nt
);
6024 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6025 of template arguments. Returns 0 otherwise. */
6028 comp_template_args (tree oldargs
, tree newargs
)
6032 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
6035 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
6037 tree nt
= TREE_VEC_ELT (newargs
, i
);
6038 tree ot
= TREE_VEC_ELT (oldargs
, i
);
6040 if (! template_args_equal (ot
, nt
))
6047 add_pending_template (tree d
)
6049 tree ti
= (TYPE_P (d
)
6050 ? CLASSTYPE_TEMPLATE_INFO (d
)
6051 : DECL_TEMPLATE_INFO (d
));
6052 struct pending_template
*pt
;
6055 if (TI_PENDING_TEMPLATE_FLAG (ti
))
6058 /* We are called both from instantiate_decl, where we've already had a
6059 tinst_level pushed, and instantiate_template, where we haven't.
6061 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
6064 push_tinst_level (d
);
6066 pt
= GGC_NEW (struct pending_template
);
6068 pt
->tinst
= current_tinst_level
;
6069 if (last_pending_template
)
6070 last_pending_template
->next
= pt
;
6072 pending_templates
= pt
;
6074 last_pending_template
= pt
;
6076 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
6083 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6084 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6085 documentation for TEMPLATE_ID_EXPR. */
6088 lookup_template_function (tree fns
, tree arglist
)
6092 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
6093 return error_mark_node
;
6095 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
6096 gcc_assert (fns
&& (is_overloaded_fn (fns
)
6097 || TREE_CODE (fns
) == IDENTIFIER_NODE
));
6099 if (BASELINK_P (fns
))
6101 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
6103 BASELINK_FUNCTIONS (fns
),
6108 type
= TREE_TYPE (fns
);
6109 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
6110 type
= unknown_type_node
;
6112 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
6115 /* Within the scope of a template class S<T>, the name S gets bound
6116 (in build_self_reference) to a TYPE_DECL for the class, not a
6117 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6118 or one of its enclosing classes, and that type is a template,
6119 return the associated TEMPLATE_DECL. Otherwise, the original
6122 Also handle the case when DECL is a TREE_LIST of ambiguous
6123 injected-class-names from different bases. */
6126 maybe_get_template_decl_from_type_decl (tree decl
)
6128 if (decl
== NULL_TREE
)
6131 /* DR 176: A lookup that finds an injected-class-name (10.2
6132 [class.member.lookup]) can result in an ambiguity in certain cases
6133 (for example, if it is found in more than one base class). If all of
6134 the injected-class-names that are found refer to specializations of
6135 the same class template, and if the name is followed by a
6136 template-argument-list, the reference refers to the class template
6137 itself and not a specialization thereof, and is not ambiguous. */
6138 if (TREE_CODE (decl
) == TREE_LIST
)
6140 tree t
, tmpl
= NULL_TREE
;
6141 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
6143 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
6146 else if (tmpl
!= elt
)
6149 if (tmpl
&& t
== NULL_TREE
)
6155 return (decl
!= NULL_TREE
6156 && DECL_SELF_REFERENCE_P (decl
)
6157 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
6158 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
6161 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6162 parameters, find the desired type.
6164 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6166 IN_DECL, if non-NULL, is the template declaration we are trying to
6169 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6170 the class we are looking up.
6172 Issue error and warning messages under control of COMPLAIN.
6174 If the template class is really a local class in a template
6175 function, then the FUNCTION_CONTEXT is the function in which it is
6178 ??? Note that this function is currently called *twice* for each
6179 template-id: the first time from the parser, while creating the
6180 incomplete type (finish_template_type), and the second type during the
6181 real instantiation (instantiate_template_class). This is surely something
6182 that we want to avoid. It also causes some problems with argument
6183 coercion (see convert_nontype_argument for more information on this). */
6186 lookup_template_class (tree d1
,
6191 tsubst_flags_t complain
)
6193 tree templ
= NULL_TREE
, parmlist
;
6200 timevar_push (TV_NAME_LOOKUP
);
6202 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
6204 tree value
= innermost_non_namespace_value (d1
);
6205 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
6210 push_decl_namespace (context
);
6211 templ
= lookup_name (d1
);
6212 templ
= maybe_get_template_decl_from_type_decl (templ
);
6214 pop_decl_namespace ();
6217 context
= DECL_CONTEXT (templ
);
6219 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
6221 tree type
= TREE_TYPE (d1
);
6223 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6224 an implicit typename for the second A. Deal with it. */
6225 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
6226 type
= TREE_TYPE (type
);
6228 if (CLASSTYPE_TEMPLATE_INFO (type
))
6230 templ
= CLASSTYPE_TI_TEMPLATE (type
);
6231 d1
= DECL_NAME (templ
);
6234 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
6235 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
6237 templ
= TYPE_TI_TEMPLATE (d1
);
6238 d1
= DECL_NAME (templ
);
6240 else if (TREE_CODE (d1
) == TEMPLATE_DECL
6241 && DECL_TEMPLATE_RESULT (d1
)
6242 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
6245 d1
= DECL_NAME (templ
);
6246 context
= DECL_CONTEXT (templ
);
6249 /* Issue an error message if we didn't find a template. */
6252 if (complain
& tf_error
)
6253 error ("%qT is not a template", d1
);
6254 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
6257 if (TREE_CODE (templ
) != TEMPLATE_DECL
6258 /* Make sure it's a user visible template, if it was named by
6260 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
6261 && !PRIMARY_TEMPLATE_P (templ
)))
6263 if (complain
& tf_error
)
6265 error ("non-template type %qT used as a template", d1
);
6267 error ("for template declaration %q+D", in_decl
);
6269 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
6272 complain
&= ~tf_user
;
6274 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
6276 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
6277 template arguments */
6283 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
6285 /* Consider an example where a template template parameter declared as
6287 template <class T, class U = std::allocator<T> > class TT
6289 The template parameter level of T and U are one level larger than
6290 of TT. To proper process the default argument of U, say when an
6291 instantiation `TT<int>' is seen, we need to build the full
6292 arguments containing {int} as the innermost level. Outer levels,
6293 available when not appearing as default template argument, can be
6294 obtained from the arguments of the enclosing template.
6296 Suppose that TT is later substituted with std::vector. The above
6297 instantiation is `TT<int, std::allocator<T> >' with TT at
6298 level 1, and T at level 2, while the template arguments at level 1
6299 becomes {std::vector} and the inner level 2 is {int}. */
6301 outer
= DECL_CONTEXT (templ
);
6303 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
6304 else if (current_template_parms
)
6305 /* This is an argument of the current template, so we haven't set
6306 DECL_CONTEXT yet. */
6307 outer
= current_template_args ();
6310 arglist
= add_to_template_args (outer
, arglist
);
6312 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
6314 /*require_all_args=*/true,
6315 /*use_default_args=*/true);
6316 if (arglist2
== error_mark_node
6317 || (!uses_template_parms (arglist2
)
6318 && check_instantiated_args (templ
, arglist2
, complain
)))
6319 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
6321 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
6322 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, parm
);
6326 tree template_type
= TREE_TYPE (templ
);
6329 tree found
= NULL_TREE
;
6332 int is_partial_instantiation
;
6334 gen_tmpl
= most_general_template (templ
);
6335 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
6336 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
6337 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
6339 if (arg_depth
== 1 && parm_depth
> 1)
6341 /* We've been given an incomplete set of template arguments.
6344 template <class T> struct S1 {
6345 template <class U> struct S2 {};
6346 template <class U> struct S2<U*> {};
6349 we will be called with an ARGLIST of `U*', but the
6350 TEMPLATE will be `template <class T> template
6351 <class U> struct S1<T>::S2'. We must fill in the missing
6354 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
6356 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
6359 /* Now we should have enough arguments. */
6360 gcc_assert (parm_depth
== arg_depth
);
6362 /* From here on, we're only interested in the most general
6365 /* Calculate the BOUND_ARGS. These will be the args that are
6366 actually tsubst'd into the definition to create the
6370 /* We have multiple levels of arguments to coerce, at once. */
6372 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
6374 tree bound_args
= make_tree_vec (parm_depth
);
6376 for (i
= saved_depth
,
6377 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
6378 i
> 0 && t
!= NULL_TREE
;
6379 --i
, t
= TREE_CHAIN (t
))
6381 tree a
= coerce_template_parms (TREE_VALUE (t
),
6384 /*require_all_args=*/true,
6385 /*use_default_args=*/true);
6387 /* Don't process further if one of the levels fails. */
6388 if (a
== error_mark_node
)
6390 /* Restore the ARGLIST to its full size. */
6391 TREE_VEC_LENGTH (arglist
) = saved_depth
;
6392 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
6395 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
6397 /* We temporarily reduce the length of the ARGLIST so
6398 that coerce_template_parms will see only the arguments
6399 corresponding to the template parameters it is
6401 TREE_VEC_LENGTH (arglist
)--;
6404 /* Restore the ARGLIST to its full size. */
6405 TREE_VEC_LENGTH (arglist
) = saved_depth
;
6407 arglist
= bound_args
;
6411 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
6412 INNERMOST_TEMPLATE_ARGS (arglist
),
6415 /*require_all_args=*/true,
6416 /*use_default_args=*/true);
6418 if (arglist
== error_mark_node
)
6419 /* We were unable to bind the arguments. */
6420 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
6422 /* In the scope of a template class, explicit references to the
6423 template class refer to the type of the template, not any
6424 instantiation of it. For example, in:
6426 template <class T> class C { void f(C<T>); }
6428 the `C<T>' is just the same as `C'. Outside of the
6429 class, however, such a reference is an instantiation. */
6431 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
6432 || currently_open_class (template_type
))
6433 /* comp_template_args is expensive, check it last. */
6434 && comp_template_args (TYPE_TI_ARGS (template_type
),
6436 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, template_type
);
6438 /* If we already have this specialization, return it. */
6439 elt
.tmpl
= gen_tmpl
;
6441 hash
= hash_specialization (&elt
);
6442 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
6446 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, entry
->spec
);
6448 /* This type is a "partial instantiation" if any of the template
6449 arguments still involve template parameters. Note that we set
6450 IS_PARTIAL_INSTANTIATION for partial specializations as
6452 is_partial_instantiation
= uses_template_parms (arglist
);
6454 /* If the deduced arguments are invalid, then the binding
6456 if (!is_partial_instantiation
6457 && check_instantiated_args (gen_tmpl
,
6458 INNERMOST_TEMPLATE_ARGS (arglist
),
6460 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
6462 if (!is_partial_instantiation
6463 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
6464 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
6465 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
6467 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
6468 DECL_NAME (gen_tmpl
),
6469 /*tag_scope=*/ts_global
);
6470 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, found
);
6473 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
6476 context
= global_namespace
;
6478 /* Create the type. */
6479 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
6481 if (!is_partial_instantiation
)
6483 set_current_access_from_decl (TYPE_NAME (template_type
));
6484 t
= start_enum (TYPE_IDENTIFIER (template_type
),
6485 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
6486 arglist
, complain
, in_decl
),
6487 SCOPED_ENUM_P (template_type
));
6491 /* We don't want to call start_enum for this type, since
6492 the values for the enumeration constants may involve
6493 template parameters. And, no one should be interested
6494 in the enumeration constants for such a type. */
6495 t
= cxx_make_type (ENUMERAL_TYPE
);
6496 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
6501 t
= make_class_type (TREE_CODE (template_type
));
6502 CLASSTYPE_DECLARED_CLASS (t
)
6503 = CLASSTYPE_DECLARED_CLASS (template_type
);
6504 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
6505 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
6507 /* A local class. Make sure the decl gets registered properly. */
6508 if (context
== current_function_decl
)
6509 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
6511 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
6512 /* This instantiation is another name for the primary
6513 template type. Set the TYPE_CANONICAL field
6515 TYPE_CANONICAL (t
) = template_type
;
6516 else if (any_template_arguments_need_structural_equality_p (arglist
))
6517 /* Some of the template arguments require structural
6518 equality testing, so this template class requires
6519 structural equality testing. */
6520 SET_TYPE_STRUCTURAL_EQUALITY (t
);
6523 /* If we called start_enum or pushtag above, this information
6524 will already be set up. */
6527 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
6529 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
6530 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
6531 DECL_SOURCE_LOCATION (type_decl
)
6532 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
6535 type_decl
= TYPE_NAME (t
);
6537 TREE_PRIVATE (type_decl
)
6538 = TREE_PRIVATE (TYPE_STUB_DECL (template_type
));
6539 TREE_PROTECTED (type_decl
)
6540 = TREE_PROTECTED (TYPE_STUB_DECL (template_type
));
6541 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
6543 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
6544 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
6547 /* Set up the template information. We have to figure out which
6548 template is the immediate parent if this is a full
6550 if (parm_depth
== 1 || is_partial_instantiation
6551 || !PRIMARY_TEMPLATE_P (gen_tmpl
))
6552 /* This case is easy; there are no member templates involved. */
6556 /* This is a full instantiation of a member template. Find
6557 the partial instantiation of which this is an instance. */
6559 /* Temporarily reduce by one the number of levels in the ARGLIST
6560 so as to avoid comparing the last set of arguments. */
6561 TREE_VEC_LENGTH (arglist
)--;
6562 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
6563 TREE_VEC_LENGTH (arglist
)++;
6564 found
= CLASSTYPE_TI_TEMPLATE (found
);
6567 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
6570 slot
= (spec_entry
**) htab_find_slot_with_hash (type_specializations
,
6571 &elt
, hash
, INSERT
);
6572 *slot
= GGC_NEW (spec_entry
);
6575 /* Note this use of the partial instantiation so we can check it
6576 later in maybe_process_partial_specialization. */
6577 DECL_TEMPLATE_INSTANTIATIONS (templ
)
6578 = tree_cons (arglist
, t
,
6579 DECL_TEMPLATE_INSTANTIATIONS (templ
));
6581 if (TREE_CODE (t
) == ENUMERAL_TYPE
6582 && !is_partial_instantiation
)
6583 /* Now that the type has been registered on the instantiations
6584 list, we set up the enumerators. Because the enumeration
6585 constants may involve the enumeration type itself, we make
6586 sure to register the type first, and then create the
6587 constants. That way, doing tsubst_expr for the enumeration
6588 constants won't result in recursive calls here; we'll find
6589 the instantiation and exit above. */
6590 tsubst_enum (template_type
, t
, arglist
);
6592 if (is_partial_instantiation
)
6593 /* If the type makes use of template parameters, the
6594 code that generates debugging information will crash. */
6595 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
6597 /* Possibly limit visibility based on template args. */
6598 TREE_PUBLIC (type_decl
) = 1;
6599 determine_visibility (type_decl
);
6601 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
6603 timevar_pop (TV_NAME_LOOKUP
);
6610 /* True when we should also visit template parameters that occur in
6611 non-deduced contexts. */
6612 bool include_nondeduced_p
;
6613 struct pointer_set_t
*visited
;
6616 /* Called from for_each_template_parm via walk_tree. */
6619 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
6622 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
6623 tree_fn_t fn
= pfd
->fn
;
6624 void *data
= pfd
->data
;
6627 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
6628 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
6629 pfd
->include_nondeduced_p
))
6630 return error_mark_node
;
6632 switch (TREE_CODE (t
))
6635 if (TYPE_PTRMEMFUNC_P (t
))
6641 if (!TYPE_TEMPLATE_INFO (t
))
6643 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
6644 fn
, data
, pfd
->visited
,
6645 pfd
->include_nondeduced_p
))
6646 return error_mark_node
;
6650 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
6651 fn
, data
, pfd
->visited
,
6652 pfd
->include_nondeduced_p
)
6653 || for_each_template_parm (TYPE_MAX_VALUE (t
),
6654 fn
, data
, pfd
->visited
,
6655 pfd
->include_nondeduced_p
))
6656 return error_mark_node
;
6660 /* Since we're not going to walk subtrees, we have to do this
6662 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
6663 pfd
->visited
, pfd
->include_nondeduced_p
))
6664 return error_mark_node
;
6668 /* Check the return type. */
6669 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
6670 pfd
->include_nondeduced_p
))
6671 return error_mark_node
;
6673 /* Check the parameter types. Since default arguments are not
6674 instantiated until they are needed, the TYPE_ARG_TYPES may
6675 contain expressions that involve template parameters. But,
6676 no-one should be looking at them yet. And, once they're
6677 instantiated, they don't contain template parameters, so
6678 there's no point in looking at them then, either. */
6682 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
6683 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
6684 pfd
->visited
, pfd
->include_nondeduced_p
))
6685 return error_mark_node
;
6687 /* Since we've already handled the TYPE_ARG_TYPES, we don't
6688 want walk_tree walking into them itself. */
6694 if (pfd
->include_nondeduced_p
6695 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
6697 pfd
->include_nondeduced_p
))
6698 return error_mark_node
;
6703 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
6704 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
6705 pfd
->visited
, pfd
->include_nondeduced_p
))
6706 return error_mark_node
;
6711 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
6712 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
6713 pfd
->visited
, pfd
->include_nondeduced_p
))
6714 return error_mark_node
;
6715 if (DECL_CONTEXT (t
)
6716 && pfd
->include_nondeduced_p
6717 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
6718 pfd
->visited
, pfd
->include_nondeduced_p
))
6719 return error_mark_node
;
6722 case BOUND_TEMPLATE_TEMPLATE_PARM
:
6723 /* Record template parameters such as `T' inside `TT<T>'. */
6724 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
6725 pfd
->include_nondeduced_p
))
6726 return error_mark_node
;
6729 case TEMPLATE_TEMPLATE_PARM
:
6730 case TEMPLATE_TYPE_PARM
:
6731 case TEMPLATE_PARM_INDEX
:
6732 if (fn
&& (*fn
)(t
, data
))
6733 return error_mark_node
;
6735 return error_mark_node
;
6739 /* A template template parameter is encountered. */
6740 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
6741 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
6742 pfd
->include_nondeduced_p
))
6743 return error_mark_node
;
6745 /* Already substituted template template parameter */
6751 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
6753 pfd
->include_nondeduced_p
))
6754 return error_mark_node
;
6758 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
6759 && pfd
->include_nondeduced_p
6760 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
6761 (TREE_TYPE (t
)), fn
, data
,
6762 pfd
->visited
, pfd
->include_nondeduced_p
))
6763 return error_mark_node
;
6768 /* If there's no type, then this thing must be some expression
6769 involving template parameters. */
6770 if (!fn
&& !TREE_TYPE (t
))
6771 return error_mark_node
;
6776 case REINTERPRET_CAST_EXPR
:
6777 case CONST_CAST_EXPR
:
6778 case STATIC_CAST_EXPR
:
6779 case DYNAMIC_CAST_EXPR
:
6783 case PSEUDO_DTOR_EXPR
:
6785 return error_mark_node
;
6792 /* We didn't find any template parameters we liked. */
6796 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
6797 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
6798 call FN with the parameter and the DATA.
6799 If FN returns nonzero, the iteration is terminated, and
6800 for_each_template_parm returns 1. Otherwise, the iteration
6801 continues. If FN never returns a nonzero value, the value
6802 returned by for_each_template_parm is 0. If FN is NULL, it is
6803 considered to be the function which always returns 1.
6805 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
6806 parameters that occur in non-deduced contexts. When false, only
6807 visits those template parameters that can be deduced. */
6810 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
6811 struct pointer_set_t
*visited
,
6812 bool include_nondeduced_p
)
6814 struct pair_fn_data pfd
;
6820 pfd
.include_nondeduced_p
= include_nondeduced_p
;
6822 /* Walk the tree. (Conceptually, we would like to walk without
6823 duplicates, but for_each_template_parm_r recursively calls
6824 for_each_template_parm, so we would need to reorganize a fair
6825 bit to use walk_tree_without_duplicates, so we keep our own
6828 pfd
.visited
= visited
;
6830 pfd
.visited
= pointer_set_create ();
6831 result
= cp_walk_tree (&t
,
6832 for_each_template_parm_r
,
6834 pfd
.visited
) != NULL_TREE
;
6839 pointer_set_destroy (pfd
.visited
);
6846 /* Returns true if T depends on any template parameter. */
6849 uses_template_parms (tree t
)
6852 int saved_processing_template_decl
;
6854 saved_processing_template_decl
= processing_template_decl
;
6855 if (!saved_processing_template_decl
)
6856 processing_template_decl
= 1;
6858 dependent_p
= dependent_type_p (t
);
6859 else if (TREE_CODE (t
) == TREE_VEC
)
6860 dependent_p
= any_dependent_template_arguments_p (t
);
6861 else if (TREE_CODE (t
) == TREE_LIST
)
6862 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
6863 || uses_template_parms (TREE_CHAIN (t
)));
6864 else if (TREE_CODE (t
) == TYPE_DECL
)
6865 dependent_p
= dependent_type_p (TREE_TYPE (t
));
6868 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
6869 || TREE_CODE (t
) == OVERLOAD
6870 || TREE_CODE (t
) == BASELINK
6871 || TREE_CODE (t
) == IDENTIFIER_NODE
6872 || TREE_CODE (t
) == TRAIT_EXPR
6873 || TREE_CODE (t
) == CONSTRUCTOR
6874 || CONSTANT_CLASS_P (t
))
6875 dependent_p
= (type_dependent_expression_p (t
)
6876 || value_dependent_expression_p (t
));
6879 gcc_assert (t
== error_mark_node
);
6880 dependent_p
= false;
6883 processing_template_decl
= saved_processing_template_decl
;
6888 /* Returns true if T depends on any template parameter with level LEVEL. */
6891 uses_template_parms_level (tree t
, int level
)
6893 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
6894 /*include_nondeduced_p=*/true);
6897 static int tinst_depth
;
6898 extern int max_tinst_depth
;
6899 #ifdef GATHER_STATISTICS
6902 static int tinst_level_tick
;
6903 static int last_template_error_tick
;
6905 /* We're starting to instantiate D; record the template instantiation context
6906 for diagnostics and to restore it later. */
6909 push_tinst_level (tree d
)
6911 struct tinst_level
*new_level
;
6913 if (tinst_depth
>= max_tinst_depth
)
6915 /* If the instantiation in question still has unbound template parms,
6916 we don't really care if we can't instantiate it, so just return.
6917 This happens with base instantiation for implicit `typename'. */
6918 if (uses_template_parms (d
))
6921 last_template_error_tick
= tinst_level_tick
;
6922 error ("template instantiation depth exceeds maximum of %d (use "
6923 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
6924 max_tinst_depth
, d
);
6926 print_instantiation_context ();
6931 new_level
= GGC_NEW (struct tinst_level
);
6932 new_level
->decl
= d
;
6933 new_level
->locus
= input_location
;
6934 new_level
->in_system_header_p
= in_system_header
;
6935 new_level
->next
= current_tinst_level
;
6936 current_tinst_level
= new_level
;
6939 #ifdef GATHER_STATISTICS
6940 if (tinst_depth
> depth_reached
)
6941 depth_reached
= tinst_depth
;
6948 /* We're done instantiating this template; return to the instantiation
6952 pop_tinst_level (void)
6954 /* Restore the filename and line number stashed away when we started
6955 this instantiation. */
6956 input_location
= current_tinst_level
->locus
;
6957 current_tinst_level
= current_tinst_level
->next
;
6962 /* We're instantiating a deferred template; restore the template
6963 instantiation context in which the instantiation was requested, which
6964 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
6967 reopen_tinst_level (struct tinst_level
*level
)
6969 struct tinst_level
*t
;
6972 for (t
= level
; t
; t
= t
->next
)
6975 current_tinst_level
= level
;
6980 /* Returns the TINST_LEVEL which gives the original instantiation
6983 struct tinst_level
*
6984 outermost_tinst_level (void)
6986 struct tinst_level
*level
= current_tinst_level
;
6989 level
= level
->next
;
6993 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
6996 parameter_of_template_p (tree parm
, tree templ
)
7001 if (!parm
|| !templ
)
7004 gcc_assert (DECL_TEMPLATE_PARM_P (parm
));
7005 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
7007 parms
= DECL_TEMPLATE_PARMS (templ
);
7008 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
7010 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
7011 if (parm
== TREE_VALUE (TREE_VEC_ELT (parms
, i
)))
7017 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
7018 vector of template arguments, as for tsubst.
7020 Returns an appropriate tsubst'd friend declaration. */
7023 tsubst_friend_function (tree decl
, tree args
)
7027 if (TREE_CODE (decl
) == FUNCTION_DECL
7028 && DECL_TEMPLATE_INSTANTIATION (decl
)
7029 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
7030 /* This was a friend declared with an explicit template
7031 argument list, e.g.:
7035 to indicate that f was a template instantiation, not a new
7036 function declaration. Now, we have to figure out what
7037 instantiation of what template. */
7039 tree template_id
, arglist
, fns
;
7042 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
7044 /* Friend functions are looked up in the containing namespace scope.
7045 We must enter that scope, to avoid finding member functions of the
7046 current class with same name. */
7047 push_nested_namespace (ns
);
7048 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
7049 tf_warning_or_error
, NULL_TREE
,
7050 /*integral_constant_expression_p=*/false);
7051 pop_nested_namespace (ns
);
7052 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
7053 tf_warning_or_error
, NULL_TREE
);
7054 template_id
= lookup_template_function (fns
, arglist
);
7056 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
7057 tmpl
= determine_specialization (template_id
, new_friend
,
7059 /*need_member_template=*/0,
7060 TREE_VEC_LENGTH (args
),
7062 return instantiate_template (tmpl
, new_args
, tf_error
);
7065 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
7067 /* The NEW_FRIEND will look like an instantiation, to the
7068 compiler, but is not an instantiation from the point of view of
7069 the language. For example, we might have had:
7071 template <class T> struct S {
7072 template <class U> friend void f(T, U);
7075 Then, in S<int>, template <class U> void f(int, U) is not an
7076 instantiation of anything. */
7077 if (new_friend
== error_mark_node
)
7078 return error_mark_node
;
7080 DECL_USE_TEMPLATE (new_friend
) = 0;
7081 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
7083 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
7084 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
7085 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
7088 /* The mangled name for the NEW_FRIEND is incorrect. The function
7089 is not a template instantiation and should not be mangled like
7090 one. Therefore, we forget the mangling here; we'll recompute it
7091 later if we need it. */
7092 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
7094 SET_DECL_RTL (new_friend
, NULL_RTX
);
7095 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
7098 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
7101 tree new_friend_template_info
;
7102 tree new_friend_result_template_info
;
7104 int new_friend_is_defn
;
7106 /* We must save some information from NEW_FRIEND before calling
7107 duplicate decls since that function will free NEW_FRIEND if
7109 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
7110 new_friend_is_defn
=
7111 (DECL_INITIAL (DECL_TEMPLATE_RESULT
7112 (template_for_substitution (new_friend
)))
7114 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
7116 /* This declaration is a `primary' template. */
7117 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
7119 new_friend_result_template_info
7120 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
7123 new_friend_result_template_info
= NULL_TREE
;
7125 /* Make the init_value nonzero so pushdecl knows this is a defn. */
7126 if (new_friend_is_defn
)
7127 DECL_INITIAL (new_friend
) = error_mark_node
;
7129 /* Inside pushdecl_namespace_level, we will push into the
7130 current namespace. However, the friend function should go
7131 into the namespace of the template. */
7132 ns
= decl_namespace_context (new_friend
);
7133 push_nested_namespace (ns
);
7134 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
7135 pop_nested_namespace (ns
);
7137 if (old_decl
== error_mark_node
)
7138 return error_mark_node
;
7140 if (old_decl
!= new_friend
)
7142 /* This new friend declaration matched an existing
7143 declaration. For example, given:
7145 template <class T> void f(T);
7146 template <class U> class C {
7147 template <class T> friend void f(T) {}
7150 the friend declaration actually provides the definition
7151 of `f', once C has been instantiated for some type. So,
7152 old_decl will be the out-of-class template declaration,
7153 while new_friend is the in-class definition.
7155 But, if `f' was called before this point, the
7156 instantiation of `f' will have DECL_TI_ARGS corresponding
7157 to `T' but not to `U', references to which might appear
7158 in the definition of `f'. Previously, the most general
7159 template for an instantiation of `f' was the out-of-class
7160 version; now it is the in-class version. Therefore, we
7161 run through all specialization of `f', adding to their
7162 DECL_TI_ARGS appropriately. In particular, they need a
7163 new set of outer arguments, corresponding to the
7164 arguments for this class instantiation.
7166 The same situation can arise with something like this:
7169 template <class T> class C {
7173 when `C<int>' is instantiated. Now, `f(int)' is defined
7176 if (!new_friend_is_defn
)
7177 /* On the other hand, if the in-class declaration does
7178 *not* provide a definition, then we don't want to alter
7179 existing definitions. We can just leave everything
7184 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
7185 tree new_args
= TI_ARGS (new_friend_template_info
);
7187 /* Overwrite whatever template info was there before, if
7188 any, with the new template information pertaining to
7190 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
7192 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
7193 /* We should have called reregister_specialization in
7195 gcc_assert (retrieve_specialization (new_template
,
7202 /* Indicate that the old function template is a partial
7204 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
7205 = new_friend_result_template_info
;
7207 gcc_assert (new_template
7208 == most_general_template (new_template
));
7209 gcc_assert (new_template
!= old_decl
);
7211 /* Reassign any specializations already in the hash table
7212 to the new more general template, and add the
7213 additional template args. */
7214 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
7218 tree spec
= TREE_VALUE (t
);
7221 elt
.tmpl
= old_decl
;
7222 elt
.args
= DECL_TI_ARGS (spec
);
7223 elt
.spec
= NULL_TREE
;
7225 htab_remove_elt (decl_specializations
, &elt
);
7228 = add_outermost_template_args (new_args
,
7229 DECL_TI_ARGS (spec
));
7231 register_specialization
7232 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
7235 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
7239 /* The information from NEW_FRIEND has been merged into OLD_DECL
7240 by duplicate_decls. */
7241 new_friend
= old_decl
;
7246 tree context
= DECL_CONTEXT (new_friend
);
7250 template <class T> class C {
7251 template <class U> friend void C1<U>::f (); // case 1
7252 friend void C2<T>::f (); // case 2
7254 we only need to make sure CONTEXT is a complete type for
7255 case 2. To distinguish between the two cases, we note that
7256 CONTEXT of case 1 remains dependent type after tsubst while
7257 this isn't true for case 2. */
7258 ++processing_template_decl
;
7259 dependent_p
= dependent_type_p (context
);
7260 --processing_template_decl
;
7263 && !complete_type_or_else (context
, NULL_TREE
))
7264 return error_mark_node
;
7266 if (COMPLETE_TYPE_P (context
))
7268 /* Check to see that the declaration is really present, and,
7269 possibly obtain an improved declaration. */
7270 tree fn
= check_classfn (context
,
7271 new_friend
, NULL_TREE
);
7281 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
7282 template arguments, as for tsubst.
7284 Returns an appropriate tsubst'd friend type or error_mark_node on
7288 tsubst_friend_class (tree friend_tmpl
, tree args
)
7294 context
= DECL_CONTEXT (friend_tmpl
);
7298 if (TREE_CODE (context
) == NAMESPACE_DECL
)
7299 push_nested_namespace (context
);
7301 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
7304 /* Look for a class template declaration. We look for hidden names
7305 because two friend declarations of the same template are the
7306 same. For example, in:
7309 template <typename> friend class F;
7311 template <typename> struct B {
7312 template <typename> friend class F;
7315 both F templates are the same. */
7316 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
7317 /*block_p=*/true, 0,
7318 LOOKUP_COMPLAIN
| LOOKUP_HIDDEN
);
7320 /* But, if we don't find one, it might be because we're in a
7321 situation like this:
7329 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
7330 for `S<int>', not the TEMPLATE_DECL. */
7331 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
7333 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
7334 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
7337 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
7339 /* The friend template has already been declared. Just
7340 check to see that the declarations match, and install any new
7341 default parameters. We must tsubst the default parameters,
7342 of course. We only need the innermost template parameters
7343 because that is all that redeclare_class_template will look
7345 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
7346 > TMPL_ARGS_DEPTH (args
))
7349 location_t saved_input_location
;
7350 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
7351 args
, tf_warning_or_error
);
7353 saved_input_location
= input_location
;
7354 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
7355 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
7356 input_location
= saved_input_location
;
7360 friend_type
= TREE_TYPE (tmpl
);
7364 /* The friend template has not already been declared. In this
7365 case, the instantiation of the template class will cause the
7366 injection of this template into the global scope. */
7367 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
7368 if (tmpl
== error_mark_node
)
7369 return error_mark_node
;
7371 /* The new TMPL is not an instantiation of anything, so we
7372 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
7373 the new type because that is supposed to be the corresponding
7374 template decl, i.e., TMPL. */
7375 DECL_USE_TEMPLATE (tmpl
) = 0;
7376 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
7377 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
7378 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
7379 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
7381 /* Inject this template into the global scope. */
7382 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
7387 if (TREE_CODE (context
) == NAMESPACE_DECL
)
7388 pop_nested_namespace (context
);
7390 pop_nested_class ();
7396 /* Returns zero if TYPE cannot be completed later due to circularity.
7397 Otherwise returns one. */
7400 can_complete_type_without_circularity (tree type
)
7402 if (type
== NULL_TREE
|| type
== error_mark_node
)
7404 else if (COMPLETE_TYPE_P (type
))
7406 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
7407 return can_complete_type_without_circularity (TREE_TYPE (type
));
7408 else if (CLASS_TYPE_P (type
)
7409 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
7415 /* Apply any attributes which had to be deferred until instantiation
7416 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
7417 ARGS, COMPLAIN, IN_DECL are as tsubst. */
7420 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
7421 tree args
, tsubst_flags_t complain
, tree in_decl
)
7423 tree last_dep
= NULL_TREE
;
7427 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
7428 if (ATTR_IS_DEPENDENT (t
))
7431 attributes
= copy_list (attributes
);
7435 if (DECL_P (*decl_p
))
7437 if (TREE_TYPE (*decl_p
) == error_mark_node
)
7439 p
= &DECL_ATTRIBUTES (*decl_p
);
7442 p
= &TYPE_ATTRIBUTES (*decl_p
);
7446 tree late_attrs
= NULL_TREE
;
7447 tree
*q
= &late_attrs
;
7449 for (*p
= attributes
; *p
; )
7452 if (ATTR_IS_DEPENDENT (t
))
7454 *p
= TREE_CHAIN (t
);
7455 TREE_CHAIN (t
) = NULL_TREE
;
7456 /* If the first attribute argument is an identifier, don't
7457 pass it through tsubst. Attributes like mode, format,
7458 cleanup and several target specific attributes expect it
7461 && TREE_CODE (TREE_VALUE (t
)) == TREE_LIST
7462 && TREE_VALUE (TREE_VALUE (t
))
7463 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t
)))
7464 == IDENTIFIER_NODE
))
7467 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
7469 /*integral_constant_expression_p=*/false);
7470 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
7472 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
7477 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
7478 /*integral_constant_expression_p=*/false);
7480 q
= &TREE_CHAIN (t
);
7483 p
= &TREE_CHAIN (t
);
7486 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
7490 /* Perform (or defer) access check for typedefs that were referenced
7491 from within the template TMPL code.
7492 This is a subroutine of instantiate_template and instantiate_class_template.
7493 TMPL is the template to consider and TARGS is the list of arguments of
7497 perform_typedefs_access_check (tree tmpl
, tree targs
)
7499 location_t saved_location
;
7501 qualified_typedef_usage_t
*iter
;
7504 || (!CLASS_TYPE_P (tmpl
)
7505 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
7508 saved_location
= input_location
;
7510 VEC_iterate (qualified_typedef_usage_t
,
7511 get_types_needing_access_check (tmpl
),
7515 tree type_decl
= iter
->typedef_decl
;
7516 tree type_scope
= iter
->context
;
7518 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
7521 if (uses_template_parms (type_decl
))
7522 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
7523 if (uses_template_parms (type_scope
))
7524 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
7526 /* Make access check error messages point to the location
7527 of the use of the typedef. */
7528 input_location
= iter
->locus
;
7529 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
7530 type_decl
, type_decl
);
7532 input_location
= saved_location
;
7536 instantiate_class_template (tree type
)
7538 tree templ
, args
, pattern
, t
, member
;
7542 unsigned int saved_maximum_field_alignment
;
7544 if (type
== error_mark_node
)
7545 return error_mark_node
;
7547 if (TYPE_BEING_DEFINED (type
)
7548 || COMPLETE_TYPE_P (type
)
7549 || uses_template_parms (type
))
7552 /* Figure out which template is being instantiated. */
7553 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
7554 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
7556 /* Determine what specialization of the original template to
7558 t
= most_specialized_class (type
, templ
);
7559 if (t
== error_mark_node
)
7561 TYPE_BEING_DEFINED (type
) = 1;
7562 return error_mark_node
;
7566 /* This TYPE is actually an instantiation of a partial
7567 specialization. We replace the innermost set of ARGS with
7568 the arguments appropriate for substitution. For example,
7571 template <class T> struct S {};
7572 template <class T> struct S<T*> {};
7574 and supposing that we are instantiating S<int*>, ARGS will
7575 presently be {int*} -- but we need {int}. */
7576 pattern
= TREE_TYPE (t
);
7577 args
= TREE_PURPOSE (t
);
7581 pattern
= TREE_TYPE (templ
);
7582 args
= CLASSTYPE_TI_ARGS (type
);
7585 /* If the template we're instantiating is incomplete, then clearly
7586 there's nothing we can do. */
7587 if (!COMPLETE_TYPE_P (pattern
))
7590 /* If we've recursively instantiated too many templates, stop. */
7591 if (! push_tinst_level (type
))
7594 /* Now we're really doing the instantiation. Mark the type as in
7595 the process of being defined. */
7596 TYPE_BEING_DEFINED (type
) = 1;
7598 /* We may be in the middle of deferred access check. Disable
7600 push_deferring_access_checks (dk_no_deferred
);
7602 push_to_top_level ();
7603 /* Use #pragma pack from the template context. */
7604 saved_maximum_field_alignment
= maximum_field_alignment
;
7605 maximum_field_alignment
= TYPE_PRECISION (pattern
);
7607 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
7609 /* Set the input location to the most specialized template definition.
7610 This is needed if tsubsting causes an error. */
7611 typedecl
= TYPE_MAIN_DECL (pattern
);
7612 input_location
= DECL_SOURCE_LOCATION (typedecl
);
7614 TYPE_HAS_USER_CONSTRUCTOR (type
) = TYPE_HAS_USER_CONSTRUCTOR (pattern
);
7615 TYPE_HAS_NEW_OPERATOR (type
) = TYPE_HAS_NEW_OPERATOR (pattern
);
7616 TYPE_HAS_ARRAY_NEW_OPERATOR (type
) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern
);
7617 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
7618 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
7619 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
7620 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
7621 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
7622 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
7623 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
7624 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
7625 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
7626 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
7627 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
7628 if (ANON_AGGR_TYPE_P (pattern
))
7629 SET_ANON_AGGR_TYPE_P (type
);
7630 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
7632 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
7633 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
7636 pbinfo
= TYPE_BINFO (pattern
);
7638 /* We should never instantiate a nested class before its enclosing
7639 class; we need to look up the nested class by name before we can
7640 instantiate it, and that lookup should instantiate the enclosing
7642 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
7643 || COMPLETE_TYPE_P (TYPE_CONTEXT (type
))
7644 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type
)));
7646 base_list
= NULL_TREE
;
7647 if (BINFO_N_BASE_BINFOS (pbinfo
))
7650 tree context
= TYPE_CONTEXT (type
);
7654 /* We must enter the scope containing the type, as that is where
7655 the accessibility of types named in dependent bases are
7657 pushed_scope
= push_scope (context
? context
: global_namespace
);
7659 /* Substitute into each of the bases to determine the actual
7661 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
7664 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
7665 tree expanded_bases
= NULL_TREE
;
7668 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
7671 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
7672 args
, tf_error
, NULL_TREE
);
7673 if (expanded_bases
== error_mark_node
)
7676 len
= TREE_VEC_LENGTH (expanded_bases
);
7679 for (idx
= 0; idx
< len
; idx
++)
7682 /* Extract the already-expanded base class. */
7683 base
= TREE_VEC_ELT (expanded_bases
, idx
);
7685 /* Substitute to figure out the base class. */
7686 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
7689 if (base
== error_mark_node
)
7692 base_list
= tree_cons (access
, base
, base_list
);
7693 if (BINFO_VIRTUAL_P (pbase_binfo
))
7694 TREE_TYPE (base_list
) = integer_type_node
;
7698 /* The list is now in reverse order; correct that. */
7699 base_list
= nreverse (base_list
);
7702 pop_scope (pushed_scope
);
7704 /* Now call xref_basetypes to set up all the base-class
7706 xref_basetypes (type
, base_list
);
7708 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
7709 (int) ATTR_FLAG_TYPE_IN_PLACE
,
7710 args
, tf_error
, NULL_TREE
);
7712 /* Now that our base classes are set up, enter the scope of the
7713 class, so that name lookups into base classes, etc. will work
7714 correctly. This is precisely analogous to what we do in
7715 begin_class_definition when defining an ordinary non-template
7716 class, except we also need to push the enclosing classes. */
7717 push_nested_class (type
);
7719 /* Now members are processed in the order of declaration. */
7720 for (member
= CLASSTYPE_DECL_LIST (pattern
);
7721 member
; member
= TREE_CHAIN (member
))
7723 tree t
= TREE_VALUE (member
);
7725 if (TREE_PURPOSE (member
))
7729 /* Build new CLASSTYPE_NESTED_UTDS. */
7732 bool class_template_p
;
7734 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
7735 && TYPE_LANG_SPECIFIC (t
)
7736 && CLASSTYPE_IS_TEMPLATE (t
));
7737 /* If the member is a class template, then -- even after
7738 substitution -- there may be dependent types in the
7739 template argument list for the class. We increment
7740 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
7741 that function will assume that no types are dependent
7742 when outside of a template. */
7743 if (class_template_p
)
7744 ++processing_template_decl
;
7745 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
7746 if (class_template_p
)
7747 --processing_template_decl
;
7748 if (newtag
== error_mark_node
)
7751 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
7753 tree name
= TYPE_IDENTIFIER (t
);
7755 if (class_template_p
)
7756 /* Unfortunately, lookup_template_class sets
7757 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
7758 instantiation (i.e., for the type of a member
7759 template class nested within a template class.)
7760 This behavior is required for
7761 maybe_process_partial_specialization to work
7762 correctly, but is not accurate in this case;
7763 the TAG is not an instantiation of anything.
7764 (The corresponding TEMPLATE_DECL is an
7765 instantiation, but the TYPE is not.) */
7766 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
7768 /* Now, we call pushtag to put this NEWTAG into the scope of
7769 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
7770 pushtag calling push_template_decl. We don't have to do
7771 this for enums because it will already have been done in
7774 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
7775 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
7778 else if (TREE_CODE (t
) == FUNCTION_DECL
7779 || DECL_FUNCTION_TEMPLATE_P (t
))
7781 /* Build new TYPE_METHODS. */
7784 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7785 ++processing_template_decl
;
7786 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
7787 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7788 --processing_template_decl
;
7789 set_current_access_from_decl (r
);
7790 finish_member_declaration (r
);
7794 /* Build new TYPE_FIELDS. */
7795 if (TREE_CODE (t
) == STATIC_ASSERT
)
7798 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
7799 tf_warning_or_error
, NULL_TREE
,
7800 /*integral_constant_expression_p=*/true);
7801 finish_static_assert (condition
,
7802 STATIC_ASSERT_MESSAGE (t
),
7803 STATIC_ASSERT_SOURCE_LOCATION (t
),
7806 else if (TREE_CODE (t
) != CONST_DECL
)
7810 /* The file and line for this declaration, to
7811 assist in error message reporting. Since we
7812 called push_tinst_level above, we don't need to
7814 input_location
= DECL_SOURCE_LOCATION (t
);
7816 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7817 ++processing_template_decl
;
7818 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
7819 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7820 --processing_template_decl
;
7821 if (TREE_CODE (r
) == VAR_DECL
)
7825 [t]he initialization (and any associated
7826 side-effects) of a static data member does
7827 not occur unless the static data member is
7828 itself used in a way that requires the
7829 definition of the static data member to
7832 Therefore, we do not substitute into the
7833 initialized for the static data member here. */
7834 finish_static_data_member_decl
7837 /*init_const_expr_p=*/false,
7838 /*asmspec_tree=*/NULL_TREE
,
7840 if (DECL_INITIALIZED_IN_CLASS_P (r
))
7841 check_static_variable_definition (r
, TREE_TYPE (r
));
7843 else if (TREE_CODE (r
) == FIELD_DECL
)
7845 /* Determine whether R has a valid type and can be
7846 completed later. If R is invalid, then it is
7847 replaced by error_mark_node so that it will not be
7848 added to TYPE_FIELDS. */
7849 tree rtype
= TREE_TYPE (r
);
7850 if (can_complete_type_without_circularity (rtype
))
7851 complete_type (rtype
);
7853 if (!COMPLETE_TYPE_P (rtype
))
7855 cxx_incomplete_type_error (r
, rtype
);
7856 r
= error_mark_node
;
7860 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
7861 such a thing will already have been added to the field
7862 list by tsubst_enum in finish_member_declaration in the
7863 CLASSTYPE_NESTED_UTDS case above. */
7864 if (!(TREE_CODE (r
) == TYPE_DECL
7865 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
7866 && DECL_ARTIFICIAL (r
)))
7868 set_current_access_from_decl (r
);
7869 finish_member_declaration (r
);
7876 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
))
7878 /* Build new CLASSTYPE_FRIEND_CLASSES. */
7880 tree friend_type
= t
;
7881 bool adjust_processing_template_decl
= false;
7883 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
7885 /* template <class T> friend class C; */
7886 friend_type
= tsubst_friend_class (friend_type
, args
);
7887 adjust_processing_template_decl
= true;
7889 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
7891 /* template <class T> friend class C::D; */
7892 friend_type
= tsubst (friend_type
, args
,
7893 tf_warning_or_error
, NULL_TREE
);
7894 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
7895 friend_type
= TREE_TYPE (friend_type
);
7896 adjust_processing_template_decl
= true;
7898 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
)
7900 /* This could be either
7904 when dependent_type_p is false or
7906 template <class U> friend class T::C;
7909 friend_type
= tsubst (friend_type
, args
,
7910 tf_warning_or_error
, NULL_TREE
);
7911 /* Bump processing_template_decl for correct
7912 dependent_type_p calculation. */
7913 ++processing_template_decl
;
7914 if (dependent_type_p (friend_type
))
7915 adjust_processing_template_decl
= true;
7916 --processing_template_decl
;
7918 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
7919 && hidden_name_p (TYPE_NAME (friend_type
)))
7923 where C hasn't been declared yet. Let's lookup name
7924 from namespace scope directly, bypassing any name that
7925 come from dependent base class. */
7926 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
7928 /* The call to xref_tag_from_type does injection for friend
7930 push_nested_namespace (ns
);
7932 xref_tag_from_type (friend_type
, NULL_TREE
,
7933 /*tag_scope=*/ts_current
);
7934 pop_nested_namespace (ns
);
7936 else if (uses_template_parms (friend_type
))
7937 /* friend class C<T>; */
7938 friend_type
= tsubst (friend_type
, args
,
7939 tf_warning_or_error
, NULL_TREE
);
7944 where C is already declared or
7946 friend class C<int>;
7948 We don't have to do anything in these cases. */
7950 if (adjust_processing_template_decl
)
7951 /* Trick make_friend_class into realizing that the friend
7952 we're adding is a template, not an ordinary class. It's
7953 important that we use make_friend_class since it will
7954 perform some error-checking and output cross-reference
7956 ++processing_template_decl
;
7958 if (friend_type
!= error_mark_node
)
7959 make_friend_class (type
, friend_type
, /*complain=*/false);
7961 if (adjust_processing_template_decl
)
7962 --processing_template_decl
;
7966 /* Build new DECL_FRIENDLIST. */
7969 /* The file and line for this declaration, to
7970 assist in error message reporting. Since we
7971 called push_tinst_level above, we don't need to
7973 input_location
= DECL_SOURCE_LOCATION (t
);
7975 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7977 ++processing_template_decl
;
7978 push_deferring_access_checks (dk_no_check
);
7981 r
= tsubst_friend_function (t
, args
);
7982 add_friend (type
, r
, /*complain=*/false);
7983 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7985 pop_deferring_access_checks ();
7986 --processing_template_decl
;
7992 /* Set the file and line number information to whatever is given for
7993 the class itself. This puts error messages involving generated
7994 implicit functions at a predictable point, and the same point
7995 that would be used for non-template classes. */
7996 input_location
= DECL_SOURCE_LOCATION (typedecl
);
7998 unreverse_member_declarations (type
);
7999 finish_struct_1 (type
);
8000 TYPE_BEING_DEFINED (type
) = 0;
8002 /* Now that the class is complete, instantiate default arguments for
8003 any member functions. We don't do this earlier because the
8004 default arguments may reference members of the class. */
8005 if (!PRIMARY_TEMPLATE_P (templ
))
8006 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
8007 if (TREE_CODE (t
) == FUNCTION_DECL
8008 /* Implicitly generated member functions will not have template
8009 information; they are not instantiations, but instead are
8010 created "fresh" for each instantiation. */
8011 && DECL_TEMPLATE_INFO (t
))
8012 tsubst_default_arguments (t
);
8014 /* Some typedefs referenced from within the template code need to be access
8015 checked at template instantiation time, i.e now. These types were
8016 added to the template at parsing time. Let's get those and perform
8017 the access checks then. */
8018 perform_typedefs_access_check (pattern
, args
);
8019 perform_deferred_access_checks ();
8020 pop_nested_class ();
8021 maximum_field_alignment
= saved_maximum_field_alignment
;
8022 pop_from_top_level ();
8023 pop_deferring_access_checks ();
8026 /* The vtable for a template class can be emitted in any translation
8027 unit in which the class is instantiated. When there is no key
8028 method, however, finish_struct_1 will already have added TYPE to
8029 the keyed_classes list. */
8030 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
8031 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
8037 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
8043 else if (TYPE_P (t
))
8044 r
= tsubst (t
, args
, complain
, in_decl
);
8047 r
= tsubst_expr (t
, args
, complain
, in_decl
,
8048 /*integral_constant_expression_p=*/true);
8049 r
= fold_non_dependent_expr (r
);
8054 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
8055 NONTYPE_ARGUMENT_PACK. */
8058 make_fnparm_pack (tree spec_parm
)
8060 /* Collect all of the extra "packed" parameters into an
8064 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
8065 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8066 int i
, len
= list_length (spec_parm
);
8068 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
8069 parmvec
= make_tree_vec (len
);
8070 parmtypevec
= make_tree_vec (len
);
8071 for (i
= 0; i
< len
; i
++, spec_parm
= TREE_CHAIN (spec_parm
))
8073 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
8074 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
8077 /* Build the argument packs. */
8078 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
8079 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
8080 TREE_TYPE (argpack
) = argtypepack
;
8085 /* Substitute ARGS into T, which is an pack expansion
8086 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
8087 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
8088 (if only a partial substitution could be performed) or
8089 ERROR_MARK_NODE if there was an error. */
8091 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
8095 tree pack
, packs
= NULL_TREE
, unsubstituted_packs
= NULL_TREE
;
8099 bool very_local_specializations
= false;
8101 gcc_assert (PACK_EXPANSION_P (t
));
8102 pattern
= PACK_EXPANSION_PATTERN (t
);
8104 /* Determine the argument packs that will instantiate the parameter
8105 packs used in the expansion expression. While we're at it,
8106 compute the number of arguments to be expanded and make sure it
8108 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
8109 pack
= TREE_CHAIN (pack
))
8111 tree parm_pack
= TREE_VALUE (pack
);
8112 tree arg_pack
= NULL_TREE
;
8113 tree orig_arg
= NULL_TREE
;
8115 if (TREE_CODE (parm_pack
) == PARM_DECL
)
8117 arg_pack
= retrieve_local_specialization (parm_pack
);
8118 if (arg_pack
== NULL_TREE
)
8120 /* This can happen for a parameter name used later in a function
8121 declaration (such as in a late-specified return type). Just
8122 make a dummy decl, since it's only used for its type. */
8123 gcc_assert (cp_unevaluated_operand
!= 0);
8124 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
8125 arg_pack
= make_fnparm_pack (arg_pack
);
8130 int level
, idx
, levels
;
8131 template_parm_level_and_index (parm_pack
, &level
, &idx
);
8133 levels
= TMPL_ARGS_DEPTH (args
);
8134 if (level
<= levels
)
8135 arg_pack
= TMPL_ARG (args
, level
, idx
);
8138 orig_arg
= arg_pack
;
8139 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
8140 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
8142 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
8143 /* This can only happen if we forget to expand an argument
8144 pack somewhere else. Just return an error, silently. */
8146 result
= make_tree_vec (1);
8147 TREE_VEC_ELT (result
, 0) = error_mark_node
;
8152 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
)) == 1
8153 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0)))
8155 tree expansion
= TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0);
8156 tree pattern
= PACK_EXPANSION_PATTERN (expansion
);
8157 if ((TYPE_P (pattern
) && same_type_p (pattern
, parm_pack
))
8158 || (!TYPE_P (pattern
) && cp_tree_equal (parm_pack
, pattern
)))
8159 /* The argument pack that the parameter maps to is just an
8160 expansion of the parameter itself, such as one would
8161 find in the implicit typedef of a class inside the
8162 class itself. Consider this parameter "unsubstituted",
8163 so that we will maintain the outer pack expansion. */
8164 arg_pack
= NULL_TREE
;
8170 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
8172 /* It's all-or-nothing with incomplete argument packs. */
8173 if (incomplete
&& !ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
8174 return error_mark_node
;
8176 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
8181 else if (len
!= my_len
)
8184 /* We got explicit args for some packs but not others;
8185 do nothing now and try again after deduction. */
8187 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
8188 error ("mismatched argument pack lengths while expanding "
8192 error ("mismatched argument pack lengths while expanding "
8195 return error_mark_node
;
8198 /* Keep track of the parameter packs and their corresponding
8200 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
8201 TREE_TYPE (packs
) = orig_arg
;
8204 /* We can't substitute for this parameter pack. */
8205 unsubstituted_packs
= tree_cons (TREE_PURPOSE (pack
),
8207 unsubstituted_packs
);
8210 /* We cannot expand this expansion expression, because we don't have
8211 all of the argument packs we need. Substitute into the pattern
8212 and return a PACK_EXPANSION_*. The caller will need to deal with
8214 if (unsubstituted_packs
)
8217 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
8218 new_pat
= tsubst_expr (pattern
, args
, complain
, in_decl
,
8219 /*integral_constant_expression_p=*/false);
8221 new_pat
= tsubst (pattern
, args
, complain
, in_decl
);
8222 return make_pack_expansion (new_pat
);
8225 /* We could not find any argument packs that work. */
8227 return error_mark_node
;
8229 if (!local_specializations
)
8231 /* We're in a late-specified return type, so we don't have a local
8232 specializations table. Create one for doing this expansion. */
8233 very_local_specializations
= true;
8234 local_specializations
= htab_create (37,
8235 hash_local_specialization
,
8236 eq_local_specializations
,
8240 /* For each argument in each argument pack, substitute into the
8242 result
= make_tree_vec (len
+ incomplete
);
8243 for (i
= 0; i
< len
+ incomplete
; ++i
)
8245 /* For parameter pack, change the substitution of the parameter
8246 pack to the ith argument in its argument pack, then expand
8248 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
8250 tree parm
= TREE_PURPOSE (pack
);
8252 if (TREE_CODE (parm
) == PARM_DECL
)
8254 /* Select the Ith argument from the pack. */
8255 tree arg
= make_node (ARGUMENT_PACK_SELECT
);
8256 ARGUMENT_PACK_SELECT_FROM_PACK (arg
) = TREE_VALUE (pack
);
8257 ARGUMENT_PACK_SELECT_INDEX (arg
) = i
;
8259 register_local_specialization (arg
, parm
);
8265 template_parm_level_and_index (parm
, &level
, &idx
);
8269 /* Select the Ith argument from the pack. */
8270 value
= make_node (ARGUMENT_PACK_SELECT
);
8271 ARGUMENT_PACK_SELECT_FROM_PACK (value
) = TREE_VALUE (pack
);
8272 ARGUMENT_PACK_SELECT_INDEX (value
) = i
;
8275 /* Update the corresponding argument. */
8276 TMPL_ARG (args
, level
, idx
) = value
;
8280 /* Substitute into the PATTERN with the altered arguments. */
8281 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
8282 TREE_VEC_ELT (result
, i
) =
8283 tsubst_expr (pattern
, args
, complain
, in_decl
,
8284 /*integral_constant_expression_p=*/false);
8286 TREE_VEC_ELT (result
, i
) = tsubst (pattern
, args
, complain
, in_decl
);
8289 /* When we have incomplete argument packs, the last "expanded"
8290 result is itself a pack expansion, which allows us
8291 to deduce more arguments. */
8292 TREE_VEC_ELT (result
, i
) =
8293 make_pack_expansion (TREE_VEC_ELT (result
, i
));
8295 if (TREE_VEC_ELT (result
, i
) == error_mark_node
)
8297 result
= error_mark_node
;
8302 /* Update ARGS to restore the substitution from parameter packs to
8303 their argument packs. */
8304 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
8306 tree parm
= TREE_PURPOSE (pack
);
8308 if (TREE_CODE (parm
) == PARM_DECL
)
8309 register_local_specialization (TREE_TYPE (pack
), parm
);
8313 template_parm_level_and_index (parm
, &level
, &idx
);
8315 /* Update the corresponding argument. */
8316 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
8317 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
8320 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
8324 if (very_local_specializations
)
8326 htab_delete (local_specializations
);
8327 local_specializations
= NULL
;
8333 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
8334 TMPL. We do this using DECL_PARM_INDEX, which should work even with
8335 parameter packs; all parms generated from a function parameter pack will
8336 have the same DECL_PARM_INDEX. */
8339 get_pattern_parm (tree parm
, tree tmpl
)
8341 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
8344 if (DECL_ARTIFICIAL (parm
))
8346 for (patparm
= DECL_ARGUMENTS (pattern
);
8347 patparm
; patparm
= TREE_CHAIN (patparm
))
8348 if (DECL_ARTIFICIAL (patparm
)
8349 && DECL_NAME (parm
) == DECL_NAME (patparm
))
8354 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
8355 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
8356 gcc_assert (DECL_PARM_INDEX (patparm
)
8357 == DECL_PARM_INDEX (parm
));
8363 /* Substitute ARGS into the vector or list of template arguments T. */
8366 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
8369 int len
= TREE_VEC_LENGTH (t
);
8370 int need_new
= 0, i
, expanded_len_adjust
= 0, out
;
8371 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
8373 for (i
= 0; i
< len
; i
++)
8375 tree orig_arg
= TREE_VEC_ELT (t
, i
);
8378 if (TREE_CODE (orig_arg
) == TREE_VEC
)
8379 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
8380 else if (PACK_EXPANSION_P (orig_arg
))
8382 /* Substitute into an expansion expression. */
8383 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
8385 if (TREE_CODE (new_arg
) == TREE_VEC
)
8386 /* Add to the expanded length adjustment the number of
8387 expanded arguments. We subtract one from this
8388 measurement, because the argument pack expression
8389 itself is already counted as 1 in
8390 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
8391 the argument pack is empty. */
8392 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
8394 else if (ARGUMENT_PACK_P (orig_arg
))
8396 /* Substitute into each of the arguments. */
8397 new_arg
= TYPE_P (orig_arg
)
8398 ? cxx_make_type (TREE_CODE (orig_arg
))
8399 : make_node (TREE_CODE (orig_arg
));
8401 SET_ARGUMENT_PACK_ARGS (
8403 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
8404 args
, complain
, in_decl
));
8406 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
8407 new_arg
= error_mark_node
;
8409 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
8410 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
8412 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
8414 if (TREE_TYPE (new_arg
) == error_mark_node
)
8415 new_arg
= error_mark_node
;
8419 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
8421 if (new_arg
== error_mark_node
)
8422 return error_mark_node
;
8425 if (new_arg
!= orig_arg
)
8432 /* Make space for the expanded arguments coming from template
8434 t
= make_tree_vec (len
+ expanded_len_adjust
);
8435 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
8436 arguments for a member template.
8437 In that case each TREE_VEC in ORIG_T represents a level of template
8438 arguments, and ORIG_T won't carry any non defaulted argument count.
8439 It will rather be the nested TREE_VECs that will carry one.
8440 In other words, ORIG_T carries a non defaulted argument count only
8441 if it doesn't contain any nested TREE_VEC. */
8442 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
8444 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
8445 count
+= expanded_len_adjust
;
8446 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
8448 for (i
= 0, out
= 0; i
< len
; i
++)
8450 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
8451 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
8452 && TREE_CODE (elts
[i
]) == TREE_VEC
)
8456 /* Now expand the template argument pack "in place". */
8457 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
8458 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
8462 TREE_VEC_ELT (t
, out
) = elts
[i
];
8470 /* Return the result of substituting ARGS into the template parameters
8471 given by PARMS. If there are m levels of ARGS and m + n levels of
8472 PARMS, then the result will contain n levels of PARMS. For
8473 example, if PARMS is `template <class T> template <class U>
8474 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
8475 result will be `template <int*, double, class V>'. */
8478 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
8483 /* When substituting into a template, we must set
8484 PROCESSING_TEMPLATE_DECL as the template parameters may be
8485 dependent if they are based on one-another, and the dependency
8486 predicates are short-circuit outside of templates. */
8487 ++processing_template_decl
;
8489 for (new_parms
= &r
;
8490 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
8491 new_parms
= &(TREE_CHAIN (*new_parms
)),
8492 parms
= TREE_CHAIN (parms
))
8495 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
8498 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
8504 if (parms
== error_mark_node
)
8507 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
8509 if (tuple
== error_mark_node
)
8512 default_value
= TREE_PURPOSE (tuple
);
8513 parm_decl
= TREE_VALUE (tuple
);
8515 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
8516 if (TREE_CODE (parm_decl
) == PARM_DECL
8517 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
8518 parm_decl
= error_mark_node
;
8519 default_value
= tsubst_template_arg (default_value
, args
,
8520 complain
, NULL_TREE
);
8522 tuple
= build_tree_list (default_value
, parm_decl
);
8523 TREE_VEC_ELT (new_vec
, i
) = tuple
;
8527 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
8528 - TMPL_ARGS_DEPTH (args
)),
8529 new_vec
, NULL_TREE
);
8532 --processing_template_decl
;
8537 /* Substitute the ARGS into the indicated aggregate (or enumeration)
8538 type T. If T is not an aggregate or enumeration type, it is
8539 handled as if by tsubst. IN_DECL is as for tsubst. If
8540 ENTERING_SCOPE is nonzero, T is the context for a template which
8541 we are presently tsubst'ing. Return the substituted value. */
8544 tsubst_aggr_type (tree t
,
8546 tsubst_flags_t complain
,
8553 switch (TREE_CODE (t
))
8556 if (TYPE_PTRMEMFUNC_P (t
))
8557 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
8559 /* Else fall through. */
8562 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
8567 int saved_unevaluated_operand
;
8568 int saved_inhibit_evaluation_warnings
;
8570 /* In "sizeof(X<I>)" we need to evaluate "I". */
8571 saved_unevaluated_operand
= cp_unevaluated_operand
;
8572 cp_unevaluated_operand
= 0;
8573 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
8574 c_inhibit_evaluation_warnings
= 0;
8576 /* First, determine the context for the type we are looking
8578 context
= TYPE_CONTEXT (t
);
8581 context
= tsubst_aggr_type (context
, args
, complain
,
8582 in_decl
, /*entering_scope=*/1);
8583 /* If context is a nested class inside a class template,
8584 it may still need to be instantiated (c++/33959). */
8585 if (TYPE_P (context
))
8586 context
= complete_type (context
);
8589 /* Then, figure out what arguments are appropriate for the
8590 type we are trying to find. For example, given:
8592 template <class T> struct S;
8593 template <class T, class U> void f(T, U) { S<U> su; }
8595 and supposing that we are instantiating f<int, double>,
8596 then our ARGS will be {int, double}, but, when looking up
8597 S we only want {double}. */
8598 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
8600 if (argvec
== error_mark_node
)
8601 r
= error_mark_node
;
8604 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
8605 entering_scope
, complain
);
8606 r
= cp_build_qualified_type_real (r
, TYPE_QUALS (t
), complain
);
8609 cp_unevaluated_operand
= saved_unevaluated_operand
;
8610 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
8615 /* This is not a template type, so there's nothing to do. */
8619 return tsubst (t
, args
, complain
, in_decl
);
8623 /* Substitute into the default argument ARG (a default argument for
8624 FN), which has the indicated TYPE. */
8627 tsubst_default_argument (tree fn
, tree type
, tree arg
)
8629 tree saved_class_ptr
= NULL_TREE
;
8630 tree saved_class_ref
= NULL_TREE
;
8632 /* This default argument came from a template. Instantiate the
8633 default argument here, not in tsubst. In the case of
8642 we must be careful to do name lookup in the scope of S<T>,
8643 rather than in the current class. */
8644 push_access_scope (fn
);
8645 /* The "this" pointer is not valid in a default argument. */
8648 saved_class_ptr
= current_class_ptr
;
8649 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
8650 saved_class_ref
= current_class_ref
;
8651 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
8654 push_deferring_access_checks(dk_no_deferred
);
8655 /* The default argument expression may cause implicitly defined
8656 member functions to be synthesized, which will result in garbage
8657 collection. We must treat this situation as if we were within
8658 the body of function so as to avoid collecting live data on the
8661 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
8662 tf_warning_or_error
, NULL_TREE
,
8663 /*integral_constant_expression_p=*/false);
8665 pop_deferring_access_checks();
8667 /* Restore the "this" pointer. */
8670 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
8671 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
8674 /* Make sure the default argument is reasonable. */
8675 arg
= check_default_argument (type
, arg
);
8677 pop_access_scope (fn
);
8682 /* Substitute into all the default arguments for FN. */
8685 tsubst_default_arguments (tree fn
)
8690 tmpl_args
= DECL_TI_ARGS (fn
);
8692 /* If this function is not yet instantiated, we certainly don't need
8693 its default arguments. */
8694 if (uses_template_parms (tmpl_args
))
8697 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
8699 arg
= TREE_CHAIN (arg
))
8700 if (TREE_PURPOSE (arg
))
8701 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
8703 TREE_PURPOSE (arg
));
8706 /* Substitute the ARGS into the T, which is a _DECL. Return the
8707 result of the substitution. Issue error and warning messages under
8708 control of COMPLAIN. */
8711 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
8713 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
8714 location_t saved_loc
;
8719 /* Set the filename and linenumber to improve error-reporting. */
8720 saved_loc
= input_location
;
8721 input_location
= DECL_SOURCE_LOCATION (t
);
8723 switch (TREE_CODE (t
))
8727 /* We can get here when processing a member function template,
8728 member class template, or template template parameter. */
8729 tree decl
= DECL_TEMPLATE_RESULT (t
);
8734 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
8736 /* Template template parameter is treated here. */
8737 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8738 if (new_type
== error_mark_node
)
8739 RETURN (error_mark_node
);
8742 TREE_CHAIN (r
) = NULL_TREE
;
8743 TREE_TYPE (r
) = new_type
;
8744 DECL_TEMPLATE_RESULT (r
)
8745 = build_decl (DECL_SOURCE_LOCATION (decl
),
8746 TYPE_DECL
, DECL_NAME (decl
), new_type
);
8747 DECL_TEMPLATE_PARMS (r
)
8748 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
8750 TYPE_NAME (new_type
) = r
;
8754 /* We might already have an instance of this template.
8755 The ARGS are for the surrounding class type, so the
8756 full args contain the tsubst'd args for the context,
8757 plus the innermost args from the template decl. */
8758 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
8759 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
8760 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
8761 /* Because this is a template, the arguments will still be
8762 dependent, even after substitution. If
8763 PROCESSING_TEMPLATE_DECL is not set, the dependency
8764 predicates will short-circuit. */
8765 ++processing_template_decl
;
8766 full_args
= tsubst_template_args (tmpl_args
, args
,
8768 --processing_template_decl
;
8769 if (full_args
== error_mark_node
)
8770 RETURN (error_mark_node
);
8772 /* If this is a default template template argument,
8773 tsubst might not have changed anything. */
8774 if (full_args
== tmpl_args
)
8777 hash
= hash_tmpl_and_args (t
, full_args
);
8778 spec
= retrieve_specialization (t
, full_args
, hash
);
8779 if (spec
!= NULL_TREE
)
8785 /* Make a new template decl. It will be similar to the
8786 original, but will record the current template arguments.
8787 We also create a new function declaration, which is just
8788 like the old one, but points to this new template, rather
8789 than the old one. */
8791 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
8792 TREE_CHAIN (r
) = NULL_TREE
;
8794 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
8796 if (TREE_CODE (decl
) == TYPE_DECL
)
8799 ++processing_template_decl
;
8800 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8801 --processing_template_decl
;
8802 if (new_type
== error_mark_node
)
8803 RETURN (error_mark_node
);
8805 TREE_TYPE (r
) = new_type
;
8806 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
8807 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
8808 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
8809 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
8814 ++processing_template_decl
;
8815 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
8816 --processing_template_decl
;
8817 if (new_decl
== error_mark_node
)
8818 RETURN (error_mark_node
);
8820 DECL_TEMPLATE_RESULT (r
) = new_decl
;
8821 DECL_TI_TEMPLATE (new_decl
) = r
;
8822 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
8823 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
8824 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
8827 SET_DECL_IMPLICIT_INSTANTIATION (r
);
8828 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
8829 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
8831 /* The template parameters for this new template are all the
8832 template parameters for the old template, except the
8833 outermost level of parameters. */
8834 DECL_TEMPLATE_PARMS (r
)
8835 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
8838 if (PRIMARY_TEMPLATE_P (t
))
8839 DECL_PRIMARY_TEMPLATE (r
) = r
;
8841 if (TREE_CODE (decl
) != TYPE_DECL
)
8842 /* Record this non-type partial instantiation. */
8843 register_specialization (r
, t
,
8844 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
8852 tree argvec
= NULL_TREE
;
8860 /* Nobody should be tsubst'ing into non-template functions. */
8861 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
8863 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
8868 /* If T is not dependent, just return it. We have to
8869 increment PROCESSING_TEMPLATE_DECL because
8870 value_dependent_expression_p assumes that nothing is
8871 dependent when PROCESSING_TEMPLATE_DECL is zero. */
8872 ++processing_template_decl
;
8873 dependent_p
= value_dependent_expression_p (t
);
8874 --processing_template_decl
;
8878 /* Calculate the most general template of which R is a
8879 specialization, and the complete set of arguments used to
8881 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
8882 argvec
= tsubst_template_args (DECL_TI_ARGS
8883 (DECL_TEMPLATE_RESULT (gen_tmpl
)),
8884 args
, complain
, in_decl
);
8886 /* Check to see if we already have this specialization. */
8887 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
8888 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
8896 /* We can see more levels of arguments than parameters if
8897 there was a specialization of a member template, like
8900 template <class T> struct S { template <class U> void f(); }
8901 template <> template <class U> void S<int>::f(U);
8903 Here, we'll be substituting into the specialization,
8904 because that's where we can find the code we actually
8905 want to generate, but we'll have enough arguments for
8906 the most general template.
8908 We also deal with the peculiar case:
8910 template <class T> struct S {
8911 template <class U> friend void f();
8913 template <class U> void f() {}
8915 template void f<double>();
8917 Here, the ARGS for the instantiation of will be {int,
8918 double}. But, we only need as many ARGS as there are
8919 levels of template parameters in CODE_PATTERN. We are
8920 careful not to get fooled into reducing the ARGS in
8923 template <class T> struct S { template <class U> void f(U); }
8924 template <class T> template <> void S<T>::f(int) {}
8926 which we can spot because the pattern will be a
8927 specialization in this case. */
8928 args_depth
= TMPL_ARGS_DEPTH (args
);
8930 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
8931 if (args_depth
> parms_depth
8932 && !DECL_TEMPLATE_SPECIALIZATION (t
))
8933 args
= get_innermost_template_args (args
, parms_depth
);
8937 /* This special case arises when we have something like this:
8939 template <class T> struct S {
8940 friend void f<int>(int, double);
8943 Here, the DECL_TI_TEMPLATE for the friend declaration
8944 will be an IDENTIFIER_NODE. We are being called from
8945 tsubst_friend_function, and we want only to create a
8946 new decl (R) with appropriate types so that we can call
8947 determine_specialization. */
8948 gen_tmpl
= NULL_TREE
;
8951 if (DECL_CLASS_SCOPE_P (t
))
8953 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
8957 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
8958 complain
, t
, /*entering_scope=*/1);
8963 ctx
= DECL_CONTEXT (t
);
8965 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8966 if (type
== error_mark_node
)
8967 RETURN (error_mark_node
);
8969 /* We do NOT check for matching decls pushed separately at this
8970 point, as they may not represent instantiations of this
8971 template, and in any case are considered separate under the
8974 DECL_USE_TEMPLATE (r
) = 0;
8975 TREE_TYPE (r
) = type
;
8976 /* Clear out the mangled name and RTL for the instantiation. */
8977 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
8978 SET_DECL_RTL (r
, NULL_RTX
);
8979 /* Leave DECL_INITIAL set on deleted instantiations. */
8980 if (!DECL_DELETED_FN (r
))
8981 DECL_INITIAL (r
) = NULL_TREE
;
8982 DECL_CONTEXT (r
) = ctx
;
8984 if (member
&& DECL_CONV_FN_P (r
))
8985 /* Type-conversion operator. Reconstruct the name, in
8986 case it's the name of one of the template's parameters. */
8987 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
8989 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
8991 DECL_RESULT (r
) = NULL_TREE
;
8993 TREE_STATIC (r
) = 0;
8994 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
8995 DECL_EXTERNAL (r
) = 1;
8996 /* If this is an instantiation of a function with internal
8997 linkage, we already know what object file linkage will be
8998 assigned to the instantiation. */
8999 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
9000 DECL_DEFER_OUTPUT (r
) = 0;
9001 TREE_CHAIN (r
) = NULL_TREE
;
9002 DECL_PENDING_INLINE_INFO (r
) = 0;
9003 DECL_PENDING_INLINE_P (r
) = 0;
9004 DECL_SAVED_TREE (r
) = NULL_TREE
;
9005 DECL_STRUCT_FUNCTION (r
) = NULL
;
9007 /* We'll re-clone as appropriate in instantiate_template. */
9008 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
9010 /* If we aren't complaining now, return on error before we register
9011 the specialization so that we'll complain eventually. */
9012 if ((complain
& tf_error
) == 0
9013 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
9014 && !grok_op_properties (r
, /*complain=*/false))
9015 RETURN (error_mark_node
);
9017 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
9018 this in the special friend case mentioned above where
9019 GEN_TMPL is NULL. */
9022 DECL_TEMPLATE_INFO (r
)
9023 = build_template_info (gen_tmpl
, argvec
);
9024 SET_DECL_IMPLICIT_INSTANTIATION (r
);
9025 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
9027 /* We're not supposed to instantiate default arguments
9028 until they are called, for a template. But, for a
9031 template <class T> void f ()
9032 { extern void g(int i = T()); }
9034 we should do the substitution when the template is
9035 instantiated. We handle the member function case in
9036 instantiate_class_template since the default arguments
9037 might refer to other members of the class. */
9039 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9040 && !uses_template_parms (argvec
))
9041 tsubst_default_arguments (r
);
9044 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
9046 /* Copy the list of befriending classes. */
9047 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
9049 friends
= &TREE_CHAIN (*friends
))
9051 *friends
= copy_node (*friends
);
9052 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
9057 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
9059 maybe_retrofit_in_chrg (r
);
9060 if (DECL_CONSTRUCTOR_P (r
))
9061 grok_ctor_properties (ctx
, r
);
9062 /* If this is an instantiation of a member template, clone it.
9063 If it isn't, that'll be handled by
9064 clone_constructors_and_destructors. */
9065 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
9066 clone_function_decl (r
, /*update_method_vec_p=*/0);
9068 else if ((complain
& tf_error
) != 0
9069 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
9070 && !grok_op_properties (r
, /*complain=*/true))
9071 RETURN (error_mark_node
);
9073 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
9074 SET_DECL_FRIEND_CONTEXT (r
,
9075 tsubst (DECL_FRIEND_CONTEXT (t
),
9076 args
, complain
, in_decl
));
9078 /* Possibly limit visibility based on template args. */
9079 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
9080 if (DECL_VISIBILITY_SPECIFIED (t
))
9082 DECL_VISIBILITY_SPECIFIED (r
) = 0;
9084 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
9086 determine_visibility (r
);
9087 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
9088 && !processing_template_decl
)
9089 defaulted_late_check (r
);
9091 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
9092 args
, complain
, in_decl
);
9098 tree type
= NULL_TREE
;
9100 tree expanded_types
= NULL_TREE
;
9101 tree prev_r
= NULL_TREE
;
9102 tree first_r
= NULL_TREE
;
9104 if (FUNCTION_PARAMETER_PACK_P (t
))
9106 /* If there is a local specialization that isn't a
9107 parameter pack, it means that we're doing a "simple"
9108 substitution from inside tsubst_pack_expansion. Just
9109 return the local specialization (which will be a single
9111 tree spec
= retrieve_local_specialization (t
);
9113 && TREE_CODE (spec
) == PARM_DECL
9114 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
9117 /* Expand the TYPE_PACK_EXPANSION that provides the types for
9118 the parameters in this function parameter pack. */
9119 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
9121 if (TREE_CODE (expanded_types
) == TREE_VEC
)
9123 len
= TREE_VEC_LENGTH (expanded_types
);
9125 /* Zero-length parameter packs are boring. Just substitute
9128 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
9133 /* All we did was update the type. Make a note of that. */
9134 type
= expanded_types
;
9135 expanded_types
= NULL_TREE
;
9139 /* Loop through all of the parameter's we'll build. When T is
9140 a function parameter pack, LEN is the number of expanded
9141 types in EXPANDED_TYPES; otherwise, LEN is 1. */
9143 for (i
= 0; i
< len
; ++i
)
9147 if (DECL_TEMPLATE_PARM_P (t
))
9148 SET_DECL_TEMPLATE_PARM_P (r
);
9150 /* An argument of a function parameter pack is not a parameter
9152 FUNCTION_PARAMETER_PACK_P (r
) = false;
9155 /* We're on the Ith parameter of the function parameter
9158 /* Get the Ith type. */
9159 type
= TREE_VEC_ELT (expanded_types
, i
);
9162 /* Rename the parameter to include the index. */
9164 make_ith_pack_parameter_name (DECL_NAME (r
), i
);
9167 /* We're dealing with a normal parameter. */
9168 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9170 type
= type_decays_to (type
);
9171 TREE_TYPE (r
) = type
;
9172 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
9174 if (DECL_INITIAL (r
))
9176 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
9177 DECL_INITIAL (r
) = TREE_TYPE (r
);
9179 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
9183 DECL_CONTEXT (r
) = NULL_TREE
;
9185 if (!DECL_TEMPLATE_PARM_P (r
))
9186 DECL_ARG_TYPE (r
) = type_passed_as (type
);
9188 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
9189 args
, complain
, in_decl
);
9191 /* Keep track of the first new parameter we
9192 generate. That's what will be returned to the
9197 /* Build a proper chain of parameters when substituting
9198 into a function parameter pack. */
9200 TREE_CHAIN (prev_r
) = r
;
9204 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
,
9205 complain
, TREE_CHAIN (t
));
9207 /* FIRST_R contains the start of the chain we've built. */
9217 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9218 if (type
== error_mark_node
)
9219 RETURN (error_mark_node
);
9220 TREE_TYPE (r
) = type
;
9221 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
9223 /* DECL_INITIAL gives the number of bits in a bit-field. */
9225 = tsubst_expr (DECL_INITIAL (t
), args
,
9227 /*integral_constant_expression_p=*/true);
9228 /* We don't have to set DECL_CONTEXT here; it is set by
9229 finish_member_declaration. */
9230 TREE_CHAIN (r
) = NULL_TREE
;
9231 if (VOID_TYPE_P (type
))
9232 error ("instantiation of %q+D as type %qT", r
, type
);
9234 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
9235 args
, complain
, in_decl
);
9240 /* We reach here only for member using decls. */
9241 if (DECL_DEPENDENT_P (t
))
9243 r
= do_class_using_decl
9244 (tsubst_copy (USING_DECL_SCOPE (t
), args
, complain
, in_decl
),
9245 tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
));
9247 r
= error_mark_node
;
9250 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
9251 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
9257 TREE_CHAIN (r
) = NULL_TREE
;
9264 tree argvec
= NULL_TREE
;
9265 tree gen_tmpl
= NULL_TREE
;
9267 tree tmpl
= NULL_TREE
;
9269 tree type
= NULL_TREE
;
9272 if (TREE_CODE (t
) == TYPE_DECL
9273 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
9275 /* If this is the canonical decl, we don't have to
9276 mess with instantiations, and often we can't (for
9277 typename, template type parms and such). Note that
9278 TYPE_NAME is not correct for the above test if
9279 we've copied the type for a typedef. */
9280 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9281 if (type
== error_mark_node
)
9282 RETURN (error_mark_node
);
9283 r
= TYPE_NAME (type
);
9287 /* Check to see if we already have the specialization we
9290 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
9292 /* T is a static data member or namespace-scope entity.
9293 We have to substitute into namespace-scope variables
9294 (even though such entities are never templates) because
9297 template <class T> void f() { extern T t; }
9299 where the entity referenced is not known until
9300 instantiation time. */
9302 ctx
= DECL_CONTEXT (t
);
9303 if (DECL_CLASS_SCOPE_P (t
))
9305 ctx
= tsubst_aggr_type (ctx
, args
,
9307 in_decl
, /*entering_scope=*/1);
9308 /* If CTX is unchanged, then T is in fact the
9309 specialization we want. That situation occurs when
9310 referencing a static data member within in its own
9311 class. We can use pointer equality, rather than
9312 same_type_p, because DECL_CONTEXT is always
9314 if (ctx
== DECL_CONTEXT (t
))
9320 tmpl
= DECL_TI_TEMPLATE (t
);
9321 gen_tmpl
= most_general_template (tmpl
);
9322 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
9323 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
9324 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
9329 /* A local variable. */
9331 /* Subsequent calls to pushdecl will fill this in. */
9333 spec
= retrieve_local_specialization (t
);
9335 /* If we already have the specialization we need, there is
9336 nothing more to do. */
9343 /* Create a new node for the specialization we need. */
9345 if (type
== NULL_TREE
)
9347 if (is_typedef_decl (t
))
9348 type
= DECL_ORIGINAL_TYPE (t
);
9350 type
= TREE_TYPE (t
);
9351 type
= tsubst (type
, args
, complain
, in_decl
);
9353 if (TREE_CODE (r
) == VAR_DECL
)
9355 /* Even if the original location is out of scope, the
9356 newly substituted one is not. */
9357 DECL_DEAD_FOR_LOCAL (r
) = 0;
9358 DECL_INITIALIZED_P (r
) = 0;
9359 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
9360 if (type
== error_mark_node
)
9361 RETURN (error_mark_node
);
9362 if (TREE_CODE (type
) == FUNCTION_TYPE
)
9364 /* It may seem that this case cannot occur, since:
9369 declares a function, not a variable. However:
9372 template <typename T> void g() { T t; }
9373 template void g<f>();
9375 is an attempt to declare a variable with function
9377 error ("variable %qD has function type",
9378 /* R is not yet sufficiently initialized, so we
9379 just use its name. */
9381 RETURN (error_mark_node
);
9383 type
= complete_type (type
);
9384 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
9385 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
);
9386 type
= check_var_type (DECL_NAME (r
), type
);
9388 if (DECL_HAS_VALUE_EXPR_P (t
))
9390 tree ve
= DECL_VALUE_EXPR (t
);
9391 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
9392 /*constant_expression_p=*/false);
9393 SET_DECL_VALUE_EXPR (r
, ve
);
9396 else if (DECL_SELF_REFERENCE_P (t
))
9397 SET_DECL_SELF_REFERENCE_P (r
);
9398 TREE_TYPE (r
) = type
;
9399 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
9400 DECL_CONTEXT (r
) = ctx
;
9401 /* Clear out the mangled name and RTL for the instantiation. */
9402 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
9403 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
9404 SET_DECL_RTL (r
, NULL_RTX
);
9405 /* The initializer must not be expanded until it is required;
9407 DECL_INITIAL (r
) = NULL_TREE
;
9408 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
9409 SET_DECL_RTL (r
, NULL_RTX
);
9410 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
9411 if (TREE_CODE (r
) == VAR_DECL
)
9413 /* Possibly limit visibility based on template args. */
9414 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
9415 if (DECL_VISIBILITY_SPECIFIED (t
))
9417 DECL_VISIBILITY_SPECIFIED (r
) = 0;
9419 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
9421 determine_visibility (r
);
9426 /* A static data member declaration is always marked
9427 external when it is declared in-class, even if an
9428 initializer is present. We mimic the non-template
9430 DECL_EXTERNAL (r
) = 1;
9432 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
9433 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
9434 SET_DECL_IMPLICIT_INSTANTIATION (r
);
9436 else if (cp_unevaluated_operand
)
9438 /* We're substituting this var in a decltype outside of its
9439 scope, such as for a lambda return type. Don't add it to
9440 local_specializations, do perform auto deduction. */
9441 tree auto_node
= type_uses_auto (type
);
9443 = tsubst_expr (DECL_INITIAL (t
), args
, complain
, in_decl
,
9444 /*constant_expression_p=*/false);
9446 if (auto_node
&& init
&& describable_type (init
))
9448 type
= do_auto_deduction (type
, init
, auto_node
);
9449 TREE_TYPE (r
) = type
;
9453 register_local_specialization (r
, t
);
9455 TREE_CHAIN (r
) = NULL_TREE
;
9457 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
9459 args
, complain
, in_decl
);
9461 /* Preserve a typedef that names a type. */
9462 if (is_typedef_decl (r
))
9464 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
9465 set_underlying_type (r
);
9478 /* Restore the file and line information. */
9479 input_location
= saved_loc
;
9484 /* Substitute into the ARG_TYPES of a function type. */
9487 tsubst_arg_types (tree arg_types
,
9489 tsubst_flags_t complain
,
9492 tree remaining_arg_types
;
9493 tree type
= NULL_TREE
;
9495 tree expanded_args
= NULL_TREE
;
9498 if (!arg_types
|| arg_types
== void_list_node
)
9501 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
9502 args
, complain
, in_decl
);
9503 if (remaining_arg_types
== error_mark_node
)
9504 return error_mark_node
;
9506 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
9508 /* For a pack expansion, perform substitution on the
9509 entire expression. Later on, we'll handle the arguments
9511 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
9512 args
, complain
, in_decl
);
9514 if (TREE_CODE (expanded_args
) == TREE_VEC
)
9515 /* So that we'll spin through the parameters, one by one. */
9516 i
= TREE_VEC_LENGTH (expanded_args
);
9519 /* We only partially substituted into the parameter
9520 pack. Our type is TYPE_PACK_EXPANSION. */
9521 type
= expanded_args
;
9522 expanded_args
= NULL_TREE
;
9530 type
= TREE_VEC_ELT (expanded_args
, i
);
9532 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
9534 if (type
== error_mark_node
)
9535 return error_mark_node
;
9536 if (VOID_TYPE_P (type
))
9538 if (complain
& tf_error
)
9540 error ("invalid parameter type %qT", type
);
9542 error ("in declaration %q+D", in_decl
);
9544 return error_mark_node
;
9547 /* Do array-to-pointer, function-to-pointer conversion, and ignore
9548 top-level qualifiers as required. */
9549 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
9551 /* We do not substitute into default arguments here. The standard
9552 mandates that they be instantiated only when needed, which is
9553 done in build_over_call. */
9554 default_arg
= TREE_PURPOSE (arg_types
);
9556 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
9558 /* We've instantiated a template before its default arguments
9559 have been parsed. This can happen for a nested template
9560 class, and is not an error unless we require the default
9561 argument in a call of this function. */
9562 remaining_arg_types
=
9563 tree_cons (default_arg
, type
, remaining_arg_types
);
9564 VEC_safe_push (tree
, gc
, DEFARG_INSTANTIATIONS (default_arg
),
9565 remaining_arg_types
);
9568 remaining_arg_types
=
9569 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
9572 return remaining_arg_types
;
9575 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
9576 *not* handle the exception-specification for FNTYPE, because the
9577 initial substitution of explicitly provided template parameters
9578 during argument deduction forbids substitution into the
9579 exception-specification:
9583 All references in the function type of the function template to the
9584 corresponding template parameters are replaced by the specified tem-
9585 plate argument values. If a substitution in a template parameter or
9586 in the function type of the function template results in an invalid
9587 type, type deduction fails. [Note: The equivalent substitution in
9588 exception specifications is done only when the function is instanti-
9589 ated, at which point a program is ill-formed if the substitution
9590 results in an invalid type.] */
9593 tsubst_function_type (tree t
,
9595 tsubst_flags_t complain
,
9602 /* The TYPE_CONTEXT is not used for function/method types. */
9603 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
9605 /* Substitute the return type. */
9606 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9607 if (return_type
== error_mark_node
)
9608 return error_mark_node
;
9609 /* The standard does not presently indicate that creation of a
9610 function type with an invalid return type is a deduction failure.
9611 However, that is clearly analogous to creating an array of "void"
9612 or a reference to a reference. This is core issue #486. */
9613 if (TREE_CODE (return_type
) == ARRAY_TYPE
9614 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
9616 if (complain
& tf_error
)
9618 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
9619 error ("function returning an array");
9621 error ("function returning a function");
9623 return error_mark_node
;
9626 /* Substitute the argument types. */
9627 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
,
9629 if (arg_types
== error_mark_node
)
9630 return error_mark_node
;
9632 /* Construct a new type node and return it. */
9633 if (TREE_CODE (t
) == FUNCTION_TYPE
)
9634 fntype
= build_function_type (return_type
, arg_types
);
9637 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
9638 if (! MAYBE_CLASS_TYPE_P (r
))
9642 Type deduction may fail for any of the following
9645 -- Attempting to create "pointer to member of T" when T
9646 is not a class type. */
9647 if (complain
& tf_error
)
9648 error ("creating pointer to member function of non-class type %qT",
9650 return error_mark_node
;
9653 fntype
= build_method_type_directly (r
, return_type
,
9654 TREE_CHAIN (arg_types
));
9656 fntype
= cp_build_qualified_type_real (fntype
, TYPE_QUALS (t
), complain
);
9657 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
9662 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
9663 ARGS into that specification, and return the substituted
9664 specification. If there is no specification, return NULL_TREE. */
9667 tsubst_exception_specification (tree fntype
,
9669 tsubst_flags_t complain
,
9675 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
9676 new_specs
= NULL_TREE
;
9679 if (! TREE_VALUE (specs
))
9686 tree expanded_specs
= NULL_TREE
;
9688 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
9690 /* Expand the pack expansion type. */
9691 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
9695 if (expanded_specs
== error_mark_node
)
9696 return error_mark_node
;
9697 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
9698 len
= TREE_VEC_LENGTH (expanded_specs
);
9701 /* We're substituting into a member template, so
9702 we got a TYPE_PACK_EXPANSION back. Add that
9703 expansion and move on. */
9704 gcc_assert (TREE_CODE (expanded_specs
)
9705 == TYPE_PACK_EXPANSION
);
9706 new_specs
= add_exception_specifier (new_specs
,
9709 specs
= TREE_CHAIN (specs
);
9714 for (i
= 0; i
< len
; ++i
)
9717 spec
= TREE_VEC_ELT (expanded_specs
, i
);
9719 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
9720 if (spec
== error_mark_node
)
9722 new_specs
= add_exception_specifier (new_specs
, spec
,
9726 specs
= TREE_CHAIN (specs
);
9732 /* Take the tree structure T and replace template parameters used
9733 therein with the argument vector ARGS. IN_DECL is an associated
9734 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
9735 Issue error and warning messages under control of COMPLAIN. Note
9736 that we must be relatively non-tolerant of extensions here, in
9737 order to preserve conformance; if we allow substitutions that
9738 should not be allowed, we may allow argument deductions that should
9739 not succeed, and therefore report ambiguous overload situations
9740 where there are none. In theory, we could allow the substitution,
9741 but indicate that it should have failed, and allow our caller to
9742 make sure that the right thing happens, but we don't try to do this
9745 This function is used for dealing with types, decls and the like;
9746 for expressions, use tsubst_expr or tsubst_copy. */
9749 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9753 if (t
== NULL_TREE
|| t
== error_mark_node
9754 || t
== integer_type_node
9755 || t
== void_type_node
9756 || t
== char_type_node
9757 || t
== unknown_type_node
9758 || TREE_CODE (t
) == NAMESPACE_DECL
)
9762 return tsubst_decl (t
, args
, complain
);
9764 if (args
== NULL_TREE
)
9767 if (TREE_CODE (t
) == IDENTIFIER_NODE
)
9768 type
= IDENTIFIER_TYPE_VALUE (t
);
9770 type
= TREE_TYPE (t
);
9772 gcc_assert (type
!= unknown_type_node
);
9774 /* Reuse typedefs. We need to do this to handle dependent attributes,
9775 such as attribute aligned. */
9778 && TYPE_NAME (t
) != TYPE_MAIN_DECL (t
))
9780 tree decl
= TYPE_NAME (t
);
9782 if (DECL_CLASS_SCOPE_P (decl
)
9783 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
9784 && uses_template_parms (DECL_CONTEXT (decl
)))
9786 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
9787 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
9788 r
= retrieve_specialization (tmpl
, gen_args
, 0);
9790 else if (DECL_FUNCTION_SCOPE_P (decl
)
9791 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
9792 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
9793 r
= retrieve_local_specialization (decl
);
9795 /* The typedef is from a non-template context. */
9801 r
= cp_build_qualified_type_real
9802 (r
, cp_type_quals (t
) | cp_type_quals (r
),
9803 complain
| tf_ignore_bad_quals
);
9806 /* Else we must be instantiating the typedef, so fall through. */
9810 && TREE_CODE (t
) != TYPENAME_TYPE
9811 && TREE_CODE (t
) != IDENTIFIER_NODE
9812 && TREE_CODE (t
) != FUNCTION_TYPE
9813 && TREE_CODE (t
) != METHOD_TYPE
)
9814 type
= tsubst (type
, args
, complain
, in_decl
);
9815 if (type
== error_mark_node
)
9816 return error_mark_node
;
9818 switch (TREE_CODE (t
))
9823 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
9824 /*entering_scope=*/0);
9827 case IDENTIFIER_NODE
:
9839 if (t
== integer_type_node
)
9842 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
9843 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
9847 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
9849 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
9850 /*integral_constant_expression_p=*/false);
9852 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
9854 if (TREE_CODE (max
) == NOP_EXPR
9855 && TREE_SIDE_EFFECTS (omax
)
9856 && !TREE_TYPE (max
))
9857 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
9859 max
= fold_decl_constant_value (max
);
9861 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
9862 with TREE_SIDE_EFFECTS that indicates this is not an integral
9863 constant expression. */
9864 if (processing_template_decl
9865 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
9867 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
9868 TREE_SIDE_EFFECTS (max
) = 1;
9871 if (TREE_CODE (max
) != INTEGER_CST
9872 && !at_function_scope_p ()
9873 && !TREE_SIDE_EFFECTS (max
)
9874 && !value_dependent_expression_p (max
))
9876 if (complain
& tf_error
)
9877 error ("array bound is not an integer constant");
9878 return error_mark_node
;
9883 Type deduction may fail for any of the following
9886 Attempting to create an array with a size that is
9887 zero or negative. */
9888 if (integer_zerop (max
) && !(complain
& tf_error
))
9889 /* We must fail if performing argument deduction (as
9890 indicated by the state of complain), so that
9891 another substitution can be found. */
9892 return error_mark_node
;
9893 else if (TREE_CODE (max
) == INTEGER_CST
9894 && INT_CST_LT (max
, integer_zero_node
))
9896 if (complain
& tf_error
)
9897 error ("creating array with negative size (%qE)", max
);
9899 return error_mark_node
;
9902 return compute_array_index_type (NULL_TREE
, max
);
9905 case TEMPLATE_TYPE_PARM
:
9906 case TEMPLATE_TEMPLATE_PARM
:
9907 case BOUND_TEMPLATE_TEMPLATE_PARM
:
9908 case TEMPLATE_PARM_INDEX
:
9913 tree arg
= NULL_TREE
;
9917 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
9918 template_parm_level_and_index (t
, &level
, &idx
);
9920 levels
= TMPL_ARGS_DEPTH (args
);
9921 if (level
<= levels
)
9923 arg
= TMPL_ARG (args
, level
, idx
);
9925 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
9926 /* See through ARGUMENT_PACK_SELECT arguments. */
9927 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
9930 if (arg
== error_mark_node
)
9931 return error_mark_node
;
9932 else if (arg
!= NULL_TREE
)
9934 if (ARGUMENT_PACK_P (arg
))
9935 /* If ARG is an argument pack, we don't actually want to
9936 perform a substitution here, because substitutions
9937 for argument packs are only done
9938 element-by-element. We can get to this point when
9939 substituting the type of a non-type template
9940 parameter pack, when that type actually contains
9941 template parameter packs from an outer template, e.g.,
9943 template<typename... Types> struct A {
9944 template<Types... Values> struct B { };
9948 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
9951 gcc_assert (TYPE_P (arg
));
9953 /* cv-quals from the template are discarded when
9954 substituting in a function or reference type. */
9955 if (TREE_CODE (arg
) == FUNCTION_TYPE
9956 || TREE_CODE (arg
) == METHOD_TYPE
9957 || TREE_CODE (arg
) == REFERENCE_TYPE
)
9958 quals
= cp_type_quals (arg
);
9960 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
9962 return cp_build_qualified_type_real
9963 (arg
, quals
, complain
| tf_ignore_bad_quals
);
9965 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
9967 /* We are processing a type constructed from a
9968 template template parameter. */
9969 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
9970 args
, complain
, in_decl
);
9971 if (argvec
== error_mark_node
)
9972 return error_mark_node
;
9974 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
9975 are resolving nested-types in the signature of a
9976 member function templates. Otherwise ARG is a
9977 TEMPLATE_DECL and is the real template to be
9979 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
9980 arg
= TYPE_NAME (arg
);
9982 r
= lookup_template_class (arg
,
9985 /*entering_scope=*/0,
9987 return cp_build_qualified_type_real
9988 (r
, TYPE_QUALS (t
), complain
);
9991 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
9996 /* This can happen during the attempted tsubst'ing in
9997 unify. This means that we don't yet have any information
9998 about the template parameter in question. */
10001 /* If we get here, we must have been looking at a parm for a
10002 more deeply nested template. Make a new version of this
10003 template parameter, but with a lower level. */
10004 switch (TREE_CODE (t
))
10006 case TEMPLATE_TYPE_PARM
:
10007 case TEMPLATE_TEMPLATE_PARM
:
10008 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10009 if (cp_type_quals (t
))
10011 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
10012 r
= cp_build_qualified_type_real
10013 (r
, cp_type_quals (t
),
10014 complain
| (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
10015 ? tf_ignore_bad_quals
: 0));
10020 TEMPLATE_TYPE_PARM_INDEX (r
)
10021 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
10022 r
, levels
, args
, complain
);
10023 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
10024 TYPE_MAIN_VARIANT (r
) = r
;
10025 TYPE_POINTER_TO (r
) = NULL_TREE
;
10026 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
10028 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
10029 /* We have reduced the level of the template
10030 template parameter, but not the levels of its
10031 template parameters, so canonical_type_parameter
10032 will not be able to find the canonical template
10033 template parameter for this level. Thus, we
10034 require structural equality checking to compare
10035 TEMPLATE_TEMPLATE_PARMs. */
10036 SET_TYPE_STRUCTURAL_EQUALITY (r
);
10037 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
10038 SET_TYPE_STRUCTURAL_EQUALITY (r
);
10040 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
10042 if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
10044 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
10045 complain
, in_decl
);
10046 if (argvec
== error_mark_node
)
10047 return error_mark_node
;
10049 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
10050 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
10055 case TEMPLATE_PARM_INDEX
:
10056 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
10060 gcc_unreachable ();
10068 tree purpose
, value
, chain
;
10070 if (t
== void_list_node
)
10073 purpose
= TREE_PURPOSE (t
);
10076 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
10077 if (purpose
== error_mark_node
)
10078 return error_mark_node
;
10080 value
= TREE_VALUE (t
);
10083 value
= tsubst (value
, args
, complain
, in_decl
);
10084 if (value
== error_mark_node
)
10085 return error_mark_node
;
10087 chain
= TREE_CHAIN (t
);
10088 if (chain
&& chain
!= void_type_node
)
10090 chain
= tsubst (chain
, args
, complain
, in_decl
);
10091 if (chain
== error_mark_node
)
10092 return error_mark_node
;
10094 if (purpose
== TREE_PURPOSE (t
)
10095 && value
== TREE_VALUE (t
)
10096 && chain
== TREE_CHAIN (t
))
10098 return hash_tree_cons (purpose
, value
, chain
);
10102 /* We should never be tsubsting a binfo. */
10103 gcc_unreachable ();
10106 /* A vector of template arguments. */
10107 gcc_assert (!type
);
10108 return tsubst_template_args (t
, args
, complain
, in_decl
);
10111 case REFERENCE_TYPE
:
10113 enum tree_code code
;
10115 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
10118 code
= TREE_CODE (t
);
10123 Type deduction may fail for any of the following
10126 -- Attempting to create a pointer to reference type.
10127 -- Attempting to create a reference to a reference type or
10128 a reference to void.
10130 Core issue 106 says that creating a reference to a reference
10131 during instantiation is no longer a cause for failure. We
10132 only enforce this check in strict C++98 mode. */
10133 if ((TREE_CODE (type
) == REFERENCE_TYPE
10134 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
10135 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
10137 static location_t last_loc
;
10139 /* We keep track of the last time we issued this error
10140 message to avoid spewing a ton of messages during a
10141 single bad template instantiation. */
10142 if (complain
& tf_error
10143 && last_loc
!= input_location
)
10145 if (TREE_CODE (type
) == VOID_TYPE
)
10146 error ("forming reference to void");
10147 else if (code
== POINTER_TYPE
)
10148 error ("forming pointer to reference type %qT", type
);
10150 error ("forming reference to reference type %qT", type
);
10151 last_loc
= input_location
;
10154 return error_mark_node
;
10156 else if (code
== POINTER_TYPE
)
10158 r
= build_pointer_type (type
);
10159 if (TREE_CODE (type
) == METHOD_TYPE
)
10160 r
= build_ptrmemfunc_type (r
);
10162 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
10163 /* In C++0x, during template argument substitution, when there is an
10164 attempt to create a reference to a reference type, reference
10165 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
10167 "If a template-argument for a template-parameter T names a type
10168 that is a reference to a type A, an attempt to create the type
10169 'lvalue reference to cv T' creates the type 'lvalue reference to
10170 A,' while an attempt to create the type type rvalue reference to
10171 cv T' creates the type T"
10173 r
= cp_build_reference_type
10175 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
10177 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
10178 r
= cp_build_qualified_type_real (r
, TYPE_QUALS (t
), complain
);
10180 if (r
!= error_mark_node
)
10181 /* Will this ever be needed for TYPE_..._TO values? */
10188 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
10189 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
10193 Type deduction may fail for any of the following
10196 -- Attempting to create "pointer to member of T" when T
10197 is not a class type. */
10198 if (complain
& tf_error
)
10199 error ("creating pointer to member of non-class type %qT", r
);
10200 return error_mark_node
;
10202 if (TREE_CODE (type
) == REFERENCE_TYPE
)
10204 if (complain
& tf_error
)
10205 error ("creating pointer to member reference type %qT", type
);
10206 return error_mark_node
;
10208 if (TREE_CODE (type
) == VOID_TYPE
)
10210 if (complain
& tf_error
)
10211 error ("creating pointer to member of type void");
10212 return error_mark_node
;
10214 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
10215 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10217 /* The type of the implicit object parameter gets its
10218 cv-qualifiers from the FUNCTION_TYPE. */
10220 tree method_type
= build_memfn_type (type
, r
, cp_type_quals (type
));
10221 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
10222 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
10226 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
10230 case FUNCTION_TYPE
:
10235 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
10236 if (fntype
== error_mark_node
)
10237 return error_mark_node
;
10239 /* Substitute the exception specification. */
10240 specs
= tsubst_exception_specification (t
, args
, complain
,
10242 if (specs
== error_mark_node
)
10243 return error_mark_node
;
10245 fntype
= build_exception_variant (fntype
, specs
);
10250 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
10251 if (domain
== error_mark_node
)
10252 return error_mark_node
;
10254 /* As an optimization, we avoid regenerating the array type if
10255 it will obviously be the same as T. */
10256 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
10259 /* These checks should match the ones in grokdeclarator.
10263 The deduction may fail for any of the following reasons:
10265 -- Attempting to create an array with an element type that
10266 is void, a function type, or a reference type, or [DR337]
10267 an abstract class type. */
10268 if (TREE_CODE (type
) == VOID_TYPE
10269 || TREE_CODE (type
) == FUNCTION_TYPE
10270 || TREE_CODE (type
) == REFERENCE_TYPE
)
10272 if (complain
& tf_error
)
10273 error ("creating array of %qT", type
);
10274 return error_mark_node
;
10276 if (CLASS_TYPE_P (type
) && CLASSTYPE_PURE_VIRTUALS (type
))
10278 if (complain
& tf_error
)
10279 error ("creating array of %qT, which is an abstract class type",
10281 return error_mark_node
;
10284 r
= build_cplus_array_type (type
, domain
);
10286 if (TYPE_USER_ALIGN (t
))
10288 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
10289 TYPE_USER_ALIGN (r
) = 1;
10298 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
10299 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
10301 if (e1
== error_mark_node
|| e2
== error_mark_node
)
10302 return error_mark_node
;
10304 return fold_build2_loc (input_location
,
10305 TREE_CODE (t
), TREE_TYPE (t
), e1
, e2
);
10311 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
10312 if (e
== error_mark_node
)
10313 return error_mark_node
;
10315 return fold_build1_loc (input_location
, TREE_CODE (t
), TREE_TYPE (t
), e
);
10318 case TYPENAME_TYPE
:
10320 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
10321 in_decl
, /*entering_scope=*/1);
10322 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
10323 complain
, in_decl
);
10325 if (ctx
== error_mark_node
|| f
== error_mark_node
)
10326 return error_mark_node
;
10328 if (!MAYBE_CLASS_TYPE_P (ctx
))
10330 if (complain
& tf_error
)
10331 error ("%qT is not a class, struct, or union type", ctx
);
10332 return error_mark_node
;
10334 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
10336 /* Normally, make_typename_type does not require that the CTX
10337 have complete type in order to allow things like:
10339 template <class T> struct S { typename S<T>::X Y; };
10341 But, such constructs have already been resolved by this
10342 point, so here CTX really should have complete type, unless
10343 it's a partial instantiation. */
10344 if (!(complain
& tf_no_class_instantiations
))
10345 ctx
= complete_type (ctx
);
10346 if (!COMPLETE_TYPE_P (ctx
))
10348 if (complain
& tf_error
)
10349 cxx_incomplete_type_error (NULL_TREE
, ctx
);
10350 return error_mark_node
;
10354 f
= make_typename_type (ctx
, f
, typename_type
,
10355 (complain
& tf_error
) | tf_keep_type_decl
);
10356 if (f
== error_mark_node
)
10358 if (TREE_CODE (f
) == TYPE_DECL
)
10360 complain
|= tf_ignore_bad_quals
;
10364 if (TREE_CODE (f
) != TYPENAME_TYPE
)
10366 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
10367 error ("%qT resolves to %qT, which is not an enumeration type",
10369 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
10370 error ("%qT resolves to %qT, which is is not a class type",
10374 return cp_build_qualified_type_real
10375 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
10378 case UNBOUND_CLASS_TEMPLATE
:
10380 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
10381 in_decl
, /*entering_scope=*/1);
10382 tree name
= TYPE_IDENTIFIER (t
);
10383 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
10385 if (ctx
== error_mark_node
|| name
== error_mark_node
)
10386 return error_mark_node
;
10389 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
10390 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
10396 gcc_unreachable ();
10400 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
10401 tree e2
= tsubst_expr (TREE_OPERAND (t
, 1), args
, complain
, in_decl
,
10402 /*integral_constant_expression_p=*/false);
10403 if (e1
== error_mark_node
|| e2
== error_mark_node
)
10404 return error_mark_node
;
10406 return build_nt (ARRAY_REF
, e1
, e2
, NULL_TREE
, NULL_TREE
);
10411 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
10412 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
10413 if (e1
== error_mark_node
|| e2
== error_mark_node
)
10414 return error_mark_node
;
10416 return build_qualified_name (/*type=*/NULL_TREE
,
10417 e1
, e2
, QUALIFIED_NAME_IS_TEMPLATE (t
));
10424 ++cp_unevaluated_operand
;
10425 ++c_inhibit_evaluation_warnings
;
10427 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
10429 /*integral_constant_expression_p=*/false);
10431 --cp_unevaluated_operand
;
10432 --c_inhibit_evaluation_warnings
;
10434 type
= finish_typeof (type
);
10435 return cp_build_qualified_type_real (type
,
10437 | cp_type_quals (type
),
10441 case DECLTYPE_TYPE
:
10445 ++cp_unevaluated_operand
;
10446 ++c_inhibit_evaluation_warnings
;
10448 type
= tsubst_expr (DECLTYPE_TYPE_EXPR (t
), args
,
10450 /*integral_constant_expression_p=*/false);
10452 --cp_unevaluated_operand
;
10453 --c_inhibit_evaluation_warnings
;
10455 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
10456 type
= lambda_capture_field_type (type
);
10457 else if (DECLTYPE_FOR_LAMBDA_RETURN (t
))
10458 type
= lambda_return_type (type
);
10460 type
= finish_decltype_type
10461 (type
, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
));
10462 return cp_build_qualified_type_real (type
,
10464 | cp_type_quals (type
),
10468 case TYPE_ARGUMENT_PACK
:
10469 case NONTYPE_ARGUMENT_PACK
:
10471 tree r
= TYPE_P (t
)
10472 ? cxx_make_type (TREE_CODE (t
))
10473 : make_node (TREE_CODE (t
));
10475 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
10479 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
10481 /* For template nontype argument packs, also substitute into
10483 if (TREE_CODE (t
) == NONTYPE_ARGUMENT_PACK
)
10484 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10491 sorry ("use of %qs in template",
10492 tree_code_name
[(int) TREE_CODE (t
)]);
10493 return error_mark_node
;
10497 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
10498 type of the expression on the left-hand side of the "." or "->"
10502 tsubst_baselink (tree baselink
, tree object_type
,
10503 tree args
, tsubst_flags_t complain
, tree in_decl
)
10506 tree qualifying_scope
;
10509 tree template_args
= 0;
10510 bool template_id_p
= false;
10512 /* A baselink indicates a function from a base class. Both the
10513 BASELINK_ACCESS_BINFO and the base class referenced may
10514 indicate bases of the template class, rather than the
10515 instantiated class. In addition, lookups that were not
10516 ambiguous before may be ambiguous now. Therefore, we perform
10517 the lookup again. */
10518 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
10519 qualifying_scope
= tsubst (qualifying_scope
, args
,
10520 complain
, in_decl
);
10521 fns
= BASELINK_FUNCTIONS (baselink
);
10522 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
10523 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
10525 template_id_p
= true;
10526 template_args
= TREE_OPERAND (fns
, 1);
10527 fns
= TREE_OPERAND (fns
, 0);
10529 template_args
= tsubst_template_args (template_args
, args
,
10530 complain
, in_decl
);
10532 name
= DECL_NAME (get_first_fn (fns
));
10533 if (IDENTIFIER_TYPENAME_P (name
))
10534 name
= mangle_conv_op_name_for_type (optype
);
10535 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
10537 /* If lookup found a single function, mark it as used at this
10538 point. (If it lookup found multiple functions the one selected
10539 later by overload resolution will be marked as used at that
10541 if (BASELINK_P (baselink
))
10542 fns
= BASELINK_FUNCTIONS (baselink
);
10543 if (!template_id_p
&& !really_overloaded_fn (fns
))
10544 mark_used (OVL_CURRENT (fns
));
10546 /* Add back the template arguments, if present. */
10547 if (BASELINK_P (baselink
) && template_id_p
)
10548 BASELINK_FUNCTIONS (baselink
)
10549 = build_nt (TEMPLATE_ID_EXPR
,
10550 BASELINK_FUNCTIONS (baselink
),
10552 /* Update the conversion operator type. */
10553 BASELINK_OPTYPE (baselink
) = optype
;
10556 object_type
= current_class_type
;
10557 return adjust_result_of_qualified_name_lookup (baselink
,
10562 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
10563 true if the qualified-id will be a postfix-expression in-and-of
10564 itself; false if more of the postfix-expression follows the
10565 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
10569 tsubst_qualified_id (tree qualified_id
, tree args
,
10570 tsubst_flags_t complain
, tree in_decl
,
10571 bool done
, bool address_p
)
10577 tree template_args
;
10579 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
10581 /* Figure out what name to look up. */
10582 name
= TREE_OPERAND (qualified_id
, 1);
10583 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
10585 is_template
= true;
10586 template_args
= TREE_OPERAND (name
, 1);
10588 template_args
= tsubst_template_args (template_args
, args
,
10589 complain
, in_decl
);
10590 name
= TREE_OPERAND (name
, 0);
10594 is_template
= false;
10595 template_args
= NULL_TREE
;
10598 /* Substitute into the qualifying scope. When there are no ARGS, we
10599 are just trying to simplify a non-dependent expression. In that
10600 case the qualifying scope may be dependent, and, in any case,
10601 substituting will not help. */
10602 scope
= TREE_OPERAND (qualified_id
, 0);
10605 scope
= tsubst (scope
, args
, complain
, in_decl
);
10606 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
10611 if (dependent_type_p (scope
))
10613 tree type
= NULL_TREE
;
10614 if (DECL_P (expr
) && !dependent_scope_p (scope
))
10615 type
= TREE_TYPE (expr
);
10616 return build_qualified_name (type
, scope
, expr
,
10617 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
10620 if (!BASELINK_P (name
) && !DECL_P (expr
))
10622 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
10624 /* A BIT_NOT_EXPR is used to represent a destructor. */
10625 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
10627 error ("qualifying type %qT does not match destructor name ~%qT",
10628 scope
, TREE_OPERAND (expr
, 0));
10629 expr
= error_mark_node
;
10632 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
10633 /*is_type_p=*/0, false);
10636 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
10637 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
10638 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
10640 if (complain
& tf_error
)
10642 error ("dependent-name %qE is parsed as a non-type, but "
10643 "instantiation yields a type", qualified_id
);
10644 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
10646 return error_mark_node
;
10652 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
10654 /* Remember that there was a reference to this entity. */
10658 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
10660 if (complain
& tf_error
)
10661 qualified_name_lookup_error (scope
,
10662 TREE_OPERAND (qualified_id
, 1),
10663 expr
, input_location
);
10664 return error_mark_node
;
10668 expr
= lookup_template_function (expr
, template_args
);
10670 if (expr
== error_mark_node
&& complain
& tf_error
)
10671 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
10672 expr
, input_location
);
10673 else if (TYPE_P (scope
))
10675 expr
= (adjust_result_of_qualified_name_lookup
10676 (expr
, scope
, current_class_type
));
10677 expr
= (finish_qualified_id_expr
10678 (scope
, expr
, done
, address_p
,
10679 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
10680 /*template_arg_p=*/false));
10683 /* Expressions do not generally have reference type. */
10684 if (TREE_CODE (expr
) != SCOPE_REF
10685 /* However, if we're about to form a pointer-to-member, we just
10686 want the referenced member referenced. */
10687 && TREE_CODE (expr
) != OFFSET_REF
)
10688 expr
= convert_from_reference (expr
);
10693 /* Like tsubst, but deals with expressions. This function just replaces
10694 template parms; to finish processing the resultant expression, use
10698 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10700 enum tree_code code
;
10703 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
10706 code
= TREE_CODE (t
);
10711 r
= retrieve_local_specialization (t
);
10716 /* This can happen for a parameter name used later in a function
10717 declaration (such as in a late-specified return type). Just
10718 make a dummy decl, since it's only used for its type. */
10719 gcc_assert (cp_unevaluated_operand
!= 0);
10720 /* We copy T because want to tsubst the PARM_DECL only,
10721 not the following PARM_DECLs that are chained to T. */
10723 r
= tsubst_decl (c
, args
, complain
);
10724 /* Give it the template pattern as its context; its true context
10725 hasn't been instantiated yet and this is good enough for
10727 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
10730 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
10731 r
= ARGUMENT_PACK_SELECT_ARG (r
);
10740 if (DECL_TEMPLATE_PARM_P (t
))
10741 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
10742 /* There is no need to substitute into namespace-scope
10744 if (DECL_NAMESPACE_SCOPE_P (t
))
10746 /* If ARGS is NULL, then T is known to be non-dependent. */
10747 if (args
== NULL_TREE
)
10748 return integral_constant_value (t
);
10750 /* Unfortunately, we cannot just call lookup_name here.
10753 template <int I> int f() {
10755 struct S { void g() { E e = a; } };
10758 When we instantiate f<7>::S::g(), say, lookup_name is not
10759 clever enough to find f<7>::a. */
10761 = tsubst_aggr_type (TREE_TYPE (t
), args
, complain
, in_decl
,
10762 /*entering_scope=*/0);
10764 for (v
= TYPE_VALUES (enum_type
);
10766 v
= TREE_CHAIN (v
))
10767 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
10768 return TREE_VALUE (v
);
10770 /* We didn't find the name. That should never happen; if
10771 name-lookup found it during preliminary parsing, we
10772 should find it again here during instantiation. */
10773 gcc_unreachable ();
10778 if (DECL_CONTEXT (t
))
10782 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
10783 /*entering_scope=*/1);
10784 if (ctx
!= DECL_CONTEXT (t
))
10786 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
10789 if (complain
& tf_error
)
10790 error ("using invalid field %qD", t
);
10791 return error_mark_node
;
10800 case FUNCTION_DECL
:
10801 if ((DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
10802 || local_variable_p (t
))
10803 t
= tsubst (t
, args
, complain
, in_decl
);
10808 return tsubst_baselink (t
, current_class_type
, args
, complain
, in_decl
);
10810 case TEMPLATE_DECL
:
10811 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10812 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
10813 args
, complain
, in_decl
);
10814 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
10815 return tsubst (t
, args
, complain
, in_decl
);
10816 else if (DECL_CLASS_SCOPE_P (t
)
10817 && uses_template_parms (DECL_CONTEXT (t
)))
10819 /* Template template argument like the following example need
10822 template <template <class> class TT> struct C {};
10823 template <class T> struct D {
10824 template <class U> struct E {};
10829 We are processing the template argument `E' in #1 for
10830 the template instantiation #2. Originally, `E' is a
10831 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
10832 have to substitute this with one having context `D<int>'. */
10834 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
10835 return lookup_field (context
, DECL_NAME(t
), 0, false);
10838 /* Ordinary template template argument. */
10842 case REINTERPRET_CAST_EXPR
:
10843 case CONST_CAST_EXPR
:
10844 case STATIC_CAST_EXPR
:
10845 case DYNAMIC_CAST_EXPR
:
10848 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
10849 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
10852 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
10854 /* We only want to compute the number of arguments. */
10855 tree expanded
= tsubst_pack_expansion (TREE_OPERAND (t
, 0), args
,
10856 complain
, in_decl
);
10859 if (TREE_CODE (expanded
) == TREE_VEC
)
10860 len
= TREE_VEC_LENGTH (expanded
);
10862 if (expanded
== error_mark_node
)
10863 return error_mark_node
;
10864 else if (PACK_EXPANSION_P (expanded
)
10865 || (TREE_CODE (expanded
) == TREE_VEC
10867 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
10869 if (TREE_CODE (expanded
) == TREE_VEC
)
10870 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
10872 if (TYPE_P (expanded
))
10873 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
10874 complain
& tf_error
);
10876 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
10877 complain
& tf_error
);
10880 return build_int_cst (size_type_node
, len
);
10886 case TRUTH_NOT_EXPR
:
10889 case UNARY_PLUS_EXPR
: /* Unary + */
10894 case REALPART_EXPR
:
10895 case IMAGPART_EXPR
:
10897 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
10898 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
10900 case COMPONENT_REF
:
10905 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
10906 name
= TREE_OPERAND (t
, 1);
10907 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
10909 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
10910 complain
, in_decl
);
10911 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
10913 else if (TREE_CODE (name
) == SCOPE_REF
10914 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
10916 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
10917 complain
, in_decl
);
10918 name
= TREE_OPERAND (name
, 1);
10919 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
10920 complain
, in_decl
);
10921 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
10922 name
= build_qualified_name (/*type=*/NULL_TREE
,
10924 /*template_p=*/false);
10926 else if (TREE_CODE (name
) == BASELINK
)
10927 name
= tsubst_baselink (name
,
10928 non_reference (TREE_TYPE (object
)),
10932 name
= tsubst_copy (name
, args
, complain
, in_decl
);
10933 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
10939 case TRUNC_DIV_EXPR
:
10940 case CEIL_DIV_EXPR
:
10941 case FLOOR_DIV_EXPR
:
10942 case ROUND_DIV_EXPR
:
10943 case EXACT_DIV_EXPR
:
10947 case TRUNC_MOD_EXPR
:
10948 case FLOOR_MOD_EXPR
:
10949 case TRUTH_ANDIF_EXPR
:
10950 case TRUTH_ORIF_EXPR
:
10951 case TRUTH_AND_EXPR
:
10952 case TRUTH_OR_EXPR
:
10965 case COMPOUND_EXPR
:
10968 case PREDECREMENT_EXPR
:
10969 case PREINCREMENT_EXPR
:
10970 case POSTDECREMENT_EXPR
:
10971 case POSTINCREMENT_EXPR
:
10973 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
10974 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
10977 return build_qualified_name (/*type=*/NULL_TREE
,
10978 tsubst_copy (TREE_OPERAND (t
, 0),
10979 args
, complain
, in_decl
),
10980 tsubst_copy (TREE_OPERAND (t
, 1),
10981 args
, complain
, in_decl
),
10982 QUALIFIED_NAME_IS_TEMPLATE (t
));
10987 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
10988 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
10989 NULL_TREE
, NULL_TREE
);
10993 int n
= VL_EXP_OPERAND_LENGTH (t
);
10994 tree result
= build_vl_exp (CALL_EXPR
, n
);
10996 for (i
= 0; i
< n
; i
++)
10997 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
10998 complain
, in_decl
);
11004 case PSEUDO_DTOR_EXPR
:
11007 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11008 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
11009 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
11010 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
11017 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11018 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
11019 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
11020 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
11027 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11028 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
11029 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
11030 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
11034 case TEMPLATE_ID_EXPR
:
11036 /* Substituted template arguments */
11037 tree fn
= TREE_OPERAND (t
, 0);
11038 tree targs
= TREE_OPERAND (t
, 1);
11040 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
11042 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
11044 return lookup_template_function (fn
, targs
);
11049 tree purpose
, value
, chain
;
11051 if (t
== void_list_node
)
11054 purpose
= TREE_PURPOSE (t
);
11056 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
11057 value
= TREE_VALUE (t
);
11059 value
= tsubst_copy (value
, args
, complain
, in_decl
);
11060 chain
= TREE_CHAIN (t
);
11061 if (chain
&& chain
!= void_type_node
)
11062 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
11063 if (purpose
== TREE_PURPOSE (t
)
11064 && value
== TREE_VALUE (t
)
11065 && chain
== TREE_CHAIN (t
))
11067 return tree_cons (purpose
, value
, chain
);
11072 case ENUMERAL_TYPE
:
11074 case TEMPLATE_TYPE_PARM
:
11075 case TEMPLATE_TEMPLATE_PARM
:
11076 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11077 case TEMPLATE_PARM_INDEX
:
11079 case REFERENCE_TYPE
:
11081 case FUNCTION_TYPE
:
11084 case TYPENAME_TYPE
:
11085 case UNBOUND_CLASS_TEMPLATE
:
11087 case DECLTYPE_TYPE
:
11089 return tsubst (t
, args
, complain
, in_decl
);
11091 case IDENTIFIER_NODE
:
11092 if (IDENTIFIER_TYPENAME_P (t
))
11094 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11095 return mangle_conv_op_name_for_type (new_type
);
11101 /* This is handled by tsubst_copy_and_build. */
11102 gcc_unreachable ();
11105 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
11107 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
11109 case CLEANUP_POINT_EXPR
:
11110 /* We shouldn't have built any of these during initial template
11111 generation. Instead, they should be built during instantiation
11112 in response to the saved STMT_IS_FULL_EXPR_P setting. */
11113 gcc_unreachable ();
11116 mark_used (TREE_OPERAND (t
, 1));
11119 case EXPR_PACK_EXPANSION
:
11120 error ("invalid use of pack expansion expression");
11121 return error_mark_node
;
11123 case NONTYPE_ARGUMENT_PACK
:
11124 error ("use %<...%> to expand argument pack");
11125 return error_mark_node
;
11132 /* Like tsubst_copy, but specifically for OpenMP clauses. */
11135 tsubst_omp_clauses (tree clauses
, tree args
, tsubst_flags_t complain
,
11138 tree new_clauses
= NULL
, nc
, oc
;
11140 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
11142 nc
= copy_node (oc
);
11143 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
11146 switch (OMP_CLAUSE_CODE (nc
))
11148 case OMP_CLAUSE_LASTPRIVATE
:
11149 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
11151 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
11152 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
11153 in_decl
, /*integral_constant_expression_p=*/false);
11154 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
11155 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
11158 case OMP_CLAUSE_PRIVATE
:
11159 case OMP_CLAUSE_SHARED
:
11160 case OMP_CLAUSE_FIRSTPRIVATE
:
11161 case OMP_CLAUSE_REDUCTION
:
11162 case OMP_CLAUSE_COPYIN
:
11163 case OMP_CLAUSE_COPYPRIVATE
:
11164 case OMP_CLAUSE_IF
:
11165 case OMP_CLAUSE_NUM_THREADS
:
11166 case OMP_CLAUSE_SCHEDULE
:
11167 case OMP_CLAUSE_COLLAPSE
:
11168 OMP_CLAUSE_OPERAND (nc
, 0)
11169 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
11170 in_decl
, /*integral_constant_expression_p=*/false);
11172 case OMP_CLAUSE_NOWAIT
:
11173 case OMP_CLAUSE_ORDERED
:
11174 case OMP_CLAUSE_DEFAULT
:
11175 case OMP_CLAUSE_UNTIED
:
11178 gcc_unreachable ();
11182 return finish_omp_clauses (nreverse (new_clauses
));
11185 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
11188 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
11191 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
11193 tree purpose
, value
, chain
;
11198 if (TREE_CODE (t
) != TREE_LIST
)
11199 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
11200 /*function_p=*/false,
11201 /*integral_constant_expression_p=*/false);
11203 if (t
== void_list_node
)
11206 purpose
= TREE_PURPOSE (t
);
11208 purpose
= RECUR (purpose
);
11209 value
= TREE_VALUE (t
);
11210 if (value
&& TREE_CODE (value
) != LABEL_DECL
)
11211 value
= RECUR (value
);
11212 chain
= TREE_CHAIN (t
);
11213 if (chain
&& chain
!= void_type_node
)
11214 chain
= RECUR (chain
);
11215 return tree_cons (purpose
, value
, chain
);
11219 /* Substitute one OMP_FOR iterator. */
11222 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
11223 tree condv
, tree incrv
, tree
*clauses
,
11224 tree args
, tsubst_flags_t complain
, tree in_decl
,
11225 bool integral_constant_expression_p
)
11227 #define RECUR(NODE) \
11228 tsubst_expr ((NODE), args, complain, in_decl, \
11229 integral_constant_expression_p)
11230 tree decl
, init
, cond
, incr
, auto_node
;
11232 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
11233 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
11234 decl
= RECUR (TREE_OPERAND (init
, 0));
11235 init
= TREE_OPERAND (init
, 1);
11236 auto_node
= type_uses_auto (TREE_TYPE (decl
));
11237 if (auto_node
&& init
)
11239 tree init_expr
= init
;
11240 if (TREE_CODE (init_expr
) == DECL_EXPR
)
11241 init_expr
= DECL_INITIAL (DECL_EXPR_DECL (init_expr
));
11242 init_expr
= RECUR (init_expr
);
11244 = do_auto_deduction (TREE_TYPE (decl
), init_expr
, auto_node
);
11246 gcc_assert (!type_dependent_expression_p (decl
));
11248 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
11250 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
11251 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
11252 if (TREE_CODE (incr
) == MODIFY_EXPR
)
11253 incr
= build_x_modify_expr (RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
11254 RECUR (TREE_OPERAND (incr
, 1)),
11257 incr
= RECUR (incr
);
11258 TREE_VEC_ELT (declv
, i
) = decl
;
11259 TREE_VEC_ELT (initv
, i
) = init
;
11260 TREE_VEC_ELT (condv
, i
) = cond
;
11261 TREE_VEC_ELT (incrv
, i
) = incr
;
11265 if (init
&& TREE_CODE (init
) != DECL_EXPR
)
11268 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
11270 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
11271 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
11272 && OMP_CLAUSE_DECL (c
) == decl
)
11274 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
11275 && OMP_CLAUSE_DECL (c
) == decl
)
11276 error ("iteration variable %qD should not be firstprivate", decl
);
11277 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
11278 && OMP_CLAUSE_DECL (c
) == decl
)
11279 error ("iteration variable %qD should not be reduction", decl
);
11283 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
11284 OMP_CLAUSE_DECL (c
) = decl
;
11285 c
= finish_omp_clauses (c
);
11288 OMP_CLAUSE_CHAIN (c
) = *clauses
;
11293 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
11294 if (COMPARISON_CLASS_P (cond
))
11295 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
11296 RECUR (TREE_OPERAND (cond
, 0)),
11297 RECUR (TREE_OPERAND (cond
, 1)));
11299 cond
= RECUR (cond
);
11300 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
11301 switch (TREE_CODE (incr
))
11303 case PREINCREMENT_EXPR
:
11304 case PREDECREMENT_EXPR
:
11305 case POSTINCREMENT_EXPR
:
11306 case POSTDECREMENT_EXPR
:
11307 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
11308 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
11311 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
11312 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
11314 tree rhs
= TREE_OPERAND (incr
, 1);
11315 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
11316 RECUR (TREE_OPERAND (incr
, 0)),
11317 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
11318 RECUR (TREE_OPERAND (rhs
, 0)),
11319 RECUR (TREE_OPERAND (rhs
, 1))));
11322 incr
= RECUR (incr
);
11325 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
11326 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
11328 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
11329 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
11330 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
11331 TREE_TYPE (decl
), lhs
,
11332 RECUR (TREE_OPERAND (incr
, 2))));
11334 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
11335 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
11336 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
11338 tree rhs
= TREE_OPERAND (incr
, 2);
11339 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
11340 RECUR (TREE_OPERAND (incr
, 0)),
11341 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
11342 RECUR (TREE_OPERAND (rhs
, 0)),
11343 RECUR (TREE_OPERAND (rhs
, 1))));
11346 incr
= RECUR (incr
);
11349 incr
= RECUR (incr
);
11353 TREE_VEC_ELT (declv
, i
) = decl
;
11354 TREE_VEC_ELT (initv
, i
) = init
;
11355 TREE_VEC_ELT (condv
, i
) = cond
;
11356 TREE_VEC_ELT (incrv
, i
) = incr
;
11360 /* Like tsubst_copy for expressions, etc. but also does semantic
11364 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
11365 bool integral_constant_expression_p
)
11367 #define RECUR(NODE) \
11368 tsubst_expr ((NODE), args, complain, in_decl, \
11369 integral_constant_expression_p)
11373 if (t
== NULL_TREE
|| t
== error_mark_node
)
11376 if (EXPR_HAS_LOCATION (t
))
11377 input_location
= EXPR_LOCATION (t
);
11378 if (STATEMENT_CODE_P (TREE_CODE (t
)))
11379 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
11381 switch (TREE_CODE (t
))
11383 case STATEMENT_LIST
:
11385 tree_stmt_iterator i
;
11386 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
11387 RECUR (tsi_stmt (i
));
11391 case CTOR_INITIALIZER
:
11392 finish_mem_initializers (tsubst_initializer_list
11393 (TREE_OPERAND (t
, 0), args
));
11397 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
11401 tmp
= RECUR (EXPR_STMT_EXPR (t
));
11402 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
11403 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
11405 finish_expr_stmt (tmp
);
11409 do_using_directive (RECUR (USING_STMT_NAMESPACE (t
)));
11417 decl
= DECL_EXPR_DECL (t
);
11418 if (TREE_CODE (decl
) == LABEL_DECL
)
11419 finish_label_decl (DECL_NAME (decl
));
11420 else if (TREE_CODE (decl
) == USING_DECL
)
11422 tree scope
= USING_DECL_SCOPE (decl
);
11423 tree name
= DECL_NAME (decl
);
11426 scope
= RECUR (scope
);
11427 decl
= lookup_qualified_name (scope
, name
,
11428 /*is_type_p=*/false,
11429 /*complain=*/false);
11430 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
11431 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
11433 do_local_using_decl (decl
, scope
, name
);
11437 init
= DECL_INITIAL (decl
);
11438 decl
= tsubst (decl
, args
, complain
, in_decl
);
11439 if (decl
!= error_mark_node
)
11441 /* By marking the declaration as instantiated, we avoid
11442 trying to instantiate it. Since instantiate_decl can't
11443 handle local variables, and since we've already done
11444 all that needs to be done, that's the right thing to
11446 if (TREE_CODE (decl
) == VAR_DECL
)
11447 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
11448 if (TREE_CODE (decl
) == VAR_DECL
11449 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
11450 /* Anonymous aggregates are a special case. */
11451 finish_anon_union (decl
);
11454 maybe_push_decl (decl
);
11455 if (TREE_CODE (decl
) == VAR_DECL
11456 && DECL_PRETTY_FUNCTION_P (decl
))
11458 /* For __PRETTY_FUNCTION__ we have to adjust the
11460 const char *const name
11461 = cxx_printable_name (current_function_decl
, 2);
11462 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
11466 tree t
= RECUR (init
);
11469 /* If we had an initializer but it
11470 instantiated to nothing,
11471 value-initialize the object. This will
11472 only occur when the initializer was a
11473 pack expansion where the parameter packs
11474 used in that expansion were of length
11476 init
= build_value_init (TREE_TYPE (decl
));
11481 cp_finish_decl (decl
, init
, false, NULL_TREE
, 0);
11486 /* A DECL_EXPR can also be used as an expression, in the condition
11487 clause of an if/for/while construct. */
11492 stmt
= begin_for_stmt ();
11493 RECUR (FOR_INIT_STMT (t
));
11494 finish_for_init_stmt (stmt
);
11495 tmp
= RECUR (FOR_COND (t
));
11496 finish_for_cond (tmp
, stmt
);
11497 tmp
= RECUR (FOR_EXPR (t
));
11498 finish_for_expr (tmp
, stmt
);
11499 RECUR (FOR_BODY (t
));
11500 finish_for_stmt (stmt
);
11504 stmt
= begin_while_stmt ();
11505 tmp
= RECUR (WHILE_COND (t
));
11506 finish_while_stmt_cond (tmp
, stmt
);
11507 RECUR (WHILE_BODY (t
));
11508 finish_while_stmt (stmt
);
11512 stmt
= begin_do_stmt ();
11513 RECUR (DO_BODY (t
));
11514 finish_do_body (stmt
);
11515 tmp
= RECUR (DO_COND (t
));
11516 finish_do_stmt (tmp
, stmt
);
11520 stmt
= begin_if_stmt ();
11521 tmp
= RECUR (IF_COND (t
));
11522 finish_if_stmt_cond (tmp
, stmt
);
11523 RECUR (THEN_CLAUSE (t
));
11524 finish_then_clause (stmt
);
11526 if (ELSE_CLAUSE (t
))
11528 begin_else_clause (stmt
);
11529 RECUR (ELSE_CLAUSE (t
));
11530 finish_else_clause (stmt
);
11533 finish_if_stmt (stmt
);
11537 if (BIND_EXPR_BODY_BLOCK (t
))
11538 stmt
= begin_function_body ();
11540 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
11541 ? BCS_TRY_BLOCK
: 0);
11543 RECUR (BIND_EXPR_BODY (t
));
11545 if (BIND_EXPR_BODY_BLOCK (t
))
11546 finish_function_body (stmt
);
11548 finish_compound_stmt (stmt
);
11552 finish_break_stmt ();
11555 case CONTINUE_STMT
:
11556 finish_continue_stmt ();
11560 stmt
= begin_switch_stmt ();
11561 tmp
= RECUR (SWITCH_STMT_COND (t
));
11562 finish_switch_cond (tmp
, stmt
);
11563 RECUR (SWITCH_STMT_BODY (t
));
11564 finish_switch_stmt (stmt
);
11567 case CASE_LABEL_EXPR
:
11568 finish_case_label (EXPR_LOCATION (t
),
11569 RECUR (CASE_LOW (t
)),
11570 RECUR (CASE_HIGH (t
)));
11575 tree decl
= LABEL_EXPR_LABEL (t
);
11578 label
= finish_label_stmt (DECL_NAME (decl
));
11579 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
11580 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
11585 tmp
= GOTO_DESTINATION (t
);
11586 if (TREE_CODE (tmp
) != LABEL_DECL
)
11587 /* Computed goto's must be tsubst'd into. On the other hand,
11588 non-computed gotos must not be; the identifier in question
11589 will have no binding. */
11592 tmp
= DECL_NAME (tmp
);
11593 finish_goto_stmt (tmp
);
11597 tmp
= finish_asm_stmt
11598 (ASM_VOLATILE_P (t
),
11599 RECUR (ASM_STRING (t
)),
11600 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
11601 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
11602 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
11603 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
11605 tree asm_expr
= tmp
;
11606 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
11607 asm_expr
= TREE_OPERAND (asm_expr
, 0);
11608 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
11615 stmt
= begin_try_block ();
11616 RECUR (TRY_STMTS (t
));
11617 finish_cleanup_try_block (stmt
);
11618 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
11622 tree compound_stmt
= NULL_TREE
;
11624 if (FN_TRY_BLOCK_P (t
))
11625 stmt
= begin_function_try_block (&compound_stmt
);
11627 stmt
= begin_try_block ();
11629 RECUR (TRY_STMTS (t
));
11631 if (FN_TRY_BLOCK_P (t
))
11632 finish_function_try_block (stmt
);
11634 finish_try_block (stmt
);
11636 RECUR (TRY_HANDLERS (t
));
11637 if (FN_TRY_BLOCK_P (t
))
11638 finish_function_handler_sequence (stmt
, compound_stmt
);
11640 finish_handler_sequence (stmt
);
11646 tree decl
= HANDLER_PARMS (t
);
11650 decl
= tsubst (decl
, args
, complain
, in_decl
);
11651 /* Prevent instantiate_decl from trying to instantiate
11652 this variable. We've already done all that needs to be
11654 if (decl
!= error_mark_node
)
11655 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
11657 stmt
= begin_handler ();
11658 finish_handler_parms (decl
, stmt
);
11659 RECUR (HANDLER_BODY (t
));
11660 finish_handler (stmt
);
11665 tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
11668 case STATIC_ASSERT
:
11671 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
11674 /*integral_constant_expression_p=*/true);
11675 finish_static_assert (condition
,
11676 STATIC_ASSERT_MESSAGE (t
),
11677 STATIC_ASSERT_SOURCE_LOCATION (t
),
11678 /*member_p=*/false);
11683 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
),
11684 args
, complain
, in_decl
);
11685 stmt
= begin_omp_parallel ();
11686 RECUR (OMP_PARALLEL_BODY (t
));
11687 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
11688 = OMP_PARALLEL_COMBINED (t
);
11692 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
),
11693 args
, complain
, in_decl
);
11694 stmt
= begin_omp_task ();
11695 RECUR (OMP_TASK_BODY (t
));
11696 finish_omp_task (tmp
, stmt
);
11701 tree clauses
, body
, pre_body
;
11702 tree declv
, initv
, condv
, incrv
;
11705 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
),
11706 args
, complain
, in_decl
);
11707 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
11708 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
11709 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
11710 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
11712 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
11713 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
11714 &clauses
, args
, complain
, in_decl
,
11715 integral_constant_expression_p
);
11717 stmt
= begin_omp_structured_block ();
11719 for (i
= 0; i
< TREE_VEC_LENGTH (initv
); i
++)
11720 if (TREE_VEC_ELT (initv
, i
) == NULL
11721 || TREE_CODE (TREE_VEC_ELT (initv
, i
)) != DECL_EXPR
)
11722 TREE_VEC_ELT (initv
, i
) = RECUR (TREE_VEC_ELT (initv
, i
));
11723 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv
, i
))))
11725 tree init
= RECUR (TREE_VEC_ELT (initv
, i
));
11726 gcc_assert (init
== TREE_VEC_ELT (declv
, i
));
11727 TREE_VEC_ELT (initv
, i
) = NULL_TREE
;
11731 tree decl_expr
= TREE_VEC_ELT (initv
, i
);
11732 tree init
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
11733 gcc_assert (init
!= NULL
);
11734 TREE_VEC_ELT (initv
, i
) = RECUR (init
);
11735 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL
;
11737 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init
;
11740 pre_body
= push_stmt_list ();
11741 RECUR (OMP_FOR_PRE_BODY (t
));
11742 pre_body
= pop_stmt_list (pre_body
);
11744 body
= push_stmt_list ();
11745 RECUR (OMP_FOR_BODY (t
));
11746 body
= pop_stmt_list (body
);
11748 t
= finish_omp_for (EXPR_LOCATION (t
), declv
, initv
, condv
, incrv
,
11749 body
, pre_body
, clauses
);
11751 add_stmt (finish_omp_structured_block (stmt
));
11757 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), args
, complain
, in_decl
);
11758 stmt
= push_stmt_list ();
11759 RECUR (OMP_BODY (t
));
11760 stmt
= pop_stmt_list (stmt
);
11763 OMP_BODY (t
) = stmt
;
11764 OMP_CLAUSES (t
) = tmp
;
11772 stmt
= push_stmt_list ();
11773 RECUR (OMP_BODY (t
));
11774 stmt
= pop_stmt_list (stmt
);
11777 OMP_BODY (t
) = stmt
;
11782 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
11784 tree op1
= TREE_OPERAND (t
, 1);
11785 tree lhs
= RECUR (TREE_OPERAND (op1
, 0));
11786 tree rhs
= RECUR (TREE_OPERAND (op1
, 1));
11787 finish_omp_atomic (TREE_CODE (op1
), lhs
, rhs
);
11791 case EXPR_PACK_EXPANSION
:
11792 error ("invalid use of pack expansion expression");
11793 return error_mark_node
;
11795 case NONTYPE_ARGUMENT_PACK
:
11796 error ("use %<...%> to expand argument pack");
11797 return error_mark_node
;
11800 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
11802 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
11803 /*function_p=*/false,
11804 integral_constant_expression_p
);
11811 /* T is a postfix-expression that is not being used in a function
11812 call. Return the substituted version of T. */
11815 tsubst_non_call_postfix_expression (tree t
, tree args
,
11816 tsubst_flags_t complain
,
11819 if (TREE_CODE (t
) == SCOPE_REF
)
11820 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
11821 /*done=*/false, /*address_p=*/false);
11823 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
11824 /*function_p=*/false,
11825 /*integral_constant_expression_p=*/false);
11830 /* Like tsubst but deals with expressions and performs semantic
11831 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
11834 tsubst_copy_and_build (tree t
,
11836 tsubst_flags_t complain
,
11839 bool integral_constant_expression_p
)
11841 #define RECUR(NODE) \
11842 tsubst_copy_and_build (NODE, args, complain, in_decl, \
11843 /*function_p=*/false, \
11844 integral_constant_expression_p)
11848 if (t
== NULL_TREE
|| t
== error_mark_node
)
11851 switch (TREE_CODE (t
))
11855 /* Fall through. */
11856 case IDENTIFIER_NODE
:
11860 bool non_integral_constant_expression_p
;
11861 const char *error_msg
;
11863 if (IDENTIFIER_TYPENAME_P (t
))
11865 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11866 t
= mangle_conv_op_name_for_type (new_type
);
11869 /* Look up the name. */
11870 decl
= lookup_name (t
);
11872 /* By convention, expressions use ERROR_MARK_NODE to indicate
11873 failure, not NULL_TREE. */
11874 if (decl
== NULL_TREE
)
11875 decl
= error_mark_node
;
11877 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
11879 integral_constant_expression_p
,
11880 /*allow_non_integral_constant_expression_p=*/false,
11881 &non_integral_constant_expression_p
,
11882 /*template_p=*/false,
11884 /*address_p=*/false,
11885 /*template_arg_p=*/false,
11890 if (!function_p
&& TREE_CODE (decl
) == IDENTIFIER_NODE
)
11891 decl
= unqualified_name_lookup_error (decl
);
11895 case TEMPLATE_ID_EXPR
:
11898 tree templ
= RECUR (TREE_OPERAND (t
, 0));
11899 tree targs
= TREE_OPERAND (t
, 1);
11902 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
11904 if (TREE_CODE (templ
) == COMPONENT_REF
)
11906 object
= TREE_OPERAND (templ
, 0);
11907 templ
= TREE_OPERAND (templ
, 1);
11910 object
= NULL_TREE
;
11911 templ
= lookup_template_function (templ
, targs
);
11914 return build3 (COMPONENT_REF
, TREE_TYPE (templ
),
11915 object
, templ
, NULL_TREE
);
11917 return baselink_for_fns (templ
);
11922 tree r
= RECUR (TREE_OPERAND (t
, 0));
11924 if (REFERENCE_REF_P (t
))
11926 /* A type conversion to reference type will be enclosed in
11927 such an indirect ref, but the substitution of the cast
11928 will have also added such an indirect ref. */
11929 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
11930 r
= convert_from_reference (r
);
11933 r
= build_x_indirect_ref (r
, RO_UNARY_STAR
, complain
);
11939 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
11940 RECUR (TREE_OPERAND (t
, 0)));
11943 case REINTERPRET_CAST_EXPR
:
11944 case CONST_CAST_EXPR
:
11945 case DYNAMIC_CAST_EXPR
:
11946 case STATIC_CAST_EXPR
:
11951 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11952 if (integral_constant_expression_p
11953 && !cast_valid_in_integral_constant_expression_p (type
))
11955 if (complain
& tf_error
)
11956 error ("a cast to a type other than an integral or "
11957 "enumeration type cannot appear in a constant-expression");
11958 return error_mark_node
;
11961 op
= RECUR (TREE_OPERAND (t
, 0));
11963 switch (TREE_CODE (t
))
11966 return build_functional_cast (type
, op
, complain
);
11967 case REINTERPRET_CAST_EXPR
:
11968 return build_reinterpret_cast (type
, op
, complain
);
11969 case CONST_CAST_EXPR
:
11970 return build_const_cast (type
, op
, complain
);
11971 case DYNAMIC_CAST_EXPR
:
11972 return build_dynamic_cast (type
, op
, complain
);
11973 case STATIC_CAST_EXPR
:
11974 return build_static_cast (type
, op
, complain
);
11976 gcc_unreachable ();
11980 case POSTDECREMENT_EXPR
:
11981 case POSTINCREMENT_EXPR
:
11982 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
11983 args
, complain
, in_decl
);
11984 return build_x_unary_op (TREE_CODE (t
), op1
, complain
);
11986 case PREDECREMENT_EXPR
:
11987 case PREINCREMENT_EXPR
:
11991 case TRUTH_NOT_EXPR
:
11992 case UNARY_PLUS_EXPR
: /* Unary + */
11993 case REALPART_EXPR
:
11994 case IMAGPART_EXPR
:
11995 return build_x_unary_op (TREE_CODE (t
), RECUR (TREE_OPERAND (t
, 0)),
11999 op1
= TREE_OPERAND (t
, 0);
12000 if (TREE_CODE (op1
) == SCOPE_REF
)
12001 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
12002 /*done=*/true, /*address_p=*/true);
12004 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
12006 if (TREE_CODE (op1
) == LABEL_DECL
)
12007 return finish_label_address_expr (DECL_NAME (op1
),
12008 EXPR_LOCATION (op1
));
12009 return build_x_unary_op (ADDR_EXPR
, op1
, complain
);
12014 case TRUNC_DIV_EXPR
:
12015 case CEIL_DIV_EXPR
:
12016 case FLOOR_DIV_EXPR
:
12017 case ROUND_DIV_EXPR
:
12018 case EXACT_DIV_EXPR
:
12022 case TRUNC_MOD_EXPR
:
12023 case FLOOR_MOD_EXPR
:
12024 case TRUTH_ANDIF_EXPR
:
12025 case TRUTH_ORIF_EXPR
:
12026 case TRUTH_AND_EXPR
:
12027 case TRUTH_OR_EXPR
:
12042 return build_x_binary_op
12044 RECUR (TREE_OPERAND (t
, 0)),
12045 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
12047 : TREE_CODE (TREE_OPERAND (t
, 0))),
12048 RECUR (TREE_OPERAND (t
, 1)),
12049 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
12051 : TREE_CODE (TREE_OPERAND (t
, 1))),
12052 /*overloaded_p=*/NULL
,
12056 return tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
12057 /*address_p=*/false);
12059 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
12060 args
, complain
, in_decl
);
12061 return build_x_array_ref (op1
, RECUR (TREE_OPERAND (t
, 1)), complain
);
12064 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
12065 return tsubst_copy (t
, args
, complain
, in_decl
);
12069 op1
= TREE_OPERAND (t
, 0);
12072 /* When there are no ARGS, we are trying to evaluate a
12073 non-dependent expression from the parser. Trying to do
12074 the substitutions may not work. */
12076 op1
= TREE_TYPE (op1
);
12080 ++cp_unevaluated_operand
;
12081 ++c_inhibit_evaluation_warnings
;
12082 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
12083 /*function_p=*/false,
12084 /*integral_constant_expression_p=*/false);
12085 --cp_unevaluated_operand
;
12086 --c_inhibit_evaluation_warnings
;
12089 return cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
12090 complain
& tf_error
);
12092 return cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
12093 complain
& tf_error
);
12097 tree r
= build_x_modify_expr
12098 (RECUR (TREE_OPERAND (t
, 0)),
12099 TREE_CODE (TREE_OPERAND (t
, 1)),
12100 RECUR (TREE_OPERAND (t
, 2)),
12102 /* TREE_NO_WARNING must be set if either the expression was
12103 parenthesized or it uses an operator such as >>= rather
12104 than plain assignment. In the former case, it was already
12105 set and must be copied. In the latter case,
12106 build_x_modify_expr sets it and it must not be reset
12108 if (TREE_NO_WARNING (t
))
12109 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12114 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
12115 args
, complain
, in_decl
);
12116 /* Remember that there was a reference to this entity. */
12119 return build_x_arrow (op1
);
12123 tree placement
= RECUR (TREE_OPERAND (t
, 0));
12124 tree init
= RECUR (TREE_OPERAND (t
, 3));
12125 VEC(tree
,gc
) *placement_vec
;
12126 VEC(tree
,gc
) *init_vec
;
12129 if (placement
== NULL_TREE
)
12130 placement_vec
= NULL
;
12133 placement_vec
= make_tree_vector ();
12134 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
12135 VEC_safe_push (tree
, gc
, placement_vec
, TREE_VALUE (placement
));
12138 /* If there was an initializer in the original tree, but it
12139 instantiated to an empty list, then we should pass a
12140 non-NULL empty vector to tell build_new that it was an
12141 empty initializer() rather than no initializer. This can
12142 only happen when the initializer is a pack expansion whose
12143 parameter packs are of length zero. */
12144 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
12148 init_vec
= make_tree_vector ();
12149 if (init
== void_zero_node
)
12150 gcc_assert (init_vec
!= NULL
);
12153 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
12154 VEC_safe_push (tree
, gc
, init_vec
, TREE_VALUE (init
));
12158 ret
= build_new (&placement_vec
,
12159 RECUR (TREE_OPERAND (t
, 1)),
12160 RECUR (TREE_OPERAND (t
, 2)),
12162 NEW_EXPR_USE_GLOBAL (t
),
12165 if (placement_vec
!= NULL
)
12166 release_tree_vector (placement_vec
);
12167 if (init_vec
!= NULL
)
12168 release_tree_vector (init_vec
);
12174 return delete_sanity
12175 (RECUR (TREE_OPERAND (t
, 0)),
12176 RECUR (TREE_OPERAND (t
, 1)),
12177 DELETE_EXPR_USE_VEC (t
),
12178 DELETE_EXPR_USE_GLOBAL (t
));
12180 case COMPOUND_EXPR
:
12181 return build_x_compound_expr (RECUR (TREE_OPERAND (t
, 0)),
12182 RECUR (TREE_OPERAND (t
, 1)),
12188 VEC(tree
,gc
) *call_args
;
12189 unsigned int nargs
, i
;
12194 function
= CALL_EXPR_FN (t
);
12195 /* When we parsed the expression, we determined whether or
12196 not Koenig lookup should be performed. */
12197 koenig_p
= KOENIG_LOOKUP_P (t
);
12198 if (TREE_CODE (function
) == SCOPE_REF
)
12200 qualified_p
= true;
12201 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
12203 /*address_p=*/false);
12207 if (TREE_CODE (function
) == COMPONENT_REF
)
12209 tree op
= TREE_OPERAND (function
, 1);
12211 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
12212 || (BASELINK_P (op
)
12213 && BASELINK_QUALIFIED_P (op
)));
12216 qualified_p
= false;
12218 function
= tsubst_copy_and_build (function
, args
, complain
,
12221 integral_constant_expression_p
);
12223 if (BASELINK_P (function
))
12224 qualified_p
= true;
12227 nargs
= call_expr_nargs (t
);
12228 call_args
= make_tree_vector ();
12229 for (i
= 0; i
< nargs
; ++i
)
12231 tree arg
= CALL_EXPR_ARG (t
, i
);
12233 if (!PACK_EXPANSION_P (arg
))
12234 VEC_safe_push (tree
, gc
, call_args
,
12235 RECUR (CALL_EXPR_ARG (t
, i
)));
12238 /* Expand the pack expansion and push each entry onto
12240 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
12241 if (TREE_CODE (arg
) == TREE_VEC
)
12243 unsigned int len
, j
;
12245 len
= TREE_VEC_LENGTH (arg
);
12246 for (j
= 0; j
< len
; ++j
)
12248 tree value
= TREE_VEC_ELT (arg
, j
);
12249 if (value
!= NULL_TREE
)
12250 value
= convert_from_reference (value
);
12251 VEC_safe_push (tree
, gc
, call_args
, value
);
12256 /* A partial substitution. Add one entry. */
12257 VEC_safe_push (tree
, gc
, call_args
, arg
);
12262 /* We do not perform argument-dependent lookup if normal
12263 lookup finds a non-function, in accordance with the
12264 expected resolution of DR 218. */
12266 && ((is_overloaded_fn (function
)
12267 /* If lookup found a member function, the Koenig lookup is
12268 not appropriate, even if an unqualified-name was used
12269 to denote the function. */
12270 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
12271 || TREE_CODE (function
) == IDENTIFIER_NODE
)
12272 /* Only do this when substitution turns a dependent call
12273 into a non-dependent call. */
12274 && type_dependent_expression_p_push (t
)
12275 && !any_type_dependent_arguments_p (call_args
))
12276 function
= perform_koenig_lookup (function
, call_args
);
12278 if (TREE_CODE (function
) == IDENTIFIER_NODE
)
12280 unqualified_name_lookup_error (function
);
12281 release_tree_vector (call_args
);
12282 return error_mark_node
;
12285 /* Remember that there was a reference to this entity. */
12286 if (DECL_P (function
))
12287 mark_used (function
);
12289 if (TREE_CODE (function
) == OFFSET_REF
)
12290 ret
= build_offset_ref_call_from_tree (function
, &call_args
);
12291 else if (TREE_CODE (function
) == COMPONENT_REF
)
12293 if (!BASELINK_P (TREE_OPERAND (function
, 1)))
12294 ret
= finish_call_expr (function
, &call_args
,
12295 /*disallow_virtual=*/false,
12296 /*koenig_p=*/false,
12299 ret
= (build_new_method_call
12300 (TREE_OPERAND (function
, 0),
12301 TREE_OPERAND (function
, 1),
12302 &call_args
, NULL_TREE
,
12303 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
12308 ret
= finish_call_expr (function
, &call_args
,
12309 /*disallow_virtual=*/qualified_p
,
12313 release_tree_vector (call_args
);
12319 return build_x_conditional_expr
12320 (RECUR (TREE_OPERAND (t
, 0)),
12321 RECUR (TREE_OPERAND (t
, 1)),
12322 RECUR (TREE_OPERAND (t
, 2)),
12325 case PSEUDO_DTOR_EXPR
:
12326 return finish_pseudo_destructor_expr
12327 (RECUR (TREE_OPERAND (t
, 0)),
12328 RECUR (TREE_OPERAND (t
, 1)),
12329 RECUR (TREE_OPERAND (t
, 2)));
12333 tree purpose
, value
, chain
;
12335 if (t
== void_list_node
)
12338 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
12339 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
12341 /* We have pack expansions, so expand those and
12342 create a new list out of it. */
12343 tree purposevec
= NULL_TREE
;
12344 tree valuevec
= NULL_TREE
;
12348 /* Expand the argument expressions. */
12349 if (TREE_PURPOSE (t
))
12350 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
12351 complain
, in_decl
);
12352 if (TREE_VALUE (t
))
12353 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
12354 complain
, in_decl
);
12356 /* Build the rest of the list. */
12357 chain
= TREE_CHAIN (t
);
12358 if (chain
&& chain
!= void_type_node
)
12359 chain
= RECUR (chain
);
12361 /* Determine the number of arguments. */
12362 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
12364 len
= TREE_VEC_LENGTH (purposevec
);
12365 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
12367 else if (TREE_CODE (valuevec
) == TREE_VEC
)
12368 len
= TREE_VEC_LENGTH (valuevec
);
12371 /* Since we only performed a partial substitution into
12372 the argument pack, we only return a single list
12374 if (purposevec
== TREE_PURPOSE (t
)
12375 && valuevec
== TREE_VALUE (t
)
12376 && chain
== TREE_CHAIN (t
))
12379 return tree_cons (purposevec
, valuevec
, chain
);
12382 /* Convert the argument vectors into a TREE_LIST */
12386 /* Grab the Ith values. */
12388 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
12391 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
12394 /* Build the list (backwards). */
12395 chain
= tree_cons (purpose
, value
, chain
);
12401 purpose
= TREE_PURPOSE (t
);
12403 purpose
= RECUR (purpose
);
12404 value
= TREE_VALUE (t
);
12406 value
= RECUR (value
);
12407 chain
= TREE_CHAIN (t
);
12408 if (chain
&& chain
!= void_type_node
)
12409 chain
= RECUR (chain
);
12410 if (purpose
== TREE_PURPOSE (t
)
12411 && value
== TREE_VALUE (t
)
12412 && chain
== TREE_CHAIN (t
))
12414 return tree_cons (purpose
, value
, chain
);
12417 case COMPONENT_REF
:
12423 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
12424 args
, complain
, in_decl
);
12425 /* Remember that there was a reference to this entity. */
12426 if (DECL_P (object
))
12427 mark_used (object
);
12428 object_type
= TREE_TYPE (object
);
12430 member
= TREE_OPERAND (t
, 1);
12431 if (BASELINK_P (member
))
12432 member
= tsubst_baselink (member
,
12433 non_reference (TREE_TYPE (object
)),
12434 args
, complain
, in_decl
);
12436 member
= tsubst_copy (member
, args
, complain
, in_decl
);
12437 if (member
== error_mark_node
)
12438 return error_mark_node
;
12440 if (object_type
&& !CLASS_TYPE_P (object_type
))
12442 if (SCALAR_TYPE_P (object_type
))
12444 tree s
= NULL_TREE
;
12445 tree dtor
= member
;
12447 if (TREE_CODE (dtor
) == SCOPE_REF
)
12449 s
= TREE_OPERAND (dtor
, 0);
12450 dtor
= TREE_OPERAND (dtor
, 1);
12452 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
12454 dtor
= TREE_OPERAND (dtor
, 0);
12456 return finish_pseudo_destructor_expr (object
, s
, dtor
);
12460 else if (TREE_CODE (member
) == SCOPE_REF
12461 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
12466 /* Lookup the template functions now that we know what the
12468 tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
12469 args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
12470 member
= lookup_qualified_name (TREE_OPERAND (member
, 0), tmpl
,
12471 /*is_type_p=*/false,
12472 /*complain=*/false);
12473 if (BASELINK_P (member
))
12475 BASELINK_FUNCTIONS (member
)
12476 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
12478 member
= (adjust_result_of_qualified_name_lookup
12479 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
12484 qualified_name_lookup_error (object_type
, tmpl
, member
,
12486 return error_mark_node
;
12489 else if (TREE_CODE (member
) == SCOPE_REF
12490 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
12491 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
12493 if (complain
& tf_error
)
12495 if (TYPE_P (TREE_OPERAND (member
, 0)))
12496 error ("%qT is not a class or namespace",
12497 TREE_OPERAND (member
, 0));
12499 error ("%qD is not a class or namespace",
12500 TREE_OPERAND (member
, 0));
12502 return error_mark_node
;
12504 else if (TREE_CODE (member
) == FIELD_DECL
)
12505 return finish_non_static_data_member (member
, object
, NULL_TREE
);
12507 return finish_class_member_access_expr (object
, member
,
12508 /*template_p=*/false,
12514 (RECUR (TREE_OPERAND (t
, 0)));
12518 VEC(constructor_elt
,gc
) *n
;
12519 constructor_elt
*ce
;
12520 unsigned HOST_WIDE_INT idx
;
12521 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12522 bool process_index_p
;
12524 bool need_copy_p
= false;
12527 if (type
== error_mark_node
)
12528 return error_mark_node
;
12530 /* digest_init will do the wrong thing if we let it. */
12531 if (type
&& TYPE_PTRMEMFUNC_P (type
))
12534 /* We do not want to process the index of aggregate
12535 initializers as they are identifier nodes which will be
12536 looked up by digest_init. */
12537 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
12539 n
= VEC_copy (constructor_elt
, gc
, CONSTRUCTOR_ELTS (t
));
12540 newlen
= VEC_length (constructor_elt
, n
);
12541 for (idx
= 0; VEC_iterate (constructor_elt
, n
, idx
, ce
); idx
++)
12543 if (ce
->index
&& process_index_p
)
12544 ce
->index
= RECUR (ce
->index
);
12546 if (PACK_EXPANSION_P (ce
->value
))
12548 /* Substitute into the pack expansion. */
12549 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
12552 if (ce
->value
== error_mark_node
)
12554 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
12555 /* Just move the argument into place. */
12556 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
12559 /* Update the length of the final CONSTRUCTOR
12560 arguments vector, and note that we will need to
12562 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
12563 need_copy_p
= true;
12567 ce
->value
= RECUR (ce
->value
);
12572 VEC(constructor_elt
,gc
) *old_n
= n
;
12574 n
= VEC_alloc (constructor_elt
, gc
, newlen
);
12575 for (idx
= 0; VEC_iterate (constructor_elt
, old_n
, idx
, ce
);
12578 if (TREE_CODE (ce
->value
) == TREE_VEC
)
12580 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
12581 for (i
= 0; i
< len
; ++i
)
12582 CONSTRUCTOR_APPEND_ELT (n
, 0,
12583 TREE_VEC_ELT (ce
->value
, i
));
12586 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
12590 r
= build_constructor (init_list_type_node
, n
);
12591 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
12593 if (TREE_HAS_CONSTRUCTOR (t
))
12594 return finish_compound_literal (type
, r
);
12601 tree operand_0
= RECUR (TREE_OPERAND (t
, 0));
12602 if (TYPE_P (operand_0
))
12603 return get_typeid (operand_0
);
12604 return build_typeid (operand_0
);
12614 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
12616 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
12617 /* If the original type was a reference, we'll be wrapped in
12618 the appropriate INDIRECT_REF. */
12619 r
= convert_from_reference (r
);
12624 return build_x_va_arg (RECUR (TREE_OPERAND (t
, 0)),
12625 tsubst_copy (TREE_TYPE (t
), args
, complain
,
12628 case OFFSETOF_EXPR
:
12629 return finish_offsetof (RECUR (TREE_OPERAND (t
, 0)));
12633 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
12634 complain
, in_decl
);
12636 tree type2
= TRAIT_EXPR_TYPE2 (t
);
12638 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
12640 return finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
);
12645 tree old_stmt_expr
= cur_stmt_expr
;
12646 tree stmt_expr
= begin_stmt_expr ();
12648 cur_stmt_expr
= stmt_expr
;
12649 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
12650 integral_constant_expression_p
);
12651 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
12652 cur_stmt_expr
= old_stmt_expr
;
12654 /* If the resulting list of expression statement is empty,
12655 fold it further into void_zero_node. */
12656 if (empty_expr_stmt_p (stmt_expr
))
12657 stmt_expr
= void_zero_node
;
12663 t
= tsubst_copy (t
, args
, complain
, in_decl
);
12664 /* As in finish_id_expression, we resolve enumeration constants
12665 to their underlying values. */
12666 if (TREE_CODE (t
) == CONST_DECL
)
12668 used_types_insert (TREE_TYPE (t
));
12669 return DECL_INITIAL (t
);
12675 tree r
= build_lambda_expr ();
12677 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
12678 TREE_TYPE (r
) = type
;
12679 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
12681 LAMBDA_EXPR_LOCATION (r
)
12682 = LAMBDA_EXPR_LOCATION (t
);
12683 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
12684 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
12685 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
12686 LAMBDA_EXPR_DISCRIMINATOR (r
)
12687 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
12688 LAMBDA_EXPR_CAPTURE_LIST (r
)
12689 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t
));
12690 LAMBDA_EXPR_THIS_CAPTURE (r
)
12691 = RECUR (LAMBDA_EXPR_THIS_CAPTURE (t
));
12692 LAMBDA_EXPR_EXTRA_SCOPE (r
)
12693 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t
));
12695 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
12696 determine_visibility (TYPE_NAME (type
));
12697 /* Now that we know visibility, instantiate the type so we have a
12698 declaration of the op() for later calls to lambda_function. */
12699 complete_type (type
);
12701 type
= tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
12703 apply_lambda_return_type (r
, type
);
12705 return build_lambda_object (r
);
12709 /* Handle Objective-C++ constructs, if appropriate. */
12712 = objcp_tsubst_copy_and_build (t
, args
, complain
,
12713 in_decl
, /*function_p=*/false);
12717 return tsubst_copy (t
, args
, complain
, in_decl
);
12723 /* Verify that the instantiated ARGS are valid. For type arguments,
12724 make sure that the type's linkage is ok. For non-type arguments,
12725 make sure they are constants if they are integral or enumerations.
12726 Emit an error under control of COMPLAIN, and return TRUE on error. */
12729 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
12731 if (ARGUMENT_PACK_P (t
))
12733 tree vec
= ARGUMENT_PACK_ARGS (t
);
12734 int len
= TREE_VEC_LENGTH (vec
);
12735 bool result
= false;
12738 for (i
= 0; i
< len
; ++i
)
12739 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
12743 else if (TYPE_P (t
))
12745 /* [basic.link]: A name with no linkage (notably, the name
12746 of a class or enumeration declared in a local scope)
12747 shall not be used to declare an entity with linkage.
12748 This implies that names with no linkage cannot be used as
12751 DR 757 relaxes this restriction for C++0x. */
12752 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
12753 : no_linkage_check (t
, /*relaxed_p=*/false));
12757 /* DR 488 makes use of a type with no linkage cause
12758 type deduction to fail. */
12759 if (complain
& tf_error
)
12761 if (TYPE_ANONYMOUS_P (nt
))
12762 error ("%qT is/uses anonymous type", t
);
12764 error ("template argument for %qD uses local type %qT",
12769 /* In order to avoid all sorts of complications, we do not
12770 allow variably-modified types as template arguments. */
12771 else if (variably_modified_type_p (t
, NULL_TREE
))
12773 if (complain
& tf_error
)
12774 error ("%qT is a variably modified type", t
);
12778 /* A non-type argument of integral or enumerated type must be a
12780 else if (TREE_TYPE (t
)
12781 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
12782 && !TREE_CONSTANT (t
))
12784 if (complain
& tf_error
)
12785 error ("integral expression %qE is not constant", t
);
12792 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
12794 int ix
, len
= DECL_NTPARMS (tmpl
);
12795 bool result
= false;
12797 for (ix
= 0; ix
!= len
; ix
++)
12799 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
12802 if (result
&& (complain
& tf_error
))
12803 error (" trying to instantiate %qD", tmpl
);
12807 /* Instantiate the indicated variable or function template TMPL with
12808 the template arguments in TARG_PTR. */
12811 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
12813 tree targ_ptr
= orig_args
;
12817 HOST_WIDE_INT saved_processing_template_decl
;
12819 if (tmpl
== error_mark_node
)
12820 return error_mark_node
;
12822 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
12824 /* If this function is a clone, handle it specially. */
12825 if (DECL_CLONED_FUNCTION_P (tmpl
))
12830 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
12831 DECL_CLONED_FUNCTION. */
12832 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
12833 targ_ptr
, complain
);
12834 if (spec
== error_mark_node
)
12835 return error_mark_node
;
12837 /* Look for the clone. */
12838 FOR_EACH_CLONE (clone
, spec
)
12839 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
12841 /* We should always have found the clone by now. */
12842 gcc_unreachable ();
12846 /* Check to see if we already have this specialization. */
12847 gen_tmpl
= most_general_template (tmpl
);
12848 if (tmpl
!= gen_tmpl
)
12849 /* The TMPL is a partial instantiation. To get a full set of
12850 arguments we must add the arguments used to perform the
12851 partial instantiation. */
12852 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
12855 /* It would be nice to avoid hashing here and then again in tsubst_decl,
12856 but it doesn't seem to be on the hot path. */
12857 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
12859 gcc_assert (tmpl
== gen_tmpl
12860 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
12862 || fndecl
== NULL_TREE
);
12864 if (spec
!= NULL_TREE
)
12867 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
12869 return error_mark_node
;
12871 /* We are building a FUNCTION_DECL, during which the access of its
12872 parameters and return types have to be checked. However this
12873 FUNCTION_DECL which is the desired context for access checking
12874 is not built yet. We solve this chicken-and-egg problem by
12875 deferring all checks until we have the FUNCTION_DECL. */
12876 push_deferring_access_checks (dk_deferred
);
12878 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
12879 (because, for example, we have encountered a non-dependent
12880 function call in the body of a template function and must now
12881 determine which of several overloaded functions will be called),
12882 within the instantiation itself we are not processing a
12884 saved_processing_template_decl
= processing_template_decl
;
12885 processing_template_decl
= 0;
12886 /* Substitute template parameters to obtain the specialization. */
12887 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
12888 targ_ptr
, complain
, gen_tmpl
);
12889 processing_template_decl
= saved_processing_template_decl
;
12890 if (fndecl
== error_mark_node
)
12891 return error_mark_node
;
12893 /* Now we know the specialization, compute access previously
12895 push_access_scope (fndecl
);
12897 /* Some typedefs referenced from within the template code need to be access
12898 checked at template instantiation time, i.e now. These types were
12899 added to the template at parsing time. Let's get those and perfom
12900 the acces checks then. */
12901 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl
), targ_ptr
);
12902 perform_deferred_access_checks ();
12903 pop_access_scope (fndecl
);
12904 pop_deferring_access_checks ();
12906 /* The DECL_TI_TEMPLATE should always be the immediate parent
12907 template, not the most general template. */
12908 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
12910 /* If we've just instantiated the main entry point for a function,
12911 instantiate all the alternate entry points as well. We do this
12912 by cloning the instantiation of the main entry point, not by
12913 instantiating the template clones. */
12914 if (TREE_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl
)))
12915 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
12920 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
12921 NARGS elements of the arguments that are being used when calling
12922 it. TARGS is a vector into which the deduced template arguments
12925 Return zero for success, 2 for an incomplete match that doesn't resolve
12926 all the types, and 1 for complete failure. An error message will be
12927 printed only for an incomplete match.
12929 If FN is a conversion operator, or we are trying to produce a specific
12930 specialization, RETURN_TYPE is the return type desired.
12932 The EXPLICIT_TARGS are explicit template arguments provided via a
12935 The parameter STRICT is one of:
12938 We are deducing arguments for a function call, as in
12939 [temp.deduct.call].
12942 We are deducing arguments for a conversion function, as in
12943 [temp.deduct.conv].
12946 We are deducing arguments when doing an explicit instantiation
12947 as in [temp.explicit], when determining an explicit specialization
12948 as in [temp.expl.spec], or when taking the address of a function
12949 template, as in [temp.deduct.funcaddr]. */
12952 fn_type_unification (tree fn
,
12953 tree explicit_targs
,
12956 unsigned int nargs
,
12958 unification_kind_t strict
,
12964 bool incomplete_argument_packs_p
= false;
12966 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
12968 fntype
= TREE_TYPE (fn
);
12969 if (explicit_targs
)
12973 The specified template arguments must match the template
12974 parameters in kind (i.e., type, nontype, template), and there
12975 must not be more arguments than there are parameters;
12976 otherwise type deduction fails.
12978 Nontype arguments must match the types of the corresponding
12979 nontype template parameters, or must be convertible to the
12980 types of the corresponding nontype parameters as specified in
12981 _temp.arg.nontype_, otherwise type deduction fails.
12983 All references in the function type of the function template
12984 to the corresponding template parameters are replaced by the
12985 specified template argument values. If a substitution in a
12986 template parameter or in the function type of the function
12987 template results in an invalid type, type deduction fails. */
12988 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
12989 int i
, len
= TREE_VEC_LENGTH (tparms
);
12990 tree converted_args
;
12991 bool incomplete
= false;
12993 if (explicit_targs
== error_mark_node
)
12997 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
, tf_none
,
12998 /*require_all_args=*/false,
12999 /*use_default_args=*/false));
13000 if (converted_args
== error_mark_node
)
13003 /* Substitute the explicit args into the function type. This is
13004 necessary so that, for instance, explicitly declared function
13005 arguments can match null pointed constants. If we were given
13006 an incomplete set of explicit args, we must not do semantic
13007 processing during substitution as we could create partial
13009 for (i
= 0; i
< len
; i
++)
13011 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
13012 bool parameter_pack
= false;
13014 /* Dig out the actual parm. */
13015 if (TREE_CODE (parm
) == TYPE_DECL
13016 || TREE_CODE (parm
) == TEMPLATE_DECL
)
13018 parm
= TREE_TYPE (parm
);
13019 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
13021 else if (TREE_CODE (parm
) == PARM_DECL
)
13023 parm
= DECL_INITIAL (parm
);
13024 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
13027 if (parameter_pack
)
13031 template_parm_level_and_index (parm
, &level
, &idx
);
13033 /* Mark the argument pack as "incomplete". We could
13034 still deduce more arguments during unification. */
13035 targ
= TMPL_ARG (converted_args
, level
, idx
);
13038 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
13039 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
13040 = ARGUMENT_PACK_ARGS (targ
);
13043 /* We have some incomplete argument packs. */
13044 incomplete_argument_packs_p
= true;
13048 if (incomplete_argument_packs_p
)
13049 /* Any substitution is guaranteed to be incomplete if there
13050 are incomplete argument packs, because we can still deduce
13054 incomplete
= NUM_TMPL_ARGS (explicit_targs
) != NUM_TMPL_ARGS (targs
);
13056 processing_template_decl
+= incomplete
;
13057 fntype
= tsubst (fntype
, converted_args
, tf_none
, NULL_TREE
);
13058 processing_template_decl
-= incomplete
;
13060 if (fntype
== error_mark_node
)
13063 /* Place the explicitly specified arguments in TARGS. */
13064 for (i
= NUM_TMPL_ARGS (converted_args
); i
--;)
13065 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (converted_args
, i
);
13068 /* Never do unification on the 'this' parameter. */
13069 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
13075 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
13076 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
13077 new_args
[0] = return_type
;
13078 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
13083 /* We allow incomplete unification without an error message here
13084 because the standard doesn't seem to explicitly prohibit it. Our
13085 callers must be ready to deal with unification failures in any
13087 result
= type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
13088 targs
, parms
, args
, nargs
, /*subr=*/0,
13091 if (result
== 0 && incomplete_argument_packs_p
)
13093 int i
, len
= NUM_TMPL_ARGS (targs
);
13095 /* Clear the "incomplete" flags on all argument packs. */
13096 for (i
= 0; i
< len
; i
++)
13098 tree arg
= TREE_VEC_ELT (targs
, i
);
13099 if (ARGUMENT_PACK_P (arg
))
13101 ARGUMENT_PACK_INCOMPLETE_P (arg
) = 0;
13102 ARGUMENT_PACK_EXPLICIT_ARGS (arg
) = NULL_TREE
;
13107 /* Now that we have bindings for all of the template arguments,
13108 ensure that the arguments deduced for the template template
13109 parameters have compatible template parameter lists. We cannot
13110 check this property before we have deduced all template
13111 arguments, because the template parameter types of a template
13112 template parameter might depend on prior template parameters
13113 deduced after the template template parameter. The following
13114 ill-formed example illustrates this issue:
13116 template<typename T, template<T> class C> void f(C<5>, T);
13118 template<int N> struct X {};
13121 f(X<5>(), 5l); // error: template argument deduction fails
13124 The template parameter list of 'C' depends on the template type
13125 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
13126 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
13127 time that we deduce 'C'. */
13129 && !template_template_parm_bindings_ok_p
13130 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
13134 /* All is well so far. Now, check:
13138 When all template arguments have been deduced, all uses of
13139 template parameters in nondeduced contexts are replaced with
13140 the corresponding deduced argument values. If the
13141 substitution results in an invalid type, as described above,
13142 type deduction fails. */
13144 tree substed
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
13145 if (substed
== error_mark_node
)
13148 /* If we're looking for an exact match, check that what we got
13149 is indeed an exact match. It might not be if some template
13150 parameters are used in non-deduced contexts. */
13151 if (strict
== DEDUCE_EXACT
)
13156 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (substed
));
13158 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
13159 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
13160 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
13168 /* Adjust types before performing type deduction, as described in
13169 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
13170 sections are symmetric. PARM is the type of a function parameter
13171 or the return type of the conversion function. ARG is the type of
13172 the argument passed to the call, or the type of the value
13173 initialized with the result of the conversion function.
13174 ARG_EXPR is the original argument expression, which may be null. */
13177 maybe_adjust_types_for_deduction (unification_kind_t strict
,
13191 /* Swap PARM and ARG throughout the remainder of this
13192 function; the handling is precisely symmetric since PARM
13193 will initialize ARG rather than vice versa. */
13201 /* Core issue #873: Do the DR606 thing (see below) for these cases,
13202 too, but here handle it by stripping the reference from PARM
13203 rather than by adding it to ARG. */
13204 if (TREE_CODE (*parm
) == REFERENCE_TYPE
13205 && TYPE_REF_IS_RVALUE (*parm
)
13206 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
13207 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
13208 && TREE_CODE (*arg
) == REFERENCE_TYPE
13209 && !TYPE_REF_IS_RVALUE (*arg
))
13210 *parm
= TREE_TYPE (*parm
);
13211 /* Nothing else to do in this case. */
13215 gcc_unreachable ();
13218 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
13220 /* [temp.deduct.call]
13222 If P is not a reference type:
13224 --If A is an array type, the pointer type produced by the
13225 array-to-pointer standard conversion (_conv.array_) is
13226 used in place of A for type deduction; otherwise,
13228 --If A is a function type, the pointer type produced by
13229 the function-to-pointer standard conversion
13230 (_conv.func_) is used in place of A for type deduction;
13233 --If A is a cv-qualified type, the top level
13234 cv-qualifiers of A's type are ignored for type
13236 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
13237 *arg
= build_pointer_type (TREE_TYPE (*arg
));
13238 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
13239 *arg
= build_pointer_type (*arg
);
13241 *arg
= TYPE_MAIN_VARIANT (*arg
);
13244 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
13245 of the form T&&, where T is a template parameter, and the argument
13246 is an lvalue, T is deduced as A& */
13247 if (TREE_CODE (*parm
) == REFERENCE_TYPE
13248 && TYPE_REF_IS_RVALUE (*parm
)
13249 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
13250 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
13251 && arg_expr
&& real_lvalue_p (arg_expr
))
13252 *arg
= build_reference_type (*arg
);
13254 /* [temp.deduct.call]
13256 If P is a cv-qualified type, the top level cv-qualifiers
13257 of P's type are ignored for type deduction. If P is a
13258 reference type, the type referred to by P is used for
13260 *parm
= TYPE_MAIN_VARIANT (*parm
);
13261 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
13263 *parm
= TREE_TYPE (*parm
);
13264 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
13267 /* DR 322. For conversion deduction, remove a reference type on parm
13268 too (which has been swapped into ARG). */
13269 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
13270 *arg
= TREE_TYPE (*arg
);
13275 /* Most parms like fn_type_unification.
13277 If SUBR is 1, we're being called recursively (to unify the
13278 arguments of a function or method parameter of a function
13282 type_unification_real (tree tparms
,
13286 unsigned int xnargs
,
13288 unification_kind_t strict
,
13291 tree parm
, arg
, arg_expr
;
13293 int ntparms
= TREE_VEC_LENGTH (tparms
);
13295 int saw_undeduced
= 0;
13298 unsigned int nargs
;
13301 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
13302 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
13303 gcc_assert (ntparms
> 0);
13305 /* Reset the number of non-defaulted template arguments contained
13307 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
13312 sub_strict
= (UNIFY_ALLOW_OUTER_LEVEL
| UNIFY_ALLOW_MORE_CV_QUAL
13313 | UNIFY_ALLOW_DERIVED
);
13317 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
13321 sub_strict
= UNIFY_ALLOW_NONE
;
13325 gcc_unreachable ();
13334 while (parms
&& parms
!= void_list_node
13337 if (TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
13340 parm
= TREE_VALUE (parms
);
13341 parms
= TREE_CHAIN (parms
);
13346 if (arg
== error_mark_node
)
13348 if (arg
== unknown_type_node
)
13349 /* We can't deduce anything from this, but we might get all the
13350 template args from other function args. */
13353 /* Conversions will be performed on a function argument that
13354 corresponds with a function parameter that contains only
13355 non-deducible template parameters and explicitly specified
13356 template parameters. */
13357 if (!uses_template_parms (parm
))
13362 type
= TREE_TYPE (arg
);
13366 if (same_type_p (parm
, type
))
13368 if (strict
!= DEDUCE_EXACT
13369 && can_convert_arg (parm
, type
, TYPE_P (arg
) ? NULL_TREE
: arg
,
13378 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
13379 if (type_unknown_p (arg
))
13381 /* [temp.deduct.type]
13383 A template-argument can be deduced from a pointer to
13384 function or pointer to member function argument if
13385 the set of overloaded functions does not contain
13386 function templates and at most one of a set of
13387 overloaded functions provides a unique match. */
13388 if (resolve_overloaded_unification
13389 (tparms
, targs
, parm
, arg
, strict
, sub_strict
))
13395 arg
= unlowered_expr_type (arg
);
13396 if (arg
== error_mark_node
)
13401 int arg_strict
= sub_strict
;
13404 arg_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
,
13407 if (arg
== init_list_type_node
&& arg_expr
)
13409 if (unify (tparms
, targs
, parm
, arg
, arg_strict
))
13416 && parms
!= void_list_node
13417 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
13419 /* Unify the remaining arguments with the pack expansion type. */
13421 tree parmvec
= make_tree_vec (1);
13423 /* Allocate a TREE_VEC and copy in all of the arguments */
13424 argvec
= make_tree_vec (nargs
- ia
);
13425 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
13426 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
13428 /* Copy the parameter into parmvec. */
13429 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
13430 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
13431 /*call_args_p=*/true, /*subr=*/subr
))
13434 /* Advance to the end of the list of parameters. */
13435 parms
= TREE_CHAIN (parms
);
13438 /* Fail if we've reached the end of the parm list, and more args
13439 are present, and the parm list isn't variadic. */
13440 if (ia
< nargs
&& parms
== void_list_node
)
13442 /* Fail if parms are left and they don't have default values. */
13443 if (parms
&& parms
!= void_list_node
13444 && TREE_PURPOSE (parms
) == NULL_TREE
)
13448 for (i
= 0; i
< ntparms
; i
++)
13449 if (!TREE_VEC_ELT (targs
, i
))
13453 if (TREE_VEC_ELT (tparms
, i
) == error_mark_node
)
13456 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
13458 /* If this is an undeduced nontype parameter that depends on
13459 a type parameter, try another pass; its type may have been
13460 deduced from a later argument than the one from which
13461 this parameter can be deduced. */
13462 if (TREE_CODE (tparm
) == PARM_DECL
13463 && uses_template_parms (TREE_TYPE (tparm
))
13464 && !saw_undeduced
++)
13467 /* Core issue #226 (C++0x) [temp.deduct]:
13469 If a template argument has not been deduced, its
13470 default template argument, if any, is used.
13472 When we are in C++98 mode, TREE_PURPOSE will either
13473 be NULL_TREE or ERROR_MARK_NODE, so we do not need
13474 to explicitly check cxx_dialect here. */
13475 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
13477 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
13478 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
13479 arg
= tsubst_template_arg (arg
, targs
, tf_none
, NULL_TREE
);
13480 arg
= convert_template_argument (parm
, arg
, targs
, tf_none
,
13482 if (arg
== error_mark_node
)
13486 TREE_VEC_ELT (targs
, i
) = arg
;
13487 /* The position of the first default template argument,
13488 is also the number of non-defaulted arguments in TARGS.
13490 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
13491 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
13496 /* If the type parameter is a parameter pack, then it will
13497 be deduced to an empty parameter pack. */
13498 if (template_parameter_pack_p (tparm
))
13502 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
13504 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
13505 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
13506 TREE_CONSTANT (arg
) = 1;
13509 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
13511 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
13513 TREE_VEC_ELT (targs
, i
) = arg
;
13519 #ifdef ENABLE_CHECKING
13520 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
13521 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
13527 /* Subroutine of type_unification_real. Args are like the variables
13528 at the call site. ARG is an overloaded function (or template-id);
13529 we try deducing template args from each of the overloads, and if
13530 only one succeeds, we go with that. Modifies TARGS and returns
13531 true on success. */
13534 resolve_overloaded_unification (tree tparms
,
13538 unification_kind_t strict
,
13541 tree tempargs
= copy_node (targs
);
13543 tree goodfn
= NULL_TREE
;
13546 if (TREE_CODE (arg
) == ADDR_EXPR
)
13548 arg
= TREE_OPERAND (arg
, 0);
13554 if (TREE_CODE (arg
) == COMPONENT_REF
)
13555 /* Handle `&x' where `x' is some static or non-static member
13557 arg
= TREE_OPERAND (arg
, 1);
13559 if (TREE_CODE (arg
) == OFFSET_REF
)
13560 arg
= TREE_OPERAND (arg
, 1);
13562 /* Strip baselink information. */
13563 if (BASELINK_P (arg
))
13564 arg
= BASELINK_FUNCTIONS (arg
);
13566 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
13568 /* If we got some explicit template args, we need to plug them into
13569 the affected templates before we try to unify, in case the
13570 explicit args will completely resolve the templates in question. */
13572 tree expl_subargs
= TREE_OPERAND (arg
, 1);
13573 arg
= TREE_OPERAND (arg
, 0);
13575 for (; arg
; arg
= OVL_NEXT (arg
))
13577 tree fn
= OVL_CURRENT (arg
);
13578 tree subargs
, elem
;
13580 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
13583 ++processing_template_decl
;
13584 subargs
= get_bindings (fn
, DECL_TEMPLATE_RESULT (fn
),
13585 expl_subargs
, /*check_ret=*/false);
13588 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
13589 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
13590 elem
, strict
, sub_strict
, addr_p
)
13591 && (!goodfn
|| !decls_match (goodfn
, elem
)))
13597 --processing_template_decl
;
13600 else if (TREE_CODE (arg
) != OVERLOAD
13601 && TREE_CODE (arg
) != FUNCTION_DECL
)
13602 /* If ARG is, for example, "(0, &f)" then its type will be unknown
13603 -- but the deduction does not succeed because the expression is
13604 not just the function on its own. */
13607 for (; arg
; arg
= OVL_NEXT (arg
))
13608 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
13609 TREE_TYPE (OVL_CURRENT (arg
)),
13610 strict
, sub_strict
, addr_p
)
13611 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
13613 goodfn
= OVL_CURRENT (arg
);
13617 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13618 to function or pointer to member function argument if the set of
13619 overloaded functions does not contain function templates and at most
13620 one of a set of overloaded functions provides a unique match.
13622 So if we found multiple possibilities, we return success but don't
13623 deduce anything. */
13627 int i
= TREE_VEC_LENGTH (targs
);
13629 if (TREE_VEC_ELT (tempargs
, i
))
13630 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
13638 /* Core DR 115: In contexts where deduction is done and fails, or in
13639 contexts where deduction is not done, if a template argument list is
13640 specified and it, along with any default template arguments, identifies
13641 a single function template specialization, then the template-id is an
13642 lvalue for the function template specialization. */
13645 resolve_nondeduced_context (tree orig_expr
)
13647 tree expr
, offset
, baselink
;
13650 if (!type_unknown_p (orig_expr
))
13655 offset
= NULL_TREE
;
13656 baselink
= NULL_TREE
;
13658 if (TREE_CODE (expr
) == ADDR_EXPR
)
13660 expr
= TREE_OPERAND (expr
, 0);
13663 if (TREE_CODE (expr
) == OFFSET_REF
)
13666 expr
= TREE_OPERAND (expr
, 1);
13668 if (TREE_CODE (expr
) == BASELINK
)
13671 expr
= BASELINK_FUNCTIONS (expr
);
13674 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
13677 tree goodfn
= NULL_TREE
;
13679 /* If we got some explicit template args, we need to plug them into
13680 the affected templates before we try to unify, in case the
13681 explicit args will completely resolve the templates in question. */
13683 tree expl_subargs
= TREE_OPERAND (expr
, 1);
13684 tree arg
= TREE_OPERAND (expr
, 0);
13685 tree badfn
= NULL_TREE
;
13686 tree badargs
= NULL_TREE
;
13688 for (; arg
; arg
= OVL_NEXT (arg
))
13690 tree fn
= OVL_CURRENT (arg
);
13691 tree subargs
, elem
;
13693 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
13696 ++processing_template_decl
;
13697 subargs
= get_bindings (fn
, DECL_TEMPLATE_RESULT (fn
),
13698 expl_subargs
, /*check_ret=*/false);
13699 if (subargs
&& !any_dependent_template_arguments_p (subargs
))
13701 elem
= instantiate_template (fn
, subargs
, tf_none
);
13702 if (elem
== error_mark_node
)
13707 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
13713 --processing_template_decl
;
13719 expr
= build_baselink (BASELINK_BINFO (baselink
),
13720 BASELINK_ACCESS_BINFO (baselink
),
13721 expr
, BASELINK_OPTYPE (baselink
));
13723 expr
= build2 (OFFSET_REF
, TREE_TYPE (expr
),
13724 TREE_OPERAND (offset
, 0), expr
);
13726 expr
= build_address (expr
);
13729 else if (good
== 0 && badargs
)
13730 /* There were no good options and at least one bad one, so let the
13731 user know what the problem is. */
13732 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
13737 /* Subroutine of resolve_overloaded_unification; does deduction for a single
13738 overload. Fills TARGS with any deduced arguments, or error_mark_node if
13739 different overloads deduce different arguments for a given parm.
13740 ADDR_P is true if the expression for which deduction is being
13741 performed was of the form "& fn" rather than simply "fn".
13743 Returns 1 on success. */
13746 try_one_overload (tree tparms
,
13751 unification_kind_t strict
,
13759 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13760 to function or pointer to member function argument if the set of
13761 overloaded functions does not contain function templates and at most
13762 one of a set of overloaded functions provides a unique match.
13764 So if this is a template, just return success. */
13766 if (uses_template_parms (arg
))
13769 if (TREE_CODE (arg
) == METHOD_TYPE
)
13770 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
13772 arg
= build_pointer_type (arg
);
13774 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
13776 /* We don't copy orig_targs for this because if we have already deduced
13777 some template args from previous args, unify would complain when we
13778 try to deduce a template parameter for the same argument, even though
13779 there isn't really a conflict. */
13780 nargs
= TREE_VEC_LENGTH (targs
);
13781 tempargs
= make_tree_vec (nargs
);
13783 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
) != 0)
13786 /* First make sure we didn't deduce anything that conflicts with
13787 explicitly specified args. */
13788 for (i
= nargs
; i
--; )
13790 tree elt
= TREE_VEC_ELT (tempargs
, i
);
13791 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
13795 else if (uses_template_parms (elt
))
13796 /* Since we're unifying against ourselves, we will fill in
13797 template args used in the function parm list with our own
13798 template parms. Discard them. */
13799 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
13800 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
13804 for (i
= nargs
; i
--; )
13806 tree elt
= TREE_VEC_ELT (tempargs
, i
);
13809 TREE_VEC_ELT (targs
, i
) = elt
;
13815 /* PARM is a template class (perhaps with unbound template
13816 parameters). ARG is a fully instantiated type. If ARG can be
13817 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
13818 TARGS are as for unify. */
13821 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
)
13823 tree copy_of_targs
;
13825 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
13826 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
13827 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
13830 /* We need to make a new template argument vector for the call to
13831 unify. If we used TARGS, we'd clutter it up with the result of
13832 the attempted unification, even if this class didn't work out.
13833 We also don't want to commit ourselves to all the unifications
13834 we've already done, since unification is supposed to be done on
13835 an argument-by-argument basis. In other words, consider the
13836 following pathological case:
13838 template <int I, int J, int K>
13841 template <int I, int J>
13842 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
13844 template <int I, int J, int K>
13845 void f(S<I, J, K>, S<I, I, I>);
13854 Now, by the time we consider the unification involving `s2', we
13855 already know that we must have `f<0, 0, 0>'. But, even though
13856 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
13857 because there are two ways to unify base classes of S<0, 1, 2>
13858 with S<I, I, I>. If we kept the already deduced knowledge, we
13859 would reject the possibility I=1. */
13860 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
13862 /* If unification failed, we're done. */
13863 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
13864 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
))
13870 /* Given a template type PARM and a class type ARG, find the unique
13871 base type in ARG that is an instance of PARM. We do not examine
13872 ARG itself; only its base-classes. If there is not exactly one
13873 appropriate base class, return NULL_TREE. PARM may be the type of
13874 a partial specialization, as well as a plain template type. Used
13878 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
)
13880 tree rval
= NULL_TREE
;
13883 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
13885 binfo
= TYPE_BINFO (complete_type (arg
));
13887 /* The type could not be completed. */
13890 /* Walk in inheritance graph order. The search order is not
13891 important, and this avoids multiple walks of virtual bases. */
13892 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
13894 tree r
= try_class_unification (tparms
, targs
, parm
, BINFO_TYPE (binfo
));
13898 /* If there is more than one satisfactory baseclass, then:
13902 If they yield more than one possible deduced A, the type
13906 if (rval
&& !same_type_p (r
, rval
))
13916 /* Returns the level of DECL, which declares a template parameter. */
13919 template_decl_level (tree decl
)
13921 switch (TREE_CODE (decl
))
13924 case TEMPLATE_DECL
:
13925 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
13928 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
13931 gcc_unreachable ();
13936 /* Decide whether ARG can be unified with PARM, considering only the
13937 cv-qualifiers of each type, given STRICT as documented for unify.
13938 Returns nonzero iff the unification is OK on that basis. */
13941 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
13943 int arg_quals
= cp_type_quals (arg
);
13944 int parm_quals
= cp_type_quals (parm
);
13946 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
13947 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
13949 /* Although a CVR qualifier is ignored when being applied to a
13950 substituted template parameter ([8.3.2]/1 for example), that
13951 does not apply during deduction [14.8.2.4]/1, (even though
13952 that is not explicitly mentioned, [14.8.2.4]/9 indicates
13953 this). Except when we're allowing additional CV qualifiers
13954 at the outer level [14.8.2.1]/3,1st bullet. */
13955 if ((TREE_CODE (arg
) == REFERENCE_TYPE
13956 || TREE_CODE (arg
) == FUNCTION_TYPE
13957 || TREE_CODE (arg
) == METHOD_TYPE
)
13958 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
13961 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
13962 && (parm_quals
& TYPE_QUAL_RESTRICT
))
13966 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
13967 && (arg_quals
& parm_quals
) != parm_quals
)
13970 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
13971 && (parm_quals
& arg_quals
) != arg_quals
)
13977 /* Determines the LEVEL and INDEX for the template parameter PARM. */
13979 template_parm_level_and_index (tree parm
, int* level
, int* index
)
13981 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
13982 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
13983 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
13985 *index
= TEMPLATE_TYPE_IDX (parm
);
13986 *level
= TEMPLATE_TYPE_LEVEL (parm
);
13990 *index
= TEMPLATE_PARM_IDX (parm
);
13991 *level
= TEMPLATE_PARM_LEVEL (parm
);
13995 /* Unifies the remaining arguments in PACKED_ARGS with the pack
13996 expansion at the end of PACKED_PARMS. Returns 0 if the type
13997 deduction succeeds, 1 otherwise. STRICT is the same as in
13998 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
13999 call argument list. We'll need to adjust the arguments to make them
14000 types. SUBR tells us if this is from a recursive call to
14001 type_unification_real. */
14003 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
14004 tree packed_args
, int strict
, bool call_args_p
,
14008 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
14009 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
14010 tree pack
, packs
= NULL_TREE
;
14011 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
14012 int len
= TREE_VEC_LENGTH (packed_args
);
14014 /* Determine the parameter packs we will be deducing from the
14015 pattern, and record their current deductions. */
14016 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
14017 pack
; pack
= TREE_CHAIN (pack
))
14019 tree parm_pack
= TREE_VALUE (pack
);
14022 /* Determine the index and level of this parameter pack. */
14023 template_parm_level_and_index (parm_pack
, &level
, &idx
);
14025 /* Keep track of the parameter packs and their corresponding
14027 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
14028 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
14031 /* Loop through all of the arguments that have not yet been
14032 unified and unify each with the pattern. */
14033 for (i
= start
; i
< len
; i
++)
14035 tree parm
= pattern
;
14037 /* For each parameter pack, clear out the deduced value so that
14038 we can deduce it again. */
14039 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
14042 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
14044 TMPL_ARG (targs
, level
, idx
) = NULL_TREE
;
14047 /* Unify the pattern with the current argument. */
14049 tree arg
= TREE_VEC_ELT (packed_args
, i
);
14050 tree arg_expr
= NULL_TREE
;
14051 int arg_strict
= strict
;
14052 bool skip_arg_p
= false;
14058 /* This mirrors what we do in type_unification_real. */
14062 sub_strict
= (UNIFY_ALLOW_OUTER_LEVEL
14063 | UNIFY_ALLOW_MORE_CV_QUAL
14064 | UNIFY_ALLOW_DERIVED
);
14068 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
14072 sub_strict
= UNIFY_ALLOW_NONE
;
14076 gcc_unreachable ();
14081 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
14082 if (type_unknown_p (arg
))
14084 /* [temp.deduct.type] A template-argument can be
14085 deduced from a pointer to function or pointer
14086 to member function argument if the set of
14087 overloaded functions does not contain function
14088 templates and at most one of a set of
14089 overloaded functions provides a unique
14092 if (resolve_overloaded_unification
14093 (tparms
, targs
, parm
, arg
,
14094 (unification_kind_t
) strict
,
14104 arg
= unlowered_expr_type (arg
);
14105 if (arg
== error_mark_node
)
14110 arg_strict
= sub_strict
;
14114 maybe_adjust_types_for_deduction ((unification_kind_t
) strict
,
14115 &parm
, &arg
, arg_expr
);
14120 /* For deduction from an init-list we need the actual list. */
14121 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
14123 if (unify (tparms
, targs
, parm
, arg
, arg_strict
))
14128 /* For each parameter pack, collect the deduced value. */
14129 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
14132 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
14134 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
14135 TMPL_ARG (targs
, level
, idx
);
14139 /* Verify that the results of unification with the parameter packs
14140 produce results consistent with what we've seen before, and make
14141 the deduced argument packs available. */
14142 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
14144 tree old_pack
= TREE_VALUE (pack
);
14145 tree new_args
= TREE_TYPE (pack
);
14146 int i
, len
= TREE_VEC_LENGTH (new_args
);
14148 bool nondeduced_p
= false;
14150 /* By default keep the original deduced argument pack.
14151 If necessary, more specific code is going to update the
14152 resulting deduced argument later down in this function. */
14153 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
14154 TMPL_ARG (targs
, level
, idx
) = old_pack
;
14156 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
14157 actually deduce anything. */
14158 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
14159 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
14160 nondeduced_p
= true;
14164 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
14166 /* Prepend the explicit arguments onto NEW_ARGS. */
14167 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
14168 tree old_args
= new_args
;
14169 int i
, explicit_len
= TREE_VEC_LENGTH (explicit_args
);
14170 int len
= explicit_len
+ TREE_VEC_LENGTH (old_args
);
14172 /* Copy the explicit arguments. */
14173 new_args
= make_tree_vec (len
);
14174 for (i
= 0; i
< explicit_len
; i
++)
14175 TREE_VEC_ELT (new_args
, i
) = TREE_VEC_ELT (explicit_args
, i
);
14177 /* Copy the deduced arguments. */
14178 for (; i
< len
; i
++)
14179 TREE_VEC_ELT (new_args
, i
) =
14180 TREE_VEC_ELT (old_args
, i
- explicit_len
);
14186 /* Build the deduced *_ARGUMENT_PACK. */
14187 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
14189 result
= make_node (NONTYPE_ARGUMENT_PACK
);
14190 TREE_TYPE (result
) =
14191 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
14192 TREE_CONSTANT (result
) = 1;
14195 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
14197 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
14199 /* Note the deduced argument packs for this parameter
14201 TMPL_ARG (targs
, level
, idx
) = result
;
14203 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
14204 && (ARGUMENT_PACK_ARGS (old_pack
)
14205 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
14207 /* We only had the explicitly-provided arguments before, but
14208 now we have a complete set of arguments. */
14209 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
14211 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
14212 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
14213 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
14215 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack
),
14217 /* Inconsistent unification of this parameter pack. */
14224 /* Deduce the value of template parameters. TPARMS is the (innermost)
14225 set of template parameters to a template. TARGS is the bindings
14226 for those template parameters, as determined thus far; TARGS may
14227 include template arguments for outer levels of template parameters
14228 as well. PARM is a parameter to a template function, or a
14229 subcomponent of that parameter; ARG is the corresponding argument.
14230 This function attempts to match PARM with ARG in a manner
14231 consistent with the existing assignments in TARGS. If more values
14232 are deduced, then TARGS is updated.
14234 Returns 0 if the type deduction succeeds, 1 otherwise. The
14235 parameter STRICT is a bitwise or of the following flags:
14238 Require an exact match between PARM and ARG.
14239 UNIFY_ALLOW_MORE_CV_QUAL:
14240 Allow the deduced ARG to be more cv-qualified (by qualification
14241 conversion) than ARG.
14242 UNIFY_ALLOW_LESS_CV_QUAL:
14243 Allow the deduced ARG to be less cv-qualified than ARG.
14244 UNIFY_ALLOW_DERIVED:
14245 Allow the deduced ARG to be a template base class of ARG,
14246 or a pointer to a template base class of the type pointed to by
14248 UNIFY_ALLOW_INTEGER:
14249 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
14250 case for more information.
14251 UNIFY_ALLOW_OUTER_LEVEL:
14252 This is the outermost level of a deduction. Used to determine validity
14253 of qualification conversions. A valid qualification conversion must
14254 have const qualified pointers leading up to the inner type which
14255 requires additional CV quals, except at the outer level, where const
14256 is not required [conv.qual]. It would be normal to set this flag in
14257 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
14258 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
14259 This is the outermost level of a deduction, and PARM can be more CV
14260 qualified at this point.
14261 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
14262 This is the outermost level of a deduction, and PARM can be less CV
14263 qualified at this point. */
14266 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
)
14271 int strict_in
= strict
;
14273 /* I don't think this will do the right thing with respect to types.
14274 But the only case I've seen it in so far has been array bounds, where
14275 signedness is the only information lost, and I think that will be
14277 while (TREE_CODE (parm
) == NOP_EXPR
)
14278 parm
= TREE_OPERAND (parm
, 0);
14280 if (arg
== error_mark_node
)
14282 if (arg
== unknown_type_node
14283 || arg
== init_list_type_node
)
14284 /* We can't deduce anything from this, but we might get all the
14285 template args from other function args. */
14288 /* If PARM uses template parameters, then we can't bail out here,
14289 even if ARG == PARM, since we won't record unifications for the
14290 template parameters. We might need them if we're trying to
14291 figure out which of two things is more specialized. */
14292 if (arg
== parm
&& !uses_template_parms (parm
))
14295 /* Handle init lists early, so the rest of the function can assume
14296 we're dealing with a type. */
14297 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
14301 tree orig_parm
= parm
;
14303 /* Replace T with std::initializer_list<T> for deduction. */
14304 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
14305 && flag_deduce_init_list
)
14306 parm
= listify (parm
);
14308 if (!is_std_init_list (parm
))
14309 /* We can only deduce from an initializer list argument if the
14310 parameter is std::initializer_list; otherwise this is a
14311 non-deduced context. */
14314 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
14316 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
14318 int elt_strict
= strict
;
14319 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
14321 tree type
= TREE_TYPE (elt
);
14322 /* It should only be possible to get here for a call. */
14323 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
14324 elt_strict
|= maybe_adjust_types_for_deduction
14325 (DEDUCE_CALL
, &elttype
, &type
, elt
);
14329 if (unify (tparms
, targs
, elttype
, elt
, elt_strict
))
14333 /* If the std::initializer_list<T> deduction worked, replace the
14334 deduced A with std::initializer_list<A>. */
14335 if (orig_parm
!= parm
)
14337 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
14338 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
14339 targ
= listify (targ
);
14340 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
14345 /* Immediately reject some pairs that won't unify because of
14346 cv-qualification mismatches. */
14347 if (TREE_CODE (arg
) == TREE_CODE (parm
)
14349 /* It is the elements of the array which hold the cv quals of an array
14350 type, and the elements might be template type parms. We'll check
14351 when we recurse. */
14352 && TREE_CODE (arg
) != ARRAY_TYPE
14353 /* We check the cv-qualifiers when unifying with template type
14354 parameters below. We want to allow ARG `const T' to unify with
14355 PARM `T' for example, when computing which of two templates
14356 is more specialized, for example. */
14357 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
14358 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
14361 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
14362 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
14363 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
14364 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
14365 strict
&= ~UNIFY_ALLOW_DERIVED
;
14366 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
14367 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
14369 switch (TREE_CODE (parm
))
14371 case TYPENAME_TYPE
:
14373 case UNBOUND_CLASS_TEMPLATE
:
14374 /* In a type which contains a nested-name-specifier, template
14375 argument values cannot be deduced for template parameters used
14376 within the nested-name-specifier. */
14379 case TEMPLATE_TYPE_PARM
:
14380 case TEMPLATE_TEMPLATE_PARM
:
14381 case BOUND_TEMPLATE_TEMPLATE_PARM
:
14382 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
14383 if (tparm
== error_mark_node
)
14386 if (TEMPLATE_TYPE_LEVEL (parm
)
14387 != template_decl_level (tparm
))
14388 /* The PARM is not one we're trying to unify. Just check
14389 to see if it matches ARG. */
14390 return (TREE_CODE (arg
) == TREE_CODE (parm
)
14391 && same_type_p (parm
, arg
)) ? 0 : 1;
14392 idx
= TEMPLATE_TYPE_IDX (parm
);
14393 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
14394 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
14396 /* Check for mixed types and values. */
14397 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
14398 && TREE_CODE (tparm
) != TYPE_DECL
)
14399 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
14400 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
14403 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
14405 /* ARG must be constructed from a template class or a template
14406 template parameter. */
14407 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
14408 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
14412 tree parmvec
= TYPE_TI_ARGS (parm
);
14413 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
14415 = DECL_INNERMOST_TEMPLATE_PARMS
14416 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
14418 int parm_variadic_p
= 0;
14420 /* The resolution to DR150 makes clear that default
14421 arguments for an N-argument may not be used to bind T
14422 to a template template parameter with fewer than N
14423 parameters. It is not safe to permit the binding of
14424 default arguments as an extension, as that may change
14425 the meaning of a conforming program. Consider:
14427 struct Dense { static const unsigned int dim = 1; };
14429 template <template <typename> class View,
14431 void operator+(float, View<Block> const&);
14433 template <typename Block,
14434 unsigned int Dim = Block::dim>
14435 struct Lvalue_proxy { operator float() const; };
14439 Lvalue_proxy<Dense> p;
14444 Here, if Lvalue_proxy is permitted to bind to View, then
14445 the global operator+ will be used; if they are not, the
14446 Lvalue_proxy will be converted to float. */
14447 if (coerce_template_parms (parm_parms
,
14449 TYPE_TI_TEMPLATE (parm
),
14451 /*require_all_args=*/true,
14452 /*use_default_args=*/false)
14453 == error_mark_node
)
14456 /* Deduce arguments T, i from TT<T> or TT<i>.
14457 We check each element of PARMVEC and ARGVEC individually
14458 rather than the whole TREE_VEC since they can have
14459 different number of elements. */
14461 parmvec
= expand_template_argument_pack (parmvec
);
14462 argvec
= expand_template_argument_pack (argvec
);
14464 len
= TREE_VEC_LENGTH (parmvec
);
14466 /* Check if the parameters end in a pack, making them
14469 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
14470 parm_variadic_p
= 1;
14472 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
14475 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
14477 if (unify (tparms
, targs
,
14478 TREE_VEC_ELT (parmvec
, i
),
14479 TREE_VEC_ELT (argvec
, i
),
14484 if (parm_variadic_p
14485 && unify_pack_expansion (tparms
, targs
,
14488 /*call_args_p=*/false,
14492 arg
= TYPE_TI_TEMPLATE (arg
);
14494 /* Fall through to deduce template name. */
14497 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
14498 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
14500 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
14502 /* Simple cases: Value already set, does match or doesn't. */
14503 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
14510 /* If PARM is `const T' and ARG is only `int', we don't have
14511 a match unless we are allowing additional qualification.
14512 If ARG is `const int' and PARM is just `T' that's OK;
14513 that binds `const int' to `T'. */
14514 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
14518 /* Consider the case where ARG is `const volatile int' and
14519 PARM is `const T'. Then, T should be `volatile int'. */
14520 arg
= cp_build_qualified_type_real
14521 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
14522 if (arg
== error_mark_node
)
14525 /* Simple cases: Value already set, does match or doesn't. */
14526 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
14531 /* Make sure that ARG is not a variable-sized array. (Note
14532 that were talking about variable-sized arrays (like
14533 `int[n]'), rather than arrays of unknown size (like
14534 `int[]').) We'll get very confused by such a type since
14535 the bound of the array will not be computable in an
14536 instantiation. Besides, such types are not allowed in
14537 ISO C++, so we can do as we please here. */
14538 if (variably_modified_type_p (arg
, NULL_TREE
))
14541 /* Strip typedefs as in convert_template_argument. */
14542 arg
= strip_typedefs (arg
);
14545 /* If ARG is a parameter pack or an expansion, we cannot unify
14546 against it unless PARM is also a parameter pack. */
14547 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
14548 && !template_parameter_pack_p (parm
))
14551 /* If the argument deduction results is a METHOD_TYPE,
14552 then there is a problem.
14553 METHOD_TYPE doesn't map to any real C++ type the result of
14554 the deduction can not be of that type. */
14555 if (TREE_CODE (arg
) == METHOD_TYPE
)
14558 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
14561 case TEMPLATE_PARM_INDEX
:
14562 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
14563 if (tparm
== error_mark_node
)
14566 if (TEMPLATE_PARM_LEVEL (parm
)
14567 != template_decl_level (tparm
))
14568 /* The PARM is not one we're trying to unify. Just check
14569 to see if it matches ARG. */
14570 return !(TREE_CODE (arg
) == TREE_CODE (parm
)
14571 && cp_tree_equal (parm
, arg
));
14573 idx
= TEMPLATE_PARM_IDX (parm
);
14574 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
14577 return !cp_tree_equal (targ
, arg
);
14579 /* [temp.deduct.type] If, in the declaration of a function template
14580 with a non-type template-parameter, the non-type
14581 template-parameter is used in an expression in the function
14582 parameter-list and, if the corresponding template-argument is
14583 deduced, the template-argument type shall match the type of the
14584 template-parameter exactly, except that a template-argument
14585 deduced from an array bound may be of any integral type.
14586 The non-type parameter might use already deduced type parameters. */
14587 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
14588 if (!TREE_TYPE (arg
))
14589 /* Template-parameter dependent expression. Just accept it for now.
14590 It will later be processed in convert_template_argument. */
14592 else if (same_type_p (TREE_TYPE (arg
), tparm
))
14594 else if ((strict
& UNIFY_ALLOW_INTEGER
)
14595 && (TREE_CODE (tparm
) == INTEGER_TYPE
14596 || TREE_CODE (tparm
) == BOOLEAN_TYPE
))
14597 /* Convert the ARG to the type of PARM; the deduced non-type
14598 template argument must exactly match the types of the
14599 corresponding parameter. */
14600 arg
= fold (build_nop (tparm
, arg
));
14601 else if (uses_template_parms (tparm
))
14602 /* We haven't deduced the type of this parameter yet. Try again
14608 /* If ARG is a parameter pack or an expansion, we cannot unify
14609 against it unless PARM is also a parameter pack. */
14610 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
14611 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
14614 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
14619 /* A pointer-to-member constant can be unified only with
14620 another constant. */
14621 if (TREE_CODE (arg
) != PTRMEM_CST
)
14624 /* Just unify the class member. It would be useless (and possibly
14625 wrong, depending on the strict flags) to unify also
14626 PTRMEM_CST_CLASS, because we want to be sure that both parm and
14627 arg refer to the same variable, even if through different
14628 classes. For instance:
14630 struct A { int x; };
14633 Unification of &A::x and &B::x must succeed. */
14634 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
14635 PTRMEM_CST_MEMBER (arg
), strict
);
14640 if (TREE_CODE (arg
) != POINTER_TYPE
)
14643 /* [temp.deduct.call]
14645 A can be another pointer or pointer to member type that can
14646 be converted to the deduced A via a qualification
14647 conversion (_conv.qual_).
14649 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
14650 This will allow for additional cv-qualification of the
14651 pointed-to types if appropriate. */
14653 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
14654 /* The derived-to-base conversion only persists through one
14655 level of pointers. */
14656 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
14658 return unify (tparms
, targs
, TREE_TYPE (parm
),
14659 TREE_TYPE (arg
), strict
);
14662 case REFERENCE_TYPE
:
14663 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
14665 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
14666 strict
& UNIFY_ALLOW_MORE_CV_QUAL
);
14669 if (TREE_CODE (arg
) != ARRAY_TYPE
)
14671 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
14672 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
14674 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
14681 /* Our representation of array types uses "N - 1" as the
14682 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
14683 not an integer constant. We cannot unify arbitrarily
14684 complex expressions, so we eliminate the MINUS_EXPRs
14686 parm_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (parm
));
14687 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
14690 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
14691 parm_max
= TREE_OPERAND (parm_max
, 0);
14693 arg_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (arg
));
14694 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
14697 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
14698 trying to unify the type of a variable with the type
14699 of a template parameter. For example:
14701 template <unsigned int N>
14702 void f (char (&) [N]);
14709 Here, the type of the ARG will be "int [g(i)]", and
14710 may be a SAVE_EXPR, etc. */
14711 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
14713 arg_max
= TREE_OPERAND (arg_max
, 0);
14716 /* If only one of the bounds used a MINUS_EXPR, compensate
14717 by adding one to the other bound. */
14718 if (parm_cst
&& !arg_cst
)
14719 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
14723 else if (arg_cst
&& !parm_cst
)
14724 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
14729 if (unify (tparms
, targs
, parm_max
, arg_max
, UNIFY_ALLOW_INTEGER
))
14732 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
14733 strict
& UNIFY_ALLOW_MORE_CV_QUAL
);
14740 case ENUMERAL_TYPE
:
14742 if (TREE_CODE (arg
) != TREE_CODE (parm
))
14745 /* We have already checked cv-qualification at the top of the
14747 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
14750 /* As far as unification is concerned, this wins. Later checks
14751 will invalidate it if necessary. */
14754 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
14755 /* Type INTEGER_CST can come from ordinary constant template args. */
14757 while (TREE_CODE (arg
) == NOP_EXPR
)
14758 arg
= TREE_OPERAND (arg
, 0);
14760 if (TREE_CODE (arg
) != INTEGER_CST
)
14762 return !tree_int_cst_equal (parm
, arg
);
14767 if (TREE_CODE (arg
) != TREE_VEC
)
14769 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
14771 for (i
= 0; i
< TREE_VEC_LENGTH (parm
); ++i
)
14772 if (unify (tparms
, targs
,
14773 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
14781 if (TREE_CODE (arg
) != TREE_CODE (parm
))
14784 if (TYPE_PTRMEMFUNC_P (parm
))
14786 if (!TYPE_PTRMEMFUNC_P (arg
))
14789 return unify (tparms
, targs
,
14790 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
14791 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
14795 if (CLASSTYPE_TEMPLATE_INFO (parm
))
14797 tree t
= NULL_TREE
;
14799 if (strict_in
& UNIFY_ALLOW_DERIVED
)
14801 /* First, we try to unify the PARM and ARG directly. */
14802 t
= try_class_unification (tparms
, targs
,
14807 /* Fallback to the special case allowed in
14808 [temp.deduct.call]:
14810 If P is a class, and P has the form
14811 template-id, then A can be a derived class of
14812 the deduced A. Likewise, if P is a pointer to
14813 a class of the form template-id, A can be a
14814 pointer to a derived class pointed to by the
14816 t
= get_template_base (tparms
, targs
, parm
, arg
);
14822 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
14823 && (CLASSTYPE_TI_TEMPLATE (parm
)
14824 == CLASSTYPE_TI_TEMPLATE (arg
)))
14825 /* Perhaps PARM is something like S<U> and ARG is S<int>.
14826 Then, we should unify `int' and `U'. */
14829 /* There's no chance of unification succeeding. */
14832 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
14833 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
);
14835 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
14840 case FUNCTION_TYPE
:
14842 unsigned int nargs
;
14847 if (TREE_CODE (arg
) != TREE_CODE (parm
))
14850 /* CV qualifications for methods can never be deduced, they must
14851 match exactly. We need to check them explicitly here,
14852 because type_unification_real treats them as any other
14853 cv-qualified parameter. */
14854 if (TREE_CODE (parm
) == METHOD_TYPE
14855 && (!check_cv_quals_for_unify
14857 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg
))),
14858 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm
))))))
14861 if (unify (tparms
, targs
, TREE_TYPE (parm
),
14862 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
))
14865 nargs
= list_length (TYPE_ARG_TYPES (arg
));
14866 args
= XALLOCAVEC (tree
, nargs
);
14867 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
14868 a
!= NULL_TREE
&& a
!= void_list_node
;
14869 a
= TREE_CHAIN (a
), ++i
)
14870 args
[i
] = TREE_VALUE (a
);
14873 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
14874 args
, nargs
, 1, DEDUCE_EXACT
,
14879 /* Unify a pointer to member with a pointer to member function, which
14880 deduces the type of the member as a function type. */
14881 if (TYPE_PTRMEMFUNC_P (arg
))
14885 cp_cv_quals cv_quals
;
14887 /* Check top-level cv qualifiers */
14888 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
14891 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
14892 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
), UNIFY_ALLOW_NONE
))
14895 /* Determine the type of the function we are unifying against. */
14896 method_type
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg
));
14898 build_function_type (TREE_TYPE (method_type
),
14899 TREE_CHAIN (TYPE_ARG_TYPES (method_type
)));
14901 /* Extract the cv-qualifiers of the member function from the
14902 implicit object parameter and place them on the function
14903 type to be restored later. */
14905 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type
))));
14906 fntype
= build_qualified_type (fntype
, cv_quals
);
14907 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
);
14910 if (TREE_CODE (arg
) != OFFSET_TYPE
)
14912 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
14913 TYPE_OFFSET_BASETYPE (arg
), UNIFY_ALLOW_NONE
))
14915 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
14919 if (DECL_TEMPLATE_PARM_P (parm
))
14920 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
);
14921 if (arg
!= integral_constant_value (parm
))
14926 case TEMPLATE_DECL
:
14927 /* Matched cases are handled by the ARG == PARM test above. */
14930 case TYPE_ARGUMENT_PACK
:
14931 case NONTYPE_ARGUMENT_PACK
:
14933 tree packed_parms
= ARGUMENT_PACK_ARGS (parm
);
14934 tree packed_args
= ARGUMENT_PACK_ARGS (arg
);
14935 int i
, len
= TREE_VEC_LENGTH (packed_parms
);
14936 int argslen
= TREE_VEC_LENGTH (packed_args
);
14937 int parm_variadic_p
= 0;
14939 for (i
= 0; i
< len
; ++i
)
14941 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms
, i
)))
14944 /* We can unify against something with a trailing
14946 parm_variadic_p
= 1;
14948 /* Since there is something following the pack
14949 expansion, we cannot unify this template argument
14956 /* If we don't have enough arguments to satisfy the parameters
14957 (not counting the pack expression at the end), or we have
14958 too many arguments for a parameter list that doesn't end in
14959 a pack expression, we can't unify. */
14960 if (argslen
< (len
- parm_variadic_p
)
14961 || (argslen
> len
&& !parm_variadic_p
))
14964 /* Unify all of the parameters that precede the (optional)
14965 pack expression. */
14966 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
14968 if (unify (tparms
, targs
, TREE_VEC_ELT (packed_parms
, i
),
14969 TREE_VEC_ELT (packed_args
, i
), strict
))
14973 if (parm_variadic_p
)
14974 return unify_pack_expansion (tparms
, targs
,
14975 packed_parms
, packed_args
,
14976 strict
, /*call_args_p=*/false,
14984 case DECLTYPE_TYPE
:
14985 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
14990 /* Unification fails if we hit an error node. */
14994 gcc_assert (EXPR_P (parm
));
14996 /* We must be looking at an expression. This can happen with
15000 void foo(S<I>, S<I + 2>);
15002 This is a "nondeduced context":
15006 The nondeduced contexts are:
15008 --A type that is a template-id in which one or more of
15009 the template-arguments is an expression that references
15010 a template-parameter.
15012 In these cases, we assume deduction succeeded, but don't
15013 actually infer any unifications. */
15015 if (!uses_template_parms (parm
)
15016 && !template_args_equal (parm
, arg
))
15023 /* Note that DECL can be defined in this translation unit, if
15027 mark_definable (tree decl
)
15030 DECL_NOT_REALLY_EXTERN (decl
) = 1;
15031 FOR_EACH_CLONE (clone
, decl
)
15032 DECL_NOT_REALLY_EXTERN (clone
) = 1;
15035 /* Called if RESULT is explicitly instantiated, or is a member of an
15036 explicitly instantiated class. */
15039 mark_decl_instantiated (tree result
, int extern_p
)
15041 SET_DECL_EXPLICIT_INSTANTIATION (result
);
15043 /* If this entity has already been written out, it's too late to
15044 make any modifications. */
15045 if (TREE_ASM_WRITTEN (result
))
15048 if (TREE_CODE (result
) != FUNCTION_DECL
)
15049 /* The TREE_PUBLIC flag for function declarations will have been
15050 set correctly by tsubst. */
15051 TREE_PUBLIC (result
) = 1;
15053 /* This might have been set by an earlier implicit instantiation. */
15054 DECL_COMDAT (result
) = 0;
15057 DECL_NOT_REALLY_EXTERN (result
) = 0;
15060 mark_definable (result
);
15061 /* Always make artificials weak. */
15062 if (DECL_ARTIFICIAL (result
) && flag_weak
)
15063 comdat_linkage (result
);
15064 /* For WIN32 we also want to put explicit instantiations in
15065 linkonce sections. */
15066 else if (TREE_PUBLIC (result
))
15067 maybe_make_one_only (result
);
15070 /* If EXTERN_P, then this function will not be emitted -- unless
15071 followed by an explicit instantiation, at which point its linkage
15072 will be adjusted. If !EXTERN_P, then this function will be
15073 emitted here. In neither circumstance do we want
15074 import_export_decl to adjust the linkage. */
15075 DECL_INTERFACE_KNOWN (result
) = 1;
15078 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
15079 important template arguments. If any are missing, we check whether
15080 they're important by using error_mark_node for substituting into any
15081 args that were used for partial ordering (the ones between ARGS and END)
15082 and seeing if it bubbles up. */
15085 check_undeduced_parms (tree targs
, tree args
, tree end
)
15087 bool found
= false;
15089 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
15090 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
15093 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
15097 for (; args
!= end
; args
= TREE_CHAIN (args
))
15099 tree substed
= tsubst (TREE_VALUE (args
), targs
, tf_none
, NULL_TREE
);
15100 if (substed
== error_mark_node
)
15107 /* Given two function templates PAT1 and PAT2, return:
15109 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
15110 -1 if PAT2 is more specialized than PAT1.
15111 0 if neither is more specialized.
15113 LEN indicates the number of parameters we should consider
15114 (defaulted parameters should not be considered).
15116 The 1998 std underspecified function template partial ordering, and
15117 DR214 addresses the issue. We take pairs of arguments, one from
15118 each of the templates, and deduce them against each other. One of
15119 the templates will be more specialized if all the *other*
15120 template's arguments deduce against its arguments and at least one
15121 of its arguments *does* *not* deduce against the other template's
15122 corresponding argument. Deduction is done as for class templates.
15123 The arguments used in deduction have reference and top level cv
15124 qualifiers removed. Iff both arguments were originally reference
15125 types *and* deduction succeeds in both directions, the template
15126 with the more cv-qualified argument wins for that pairing (if
15127 neither is more cv-qualified, they both are equal). Unlike regular
15128 deduction, after all the arguments have been deduced in this way,
15129 we do *not* verify the deduced template argument values can be
15130 substituted into non-deduced contexts.
15132 The logic can be a bit confusing here, because we look at deduce1 and
15133 targs1 to see if pat2 is at least as specialized, and vice versa; if we
15134 can find template arguments for pat1 to make arg1 look like arg2, that
15135 means that arg2 is at least as specialized as arg1. */
15138 more_specialized_fn (tree pat1
, tree pat2
, int len
)
15140 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
15141 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
15142 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
15143 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
15144 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
15145 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
15146 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
15147 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
15148 tree origs1
, origs2
;
15149 bool lose1
= false;
15150 bool lose2
= false;
15152 /* Remove the this parameter from non-static member functions. If
15153 one is a non-static member function and the other is not a static
15154 member function, remove the first parameter from that function
15155 also. This situation occurs for operator functions where we
15156 locate both a member function (with this pointer) and non-member
15157 operator (with explicit first operand). */
15158 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
15160 len
--; /* LEN is the number of significant arguments for DECL1 */
15161 args1
= TREE_CHAIN (args1
);
15162 if (!DECL_STATIC_FUNCTION_P (decl2
))
15163 args2
= TREE_CHAIN (args2
);
15165 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
15167 args2
= TREE_CHAIN (args2
);
15168 if (!DECL_STATIC_FUNCTION_P (decl1
))
15171 args1
= TREE_CHAIN (args1
);
15175 /* If only one is a conversion operator, they are unordered. */
15176 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
15179 /* Consider the return type for a conversion function */
15180 if (DECL_CONV_FN_P (decl1
))
15182 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
15183 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
15187 processing_template_decl
++;
15193 /* Stop when an ellipsis is seen. */
15194 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
15196 tree arg1
= TREE_VALUE (args1
);
15197 tree arg2
= TREE_VALUE (args2
);
15198 int deduce1
, deduce2
;
15202 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
15203 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
15205 /* When both arguments are pack expansions, we need only
15206 unify the patterns themselves. */
15207 arg1
= PACK_EXPANSION_PATTERN (arg1
);
15208 arg2
= PACK_EXPANSION_PATTERN (arg2
);
15210 /* This is the last comparison we need to do. */
15214 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
15216 arg1
= TREE_TYPE (arg1
);
15217 quals1
= cp_type_quals (arg1
);
15220 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
15222 arg2
= TREE_TYPE (arg2
);
15223 quals2
= cp_type_quals (arg2
);
15226 if ((quals1
< 0) != (quals2
< 0))
15228 /* Only of the args is a reference, see if we should apply
15229 array/function pointer decay to it. This is not part of
15230 DR214, but is, IMHO, consistent with the deduction rules
15231 for the function call itself, and with our earlier
15232 implementation of the underspecified partial ordering
15233 rules. (nathan). */
15236 switch (TREE_CODE (arg1
))
15239 arg1
= TREE_TYPE (arg1
);
15241 case FUNCTION_TYPE
:
15242 arg1
= build_pointer_type (arg1
);
15251 switch (TREE_CODE (arg2
))
15254 arg2
= TREE_TYPE (arg2
);
15256 case FUNCTION_TYPE
:
15257 arg2
= build_pointer_type (arg2
);
15266 arg1
= TYPE_MAIN_VARIANT (arg1
);
15267 arg2
= TYPE_MAIN_VARIANT (arg2
);
15269 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
15271 int i
, len2
= list_length (args2
);
15272 tree parmvec
= make_tree_vec (1);
15273 tree argvec
= make_tree_vec (len2
);
15276 /* Setup the parameter vector, which contains only ARG1. */
15277 TREE_VEC_ELT (parmvec
, 0) = arg1
;
15279 /* Setup the argument vector, which contains the remaining
15281 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
15282 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
15284 deduce1
= !unify_pack_expansion (tparms1
, targs1
, parmvec
,
15285 argvec
, UNIFY_ALLOW_NONE
,
15286 /*call_args_p=*/false,
15289 /* We cannot deduce in the other direction, because ARG1 is
15290 a pack expansion but ARG2 is not. */
15293 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
15295 int i
, len1
= list_length (args1
);
15296 tree parmvec
= make_tree_vec (1);
15297 tree argvec
= make_tree_vec (len1
);
15300 /* Setup the parameter vector, which contains only ARG1. */
15301 TREE_VEC_ELT (parmvec
, 0) = arg2
;
15303 /* Setup the argument vector, which contains the remaining
15305 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
15306 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
15308 deduce2
= !unify_pack_expansion (tparms2
, targs2
, parmvec
,
15309 argvec
, UNIFY_ALLOW_NONE
,
15310 /*call_args_p=*/false,
15313 /* We cannot deduce in the other direction, because ARG2 is
15314 a pack expansion but ARG1 is not.*/
15320 /* The normal case, where neither argument is a pack
15322 deduce1
= !unify (tparms1
, targs1
, arg1
, arg2
, UNIFY_ALLOW_NONE
);
15323 deduce2
= !unify (tparms2
, targs2
, arg2
, arg1
, UNIFY_ALLOW_NONE
);
15326 /* If we couldn't deduce arguments for tparms1 to make arg1 match
15327 arg2, then arg2 is not as specialized as arg1. */
15333 /* "If, for a given type, deduction succeeds in both directions
15334 (i.e., the types are identical after the transformations above)
15335 and if the type from the argument template is more cv-qualified
15336 than the type from the parameter template (as described above)
15337 that type is considered to be more specialized than the other. If
15338 neither type is more cv-qualified than the other then neither type
15339 is more specialized than the other."
15341 We check same_type_p explicitly because deduction can also succeed
15342 in both directions when there is a nondeduced context. */
15343 if (deduce1
&& deduce2
15344 && quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0
15345 && same_type_p (arg1
, arg2
))
15347 if ((quals1
& quals2
) == quals2
)
15349 if ((quals1
& quals2
) == quals1
)
15353 if (lose1
&& lose2
)
15354 /* We've failed to deduce something in either direction.
15355 These must be unordered. */
15358 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
15359 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
15360 /* We have already processed all of the arguments in our
15361 handing of the pack expansion type. */
15364 args1
= TREE_CHAIN (args1
);
15365 args2
= TREE_CHAIN (args2
);
15368 /* "In most cases, all template parameters must have values in order for
15369 deduction to succeed, but for partial ordering purposes a template
15370 parameter may remain without a value provided it is not used in the
15371 types being used for partial ordering."
15373 Thus, if we are missing any of the targs1 we need to substitute into
15374 origs1, then pat2 is not as specialized as pat1. This can happen when
15375 there is a nondeduced context. */
15376 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
15378 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
15381 processing_template_decl
--;
15383 /* All things being equal, if the next argument is a pack expansion
15384 for one function but not for the other, prefer the
15385 non-variadic function. FIXME this is bogus; see c++/41958. */
15387 && args1
&& TREE_VALUE (args1
)
15388 && args2
&& TREE_VALUE (args2
))
15390 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
15391 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
15394 if (lose1
== lose2
)
15402 /* Determine which of two partial specializations is more specialized.
15404 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
15405 to the first partial specialization. The TREE_VALUE is the
15406 innermost set of template parameters for the partial
15407 specialization. PAT2 is similar, but for the second template.
15409 Return 1 if the first partial specialization is more specialized;
15410 -1 if the second is more specialized; 0 if neither is more
15413 See [temp.class.order] for information about determining which of
15414 two templates is more specialized. */
15417 more_specialized_class (tree pat1
, tree pat2
)
15422 bool any_deductions
= false;
15424 tmpl1
= TREE_TYPE (pat1
);
15425 tmpl2
= TREE_TYPE (pat2
);
15427 /* Just like what happens for functions, if we are ordering between
15428 different class template specializations, we may encounter dependent
15429 types in the arguments, and we need our dependency check functions
15430 to behave correctly. */
15431 ++processing_template_decl
;
15432 targs
= get_class_bindings (TREE_VALUE (pat1
),
15433 CLASSTYPE_TI_ARGS (tmpl1
),
15434 CLASSTYPE_TI_ARGS (tmpl2
));
15438 any_deductions
= true;
15441 targs
= get_class_bindings (TREE_VALUE (pat2
),
15442 CLASSTYPE_TI_ARGS (tmpl2
),
15443 CLASSTYPE_TI_ARGS (tmpl1
));
15447 any_deductions
= true;
15449 --processing_template_decl
;
15451 /* In the case of a tie where at least one of the class templates
15452 has a parameter pack at the end, the template with the most
15453 non-packed parameters wins. */
15456 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
15457 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
15459 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
15460 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
15461 int len1
= TREE_VEC_LENGTH (args1
);
15462 int len2
= TREE_VEC_LENGTH (args2
);
15464 /* We don't count the pack expansion at the end. */
15465 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
15467 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
15472 else if (len1
< len2
)
15479 /* Return the template arguments that will produce the function signature
15480 DECL from the function template FN, with the explicit template
15481 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
15482 also match. Return NULL_TREE if no satisfactory arguments could be
15486 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
15488 int ntparms
= DECL_NTPARMS (fn
);
15489 tree targs
= make_tree_vec (ntparms
);
15491 tree decl_arg_types
;
15493 unsigned int nargs
, ix
;
15496 /* Substitute the explicit template arguments into the type of DECL.
15497 The call to fn_type_unification will handle substitution into the
15499 decl_type
= TREE_TYPE (decl
);
15500 if (explicit_args
&& uses_template_parms (decl_type
))
15503 tree converted_args
;
15505 if (DECL_TEMPLATE_INFO (decl
))
15506 tmpl
= DECL_TI_TEMPLATE (decl
);
15508 /* We can get here for some invalid specializations. */
15512 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
15513 explicit_args
, NULL_TREE
,
15515 /*require_all_args=*/false,
15516 /*use_default_args=*/false);
15517 if (converted_args
== error_mark_node
)
15520 decl_type
= tsubst (decl_type
, converted_args
, tf_none
, NULL_TREE
);
15521 if (decl_type
== error_mark_node
)
15525 /* Never do unification on the 'this' parameter. */
15526 decl_arg_types
= skip_artificial_parms_for (decl
,
15527 TYPE_ARG_TYPES (decl_type
));
15529 nargs
= list_length (decl_arg_types
);
15530 args
= XALLOCAVEC (tree
, nargs
);
15531 for (arg
= decl_arg_types
, ix
= 0;
15532 arg
!= NULL_TREE
&& arg
!= void_list_node
;
15533 arg
= TREE_CHAIN (arg
), ++ix
)
15534 args
[ix
] = TREE_VALUE (arg
);
15536 if (fn_type_unification (fn
, explicit_args
, targs
,
15538 (check_rettype
|| DECL_CONV_FN_P (fn
)
15539 ? TREE_TYPE (decl_type
) : NULL_TREE
),
15540 DEDUCE_EXACT
, LOOKUP_NORMAL
))
15546 /* Return the innermost template arguments that, when applied to a
15547 template specialization whose innermost template parameters are
15548 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
15551 For example, suppose we have:
15553 template <class T, class U> struct S {};
15554 template <class T> struct S<T*, int> {};
15556 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
15557 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
15558 int}. The resulting vector will be {double}, indicating that `T'
15559 is bound to `double'. */
15562 get_class_bindings (tree tparms
, tree spec_args
, tree args
)
15564 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
15566 tree innermost_deduced_args
;
15568 innermost_deduced_args
= make_tree_vec (ntparms
);
15569 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
15571 deduced_args
= copy_node (args
);
15572 SET_TMPL_ARGS_LEVEL (deduced_args
,
15573 TMPL_ARGS_DEPTH (deduced_args
),
15574 innermost_deduced_args
);
15577 deduced_args
= innermost_deduced_args
;
15579 if (unify (tparms
, deduced_args
,
15580 INNERMOST_TEMPLATE_ARGS (spec_args
),
15581 INNERMOST_TEMPLATE_ARGS (args
),
15585 for (i
= 0; i
< ntparms
; ++i
)
15586 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
15589 /* Verify that nondeduced template arguments agree with the type
15590 obtained from argument deduction.
15594 struct A { typedef int X; };
15595 template <class T, class U> struct C {};
15596 template <class T> struct C<T, typename T::X> {};
15598 Then with the instantiation `C<A, int>', we can deduce that
15599 `T' is `A' but unify () does not check whether `typename T::X'
15601 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
15602 if (spec_args
== error_mark_node
15603 /* We only need to check the innermost arguments; the other
15604 arguments will always agree. */
15605 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
15606 INNERMOST_TEMPLATE_ARGS (args
)))
15609 /* Now that we have bindings for all of the template arguments,
15610 ensure that the arguments deduced for the template template
15611 parameters have compatible template parameter lists. See the use
15612 of template_template_parm_bindings_ok_p in fn_type_unification
15613 for more information. */
15614 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
15617 return deduced_args
;
15620 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
15621 Return the TREE_LIST node with the most specialized template, if
15622 any. If there is no most specialized template, the error_mark_node
15625 Note that this function does not look at, or modify, the
15626 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
15627 returned is one of the elements of INSTANTIATIONS, callers may
15628 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
15629 and retrieve it from the value returned. */
15632 most_specialized_instantiation (tree templates
)
15636 ++processing_template_decl
;
15639 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
15643 if (get_bindings (TREE_VALUE (champ
),
15644 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
15645 NULL_TREE
, /*check_ret=*/false))
15648 if (get_bindings (TREE_VALUE (fn
),
15649 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
15650 NULL_TREE
, /*check_ret=*/false))
15657 /* Equally specialized, move to next function. If there
15658 is no next function, nothing's most specialized. */
15659 fn
= TREE_CHAIN (fn
);
15667 /* Now verify that champ is better than everything earlier in the
15668 instantiation list. */
15669 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
15670 if (get_bindings (TREE_VALUE (champ
),
15671 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
15672 NULL_TREE
, /*check_ret=*/false)
15673 || !get_bindings (TREE_VALUE (fn
),
15674 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
15675 NULL_TREE
, /*check_ret=*/false))
15681 processing_template_decl
--;
15684 return error_mark_node
;
15689 /* If DECL is a specialization of some template, return the most
15690 general such template. Otherwise, returns NULL_TREE.
15692 For example, given:
15694 template <class T> struct S { template <class U> void f(U); };
15696 if TMPL is `template <class U> void S<int>::f(U)' this will return
15697 the full template. This function will not trace past partial
15698 specializations, however. For example, given in addition:
15700 template <class T> struct S<T*> { template <class U> void f(U); };
15702 if TMPL is `template <class U> void S<int*>::f(U)' this will return
15703 `template <class T> template <class U> S<T*>::f(U)'. */
15706 most_general_template (tree decl
)
15708 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
15709 an immediate specialization. */
15710 if (TREE_CODE (decl
) == FUNCTION_DECL
)
15712 if (DECL_TEMPLATE_INFO (decl
)) {
15713 decl
= DECL_TI_TEMPLATE (decl
);
15715 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
15716 template friend. */
15717 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
15723 /* Look for more and more general templates. */
15724 while (DECL_TEMPLATE_INFO (decl
))
15726 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
15727 (See cp-tree.h for details.) */
15728 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
15731 if (CLASS_TYPE_P (TREE_TYPE (decl
))
15732 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
15735 /* Stop if we run into an explicitly specialized class template. */
15736 if (!DECL_NAMESPACE_SCOPE_P (decl
)
15737 && DECL_CONTEXT (decl
)
15738 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
15741 decl
= DECL_TI_TEMPLATE (decl
);
15747 /* Return the most specialized of the class template partial
15748 specializations of TMPL which can produce TYPE, a specialization of
15749 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
15750 a _TYPE node corresponding to the partial specialization, while the
15751 TREE_PURPOSE is the set of template arguments that must be
15752 substituted into the TREE_TYPE in order to generate TYPE.
15754 If the choice of partial specialization is ambiguous, a diagnostic
15755 is issued, and the error_mark_node is returned. If there are no
15756 partial specializations of TMPL matching TYPE, then NULL_TREE is
15760 most_specialized_class (tree type
, tree tmpl
)
15762 tree list
= NULL_TREE
;
15768 tree outer_args
= NULL_TREE
;
15770 tmpl
= most_general_template (tmpl
);
15771 args
= CLASSTYPE_TI_ARGS (type
);
15773 /* For determining which partial specialization to use, only the
15774 innermost args are interesting. */
15775 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
15777 outer_args
= strip_innermost_template_args (args
, 1);
15778 args
= INNERMOST_TEMPLATE_ARGS (args
);
15781 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
15783 tree partial_spec_args
;
15785 tree parms
= TREE_VALUE (t
);
15787 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
15792 ++processing_template_decl
;
15794 /* Discard the outer levels of args, and then substitute in the
15795 template args from the enclosing class. */
15796 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
15797 partial_spec_args
= tsubst_template_args
15798 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
15800 /* PARMS already refers to just the innermost parms, but the
15801 template parms in partial_spec_args had their levels lowered
15802 by tsubst, so we need to do the same for the parm list. We
15803 can't just tsubst the TREE_VEC itself, as tsubst wants to
15804 treat a TREE_VEC as an argument vector. */
15805 parms
= copy_node (parms
);
15806 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
15807 TREE_VEC_ELT (parms
, i
) =
15808 tsubst (TREE_VEC_ELT (parms
, i
), outer_args
, tf_none
, NULL_TREE
);
15810 --processing_template_decl
;
15812 spec_args
= get_class_bindings (parms
,
15818 spec_args
= add_to_template_args (outer_args
, spec_args
);
15819 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
15820 TREE_TYPE (list
) = TREE_TYPE (t
);
15827 ambiguous_p
= false;
15830 t
= TREE_CHAIN (t
);
15831 for (; t
; t
= TREE_CHAIN (t
))
15833 fate
= more_specialized_class (champ
, t
);
15840 t
= TREE_CHAIN (t
);
15843 ambiguous_p
= true;
15852 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
15854 fate
= more_specialized_class (champ
, t
);
15857 ambiguous_p
= true;
15865 char *spaces
= NULL
;
15866 error ("ambiguous class template instantiation for %q#T", type
);
15867 str
= TREE_CHAIN (list
) ? _("candidates are:") : _("candidate is:");
15868 for (t
= list
; t
; t
= TREE_CHAIN (t
))
15870 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
15871 spaces
= spaces
? spaces
: get_spaces (str
);
15874 return error_mark_node
;
15880 /* Explicitly instantiate DECL. */
15883 do_decl_instantiation (tree decl
, tree storage
)
15885 tree result
= NULL_TREE
;
15888 if (!decl
|| decl
== error_mark_node
)
15889 /* An error occurred, for which grokdeclarator has already issued
15890 an appropriate message. */
15892 else if (! DECL_LANG_SPECIFIC (decl
))
15894 error ("explicit instantiation of non-template %q#D", decl
);
15897 else if (TREE_CODE (decl
) == VAR_DECL
)
15899 /* There is an asymmetry here in the way VAR_DECLs and
15900 FUNCTION_DECLs are handled by grokdeclarator. In the case of
15901 the latter, the DECL we get back will be marked as a
15902 template instantiation, and the appropriate
15903 DECL_TEMPLATE_INFO will be set up. This does not happen for
15904 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
15905 should handle VAR_DECLs as it currently handles
15907 if (!DECL_CLASS_SCOPE_P (decl
))
15909 error ("%qD is not a static data member of a class template", decl
);
15912 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
15913 if (!result
|| TREE_CODE (result
) != VAR_DECL
)
15915 error ("no matching template for %qD found", decl
);
15918 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
15920 error ("type %qT for explicit instantiation %qD does not match "
15921 "declared type %qT", TREE_TYPE (result
), decl
,
15926 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
15928 error ("explicit instantiation of %q#D", decl
);
15934 /* Check for various error cases. Note that if the explicit
15935 instantiation is valid the RESULT will currently be marked as an
15936 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
15937 until we get here. */
15939 if (DECL_TEMPLATE_SPECIALIZATION (result
))
15941 /* DR 259 [temp.spec].
15943 Both an explicit instantiation and a declaration of an explicit
15944 specialization shall not appear in a program unless the explicit
15945 instantiation follows a declaration of the explicit specialization.
15947 For a given set of template parameters, if an explicit
15948 instantiation of a template appears after a declaration of an
15949 explicit specialization for that template, the explicit
15950 instantiation has no effect. */
15953 else if (DECL_EXPLICIT_INSTANTIATION (result
))
15957 No program shall explicitly instantiate any template more
15960 We check DECL_NOT_REALLY_EXTERN so as not to complain when
15961 the first instantiation was `extern' and the second is not,
15962 and EXTERN_P for the opposite case. */
15963 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
15964 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
15965 /* If an "extern" explicit instantiation follows an ordinary
15966 explicit instantiation, the template is instantiated. */
15970 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
15972 error ("no matching template for %qD found", result
);
15975 else if (!DECL_TEMPLATE_INFO (result
))
15977 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
15981 if (storage
== NULL_TREE
)
15983 else if (storage
== ridpointers
[(int) RID_EXTERN
])
15985 if (!in_system_header
&& (cxx_dialect
== cxx98
))
15986 pedwarn (input_location
, OPT_pedantic
,
15987 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
15992 error ("storage class %qD applied to template instantiation", storage
);
15994 check_explicit_instantiation_namespace (result
);
15995 mark_decl_instantiated (result
, extern_p
);
15997 instantiate_decl (result
, /*defer_ok=*/1,
15998 /*expl_inst_class_mem_p=*/false);
16002 mark_class_instantiated (tree t
, int extern_p
)
16004 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
16005 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
16006 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
16007 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
16010 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
16011 rest_of_type_compilation (t
, 1);
16015 /* Called from do_type_instantiation through binding_table_foreach to
16016 do recursive instantiation for the type bound in ENTRY. */
16018 bt_instantiate_type_proc (binding_entry entry
, void *data
)
16020 tree storage
= *(tree
*) data
;
16022 if (MAYBE_CLASS_TYPE_P (entry
->type
)
16023 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
16024 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
16027 /* Called from do_type_instantiation to instantiate a member
16028 (a member function or a static member variable) of an
16029 explicitly instantiated class template. */
16031 instantiate_class_member (tree decl
, int extern_p
)
16033 mark_decl_instantiated (decl
, extern_p
);
16035 instantiate_decl (decl
, /*defer_ok=*/1,
16036 /*expl_inst_class_mem_p=*/true);
16039 /* Perform an explicit instantiation of template class T. STORAGE, if
16040 non-null, is the RID for extern, inline or static. COMPLAIN is
16041 nonzero if this is called from the parser, zero if called recursively,
16042 since the standard is unclear (as detailed below). */
16045 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
16050 int previous_instantiation_extern_p
= 0;
16052 if (TREE_CODE (t
) == TYPE_DECL
)
16055 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
16057 error ("explicit instantiation of non-template type %qT", t
);
16063 if (!COMPLETE_TYPE_P (t
))
16065 if (complain
& tf_error
)
16066 error ("explicit instantiation of %q#T before definition of template",
16071 if (storage
!= NULL_TREE
)
16073 if (!in_system_header
)
16075 if (storage
== ridpointers
[(int) RID_EXTERN
])
16077 if (cxx_dialect
== cxx98
)
16078 pedwarn (input_location
, OPT_pedantic
,
16079 "ISO C++ 1998 forbids the use of %<extern%> on "
16080 "explicit instantiations");
16083 pedwarn (input_location
, OPT_pedantic
,
16084 "ISO C++ forbids the use of %qE"
16085 " on explicit instantiations", storage
);
16088 if (storage
== ridpointers
[(int) RID_INLINE
])
16090 else if (storage
== ridpointers
[(int) RID_EXTERN
])
16092 else if (storage
== ridpointers
[(int) RID_STATIC
])
16096 error ("storage class %qD applied to template instantiation",
16102 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
16104 /* DR 259 [temp.spec].
16106 Both an explicit instantiation and a declaration of an explicit
16107 specialization shall not appear in a program unless the explicit
16108 instantiation follows a declaration of the explicit specialization.
16110 For a given set of template parameters, if an explicit
16111 instantiation of a template appears after a declaration of an
16112 explicit specialization for that template, the explicit
16113 instantiation has no effect. */
16116 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
16120 No program shall explicitly instantiate any template more
16123 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
16124 instantiation was `extern'. If EXTERN_P then the second is.
16125 These cases are OK. */
16126 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
16128 if (!previous_instantiation_extern_p
&& !extern_p
16129 && (complain
& tf_error
))
16130 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
16132 /* If we've already instantiated the template, just return now. */
16133 if (!CLASSTYPE_INTERFACE_ONLY (t
))
16137 check_explicit_instantiation_namespace (TYPE_NAME (t
));
16138 mark_class_instantiated (t
, extern_p
);
16146 /* In contrast to implicit instantiation, where only the
16147 declarations, and not the definitions, of members are
16148 instantiated, we have here:
16152 The explicit instantiation of a class template specialization
16153 implies the instantiation of all of its members not
16154 previously explicitly specialized in the translation unit
16155 containing the explicit instantiation.
16157 Of course, we can't instantiate member template classes, since
16158 we don't have any arguments for them. Note that the standard
16159 is unclear on whether the instantiation of the members are
16160 *explicit* instantiations or not. However, the most natural
16161 interpretation is that it should be an explicit instantiation. */
16164 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
16165 if (TREE_CODE (tmp
) == FUNCTION_DECL
16166 && DECL_TEMPLATE_INSTANTIATION (tmp
))
16167 instantiate_class_member (tmp
, extern_p
);
16169 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
16170 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
16171 instantiate_class_member (tmp
, extern_p
);
16173 if (CLASSTYPE_NESTED_UTDS (t
))
16174 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
16175 bt_instantiate_type_proc
, &storage
);
16179 /* Given a function DECL, which is a specialization of TMPL, modify
16180 DECL to be a re-instantiation of TMPL with the same template
16181 arguments. TMPL should be the template into which tsubst'ing
16182 should occur for DECL, not the most general template.
16184 One reason for doing this is a scenario like this:
16187 void f(const T&, int i);
16189 void g() { f(3, 7); }
16192 void f(const T& t, const int i) { }
16194 Note that when the template is first instantiated, with
16195 instantiate_template, the resulting DECL will have no name for the
16196 first parameter, and the wrong type for the second. So, when we go
16197 to instantiate the DECL, we regenerate it. */
16200 regenerate_decl_from_template (tree decl
, tree tmpl
)
16202 /* The arguments used to instantiate DECL, from the most general
16207 args
= DECL_TI_ARGS (decl
);
16208 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
16210 /* Make sure that we can see identifiers, and compute access
16212 push_access_scope (decl
);
16214 if (TREE_CODE (decl
) == FUNCTION_DECL
)
16222 args_depth
= TMPL_ARGS_DEPTH (args
);
16223 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
16224 if (args_depth
> parms_depth
)
16225 args
= get_innermost_template_args (args
, parms_depth
);
16227 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
16228 args
, tf_error
, NULL_TREE
);
16230 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
16233 /* Merge parameter declarations. */
16234 decl_parm
= skip_artificial_parms_for (decl
,
16235 DECL_ARGUMENTS (decl
));
16237 = skip_artificial_parms_for (code_pattern
,
16238 DECL_ARGUMENTS (code_pattern
));
16239 while (decl_parm
&& !FUNCTION_PARAMETER_PACK_P (pattern_parm
))
16244 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
16245 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
16246 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
16248 parm_type
= type_decays_to (parm_type
);
16249 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
16250 TREE_TYPE (decl_parm
) = parm_type
;
16251 attributes
= DECL_ATTRIBUTES (pattern_parm
);
16252 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
16254 DECL_ATTRIBUTES (decl_parm
) = attributes
;
16255 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
16257 decl_parm
= TREE_CHAIN (decl_parm
);
16258 pattern_parm
= TREE_CHAIN (pattern_parm
);
16260 /* Merge any parameters that match with the function parameter
16262 if (pattern_parm
&& FUNCTION_PARAMETER_PACK_P (pattern_parm
))
16265 tree expanded_types
;
16266 /* Expand the TYPE_PACK_EXPANSION that provides the types for
16267 the parameters in this function parameter pack. */
16268 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
16269 args
, tf_error
, NULL_TREE
);
16270 len
= TREE_VEC_LENGTH (expanded_types
);
16271 for (i
= 0; i
< len
; i
++)
16276 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
16277 /* Rename the parameter to include the index. */
16278 DECL_NAME (decl_parm
) =
16279 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
16280 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
16281 parm_type
= type_decays_to (parm_type
);
16282 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
16283 TREE_TYPE (decl_parm
) = parm_type
;
16284 attributes
= DECL_ATTRIBUTES (pattern_parm
);
16285 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
16287 DECL_ATTRIBUTES (decl_parm
) = attributes
;
16288 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
16290 decl_parm
= TREE_CHAIN (decl_parm
);
16293 /* Merge additional specifiers from the CODE_PATTERN. */
16294 if (DECL_DECLARED_INLINE_P (code_pattern
)
16295 && !DECL_DECLARED_INLINE_P (decl
))
16296 DECL_DECLARED_INLINE_P (decl
) = 1;
16298 else if (TREE_CODE (decl
) == VAR_DECL
)
16299 DECL_INITIAL (decl
) =
16300 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
16301 tf_error
, DECL_TI_TEMPLATE (decl
),
16302 /*integral_constant_expression_p=*/false);
16304 gcc_unreachable ();
16306 pop_access_scope (decl
);
16309 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
16310 substituted to get DECL. */
16313 template_for_substitution (tree decl
)
16315 tree tmpl
= DECL_TI_TEMPLATE (decl
);
16317 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
16318 for the instantiation. This is not always the most general
16319 template. Consider, for example:
16322 struct S { template <class U> void f();
16323 template <> void f<int>(); };
16325 and an instantiation of S<double>::f<int>. We want TD to be the
16326 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
16327 while (/* An instantiation cannot have a definition, so we need a
16328 more general template. */
16329 DECL_TEMPLATE_INSTANTIATION (tmpl
)
16330 /* We must also deal with friend templates. Given:
16332 template <class T> struct S {
16333 template <class U> friend void f() {};
16336 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
16337 so far as the language is concerned, but that's still
16338 where we get the pattern for the instantiation from. On
16339 other hand, if the definition comes outside the class, say:
16341 template <class T> struct S {
16342 template <class U> friend void f();
16344 template <class U> friend void f() {}
16346 we don't need to look any further. That's what the check for
16347 DECL_INITIAL is for. */
16348 || (TREE_CODE (decl
) == FUNCTION_DECL
16349 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
16350 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
16352 /* The present template, TD, should not be a definition. If it
16353 were a definition, we should be using it! Note that we
16354 cannot restructure the loop to just keep going until we find
16355 a template with a definition, since that might go too far if
16356 a specialization was declared, but not defined. */
16357 gcc_assert (TREE_CODE (decl
) != VAR_DECL
16358 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
16360 /* Fetch the more general template. */
16361 tmpl
= DECL_TI_TEMPLATE (tmpl
);
16367 /* Returns true if we need to instantiate this template instance even if we
16368 know we aren't going to emit it.. */
16371 always_instantiate_p (tree decl
)
16373 /* We always instantiate inline functions so that we can inline them. An
16374 explicit instantiation declaration prohibits implicit instantiation of
16375 non-inline functions. With high levels of optimization, we would
16376 normally inline non-inline functions -- but we're not allowed to do
16377 that for "extern template" functions. Therefore, we check
16378 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
16379 return ((TREE_CODE (decl
) == FUNCTION_DECL
16380 && DECL_DECLARED_INLINE_P (decl
))
16381 /* And we need to instantiate static data members so that
16382 their initializers are available in integral constant
16384 || (TREE_CODE (decl
) == VAR_DECL
16385 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
)));
16388 /* Produce the definition of D, a _DECL generated from a template. If
16389 DEFER_OK is nonzero, then we don't have to actually do the
16390 instantiation now; we just have to do it sometime. Normally it is
16391 an error if this is an explicit instantiation but D is undefined.
16392 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
16393 explicitly instantiated class template. */
16396 instantiate_decl (tree d
, int defer_ok
,
16397 bool expl_inst_class_mem_p
)
16399 tree tmpl
= DECL_TI_TEMPLATE (d
);
16406 bool pattern_defined
;
16408 location_t saved_loc
= input_location
;
16411 /* This function should only be used to instantiate templates for
16412 functions and static member variables. */
16413 gcc_assert (TREE_CODE (d
) == FUNCTION_DECL
16414 || TREE_CODE (d
) == VAR_DECL
);
16416 /* Variables are never deferred; if instantiation is required, they
16417 are instantiated right away. That allows for better code in the
16418 case that an expression refers to the value of the variable --
16419 if the variable has a constant value the referring expression can
16420 take advantage of that fact. */
16421 if (TREE_CODE (d
) == VAR_DECL
)
16424 /* Don't instantiate cloned functions. Instead, instantiate the
16425 functions they cloned. */
16426 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
16427 d
= DECL_CLONED_FUNCTION (d
);
16429 if (DECL_TEMPLATE_INSTANTIATED (d
)
16430 || DECL_TEMPLATE_SPECIALIZATION (d
))
16431 /* D has already been instantiated or explicitly specialized, so
16432 there's nothing for us to do here.
16434 It might seem reasonable to check whether or not D is an explicit
16435 instantiation, and, if so, stop here. But when an explicit
16436 instantiation is deferred until the end of the compilation,
16437 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
16438 the instantiation. */
16441 /* Check to see whether we know that this template will be
16442 instantiated in some other file, as with "extern template"
16444 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
16446 /* In general, we do not instantiate such templates. */
16447 if (external_p
&& !always_instantiate_p (d
))
16450 gen_tmpl
= most_general_template (tmpl
);
16451 gen_args
= DECL_TI_ARGS (d
);
16453 if (tmpl
!= gen_tmpl
)
16454 /* We should already have the extra args. */
16455 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
16456 == TMPL_ARGS_DEPTH (gen_args
));
16457 /* And what's in the hash table should match D. */
16458 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
16459 || spec
== NULL_TREE
);
16461 /* This needs to happen before any tsubsting. */
16462 if (! push_tinst_level (d
))
16465 timevar_push (TV_PARSE
);
16467 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
16468 for the instantiation. */
16469 td
= template_for_substitution (d
);
16470 code_pattern
= DECL_TEMPLATE_RESULT (td
);
16472 /* We should never be trying to instantiate a member of a class
16473 template or partial specialization. */
16474 gcc_assert (d
!= code_pattern
);
16476 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
16477 || DECL_TEMPLATE_SPECIALIZATION (td
))
16478 /* In the case of a friend template whose definition is provided
16479 outside the class, we may have too many arguments. Drop the
16480 ones we don't need. The same is true for specializations. */
16481 args
= get_innermost_template_args
16482 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
16486 if (TREE_CODE (d
) == FUNCTION_DECL
)
16487 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
);
16489 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
16491 /* We may be in the middle of deferred access check. Disable it now. */
16492 push_deferring_access_checks (dk_no_deferred
);
16494 /* Unless an explicit instantiation directive has already determined
16495 the linkage of D, remember that a definition is available for
16497 if (pattern_defined
16498 && !DECL_INTERFACE_KNOWN (d
)
16499 && !DECL_NOT_REALLY_EXTERN (d
))
16500 mark_definable (d
);
16502 input_location
= DECL_SOURCE_LOCATION (d
);
16504 /* If D is a member of an explicitly instantiated class template,
16505 and no definition is available, treat it like an implicit
16507 if (!pattern_defined
&& expl_inst_class_mem_p
16508 && DECL_EXPLICIT_INSTANTIATION (d
))
16510 DECL_NOT_REALLY_EXTERN (d
) = 0;
16511 DECL_INTERFACE_KNOWN (d
) = 0;
16512 SET_DECL_IMPLICIT_INSTANTIATION (d
);
16515 /* Recheck the substitutions to obtain any warning messages
16516 about ignoring cv qualifiers. Don't do this for artificial decls,
16517 as it breaks the context-sensitive substitution for lambda op(). */
16518 if (!defer_ok
&& !DECL_ARTIFICIAL (d
))
16520 tree gen
= DECL_TEMPLATE_RESULT (gen_tmpl
);
16521 tree type
= TREE_TYPE (gen
);
16523 /* Make sure that we can see identifiers, and compute access
16524 correctly. D is already the target FUNCTION_DECL with the
16526 push_access_scope (d
);
16528 if (TREE_CODE (gen
) == FUNCTION_DECL
)
16530 tsubst (DECL_ARGUMENTS (gen
), gen_args
, tf_warning_or_error
, d
);
16531 tsubst_exception_specification (type
, gen_args
, tf_warning_or_error
,
16533 /* Don't simply tsubst the function type, as that will give
16534 duplicate warnings about poor parameter qualifications.
16535 The function arguments are the same as the decl_arguments
16536 without the top level cv qualifiers. */
16537 type
= TREE_TYPE (type
);
16539 tsubst (type
, gen_args
, tf_warning_or_error
, d
);
16541 pop_access_scope (d
);
16544 /* Defer all other templates, unless we have been explicitly
16545 forbidden from doing so. */
16546 if (/* If there is no definition, we cannot instantiate the
16549 /* If it's OK to postpone instantiation, do so. */
16551 /* If this is a static data member that will be defined
16552 elsewhere, we don't want to instantiate the entire data
16553 member, but we do want to instantiate the initializer so that
16554 we can substitute that elsewhere. */
16555 || (external_p
&& TREE_CODE (d
) == VAR_DECL
))
16557 /* The definition of the static data member is now required so
16558 we must substitute the initializer. */
16559 if (TREE_CODE (d
) == VAR_DECL
16560 && !DECL_INITIAL (d
)
16561 && DECL_INITIAL (code_pattern
))
16566 ns
= decl_namespace_context (d
);
16567 push_nested_namespace (ns
);
16568 push_nested_class (DECL_CONTEXT (d
));
16569 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
16571 tf_warning_or_error
, NULL_TREE
,
16572 /*integral_constant_expression_p=*/false);
16573 cp_finish_decl (d
, init
, /*init_const_expr_p=*/false,
16574 /*asmspec_tree=*/NULL_TREE
,
16575 LOOKUP_ONLYCONVERTING
);
16576 pop_nested_class ();
16577 pop_nested_namespace (ns
);
16580 /* We restore the source position here because it's used by
16581 add_pending_template. */
16582 input_location
= saved_loc
;
16584 if (at_eof
&& !pattern_defined
16585 && DECL_EXPLICIT_INSTANTIATION (d
)
16586 && DECL_NOT_REALLY_EXTERN (d
))
16589 The definition of a non-exported function template, a
16590 non-exported member function template, or a non-exported
16591 member function or static data member of a class template
16592 shall be present in every translation unit in which it is
16593 explicitly instantiated. */
16594 permerror (input_location
, "explicit instantiation of %qD "
16595 "but no definition available", d
);
16597 /* ??? Historically, we have instantiated inline functions, even
16598 when marked as "extern template". */
16599 if (!(external_p
&& TREE_CODE (d
) == VAR_DECL
))
16600 add_pending_template (d
);
16603 /* Tell the repository that D is available in this translation unit
16604 -- and see if it is supposed to be instantiated here. */
16605 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
16607 /* In a PCH file, despite the fact that the repository hasn't
16608 requested instantiation in the PCH it is still possible that
16609 an instantiation will be required in a file that includes the
16612 add_pending_template (d
);
16613 /* Instantiate inline functions so that the inliner can do its
16614 job, even though we'll not be emitting a copy of this
16616 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
16620 need_push
= !cfun
|| !global_bindings_p ();
16622 push_to_top_level ();
16624 /* Mark D as instantiated so that recursive calls to
16625 instantiate_decl do not try to instantiate it again. */
16626 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
16628 /* Regenerate the declaration in case the template has been modified
16629 by a subsequent redeclaration. */
16630 regenerate_decl_from_template (d
, td
);
16632 /* We already set the file and line above. Reset them now in case
16633 they changed as a result of calling regenerate_decl_from_template. */
16634 input_location
= DECL_SOURCE_LOCATION (d
);
16636 if (TREE_CODE (d
) == VAR_DECL
)
16640 /* Clear out DECL_RTL; whatever was there before may not be right
16641 since we've reset the type of the declaration. */
16642 SET_DECL_RTL (d
, NULL_RTX
);
16643 DECL_IN_AGGR_P (d
) = 0;
16645 /* The initializer is placed in DECL_INITIAL by
16646 regenerate_decl_from_template. Pull it out so that
16647 cp_finish_decl can process it. */
16648 init
= DECL_INITIAL (d
);
16649 DECL_INITIAL (d
) = NULL_TREE
;
16650 DECL_INITIALIZED_P (d
) = 0;
16652 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
16653 initializer. That function will defer actual emission until
16654 we have a chance to determine linkage. */
16655 DECL_EXTERNAL (d
) = 0;
16657 /* Enter the scope of D so that access-checking works correctly. */
16658 push_nested_class (DECL_CONTEXT (d
));
16659 cp_finish_decl (d
, init
, false, NULL_TREE
, 0);
16660 pop_nested_class ();
16662 else if (TREE_CODE (d
) == FUNCTION_DECL
)
16664 htab_t saved_local_specializations
;
16669 /* Save away the current list, in case we are instantiating one
16670 template from within the body of another. */
16671 saved_local_specializations
= local_specializations
;
16673 /* Set up the list of local specializations. */
16674 local_specializations
= htab_create (37,
16675 hash_local_specialization
,
16676 eq_local_specializations
,
16679 /* Set up context. */
16680 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
16682 /* Create substitution entries for the parameters. */
16683 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
16684 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
16685 spec_parm
= DECL_ARGUMENTS (d
);
16686 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
16688 register_local_specialization (spec_parm
, tmpl_parm
);
16689 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
16690 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
16692 while (tmpl_parm
&& !FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
16694 register_local_specialization (spec_parm
, tmpl_parm
);
16695 tmpl_parm
= TREE_CHAIN (tmpl_parm
);
16696 spec_parm
= TREE_CHAIN (spec_parm
);
16698 if (tmpl_parm
&& FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
16700 /* Register the (value) argument pack as a specialization of
16701 TMPL_PARM, then move on. */
16702 tree argpack
= make_fnparm_pack (spec_parm
);
16703 register_local_specialization (argpack
, tmpl_parm
);
16704 tmpl_parm
= TREE_CHAIN (tmpl_parm
);
16705 spec_parm
= NULL_TREE
;
16707 gcc_assert (!spec_parm
);
16709 /* Substitute into the body of the function. */
16710 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
16711 tf_warning_or_error
, tmpl
,
16712 /*integral_constant_expression_p=*/false);
16714 /* Set the current input_location to the end of the function
16715 so that finish_function knows where we are. */
16716 input_location
= DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
16718 /* We don't need the local specializations any more. */
16719 htab_delete (local_specializations
);
16720 local_specializations
= saved_local_specializations
;
16722 /* Finish the function. */
16723 d
= finish_function (0);
16724 expand_or_defer_fn (d
);
16727 /* We're not deferring instantiation any more. */
16728 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
16731 pop_from_top_level ();
16734 input_location
= saved_loc
;
16735 pop_deferring_access_checks ();
16736 pop_tinst_level ();
16738 timevar_pop (TV_PARSE
);
16743 /* Run through the list of templates that we wish we could
16744 instantiate, and instantiate any we can. RETRIES is the
16745 number of times we retry pending template instantiation. */
16748 instantiate_pending_templates (int retries
)
16751 location_t saved_loc
= input_location
;
16753 /* Instantiating templates may trigger vtable generation. This in turn
16754 may require further template instantiations. We place a limit here
16755 to avoid infinite loop. */
16756 if (pending_templates
&& retries
>= max_tinst_depth
)
16758 tree decl
= pending_templates
->tinst
->decl
;
16760 error ("template instantiation depth exceeds maximum of %d"
16761 " instantiating %q+D, possibly from virtual table generation"
16762 " (use -ftemplate-depth-NN to increase the maximum)",
16763 max_tinst_depth
, decl
);
16764 if (TREE_CODE (decl
) == FUNCTION_DECL
)
16765 /* Pretend that we defined it. */
16766 DECL_INITIAL (decl
) = error_mark_node
;
16772 struct pending_template
**t
= &pending_templates
;
16773 struct pending_template
*last
= NULL
;
16777 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
16778 bool complete
= false;
16780 if (TYPE_P (instantiation
))
16784 if (!COMPLETE_TYPE_P (instantiation
))
16786 instantiate_class_template (instantiation
);
16787 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
16788 for (fn
= TYPE_METHODS (instantiation
);
16790 fn
= TREE_CHAIN (fn
))
16791 if (! DECL_ARTIFICIAL (fn
))
16792 instantiate_decl (fn
,
16794 /*expl_inst_class_mem_p=*/false);
16795 if (COMPLETE_TYPE_P (instantiation
))
16799 complete
= COMPLETE_TYPE_P (instantiation
);
16803 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
16804 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
16807 = instantiate_decl (instantiation
,
16809 /*expl_inst_class_mem_p=*/false);
16810 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
16814 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
16815 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
16819 /* If INSTANTIATION has been instantiated, then we don't
16820 need to consider it again in the future. */
16828 current_tinst_level
= NULL
;
16830 last_pending_template
= last
;
16832 while (reconsider
);
16834 input_location
= saved_loc
;
16837 /* Substitute ARGVEC into T, which is a list of initializers for
16838 either base class or a non-static data member. The TREE_PURPOSEs
16839 are DECLs, and the TREE_VALUEs are the initializer values. Used by
16840 instantiate_decl. */
16843 tsubst_initializer_list (tree t
, tree argvec
)
16845 tree inits
= NULL_TREE
;
16847 for (; t
; t
= TREE_CHAIN (t
))
16851 tree expanded_bases
= NULL_TREE
;
16852 tree expanded_arguments
= NULL_TREE
;
16855 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
16860 /* Expand the base class expansion type into separate base
16862 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
16863 tf_warning_or_error
,
16865 if (expanded_bases
== error_mark_node
)
16868 /* We'll be building separate TREE_LISTs of arguments for
16870 len
= TREE_VEC_LENGTH (expanded_bases
);
16871 expanded_arguments
= make_tree_vec (len
);
16872 for (i
= 0; i
< len
; i
++)
16873 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
16875 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
16876 expand each argument in the TREE_VALUE of t. */
16877 expr
= make_node (EXPR_PACK_EXPANSION
);
16878 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
16879 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
16881 if (TREE_VALUE (t
) == void_type_node
)
16882 /* VOID_TYPE_NODE is used to indicate
16883 value-initialization. */
16885 for (i
= 0; i
< len
; i
++)
16886 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
16890 /* Substitute parameter packs into each argument in the
16892 in_base_initializer
= 1;
16893 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
16895 tree expanded_exprs
;
16897 /* Expand the argument. */
16898 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
16900 = tsubst_pack_expansion (expr
, argvec
,
16901 tf_warning_or_error
,
16903 if (expanded_exprs
== error_mark_node
)
16906 /* Prepend each of the expanded expressions to the
16907 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
16908 for (i
= 0; i
< len
; i
++)
16910 TREE_VEC_ELT (expanded_arguments
, i
) =
16911 tree_cons (NULL_TREE
,
16912 TREE_VEC_ELT (expanded_exprs
, i
),
16913 TREE_VEC_ELT (expanded_arguments
, i
));
16916 in_base_initializer
= 0;
16918 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
16919 since we built them backwards. */
16920 for (i
= 0; i
< len
; i
++)
16922 TREE_VEC_ELT (expanded_arguments
, i
) =
16923 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
16928 for (i
= 0; i
< len
; ++i
)
16930 if (expanded_bases
)
16932 decl
= TREE_VEC_ELT (expanded_bases
, i
);
16933 decl
= expand_member_init (decl
);
16934 init
= TREE_VEC_ELT (expanded_arguments
, i
);
16938 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
16939 tf_warning_or_error
, NULL_TREE
);
16941 decl
= expand_member_init (decl
);
16942 if (decl
&& !DECL_P (decl
))
16943 in_base_initializer
= 1;
16945 init
= tsubst_expr (TREE_VALUE (t
), argvec
,
16946 tf_warning_or_error
, NULL_TREE
,
16947 /*integral_constant_expression_p=*/false);
16948 in_base_initializer
= 0;
16953 init
= build_tree_list (decl
, init
);
16954 TREE_CHAIN (init
) = inits
;
16962 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
16965 set_current_access_from_decl (tree decl
)
16967 if (TREE_PRIVATE (decl
))
16968 current_access_specifier
= access_private_node
;
16969 else if (TREE_PROTECTED (decl
))
16970 current_access_specifier
= access_protected_node
;
16972 current_access_specifier
= access_public_node
;
16975 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
16976 is the instantiation (which should have been created with
16977 start_enum) and ARGS are the template arguments to use. */
16980 tsubst_enum (tree tag
, tree newtag
, tree args
)
16984 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
16989 decl
= TREE_VALUE (e
);
16990 /* Note that in a template enum, the TREE_VALUE is the
16991 CONST_DECL, not the corresponding INTEGER_CST. */
16992 value
= tsubst_expr (DECL_INITIAL (decl
),
16993 args
, tf_warning_or_error
, NULL_TREE
,
16994 /*integral_constant_expression_p=*/true);
16996 /* Give this enumeration constant the correct access. */
16997 set_current_access_from_decl (decl
);
16999 /* Actually build the enumerator itself. */
17000 build_enumerator (DECL_NAME (decl
), value
, newtag
);
17003 finish_enum (newtag
);
17004 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
17005 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
17008 /* DECL is a FUNCTION_DECL that is a template specialization. Return
17009 its type -- but without substituting the innermost set of template
17010 arguments. So, innermost set of template parameters will appear in
17014 get_mostly_instantiated_function_type (tree decl
)
17022 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
17023 targs
= DECL_TI_ARGS (decl
);
17024 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
17025 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
17027 /* There should be as many levels of arguments as there are levels
17029 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
17031 fn_type
= TREE_TYPE (tmpl
);
17033 if (parm_depth
== 1)
17034 /* No substitution is necessary. */
17038 int i
, save_access_control
;
17041 /* Replace the innermost level of the TARGS with NULL_TREEs to
17042 let tsubst know not to substitute for those parameters. */
17043 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
17044 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
17045 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
17046 TMPL_ARGS_LEVEL (targs
, i
));
17047 SET_TMPL_ARGS_LEVEL (partial_args
,
17048 TMPL_ARGS_DEPTH (targs
),
17049 make_tree_vec (DECL_NTPARMS (tmpl
)));
17051 /* Disable access control as this function is used only during
17053 save_access_control
= flag_access_control
;
17054 flag_access_control
= 0;
17056 ++processing_template_decl
;
17057 /* Now, do the (partial) substitution to figure out the
17058 appropriate function type. */
17059 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
17060 --processing_template_decl
;
17062 /* Substitute into the template parameters to obtain the real
17063 innermost set of parameters. This step is important if the
17064 innermost set of template parameters contains value
17065 parameters whose types depend on outer template parameters. */
17066 TREE_VEC_LENGTH (partial_args
)--;
17067 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
17069 flag_access_control
= save_access_control
;
17075 /* Return truthvalue if we're processing a template different from
17076 the last one involved in diagnostics. */
17078 problematic_instantiation_changed (void)
17080 return last_template_error_tick
!= tinst_level_tick
;
17083 /* Remember current template involved in diagnostics. */
17085 record_last_problematic_instantiation (void)
17087 last_template_error_tick
= tinst_level_tick
;
17090 struct tinst_level
*
17091 current_instantiation (void)
17093 return current_tinst_level
;
17096 /* [temp.param] Check that template non-type parm TYPE is of an allowable
17097 type. Return zero for ok, nonzero for disallowed. Issue error and
17098 warning messages under control of COMPLAIN. */
17101 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
17103 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
17105 else if (POINTER_TYPE_P (type
))
17107 else if (TYPE_PTR_TO_MEMBER_P (type
))
17109 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
17111 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
17114 if (complain
& tf_error
)
17115 error ("%q#T is not a valid type for a template constant parameter", type
);
17119 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
17120 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
17123 dependent_type_p_r (tree type
)
17129 A type is dependent if it is:
17131 -- a template parameter. Template template parameters are types
17132 for us (since TYPE_P holds true for them) so we handle
17134 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
17135 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
17137 /* -- a qualified-id with a nested-name-specifier which contains a
17138 class-name that names a dependent type or whose unqualified-id
17139 names a dependent type. */
17140 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17142 /* -- a cv-qualified type where the cv-unqualified type is
17144 type
= TYPE_MAIN_VARIANT (type
);
17145 /* -- a compound type constructed from any dependent type. */
17146 if (TYPE_PTR_TO_MEMBER_P (type
))
17147 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
17148 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
17150 else if (TREE_CODE (type
) == POINTER_TYPE
17151 || TREE_CODE (type
) == REFERENCE_TYPE
)
17152 return dependent_type_p (TREE_TYPE (type
));
17153 else if (TREE_CODE (type
) == FUNCTION_TYPE
17154 || TREE_CODE (type
) == METHOD_TYPE
)
17158 if (dependent_type_p (TREE_TYPE (type
)))
17160 for (arg_type
= TYPE_ARG_TYPES (type
);
17162 arg_type
= TREE_CHAIN (arg_type
))
17163 if (dependent_type_p (TREE_VALUE (arg_type
)))
17167 /* -- an array type constructed from any dependent type or whose
17168 size is specified by a constant expression that is
17169 value-dependent. */
17170 if (TREE_CODE (type
) == ARRAY_TYPE
)
17172 if (TYPE_DOMAIN (type
)
17173 && dependent_type_p (TYPE_DOMAIN (type
)))
17175 return dependent_type_p (TREE_TYPE (type
));
17177 else if (TREE_CODE (type
) == INTEGER_TYPE
17178 && !TREE_CONSTANT (TYPE_MAX_VALUE (type
)))
17180 /* If this is the TYPE_DOMAIN of an array type, consider it
17181 dependent. We already checked for value-dependence in
17182 compute_array_index_type. */
17183 return type_dependent_expression_p (TYPE_MAX_VALUE (type
));
17186 /* -- a template-id in which either the template name is a template
17188 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17190 /* ... or any of the template arguments is a dependent type or
17191 an expression that is type-dependent or value-dependent. */
17192 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
17193 && (any_dependent_template_arguments_p
17194 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
17197 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
17198 argument of the `typeof' expression is not type-dependent, then
17199 it should already been have resolved. */
17200 if (TREE_CODE (type
) == TYPEOF_TYPE
17201 || TREE_CODE (type
) == DECLTYPE_TYPE
)
17204 /* A template argument pack is dependent if any of its packed
17206 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
17208 tree args
= ARGUMENT_PACK_ARGS (type
);
17209 int i
, len
= TREE_VEC_LENGTH (args
);
17210 for (i
= 0; i
< len
; ++i
)
17211 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
17215 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
17216 be template parameters. */
17217 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
17220 /* The standard does not specifically mention types that are local
17221 to template functions or local classes, but they should be
17222 considered dependent too. For example:
17224 template <int I> void f() {
17229 The size of `E' cannot be known until the value of `I' has been
17230 determined. Therefore, `E' must be considered dependent. */
17231 scope
= TYPE_CONTEXT (type
);
17232 if (scope
&& TYPE_P (scope
))
17233 return dependent_type_p (scope
);
17234 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
17235 return type_dependent_expression_p (scope
);
17237 /* Other types are non-dependent. */
17241 /* Returns TRUE if TYPE is dependent, in the sense of
17242 [temp.dep.type]. */
17245 dependent_type_p (tree type
)
17247 /* If there are no template parameters in scope, then there can't be
17248 any dependent types. */
17249 if (!processing_template_decl
)
17251 /* If we are not processing a template, then nobody should be
17252 providing us with a dependent type. */
17254 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
17258 /* If the type is NULL, we have not computed a type for the entity
17259 in question; in that case, the type is dependent. */
17263 /* Erroneous types can be considered non-dependent. */
17264 if (type
== error_mark_node
)
17267 /* If we have not already computed the appropriate value for TYPE,
17269 if (!TYPE_DEPENDENT_P_VALID (type
))
17271 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
17272 TYPE_DEPENDENT_P_VALID (type
) = 1;
17275 return TYPE_DEPENDENT_P (type
);
17278 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
17279 lookup. In other words, a dependent type that is not the current
17283 dependent_scope_p (tree scope
)
17285 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
17286 && !currently_open_class (scope
));
17289 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
17292 dependent_scope_ref_p (tree expression
, bool criterion (tree
))
17297 gcc_assert (TREE_CODE (expression
) == SCOPE_REF
);
17299 if (!TYPE_P (TREE_OPERAND (expression
, 0)))
17302 scope
= TREE_OPERAND (expression
, 0);
17303 name
= TREE_OPERAND (expression
, 1);
17307 An id-expression is type-dependent if it contains a
17308 nested-name-specifier that contains a class-name that names a
17310 /* The suggested resolution to Core Issue 224 implies that if the
17311 qualifying type is the current class, then we must peek
17314 && currently_open_class (scope
)
17315 && !criterion (name
))
17317 if (dependent_type_p (scope
))
17323 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
17324 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
17328 value_dependent_expression_p (tree expression
)
17330 if (!processing_template_decl
)
17333 /* A name declared with a dependent type. */
17334 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
17337 switch (TREE_CODE (expression
))
17339 case IDENTIFIER_NODE
:
17340 /* A name that has not been looked up -- must be dependent. */
17343 case TEMPLATE_PARM_INDEX
:
17344 /* A non-type template parm. */
17348 /* A non-type template parm. */
17349 if (DECL_TEMPLATE_PARM_P (expression
))
17351 return value_dependent_expression_p (DECL_INITIAL (expression
));
17354 /* A constant with integral or enumeration type and is initialized
17355 with an expression that is value-dependent. */
17356 if (DECL_INITIAL (expression
)
17357 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression
))
17358 && value_dependent_expression_p (DECL_INITIAL (expression
)))
17362 case DYNAMIC_CAST_EXPR
:
17363 case STATIC_CAST_EXPR
:
17364 case CONST_CAST_EXPR
:
17365 case REINTERPRET_CAST_EXPR
:
17367 /* These expressions are value-dependent if the type to which
17368 the cast occurs is dependent or the expression being casted
17369 is value-dependent. */
17371 tree type
= TREE_TYPE (expression
);
17373 if (dependent_type_p (type
))
17376 /* A functional cast has a list of operands. */
17377 expression
= TREE_OPERAND (expression
, 0);
17380 /* If there are no operands, it must be an expression such
17381 as "int()". This should not happen for aggregate types
17382 because it would form non-constant expressions. */
17383 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
17388 if (TREE_CODE (expression
) == TREE_LIST
)
17389 return any_value_dependent_elements_p (expression
);
17391 return value_dependent_expression_p (expression
);
17396 /* A `sizeof' expression is value-dependent if the operand is
17397 type-dependent or is a pack expansion. */
17398 expression
= TREE_OPERAND (expression
, 0);
17399 if (PACK_EXPANSION_P (expression
))
17401 else if (TYPE_P (expression
))
17402 return dependent_type_p (expression
);
17403 return type_dependent_expression_p (expression
);
17406 return dependent_scope_ref_p (expression
, value_dependent_expression_p
);
17408 case COMPONENT_REF
:
17409 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
17410 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
17413 /* A CALL_EXPR may appear in a constant expression if it is a
17414 call to a builtin function, e.g., __builtin_constant_p. All
17415 such calls are value-dependent. */
17418 case NONTYPE_ARGUMENT_PACK
:
17419 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
17420 is value-dependent. */
17422 tree values
= ARGUMENT_PACK_ARGS (expression
);
17423 int i
, len
= TREE_VEC_LENGTH (values
);
17425 for (i
= 0; i
< len
; ++i
)
17426 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
17434 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
17435 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
17436 || (type2
? dependent_type_p (type2
) : false));
17440 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
17441 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
17444 /* A constant expression is value-dependent if any subexpression is
17445 value-dependent. */
17446 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
17448 case tcc_reference
:
17450 return (value_dependent_expression_p
17451 (TREE_OPERAND (expression
, 0)));
17453 case tcc_comparison
:
17455 return ((value_dependent_expression_p
17456 (TREE_OPERAND (expression
, 0)))
17457 || (value_dependent_expression_p
17458 (TREE_OPERAND (expression
, 1))));
17460 case tcc_expression
:
17464 for (i
= 0; i
< TREE_OPERAND_LENGTH (expression
); ++i
)
17465 /* In some cases, some of the operands may be missing.
17466 (For example, in the case of PREDECREMENT_EXPR, the
17467 amount to increment by may be missing.) That doesn't
17468 make the expression dependent. */
17469 if (TREE_OPERAND (expression
, i
)
17470 && (value_dependent_expression_p
17471 (TREE_OPERAND (expression
, i
))))
17481 /* The expression is not value-dependent. */
17485 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
17486 [temp.dep.expr]. */
17489 type_dependent_expression_p (tree expression
)
17491 if (!processing_template_decl
)
17494 if (expression
== error_mark_node
)
17497 /* An unresolved name is always dependent. */
17498 if (TREE_CODE (expression
) == IDENTIFIER_NODE
17499 || TREE_CODE (expression
) == USING_DECL
)
17502 /* Some expression forms are never type-dependent. */
17503 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
17504 || TREE_CODE (expression
) == SIZEOF_EXPR
17505 || TREE_CODE (expression
) == ALIGNOF_EXPR
17506 || TREE_CODE (expression
) == TRAIT_EXPR
17507 || TREE_CODE (expression
) == TYPEID_EXPR
17508 || TREE_CODE (expression
) == DELETE_EXPR
17509 || TREE_CODE (expression
) == VEC_DELETE_EXPR
17510 || TREE_CODE (expression
) == THROW_EXPR
)
17513 /* The types of these expressions depends only on the type to which
17514 the cast occurs. */
17515 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
17516 || TREE_CODE (expression
) == STATIC_CAST_EXPR
17517 || TREE_CODE (expression
) == CONST_CAST_EXPR
17518 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
17519 || TREE_CODE (expression
) == CAST_EXPR
)
17520 return dependent_type_p (TREE_TYPE (expression
));
17522 /* The types of these expressions depends only on the type created
17523 by the expression. */
17524 if (TREE_CODE (expression
) == NEW_EXPR
17525 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
17527 /* For NEW_EXPR tree nodes created inside a template, either
17528 the object type itself or a TREE_LIST may appear as the
17530 tree type
= TREE_OPERAND (expression
, 1);
17531 if (TREE_CODE (type
) == TREE_LIST
)
17532 /* This is an array type. We need to check array dimensions
17534 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
17535 || value_dependent_expression_p
17536 (TREE_OPERAND (TREE_VALUE (type
), 1));
17538 return dependent_type_p (type
);
17541 if (TREE_CODE (expression
) == SCOPE_REF
17542 && dependent_scope_ref_p (expression
,
17543 type_dependent_expression_p
))
17546 if (TREE_CODE (expression
) == FUNCTION_DECL
17547 && DECL_LANG_SPECIFIC (expression
)
17548 && DECL_TEMPLATE_INFO (expression
)
17549 && (any_dependent_template_arguments_p
17550 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
17553 if (TREE_CODE (expression
) == TEMPLATE_DECL
17554 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
17557 if (TREE_CODE (expression
) == STMT_EXPR
)
17558 expression
= stmt_expr_value_expr (expression
);
17560 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
17565 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
17567 if (type_dependent_expression_p (elt
))
17573 if (TREE_TYPE (expression
) == unknown_type_node
)
17575 if (TREE_CODE (expression
) == ADDR_EXPR
)
17576 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
17577 if (TREE_CODE (expression
) == COMPONENT_REF
17578 || TREE_CODE (expression
) == OFFSET_REF
)
17580 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
17582 expression
= TREE_OPERAND (expression
, 1);
17583 if (TREE_CODE (expression
) == IDENTIFIER_NODE
)
17586 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
17587 if (TREE_CODE (expression
) == SCOPE_REF
)
17590 if (TREE_CODE (expression
) == BASELINK
)
17591 expression
= BASELINK_FUNCTIONS (expression
);
17593 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
17595 if (any_dependent_template_arguments_p
17596 (TREE_OPERAND (expression
, 1)))
17598 expression
= TREE_OPERAND (expression
, 0);
17600 gcc_assert (TREE_CODE (expression
) == OVERLOAD
17601 || TREE_CODE (expression
) == FUNCTION_DECL
);
17605 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
17607 expression
= OVL_NEXT (expression
);
17612 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
17614 return (dependent_type_p (TREE_TYPE (expression
)));
17617 /* Like type_dependent_expression_p, but it also works while not processing
17618 a template definition, i.e. during substitution or mangling. */
17621 type_dependent_expression_p_push (tree expr
)
17624 ++processing_template_decl
;
17625 b
= type_dependent_expression_p (expr
);
17626 --processing_template_decl
;
17630 /* Returns TRUE if ARGS contains a type-dependent expression. */
17633 any_type_dependent_arguments_p (const VEC(tree
,gc
) *args
)
17638 for (i
= 0; VEC_iterate (tree
, args
, i
, arg
); ++i
)
17640 if (type_dependent_expression_p (arg
))
17646 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
17647 expressions) contains any value-dependent expressions. */
17650 any_value_dependent_elements_p (const_tree list
)
17652 for (; list
; list
= TREE_CHAIN (list
))
17653 if (value_dependent_expression_p (TREE_VALUE (list
)))
17659 /* Returns TRUE if the ARG (a template argument) is dependent. */
17662 dependent_template_arg_p (tree arg
)
17664 if (!processing_template_decl
)
17667 if (TREE_CODE (arg
) == TEMPLATE_DECL
17668 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
17669 return dependent_template_p (arg
);
17670 else if (ARGUMENT_PACK_P (arg
))
17672 tree args
= ARGUMENT_PACK_ARGS (arg
);
17673 int i
, len
= TREE_VEC_LENGTH (args
);
17674 for (i
= 0; i
< len
; ++i
)
17676 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
17682 else if (TYPE_P (arg
))
17683 return dependent_type_p (arg
);
17685 return (type_dependent_expression_p (arg
)
17686 || value_dependent_expression_p (arg
));
17689 /* Returns true if ARGS (a collection of template arguments) contains
17690 any types that require structural equality testing. */
17693 any_template_arguments_need_structural_equality_p (tree args
)
17700 if (args
== error_mark_node
)
17703 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
17705 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
17706 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
17708 tree arg
= TREE_VEC_ELT (level
, j
);
17709 tree packed_args
= NULL_TREE
;
17712 if (ARGUMENT_PACK_P (arg
))
17714 /* Look inside the argument pack. */
17715 packed_args
= ARGUMENT_PACK_ARGS (arg
);
17716 len
= TREE_VEC_LENGTH (packed_args
);
17719 for (k
= 0; k
< len
; ++k
)
17722 arg
= TREE_VEC_ELT (packed_args
, k
);
17724 if (error_operand_p (arg
))
17726 else if (TREE_CODE (arg
) == TEMPLATE_DECL
17727 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
17729 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
17731 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
17732 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
17741 /* Returns true if ARGS (a collection of template arguments) contains
17742 any dependent arguments. */
17745 any_dependent_template_arguments_p (const_tree args
)
17752 if (args
== error_mark_node
)
17755 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
17757 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
17758 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
17759 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
17766 /* Returns TRUE if the template TMPL is dependent. */
17769 dependent_template_p (tree tmpl
)
17771 if (TREE_CODE (tmpl
) == OVERLOAD
)
17775 if (dependent_template_p (OVL_FUNCTION (tmpl
)))
17777 tmpl
= OVL_CHAIN (tmpl
);
17782 /* Template template parameters are dependent. */
17783 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
17784 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
17786 /* So are names that have not been looked up. */
17787 if (TREE_CODE (tmpl
) == SCOPE_REF
17788 || TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
17790 /* So are member templates of dependent classes. */
17791 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
17792 return dependent_type_p (DECL_CONTEXT (tmpl
));
17796 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
17799 dependent_template_id_p (tree tmpl
, tree args
)
17801 return (dependent_template_p (tmpl
)
17802 || any_dependent_template_arguments_p (args
));
17805 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
17809 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
17813 if (!processing_template_decl
)
17816 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
17818 tree decl
= TREE_VEC_ELT (declv
, i
);
17819 tree init
= TREE_VEC_ELT (initv
, i
);
17820 tree cond
= TREE_VEC_ELT (condv
, i
);
17821 tree incr
= TREE_VEC_ELT (incrv
, i
);
17823 if (type_dependent_expression_p (decl
))
17826 if (init
&& type_dependent_expression_p (init
))
17829 if (type_dependent_expression_p (cond
))
17832 if (COMPARISON_CLASS_P (cond
)
17833 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
17834 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
17837 if (TREE_CODE (incr
) == MODOP_EXPR
)
17839 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
17840 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
17843 else if (type_dependent_expression_p (incr
))
17845 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
17847 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
17849 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
17851 tree t
= TREE_OPERAND (incr
, 1);
17852 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
17853 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
17862 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
17863 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
17864 no such TYPE can be found. Note that this function peers inside
17865 uninstantiated templates and therefore should be used only in
17866 extremely limited situations. ONLY_CURRENT_P restricts this
17867 peering to the currently open classes hierarchy (which is required
17868 when comparing types). */
17871 resolve_typename_type (tree type
, bool only_current_p
)
17880 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
17882 scope
= TYPE_CONTEXT (type
);
17883 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
17884 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
17885 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
17886 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
17887 identifier of the TYPENAME_TYPE anymore.
17888 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
17889 TYPENAME_TYPE instead, we avoid messing up with a possible
17890 typedef variant case. */
17891 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
17893 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
17894 it first before we can figure out what NAME refers to. */
17895 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
17896 scope
= resolve_typename_type (scope
, only_current_p
);
17897 /* If we don't know what SCOPE refers to, then we cannot resolve the
17899 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
17901 /* If the SCOPE is a template type parameter, we have no way of
17902 resolving the name. */
17903 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
17905 /* If the SCOPE is not the current instantiation, there's no reason
17906 to look inside it. */
17907 if (only_current_p
&& !currently_open_class (scope
))
17909 /* If this is a typedef, we don't want to look inside (c++/11987). */
17910 if (typedef_variant_p (type
))
17912 /* If SCOPE isn't the template itself, it will not have a valid
17913 TYPE_FIELDS list. */
17914 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
17915 /* scope is either the template itself or a compatible instantiation
17916 like X<T>, so look up the name in the original template. */
17917 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
17919 /* scope is a partial instantiation, so we can't do the lookup or we
17920 will lose the template arguments. */
17922 /* Enter the SCOPE so that name lookup will be resolved as if we
17923 were in the class definition. In particular, SCOPE will no
17924 longer be considered a dependent type. */
17925 pushed_scope
= push_scope (scope
);
17926 /* Look up the declaration. */
17927 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true);
17929 result
= NULL_TREE
;
17931 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
17932 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
17935 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == IDENTIFIER_NODE
17936 && TREE_CODE (decl
) == TYPE_DECL
)
17938 result
= TREE_TYPE (decl
);
17939 if (result
== error_mark_node
)
17940 result
= NULL_TREE
;
17942 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
17943 && DECL_CLASS_TEMPLATE_P (decl
))
17947 /* Obtain the template and the arguments. */
17948 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
17949 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
17950 /* Instantiate the template. */
17951 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
17952 /*entering_scope=*/0,
17953 tf_error
| tf_user
);
17954 if (result
== error_mark_node
)
17955 result
= NULL_TREE
;
17958 /* Leave the SCOPE. */
17960 pop_scope (pushed_scope
);
17962 /* If we failed to resolve it, return the original typename. */
17966 /* If lookup found a typename type, resolve that too. */
17967 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
17969 /* Ill-formed programs can cause infinite recursion here, so we
17970 must catch that. */
17971 TYPENAME_IS_RESOLVING_P (type
) = 1;
17972 result
= resolve_typename_type (result
, only_current_p
);
17973 TYPENAME_IS_RESOLVING_P (type
) = 0;
17976 /* Qualify the resulting type. */
17977 quals
= cp_type_quals (type
);
17979 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
17984 /* EXPR is an expression which is not type-dependent. Return a proxy
17985 for EXPR that can be used to compute the types of larger
17986 expressions containing EXPR. */
17989 build_non_dependent_expr (tree expr
)
17993 /* Preserve null pointer constants so that the type of things like
17994 "p == 0" where "p" is a pointer can be determined. */
17995 if (null_ptr_cst_p (expr
))
17997 /* Preserve OVERLOADs; the functions must be available to resolve
18000 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
18001 inner_expr
= stmt_expr_value_expr (inner_expr
);
18002 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
18003 inner_expr
= TREE_OPERAND (inner_expr
, 0);
18004 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
18005 inner_expr
= TREE_OPERAND (inner_expr
, 1);
18006 if (is_overloaded_fn (inner_expr
)
18007 || TREE_CODE (inner_expr
) == OFFSET_REF
)
18009 /* There is no need to return a proxy for a variable. */
18010 if (TREE_CODE (expr
) == VAR_DECL
)
18012 /* Preserve string constants; conversions from string constants to
18013 "char *" are allowed, even though normally a "const char *"
18014 cannot be used to initialize a "char *". */
18015 if (TREE_CODE (expr
) == STRING_CST
)
18017 /* Preserve arithmetic constants, as an optimization -- there is no
18018 reason to create a new node. */
18019 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
18021 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
18022 There is at least one place where we want to know that a
18023 particular expression is a throw-expression: when checking a ?:
18024 expression, there are special rules if the second or third
18025 argument is a throw-expression. */
18026 if (TREE_CODE (expr
) == THROW_EXPR
)
18029 if (TREE_CODE (expr
) == COND_EXPR
)
18030 return build3 (COND_EXPR
,
18032 TREE_OPERAND (expr
, 0),
18033 (TREE_OPERAND (expr
, 1)
18034 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
18035 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
18036 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
18037 if (TREE_CODE (expr
) == COMPOUND_EXPR
18038 && !COMPOUND_EXPR_OVERLOADED (expr
))
18039 return build2 (COMPOUND_EXPR
,
18041 TREE_OPERAND (expr
, 0),
18042 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
18044 /* If the type is unknown, it can't really be non-dependent */
18045 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
18047 /* Otherwise, build a NON_DEPENDENT_EXPR.
18049 REFERENCE_TYPEs are not stripped for expressions in templates
18050 because doing so would play havoc with mangling. Consider, for
18053 template <typename T> void f<T& g>() { g(); }
18055 In the body of "f", the expression for "g" will have
18056 REFERENCE_TYPE, even though the standard says that it should
18057 not. The reason is that we must preserve the syntactic form of
18058 the expression so that mangling (say) "f<g>" inside the body of
18059 "f" works out correctly. Therefore, the REFERENCE_TYPE is
18061 return build1 (NON_DEPENDENT_EXPR
, non_reference (TREE_TYPE (expr
)), expr
);
18064 /* ARGS is a vector of expressions as arguments to a function call.
18065 Replace the arguments with equivalent non-dependent expressions.
18066 This modifies ARGS in place. */
18069 make_args_non_dependent (VEC(tree
,gc
) *args
)
18074 for (ix
= 0; VEC_iterate (tree
, args
, ix
, arg
); ++ix
)
18076 tree newarg
= build_non_dependent_expr (arg
);
18078 VEC_replace (tree
, args
, ix
, newarg
);
18082 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
18083 with a level one deeper than the actual template parms. */
18088 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
18089 TYPE_NAME (au
) = build_decl (BUILTINS_LOCATION
,
18090 TYPE_DECL
, get_identifier ("auto"), au
);
18091 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
18092 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
18093 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
18094 TYPE_NAME (au
), NULL_TREE
);
18095 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
18096 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
18097 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
18102 /* Given type ARG, return std::initializer_list<ARG>. */
18107 tree std_init_list
= namespace_binding
18108 (get_identifier ("initializer_list"), std_node
);
18110 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
18112 error ("deducing from brace-enclosed initializer list requires "
18113 "#include <initializer_list>");
18114 return error_mark_node
;
18116 argvec
= make_tree_vec (1);
18117 TREE_VEC_ELT (argvec
, 0) = arg
;
18118 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
18119 NULL_TREE
, 0, tf_warning_or_error
);
18122 /* Replace auto in TYPE with std::initializer_list<auto>. */
18125 listify_autos (tree type
, tree auto_node
)
18127 tree init_auto
= listify (auto_node
);
18128 tree argvec
= make_tree_vec (1);
18129 TREE_VEC_ELT (argvec
, 0) = init_auto
;
18130 if (processing_template_decl
)
18131 argvec
= add_to_template_args (current_template_args (), argvec
);
18132 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
18135 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
18136 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
18139 do_auto_deduction (tree type
, tree init
, tree auto_node
)
18141 tree parms
, tparms
, targs
;
18145 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
18146 with either a new invented type template parameter U or, if the
18147 initializer is a braced-init-list (8.5.4), with
18148 std::initializer_list<U>. */
18149 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
18150 type
= listify_autos (type
, auto_node
);
18152 parms
= build_tree_list (NULL_TREE
, type
);
18154 tparms
= make_tree_vec (1);
18155 targs
= make_tree_vec (1);
18156 TREE_VEC_ELT (tparms
, 0)
18157 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
18158 val
= type_unification_real (tparms
, targs
, parms
, args
, 1, 0,
18159 DEDUCE_CALL
, LOOKUP_NORMAL
);
18162 error ("unable to deduce %qT from %qE", type
, init
);
18163 return error_mark_node
;
18166 /* If the list of declarators contains more than one declarator, the type
18167 of each declared variable is determined as described above. If the
18168 type deduced for the template parameter U is not the same in each
18169 deduction, the program is ill-formed. */
18170 if (TREE_TYPE (auto_node
)
18171 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
18173 error ("inconsistent deduction for %qT: %qT and then %qT",
18174 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
18175 return error_mark_node
;
18177 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
18179 if (processing_template_decl
)
18180 targs
= add_to_template_args (current_template_args (), targs
);
18181 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
18184 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
18188 splice_late_return_type (tree type
, tree late_return_type
)
18192 if (late_return_type
== NULL_TREE
)
18194 argvec
= make_tree_vec (1);
18195 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
18196 if (processing_template_decl
)
18197 argvec
= add_to_template_args (current_template_args (), argvec
);
18198 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
18201 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
18204 is_auto (const_tree type
)
18206 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
18207 && TYPE_IDENTIFIER (type
) == get_identifier ("auto"))
18213 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
18214 appear as a type-specifier for the declaration in question, we don't
18215 have to look through the whole type. */
18218 type_uses_auto (tree type
)
18220 enum tree_code code
;
18221 if (is_auto (type
))
18224 code
= TREE_CODE (type
);
18226 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
18227 || code
== OFFSET_TYPE
|| code
== FUNCTION_TYPE
18228 || code
== METHOD_TYPE
|| code
== ARRAY_TYPE
)
18229 return type_uses_auto (TREE_TYPE (type
));
18231 if (TYPE_PTRMEMFUNC_P (type
))
18232 return type_uses_auto (TREE_TYPE (TREE_TYPE
18233 (TYPE_PTRMEMFUNC_FN_TYPE (type
))));
18238 /* For a given template T, return the vector of typedefs referenced
18239 in T for which access check is needed at T instantiation time.
18240 T is either a FUNCTION_DECL or a RECORD_TYPE.
18241 Those typedefs were added to T by the function
18242 append_type_to_template_for_access_check. */
18244 VEC(qualified_typedef_usage_t
,gc
)*
18245 get_types_needing_access_check (tree t
)
18248 VEC(qualified_typedef_usage_t
,gc
) *result
= NULL
;
18250 if (!t
|| t
== error_mark_node
)
18253 if (!(ti
= get_template_info (t
)))
18256 if (CLASS_TYPE_P (t
)
18257 || TREE_CODE (t
) == FUNCTION_DECL
)
18259 if (!TI_TEMPLATE (ti
))
18262 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
18268 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
18269 tied to T. That list of typedefs will be access checked at
18270 T instantiation time.
18271 T is either a FUNCTION_DECL or a RECORD_TYPE.
18272 TYPE_DECL is a TYPE_DECL node representing a typedef.
18273 SCOPE is the scope through which TYPE_DECL is accessed.
18274 LOCATION is the location of the usage point of TYPE_DECL.
18276 This function is a subroutine of
18277 append_type_to_template_for_access_check. */
18280 append_type_to_template_for_access_check_1 (tree t
,
18283 location_t location
)
18285 qualified_typedef_usage_t typedef_usage
;
18288 if (!t
|| t
== error_mark_node
)
18291 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
18292 || CLASS_TYPE_P (t
))
18294 && TREE_CODE (type_decl
) == TYPE_DECL
18297 if (!(ti
= get_template_info (t
)))
18300 gcc_assert (TI_TEMPLATE (ti
));
18302 typedef_usage
.typedef_decl
= type_decl
;
18303 typedef_usage
.context
= scope
;
18304 typedef_usage
.locus
= location
;
18306 VEC_safe_push (qualified_typedef_usage_t
, gc
,
18307 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
),
18311 /* Append TYPE_DECL to the template TEMPL.
18312 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
18313 At TEMPL instanciation time, TYPE_DECL will be checked to see
18314 if it can be accessed through SCOPE.
18315 LOCATION is the location of the usage point of TYPE_DECL.
18317 e.g. consider the following code snippet:
18324 template<class U> struct S
18326 C::myint mi; // <-- usage point of the typedef C::myint
18331 At S<char> instantiation time, we need to check the access of C::myint
18332 In other words, we need to check the access of the myint typedef through
18333 the C scope. For that purpose, this function will add the myint typedef
18334 and the scope C through which its being accessed to a list of typedefs
18335 tied to the template S. That list will be walked at template instantiation
18336 time and access check performed on each typedefs it contains.
18337 Note that this particular code snippet should yield an error because
18338 myint is private to C. */
18341 append_type_to_template_for_access_check (tree templ
,
18344 location_t location
)
18346 qualified_typedef_usage_t
*iter
;
18349 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
18351 /* Make sure we don't append the type to the template twice. */
18353 VEC_iterate (qualified_typedef_usage_t
,
18354 get_types_needing_access_check (templ
),
18357 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
18360 append_type_to_template_for_access_check_1 (templ
, type_decl
,
18364 /* Set up the hash tables for template instantiations. */
18367 init_template_processing (void)
18369 decl_specializations
= htab_create_ggc (37,
18370 hash_specialization
,
18371 eq_specializations
,
18373 type_specializations
= htab_create_ggc (37,
18374 hash_specialization
,
18375 eq_specializations
,
18379 #include "gt-cp-pt.h"