1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
6 Rewritten by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* Known bugs or deficiencies include:
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
31 #include "coretypes.h"
35 #include "pointer-set.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "cp-objcp-common.h"
41 #include "tree-inline.h"
46 #include "tree-iterator.h"
49 /* The type of functions taking a tree, and some additional data, and
51 typedef int (*tree_fn_t
) (tree
, void*);
53 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
54 instantiations have been deferred, either because their definitions
55 were not yet available, or because we were putting off doing the work. */
56 struct GTY ((chain_next ("%h.next"))) pending_template
{
57 struct pending_template
*next
;
58 struct tinst_level
*tinst
;
61 static GTY(()) struct pending_template
*pending_templates
;
62 static GTY(()) struct pending_template
*last_pending_template
;
64 int processing_template_parmlist
;
65 static int template_header_count
;
67 static GTY(()) tree saved_trees
;
68 static VEC(int,heap
) *inline_parm_levels
;
70 static GTY(()) struct tinst_level
*current_tinst_level
;
72 static GTY(()) tree saved_access_scope
;
74 /* Live only within one (recursive) call to tsubst_expr. We use
75 this to pass the statement expression node from the STMT_EXPR
76 to the EXPR_STMT that is its result. */
77 static tree cur_stmt_expr
;
79 /* A map from local variable declarations in the body of the template
80 presently being instantiated to the corresponding instantiated
82 static htab_t local_specializations
;
84 typedef struct GTY(()) spec_entry
91 static GTY ((param_is (spec_entry
)))
92 htab_t decl_specializations
;
94 static GTY ((param_is (spec_entry
)))
95 htab_t type_specializations
;
97 /* Contains canonical template parameter types. The vector is indexed by
98 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
99 TREE_LIST, whose TREE_VALUEs contain the canonical template
100 parameters of various types and levels. */
101 static GTY(()) VEC(tree
,gc
) *canonical_template_parms
;
103 #define UNIFY_ALLOW_NONE 0
104 #define UNIFY_ALLOW_MORE_CV_QUAL 1
105 #define UNIFY_ALLOW_LESS_CV_QUAL 2
106 #define UNIFY_ALLOW_DERIVED 4
107 #define UNIFY_ALLOW_INTEGER 8
108 #define UNIFY_ALLOW_OUTER_LEVEL 16
109 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
110 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
112 static void push_access_scope (tree
);
113 static void pop_access_scope (tree
);
114 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
115 unification_kind_t
, int);
116 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
117 unification_kind_t
, int, bool);
118 static int unify (tree
, tree
, tree
, tree
, int);
119 static void add_pending_template (tree
);
120 static tree
reopen_tinst_level (struct tinst_level
*);
121 static tree
tsubst_initializer_list (tree
, tree
);
122 static tree
get_class_bindings (tree
, tree
, tree
);
123 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
125 static void tsubst_enum (tree
, tree
, tree
);
126 static tree
add_to_template_args (tree
, tree
);
127 static tree
add_outermost_template_args (tree
, tree
);
128 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
129 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
131 static int type_unification_real (tree
, tree
, tree
, const tree
*,
132 unsigned int, int, unification_kind_t
, int);
133 static void note_template_header (int);
134 static tree
convert_nontype_argument_function (tree
, tree
);
135 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
136 static tree
convert_template_argument (tree
, tree
, tree
,
137 tsubst_flags_t
, int, tree
);
138 static int for_each_template_parm (tree
, tree_fn_t
, void*,
139 struct pointer_set_t
*, bool);
140 static tree
expand_template_argument_pack (tree
);
141 static tree
build_template_parm_index (int, int, int, int, tree
, tree
);
142 static bool inline_needs_template_parms (tree
);
143 static void push_inline_template_parms_recursive (tree
, int);
144 static tree
retrieve_local_specialization (tree
);
145 static void register_local_specialization (tree
, tree
);
146 static hashval_t
hash_specialization (const void *p
);
147 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
148 static int mark_template_parm (tree
, void *);
149 static int template_parm_this_level_p (tree
, void *);
150 static tree
tsubst_friend_function (tree
, tree
);
151 static tree
tsubst_friend_class (tree
, tree
);
152 static int can_complete_type_without_circularity (tree
);
153 static tree
get_bindings (tree
, tree
, tree
, bool);
154 static int template_decl_level (tree
);
155 static int check_cv_quals_for_unify (int, tree
, tree
);
156 static void template_parm_level_and_index (tree
, int*, int*);
157 static int unify_pack_expansion (tree
, tree
, tree
, tree
, int, bool, bool);
158 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
159 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
160 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
161 static void regenerate_decl_from_template (tree
, tree
);
162 static tree
most_specialized_class (tree
, tree
, tsubst_flags_t
);
163 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
164 static tree
tsubst_arg_types (tree
, tree
, tsubst_flags_t
, tree
);
165 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
166 static bool check_specialization_scope (void);
167 static tree
process_partial_specialization (tree
);
168 static void set_current_access_from_decl (tree
);
169 static tree
get_template_base (tree
, tree
, tree
, tree
);
170 static tree
try_class_unification (tree
, tree
, tree
, tree
);
171 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
173 static bool template_template_parm_bindings_ok_p (tree
, tree
);
174 static int template_args_equal (tree
, tree
);
175 static void tsubst_default_arguments (tree
);
176 static tree
for_each_template_parm_r (tree
*, int *, void *);
177 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
178 static void copy_default_args_to_explicit_spec (tree
);
179 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
180 static int eq_local_specializations (const void *, const void *);
181 static bool dependent_template_arg_p (tree
);
182 static bool any_template_arguments_need_structural_equality_p (tree
);
183 static bool dependent_type_p_r (tree
);
184 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
, bool);
185 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
186 static tree
tsubst_pack_expansion (tree
, tree
, tsubst_flags_t
, tree
);
187 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
188 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
189 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
191 static tree
listify (tree
);
192 static tree
listify_autos (tree
, tree
);
193 static tree
template_parm_to_arg (tree t
);
194 static tree
current_template_args (void);
195 static tree
fixup_template_type_parm_type (tree
, int);
196 static tree
fixup_template_parm_index (tree
, tree
, int);
197 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
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
) : CP_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 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
501 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
502 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
504 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
505 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
507 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
508 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
513 /* Like add_to_template_args, but only the outermost ARGS are added to
514 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
515 (EXTRA_ARGS) levels are added. This function is used to combine
516 the template arguments from a partial instantiation with the
517 template arguments used to attain the full instantiation from the
518 partial instantiation. */
521 add_outermost_template_args (tree args
, tree extra_args
)
525 /* If there are more levels of EXTRA_ARGS than there are ARGS,
526 something very fishy is going on. */
527 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
529 /* If *all* the new arguments will be the EXTRA_ARGS, just return
531 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
534 /* For the moment, we make ARGS look like it contains fewer levels. */
535 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
537 new_args
= add_to_template_args (args
, extra_args
);
539 /* Now, we restore ARGS to its full dimensions. */
540 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
545 /* Return the N levels of innermost template arguments from the ARGS. */
548 get_innermost_template_args (tree args
, int n
)
556 /* If N is 1, just return the innermost set of template arguments. */
558 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
560 /* If we're not removing anything, just return the arguments we were
562 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
563 gcc_assert (extra_levels
>= 0);
564 if (extra_levels
== 0)
567 /* Make a new set of arguments, not containing the outer arguments. */
568 new_args
= make_tree_vec (n
);
569 for (i
= 1; i
<= n
; ++i
)
570 SET_TMPL_ARGS_LEVEL (new_args
, i
,
571 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
576 /* The inverse of get_innermost_template_args: Return all but the innermost
577 EXTRA_LEVELS levels of template arguments from the ARGS. */
580 strip_innermost_template_args (tree args
, int extra_levels
)
583 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
588 /* If N is 1, just return the outermost set of template arguments. */
590 return TMPL_ARGS_LEVEL (args
, 1);
592 /* If we're not removing anything, just return the arguments we were
594 gcc_assert (extra_levels
>= 0);
595 if (extra_levels
== 0)
598 /* Make a new set of arguments, not containing the inner arguments. */
599 new_args
= make_tree_vec (n
);
600 for (i
= 1; i
<= n
; ++i
)
601 SET_TMPL_ARGS_LEVEL (new_args
, i
,
602 TMPL_ARGS_LEVEL (args
, i
));
607 /* We've got a template header coming up; push to a new level for storing
611 begin_template_parm_list (void)
613 /* We use a non-tag-transparent scope here, which causes pushtag to
614 put tags in this scope, rather than in the enclosing class or
615 namespace scope. This is the right thing, since we want
616 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
617 global template class, push_template_decl handles putting the
618 TEMPLATE_DECL into top-level scope. For a nested template class,
621 template <class T> struct S1 {
622 template <class T> struct S2 {};
625 pushtag contains special code to call pushdecl_with_scope on the
626 TEMPLATE_DECL for S2. */
627 begin_scope (sk_template_parms
, NULL
);
628 ++processing_template_decl
;
629 ++processing_template_parmlist
;
630 note_template_header (0);
633 /* This routine is called when a specialization is declared. If it is
634 invalid to declare a specialization here, an error is reported and
635 false is returned, otherwise this routine will return true. */
638 check_specialization_scope (void)
640 tree scope
= current_scope ();
644 An explicit specialization shall be declared in the namespace of
645 which the template is a member, or, for member templates, in the
646 namespace of which the enclosing class or enclosing class
647 template is a member. An explicit specialization of a member
648 function, member class or static data member of a class template
649 shall be declared in the namespace of which the class template
651 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
653 error ("explicit specialization in non-namespace scope %qD", scope
);
659 In an explicit specialization declaration for a member of a class
660 template or a member template that appears in namespace scope,
661 the member template and some of its enclosing class templates may
662 remain unspecialized, except that the declaration shall not
663 explicitly specialize a class member template if its enclosing
664 class templates are not explicitly specialized as well. */
665 if (current_template_parms
)
667 error ("enclosing class templates are not explicitly specialized");
674 /* We've just seen template <>. */
677 begin_specialization (void)
679 begin_scope (sk_template_spec
, NULL
);
680 note_template_header (1);
681 return check_specialization_scope ();
684 /* Called at then end of processing a declaration preceded by
688 end_specialization (void)
691 reset_specialization ();
694 /* Any template <>'s that we have seen thus far are not referring to a
695 function specialization. */
698 reset_specialization (void)
700 processing_specialization
= 0;
701 template_header_count
= 0;
704 /* We've just seen a template header. If SPECIALIZATION is nonzero,
705 it was of the form template <>. */
708 note_template_header (int specialization
)
710 processing_specialization
= specialization
;
711 template_header_count
++;
714 /* We're beginning an explicit instantiation. */
717 begin_explicit_instantiation (void)
719 gcc_assert (!processing_explicit_instantiation
);
720 processing_explicit_instantiation
= true;
725 end_explicit_instantiation (void)
727 gcc_assert (processing_explicit_instantiation
);
728 processing_explicit_instantiation
= false;
731 /* An explicit specialization or partial specialization TMPL is being
732 declared. Check that the namespace in which the specialization is
733 occurring is permissible. Returns false iff it is invalid to
734 specialize TMPL in the current namespace. */
737 check_specialization_namespace (tree tmpl
)
739 tree tpl_ns
= decl_namespace_context (tmpl
);
743 An explicit specialization shall be declared in the namespace of
744 which the template is a member, or, for member templates, in the
745 namespace of which the enclosing class or enclosing class
746 template is a member. An explicit specialization of a member
747 function, member class or static data member of a class template
748 shall be declared in the namespace of which the class template is
750 if (current_scope() != DECL_CONTEXT (tmpl
)
751 && !at_namespace_scope_p ())
753 error ("specialization of %qD must appear at namespace scope", tmpl
);
756 if (is_associated_namespace (current_namespace
, tpl_ns
))
757 /* Same or super-using namespace. */
761 permerror (input_location
, "specialization of %qD in different namespace", tmpl
);
762 permerror (input_location
, " from definition of %q+#D", tmpl
);
767 /* SPEC is an explicit instantiation. Check that it is valid to
768 perform this explicit instantiation in the current namespace. */
771 check_explicit_instantiation_namespace (tree spec
)
775 /* DR 275: An explicit instantiation shall appear in an enclosing
776 namespace of its template. */
777 ns
= decl_namespace_context (spec
);
778 if (!is_ancestor (current_namespace
, ns
))
779 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
780 "(which does not enclose namespace %qD)",
781 spec
, current_namespace
, ns
);
784 /* The TYPE is being declared. If it is a template type, that means it
785 is a partial specialization. Do appropriate error-checking. */
788 maybe_process_partial_specialization (tree type
)
792 if (type
== error_mark_node
)
793 return error_mark_node
;
795 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
797 error ("name of class shadows template template parameter %qD",
799 return error_mark_node
;
802 context
= TYPE_CONTEXT (type
);
804 if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
806 /* This is for ordinary explicit specialization and partial
807 specialization of a template class such as:
809 template <> class C<int>;
813 template <class T> class C<T*>;
815 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
817 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
818 && !COMPLETE_TYPE_P (type
))
820 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
));
821 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
822 if (processing_template_decl
)
824 if (push_template_decl (TYPE_MAIN_DECL (type
))
826 return error_mark_node
;
829 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
830 error ("specialization of %qT after instantiation", type
);
832 else if (CLASS_TYPE_P (type
)
833 && !CLASSTYPE_USE_TEMPLATE (type
)
834 && CLASSTYPE_TEMPLATE_INFO (type
)
835 && context
&& CLASS_TYPE_P (context
)
836 && CLASSTYPE_TEMPLATE_INFO (context
))
838 /* This is for an explicit specialization of member class
839 template according to [temp.expl.spec/18]:
841 template <> template <class U> class C<int>::D;
843 The context `C<int>' must be an implicit instantiation.
844 Otherwise this is just a member class template declared
847 template <> class C<int> { template <class U> class D; };
848 template <> template <class U> class C<int>::D;
850 In the first case, `C<int>::D' is a specialization of `C<T>::D'
851 while in the second case, `C<int>::D' is a primary template
852 and `C<T>::D' may not exist. */
854 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
855 && !COMPLETE_TYPE_P (type
))
858 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
860 if (current_namespace
861 != decl_namespace_context (tmpl
))
863 permerror (input_location
, "specializing %q#T in different namespace", type
);
864 permerror (input_location
, " from definition of %q+#D", tmpl
);
867 /* Check for invalid specialization after instantiation:
869 template <> template <> class C<int>::D<int>;
870 template <> template <class U> class C<int>::D; */
872 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
873 t
; t
= TREE_CHAIN (t
))
875 tree inst
= TREE_VALUE (t
);
876 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
))
878 /* We already have a full specialization of this partial
879 instantiation. Reassign it to the new member
880 specialization template. */
884 elt
.tmpl
= most_general_template (tmpl
);
885 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
888 htab_remove_elt (type_specializations
, &elt
);
891 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
893 slot
= (spec_entry
**)
894 htab_find_slot (type_specializations
, &elt
, INSERT
);
895 *slot
= ggc_alloc_spec_entry ();
898 else if (COMPLETE_OR_OPEN_TYPE_P (inst
))
899 /* But if we've had an implicit instantiation, that's a
900 problem ([temp.expl.spec]/6). */
901 error ("specialization %qT after instantiation %qT",
905 /* Mark TYPE as a specialization. And as a result, we only
906 have one level of template argument for the innermost
908 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
909 CLASSTYPE_TI_ARGS (type
)
910 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
913 else if (processing_specialization
)
915 /* Someday C++0x may allow for enum template specialization. */
916 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
917 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
918 pedwarn (input_location
, OPT_pedantic
, "template specialization "
919 "of %qD not allowed by ISO C++", type
);
922 error ("explicit specialization of non-template %qT", type
);
923 return error_mark_node
;
930 /* Returns nonzero if we can optimize the retrieval of specializations
931 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
932 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
935 optimize_specialization_lookup_p (tree tmpl
)
937 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
938 && DECL_CLASS_SCOPE_P (tmpl
)
939 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
941 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
942 /* The optimized lookup depends on the fact that the
943 template arguments for the member function template apply
944 purely to the containing class, which is not true if the
945 containing class is an explicit or partial
947 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
948 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
949 && !DECL_CONV_FN_P (tmpl
)
950 /* It is possible to have a template that is not a member
951 template and is not a member of a template class:
953 template <typename T>
954 struct S { friend A::f(); };
956 Here, the friend function is a template, but the context does
957 not have template information. The optimized lookup relies
958 on having ARGS be the template arguments for both the class
959 and the function template. */
960 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
963 /* Retrieve the specialization (in the sense of [temp.spec] - a
964 specialization is either an instantiation or an explicit
965 specialization) of TMPL for the given template ARGS. If there is
966 no such specialization, return NULL_TREE. The ARGS are a vector of
967 arguments, or a vector of vectors of arguments, in the case of
968 templates with more than one level of parameters.
970 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
971 then we search for a partial specialization matching ARGS. This
972 parameter is ignored if TMPL is not a class template. */
975 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
977 if (args
== error_mark_node
)
980 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
982 /* There should be as many levels of arguments as there are
983 levels of parameters. */
984 gcc_assert (TMPL_ARGS_DEPTH (args
)
985 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
987 if (optimize_specialization_lookup_p (tmpl
))
990 tree class_specialization
;
991 VEC(tree
,gc
) *methods
;
995 /* The template arguments actually apply to the containing
996 class. Find the class specialization with those
998 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1000 = retrieve_specialization (class_template
, args
, 0);
1001 if (!class_specialization
)
1003 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1004 for the specialization. */
1005 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1008 /* Iterate through the methods with the indicated name, looking
1009 for the one that has an instance of TMPL. */
1010 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1011 for (fns
= VEC_index (tree
, methods
, idx
); fns
; fns
= OVL_NEXT (fns
))
1013 tree fn
= OVL_CURRENT (fns
);
1014 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1015 /* using-declarations can add base methods to the method vec,
1016 and we don't want those here. */
1017 && DECL_CONTEXT (fn
) == class_specialization
)
1026 htab_t specializations
;
1030 elt
.spec
= NULL_TREE
;
1032 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1033 specializations
= type_specializations
;
1035 specializations
= decl_specializations
;
1038 hash
= hash_specialization (&elt
);
1039 found
= (spec_entry
*) htab_find_with_hash (specializations
, &elt
, hash
);
1047 /* Like retrieve_specialization, but for local declarations. */
1050 retrieve_local_specialization (tree tmpl
)
1054 if (local_specializations
== NULL
)
1057 spec
= (tree
) htab_find_with_hash (local_specializations
, tmpl
,
1058 htab_hash_pointer (tmpl
));
1059 return spec
? TREE_PURPOSE (spec
) : NULL_TREE
;
1062 /* Returns nonzero iff DECL is a specialization of TMPL. */
1065 is_specialization_of (tree decl
, tree tmpl
)
1069 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1073 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1079 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1081 for (t
= TREE_TYPE (decl
);
1083 t
= CLASSTYPE_USE_TEMPLATE (t
)
1084 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1085 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1092 /* Returns nonzero iff DECL is a specialization of friend declaration
1093 FRIEND_DECL according to [temp.friend]. */
1096 is_specialization_of_friend (tree decl
, tree friend_decl
)
1098 bool need_template
= true;
1101 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1102 || TREE_CODE (decl
) == TYPE_DECL
);
1104 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1105 of a template class, we want to check if DECL is a specialization
1107 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1108 && DECL_TEMPLATE_INFO (friend_decl
)
1109 && !DECL_USE_TEMPLATE (friend_decl
))
1111 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1112 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1113 need_template
= false;
1115 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1116 && !PRIMARY_TEMPLATE_P (friend_decl
))
1117 need_template
= false;
1119 /* There is nothing to do if this is not a template friend. */
1120 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1123 if (is_specialization_of (decl
, friend_decl
))
1127 A member of a class template may be declared to be a friend of a
1128 non-template class. In this case, the corresponding member of
1129 every specialization of the class template is a friend of the
1130 class granting friendship.
1132 For example, given a template friend declaration
1134 template <class T> friend void A<T>::f();
1136 the member function below is considered a friend
1138 template <> struct A<int> {
1142 For this type of template friend, TEMPLATE_DEPTH below will be
1143 nonzero. To determine if DECL is a friend of FRIEND, we first
1144 check if the enclosing class is a specialization of another. */
1146 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1148 && DECL_CLASS_SCOPE_P (decl
)
1149 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1150 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1152 /* Next, we check the members themselves. In order to handle
1153 a few tricky cases, such as when FRIEND_DECL's are
1155 template <class T> friend void A<T>::g(T t);
1156 template <class T> template <T t> friend void A<T>::h();
1160 void A<int>::g(int);
1161 template <int> void A<int>::h();
1163 we need to figure out ARGS, the template arguments from
1164 the context of DECL. This is required for template substitution
1165 of `T' in the function parameter of `g' and template parameter
1166 of `h' in the above examples. Here ARGS corresponds to `int'. */
1168 tree context
= DECL_CONTEXT (decl
);
1169 tree args
= NULL_TREE
;
1170 int current_depth
= 0;
1172 while (current_depth
< template_depth
)
1174 if (CLASSTYPE_TEMPLATE_INFO (context
))
1176 if (current_depth
== 0)
1177 args
= TYPE_TI_ARGS (context
);
1179 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1182 context
= TYPE_CONTEXT (context
);
1185 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1190 tree friend_args_type
;
1191 tree decl_args_type
;
1193 /* Make sure that both DECL and FRIEND_DECL are templates or
1195 is_template
= DECL_TEMPLATE_INFO (decl
)
1196 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1197 if (need_template
^ is_template
)
1199 else if (is_template
)
1201 /* If both are templates, check template parameter list. */
1203 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1205 if (!comp_template_parms
1206 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1210 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1213 decl_type
= TREE_TYPE (decl
);
1215 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1216 tf_none
, NULL_TREE
);
1217 if (friend_type
== error_mark_node
)
1220 /* Check if return types match. */
1221 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1224 /* Check if function parameter types match, ignoring the
1225 `this' parameter. */
1226 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1227 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1228 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1229 friend_args_type
= TREE_CHAIN (friend_args_type
);
1230 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1231 decl_args_type
= TREE_CHAIN (decl_args_type
);
1233 return compparms (decl_args_type
, friend_args_type
);
1237 /* DECL is a TYPE_DECL */
1239 tree decl_type
= TREE_TYPE (decl
);
1241 /* Make sure that both DECL and FRIEND_DECL are templates or
1244 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1245 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1247 if (need_template
^ is_template
)
1249 else if (is_template
)
1252 /* If both are templates, check the name of the two
1253 TEMPLATE_DECL's first because is_friend didn't. */
1254 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1255 != DECL_NAME (friend_decl
))
1258 /* Now check template parameter list. */
1260 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1262 return comp_template_parms
1263 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1267 return (DECL_NAME (decl
)
1268 == DECL_NAME (friend_decl
));
1274 /* Register the specialization SPEC as a specialization of TMPL with
1275 the indicated ARGS. IS_FRIEND indicates whether the specialization
1276 is actually just a friend declaration. Returns SPEC, or an
1277 equivalent prior declaration, if available. */
1280 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1284 spec_entry
**slot
= NULL
;
1287 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
));
1289 if (TREE_CODE (spec
) == FUNCTION_DECL
1290 && uses_template_parms (DECL_TI_ARGS (spec
)))
1291 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1292 register it; we want the corresponding TEMPLATE_DECL instead.
1293 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1294 the more obvious `uses_template_parms (spec)' to avoid problems
1295 with default function arguments. In particular, given
1296 something like this:
1298 template <class T> void f(T t1, T t = T())
1300 the default argument expression is not substituted for in an
1301 instantiation unless and until it is actually needed. */
1304 if (optimize_specialization_lookup_p (tmpl
))
1305 /* We don't put these specializations in the hash table, but we might
1306 want to give an error about a mismatch. */
1307 fn
= retrieve_specialization (tmpl
, args
, 0);
1315 hash
= hash_specialization (&elt
);
1317 slot
= (spec_entry
**)
1318 htab_find_slot_with_hash (decl_specializations
, &elt
, hash
, INSERT
);
1325 /* We can sometimes try to re-register a specialization that we've
1326 already got. In particular, regenerate_decl_from_template calls
1327 duplicate_decls which will update the specialization list. But,
1328 we'll still get called again here anyhow. It's more convenient
1329 to simply allow this than to try to prevent it. */
1332 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1334 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1336 if (DECL_ODR_USED (fn
)
1337 || DECL_EXPLICIT_INSTANTIATION (fn
))
1339 error ("specialization of %qD after instantiation",
1341 return error_mark_node
;
1346 /* This situation should occur only if the first
1347 specialization is an implicit instantiation, the
1348 second is an explicit specialization, and the
1349 implicit instantiation has not yet been used. That
1350 situation can occur if we have implicitly
1351 instantiated a member function and then specialized
1354 We can also wind up here if a friend declaration that
1355 looked like an instantiation turns out to be a
1358 template <class T> void foo(T);
1359 class S { friend void foo<>(int) };
1360 template <> void foo(int);
1362 We transform the existing DECL in place so that any
1363 pointers to it become pointers to the updated
1366 If there was a definition for the template, but not
1367 for the specialization, we want this to look as if
1368 there were no definition, and vice versa. */
1369 DECL_INITIAL (fn
) = NULL_TREE
;
1370 duplicate_decls (spec
, fn
, is_friend
);
1371 /* The call to duplicate_decls will have applied
1374 An explicit specialization of a function template
1375 is inline only if it is explicitly declared to be,
1376 and independently of whether its function template
1379 to the primary function; now copy the inline bits to
1380 the various clones. */
1381 FOR_EACH_CLONE (clone
, fn
)
1383 DECL_DECLARED_INLINE_P (clone
)
1384 = DECL_DECLARED_INLINE_P (fn
);
1385 DECL_SOURCE_LOCATION (clone
)
1386 = DECL_SOURCE_LOCATION (fn
);
1388 check_specialization_namespace (fn
);
1393 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1395 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1396 /* Dup decl failed, but this is a new definition. Set the
1397 line number so any errors match this new
1399 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1405 return duplicate_decls (spec
, fn
, is_friend
);
1407 /* A specialization must be declared in the same namespace as the
1408 template it is specializing. */
1409 if (DECL_TEMPLATE_SPECIALIZATION (spec
)
1410 && !check_specialization_namespace (tmpl
))
1411 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1413 if (!optimize_specialization_lookup_p (tmpl
))
1415 gcc_assert (tmpl
&& args
&& spec
);
1416 *slot
= ggc_alloc_spec_entry ();
1418 if (TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1419 && PRIMARY_TEMPLATE_P (tmpl
)
1420 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1421 /* TMPL is a forward declaration of a template function; keep a list
1422 of all specializations in case we need to reassign them to a friend
1423 template later in tsubst_friend_function. */
1424 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1425 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1431 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1432 TMPL and ARGS members, ignores SPEC. */
1435 eq_specializations (const void *p1
, const void *p2
)
1437 const spec_entry
*e1
= (const spec_entry
*)p1
;
1438 const spec_entry
*e2
= (const spec_entry
*)p2
;
1440 return (e1
->tmpl
== e2
->tmpl
1441 && comp_template_args (e1
->args
, e2
->args
));
1444 /* Returns a hash for a template TMPL and template arguments ARGS. */
1447 hash_tmpl_and_args (tree tmpl
, tree args
)
1449 hashval_t val
= DECL_UID (tmpl
);
1450 return iterative_hash_template_arg (args
, val
);
1453 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1457 hash_specialization (const void *p
)
1459 const spec_entry
*e
= (const spec_entry
*)p
;
1460 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1463 /* Recursively calculate a hash value for a template argument ARG, for use
1464 in the hash tables of template specializations. */
1467 iterative_hash_template_arg (tree arg
, hashval_t val
)
1469 unsigned HOST_WIDE_INT i
;
1470 enum tree_code code
;
1473 if (arg
== NULL_TREE
)
1474 return iterative_hash_object (arg
, val
);
1479 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1480 /* We can get one of these when re-hashing a previous entry in the middle
1481 of substituting into a pack expansion. Just look through it. */
1482 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1484 code
= TREE_CODE (arg
);
1485 tclass
= TREE_CODE_CLASS (code
);
1487 val
= iterative_hash_object (code
, val
);
1494 case IDENTIFIER_NODE
:
1495 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1499 int i
, len
= TREE_VEC_LENGTH (arg
);
1500 for (i
= 0; i
< len
; ++i
)
1501 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1505 case TYPE_PACK_EXPANSION
:
1506 case EXPR_PACK_EXPANSION
:
1507 return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1509 case TYPE_ARGUMENT_PACK
:
1510 case NONTYPE_ARGUMENT_PACK
:
1511 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1514 for (; arg
; arg
= TREE_CHAIN (arg
))
1515 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1519 for (; arg
; arg
= OVL_CHAIN (arg
))
1520 val
= iterative_hash_template_arg (OVL_FUNCTION (arg
), val
);
1526 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1528 val
= iterative_hash_template_arg (field
, val
);
1529 val
= iterative_hash_template_arg (value
, val
);
1535 if (!DECL_ARTIFICIAL (arg
))
1536 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1537 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1540 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1543 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1544 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1546 case TEMPLATE_PARM_INDEX
:
1547 val
= iterative_hash_template_arg
1548 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1549 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1550 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1553 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1554 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1555 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1558 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1560 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1564 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1565 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1566 val
= iterative_hash_object (code
, val
);
1567 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1570 /* layout_type sets structural equality for arrays of
1571 incomplete type, so we can't rely on the canonical type
1573 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1574 return iterative_hash_template_arg (TYPE_DOMAIN (arg
), val
);
1577 /* A lambda can't appear in a template arg, but don't crash on
1579 gcc_assert (seen_error ());
1583 case STATIC_CAST_EXPR
:
1584 case REINTERPRET_CAST_EXPR
:
1585 case CONST_CAST_EXPR
:
1586 case DYNAMIC_CAST_EXPR
:
1588 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1589 /* Now hash operands as usual. */
1599 if (TYPE_CANONICAL (arg
))
1600 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1602 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1603 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1604 /* Otherwise just compare the types during lookup. */
1607 case tcc_declaration
:
1609 return iterative_hash_expr (arg
, val
);
1612 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1614 unsigned n
= TREE_OPERAND_LENGTH (arg
);
1615 for (i
= 0; i
< n
; ++i
)
1616 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1624 /* Unregister the specialization SPEC as a specialization of TMPL.
1625 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1626 if the SPEC was listed as a specialization of TMPL.
1628 Note that SPEC has been ggc_freed, so we can't look inside it. */
1631 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1636 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1637 elt
.args
= TI_ARGS (tinfo
);
1638 elt
.spec
= NULL_TREE
;
1640 slot
= (spec_entry
**) htab_find_slot (decl_specializations
, &elt
, INSERT
);
1643 gcc_assert ((*slot
)->spec
== spec
|| (*slot
)->spec
== new_spec
);
1644 gcc_assert (new_spec
!= NULL_TREE
);
1645 (*slot
)->spec
= new_spec
;
1652 /* Compare an entry in the local specializations hash table P1 (which
1653 is really a pointer to a TREE_LIST) with P2 (which is really a
1657 eq_local_specializations (const void *p1
, const void *p2
)
1659 return TREE_VALUE ((const_tree
) p1
) == (const_tree
) p2
;
1662 /* Hash P1, an entry in the local specializations table. */
1665 hash_local_specialization (const void* p1
)
1667 return htab_hash_pointer (TREE_VALUE ((const_tree
) p1
));
1670 /* Like register_specialization, but for local declarations. We are
1671 registering SPEC, an instantiation of TMPL. */
1674 register_local_specialization (tree spec
, tree tmpl
)
1678 slot
= htab_find_slot_with_hash (local_specializations
, tmpl
,
1679 htab_hash_pointer (tmpl
), INSERT
);
1680 *slot
= build_tree_list (spec
, tmpl
);
1683 /* TYPE is a class type. Returns true if TYPE is an explicitly
1684 specialized class. */
1687 explicit_class_specialization_p (tree type
)
1689 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1691 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1694 /* Print the list of functions at FNS, going through all the overloads
1695 for each element of the list. Alternatively, FNS can not be a
1696 TREE_LIST, in which case it will be printed together with all the
1699 MORE and *STR should respectively be FALSE and NULL when the function
1700 is called from the outside. They are used internally on recursive
1701 calls. print_candidates manages the two parameters and leaves NULL
1702 in *STR when it ends. */
1705 print_candidates_1 (tree fns
, bool more
, const char **str
)
1708 char *spaces
= NULL
;
1710 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1711 if (TREE_CODE (fn
) == TREE_LIST
)
1713 gcc_assert (!OVL_NEXT (fn
) && !is_overloaded_fn (fn
));
1714 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1715 print_candidates_1 (TREE_VALUE (fn2
),
1716 TREE_CHAIN (fn2
) || more
, str
);
1722 /* Pick the prefix string. */
1723 if (!more
&& !OVL_NEXT (fns
))
1725 error ("candidate is: %+#D", OVL_CURRENT (fn
));
1729 *str
= _("candidates are:");
1730 spaces
= get_spaces (*str
);
1732 error ("%s %+#D", *str
, OVL_CURRENT (fn
));
1733 *str
= spaces
? spaces
: *str
;
1743 /* Print the list of candidate FNS in an error message. */
1746 print_candidates (tree fns
)
1748 const char *str
= NULL
;
1749 print_candidates_1 (fns
, false, &str
);
1750 gcc_assert (str
== NULL
);
1753 /* Returns the template (one of the functions given by TEMPLATE_ID)
1754 which can be specialized to match the indicated DECL with the
1755 explicit template args given in TEMPLATE_ID. The DECL may be
1756 NULL_TREE if none is available. In that case, the functions in
1757 TEMPLATE_ID are non-members.
1759 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1760 specialization of a member template.
1762 The TEMPLATE_COUNT is the number of references to qualifying
1763 template classes that appeared in the name of the function. See
1764 check_explicit_specialization for a more accurate description.
1766 TSK indicates what kind of template declaration (if any) is being
1767 declared. TSK_TEMPLATE indicates that the declaration given by
1768 DECL, though a FUNCTION_DECL, has template parameters, and is
1769 therefore a template function.
1771 The template args (those explicitly specified and those deduced)
1772 are output in a newly created vector *TARGS_OUT.
1774 If it is impossible to determine the result, an error message is
1775 issued. The error_mark_node is returned to indicate failure. */
1778 determine_specialization (tree template_id
,
1781 int need_member_template
,
1787 tree explicit_targs
;
1788 tree candidates
= NULL_TREE
;
1789 /* A TREE_LIST of templates of which DECL may be a specialization.
1790 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1791 corresponding TREE_PURPOSE is the set of template arguments that,
1792 when used to instantiate the template, would produce a function
1793 with the signature of DECL. */
1794 tree templates
= NULL_TREE
;
1796 struct cp_binding_level
*b
;
1798 *targs_out
= NULL_TREE
;
1800 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1801 return error_mark_node
;
1803 fns
= TREE_OPERAND (template_id
, 0);
1804 explicit_targs
= TREE_OPERAND (template_id
, 1);
1806 if (fns
== error_mark_node
)
1807 return error_mark_node
;
1809 /* Check for baselinks. */
1810 if (BASELINK_P (fns
))
1811 fns
= BASELINK_FUNCTIONS (fns
);
1813 if (!is_overloaded_fn (fns
))
1815 error ("%qD is not a function template", fns
);
1816 return error_mark_node
;
1819 /* Count the number of template headers specified for this
1822 for (b
= current_binding_level
;
1823 b
->kind
== sk_template_parms
;
1827 for (; fns
; fns
= OVL_NEXT (fns
))
1829 tree fn
= OVL_CURRENT (fns
);
1831 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1833 tree decl_arg_types
;
1836 /* In case of explicit specialization, we need to check if
1837 the number of template headers appearing in the specialization
1838 is correct. This is usually done in check_explicit_specialization,
1839 but the check done there cannot be exhaustive when specializing
1840 member functions. Consider the following code:
1842 template <> void A<int>::f(int);
1843 template <> template <> void A<int>::f(int);
1845 Assuming that A<int> is not itself an explicit specialization
1846 already, the first line specializes "f" which is a non-template
1847 member function, whilst the second line specializes "f" which
1848 is a template member function. So both lines are syntactically
1849 correct, and check_explicit_specialization does not reject
1852 Here, we can do better, as we are matching the specialization
1853 against the declarations. We count the number of template
1854 headers, and we check if they match TEMPLATE_COUNT + 1
1855 (TEMPLATE_COUNT is the number of qualifying template classes,
1856 plus there must be another header for the member template
1859 Notice that if header_count is zero, this is not a
1860 specialization but rather a template instantiation, so there
1861 is no check we can perform here. */
1862 if (header_count
&& header_count
!= template_count
+ 1)
1865 /* Check that the number of template arguments at the
1866 innermost level for DECL is the same as for FN. */
1867 if (current_binding_level
->kind
== sk_template_parms
1868 && !current_binding_level
->explicit_spec_p
1869 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1870 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1871 (current_template_parms
))))
1874 /* DECL might be a specialization of FN. */
1875 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1876 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1878 /* For a non-static member function, we need to make sure
1879 that the const qualification is the same. Since
1880 get_bindings does not try to merge the "this" parameter,
1881 we must do the comparison explicitly. */
1882 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1883 && !same_type_p (TREE_VALUE (fn_arg_types
),
1884 TREE_VALUE (decl_arg_types
)))
1887 /* Skip the "this" parameter and, for constructors of
1888 classes with virtual bases, the VTT parameter. A
1889 full specialization of a constructor will have a VTT
1890 parameter, but a template never will. */
1892 = skip_artificial_parms_for (decl
, decl_arg_types
);
1894 = skip_artificial_parms_for (fn
, fn_arg_types
);
1896 /* Check that the number of function parameters matches.
1898 template <class T> void f(int i = 0);
1899 template <> void f<int>();
1900 The specialization f<int> is invalid but is not caught
1901 by get_bindings below. */
1902 if (list_length (fn_arg_types
) != list_length (decl_arg_types
))
1905 /* Function templates cannot be specializations; there are
1906 no partial specializations of functions. Therefore, if
1907 the type of DECL does not match FN, there is no
1909 if (tsk
== tsk_template
)
1911 if (compparms (fn_arg_types
, decl_arg_types
))
1912 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1916 /* See whether this function might be a specialization of this
1918 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1921 /* We cannot deduce template arguments that when used to
1922 specialize TMPL will produce DECL. */
1925 /* Save this template, and the arguments deduced. */
1926 templates
= tree_cons (targs
, fn
, templates
);
1928 else if (need_member_template
)
1929 /* FN is an ordinary member function, and we need a
1930 specialization of a member template. */
1932 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1933 /* We can get IDENTIFIER_NODEs here in certain erroneous
1936 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1937 /* This is just an ordinary non-member function. Nothing can
1938 be a specialization of that. */
1940 else if (DECL_ARTIFICIAL (fn
))
1941 /* Cannot specialize functions that are created implicitly. */
1945 tree decl_arg_types
;
1947 /* This is an ordinary member function. However, since
1948 we're here, we can assume it's enclosing class is a
1949 template class. For example,
1951 template <typename T> struct S { void f(); };
1952 template <> void S<int>::f() {}
1954 Here, S<int>::f is a non-template, but S<int> is a
1955 template class. If FN has the same type as DECL, we
1956 might be in business. */
1958 if (!DECL_TEMPLATE_INFO (fn
))
1959 /* Its enclosing class is an explicit specialization
1960 of a template class. This is not a candidate. */
1963 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
1964 TREE_TYPE (TREE_TYPE (fn
))))
1965 /* The return types differ. */
1968 /* Adjust the type of DECL in case FN is a static member. */
1969 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1970 if (DECL_STATIC_FUNCTION_P (fn
)
1971 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1972 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1974 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1977 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1981 if (templates
&& TREE_CHAIN (templates
))
1987 It is possible for a specialization with a given function
1988 signature to be instantiated from more than one function
1989 template. In such cases, explicit specification of the
1990 template arguments must be used to uniquely identify the
1991 function template specialization being specialized.
1993 Note that here, there's no suggestion that we're supposed to
1994 determine which of the candidate templates is most
1995 specialized. However, we, also have:
1999 Partial ordering of overloaded function template
2000 declarations is used in the following contexts to select
2001 the function template to which a function template
2002 specialization refers:
2004 -- when an explicit specialization refers to a function
2007 So, we do use the partial ordering rules, at least for now.
2008 This extension can only serve to make invalid programs valid,
2009 so it's safe. And, there is strong anecdotal evidence that
2010 the committee intended the partial ordering rules to apply;
2011 the EDG front end has that behavior, and John Spicer claims
2012 that the committee simply forgot to delete the wording in
2013 [temp.expl.spec]. */
2014 tree tmpl
= most_specialized_instantiation (templates
);
2015 if (tmpl
!= error_mark_node
)
2018 TREE_CHAIN (templates
) = NULL_TREE
;
2022 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2024 error ("template-id %qD for %q+D does not match any template "
2025 "declaration", template_id
, decl
);
2026 if (header_count
&& header_count
!= template_count
+ 1)
2027 inform (input_location
, "saw %d %<template<>%>, need %d for "
2028 "specializing a member function template",
2029 header_count
, template_count
+ 1);
2030 return error_mark_node
;
2032 else if ((templates
&& TREE_CHAIN (templates
))
2033 || (candidates
&& TREE_CHAIN (candidates
))
2034 || (templates
&& candidates
))
2036 error ("ambiguous template specialization %qD for %q+D",
2038 candidates
= chainon (candidates
, templates
);
2039 print_candidates (candidates
);
2040 return error_mark_node
;
2043 /* We have one, and exactly one, match. */
2046 tree fn
= TREE_VALUE (candidates
);
2047 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2048 /* DECL is a re-declaration or partial instantiation of a template
2050 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2052 /* It was a specialization of an ordinary member function in a
2054 return DECL_TI_TEMPLATE (fn
);
2057 /* It was a specialization of a template. */
2058 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2059 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2061 *targs_out
= copy_node (targs
);
2062 SET_TMPL_ARGS_LEVEL (*targs_out
,
2063 TMPL_ARGS_DEPTH (*targs_out
),
2064 TREE_PURPOSE (templates
));
2067 *targs_out
= TREE_PURPOSE (templates
);
2068 return TREE_VALUE (templates
);
2071 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2072 but with the default argument values filled in from those in the
2076 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2079 tree new_spec_types
;
2084 if (spec_types
== void_list_node
)
2085 return void_list_node
;
2087 /* Substitute into the rest of the list. */
2089 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2090 TREE_CHAIN (tmpl_types
));
2092 /* Add the default argument for this parameter. */
2093 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2094 TREE_VALUE (spec_types
),
2098 /* DECL is an explicit specialization. Replicate default arguments
2099 from the template it specializes. (That way, code like:
2101 template <class T> void f(T = 3);
2102 template <> void f(double);
2105 works, as required.) An alternative approach would be to look up
2106 the correct default arguments at the call-site, but this approach
2107 is consistent with how implicit instantiations are handled. */
2110 copy_default_args_to_explicit_spec (tree decl
)
2115 tree new_spec_types
;
2119 tree object_type
= NULL_TREE
;
2120 tree in_charge
= NULL_TREE
;
2121 tree vtt
= NULL_TREE
;
2123 /* See if there's anything we need to do. */
2124 tmpl
= DECL_TI_TEMPLATE (decl
);
2125 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2126 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2127 if (TREE_PURPOSE (t
))
2132 old_type
= TREE_TYPE (decl
);
2133 spec_types
= TYPE_ARG_TYPES (old_type
);
2135 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2137 /* Remove the this pointer, but remember the object's type for
2139 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2140 spec_types
= TREE_CHAIN (spec_types
);
2141 tmpl_types
= TREE_CHAIN (tmpl_types
);
2143 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2145 /* DECL may contain more parameters than TMPL due to the extra
2146 in-charge parameter in constructors and destructors. */
2147 in_charge
= spec_types
;
2148 spec_types
= TREE_CHAIN (spec_types
);
2150 if (DECL_HAS_VTT_PARM_P (decl
))
2153 spec_types
= TREE_CHAIN (spec_types
);
2157 /* Compute the merged default arguments. */
2159 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2161 /* Compute the new FUNCTION_TYPE. */
2165 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2170 /* Put the in-charge parameter back. */
2171 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2172 TREE_VALUE (in_charge
),
2175 new_type
= build_method_type_directly (object_type
,
2176 TREE_TYPE (old_type
),
2180 new_type
= build_function_type (TREE_TYPE (old_type
),
2182 new_type
= cp_build_type_attribute_variant (new_type
,
2183 TYPE_ATTRIBUTES (old_type
));
2184 new_type
= build_exception_variant (new_type
,
2185 TYPE_RAISES_EXCEPTIONS (old_type
));
2186 TREE_TYPE (decl
) = new_type
;
2189 /* Check to see if the function just declared, as indicated in
2190 DECLARATOR, and in DECL, is a specialization of a function
2191 template. We may also discover that the declaration is an explicit
2192 instantiation at this point.
2194 Returns DECL, or an equivalent declaration that should be used
2195 instead if all goes well. Issues an error message if something is
2196 amiss. Returns error_mark_node if the error is not easily
2199 FLAGS is a bitmask consisting of the following flags:
2201 2: The function has a definition.
2202 4: The function is a friend.
2204 The TEMPLATE_COUNT is the number of references to qualifying
2205 template classes that appeared in the name of the function. For
2208 template <class T> struct S { void f(); };
2211 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2212 classes are not counted in the TEMPLATE_COUNT, so that in
2214 template <class T> struct S {};
2215 template <> struct S<int> { void f(); }
2216 template <> void S<int>::f();
2218 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2219 invalid; there should be no template <>.)
2221 If the function is a specialization, it is marked as such via
2222 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2223 is set up correctly, and it is added to the list of specializations
2224 for that template. */
2227 check_explicit_specialization (tree declarator
,
2232 int have_def
= flags
& 2;
2233 int is_friend
= flags
& 4;
2234 int specialization
= 0;
2235 int explicit_instantiation
= 0;
2236 int member_specialization
= 0;
2237 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2238 tree dname
= DECL_NAME (decl
);
2243 if (!processing_specialization
)
2246 tsk
= tsk_excessive_parms
;
2249 tsk
= current_tmpl_spec_kind (template_count
);
2254 if (processing_specialization
)
2257 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2259 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2262 /* This could be something like:
2264 template <class T> void f(T);
2265 class S { friend void f<>(int); } */
2269 /* This case handles bogus declarations like template <>
2270 template <class T> void f<int>(); */
2272 error ("template-id %qD in declaration of primary template",
2279 case tsk_invalid_member_spec
:
2280 /* The error has already been reported in
2281 check_specialization_scope. */
2282 return error_mark_node
;
2284 case tsk_invalid_expl_inst
:
2285 error ("template parameter list used in explicit instantiation");
2291 error ("definition provided for explicit instantiation");
2293 explicit_instantiation
= 1;
2296 case tsk_excessive_parms
:
2297 case tsk_insufficient_parms
:
2298 if (tsk
== tsk_excessive_parms
)
2299 error ("too many template parameter lists in declaration of %qD",
2301 else if (template_header_count
)
2302 error("too few template parameter lists in declaration of %qD", decl
);
2304 error("explicit specialization of %qD must be introduced by "
2305 "%<template <>%>", decl
);
2309 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2311 member_specialization
= 1;
2317 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2319 /* This case handles bogus declarations like template <>
2320 template <class T> void f<int>(); */
2322 if (uses_template_parms (declarator
))
2323 error ("function template partial specialization %qD "
2324 "is not allowed", declarator
);
2326 error ("template-id %qD in declaration of primary template",
2331 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2332 /* This is a specialization of a member template, without
2333 specialization the containing class. Something like:
2335 template <class T> struct S {
2336 template <class U> void f (U);
2338 template <> template <class U> void S<int>::f(U) {}
2340 That's a specialization -- but of the entire template. */
2348 if (specialization
|| member_specialization
)
2350 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2351 for (; t
; t
= TREE_CHAIN (t
))
2352 if (TREE_PURPOSE (t
))
2354 permerror (input_location
,
2355 "default argument specified in explicit specialization");
2360 if (specialization
|| member_specialization
|| explicit_instantiation
)
2362 tree tmpl
= NULL_TREE
;
2363 tree targs
= NULL_TREE
;
2365 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2366 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2370 gcc_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
);
2375 /* If there is no class context, the explicit instantiation
2376 must be at namespace scope. */
2377 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2379 /* Find the namespace binding, using the declaration
2381 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2383 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2385 error ("%qD is not a template function", dname
);
2386 fns
= error_mark_node
;
2390 tree fn
= OVL_CURRENT (fns
);
2391 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2392 CP_DECL_CONTEXT (fn
)))
2393 error ("%qD is not declared in %qD",
2394 decl
, current_namespace
);
2398 declarator
= lookup_template_function (fns
, NULL_TREE
);
2401 if (declarator
== error_mark_node
)
2402 return error_mark_node
;
2404 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2406 if (!explicit_instantiation
)
2407 /* A specialization in class scope. This is invalid,
2408 but the error will already have been flagged by
2409 check_specialization_scope. */
2410 return error_mark_node
;
2413 /* It's not valid to write an explicit instantiation in
2416 class C { template void f(); }
2418 This case is caught by the parser. However, on
2421 template class C { void f(); };
2423 (which is invalid) we can get here. The error will be
2430 else if (ctype
!= NULL_TREE
2431 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
2434 /* Find the list of functions in ctype that have the same
2435 name as the declared function. */
2436 tree name
= TREE_OPERAND (declarator
, 0);
2437 tree fns
= NULL_TREE
;
2440 if (constructor_name_p (name
, ctype
))
2442 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2444 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2445 : !CLASSTYPE_DESTRUCTORS (ctype
))
2447 /* From [temp.expl.spec]:
2449 If such an explicit specialization for the member
2450 of a class template names an implicitly-declared
2451 special member function (clause _special_), the
2452 program is ill-formed.
2454 Similar language is found in [temp.explicit]. */
2455 error ("specialization of implicitly-declared special member function");
2456 return error_mark_node
;
2459 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2462 if (!DECL_CONV_FN_P (decl
))
2464 idx
= lookup_fnfields_1 (ctype
, name
);
2466 fns
= VEC_index (tree
, CLASSTYPE_METHOD_VEC (ctype
), idx
);
2470 VEC(tree
,gc
) *methods
;
2473 /* For a type-conversion operator, we cannot do a
2474 name-based lookup. We might be looking for `operator
2475 int' which will be a specialization of `operator T'.
2476 So, we find *all* the conversion operators, and then
2477 select from them. */
2480 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2482 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2483 VEC_iterate (tree
, methods
, idx
, ovl
);
2486 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2487 /* There are no more conversion functions. */
2490 /* Glue all these conversion functions together
2491 with those we already have. */
2492 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2493 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2497 if (fns
== NULL_TREE
)
2499 error ("no member function %qD declared in %qT", name
, ctype
);
2500 return error_mark_node
;
2503 TREE_OPERAND (declarator
, 0) = fns
;
2506 /* Figure out what exactly is being specialized at this point.
2507 Note that for an explicit instantiation, even one for a
2508 member function, we cannot tell apriori whether the
2509 instantiation is for a member template, or just a member
2510 function of a template class. Even if a member template is
2511 being instantiated, the member template arguments may be
2512 elided if they can be deduced from the rest of the
2514 tmpl
= determine_specialization (declarator
, decl
,
2516 member_specialization
,
2520 if (!tmpl
|| tmpl
== error_mark_node
)
2521 /* We couldn't figure out what this declaration was
2523 return error_mark_node
;
2526 tree gen_tmpl
= most_general_template (tmpl
);
2528 if (explicit_instantiation
)
2530 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2531 is done by do_decl_instantiation later. */
2533 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2534 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2536 if (arg_depth
> parm_depth
)
2538 /* If TMPL is not the most general template (for
2539 example, if TMPL is a friend template that is
2540 injected into namespace scope), then there will
2541 be too many levels of TARGS. Remove some of them
2546 new_targs
= make_tree_vec (parm_depth
);
2547 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2548 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2549 = TREE_VEC_ELT (targs
, i
);
2553 return instantiate_template (tmpl
, targs
, tf_error
);
2556 /* If we thought that the DECL was a member function, but it
2557 turns out to be specializing a static member function,
2558 make DECL a static member function as well. */
2559 if (DECL_STATIC_FUNCTION_P (tmpl
)
2560 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2561 revert_static_member_fn (decl
);
2563 /* If this is a specialization of a member template of a
2564 template class, we want to return the TEMPLATE_DECL, not
2565 the specialization of it. */
2566 if (tsk
== tsk_template
)
2568 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2569 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2570 DECL_INITIAL (result
) = NULL_TREE
;
2574 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2575 DECL_SOURCE_LOCATION (result
)
2576 = DECL_SOURCE_LOCATION (decl
);
2577 /* We want to use the argument list specified in the
2578 definition, not in the original declaration. */
2579 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2580 for (parm
= DECL_ARGUMENTS (result
); parm
;
2581 parm
= DECL_CHAIN (parm
))
2582 DECL_CONTEXT (parm
) = result
;
2584 return register_specialization (tmpl
, gen_tmpl
, targs
,
2588 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2589 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2591 /* Inherit default function arguments from the template
2592 DECL is specializing. */
2593 copy_default_args_to_explicit_spec (decl
);
2595 /* This specialization has the same protection as the
2596 template it specializes. */
2597 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2598 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2600 /* 7.1.1-1 [dcl.stc]
2602 A storage-class-specifier shall not be specified in an
2603 explicit specialization...
2605 The parser rejects these, so unless action is taken here,
2606 explicit function specializations will always appear with
2609 The action recommended by the C++ CWG in response to C++
2610 defect report 605 is to make the storage class and linkage
2611 of the explicit specialization match the templated function:
2613 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2615 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2617 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2618 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2620 /* This specialization has the same linkage and visibility as
2621 the function template it specializes. */
2622 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2623 if (! TREE_PUBLIC (decl
))
2625 DECL_INTERFACE_KNOWN (decl
) = 1;
2626 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2628 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2629 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2631 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2632 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2636 /* If DECL is a friend declaration, declared using an
2637 unqualified name, the namespace associated with DECL may
2638 have been set incorrectly. For example, in:
2640 template <typename T> void f(T);
2642 struct S { friend void f<int>(int); }
2645 we will have set the DECL_CONTEXT for the friend
2646 declaration to N, rather than to the global namespace. */
2647 if (DECL_NAMESPACE_SCOPE_P (decl
))
2648 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2650 if (is_friend
&& !have_def
)
2651 /* This is not really a declaration of a specialization.
2652 It's just the name of an instantiation. But, it's not
2653 a request for an instantiation, either. */
2654 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2655 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2656 /* This is indeed a specialization. In case of constructors
2657 and destructors, we need in-charge and not-in-charge
2658 versions in V3 ABI. */
2659 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2661 /* Register this specialization so that we can find it
2663 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2670 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2671 parameters. These are represented in the same format used for
2672 DECL_TEMPLATE_PARMS. */
2675 comp_template_parms (const_tree parms1
, const_tree parms2
)
2680 if (parms1
== parms2
)
2683 for (p1
= parms1
, p2
= parms2
;
2684 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2685 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2687 tree t1
= TREE_VALUE (p1
);
2688 tree t2
= TREE_VALUE (p2
);
2691 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2692 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2694 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2697 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2699 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2700 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2702 /* If either of the template parameters are invalid, assume
2703 they match for the sake of error recovery. */
2704 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2707 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2710 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2711 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2712 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2714 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2719 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2720 /* One set of parameters has more parameters lists than the
2727 /* Determine whether PARM is a parameter pack. */
2730 template_parameter_pack_p (const_tree parm
)
2732 /* Determine if we have a non-type template parameter pack. */
2733 if (TREE_CODE (parm
) == PARM_DECL
)
2734 return (DECL_TEMPLATE_PARM_P (parm
)
2735 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2737 /* If this is a list of template parameters, we could get a
2738 TYPE_DECL or a TEMPLATE_DECL. */
2739 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2740 parm
= TREE_TYPE (parm
);
2742 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2743 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2744 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2747 /* Determine if T is a function parameter pack. */
2750 function_parameter_pack_p (const_tree t
)
2752 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2753 return FUNCTION_PARAMETER_PACK_P (t
);
2757 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2758 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2761 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2763 if (! primary_func_tmpl_inst
2764 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2765 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2768 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2771 /* Return true iff the function parameter PARAM_DECL was expanded
2772 from the function parameter pack PACK. */
2775 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
2777 if (DECL_ARTIFICIAL (param_decl
)
2778 || !function_parameter_pack_p (pack
))
2781 /* The parameter pack and its pack arguments have the same
2783 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
2786 /* Determine whether ARGS describes a variadic template args list,
2787 i.e., one that is terminated by a template argument pack. */
2790 template_args_variadic_p (tree args
)
2795 if (args
== NULL_TREE
)
2798 args
= INNERMOST_TEMPLATE_ARGS (args
);
2799 nargs
= TREE_VEC_LENGTH (args
);
2804 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2806 return ARGUMENT_PACK_P (last_parm
);
2809 /* Generate a new name for the parameter pack name NAME (an
2810 IDENTIFIER_NODE) that incorporates its */
2813 make_ith_pack_parameter_name (tree name
, int i
)
2815 /* Munge the name to include the parameter index. */
2816 #define NUMBUF_LEN 128
2817 char numbuf
[NUMBUF_LEN
];
2821 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
2822 newname_len
= IDENTIFIER_LENGTH (name
)
2823 + strlen (numbuf
) + 2;
2824 newname
= (char*)alloca (newname_len
);
2825 snprintf (newname
, newname_len
,
2826 "%s#%i", IDENTIFIER_POINTER (name
), i
);
2827 return get_identifier (newname
);
2830 /* Return true if T is a primary function
2831 or class template instantiation. */
2834 primary_template_instantiation_p (const_tree t
)
2839 if (TREE_CODE (t
) == FUNCTION_DECL
)
2840 return DECL_LANG_SPECIFIC (t
)
2841 && DECL_TEMPLATE_INSTANTIATION (t
)
2842 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
2843 else if (CLASS_TYPE_P (t
))
2844 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
2845 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
2849 /* Return true if PARM is a template template parameter. */
2852 template_template_parameter_p (const_tree parm
)
2854 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
2857 /* Return the template parameters of T if T is a
2858 primary template instantiation, NULL otherwise. */
2861 get_primary_template_innermost_parameters (const_tree t
)
2863 tree parms
= NULL
, template_info
= NULL
;
2865 if ((template_info
= get_template_info (t
))
2866 && primary_template_instantiation_p (t
))
2867 parms
= INNERMOST_TEMPLATE_PARMS
2868 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
2873 /* Return the template parameters of the LEVELth level from the full list
2874 of template parameters PARMS. */
2877 get_template_parms_at_level (tree parms
, int level
)
2881 || TREE_CODE (parms
) != TREE_LIST
2882 || level
> TMPL_PARMS_DEPTH (parms
))
2885 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
2886 if (TMPL_PARMS_DEPTH (p
) == level
)
2892 /* Returns the template arguments of T if T is a template instantiation,
2896 get_template_innermost_arguments (const_tree t
)
2898 tree args
= NULL
, template_info
= NULL
;
2900 if ((template_info
= get_template_info (t
))
2901 && TI_ARGS (template_info
))
2902 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
2907 /* Return the argument pack elements of T if T is a template argument pack,
2911 get_template_argument_pack_elems (const_tree t
)
2913 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
2914 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
2917 return ARGUMENT_PACK_ARGS (t
);
2920 /* Structure used to track the progress of find_parameter_packs_r. */
2921 struct find_parameter_pack_data
2923 /* TREE_LIST that will contain all of the parameter packs found by
2925 tree
* parameter_packs
;
2927 /* Set of AST nodes that have been visited by the traversal. */
2928 struct pointer_set_t
*visited
;
2931 /* Identifies all of the argument packs that occur in a template
2932 argument and appends them to the TREE_LIST inside DATA, which is a
2933 find_parameter_pack_data structure. This is a subroutine of
2934 make_pack_expansion and uses_parameter_packs. */
2936 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
2939 struct find_parameter_pack_data
* ppd
=
2940 (struct find_parameter_pack_data
*)data
;
2941 bool parameter_pack_p
= false;
2943 /* Identify whether this is a parameter pack or not. */
2944 switch (TREE_CODE (t
))
2946 case TEMPLATE_PARM_INDEX
:
2947 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
2948 parameter_pack_p
= true;
2951 case TEMPLATE_TYPE_PARM
:
2952 case TEMPLATE_TEMPLATE_PARM
:
2953 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
2954 parameter_pack_p
= true;
2958 if (FUNCTION_PARAMETER_PACK_P (t
))
2960 /* We don't want to walk into the type of a PARM_DECL,
2961 because we don't want to see the type parameter pack. */
2963 parameter_pack_p
= true;
2968 /* Not a parameter pack. */
2972 if (parameter_pack_p
)
2974 /* Add this parameter pack to the list. */
2975 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
2979 cp_walk_tree (&TYPE_CONTEXT (t
),
2980 &find_parameter_packs_r
, ppd
, ppd
->visited
);
2982 /* This switch statement will return immediately if we don't find a
2984 switch (TREE_CODE (t
))
2986 case TEMPLATE_PARM_INDEX
:
2989 case BOUND_TEMPLATE_TEMPLATE_PARM
:
2990 /* Check the template itself. */
2991 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
2992 &find_parameter_packs_r
, ppd
, ppd
->visited
);
2993 /* Check the template arguments. */
2994 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
2999 case TEMPLATE_TYPE_PARM
:
3000 case TEMPLATE_TEMPLATE_PARM
:
3007 if (TYPE_PTRMEMFUNC_P (t
))
3013 if (TYPE_TEMPLATE_INFO (t
))
3014 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
3015 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3021 cp_walk_tree (&TREE_TYPE (t
),
3022 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3026 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3031 case TYPE_PACK_EXPANSION
:
3032 case EXPR_PACK_EXPANSION
:
3037 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3042 case IDENTIFIER_NODE
:
3043 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3055 /* Determines if the expression or type T uses any parameter packs. */
3057 uses_parameter_packs (tree t
)
3059 tree parameter_packs
= NULL_TREE
;
3060 struct find_parameter_pack_data ppd
;
3061 ppd
.parameter_packs
= ¶meter_packs
;
3062 ppd
.visited
= pointer_set_create ();
3063 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3064 pointer_set_destroy (ppd
.visited
);
3065 return parameter_packs
!= NULL_TREE
;
3068 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3069 representation a base-class initializer into a parameter pack
3070 expansion. If all goes well, the resulting node will be an
3071 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3074 make_pack_expansion (tree arg
)
3077 tree parameter_packs
= NULL_TREE
;
3078 bool for_types
= false;
3079 struct find_parameter_pack_data ppd
;
3081 if (!arg
|| arg
== error_mark_node
)
3084 if (TREE_CODE (arg
) == TREE_LIST
)
3086 /* The only time we will see a TREE_LIST here is for a base
3087 class initializer. In this case, the TREE_PURPOSE will be a
3088 _TYPE node (representing the base class expansion we're
3089 initializing) and the TREE_VALUE will be a TREE_LIST
3090 containing the initialization arguments.
3092 The resulting expansion looks somewhat different from most
3093 expansions. Rather than returning just one _EXPANSION, we
3094 return a TREE_LIST whose TREE_PURPOSE is a
3095 TYPE_PACK_EXPANSION containing the bases that will be
3096 initialized. The TREE_VALUE will be identical to the
3097 original TREE_VALUE, which is a list of arguments that will
3098 be passed to each base. We do not introduce any new pack
3099 expansion nodes into the TREE_VALUE (although it is possible
3100 that some already exist), because the TREE_PURPOSE and
3101 TREE_VALUE all need to be expanded together with the same
3102 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3103 resulting TREE_PURPOSE will mention the parameter packs in
3104 both the bases and the arguments to the bases. */
3107 tree parameter_packs
= NULL_TREE
;
3109 /* Determine which parameter packs will be used by the base
3111 ppd
.visited
= pointer_set_create ();
3112 ppd
.parameter_packs
= ¶meter_packs
;
3113 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3116 if (parameter_packs
== NULL_TREE
)
3118 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3119 pointer_set_destroy (ppd
.visited
);
3120 return error_mark_node
;
3123 if (TREE_VALUE (arg
) != void_type_node
)
3125 /* Collect the sets of parameter packs used in each of the
3126 initialization arguments. */
3127 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3129 /* Determine which parameter packs will be expanded in this
3131 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3136 pointer_set_destroy (ppd
.visited
);
3138 /* Create the pack expansion type for the base type. */
3139 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3140 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3141 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3143 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3144 they will rarely be compared to anything. */
3145 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3147 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3150 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3153 /* Build the PACK_EXPANSION_* node. */
3155 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3156 : make_node (EXPR_PACK_EXPANSION
);
3157 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3158 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3160 /* Propagate type and const-expression information. */
3161 TREE_TYPE (result
) = TREE_TYPE (arg
);
3162 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3165 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3166 they will rarely be compared to anything. */
3167 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3169 /* Determine which parameter packs will be expanded. */
3170 ppd
.parameter_packs
= ¶meter_packs
;
3171 ppd
.visited
= pointer_set_create ();
3172 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3173 pointer_set_destroy (ppd
.visited
);
3175 /* Make sure we found some parameter packs. */
3176 if (parameter_packs
== NULL_TREE
)
3179 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3181 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3182 return error_mark_node
;
3184 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3189 /* Checks T for any "bare" parameter packs, which have not yet been
3190 expanded, and issues an error if any are found. This operation can
3191 only be done on full expressions or types (e.g., an expression
3192 statement, "if" condition, etc.), because we could have expressions like:
3194 foo(f(g(h(args)))...)
3196 where "args" is a parameter pack. check_for_bare_parameter_packs
3197 should not be called for the subexpressions args, h(args),
3198 g(h(args)), or f(g(h(args))), because we would produce erroneous
3201 Returns TRUE and emits an error if there were bare parameter packs,
3202 returns FALSE otherwise. */
3204 check_for_bare_parameter_packs (tree t
)
3206 tree parameter_packs
= NULL_TREE
;
3207 struct find_parameter_pack_data ppd
;
3209 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3212 if (TREE_CODE (t
) == TYPE_DECL
)
3215 ppd
.parameter_packs
= ¶meter_packs
;
3216 ppd
.visited
= pointer_set_create ();
3217 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3218 pointer_set_destroy (ppd
.visited
);
3220 if (parameter_packs
)
3222 error ("parameter packs not expanded with %<...%>:");
3223 while (parameter_packs
)
3225 tree pack
= TREE_VALUE (parameter_packs
);
3226 tree name
= NULL_TREE
;
3228 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3229 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3230 name
= TYPE_NAME (pack
);
3231 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3232 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3234 name
= DECL_NAME (pack
);
3237 inform (input_location
, " %qD", name
);
3239 inform (input_location
, " <anonymous>");
3241 parameter_packs
= TREE_CHAIN (parameter_packs
);
3250 /* Expand any parameter packs that occur in the template arguments in
3253 expand_template_argument_pack (tree args
)
3255 tree result_args
= NULL_TREE
;
3256 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3257 int num_result_args
= -1;
3258 int non_default_args_count
= -1;
3260 /* First, determine if we need to expand anything, and the number of
3261 slots we'll need. */
3262 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3264 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3265 if (arg
== NULL_TREE
)
3267 if (ARGUMENT_PACK_P (arg
))
3269 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3270 if (num_result_args
< 0)
3271 num_result_args
= in_arg
+ num_packed
;
3273 num_result_args
+= num_packed
;
3277 if (num_result_args
>= 0)
3282 /* If no expansion is necessary, we're done. */
3283 if (num_result_args
< 0)
3286 /* Expand arguments. */
3287 result_args
= make_tree_vec (num_result_args
);
3288 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3289 non_default_args_count
=
3290 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3291 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3293 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3294 if (ARGUMENT_PACK_P (arg
))
3296 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3297 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3298 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3299 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3300 if (non_default_args_count
> 0)
3301 non_default_args_count
+= num_packed
;
3305 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3309 if (non_default_args_count
>= 0)
3310 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3314 /* Checks if DECL shadows a template parameter.
3316 [temp.local]: A template-parameter shall not be redeclared within its
3317 scope (including nested scopes).
3319 Emits an error and returns TRUE if the DECL shadows a parameter,
3320 returns FALSE otherwise. */
3323 check_template_shadow (tree decl
)
3327 /* If we're not in a template, we can't possibly shadow a template
3329 if (!current_template_parms
)
3332 /* Figure out what we're shadowing. */
3333 if (TREE_CODE (decl
) == OVERLOAD
)
3334 decl
= OVL_CURRENT (decl
);
3335 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3337 /* If there's no previous binding for this name, we're not shadowing
3338 anything, let alone a template parameter. */
3342 /* If we're not shadowing a template parameter, we're done. Note
3343 that OLDDECL might be an OVERLOAD (or perhaps even an
3344 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3346 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3349 /* We check for decl != olddecl to avoid bogus errors for using a
3350 name inside a class. We check TPFI to avoid duplicate errors for
3351 inline member templates. */
3353 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
3356 error ("declaration of %q+#D", decl
);
3357 error (" shadows template parm %q+#D", olddecl
);
3361 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3362 ORIG_LEVEL, DECL, and TYPE. NUM_SIBLINGS is the total number of
3363 template parameters. */
3366 build_template_parm_index (int index
,
3373 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3374 TEMPLATE_PARM_IDX (t
) = index
;
3375 TEMPLATE_PARM_LEVEL (t
) = level
;
3376 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3377 TEMPLATE_PARM_NUM_SIBLINGS (t
) = num_siblings
;
3378 TEMPLATE_PARM_DECL (t
) = decl
;
3379 TREE_TYPE (t
) = type
;
3380 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3381 TREE_READONLY (t
) = TREE_READONLY (decl
);
3386 /* Find the canonical type parameter for the given template type
3387 parameter. Returns the canonical type parameter, which may be TYPE
3388 if no such parameter existed. */
3391 canonical_type_parameter (tree type
)
3394 int idx
= TEMPLATE_TYPE_IDX (type
);
3395 if (!canonical_template_parms
)
3396 canonical_template_parms
= VEC_alloc (tree
, gc
, idx
+1);
3398 while (VEC_length (tree
, canonical_template_parms
) <= (unsigned)idx
)
3399 VEC_safe_push (tree
, gc
, canonical_template_parms
, NULL_TREE
);
3401 list
= VEC_index (tree
, canonical_template_parms
, idx
);
3402 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3403 list
= TREE_CHAIN (list
);
3406 return TREE_VALUE (list
);
3409 VEC_replace(tree
, canonical_template_parms
, idx
,
3410 tree_cons (NULL_TREE
, type
,
3411 VEC_index (tree
, canonical_template_parms
, idx
)));
3416 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3417 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3418 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3419 new one is created. */
3422 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3423 tsubst_flags_t complain
)
3425 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3426 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3427 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3428 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3430 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3433 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3434 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3435 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3436 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3437 DECL_ARTIFICIAL (decl
) = 1;
3438 SET_DECL_TEMPLATE_PARM_P (decl
);
3440 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3441 TEMPLATE_PARM_LEVEL (index
) - levels
,
3442 TEMPLATE_PARM_ORIG_LEVEL (index
),
3443 TEMPLATE_PARM_NUM_SIBLINGS (index
),
3445 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3446 TEMPLATE_PARM_PARAMETER_PACK (t
)
3447 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3449 /* Template template parameters need this. */
3450 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3451 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3452 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3456 return TEMPLATE_PARM_DESCENDANTS (index
);
3459 /* Process information from new template parameter PARM and append it
3460 to the LIST being built. This new parameter is a non-type
3461 parameter iff IS_NON_TYPE is true. This new parameter is a
3462 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3463 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3464 parameter list PARM belongs to. This is used used to create a
3465 proper canonical type for the type of PARM that is to be created,
3466 iff PARM is a type. If the size is not known, this parameter shall
3470 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
3471 bool is_non_type
, bool is_parameter_pack
,
3472 unsigned num_template_parms
)
3479 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3480 defval
= TREE_PURPOSE (parm
);
3484 tree p
= tree_last (list
);
3486 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3489 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3490 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3492 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3502 parm
= TREE_VALUE (parm
);
3504 SET_DECL_TEMPLATE_PARM_P (parm
);
3506 if (TREE_TYPE (parm
) == error_mark_node
)
3508 err_parm_list
= build_tree_list (defval
, parm
);
3509 TREE_VALUE (err_parm_list
) = error_mark_node
;
3510 return chainon (list
, err_parm_list
);
3516 The top-level cv-qualifiers on the template-parameter are
3517 ignored when determining its type. */
3518 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3519 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3521 err_parm_list
= build_tree_list (defval
, parm
);
3522 TREE_VALUE (err_parm_list
) = error_mark_node
;
3523 return chainon (list
, err_parm_list
);
3526 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
)
3528 /* This template parameter is not a parameter pack, but it
3529 should be. Complain about "bare" parameter packs. */
3530 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3532 /* Recover by calling this a parameter pack. */
3533 is_parameter_pack
= true;
3537 /* A template parameter is not modifiable. */
3538 TREE_CONSTANT (parm
) = 1;
3539 TREE_READONLY (parm
) = 1;
3540 decl
= build_decl (parm_loc
,
3541 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3542 TREE_CONSTANT (decl
) = 1;
3543 TREE_READONLY (decl
) = 1;
3544 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3545 = build_template_parm_index (idx
, processing_template_decl
,
3546 processing_template_decl
,
3548 decl
, TREE_TYPE (parm
));
3550 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3551 = is_parameter_pack
;
3556 parm
= TREE_VALUE (TREE_VALUE (parm
));
3558 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3560 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3561 /* This is for distinguishing between real templates and template
3562 template parameters */
3563 TREE_TYPE (parm
) = t
;
3564 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3569 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3570 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3571 decl
= build_decl (parm_loc
,
3572 TYPE_DECL
, parm
, t
);
3575 TYPE_NAME (t
) = decl
;
3576 TYPE_STUB_DECL (t
) = decl
;
3578 TEMPLATE_TYPE_PARM_INDEX (t
)
3579 = build_template_parm_index (idx
, processing_template_decl
,
3580 processing_template_decl
,
3582 decl
, TREE_TYPE (parm
));
3583 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3584 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3586 DECL_ARTIFICIAL (decl
) = 1;
3587 SET_DECL_TEMPLATE_PARM_P (decl
);
3589 parm
= build_tree_list (defval
, parm
);
3590 return chainon (list
, parm
);
3593 /* The end of a template parameter list has been reached. Process the
3594 tree list into a parameter vector, converting each parameter into a more
3595 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3599 end_template_parm_list (tree parms
)
3603 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3605 current_template_parms
3606 = tree_cons (size_int (processing_template_decl
),
3607 saved_parmlist
, current_template_parms
);
3609 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3611 next
= TREE_CHAIN (parm
);
3612 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3613 TREE_CHAIN (parm
) = NULL_TREE
;
3616 --processing_template_parmlist
;
3618 return saved_parmlist
;
3621 /* Create a new type almost identical to TYPE but which has the
3622 following differences:
3624 1/ T has a new TEMPLATE_PARM_INDEX that carries the new number of
3625 template sibling parameters of T.
3627 2/ T has a new canonical type that matches the new number
3630 3/ From now on, T is going to be what lookups referring to the
3631 name of TYPE will return. No lookup should return TYPE anymore.
3633 NUM_PARMS is the new number of sibling parms TYPE belongs to.
3635 This is a subroutine of fixup_template_parms. */
3638 fixup_template_type_parm_type (tree type
, int num_parms
)
3640 tree orig_idx
= TEMPLATE_TYPE_PARM_INDEX (type
), idx
;
3642 /* This is the decl which name is inserted into the symbol table for
3643 the template parm type. So whenever we lookup the type name, this
3644 is the DECL we get. */
3647 /* Do not fix up the type twice. */
3648 if (orig_idx
&& TEMPLATE_PARM_NUM_SIBLINGS (orig_idx
) != 0)
3651 t
= copy_type (type
);
3652 decl
= TYPE_NAME (t
);
3654 TYPE_MAIN_VARIANT (t
) = t
;
3655 TYPE_NEXT_VARIANT (t
)= NULL_TREE
;
3656 TYPE_POINTER_TO (t
) = 0;
3657 TYPE_REFERENCE_TO (t
) = 0;
3659 idx
= build_template_parm_index (TEMPLATE_PARM_IDX (orig_idx
),
3660 TEMPLATE_PARM_LEVEL (orig_idx
),
3661 TEMPLATE_PARM_ORIG_LEVEL (orig_idx
),
3664 TEMPLATE_PARM_DESCENDANTS (idx
) = TEMPLATE_PARM_DESCENDANTS (orig_idx
);
3665 TEMPLATE_PARM_PARAMETER_PACK (idx
) = TEMPLATE_PARM_PARAMETER_PACK (orig_idx
);
3666 TEMPLATE_TYPE_PARM_INDEX (t
) = idx
;
3668 TYPE_STUB_DECL (t
) = decl
;
3669 TEMPLATE_TYPE_DECL (t
) = decl
;
3670 if (TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
3671 TREE_TYPE (DECL_TEMPLATE_RESULT (decl
)) = t
;
3673 /* Update the type associated to the type name stored in the symbol
3674 table. Now, whenever the type name is looked up, the resulting
3675 type is properly fixed up. */
3676 TREE_TYPE (decl
) = t
;
3678 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3683 /* Create and return a new TEMPLATE_PARM_INDEX that is almost
3684 identical to I, but that is fixed up as to:
3686 1/ carry the number of sibling parms (NUM_PARMS) of the template
3687 parm represented by I.
3689 2/ replace all references to template parm types declared before I
3690 (in the same template parm list as I) by references to template
3691 parm types contained in ARGS. ARGS should contain the list of
3692 template parms that have been fixed up so far, in a form suitable
3693 to be passed to tsubst.
3695 This is a subroutine of fixup_template_parms. */
3698 fixup_template_parm_index (tree i
, tree args
, int num_parms
)
3700 tree index
, decl
, type
;
3703 || TREE_CODE (i
) != TEMPLATE_PARM_INDEX
3704 /* Do not fix up the index twice. */
3705 || (TEMPLATE_PARM_NUM_SIBLINGS (i
) != 0))
3708 decl
= TEMPLATE_PARM_DECL (i
);
3709 type
= TREE_TYPE (decl
);
3711 index
= build_template_parm_index (TEMPLATE_PARM_IDX (i
),
3712 TEMPLATE_PARM_LEVEL (i
),
3713 TEMPLATE_PARM_ORIG_LEVEL (i
),
3717 TEMPLATE_PARM_DESCENDANTS (index
) = TEMPLATE_PARM_DESCENDANTS (i
);
3718 TEMPLATE_PARM_PARAMETER_PACK (index
) = TEMPLATE_PARM_PARAMETER_PACK (i
);
3720 type
= tsubst (type
, args
, tf_none
, NULL_TREE
);
3722 TREE_TYPE (decl
) = type
;
3723 TREE_TYPE (index
) = type
;
3729 This is a subroutine of fixup_template_parms.
3731 It computes the canonical type of the type of the template
3732 parameter PARM_DESC and update all references to that type so that
3733 they use the newly computed canonical type. No access check is
3734 performed during the fixup. PARM_DESC is a TREE_LIST which
3735 TREE_VALUE is the template parameter and its TREE_PURPOSE is the
3736 default argument of the template parm if any. IDX is the index of
3737 the template parameter, starting at 0. NUM_PARMS is the number of
3738 template parameters in the set PARM_DESC belongs to. ARGLIST is a
3739 TREE_VEC containing the full set of template parameters in a form
3740 suitable to be passed to substs functions as their ARGS
3741 argument. This is what current_template_args returns for a given
3742 template. The innermost vector of args in ARGLIST is the set of
3743 template parms that have been fixed up so far. This function adds
3744 the fixed up parameter into that vector. */
3747 fixup_template_parm (tree parm_desc
,
3752 tree parm
= TREE_VALUE (parm_desc
);
3753 tree fixedup_args
= INNERMOST_TEMPLATE_ARGS (arglist
);
3755 push_deferring_access_checks (dk_no_check
);
3757 if (TREE_CODE (parm
) == TYPE_DECL
)
3759 /* PARM is a template type parameter. Fix up its type, add
3760 the fixed-up template parm to the vector of fixed-up
3761 template parms so far, and substitute the fixed-up
3762 template parms into the default argument of this
3765 fixup_template_type_parm_type (TREE_TYPE (parm
), num_parms
);
3766 TREE_TYPE (parm
) = t
;
3768 TREE_VEC_ELT (fixedup_args
, idx
) = template_parm_to_arg (parm_desc
);
3770 else if (TREE_CODE (parm
) == TEMPLATE_DECL
)
3772 /* PARM is a template template parameter. This is going to
3774 tree tparms
, targs
, innermost_args
, t
;
3777 /* First, fix up the parms of the template template parm
3778 because the parms are involved in defining the new canonical
3779 type of the template template parm. */
3781 /* So we need to substitute the template parm types that have
3782 been fixed up so far into the template parms of this template
3783 template parm. E.g, consider this:
3785 template<class T, template<T u> class TT> class S;
3787 In this case we want to substitute T into the
3788 template parameters of TT.
3790 So let's walk the template parms of PARM here, and
3791 tsubst ARGLIST into into each of the template
3794 /* For this substitution we need to build the full set of
3795 template parameters and use that as arguments for the
3796 tsubsting function. */
3797 tparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
3799 /* This will contain the innermost parms of PARM into which
3800 we have substituted so far. */
3801 innermost_args
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
3802 targs
= add_to_template_args (arglist
, innermost_args
);
3803 for (j
= 0; j
< TREE_VEC_LENGTH (tparms
); ++j
)
3807 parameter
= TREE_VEC_ELT (tparms
, j
);
3809 /* INNERMOST_ARGS needs to have at least the same number
3810 of elements as the index PARAMETER, ortherwise
3811 tsubsting into PARAMETER will result in partially
3812 instantiating it, reducing its tempate parm
3813 level. Let's tactically fill INNERMOST_ARGS for that
3815 TREE_VEC_ELT (innermost_args
, j
) =
3816 template_parm_to_arg (parameter
);
3818 fixup_template_parm (parameter
, j
,
3819 TREE_VEC_LENGTH (tparms
),
3823 /* Now fix up the type of the template template parm. */
3825 t
= fixup_template_type_parm_type (TREE_TYPE (parm
), num_parms
);
3826 TREE_TYPE (parm
) = t
;
3828 TREE_VEC_ELT (fixedup_args
, idx
) =
3829 template_parm_to_arg (parm_desc
);
3831 else if (TREE_CODE (parm
) == PARM_DECL
)
3833 /* PARM is a non-type template parameter. We need to:
3835 * Fix up its TEMPLATE_PARM_INDEX to make it carry the
3836 proper number of sibling parameters.
3838 * Make lookups of the template parameter return a reference
3839 to the fixed-up index. No lookup should return references
3840 to the former index anymore.
3842 * Substitute the template parms that got fixed up so far
3844 * into the type of PARM. */
3846 tree index
= DECL_INITIAL (parm
);
3848 /* PUSHED_DECL is the decl added to the symbol table with
3849 the name of the parameter. E,g:
3851 template<class T, T u> //#0
3852 auto my_function(T t) -> decltype(u); //#1
3854 Here, when looking up u at //#1, we get the decl of u
3855 resulting from the declaration in #0. This is what
3856 PUSHED_DECL is. We need to replace the reference to the
3857 old TEMPLATE_PARM_INDEX carried by PUSHED_DECL by the
3858 fixed-up TEMPLATE_PARM_INDEX. */
3859 tree pushed_decl
= TEMPLATE_PARM_DECL (index
);
3861 /* Let's fix up the TEMPLATE_PARM_INDEX then. Note that we must
3862 fixup the type of PUSHED_DECL as well and luckily
3863 fixup_template_parm_index does it for us too. */
3864 tree fixed_up_index
=
3865 fixup_template_parm_index (index
, arglist
, num_parms
);
3867 DECL_INITIAL (pushed_decl
) = DECL_INITIAL (parm
) = fixed_up_index
;
3869 /* Add this fixed up PARM to the template parms we've fixed
3870 up so far and use that to substitute the fixed-up
3871 template parms into the type of PARM. */
3872 TREE_VEC_ELT (fixedup_args
, idx
) =
3873 template_parm_to_arg (parm_desc
);
3874 TREE_TYPE (parm
) = tsubst (TREE_TYPE (parm
), arglist
,
3875 tf_none
, NULL_TREE
);
3878 TREE_PURPOSE (parm_desc
) =
3879 tsubst_template_arg (TREE_PURPOSE (parm_desc
),
3880 arglist
, tf_none
, parm
);
3882 pop_deferring_access_checks ();
3885 /* Walk the current template parms and properly compute the canonical
3886 types of the dependent types created during
3887 cp_parser_template_parameter_list. */
3890 fixup_template_parms (void)
3897 parameter_vec
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
3898 if (parameter_vec
== NULL_TREE
)
3901 num_parms
= TREE_VEC_LENGTH (parameter_vec
);
3903 /* This vector contains the current innermost template parms that
3904 have been fixed up so far. The form of FIXEDUP_ARGS is suitable
3905 to be passed to tsubst* functions as their ARGS argument. */
3906 fixedup_args
= make_tree_vec (num_parms
);
3908 /* This vector contains the full set of template parms in a form
3909 suitable to be passed to substs functions as their ARGS
3911 arglist
= current_template_args ();
3912 arglist
= add_outermost_template_args (arglist
, fixedup_args
);
3914 /* Let's do the proper fixup now. */
3915 for (i
= 0; i
< num_parms
; ++i
)
3916 fixup_template_parm (TREE_VEC_ELT (parameter_vec
, i
),
3917 i
, num_parms
, arglist
);
3920 /* end_template_decl is called after a template declaration is seen. */
3923 end_template_decl (void)
3925 reset_specialization ();
3927 if (! processing_template_decl
)
3930 /* This matches the pushlevel in begin_template_parm_list. */
3933 --processing_template_decl
;
3934 current_template_parms
= TREE_CHAIN (current_template_parms
);
3937 /* Takes a TREE_LIST representing a template parameter and convert it
3938 into an argument suitable to be passed to the type substitution
3939 functions. Note that If the TREE_LIST contains an error_mark
3940 node, the returned argument is error_mark_node. */
3943 template_parm_to_arg (tree t
)
3947 || TREE_CODE (t
) != TREE_LIST
)
3950 if (error_operand_p (TREE_VALUE (t
)))
3951 return error_mark_node
;
3955 if (TREE_CODE (t
) == TYPE_DECL
3956 || TREE_CODE (t
) == TEMPLATE_DECL
)
3960 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3962 /* Turn this argument into a TYPE_ARGUMENT_PACK
3963 with a single element, which expands T. */
3964 tree vec
= make_tree_vec (1);
3965 #ifdef ENABLE_CHECKING
3966 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3967 (vec
, TREE_VEC_LENGTH (vec
));
3969 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3971 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
3972 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3977 t
= DECL_INITIAL (t
);
3979 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3981 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3982 with a single element, which expands T. */
3983 tree vec
= make_tree_vec (1);
3984 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3985 #ifdef ENABLE_CHECKING
3986 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3987 (vec
, TREE_VEC_LENGTH (vec
));
3989 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3991 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3992 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3993 TREE_TYPE (t
) = type
;
3999 /* Within the declaration of a template, return all levels of template
4000 parameters that apply. The template parameters are represented as
4001 a TREE_VEC, in the form documented in cp-tree.h for template
4005 current_template_args (void)
4008 tree args
= NULL_TREE
;
4009 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
4012 /* If there is only one level of template parameters, we do not
4013 create a TREE_VEC of TREE_VECs. Instead, we return a single
4014 TREE_VEC containing the arguments. */
4016 args
= make_tree_vec (length
);
4018 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
4020 tree a
= copy_node (TREE_VALUE (header
));
4023 TREE_TYPE (a
) = NULL_TREE
;
4024 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4025 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4027 #ifdef ENABLE_CHECKING
4028 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4032 TREE_VEC_ELT (args
, --l
) = a
;
4037 if (length
> 1 && TREE_VEC_ELT (args
, 0) == NULL_TREE
)
4038 /* This can happen for template parms of a template template
4041 template<template<class T, class U> class TT> struct S;
4043 Consider the level of the parms of TT; T and U both have
4044 level 2; TT has no template parm of level 1. So in this case
4045 the first element of full_template_args is NULL_TREE. If we
4046 leave it like this TMPL_ARG_DEPTH on args returns 1 instead
4047 of 2. This will make tsubst wrongly consider that T and U
4048 have level 1. Instead, let's create a dummy vector as the
4049 first element of full_template_args so that TMPL_ARG_DEPTH
4050 returns the correct depth for args. */
4051 TREE_VEC_ELT (args
, 0) = make_tree_vec (1);
4055 /* Update the declared TYPE by doing any lookups which were thought to be
4056 dependent, but are not now that we know the SCOPE of the declarator. */
4059 maybe_update_decl_type (tree orig_type
, tree scope
)
4061 tree type
= orig_type
;
4063 if (type
== NULL_TREE
)
4066 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4067 type
= TREE_TYPE (type
);
4069 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4070 && dependent_type_p (type
)
4071 /* Don't bother building up the args in this case. */
4072 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4074 /* tsubst in the args corresponding to the template parameters,
4075 including auto if present. Most things will be unchanged, but
4076 make_typename_type and tsubst_qualified_id will resolve
4077 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4078 tree args
= current_template_args ();
4079 tree auto_node
= type_uses_auto (type
);
4083 tree auto_vec
= make_tree_vec (1);
4084 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4085 args
= add_to_template_args (args
, auto_vec
);
4087 pushed
= push_scope (scope
);
4088 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4093 if (type
== error_mark_node
)
4096 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4098 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4101 type
= TYPE_NAME (type
);
4106 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4107 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
4108 a member template. Used by push_template_decl below. */
4111 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4113 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4114 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4115 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4116 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4121 struct template_parm_data
4123 /* The level of the template parameters we are currently
4127 /* The index of the specialization argument we are currently
4131 /* An array whose size is the number of template parameters. The
4132 elements are nonzero if the parameter has been used in any one
4133 of the arguments processed so far. */
4136 /* An array whose size is the number of template arguments. The
4137 elements are nonzero if the argument makes use of template
4138 parameters of this level. */
4139 int* arg_uses_template_parms
;
4142 /* Subroutine of push_template_decl used to see if each template
4143 parameter in a partial specialization is used in the explicit
4144 argument list. If T is of the LEVEL given in DATA (which is
4145 treated as a template_parm_data*), then DATA->PARMS is marked
4149 mark_template_parm (tree t
, void* data
)
4153 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4155 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4157 level
= TEMPLATE_PARM_LEVEL (t
);
4158 idx
= TEMPLATE_PARM_IDX (t
);
4162 level
= TEMPLATE_TYPE_LEVEL (t
);
4163 idx
= TEMPLATE_TYPE_IDX (t
);
4166 if (level
== tpd
->level
)
4168 tpd
->parms
[idx
] = 1;
4169 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4172 /* Return zero so that for_each_template_parm will continue the
4173 traversal of the tree; we want to mark *every* template parm. */
4177 /* Process the partial specialization DECL. */
4180 process_partial_specialization (tree decl
)
4182 tree type
= TREE_TYPE (decl
);
4183 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
4184 tree specargs
= CLASSTYPE_TI_ARGS (type
);
4185 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4186 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4189 int nargs
= TREE_VEC_LENGTH (inner_args
);
4192 bool did_error_intro
= false;
4193 struct template_parm_data tpd
;
4194 struct template_parm_data tpd2
;
4196 gcc_assert (current_template_parms
);
4198 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4199 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4201 /* We check that each of the template parameters given in the
4202 partial specialization is used in the argument list to the
4203 specialization. For example:
4205 template <class T> struct S;
4206 template <class T> struct S<T*>;
4208 The second declaration is OK because `T*' uses the template
4209 parameter T, whereas
4211 template <class T> struct S<int>;
4213 is no good. Even trickier is:
4224 The S2<T> declaration is actually invalid; it is a
4225 full-specialization. Of course,
4228 struct S2<T (*)(U)>;
4230 or some such would have been OK. */
4231 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4232 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4233 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4235 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4236 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4237 for (i
= 0; i
< nargs
; ++i
)
4239 tpd
.current_arg
= i
;
4240 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4241 &mark_template_parm
,
4244 /*include_nondeduced_p=*/false);
4246 for (i
= 0; i
< ntparms
; ++i
)
4247 if (tpd
.parms
[i
] == 0)
4249 /* One of the template parms was not used in the
4251 if (!did_error_intro
)
4253 error ("template parameters not used in partial specialization:");
4254 did_error_intro
= true;
4257 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4260 if (did_error_intro
)
4261 return error_mark_node
;
4263 /* [temp.class.spec]
4265 The argument list of the specialization shall not be identical to
4266 the implicit argument list of the primary template. */
4267 if (comp_template_args
4269 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4271 error ("partial specialization %qT does not specialize any template arguments", type
);
4273 /* [temp.class.spec]
4275 A partially specialized non-type argument expression shall not
4276 involve template parameters of the partial specialization except
4277 when the argument expression is a simple identifier.
4279 The type of a template parameter corresponding to a specialized
4280 non-type argument shall not be dependent on a parameter of the
4283 Also, we verify that pack expansions only occur at the
4284 end of the argument list. */
4285 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4287 for (i
= 0; i
< nargs
; ++i
)
4289 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4290 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4291 tree packed_args
= NULL_TREE
;
4294 if (ARGUMENT_PACK_P (arg
))
4296 /* Extract the arguments from the argument pack. We'll be
4297 iterating over these in the following loop. */
4298 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4299 len
= TREE_VEC_LENGTH (packed_args
);
4302 for (j
= 0; j
< len
; j
++)
4305 /* Get the Jth argument in the parameter pack. */
4306 arg
= TREE_VEC_ELT (packed_args
, j
);
4308 if (PACK_EXPANSION_P (arg
))
4310 /* Pack expansions must come at the end of the
4312 if ((packed_args
&& j
< len
- 1)
4313 || (!packed_args
&& i
< nargs
- 1))
4315 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4316 error ("parameter pack argument %qE must be at the "
4317 "end of the template argument list", arg
);
4319 error ("parameter pack argument %qT must be at the "
4320 "end of the template argument list", arg
);
4324 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4325 /* We only care about the pattern. */
4326 arg
= PACK_EXPANSION_PATTERN (arg
);
4328 if (/* These first two lines are the `non-type' bit. */
4330 && TREE_CODE (arg
) != TEMPLATE_DECL
4331 /* This next line is the `argument expression is not just a
4332 simple identifier' condition and also the `specialized
4333 non-type argument' bit. */
4334 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
4336 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4337 || (packed_args
&& uses_template_parms (arg
)))
4338 error ("template argument %qE involves template parameter(s)",
4342 /* Look at the corresponding template parameter,
4343 marking which template parameters its type depends
4345 tree type
= TREE_TYPE (parm
);
4349 /* We haven't yet initialized TPD2. Do so now. */
4350 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4351 /* The number of parameters here is the number in the
4352 main template, which, as checked in the assertion
4354 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4356 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4359 /* Mark the template parameters. But this time, we're
4360 looking for the template parameters of the main
4361 template, not in the specialization. */
4362 tpd2
.current_arg
= i
;
4363 tpd2
.arg_uses_template_parms
[i
] = 0;
4364 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4365 for_each_template_parm (type
,
4366 &mark_template_parm
,
4369 /*include_nondeduced_p=*/false);
4371 if (tpd2
.arg_uses_template_parms
[i
])
4373 /* The type depended on some template parameters.
4374 If they are fully specialized in the
4375 specialization, that's OK. */
4378 for (j
= 0; j
< nargs
; ++j
)
4379 if (tpd2
.parms
[j
] != 0
4380 && tpd
.arg_uses_template_parms
[j
])
4383 error_n (input_location
, count
,
4384 "type %qT of template argument %qE depends "
4385 "on a template parameter",
4386 "type %qT of template argument %qE depends "
4387 "on template parameters",
4396 /* We should only get here once. */
4397 gcc_assert (!COMPLETE_TYPE_P (type
));
4399 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4400 = tree_cons (specargs
, inner_parms
,
4401 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4402 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4404 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4405 inst
= TREE_CHAIN (inst
))
4407 tree inst_type
= TREE_VALUE (inst
);
4408 if (COMPLETE_TYPE_P (inst_type
)
4409 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type
))
4411 tree spec
= most_specialized_class (inst_type
, maintmpl
, tf_none
);
4412 if (spec
&& TREE_TYPE (spec
) == type
)
4413 permerror (input_location
,
4414 "partial specialization of %qT after instantiation "
4415 "of %qT", type
, inst_type
);
4422 /* Check that a template declaration's use of default arguments and
4423 parameter packs is not invalid. Here, PARMS are the template
4424 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
4425 a primary template. IS_PARTIAL is nonzero if DECL is a partial
4429 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4430 declaration (but not a definition); 1 indicates a declaration, 2
4431 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4432 emitted for extraneous default arguments.
4434 Returns TRUE if there were no errors found, FALSE otherwise. */
4437 check_default_tmpl_args (tree decl
, tree parms
, int is_primary
,
4438 int is_partial
, int is_friend_decl
)
4441 int last_level_to_check
;
4443 bool no_errors
= true;
4447 A default template-argument shall not be specified in a
4448 function template declaration or a function template definition, nor
4449 in the template-parameter-list of the definition of a member of a
4452 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
4453 /* You can't have a function template declaration in a local
4454 scope, nor you can you define a member of a class template in a
4458 if (current_class_type
4459 && !TYPE_BEING_DEFINED (current_class_type
)
4460 && DECL_LANG_SPECIFIC (decl
)
4461 && DECL_DECLARES_FUNCTION_P (decl
)
4462 /* If this is either a friend defined in the scope of the class
4463 or a member function. */
4464 && (DECL_FUNCTION_MEMBER_P (decl
)
4465 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4466 : DECL_FRIEND_CONTEXT (decl
)
4467 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4469 /* And, if it was a member function, it really was defined in
4470 the scope of the class. */
4471 && (!DECL_FUNCTION_MEMBER_P (decl
)
4472 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4473 /* We already checked these parameters when the template was
4474 declared, so there's no need to do it again now. This function
4475 was defined in class scope, but we're processing it's body now
4476 that the class is complete. */
4479 /* Core issue 226 (C++0x only): the following only applies to class
4481 if ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
)
4485 If a template-parameter has a default template-argument, all
4486 subsequent template-parameters shall have a default
4487 template-argument supplied. */
4488 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4490 tree inner_parms
= TREE_VALUE (parm_level
);
4491 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4492 int seen_def_arg_p
= 0;
4495 for (i
= 0; i
< ntparms
; ++i
)
4497 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4499 if (parm
== error_mark_node
)
4502 if (TREE_PURPOSE (parm
))
4504 else if (seen_def_arg_p
4505 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4507 error ("no default argument for %qD", TREE_VALUE (parm
));
4508 /* For better subsequent error-recovery, we indicate that
4509 there should have been a default argument. */
4510 TREE_PURPOSE (parm
) = error_mark_node
;
4516 /* Don't complain about an enclosing partial
4518 && parm_level
== parms
4519 && TREE_CODE (decl
) == TYPE_DECL
4521 && template_parameter_pack_p (TREE_VALUE (parm
)))
4523 /* A primary class template can only have one
4524 parameter pack, at the end of the template
4527 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
4528 error ("parameter pack %qE must be at the end of the"
4529 " template parameter list", TREE_VALUE (parm
));
4531 error ("parameter pack %qT must be at the end of the"
4532 " template parameter list",
4533 TREE_TYPE (TREE_VALUE (parm
)));
4535 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4543 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4547 /* For an ordinary class template, default template arguments are
4548 allowed at the innermost level, e.g.:
4549 template <class T = int>
4551 but, in a partial specialization, they're not allowed even
4552 there, as we have in [temp.class.spec]:
4554 The template parameter list of a specialization shall not
4555 contain default template argument values.
4557 So, for a partial specialization, or for a function template
4558 (in C++98/C++03), we look at all of them. */
4561 /* But, for a primary class template that is not a partial
4562 specialization we look at all template parameters except the
4564 parms
= TREE_CHAIN (parms
);
4566 /* Figure out what error message to issue. */
4567 if (is_friend_decl
== 2)
4568 msg
= G_("default template arguments may not be used in function template "
4569 "friend re-declaration");
4570 else if (is_friend_decl
)
4571 msg
= G_("default template arguments may not be used in function template "
4572 "friend declarations");
4573 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4574 msg
= G_("default template arguments may not be used in function templates "
4575 "without -std=c++0x or -std=gnu++0x");
4576 else if (is_partial
)
4577 msg
= G_("default template arguments may not be used in "
4578 "partial specializations");
4580 msg
= G_("default argument for template parameter for class enclosing %qD");
4582 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4583 /* If we're inside a class definition, there's no need to
4584 examine the parameters to the class itself. On the one
4585 hand, they will be checked when the class is defined, and,
4586 on the other, default arguments are valid in things like:
4587 template <class T = double>
4588 struct S { template <class U> void f(U); };
4589 Here the default argument for `S' has no bearing on the
4590 declaration of `f'. */
4591 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4593 /* Check everything. */
4594 last_level_to_check
= 0;
4596 for (parm_level
= parms
;
4597 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4598 parm_level
= TREE_CHAIN (parm_level
))
4600 tree inner_parms
= TREE_VALUE (parm_level
);
4604 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4605 for (i
= 0; i
< ntparms
; ++i
)
4607 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4610 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4615 if (is_friend_decl
== 2)
4622 /* Clear out the default argument so that we are not
4624 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4628 /* At this point, if we're still interested in issuing messages,
4629 they must apply to classes surrounding the object declared. */
4631 msg
= G_("default argument for template parameter for class "
4638 /* Worker for push_template_decl_real, called via
4639 for_each_template_parm. DATA is really an int, indicating the
4640 level of the parameters we are interested in. If T is a template
4641 parameter of that level, return nonzero. */
4644 template_parm_this_level_p (tree t
, void* data
)
4646 int this_level
= *(int *)data
;
4649 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4650 level
= TEMPLATE_PARM_LEVEL (t
);
4652 level
= TEMPLATE_TYPE_LEVEL (t
);
4653 return level
== this_level
;
4656 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4657 parameters given by current_template_args, or reuses a
4658 previously existing one, if appropriate. Returns the DECL, or an
4659 equivalent one, if it is replaced via a call to duplicate_decls.
4661 If IS_FRIEND is true, DECL is a friend declaration. */
4664 push_template_decl_real (tree decl
, bool is_friend
)
4672 int new_template_p
= 0;
4673 /* True if the template is a member template, in the sense of
4675 bool member_template_p
= false;
4677 if (decl
== error_mark_node
|| !current_template_parms
)
4678 return error_mark_node
;
4680 /* See if this is a partial specialization. */
4681 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
4682 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4683 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
4685 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4689 /* For a friend, we want the context of the friend function, not
4690 the type of which it is a friend. */
4691 ctx
= CP_DECL_CONTEXT (decl
);
4692 else if (CP_DECL_CONTEXT (decl
)
4693 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4694 /* In the case of a virtual function, we want the class in which
4696 ctx
= CP_DECL_CONTEXT (decl
);
4698 /* Otherwise, if we're currently defining some class, the DECL
4699 is assumed to be a member of the class. */
4700 ctx
= current_scope ();
4702 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4705 if (!DECL_CONTEXT (decl
))
4706 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4708 /* See if this is a primary template. */
4709 if (is_friend
&& ctx
)
4710 /* A friend template that specifies a class context, i.e.
4711 template <typename T> friend void A<T>::f();
4715 primary
= template_parm_scope_p ();
4719 if (DECL_CLASS_SCOPE_P (decl
))
4720 member_template_p
= true;
4721 if (TREE_CODE (decl
) == TYPE_DECL
4722 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4724 error ("template class without a name");
4725 return error_mark_node
;
4727 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4729 if (DECL_DESTRUCTOR_P (decl
))
4733 A destructor shall not be a member template. */
4734 error ("destructor %qD declared as member template", decl
);
4735 return error_mark_node
;
4737 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4738 && (!prototype_p (TREE_TYPE (decl
))
4739 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4740 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4741 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4742 == void_list_node
)))
4744 /* [basic.stc.dynamic.allocation]
4746 An allocation function can be a function
4747 template. ... Template allocation functions shall
4748 have two or more parameters. */
4749 error ("invalid template declaration of %qD", decl
);
4750 return error_mark_node
;
4753 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4754 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4758 error ("template declaration of %q#D", decl
);
4759 return error_mark_node
;
4763 /* Check to see that the rules regarding the use of default
4764 arguments are not being violated. */
4765 check_default_tmpl_args (decl
, current_template_parms
,
4766 primary
, is_partial
, /*is_friend_decl=*/0);
4768 /* Ensure that there are no parameter packs in the type of this
4769 declaration that have not been expanded. */
4770 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4772 /* Check each of the arguments individually to see if there are
4773 any bare parameter packs. */
4774 tree type
= TREE_TYPE (decl
);
4775 tree arg
= DECL_ARGUMENTS (decl
);
4776 tree argtype
= TYPE_ARG_TYPES (type
);
4778 while (arg
&& argtype
)
4780 if (!FUNCTION_PARAMETER_PACK_P (arg
)
4781 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4783 /* This is a PARM_DECL that contains unexpanded parameter
4784 packs. We have already complained about this in the
4785 check_for_bare_parameter_packs call, so just replace
4786 these types with ERROR_MARK_NODE. */
4787 TREE_TYPE (arg
) = error_mark_node
;
4788 TREE_VALUE (argtype
) = error_mark_node
;
4791 arg
= DECL_CHAIN (arg
);
4792 argtype
= TREE_CHAIN (argtype
);
4795 /* Check for bare parameter packs in the return type and the
4796 exception specifiers. */
4797 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4798 /* Errors were already issued, set return type to int
4799 as the frontend doesn't expect error_mark_node as
4801 TREE_TYPE (type
) = integer_type_node
;
4802 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4803 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4805 else if (check_for_bare_parameter_packs (TREE_TYPE (decl
)))
4807 TREE_TYPE (decl
) = error_mark_node
;
4808 return error_mark_node
;
4812 return process_partial_specialization (decl
);
4814 args
= current_template_args ();
4817 || TREE_CODE (ctx
) == FUNCTION_DECL
4818 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4819 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4821 if (DECL_LANG_SPECIFIC (decl
)
4822 && DECL_TEMPLATE_INFO (decl
)
4823 && DECL_TI_TEMPLATE (decl
))
4824 tmpl
= DECL_TI_TEMPLATE (decl
);
4825 /* If DECL is a TYPE_DECL for a class-template, then there won't
4826 be DECL_LANG_SPECIFIC. The information equivalent to
4827 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4828 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4829 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4830 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4832 /* Since a template declaration already existed for this
4833 class-type, we must be redeclaring it here. Make sure
4834 that the redeclaration is valid. */
4835 redeclare_class_template (TREE_TYPE (decl
),
4836 current_template_parms
);
4837 /* We don't need to create a new TEMPLATE_DECL; just use the
4838 one we already had. */
4839 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4843 tmpl
= build_template_decl (decl
, current_template_parms
,
4847 if (DECL_LANG_SPECIFIC (decl
)
4848 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4850 /* A specialization of a member template of a template
4852 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4853 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4854 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4860 tree a
, t
, current
, parms
;
4862 tree tinfo
= get_template_info (decl
);
4866 error ("template definition of non-template %q#D", decl
);
4867 return error_mark_node
;
4870 tmpl
= TI_TEMPLATE (tinfo
);
4872 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4873 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4874 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4875 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4879 /* The declaration is a specialization of a member
4880 template, declared outside the class. Therefore, the
4881 innermost template arguments will be NULL, so we
4882 replace them with the arguments determined by the
4883 earlier call to check_explicit_specialization. */
4884 args
= DECL_TI_ARGS (decl
);
4887 = build_template_decl (decl
, current_template_parms
,
4889 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4890 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4891 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4892 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4893 DECL_TEMPLATE_INFO (new_tmpl
)
4894 = build_template_info (tmpl
, args
);
4896 register_specialization (new_tmpl
,
4897 most_general_template (tmpl
),
4903 /* Make sure the template headers we got make sense. */
4905 parms
= DECL_TEMPLATE_PARMS (tmpl
);
4906 i
= TMPL_PARMS_DEPTH (parms
);
4907 if (TMPL_ARGS_DEPTH (args
) != i
)
4909 error ("expected %d levels of template parms for %q#D, got %d",
4910 i
, decl
, TMPL_ARGS_DEPTH (args
));
4913 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
4915 a
= TMPL_ARGS_LEVEL (args
, i
);
4916 t
= INNERMOST_TEMPLATE_PARMS (parms
);
4918 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
4920 if (current
== decl
)
4921 error ("got %d template parameters for %q#D",
4922 TREE_VEC_LENGTH (a
), decl
);
4924 error ("got %d template parameters for %q#T",
4925 TREE_VEC_LENGTH (a
), current
);
4926 error (" but %d required", TREE_VEC_LENGTH (t
));
4927 return error_mark_node
;
4930 if (current
== decl
)
4932 else if (current
== NULL_TREE
)
4933 /* Can happen in erroneous input. */
4936 current
= (TYPE_P (current
)
4937 ? TYPE_CONTEXT (current
)
4938 : DECL_CONTEXT (current
));
4941 /* Check that the parms are used in the appropriate qualifying scopes
4942 in the declarator. */
4943 if (!comp_template_args
4945 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
4948 template arguments to %qD do not match original template %qD",
4949 decl
, DECL_TEMPLATE_RESULT (tmpl
));
4950 if (!uses_template_parms (TI_ARGS (tinfo
)))
4951 inform (input_location
, "use template<> for an explicit specialization");
4952 /* Avoid crash in import_export_decl. */
4953 DECL_INTERFACE_KNOWN (decl
) = 1;
4954 return error_mark_node
;
4958 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4959 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4961 /* Push template declarations for global functions and types. Note
4962 that we do not try to push a global template friend declared in a
4963 template class; such a thing may well depend on the template
4964 parameters of the class. */
4965 if (new_template_p
&& !ctx
4966 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
4968 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
4969 if (tmpl
== error_mark_node
)
4970 return error_mark_node
;
4972 /* Hide template friend classes that haven't been declared yet. */
4973 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
4975 DECL_ANTICIPATED (tmpl
) = 1;
4976 DECL_FRIEND_P (tmpl
) = 1;
4982 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
4985 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4986 if (DECL_CONV_FN_P (tmpl
))
4988 int depth
= TMPL_PARMS_DEPTH (parms
);
4990 /* It is a conversion operator. See if the type converted to
4991 depends on innermost template operands. */
4993 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
4995 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
4998 /* Give template template parms a DECL_CONTEXT of the template
4999 for which they are a parameter. */
5000 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5001 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5003 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5004 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5005 DECL_CONTEXT (parm
) = tmpl
;
5009 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5010 back to its most general template. If TMPL is a specialization,
5011 ARGS may only have the innermost set of arguments. Add the missing
5012 argument levels if necessary. */
5013 if (DECL_TEMPLATE_INFO (tmpl
))
5014 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5016 info
= build_template_info (tmpl
, args
);
5018 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5019 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5020 else if (DECL_LANG_SPECIFIC (decl
))
5021 DECL_TEMPLATE_INFO (decl
) = info
;
5023 return DECL_TEMPLATE_RESULT (tmpl
);
5027 push_template_decl (tree decl
)
5029 return push_template_decl_real (decl
, false);
5032 /* Called when a class template TYPE is redeclared with the indicated
5033 template PARMS, e.g.:
5035 template <class T> struct S;
5036 template <class T> struct S {}; */
5039 redeclare_class_template (tree type
, tree parms
)
5045 if (!TYPE_TEMPLATE_INFO (type
))
5047 error ("%qT is not a template type", type
);
5051 tmpl
= TYPE_TI_TEMPLATE (type
);
5052 if (!PRIMARY_TEMPLATE_P (tmpl
))
5053 /* The type is nested in some template class. Nothing to worry
5054 about here; there are no new template parameters for the nested
5060 error ("template specifiers not specified in declaration of %qD",
5065 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5066 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5068 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5070 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5071 "redeclared with %d template parameter",
5072 "redeclared with %d template parameters",
5073 TREE_VEC_LENGTH (parms
));
5074 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
5075 "previous declaration %q+D used %d template parameter",
5076 "previous declaration %q+D used %d template parameters",
5077 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5081 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5088 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5089 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5092 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5093 if (tmpl_parm
== error_mark_node
)
5096 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5097 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5098 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5100 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5102 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5103 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5104 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5105 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5106 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5107 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5108 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5109 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5110 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5112 error ("template parameter %q+#D", tmpl_parm
);
5113 error ("redeclared here as %q#D", parm
);
5117 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5119 /* We have in [temp.param]:
5121 A template-parameter may not be given default arguments
5122 by two different declarations in the same scope. */
5123 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5124 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5125 "original definition appeared here");
5129 if (parm_default
!= NULL_TREE
)
5130 /* Update the previous template parameters (which are the ones
5131 that will really count) with the new default value. */
5132 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5133 else if (tmpl_default
!= NULL_TREE
)
5134 /* Update the new parameters, too; they'll be used as the
5135 parameters for any members. */
5136 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5142 /* Simplify EXPR if it is a non-dependent expression. Returns the
5143 (possibly simplified) expression. */
5146 fold_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5148 if (expr
== NULL_TREE
)
5151 /* If we're in a template, but EXPR isn't value dependent, simplify
5152 it. We're supposed to treat:
5154 template <typename T> void f(T[1 + 1]);
5155 template <typename T> void f(T[2]);
5157 as two declarations of the same function, for example. */
5158 if (processing_template_decl
5159 && !type_dependent_expression_p (expr
)
5160 && potential_constant_expression (expr
)
5161 && !value_dependent_expression_p (expr
))
5163 HOST_WIDE_INT saved_processing_template_decl
;
5165 saved_processing_template_decl
= processing_template_decl
;
5166 processing_template_decl
= 0;
5167 expr
= tsubst_copy_and_build (expr
,
5170 /*in_decl=*/NULL_TREE
,
5171 /*function_p=*/false,
5172 /*integral_constant_expression_p=*/true);
5173 processing_template_decl
= saved_processing_template_decl
;
5179 fold_non_dependent_expr (tree expr
)
5181 return fold_non_dependent_expr_sfinae (expr
, tf_error
);
5184 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5185 must be a function or a pointer-to-function type, as specified
5186 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5187 and check that the resulting function has external linkage. */
5190 convert_nontype_argument_function (tree type
, tree expr
)
5195 fn
= instantiate_type (type
, fns
, tf_none
);
5196 if (fn
== error_mark_node
)
5197 return error_mark_node
;
5200 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5201 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5202 if (TREE_CODE (fn_no_ptr
) == BASELINK
)
5203 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5205 /* [temp.arg.nontype]/1
5207 A template-argument for a non-type, non-template template-parameter
5210 -- the address of an object or function with external linkage. */
5211 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr
))
5213 error ("%qE is not a valid template argument for type %qT "
5214 "because function %qD has not external linkage",
5215 expr
, type
, fn_no_ptr
);
5222 /* Subroutine of convert_nontype_argument.
5223 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5224 Emit an error otherwise. */
5227 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5228 tsubst_flags_t complain
)
5231 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5233 if (complain
& tf_error
)
5235 error ("%qE is not a valid template argument for type %qT",
5237 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5242 /* Returns TRUE iff the address of OP is value-dependent.
5244 14.6.2.4 [temp.dep.temp]:
5245 A non-integral non-type template-argument is dependent if its type is
5246 dependent or it has either of the following forms
5249 and contains a nested-name-specifier which specifies a class-name that
5250 names a dependent type.
5252 We generalize this to just say that the address of a member of a
5253 dependent class is value-dependent; the above doesn't cover the
5254 address of a static data member named with an unqualified-id. */
5257 has_value_dependent_address (tree op
)
5259 /* We could use get_inner_reference here, but there's no need;
5260 this is only relevant for template non-type arguments, which
5261 can only be expressed as &id-expression. */
5264 tree ctx
= CP_DECL_CONTEXT (op
);
5265 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5272 /* Attempt to convert the non-type template parameter EXPR to the
5273 indicated TYPE. If the conversion is successful, return the
5274 converted value. If the conversion is unsuccessful, return
5275 NULL_TREE if we issued an error message, or error_mark_node if we
5276 did not. We issue error messages for out-and-out bad template
5277 parameters, but not simply because the conversion failed, since we
5278 might be just trying to do argument deduction. Both TYPE and EXPR
5279 must be non-dependent.
5281 The conversion follows the special rules described in
5282 [temp.arg.nontype], and it is much more strict than an implicit
5285 This function is called twice for each template argument (see
5286 lookup_template_class for a more accurate description of this
5287 problem). This means that we need to handle expressions which
5288 are not valid in a C++ source, but can be created from the
5289 first call (for instance, casts to perform conversions). These
5290 hacks can go away after we fix the double coercion problem. */
5293 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5297 /* Detect immediately string literals as invalid non-type argument.
5298 This special-case is not needed for correctness (we would easily
5299 catch this later), but only to provide better diagnostic for this
5300 common user mistake. As suggested by DR 100, we do not mention
5301 linkage issues in the diagnostic as this is not the point. */
5302 /* FIXME we're making this OK. */
5303 if (TREE_CODE (expr
) == STRING_CST
)
5305 if (complain
& tf_error
)
5306 error ("%qE is not a valid template argument for type %qT "
5307 "because string literals can never be used in this context",
5312 /* Add the ADDR_EXPR now for the benefit of
5313 value_dependent_expression_p. */
5314 if (TYPE_PTROBV_P (type
))
5315 expr
= decay_conversion (expr
);
5317 /* If we are in a template, EXPR may be non-dependent, but still
5318 have a syntactic, rather than semantic, form. For example, EXPR
5319 might be a SCOPE_REF, rather than the VAR_DECL to which the
5320 SCOPE_REF refers. Preserving the qualifying scope is necessary
5321 so that access checking can be performed when the template is
5322 instantiated -- but here we need the resolved form so that we can
5323 convert the argument. */
5324 if (TYPE_REF_OBJ_P (type
)
5325 && has_value_dependent_address (expr
))
5326 /* If we want the address and it's value-dependent, don't fold. */;
5327 else if (!type_unknown_p (expr
))
5328 expr
= fold_non_dependent_expr_sfinae (expr
, complain
);
5329 if (error_operand_p (expr
))
5330 return error_mark_node
;
5331 expr_type
= TREE_TYPE (expr
);
5332 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5333 expr
= mark_lvalue_use (expr
);
5335 expr
= mark_rvalue_use (expr
);
5337 /* HACK: Due to double coercion, we can get a
5338 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5339 which is the tree that we built on the first call (see
5340 below when coercing to reference to object or to reference to
5341 function). We just strip everything and get to the arg.
5342 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5344 if (TREE_CODE (expr
) == NOP_EXPR
)
5346 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5348 /* ??? Maybe we could use convert_from_reference here, but we
5349 would need to relax its constraints because the NOP_EXPR
5350 could actually change the type to something more cv-qualified,
5351 and this is not folded by convert_from_reference. */
5352 tree addr
= TREE_OPERAND (expr
, 0);
5353 gcc_assert (TREE_CODE (expr_type
) == REFERENCE_TYPE
);
5354 gcc_assert (TREE_CODE (addr
) == ADDR_EXPR
);
5355 gcc_assert (TREE_CODE (TREE_TYPE (addr
)) == POINTER_TYPE
);
5356 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5357 (TREE_TYPE (expr_type
),
5358 TREE_TYPE (TREE_TYPE (addr
))));
5360 expr
= TREE_OPERAND (addr
, 0);
5361 expr_type
= TREE_TYPE (expr
);
5364 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5365 parameter is a pointer to object, through decay and
5366 qualification conversion. Let's strip everything. */
5367 else if (TYPE_PTROBV_P (type
))
5370 gcc_assert (TREE_CODE (expr
) == ADDR_EXPR
);
5371 gcc_assert (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
);
5372 /* Skip the ADDR_EXPR only if it is part of the decay for
5373 an array. Otherwise, it is part of the original argument
5374 in the source code. */
5375 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == ARRAY_TYPE
)
5376 expr
= TREE_OPERAND (expr
, 0);
5377 expr_type
= TREE_TYPE (expr
);
5381 /* [temp.arg.nontype]/5, bullet 1
5383 For a non-type template-parameter of integral or enumeration type,
5384 integral promotions (_conv.prom_) and integral conversions
5385 (_conv.integral_) are applied. */
5386 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5388 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
5389 t
= maybe_constant_value (t
);
5390 if (t
!= error_mark_node
)
5393 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
5394 return error_mark_node
;
5396 /* Notice that there are constant expressions like '4 % 0' which
5397 do not fold into integer constants. */
5398 if (TREE_CODE (expr
) != INTEGER_CST
)
5400 if (complain
& tf_error
)
5402 error ("%qE is not a valid template argument for type %qT "
5403 "because it is a non-constant expression", expr
, type
);
5404 cxx_constant_value (expr
);
5409 /* [temp.arg.nontype]/5, bullet 2
5411 For a non-type template-parameter of type pointer to object,
5412 qualification conversions (_conv.qual_) and the array-to-pointer
5413 conversion (_conv.array_) are applied. */
5414 else if (TYPE_PTROBV_P (type
))
5416 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5418 A template-argument for a non-type, non-template template-parameter
5419 shall be one of: [...]
5421 -- the name of a non-type template-parameter;
5422 -- the address of an object or function with external linkage, [...]
5423 expressed as "& id-expression" where the & is optional if the name
5424 refers to a function or array, or if the corresponding
5425 template-parameter is a reference.
5427 Here, we do not care about functions, as they are invalid anyway
5428 for a parameter of type pointer-to-object. */
5430 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5431 /* Non-type template parameters are OK. */
5433 else if (TREE_CODE (expr
) != ADDR_EXPR
5434 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5436 if (TREE_CODE (expr
) == VAR_DECL
)
5438 error ("%qD is not a valid template argument "
5439 "because %qD is a variable, not the address of "
5444 /* Other values, like integer constants, might be valid
5445 non-type arguments of some other type. */
5446 return error_mark_node
;
5452 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
5453 ? TREE_OPERAND (expr
, 0) : expr
);
5454 if (TREE_CODE (decl
) != VAR_DECL
)
5456 error ("%qE is not a valid template argument of type %qT "
5457 "because %qE is not a variable",
5461 else if (!DECL_EXTERNAL_LINKAGE_P (decl
))
5463 error ("%qE is not a valid template argument of type %qT "
5464 "because %qD does not have external linkage",
5470 expr
= decay_conversion (expr
);
5471 if (expr
== error_mark_node
)
5472 return error_mark_node
;
5474 expr
= perform_qualification_conversions (type
, expr
);
5475 if (expr
== error_mark_node
)
5476 return error_mark_node
;
5478 /* [temp.arg.nontype]/5, bullet 3
5480 For a non-type template-parameter of type reference to object, no
5481 conversions apply. The type referred to by the reference may be more
5482 cv-qualified than the (otherwise identical) type of the
5483 template-argument. The template-parameter is bound directly to the
5484 template-argument, which must be an lvalue. */
5485 else if (TYPE_REF_OBJ_P (type
))
5487 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
5489 return error_mark_node
;
5491 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5493 error ("%qE is not a valid template argument for type %qT "
5494 "because of conflicts in cv-qualification", expr
, type
);
5498 if (!real_lvalue_p (expr
))
5500 error ("%qE is not a valid template argument for type %qT "
5501 "because it is not an lvalue", expr
, type
);
5505 /* [temp.arg.nontype]/1
5507 A template-argument for a non-type, non-template template-parameter
5508 shall be one of: [...]
5510 -- the address of an object or function with external linkage. */
5511 if (TREE_CODE (expr
) == INDIRECT_REF
5512 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5514 expr
= TREE_OPERAND (expr
, 0);
5517 error ("%q#D is not a valid template argument for type %qT "
5518 "because a reference variable does not have a constant "
5519 "address", expr
, type
);
5526 error ("%qE is not a valid template argument for type %qT "
5527 "because it is not an object with external linkage",
5532 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5534 error ("%qE is not a valid template argument for type %qT "
5535 "because object %qD has not external linkage",
5540 expr
= build_nop (type
, build_address (expr
));
5542 /* [temp.arg.nontype]/5, bullet 4
5544 For a non-type template-parameter of type pointer to function, only
5545 the function-to-pointer conversion (_conv.func_) is applied. If the
5546 template-argument represents a set of overloaded functions (or a
5547 pointer to such), the matching function is selected from the set
5549 else if (TYPE_PTRFN_P (type
))
5551 /* If the argument is a template-id, we might not have enough
5552 context information to decay the pointer. */
5553 if (!type_unknown_p (expr_type
))
5555 expr
= decay_conversion (expr
);
5556 if (expr
== error_mark_node
)
5557 return error_mark_node
;
5560 expr
= convert_nontype_argument_function (type
, expr
);
5561 if (!expr
|| expr
== error_mark_node
)
5564 if (TREE_CODE (expr
) != ADDR_EXPR
)
5566 error ("%qE is not a valid template argument for type %qT", expr
, type
);
5567 error ("it must be the address of a function with external linkage");
5571 /* [temp.arg.nontype]/5, bullet 5
5573 For a non-type template-parameter of type reference to function, no
5574 conversions apply. If the template-argument represents a set of
5575 overloaded functions, the matching function is selected from the set
5577 else if (TYPE_REFFN_P (type
))
5579 if (TREE_CODE (expr
) == ADDR_EXPR
)
5581 error ("%qE is not a valid template argument for type %qT "
5582 "because it is a pointer", expr
, type
);
5583 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
5587 expr
= convert_nontype_argument_function (TREE_TYPE (type
), expr
);
5588 if (!expr
|| expr
== error_mark_node
)
5591 expr
= build_nop (type
, build_address (expr
));
5593 /* [temp.arg.nontype]/5, bullet 6
5595 For a non-type template-parameter of type pointer to member function,
5596 no conversions apply. If the template-argument represents a set of
5597 overloaded member functions, the matching member function is selected
5598 from the set (_over.over_). */
5599 else if (TYPE_PTRMEMFUNC_P (type
))
5601 expr
= instantiate_type (type
, expr
, tf_none
);
5602 if (expr
== error_mark_node
)
5603 return error_mark_node
;
5605 /* [temp.arg.nontype] bullet 1 says the pointer to member
5606 expression must be a pointer-to-member constant. */
5607 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5608 return error_mark_node
;
5610 /* There is no way to disable standard conversions in
5611 resolve_address_of_overloaded_function (called by
5612 instantiate_type). It is possible that the call succeeded by
5613 converting &B::I to &D::I (where B is a base of D), so we need
5614 to reject this conversion here.
5616 Actually, even if there was a way to disable standard conversions,
5617 it would still be better to reject them here so that we can
5618 provide a superior diagnostic. */
5619 if (!same_type_p (TREE_TYPE (expr
), type
))
5621 error ("%qE is not a valid template argument for type %qT "
5622 "because it is of type %qT", expr
, type
,
5624 /* If we are just one standard conversion off, explain. */
5625 if (can_convert (type
, TREE_TYPE (expr
)))
5626 inform (input_location
,
5627 "standard conversions are not allowed in this context");
5631 /* [temp.arg.nontype]/5, bullet 7
5633 For a non-type template-parameter of type pointer to data member,
5634 qualification conversions (_conv.qual_) are applied. */
5635 else if (TYPE_PTRMEM_P (type
))
5637 /* [temp.arg.nontype] bullet 1 says the pointer to member
5638 expression must be a pointer-to-member constant. */
5639 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5640 return error_mark_node
;
5642 expr
= perform_qualification_conversions (type
, expr
);
5643 if (expr
== error_mark_node
)
5646 /* A template non-type parameter must be one of the above. */
5650 /* Sanity check: did we actually convert the argument to the
5652 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5653 (type
, TREE_TYPE (expr
)));
5657 /* Subroutine of coerce_template_template_parms, which returns 1 if
5658 PARM_PARM and ARG_PARM match using the rule for the template
5659 parameters of template template parameters. Both PARM and ARG are
5660 template parameters; the rest of the arguments are the same as for
5661 coerce_template_template_parms.
5664 coerce_template_template_parm (tree parm
,
5666 tsubst_flags_t complain
,
5670 if (arg
== NULL_TREE
|| arg
== error_mark_node
5671 || parm
== NULL_TREE
|| parm
== error_mark_node
)
5674 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5677 switch (TREE_CODE (parm
))
5680 /* We encounter instantiations of templates like
5681 template <template <template <class> class> class TT>
5684 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
5685 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
5687 if (!coerce_template_template_parms
5688 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
5694 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
5695 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5696 /* Argument is a parameter pack but parameter is not. */
5701 /* The tsubst call is used to handle cases such as
5703 template <int> class C {};
5704 template <class T, template <T> class TT> class D {};
5707 i.e. the parameter list of TT depends on earlier parameters. */
5708 if (!uses_template_parms (TREE_TYPE (arg
))
5710 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
5714 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
5715 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
5716 /* Argument is a parameter pack but parameter is not. */
5729 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5730 template template parameters. Both PARM_PARMS and ARG_PARMS are
5731 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5734 Consider the example:
5735 template <class T> class A;
5736 template<template <class U> class TT> class B;
5738 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5739 the parameters to A, and OUTER_ARGS contains A. */
5742 coerce_template_template_parms (tree parm_parms
,
5744 tsubst_flags_t complain
,
5748 int nparms
, nargs
, i
;
5752 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
5753 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
5755 nparms
= TREE_VEC_LENGTH (parm_parms
);
5756 nargs
= TREE_VEC_LENGTH (arg_parms
);
5758 /* Determine whether we have a parameter pack at the end of the
5759 template template parameter's template parameter list. */
5760 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
5762 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
5764 if (parm
== error_mark_node
)
5767 switch (TREE_CODE (parm
))
5771 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5776 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
5786 && !(variadic_p
&& nargs
>= nparms
- 1))
5789 /* Check all of the template parameters except the parameter pack at
5790 the end (if any). */
5791 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
5793 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
5794 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
5797 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
5798 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
5800 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
5808 /* Check each of the template parameters in the template
5809 argument against the template parameter pack at the end of
5810 the template template parameter. */
5811 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
5814 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
5816 for (; i
< nargs
; ++i
)
5818 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
5821 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
5823 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
5832 /* Verifies that the deduced template arguments (in TARGS) for the
5833 template template parameters (in TPARMS) represent valid bindings,
5834 by comparing the template parameter list of each template argument
5835 to the template parameter list of its corresponding template
5836 template parameter, in accordance with DR150. This
5837 routine can only be called after all template arguments have been
5838 deduced. It will return TRUE if all of the template template
5839 parameter bindings are okay, FALSE otherwise. */
5841 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
5843 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
5846 /* We're dealing with template parms in this process. */
5847 ++processing_template_decl
;
5849 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
5851 for (i
= 0; i
< ntparms
; ++i
)
5853 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
5854 tree targ
= TREE_VEC_ELT (targs
, i
);
5856 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
5858 tree packed_args
= NULL_TREE
;
5861 if (ARGUMENT_PACK_P (targ
))
5863 /* Look inside the argument pack. */
5864 packed_args
= ARGUMENT_PACK_ARGS (targ
);
5865 len
= TREE_VEC_LENGTH (packed_args
);
5868 for (idx
= 0; idx
< len
; ++idx
)
5870 tree targ_parms
= NULL_TREE
;
5873 /* Extract the next argument from the argument
5875 targ
= TREE_VEC_ELT (packed_args
, idx
);
5877 if (PACK_EXPANSION_P (targ
))
5878 /* Look at the pattern of the pack expansion. */
5879 targ
= PACK_EXPANSION_PATTERN (targ
);
5881 /* Extract the template parameters from the template
5883 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
5884 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
5885 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
5886 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
5888 /* Verify that we can coerce the template template
5889 parameters from the template argument to the template
5890 parameter. This requires an exact match. */
5892 && !coerce_template_template_parms
5893 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
5908 --processing_template_decl
;
5912 /* Convert the indicated template ARG as necessary to match the
5913 indicated template PARM. Returns the converted ARG, or
5914 error_mark_node if the conversion was unsuccessful. Error and
5915 warning messages are issued under control of COMPLAIN. This
5916 conversion is for the Ith parameter in the parameter list. ARGS is
5917 the full set of template arguments deduced so far. */
5920 convert_template_argument (tree parm
,
5923 tsubst_flags_t complain
,
5929 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
5931 if (TREE_CODE (arg
) == TREE_LIST
5932 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
5934 /* The template argument was the name of some
5935 member function. That's usually
5936 invalid, but static members are OK. In any
5937 case, grab the underlying fields/functions
5938 and issue an error later if required. */
5939 orig_arg
= TREE_VALUE (arg
);
5940 TREE_TYPE (arg
) = unknown_type_node
;
5945 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
5946 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
5947 || requires_tmpl_type
);
5949 /* When determining whether an argument pack expansion is a template,
5950 look at the pattern. */
5951 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
5952 arg
= PACK_EXPANSION_PATTERN (arg
);
5954 /* Deal with an injected-class-name used as a template template arg. */
5955 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
5957 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
5958 if (TREE_CODE (t
) == TEMPLATE_DECL
)
5960 if (complain
& tf_warning_or_error
)
5961 pedwarn (input_location
, OPT_pedantic
, "injected-class-name %qD"
5962 " used as template template argument", TYPE_NAME (arg
));
5963 else if (flag_pedantic_errors
)
5971 ((TREE_CODE (arg
) == TEMPLATE_DECL
5972 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
5973 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
5974 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
5977 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
5978 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
5979 arg
= TYPE_STUB_DECL (arg
);
5981 is_type
= TYPE_P (arg
) || is_tmpl_type
;
5983 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
5984 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
5986 permerror (input_location
, "to refer to a type member of a template parameter, "
5987 "use %<typename %E%>", orig_arg
);
5989 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
5990 TREE_OPERAND (arg
, 1),
5992 complain
& tf_error
);
5996 if (is_type
!= requires_type
)
6000 if (complain
& tf_error
)
6002 error ("type/value mismatch at argument %d in template "
6003 "parameter list for %qD",
6006 error (" expected a constant of type %qT, got %qT",
6008 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6009 else if (requires_tmpl_type
)
6010 error (" expected a class template, got %qE", orig_arg
);
6012 error (" expected a type, got %qE", orig_arg
);
6015 return error_mark_node
;
6017 if (is_tmpl_type
^ requires_tmpl_type
)
6019 if (in_decl
&& (complain
& tf_error
))
6021 error ("type/value mismatch at argument %d in template "
6022 "parameter list for %qD",
6025 error (" expected a type, got %qT", DECL_NAME (arg
));
6027 error (" expected a class template, got %qT", orig_arg
);
6029 return error_mark_node
;
6034 if (requires_tmpl_type
)
6036 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6037 /* The number of argument required is not known yet.
6038 Just accept it for now. */
6039 val
= TREE_TYPE (arg
);
6042 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6045 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6047 if (coerce_template_template_parms (parmparm
, argparm
,
6053 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6055 if (val
!= error_mark_node
)
6057 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6058 val
= TREE_TYPE (val
);
6059 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6060 val
= make_pack_expansion (val
);
6065 if (in_decl
&& (complain
& tf_error
))
6067 error ("type/value mismatch at argument %d in "
6068 "template parameter list for %qD",
6070 error (" expected a template of type %qD, got %qT",
6074 val
= error_mark_node
;
6080 /* We only form one instance of each template specialization.
6081 Therefore, if we use a non-canonical variant (i.e., a
6082 typedef), any future messages referring to the type will use
6083 the typedef, which is confusing if those future uses do not
6084 themselves also use the typedef. */
6086 val
= strip_typedefs (val
);
6090 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6092 if (invalid_nontype_parm_type_p (t
, complain
))
6093 return error_mark_node
;
6095 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6097 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6101 /* Not sure if this is reachable, but it doesn't hurt
6103 error ("type mismatch in nontype parameter pack");
6104 val
= error_mark_node
;
6107 else if (!uses_template_parms (orig_arg
) && !uses_template_parms (t
))
6108 /* We used to call digest_init here. However, digest_init
6109 will report errors, which we don't want when complain
6110 is zero. More importantly, digest_init will try too
6111 hard to convert things: for example, `0' should not be
6112 converted to pointer type at this point according to
6113 the standard. Accepting this is not merely an
6114 extension, since deciding whether or not these
6115 conversions can occur is part of determining which
6116 function template to call, or whether a given explicit
6117 argument specification is valid. */
6118 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6122 if (val
== NULL_TREE
)
6123 val
= error_mark_node
;
6124 else if (val
== error_mark_node
&& (complain
& tf_error
))
6125 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6127 if (TREE_CODE (val
) == SCOPE_REF
)
6129 /* Strip typedefs from the SCOPE_REF. */
6130 tree type
= strip_typedefs (TREE_TYPE (val
));
6131 tree scope
= strip_typedefs (TREE_OPERAND (val
, 0));
6132 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6133 QUALIFIED_NAME_IS_TEMPLATE (val
));
6140 /* Coerces the remaining template arguments in INNER_ARGS (from
6141 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6142 Returns the coerced argument pack. PARM_IDX is the position of this
6143 parameter in the template parameter list. ARGS is the original
6144 template argument list. */
6146 coerce_template_parameter_pack (tree parms
,
6154 tsubst_flags_t complain
)
6156 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6157 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6160 tree packed_types
= NULL_TREE
;
6162 if (arg_idx
> nargs
)
6165 packed_args
= make_tree_vec (nargs
- arg_idx
);
6167 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
6168 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
6170 /* When the template parameter is a non-type template
6171 parameter pack whose type uses parameter packs, we need
6172 to look at each of the template arguments
6173 separately. Build a vector of the types for these
6174 non-type template parameters in PACKED_TYPES. */
6176 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
6177 packed_types
= tsubst_pack_expansion (expansion
, args
,
6180 if (packed_types
== error_mark_node
)
6181 return error_mark_node
;
6183 /* Check that we have the right number of arguments. */
6185 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
6186 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
6189 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
6190 error ("wrong number of template arguments (%d, should be %d)",
6191 nargs
, needed_parms
);
6192 return error_mark_node
;
6195 /* If we aren't able to check the actual arguments now
6196 (because they haven't been expanded yet), we can at least
6197 verify that all of the types used for the non-type
6198 template parameter pack are, in fact, valid for non-type
6199 template parameters. */
6201 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6203 int j
, len
= TREE_VEC_LENGTH (packed_types
);
6204 for (j
= 0; j
< len
; ++j
)
6206 tree t
= TREE_VEC_ELT (packed_types
, j
);
6207 if (invalid_nontype_parm_type_p (t
, complain
))
6208 return error_mark_node
;
6213 /* Convert the remaining arguments, which will be a part of the
6214 parameter pack "parm". */
6215 for (; arg_idx
< nargs
; ++arg_idx
)
6217 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6218 tree actual_parm
= TREE_VALUE (parm
);
6220 if (packed_types
&& !PACK_EXPANSION_P (arg
))
6222 /* When we have a vector of types (corresponding to the
6223 non-type template parameter pack that uses parameter
6224 packs in its type, as mention above), and the
6225 argument is not an expansion (which expands to a
6226 currently unknown number of arguments), clone the
6227 parm and give it the next type in PACKED_TYPES. */
6228 actual_parm
= copy_node (actual_parm
);
6229 TREE_TYPE (actual_parm
) =
6230 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
6233 if (arg
!= error_mark_node
)
6234 arg
= convert_template_argument (actual_parm
,
6235 arg
, new_args
, complain
, parm_idx
,
6237 if (arg
== error_mark_node
)
6239 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
6242 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6243 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
6244 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
6247 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
6248 TREE_TYPE (argument_pack
)
6249 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
6250 TREE_CONSTANT (argument_pack
) = 1;
6253 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
6254 #ifdef ENABLE_CHECKING
6255 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
6256 TREE_VEC_LENGTH (packed_args
));
6258 return argument_pack
;
6261 /* Convert all template arguments to their appropriate types, and
6262 return a vector containing the innermost resulting template
6263 arguments. If any error occurs, return error_mark_node. Error and
6264 warning messages are issued under control of COMPLAIN.
6266 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6267 for arguments not specified in ARGS. Otherwise, if
6268 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6269 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6270 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6274 coerce_template_parms (tree parms
,
6277 tsubst_flags_t complain
,
6278 bool require_all_args
,
6279 bool use_default_args
)
6281 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
6284 tree new_inner_args
;
6285 int saved_unevaluated_operand
;
6286 int saved_inhibit_evaluation_warnings
;
6288 /* When used as a boolean value, indicates whether this is a
6289 variadic template parameter list. Since it's an int, we can also
6290 subtract it from nparms to get the number of non-variadic
6294 if (args
== error_mark_node
)
6295 return error_mark_node
;
6297 nparms
= TREE_VEC_LENGTH (parms
);
6299 /* Determine if there are any parameter packs. */
6300 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
6302 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
6303 if (template_parameter_pack_p (tparm
))
6307 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
6308 /* If there are 0 or 1 parameter packs, we need to expand any argument
6309 packs so that we can deduce a parameter pack from some non-packed args
6310 followed by an argument pack, as in variadic85.C. If there are more
6311 than that, we need to leave argument packs intact so the arguments are
6312 assigned to the right parameter packs. This should only happen when
6313 dealing with a nested class inside a partial specialization of a class
6314 template, as in variadic92.C. */
6315 if (variadic_p
<= 1)
6316 inner_args
= expand_template_argument_pack (inner_args
);
6318 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6319 if ((nargs
> nparms
&& !variadic_p
)
6320 || (nargs
< nparms
- variadic_p
6322 && (!use_default_args
6323 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
6324 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
6326 if (complain
& tf_error
)
6331 error ("wrong number of template arguments "
6332 "(%d, should be %d or more)", nargs
, nparms
);
6335 error ("wrong number of template arguments "
6336 "(%d, should be %d)", nargs
, nparms
);
6339 error ("provided for %q+D", in_decl
);
6342 return error_mark_node
;
6345 /* We need to evaluate the template arguments, even though this
6346 template-id may be nested within a "sizeof". */
6347 saved_unevaluated_operand
= cp_unevaluated_operand
;
6348 cp_unevaluated_operand
= 0;
6349 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6350 c_inhibit_evaluation_warnings
= 0;
6351 new_inner_args
= make_tree_vec (nparms
);
6352 new_args
= add_outermost_template_args (args
, new_inner_args
);
6353 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
6358 /* Get the Ith template parameter. */
6359 parm
= TREE_VEC_ELT (parms
, parm_idx
);
6361 if (parm
== error_mark_node
)
6363 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
6367 /* Calculate the next argument. */
6368 if (arg_idx
< nargs
)
6369 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6373 if (template_parameter_pack_p (TREE_VALUE (parm
))
6374 && !(arg
&& ARGUMENT_PACK_P (arg
)))
6376 /* All remaining arguments will be placed in the
6377 template parameter pack PARM. */
6378 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
6379 inner_args
, arg_idx
,
6383 /* Store this argument. */
6384 if (arg
== error_mark_node
)
6386 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
6388 /* We are done with all of the arguments. */
6395 if (PACK_EXPANSION_P (arg
))
6397 if (complain
& tf_error
)
6399 /* FIXME this restriction was removed by N2555; see
6401 /* If ARG is a pack expansion, but PARM is not a
6402 template parameter pack (if it were, we would have
6403 handled it above), we're trying to expand into a
6404 fixed-length argument list. */
6405 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
6406 sorry ("cannot expand %<%E%> into a fixed-length "
6407 "argument list", arg
);
6409 sorry ("cannot expand %<%T%> into a fixed-length "
6410 "argument list", arg
);
6415 else if (require_all_args
)
6417 /* There must be a default arg in this case. */
6418 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
6420 /* The position of the first default template argument,
6421 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6423 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6424 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
, arg_idx
);
6429 if (arg
== error_mark_node
)
6431 if (complain
& tf_error
)
6432 error ("template argument %d is invalid", arg_idx
+ 1);
6435 /* This only occurs if there was an error in the template
6436 parameter list itself (which we would already have
6437 reported) that we are trying to recover from, e.g., a class
6438 template with a parameter list such as
6439 template<typename..., typename>. */
6442 arg
= convert_template_argument (TREE_VALUE (parm
),
6443 arg
, new_args
, complain
,
6446 if (arg
== error_mark_node
)
6448 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
6450 cp_unevaluated_operand
= saved_unevaluated_operand
;
6451 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
6454 return error_mark_node
;
6456 #ifdef ENABLE_CHECKING
6457 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6458 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6459 TREE_VEC_LENGTH (new_inner_args
));
6462 return new_inner_args
;
6465 /* Returns 1 if template args OT and NT are equivalent. */
6468 template_args_equal (tree ot
, tree nt
)
6473 if (TREE_CODE (nt
) == TREE_VEC
)
6474 /* For member templates */
6475 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
6476 else if (PACK_EXPANSION_P (ot
))
6477 return PACK_EXPANSION_P (nt
)
6478 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
6479 PACK_EXPANSION_PATTERN (nt
));
6480 else if (ARGUMENT_PACK_P (ot
))
6485 if (!ARGUMENT_PACK_P (nt
))
6488 opack
= ARGUMENT_PACK_ARGS (ot
);
6489 npack
= ARGUMENT_PACK_ARGS (nt
);
6490 len
= TREE_VEC_LENGTH (opack
);
6491 if (TREE_VEC_LENGTH (npack
) != len
)
6493 for (i
= 0; i
< len
; ++i
)
6494 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
6495 TREE_VEC_ELT (npack
, i
)))
6499 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
6501 /* We get here probably because we are in the middle of substituting
6502 into the pattern of a pack expansion. In that case the
6503 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6504 interested in. So we want to use the initial pack argument for
6506 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
6507 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
6508 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
6509 return template_args_equal (ot
, nt
);
6511 else if (TYPE_P (nt
))
6512 return TYPE_P (ot
) && same_type_p (ot
, nt
);
6513 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
6516 return cp_tree_equal (ot
, nt
);
6519 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6520 of template arguments. Returns 0 otherwise. */
6523 comp_template_args (tree oldargs
, tree newargs
)
6527 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
6530 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
6532 tree nt
= TREE_VEC_ELT (newargs
, i
);
6533 tree ot
= TREE_VEC_ELT (oldargs
, i
);
6535 if (! template_args_equal (ot
, nt
))
6542 add_pending_template (tree d
)
6544 tree ti
= (TYPE_P (d
)
6545 ? CLASSTYPE_TEMPLATE_INFO (d
)
6546 : DECL_TEMPLATE_INFO (d
));
6547 struct pending_template
*pt
;
6550 if (TI_PENDING_TEMPLATE_FLAG (ti
))
6553 /* We are called both from instantiate_decl, where we've already had a
6554 tinst_level pushed, and instantiate_template, where we haven't.
6556 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
6559 push_tinst_level (d
);
6561 pt
= ggc_alloc_pending_template ();
6563 pt
->tinst
= current_tinst_level
;
6564 if (last_pending_template
)
6565 last_pending_template
->next
= pt
;
6567 pending_templates
= pt
;
6569 last_pending_template
= pt
;
6571 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
6578 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6579 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6580 documentation for TEMPLATE_ID_EXPR. */
6583 lookup_template_function (tree fns
, tree arglist
)
6587 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
6588 return error_mark_node
;
6590 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
6591 gcc_assert (fns
&& (is_overloaded_fn (fns
)
6592 || TREE_CODE (fns
) == IDENTIFIER_NODE
));
6594 if (BASELINK_P (fns
))
6596 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
6598 BASELINK_FUNCTIONS (fns
),
6603 type
= TREE_TYPE (fns
);
6604 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
6605 type
= unknown_type_node
;
6607 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
6610 /* Within the scope of a template class S<T>, the name S gets bound
6611 (in build_self_reference) to a TYPE_DECL for the class, not a
6612 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6613 or one of its enclosing classes, and that type is a template,
6614 return the associated TEMPLATE_DECL. Otherwise, the original
6617 Also handle the case when DECL is a TREE_LIST of ambiguous
6618 injected-class-names from different bases. */
6621 maybe_get_template_decl_from_type_decl (tree decl
)
6623 if (decl
== NULL_TREE
)
6626 /* DR 176: A lookup that finds an injected-class-name (10.2
6627 [class.member.lookup]) can result in an ambiguity in certain cases
6628 (for example, if it is found in more than one base class). If all of
6629 the injected-class-names that are found refer to specializations of
6630 the same class template, and if the name is followed by a
6631 template-argument-list, the reference refers to the class template
6632 itself and not a specialization thereof, and is not ambiguous. */
6633 if (TREE_CODE (decl
) == TREE_LIST
)
6635 tree t
, tmpl
= NULL_TREE
;
6636 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
6638 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
6641 else if (tmpl
!= elt
)
6644 if (tmpl
&& t
== NULL_TREE
)
6650 return (decl
!= NULL_TREE
6651 && DECL_SELF_REFERENCE_P (decl
)
6652 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
6653 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
6656 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6657 parameters, find the desired type.
6659 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6661 IN_DECL, if non-NULL, is the template declaration we are trying to
6664 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6665 the class we are looking up.
6667 Issue error and warning messages under control of COMPLAIN.
6669 If the template class is really a local class in a template
6670 function, then the FUNCTION_CONTEXT is the function in which it is
6673 ??? Note that this function is currently called *twice* for each
6674 template-id: the first time from the parser, while creating the
6675 incomplete type (finish_template_type), and the second type during the
6676 real instantiation (instantiate_template_class). This is surely something
6677 that we want to avoid. It also causes some problems with argument
6678 coercion (see convert_nontype_argument for more information on this). */
6681 lookup_template_class (tree d1
,
6686 tsubst_flags_t complain
)
6688 tree templ
= NULL_TREE
, parmlist
;
6695 timevar_push (TV_NAME_LOOKUP
);
6697 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
6699 tree value
= innermost_non_namespace_value (d1
);
6700 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
6705 push_decl_namespace (context
);
6706 templ
= lookup_name (d1
);
6707 templ
= maybe_get_template_decl_from_type_decl (templ
);
6709 pop_decl_namespace ();
6712 context
= DECL_CONTEXT (templ
);
6714 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
6716 tree type
= TREE_TYPE (d1
);
6718 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6719 an implicit typename for the second A. Deal with it. */
6720 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
6721 type
= TREE_TYPE (type
);
6723 if (CLASSTYPE_TEMPLATE_INFO (type
))
6725 templ
= CLASSTYPE_TI_TEMPLATE (type
);
6726 d1
= DECL_NAME (templ
);
6729 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
6730 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
6732 templ
= TYPE_TI_TEMPLATE (d1
);
6733 d1
= DECL_NAME (templ
);
6735 else if (TREE_CODE (d1
) == TEMPLATE_DECL
6736 && DECL_TEMPLATE_RESULT (d1
)
6737 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
6740 d1
= DECL_NAME (templ
);
6741 context
= DECL_CONTEXT (templ
);
6744 /* Issue an error message if we didn't find a template. */
6747 if (complain
& tf_error
)
6748 error ("%qT is not a template", d1
);
6749 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
6752 if (TREE_CODE (templ
) != TEMPLATE_DECL
6753 /* Make sure it's a user visible template, if it was named by
6755 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
6756 && !PRIMARY_TEMPLATE_P (templ
)))
6758 if (complain
& tf_error
)
6760 error ("non-template type %qT used as a template", d1
);
6762 error ("for template declaration %q+D", in_decl
);
6764 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
6767 complain
&= ~tf_user
;
6769 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
6771 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
6772 template arguments */
6778 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
6780 /* Consider an example where a template template parameter declared as
6782 template <class T, class U = std::allocator<T> > class TT
6784 The template parameter level of T and U are one level larger than
6785 of TT. To proper process the default argument of U, say when an
6786 instantiation `TT<int>' is seen, we need to build the full
6787 arguments containing {int} as the innermost level. Outer levels,
6788 available when not appearing as default template argument, can be
6789 obtained from the arguments of the enclosing template.
6791 Suppose that TT is later substituted with std::vector. The above
6792 instantiation is `TT<int, std::allocator<T> >' with TT at
6793 level 1, and T at level 2, while the template arguments at level 1
6794 becomes {std::vector} and the inner level 2 is {int}. */
6796 outer
= DECL_CONTEXT (templ
);
6798 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
6799 else if (current_template_parms
)
6800 /* This is an argument of the current template, so we haven't set
6801 DECL_CONTEXT yet. */
6802 outer
= current_template_args ();
6805 arglist
= add_to_template_args (outer
, arglist
);
6807 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
6809 /*require_all_args=*/true,
6810 /*use_default_args=*/true);
6811 if (arglist2
== error_mark_node
6812 || (!uses_template_parms (arglist2
)
6813 && check_instantiated_args (templ
, arglist2
, complain
)))
6814 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
6816 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
6817 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, parm
);
6821 tree template_type
= TREE_TYPE (templ
);
6824 tree found
= NULL_TREE
;
6827 int is_dependent_type
;
6828 int use_partial_inst_tmpl
= false;
6830 gen_tmpl
= most_general_template (templ
);
6831 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
6832 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
6833 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
6835 if (arg_depth
== 1 && parm_depth
> 1)
6837 /* We've been given an incomplete set of template arguments.
6840 template <class T> struct S1 {
6841 template <class U> struct S2 {};
6842 template <class U> struct S2<U*> {};
6845 we will be called with an ARGLIST of `U*', but the
6846 TEMPLATE will be `template <class T> template
6847 <class U> struct S1<T>::S2'. We must fill in the missing
6850 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
6852 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
6855 /* Now we should have enough arguments. */
6856 gcc_assert (parm_depth
== arg_depth
);
6858 /* From here on, we're only interested in the most general
6861 /* Calculate the BOUND_ARGS. These will be the args that are
6862 actually tsubst'd into the definition to create the
6866 /* We have multiple levels of arguments to coerce, at once. */
6868 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
6870 tree bound_args
= make_tree_vec (parm_depth
);
6872 for (i
= saved_depth
,
6873 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
6874 i
> 0 && t
!= NULL_TREE
;
6875 --i
, t
= TREE_CHAIN (t
))
6878 if (i
== saved_depth
)
6879 a
= coerce_template_parms (TREE_VALUE (t
),
6882 /*require_all_args=*/true,
6883 /*use_default_args=*/true);
6885 /* Outer levels should have already been coerced. */
6886 a
= TMPL_ARGS_LEVEL (arglist
, i
);
6888 /* Don't process further if one of the levels fails. */
6889 if (a
== error_mark_node
)
6891 /* Restore the ARGLIST to its full size. */
6892 TREE_VEC_LENGTH (arglist
) = saved_depth
;
6893 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
6896 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
6898 /* We temporarily reduce the length of the ARGLIST so
6899 that coerce_template_parms will see only the arguments
6900 corresponding to the template parameters it is
6902 TREE_VEC_LENGTH (arglist
)--;
6905 /* Restore the ARGLIST to its full size. */
6906 TREE_VEC_LENGTH (arglist
) = saved_depth
;
6908 arglist
= bound_args
;
6912 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
6913 INNERMOST_TEMPLATE_ARGS (arglist
),
6916 /*require_all_args=*/true,
6917 /*use_default_args=*/true);
6919 if (arglist
== error_mark_node
)
6920 /* We were unable to bind the arguments. */
6921 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
6923 /* In the scope of a template class, explicit references to the
6924 template class refer to the type of the template, not any
6925 instantiation of it. For example, in:
6927 template <class T> class C { void f(C<T>); }
6929 the `C<T>' is just the same as `C'. Outside of the
6930 class, however, such a reference is an instantiation. */
6932 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
6933 || currently_open_class (template_type
))
6934 /* comp_template_args is expensive, check it last. */
6935 && comp_template_args (TYPE_TI_ARGS (template_type
),
6937 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, template_type
);
6939 /* If we already have this specialization, return it. */
6940 elt
.tmpl
= gen_tmpl
;
6942 hash
= hash_specialization (&elt
);
6943 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
6947 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, entry
->spec
);
6949 is_dependent_type
= uses_template_parms (arglist
);
6951 /* If the deduced arguments are invalid, then the binding
6953 if (!is_dependent_type
6954 && check_instantiated_args (gen_tmpl
,
6955 INNERMOST_TEMPLATE_ARGS (arglist
),
6957 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
6959 if (!is_dependent_type
6960 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
6961 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
6962 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
6964 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
6965 DECL_NAME (gen_tmpl
),
6966 /*tag_scope=*/ts_global
);
6967 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, found
);
6970 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
6973 context
= global_namespace
;
6975 /* Create the type. */
6976 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
6978 if (!is_dependent_type
)
6980 set_current_access_from_decl (TYPE_NAME (template_type
));
6981 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
6982 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
6983 arglist
, complain
, in_decl
),
6984 SCOPED_ENUM_P (template_type
), NULL
);
6988 /* We don't want to call start_enum for this type, since
6989 the values for the enumeration constants may involve
6990 template parameters. And, no one should be interested
6991 in the enumeration constants for such a type. */
6992 t
= cxx_make_type (ENUMERAL_TYPE
);
6993 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
6995 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
6996 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
6997 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7001 t
= make_class_type (TREE_CODE (template_type
));
7002 CLASSTYPE_DECLARED_CLASS (t
)
7003 = CLASSTYPE_DECLARED_CLASS (template_type
);
7004 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7005 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7007 /* A local class. Make sure the decl gets registered properly. */
7008 if (context
== current_function_decl
)
7009 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7011 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7012 /* This instantiation is another name for the primary
7013 template type. Set the TYPE_CANONICAL field
7015 TYPE_CANONICAL (t
) = template_type
;
7016 else if (any_template_arguments_need_structural_equality_p (arglist
))
7017 /* Some of the template arguments require structural
7018 equality testing, so this template class requires
7019 structural equality testing. */
7020 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7023 /* If we called start_enum or pushtag above, this information
7024 will already be set up. */
7027 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
7029 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
7030 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
7031 DECL_SOURCE_LOCATION (type_decl
)
7032 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
7035 type_decl
= TYPE_NAME (t
);
7037 TREE_PRIVATE (type_decl
)
7038 = TREE_PRIVATE (TYPE_STUB_DECL (template_type
));
7039 TREE_PROTECTED (type_decl
)
7040 = TREE_PROTECTED (TYPE_STUB_DECL (template_type
));
7041 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
7043 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
7044 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
7047 /* Let's consider the explicit specialization of a member
7048 of a class template specialization that is implicitely instantiated,
7053 template<class U> struct M {}; //#0
7058 struct S<int>::M<char> //#1
7062 [temp.expl.spec]/4 says this is valid.
7064 In this case, when we write:
7067 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7070 When we encounter #1, we want to store the partial instantiation
7071 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
7073 For all cases other than this "explicit specialization of member of a
7074 class template", we just want to store the most general template into
7075 the CLASSTYPE_TI_TEMPLATE of M.
7077 This case of "explicit specialization of member of a class template"
7079 1/ the enclosing class is an instantiation of, and therefore not
7080 the same as, the context of the most general template, and
7081 2/ we aren't looking at the partial instantiation itself, i.e.
7082 the innermost arguments are not the same as the innermost parms of
7083 the most general template.
7085 So it's only when 1/ and 2/ happens that we want to use the partial
7086 instantiation of the member template in lieu of its most general
7089 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
7090 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
7091 /* the enclosing class must be an instantiation... */
7092 && CLASS_TYPE_P (context
)
7093 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
7095 tree partial_inst_args
;
7096 TREE_VEC_LENGTH (arglist
)--;
7097 ++processing_template_decl
;
7099 tsubst (INNERMOST_TEMPLATE_ARGS
7100 (CLASSTYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
7101 arglist
, complain
, NULL_TREE
);
7102 --processing_template_decl
;
7103 TREE_VEC_LENGTH (arglist
)++;
7104 use_partial_inst_tmpl
=
7105 /*...and we must not be looking at the partial instantiation
7107 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
7111 if (!use_partial_inst_tmpl
)
7112 /* This case is easy; there are no member templates involved. */
7116 /* This is a full instantiation of a member template. Find
7117 the partial instantiation of which this is an instance. */
7119 /* Temporarily reduce by one the number of levels in the ARGLIST
7120 so as to avoid comparing the last set of arguments. */
7121 TREE_VEC_LENGTH (arglist
)--;
7122 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
7123 TREE_VEC_LENGTH (arglist
)++;
7124 found
= CLASSTYPE_TI_TEMPLATE (found
);
7127 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
7130 slot
= (spec_entry
**) htab_find_slot_with_hash (type_specializations
,
7131 &elt
, hash
, INSERT
);
7132 *slot
= ggc_alloc_spec_entry ();
7135 /* Note this use of the partial instantiation so we can check it
7136 later in maybe_process_partial_specialization. */
7137 DECL_TEMPLATE_INSTANTIATIONS (templ
)
7138 = tree_cons (arglist
, t
,
7139 DECL_TEMPLATE_INSTANTIATIONS (templ
));
7141 if (TREE_CODE (t
) == ENUMERAL_TYPE
&& !is_dependent_type
)
7142 /* Now that the type has been registered on the instantiations
7143 list, we set up the enumerators. Because the enumeration
7144 constants may involve the enumeration type itself, we make
7145 sure to register the type first, and then create the
7146 constants. That way, doing tsubst_expr for the enumeration
7147 constants won't result in recursive calls here; we'll find
7148 the instantiation and exit above. */
7149 tsubst_enum (template_type
, t
, arglist
);
7151 if (is_dependent_type
)
7152 /* If the type makes use of template parameters, the
7153 code that generates debugging information will crash. */
7154 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
7156 /* Possibly limit visibility based on template args. */
7157 TREE_PUBLIC (type_decl
) = 1;
7158 determine_visibility (type_decl
);
7160 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
7162 timevar_pop (TV_NAME_LOOKUP
);
7169 /* True when we should also visit template parameters that occur in
7170 non-deduced contexts. */
7171 bool include_nondeduced_p
;
7172 struct pointer_set_t
*visited
;
7175 /* Called from for_each_template_parm via walk_tree. */
7178 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
7181 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
7182 tree_fn_t fn
= pfd
->fn
;
7183 void *data
= pfd
->data
;
7186 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
7187 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
7188 pfd
->include_nondeduced_p
))
7189 return error_mark_node
;
7191 switch (TREE_CODE (t
))
7194 if (TYPE_PTRMEMFUNC_P (t
))
7200 if (!TYPE_TEMPLATE_INFO (t
))
7202 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
7203 fn
, data
, pfd
->visited
,
7204 pfd
->include_nondeduced_p
))
7205 return error_mark_node
;
7209 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
7210 fn
, data
, pfd
->visited
,
7211 pfd
->include_nondeduced_p
)
7212 || for_each_template_parm (TYPE_MAX_VALUE (t
),
7213 fn
, data
, pfd
->visited
,
7214 pfd
->include_nondeduced_p
))
7215 return error_mark_node
;
7219 /* Since we're not going to walk subtrees, we have to do this
7221 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
7222 pfd
->visited
, pfd
->include_nondeduced_p
))
7223 return error_mark_node
;
7227 /* Check the return type. */
7228 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7229 pfd
->include_nondeduced_p
))
7230 return error_mark_node
;
7232 /* Check the parameter types. Since default arguments are not
7233 instantiated until they are needed, the TYPE_ARG_TYPES may
7234 contain expressions that involve template parameters. But,
7235 no-one should be looking at them yet. And, once they're
7236 instantiated, they don't contain template parameters, so
7237 there's no point in looking at them then, either. */
7241 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
7242 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
7243 pfd
->visited
, pfd
->include_nondeduced_p
))
7244 return error_mark_node
;
7246 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7247 want walk_tree walking into them itself. */
7253 if (pfd
->include_nondeduced_p
7254 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
7256 pfd
->include_nondeduced_p
))
7257 return error_mark_node
;
7262 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
7263 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
7264 pfd
->visited
, pfd
->include_nondeduced_p
))
7265 return error_mark_node
;
7270 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
7271 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
7272 pfd
->visited
, pfd
->include_nondeduced_p
))
7273 return error_mark_node
;
7274 if (DECL_CONTEXT (t
)
7275 && pfd
->include_nondeduced_p
7276 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
7277 pfd
->visited
, pfd
->include_nondeduced_p
))
7278 return error_mark_node
;
7281 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7282 /* Record template parameters such as `T' inside `TT<T>'. */
7283 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
7284 pfd
->include_nondeduced_p
))
7285 return error_mark_node
;
7288 case TEMPLATE_TEMPLATE_PARM
:
7289 case TEMPLATE_TYPE_PARM
:
7290 case TEMPLATE_PARM_INDEX
:
7291 if (fn
&& (*fn
)(t
, data
))
7292 return error_mark_node
;
7294 return error_mark_node
;
7298 /* A template template parameter is encountered. */
7299 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
7300 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7301 pfd
->include_nondeduced_p
))
7302 return error_mark_node
;
7304 /* Already substituted template template parameter */
7310 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
7312 pfd
->include_nondeduced_p
))
7313 return error_mark_node
;
7317 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
7318 && pfd
->include_nondeduced_p
7319 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7320 (TREE_TYPE (t
)), fn
, data
,
7321 pfd
->visited
, pfd
->include_nondeduced_p
))
7322 return error_mark_node
;
7327 /* If there's no type, then this thing must be some expression
7328 involving template parameters. */
7329 if (!fn
&& !TREE_TYPE (t
))
7330 return error_mark_node
;
7335 case REINTERPRET_CAST_EXPR
:
7336 case CONST_CAST_EXPR
:
7337 case STATIC_CAST_EXPR
:
7338 case DYNAMIC_CAST_EXPR
:
7342 case PSEUDO_DTOR_EXPR
:
7344 return error_mark_node
;
7351 /* We didn't find any template parameters we liked. */
7355 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7356 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7357 call FN with the parameter and the DATA.
7358 If FN returns nonzero, the iteration is terminated, and
7359 for_each_template_parm returns 1. Otherwise, the iteration
7360 continues. If FN never returns a nonzero value, the value
7361 returned by for_each_template_parm is 0. If FN is NULL, it is
7362 considered to be the function which always returns 1.
7364 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7365 parameters that occur in non-deduced contexts. When false, only
7366 visits those template parameters that can be deduced. */
7369 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
7370 struct pointer_set_t
*visited
,
7371 bool include_nondeduced_p
)
7373 struct pair_fn_data pfd
;
7379 pfd
.include_nondeduced_p
= include_nondeduced_p
;
7381 /* Walk the tree. (Conceptually, we would like to walk without
7382 duplicates, but for_each_template_parm_r recursively calls
7383 for_each_template_parm, so we would need to reorganize a fair
7384 bit to use walk_tree_without_duplicates, so we keep our own
7387 pfd
.visited
= visited
;
7389 pfd
.visited
= pointer_set_create ();
7390 result
= cp_walk_tree (&t
,
7391 for_each_template_parm_r
,
7393 pfd
.visited
) != NULL_TREE
;
7398 pointer_set_destroy (pfd
.visited
);
7405 /* Returns true if T depends on any template parameter. */
7408 uses_template_parms (tree t
)
7411 int saved_processing_template_decl
;
7413 saved_processing_template_decl
= processing_template_decl
;
7414 if (!saved_processing_template_decl
)
7415 processing_template_decl
= 1;
7417 dependent_p
= dependent_type_p (t
);
7418 else if (TREE_CODE (t
) == TREE_VEC
)
7419 dependent_p
= any_dependent_template_arguments_p (t
);
7420 else if (TREE_CODE (t
) == TREE_LIST
)
7421 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
7422 || uses_template_parms (TREE_CHAIN (t
)));
7423 else if (TREE_CODE (t
) == TYPE_DECL
)
7424 dependent_p
= dependent_type_p (TREE_TYPE (t
));
7427 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
7428 || TREE_CODE (t
) == OVERLOAD
7429 || TREE_CODE (t
) == BASELINK
7430 || TREE_CODE (t
) == IDENTIFIER_NODE
7431 || TREE_CODE (t
) == TRAIT_EXPR
7432 || TREE_CODE (t
) == CONSTRUCTOR
7433 || CONSTANT_CLASS_P (t
))
7434 dependent_p
= (type_dependent_expression_p (t
)
7435 || value_dependent_expression_p (t
));
7438 gcc_assert (t
== error_mark_node
);
7439 dependent_p
= false;
7442 processing_template_decl
= saved_processing_template_decl
;
7447 /* Returns true if T depends on any template parameter with level LEVEL. */
7450 uses_template_parms_level (tree t
, int level
)
7452 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
7453 /*include_nondeduced_p=*/true);
7456 static int tinst_depth
;
7457 extern int max_tinst_depth
;
7458 #ifdef GATHER_STATISTICS
7461 static int tinst_level_tick
;
7462 static int last_template_error_tick
;
7464 /* We're starting to instantiate D; record the template instantiation context
7465 for diagnostics and to restore it later. */
7468 push_tinst_level (tree d
)
7470 struct tinst_level
*new_level
;
7472 if (tinst_depth
>= max_tinst_depth
)
7474 /* If the instantiation in question still has unbound template parms,
7475 we don't really care if we can't instantiate it, so just return.
7476 This happens with base instantiation for implicit `typename'. */
7477 if (uses_template_parms (d
))
7480 last_template_error_tick
= tinst_level_tick
;
7481 error ("template instantiation depth exceeds maximum of %d (use "
7482 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7483 max_tinst_depth
, d
);
7485 print_instantiation_context ();
7490 new_level
= ggc_alloc_tinst_level ();
7491 new_level
->decl
= d
;
7492 new_level
->locus
= input_location
;
7493 new_level
->in_system_header_p
= in_system_header
;
7494 new_level
->next
= current_tinst_level
;
7495 current_tinst_level
= new_level
;
7498 #ifdef GATHER_STATISTICS
7499 if (tinst_depth
> depth_reached
)
7500 depth_reached
= tinst_depth
;
7507 /* We're done instantiating this template; return to the instantiation
7511 pop_tinst_level (void)
7513 /* Restore the filename and line number stashed away when we started
7514 this instantiation. */
7515 input_location
= current_tinst_level
->locus
;
7516 current_tinst_level
= current_tinst_level
->next
;
7521 /* We're instantiating a deferred template; restore the template
7522 instantiation context in which the instantiation was requested, which
7523 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7526 reopen_tinst_level (struct tinst_level
*level
)
7528 struct tinst_level
*t
;
7531 for (t
= level
; t
; t
= t
->next
)
7534 current_tinst_level
= level
;
7539 /* Returns the TINST_LEVEL which gives the original instantiation
7542 struct tinst_level
*
7543 outermost_tinst_level (void)
7545 struct tinst_level
*level
= current_tinst_level
;
7548 level
= level
->next
;
7552 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
7555 parameter_of_template_p (tree parm
, tree templ
)
7560 if (!parm
|| !templ
)
7563 gcc_assert (DECL_TEMPLATE_PARM_P (parm
));
7564 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
7566 parms
= DECL_TEMPLATE_PARMS (templ
);
7567 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
7569 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
7570 if (parm
== TREE_VALUE (TREE_VEC_ELT (parms
, i
)))
7576 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
7577 vector of template arguments, as for tsubst.
7579 Returns an appropriate tsubst'd friend declaration. */
7582 tsubst_friend_function (tree decl
, tree args
)
7586 if (TREE_CODE (decl
) == FUNCTION_DECL
7587 && DECL_TEMPLATE_INSTANTIATION (decl
)
7588 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
7589 /* This was a friend declared with an explicit template
7590 argument list, e.g.:
7594 to indicate that f was a template instantiation, not a new
7595 function declaration. Now, we have to figure out what
7596 instantiation of what template. */
7598 tree template_id
, arglist
, fns
;
7601 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
7603 /* Friend functions are looked up in the containing namespace scope.
7604 We must enter that scope, to avoid finding member functions of the
7605 current class with same name. */
7606 push_nested_namespace (ns
);
7607 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
7608 tf_warning_or_error
, NULL_TREE
,
7609 /*integral_constant_expression_p=*/false);
7610 pop_nested_namespace (ns
);
7611 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
7612 tf_warning_or_error
, NULL_TREE
);
7613 template_id
= lookup_template_function (fns
, arglist
);
7615 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
7616 tmpl
= determine_specialization (template_id
, new_friend
,
7618 /*need_member_template=*/0,
7619 TREE_VEC_LENGTH (args
),
7621 return instantiate_template (tmpl
, new_args
, tf_error
);
7624 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
7626 /* The NEW_FRIEND will look like an instantiation, to the
7627 compiler, but is not an instantiation from the point of view of
7628 the language. For example, we might have had:
7630 template <class T> struct S {
7631 template <class U> friend void f(T, U);
7634 Then, in S<int>, template <class U> void f(int, U) is not an
7635 instantiation of anything. */
7636 if (new_friend
== error_mark_node
)
7637 return error_mark_node
;
7639 DECL_USE_TEMPLATE (new_friend
) = 0;
7640 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
7642 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
7643 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
7644 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
7647 /* The mangled name for the NEW_FRIEND is incorrect. The function
7648 is not a template instantiation and should not be mangled like
7649 one. Therefore, we forget the mangling here; we'll recompute it
7650 later if we need it. */
7651 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
7653 SET_DECL_RTL (new_friend
, NULL
);
7654 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
7657 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
7660 tree new_friend_template_info
;
7661 tree new_friend_result_template_info
;
7663 int new_friend_is_defn
;
7665 /* We must save some information from NEW_FRIEND before calling
7666 duplicate decls since that function will free NEW_FRIEND if
7668 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
7669 new_friend_is_defn
=
7670 (DECL_INITIAL (DECL_TEMPLATE_RESULT
7671 (template_for_substitution (new_friend
)))
7673 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
7675 /* This declaration is a `primary' template. */
7676 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
7678 new_friend_result_template_info
7679 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
7682 new_friend_result_template_info
= NULL_TREE
;
7684 /* Make the init_value nonzero so pushdecl knows this is a defn. */
7685 if (new_friend_is_defn
)
7686 DECL_INITIAL (new_friend
) = error_mark_node
;
7688 /* Inside pushdecl_namespace_level, we will push into the
7689 current namespace. However, the friend function should go
7690 into the namespace of the template. */
7691 ns
= decl_namespace_context (new_friend
);
7692 push_nested_namespace (ns
);
7693 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
7694 pop_nested_namespace (ns
);
7696 if (old_decl
== error_mark_node
)
7697 return error_mark_node
;
7699 if (old_decl
!= new_friend
)
7701 /* This new friend declaration matched an existing
7702 declaration. For example, given:
7704 template <class T> void f(T);
7705 template <class U> class C {
7706 template <class T> friend void f(T) {}
7709 the friend declaration actually provides the definition
7710 of `f', once C has been instantiated for some type. So,
7711 old_decl will be the out-of-class template declaration,
7712 while new_friend is the in-class definition.
7714 But, if `f' was called before this point, the
7715 instantiation of `f' will have DECL_TI_ARGS corresponding
7716 to `T' but not to `U', references to which might appear
7717 in the definition of `f'. Previously, the most general
7718 template for an instantiation of `f' was the out-of-class
7719 version; now it is the in-class version. Therefore, we
7720 run through all specialization of `f', adding to their
7721 DECL_TI_ARGS appropriately. In particular, they need a
7722 new set of outer arguments, corresponding to the
7723 arguments for this class instantiation.
7725 The same situation can arise with something like this:
7728 template <class T> class C {
7732 when `C<int>' is instantiated. Now, `f(int)' is defined
7735 if (!new_friend_is_defn
)
7736 /* On the other hand, if the in-class declaration does
7737 *not* provide a definition, then we don't want to alter
7738 existing definitions. We can just leave everything
7743 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
7744 tree new_args
= TI_ARGS (new_friend_template_info
);
7746 /* Overwrite whatever template info was there before, if
7747 any, with the new template information pertaining to
7749 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
7751 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
7753 /* We should have called reregister_specialization in
7755 gcc_assert (retrieve_specialization (new_template
,
7759 /* Instantiate it if the global has already been used. */
7760 if (DECL_ODR_USED (old_decl
))
7761 instantiate_decl (old_decl
, /*defer_ok=*/true,
7762 /*expl_inst_class_mem_p=*/false);
7768 /* Indicate that the old function template is a partial
7770 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
7771 = new_friend_result_template_info
;
7773 gcc_assert (new_template
7774 == most_general_template (new_template
));
7775 gcc_assert (new_template
!= old_decl
);
7777 /* Reassign any specializations already in the hash table
7778 to the new more general template, and add the
7779 additional template args. */
7780 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
7784 tree spec
= TREE_VALUE (t
);
7787 elt
.tmpl
= old_decl
;
7788 elt
.args
= DECL_TI_ARGS (spec
);
7789 elt
.spec
= NULL_TREE
;
7791 htab_remove_elt (decl_specializations
, &elt
);
7794 = add_outermost_template_args (new_args
,
7795 DECL_TI_ARGS (spec
));
7797 register_specialization
7798 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
7801 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
7805 /* The information from NEW_FRIEND has been merged into OLD_DECL
7806 by duplicate_decls. */
7807 new_friend
= old_decl
;
7812 tree context
= DECL_CONTEXT (new_friend
);
7816 template <class T> class C {
7817 template <class U> friend void C1<U>::f (); // case 1
7818 friend void C2<T>::f (); // case 2
7820 we only need to make sure CONTEXT is a complete type for
7821 case 2. To distinguish between the two cases, we note that
7822 CONTEXT of case 1 remains dependent type after tsubst while
7823 this isn't true for case 2. */
7824 ++processing_template_decl
;
7825 dependent_p
= dependent_type_p (context
);
7826 --processing_template_decl
;
7829 && !complete_type_or_else (context
, NULL_TREE
))
7830 return error_mark_node
;
7832 if (COMPLETE_TYPE_P (context
))
7834 /* Check to see that the declaration is really present, and,
7835 possibly obtain an improved declaration. */
7836 tree fn
= check_classfn (context
,
7837 new_friend
, NULL_TREE
);
7847 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
7848 template arguments, as for tsubst.
7850 Returns an appropriate tsubst'd friend type or error_mark_node on
7854 tsubst_friend_class (tree friend_tmpl
, tree args
)
7860 context
= CP_DECL_CONTEXT (friend_tmpl
);
7862 if (context
!= global_namespace
)
7864 if (TREE_CODE (context
) == NAMESPACE_DECL
)
7865 push_nested_namespace (context
);
7867 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
7870 /* Look for a class template declaration. We look for hidden names
7871 because two friend declarations of the same template are the
7872 same. For example, in:
7875 template <typename> friend class F;
7877 template <typename> struct B {
7878 template <typename> friend class F;
7881 both F templates are the same. */
7882 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
7883 /*block_p=*/true, 0,
7884 LOOKUP_COMPLAIN
| LOOKUP_HIDDEN
);
7886 /* But, if we don't find one, it might be because we're in a
7887 situation like this:
7895 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
7896 for `S<int>', not the TEMPLATE_DECL. */
7897 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
7899 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
7900 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
7903 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
7905 /* The friend template has already been declared. Just
7906 check to see that the declarations match, and install any new
7907 default parameters. We must tsubst the default parameters,
7908 of course. We only need the innermost template parameters
7909 because that is all that redeclare_class_template will look
7911 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
7912 > TMPL_ARGS_DEPTH (args
))
7915 location_t saved_input_location
;
7916 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
7917 args
, tf_warning_or_error
);
7919 saved_input_location
= input_location
;
7920 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
7921 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
7922 input_location
= saved_input_location
;
7926 friend_type
= TREE_TYPE (tmpl
);
7930 /* The friend template has not already been declared. In this
7931 case, the instantiation of the template class will cause the
7932 injection of this template into the global scope. */
7933 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
7934 if (tmpl
== error_mark_node
)
7935 return error_mark_node
;
7937 /* The new TMPL is not an instantiation of anything, so we
7938 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
7939 the new type because that is supposed to be the corresponding
7940 template decl, i.e., TMPL. */
7941 DECL_USE_TEMPLATE (tmpl
) = 0;
7942 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
7943 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
7944 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
7945 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
7947 /* Inject this template into the global scope. */
7948 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
7951 if (context
!= global_namespace
)
7953 if (TREE_CODE (context
) == NAMESPACE_DECL
)
7954 pop_nested_namespace (context
);
7956 pop_nested_class ();
7962 /* Returns zero if TYPE cannot be completed later due to circularity.
7963 Otherwise returns one. */
7966 can_complete_type_without_circularity (tree type
)
7968 if (type
== NULL_TREE
|| type
== error_mark_node
)
7970 else if (COMPLETE_TYPE_P (type
))
7972 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
7973 return can_complete_type_without_circularity (TREE_TYPE (type
));
7974 else if (CLASS_TYPE_P (type
)
7975 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
7981 /* Apply any attributes which had to be deferred until instantiation
7982 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
7983 ARGS, COMPLAIN, IN_DECL are as tsubst. */
7986 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
7987 tree args
, tsubst_flags_t complain
, tree in_decl
)
7989 tree last_dep
= NULL_TREE
;
7993 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
7994 if (ATTR_IS_DEPENDENT (t
))
7997 attributes
= copy_list (attributes
);
8001 if (DECL_P (*decl_p
))
8003 if (TREE_TYPE (*decl_p
) == error_mark_node
)
8005 p
= &DECL_ATTRIBUTES (*decl_p
);
8008 p
= &TYPE_ATTRIBUTES (*decl_p
);
8012 tree late_attrs
= NULL_TREE
;
8013 tree
*q
= &late_attrs
;
8015 for (*p
= attributes
; *p
; )
8018 if (ATTR_IS_DEPENDENT (t
))
8020 *p
= TREE_CHAIN (t
);
8021 TREE_CHAIN (t
) = NULL_TREE
;
8022 /* If the first attribute argument is an identifier, don't
8023 pass it through tsubst. Attributes like mode, format,
8024 cleanup and several target specific attributes expect it
8027 && TREE_CODE (TREE_VALUE (t
)) == TREE_LIST
8028 && TREE_VALUE (TREE_VALUE (t
))
8029 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t
)))
8030 == IDENTIFIER_NODE
))
8033 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
8035 /*integral_constant_expression_p=*/false);
8036 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
8038 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
8043 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
8044 /*integral_constant_expression_p=*/false);
8046 q
= &TREE_CHAIN (t
);
8049 p
= &TREE_CHAIN (t
);
8052 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
8056 /* Perform (or defer) access check for typedefs that were referenced
8057 from within the template TMPL code.
8058 This is a subroutine of instantiate_template and instantiate_class_template.
8059 TMPL is the template to consider and TARGS is the list of arguments of
8063 perform_typedefs_access_check (tree tmpl
, tree targs
)
8065 location_t saved_location
;
8067 qualified_typedef_usage_t
*iter
;
8070 || (!CLASS_TYPE_P (tmpl
)
8071 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
8074 saved_location
= input_location
;
8075 FOR_EACH_VEC_ELT (qualified_typedef_usage_t
,
8076 get_types_needing_access_check (tmpl
),
8079 tree type_decl
= iter
->typedef_decl
;
8080 tree type_scope
= iter
->context
;
8082 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
8085 if (uses_template_parms (type_decl
))
8086 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
8087 if (uses_template_parms (type_scope
))
8088 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
8090 /* Make access check error messages point to the location
8091 of the use of the typedef. */
8092 input_location
= iter
->locus
;
8093 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
8094 type_decl
, type_decl
);
8096 input_location
= saved_location
;
8100 instantiate_class_template (tree type
)
8102 tree templ
, args
, pattern
, t
, member
;
8106 unsigned int saved_maximum_field_alignment
;
8108 if (type
== error_mark_node
)
8109 return error_mark_node
;
8111 if (COMPLETE_OR_OPEN_TYPE_P (type
)
8112 || uses_template_parms (type
))
8115 /* Figure out which template is being instantiated. */
8116 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
8117 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
8119 /* Determine what specialization of the original template to
8121 t
= most_specialized_class (type
, templ
, tf_warning_or_error
);
8122 if (t
== error_mark_node
)
8124 TYPE_BEING_DEFINED (type
) = 1;
8125 return error_mark_node
;
8129 /* This TYPE is actually an instantiation of a partial
8130 specialization. We replace the innermost set of ARGS with
8131 the arguments appropriate for substitution. For example,
8134 template <class T> struct S {};
8135 template <class T> struct S<T*> {};
8137 and supposing that we are instantiating S<int*>, ARGS will
8138 presently be {int*} -- but we need {int}. */
8139 pattern
= TREE_TYPE (t
);
8140 args
= TREE_PURPOSE (t
);
8144 pattern
= TREE_TYPE (templ
);
8145 args
= CLASSTYPE_TI_ARGS (type
);
8148 /* If the template we're instantiating is incomplete, then clearly
8149 there's nothing we can do. */
8150 if (!COMPLETE_TYPE_P (pattern
))
8153 /* If we've recursively instantiated too many templates, stop. */
8154 if (! push_tinst_level (type
))
8157 /* Now we're really doing the instantiation. Mark the type as in
8158 the process of being defined. */
8159 TYPE_BEING_DEFINED (type
) = 1;
8161 /* We may be in the middle of deferred access check. Disable
8163 push_deferring_access_checks (dk_no_deferred
);
8165 push_to_top_level ();
8166 /* Use #pragma pack from the template context. */
8167 saved_maximum_field_alignment
= maximum_field_alignment
;
8168 maximum_field_alignment
= TYPE_PRECISION (pattern
);
8170 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
8172 /* Set the input location to the most specialized template definition.
8173 This is needed if tsubsting causes an error. */
8174 typedecl
= TYPE_MAIN_DECL (pattern
);
8175 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
8176 DECL_SOURCE_LOCATION (typedecl
);
8178 TYPE_HAS_USER_CONSTRUCTOR (type
) = TYPE_HAS_USER_CONSTRUCTOR (pattern
);
8179 TYPE_HAS_NEW_OPERATOR (type
) = TYPE_HAS_NEW_OPERATOR (pattern
);
8180 TYPE_HAS_ARRAY_NEW_OPERATOR (type
) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern
);
8181 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
8182 TYPE_HAS_COPY_ASSIGN (type
) = TYPE_HAS_COPY_ASSIGN (pattern
);
8183 TYPE_HAS_CONST_COPY_ASSIGN (type
) = TYPE_HAS_CONST_COPY_ASSIGN (pattern
);
8184 TYPE_HAS_COPY_CTOR (type
) = TYPE_HAS_COPY_CTOR (pattern
);
8185 TYPE_HAS_CONST_COPY_CTOR (type
) = TYPE_HAS_CONST_COPY_CTOR (pattern
);
8186 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
8187 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
8188 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
8189 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
8190 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
8191 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
8192 if (ANON_AGGR_TYPE_P (pattern
))
8193 SET_ANON_AGGR_TYPE_P (type
);
8194 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
8196 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
8197 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
8200 pbinfo
= TYPE_BINFO (pattern
);
8202 /* We should never instantiate a nested class before its enclosing
8203 class; we need to look up the nested class by name before we can
8204 instantiate it, and that lookup should instantiate the enclosing
8206 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
8207 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
8209 base_list
= NULL_TREE
;
8210 if (BINFO_N_BASE_BINFOS (pbinfo
))
8216 /* We must enter the scope containing the type, as that is where
8217 the accessibility of types named in dependent bases are
8219 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
8221 /* Substitute into each of the bases to determine the actual
8223 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
8226 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
8227 tree expanded_bases
= NULL_TREE
;
8230 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
8233 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
8234 args
, tf_error
, NULL_TREE
);
8235 if (expanded_bases
== error_mark_node
)
8238 len
= TREE_VEC_LENGTH (expanded_bases
);
8241 for (idx
= 0; idx
< len
; idx
++)
8244 /* Extract the already-expanded base class. */
8245 base
= TREE_VEC_ELT (expanded_bases
, idx
);
8247 /* Substitute to figure out the base class. */
8248 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
8251 if (base
== error_mark_node
)
8254 base_list
= tree_cons (access
, base
, base_list
);
8255 if (BINFO_VIRTUAL_P (pbase_binfo
))
8256 TREE_TYPE (base_list
) = integer_type_node
;
8260 /* The list is now in reverse order; correct that. */
8261 base_list
= nreverse (base_list
);
8264 pop_scope (pushed_scope
);
8266 /* Now call xref_basetypes to set up all the base-class
8268 xref_basetypes (type
, base_list
);
8270 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
8271 (int) ATTR_FLAG_TYPE_IN_PLACE
,
8272 args
, tf_error
, NULL_TREE
);
8273 fixup_attribute_variants (type
);
8275 /* Now that our base classes are set up, enter the scope of the
8276 class, so that name lookups into base classes, etc. will work
8277 correctly. This is precisely analogous to what we do in
8278 begin_class_definition when defining an ordinary non-template
8279 class, except we also need to push the enclosing classes. */
8280 push_nested_class (type
);
8282 /* Now members are processed in the order of declaration. */
8283 for (member
= CLASSTYPE_DECL_LIST (pattern
);
8284 member
; member
= TREE_CHAIN (member
))
8286 tree t
= TREE_VALUE (member
);
8288 if (TREE_PURPOSE (member
))
8292 /* Build new CLASSTYPE_NESTED_UTDS. */
8295 bool class_template_p
;
8297 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
8298 && TYPE_LANG_SPECIFIC (t
)
8299 && CLASSTYPE_IS_TEMPLATE (t
));
8300 /* If the member is a class template, then -- even after
8301 substitution -- there may be dependent types in the
8302 template argument list for the class. We increment
8303 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8304 that function will assume that no types are dependent
8305 when outside of a template. */
8306 if (class_template_p
)
8307 ++processing_template_decl
;
8308 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8309 if (class_template_p
)
8310 --processing_template_decl
;
8311 if (newtag
== error_mark_node
)
8314 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
8316 tree name
= TYPE_IDENTIFIER (t
);
8318 if (class_template_p
)
8319 /* Unfortunately, lookup_template_class sets
8320 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8321 instantiation (i.e., for the type of a member
8322 template class nested within a template class.)
8323 This behavior is required for
8324 maybe_process_partial_specialization to work
8325 correctly, but is not accurate in this case;
8326 the TAG is not an instantiation of anything.
8327 (The corresponding TEMPLATE_DECL is an
8328 instantiation, but the TYPE is not.) */
8329 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
8331 /* Now, we call pushtag to put this NEWTAG into the scope of
8332 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8333 pushtag calling push_template_decl. We don't have to do
8334 this for enums because it will already have been done in
8337 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
8338 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
8341 else if (TREE_CODE (t
) == FUNCTION_DECL
8342 || DECL_FUNCTION_TEMPLATE_P (t
))
8344 /* Build new TYPE_METHODS. */
8347 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8348 ++processing_template_decl
;
8349 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8350 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8351 --processing_template_decl
;
8352 set_current_access_from_decl (r
);
8353 finish_member_declaration (r
);
8357 /* Build new TYPE_FIELDS. */
8358 if (TREE_CODE (t
) == STATIC_ASSERT
)
8361 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
8362 tf_warning_or_error
, NULL_TREE
,
8363 /*integral_constant_expression_p=*/true);
8364 finish_static_assert (condition
,
8365 STATIC_ASSERT_MESSAGE (t
),
8366 STATIC_ASSERT_SOURCE_LOCATION (t
),
8369 else if (TREE_CODE (t
) != CONST_DECL
)
8373 /* The file and line for this declaration, to
8374 assist in error message reporting. Since we
8375 called push_tinst_level above, we don't need to
8377 input_location
= DECL_SOURCE_LOCATION (t
);
8379 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8380 ++processing_template_decl
;
8381 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
8382 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8383 --processing_template_decl
;
8384 if (TREE_CODE (r
) == VAR_DECL
)
8388 [t]he initialization (and any associated
8389 side-effects) of a static data member does
8390 not occur unless the static data member is
8391 itself used in a way that requires the
8392 definition of the static data member to
8395 Therefore, we do not substitute into the
8396 initialized for the static data member here. */
8397 finish_static_data_member_decl
8400 /*init_const_expr_p=*/false,
8401 /*asmspec_tree=*/NULL_TREE
,
8403 if (DECL_INITIALIZED_IN_CLASS_P (r
))
8404 check_static_variable_definition (r
, TREE_TYPE (r
));
8406 else if (TREE_CODE (r
) == FIELD_DECL
)
8408 /* Determine whether R has a valid type and can be
8409 completed later. If R is invalid, then it is
8410 replaced by error_mark_node so that it will not be
8411 added to TYPE_FIELDS. */
8412 tree rtype
= TREE_TYPE (r
);
8413 if (can_complete_type_without_circularity (rtype
))
8414 complete_type (rtype
);
8416 if (!COMPLETE_TYPE_P (rtype
))
8418 cxx_incomplete_type_error (r
, rtype
);
8419 r
= error_mark_node
;
8423 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8424 such a thing will already have been added to the field
8425 list by tsubst_enum in finish_member_declaration in the
8426 CLASSTYPE_NESTED_UTDS case above. */
8427 if (!(TREE_CODE (r
) == TYPE_DECL
8428 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
8429 && DECL_ARTIFICIAL (r
)))
8431 set_current_access_from_decl (r
);
8432 finish_member_declaration (r
);
8439 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
))
8441 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8443 tree friend_type
= t
;
8444 bool adjust_processing_template_decl
= false;
8446 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
8448 /* template <class T> friend class C; */
8449 friend_type
= tsubst_friend_class (friend_type
, args
);
8450 adjust_processing_template_decl
= true;
8452 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
8454 /* template <class T> friend class C::D; */
8455 friend_type
= tsubst (friend_type
, args
,
8456 tf_warning_or_error
, NULL_TREE
);
8457 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
8458 friend_type
= TREE_TYPE (friend_type
);
8459 adjust_processing_template_decl
= true;
8461 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
)
8463 /* This could be either
8467 when dependent_type_p is false or
8469 template <class U> friend class T::C;
8472 friend_type
= tsubst (friend_type
, args
,
8473 tf_warning_or_error
, NULL_TREE
);
8474 /* Bump processing_template_decl for correct
8475 dependent_type_p calculation. */
8476 ++processing_template_decl
;
8477 if (dependent_type_p (friend_type
))
8478 adjust_processing_template_decl
= true;
8479 --processing_template_decl
;
8481 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
8482 && hidden_name_p (TYPE_NAME (friend_type
)))
8486 where C hasn't been declared yet. Let's lookup name
8487 from namespace scope directly, bypassing any name that
8488 come from dependent base class. */
8489 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
8491 /* The call to xref_tag_from_type does injection for friend
8493 push_nested_namespace (ns
);
8495 xref_tag_from_type (friend_type
, NULL_TREE
,
8496 /*tag_scope=*/ts_current
);
8497 pop_nested_namespace (ns
);
8499 else if (uses_template_parms (friend_type
))
8500 /* friend class C<T>; */
8501 friend_type
= tsubst (friend_type
, args
,
8502 tf_warning_or_error
, NULL_TREE
);
8507 where C is already declared or
8509 friend class C<int>;
8511 We don't have to do anything in these cases. */
8513 if (adjust_processing_template_decl
)
8514 /* Trick make_friend_class into realizing that the friend
8515 we're adding is a template, not an ordinary class. It's
8516 important that we use make_friend_class since it will
8517 perform some error-checking and output cross-reference
8519 ++processing_template_decl
;
8521 if (friend_type
!= error_mark_node
)
8522 make_friend_class (type
, friend_type
, /*complain=*/false);
8524 if (adjust_processing_template_decl
)
8525 --processing_template_decl
;
8529 /* Build new DECL_FRIENDLIST. */
8532 /* The file and line for this declaration, to
8533 assist in error message reporting. Since we
8534 called push_tinst_level above, we don't need to
8536 input_location
= DECL_SOURCE_LOCATION (t
);
8538 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8540 ++processing_template_decl
;
8541 push_deferring_access_checks (dk_no_check
);
8544 r
= tsubst_friend_function (t
, args
);
8545 add_friend (type
, r
, /*complain=*/false);
8546 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8548 pop_deferring_access_checks ();
8549 --processing_template_decl
;
8555 /* Set the file and line number information to whatever is given for
8556 the class itself. This puts error messages involving generated
8557 implicit functions at a predictable point, and the same point
8558 that would be used for non-template classes. */
8559 input_location
= DECL_SOURCE_LOCATION (typedecl
);
8561 unreverse_member_declarations (type
);
8562 finish_struct_1 (type
);
8563 TYPE_BEING_DEFINED (type
) = 0;
8565 /* We don't instantiate default arguments for member functions. 14.7.1:
8567 The implicit instantiation of a class template specialization causes
8568 the implicit instantiation of the declarations, but not of the
8569 definitions or default arguments, of the class member functions,
8570 member classes, static data members and member templates.... */
8572 /* Some typedefs referenced from within the template code need to be access
8573 checked at template instantiation time, i.e now. These types were
8574 added to the template at parsing time. Let's get those and perform
8575 the access checks then. */
8576 perform_typedefs_access_check (pattern
, args
);
8577 perform_deferred_access_checks ();
8578 pop_nested_class ();
8579 maximum_field_alignment
= saved_maximum_field_alignment
;
8580 pop_from_top_level ();
8581 pop_deferring_access_checks ();
8584 /* The vtable for a template class can be emitted in any translation
8585 unit in which the class is instantiated. When there is no key
8586 method, however, finish_struct_1 will already have added TYPE to
8587 the keyed_classes list. */
8588 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
8589 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
8595 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
8601 else if (TYPE_P (t
))
8602 r
= tsubst (t
, args
, complain
, in_decl
);
8605 if (!(complain
& tf_warning
))
8606 ++c_inhibit_evaluation_warnings
;
8607 r
= tsubst_expr (t
, args
, complain
, in_decl
,
8608 /*integral_constant_expression_p=*/true);
8609 if (!(complain
& tf_warning
))
8610 --c_inhibit_evaluation_warnings
;
8615 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
8616 NONTYPE_ARGUMENT_PACK. */
8619 make_fnparm_pack (tree spec_parm
)
8621 /* Collect all of the extra "packed" parameters into an
8625 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
8626 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8627 int i
, len
= list_length (spec_parm
);
8629 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
8630 parmvec
= make_tree_vec (len
);
8631 parmtypevec
= make_tree_vec (len
);
8632 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
8634 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
8635 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
8638 /* Build the argument packs. */
8639 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
8640 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
8641 TREE_TYPE (argpack
) = argtypepack
;
8646 /* Substitute ARGS into T, which is an pack expansion
8647 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
8648 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
8649 (if only a partial substitution could be performed) or
8650 ERROR_MARK_NODE if there was an error. */
8652 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
8656 tree pack
, packs
= NULL_TREE
, unsubstituted_packs
= NULL_TREE
;
8660 htab_t saved_local_specializations
= NULL
;
8662 gcc_assert (PACK_EXPANSION_P (t
));
8663 pattern
= PACK_EXPANSION_PATTERN (t
);
8665 /* Determine the argument packs that will instantiate the parameter
8666 packs used in the expansion expression. While we're at it,
8667 compute the number of arguments to be expanded and make sure it
8669 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
8670 pack
= TREE_CHAIN (pack
))
8672 tree parm_pack
= TREE_VALUE (pack
);
8673 tree arg_pack
= NULL_TREE
;
8674 tree orig_arg
= NULL_TREE
;
8676 if (TREE_CODE (parm_pack
) == PARM_DECL
)
8678 if (!cp_unevaluated_operand
)
8679 arg_pack
= retrieve_local_specialization (parm_pack
);
8682 /* We can't rely on local_specializations for a parameter
8683 name used later in a function declaration (such as in a
8684 late-specified return type). Even if it exists, it might
8685 have the wrong value for a recursive call. Just make a
8686 dummy decl, since it's only used for its type. */
8687 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
8688 arg_pack
= make_fnparm_pack (arg_pack
);
8693 int level
, idx
, levels
;
8694 template_parm_level_and_index (parm_pack
, &level
, &idx
);
8696 levels
= TMPL_ARGS_DEPTH (args
);
8697 if (level
<= levels
)
8698 arg_pack
= TMPL_ARG (args
, level
, idx
);
8701 orig_arg
= arg_pack
;
8702 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
8703 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
8705 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
8706 /* This can only happen if we forget to expand an argument
8707 pack somewhere else. Just return an error, silently. */
8709 result
= make_tree_vec (1);
8710 TREE_VEC_ELT (result
, 0) = error_mark_node
;
8714 /* For clarity in the comments below let's use the
8715 representation 'argument_pack<elements>' to denote an
8716 argument pack and its elements.
8718 In the 'if' block below, we want to detect cases where
8719 ARG_PACK is argument_pack<PARM_PACK...>. I.e, we want to
8720 check if ARG_PACK is an argument pack which sole element is
8721 the expansion of PARM_PACK. That argument pack is typically
8722 created by template_parm_to_arg when passed a parameter
8725 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
)) == 1
8726 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0)))
8728 tree expansion
= TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0);
8729 tree pattern
= PACK_EXPANSION_PATTERN (expansion
);
8730 /* So we have an argument_pack<P...>. We want to test if P
8731 is actually PARM_PACK. We will not use cp_tree_equal to
8732 test P and PARM_PACK because during type fixup (by
8733 fixup_template_parm) P can be a pre-fixup version of a
8734 type and PARM_PACK be its post-fixup version.
8735 cp_tree_equal would consider them as different even
8736 though we would want to consider them compatible for our
8737 precise purpose here.
8739 Thus we are going to consider that P and PARM_PACK are
8740 compatible if they have the same DECL. */
8741 if ((/* If ARG_PACK is a type parameter pack named by the
8742 same DECL as parm_pack ... */
8744 && TYPE_P (parm_pack
)
8745 && TYPE_NAME (pattern
) == TYPE_NAME (parm_pack
))
8746 /* ... or if ARG_PACK is a non-type parameter
8747 named by the same DECL as parm_pack ... */
8748 || (TREE_CODE (pattern
) == TEMPLATE_PARM_INDEX
8749 && TREE_CODE (parm_pack
) == PARM_DECL
8750 && TEMPLATE_PARM_DECL (pattern
)
8751 == TEMPLATE_PARM_DECL (DECL_INITIAL (parm_pack
))))
8752 && template_parameter_pack_p (pattern
))
8753 /* ... then the argument pack that the parameter maps to
8754 is just an expansion of the parameter itself, such as
8755 one would find in the implicit typedef of a class
8756 inside the class itself. Consider this parameter
8757 "unsubstituted", so that we will maintain the outer
8759 arg_pack
= NULL_TREE
;
8765 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
8767 /* It's all-or-nothing with incomplete argument packs. */
8768 if (incomplete
&& !ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
8769 return error_mark_node
;
8771 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
8776 else if (len
!= my_len
)
8779 /* We got explicit args for some packs but not others;
8780 do nothing now and try again after deduction. */
8782 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
8783 error ("mismatched argument pack lengths while expanding "
8787 error ("mismatched argument pack lengths while expanding "
8790 return error_mark_node
;
8793 /* Keep track of the parameter packs and their corresponding
8795 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
8796 TREE_TYPE (packs
) = orig_arg
;
8799 /* We can't substitute for this parameter pack. */
8800 unsubstituted_packs
= tree_cons (TREE_PURPOSE (pack
),
8802 unsubstituted_packs
);
8805 /* We cannot expand this expansion expression, because we don't have
8806 all of the argument packs we need. Substitute into the pattern
8807 and return a PACK_EXPANSION_*. The caller will need to deal with
8809 if (unsubstituted_packs
)
8812 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
8813 new_pat
= tsubst_expr (pattern
, args
, complain
, in_decl
,
8814 /*integral_constant_expression_p=*/false);
8816 new_pat
= tsubst (pattern
, args
, complain
, in_decl
);
8817 return make_pack_expansion (new_pat
);
8820 /* We could not find any argument packs that work. */
8822 return error_mark_node
;
8824 if (cp_unevaluated_operand
)
8826 /* We're in a late-specified return type, so create our own local
8827 specializations table; the current table is either NULL or (in the
8828 case of recursive unification) might have bindings that we don't
8829 want to use or alter. */
8830 saved_local_specializations
= local_specializations
;
8831 local_specializations
= htab_create (37,
8832 hash_local_specialization
,
8833 eq_local_specializations
,
8837 /* For each argument in each argument pack, substitute into the
8839 result
= make_tree_vec (len
+ incomplete
);
8840 for (i
= 0; i
< len
+ incomplete
; ++i
)
8842 /* For parameter pack, change the substitution of the parameter
8843 pack to the ith argument in its argument pack, then expand
8845 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
8847 tree parm
= TREE_PURPOSE (pack
);
8849 if (TREE_CODE (parm
) == PARM_DECL
)
8851 /* Select the Ith argument from the pack. */
8852 tree arg
= make_node (ARGUMENT_PACK_SELECT
);
8853 ARGUMENT_PACK_SELECT_FROM_PACK (arg
) = TREE_VALUE (pack
);
8854 ARGUMENT_PACK_SELECT_INDEX (arg
) = i
;
8856 register_local_specialization (arg
, parm
);
8862 template_parm_level_and_index (parm
, &level
, &idx
);
8866 /* Select the Ith argument from the pack. */
8867 value
= make_node (ARGUMENT_PACK_SELECT
);
8868 ARGUMENT_PACK_SELECT_FROM_PACK (value
) = TREE_VALUE (pack
);
8869 ARGUMENT_PACK_SELECT_INDEX (value
) = i
;
8872 /* Update the corresponding argument. */
8873 TMPL_ARG (args
, level
, idx
) = value
;
8877 /* Substitute into the PATTERN with the altered arguments. */
8878 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
8879 TREE_VEC_ELT (result
, i
) =
8880 tsubst_expr (pattern
, args
, complain
, in_decl
,
8881 /*integral_constant_expression_p=*/false);
8883 TREE_VEC_ELT (result
, i
) = tsubst (pattern
, args
, complain
, in_decl
);
8886 /* When we have incomplete argument packs, the last "expanded"
8887 result is itself a pack expansion, which allows us
8888 to deduce more arguments. */
8889 TREE_VEC_ELT (result
, i
) =
8890 make_pack_expansion (TREE_VEC_ELT (result
, i
));
8892 if (TREE_VEC_ELT (result
, i
) == error_mark_node
)
8894 result
= error_mark_node
;
8899 /* Update ARGS to restore the substitution from parameter packs to
8900 their argument packs. */
8901 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
8903 tree parm
= TREE_PURPOSE (pack
);
8905 if (TREE_CODE (parm
) == PARM_DECL
)
8906 register_local_specialization (TREE_TYPE (pack
), parm
);
8910 template_parm_level_and_index (parm
, &level
, &idx
);
8912 /* Update the corresponding argument. */
8913 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
8914 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
8917 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
8921 if (saved_local_specializations
)
8923 htab_delete (local_specializations
);
8924 local_specializations
= saved_local_specializations
;
8930 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
8931 TMPL. We do this using DECL_PARM_INDEX, which should work even with
8932 parameter packs; all parms generated from a function parameter pack will
8933 have the same DECL_PARM_INDEX. */
8936 get_pattern_parm (tree parm
, tree tmpl
)
8938 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
8941 if (DECL_ARTIFICIAL (parm
))
8943 for (patparm
= DECL_ARGUMENTS (pattern
);
8944 patparm
; patparm
= DECL_CHAIN (patparm
))
8945 if (DECL_ARTIFICIAL (patparm
)
8946 && DECL_NAME (parm
) == DECL_NAME (patparm
))
8951 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
8952 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
8953 gcc_assert (DECL_PARM_INDEX (patparm
)
8954 == DECL_PARM_INDEX (parm
));
8960 /* Substitute ARGS into the vector or list of template arguments T. */
8963 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
8966 int len
= TREE_VEC_LENGTH (t
);
8967 int need_new
= 0, i
, expanded_len_adjust
= 0, out
;
8968 tree
*elts
= XALLOCAVEC (tree
, len
);
8970 for (i
= 0; i
< len
; i
++)
8972 tree orig_arg
= TREE_VEC_ELT (t
, i
);
8975 if (TREE_CODE (orig_arg
) == TREE_VEC
)
8976 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
8977 else if (PACK_EXPANSION_P (orig_arg
))
8979 /* Substitute into an expansion expression. */
8980 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
8982 if (TREE_CODE (new_arg
) == TREE_VEC
)
8983 /* Add to the expanded length adjustment the number of
8984 expanded arguments. We subtract one from this
8985 measurement, because the argument pack expression
8986 itself is already counted as 1 in
8987 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
8988 the argument pack is empty. */
8989 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
8991 else if (ARGUMENT_PACK_P (orig_arg
))
8993 /* Substitute into each of the arguments. */
8994 new_arg
= TYPE_P (orig_arg
)
8995 ? cxx_make_type (TREE_CODE (orig_arg
))
8996 : make_node (TREE_CODE (orig_arg
));
8998 SET_ARGUMENT_PACK_ARGS (
9000 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
9001 args
, complain
, in_decl
));
9003 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
9004 new_arg
= error_mark_node
;
9006 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
9007 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
9009 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
9011 if (TREE_TYPE (new_arg
) == error_mark_node
)
9012 new_arg
= error_mark_node
;
9016 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
9018 if (new_arg
== error_mark_node
)
9019 return error_mark_node
;
9022 if (new_arg
!= orig_arg
)
9029 /* Make space for the expanded arguments coming from template
9031 t
= make_tree_vec (len
+ expanded_len_adjust
);
9032 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9033 arguments for a member template.
9034 In that case each TREE_VEC in ORIG_T represents a level of template
9035 arguments, and ORIG_T won't carry any non defaulted argument count.
9036 It will rather be the nested TREE_VECs that will carry one.
9037 In other words, ORIG_T carries a non defaulted argument count only
9038 if it doesn't contain any nested TREE_VEC. */
9039 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
9041 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
9042 count
+= expanded_len_adjust
;
9043 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
9045 for (i
= 0, out
= 0; i
< len
; i
++)
9047 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
9048 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
9049 && TREE_CODE (elts
[i
]) == TREE_VEC
)
9053 /* Now expand the template argument pack "in place". */
9054 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
9055 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
9059 TREE_VEC_ELT (t
, out
) = elts
[i
];
9067 /* Return the result of substituting ARGS into the template parameters
9068 given by PARMS. If there are m levels of ARGS and m + n levels of
9069 PARMS, then the result will contain n levels of PARMS. For
9070 example, if PARMS is `template <class T> template <class U>
9071 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9072 result will be `template <int*, double, class V>'. */
9075 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
9080 /* When substituting into a template, we must set
9081 PROCESSING_TEMPLATE_DECL as the template parameters may be
9082 dependent if they are based on one-another, and the dependency
9083 predicates are short-circuit outside of templates. */
9084 ++processing_template_decl
;
9086 for (new_parms
= &r
;
9087 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
9088 new_parms
= &(TREE_CHAIN (*new_parms
)),
9089 parms
= TREE_CHAIN (parms
))
9092 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
9095 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
9099 if (parms
== error_mark_node
)
9102 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
9104 if (tuple
== error_mark_node
)
9107 TREE_VEC_ELT (new_vec
, i
) =
9108 tsubst_template_parm (tuple
, args
, complain
);
9112 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
9113 - TMPL_ARGS_DEPTH (args
)),
9114 new_vec
, NULL_TREE
);
9117 --processing_template_decl
;
9122 /* Return the result of substituting ARGS into one template parameter
9123 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9124 parameter and which TREE_PURPOSE is the default argument of the
9125 template parameter. */
9128 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
9130 tree default_value
, parm_decl
;
9132 if (args
== NULL_TREE
9134 || t
== error_mark_node
)
9137 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
9139 default_value
= TREE_PURPOSE (t
);
9140 parm_decl
= TREE_VALUE (t
);
9142 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
9143 if (TREE_CODE (parm_decl
) == PARM_DECL
9144 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
9145 parm_decl
= error_mark_node
;
9146 default_value
= tsubst_template_arg (default_value
, args
,
9147 complain
, NULL_TREE
);
9149 return build_tree_list (default_value
, parm_decl
);
9152 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9153 type T. If T is not an aggregate or enumeration type, it is
9154 handled as if by tsubst. IN_DECL is as for tsubst. If
9155 ENTERING_SCOPE is nonzero, T is the context for a template which
9156 we are presently tsubst'ing. Return the substituted value. */
9159 tsubst_aggr_type (tree t
,
9161 tsubst_flags_t complain
,
9168 switch (TREE_CODE (t
))
9171 if (TYPE_PTRMEMFUNC_P (t
))
9172 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
9174 /* Else fall through. */
9177 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
9182 int saved_unevaluated_operand
;
9183 int saved_inhibit_evaluation_warnings
;
9185 /* In "sizeof(X<I>)" we need to evaluate "I". */
9186 saved_unevaluated_operand
= cp_unevaluated_operand
;
9187 cp_unevaluated_operand
= 0;
9188 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9189 c_inhibit_evaluation_warnings
= 0;
9191 /* First, determine the context for the type we are looking
9193 context
= TYPE_CONTEXT (t
);
9196 context
= tsubst_aggr_type (context
, args
, complain
,
9197 in_decl
, /*entering_scope=*/1);
9198 /* If context is a nested class inside a class template,
9199 it may still need to be instantiated (c++/33959). */
9200 if (TYPE_P (context
))
9201 context
= complete_type (context
);
9204 /* Then, figure out what arguments are appropriate for the
9205 type we are trying to find. For example, given:
9207 template <class T> struct S;
9208 template <class T, class U> void f(T, U) { S<U> su; }
9210 and supposing that we are instantiating f<int, double>,
9211 then our ARGS will be {int, double}, but, when looking up
9212 S we only want {double}. */
9213 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
9215 if (argvec
== error_mark_node
)
9216 r
= error_mark_node
;
9219 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
9220 entering_scope
, complain
);
9221 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
9224 cp_unevaluated_operand
= saved_unevaluated_operand
;
9225 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
9230 /* This is not a template type, so there's nothing to do. */
9234 return tsubst (t
, args
, complain
, in_decl
);
9238 /* Substitute into the default argument ARG (a default argument for
9239 FN), which has the indicated TYPE. */
9242 tsubst_default_argument (tree fn
, tree type
, tree arg
)
9244 tree saved_class_ptr
= NULL_TREE
;
9245 tree saved_class_ref
= NULL_TREE
;
9247 /* This can happen in invalid code. */
9248 if (TREE_CODE (arg
) == DEFAULT_ARG
)
9251 /* This default argument came from a template. Instantiate the
9252 default argument here, not in tsubst. In the case of
9261 we must be careful to do name lookup in the scope of S<T>,
9262 rather than in the current class. */
9263 push_access_scope (fn
);
9264 /* The "this" pointer is not valid in a default argument. */
9267 saved_class_ptr
= current_class_ptr
;
9268 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
9269 saved_class_ref
= current_class_ref
;
9270 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
9273 push_deferring_access_checks(dk_no_deferred
);
9274 /* The default argument expression may cause implicitly defined
9275 member functions to be synthesized, which will result in garbage
9276 collection. We must treat this situation as if we were within
9277 the body of function so as to avoid collecting live data on the
9280 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
9281 tf_warning_or_error
, NULL_TREE
,
9282 /*integral_constant_expression_p=*/false);
9284 pop_deferring_access_checks();
9286 /* Restore the "this" pointer. */
9289 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
9290 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
9293 /* Make sure the default argument is reasonable. */
9294 arg
= check_default_argument (type
, arg
);
9296 pop_access_scope (fn
);
9301 /* Substitute into all the default arguments for FN. */
9304 tsubst_default_arguments (tree fn
)
9309 tmpl_args
= DECL_TI_ARGS (fn
);
9311 /* If this function is not yet instantiated, we certainly don't need
9312 its default arguments. */
9313 if (uses_template_parms (tmpl_args
))
9315 /* Don't do this again for clones. */
9316 if (DECL_CLONED_FUNCTION_P (fn
))
9319 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
9321 arg
= TREE_CHAIN (arg
))
9322 if (TREE_PURPOSE (arg
))
9323 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
9325 TREE_PURPOSE (arg
));
9328 /* Substitute the ARGS into the T, which is a _DECL. Return the
9329 result of the substitution. Issue error and warning messages under
9330 control of COMPLAIN. */
9333 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
9335 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
9336 location_t saved_loc
;
9341 /* Set the filename and linenumber to improve error-reporting. */
9342 saved_loc
= input_location
;
9343 input_location
= DECL_SOURCE_LOCATION (t
);
9345 switch (TREE_CODE (t
))
9349 /* We can get here when processing a member function template,
9350 member class template, or template template parameter. */
9351 tree decl
= DECL_TEMPLATE_RESULT (t
);
9356 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9358 /* Template template parameter is treated here. */
9359 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9360 if (new_type
== error_mark_node
)
9361 RETURN (error_mark_node
);
9364 DECL_CHAIN (r
) = NULL_TREE
;
9365 TREE_TYPE (r
) = new_type
;
9366 DECL_TEMPLATE_RESULT (r
)
9367 = build_decl (DECL_SOURCE_LOCATION (decl
),
9368 TYPE_DECL
, DECL_NAME (decl
), new_type
);
9369 DECL_TEMPLATE_PARMS (r
)
9370 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
9372 TYPE_NAME (new_type
) = r
;
9376 /* We might already have an instance of this template.
9377 The ARGS are for the surrounding class type, so the
9378 full args contain the tsubst'd args for the context,
9379 plus the innermost args from the template decl. */
9380 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
9381 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
9382 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
9383 /* Because this is a template, the arguments will still be
9384 dependent, even after substitution. If
9385 PROCESSING_TEMPLATE_DECL is not set, the dependency
9386 predicates will short-circuit. */
9387 ++processing_template_decl
;
9388 full_args
= tsubst_template_args (tmpl_args
, args
,
9390 --processing_template_decl
;
9391 if (full_args
== error_mark_node
)
9392 RETURN (error_mark_node
);
9394 /* If this is a default template template argument,
9395 tsubst might not have changed anything. */
9396 if (full_args
== tmpl_args
)
9399 hash
= hash_tmpl_and_args (t
, full_args
);
9400 spec
= retrieve_specialization (t
, full_args
, hash
);
9401 if (spec
!= NULL_TREE
)
9407 /* Make a new template decl. It will be similar to the
9408 original, but will record the current template arguments.
9409 We also create a new function declaration, which is just
9410 like the old one, but points to this new template, rather
9411 than the old one. */
9413 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
9414 DECL_CHAIN (r
) = NULL_TREE
;
9416 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
9418 if (TREE_CODE (decl
) == TYPE_DECL
)
9421 ++processing_template_decl
;
9422 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9423 --processing_template_decl
;
9424 if (new_type
== error_mark_node
)
9425 RETURN (error_mark_node
);
9427 TREE_TYPE (r
) = new_type
;
9428 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
9429 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
9430 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
9431 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
9436 ++processing_template_decl
;
9437 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
9438 --processing_template_decl
;
9439 if (new_decl
== error_mark_node
)
9440 RETURN (error_mark_node
);
9442 DECL_TEMPLATE_RESULT (r
) = new_decl
;
9443 DECL_TI_TEMPLATE (new_decl
) = r
;
9444 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
9445 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
9446 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
9449 SET_DECL_IMPLICIT_INSTANTIATION (r
);
9450 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
9451 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
9453 /* The template parameters for this new template are all the
9454 template parameters for the old template, except the
9455 outermost level of parameters. */
9456 DECL_TEMPLATE_PARMS (r
)
9457 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
9460 if (PRIMARY_TEMPLATE_P (t
))
9461 DECL_PRIMARY_TEMPLATE (r
) = r
;
9463 if (TREE_CODE (decl
) != TYPE_DECL
)
9464 /* Record this non-type partial instantiation. */
9465 register_specialization (r
, t
,
9466 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
9474 tree argvec
= NULL_TREE
;
9482 /* Nobody should be tsubst'ing into non-template functions. */
9483 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
9485 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
9490 /* If T is not dependent, just return it. We have to
9491 increment PROCESSING_TEMPLATE_DECL because
9492 value_dependent_expression_p assumes that nothing is
9493 dependent when PROCESSING_TEMPLATE_DECL is zero. */
9494 ++processing_template_decl
;
9495 dependent_p
= value_dependent_expression_p (t
);
9496 --processing_template_decl
;
9500 /* Calculate the most general template of which R is a
9501 specialization, and the complete set of arguments used to
9503 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
9504 argvec
= tsubst_template_args (DECL_TI_ARGS
9505 (DECL_TEMPLATE_RESULT
9506 (DECL_TI_TEMPLATE (t
))),
9507 args
, complain
, in_decl
);
9509 /* Check to see if we already have this specialization. */
9510 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
9511 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
9519 /* We can see more levels of arguments than parameters if
9520 there was a specialization of a member template, like
9523 template <class T> struct S { template <class U> void f(); }
9524 template <> template <class U> void S<int>::f(U);
9526 Here, we'll be substituting into the specialization,
9527 because that's where we can find the code we actually
9528 want to generate, but we'll have enough arguments for
9529 the most general template.
9531 We also deal with the peculiar case:
9533 template <class T> struct S {
9534 template <class U> friend void f();
9536 template <class U> void f() {}
9538 template void f<double>();
9540 Here, the ARGS for the instantiation of will be {int,
9541 double}. But, we only need as many ARGS as there are
9542 levels of template parameters in CODE_PATTERN. We are
9543 careful not to get fooled into reducing the ARGS in
9546 template <class T> struct S { template <class U> void f(U); }
9547 template <class T> template <> void S<T>::f(int) {}
9549 which we can spot because the pattern will be a
9550 specialization in this case. */
9551 args_depth
= TMPL_ARGS_DEPTH (args
);
9553 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
9554 if (args_depth
> parms_depth
9555 && !DECL_TEMPLATE_SPECIALIZATION (t
))
9556 args
= get_innermost_template_args (args
, parms_depth
);
9560 /* This special case arises when we have something like this:
9562 template <class T> struct S {
9563 friend void f<int>(int, double);
9566 Here, the DECL_TI_TEMPLATE for the friend declaration
9567 will be an IDENTIFIER_NODE. We are being called from
9568 tsubst_friend_function, and we want only to create a
9569 new decl (R) with appropriate types so that we can call
9570 determine_specialization. */
9571 gen_tmpl
= NULL_TREE
;
9574 if (DECL_CLASS_SCOPE_P (t
))
9576 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
9580 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
9581 complain
, t
, /*entering_scope=*/1);
9586 ctx
= DECL_CONTEXT (t
);
9588 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9589 if (type
== error_mark_node
)
9590 RETURN (error_mark_node
);
9592 /* We do NOT check for matching decls pushed separately at this
9593 point, as they may not represent instantiations of this
9594 template, and in any case are considered separate under the
9597 DECL_USE_TEMPLATE (r
) = 0;
9598 TREE_TYPE (r
) = type
;
9599 /* Clear out the mangled name and RTL for the instantiation. */
9600 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
9601 SET_DECL_RTL (r
, NULL
);
9602 /* Leave DECL_INITIAL set on deleted instantiations. */
9603 if (!DECL_DELETED_FN (r
))
9604 DECL_INITIAL (r
) = NULL_TREE
;
9605 DECL_CONTEXT (r
) = ctx
;
9607 if (member
&& DECL_CONV_FN_P (r
))
9608 /* Type-conversion operator. Reconstruct the name, in
9609 case it's the name of one of the template's parameters. */
9610 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
9612 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
9614 DECL_RESULT (r
) = NULL_TREE
;
9616 TREE_STATIC (r
) = 0;
9617 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
9618 DECL_EXTERNAL (r
) = 1;
9619 /* If this is an instantiation of a function with internal
9620 linkage, we already know what object file linkage will be
9621 assigned to the instantiation. */
9622 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
9623 DECL_DEFER_OUTPUT (r
) = 0;
9624 DECL_CHAIN (r
) = NULL_TREE
;
9625 DECL_PENDING_INLINE_INFO (r
) = 0;
9626 DECL_PENDING_INLINE_P (r
) = 0;
9627 DECL_SAVED_TREE (r
) = NULL_TREE
;
9628 DECL_STRUCT_FUNCTION (r
) = NULL
;
9630 /* We'll re-clone as appropriate in instantiate_template. */
9631 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
9633 /* If we aren't complaining now, return on error before we register
9634 the specialization so that we'll complain eventually. */
9635 if ((complain
& tf_error
) == 0
9636 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
9637 && !grok_op_properties (r
, /*complain=*/false))
9638 RETURN (error_mark_node
);
9640 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
9641 this in the special friend case mentioned above where
9642 GEN_TMPL is NULL. */
9645 DECL_TEMPLATE_INFO (r
)
9646 = build_template_info (gen_tmpl
, argvec
);
9647 SET_DECL_IMPLICIT_INSTANTIATION (r
);
9648 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
9650 /* We're not supposed to instantiate default arguments
9651 until they are called, for a template. But, for a
9654 template <class T> void f ()
9655 { extern void g(int i = T()); }
9657 we should do the substitution when the template is
9658 instantiated. We handle the member function case in
9659 instantiate_class_template since the default arguments
9660 might refer to other members of the class. */
9662 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9663 && !uses_template_parms (argvec
))
9664 tsubst_default_arguments (r
);
9667 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
9669 /* Copy the list of befriending classes. */
9670 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
9672 friends
= &TREE_CHAIN (*friends
))
9674 *friends
= copy_node (*friends
);
9675 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
9680 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
9682 maybe_retrofit_in_chrg (r
);
9683 if (DECL_CONSTRUCTOR_P (r
))
9684 grok_ctor_properties (ctx
, r
);
9685 /* If this is an instantiation of a member template, clone it.
9686 If it isn't, that'll be handled by
9687 clone_constructors_and_destructors. */
9688 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
9689 clone_function_decl (r
, /*update_method_vec_p=*/0);
9691 else if ((complain
& tf_error
) != 0
9692 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
9693 && !grok_op_properties (r
, /*complain=*/true))
9694 RETURN (error_mark_node
);
9696 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
9697 SET_DECL_FRIEND_CONTEXT (r
,
9698 tsubst (DECL_FRIEND_CONTEXT (t
),
9699 args
, complain
, in_decl
));
9701 /* Possibly limit visibility based on template args. */
9702 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
9703 if (DECL_VISIBILITY_SPECIFIED (t
))
9705 DECL_VISIBILITY_SPECIFIED (r
) = 0;
9707 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
9709 determine_visibility (r
);
9710 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
9711 && !processing_template_decl
)
9712 defaulted_late_check (r
);
9714 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
9715 args
, complain
, in_decl
);
9721 tree type
= NULL_TREE
;
9723 tree expanded_types
= NULL_TREE
;
9724 tree prev_r
= NULL_TREE
;
9725 tree first_r
= NULL_TREE
;
9727 if (FUNCTION_PARAMETER_PACK_P (t
))
9729 /* If there is a local specialization that isn't a
9730 parameter pack, it means that we're doing a "simple"
9731 substitution from inside tsubst_pack_expansion. Just
9732 return the local specialization (which will be a single
9734 tree spec
= retrieve_local_specialization (t
);
9736 && TREE_CODE (spec
) == PARM_DECL
9737 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
9740 /* Expand the TYPE_PACK_EXPANSION that provides the types for
9741 the parameters in this function parameter pack. */
9742 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
9744 if (TREE_CODE (expanded_types
) == TREE_VEC
)
9746 len
= TREE_VEC_LENGTH (expanded_types
);
9748 /* Zero-length parameter packs are boring. Just substitute
9751 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
9756 /* All we did was update the type. Make a note of that. */
9757 type
= expanded_types
;
9758 expanded_types
= NULL_TREE
;
9762 /* Loop through all of the parameter's we'll build. When T is
9763 a function parameter pack, LEN is the number of expanded
9764 types in EXPANDED_TYPES; otherwise, LEN is 1. */
9766 for (i
= 0; i
< len
; ++i
)
9770 if (DECL_TEMPLATE_PARM_P (t
))
9771 SET_DECL_TEMPLATE_PARM_P (r
);
9773 /* An argument of a function parameter pack is not a parameter
9775 FUNCTION_PARAMETER_PACK_P (r
) = false;
9778 /* We're on the Ith parameter of the function parameter
9781 /* Get the Ith type. */
9782 type
= TREE_VEC_ELT (expanded_types
, i
);
9785 /* Rename the parameter to include the index. */
9787 make_ith_pack_parameter_name (DECL_NAME (r
), i
);
9790 /* We're dealing with a normal parameter. */
9791 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9793 type
= type_decays_to (type
);
9794 TREE_TYPE (r
) = type
;
9795 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
9797 if (DECL_INITIAL (r
))
9799 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
9800 DECL_INITIAL (r
) = TREE_TYPE (r
);
9802 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
9806 DECL_CONTEXT (r
) = NULL_TREE
;
9808 if (!DECL_TEMPLATE_PARM_P (r
))
9809 DECL_ARG_TYPE (r
) = type_passed_as (type
);
9811 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
9812 args
, complain
, in_decl
);
9814 /* Keep track of the first new parameter we
9815 generate. That's what will be returned to the
9820 /* Build a proper chain of parameters when substituting
9821 into a function parameter pack. */
9823 DECL_CHAIN (prev_r
) = r
;
9827 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
9828 complain
, DECL_CHAIN (t
));
9830 /* FIRST_R contains the start of the chain we've built. */
9840 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9841 if (type
== error_mark_node
)
9842 RETURN (error_mark_node
);
9843 TREE_TYPE (r
) = type
;
9844 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
9846 /* DECL_INITIAL gives the number of bits in a bit-field. */
9848 = tsubst_expr (DECL_INITIAL (t
), args
,
9850 /*integral_constant_expression_p=*/true);
9851 /* We don't have to set DECL_CONTEXT here; it is set by
9852 finish_member_declaration. */
9853 DECL_CHAIN (r
) = NULL_TREE
;
9854 if (VOID_TYPE_P (type
))
9855 error ("instantiation of %q+D as type %qT", r
, type
);
9857 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
9858 args
, complain
, in_decl
);
9863 /* We reach here only for member using decls. */
9864 if (DECL_DEPENDENT_P (t
))
9866 r
= do_class_using_decl
9867 (tsubst_copy (USING_DECL_SCOPE (t
), args
, complain
, in_decl
),
9868 tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
));
9870 r
= error_mark_node
;
9873 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
9874 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
9880 DECL_CHAIN (r
) = NULL_TREE
;
9887 tree argvec
= NULL_TREE
;
9888 tree gen_tmpl
= NULL_TREE
;
9890 tree tmpl
= NULL_TREE
;
9892 tree type
= NULL_TREE
;
9895 if (TREE_CODE (t
) == TYPE_DECL
9896 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
9898 /* If this is the canonical decl, we don't have to
9899 mess with instantiations, and often we can't (for
9900 typename, template type parms and such). Note that
9901 TYPE_NAME is not correct for the above test if
9902 we've copied the type for a typedef. */
9903 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9904 if (type
== error_mark_node
)
9905 RETURN (error_mark_node
);
9906 r
= TYPE_NAME (type
);
9910 /* Check to see if we already have the specialization we
9913 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
9915 /* T is a static data member or namespace-scope entity.
9916 We have to substitute into namespace-scope variables
9917 (even though such entities are never templates) because
9920 template <class T> void f() { extern T t; }
9922 where the entity referenced is not known until
9923 instantiation time. */
9925 ctx
= DECL_CONTEXT (t
);
9926 if (DECL_CLASS_SCOPE_P (t
))
9928 ctx
= tsubst_aggr_type (ctx
, args
,
9930 in_decl
, /*entering_scope=*/1);
9931 /* If CTX is unchanged, then T is in fact the
9932 specialization we want. That situation occurs when
9933 referencing a static data member within in its own
9934 class. We can use pointer equality, rather than
9935 same_type_p, because DECL_CONTEXT is always
9937 if (ctx
== DECL_CONTEXT (t
))
9943 tmpl
= DECL_TI_TEMPLATE (t
);
9944 gen_tmpl
= most_general_template (tmpl
);
9945 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
9946 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
9947 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
9952 /* A local variable. */
9954 /* Subsequent calls to pushdecl will fill this in. */
9956 spec
= retrieve_local_specialization (t
);
9958 /* If we already have the specialization we need, there is
9959 nothing more to do. */
9966 /* Create a new node for the specialization we need. */
9968 if (type
== NULL_TREE
)
9970 if (is_typedef_decl (t
))
9971 type
= DECL_ORIGINAL_TYPE (t
);
9973 type
= TREE_TYPE (t
);
9974 if (TREE_CODE (t
) == VAR_DECL
&& VAR_HAD_UNKNOWN_BOUND (t
))
9975 type
= strip_array_domain (type
);
9976 type
= tsubst (type
, args
, complain
, in_decl
);
9978 if (TREE_CODE (r
) == VAR_DECL
)
9980 /* Even if the original location is out of scope, the
9981 newly substituted one is not. */
9982 DECL_DEAD_FOR_LOCAL (r
) = 0;
9983 DECL_INITIALIZED_P (r
) = 0;
9984 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
9985 if (type
== error_mark_node
)
9986 RETURN (error_mark_node
);
9987 if (TREE_CODE (type
) == FUNCTION_TYPE
)
9989 /* It may seem that this case cannot occur, since:
9994 declares a function, not a variable. However:
9997 template <typename T> void g() { T t; }
9998 template void g<f>();
10000 is an attempt to declare a variable with function
10002 error ("variable %qD has function type",
10003 /* R is not yet sufficiently initialized, so we
10004 just use its name. */
10006 RETURN (error_mark_node
);
10008 type
= complete_type (type
);
10009 /* Wait until cp_finish_decl to set this again, to handle
10010 circular dependency (template/instantiate6.C). */
10011 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
10012 type
= check_var_type (DECL_NAME (r
), type
);
10014 if (DECL_HAS_VALUE_EXPR_P (t
))
10016 tree ve
= DECL_VALUE_EXPR (t
);
10017 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
10018 /*constant_expression_p=*/false);
10019 SET_DECL_VALUE_EXPR (r
, ve
);
10022 else if (DECL_SELF_REFERENCE_P (t
))
10023 SET_DECL_SELF_REFERENCE_P (r
);
10024 TREE_TYPE (r
) = type
;
10025 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10026 DECL_CONTEXT (r
) = ctx
;
10027 /* Clear out the mangled name and RTL for the instantiation. */
10028 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10029 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10030 SET_DECL_RTL (r
, NULL
);
10031 /* The initializer must not be expanded until it is required;
10032 see [temp.inst]. */
10033 DECL_INITIAL (r
) = NULL_TREE
;
10034 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10035 SET_DECL_RTL (r
, NULL
);
10036 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
10037 if (TREE_CODE (r
) == VAR_DECL
)
10039 /* Possibly limit visibility based on template args. */
10040 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10041 if (DECL_VISIBILITY_SPECIFIED (t
))
10043 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10044 DECL_ATTRIBUTES (r
)
10045 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10047 determine_visibility (r
);
10052 /* A static data member declaration is always marked
10053 external when it is declared in-class, even if an
10054 initializer is present. We mimic the non-template
10055 processing here. */
10056 DECL_EXTERNAL (r
) = 1;
10058 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10059 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
10060 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10062 else if (cp_unevaluated_operand
)
10064 /* We're substituting this var in a decltype outside of its
10065 scope, such as for a lambda return type. Don't add it to
10066 local_specializations, do perform auto deduction. */
10067 tree auto_node
= type_uses_auto (type
);
10069 = tsubst_expr (DECL_INITIAL (t
), args
, complain
, in_decl
,
10070 /*constant_expression_p=*/false);
10072 if (auto_node
&& init
)
10074 init
= resolve_nondeduced_context (init
);
10075 if (describable_type (init
))
10077 type
= do_auto_deduction (type
, init
, auto_node
);
10078 TREE_TYPE (r
) = type
;
10083 register_local_specialization (r
, t
);
10085 DECL_CHAIN (r
) = NULL_TREE
;
10087 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
10089 args
, complain
, in_decl
);
10091 /* Preserve a typedef that names a type. */
10092 if (is_typedef_decl (r
))
10094 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
10095 set_underlying_type (r
);
10098 layout_decl (r
, 0);
10103 gcc_unreachable ();
10108 /* Restore the file and line information. */
10109 input_location
= saved_loc
;
10114 /* Substitute into the ARG_TYPES of a function type. */
10117 tsubst_arg_types (tree arg_types
,
10119 tsubst_flags_t complain
,
10122 tree remaining_arg_types
;
10123 tree type
= NULL_TREE
;
10125 tree expanded_args
= NULL_TREE
;
10128 if (!arg_types
|| arg_types
== void_list_node
)
10131 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
10132 args
, complain
, in_decl
);
10133 if (remaining_arg_types
== error_mark_node
)
10134 return error_mark_node
;
10136 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
10138 /* For a pack expansion, perform substitution on the
10139 entire expression. Later on, we'll handle the arguments
10141 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
10142 args
, complain
, in_decl
);
10144 if (TREE_CODE (expanded_args
) == TREE_VEC
)
10145 /* So that we'll spin through the parameters, one by one. */
10146 i
= TREE_VEC_LENGTH (expanded_args
);
10149 /* We only partially substituted into the parameter
10150 pack. Our type is TYPE_PACK_EXPANSION. */
10151 type
= expanded_args
;
10152 expanded_args
= NULL_TREE
;
10160 type
= TREE_VEC_ELT (expanded_args
, i
);
10162 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
10164 if (type
== error_mark_node
)
10165 return error_mark_node
;
10166 if (VOID_TYPE_P (type
))
10168 if (complain
& tf_error
)
10170 error ("invalid parameter type %qT", type
);
10172 error ("in declaration %q+D", in_decl
);
10174 return error_mark_node
;
10177 /* Do array-to-pointer, function-to-pointer conversion, and ignore
10178 top-level qualifiers as required. */
10179 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
10181 /* We do not substitute into default arguments here. The standard
10182 mandates that they be instantiated only when needed, which is
10183 done in build_over_call. */
10184 default_arg
= TREE_PURPOSE (arg_types
);
10186 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
10188 /* We've instantiated a template before its default arguments
10189 have been parsed. This can happen for a nested template
10190 class, and is not an error unless we require the default
10191 argument in a call of this function. */
10192 remaining_arg_types
=
10193 tree_cons (default_arg
, type
, remaining_arg_types
);
10194 VEC_safe_push (tree
, gc
, DEFARG_INSTANTIATIONS (default_arg
),
10195 remaining_arg_types
);
10198 remaining_arg_types
=
10199 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
10202 return remaining_arg_types
;
10205 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
10206 *not* handle the exception-specification for FNTYPE, because the
10207 initial substitution of explicitly provided template parameters
10208 during argument deduction forbids substitution into the
10209 exception-specification:
10213 All references in the function type of the function template to the
10214 corresponding template parameters are replaced by the specified tem-
10215 plate argument values. If a substitution in a template parameter or
10216 in the function type of the function template results in an invalid
10217 type, type deduction fails. [Note: The equivalent substitution in
10218 exception specifications is done only when the function is instanti-
10219 ated, at which point a program is ill-formed if the substitution
10220 results in an invalid type.] */
10223 tsubst_function_type (tree t
,
10225 tsubst_flags_t complain
,
10232 /* The TYPE_CONTEXT is not used for function/method types. */
10233 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
10235 /* Substitute the return type. */
10236 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10237 if (return_type
== error_mark_node
)
10238 return error_mark_node
;
10239 /* The standard does not presently indicate that creation of a
10240 function type with an invalid return type is a deduction failure.
10241 However, that is clearly analogous to creating an array of "void"
10242 or a reference to a reference. This is core issue #486. */
10243 if (TREE_CODE (return_type
) == ARRAY_TYPE
10244 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
10246 if (complain
& tf_error
)
10248 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
10249 error ("function returning an array");
10251 error ("function returning a function");
10253 return error_mark_node
;
10256 /* Substitute the argument types. */
10257 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
,
10258 complain
, in_decl
);
10259 if (arg_types
== error_mark_node
)
10260 return error_mark_node
;
10262 /* Construct a new type node and return it. */
10263 if (TREE_CODE (t
) == FUNCTION_TYPE
)
10265 fntype
= build_function_type (return_type
, arg_types
);
10266 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
10270 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
10271 if (! MAYBE_CLASS_TYPE_P (r
))
10275 Type deduction may fail for any of the following
10278 -- Attempting to create "pointer to member of T" when T
10279 is not a class type. */
10280 if (complain
& tf_error
)
10281 error ("creating pointer to member function of non-class type %qT",
10283 return error_mark_node
;
10286 fntype
= build_method_type_directly (r
, return_type
,
10287 TREE_CHAIN (arg_types
));
10289 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
10294 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
10295 ARGS into that specification, and return the substituted
10296 specification. If there is no specification, return NULL_TREE. */
10299 tsubst_exception_specification (tree fntype
,
10301 tsubst_flags_t complain
,
10307 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
10308 new_specs
= NULL_TREE
;
10309 if (specs
&& TREE_PURPOSE (specs
))
10311 /* A noexcept-specifier. */
10312 new_specs
= tsubst_copy_and_build
10313 (TREE_PURPOSE (specs
), args
, complain
, in_decl
, /*function_p=*/false,
10314 /*integral_constant_expression_p=*/true);
10315 new_specs
= build_noexcept_spec (new_specs
, complain
);
10319 if (! TREE_VALUE (specs
))
10326 tree expanded_specs
= NULL_TREE
;
10328 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
10330 /* Expand the pack expansion type. */
10331 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
10335 if (expanded_specs
== error_mark_node
)
10336 return error_mark_node
;
10337 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
10338 len
= TREE_VEC_LENGTH (expanded_specs
);
10341 /* We're substituting into a member template, so
10342 we got a TYPE_PACK_EXPANSION back. Add that
10343 expansion and move on. */
10344 gcc_assert (TREE_CODE (expanded_specs
)
10345 == TYPE_PACK_EXPANSION
);
10346 new_specs
= add_exception_specifier (new_specs
,
10349 specs
= TREE_CHAIN (specs
);
10354 for (i
= 0; i
< len
; ++i
)
10356 if (expanded_specs
)
10357 spec
= TREE_VEC_ELT (expanded_specs
, i
);
10359 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
10360 if (spec
== error_mark_node
)
10362 new_specs
= add_exception_specifier (new_specs
, spec
,
10366 specs
= TREE_CHAIN (specs
);
10372 /* Take the tree structure T and replace template parameters used
10373 therein with the argument vector ARGS. IN_DECL is an associated
10374 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
10375 Issue error and warning messages under control of COMPLAIN. Note
10376 that we must be relatively non-tolerant of extensions here, in
10377 order to preserve conformance; if we allow substitutions that
10378 should not be allowed, we may allow argument deductions that should
10379 not succeed, and therefore report ambiguous overload situations
10380 where there are none. In theory, we could allow the substitution,
10381 but indicate that it should have failed, and allow our caller to
10382 make sure that the right thing happens, but we don't try to do this
10385 This function is used for dealing with types, decls and the like;
10386 for expressions, use tsubst_expr or tsubst_copy. */
10389 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10391 enum tree_code code
;
10394 if (t
== NULL_TREE
|| t
== error_mark_node
10395 || t
== integer_type_node
10396 || t
== void_type_node
10397 || t
== char_type_node
10398 || t
== unknown_type_node
10399 || TREE_CODE (t
) == NAMESPACE_DECL
10400 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
10404 return tsubst_decl (t
, args
, complain
);
10406 if (args
== NULL_TREE
)
10409 code
= TREE_CODE (t
);
10411 if (code
== IDENTIFIER_NODE
)
10412 type
= IDENTIFIER_TYPE_VALUE (t
);
10414 type
= TREE_TYPE (t
);
10416 gcc_assert (type
!= unknown_type_node
);
10418 /* Reuse typedefs. We need to do this to handle dependent attributes,
10419 such as attribute aligned. */
10421 && typedef_variant_p (t
))
10423 tree decl
= TYPE_NAME (t
);
10425 if (DECL_CLASS_SCOPE_P (decl
)
10426 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
10427 && uses_template_parms (DECL_CONTEXT (decl
)))
10429 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
10430 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
10431 r
= retrieve_specialization (tmpl
, gen_args
, 0);
10433 else if (DECL_FUNCTION_SCOPE_P (decl
)
10434 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
10435 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
10436 r
= retrieve_local_specialization (decl
);
10438 /* The typedef is from a non-template context. */
10444 r
= cp_build_qualified_type_real
10445 (r
, cp_type_quals (t
) | cp_type_quals (r
),
10446 complain
| tf_ignore_bad_quals
);
10449 /* Else we must be instantiating the typedef, so fall through. */
10453 && code
!= TYPENAME_TYPE
10454 && code
!= TEMPLATE_TYPE_PARM
10455 && code
!= IDENTIFIER_NODE
10456 && code
!= FUNCTION_TYPE
10457 && code
!= METHOD_TYPE
)
10458 type
= tsubst (type
, args
, complain
, in_decl
);
10459 if (type
== error_mark_node
)
10460 return error_mark_node
;
10466 case ENUMERAL_TYPE
:
10467 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
10468 /*entering_scope=*/0);
10471 case IDENTIFIER_NODE
:
10482 if (t
== integer_type_node
)
10485 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
10486 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
10490 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
10492 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
10493 /*integral_constant_expression_p=*/false);
10495 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
10497 if (TREE_CODE (max
) == NOP_EXPR
10498 && TREE_SIDE_EFFECTS (omax
)
10499 && !TREE_TYPE (max
))
10500 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
10502 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
10503 with TREE_SIDE_EFFECTS that indicates this is not an integral
10504 constant expression. */
10505 if (processing_template_decl
10506 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
10508 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
10509 TREE_SIDE_EFFECTS (max
) = 1;
10512 return compute_array_index_type (NULL_TREE
, max
, complain
);
10515 case TEMPLATE_TYPE_PARM
:
10516 case TEMPLATE_TEMPLATE_PARM
:
10517 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10518 case TEMPLATE_PARM_INDEX
:
10523 tree arg
= NULL_TREE
;
10527 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
10528 template_parm_level_and_index (t
, &level
, &idx
);
10530 levels
= TMPL_ARGS_DEPTH (args
);
10531 if (level
<= levels
)
10533 arg
= TMPL_ARG (args
, level
, idx
);
10535 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
10536 /* See through ARGUMENT_PACK_SELECT arguments. */
10537 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
10540 if (arg
== error_mark_node
)
10541 return error_mark_node
;
10542 else if (arg
!= NULL_TREE
)
10544 if (ARGUMENT_PACK_P (arg
))
10545 /* If ARG is an argument pack, we don't actually want to
10546 perform a substitution here, because substitutions
10547 for argument packs are only done
10548 element-by-element. We can get to this point when
10549 substituting the type of a non-type template
10550 parameter pack, when that type actually contains
10551 template parameter packs from an outer template, e.g.,
10553 template<typename... Types> struct A {
10554 template<Types... Values> struct B { };
10558 if (code
== TEMPLATE_TYPE_PARM
)
10561 gcc_assert (TYPE_P (arg
));
10563 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
10565 return cp_build_qualified_type_real
10566 (arg
, quals
, complain
| tf_ignore_bad_quals
);
10568 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
10570 /* We are processing a type constructed from a
10571 template template parameter. */
10572 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
10573 args
, complain
, in_decl
);
10574 if (argvec
== error_mark_node
)
10575 return error_mark_node
;
10577 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
10578 are resolving nested-types in the signature of a
10579 member function templates. Otherwise ARG is a
10580 TEMPLATE_DECL and is the real template to be
10582 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
10583 arg
= TYPE_NAME (arg
);
10585 r
= lookup_template_class (arg
,
10587 DECL_CONTEXT (arg
),
10588 /*entering_scope=*/0,
10590 return cp_build_qualified_type_real
10591 (r
, cp_type_quals (t
), complain
);
10594 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
10595 return unshare_expr (arg
);
10599 /* This can happen during the attempted tsubst'ing in
10600 unify. This means that we don't yet have any information
10601 about the template parameter in question. */
10604 /* If we get here, we must have been looking at a parm for a
10605 more deeply nested template. Make a new version of this
10606 template parameter, but with a lower level. */
10609 case TEMPLATE_TYPE_PARM
:
10610 case TEMPLATE_TEMPLATE_PARM
:
10611 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10612 if (cp_type_quals (t
))
10614 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
10615 r
= cp_build_qualified_type_real
10616 (r
, cp_type_quals (t
),
10617 complain
| (code
== TEMPLATE_TYPE_PARM
10618 ? tf_ignore_bad_quals
: 0));
10623 TEMPLATE_TYPE_PARM_INDEX (r
)
10624 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
10625 r
, levels
, args
, complain
);
10626 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
10627 TYPE_MAIN_VARIANT (r
) = r
;
10628 TYPE_POINTER_TO (r
) = NULL_TREE
;
10629 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
10631 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
10632 /* We have reduced the level of the template
10633 template parameter, but not the levels of its
10634 template parameters, so canonical_type_parameter
10635 will not be able to find the canonical template
10636 template parameter for this level. Thus, we
10637 require structural equality checking to compare
10638 TEMPLATE_TEMPLATE_PARMs. */
10639 SET_TYPE_STRUCTURAL_EQUALITY (r
);
10640 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
10641 SET_TYPE_STRUCTURAL_EQUALITY (r
);
10643 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
10645 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
10647 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
10648 complain
, in_decl
);
10649 if (argvec
== error_mark_node
)
10650 return error_mark_node
;
10652 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
10653 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
10658 case TEMPLATE_PARM_INDEX
:
10659 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
10663 gcc_unreachable ();
10671 tree purpose
, value
, chain
;
10673 if (t
== void_list_node
)
10676 purpose
= TREE_PURPOSE (t
);
10679 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
10680 if (purpose
== error_mark_node
)
10681 return error_mark_node
;
10683 value
= TREE_VALUE (t
);
10686 value
= tsubst (value
, args
, complain
, in_decl
);
10687 if (value
== error_mark_node
)
10688 return error_mark_node
;
10690 chain
= TREE_CHAIN (t
);
10691 if (chain
&& chain
!= void_type_node
)
10693 chain
= tsubst (chain
, args
, complain
, in_decl
);
10694 if (chain
== error_mark_node
)
10695 return error_mark_node
;
10697 if (purpose
== TREE_PURPOSE (t
)
10698 && value
== TREE_VALUE (t
)
10699 && chain
== TREE_CHAIN (t
))
10701 return hash_tree_cons (purpose
, value
, chain
);
10705 /* We should never be tsubsting a binfo. */
10706 gcc_unreachable ();
10709 /* A vector of template arguments. */
10710 gcc_assert (!type
);
10711 return tsubst_template_args (t
, args
, complain
, in_decl
);
10714 case REFERENCE_TYPE
:
10716 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
10721 Type deduction may fail for any of the following
10724 -- Attempting to create a pointer to reference type.
10725 -- Attempting to create a reference to a reference type or
10726 a reference to void.
10728 Core issue 106 says that creating a reference to a reference
10729 during instantiation is no longer a cause for failure. We
10730 only enforce this check in strict C++98 mode. */
10731 if ((TREE_CODE (type
) == REFERENCE_TYPE
10732 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
10733 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
10735 static location_t last_loc
;
10737 /* We keep track of the last time we issued this error
10738 message to avoid spewing a ton of messages during a
10739 single bad template instantiation. */
10740 if (complain
& tf_error
10741 && last_loc
!= input_location
)
10743 if (TREE_CODE (type
) == VOID_TYPE
)
10744 error ("forming reference to void");
10745 else if (code
== POINTER_TYPE
)
10746 error ("forming pointer to reference type %qT", type
);
10748 error ("forming reference to reference type %qT", type
);
10749 last_loc
= input_location
;
10752 return error_mark_node
;
10754 else if (code
== POINTER_TYPE
)
10756 r
= build_pointer_type (type
);
10757 if (TREE_CODE (type
) == METHOD_TYPE
)
10758 r
= build_ptrmemfunc_type (r
);
10760 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
10761 /* In C++0x, during template argument substitution, when there is an
10762 attempt to create a reference to a reference type, reference
10763 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
10765 "If a template-argument for a template-parameter T names a type
10766 that is a reference to a type A, an attempt to create the type
10767 'lvalue reference to cv T' creates the type 'lvalue reference to
10768 A,' while an attempt to create the type type rvalue reference to
10769 cv T' creates the type T"
10771 r
= cp_build_reference_type
10773 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
10775 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
10776 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
10778 if (r
!= error_mark_node
)
10779 /* Will this ever be needed for TYPE_..._TO values? */
10786 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
10787 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
10791 Type deduction may fail for any of the following
10794 -- Attempting to create "pointer to member of T" when T
10795 is not a class type. */
10796 if (complain
& tf_error
)
10797 error ("creating pointer to member of non-class type %qT", r
);
10798 return error_mark_node
;
10800 if (TREE_CODE (type
) == REFERENCE_TYPE
)
10802 if (complain
& tf_error
)
10803 error ("creating pointer to member reference type %qT", type
);
10804 return error_mark_node
;
10806 if (TREE_CODE (type
) == VOID_TYPE
)
10808 if (complain
& tf_error
)
10809 error ("creating pointer to member of type void");
10810 return error_mark_node
;
10812 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
10813 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10815 /* The type of the implicit object parameter gets its
10816 cv-qualifiers from the FUNCTION_TYPE. */
10818 tree method_type
= build_memfn_type (type
, r
, type_memfn_quals (type
));
10819 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
10820 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
10824 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
10828 case FUNCTION_TYPE
:
10833 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
10834 if (fntype
== error_mark_node
)
10835 return error_mark_node
;
10837 /* Substitute the exception specification. */
10838 specs
= tsubst_exception_specification (t
, args
, complain
,
10840 if (specs
== error_mark_node
)
10841 return error_mark_node
;
10843 fntype
= build_exception_variant (fntype
, specs
);
10848 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
10849 if (domain
== error_mark_node
)
10850 return error_mark_node
;
10852 /* As an optimization, we avoid regenerating the array type if
10853 it will obviously be the same as T. */
10854 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
10857 /* These checks should match the ones in grokdeclarator.
10861 The deduction may fail for any of the following reasons:
10863 -- Attempting to create an array with an element type that
10864 is void, a function type, or a reference type, or [DR337]
10865 an abstract class type. */
10866 if (TREE_CODE (type
) == VOID_TYPE
10867 || TREE_CODE (type
) == FUNCTION_TYPE
10868 || TREE_CODE (type
) == REFERENCE_TYPE
)
10870 if (complain
& tf_error
)
10871 error ("creating array of %qT", type
);
10872 return error_mark_node
;
10874 if (CLASS_TYPE_P (type
) && CLASSTYPE_PURE_VIRTUALS (type
))
10876 if (complain
& tf_error
)
10877 error ("creating array of %qT, which is an abstract class type",
10879 return error_mark_node
;
10882 r
= build_cplus_array_type (type
, domain
);
10884 if (TYPE_USER_ALIGN (t
))
10886 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
10887 TYPE_USER_ALIGN (r
) = 1;
10893 case TYPENAME_TYPE
:
10895 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
10896 in_decl
, /*entering_scope=*/1);
10897 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
10898 complain
, in_decl
);
10900 if (ctx
== error_mark_node
|| f
== error_mark_node
)
10901 return error_mark_node
;
10903 if (!MAYBE_CLASS_TYPE_P (ctx
))
10905 if (complain
& tf_error
)
10906 error ("%qT is not a class, struct, or union type", ctx
);
10907 return error_mark_node
;
10909 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
10911 /* Normally, make_typename_type does not require that the CTX
10912 have complete type in order to allow things like:
10914 template <class T> struct S { typename S<T>::X Y; };
10916 But, such constructs have already been resolved by this
10917 point, so here CTX really should have complete type, unless
10918 it's a partial instantiation. */
10919 ctx
= complete_type (ctx
);
10920 if (!COMPLETE_TYPE_P (ctx
))
10922 if (complain
& tf_error
)
10923 cxx_incomplete_type_error (NULL_TREE
, ctx
);
10924 return error_mark_node
;
10928 f
= make_typename_type (ctx
, f
, typename_type
,
10929 (complain
& tf_error
) | tf_keep_type_decl
);
10930 if (f
== error_mark_node
)
10932 if (TREE_CODE (f
) == TYPE_DECL
)
10934 complain
|= tf_ignore_bad_quals
;
10938 if (TREE_CODE (f
) != TYPENAME_TYPE
)
10940 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
10941 error ("%qT resolves to %qT, which is not an enumeration type",
10943 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
10944 error ("%qT resolves to %qT, which is is not a class type",
10948 return cp_build_qualified_type_real
10949 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
10952 case UNBOUND_CLASS_TEMPLATE
:
10954 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
10955 in_decl
, /*entering_scope=*/1);
10956 tree name
= TYPE_IDENTIFIER (t
);
10957 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
10959 if (ctx
== error_mark_node
|| name
== error_mark_node
)
10960 return error_mark_node
;
10963 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
10964 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
10971 ++cp_unevaluated_operand
;
10972 ++c_inhibit_evaluation_warnings
;
10974 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
10976 /*integral_constant_expression_p=*/false);
10978 --cp_unevaluated_operand
;
10979 --c_inhibit_evaluation_warnings
;
10981 type
= finish_typeof (type
);
10982 return cp_build_qualified_type_real (type
,
10984 | cp_type_quals (type
),
10988 case DECLTYPE_TYPE
:
10992 ++cp_unevaluated_operand
;
10993 ++c_inhibit_evaluation_warnings
;
10995 type
= tsubst_expr (DECLTYPE_TYPE_EXPR (t
), args
,
10997 /*integral_constant_expression_p=*/false);
10999 --cp_unevaluated_operand
;
11000 --c_inhibit_evaluation_warnings
;
11002 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
11003 type
= lambda_capture_field_type (type
);
11004 else if (DECLTYPE_FOR_LAMBDA_RETURN (t
))
11005 type
= lambda_return_type (type
);
11007 type
= finish_decltype_type
11008 (type
, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
));
11009 return cp_build_qualified_type_real (type
,
11011 | cp_type_quals (type
),
11015 case TYPE_ARGUMENT_PACK
:
11016 case NONTYPE_ARGUMENT_PACK
:
11018 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
11020 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
11024 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
11026 /* For template nontype argument packs, also substitute into
11028 if (code
== NONTYPE_ARGUMENT_PACK
)
11029 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11047 /* We should use one of the expression tsubsts for these codes. */
11048 gcc_unreachable ();
11051 sorry ("use of %qs in template", tree_code_name
[(int) code
]);
11052 return error_mark_node
;
11056 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
11057 type of the expression on the left-hand side of the "." or "->"
11061 tsubst_baselink (tree baselink
, tree object_type
,
11062 tree args
, tsubst_flags_t complain
, tree in_decl
)
11065 tree qualifying_scope
;
11068 tree template_args
= 0;
11069 bool template_id_p
= false;
11071 /* A baselink indicates a function from a base class. Both the
11072 BASELINK_ACCESS_BINFO and the base class referenced may
11073 indicate bases of the template class, rather than the
11074 instantiated class. In addition, lookups that were not
11075 ambiguous before may be ambiguous now. Therefore, we perform
11076 the lookup again. */
11077 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
11078 qualifying_scope
= tsubst (qualifying_scope
, args
,
11079 complain
, in_decl
);
11080 fns
= BASELINK_FUNCTIONS (baselink
);
11081 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
11082 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
11084 template_id_p
= true;
11085 template_args
= TREE_OPERAND (fns
, 1);
11086 fns
= TREE_OPERAND (fns
, 0);
11088 template_args
= tsubst_template_args (template_args
, args
,
11089 complain
, in_decl
);
11091 name
= DECL_NAME (get_first_fn (fns
));
11092 if (IDENTIFIER_TYPENAME_P (name
))
11093 name
= mangle_conv_op_name_for_type (optype
);
11094 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
11096 return error_mark_node
;
11098 /* If lookup found a single function, mark it as used at this
11099 point. (If it lookup found multiple functions the one selected
11100 later by overload resolution will be marked as used at that
11102 if (BASELINK_P (baselink
))
11103 fns
= BASELINK_FUNCTIONS (baselink
);
11104 if (!template_id_p
&& !really_overloaded_fn (fns
))
11105 mark_used (OVL_CURRENT (fns
));
11107 /* Add back the template arguments, if present. */
11108 if (BASELINK_P (baselink
) && template_id_p
)
11109 BASELINK_FUNCTIONS (baselink
)
11110 = build_nt (TEMPLATE_ID_EXPR
,
11111 BASELINK_FUNCTIONS (baselink
),
11113 /* Update the conversion operator type. */
11114 BASELINK_OPTYPE (baselink
) = optype
;
11117 object_type
= current_class_type
;
11118 return adjust_result_of_qualified_name_lookup (baselink
,
11123 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
11124 true if the qualified-id will be a postfix-expression in-and-of
11125 itself; false if more of the postfix-expression follows the
11126 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
11130 tsubst_qualified_id (tree qualified_id
, tree args
,
11131 tsubst_flags_t complain
, tree in_decl
,
11132 bool done
, bool address_p
)
11138 tree template_args
;
11140 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
11142 /* Figure out what name to look up. */
11143 name
= TREE_OPERAND (qualified_id
, 1);
11144 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
11146 is_template
= true;
11147 template_args
= TREE_OPERAND (name
, 1);
11149 template_args
= tsubst_template_args (template_args
, args
,
11150 complain
, in_decl
);
11151 name
= TREE_OPERAND (name
, 0);
11155 is_template
= false;
11156 template_args
= NULL_TREE
;
11159 /* Substitute into the qualifying scope. When there are no ARGS, we
11160 are just trying to simplify a non-dependent expression. In that
11161 case the qualifying scope may be dependent, and, in any case,
11162 substituting will not help. */
11163 scope
= TREE_OPERAND (qualified_id
, 0);
11166 scope
= tsubst (scope
, args
, complain
, in_decl
);
11167 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
11172 if (dependent_scope_p (scope
))
11173 return build_qualified_name (NULL_TREE
, scope
, expr
,
11174 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
11176 if (!BASELINK_P (name
) && !DECL_P (expr
))
11178 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
11180 /* A BIT_NOT_EXPR is used to represent a destructor. */
11181 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
11183 error ("qualifying type %qT does not match destructor name ~%qT",
11184 scope
, TREE_OPERAND (expr
, 0));
11185 expr
= error_mark_node
;
11188 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
11189 /*is_type_p=*/0, false);
11192 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
11193 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
11194 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
11196 if (complain
& tf_error
)
11198 error ("dependent-name %qE is parsed as a non-type, but "
11199 "instantiation yields a type", qualified_id
);
11200 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
11202 return error_mark_node
;
11208 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
11210 /* Remember that there was a reference to this entity. */
11214 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
11216 if (complain
& tf_error
)
11217 qualified_name_lookup_error (scope
,
11218 TREE_OPERAND (qualified_id
, 1),
11219 expr
, input_location
);
11220 return error_mark_node
;
11224 expr
= lookup_template_function (expr
, template_args
);
11226 if (expr
== error_mark_node
&& complain
& tf_error
)
11227 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
11228 expr
, input_location
);
11229 else if (TYPE_P (scope
))
11231 expr
= (adjust_result_of_qualified_name_lookup
11232 (expr
, scope
, current_class_type
));
11233 expr
= (finish_qualified_id_expr
11234 (scope
, expr
, done
, address_p
,
11235 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
11236 /*template_arg_p=*/false));
11239 /* Expressions do not generally have reference type. */
11240 if (TREE_CODE (expr
) != SCOPE_REF
11241 /* However, if we're about to form a pointer-to-member, we just
11242 want the referenced member referenced. */
11243 && TREE_CODE (expr
) != OFFSET_REF
)
11244 expr
= convert_from_reference (expr
);
11249 /* Like tsubst, but deals with expressions. This function just replaces
11250 template parms; to finish processing the resultant expression, use
11254 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11256 enum tree_code code
;
11259 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
11262 code
= TREE_CODE (t
);
11267 r
= retrieve_local_specialization (t
);
11272 /* This can happen for a parameter name used later in a function
11273 declaration (such as in a late-specified return type). Just
11274 make a dummy decl, since it's only used for its type. */
11275 gcc_assert (cp_unevaluated_operand
!= 0);
11276 /* We copy T because want to tsubst the PARM_DECL only,
11277 not the following PARM_DECLs that are chained to T. */
11279 r
= tsubst_decl (c
, args
, complain
);
11280 /* Give it the template pattern as its context; its true context
11281 hasn't been instantiated yet and this is good enough for
11283 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
11286 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
11287 r
= ARGUMENT_PACK_SELECT_ARG (r
);
11296 if (DECL_TEMPLATE_PARM_P (t
))
11297 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
11298 /* There is no need to substitute into namespace-scope
11300 if (DECL_NAMESPACE_SCOPE_P (t
))
11302 /* If ARGS is NULL, then T is known to be non-dependent. */
11303 if (args
== NULL_TREE
)
11304 return integral_constant_value (t
);
11306 /* Unfortunately, we cannot just call lookup_name here.
11309 template <int I> int f() {
11311 struct S { void g() { E e = a; } };
11314 When we instantiate f<7>::S::g(), say, lookup_name is not
11315 clever enough to find f<7>::a. */
11317 = tsubst_aggr_type (TREE_TYPE (t
), args
, complain
, in_decl
,
11318 /*entering_scope=*/0);
11320 for (v
= TYPE_VALUES (enum_type
);
11322 v
= TREE_CHAIN (v
))
11323 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
11324 return TREE_VALUE (v
);
11326 /* We didn't find the name. That should never happen; if
11327 name-lookup found it during preliminary parsing, we
11328 should find it again here during instantiation. */
11329 gcc_unreachable ();
11334 if (DECL_CONTEXT (t
))
11338 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
11339 /*entering_scope=*/1);
11340 if (ctx
!= DECL_CONTEXT (t
))
11342 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
11345 if (complain
& tf_error
)
11346 error ("using invalid field %qD", t
);
11347 return error_mark_node
;
11356 case FUNCTION_DECL
:
11357 if ((DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
11358 || local_variable_p (t
))
11359 t
= tsubst (t
, args
, complain
, in_decl
);
11364 /* An OVERLOAD will always be a non-dependent overload set; an
11365 overload set from function scope will just be represented with an
11366 IDENTIFIER_NODE, and from class scope with a BASELINK. */
11367 gcc_assert (!uses_template_parms (t
));
11371 return tsubst_baselink (t
, current_class_type
, args
, complain
, in_decl
);
11373 case TEMPLATE_DECL
:
11374 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11375 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
11376 args
, complain
, in_decl
);
11377 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
11378 return tsubst (t
, args
, complain
, in_decl
);
11379 else if (DECL_CLASS_SCOPE_P (t
)
11380 && uses_template_parms (DECL_CONTEXT (t
)))
11382 /* Template template argument like the following example need
11385 template <template <class> class TT> struct C {};
11386 template <class T> struct D {
11387 template <class U> struct E {};
11392 We are processing the template argument `E' in #1 for
11393 the template instantiation #2. Originally, `E' is a
11394 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
11395 have to substitute this with one having context `D<int>'. */
11397 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
11398 return lookup_field (context
, DECL_NAME(t
), 0, false);
11401 /* Ordinary template template argument. */
11405 case REINTERPRET_CAST_EXPR
:
11406 case CONST_CAST_EXPR
:
11407 case STATIC_CAST_EXPR
:
11408 case DYNAMIC_CAST_EXPR
:
11411 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
11412 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
11415 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
11421 ++cp_unevaluated_operand
;
11422 ++c_inhibit_evaluation_warnings
;
11423 /* We only want to compute the number of arguments. */
11424 expanded
= tsubst_pack_expansion (TREE_OPERAND (t
, 0), args
,
11425 complain
, in_decl
);
11426 --cp_unevaluated_operand
;
11427 --c_inhibit_evaluation_warnings
;
11429 if (TREE_CODE (expanded
) == TREE_VEC
)
11430 len
= TREE_VEC_LENGTH (expanded
);
11432 if (expanded
== error_mark_node
)
11433 return error_mark_node
;
11434 else if (PACK_EXPANSION_P (expanded
)
11435 || (TREE_CODE (expanded
) == TREE_VEC
11437 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
11439 if (TREE_CODE (expanded
) == TREE_VEC
)
11440 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
11442 if (TYPE_P (expanded
))
11443 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
11444 complain
& tf_error
);
11446 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
11447 complain
& tf_error
);
11450 return build_int_cst (size_type_node
, len
);
11456 case TRUTH_NOT_EXPR
:
11459 case UNARY_PLUS_EXPR
: /* Unary + */
11461 case AT_ENCODE_EXPR
:
11465 case REALPART_EXPR
:
11466 case IMAGPART_EXPR
:
11468 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
11469 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
11471 case COMPONENT_REF
:
11476 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
11477 name
= TREE_OPERAND (t
, 1);
11478 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
11480 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
11481 complain
, in_decl
);
11482 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
11484 else if (TREE_CODE (name
) == SCOPE_REF
11485 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
11487 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
11488 complain
, in_decl
);
11489 name
= TREE_OPERAND (name
, 1);
11490 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
11491 complain
, in_decl
);
11492 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
11493 name
= build_qualified_name (/*type=*/NULL_TREE
,
11495 /*template_p=*/false);
11497 else if (TREE_CODE (name
) == BASELINK
)
11498 name
= tsubst_baselink (name
,
11499 non_reference (TREE_TYPE (object
)),
11503 name
= tsubst_copy (name
, args
, complain
, in_decl
);
11504 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
11510 case TRUNC_DIV_EXPR
:
11511 case CEIL_DIV_EXPR
:
11512 case FLOOR_DIV_EXPR
:
11513 case ROUND_DIV_EXPR
:
11514 case EXACT_DIV_EXPR
:
11518 case TRUNC_MOD_EXPR
:
11519 case FLOOR_MOD_EXPR
:
11520 case TRUTH_ANDIF_EXPR
:
11521 case TRUTH_ORIF_EXPR
:
11522 case TRUTH_AND_EXPR
:
11523 case TRUTH_OR_EXPR
:
11536 case COMPOUND_EXPR
:
11539 case PREDECREMENT_EXPR
:
11540 case PREINCREMENT_EXPR
:
11541 case POSTDECREMENT_EXPR
:
11542 case POSTINCREMENT_EXPR
:
11544 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11545 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
11548 return build_qualified_name (/*type=*/NULL_TREE
,
11549 tsubst_copy (TREE_OPERAND (t
, 0),
11550 args
, complain
, in_decl
),
11551 tsubst_copy (TREE_OPERAND (t
, 1),
11552 args
, complain
, in_decl
),
11553 QUALIFIED_NAME_IS_TEMPLATE (t
));
11558 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11559 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
11560 NULL_TREE
, NULL_TREE
);
11564 int n
= VL_EXP_OPERAND_LENGTH (t
);
11565 tree result
= build_vl_exp (CALL_EXPR
, n
);
11567 for (i
= 0; i
< n
; i
++)
11568 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
11569 complain
, in_decl
);
11575 case PSEUDO_DTOR_EXPR
:
11578 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11579 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
11580 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
11581 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
11588 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11589 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
11590 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
11591 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
11598 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11599 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
11600 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
11601 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
11605 case TEMPLATE_ID_EXPR
:
11607 /* Substituted template arguments */
11608 tree fn
= TREE_OPERAND (t
, 0);
11609 tree targs
= TREE_OPERAND (t
, 1);
11611 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
11613 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
11615 return lookup_template_function (fn
, targs
);
11620 tree purpose
, value
, chain
;
11622 if (t
== void_list_node
)
11625 purpose
= TREE_PURPOSE (t
);
11627 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
11628 value
= TREE_VALUE (t
);
11630 value
= tsubst_copy (value
, args
, complain
, in_decl
);
11631 chain
= TREE_CHAIN (t
);
11632 if (chain
&& chain
!= void_type_node
)
11633 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
11634 if (purpose
== TREE_PURPOSE (t
)
11635 && value
== TREE_VALUE (t
)
11636 && chain
== TREE_CHAIN (t
))
11638 return tree_cons (purpose
, value
, chain
);
11643 case ENUMERAL_TYPE
:
11645 case TEMPLATE_TYPE_PARM
:
11646 case TEMPLATE_TEMPLATE_PARM
:
11647 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11648 case TEMPLATE_PARM_INDEX
:
11650 case REFERENCE_TYPE
:
11652 case FUNCTION_TYPE
:
11655 case TYPENAME_TYPE
:
11656 case UNBOUND_CLASS_TEMPLATE
:
11658 case DECLTYPE_TYPE
:
11660 return tsubst (t
, args
, complain
, in_decl
);
11662 case IDENTIFIER_NODE
:
11663 if (IDENTIFIER_TYPENAME_P (t
))
11665 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11666 return mangle_conv_op_name_for_type (new_type
);
11672 /* This is handled by tsubst_copy_and_build. */
11673 gcc_unreachable ();
11676 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
11678 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
11680 case CLEANUP_POINT_EXPR
:
11681 /* We shouldn't have built any of these during initial template
11682 generation. Instead, they should be built during instantiation
11683 in response to the saved STMT_IS_FULL_EXPR_P setting. */
11684 gcc_unreachable ();
11688 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
11689 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11690 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
11691 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
11692 mark_used (TREE_OPERAND (r
, 1));
11695 case EXPR_PACK_EXPANSION
:
11696 error ("invalid use of pack expansion expression");
11697 return error_mark_node
;
11699 case NONTYPE_ARGUMENT_PACK
:
11700 error ("use %<...%> to expand argument pack");
11701 return error_mark_node
;
11708 /* Instantiate any typedefs in the type. */
11709 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11710 r
= fold_convert (type
, t
);
11711 gcc_assert (TREE_CODE (r
) == code
);
11716 /* These can sometimes show up in a partial instantiation, but never
11717 involve template parms. */
11718 gcc_assert (!uses_template_parms (t
));
11722 gcc_unreachable ();
11726 /* Like tsubst_copy, but specifically for OpenMP clauses. */
11729 tsubst_omp_clauses (tree clauses
, tree args
, tsubst_flags_t complain
,
11732 tree new_clauses
= NULL
, nc
, oc
;
11734 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
11736 nc
= copy_node (oc
);
11737 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
11740 switch (OMP_CLAUSE_CODE (nc
))
11742 case OMP_CLAUSE_LASTPRIVATE
:
11743 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
11745 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
11746 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
11747 in_decl
, /*integral_constant_expression_p=*/false);
11748 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
11749 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
11752 case OMP_CLAUSE_PRIVATE
:
11753 case OMP_CLAUSE_SHARED
:
11754 case OMP_CLAUSE_FIRSTPRIVATE
:
11755 case OMP_CLAUSE_REDUCTION
:
11756 case OMP_CLAUSE_COPYIN
:
11757 case OMP_CLAUSE_COPYPRIVATE
:
11758 case OMP_CLAUSE_IF
:
11759 case OMP_CLAUSE_NUM_THREADS
:
11760 case OMP_CLAUSE_SCHEDULE
:
11761 case OMP_CLAUSE_COLLAPSE
:
11762 OMP_CLAUSE_OPERAND (nc
, 0)
11763 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
11764 in_decl
, /*integral_constant_expression_p=*/false);
11766 case OMP_CLAUSE_NOWAIT
:
11767 case OMP_CLAUSE_ORDERED
:
11768 case OMP_CLAUSE_DEFAULT
:
11769 case OMP_CLAUSE_UNTIED
:
11772 gcc_unreachable ();
11776 return finish_omp_clauses (nreverse (new_clauses
));
11779 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
11782 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
11785 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
11787 tree purpose
, value
, chain
;
11792 if (TREE_CODE (t
) != TREE_LIST
)
11793 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
11794 /*function_p=*/false,
11795 /*integral_constant_expression_p=*/false);
11797 if (t
== void_list_node
)
11800 purpose
= TREE_PURPOSE (t
);
11802 purpose
= RECUR (purpose
);
11803 value
= TREE_VALUE (t
);
11804 if (value
&& TREE_CODE (value
) != LABEL_DECL
)
11805 value
= RECUR (value
);
11806 chain
= TREE_CHAIN (t
);
11807 if (chain
&& chain
!= void_type_node
)
11808 chain
= RECUR (chain
);
11809 return tree_cons (purpose
, value
, chain
);
11813 /* Substitute one OMP_FOR iterator. */
11816 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
11817 tree condv
, tree incrv
, tree
*clauses
,
11818 tree args
, tsubst_flags_t complain
, tree in_decl
,
11819 bool integral_constant_expression_p
)
11821 #define RECUR(NODE) \
11822 tsubst_expr ((NODE), args, complain, in_decl, \
11823 integral_constant_expression_p)
11824 tree decl
, init
, cond
, incr
, auto_node
;
11826 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
11827 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
11828 decl
= RECUR (TREE_OPERAND (init
, 0));
11829 init
= TREE_OPERAND (init
, 1);
11830 auto_node
= type_uses_auto (TREE_TYPE (decl
));
11831 if (auto_node
&& init
)
11833 tree init_expr
= init
;
11834 if (TREE_CODE (init_expr
) == DECL_EXPR
)
11835 init_expr
= DECL_INITIAL (DECL_EXPR_DECL (init_expr
));
11836 init_expr
= RECUR (init_expr
);
11838 = do_auto_deduction (TREE_TYPE (decl
), init_expr
, auto_node
);
11840 gcc_assert (!type_dependent_expression_p (decl
));
11842 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
11844 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
11845 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
11846 if (TREE_CODE (incr
) == MODIFY_EXPR
)
11847 incr
= build_x_modify_expr (RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
11848 RECUR (TREE_OPERAND (incr
, 1)),
11851 incr
= RECUR (incr
);
11852 TREE_VEC_ELT (declv
, i
) = decl
;
11853 TREE_VEC_ELT (initv
, i
) = init
;
11854 TREE_VEC_ELT (condv
, i
) = cond
;
11855 TREE_VEC_ELT (incrv
, i
) = incr
;
11859 if (init
&& TREE_CODE (init
) != DECL_EXPR
)
11862 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
11864 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
11865 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
11866 && OMP_CLAUSE_DECL (c
) == decl
)
11868 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
11869 && OMP_CLAUSE_DECL (c
) == decl
)
11870 error ("iteration variable %qD should not be firstprivate", decl
);
11871 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
11872 && OMP_CLAUSE_DECL (c
) == decl
)
11873 error ("iteration variable %qD should not be reduction", decl
);
11877 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
11878 OMP_CLAUSE_DECL (c
) = decl
;
11879 c
= finish_omp_clauses (c
);
11882 OMP_CLAUSE_CHAIN (c
) = *clauses
;
11887 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
11888 if (COMPARISON_CLASS_P (cond
))
11889 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
11890 RECUR (TREE_OPERAND (cond
, 0)),
11891 RECUR (TREE_OPERAND (cond
, 1)));
11893 cond
= RECUR (cond
);
11894 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
11895 switch (TREE_CODE (incr
))
11897 case PREINCREMENT_EXPR
:
11898 case PREDECREMENT_EXPR
:
11899 case POSTINCREMENT_EXPR
:
11900 case POSTDECREMENT_EXPR
:
11901 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
11902 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
11905 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
11906 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
11908 tree rhs
= TREE_OPERAND (incr
, 1);
11909 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
11910 RECUR (TREE_OPERAND (incr
, 0)),
11911 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
11912 RECUR (TREE_OPERAND (rhs
, 0)),
11913 RECUR (TREE_OPERAND (rhs
, 1))));
11916 incr
= RECUR (incr
);
11919 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
11920 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
11922 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
11923 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
11924 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
11925 TREE_TYPE (decl
), lhs
,
11926 RECUR (TREE_OPERAND (incr
, 2))));
11928 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
11929 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
11930 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
11932 tree rhs
= TREE_OPERAND (incr
, 2);
11933 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
11934 RECUR (TREE_OPERAND (incr
, 0)),
11935 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
11936 RECUR (TREE_OPERAND (rhs
, 0)),
11937 RECUR (TREE_OPERAND (rhs
, 1))));
11940 incr
= RECUR (incr
);
11943 incr
= RECUR (incr
);
11947 TREE_VEC_ELT (declv
, i
) = decl
;
11948 TREE_VEC_ELT (initv
, i
) = init
;
11949 TREE_VEC_ELT (condv
, i
) = cond
;
11950 TREE_VEC_ELT (incrv
, i
) = incr
;
11954 /* Like tsubst_copy for expressions, etc. but also does semantic
11958 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
11959 bool integral_constant_expression_p
)
11961 #define RECUR(NODE) \
11962 tsubst_expr ((NODE), args, complain, in_decl, \
11963 integral_constant_expression_p)
11967 if (t
== NULL_TREE
|| t
== error_mark_node
)
11970 if (EXPR_HAS_LOCATION (t
))
11971 input_location
= EXPR_LOCATION (t
);
11972 if (STATEMENT_CODE_P (TREE_CODE (t
)))
11973 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
11975 switch (TREE_CODE (t
))
11977 case STATEMENT_LIST
:
11979 tree_stmt_iterator i
;
11980 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
11981 RECUR (tsi_stmt (i
));
11985 case CTOR_INITIALIZER
:
11986 finish_mem_initializers (tsubst_initializer_list
11987 (TREE_OPERAND (t
, 0), args
));
11991 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
11995 tmp
= RECUR (EXPR_STMT_EXPR (t
));
11996 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
11997 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
11999 finish_expr_stmt (tmp
);
12003 do_using_directive (USING_STMT_NAMESPACE (t
));
12008 tree decl
, pattern_decl
;
12011 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
12012 if (TREE_CODE (decl
) == LABEL_DECL
)
12013 finish_label_decl (DECL_NAME (decl
));
12014 else if (TREE_CODE (decl
) == USING_DECL
)
12016 tree scope
= USING_DECL_SCOPE (decl
);
12017 tree name
= DECL_NAME (decl
);
12020 scope
= tsubst (scope
, args
, complain
, in_decl
);
12021 decl
= lookup_qualified_name (scope
, name
,
12022 /*is_type_p=*/false,
12023 /*complain=*/false);
12024 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
12025 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
12027 do_local_using_decl (decl
, scope
, name
);
12031 init
= DECL_INITIAL (decl
);
12032 decl
= tsubst (decl
, args
, complain
, in_decl
);
12033 if (decl
!= error_mark_node
)
12035 /* By marking the declaration as instantiated, we avoid
12036 trying to instantiate it. Since instantiate_decl can't
12037 handle local variables, and since we've already done
12038 all that needs to be done, that's the right thing to
12040 if (TREE_CODE (decl
) == VAR_DECL
)
12041 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
12042 if (TREE_CODE (decl
) == VAR_DECL
12043 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
12044 /* Anonymous aggregates are a special case. */
12045 finish_anon_union (decl
);
12048 int const_init
= false;
12049 maybe_push_decl (decl
);
12050 if (TREE_CODE (decl
) == VAR_DECL
12051 && DECL_PRETTY_FUNCTION_P (decl
))
12053 /* For __PRETTY_FUNCTION__ we have to adjust the
12055 const char *const name
12056 = cxx_printable_name (current_function_decl
, 2);
12057 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
12061 tree t
= RECUR (init
);
12065 /* If we had an initializer but it
12066 instantiated to nothing,
12067 value-initialize the object. This will
12068 only occur when the initializer was a
12069 pack expansion where the parameter packs
12070 used in that expansion were of length
12072 init
= build_value_init (TREE_TYPE (decl
),
12074 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
12075 init
= get_target_expr (init
);
12081 if (TREE_CODE (decl
) == VAR_DECL
)
12082 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
12084 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
12089 /* A DECL_EXPR can also be used as an expression, in the condition
12090 clause of an if/for/while construct. */
12095 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12096 RECUR (FOR_INIT_STMT (t
));
12097 finish_for_init_stmt (stmt
);
12098 tmp
= RECUR (FOR_COND (t
));
12099 finish_for_cond (tmp
, stmt
);
12100 tmp
= RECUR (FOR_EXPR (t
));
12101 finish_for_expr (tmp
, stmt
);
12102 RECUR (FOR_BODY (t
));
12103 finish_for_stmt (stmt
);
12106 case RANGE_FOR_STMT
:
12109 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12110 decl
= RANGE_FOR_DECL (t
);
12111 decl
= tsubst (decl
, args
, complain
, in_decl
);
12112 maybe_push_decl (decl
);
12113 expr
= RECUR (RANGE_FOR_EXPR (t
));
12114 stmt
= cp_convert_range_for (stmt
, decl
, expr
);
12115 RECUR (RANGE_FOR_BODY (t
));
12116 finish_for_stmt (stmt
);
12121 stmt
= begin_while_stmt ();
12122 tmp
= RECUR (WHILE_COND (t
));
12123 finish_while_stmt_cond (tmp
, stmt
);
12124 RECUR (WHILE_BODY (t
));
12125 finish_while_stmt (stmt
);
12129 stmt
= begin_do_stmt ();
12130 RECUR (DO_BODY (t
));
12131 finish_do_body (stmt
);
12132 tmp
= RECUR (DO_COND (t
));
12133 finish_do_stmt (tmp
, stmt
);
12137 stmt
= begin_if_stmt ();
12138 tmp
= RECUR (IF_COND (t
));
12139 finish_if_stmt_cond (tmp
, stmt
);
12140 RECUR (THEN_CLAUSE (t
));
12141 finish_then_clause (stmt
);
12143 if (ELSE_CLAUSE (t
))
12145 begin_else_clause (stmt
);
12146 RECUR (ELSE_CLAUSE (t
));
12147 finish_else_clause (stmt
);
12150 finish_if_stmt (stmt
);
12154 if (BIND_EXPR_BODY_BLOCK (t
))
12155 stmt
= begin_function_body ();
12157 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
12158 ? BCS_TRY_BLOCK
: 0);
12160 RECUR (BIND_EXPR_BODY (t
));
12162 if (BIND_EXPR_BODY_BLOCK (t
))
12163 finish_function_body (stmt
);
12165 finish_compound_stmt (stmt
);
12169 finish_break_stmt ();
12172 case CONTINUE_STMT
:
12173 finish_continue_stmt ();
12177 stmt
= begin_switch_stmt ();
12178 tmp
= RECUR (SWITCH_STMT_COND (t
));
12179 finish_switch_cond (tmp
, stmt
);
12180 RECUR (SWITCH_STMT_BODY (t
));
12181 finish_switch_stmt (stmt
);
12184 case CASE_LABEL_EXPR
:
12185 finish_case_label (EXPR_LOCATION (t
),
12186 RECUR (CASE_LOW (t
)),
12187 RECUR (CASE_HIGH (t
)));
12192 tree decl
= LABEL_EXPR_LABEL (t
);
12195 label
= finish_label_stmt (DECL_NAME (decl
));
12196 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
12197 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
12202 tmp
= GOTO_DESTINATION (t
);
12203 if (TREE_CODE (tmp
) != LABEL_DECL
)
12204 /* Computed goto's must be tsubst'd into. On the other hand,
12205 non-computed gotos must not be; the identifier in question
12206 will have no binding. */
12209 tmp
= DECL_NAME (tmp
);
12210 finish_goto_stmt (tmp
);
12214 tmp
= finish_asm_stmt
12215 (ASM_VOLATILE_P (t
),
12216 RECUR (ASM_STRING (t
)),
12217 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
12218 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
12219 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
12220 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
12222 tree asm_expr
= tmp
;
12223 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
12224 asm_expr
= TREE_OPERAND (asm_expr
, 0);
12225 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
12232 stmt
= begin_try_block ();
12233 RECUR (TRY_STMTS (t
));
12234 finish_cleanup_try_block (stmt
);
12235 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
12239 tree compound_stmt
= NULL_TREE
;
12241 if (FN_TRY_BLOCK_P (t
))
12242 stmt
= begin_function_try_block (&compound_stmt
);
12244 stmt
= begin_try_block ();
12246 RECUR (TRY_STMTS (t
));
12248 if (FN_TRY_BLOCK_P (t
))
12249 finish_function_try_block (stmt
);
12251 finish_try_block (stmt
);
12253 RECUR (TRY_HANDLERS (t
));
12254 if (FN_TRY_BLOCK_P (t
))
12255 finish_function_handler_sequence (stmt
, compound_stmt
);
12257 finish_handler_sequence (stmt
);
12263 tree decl
= HANDLER_PARMS (t
);
12267 decl
= tsubst (decl
, args
, complain
, in_decl
);
12268 /* Prevent instantiate_decl from trying to instantiate
12269 this variable. We've already done all that needs to be
12271 if (decl
!= error_mark_node
)
12272 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
12274 stmt
= begin_handler ();
12275 finish_handler_parms (decl
, stmt
);
12276 RECUR (HANDLER_BODY (t
));
12277 finish_handler (stmt
);
12282 tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
12285 case STATIC_ASSERT
:
12288 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
12291 /*integral_constant_expression_p=*/true);
12292 finish_static_assert (condition
,
12293 STATIC_ASSERT_MESSAGE (t
),
12294 STATIC_ASSERT_SOURCE_LOCATION (t
),
12295 /*member_p=*/false);
12300 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
),
12301 args
, complain
, in_decl
);
12302 stmt
= begin_omp_parallel ();
12303 RECUR (OMP_PARALLEL_BODY (t
));
12304 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
12305 = OMP_PARALLEL_COMBINED (t
);
12309 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
),
12310 args
, complain
, in_decl
);
12311 stmt
= begin_omp_task ();
12312 RECUR (OMP_TASK_BODY (t
));
12313 finish_omp_task (tmp
, stmt
);
12318 tree clauses
, body
, pre_body
;
12319 tree declv
, initv
, condv
, incrv
;
12322 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
),
12323 args
, complain
, in_decl
);
12324 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
12325 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
12326 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
12327 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
12329 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
12330 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
12331 &clauses
, args
, complain
, in_decl
,
12332 integral_constant_expression_p
);
12334 stmt
= begin_omp_structured_block ();
12336 for (i
= 0; i
< TREE_VEC_LENGTH (initv
); i
++)
12337 if (TREE_VEC_ELT (initv
, i
) == NULL
12338 || TREE_CODE (TREE_VEC_ELT (initv
, i
)) != DECL_EXPR
)
12339 TREE_VEC_ELT (initv
, i
) = RECUR (TREE_VEC_ELT (initv
, i
));
12340 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv
, i
))))
12342 tree init
= RECUR (TREE_VEC_ELT (initv
, i
));
12343 gcc_assert (init
== TREE_VEC_ELT (declv
, i
));
12344 TREE_VEC_ELT (initv
, i
) = NULL_TREE
;
12348 tree decl_expr
= TREE_VEC_ELT (initv
, i
);
12349 tree init
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
12350 gcc_assert (init
!= NULL
);
12351 TREE_VEC_ELT (initv
, i
) = RECUR (init
);
12352 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL
;
12354 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init
;
12357 pre_body
= push_stmt_list ();
12358 RECUR (OMP_FOR_PRE_BODY (t
));
12359 pre_body
= pop_stmt_list (pre_body
);
12361 body
= push_stmt_list ();
12362 RECUR (OMP_FOR_BODY (t
));
12363 body
= pop_stmt_list (body
);
12365 t
= finish_omp_for (EXPR_LOCATION (t
), declv
, initv
, condv
, incrv
,
12366 body
, pre_body
, clauses
);
12368 add_stmt (finish_omp_structured_block (stmt
));
12374 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), args
, complain
, in_decl
);
12375 stmt
= push_stmt_list ();
12376 RECUR (OMP_BODY (t
));
12377 stmt
= pop_stmt_list (stmt
);
12380 OMP_BODY (t
) = stmt
;
12381 OMP_CLAUSES (t
) = tmp
;
12389 stmt
= push_stmt_list ();
12390 RECUR (OMP_BODY (t
));
12391 stmt
= pop_stmt_list (stmt
);
12394 OMP_BODY (t
) = stmt
;
12399 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
12401 tree op1
= TREE_OPERAND (t
, 1);
12402 tree lhs
= RECUR (TREE_OPERAND (op1
, 0));
12403 tree rhs
= RECUR (TREE_OPERAND (op1
, 1));
12404 finish_omp_atomic (TREE_CODE (op1
), lhs
, rhs
);
12408 case EXPR_PACK_EXPANSION
:
12409 error ("invalid use of pack expansion expression");
12410 return error_mark_node
;
12412 case NONTYPE_ARGUMENT_PACK
:
12413 error ("use %<...%> to expand argument pack");
12414 return error_mark_node
;
12417 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
12419 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12420 /*function_p=*/false,
12421 integral_constant_expression_p
);
12428 /* T is a postfix-expression that is not being used in a function
12429 call. Return the substituted version of T. */
12432 tsubst_non_call_postfix_expression (tree t
, tree args
,
12433 tsubst_flags_t complain
,
12436 if (TREE_CODE (t
) == SCOPE_REF
)
12437 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
12438 /*done=*/false, /*address_p=*/false);
12440 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12441 /*function_p=*/false,
12442 /*integral_constant_expression_p=*/false);
12447 /* Like tsubst but deals with expressions and performs semantic
12448 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
12451 tsubst_copy_and_build (tree t
,
12453 tsubst_flags_t complain
,
12456 bool integral_constant_expression_p
)
12458 #define RECUR(NODE) \
12459 tsubst_copy_and_build (NODE, args, complain, in_decl, \
12460 /*function_p=*/false, \
12461 integral_constant_expression_p)
12465 if (t
== NULL_TREE
|| t
== error_mark_node
)
12468 switch (TREE_CODE (t
))
12472 /* Fall through. */
12473 case IDENTIFIER_NODE
:
12477 bool non_integral_constant_expression_p
;
12478 const char *error_msg
;
12480 if (IDENTIFIER_TYPENAME_P (t
))
12482 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12483 t
= mangle_conv_op_name_for_type (new_type
);
12486 /* Look up the name. */
12487 decl
= lookup_name (t
);
12489 /* By convention, expressions use ERROR_MARK_NODE to indicate
12490 failure, not NULL_TREE. */
12491 if (decl
== NULL_TREE
)
12492 decl
= error_mark_node
;
12494 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
12496 integral_constant_expression_p
,
12497 /*allow_non_integral_constant_expression_p=*/false,
12498 &non_integral_constant_expression_p
,
12499 /*template_p=*/false,
12501 /*address_p=*/false,
12502 /*template_arg_p=*/false,
12507 if (!function_p
&& TREE_CODE (decl
) == IDENTIFIER_NODE
)
12508 decl
= unqualified_name_lookup_error (decl
);
12512 case TEMPLATE_ID_EXPR
:
12515 tree templ
= RECUR (TREE_OPERAND (t
, 0));
12516 tree targs
= TREE_OPERAND (t
, 1);
12519 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12521 if (TREE_CODE (templ
) == COMPONENT_REF
)
12523 object
= TREE_OPERAND (templ
, 0);
12524 templ
= TREE_OPERAND (templ
, 1);
12527 object
= NULL_TREE
;
12528 templ
= lookup_template_function (templ
, targs
);
12531 return build3 (COMPONENT_REF
, TREE_TYPE (templ
),
12532 object
, templ
, NULL_TREE
);
12534 return baselink_for_fns (templ
);
12539 tree r
= RECUR (TREE_OPERAND (t
, 0));
12541 if (REFERENCE_REF_P (t
))
12543 /* A type conversion to reference type will be enclosed in
12544 such an indirect ref, but the substitution of the cast
12545 will have also added such an indirect ref. */
12546 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
12547 r
= convert_from_reference (r
);
12550 r
= build_x_indirect_ref (r
, RO_UNARY_STAR
, complain
);
12556 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12557 RECUR (TREE_OPERAND (t
, 0)));
12560 case REINTERPRET_CAST_EXPR
:
12561 case CONST_CAST_EXPR
:
12562 case DYNAMIC_CAST_EXPR
:
12563 case STATIC_CAST_EXPR
:
12568 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12569 if (integral_constant_expression_p
12570 && !cast_valid_in_integral_constant_expression_p (type
))
12572 if (complain
& tf_error
)
12573 error ("a cast to a type other than an integral or "
12574 "enumeration type cannot appear in a constant-expression");
12575 return error_mark_node
;
12578 op
= RECUR (TREE_OPERAND (t
, 0));
12580 switch (TREE_CODE (t
))
12583 return build_functional_cast (type
, op
, complain
);
12584 case REINTERPRET_CAST_EXPR
:
12585 return build_reinterpret_cast (type
, op
, complain
);
12586 case CONST_CAST_EXPR
:
12587 return build_const_cast (type
, op
, complain
);
12588 case DYNAMIC_CAST_EXPR
:
12589 return build_dynamic_cast (type
, op
, complain
);
12590 case STATIC_CAST_EXPR
:
12591 return build_static_cast (type
, op
, complain
);
12593 gcc_unreachable ();
12597 case POSTDECREMENT_EXPR
:
12598 case POSTINCREMENT_EXPR
:
12599 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
12600 args
, complain
, in_decl
);
12601 return build_x_unary_op (TREE_CODE (t
), op1
, complain
);
12603 case PREDECREMENT_EXPR
:
12604 case PREINCREMENT_EXPR
:
12608 case TRUTH_NOT_EXPR
:
12609 case UNARY_PLUS_EXPR
: /* Unary + */
12610 case REALPART_EXPR
:
12611 case IMAGPART_EXPR
:
12612 return build_x_unary_op (TREE_CODE (t
), RECUR (TREE_OPERAND (t
, 0)),
12616 op1
= TREE_OPERAND (t
, 0);
12617 if (TREE_CODE (op1
) == LABEL_DECL
)
12618 return finish_label_address_expr (DECL_NAME (op1
),
12619 EXPR_LOCATION (op1
));
12620 if (TREE_CODE (op1
) == SCOPE_REF
)
12621 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
12622 /*done=*/true, /*address_p=*/true);
12624 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
12626 return build_x_unary_op (ADDR_EXPR
, op1
, complain
);
12631 case TRUNC_DIV_EXPR
:
12632 case CEIL_DIV_EXPR
:
12633 case FLOOR_DIV_EXPR
:
12634 case ROUND_DIV_EXPR
:
12635 case EXACT_DIV_EXPR
:
12639 case TRUNC_MOD_EXPR
:
12640 case FLOOR_MOD_EXPR
:
12641 case TRUTH_ANDIF_EXPR
:
12642 case TRUTH_ORIF_EXPR
:
12643 case TRUTH_AND_EXPR
:
12644 case TRUTH_OR_EXPR
:
12659 return build_x_binary_op
12661 RECUR (TREE_OPERAND (t
, 0)),
12662 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
12664 : TREE_CODE (TREE_OPERAND (t
, 0))),
12665 RECUR (TREE_OPERAND (t
, 1)),
12666 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
12668 : TREE_CODE (TREE_OPERAND (t
, 1))),
12669 /*overloaded_p=*/NULL
,
12673 return tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
12674 /*address_p=*/false);
12676 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
12677 args
, complain
, in_decl
);
12678 return build_x_array_ref (op1
, RECUR (TREE_OPERAND (t
, 1)), complain
);
12681 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
12682 return tsubst_copy (t
, args
, complain
, in_decl
);
12686 op1
= TREE_OPERAND (t
, 0);
12689 /* When there are no ARGS, we are trying to evaluate a
12690 non-dependent expression from the parser. Trying to do
12691 the substitutions may not work. */
12693 op1
= TREE_TYPE (op1
);
12697 ++cp_unevaluated_operand
;
12698 ++c_inhibit_evaluation_warnings
;
12699 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
12700 /*function_p=*/false,
12701 /*integral_constant_expression_p=*/false);
12702 --cp_unevaluated_operand
;
12703 --c_inhibit_evaluation_warnings
;
12706 return cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
12707 complain
& tf_error
);
12709 return cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
12710 complain
& tf_error
);
12712 case AT_ENCODE_EXPR
:
12714 op1
= TREE_OPERAND (t
, 0);
12715 ++cp_unevaluated_operand
;
12716 ++c_inhibit_evaluation_warnings
;
12717 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
12718 /*function_p=*/false,
12719 /*integral_constant_expression_p=*/false);
12720 --cp_unevaluated_operand
;
12721 --c_inhibit_evaluation_warnings
;
12722 return objc_build_encode_expr (op1
);
12725 case NOEXCEPT_EXPR
:
12726 op1
= TREE_OPERAND (t
, 0);
12727 ++cp_unevaluated_operand
;
12728 ++c_inhibit_evaluation_warnings
;
12729 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
12730 /*function_p=*/false,
12731 /*integral_constant_expression_p=*/false);
12732 --cp_unevaluated_operand
;
12733 --c_inhibit_evaluation_warnings
;
12734 return finish_noexcept_expr (op1
, complain
);
12738 tree r
= build_x_modify_expr
12739 (RECUR (TREE_OPERAND (t
, 0)),
12740 TREE_CODE (TREE_OPERAND (t
, 1)),
12741 RECUR (TREE_OPERAND (t
, 2)),
12743 /* TREE_NO_WARNING must be set if either the expression was
12744 parenthesized or it uses an operator such as >>= rather
12745 than plain assignment. In the former case, it was already
12746 set and must be copied. In the latter case,
12747 build_x_modify_expr sets it and it must not be reset
12749 if (TREE_NO_WARNING (t
))
12750 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12755 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
12756 args
, complain
, in_decl
);
12757 /* Remember that there was a reference to this entity. */
12760 return build_x_arrow (op1
);
12764 tree placement
= RECUR (TREE_OPERAND (t
, 0));
12765 tree init
= RECUR (TREE_OPERAND (t
, 3));
12766 VEC(tree
,gc
) *placement_vec
;
12767 VEC(tree
,gc
) *init_vec
;
12770 if (placement
== NULL_TREE
)
12771 placement_vec
= NULL
;
12774 placement_vec
= make_tree_vector ();
12775 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
12776 VEC_safe_push (tree
, gc
, placement_vec
, TREE_VALUE (placement
));
12779 /* If there was an initializer in the original tree, but it
12780 instantiated to an empty list, then we should pass a
12781 non-NULL empty vector to tell build_new that it was an
12782 empty initializer() rather than no initializer. This can
12783 only happen when the initializer is a pack expansion whose
12784 parameter packs are of length zero. */
12785 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
12789 init_vec
= make_tree_vector ();
12790 if (init
== void_zero_node
)
12791 gcc_assert (init_vec
!= NULL
);
12794 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
12795 VEC_safe_push (tree
, gc
, init_vec
, TREE_VALUE (init
));
12799 ret
= build_new (&placement_vec
,
12800 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12801 RECUR (TREE_OPERAND (t
, 2)),
12803 NEW_EXPR_USE_GLOBAL (t
),
12806 if (placement_vec
!= NULL
)
12807 release_tree_vector (placement_vec
);
12808 if (init_vec
!= NULL
)
12809 release_tree_vector (init_vec
);
12815 return delete_sanity
12816 (RECUR (TREE_OPERAND (t
, 0)),
12817 RECUR (TREE_OPERAND (t
, 1)),
12818 DELETE_EXPR_USE_VEC (t
),
12819 DELETE_EXPR_USE_GLOBAL (t
));
12821 case COMPOUND_EXPR
:
12822 return build_x_compound_expr (RECUR (TREE_OPERAND (t
, 0)),
12823 RECUR (TREE_OPERAND (t
, 1)),
12829 VEC(tree
,gc
) *call_args
;
12830 unsigned int nargs
, i
;
12835 function
= CALL_EXPR_FN (t
);
12836 /* When we parsed the expression, we determined whether or
12837 not Koenig lookup should be performed. */
12838 koenig_p
= KOENIG_LOOKUP_P (t
);
12839 if (TREE_CODE (function
) == SCOPE_REF
)
12841 qualified_p
= true;
12842 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
12844 /*address_p=*/false);
12848 if (TREE_CODE (function
) == COMPONENT_REF
)
12850 tree op
= TREE_OPERAND (function
, 1);
12852 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
12853 || (BASELINK_P (op
)
12854 && BASELINK_QUALIFIED_P (op
)));
12857 qualified_p
= false;
12859 function
= tsubst_copy_and_build (function
, args
, complain
,
12862 integral_constant_expression_p
);
12864 if (BASELINK_P (function
))
12865 qualified_p
= true;
12868 nargs
= call_expr_nargs (t
);
12869 call_args
= make_tree_vector ();
12870 for (i
= 0; i
< nargs
; ++i
)
12872 tree arg
= CALL_EXPR_ARG (t
, i
);
12874 if (!PACK_EXPANSION_P (arg
))
12875 VEC_safe_push (tree
, gc
, call_args
,
12876 RECUR (CALL_EXPR_ARG (t
, i
)));
12879 /* Expand the pack expansion and push each entry onto
12881 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
12882 if (TREE_CODE (arg
) == TREE_VEC
)
12884 unsigned int len
, j
;
12886 len
= TREE_VEC_LENGTH (arg
);
12887 for (j
= 0; j
< len
; ++j
)
12889 tree value
= TREE_VEC_ELT (arg
, j
);
12890 if (value
!= NULL_TREE
)
12891 value
= convert_from_reference (value
);
12892 VEC_safe_push (tree
, gc
, call_args
, value
);
12897 /* A partial substitution. Add one entry. */
12898 VEC_safe_push (tree
, gc
, call_args
, arg
);
12903 /* We do not perform argument-dependent lookup if normal
12904 lookup finds a non-function, in accordance with the
12905 expected resolution of DR 218. */
12907 && ((is_overloaded_fn (function
)
12908 /* If lookup found a member function, the Koenig lookup is
12909 not appropriate, even if an unqualified-name was used
12910 to denote the function. */
12911 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
12912 || TREE_CODE (function
) == IDENTIFIER_NODE
)
12913 /* Only do this when substitution turns a dependent call
12914 into a non-dependent call. */
12915 && type_dependent_expression_p_push (t
)
12916 && !any_type_dependent_arguments_p (call_args
))
12917 function
= perform_koenig_lookup (function
, call_args
, false);
12919 if (TREE_CODE (function
) == IDENTIFIER_NODE
)
12921 unqualified_name_lookup_error (function
);
12922 release_tree_vector (call_args
);
12923 return error_mark_node
;
12926 /* Remember that there was a reference to this entity. */
12927 if (DECL_P (function
))
12928 mark_used (function
);
12930 if (TREE_CODE (function
) == OFFSET_REF
)
12931 ret
= build_offset_ref_call_from_tree (function
, &call_args
);
12932 else if (TREE_CODE (function
) == COMPONENT_REF
)
12934 tree instance
= TREE_OPERAND (function
, 0);
12935 tree fn
= TREE_OPERAND (function
, 1);
12937 if (processing_template_decl
12938 && (type_dependent_expression_p (instance
)
12939 || (!BASELINK_P (fn
)
12940 && TREE_CODE (fn
) != FIELD_DECL
)
12941 || type_dependent_expression_p (fn
)
12942 || any_type_dependent_arguments_p (call_args
)))
12943 ret
= build_nt_call_vec (function
, call_args
);
12944 else if (!BASELINK_P (fn
))
12945 ret
= finish_call_expr (function
, &call_args
,
12946 /*disallow_virtual=*/false,
12947 /*koenig_p=*/false,
12950 ret
= (build_new_method_call
12952 &call_args
, NULL_TREE
,
12953 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
12958 ret
= finish_call_expr (function
, &call_args
,
12959 /*disallow_virtual=*/qualified_p
,
12963 release_tree_vector (call_args
);
12969 return build_x_conditional_expr
12970 (RECUR (TREE_OPERAND (t
, 0)),
12971 RECUR (TREE_OPERAND (t
, 1)),
12972 RECUR (TREE_OPERAND (t
, 2)),
12975 case PSEUDO_DTOR_EXPR
:
12976 return finish_pseudo_destructor_expr
12977 (RECUR (TREE_OPERAND (t
, 0)),
12978 RECUR (TREE_OPERAND (t
, 1)),
12979 RECUR (TREE_OPERAND (t
, 2)));
12983 tree purpose
, value
, chain
;
12985 if (t
== void_list_node
)
12988 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
12989 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
12991 /* We have pack expansions, so expand those and
12992 create a new list out of it. */
12993 tree purposevec
= NULL_TREE
;
12994 tree valuevec
= NULL_TREE
;
12998 /* Expand the argument expressions. */
12999 if (TREE_PURPOSE (t
))
13000 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
13001 complain
, in_decl
);
13002 if (TREE_VALUE (t
))
13003 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
13004 complain
, in_decl
);
13006 /* Build the rest of the list. */
13007 chain
= TREE_CHAIN (t
);
13008 if (chain
&& chain
!= void_type_node
)
13009 chain
= RECUR (chain
);
13011 /* Determine the number of arguments. */
13012 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
13014 len
= TREE_VEC_LENGTH (purposevec
);
13015 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
13017 else if (TREE_CODE (valuevec
) == TREE_VEC
)
13018 len
= TREE_VEC_LENGTH (valuevec
);
13021 /* Since we only performed a partial substitution into
13022 the argument pack, we only return a single list
13024 if (purposevec
== TREE_PURPOSE (t
)
13025 && valuevec
== TREE_VALUE (t
)
13026 && chain
== TREE_CHAIN (t
))
13029 return tree_cons (purposevec
, valuevec
, chain
);
13032 /* Convert the argument vectors into a TREE_LIST */
13036 /* Grab the Ith values. */
13038 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
13041 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
13044 /* Build the list (backwards). */
13045 chain
= tree_cons (purpose
, value
, chain
);
13051 purpose
= TREE_PURPOSE (t
);
13053 purpose
= RECUR (purpose
);
13054 value
= TREE_VALUE (t
);
13056 value
= RECUR (value
);
13057 chain
= TREE_CHAIN (t
);
13058 if (chain
&& chain
!= void_type_node
)
13059 chain
= RECUR (chain
);
13060 if (purpose
== TREE_PURPOSE (t
)
13061 && value
== TREE_VALUE (t
)
13062 && chain
== TREE_CHAIN (t
))
13064 return tree_cons (purpose
, value
, chain
);
13067 case COMPONENT_REF
:
13073 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13074 args
, complain
, in_decl
);
13075 /* Remember that there was a reference to this entity. */
13076 if (DECL_P (object
))
13077 mark_used (object
);
13078 object_type
= TREE_TYPE (object
);
13080 member
= TREE_OPERAND (t
, 1);
13081 if (BASELINK_P (member
))
13082 member
= tsubst_baselink (member
,
13083 non_reference (TREE_TYPE (object
)),
13084 args
, complain
, in_decl
);
13086 member
= tsubst_copy (member
, args
, complain
, in_decl
);
13087 if (member
== error_mark_node
)
13088 return error_mark_node
;
13090 if (object_type
&& !CLASS_TYPE_P (object_type
))
13092 if (SCALAR_TYPE_P (object_type
))
13094 tree s
= NULL_TREE
;
13095 tree dtor
= member
;
13097 if (TREE_CODE (dtor
) == SCOPE_REF
)
13099 s
= TREE_OPERAND (dtor
, 0);
13100 dtor
= TREE_OPERAND (dtor
, 1);
13102 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
13104 dtor
= TREE_OPERAND (dtor
, 0);
13106 return finish_pseudo_destructor_expr (object
, s
, dtor
);
13110 else if (TREE_CODE (member
) == SCOPE_REF
13111 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
13116 /* Lookup the template functions now that we know what the
13118 tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
13119 args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
13120 member
= lookup_qualified_name (TREE_OPERAND (member
, 0), tmpl
,
13121 /*is_type_p=*/false,
13122 /*complain=*/false);
13123 if (BASELINK_P (member
))
13125 BASELINK_FUNCTIONS (member
)
13126 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
13128 member
= (adjust_result_of_qualified_name_lookup
13129 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
13134 qualified_name_lookup_error (object_type
, tmpl
, member
,
13136 return error_mark_node
;
13139 else if (TREE_CODE (member
) == SCOPE_REF
13140 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
13141 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
13143 if (complain
& tf_error
)
13145 if (TYPE_P (TREE_OPERAND (member
, 0)))
13146 error ("%qT is not a class or namespace",
13147 TREE_OPERAND (member
, 0));
13149 error ("%qD is not a class or namespace",
13150 TREE_OPERAND (member
, 0));
13152 return error_mark_node
;
13154 else if (TREE_CODE (member
) == FIELD_DECL
)
13155 return finish_non_static_data_member (member
, object
, NULL_TREE
);
13157 return finish_class_member_access_expr (object
, member
,
13158 /*template_p=*/false,
13164 (RECUR (TREE_OPERAND (t
, 0)));
13168 VEC(constructor_elt
,gc
) *n
;
13169 constructor_elt
*ce
;
13170 unsigned HOST_WIDE_INT idx
;
13171 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13172 bool process_index_p
;
13174 bool need_copy_p
= false;
13177 if (type
== error_mark_node
)
13178 return error_mark_node
;
13180 /* digest_init will do the wrong thing if we let it. */
13181 if (type
&& TYPE_PTRMEMFUNC_P (type
))
13184 /* We do not want to process the index of aggregate
13185 initializers as they are identifier nodes which will be
13186 looked up by digest_init. */
13187 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
13189 n
= VEC_copy (constructor_elt
, gc
, CONSTRUCTOR_ELTS (t
));
13190 newlen
= VEC_length (constructor_elt
, n
);
13191 FOR_EACH_VEC_ELT (constructor_elt
, n
, idx
, ce
)
13193 if (ce
->index
&& process_index_p
)
13194 ce
->index
= RECUR (ce
->index
);
13196 if (PACK_EXPANSION_P (ce
->value
))
13198 /* Substitute into the pack expansion. */
13199 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
13202 if (ce
->value
== error_mark_node
)
13204 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
13205 /* Just move the argument into place. */
13206 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
13209 /* Update the length of the final CONSTRUCTOR
13210 arguments vector, and note that we will need to
13212 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
13213 need_copy_p
= true;
13217 ce
->value
= RECUR (ce
->value
);
13222 VEC(constructor_elt
,gc
) *old_n
= n
;
13224 n
= VEC_alloc (constructor_elt
, gc
, newlen
);
13225 FOR_EACH_VEC_ELT (constructor_elt
, old_n
, idx
, ce
)
13227 if (TREE_CODE (ce
->value
) == TREE_VEC
)
13229 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
13230 for (i
= 0; i
< len
; ++i
)
13231 CONSTRUCTOR_APPEND_ELT (n
, 0,
13232 TREE_VEC_ELT (ce
->value
, i
));
13235 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
13239 r
= build_constructor (init_list_type_node
, n
);
13240 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
13242 if (TREE_HAS_CONSTRUCTOR (t
))
13243 return finish_compound_literal (type
, r
);
13245 TREE_TYPE (r
) = type
;
13251 tree operand_0
= TREE_OPERAND (t
, 0);
13252 if (TYPE_P (operand_0
))
13254 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
13255 return get_typeid (operand_0
);
13259 operand_0
= RECUR (operand_0
);
13260 return build_typeid (operand_0
);
13271 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
13273 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
13274 /* If the original type was a reference, we'll be wrapped in
13275 the appropriate INDIRECT_REF. */
13276 r
= convert_from_reference (r
);
13281 return build_x_va_arg (RECUR (TREE_OPERAND (t
, 0)),
13282 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
13284 case OFFSETOF_EXPR
:
13285 return finish_offsetof (RECUR (TREE_OPERAND (t
, 0)));
13289 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
13290 complain
, in_decl
);
13292 tree type2
= TRAIT_EXPR_TYPE2 (t
);
13294 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
13296 return finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
);
13301 tree old_stmt_expr
= cur_stmt_expr
;
13302 tree stmt_expr
= begin_stmt_expr ();
13304 cur_stmt_expr
= stmt_expr
;
13305 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
13306 integral_constant_expression_p
);
13307 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
13308 cur_stmt_expr
= old_stmt_expr
;
13310 /* If the resulting list of expression statement is empty,
13311 fold it further into void_zero_node. */
13312 if (empty_expr_stmt_p (stmt_expr
))
13313 stmt_expr
= void_zero_node
;
13319 t
= tsubst_copy (t
, args
, complain
, in_decl
);
13320 /* As in finish_id_expression, we resolve enumeration constants
13321 to their underlying values. */
13322 if (TREE_CODE (t
) == CONST_DECL
)
13324 used_types_insert (TREE_TYPE (t
));
13325 return DECL_INITIAL (t
);
13331 tree r
= build_lambda_expr ();
13333 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
13334 TREE_TYPE (r
) = type
;
13335 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
13337 LAMBDA_EXPR_LOCATION (r
)
13338 = LAMBDA_EXPR_LOCATION (t
);
13339 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
13340 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
13341 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
13342 LAMBDA_EXPR_DISCRIMINATOR (r
)
13343 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
13344 LAMBDA_EXPR_CAPTURE_LIST (r
)
13345 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t
));
13346 LAMBDA_EXPR_THIS_CAPTURE (r
)
13347 = RECUR (LAMBDA_EXPR_THIS_CAPTURE (t
));
13348 LAMBDA_EXPR_EXTRA_SCOPE (r
)
13349 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t
));
13351 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
13352 determine_visibility (TYPE_NAME (type
));
13353 /* Now that we know visibility, instantiate the type so we have a
13354 declaration of the op() for later calls to lambda_function. */
13355 complete_type (type
);
13357 type
= tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
13359 apply_lambda_return_type (r
, type
);
13361 return build_lambda_object (r
);
13365 /* We can get here for a constant initializer of non-dependent type.
13366 FIXME stop folding in cp_parser_initializer_clause. */
13367 gcc_assert (TREE_CONSTANT (t
));
13368 return get_target_expr (RECUR (TARGET_EXPR_INITIAL (t
)));
13371 /* Handle Objective-C++ constructs, if appropriate. */
13374 = objcp_tsubst_copy_and_build (t
, args
, complain
,
13375 in_decl
, /*function_p=*/false);
13379 return tsubst_copy (t
, args
, complain
, in_decl
);
13385 /* Verify that the instantiated ARGS are valid. For type arguments,
13386 make sure that the type's linkage is ok. For non-type arguments,
13387 make sure they are constants if they are integral or enumerations.
13388 Emit an error under control of COMPLAIN, and return TRUE on error. */
13391 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
13393 if (ARGUMENT_PACK_P (t
))
13395 tree vec
= ARGUMENT_PACK_ARGS (t
);
13396 int len
= TREE_VEC_LENGTH (vec
);
13397 bool result
= false;
13400 for (i
= 0; i
< len
; ++i
)
13401 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
13405 else if (TYPE_P (t
))
13407 /* [basic.link]: A name with no linkage (notably, the name
13408 of a class or enumeration declared in a local scope)
13409 shall not be used to declare an entity with linkage.
13410 This implies that names with no linkage cannot be used as
13413 DR 757 relaxes this restriction for C++0x. */
13414 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
13415 : no_linkage_check (t
, /*relaxed_p=*/false));
13419 /* DR 488 makes use of a type with no linkage cause
13420 type deduction to fail. */
13421 if (complain
& tf_error
)
13423 if (TYPE_ANONYMOUS_P (nt
))
13424 error ("%qT is/uses anonymous type", t
);
13426 error ("template argument for %qD uses local type %qT",
13431 /* In order to avoid all sorts of complications, we do not
13432 allow variably-modified types as template arguments. */
13433 else if (variably_modified_type_p (t
, NULL_TREE
))
13435 if (complain
& tf_error
)
13436 error ("%qT is a variably modified type", t
);
13440 /* A non-type argument of integral or enumerated type must be a
13442 else if (TREE_TYPE (t
)
13443 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
13444 && !TREE_CONSTANT (t
))
13446 if (complain
& tf_error
)
13447 error ("integral expression %qE is not constant", t
);
13454 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
13456 int ix
, len
= DECL_NTPARMS (tmpl
);
13457 bool result
= false;
13459 for (ix
= 0; ix
!= len
; ix
++)
13461 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
13464 if (result
&& (complain
& tf_error
))
13465 error (" trying to instantiate %qD", tmpl
);
13469 /* Instantiate the indicated variable or function template TMPL with
13470 the template arguments in TARG_PTR. */
13473 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
13475 tree targ_ptr
= orig_args
;
13479 HOST_WIDE_INT saved_processing_template_decl
;
13481 if (tmpl
== error_mark_node
)
13482 return error_mark_node
;
13484 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
13486 /* If this function is a clone, handle it specially. */
13487 if (DECL_CLONED_FUNCTION_P (tmpl
))
13492 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
13493 DECL_CLONED_FUNCTION. */
13494 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
13495 targ_ptr
, complain
);
13496 if (spec
== error_mark_node
)
13497 return error_mark_node
;
13499 /* Look for the clone. */
13500 FOR_EACH_CLONE (clone
, spec
)
13501 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
13503 /* We should always have found the clone by now. */
13504 gcc_unreachable ();
13508 /* Check to see if we already have this specialization. */
13509 gen_tmpl
= most_general_template (tmpl
);
13510 if (tmpl
!= gen_tmpl
)
13511 /* The TMPL is a partial instantiation. To get a full set of
13512 arguments we must add the arguments used to perform the
13513 partial instantiation. */
13514 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
13517 /* It would be nice to avoid hashing here and then again in tsubst_decl,
13518 but it doesn't seem to be on the hot path. */
13519 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
13521 gcc_assert (tmpl
== gen_tmpl
13522 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
13524 || fndecl
== NULL_TREE
);
13526 if (spec
!= NULL_TREE
)
13529 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
13531 return error_mark_node
;
13533 /* We are building a FUNCTION_DECL, during which the access of its
13534 parameters and return types have to be checked. However this
13535 FUNCTION_DECL which is the desired context for access checking
13536 is not built yet. We solve this chicken-and-egg problem by
13537 deferring all checks until we have the FUNCTION_DECL. */
13538 push_deferring_access_checks (dk_deferred
);
13540 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
13541 (because, for example, we have encountered a non-dependent
13542 function call in the body of a template function and must now
13543 determine which of several overloaded functions will be called),
13544 within the instantiation itself we are not processing a
13546 saved_processing_template_decl
= processing_template_decl
;
13547 processing_template_decl
= 0;
13548 /* Substitute template parameters to obtain the specialization. */
13549 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
13550 targ_ptr
, complain
, gen_tmpl
);
13551 processing_template_decl
= saved_processing_template_decl
;
13552 if (fndecl
== error_mark_node
)
13553 return error_mark_node
;
13555 /* Now we know the specialization, compute access previously
13557 push_access_scope (fndecl
);
13559 /* Some typedefs referenced from within the template code need to be access
13560 checked at template instantiation time, i.e now. These types were
13561 added to the template at parsing time. Let's get those and perfom
13562 the acces checks then. */
13563 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl
), targ_ptr
);
13564 perform_deferred_access_checks ();
13565 pop_access_scope (fndecl
);
13566 pop_deferring_access_checks ();
13568 /* The DECL_TI_TEMPLATE should always be the immediate parent
13569 template, not the most general template. */
13570 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
13572 /* If we've just instantiated the main entry point for a function,
13573 instantiate all the alternate entry points as well. We do this
13574 by cloning the instantiation of the main entry point, not by
13575 instantiating the template clones. */
13576 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
13577 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
13582 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
13583 NARGS elements of the arguments that are being used when calling
13584 it. TARGS is a vector into which the deduced template arguments
13587 Return zero for success, 2 for an incomplete match that doesn't resolve
13588 all the types, and 1 for complete failure. An error message will be
13589 printed only for an incomplete match.
13591 If FN is a conversion operator, or we are trying to produce a specific
13592 specialization, RETURN_TYPE is the return type desired.
13594 The EXPLICIT_TARGS are explicit template arguments provided via a
13597 The parameter STRICT is one of:
13600 We are deducing arguments for a function call, as in
13601 [temp.deduct.call].
13604 We are deducing arguments for a conversion function, as in
13605 [temp.deduct.conv].
13608 We are deducing arguments when doing an explicit instantiation
13609 as in [temp.explicit], when determining an explicit specialization
13610 as in [temp.expl.spec], or when taking the address of a function
13611 template, as in [temp.deduct.funcaddr]. */
13614 fn_type_unification (tree fn
,
13615 tree explicit_targs
,
13618 unsigned int nargs
,
13620 unification_kind_t strict
,
13626 bool incomplete_argument_packs_p
= false;
13628 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
13630 fntype
= TREE_TYPE (fn
);
13631 if (explicit_targs
)
13635 The specified template arguments must match the template
13636 parameters in kind (i.e., type, nontype, template), and there
13637 must not be more arguments than there are parameters;
13638 otherwise type deduction fails.
13640 Nontype arguments must match the types of the corresponding
13641 nontype template parameters, or must be convertible to the
13642 types of the corresponding nontype parameters as specified in
13643 _temp.arg.nontype_, otherwise type deduction fails.
13645 All references in the function type of the function template
13646 to the corresponding template parameters are replaced by the
13647 specified template argument values. If a substitution in a
13648 template parameter or in the function type of the function
13649 template results in an invalid type, type deduction fails. */
13650 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
13651 int i
, len
= TREE_VEC_LENGTH (tparms
);
13652 tree converted_args
;
13653 bool incomplete
= false;
13655 if (explicit_targs
== error_mark_node
)
13659 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
, tf_none
,
13660 /*require_all_args=*/false,
13661 /*use_default_args=*/false));
13662 if (converted_args
== error_mark_node
)
13665 /* Substitute the explicit args into the function type. This is
13666 necessary so that, for instance, explicitly declared function
13667 arguments can match null pointed constants. If we were given
13668 an incomplete set of explicit args, we must not do semantic
13669 processing during substitution as we could create partial
13671 for (i
= 0; i
< len
; i
++)
13673 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
13674 bool parameter_pack
= false;
13676 /* Dig out the actual parm. */
13677 if (TREE_CODE (parm
) == TYPE_DECL
13678 || TREE_CODE (parm
) == TEMPLATE_DECL
)
13680 parm
= TREE_TYPE (parm
);
13681 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
13683 else if (TREE_CODE (parm
) == PARM_DECL
)
13685 parm
= DECL_INITIAL (parm
);
13686 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
13689 if (parameter_pack
)
13693 template_parm_level_and_index (parm
, &level
, &idx
);
13695 /* Mark the argument pack as "incomplete". We could
13696 still deduce more arguments during unification. */
13697 targ
= TMPL_ARG (converted_args
, level
, idx
);
13700 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
13701 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
13702 = ARGUMENT_PACK_ARGS (targ
);
13705 /* We have some incomplete argument packs. */
13706 incomplete_argument_packs_p
= true;
13710 if (incomplete_argument_packs_p
)
13711 /* Any substitution is guaranteed to be incomplete if there
13712 are incomplete argument packs, because we can still deduce
13716 incomplete
= NUM_TMPL_ARGS (explicit_targs
) != NUM_TMPL_ARGS (targs
);
13718 processing_template_decl
+= incomplete
;
13719 fntype
= tsubst (fntype
, converted_args
, tf_none
, NULL_TREE
);
13720 processing_template_decl
-= incomplete
;
13722 if (fntype
== error_mark_node
)
13725 /* Place the explicitly specified arguments in TARGS. */
13726 for (i
= NUM_TMPL_ARGS (converted_args
); i
--;)
13727 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (converted_args
, i
);
13730 /* Never do unification on the 'this' parameter. */
13731 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
13737 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
13738 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
13739 new_args
[0] = return_type
;
13740 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
13745 /* We allow incomplete unification without an error message here
13746 because the standard doesn't seem to explicitly prohibit it. Our
13747 callers must be ready to deal with unification failures in any
13749 result
= type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
13750 targs
, parms
, args
, nargs
, /*subr=*/0,
13753 if (result
== 0 && incomplete_argument_packs_p
)
13755 int i
, len
= NUM_TMPL_ARGS (targs
);
13757 /* Clear the "incomplete" flags on all argument packs. */
13758 for (i
= 0; i
< len
; i
++)
13760 tree arg
= TREE_VEC_ELT (targs
, i
);
13761 if (ARGUMENT_PACK_P (arg
))
13763 ARGUMENT_PACK_INCOMPLETE_P (arg
) = 0;
13764 ARGUMENT_PACK_EXPLICIT_ARGS (arg
) = NULL_TREE
;
13769 /* Now that we have bindings for all of the template arguments,
13770 ensure that the arguments deduced for the template template
13771 parameters have compatible template parameter lists. We cannot
13772 check this property before we have deduced all template
13773 arguments, because the template parameter types of a template
13774 template parameter might depend on prior template parameters
13775 deduced after the template template parameter. The following
13776 ill-formed example illustrates this issue:
13778 template<typename T, template<T> class C> void f(C<5>, T);
13780 template<int N> struct X {};
13783 f(X<5>(), 5l); // error: template argument deduction fails
13786 The template parameter list of 'C' depends on the template type
13787 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
13788 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
13789 time that we deduce 'C'. */
13791 && !template_template_parm_bindings_ok_p
13792 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
13796 /* All is well so far. Now, check:
13800 When all template arguments have been deduced, all uses of
13801 template parameters in nondeduced contexts are replaced with
13802 the corresponding deduced argument values. If the
13803 substitution results in an invalid type, as described above,
13804 type deduction fails. */
13806 tree substed
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
13807 if (substed
== error_mark_node
)
13810 /* If we're looking for an exact match, check that what we got
13811 is indeed an exact match. It might not be if some template
13812 parameters are used in non-deduced contexts. */
13813 if (strict
== DEDUCE_EXACT
)
13818 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (substed
));
13820 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
13821 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
13822 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
13830 /* Adjust types before performing type deduction, as described in
13831 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
13832 sections are symmetric. PARM is the type of a function parameter
13833 or the return type of the conversion function. ARG is the type of
13834 the argument passed to the call, or the type of the value
13835 initialized with the result of the conversion function.
13836 ARG_EXPR is the original argument expression, which may be null. */
13839 maybe_adjust_types_for_deduction (unification_kind_t strict
,
13853 /* Swap PARM and ARG throughout the remainder of this
13854 function; the handling is precisely symmetric since PARM
13855 will initialize ARG rather than vice versa. */
13863 /* Core issue #873: Do the DR606 thing (see below) for these cases,
13864 too, but here handle it by stripping the reference from PARM
13865 rather than by adding it to ARG. */
13866 if (TREE_CODE (*parm
) == REFERENCE_TYPE
13867 && TYPE_REF_IS_RVALUE (*parm
)
13868 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
13869 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
13870 && TREE_CODE (*arg
) == REFERENCE_TYPE
13871 && !TYPE_REF_IS_RVALUE (*arg
))
13872 *parm
= TREE_TYPE (*parm
);
13873 /* Nothing else to do in this case. */
13877 gcc_unreachable ();
13880 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
13882 /* [temp.deduct.call]
13884 If P is not a reference type:
13886 --If A is an array type, the pointer type produced by the
13887 array-to-pointer standard conversion (_conv.array_) is
13888 used in place of A for type deduction; otherwise,
13890 --If A is a function type, the pointer type produced by
13891 the function-to-pointer standard conversion
13892 (_conv.func_) is used in place of A for type deduction;
13895 --If A is a cv-qualified type, the top level
13896 cv-qualifiers of A's type are ignored for type
13898 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
13899 *arg
= build_pointer_type (TREE_TYPE (*arg
));
13900 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
13901 *arg
= build_pointer_type (*arg
);
13903 *arg
= TYPE_MAIN_VARIANT (*arg
);
13906 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
13907 of the form T&&, where T is a template parameter, and the argument
13908 is an lvalue, T is deduced as A& */
13909 if (TREE_CODE (*parm
) == REFERENCE_TYPE
13910 && TYPE_REF_IS_RVALUE (*parm
)
13911 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
13912 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
13913 && arg_expr
&& real_lvalue_p (arg_expr
))
13914 *arg
= build_reference_type (*arg
);
13916 /* [temp.deduct.call]
13918 If P is a cv-qualified type, the top level cv-qualifiers
13919 of P's type are ignored for type deduction. If P is a
13920 reference type, the type referred to by P is used for
13922 *parm
= TYPE_MAIN_VARIANT (*parm
);
13923 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
13925 *parm
= TREE_TYPE (*parm
);
13926 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
13929 /* DR 322. For conversion deduction, remove a reference type on parm
13930 too (which has been swapped into ARG). */
13931 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
13932 *arg
= TREE_TYPE (*arg
);
13937 /* Most parms like fn_type_unification.
13939 If SUBR is 1, we're being called recursively (to unify the
13940 arguments of a function or method parameter of a function
13944 type_unification_real (tree tparms
,
13948 unsigned int xnargs
,
13950 unification_kind_t strict
,
13953 tree parm
, arg
, arg_expr
;
13955 int ntparms
= TREE_VEC_LENGTH (tparms
);
13957 int saw_undeduced
= 0;
13960 unsigned int nargs
;
13963 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
13964 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
13965 gcc_assert (ntparms
> 0);
13967 /* Reset the number of non-defaulted template arguments contained
13969 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
13974 sub_strict
= (UNIFY_ALLOW_OUTER_LEVEL
| UNIFY_ALLOW_MORE_CV_QUAL
13975 | UNIFY_ALLOW_DERIVED
);
13979 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
13983 sub_strict
= UNIFY_ALLOW_NONE
;
13987 gcc_unreachable ();
13996 while (parms
&& parms
!= void_list_node
13999 if (TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
14002 parm
= TREE_VALUE (parms
);
14003 parms
= TREE_CHAIN (parms
);
14008 if (arg
== error_mark_node
)
14010 if (arg
== unknown_type_node
)
14011 /* We can't deduce anything from this, but we might get all the
14012 template args from other function args. */
14015 /* Conversions will be performed on a function argument that
14016 corresponds with a function parameter that contains only
14017 non-deducible template parameters and explicitly specified
14018 template parameters. */
14019 if (!uses_template_parms (parm
))
14024 type
= TREE_TYPE (arg
);
14028 if (same_type_p (parm
, type
))
14030 if (strict
!= DEDUCE_EXACT
14031 && can_convert_arg (parm
, type
, TYPE_P (arg
) ? NULL_TREE
: arg
,
14040 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
14041 if (type_unknown_p (arg
))
14043 /* [temp.deduct.type]
14045 A template-argument can be deduced from a pointer to
14046 function or pointer to member function argument if
14047 the set of overloaded functions does not contain
14048 function templates and at most one of a set of
14049 overloaded functions provides a unique match. */
14050 if (resolve_overloaded_unification
14051 (tparms
, targs
, parm
, arg
, strict
, sub_strict
))
14057 arg
= unlowered_expr_type (arg
);
14058 if (arg
== error_mark_node
)
14063 int arg_strict
= sub_strict
;
14066 arg_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
,
14069 if (arg
== init_list_type_node
&& arg_expr
)
14071 if (unify (tparms
, targs
, parm
, arg
, arg_strict
))
14078 && parms
!= void_list_node
14079 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
14081 /* Unify the remaining arguments with the pack expansion type. */
14083 tree parmvec
= make_tree_vec (1);
14085 /* Allocate a TREE_VEC and copy in all of the arguments */
14086 argvec
= make_tree_vec (nargs
- ia
);
14087 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
14088 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
14090 /* Copy the parameter into parmvec. */
14091 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
14092 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
14093 /*call_args_p=*/true, /*subr=*/subr
))
14096 /* Advance to the end of the list of parameters. */
14097 parms
= TREE_CHAIN (parms
);
14100 /* Fail if we've reached the end of the parm list, and more args
14101 are present, and the parm list isn't variadic. */
14102 if (ia
< nargs
&& parms
== void_list_node
)
14104 /* Fail if parms are left and they don't have default values. */
14105 if (parms
&& parms
!= void_list_node
14106 && TREE_PURPOSE (parms
) == NULL_TREE
)
14110 for (i
= 0; i
< ntparms
; i
++)
14111 if (!TREE_VEC_ELT (targs
, i
))
14115 if (TREE_VEC_ELT (tparms
, i
) == error_mark_node
)
14118 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
14120 /* If this is an undeduced nontype parameter that depends on
14121 a type parameter, try another pass; its type may have been
14122 deduced from a later argument than the one from which
14123 this parameter can be deduced. */
14124 if (TREE_CODE (tparm
) == PARM_DECL
14125 && uses_template_parms (TREE_TYPE (tparm
))
14126 && !saw_undeduced
++)
14129 /* Core issue #226 (C++0x) [temp.deduct]:
14131 If a template argument has not been deduced, its
14132 default template argument, if any, is used.
14134 When we are in C++98 mode, TREE_PURPOSE will either
14135 be NULL_TREE or ERROR_MARK_NODE, so we do not need
14136 to explicitly check cxx_dialect here. */
14137 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
14139 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
14140 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
14141 arg
= tsubst_template_arg (arg
, targs
, tf_none
, NULL_TREE
);
14142 arg
= convert_template_argument (parm
, arg
, targs
, tf_none
,
14144 if (arg
== error_mark_node
)
14148 TREE_VEC_ELT (targs
, i
) = arg
;
14149 /* The position of the first default template argument,
14150 is also the number of non-defaulted arguments in TARGS.
14152 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
14153 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
14158 /* If the type parameter is a parameter pack, then it will
14159 be deduced to an empty parameter pack. */
14160 if (template_parameter_pack_p (tparm
))
14164 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
14166 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
14167 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
14168 TREE_CONSTANT (arg
) = 1;
14171 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
14173 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
14175 TREE_VEC_ELT (targs
, i
) = arg
;
14181 #ifdef ENABLE_CHECKING
14182 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
14183 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
14189 /* Subroutine of type_unification_real. Args are like the variables
14190 at the call site. ARG is an overloaded function (or template-id);
14191 we try deducing template args from each of the overloads, and if
14192 only one succeeds, we go with that. Modifies TARGS and returns
14193 true on success. */
14196 resolve_overloaded_unification (tree tparms
,
14200 unification_kind_t strict
,
14203 tree tempargs
= copy_node (targs
);
14205 tree goodfn
= NULL_TREE
;
14208 if (TREE_CODE (arg
) == ADDR_EXPR
)
14210 arg
= TREE_OPERAND (arg
, 0);
14216 if (TREE_CODE (arg
) == COMPONENT_REF
)
14217 /* Handle `&x' where `x' is some static or non-static member
14219 arg
= TREE_OPERAND (arg
, 1);
14221 if (TREE_CODE (arg
) == OFFSET_REF
)
14222 arg
= TREE_OPERAND (arg
, 1);
14224 /* Strip baselink information. */
14225 if (BASELINK_P (arg
))
14226 arg
= BASELINK_FUNCTIONS (arg
);
14228 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
14230 /* If we got some explicit template args, we need to plug them into
14231 the affected templates before we try to unify, in case the
14232 explicit args will completely resolve the templates in question. */
14234 tree expl_subargs
= TREE_OPERAND (arg
, 1);
14235 arg
= TREE_OPERAND (arg
, 0);
14237 for (; arg
; arg
= OVL_NEXT (arg
))
14239 tree fn
= OVL_CURRENT (arg
);
14240 tree subargs
, elem
;
14242 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
14245 ++processing_template_decl
;
14246 subargs
= get_bindings (fn
, DECL_TEMPLATE_RESULT (fn
),
14247 expl_subargs
, /*check_ret=*/false);
14250 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
14251 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
14252 elem
, strict
, sub_strict
, addr_p
)
14253 && (!goodfn
|| !decls_match (goodfn
, elem
)))
14259 --processing_template_decl
;
14262 else if (TREE_CODE (arg
) != OVERLOAD
14263 && TREE_CODE (arg
) != FUNCTION_DECL
)
14264 /* If ARG is, for example, "(0, &f)" then its type will be unknown
14265 -- but the deduction does not succeed because the expression is
14266 not just the function on its own. */
14269 for (; arg
; arg
= OVL_NEXT (arg
))
14270 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
14271 TREE_TYPE (OVL_CURRENT (arg
)),
14272 strict
, sub_strict
, addr_p
)
14273 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
14275 goodfn
= OVL_CURRENT (arg
);
14279 /* [temp.deduct.type] A template-argument can be deduced from a pointer
14280 to function or pointer to member function argument if the set of
14281 overloaded functions does not contain function templates and at most
14282 one of a set of overloaded functions provides a unique match.
14284 So if we found multiple possibilities, we return success but don't
14285 deduce anything. */
14289 int i
= TREE_VEC_LENGTH (targs
);
14291 if (TREE_VEC_ELT (tempargs
, i
))
14292 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
14300 /* Core DR 115: In contexts where deduction is done and fails, or in
14301 contexts where deduction is not done, if a template argument list is
14302 specified and it, along with any default template arguments, identifies
14303 a single function template specialization, then the template-id is an
14304 lvalue for the function template specialization. */
14307 resolve_nondeduced_context (tree orig_expr
)
14309 tree expr
, offset
, baselink
;
14312 if (!type_unknown_p (orig_expr
))
14317 offset
= NULL_TREE
;
14318 baselink
= NULL_TREE
;
14320 if (TREE_CODE (expr
) == ADDR_EXPR
)
14322 expr
= TREE_OPERAND (expr
, 0);
14325 if (TREE_CODE (expr
) == OFFSET_REF
)
14328 expr
= TREE_OPERAND (expr
, 1);
14330 if (TREE_CODE (expr
) == BASELINK
)
14333 expr
= BASELINK_FUNCTIONS (expr
);
14336 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
14339 tree goodfn
= NULL_TREE
;
14341 /* If we got some explicit template args, we need to plug them into
14342 the affected templates before we try to unify, in case the
14343 explicit args will completely resolve the templates in question. */
14345 tree expl_subargs
= TREE_OPERAND (expr
, 1);
14346 tree arg
= TREE_OPERAND (expr
, 0);
14347 tree badfn
= NULL_TREE
;
14348 tree badargs
= NULL_TREE
;
14350 for (; arg
; arg
= OVL_NEXT (arg
))
14352 tree fn
= OVL_CURRENT (arg
);
14353 tree subargs
, elem
;
14355 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
14358 ++processing_template_decl
;
14359 subargs
= get_bindings (fn
, DECL_TEMPLATE_RESULT (fn
),
14360 expl_subargs
, /*check_ret=*/false);
14361 if (subargs
&& !any_dependent_template_arguments_p (subargs
))
14363 elem
= instantiate_template (fn
, subargs
, tf_none
);
14364 if (elem
== error_mark_node
)
14369 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
14375 --processing_template_decl
;
14381 expr
= build_baselink (BASELINK_BINFO (baselink
),
14382 BASELINK_ACCESS_BINFO (baselink
),
14383 expr
, BASELINK_OPTYPE (baselink
));
14387 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
14388 expr
= build_offset_ref (base
, expr
, addr
);
14391 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
14394 else if (good
== 0 && badargs
)
14395 /* There were no good options and at least one bad one, so let the
14396 user know what the problem is. */
14397 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
14402 /* Subroutine of resolve_overloaded_unification; does deduction for a single
14403 overload. Fills TARGS with any deduced arguments, or error_mark_node if
14404 different overloads deduce different arguments for a given parm.
14405 ADDR_P is true if the expression for which deduction is being
14406 performed was of the form "& fn" rather than simply "fn".
14408 Returns 1 on success. */
14411 try_one_overload (tree tparms
,
14416 unification_kind_t strict
,
14424 /* [temp.deduct.type] A template-argument can be deduced from a pointer
14425 to function or pointer to member function argument if the set of
14426 overloaded functions does not contain function templates and at most
14427 one of a set of overloaded functions provides a unique match.
14429 So if this is a template, just return success. */
14431 if (uses_template_parms (arg
))
14434 if (TREE_CODE (arg
) == METHOD_TYPE
)
14435 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
14437 arg
= build_pointer_type (arg
);
14439 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
14441 /* We don't copy orig_targs for this because if we have already deduced
14442 some template args from previous args, unify would complain when we
14443 try to deduce a template parameter for the same argument, even though
14444 there isn't really a conflict. */
14445 nargs
= TREE_VEC_LENGTH (targs
);
14446 tempargs
= make_tree_vec (nargs
);
14448 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
) != 0)
14451 /* First make sure we didn't deduce anything that conflicts with
14452 explicitly specified args. */
14453 for (i
= nargs
; i
--; )
14455 tree elt
= TREE_VEC_ELT (tempargs
, i
);
14456 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
14460 else if (uses_template_parms (elt
))
14461 /* Since we're unifying against ourselves, we will fill in
14462 template args used in the function parm list with our own
14463 template parms. Discard them. */
14464 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
14465 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
14469 for (i
= nargs
; i
--; )
14471 tree elt
= TREE_VEC_ELT (tempargs
, i
);
14474 TREE_VEC_ELT (targs
, i
) = elt
;
14480 /* PARM is a template class (perhaps with unbound template
14481 parameters). ARG is a fully instantiated type. If ARG can be
14482 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
14483 TARGS are as for unify. */
14486 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
)
14488 tree copy_of_targs
;
14490 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
14491 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
14492 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
14495 /* We need to make a new template argument vector for the call to
14496 unify. If we used TARGS, we'd clutter it up with the result of
14497 the attempted unification, even if this class didn't work out.
14498 We also don't want to commit ourselves to all the unifications
14499 we've already done, since unification is supposed to be done on
14500 an argument-by-argument basis. In other words, consider the
14501 following pathological case:
14503 template <int I, int J, int K>
14506 template <int I, int J>
14507 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
14509 template <int I, int J, int K>
14510 void f(S<I, J, K>, S<I, I, I>);
14519 Now, by the time we consider the unification involving `s2', we
14520 already know that we must have `f<0, 0, 0>'. But, even though
14521 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
14522 because there are two ways to unify base classes of S<0, 1, 2>
14523 with S<I, I, I>. If we kept the already deduced knowledge, we
14524 would reject the possibility I=1. */
14525 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
14527 /* If unification failed, we're done. */
14528 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
14529 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
))
14535 /* Given a template type PARM and a class type ARG, find the unique
14536 base type in ARG that is an instance of PARM. We do not examine
14537 ARG itself; only its base-classes. If there is not exactly one
14538 appropriate base class, return NULL_TREE. PARM may be the type of
14539 a partial specialization, as well as a plain template type. Used
14543 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
)
14545 tree rval
= NULL_TREE
;
14548 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
14550 binfo
= TYPE_BINFO (complete_type (arg
));
14552 /* The type could not be completed. */
14555 /* Walk in inheritance graph order. The search order is not
14556 important, and this avoids multiple walks of virtual bases. */
14557 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
14559 tree r
= try_class_unification (tparms
, targs
, parm
, BINFO_TYPE (binfo
));
14563 /* If there is more than one satisfactory baseclass, then:
14567 If they yield more than one possible deduced A, the type
14571 if (rval
&& !same_type_p (r
, rval
))
14581 /* Returns the level of DECL, which declares a template parameter. */
14584 template_decl_level (tree decl
)
14586 switch (TREE_CODE (decl
))
14589 case TEMPLATE_DECL
:
14590 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
14593 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
14596 gcc_unreachable ();
14601 /* Decide whether ARG can be unified with PARM, considering only the
14602 cv-qualifiers of each type, given STRICT as documented for unify.
14603 Returns nonzero iff the unification is OK on that basis. */
14606 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
14608 int arg_quals
= cp_type_quals (arg
);
14609 int parm_quals
= cp_type_quals (parm
);
14611 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
14612 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
14614 /* Although a CVR qualifier is ignored when being applied to a
14615 substituted template parameter ([8.3.2]/1 for example), that
14616 does not allow us to unify "const T" with "int&" because both
14617 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
14618 It is ok when we're allowing additional CV qualifiers
14619 at the outer level [14.8.2.1]/3,1st bullet. */
14620 if ((TREE_CODE (arg
) == REFERENCE_TYPE
14621 || TREE_CODE (arg
) == FUNCTION_TYPE
14622 || TREE_CODE (arg
) == METHOD_TYPE
)
14623 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
14626 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
14627 && (parm_quals
& TYPE_QUAL_RESTRICT
))
14631 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
14632 && (arg_quals
& parm_quals
) != parm_quals
)
14635 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
14636 && (parm_quals
& arg_quals
) != arg_quals
)
14642 /* Determines the LEVEL and INDEX for the template parameter PARM. */
14644 template_parm_level_and_index (tree parm
, int* level
, int* index
)
14646 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
14647 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
14648 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
14650 *index
= TEMPLATE_TYPE_IDX (parm
);
14651 *level
= TEMPLATE_TYPE_LEVEL (parm
);
14655 *index
= TEMPLATE_PARM_IDX (parm
);
14656 *level
= TEMPLATE_PARM_LEVEL (parm
);
14660 /* Unifies the remaining arguments in PACKED_ARGS with the pack
14661 expansion at the end of PACKED_PARMS. Returns 0 if the type
14662 deduction succeeds, 1 otherwise. STRICT is the same as in
14663 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
14664 call argument list. We'll need to adjust the arguments to make them
14665 types. SUBR tells us if this is from a recursive call to
14666 type_unification_real. */
14668 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
14669 tree packed_args
, int strict
, bool call_args_p
,
14673 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
14674 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
14675 tree pack
, packs
= NULL_TREE
;
14676 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
14677 int len
= TREE_VEC_LENGTH (packed_args
);
14679 /* Determine the parameter packs we will be deducing from the
14680 pattern, and record their current deductions. */
14681 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
14682 pack
; pack
= TREE_CHAIN (pack
))
14684 tree parm_pack
= TREE_VALUE (pack
);
14687 /* Determine the index and level of this parameter pack. */
14688 template_parm_level_and_index (parm_pack
, &level
, &idx
);
14690 /* Keep track of the parameter packs and their corresponding
14692 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
14693 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
14696 /* Loop through all of the arguments that have not yet been
14697 unified and unify each with the pattern. */
14698 for (i
= start
; i
< len
; i
++)
14700 tree parm
= pattern
;
14702 /* For each parameter pack, clear out the deduced value so that
14703 we can deduce it again. */
14704 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
14707 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
14709 TMPL_ARG (targs
, level
, idx
) = NULL_TREE
;
14712 /* Unify the pattern with the current argument. */
14714 tree arg
= TREE_VEC_ELT (packed_args
, i
);
14715 tree arg_expr
= NULL_TREE
;
14716 int arg_strict
= strict
;
14717 bool skip_arg_p
= false;
14723 /* This mirrors what we do in type_unification_real. */
14727 sub_strict
= (UNIFY_ALLOW_OUTER_LEVEL
14728 | UNIFY_ALLOW_MORE_CV_QUAL
14729 | UNIFY_ALLOW_DERIVED
);
14733 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
14737 sub_strict
= UNIFY_ALLOW_NONE
;
14741 gcc_unreachable ();
14746 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
14747 if (type_unknown_p (arg
))
14749 /* [temp.deduct.type] A template-argument can be
14750 deduced from a pointer to function or pointer
14751 to member function argument if the set of
14752 overloaded functions does not contain function
14753 templates and at most one of a set of
14754 overloaded functions provides a unique
14757 if (resolve_overloaded_unification
14758 (tparms
, targs
, parm
, arg
,
14759 (unification_kind_t
) strict
,
14769 arg
= unlowered_expr_type (arg
);
14770 if (arg
== error_mark_node
)
14775 arg_strict
= sub_strict
;
14779 maybe_adjust_types_for_deduction ((unification_kind_t
) strict
,
14780 &parm
, &arg
, arg_expr
);
14785 /* For deduction from an init-list we need the actual list. */
14786 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
14788 if (unify (tparms
, targs
, parm
, arg
, arg_strict
))
14793 /* For each parameter pack, collect the deduced value. */
14794 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
14797 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
14799 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
14800 TMPL_ARG (targs
, level
, idx
);
14804 /* Verify that the results of unification with the parameter packs
14805 produce results consistent with what we've seen before, and make
14806 the deduced argument packs available. */
14807 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
14809 tree old_pack
= TREE_VALUE (pack
);
14810 tree new_args
= TREE_TYPE (pack
);
14811 int i
, len
= TREE_VEC_LENGTH (new_args
);
14813 bool nondeduced_p
= false;
14815 /* By default keep the original deduced argument pack.
14816 If necessary, more specific code is going to update the
14817 resulting deduced argument later down in this function. */
14818 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
14819 TMPL_ARG (targs
, level
, idx
) = old_pack
;
14821 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
14822 actually deduce anything. */
14823 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
14824 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
14825 nondeduced_p
= true;
14829 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
14831 /* Prepend the explicit arguments onto NEW_ARGS. */
14832 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
14833 tree old_args
= new_args
;
14834 int i
, explicit_len
= TREE_VEC_LENGTH (explicit_args
);
14835 int len
= explicit_len
+ TREE_VEC_LENGTH (old_args
);
14837 /* Copy the explicit arguments. */
14838 new_args
= make_tree_vec (len
);
14839 for (i
= 0; i
< explicit_len
; i
++)
14840 TREE_VEC_ELT (new_args
, i
) = TREE_VEC_ELT (explicit_args
, i
);
14842 /* Copy the deduced arguments. */
14843 for (; i
< len
; i
++)
14844 TREE_VEC_ELT (new_args
, i
) =
14845 TREE_VEC_ELT (old_args
, i
- explicit_len
);
14851 /* Build the deduced *_ARGUMENT_PACK. */
14852 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
14854 result
= make_node (NONTYPE_ARGUMENT_PACK
);
14855 TREE_TYPE (result
) =
14856 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
14857 TREE_CONSTANT (result
) = 1;
14860 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
14862 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
14864 /* Note the deduced argument packs for this parameter
14866 TMPL_ARG (targs
, level
, idx
) = result
;
14868 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
14869 && (ARGUMENT_PACK_ARGS (old_pack
)
14870 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
14872 /* We only had the explicitly-provided arguments before, but
14873 now we have a complete set of arguments. */
14874 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
14876 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
14877 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
14878 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
14880 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack
),
14882 /* Inconsistent unification of this parameter pack. */
14889 /* Deduce the value of template parameters. TPARMS is the (innermost)
14890 set of template parameters to a template. TARGS is the bindings
14891 for those template parameters, as determined thus far; TARGS may
14892 include template arguments for outer levels of template parameters
14893 as well. PARM is a parameter to a template function, or a
14894 subcomponent of that parameter; ARG is the corresponding argument.
14895 This function attempts to match PARM with ARG in a manner
14896 consistent with the existing assignments in TARGS. If more values
14897 are deduced, then TARGS is updated.
14899 Returns 0 if the type deduction succeeds, 1 otherwise. The
14900 parameter STRICT is a bitwise or of the following flags:
14903 Require an exact match between PARM and ARG.
14904 UNIFY_ALLOW_MORE_CV_QUAL:
14905 Allow the deduced ARG to be more cv-qualified (by qualification
14906 conversion) than ARG.
14907 UNIFY_ALLOW_LESS_CV_QUAL:
14908 Allow the deduced ARG to be less cv-qualified than ARG.
14909 UNIFY_ALLOW_DERIVED:
14910 Allow the deduced ARG to be a template base class of ARG,
14911 or a pointer to a template base class of the type pointed to by
14913 UNIFY_ALLOW_INTEGER:
14914 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
14915 case for more information.
14916 UNIFY_ALLOW_OUTER_LEVEL:
14917 This is the outermost level of a deduction. Used to determine validity
14918 of qualification conversions. A valid qualification conversion must
14919 have const qualified pointers leading up to the inner type which
14920 requires additional CV quals, except at the outer level, where const
14921 is not required [conv.qual]. It would be normal to set this flag in
14922 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
14923 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
14924 This is the outermost level of a deduction, and PARM can be more CV
14925 qualified at this point.
14926 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
14927 This is the outermost level of a deduction, and PARM can be less CV
14928 qualified at this point. */
14931 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
)
14936 int strict_in
= strict
;
14938 /* I don't think this will do the right thing with respect to types.
14939 But the only case I've seen it in so far has been array bounds, where
14940 signedness is the only information lost, and I think that will be
14942 while (TREE_CODE (parm
) == NOP_EXPR
)
14943 parm
= TREE_OPERAND (parm
, 0);
14945 if (arg
== error_mark_node
)
14947 if (arg
== unknown_type_node
14948 || arg
== init_list_type_node
)
14949 /* We can't deduce anything from this, but we might get all the
14950 template args from other function args. */
14953 /* If PARM uses template parameters, then we can't bail out here,
14954 even if ARG == PARM, since we won't record unifications for the
14955 template parameters. We might need them if we're trying to
14956 figure out which of two things is more specialized. */
14957 if (arg
== parm
&& !uses_template_parms (parm
))
14960 /* Handle init lists early, so the rest of the function can assume
14961 we're dealing with a type. */
14962 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
14966 tree orig_parm
= parm
;
14968 /* Replace T with std::initializer_list<T> for deduction. */
14969 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
14970 && flag_deduce_init_list
)
14971 parm
= listify (parm
);
14973 if (!is_std_init_list (parm
))
14974 /* We can only deduce from an initializer list argument if the
14975 parameter is std::initializer_list; otherwise this is a
14976 non-deduced context. */
14979 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
14981 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
14983 int elt_strict
= strict
;
14985 if (elt
== error_mark_node
)
14988 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
14990 tree type
= TREE_TYPE (elt
);
14991 /* It should only be possible to get here for a call. */
14992 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
14993 elt_strict
|= maybe_adjust_types_for_deduction
14994 (DEDUCE_CALL
, &elttype
, &type
, elt
);
14998 if (unify (tparms
, targs
, elttype
, elt
, elt_strict
))
15002 /* If the std::initializer_list<T> deduction worked, replace the
15003 deduced A with std::initializer_list<A>. */
15004 if (orig_parm
!= parm
)
15006 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
15007 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
15008 targ
= listify (targ
);
15009 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
15014 /* Immediately reject some pairs that won't unify because of
15015 cv-qualification mismatches. */
15016 if (TREE_CODE (arg
) == TREE_CODE (parm
)
15018 /* It is the elements of the array which hold the cv quals of an array
15019 type, and the elements might be template type parms. We'll check
15020 when we recurse. */
15021 && TREE_CODE (arg
) != ARRAY_TYPE
15022 /* We check the cv-qualifiers when unifying with template type
15023 parameters below. We want to allow ARG `const T' to unify with
15024 PARM `T' for example, when computing which of two templates
15025 is more specialized, for example. */
15026 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
15027 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
15030 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
15031 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
15032 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
15033 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
15034 strict
&= ~UNIFY_ALLOW_DERIVED
;
15035 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
15036 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
15038 switch (TREE_CODE (parm
))
15040 case TYPENAME_TYPE
:
15042 case UNBOUND_CLASS_TEMPLATE
:
15043 /* In a type which contains a nested-name-specifier, template
15044 argument values cannot be deduced for template parameters used
15045 within the nested-name-specifier. */
15048 case TEMPLATE_TYPE_PARM
:
15049 case TEMPLATE_TEMPLATE_PARM
:
15050 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15051 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
15052 if (tparm
== error_mark_node
)
15055 if (TEMPLATE_TYPE_LEVEL (parm
)
15056 != template_decl_level (tparm
))
15057 /* The PARM is not one we're trying to unify. Just check
15058 to see if it matches ARG. */
15059 return (TREE_CODE (arg
) == TREE_CODE (parm
)
15060 && same_type_p (parm
, arg
)) ? 0 : 1;
15061 idx
= TEMPLATE_TYPE_IDX (parm
);
15062 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
15063 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
15065 /* Check for mixed types and values. */
15066 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
15067 && TREE_CODE (tparm
) != TYPE_DECL
)
15068 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
15069 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
15072 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
15074 /* ARG must be constructed from a template class or a template
15075 template parameter. */
15076 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
15077 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
15081 tree parmvec
= TYPE_TI_ARGS (parm
);
15082 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
15083 tree full_argvec
= add_to_template_args (targs
, argvec
);
15085 = DECL_INNERMOST_TEMPLATE_PARMS
15086 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
15088 int parm_variadic_p
= 0;
15090 /* The resolution to DR150 makes clear that default
15091 arguments for an N-argument may not be used to bind T
15092 to a template template parameter with fewer than N
15093 parameters. It is not safe to permit the binding of
15094 default arguments as an extension, as that may change
15095 the meaning of a conforming program. Consider:
15097 struct Dense { static const unsigned int dim = 1; };
15099 template <template <typename> class View,
15101 void operator+(float, View<Block> const&);
15103 template <typename Block,
15104 unsigned int Dim = Block::dim>
15105 struct Lvalue_proxy { operator float() const; };
15109 Lvalue_proxy<Dense> p;
15114 Here, if Lvalue_proxy is permitted to bind to View, then
15115 the global operator+ will be used; if they are not, the
15116 Lvalue_proxy will be converted to float. */
15117 if (coerce_template_parms (parm_parms
,
15119 TYPE_TI_TEMPLATE (parm
),
15121 /*require_all_args=*/true,
15122 /*use_default_args=*/false)
15123 == error_mark_node
)
15126 /* Deduce arguments T, i from TT<T> or TT<i>.
15127 We check each element of PARMVEC and ARGVEC individually
15128 rather than the whole TREE_VEC since they can have
15129 different number of elements. */
15131 parmvec
= expand_template_argument_pack (parmvec
);
15132 argvec
= expand_template_argument_pack (argvec
);
15134 len
= TREE_VEC_LENGTH (parmvec
);
15136 /* Check if the parameters end in a pack, making them
15139 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
15140 parm_variadic_p
= 1;
15142 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
15145 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
15147 if (unify (tparms
, targs
,
15148 TREE_VEC_ELT (parmvec
, i
),
15149 TREE_VEC_ELT (argvec
, i
),
15154 if (parm_variadic_p
15155 && unify_pack_expansion (tparms
, targs
,
15158 /*call_args_p=*/false,
15162 arg
= TYPE_TI_TEMPLATE (arg
);
15164 /* Fall through to deduce template name. */
15167 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
15168 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
15170 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
15172 /* Simple cases: Value already set, does match or doesn't. */
15173 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
15180 /* If PARM is `const T' and ARG is only `int', we don't have
15181 a match unless we are allowing additional qualification.
15182 If ARG is `const int' and PARM is just `T' that's OK;
15183 that binds `const int' to `T'. */
15184 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
15188 /* Consider the case where ARG is `const volatile int' and
15189 PARM is `const T'. Then, T should be `volatile int'. */
15190 arg
= cp_build_qualified_type_real
15191 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
15192 if (arg
== error_mark_node
)
15195 /* Simple cases: Value already set, does match or doesn't. */
15196 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
15201 /* Make sure that ARG is not a variable-sized array. (Note
15202 that were talking about variable-sized arrays (like
15203 `int[n]'), rather than arrays of unknown size (like
15204 `int[]').) We'll get very confused by such a type since
15205 the bound of the array will not be computable in an
15206 instantiation. Besides, such types are not allowed in
15207 ISO C++, so we can do as we please here. */
15208 if (variably_modified_type_p (arg
, NULL_TREE
))
15211 /* Strip typedefs as in convert_template_argument. */
15212 arg
= strip_typedefs (arg
);
15215 /* If ARG is a parameter pack or an expansion, we cannot unify
15216 against it unless PARM is also a parameter pack. */
15217 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
15218 && !template_parameter_pack_p (parm
))
15221 /* If the argument deduction results is a METHOD_TYPE,
15222 then there is a problem.
15223 METHOD_TYPE doesn't map to any real C++ type the result of
15224 the deduction can not be of that type. */
15225 if (TREE_CODE (arg
) == METHOD_TYPE
)
15228 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
15231 case TEMPLATE_PARM_INDEX
:
15232 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
15233 if (tparm
== error_mark_node
)
15236 if (TEMPLATE_PARM_LEVEL (parm
)
15237 != template_decl_level (tparm
))
15238 /* The PARM is not one we're trying to unify. Just check
15239 to see if it matches ARG. */
15240 return !(TREE_CODE (arg
) == TREE_CODE (parm
)
15241 && cp_tree_equal (parm
, arg
));
15243 idx
= TEMPLATE_PARM_IDX (parm
);
15244 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
15247 return !cp_tree_equal (targ
, arg
);
15249 /* [temp.deduct.type] If, in the declaration of a function template
15250 with a non-type template-parameter, the non-type
15251 template-parameter is used in an expression in the function
15252 parameter-list and, if the corresponding template-argument is
15253 deduced, the template-argument type shall match the type of the
15254 template-parameter exactly, except that a template-argument
15255 deduced from an array bound may be of any integral type.
15256 The non-type parameter might use already deduced type parameters. */
15257 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
15258 if (!TREE_TYPE (arg
))
15259 /* Template-parameter dependent expression. Just accept it for now.
15260 It will later be processed in convert_template_argument. */
15262 else if (same_type_p (TREE_TYPE (arg
), tparm
))
15264 else if ((strict
& UNIFY_ALLOW_INTEGER
)
15265 && (TREE_CODE (tparm
) == INTEGER_TYPE
15266 || TREE_CODE (tparm
) == BOOLEAN_TYPE
))
15267 /* Convert the ARG to the type of PARM; the deduced non-type
15268 template argument must exactly match the types of the
15269 corresponding parameter. */
15270 arg
= fold (build_nop (tparm
, arg
));
15271 else if (uses_template_parms (tparm
))
15272 /* We haven't deduced the type of this parameter yet. Try again
15278 /* If ARG is a parameter pack or an expansion, we cannot unify
15279 against it unless PARM is also a parameter pack. */
15280 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
15281 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
15284 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
15289 /* A pointer-to-member constant can be unified only with
15290 another constant. */
15291 if (TREE_CODE (arg
) != PTRMEM_CST
)
15294 /* Just unify the class member. It would be useless (and possibly
15295 wrong, depending on the strict flags) to unify also
15296 PTRMEM_CST_CLASS, because we want to be sure that both parm and
15297 arg refer to the same variable, even if through different
15298 classes. For instance:
15300 struct A { int x; };
15303 Unification of &A::x and &B::x must succeed. */
15304 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
15305 PTRMEM_CST_MEMBER (arg
), strict
);
15310 if (TREE_CODE (arg
) != POINTER_TYPE
)
15313 /* [temp.deduct.call]
15315 A can be another pointer or pointer to member type that can
15316 be converted to the deduced A via a qualification
15317 conversion (_conv.qual_).
15319 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
15320 This will allow for additional cv-qualification of the
15321 pointed-to types if appropriate. */
15323 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
15324 /* The derived-to-base conversion only persists through one
15325 level of pointers. */
15326 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
15328 return unify (tparms
, targs
, TREE_TYPE (parm
),
15329 TREE_TYPE (arg
), strict
);
15332 case REFERENCE_TYPE
:
15333 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
15335 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
15336 strict
& UNIFY_ALLOW_MORE_CV_QUAL
);
15339 if (TREE_CODE (arg
) != ARRAY_TYPE
)
15341 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
15342 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
15344 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
15351 /* Our representation of array types uses "N - 1" as the
15352 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
15353 not an integer constant. We cannot unify arbitrarily
15354 complex expressions, so we eliminate the MINUS_EXPRs
15356 parm_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (parm
));
15357 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
15360 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
15361 parm_max
= TREE_OPERAND (parm_max
, 0);
15363 arg_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (arg
));
15364 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
15367 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
15368 trying to unify the type of a variable with the type
15369 of a template parameter. For example:
15371 template <unsigned int N>
15372 void f (char (&) [N]);
15379 Here, the type of the ARG will be "int [g(i)]", and
15380 may be a SAVE_EXPR, etc. */
15381 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
15383 arg_max
= TREE_OPERAND (arg_max
, 0);
15386 /* If only one of the bounds used a MINUS_EXPR, compensate
15387 by adding one to the other bound. */
15388 if (parm_cst
&& !arg_cst
)
15389 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
15393 else if (arg_cst
&& !parm_cst
)
15394 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
15399 if (unify (tparms
, targs
, parm_max
, arg_max
, UNIFY_ALLOW_INTEGER
))
15402 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
15403 strict
& UNIFY_ALLOW_MORE_CV_QUAL
);
15410 case ENUMERAL_TYPE
:
15412 if (TREE_CODE (arg
) != TREE_CODE (parm
))
15415 /* We have already checked cv-qualification at the top of the
15417 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
15420 /* As far as unification is concerned, this wins. Later checks
15421 will invalidate it if necessary. */
15424 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
15425 /* Type INTEGER_CST can come from ordinary constant template args. */
15427 while (TREE_CODE (arg
) == NOP_EXPR
)
15428 arg
= TREE_OPERAND (arg
, 0);
15430 if (TREE_CODE (arg
) != INTEGER_CST
)
15432 return !tree_int_cst_equal (parm
, arg
);
15437 if (TREE_CODE (arg
) != TREE_VEC
)
15439 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
15441 for (i
= 0; i
< TREE_VEC_LENGTH (parm
); ++i
)
15442 if (unify (tparms
, targs
,
15443 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
15451 if (TREE_CODE (arg
) != TREE_CODE (parm
))
15454 if (TYPE_PTRMEMFUNC_P (parm
))
15456 if (!TYPE_PTRMEMFUNC_P (arg
))
15459 return unify (tparms
, targs
,
15460 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
15461 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
15465 if (CLASSTYPE_TEMPLATE_INFO (parm
))
15467 tree t
= NULL_TREE
;
15469 if (strict_in
& UNIFY_ALLOW_DERIVED
)
15471 /* First, we try to unify the PARM and ARG directly. */
15472 t
= try_class_unification (tparms
, targs
,
15477 /* Fallback to the special case allowed in
15478 [temp.deduct.call]:
15480 If P is a class, and P has the form
15481 template-id, then A can be a derived class of
15482 the deduced A. Likewise, if P is a pointer to
15483 a class of the form template-id, A can be a
15484 pointer to a derived class pointed to by the
15486 t
= get_template_base (tparms
, targs
, parm
, arg
);
15492 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
15493 && (CLASSTYPE_TI_TEMPLATE (parm
)
15494 == CLASSTYPE_TI_TEMPLATE (arg
)))
15495 /* Perhaps PARM is something like S<U> and ARG is S<int>.
15496 Then, we should unify `int' and `U'. */
15499 /* There's no chance of unification succeeding. */
15502 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
15503 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
);
15505 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
15510 case FUNCTION_TYPE
:
15512 unsigned int nargs
;
15517 if (TREE_CODE (arg
) != TREE_CODE (parm
))
15520 /* CV qualifications for methods can never be deduced, they must
15521 match exactly. We need to check them explicitly here,
15522 because type_unification_real treats them as any other
15523 cv-qualified parameter. */
15524 if (TREE_CODE (parm
) == METHOD_TYPE
15525 && (!check_cv_quals_for_unify
15527 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg
))),
15528 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm
))))))
15531 if (unify (tparms
, targs
, TREE_TYPE (parm
),
15532 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
))
15535 nargs
= list_length (TYPE_ARG_TYPES (arg
));
15536 args
= XALLOCAVEC (tree
, nargs
);
15537 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
15538 a
!= NULL_TREE
&& a
!= void_list_node
;
15539 a
= TREE_CHAIN (a
), ++i
)
15540 args
[i
] = TREE_VALUE (a
);
15543 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
15544 args
, nargs
, 1, DEDUCE_EXACT
,
15549 /* Unify a pointer to member with a pointer to member function, which
15550 deduces the type of the member as a function type. */
15551 if (TYPE_PTRMEMFUNC_P (arg
))
15556 /* Check top-level cv qualifiers */
15557 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
15560 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
15561 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
), UNIFY_ALLOW_NONE
))
15564 /* Determine the type of the function we are unifying against. */
15565 method_type
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg
));
15567 build_function_type (TREE_TYPE (method_type
),
15568 TREE_CHAIN (TYPE_ARG_TYPES (method_type
)));
15570 /* Extract the cv-qualifiers of the member function from the
15571 implicit object parameter and place them on the function
15572 type to be restored later. */
15573 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (method_type
));
15574 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
);
15577 if (TREE_CODE (arg
) != OFFSET_TYPE
)
15579 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
15580 TYPE_OFFSET_BASETYPE (arg
), UNIFY_ALLOW_NONE
))
15582 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
15586 if (DECL_TEMPLATE_PARM_P (parm
))
15587 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
);
15588 if (arg
!= integral_constant_value (parm
))
15593 case TEMPLATE_DECL
:
15594 /* Matched cases are handled by the ARG == PARM test above. */
15598 /* A non-type template parameter that is a variable should be a
15599 an integral constant, in which case, it whould have been
15600 folded into its (constant) value. So we should not be getting
15601 a variable here. */
15602 gcc_unreachable ();
15604 case TYPE_ARGUMENT_PACK
:
15605 case NONTYPE_ARGUMENT_PACK
:
15607 tree packed_parms
= ARGUMENT_PACK_ARGS (parm
);
15608 tree packed_args
= ARGUMENT_PACK_ARGS (arg
);
15609 int i
, len
= TREE_VEC_LENGTH (packed_parms
);
15610 int argslen
= TREE_VEC_LENGTH (packed_args
);
15611 int parm_variadic_p
= 0;
15613 for (i
= 0; i
< len
; ++i
)
15615 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms
, i
)))
15618 /* We can unify against something with a trailing
15620 parm_variadic_p
= 1;
15622 /* Since there is something following the pack
15623 expansion, we cannot unify this template argument
15630 /* If we don't have enough arguments to satisfy the parameters
15631 (not counting the pack expression at the end), or we have
15632 too many arguments for a parameter list that doesn't end in
15633 a pack expression, we can't unify. */
15634 if (argslen
< (len
- parm_variadic_p
)
15635 || (argslen
> len
&& !parm_variadic_p
))
15638 /* Unify all of the parameters that precede the (optional)
15639 pack expression. */
15640 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
15642 if (unify (tparms
, targs
, TREE_VEC_ELT (packed_parms
, i
),
15643 TREE_VEC_ELT (packed_args
, i
), strict
))
15647 if (parm_variadic_p
)
15648 return unify_pack_expansion (tparms
, targs
,
15649 packed_parms
, packed_args
,
15650 strict
, /*call_args_p=*/false,
15658 case DECLTYPE_TYPE
:
15659 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
15664 /* Unification fails if we hit an error node. */
15668 gcc_assert (EXPR_P (parm
));
15670 /* We must be looking at an expression. This can happen with
15674 void foo(S<I>, S<I + 2>);
15676 This is a "nondeduced context":
15680 The nondeduced contexts are:
15682 --A type that is a template-id in which one or more of
15683 the template-arguments is an expression that references
15684 a template-parameter.
15686 In these cases, we assume deduction succeeded, but don't
15687 actually infer any unifications. */
15689 if (!uses_template_parms (parm
)
15690 && !template_args_equal (parm
, arg
))
15697 /* Note that DECL can be defined in this translation unit, if
15701 mark_definable (tree decl
)
15704 DECL_NOT_REALLY_EXTERN (decl
) = 1;
15705 FOR_EACH_CLONE (clone
, decl
)
15706 DECL_NOT_REALLY_EXTERN (clone
) = 1;
15709 /* Called if RESULT is explicitly instantiated, or is a member of an
15710 explicitly instantiated class. */
15713 mark_decl_instantiated (tree result
, int extern_p
)
15715 SET_DECL_EXPLICIT_INSTANTIATION (result
);
15717 /* If this entity has already been written out, it's too late to
15718 make any modifications. */
15719 if (TREE_ASM_WRITTEN (result
))
15722 if (TREE_CODE (result
) != FUNCTION_DECL
)
15723 /* The TREE_PUBLIC flag for function declarations will have been
15724 set correctly by tsubst. */
15725 TREE_PUBLIC (result
) = 1;
15727 /* This might have been set by an earlier implicit instantiation. */
15728 DECL_COMDAT (result
) = 0;
15731 DECL_NOT_REALLY_EXTERN (result
) = 0;
15734 mark_definable (result
);
15735 /* Always make artificials weak. */
15736 if (DECL_ARTIFICIAL (result
) && flag_weak
)
15737 comdat_linkage (result
);
15738 /* For WIN32 we also want to put explicit instantiations in
15739 linkonce sections. */
15740 else if (TREE_PUBLIC (result
))
15741 maybe_make_one_only (result
);
15744 /* If EXTERN_P, then this function will not be emitted -- unless
15745 followed by an explicit instantiation, at which point its linkage
15746 will be adjusted. If !EXTERN_P, then this function will be
15747 emitted here. In neither circumstance do we want
15748 import_export_decl to adjust the linkage. */
15749 DECL_INTERFACE_KNOWN (result
) = 1;
15752 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
15753 important template arguments. If any are missing, we check whether
15754 they're important by using error_mark_node for substituting into any
15755 args that were used for partial ordering (the ones between ARGS and END)
15756 and seeing if it bubbles up. */
15759 check_undeduced_parms (tree targs
, tree args
, tree end
)
15761 bool found
= false;
15763 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
15764 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
15767 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
15771 for (; args
!= end
; args
= TREE_CHAIN (args
))
15773 tree substed
= tsubst (TREE_VALUE (args
), targs
, tf_none
, NULL_TREE
);
15774 if (substed
== error_mark_node
)
15781 /* Given two function templates PAT1 and PAT2, return:
15783 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
15784 -1 if PAT2 is more specialized than PAT1.
15785 0 if neither is more specialized.
15787 LEN indicates the number of parameters we should consider
15788 (defaulted parameters should not be considered).
15790 The 1998 std underspecified function template partial ordering, and
15791 DR214 addresses the issue. We take pairs of arguments, one from
15792 each of the templates, and deduce them against each other. One of
15793 the templates will be more specialized if all the *other*
15794 template's arguments deduce against its arguments and at least one
15795 of its arguments *does* *not* deduce against the other template's
15796 corresponding argument. Deduction is done as for class templates.
15797 The arguments used in deduction have reference and top level cv
15798 qualifiers removed. Iff both arguments were originally reference
15799 types *and* deduction succeeds in both directions, the template
15800 with the more cv-qualified argument wins for that pairing (if
15801 neither is more cv-qualified, they both are equal). Unlike regular
15802 deduction, after all the arguments have been deduced in this way,
15803 we do *not* verify the deduced template argument values can be
15804 substituted into non-deduced contexts.
15806 The logic can be a bit confusing here, because we look at deduce1 and
15807 targs1 to see if pat2 is at least as specialized, and vice versa; if we
15808 can find template arguments for pat1 to make arg1 look like arg2, that
15809 means that arg2 is at least as specialized as arg1. */
15812 more_specialized_fn (tree pat1
, tree pat2
, int len
)
15814 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
15815 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
15816 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
15817 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
15818 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
15819 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
15820 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
15821 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
15822 tree origs1
, origs2
;
15823 bool lose1
= false;
15824 bool lose2
= false;
15826 /* Remove the this parameter from non-static member functions. If
15827 one is a non-static member function and the other is not a static
15828 member function, remove the first parameter from that function
15829 also. This situation occurs for operator functions where we
15830 locate both a member function (with this pointer) and non-member
15831 operator (with explicit first operand). */
15832 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
15834 len
--; /* LEN is the number of significant arguments for DECL1 */
15835 args1
= TREE_CHAIN (args1
);
15836 if (!DECL_STATIC_FUNCTION_P (decl2
))
15837 args2
= TREE_CHAIN (args2
);
15839 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
15841 args2
= TREE_CHAIN (args2
);
15842 if (!DECL_STATIC_FUNCTION_P (decl1
))
15845 args1
= TREE_CHAIN (args1
);
15849 /* If only one is a conversion operator, they are unordered. */
15850 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
15853 /* Consider the return type for a conversion function */
15854 if (DECL_CONV_FN_P (decl1
))
15856 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
15857 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
15861 processing_template_decl
++;
15867 /* Stop when an ellipsis is seen. */
15868 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
15870 tree arg1
= TREE_VALUE (args1
);
15871 tree arg2
= TREE_VALUE (args2
);
15872 int deduce1
, deduce2
;
15876 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
15877 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
15879 /* When both arguments are pack expansions, we need only
15880 unify the patterns themselves. */
15881 arg1
= PACK_EXPANSION_PATTERN (arg1
);
15882 arg2
= PACK_EXPANSION_PATTERN (arg2
);
15884 /* This is the last comparison we need to do. */
15888 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
15890 arg1
= TREE_TYPE (arg1
);
15891 quals1
= cp_type_quals (arg1
);
15894 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
15896 arg2
= TREE_TYPE (arg2
);
15897 quals2
= cp_type_quals (arg2
);
15900 if ((quals1
< 0) != (quals2
< 0))
15902 /* Only of the args is a reference, see if we should apply
15903 array/function pointer decay to it. This is not part of
15904 DR214, but is, IMHO, consistent with the deduction rules
15905 for the function call itself, and with our earlier
15906 implementation of the underspecified partial ordering
15907 rules. (nathan). */
15910 switch (TREE_CODE (arg1
))
15913 arg1
= TREE_TYPE (arg1
);
15915 case FUNCTION_TYPE
:
15916 arg1
= build_pointer_type (arg1
);
15925 switch (TREE_CODE (arg2
))
15928 arg2
= TREE_TYPE (arg2
);
15930 case FUNCTION_TYPE
:
15931 arg2
= build_pointer_type (arg2
);
15940 arg1
= TYPE_MAIN_VARIANT (arg1
);
15941 arg2
= TYPE_MAIN_VARIANT (arg2
);
15943 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
15945 int i
, len2
= list_length (args2
);
15946 tree parmvec
= make_tree_vec (1);
15947 tree argvec
= make_tree_vec (len2
);
15950 /* Setup the parameter vector, which contains only ARG1. */
15951 TREE_VEC_ELT (parmvec
, 0) = arg1
;
15953 /* Setup the argument vector, which contains the remaining
15955 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
15956 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
15958 deduce1
= !unify_pack_expansion (tparms1
, targs1
, parmvec
,
15959 argvec
, UNIFY_ALLOW_NONE
,
15960 /*call_args_p=*/false,
15963 /* We cannot deduce in the other direction, because ARG1 is
15964 a pack expansion but ARG2 is not. */
15967 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
15969 int i
, len1
= list_length (args1
);
15970 tree parmvec
= make_tree_vec (1);
15971 tree argvec
= make_tree_vec (len1
);
15974 /* Setup the parameter vector, which contains only ARG1. */
15975 TREE_VEC_ELT (parmvec
, 0) = arg2
;
15977 /* Setup the argument vector, which contains the remaining
15979 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
15980 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
15982 deduce2
= !unify_pack_expansion (tparms2
, targs2
, parmvec
,
15983 argvec
, UNIFY_ALLOW_NONE
,
15984 /*call_args_p=*/false,
15987 /* We cannot deduce in the other direction, because ARG2 is
15988 a pack expansion but ARG1 is not.*/
15994 /* The normal case, where neither argument is a pack
15996 deduce1
= !unify (tparms1
, targs1
, arg1
, arg2
, UNIFY_ALLOW_NONE
);
15997 deduce2
= !unify (tparms2
, targs2
, arg2
, arg1
, UNIFY_ALLOW_NONE
);
16000 /* If we couldn't deduce arguments for tparms1 to make arg1 match
16001 arg2, then arg2 is not as specialized as arg1. */
16007 /* "If, for a given type, deduction succeeds in both directions
16008 (i.e., the types are identical after the transformations above)
16009 and if the type from the argument template is more cv-qualified
16010 than the type from the parameter template (as described above)
16011 that type is considered to be more specialized than the other. If
16012 neither type is more cv-qualified than the other then neither type
16013 is more specialized than the other." */
16015 if (deduce1
&& deduce2
16016 && quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
16018 if ((quals1
& quals2
) == quals2
)
16020 if ((quals1
& quals2
) == quals1
)
16024 if (lose1
&& lose2
)
16025 /* We've failed to deduce something in either direction.
16026 These must be unordered. */
16029 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
16030 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
16031 /* We have already processed all of the arguments in our
16032 handing of the pack expansion type. */
16035 args1
= TREE_CHAIN (args1
);
16036 args2
= TREE_CHAIN (args2
);
16039 /* "In most cases, all template parameters must have values in order for
16040 deduction to succeed, but for partial ordering purposes a template
16041 parameter may remain without a value provided it is not used in the
16042 types being used for partial ordering."
16044 Thus, if we are missing any of the targs1 we need to substitute into
16045 origs1, then pat2 is not as specialized as pat1. This can happen when
16046 there is a nondeduced context. */
16047 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
16049 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
16052 processing_template_decl
--;
16054 /* All things being equal, if the next argument is a pack expansion
16055 for one function but not for the other, prefer the
16056 non-variadic function. FIXME this is bogus; see c++/41958. */
16058 && args1
&& TREE_VALUE (args1
)
16059 && args2
&& TREE_VALUE (args2
))
16061 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
16062 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
16065 if (lose1
== lose2
)
16073 /* Determine which of two partial specializations is more specialized.
16075 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
16076 to the first partial specialization. The TREE_VALUE is the
16077 innermost set of template parameters for the partial
16078 specialization. PAT2 is similar, but for the second template.
16080 Return 1 if the first partial specialization is more specialized;
16081 -1 if the second is more specialized; 0 if neither is more
16084 See [temp.class.order] for information about determining which of
16085 two templates is more specialized. */
16088 more_specialized_class (tree pat1
, tree pat2
)
16093 bool any_deductions
= false;
16095 tmpl1
= TREE_TYPE (pat1
);
16096 tmpl2
= TREE_TYPE (pat2
);
16098 /* Just like what happens for functions, if we are ordering between
16099 different class template specializations, we may encounter dependent
16100 types in the arguments, and we need our dependency check functions
16101 to behave correctly. */
16102 ++processing_template_decl
;
16103 targs
= get_class_bindings (TREE_VALUE (pat1
),
16104 CLASSTYPE_TI_ARGS (tmpl1
),
16105 CLASSTYPE_TI_ARGS (tmpl2
));
16109 any_deductions
= true;
16112 targs
= get_class_bindings (TREE_VALUE (pat2
),
16113 CLASSTYPE_TI_ARGS (tmpl2
),
16114 CLASSTYPE_TI_ARGS (tmpl1
));
16118 any_deductions
= true;
16120 --processing_template_decl
;
16122 /* In the case of a tie where at least one of the class templates
16123 has a parameter pack at the end, the template with the most
16124 non-packed parameters wins. */
16127 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
16128 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
16130 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
16131 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
16132 int len1
= TREE_VEC_LENGTH (args1
);
16133 int len2
= TREE_VEC_LENGTH (args2
);
16135 /* We don't count the pack expansion at the end. */
16136 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
16138 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
16143 else if (len1
< len2
)
16150 /* Return the template arguments that will produce the function signature
16151 DECL from the function template FN, with the explicit template
16152 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
16153 also match. Return NULL_TREE if no satisfactory arguments could be
16157 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
16159 int ntparms
= DECL_NTPARMS (fn
);
16160 tree targs
= make_tree_vec (ntparms
);
16162 tree decl_arg_types
;
16164 unsigned int nargs
, ix
;
16167 /* Substitute the explicit template arguments into the type of DECL.
16168 The call to fn_type_unification will handle substitution into the
16170 decl_type
= TREE_TYPE (decl
);
16171 if (explicit_args
&& uses_template_parms (decl_type
))
16174 tree converted_args
;
16176 if (DECL_TEMPLATE_INFO (decl
))
16177 tmpl
= DECL_TI_TEMPLATE (decl
);
16179 /* We can get here for some invalid specializations. */
16183 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
16184 explicit_args
, NULL_TREE
,
16186 /*require_all_args=*/false,
16187 /*use_default_args=*/false);
16188 if (converted_args
== error_mark_node
)
16191 decl_type
= tsubst (decl_type
, converted_args
, tf_none
, NULL_TREE
);
16192 if (decl_type
== error_mark_node
)
16196 /* Never do unification on the 'this' parameter. */
16197 decl_arg_types
= skip_artificial_parms_for (decl
,
16198 TYPE_ARG_TYPES (decl_type
));
16200 nargs
= list_length (decl_arg_types
);
16201 args
= XALLOCAVEC (tree
, nargs
);
16202 for (arg
= decl_arg_types
, ix
= 0;
16203 arg
!= NULL_TREE
&& arg
!= void_list_node
;
16204 arg
= TREE_CHAIN (arg
), ++ix
)
16205 args
[ix
] = TREE_VALUE (arg
);
16207 if (fn_type_unification (fn
, explicit_args
, targs
,
16209 (check_rettype
|| DECL_CONV_FN_P (fn
)
16210 ? TREE_TYPE (decl_type
) : NULL_TREE
),
16211 DEDUCE_EXACT
, LOOKUP_NORMAL
))
16217 /* Return the innermost template arguments that, when applied to a
16218 template specialization whose innermost template parameters are
16219 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
16222 For example, suppose we have:
16224 template <class T, class U> struct S {};
16225 template <class T> struct S<T*, int> {};
16227 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
16228 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
16229 int}. The resulting vector will be {double}, indicating that `T'
16230 is bound to `double'. */
16233 get_class_bindings (tree tparms
, tree spec_args
, tree args
)
16235 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
16237 tree innermost_deduced_args
;
16239 innermost_deduced_args
= make_tree_vec (ntparms
);
16240 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
16242 deduced_args
= copy_node (args
);
16243 SET_TMPL_ARGS_LEVEL (deduced_args
,
16244 TMPL_ARGS_DEPTH (deduced_args
),
16245 innermost_deduced_args
);
16248 deduced_args
= innermost_deduced_args
;
16250 if (unify (tparms
, deduced_args
,
16251 INNERMOST_TEMPLATE_ARGS (spec_args
),
16252 INNERMOST_TEMPLATE_ARGS (args
),
16256 for (i
= 0; i
< ntparms
; ++i
)
16257 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
16260 /* Verify that nondeduced template arguments agree with the type
16261 obtained from argument deduction.
16265 struct A { typedef int X; };
16266 template <class T, class U> struct C {};
16267 template <class T> struct C<T, typename T::X> {};
16269 Then with the instantiation `C<A, int>', we can deduce that
16270 `T' is `A' but unify () does not check whether `typename T::X'
16272 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
16273 if (spec_args
== error_mark_node
16274 /* We only need to check the innermost arguments; the other
16275 arguments will always agree. */
16276 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
16277 INNERMOST_TEMPLATE_ARGS (args
)))
16280 /* Now that we have bindings for all of the template arguments,
16281 ensure that the arguments deduced for the template template
16282 parameters have compatible template parameter lists. See the use
16283 of template_template_parm_bindings_ok_p in fn_type_unification
16284 for more information. */
16285 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
16288 return deduced_args
;
16291 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
16292 Return the TREE_LIST node with the most specialized template, if
16293 any. If there is no most specialized template, the error_mark_node
16296 Note that this function does not look at, or modify, the
16297 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
16298 returned is one of the elements of INSTANTIATIONS, callers may
16299 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
16300 and retrieve it from the value returned. */
16303 most_specialized_instantiation (tree templates
)
16307 ++processing_template_decl
;
16310 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
16314 if (get_bindings (TREE_VALUE (champ
),
16315 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
16316 NULL_TREE
, /*check_ret=*/false))
16319 if (get_bindings (TREE_VALUE (fn
),
16320 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
16321 NULL_TREE
, /*check_ret=*/false))
16328 /* Equally specialized, move to next function. If there
16329 is no next function, nothing's most specialized. */
16330 fn
= TREE_CHAIN (fn
);
16338 /* Now verify that champ is better than everything earlier in the
16339 instantiation list. */
16340 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
16341 if (get_bindings (TREE_VALUE (champ
),
16342 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
16343 NULL_TREE
, /*check_ret=*/false)
16344 || !get_bindings (TREE_VALUE (fn
),
16345 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
16346 NULL_TREE
, /*check_ret=*/false))
16352 processing_template_decl
--;
16355 return error_mark_node
;
16360 /* If DECL is a specialization of some template, return the most
16361 general such template. Otherwise, returns NULL_TREE.
16363 For example, given:
16365 template <class T> struct S { template <class U> void f(U); };
16367 if TMPL is `template <class U> void S<int>::f(U)' this will return
16368 the full template. This function will not trace past partial
16369 specializations, however. For example, given in addition:
16371 template <class T> struct S<T*> { template <class U> void f(U); };
16373 if TMPL is `template <class U> void S<int*>::f(U)' this will return
16374 `template <class T> template <class U> S<T*>::f(U)'. */
16377 most_general_template (tree decl
)
16379 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
16380 an immediate specialization. */
16381 if (TREE_CODE (decl
) == FUNCTION_DECL
)
16383 if (DECL_TEMPLATE_INFO (decl
)) {
16384 decl
= DECL_TI_TEMPLATE (decl
);
16386 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
16387 template friend. */
16388 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
16394 /* Look for more and more general templates. */
16395 while (DECL_TEMPLATE_INFO (decl
))
16397 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
16398 (See cp-tree.h for details.) */
16399 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
16402 if (CLASS_TYPE_P (TREE_TYPE (decl
))
16403 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
16406 /* Stop if we run into an explicitly specialized class template. */
16407 if (!DECL_NAMESPACE_SCOPE_P (decl
)
16408 && DECL_CONTEXT (decl
)
16409 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
16412 decl
= DECL_TI_TEMPLATE (decl
);
16418 /* Return the most specialized of the class template partial
16419 specializations of TMPL which can produce TYPE, a specialization of
16420 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
16421 a _TYPE node corresponding to the partial specialization, while the
16422 TREE_PURPOSE is the set of template arguments that must be
16423 substituted into the TREE_TYPE in order to generate TYPE.
16425 If the choice of partial specialization is ambiguous, a diagnostic
16426 is issued, and the error_mark_node is returned. If there are no
16427 partial specializations of TMPL matching TYPE, then NULL_TREE is
16431 most_specialized_class (tree type
, tree tmpl
, tsubst_flags_t complain
)
16433 tree list
= NULL_TREE
;
16439 tree outer_args
= NULL_TREE
;
16441 tmpl
= most_general_template (tmpl
);
16442 args
= CLASSTYPE_TI_ARGS (type
);
16444 /* For determining which partial specialization to use, only the
16445 innermost args are interesting. */
16446 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
16448 outer_args
= strip_innermost_template_args (args
, 1);
16449 args
= INNERMOST_TEMPLATE_ARGS (args
);
16452 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
16454 tree partial_spec_args
;
16456 tree parms
= TREE_VALUE (t
);
16458 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
16460 ++processing_template_decl
;
16466 /* Discard the outer levels of args, and then substitute in the
16467 template args from the enclosing class. */
16468 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
16469 partial_spec_args
= tsubst_template_args
16470 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
16472 /* PARMS already refers to just the innermost parms, but the
16473 template parms in partial_spec_args had their levels lowered
16474 by tsubst, so we need to do the same for the parm list. We
16475 can't just tsubst the TREE_VEC itself, as tsubst wants to
16476 treat a TREE_VEC as an argument vector. */
16477 parms
= copy_node (parms
);
16478 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
16479 TREE_VEC_ELT (parms
, i
) =
16480 tsubst (TREE_VEC_ELT (parms
, i
), outer_args
, tf_none
, NULL_TREE
);
16484 partial_spec_args
=
16485 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
16486 add_to_template_args (outer_args
,
16487 partial_spec_args
),
16489 /*require_all_args=*/true,
16490 /*use_default_args=*/true);
16492 --processing_template_decl
;
16494 if (partial_spec_args
== error_mark_node
)
16495 return error_mark_node
;
16497 spec_args
= get_class_bindings (parms
,
16503 spec_args
= add_to_template_args (outer_args
, spec_args
);
16504 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
16505 TREE_TYPE (list
) = TREE_TYPE (t
);
16512 ambiguous_p
= false;
16515 t
= TREE_CHAIN (t
);
16516 for (; t
; t
= TREE_CHAIN (t
))
16518 fate
= more_specialized_class (champ
, t
);
16525 t
= TREE_CHAIN (t
);
16528 ambiguous_p
= true;
16537 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
16539 fate
= more_specialized_class (champ
, t
);
16542 ambiguous_p
= true;
16550 char *spaces
= NULL
;
16551 if (!(complain
& tf_error
))
16552 return error_mark_node
;
16553 error ("ambiguous class template instantiation for %q#T", type
);
16554 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
16555 for (t
= list
; t
; t
= TREE_CHAIN (t
))
16557 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
16558 spaces
= spaces
? spaces
: get_spaces (str
);
16561 return error_mark_node
;
16567 /* Explicitly instantiate DECL. */
16570 do_decl_instantiation (tree decl
, tree storage
)
16572 tree result
= NULL_TREE
;
16575 if (!decl
|| decl
== error_mark_node
)
16576 /* An error occurred, for which grokdeclarator has already issued
16577 an appropriate message. */
16579 else if (! DECL_LANG_SPECIFIC (decl
))
16581 error ("explicit instantiation of non-template %q#D", decl
);
16584 else if (TREE_CODE (decl
) == VAR_DECL
)
16586 /* There is an asymmetry here in the way VAR_DECLs and
16587 FUNCTION_DECLs are handled by grokdeclarator. In the case of
16588 the latter, the DECL we get back will be marked as a
16589 template instantiation, and the appropriate
16590 DECL_TEMPLATE_INFO will be set up. This does not happen for
16591 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
16592 should handle VAR_DECLs as it currently handles
16594 if (!DECL_CLASS_SCOPE_P (decl
))
16596 error ("%qD is not a static data member of a class template", decl
);
16599 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
16600 if (!result
|| TREE_CODE (result
) != VAR_DECL
)
16602 error ("no matching template for %qD found", decl
);
16605 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
16607 error ("type %qT for explicit instantiation %qD does not match "
16608 "declared type %qT", TREE_TYPE (result
), decl
,
16613 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
16615 error ("explicit instantiation of %q#D", decl
);
16621 /* Check for various error cases. Note that if the explicit
16622 instantiation is valid the RESULT will currently be marked as an
16623 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
16624 until we get here. */
16626 if (DECL_TEMPLATE_SPECIALIZATION (result
))
16628 /* DR 259 [temp.spec].
16630 Both an explicit instantiation and a declaration of an explicit
16631 specialization shall not appear in a program unless the explicit
16632 instantiation follows a declaration of the explicit specialization.
16634 For a given set of template parameters, if an explicit
16635 instantiation of a template appears after a declaration of an
16636 explicit specialization for that template, the explicit
16637 instantiation has no effect. */
16640 else if (DECL_EXPLICIT_INSTANTIATION (result
))
16644 No program shall explicitly instantiate any template more
16647 We check DECL_NOT_REALLY_EXTERN so as not to complain when
16648 the first instantiation was `extern' and the second is not,
16649 and EXTERN_P for the opposite case. */
16650 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
16651 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
16652 /* If an "extern" explicit instantiation follows an ordinary
16653 explicit instantiation, the template is instantiated. */
16657 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
16659 error ("no matching template for %qD found", result
);
16662 else if (!DECL_TEMPLATE_INFO (result
))
16664 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
16668 if (storage
== NULL_TREE
)
16670 else if (storage
== ridpointers
[(int) RID_EXTERN
])
16672 if (!in_system_header
&& (cxx_dialect
== cxx98
))
16673 pedwarn (input_location
, OPT_pedantic
,
16674 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
16679 error ("storage class %qD applied to template instantiation", storage
);
16681 check_explicit_instantiation_namespace (result
);
16682 mark_decl_instantiated (result
, extern_p
);
16684 instantiate_decl (result
, /*defer_ok=*/1,
16685 /*expl_inst_class_mem_p=*/false);
16689 mark_class_instantiated (tree t
, int extern_p
)
16691 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
16692 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
16693 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
16694 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
16697 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
16698 rest_of_type_compilation (t
, 1);
16702 /* Called from do_type_instantiation through binding_table_foreach to
16703 do recursive instantiation for the type bound in ENTRY. */
16705 bt_instantiate_type_proc (binding_entry entry
, void *data
)
16707 tree storage
= *(tree
*) data
;
16709 if (MAYBE_CLASS_TYPE_P (entry
->type
)
16710 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
16711 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
16714 /* Called from do_type_instantiation to instantiate a member
16715 (a member function or a static member variable) of an
16716 explicitly instantiated class template. */
16718 instantiate_class_member (tree decl
, int extern_p
)
16720 mark_decl_instantiated (decl
, extern_p
);
16722 instantiate_decl (decl
, /*defer_ok=*/1,
16723 /*expl_inst_class_mem_p=*/true);
16726 /* Perform an explicit instantiation of template class T. STORAGE, if
16727 non-null, is the RID for extern, inline or static. COMPLAIN is
16728 nonzero if this is called from the parser, zero if called recursively,
16729 since the standard is unclear (as detailed below). */
16732 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
16737 int previous_instantiation_extern_p
= 0;
16739 if (TREE_CODE (t
) == TYPE_DECL
)
16742 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
16744 error ("explicit instantiation of non-template type %qT", t
);
16750 if (!COMPLETE_TYPE_P (t
))
16752 if (complain
& tf_error
)
16753 error ("explicit instantiation of %q#T before definition of template",
16758 if (storage
!= NULL_TREE
)
16760 if (!in_system_header
)
16762 if (storage
== ridpointers
[(int) RID_EXTERN
])
16764 if (cxx_dialect
== cxx98
)
16765 pedwarn (input_location
, OPT_pedantic
,
16766 "ISO C++ 1998 forbids the use of %<extern%> on "
16767 "explicit instantiations");
16770 pedwarn (input_location
, OPT_pedantic
,
16771 "ISO C++ forbids the use of %qE"
16772 " on explicit instantiations", storage
);
16775 if (storage
== ridpointers
[(int) RID_INLINE
])
16777 else if (storage
== ridpointers
[(int) RID_EXTERN
])
16779 else if (storage
== ridpointers
[(int) RID_STATIC
])
16783 error ("storage class %qD applied to template instantiation",
16789 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
16791 /* DR 259 [temp.spec].
16793 Both an explicit instantiation and a declaration of an explicit
16794 specialization shall not appear in a program unless the explicit
16795 instantiation follows a declaration of the explicit specialization.
16797 For a given set of template parameters, if an explicit
16798 instantiation of a template appears after a declaration of an
16799 explicit specialization for that template, the explicit
16800 instantiation has no effect. */
16803 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
16807 No program shall explicitly instantiate any template more
16810 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
16811 instantiation was `extern'. If EXTERN_P then the second is.
16812 These cases are OK. */
16813 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
16815 if (!previous_instantiation_extern_p
&& !extern_p
16816 && (complain
& tf_error
))
16817 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
16819 /* If we've already instantiated the template, just return now. */
16820 if (!CLASSTYPE_INTERFACE_ONLY (t
))
16824 check_explicit_instantiation_namespace (TYPE_NAME (t
));
16825 mark_class_instantiated (t
, extern_p
);
16833 /* In contrast to implicit instantiation, where only the
16834 declarations, and not the definitions, of members are
16835 instantiated, we have here:
16839 The explicit instantiation of a class template specialization
16840 implies the instantiation of all of its members not
16841 previously explicitly specialized in the translation unit
16842 containing the explicit instantiation.
16844 Of course, we can't instantiate member template classes, since
16845 we don't have any arguments for them. Note that the standard
16846 is unclear on whether the instantiation of the members are
16847 *explicit* instantiations or not. However, the most natural
16848 interpretation is that it should be an explicit instantiation. */
16851 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
16852 if (TREE_CODE (tmp
) == FUNCTION_DECL
16853 && DECL_TEMPLATE_INSTANTIATION (tmp
))
16854 instantiate_class_member (tmp
, extern_p
);
16856 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
16857 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
16858 instantiate_class_member (tmp
, extern_p
);
16860 if (CLASSTYPE_NESTED_UTDS (t
))
16861 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
16862 bt_instantiate_type_proc
, &storage
);
16866 /* Given a function DECL, which is a specialization of TMPL, modify
16867 DECL to be a re-instantiation of TMPL with the same template
16868 arguments. TMPL should be the template into which tsubst'ing
16869 should occur for DECL, not the most general template.
16871 One reason for doing this is a scenario like this:
16874 void f(const T&, int i);
16876 void g() { f(3, 7); }
16879 void f(const T& t, const int i) { }
16881 Note that when the template is first instantiated, with
16882 instantiate_template, the resulting DECL will have no name for the
16883 first parameter, and the wrong type for the second. So, when we go
16884 to instantiate the DECL, we regenerate it. */
16887 regenerate_decl_from_template (tree decl
, tree tmpl
)
16889 /* The arguments used to instantiate DECL, from the most general
16894 args
= DECL_TI_ARGS (decl
);
16895 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
16897 /* Make sure that we can see identifiers, and compute access
16899 push_access_scope (decl
);
16901 if (TREE_CODE (decl
) == FUNCTION_DECL
)
16909 args_depth
= TMPL_ARGS_DEPTH (args
);
16910 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
16911 if (args_depth
> parms_depth
)
16912 args
= get_innermost_template_args (args
, parms_depth
);
16914 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
16915 args
, tf_error
, NULL_TREE
);
16917 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
16920 /* Merge parameter declarations. */
16921 decl_parm
= skip_artificial_parms_for (decl
,
16922 DECL_ARGUMENTS (decl
));
16924 = skip_artificial_parms_for (code_pattern
,
16925 DECL_ARGUMENTS (code_pattern
));
16926 while (decl_parm
&& !FUNCTION_PARAMETER_PACK_P (pattern_parm
))
16931 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
16932 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
16933 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
16935 parm_type
= type_decays_to (parm_type
);
16936 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
16937 TREE_TYPE (decl_parm
) = parm_type
;
16938 attributes
= DECL_ATTRIBUTES (pattern_parm
);
16939 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
16941 DECL_ATTRIBUTES (decl_parm
) = attributes
;
16942 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
16944 decl_parm
= DECL_CHAIN (decl_parm
);
16945 pattern_parm
= DECL_CHAIN (pattern_parm
);
16947 /* Merge any parameters that match with the function parameter
16949 if (pattern_parm
&& FUNCTION_PARAMETER_PACK_P (pattern_parm
))
16952 tree expanded_types
;
16953 /* Expand the TYPE_PACK_EXPANSION that provides the types for
16954 the parameters in this function parameter pack. */
16955 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
16956 args
, tf_error
, NULL_TREE
);
16957 len
= TREE_VEC_LENGTH (expanded_types
);
16958 for (i
= 0; i
< len
; i
++)
16963 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
16964 /* Rename the parameter to include the index. */
16965 DECL_NAME (decl_parm
) =
16966 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
16967 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
16968 parm_type
= type_decays_to (parm_type
);
16969 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
16970 TREE_TYPE (decl_parm
) = parm_type
;
16971 attributes
= DECL_ATTRIBUTES (pattern_parm
);
16972 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
16974 DECL_ATTRIBUTES (decl_parm
) = attributes
;
16975 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
16977 decl_parm
= DECL_CHAIN (decl_parm
);
16980 /* Merge additional specifiers from the CODE_PATTERN. */
16981 if (DECL_DECLARED_INLINE_P (code_pattern
)
16982 && !DECL_DECLARED_INLINE_P (decl
))
16983 DECL_DECLARED_INLINE_P (decl
) = 1;
16985 else if (TREE_CODE (decl
) == VAR_DECL
)
16987 DECL_INITIAL (decl
) =
16988 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
16989 tf_error
, DECL_TI_TEMPLATE (decl
),
16990 /*integral_constant_expression_p=*/false);
16991 if (VAR_HAD_UNKNOWN_BOUND (decl
))
16992 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
16993 tf_error
, DECL_TI_TEMPLATE (decl
));
16996 gcc_unreachable ();
16998 pop_access_scope (decl
);
17001 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
17002 substituted to get DECL. */
17005 template_for_substitution (tree decl
)
17007 tree tmpl
= DECL_TI_TEMPLATE (decl
);
17009 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
17010 for the instantiation. This is not always the most general
17011 template. Consider, for example:
17014 struct S { template <class U> void f();
17015 template <> void f<int>(); };
17017 and an instantiation of S<double>::f<int>. We want TD to be the
17018 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
17019 while (/* An instantiation cannot have a definition, so we need a
17020 more general template. */
17021 DECL_TEMPLATE_INSTANTIATION (tmpl
)
17022 /* We must also deal with friend templates. Given:
17024 template <class T> struct S {
17025 template <class U> friend void f() {};
17028 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
17029 so far as the language is concerned, but that's still
17030 where we get the pattern for the instantiation from. On
17031 other hand, if the definition comes outside the class, say:
17033 template <class T> struct S {
17034 template <class U> friend void f();
17036 template <class U> friend void f() {}
17038 we don't need to look any further. That's what the check for
17039 DECL_INITIAL is for. */
17040 || (TREE_CODE (decl
) == FUNCTION_DECL
17041 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
17042 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
17044 /* The present template, TD, should not be a definition. If it
17045 were a definition, we should be using it! Note that we
17046 cannot restructure the loop to just keep going until we find
17047 a template with a definition, since that might go too far if
17048 a specialization was declared, but not defined. */
17049 gcc_assert (TREE_CODE (decl
) != VAR_DECL
17050 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
17052 /* Fetch the more general template. */
17053 tmpl
= DECL_TI_TEMPLATE (tmpl
);
17059 /* Returns true if we need to instantiate this template instance even if we
17060 know we aren't going to emit it.. */
17063 always_instantiate_p (tree decl
)
17065 /* We always instantiate inline functions so that we can inline them. An
17066 explicit instantiation declaration prohibits implicit instantiation of
17067 non-inline functions. With high levels of optimization, we would
17068 normally inline non-inline functions -- but we're not allowed to do
17069 that for "extern template" functions. Therefore, we check
17070 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
17071 return ((TREE_CODE (decl
) == FUNCTION_DECL
17072 && DECL_DECLARED_INLINE_P (decl
))
17073 /* And we need to instantiate static data members so that
17074 their initializers are available in integral constant
17076 || (TREE_CODE (decl
) == VAR_DECL
17077 && decl_maybe_constant_var_p (decl
)));
17080 /* Produce the definition of D, a _DECL generated from a template. If
17081 DEFER_OK is nonzero, then we don't have to actually do the
17082 instantiation now; we just have to do it sometime. Normally it is
17083 an error if this is an explicit instantiation but D is undefined.
17084 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
17085 explicitly instantiated class template. */
17088 instantiate_decl (tree d
, int defer_ok
,
17089 bool expl_inst_class_mem_p
)
17091 tree tmpl
= DECL_TI_TEMPLATE (d
);
17098 bool pattern_defined
;
17100 location_t saved_loc
= input_location
;
17103 /* This function should only be used to instantiate templates for
17104 functions and static member variables. */
17105 gcc_assert (TREE_CODE (d
) == FUNCTION_DECL
17106 || TREE_CODE (d
) == VAR_DECL
);
17108 /* Variables are never deferred; if instantiation is required, they
17109 are instantiated right away. That allows for better code in the
17110 case that an expression refers to the value of the variable --
17111 if the variable has a constant value the referring expression can
17112 take advantage of that fact. */
17113 if (TREE_CODE (d
) == VAR_DECL
17114 || DECL_DECLARED_CONSTEXPR_P (d
))
17117 /* Don't instantiate cloned functions. Instead, instantiate the
17118 functions they cloned. */
17119 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
17120 d
= DECL_CLONED_FUNCTION (d
);
17122 if (DECL_TEMPLATE_INSTANTIATED (d
)
17123 || DECL_TEMPLATE_SPECIALIZATION (d
))
17124 /* D has already been instantiated or explicitly specialized, so
17125 there's nothing for us to do here.
17127 It might seem reasonable to check whether or not D is an explicit
17128 instantiation, and, if so, stop here. But when an explicit
17129 instantiation is deferred until the end of the compilation,
17130 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
17131 the instantiation. */
17134 /* Check to see whether we know that this template will be
17135 instantiated in some other file, as with "extern template"
17137 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
17139 /* In general, we do not instantiate such templates. */
17140 if (external_p
&& !always_instantiate_p (d
))
17143 gen_tmpl
= most_general_template (tmpl
);
17144 gen_args
= DECL_TI_ARGS (d
);
17146 if (tmpl
!= gen_tmpl
)
17147 /* We should already have the extra args. */
17148 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
17149 == TMPL_ARGS_DEPTH (gen_args
));
17150 /* And what's in the hash table should match D. */
17151 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
17152 || spec
== NULL_TREE
);
17154 /* This needs to happen before any tsubsting. */
17155 if (! push_tinst_level (d
))
17158 timevar_push (TV_PARSE
);
17160 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
17161 for the instantiation. */
17162 td
= template_for_substitution (d
);
17163 code_pattern
= DECL_TEMPLATE_RESULT (td
);
17165 /* We should never be trying to instantiate a member of a class
17166 template or partial specialization. */
17167 gcc_assert (d
!= code_pattern
);
17169 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
17170 || DECL_TEMPLATE_SPECIALIZATION (td
))
17171 /* In the case of a friend template whose definition is provided
17172 outside the class, we may have too many arguments. Drop the
17173 ones we don't need. The same is true for specializations. */
17174 args
= get_innermost_template_args
17175 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
17179 if (TREE_CODE (d
) == FUNCTION_DECL
)
17180 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
);
17182 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
17184 /* We may be in the middle of deferred access check. Disable it now. */
17185 push_deferring_access_checks (dk_no_deferred
);
17187 /* Unless an explicit instantiation directive has already determined
17188 the linkage of D, remember that a definition is available for
17190 if (pattern_defined
17191 && !DECL_INTERFACE_KNOWN (d
)
17192 && !DECL_NOT_REALLY_EXTERN (d
))
17193 mark_definable (d
);
17195 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
17196 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
17197 input_location
= DECL_SOURCE_LOCATION (d
);
17199 /* If D is a member of an explicitly instantiated class template,
17200 and no definition is available, treat it like an implicit
17202 if (!pattern_defined
&& expl_inst_class_mem_p
17203 && DECL_EXPLICIT_INSTANTIATION (d
))
17205 DECL_NOT_REALLY_EXTERN (d
) = 0;
17206 DECL_INTERFACE_KNOWN (d
) = 0;
17207 SET_DECL_IMPLICIT_INSTANTIATION (d
);
17210 /* Recheck the substitutions to obtain any warning messages
17211 about ignoring cv qualifiers. Don't do this for artificial decls,
17212 as it breaks the context-sensitive substitution for lambda op(). */
17213 if (!defer_ok
&& !DECL_ARTIFICIAL (d
))
17215 tree gen
= DECL_TEMPLATE_RESULT (gen_tmpl
);
17216 tree type
= TREE_TYPE (gen
);
17218 /* Make sure that we can see identifiers, and compute access
17219 correctly. D is already the target FUNCTION_DECL with the
17221 push_access_scope (d
);
17223 if (TREE_CODE (gen
) == FUNCTION_DECL
)
17225 tsubst (DECL_ARGUMENTS (gen
), gen_args
, tf_warning_or_error
, d
);
17226 tsubst_exception_specification (type
, gen_args
, tf_warning_or_error
,
17228 /* Don't simply tsubst the function type, as that will give
17229 duplicate warnings about poor parameter qualifications.
17230 The function arguments are the same as the decl_arguments
17231 without the top level cv qualifiers. */
17232 type
= TREE_TYPE (type
);
17234 tsubst (type
, gen_args
, tf_warning_or_error
, d
);
17236 pop_access_scope (d
);
17239 /* Defer all other templates, unless we have been explicitly
17240 forbidden from doing so. */
17241 if (/* If there is no definition, we cannot instantiate the
17244 /* If it's OK to postpone instantiation, do so. */
17246 /* If this is a static data member that will be defined
17247 elsewhere, we don't want to instantiate the entire data
17248 member, but we do want to instantiate the initializer so that
17249 we can substitute that elsewhere. */
17250 || (external_p
&& TREE_CODE (d
) == VAR_DECL
))
17252 /* The definition of the static data member is now required so
17253 we must substitute the initializer. */
17254 if (TREE_CODE (d
) == VAR_DECL
17255 && !DECL_INITIAL (d
)
17256 && DECL_INITIAL (code_pattern
))
17260 bool const_init
= false;
17262 ns
= decl_namespace_context (d
);
17263 push_nested_namespace (ns
);
17264 push_nested_class (DECL_CONTEXT (d
));
17265 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
17267 tf_warning_or_error
, NULL_TREE
,
17268 /*integral_constant_expression_p=*/false);
17269 /* Make sure the initializer is still constant, in case of
17270 circular dependency (template/instantiate6.C). */
17272 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
17273 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
17274 /*asmspec_tree=*/NULL_TREE
,
17275 LOOKUP_ONLYCONVERTING
);
17276 pop_nested_class ();
17277 pop_nested_namespace (ns
);
17280 /* We restore the source position here because it's used by
17281 add_pending_template. */
17282 input_location
= saved_loc
;
17284 if (at_eof
&& !pattern_defined
17285 && DECL_EXPLICIT_INSTANTIATION (d
)
17286 && DECL_NOT_REALLY_EXTERN (d
))
17289 The definition of a non-exported function template, a
17290 non-exported member function template, or a non-exported
17291 member function or static data member of a class template
17292 shall be present in every translation unit in which it is
17293 explicitly instantiated. */
17294 permerror (input_location
, "explicit instantiation of %qD "
17295 "but no definition available", d
);
17297 /* If we're in unevaluated context, we just wanted to get the
17298 constant value; this isn't an odr use, so don't queue
17299 a full instantiation. */
17300 if (cp_unevaluated_operand
!= 0)
17302 /* ??? Historically, we have instantiated inline functions, even
17303 when marked as "extern template". */
17304 if (!(external_p
&& TREE_CODE (d
) == VAR_DECL
))
17305 add_pending_template (d
);
17308 /* Tell the repository that D is available in this translation unit
17309 -- and see if it is supposed to be instantiated here. */
17310 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
17312 /* In a PCH file, despite the fact that the repository hasn't
17313 requested instantiation in the PCH it is still possible that
17314 an instantiation will be required in a file that includes the
17317 add_pending_template (d
);
17318 /* Instantiate inline functions so that the inliner can do its
17319 job, even though we'll not be emitting a copy of this
17321 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
17325 need_push
= !cfun
|| !global_bindings_p ();
17327 push_to_top_level ();
17329 /* Mark D as instantiated so that recursive calls to
17330 instantiate_decl do not try to instantiate it again. */
17331 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
17333 /* Regenerate the declaration in case the template has been modified
17334 by a subsequent redeclaration. */
17335 regenerate_decl_from_template (d
, td
);
17337 /* We already set the file and line above. Reset them now in case
17338 they changed as a result of calling regenerate_decl_from_template. */
17339 input_location
= DECL_SOURCE_LOCATION (d
);
17341 if (TREE_CODE (d
) == VAR_DECL
)
17344 bool const_init
= false;
17346 /* Clear out DECL_RTL; whatever was there before may not be right
17347 since we've reset the type of the declaration. */
17348 SET_DECL_RTL (d
, NULL
);
17349 DECL_IN_AGGR_P (d
) = 0;
17351 /* The initializer is placed in DECL_INITIAL by
17352 regenerate_decl_from_template so we don't need to
17353 push/pop_access_scope again here. Pull it out so that
17354 cp_finish_decl can process it. */
17355 init
= DECL_INITIAL (d
);
17356 DECL_INITIAL (d
) = NULL_TREE
;
17357 DECL_INITIALIZED_P (d
) = 0;
17359 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
17360 initializer. That function will defer actual emission until
17361 we have a chance to determine linkage. */
17362 DECL_EXTERNAL (d
) = 0;
17364 /* Enter the scope of D so that access-checking works correctly. */
17365 push_nested_class (DECL_CONTEXT (d
));
17366 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
17367 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
17368 pop_nested_class ();
17370 else if (TREE_CODE (d
) == FUNCTION_DECL
)
17372 htab_t saved_local_specializations
;
17377 /* Save away the current list, in case we are instantiating one
17378 template from within the body of another. */
17379 saved_local_specializations
= local_specializations
;
17381 /* Set up the list of local specializations. */
17382 local_specializations
= htab_create (37,
17383 hash_local_specialization
,
17384 eq_local_specializations
,
17387 /* Set up context. */
17388 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
17390 /* Create substitution entries for the parameters. */
17391 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
17392 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
17393 spec_parm
= DECL_ARGUMENTS (d
);
17394 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
17396 register_local_specialization (spec_parm
, tmpl_parm
);
17397 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
17398 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
17400 while (tmpl_parm
&& !FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
17402 register_local_specialization (spec_parm
, tmpl_parm
);
17403 tmpl_parm
= DECL_CHAIN (tmpl_parm
);
17404 spec_parm
= DECL_CHAIN (spec_parm
);
17406 if (tmpl_parm
&& FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
17408 /* Register the (value) argument pack as a specialization of
17409 TMPL_PARM, then move on. */
17410 tree argpack
= make_fnparm_pack (spec_parm
);
17411 register_local_specialization (argpack
, tmpl_parm
);
17412 tmpl_parm
= DECL_CHAIN (tmpl_parm
);
17413 spec_parm
= NULL_TREE
;
17415 gcc_assert (!spec_parm
);
17417 /* Substitute into the body of the function. */
17418 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
17419 tf_warning_or_error
, tmpl
,
17420 /*integral_constant_expression_p=*/false);
17422 /* Set the current input_location to the end of the function
17423 so that finish_function knows where we are. */
17424 input_location
= DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
17426 /* We don't need the local specializations any more. */
17427 htab_delete (local_specializations
);
17428 local_specializations
= saved_local_specializations
;
17430 /* Finish the function. */
17431 d
= finish_function (0);
17432 expand_or_defer_fn (d
);
17435 /* We're not deferring instantiation any more. */
17436 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
17439 pop_from_top_level ();
17442 input_location
= saved_loc
;
17443 pop_deferring_access_checks ();
17444 pop_tinst_level ();
17446 timevar_pop (TV_PARSE
);
17451 /* Run through the list of templates that we wish we could
17452 instantiate, and instantiate any we can. RETRIES is the
17453 number of times we retry pending template instantiation. */
17456 instantiate_pending_templates (int retries
)
17459 location_t saved_loc
= input_location
;
17461 /* Instantiating templates may trigger vtable generation. This in turn
17462 may require further template instantiations. We place a limit here
17463 to avoid infinite loop. */
17464 if (pending_templates
&& retries
>= max_tinst_depth
)
17466 tree decl
= pending_templates
->tinst
->decl
;
17468 error ("template instantiation depth exceeds maximum of %d"
17469 " instantiating %q+D, possibly from virtual table generation"
17470 " (use -ftemplate-depth= to increase the maximum)",
17471 max_tinst_depth
, decl
);
17472 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17473 /* Pretend that we defined it. */
17474 DECL_INITIAL (decl
) = error_mark_node
;
17480 struct pending_template
**t
= &pending_templates
;
17481 struct pending_template
*last
= NULL
;
17485 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
17486 bool complete
= false;
17488 if (TYPE_P (instantiation
))
17492 if (!COMPLETE_TYPE_P (instantiation
))
17494 instantiate_class_template (instantiation
);
17495 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
17496 for (fn
= TYPE_METHODS (instantiation
);
17498 fn
= TREE_CHAIN (fn
))
17499 if (! DECL_ARTIFICIAL (fn
))
17500 instantiate_decl (fn
,
17502 /*expl_inst_class_mem_p=*/false);
17503 if (COMPLETE_TYPE_P (instantiation
))
17507 complete
= COMPLETE_TYPE_P (instantiation
);
17511 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
17512 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
17515 = instantiate_decl (instantiation
,
17517 /*expl_inst_class_mem_p=*/false);
17518 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
17522 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
17523 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
17527 /* If INSTANTIATION has been instantiated, then we don't
17528 need to consider it again in the future. */
17536 current_tinst_level
= NULL
;
17538 last_pending_template
= last
;
17540 while (reconsider
);
17542 input_location
= saved_loc
;
17545 /* Substitute ARGVEC into T, which is a list of initializers for
17546 either base class or a non-static data member. The TREE_PURPOSEs
17547 are DECLs, and the TREE_VALUEs are the initializer values. Used by
17548 instantiate_decl. */
17551 tsubst_initializer_list (tree t
, tree argvec
)
17553 tree inits
= NULL_TREE
;
17555 for (; t
; t
= TREE_CHAIN (t
))
17559 tree expanded_bases
= NULL_TREE
;
17560 tree expanded_arguments
= NULL_TREE
;
17563 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
17568 /* Expand the base class expansion type into separate base
17570 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
17571 tf_warning_or_error
,
17573 if (expanded_bases
== error_mark_node
)
17576 /* We'll be building separate TREE_LISTs of arguments for
17578 len
= TREE_VEC_LENGTH (expanded_bases
);
17579 expanded_arguments
= make_tree_vec (len
);
17580 for (i
= 0; i
< len
; i
++)
17581 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
17583 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
17584 expand each argument in the TREE_VALUE of t. */
17585 expr
= make_node (EXPR_PACK_EXPANSION
);
17586 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
17587 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
17589 if (TREE_VALUE (t
) == void_type_node
)
17590 /* VOID_TYPE_NODE is used to indicate
17591 value-initialization. */
17593 for (i
= 0; i
< len
; i
++)
17594 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
17598 /* Substitute parameter packs into each argument in the
17600 in_base_initializer
= 1;
17601 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
17603 tree expanded_exprs
;
17605 /* Expand the argument. */
17606 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
17608 = tsubst_pack_expansion (expr
, argvec
,
17609 tf_warning_or_error
,
17611 if (expanded_exprs
== error_mark_node
)
17614 /* Prepend each of the expanded expressions to the
17615 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
17616 for (i
= 0; i
< len
; i
++)
17618 TREE_VEC_ELT (expanded_arguments
, i
) =
17619 tree_cons (NULL_TREE
,
17620 TREE_VEC_ELT (expanded_exprs
, i
),
17621 TREE_VEC_ELT (expanded_arguments
, i
));
17624 in_base_initializer
= 0;
17626 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
17627 since we built them backwards. */
17628 for (i
= 0; i
< len
; i
++)
17630 TREE_VEC_ELT (expanded_arguments
, i
) =
17631 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
17636 for (i
= 0; i
< len
; ++i
)
17638 if (expanded_bases
)
17640 decl
= TREE_VEC_ELT (expanded_bases
, i
);
17641 decl
= expand_member_init (decl
);
17642 init
= TREE_VEC_ELT (expanded_arguments
, i
);
17646 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
17647 tf_warning_or_error
, NULL_TREE
);
17649 decl
= expand_member_init (decl
);
17650 if (decl
&& !DECL_P (decl
))
17651 in_base_initializer
= 1;
17653 init
= TREE_VALUE (t
);
17654 if (init
!= void_type_node
)
17655 init
= tsubst_expr (init
, argvec
,
17656 tf_warning_or_error
, NULL_TREE
,
17657 /*integral_constant_expression_p=*/false);
17658 in_base_initializer
= 0;
17663 init
= build_tree_list (decl
, init
);
17664 TREE_CHAIN (init
) = inits
;
17672 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
17675 set_current_access_from_decl (tree decl
)
17677 if (TREE_PRIVATE (decl
))
17678 current_access_specifier
= access_private_node
;
17679 else if (TREE_PROTECTED (decl
))
17680 current_access_specifier
= access_protected_node
;
17682 current_access_specifier
= access_public_node
;
17685 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
17686 is the instantiation (which should have been created with
17687 start_enum) and ARGS are the template arguments to use. */
17690 tsubst_enum (tree tag
, tree newtag
, tree args
)
17694 if (SCOPED_ENUM_P (newtag
))
17695 begin_scope (sk_scoped_enum
, newtag
);
17697 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
17702 decl
= TREE_VALUE (e
);
17703 /* Note that in a template enum, the TREE_VALUE is the
17704 CONST_DECL, not the corresponding INTEGER_CST. */
17705 value
= tsubst_expr (DECL_INITIAL (decl
),
17706 args
, tf_warning_or_error
, NULL_TREE
,
17707 /*integral_constant_expression_p=*/true);
17709 /* Give this enumeration constant the correct access. */
17710 set_current_access_from_decl (decl
);
17712 /* Actually build the enumerator itself. */
17714 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
17717 if (SCOPED_ENUM_P (newtag
))
17720 finish_enum_value_list (newtag
);
17721 finish_enum (newtag
);
17723 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
17724 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
17727 /* DECL is a FUNCTION_DECL that is a template specialization. Return
17728 its type -- but without substituting the innermost set of template
17729 arguments. So, innermost set of template parameters will appear in
17733 get_mostly_instantiated_function_type (tree decl
)
17741 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
17742 targs
= DECL_TI_ARGS (decl
);
17743 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
17744 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
17746 /* There should be as many levels of arguments as there are levels
17748 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
17750 fn_type
= TREE_TYPE (tmpl
);
17752 if (parm_depth
== 1)
17753 /* No substitution is necessary. */
17757 int i
, save_access_control
;
17760 /* Replace the innermost level of the TARGS with NULL_TREEs to
17761 let tsubst know not to substitute for those parameters. */
17762 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
17763 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
17764 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
17765 TMPL_ARGS_LEVEL (targs
, i
));
17766 SET_TMPL_ARGS_LEVEL (partial_args
,
17767 TMPL_ARGS_DEPTH (targs
),
17768 make_tree_vec (DECL_NTPARMS (tmpl
)));
17770 /* Disable access control as this function is used only during
17772 save_access_control
= flag_access_control
;
17773 flag_access_control
= 0;
17775 ++processing_template_decl
;
17776 /* Now, do the (partial) substitution to figure out the
17777 appropriate function type. */
17778 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
17779 --processing_template_decl
;
17781 /* Substitute into the template parameters to obtain the real
17782 innermost set of parameters. This step is important if the
17783 innermost set of template parameters contains value
17784 parameters whose types depend on outer template parameters. */
17785 TREE_VEC_LENGTH (partial_args
)--;
17786 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
17788 flag_access_control
= save_access_control
;
17794 /* Return truthvalue if we're processing a template different from
17795 the last one involved in diagnostics. */
17797 problematic_instantiation_changed (void)
17799 return last_template_error_tick
!= tinst_level_tick
;
17802 /* Remember current template involved in diagnostics. */
17804 record_last_problematic_instantiation (void)
17806 last_template_error_tick
= tinst_level_tick
;
17809 struct tinst_level
*
17810 current_instantiation (void)
17812 return current_tinst_level
;
17815 /* [temp.param] Check that template non-type parm TYPE is of an allowable
17816 type. Return zero for ok, nonzero for disallowed. Issue error and
17817 warning messages under control of COMPLAIN. */
17820 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
17822 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
17824 else if (POINTER_TYPE_P (type
))
17826 else if (TYPE_PTR_TO_MEMBER_P (type
))
17828 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
17830 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
17833 if (complain
& tf_error
)
17834 error ("%q#T is not a valid type for a template constant parameter", type
);
17838 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
17839 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
17842 dependent_type_p_r (tree type
)
17848 A type is dependent if it is:
17850 -- a template parameter. Template template parameters are types
17851 for us (since TYPE_P holds true for them) so we handle
17853 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
17854 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
17856 /* -- a qualified-id with a nested-name-specifier which contains a
17857 class-name that names a dependent type or whose unqualified-id
17858 names a dependent type. */
17859 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17861 /* -- a cv-qualified type where the cv-unqualified type is
17863 type
= TYPE_MAIN_VARIANT (type
);
17864 /* -- a compound type constructed from any dependent type. */
17865 if (TYPE_PTR_TO_MEMBER_P (type
))
17866 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
17867 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
17869 else if (TREE_CODE (type
) == POINTER_TYPE
17870 || TREE_CODE (type
) == REFERENCE_TYPE
)
17871 return dependent_type_p (TREE_TYPE (type
));
17872 else if (TREE_CODE (type
) == FUNCTION_TYPE
17873 || TREE_CODE (type
) == METHOD_TYPE
)
17877 if (dependent_type_p (TREE_TYPE (type
)))
17879 for (arg_type
= TYPE_ARG_TYPES (type
);
17881 arg_type
= TREE_CHAIN (arg_type
))
17882 if (dependent_type_p (TREE_VALUE (arg_type
)))
17886 /* -- an array type constructed from any dependent type or whose
17887 size is specified by a constant expression that is
17890 We checked for type- and value-dependence of the bounds in
17891 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
17892 if (TREE_CODE (type
) == ARRAY_TYPE
)
17894 if (TYPE_DOMAIN (type
)
17895 && dependent_type_p (TYPE_DOMAIN (type
)))
17897 return dependent_type_p (TREE_TYPE (type
));
17900 /* -- a template-id in which either the template name is a template
17902 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17904 /* ... or any of the template arguments is a dependent type or
17905 an expression that is type-dependent or value-dependent. */
17906 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
17907 && (any_dependent_template_arguments_p
17908 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
17911 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
17912 argument of the `typeof' expression is not type-dependent, then
17913 it should already been have resolved. */
17914 if (TREE_CODE (type
) == TYPEOF_TYPE
17915 || TREE_CODE (type
) == DECLTYPE_TYPE
)
17918 /* A template argument pack is dependent if any of its packed
17920 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
17922 tree args
= ARGUMENT_PACK_ARGS (type
);
17923 int i
, len
= TREE_VEC_LENGTH (args
);
17924 for (i
= 0; i
< len
; ++i
)
17925 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
17929 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
17930 be template parameters. */
17931 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
17934 /* The standard does not specifically mention types that are local
17935 to template functions or local classes, but they should be
17936 considered dependent too. For example:
17938 template <int I> void f() {
17943 The size of `E' cannot be known until the value of `I' has been
17944 determined. Therefore, `E' must be considered dependent. */
17945 scope
= TYPE_CONTEXT (type
);
17946 if (scope
&& TYPE_P (scope
))
17947 return dependent_type_p (scope
);
17948 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
17949 return type_dependent_expression_p (scope
);
17951 /* Other types are non-dependent. */
17955 /* Returns TRUE if TYPE is dependent, in the sense of
17956 [temp.dep.type]. Note that a NULL type is considered dependent. */
17959 dependent_type_p (tree type
)
17961 /* If there are no template parameters in scope, then there can't be
17962 any dependent types. */
17963 if (!processing_template_decl
)
17965 /* If we are not processing a template, then nobody should be
17966 providing us with a dependent type. */
17968 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
17972 /* If the type is NULL, we have not computed a type for the entity
17973 in question; in that case, the type is dependent. */
17977 /* Erroneous types can be considered non-dependent. */
17978 if (type
== error_mark_node
)
17981 /* If we have not already computed the appropriate value for TYPE,
17983 if (!TYPE_DEPENDENT_P_VALID (type
))
17985 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
17986 TYPE_DEPENDENT_P_VALID (type
) = 1;
17989 return TYPE_DEPENDENT_P (type
);
17992 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
17993 lookup. In other words, a dependent type that is not the current
17997 dependent_scope_p (tree scope
)
17999 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
18000 && !currently_open_class (scope
));
18003 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
18004 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
18007 /* Note that this predicate is not appropriate for general expressions;
18008 only constant expressions (that satisfy potential_constant_expression)
18009 can be tested for value dependence.
18011 We should really also have a predicate for "instantiation-dependent".
18013 fold_non_dependent_expr: fold if constant and not type-dependent and not value-dependent.
18014 (what about instantiation-dependent constant-expressions?)
18015 is_late_template_attribute: defer if instantiation-dependent.
18016 compute_array_index_type: proceed if constant and not t- or v-dependent
18017 if instantiation-dependent, need to remember full expression
18018 uses_template_parms: FIXME - need to audit callers
18019 tsubst_decl [function_decl]: Why is this using value_dependent_expression_p?
18020 dependent_type_p [array_type]: dependent if index type is dependent
18022 static_assert - instantiation-dependent */
18025 value_dependent_expression_p (tree expression
)
18027 if (!processing_template_decl
)
18030 /* A name declared with a dependent type. */
18031 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
18034 switch (TREE_CODE (expression
))
18036 case IDENTIFIER_NODE
:
18037 /* A name that has not been looked up -- must be dependent. */
18040 case TEMPLATE_PARM_INDEX
:
18041 /* A non-type template parm. */
18045 /* A non-type template parm. */
18046 if (DECL_TEMPLATE_PARM_P (expression
))
18048 return value_dependent_expression_p (DECL_INITIAL (expression
));
18051 /* A constant with integral or enumeration type and is initialized
18052 with an expression that is value-dependent. */
18053 if (DECL_INITIAL (expression
)
18054 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression
))
18055 && value_dependent_expression_p (DECL_INITIAL (expression
)))
18059 case DYNAMIC_CAST_EXPR
:
18060 case STATIC_CAST_EXPR
:
18061 case CONST_CAST_EXPR
:
18062 case REINTERPRET_CAST_EXPR
:
18064 /* These expressions are value-dependent if the type to which
18065 the cast occurs is dependent or the expression being casted
18066 is value-dependent. */
18068 tree type
= TREE_TYPE (expression
);
18070 if (dependent_type_p (type
))
18073 /* A functional cast has a list of operands. */
18074 expression
= TREE_OPERAND (expression
, 0);
18077 /* If there are no operands, it must be an expression such
18078 as "int()". This should not happen for aggregate types
18079 because it would form non-constant expressions. */
18080 gcc_assert (cxx_dialect
>= cxx0x
18081 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
18086 if (TREE_CODE (expression
) == TREE_LIST
)
18087 return any_value_dependent_elements_p (expression
);
18089 return value_dependent_expression_p (expression
);
18094 /* A `sizeof' expression is value-dependent if the operand is
18095 type-dependent or is a pack expansion. */
18096 expression
= TREE_OPERAND (expression
, 0);
18097 if (PACK_EXPANSION_P (expression
))
18099 else if (TYPE_P (expression
))
18100 return dependent_type_p (expression
);
18101 return type_dependent_expression_p (expression
);
18103 case AT_ENCODE_EXPR
:
18104 /* An 'encode' expression is value-dependent if the operand is
18106 expression
= TREE_OPERAND (expression
, 0);
18107 return dependent_type_p (expression
);
18109 case NOEXCEPT_EXPR
:
18110 expression
= TREE_OPERAND (expression
, 0);
18111 /* FIXME why check value-dependency? */
18112 return (type_dependent_expression_p (expression
)
18113 || value_dependent_expression_p (expression
));
18117 tree name
= TREE_OPERAND (expression
, 1);
18118 return value_dependent_expression_p (name
);
18121 case COMPONENT_REF
:
18122 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
18123 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
18125 case NONTYPE_ARGUMENT_PACK
:
18126 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
18127 is value-dependent. */
18129 tree values
= ARGUMENT_PACK_ARGS (expression
);
18130 int i
, len
= TREE_VEC_LENGTH (values
);
18132 for (i
= 0; i
< len
; ++i
)
18133 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
18141 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
18142 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
18143 || (type2
? dependent_type_p (type2
) : false));
18147 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
18148 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
18151 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
18152 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
18156 tree op
= TREE_OPERAND (expression
, 0);
18157 return (value_dependent_expression_p (op
)
18158 || has_value_dependent_address (op
));
18163 tree fn
= get_callee_fndecl (expression
);
18165 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
18167 nargs
= call_expr_nargs (expression
);
18168 for (i
= 0; i
< nargs
; ++i
)
18170 tree op
= CALL_EXPR_ARG (expression
, i
);
18171 /* In a call to a constexpr member function, look through the
18172 implicit ADDR_EXPR on the object argument so that it doesn't
18173 cause the call to be considered value-dependent. We also
18174 look through it in potential_constant_expression. */
18175 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
18176 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
18177 && TREE_CODE (op
) == ADDR_EXPR
)
18178 op
= TREE_OPERAND (op
, 0);
18179 if (value_dependent_expression_p (op
))
18186 /* A constant expression is value-dependent if any subexpression is
18187 value-dependent. */
18188 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
18190 case tcc_reference
:
18192 return (value_dependent_expression_p
18193 (TREE_OPERAND (expression
, 0)));
18195 case tcc_comparison
:
18197 return ((value_dependent_expression_p
18198 (TREE_OPERAND (expression
, 0)))
18199 || (value_dependent_expression_p
18200 (TREE_OPERAND (expression
, 1))));
18202 case tcc_expression
:
18206 for (i
= 0; i
< TREE_OPERAND_LENGTH (expression
); ++i
)
18207 /* In some cases, some of the operands may be missing.
18208 (For example, in the case of PREDECREMENT_EXPR, the
18209 amount to increment by may be missing.) That doesn't
18210 make the expression dependent. */
18211 if (TREE_OPERAND (expression
, i
)
18212 && (value_dependent_expression_p
18213 (TREE_OPERAND (expression
, i
))))
18223 /* The expression is not value-dependent. */
18227 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
18228 [temp.dep.expr]. Note that an expression with no type is
18229 considered dependent. Other parts of the compiler arrange for an
18230 expression with type-dependent subexpressions to have no type, so
18231 this function doesn't have to be fully recursive. */
18234 type_dependent_expression_p (tree expression
)
18236 if (!processing_template_decl
)
18239 if (expression
== error_mark_node
)
18242 /* An unresolved name is always dependent. */
18243 if (TREE_CODE (expression
) == IDENTIFIER_NODE
18244 || TREE_CODE (expression
) == USING_DECL
)
18247 /* Some expression forms are never type-dependent. */
18248 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
18249 || TREE_CODE (expression
) == SIZEOF_EXPR
18250 || TREE_CODE (expression
) == ALIGNOF_EXPR
18251 || TREE_CODE (expression
) == AT_ENCODE_EXPR
18252 || TREE_CODE (expression
) == NOEXCEPT_EXPR
18253 || TREE_CODE (expression
) == TRAIT_EXPR
18254 || TREE_CODE (expression
) == TYPEID_EXPR
18255 || TREE_CODE (expression
) == DELETE_EXPR
18256 || TREE_CODE (expression
) == VEC_DELETE_EXPR
18257 || TREE_CODE (expression
) == THROW_EXPR
)
18260 /* The types of these expressions depends only on the type to which
18261 the cast occurs. */
18262 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
18263 || TREE_CODE (expression
) == STATIC_CAST_EXPR
18264 || TREE_CODE (expression
) == CONST_CAST_EXPR
18265 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
18266 || TREE_CODE (expression
) == CAST_EXPR
)
18267 return dependent_type_p (TREE_TYPE (expression
));
18269 /* The types of these expressions depends only on the type created
18270 by the expression. */
18271 if (TREE_CODE (expression
) == NEW_EXPR
18272 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
18274 /* For NEW_EXPR tree nodes created inside a template, either
18275 the object type itself or a TREE_LIST may appear as the
18277 tree type
= TREE_OPERAND (expression
, 1);
18278 if (TREE_CODE (type
) == TREE_LIST
)
18279 /* This is an array type. We need to check array dimensions
18281 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
18282 || value_dependent_expression_p
18283 (TREE_OPERAND (TREE_VALUE (type
), 1));
18285 return dependent_type_p (type
);
18288 if (TREE_CODE (expression
) == SCOPE_REF
)
18290 tree scope
= TREE_OPERAND (expression
, 0);
18291 tree name
= TREE_OPERAND (expression
, 1);
18293 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
18294 contains an identifier associated by name lookup with one or more
18295 declarations declared with a dependent type, or...a
18296 nested-name-specifier or qualified-id that names a member of an
18297 unknown specialization. */
18298 return (type_dependent_expression_p (name
)
18299 || dependent_scope_p (scope
));
18302 if (TREE_CODE (expression
) == FUNCTION_DECL
18303 && DECL_LANG_SPECIFIC (expression
)
18304 && DECL_TEMPLATE_INFO (expression
)
18305 && (any_dependent_template_arguments_p
18306 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
18309 if (TREE_CODE (expression
) == TEMPLATE_DECL
18310 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
18313 if (TREE_CODE (expression
) == STMT_EXPR
)
18314 expression
= stmt_expr_value_expr (expression
);
18316 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
18321 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
18323 if (type_dependent_expression_p (elt
))
18329 /* A static data member of the current instantiation with incomplete
18330 array type is type-dependent, as the definition and specializations
18331 can have different bounds. */
18332 if (TREE_CODE (expression
) == VAR_DECL
18333 && DECL_CLASS_SCOPE_P (expression
)
18334 && dependent_type_p (DECL_CONTEXT (expression
))
18335 && VAR_HAD_UNKNOWN_BOUND (expression
))
18338 if (TREE_TYPE (expression
) == unknown_type_node
)
18340 if (TREE_CODE (expression
) == ADDR_EXPR
)
18341 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
18342 if (TREE_CODE (expression
) == COMPONENT_REF
18343 || TREE_CODE (expression
) == OFFSET_REF
)
18345 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
18347 expression
= TREE_OPERAND (expression
, 1);
18348 if (TREE_CODE (expression
) == IDENTIFIER_NODE
)
18351 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
18352 if (TREE_CODE (expression
) == SCOPE_REF
)
18355 if (TREE_CODE (expression
) == BASELINK
)
18356 expression
= BASELINK_FUNCTIONS (expression
);
18358 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
18360 if (any_dependent_template_arguments_p
18361 (TREE_OPERAND (expression
, 1)))
18363 expression
= TREE_OPERAND (expression
, 0);
18365 gcc_assert (TREE_CODE (expression
) == OVERLOAD
18366 || TREE_CODE (expression
) == FUNCTION_DECL
);
18370 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
18372 expression
= OVL_NEXT (expression
);
18377 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
18379 return (dependent_type_p (TREE_TYPE (expression
)));
18382 /* Like type_dependent_expression_p, but it also works while not processing
18383 a template definition, i.e. during substitution or mangling. */
18386 type_dependent_expression_p_push (tree expr
)
18389 ++processing_template_decl
;
18390 b
= type_dependent_expression_p (expr
);
18391 --processing_template_decl
;
18395 /* Returns TRUE if ARGS contains a type-dependent expression. */
18398 any_type_dependent_arguments_p (const VEC(tree
,gc
) *args
)
18403 FOR_EACH_VEC_ELT (tree
, args
, i
, arg
)
18405 if (type_dependent_expression_p (arg
))
18411 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
18412 expressions) contains any value-dependent expressions. */
18415 any_value_dependent_elements_p (const_tree list
)
18417 for (; list
; list
= TREE_CHAIN (list
))
18418 if (value_dependent_expression_p (TREE_VALUE (list
)))
18424 /* Returns TRUE if the ARG (a template argument) is dependent. */
18427 dependent_template_arg_p (tree arg
)
18429 if (!processing_template_decl
)
18432 /* Assume a template argument that was wrongly written by the user
18433 is dependent. This is consistent with what
18434 any_dependent_template_arguments_p [that calls this function]
18436 if (arg
== error_mark_node
)
18439 if (TREE_CODE (arg
) == TEMPLATE_DECL
18440 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
18441 return dependent_template_p (arg
);
18442 else if (ARGUMENT_PACK_P (arg
))
18444 tree args
= ARGUMENT_PACK_ARGS (arg
);
18445 int i
, len
= TREE_VEC_LENGTH (args
);
18446 for (i
= 0; i
< len
; ++i
)
18448 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
18454 else if (TYPE_P (arg
))
18455 return dependent_type_p (arg
);
18457 return (type_dependent_expression_p (arg
)
18458 || value_dependent_expression_p (arg
));
18461 /* Returns true if ARGS (a collection of template arguments) contains
18462 any types that require structural equality testing. */
18465 any_template_arguments_need_structural_equality_p (tree args
)
18472 if (args
== error_mark_node
)
18475 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
18477 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
18478 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
18480 tree arg
= TREE_VEC_ELT (level
, j
);
18481 tree packed_args
= NULL_TREE
;
18484 if (ARGUMENT_PACK_P (arg
))
18486 /* Look inside the argument pack. */
18487 packed_args
= ARGUMENT_PACK_ARGS (arg
);
18488 len
= TREE_VEC_LENGTH (packed_args
);
18491 for (k
= 0; k
< len
; ++k
)
18494 arg
= TREE_VEC_ELT (packed_args
, k
);
18496 if (error_operand_p (arg
))
18498 else if (TREE_CODE (arg
) == TEMPLATE_DECL
18499 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
18501 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
18503 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
18504 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
18513 /* Returns true if ARGS (a collection of template arguments) contains
18514 any dependent arguments. */
18517 any_dependent_template_arguments_p (const_tree args
)
18524 if (args
== error_mark_node
)
18527 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
18529 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
18530 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
18531 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
18538 /* Returns TRUE if the template TMPL is dependent. */
18541 dependent_template_p (tree tmpl
)
18543 if (TREE_CODE (tmpl
) == OVERLOAD
)
18547 if (dependent_template_p (OVL_FUNCTION (tmpl
)))
18549 tmpl
= OVL_CHAIN (tmpl
);
18554 /* Template template parameters are dependent. */
18555 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
18556 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
18558 /* So are names that have not been looked up. */
18559 if (TREE_CODE (tmpl
) == SCOPE_REF
18560 || TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
18562 /* So are member templates of dependent classes. */
18563 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
18564 return dependent_type_p (DECL_CONTEXT (tmpl
));
18568 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
18571 dependent_template_id_p (tree tmpl
, tree args
)
18573 return (dependent_template_p (tmpl
)
18574 || any_dependent_template_arguments_p (args
));
18577 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
18581 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
18585 if (!processing_template_decl
)
18588 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
18590 tree decl
= TREE_VEC_ELT (declv
, i
);
18591 tree init
= TREE_VEC_ELT (initv
, i
);
18592 tree cond
= TREE_VEC_ELT (condv
, i
);
18593 tree incr
= TREE_VEC_ELT (incrv
, i
);
18595 if (type_dependent_expression_p (decl
))
18598 if (init
&& type_dependent_expression_p (init
))
18601 if (type_dependent_expression_p (cond
))
18604 if (COMPARISON_CLASS_P (cond
)
18605 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
18606 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
18609 if (TREE_CODE (incr
) == MODOP_EXPR
)
18611 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
18612 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
18615 else if (type_dependent_expression_p (incr
))
18617 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
18619 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
18621 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
18623 tree t
= TREE_OPERAND (incr
, 1);
18624 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
18625 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
18634 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
18635 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
18636 no such TYPE can be found. Note that this function peers inside
18637 uninstantiated templates and therefore should be used only in
18638 extremely limited situations. ONLY_CURRENT_P restricts this
18639 peering to the currently open classes hierarchy (which is required
18640 when comparing types). */
18643 resolve_typename_type (tree type
, bool only_current_p
)
18652 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
18654 scope
= TYPE_CONTEXT (type
);
18655 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
18656 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
18657 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
18658 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
18659 identifier of the TYPENAME_TYPE anymore.
18660 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
18661 TYPENAME_TYPE instead, we avoid messing up with a possible
18662 typedef variant case. */
18663 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
18665 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
18666 it first before we can figure out what NAME refers to. */
18667 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
18668 scope
= resolve_typename_type (scope
, only_current_p
);
18669 /* If we don't know what SCOPE refers to, then we cannot resolve the
18671 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
18673 /* If the SCOPE is a template type parameter, we have no way of
18674 resolving the name. */
18675 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
18677 /* If the SCOPE is not the current instantiation, there's no reason
18678 to look inside it. */
18679 if (only_current_p
&& !currently_open_class (scope
))
18681 /* If this is a typedef, we don't want to look inside (c++/11987). */
18682 if (typedef_variant_p (type
))
18684 /* If SCOPE isn't the template itself, it will not have a valid
18685 TYPE_FIELDS list. */
18686 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
18687 /* scope is either the template itself or a compatible instantiation
18688 like X<T>, so look up the name in the original template. */
18689 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
18691 /* scope is a partial instantiation, so we can't do the lookup or we
18692 will lose the template arguments. */
18694 /* Enter the SCOPE so that name lookup will be resolved as if we
18695 were in the class definition. In particular, SCOPE will no
18696 longer be considered a dependent type. */
18697 pushed_scope
= push_scope (scope
);
18698 /* Look up the declaration. */
18699 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true);
18701 result
= NULL_TREE
;
18703 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
18704 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
18707 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == IDENTIFIER_NODE
18708 && TREE_CODE (decl
) == TYPE_DECL
)
18710 result
= TREE_TYPE (decl
);
18711 if (result
== error_mark_node
)
18712 result
= NULL_TREE
;
18714 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
18715 && DECL_CLASS_TEMPLATE_P (decl
))
18719 /* Obtain the template and the arguments. */
18720 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
18721 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
18722 /* Instantiate the template. */
18723 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
18724 /*entering_scope=*/0,
18725 tf_error
| tf_user
);
18726 if (result
== error_mark_node
)
18727 result
= NULL_TREE
;
18730 /* Leave the SCOPE. */
18732 pop_scope (pushed_scope
);
18734 /* If we failed to resolve it, return the original typename. */
18738 /* If lookup found a typename type, resolve that too. */
18739 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
18741 /* Ill-formed programs can cause infinite recursion here, so we
18742 must catch that. */
18743 TYPENAME_IS_RESOLVING_P (type
) = 1;
18744 result
= resolve_typename_type (result
, only_current_p
);
18745 TYPENAME_IS_RESOLVING_P (type
) = 0;
18748 /* Qualify the resulting type. */
18749 quals
= cp_type_quals (type
);
18751 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
18756 /* EXPR is an expression which is not type-dependent. Return a proxy
18757 for EXPR that can be used to compute the types of larger
18758 expressions containing EXPR. */
18761 build_non_dependent_expr (tree expr
)
18765 /* Preserve null pointer constants so that the type of things like
18766 "p == 0" where "p" is a pointer can be determined. */
18767 if (null_ptr_cst_p (expr
))
18769 /* Preserve OVERLOADs; the functions must be available to resolve
18772 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
18773 inner_expr
= stmt_expr_value_expr (inner_expr
);
18774 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
18775 inner_expr
= TREE_OPERAND (inner_expr
, 0);
18776 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
18777 inner_expr
= TREE_OPERAND (inner_expr
, 1);
18778 if (is_overloaded_fn (inner_expr
)
18779 || TREE_CODE (inner_expr
) == OFFSET_REF
)
18781 /* There is no need to return a proxy for a variable. */
18782 if (TREE_CODE (expr
) == VAR_DECL
)
18784 /* Preserve string constants; conversions from string constants to
18785 "char *" are allowed, even though normally a "const char *"
18786 cannot be used to initialize a "char *". */
18787 if (TREE_CODE (expr
) == STRING_CST
)
18789 /* Preserve arithmetic constants, as an optimization -- there is no
18790 reason to create a new node. */
18791 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
18793 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
18794 There is at least one place where we want to know that a
18795 particular expression is a throw-expression: when checking a ?:
18796 expression, there are special rules if the second or third
18797 argument is a throw-expression. */
18798 if (TREE_CODE (expr
) == THROW_EXPR
)
18801 if (TREE_CODE (expr
) == COND_EXPR
)
18802 return build3 (COND_EXPR
,
18804 TREE_OPERAND (expr
, 0),
18805 (TREE_OPERAND (expr
, 1)
18806 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
18807 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
18808 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
18809 if (TREE_CODE (expr
) == COMPOUND_EXPR
18810 && !COMPOUND_EXPR_OVERLOADED (expr
))
18811 return build2 (COMPOUND_EXPR
,
18813 TREE_OPERAND (expr
, 0),
18814 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
18816 /* If the type is unknown, it can't really be non-dependent */
18817 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
18819 /* Otherwise, build a NON_DEPENDENT_EXPR.
18821 REFERENCE_TYPEs are not stripped for expressions in templates
18822 because doing so would play havoc with mangling. Consider, for
18825 template <typename T> void f<T& g>() { g(); }
18827 In the body of "f", the expression for "g" will have
18828 REFERENCE_TYPE, even though the standard says that it should
18829 not. The reason is that we must preserve the syntactic form of
18830 the expression so that mangling (say) "f<g>" inside the body of
18831 "f" works out correctly. Therefore, the REFERENCE_TYPE is
18833 return build1 (NON_DEPENDENT_EXPR
, non_reference (TREE_TYPE (expr
)), expr
);
18836 /* ARGS is a vector of expressions as arguments to a function call.
18837 Replace the arguments with equivalent non-dependent expressions.
18838 This modifies ARGS in place. */
18841 make_args_non_dependent (VEC(tree
,gc
) *args
)
18846 FOR_EACH_VEC_ELT (tree
, args
, ix
, arg
)
18848 tree newarg
= build_non_dependent_expr (arg
);
18850 VEC_replace (tree
, args
, ix
, newarg
);
18854 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
18855 with a level one deeper than the actual template parms. */
18860 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
18861 TYPE_NAME (au
) = build_decl (BUILTINS_LOCATION
,
18862 TYPE_DECL
, get_identifier ("auto"), au
);
18863 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
18864 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
18865 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
18866 0, TYPE_NAME (au
), NULL_TREE
);
18867 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
18868 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
18869 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
18874 /* Given type ARG, return std::initializer_list<ARG>. */
18879 tree std_init_list
= namespace_binding
18880 (get_identifier ("initializer_list"), std_node
);
18882 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
18884 error ("deducing from brace-enclosed initializer list requires "
18885 "#include <initializer_list>");
18886 return error_mark_node
;
18888 argvec
= make_tree_vec (1);
18889 TREE_VEC_ELT (argvec
, 0) = arg
;
18890 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
18891 NULL_TREE
, 0, tf_warning_or_error
);
18894 /* Replace auto in TYPE with std::initializer_list<auto>. */
18897 listify_autos (tree type
, tree auto_node
)
18899 tree init_auto
= listify (auto_node
);
18900 tree argvec
= make_tree_vec (1);
18901 TREE_VEC_ELT (argvec
, 0) = init_auto
;
18902 if (processing_template_decl
)
18903 argvec
= add_to_template_args (current_template_args (), argvec
);
18904 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
18907 /* walk_tree helper for do_auto_deduction. */
18910 contains_auto_r (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
18913 /* Is this a variable with the type we're looking for? */
18915 && TREE_TYPE (*tp
) == type
)
18921 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
18922 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
18925 do_auto_deduction (tree type
, tree init
, tree auto_node
)
18927 tree parms
, tparms
, targs
;
18932 /* The name of the object being declared shall not appear in the
18933 initializer expression. */
18934 decl
= cp_walk_tree_without_duplicates (&init
, contains_auto_r
, type
);
18937 error ("variable %q#D with %<auto%> type used in its own "
18938 "initializer", decl
);
18939 return error_mark_node
;
18942 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
18943 with either a new invented type template parameter U or, if the
18944 initializer is a braced-init-list (8.5.4), with
18945 std::initializer_list<U>. */
18946 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
18947 type
= listify_autos (type
, auto_node
);
18949 init
= resolve_nondeduced_context (init
);
18951 parms
= build_tree_list (NULL_TREE
, type
);
18953 tparms
= make_tree_vec (1);
18954 targs
= make_tree_vec (1);
18955 TREE_VEC_ELT (tparms
, 0)
18956 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
18957 val
= type_unification_real (tparms
, targs
, parms
, args
, 1, 0,
18958 DEDUCE_CALL
, LOOKUP_NORMAL
);
18961 if (type
&& type
!= error_mark_node
)
18962 /* If type is error_mark_node a diagnostic must have been
18963 emitted by now. Also, having a mention to '<type error>'
18964 in the diagnostic is not really useful to the user. */
18965 error ("unable to deduce %qT from %qE", type
, init
);
18966 return error_mark_node
;
18969 /* If the list of declarators contains more than one declarator, the type
18970 of each declared variable is determined as described above. If the
18971 type deduced for the template parameter U is not the same in each
18972 deduction, the program is ill-formed. */
18973 if (TREE_TYPE (auto_node
)
18974 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
18976 error ("inconsistent deduction for %qT: %qT and then %qT",
18977 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
18978 return error_mark_node
;
18980 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
18982 if (processing_template_decl
)
18983 targs
= add_to_template_args (current_template_args (), targs
);
18984 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
18987 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
18991 splice_late_return_type (tree type
, tree late_return_type
)
18995 if (late_return_type
== NULL_TREE
)
18997 argvec
= make_tree_vec (1);
18998 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
18999 if (processing_template_decl
)
19000 argvec
= add_to_template_args (current_template_args (), argvec
);
19001 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
19004 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
19007 is_auto (const_tree type
)
19009 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
19010 && TYPE_IDENTIFIER (type
) == get_identifier ("auto"))
19016 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
19017 appear as a type-specifier for the declaration in question, we don't
19018 have to look through the whole type. */
19021 type_uses_auto (tree type
)
19023 enum tree_code code
;
19024 if (is_auto (type
))
19027 code
= TREE_CODE (type
);
19029 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
19030 || code
== OFFSET_TYPE
|| code
== FUNCTION_TYPE
19031 || code
== METHOD_TYPE
|| code
== ARRAY_TYPE
)
19032 return type_uses_auto (TREE_TYPE (type
));
19034 if (TYPE_PTRMEMFUNC_P (type
))
19035 return type_uses_auto (TREE_TYPE (TREE_TYPE
19036 (TYPE_PTRMEMFUNC_FN_TYPE (type
))));
19041 /* For a given template T, return the vector of typedefs referenced
19042 in T for which access check is needed at T instantiation time.
19043 T is either a FUNCTION_DECL or a RECORD_TYPE.
19044 Those typedefs were added to T by the function
19045 append_type_to_template_for_access_check. */
19047 VEC(qualified_typedef_usage_t
,gc
)*
19048 get_types_needing_access_check (tree t
)
19051 VEC(qualified_typedef_usage_t
,gc
) *result
= NULL
;
19053 if (!t
|| t
== error_mark_node
)
19056 if (!(ti
= get_template_info (t
)))
19059 if (CLASS_TYPE_P (t
)
19060 || TREE_CODE (t
) == FUNCTION_DECL
)
19062 if (!TI_TEMPLATE (ti
))
19065 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
19071 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
19072 tied to T. That list of typedefs will be access checked at
19073 T instantiation time.
19074 T is either a FUNCTION_DECL or a RECORD_TYPE.
19075 TYPE_DECL is a TYPE_DECL node representing a typedef.
19076 SCOPE is the scope through which TYPE_DECL is accessed.
19077 LOCATION is the location of the usage point of TYPE_DECL.
19079 This function is a subroutine of
19080 append_type_to_template_for_access_check. */
19083 append_type_to_template_for_access_check_1 (tree t
,
19086 location_t location
)
19088 qualified_typedef_usage_t typedef_usage
;
19091 if (!t
|| t
== error_mark_node
)
19094 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
19095 || CLASS_TYPE_P (t
))
19097 && TREE_CODE (type_decl
) == TYPE_DECL
19100 if (!(ti
= get_template_info (t
)))
19103 gcc_assert (TI_TEMPLATE (ti
));
19105 typedef_usage
.typedef_decl
= type_decl
;
19106 typedef_usage
.context
= scope
;
19107 typedef_usage
.locus
= location
;
19109 VEC_safe_push (qualified_typedef_usage_t
, gc
,
19110 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
),
19114 /* Append TYPE_DECL to the template TEMPL.
19115 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
19116 At TEMPL instanciation time, TYPE_DECL will be checked to see
19117 if it can be accessed through SCOPE.
19118 LOCATION is the location of the usage point of TYPE_DECL.
19120 e.g. consider the following code snippet:
19127 template<class U> struct S
19129 C::myint mi; // <-- usage point of the typedef C::myint
19134 At S<char> instantiation time, we need to check the access of C::myint
19135 In other words, we need to check the access of the myint typedef through
19136 the C scope. For that purpose, this function will add the myint typedef
19137 and the scope C through which its being accessed to a list of typedefs
19138 tied to the template S. That list will be walked at template instantiation
19139 time and access check performed on each typedefs it contains.
19140 Note that this particular code snippet should yield an error because
19141 myint is private to C. */
19144 append_type_to_template_for_access_check (tree templ
,
19147 location_t location
)
19149 qualified_typedef_usage_t
*iter
;
19152 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
19154 /* Make sure we don't append the type to the template twice. */
19155 FOR_EACH_VEC_ELT (qualified_typedef_usage_t
,
19156 get_types_needing_access_check (templ
),
19158 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
19161 append_type_to_template_for_access_check_1 (templ
, type_decl
,
19165 /* Set up the hash tables for template instantiations. */
19168 init_template_processing (void)
19170 decl_specializations
= htab_create_ggc (37,
19171 hash_specialization
,
19172 eq_specializations
,
19174 type_specializations
= htab_create_ggc (37,
19175 hash_specialization
,
19176 eq_specializations
,
19180 /* Print stats about the template hash tables for -fstats. */
19183 print_template_statistics (void)
19185 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
19186 "%f collisions\n", (long) htab_size (decl_specializations
),
19187 (long) htab_elements (decl_specializations
),
19188 htab_collisions (decl_specializations
));
19189 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
19190 "%f collisions\n", (long) htab_size (type_specializations
),
19191 (long) htab_elements (type_specializations
),
19192 htab_collisions (type_specializations
));
19195 #include "gt-cp-pt.h"