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 pushclass (NULL_TREE
);
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
))
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_NEXT (arg
))
1520 val
= iterative_hash_template_arg (OVL_CURRENT (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
))
1537 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1538 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1540 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1543 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1546 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1547 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1549 case TEMPLATE_PARM_INDEX
:
1550 val
= iterative_hash_template_arg
1551 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1552 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1553 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1556 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1557 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1558 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1561 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1563 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1567 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1568 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1569 val
= iterative_hash_object (code
, val
);
1570 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1573 /* A lambda can't appear in a template arg, but don't crash on
1575 gcc_assert (seen_error ());
1579 case STATIC_CAST_EXPR
:
1580 case REINTERPRET_CAST_EXPR
:
1581 case CONST_CAST_EXPR
:
1582 case DYNAMIC_CAST_EXPR
:
1584 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1585 /* Now hash operands as usual. */
1595 if (TYPE_CANONICAL (arg
))
1596 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1598 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1599 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1600 /* Otherwise just compare the types during lookup. */
1603 case tcc_declaration
:
1605 return iterative_hash_expr (arg
, val
);
1608 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1610 unsigned n
= TREE_OPERAND_LENGTH (arg
);
1611 for (i
= 0; i
< n
; ++i
)
1612 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1620 /* Unregister the specialization SPEC as a specialization of TMPL.
1621 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1622 if the SPEC was listed as a specialization of TMPL.
1624 Note that SPEC has been ggc_freed, so we can't look inside it. */
1627 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1632 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1633 elt
.args
= TI_ARGS (tinfo
);
1634 elt
.spec
= NULL_TREE
;
1636 slot
= (spec_entry
**) htab_find_slot (decl_specializations
, &elt
, INSERT
);
1639 gcc_assert ((*slot
)->spec
== spec
|| (*slot
)->spec
== new_spec
);
1640 gcc_assert (new_spec
!= NULL_TREE
);
1641 (*slot
)->spec
= new_spec
;
1648 /* Compare an entry in the local specializations hash table P1 (which
1649 is really a pointer to a TREE_LIST) with P2 (which is really a
1653 eq_local_specializations (const void *p1
, const void *p2
)
1655 return TREE_VALUE ((const_tree
) p1
) == (const_tree
) p2
;
1658 /* Hash P1, an entry in the local specializations table. */
1661 hash_local_specialization (const void* p1
)
1663 return htab_hash_pointer (TREE_VALUE ((const_tree
) p1
));
1666 /* Like register_specialization, but for local declarations. We are
1667 registering SPEC, an instantiation of TMPL. */
1670 register_local_specialization (tree spec
, tree tmpl
)
1674 slot
= htab_find_slot_with_hash (local_specializations
, tmpl
,
1675 htab_hash_pointer (tmpl
), INSERT
);
1676 *slot
= build_tree_list (spec
, tmpl
);
1679 /* TYPE is a class type. Returns true if TYPE is an explicitly
1680 specialized class. */
1683 explicit_class_specialization_p (tree type
)
1685 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1687 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1690 /* Print the list of functions at FNS, going through all the overloads
1691 for each element of the list. Alternatively, FNS can not be a
1692 TREE_LIST, in which case it will be printed together with all the
1695 MORE and *STR should respectively be FALSE and NULL when the function
1696 is called from the outside. They are used internally on recursive
1697 calls. print_candidates manages the two parameters and leaves NULL
1698 in *STR when it ends. */
1701 print_candidates_1 (tree fns
, bool more
, const char **str
)
1704 char *spaces
= NULL
;
1706 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1707 if (TREE_CODE (fn
) == TREE_LIST
)
1709 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1710 print_candidates_1 (TREE_VALUE (fn2
),
1711 TREE_CHAIN (fn2
) || more
, str
);
1717 /* Pick the prefix string. */
1718 if (!more
&& !OVL_NEXT (fns
))
1720 error ("candidate is: %+#D", OVL_CURRENT (fn
));
1724 *str
= _("candidates are:");
1725 spaces
= get_spaces (*str
);
1727 error ("%s %+#D", *str
, OVL_CURRENT (fn
));
1728 *str
= spaces
? spaces
: *str
;
1738 /* Print the list of candidate FNS in an error message. FNS can also
1739 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1742 print_candidates (tree fns
)
1744 const char *str
= NULL
;
1745 print_candidates_1 (fns
, false, &str
);
1746 gcc_assert (str
== NULL
);
1749 /* Returns the template (one of the functions given by TEMPLATE_ID)
1750 which can be specialized to match the indicated DECL with the
1751 explicit template args given in TEMPLATE_ID. The DECL may be
1752 NULL_TREE if none is available. In that case, the functions in
1753 TEMPLATE_ID are non-members.
1755 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1756 specialization of a member template.
1758 The TEMPLATE_COUNT is the number of references to qualifying
1759 template classes that appeared in the name of the function. See
1760 check_explicit_specialization for a more accurate description.
1762 TSK indicates what kind of template declaration (if any) is being
1763 declared. TSK_TEMPLATE indicates that the declaration given by
1764 DECL, though a FUNCTION_DECL, has template parameters, and is
1765 therefore a template function.
1767 The template args (those explicitly specified and those deduced)
1768 are output in a newly created vector *TARGS_OUT.
1770 If it is impossible to determine the result, an error message is
1771 issued. The error_mark_node is returned to indicate failure. */
1774 determine_specialization (tree template_id
,
1777 int need_member_template
,
1783 tree explicit_targs
;
1784 tree candidates
= NULL_TREE
;
1785 /* A TREE_LIST of templates of which DECL may be a specialization.
1786 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1787 corresponding TREE_PURPOSE is the set of template arguments that,
1788 when used to instantiate the template, would produce a function
1789 with the signature of DECL. */
1790 tree templates
= NULL_TREE
;
1792 struct cp_binding_level
*b
;
1794 *targs_out
= NULL_TREE
;
1796 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1797 return error_mark_node
;
1799 fns
= TREE_OPERAND (template_id
, 0);
1800 explicit_targs
= TREE_OPERAND (template_id
, 1);
1802 if (fns
== error_mark_node
)
1803 return error_mark_node
;
1805 /* Check for baselinks. */
1806 if (BASELINK_P (fns
))
1807 fns
= BASELINK_FUNCTIONS (fns
);
1809 if (!is_overloaded_fn (fns
))
1811 error ("%qD is not a function template", fns
);
1812 return error_mark_node
;
1815 /* Count the number of template headers specified for this
1818 for (b
= current_binding_level
;
1819 b
->kind
== sk_template_parms
;
1823 for (; fns
; fns
= OVL_NEXT (fns
))
1825 tree fn
= OVL_CURRENT (fns
);
1827 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1829 tree decl_arg_types
;
1832 /* In case of explicit specialization, we need to check if
1833 the number of template headers appearing in the specialization
1834 is correct. This is usually done in check_explicit_specialization,
1835 but the check done there cannot be exhaustive when specializing
1836 member functions. Consider the following code:
1838 template <> void A<int>::f(int);
1839 template <> template <> void A<int>::f(int);
1841 Assuming that A<int> is not itself an explicit specialization
1842 already, the first line specializes "f" which is a non-template
1843 member function, whilst the second line specializes "f" which
1844 is a template member function. So both lines are syntactically
1845 correct, and check_explicit_specialization does not reject
1848 Here, we can do better, as we are matching the specialization
1849 against the declarations. We count the number of template
1850 headers, and we check if they match TEMPLATE_COUNT + 1
1851 (TEMPLATE_COUNT is the number of qualifying template classes,
1852 plus there must be another header for the member template
1855 Notice that if header_count is zero, this is not a
1856 specialization but rather a template instantiation, so there
1857 is no check we can perform here. */
1858 if (header_count
&& header_count
!= template_count
+ 1)
1861 /* Check that the number of template arguments at the
1862 innermost level for DECL is the same as for FN. */
1863 if (current_binding_level
->kind
== sk_template_parms
1864 && !current_binding_level
->explicit_spec_p
1865 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1866 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1867 (current_template_parms
))))
1870 /* DECL might be a specialization of FN. */
1871 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1872 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1874 /* For a non-static member function, we need to make sure
1875 that the const qualification is the same. Since
1876 get_bindings does not try to merge the "this" parameter,
1877 we must do the comparison explicitly. */
1878 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1879 && !same_type_p (TREE_VALUE (fn_arg_types
),
1880 TREE_VALUE (decl_arg_types
)))
1883 /* Skip the "this" parameter and, for constructors of
1884 classes with virtual bases, the VTT parameter. A
1885 full specialization of a constructor will have a VTT
1886 parameter, but a template never will. */
1888 = skip_artificial_parms_for (decl
, decl_arg_types
);
1890 = skip_artificial_parms_for (fn
, fn_arg_types
);
1892 /* Check that the number of function parameters matches.
1894 template <class T> void f(int i = 0);
1895 template <> void f<int>();
1896 The specialization f<int> is invalid but is not caught
1897 by get_bindings below. */
1898 if (list_length (fn_arg_types
) != list_length (decl_arg_types
))
1901 /* Function templates cannot be specializations; there are
1902 no partial specializations of functions. Therefore, if
1903 the type of DECL does not match FN, there is no
1905 if (tsk
== tsk_template
)
1907 if (compparms (fn_arg_types
, decl_arg_types
))
1908 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1912 /* See whether this function might be a specialization of this
1914 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1917 /* We cannot deduce template arguments that when used to
1918 specialize TMPL will produce DECL. */
1921 /* Save this template, and the arguments deduced. */
1922 templates
= tree_cons (targs
, fn
, templates
);
1924 else if (need_member_template
)
1925 /* FN is an ordinary member function, and we need a
1926 specialization of a member template. */
1928 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1929 /* We can get IDENTIFIER_NODEs here in certain erroneous
1932 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1933 /* This is just an ordinary non-member function. Nothing can
1934 be a specialization of that. */
1936 else if (DECL_ARTIFICIAL (fn
))
1937 /* Cannot specialize functions that are created implicitly. */
1941 tree decl_arg_types
;
1943 /* This is an ordinary member function. However, since
1944 we're here, we can assume it's enclosing class is a
1945 template class. For example,
1947 template <typename T> struct S { void f(); };
1948 template <> void S<int>::f() {}
1950 Here, S<int>::f is a non-template, but S<int> is a
1951 template class. If FN has the same type as DECL, we
1952 might be in business. */
1954 if (!DECL_TEMPLATE_INFO (fn
))
1955 /* Its enclosing class is an explicit specialization
1956 of a template class. This is not a candidate. */
1959 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
1960 TREE_TYPE (TREE_TYPE (fn
))))
1961 /* The return types differ. */
1964 /* Adjust the type of DECL in case FN is a static member. */
1965 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1966 if (DECL_STATIC_FUNCTION_P (fn
)
1967 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1968 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1970 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1973 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1977 if (templates
&& TREE_CHAIN (templates
))
1983 It is possible for a specialization with a given function
1984 signature to be instantiated from more than one function
1985 template. In such cases, explicit specification of the
1986 template arguments must be used to uniquely identify the
1987 function template specialization being specialized.
1989 Note that here, there's no suggestion that we're supposed to
1990 determine which of the candidate templates is most
1991 specialized. However, we, also have:
1995 Partial ordering of overloaded function template
1996 declarations is used in the following contexts to select
1997 the function template to which a function template
1998 specialization refers:
2000 -- when an explicit specialization refers to a function
2003 So, we do use the partial ordering rules, at least for now.
2004 This extension can only serve to make invalid programs valid,
2005 so it's safe. And, there is strong anecdotal evidence that
2006 the committee intended the partial ordering rules to apply;
2007 the EDG front end has that behavior, and John Spicer claims
2008 that the committee simply forgot to delete the wording in
2009 [temp.expl.spec]. */
2010 tree tmpl
= most_specialized_instantiation (templates
);
2011 if (tmpl
!= error_mark_node
)
2014 TREE_CHAIN (templates
) = NULL_TREE
;
2018 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2020 error ("template-id %qD for %q+D does not match any template "
2021 "declaration", template_id
, decl
);
2022 if (header_count
&& header_count
!= template_count
+ 1)
2023 inform (input_location
, "saw %d %<template<>%>, need %d for "
2024 "specializing a member function template",
2025 header_count
, template_count
+ 1);
2026 return error_mark_node
;
2028 else if ((templates
&& TREE_CHAIN (templates
))
2029 || (candidates
&& TREE_CHAIN (candidates
))
2030 || (templates
&& candidates
))
2032 error ("ambiguous template specialization %qD for %q+D",
2034 candidates
= chainon (candidates
, templates
);
2035 print_candidates (candidates
);
2036 return error_mark_node
;
2039 /* We have one, and exactly one, match. */
2042 tree fn
= TREE_VALUE (candidates
);
2043 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2044 /* DECL is a re-declaration or partial instantiation of a template
2046 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2048 /* It was a specialization of an ordinary member function in a
2050 return DECL_TI_TEMPLATE (fn
);
2053 /* It was a specialization of a template. */
2054 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2055 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2057 *targs_out
= copy_node (targs
);
2058 SET_TMPL_ARGS_LEVEL (*targs_out
,
2059 TMPL_ARGS_DEPTH (*targs_out
),
2060 TREE_PURPOSE (templates
));
2063 *targs_out
= TREE_PURPOSE (templates
);
2064 return TREE_VALUE (templates
);
2067 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2068 but with the default argument values filled in from those in the
2072 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2075 tree new_spec_types
;
2080 if (spec_types
== void_list_node
)
2081 return void_list_node
;
2083 /* Substitute into the rest of the list. */
2085 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2086 TREE_CHAIN (tmpl_types
));
2088 /* Add the default argument for this parameter. */
2089 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2090 TREE_VALUE (spec_types
),
2094 /* DECL is an explicit specialization. Replicate default arguments
2095 from the template it specializes. (That way, code like:
2097 template <class T> void f(T = 3);
2098 template <> void f(double);
2101 works, as required.) An alternative approach would be to look up
2102 the correct default arguments at the call-site, but this approach
2103 is consistent with how implicit instantiations are handled. */
2106 copy_default_args_to_explicit_spec (tree decl
)
2111 tree new_spec_types
;
2115 tree object_type
= NULL_TREE
;
2116 tree in_charge
= NULL_TREE
;
2117 tree vtt
= NULL_TREE
;
2119 /* See if there's anything we need to do. */
2120 tmpl
= DECL_TI_TEMPLATE (decl
);
2121 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2122 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2123 if (TREE_PURPOSE (t
))
2128 old_type
= TREE_TYPE (decl
);
2129 spec_types
= TYPE_ARG_TYPES (old_type
);
2131 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2133 /* Remove the this pointer, but remember the object's type for
2135 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2136 spec_types
= TREE_CHAIN (spec_types
);
2137 tmpl_types
= TREE_CHAIN (tmpl_types
);
2139 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2141 /* DECL may contain more parameters than TMPL due to the extra
2142 in-charge parameter in constructors and destructors. */
2143 in_charge
= spec_types
;
2144 spec_types
= TREE_CHAIN (spec_types
);
2146 if (DECL_HAS_VTT_PARM_P (decl
))
2149 spec_types
= TREE_CHAIN (spec_types
);
2153 /* Compute the merged default arguments. */
2155 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2157 /* Compute the new FUNCTION_TYPE. */
2161 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2166 /* Put the in-charge parameter back. */
2167 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2168 TREE_VALUE (in_charge
),
2171 new_type
= build_method_type_directly (object_type
,
2172 TREE_TYPE (old_type
),
2176 new_type
= build_function_type (TREE_TYPE (old_type
),
2178 new_type
= cp_build_type_attribute_variant (new_type
,
2179 TYPE_ATTRIBUTES (old_type
));
2180 new_type
= build_exception_variant (new_type
,
2181 TYPE_RAISES_EXCEPTIONS (old_type
));
2182 TREE_TYPE (decl
) = new_type
;
2185 /* Check to see if the function just declared, as indicated in
2186 DECLARATOR, and in DECL, is a specialization of a function
2187 template. We may also discover that the declaration is an explicit
2188 instantiation at this point.
2190 Returns DECL, or an equivalent declaration that should be used
2191 instead if all goes well. Issues an error message if something is
2192 amiss. Returns error_mark_node if the error is not easily
2195 FLAGS is a bitmask consisting of the following flags:
2197 2: The function has a definition.
2198 4: The function is a friend.
2200 The TEMPLATE_COUNT is the number of references to qualifying
2201 template classes that appeared in the name of the function. For
2204 template <class T> struct S { void f(); };
2207 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2208 classes are not counted in the TEMPLATE_COUNT, so that in
2210 template <class T> struct S {};
2211 template <> struct S<int> { void f(); }
2212 template <> void S<int>::f();
2214 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2215 invalid; there should be no template <>.)
2217 If the function is a specialization, it is marked as such via
2218 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2219 is set up correctly, and it is added to the list of specializations
2220 for that template. */
2223 check_explicit_specialization (tree declarator
,
2228 int have_def
= flags
& 2;
2229 int is_friend
= flags
& 4;
2230 int specialization
= 0;
2231 int explicit_instantiation
= 0;
2232 int member_specialization
= 0;
2233 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2234 tree dname
= DECL_NAME (decl
);
2239 if (!processing_specialization
)
2242 tsk
= tsk_excessive_parms
;
2245 tsk
= current_tmpl_spec_kind (template_count
);
2250 if (processing_specialization
)
2253 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2255 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2258 /* This could be something like:
2260 template <class T> void f(T);
2261 class S { friend void f<>(int); } */
2265 /* This case handles bogus declarations like template <>
2266 template <class T> void f<int>(); */
2268 error ("template-id %qD in declaration of primary template",
2275 case tsk_invalid_member_spec
:
2276 /* The error has already been reported in
2277 check_specialization_scope. */
2278 return error_mark_node
;
2280 case tsk_invalid_expl_inst
:
2281 error ("template parameter list used in explicit instantiation");
2287 error ("definition provided for explicit instantiation");
2289 explicit_instantiation
= 1;
2292 case tsk_excessive_parms
:
2293 case tsk_insufficient_parms
:
2294 if (tsk
== tsk_excessive_parms
)
2295 error ("too many template parameter lists in declaration of %qD",
2297 else if (template_header_count
)
2298 error("too few template parameter lists in declaration of %qD", decl
);
2300 error("explicit specialization of %qD must be introduced by "
2301 "%<template <>%>", decl
);
2305 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2307 member_specialization
= 1;
2313 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2315 /* This case handles bogus declarations like template <>
2316 template <class T> void f<int>(); */
2318 if (uses_template_parms (declarator
))
2319 error ("function template partial specialization %qD "
2320 "is not allowed", declarator
);
2322 error ("template-id %qD in declaration of primary template",
2327 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2328 /* This is a specialization of a member template, without
2329 specialization the containing class. Something like:
2331 template <class T> struct S {
2332 template <class U> void f (U);
2334 template <> template <class U> void S<int>::f(U) {}
2336 That's a specialization -- but of the entire template. */
2344 if (specialization
|| member_specialization
)
2346 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2347 for (; t
; t
= TREE_CHAIN (t
))
2348 if (TREE_PURPOSE (t
))
2350 permerror (input_location
,
2351 "default argument specified in explicit specialization");
2356 if (specialization
|| member_specialization
|| explicit_instantiation
)
2358 tree tmpl
= NULL_TREE
;
2359 tree targs
= NULL_TREE
;
2361 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2362 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2366 gcc_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
);
2371 /* If there is no class context, the explicit instantiation
2372 must be at namespace scope. */
2373 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2375 /* Find the namespace binding, using the declaration
2377 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2379 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2381 error ("%qD is not a template function", dname
);
2382 fns
= error_mark_node
;
2386 tree fn
= OVL_CURRENT (fns
);
2387 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2388 CP_DECL_CONTEXT (fn
)))
2389 error ("%qD is not declared in %qD",
2390 decl
, current_namespace
);
2394 declarator
= lookup_template_function (fns
, NULL_TREE
);
2397 if (declarator
== error_mark_node
)
2398 return error_mark_node
;
2400 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2402 if (!explicit_instantiation
)
2403 /* A specialization in class scope. This is invalid,
2404 but the error will already have been flagged by
2405 check_specialization_scope. */
2406 return error_mark_node
;
2409 /* It's not valid to write an explicit instantiation in
2412 class C { template void f(); }
2414 This case is caught by the parser. However, on
2417 template class C { void f(); };
2419 (which is invalid) we can get here. The error will be
2426 else if (ctype
!= NULL_TREE
2427 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
2430 /* Find the list of functions in ctype that have the same
2431 name as the declared function. */
2432 tree name
= TREE_OPERAND (declarator
, 0);
2433 tree fns
= NULL_TREE
;
2436 if (constructor_name_p (name
, ctype
))
2438 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2440 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2441 : !CLASSTYPE_DESTRUCTORS (ctype
))
2443 /* From [temp.expl.spec]:
2445 If such an explicit specialization for the member
2446 of a class template names an implicitly-declared
2447 special member function (clause _special_), the
2448 program is ill-formed.
2450 Similar language is found in [temp.explicit]. */
2451 error ("specialization of implicitly-declared special member function");
2452 return error_mark_node
;
2455 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2458 if (!DECL_CONV_FN_P (decl
))
2460 idx
= lookup_fnfields_1 (ctype
, name
);
2462 fns
= VEC_index (tree
, CLASSTYPE_METHOD_VEC (ctype
), idx
);
2466 VEC(tree
,gc
) *methods
;
2469 /* For a type-conversion operator, we cannot do a
2470 name-based lookup. We might be looking for `operator
2471 int' which will be a specialization of `operator T'.
2472 So, we find *all* the conversion operators, and then
2473 select from them. */
2476 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2478 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2479 VEC_iterate (tree
, methods
, idx
, ovl
);
2482 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2483 /* There are no more conversion functions. */
2486 /* Glue all these conversion functions together
2487 with those we already have. */
2488 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2489 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2493 if (fns
== NULL_TREE
)
2495 error ("no member function %qD declared in %qT", name
, ctype
);
2496 return error_mark_node
;
2499 TREE_OPERAND (declarator
, 0) = fns
;
2502 /* Figure out what exactly is being specialized at this point.
2503 Note that for an explicit instantiation, even one for a
2504 member function, we cannot tell apriori whether the
2505 instantiation is for a member template, or just a member
2506 function of a template class. Even if a member template is
2507 being instantiated, the member template arguments may be
2508 elided if they can be deduced from the rest of the
2510 tmpl
= determine_specialization (declarator
, decl
,
2512 member_specialization
,
2516 if (!tmpl
|| tmpl
== error_mark_node
)
2517 /* We couldn't figure out what this declaration was
2519 return error_mark_node
;
2522 tree gen_tmpl
= most_general_template (tmpl
);
2524 if (explicit_instantiation
)
2526 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2527 is done by do_decl_instantiation later. */
2529 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2530 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2532 if (arg_depth
> parm_depth
)
2534 /* If TMPL is not the most general template (for
2535 example, if TMPL is a friend template that is
2536 injected into namespace scope), then there will
2537 be too many levels of TARGS. Remove some of them
2542 new_targs
= make_tree_vec (parm_depth
);
2543 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2544 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2545 = TREE_VEC_ELT (targs
, i
);
2549 return instantiate_template (tmpl
, targs
, tf_error
);
2552 /* If we thought that the DECL was a member function, but it
2553 turns out to be specializing a static member function,
2554 make DECL a static member function as well. */
2555 if (DECL_STATIC_FUNCTION_P (tmpl
)
2556 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2557 revert_static_member_fn (decl
);
2559 /* If this is a specialization of a member template of a
2560 template class, we want to return the TEMPLATE_DECL, not
2561 the specialization of it. */
2562 if (tsk
== tsk_template
)
2564 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2565 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2566 DECL_INITIAL (result
) = NULL_TREE
;
2570 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2571 DECL_SOURCE_LOCATION (result
)
2572 = DECL_SOURCE_LOCATION (decl
);
2573 /* We want to use the argument list specified in the
2574 definition, not in the original declaration. */
2575 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2576 for (parm
= DECL_ARGUMENTS (result
); parm
;
2577 parm
= DECL_CHAIN (parm
))
2578 DECL_CONTEXT (parm
) = result
;
2580 return register_specialization (tmpl
, gen_tmpl
, targs
,
2584 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2585 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2587 /* Inherit default function arguments from the template
2588 DECL is specializing. */
2589 copy_default_args_to_explicit_spec (decl
);
2591 /* This specialization has the same protection as the
2592 template it specializes. */
2593 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2594 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2596 /* 7.1.1-1 [dcl.stc]
2598 A storage-class-specifier shall not be specified in an
2599 explicit specialization...
2601 The parser rejects these, so unless action is taken here,
2602 explicit function specializations will always appear with
2605 The action recommended by the C++ CWG in response to C++
2606 defect report 605 is to make the storage class and linkage
2607 of the explicit specialization match the templated function:
2609 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2611 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2613 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2614 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2616 /* This specialization has the same linkage and visibility as
2617 the function template it specializes. */
2618 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2619 if (! TREE_PUBLIC (decl
))
2621 DECL_INTERFACE_KNOWN (decl
) = 1;
2622 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2624 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2625 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2627 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2628 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2632 /* If DECL is a friend declaration, declared using an
2633 unqualified name, the namespace associated with DECL may
2634 have been set incorrectly. For example, in:
2636 template <typename T> void f(T);
2638 struct S { friend void f<int>(int); }
2641 we will have set the DECL_CONTEXT for the friend
2642 declaration to N, rather than to the global namespace. */
2643 if (DECL_NAMESPACE_SCOPE_P (decl
))
2644 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2646 if (is_friend
&& !have_def
)
2647 /* This is not really a declaration of a specialization.
2648 It's just the name of an instantiation. But, it's not
2649 a request for an instantiation, either. */
2650 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2651 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2652 /* This is indeed a specialization. In case of constructors
2653 and destructors, we need in-charge and not-in-charge
2654 versions in V3 ABI. */
2655 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2657 /* Register this specialization so that we can find it
2659 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2666 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2667 parameters. These are represented in the same format used for
2668 DECL_TEMPLATE_PARMS. */
2671 comp_template_parms (const_tree parms1
, const_tree parms2
)
2676 if (parms1
== parms2
)
2679 for (p1
= parms1
, p2
= parms2
;
2680 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2681 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2683 tree t1
= TREE_VALUE (p1
);
2684 tree t2
= TREE_VALUE (p2
);
2687 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2688 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2690 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2693 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2695 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2696 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2698 /* If either of the template parameters are invalid, assume
2699 they match for the sake of error recovery. */
2700 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2703 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2706 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2707 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2708 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2710 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2715 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2716 /* One set of parameters has more parameters lists than the
2723 /* Determine whether PARM is a parameter pack. */
2726 template_parameter_pack_p (const_tree parm
)
2728 /* Determine if we have a non-type template parameter pack. */
2729 if (TREE_CODE (parm
) == PARM_DECL
)
2730 return (DECL_TEMPLATE_PARM_P (parm
)
2731 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2733 /* If this is a list of template parameters, we could get a
2734 TYPE_DECL or a TEMPLATE_DECL. */
2735 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2736 parm
= TREE_TYPE (parm
);
2738 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2739 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2740 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2743 /* Determine if T is a function parameter pack. */
2746 function_parameter_pack_p (const_tree t
)
2748 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2749 return FUNCTION_PARAMETER_PACK_P (t
);
2753 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2754 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2757 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2759 if (! primary_func_tmpl_inst
2760 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2761 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2764 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2767 /* Return true iff the function parameter PARAM_DECL was expanded
2768 from the function parameter pack PACK. */
2771 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
2773 if (DECL_ARTIFICIAL (param_decl
)
2774 || !function_parameter_pack_p (pack
))
2777 /* The parameter pack and its pack arguments have the same
2779 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
2782 /* Determine whether ARGS describes a variadic template args list,
2783 i.e., one that is terminated by a template argument pack. */
2786 template_args_variadic_p (tree args
)
2791 if (args
== NULL_TREE
)
2794 args
= INNERMOST_TEMPLATE_ARGS (args
);
2795 nargs
= TREE_VEC_LENGTH (args
);
2800 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2802 return ARGUMENT_PACK_P (last_parm
);
2805 /* Generate a new name for the parameter pack name NAME (an
2806 IDENTIFIER_NODE) that incorporates its */
2809 make_ith_pack_parameter_name (tree name
, int i
)
2811 /* Munge the name to include the parameter index. */
2812 #define NUMBUF_LEN 128
2813 char numbuf
[NUMBUF_LEN
];
2817 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
2818 newname_len
= IDENTIFIER_LENGTH (name
)
2819 + strlen (numbuf
) + 2;
2820 newname
= (char*)alloca (newname_len
);
2821 snprintf (newname
, newname_len
,
2822 "%s#%i", IDENTIFIER_POINTER (name
), i
);
2823 return get_identifier (newname
);
2826 /* Return true if T is a primary function
2827 or class template instantiation. */
2830 primary_template_instantiation_p (const_tree t
)
2835 if (TREE_CODE (t
) == FUNCTION_DECL
)
2836 return DECL_LANG_SPECIFIC (t
)
2837 && DECL_TEMPLATE_INSTANTIATION (t
)
2838 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
2839 else if (CLASS_TYPE_P (t
))
2840 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
2841 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
2845 /* Return true if PARM is a template template parameter. */
2848 template_template_parameter_p (const_tree parm
)
2850 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
2853 /* Return the template parameters of T if T is a
2854 primary template instantiation, NULL otherwise. */
2857 get_primary_template_innermost_parameters (const_tree t
)
2859 tree parms
= NULL
, template_info
= NULL
;
2861 if ((template_info
= get_template_info (t
))
2862 && primary_template_instantiation_p (t
))
2863 parms
= INNERMOST_TEMPLATE_PARMS
2864 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
2869 /* Return the template parameters of the LEVELth level from the full list
2870 of template parameters PARMS. */
2873 get_template_parms_at_level (tree parms
, int level
)
2877 || TREE_CODE (parms
) != TREE_LIST
2878 || level
> TMPL_PARMS_DEPTH (parms
))
2881 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
2882 if (TMPL_PARMS_DEPTH (p
) == level
)
2888 /* Returns the template arguments of T if T is a template instantiation,
2892 get_template_innermost_arguments (const_tree t
)
2894 tree args
= NULL
, template_info
= NULL
;
2896 if ((template_info
= get_template_info (t
))
2897 && TI_ARGS (template_info
))
2898 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
2903 /* Return the argument pack elements of T if T is a template argument pack,
2907 get_template_argument_pack_elems (const_tree t
)
2909 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
2910 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
2913 return ARGUMENT_PACK_ARGS (t
);
2916 /* Structure used to track the progress of find_parameter_packs_r. */
2917 struct find_parameter_pack_data
2919 /* TREE_LIST that will contain all of the parameter packs found by
2921 tree
* parameter_packs
;
2923 /* Set of AST nodes that have been visited by the traversal. */
2924 struct pointer_set_t
*visited
;
2927 /* Identifies all of the argument packs that occur in a template
2928 argument and appends them to the TREE_LIST inside DATA, which is a
2929 find_parameter_pack_data structure. This is a subroutine of
2930 make_pack_expansion and uses_parameter_packs. */
2932 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
2935 struct find_parameter_pack_data
* ppd
=
2936 (struct find_parameter_pack_data
*)data
;
2937 bool parameter_pack_p
= false;
2939 /* Identify whether this is a parameter pack or not. */
2940 switch (TREE_CODE (t
))
2942 case TEMPLATE_PARM_INDEX
:
2943 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
2944 parameter_pack_p
= true;
2947 case TEMPLATE_TYPE_PARM
:
2948 case TEMPLATE_TEMPLATE_PARM
:
2949 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
2950 parameter_pack_p
= true;
2954 if (FUNCTION_PARAMETER_PACK_P (t
))
2956 /* We don't want to walk into the type of a PARM_DECL,
2957 because we don't want to see the type parameter pack. */
2959 parameter_pack_p
= true;
2964 /* Not a parameter pack. */
2968 if (parameter_pack_p
)
2970 /* Add this parameter pack to the list. */
2971 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
2975 cp_walk_tree (&TYPE_CONTEXT (t
),
2976 &find_parameter_packs_r
, ppd
, ppd
->visited
);
2978 /* This switch statement will return immediately if we don't find a
2980 switch (TREE_CODE (t
))
2982 case TEMPLATE_PARM_INDEX
:
2985 case BOUND_TEMPLATE_TEMPLATE_PARM
:
2986 /* Check the template itself. */
2987 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
2988 &find_parameter_packs_r
, ppd
, ppd
->visited
);
2989 /* Check the template arguments. */
2990 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
2995 case TEMPLATE_TYPE_PARM
:
2996 case TEMPLATE_TEMPLATE_PARM
:
3003 if (TYPE_PTRMEMFUNC_P (t
))
3009 if (TYPE_TEMPLATE_INFO (t
))
3010 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
3011 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3017 cp_walk_tree (&TREE_TYPE (t
),
3018 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3022 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3027 case TYPE_PACK_EXPANSION
:
3028 case EXPR_PACK_EXPANSION
:
3033 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3038 case IDENTIFIER_NODE
:
3039 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3051 /* Determines if the expression or type T uses any parameter packs. */
3053 uses_parameter_packs (tree t
)
3055 tree parameter_packs
= NULL_TREE
;
3056 struct find_parameter_pack_data ppd
;
3057 ppd
.parameter_packs
= ¶meter_packs
;
3058 ppd
.visited
= pointer_set_create ();
3059 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3060 pointer_set_destroy (ppd
.visited
);
3061 return parameter_packs
!= NULL_TREE
;
3064 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3065 representation a base-class initializer into a parameter pack
3066 expansion. If all goes well, the resulting node will be an
3067 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3070 make_pack_expansion (tree arg
)
3073 tree parameter_packs
= NULL_TREE
;
3074 bool for_types
= false;
3075 struct find_parameter_pack_data ppd
;
3077 if (!arg
|| arg
== error_mark_node
)
3080 if (TREE_CODE (arg
) == TREE_LIST
)
3082 /* The only time we will see a TREE_LIST here is for a base
3083 class initializer. In this case, the TREE_PURPOSE will be a
3084 _TYPE node (representing the base class expansion we're
3085 initializing) and the TREE_VALUE will be a TREE_LIST
3086 containing the initialization arguments.
3088 The resulting expansion looks somewhat different from most
3089 expansions. Rather than returning just one _EXPANSION, we
3090 return a TREE_LIST whose TREE_PURPOSE is a
3091 TYPE_PACK_EXPANSION containing the bases that will be
3092 initialized. The TREE_VALUE will be identical to the
3093 original TREE_VALUE, which is a list of arguments that will
3094 be passed to each base. We do not introduce any new pack
3095 expansion nodes into the TREE_VALUE (although it is possible
3096 that some already exist), because the TREE_PURPOSE and
3097 TREE_VALUE all need to be expanded together with the same
3098 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3099 resulting TREE_PURPOSE will mention the parameter packs in
3100 both the bases and the arguments to the bases. */
3103 tree parameter_packs
= NULL_TREE
;
3105 /* Determine which parameter packs will be used by the base
3107 ppd
.visited
= pointer_set_create ();
3108 ppd
.parameter_packs
= ¶meter_packs
;
3109 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3112 if (parameter_packs
== NULL_TREE
)
3114 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3115 pointer_set_destroy (ppd
.visited
);
3116 return error_mark_node
;
3119 if (TREE_VALUE (arg
) != void_type_node
)
3121 /* Collect the sets of parameter packs used in each of the
3122 initialization arguments. */
3123 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3125 /* Determine which parameter packs will be expanded in this
3127 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3132 pointer_set_destroy (ppd
.visited
);
3134 /* Create the pack expansion type for the base type. */
3135 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3136 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3137 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3139 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3140 they will rarely be compared to anything. */
3141 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3143 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3146 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3149 /* Build the PACK_EXPANSION_* node. */
3151 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3152 : make_node (EXPR_PACK_EXPANSION
);
3153 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3154 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3156 /* Propagate type and const-expression information. */
3157 TREE_TYPE (result
) = TREE_TYPE (arg
);
3158 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3161 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3162 they will rarely be compared to anything. */
3163 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3165 /* Determine which parameter packs will be expanded. */
3166 ppd
.parameter_packs
= ¶meter_packs
;
3167 ppd
.visited
= pointer_set_create ();
3168 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3169 pointer_set_destroy (ppd
.visited
);
3171 /* Make sure we found some parameter packs. */
3172 if (parameter_packs
== NULL_TREE
)
3175 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3177 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3178 return error_mark_node
;
3180 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3185 /* Checks T for any "bare" parameter packs, which have not yet been
3186 expanded, and issues an error if any are found. This operation can
3187 only be done on full expressions or types (e.g., an expression
3188 statement, "if" condition, etc.), because we could have expressions like:
3190 foo(f(g(h(args)))...)
3192 where "args" is a parameter pack. check_for_bare_parameter_packs
3193 should not be called for the subexpressions args, h(args),
3194 g(h(args)), or f(g(h(args))), because we would produce erroneous
3197 Returns TRUE and emits an error if there were bare parameter packs,
3198 returns FALSE otherwise. */
3200 check_for_bare_parameter_packs (tree t
)
3202 tree parameter_packs
= NULL_TREE
;
3203 struct find_parameter_pack_data ppd
;
3205 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3208 if (TREE_CODE (t
) == TYPE_DECL
)
3211 ppd
.parameter_packs
= ¶meter_packs
;
3212 ppd
.visited
= pointer_set_create ();
3213 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3214 pointer_set_destroy (ppd
.visited
);
3216 if (parameter_packs
)
3218 error ("parameter packs not expanded with %<...%>:");
3219 while (parameter_packs
)
3221 tree pack
= TREE_VALUE (parameter_packs
);
3222 tree name
= NULL_TREE
;
3224 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3225 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3226 name
= TYPE_NAME (pack
);
3227 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3228 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3230 name
= DECL_NAME (pack
);
3233 inform (input_location
, " %qD", name
);
3235 inform (input_location
, " <anonymous>");
3237 parameter_packs
= TREE_CHAIN (parameter_packs
);
3246 /* Expand any parameter packs that occur in the template arguments in
3249 expand_template_argument_pack (tree args
)
3251 tree result_args
= NULL_TREE
;
3252 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3253 int num_result_args
= -1;
3254 int non_default_args_count
= -1;
3256 /* First, determine if we need to expand anything, and the number of
3257 slots we'll need. */
3258 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3260 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3261 if (arg
== NULL_TREE
)
3263 if (ARGUMENT_PACK_P (arg
))
3265 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3266 if (num_result_args
< 0)
3267 num_result_args
= in_arg
+ num_packed
;
3269 num_result_args
+= num_packed
;
3273 if (num_result_args
>= 0)
3278 /* If no expansion is necessary, we're done. */
3279 if (num_result_args
< 0)
3282 /* Expand arguments. */
3283 result_args
= make_tree_vec (num_result_args
);
3284 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3285 non_default_args_count
=
3286 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3287 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3289 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3290 if (ARGUMENT_PACK_P (arg
))
3292 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3293 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3294 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3295 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3296 if (non_default_args_count
> 0)
3297 non_default_args_count
+= num_packed
;
3301 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3305 if (non_default_args_count
>= 0)
3306 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3310 /* Checks if DECL shadows a template parameter.
3312 [temp.local]: A template-parameter shall not be redeclared within its
3313 scope (including nested scopes).
3315 Emits an error and returns TRUE if the DECL shadows a parameter,
3316 returns FALSE otherwise. */
3319 check_template_shadow (tree decl
)
3323 /* If we're not in a template, we can't possibly shadow a template
3325 if (!current_template_parms
)
3328 /* Figure out what we're shadowing. */
3329 if (TREE_CODE (decl
) == OVERLOAD
)
3330 decl
= OVL_CURRENT (decl
);
3331 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3333 /* If there's no previous binding for this name, we're not shadowing
3334 anything, let alone a template parameter. */
3338 /* If we're not shadowing a template parameter, we're done. Note
3339 that OLDDECL might be an OVERLOAD (or perhaps even an
3340 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3342 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3345 /* We check for decl != olddecl to avoid bogus errors for using a
3346 name inside a class. We check TPFI to avoid duplicate errors for
3347 inline member templates. */
3349 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
3352 error ("declaration of %q+#D", decl
);
3353 error (" shadows template parm %q+#D", olddecl
);
3357 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3358 ORIG_LEVEL, DECL, and TYPE. NUM_SIBLINGS is the total number of
3359 template parameters. */
3362 build_template_parm_index (int index
,
3369 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3370 TEMPLATE_PARM_IDX (t
) = index
;
3371 TEMPLATE_PARM_LEVEL (t
) = level
;
3372 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3373 TEMPLATE_PARM_NUM_SIBLINGS (t
) = num_siblings
;
3374 TEMPLATE_PARM_DECL (t
) = decl
;
3375 TREE_TYPE (t
) = type
;
3376 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3377 TREE_READONLY (t
) = TREE_READONLY (decl
);
3382 /* Find the canonical type parameter for the given template type
3383 parameter. Returns the canonical type parameter, which may be TYPE
3384 if no such parameter existed. */
3387 canonical_type_parameter (tree type
)
3390 int idx
= TEMPLATE_TYPE_IDX (type
);
3391 if (!canonical_template_parms
)
3392 canonical_template_parms
= VEC_alloc (tree
, gc
, idx
+1);
3394 while (VEC_length (tree
, canonical_template_parms
) <= (unsigned)idx
)
3395 VEC_safe_push (tree
, gc
, canonical_template_parms
, NULL_TREE
);
3397 list
= VEC_index (tree
, canonical_template_parms
, idx
);
3398 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3399 list
= TREE_CHAIN (list
);
3402 return TREE_VALUE (list
);
3405 VEC_replace(tree
, canonical_template_parms
, idx
,
3406 tree_cons (NULL_TREE
, type
,
3407 VEC_index (tree
, canonical_template_parms
, idx
)));
3412 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3413 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3414 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3415 new one is created. */
3418 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3419 tsubst_flags_t complain
)
3421 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3422 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3423 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3424 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3426 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3429 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3430 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3431 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3432 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3433 DECL_ARTIFICIAL (decl
) = 1;
3434 SET_DECL_TEMPLATE_PARM_P (decl
);
3436 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3437 TEMPLATE_PARM_LEVEL (index
) - levels
,
3438 TEMPLATE_PARM_ORIG_LEVEL (index
),
3439 TEMPLATE_PARM_NUM_SIBLINGS (index
),
3441 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3442 TEMPLATE_PARM_PARAMETER_PACK (t
)
3443 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3445 /* Template template parameters need this. */
3446 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3447 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3448 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3452 return TEMPLATE_PARM_DESCENDANTS (index
);
3455 /* Process information from new template parameter PARM and append it
3456 to the LIST being built. This new parameter is a non-type
3457 parameter iff IS_NON_TYPE is true. This new parameter is a
3458 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3459 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3460 parameter list PARM belongs to. This is used used to create a
3461 proper canonical type for the type of PARM that is to be created,
3462 iff PARM is a type. If the size is not known, this parameter shall
3466 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
3467 bool is_non_type
, bool is_parameter_pack
,
3468 unsigned num_template_parms
)
3475 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3476 defval
= TREE_PURPOSE (parm
);
3480 tree p
= tree_last (list
);
3482 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3485 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3486 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3488 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3498 parm
= TREE_VALUE (parm
);
3500 SET_DECL_TEMPLATE_PARM_P (parm
);
3502 if (TREE_TYPE (parm
) == error_mark_node
)
3504 err_parm_list
= build_tree_list (defval
, parm
);
3505 TREE_VALUE (err_parm_list
) = error_mark_node
;
3506 return chainon (list
, err_parm_list
);
3512 The top-level cv-qualifiers on the template-parameter are
3513 ignored when determining its type. */
3514 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3515 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3517 err_parm_list
= build_tree_list (defval
, parm
);
3518 TREE_VALUE (err_parm_list
) = error_mark_node
;
3519 return chainon (list
, err_parm_list
);
3522 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
)
3524 /* This template parameter is not a parameter pack, but it
3525 should be. Complain about "bare" parameter packs. */
3526 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3528 /* Recover by calling this a parameter pack. */
3529 is_parameter_pack
= true;
3533 /* A template parameter is not modifiable. */
3534 TREE_CONSTANT (parm
) = 1;
3535 TREE_READONLY (parm
) = 1;
3536 decl
= build_decl (parm_loc
,
3537 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3538 TREE_CONSTANT (decl
) = 1;
3539 TREE_READONLY (decl
) = 1;
3540 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3541 = build_template_parm_index (idx
, processing_template_decl
,
3542 processing_template_decl
,
3544 decl
, TREE_TYPE (parm
));
3546 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3547 = is_parameter_pack
;
3552 parm
= TREE_VALUE (TREE_VALUE (parm
));
3554 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3556 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3557 /* This is for distinguishing between real templates and template
3558 template parameters */
3559 TREE_TYPE (parm
) = t
;
3560 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3565 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3566 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3567 decl
= build_decl (parm_loc
,
3568 TYPE_DECL
, parm
, t
);
3571 TYPE_NAME (t
) = decl
;
3572 TYPE_STUB_DECL (t
) = decl
;
3574 TEMPLATE_TYPE_PARM_INDEX (t
)
3575 = build_template_parm_index (idx
, processing_template_decl
,
3576 processing_template_decl
,
3578 decl
, TREE_TYPE (parm
));
3579 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3580 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3582 DECL_ARTIFICIAL (decl
) = 1;
3583 SET_DECL_TEMPLATE_PARM_P (decl
);
3585 parm
= build_tree_list (defval
, parm
);
3586 return chainon (list
, parm
);
3589 /* The end of a template parameter list has been reached. Process the
3590 tree list into a parameter vector, converting each parameter into a more
3591 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3595 end_template_parm_list (tree parms
)
3599 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3601 current_template_parms
3602 = tree_cons (size_int (processing_template_decl
),
3603 saved_parmlist
, current_template_parms
);
3605 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3607 next
= TREE_CHAIN (parm
);
3608 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3609 TREE_CHAIN (parm
) = NULL_TREE
;
3612 --processing_template_parmlist
;
3614 return saved_parmlist
;
3617 /* Create a new type almost identical to TYPE but which has the
3618 following differences:
3620 1/ T has a new TEMPLATE_PARM_INDEX that carries the new number of
3621 template sibling parameters of T.
3623 2/ T has a new canonical type that matches the new number
3626 3/ From now on, T is going to be what lookups referring to the
3627 name of TYPE will return. No lookup should return TYPE anymore.
3629 NUM_PARMS is the new number of sibling parms TYPE belongs to.
3631 This is a subroutine of fixup_template_parms. */
3634 fixup_template_type_parm_type (tree type
, int num_parms
)
3636 tree orig_idx
= TEMPLATE_TYPE_PARM_INDEX (type
), idx
;
3638 /* This is the decl which name is inserted into the symbol table for
3639 the template parm type. So whenever we lookup the type name, this
3640 is the DECL we get. */
3643 /* Do not fix up the type twice. */
3644 if (orig_idx
&& TEMPLATE_PARM_NUM_SIBLINGS (orig_idx
) != 0)
3647 t
= copy_type (type
);
3648 decl
= TYPE_NAME (t
);
3650 TYPE_MAIN_VARIANT (t
) = t
;
3651 TYPE_NEXT_VARIANT (t
)= NULL_TREE
;
3652 TYPE_POINTER_TO (t
) = 0;
3653 TYPE_REFERENCE_TO (t
) = 0;
3655 idx
= build_template_parm_index (TEMPLATE_PARM_IDX (orig_idx
),
3656 TEMPLATE_PARM_LEVEL (orig_idx
),
3657 TEMPLATE_PARM_ORIG_LEVEL (orig_idx
),
3660 TEMPLATE_PARM_DESCENDANTS (idx
) = TEMPLATE_PARM_DESCENDANTS (orig_idx
);
3661 TEMPLATE_PARM_PARAMETER_PACK (idx
) = TEMPLATE_PARM_PARAMETER_PACK (orig_idx
);
3662 TEMPLATE_TYPE_PARM_INDEX (t
) = idx
;
3664 TYPE_STUB_DECL (t
) = decl
;
3665 TEMPLATE_TYPE_DECL (t
) = decl
;
3666 if (TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
3667 TREE_TYPE (DECL_TEMPLATE_RESULT (decl
)) = t
;
3669 /* Update the type associated to the type name stored in the symbol
3670 table. Now, whenever the type name is looked up, the resulting
3671 type is properly fixed up. */
3672 TREE_TYPE (decl
) = t
;
3674 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3679 /* Create and return a new TEMPLATE_PARM_INDEX that is almost
3680 identical to I, but that is fixed up as to:
3682 1/ carry the number of sibling parms (NUM_PARMS) of the template
3683 parm represented by I.
3685 2/ replace all references to template parm types declared before I
3686 (in the same template parm list as I) by references to template
3687 parm types contained in ARGS. ARGS should contain the list of
3688 template parms that have been fixed up so far, in a form suitable
3689 to be passed to tsubst.
3691 This is a subroutine of fixup_template_parms. */
3694 fixup_template_parm_index (tree i
, tree args
, int num_parms
)
3696 tree index
, decl
, type
;
3699 || TREE_CODE (i
) != TEMPLATE_PARM_INDEX
3700 /* Do not fix up the index twice. */
3701 || (TEMPLATE_PARM_NUM_SIBLINGS (i
) != 0))
3704 decl
= TEMPLATE_PARM_DECL (i
);
3705 type
= TREE_TYPE (decl
);
3707 index
= build_template_parm_index (TEMPLATE_PARM_IDX (i
),
3708 TEMPLATE_PARM_LEVEL (i
),
3709 TEMPLATE_PARM_ORIG_LEVEL (i
),
3713 TEMPLATE_PARM_DESCENDANTS (index
) = TEMPLATE_PARM_DESCENDANTS (i
);
3714 TEMPLATE_PARM_PARAMETER_PACK (index
) = TEMPLATE_PARM_PARAMETER_PACK (i
);
3716 type
= tsubst (type
, args
, tf_none
, NULL_TREE
);
3718 TREE_TYPE (decl
) = type
;
3719 TREE_TYPE (index
) = type
;
3725 This is a subroutine of fixup_template_parms.
3727 It computes the canonical type of the type of the template
3728 parameter PARM_DESC and update all references to that type so that
3729 they use the newly computed canonical type. No access check is
3730 performed during the fixup. PARM_DESC is a TREE_LIST which
3731 TREE_VALUE is the template parameter and its TREE_PURPOSE is the
3732 default argument of the template parm if any. IDX is the index of
3733 the template parameter, starting at 0. NUM_PARMS is the number of
3734 template parameters in the set PARM_DESC belongs to. ARGLIST is a
3735 TREE_VEC containing the full set of template parameters in a form
3736 suitable to be passed to substs functions as their ARGS
3737 argument. This is what current_template_args returns for a given
3738 template. The innermost vector of args in ARGLIST is the set of
3739 template parms that have been fixed up so far. This function adds
3740 the fixed up parameter into that vector. */
3743 fixup_template_parm (tree parm_desc
,
3748 tree parm
= TREE_VALUE (parm_desc
);
3749 tree fixedup_args
= INNERMOST_TEMPLATE_ARGS (arglist
);
3751 push_deferring_access_checks (dk_no_check
);
3753 if (TREE_CODE (parm
) == TYPE_DECL
)
3755 /* PARM is a template type parameter. Fix up its type, add
3756 the fixed-up template parm to the vector of fixed-up
3757 template parms so far, and substitute the fixed-up
3758 template parms into the default argument of this
3761 fixup_template_type_parm_type (TREE_TYPE (parm
), num_parms
);
3762 TREE_TYPE (parm
) = t
;
3764 TREE_VEC_ELT (fixedup_args
, idx
) = template_parm_to_arg (parm_desc
);
3766 else if (TREE_CODE (parm
) == TEMPLATE_DECL
)
3768 /* PARM is a template template parameter. This is going to
3770 tree tparms
, targs
, innermost_args
, t
;
3773 /* First, fix up the parms of the template template parm
3774 because the parms are involved in defining the new canonical
3775 type of the template template parm. */
3777 /* So we need to substitute the template parm types that have
3778 been fixed up so far into the template parms of this template
3779 template parm. E.g, consider this:
3781 template<class T, template<T u> class TT> class S;
3783 In this case we want to substitute T into the
3784 template parameters of TT.
3786 So let's walk the template parms of PARM here, and
3787 tsubst ARGLIST into into each of the template
3790 /* For this substitution we need to build the full set of
3791 template parameters and use that as arguments for the
3792 tsubsting function. */
3793 tparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
3795 /* This will contain the innermost parms of PARM into which
3796 we have substituted so far. */
3797 innermost_args
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
3798 targs
= add_to_template_args (arglist
, innermost_args
);
3799 for (j
= 0; j
< TREE_VEC_LENGTH (tparms
); ++j
)
3803 parameter
= TREE_VEC_ELT (tparms
, j
);
3805 /* INNERMOST_ARGS needs to have at least the same number
3806 of elements as the index PARAMETER, ortherwise
3807 tsubsting into PARAMETER will result in partially
3808 instantiating it, reducing its tempate parm
3809 level. Let's tactically fill INNERMOST_ARGS for that
3811 TREE_VEC_ELT (innermost_args
, j
) =
3812 template_parm_to_arg (parameter
);
3814 fixup_template_parm (parameter
, j
,
3815 TREE_VEC_LENGTH (tparms
),
3819 /* Now fix up the type of the template template parm. */
3821 t
= fixup_template_type_parm_type (TREE_TYPE (parm
), num_parms
);
3822 TREE_TYPE (parm
) = t
;
3824 TREE_VEC_ELT (fixedup_args
, idx
) =
3825 template_parm_to_arg (parm_desc
);
3827 else if (TREE_CODE (parm
) == PARM_DECL
)
3829 /* PARM is a non-type template parameter. We need to:
3831 * Fix up its TEMPLATE_PARM_INDEX to make it carry the
3832 proper number of sibling parameters.
3834 * Make lookups of the template parameter return a reference
3835 to the fixed-up index. No lookup should return references
3836 to the former index anymore.
3838 * Substitute the template parms that got fixed up so far
3840 * into the type of PARM. */
3842 tree index
= DECL_INITIAL (parm
);
3844 /* PUSHED_DECL is the decl added to the symbol table with
3845 the name of the parameter. E,g:
3847 template<class T, T u> //#0
3848 auto my_function(T t) -> decltype(u); //#1
3850 Here, when looking up u at //#1, we get the decl of u
3851 resulting from the declaration in #0. This is what
3852 PUSHED_DECL is. We need to replace the reference to the
3853 old TEMPLATE_PARM_INDEX carried by PUSHED_DECL by the
3854 fixed-up TEMPLATE_PARM_INDEX. */
3855 tree pushed_decl
= TEMPLATE_PARM_DECL (index
);
3857 /* Let's fix up the TEMPLATE_PARM_INDEX then. Note that we must
3858 fixup the type of PUSHED_DECL as well and luckily
3859 fixup_template_parm_index does it for us too. */
3860 tree fixed_up_index
=
3861 fixup_template_parm_index (index
, arglist
, num_parms
);
3863 DECL_INITIAL (pushed_decl
) = DECL_INITIAL (parm
) = fixed_up_index
;
3865 /* Add this fixed up PARM to the template parms we've fixed
3866 up so far and use that to substitute the fixed-up
3867 template parms into the type of PARM. */
3868 TREE_VEC_ELT (fixedup_args
, idx
) =
3869 template_parm_to_arg (parm_desc
);
3870 TREE_TYPE (parm
) = tsubst (TREE_TYPE (parm
), arglist
,
3871 tf_none
, NULL_TREE
);
3874 TREE_PURPOSE (parm_desc
) =
3875 tsubst_template_arg (TREE_PURPOSE (parm_desc
),
3876 arglist
, tf_none
, parm
);
3878 pop_deferring_access_checks ();
3881 /* Walk the current template parms and properly compute the canonical
3882 types of the dependent types created during
3883 cp_parser_template_parameter_list. */
3886 fixup_template_parms (void)
3893 parameter_vec
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
3894 if (parameter_vec
== NULL_TREE
)
3897 num_parms
= TREE_VEC_LENGTH (parameter_vec
);
3899 /* This vector contains the current innermost template parms that
3900 have been fixed up so far. The form of FIXEDUP_ARGS is suitable
3901 to be passed to tsubst* functions as their ARGS argument. */
3902 fixedup_args
= make_tree_vec (num_parms
);
3904 /* This vector contains the full set of template parms in a form
3905 suitable to be passed to substs functions as their ARGS
3907 arglist
= current_template_args ();
3908 arglist
= add_outermost_template_args (arglist
, fixedup_args
);
3910 /* Let's do the proper fixup now. */
3911 for (i
= 0; i
< num_parms
; ++i
)
3912 fixup_template_parm (TREE_VEC_ELT (parameter_vec
, i
),
3913 i
, num_parms
, arglist
);
3916 /* end_template_decl is called after a template declaration is seen. */
3919 end_template_decl (void)
3921 reset_specialization ();
3923 if (! processing_template_decl
)
3926 /* This matches the pushlevel in begin_template_parm_list. */
3929 --processing_template_decl
;
3930 current_template_parms
= TREE_CHAIN (current_template_parms
);
3933 /* Takes a TREE_LIST representing a template parameter and convert it
3934 into an argument suitable to be passed to the type substitution
3935 functions. Note that If the TREE_LIST contains an error_mark
3936 node, the returned argument is error_mark_node. */
3939 template_parm_to_arg (tree t
)
3943 || TREE_CODE (t
) != TREE_LIST
)
3946 if (error_operand_p (TREE_VALUE (t
)))
3947 return error_mark_node
;
3951 if (TREE_CODE (t
) == TYPE_DECL
3952 || TREE_CODE (t
) == TEMPLATE_DECL
)
3956 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3958 /* Turn this argument into a TYPE_ARGUMENT_PACK
3959 with a single element, which expands T. */
3960 tree vec
= make_tree_vec (1);
3961 #ifdef ENABLE_CHECKING
3962 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3963 (vec
, TREE_VEC_LENGTH (vec
));
3965 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3967 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
3968 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3973 t
= DECL_INITIAL (t
);
3975 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3977 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3978 with a single element, which expands T. */
3979 tree vec
= make_tree_vec (1);
3980 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3981 #ifdef ENABLE_CHECKING
3982 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3983 (vec
, TREE_VEC_LENGTH (vec
));
3985 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3987 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3988 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3989 TREE_TYPE (t
) = type
;
3995 /* Within the declaration of a template, return all levels of template
3996 parameters that apply. The template parameters are represented as
3997 a TREE_VEC, in the form documented in cp-tree.h for template
4001 current_template_args (void)
4004 tree args
= NULL_TREE
;
4005 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
4008 /* If there is only one level of template parameters, we do not
4009 create a TREE_VEC of TREE_VECs. Instead, we return a single
4010 TREE_VEC containing the arguments. */
4012 args
= make_tree_vec (length
);
4014 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
4016 tree a
= copy_node (TREE_VALUE (header
));
4019 TREE_TYPE (a
) = NULL_TREE
;
4020 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4021 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4023 #ifdef ENABLE_CHECKING
4024 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4028 TREE_VEC_ELT (args
, --l
) = a
;
4033 if (length
> 1 && TREE_VEC_ELT (args
, 0) == NULL_TREE
)
4034 /* This can happen for template parms of a template template
4037 template<template<class T, class U> class TT> struct S;
4039 Consider the level of the parms of TT; T and U both have
4040 level 2; TT has no template parm of level 1. So in this case
4041 the first element of full_template_args is NULL_TREE. If we
4042 leave it like this TMPL_ARG_DEPTH on args returns 1 instead
4043 of 2. This will make tsubst wrongly consider that T and U
4044 have level 1. Instead, let's create a dummy vector as the
4045 first element of full_template_args so that TMPL_ARG_DEPTH
4046 returns the correct depth for args. */
4047 TREE_VEC_ELT (args
, 0) = make_tree_vec (1);
4051 /* Update the declared TYPE by doing any lookups which were thought to be
4052 dependent, but are not now that we know the SCOPE of the declarator. */
4055 maybe_update_decl_type (tree orig_type
, tree scope
)
4057 tree type
= orig_type
;
4059 if (type
== NULL_TREE
)
4062 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4063 type
= TREE_TYPE (type
);
4065 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4066 && dependent_type_p (type
)
4067 /* Don't bother building up the args in this case. */
4068 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4070 /* tsubst in the args corresponding to the template parameters,
4071 including auto if present. Most things will be unchanged, but
4072 make_typename_type and tsubst_qualified_id will resolve
4073 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4074 tree args
= current_template_args ();
4075 tree auto_node
= type_uses_auto (type
);
4079 tree auto_vec
= make_tree_vec (1);
4080 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4081 args
= add_to_template_args (args
, auto_vec
);
4083 pushed
= push_scope (scope
);
4084 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4089 if (type
== error_mark_node
)
4092 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4094 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4097 type
= TYPE_NAME (type
);
4102 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4103 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
4104 a member template. Used by push_template_decl below. */
4107 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4109 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4110 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4111 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4112 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4117 struct template_parm_data
4119 /* The level of the template parameters we are currently
4123 /* The index of the specialization argument we are currently
4127 /* An array whose size is the number of template parameters. The
4128 elements are nonzero if the parameter has been used in any one
4129 of the arguments processed so far. */
4132 /* An array whose size is the number of template arguments. The
4133 elements are nonzero if the argument makes use of template
4134 parameters of this level. */
4135 int* arg_uses_template_parms
;
4138 /* Subroutine of push_template_decl used to see if each template
4139 parameter in a partial specialization is used in the explicit
4140 argument list. If T is of the LEVEL given in DATA (which is
4141 treated as a template_parm_data*), then DATA->PARMS is marked
4145 mark_template_parm (tree t
, void* data
)
4149 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4151 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4153 level
= TEMPLATE_PARM_LEVEL (t
);
4154 idx
= TEMPLATE_PARM_IDX (t
);
4158 level
= TEMPLATE_TYPE_LEVEL (t
);
4159 idx
= TEMPLATE_TYPE_IDX (t
);
4162 if (level
== tpd
->level
)
4164 tpd
->parms
[idx
] = 1;
4165 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4168 /* Return zero so that for_each_template_parm will continue the
4169 traversal of the tree; we want to mark *every* template parm. */
4173 /* Process the partial specialization DECL. */
4176 process_partial_specialization (tree decl
)
4178 tree type
= TREE_TYPE (decl
);
4179 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
4180 tree specargs
= CLASSTYPE_TI_ARGS (type
);
4181 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4182 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4185 int nargs
= TREE_VEC_LENGTH (inner_args
);
4188 bool did_error_intro
= false;
4189 struct template_parm_data tpd
;
4190 struct template_parm_data tpd2
;
4192 gcc_assert (current_template_parms
);
4194 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4195 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4197 /* We check that each of the template parameters given in the
4198 partial specialization is used in the argument list to the
4199 specialization. For example:
4201 template <class T> struct S;
4202 template <class T> struct S<T*>;
4204 The second declaration is OK because `T*' uses the template
4205 parameter T, whereas
4207 template <class T> struct S<int>;
4209 is no good. Even trickier is:
4220 The S2<T> declaration is actually invalid; it is a
4221 full-specialization. Of course,
4224 struct S2<T (*)(U)>;
4226 or some such would have been OK. */
4227 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4228 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4229 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4231 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4232 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4233 for (i
= 0; i
< nargs
; ++i
)
4235 tpd
.current_arg
= i
;
4236 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4237 &mark_template_parm
,
4240 /*include_nondeduced_p=*/false);
4242 for (i
= 0; i
< ntparms
; ++i
)
4243 if (tpd
.parms
[i
] == 0)
4245 /* One of the template parms was not used in the
4247 if (!did_error_intro
)
4249 error ("template parameters not used in partial specialization:");
4250 did_error_intro
= true;
4253 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4256 if (did_error_intro
)
4257 return error_mark_node
;
4259 /* [temp.class.spec]
4261 The argument list of the specialization shall not be identical to
4262 the implicit argument list of the primary template. */
4263 if (comp_template_args
4265 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4267 error ("partial specialization %qT does not specialize any template arguments", type
);
4269 /* [temp.class.spec]
4271 A partially specialized non-type argument expression shall not
4272 involve template parameters of the partial specialization except
4273 when the argument expression is a simple identifier.
4275 The type of a template parameter corresponding to a specialized
4276 non-type argument shall not be dependent on a parameter of the
4279 Also, we verify that pack expansions only occur at the
4280 end of the argument list. */
4281 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4283 for (i
= 0; i
< nargs
; ++i
)
4285 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4286 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4287 tree packed_args
= NULL_TREE
;
4290 if (ARGUMENT_PACK_P (arg
))
4292 /* Extract the arguments from the argument pack. We'll be
4293 iterating over these in the following loop. */
4294 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4295 len
= TREE_VEC_LENGTH (packed_args
);
4298 for (j
= 0; j
< len
; j
++)
4301 /* Get the Jth argument in the parameter pack. */
4302 arg
= TREE_VEC_ELT (packed_args
, j
);
4304 if (PACK_EXPANSION_P (arg
))
4306 /* Pack expansions must come at the end of the
4308 if ((packed_args
&& j
< len
- 1)
4309 || (!packed_args
&& i
< nargs
- 1))
4311 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4312 error ("parameter pack argument %qE must be at the "
4313 "end of the template argument list", arg
);
4315 error ("parameter pack argument %qT must be at the "
4316 "end of the template argument list", arg
);
4320 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4321 /* We only care about the pattern. */
4322 arg
= PACK_EXPANSION_PATTERN (arg
);
4324 if (/* These first two lines are the `non-type' bit. */
4326 && TREE_CODE (arg
) != TEMPLATE_DECL
4327 /* This next line is the `argument expression is not just a
4328 simple identifier' condition and also the `specialized
4329 non-type argument' bit. */
4330 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
4332 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4333 || (packed_args
&& uses_template_parms (arg
)))
4334 error ("template argument %qE involves template parameter(s)",
4338 /* Look at the corresponding template parameter,
4339 marking which template parameters its type depends
4341 tree type
= TREE_TYPE (parm
);
4345 /* We haven't yet initialized TPD2. Do so now. */
4346 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4347 /* The number of parameters here is the number in the
4348 main template, which, as checked in the assertion
4350 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4352 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4355 /* Mark the template parameters. But this time, we're
4356 looking for the template parameters of the main
4357 template, not in the specialization. */
4358 tpd2
.current_arg
= i
;
4359 tpd2
.arg_uses_template_parms
[i
] = 0;
4360 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4361 for_each_template_parm (type
,
4362 &mark_template_parm
,
4365 /*include_nondeduced_p=*/false);
4367 if (tpd2
.arg_uses_template_parms
[i
])
4369 /* The type depended on some template parameters.
4370 If they are fully specialized in the
4371 specialization, that's OK. */
4374 for (j
= 0; j
< nargs
; ++j
)
4375 if (tpd2
.parms
[j
] != 0
4376 && tpd
.arg_uses_template_parms
[j
])
4379 error_n (input_location
, count
,
4380 "type %qT of template argument %qE depends "
4381 "on a template parameter",
4382 "type %qT of template argument %qE depends "
4383 "on template parameters",
4392 /* We should only get here once. */
4393 gcc_assert (!COMPLETE_TYPE_P (type
));
4395 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4396 = tree_cons (specargs
, inner_parms
,
4397 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4398 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4400 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4401 inst
= TREE_CHAIN (inst
))
4403 tree inst_type
= TREE_VALUE (inst
);
4404 if (COMPLETE_TYPE_P (inst_type
)
4405 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type
))
4407 tree spec
= most_specialized_class (inst_type
, maintmpl
, tf_none
);
4408 if (spec
&& TREE_TYPE (spec
) == type
)
4409 permerror (input_location
,
4410 "partial specialization of %qT after instantiation "
4411 "of %qT", type
, inst_type
);
4418 /* Check that a template declaration's use of default arguments and
4419 parameter packs is not invalid. Here, PARMS are the template
4420 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
4421 a primary template. IS_PARTIAL is nonzero if DECL is a partial
4425 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4426 declaration (but not a definition); 1 indicates a declaration, 2
4427 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4428 emitted for extraneous default arguments.
4430 Returns TRUE if there were no errors found, FALSE otherwise. */
4433 check_default_tmpl_args (tree decl
, tree parms
, int is_primary
,
4434 int is_partial
, int is_friend_decl
)
4437 int last_level_to_check
;
4439 bool no_errors
= true;
4443 A default template-argument shall not be specified in a
4444 function template declaration or a function template definition, nor
4445 in the template-parameter-list of the definition of a member of a
4448 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
4449 /* You can't have a function template declaration in a local
4450 scope, nor you can you define a member of a class template in a
4454 if (current_class_type
4455 && !TYPE_BEING_DEFINED (current_class_type
)
4456 && DECL_LANG_SPECIFIC (decl
)
4457 && DECL_DECLARES_FUNCTION_P (decl
)
4458 /* If this is either a friend defined in the scope of the class
4459 or a member function. */
4460 && (DECL_FUNCTION_MEMBER_P (decl
)
4461 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4462 : DECL_FRIEND_CONTEXT (decl
)
4463 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4465 /* And, if it was a member function, it really was defined in
4466 the scope of the class. */
4467 && (!DECL_FUNCTION_MEMBER_P (decl
)
4468 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4469 /* We already checked these parameters when the template was
4470 declared, so there's no need to do it again now. This function
4471 was defined in class scope, but we're processing it's body now
4472 that the class is complete. */
4475 /* Core issue 226 (C++0x only): the following only applies to class
4477 if ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
)
4481 If a template-parameter has a default template-argument, all
4482 subsequent template-parameters shall have a default
4483 template-argument supplied. */
4484 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4486 tree inner_parms
= TREE_VALUE (parm_level
);
4487 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4488 int seen_def_arg_p
= 0;
4491 for (i
= 0; i
< ntparms
; ++i
)
4493 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4495 if (parm
== error_mark_node
)
4498 if (TREE_PURPOSE (parm
))
4500 else if (seen_def_arg_p
4501 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4503 error ("no default argument for %qD", TREE_VALUE (parm
));
4504 /* For better subsequent error-recovery, we indicate that
4505 there should have been a default argument. */
4506 TREE_PURPOSE (parm
) = error_mark_node
;
4512 /* Don't complain about an enclosing partial
4514 && parm_level
== parms
4515 && TREE_CODE (decl
) == TYPE_DECL
4517 && template_parameter_pack_p (TREE_VALUE (parm
)))
4519 /* A primary class template can only have one
4520 parameter pack, at the end of the template
4523 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
4524 error ("parameter pack %qE must be at the end of the"
4525 " template parameter list", TREE_VALUE (parm
));
4527 error ("parameter pack %qT must be at the end of the"
4528 " template parameter list",
4529 TREE_TYPE (TREE_VALUE (parm
)));
4531 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4539 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4543 /* For an ordinary class template, default template arguments are
4544 allowed at the innermost level, e.g.:
4545 template <class T = int>
4547 but, in a partial specialization, they're not allowed even
4548 there, as we have in [temp.class.spec]:
4550 The template parameter list of a specialization shall not
4551 contain default template argument values.
4553 So, for a partial specialization, or for a function template
4554 (in C++98/C++03), we look at all of them. */
4557 /* But, for a primary class template that is not a partial
4558 specialization we look at all template parameters except the
4560 parms
= TREE_CHAIN (parms
);
4562 /* Figure out what error message to issue. */
4563 if (is_friend_decl
== 2)
4564 msg
= G_("default template arguments may not be used in function template "
4565 "friend re-declaration");
4566 else if (is_friend_decl
)
4567 msg
= G_("default template arguments may not be used in function template "
4568 "friend declarations");
4569 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4570 msg
= G_("default template arguments may not be used in function templates "
4571 "without -std=c++0x or -std=gnu++0x");
4572 else if (is_partial
)
4573 msg
= G_("default template arguments may not be used in "
4574 "partial specializations");
4576 msg
= G_("default argument for template parameter for class enclosing %qD");
4578 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4579 /* If we're inside a class definition, there's no need to
4580 examine the parameters to the class itself. On the one
4581 hand, they will be checked when the class is defined, and,
4582 on the other, default arguments are valid in things like:
4583 template <class T = double>
4584 struct S { template <class U> void f(U); };
4585 Here the default argument for `S' has no bearing on the
4586 declaration of `f'. */
4587 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4589 /* Check everything. */
4590 last_level_to_check
= 0;
4592 for (parm_level
= parms
;
4593 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4594 parm_level
= TREE_CHAIN (parm_level
))
4596 tree inner_parms
= TREE_VALUE (parm_level
);
4600 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4601 for (i
= 0; i
< ntparms
; ++i
)
4603 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4606 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4611 if (is_friend_decl
== 2)
4618 /* Clear out the default argument so that we are not
4620 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4624 /* At this point, if we're still interested in issuing messages,
4625 they must apply to classes surrounding the object declared. */
4627 msg
= G_("default argument for template parameter for class "
4634 /* Worker for push_template_decl_real, called via
4635 for_each_template_parm. DATA is really an int, indicating the
4636 level of the parameters we are interested in. If T is a template
4637 parameter of that level, return nonzero. */
4640 template_parm_this_level_p (tree t
, void* data
)
4642 int this_level
= *(int *)data
;
4645 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4646 level
= TEMPLATE_PARM_LEVEL (t
);
4648 level
= TEMPLATE_TYPE_LEVEL (t
);
4649 return level
== this_level
;
4652 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4653 parameters given by current_template_args, or reuses a
4654 previously existing one, if appropriate. Returns the DECL, or an
4655 equivalent one, if it is replaced via a call to duplicate_decls.
4657 If IS_FRIEND is true, DECL is a friend declaration. */
4660 push_template_decl_real (tree decl
, bool is_friend
)
4668 int new_template_p
= 0;
4669 /* True if the template is a member template, in the sense of
4671 bool member_template_p
= false;
4673 if (decl
== error_mark_node
|| !current_template_parms
)
4674 return error_mark_node
;
4676 /* See if this is a partial specialization. */
4677 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
4678 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4679 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
4681 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4685 /* For a friend, we want the context of the friend function, not
4686 the type of which it is a friend. */
4687 ctx
= CP_DECL_CONTEXT (decl
);
4688 else if (CP_DECL_CONTEXT (decl
)
4689 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4690 /* In the case of a virtual function, we want the class in which
4692 ctx
= CP_DECL_CONTEXT (decl
);
4694 /* Otherwise, if we're currently defining some class, the DECL
4695 is assumed to be a member of the class. */
4696 ctx
= current_scope ();
4698 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4701 if (!DECL_CONTEXT (decl
))
4702 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4704 /* See if this is a primary template. */
4705 if (is_friend
&& ctx
)
4706 /* A friend template that specifies a class context, i.e.
4707 template <typename T> friend void A<T>::f();
4711 primary
= template_parm_scope_p ();
4715 if (DECL_CLASS_SCOPE_P (decl
))
4716 member_template_p
= true;
4717 if (TREE_CODE (decl
) == TYPE_DECL
4718 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4720 error ("template class without a name");
4721 return error_mark_node
;
4723 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4725 if (DECL_DESTRUCTOR_P (decl
))
4729 A destructor shall not be a member template. */
4730 error ("destructor %qD declared as member template", decl
);
4731 return error_mark_node
;
4733 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4734 && (!prototype_p (TREE_TYPE (decl
))
4735 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4736 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4737 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4738 == void_list_node
)))
4740 /* [basic.stc.dynamic.allocation]
4742 An allocation function can be a function
4743 template. ... Template allocation functions shall
4744 have two or more parameters. */
4745 error ("invalid template declaration of %qD", decl
);
4746 return error_mark_node
;
4749 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4750 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4754 error ("template declaration of %q#D", decl
);
4755 return error_mark_node
;
4759 /* Check to see that the rules regarding the use of default
4760 arguments are not being violated. */
4761 check_default_tmpl_args (decl
, current_template_parms
,
4762 primary
, is_partial
, /*is_friend_decl=*/0);
4764 /* Ensure that there are no parameter packs in the type of this
4765 declaration that have not been expanded. */
4766 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4768 /* Check each of the arguments individually to see if there are
4769 any bare parameter packs. */
4770 tree type
= TREE_TYPE (decl
);
4771 tree arg
= DECL_ARGUMENTS (decl
);
4772 tree argtype
= TYPE_ARG_TYPES (type
);
4774 while (arg
&& argtype
)
4776 if (!FUNCTION_PARAMETER_PACK_P (arg
)
4777 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4779 /* This is a PARM_DECL that contains unexpanded parameter
4780 packs. We have already complained about this in the
4781 check_for_bare_parameter_packs call, so just replace
4782 these types with ERROR_MARK_NODE. */
4783 TREE_TYPE (arg
) = error_mark_node
;
4784 TREE_VALUE (argtype
) = error_mark_node
;
4787 arg
= DECL_CHAIN (arg
);
4788 argtype
= TREE_CHAIN (argtype
);
4791 /* Check for bare parameter packs in the return type and the
4792 exception specifiers. */
4793 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4794 /* Errors were already issued, set return type to int
4795 as the frontend doesn't expect error_mark_node as
4797 TREE_TYPE (type
) = integer_type_node
;
4798 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4799 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4801 else if (check_for_bare_parameter_packs (TREE_TYPE (decl
)))
4803 TREE_TYPE (decl
) = error_mark_node
;
4804 return error_mark_node
;
4808 return process_partial_specialization (decl
);
4810 args
= current_template_args ();
4813 || TREE_CODE (ctx
) == FUNCTION_DECL
4814 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4815 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4817 if (DECL_LANG_SPECIFIC (decl
)
4818 && DECL_TEMPLATE_INFO (decl
)
4819 && DECL_TI_TEMPLATE (decl
))
4820 tmpl
= DECL_TI_TEMPLATE (decl
);
4821 /* If DECL is a TYPE_DECL for a class-template, then there won't
4822 be DECL_LANG_SPECIFIC. The information equivalent to
4823 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4824 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4825 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4826 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4828 /* Since a template declaration already existed for this
4829 class-type, we must be redeclaring it here. Make sure
4830 that the redeclaration is valid. */
4831 redeclare_class_template (TREE_TYPE (decl
),
4832 current_template_parms
);
4833 /* We don't need to create a new TEMPLATE_DECL; just use the
4834 one we already had. */
4835 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4839 tmpl
= build_template_decl (decl
, current_template_parms
,
4843 if (DECL_LANG_SPECIFIC (decl
)
4844 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4846 /* A specialization of a member template of a template
4848 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4849 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4850 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4856 tree a
, t
, current
, parms
;
4858 tree tinfo
= get_template_info (decl
);
4862 error ("template definition of non-template %q#D", decl
);
4863 return error_mark_node
;
4866 tmpl
= TI_TEMPLATE (tinfo
);
4868 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4869 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4870 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4871 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4875 /* The declaration is a specialization of a member
4876 template, declared outside the class. Therefore, the
4877 innermost template arguments will be NULL, so we
4878 replace them with the arguments determined by the
4879 earlier call to check_explicit_specialization. */
4880 args
= DECL_TI_ARGS (decl
);
4883 = build_template_decl (decl
, current_template_parms
,
4885 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4886 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4887 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4888 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4889 DECL_TEMPLATE_INFO (new_tmpl
)
4890 = build_template_info (tmpl
, args
);
4892 register_specialization (new_tmpl
,
4893 most_general_template (tmpl
),
4899 /* Make sure the template headers we got make sense. */
4901 parms
= DECL_TEMPLATE_PARMS (tmpl
);
4902 i
= TMPL_PARMS_DEPTH (parms
);
4903 if (TMPL_ARGS_DEPTH (args
) != i
)
4905 error ("expected %d levels of template parms for %q#D, got %d",
4906 i
, decl
, TMPL_ARGS_DEPTH (args
));
4909 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
4911 a
= TMPL_ARGS_LEVEL (args
, i
);
4912 t
= INNERMOST_TEMPLATE_PARMS (parms
);
4914 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
4916 if (current
== decl
)
4917 error ("got %d template parameters for %q#D",
4918 TREE_VEC_LENGTH (a
), decl
);
4920 error ("got %d template parameters for %q#T",
4921 TREE_VEC_LENGTH (a
), current
);
4922 error (" but %d required", TREE_VEC_LENGTH (t
));
4923 return error_mark_node
;
4926 if (current
== decl
)
4928 else if (current
== NULL_TREE
)
4929 /* Can happen in erroneous input. */
4932 current
= (TYPE_P (current
)
4933 ? TYPE_CONTEXT (current
)
4934 : DECL_CONTEXT (current
));
4937 /* Check that the parms are used in the appropriate qualifying scopes
4938 in the declarator. */
4939 if (!comp_template_args
4941 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
4944 template arguments to %qD do not match original template %qD",
4945 decl
, DECL_TEMPLATE_RESULT (tmpl
));
4946 if (!uses_template_parms (TI_ARGS (tinfo
)))
4947 inform (input_location
, "use template<> for an explicit specialization");
4948 /* Avoid crash in import_export_decl. */
4949 DECL_INTERFACE_KNOWN (decl
) = 1;
4950 return error_mark_node
;
4954 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4955 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4957 /* Push template declarations for global functions and types. Note
4958 that we do not try to push a global template friend declared in a
4959 template class; such a thing may well depend on the template
4960 parameters of the class. */
4961 if (new_template_p
&& !ctx
4962 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
4964 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
4965 if (tmpl
== error_mark_node
)
4966 return error_mark_node
;
4968 /* Hide template friend classes that haven't been declared yet. */
4969 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
4971 DECL_ANTICIPATED (tmpl
) = 1;
4972 DECL_FRIEND_P (tmpl
) = 1;
4978 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
4981 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4982 if (DECL_CONV_FN_P (tmpl
))
4984 int depth
= TMPL_PARMS_DEPTH (parms
);
4986 /* It is a conversion operator. See if the type converted to
4987 depends on innermost template operands. */
4989 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
4991 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
4994 /* Give template template parms a DECL_CONTEXT of the template
4995 for which they are a parameter. */
4996 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4997 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
4999 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5000 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5001 DECL_CONTEXT (parm
) = tmpl
;
5005 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5006 back to its most general template. If TMPL is a specialization,
5007 ARGS may only have the innermost set of arguments. Add the missing
5008 argument levels if necessary. */
5009 if (DECL_TEMPLATE_INFO (tmpl
))
5010 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5012 info
= build_template_info (tmpl
, args
);
5014 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5015 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5016 else if (DECL_LANG_SPECIFIC (decl
))
5017 DECL_TEMPLATE_INFO (decl
) = info
;
5019 return DECL_TEMPLATE_RESULT (tmpl
);
5023 push_template_decl (tree decl
)
5025 return push_template_decl_real (decl
, false);
5028 /* Called when a class template TYPE is redeclared with the indicated
5029 template PARMS, e.g.:
5031 template <class T> struct S;
5032 template <class T> struct S {}; */
5035 redeclare_class_template (tree type
, tree parms
)
5041 if (!TYPE_TEMPLATE_INFO (type
))
5043 error ("%qT is not a template type", type
);
5047 tmpl
= TYPE_TI_TEMPLATE (type
);
5048 if (!PRIMARY_TEMPLATE_P (tmpl
))
5049 /* The type is nested in some template class. Nothing to worry
5050 about here; there are no new template parameters for the nested
5056 error ("template specifiers not specified in declaration of %qD",
5061 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5062 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5064 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5066 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5067 "redeclared with %d template parameter",
5068 "redeclared with %d template parameters",
5069 TREE_VEC_LENGTH (parms
));
5070 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
5071 "previous declaration %q+D used %d template parameter",
5072 "previous declaration %q+D used %d template parameters",
5073 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5077 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5084 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5085 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5088 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5089 if (tmpl_parm
== error_mark_node
)
5092 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5093 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5094 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5096 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5098 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5099 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5100 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5101 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5102 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5103 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5104 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5105 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5106 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5108 error ("template parameter %q+#D", tmpl_parm
);
5109 error ("redeclared here as %q#D", parm
);
5113 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5115 /* We have in [temp.param]:
5117 A template-parameter may not be given default arguments
5118 by two different declarations in the same scope. */
5119 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5120 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5121 "original definition appeared here");
5125 if (parm_default
!= NULL_TREE
)
5126 /* Update the previous template parameters (which are the ones
5127 that will really count) with the new default value. */
5128 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5129 else if (tmpl_default
!= NULL_TREE
)
5130 /* Update the new parameters, too; they'll be used as the
5131 parameters for any members. */
5132 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5138 /* Simplify EXPR if it is a non-dependent expression. Returns the
5139 (possibly simplified) expression. */
5142 fold_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5144 if (expr
== NULL_TREE
)
5147 /* If we're in a template, but EXPR isn't value dependent, simplify
5148 it. We're supposed to treat:
5150 template <typename T> void f(T[1 + 1]);
5151 template <typename T> void f(T[2]);
5153 as two declarations of the same function, for example. */
5154 if (processing_template_decl
5155 && !type_dependent_expression_p (expr
)
5156 && potential_constant_expression (expr
)
5157 && !value_dependent_expression_p (expr
))
5159 HOST_WIDE_INT saved_processing_template_decl
;
5161 saved_processing_template_decl
= processing_template_decl
;
5162 processing_template_decl
= 0;
5163 expr
= tsubst_copy_and_build (expr
,
5166 /*in_decl=*/NULL_TREE
,
5167 /*function_p=*/false,
5168 /*integral_constant_expression_p=*/true);
5169 processing_template_decl
= saved_processing_template_decl
;
5175 fold_non_dependent_expr (tree expr
)
5177 return fold_non_dependent_expr_sfinae (expr
, tf_error
);
5180 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5181 must be a function or a pointer-to-function type, as specified
5182 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5183 and check that the resulting function has external linkage. */
5186 convert_nontype_argument_function (tree type
, tree expr
)
5191 fn
= instantiate_type (type
, fns
, tf_none
);
5192 if (fn
== error_mark_node
)
5193 return error_mark_node
;
5196 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5197 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5198 if (TREE_CODE (fn_no_ptr
) == BASELINK
)
5199 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5201 /* [temp.arg.nontype]/1
5203 A template-argument for a non-type, non-template template-parameter
5206 -- the address of an object or function with external linkage. */
5207 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr
))
5209 error ("%qE is not a valid template argument for type %qT "
5210 "because function %qD has not external linkage",
5211 expr
, type
, fn_no_ptr
);
5218 /* Subroutine of convert_nontype_argument.
5219 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5220 Emit an error otherwise. */
5223 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5224 tsubst_flags_t complain
)
5227 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5229 if (complain
& tf_error
)
5231 error ("%qE is not a valid template argument for type %qT",
5233 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5238 /* Returns TRUE iff the address of OP is value-dependent.
5240 14.6.2.4 [temp.dep.temp]:
5241 A non-integral non-type template-argument is dependent if its type is
5242 dependent or it has either of the following forms
5245 and contains a nested-name-specifier which specifies a class-name that
5246 names a dependent type.
5248 We generalize this to just say that the address of a member of a
5249 dependent class is value-dependent; the above doesn't cover the
5250 address of a static data member named with an unqualified-id. */
5253 has_value_dependent_address (tree op
)
5255 /* We could use get_inner_reference here, but there's no need;
5256 this is only relevant for template non-type arguments, which
5257 can only be expressed as &id-expression. */
5260 tree ctx
= CP_DECL_CONTEXT (op
);
5261 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5268 /* Attempt to convert the non-type template parameter EXPR to the
5269 indicated TYPE. If the conversion is successful, return the
5270 converted value. If the conversion is unsuccessful, return
5271 NULL_TREE if we issued an error message, or error_mark_node if we
5272 did not. We issue error messages for out-and-out bad template
5273 parameters, but not simply because the conversion failed, since we
5274 might be just trying to do argument deduction. Both TYPE and EXPR
5275 must be non-dependent.
5277 The conversion follows the special rules described in
5278 [temp.arg.nontype], and it is much more strict than an implicit
5281 This function is called twice for each template argument (see
5282 lookup_template_class for a more accurate description of this
5283 problem). This means that we need to handle expressions which
5284 are not valid in a C++ source, but can be created from the
5285 first call (for instance, casts to perform conversions). These
5286 hacks can go away after we fix the double coercion problem. */
5289 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5293 /* Detect immediately string literals as invalid non-type argument.
5294 This special-case is not needed for correctness (we would easily
5295 catch this later), but only to provide better diagnostic for this
5296 common user mistake. As suggested by DR 100, we do not mention
5297 linkage issues in the diagnostic as this is not the point. */
5298 /* FIXME we're making this OK. */
5299 if (TREE_CODE (expr
) == STRING_CST
)
5301 if (complain
& tf_error
)
5302 error ("%qE is not a valid template argument for type %qT "
5303 "because string literals can never be used in this context",
5308 /* Add the ADDR_EXPR now for the benefit of
5309 value_dependent_expression_p. */
5310 if (TYPE_PTROBV_P (type
)
5311 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
5312 expr
= decay_conversion (expr
);
5314 /* If we are in a template, EXPR may be non-dependent, but still
5315 have a syntactic, rather than semantic, form. For example, EXPR
5316 might be a SCOPE_REF, rather than the VAR_DECL to which the
5317 SCOPE_REF refers. Preserving the qualifying scope is necessary
5318 so that access checking can be performed when the template is
5319 instantiated -- but here we need the resolved form so that we can
5320 convert the argument. */
5321 if (TYPE_REF_OBJ_P (type
)
5322 && has_value_dependent_address (expr
))
5323 /* If we want the address and it's value-dependent, don't fold. */;
5324 else if (!type_unknown_p (expr
))
5325 expr
= fold_non_dependent_expr_sfinae (expr
, complain
);
5326 if (error_operand_p (expr
))
5327 return error_mark_node
;
5328 expr_type
= TREE_TYPE (expr
);
5329 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5330 expr
= mark_lvalue_use (expr
);
5332 expr
= mark_rvalue_use (expr
);
5334 /* HACK: Due to double coercion, we can get a
5335 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5336 which is the tree that we built on the first call (see
5337 below when coercing to reference to object or to reference to
5338 function). We just strip everything and get to the arg.
5339 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5341 if (TREE_CODE (expr
) == NOP_EXPR
)
5343 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5345 /* ??? Maybe we could use convert_from_reference here, but we
5346 would need to relax its constraints because the NOP_EXPR
5347 could actually change the type to something more cv-qualified,
5348 and this is not folded by convert_from_reference. */
5349 tree addr
= TREE_OPERAND (expr
, 0);
5350 gcc_assert (TREE_CODE (expr_type
) == REFERENCE_TYPE
);
5351 gcc_assert (TREE_CODE (addr
) == ADDR_EXPR
);
5352 gcc_assert (TREE_CODE (TREE_TYPE (addr
)) == POINTER_TYPE
);
5353 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5354 (TREE_TYPE (expr_type
),
5355 TREE_TYPE (TREE_TYPE (addr
))));
5357 expr
= TREE_OPERAND (addr
, 0);
5358 expr_type
= TREE_TYPE (expr
);
5361 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5362 parameter is a pointer to object, through decay and
5363 qualification conversion. Let's strip everything. */
5364 else if (TYPE_PTROBV_P (type
))
5367 gcc_assert (TREE_CODE (expr
) == ADDR_EXPR
);
5368 gcc_assert (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
);
5369 /* Skip the ADDR_EXPR only if it is part of the decay for
5370 an array. Otherwise, it is part of the original argument
5371 in the source code. */
5372 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == ARRAY_TYPE
)
5373 expr
= TREE_OPERAND (expr
, 0);
5374 expr_type
= TREE_TYPE (expr
);
5378 /* [temp.arg.nontype]/5, bullet 1
5380 For a non-type template-parameter of integral or enumeration type,
5381 integral promotions (_conv.prom_) and integral conversions
5382 (_conv.integral_) are applied. */
5383 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5385 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
5386 t
= maybe_constant_value (t
);
5387 if (t
!= error_mark_node
)
5390 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
5391 return error_mark_node
;
5393 /* Notice that there are constant expressions like '4 % 0' which
5394 do not fold into integer constants. */
5395 if (TREE_CODE (expr
) != INTEGER_CST
)
5397 if (complain
& tf_error
)
5399 int errs
= errorcount
, warns
= warningcount
;
5400 expr
= cxx_constant_value (expr
);
5401 if (errorcount
> errs
|| warningcount
> warns
)
5402 inform (EXPR_LOC_OR_HERE (expr
),
5403 "in template argument for type %qT ", type
);
5404 if (expr
== error_mark_node
)
5406 /* else cxx_constant_value complained but gave us
5407 a real constant, so go ahead. */
5408 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
5414 /* [temp.arg.nontype]/5, bullet 2
5416 For a non-type template-parameter of type pointer to object,
5417 qualification conversions (_conv.qual_) and the array-to-pointer
5418 conversion (_conv.array_) are applied. */
5419 else if (TYPE_PTROBV_P (type
))
5421 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5423 A template-argument for a non-type, non-template template-parameter
5424 shall be one of: [...]
5426 -- the name of a non-type template-parameter;
5427 -- the address of an object or function with external linkage, [...]
5428 expressed as "& id-expression" where the & is optional if the name
5429 refers to a function or array, or if the corresponding
5430 template-parameter is a reference.
5432 Here, we do not care about functions, as they are invalid anyway
5433 for a parameter of type pointer-to-object. */
5435 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5436 /* Non-type template parameters are OK. */
5438 else if (TREE_CODE (expr
) != ADDR_EXPR
5439 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5441 if (TREE_CODE (expr
) == VAR_DECL
)
5443 error ("%qD is not a valid template argument "
5444 "because %qD is a variable, not the address of "
5449 /* Other values, like integer constants, might be valid
5450 non-type arguments of some other type. */
5451 return error_mark_node
;
5457 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
5458 ? TREE_OPERAND (expr
, 0) : expr
);
5459 if (TREE_CODE (decl
) != VAR_DECL
)
5461 error ("%qE is not a valid template argument of type %qT "
5462 "because %qE is not a variable",
5466 else if (!DECL_EXTERNAL_LINKAGE_P (decl
))
5468 error ("%qE is not a valid template argument of type %qT "
5469 "because %qD does not have external linkage",
5475 expr
= decay_conversion (expr
);
5476 if (expr
== error_mark_node
)
5477 return error_mark_node
;
5479 expr
= perform_qualification_conversions (type
, expr
);
5480 if (expr
== error_mark_node
)
5481 return error_mark_node
;
5483 /* [temp.arg.nontype]/5, bullet 3
5485 For a non-type template-parameter of type reference to object, no
5486 conversions apply. The type referred to by the reference may be more
5487 cv-qualified than the (otherwise identical) type of the
5488 template-argument. The template-parameter is bound directly to the
5489 template-argument, which must be an lvalue. */
5490 else if (TYPE_REF_OBJ_P (type
))
5492 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
5494 return error_mark_node
;
5496 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5498 error ("%qE is not a valid template argument for type %qT "
5499 "because of conflicts in cv-qualification", expr
, type
);
5503 if (!real_lvalue_p (expr
))
5505 error ("%qE is not a valid template argument for type %qT "
5506 "because it is not an lvalue", expr
, type
);
5510 /* [temp.arg.nontype]/1
5512 A template-argument for a non-type, non-template template-parameter
5513 shall be one of: [...]
5515 -- the address of an object or function with external linkage. */
5516 if (TREE_CODE (expr
) == INDIRECT_REF
5517 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5519 expr
= TREE_OPERAND (expr
, 0);
5522 error ("%q#D is not a valid template argument for type %qT "
5523 "because a reference variable does not have a constant "
5524 "address", expr
, type
);
5531 error ("%qE is not a valid template argument for type %qT "
5532 "because it is not an object with external linkage",
5537 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5539 error ("%qE is not a valid template argument for type %qT "
5540 "because object %qD has not external linkage",
5545 expr
= build_nop (type
, build_address (expr
));
5547 /* [temp.arg.nontype]/5, bullet 4
5549 For a non-type template-parameter of type pointer to function, only
5550 the function-to-pointer conversion (_conv.func_) is applied. If the
5551 template-argument represents a set of overloaded functions (or a
5552 pointer to such), the matching function is selected from the set
5554 else if (TYPE_PTRFN_P (type
))
5556 /* If the argument is a template-id, we might not have enough
5557 context information to decay the pointer. */
5558 if (!type_unknown_p (expr_type
))
5560 expr
= decay_conversion (expr
);
5561 if (expr
== error_mark_node
)
5562 return error_mark_node
;
5565 expr
= convert_nontype_argument_function (type
, expr
);
5566 if (!expr
|| expr
== error_mark_node
)
5569 if (TREE_CODE (expr
) != ADDR_EXPR
)
5571 error ("%qE is not a valid template argument for type %qT", expr
, type
);
5572 error ("it must be the address of a function with external linkage");
5576 /* [temp.arg.nontype]/5, bullet 5
5578 For a non-type template-parameter of type reference to function, no
5579 conversions apply. If the template-argument represents a set of
5580 overloaded functions, the matching function is selected from the set
5582 else if (TYPE_REFFN_P (type
))
5584 if (TREE_CODE (expr
) == ADDR_EXPR
)
5586 error ("%qE is not a valid template argument for type %qT "
5587 "because it is a pointer", expr
, type
);
5588 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
5592 expr
= convert_nontype_argument_function (TREE_TYPE (type
), expr
);
5593 if (!expr
|| expr
== error_mark_node
)
5596 expr
= build_nop (type
, build_address (expr
));
5598 /* [temp.arg.nontype]/5, bullet 6
5600 For a non-type template-parameter of type pointer to member function,
5601 no conversions apply. If the template-argument represents a set of
5602 overloaded member functions, the matching member function is selected
5603 from the set (_over.over_). */
5604 else if (TYPE_PTRMEMFUNC_P (type
))
5606 expr
= instantiate_type (type
, expr
, tf_none
);
5607 if (expr
== error_mark_node
)
5608 return error_mark_node
;
5610 /* [temp.arg.nontype] bullet 1 says the pointer to member
5611 expression must be a pointer-to-member constant. */
5612 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5613 return error_mark_node
;
5615 /* There is no way to disable standard conversions in
5616 resolve_address_of_overloaded_function (called by
5617 instantiate_type). It is possible that the call succeeded by
5618 converting &B::I to &D::I (where B is a base of D), so we need
5619 to reject this conversion here.
5621 Actually, even if there was a way to disable standard conversions,
5622 it would still be better to reject them here so that we can
5623 provide a superior diagnostic. */
5624 if (!same_type_p (TREE_TYPE (expr
), type
))
5626 error ("%qE is not a valid template argument for type %qT "
5627 "because it is of type %qT", expr
, type
,
5629 /* If we are just one standard conversion off, explain. */
5630 if (can_convert (type
, TREE_TYPE (expr
)))
5631 inform (input_location
,
5632 "standard conversions are not allowed in this context");
5636 /* [temp.arg.nontype]/5, bullet 7
5638 For a non-type template-parameter of type pointer to data member,
5639 qualification conversions (_conv.qual_) are applied. */
5640 else if (TYPE_PTRMEM_P (type
))
5642 /* [temp.arg.nontype] bullet 1 says the pointer to member
5643 expression must be a pointer-to-member constant. */
5644 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5645 return error_mark_node
;
5647 expr
= perform_qualification_conversions (type
, expr
);
5648 if (expr
== error_mark_node
)
5651 /* A template non-type parameter must be one of the above. */
5655 /* Sanity check: did we actually convert the argument to the
5657 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5658 (type
, TREE_TYPE (expr
)));
5662 /* Subroutine of coerce_template_template_parms, which returns 1 if
5663 PARM_PARM and ARG_PARM match using the rule for the template
5664 parameters of template template parameters. Both PARM and ARG are
5665 template parameters; the rest of the arguments are the same as for
5666 coerce_template_template_parms.
5669 coerce_template_template_parm (tree parm
,
5671 tsubst_flags_t complain
,
5675 if (arg
== NULL_TREE
|| arg
== error_mark_node
5676 || parm
== NULL_TREE
|| parm
== error_mark_node
)
5679 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5682 switch (TREE_CODE (parm
))
5685 /* We encounter instantiations of templates like
5686 template <template <template <class> class> class TT>
5689 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
5690 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
5692 if (!coerce_template_template_parms
5693 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
5699 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
5700 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5701 /* Argument is a parameter pack but parameter is not. */
5706 /* The tsubst call is used to handle cases such as
5708 template <int> class C {};
5709 template <class T, template <T> class TT> class D {};
5712 i.e. the parameter list of TT depends on earlier parameters. */
5713 if (!uses_template_parms (TREE_TYPE (arg
))
5715 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
5719 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
5720 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
5721 /* Argument is a parameter pack but parameter is not. */
5734 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5735 template template parameters. Both PARM_PARMS and ARG_PARMS are
5736 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5739 Consider the example:
5740 template <class T> class A;
5741 template<template <class U> class TT> class B;
5743 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5744 the parameters to A, and OUTER_ARGS contains A. */
5747 coerce_template_template_parms (tree parm_parms
,
5749 tsubst_flags_t complain
,
5753 int nparms
, nargs
, i
;
5757 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
5758 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
5760 nparms
= TREE_VEC_LENGTH (parm_parms
);
5761 nargs
= TREE_VEC_LENGTH (arg_parms
);
5763 /* Determine whether we have a parameter pack at the end of the
5764 template template parameter's template parameter list. */
5765 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
5767 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
5769 if (parm
== error_mark_node
)
5772 switch (TREE_CODE (parm
))
5776 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5781 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
5791 && !(variadic_p
&& nargs
>= nparms
- 1))
5794 /* Check all of the template parameters except the parameter pack at
5795 the end (if any). */
5796 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
5798 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
5799 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
5802 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
5803 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
5805 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
5813 /* Check each of the template parameters in the template
5814 argument against the template parameter pack at the end of
5815 the template template parameter. */
5816 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
5819 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
5821 for (; i
< nargs
; ++i
)
5823 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
5826 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
5828 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
5837 /* Verifies that the deduced template arguments (in TARGS) for the
5838 template template parameters (in TPARMS) represent valid bindings,
5839 by comparing the template parameter list of each template argument
5840 to the template parameter list of its corresponding template
5841 template parameter, in accordance with DR150. This
5842 routine can only be called after all template arguments have been
5843 deduced. It will return TRUE if all of the template template
5844 parameter bindings are okay, FALSE otherwise. */
5846 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
5848 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
5851 /* We're dealing with template parms in this process. */
5852 ++processing_template_decl
;
5854 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
5856 for (i
= 0; i
< ntparms
; ++i
)
5858 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
5859 tree targ
= TREE_VEC_ELT (targs
, i
);
5861 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
5863 tree packed_args
= NULL_TREE
;
5866 if (ARGUMENT_PACK_P (targ
))
5868 /* Look inside the argument pack. */
5869 packed_args
= ARGUMENT_PACK_ARGS (targ
);
5870 len
= TREE_VEC_LENGTH (packed_args
);
5873 for (idx
= 0; idx
< len
; ++idx
)
5875 tree targ_parms
= NULL_TREE
;
5878 /* Extract the next argument from the argument
5880 targ
= TREE_VEC_ELT (packed_args
, idx
);
5882 if (PACK_EXPANSION_P (targ
))
5883 /* Look at the pattern of the pack expansion. */
5884 targ
= PACK_EXPANSION_PATTERN (targ
);
5886 /* Extract the template parameters from the template
5888 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
5889 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
5890 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
5891 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
5893 /* Verify that we can coerce the template template
5894 parameters from the template argument to the template
5895 parameter. This requires an exact match. */
5897 && !coerce_template_template_parms
5898 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
5913 --processing_template_decl
;
5917 /* Convert the indicated template ARG as necessary to match the
5918 indicated template PARM. Returns the converted ARG, or
5919 error_mark_node if the conversion was unsuccessful. Error and
5920 warning messages are issued under control of COMPLAIN. This
5921 conversion is for the Ith parameter in the parameter list. ARGS is
5922 the full set of template arguments deduced so far. */
5925 convert_template_argument (tree parm
,
5928 tsubst_flags_t complain
,
5934 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
5936 if (TREE_CODE (arg
) == TREE_LIST
5937 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
5939 /* The template argument was the name of some
5940 member function. That's usually
5941 invalid, but static members are OK. In any
5942 case, grab the underlying fields/functions
5943 and issue an error later if required. */
5944 orig_arg
= TREE_VALUE (arg
);
5945 TREE_TYPE (arg
) = unknown_type_node
;
5950 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
5951 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
5952 || requires_tmpl_type
);
5954 /* When determining whether an argument pack expansion is a template,
5955 look at the pattern. */
5956 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
5957 arg
= PACK_EXPANSION_PATTERN (arg
);
5959 /* Deal with an injected-class-name used as a template template arg. */
5960 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
5962 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
5963 if (TREE_CODE (t
) == TEMPLATE_DECL
)
5965 if (cxx_dialect
>= cxx0x
)
5966 /* OK under DR 1004. */;
5967 else if (complain
& tf_warning_or_error
)
5968 pedwarn (input_location
, OPT_pedantic
, "injected-class-name %qD"
5969 " used as template template argument", TYPE_NAME (arg
));
5970 else if (flag_pedantic_errors
)
5978 ((TREE_CODE (arg
) == TEMPLATE_DECL
5979 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
5980 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
5981 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
5984 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
5985 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
5986 arg
= TYPE_STUB_DECL (arg
);
5988 is_type
= TYPE_P (arg
) || is_tmpl_type
;
5990 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
5991 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
5993 permerror (input_location
, "to refer to a type member of a template parameter, "
5994 "use %<typename %E%>", orig_arg
);
5996 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
5997 TREE_OPERAND (arg
, 1),
5999 complain
& tf_error
);
6003 if (is_type
!= requires_type
)
6007 if (complain
& tf_error
)
6009 error ("type/value mismatch at argument %d in template "
6010 "parameter list for %qD",
6013 error (" expected a constant of type %qT, got %qT",
6015 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6016 else if (requires_tmpl_type
)
6017 error (" expected a class template, got %qE", orig_arg
);
6019 error (" expected a type, got %qE", orig_arg
);
6022 return error_mark_node
;
6024 if (is_tmpl_type
^ requires_tmpl_type
)
6026 if (in_decl
&& (complain
& tf_error
))
6028 error ("type/value mismatch at argument %d in template "
6029 "parameter list for %qD",
6032 error (" expected a type, got %qT", DECL_NAME (arg
));
6034 error (" expected a class template, got %qT", orig_arg
);
6036 return error_mark_node
;
6041 if (requires_tmpl_type
)
6043 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6044 /* The number of argument required is not known yet.
6045 Just accept it for now. */
6046 val
= TREE_TYPE (arg
);
6049 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6052 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6054 if (coerce_template_template_parms (parmparm
, argparm
,
6060 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6062 if (val
!= error_mark_node
)
6064 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6065 val
= TREE_TYPE (val
);
6066 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6067 val
= make_pack_expansion (val
);
6072 if (in_decl
&& (complain
& tf_error
))
6074 error ("type/value mismatch at argument %d in "
6075 "template parameter list for %qD",
6077 error (" expected a template of type %qD, got %qT",
6081 val
= error_mark_node
;
6087 /* We only form one instance of each template specialization.
6088 Therefore, if we use a non-canonical variant (i.e., a
6089 typedef), any future messages referring to the type will use
6090 the typedef, which is confusing if those future uses do not
6091 themselves also use the typedef. */
6093 val
= strip_typedefs (val
);
6097 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6099 if (invalid_nontype_parm_type_p (t
, complain
))
6100 return error_mark_node
;
6102 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6104 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6108 /* Not sure if this is reachable, but it doesn't hurt
6110 error ("type mismatch in nontype parameter pack");
6111 val
= error_mark_node
;
6114 else if (!uses_template_parms (orig_arg
) && !uses_template_parms (t
))
6115 /* We used to call digest_init here. However, digest_init
6116 will report errors, which we don't want when complain
6117 is zero. More importantly, digest_init will try too
6118 hard to convert things: for example, `0' should not be
6119 converted to pointer type at this point according to
6120 the standard. Accepting this is not merely an
6121 extension, since deciding whether or not these
6122 conversions can occur is part of determining which
6123 function template to call, or whether a given explicit
6124 argument specification is valid. */
6125 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6129 if (val
== NULL_TREE
)
6130 val
= error_mark_node
;
6131 else if (val
== error_mark_node
&& (complain
& tf_error
))
6132 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6134 if (TREE_CODE (val
) == SCOPE_REF
)
6136 /* Strip typedefs from the SCOPE_REF. */
6137 tree type
= strip_typedefs (TREE_TYPE (val
));
6138 tree scope
= strip_typedefs (TREE_OPERAND (val
, 0));
6139 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6140 QUALIFIED_NAME_IS_TEMPLATE (val
));
6147 /* Coerces the remaining template arguments in INNER_ARGS (from
6148 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6149 Returns the coerced argument pack. PARM_IDX is the position of this
6150 parameter in the template parameter list. ARGS is the original
6151 template argument list. */
6153 coerce_template_parameter_pack (tree parms
,
6161 tsubst_flags_t complain
)
6163 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6164 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6167 tree packed_types
= NULL_TREE
;
6169 if (arg_idx
> nargs
)
6172 packed_args
= make_tree_vec (nargs
- arg_idx
);
6174 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
6175 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
6177 /* When the template parameter is a non-type template
6178 parameter pack whose type uses parameter packs, we need
6179 to look at each of the template arguments
6180 separately. Build a vector of the types for these
6181 non-type template parameters in PACKED_TYPES. */
6183 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
6184 packed_types
= tsubst_pack_expansion (expansion
, args
,
6187 if (packed_types
== error_mark_node
)
6188 return error_mark_node
;
6190 /* Check that we have the right number of arguments. */
6192 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
6193 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
6196 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
6197 error ("wrong number of template arguments (%d, should be %d)",
6198 nargs
, needed_parms
);
6199 return error_mark_node
;
6202 /* If we aren't able to check the actual arguments now
6203 (because they haven't been expanded yet), we can at least
6204 verify that all of the types used for the non-type
6205 template parameter pack are, in fact, valid for non-type
6206 template parameters. */
6208 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6210 int j
, len
= TREE_VEC_LENGTH (packed_types
);
6211 for (j
= 0; j
< len
; ++j
)
6213 tree t
= TREE_VEC_ELT (packed_types
, j
);
6214 if (invalid_nontype_parm_type_p (t
, complain
))
6215 return error_mark_node
;
6220 /* Convert the remaining arguments, which will be a part of the
6221 parameter pack "parm". */
6222 for (; arg_idx
< nargs
; ++arg_idx
)
6224 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6225 tree actual_parm
= TREE_VALUE (parm
);
6227 if (packed_types
&& !PACK_EXPANSION_P (arg
))
6229 /* When we have a vector of types (corresponding to the
6230 non-type template parameter pack that uses parameter
6231 packs in its type, as mention above), and the
6232 argument is not an expansion (which expands to a
6233 currently unknown number of arguments), clone the
6234 parm and give it the next type in PACKED_TYPES. */
6235 actual_parm
= copy_node (actual_parm
);
6236 TREE_TYPE (actual_parm
) =
6237 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
6240 if (arg
!= error_mark_node
)
6241 arg
= convert_template_argument (actual_parm
,
6242 arg
, new_args
, complain
, parm_idx
,
6244 if (arg
== error_mark_node
)
6246 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
6249 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6250 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
6251 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
6254 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
6255 TREE_TYPE (argument_pack
)
6256 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
6257 TREE_CONSTANT (argument_pack
) = 1;
6260 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
6261 #ifdef ENABLE_CHECKING
6262 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
6263 TREE_VEC_LENGTH (packed_args
));
6265 return argument_pack
;
6268 /* Convert all template arguments to their appropriate types, and
6269 return a vector containing the innermost resulting template
6270 arguments. If any error occurs, return error_mark_node. Error and
6271 warning messages are issued under control of COMPLAIN.
6273 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6274 for arguments not specified in ARGS. Otherwise, if
6275 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6276 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6277 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6281 coerce_template_parms (tree parms
,
6284 tsubst_flags_t complain
,
6285 bool require_all_args
,
6286 bool use_default_args
)
6288 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
6291 tree new_inner_args
;
6292 int saved_unevaluated_operand
;
6293 int saved_inhibit_evaluation_warnings
;
6295 /* When used as a boolean value, indicates whether this is a
6296 variadic template parameter list. Since it's an int, we can also
6297 subtract it from nparms to get the number of non-variadic
6301 if (args
== error_mark_node
)
6302 return error_mark_node
;
6304 nparms
= TREE_VEC_LENGTH (parms
);
6306 /* Determine if there are any parameter packs. */
6307 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
6309 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
6310 if (template_parameter_pack_p (tparm
))
6314 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
6315 /* If there are 0 or 1 parameter packs, we need to expand any argument
6316 packs so that we can deduce a parameter pack from some non-packed args
6317 followed by an argument pack, as in variadic85.C. If there are more
6318 than that, we need to leave argument packs intact so the arguments are
6319 assigned to the right parameter packs. This should only happen when
6320 dealing with a nested class inside a partial specialization of a class
6321 template, as in variadic92.C. */
6322 if (variadic_p
<= 1)
6323 inner_args
= expand_template_argument_pack (inner_args
);
6325 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6326 if ((nargs
> nparms
&& !variadic_p
)
6327 || (nargs
< nparms
- variadic_p
6329 && (!use_default_args
6330 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
6331 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
6333 if (complain
& tf_error
)
6338 error ("wrong number of template arguments "
6339 "(%d, should be %d or more)", nargs
, nparms
);
6342 error ("wrong number of template arguments "
6343 "(%d, should be %d)", nargs
, nparms
);
6346 error ("provided for %q+D", in_decl
);
6349 return error_mark_node
;
6352 /* We need to evaluate the template arguments, even though this
6353 template-id may be nested within a "sizeof". */
6354 saved_unevaluated_operand
= cp_unevaluated_operand
;
6355 cp_unevaluated_operand
= 0;
6356 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6357 c_inhibit_evaluation_warnings
= 0;
6358 new_inner_args
= make_tree_vec (nparms
);
6359 new_args
= add_outermost_template_args (args
, new_inner_args
);
6360 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
6365 /* Get the Ith template parameter. */
6366 parm
= TREE_VEC_ELT (parms
, parm_idx
);
6368 if (parm
== error_mark_node
)
6370 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
6374 /* Calculate the next argument. */
6375 if (arg_idx
< nargs
)
6376 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6380 if (template_parameter_pack_p (TREE_VALUE (parm
))
6381 && !(arg
&& ARGUMENT_PACK_P (arg
)))
6383 /* All remaining arguments will be placed in the
6384 template parameter pack PARM. */
6385 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
6386 inner_args
, arg_idx
,
6390 /* Store this argument. */
6391 if (arg
== error_mark_node
)
6393 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
6395 /* We are done with all of the arguments. */
6402 if (PACK_EXPANSION_P (arg
))
6404 if (complain
& tf_error
)
6406 /* FIXME this restriction was removed by N2555; see
6408 /* If ARG is a pack expansion, but PARM is not a
6409 template parameter pack (if it were, we would have
6410 handled it above), we're trying to expand into a
6411 fixed-length argument list. */
6412 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
6413 sorry ("cannot expand %<%E%> into a fixed-length "
6414 "argument list", arg
);
6416 sorry ("cannot expand %<%T%> into a fixed-length "
6417 "argument list", arg
);
6422 else if (require_all_args
)
6424 /* There must be a default arg in this case. */
6425 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
6427 /* The position of the first default template argument,
6428 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6430 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6431 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
, arg_idx
);
6436 if (arg
== error_mark_node
)
6438 if (complain
& tf_error
)
6439 error ("template argument %d is invalid", arg_idx
+ 1);
6442 /* This only occurs if there was an error in the template
6443 parameter list itself (which we would already have
6444 reported) that we are trying to recover from, e.g., a class
6445 template with a parameter list such as
6446 template<typename..., typename>. */
6449 arg
= convert_template_argument (TREE_VALUE (parm
),
6450 arg
, new_args
, complain
,
6453 if (arg
== error_mark_node
)
6455 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
6457 cp_unevaluated_operand
= saved_unevaluated_operand
;
6458 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
6461 return error_mark_node
;
6463 #ifdef ENABLE_CHECKING
6464 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6465 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6466 TREE_VEC_LENGTH (new_inner_args
));
6469 return new_inner_args
;
6472 /* Returns 1 if template args OT and NT are equivalent. */
6475 template_args_equal (tree ot
, tree nt
)
6480 if (TREE_CODE (nt
) == TREE_VEC
)
6481 /* For member templates */
6482 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
6483 else if (PACK_EXPANSION_P (ot
))
6484 return PACK_EXPANSION_P (nt
)
6485 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
6486 PACK_EXPANSION_PATTERN (nt
));
6487 else if (ARGUMENT_PACK_P (ot
))
6492 if (!ARGUMENT_PACK_P (nt
))
6495 opack
= ARGUMENT_PACK_ARGS (ot
);
6496 npack
= ARGUMENT_PACK_ARGS (nt
);
6497 len
= TREE_VEC_LENGTH (opack
);
6498 if (TREE_VEC_LENGTH (npack
) != len
)
6500 for (i
= 0; i
< len
; ++i
)
6501 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
6502 TREE_VEC_ELT (npack
, i
)))
6506 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
6508 /* We get here probably because we are in the middle of substituting
6509 into the pattern of a pack expansion. In that case the
6510 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6511 interested in. So we want to use the initial pack argument for
6513 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
6514 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
6515 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
6516 return template_args_equal (ot
, nt
);
6518 else if (TYPE_P (nt
))
6519 return TYPE_P (ot
) && same_type_p (ot
, nt
);
6520 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
6523 return cp_tree_equal (ot
, nt
);
6526 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6527 of template arguments. Returns 0 otherwise. */
6530 comp_template_args (tree oldargs
, tree newargs
)
6534 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
6537 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
6539 tree nt
= TREE_VEC_ELT (newargs
, i
);
6540 tree ot
= TREE_VEC_ELT (oldargs
, i
);
6542 if (! template_args_equal (ot
, nt
))
6549 add_pending_template (tree d
)
6551 tree ti
= (TYPE_P (d
)
6552 ? CLASSTYPE_TEMPLATE_INFO (d
)
6553 : DECL_TEMPLATE_INFO (d
));
6554 struct pending_template
*pt
;
6557 if (TI_PENDING_TEMPLATE_FLAG (ti
))
6560 /* We are called both from instantiate_decl, where we've already had a
6561 tinst_level pushed, and instantiate_template, where we haven't.
6563 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
6566 push_tinst_level (d
);
6568 pt
= ggc_alloc_pending_template ();
6570 pt
->tinst
= current_tinst_level
;
6571 if (last_pending_template
)
6572 last_pending_template
->next
= pt
;
6574 pending_templates
= pt
;
6576 last_pending_template
= pt
;
6578 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
6585 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6586 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6587 documentation for TEMPLATE_ID_EXPR. */
6590 lookup_template_function (tree fns
, tree arglist
)
6594 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
6595 return error_mark_node
;
6597 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
6598 gcc_assert (fns
&& (is_overloaded_fn (fns
)
6599 || TREE_CODE (fns
) == IDENTIFIER_NODE
));
6601 if (BASELINK_P (fns
))
6603 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
6605 BASELINK_FUNCTIONS (fns
),
6610 type
= TREE_TYPE (fns
);
6611 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
6612 type
= unknown_type_node
;
6614 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
6617 /* Within the scope of a template class S<T>, the name S gets bound
6618 (in build_self_reference) to a TYPE_DECL for the class, not a
6619 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6620 or one of its enclosing classes, and that type is a template,
6621 return the associated TEMPLATE_DECL. Otherwise, the original
6624 Also handle the case when DECL is a TREE_LIST of ambiguous
6625 injected-class-names from different bases. */
6628 maybe_get_template_decl_from_type_decl (tree decl
)
6630 if (decl
== NULL_TREE
)
6633 /* DR 176: A lookup that finds an injected-class-name (10.2
6634 [class.member.lookup]) can result in an ambiguity in certain cases
6635 (for example, if it is found in more than one base class). If all of
6636 the injected-class-names that are found refer to specializations of
6637 the same class template, and if the name is followed by a
6638 template-argument-list, the reference refers to the class template
6639 itself and not a specialization thereof, and is not ambiguous. */
6640 if (TREE_CODE (decl
) == TREE_LIST
)
6642 tree t
, tmpl
= NULL_TREE
;
6643 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
6645 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
6648 else if (tmpl
!= elt
)
6651 if (tmpl
&& t
== NULL_TREE
)
6657 return (decl
!= NULL_TREE
6658 && DECL_SELF_REFERENCE_P (decl
)
6659 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
6660 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
6663 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6664 parameters, find the desired type.
6666 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6668 IN_DECL, if non-NULL, is the template declaration we are trying to
6671 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6672 the class we are looking up.
6674 Issue error and warning messages under control of COMPLAIN.
6676 If the template class is really a local class in a template
6677 function, then the FUNCTION_CONTEXT is the function in which it is
6680 ??? Note that this function is currently called *twice* for each
6681 template-id: the first time from the parser, while creating the
6682 incomplete type (finish_template_type), and the second type during the
6683 real instantiation (instantiate_template_class). This is surely something
6684 that we want to avoid. It also causes some problems with argument
6685 coercion (see convert_nontype_argument for more information on this). */
6688 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
6689 int entering_scope
, tsubst_flags_t complain
)
6691 tree templ
= NULL_TREE
, parmlist
;
6698 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
6700 tree value
= innermost_non_namespace_value (d1
);
6701 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
6706 push_decl_namespace (context
);
6707 templ
= lookup_name (d1
);
6708 templ
= maybe_get_template_decl_from_type_decl (templ
);
6710 pop_decl_namespace ();
6713 context
= DECL_CONTEXT (templ
);
6715 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
6717 tree type
= TREE_TYPE (d1
);
6719 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6720 an implicit typename for the second A. Deal with it. */
6721 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
6722 type
= TREE_TYPE (type
);
6724 if (CLASSTYPE_TEMPLATE_INFO (type
))
6726 templ
= CLASSTYPE_TI_TEMPLATE (type
);
6727 d1
= DECL_NAME (templ
);
6730 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
6731 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
6733 templ
= TYPE_TI_TEMPLATE (d1
);
6734 d1
= DECL_NAME (templ
);
6736 else if (TREE_CODE (d1
) == TEMPLATE_DECL
6737 && DECL_TEMPLATE_RESULT (d1
)
6738 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
6741 d1
= DECL_NAME (templ
);
6742 context
= DECL_CONTEXT (templ
);
6745 /* Issue an error message if we didn't find a template. */
6748 if (complain
& tf_error
)
6749 error ("%qT is not a template", d1
);
6750 return error_mark_node
;
6753 if (TREE_CODE (templ
) != TEMPLATE_DECL
6754 /* Make sure it's a user visible template, if it was named by
6756 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
6757 && !PRIMARY_TEMPLATE_P (templ
)))
6759 if (complain
& tf_error
)
6761 error ("non-template type %qT used as a template", d1
);
6763 error ("for template declaration %q+D", in_decl
);
6765 return error_mark_node
;
6768 complain
&= ~tf_user
;
6770 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
6772 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
6773 template arguments */
6779 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
6781 /* Consider an example where a template template parameter declared as
6783 template <class T, class U = std::allocator<T> > class TT
6785 The template parameter level of T and U are one level larger than
6786 of TT. To proper process the default argument of U, say when an
6787 instantiation `TT<int>' is seen, we need to build the full
6788 arguments containing {int} as the innermost level. Outer levels,
6789 available when not appearing as default template argument, can be
6790 obtained from the arguments of the enclosing template.
6792 Suppose that TT is later substituted with std::vector. The above
6793 instantiation is `TT<int, std::allocator<T> >' with TT at
6794 level 1, and T at level 2, while the template arguments at level 1
6795 becomes {std::vector} and the inner level 2 is {int}. */
6797 outer
= DECL_CONTEXT (templ
);
6799 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
6800 else if (current_template_parms
)
6801 /* This is an argument of the current template, so we haven't set
6802 DECL_CONTEXT yet. */
6803 outer
= current_template_args ();
6806 arglist
= add_to_template_args (outer
, arglist
);
6808 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
6810 /*require_all_args=*/true,
6811 /*use_default_args=*/true);
6812 if (arglist2
== error_mark_node
6813 || (!uses_template_parms (arglist2
)
6814 && check_instantiated_args (templ
, arglist2
, complain
)))
6815 return error_mark_node
;
6817 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
6822 tree template_type
= TREE_TYPE (templ
);
6825 tree found
= NULL_TREE
;
6828 int is_dependent_type
;
6829 int use_partial_inst_tmpl
= false;
6831 gen_tmpl
= most_general_template (templ
);
6832 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
6833 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
6834 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
6836 if (arg_depth
== 1 && parm_depth
> 1)
6838 /* We've been given an incomplete set of template arguments.
6841 template <class T> struct S1 {
6842 template <class U> struct S2 {};
6843 template <class U> struct S2<U*> {};
6846 we will be called with an ARGLIST of `U*', but the
6847 TEMPLATE will be `template <class T> template
6848 <class U> struct S1<T>::S2'. We must fill in the missing
6851 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
6853 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
6856 /* Now we should have enough arguments. */
6857 gcc_assert (parm_depth
== arg_depth
);
6859 /* From here on, we're only interested in the most general
6862 /* Calculate the BOUND_ARGS. These will be the args that are
6863 actually tsubst'd into the definition to create the
6867 /* We have multiple levels of arguments to coerce, at once. */
6869 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
6871 tree bound_args
= make_tree_vec (parm_depth
);
6873 for (i
= saved_depth
,
6874 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
6875 i
> 0 && t
!= NULL_TREE
;
6876 --i
, t
= TREE_CHAIN (t
))
6879 if (i
== saved_depth
)
6880 a
= coerce_template_parms (TREE_VALUE (t
),
6883 /*require_all_args=*/true,
6884 /*use_default_args=*/true);
6886 /* Outer levels should have already been coerced. */
6887 a
= TMPL_ARGS_LEVEL (arglist
, i
);
6889 /* Don't process further if one of the levels fails. */
6890 if (a
== error_mark_node
)
6892 /* Restore the ARGLIST to its full size. */
6893 TREE_VEC_LENGTH (arglist
) = saved_depth
;
6894 return error_mark_node
;
6897 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
6899 /* We temporarily reduce the length of the ARGLIST so
6900 that coerce_template_parms will see only the arguments
6901 corresponding to the template parameters it is
6903 TREE_VEC_LENGTH (arglist
)--;
6906 /* Restore the ARGLIST to its full size. */
6907 TREE_VEC_LENGTH (arglist
) = saved_depth
;
6909 arglist
= bound_args
;
6913 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
6914 INNERMOST_TEMPLATE_ARGS (arglist
),
6917 /*require_all_args=*/true,
6918 /*use_default_args=*/true);
6920 if (arglist
== error_mark_node
)
6921 /* We were unable to bind the arguments. */
6922 return error_mark_node
;
6924 /* In the scope of a template class, explicit references to the
6925 template class refer to the type of the template, not any
6926 instantiation of it. For example, in:
6928 template <class T> class C { void f(C<T>); }
6930 the `C<T>' is just the same as `C'. Outside of the
6931 class, however, such a reference is an instantiation. */
6933 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
6934 || currently_open_class (template_type
))
6935 /* comp_template_args is expensive, check it last. */
6936 && comp_template_args (TYPE_TI_ARGS (template_type
),
6938 return template_type
;
6940 /* If we already have this specialization, return it. */
6941 elt
.tmpl
= gen_tmpl
;
6943 hash
= hash_specialization (&elt
);
6944 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
6950 is_dependent_type
= uses_template_parms (arglist
);
6952 /* If the deduced arguments are invalid, then the binding
6954 if (!is_dependent_type
6955 && check_instantiated_args (gen_tmpl
,
6956 INNERMOST_TEMPLATE_ARGS (arglist
),
6958 return error_mark_node
;
6960 if (!is_dependent_type
6961 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
6962 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
6963 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
6965 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
6966 DECL_NAME (gen_tmpl
),
6967 /*tag_scope=*/ts_global
);
6971 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
6974 context
= global_namespace
;
6976 /* Create the type. */
6977 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
6979 if (!is_dependent_type
)
6981 set_current_access_from_decl (TYPE_NAME (template_type
));
6982 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
6983 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
6984 arglist
, complain
, in_decl
),
6985 SCOPED_ENUM_P (template_type
), NULL
);
6989 /* We don't want to call start_enum for this type, since
6990 the values for the enumeration constants may involve
6991 template parameters. And, no one should be interested
6992 in the enumeration constants for such a type. */
6993 t
= cxx_make_type (ENUMERAL_TYPE
);
6994 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
6996 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
6997 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
6998 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7002 t
= make_class_type (TREE_CODE (template_type
));
7003 CLASSTYPE_DECLARED_CLASS (t
)
7004 = CLASSTYPE_DECLARED_CLASS (template_type
);
7005 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7006 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7008 /* A local class. Make sure the decl gets registered properly. */
7009 if (context
== current_function_decl
)
7010 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7012 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7013 /* This instantiation is another name for the primary
7014 template type. Set the TYPE_CANONICAL field
7016 TYPE_CANONICAL (t
) = template_type
;
7017 else if (any_template_arguments_need_structural_equality_p (arglist
))
7018 /* Some of the template arguments require structural
7019 equality testing, so this template class requires
7020 structural equality testing. */
7021 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7024 /* If we called start_enum or pushtag above, this information
7025 will already be set up. */
7028 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
7030 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
7031 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
7032 DECL_SOURCE_LOCATION (type_decl
)
7033 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
7036 type_decl
= TYPE_NAME (t
);
7038 TREE_PRIVATE (type_decl
)
7039 = TREE_PRIVATE (TYPE_STUB_DECL (template_type
));
7040 TREE_PROTECTED (type_decl
)
7041 = TREE_PROTECTED (TYPE_STUB_DECL (template_type
));
7042 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
7044 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
7045 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
7048 /* Let's consider the explicit specialization of a member
7049 of a class template specialization that is implicitely instantiated,
7054 template<class U> struct M {}; //#0
7059 struct S<int>::M<char> //#1
7063 [temp.expl.spec]/4 says this is valid.
7065 In this case, when we write:
7068 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7071 When we encounter #1, we want to store the partial instantiation
7072 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
7074 For all cases other than this "explicit specialization of member of a
7075 class template", we just want to store the most general template into
7076 the CLASSTYPE_TI_TEMPLATE of M.
7078 This case of "explicit specialization of member of a class template"
7080 1/ the enclosing class is an instantiation of, and therefore not
7081 the same as, the context of the most general template, and
7082 2/ we aren't looking at the partial instantiation itself, i.e.
7083 the innermost arguments are not the same as the innermost parms of
7084 the most general template.
7086 So it's only when 1/ and 2/ happens that we want to use the partial
7087 instantiation of the member template in lieu of its most general
7090 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
7091 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
7092 /* the enclosing class must be an instantiation... */
7093 && CLASS_TYPE_P (context
)
7094 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
7096 tree partial_inst_args
;
7097 TREE_VEC_LENGTH (arglist
)--;
7098 ++processing_template_decl
;
7100 tsubst (INNERMOST_TEMPLATE_ARGS
7101 (CLASSTYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
7102 arglist
, complain
, NULL_TREE
);
7103 --processing_template_decl
;
7104 TREE_VEC_LENGTH (arglist
)++;
7105 use_partial_inst_tmpl
=
7106 /*...and we must not be looking at the partial instantiation
7108 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
7112 if (!use_partial_inst_tmpl
)
7113 /* This case is easy; there are no member templates involved. */
7117 /* This is a full instantiation of a member template. Find
7118 the partial instantiation of which this is an instance. */
7120 /* Temporarily reduce by one the number of levels in the ARGLIST
7121 so as to avoid comparing the last set of arguments. */
7122 TREE_VEC_LENGTH (arglist
)--;
7123 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
7124 TREE_VEC_LENGTH (arglist
)++;
7125 found
= CLASSTYPE_TI_TEMPLATE (found
);
7128 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
7131 slot
= (spec_entry
**) htab_find_slot_with_hash (type_specializations
,
7132 &elt
, hash
, INSERT
);
7133 *slot
= ggc_alloc_spec_entry ();
7136 /* Note this use of the partial instantiation so we can check it
7137 later in maybe_process_partial_specialization. */
7138 DECL_TEMPLATE_INSTANTIATIONS (templ
)
7139 = tree_cons (arglist
, t
,
7140 DECL_TEMPLATE_INSTANTIATIONS (templ
));
7142 if (TREE_CODE (t
) == ENUMERAL_TYPE
&& !is_dependent_type
)
7143 /* Now that the type has been registered on the instantiations
7144 list, we set up the enumerators. Because the enumeration
7145 constants may involve the enumeration type itself, we make
7146 sure to register the type first, and then create the
7147 constants. That way, doing tsubst_expr for the enumeration
7148 constants won't result in recursive calls here; we'll find
7149 the instantiation and exit above. */
7150 tsubst_enum (template_type
, t
, arglist
);
7152 if (is_dependent_type
)
7153 /* If the type makes use of template parameters, the
7154 code that generates debugging information will crash. */
7155 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
7157 /* Possibly limit visibility based on template args. */
7158 TREE_PUBLIC (type_decl
) = 1;
7159 determine_visibility (type_decl
);
7165 /* Wrapper for lookup_template_class_1. */
7168 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
7169 int entering_scope
, tsubst_flags_t complain
)
7172 timevar_push (TV_TEMPLATE_INST
);
7173 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
7174 entering_scope
, complain
);
7175 timevar_pop (TV_TEMPLATE_INST
);
7183 /* True when we should also visit template parameters that occur in
7184 non-deduced contexts. */
7185 bool include_nondeduced_p
;
7186 struct pointer_set_t
*visited
;
7189 /* Called from for_each_template_parm via walk_tree. */
7192 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
7195 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
7196 tree_fn_t fn
= pfd
->fn
;
7197 void *data
= pfd
->data
;
7200 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
7201 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
7202 pfd
->include_nondeduced_p
))
7203 return error_mark_node
;
7205 switch (TREE_CODE (t
))
7208 if (TYPE_PTRMEMFUNC_P (t
))
7214 if (!TYPE_TEMPLATE_INFO (t
))
7216 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
7217 fn
, data
, pfd
->visited
,
7218 pfd
->include_nondeduced_p
))
7219 return error_mark_node
;
7223 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
7224 fn
, data
, pfd
->visited
,
7225 pfd
->include_nondeduced_p
)
7226 || for_each_template_parm (TYPE_MAX_VALUE (t
),
7227 fn
, data
, pfd
->visited
,
7228 pfd
->include_nondeduced_p
))
7229 return error_mark_node
;
7233 /* Since we're not going to walk subtrees, we have to do this
7235 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
7236 pfd
->visited
, pfd
->include_nondeduced_p
))
7237 return error_mark_node
;
7241 /* Check the return type. */
7242 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7243 pfd
->include_nondeduced_p
))
7244 return error_mark_node
;
7246 /* Check the parameter types. Since default arguments are not
7247 instantiated until they are needed, the TYPE_ARG_TYPES may
7248 contain expressions that involve template parameters. But,
7249 no-one should be looking at them yet. And, once they're
7250 instantiated, they don't contain template parameters, so
7251 there's no point in looking at them then, either. */
7255 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
7256 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
7257 pfd
->visited
, pfd
->include_nondeduced_p
))
7258 return error_mark_node
;
7260 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7261 want walk_tree walking into them itself. */
7267 case UNDERLYING_TYPE
:
7268 if (pfd
->include_nondeduced_p
7269 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
7271 pfd
->include_nondeduced_p
))
7272 return error_mark_node
;
7277 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
7278 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
7279 pfd
->visited
, pfd
->include_nondeduced_p
))
7280 return error_mark_node
;
7285 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
7286 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
7287 pfd
->visited
, pfd
->include_nondeduced_p
))
7288 return error_mark_node
;
7289 if (DECL_CONTEXT (t
)
7290 && pfd
->include_nondeduced_p
7291 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
7292 pfd
->visited
, pfd
->include_nondeduced_p
))
7293 return error_mark_node
;
7296 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7297 /* Record template parameters such as `T' inside `TT<T>'. */
7298 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
7299 pfd
->include_nondeduced_p
))
7300 return error_mark_node
;
7303 case TEMPLATE_TEMPLATE_PARM
:
7304 case TEMPLATE_TYPE_PARM
:
7305 case TEMPLATE_PARM_INDEX
:
7306 if (fn
&& (*fn
)(t
, data
))
7307 return error_mark_node
;
7309 return error_mark_node
;
7313 /* A template template parameter is encountered. */
7314 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
7315 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7316 pfd
->include_nondeduced_p
))
7317 return error_mark_node
;
7319 /* Already substituted template template parameter */
7325 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
7327 pfd
->include_nondeduced_p
))
7328 return error_mark_node
;
7332 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
7333 && pfd
->include_nondeduced_p
7334 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7335 (TREE_TYPE (t
)), fn
, data
,
7336 pfd
->visited
, pfd
->include_nondeduced_p
))
7337 return error_mark_node
;
7342 /* If there's no type, then this thing must be some expression
7343 involving template parameters. */
7344 if (!fn
&& !TREE_TYPE (t
))
7345 return error_mark_node
;
7350 case REINTERPRET_CAST_EXPR
:
7351 case CONST_CAST_EXPR
:
7352 case STATIC_CAST_EXPR
:
7353 case DYNAMIC_CAST_EXPR
:
7357 case PSEUDO_DTOR_EXPR
:
7359 return error_mark_node
;
7366 /* We didn't find any template parameters we liked. */
7370 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7371 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7372 call FN with the parameter and the DATA.
7373 If FN returns nonzero, the iteration is terminated, and
7374 for_each_template_parm returns 1. Otherwise, the iteration
7375 continues. If FN never returns a nonzero value, the value
7376 returned by for_each_template_parm is 0. If FN is NULL, it is
7377 considered to be the function which always returns 1.
7379 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7380 parameters that occur in non-deduced contexts. When false, only
7381 visits those template parameters that can be deduced. */
7384 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
7385 struct pointer_set_t
*visited
,
7386 bool include_nondeduced_p
)
7388 struct pair_fn_data pfd
;
7394 pfd
.include_nondeduced_p
= include_nondeduced_p
;
7396 /* Walk the tree. (Conceptually, we would like to walk without
7397 duplicates, but for_each_template_parm_r recursively calls
7398 for_each_template_parm, so we would need to reorganize a fair
7399 bit to use walk_tree_without_duplicates, so we keep our own
7402 pfd
.visited
= visited
;
7404 pfd
.visited
= pointer_set_create ();
7405 result
= cp_walk_tree (&t
,
7406 for_each_template_parm_r
,
7408 pfd
.visited
) != NULL_TREE
;
7413 pointer_set_destroy (pfd
.visited
);
7420 /* Returns true if T depends on any template parameter. */
7423 uses_template_parms (tree t
)
7426 int saved_processing_template_decl
;
7428 saved_processing_template_decl
= processing_template_decl
;
7429 if (!saved_processing_template_decl
)
7430 processing_template_decl
= 1;
7432 dependent_p
= dependent_type_p (t
);
7433 else if (TREE_CODE (t
) == TREE_VEC
)
7434 dependent_p
= any_dependent_template_arguments_p (t
);
7435 else if (TREE_CODE (t
) == TREE_LIST
)
7436 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
7437 || uses_template_parms (TREE_CHAIN (t
)));
7438 else if (TREE_CODE (t
) == TYPE_DECL
)
7439 dependent_p
= dependent_type_p (TREE_TYPE (t
));
7442 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
7443 || TREE_CODE (t
) == OVERLOAD
7444 || TREE_CODE (t
) == BASELINK
7445 || TREE_CODE (t
) == IDENTIFIER_NODE
7446 || TREE_CODE (t
) == TRAIT_EXPR
7447 || TREE_CODE (t
) == CONSTRUCTOR
7448 || CONSTANT_CLASS_P (t
))
7449 dependent_p
= (type_dependent_expression_p (t
)
7450 || value_dependent_expression_p (t
));
7453 gcc_assert (t
== error_mark_node
);
7454 dependent_p
= false;
7457 processing_template_decl
= saved_processing_template_decl
;
7462 /* Returns true if T depends on any template parameter with level LEVEL. */
7465 uses_template_parms_level (tree t
, int level
)
7467 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
7468 /*include_nondeduced_p=*/true);
7471 static int tinst_depth
;
7472 extern int max_tinst_depth
;
7473 #ifdef GATHER_STATISTICS
7476 static int tinst_level_tick
;
7477 static int last_template_error_tick
;
7479 /* We're starting to instantiate D; record the template instantiation context
7480 for diagnostics and to restore it later. */
7483 push_tinst_level (tree d
)
7485 struct tinst_level
*new_level
;
7487 if (tinst_depth
>= max_tinst_depth
)
7489 /* If the instantiation in question still has unbound template parms,
7490 we don't really care if we can't instantiate it, so just return.
7491 This happens with base instantiation for implicit `typename'. */
7492 if (uses_template_parms (d
))
7495 last_template_error_tick
= tinst_level_tick
;
7496 error ("template instantiation depth exceeds maximum of %d (use "
7497 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7498 max_tinst_depth
, d
);
7500 print_instantiation_context ();
7505 new_level
= ggc_alloc_tinst_level ();
7506 new_level
->decl
= d
;
7507 new_level
->locus
= input_location
;
7508 new_level
->in_system_header_p
= in_system_header
;
7509 new_level
->next
= current_tinst_level
;
7510 current_tinst_level
= new_level
;
7513 #ifdef GATHER_STATISTICS
7514 if (tinst_depth
> depth_reached
)
7515 depth_reached
= tinst_depth
;
7522 /* We're done instantiating this template; return to the instantiation
7526 pop_tinst_level (void)
7528 /* Restore the filename and line number stashed away when we started
7529 this instantiation. */
7530 input_location
= current_tinst_level
->locus
;
7531 current_tinst_level
= current_tinst_level
->next
;
7536 /* We're instantiating a deferred template; restore the template
7537 instantiation context in which the instantiation was requested, which
7538 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7541 reopen_tinst_level (struct tinst_level
*level
)
7543 struct tinst_level
*t
;
7546 for (t
= level
; t
; t
= t
->next
)
7549 current_tinst_level
= level
;
7554 /* Returns the TINST_LEVEL which gives the original instantiation
7557 struct tinst_level
*
7558 outermost_tinst_level (void)
7560 struct tinst_level
*level
= current_tinst_level
;
7563 level
= level
->next
;
7567 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
7570 parameter_of_template_p (tree parm
, tree templ
)
7575 if (!parm
|| !templ
)
7578 gcc_assert (DECL_TEMPLATE_PARM_P (parm
));
7579 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
7581 parms
= DECL_TEMPLATE_PARMS (templ
);
7582 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
7584 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
7585 if (parm
== TREE_VALUE (TREE_VEC_ELT (parms
, i
)))
7591 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
7592 vector of template arguments, as for tsubst.
7594 Returns an appropriate tsubst'd friend declaration. */
7597 tsubst_friend_function (tree decl
, tree args
)
7601 if (TREE_CODE (decl
) == FUNCTION_DECL
7602 && DECL_TEMPLATE_INSTANTIATION (decl
)
7603 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
7604 /* This was a friend declared with an explicit template
7605 argument list, e.g.:
7609 to indicate that f was a template instantiation, not a new
7610 function declaration. Now, we have to figure out what
7611 instantiation of what template. */
7613 tree template_id
, arglist
, fns
;
7616 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
7618 /* Friend functions are looked up in the containing namespace scope.
7619 We must enter that scope, to avoid finding member functions of the
7620 current class with same name. */
7621 push_nested_namespace (ns
);
7622 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
7623 tf_warning_or_error
, NULL_TREE
,
7624 /*integral_constant_expression_p=*/false);
7625 pop_nested_namespace (ns
);
7626 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
7627 tf_warning_or_error
, NULL_TREE
);
7628 template_id
= lookup_template_function (fns
, arglist
);
7630 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
7631 tmpl
= determine_specialization (template_id
, new_friend
,
7633 /*need_member_template=*/0,
7634 TREE_VEC_LENGTH (args
),
7636 return instantiate_template (tmpl
, new_args
, tf_error
);
7639 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
7641 /* The NEW_FRIEND will look like an instantiation, to the
7642 compiler, but is not an instantiation from the point of view of
7643 the language. For example, we might have had:
7645 template <class T> struct S {
7646 template <class U> friend void f(T, U);
7649 Then, in S<int>, template <class U> void f(int, U) is not an
7650 instantiation of anything. */
7651 if (new_friend
== error_mark_node
)
7652 return error_mark_node
;
7654 DECL_USE_TEMPLATE (new_friend
) = 0;
7655 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
7657 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
7658 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
7659 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
7662 /* The mangled name for the NEW_FRIEND is incorrect. The function
7663 is not a template instantiation and should not be mangled like
7664 one. Therefore, we forget the mangling here; we'll recompute it
7665 later if we need it. */
7666 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
7668 SET_DECL_RTL (new_friend
, NULL
);
7669 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
7672 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
7675 tree new_friend_template_info
;
7676 tree new_friend_result_template_info
;
7678 int new_friend_is_defn
;
7680 /* We must save some information from NEW_FRIEND before calling
7681 duplicate decls since that function will free NEW_FRIEND if
7683 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
7684 new_friend_is_defn
=
7685 (DECL_INITIAL (DECL_TEMPLATE_RESULT
7686 (template_for_substitution (new_friend
)))
7688 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
7690 /* This declaration is a `primary' template. */
7691 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
7693 new_friend_result_template_info
7694 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
7697 new_friend_result_template_info
= NULL_TREE
;
7699 /* Make the init_value nonzero so pushdecl knows this is a defn. */
7700 if (new_friend_is_defn
)
7701 DECL_INITIAL (new_friend
) = error_mark_node
;
7703 /* Inside pushdecl_namespace_level, we will push into the
7704 current namespace. However, the friend function should go
7705 into the namespace of the template. */
7706 ns
= decl_namespace_context (new_friend
);
7707 push_nested_namespace (ns
);
7708 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
7709 pop_nested_namespace (ns
);
7711 if (old_decl
== error_mark_node
)
7712 return error_mark_node
;
7714 if (old_decl
!= new_friend
)
7716 /* This new friend declaration matched an existing
7717 declaration. For example, given:
7719 template <class T> void f(T);
7720 template <class U> class C {
7721 template <class T> friend void f(T) {}
7724 the friend declaration actually provides the definition
7725 of `f', once C has been instantiated for some type. So,
7726 old_decl will be the out-of-class template declaration,
7727 while new_friend is the in-class definition.
7729 But, if `f' was called before this point, the
7730 instantiation of `f' will have DECL_TI_ARGS corresponding
7731 to `T' but not to `U', references to which might appear
7732 in the definition of `f'. Previously, the most general
7733 template for an instantiation of `f' was the out-of-class
7734 version; now it is the in-class version. Therefore, we
7735 run through all specialization of `f', adding to their
7736 DECL_TI_ARGS appropriately. In particular, they need a
7737 new set of outer arguments, corresponding to the
7738 arguments for this class instantiation.
7740 The same situation can arise with something like this:
7743 template <class T> class C {
7747 when `C<int>' is instantiated. Now, `f(int)' is defined
7750 if (!new_friend_is_defn
)
7751 /* On the other hand, if the in-class declaration does
7752 *not* provide a definition, then we don't want to alter
7753 existing definitions. We can just leave everything
7758 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
7759 tree new_args
= TI_ARGS (new_friend_template_info
);
7761 /* Overwrite whatever template info was there before, if
7762 any, with the new template information pertaining to
7764 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
7766 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
7768 /* We should have called reregister_specialization in
7770 gcc_assert (retrieve_specialization (new_template
,
7774 /* Instantiate it if the global has already been used. */
7775 if (DECL_ODR_USED (old_decl
))
7776 instantiate_decl (old_decl
, /*defer_ok=*/true,
7777 /*expl_inst_class_mem_p=*/false);
7783 /* Indicate that the old function template is a partial
7785 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
7786 = new_friend_result_template_info
;
7788 gcc_assert (new_template
7789 == most_general_template (new_template
));
7790 gcc_assert (new_template
!= old_decl
);
7792 /* Reassign any specializations already in the hash table
7793 to the new more general template, and add the
7794 additional template args. */
7795 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
7799 tree spec
= TREE_VALUE (t
);
7802 elt
.tmpl
= old_decl
;
7803 elt
.args
= DECL_TI_ARGS (spec
);
7804 elt
.spec
= NULL_TREE
;
7806 htab_remove_elt (decl_specializations
, &elt
);
7809 = add_outermost_template_args (new_args
,
7810 DECL_TI_ARGS (spec
));
7812 register_specialization
7813 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
7816 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
7820 /* The information from NEW_FRIEND has been merged into OLD_DECL
7821 by duplicate_decls. */
7822 new_friend
= old_decl
;
7827 tree context
= DECL_CONTEXT (new_friend
);
7831 template <class T> class C {
7832 template <class U> friend void C1<U>::f (); // case 1
7833 friend void C2<T>::f (); // case 2
7835 we only need to make sure CONTEXT is a complete type for
7836 case 2. To distinguish between the two cases, we note that
7837 CONTEXT of case 1 remains dependent type after tsubst while
7838 this isn't true for case 2. */
7839 ++processing_template_decl
;
7840 dependent_p
= dependent_type_p (context
);
7841 --processing_template_decl
;
7844 && !complete_type_or_else (context
, NULL_TREE
))
7845 return error_mark_node
;
7847 if (COMPLETE_TYPE_P (context
))
7849 /* Check to see that the declaration is really present, and,
7850 possibly obtain an improved declaration. */
7851 tree fn
= check_classfn (context
,
7852 new_friend
, NULL_TREE
);
7862 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
7863 template arguments, as for tsubst.
7865 Returns an appropriate tsubst'd friend type or error_mark_node on
7869 tsubst_friend_class (tree friend_tmpl
, tree args
)
7875 context
= CP_DECL_CONTEXT (friend_tmpl
);
7877 if (context
!= global_namespace
)
7879 if (TREE_CODE (context
) == NAMESPACE_DECL
)
7880 push_nested_namespace (context
);
7882 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
7885 /* Look for a class template declaration. We look for hidden names
7886 because two friend declarations of the same template are the
7887 same. For example, in:
7890 template <typename> friend class F;
7892 template <typename> struct B {
7893 template <typename> friend class F;
7896 both F templates are the same. */
7897 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
7898 /*block_p=*/true, 0,
7899 LOOKUP_COMPLAIN
| LOOKUP_HIDDEN
);
7901 /* But, if we don't find one, it might be because we're in a
7902 situation like this:
7910 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
7911 for `S<int>', not the TEMPLATE_DECL. */
7912 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
7914 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
7915 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
7918 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
7920 /* The friend template has already been declared. Just
7921 check to see that the declarations match, and install any new
7922 default parameters. We must tsubst the default parameters,
7923 of course. We only need the innermost template parameters
7924 because that is all that redeclare_class_template will look
7926 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
7927 > TMPL_ARGS_DEPTH (args
))
7930 location_t saved_input_location
;
7931 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
7932 args
, tf_warning_or_error
);
7934 saved_input_location
= input_location
;
7935 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
7936 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
7937 input_location
= saved_input_location
;
7941 friend_type
= TREE_TYPE (tmpl
);
7945 /* The friend template has not already been declared. In this
7946 case, the instantiation of the template class will cause the
7947 injection of this template into the global scope. */
7948 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
7949 if (tmpl
== error_mark_node
)
7950 return error_mark_node
;
7952 /* The new TMPL is not an instantiation of anything, so we
7953 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
7954 the new type because that is supposed to be the corresponding
7955 template decl, i.e., TMPL. */
7956 DECL_USE_TEMPLATE (tmpl
) = 0;
7957 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
7958 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
7959 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
7960 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
7962 /* Inject this template into the global scope. */
7963 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
7966 if (context
!= global_namespace
)
7968 if (TREE_CODE (context
) == NAMESPACE_DECL
)
7969 pop_nested_namespace (context
);
7971 pop_nested_class ();
7977 /* Returns zero if TYPE cannot be completed later due to circularity.
7978 Otherwise returns one. */
7981 can_complete_type_without_circularity (tree type
)
7983 if (type
== NULL_TREE
|| type
== error_mark_node
)
7985 else if (COMPLETE_TYPE_P (type
))
7987 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
7988 return can_complete_type_without_circularity (TREE_TYPE (type
));
7989 else if (CLASS_TYPE_P (type
)
7990 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
7996 /* Apply any attributes which had to be deferred until instantiation
7997 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
7998 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8001 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
8002 tree args
, tsubst_flags_t complain
, tree in_decl
)
8004 tree last_dep
= NULL_TREE
;
8008 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
8009 if (ATTR_IS_DEPENDENT (t
))
8012 attributes
= copy_list (attributes
);
8016 if (DECL_P (*decl_p
))
8018 if (TREE_TYPE (*decl_p
) == error_mark_node
)
8020 p
= &DECL_ATTRIBUTES (*decl_p
);
8023 p
= &TYPE_ATTRIBUTES (*decl_p
);
8027 tree late_attrs
= NULL_TREE
;
8028 tree
*q
= &late_attrs
;
8030 for (*p
= attributes
; *p
; )
8033 if (ATTR_IS_DEPENDENT (t
))
8035 *p
= TREE_CHAIN (t
);
8036 TREE_CHAIN (t
) = NULL_TREE
;
8037 /* If the first attribute argument is an identifier, don't
8038 pass it through tsubst. Attributes like mode, format,
8039 cleanup and several target specific attributes expect it
8042 && TREE_CODE (TREE_VALUE (t
)) == TREE_LIST
8043 && TREE_VALUE (TREE_VALUE (t
))
8044 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t
)))
8045 == IDENTIFIER_NODE
))
8048 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
8050 /*integral_constant_expression_p=*/false);
8051 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
8053 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
8058 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
8059 /*integral_constant_expression_p=*/false);
8061 q
= &TREE_CHAIN (t
);
8064 p
= &TREE_CHAIN (t
);
8067 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
8071 /* Perform (or defer) access check for typedefs that were referenced
8072 from within the template TMPL code.
8073 This is a subroutine of instantiate_template and instantiate_class_template.
8074 TMPL is the template to consider and TARGS is the list of arguments of
8078 perform_typedefs_access_check (tree tmpl
, tree targs
)
8080 location_t saved_location
;
8082 qualified_typedef_usage_t
*iter
;
8085 || (!CLASS_TYPE_P (tmpl
)
8086 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
8089 saved_location
= input_location
;
8090 FOR_EACH_VEC_ELT (qualified_typedef_usage_t
,
8091 get_types_needing_access_check (tmpl
),
8094 tree type_decl
= iter
->typedef_decl
;
8095 tree type_scope
= iter
->context
;
8097 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
8100 if (uses_template_parms (type_decl
))
8101 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
8102 if (uses_template_parms (type_scope
))
8103 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
8105 /* Make access check error messages point to the location
8106 of the use of the typedef. */
8107 input_location
= iter
->locus
;
8108 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
8109 type_decl
, type_decl
);
8111 input_location
= saved_location
;
8115 instantiate_class_template_1 (tree type
)
8117 tree templ
, args
, pattern
, t
, member
;
8121 unsigned int saved_maximum_field_alignment
;
8123 if (type
== error_mark_node
)
8124 return error_mark_node
;
8126 if (COMPLETE_OR_OPEN_TYPE_P (type
)
8127 || uses_template_parms (type
))
8130 /* Figure out which template is being instantiated. */
8131 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
8132 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
8134 /* Determine what specialization of the original template to
8136 t
= most_specialized_class (type
, templ
, tf_warning_or_error
);
8137 if (t
== error_mark_node
)
8139 TYPE_BEING_DEFINED (type
) = 1;
8140 return error_mark_node
;
8144 /* This TYPE is actually an instantiation of a partial
8145 specialization. We replace the innermost set of ARGS with
8146 the arguments appropriate for substitution. For example,
8149 template <class T> struct S {};
8150 template <class T> struct S<T*> {};
8152 and supposing that we are instantiating S<int*>, ARGS will
8153 presently be {int*} -- but we need {int}. */
8154 pattern
= TREE_TYPE (t
);
8155 args
= TREE_PURPOSE (t
);
8159 pattern
= TREE_TYPE (templ
);
8160 args
= CLASSTYPE_TI_ARGS (type
);
8163 /* If the template we're instantiating is incomplete, then clearly
8164 there's nothing we can do. */
8165 if (!COMPLETE_TYPE_P (pattern
))
8168 /* If we've recursively instantiated too many templates, stop. */
8169 if (! push_tinst_level (type
))
8172 /* Now we're really doing the instantiation. Mark the type as in
8173 the process of being defined. */
8174 TYPE_BEING_DEFINED (type
) = 1;
8176 /* We may be in the middle of deferred access check. Disable
8178 push_deferring_access_checks (dk_no_deferred
);
8180 push_to_top_level ();
8181 /* Use #pragma pack from the template context. */
8182 saved_maximum_field_alignment
= maximum_field_alignment
;
8183 maximum_field_alignment
= TYPE_PRECISION (pattern
);
8185 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
8187 /* Set the input location to the most specialized template definition.
8188 This is needed if tsubsting causes an error. */
8189 typedecl
= TYPE_MAIN_DECL (pattern
);
8190 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
8191 DECL_SOURCE_LOCATION (typedecl
);
8193 TYPE_HAS_USER_CONSTRUCTOR (type
) = TYPE_HAS_USER_CONSTRUCTOR (pattern
);
8194 TYPE_HAS_NEW_OPERATOR (type
) = TYPE_HAS_NEW_OPERATOR (pattern
);
8195 TYPE_HAS_ARRAY_NEW_OPERATOR (type
) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern
);
8196 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
8197 TYPE_HAS_COPY_ASSIGN (type
) = TYPE_HAS_COPY_ASSIGN (pattern
);
8198 TYPE_HAS_CONST_COPY_ASSIGN (type
) = TYPE_HAS_CONST_COPY_ASSIGN (pattern
);
8199 TYPE_HAS_COPY_CTOR (type
) = TYPE_HAS_COPY_CTOR (pattern
);
8200 TYPE_HAS_CONST_COPY_CTOR (type
) = TYPE_HAS_CONST_COPY_CTOR (pattern
);
8201 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
8202 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
8203 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
8204 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
8205 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
8206 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
8207 if (ANON_AGGR_TYPE_P (pattern
))
8208 SET_ANON_AGGR_TYPE_P (type
);
8209 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
8211 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
8212 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
8214 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
8216 pbinfo
= TYPE_BINFO (pattern
);
8218 /* We should never instantiate a nested class before its enclosing
8219 class; we need to look up the nested class by name before we can
8220 instantiate it, and that lookup should instantiate the enclosing
8222 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
8223 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
8225 base_list
= NULL_TREE
;
8226 if (BINFO_N_BASE_BINFOS (pbinfo
))
8232 /* We must enter the scope containing the type, as that is where
8233 the accessibility of types named in dependent bases are
8235 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
8237 /* Substitute into each of the bases to determine the actual
8239 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
8242 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
8243 tree expanded_bases
= NULL_TREE
;
8246 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
8249 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
8250 args
, tf_error
, NULL_TREE
);
8251 if (expanded_bases
== error_mark_node
)
8254 len
= TREE_VEC_LENGTH (expanded_bases
);
8257 for (idx
= 0; idx
< len
; idx
++)
8260 /* Extract the already-expanded base class. */
8261 base
= TREE_VEC_ELT (expanded_bases
, idx
);
8263 /* Substitute to figure out the base class. */
8264 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
8267 if (base
== error_mark_node
)
8270 base_list
= tree_cons (access
, base
, base_list
);
8271 if (BINFO_VIRTUAL_P (pbase_binfo
))
8272 TREE_TYPE (base_list
) = integer_type_node
;
8276 /* The list is now in reverse order; correct that. */
8277 base_list
= nreverse (base_list
);
8280 pop_scope (pushed_scope
);
8282 /* Now call xref_basetypes to set up all the base-class
8284 xref_basetypes (type
, base_list
);
8286 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
8287 (int) ATTR_FLAG_TYPE_IN_PLACE
,
8288 args
, tf_error
, NULL_TREE
);
8289 fixup_attribute_variants (type
);
8291 /* Now that our base classes are set up, enter the scope of the
8292 class, so that name lookups into base classes, etc. will work
8293 correctly. This is precisely analogous to what we do in
8294 begin_class_definition when defining an ordinary non-template
8295 class, except we also need to push the enclosing classes. */
8296 push_nested_class (type
);
8298 /* Now members are processed in the order of declaration. */
8299 for (member
= CLASSTYPE_DECL_LIST (pattern
);
8300 member
; member
= TREE_CHAIN (member
))
8302 tree t
= TREE_VALUE (member
);
8304 if (TREE_PURPOSE (member
))
8308 /* Build new CLASSTYPE_NESTED_UTDS. */
8311 bool class_template_p
;
8313 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
8314 && TYPE_LANG_SPECIFIC (t
)
8315 && CLASSTYPE_IS_TEMPLATE (t
));
8316 /* If the member is a class template, then -- even after
8317 substitution -- there may be dependent types in the
8318 template argument list for the class. We increment
8319 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8320 that function will assume that no types are dependent
8321 when outside of a template. */
8322 if (class_template_p
)
8323 ++processing_template_decl
;
8324 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8325 if (class_template_p
)
8326 --processing_template_decl
;
8327 if (newtag
== error_mark_node
)
8330 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
8332 tree name
= TYPE_IDENTIFIER (t
);
8334 if (class_template_p
)
8335 /* Unfortunately, lookup_template_class sets
8336 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8337 instantiation (i.e., for the type of a member
8338 template class nested within a template class.)
8339 This behavior is required for
8340 maybe_process_partial_specialization to work
8341 correctly, but is not accurate in this case;
8342 the TAG is not an instantiation of anything.
8343 (The corresponding TEMPLATE_DECL is an
8344 instantiation, but the TYPE is not.) */
8345 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
8347 /* Now, we call pushtag to put this NEWTAG into the scope of
8348 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8349 pushtag calling push_template_decl. We don't have to do
8350 this for enums because it will already have been done in
8353 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
8354 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
8357 else if (TREE_CODE (t
) == FUNCTION_DECL
8358 || DECL_FUNCTION_TEMPLATE_P (t
))
8360 /* Build new TYPE_METHODS. */
8363 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8364 ++processing_template_decl
;
8365 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8366 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8367 --processing_template_decl
;
8368 set_current_access_from_decl (r
);
8369 finish_member_declaration (r
);
8373 /* Build new TYPE_FIELDS. */
8374 if (TREE_CODE (t
) == STATIC_ASSERT
)
8377 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
8378 tf_warning_or_error
, NULL_TREE
,
8379 /*integral_constant_expression_p=*/true);
8380 finish_static_assert (condition
,
8381 STATIC_ASSERT_MESSAGE (t
),
8382 STATIC_ASSERT_SOURCE_LOCATION (t
),
8385 else if (TREE_CODE (t
) != CONST_DECL
)
8389 /* The file and line for this declaration, to
8390 assist in error message reporting. Since we
8391 called push_tinst_level above, we don't need to
8393 input_location
= DECL_SOURCE_LOCATION (t
);
8395 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8396 ++processing_template_decl
;
8397 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
8398 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8399 --processing_template_decl
;
8400 if (TREE_CODE (r
) == VAR_DECL
)
8404 [t]he initialization (and any associated
8405 side-effects) of a static data member does
8406 not occur unless the static data member is
8407 itself used in a way that requires the
8408 definition of the static data member to
8411 Therefore, we do not substitute into the
8412 initialized for the static data member here. */
8413 finish_static_data_member_decl
8416 /*init_const_expr_p=*/false,
8417 /*asmspec_tree=*/NULL_TREE
,
8420 else if (TREE_CODE (r
) == FIELD_DECL
)
8422 /* Determine whether R has a valid type and can be
8423 completed later. If R is invalid, then it is
8424 replaced by error_mark_node so that it will not be
8425 added to TYPE_FIELDS. */
8426 tree rtype
= TREE_TYPE (r
);
8427 if (can_complete_type_without_circularity (rtype
))
8428 complete_type (rtype
);
8430 if (!COMPLETE_TYPE_P (rtype
))
8432 cxx_incomplete_type_error (r
, rtype
);
8433 r
= error_mark_node
;
8437 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8438 such a thing will already have been added to the field
8439 list by tsubst_enum in finish_member_declaration in the
8440 CLASSTYPE_NESTED_UTDS case above. */
8441 if (!(TREE_CODE (r
) == TYPE_DECL
8442 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
8443 && DECL_ARTIFICIAL (r
)))
8445 set_current_access_from_decl (r
);
8446 finish_member_declaration (r
);
8453 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
))
8455 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8457 tree friend_type
= t
;
8458 bool adjust_processing_template_decl
= false;
8460 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
8462 /* template <class T> friend class C; */
8463 friend_type
= tsubst_friend_class (friend_type
, args
);
8464 adjust_processing_template_decl
= true;
8466 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
8468 /* template <class T> friend class C::D; */
8469 friend_type
= tsubst (friend_type
, args
,
8470 tf_warning_or_error
, NULL_TREE
);
8471 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
8472 friend_type
= TREE_TYPE (friend_type
);
8473 adjust_processing_template_decl
= true;
8475 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
8476 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
8478 /* This could be either
8482 when dependent_type_p is false or
8484 template <class U> friend class T::C;
8487 friend_type
= tsubst (friend_type
, args
,
8488 tf_warning_or_error
, NULL_TREE
);
8489 /* Bump processing_template_decl for correct
8490 dependent_type_p calculation. */
8491 ++processing_template_decl
;
8492 if (dependent_type_p (friend_type
))
8493 adjust_processing_template_decl
= true;
8494 --processing_template_decl
;
8496 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
8497 && hidden_name_p (TYPE_NAME (friend_type
)))
8501 where C hasn't been declared yet. Let's lookup name
8502 from namespace scope directly, bypassing any name that
8503 come from dependent base class. */
8504 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
8506 /* The call to xref_tag_from_type does injection for friend
8508 push_nested_namespace (ns
);
8510 xref_tag_from_type (friend_type
, NULL_TREE
,
8511 /*tag_scope=*/ts_current
);
8512 pop_nested_namespace (ns
);
8514 else if (uses_template_parms (friend_type
))
8515 /* friend class C<T>; */
8516 friend_type
= tsubst (friend_type
, args
,
8517 tf_warning_or_error
, NULL_TREE
);
8522 where C is already declared or
8524 friend class C<int>;
8526 We don't have to do anything in these cases. */
8528 if (adjust_processing_template_decl
)
8529 /* Trick make_friend_class into realizing that the friend
8530 we're adding is a template, not an ordinary class. It's
8531 important that we use make_friend_class since it will
8532 perform some error-checking and output cross-reference
8534 ++processing_template_decl
;
8536 if (friend_type
!= error_mark_node
)
8537 make_friend_class (type
, friend_type
, /*complain=*/false);
8539 if (adjust_processing_template_decl
)
8540 --processing_template_decl
;
8544 /* Build new DECL_FRIENDLIST. */
8547 /* The file and line for this declaration, to
8548 assist in error message reporting. Since we
8549 called push_tinst_level above, we don't need to
8551 input_location
= DECL_SOURCE_LOCATION (t
);
8553 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8555 ++processing_template_decl
;
8556 push_deferring_access_checks (dk_no_check
);
8559 r
= tsubst_friend_function (t
, args
);
8560 add_friend (type
, r
, /*complain=*/false);
8561 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8563 pop_deferring_access_checks ();
8564 --processing_template_decl
;
8570 if (CLASSTYPE_LAMBDA_EXPR (type
))
8571 maybe_add_lambda_conv_op (type
);
8573 /* Set the file and line number information to whatever is given for
8574 the class itself. This puts error messages involving generated
8575 implicit functions at a predictable point, and the same point
8576 that would be used for non-template classes. */
8577 input_location
= DECL_SOURCE_LOCATION (typedecl
);
8579 unreverse_member_declarations (type
);
8580 finish_struct_1 (type
);
8581 TYPE_BEING_DEFINED (type
) = 0;
8583 /* We don't instantiate default arguments for member functions. 14.7.1:
8585 The implicit instantiation of a class template specialization causes
8586 the implicit instantiation of the declarations, but not of the
8587 definitions or default arguments, of the class member functions,
8588 member classes, static data members and member templates.... */
8590 /* Some typedefs referenced from within the template code need to be access
8591 checked at template instantiation time, i.e now. These types were
8592 added to the template at parsing time. Let's get those and perform
8593 the access checks then. */
8594 perform_typedefs_access_check (pattern
, args
);
8595 perform_deferred_access_checks ();
8596 pop_nested_class ();
8597 maximum_field_alignment
= saved_maximum_field_alignment
;
8598 pop_from_top_level ();
8599 pop_deferring_access_checks ();
8602 /* The vtable for a template class can be emitted in any translation
8603 unit in which the class is instantiated. When there is no key
8604 method, however, finish_struct_1 will already have added TYPE to
8605 the keyed_classes list. */
8606 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
8607 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
8612 /* Wrapper for instantiate_class_template_1. */
8615 instantiate_class_template (tree type
)
8618 timevar_push (TV_TEMPLATE_INST
);
8619 ret
= instantiate_class_template_1 (type
);
8620 timevar_pop (TV_TEMPLATE_INST
);
8625 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
8631 else if (TYPE_P (t
))
8632 r
= tsubst (t
, args
, complain
, in_decl
);
8635 if (!(complain
& tf_warning
))
8636 ++c_inhibit_evaluation_warnings
;
8637 r
= tsubst_expr (t
, args
, complain
, in_decl
,
8638 /*integral_constant_expression_p=*/true);
8639 if (!(complain
& tf_warning
))
8640 --c_inhibit_evaluation_warnings
;
8645 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
8646 NONTYPE_ARGUMENT_PACK. */
8649 make_fnparm_pack (tree spec_parm
)
8651 /* Collect all of the extra "packed" parameters into an
8655 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
8656 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8657 int i
, len
= list_length (spec_parm
);
8659 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
8660 parmvec
= make_tree_vec (len
);
8661 parmtypevec
= make_tree_vec (len
);
8662 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
8664 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
8665 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
8668 /* Build the argument packs. */
8669 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
8670 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
8671 TREE_TYPE (argpack
) = argtypepack
;
8676 /* Substitute ARGS into T, which is an pack expansion
8677 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
8678 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
8679 (if only a partial substitution could be performed) or
8680 ERROR_MARK_NODE if there was an error. */
8682 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
8686 tree pack
, packs
= NULL_TREE
, unsubstituted_packs
= NULL_TREE
;
8690 htab_t saved_local_specializations
= NULL
;
8692 gcc_assert (PACK_EXPANSION_P (t
));
8693 pattern
= PACK_EXPANSION_PATTERN (t
);
8695 /* Determine the argument packs that will instantiate the parameter
8696 packs used in the expansion expression. While we're at it,
8697 compute the number of arguments to be expanded and make sure it
8699 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
8700 pack
= TREE_CHAIN (pack
))
8702 tree parm_pack
= TREE_VALUE (pack
);
8703 tree arg_pack
= NULL_TREE
;
8704 tree orig_arg
= NULL_TREE
;
8706 if (TREE_CODE (parm_pack
) == PARM_DECL
)
8708 if (!cp_unevaluated_operand
)
8709 arg_pack
= retrieve_local_specialization (parm_pack
);
8712 /* We can't rely on local_specializations for a parameter
8713 name used later in a function declaration (such as in a
8714 late-specified return type). Even if it exists, it might
8715 have the wrong value for a recursive call. Just make a
8716 dummy decl, since it's only used for its type. */
8717 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
8718 if (arg_pack
&& FUNCTION_PARAMETER_PACK_P (arg_pack
))
8719 /* Partial instantiation of the parm_pack, we can't build
8720 up an argument pack yet. */
8721 arg_pack
= NULL_TREE
;
8723 arg_pack
= make_fnparm_pack (arg_pack
);
8728 int level
, idx
, levels
;
8729 template_parm_level_and_index (parm_pack
, &level
, &idx
);
8731 levels
= TMPL_ARGS_DEPTH (args
);
8732 if (level
<= levels
)
8733 arg_pack
= TMPL_ARG (args
, level
, idx
);
8736 orig_arg
= arg_pack
;
8737 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
8738 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
8740 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
8741 /* This can only happen if we forget to expand an argument
8742 pack somewhere else. Just return an error, silently. */
8744 result
= make_tree_vec (1);
8745 TREE_VEC_ELT (result
, 0) = error_mark_node
;
8749 /* For clarity in the comments below let's use the
8750 representation 'argument_pack<elements>' to denote an
8751 argument pack and its elements.
8753 In the 'if' block below, we want to detect cases where
8754 ARG_PACK is argument_pack<PARM_PACK...>. I.e, we want to
8755 check if ARG_PACK is an argument pack which sole element is
8756 the expansion of PARM_PACK. That argument pack is typically
8757 created by template_parm_to_arg when passed a parameter
8760 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
)) == 1
8761 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0)))
8763 tree expansion
= TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0);
8764 tree pattern
= PACK_EXPANSION_PATTERN (expansion
);
8765 /* So we have an argument_pack<P...>. We want to test if P
8766 is actually PARM_PACK. We will not use cp_tree_equal to
8767 test P and PARM_PACK because during type fixup (by
8768 fixup_template_parm) P can be a pre-fixup version of a
8769 type and PARM_PACK be its post-fixup version.
8770 cp_tree_equal would consider them as different even
8771 though we would want to consider them compatible for our
8772 precise purpose here.
8774 Thus we are going to consider that P and PARM_PACK are
8775 compatible if they have the same DECL. */
8776 if ((/* If ARG_PACK is a type parameter pack named by the
8777 same DECL as parm_pack ... */
8779 && TYPE_P (parm_pack
)
8780 && TYPE_NAME (pattern
) == TYPE_NAME (parm_pack
))
8781 /* ... or if ARG_PACK is a non-type parameter
8782 named by the same DECL as parm_pack ... */
8783 || (TREE_CODE (pattern
) == TEMPLATE_PARM_INDEX
8784 && TREE_CODE (parm_pack
) == PARM_DECL
8785 && TEMPLATE_PARM_DECL (pattern
)
8786 == TEMPLATE_PARM_DECL (DECL_INITIAL (parm_pack
))))
8787 && template_parameter_pack_p (pattern
))
8788 /* ... then the argument pack that the parameter maps to
8789 is just an expansion of the parameter itself, such as
8790 one would find in the implicit typedef of a class
8791 inside the class itself. Consider this parameter
8792 "unsubstituted", so that we will maintain the outer
8794 arg_pack
= NULL_TREE
;
8800 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
8802 /* It's all-or-nothing with incomplete argument packs. */
8803 if (incomplete
&& !ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
8804 return error_mark_node
;
8806 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
8811 else if (len
!= my_len
)
8814 /* We got explicit args for some packs but not others;
8815 do nothing now and try again after deduction. */
8817 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
8818 error ("mismatched argument pack lengths while expanding "
8822 error ("mismatched argument pack lengths while expanding "
8825 return error_mark_node
;
8828 /* Keep track of the parameter packs and their corresponding
8830 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
8831 TREE_TYPE (packs
) = orig_arg
;
8834 /* We can't substitute for this parameter pack. */
8835 unsubstituted_packs
= tree_cons (TREE_PURPOSE (pack
),
8837 unsubstituted_packs
);
8840 /* We cannot expand this expansion expression, because we don't have
8841 all of the argument packs we need. Substitute into the pattern
8842 and return a PACK_EXPANSION_*. The caller will need to deal with
8844 if (unsubstituted_packs
)
8847 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
8848 new_pat
= tsubst_expr (pattern
, args
, complain
, in_decl
,
8849 /*integral_constant_expression_p=*/false);
8851 new_pat
= tsubst (pattern
, args
, complain
, in_decl
);
8852 return make_pack_expansion (new_pat
);
8855 /* We could not find any argument packs that work. */
8857 return error_mark_node
;
8859 if (cp_unevaluated_operand
)
8861 /* We're in a late-specified return type, so create our own local
8862 specializations table; the current table is either NULL or (in the
8863 case of recursive unification) might have bindings that we don't
8864 want to use or alter. */
8865 saved_local_specializations
= local_specializations
;
8866 local_specializations
= htab_create (37,
8867 hash_local_specialization
,
8868 eq_local_specializations
,
8872 /* For each argument in each argument pack, substitute into the
8874 result
= make_tree_vec (len
+ incomplete
);
8875 for (i
= 0; i
< len
+ incomplete
; ++i
)
8877 /* For parameter pack, change the substitution of the parameter
8878 pack to the ith argument in its argument pack, then expand
8880 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
8882 tree parm
= TREE_PURPOSE (pack
);
8884 if (TREE_CODE (parm
) == PARM_DECL
)
8886 /* Select the Ith argument from the pack. */
8887 tree arg
= make_node (ARGUMENT_PACK_SELECT
);
8888 ARGUMENT_PACK_SELECT_FROM_PACK (arg
) = TREE_VALUE (pack
);
8889 ARGUMENT_PACK_SELECT_INDEX (arg
) = i
;
8891 register_local_specialization (arg
, parm
);
8897 template_parm_level_and_index (parm
, &level
, &idx
);
8901 /* Select the Ith argument from the pack. */
8902 value
= make_node (ARGUMENT_PACK_SELECT
);
8903 ARGUMENT_PACK_SELECT_FROM_PACK (value
) = TREE_VALUE (pack
);
8904 ARGUMENT_PACK_SELECT_INDEX (value
) = i
;
8907 /* Update the corresponding argument. */
8908 TMPL_ARG (args
, level
, idx
) = value
;
8912 /* Substitute into the PATTERN with the altered arguments. */
8913 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
8914 TREE_VEC_ELT (result
, i
) =
8915 tsubst_expr (pattern
, args
, complain
, in_decl
,
8916 /*integral_constant_expression_p=*/false);
8918 TREE_VEC_ELT (result
, i
) = tsubst (pattern
, args
, complain
, in_decl
);
8921 /* When we have incomplete argument packs, the last "expanded"
8922 result is itself a pack expansion, which allows us
8923 to deduce more arguments. */
8924 TREE_VEC_ELT (result
, i
) =
8925 make_pack_expansion (TREE_VEC_ELT (result
, i
));
8927 if (TREE_VEC_ELT (result
, i
) == error_mark_node
)
8929 result
= error_mark_node
;
8934 /* Update ARGS to restore the substitution from parameter packs to
8935 their argument packs. */
8936 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
8938 tree parm
= TREE_PURPOSE (pack
);
8940 if (TREE_CODE (parm
) == PARM_DECL
)
8941 register_local_specialization (TREE_TYPE (pack
), parm
);
8945 template_parm_level_and_index (parm
, &level
, &idx
);
8947 /* Update the corresponding argument. */
8948 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
8949 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
8952 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
8956 if (saved_local_specializations
)
8958 htab_delete (local_specializations
);
8959 local_specializations
= saved_local_specializations
;
8965 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
8966 TMPL. We do this using DECL_PARM_INDEX, which should work even with
8967 parameter packs; all parms generated from a function parameter pack will
8968 have the same DECL_PARM_INDEX. */
8971 get_pattern_parm (tree parm
, tree tmpl
)
8973 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
8976 if (DECL_ARTIFICIAL (parm
))
8978 for (patparm
= DECL_ARGUMENTS (pattern
);
8979 patparm
; patparm
= DECL_CHAIN (patparm
))
8980 if (DECL_ARTIFICIAL (patparm
)
8981 && DECL_NAME (parm
) == DECL_NAME (patparm
))
8986 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
8987 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
8988 gcc_assert (DECL_PARM_INDEX (patparm
)
8989 == DECL_PARM_INDEX (parm
));
8995 /* Substitute ARGS into the vector or list of template arguments T. */
8998 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9001 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
9004 if (t
== error_mark_node
)
9005 return error_mark_node
;
9007 len
= TREE_VEC_LENGTH (t
);
9008 elts
= XALLOCAVEC (tree
, len
);
9010 for (i
= 0; i
< len
; i
++)
9012 tree orig_arg
= TREE_VEC_ELT (t
, i
);
9015 if (TREE_CODE (orig_arg
) == TREE_VEC
)
9016 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
9017 else if (PACK_EXPANSION_P (orig_arg
))
9019 /* Substitute into an expansion expression. */
9020 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
9022 if (TREE_CODE (new_arg
) == TREE_VEC
)
9023 /* Add to the expanded length adjustment the number of
9024 expanded arguments. We subtract one from this
9025 measurement, because the argument pack expression
9026 itself is already counted as 1 in
9027 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9028 the argument pack is empty. */
9029 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
9031 else if (ARGUMENT_PACK_P (orig_arg
))
9033 /* Substitute into each of the arguments. */
9034 new_arg
= TYPE_P (orig_arg
)
9035 ? cxx_make_type (TREE_CODE (orig_arg
))
9036 : make_node (TREE_CODE (orig_arg
));
9038 SET_ARGUMENT_PACK_ARGS (
9040 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
9041 args
, complain
, in_decl
));
9043 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
9044 new_arg
= error_mark_node
;
9046 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
9047 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
9049 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
9051 if (TREE_TYPE (new_arg
) == error_mark_node
)
9052 new_arg
= error_mark_node
;
9056 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
9058 if (new_arg
== error_mark_node
)
9059 return error_mark_node
;
9062 if (new_arg
!= orig_arg
)
9069 /* Make space for the expanded arguments coming from template
9071 t
= make_tree_vec (len
+ expanded_len_adjust
);
9072 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9073 arguments for a member template.
9074 In that case each TREE_VEC in ORIG_T represents a level of template
9075 arguments, and ORIG_T won't carry any non defaulted argument count.
9076 It will rather be the nested TREE_VECs that will carry one.
9077 In other words, ORIG_T carries a non defaulted argument count only
9078 if it doesn't contain any nested TREE_VEC. */
9079 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
9081 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
9082 count
+= expanded_len_adjust
;
9083 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
9085 for (i
= 0, out
= 0; i
< len
; i
++)
9087 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
9088 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
9089 && TREE_CODE (elts
[i
]) == TREE_VEC
)
9093 /* Now expand the template argument pack "in place". */
9094 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
9095 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
9099 TREE_VEC_ELT (t
, out
) = elts
[i
];
9107 /* Return the result of substituting ARGS into the template parameters
9108 given by PARMS. If there are m levels of ARGS and m + n levels of
9109 PARMS, then the result will contain n levels of PARMS. For
9110 example, if PARMS is `template <class T> template <class U>
9111 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9112 result will be `template <int*, double, class V>'. */
9115 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
9120 /* When substituting into a template, we must set
9121 PROCESSING_TEMPLATE_DECL as the template parameters may be
9122 dependent if they are based on one-another, and the dependency
9123 predicates are short-circuit outside of templates. */
9124 ++processing_template_decl
;
9126 for (new_parms
= &r
;
9127 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
9128 new_parms
= &(TREE_CHAIN (*new_parms
)),
9129 parms
= TREE_CHAIN (parms
))
9132 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
9135 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
9139 if (parms
== error_mark_node
)
9142 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
9144 if (tuple
== error_mark_node
)
9147 TREE_VEC_ELT (new_vec
, i
) =
9148 tsubst_template_parm (tuple
, args
, complain
);
9152 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
9153 - TMPL_ARGS_DEPTH (args
)),
9154 new_vec
, NULL_TREE
);
9157 --processing_template_decl
;
9162 /* Return the result of substituting ARGS into one template parameter
9163 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9164 parameter and which TREE_PURPOSE is the default argument of the
9165 template parameter. */
9168 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
9170 tree default_value
, parm_decl
;
9172 if (args
== NULL_TREE
9174 || t
== error_mark_node
)
9177 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
9179 default_value
= TREE_PURPOSE (t
);
9180 parm_decl
= TREE_VALUE (t
);
9182 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
9183 if (TREE_CODE (parm_decl
) == PARM_DECL
9184 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
9185 parm_decl
= error_mark_node
;
9186 default_value
= tsubst_template_arg (default_value
, args
,
9187 complain
, NULL_TREE
);
9189 return build_tree_list (default_value
, parm_decl
);
9192 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9193 type T. If T is not an aggregate or enumeration type, it is
9194 handled as if by tsubst. IN_DECL is as for tsubst. If
9195 ENTERING_SCOPE is nonzero, T is the context for a template which
9196 we are presently tsubst'ing. Return the substituted value. */
9199 tsubst_aggr_type (tree t
,
9201 tsubst_flags_t complain
,
9208 switch (TREE_CODE (t
))
9211 if (TYPE_PTRMEMFUNC_P (t
))
9212 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
9214 /* Else fall through. */
9217 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
9222 int saved_unevaluated_operand
;
9223 int saved_inhibit_evaluation_warnings
;
9225 /* In "sizeof(X<I>)" we need to evaluate "I". */
9226 saved_unevaluated_operand
= cp_unevaluated_operand
;
9227 cp_unevaluated_operand
= 0;
9228 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9229 c_inhibit_evaluation_warnings
= 0;
9231 /* First, determine the context for the type we are looking
9233 context
= TYPE_CONTEXT (t
);
9236 context
= tsubst_aggr_type (context
, args
, complain
,
9237 in_decl
, /*entering_scope=*/1);
9238 /* If context is a nested class inside a class template,
9239 it may still need to be instantiated (c++/33959). */
9240 if (TYPE_P (context
))
9241 context
= complete_type (context
);
9244 /* Then, figure out what arguments are appropriate for the
9245 type we are trying to find. For example, given:
9247 template <class T> struct S;
9248 template <class T, class U> void f(T, U) { S<U> su; }
9250 and supposing that we are instantiating f<int, double>,
9251 then our ARGS will be {int, double}, but, when looking up
9252 S we only want {double}. */
9253 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
9255 if (argvec
== error_mark_node
)
9256 r
= error_mark_node
;
9259 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
9260 entering_scope
, complain
);
9261 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
9264 cp_unevaluated_operand
= saved_unevaluated_operand
;
9265 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
9270 /* This is not a template type, so there's nothing to do. */
9274 return tsubst (t
, args
, complain
, in_decl
);
9278 /* Substitute into the default argument ARG (a default argument for
9279 FN), which has the indicated TYPE. */
9282 tsubst_default_argument (tree fn
, tree type
, tree arg
)
9284 tree saved_class_ptr
= NULL_TREE
;
9285 tree saved_class_ref
= NULL_TREE
;
9287 /* This can happen in invalid code. */
9288 if (TREE_CODE (arg
) == DEFAULT_ARG
)
9291 /* This default argument came from a template. Instantiate the
9292 default argument here, not in tsubst. In the case of
9301 we must be careful to do name lookup in the scope of S<T>,
9302 rather than in the current class. */
9303 push_access_scope (fn
);
9304 /* The "this" pointer is not valid in a default argument. */
9307 saved_class_ptr
= current_class_ptr
;
9308 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
9309 saved_class_ref
= current_class_ref
;
9310 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
9313 push_deferring_access_checks(dk_no_deferred
);
9314 /* The default argument expression may cause implicitly defined
9315 member functions to be synthesized, which will result in garbage
9316 collection. We must treat this situation as if we were within
9317 the body of function so as to avoid collecting live data on the
9320 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
9321 tf_warning_or_error
, NULL_TREE
,
9322 /*integral_constant_expression_p=*/false);
9324 pop_deferring_access_checks();
9326 /* Restore the "this" pointer. */
9329 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
9330 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
9333 /* Make sure the default argument is reasonable. */
9334 arg
= check_default_argument (type
, arg
);
9336 pop_access_scope (fn
);
9341 /* Substitute into all the default arguments for FN. */
9344 tsubst_default_arguments (tree fn
)
9349 tmpl_args
= DECL_TI_ARGS (fn
);
9351 /* If this function is not yet instantiated, we certainly don't need
9352 its default arguments. */
9353 if (uses_template_parms (tmpl_args
))
9355 /* Don't do this again for clones. */
9356 if (DECL_CLONED_FUNCTION_P (fn
))
9359 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
9361 arg
= TREE_CHAIN (arg
))
9362 if (TREE_PURPOSE (arg
))
9363 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
9365 TREE_PURPOSE (arg
));
9368 /* Substitute the ARGS into the T, which is a _DECL. Return the
9369 result of the substitution. Issue error and warning messages under
9370 control of COMPLAIN. */
9373 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
9375 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
9376 location_t saved_loc
;
9381 /* Set the filename and linenumber to improve error-reporting. */
9382 saved_loc
= input_location
;
9383 input_location
= DECL_SOURCE_LOCATION (t
);
9385 switch (TREE_CODE (t
))
9389 /* We can get here when processing a member function template,
9390 member class template, or template template parameter. */
9391 tree decl
= DECL_TEMPLATE_RESULT (t
);
9396 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9398 /* Template template parameter is treated here. */
9399 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9400 if (new_type
== error_mark_node
)
9401 RETURN (error_mark_node
);
9404 DECL_CHAIN (r
) = NULL_TREE
;
9405 TREE_TYPE (r
) = new_type
;
9406 DECL_TEMPLATE_RESULT (r
)
9407 = build_decl (DECL_SOURCE_LOCATION (decl
),
9408 TYPE_DECL
, DECL_NAME (decl
), new_type
);
9409 DECL_TEMPLATE_PARMS (r
)
9410 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
9412 TYPE_NAME (new_type
) = r
;
9416 /* We might already have an instance of this template.
9417 The ARGS are for the surrounding class type, so the
9418 full args contain the tsubst'd args for the context,
9419 plus the innermost args from the template decl. */
9420 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
9421 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
9422 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
9423 /* Because this is a template, the arguments will still be
9424 dependent, even after substitution. If
9425 PROCESSING_TEMPLATE_DECL is not set, the dependency
9426 predicates will short-circuit. */
9427 ++processing_template_decl
;
9428 full_args
= tsubst_template_args (tmpl_args
, args
,
9430 --processing_template_decl
;
9431 if (full_args
== error_mark_node
)
9432 RETURN (error_mark_node
);
9434 /* If this is a default template template argument,
9435 tsubst might not have changed anything. */
9436 if (full_args
== tmpl_args
)
9439 hash
= hash_tmpl_and_args (t
, full_args
);
9440 spec
= retrieve_specialization (t
, full_args
, hash
);
9441 if (spec
!= NULL_TREE
)
9447 /* Make a new template decl. It will be similar to the
9448 original, but will record the current template arguments.
9449 We also create a new function declaration, which is just
9450 like the old one, but points to this new template, rather
9451 than the old one. */
9453 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
9454 DECL_CHAIN (r
) = NULL_TREE
;
9456 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
9458 if (TREE_CODE (decl
) == TYPE_DECL
)
9461 ++processing_template_decl
;
9462 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9463 --processing_template_decl
;
9464 if (new_type
== error_mark_node
)
9465 RETURN (error_mark_node
);
9467 TREE_TYPE (r
) = new_type
;
9468 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
9469 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
9470 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
9471 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
9476 ++processing_template_decl
;
9477 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
9478 --processing_template_decl
;
9479 if (new_decl
== error_mark_node
)
9480 RETURN (error_mark_node
);
9482 DECL_TEMPLATE_RESULT (r
) = new_decl
;
9483 DECL_TI_TEMPLATE (new_decl
) = r
;
9484 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
9485 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
9486 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
9489 SET_DECL_IMPLICIT_INSTANTIATION (r
);
9490 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
9491 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
9493 /* The template parameters for this new template are all the
9494 template parameters for the old template, except the
9495 outermost level of parameters. */
9496 DECL_TEMPLATE_PARMS (r
)
9497 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
9500 if (PRIMARY_TEMPLATE_P (t
))
9501 DECL_PRIMARY_TEMPLATE (r
) = r
;
9503 if (TREE_CODE (decl
) != TYPE_DECL
)
9504 /* Record this non-type partial instantiation. */
9505 register_specialization (r
, t
,
9506 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
9514 tree argvec
= NULL_TREE
;
9522 /* Nobody should be tsubst'ing into non-template functions. */
9523 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
9525 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
9530 /* If T is not dependent, just return it. We have to
9531 increment PROCESSING_TEMPLATE_DECL because
9532 value_dependent_expression_p assumes that nothing is
9533 dependent when PROCESSING_TEMPLATE_DECL is zero. */
9534 ++processing_template_decl
;
9535 dependent_p
= value_dependent_expression_p (t
);
9536 --processing_template_decl
;
9540 /* Calculate the most general template of which R is a
9541 specialization, and the complete set of arguments used to
9543 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
9544 argvec
= tsubst_template_args (DECL_TI_ARGS
9545 (DECL_TEMPLATE_RESULT
9546 (DECL_TI_TEMPLATE (t
))),
9547 args
, complain
, in_decl
);
9549 /* Check to see if we already have this specialization. */
9550 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
9551 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
9559 /* We can see more levels of arguments than parameters if
9560 there was a specialization of a member template, like
9563 template <class T> struct S { template <class U> void f(); }
9564 template <> template <class U> void S<int>::f(U);
9566 Here, we'll be substituting into the specialization,
9567 because that's where we can find the code we actually
9568 want to generate, but we'll have enough arguments for
9569 the most general template.
9571 We also deal with the peculiar case:
9573 template <class T> struct S {
9574 template <class U> friend void f();
9576 template <class U> void f() {}
9578 template void f<double>();
9580 Here, the ARGS for the instantiation of will be {int,
9581 double}. But, we only need as many ARGS as there are
9582 levels of template parameters in CODE_PATTERN. We are
9583 careful not to get fooled into reducing the ARGS in
9586 template <class T> struct S { template <class U> void f(U); }
9587 template <class T> template <> void S<T>::f(int) {}
9589 which we can spot because the pattern will be a
9590 specialization in this case. */
9591 args_depth
= TMPL_ARGS_DEPTH (args
);
9593 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
9594 if (args_depth
> parms_depth
9595 && !DECL_TEMPLATE_SPECIALIZATION (t
))
9596 args
= get_innermost_template_args (args
, parms_depth
);
9600 /* This special case arises when we have something like this:
9602 template <class T> struct S {
9603 friend void f<int>(int, double);
9606 Here, the DECL_TI_TEMPLATE for the friend declaration
9607 will be an IDENTIFIER_NODE. We are being called from
9608 tsubst_friend_function, and we want only to create a
9609 new decl (R) with appropriate types so that we can call
9610 determine_specialization. */
9611 gen_tmpl
= NULL_TREE
;
9614 if (DECL_CLASS_SCOPE_P (t
))
9616 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
9620 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
9621 complain
, t
, /*entering_scope=*/1);
9626 ctx
= DECL_CONTEXT (t
);
9628 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9629 if (type
== error_mark_node
)
9630 RETURN (error_mark_node
);
9632 /* We do NOT check for matching decls pushed separately at this
9633 point, as they may not represent instantiations of this
9634 template, and in any case are considered separate under the
9637 DECL_USE_TEMPLATE (r
) = 0;
9638 TREE_TYPE (r
) = type
;
9639 /* Clear out the mangled name and RTL for the instantiation. */
9640 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
9641 SET_DECL_RTL (r
, NULL
);
9642 /* Leave DECL_INITIAL set on deleted instantiations. */
9643 if (!DECL_DELETED_FN (r
))
9644 DECL_INITIAL (r
) = NULL_TREE
;
9645 DECL_CONTEXT (r
) = ctx
;
9647 if (member
&& DECL_CONV_FN_P (r
))
9648 /* Type-conversion operator. Reconstruct the name, in
9649 case it's the name of one of the template's parameters. */
9650 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
9652 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
9654 DECL_RESULT (r
) = NULL_TREE
;
9656 TREE_STATIC (r
) = 0;
9657 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
9658 DECL_EXTERNAL (r
) = 1;
9659 /* If this is an instantiation of a function with internal
9660 linkage, we already know what object file linkage will be
9661 assigned to the instantiation. */
9662 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
9663 DECL_DEFER_OUTPUT (r
) = 0;
9664 DECL_CHAIN (r
) = NULL_TREE
;
9665 DECL_PENDING_INLINE_INFO (r
) = 0;
9666 DECL_PENDING_INLINE_P (r
) = 0;
9667 DECL_SAVED_TREE (r
) = NULL_TREE
;
9668 DECL_STRUCT_FUNCTION (r
) = NULL
;
9670 /* We'll re-clone as appropriate in instantiate_template. */
9671 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
9673 /* If we aren't complaining now, return on error before we register
9674 the specialization so that we'll complain eventually. */
9675 if ((complain
& tf_error
) == 0
9676 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
9677 && !grok_op_properties (r
, /*complain=*/false))
9678 RETURN (error_mark_node
);
9680 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
9681 this in the special friend case mentioned above where
9682 GEN_TMPL is NULL. */
9685 DECL_TEMPLATE_INFO (r
)
9686 = build_template_info (gen_tmpl
, argvec
);
9687 SET_DECL_IMPLICIT_INSTANTIATION (r
);
9688 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
9690 /* We're not supposed to instantiate default arguments
9691 until they are called, for a template. But, for a
9694 template <class T> void f ()
9695 { extern void g(int i = T()); }
9697 we should do the substitution when the template is
9698 instantiated. We handle the member function case in
9699 instantiate_class_template since the default arguments
9700 might refer to other members of the class. */
9702 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9703 && !uses_template_parms (argvec
))
9704 tsubst_default_arguments (r
);
9707 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
9709 /* Copy the list of befriending classes. */
9710 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
9712 friends
= &TREE_CHAIN (*friends
))
9714 *friends
= copy_node (*friends
);
9715 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
9720 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
9722 maybe_retrofit_in_chrg (r
);
9723 if (DECL_CONSTRUCTOR_P (r
))
9724 grok_ctor_properties (ctx
, r
);
9725 /* If this is an instantiation of a member template, clone it.
9726 If it isn't, that'll be handled by
9727 clone_constructors_and_destructors. */
9728 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
9729 clone_function_decl (r
, /*update_method_vec_p=*/0);
9731 else if ((complain
& tf_error
) != 0
9732 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
9733 && !grok_op_properties (r
, /*complain=*/true))
9734 RETURN (error_mark_node
);
9736 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
9737 SET_DECL_FRIEND_CONTEXT (r
,
9738 tsubst (DECL_FRIEND_CONTEXT (t
),
9739 args
, complain
, in_decl
));
9741 /* Possibly limit visibility based on template args. */
9742 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
9743 if (DECL_VISIBILITY_SPECIFIED (t
))
9745 DECL_VISIBILITY_SPECIFIED (r
) = 0;
9747 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
9749 determine_visibility (r
);
9750 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
9751 && !processing_template_decl
)
9752 defaulted_late_check (r
);
9754 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
9755 args
, complain
, in_decl
);
9761 tree type
= NULL_TREE
;
9763 tree expanded_types
= NULL_TREE
;
9764 tree prev_r
= NULL_TREE
;
9765 tree first_r
= NULL_TREE
;
9767 if (FUNCTION_PARAMETER_PACK_P (t
))
9769 /* If there is a local specialization that isn't a
9770 parameter pack, it means that we're doing a "simple"
9771 substitution from inside tsubst_pack_expansion. Just
9772 return the local specialization (which will be a single
9774 tree spec
= retrieve_local_specialization (t
);
9776 && TREE_CODE (spec
) == PARM_DECL
9777 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
9780 /* Expand the TYPE_PACK_EXPANSION that provides the types for
9781 the parameters in this function parameter pack. */
9782 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
9784 if (TREE_CODE (expanded_types
) == TREE_VEC
)
9786 len
= TREE_VEC_LENGTH (expanded_types
);
9788 /* Zero-length parameter packs are boring. Just substitute
9791 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
9796 /* All we did was update the type. Make a note of that. */
9797 type
= expanded_types
;
9798 expanded_types
= NULL_TREE
;
9802 /* Loop through all of the parameter's we'll build. When T is
9803 a function parameter pack, LEN is the number of expanded
9804 types in EXPANDED_TYPES; otherwise, LEN is 1. */
9806 for (i
= 0; i
< len
; ++i
)
9810 if (DECL_TEMPLATE_PARM_P (t
))
9811 SET_DECL_TEMPLATE_PARM_P (r
);
9814 /* We're on the Ith parameter of the function parameter
9817 /* An argument of a function parameter pack is not a parameter
9819 FUNCTION_PARAMETER_PACK_P (r
) = false;
9821 /* Get the Ith type. */
9822 type
= TREE_VEC_ELT (expanded_types
, i
);
9825 /* Rename the parameter to include the index. */
9827 make_ith_pack_parameter_name (DECL_NAME (r
), i
);
9830 /* We're dealing with a normal parameter. */
9831 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9833 type
= type_decays_to (type
);
9834 TREE_TYPE (r
) = type
;
9835 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
9837 if (DECL_INITIAL (r
))
9839 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
9840 DECL_INITIAL (r
) = TREE_TYPE (r
);
9842 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
9846 DECL_CONTEXT (r
) = NULL_TREE
;
9848 if (!DECL_TEMPLATE_PARM_P (r
))
9849 DECL_ARG_TYPE (r
) = type_passed_as (type
);
9851 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
9852 args
, complain
, in_decl
);
9854 /* Keep track of the first new parameter we
9855 generate. That's what will be returned to the
9860 /* Build a proper chain of parameters when substituting
9861 into a function parameter pack. */
9863 DECL_CHAIN (prev_r
) = r
;
9867 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
9868 complain
, DECL_CHAIN (t
));
9870 /* FIRST_R contains the start of the chain we've built. */
9880 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9881 if (type
== error_mark_node
)
9882 RETURN (error_mark_node
);
9883 TREE_TYPE (r
) = type
;
9884 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
9886 /* DECL_INITIAL gives the number of bits in a bit-field. */
9888 = tsubst_expr (DECL_INITIAL (t
), args
,
9890 /*integral_constant_expression_p=*/true);
9891 /* We don't have to set DECL_CONTEXT here; it is set by
9892 finish_member_declaration. */
9893 DECL_CHAIN (r
) = NULL_TREE
;
9894 if (VOID_TYPE_P (type
))
9895 error ("instantiation of %q+D as type %qT", r
, type
);
9897 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
9898 args
, complain
, in_decl
);
9903 /* We reach here only for member using decls. */
9904 if (DECL_DEPENDENT_P (t
))
9906 r
= do_class_using_decl
9907 (tsubst_copy (USING_DECL_SCOPE (t
), args
, complain
, in_decl
),
9908 tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
));
9910 r
= error_mark_node
;
9913 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
9914 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
9920 DECL_CHAIN (r
) = NULL_TREE
;
9927 tree argvec
= NULL_TREE
;
9928 tree gen_tmpl
= NULL_TREE
;
9930 tree tmpl
= NULL_TREE
;
9932 tree type
= NULL_TREE
;
9935 if (TREE_CODE (t
) == TYPE_DECL
9936 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
9938 /* If this is the canonical decl, we don't have to
9939 mess with instantiations, and often we can't (for
9940 typename, template type parms and such). Note that
9941 TYPE_NAME is not correct for the above test if
9942 we've copied the type for a typedef. */
9943 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9944 if (type
== error_mark_node
)
9945 RETURN (error_mark_node
);
9946 r
= TYPE_NAME (type
);
9950 /* Check to see if we already have the specialization we
9953 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
9955 /* T is a static data member or namespace-scope entity.
9956 We have to substitute into namespace-scope variables
9957 (even though such entities are never templates) because
9960 template <class T> void f() { extern T t; }
9962 where the entity referenced is not known until
9963 instantiation time. */
9965 ctx
= DECL_CONTEXT (t
);
9966 if (DECL_CLASS_SCOPE_P (t
))
9968 ctx
= tsubst_aggr_type (ctx
, args
,
9970 in_decl
, /*entering_scope=*/1);
9971 /* If CTX is unchanged, then T is in fact the
9972 specialization we want. That situation occurs when
9973 referencing a static data member within in its own
9974 class. We can use pointer equality, rather than
9975 same_type_p, because DECL_CONTEXT is always
9977 if (ctx
== DECL_CONTEXT (t
))
9983 tmpl
= DECL_TI_TEMPLATE (t
);
9984 gen_tmpl
= most_general_template (tmpl
);
9985 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
9986 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
9987 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
9992 /* A local variable. */
9994 /* Subsequent calls to pushdecl will fill this in. */
9996 spec
= retrieve_local_specialization (t
);
9998 /* If we already have the specialization we need, there is
9999 nothing more to do. */
10006 /* Create a new node for the specialization we need. */
10008 if (type
== NULL_TREE
)
10010 if (is_typedef_decl (t
))
10011 type
= DECL_ORIGINAL_TYPE (t
);
10013 type
= TREE_TYPE (t
);
10014 if (TREE_CODE (t
) == VAR_DECL
&& VAR_HAD_UNKNOWN_BOUND (t
))
10015 type
= strip_array_domain (type
);
10016 type
= tsubst (type
, args
, complain
, in_decl
);
10018 if (TREE_CODE (r
) == VAR_DECL
)
10020 /* Even if the original location is out of scope, the
10021 newly substituted one is not. */
10022 DECL_DEAD_FOR_LOCAL (r
) = 0;
10023 DECL_INITIALIZED_P (r
) = 0;
10024 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
10025 if (type
== error_mark_node
)
10026 RETURN (error_mark_node
);
10027 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10029 /* It may seem that this case cannot occur, since:
10034 declares a function, not a variable. However:
10037 template <typename T> void g() { T t; }
10038 template void g<f>();
10040 is an attempt to declare a variable with function
10042 error ("variable %qD has function type",
10043 /* R is not yet sufficiently initialized, so we
10044 just use its name. */
10046 RETURN (error_mark_node
);
10048 type
= complete_type (type
);
10049 /* Wait until cp_finish_decl to set this again, to handle
10050 circular dependency (template/instantiate6.C). */
10051 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
10052 type
= check_var_type (DECL_NAME (r
), type
);
10054 if (DECL_HAS_VALUE_EXPR_P (t
))
10056 tree ve
= DECL_VALUE_EXPR (t
);
10057 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
10058 /*constant_expression_p=*/false);
10059 SET_DECL_VALUE_EXPR (r
, ve
);
10062 else if (DECL_SELF_REFERENCE_P (t
))
10063 SET_DECL_SELF_REFERENCE_P (r
);
10064 TREE_TYPE (r
) = type
;
10065 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10066 DECL_CONTEXT (r
) = ctx
;
10067 /* Clear out the mangled name and RTL for the instantiation. */
10068 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10069 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10070 SET_DECL_RTL (r
, NULL
);
10071 /* The initializer must not be expanded until it is required;
10072 see [temp.inst]. */
10073 DECL_INITIAL (r
) = NULL_TREE
;
10074 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10075 SET_DECL_RTL (r
, NULL
);
10076 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
10077 if (TREE_CODE (r
) == VAR_DECL
)
10079 /* Possibly limit visibility based on template args. */
10080 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10081 if (DECL_VISIBILITY_SPECIFIED (t
))
10083 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10084 DECL_ATTRIBUTES (r
)
10085 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10087 determine_visibility (r
);
10092 /* A static data member declaration is always marked
10093 external when it is declared in-class, even if an
10094 initializer is present. We mimic the non-template
10095 processing here. */
10096 DECL_EXTERNAL (r
) = 1;
10098 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10099 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
10100 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10102 else if (cp_unevaluated_operand
)
10104 /* We're substituting this var in a decltype outside of its
10105 scope, such as for a lambda return type. Don't add it to
10106 local_specializations, do perform auto deduction. */
10107 tree auto_node
= type_uses_auto (type
);
10109 = tsubst_expr (DECL_INITIAL (t
), args
, complain
, in_decl
,
10110 /*constant_expression_p=*/false);
10112 if (auto_node
&& init
)
10114 init
= resolve_nondeduced_context (init
);
10115 if (describable_type (init
))
10117 type
= do_auto_deduction (type
, init
, auto_node
);
10118 TREE_TYPE (r
) = type
;
10123 register_local_specialization (r
, t
);
10125 DECL_CHAIN (r
) = NULL_TREE
;
10127 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
10129 args
, complain
, in_decl
);
10131 /* Preserve a typedef that names a type. */
10132 if (is_typedef_decl (r
))
10134 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
10135 set_underlying_type (r
);
10138 layout_decl (r
, 0);
10143 gcc_unreachable ();
10148 /* Restore the file and line information. */
10149 input_location
= saved_loc
;
10154 /* Substitute into the ARG_TYPES of a function type. */
10157 tsubst_arg_types (tree arg_types
,
10159 tsubst_flags_t complain
,
10162 tree remaining_arg_types
;
10163 tree type
= NULL_TREE
;
10165 tree expanded_args
= NULL_TREE
;
10168 if (!arg_types
|| arg_types
== void_list_node
)
10171 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
10172 args
, complain
, in_decl
);
10173 if (remaining_arg_types
== error_mark_node
)
10174 return error_mark_node
;
10176 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
10178 /* For a pack expansion, perform substitution on the
10179 entire expression. Later on, we'll handle the arguments
10181 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
10182 args
, complain
, in_decl
);
10184 if (TREE_CODE (expanded_args
) == TREE_VEC
)
10185 /* So that we'll spin through the parameters, one by one. */
10186 i
= TREE_VEC_LENGTH (expanded_args
);
10189 /* We only partially substituted into the parameter
10190 pack. Our type is TYPE_PACK_EXPANSION. */
10191 type
= expanded_args
;
10192 expanded_args
= NULL_TREE
;
10200 type
= TREE_VEC_ELT (expanded_args
, i
);
10202 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
10204 if (type
== error_mark_node
)
10205 return error_mark_node
;
10206 if (VOID_TYPE_P (type
))
10208 if (complain
& tf_error
)
10210 error ("invalid parameter type %qT", type
);
10212 error ("in declaration %q+D", in_decl
);
10214 return error_mark_node
;
10217 /* Do array-to-pointer, function-to-pointer conversion, and ignore
10218 top-level qualifiers as required. */
10219 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
10221 /* We do not substitute into default arguments here. The standard
10222 mandates that they be instantiated only when needed, which is
10223 done in build_over_call. */
10224 default_arg
= TREE_PURPOSE (arg_types
);
10226 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
10228 /* We've instantiated a template before its default arguments
10229 have been parsed. This can happen for a nested template
10230 class, and is not an error unless we require the default
10231 argument in a call of this function. */
10232 remaining_arg_types
=
10233 tree_cons (default_arg
, type
, remaining_arg_types
);
10234 VEC_safe_push (tree
, gc
, DEFARG_INSTANTIATIONS (default_arg
),
10235 remaining_arg_types
);
10238 remaining_arg_types
=
10239 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
10242 return remaining_arg_types
;
10245 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
10246 *not* handle the exception-specification for FNTYPE, because the
10247 initial substitution of explicitly provided template parameters
10248 during argument deduction forbids substitution into the
10249 exception-specification:
10253 All references in the function type of the function template to the
10254 corresponding template parameters are replaced by the specified tem-
10255 plate argument values. If a substitution in a template parameter or
10256 in the function type of the function template results in an invalid
10257 type, type deduction fails. [Note: The equivalent substitution in
10258 exception specifications is done only when the function is instanti-
10259 ated, at which point a program is ill-formed if the substitution
10260 results in an invalid type.] */
10263 tsubst_function_type (tree t
,
10265 tsubst_flags_t complain
,
10272 /* The TYPE_CONTEXT is not used for function/method types. */
10273 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
10275 /* Substitute the return type. */
10276 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10277 if (return_type
== error_mark_node
)
10278 return error_mark_node
;
10279 /* The standard does not presently indicate that creation of a
10280 function type with an invalid return type is a deduction failure.
10281 However, that is clearly analogous to creating an array of "void"
10282 or a reference to a reference. This is core issue #486. */
10283 if (TREE_CODE (return_type
) == ARRAY_TYPE
10284 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
10286 if (complain
& tf_error
)
10288 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
10289 error ("function returning an array");
10291 error ("function returning a function");
10293 return error_mark_node
;
10296 /* Substitute the argument types. */
10297 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
,
10298 complain
, in_decl
);
10299 if (arg_types
== error_mark_node
)
10300 return error_mark_node
;
10302 /* Construct a new type node and return it. */
10303 if (TREE_CODE (t
) == FUNCTION_TYPE
)
10305 fntype
= build_function_type (return_type
, arg_types
);
10306 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
10310 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
10311 if (! MAYBE_CLASS_TYPE_P (r
))
10315 Type deduction may fail for any of the following
10318 -- Attempting to create "pointer to member of T" when T
10319 is not a class type. */
10320 if (complain
& tf_error
)
10321 error ("creating pointer to member function of non-class type %qT",
10323 return error_mark_node
;
10326 fntype
= build_method_type_directly (r
, return_type
,
10327 TREE_CHAIN (arg_types
));
10329 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
10334 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
10335 ARGS into that specification, and return the substituted
10336 specification. If there is no specification, return NULL_TREE. */
10339 tsubst_exception_specification (tree fntype
,
10341 tsubst_flags_t complain
,
10347 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
10348 new_specs
= NULL_TREE
;
10349 if (specs
&& TREE_PURPOSE (specs
))
10351 /* A noexcept-specifier. */
10352 new_specs
= tsubst_copy_and_build
10353 (TREE_PURPOSE (specs
), args
, complain
, in_decl
, /*function_p=*/false,
10354 /*integral_constant_expression_p=*/true);
10355 new_specs
= build_noexcept_spec (new_specs
, complain
);
10359 if (! TREE_VALUE (specs
))
10366 tree expanded_specs
= NULL_TREE
;
10368 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
10370 /* Expand the pack expansion type. */
10371 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
10375 if (expanded_specs
== error_mark_node
)
10376 return error_mark_node
;
10377 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
10378 len
= TREE_VEC_LENGTH (expanded_specs
);
10381 /* We're substituting into a member template, so
10382 we got a TYPE_PACK_EXPANSION back. Add that
10383 expansion and move on. */
10384 gcc_assert (TREE_CODE (expanded_specs
)
10385 == TYPE_PACK_EXPANSION
);
10386 new_specs
= add_exception_specifier (new_specs
,
10389 specs
= TREE_CHAIN (specs
);
10394 for (i
= 0; i
< len
; ++i
)
10396 if (expanded_specs
)
10397 spec
= TREE_VEC_ELT (expanded_specs
, i
);
10399 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
10400 if (spec
== error_mark_node
)
10402 new_specs
= add_exception_specifier (new_specs
, spec
,
10406 specs
= TREE_CHAIN (specs
);
10412 /* Take the tree structure T and replace template parameters used
10413 therein with the argument vector ARGS. IN_DECL is an associated
10414 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
10415 Issue error and warning messages under control of COMPLAIN. Note
10416 that we must be relatively non-tolerant of extensions here, in
10417 order to preserve conformance; if we allow substitutions that
10418 should not be allowed, we may allow argument deductions that should
10419 not succeed, and therefore report ambiguous overload situations
10420 where there are none. In theory, we could allow the substitution,
10421 but indicate that it should have failed, and allow our caller to
10422 make sure that the right thing happens, but we don't try to do this
10425 This function is used for dealing with types, decls and the like;
10426 for expressions, use tsubst_expr or tsubst_copy. */
10429 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10431 enum tree_code code
;
10434 if (t
== NULL_TREE
|| t
== error_mark_node
10435 || t
== integer_type_node
10436 || t
== void_type_node
10437 || t
== char_type_node
10438 || t
== unknown_type_node
10439 || TREE_CODE (t
) == NAMESPACE_DECL
10440 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
10444 return tsubst_decl (t
, args
, complain
);
10446 if (args
== NULL_TREE
)
10449 code
= TREE_CODE (t
);
10451 if (code
== IDENTIFIER_NODE
)
10452 type
= IDENTIFIER_TYPE_VALUE (t
);
10454 type
= TREE_TYPE (t
);
10456 gcc_assert (type
!= unknown_type_node
);
10458 /* Reuse typedefs. We need to do this to handle dependent attributes,
10459 such as attribute aligned. */
10461 && typedef_variant_p (t
))
10463 tree decl
= TYPE_NAME (t
);
10465 if (DECL_CLASS_SCOPE_P (decl
)
10466 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
10467 && uses_template_parms (DECL_CONTEXT (decl
)))
10469 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
10470 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
10471 r
= retrieve_specialization (tmpl
, gen_args
, 0);
10473 else if (DECL_FUNCTION_SCOPE_P (decl
)
10474 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
10475 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
10476 r
= retrieve_local_specialization (decl
);
10478 /* The typedef is from a non-template context. */
10484 r
= cp_build_qualified_type_real
10485 (r
, cp_type_quals (t
) | cp_type_quals (r
),
10486 complain
| tf_ignore_bad_quals
);
10489 /* Else we must be instantiating the typedef, so fall through. */
10493 && code
!= TYPENAME_TYPE
10494 && code
!= TEMPLATE_TYPE_PARM
10495 && code
!= IDENTIFIER_NODE
10496 && code
!= FUNCTION_TYPE
10497 && code
!= METHOD_TYPE
)
10498 type
= tsubst (type
, args
, complain
, in_decl
);
10499 if (type
== error_mark_node
)
10500 return error_mark_node
;
10506 case ENUMERAL_TYPE
:
10507 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
10508 /*entering_scope=*/0);
10511 case IDENTIFIER_NODE
:
10522 if (t
== integer_type_node
)
10525 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
10526 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
10530 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
10532 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
10533 /*integral_constant_expression_p=*/false);
10535 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
10537 if (TREE_CODE (max
) == NOP_EXPR
10538 && TREE_SIDE_EFFECTS (omax
)
10539 && !TREE_TYPE (max
))
10540 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
10542 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
10543 with TREE_SIDE_EFFECTS that indicates this is not an integral
10544 constant expression. */
10545 if (processing_template_decl
10546 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
10548 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
10549 TREE_SIDE_EFFECTS (max
) = 1;
10552 return compute_array_index_type (NULL_TREE
, max
, complain
);
10555 case TEMPLATE_TYPE_PARM
:
10556 case TEMPLATE_TEMPLATE_PARM
:
10557 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10558 case TEMPLATE_PARM_INDEX
:
10563 tree arg
= NULL_TREE
;
10567 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
10568 template_parm_level_and_index (t
, &level
, &idx
);
10570 levels
= TMPL_ARGS_DEPTH (args
);
10571 if (level
<= levels
)
10573 arg
= TMPL_ARG (args
, level
, idx
);
10575 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
10576 /* See through ARGUMENT_PACK_SELECT arguments. */
10577 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
10580 if (arg
== error_mark_node
)
10581 return error_mark_node
;
10582 else if (arg
!= NULL_TREE
)
10584 if (ARGUMENT_PACK_P (arg
))
10585 /* If ARG is an argument pack, we don't actually want to
10586 perform a substitution here, because substitutions
10587 for argument packs are only done
10588 element-by-element. We can get to this point when
10589 substituting the type of a non-type template
10590 parameter pack, when that type actually contains
10591 template parameter packs from an outer template, e.g.,
10593 template<typename... Types> struct A {
10594 template<Types... Values> struct B { };
10598 if (code
== TEMPLATE_TYPE_PARM
)
10601 gcc_assert (TYPE_P (arg
));
10603 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
10605 return cp_build_qualified_type_real
10606 (arg
, quals
, complain
| tf_ignore_bad_quals
);
10608 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
10610 /* We are processing a type constructed from a
10611 template template parameter. */
10612 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
10613 args
, complain
, in_decl
);
10614 if (argvec
== error_mark_node
)
10615 return error_mark_node
;
10617 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
10618 are resolving nested-types in the signature of a
10619 member function templates. Otherwise ARG is a
10620 TEMPLATE_DECL and is the real template to be
10622 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
10623 arg
= TYPE_NAME (arg
);
10625 r
= lookup_template_class (arg
,
10627 DECL_CONTEXT (arg
),
10628 /*entering_scope=*/0,
10630 return cp_build_qualified_type_real
10631 (r
, cp_type_quals (t
), complain
);
10634 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
10635 return unshare_expr (arg
);
10639 /* This can happen during the attempted tsubst'ing in
10640 unify. This means that we don't yet have any information
10641 about the template parameter in question. */
10644 /* If we get here, we must have been looking at a parm for a
10645 more deeply nested template. Make a new version of this
10646 template parameter, but with a lower level. */
10649 case TEMPLATE_TYPE_PARM
:
10650 case TEMPLATE_TEMPLATE_PARM
:
10651 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10652 if (cp_type_quals (t
))
10654 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
10655 r
= cp_build_qualified_type_real
10656 (r
, cp_type_quals (t
),
10657 complain
| (code
== TEMPLATE_TYPE_PARM
10658 ? tf_ignore_bad_quals
: 0));
10663 TEMPLATE_TYPE_PARM_INDEX (r
)
10664 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
10665 r
, levels
, args
, complain
);
10666 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
10667 TYPE_MAIN_VARIANT (r
) = r
;
10668 TYPE_POINTER_TO (r
) = NULL_TREE
;
10669 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
10671 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
10672 /* We have reduced the level of the template
10673 template parameter, but not the levels of its
10674 template parameters, so canonical_type_parameter
10675 will not be able to find the canonical template
10676 template parameter for this level. Thus, we
10677 require structural equality checking to compare
10678 TEMPLATE_TEMPLATE_PARMs. */
10679 SET_TYPE_STRUCTURAL_EQUALITY (r
);
10680 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
10681 SET_TYPE_STRUCTURAL_EQUALITY (r
);
10683 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
10685 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
10687 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
10688 complain
, in_decl
);
10689 if (argvec
== error_mark_node
)
10690 return error_mark_node
;
10692 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
10693 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
10698 case TEMPLATE_PARM_INDEX
:
10699 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
10703 gcc_unreachable ();
10711 tree purpose
, value
, chain
;
10713 if (t
== void_list_node
)
10716 purpose
= TREE_PURPOSE (t
);
10719 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
10720 if (purpose
== error_mark_node
)
10721 return error_mark_node
;
10723 value
= TREE_VALUE (t
);
10726 value
= tsubst (value
, args
, complain
, in_decl
);
10727 if (value
== error_mark_node
)
10728 return error_mark_node
;
10730 chain
= TREE_CHAIN (t
);
10731 if (chain
&& chain
!= void_type_node
)
10733 chain
= tsubst (chain
, args
, complain
, in_decl
);
10734 if (chain
== error_mark_node
)
10735 return error_mark_node
;
10737 if (purpose
== TREE_PURPOSE (t
)
10738 && value
== TREE_VALUE (t
)
10739 && chain
== TREE_CHAIN (t
))
10741 return hash_tree_cons (purpose
, value
, chain
);
10745 /* We should never be tsubsting a binfo. */
10746 gcc_unreachable ();
10749 /* A vector of template arguments. */
10750 gcc_assert (!type
);
10751 return tsubst_template_args (t
, args
, complain
, in_decl
);
10754 case REFERENCE_TYPE
:
10756 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
10761 Type deduction may fail for any of the following
10764 -- Attempting to create a pointer to reference type.
10765 -- Attempting to create a reference to a reference type or
10766 a reference to void.
10768 Core issue 106 says that creating a reference to a reference
10769 during instantiation is no longer a cause for failure. We
10770 only enforce this check in strict C++98 mode. */
10771 if ((TREE_CODE (type
) == REFERENCE_TYPE
10772 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
10773 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
10775 static location_t last_loc
;
10777 /* We keep track of the last time we issued this error
10778 message to avoid spewing a ton of messages during a
10779 single bad template instantiation. */
10780 if (complain
& tf_error
10781 && last_loc
!= input_location
)
10783 if (TREE_CODE (type
) == VOID_TYPE
)
10784 error ("forming reference to void");
10785 else if (code
== POINTER_TYPE
)
10786 error ("forming pointer to reference type %qT", type
);
10788 error ("forming reference to reference type %qT", type
);
10789 last_loc
= input_location
;
10792 return error_mark_node
;
10794 else if (code
== POINTER_TYPE
)
10796 r
= build_pointer_type (type
);
10797 if (TREE_CODE (type
) == METHOD_TYPE
)
10798 r
= build_ptrmemfunc_type (r
);
10800 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
10801 /* In C++0x, during template argument substitution, when there is an
10802 attempt to create a reference to a reference type, reference
10803 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
10805 "If a template-argument for a template-parameter T names a type
10806 that is a reference to a type A, an attempt to create the type
10807 'lvalue reference to cv T' creates the type 'lvalue reference to
10808 A,' while an attempt to create the type type rvalue reference to
10809 cv T' creates the type T"
10811 r
= cp_build_reference_type
10813 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
10815 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
10816 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
10818 if (r
!= error_mark_node
)
10819 /* Will this ever be needed for TYPE_..._TO values? */
10826 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
10827 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
10831 Type deduction may fail for any of the following
10834 -- Attempting to create "pointer to member of T" when T
10835 is not a class type. */
10836 if (complain
& tf_error
)
10837 error ("creating pointer to member of non-class type %qT", r
);
10838 return error_mark_node
;
10840 if (TREE_CODE (type
) == REFERENCE_TYPE
)
10842 if (complain
& tf_error
)
10843 error ("creating pointer to member reference type %qT", type
);
10844 return error_mark_node
;
10846 if (TREE_CODE (type
) == VOID_TYPE
)
10848 if (complain
& tf_error
)
10849 error ("creating pointer to member of type void");
10850 return error_mark_node
;
10852 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
10853 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10855 /* The type of the implicit object parameter gets its
10856 cv-qualifiers from the FUNCTION_TYPE. */
10858 tree method_type
= build_memfn_type (type
, r
, type_memfn_quals (type
));
10859 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
10860 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
10864 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
10868 case FUNCTION_TYPE
:
10873 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
10874 if (fntype
== error_mark_node
)
10875 return error_mark_node
;
10877 /* Substitute the exception specification. */
10878 specs
= tsubst_exception_specification (t
, args
, complain
,
10880 if (specs
== error_mark_node
)
10881 return error_mark_node
;
10883 fntype
= build_exception_variant (fntype
, specs
);
10888 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
10889 if (domain
== error_mark_node
)
10890 return error_mark_node
;
10892 /* As an optimization, we avoid regenerating the array type if
10893 it will obviously be the same as T. */
10894 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
10897 /* These checks should match the ones in grokdeclarator.
10901 The deduction may fail for any of the following reasons:
10903 -- Attempting to create an array with an element type that
10904 is void, a function type, or a reference type, or [DR337]
10905 an abstract class type. */
10906 if (TREE_CODE (type
) == VOID_TYPE
10907 || TREE_CODE (type
) == FUNCTION_TYPE
10908 || TREE_CODE (type
) == REFERENCE_TYPE
)
10910 if (complain
& tf_error
)
10911 error ("creating array of %qT", type
);
10912 return error_mark_node
;
10914 if (CLASS_TYPE_P (type
) && CLASSTYPE_PURE_VIRTUALS (type
))
10916 if (complain
& tf_error
)
10917 error ("creating array of %qT, which is an abstract class type",
10919 return error_mark_node
;
10922 r
= build_cplus_array_type (type
, domain
);
10924 if (TYPE_USER_ALIGN (t
))
10926 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
10927 TYPE_USER_ALIGN (r
) = 1;
10933 case TYPENAME_TYPE
:
10935 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
10936 in_decl
, /*entering_scope=*/1);
10937 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
10938 complain
, in_decl
);
10940 if (ctx
== error_mark_node
|| f
== error_mark_node
)
10941 return error_mark_node
;
10943 if (!MAYBE_CLASS_TYPE_P (ctx
))
10945 if (complain
& tf_error
)
10946 error ("%qT is not a class, struct, or union type", ctx
);
10947 return error_mark_node
;
10949 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
10951 /* Normally, make_typename_type does not require that the CTX
10952 have complete type in order to allow things like:
10954 template <class T> struct S { typename S<T>::X Y; };
10956 But, such constructs have already been resolved by this
10957 point, so here CTX really should have complete type, unless
10958 it's a partial instantiation. */
10959 ctx
= complete_type (ctx
);
10960 if (!COMPLETE_TYPE_P (ctx
))
10962 if (complain
& tf_error
)
10963 cxx_incomplete_type_error (NULL_TREE
, ctx
);
10964 return error_mark_node
;
10968 f
= make_typename_type (ctx
, f
, typename_type
,
10969 (complain
& tf_error
) | tf_keep_type_decl
);
10970 if (f
== error_mark_node
)
10972 if (TREE_CODE (f
) == TYPE_DECL
)
10974 complain
|= tf_ignore_bad_quals
;
10978 if (TREE_CODE (f
) != TYPENAME_TYPE
)
10980 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
10982 if (complain
& tf_error
)
10983 error ("%qT resolves to %qT, which is not an enumeration type",
10986 return error_mark_node
;
10988 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
10990 if (complain
& tf_error
)
10991 error ("%qT resolves to %qT, which is is not a class type",
10994 return error_mark_node
;
10998 return cp_build_qualified_type_real
10999 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
11002 case UNBOUND_CLASS_TEMPLATE
:
11004 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11005 in_decl
, /*entering_scope=*/1);
11006 tree name
= TYPE_IDENTIFIER (t
);
11007 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
11009 if (ctx
== error_mark_node
|| name
== error_mark_node
)
11010 return error_mark_node
;
11013 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
11014 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
11021 ++cp_unevaluated_operand
;
11022 ++c_inhibit_evaluation_warnings
;
11024 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
11026 /*integral_constant_expression_p=*/false);
11028 --cp_unevaluated_operand
;
11029 --c_inhibit_evaluation_warnings
;
11031 type
= finish_typeof (type
);
11032 return cp_build_qualified_type_real (type
,
11034 | cp_type_quals (type
),
11038 case DECLTYPE_TYPE
:
11042 ++cp_unevaluated_operand
;
11043 ++c_inhibit_evaluation_warnings
;
11045 type
= tsubst_expr (DECLTYPE_TYPE_EXPR (t
), args
,
11047 /*integral_constant_expression_p=*/false);
11049 --cp_unevaluated_operand
;
11050 --c_inhibit_evaluation_warnings
;
11052 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
11053 type
= lambda_capture_field_type (type
);
11054 else if (DECLTYPE_FOR_LAMBDA_RETURN (t
))
11055 type
= lambda_return_type (type
);
11057 type
= finish_decltype_type
11058 (type
, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
), complain
);
11059 return cp_build_qualified_type_real (type
,
11061 | cp_type_quals (type
),
11065 case UNDERLYING_TYPE
:
11067 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
11068 complain
, in_decl
);
11069 return finish_underlying_type (type
);
11072 case TYPE_ARGUMENT_PACK
:
11073 case NONTYPE_ARGUMENT_PACK
:
11075 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
11077 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
11081 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
11083 /* For template nontype argument packs, also substitute into
11085 if (code
== NONTYPE_ARGUMENT_PACK
)
11086 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11104 /* We should use one of the expression tsubsts for these codes. */
11105 gcc_unreachable ();
11108 sorry ("use of %qs in template", tree_code_name
[(int) code
]);
11109 return error_mark_node
;
11113 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
11114 type of the expression on the left-hand side of the "." or "->"
11118 tsubst_baselink (tree baselink
, tree object_type
,
11119 tree args
, tsubst_flags_t complain
, tree in_decl
)
11122 tree qualifying_scope
;
11125 tree template_args
= 0;
11126 bool template_id_p
= false;
11128 /* A baselink indicates a function from a base class. Both the
11129 BASELINK_ACCESS_BINFO and the base class referenced may
11130 indicate bases of the template class, rather than the
11131 instantiated class. In addition, lookups that were not
11132 ambiguous before may be ambiguous now. Therefore, we perform
11133 the lookup again. */
11134 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
11135 qualifying_scope
= tsubst (qualifying_scope
, args
,
11136 complain
, in_decl
);
11137 fns
= BASELINK_FUNCTIONS (baselink
);
11138 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
11139 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
11141 template_id_p
= true;
11142 template_args
= TREE_OPERAND (fns
, 1);
11143 fns
= TREE_OPERAND (fns
, 0);
11145 template_args
= tsubst_template_args (template_args
, args
,
11146 complain
, in_decl
);
11148 name
= DECL_NAME (get_first_fn (fns
));
11149 if (IDENTIFIER_TYPENAME_P (name
))
11150 name
= mangle_conv_op_name_for_type (optype
);
11151 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
11153 return error_mark_node
;
11155 /* If lookup found a single function, mark it as used at this
11156 point. (If it lookup found multiple functions the one selected
11157 later by overload resolution will be marked as used at that
11159 if (BASELINK_P (baselink
))
11160 fns
= BASELINK_FUNCTIONS (baselink
);
11161 if (!template_id_p
&& !really_overloaded_fn (fns
))
11162 mark_used (OVL_CURRENT (fns
));
11164 /* Add back the template arguments, if present. */
11165 if (BASELINK_P (baselink
) && template_id_p
)
11166 BASELINK_FUNCTIONS (baselink
)
11167 = build_nt (TEMPLATE_ID_EXPR
,
11168 BASELINK_FUNCTIONS (baselink
),
11170 /* Update the conversion operator type. */
11171 BASELINK_OPTYPE (baselink
) = optype
;
11174 object_type
= current_class_type
;
11175 return adjust_result_of_qualified_name_lookup (baselink
,
11180 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
11181 true if the qualified-id will be a postfix-expression in-and-of
11182 itself; false if more of the postfix-expression follows the
11183 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
11187 tsubst_qualified_id (tree qualified_id
, tree args
,
11188 tsubst_flags_t complain
, tree in_decl
,
11189 bool done
, bool address_p
)
11195 tree template_args
;
11197 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
11199 /* Figure out what name to look up. */
11200 name
= TREE_OPERAND (qualified_id
, 1);
11201 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
11203 is_template
= true;
11204 template_args
= TREE_OPERAND (name
, 1);
11206 template_args
= tsubst_template_args (template_args
, args
,
11207 complain
, in_decl
);
11208 name
= TREE_OPERAND (name
, 0);
11212 is_template
= false;
11213 template_args
= NULL_TREE
;
11216 /* Substitute into the qualifying scope. When there are no ARGS, we
11217 are just trying to simplify a non-dependent expression. In that
11218 case the qualifying scope may be dependent, and, in any case,
11219 substituting will not help. */
11220 scope
= TREE_OPERAND (qualified_id
, 0);
11223 scope
= tsubst (scope
, args
, complain
, in_decl
);
11224 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
11229 if (dependent_scope_p (scope
))
11230 return build_qualified_name (NULL_TREE
, scope
, expr
,
11231 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
11233 if (!BASELINK_P (name
) && !DECL_P (expr
))
11235 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
11237 /* A BIT_NOT_EXPR is used to represent a destructor. */
11238 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
11240 error ("qualifying type %qT does not match destructor name ~%qT",
11241 scope
, TREE_OPERAND (expr
, 0));
11242 expr
= error_mark_node
;
11245 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
11246 /*is_type_p=*/0, false);
11249 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
11250 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
11251 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
11253 if (complain
& tf_error
)
11255 error ("dependent-name %qE is parsed as a non-type, but "
11256 "instantiation yields a type", qualified_id
);
11257 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
11259 return error_mark_node
;
11265 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
11267 /* Remember that there was a reference to this entity. */
11271 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
11273 if (complain
& tf_error
)
11274 qualified_name_lookup_error (scope
,
11275 TREE_OPERAND (qualified_id
, 1),
11276 expr
, input_location
);
11277 return error_mark_node
;
11281 expr
= lookup_template_function (expr
, template_args
);
11283 if (expr
== error_mark_node
&& complain
& tf_error
)
11284 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
11285 expr
, input_location
);
11286 else if (TYPE_P (scope
))
11288 expr
= (adjust_result_of_qualified_name_lookup
11289 (expr
, scope
, current_class_type
));
11290 expr
= (finish_qualified_id_expr
11291 (scope
, expr
, done
, address_p
,
11292 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
11293 /*template_arg_p=*/false));
11296 /* Expressions do not generally have reference type. */
11297 if (TREE_CODE (expr
) != SCOPE_REF
11298 /* However, if we're about to form a pointer-to-member, we just
11299 want the referenced member referenced. */
11300 && TREE_CODE (expr
) != OFFSET_REF
)
11301 expr
= convert_from_reference (expr
);
11306 /* Like tsubst, but deals with expressions. This function just replaces
11307 template parms; to finish processing the resultant expression, use
11311 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11313 enum tree_code code
;
11316 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
11319 code
= TREE_CODE (t
);
11324 r
= retrieve_local_specialization (t
);
11329 /* This can happen for a parameter name used later in a function
11330 declaration (such as in a late-specified return type). Just
11331 make a dummy decl, since it's only used for its type. */
11332 gcc_assert (cp_unevaluated_operand
!= 0);
11333 /* We copy T because want to tsubst the PARM_DECL only,
11334 not the following PARM_DECLs that are chained to T. */
11336 r
= tsubst_decl (c
, args
, complain
);
11337 /* Give it the template pattern as its context; its true context
11338 hasn't been instantiated yet and this is good enough for
11340 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
11343 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
11344 r
= ARGUMENT_PACK_SELECT_ARG (r
);
11353 if (DECL_TEMPLATE_PARM_P (t
))
11354 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
11355 /* There is no need to substitute into namespace-scope
11357 if (DECL_NAMESPACE_SCOPE_P (t
))
11359 /* If ARGS is NULL, then T is known to be non-dependent. */
11360 if (args
== NULL_TREE
)
11361 return integral_constant_value (t
);
11363 /* Unfortunately, we cannot just call lookup_name here.
11366 template <int I> int f() {
11368 struct S { void g() { E e = a; } };
11371 When we instantiate f<7>::S::g(), say, lookup_name is not
11372 clever enough to find f<7>::a. */
11374 = tsubst_aggr_type (TREE_TYPE (t
), args
, complain
, in_decl
,
11375 /*entering_scope=*/0);
11377 for (v
= TYPE_VALUES (enum_type
);
11379 v
= TREE_CHAIN (v
))
11380 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
11381 return TREE_VALUE (v
);
11383 /* We didn't find the name. That should never happen; if
11384 name-lookup found it during preliminary parsing, we
11385 should find it again here during instantiation. */
11386 gcc_unreachable ();
11391 if (DECL_CONTEXT (t
))
11395 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
11396 /*entering_scope=*/1);
11397 if (ctx
!= DECL_CONTEXT (t
))
11399 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
11402 if (complain
& tf_error
)
11403 error ("using invalid field %qD", t
);
11404 return error_mark_node
;
11413 case FUNCTION_DECL
:
11414 if ((DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
11415 || local_variable_p (t
))
11416 t
= tsubst (t
, args
, complain
, in_decl
);
11421 /* An OVERLOAD will always be a non-dependent overload set; an
11422 overload set from function scope will just be represented with an
11423 IDENTIFIER_NODE, and from class scope with a BASELINK. */
11424 gcc_assert (!uses_template_parms (t
));
11428 return tsubst_baselink (t
, current_class_type
, args
, complain
, in_decl
);
11430 case TEMPLATE_DECL
:
11431 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11432 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
11433 args
, complain
, in_decl
);
11434 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
11435 return tsubst (t
, args
, complain
, in_decl
);
11436 else if (DECL_CLASS_SCOPE_P (t
)
11437 && uses_template_parms (DECL_CONTEXT (t
)))
11439 /* Template template argument like the following example need
11442 template <template <class> class TT> struct C {};
11443 template <class T> struct D {
11444 template <class U> struct E {};
11449 We are processing the template argument `E' in #1 for
11450 the template instantiation #2. Originally, `E' is a
11451 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
11452 have to substitute this with one having context `D<int>'. */
11454 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
11455 return lookup_field (context
, DECL_NAME(t
), 0, false);
11458 /* Ordinary template template argument. */
11462 case REINTERPRET_CAST_EXPR
:
11463 case CONST_CAST_EXPR
:
11464 case STATIC_CAST_EXPR
:
11465 case DYNAMIC_CAST_EXPR
:
11468 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
11469 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
11472 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
11478 ++cp_unevaluated_operand
;
11479 ++c_inhibit_evaluation_warnings
;
11480 /* We only want to compute the number of arguments. */
11481 expanded
= tsubst_pack_expansion (TREE_OPERAND (t
, 0), args
,
11482 complain
, in_decl
);
11483 --cp_unevaluated_operand
;
11484 --c_inhibit_evaluation_warnings
;
11486 if (TREE_CODE (expanded
) == TREE_VEC
)
11487 len
= TREE_VEC_LENGTH (expanded
);
11489 if (expanded
== error_mark_node
)
11490 return error_mark_node
;
11491 else if (PACK_EXPANSION_P (expanded
)
11492 || (TREE_CODE (expanded
) == TREE_VEC
11494 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
11496 if (TREE_CODE (expanded
) == TREE_VEC
)
11497 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
11499 if (TYPE_P (expanded
))
11500 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
11501 complain
& tf_error
);
11503 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
11504 complain
& tf_error
);
11507 return build_int_cst (size_type_node
, len
);
11513 case TRUTH_NOT_EXPR
:
11516 case UNARY_PLUS_EXPR
: /* Unary + */
11518 case AT_ENCODE_EXPR
:
11522 case REALPART_EXPR
:
11523 case IMAGPART_EXPR
:
11525 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
11526 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
11528 case COMPONENT_REF
:
11533 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
11534 name
= TREE_OPERAND (t
, 1);
11535 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
11537 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
11538 complain
, in_decl
);
11539 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
11541 else if (TREE_CODE (name
) == SCOPE_REF
11542 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
11544 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
11545 complain
, in_decl
);
11546 name
= TREE_OPERAND (name
, 1);
11547 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
11548 complain
, in_decl
);
11549 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
11550 name
= build_qualified_name (/*type=*/NULL_TREE
,
11552 /*template_p=*/false);
11554 else if (TREE_CODE (name
) == BASELINK
)
11555 name
= tsubst_baselink (name
,
11556 non_reference (TREE_TYPE (object
)),
11560 name
= tsubst_copy (name
, args
, complain
, in_decl
);
11561 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
11567 case TRUNC_DIV_EXPR
:
11568 case CEIL_DIV_EXPR
:
11569 case FLOOR_DIV_EXPR
:
11570 case ROUND_DIV_EXPR
:
11571 case EXACT_DIV_EXPR
:
11575 case TRUNC_MOD_EXPR
:
11576 case FLOOR_MOD_EXPR
:
11577 case TRUTH_ANDIF_EXPR
:
11578 case TRUTH_ORIF_EXPR
:
11579 case TRUTH_AND_EXPR
:
11580 case TRUTH_OR_EXPR
:
11593 case COMPOUND_EXPR
:
11596 case PREDECREMENT_EXPR
:
11597 case PREINCREMENT_EXPR
:
11598 case POSTDECREMENT_EXPR
:
11599 case POSTINCREMENT_EXPR
:
11601 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11602 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
11605 return build_qualified_name (/*type=*/NULL_TREE
,
11606 tsubst_copy (TREE_OPERAND (t
, 0),
11607 args
, complain
, in_decl
),
11608 tsubst_copy (TREE_OPERAND (t
, 1),
11609 args
, complain
, in_decl
),
11610 QUALIFIED_NAME_IS_TEMPLATE (t
));
11615 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11616 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
11617 NULL_TREE
, NULL_TREE
);
11621 int n
= VL_EXP_OPERAND_LENGTH (t
);
11622 tree result
= build_vl_exp (CALL_EXPR
, n
);
11624 for (i
= 0; i
< n
; i
++)
11625 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
11626 complain
, in_decl
);
11632 case PSEUDO_DTOR_EXPR
:
11635 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11636 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
11637 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
11638 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
11645 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11646 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
11647 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
11648 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
11655 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11656 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
11657 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
11658 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
11662 case TEMPLATE_ID_EXPR
:
11664 /* Substituted template arguments */
11665 tree fn
= TREE_OPERAND (t
, 0);
11666 tree targs
= TREE_OPERAND (t
, 1);
11668 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
11670 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
11672 return lookup_template_function (fn
, targs
);
11677 tree purpose
, value
, chain
;
11679 if (t
== void_list_node
)
11682 purpose
= TREE_PURPOSE (t
);
11684 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
11685 value
= TREE_VALUE (t
);
11687 value
= tsubst_copy (value
, args
, complain
, in_decl
);
11688 chain
= TREE_CHAIN (t
);
11689 if (chain
&& chain
!= void_type_node
)
11690 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
11691 if (purpose
== TREE_PURPOSE (t
)
11692 && value
== TREE_VALUE (t
)
11693 && chain
== TREE_CHAIN (t
))
11695 return tree_cons (purpose
, value
, chain
);
11700 case ENUMERAL_TYPE
:
11702 case TEMPLATE_TYPE_PARM
:
11703 case TEMPLATE_TEMPLATE_PARM
:
11704 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11705 case TEMPLATE_PARM_INDEX
:
11707 case REFERENCE_TYPE
:
11709 case FUNCTION_TYPE
:
11712 case TYPENAME_TYPE
:
11713 case UNBOUND_CLASS_TEMPLATE
:
11715 case DECLTYPE_TYPE
:
11717 return tsubst (t
, args
, complain
, in_decl
);
11719 case IDENTIFIER_NODE
:
11720 if (IDENTIFIER_TYPENAME_P (t
))
11722 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11723 return mangle_conv_op_name_for_type (new_type
);
11729 /* This is handled by tsubst_copy_and_build. */
11730 gcc_unreachable ();
11733 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
11735 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
11737 case CLEANUP_POINT_EXPR
:
11738 /* We shouldn't have built any of these during initial template
11739 generation. Instead, they should be built during instantiation
11740 in response to the saved STMT_IS_FULL_EXPR_P setting. */
11741 gcc_unreachable ();
11745 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
11746 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11747 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
11748 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
11749 mark_used (TREE_OPERAND (r
, 1));
11752 case EXPR_PACK_EXPANSION
:
11753 error ("invalid use of pack expansion expression");
11754 return error_mark_node
;
11756 case NONTYPE_ARGUMENT_PACK
:
11757 error ("use %<...%> to expand argument pack");
11758 return error_mark_node
;
11765 /* Instantiate any typedefs in the type. */
11766 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11767 r
= fold_convert (type
, t
);
11768 gcc_assert (TREE_CODE (r
) == code
);
11773 /* These can sometimes show up in a partial instantiation, but never
11774 involve template parms. */
11775 gcc_assert (!uses_template_parms (t
));
11779 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
11780 gcc_checking_assert (false);
11785 /* Like tsubst_copy, but specifically for OpenMP clauses. */
11788 tsubst_omp_clauses (tree clauses
, tree args
, tsubst_flags_t complain
,
11791 tree new_clauses
= NULL
, nc
, oc
;
11793 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
11795 nc
= copy_node (oc
);
11796 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
11799 switch (OMP_CLAUSE_CODE (nc
))
11801 case OMP_CLAUSE_LASTPRIVATE
:
11802 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
11804 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
11805 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
11806 in_decl
, /*integral_constant_expression_p=*/false);
11807 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
11808 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
11811 case OMP_CLAUSE_PRIVATE
:
11812 case OMP_CLAUSE_SHARED
:
11813 case OMP_CLAUSE_FIRSTPRIVATE
:
11814 case OMP_CLAUSE_REDUCTION
:
11815 case OMP_CLAUSE_COPYIN
:
11816 case OMP_CLAUSE_COPYPRIVATE
:
11817 case OMP_CLAUSE_IF
:
11818 case OMP_CLAUSE_NUM_THREADS
:
11819 case OMP_CLAUSE_SCHEDULE
:
11820 case OMP_CLAUSE_COLLAPSE
:
11821 OMP_CLAUSE_OPERAND (nc
, 0)
11822 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
11823 in_decl
, /*integral_constant_expression_p=*/false);
11825 case OMP_CLAUSE_NOWAIT
:
11826 case OMP_CLAUSE_ORDERED
:
11827 case OMP_CLAUSE_DEFAULT
:
11828 case OMP_CLAUSE_UNTIED
:
11831 gcc_unreachable ();
11835 return finish_omp_clauses (nreverse (new_clauses
));
11838 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
11841 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
11844 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
11846 tree purpose
, value
, chain
;
11851 if (TREE_CODE (t
) != TREE_LIST
)
11852 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
11853 /*function_p=*/false,
11854 /*integral_constant_expression_p=*/false);
11856 if (t
== void_list_node
)
11859 purpose
= TREE_PURPOSE (t
);
11861 purpose
= RECUR (purpose
);
11862 value
= TREE_VALUE (t
);
11863 if (value
&& TREE_CODE (value
) != LABEL_DECL
)
11864 value
= RECUR (value
);
11865 chain
= TREE_CHAIN (t
);
11866 if (chain
&& chain
!= void_type_node
)
11867 chain
= RECUR (chain
);
11868 return tree_cons (purpose
, value
, chain
);
11872 /* Substitute one OMP_FOR iterator. */
11875 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
11876 tree condv
, tree incrv
, tree
*clauses
,
11877 tree args
, tsubst_flags_t complain
, tree in_decl
,
11878 bool integral_constant_expression_p
)
11880 #define RECUR(NODE) \
11881 tsubst_expr ((NODE), args, complain, in_decl, \
11882 integral_constant_expression_p)
11883 tree decl
, init
, cond
, incr
, auto_node
;
11885 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
11886 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
11887 decl
= RECUR (TREE_OPERAND (init
, 0));
11888 init
= TREE_OPERAND (init
, 1);
11889 auto_node
= type_uses_auto (TREE_TYPE (decl
));
11890 if (auto_node
&& init
)
11892 tree init_expr
= init
;
11893 if (TREE_CODE (init_expr
) == DECL_EXPR
)
11894 init_expr
= DECL_INITIAL (DECL_EXPR_DECL (init_expr
));
11895 init_expr
= RECUR (init_expr
);
11897 = do_auto_deduction (TREE_TYPE (decl
), init_expr
, auto_node
);
11899 gcc_assert (!type_dependent_expression_p (decl
));
11901 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
11903 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
11904 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
11905 if (TREE_CODE (incr
) == MODIFY_EXPR
)
11906 incr
= build_x_modify_expr (RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
11907 RECUR (TREE_OPERAND (incr
, 1)),
11910 incr
= RECUR (incr
);
11911 TREE_VEC_ELT (declv
, i
) = decl
;
11912 TREE_VEC_ELT (initv
, i
) = init
;
11913 TREE_VEC_ELT (condv
, i
) = cond
;
11914 TREE_VEC_ELT (incrv
, i
) = incr
;
11918 if (init
&& TREE_CODE (init
) != DECL_EXPR
)
11921 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
11923 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
11924 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
11925 && OMP_CLAUSE_DECL (c
) == decl
)
11927 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
11928 && OMP_CLAUSE_DECL (c
) == decl
)
11929 error ("iteration variable %qD should not be firstprivate", decl
);
11930 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
11931 && OMP_CLAUSE_DECL (c
) == decl
)
11932 error ("iteration variable %qD should not be reduction", decl
);
11936 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
11937 OMP_CLAUSE_DECL (c
) = decl
;
11938 c
= finish_omp_clauses (c
);
11941 OMP_CLAUSE_CHAIN (c
) = *clauses
;
11946 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
11947 if (COMPARISON_CLASS_P (cond
))
11948 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
11949 RECUR (TREE_OPERAND (cond
, 0)),
11950 RECUR (TREE_OPERAND (cond
, 1)));
11952 cond
= RECUR (cond
);
11953 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
11954 switch (TREE_CODE (incr
))
11956 case PREINCREMENT_EXPR
:
11957 case PREDECREMENT_EXPR
:
11958 case POSTINCREMENT_EXPR
:
11959 case POSTDECREMENT_EXPR
:
11960 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
11961 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
11964 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
11965 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
11967 tree rhs
= TREE_OPERAND (incr
, 1);
11968 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
11969 RECUR (TREE_OPERAND (incr
, 0)),
11970 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
11971 RECUR (TREE_OPERAND (rhs
, 0)),
11972 RECUR (TREE_OPERAND (rhs
, 1))));
11975 incr
= RECUR (incr
);
11978 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
11979 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
11981 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
11982 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
11983 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
11984 TREE_TYPE (decl
), lhs
,
11985 RECUR (TREE_OPERAND (incr
, 2))));
11987 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
11988 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
11989 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
11991 tree rhs
= TREE_OPERAND (incr
, 2);
11992 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
11993 RECUR (TREE_OPERAND (incr
, 0)),
11994 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
11995 RECUR (TREE_OPERAND (rhs
, 0)),
11996 RECUR (TREE_OPERAND (rhs
, 1))));
11999 incr
= RECUR (incr
);
12002 incr
= RECUR (incr
);
12006 TREE_VEC_ELT (declv
, i
) = decl
;
12007 TREE_VEC_ELT (initv
, i
) = init
;
12008 TREE_VEC_ELT (condv
, i
) = cond
;
12009 TREE_VEC_ELT (incrv
, i
) = incr
;
12013 /* Like tsubst_copy for expressions, etc. but also does semantic
12017 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
12018 bool integral_constant_expression_p
)
12020 #define RECUR(NODE) \
12021 tsubst_expr ((NODE), args, complain, in_decl, \
12022 integral_constant_expression_p)
12026 if (t
== NULL_TREE
|| t
== error_mark_node
)
12029 if (EXPR_HAS_LOCATION (t
))
12030 input_location
= EXPR_LOCATION (t
);
12031 if (STATEMENT_CODE_P (TREE_CODE (t
)))
12032 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
12034 switch (TREE_CODE (t
))
12036 case STATEMENT_LIST
:
12038 tree_stmt_iterator i
;
12039 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
12040 RECUR (tsi_stmt (i
));
12044 case CTOR_INITIALIZER
:
12045 finish_mem_initializers (tsubst_initializer_list
12046 (TREE_OPERAND (t
, 0), args
));
12050 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
12054 tmp
= RECUR (EXPR_STMT_EXPR (t
));
12055 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
12056 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
12058 finish_expr_stmt (tmp
);
12062 do_using_directive (USING_STMT_NAMESPACE (t
));
12067 tree decl
, pattern_decl
;
12070 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
12071 if (TREE_CODE (decl
) == LABEL_DECL
)
12072 finish_label_decl (DECL_NAME (decl
));
12073 else if (TREE_CODE (decl
) == USING_DECL
)
12075 tree scope
= USING_DECL_SCOPE (decl
);
12076 tree name
= DECL_NAME (decl
);
12079 scope
= tsubst (scope
, args
, complain
, in_decl
);
12080 decl
= lookup_qualified_name (scope
, name
,
12081 /*is_type_p=*/false,
12082 /*complain=*/false);
12083 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
12084 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
12086 do_local_using_decl (decl
, scope
, name
);
12090 init
= DECL_INITIAL (decl
);
12091 decl
= tsubst (decl
, args
, complain
, in_decl
);
12092 if (decl
!= error_mark_node
)
12094 /* By marking the declaration as instantiated, we avoid
12095 trying to instantiate it. Since instantiate_decl can't
12096 handle local variables, and since we've already done
12097 all that needs to be done, that's the right thing to
12099 if (TREE_CODE (decl
) == VAR_DECL
)
12100 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
12101 if (TREE_CODE (decl
) == VAR_DECL
12102 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
12103 /* Anonymous aggregates are a special case. */
12104 finish_anon_union (decl
);
12107 int const_init
= false;
12108 maybe_push_decl (decl
);
12109 if (TREE_CODE (decl
) == VAR_DECL
12110 && DECL_PRETTY_FUNCTION_P (decl
))
12112 /* For __PRETTY_FUNCTION__ we have to adjust the
12114 const char *const name
12115 = cxx_printable_name (current_function_decl
, 2);
12116 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
12120 tree t
= RECUR (init
);
12124 /* If we had an initializer but it
12125 instantiated to nothing,
12126 value-initialize the object. This will
12127 only occur when the initializer was a
12128 pack expansion where the parameter packs
12129 used in that expansion were of length
12131 init
= build_value_init (TREE_TYPE (decl
),
12133 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
12134 init
= get_target_expr_sfinae (init
, complain
);
12140 if (TREE_CODE (decl
) == VAR_DECL
)
12141 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
12143 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
12148 /* A DECL_EXPR can also be used as an expression, in the condition
12149 clause of an if/for/while construct. */
12154 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12155 RECUR (FOR_INIT_STMT (t
));
12156 finish_for_init_stmt (stmt
);
12157 tmp
= RECUR (FOR_COND (t
));
12158 finish_for_cond (tmp
, stmt
);
12159 tmp
= RECUR (FOR_EXPR (t
));
12160 finish_for_expr (tmp
, stmt
);
12161 RECUR (FOR_BODY (t
));
12162 finish_for_stmt (stmt
);
12165 case RANGE_FOR_STMT
:
12168 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12169 decl
= RANGE_FOR_DECL (t
);
12170 decl
= tsubst (decl
, args
, complain
, in_decl
);
12171 maybe_push_decl (decl
);
12172 expr
= RECUR (RANGE_FOR_EXPR (t
));
12173 stmt
= cp_convert_range_for (stmt
, decl
, expr
);
12174 RECUR (RANGE_FOR_BODY (t
));
12175 finish_for_stmt (stmt
);
12180 stmt
= begin_while_stmt ();
12181 tmp
= RECUR (WHILE_COND (t
));
12182 finish_while_stmt_cond (tmp
, stmt
);
12183 RECUR (WHILE_BODY (t
));
12184 finish_while_stmt (stmt
);
12188 stmt
= begin_do_stmt ();
12189 RECUR (DO_BODY (t
));
12190 finish_do_body (stmt
);
12191 tmp
= RECUR (DO_COND (t
));
12192 finish_do_stmt (tmp
, stmt
);
12196 stmt
= begin_if_stmt ();
12197 tmp
= RECUR (IF_COND (t
));
12198 finish_if_stmt_cond (tmp
, stmt
);
12199 RECUR (THEN_CLAUSE (t
));
12200 finish_then_clause (stmt
);
12202 if (ELSE_CLAUSE (t
))
12204 begin_else_clause (stmt
);
12205 RECUR (ELSE_CLAUSE (t
));
12206 finish_else_clause (stmt
);
12209 finish_if_stmt (stmt
);
12213 if (BIND_EXPR_BODY_BLOCK (t
))
12214 stmt
= begin_function_body ();
12216 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
12217 ? BCS_TRY_BLOCK
: 0);
12219 RECUR (BIND_EXPR_BODY (t
));
12221 if (BIND_EXPR_BODY_BLOCK (t
))
12222 finish_function_body (stmt
);
12224 finish_compound_stmt (stmt
);
12228 finish_break_stmt ();
12231 case CONTINUE_STMT
:
12232 finish_continue_stmt ();
12236 stmt
= begin_switch_stmt ();
12237 tmp
= RECUR (SWITCH_STMT_COND (t
));
12238 finish_switch_cond (tmp
, stmt
);
12239 RECUR (SWITCH_STMT_BODY (t
));
12240 finish_switch_stmt (stmt
);
12243 case CASE_LABEL_EXPR
:
12244 finish_case_label (EXPR_LOCATION (t
),
12245 RECUR (CASE_LOW (t
)),
12246 RECUR (CASE_HIGH (t
)));
12251 tree decl
= LABEL_EXPR_LABEL (t
);
12254 label
= finish_label_stmt (DECL_NAME (decl
));
12255 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
12256 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
12261 tmp
= GOTO_DESTINATION (t
);
12262 if (TREE_CODE (tmp
) != LABEL_DECL
)
12263 /* Computed goto's must be tsubst'd into. On the other hand,
12264 non-computed gotos must not be; the identifier in question
12265 will have no binding. */
12268 tmp
= DECL_NAME (tmp
);
12269 finish_goto_stmt (tmp
);
12273 tmp
= finish_asm_stmt
12274 (ASM_VOLATILE_P (t
),
12275 RECUR (ASM_STRING (t
)),
12276 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
12277 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
12278 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
12279 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
12281 tree asm_expr
= tmp
;
12282 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
12283 asm_expr
= TREE_OPERAND (asm_expr
, 0);
12284 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
12291 stmt
= begin_try_block ();
12292 RECUR (TRY_STMTS (t
));
12293 finish_cleanup_try_block (stmt
);
12294 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
12298 tree compound_stmt
= NULL_TREE
;
12300 if (FN_TRY_BLOCK_P (t
))
12301 stmt
= begin_function_try_block (&compound_stmt
);
12303 stmt
= begin_try_block ();
12305 RECUR (TRY_STMTS (t
));
12307 if (FN_TRY_BLOCK_P (t
))
12308 finish_function_try_block (stmt
);
12310 finish_try_block (stmt
);
12312 RECUR (TRY_HANDLERS (t
));
12313 if (FN_TRY_BLOCK_P (t
))
12314 finish_function_handler_sequence (stmt
, compound_stmt
);
12316 finish_handler_sequence (stmt
);
12322 tree decl
= HANDLER_PARMS (t
);
12326 decl
= tsubst (decl
, args
, complain
, in_decl
);
12327 /* Prevent instantiate_decl from trying to instantiate
12328 this variable. We've already done all that needs to be
12330 if (decl
!= error_mark_node
)
12331 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
12333 stmt
= begin_handler ();
12334 finish_handler_parms (decl
, stmt
);
12335 RECUR (HANDLER_BODY (t
));
12336 finish_handler (stmt
);
12341 tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
12344 case STATIC_ASSERT
:
12347 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
12350 /*integral_constant_expression_p=*/true);
12351 finish_static_assert (condition
,
12352 STATIC_ASSERT_MESSAGE (t
),
12353 STATIC_ASSERT_SOURCE_LOCATION (t
),
12354 /*member_p=*/false);
12359 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
),
12360 args
, complain
, in_decl
);
12361 stmt
= begin_omp_parallel ();
12362 RECUR (OMP_PARALLEL_BODY (t
));
12363 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
12364 = OMP_PARALLEL_COMBINED (t
);
12368 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
),
12369 args
, complain
, in_decl
);
12370 stmt
= begin_omp_task ();
12371 RECUR (OMP_TASK_BODY (t
));
12372 finish_omp_task (tmp
, stmt
);
12377 tree clauses
, body
, pre_body
;
12378 tree declv
, initv
, condv
, incrv
;
12381 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
),
12382 args
, complain
, in_decl
);
12383 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
12384 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
12385 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
12386 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
12388 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
12389 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
12390 &clauses
, args
, complain
, in_decl
,
12391 integral_constant_expression_p
);
12393 stmt
= begin_omp_structured_block ();
12395 for (i
= 0; i
< TREE_VEC_LENGTH (initv
); i
++)
12396 if (TREE_VEC_ELT (initv
, i
) == NULL
12397 || TREE_CODE (TREE_VEC_ELT (initv
, i
)) != DECL_EXPR
)
12398 TREE_VEC_ELT (initv
, i
) = RECUR (TREE_VEC_ELT (initv
, i
));
12399 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv
, i
))))
12401 tree init
= RECUR (TREE_VEC_ELT (initv
, i
));
12402 gcc_assert (init
== TREE_VEC_ELT (declv
, i
));
12403 TREE_VEC_ELT (initv
, i
) = NULL_TREE
;
12407 tree decl_expr
= TREE_VEC_ELT (initv
, i
);
12408 tree init
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
12409 gcc_assert (init
!= NULL
);
12410 TREE_VEC_ELT (initv
, i
) = RECUR (init
);
12411 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL
;
12413 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init
;
12416 pre_body
= push_stmt_list ();
12417 RECUR (OMP_FOR_PRE_BODY (t
));
12418 pre_body
= pop_stmt_list (pre_body
);
12420 body
= push_stmt_list ();
12421 RECUR (OMP_FOR_BODY (t
));
12422 body
= pop_stmt_list (body
);
12424 t
= finish_omp_for (EXPR_LOCATION (t
), declv
, initv
, condv
, incrv
,
12425 body
, pre_body
, clauses
);
12427 add_stmt (finish_omp_structured_block (stmt
));
12433 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), args
, complain
, in_decl
);
12434 stmt
= push_stmt_list ();
12435 RECUR (OMP_BODY (t
));
12436 stmt
= pop_stmt_list (stmt
);
12439 OMP_BODY (t
) = stmt
;
12440 OMP_CLAUSES (t
) = tmp
;
12448 stmt
= push_stmt_list ();
12449 RECUR (OMP_BODY (t
));
12450 stmt
= pop_stmt_list (stmt
);
12453 OMP_BODY (t
) = stmt
;
12458 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
12460 tree op1
= TREE_OPERAND (t
, 1);
12461 tree lhs
= RECUR (TREE_OPERAND (op1
, 0));
12462 tree rhs
= RECUR (TREE_OPERAND (op1
, 1));
12463 finish_omp_atomic (TREE_CODE (op1
), lhs
, rhs
);
12467 case EXPR_PACK_EXPANSION
:
12468 error ("invalid use of pack expansion expression");
12469 return error_mark_node
;
12471 case NONTYPE_ARGUMENT_PACK
:
12472 error ("use %<...%> to expand argument pack");
12473 return error_mark_node
;
12476 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
12478 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12479 /*function_p=*/false,
12480 integral_constant_expression_p
);
12487 /* T is a postfix-expression that is not being used in a function
12488 call. Return the substituted version of T. */
12491 tsubst_non_call_postfix_expression (tree t
, tree args
,
12492 tsubst_flags_t complain
,
12495 if (TREE_CODE (t
) == SCOPE_REF
)
12496 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
12497 /*done=*/false, /*address_p=*/false);
12499 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12500 /*function_p=*/false,
12501 /*integral_constant_expression_p=*/false);
12506 /* Like tsubst but deals with expressions and performs semantic
12507 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
12510 tsubst_copy_and_build (tree t
,
12512 tsubst_flags_t complain
,
12515 bool integral_constant_expression_p
)
12517 #define RECUR(NODE) \
12518 tsubst_copy_and_build (NODE, args, complain, in_decl, \
12519 /*function_p=*/false, \
12520 integral_constant_expression_p)
12524 if (t
== NULL_TREE
|| t
== error_mark_node
)
12527 switch (TREE_CODE (t
))
12531 /* Fall through. */
12532 case IDENTIFIER_NODE
:
12536 bool non_integral_constant_expression_p
;
12537 const char *error_msg
;
12539 if (IDENTIFIER_TYPENAME_P (t
))
12541 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12542 t
= mangle_conv_op_name_for_type (new_type
);
12545 /* Look up the name. */
12546 decl
= lookup_name (t
);
12548 /* By convention, expressions use ERROR_MARK_NODE to indicate
12549 failure, not NULL_TREE. */
12550 if (decl
== NULL_TREE
)
12551 decl
= error_mark_node
;
12553 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
12555 integral_constant_expression_p
,
12556 /*allow_non_integral_constant_expression_p=*/false,
12557 &non_integral_constant_expression_p
,
12558 /*template_p=*/false,
12560 /*address_p=*/false,
12561 /*template_arg_p=*/false,
12566 if (!function_p
&& TREE_CODE (decl
) == IDENTIFIER_NODE
)
12567 decl
= unqualified_name_lookup_error (decl
);
12571 case TEMPLATE_ID_EXPR
:
12574 tree templ
= RECUR (TREE_OPERAND (t
, 0));
12575 tree targs
= TREE_OPERAND (t
, 1);
12578 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12580 if (TREE_CODE (templ
) == COMPONENT_REF
)
12582 object
= TREE_OPERAND (templ
, 0);
12583 templ
= TREE_OPERAND (templ
, 1);
12586 object
= NULL_TREE
;
12587 templ
= lookup_template_function (templ
, targs
);
12590 return build3 (COMPONENT_REF
, TREE_TYPE (templ
),
12591 object
, templ
, NULL_TREE
);
12593 return baselink_for_fns (templ
);
12598 tree r
= RECUR (TREE_OPERAND (t
, 0));
12600 if (REFERENCE_REF_P (t
))
12602 /* A type conversion to reference type will be enclosed in
12603 such an indirect ref, but the substitution of the cast
12604 will have also added such an indirect ref. */
12605 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
12606 r
= convert_from_reference (r
);
12609 r
= build_x_indirect_ref (r
, RO_UNARY_STAR
, complain
);
12615 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12616 RECUR (TREE_OPERAND (t
, 0)));
12619 case REINTERPRET_CAST_EXPR
:
12620 case CONST_CAST_EXPR
:
12621 case DYNAMIC_CAST_EXPR
:
12622 case STATIC_CAST_EXPR
:
12627 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12628 if (integral_constant_expression_p
12629 && !cast_valid_in_integral_constant_expression_p (type
))
12631 if (complain
& tf_error
)
12632 error ("a cast to a type other than an integral or "
12633 "enumeration type cannot appear in a constant-expression");
12634 return error_mark_node
;
12637 op
= RECUR (TREE_OPERAND (t
, 0));
12639 switch (TREE_CODE (t
))
12642 return build_functional_cast (type
, op
, complain
);
12643 case REINTERPRET_CAST_EXPR
:
12644 return build_reinterpret_cast (type
, op
, complain
);
12645 case CONST_CAST_EXPR
:
12646 return build_const_cast (type
, op
, complain
);
12647 case DYNAMIC_CAST_EXPR
:
12648 return build_dynamic_cast (type
, op
, complain
);
12649 case STATIC_CAST_EXPR
:
12650 return build_static_cast (type
, op
, complain
);
12652 gcc_unreachable ();
12656 case POSTDECREMENT_EXPR
:
12657 case POSTINCREMENT_EXPR
:
12658 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
12659 args
, complain
, in_decl
);
12660 return build_x_unary_op (TREE_CODE (t
), op1
, complain
);
12662 case PREDECREMENT_EXPR
:
12663 case PREINCREMENT_EXPR
:
12667 case TRUTH_NOT_EXPR
:
12668 case UNARY_PLUS_EXPR
: /* Unary + */
12669 case REALPART_EXPR
:
12670 case IMAGPART_EXPR
:
12671 return build_x_unary_op (TREE_CODE (t
), RECUR (TREE_OPERAND (t
, 0)),
12675 op1
= TREE_OPERAND (t
, 0);
12676 if (TREE_CODE (op1
) == LABEL_DECL
)
12677 return finish_label_address_expr (DECL_NAME (op1
),
12678 EXPR_LOCATION (op1
));
12679 if (TREE_CODE (op1
) == SCOPE_REF
)
12680 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
12681 /*done=*/true, /*address_p=*/true);
12683 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
12685 return build_x_unary_op (ADDR_EXPR
, op1
, complain
);
12690 case TRUNC_DIV_EXPR
:
12691 case CEIL_DIV_EXPR
:
12692 case FLOOR_DIV_EXPR
:
12693 case ROUND_DIV_EXPR
:
12694 case EXACT_DIV_EXPR
:
12698 case TRUNC_MOD_EXPR
:
12699 case FLOOR_MOD_EXPR
:
12700 case TRUTH_ANDIF_EXPR
:
12701 case TRUTH_ORIF_EXPR
:
12702 case TRUTH_AND_EXPR
:
12703 case TRUTH_OR_EXPR
:
12718 return build_x_binary_op
12720 RECUR (TREE_OPERAND (t
, 0)),
12721 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
12723 : TREE_CODE (TREE_OPERAND (t
, 0))),
12724 RECUR (TREE_OPERAND (t
, 1)),
12725 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
12727 : TREE_CODE (TREE_OPERAND (t
, 1))),
12728 /*overloaded_p=*/NULL
,
12732 return tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
12733 /*address_p=*/false);
12735 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
12736 args
, complain
, in_decl
);
12737 return build_x_array_ref (op1
, RECUR (TREE_OPERAND (t
, 1)), complain
);
12740 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
12741 return tsubst_copy (t
, args
, complain
, in_decl
);
12745 op1
= TREE_OPERAND (t
, 0);
12748 /* When there are no ARGS, we are trying to evaluate a
12749 non-dependent expression from the parser. Trying to do
12750 the substitutions may not work. */
12752 op1
= TREE_TYPE (op1
);
12756 ++cp_unevaluated_operand
;
12757 ++c_inhibit_evaluation_warnings
;
12758 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
12759 /*function_p=*/false,
12760 /*integral_constant_expression_p=*/false);
12761 --cp_unevaluated_operand
;
12762 --c_inhibit_evaluation_warnings
;
12765 return cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
12766 complain
& tf_error
);
12768 return cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
12769 complain
& tf_error
);
12771 case AT_ENCODE_EXPR
:
12773 op1
= TREE_OPERAND (t
, 0);
12774 ++cp_unevaluated_operand
;
12775 ++c_inhibit_evaluation_warnings
;
12776 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
12777 /*function_p=*/false,
12778 /*integral_constant_expression_p=*/false);
12779 --cp_unevaluated_operand
;
12780 --c_inhibit_evaluation_warnings
;
12781 return objc_build_encode_expr (op1
);
12784 case NOEXCEPT_EXPR
:
12785 op1
= TREE_OPERAND (t
, 0);
12786 ++cp_unevaluated_operand
;
12787 ++c_inhibit_evaluation_warnings
;
12788 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
12789 /*function_p=*/false,
12790 /*integral_constant_expression_p=*/false);
12791 --cp_unevaluated_operand
;
12792 --c_inhibit_evaluation_warnings
;
12793 return finish_noexcept_expr (op1
, complain
);
12797 tree r
= build_x_modify_expr
12798 (RECUR (TREE_OPERAND (t
, 0)),
12799 TREE_CODE (TREE_OPERAND (t
, 1)),
12800 RECUR (TREE_OPERAND (t
, 2)),
12802 /* TREE_NO_WARNING must be set if either the expression was
12803 parenthesized or it uses an operator such as >>= rather
12804 than plain assignment. In the former case, it was already
12805 set and must be copied. In the latter case,
12806 build_x_modify_expr sets it and it must not be reset
12808 if (TREE_NO_WARNING (t
))
12809 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12814 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
12815 args
, complain
, in_decl
);
12816 /* Remember that there was a reference to this entity. */
12819 return build_x_arrow (op1
);
12823 tree placement
= RECUR (TREE_OPERAND (t
, 0));
12824 tree init
= RECUR (TREE_OPERAND (t
, 3));
12825 VEC(tree
,gc
) *placement_vec
;
12826 VEC(tree
,gc
) *init_vec
;
12829 if (placement
== NULL_TREE
)
12830 placement_vec
= NULL
;
12833 placement_vec
= make_tree_vector ();
12834 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
12835 VEC_safe_push (tree
, gc
, placement_vec
, TREE_VALUE (placement
));
12838 /* If there was an initializer in the original tree, but it
12839 instantiated to an empty list, then we should pass a
12840 non-NULL empty vector to tell build_new that it was an
12841 empty initializer() rather than no initializer. This can
12842 only happen when the initializer is a pack expansion whose
12843 parameter packs are of length zero. */
12844 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
12848 init_vec
= make_tree_vector ();
12849 if (init
== void_zero_node
)
12850 gcc_assert (init_vec
!= NULL
);
12853 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
12854 VEC_safe_push (tree
, gc
, init_vec
, TREE_VALUE (init
));
12858 ret
= build_new (&placement_vec
,
12859 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12860 RECUR (TREE_OPERAND (t
, 2)),
12862 NEW_EXPR_USE_GLOBAL (t
),
12865 if (placement_vec
!= NULL
)
12866 release_tree_vector (placement_vec
);
12867 if (init_vec
!= NULL
)
12868 release_tree_vector (init_vec
);
12874 return delete_sanity
12875 (RECUR (TREE_OPERAND (t
, 0)),
12876 RECUR (TREE_OPERAND (t
, 1)),
12877 DELETE_EXPR_USE_VEC (t
),
12878 DELETE_EXPR_USE_GLOBAL (t
),
12881 case COMPOUND_EXPR
:
12882 return build_x_compound_expr (RECUR (TREE_OPERAND (t
, 0)),
12883 RECUR (TREE_OPERAND (t
, 1)),
12889 VEC(tree
,gc
) *call_args
;
12890 unsigned int nargs
, i
;
12895 function
= CALL_EXPR_FN (t
);
12896 /* When we parsed the expression, we determined whether or
12897 not Koenig lookup should be performed. */
12898 koenig_p
= KOENIG_LOOKUP_P (t
);
12899 if (TREE_CODE (function
) == SCOPE_REF
)
12901 qualified_p
= true;
12902 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
12904 /*address_p=*/false);
12906 else if (koenig_p
&& TREE_CODE (function
) == IDENTIFIER_NODE
)
12908 /* Do nothing; calling tsubst_copy_and_build on an identifier
12909 would incorrectly perform unqualified lookup again.
12911 Note that we can also have an IDENTIFIER_NODE if the earlier
12912 unqualified lookup found a member function; in that case
12913 koenig_p will be false and we do want to do the lookup
12914 again to find the instantiated member function.
12916 FIXME but doing that causes c++/15272, so we need to stop
12917 using IDENTIFIER_NODE in that situation. */
12918 qualified_p
= false;
12922 if (TREE_CODE (function
) == COMPONENT_REF
)
12924 tree op
= TREE_OPERAND (function
, 1);
12926 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
12927 || (BASELINK_P (op
)
12928 && BASELINK_QUALIFIED_P (op
)));
12931 qualified_p
= false;
12933 function
= tsubst_copy_and_build (function
, args
, complain
,
12936 integral_constant_expression_p
);
12938 if (BASELINK_P (function
))
12939 qualified_p
= true;
12942 nargs
= call_expr_nargs (t
);
12943 call_args
= make_tree_vector ();
12944 for (i
= 0; i
< nargs
; ++i
)
12946 tree arg
= CALL_EXPR_ARG (t
, i
);
12948 if (!PACK_EXPANSION_P (arg
))
12949 VEC_safe_push (tree
, gc
, call_args
,
12950 RECUR (CALL_EXPR_ARG (t
, i
)));
12953 /* Expand the pack expansion and push each entry onto
12955 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
12956 if (TREE_CODE (arg
) == TREE_VEC
)
12958 unsigned int len
, j
;
12960 len
= TREE_VEC_LENGTH (arg
);
12961 for (j
= 0; j
< len
; ++j
)
12963 tree value
= TREE_VEC_ELT (arg
, j
);
12964 if (value
!= NULL_TREE
)
12965 value
= convert_from_reference (value
);
12966 VEC_safe_push (tree
, gc
, call_args
, value
);
12971 /* A partial substitution. Add one entry. */
12972 VEC_safe_push (tree
, gc
, call_args
, arg
);
12977 /* We do not perform argument-dependent lookup if normal
12978 lookup finds a non-function, in accordance with the
12979 expected resolution of DR 218. */
12981 && ((is_overloaded_fn (function
)
12982 /* If lookup found a member function, the Koenig lookup is
12983 not appropriate, even if an unqualified-name was used
12984 to denote the function. */
12985 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
12986 || TREE_CODE (function
) == IDENTIFIER_NODE
)
12987 /* Only do this when substitution turns a dependent call
12988 into a non-dependent call. */
12989 && type_dependent_expression_p_push (t
)
12990 && !any_type_dependent_arguments_p (call_args
))
12991 function
= perform_koenig_lookup (function
, call_args
, false,
12994 if (TREE_CODE (function
) == IDENTIFIER_NODE
12995 && !any_type_dependent_arguments_p (call_args
))
12997 if (koenig_p
&& (complain
& tf_warning_or_error
))
12999 /* For backwards compatibility and good diagnostics, try
13000 the unqualified lookup again if we aren't in SFINAE
13002 tree unq
= (tsubst_copy_and_build
13003 (function
, args
, complain
, in_decl
, true,
13004 integral_constant_expression_p
));
13005 if (unq
!= function
)
13008 if (TREE_CODE (fn
) == COMPONENT_REF
)
13009 fn
= TREE_OPERAND (fn
, 1);
13010 if (is_overloaded_fn (fn
))
13011 fn
= get_first_fn (fn
);
13012 permerror (EXPR_LOC_OR_HERE (t
),
13013 "%qD was not declared in this scope, "
13014 "and no declarations were found by "
13015 "argument-dependent lookup at the point "
13016 "of instantiation", function
);
13017 if (DECL_CLASS_SCOPE_P (fn
))
13019 inform (EXPR_LOC_OR_HERE (t
),
13020 "declarations in dependent base %qT are "
13021 "not found by unqualified lookup",
13022 DECL_CLASS_CONTEXT (fn
));
13023 if (current_class_ptr
)
13024 inform (EXPR_LOC_OR_HERE (t
),
13025 "use %<this->%D%> instead", function
);
13027 inform (EXPR_LOC_OR_HERE (t
),
13028 "use %<%T::%D%> instead",
13029 current_class_name
, function
);
13032 inform (0, "%q+D declared here, later in the "
13033 "translation unit", fn
);
13037 if (TREE_CODE (function
) == IDENTIFIER_NODE
)
13039 unqualified_name_lookup_error (function
);
13040 release_tree_vector (call_args
);
13041 return error_mark_node
;
13045 /* Remember that there was a reference to this entity. */
13046 if (DECL_P (function
))
13047 mark_used (function
);
13049 if (TREE_CODE (function
) == OFFSET_REF
)
13050 ret
= build_offset_ref_call_from_tree (function
, &call_args
);
13051 else if (TREE_CODE (function
) == COMPONENT_REF
)
13053 tree instance
= TREE_OPERAND (function
, 0);
13054 tree fn
= TREE_OPERAND (function
, 1);
13056 if (processing_template_decl
13057 && (type_dependent_expression_p (instance
)
13058 || (!BASELINK_P (fn
)
13059 && TREE_CODE (fn
) != FIELD_DECL
)
13060 || type_dependent_expression_p (fn
)
13061 || any_type_dependent_arguments_p (call_args
)))
13062 ret
= build_nt_call_vec (function
, call_args
);
13063 else if (!BASELINK_P (fn
))
13064 ret
= finish_call_expr (function
, &call_args
,
13065 /*disallow_virtual=*/false,
13066 /*koenig_p=*/false,
13069 ret
= (build_new_method_call
13071 &call_args
, NULL_TREE
,
13072 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
13077 ret
= finish_call_expr (function
, &call_args
,
13078 /*disallow_virtual=*/qualified_p
,
13082 release_tree_vector (call_args
);
13088 return build_x_conditional_expr
13089 (RECUR (TREE_OPERAND (t
, 0)),
13090 RECUR (TREE_OPERAND (t
, 1)),
13091 RECUR (TREE_OPERAND (t
, 2)),
13094 case PSEUDO_DTOR_EXPR
:
13095 return finish_pseudo_destructor_expr
13096 (RECUR (TREE_OPERAND (t
, 0)),
13097 RECUR (TREE_OPERAND (t
, 1)),
13098 tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
13102 tree purpose
, value
, chain
;
13104 if (t
== void_list_node
)
13107 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
13108 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
13110 /* We have pack expansions, so expand those and
13111 create a new list out of it. */
13112 tree purposevec
= NULL_TREE
;
13113 tree valuevec
= NULL_TREE
;
13117 /* Expand the argument expressions. */
13118 if (TREE_PURPOSE (t
))
13119 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
13120 complain
, in_decl
);
13121 if (TREE_VALUE (t
))
13122 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
13123 complain
, in_decl
);
13125 /* Build the rest of the list. */
13126 chain
= TREE_CHAIN (t
);
13127 if (chain
&& chain
!= void_type_node
)
13128 chain
= RECUR (chain
);
13130 /* Determine the number of arguments. */
13131 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
13133 len
= TREE_VEC_LENGTH (purposevec
);
13134 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
13136 else if (TREE_CODE (valuevec
) == TREE_VEC
)
13137 len
= TREE_VEC_LENGTH (valuevec
);
13140 /* Since we only performed a partial substitution into
13141 the argument pack, we only return a single list
13143 if (purposevec
== TREE_PURPOSE (t
)
13144 && valuevec
== TREE_VALUE (t
)
13145 && chain
== TREE_CHAIN (t
))
13148 return tree_cons (purposevec
, valuevec
, chain
);
13151 /* Convert the argument vectors into a TREE_LIST */
13155 /* Grab the Ith values. */
13157 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
13160 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
13163 /* Build the list (backwards). */
13164 chain
= tree_cons (purpose
, value
, chain
);
13170 purpose
= TREE_PURPOSE (t
);
13172 purpose
= RECUR (purpose
);
13173 value
= TREE_VALUE (t
);
13175 value
= RECUR (value
);
13176 chain
= TREE_CHAIN (t
);
13177 if (chain
&& chain
!= void_type_node
)
13178 chain
= RECUR (chain
);
13179 if (purpose
== TREE_PURPOSE (t
)
13180 && value
== TREE_VALUE (t
)
13181 && chain
== TREE_CHAIN (t
))
13183 return tree_cons (purpose
, value
, chain
);
13186 case COMPONENT_REF
:
13192 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13193 args
, complain
, in_decl
);
13194 /* Remember that there was a reference to this entity. */
13195 if (DECL_P (object
))
13196 mark_used (object
);
13197 object_type
= TREE_TYPE (object
);
13199 member
= TREE_OPERAND (t
, 1);
13200 if (BASELINK_P (member
))
13201 member
= tsubst_baselink (member
,
13202 non_reference (TREE_TYPE (object
)),
13203 args
, complain
, in_decl
);
13205 member
= tsubst_copy (member
, args
, complain
, in_decl
);
13206 if (member
== error_mark_node
)
13207 return error_mark_node
;
13209 if (object_type
&& !CLASS_TYPE_P (object_type
))
13211 if (SCALAR_TYPE_P (object_type
))
13213 tree s
= NULL_TREE
;
13214 tree dtor
= member
;
13216 if (TREE_CODE (dtor
) == SCOPE_REF
)
13218 s
= TREE_OPERAND (dtor
, 0);
13219 dtor
= TREE_OPERAND (dtor
, 1);
13221 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
13223 dtor
= TREE_OPERAND (dtor
, 0);
13225 return finish_pseudo_destructor_expr (object
, s
, dtor
);
13229 else if (TREE_CODE (member
) == SCOPE_REF
13230 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
13235 /* Lookup the template functions now that we know what the
13237 tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
13238 args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
13239 member
= lookup_qualified_name (TREE_OPERAND (member
, 0), tmpl
,
13240 /*is_type_p=*/false,
13241 /*complain=*/false);
13242 if (BASELINK_P (member
))
13244 BASELINK_FUNCTIONS (member
)
13245 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
13247 member
= (adjust_result_of_qualified_name_lookup
13248 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
13253 qualified_name_lookup_error (object_type
, tmpl
, member
,
13255 return error_mark_node
;
13258 else if (TREE_CODE (member
) == SCOPE_REF
13259 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
13260 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
13262 if (complain
& tf_error
)
13264 if (TYPE_P (TREE_OPERAND (member
, 0)))
13265 error ("%qT is not a class or namespace",
13266 TREE_OPERAND (member
, 0));
13268 error ("%qD is not a class or namespace",
13269 TREE_OPERAND (member
, 0));
13271 return error_mark_node
;
13273 else if (TREE_CODE (member
) == FIELD_DECL
)
13274 return finish_non_static_data_member (member
, object
, NULL_TREE
);
13276 return finish_class_member_access_expr (object
, member
,
13277 /*template_p=*/false,
13283 (RECUR (TREE_OPERAND (t
, 0)));
13287 VEC(constructor_elt
,gc
) *n
;
13288 constructor_elt
*ce
;
13289 unsigned HOST_WIDE_INT idx
;
13290 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13291 bool process_index_p
;
13293 bool need_copy_p
= false;
13296 if (type
== error_mark_node
)
13297 return error_mark_node
;
13299 /* digest_init will do the wrong thing if we let it. */
13300 if (type
&& TYPE_PTRMEMFUNC_P (type
))
13303 /* We do not want to process the index of aggregate
13304 initializers as they are identifier nodes which will be
13305 looked up by digest_init. */
13306 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
13308 n
= VEC_copy (constructor_elt
, gc
, CONSTRUCTOR_ELTS (t
));
13309 newlen
= VEC_length (constructor_elt
, n
);
13310 FOR_EACH_VEC_ELT (constructor_elt
, n
, idx
, ce
)
13312 if (ce
->index
&& process_index_p
)
13313 ce
->index
= RECUR (ce
->index
);
13315 if (PACK_EXPANSION_P (ce
->value
))
13317 /* Substitute into the pack expansion. */
13318 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
13321 if (ce
->value
== error_mark_node
13322 || PACK_EXPANSION_P (ce
->value
))
13324 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
13325 /* Just move the argument into place. */
13326 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
13329 /* Update the length of the final CONSTRUCTOR
13330 arguments vector, and note that we will need to
13332 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
13333 need_copy_p
= true;
13337 ce
->value
= RECUR (ce
->value
);
13342 VEC(constructor_elt
,gc
) *old_n
= n
;
13344 n
= VEC_alloc (constructor_elt
, gc
, newlen
);
13345 FOR_EACH_VEC_ELT (constructor_elt
, old_n
, idx
, ce
)
13347 if (TREE_CODE (ce
->value
) == TREE_VEC
)
13349 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
13350 for (i
= 0; i
< len
; ++i
)
13351 CONSTRUCTOR_APPEND_ELT (n
, 0,
13352 TREE_VEC_ELT (ce
->value
, i
));
13355 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
13359 r
= build_constructor (init_list_type_node
, n
);
13360 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
13362 if (TREE_HAS_CONSTRUCTOR (t
))
13363 return finish_compound_literal (type
, r
, complain
);
13365 TREE_TYPE (r
) = type
;
13371 tree operand_0
= TREE_OPERAND (t
, 0);
13372 if (TYPE_P (operand_0
))
13374 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
13375 return get_typeid (operand_0
);
13379 operand_0
= RECUR (operand_0
);
13380 return build_typeid (operand_0
);
13391 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
13393 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
13394 /* If the original type was a reference, we'll be wrapped in
13395 the appropriate INDIRECT_REF. */
13396 r
= convert_from_reference (r
);
13401 return build_x_va_arg (RECUR (TREE_OPERAND (t
, 0)),
13402 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
13404 case OFFSETOF_EXPR
:
13405 return finish_offsetof (RECUR (TREE_OPERAND (t
, 0)));
13409 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
13410 complain
, in_decl
);
13412 tree type2
= TRAIT_EXPR_TYPE2 (t
);
13414 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
13416 return finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
);
13421 tree old_stmt_expr
= cur_stmt_expr
;
13422 tree stmt_expr
= begin_stmt_expr ();
13424 cur_stmt_expr
= stmt_expr
;
13425 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
13426 integral_constant_expression_p
);
13427 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
13428 cur_stmt_expr
= old_stmt_expr
;
13430 /* If the resulting list of expression statement is empty,
13431 fold it further into void_zero_node. */
13432 if (empty_expr_stmt_p (stmt_expr
))
13433 stmt_expr
= void_zero_node
;
13439 t
= tsubst_copy (t
, args
, complain
, in_decl
);
13440 /* As in finish_id_expression, we resolve enumeration constants
13441 to their underlying values. */
13442 if (TREE_CODE (t
) == CONST_DECL
)
13444 used_types_insert (TREE_TYPE (t
));
13445 return DECL_INITIAL (t
);
13451 tree r
= build_lambda_expr ();
13453 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
13454 TREE_TYPE (r
) = type
;
13455 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
13457 LAMBDA_EXPR_LOCATION (r
)
13458 = LAMBDA_EXPR_LOCATION (t
);
13459 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
13460 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
13461 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
13462 LAMBDA_EXPR_DISCRIMINATOR (r
)
13463 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
13464 LAMBDA_EXPR_CAPTURE_LIST (r
)
13465 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t
));
13466 LAMBDA_EXPR_THIS_CAPTURE (r
)
13467 = RECUR (LAMBDA_EXPR_THIS_CAPTURE (t
));
13468 LAMBDA_EXPR_EXTRA_SCOPE (r
)
13469 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t
));
13471 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
13472 determine_visibility (TYPE_NAME (type
));
13473 /* Now that we know visibility, instantiate the type so we have a
13474 declaration of the op() for later calls to lambda_function. */
13475 complete_type (type
);
13477 type
= tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
13479 apply_lambda_return_type (r
, type
);
13481 return build_lambda_object (r
);
13485 /* We can get here for a constant initializer of non-dependent type.
13486 FIXME stop folding in cp_parser_initializer_clause. */
13487 gcc_assert (TREE_CONSTANT (t
));
13489 tree r
= get_target_expr (RECUR (TARGET_EXPR_INITIAL (t
)));
13490 TREE_CONSTANT (r
) = true;
13495 /* Handle Objective-C++ constructs, if appropriate. */
13498 = objcp_tsubst_copy_and_build (t
, args
, complain
,
13499 in_decl
, /*function_p=*/false);
13503 return tsubst_copy (t
, args
, complain
, in_decl
);
13509 /* Verify that the instantiated ARGS are valid. For type arguments,
13510 make sure that the type's linkage is ok. For non-type arguments,
13511 make sure they are constants if they are integral or enumerations.
13512 Emit an error under control of COMPLAIN, and return TRUE on error. */
13515 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
13517 if (ARGUMENT_PACK_P (t
))
13519 tree vec
= ARGUMENT_PACK_ARGS (t
);
13520 int len
= TREE_VEC_LENGTH (vec
);
13521 bool result
= false;
13524 for (i
= 0; i
< len
; ++i
)
13525 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
13529 else if (TYPE_P (t
))
13531 /* [basic.link]: A name with no linkage (notably, the name
13532 of a class or enumeration declared in a local scope)
13533 shall not be used to declare an entity with linkage.
13534 This implies that names with no linkage cannot be used as
13537 DR 757 relaxes this restriction for C++0x. */
13538 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
13539 : no_linkage_check (t
, /*relaxed_p=*/false));
13543 /* DR 488 makes use of a type with no linkage cause
13544 type deduction to fail. */
13545 if (complain
& tf_error
)
13547 if (TYPE_ANONYMOUS_P (nt
))
13548 error ("%qT is/uses anonymous type", t
);
13550 error ("template argument for %qD uses local type %qT",
13555 /* In order to avoid all sorts of complications, we do not
13556 allow variably-modified types as template arguments. */
13557 else if (variably_modified_type_p (t
, NULL_TREE
))
13559 if (complain
& tf_error
)
13560 error ("%qT is a variably modified type", t
);
13564 /* A non-type argument of integral or enumerated type must be a
13566 else if (TREE_TYPE (t
)
13567 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
13568 && !TREE_CONSTANT (t
))
13570 if (complain
& tf_error
)
13571 error ("integral expression %qE is not constant", t
);
13578 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
13580 int ix
, len
= DECL_NTPARMS (tmpl
);
13581 bool result
= false;
13583 for (ix
= 0; ix
!= len
; ix
++)
13585 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
13588 if (result
&& (complain
& tf_error
))
13589 error (" trying to instantiate %qD", tmpl
);
13593 DEF_VEC_O (spec_entry
);
13594 DEF_VEC_ALLOC_O (spec_entry
,gc
);
13595 static GTY(()) VEC(spec_entry
,gc
) *current_deduction_vec
;
13596 static GTY((param_is (spec_entry
))) htab_t current_deduction_htab
;
13598 /* In C++0x, it's possible to have a function template whose type depends
13599 on itself recursively. This is most obvious with decltype, but can also
13600 occur with enumeration scope (c++/48969). So we need to catch infinite
13601 recursion and reject the substitution at deduction time.
13603 Use of a VEC here is O(n^2) in the depth of function template argument
13604 deduction substitution, but using a hash table creates a lot of constant
13605 overhead for the typical case of very low depth. So to make the typical
13606 case fast we start out with a VEC and switch to a hash table only if
13607 depth gets to be significant; in one metaprogramming testcase, even at
13608 depth 80 the overhead of the VEC relative to a hash table was only about
13609 0.5% of compile time. */
13612 deduction_tsubst_fntype (tree fn
, tree targs
)
13621 tree fntype
= TREE_TYPE (fn
);
13623 /* We don't need to worry about this in C++98. */
13624 if (cxx_dialect
< cxx0x
)
13625 return tsubst (fntype
, targs
, tf_none
, NULL_TREE
);
13627 /* If we're seeing a lot of recursion, switch over to a hash table. The
13628 constant 40 is fairly arbitrary. */
13629 if (!current_deduction_htab
13630 && VEC_length (spec_entry
, current_deduction_vec
) > 40)
13632 current_deduction_htab
= htab_create_ggc (40*2, hash_specialization
,
13633 eq_specializations
, ggc_free
);
13634 FOR_EACH_VEC_ELT (spec_entry
, current_deduction_vec
, i
, p
)
13636 slot
= (spec_entry
**) htab_find_slot (current_deduction_htab
,
13638 *slot
= ggc_alloc_spec_entry ();
13641 VEC_free (spec_entry
, gc
, current_deduction_vec
);
13644 /* Now check everything in the vector, if any. */
13645 FOR_EACH_VEC_ELT (spec_entry
, current_deduction_vec
, i
, p
)
13646 if (p
->tmpl
== fn
&& comp_template_args (p
->args
, targs
))
13648 p
->spec
= error_mark_node
;
13649 return error_mark_node
;
13654 elt
.spec
= NULL_TREE
;
13656 /* If we've created a hash table, look there. */
13657 if (current_deduction_htab
)
13659 hash
= hash_specialization (&elt
);
13660 slot
= (spec_entry
**)
13661 htab_find_slot_with_hash (current_deduction_htab
, &elt
, hash
, INSERT
);
13664 /* We already have an entry for this. */
13665 (*slot
)->spec
= error_mark_node
;
13666 return error_mark_node
;
13670 /* Create a new entry. */
13671 *slot
= ggc_alloc_spec_entry ();
13677 /* No hash table, so add it to the VEC. */
13679 VEC_safe_push (spec_entry
, gc
, current_deduction_vec
, &elt
);
13682 r
= tsubst (fntype
, targs
, tf_none
, NULL_TREE
);
13684 /* After doing the substitution, make sure we didn't hit it again. Note
13685 that we might have switched to a hash table during tsubst. */
13686 if (current_deduction_htab
)
13689 hash
= hash_specialization (&elt
);
13690 slot
= (spec_entry
**)
13691 htab_find_slot_with_hash (current_deduction_htab
, &elt
, hash
,
13693 if ((*slot
)->spec
== error_mark_node
)
13694 r
= error_mark_node
;
13695 htab_clear_slot (current_deduction_htab
, (void**)slot
);
13699 if (VEC_last (spec_entry
, current_deduction_vec
)->spec
13700 == error_mark_node
)
13701 r
= error_mark_node
;
13702 VEC_pop (spec_entry
, current_deduction_vec
);
13707 /* Instantiate the indicated variable or function template TMPL with
13708 the template arguments in TARG_PTR. */
13711 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
13713 tree targ_ptr
= orig_args
;
13717 HOST_WIDE_INT saved_processing_template_decl
;
13719 if (tmpl
== error_mark_node
)
13720 return error_mark_node
;
13722 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
13724 /* If this function is a clone, handle it specially. */
13725 if (DECL_CLONED_FUNCTION_P (tmpl
))
13730 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
13731 DECL_CLONED_FUNCTION. */
13732 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
13733 targ_ptr
, complain
);
13734 if (spec
== error_mark_node
)
13735 return error_mark_node
;
13737 /* Look for the clone. */
13738 FOR_EACH_CLONE (clone
, spec
)
13739 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
13741 /* We should always have found the clone by now. */
13742 gcc_unreachable ();
13746 /* Check to see if we already have this specialization. */
13747 gen_tmpl
= most_general_template (tmpl
);
13748 if (tmpl
!= gen_tmpl
)
13749 /* The TMPL is a partial instantiation. To get a full set of
13750 arguments we must add the arguments used to perform the
13751 partial instantiation. */
13752 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
13755 /* It would be nice to avoid hashing here and then again in tsubst_decl,
13756 but it doesn't seem to be on the hot path. */
13757 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
13759 gcc_assert (tmpl
== gen_tmpl
13760 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
13762 || fndecl
== NULL_TREE
);
13764 if (spec
!= NULL_TREE
)
13767 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
13769 return error_mark_node
;
13771 /* We are building a FUNCTION_DECL, during which the access of its
13772 parameters and return types have to be checked. However this
13773 FUNCTION_DECL which is the desired context for access checking
13774 is not built yet. We solve this chicken-and-egg problem by
13775 deferring all checks until we have the FUNCTION_DECL. */
13776 push_deferring_access_checks (dk_deferred
);
13778 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
13779 (because, for example, we have encountered a non-dependent
13780 function call in the body of a template function and must now
13781 determine which of several overloaded functions will be called),
13782 within the instantiation itself we are not processing a
13784 saved_processing_template_decl
= processing_template_decl
;
13785 processing_template_decl
= 0;
13786 /* Substitute template parameters to obtain the specialization. */
13787 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
13788 targ_ptr
, complain
, gen_tmpl
);
13789 processing_template_decl
= saved_processing_template_decl
;
13790 if (fndecl
== error_mark_node
)
13791 return error_mark_node
;
13793 /* Now we know the specialization, compute access previously
13795 push_access_scope (fndecl
);
13797 /* Some typedefs referenced from within the template code need to be access
13798 checked at template instantiation time, i.e now. These types were
13799 added to the template at parsing time. Let's get those and perfom
13800 the acces checks then. */
13801 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl
), targ_ptr
);
13802 perform_deferred_access_checks ();
13803 pop_access_scope (fndecl
);
13804 pop_deferring_access_checks ();
13806 /* The DECL_TI_TEMPLATE should always be the immediate parent
13807 template, not the most general template. */
13808 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
13810 /* If we've just instantiated the main entry point for a function,
13811 instantiate all the alternate entry points as well. We do this
13812 by cloning the instantiation of the main entry point, not by
13813 instantiating the template clones. */
13814 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
13815 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
13820 /* Wrapper for instantiate_template_1. */
13823 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
13826 timevar_push (TV_TEMPLATE_INST
);
13827 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
13828 timevar_pop (TV_TEMPLATE_INST
);
13832 /* We're going to do deduction substitution on the type of TMPL, a function
13833 template. In C++11 mode, push into that access scope. In C++03 mode,
13834 disable access checking. */
13837 push_deduction_access_scope (tree tmpl
)
13839 if (cxx_dialect
>= cxx0x
)
13840 push_access_scope (DECL_TEMPLATE_RESULT (tmpl
));
13842 push_deferring_access_checks (dk_no_check
);
13845 /* And pop back out. */
13848 pop_deduction_access_scope (tree tmpl
)
13850 if (cxx_dialect
>= cxx0x
)
13851 pop_access_scope (DECL_TEMPLATE_RESULT (tmpl
));
13853 pop_deferring_access_checks ();
13856 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
13857 NARGS elements of the arguments that are being used when calling
13858 it. TARGS is a vector into which the deduced template arguments
13861 Return zero for success, 2 for an incomplete match that doesn't resolve
13862 all the types, and 1 for complete failure. An error message will be
13863 printed only for an incomplete match.
13865 If FN is a conversion operator, or we are trying to produce a specific
13866 specialization, RETURN_TYPE is the return type desired.
13868 The EXPLICIT_TARGS are explicit template arguments provided via a
13871 The parameter STRICT is one of:
13874 We are deducing arguments for a function call, as in
13875 [temp.deduct.call].
13878 We are deducing arguments for a conversion function, as in
13879 [temp.deduct.conv].
13882 We are deducing arguments when doing an explicit instantiation
13883 as in [temp.explicit], when determining an explicit specialization
13884 as in [temp.expl.spec], or when taking the address of a function
13885 template, as in [temp.deduct.funcaddr]. */
13888 fn_type_unification (tree fn
,
13889 tree explicit_targs
,
13892 unsigned int nargs
,
13894 unification_kind_t strict
,
13900 bool incomplete_argument_packs_p
= false;
13902 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
13904 fntype
= TREE_TYPE (fn
);
13905 if (explicit_targs
)
13909 The specified template arguments must match the template
13910 parameters in kind (i.e., type, nontype, template), and there
13911 must not be more arguments than there are parameters;
13912 otherwise type deduction fails.
13914 Nontype arguments must match the types of the corresponding
13915 nontype template parameters, or must be convertible to the
13916 types of the corresponding nontype parameters as specified in
13917 _temp.arg.nontype_, otherwise type deduction fails.
13919 All references in the function type of the function template
13920 to the corresponding template parameters are replaced by the
13921 specified template argument values. If a substitution in a
13922 template parameter or in the function type of the function
13923 template results in an invalid type, type deduction fails. */
13924 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
13925 int i
, len
= TREE_VEC_LENGTH (tparms
);
13926 tree converted_args
;
13927 bool incomplete
= false;
13929 if (explicit_targs
== error_mark_node
)
13933 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
, tf_none
,
13934 /*require_all_args=*/false,
13935 /*use_default_args=*/false));
13936 if (converted_args
== error_mark_node
)
13939 /* Substitute the explicit args into the function type. This is
13940 necessary so that, for instance, explicitly declared function
13941 arguments can match null pointed constants. If we were given
13942 an incomplete set of explicit args, we must not do semantic
13943 processing during substitution as we could create partial
13945 for (i
= 0; i
< len
; i
++)
13947 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
13948 bool parameter_pack
= false;
13950 /* Dig out the actual parm. */
13951 if (TREE_CODE (parm
) == TYPE_DECL
13952 || TREE_CODE (parm
) == TEMPLATE_DECL
)
13954 parm
= TREE_TYPE (parm
);
13955 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
13957 else if (TREE_CODE (parm
) == PARM_DECL
)
13959 parm
= DECL_INITIAL (parm
);
13960 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
13963 if (parameter_pack
)
13967 template_parm_level_and_index (parm
, &level
, &idx
);
13969 /* Mark the argument pack as "incomplete". We could
13970 still deduce more arguments during unification.
13971 We remove this mark in type_unification_real. */
13972 targ
= TMPL_ARG (converted_args
, level
, idx
);
13975 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
13976 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
13977 = ARGUMENT_PACK_ARGS (targ
);
13980 /* We have some incomplete argument packs. */
13981 incomplete_argument_packs_p
= true;
13985 if (incomplete_argument_packs_p
)
13986 /* Any substitution is guaranteed to be incomplete if there
13987 are incomplete argument packs, because we can still deduce
13991 incomplete
= NUM_TMPL_ARGS (explicit_targs
) != NUM_TMPL_ARGS (targs
);
13993 processing_template_decl
+= incomplete
;
13994 push_deduction_access_scope (fn
);
13995 fntype
= deduction_tsubst_fntype (fn
, converted_args
);
13996 pop_deduction_access_scope (fn
);
13997 processing_template_decl
-= incomplete
;
13999 if (fntype
== error_mark_node
)
14002 /* Place the explicitly specified arguments in TARGS. */
14003 for (i
= NUM_TMPL_ARGS (converted_args
); i
--;)
14004 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (converted_args
, i
);
14007 /* Never do unification on the 'this' parameter. */
14008 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
14014 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
14015 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
14016 new_args
[0] = return_type
;
14017 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
14022 /* We allow incomplete unification without an error message here
14023 because the standard doesn't seem to explicitly prohibit it. Our
14024 callers must be ready to deal with unification failures in any
14026 result
= type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
14027 targs
, parms
, args
, nargs
, /*subr=*/0,
14030 /* Now that we have bindings for all of the template arguments,
14031 ensure that the arguments deduced for the template template
14032 parameters have compatible template parameter lists. We cannot
14033 check this property before we have deduced all template
14034 arguments, because the template parameter types of a template
14035 template parameter might depend on prior template parameters
14036 deduced after the template template parameter. The following
14037 ill-formed example illustrates this issue:
14039 template<typename T, template<T> class C> void f(C<5>, T);
14041 template<int N> struct X {};
14044 f(X<5>(), 5l); // error: template argument deduction fails
14047 The template parameter list of 'C' depends on the template type
14048 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
14049 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
14050 time that we deduce 'C'. */
14052 && !template_template_parm_bindings_ok_p
14053 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
14057 /* All is well so far. Now, check:
14061 When all template arguments have been deduced, all uses of
14062 template parameters in nondeduced contexts are replaced with
14063 the corresponding deduced argument values. If the
14064 substitution results in an invalid type, as described above,
14065 type deduction fails. */
14068 push_deduction_access_scope (fn
);
14069 substed
= deduction_tsubst_fntype (fn
, targs
);
14070 pop_deduction_access_scope (fn
);
14071 if (substed
== error_mark_node
)
14074 /* If we're looking for an exact match, check that what we got
14075 is indeed an exact match. It might not be if some template
14076 parameters are used in non-deduced contexts. */
14077 if (strict
== DEDUCE_EXACT
)
14082 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (substed
));
14084 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
14085 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
14086 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
14094 /* Adjust types before performing type deduction, as described in
14095 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
14096 sections are symmetric. PARM is the type of a function parameter
14097 or the return type of the conversion function. ARG is the type of
14098 the argument passed to the call, or the type of the value
14099 initialized with the result of the conversion function.
14100 ARG_EXPR is the original argument expression, which may be null. */
14103 maybe_adjust_types_for_deduction (unification_kind_t strict
,
14117 /* Swap PARM and ARG throughout the remainder of this
14118 function; the handling is precisely symmetric since PARM
14119 will initialize ARG rather than vice versa. */
14127 /* Core issue #873: Do the DR606 thing (see below) for these cases,
14128 too, but here handle it by stripping the reference from PARM
14129 rather than by adding it to ARG. */
14130 if (TREE_CODE (*parm
) == REFERENCE_TYPE
14131 && TYPE_REF_IS_RVALUE (*parm
)
14132 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
14133 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
14134 && TREE_CODE (*arg
) == REFERENCE_TYPE
14135 && !TYPE_REF_IS_RVALUE (*arg
))
14136 *parm
= TREE_TYPE (*parm
);
14137 /* Nothing else to do in this case. */
14141 gcc_unreachable ();
14144 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
14146 /* [temp.deduct.call]
14148 If P is not a reference type:
14150 --If A is an array type, the pointer type produced by the
14151 array-to-pointer standard conversion (_conv.array_) is
14152 used in place of A for type deduction; otherwise,
14154 --If A is a function type, the pointer type produced by
14155 the function-to-pointer standard conversion
14156 (_conv.func_) is used in place of A for type deduction;
14159 --If A is a cv-qualified type, the top level
14160 cv-qualifiers of A's type are ignored for type
14162 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
14163 *arg
= build_pointer_type (TREE_TYPE (*arg
));
14164 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
14165 *arg
= build_pointer_type (*arg
);
14167 *arg
= TYPE_MAIN_VARIANT (*arg
);
14170 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
14171 of the form T&&, where T is a template parameter, and the argument
14172 is an lvalue, T is deduced as A& */
14173 if (TREE_CODE (*parm
) == REFERENCE_TYPE
14174 && TYPE_REF_IS_RVALUE (*parm
)
14175 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
14176 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
14177 && (arg_expr
? real_lvalue_p (arg_expr
)
14178 /* try_one_overload doesn't provide an arg_expr, but
14179 functions are always lvalues. */
14180 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
14181 *arg
= build_reference_type (*arg
);
14183 /* [temp.deduct.call]
14185 If P is a cv-qualified type, the top level cv-qualifiers
14186 of P's type are ignored for type deduction. If P is a
14187 reference type, the type referred to by P is used for
14189 *parm
= TYPE_MAIN_VARIANT (*parm
);
14190 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
14192 *parm
= TREE_TYPE (*parm
);
14193 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
14196 /* DR 322. For conversion deduction, remove a reference type on parm
14197 too (which has been swapped into ARG). */
14198 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
14199 *arg
= TREE_TYPE (*arg
);
14204 /* Most parms like fn_type_unification.
14206 If SUBR is 1, we're being called recursively (to unify the
14207 arguments of a function or method parameter of a function
14211 type_unification_real (tree tparms
,
14215 unsigned int xnargs
,
14217 unification_kind_t strict
,
14220 tree parm
, arg
, arg_expr
;
14222 int ntparms
= TREE_VEC_LENGTH (tparms
);
14224 int saw_undeduced
= 0;
14227 unsigned int nargs
;
14230 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
14231 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
14232 gcc_assert (ntparms
> 0);
14234 /* Reset the number of non-defaulted template arguments contained
14236 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
14241 sub_strict
= (UNIFY_ALLOW_OUTER_LEVEL
| UNIFY_ALLOW_MORE_CV_QUAL
14242 | UNIFY_ALLOW_DERIVED
);
14246 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
14250 sub_strict
= UNIFY_ALLOW_NONE
;
14254 gcc_unreachable ();
14263 while (parms
&& parms
!= void_list_node
14266 parm
= TREE_VALUE (parms
);
14268 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
14269 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
14270 /* For a function parameter pack that occurs at the end of the
14271 parameter-declaration-list, the type A of each remaining
14272 argument of the call is compared with the type P of the
14273 declarator-id of the function parameter pack. */
14276 parms
= TREE_CHAIN (parms
);
14278 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
14279 /* For a function parameter pack that does not occur at the
14280 end of the parameter-declaration-list, the type of the
14281 parameter pack is a non-deduced context. */
14288 if (arg
== error_mark_node
)
14290 if (arg
== unknown_type_node
)
14291 /* We can't deduce anything from this, but we might get all the
14292 template args from other function args. */
14295 /* Conversions will be performed on a function argument that
14296 corresponds with a function parameter that contains only
14297 non-deducible template parameters and explicitly specified
14298 template parameters. */
14299 if (!uses_template_parms (parm
))
14304 type
= TREE_TYPE (arg
);
14308 if (same_type_p (parm
, type
))
14310 if (strict
!= DEDUCE_EXACT
14311 && can_convert_arg (parm
, type
, TYPE_P (arg
) ? NULL_TREE
: arg
,
14320 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
14321 if (type_unknown_p (arg
))
14323 /* [temp.deduct.type]
14325 A template-argument can be deduced from a pointer to
14326 function or pointer to member function argument if
14327 the set of overloaded functions does not contain
14328 function templates and at most one of a set of
14329 overloaded functions provides a unique match. */
14330 if (resolve_overloaded_unification
14331 (tparms
, targs
, parm
, arg
, strict
, sub_strict
))
14337 arg
= unlowered_expr_type (arg
);
14338 if (arg
== error_mark_node
)
14343 int arg_strict
= sub_strict
;
14346 arg_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
,
14349 if (arg
== init_list_type_node
&& arg_expr
)
14351 if (unify (tparms
, targs
, parm
, arg
, arg_strict
))
14358 && parms
!= void_list_node
14359 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
14361 /* Unify the remaining arguments with the pack expansion type. */
14363 tree parmvec
= make_tree_vec (1);
14365 /* Allocate a TREE_VEC and copy in all of the arguments */
14366 argvec
= make_tree_vec (nargs
- ia
);
14367 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
14368 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
14370 /* Copy the parameter into parmvec. */
14371 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
14372 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
14373 /*call_args_p=*/true, /*subr=*/subr
))
14376 /* Advance to the end of the list of parameters. */
14377 parms
= TREE_CHAIN (parms
);
14380 /* Fail if we've reached the end of the parm list, and more args
14381 are present, and the parm list isn't variadic. */
14382 if (ia
< nargs
&& parms
== void_list_node
)
14384 /* Fail if parms are left and they don't have default values. */
14385 if (parms
&& parms
!= void_list_node
14386 && TREE_PURPOSE (parms
) == NULL_TREE
)
14391 /* Check to see if we need another pass before we start clearing
14392 ARGUMENT_PACK_INCOMPLETE_P. */
14393 for (i
= 0; i
< ntparms
; i
++)
14395 tree targ
= TREE_VEC_ELT (targs
, i
);
14396 tree tparm
= TREE_VEC_ELT (tparms
, i
);
14398 if (targ
|| tparm
== error_mark_node
)
14400 tparm
= TREE_VALUE (tparm
);
14402 /* If this is an undeduced nontype parameter that depends on
14403 a type parameter, try another pass; its type may have been
14404 deduced from a later argument than the one from which
14405 this parameter can be deduced. */
14406 if (TREE_CODE (tparm
) == PARM_DECL
14407 && uses_template_parms (TREE_TYPE (tparm
))
14408 && !saw_undeduced
++)
14412 for (i
= 0; i
< ntparms
; i
++)
14414 tree targ
= TREE_VEC_ELT (targs
, i
);
14415 tree tparm
= TREE_VEC_ELT (tparms
, i
);
14417 /* Clear the "incomplete" flags on all argument packs now so that
14418 substituting them into later default arguments works. */
14419 if (targ
&& ARGUMENT_PACK_P (targ
))
14421 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
14422 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
14425 if (targ
|| tparm
== error_mark_node
)
14427 tparm
= TREE_VALUE (tparm
);
14429 /* Core issue #226 (C++0x) [temp.deduct]:
14431 If a template argument has not been deduced, its
14432 default template argument, if any, is used.
14434 When we are in C++98 mode, TREE_PURPOSE will either
14435 be NULL_TREE or ERROR_MARK_NODE, so we do not need
14436 to explicitly check cxx_dialect here. */
14437 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
14439 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
14440 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
14441 arg
= tsubst_template_arg (arg
, targs
, tf_none
, NULL_TREE
);
14442 arg
= convert_template_argument (parm
, arg
, targs
, tf_none
,
14444 if (arg
== error_mark_node
)
14448 TREE_VEC_ELT (targs
, i
) = arg
;
14449 /* The position of the first default template argument,
14450 is also the number of non-defaulted arguments in TARGS.
14452 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
14453 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
14458 /* If the type parameter is a parameter pack, then it will
14459 be deduced to an empty parameter pack. */
14460 if (template_parameter_pack_p (tparm
))
14464 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
14466 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
14467 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
14468 TREE_CONSTANT (arg
) = 1;
14471 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
14473 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
14475 TREE_VEC_ELT (targs
, i
) = arg
;
14482 #ifdef ENABLE_CHECKING
14483 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
14484 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
14490 /* Subroutine of type_unification_real. Args are like the variables
14491 at the call site. ARG is an overloaded function (or template-id);
14492 we try deducing template args from each of the overloads, and if
14493 only one succeeds, we go with that. Modifies TARGS and returns
14494 true on success. */
14497 resolve_overloaded_unification (tree tparms
,
14501 unification_kind_t strict
,
14504 tree tempargs
= copy_node (targs
);
14506 tree goodfn
= NULL_TREE
;
14509 if (TREE_CODE (arg
) == ADDR_EXPR
)
14511 arg
= TREE_OPERAND (arg
, 0);
14517 if (TREE_CODE (arg
) == COMPONENT_REF
)
14518 /* Handle `&x' where `x' is some static or non-static member
14520 arg
= TREE_OPERAND (arg
, 1);
14522 if (TREE_CODE (arg
) == OFFSET_REF
)
14523 arg
= TREE_OPERAND (arg
, 1);
14525 /* Strip baselink information. */
14526 if (BASELINK_P (arg
))
14527 arg
= BASELINK_FUNCTIONS (arg
);
14529 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
14531 /* If we got some explicit template args, we need to plug them into
14532 the affected templates before we try to unify, in case the
14533 explicit args will completely resolve the templates in question. */
14535 tree expl_subargs
= TREE_OPERAND (arg
, 1);
14536 arg
= TREE_OPERAND (arg
, 0);
14538 for (; arg
; arg
= OVL_NEXT (arg
))
14540 tree fn
= OVL_CURRENT (arg
);
14541 tree subargs
, elem
;
14543 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
14546 ++processing_template_decl
;
14547 subargs
= get_bindings (fn
, DECL_TEMPLATE_RESULT (fn
),
14548 expl_subargs
, /*check_ret=*/false);
14551 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
14552 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
14553 elem
, strict
, sub_strict
, addr_p
)
14554 && (!goodfn
|| !decls_match (goodfn
, elem
)))
14560 --processing_template_decl
;
14563 else if (TREE_CODE (arg
) != OVERLOAD
14564 && TREE_CODE (arg
) != FUNCTION_DECL
)
14565 /* If ARG is, for example, "(0, &f)" then its type will be unknown
14566 -- but the deduction does not succeed because the expression is
14567 not just the function on its own. */
14570 for (; arg
; arg
= OVL_NEXT (arg
))
14571 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
14572 TREE_TYPE (OVL_CURRENT (arg
)),
14573 strict
, sub_strict
, addr_p
)
14574 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
14576 goodfn
= OVL_CURRENT (arg
);
14580 /* [temp.deduct.type] A template-argument can be deduced from a pointer
14581 to function or pointer to member function argument if the set of
14582 overloaded functions does not contain function templates and at most
14583 one of a set of overloaded functions provides a unique match.
14585 So if we found multiple possibilities, we return success but don't
14586 deduce anything. */
14590 int i
= TREE_VEC_LENGTH (targs
);
14592 if (TREE_VEC_ELT (tempargs
, i
))
14593 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
14601 /* Core DR 115: In contexts where deduction is done and fails, or in
14602 contexts where deduction is not done, if a template argument list is
14603 specified and it, along with any default template arguments, identifies
14604 a single function template specialization, then the template-id is an
14605 lvalue for the function template specialization. */
14608 resolve_nondeduced_context (tree orig_expr
)
14610 tree expr
, offset
, baselink
;
14613 if (!type_unknown_p (orig_expr
))
14618 offset
= NULL_TREE
;
14619 baselink
= NULL_TREE
;
14621 if (TREE_CODE (expr
) == ADDR_EXPR
)
14623 expr
= TREE_OPERAND (expr
, 0);
14626 if (TREE_CODE (expr
) == OFFSET_REF
)
14629 expr
= TREE_OPERAND (expr
, 1);
14631 if (TREE_CODE (expr
) == BASELINK
)
14634 expr
= BASELINK_FUNCTIONS (expr
);
14637 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
14640 tree goodfn
= NULL_TREE
;
14642 /* If we got some explicit template args, we need to plug them into
14643 the affected templates before we try to unify, in case the
14644 explicit args will completely resolve the templates in question. */
14646 tree expl_subargs
= TREE_OPERAND (expr
, 1);
14647 tree arg
= TREE_OPERAND (expr
, 0);
14648 tree badfn
= NULL_TREE
;
14649 tree badargs
= NULL_TREE
;
14651 for (; arg
; arg
= OVL_NEXT (arg
))
14653 tree fn
= OVL_CURRENT (arg
);
14654 tree subargs
, elem
;
14656 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
14659 ++processing_template_decl
;
14660 subargs
= get_bindings (fn
, DECL_TEMPLATE_RESULT (fn
),
14661 expl_subargs
, /*check_ret=*/false);
14662 if (subargs
&& !any_dependent_template_arguments_p (subargs
))
14664 elem
= instantiate_template (fn
, subargs
, tf_none
);
14665 if (elem
== error_mark_node
)
14670 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
14676 --processing_template_decl
;
14682 expr
= build_baselink (BASELINK_BINFO (baselink
),
14683 BASELINK_ACCESS_BINFO (baselink
),
14684 expr
, BASELINK_OPTYPE (baselink
));
14688 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
14689 expr
= build_offset_ref (base
, expr
, addr
);
14692 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
14695 else if (good
== 0 && badargs
)
14696 /* There were no good options and at least one bad one, so let the
14697 user know what the problem is. */
14698 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
14703 /* Subroutine of resolve_overloaded_unification; does deduction for a single
14704 overload. Fills TARGS with any deduced arguments, or error_mark_node if
14705 different overloads deduce different arguments for a given parm.
14706 ADDR_P is true if the expression for which deduction is being
14707 performed was of the form "& fn" rather than simply "fn".
14709 Returns 1 on success. */
14712 try_one_overload (tree tparms
,
14717 unification_kind_t strict
,
14725 /* [temp.deduct.type] A template-argument can be deduced from a pointer
14726 to function or pointer to member function argument if the set of
14727 overloaded functions does not contain function templates and at most
14728 one of a set of overloaded functions provides a unique match.
14730 So if this is a template, just return success. */
14732 if (uses_template_parms (arg
))
14735 if (TREE_CODE (arg
) == METHOD_TYPE
)
14736 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
14738 arg
= build_pointer_type (arg
);
14740 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
14742 /* We don't copy orig_targs for this because if we have already deduced
14743 some template args from previous args, unify would complain when we
14744 try to deduce a template parameter for the same argument, even though
14745 there isn't really a conflict. */
14746 nargs
= TREE_VEC_LENGTH (targs
);
14747 tempargs
= make_tree_vec (nargs
);
14749 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
) != 0)
14752 /* First make sure we didn't deduce anything that conflicts with
14753 explicitly specified args. */
14754 for (i
= nargs
; i
--; )
14756 tree elt
= TREE_VEC_ELT (tempargs
, i
);
14757 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
14761 else if (uses_template_parms (elt
))
14762 /* Since we're unifying against ourselves, we will fill in
14763 template args used in the function parm list with our own
14764 template parms. Discard them. */
14765 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
14766 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
14770 for (i
= nargs
; i
--; )
14772 tree elt
= TREE_VEC_ELT (tempargs
, i
);
14775 TREE_VEC_ELT (targs
, i
) = elt
;
14781 /* PARM is a template class (perhaps with unbound template
14782 parameters). ARG is a fully instantiated type. If ARG can be
14783 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
14784 TARGS are as for unify. */
14787 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
)
14789 tree copy_of_targs
;
14791 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
14792 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
14793 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
14796 /* We need to make a new template argument vector for the call to
14797 unify. If we used TARGS, we'd clutter it up with the result of
14798 the attempted unification, even if this class didn't work out.
14799 We also don't want to commit ourselves to all the unifications
14800 we've already done, since unification is supposed to be done on
14801 an argument-by-argument basis. In other words, consider the
14802 following pathological case:
14804 template <int I, int J, int K>
14807 template <int I, int J>
14808 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
14810 template <int I, int J, int K>
14811 void f(S<I, J, K>, S<I, I, I>);
14820 Now, by the time we consider the unification involving `s2', we
14821 already know that we must have `f<0, 0, 0>'. But, even though
14822 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
14823 because there are two ways to unify base classes of S<0, 1, 2>
14824 with S<I, I, I>. If we kept the already deduced knowledge, we
14825 would reject the possibility I=1. */
14826 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
14828 /* If unification failed, we're done. */
14829 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
14830 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
))
14836 /* Given a template type PARM and a class type ARG, find the unique
14837 base type in ARG that is an instance of PARM. We do not examine
14838 ARG itself; only its base-classes. If there is not exactly one
14839 appropriate base class, return NULL_TREE. PARM may be the type of
14840 a partial specialization, as well as a plain template type. Used
14844 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
)
14846 tree rval
= NULL_TREE
;
14849 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
14851 binfo
= TYPE_BINFO (complete_type (arg
));
14853 /* The type could not be completed. */
14856 /* Walk in inheritance graph order. The search order is not
14857 important, and this avoids multiple walks of virtual bases. */
14858 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
14860 tree r
= try_class_unification (tparms
, targs
, parm
, BINFO_TYPE (binfo
));
14864 /* If there is more than one satisfactory baseclass, then:
14868 If they yield more than one possible deduced A, the type
14872 if (rval
&& !same_type_p (r
, rval
))
14882 /* Returns the level of DECL, which declares a template parameter. */
14885 template_decl_level (tree decl
)
14887 switch (TREE_CODE (decl
))
14890 case TEMPLATE_DECL
:
14891 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
14894 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
14897 gcc_unreachable ();
14902 /* Decide whether ARG can be unified with PARM, considering only the
14903 cv-qualifiers of each type, given STRICT as documented for unify.
14904 Returns nonzero iff the unification is OK on that basis. */
14907 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
14909 int arg_quals
= cp_type_quals (arg
);
14910 int parm_quals
= cp_type_quals (parm
);
14912 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
14913 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
14915 /* Although a CVR qualifier is ignored when being applied to a
14916 substituted template parameter ([8.3.2]/1 for example), that
14917 does not allow us to unify "const T" with "int&" because both
14918 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
14919 It is ok when we're allowing additional CV qualifiers
14920 at the outer level [14.8.2.1]/3,1st bullet. */
14921 if ((TREE_CODE (arg
) == REFERENCE_TYPE
14922 || TREE_CODE (arg
) == FUNCTION_TYPE
14923 || TREE_CODE (arg
) == METHOD_TYPE
)
14924 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
14927 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
14928 && (parm_quals
& TYPE_QUAL_RESTRICT
))
14932 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
14933 && (arg_quals
& parm_quals
) != parm_quals
)
14936 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
14937 && (parm_quals
& arg_quals
) != arg_quals
)
14943 /* Determines the LEVEL and INDEX for the template parameter PARM. */
14945 template_parm_level_and_index (tree parm
, int* level
, int* index
)
14947 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
14948 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
14949 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
14951 *index
= TEMPLATE_TYPE_IDX (parm
);
14952 *level
= TEMPLATE_TYPE_LEVEL (parm
);
14956 *index
= TEMPLATE_PARM_IDX (parm
);
14957 *level
= TEMPLATE_PARM_LEVEL (parm
);
14961 /* Unifies the remaining arguments in PACKED_ARGS with the pack
14962 expansion at the end of PACKED_PARMS. Returns 0 if the type
14963 deduction succeeds, 1 otherwise. STRICT is the same as in
14964 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
14965 call argument list. We'll need to adjust the arguments to make them
14966 types. SUBR tells us if this is from a recursive call to
14967 type_unification_real. */
14969 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
14970 tree packed_args
, int strict
, bool call_args_p
,
14974 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
14975 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
14976 tree pack
, packs
= NULL_TREE
;
14977 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
14978 int len
= TREE_VEC_LENGTH (packed_args
);
14980 /* Determine the parameter packs we will be deducing from the
14981 pattern, and record their current deductions. */
14982 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
14983 pack
; pack
= TREE_CHAIN (pack
))
14985 tree parm_pack
= TREE_VALUE (pack
);
14988 /* Determine the index and level of this parameter pack. */
14989 template_parm_level_and_index (parm_pack
, &level
, &idx
);
14991 /* Keep track of the parameter packs and their corresponding
14993 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
14994 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
14997 /* Loop through all of the arguments that have not yet been
14998 unified and unify each with the pattern. */
14999 for (i
= start
; i
< len
; i
++)
15001 tree parm
= pattern
;
15003 /* For each parameter pack, clear out the deduced value so that
15004 we can deduce it again. */
15005 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
15008 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
15010 TMPL_ARG (targs
, level
, idx
) = NULL_TREE
;
15013 /* Unify the pattern with the current argument. */
15015 tree arg
= TREE_VEC_ELT (packed_args
, i
);
15016 tree arg_expr
= NULL_TREE
;
15017 int arg_strict
= strict
;
15018 bool skip_arg_p
= false;
15024 /* This mirrors what we do in type_unification_real. */
15028 sub_strict
= (UNIFY_ALLOW_OUTER_LEVEL
15029 | UNIFY_ALLOW_MORE_CV_QUAL
15030 | UNIFY_ALLOW_DERIVED
);
15034 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
15038 sub_strict
= UNIFY_ALLOW_NONE
;
15042 gcc_unreachable ();
15047 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
15048 if (type_unknown_p (arg
))
15050 /* [temp.deduct.type] A template-argument can be
15051 deduced from a pointer to function or pointer
15052 to member function argument if the set of
15053 overloaded functions does not contain function
15054 templates and at most one of a set of
15055 overloaded functions provides a unique
15058 if (resolve_overloaded_unification
15059 (tparms
, targs
, parm
, arg
,
15060 (unification_kind_t
) strict
,
15070 arg
= unlowered_expr_type (arg
);
15071 if (arg
== error_mark_node
)
15076 arg_strict
= sub_strict
;
15080 maybe_adjust_types_for_deduction ((unification_kind_t
) strict
,
15081 &parm
, &arg
, arg_expr
);
15086 /* For deduction from an init-list we need the actual list. */
15087 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
15089 if (unify (tparms
, targs
, parm
, arg
, arg_strict
))
15094 /* For each parameter pack, collect the deduced value. */
15095 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
15098 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
15100 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
15101 TMPL_ARG (targs
, level
, idx
);
15105 /* Verify that the results of unification with the parameter packs
15106 produce results consistent with what we've seen before, and make
15107 the deduced argument packs available. */
15108 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
15110 tree old_pack
= TREE_VALUE (pack
);
15111 tree new_args
= TREE_TYPE (pack
);
15112 int i
, len
= TREE_VEC_LENGTH (new_args
);
15114 bool nondeduced_p
= false;
15116 /* By default keep the original deduced argument pack.
15117 If necessary, more specific code is going to update the
15118 resulting deduced argument later down in this function. */
15119 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
15120 TMPL_ARG (targs
, level
, idx
) = old_pack
;
15122 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
15123 actually deduce anything. */
15124 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
15125 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
15126 nondeduced_p
= true;
15130 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
15132 /* Prepend the explicit arguments onto NEW_ARGS. */
15133 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
15134 tree old_args
= new_args
;
15135 int i
, explicit_len
= TREE_VEC_LENGTH (explicit_args
);
15136 int len
= explicit_len
+ TREE_VEC_LENGTH (old_args
);
15138 /* Copy the explicit arguments. */
15139 new_args
= make_tree_vec (len
);
15140 for (i
= 0; i
< explicit_len
; i
++)
15141 TREE_VEC_ELT (new_args
, i
) = TREE_VEC_ELT (explicit_args
, i
);
15143 /* Copy the deduced arguments. */
15144 for (; i
< len
; i
++)
15145 TREE_VEC_ELT (new_args
, i
) =
15146 TREE_VEC_ELT (old_args
, i
- explicit_len
);
15152 /* Build the deduced *_ARGUMENT_PACK. */
15153 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
15155 result
= make_node (NONTYPE_ARGUMENT_PACK
);
15156 TREE_TYPE (result
) =
15157 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
15158 TREE_CONSTANT (result
) = 1;
15161 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
15163 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
15165 /* Note the deduced argument packs for this parameter
15167 TMPL_ARG (targs
, level
, idx
) = result
;
15169 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
15170 && (ARGUMENT_PACK_ARGS (old_pack
)
15171 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
15173 /* We only had the explicitly-provided arguments before, but
15174 now we have a complete set of arguments. */
15175 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
15177 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
15178 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
15179 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
15181 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack
),
15183 /* Inconsistent unification of this parameter pack. */
15190 /* Deduce the value of template parameters. TPARMS is the (innermost)
15191 set of template parameters to a template. TARGS is the bindings
15192 for those template parameters, as determined thus far; TARGS may
15193 include template arguments for outer levels of template parameters
15194 as well. PARM is a parameter to a template function, or a
15195 subcomponent of that parameter; ARG is the corresponding argument.
15196 This function attempts to match PARM with ARG in a manner
15197 consistent with the existing assignments in TARGS. If more values
15198 are deduced, then TARGS is updated.
15200 Returns 0 if the type deduction succeeds, 1 otherwise. The
15201 parameter STRICT is a bitwise or of the following flags:
15204 Require an exact match between PARM and ARG.
15205 UNIFY_ALLOW_MORE_CV_QUAL:
15206 Allow the deduced ARG to be more cv-qualified (by qualification
15207 conversion) than ARG.
15208 UNIFY_ALLOW_LESS_CV_QUAL:
15209 Allow the deduced ARG to be less cv-qualified than ARG.
15210 UNIFY_ALLOW_DERIVED:
15211 Allow the deduced ARG to be a template base class of ARG,
15212 or a pointer to a template base class of the type pointed to by
15214 UNIFY_ALLOW_INTEGER:
15215 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
15216 case for more information.
15217 UNIFY_ALLOW_OUTER_LEVEL:
15218 This is the outermost level of a deduction. Used to determine validity
15219 of qualification conversions. A valid qualification conversion must
15220 have const qualified pointers leading up to the inner type which
15221 requires additional CV quals, except at the outer level, where const
15222 is not required [conv.qual]. It would be normal to set this flag in
15223 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
15224 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
15225 This is the outermost level of a deduction, and PARM can be more CV
15226 qualified at this point.
15227 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
15228 This is the outermost level of a deduction, and PARM can be less CV
15229 qualified at this point. */
15232 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
)
15237 int strict_in
= strict
;
15239 /* I don't think this will do the right thing with respect to types.
15240 But the only case I've seen it in so far has been array bounds, where
15241 signedness is the only information lost, and I think that will be
15243 while (TREE_CODE (parm
) == NOP_EXPR
)
15244 parm
= TREE_OPERAND (parm
, 0);
15246 if (arg
== error_mark_node
)
15248 if (arg
== unknown_type_node
15249 || arg
== init_list_type_node
)
15250 /* We can't deduce anything from this, but we might get all the
15251 template args from other function args. */
15254 /* If PARM uses template parameters, then we can't bail out here,
15255 even if ARG == PARM, since we won't record unifications for the
15256 template parameters. We might need them if we're trying to
15257 figure out which of two things is more specialized. */
15258 if (arg
== parm
&& !uses_template_parms (parm
))
15261 /* Handle init lists early, so the rest of the function can assume
15262 we're dealing with a type. */
15263 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
15267 tree orig_parm
= parm
;
15269 /* Replace T with std::initializer_list<T> for deduction. */
15270 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
15271 && flag_deduce_init_list
)
15272 parm
= listify (parm
);
15274 if (!is_std_init_list (parm
))
15275 /* We can only deduce from an initializer list argument if the
15276 parameter is std::initializer_list; otherwise this is a
15277 non-deduced context. */
15280 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
15282 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
15284 int elt_strict
= strict
;
15286 if (elt
== error_mark_node
)
15289 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
15291 tree type
= TREE_TYPE (elt
);
15292 /* It should only be possible to get here for a call. */
15293 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
15294 elt_strict
|= maybe_adjust_types_for_deduction
15295 (DEDUCE_CALL
, &elttype
, &type
, elt
);
15299 if (unify (tparms
, targs
, elttype
, elt
, elt_strict
))
15303 /* If the std::initializer_list<T> deduction worked, replace the
15304 deduced A with std::initializer_list<A>. */
15305 if (orig_parm
!= parm
)
15307 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
15308 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
15309 targ
= listify (targ
);
15310 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
15315 /* Immediately reject some pairs that won't unify because of
15316 cv-qualification mismatches. */
15317 if (TREE_CODE (arg
) == TREE_CODE (parm
)
15319 /* It is the elements of the array which hold the cv quals of an array
15320 type, and the elements might be template type parms. We'll check
15321 when we recurse. */
15322 && TREE_CODE (arg
) != ARRAY_TYPE
15323 /* We check the cv-qualifiers when unifying with template type
15324 parameters below. We want to allow ARG `const T' to unify with
15325 PARM `T' for example, when computing which of two templates
15326 is more specialized, for example. */
15327 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
15328 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
15331 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
15332 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
15333 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
15334 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
15335 strict
&= ~UNIFY_ALLOW_DERIVED
;
15336 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
15337 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
15339 switch (TREE_CODE (parm
))
15341 case TYPENAME_TYPE
:
15343 case UNBOUND_CLASS_TEMPLATE
:
15344 /* In a type which contains a nested-name-specifier, template
15345 argument values cannot be deduced for template parameters used
15346 within the nested-name-specifier. */
15349 case TEMPLATE_TYPE_PARM
:
15350 case TEMPLATE_TEMPLATE_PARM
:
15351 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15352 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
15353 if (tparm
== error_mark_node
)
15356 if (TEMPLATE_TYPE_LEVEL (parm
)
15357 != template_decl_level (tparm
))
15358 /* The PARM is not one we're trying to unify. Just check
15359 to see if it matches ARG. */
15360 return (TREE_CODE (arg
) == TREE_CODE (parm
)
15361 && same_type_p (parm
, arg
)) ? 0 : 1;
15362 idx
= TEMPLATE_TYPE_IDX (parm
);
15363 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
15364 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
15366 /* Check for mixed types and values. */
15367 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
15368 && TREE_CODE (tparm
) != TYPE_DECL
)
15369 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
15370 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
15373 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
15375 /* ARG must be constructed from a template class or a template
15376 template parameter. */
15377 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
15378 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
15382 tree parmvec
= TYPE_TI_ARGS (parm
);
15383 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
15384 tree full_argvec
= add_to_template_args (targs
, argvec
);
15386 = DECL_INNERMOST_TEMPLATE_PARMS
15387 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
15389 int parm_variadic_p
= 0;
15391 /* The resolution to DR150 makes clear that default
15392 arguments for an N-argument may not be used to bind T
15393 to a template template parameter with fewer than N
15394 parameters. It is not safe to permit the binding of
15395 default arguments as an extension, as that may change
15396 the meaning of a conforming program. Consider:
15398 struct Dense { static const unsigned int dim = 1; };
15400 template <template <typename> class View,
15402 void operator+(float, View<Block> const&);
15404 template <typename Block,
15405 unsigned int Dim = Block::dim>
15406 struct Lvalue_proxy { operator float() const; };
15410 Lvalue_proxy<Dense> p;
15415 Here, if Lvalue_proxy is permitted to bind to View, then
15416 the global operator+ will be used; if they are not, the
15417 Lvalue_proxy will be converted to float. */
15418 if (coerce_template_parms (parm_parms
,
15420 TYPE_TI_TEMPLATE (parm
),
15422 /*require_all_args=*/true,
15423 /*use_default_args=*/false)
15424 == error_mark_node
)
15427 /* Deduce arguments T, i from TT<T> or TT<i>.
15428 We check each element of PARMVEC and ARGVEC individually
15429 rather than the whole TREE_VEC since they can have
15430 different number of elements. */
15432 parmvec
= expand_template_argument_pack (parmvec
);
15433 argvec
= expand_template_argument_pack (argvec
);
15435 len
= TREE_VEC_LENGTH (parmvec
);
15437 /* Check if the parameters end in a pack, making them
15440 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
15441 parm_variadic_p
= 1;
15443 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
15446 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
15448 if (unify (tparms
, targs
,
15449 TREE_VEC_ELT (parmvec
, i
),
15450 TREE_VEC_ELT (argvec
, i
),
15455 if (parm_variadic_p
15456 && unify_pack_expansion (tparms
, targs
,
15459 /*call_args_p=*/false,
15463 arg
= TYPE_TI_TEMPLATE (arg
);
15465 /* Fall through to deduce template name. */
15468 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
15469 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
15471 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
15473 /* Simple cases: Value already set, does match or doesn't. */
15474 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
15481 /* If PARM is `const T' and ARG is only `int', we don't have
15482 a match unless we are allowing additional qualification.
15483 If ARG is `const int' and PARM is just `T' that's OK;
15484 that binds `const int' to `T'. */
15485 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
15489 /* Consider the case where ARG is `const volatile int' and
15490 PARM is `const T'. Then, T should be `volatile int'. */
15491 arg
= cp_build_qualified_type_real
15492 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
15493 if (arg
== error_mark_node
)
15496 /* Simple cases: Value already set, does match or doesn't. */
15497 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
15502 /* Make sure that ARG is not a variable-sized array. (Note
15503 that were talking about variable-sized arrays (like
15504 `int[n]'), rather than arrays of unknown size (like
15505 `int[]').) We'll get very confused by such a type since
15506 the bound of the array will not be computable in an
15507 instantiation. Besides, such types are not allowed in
15508 ISO C++, so we can do as we please here. */
15509 if (variably_modified_type_p (arg
, NULL_TREE
))
15512 /* Strip typedefs as in convert_template_argument. */
15513 arg
= strip_typedefs (arg
);
15516 /* If ARG is a parameter pack or an expansion, we cannot unify
15517 against it unless PARM is also a parameter pack. */
15518 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
15519 && !template_parameter_pack_p (parm
))
15522 /* If the argument deduction results is a METHOD_TYPE,
15523 then there is a problem.
15524 METHOD_TYPE doesn't map to any real C++ type the result of
15525 the deduction can not be of that type. */
15526 if (TREE_CODE (arg
) == METHOD_TYPE
)
15529 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
15532 case TEMPLATE_PARM_INDEX
:
15533 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
15534 if (tparm
== error_mark_node
)
15537 if (TEMPLATE_PARM_LEVEL (parm
)
15538 != template_decl_level (tparm
))
15539 /* The PARM is not one we're trying to unify. Just check
15540 to see if it matches ARG. */
15541 return !(TREE_CODE (arg
) == TREE_CODE (parm
)
15542 && cp_tree_equal (parm
, arg
));
15544 idx
= TEMPLATE_PARM_IDX (parm
);
15545 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
15548 return !cp_tree_equal (targ
, arg
);
15550 /* [temp.deduct.type] If, in the declaration of a function template
15551 with a non-type template-parameter, the non-type
15552 template-parameter is used in an expression in the function
15553 parameter-list and, if the corresponding template-argument is
15554 deduced, the template-argument type shall match the type of the
15555 template-parameter exactly, except that a template-argument
15556 deduced from an array bound may be of any integral type.
15557 The non-type parameter might use already deduced type parameters. */
15558 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
15559 if (!TREE_TYPE (arg
))
15560 /* Template-parameter dependent expression. Just accept it for now.
15561 It will later be processed in convert_template_argument. */
15563 else if (same_type_p (TREE_TYPE (arg
), tparm
))
15565 else if ((strict
& UNIFY_ALLOW_INTEGER
)
15566 && (TREE_CODE (tparm
) == INTEGER_TYPE
15567 || TREE_CODE (tparm
) == BOOLEAN_TYPE
))
15568 /* Convert the ARG to the type of PARM; the deduced non-type
15569 template argument must exactly match the types of the
15570 corresponding parameter. */
15571 arg
= fold (build_nop (tparm
, arg
));
15572 else if (uses_template_parms (tparm
))
15573 /* We haven't deduced the type of this parameter yet. Try again
15579 /* If ARG is a parameter pack or an expansion, we cannot unify
15580 against it unless PARM is also a parameter pack. */
15581 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
15582 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
15585 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
15590 /* A pointer-to-member constant can be unified only with
15591 another constant. */
15592 if (TREE_CODE (arg
) != PTRMEM_CST
)
15595 /* Just unify the class member. It would be useless (and possibly
15596 wrong, depending on the strict flags) to unify also
15597 PTRMEM_CST_CLASS, because we want to be sure that both parm and
15598 arg refer to the same variable, even if through different
15599 classes. For instance:
15601 struct A { int x; };
15604 Unification of &A::x and &B::x must succeed. */
15605 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
15606 PTRMEM_CST_MEMBER (arg
), strict
);
15611 if (TREE_CODE (arg
) != POINTER_TYPE
)
15614 /* [temp.deduct.call]
15616 A can be another pointer or pointer to member type that can
15617 be converted to the deduced A via a qualification
15618 conversion (_conv.qual_).
15620 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
15621 This will allow for additional cv-qualification of the
15622 pointed-to types if appropriate. */
15624 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
15625 /* The derived-to-base conversion only persists through one
15626 level of pointers. */
15627 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
15629 return unify (tparms
, targs
, TREE_TYPE (parm
),
15630 TREE_TYPE (arg
), strict
);
15633 case REFERENCE_TYPE
:
15634 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
15636 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
15637 strict
& UNIFY_ALLOW_MORE_CV_QUAL
);
15640 if (TREE_CODE (arg
) != ARRAY_TYPE
)
15642 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
15643 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
15645 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
15652 /* Our representation of array types uses "N - 1" as the
15653 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
15654 not an integer constant. We cannot unify arbitrarily
15655 complex expressions, so we eliminate the MINUS_EXPRs
15657 parm_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (parm
));
15658 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
15661 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
15662 parm_max
= TREE_OPERAND (parm_max
, 0);
15664 arg_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (arg
));
15665 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
15668 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
15669 trying to unify the type of a variable with the type
15670 of a template parameter. For example:
15672 template <unsigned int N>
15673 void f (char (&) [N]);
15680 Here, the type of the ARG will be "int [g(i)]", and
15681 may be a SAVE_EXPR, etc. */
15682 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
15684 arg_max
= TREE_OPERAND (arg_max
, 0);
15687 /* If only one of the bounds used a MINUS_EXPR, compensate
15688 by adding one to the other bound. */
15689 if (parm_cst
&& !arg_cst
)
15690 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
15694 else if (arg_cst
&& !parm_cst
)
15695 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
15700 if (unify (tparms
, targs
, parm_max
, arg_max
, UNIFY_ALLOW_INTEGER
))
15703 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
15704 strict
& UNIFY_ALLOW_MORE_CV_QUAL
);
15711 case ENUMERAL_TYPE
:
15713 if (TREE_CODE (arg
) != TREE_CODE (parm
))
15716 /* We have already checked cv-qualification at the top of the
15718 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
15721 /* As far as unification is concerned, this wins. Later checks
15722 will invalidate it if necessary. */
15725 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
15726 /* Type INTEGER_CST can come from ordinary constant template args. */
15728 while (TREE_CODE (arg
) == NOP_EXPR
)
15729 arg
= TREE_OPERAND (arg
, 0);
15731 if (TREE_CODE (arg
) != INTEGER_CST
)
15733 return !tree_int_cst_equal (parm
, arg
);
15738 if (TREE_CODE (arg
) != TREE_VEC
)
15740 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
15742 for (i
= 0; i
< TREE_VEC_LENGTH (parm
); ++i
)
15743 if (unify (tparms
, targs
,
15744 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
15752 if (TREE_CODE (arg
) != TREE_CODE (parm
))
15755 if (TYPE_PTRMEMFUNC_P (parm
))
15757 if (!TYPE_PTRMEMFUNC_P (arg
))
15760 return unify (tparms
, targs
,
15761 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
15762 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
15766 if (CLASSTYPE_TEMPLATE_INFO (parm
))
15768 tree t
= NULL_TREE
;
15770 if (strict_in
& UNIFY_ALLOW_DERIVED
)
15772 /* First, we try to unify the PARM and ARG directly. */
15773 t
= try_class_unification (tparms
, targs
,
15778 /* Fallback to the special case allowed in
15779 [temp.deduct.call]:
15781 If P is a class, and P has the form
15782 template-id, then A can be a derived class of
15783 the deduced A. Likewise, if P is a pointer to
15784 a class of the form template-id, A can be a
15785 pointer to a derived class pointed to by the
15787 t
= get_template_base (tparms
, targs
, parm
, arg
);
15793 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
15794 && (CLASSTYPE_TI_TEMPLATE (parm
)
15795 == CLASSTYPE_TI_TEMPLATE (arg
)))
15796 /* Perhaps PARM is something like S<U> and ARG is S<int>.
15797 Then, we should unify `int' and `U'. */
15800 /* There's no chance of unification succeeding. */
15803 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
15804 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
);
15806 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
15811 case FUNCTION_TYPE
:
15813 unsigned int nargs
;
15818 if (TREE_CODE (arg
) != TREE_CODE (parm
))
15821 /* CV qualifications for methods can never be deduced, they must
15822 match exactly. We need to check them explicitly here,
15823 because type_unification_real treats them as any other
15824 cv-qualified parameter. */
15825 if (TREE_CODE (parm
) == METHOD_TYPE
15826 && (!check_cv_quals_for_unify
15828 class_of_this_parm (arg
),
15829 class_of_this_parm (parm
))))
15832 if (unify (tparms
, targs
, TREE_TYPE (parm
),
15833 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
))
15836 nargs
= list_length (TYPE_ARG_TYPES (arg
));
15837 args
= XALLOCAVEC (tree
, nargs
);
15838 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
15839 a
!= NULL_TREE
&& a
!= void_list_node
;
15840 a
= TREE_CHAIN (a
), ++i
)
15841 args
[i
] = TREE_VALUE (a
);
15844 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
15845 args
, nargs
, 1, DEDUCE_EXACT
,
15850 /* Unify a pointer to member with a pointer to member function, which
15851 deduces the type of the member as a function type. */
15852 if (TYPE_PTRMEMFUNC_P (arg
))
15857 /* Check top-level cv qualifiers */
15858 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
15861 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
15862 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
), UNIFY_ALLOW_NONE
))
15865 /* Determine the type of the function we are unifying against. */
15866 method_type
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg
));
15868 build_function_type (TREE_TYPE (method_type
),
15869 TREE_CHAIN (TYPE_ARG_TYPES (method_type
)));
15871 /* Extract the cv-qualifiers of the member function from the
15872 implicit object parameter and place them on the function
15873 type to be restored later. */
15874 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (method_type
));
15875 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
);
15878 if (TREE_CODE (arg
) != OFFSET_TYPE
)
15880 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
15881 TYPE_OFFSET_BASETYPE (arg
), UNIFY_ALLOW_NONE
))
15883 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
15887 if (DECL_TEMPLATE_PARM_P (parm
))
15888 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
);
15889 if (arg
!= integral_constant_value (parm
))
15894 case TEMPLATE_DECL
:
15895 /* Matched cases are handled by the ARG == PARM test above. */
15899 /* A non-type template parameter that is a variable should be a
15900 an integral constant, in which case, it whould have been
15901 folded into its (constant) value. So we should not be getting
15902 a variable here. */
15903 gcc_unreachable ();
15905 case TYPE_ARGUMENT_PACK
:
15906 case NONTYPE_ARGUMENT_PACK
:
15908 tree packed_parms
= ARGUMENT_PACK_ARGS (parm
);
15909 tree packed_args
= ARGUMENT_PACK_ARGS (arg
);
15910 int i
, len
= TREE_VEC_LENGTH (packed_parms
);
15911 int argslen
= TREE_VEC_LENGTH (packed_args
);
15912 int parm_variadic_p
= 0;
15914 for (i
= 0; i
< len
; ++i
)
15916 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms
, i
)))
15919 /* We can unify against something with a trailing
15921 parm_variadic_p
= 1;
15923 /* Since there is something following the pack
15924 expansion, we cannot unify this template argument
15931 /* If we don't have enough arguments to satisfy the parameters
15932 (not counting the pack expression at the end), or we have
15933 too many arguments for a parameter list that doesn't end in
15934 a pack expression, we can't unify. */
15935 if (argslen
< (len
- parm_variadic_p
)
15936 || (argslen
> len
&& !parm_variadic_p
))
15939 /* Unify all of the parameters that precede the (optional)
15940 pack expression. */
15941 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
15943 if (unify (tparms
, targs
, TREE_VEC_ELT (packed_parms
, i
),
15944 TREE_VEC_ELT (packed_args
, i
), strict
))
15948 if (parm_variadic_p
)
15949 return unify_pack_expansion (tparms
, targs
,
15950 packed_parms
, packed_args
,
15951 strict
, /*call_args_p=*/false,
15959 case DECLTYPE_TYPE
:
15960 case UNDERLYING_TYPE
:
15961 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
15962 or UNDERLYING_TYPE nodes. */
15966 /* Unification fails if we hit an error node. */
15970 /* An unresolved overload is a nondeduced context. */
15971 if (type_unknown_p (parm
))
15973 gcc_assert (EXPR_P (parm
));
15975 /* We must be looking at an expression. This can happen with
15979 void foo(S<I>, S<I + 2>);
15981 This is a "nondeduced context":
15985 The nondeduced contexts are:
15987 --A type that is a template-id in which one or more of
15988 the template-arguments is an expression that references
15989 a template-parameter.
15991 In these cases, we assume deduction succeeded, but don't
15992 actually infer any unifications. */
15994 if (!uses_template_parms (parm
)
15995 && !template_args_equal (parm
, arg
))
16002 /* Note that DECL can be defined in this translation unit, if
16006 mark_definable (tree decl
)
16009 DECL_NOT_REALLY_EXTERN (decl
) = 1;
16010 FOR_EACH_CLONE (clone
, decl
)
16011 DECL_NOT_REALLY_EXTERN (clone
) = 1;
16014 /* Called if RESULT is explicitly instantiated, or is a member of an
16015 explicitly instantiated class. */
16018 mark_decl_instantiated (tree result
, int extern_p
)
16020 SET_DECL_EXPLICIT_INSTANTIATION (result
);
16022 /* If this entity has already been written out, it's too late to
16023 make any modifications. */
16024 if (TREE_ASM_WRITTEN (result
))
16027 if (TREE_CODE (result
) != FUNCTION_DECL
)
16028 /* The TREE_PUBLIC flag for function declarations will have been
16029 set correctly by tsubst. */
16030 TREE_PUBLIC (result
) = 1;
16032 /* This might have been set by an earlier implicit instantiation. */
16033 DECL_COMDAT (result
) = 0;
16036 DECL_NOT_REALLY_EXTERN (result
) = 0;
16039 mark_definable (result
);
16040 /* Always make artificials weak. */
16041 if (DECL_ARTIFICIAL (result
) && flag_weak
)
16042 comdat_linkage (result
);
16043 /* For WIN32 we also want to put explicit instantiations in
16044 linkonce sections. */
16045 else if (TREE_PUBLIC (result
))
16046 maybe_make_one_only (result
);
16049 /* If EXTERN_P, then this function will not be emitted -- unless
16050 followed by an explicit instantiation, at which point its linkage
16051 will be adjusted. If !EXTERN_P, then this function will be
16052 emitted here. In neither circumstance do we want
16053 import_export_decl to adjust the linkage. */
16054 DECL_INTERFACE_KNOWN (result
) = 1;
16057 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
16058 important template arguments. If any are missing, we check whether
16059 they're important by using error_mark_node for substituting into any
16060 args that were used for partial ordering (the ones between ARGS and END)
16061 and seeing if it bubbles up. */
16064 check_undeduced_parms (tree targs
, tree args
, tree end
)
16066 bool found
= false;
16068 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
16069 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
16072 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
16076 for (; args
!= end
; args
= TREE_CHAIN (args
))
16078 tree substed
= tsubst (TREE_VALUE (args
), targs
, tf_none
, NULL_TREE
);
16079 if (substed
== error_mark_node
)
16086 /* Given two function templates PAT1 and PAT2, return:
16088 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
16089 -1 if PAT2 is more specialized than PAT1.
16090 0 if neither is more specialized.
16092 LEN indicates the number of parameters we should consider
16093 (defaulted parameters should not be considered).
16095 The 1998 std underspecified function template partial ordering, and
16096 DR214 addresses the issue. We take pairs of arguments, one from
16097 each of the templates, and deduce them against each other. One of
16098 the templates will be more specialized if all the *other*
16099 template's arguments deduce against its arguments and at least one
16100 of its arguments *does* *not* deduce against the other template's
16101 corresponding argument. Deduction is done as for class templates.
16102 The arguments used in deduction have reference and top level cv
16103 qualifiers removed. Iff both arguments were originally reference
16104 types *and* deduction succeeds in both directions, the template
16105 with the more cv-qualified argument wins for that pairing (if
16106 neither is more cv-qualified, they both are equal). Unlike regular
16107 deduction, after all the arguments have been deduced in this way,
16108 we do *not* verify the deduced template argument values can be
16109 substituted into non-deduced contexts.
16111 The logic can be a bit confusing here, because we look at deduce1 and
16112 targs1 to see if pat2 is at least as specialized, and vice versa; if we
16113 can find template arguments for pat1 to make arg1 look like arg2, that
16114 means that arg2 is at least as specialized as arg1. */
16117 more_specialized_fn (tree pat1
, tree pat2
, int len
)
16119 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
16120 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
16121 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
16122 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
16123 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
16124 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
16125 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
16126 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
16127 tree origs1
, origs2
;
16128 bool lose1
= false;
16129 bool lose2
= false;
16131 /* Remove the this parameter from non-static member functions. If
16132 one is a non-static member function and the other is not a static
16133 member function, remove the first parameter from that function
16134 also. This situation occurs for operator functions where we
16135 locate both a member function (with this pointer) and non-member
16136 operator (with explicit first operand). */
16137 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
16139 len
--; /* LEN is the number of significant arguments for DECL1 */
16140 args1
= TREE_CHAIN (args1
);
16141 if (!DECL_STATIC_FUNCTION_P (decl2
))
16142 args2
= TREE_CHAIN (args2
);
16144 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
16146 args2
= TREE_CHAIN (args2
);
16147 if (!DECL_STATIC_FUNCTION_P (decl1
))
16150 args1
= TREE_CHAIN (args1
);
16154 /* If only one is a conversion operator, they are unordered. */
16155 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
16158 /* Consider the return type for a conversion function */
16159 if (DECL_CONV_FN_P (decl1
))
16161 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
16162 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
16166 processing_template_decl
++;
16172 /* Stop when an ellipsis is seen. */
16173 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
16175 tree arg1
= TREE_VALUE (args1
);
16176 tree arg2
= TREE_VALUE (args2
);
16177 int deduce1
, deduce2
;
16181 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
16182 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
16184 /* When both arguments are pack expansions, we need only
16185 unify the patterns themselves. */
16186 arg1
= PACK_EXPANSION_PATTERN (arg1
);
16187 arg2
= PACK_EXPANSION_PATTERN (arg2
);
16189 /* This is the last comparison we need to do. */
16193 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
16195 arg1
= TREE_TYPE (arg1
);
16196 quals1
= cp_type_quals (arg1
);
16199 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
16201 arg2
= TREE_TYPE (arg2
);
16202 quals2
= cp_type_quals (arg2
);
16205 if ((quals1
< 0) != (quals2
< 0))
16207 /* Only of the args is a reference, see if we should apply
16208 array/function pointer decay to it. This is not part of
16209 DR214, but is, IMHO, consistent with the deduction rules
16210 for the function call itself, and with our earlier
16211 implementation of the underspecified partial ordering
16212 rules. (nathan). */
16215 switch (TREE_CODE (arg1
))
16218 arg1
= TREE_TYPE (arg1
);
16220 case FUNCTION_TYPE
:
16221 arg1
= build_pointer_type (arg1
);
16230 switch (TREE_CODE (arg2
))
16233 arg2
= TREE_TYPE (arg2
);
16235 case FUNCTION_TYPE
:
16236 arg2
= build_pointer_type (arg2
);
16245 arg1
= TYPE_MAIN_VARIANT (arg1
);
16246 arg2
= TYPE_MAIN_VARIANT (arg2
);
16248 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
16250 int i
, len2
= list_length (args2
);
16251 tree parmvec
= make_tree_vec (1);
16252 tree argvec
= make_tree_vec (len2
);
16255 /* Setup the parameter vector, which contains only ARG1. */
16256 TREE_VEC_ELT (parmvec
, 0) = arg1
;
16258 /* Setup the argument vector, which contains the remaining
16260 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
16261 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
16263 deduce1
= !unify_pack_expansion (tparms1
, targs1
, parmvec
,
16264 argvec
, UNIFY_ALLOW_NONE
,
16265 /*call_args_p=*/false,
16268 /* We cannot deduce in the other direction, because ARG1 is
16269 a pack expansion but ARG2 is not. */
16272 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
16274 int i
, len1
= list_length (args1
);
16275 tree parmvec
= make_tree_vec (1);
16276 tree argvec
= make_tree_vec (len1
);
16279 /* Setup the parameter vector, which contains only ARG1. */
16280 TREE_VEC_ELT (parmvec
, 0) = arg2
;
16282 /* Setup the argument vector, which contains the remaining
16284 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
16285 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
16287 deduce2
= !unify_pack_expansion (tparms2
, targs2
, parmvec
,
16288 argvec
, UNIFY_ALLOW_NONE
,
16289 /*call_args_p=*/false,
16292 /* We cannot deduce in the other direction, because ARG2 is
16293 a pack expansion but ARG1 is not.*/
16299 /* The normal case, where neither argument is a pack
16301 deduce1
= !unify (tparms1
, targs1
, arg1
, arg2
, UNIFY_ALLOW_NONE
);
16302 deduce2
= !unify (tparms2
, targs2
, arg2
, arg1
, UNIFY_ALLOW_NONE
);
16305 /* If we couldn't deduce arguments for tparms1 to make arg1 match
16306 arg2, then arg2 is not as specialized as arg1. */
16312 /* "If, for a given type, deduction succeeds in both directions
16313 (i.e., the types are identical after the transformations above)
16314 and if the type from the argument template is more cv-qualified
16315 than the type from the parameter template (as described above)
16316 that type is considered to be more specialized than the other. If
16317 neither type is more cv-qualified than the other then neither type
16318 is more specialized than the other." */
16320 if (deduce1
&& deduce2
16321 && quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
16323 if ((quals1
& quals2
) == quals2
)
16325 if ((quals1
& quals2
) == quals1
)
16329 if (lose1
&& lose2
)
16330 /* We've failed to deduce something in either direction.
16331 These must be unordered. */
16334 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
16335 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
16336 /* We have already processed all of the arguments in our
16337 handing of the pack expansion type. */
16340 args1
= TREE_CHAIN (args1
);
16341 args2
= TREE_CHAIN (args2
);
16344 /* "In most cases, all template parameters must have values in order for
16345 deduction to succeed, but for partial ordering purposes a template
16346 parameter may remain without a value provided it is not used in the
16347 types being used for partial ordering."
16349 Thus, if we are missing any of the targs1 we need to substitute into
16350 origs1, then pat2 is not as specialized as pat1. This can happen when
16351 there is a nondeduced context. */
16352 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
16354 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
16357 processing_template_decl
--;
16359 /* All things being equal, if the next argument is a pack expansion
16360 for one function but not for the other, prefer the
16361 non-variadic function. FIXME this is bogus; see c++/41958. */
16363 && args1
&& TREE_VALUE (args1
)
16364 && args2
&& TREE_VALUE (args2
))
16366 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
16367 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
16370 if (lose1
== lose2
)
16378 /* Determine which of two partial specializations is more specialized.
16380 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
16381 to the first partial specialization. The TREE_VALUE is the
16382 innermost set of template parameters for the partial
16383 specialization. PAT2 is similar, but for the second template.
16385 Return 1 if the first partial specialization is more specialized;
16386 -1 if the second is more specialized; 0 if neither is more
16389 See [temp.class.order] for information about determining which of
16390 two templates is more specialized. */
16393 more_specialized_class (tree pat1
, tree pat2
)
16398 bool any_deductions
= false;
16400 tmpl1
= TREE_TYPE (pat1
);
16401 tmpl2
= TREE_TYPE (pat2
);
16403 /* Just like what happens for functions, if we are ordering between
16404 different class template specializations, we may encounter dependent
16405 types in the arguments, and we need our dependency check functions
16406 to behave correctly. */
16407 ++processing_template_decl
;
16408 targs
= get_class_bindings (TREE_VALUE (pat1
),
16409 CLASSTYPE_TI_ARGS (tmpl1
),
16410 CLASSTYPE_TI_ARGS (tmpl2
));
16414 any_deductions
= true;
16417 targs
= get_class_bindings (TREE_VALUE (pat2
),
16418 CLASSTYPE_TI_ARGS (tmpl2
),
16419 CLASSTYPE_TI_ARGS (tmpl1
));
16423 any_deductions
= true;
16425 --processing_template_decl
;
16427 /* In the case of a tie where at least one of the class templates
16428 has a parameter pack at the end, the template with the most
16429 non-packed parameters wins. */
16432 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
16433 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
16435 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
16436 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
16437 int len1
= TREE_VEC_LENGTH (args1
);
16438 int len2
= TREE_VEC_LENGTH (args2
);
16440 /* We don't count the pack expansion at the end. */
16441 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
16443 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
16448 else if (len1
< len2
)
16455 /* Return the template arguments that will produce the function signature
16456 DECL from the function template FN, with the explicit template
16457 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
16458 also match. Return NULL_TREE if no satisfactory arguments could be
16462 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
16464 int ntparms
= DECL_NTPARMS (fn
);
16465 tree targs
= make_tree_vec (ntparms
);
16467 tree decl_arg_types
;
16469 unsigned int nargs
, ix
;
16472 /* Substitute the explicit template arguments into the type of DECL.
16473 The call to fn_type_unification will handle substitution into the
16475 decl_type
= TREE_TYPE (decl
);
16476 if (explicit_args
&& uses_template_parms (decl_type
))
16479 tree converted_args
;
16481 if (DECL_TEMPLATE_INFO (decl
))
16482 tmpl
= DECL_TI_TEMPLATE (decl
);
16484 /* We can get here for some invalid specializations. */
16488 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
16489 explicit_args
, NULL_TREE
,
16491 /*require_all_args=*/false,
16492 /*use_default_args=*/false);
16493 if (converted_args
== error_mark_node
)
16496 decl_type
= tsubst (decl_type
, converted_args
, tf_none
, NULL_TREE
);
16497 if (decl_type
== error_mark_node
)
16501 /* Never do unification on the 'this' parameter. */
16502 decl_arg_types
= skip_artificial_parms_for (decl
,
16503 TYPE_ARG_TYPES (decl_type
));
16505 nargs
= list_length (decl_arg_types
);
16506 args
= XALLOCAVEC (tree
, nargs
);
16507 for (arg
= decl_arg_types
, ix
= 0;
16508 arg
!= NULL_TREE
&& arg
!= void_list_node
;
16509 arg
= TREE_CHAIN (arg
), ++ix
)
16510 args
[ix
] = TREE_VALUE (arg
);
16512 if (fn_type_unification (fn
, explicit_args
, targs
,
16514 (check_rettype
|| DECL_CONV_FN_P (fn
)
16515 ? TREE_TYPE (decl_type
) : NULL_TREE
),
16516 DEDUCE_EXACT
, LOOKUP_NORMAL
))
16522 /* Return the innermost template arguments that, when applied to a
16523 template specialization whose innermost template parameters are
16524 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
16527 For example, suppose we have:
16529 template <class T, class U> struct S {};
16530 template <class T> struct S<T*, int> {};
16532 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
16533 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
16534 int}. The resulting vector will be {double}, indicating that `T'
16535 is bound to `double'. */
16538 get_class_bindings (tree tparms
, tree spec_args
, tree args
)
16540 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
16542 tree innermost_deduced_args
;
16544 innermost_deduced_args
= make_tree_vec (ntparms
);
16545 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
16547 deduced_args
= copy_node (args
);
16548 SET_TMPL_ARGS_LEVEL (deduced_args
,
16549 TMPL_ARGS_DEPTH (deduced_args
),
16550 innermost_deduced_args
);
16553 deduced_args
= innermost_deduced_args
;
16555 if (unify (tparms
, deduced_args
,
16556 INNERMOST_TEMPLATE_ARGS (spec_args
),
16557 INNERMOST_TEMPLATE_ARGS (args
),
16561 for (i
= 0; i
< ntparms
; ++i
)
16562 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
16565 /* Verify that nondeduced template arguments agree with the type
16566 obtained from argument deduction.
16570 struct A { typedef int X; };
16571 template <class T, class U> struct C {};
16572 template <class T> struct C<T, typename T::X> {};
16574 Then with the instantiation `C<A, int>', we can deduce that
16575 `T' is `A' but unify () does not check whether `typename T::X'
16577 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
16578 if (spec_args
== error_mark_node
16579 /* We only need to check the innermost arguments; the other
16580 arguments will always agree. */
16581 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
16582 INNERMOST_TEMPLATE_ARGS (args
)))
16585 /* Now that we have bindings for all of the template arguments,
16586 ensure that the arguments deduced for the template template
16587 parameters have compatible template parameter lists. See the use
16588 of template_template_parm_bindings_ok_p in fn_type_unification
16589 for more information. */
16590 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
16593 return deduced_args
;
16596 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
16597 Return the TREE_LIST node with the most specialized template, if
16598 any. If there is no most specialized template, the error_mark_node
16601 Note that this function does not look at, or modify, the
16602 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
16603 returned is one of the elements of INSTANTIATIONS, callers may
16604 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
16605 and retrieve it from the value returned. */
16608 most_specialized_instantiation (tree templates
)
16612 ++processing_template_decl
;
16615 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
16619 if (get_bindings (TREE_VALUE (champ
),
16620 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
16621 NULL_TREE
, /*check_ret=*/false))
16624 if (get_bindings (TREE_VALUE (fn
),
16625 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
16626 NULL_TREE
, /*check_ret=*/false))
16633 /* Equally specialized, move to next function. If there
16634 is no next function, nothing's most specialized. */
16635 fn
= TREE_CHAIN (fn
);
16643 /* Now verify that champ is better than everything earlier in the
16644 instantiation list. */
16645 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
16646 if (get_bindings (TREE_VALUE (champ
),
16647 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
16648 NULL_TREE
, /*check_ret=*/false)
16649 || !get_bindings (TREE_VALUE (fn
),
16650 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
16651 NULL_TREE
, /*check_ret=*/false))
16657 processing_template_decl
--;
16660 return error_mark_node
;
16665 /* If DECL is a specialization of some template, return the most
16666 general such template. Otherwise, returns NULL_TREE.
16668 For example, given:
16670 template <class T> struct S { template <class U> void f(U); };
16672 if TMPL is `template <class U> void S<int>::f(U)' this will return
16673 the full template. This function will not trace past partial
16674 specializations, however. For example, given in addition:
16676 template <class T> struct S<T*> { template <class U> void f(U); };
16678 if TMPL is `template <class U> void S<int*>::f(U)' this will return
16679 `template <class T> template <class U> S<T*>::f(U)'. */
16682 most_general_template (tree decl
)
16684 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
16685 an immediate specialization. */
16686 if (TREE_CODE (decl
) == FUNCTION_DECL
)
16688 if (DECL_TEMPLATE_INFO (decl
)) {
16689 decl
= DECL_TI_TEMPLATE (decl
);
16691 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
16692 template friend. */
16693 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
16699 /* Look for more and more general templates. */
16700 while (DECL_TEMPLATE_INFO (decl
))
16702 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
16703 (See cp-tree.h for details.) */
16704 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
16707 if (CLASS_TYPE_P (TREE_TYPE (decl
))
16708 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
16711 /* Stop if we run into an explicitly specialized class template. */
16712 if (!DECL_NAMESPACE_SCOPE_P (decl
)
16713 && DECL_CONTEXT (decl
)
16714 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
16717 decl
= DECL_TI_TEMPLATE (decl
);
16723 /* Return the most specialized of the class template partial
16724 specializations of TMPL which can produce TYPE, a specialization of
16725 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
16726 a _TYPE node corresponding to the partial specialization, while the
16727 TREE_PURPOSE is the set of template arguments that must be
16728 substituted into the TREE_TYPE in order to generate TYPE.
16730 If the choice of partial specialization is ambiguous, a diagnostic
16731 is issued, and the error_mark_node is returned. If there are no
16732 partial specializations of TMPL matching TYPE, then NULL_TREE is
16736 most_specialized_class (tree type
, tree tmpl
, tsubst_flags_t complain
)
16738 tree list
= NULL_TREE
;
16744 tree outer_args
= NULL_TREE
;
16746 tmpl
= most_general_template (tmpl
);
16747 args
= CLASSTYPE_TI_ARGS (type
);
16749 /* For determining which partial specialization to use, only the
16750 innermost args are interesting. */
16751 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
16753 outer_args
= strip_innermost_template_args (args
, 1);
16754 args
= INNERMOST_TEMPLATE_ARGS (args
);
16757 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
16759 tree partial_spec_args
;
16761 tree parms
= TREE_VALUE (t
);
16763 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
16765 ++processing_template_decl
;
16771 /* Discard the outer levels of args, and then substitute in the
16772 template args from the enclosing class. */
16773 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
16774 partial_spec_args
= tsubst_template_args
16775 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
16777 /* PARMS already refers to just the innermost parms, but the
16778 template parms in partial_spec_args had their levels lowered
16779 by tsubst, so we need to do the same for the parm list. We
16780 can't just tsubst the TREE_VEC itself, as tsubst wants to
16781 treat a TREE_VEC as an argument vector. */
16782 parms
= copy_node (parms
);
16783 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
16784 TREE_VEC_ELT (parms
, i
) =
16785 tsubst (TREE_VEC_ELT (parms
, i
), outer_args
, tf_none
, NULL_TREE
);
16789 partial_spec_args
=
16790 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
16791 add_to_template_args (outer_args
,
16792 partial_spec_args
),
16794 /*require_all_args=*/true,
16795 /*use_default_args=*/true);
16797 --processing_template_decl
;
16799 if (partial_spec_args
== error_mark_node
)
16800 return error_mark_node
;
16802 spec_args
= get_class_bindings (parms
,
16808 spec_args
= add_to_template_args (outer_args
, spec_args
);
16809 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
16810 TREE_TYPE (list
) = TREE_TYPE (t
);
16817 ambiguous_p
= false;
16820 t
= TREE_CHAIN (t
);
16821 for (; t
; t
= TREE_CHAIN (t
))
16823 fate
= more_specialized_class (champ
, t
);
16830 t
= TREE_CHAIN (t
);
16833 ambiguous_p
= true;
16842 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
16844 fate
= more_specialized_class (champ
, t
);
16847 ambiguous_p
= true;
16855 char *spaces
= NULL
;
16856 if (!(complain
& tf_error
))
16857 return error_mark_node
;
16858 error ("ambiguous class template instantiation for %q#T", type
);
16859 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
16860 for (t
= list
; t
; t
= TREE_CHAIN (t
))
16862 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
16863 spaces
= spaces
? spaces
: get_spaces (str
);
16866 return error_mark_node
;
16872 /* Explicitly instantiate DECL. */
16875 do_decl_instantiation (tree decl
, tree storage
)
16877 tree result
= NULL_TREE
;
16880 if (!decl
|| decl
== error_mark_node
)
16881 /* An error occurred, for which grokdeclarator has already issued
16882 an appropriate message. */
16884 else if (! DECL_LANG_SPECIFIC (decl
))
16886 error ("explicit instantiation of non-template %q#D", decl
);
16889 else if (TREE_CODE (decl
) == VAR_DECL
)
16891 /* There is an asymmetry here in the way VAR_DECLs and
16892 FUNCTION_DECLs are handled by grokdeclarator. In the case of
16893 the latter, the DECL we get back will be marked as a
16894 template instantiation, and the appropriate
16895 DECL_TEMPLATE_INFO will be set up. This does not happen for
16896 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
16897 should handle VAR_DECLs as it currently handles
16899 if (!DECL_CLASS_SCOPE_P (decl
))
16901 error ("%qD is not a static data member of a class template", decl
);
16904 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
16905 if (!result
|| TREE_CODE (result
) != VAR_DECL
)
16907 error ("no matching template for %qD found", decl
);
16910 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
16912 error ("type %qT for explicit instantiation %qD does not match "
16913 "declared type %qT", TREE_TYPE (result
), decl
,
16918 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
16920 error ("explicit instantiation of %q#D", decl
);
16926 /* Check for various error cases. Note that if the explicit
16927 instantiation is valid the RESULT will currently be marked as an
16928 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
16929 until we get here. */
16931 if (DECL_TEMPLATE_SPECIALIZATION (result
))
16933 /* DR 259 [temp.spec].
16935 Both an explicit instantiation and a declaration of an explicit
16936 specialization shall not appear in a program unless the explicit
16937 instantiation follows a declaration of the explicit specialization.
16939 For a given set of template parameters, if an explicit
16940 instantiation of a template appears after a declaration of an
16941 explicit specialization for that template, the explicit
16942 instantiation has no effect. */
16945 else if (DECL_EXPLICIT_INSTANTIATION (result
))
16949 No program shall explicitly instantiate any template more
16952 We check DECL_NOT_REALLY_EXTERN so as not to complain when
16953 the first instantiation was `extern' and the second is not,
16954 and EXTERN_P for the opposite case. */
16955 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
16956 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
16957 /* If an "extern" explicit instantiation follows an ordinary
16958 explicit instantiation, the template is instantiated. */
16962 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
16964 error ("no matching template for %qD found", result
);
16967 else if (!DECL_TEMPLATE_INFO (result
))
16969 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
16973 if (storage
== NULL_TREE
)
16975 else if (storage
== ridpointers
[(int) RID_EXTERN
])
16977 if (!in_system_header
&& (cxx_dialect
== cxx98
))
16978 pedwarn (input_location
, OPT_pedantic
,
16979 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
16984 error ("storage class %qD applied to template instantiation", storage
);
16986 check_explicit_instantiation_namespace (result
);
16987 mark_decl_instantiated (result
, extern_p
);
16989 instantiate_decl (result
, /*defer_ok=*/1,
16990 /*expl_inst_class_mem_p=*/false);
16994 mark_class_instantiated (tree t
, int extern_p
)
16996 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
16997 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
16998 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
16999 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
17002 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
17003 rest_of_type_compilation (t
, 1);
17007 /* Called from do_type_instantiation through binding_table_foreach to
17008 do recursive instantiation for the type bound in ENTRY. */
17010 bt_instantiate_type_proc (binding_entry entry
, void *data
)
17012 tree storage
= *(tree
*) data
;
17014 if (MAYBE_CLASS_TYPE_P (entry
->type
)
17015 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
17016 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
17019 /* Called from do_type_instantiation to instantiate a member
17020 (a member function or a static member variable) of an
17021 explicitly instantiated class template. */
17023 instantiate_class_member (tree decl
, int extern_p
)
17025 mark_decl_instantiated (decl
, extern_p
);
17027 instantiate_decl (decl
, /*defer_ok=*/1,
17028 /*expl_inst_class_mem_p=*/true);
17031 /* Perform an explicit instantiation of template class T. STORAGE, if
17032 non-null, is the RID for extern, inline or static. COMPLAIN is
17033 nonzero if this is called from the parser, zero if called recursively,
17034 since the standard is unclear (as detailed below). */
17037 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
17042 int previous_instantiation_extern_p
= 0;
17044 if (TREE_CODE (t
) == TYPE_DECL
)
17047 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
17049 error ("explicit instantiation of non-template type %qT", t
);
17055 if (!COMPLETE_TYPE_P (t
))
17057 if (complain
& tf_error
)
17058 error ("explicit instantiation of %q#T before definition of template",
17063 if (storage
!= NULL_TREE
)
17065 if (!in_system_header
)
17067 if (storage
== ridpointers
[(int) RID_EXTERN
])
17069 if (cxx_dialect
== cxx98
)
17070 pedwarn (input_location
, OPT_pedantic
,
17071 "ISO C++ 1998 forbids the use of %<extern%> on "
17072 "explicit instantiations");
17075 pedwarn (input_location
, OPT_pedantic
,
17076 "ISO C++ forbids the use of %qE"
17077 " on explicit instantiations", storage
);
17080 if (storage
== ridpointers
[(int) RID_INLINE
])
17082 else if (storage
== ridpointers
[(int) RID_EXTERN
])
17084 else if (storage
== ridpointers
[(int) RID_STATIC
])
17088 error ("storage class %qD applied to template instantiation",
17094 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
17096 /* DR 259 [temp.spec].
17098 Both an explicit instantiation and a declaration of an explicit
17099 specialization shall not appear in a program unless the explicit
17100 instantiation follows a declaration of the explicit specialization.
17102 For a given set of template parameters, if an explicit
17103 instantiation of a template appears after a declaration of an
17104 explicit specialization for that template, the explicit
17105 instantiation has no effect. */
17108 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
17112 No program shall explicitly instantiate any template more
17115 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
17116 instantiation was `extern'. If EXTERN_P then the second is.
17117 These cases are OK. */
17118 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
17120 if (!previous_instantiation_extern_p
&& !extern_p
17121 && (complain
& tf_error
))
17122 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
17124 /* If we've already instantiated the template, just return now. */
17125 if (!CLASSTYPE_INTERFACE_ONLY (t
))
17129 check_explicit_instantiation_namespace (TYPE_NAME (t
));
17130 mark_class_instantiated (t
, extern_p
);
17138 /* In contrast to implicit instantiation, where only the
17139 declarations, and not the definitions, of members are
17140 instantiated, we have here:
17144 The explicit instantiation of a class template specialization
17145 implies the instantiation of all of its members not
17146 previously explicitly specialized in the translation unit
17147 containing the explicit instantiation.
17149 Of course, we can't instantiate member template classes, since
17150 we don't have any arguments for them. Note that the standard
17151 is unclear on whether the instantiation of the members are
17152 *explicit* instantiations or not. However, the most natural
17153 interpretation is that it should be an explicit instantiation. */
17156 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
17157 if (TREE_CODE (tmp
) == FUNCTION_DECL
17158 && DECL_TEMPLATE_INSTANTIATION (tmp
))
17159 instantiate_class_member (tmp
, extern_p
);
17161 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
17162 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
17163 instantiate_class_member (tmp
, extern_p
);
17165 if (CLASSTYPE_NESTED_UTDS (t
))
17166 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
17167 bt_instantiate_type_proc
, &storage
);
17171 /* Given a function DECL, which is a specialization of TMPL, modify
17172 DECL to be a re-instantiation of TMPL with the same template
17173 arguments. TMPL should be the template into which tsubst'ing
17174 should occur for DECL, not the most general template.
17176 One reason for doing this is a scenario like this:
17179 void f(const T&, int i);
17181 void g() { f(3, 7); }
17184 void f(const T& t, const int i) { }
17186 Note that when the template is first instantiated, with
17187 instantiate_template, the resulting DECL will have no name for the
17188 first parameter, and the wrong type for the second. So, when we go
17189 to instantiate the DECL, we regenerate it. */
17192 regenerate_decl_from_template (tree decl
, tree tmpl
)
17194 /* The arguments used to instantiate DECL, from the most general
17199 args
= DECL_TI_ARGS (decl
);
17200 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
17202 /* Make sure that we can see identifiers, and compute access
17204 push_access_scope (decl
);
17206 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17214 args_depth
= TMPL_ARGS_DEPTH (args
);
17215 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
17216 if (args_depth
> parms_depth
)
17217 args
= get_innermost_template_args (args
, parms_depth
);
17219 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
17220 args
, tf_error
, NULL_TREE
);
17222 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
17225 /* Merge parameter declarations. */
17226 decl_parm
= skip_artificial_parms_for (decl
,
17227 DECL_ARGUMENTS (decl
));
17229 = skip_artificial_parms_for (code_pattern
,
17230 DECL_ARGUMENTS (code_pattern
));
17231 while (decl_parm
&& !FUNCTION_PARAMETER_PACK_P (pattern_parm
))
17236 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
17237 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
17238 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
17240 parm_type
= type_decays_to (parm_type
);
17241 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
17242 TREE_TYPE (decl_parm
) = parm_type
;
17243 attributes
= DECL_ATTRIBUTES (pattern_parm
);
17244 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
17246 DECL_ATTRIBUTES (decl_parm
) = attributes
;
17247 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
17249 decl_parm
= DECL_CHAIN (decl_parm
);
17250 pattern_parm
= DECL_CHAIN (pattern_parm
);
17252 /* Merge any parameters that match with the function parameter
17254 if (pattern_parm
&& FUNCTION_PARAMETER_PACK_P (pattern_parm
))
17257 tree expanded_types
;
17258 /* Expand the TYPE_PACK_EXPANSION that provides the types for
17259 the parameters in this function parameter pack. */
17260 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
17261 args
, tf_error
, NULL_TREE
);
17262 len
= TREE_VEC_LENGTH (expanded_types
);
17263 for (i
= 0; i
< len
; i
++)
17268 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
17269 /* Rename the parameter to include the index. */
17270 DECL_NAME (decl_parm
) =
17271 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
17272 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
17273 parm_type
= type_decays_to (parm_type
);
17274 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
17275 TREE_TYPE (decl_parm
) = parm_type
;
17276 attributes
= DECL_ATTRIBUTES (pattern_parm
);
17277 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
17279 DECL_ATTRIBUTES (decl_parm
) = attributes
;
17280 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
17282 decl_parm
= DECL_CHAIN (decl_parm
);
17285 /* Merge additional specifiers from the CODE_PATTERN. */
17286 if (DECL_DECLARED_INLINE_P (code_pattern
)
17287 && !DECL_DECLARED_INLINE_P (decl
))
17288 DECL_DECLARED_INLINE_P (decl
) = 1;
17290 else if (TREE_CODE (decl
) == VAR_DECL
)
17292 DECL_INITIAL (decl
) =
17293 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
17294 tf_error
, DECL_TI_TEMPLATE (decl
),
17295 /*integral_constant_expression_p=*/false);
17296 if (VAR_HAD_UNKNOWN_BOUND (decl
))
17297 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
17298 tf_error
, DECL_TI_TEMPLATE (decl
));
17301 gcc_unreachable ();
17303 pop_access_scope (decl
);
17306 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
17307 substituted to get DECL. */
17310 template_for_substitution (tree decl
)
17312 tree tmpl
= DECL_TI_TEMPLATE (decl
);
17314 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
17315 for the instantiation. This is not always the most general
17316 template. Consider, for example:
17319 struct S { template <class U> void f();
17320 template <> void f<int>(); };
17322 and an instantiation of S<double>::f<int>. We want TD to be the
17323 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
17324 while (/* An instantiation cannot have a definition, so we need a
17325 more general template. */
17326 DECL_TEMPLATE_INSTANTIATION (tmpl
)
17327 /* We must also deal with friend templates. Given:
17329 template <class T> struct S {
17330 template <class U> friend void f() {};
17333 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
17334 so far as the language is concerned, but that's still
17335 where we get the pattern for the instantiation from. On
17336 other hand, if the definition comes outside the class, say:
17338 template <class T> struct S {
17339 template <class U> friend void f();
17341 template <class U> friend void f() {}
17343 we don't need to look any further. That's what the check for
17344 DECL_INITIAL is for. */
17345 || (TREE_CODE (decl
) == FUNCTION_DECL
17346 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
17347 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
17349 /* The present template, TD, should not be a definition. If it
17350 were a definition, we should be using it! Note that we
17351 cannot restructure the loop to just keep going until we find
17352 a template with a definition, since that might go too far if
17353 a specialization was declared, but not defined. */
17354 gcc_assert (TREE_CODE (decl
) != VAR_DECL
17355 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
17357 /* Fetch the more general template. */
17358 tmpl
= DECL_TI_TEMPLATE (tmpl
);
17364 /* Returns true if we need to instantiate this template instance even if we
17365 know we aren't going to emit it.. */
17368 always_instantiate_p (tree decl
)
17370 /* We always instantiate inline functions so that we can inline them. An
17371 explicit instantiation declaration prohibits implicit instantiation of
17372 non-inline functions. With high levels of optimization, we would
17373 normally inline non-inline functions -- but we're not allowed to do
17374 that for "extern template" functions. Therefore, we check
17375 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
17376 return ((TREE_CODE (decl
) == FUNCTION_DECL
17377 && DECL_DECLARED_INLINE_P (decl
))
17378 /* And we need to instantiate static data members so that
17379 their initializers are available in integral constant
17381 || (TREE_CODE (decl
) == VAR_DECL
17382 && decl_maybe_constant_var_p (decl
)));
17385 /* Produce the definition of D, a _DECL generated from a template. If
17386 DEFER_OK is nonzero, then we don't have to actually do the
17387 instantiation now; we just have to do it sometime. Normally it is
17388 an error if this is an explicit instantiation but D is undefined.
17389 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
17390 explicitly instantiated class template. */
17393 instantiate_decl (tree d
, int defer_ok
,
17394 bool expl_inst_class_mem_p
)
17396 tree tmpl
= DECL_TI_TEMPLATE (d
);
17403 bool pattern_defined
;
17405 location_t saved_loc
= input_location
;
17408 /* This function should only be used to instantiate templates for
17409 functions and static member variables. */
17410 gcc_assert (TREE_CODE (d
) == FUNCTION_DECL
17411 || TREE_CODE (d
) == VAR_DECL
);
17413 /* Variables are never deferred; if instantiation is required, they
17414 are instantiated right away. That allows for better code in the
17415 case that an expression refers to the value of the variable --
17416 if the variable has a constant value the referring expression can
17417 take advantage of that fact. */
17418 if (TREE_CODE (d
) == VAR_DECL
17419 || DECL_DECLARED_CONSTEXPR_P (d
))
17422 /* Don't instantiate cloned functions. Instead, instantiate the
17423 functions they cloned. */
17424 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
17425 d
= DECL_CLONED_FUNCTION (d
);
17427 if (DECL_TEMPLATE_INSTANTIATED (d
)
17428 || DECL_TEMPLATE_SPECIALIZATION (d
))
17429 /* D has already been instantiated or explicitly specialized, so
17430 there's nothing for us to do here.
17432 It might seem reasonable to check whether or not D is an explicit
17433 instantiation, and, if so, stop here. But when an explicit
17434 instantiation is deferred until the end of the compilation,
17435 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
17436 the instantiation. */
17439 /* Check to see whether we know that this template will be
17440 instantiated in some other file, as with "extern template"
17442 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
17444 /* In general, we do not instantiate such templates. */
17445 if (external_p
&& !always_instantiate_p (d
))
17448 gen_tmpl
= most_general_template (tmpl
);
17449 gen_args
= DECL_TI_ARGS (d
);
17451 if (tmpl
!= gen_tmpl
)
17452 /* We should already have the extra args. */
17453 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
17454 == TMPL_ARGS_DEPTH (gen_args
));
17455 /* And what's in the hash table should match D. */
17456 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
17457 || spec
== NULL_TREE
);
17459 /* This needs to happen before any tsubsting. */
17460 if (! push_tinst_level (d
))
17463 timevar_push (TV_TEMPLATE_INST
);
17465 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
17466 for the instantiation. */
17467 td
= template_for_substitution (d
);
17468 code_pattern
= DECL_TEMPLATE_RESULT (td
);
17470 /* We should never be trying to instantiate a member of a class
17471 template or partial specialization. */
17472 gcc_assert (d
!= code_pattern
);
17474 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
17475 || DECL_TEMPLATE_SPECIALIZATION (td
))
17476 /* In the case of a friend template whose definition is provided
17477 outside the class, we may have too many arguments. Drop the
17478 ones we don't need. The same is true for specializations. */
17479 args
= get_innermost_template_args
17480 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
17484 if (TREE_CODE (d
) == FUNCTION_DECL
)
17485 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
17486 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
));
17488 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
17490 /* We may be in the middle of deferred access check. Disable it now. */
17491 push_deferring_access_checks (dk_no_deferred
);
17493 /* Unless an explicit instantiation directive has already determined
17494 the linkage of D, remember that a definition is available for
17496 if (pattern_defined
17497 && !DECL_INTERFACE_KNOWN (d
)
17498 && !DECL_NOT_REALLY_EXTERN (d
))
17499 mark_definable (d
);
17501 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
17502 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
17503 input_location
= DECL_SOURCE_LOCATION (d
);
17505 /* If D is a member of an explicitly instantiated class template,
17506 and no definition is available, treat it like an implicit
17508 if (!pattern_defined
&& expl_inst_class_mem_p
17509 && DECL_EXPLICIT_INSTANTIATION (d
))
17511 /* Leave linkage flags alone on instantiations with anonymous
17513 if (TREE_PUBLIC (d
))
17515 DECL_NOT_REALLY_EXTERN (d
) = 0;
17516 DECL_INTERFACE_KNOWN (d
) = 0;
17518 SET_DECL_IMPLICIT_INSTANTIATION (d
);
17521 /* Recheck the substitutions to obtain any warning messages
17522 about ignoring cv qualifiers. Don't do this for artificial decls,
17523 as it breaks the context-sensitive substitution for lambda op(). */
17524 if (!defer_ok
&& !DECL_ARTIFICIAL (d
))
17526 tree gen
= DECL_TEMPLATE_RESULT (gen_tmpl
);
17527 tree type
= TREE_TYPE (gen
);
17529 /* Make sure that we can see identifiers, and compute access
17530 correctly. D is already the target FUNCTION_DECL with the
17532 push_access_scope (d
);
17534 if (TREE_CODE (gen
) == FUNCTION_DECL
)
17536 tsubst (DECL_ARGUMENTS (gen
), gen_args
, tf_warning_or_error
, d
);
17537 tsubst_exception_specification (type
, gen_args
, tf_warning_or_error
,
17539 /* Don't simply tsubst the function type, as that will give
17540 duplicate warnings about poor parameter qualifications.
17541 The function arguments are the same as the decl_arguments
17542 without the top level cv qualifiers. */
17543 type
= TREE_TYPE (type
);
17545 tsubst (type
, gen_args
, tf_warning_or_error
, d
);
17547 pop_access_scope (d
);
17550 /* Defer all other templates, unless we have been explicitly
17551 forbidden from doing so. */
17552 if (/* If there is no definition, we cannot instantiate the
17555 /* If it's OK to postpone instantiation, do so. */
17557 /* If this is a static data member that will be defined
17558 elsewhere, we don't want to instantiate the entire data
17559 member, but we do want to instantiate the initializer so that
17560 we can substitute that elsewhere. */
17561 || (external_p
&& TREE_CODE (d
) == VAR_DECL
))
17563 /* The definition of the static data member is now required so
17564 we must substitute the initializer. */
17565 if (TREE_CODE (d
) == VAR_DECL
17566 && !DECL_INITIAL (d
)
17567 && DECL_INITIAL (code_pattern
))
17571 bool const_init
= false;
17573 ns
= decl_namespace_context (d
);
17574 push_nested_namespace (ns
);
17575 push_nested_class (DECL_CONTEXT (d
));
17576 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
17578 tf_warning_or_error
, NULL_TREE
,
17579 /*integral_constant_expression_p=*/false);
17580 /* Make sure the initializer is still constant, in case of
17581 circular dependency (template/instantiate6.C). */
17583 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
17584 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
17585 /*asmspec_tree=*/NULL_TREE
,
17586 LOOKUP_ONLYCONVERTING
);
17587 pop_nested_class ();
17588 pop_nested_namespace (ns
);
17591 /* We restore the source position here because it's used by
17592 add_pending_template. */
17593 input_location
= saved_loc
;
17595 if (at_eof
&& !pattern_defined
17596 && DECL_EXPLICIT_INSTANTIATION (d
)
17597 && DECL_NOT_REALLY_EXTERN (d
))
17600 The definition of a non-exported function template, a
17601 non-exported member function template, or a non-exported
17602 member function or static data member of a class template
17603 shall be present in every translation unit in which it is
17604 explicitly instantiated. */
17605 permerror (input_location
, "explicit instantiation of %qD "
17606 "but no definition available", d
);
17608 /* If we're in unevaluated context, we just wanted to get the
17609 constant value; this isn't an odr use, so don't queue
17610 a full instantiation. */
17611 if (cp_unevaluated_operand
!= 0)
17613 /* ??? Historically, we have instantiated inline functions, even
17614 when marked as "extern template". */
17615 if (!(external_p
&& TREE_CODE (d
) == VAR_DECL
))
17616 add_pending_template (d
);
17619 /* Tell the repository that D is available in this translation unit
17620 -- and see if it is supposed to be instantiated here. */
17621 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
17623 /* In a PCH file, despite the fact that the repository hasn't
17624 requested instantiation in the PCH it is still possible that
17625 an instantiation will be required in a file that includes the
17628 add_pending_template (d
);
17629 /* Instantiate inline functions so that the inliner can do its
17630 job, even though we'll not be emitting a copy of this
17632 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
17636 need_push
= !cfun
|| !global_bindings_p ();
17638 push_to_top_level ();
17640 /* Mark D as instantiated so that recursive calls to
17641 instantiate_decl do not try to instantiate it again. */
17642 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
17644 /* Regenerate the declaration in case the template has been modified
17645 by a subsequent redeclaration. */
17646 regenerate_decl_from_template (d
, td
);
17648 /* We already set the file and line above. Reset them now in case
17649 they changed as a result of calling regenerate_decl_from_template. */
17650 input_location
= DECL_SOURCE_LOCATION (d
);
17652 if (TREE_CODE (d
) == VAR_DECL
)
17655 bool const_init
= false;
17657 /* Clear out DECL_RTL; whatever was there before may not be right
17658 since we've reset the type of the declaration. */
17659 SET_DECL_RTL (d
, NULL
);
17660 DECL_IN_AGGR_P (d
) = 0;
17662 /* The initializer is placed in DECL_INITIAL by
17663 regenerate_decl_from_template so we don't need to
17664 push/pop_access_scope again here. Pull it out so that
17665 cp_finish_decl can process it. */
17666 init
= DECL_INITIAL (d
);
17667 DECL_INITIAL (d
) = NULL_TREE
;
17668 DECL_INITIALIZED_P (d
) = 0;
17670 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
17671 initializer. That function will defer actual emission until
17672 we have a chance to determine linkage. */
17673 DECL_EXTERNAL (d
) = 0;
17675 /* Enter the scope of D so that access-checking works correctly. */
17676 push_nested_class (DECL_CONTEXT (d
));
17677 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
17678 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
17679 pop_nested_class ();
17681 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
17682 synthesize_method (d
);
17683 else if (TREE_CODE (d
) == FUNCTION_DECL
)
17685 htab_t saved_local_specializations
;
17690 /* Save away the current list, in case we are instantiating one
17691 template from within the body of another. */
17692 saved_local_specializations
= local_specializations
;
17694 /* Set up the list of local specializations. */
17695 local_specializations
= htab_create (37,
17696 hash_local_specialization
,
17697 eq_local_specializations
,
17700 /* Set up context. */
17701 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
17703 /* Create substitution entries for the parameters. */
17704 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
17705 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
17706 spec_parm
= DECL_ARGUMENTS (d
);
17707 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
17709 register_local_specialization (spec_parm
, tmpl_parm
);
17710 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
17711 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
17713 while (tmpl_parm
&& !FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
17715 register_local_specialization (spec_parm
, tmpl_parm
);
17716 tmpl_parm
= DECL_CHAIN (tmpl_parm
);
17717 spec_parm
= DECL_CHAIN (spec_parm
);
17719 if (tmpl_parm
&& FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
17721 /* Register the (value) argument pack as a specialization of
17722 TMPL_PARM, then move on. */
17723 tree argpack
= make_fnparm_pack (spec_parm
);
17724 register_local_specialization (argpack
, tmpl_parm
);
17725 tmpl_parm
= DECL_CHAIN (tmpl_parm
);
17726 spec_parm
= NULL_TREE
;
17728 gcc_assert (!spec_parm
);
17730 /* Substitute into the body of the function. */
17731 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
17732 tf_warning_or_error
, tmpl
,
17733 /*integral_constant_expression_p=*/false);
17735 /* Set the current input_location to the end of the function
17736 so that finish_function knows where we are. */
17737 input_location
= DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
17739 /* We don't need the local specializations any more. */
17740 htab_delete (local_specializations
);
17741 local_specializations
= saved_local_specializations
;
17743 /* Finish the function. */
17744 d
= finish_function (0);
17745 expand_or_defer_fn (d
);
17748 /* We're not deferring instantiation any more. */
17749 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
17752 pop_from_top_level ();
17755 input_location
= saved_loc
;
17756 pop_deferring_access_checks ();
17757 pop_tinst_level ();
17759 timevar_pop (TV_TEMPLATE_INST
);
17764 /* Run through the list of templates that we wish we could
17765 instantiate, and instantiate any we can. RETRIES is the
17766 number of times we retry pending template instantiation. */
17769 instantiate_pending_templates (int retries
)
17772 location_t saved_loc
= input_location
;
17774 /* Instantiating templates may trigger vtable generation. This in turn
17775 may require further template instantiations. We place a limit here
17776 to avoid infinite loop. */
17777 if (pending_templates
&& retries
>= max_tinst_depth
)
17779 tree decl
= pending_templates
->tinst
->decl
;
17781 error ("template instantiation depth exceeds maximum of %d"
17782 " instantiating %q+D, possibly from virtual table generation"
17783 " (use -ftemplate-depth= to increase the maximum)",
17784 max_tinst_depth
, decl
);
17785 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17786 /* Pretend that we defined it. */
17787 DECL_INITIAL (decl
) = error_mark_node
;
17793 struct pending_template
**t
= &pending_templates
;
17794 struct pending_template
*last
= NULL
;
17798 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
17799 bool complete
= false;
17801 if (TYPE_P (instantiation
))
17805 if (!COMPLETE_TYPE_P (instantiation
))
17807 instantiate_class_template (instantiation
);
17808 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
17809 for (fn
= TYPE_METHODS (instantiation
);
17811 fn
= TREE_CHAIN (fn
))
17812 if (! DECL_ARTIFICIAL (fn
))
17813 instantiate_decl (fn
,
17815 /*expl_inst_class_mem_p=*/false);
17816 if (COMPLETE_TYPE_P (instantiation
))
17820 complete
= COMPLETE_TYPE_P (instantiation
);
17824 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
17825 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
17828 = instantiate_decl (instantiation
,
17830 /*expl_inst_class_mem_p=*/false);
17831 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
17835 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
17836 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
17840 /* If INSTANTIATION has been instantiated, then we don't
17841 need to consider it again in the future. */
17849 current_tinst_level
= NULL
;
17851 last_pending_template
= last
;
17853 while (reconsider
);
17855 input_location
= saved_loc
;
17858 /* Substitute ARGVEC into T, which is a list of initializers for
17859 either base class or a non-static data member. The TREE_PURPOSEs
17860 are DECLs, and the TREE_VALUEs are the initializer values. Used by
17861 instantiate_decl. */
17864 tsubst_initializer_list (tree t
, tree argvec
)
17866 tree inits
= NULL_TREE
;
17868 for (; t
; t
= TREE_CHAIN (t
))
17872 tree expanded_bases
= NULL_TREE
;
17873 tree expanded_arguments
= NULL_TREE
;
17876 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
17881 /* Expand the base class expansion type into separate base
17883 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
17884 tf_warning_or_error
,
17886 if (expanded_bases
== error_mark_node
)
17889 /* We'll be building separate TREE_LISTs of arguments for
17891 len
= TREE_VEC_LENGTH (expanded_bases
);
17892 expanded_arguments
= make_tree_vec (len
);
17893 for (i
= 0; i
< len
; i
++)
17894 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
17896 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
17897 expand each argument in the TREE_VALUE of t. */
17898 expr
= make_node (EXPR_PACK_EXPANSION
);
17899 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
17900 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
17902 if (TREE_VALUE (t
) == void_type_node
)
17903 /* VOID_TYPE_NODE is used to indicate
17904 value-initialization. */
17906 for (i
= 0; i
< len
; i
++)
17907 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
17911 /* Substitute parameter packs into each argument in the
17913 in_base_initializer
= 1;
17914 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
17916 tree expanded_exprs
;
17918 /* Expand the argument. */
17919 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
17921 = tsubst_pack_expansion (expr
, argvec
,
17922 tf_warning_or_error
,
17924 if (expanded_exprs
== error_mark_node
)
17927 /* Prepend each of the expanded expressions to the
17928 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
17929 for (i
= 0; i
< len
; i
++)
17931 TREE_VEC_ELT (expanded_arguments
, i
) =
17932 tree_cons (NULL_TREE
,
17933 TREE_VEC_ELT (expanded_exprs
, i
),
17934 TREE_VEC_ELT (expanded_arguments
, i
));
17937 in_base_initializer
= 0;
17939 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
17940 since we built them backwards. */
17941 for (i
= 0; i
< len
; i
++)
17943 TREE_VEC_ELT (expanded_arguments
, i
) =
17944 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
17949 for (i
= 0; i
< len
; ++i
)
17951 if (expanded_bases
)
17953 decl
= TREE_VEC_ELT (expanded_bases
, i
);
17954 decl
= expand_member_init (decl
);
17955 init
= TREE_VEC_ELT (expanded_arguments
, i
);
17959 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
17960 tf_warning_or_error
, NULL_TREE
);
17962 decl
= expand_member_init (decl
);
17963 if (decl
&& !DECL_P (decl
))
17964 in_base_initializer
= 1;
17966 init
= TREE_VALUE (t
);
17967 if (init
!= void_type_node
)
17968 init
= tsubst_expr (init
, argvec
,
17969 tf_warning_or_error
, NULL_TREE
,
17970 /*integral_constant_expression_p=*/false);
17971 in_base_initializer
= 0;
17976 init
= build_tree_list (decl
, init
);
17977 TREE_CHAIN (init
) = inits
;
17985 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
17988 set_current_access_from_decl (tree decl
)
17990 if (TREE_PRIVATE (decl
))
17991 current_access_specifier
= access_private_node
;
17992 else if (TREE_PROTECTED (decl
))
17993 current_access_specifier
= access_protected_node
;
17995 current_access_specifier
= access_public_node
;
17998 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
17999 is the instantiation (which should have been created with
18000 start_enum) and ARGS are the template arguments to use. */
18003 tsubst_enum (tree tag
, tree newtag
, tree args
)
18007 if (SCOPED_ENUM_P (newtag
))
18008 begin_scope (sk_scoped_enum
, newtag
);
18010 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
18015 decl
= TREE_VALUE (e
);
18016 /* Note that in a template enum, the TREE_VALUE is the
18017 CONST_DECL, not the corresponding INTEGER_CST. */
18018 value
= tsubst_expr (DECL_INITIAL (decl
),
18019 args
, tf_warning_or_error
, NULL_TREE
,
18020 /*integral_constant_expression_p=*/true);
18022 /* Give this enumeration constant the correct access. */
18023 set_current_access_from_decl (decl
);
18025 /* Actually build the enumerator itself. */
18027 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
18030 if (SCOPED_ENUM_P (newtag
))
18033 finish_enum_value_list (newtag
);
18034 finish_enum (newtag
);
18036 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
18037 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
18040 /* DECL is a FUNCTION_DECL that is a template specialization. Return
18041 its type -- but without substituting the innermost set of template
18042 arguments. So, innermost set of template parameters will appear in
18046 get_mostly_instantiated_function_type (tree decl
)
18054 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
18055 targs
= DECL_TI_ARGS (decl
);
18056 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
18057 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
18059 /* There should be as many levels of arguments as there are levels
18061 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
18063 fn_type
= TREE_TYPE (tmpl
);
18065 if (parm_depth
== 1)
18066 /* No substitution is necessary. */
18073 /* Replace the innermost level of the TARGS with NULL_TREEs to
18074 let tsubst know not to substitute for those parameters. */
18075 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
18076 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
18077 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
18078 TMPL_ARGS_LEVEL (targs
, i
));
18079 SET_TMPL_ARGS_LEVEL (partial_args
,
18080 TMPL_ARGS_DEPTH (targs
),
18081 make_tree_vec (DECL_NTPARMS (tmpl
)));
18083 /* Disable access control as this function is used only during
18085 push_deferring_access_checks (dk_no_check
);
18087 ++processing_template_decl
;
18088 /* Now, do the (partial) substitution to figure out the
18089 appropriate function type. */
18090 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
18091 --processing_template_decl
;
18093 /* Substitute into the template parameters to obtain the real
18094 innermost set of parameters. This step is important if the
18095 innermost set of template parameters contains value
18096 parameters whose types depend on outer template parameters. */
18097 TREE_VEC_LENGTH (partial_args
)--;
18098 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
18100 pop_deferring_access_checks ();
18106 /* Return truthvalue if we're processing a template different from
18107 the last one involved in diagnostics. */
18109 problematic_instantiation_changed (void)
18111 return last_template_error_tick
!= tinst_level_tick
;
18114 /* Remember current template involved in diagnostics. */
18116 record_last_problematic_instantiation (void)
18118 last_template_error_tick
= tinst_level_tick
;
18121 struct tinst_level
*
18122 current_instantiation (void)
18124 return current_tinst_level
;
18127 /* [temp.param] Check that template non-type parm TYPE is of an allowable
18128 type. Return zero for ok, nonzero for disallowed. Issue error and
18129 warning messages under control of COMPLAIN. */
18132 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
18134 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
18136 else if (POINTER_TYPE_P (type
))
18138 else if (TYPE_PTR_TO_MEMBER_P (type
))
18140 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
18142 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
18144 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
18147 if (complain
& tf_error
)
18148 error ("%q#T is not a valid type for a template constant parameter", type
);
18152 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
18153 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
18156 dependent_type_p_r (tree type
)
18162 A type is dependent if it is:
18164 -- a template parameter. Template template parameters are types
18165 for us (since TYPE_P holds true for them) so we handle
18167 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
18168 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
18170 /* -- a qualified-id with a nested-name-specifier which contains a
18171 class-name that names a dependent type or whose unqualified-id
18172 names a dependent type. */
18173 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18175 /* -- a cv-qualified type where the cv-unqualified type is
18177 type
= TYPE_MAIN_VARIANT (type
);
18178 /* -- a compound type constructed from any dependent type. */
18179 if (TYPE_PTR_TO_MEMBER_P (type
))
18180 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
18181 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
18183 else if (TREE_CODE (type
) == POINTER_TYPE
18184 || TREE_CODE (type
) == REFERENCE_TYPE
)
18185 return dependent_type_p (TREE_TYPE (type
));
18186 else if (TREE_CODE (type
) == FUNCTION_TYPE
18187 || TREE_CODE (type
) == METHOD_TYPE
)
18191 if (dependent_type_p (TREE_TYPE (type
)))
18193 for (arg_type
= TYPE_ARG_TYPES (type
);
18195 arg_type
= TREE_CHAIN (arg_type
))
18196 if (dependent_type_p (TREE_VALUE (arg_type
)))
18200 /* -- an array type constructed from any dependent type or whose
18201 size is specified by a constant expression that is
18204 We checked for type- and value-dependence of the bounds in
18205 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
18206 if (TREE_CODE (type
) == ARRAY_TYPE
)
18208 if (TYPE_DOMAIN (type
)
18209 && dependent_type_p (TYPE_DOMAIN (type
)))
18211 return dependent_type_p (TREE_TYPE (type
));
18214 /* -- a template-id in which either the template name is a template
18216 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
18218 /* ... or any of the template arguments is a dependent type or
18219 an expression that is type-dependent or value-dependent. */
18220 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
18221 && (any_dependent_template_arguments_p
18222 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
18225 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
18226 dependent; if the argument of the `typeof' expression is not
18227 type-dependent, then it should already been have resolved. */
18228 if (TREE_CODE (type
) == TYPEOF_TYPE
18229 || TREE_CODE (type
) == DECLTYPE_TYPE
18230 || TREE_CODE (type
) == UNDERLYING_TYPE
)
18233 /* A template argument pack is dependent if any of its packed
18235 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
18237 tree args
= ARGUMENT_PACK_ARGS (type
);
18238 int i
, len
= TREE_VEC_LENGTH (args
);
18239 for (i
= 0; i
< len
; ++i
)
18240 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
18244 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
18245 be template parameters. */
18246 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
18249 /* The standard does not specifically mention types that are local
18250 to template functions or local classes, but they should be
18251 considered dependent too. For example:
18253 template <int I> void f() {
18258 The size of `E' cannot be known until the value of `I' has been
18259 determined. Therefore, `E' must be considered dependent. */
18260 scope
= TYPE_CONTEXT (type
);
18261 if (scope
&& TYPE_P (scope
))
18262 return dependent_type_p (scope
);
18263 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
18264 return type_dependent_expression_p (scope
);
18266 /* Other types are non-dependent. */
18270 /* Returns TRUE if TYPE is dependent, in the sense of
18271 [temp.dep.type]. Note that a NULL type is considered dependent. */
18274 dependent_type_p (tree type
)
18276 /* If there are no template parameters in scope, then there can't be
18277 any dependent types. */
18278 if (!processing_template_decl
)
18280 /* If we are not processing a template, then nobody should be
18281 providing us with a dependent type. */
18283 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
18287 /* If the type is NULL, we have not computed a type for the entity
18288 in question; in that case, the type is dependent. */
18292 /* Erroneous types can be considered non-dependent. */
18293 if (type
== error_mark_node
)
18296 /* If we have not already computed the appropriate value for TYPE,
18298 if (!TYPE_DEPENDENT_P_VALID (type
))
18300 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
18301 TYPE_DEPENDENT_P_VALID (type
) = 1;
18304 return TYPE_DEPENDENT_P (type
);
18307 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
18308 lookup. In other words, a dependent type that is not the current
18312 dependent_scope_p (tree scope
)
18314 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
18315 && !currently_open_class (scope
));
18318 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
18319 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
18322 /* Note that this predicate is not appropriate for general expressions;
18323 only constant expressions (that satisfy potential_constant_expression)
18324 can be tested for value dependence.
18326 We should really also have a predicate for "instantiation-dependent".
18328 fold_non_dependent_expr: fold if constant and not type-dependent and not value-dependent.
18329 (what about instantiation-dependent constant-expressions?)
18330 is_late_template_attribute: defer if instantiation-dependent.
18331 compute_array_index_type: proceed if constant and not t- or v-dependent
18332 if instantiation-dependent, need to remember full expression
18333 uses_template_parms: FIXME - need to audit callers
18334 tsubst_decl [function_decl]: Why is this using value_dependent_expression_p?
18335 dependent_type_p [array_type]: dependent if index type is dependent
18337 static_assert - instantiation-dependent */
18340 value_dependent_expression_p (tree expression
)
18342 if (!processing_template_decl
)
18345 /* A name declared with a dependent type. */
18346 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
18349 switch (TREE_CODE (expression
))
18351 case IDENTIFIER_NODE
:
18352 /* A name that has not been looked up -- must be dependent. */
18355 case TEMPLATE_PARM_INDEX
:
18356 /* A non-type template parm. */
18360 /* A non-type template parm. */
18361 if (DECL_TEMPLATE_PARM_P (expression
))
18363 return value_dependent_expression_p (DECL_INITIAL (expression
));
18366 /* A constant with literal type and is initialized
18367 with an expression that is value-dependent. */
18368 if (DECL_INITIAL (expression
)
18369 && decl_constant_var_p (expression
)
18370 && value_dependent_expression_p (DECL_INITIAL (expression
)))
18374 case DYNAMIC_CAST_EXPR
:
18375 case STATIC_CAST_EXPR
:
18376 case CONST_CAST_EXPR
:
18377 case REINTERPRET_CAST_EXPR
:
18379 /* These expressions are value-dependent if the type to which
18380 the cast occurs is dependent or the expression being casted
18381 is value-dependent. */
18383 tree type
= TREE_TYPE (expression
);
18385 if (dependent_type_p (type
))
18388 /* A functional cast has a list of operands. */
18389 expression
= TREE_OPERAND (expression
, 0);
18392 /* If there are no operands, it must be an expression such
18393 as "int()". This should not happen for aggregate types
18394 because it would form non-constant expressions. */
18395 gcc_assert (cxx_dialect
>= cxx0x
18396 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
18401 if (TREE_CODE (expression
) == TREE_LIST
)
18402 return any_value_dependent_elements_p (expression
);
18404 return value_dependent_expression_p (expression
);
18410 /* A `sizeof' expression is value-dependent if the operand is
18411 type-dependent or is a pack expansion. */
18412 expression
= TREE_OPERAND (expression
, 0);
18413 if (PACK_EXPANSION_P (expression
))
18415 else if (TYPE_P (expression
))
18416 return dependent_type_p (expression
);
18417 return type_dependent_expression_p (expression
);
18419 case AT_ENCODE_EXPR
:
18420 /* An 'encode' expression is value-dependent if the operand is
18422 expression
= TREE_OPERAND (expression
, 0);
18423 return dependent_type_p (expression
);
18425 case NOEXCEPT_EXPR
:
18426 expression
= TREE_OPERAND (expression
, 0);
18427 return type_dependent_expression_p (expression
);
18431 tree name
= TREE_OPERAND (expression
, 1);
18432 return value_dependent_expression_p (name
);
18435 case COMPONENT_REF
:
18436 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
18437 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
18439 case NONTYPE_ARGUMENT_PACK
:
18440 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
18441 is value-dependent. */
18443 tree values
= ARGUMENT_PACK_ARGS (expression
);
18444 int i
, len
= TREE_VEC_LENGTH (values
);
18446 for (i
= 0; i
< len
; ++i
)
18447 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
18455 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
18456 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
18457 || (type2
? dependent_type_p (type2
) : false));
18461 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
18462 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
18465 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
18466 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
18470 tree op
= TREE_OPERAND (expression
, 0);
18471 return (value_dependent_expression_p (op
)
18472 || has_value_dependent_address (op
));
18477 tree fn
= get_callee_fndecl (expression
);
18479 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
18481 nargs
= call_expr_nargs (expression
);
18482 for (i
= 0; i
< nargs
; ++i
)
18484 tree op
= CALL_EXPR_ARG (expression
, i
);
18485 /* In a call to a constexpr member function, look through the
18486 implicit ADDR_EXPR on the object argument so that it doesn't
18487 cause the call to be considered value-dependent. We also
18488 look through it in potential_constant_expression. */
18489 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
18490 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
18491 && TREE_CODE (op
) == ADDR_EXPR
)
18492 op
= TREE_OPERAND (op
, 0);
18493 if (value_dependent_expression_p (op
))
18499 case TEMPLATE_ID_EXPR
:
18500 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
18502 return type_dependent_expression_p (expression
);
18508 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
18509 if (value_dependent_expression_p (val
))
18515 /* A constant expression is value-dependent if any subexpression is
18516 value-dependent. */
18517 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
18519 case tcc_reference
:
18521 case tcc_comparison
:
18523 case tcc_expression
:
18526 int i
, len
= cp_tree_operand_length (expression
);
18528 for (i
= 0; i
< len
; i
++)
18530 tree t
= TREE_OPERAND (expression
, i
);
18532 /* In some cases, some of the operands may be missing.l
18533 (For example, in the case of PREDECREMENT_EXPR, the
18534 amount to increment by may be missing.) That doesn't
18535 make the expression dependent. */
18536 if (t
&& value_dependent_expression_p (t
))
18547 /* The expression is not value-dependent. */
18551 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
18552 [temp.dep.expr]. Note that an expression with no type is
18553 considered dependent. Other parts of the compiler arrange for an
18554 expression with type-dependent subexpressions to have no type, so
18555 this function doesn't have to be fully recursive. */
18558 type_dependent_expression_p (tree expression
)
18560 if (!processing_template_decl
)
18563 if (expression
== error_mark_node
)
18566 /* An unresolved name is always dependent. */
18567 if (TREE_CODE (expression
) == IDENTIFIER_NODE
18568 || TREE_CODE (expression
) == USING_DECL
)
18571 /* Some expression forms are never type-dependent. */
18572 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
18573 || TREE_CODE (expression
) == SIZEOF_EXPR
18574 || TREE_CODE (expression
) == ALIGNOF_EXPR
18575 || TREE_CODE (expression
) == AT_ENCODE_EXPR
18576 || TREE_CODE (expression
) == NOEXCEPT_EXPR
18577 || TREE_CODE (expression
) == TRAIT_EXPR
18578 || TREE_CODE (expression
) == TYPEID_EXPR
18579 || TREE_CODE (expression
) == DELETE_EXPR
18580 || TREE_CODE (expression
) == VEC_DELETE_EXPR
18581 || TREE_CODE (expression
) == THROW_EXPR
)
18584 /* The types of these expressions depends only on the type to which
18585 the cast occurs. */
18586 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
18587 || TREE_CODE (expression
) == STATIC_CAST_EXPR
18588 || TREE_CODE (expression
) == CONST_CAST_EXPR
18589 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
18590 || TREE_CODE (expression
) == CAST_EXPR
)
18591 return dependent_type_p (TREE_TYPE (expression
));
18593 /* The types of these expressions depends only on the type created
18594 by the expression. */
18595 if (TREE_CODE (expression
) == NEW_EXPR
18596 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
18598 /* For NEW_EXPR tree nodes created inside a template, either
18599 the object type itself or a TREE_LIST may appear as the
18601 tree type
= TREE_OPERAND (expression
, 1);
18602 if (TREE_CODE (type
) == TREE_LIST
)
18603 /* This is an array type. We need to check array dimensions
18605 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
18606 || value_dependent_expression_p
18607 (TREE_OPERAND (TREE_VALUE (type
), 1));
18609 return dependent_type_p (type
);
18612 if (TREE_CODE (expression
) == SCOPE_REF
)
18614 tree scope
= TREE_OPERAND (expression
, 0);
18615 tree name
= TREE_OPERAND (expression
, 1);
18617 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
18618 contains an identifier associated by name lookup with one or more
18619 declarations declared with a dependent type, or...a
18620 nested-name-specifier or qualified-id that names a member of an
18621 unknown specialization. */
18622 return (type_dependent_expression_p (name
)
18623 || dependent_scope_p (scope
));
18626 if (TREE_CODE (expression
) == FUNCTION_DECL
18627 && DECL_LANG_SPECIFIC (expression
)
18628 && DECL_TEMPLATE_INFO (expression
)
18629 && (any_dependent_template_arguments_p
18630 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
18633 if (TREE_CODE (expression
) == TEMPLATE_DECL
18634 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
18637 if (TREE_CODE (expression
) == STMT_EXPR
)
18638 expression
= stmt_expr_value_expr (expression
);
18640 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
18645 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
18647 if (type_dependent_expression_p (elt
))
18653 /* A static data member of the current instantiation with incomplete
18654 array type is type-dependent, as the definition and specializations
18655 can have different bounds. */
18656 if (TREE_CODE (expression
) == VAR_DECL
18657 && DECL_CLASS_SCOPE_P (expression
)
18658 && dependent_type_p (DECL_CONTEXT (expression
))
18659 && VAR_HAD_UNKNOWN_BOUND (expression
))
18662 if (TREE_TYPE (expression
) == unknown_type_node
)
18664 if (TREE_CODE (expression
) == ADDR_EXPR
)
18665 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
18666 if (TREE_CODE (expression
) == COMPONENT_REF
18667 || TREE_CODE (expression
) == OFFSET_REF
)
18669 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
18671 expression
= TREE_OPERAND (expression
, 1);
18672 if (TREE_CODE (expression
) == IDENTIFIER_NODE
)
18675 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
18676 if (TREE_CODE (expression
) == SCOPE_REF
)
18679 if (TREE_CODE (expression
) == BASELINK
)
18680 expression
= BASELINK_FUNCTIONS (expression
);
18682 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
18684 if (any_dependent_template_arguments_p
18685 (TREE_OPERAND (expression
, 1)))
18687 expression
= TREE_OPERAND (expression
, 0);
18689 gcc_assert (TREE_CODE (expression
) == OVERLOAD
18690 || TREE_CODE (expression
) == FUNCTION_DECL
);
18694 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
18696 expression
= OVL_NEXT (expression
);
18701 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
18703 return (dependent_type_p (TREE_TYPE (expression
)));
18706 /* Like type_dependent_expression_p, but it also works while not processing
18707 a template definition, i.e. during substitution or mangling. */
18710 type_dependent_expression_p_push (tree expr
)
18713 ++processing_template_decl
;
18714 b
= type_dependent_expression_p (expr
);
18715 --processing_template_decl
;
18719 /* Returns TRUE if ARGS contains a type-dependent expression. */
18722 any_type_dependent_arguments_p (const VEC(tree
,gc
) *args
)
18727 FOR_EACH_VEC_ELT (tree
, args
, i
, arg
)
18729 if (type_dependent_expression_p (arg
))
18735 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
18736 expressions) contains any type-dependent expressions. */
18739 any_type_dependent_elements_p (const_tree list
)
18741 for (; list
; list
= TREE_CHAIN (list
))
18742 if (value_dependent_expression_p (TREE_VALUE (list
)))
18748 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
18749 expressions) contains any value-dependent expressions. */
18752 any_value_dependent_elements_p (const_tree list
)
18754 for (; list
; list
= TREE_CHAIN (list
))
18755 if (value_dependent_expression_p (TREE_VALUE (list
)))
18761 /* Returns TRUE if the ARG (a template argument) is dependent. */
18764 dependent_template_arg_p (tree arg
)
18766 if (!processing_template_decl
)
18769 /* Assume a template argument that was wrongly written by the user
18770 is dependent. This is consistent with what
18771 any_dependent_template_arguments_p [that calls this function]
18773 if (arg
== error_mark_node
)
18776 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
18777 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
18779 if (TREE_CODE (arg
) == TEMPLATE_DECL
18780 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
18781 return dependent_template_p (arg
);
18782 else if (ARGUMENT_PACK_P (arg
))
18784 tree args
= ARGUMENT_PACK_ARGS (arg
);
18785 int i
, len
= TREE_VEC_LENGTH (args
);
18786 for (i
= 0; i
< len
; ++i
)
18788 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
18794 else if (TYPE_P (arg
))
18795 return dependent_type_p (arg
);
18797 return (type_dependent_expression_p (arg
)
18798 || value_dependent_expression_p (arg
));
18801 /* Returns true if ARGS (a collection of template arguments) contains
18802 any types that require structural equality testing. */
18805 any_template_arguments_need_structural_equality_p (tree args
)
18812 if (args
== error_mark_node
)
18815 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
18817 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
18818 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
18820 tree arg
= TREE_VEC_ELT (level
, j
);
18821 tree packed_args
= NULL_TREE
;
18824 if (ARGUMENT_PACK_P (arg
))
18826 /* Look inside the argument pack. */
18827 packed_args
= ARGUMENT_PACK_ARGS (arg
);
18828 len
= TREE_VEC_LENGTH (packed_args
);
18831 for (k
= 0; k
< len
; ++k
)
18834 arg
= TREE_VEC_ELT (packed_args
, k
);
18836 if (error_operand_p (arg
))
18838 else if (TREE_CODE (arg
) == TEMPLATE_DECL
18839 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
18841 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
18843 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
18844 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
18853 /* Returns true if ARGS (a collection of template arguments) contains
18854 any dependent arguments. */
18857 any_dependent_template_arguments_p (const_tree args
)
18864 if (args
== error_mark_node
)
18867 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
18869 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
18870 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
18871 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
18878 /* Returns TRUE if the template TMPL is dependent. */
18881 dependent_template_p (tree tmpl
)
18883 if (TREE_CODE (tmpl
) == OVERLOAD
)
18887 if (dependent_template_p (OVL_CURRENT (tmpl
)))
18889 tmpl
= OVL_NEXT (tmpl
);
18894 /* Template template parameters are dependent. */
18895 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
18896 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
18898 /* So are names that have not been looked up. */
18899 if (TREE_CODE (tmpl
) == SCOPE_REF
18900 || TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
18902 /* So are member templates of dependent classes. */
18903 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
18904 return dependent_type_p (DECL_CONTEXT (tmpl
));
18908 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
18911 dependent_template_id_p (tree tmpl
, tree args
)
18913 return (dependent_template_p (tmpl
)
18914 || any_dependent_template_arguments_p (args
));
18917 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
18921 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
18925 if (!processing_template_decl
)
18928 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
18930 tree decl
= TREE_VEC_ELT (declv
, i
);
18931 tree init
= TREE_VEC_ELT (initv
, i
);
18932 tree cond
= TREE_VEC_ELT (condv
, i
);
18933 tree incr
= TREE_VEC_ELT (incrv
, i
);
18935 if (type_dependent_expression_p (decl
))
18938 if (init
&& type_dependent_expression_p (init
))
18941 if (type_dependent_expression_p (cond
))
18944 if (COMPARISON_CLASS_P (cond
)
18945 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
18946 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
18949 if (TREE_CODE (incr
) == MODOP_EXPR
)
18951 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
18952 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
18955 else if (type_dependent_expression_p (incr
))
18957 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
18959 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
18961 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
18963 tree t
= TREE_OPERAND (incr
, 1);
18964 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
18965 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
18974 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
18975 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
18976 no such TYPE can be found. Note that this function peers inside
18977 uninstantiated templates and therefore should be used only in
18978 extremely limited situations. ONLY_CURRENT_P restricts this
18979 peering to the currently open classes hierarchy (which is required
18980 when comparing types). */
18983 resolve_typename_type (tree type
, bool only_current_p
)
18992 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
18994 scope
= TYPE_CONTEXT (type
);
18995 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
18996 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
18997 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
18998 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
18999 identifier of the TYPENAME_TYPE anymore.
19000 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
19001 TYPENAME_TYPE instead, we avoid messing up with a possible
19002 typedef variant case. */
19003 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
19005 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
19006 it first before we can figure out what NAME refers to. */
19007 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
19008 scope
= resolve_typename_type (scope
, only_current_p
);
19009 /* If we don't know what SCOPE refers to, then we cannot resolve the
19011 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
19013 /* If the SCOPE is a template type parameter, we have no way of
19014 resolving the name. */
19015 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
19017 /* If the SCOPE is not the current instantiation, there's no reason
19018 to look inside it. */
19019 if (only_current_p
&& !currently_open_class (scope
))
19021 /* If this is a typedef, we don't want to look inside (c++/11987). */
19022 if (typedef_variant_p (type
))
19024 /* If SCOPE isn't the template itself, it will not have a valid
19025 TYPE_FIELDS list. */
19026 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
19027 /* scope is either the template itself or a compatible instantiation
19028 like X<T>, so look up the name in the original template. */
19029 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
19031 /* scope is a partial instantiation, so we can't do the lookup or we
19032 will lose the template arguments. */
19034 /* Enter the SCOPE so that name lookup will be resolved as if we
19035 were in the class definition. In particular, SCOPE will no
19036 longer be considered a dependent type. */
19037 pushed_scope
= push_scope (scope
);
19038 /* Look up the declaration. */
19039 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true);
19041 result
= NULL_TREE
;
19043 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
19044 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
19047 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == IDENTIFIER_NODE
19048 && TREE_CODE (decl
) == TYPE_DECL
)
19050 result
= TREE_TYPE (decl
);
19051 if (result
== error_mark_node
)
19052 result
= NULL_TREE
;
19054 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
19055 && DECL_CLASS_TEMPLATE_P (decl
))
19059 /* Obtain the template and the arguments. */
19060 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
19061 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
19062 /* Instantiate the template. */
19063 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
19064 /*entering_scope=*/0,
19065 tf_error
| tf_user
);
19066 if (result
== error_mark_node
)
19067 result
= NULL_TREE
;
19070 /* Leave the SCOPE. */
19072 pop_scope (pushed_scope
);
19074 /* If we failed to resolve it, return the original typename. */
19078 /* If lookup found a typename type, resolve that too. */
19079 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
19081 /* Ill-formed programs can cause infinite recursion here, so we
19082 must catch that. */
19083 TYPENAME_IS_RESOLVING_P (type
) = 1;
19084 result
= resolve_typename_type (result
, only_current_p
);
19085 TYPENAME_IS_RESOLVING_P (type
) = 0;
19088 /* Qualify the resulting type. */
19089 quals
= cp_type_quals (type
);
19091 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
19096 /* EXPR is an expression which is not type-dependent. Return a proxy
19097 for EXPR that can be used to compute the types of larger
19098 expressions containing EXPR. */
19101 build_non_dependent_expr (tree expr
)
19105 #ifdef ENABLE_CHECKING
19106 /* Try to get a constant value for all non-type-dependent expressions in
19107 order to expose bugs in *_dependent_expression_p and constexpr. */
19108 if (cxx_dialect
>= cxx0x
)
19109 maybe_constant_value (fold_non_dependent_expr (expr
));
19112 /* Preserve OVERLOADs; the functions must be available to resolve
19115 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
19116 inner_expr
= stmt_expr_value_expr (inner_expr
);
19117 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
19118 inner_expr
= TREE_OPERAND (inner_expr
, 0);
19119 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
19120 inner_expr
= TREE_OPERAND (inner_expr
, 1);
19121 if (is_overloaded_fn (inner_expr
)
19122 || TREE_CODE (inner_expr
) == OFFSET_REF
)
19124 /* There is no need to return a proxy for a variable. */
19125 if (TREE_CODE (expr
) == VAR_DECL
)
19127 /* Preserve string constants; conversions from string constants to
19128 "char *" are allowed, even though normally a "const char *"
19129 cannot be used to initialize a "char *". */
19130 if (TREE_CODE (expr
) == STRING_CST
)
19132 /* Preserve arithmetic constants, as an optimization -- there is no
19133 reason to create a new node. */
19134 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
19136 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
19137 There is at least one place where we want to know that a
19138 particular expression is a throw-expression: when checking a ?:
19139 expression, there are special rules if the second or third
19140 argument is a throw-expression. */
19141 if (TREE_CODE (expr
) == THROW_EXPR
)
19144 if (TREE_CODE (expr
) == COND_EXPR
)
19145 return build3 (COND_EXPR
,
19147 TREE_OPERAND (expr
, 0),
19148 (TREE_OPERAND (expr
, 1)
19149 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
19150 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
19151 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
19152 if (TREE_CODE (expr
) == COMPOUND_EXPR
19153 && !COMPOUND_EXPR_OVERLOADED (expr
))
19154 return build2 (COMPOUND_EXPR
,
19156 TREE_OPERAND (expr
, 0),
19157 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
19159 /* Keep dereferences outside the NON_DEPENDENT_EXPR so lvalue_kind
19160 doesn't need to look inside. */
19161 if (REFERENCE_REF_P (expr
))
19162 return convert_from_reference (build_non_dependent_expr
19163 (TREE_OPERAND (expr
, 0)));
19165 /* If the type is unknown, it can't really be non-dependent */
19166 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
19168 /* Otherwise, build a NON_DEPENDENT_EXPR. */
19169 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
19172 /* ARGS is a vector of expressions as arguments to a function call.
19173 Replace the arguments with equivalent non-dependent expressions.
19174 This modifies ARGS in place. */
19177 make_args_non_dependent (VEC(tree
,gc
) *args
)
19182 FOR_EACH_VEC_ELT (tree
, args
, ix
, arg
)
19184 tree newarg
= build_non_dependent_expr (arg
);
19186 VEC_replace (tree
, args
, ix
, newarg
);
19190 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
19191 with a level one deeper than the actual template parms. */
19196 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
19197 TYPE_NAME (au
) = build_decl (BUILTINS_LOCATION
,
19198 TYPE_DECL
, get_identifier ("auto"), au
);
19199 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
19200 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
19201 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
19202 0, TYPE_NAME (au
), NULL_TREE
);
19203 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
19204 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
19205 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
19210 /* Given type ARG, return std::initializer_list<ARG>. */
19215 tree std_init_list
= namespace_binding
19216 (get_identifier ("initializer_list"), std_node
);
19218 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
19220 error ("deducing from brace-enclosed initializer list requires "
19221 "#include <initializer_list>");
19222 return error_mark_node
;
19224 argvec
= make_tree_vec (1);
19225 TREE_VEC_ELT (argvec
, 0) = arg
;
19226 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
19227 NULL_TREE
, 0, tf_warning_or_error
);
19230 /* Replace auto in TYPE with std::initializer_list<auto>. */
19233 listify_autos (tree type
, tree auto_node
)
19235 tree init_auto
= listify (auto_node
);
19236 tree argvec
= make_tree_vec (1);
19237 TREE_VEC_ELT (argvec
, 0) = init_auto
;
19238 if (processing_template_decl
)
19239 argvec
= add_to_template_args (current_template_args (), argvec
);
19240 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
19243 /* walk_tree helper for do_auto_deduction. */
19246 contains_auto_r (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
19249 /* Is this a variable with the type we're looking for? */
19251 && TREE_TYPE (*tp
) == type
)
19257 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
19258 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
19261 do_auto_deduction (tree type
, tree init
, tree auto_node
)
19263 tree parms
, tparms
, targs
;
19268 /* The name of the object being declared shall not appear in the
19269 initializer expression. */
19270 decl
= cp_walk_tree_without_duplicates (&init
, contains_auto_r
, type
);
19273 error ("variable %q#D with %<auto%> type used in its own "
19274 "initializer", decl
);
19275 return error_mark_node
;
19278 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
19279 with either a new invented type template parameter U or, if the
19280 initializer is a braced-init-list (8.5.4), with
19281 std::initializer_list<U>. */
19282 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
19283 type
= listify_autos (type
, auto_node
);
19285 init
= resolve_nondeduced_context (init
);
19287 parms
= build_tree_list (NULL_TREE
, type
);
19289 tparms
= make_tree_vec (1);
19290 targs
= make_tree_vec (1);
19291 TREE_VEC_ELT (tparms
, 0)
19292 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
19293 val
= type_unification_real (tparms
, targs
, parms
, args
, 1, 0,
19294 DEDUCE_CALL
, LOOKUP_NORMAL
);
19297 if (type
&& type
!= error_mark_node
)
19298 /* If type is error_mark_node a diagnostic must have been
19299 emitted by now. Also, having a mention to '<type error>'
19300 in the diagnostic is not really useful to the user. */
19301 error ("unable to deduce %qT from %qE", type
, init
);
19302 return error_mark_node
;
19305 /* If the list of declarators contains more than one declarator, the type
19306 of each declared variable is determined as described above. If the
19307 type deduced for the template parameter U is not the same in each
19308 deduction, the program is ill-formed. */
19309 if (TREE_TYPE (auto_node
)
19310 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
19312 error ("inconsistent deduction for %qT: %qT and then %qT",
19313 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
19314 return error_mark_node
;
19316 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
19318 if (processing_template_decl
)
19319 targs
= add_to_template_args (current_template_args (), targs
);
19320 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
19323 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
19327 splice_late_return_type (tree type
, tree late_return_type
)
19331 if (late_return_type
== NULL_TREE
)
19333 argvec
= make_tree_vec (1);
19334 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
19335 if (processing_template_parmlist
)
19336 /* For a late-specified return type in a template type-parameter, we
19337 need to add a dummy argument level for its parmlist. */
19338 argvec
= add_to_template_args
19339 (make_tree_vec (processing_template_parmlist
), argvec
);
19340 if (current_template_parms
)
19341 argvec
= add_to_template_args (current_template_args (), argvec
);
19342 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
19345 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
19348 is_auto (const_tree type
)
19350 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
19351 && TYPE_IDENTIFIER (type
) == get_identifier ("auto"))
19357 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
19358 appear as a type-specifier for the declaration in question, we don't
19359 have to look through the whole type. */
19362 type_uses_auto (tree type
)
19364 enum tree_code code
;
19365 if (is_auto (type
))
19368 code
= TREE_CODE (type
);
19370 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
19371 || code
== OFFSET_TYPE
|| code
== FUNCTION_TYPE
19372 || code
== METHOD_TYPE
|| code
== ARRAY_TYPE
)
19373 return type_uses_auto (TREE_TYPE (type
));
19375 if (TYPE_PTRMEMFUNC_P (type
))
19376 return type_uses_auto (TREE_TYPE (TREE_TYPE
19377 (TYPE_PTRMEMFUNC_FN_TYPE (type
))));
19382 /* For a given template T, return the vector of typedefs referenced
19383 in T for which access check is needed at T instantiation time.
19384 T is either a FUNCTION_DECL or a RECORD_TYPE.
19385 Those typedefs were added to T by the function
19386 append_type_to_template_for_access_check. */
19388 VEC(qualified_typedef_usage_t
,gc
)*
19389 get_types_needing_access_check (tree t
)
19392 VEC(qualified_typedef_usage_t
,gc
) *result
= NULL
;
19394 if (!t
|| t
== error_mark_node
)
19397 if (!(ti
= get_template_info (t
)))
19400 if (CLASS_TYPE_P (t
)
19401 || TREE_CODE (t
) == FUNCTION_DECL
)
19403 if (!TI_TEMPLATE (ti
))
19406 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
19412 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
19413 tied to T. That list of typedefs will be access checked at
19414 T instantiation time.
19415 T is either a FUNCTION_DECL or a RECORD_TYPE.
19416 TYPE_DECL is a TYPE_DECL node representing a typedef.
19417 SCOPE is the scope through which TYPE_DECL is accessed.
19418 LOCATION is the location of the usage point of TYPE_DECL.
19420 This function is a subroutine of
19421 append_type_to_template_for_access_check. */
19424 append_type_to_template_for_access_check_1 (tree t
,
19427 location_t location
)
19429 qualified_typedef_usage_t typedef_usage
;
19432 if (!t
|| t
== error_mark_node
)
19435 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
19436 || CLASS_TYPE_P (t
))
19438 && TREE_CODE (type_decl
) == TYPE_DECL
19441 if (!(ti
= get_template_info (t
)))
19444 gcc_assert (TI_TEMPLATE (ti
));
19446 typedef_usage
.typedef_decl
= type_decl
;
19447 typedef_usage
.context
= scope
;
19448 typedef_usage
.locus
= location
;
19450 VEC_safe_push (qualified_typedef_usage_t
, gc
,
19451 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
),
19455 /* Append TYPE_DECL to the template TEMPL.
19456 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
19457 At TEMPL instanciation time, TYPE_DECL will be checked to see
19458 if it can be accessed through SCOPE.
19459 LOCATION is the location of the usage point of TYPE_DECL.
19461 e.g. consider the following code snippet:
19468 template<class U> struct S
19470 C::myint mi; // <-- usage point of the typedef C::myint
19475 At S<char> instantiation time, we need to check the access of C::myint
19476 In other words, we need to check the access of the myint typedef through
19477 the C scope. For that purpose, this function will add the myint typedef
19478 and the scope C through which its being accessed to a list of typedefs
19479 tied to the template S. That list will be walked at template instantiation
19480 time and access check performed on each typedefs it contains.
19481 Note that this particular code snippet should yield an error because
19482 myint is private to C. */
19485 append_type_to_template_for_access_check (tree templ
,
19488 location_t location
)
19490 qualified_typedef_usage_t
*iter
;
19493 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
19495 /* Make sure we don't append the type to the template twice. */
19496 FOR_EACH_VEC_ELT (qualified_typedef_usage_t
,
19497 get_types_needing_access_check (templ
),
19499 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
19502 append_type_to_template_for_access_check_1 (templ
, type_decl
,
19506 /* Set up the hash tables for template instantiations. */
19509 init_template_processing (void)
19511 decl_specializations
= htab_create_ggc (37,
19512 hash_specialization
,
19513 eq_specializations
,
19515 type_specializations
= htab_create_ggc (37,
19516 hash_specialization
,
19517 eq_specializations
,
19521 /* Print stats about the template hash tables for -fstats. */
19524 print_template_statistics (void)
19526 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
19527 "%f collisions\n", (long) htab_size (decl_specializations
),
19528 (long) htab_elements (decl_specializations
),
19529 htab_collisions (decl_specializations
));
19530 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
19531 "%f collisions\n", (long) htab_size (type_specializations
),
19532 (long) htab_elements (type_specializations
),
19533 htab_collisions (type_specializations
));
19534 if (current_deduction_htab
)
19535 fprintf (stderr
, "current_deduction_htab: size %ld, %ld elements, "
19536 "%f collisions\n", (long) htab_size (current_deduction_htab
),
19537 (long) htab_elements (current_deduction_htab
),
19538 htab_collisions (current_deduction_htab
));
19541 #include "gt-cp-pt.h"