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
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 "cp-objcp-common.h"
40 #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 (()) 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
);
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, 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
);
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 hashval_t
iterative_hash_template_arg (tree arg
, hashval_t val
);
192 static tree
listify (tree
);
193 static tree
listify_autos (tree
, tree
);
195 /* Make the current scope suitable for access checking when we are
196 processing T. T can be FUNCTION_DECL for instantiated function
197 template, or VAR_DECL for static member variable (need by
198 instantiate_decl). */
201 push_access_scope (tree t
)
203 gcc_assert (TREE_CODE (t
) == FUNCTION_DECL
204 || TREE_CODE (t
) == VAR_DECL
);
206 if (DECL_FRIEND_CONTEXT (t
))
207 push_nested_class (DECL_FRIEND_CONTEXT (t
));
208 else if (DECL_CLASS_SCOPE_P (t
))
209 push_nested_class (DECL_CONTEXT (t
));
211 push_to_top_level ();
213 if (TREE_CODE (t
) == FUNCTION_DECL
)
215 saved_access_scope
= tree_cons
216 (NULL_TREE
, current_function_decl
, saved_access_scope
);
217 current_function_decl
= t
;
221 /* Restore the scope set up by push_access_scope. T is the node we
225 pop_access_scope (tree t
)
227 if (TREE_CODE (t
) == FUNCTION_DECL
)
229 current_function_decl
= TREE_VALUE (saved_access_scope
);
230 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
233 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
236 pop_from_top_level ();
239 /* Do any processing required when DECL (a member template
240 declaration) is finished. Returns the TEMPLATE_DECL corresponding
241 to DECL, unless it is a specialization, in which case the DECL
242 itself is returned. */
245 finish_member_template_decl (tree decl
)
247 if (decl
== error_mark_node
)
248 return error_mark_node
;
250 gcc_assert (DECL_P (decl
));
252 if (TREE_CODE (decl
) == TYPE_DECL
)
256 type
= TREE_TYPE (decl
);
257 if (type
== error_mark_node
)
258 return error_mark_node
;
259 if (MAYBE_CLASS_TYPE_P (type
)
260 && CLASSTYPE_TEMPLATE_INFO (type
)
261 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
263 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
264 check_member_template (tmpl
);
269 else if (TREE_CODE (decl
) == FIELD_DECL
)
270 error ("data member %qD cannot be a member template", decl
);
271 else if (DECL_TEMPLATE_INFO (decl
))
273 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
275 check_member_template (DECL_TI_TEMPLATE (decl
));
276 return DECL_TI_TEMPLATE (decl
);
282 error ("invalid member template declaration %qD", decl
);
284 return error_mark_node
;
287 /* Create a template info node. */
290 build_template_info (tree template_decl
, tree template_args
)
292 tree result
= make_node (TEMPLATE_INFO
);
293 TI_TEMPLATE (result
) = template_decl
;
294 TI_ARGS (result
) = template_args
;
298 /* Return the template info node corresponding to T, whatever T is. */
301 get_template_info (const_tree t
)
303 tree tinfo
= NULL_TREE
;
305 if (!t
|| t
== error_mark_node
)
308 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
309 tinfo
= DECL_TEMPLATE_INFO (t
);
311 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
314 if (TAGGED_TYPE_P (t
))
315 tinfo
= TYPE_TEMPLATE_INFO (t
);
316 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
317 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
322 /* Returns the template nesting level of the indicated class TYPE.
332 A<T>::B<U> has depth two, while A<T> has depth one.
333 Both A<T>::B<int> and A<int>::B<U> have depth one, if
334 they are instantiations, not specializations.
336 This function is guaranteed to return 0 if passed NULL_TREE so
337 that, for example, `template_class_depth (current_class_type)' is
341 template_class_depth (tree type
)
346 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
347 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
348 ? CP_DECL_CONTEXT (type
) : TYPE_CONTEXT (type
))
350 tree tinfo
= get_template_info (type
);
352 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
353 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
360 /* Subroutine of maybe_begin_member_template_processing.
361 Returns true if processing DECL needs us to push template parms. */
364 inline_needs_template_parms (tree decl
)
366 if (! DECL_TEMPLATE_INFO (decl
))
369 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
370 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
373 /* Subroutine of maybe_begin_member_template_processing.
374 Push the template parms in PARMS, starting from LEVELS steps into the
375 chain, and ending at the beginning, since template parms are listed
379 push_inline_template_parms_recursive (tree parmlist
, int levels
)
381 tree parms
= TREE_VALUE (parmlist
);
385 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
387 ++processing_template_decl
;
388 current_template_parms
389 = tree_cons (size_int (processing_template_decl
),
390 parms
, current_template_parms
);
391 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
393 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
395 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
397 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
399 if (parm
== error_mark_node
)
402 gcc_assert (DECL_P (parm
));
404 switch (TREE_CODE (parm
))
413 /* Make a CONST_DECL as is done in process_template_parm.
414 It is ugly that we recreate this here; the original
415 version built in process_template_parm is no longer
417 tree decl
= build_decl (DECL_SOURCE_LOCATION (parm
),
418 CONST_DECL
, DECL_NAME (parm
),
420 DECL_ARTIFICIAL (decl
) = 1;
421 TREE_CONSTANT (decl
) = 1;
422 TREE_READONLY (decl
) = 1;
423 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
424 SET_DECL_TEMPLATE_PARM_P (decl
);
435 /* Restore the template parameter context for a member template or
436 a friend template defined in a class definition. */
439 maybe_begin_member_template_processing (tree decl
)
444 if (inline_needs_template_parms (decl
))
446 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
447 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
449 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
452 parms
= TREE_CHAIN (parms
);
455 push_inline_template_parms_recursive (parms
, levels
);
458 /* Remember how many levels of template parameters we pushed so that
459 we can pop them later. */
460 VEC_safe_push (int, heap
, inline_parm_levels
, levels
);
463 /* Undo the effects of maybe_begin_member_template_processing. */
466 maybe_end_member_template_processing (void)
471 if (VEC_length (int, inline_parm_levels
) == 0)
474 last
= VEC_pop (int, inline_parm_levels
);
475 for (i
= 0; i
< last
; ++i
)
477 --processing_template_decl
;
478 current_template_parms
= TREE_CHAIN (current_template_parms
);
483 /* Return a new template argument vector which contains all of ARGS,
484 but has as its innermost set of arguments the EXTRA_ARGS. */
487 add_to_template_args (tree args
, tree extra_args
)
494 if (args
== NULL_TREE
)
497 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
498 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
500 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
501 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
503 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
504 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
509 /* Like add_to_template_args, but only the outermost ARGS are added to
510 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
511 (EXTRA_ARGS) levels are added. This function is used to combine
512 the template arguments from a partial instantiation with the
513 template arguments used to attain the full instantiation from the
514 partial instantiation. */
517 add_outermost_template_args (tree args
, tree extra_args
)
521 /* If there are more levels of EXTRA_ARGS than there are ARGS,
522 something very fishy is going on. */
523 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
525 /* If *all* the new arguments will be the EXTRA_ARGS, just return
527 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
530 /* For the moment, we make ARGS look like it contains fewer levels. */
531 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
533 new_args
= add_to_template_args (args
, extra_args
);
535 /* Now, we restore ARGS to its full dimensions. */
536 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
541 /* Return the N levels of innermost template arguments from the ARGS. */
544 get_innermost_template_args (tree args
, int n
)
552 /* If N is 1, just return the innermost set of template arguments. */
554 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
556 /* If we're not removing anything, just return the arguments we were
558 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
559 gcc_assert (extra_levels
>= 0);
560 if (extra_levels
== 0)
563 /* Make a new set of arguments, not containing the outer arguments. */
564 new_args
= make_tree_vec (n
);
565 for (i
= 1; i
<= n
; ++i
)
566 SET_TMPL_ARGS_LEVEL (new_args
, i
,
567 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
572 /* The inverse of get_innermost_template_args: Return all but the innermost
573 EXTRA_LEVELS levels of template arguments from the ARGS. */
576 strip_innermost_template_args (tree args
, int extra_levels
)
579 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
584 /* If N is 1, just return the outermost set of template arguments. */
586 return TMPL_ARGS_LEVEL (args
, 1);
588 /* If we're not removing anything, just return the arguments we were
590 gcc_assert (extra_levels
>= 0);
591 if (extra_levels
== 0)
594 /* Make a new set of arguments, not containing the inner arguments. */
595 new_args
= make_tree_vec (n
);
596 for (i
= 1; i
<= n
; ++i
)
597 SET_TMPL_ARGS_LEVEL (new_args
, i
,
598 TMPL_ARGS_LEVEL (args
, i
));
603 /* We've got a template header coming up; push to a new level for storing
607 begin_template_parm_list (void)
609 /* We use a non-tag-transparent scope here, which causes pushtag to
610 put tags in this scope, rather than in the enclosing class or
611 namespace scope. This is the right thing, since we want
612 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
613 global template class, push_template_decl handles putting the
614 TEMPLATE_DECL into top-level scope. For a nested template class,
617 template <class T> struct S1 {
618 template <class T> struct S2 {};
621 pushtag contains special code to call pushdecl_with_scope on the
622 TEMPLATE_DECL for S2. */
623 begin_scope (sk_template_parms
, NULL
);
624 ++processing_template_decl
;
625 ++processing_template_parmlist
;
626 note_template_header (0);
629 /* This routine is called when a specialization is declared. If it is
630 invalid to declare a specialization here, an error is reported and
631 false is returned, otherwise this routine will return true. */
634 check_specialization_scope (void)
636 tree scope
= current_scope ();
640 An explicit specialization shall be declared in the namespace of
641 which the template is a member, or, for member templates, in the
642 namespace of which the enclosing class or enclosing class
643 template is a member. An explicit specialization of a member
644 function, member class or static data member of a class template
645 shall be declared in the namespace of which the class template
647 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
649 error ("explicit specialization in non-namespace scope %qD", scope
);
655 In an explicit specialization declaration for a member of a class
656 template or a member template that appears in namespace scope,
657 the member template and some of its enclosing class templates may
658 remain unspecialized, except that the declaration shall not
659 explicitly specialize a class member template if its enclosing
660 class templates are not explicitly specialized as well. */
661 if (current_template_parms
)
663 error ("enclosing class templates are not explicitly specialized");
670 /* We've just seen template <>. */
673 begin_specialization (void)
675 begin_scope (sk_template_spec
, NULL
);
676 note_template_header (1);
677 return check_specialization_scope ();
680 /* Called at then end of processing a declaration preceded by
684 end_specialization (void)
687 reset_specialization ();
690 /* Any template <>'s that we have seen thus far are not referring to a
691 function specialization. */
694 reset_specialization (void)
696 processing_specialization
= 0;
697 template_header_count
= 0;
700 /* We've just seen a template header. If SPECIALIZATION is nonzero,
701 it was of the form template <>. */
704 note_template_header (int specialization
)
706 processing_specialization
= specialization
;
707 template_header_count
++;
710 /* We're beginning an explicit instantiation. */
713 begin_explicit_instantiation (void)
715 gcc_assert (!processing_explicit_instantiation
);
716 processing_explicit_instantiation
= true;
721 end_explicit_instantiation (void)
723 gcc_assert (processing_explicit_instantiation
);
724 processing_explicit_instantiation
= false;
727 /* An explicit specialization or partial specialization TMPL is being
728 declared. Check that the namespace in which the specialization is
729 occurring is permissible. Returns false iff it is invalid to
730 specialize TMPL in the current namespace. */
733 check_specialization_namespace (tree tmpl
)
735 tree tpl_ns
= decl_namespace_context (tmpl
);
739 An explicit specialization shall be declared in the namespace of
740 which the template is a member, or, for member templates, in the
741 namespace of which the enclosing class or enclosing class
742 template is a member. An explicit specialization of a member
743 function, member class or static data member of a class template
744 shall be declared in the namespace of which the class template is
746 if (current_scope() != DECL_CONTEXT (tmpl
)
747 && !at_namespace_scope_p ())
749 error ("specialization of %qD must appear at namespace scope", tmpl
);
752 if (is_associated_namespace (current_namespace
, tpl_ns
))
753 /* Same or super-using namespace. */
757 permerror (input_location
, "specialization of %qD in different namespace", tmpl
);
758 permerror (input_location
, " from definition of %q+#D", tmpl
);
763 /* SPEC is an explicit instantiation. Check that it is valid to
764 perform this explicit instantiation in the current namespace. */
767 check_explicit_instantiation_namespace (tree spec
)
771 /* DR 275: An explicit instantiation shall appear in an enclosing
772 namespace of its template. */
773 ns
= decl_namespace_context (spec
);
774 if (!is_ancestor (current_namespace
, ns
))
775 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
776 "(which does not enclose namespace %qD)",
777 spec
, current_namespace
, ns
);
780 /* The TYPE is being declared. If it is a template type, that means it
781 is a partial specialization. Do appropriate error-checking. */
784 maybe_process_partial_specialization (tree type
)
788 if (type
== error_mark_node
)
789 return error_mark_node
;
791 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
793 error ("name of class shadows template template parameter %qD",
795 return error_mark_node
;
798 context
= TYPE_CONTEXT (type
);
800 if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
802 /* This is for ordinary explicit specialization and partial
803 specialization of a template class such as:
805 template <> class C<int>;
809 template <class T> class C<T*>;
811 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
813 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
814 && !COMPLETE_TYPE_P (type
))
816 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
));
817 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
818 if (processing_template_decl
)
820 if (push_template_decl (TYPE_MAIN_DECL (type
))
822 return error_mark_node
;
825 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
826 error ("specialization of %qT after instantiation", type
);
828 else if (CLASS_TYPE_P (type
)
829 && !CLASSTYPE_USE_TEMPLATE (type
)
830 && CLASSTYPE_TEMPLATE_INFO (type
)
831 && context
&& CLASS_TYPE_P (context
)
832 && CLASSTYPE_TEMPLATE_INFO (context
))
834 /* This is for an explicit specialization of member class
835 template according to [temp.expl.spec/18]:
837 template <> template <class U> class C<int>::D;
839 The context `C<int>' must be an implicit instantiation.
840 Otherwise this is just a member class template declared
843 template <> class C<int> { template <class U> class D; };
844 template <> template <class U> class C<int>::D;
846 In the first case, `C<int>::D' is a specialization of `C<T>::D'
847 while in the second case, `C<int>::D' is a primary template
848 and `C<T>::D' may not exist. */
850 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
851 && !COMPLETE_TYPE_P (type
))
854 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
856 if (current_namespace
857 != decl_namespace_context (tmpl
))
859 permerror (input_location
, "specializing %q#T in different namespace", type
);
860 permerror (input_location
, " from definition of %q+#D", tmpl
);
863 /* Check for invalid specialization after instantiation:
865 template <> template <> class C<int>::D<int>;
866 template <> template <class U> class C<int>::D; */
868 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
869 t
; t
= TREE_CHAIN (t
))
871 tree inst
= TREE_VALUE (t
);
872 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
))
874 /* We already have a full specialization of this partial
875 instantiation. Reassign it to the new member
876 specialization template. */
880 elt
.tmpl
= most_general_template (tmpl
);
881 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
884 htab_remove_elt (type_specializations
, &elt
);
887 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
889 slot
= (spec_entry
**)
890 htab_find_slot (type_specializations
, &elt
, INSERT
);
891 *slot
= GGC_NEW (spec_entry
);
894 else if (COMPLETE_TYPE_P (inst
) || TYPE_BEING_DEFINED (inst
))
895 /* But if we've had an implicit instantiation, that's a
896 problem ([temp.expl.spec]/6). */
897 error ("specialization %qT after instantiation %qT",
901 /* Mark TYPE as a specialization. And as a result, we only
902 have one level of template argument for the innermost
904 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
905 CLASSTYPE_TI_ARGS (type
)
906 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
909 else if (processing_specialization
)
911 error ("explicit specialization of non-template %qT", type
);
912 return error_mark_node
;
918 /* Returns nonzero if we can optimize the retrieval of specializations
919 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
920 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
923 optimize_specialization_lookup_p (tree tmpl
)
925 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
926 && DECL_CLASS_SCOPE_P (tmpl
)
927 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
929 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
930 /* The optimized lookup depends on the fact that the
931 template arguments for the member function template apply
932 purely to the containing class, which is not true if the
933 containing class is an explicit or partial
935 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
936 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
937 && !DECL_CONV_FN_P (tmpl
)
938 /* It is possible to have a template that is not a member
939 template and is not a member of a template class:
941 template <typename T>
942 struct S { friend A::f(); };
944 Here, the friend function is a template, but the context does
945 not have template information. The optimized lookup relies
946 on having ARGS be the template arguments for both the class
947 and the function template. */
948 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
951 /* Retrieve the specialization (in the sense of [temp.spec] - a
952 specialization is either an instantiation or an explicit
953 specialization) of TMPL for the given template ARGS. If there is
954 no such specialization, return NULL_TREE. The ARGS are a vector of
955 arguments, or a vector of vectors of arguments, in the case of
956 templates with more than one level of parameters.
958 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
959 then we search for a partial specialization matching ARGS. This
960 parameter is ignored if TMPL is not a class template. */
963 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
965 if (args
== error_mark_node
)
968 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
970 /* There should be as many levels of arguments as there are
971 levels of parameters. */
972 gcc_assert (TMPL_ARGS_DEPTH (args
)
973 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
975 if (optimize_specialization_lookup_p (tmpl
))
978 tree class_specialization
;
979 VEC(tree
,gc
) *methods
;
983 /* The template arguments actually apply to the containing
984 class. Find the class specialization with those
986 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
988 = retrieve_specialization (class_template
, args
, 0);
989 if (!class_specialization
)
991 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
992 for the specialization. */
993 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
996 /* Iterate through the methods with the indicated name, looking
997 for the one that has an instance of TMPL. */
998 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
999 for (fns
= VEC_index (tree
, methods
, idx
); fns
; fns
= OVL_NEXT (fns
))
1001 tree fn
= OVL_CURRENT (fns
);
1002 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1003 /* using-declarations can add base methods to the method vec,
1004 and we don't want those here. */
1005 && DECL_CONTEXT (fn
) == class_specialization
)
1014 htab_t specializations
;
1018 elt
.spec
= NULL_TREE
;
1020 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1021 specializations
= type_specializations
;
1023 specializations
= decl_specializations
;
1026 hash
= hash_specialization (&elt
);
1027 found
= (spec_entry
*) htab_find_with_hash (specializations
, &elt
, hash
);
1035 /* Like retrieve_specialization, but for local declarations. */
1038 retrieve_local_specialization (tree tmpl
)
1042 if (local_specializations
== NULL
)
1045 spec
= (tree
) htab_find_with_hash (local_specializations
, tmpl
,
1046 htab_hash_pointer (tmpl
));
1047 return spec
? TREE_PURPOSE (spec
) : NULL_TREE
;
1050 /* Returns nonzero iff DECL is a specialization of TMPL. */
1053 is_specialization_of (tree decl
, tree tmpl
)
1057 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1061 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1067 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1069 for (t
= TREE_TYPE (decl
);
1071 t
= CLASSTYPE_USE_TEMPLATE (t
)
1072 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1073 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1080 /* Returns nonzero iff DECL is a specialization of friend declaration
1081 FRIEND_DECL according to [temp.friend]. */
1084 is_specialization_of_friend (tree decl
, tree friend_decl
)
1086 bool need_template
= true;
1089 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1090 || TREE_CODE (decl
) == TYPE_DECL
);
1092 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1093 of a template class, we want to check if DECL is a specialization
1095 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1096 && DECL_TEMPLATE_INFO (friend_decl
)
1097 && !DECL_USE_TEMPLATE (friend_decl
))
1099 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1100 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1101 need_template
= false;
1103 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1104 && !PRIMARY_TEMPLATE_P (friend_decl
))
1105 need_template
= false;
1107 /* There is nothing to do if this is not a template friend. */
1108 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1111 if (is_specialization_of (decl
, friend_decl
))
1115 A member of a class template may be declared to be a friend of a
1116 non-template class. In this case, the corresponding member of
1117 every specialization of the class template is a friend of the
1118 class granting friendship.
1120 For example, given a template friend declaration
1122 template <class T> friend void A<T>::f();
1124 the member function below is considered a friend
1126 template <> struct A<int> {
1130 For this type of template friend, TEMPLATE_DEPTH below will be
1131 nonzero. To determine if DECL is a friend of FRIEND, we first
1132 check if the enclosing class is a specialization of another. */
1134 template_depth
= template_class_depth (DECL_CONTEXT (friend_decl
));
1136 && DECL_CLASS_SCOPE_P (decl
)
1137 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1138 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1140 /* Next, we check the members themselves. In order to handle
1141 a few tricky cases, such as when FRIEND_DECL's are
1143 template <class T> friend void A<T>::g(T t);
1144 template <class T> template <T t> friend void A<T>::h();
1148 void A<int>::g(int);
1149 template <int> void A<int>::h();
1151 we need to figure out ARGS, the template arguments from
1152 the context of DECL. This is required for template substitution
1153 of `T' in the function parameter of `g' and template parameter
1154 of `h' in the above examples. Here ARGS corresponds to `int'. */
1156 tree context
= DECL_CONTEXT (decl
);
1157 tree args
= NULL_TREE
;
1158 int current_depth
= 0;
1160 while (current_depth
< template_depth
)
1162 if (CLASSTYPE_TEMPLATE_INFO (context
))
1164 if (current_depth
== 0)
1165 args
= TYPE_TI_ARGS (context
);
1167 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1170 context
= TYPE_CONTEXT (context
);
1173 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1178 tree friend_args_type
;
1179 tree decl_args_type
;
1181 /* Make sure that both DECL and FRIEND_DECL are templates or
1183 is_template
= DECL_TEMPLATE_INFO (decl
)
1184 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1185 if (need_template
^ is_template
)
1187 else if (is_template
)
1189 /* If both are templates, check template parameter list. */
1191 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1193 if (!comp_template_parms
1194 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1198 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1201 decl_type
= TREE_TYPE (decl
);
1203 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1204 tf_none
, NULL_TREE
);
1205 if (friend_type
== error_mark_node
)
1208 /* Check if return types match. */
1209 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1212 /* Check if function parameter types match, ignoring the
1213 `this' parameter. */
1214 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1215 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1216 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1217 friend_args_type
= TREE_CHAIN (friend_args_type
);
1218 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1219 decl_args_type
= TREE_CHAIN (decl_args_type
);
1221 return compparms (decl_args_type
, friend_args_type
);
1225 /* DECL is a TYPE_DECL */
1227 tree decl_type
= TREE_TYPE (decl
);
1229 /* Make sure that both DECL and FRIEND_DECL are templates or
1232 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1233 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1235 if (need_template
^ is_template
)
1237 else if (is_template
)
1240 /* If both are templates, check the name of the two
1241 TEMPLATE_DECL's first because is_friend didn't. */
1242 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1243 != DECL_NAME (friend_decl
))
1246 /* Now check template parameter list. */
1248 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1250 return comp_template_parms
1251 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1255 return (DECL_NAME (decl
)
1256 == DECL_NAME (friend_decl
));
1262 /* Register the specialization SPEC as a specialization of TMPL with
1263 the indicated ARGS. IS_FRIEND indicates whether the specialization
1264 is actually just a friend declaration. Returns SPEC, or an
1265 equivalent prior declaration, if available. */
1268 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1272 spec_entry
**slot
= NULL
;
1275 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
));
1277 if (TREE_CODE (spec
) == FUNCTION_DECL
1278 && uses_template_parms (DECL_TI_ARGS (spec
)))
1279 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1280 register it; we want the corresponding TEMPLATE_DECL instead.
1281 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1282 the more obvious `uses_template_parms (spec)' to avoid problems
1283 with default function arguments. In particular, given
1284 something like this:
1286 template <class T> void f(T t1, T t = T())
1288 the default argument expression is not substituted for in an
1289 instantiation unless and until it is actually needed. */
1292 if (optimize_specialization_lookup_p (tmpl
))
1293 /* We don't put these specializations in the hash table, but we might
1294 want to give an error about a mismatch. */
1295 fn
= retrieve_specialization (tmpl
, args
, 0);
1303 hash
= hash_specialization (&elt
);
1305 slot
= (spec_entry
**)
1306 htab_find_slot_with_hash (decl_specializations
, &elt
, hash
, INSERT
);
1313 /* We can sometimes try to re-register a specialization that we've
1314 already got. In particular, regenerate_decl_from_template calls
1315 duplicate_decls which will update the specialization list. But,
1316 we'll still get called again here anyhow. It's more convenient
1317 to simply allow this than to try to prevent it. */
1320 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1322 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1324 if (DECL_ODR_USED (fn
)
1325 || DECL_EXPLICIT_INSTANTIATION (fn
))
1327 error ("specialization of %qD after instantiation",
1329 return error_mark_node
;
1334 /* This situation should occur only if the first
1335 specialization is an implicit instantiation, the
1336 second is an explicit specialization, and the
1337 implicit instantiation has not yet been used. That
1338 situation can occur if we have implicitly
1339 instantiated a member function and then specialized
1342 We can also wind up here if a friend declaration that
1343 looked like an instantiation turns out to be a
1346 template <class T> void foo(T);
1347 class S { friend void foo<>(int) };
1348 template <> void foo(int);
1350 We transform the existing DECL in place so that any
1351 pointers to it become pointers to the updated
1354 If there was a definition for the template, but not
1355 for the specialization, we want this to look as if
1356 there were no definition, and vice versa. */
1357 DECL_INITIAL (fn
) = NULL_TREE
;
1358 duplicate_decls (spec
, fn
, is_friend
);
1359 /* The call to duplicate_decls will have applied
1362 An explicit specialization of a function template
1363 is inline only if it is explicitly declared to be,
1364 and independently of whether its function template
1367 to the primary function; now copy the inline bits to
1368 the various clones. */
1369 FOR_EACH_CLONE (clone
, fn
)
1371 DECL_DECLARED_INLINE_P (clone
)
1372 = DECL_DECLARED_INLINE_P (fn
);
1373 DECL_SOURCE_LOCATION (clone
)
1374 = DECL_SOURCE_LOCATION (fn
);
1376 check_specialization_namespace (fn
);
1381 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1383 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1384 /* Dup decl failed, but this is a new definition. Set the
1385 line number so any errors match this new
1387 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1393 return duplicate_decls (spec
, fn
, is_friend
);
1395 /* A specialization must be declared in the same namespace as the
1396 template it is specializing. */
1397 if (DECL_TEMPLATE_SPECIALIZATION (spec
)
1398 && !check_specialization_namespace (tmpl
))
1399 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1401 if (!optimize_specialization_lookup_p (tmpl
))
1403 gcc_assert (tmpl
&& args
&& spec
);
1404 *slot
= GGC_NEW (spec_entry
);
1406 if (TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1407 && PRIMARY_TEMPLATE_P (tmpl
)
1408 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1409 /* TMPL is a forward declaration of a template function; keep a list
1410 of all specializations in case we need to reassign them to a friend
1411 template later in tsubst_friend_function. */
1412 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1413 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1419 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1420 TMPL and ARGS members, ignores SPEC. */
1423 eq_specializations (const void *p1
, const void *p2
)
1425 const spec_entry
*e1
= (const spec_entry
*)p1
;
1426 const spec_entry
*e2
= (const spec_entry
*)p2
;
1428 return (e1
->tmpl
== e2
->tmpl
1429 && comp_template_args (e1
->args
, e2
->args
));
1432 /* Returns a hash for a template TMPL and template arguments ARGS. */
1435 hash_tmpl_and_args (tree tmpl
, tree args
)
1437 hashval_t val
= DECL_UID (tmpl
);
1438 return iterative_hash_template_arg (args
, val
);
1441 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1445 hash_specialization (const void *p
)
1447 const spec_entry
*e
= (const spec_entry
*)p
;
1448 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1451 /* Recursively calculate a hash value for a template argument ARG, for use
1452 in the hash tables of template specializations. */
1455 iterative_hash_template_arg (tree arg
, hashval_t val
)
1457 unsigned HOST_WIDE_INT i
;
1458 enum tree_code code
;
1461 if (arg
== NULL_TREE
)
1462 return iterative_hash_object (arg
, val
);
1467 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1468 /* We can get one of these when re-hashing a previous entry in the middle
1469 of substituting into a pack expansion. Just look through it. */
1470 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1472 code
= TREE_CODE (arg
);
1473 tclass
= TREE_CODE_CLASS (code
);
1475 val
= iterative_hash_object (code
, val
);
1482 case IDENTIFIER_NODE
:
1483 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1487 int i
, len
= TREE_VEC_LENGTH (arg
);
1488 for (i
= 0; i
< len
; ++i
)
1489 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1493 case TYPE_PACK_EXPANSION
:
1494 case EXPR_PACK_EXPANSION
:
1495 return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1497 case TYPE_ARGUMENT_PACK
:
1498 case NONTYPE_ARGUMENT_PACK
:
1499 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1502 for (; arg
; arg
= TREE_CHAIN (arg
))
1503 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1507 for (; arg
; arg
= OVL_CHAIN (arg
))
1508 val
= iterative_hash_template_arg (OVL_FUNCTION (arg
), val
);
1514 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1516 val
= iterative_hash_template_arg (field
, val
);
1517 val
= iterative_hash_template_arg (value
, val
);
1523 if (!DECL_ARTIFICIAL (arg
))
1524 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1525 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1528 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1531 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1532 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1534 case TEMPLATE_PARM_INDEX
:
1535 val
= iterative_hash_template_arg
1536 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1537 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1538 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1541 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1542 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1543 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1546 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1548 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1552 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1553 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1554 val
= iterative_hash_object (code
, val
);
1555 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1558 /* layout_type sets structural equality for arrays of
1559 incomplete type, so we can't rely on the canonical type
1561 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1562 return iterative_hash_template_arg (TYPE_DOMAIN (arg
), val
);
1565 /* A lambda can't appear in a template arg, but don't crash on
1567 gcc_assert (seen_error ());
1574 if (TYPE_CANONICAL (arg
))
1575 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1577 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1578 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1579 /* Otherwise just compare the types during lookup. */
1582 case tcc_declaration
:
1584 return iterative_hash_expr (arg
, val
);
1587 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1589 unsigned n
= TREE_OPERAND_LENGTH (arg
);
1590 for (i
= 0; i
< n
; ++i
)
1591 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1600 /* Unregister the specialization SPEC as a specialization of TMPL.
1601 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1602 if the SPEC was listed as a specialization of TMPL.
1604 Note that SPEC has been ggc_freed, so we can't look inside it. */
1607 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1612 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1613 elt
.args
= TI_ARGS (tinfo
);
1614 elt
.spec
= NULL_TREE
;
1616 slot
= (spec_entry
**) htab_find_slot (decl_specializations
, &elt
, INSERT
);
1619 gcc_assert ((*slot
)->spec
== spec
|| (*slot
)->spec
== new_spec
);
1620 gcc_assert (new_spec
!= NULL_TREE
);
1621 (*slot
)->spec
= new_spec
;
1628 /* Compare an entry in the local specializations hash table P1 (which
1629 is really a pointer to a TREE_LIST) with P2 (which is really a
1633 eq_local_specializations (const void *p1
, const void *p2
)
1635 return TREE_VALUE ((const_tree
) p1
) == (const_tree
) p2
;
1638 /* Hash P1, an entry in the local specializations table. */
1641 hash_local_specialization (const void* p1
)
1643 return htab_hash_pointer (TREE_VALUE ((const_tree
) p1
));
1646 /* Like register_specialization, but for local declarations. We are
1647 registering SPEC, an instantiation of TMPL. */
1650 register_local_specialization (tree spec
, tree tmpl
)
1654 slot
= htab_find_slot_with_hash (local_specializations
, tmpl
,
1655 htab_hash_pointer (tmpl
), INSERT
);
1656 *slot
= build_tree_list (spec
, tmpl
);
1659 /* TYPE is a class type. Returns true if TYPE is an explicitly
1660 specialized class. */
1663 explicit_class_specialization_p (tree type
)
1665 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1667 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1670 /* Print the list of functions at FNS, going through all the overloads
1671 for each element of the list. Alternatively, FNS can not be a
1672 TREE_LIST, in which case it will be printed together with all the
1675 MORE and *STR should respectively be FALSE and NULL when the function
1676 is called from the outside. They are used internally on recursive
1677 calls. print_candidates manages the two parameters and leaves NULL
1678 in *STR when it ends. */
1681 print_candidates_1 (tree fns
, bool more
, const char **str
)
1684 char *spaces
= NULL
;
1686 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1687 if (TREE_CODE (fn
) == TREE_LIST
)
1689 gcc_assert (!OVL_NEXT (fn
) && !is_overloaded_fn (fn
));
1690 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1691 print_candidates_1 (TREE_VALUE (fn2
),
1692 TREE_CHAIN (fn2
) || more
, str
);
1698 /* Pick the prefix string. */
1699 if (!more
&& !OVL_NEXT (fns
))
1701 error ("candidate is: %+#D", OVL_CURRENT (fn
));
1705 *str
= _("candidates are:");
1706 spaces
= get_spaces (*str
);
1708 error ("%s %+#D", *str
, OVL_CURRENT (fn
));
1709 *str
= spaces
? spaces
: *str
;
1719 /* Print the list of candidate FNS in an error message. */
1722 print_candidates (tree fns
)
1724 const char *str
= NULL
;
1725 print_candidates_1 (fns
, false, &str
);
1726 gcc_assert (str
== NULL
);
1729 /* Returns the template (one of the functions given by TEMPLATE_ID)
1730 which can be specialized to match the indicated DECL with the
1731 explicit template args given in TEMPLATE_ID. The DECL may be
1732 NULL_TREE if none is available. In that case, the functions in
1733 TEMPLATE_ID are non-members.
1735 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1736 specialization of a member template.
1738 The TEMPLATE_COUNT is the number of references to qualifying
1739 template classes that appeared in the name of the function. See
1740 check_explicit_specialization for a more accurate description.
1742 TSK indicates what kind of template declaration (if any) is being
1743 declared. TSK_TEMPLATE indicates that the declaration given by
1744 DECL, though a FUNCTION_DECL, has template parameters, and is
1745 therefore a template function.
1747 The template args (those explicitly specified and those deduced)
1748 are output in a newly created vector *TARGS_OUT.
1750 If it is impossible to determine the result, an error message is
1751 issued. The error_mark_node is returned to indicate failure. */
1754 determine_specialization (tree template_id
,
1757 int need_member_template
,
1763 tree explicit_targs
;
1764 tree candidates
= NULL_TREE
;
1765 /* A TREE_LIST of templates of which DECL may be a specialization.
1766 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1767 corresponding TREE_PURPOSE is the set of template arguments that,
1768 when used to instantiate the template, would produce a function
1769 with the signature of DECL. */
1770 tree templates
= NULL_TREE
;
1772 struct cp_binding_level
*b
;
1774 *targs_out
= NULL_TREE
;
1776 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1777 return error_mark_node
;
1779 fns
= TREE_OPERAND (template_id
, 0);
1780 explicit_targs
= TREE_OPERAND (template_id
, 1);
1782 if (fns
== error_mark_node
)
1783 return error_mark_node
;
1785 /* Check for baselinks. */
1786 if (BASELINK_P (fns
))
1787 fns
= BASELINK_FUNCTIONS (fns
);
1789 if (!is_overloaded_fn (fns
))
1791 error ("%qD is not a function template", fns
);
1792 return error_mark_node
;
1795 /* Count the number of template headers specified for this
1798 for (b
= current_binding_level
;
1799 b
->kind
== sk_template_parms
;
1803 for (; fns
; fns
= OVL_NEXT (fns
))
1805 tree fn
= OVL_CURRENT (fns
);
1807 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1809 tree decl_arg_types
;
1812 /* In case of explicit specialization, we need to check if
1813 the number of template headers appearing in the specialization
1814 is correct. This is usually done in check_explicit_specialization,
1815 but the check done there cannot be exhaustive when specializing
1816 member functions. Consider the following code:
1818 template <> void A<int>::f(int);
1819 template <> template <> void A<int>::f(int);
1821 Assuming that A<int> is not itself an explicit specialization
1822 already, the first line specializes "f" which is a non-template
1823 member function, whilst the second line specializes "f" which
1824 is a template member function. So both lines are syntactically
1825 correct, and check_explicit_specialization does not reject
1828 Here, we can do better, as we are matching the specialization
1829 against the declarations. We count the number of template
1830 headers, and we check if they match TEMPLATE_COUNT + 1
1831 (TEMPLATE_COUNT is the number of qualifying template classes,
1832 plus there must be another header for the member template
1835 Notice that if header_count is zero, this is not a
1836 specialization but rather a template instantiation, so there
1837 is no check we can perform here. */
1838 if (header_count
&& header_count
!= template_count
+ 1)
1841 /* Check that the number of template arguments at the
1842 innermost level for DECL is the same as for FN. */
1843 if (current_binding_level
->kind
== sk_template_parms
1844 && !current_binding_level
->explicit_spec_p
1845 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1846 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1847 (current_template_parms
))))
1850 /* DECL might be a specialization of FN. */
1851 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1852 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1854 /* For a non-static member function, we need to make sure
1855 that the const qualification is the same. Since
1856 get_bindings does not try to merge the "this" parameter,
1857 we must do the comparison explicitly. */
1858 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1859 && !same_type_p (TREE_VALUE (fn_arg_types
),
1860 TREE_VALUE (decl_arg_types
)))
1863 /* Skip the "this" parameter and, for constructors of
1864 classes with virtual bases, the VTT parameter. A
1865 full specialization of a constructor will have a VTT
1866 parameter, but a template never will. */
1868 = skip_artificial_parms_for (decl
, decl_arg_types
);
1870 = skip_artificial_parms_for (fn
, fn_arg_types
);
1872 /* Check that the number of function parameters matches.
1874 template <class T> void f(int i = 0);
1875 template <> void f<int>();
1876 The specialization f<int> is invalid but is not caught
1877 by get_bindings below. */
1878 if (list_length (fn_arg_types
) != list_length (decl_arg_types
))
1881 /* Function templates cannot be specializations; there are
1882 no partial specializations of functions. Therefore, if
1883 the type of DECL does not match FN, there is no
1885 if (tsk
== tsk_template
)
1887 if (compparms (fn_arg_types
, decl_arg_types
))
1888 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1892 /* See whether this function might be a specialization of this
1894 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1897 /* We cannot deduce template arguments that when used to
1898 specialize TMPL will produce DECL. */
1901 /* Save this template, and the arguments deduced. */
1902 templates
= tree_cons (targs
, fn
, templates
);
1904 else if (need_member_template
)
1905 /* FN is an ordinary member function, and we need a
1906 specialization of a member template. */
1908 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1909 /* We can get IDENTIFIER_NODEs here in certain erroneous
1912 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1913 /* This is just an ordinary non-member function. Nothing can
1914 be a specialization of that. */
1916 else if (DECL_ARTIFICIAL (fn
))
1917 /* Cannot specialize functions that are created implicitly. */
1921 tree decl_arg_types
;
1923 /* This is an ordinary member function. However, since
1924 we're here, we can assume it's enclosing class is a
1925 template class. For example,
1927 template <typename T> struct S { void f(); };
1928 template <> void S<int>::f() {}
1930 Here, S<int>::f is a non-template, but S<int> is a
1931 template class. If FN has the same type as DECL, we
1932 might be in business. */
1934 if (!DECL_TEMPLATE_INFO (fn
))
1935 /* Its enclosing class is an explicit specialization
1936 of a template class. This is not a candidate. */
1939 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
1940 TREE_TYPE (TREE_TYPE (fn
))))
1941 /* The return types differ. */
1944 /* Adjust the type of DECL in case FN is a static member. */
1945 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1946 if (DECL_STATIC_FUNCTION_P (fn
)
1947 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1948 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1950 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1953 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1957 if (templates
&& TREE_CHAIN (templates
))
1963 It is possible for a specialization with a given function
1964 signature to be instantiated from more than one function
1965 template. In such cases, explicit specification of the
1966 template arguments must be used to uniquely identify the
1967 function template specialization being specialized.
1969 Note that here, there's no suggestion that we're supposed to
1970 determine which of the candidate templates is most
1971 specialized. However, we, also have:
1975 Partial ordering of overloaded function template
1976 declarations is used in the following contexts to select
1977 the function template to which a function template
1978 specialization refers:
1980 -- when an explicit specialization refers to a function
1983 So, we do use the partial ordering rules, at least for now.
1984 This extension can only serve to make invalid programs valid,
1985 so it's safe. And, there is strong anecdotal evidence that
1986 the committee intended the partial ordering rules to apply;
1987 the EDG front end has that behavior, and John Spicer claims
1988 that the committee simply forgot to delete the wording in
1989 [temp.expl.spec]. */
1990 tree tmpl
= most_specialized_instantiation (templates
);
1991 if (tmpl
!= error_mark_node
)
1994 TREE_CHAIN (templates
) = NULL_TREE
;
1998 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2000 error ("template-id %qD for %q+D does not match any template "
2001 "declaration", template_id
, decl
);
2002 if (header_count
&& header_count
!= template_count
+ 1)
2003 inform (input_location
, "saw %d %<template<>%>, need %d for "
2004 "specializing a member function template",
2005 header_count
, template_count
+ 1);
2006 return error_mark_node
;
2008 else if ((templates
&& TREE_CHAIN (templates
))
2009 || (candidates
&& TREE_CHAIN (candidates
))
2010 || (templates
&& candidates
))
2012 error ("ambiguous template specialization %qD for %q+D",
2014 candidates
= chainon (candidates
, templates
);
2015 print_candidates (candidates
);
2016 return error_mark_node
;
2019 /* We have one, and exactly one, match. */
2022 tree fn
= TREE_VALUE (candidates
);
2023 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2024 /* DECL is a re-declaration or partial instantiation of a template
2026 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2028 /* It was a specialization of an ordinary member function in a
2030 return DECL_TI_TEMPLATE (fn
);
2033 /* It was a specialization of a template. */
2034 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2035 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2037 *targs_out
= copy_node (targs
);
2038 SET_TMPL_ARGS_LEVEL (*targs_out
,
2039 TMPL_ARGS_DEPTH (*targs_out
),
2040 TREE_PURPOSE (templates
));
2043 *targs_out
= TREE_PURPOSE (templates
);
2044 return TREE_VALUE (templates
);
2047 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2048 but with the default argument values filled in from those in the
2052 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2055 tree new_spec_types
;
2060 if (spec_types
== void_list_node
)
2061 return void_list_node
;
2063 /* Substitute into the rest of the list. */
2065 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2066 TREE_CHAIN (tmpl_types
));
2068 /* Add the default argument for this parameter. */
2069 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2070 TREE_VALUE (spec_types
),
2074 /* DECL is an explicit specialization. Replicate default arguments
2075 from the template it specializes. (That way, code like:
2077 template <class T> void f(T = 3);
2078 template <> void f(double);
2081 works, as required.) An alternative approach would be to look up
2082 the correct default arguments at the call-site, but this approach
2083 is consistent with how implicit instantiations are handled. */
2086 copy_default_args_to_explicit_spec (tree decl
)
2091 tree new_spec_types
;
2095 tree object_type
= NULL_TREE
;
2096 tree in_charge
= NULL_TREE
;
2097 tree vtt
= NULL_TREE
;
2099 /* See if there's anything we need to do. */
2100 tmpl
= DECL_TI_TEMPLATE (decl
);
2101 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2102 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2103 if (TREE_PURPOSE (t
))
2108 old_type
= TREE_TYPE (decl
);
2109 spec_types
= TYPE_ARG_TYPES (old_type
);
2111 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2113 /* Remove the this pointer, but remember the object's type for
2115 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2116 spec_types
= TREE_CHAIN (spec_types
);
2117 tmpl_types
= TREE_CHAIN (tmpl_types
);
2119 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2121 /* DECL may contain more parameters than TMPL due to the extra
2122 in-charge parameter in constructors and destructors. */
2123 in_charge
= spec_types
;
2124 spec_types
= TREE_CHAIN (spec_types
);
2126 if (DECL_HAS_VTT_PARM_P (decl
))
2129 spec_types
= TREE_CHAIN (spec_types
);
2133 /* Compute the merged default arguments. */
2135 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2137 /* Compute the new FUNCTION_TYPE. */
2141 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2146 /* Put the in-charge parameter back. */
2147 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2148 TREE_VALUE (in_charge
),
2151 new_type
= build_method_type_directly (object_type
,
2152 TREE_TYPE (old_type
),
2156 new_type
= build_function_type (TREE_TYPE (old_type
),
2158 new_type
= cp_build_type_attribute_variant (new_type
,
2159 TYPE_ATTRIBUTES (old_type
));
2160 new_type
= build_exception_variant (new_type
,
2161 TYPE_RAISES_EXCEPTIONS (old_type
));
2162 TREE_TYPE (decl
) = new_type
;
2165 /* Check to see if the function just declared, as indicated in
2166 DECLARATOR, and in DECL, is a specialization of a function
2167 template. We may also discover that the declaration is an explicit
2168 instantiation at this point.
2170 Returns DECL, or an equivalent declaration that should be used
2171 instead if all goes well. Issues an error message if something is
2172 amiss. Returns error_mark_node if the error is not easily
2175 FLAGS is a bitmask consisting of the following flags:
2177 2: The function has a definition.
2178 4: The function is a friend.
2180 The TEMPLATE_COUNT is the number of references to qualifying
2181 template classes that appeared in the name of the function. For
2184 template <class T> struct S { void f(); };
2187 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2188 classes are not counted in the TEMPLATE_COUNT, so that in
2190 template <class T> struct S {};
2191 template <> struct S<int> { void f(); }
2192 template <> void S<int>::f();
2194 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2195 invalid; there should be no template <>.)
2197 If the function is a specialization, it is marked as such via
2198 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2199 is set up correctly, and it is added to the list of specializations
2200 for that template. */
2203 check_explicit_specialization (tree declarator
,
2208 int have_def
= flags
& 2;
2209 int is_friend
= flags
& 4;
2210 int specialization
= 0;
2211 int explicit_instantiation
= 0;
2212 int member_specialization
= 0;
2213 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2214 tree dname
= DECL_NAME (decl
);
2219 if (!processing_specialization
)
2222 tsk
= tsk_excessive_parms
;
2225 tsk
= current_tmpl_spec_kind (template_count
);
2230 if (processing_specialization
)
2233 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2235 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2238 /* This could be something like:
2240 template <class T> void f(T);
2241 class S { friend void f<>(int); } */
2245 /* This case handles bogus declarations like template <>
2246 template <class T> void f<int>(); */
2248 error ("template-id %qD in declaration of primary template",
2255 case tsk_invalid_member_spec
:
2256 /* The error has already been reported in
2257 check_specialization_scope. */
2258 return error_mark_node
;
2260 case tsk_invalid_expl_inst
:
2261 error ("template parameter list used in explicit instantiation");
2267 error ("definition provided for explicit instantiation");
2269 explicit_instantiation
= 1;
2272 case tsk_excessive_parms
:
2273 case tsk_insufficient_parms
:
2274 if (tsk
== tsk_excessive_parms
)
2275 error ("too many template parameter lists in declaration of %qD",
2277 else if (template_header_count
)
2278 error("too few template parameter lists in declaration of %qD", decl
);
2280 error("explicit specialization of %qD must be introduced by "
2281 "%<template <>%>", decl
);
2285 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2287 member_specialization
= 1;
2293 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2295 /* This case handles bogus declarations like template <>
2296 template <class T> void f<int>(); */
2298 if (uses_template_parms (declarator
))
2299 error ("function template partial specialization %qD "
2300 "is not allowed", declarator
);
2302 error ("template-id %qD in declaration of primary template",
2307 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2308 /* This is a specialization of a member template, without
2309 specialization the containing class. Something like:
2311 template <class T> struct S {
2312 template <class U> void f (U);
2314 template <> template <class U> void S<int>::f(U) {}
2316 That's a specialization -- but of the entire template. */
2324 if (specialization
|| member_specialization
)
2326 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2327 for (; t
; t
= TREE_CHAIN (t
))
2328 if (TREE_PURPOSE (t
))
2330 permerror (input_location
,
2331 "default argument specified in explicit specialization");
2336 if (specialization
|| member_specialization
|| explicit_instantiation
)
2338 tree tmpl
= NULL_TREE
;
2339 tree targs
= NULL_TREE
;
2341 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2342 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2346 gcc_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
);
2351 /* If there is no class context, the explicit instantiation
2352 must be at namespace scope. */
2353 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2355 /* Find the namespace binding, using the declaration
2357 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2359 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2361 error ("%qD is not a template function", dname
);
2362 fns
= error_mark_node
;
2366 tree fn
= OVL_CURRENT (fns
);
2367 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2368 CP_DECL_CONTEXT (fn
)))
2369 error ("%qD is not declared in %qD",
2370 decl
, current_namespace
);
2374 declarator
= lookup_template_function (fns
, NULL_TREE
);
2377 if (declarator
== error_mark_node
)
2378 return error_mark_node
;
2380 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2382 if (!explicit_instantiation
)
2383 /* A specialization in class scope. This is invalid,
2384 but the error will already have been flagged by
2385 check_specialization_scope. */
2386 return error_mark_node
;
2389 /* It's not valid to write an explicit instantiation in
2392 class C { template void f(); }
2394 This case is caught by the parser. However, on
2397 template class C { void f(); };
2399 (which is invalid) we can get here. The error will be
2406 else if (ctype
!= NULL_TREE
2407 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
2410 /* Find the list of functions in ctype that have the same
2411 name as the declared function. */
2412 tree name
= TREE_OPERAND (declarator
, 0);
2413 tree fns
= NULL_TREE
;
2416 if (constructor_name_p (name
, ctype
))
2418 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2420 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2421 : !CLASSTYPE_DESTRUCTORS (ctype
))
2423 /* From [temp.expl.spec]:
2425 If such an explicit specialization for the member
2426 of a class template names an implicitly-declared
2427 special member function (clause _special_), the
2428 program is ill-formed.
2430 Similar language is found in [temp.explicit]. */
2431 error ("specialization of implicitly-declared special member function");
2432 return error_mark_node
;
2435 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2438 if (!DECL_CONV_FN_P (decl
))
2440 idx
= lookup_fnfields_1 (ctype
, name
);
2442 fns
= VEC_index (tree
, CLASSTYPE_METHOD_VEC (ctype
), idx
);
2446 VEC(tree
,gc
) *methods
;
2449 /* For a type-conversion operator, we cannot do a
2450 name-based lookup. We might be looking for `operator
2451 int' which will be a specialization of `operator T'.
2452 So, we find *all* the conversion operators, and then
2453 select from them. */
2456 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2458 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2459 VEC_iterate (tree
, methods
, idx
, ovl
);
2462 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2463 /* There are no more conversion functions. */
2466 /* Glue all these conversion functions together
2467 with those we already have. */
2468 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2469 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2473 if (fns
== NULL_TREE
)
2475 error ("no member function %qD declared in %qT", name
, ctype
);
2476 return error_mark_node
;
2479 TREE_OPERAND (declarator
, 0) = fns
;
2482 /* Figure out what exactly is being specialized at this point.
2483 Note that for an explicit instantiation, even one for a
2484 member function, we cannot tell apriori whether the
2485 instantiation is for a member template, or just a member
2486 function of a template class. Even if a member template is
2487 being instantiated, the member template arguments may be
2488 elided if they can be deduced from the rest of the
2490 tmpl
= determine_specialization (declarator
, decl
,
2492 member_specialization
,
2496 if (!tmpl
|| tmpl
== error_mark_node
)
2497 /* We couldn't figure out what this declaration was
2499 return error_mark_node
;
2502 tree gen_tmpl
= most_general_template (tmpl
);
2504 if (explicit_instantiation
)
2506 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2507 is done by do_decl_instantiation later. */
2509 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2510 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2512 if (arg_depth
> parm_depth
)
2514 /* If TMPL is not the most general template (for
2515 example, if TMPL is a friend template that is
2516 injected into namespace scope), then there will
2517 be too many levels of TARGS. Remove some of them
2522 new_targs
= make_tree_vec (parm_depth
);
2523 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2524 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2525 = TREE_VEC_ELT (targs
, i
);
2529 return instantiate_template (tmpl
, targs
, tf_error
);
2532 /* If we thought that the DECL was a member function, but it
2533 turns out to be specializing a static member function,
2534 make DECL a static member function as well. */
2535 if (DECL_STATIC_FUNCTION_P (tmpl
)
2536 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2537 revert_static_member_fn (decl
);
2539 /* If this is a specialization of a member template of a
2540 template class, we want to return the TEMPLATE_DECL, not
2541 the specialization of it. */
2542 if (tsk
== tsk_template
)
2544 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2545 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2546 DECL_INITIAL (result
) = NULL_TREE
;
2550 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2551 DECL_SOURCE_LOCATION (result
)
2552 = DECL_SOURCE_LOCATION (decl
);
2553 /* We want to use the argument list specified in the
2554 definition, not in the original declaration. */
2555 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2556 for (parm
= DECL_ARGUMENTS (result
); parm
;
2557 parm
= TREE_CHAIN (parm
))
2558 DECL_CONTEXT (parm
) = result
;
2560 return register_specialization (tmpl
, gen_tmpl
, targs
,
2564 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2565 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2567 /* Inherit default function arguments from the template
2568 DECL is specializing. */
2569 copy_default_args_to_explicit_spec (decl
);
2571 /* This specialization has the same protection as the
2572 template it specializes. */
2573 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2574 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2576 /* 7.1.1-1 [dcl.stc]
2578 A storage-class-specifier shall not be specified in an
2579 explicit specialization...
2581 The parser rejects these, so unless action is taken here,
2582 explicit function specializations will always appear with
2585 The action recommended by the C++ CWG in response to C++
2586 defect report 605 is to make the storage class and linkage
2587 of the explicit specialization match the templated function:
2589 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2591 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2593 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2594 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2596 /* This specialization has the same linkage and visibility as
2597 the function template it specializes. */
2598 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2599 if (! TREE_PUBLIC (decl
))
2601 DECL_INTERFACE_KNOWN (decl
) = 1;
2602 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2604 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2605 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2607 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2608 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2612 /* If DECL is a friend declaration, declared using an
2613 unqualified name, the namespace associated with DECL may
2614 have been set incorrectly. For example, in:
2616 template <typename T> void f(T);
2618 struct S { friend void f<int>(int); }
2621 we will have set the DECL_CONTEXT for the friend
2622 declaration to N, rather than to the global namespace. */
2623 if (DECL_NAMESPACE_SCOPE_P (decl
))
2624 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2626 if (is_friend
&& !have_def
)
2627 /* This is not really a declaration of a specialization.
2628 It's just the name of an instantiation. But, it's not
2629 a request for an instantiation, either. */
2630 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2631 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2632 /* This is indeed a specialization. In case of constructors
2633 and destructors, we need in-charge and not-in-charge
2634 versions in V3 ABI. */
2635 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2637 /* Register this specialization so that we can find it
2639 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2646 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2647 parameters. These are represented in the same format used for
2648 DECL_TEMPLATE_PARMS. */
2651 comp_template_parms (const_tree parms1
, const_tree parms2
)
2656 if (parms1
== parms2
)
2659 for (p1
= parms1
, p2
= parms2
;
2660 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2661 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2663 tree t1
= TREE_VALUE (p1
);
2664 tree t2
= TREE_VALUE (p2
);
2667 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2668 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2670 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2673 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2675 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2676 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2678 /* If either of the template parameters are invalid, assume
2679 they match for the sake of error recovery. */
2680 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2683 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2686 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2687 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2688 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2690 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2695 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2696 /* One set of parameters has more parameters lists than the
2703 /* Determine whether PARM is a parameter pack. */
2706 template_parameter_pack_p (const_tree parm
)
2708 /* Determine if we have a non-type template parameter pack. */
2709 if (TREE_CODE (parm
) == PARM_DECL
)
2710 return (DECL_TEMPLATE_PARM_P (parm
)
2711 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2713 /* If this is a list of template parameters, we could get a
2714 TYPE_DECL or a TEMPLATE_DECL. */
2715 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2716 parm
= TREE_TYPE (parm
);
2718 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2719 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2720 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2723 /* Determine if T is a function parameter pack. */
2726 function_parameter_pack_p (const_tree t
)
2728 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2729 return FUNCTION_PARAMETER_PACK_P (t
);
2733 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2734 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2737 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2739 if (! primary_func_tmpl_inst
2740 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2741 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2744 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2747 /* Return true iff the function parameter PARAM_DECL was expanded
2748 from the function parameter pack PACK. */
2751 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
2753 if (DECL_ARTIFICIAL (param_decl
)
2754 || !function_parameter_pack_p (pack
))
2757 /* The parameter pack and its pack arguments have the same
2759 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
2762 /* Determine whether ARGS describes a variadic template args list,
2763 i.e., one that is terminated by a template argument pack. */
2766 template_args_variadic_p (tree args
)
2771 if (args
== NULL_TREE
)
2774 args
= INNERMOST_TEMPLATE_ARGS (args
);
2775 nargs
= TREE_VEC_LENGTH (args
);
2780 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2782 return ARGUMENT_PACK_P (last_parm
);
2785 /* Generate a new name for the parameter pack name NAME (an
2786 IDENTIFIER_NODE) that incorporates its */
2789 make_ith_pack_parameter_name (tree name
, int i
)
2791 /* Munge the name to include the parameter index. */
2792 #define NUMBUF_LEN 128
2793 char numbuf
[NUMBUF_LEN
];
2797 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
2798 newname_len
= IDENTIFIER_LENGTH (name
)
2799 + strlen (numbuf
) + 2;
2800 newname
= (char*)alloca (newname_len
);
2801 snprintf (newname
, newname_len
,
2802 "%s#%i", IDENTIFIER_POINTER (name
), i
);
2803 return get_identifier (newname
);
2806 /* Return true if T is a primary function
2807 or class template instantiation. */
2810 primary_template_instantiation_p (const_tree t
)
2815 if (TREE_CODE (t
) == FUNCTION_DECL
)
2816 return DECL_LANG_SPECIFIC (t
)
2817 && DECL_TEMPLATE_INSTANTIATION (t
)
2818 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
2819 else if (CLASS_TYPE_P (t
))
2820 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
2821 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
2825 /* Return true if PARM is a template template parameter. */
2828 template_template_parameter_p (const_tree parm
)
2830 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
2833 /* Return the template parameters of T if T is a
2834 primary template instantiation, NULL otherwise. */
2837 get_primary_template_innermost_parameters (const_tree t
)
2839 tree parms
= NULL
, template_info
= NULL
;
2841 if ((template_info
= get_template_info (t
))
2842 && primary_template_instantiation_p (t
))
2843 parms
= INNERMOST_TEMPLATE_PARMS
2844 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
2849 /* Return the template parameters of the LEVELth level from the full list
2850 of template parameters PARMS. */
2853 get_template_parms_at_level (tree parms
, int level
)
2857 || TREE_CODE (parms
) != TREE_LIST
2858 || level
> TMPL_PARMS_DEPTH (parms
))
2861 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
2862 if (TMPL_PARMS_DEPTH (p
) == level
)
2868 /* Returns the template arguments of T if T is a template instantiation,
2872 get_template_innermost_arguments (const_tree t
)
2874 tree args
= NULL
, template_info
= NULL
;
2876 if ((template_info
= get_template_info (t
))
2877 && TI_ARGS (template_info
))
2878 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
2883 /* Return the argument pack elements of T if T is a template argument pack,
2887 get_template_argument_pack_elems (const_tree t
)
2889 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
2890 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
2893 return ARGUMENT_PACK_ARGS (t
);
2896 /* Structure used to track the progress of find_parameter_packs_r. */
2897 struct find_parameter_pack_data
2899 /* TREE_LIST that will contain all of the parameter packs found by
2901 tree
* parameter_packs
;
2903 /* Set of AST nodes that have been visited by the traversal. */
2904 struct pointer_set_t
*visited
;
2907 /* Identifies all of the argument packs that occur in a template
2908 argument and appends them to the TREE_LIST inside DATA, which is a
2909 find_parameter_pack_data structure. This is a subroutine of
2910 make_pack_expansion and uses_parameter_packs. */
2912 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
2915 struct find_parameter_pack_data
* ppd
=
2916 (struct find_parameter_pack_data
*)data
;
2917 bool parameter_pack_p
= false;
2919 /* Identify whether this is a parameter pack or not. */
2920 switch (TREE_CODE (t
))
2922 case TEMPLATE_PARM_INDEX
:
2923 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
2924 parameter_pack_p
= true;
2927 case TEMPLATE_TYPE_PARM
:
2928 case TEMPLATE_TEMPLATE_PARM
:
2929 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
2930 parameter_pack_p
= true;
2934 if (FUNCTION_PARAMETER_PACK_P (t
))
2936 /* We don't want to walk into the type of a PARM_DECL,
2937 because we don't want to see the type parameter pack. */
2939 parameter_pack_p
= true;
2944 /* Not a parameter pack. */
2948 if (parameter_pack_p
)
2950 /* Add this parameter pack to the list. */
2951 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
2955 cp_walk_tree (&TYPE_CONTEXT (t
),
2956 &find_parameter_packs_r
, ppd
, ppd
->visited
);
2958 /* This switch statement will return immediately if we don't find a
2960 switch (TREE_CODE (t
))
2962 case TEMPLATE_PARM_INDEX
:
2965 case BOUND_TEMPLATE_TEMPLATE_PARM
:
2966 /* Check the template itself. */
2967 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
2968 &find_parameter_packs_r
, ppd
, ppd
->visited
);
2969 /* Check the template arguments. */
2970 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
2975 case TEMPLATE_TYPE_PARM
:
2976 case TEMPLATE_TEMPLATE_PARM
:
2983 if (TYPE_PTRMEMFUNC_P (t
))
2989 if (TYPE_TEMPLATE_INFO (t
))
2990 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
2991 &find_parameter_packs_r
, ppd
, ppd
->visited
);
2997 cp_walk_tree (&TREE_TYPE (t
),
2998 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3002 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3007 case TYPE_PACK_EXPANSION
:
3008 case EXPR_PACK_EXPANSION
:
3013 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3018 case IDENTIFIER_NODE
:
3019 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3031 /* Determines if the expression or type T uses any parameter packs. */
3033 uses_parameter_packs (tree t
)
3035 tree parameter_packs
= NULL_TREE
;
3036 struct find_parameter_pack_data ppd
;
3037 ppd
.parameter_packs
= ¶meter_packs
;
3038 ppd
.visited
= pointer_set_create ();
3039 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3040 pointer_set_destroy (ppd
.visited
);
3041 return parameter_packs
!= NULL_TREE
;
3044 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3045 representation a base-class initializer into a parameter pack
3046 expansion. If all goes well, the resulting node will be an
3047 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3050 make_pack_expansion (tree arg
)
3053 tree parameter_packs
= NULL_TREE
;
3054 bool for_types
= false;
3055 struct find_parameter_pack_data ppd
;
3057 if (!arg
|| arg
== error_mark_node
)
3060 if (TREE_CODE (arg
) == TREE_LIST
)
3062 /* The only time we will see a TREE_LIST here is for a base
3063 class initializer. In this case, the TREE_PURPOSE will be a
3064 _TYPE node (representing the base class expansion we're
3065 initializing) and the TREE_VALUE will be a TREE_LIST
3066 containing the initialization arguments.
3068 The resulting expansion looks somewhat different from most
3069 expansions. Rather than returning just one _EXPANSION, we
3070 return a TREE_LIST whose TREE_PURPOSE is a
3071 TYPE_PACK_EXPANSION containing the bases that will be
3072 initialized. The TREE_VALUE will be identical to the
3073 original TREE_VALUE, which is a list of arguments that will
3074 be passed to each base. We do not introduce any new pack
3075 expansion nodes into the TREE_VALUE (although it is possible
3076 that some already exist), because the TREE_PURPOSE and
3077 TREE_VALUE all need to be expanded together with the same
3078 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3079 resulting TREE_PURPOSE will mention the parameter packs in
3080 both the bases and the arguments to the bases. */
3083 tree parameter_packs
= NULL_TREE
;
3085 /* Determine which parameter packs will be used by the base
3087 ppd
.visited
= pointer_set_create ();
3088 ppd
.parameter_packs
= ¶meter_packs
;
3089 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3092 if (parameter_packs
== NULL_TREE
)
3094 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3095 pointer_set_destroy (ppd
.visited
);
3096 return error_mark_node
;
3099 if (TREE_VALUE (arg
) != void_type_node
)
3101 /* Collect the sets of parameter packs used in each of the
3102 initialization arguments. */
3103 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3105 /* Determine which parameter packs will be expanded in this
3107 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3112 pointer_set_destroy (ppd
.visited
);
3114 /* Create the pack expansion type for the base type. */
3115 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3116 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3117 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3119 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3120 they will rarely be compared to anything. */
3121 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3123 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3126 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3129 /* Build the PACK_EXPANSION_* node. */
3131 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3132 : make_node (EXPR_PACK_EXPANSION
);
3133 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3134 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3136 /* Propagate type and const-expression information. */
3137 TREE_TYPE (result
) = TREE_TYPE (arg
);
3138 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3141 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3142 they will rarely be compared to anything. */
3143 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3145 /* Determine which parameter packs will be expanded. */
3146 ppd
.parameter_packs
= ¶meter_packs
;
3147 ppd
.visited
= pointer_set_create ();
3148 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3149 pointer_set_destroy (ppd
.visited
);
3151 /* Make sure we found some parameter packs. */
3152 if (parameter_packs
== NULL_TREE
)
3155 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3157 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3158 return error_mark_node
;
3160 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3165 /* Checks T for any "bare" parameter packs, which have not yet been
3166 expanded, and issues an error if any are found. This operation can
3167 only be done on full expressions or types (e.g., an expression
3168 statement, "if" condition, etc.), because we could have expressions like:
3170 foo(f(g(h(args)))...)
3172 where "args" is a parameter pack. check_for_bare_parameter_packs
3173 should not be called for the subexpressions args, h(args),
3174 g(h(args)), or f(g(h(args))), because we would produce erroneous
3177 Returns TRUE and emits an error if there were bare parameter packs,
3178 returns FALSE otherwise. */
3180 check_for_bare_parameter_packs (tree t
)
3182 tree parameter_packs
= NULL_TREE
;
3183 struct find_parameter_pack_data ppd
;
3185 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3188 if (TREE_CODE (t
) == TYPE_DECL
)
3191 ppd
.parameter_packs
= ¶meter_packs
;
3192 ppd
.visited
= pointer_set_create ();
3193 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3194 pointer_set_destroy (ppd
.visited
);
3196 if (parameter_packs
)
3198 error ("parameter packs not expanded with %<...%>:");
3199 while (parameter_packs
)
3201 tree pack
= TREE_VALUE (parameter_packs
);
3202 tree name
= NULL_TREE
;
3204 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3205 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3206 name
= TYPE_NAME (pack
);
3207 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3208 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3210 name
= DECL_NAME (pack
);
3213 inform (input_location
, " %qD", name
);
3215 inform (input_location
, " <anonymous>");
3217 parameter_packs
= TREE_CHAIN (parameter_packs
);
3226 /* Expand any parameter packs that occur in the template arguments in
3229 expand_template_argument_pack (tree args
)
3231 tree result_args
= NULL_TREE
;
3232 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3233 int num_result_args
= -1;
3234 int non_default_args_count
= -1;
3236 /* First, determine if we need to expand anything, and the number of
3237 slots we'll need. */
3238 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3240 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3241 if (arg
== NULL_TREE
)
3243 if (ARGUMENT_PACK_P (arg
))
3245 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3246 if (num_result_args
< 0)
3247 num_result_args
= in_arg
+ num_packed
;
3249 num_result_args
+= num_packed
;
3253 if (num_result_args
>= 0)
3258 /* If no expansion is necessary, we're done. */
3259 if (num_result_args
< 0)
3262 /* Expand arguments. */
3263 result_args
= make_tree_vec (num_result_args
);
3264 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3265 non_default_args_count
=
3266 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3267 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3269 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3270 if (ARGUMENT_PACK_P (arg
))
3272 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3273 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3274 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3275 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3276 if (non_default_args_count
> 0)
3277 non_default_args_count
+= num_packed
;
3281 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3285 if (non_default_args_count
>= 0)
3286 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3290 /* Checks if DECL shadows a template parameter.
3292 [temp.local]: A template-parameter shall not be redeclared within its
3293 scope (including nested scopes).
3295 Emits an error and returns TRUE if the DECL shadows a parameter,
3296 returns FALSE otherwise. */
3299 check_template_shadow (tree decl
)
3303 /* If we're not in a template, we can't possibly shadow a template
3305 if (!current_template_parms
)
3308 /* Figure out what we're shadowing. */
3309 if (TREE_CODE (decl
) == OVERLOAD
)
3310 decl
= OVL_CURRENT (decl
);
3311 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3313 /* If there's no previous binding for this name, we're not shadowing
3314 anything, let alone a template parameter. */
3318 /* If we're not shadowing a template parameter, we're done. Note
3319 that OLDDECL might be an OVERLOAD (or perhaps even an
3320 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3322 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3325 /* We check for decl != olddecl to avoid bogus errors for using a
3326 name inside a class. We check TPFI to avoid duplicate errors for
3327 inline member templates. */
3329 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
3332 error ("declaration of %q+#D", decl
);
3333 error (" shadows template parm %q+#D", olddecl
);
3337 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3338 ORIG_LEVEL, DECL, and TYPE. */
3341 build_template_parm_index (int index
,
3347 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3348 TEMPLATE_PARM_IDX (t
) = index
;
3349 TEMPLATE_PARM_LEVEL (t
) = level
;
3350 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3351 TEMPLATE_PARM_DECL (t
) = decl
;
3352 TREE_TYPE (t
) = type
;
3353 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3354 TREE_READONLY (t
) = TREE_READONLY (decl
);
3359 /* Find the canonical type parameter for the given template type
3360 parameter. Returns the canonical type parameter, which may be TYPE
3361 if no such parameter existed. */
3363 canonical_type_parameter (tree type
)
3366 int idx
= TEMPLATE_TYPE_IDX (type
);
3367 if (!canonical_template_parms
)
3368 canonical_template_parms
= VEC_alloc (tree
, gc
, idx
+1);
3370 while (VEC_length (tree
, canonical_template_parms
) <= (unsigned)idx
)
3371 VEC_safe_push (tree
, gc
, canonical_template_parms
, NULL_TREE
);
3373 list
= VEC_index (tree
, canonical_template_parms
, idx
);
3374 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3375 list
= TREE_CHAIN (list
);
3378 return TREE_VALUE (list
);
3381 VEC_replace(tree
, canonical_template_parms
, idx
,
3382 tree_cons (NULL_TREE
, type
,
3383 VEC_index (tree
, canonical_template_parms
, idx
)));
3388 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3389 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3390 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3391 new one is created. */
3394 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3395 tsubst_flags_t complain
)
3397 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3398 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3399 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3400 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3402 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3405 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3406 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3407 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3408 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3409 DECL_ARTIFICIAL (decl
) = 1;
3410 SET_DECL_TEMPLATE_PARM_P (decl
);
3412 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3413 TEMPLATE_PARM_LEVEL (index
) - levels
,
3414 TEMPLATE_PARM_ORIG_LEVEL (index
),
3416 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3417 TEMPLATE_PARM_PARAMETER_PACK (t
)
3418 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3420 /* Template template parameters need this. */
3421 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3422 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3423 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3427 return TEMPLATE_PARM_DESCENDANTS (index
);
3430 /* Process information from new template parameter PARM and append it to the
3431 LIST being built. This new parameter is a non-type parameter iff
3432 IS_NON_TYPE is true. This new parameter is a parameter
3433 pack iff IS_PARAMETER_PACK is true. The location of PARM is in
3437 process_template_parm (tree list
, location_t parm_loc
, tree parm
, bool is_non_type
,
3438 bool is_parameter_pack
)
3445 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3446 defval
= TREE_PURPOSE (parm
);
3450 tree p
= tree_last (list
);
3452 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3455 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3456 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3458 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3468 parm
= TREE_VALUE (parm
);
3470 SET_DECL_TEMPLATE_PARM_P (parm
);
3472 if (TREE_TYPE (parm
) == error_mark_node
)
3474 err_parm_list
= build_tree_list (defval
, parm
);
3475 TREE_VALUE (err_parm_list
) = error_mark_node
;
3476 return chainon (list
, err_parm_list
);
3482 The top-level cv-qualifiers on the template-parameter are
3483 ignored when determining its type. */
3484 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3485 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3487 err_parm_list
= build_tree_list (defval
, parm
);
3488 TREE_VALUE (err_parm_list
) = error_mark_node
;
3489 return chainon (list
, err_parm_list
);
3492 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
)
3494 /* This template parameter is not a parameter pack, but it
3495 should be. Complain about "bare" parameter packs. */
3496 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3498 /* Recover by calling this a parameter pack. */
3499 is_parameter_pack
= true;
3503 /* A template parameter is not modifiable. */
3504 TREE_CONSTANT (parm
) = 1;
3505 TREE_READONLY (parm
) = 1;
3506 decl
= build_decl (parm_loc
,
3507 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3508 TREE_CONSTANT (decl
) = 1;
3509 TREE_READONLY (decl
) = 1;
3510 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3511 = build_template_parm_index (idx
, processing_template_decl
,
3512 processing_template_decl
,
3513 decl
, TREE_TYPE (parm
));
3515 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3516 = is_parameter_pack
;
3521 parm
= TREE_VALUE (TREE_VALUE (parm
));
3523 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3525 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3526 /* This is for distinguishing between real templates and template
3527 template parameters */
3528 TREE_TYPE (parm
) = t
;
3529 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3534 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3535 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3536 decl
= build_decl (parm_loc
,
3537 TYPE_DECL
, parm
, t
);
3540 TYPE_NAME (t
) = decl
;
3541 TYPE_STUB_DECL (t
) = decl
;
3543 TEMPLATE_TYPE_PARM_INDEX (t
)
3544 = build_template_parm_index (idx
, processing_template_decl
,
3545 processing_template_decl
,
3546 decl
, TREE_TYPE (parm
));
3547 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3548 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3550 DECL_ARTIFICIAL (decl
) = 1;
3551 SET_DECL_TEMPLATE_PARM_P (decl
);
3553 parm
= build_tree_list (defval
, parm
);
3554 return chainon (list
, parm
);
3557 /* The end of a template parameter list has been reached. Process the
3558 tree list into a parameter vector, converting each parameter into a more
3559 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3563 end_template_parm_list (tree parms
)
3567 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3569 current_template_parms
3570 = tree_cons (size_int (processing_template_decl
),
3571 saved_parmlist
, current_template_parms
);
3573 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3575 next
= TREE_CHAIN (parm
);
3576 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3577 TREE_CHAIN (parm
) = NULL_TREE
;
3578 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
)
3579 TEMPLATE_TYPE_PARM_SIBLING_PARMS (TREE_TYPE (TREE_VALUE (parm
))) =
3580 current_template_parms
;
3583 --processing_template_parmlist
;
3585 return saved_parmlist
;
3588 /* end_template_decl is called after a template declaration is seen. */
3591 end_template_decl (void)
3593 reset_specialization ();
3595 if (! processing_template_decl
)
3598 /* This matches the pushlevel in begin_template_parm_list. */
3601 --processing_template_decl
;
3602 current_template_parms
= TREE_CHAIN (current_template_parms
);
3605 /* Within the declaration of a template, return all levels of template
3606 parameters that apply. The template parameters are represented as
3607 a TREE_VEC, in the form documented in cp-tree.h for template
3611 current_template_args (void)
3614 tree args
= NULL_TREE
;
3615 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
3618 /* If there is only one level of template parameters, we do not
3619 create a TREE_VEC of TREE_VECs. Instead, we return a single
3620 TREE_VEC containing the arguments. */
3622 args
= make_tree_vec (length
);
3624 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
3626 tree a
= copy_node (TREE_VALUE (header
));
3629 TREE_TYPE (a
) = NULL_TREE
;
3630 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
3632 tree t
= TREE_VEC_ELT (a
, i
);
3634 /* T will be a list if we are called from within a
3635 begin/end_template_parm_list pair, but a vector directly
3636 if within a begin/end_member_template_processing pair. */
3637 if (TREE_CODE (t
) == TREE_LIST
)
3641 if (!error_operand_p (t
))
3643 if (TREE_CODE (t
) == TYPE_DECL
3644 || TREE_CODE (t
) == TEMPLATE_DECL
)
3648 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3650 /* Turn this argument into a TYPE_ARGUMENT_PACK
3651 with a single element, which expands T. */
3652 tree vec
= make_tree_vec (1);
3653 #ifdef ENABLE_CHECKING
3654 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3655 (vec
, TREE_VEC_LENGTH (vec
));
3657 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3659 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
3660 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3665 t
= DECL_INITIAL (t
);
3667 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3669 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3670 with a single element, which expands T. */
3671 tree vec
= make_tree_vec (1);
3672 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3673 #ifdef ENABLE_CHECKING
3674 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3675 (vec
, TREE_VEC_LENGTH (vec
));
3677 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3679 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3680 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3681 TREE_TYPE (t
) = type
;
3684 TREE_VEC_ELT (a
, i
) = t
;
3689 #ifdef ENABLE_CHECKING
3690 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
3694 TREE_VEC_ELT (args
, --l
) = a
;
3702 /* Update the declared TYPE by doing any lookups which were thought to be
3703 dependent, but are not now that we know the SCOPE of the declarator. */
3706 maybe_update_decl_type (tree orig_type
, tree scope
)
3708 tree type
= orig_type
;
3710 if (type
== NULL_TREE
)
3713 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3714 type
= TREE_TYPE (type
);
3716 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
3717 && dependent_type_p (type
)
3718 /* Don't bother building up the args in this case. */
3719 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
3721 /* tsubst in the args corresponding to the template parameters,
3722 including auto if present. Most things will be unchanged, but
3723 make_typename_type and tsubst_qualified_id will resolve
3724 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3725 tree args
= current_template_args ();
3726 tree auto_node
= type_uses_auto (type
);
3730 tree auto_vec
= make_tree_vec (1);
3731 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
3732 args
= add_to_template_args (args
, auto_vec
);
3734 pushed
= push_scope (scope
);
3735 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
3740 if (type
== error_mark_node
)
3743 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3745 if (same_type_p (type
, TREE_TYPE (orig_type
)))
3748 type
= TYPE_NAME (type
);
3753 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3754 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3755 a member template. Used by push_template_decl below. */
3758 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
3760 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
3761 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
3762 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
3763 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
3768 struct template_parm_data
3770 /* The level of the template parameters we are currently
3774 /* The index of the specialization argument we are currently
3778 /* An array whose size is the number of template parameters. The
3779 elements are nonzero if the parameter has been used in any one
3780 of the arguments processed so far. */
3783 /* An array whose size is the number of template arguments. The
3784 elements are nonzero if the argument makes use of template
3785 parameters of this level. */
3786 int* arg_uses_template_parms
;
3789 /* Subroutine of push_template_decl used to see if each template
3790 parameter in a partial specialization is used in the explicit
3791 argument list. If T is of the LEVEL given in DATA (which is
3792 treated as a template_parm_data*), then DATA->PARMS is marked
3796 mark_template_parm (tree t
, void* data
)
3800 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
3802 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
3804 level
= TEMPLATE_PARM_LEVEL (t
);
3805 idx
= TEMPLATE_PARM_IDX (t
);
3809 level
= TEMPLATE_TYPE_LEVEL (t
);
3810 idx
= TEMPLATE_TYPE_IDX (t
);
3813 if (level
== tpd
->level
)
3815 tpd
->parms
[idx
] = 1;
3816 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
3819 /* Return zero so that for_each_template_parm will continue the
3820 traversal of the tree; we want to mark *every* template parm. */
3824 /* Process the partial specialization DECL. */
3827 process_partial_specialization (tree decl
)
3829 tree type
= TREE_TYPE (decl
);
3830 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
3831 tree specargs
= CLASSTYPE_TI_ARGS (type
);
3832 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
3833 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
3835 int nargs
= TREE_VEC_LENGTH (inner_args
);
3838 bool did_error_intro
= false;
3839 struct template_parm_data tpd
;
3840 struct template_parm_data tpd2
;
3842 gcc_assert (current_template_parms
);
3844 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
3845 ntparms
= TREE_VEC_LENGTH (inner_parms
);
3847 /* We check that each of the template parameters given in the
3848 partial specialization is used in the argument list to the
3849 specialization. For example:
3851 template <class T> struct S;
3852 template <class T> struct S<T*>;
3854 The second declaration is OK because `T*' uses the template
3855 parameter T, whereas
3857 template <class T> struct S<int>;
3859 is no good. Even trickier is:
3870 The S2<T> declaration is actually invalid; it is a
3871 full-specialization. Of course,
3874 struct S2<T (*)(U)>;
3876 or some such would have been OK. */
3877 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
3878 tpd
.parms
= (int *) alloca (sizeof (int) * ntparms
);
3879 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
3881 tpd
.arg_uses_template_parms
= (int *) alloca (sizeof (int) * nargs
);
3882 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
3883 for (i
= 0; i
< nargs
; ++i
)
3885 tpd
.current_arg
= i
;
3886 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
3887 &mark_template_parm
,
3890 /*include_nondeduced_p=*/false);
3892 for (i
= 0; i
< ntparms
; ++i
)
3893 if (tpd
.parms
[i
] == 0)
3895 /* One of the template parms was not used in the
3897 if (!did_error_intro
)
3899 error ("template parameters not used in partial specialization:");
3900 did_error_intro
= true;
3903 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
3906 if (did_error_intro
)
3907 return error_mark_node
;
3909 /* [temp.class.spec]
3911 The argument list of the specialization shall not be identical to
3912 the implicit argument list of the primary template. */
3913 if (comp_template_args
3915 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3917 error ("partial specialization %qT does not specialize any template arguments", type
);
3919 /* [temp.class.spec]
3921 A partially specialized non-type argument expression shall not
3922 involve template parameters of the partial specialization except
3923 when the argument expression is a simple identifier.
3925 The type of a template parameter corresponding to a specialized
3926 non-type argument shall not be dependent on a parameter of the
3929 Also, we verify that pack expansions only occur at the
3930 end of the argument list. */
3931 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
3933 for (i
= 0; i
< nargs
; ++i
)
3935 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
3936 tree arg
= TREE_VEC_ELT (inner_args
, i
);
3937 tree packed_args
= NULL_TREE
;
3940 if (ARGUMENT_PACK_P (arg
))
3942 /* Extract the arguments from the argument pack. We'll be
3943 iterating over these in the following loop. */
3944 packed_args
= ARGUMENT_PACK_ARGS (arg
);
3945 len
= TREE_VEC_LENGTH (packed_args
);
3948 for (j
= 0; j
< len
; j
++)
3951 /* Get the Jth argument in the parameter pack. */
3952 arg
= TREE_VEC_ELT (packed_args
, j
);
3954 if (PACK_EXPANSION_P (arg
))
3956 /* Pack expansions must come at the end of the
3958 if ((packed_args
&& j
< len
- 1)
3959 || (!packed_args
&& i
< nargs
- 1))
3961 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
3962 error ("parameter pack argument %qE must be at the "
3963 "end of the template argument list", arg
);
3965 error ("parameter pack argument %qT must be at the "
3966 "end of the template argument list", arg
);
3970 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
3971 /* We only care about the pattern. */
3972 arg
= PACK_EXPANSION_PATTERN (arg
);
3974 if (/* These first two lines are the `non-type' bit. */
3976 && TREE_CODE (arg
) != TEMPLATE_DECL
3977 /* This next line is the `argument expression is not just a
3978 simple identifier' condition and also the `specialized
3979 non-type argument' bit. */
3980 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
3982 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
3983 || (packed_args
&& uses_template_parms (arg
)))
3984 error ("template argument %qE involves template parameter(s)",
3988 /* Look at the corresponding template parameter,
3989 marking which template parameters its type depends
3991 tree type
= TREE_TYPE (parm
);
3995 /* We haven't yet initialized TPD2. Do so now. */
3996 tpd2
.arg_uses_template_parms
3997 = (int *) alloca (sizeof (int) * nargs
);
3998 /* The number of parameters here is the number in the
3999 main template, which, as checked in the assertion
4001 tpd2
.parms
= (int *) alloca (sizeof (int) * nargs
);
4003 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4006 /* Mark the template parameters. But this time, we're
4007 looking for the template parameters of the main
4008 template, not in the specialization. */
4009 tpd2
.current_arg
= i
;
4010 tpd2
.arg_uses_template_parms
[i
] = 0;
4011 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4012 for_each_template_parm (type
,
4013 &mark_template_parm
,
4016 /*include_nondeduced_p=*/false);
4018 if (tpd2
.arg_uses_template_parms
[i
])
4020 /* The type depended on some template parameters.
4021 If they are fully specialized in the
4022 specialization, that's OK. */
4025 for (j
= 0; j
< nargs
; ++j
)
4026 if (tpd2
.parms
[j
] != 0
4027 && tpd
.arg_uses_template_parms
[j
])
4030 error_n (input_location
, count
,
4031 "type %qT of template argument %qE depends "
4032 "on a template parameter",
4033 "type %qT of template argument %qE depends "
4034 "on template parameters",
4043 /* We should only get here once. */
4044 gcc_assert (!COMPLETE_TYPE_P (type
));
4046 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4047 = tree_cons (specargs
, inner_parms
,
4048 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4049 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4053 /* Check that a template declaration's use of default arguments and
4054 parameter packs is not invalid. Here, PARMS are the template
4055 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
4056 a primary template. IS_PARTIAL is nonzero if DECL is a partial
4060 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4061 declaration (but not a definition); 1 indicates a declaration, 2
4062 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4063 emitted for extraneous default arguments.
4065 Returns TRUE if there were no errors found, FALSE otherwise. */
4068 check_default_tmpl_args (tree decl
, tree parms
, int is_primary
,
4069 int is_partial
, int is_friend_decl
)
4072 int last_level_to_check
;
4074 bool no_errors
= true;
4078 A default template-argument shall not be specified in a
4079 function template declaration or a function template definition, nor
4080 in the template-parameter-list of the definition of a member of a
4083 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
4084 /* You can't have a function template declaration in a local
4085 scope, nor you can you define a member of a class template in a
4089 if (current_class_type
4090 && !TYPE_BEING_DEFINED (current_class_type
)
4091 && DECL_LANG_SPECIFIC (decl
)
4092 && DECL_DECLARES_FUNCTION_P (decl
)
4093 /* If this is either a friend defined in the scope of the class
4094 or a member function. */
4095 && (DECL_FUNCTION_MEMBER_P (decl
)
4096 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4097 : DECL_FRIEND_CONTEXT (decl
)
4098 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4100 /* And, if it was a member function, it really was defined in
4101 the scope of the class. */
4102 && (!DECL_FUNCTION_MEMBER_P (decl
)
4103 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4104 /* We already checked these parameters when the template was
4105 declared, so there's no need to do it again now. This function
4106 was defined in class scope, but we're processing it's body now
4107 that the class is complete. */
4110 /* Core issue 226 (C++0x only): the following only applies to class
4112 if ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
)
4116 If a template-parameter has a default template-argument, all
4117 subsequent template-parameters shall have a default
4118 template-argument supplied. */
4119 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4121 tree inner_parms
= TREE_VALUE (parm_level
);
4122 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4123 int seen_def_arg_p
= 0;
4126 for (i
= 0; i
< ntparms
; ++i
)
4128 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4130 if (parm
== error_mark_node
)
4133 if (TREE_PURPOSE (parm
))
4135 else if (seen_def_arg_p
4136 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4138 error ("no default argument for %qD", TREE_VALUE (parm
));
4139 /* For better subsequent error-recovery, we indicate that
4140 there should have been a default argument. */
4141 TREE_PURPOSE (parm
) = error_mark_node
;
4147 /* Don't complain about an enclosing partial
4149 && parm_level
== parms
4150 && TREE_CODE (decl
) == TYPE_DECL
4152 && template_parameter_pack_p (TREE_VALUE (parm
)))
4154 /* A primary class template can only have one
4155 parameter pack, at the end of the template
4158 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
4159 error ("parameter pack %qE must be at the end of the"
4160 " template parameter list", TREE_VALUE (parm
));
4162 error ("parameter pack %qT must be at the end of the"
4163 " template parameter list",
4164 TREE_TYPE (TREE_VALUE (parm
)));
4166 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4174 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4178 /* For an ordinary class template, default template arguments are
4179 allowed at the innermost level, e.g.:
4180 template <class T = int>
4182 but, in a partial specialization, they're not allowed even
4183 there, as we have in [temp.class.spec]:
4185 The template parameter list of a specialization shall not
4186 contain default template argument values.
4188 So, for a partial specialization, or for a function template
4189 (in C++98/C++03), we look at all of them. */
4192 /* But, for a primary class template that is not a partial
4193 specialization we look at all template parameters except the
4195 parms
= TREE_CHAIN (parms
);
4197 /* Figure out what error message to issue. */
4198 if (is_friend_decl
== 2)
4199 msg
= G_("default template arguments may not be used in function template "
4200 "friend re-declaration");
4201 else if (is_friend_decl
)
4202 msg
= G_("default template arguments may not be used in function template "
4203 "friend declarations");
4204 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4205 msg
= G_("default template arguments may not be used in function templates "
4206 "without -std=c++0x or -std=gnu++0x");
4207 else if (is_partial
)
4208 msg
= G_("default template arguments may not be used in "
4209 "partial specializations");
4211 msg
= G_("default argument for template parameter for class enclosing %qD");
4213 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4214 /* If we're inside a class definition, there's no need to
4215 examine the parameters to the class itself. On the one
4216 hand, they will be checked when the class is defined, and,
4217 on the other, default arguments are valid in things like:
4218 template <class T = double>
4219 struct S { template <class U> void f(U); };
4220 Here the default argument for `S' has no bearing on the
4221 declaration of `f'. */
4222 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4224 /* Check everything. */
4225 last_level_to_check
= 0;
4227 for (parm_level
= parms
;
4228 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4229 parm_level
= TREE_CHAIN (parm_level
))
4231 tree inner_parms
= TREE_VALUE (parm_level
);
4235 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4236 for (i
= 0; i
< ntparms
; ++i
)
4238 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4241 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4246 if (is_friend_decl
== 2)
4253 /* Clear out the default argument so that we are not
4255 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4259 /* At this point, if we're still interested in issuing messages,
4260 they must apply to classes surrounding the object declared. */
4262 msg
= G_("default argument for template parameter for class "
4269 /* Worker for push_template_decl_real, called via
4270 for_each_template_parm. DATA is really an int, indicating the
4271 level of the parameters we are interested in. If T is a template
4272 parameter of that level, return nonzero. */
4275 template_parm_this_level_p (tree t
, void* data
)
4277 int this_level
= *(int *)data
;
4280 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4281 level
= TEMPLATE_PARM_LEVEL (t
);
4283 level
= TEMPLATE_TYPE_LEVEL (t
);
4284 return level
== this_level
;
4287 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4288 parameters given by current_template_args, or reuses a
4289 previously existing one, if appropriate. Returns the DECL, or an
4290 equivalent one, if it is replaced via a call to duplicate_decls.
4292 If IS_FRIEND is true, DECL is a friend declaration. */
4295 push_template_decl_real (tree decl
, bool is_friend
)
4303 int new_template_p
= 0;
4304 /* True if the template is a member template, in the sense of
4306 bool member_template_p
= false;
4308 if (decl
== error_mark_node
|| !current_template_parms
)
4309 return error_mark_node
;
4311 /* See if this is a partial specialization. */
4312 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
4313 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4314 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
4316 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4320 /* For a friend, we want the context of the friend function, not
4321 the type of which it is a friend. */
4322 ctx
= DECL_CONTEXT (decl
);
4323 else if (CP_DECL_CONTEXT (decl
)
4324 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4325 /* In the case of a virtual function, we want the class in which
4327 ctx
= CP_DECL_CONTEXT (decl
);
4329 /* Otherwise, if we're currently defining some class, the DECL
4330 is assumed to be a member of the class. */
4331 ctx
= current_scope ();
4333 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4336 if (!DECL_CONTEXT (decl
))
4337 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4339 /* See if this is a primary template. */
4340 if (is_friend
&& ctx
)
4341 /* A friend template that specifies a class context, i.e.
4342 template <typename T> friend void A<T>::f();
4346 primary
= template_parm_scope_p ();
4350 if (DECL_CLASS_SCOPE_P (decl
))
4351 member_template_p
= true;
4352 if (TREE_CODE (decl
) == TYPE_DECL
4353 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4355 error ("template class without a name");
4356 return error_mark_node
;
4358 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4360 if (DECL_DESTRUCTOR_P (decl
))
4364 A destructor shall not be a member template. */
4365 error ("destructor %qD declared as member template", decl
);
4366 return error_mark_node
;
4368 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4369 && (!TYPE_ARG_TYPES (TREE_TYPE (decl
))
4370 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4371 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4372 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4373 == void_list_node
)))
4375 /* [basic.stc.dynamic.allocation]
4377 An allocation function can be a function
4378 template. ... Template allocation functions shall
4379 have two or more parameters. */
4380 error ("invalid template declaration of %qD", decl
);
4381 return error_mark_node
;
4384 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4385 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4389 error ("template declaration of %q#D", decl
);
4390 return error_mark_node
;
4394 /* Check to see that the rules regarding the use of default
4395 arguments are not being violated. */
4396 check_default_tmpl_args (decl
, current_template_parms
,
4397 primary
, is_partial
, /*is_friend_decl=*/0);
4399 /* Ensure that there are no parameter packs in the type of this
4400 declaration that have not been expanded. */
4401 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4403 /* Check each of the arguments individually to see if there are
4404 any bare parameter packs. */
4405 tree type
= TREE_TYPE (decl
);
4406 tree arg
= DECL_ARGUMENTS (decl
);
4407 tree argtype
= TYPE_ARG_TYPES (type
);
4409 while (arg
&& argtype
)
4411 if (!FUNCTION_PARAMETER_PACK_P (arg
)
4412 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4414 /* This is a PARM_DECL that contains unexpanded parameter
4415 packs. We have already complained about this in the
4416 check_for_bare_parameter_packs call, so just replace
4417 these types with ERROR_MARK_NODE. */
4418 TREE_TYPE (arg
) = error_mark_node
;
4419 TREE_VALUE (argtype
) = error_mark_node
;
4422 arg
= TREE_CHAIN (arg
);
4423 argtype
= TREE_CHAIN (argtype
);
4426 /* Check for bare parameter packs in the return type and the
4427 exception specifiers. */
4428 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4429 /* Errors were already issued, set return type to int
4430 as the frontend doesn't expect error_mark_node as
4432 TREE_TYPE (type
) = integer_type_node
;
4433 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4434 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4436 else if (check_for_bare_parameter_packs (TREE_TYPE (decl
)))
4438 TREE_TYPE (decl
) = error_mark_node
;
4439 return error_mark_node
;
4443 return process_partial_specialization (decl
);
4445 args
= current_template_args ();
4448 || TREE_CODE (ctx
) == FUNCTION_DECL
4449 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4450 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4452 if (DECL_LANG_SPECIFIC (decl
)
4453 && DECL_TEMPLATE_INFO (decl
)
4454 && DECL_TI_TEMPLATE (decl
))
4455 tmpl
= DECL_TI_TEMPLATE (decl
);
4456 /* If DECL is a TYPE_DECL for a class-template, then there won't
4457 be DECL_LANG_SPECIFIC. The information equivalent to
4458 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4459 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4460 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4461 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4463 /* Since a template declaration already existed for this
4464 class-type, we must be redeclaring it here. Make sure
4465 that the redeclaration is valid. */
4466 redeclare_class_template (TREE_TYPE (decl
),
4467 current_template_parms
);
4468 /* We don't need to create a new TEMPLATE_DECL; just use the
4469 one we already had. */
4470 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4474 tmpl
= build_template_decl (decl
, current_template_parms
,
4478 if (DECL_LANG_SPECIFIC (decl
)
4479 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4481 /* A specialization of a member template of a template
4483 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4484 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4485 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4491 tree a
, t
, current
, parms
;
4493 tree tinfo
= get_template_info (decl
);
4497 error ("template definition of non-template %q#D", decl
);
4498 return error_mark_node
;
4501 tmpl
= TI_TEMPLATE (tinfo
);
4503 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4504 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4505 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4506 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4510 /* The declaration is a specialization of a member
4511 template, declared outside the class. Therefore, the
4512 innermost template arguments will be NULL, so we
4513 replace them with the arguments determined by the
4514 earlier call to check_explicit_specialization. */
4515 args
= DECL_TI_ARGS (decl
);
4518 = build_template_decl (decl
, current_template_parms
,
4520 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4521 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4522 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4523 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4524 DECL_TEMPLATE_INFO (new_tmpl
)
4525 = build_template_info (tmpl
, args
);
4527 register_specialization (new_tmpl
,
4528 most_general_template (tmpl
),
4534 /* Make sure the template headers we got make sense. */
4536 parms
= DECL_TEMPLATE_PARMS (tmpl
);
4537 i
= TMPL_PARMS_DEPTH (parms
);
4538 if (TMPL_ARGS_DEPTH (args
) != i
)
4540 error ("expected %d levels of template parms for %q#D, got %d",
4541 i
, decl
, TMPL_ARGS_DEPTH (args
));
4544 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
4546 a
= TMPL_ARGS_LEVEL (args
, i
);
4547 t
= INNERMOST_TEMPLATE_PARMS (parms
);
4549 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
4551 if (current
== decl
)
4552 error ("got %d template parameters for %q#D",
4553 TREE_VEC_LENGTH (a
), decl
);
4555 error ("got %d template parameters for %q#T",
4556 TREE_VEC_LENGTH (a
), current
);
4557 error (" but %d required", TREE_VEC_LENGTH (t
));
4558 return error_mark_node
;
4561 if (current
== decl
)
4563 else if (current
== NULL_TREE
)
4564 /* Can happen in erroneous input. */
4567 current
= (TYPE_P (current
)
4568 ? TYPE_CONTEXT (current
)
4569 : DECL_CONTEXT (current
));
4572 /* Check that the parms are used in the appropriate qualifying scopes
4573 in the declarator. */
4574 if (!comp_template_args
4576 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
4579 template arguments to %qD do not match original template %qD",
4580 decl
, DECL_TEMPLATE_RESULT (tmpl
));
4581 if (!uses_template_parms (TI_ARGS (tinfo
)))
4582 inform (input_location
, "use template<> for an explicit specialization");
4583 /* Avoid crash in import_export_decl. */
4584 DECL_INTERFACE_KNOWN (decl
) = 1;
4585 return error_mark_node
;
4589 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4590 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4592 /* Push template declarations for global functions and types. Note
4593 that we do not try to push a global template friend declared in a
4594 template class; such a thing may well depend on the template
4595 parameters of the class. */
4596 if (new_template_p
&& !ctx
4597 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
4599 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
4600 if (tmpl
== error_mark_node
)
4601 return error_mark_node
;
4603 /* Hide template friend classes that haven't been declared yet. */
4604 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
4606 DECL_ANTICIPATED (tmpl
) = 1;
4607 DECL_FRIEND_P (tmpl
) = 1;
4613 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
4616 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4617 if (DECL_CONV_FN_P (tmpl
))
4619 int depth
= TMPL_PARMS_DEPTH (parms
);
4621 /* It is a conversion operator. See if the type converted to
4622 depends on innermost template operands. */
4624 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
4626 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
4629 /* Give template template parms a DECL_CONTEXT of the template
4630 for which they are a parameter. */
4631 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4632 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
4634 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4635 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4636 DECL_CONTEXT (parm
) = tmpl
;
4640 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4641 back to its most general template. If TMPL is a specialization,
4642 ARGS may only have the innermost set of arguments. Add the missing
4643 argument levels if necessary. */
4644 if (DECL_TEMPLATE_INFO (tmpl
))
4645 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
4647 info
= build_template_info (tmpl
, args
);
4649 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
4650 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
4651 else if (DECL_LANG_SPECIFIC (decl
))
4652 DECL_TEMPLATE_INFO (decl
) = info
;
4654 return DECL_TEMPLATE_RESULT (tmpl
);
4658 push_template_decl (tree decl
)
4660 return push_template_decl_real (decl
, false);
4663 /* Called when a class template TYPE is redeclared with the indicated
4664 template PARMS, e.g.:
4666 template <class T> struct S;
4667 template <class T> struct S {}; */
4670 redeclare_class_template (tree type
, tree parms
)
4676 if (!TYPE_TEMPLATE_INFO (type
))
4678 error ("%qT is not a template type", type
);
4682 tmpl
= TYPE_TI_TEMPLATE (type
);
4683 if (!PRIMARY_TEMPLATE_P (tmpl
))
4684 /* The type is nested in some template class. Nothing to worry
4685 about here; there are no new template parameters for the nested
4691 error ("template specifiers not specified in declaration of %qD",
4696 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4697 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
4699 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
4701 error_n (input_location
, TREE_VEC_LENGTH (parms
),
4702 "redeclared with %d template parameter",
4703 "redeclared with %d template parameters",
4704 TREE_VEC_LENGTH (parms
));
4705 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
4706 "previous declaration %q+D used %d template parameter",
4707 "previous declaration %q+D used %d template parameters",
4708 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
4712 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
4719 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
4720 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
4723 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
4724 if (tmpl_parm
== error_mark_node
)
4727 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4728 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
4729 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
4731 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4733 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
4734 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
4735 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
4736 || (TREE_CODE (tmpl_parm
) != PARM_DECL
4737 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
4738 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
4739 || (TREE_CODE (tmpl_parm
) == PARM_DECL
4740 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
4741 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
4743 error ("template parameter %q+#D", tmpl_parm
);
4744 error ("redeclared here as %q#D", parm
);
4748 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
4750 /* We have in [temp.param]:
4752 A template-parameter may not be given default arguments
4753 by two different declarations in the same scope. */
4754 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
4755 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
4756 "original definition appeared here");
4760 if (parm_default
!= NULL_TREE
)
4761 /* Update the previous template parameters (which are the ones
4762 that will really count) with the new default value. */
4763 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
4764 else if (tmpl_default
!= NULL_TREE
)
4765 /* Update the new parameters, too; they'll be used as the
4766 parameters for any members. */
4767 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
4773 /* Simplify EXPR if it is a non-dependent expression. Returns the
4774 (possibly simplified) expression. */
4777 fold_non_dependent_expr (tree expr
)
4779 if (expr
== NULL_TREE
)
4782 /* If we're in a template, but EXPR isn't value dependent, simplify
4783 it. We're supposed to treat:
4785 template <typename T> void f(T[1 + 1]);
4786 template <typename T> void f(T[2]);
4788 as two declarations of the same function, for example. */
4789 if (processing_template_decl
4790 && !type_dependent_expression_p (expr
)
4791 && !value_dependent_expression_p (expr
))
4793 HOST_WIDE_INT saved_processing_template_decl
;
4795 saved_processing_template_decl
= processing_template_decl
;
4796 processing_template_decl
= 0;
4797 expr
= tsubst_copy_and_build (expr
,
4800 /*in_decl=*/NULL_TREE
,
4801 /*function_p=*/false,
4802 /*integral_constant_expression_p=*/true);
4803 processing_template_decl
= saved_processing_template_decl
;
4808 /* EXPR is an expression which is used in a constant-expression context.
4809 For instance, it could be a VAR_DECL with a constant initializer.
4810 Extract the innermost constant expression.
4812 This is basically a more powerful version of
4813 integral_constant_value, which can be used also in templates where
4814 initializers can maintain a syntactic rather than semantic form
4815 (even if they are non-dependent, for access-checking purposes). */
4818 fold_decl_constant_value (tree expr
)
4820 tree const_expr
= expr
;
4823 expr
= fold_non_dependent_expr (const_expr
);
4824 const_expr
= integral_constant_value (expr
);
4826 while (expr
!= const_expr
);
4831 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4832 must be a function or a pointer-to-function type, as specified
4833 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4834 and check that the resulting function has external linkage. */
4837 convert_nontype_argument_function (tree type
, tree expr
)
4842 fn
= instantiate_type (type
, fns
, tf_none
);
4843 if (fn
== error_mark_node
)
4844 return error_mark_node
;
4847 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
4848 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
4849 if (TREE_CODE (fn_no_ptr
) == BASELINK
)
4850 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
4852 /* [temp.arg.nontype]/1
4854 A template-argument for a non-type, non-template template-parameter
4857 -- the address of an object or function with external linkage. */
4858 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr
))
4860 error ("%qE is not a valid template argument for type %qT "
4861 "because function %qD has not external linkage",
4862 expr
, type
, fn_no_ptr
);
4869 /* Subroutine of convert_nontype_argument.
4870 Check if EXPR of type TYPE is a valid pointer-to-member constant.
4871 Emit an error otherwise. */
4874 check_valid_ptrmem_cst_expr (tree type
, tree expr
)
4877 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
4879 error ("%qE is not a valid template argument for type %qT",
4881 error ("it must be a pointer-to-member of the form `&X::Y'");
4885 /* Attempt to convert the non-type template parameter EXPR to the
4886 indicated TYPE. If the conversion is successful, return the
4887 converted value. If the conversion is unsuccessful, return
4888 NULL_TREE if we issued an error message, or error_mark_node if we
4889 did not. We issue error messages for out-and-out bad template
4890 parameters, but not simply because the conversion failed, since we
4891 might be just trying to do argument deduction. Both TYPE and EXPR
4892 must be non-dependent.
4894 The conversion follows the special rules described in
4895 [temp.arg.nontype], and it is much more strict than an implicit
4898 This function is called twice for each template argument (see
4899 lookup_template_class for a more accurate description of this
4900 problem). This means that we need to handle expressions which
4901 are not valid in a C++ source, but can be created from the
4902 first call (for instance, casts to perform conversions). These
4903 hacks can go away after we fix the double coercion problem. */
4906 convert_nontype_argument (tree type
, tree expr
)
4910 /* Detect immediately string literals as invalid non-type argument.
4911 This special-case is not needed for correctness (we would easily
4912 catch this later), but only to provide better diagnostic for this
4913 common user mistake. As suggested by DR 100, we do not mention
4914 linkage issues in the diagnostic as this is not the point. */
4915 if (TREE_CODE (expr
) == STRING_CST
)
4917 error ("%qE is not a valid template argument for type %qT "
4918 "because string literals can never be used in this context",
4923 /* If we are in a template, EXPR may be non-dependent, but still
4924 have a syntactic, rather than semantic, form. For example, EXPR
4925 might be a SCOPE_REF, rather than the VAR_DECL to which the
4926 SCOPE_REF refers. Preserving the qualifying scope is necessary
4927 so that access checking can be performed when the template is
4928 instantiated -- but here we need the resolved form so that we can
4929 convert the argument. */
4930 expr
= fold_non_dependent_expr (expr
);
4931 if (error_operand_p (expr
))
4932 return error_mark_node
;
4933 expr_type
= TREE_TYPE (expr
);
4934 expr
= mark_rvalue_use (expr
);
4936 /* HACK: Due to double coercion, we can get a
4937 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
4938 which is the tree that we built on the first call (see
4939 below when coercing to reference to object or to reference to
4940 function). We just strip everything and get to the arg.
4941 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
4943 if (TREE_CODE (expr
) == NOP_EXPR
)
4945 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
4947 /* ??? Maybe we could use convert_from_reference here, but we
4948 would need to relax its constraints because the NOP_EXPR
4949 could actually change the type to something more cv-qualified,
4950 and this is not folded by convert_from_reference. */
4951 tree addr
= TREE_OPERAND (expr
, 0);
4952 gcc_assert (TREE_CODE (expr_type
) == REFERENCE_TYPE
);
4953 gcc_assert (TREE_CODE (addr
) == ADDR_EXPR
);
4954 gcc_assert (TREE_CODE (TREE_TYPE (addr
)) == POINTER_TYPE
);
4955 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4956 (TREE_TYPE (expr_type
),
4957 TREE_TYPE (TREE_TYPE (addr
))));
4959 expr
= TREE_OPERAND (addr
, 0);
4960 expr_type
= TREE_TYPE (expr
);
4963 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
4964 parameter is a pointer to object, through decay and
4965 qualification conversion. Let's strip everything. */
4966 else if (TYPE_PTROBV_P (type
))
4969 gcc_assert (TREE_CODE (expr
) == ADDR_EXPR
);
4970 gcc_assert (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
);
4971 /* Skip the ADDR_EXPR only if it is part of the decay for
4972 an array. Otherwise, it is part of the original argument
4973 in the source code. */
4974 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == ARRAY_TYPE
)
4975 expr
= TREE_OPERAND (expr
, 0);
4976 expr_type
= TREE_TYPE (expr
);
4980 /* [temp.arg.nontype]/5, bullet 1
4982 For a non-type template-parameter of integral or enumeration type,
4983 integral promotions (_conv.prom_) and integral conversions
4984 (_conv.integral_) are applied. */
4985 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
4987 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (expr_type
))
4988 return error_mark_node
;
4990 expr
= fold_decl_constant_value (expr
);
4991 /* Notice that there are constant expressions like '4 % 0' which
4992 do not fold into integer constants. */
4993 if (TREE_CODE (expr
) != INTEGER_CST
)
4995 error ("%qE is not a valid template argument for type %qT "
4996 "because it is a non-constant expression", expr
, type
);
5000 /* At this point, an implicit conversion does what we want,
5001 because we already know that the expression is of integral
5003 expr
= ocp_convert (type
, expr
, CONV_IMPLICIT
, LOOKUP_PROTECT
);
5004 if (expr
== error_mark_node
)
5005 return error_mark_node
;
5007 /* Conversion was allowed: fold it to a bare integer constant. */
5010 /* [temp.arg.nontype]/5, bullet 2
5012 For a non-type template-parameter of type pointer to object,
5013 qualification conversions (_conv.qual_) and the array-to-pointer
5014 conversion (_conv.array_) are applied. */
5015 else if (TYPE_PTROBV_P (type
))
5017 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5019 A template-argument for a non-type, non-template template-parameter
5020 shall be one of: [...]
5022 -- the name of a non-type template-parameter;
5023 -- the address of an object or function with external linkage, [...]
5024 expressed as "& id-expression" where the & is optional if the name
5025 refers to a function or array, or if the corresponding
5026 template-parameter is a reference.
5028 Here, we do not care about functions, as they are invalid anyway
5029 for a parameter of type pointer-to-object. */
5031 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5032 /* Non-type template parameters are OK. */
5034 else if (TREE_CODE (expr
) != ADDR_EXPR
5035 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5037 if (TREE_CODE (expr
) == VAR_DECL
)
5039 error ("%qD is not a valid template argument "
5040 "because %qD is a variable, not the address of "
5045 /* Other values, like integer constants, might be valid
5046 non-type arguments of some other type. */
5047 return error_mark_node
;
5053 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
5054 ? TREE_OPERAND (expr
, 0) : expr
);
5055 if (TREE_CODE (decl
) != VAR_DECL
)
5057 error ("%qE is not a valid template argument of type %qT "
5058 "because %qE is not a variable",
5062 else if (!DECL_EXTERNAL_LINKAGE_P (decl
))
5064 error ("%qE is not a valid template argument of type %qT "
5065 "because %qD does not have external linkage",
5071 expr
= decay_conversion (expr
);
5072 if (expr
== error_mark_node
)
5073 return error_mark_node
;
5075 expr
= perform_qualification_conversions (type
, expr
);
5076 if (expr
== error_mark_node
)
5077 return error_mark_node
;
5079 /* [temp.arg.nontype]/5, bullet 3
5081 For a non-type template-parameter of type reference to object, no
5082 conversions apply. The type referred to by the reference may be more
5083 cv-qualified than the (otherwise identical) type of the
5084 template-argument. The template-parameter is bound directly to the
5085 template-argument, which must be an lvalue. */
5086 else if (TYPE_REF_OBJ_P (type
))
5088 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
5090 return error_mark_node
;
5092 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5094 error ("%qE is not a valid template argument for type %qT "
5095 "because of conflicts in cv-qualification", expr
, type
);
5099 if (!real_lvalue_p (expr
))
5101 error ("%qE is not a valid template argument for type %qT "
5102 "because it is not an lvalue", expr
, type
);
5106 /* [temp.arg.nontype]/1
5108 A template-argument for a non-type, non-template template-parameter
5109 shall be one of: [...]
5111 -- the address of an object or function with external linkage. */
5112 if (TREE_CODE (expr
) == INDIRECT_REF
5113 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5115 expr
= TREE_OPERAND (expr
, 0);
5118 error ("%q#D is not a valid template argument for type %qT "
5119 "because a reference variable does not have a constant "
5120 "address", expr
, type
);
5127 error ("%qE is not a valid template argument for type %qT "
5128 "because it is not an object with external linkage",
5133 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5135 error ("%qE is not a valid template argument for type %qT "
5136 "because object %qD has not external linkage",
5141 expr
= build_nop (type
, build_address (expr
));
5143 /* [temp.arg.nontype]/5, bullet 4
5145 For a non-type template-parameter of type pointer to function, only
5146 the function-to-pointer conversion (_conv.func_) is applied. If the
5147 template-argument represents a set of overloaded functions (or a
5148 pointer to such), the matching function is selected from the set
5150 else if (TYPE_PTRFN_P (type
))
5152 /* If the argument is a template-id, we might not have enough
5153 context information to decay the pointer. */
5154 if (!type_unknown_p (expr_type
))
5156 expr
= decay_conversion (expr
);
5157 if (expr
== error_mark_node
)
5158 return error_mark_node
;
5161 expr
= convert_nontype_argument_function (type
, expr
);
5162 if (!expr
|| expr
== error_mark_node
)
5165 if (TREE_CODE (expr
) != ADDR_EXPR
)
5167 error ("%qE is not a valid template argument for type %qT", expr
, type
);
5168 error ("it must be the address of a function with external linkage");
5172 /* [temp.arg.nontype]/5, bullet 5
5174 For a non-type template-parameter of type reference to function, no
5175 conversions apply. If the template-argument represents a set of
5176 overloaded functions, the matching function is selected from the set
5178 else if (TYPE_REFFN_P (type
))
5180 if (TREE_CODE (expr
) == ADDR_EXPR
)
5182 error ("%qE is not a valid template argument for type %qT "
5183 "because it is a pointer", expr
, type
);
5184 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
5188 expr
= convert_nontype_argument_function (TREE_TYPE (type
), expr
);
5189 if (!expr
|| expr
== error_mark_node
)
5192 expr
= build_nop (type
, build_address (expr
));
5194 /* [temp.arg.nontype]/5, bullet 6
5196 For a non-type template-parameter of type pointer to member function,
5197 no conversions apply. If the template-argument represents a set of
5198 overloaded member functions, the matching member function is selected
5199 from the set (_over.over_). */
5200 else if (TYPE_PTRMEMFUNC_P (type
))
5202 expr
= instantiate_type (type
, expr
, tf_none
);
5203 if (expr
== error_mark_node
)
5204 return error_mark_node
;
5206 /* [temp.arg.nontype] bullet 1 says the pointer to member
5207 expression must be a pointer-to-member constant. */
5208 if (!check_valid_ptrmem_cst_expr (type
, expr
))
5209 return error_mark_node
;
5211 /* There is no way to disable standard conversions in
5212 resolve_address_of_overloaded_function (called by
5213 instantiate_type). It is possible that the call succeeded by
5214 converting &B::I to &D::I (where B is a base of D), so we need
5215 to reject this conversion here.
5217 Actually, even if there was a way to disable standard conversions,
5218 it would still be better to reject them here so that we can
5219 provide a superior diagnostic. */
5220 if (!same_type_p (TREE_TYPE (expr
), type
))
5222 error ("%qE is not a valid template argument for type %qT "
5223 "because it is of type %qT", expr
, type
,
5225 /* If we are just one standard conversion off, explain. */
5226 if (can_convert (type
, TREE_TYPE (expr
)))
5227 inform (input_location
,
5228 "standard conversions are not allowed in this context");
5232 /* [temp.arg.nontype]/5, bullet 7
5234 For a non-type template-parameter of type pointer to data member,
5235 qualification conversions (_conv.qual_) are applied. */
5236 else if (TYPE_PTRMEM_P (type
))
5238 /* [temp.arg.nontype] bullet 1 says the pointer to member
5239 expression must be a pointer-to-member constant. */
5240 if (!check_valid_ptrmem_cst_expr (type
, expr
))
5241 return error_mark_node
;
5243 expr
= perform_qualification_conversions (type
, expr
);
5244 if (expr
== error_mark_node
)
5247 /* A template non-type parameter must be one of the above. */
5251 /* Sanity check: did we actually convert the argument to the
5253 gcc_assert (same_type_p (type
, TREE_TYPE (expr
)));
5257 /* Subroutine of coerce_template_template_parms, which returns 1 if
5258 PARM_PARM and ARG_PARM match using the rule for the template
5259 parameters of template template parameters. Both PARM and ARG are
5260 template parameters; the rest of the arguments are the same as for
5261 coerce_template_template_parms.
5264 coerce_template_template_parm (tree parm
,
5266 tsubst_flags_t complain
,
5270 if (arg
== NULL_TREE
|| arg
== error_mark_node
5271 || parm
== NULL_TREE
|| parm
== error_mark_node
)
5274 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5277 switch (TREE_CODE (parm
))
5280 /* We encounter instantiations of templates like
5281 template <template <template <class> class> class TT>
5284 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
5285 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
5287 if (!coerce_template_template_parms
5288 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
5294 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
5295 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5296 /* Argument is a parameter pack but parameter is not. */
5301 /* The tsubst call is used to handle cases such as
5303 template <int> class C {};
5304 template <class T, template <T> class TT> class D {};
5307 i.e. the parameter list of TT depends on earlier parameters. */
5308 if (!uses_template_parms (TREE_TYPE (arg
))
5310 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
5314 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
5315 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
5316 /* Argument is a parameter pack but parameter is not. */
5329 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5330 template template parameters. Both PARM_PARMS and ARG_PARMS are
5331 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5334 Consider the example:
5335 template <class T> class A;
5336 template<template <class U> class TT> class B;
5338 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5339 the parameters to A, and OUTER_ARGS contains A. */
5342 coerce_template_template_parms (tree parm_parms
,
5344 tsubst_flags_t complain
,
5348 int nparms
, nargs
, i
;
5352 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
5353 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
5355 nparms
= TREE_VEC_LENGTH (parm_parms
);
5356 nargs
= TREE_VEC_LENGTH (arg_parms
);
5358 /* Determine whether we have a parameter pack at the end of the
5359 template template parameter's template parameter list. */
5360 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
5362 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
5364 if (parm
== error_mark_node
)
5367 switch (TREE_CODE (parm
))
5371 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5376 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
5386 && !(variadic_p
&& nargs
>= nparms
- 1))
5389 /* Check all of the template parameters except the parameter pack at
5390 the end (if any). */
5391 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
5393 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
5394 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
5397 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
5398 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
5400 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
5408 /* Check each of the template parameters in the template
5409 argument against the template parameter pack at the end of
5410 the template template parameter. */
5411 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
5414 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
5416 for (; i
< nargs
; ++i
)
5418 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
5421 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
5423 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
5432 /* Verifies that the deduced template arguments (in TARGS) for the
5433 template template parameters (in TPARMS) represent valid bindings,
5434 by comparing the template parameter list of each template argument
5435 to the template parameter list of its corresponding template
5436 template parameter, in accordance with DR150. This
5437 routine can only be called after all template arguments have been
5438 deduced. It will return TRUE if all of the template template
5439 parameter bindings are okay, FALSE otherwise. */
5441 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
5443 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
5446 /* We're dealing with template parms in this process. */
5447 ++processing_template_decl
;
5449 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
5451 for (i
= 0; i
< ntparms
; ++i
)
5453 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
5454 tree targ
= TREE_VEC_ELT (targs
, i
);
5456 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
5458 tree packed_args
= NULL_TREE
;
5461 if (ARGUMENT_PACK_P (targ
))
5463 /* Look inside the argument pack. */
5464 packed_args
= ARGUMENT_PACK_ARGS (targ
);
5465 len
= TREE_VEC_LENGTH (packed_args
);
5468 for (idx
= 0; idx
< len
; ++idx
)
5470 tree targ_parms
= NULL_TREE
;
5473 /* Extract the next argument from the argument
5475 targ
= TREE_VEC_ELT (packed_args
, idx
);
5477 if (PACK_EXPANSION_P (targ
))
5478 /* Look at the pattern of the pack expansion. */
5479 targ
= PACK_EXPANSION_PATTERN (targ
);
5481 /* Extract the template parameters from the template
5483 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
5484 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
5485 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
5486 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
5488 /* Verify that we can coerce the template template
5489 parameters from the template argument to the template
5490 parameter. This requires an exact match. */
5492 && !coerce_template_template_parms
5493 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
5508 --processing_template_decl
;
5512 /* Convert the indicated template ARG as necessary to match the
5513 indicated template PARM. Returns the converted ARG, or
5514 error_mark_node if the conversion was unsuccessful. Error and
5515 warning messages are issued under control of COMPLAIN. This
5516 conversion is for the Ith parameter in the parameter list. ARGS is
5517 the full set of template arguments deduced so far. */
5520 convert_template_argument (tree parm
,
5523 tsubst_flags_t complain
,
5529 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
5531 if (TREE_CODE (arg
) == TREE_LIST
5532 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
5534 /* The template argument was the name of some
5535 member function. That's usually
5536 invalid, but static members are OK. In any
5537 case, grab the underlying fields/functions
5538 and issue an error later if required. */
5539 orig_arg
= TREE_VALUE (arg
);
5540 TREE_TYPE (arg
) = unknown_type_node
;
5545 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
5546 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
5547 || requires_tmpl_type
);
5549 /* When determining whether an argument pack expansion is a template,
5550 look at the pattern. */
5551 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
5552 arg
= PACK_EXPANSION_PATTERN (arg
);
5554 /* Deal with an injected-class-name used as a template template arg. */
5555 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
5557 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
5558 if (TREE_CODE (t
) == TEMPLATE_DECL
)
5560 if (complain
& tf_warning_or_error
)
5561 pedwarn (input_location
, OPT_pedantic
, "injected-class-name %qD"
5562 " used as template template argument", TYPE_NAME (arg
));
5563 else if (flag_pedantic_errors
)
5571 ((TREE_CODE (arg
) == TEMPLATE_DECL
5572 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
5573 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
5574 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
5577 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
5578 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
5579 arg
= TYPE_STUB_DECL (arg
);
5581 is_type
= TYPE_P (arg
) || is_tmpl_type
;
5583 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
5584 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
5586 permerror (input_location
, "to refer to a type member of a template parameter, "
5587 "use %<typename %E%>", orig_arg
);
5589 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
5590 TREE_OPERAND (arg
, 1),
5592 complain
& tf_error
);
5596 if (is_type
!= requires_type
)
5600 if (complain
& tf_error
)
5602 error ("type/value mismatch at argument %d in template "
5603 "parameter list for %qD",
5606 error (" expected a constant of type %qT, got %qT",
5608 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
5609 else if (requires_tmpl_type
)
5610 error (" expected a class template, got %qE", orig_arg
);
5612 error (" expected a type, got %qE", orig_arg
);
5615 return error_mark_node
;
5617 if (is_tmpl_type
^ requires_tmpl_type
)
5619 if (in_decl
&& (complain
& tf_error
))
5621 error ("type/value mismatch at argument %d in template "
5622 "parameter list for %qD",
5625 error (" expected a type, got %qT", DECL_NAME (arg
));
5627 error (" expected a class template, got %qT", orig_arg
);
5629 return error_mark_node
;
5634 if (requires_tmpl_type
)
5636 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
5637 /* The number of argument required is not known yet.
5638 Just accept it for now. */
5639 val
= TREE_TYPE (arg
);
5642 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
5645 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
5647 if (coerce_template_template_parms (parmparm
, argparm
,
5653 /* TEMPLATE_TEMPLATE_PARM node is preferred over
5655 if (val
!= error_mark_node
)
5657 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
5658 val
= TREE_TYPE (val
);
5659 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
5660 val
= make_pack_expansion (val
);
5665 if (in_decl
&& (complain
& tf_error
))
5667 error ("type/value mismatch at argument %d in "
5668 "template parameter list for %qD",
5670 error (" expected a template of type %qD, got %qT",
5674 val
= error_mark_node
;
5680 /* We only form one instance of each template specialization.
5681 Therefore, if we use a non-canonical variant (i.e., a
5682 typedef), any future messages referring to the type will use
5683 the typedef, which is confusing if those future uses do not
5684 themselves also use the typedef. */
5686 val
= strip_typedefs (val
);
5690 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
5692 if (invalid_nontype_parm_type_p (t
, complain
))
5693 return error_mark_node
;
5695 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
5697 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
5701 /* Not sure if this is reachable, but it doesn't hurt
5703 error ("type mismatch in nontype parameter pack");
5704 val
= error_mark_node
;
5707 else if (!uses_template_parms (orig_arg
) && !uses_template_parms (t
))
5708 /* We used to call digest_init here. However, digest_init
5709 will report errors, which we don't want when complain
5710 is zero. More importantly, digest_init will try too
5711 hard to convert things: for example, `0' should not be
5712 converted to pointer type at this point according to
5713 the standard. Accepting this is not merely an
5714 extension, since deciding whether or not these
5715 conversions can occur is part of determining which
5716 function template to call, or whether a given explicit
5717 argument specification is valid. */
5718 val
= convert_nontype_argument (t
, orig_arg
);
5722 if (val
== NULL_TREE
)
5723 val
= error_mark_node
;
5724 else if (val
== error_mark_node
&& (complain
& tf_error
))
5725 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
5727 if (TREE_CODE (val
) == SCOPE_REF
)
5729 /* Strip typedefs from the SCOPE_REF. */
5730 tree type
= strip_typedefs (TREE_TYPE (val
));
5731 tree scope
= strip_typedefs (TREE_OPERAND (val
, 0));
5732 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
5733 QUALIFIED_NAME_IS_TEMPLATE (val
));
5740 /* Coerces the remaining template arguments in INNER_ARGS (from
5741 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
5742 Returns the coerced argument pack. PARM_IDX is the position of this
5743 parameter in the template parameter list. ARGS is the original
5744 template argument list. */
5746 coerce_template_parameter_pack (tree parms
,
5754 tsubst_flags_t complain
)
5756 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
5757 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
5760 tree packed_types
= NULL_TREE
;
5762 if (arg_idx
> nargs
)
5765 packed_args
= make_tree_vec (nargs
- arg_idx
);
5767 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
5768 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
5770 /* When the template parameter is a non-type template
5771 parameter pack whose type uses parameter packs, we need
5772 to look at each of the template arguments
5773 separately. Build a vector of the types for these
5774 non-type template parameters in PACKED_TYPES. */
5776 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
5777 packed_types
= tsubst_pack_expansion (expansion
, args
,
5780 if (packed_types
== error_mark_node
)
5781 return error_mark_node
;
5783 /* Check that we have the right number of arguments. */
5785 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
5786 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
5789 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
5790 error ("wrong number of template arguments (%d, should be %d)",
5791 nargs
, needed_parms
);
5792 return error_mark_node
;
5795 /* If we aren't able to check the actual arguments now
5796 (because they haven't been expanded yet), we can at least
5797 verify that all of the types used for the non-type
5798 template parameter pack are, in fact, valid for non-type
5799 template parameters. */
5801 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
5803 int j
, len
= TREE_VEC_LENGTH (packed_types
);
5804 for (j
= 0; j
< len
; ++j
)
5806 tree t
= TREE_VEC_ELT (packed_types
, j
);
5807 if (invalid_nontype_parm_type_p (t
, complain
))
5808 return error_mark_node
;
5813 /* Convert the remaining arguments, which will be a part of the
5814 parameter pack "parm". */
5815 for (; arg_idx
< nargs
; ++arg_idx
)
5817 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
5818 tree actual_parm
= TREE_VALUE (parm
);
5820 if (packed_types
&& !PACK_EXPANSION_P (arg
))
5822 /* When we have a vector of types (corresponding to the
5823 non-type template parameter pack that uses parameter
5824 packs in its type, as mention above), and the
5825 argument is not an expansion (which expands to a
5826 currently unknown number of arguments), clone the
5827 parm and give it the next type in PACKED_TYPES. */
5828 actual_parm
= copy_node (actual_parm
);
5829 TREE_TYPE (actual_parm
) =
5830 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
5833 if (arg
!= error_mark_node
)
5834 arg
= convert_template_argument (actual_parm
,
5835 arg
, new_args
, complain
, parm_idx
,
5837 if (arg
== error_mark_node
)
5839 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
5842 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
5843 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
5844 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
5847 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
5848 TREE_TYPE (argument_pack
)
5849 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
5850 TREE_CONSTANT (argument_pack
) = 1;
5853 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
5854 #ifdef ENABLE_CHECKING
5855 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
5856 TREE_VEC_LENGTH (packed_args
));
5858 return argument_pack
;
5861 /* Convert all template arguments to their appropriate types, and
5862 return a vector containing the innermost resulting template
5863 arguments. If any error occurs, return error_mark_node. Error and
5864 warning messages are issued under control of COMPLAIN.
5866 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
5867 for arguments not specified in ARGS. Otherwise, if
5868 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
5869 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
5870 USE_DEFAULT_ARGS is false, then all arguments must be specified in
5874 coerce_template_parms (tree parms
,
5877 tsubst_flags_t complain
,
5878 bool require_all_args
,
5879 bool use_default_args
)
5881 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
5884 tree new_inner_args
;
5885 int saved_unevaluated_operand
;
5886 int saved_inhibit_evaluation_warnings
;
5888 /* When used as a boolean value, indicates whether this is a
5889 variadic template parameter list. Since it's an int, we can also
5890 subtract it from nparms to get the number of non-variadic
5894 nparms
= TREE_VEC_LENGTH (parms
);
5896 /* Determine if there are any parameter packs. */
5897 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
5899 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
5900 if (template_parameter_pack_p (tparm
))
5904 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
5905 /* If there are 0 or 1 parameter packs, we need to expand any argument
5906 packs so that we can deduce a parameter pack from some non-packed args
5907 followed by an argument pack, as in variadic85.C. If there are more
5908 than that, we need to leave argument packs intact so the arguments are
5909 assigned to the right parameter packs. This should only happen when
5910 dealing with a nested class inside a partial specialization of a class
5911 template, as in variadic92.C. */
5912 if (variadic_p
<= 1)
5913 inner_args
= expand_template_argument_pack (inner_args
);
5915 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
5916 if ((nargs
> nparms
&& !variadic_p
)
5917 || (nargs
< nparms
- variadic_p
5919 && (!use_default_args
5920 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
5921 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
5923 if (complain
& tf_error
)
5925 const char *or_more
= "";
5928 or_more
= " or more";
5932 error ("wrong number of template arguments (%d, should be %d%s)",
5933 nargs
, nparms
, or_more
);
5936 error ("provided for %q+D", in_decl
);
5939 return error_mark_node
;
5942 /* We need to evaluate the template arguments, even though this
5943 template-id may be nested within a "sizeof". */
5944 saved_unevaluated_operand
= cp_unevaluated_operand
;
5945 cp_unevaluated_operand
= 0;
5946 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
5947 c_inhibit_evaluation_warnings
= 0;
5948 new_inner_args
= make_tree_vec (nparms
);
5949 new_args
= add_outermost_template_args (args
, new_inner_args
);
5950 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
5955 /* Get the Ith template parameter. */
5956 parm
= TREE_VEC_ELT (parms
, parm_idx
);
5958 if (parm
== error_mark_node
)
5960 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
5964 /* Calculate the next argument. */
5965 if (arg_idx
< nargs
)
5966 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
5970 if (template_parameter_pack_p (TREE_VALUE (parm
))
5971 && !(arg
&& ARGUMENT_PACK_P (arg
)))
5973 /* All remaining arguments will be placed in the
5974 template parameter pack PARM. */
5975 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
5976 inner_args
, arg_idx
,
5980 /* Store this argument. */
5981 if (arg
== error_mark_node
)
5983 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
5985 /* We are done with all of the arguments. */
5992 if (PACK_EXPANSION_P (arg
))
5994 if (complain
& tf_error
)
5996 /* FIXME this restriction was removed by N2555; see
5998 /* If ARG is a pack expansion, but PARM is not a
5999 template parameter pack (if it were, we would have
6000 handled it above), we're trying to expand into a
6001 fixed-length argument list. */
6002 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
6003 sorry ("cannot expand %<%E%> into a fixed-length "
6004 "argument list", arg
);
6006 sorry ("cannot expand %<%T%> into a fixed-length "
6007 "argument list", arg
);
6009 return error_mark_node
;
6012 else if (require_all_args
)
6014 /* There must be a default arg in this case. */
6015 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
6017 /* The position of the first default template argument,
6018 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6020 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6021 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
, arg_idx
);
6026 if (arg
== error_mark_node
)
6028 if (complain
& tf_error
)
6029 error ("template argument %d is invalid", arg_idx
+ 1);
6032 /* This only occurs if there was an error in the template
6033 parameter list itself (which we would already have
6034 reported) that we are trying to recover from, e.g., a class
6035 template with a parameter list such as
6036 template<typename..., typename>. */
6037 return error_mark_node
;
6039 arg
= convert_template_argument (TREE_VALUE (parm
),
6040 arg
, new_args
, complain
,
6043 if (arg
== error_mark_node
)
6045 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
6047 cp_unevaluated_operand
= saved_unevaluated_operand
;
6048 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
6051 return error_mark_node
;
6053 #ifdef ENABLE_CHECKING
6054 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6055 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6056 TREE_VEC_LENGTH (new_inner_args
));
6059 return new_inner_args
;
6062 /* Returns 1 if template args OT and NT are equivalent. */
6065 template_args_equal (tree ot
, tree nt
)
6070 if (TREE_CODE (nt
) == TREE_VEC
)
6071 /* For member templates */
6072 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
6073 else if (PACK_EXPANSION_P (ot
))
6074 return PACK_EXPANSION_P (nt
)
6075 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
6076 PACK_EXPANSION_PATTERN (nt
));
6077 else if (ARGUMENT_PACK_P (ot
))
6082 if (!ARGUMENT_PACK_P (nt
))
6085 opack
= ARGUMENT_PACK_ARGS (ot
);
6086 npack
= ARGUMENT_PACK_ARGS (nt
);
6087 len
= TREE_VEC_LENGTH (opack
);
6088 if (TREE_VEC_LENGTH (npack
) != len
)
6090 for (i
= 0; i
< len
; ++i
)
6091 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
6092 TREE_VEC_ELT (npack
, i
)))
6096 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
6098 /* We get here probably because we are in the middle of substituting
6099 into the pattern of a pack expansion. In that case the
6100 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6101 interested in. So we want to use the initial pack argument for
6103 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
6104 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
6105 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
6106 return template_args_equal (ot
, nt
);
6108 else if (TYPE_P (nt
))
6109 return TYPE_P (ot
) && same_type_p (ot
, nt
);
6110 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
6113 return cp_tree_equal (ot
, nt
);
6116 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6117 of template arguments. Returns 0 otherwise. */
6120 comp_template_args (tree oldargs
, tree newargs
)
6124 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
6127 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
6129 tree nt
= TREE_VEC_ELT (newargs
, i
);
6130 tree ot
= TREE_VEC_ELT (oldargs
, i
);
6132 if (! template_args_equal (ot
, nt
))
6139 add_pending_template (tree d
)
6141 tree ti
= (TYPE_P (d
)
6142 ? CLASSTYPE_TEMPLATE_INFO (d
)
6143 : DECL_TEMPLATE_INFO (d
));
6144 struct pending_template
*pt
;
6147 if (TI_PENDING_TEMPLATE_FLAG (ti
))
6150 /* We are called both from instantiate_decl, where we've already had a
6151 tinst_level pushed, and instantiate_template, where we haven't.
6153 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
6156 push_tinst_level (d
);
6158 pt
= GGC_NEW (struct pending_template
);
6160 pt
->tinst
= current_tinst_level
;
6161 if (last_pending_template
)
6162 last_pending_template
->next
= pt
;
6164 pending_templates
= pt
;
6166 last_pending_template
= pt
;
6168 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
6175 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6176 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6177 documentation for TEMPLATE_ID_EXPR. */
6180 lookup_template_function (tree fns
, tree arglist
)
6184 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
6185 return error_mark_node
;
6187 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
6188 gcc_assert (fns
&& (is_overloaded_fn (fns
)
6189 || TREE_CODE (fns
) == IDENTIFIER_NODE
));
6191 if (BASELINK_P (fns
))
6193 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
6195 BASELINK_FUNCTIONS (fns
),
6200 type
= TREE_TYPE (fns
);
6201 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
6202 type
= unknown_type_node
;
6204 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
6207 /* Within the scope of a template class S<T>, the name S gets bound
6208 (in build_self_reference) to a TYPE_DECL for the class, not a
6209 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6210 or one of its enclosing classes, and that type is a template,
6211 return the associated TEMPLATE_DECL. Otherwise, the original
6214 Also handle the case when DECL is a TREE_LIST of ambiguous
6215 injected-class-names from different bases. */
6218 maybe_get_template_decl_from_type_decl (tree decl
)
6220 if (decl
== NULL_TREE
)
6223 /* DR 176: A lookup that finds an injected-class-name (10.2
6224 [class.member.lookup]) can result in an ambiguity in certain cases
6225 (for example, if it is found in more than one base class). If all of
6226 the injected-class-names that are found refer to specializations of
6227 the same class template, and if the name is followed by a
6228 template-argument-list, the reference refers to the class template
6229 itself and not a specialization thereof, and is not ambiguous. */
6230 if (TREE_CODE (decl
) == TREE_LIST
)
6232 tree t
, tmpl
= NULL_TREE
;
6233 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
6235 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
6238 else if (tmpl
!= elt
)
6241 if (tmpl
&& t
== NULL_TREE
)
6247 return (decl
!= NULL_TREE
6248 && DECL_SELF_REFERENCE_P (decl
)
6249 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
6250 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
6253 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6254 parameters, find the desired type.
6256 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6258 IN_DECL, if non-NULL, is the template declaration we are trying to
6261 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6262 the class we are looking up.
6264 Issue error and warning messages under control of COMPLAIN.
6266 If the template class is really a local class in a template
6267 function, then the FUNCTION_CONTEXT is the function in which it is
6270 ??? Note that this function is currently called *twice* for each
6271 template-id: the first time from the parser, while creating the
6272 incomplete type (finish_template_type), and the second type during the
6273 real instantiation (instantiate_template_class). This is surely something
6274 that we want to avoid. It also causes some problems with argument
6275 coercion (see convert_nontype_argument for more information on this). */
6278 lookup_template_class (tree d1
,
6283 tsubst_flags_t complain
)
6285 tree templ
= NULL_TREE
, parmlist
;
6292 timevar_push (TV_NAME_LOOKUP
);
6294 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
6296 tree value
= innermost_non_namespace_value (d1
);
6297 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
6302 push_decl_namespace (context
);
6303 templ
= lookup_name (d1
);
6304 templ
= maybe_get_template_decl_from_type_decl (templ
);
6306 pop_decl_namespace ();
6309 context
= DECL_CONTEXT (templ
);
6311 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
6313 tree type
= TREE_TYPE (d1
);
6315 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6316 an implicit typename for the second A. Deal with it. */
6317 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
6318 type
= TREE_TYPE (type
);
6320 if (CLASSTYPE_TEMPLATE_INFO (type
))
6322 templ
= CLASSTYPE_TI_TEMPLATE (type
);
6323 d1
= DECL_NAME (templ
);
6326 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
6327 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
6329 templ
= TYPE_TI_TEMPLATE (d1
);
6330 d1
= DECL_NAME (templ
);
6332 else if (TREE_CODE (d1
) == TEMPLATE_DECL
6333 && DECL_TEMPLATE_RESULT (d1
)
6334 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
6337 d1
= DECL_NAME (templ
);
6338 context
= DECL_CONTEXT (templ
);
6341 /* Issue an error message if we didn't find a template. */
6344 if (complain
& tf_error
)
6345 error ("%qT is not a template", d1
);
6346 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
6349 if (TREE_CODE (templ
) != TEMPLATE_DECL
6350 /* Make sure it's a user visible template, if it was named by
6352 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
6353 && !PRIMARY_TEMPLATE_P (templ
)))
6355 if (complain
& tf_error
)
6357 error ("non-template type %qT used as a template", d1
);
6359 error ("for template declaration %q+D", in_decl
);
6361 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
6364 complain
&= ~tf_user
;
6366 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
6368 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
6369 template arguments */
6375 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
6377 /* Consider an example where a template template parameter declared as
6379 template <class T, class U = std::allocator<T> > class TT
6381 The template parameter level of T and U are one level larger than
6382 of TT. To proper process the default argument of U, say when an
6383 instantiation `TT<int>' is seen, we need to build the full
6384 arguments containing {int} as the innermost level. Outer levels,
6385 available when not appearing as default template argument, can be
6386 obtained from the arguments of the enclosing template.
6388 Suppose that TT is later substituted with std::vector. The above
6389 instantiation is `TT<int, std::allocator<T> >' with TT at
6390 level 1, and T at level 2, while the template arguments at level 1
6391 becomes {std::vector} and the inner level 2 is {int}. */
6393 outer
= DECL_CONTEXT (templ
);
6395 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
6396 else if (current_template_parms
)
6397 /* This is an argument of the current template, so we haven't set
6398 DECL_CONTEXT yet. */
6399 outer
= current_template_args ();
6402 arglist
= add_to_template_args (outer
, arglist
);
6404 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
6406 /*require_all_args=*/true,
6407 /*use_default_args=*/true);
6408 if (arglist2
== error_mark_node
6409 || (!uses_template_parms (arglist2
)
6410 && check_instantiated_args (templ
, arglist2
, complain
)))
6411 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
6413 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
6414 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, parm
);
6418 tree template_type
= TREE_TYPE (templ
);
6421 tree found
= NULL_TREE
;
6424 int is_dependent_type
;
6425 int use_partial_inst_tmpl
= false;
6427 gen_tmpl
= most_general_template (templ
);
6428 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
6429 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
6430 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
6432 if (arg_depth
== 1 && parm_depth
> 1)
6434 /* We've been given an incomplete set of template arguments.
6437 template <class T> struct S1 {
6438 template <class U> struct S2 {};
6439 template <class U> struct S2<U*> {};
6442 we will be called with an ARGLIST of `U*', but the
6443 TEMPLATE will be `template <class T> template
6444 <class U> struct S1<T>::S2'. We must fill in the missing
6447 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
6449 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
6452 /* Now we should have enough arguments. */
6453 gcc_assert (parm_depth
== arg_depth
);
6455 /* From here on, we're only interested in the most general
6458 /* Calculate the BOUND_ARGS. These will be the args that are
6459 actually tsubst'd into the definition to create the
6463 /* We have multiple levels of arguments to coerce, at once. */
6465 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
6467 tree bound_args
= make_tree_vec (parm_depth
);
6469 for (i
= saved_depth
,
6470 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
6471 i
> 0 && t
!= NULL_TREE
;
6472 --i
, t
= TREE_CHAIN (t
))
6474 tree a
= coerce_template_parms (TREE_VALUE (t
),
6477 /*require_all_args=*/true,
6478 /*use_default_args=*/true);
6480 /* Don't process further if one of the levels fails. */
6481 if (a
== error_mark_node
)
6483 /* Restore the ARGLIST to its full size. */
6484 TREE_VEC_LENGTH (arglist
) = saved_depth
;
6485 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
6488 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
6490 /* We temporarily reduce the length of the ARGLIST so
6491 that coerce_template_parms will see only the arguments
6492 corresponding to the template parameters it is
6494 TREE_VEC_LENGTH (arglist
)--;
6497 /* Restore the ARGLIST to its full size. */
6498 TREE_VEC_LENGTH (arglist
) = saved_depth
;
6500 arglist
= bound_args
;
6504 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
6505 INNERMOST_TEMPLATE_ARGS (arglist
),
6508 /*require_all_args=*/true,
6509 /*use_default_args=*/true);
6511 if (arglist
== error_mark_node
)
6512 /* We were unable to bind the arguments. */
6513 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
6515 /* In the scope of a template class, explicit references to the
6516 template class refer to the type of the template, not any
6517 instantiation of it. For example, in:
6519 template <class T> class C { void f(C<T>); }
6521 the `C<T>' is just the same as `C'. Outside of the
6522 class, however, such a reference is an instantiation. */
6524 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
6525 || currently_open_class (template_type
))
6526 /* comp_template_args is expensive, check it last. */
6527 && comp_template_args (TYPE_TI_ARGS (template_type
),
6529 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, template_type
);
6531 /* If we already have this specialization, return it. */
6532 elt
.tmpl
= gen_tmpl
;
6534 hash
= hash_specialization (&elt
);
6535 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
6539 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, entry
->spec
);
6541 is_dependent_type
= uses_template_parms (arglist
);
6543 /* If the deduced arguments are invalid, then the binding
6545 if (!is_dependent_type
6546 && check_instantiated_args (gen_tmpl
,
6547 INNERMOST_TEMPLATE_ARGS (arglist
),
6549 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
6551 if (!is_dependent_type
6552 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
6553 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
6554 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
6556 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
6557 DECL_NAME (gen_tmpl
),
6558 /*tag_scope=*/ts_global
);
6559 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, found
);
6562 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
6565 context
= global_namespace
;
6567 /* Create the type. */
6568 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
6570 if (!is_dependent_type
)
6572 set_current_access_from_decl (TYPE_NAME (template_type
));
6573 t
= start_enum (TYPE_IDENTIFIER (template_type
),
6574 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
6575 arglist
, complain
, in_decl
),
6576 SCOPED_ENUM_P (template_type
));
6580 /* We don't want to call start_enum for this type, since
6581 the values for the enumeration constants may involve
6582 template parameters. And, no one should be interested
6583 in the enumeration constants for such a type. */
6584 t
= cxx_make_type (ENUMERAL_TYPE
);
6585 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
6590 t
= make_class_type (TREE_CODE (template_type
));
6591 CLASSTYPE_DECLARED_CLASS (t
)
6592 = CLASSTYPE_DECLARED_CLASS (template_type
);
6593 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
6594 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
6596 /* A local class. Make sure the decl gets registered properly. */
6597 if (context
== current_function_decl
)
6598 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
6600 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
6601 /* This instantiation is another name for the primary
6602 template type. Set the TYPE_CANONICAL field
6604 TYPE_CANONICAL (t
) = template_type
;
6605 else if (any_template_arguments_need_structural_equality_p (arglist
))
6606 /* Some of the template arguments require structural
6607 equality testing, so this template class requires
6608 structural equality testing. */
6609 SET_TYPE_STRUCTURAL_EQUALITY (t
);
6612 /* If we called start_enum or pushtag above, this information
6613 will already be set up. */
6616 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
6618 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
6619 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
6620 DECL_SOURCE_LOCATION (type_decl
)
6621 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
6624 type_decl
= TYPE_NAME (t
);
6626 TREE_PRIVATE (type_decl
)
6627 = TREE_PRIVATE (TYPE_STUB_DECL (template_type
));
6628 TREE_PROTECTED (type_decl
)
6629 = TREE_PROTECTED (TYPE_STUB_DECL (template_type
));
6630 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
6632 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
6633 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
6636 /* Let's consider the explicit specialization of a member
6637 of a class template specialization that is implicitely instantiated,
6642 template<class U> struct M {}; //#0
6647 struct S<int>::M<char> //#1
6651 [temp.expl.spec]/4 says this is valid.
6653 In this case, when we write:
6656 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
6659 When we encounter #1, we want to store the partial instantiation
6660 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
6662 For all cases other than this "explicit specialization of member of a
6663 class template", we just want to store the most general template into
6664 the CLASSTYPE_TI_TEMPLATE of M.
6666 This case of "explicit specialization of member of a class template"
6668 1/ the enclosing class is an instantiation of, and therefore not
6669 the same as, the context of the most general template, and
6670 2/ we aren't looking at the partial instantiation itself, i.e.
6671 the innermost arguments are not the same as the innermost parms of
6672 the most general template.
6674 So it's only when 1/ and 2/ happens that we want to use the partial
6675 instantiation of the member template in lieu of its most general
6678 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
6679 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
6680 /* the enclosing class must be an instantiation... */
6681 && CLASS_TYPE_P (context
)
6682 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
6684 tree partial_inst_args
;
6685 TREE_VEC_LENGTH (arglist
)--;
6686 ++processing_template_decl
;
6688 tsubst (INNERMOST_TEMPLATE_ARGS
6689 (CLASSTYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
6690 arglist
, complain
, NULL_TREE
);
6691 --processing_template_decl
;
6692 TREE_VEC_LENGTH (arglist
)++;
6693 use_partial_inst_tmpl
=
6694 /*...and we must not be looking at the partial instantiation
6696 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
6700 if (!use_partial_inst_tmpl
)
6701 /* This case is easy; there are no member templates involved. */
6705 /* This is a full instantiation of a member template. Find
6706 the partial instantiation of which this is an instance. */
6708 /* Temporarily reduce by one the number of levels in the ARGLIST
6709 so as to avoid comparing the last set of arguments. */
6710 TREE_VEC_LENGTH (arglist
)--;
6711 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
6712 TREE_VEC_LENGTH (arglist
)++;
6713 found
= CLASSTYPE_TI_TEMPLATE (found
);
6716 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
6719 slot
= (spec_entry
**) htab_find_slot_with_hash (type_specializations
,
6720 &elt
, hash
, INSERT
);
6721 *slot
= GGC_NEW (spec_entry
);
6724 /* Note this use of the partial instantiation so we can check it
6725 later in maybe_process_partial_specialization. */
6726 DECL_TEMPLATE_INSTANTIATIONS (templ
)
6727 = tree_cons (arglist
, t
,
6728 DECL_TEMPLATE_INSTANTIATIONS (templ
));
6730 if (TREE_CODE (t
) == ENUMERAL_TYPE
&& !is_dependent_type
)
6731 /* Now that the type has been registered on the instantiations
6732 list, we set up the enumerators. Because the enumeration
6733 constants may involve the enumeration type itself, we make
6734 sure to register the type first, and then create the
6735 constants. That way, doing tsubst_expr for the enumeration
6736 constants won't result in recursive calls here; we'll find
6737 the instantiation and exit above. */
6738 tsubst_enum (template_type
, t
, arglist
);
6740 if (is_dependent_type
)
6741 /* If the type makes use of template parameters, the
6742 code that generates debugging information will crash. */
6743 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
6745 /* Possibly limit visibility based on template args. */
6746 TREE_PUBLIC (type_decl
) = 1;
6747 determine_visibility (type_decl
);
6749 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
6751 timevar_pop (TV_NAME_LOOKUP
);
6758 /* True when we should also visit template parameters that occur in
6759 non-deduced contexts. */
6760 bool include_nondeduced_p
;
6761 struct pointer_set_t
*visited
;
6764 /* Called from for_each_template_parm via walk_tree. */
6767 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
6770 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
6771 tree_fn_t fn
= pfd
->fn
;
6772 void *data
= pfd
->data
;
6775 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
6776 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
6777 pfd
->include_nondeduced_p
))
6778 return error_mark_node
;
6780 switch (TREE_CODE (t
))
6783 if (TYPE_PTRMEMFUNC_P (t
))
6789 if (!TYPE_TEMPLATE_INFO (t
))
6791 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
6792 fn
, data
, pfd
->visited
,
6793 pfd
->include_nondeduced_p
))
6794 return error_mark_node
;
6798 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
6799 fn
, data
, pfd
->visited
,
6800 pfd
->include_nondeduced_p
)
6801 || for_each_template_parm (TYPE_MAX_VALUE (t
),
6802 fn
, data
, pfd
->visited
,
6803 pfd
->include_nondeduced_p
))
6804 return error_mark_node
;
6808 /* Since we're not going to walk subtrees, we have to do this
6810 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
6811 pfd
->visited
, pfd
->include_nondeduced_p
))
6812 return error_mark_node
;
6816 /* Check the return type. */
6817 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
6818 pfd
->include_nondeduced_p
))
6819 return error_mark_node
;
6821 /* Check the parameter types. Since default arguments are not
6822 instantiated until they are needed, the TYPE_ARG_TYPES may
6823 contain expressions that involve template parameters. But,
6824 no-one should be looking at them yet. And, once they're
6825 instantiated, they don't contain template parameters, so
6826 there's no point in looking at them then, either. */
6830 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
6831 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
6832 pfd
->visited
, pfd
->include_nondeduced_p
))
6833 return error_mark_node
;
6835 /* Since we've already handled the TYPE_ARG_TYPES, we don't
6836 want walk_tree walking into them itself. */
6842 if (pfd
->include_nondeduced_p
6843 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
6845 pfd
->include_nondeduced_p
))
6846 return error_mark_node
;
6851 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
6852 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
6853 pfd
->visited
, pfd
->include_nondeduced_p
))
6854 return error_mark_node
;
6859 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
6860 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
6861 pfd
->visited
, pfd
->include_nondeduced_p
))
6862 return error_mark_node
;
6863 if (DECL_CONTEXT (t
)
6864 && pfd
->include_nondeduced_p
6865 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
6866 pfd
->visited
, pfd
->include_nondeduced_p
))
6867 return error_mark_node
;
6870 case BOUND_TEMPLATE_TEMPLATE_PARM
:
6871 /* Record template parameters such as `T' inside `TT<T>'. */
6872 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
6873 pfd
->include_nondeduced_p
))
6874 return error_mark_node
;
6877 case TEMPLATE_TEMPLATE_PARM
:
6878 case TEMPLATE_TYPE_PARM
:
6879 case TEMPLATE_PARM_INDEX
:
6880 if (fn
&& (*fn
)(t
, data
))
6881 return error_mark_node
;
6883 return error_mark_node
;
6887 /* A template template parameter is encountered. */
6888 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
6889 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
6890 pfd
->include_nondeduced_p
))
6891 return error_mark_node
;
6893 /* Already substituted template template parameter */
6899 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
6901 pfd
->include_nondeduced_p
))
6902 return error_mark_node
;
6906 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
6907 && pfd
->include_nondeduced_p
6908 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
6909 (TREE_TYPE (t
)), fn
, data
,
6910 pfd
->visited
, pfd
->include_nondeduced_p
))
6911 return error_mark_node
;
6916 /* If there's no type, then this thing must be some expression
6917 involving template parameters. */
6918 if (!fn
&& !TREE_TYPE (t
))
6919 return error_mark_node
;
6924 case REINTERPRET_CAST_EXPR
:
6925 case CONST_CAST_EXPR
:
6926 case STATIC_CAST_EXPR
:
6927 case DYNAMIC_CAST_EXPR
:
6931 case PSEUDO_DTOR_EXPR
:
6933 return error_mark_node
;
6940 /* We didn't find any template parameters we liked. */
6944 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
6945 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
6946 call FN with the parameter and the DATA.
6947 If FN returns nonzero, the iteration is terminated, and
6948 for_each_template_parm returns 1. Otherwise, the iteration
6949 continues. If FN never returns a nonzero value, the value
6950 returned by for_each_template_parm is 0. If FN is NULL, it is
6951 considered to be the function which always returns 1.
6953 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
6954 parameters that occur in non-deduced contexts. When false, only
6955 visits those template parameters that can be deduced. */
6958 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
6959 struct pointer_set_t
*visited
,
6960 bool include_nondeduced_p
)
6962 struct pair_fn_data pfd
;
6968 pfd
.include_nondeduced_p
= include_nondeduced_p
;
6970 /* Walk the tree. (Conceptually, we would like to walk without
6971 duplicates, but for_each_template_parm_r recursively calls
6972 for_each_template_parm, so we would need to reorganize a fair
6973 bit to use walk_tree_without_duplicates, so we keep our own
6976 pfd
.visited
= visited
;
6978 pfd
.visited
= pointer_set_create ();
6979 result
= cp_walk_tree (&t
,
6980 for_each_template_parm_r
,
6982 pfd
.visited
) != NULL_TREE
;
6987 pointer_set_destroy (pfd
.visited
);
6994 /* Returns true if T depends on any template parameter. */
6997 uses_template_parms (tree t
)
7000 int saved_processing_template_decl
;
7002 saved_processing_template_decl
= processing_template_decl
;
7003 if (!saved_processing_template_decl
)
7004 processing_template_decl
= 1;
7006 dependent_p
= dependent_type_p (t
);
7007 else if (TREE_CODE (t
) == TREE_VEC
)
7008 dependent_p
= any_dependent_template_arguments_p (t
);
7009 else if (TREE_CODE (t
) == TREE_LIST
)
7010 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
7011 || uses_template_parms (TREE_CHAIN (t
)));
7012 else if (TREE_CODE (t
) == TYPE_DECL
)
7013 dependent_p
= dependent_type_p (TREE_TYPE (t
));
7016 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
7017 || TREE_CODE (t
) == OVERLOAD
7018 || TREE_CODE (t
) == BASELINK
7019 || TREE_CODE (t
) == IDENTIFIER_NODE
7020 || TREE_CODE (t
) == TRAIT_EXPR
7021 || TREE_CODE (t
) == CONSTRUCTOR
7022 || CONSTANT_CLASS_P (t
))
7023 dependent_p
= (type_dependent_expression_p (t
)
7024 || value_dependent_expression_p (t
));
7027 gcc_assert (t
== error_mark_node
);
7028 dependent_p
= false;
7031 processing_template_decl
= saved_processing_template_decl
;
7036 /* Returns true if T depends on any template parameter with level LEVEL. */
7039 uses_template_parms_level (tree t
, int level
)
7041 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
7042 /*include_nondeduced_p=*/true);
7045 static int tinst_depth
;
7046 extern int max_tinst_depth
;
7047 #ifdef GATHER_STATISTICS
7050 static int tinst_level_tick
;
7051 static int last_template_error_tick
;
7053 /* We're starting to instantiate D; record the template instantiation context
7054 for diagnostics and to restore it later. */
7057 push_tinst_level (tree d
)
7059 struct tinst_level
*new_level
;
7061 if (tinst_depth
>= max_tinst_depth
)
7063 /* If the instantiation in question still has unbound template parms,
7064 we don't really care if we can't instantiate it, so just return.
7065 This happens with base instantiation for implicit `typename'. */
7066 if (uses_template_parms (d
))
7069 last_template_error_tick
= tinst_level_tick
;
7070 error ("template instantiation depth exceeds maximum of %d (use "
7071 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7072 max_tinst_depth
, d
);
7074 print_instantiation_context ();
7079 new_level
= GGC_NEW (struct tinst_level
);
7080 new_level
->decl
= d
;
7081 new_level
->locus
= input_location
;
7082 new_level
->in_system_header_p
= in_system_header
;
7083 new_level
->next
= current_tinst_level
;
7084 current_tinst_level
= new_level
;
7087 #ifdef GATHER_STATISTICS
7088 if (tinst_depth
> depth_reached
)
7089 depth_reached
= tinst_depth
;
7096 /* We're done instantiating this template; return to the instantiation
7100 pop_tinst_level (void)
7102 /* Restore the filename and line number stashed away when we started
7103 this instantiation. */
7104 input_location
= current_tinst_level
->locus
;
7105 current_tinst_level
= current_tinst_level
->next
;
7110 /* We're instantiating a deferred template; restore the template
7111 instantiation context in which the instantiation was requested, which
7112 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7115 reopen_tinst_level (struct tinst_level
*level
)
7117 struct tinst_level
*t
;
7120 for (t
= level
; t
; t
= t
->next
)
7123 current_tinst_level
= level
;
7128 /* Returns the TINST_LEVEL which gives the original instantiation
7131 struct tinst_level
*
7132 outermost_tinst_level (void)
7134 struct tinst_level
*level
= current_tinst_level
;
7137 level
= level
->next
;
7141 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
7144 parameter_of_template_p (tree parm
, tree templ
)
7149 if (!parm
|| !templ
)
7152 gcc_assert (DECL_TEMPLATE_PARM_P (parm
));
7153 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
7155 parms
= DECL_TEMPLATE_PARMS (templ
);
7156 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
7158 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
7159 if (parm
== TREE_VALUE (TREE_VEC_ELT (parms
, i
)))
7165 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
7166 vector of template arguments, as for tsubst.
7168 Returns an appropriate tsubst'd friend declaration. */
7171 tsubst_friend_function (tree decl
, tree args
)
7175 if (TREE_CODE (decl
) == FUNCTION_DECL
7176 && DECL_TEMPLATE_INSTANTIATION (decl
)
7177 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
7178 /* This was a friend declared with an explicit template
7179 argument list, e.g.:
7183 to indicate that f was a template instantiation, not a new
7184 function declaration. Now, we have to figure out what
7185 instantiation of what template. */
7187 tree template_id
, arglist
, fns
;
7190 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
7192 /* Friend functions are looked up in the containing namespace scope.
7193 We must enter that scope, to avoid finding member functions of the
7194 current class with same name. */
7195 push_nested_namespace (ns
);
7196 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
7197 tf_warning_or_error
, NULL_TREE
,
7198 /*integral_constant_expression_p=*/false);
7199 pop_nested_namespace (ns
);
7200 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
7201 tf_warning_or_error
, NULL_TREE
);
7202 template_id
= lookup_template_function (fns
, arglist
);
7204 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
7205 tmpl
= determine_specialization (template_id
, new_friend
,
7207 /*need_member_template=*/0,
7208 TREE_VEC_LENGTH (args
),
7210 return instantiate_template (tmpl
, new_args
, tf_error
);
7213 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
7215 /* The NEW_FRIEND will look like an instantiation, to the
7216 compiler, but is not an instantiation from the point of view of
7217 the language. For example, we might have had:
7219 template <class T> struct S {
7220 template <class U> friend void f(T, U);
7223 Then, in S<int>, template <class U> void f(int, U) is not an
7224 instantiation of anything. */
7225 if (new_friend
== error_mark_node
)
7226 return error_mark_node
;
7228 DECL_USE_TEMPLATE (new_friend
) = 0;
7229 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
7231 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
7232 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
7233 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
7236 /* The mangled name for the NEW_FRIEND is incorrect. The function
7237 is not a template instantiation and should not be mangled like
7238 one. Therefore, we forget the mangling here; we'll recompute it
7239 later if we need it. */
7240 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
7242 SET_DECL_RTL (new_friend
, NULL
);
7243 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
7246 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
7249 tree new_friend_template_info
;
7250 tree new_friend_result_template_info
;
7252 int new_friend_is_defn
;
7254 /* We must save some information from NEW_FRIEND before calling
7255 duplicate decls since that function will free NEW_FRIEND if
7257 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
7258 new_friend_is_defn
=
7259 (DECL_INITIAL (DECL_TEMPLATE_RESULT
7260 (template_for_substitution (new_friend
)))
7262 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
7264 /* This declaration is a `primary' template. */
7265 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
7267 new_friend_result_template_info
7268 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
7271 new_friend_result_template_info
= NULL_TREE
;
7273 /* Make the init_value nonzero so pushdecl knows this is a defn. */
7274 if (new_friend_is_defn
)
7275 DECL_INITIAL (new_friend
) = error_mark_node
;
7277 /* Inside pushdecl_namespace_level, we will push into the
7278 current namespace. However, the friend function should go
7279 into the namespace of the template. */
7280 ns
= decl_namespace_context (new_friend
);
7281 push_nested_namespace (ns
);
7282 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
7283 pop_nested_namespace (ns
);
7285 if (old_decl
== error_mark_node
)
7286 return error_mark_node
;
7288 if (old_decl
!= new_friend
)
7290 /* This new friend declaration matched an existing
7291 declaration. For example, given:
7293 template <class T> void f(T);
7294 template <class U> class C {
7295 template <class T> friend void f(T) {}
7298 the friend declaration actually provides the definition
7299 of `f', once C has been instantiated for some type. So,
7300 old_decl will be the out-of-class template declaration,
7301 while new_friend is the in-class definition.
7303 But, if `f' was called before this point, the
7304 instantiation of `f' will have DECL_TI_ARGS corresponding
7305 to `T' but not to `U', references to which might appear
7306 in the definition of `f'. Previously, the most general
7307 template for an instantiation of `f' was the out-of-class
7308 version; now it is the in-class version. Therefore, we
7309 run through all specialization of `f', adding to their
7310 DECL_TI_ARGS appropriately. In particular, they need a
7311 new set of outer arguments, corresponding to the
7312 arguments for this class instantiation.
7314 The same situation can arise with something like this:
7317 template <class T> class C {
7321 when `C<int>' is instantiated. Now, `f(int)' is defined
7324 if (!new_friend_is_defn
)
7325 /* On the other hand, if the in-class declaration does
7326 *not* provide a definition, then we don't want to alter
7327 existing definitions. We can just leave everything
7332 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
7333 tree new_args
= TI_ARGS (new_friend_template_info
);
7335 /* Overwrite whatever template info was there before, if
7336 any, with the new template information pertaining to
7338 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
7340 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
7342 /* We should have called reregister_specialization in
7344 gcc_assert (retrieve_specialization (new_template
,
7348 /* Instantiate it if the global has already been used. */
7349 if (DECL_ODR_USED (old_decl
))
7350 instantiate_decl (old_decl
, /*defer_ok=*/true,
7351 /*expl_inst_class_mem_p=*/false);
7357 /* Indicate that the old function template is a partial
7359 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
7360 = new_friend_result_template_info
;
7362 gcc_assert (new_template
7363 == most_general_template (new_template
));
7364 gcc_assert (new_template
!= old_decl
);
7366 /* Reassign any specializations already in the hash table
7367 to the new more general template, and add the
7368 additional template args. */
7369 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
7373 tree spec
= TREE_VALUE (t
);
7376 elt
.tmpl
= old_decl
;
7377 elt
.args
= DECL_TI_ARGS (spec
);
7378 elt
.spec
= NULL_TREE
;
7380 htab_remove_elt (decl_specializations
, &elt
);
7383 = add_outermost_template_args (new_args
,
7384 DECL_TI_ARGS (spec
));
7386 register_specialization
7387 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
7390 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
7394 /* The information from NEW_FRIEND has been merged into OLD_DECL
7395 by duplicate_decls. */
7396 new_friend
= old_decl
;
7401 tree context
= DECL_CONTEXT (new_friend
);
7405 template <class T> class C {
7406 template <class U> friend void C1<U>::f (); // case 1
7407 friend void C2<T>::f (); // case 2
7409 we only need to make sure CONTEXT is a complete type for
7410 case 2. To distinguish between the two cases, we note that
7411 CONTEXT of case 1 remains dependent type after tsubst while
7412 this isn't true for case 2. */
7413 ++processing_template_decl
;
7414 dependent_p
= dependent_type_p (context
);
7415 --processing_template_decl
;
7418 && !complete_type_or_else (context
, NULL_TREE
))
7419 return error_mark_node
;
7421 if (COMPLETE_TYPE_P (context
))
7423 /* Check to see that the declaration is really present, and,
7424 possibly obtain an improved declaration. */
7425 tree fn
= check_classfn (context
,
7426 new_friend
, NULL_TREE
);
7436 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
7437 template arguments, as for tsubst.
7439 Returns an appropriate tsubst'd friend type or error_mark_node on
7443 tsubst_friend_class (tree friend_tmpl
, tree args
)
7449 context
= DECL_CONTEXT (friend_tmpl
);
7453 if (TREE_CODE (context
) == NAMESPACE_DECL
)
7454 push_nested_namespace (context
);
7456 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
7459 /* Look for a class template declaration. We look for hidden names
7460 because two friend declarations of the same template are the
7461 same. For example, in:
7464 template <typename> friend class F;
7466 template <typename> struct B {
7467 template <typename> friend class F;
7470 both F templates are the same. */
7471 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
7472 /*block_p=*/true, 0,
7473 LOOKUP_COMPLAIN
| LOOKUP_HIDDEN
);
7475 /* But, if we don't find one, it might be because we're in a
7476 situation like this:
7484 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
7485 for `S<int>', not the TEMPLATE_DECL. */
7486 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
7488 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
7489 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
7492 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
7494 /* The friend template has already been declared. Just
7495 check to see that the declarations match, and install any new
7496 default parameters. We must tsubst the default parameters,
7497 of course. We only need the innermost template parameters
7498 because that is all that redeclare_class_template will look
7500 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
7501 > TMPL_ARGS_DEPTH (args
))
7504 location_t saved_input_location
;
7505 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
7506 args
, tf_warning_or_error
);
7508 saved_input_location
= input_location
;
7509 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
7510 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
7511 input_location
= saved_input_location
;
7515 friend_type
= TREE_TYPE (tmpl
);
7519 /* The friend template has not already been declared. In this
7520 case, the instantiation of the template class will cause the
7521 injection of this template into the global scope. */
7522 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
7523 if (tmpl
== error_mark_node
)
7524 return error_mark_node
;
7526 /* The new TMPL is not an instantiation of anything, so we
7527 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
7528 the new type because that is supposed to be the corresponding
7529 template decl, i.e., TMPL. */
7530 DECL_USE_TEMPLATE (tmpl
) = 0;
7531 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
7532 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
7533 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
7534 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
7536 /* Inject this template into the global scope. */
7537 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
7542 if (TREE_CODE (context
) == NAMESPACE_DECL
)
7543 pop_nested_namespace (context
);
7545 pop_nested_class ();
7551 /* Returns zero if TYPE cannot be completed later due to circularity.
7552 Otherwise returns one. */
7555 can_complete_type_without_circularity (tree type
)
7557 if (type
== NULL_TREE
|| type
== error_mark_node
)
7559 else if (COMPLETE_TYPE_P (type
))
7561 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
7562 return can_complete_type_without_circularity (TREE_TYPE (type
));
7563 else if (CLASS_TYPE_P (type
)
7564 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
7570 /* Apply any attributes which had to be deferred until instantiation
7571 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
7572 ARGS, COMPLAIN, IN_DECL are as tsubst. */
7575 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
7576 tree args
, tsubst_flags_t complain
, tree in_decl
)
7578 tree last_dep
= NULL_TREE
;
7582 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
7583 if (ATTR_IS_DEPENDENT (t
))
7586 attributes
= copy_list (attributes
);
7590 if (DECL_P (*decl_p
))
7592 if (TREE_TYPE (*decl_p
) == error_mark_node
)
7594 p
= &DECL_ATTRIBUTES (*decl_p
);
7597 p
= &TYPE_ATTRIBUTES (*decl_p
);
7601 tree late_attrs
= NULL_TREE
;
7602 tree
*q
= &late_attrs
;
7604 for (*p
= attributes
; *p
; )
7607 if (ATTR_IS_DEPENDENT (t
))
7609 *p
= TREE_CHAIN (t
);
7610 TREE_CHAIN (t
) = NULL_TREE
;
7611 /* If the first attribute argument is an identifier, don't
7612 pass it through tsubst. Attributes like mode, format,
7613 cleanup and several target specific attributes expect it
7616 && TREE_CODE (TREE_VALUE (t
)) == TREE_LIST
7617 && TREE_VALUE (TREE_VALUE (t
))
7618 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t
)))
7619 == IDENTIFIER_NODE
))
7622 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
7624 /*integral_constant_expression_p=*/false);
7625 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
7627 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
7632 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
7633 /*integral_constant_expression_p=*/false);
7635 q
= &TREE_CHAIN (t
);
7638 p
= &TREE_CHAIN (t
);
7641 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
7645 /* Perform (or defer) access check for typedefs that were referenced
7646 from within the template TMPL code.
7647 This is a subroutine of instantiate_template and instantiate_class_template.
7648 TMPL is the template to consider and TARGS is the list of arguments of
7652 perform_typedefs_access_check (tree tmpl
, tree targs
)
7654 location_t saved_location
;
7656 qualified_typedef_usage_t
*iter
;
7659 || (!CLASS_TYPE_P (tmpl
)
7660 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
7663 saved_location
= input_location
;
7665 VEC_iterate (qualified_typedef_usage_t
,
7666 get_types_needing_access_check (tmpl
),
7670 tree type_decl
= iter
->typedef_decl
;
7671 tree type_scope
= iter
->context
;
7673 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
7676 if (uses_template_parms (type_decl
))
7677 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
7678 if (uses_template_parms (type_scope
))
7679 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
7681 /* Make access check error messages point to the location
7682 of the use of the typedef. */
7683 input_location
= iter
->locus
;
7684 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
7685 type_decl
, type_decl
);
7687 input_location
= saved_location
;
7691 instantiate_class_template (tree type
)
7693 tree templ
, args
, pattern
, t
, member
;
7697 unsigned int saved_maximum_field_alignment
;
7699 if (type
== error_mark_node
)
7700 return error_mark_node
;
7702 if (TYPE_BEING_DEFINED (type
)
7703 || COMPLETE_TYPE_P (type
)
7704 || uses_template_parms (type
))
7707 /* Figure out which template is being instantiated. */
7708 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
7709 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
7711 /* Determine what specialization of the original template to
7713 t
= most_specialized_class (type
, templ
);
7714 if (t
== error_mark_node
)
7716 TYPE_BEING_DEFINED (type
) = 1;
7717 return error_mark_node
;
7721 /* This TYPE is actually an instantiation of a partial
7722 specialization. We replace the innermost set of ARGS with
7723 the arguments appropriate for substitution. For example,
7726 template <class T> struct S {};
7727 template <class T> struct S<T*> {};
7729 and supposing that we are instantiating S<int*>, ARGS will
7730 presently be {int*} -- but we need {int}. */
7731 pattern
= TREE_TYPE (t
);
7732 args
= TREE_PURPOSE (t
);
7736 pattern
= TREE_TYPE (templ
);
7737 args
= CLASSTYPE_TI_ARGS (type
);
7740 /* If the template we're instantiating is incomplete, then clearly
7741 there's nothing we can do. */
7742 if (!COMPLETE_TYPE_P (pattern
))
7745 /* If we've recursively instantiated too many templates, stop. */
7746 if (! push_tinst_level (type
))
7749 /* Now we're really doing the instantiation. Mark the type as in
7750 the process of being defined. */
7751 TYPE_BEING_DEFINED (type
) = 1;
7753 /* We may be in the middle of deferred access check. Disable
7755 push_deferring_access_checks (dk_no_deferred
);
7757 push_to_top_level ();
7758 /* Use #pragma pack from the template context. */
7759 saved_maximum_field_alignment
= maximum_field_alignment
;
7760 maximum_field_alignment
= TYPE_PRECISION (pattern
);
7762 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
7764 /* Set the input location to the most specialized template definition.
7765 This is needed if tsubsting causes an error. */
7766 typedecl
= TYPE_MAIN_DECL (pattern
);
7767 input_location
= DECL_SOURCE_LOCATION (typedecl
);
7769 TYPE_HAS_USER_CONSTRUCTOR (type
) = TYPE_HAS_USER_CONSTRUCTOR (pattern
);
7770 TYPE_HAS_NEW_OPERATOR (type
) = TYPE_HAS_NEW_OPERATOR (pattern
);
7771 TYPE_HAS_ARRAY_NEW_OPERATOR (type
) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern
);
7772 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
7773 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
7774 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
7775 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
7776 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
7777 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
7778 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
7779 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
7780 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
7781 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
7782 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
7783 if (ANON_AGGR_TYPE_P (pattern
))
7784 SET_ANON_AGGR_TYPE_P (type
);
7785 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
7787 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
7788 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
7791 pbinfo
= TYPE_BINFO (pattern
);
7793 /* We should never instantiate a nested class before its enclosing
7794 class; we need to look up the nested class by name before we can
7795 instantiate it, and that lookup should instantiate the enclosing
7797 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
7798 || COMPLETE_TYPE_P (TYPE_CONTEXT (type
))
7799 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type
)));
7801 base_list
= NULL_TREE
;
7802 if (BINFO_N_BASE_BINFOS (pbinfo
))
7805 tree context
= TYPE_CONTEXT (type
);
7809 /* We must enter the scope containing the type, as that is where
7810 the accessibility of types named in dependent bases are
7812 pushed_scope
= push_scope (context
? context
: global_namespace
);
7814 /* Substitute into each of the bases to determine the actual
7816 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
7819 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
7820 tree expanded_bases
= NULL_TREE
;
7823 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
7826 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
7827 args
, tf_error
, NULL_TREE
);
7828 if (expanded_bases
== error_mark_node
)
7831 len
= TREE_VEC_LENGTH (expanded_bases
);
7834 for (idx
= 0; idx
< len
; idx
++)
7837 /* Extract the already-expanded base class. */
7838 base
= TREE_VEC_ELT (expanded_bases
, idx
);
7840 /* Substitute to figure out the base class. */
7841 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
7844 if (base
== error_mark_node
)
7847 base_list
= tree_cons (access
, base
, base_list
);
7848 if (BINFO_VIRTUAL_P (pbase_binfo
))
7849 TREE_TYPE (base_list
) = integer_type_node
;
7853 /* The list is now in reverse order; correct that. */
7854 base_list
= nreverse (base_list
);
7857 pop_scope (pushed_scope
);
7859 /* Now call xref_basetypes to set up all the base-class
7861 xref_basetypes (type
, base_list
);
7863 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
7864 (int) ATTR_FLAG_TYPE_IN_PLACE
,
7865 args
, tf_error
, NULL_TREE
);
7867 /* Now that our base classes are set up, enter the scope of the
7868 class, so that name lookups into base classes, etc. will work
7869 correctly. This is precisely analogous to what we do in
7870 begin_class_definition when defining an ordinary non-template
7871 class, except we also need to push the enclosing classes. */
7872 push_nested_class (type
);
7874 /* Now members are processed in the order of declaration. */
7875 for (member
= CLASSTYPE_DECL_LIST (pattern
);
7876 member
; member
= TREE_CHAIN (member
))
7878 tree t
= TREE_VALUE (member
);
7880 if (TREE_PURPOSE (member
))
7884 /* Build new CLASSTYPE_NESTED_UTDS. */
7887 bool class_template_p
;
7889 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
7890 && TYPE_LANG_SPECIFIC (t
)
7891 && CLASSTYPE_IS_TEMPLATE (t
));
7892 /* If the member is a class template, then -- even after
7893 substitution -- there may be dependent types in the
7894 template argument list for the class. We increment
7895 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
7896 that function will assume that no types are dependent
7897 when outside of a template. */
7898 if (class_template_p
)
7899 ++processing_template_decl
;
7900 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
7901 if (class_template_p
)
7902 --processing_template_decl
;
7903 if (newtag
== error_mark_node
)
7906 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
7908 tree name
= TYPE_IDENTIFIER (t
);
7910 if (class_template_p
)
7911 /* Unfortunately, lookup_template_class sets
7912 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
7913 instantiation (i.e., for the type of a member
7914 template class nested within a template class.)
7915 This behavior is required for
7916 maybe_process_partial_specialization to work
7917 correctly, but is not accurate in this case;
7918 the TAG is not an instantiation of anything.
7919 (The corresponding TEMPLATE_DECL is an
7920 instantiation, but the TYPE is not.) */
7921 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
7923 /* Now, we call pushtag to put this NEWTAG into the scope of
7924 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
7925 pushtag calling push_template_decl. We don't have to do
7926 this for enums because it will already have been done in
7929 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
7930 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
7933 else if (TREE_CODE (t
) == FUNCTION_DECL
7934 || DECL_FUNCTION_TEMPLATE_P (t
))
7936 /* Build new TYPE_METHODS. */
7939 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7940 ++processing_template_decl
;
7941 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
7942 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7943 --processing_template_decl
;
7944 set_current_access_from_decl (r
);
7945 finish_member_declaration (r
);
7949 /* Build new TYPE_FIELDS. */
7950 if (TREE_CODE (t
) == STATIC_ASSERT
)
7953 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
7954 tf_warning_or_error
, NULL_TREE
,
7955 /*integral_constant_expression_p=*/true);
7956 finish_static_assert (condition
,
7957 STATIC_ASSERT_MESSAGE (t
),
7958 STATIC_ASSERT_SOURCE_LOCATION (t
),
7961 else if (TREE_CODE (t
) != CONST_DECL
)
7965 /* The file and line for this declaration, to
7966 assist in error message reporting. Since we
7967 called push_tinst_level above, we don't need to
7969 input_location
= DECL_SOURCE_LOCATION (t
);
7971 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7972 ++processing_template_decl
;
7973 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
7974 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7975 --processing_template_decl
;
7976 if (TREE_CODE (r
) == VAR_DECL
)
7980 [t]he initialization (and any associated
7981 side-effects) of a static data member does
7982 not occur unless the static data member is
7983 itself used in a way that requires the
7984 definition of the static data member to
7987 Therefore, we do not substitute into the
7988 initialized for the static data member here. */
7989 finish_static_data_member_decl
7992 /*init_const_expr_p=*/false,
7993 /*asmspec_tree=*/NULL_TREE
,
7995 if (DECL_INITIALIZED_IN_CLASS_P (r
))
7996 check_static_variable_definition (r
, TREE_TYPE (r
));
7998 else if (TREE_CODE (r
) == FIELD_DECL
)
8000 /* Determine whether R has a valid type and can be
8001 completed later. If R is invalid, then it is
8002 replaced by error_mark_node so that it will not be
8003 added to TYPE_FIELDS. */
8004 tree rtype
= TREE_TYPE (r
);
8005 if (can_complete_type_without_circularity (rtype
))
8006 complete_type (rtype
);
8008 if (!COMPLETE_TYPE_P (rtype
))
8010 cxx_incomplete_type_error (r
, rtype
);
8011 r
= error_mark_node
;
8015 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8016 such a thing will already have been added to the field
8017 list by tsubst_enum in finish_member_declaration in the
8018 CLASSTYPE_NESTED_UTDS case above. */
8019 if (!(TREE_CODE (r
) == TYPE_DECL
8020 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
8021 && DECL_ARTIFICIAL (r
)))
8023 set_current_access_from_decl (r
);
8024 finish_member_declaration (r
);
8031 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
))
8033 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8035 tree friend_type
= t
;
8036 bool adjust_processing_template_decl
= false;
8038 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
8040 /* template <class T> friend class C; */
8041 friend_type
= tsubst_friend_class (friend_type
, args
);
8042 adjust_processing_template_decl
= true;
8044 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
8046 /* template <class T> friend class C::D; */
8047 friend_type
= tsubst (friend_type
, args
,
8048 tf_warning_or_error
, NULL_TREE
);
8049 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
8050 friend_type
= TREE_TYPE (friend_type
);
8051 adjust_processing_template_decl
= true;
8053 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
)
8055 /* This could be either
8059 when dependent_type_p is false or
8061 template <class U> friend class T::C;
8064 friend_type
= tsubst (friend_type
, args
,
8065 tf_warning_or_error
, NULL_TREE
);
8066 /* Bump processing_template_decl for correct
8067 dependent_type_p calculation. */
8068 ++processing_template_decl
;
8069 if (dependent_type_p (friend_type
))
8070 adjust_processing_template_decl
= true;
8071 --processing_template_decl
;
8073 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
8074 && hidden_name_p (TYPE_NAME (friend_type
)))
8078 where C hasn't been declared yet. Let's lookup name
8079 from namespace scope directly, bypassing any name that
8080 come from dependent base class. */
8081 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
8083 /* The call to xref_tag_from_type does injection for friend
8085 push_nested_namespace (ns
);
8087 xref_tag_from_type (friend_type
, NULL_TREE
,
8088 /*tag_scope=*/ts_current
);
8089 pop_nested_namespace (ns
);
8091 else if (uses_template_parms (friend_type
))
8092 /* friend class C<T>; */
8093 friend_type
= tsubst (friend_type
, args
,
8094 tf_warning_or_error
, NULL_TREE
);
8099 where C is already declared or
8101 friend class C<int>;
8103 We don't have to do anything in these cases. */
8105 if (adjust_processing_template_decl
)
8106 /* Trick make_friend_class into realizing that the friend
8107 we're adding is a template, not an ordinary class. It's
8108 important that we use make_friend_class since it will
8109 perform some error-checking and output cross-reference
8111 ++processing_template_decl
;
8113 if (friend_type
!= error_mark_node
)
8114 make_friend_class (type
, friend_type
, /*complain=*/false);
8116 if (adjust_processing_template_decl
)
8117 --processing_template_decl
;
8121 /* Build new DECL_FRIENDLIST. */
8124 /* The file and line for this declaration, to
8125 assist in error message reporting. Since we
8126 called push_tinst_level above, we don't need to
8128 input_location
= DECL_SOURCE_LOCATION (t
);
8130 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8132 ++processing_template_decl
;
8133 push_deferring_access_checks (dk_no_check
);
8136 r
= tsubst_friend_function (t
, args
);
8137 add_friend (type
, r
, /*complain=*/false);
8138 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8140 pop_deferring_access_checks ();
8141 --processing_template_decl
;
8147 /* Set the file and line number information to whatever is given for
8148 the class itself. This puts error messages involving generated
8149 implicit functions at a predictable point, and the same point
8150 that would be used for non-template classes. */
8151 input_location
= DECL_SOURCE_LOCATION (typedecl
);
8153 unreverse_member_declarations (type
);
8154 finish_struct_1 (type
);
8155 TYPE_BEING_DEFINED (type
) = 0;
8157 /* Now that the class is complete, instantiate default arguments for
8158 any member functions. We don't do this earlier because the
8159 default arguments may reference members of the class. */
8160 if (!PRIMARY_TEMPLATE_P (templ
))
8161 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
8162 if (TREE_CODE (t
) == FUNCTION_DECL
8163 /* Implicitly generated member functions will not have template
8164 information; they are not instantiations, but instead are
8165 created "fresh" for each instantiation. */
8166 && DECL_TEMPLATE_INFO (t
))
8167 tsubst_default_arguments (t
);
8169 /* Some typedefs referenced from within the template code need to be access
8170 checked at template instantiation time, i.e now. These types were
8171 added to the template at parsing time. Let's get those and perform
8172 the access checks then. */
8173 perform_typedefs_access_check (pattern
, args
);
8174 perform_deferred_access_checks ();
8175 pop_nested_class ();
8176 maximum_field_alignment
= saved_maximum_field_alignment
;
8177 pop_from_top_level ();
8178 pop_deferring_access_checks ();
8181 /* The vtable for a template class can be emitted in any translation
8182 unit in which the class is instantiated. When there is no key
8183 method, however, finish_struct_1 will already have added TYPE to
8184 the keyed_classes list. */
8185 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
8186 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
8192 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
8198 else if (TYPE_P (t
))
8199 r
= tsubst (t
, args
, complain
, in_decl
);
8202 r
= tsubst_expr (t
, args
, complain
, in_decl
,
8203 /*integral_constant_expression_p=*/true);
8204 r
= fold_non_dependent_expr (r
);
8209 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
8210 NONTYPE_ARGUMENT_PACK. */
8213 make_fnparm_pack (tree spec_parm
)
8215 /* Collect all of the extra "packed" parameters into an
8219 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
8220 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8221 int i
, len
= list_length (spec_parm
);
8223 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
8224 parmvec
= make_tree_vec (len
);
8225 parmtypevec
= make_tree_vec (len
);
8226 for (i
= 0; i
< len
; i
++, spec_parm
= TREE_CHAIN (spec_parm
))
8228 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
8229 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
8232 /* Build the argument packs. */
8233 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
8234 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
8235 TREE_TYPE (argpack
) = argtypepack
;
8240 /* Substitute ARGS into T, which is an pack expansion
8241 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
8242 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
8243 (if only a partial substitution could be performed) or
8244 ERROR_MARK_NODE if there was an error. */
8246 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
8250 tree pack
, packs
= NULL_TREE
, unsubstituted_packs
= NULL_TREE
;
8254 htab_t saved_local_specializations
= NULL
;
8256 gcc_assert (PACK_EXPANSION_P (t
));
8257 pattern
= PACK_EXPANSION_PATTERN (t
);
8259 /* Determine the argument packs that will instantiate the parameter
8260 packs used in the expansion expression. While we're at it,
8261 compute the number of arguments to be expanded and make sure it
8263 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
8264 pack
= TREE_CHAIN (pack
))
8266 tree parm_pack
= TREE_VALUE (pack
);
8267 tree arg_pack
= NULL_TREE
;
8268 tree orig_arg
= NULL_TREE
;
8270 if (TREE_CODE (parm_pack
) == PARM_DECL
)
8272 if (!cp_unevaluated_operand
)
8273 arg_pack
= retrieve_local_specialization (parm_pack
);
8276 /* We can't rely on local_specializations for a parameter
8277 name used later in a function declaration (such as in a
8278 late-specified return type). Even if it exists, it might
8279 have the wrong value for a recursive call. Just make a
8280 dummy decl, since it's only used for its type. */
8281 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
8282 arg_pack
= make_fnparm_pack (arg_pack
);
8287 int level
, idx
, levels
;
8288 template_parm_level_and_index (parm_pack
, &level
, &idx
);
8290 levels
= TMPL_ARGS_DEPTH (args
);
8291 if (level
<= levels
)
8292 arg_pack
= TMPL_ARG (args
, level
, idx
);
8295 orig_arg
= arg_pack
;
8296 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
8297 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
8299 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
8300 /* This can only happen if we forget to expand an argument
8301 pack somewhere else. Just return an error, silently. */
8303 result
= make_tree_vec (1);
8304 TREE_VEC_ELT (result
, 0) = error_mark_node
;
8309 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
)) == 1
8310 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0)))
8312 tree expansion
= TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0);
8313 tree pattern
= PACK_EXPANSION_PATTERN (expansion
);
8314 if ((TYPE_P (pattern
) && same_type_p (pattern
, parm_pack
))
8315 || (!TYPE_P (pattern
) && cp_tree_equal (parm_pack
, pattern
)))
8316 /* The argument pack that the parameter maps to is just an
8317 expansion of the parameter itself, such as one would
8318 find in the implicit typedef of a class inside the
8319 class itself. Consider this parameter "unsubstituted",
8320 so that we will maintain the outer pack expansion. */
8321 arg_pack
= NULL_TREE
;
8327 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
8329 /* It's all-or-nothing with incomplete argument packs. */
8330 if (incomplete
&& !ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
8331 return error_mark_node
;
8333 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
8338 else if (len
!= my_len
)
8341 /* We got explicit args for some packs but not others;
8342 do nothing now and try again after deduction. */
8344 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
8345 error ("mismatched argument pack lengths while expanding "
8349 error ("mismatched argument pack lengths while expanding "
8352 return error_mark_node
;
8355 /* Keep track of the parameter packs and their corresponding
8357 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
8358 TREE_TYPE (packs
) = orig_arg
;
8361 /* We can't substitute for this parameter pack. */
8362 unsubstituted_packs
= tree_cons (TREE_PURPOSE (pack
),
8364 unsubstituted_packs
);
8367 /* We cannot expand this expansion expression, because we don't have
8368 all of the argument packs we need. Substitute into the pattern
8369 and return a PACK_EXPANSION_*. The caller will need to deal with
8371 if (unsubstituted_packs
)
8374 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
8375 new_pat
= tsubst_expr (pattern
, args
, complain
, in_decl
,
8376 /*integral_constant_expression_p=*/false);
8378 new_pat
= tsubst (pattern
, args
, complain
, in_decl
);
8379 return make_pack_expansion (new_pat
);
8382 /* We could not find any argument packs that work. */
8384 return error_mark_node
;
8386 if (cp_unevaluated_operand
)
8388 /* We're in a late-specified return type, so create our own local
8389 specializations table; the current table is either NULL or (in the
8390 case of recursive unification) might have bindings that we don't
8391 want to use or alter. */
8392 saved_local_specializations
= local_specializations
;
8393 local_specializations
= htab_create (37,
8394 hash_local_specialization
,
8395 eq_local_specializations
,
8399 /* For each argument in each argument pack, substitute into the
8401 result
= make_tree_vec (len
+ incomplete
);
8402 for (i
= 0; i
< len
+ incomplete
; ++i
)
8404 /* For parameter pack, change the substitution of the parameter
8405 pack to the ith argument in its argument pack, then expand
8407 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
8409 tree parm
= TREE_PURPOSE (pack
);
8411 if (TREE_CODE (parm
) == PARM_DECL
)
8413 /* Select the Ith argument from the pack. */
8414 tree arg
= make_node (ARGUMENT_PACK_SELECT
);
8415 ARGUMENT_PACK_SELECT_FROM_PACK (arg
) = TREE_VALUE (pack
);
8416 ARGUMENT_PACK_SELECT_INDEX (arg
) = i
;
8418 register_local_specialization (arg
, parm
);
8424 template_parm_level_and_index (parm
, &level
, &idx
);
8428 /* Select the Ith argument from the pack. */
8429 value
= make_node (ARGUMENT_PACK_SELECT
);
8430 ARGUMENT_PACK_SELECT_FROM_PACK (value
) = TREE_VALUE (pack
);
8431 ARGUMENT_PACK_SELECT_INDEX (value
) = i
;
8434 /* Update the corresponding argument. */
8435 TMPL_ARG (args
, level
, idx
) = value
;
8439 /* Substitute into the PATTERN with the altered arguments. */
8440 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
8441 TREE_VEC_ELT (result
, i
) =
8442 tsubst_expr (pattern
, args
, complain
, in_decl
,
8443 /*integral_constant_expression_p=*/false);
8445 TREE_VEC_ELT (result
, i
) = tsubst (pattern
, args
, complain
, in_decl
);
8448 /* When we have incomplete argument packs, the last "expanded"
8449 result is itself a pack expansion, which allows us
8450 to deduce more arguments. */
8451 TREE_VEC_ELT (result
, i
) =
8452 make_pack_expansion (TREE_VEC_ELT (result
, i
));
8454 if (TREE_VEC_ELT (result
, i
) == error_mark_node
)
8456 result
= error_mark_node
;
8461 /* Update ARGS to restore the substitution from parameter packs to
8462 their argument packs. */
8463 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
8465 tree parm
= TREE_PURPOSE (pack
);
8467 if (TREE_CODE (parm
) == PARM_DECL
)
8468 register_local_specialization (TREE_TYPE (pack
), parm
);
8472 template_parm_level_and_index (parm
, &level
, &idx
);
8474 /* Update the corresponding argument. */
8475 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
8476 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
8479 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
8483 if (saved_local_specializations
)
8485 htab_delete (local_specializations
);
8486 local_specializations
= saved_local_specializations
;
8492 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
8493 TMPL. We do this using DECL_PARM_INDEX, which should work even with
8494 parameter packs; all parms generated from a function parameter pack will
8495 have the same DECL_PARM_INDEX. */
8498 get_pattern_parm (tree parm
, tree tmpl
)
8500 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
8503 if (DECL_ARTIFICIAL (parm
))
8505 for (patparm
= DECL_ARGUMENTS (pattern
);
8506 patparm
; patparm
= TREE_CHAIN (patparm
))
8507 if (DECL_ARTIFICIAL (patparm
)
8508 && DECL_NAME (parm
) == DECL_NAME (patparm
))
8513 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
8514 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
8515 gcc_assert (DECL_PARM_INDEX (patparm
)
8516 == DECL_PARM_INDEX (parm
));
8522 /* Substitute ARGS into the vector or list of template arguments T. */
8525 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
8528 int len
= TREE_VEC_LENGTH (t
);
8529 int need_new
= 0, i
, expanded_len_adjust
= 0, out
;
8530 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
8532 for (i
= 0; i
< len
; i
++)
8534 tree orig_arg
= TREE_VEC_ELT (t
, i
);
8537 if (TREE_CODE (orig_arg
) == TREE_VEC
)
8538 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
8539 else if (PACK_EXPANSION_P (orig_arg
))
8541 /* Substitute into an expansion expression. */
8542 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
8544 if (TREE_CODE (new_arg
) == TREE_VEC
)
8545 /* Add to the expanded length adjustment the number of
8546 expanded arguments. We subtract one from this
8547 measurement, because the argument pack expression
8548 itself is already counted as 1 in
8549 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
8550 the argument pack is empty. */
8551 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
8553 else if (ARGUMENT_PACK_P (orig_arg
))
8555 /* Substitute into each of the arguments. */
8556 new_arg
= TYPE_P (orig_arg
)
8557 ? cxx_make_type (TREE_CODE (orig_arg
))
8558 : make_node (TREE_CODE (orig_arg
));
8560 SET_ARGUMENT_PACK_ARGS (
8562 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
8563 args
, complain
, in_decl
));
8565 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
8566 new_arg
= error_mark_node
;
8568 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
8569 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
8571 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
8573 if (TREE_TYPE (new_arg
) == error_mark_node
)
8574 new_arg
= error_mark_node
;
8578 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
8580 if (new_arg
== error_mark_node
)
8581 return error_mark_node
;
8584 if (new_arg
!= orig_arg
)
8591 /* Make space for the expanded arguments coming from template
8593 t
= make_tree_vec (len
+ expanded_len_adjust
);
8594 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
8595 arguments for a member template.
8596 In that case each TREE_VEC in ORIG_T represents a level of template
8597 arguments, and ORIG_T won't carry any non defaulted argument count.
8598 It will rather be the nested TREE_VECs that will carry one.
8599 In other words, ORIG_T carries a non defaulted argument count only
8600 if it doesn't contain any nested TREE_VEC. */
8601 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
8603 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
8604 count
+= expanded_len_adjust
;
8605 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
8607 for (i
= 0, out
= 0; i
< len
; i
++)
8609 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
8610 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
8611 && TREE_CODE (elts
[i
]) == TREE_VEC
)
8615 /* Now expand the template argument pack "in place". */
8616 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
8617 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
8621 TREE_VEC_ELT (t
, out
) = elts
[i
];
8629 /* Return the result of substituting ARGS into the template parameters
8630 given by PARMS. If there are m levels of ARGS and m + n levels of
8631 PARMS, then the result will contain n levels of PARMS. For
8632 example, if PARMS is `template <class T> template <class U>
8633 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
8634 result will be `template <int*, double, class V>'. */
8637 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
8642 /* When substituting into a template, we must set
8643 PROCESSING_TEMPLATE_DECL as the template parameters may be
8644 dependent if they are based on one-another, and the dependency
8645 predicates are short-circuit outside of templates. */
8646 ++processing_template_decl
;
8648 for (new_parms
= &r
;
8649 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
8650 new_parms
= &(TREE_CHAIN (*new_parms
)),
8651 parms
= TREE_CHAIN (parms
))
8654 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
8657 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
8663 if (parms
== error_mark_node
)
8666 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
8668 if (tuple
== error_mark_node
)
8671 default_value
= TREE_PURPOSE (tuple
);
8672 parm_decl
= TREE_VALUE (tuple
);
8674 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
8675 if (TREE_CODE (parm_decl
) == PARM_DECL
8676 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
8677 parm_decl
= error_mark_node
;
8678 default_value
= tsubst_template_arg (default_value
, args
,
8679 complain
, NULL_TREE
);
8681 tuple
= build_tree_list (default_value
, parm_decl
);
8682 TREE_VEC_ELT (new_vec
, i
) = tuple
;
8686 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
8687 - TMPL_ARGS_DEPTH (args
)),
8688 new_vec
, NULL_TREE
);
8691 --processing_template_decl
;
8696 /* Substitute the ARGS into the indicated aggregate (or enumeration)
8697 type T. If T is not an aggregate or enumeration type, it is
8698 handled as if by tsubst. IN_DECL is as for tsubst. If
8699 ENTERING_SCOPE is nonzero, T is the context for a template which
8700 we are presently tsubst'ing. Return the substituted value. */
8703 tsubst_aggr_type (tree t
,
8705 tsubst_flags_t complain
,
8712 switch (TREE_CODE (t
))
8715 if (TYPE_PTRMEMFUNC_P (t
))
8716 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
8718 /* Else fall through. */
8721 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
8726 int saved_unevaluated_operand
;
8727 int saved_inhibit_evaluation_warnings
;
8729 /* In "sizeof(X<I>)" we need to evaluate "I". */
8730 saved_unevaluated_operand
= cp_unevaluated_operand
;
8731 cp_unevaluated_operand
= 0;
8732 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
8733 c_inhibit_evaluation_warnings
= 0;
8735 /* First, determine the context for the type we are looking
8737 context
= TYPE_CONTEXT (t
);
8740 context
= tsubst_aggr_type (context
, args
, complain
,
8741 in_decl
, /*entering_scope=*/1);
8742 /* If context is a nested class inside a class template,
8743 it may still need to be instantiated (c++/33959). */
8744 if (TYPE_P (context
))
8745 context
= complete_type (context
);
8748 /* Then, figure out what arguments are appropriate for the
8749 type we are trying to find. For example, given:
8751 template <class T> struct S;
8752 template <class T, class U> void f(T, U) { S<U> su; }
8754 and supposing that we are instantiating f<int, double>,
8755 then our ARGS will be {int, double}, but, when looking up
8756 S we only want {double}. */
8757 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
8759 if (argvec
== error_mark_node
)
8760 r
= error_mark_node
;
8763 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
8764 entering_scope
, complain
);
8765 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
8768 cp_unevaluated_operand
= saved_unevaluated_operand
;
8769 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
8774 /* This is not a template type, so there's nothing to do. */
8778 return tsubst (t
, args
, complain
, in_decl
);
8782 /* Substitute into the default argument ARG (a default argument for
8783 FN), which has the indicated TYPE. */
8786 tsubst_default_argument (tree fn
, tree type
, tree arg
)
8788 tree saved_class_ptr
= NULL_TREE
;
8789 tree saved_class_ref
= NULL_TREE
;
8791 /* This default argument came from a template. Instantiate the
8792 default argument here, not in tsubst. In the case of
8801 we must be careful to do name lookup in the scope of S<T>,
8802 rather than in the current class. */
8803 push_access_scope (fn
);
8804 /* The "this" pointer is not valid in a default argument. */
8807 saved_class_ptr
= current_class_ptr
;
8808 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
8809 saved_class_ref
= current_class_ref
;
8810 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
8813 push_deferring_access_checks(dk_no_deferred
);
8814 /* The default argument expression may cause implicitly defined
8815 member functions to be synthesized, which will result in garbage
8816 collection. We must treat this situation as if we were within
8817 the body of function so as to avoid collecting live data on the
8820 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
8821 tf_warning_or_error
, NULL_TREE
,
8822 /*integral_constant_expression_p=*/false);
8824 pop_deferring_access_checks();
8826 /* Restore the "this" pointer. */
8829 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
8830 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
8833 /* Make sure the default argument is reasonable. */
8834 arg
= check_default_argument (type
, arg
);
8836 pop_access_scope (fn
);
8841 /* Substitute into all the default arguments for FN. */
8844 tsubst_default_arguments (tree fn
)
8849 tmpl_args
= DECL_TI_ARGS (fn
);
8851 /* If this function is not yet instantiated, we certainly don't need
8852 its default arguments. */
8853 if (uses_template_parms (tmpl_args
))
8856 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
8858 arg
= TREE_CHAIN (arg
))
8859 if (TREE_PURPOSE (arg
))
8860 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
8862 TREE_PURPOSE (arg
));
8865 /* Substitute the ARGS into the T, which is a _DECL. Return the
8866 result of the substitution. Issue error and warning messages under
8867 control of COMPLAIN. */
8870 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
8872 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
8873 location_t saved_loc
;
8878 /* Set the filename and linenumber to improve error-reporting. */
8879 saved_loc
= input_location
;
8880 input_location
= DECL_SOURCE_LOCATION (t
);
8882 switch (TREE_CODE (t
))
8886 /* We can get here when processing a member function template,
8887 member class template, or template template parameter. */
8888 tree decl
= DECL_TEMPLATE_RESULT (t
);
8893 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
8895 /* Template template parameter is treated here. */
8896 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8897 if (new_type
== error_mark_node
)
8898 RETURN (error_mark_node
);
8901 TREE_CHAIN (r
) = NULL_TREE
;
8902 TREE_TYPE (r
) = new_type
;
8903 DECL_TEMPLATE_RESULT (r
)
8904 = build_decl (DECL_SOURCE_LOCATION (decl
),
8905 TYPE_DECL
, DECL_NAME (decl
), new_type
);
8906 DECL_TEMPLATE_PARMS (r
)
8907 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
8909 TYPE_NAME (new_type
) = r
;
8913 /* We might already have an instance of this template.
8914 The ARGS are for the surrounding class type, so the
8915 full args contain the tsubst'd args for the context,
8916 plus the innermost args from the template decl. */
8917 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
8918 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
8919 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
8920 /* Because this is a template, the arguments will still be
8921 dependent, even after substitution. If
8922 PROCESSING_TEMPLATE_DECL is not set, the dependency
8923 predicates will short-circuit. */
8924 ++processing_template_decl
;
8925 full_args
= tsubst_template_args (tmpl_args
, args
,
8927 --processing_template_decl
;
8928 if (full_args
== error_mark_node
)
8929 RETURN (error_mark_node
);
8931 /* If this is a default template template argument,
8932 tsubst might not have changed anything. */
8933 if (full_args
== tmpl_args
)
8936 hash
= hash_tmpl_and_args (t
, full_args
);
8937 spec
= retrieve_specialization (t
, full_args
, hash
);
8938 if (spec
!= NULL_TREE
)
8944 /* Make a new template decl. It will be similar to the
8945 original, but will record the current template arguments.
8946 We also create a new function declaration, which is just
8947 like the old one, but points to this new template, rather
8948 than the old one. */
8950 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
8951 TREE_CHAIN (r
) = NULL_TREE
;
8953 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
8955 if (TREE_CODE (decl
) == TYPE_DECL
)
8958 ++processing_template_decl
;
8959 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8960 --processing_template_decl
;
8961 if (new_type
== error_mark_node
)
8962 RETURN (error_mark_node
);
8964 TREE_TYPE (r
) = new_type
;
8965 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
8966 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
8967 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
8968 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
8973 ++processing_template_decl
;
8974 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
8975 --processing_template_decl
;
8976 if (new_decl
== error_mark_node
)
8977 RETURN (error_mark_node
);
8979 DECL_TEMPLATE_RESULT (r
) = new_decl
;
8980 DECL_TI_TEMPLATE (new_decl
) = r
;
8981 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
8982 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
8983 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
8986 SET_DECL_IMPLICIT_INSTANTIATION (r
);
8987 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
8988 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
8990 /* The template parameters for this new template are all the
8991 template parameters for the old template, except the
8992 outermost level of parameters. */
8993 DECL_TEMPLATE_PARMS (r
)
8994 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
8997 if (PRIMARY_TEMPLATE_P (t
))
8998 DECL_PRIMARY_TEMPLATE (r
) = r
;
9000 if (TREE_CODE (decl
) != TYPE_DECL
)
9001 /* Record this non-type partial instantiation. */
9002 register_specialization (r
, t
,
9003 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
9011 tree argvec
= NULL_TREE
;
9019 /* Nobody should be tsubst'ing into non-template functions. */
9020 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
9022 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
9027 /* If T is not dependent, just return it. We have to
9028 increment PROCESSING_TEMPLATE_DECL because
9029 value_dependent_expression_p assumes that nothing is
9030 dependent when PROCESSING_TEMPLATE_DECL is zero. */
9031 ++processing_template_decl
;
9032 dependent_p
= value_dependent_expression_p (t
);
9033 --processing_template_decl
;
9037 /* Calculate the most general template of which R is a
9038 specialization, and the complete set of arguments used to
9040 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
9041 argvec
= tsubst_template_args (DECL_TI_ARGS
9042 (DECL_TEMPLATE_RESULT
9043 (DECL_TI_TEMPLATE (t
))),
9044 args
, complain
, in_decl
);
9046 /* Check to see if we already have this specialization. */
9047 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
9048 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
9056 /* We can see more levels of arguments than parameters if
9057 there was a specialization of a member template, like
9060 template <class T> struct S { template <class U> void f(); }
9061 template <> template <class U> void S<int>::f(U);
9063 Here, we'll be substituting into the specialization,
9064 because that's where we can find the code we actually
9065 want to generate, but we'll have enough arguments for
9066 the most general template.
9068 We also deal with the peculiar case:
9070 template <class T> struct S {
9071 template <class U> friend void f();
9073 template <class U> void f() {}
9075 template void f<double>();
9077 Here, the ARGS for the instantiation of will be {int,
9078 double}. But, we only need as many ARGS as there are
9079 levels of template parameters in CODE_PATTERN. We are
9080 careful not to get fooled into reducing the ARGS in
9083 template <class T> struct S { template <class U> void f(U); }
9084 template <class T> template <> void S<T>::f(int) {}
9086 which we can spot because the pattern will be a
9087 specialization in this case. */
9088 args_depth
= TMPL_ARGS_DEPTH (args
);
9090 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
9091 if (args_depth
> parms_depth
9092 && !DECL_TEMPLATE_SPECIALIZATION (t
))
9093 args
= get_innermost_template_args (args
, parms_depth
);
9097 /* This special case arises when we have something like this:
9099 template <class T> struct S {
9100 friend void f<int>(int, double);
9103 Here, the DECL_TI_TEMPLATE for the friend declaration
9104 will be an IDENTIFIER_NODE. We are being called from
9105 tsubst_friend_function, and we want only to create a
9106 new decl (R) with appropriate types so that we can call
9107 determine_specialization. */
9108 gen_tmpl
= NULL_TREE
;
9111 if (DECL_CLASS_SCOPE_P (t
))
9113 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
9117 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
9118 complain
, t
, /*entering_scope=*/1);
9123 ctx
= DECL_CONTEXT (t
);
9125 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9126 if (type
== error_mark_node
)
9127 RETURN (error_mark_node
);
9129 /* We do NOT check for matching decls pushed separately at this
9130 point, as they may not represent instantiations of this
9131 template, and in any case are considered separate under the
9134 DECL_USE_TEMPLATE (r
) = 0;
9135 TREE_TYPE (r
) = type
;
9136 /* Clear out the mangled name and RTL for the instantiation. */
9137 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
9138 SET_DECL_RTL (r
, NULL
);
9139 /* Leave DECL_INITIAL set on deleted instantiations. */
9140 if (!DECL_DELETED_FN (r
))
9141 DECL_INITIAL (r
) = NULL_TREE
;
9142 DECL_CONTEXT (r
) = ctx
;
9144 if (member
&& DECL_CONV_FN_P (r
))
9145 /* Type-conversion operator. Reconstruct the name, in
9146 case it's the name of one of the template's parameters. */
9147 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
9149 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
9151 DECL_RESULT (r
) = NULL_TREE
;
9153 TREE_STATIC (r
) = 0;
9154 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
9155 DECL_EXTERNAL (r
) = 1;
9156 /* If this is an instantiation of a function with internal
9157 linkage, we already know what object file linkage will be
9158 assigned to the instantiation. */
9159 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
9160 DECL_DEFER_OUTPUT (r
) = 0;
9161 TREE_CHAIN (r
) = NULL_TREE
;
9162 DECL_PENDING_INLINE_INFO (r
) = 0;
9163 DECL_PENDING_INLINE_P (r
) = 0;
9164 DECL_SAVED_TREE (r
) = NULL_TREE
;
9165 DECL_STRUCT_FUNCTION (r
) = NULL
;
9167 /* We'll re-clone as appropriate in instantiate_template. */
9168 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
9170 /* If we aren't complaining now, return on error before we register
9171 the specialization so that we'll complain eventually. */
9172 if ((complain
& tf_error
) == 0
9173 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
9174 && !grok_op_properties (r
, /*complain=*/false))
9175 RETURN (error_mark_node
);
9177 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
9178 this in the special friend case mentioned above where
9179 GEN_TMPL is NULL. */
9182 DECL_TEMPLATE_INFO (r
)
9183 = build_template_info (gen_tmpl
, argvec
);
9184 SET_DECL_IMPLICIT_INSTANTIATION (r
);
9185 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
9187 /* We're not supposed to instantiate default arguments
9188 until they are called, for a template. But, for a
9191 template <class T> void f ()
9192 { extern void g(int i = T()); }
9194 we should do the substitution when the template is
9195 instantiated. We handle the member function case in
9196 instantiate_class_template since the default arguments
9197 might refer to other members of the class. */
9199 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9200 && !uses_template_parms (argvec
))
9201 tsubst_default_arguments (r
);
9204 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
9206 /* Copy the list of befriending classes. */
9207 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
9209 friends
= &TREE_CHAIN (*friends
))
9211 *friends
= copy_node (*friends
);
9212 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
9217 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
9219 maybe_retrofit_in_chrg (r
);
9220 if (DECL_CONSTRUCTOR_P (r
))
9221 grok_ctor_properties (ctx
, r
);
9222 /* If this is an instantiation of a member template, clone it.
9223 If it isn't, that'll be handled by
9224 clone_constructors_and_destructors. */
9225 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
9226 clone_function_decl (r
, /*update_method_vec_p=*/0);
9228 else if ((complain
& tf_error
) != 0
9229 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
9230 && !grok_op_properties (r
, /*complain=*/true))
9231 RETURN (error_mark_node
);
9233 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
9234 SET_DECL_FRIEND_CONTEXT (r
,
9235 tsubst (DECL_FRIEND_CONTEXT (t
),
9236 args
, complain
, in_decl
));
9238 /* Possibly limit visibility based on template args. */
9239 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
9240 if (DECL_VISIBILITY_SPECIFIED (t
))
9242 DECL_VISIBILITY_SPECIFIED (r
) = 0;
9244 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
9246 determine_visibility (r
);
9247 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
9248 && !processing_template_decl
)
9249 defaulted_late_check (r
);
9251 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
9252 args
, complain
, in_decl
);
9258 tree type
= NULL_TREE
;
9260 tree expanded_types
= NULL_TREE
;
9261 tree prev_r
= NULL_TREE
;
9262 tree first_r
= NULL_TREE
;
9264 if (FUNCTION_PARAMETER_PACK_P (t
))
9266 /* If there is a local specialization that isn't a
9267 parameter pack, it means that we're doing a "simple"
9268 substitution from inside tsubst_pack_expansion. Just
9269 return the local specialization (which will be a single
9271 tree spec
= retrieve_local_specialization (t
);
9273 && TREE_CODE (spec
) == PARM_DECL
9274 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
9277 /* Expand the TYPE_PACK_EXPANSION that provides the types for
9278 the parameters in this function parameter pack. */
9279 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
9281 if (TREE_CODE (expanded_types
) == TREE_VEC
)
9283 len
= TREE_VEC_LENGTH (expanded_types
);
9285 /* Zero-length parameter packs are boring. Just substitute
9288 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
9293 /* All we did was update the type. Make a note of that. */
9294 type
= expanded_types
;
9295 expanded_types
= NULL_TREE
;
9299 /* Loop through all of the parameter's we'll build. When T is
9300 a function parameter pack, LEN is the number of expanded
9301 types in EXPANDED_TYPES; otherwise, LEN is 1. */
9303 for (i
= 0; i
< len
; ++i
)
9307 if (DECL_TEMPLATE_PARM_P (t
))
9308 SET_DECL_TEMPLATE_PARM_P (r
);
9310 /* An argument of a function parameter pack is not a parameter
9312 FUNCTION_PARAMETER_PACK_P (r
) = false;
9315 /* We're on the Ith parameter of the function parameter
9318 /* Get the Ith type. */
9319 type
= TREE_VEC_ELT (expanded_types
, i
);
9322 /* Rename the parameter to include the index. */
9324 make_ith_pack_parameter_name (DECL_NAME (r
), i
);
9327 /* We're dealing with a normal parameter. */
9328 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9330 type
= type_decays_to (type
);
9331 TREE_TYPE (r
) = type
;
9332 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
9334 if (DECL_INITIAL (r
))
9336 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
9337 DECL_INITIAL (r
) = TREE_TYPE (r
);
9339 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
9343 DECL_CONTEXT (r
) = NULL_TREE
;
9345 if (!DECL_TEMPLATE_PARM_P (r
))
9346 DECL_ARG_TYPE (r
) = type_passed_as (type
);
9348 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
9349 args
, complain
, in_decl
);
9351 /* Keep track of the first new parameter we
9352 generate. That's what will be returned to the
9357 /* Build a proper chain of parameters when substituting
9358 into a function parameter pack. */
9360 TREE_CHAIN (prev_r
) = r
;
9364 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
,
9365 complain
, TREE_CHAIN (t
));
9367 /* FIRST_R contains the start of the chain we've built. */
9377 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9378 if (type
== error_mark_node
)
9379 RETURN (error_mark_node
);
9380 TREE_TYPE (r
) = type
;
9381 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
9383 /* DECL_INITIAL gives the number of bits in a bit-field. */
9385 = tsubst_expr (DECL_INITIAL (t
), args
,
9387 /*integral_constant_expression_p=*/true);
9388 /* We don't have to set DECL_CONTEXT here; it is set by
9389 finish_member_declaration. */
9390 TREE_CHAIN (r
) = NULL_TREE
;
9391 if (VOID_TYPE_P (type
))
9392 error ("instantiation of %q+D as type %qT", r
, type
);
9394 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
9395 args
, complain
, in_decl
);
9400 /* We reach here only for member using decls. */
9401 if (DECL_DEPENDENT_P (t
))
9403 r
= do_class_using_decl
9404 (tsubst_copy (USING_DECL_SCOPE (t
), args
, complain
, in_decl
),
9405 tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
));
9407 r
= error_mark_node
;
9410 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
9411 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
9417 TREE_CHAIN (r
) = NULL_TREE
;
9424 tree argvec
= NULL_TREE
;
9425 tree gen_tmpl
= NULL_TREE
;
9427 tree tmpl
= NULL_TREE
;
9429 tree type
= NULL_TREE
;
9432 if (TREE_CODE (t
) == TYPE_DECL
9433 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
9435 /* If this is the canonical decl, we don't have to
9436 mess with instantiations, and often we can't (for
9437 typename, template type parms and such). Note that
9438 TYPE_NAME is not correct for the above test if
9439 we've copied the type for a typedef. */
9440 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9441 if (type
== error_mark_node
)
9442 RETURN (error_mark_node
);
9443 r
= TYPE_NAME (type
);
9447 /* Check to see if we already have the specialization we
9450 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
9452 /* T is a static data member or namespace-scope entity.
9453 We have to substitute into namespace-scope variables
9454 (even though such entities are never templates) because
9457 template <class T> void f() { extern T t; }
9459 where the entity referenced is not known until
9460 instantiation time. */
9462 ctx
= DECL_CONTEXT (t
);
9463 if (DECL_CLASS_SCOPE_P (t
))
9465 ctx
= tsubst_aggr_type (ctx
, args
,
9467 in_decl
, /*entering_scope=*/1);
9468 /* If CTX is unchanged, then T is in fact the
9469 specialization we want. That situation occurs when
9470 referencing a static data member within in its own
9471 class. We can use pointer equality, rather than
9472 same_type_p, because DECL_CONTEXT is always
9474 if (ctx
== DECL_CONTEXT (t
))
9480 tmpl
= DECL_TI_TEMPLATE (t
);
9481 gen_tmpl
= most_general_template (tmpl
);
9482 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
9483 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
9484 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
9489 /* A local variable. */
9491 /* Subsequent calls to pushdecl will fill this in. */
9493 spec
= retrieve_local_specialization (t
);
9495 /* If we already have the specialization we need, there is
9496 nothing more to do. */
9503 /* Create a new node for the specialization we need. */
9505 if (type
== NULL_TREE
)
9507 if (is_typedef_decl (t
))
9508 type
= DECL_ORIGINAL_TYPE (t
);
9510 type
= TREE_TYPE (t
);
9511 if (TREE_CODE (t
) == VAR_DECL
&& VAR_HAD_UNKNOWN_BOUND (t
))
9512 type
= strip_array_domain (type
);
9513 type
= tsubst (type
, args
, complain
, in_decl
);
9515 if (TREE_CODE (r
) == VAR_DECL
)
9517 /* Even if the original location is out of scope, the
9518 newly substituted one is not. */
9519 DECL_DEAD_FOR_LOCAL (r
) = 0;
9520 DECL_INITIALIZED_P (r
) = 0;
9521 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
9522 if (type
== error_mark_node
)
9523 RETURN (error_mark_node
);
9524 if (TREE_CODE (type
) == FUNCTION_TYPE
)
9526 /* It may seem that this case cannot occur, since:
9531 declares a function, not a variable. However:
9534 template <typename T> void g() { T t; }
9535 template void g<f>();
9537 is an attempt to declare a variable with function
9539 error ("variable %qD has function type",
9540 /* R is not yet sufficiently initialized, so we
9541 just use its name. */
9543 RETURN (error_mark_node
);
9545 type
= complete_type (type
);
9546 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
9547 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
);
9548 type
= check_var_type (DECL_NAME (r
), type
);
9550 if (DECL_HAS_VALUE_EXPR_P (t
))
9552 tree ve
= DECL_VALUE_EXPR (t
);
9553 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
9554 /*constant_expression_p=*/false);
9555 SET_DECL_VALUE_EXPR (r
, ve
);
9558 else if (DECL_SELF_REFERENCE_P (t
))
9559 SET_DECL_SELF_REFERENCE_P (r
);
9560 TREE_TYPE (r
) = type
;
9561 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
9562 DECL_CONTEXT (r
) = ctx
;
9563 /* Clear out the mangled name and RTL for the instantiation. */
9564 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
9565 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
9566 SET_DECL_RTL (r
, NULL
);
9567 /* The initializer must not be expanded until it is required;
9569 DECL_INITIAL (r
) = NULL_TREE
;
9570 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
9571 SET_DECL_RTL (r
, NULL
);
9572 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
9573 if (TREE_CODE (r
) == VAR_DECL
)
9575 /* Possibly limit visibility based on template args. */
9576 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
9577 if (DECL_VISIBILITY_SPECIFIED (t
))
9579 DECL_VISIBILITY_SPECIFIED (r
) = 0;
9581 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
9583 determine_visibility (r
);
9588 /* A static data member declaration is always marked
9589 external when it is declared in-class, even if an
9590 initializer is present. We mimic the non-template
9592 DECL_EXTERNAL (r
) = 1;
9594 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
9595 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
9596 SET_DECL_IMPLICIT_INSTANTIATION (r
);
9598 else if (cp_unevaluated_operand
)
9600 /* We're substituting this var in a decltype outside of its
9601 scope, such as for a lambda return type. Don't add it to
9602 local_specializations, do perform auto deduction. */
9603 tree auto_node
= type_uses_auto (type
);
9605 = tsubst_expr (DECL_INITIAL (t
), args
, complain
, in_decl
,
9606 /*constant_expression_p=*/false);
9608 if (auto_node
&& init
&& describable_type (init
))
9610 type
= do_auto_deduction (type
, init
, auto_node
);
9611 TREE_TYPE (r
) = type
;
9615 register_local_specialization (r
, t
);
9617 TREE_CHAIN (r
) = NULL_TREE
;
9619 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
9621 args
, complain
, in_decl
);
9623 /* Preserve a typedef that names a type. */
9624 if (is_typedef_decl (r
))
9626 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
9627 set_underlying_type (r
);
9640 /* Restore the file and line information. */
9641 input_location
= saved_loc
;
9646 /* Substitute into the ARG_TYPES of a function type. */
9649 tsubst_arg_types (tree arg_types
,
9651 tsubst_flags_t complain
,
9654 tree remaining_arg_types
;
9655 tree type
= NULL_TREE
;
9657 tree expanded_args
= NULL_TREE
;
9660 if (!arg_types
|| arg_types
== void_list_node
)
9663 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
9664 args
, complain
, in_decl
);
9665 if (remaining_arg_types
== error_mark_node
)
9666 return error_mark_node
;
9668 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
9670 /* For a pack expansion, perform substitution on the
9671 entire expression. Later on, we'll handle the arguments
9673 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
9674 args
, complain
, in_decl
);
9676 if (TREE_CODE (expanded_args
) == TREE_VEC
)
9677 /* So that we'll spin through the parameters, one by one. */
9678 i
= TREE_VEC_LENGTH (expanded_args
);
9681 /* We only partially substituted into the parameter
9682 pack. Our type is TYPE_PACK_EXPANSION. */
9683 type
= expanded_args
;
9684 expanded_args
= NULL_TREE
;
9692 type
= TREE_VEC_ELT (expanded_args
, i
);
9694 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
9696 if (type
== error_mark_node
)
9697 return error_mark_node
;
9698 if (VOID_TYPE_P (type
))
9700 if (complain
& tf_error
)
9702 error ("invalid parameter type %qT", type
);
9704 error ("in declaration %q+D", in_decl
);
9706 return error_mark_node
;
9709 /* Do array-to-pointer, function-to-pointer conversion, and ignore
9710 top-level qualifiers as required. */
9711 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
9713 /* We do not substitute into default arguments here. The standard
9714 mandates that they be instantiated only when needed, which is
9715 done in build_over_call. */
9716 default_arg
= TREE_PURPOSE (arg_types
);
9718 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
9720 /* We've instantiated a template before its default arguments
9721 have been parsed. This can happen for a nested template
9722 class, and is not an error unless we require the default
9723 argument in a call of this function. */
9724 remaining_arg_types
=
9725 tree_cons (default_arg
, type
, remaining_arg_types
);
9726 VEC_safe_push (tree
, gc
, DEFARG_INSTANTIATIONS (default_arg
),
9727 remaining_arg_types
);
9730 remaining_arg_types
=
9731 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
9734 return remaining_arg_types
;
9737 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
9738 *not* handle the exception-specification for FNTYPE, because the
9739 initial substitution of explicitly provided template parameters
9740 during argument deduction forbids substitution into the
9741 exception-specification:
9745 All references in the function type of the function template to the
9746 corresponding template parameters are replaced by the specified tem-
9747 plate argument values. If a substitution in a template parameter or
9748 in the function type of the function template results in an invalid
9749 type, type deduction fails. [Note: The equivalent substitution in
9750 exception specifications is done only when the function is instanti-
9751 ated, at which point a program is ill-formed if the substitution
9752 results in an invalid type.] */
9755 tsubst_function_type (tree t
,
9757 tsubst_flags_t complain
,
9764 /* The TYPE_CONTEXT is not used for function/method types. */
9765 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
9767 /* Substitute the return type. */
9768 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9769 if (return_type
== error_mark_node
)
9770 return error_mark_node
;
9771 /* The standard does not presently indicate that creation of a
9772 function type with an invalid return type is a deduction failure.
9773 However, that is clearly analogous to creating an array of "void"
9774 or a reference to a reference. This is core issue #486. */
9775 if (TREE_CODE (return_type
) == ARRAY_TYPE
9776 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
9778 if (complain
& tf_error
)
9780 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
9781 error ("function returning an array");
9783 error ("function returning a function");
9785 return error_mark_node
;
9788 /* Substitute the argument types. */
9789 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
,
9791 if (arg_types
== error_mark_node
)
9792 return error_mark_node
;
9794 /* Construct a new type node and return it. */
9795 if (TREE_CODE (t
) == FUNCTION_TYPE
)
9797 fntype
= build_function_type (return_type
, arg_types
);
9798 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
9802 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
9803 if (! MAYBE_CLASS_TYPE_P (r
))
9807 Type deduction may fail for any of the following
9810 -- Attempting to create "pointer to member of T" when T
9811 is not a class type. */
9812 if (complain
& tf_error
)
9813 error ("creating pointer to member function of non-class type %qT",
9815 return error_mark_node
;
9818 fntype
= build_method_type_directly (r
, return_type
,
9819 TREE_CHAIN (arg_types
));
9821 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
9826 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
9827 ARGS into that specification, and return the substituted
9828 specification. If there is no specification, return NULL_TREE. */
9831 tsubst_exception_specification (tree fntype
,
9833 tsubst_flags_t complain
,
9839 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
9840 new_specs
= NULL_TREE
;
9841 if (specs
&& TREE_PURPOSE (specs
))
9843 /* A noexcept-specifier. */
9844 new_specs
= tsubst_copy_and_build
9845 (TREE_PURPOSE (specs
), args
, complain
, in_decl
, /*function_p=*/false,
9846 /*integral_constant_expression_p=*/true);
9847 new_specs
= build_noexcept_spec (new_specs
, complain
);
9851 if (! TREE_VALUE (specs
))
9858 tree expanded_specs
= NULL_TREE
;
9860 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
9862 /* Expand the pack expansion type. */
9863 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
9867 if (expanded_specs
== error_mark_node
)
9868 return error_mark_node
;
9869 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
9870 len
= TREE_VEC_LENGTH (expanded_specs
);
9873 /* We're substituting into a member template, so
9874 we got a TYPE_PACK_EXPANSION back. Add that
9875 expansion and move on. */
9876 gcc_assert (TREE_CODE (expanded_specs
)
9877 == TYPE_PACK_EXPANSION
);
9878 new_specs
= add_exception_specifier (new_specs
,
9881 specs
= TREE_CHAIN (specs
);
9886 for (i
= 0; i
< len
; ++i
)
9889 spec
= TREE_VEC_ELT (expanded_specs
, i
);
9891 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
9892 if (spec
== error_mark_node
)
9894 new_specs
= add_exception_specifier (new_specs
, spec
,
9898 specs
= TREE_CHAIN (specs
);
9904 /* Take the tree structure T and replace template parameters used
9905 therein with the argument vector ARGS. IN_DECL is an associated
9906 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
9907 Issue error and warning messages under control of COMPLAIN. Note
9908 that we must be relatively non-tolerant of extensions here, in
9909 order to preserve conformance; if we allow substitutions that
9910 should not be allowed, we may allow argument deductions that should
9911 not succeed, and therefore report ambiguous overload situations
9912 where there are none. In theory, we could allow the substitution,
9913 but indicate that it should have failed, and allow our caller to
9914 make sure that the right thing happens, but we don't try to do this
9917 This function is used for dealing with types, decls and the like;
9918 for expressions, use tsubst_expr or tsubst_copy. */
9921 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9925 if (t
== NULL_TREE
|| t
== error_mark_node
9926 || t
== integer_type_node
9927 || t
== void_type_node
9928 || t
== char_type_node
9929 || t
== unknown_type_node
9930 || TREE_CODE (t
) == NAMESPACE_DECL
)
9934 return tsubst_decl (t
, args
, complain
);
9936 if (args
== NULL_TREE
)
9939 if (TREE_CODE (t
) == IDENTIFIER_NODE
)
9940 type
= IDENTIFIER_TYPE_VALUE (t
);
9942 type
= TREE_TYPE (t
);
9944 gcc_assert (type
!= unknown_type_node
);
9946 /* Reuse typedefs. We need to do this to handle dependent attributes,
9947 such as attribute aligned. */
9950 && TYPE_NAME (t
) != TYPE_MAIN_DECL (t
))
9952 tree decl
= TYPE_NAME (t
);
9954 if (DECL_CLASS_SCOPE_P (decl
)
9955 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
9956 && uses_template_parms (DECL_CONTEXT (decl
)))
9958 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
9959 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
9960 r
= retrieve_specialization (tmpl
, gen_args
, 0);
9962 else if (DECL_FUNCTION_SCOPE_P (decl
)
9963 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
9964 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
9965 r
= retrieve_local_specialization (decl
);
9967 /* The typedef is from a non-template context. */
9973 r
= cp_build_qualified_type_real
9974 (r
, cp_type_quals (t
) | cp_type_quals (r
),
9975 complain
| tf_ignore_bad_quals
);
9978 /* Else we must be instantiating the typedef, so fall through. */
9982 && TREE_CODE (t
) != TYPENAME_TYPE
9983 && TREE_CODE (t
) != TEMPLATE_TYPE_PARM
9984 && TREE_CODE (t
) != IDENTIFIER_NODE
9985 && TREE_CODE (t
) != FUNCTION_TYPE
9986 && TREE_CODE (t
) != METHOD_TYPE
)
9987 type
= tsubst (type
, args
, complain
, in_decl
);
9988 if (type
== error_mark_node
)
9989 return error_mark_node
;
9991 switch (TREE_CODE (t
))
9996 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
9997 /*entering_scope=*/0);
10000 case IDENTIFIER_NODE
:
10012 if (t
== integer_type_node
)
10015 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
10016 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
10020 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
10022 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
10023 /*integral_constant_expression_p=*/false);
10025 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
10027 if (TREE_CODE (max
) == NOP_EXPR
10028 && TREE_SIDE_EFFECTS (omax
)
10029 && !TREE_TYPE (max
))
10030 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
10032 max
= fold_decl_constant_value (max
);
10034 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
10035 with TREE_SIDE_EFFECTS that indicates this is not an integral
10036 constant expression. */
10037 if (processing_template_decl
10038 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
10040 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
10041 TREE_SIDE_EFFECTS (max
) = 1;
10044 if (TREE_CODE (max
) != INTEGER_CST
10045 && !at_function_scope_p ()
10046 && !TREE_SIDE_EFFECTS (max
)
10047 && !value_dependent_expression_p (max
))
10049 if (complain
& tf_error
)
10050 error ("array bound is not an integer constant");
10051 return error_mark_node
;
10056 Type deduction may fail for any of the following
10059 Attempting to create an array with a size that is
10060 zero or negative. */
10061 if (integer_zerop (max
) && !(complain
& tf_error
))
10062 /* We must fail if performing argument deduction (as
10063 indicated by the state of complain), so that
10064 another substitution can be found. */
10065 return error_mark_node
;
10066 else if (TREE_CODE (max
) == INTEGER_CST
10067 && INT_CST_LT (max
, integer_zero_node
))
10069 if (complain
& tf_error
)
10070 error ("creating array with negative size (%qE)", max
);
10072 return error_mark_node
;
10075 return compute_array_index_type (NULL_TREE
, max
);
10078 case TEMPLATE_TYPE_PARM
:
10079 case TEMPLATE_TEMPLATE_PARM
:
10080 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10081 case TEMPLATE_PARM_INDEX
:
10086 tree arg
= NULL_TREE
;
10090 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
10091 template_parm_level_and_index (t
, &level
, &idx
);
10093 levels
= TMPL_ARGS_DEPTH (args
);
10094 if (level
<= levels
)
10096 arg
= TMPL_ARG (args
, level
, idx
);
10098 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
10099 /* See through ARGUMENT_PACK_SELECT arguments. */
10100 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
10103 if (arg
== error_mark_node
)
10104 return error_mark_node
;
10105 else if (arg
!= NULL_TREE
)
10107 if (ARGUMENT_PACK_P (arg
))
10108 /* If ARG is an argument pack, we don't actually want to
10109 perform a substitution here, because substitutions
10110 for argument packs are only done
10111 element-by-element. We can get to this point when
10112 substituting the type of a non-type template
10113 parameter pack, when that type actually contains
10114 template parameter packs from an outer template, e.g.,
10116 template<typename... Types> struct A {
10117 template<Types... Values> struct B { };
10121 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
10124 gcc_assert (TYPE_P (arg
));
10126 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
10128 return cp_build_qualified_type_real
10129 (arg
, quals
, complain
| tf_ignore_bad_quals
);
10131 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
10133 /* We are processing a type constructed from a
10134 template template parameter. */
10135 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
10136 args
, complain
, in_decl
);
10137 if (argvec
== error_mark_node
)
10138 return error_mark_node
;
10140 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
10141 are resolving nested-types in the signature of a
10142 member function templates. Otherwise ARG is a
10143 TEMPLATE_DECL and is the real template to be
10145 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
10146 arg
= TYPE_NAME (arg
);
10148 r
= lookup_template_class (arg
,
10150 DECL_CONTEXT (arg
),
10151 /*entering_scope=*/0,
10153 return cp_build_qualified_type_real
10154 (r
, cp_type_quals (t
), complain
);
10157 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
10158 return unshare_expr (arg
);
10162 /* This can happen during the attempted tsubst'ing in
10163 unify. This means that we don't yet have any information
10164 about the template parameter in question. */
10167 /* If we get here, we must have been looking at a parm for a
10168 more deeply nested template. Make a new version of this
10169 template parameter, but with a lower level. */
10170 switch (TREE_CODE (t
))
10172 case TEMPLATE_TYPE_PARM
:
10173 case TEMPLATE_TEMPLATE_PARM
:
10174 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10175 if (cp_type_quals (t
))
10177 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
10178 r
= cp_build_qualified_type_real
10179 (r
, cp_type_quals (t
),
10180 complain
| (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
10181 ? tf_ignore_bad_quals
: 0));
10186 TEMPLATE_TYPE_PARM_INDEX (r
)
10187 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
10188 r
, levels
, args
, complain
);
10189 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
10190 TYPE_MAIN_VARIANT (r
) = r
;
10191 TYPE_POINTER_TO (r
) = NULL_TREE
;
10192 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
10194 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
10195 /* We have reduced the level of the template
10196 template parameter, but not the levels of its
10197 template parameters, so canonical_type_parameter
10198 will not be able to find the canonical template
10199 template parameter for this level. Thus, we
10200 require structural equality checking to compare
10201 TEMPLATE_TEMPLATE_PARMs. */
10202 SET_TYPE_STRUCTURAL_EQUALITY (r
);
10203 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
10204 SET_TYPE_STRUCTURAL_EQUALITY (r
);
10206 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
10208 if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
10210 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
10211 complain
, in_decl
);
10212 if (argvec
== error_mark_node
)
10213 return error_mark_node
;
10215 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
10216 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
10221 case TEMPLATE_PARM_INDEX
:
10222 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
10226 gcc_unreachable ();
10234 tree purpose
, value
, chain
;
10236 if (t
== void_list_node
)
10239 purpose
= TREE_PURPOSE (t
);
10242 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
10243 if (purpose
== error_mark_node
)
10244 return error_mark_node
;
10246 value
= TREE_VALUE (t
);
10249 value
= tsubst (value
, args
, complain
, in_decl
);
10250 if (value
== error_mark_node
)
10251 return error_mark_node
;
10253 chain
= TREE_CHAIN (t
);
10254 if (chain
&& chain
!= void_type_node
)
10256 chain
= tsubst (chain
, args
, complain
, in_decl
);
10257 if (chain
== error_mark_node
)
10258 return error_mark_node
;
10260 if (purpose
== TREE_PURPOSE (t
)
10261 && value
== TREE_VALUE (t
)
10262 && chain
== TREE_CHAIN (t
))
10264 return hash_tree_cons (purpose
, value
, chain
);
10268 /* We should never be tsubsting a binfo. */
10269 gcc_unreachable ();
10272 /* A vector of template arguments. */
10273 gcc_assert (!type
);
10274 return tsubst_template_args (t
, args
, complain
, in_decl
);
10277 case REFERENCE_TYPE
:
10279 enum tree_code code
;
10281 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
10284 code
= TREE_CODE (t
);
10289 Type deduction may fail for any of the following
10292 -- Attempting to create a pointer to reference type.
10293 -- Attempting to create a reference to a reference type or
10294 a reference to void.
10296 Core issue 106 says that creating a reference to a reference
10297 during instantiation is no longer a cause for failure. We
10298 only enforce this check in strict C++98 mode. */
10299 if ((TREE_CODE (type
) == REFERENCE_TYPE
10300 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
10301 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
10303 static location_t last_loc
;
10305 /* We keep track of the last time we issued this error
10306 message to avoid spewing a ton of messages during a
10307 single bad template instantiation. */
10308 if (complain
& tf_error
10309 && last_loc
!= input_location
)
10311 if (TREE_CODE (type
) == VOID_TYPE
)
10312 error ("forming reference to void");
10313 else if (code
== POINTER_TYPE
)
10314 error ("forming pointer to reference type %qT", type
);
10316 error ("forming reference to reference type %qT", type
);
10317 last_loc
= input_location
;
10320 return error_mark_node
;
10322 else if (code
== POINTER_TYPE
)
10324 r
= build_pointer_type (type
);
10325 if (TREE_CODE (type
) == METHOD_TYPE
)
10326 r
= build_ptrmemfunc_type (r
);
10328 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
10329 /* In C++0x, during template argument substitution, when there is an
10330 attempt to create a reference to a reference type, reference
10331 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
10333 "If a template-argument for a template-parameter T names a type
10334 that is a reference to a type A, an attempt to create the type
10335 'lvalue reference to cv T' creates the type 'lvalue reference to
10336 A,' while an attempt to create the type type rvalue reference to
10337 cv T' creates the type T"
10339 r
= cp_build_reference_type
10341 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
10343 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
10344 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
10346 if (r
!= error_mark_node
)
10347 /* Will this ever be needed for TYPE_..._TO values? */
10354 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
10355 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
10359 Type deduction may fail for any of the following
10362 -- Attempting to create "pointer to member of T" when T
10363 is not a class type. */
10364 if (complain
& tf_error
)
10365 error ("creating pointer to member of non-class type %qT", r
);
10366 return error_mark_node
;
10368 if (TREE_CODE (type
) == REFERENCE_TYPE
)
10370 if (complain
& tf_error
)
10371 error ("creating pointer to member reference type %qT", type
);
10372 return error_mark_node
;
10374 if (TREE_CODE (type
) == VOID_TYPE
)
10376 if (complain
& tf_error
)
10377 error ("creating pointer to member of type void");
10378 return error_mark_node
;
10380 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
10381 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10383 /* The type of the implicit object parameter gets its
10384 cv-qualifiers from the FUNCTION_TYPE. */
10386 tree method_type
= build_memfn_type (type
, r
, type_memfn_quals (type
));
10387 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
10388 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
10392 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
10396 case FUNCTION_TYPE
:
10401 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
10402 if (fntype
== error_mark_node
)
10403 return error_mark_node
;
10405 /* Substitute the exception specification. */
10406 specs
= tsubst_exception_specification (t
, args
, complain
,
10408 if (specs
== error_mark_node
)
10409 return error_mark_node
;
10411 fntype
= build_exception_variant (fntype
, specs
);
10416 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
10417 if (domain
== error_mark_node
)
10418 return error_mark_node
;
10420 /* As an optimization, we avoid regenerating the array type if
10421 it will obviously be the same as T. */
10422 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
10425 /* These checks should match the ones in grokdeclarator.
10429 The deduction may fail for any of the following reasons:
10431 -- Attempting to create an array with an element type that
10432 is void, a function type, or a reference type, or [DR337]
10433 an abstract class type. */
10434 if (TREE_CODE (type
) == VOID_TYPE
10435 || TREE_CODE (type
) == FUNCTION_TYPE
10436 || TREE_CODE (type
) == REFERENCE_TYPE
)
10438 if (complain
& tf_error
)
10439 error ("creating array of %qT", type
);
10440 return error_mark_node
;
10442 if (CLASS_TYPE_P (type
) && CLASSTYPE_PURE_VIRTUALS (type
))
10444 if (complain
& tf_error
)
10445 error ("creating array of %qT, which is an abstract class type",
10447 return error_mark_node
;
10450 r
= build_cplus_array_type (type
, domain
);
10452 if (TYPE_USER_ALIGN (t
))
10454 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
10455 TYPE_USER_ALIGN (r
) = 1;
10464 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
10465 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
10467 if (e1
== error_mark_node
|| e2
== error_mark_node
)
10468 return error_mark_node
;
10470 return fold_build2_loc (input_location
,
10471 TREE_CODE (t
), TREE_TYPE (t
), e1
, e2
);
10477 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
10478 if (e
== error_mark_node
)
10479 return error_mark_node
;
10481 return fold_build1_loc (input_location
, TREE_CODE (t
), TREE_TYPE (t
), e
);
10484 case TYPENAME_TYPE
:
10486 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
10487 in_decl
, /*entering_scope=*/1);
10488 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
10489 complain
, in_decl
);
10491 if (ctx
== error_mark_node
|| f
== error_mark_node
)
10492 return error_mark_node
;
10494 if (!MAYBE_CLASS_TYPE_P (ctx
))
10496 if (complain
& tf_error
)
10497 error ("%qT is not a class, struct, or union type", ctx
);
10498 return error_mark_node
;
10500 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
10502 /* Normally, make_typename_type does not require that the CTX
10503 have complete type in order to allow things like:
10505 template <class T> struct S { typename S<T>::X Y; };
10507 But, such constructs have already been resolved by this
10508 point, so here CTX really should have complete type, unless
10509 it's a partial instantiation. */
10510 if (!(complain
& tf_no_class_instantiations
))
10511 ctx
= complete_type (ctx
);
10512 if (!COMPLETE_TYPE_P (ctx
))
10514 if (complain
& tf_error
)
10515 cxx_incomplete_type_error (NULL_TREE
, ctx
);
10516 return error_mark_node
;
10520 f
= make_typename_type (ctx
, f
, typename_type
,
10521 (complain
& tf_error
) | tf_keep_type_decl
);
10522 if (f
== error_mark_node
)
10524 if (TREE_CODE (f
) == TYPE_DECL
)
10526 complain
|= tf_ignore_bad_quals
;
10530 if (TREE_CODE (f
) != TYPENAME_TYPE
)
10532 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
10533 error ("%qT resolves to %qT, which is not an enumeration type",
10535 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
10536 error ("%qT resolves to %qT, which is is not a class type",
10540 return cp_build_qualified_type_real
10541 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
10544 case UNBOUND_CLASS_TEMPLATE
:
10546 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
10547 in_decl
, /*entering_scope=*/1);
10548 tree name
= TYPE_IDENTIFIER (t
);
10549 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
10551 if (ctx
== error_mark_node
|| name
== error_mark_node
)
10552 return error_mark_node
;
10555 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
10556 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
10562 gcc_unreachable ();
10566 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
10567 tree e2
= tsubst_expr (TREE_OPERAND (t
, 1), args
, complain
, in_decl
,
10568 /*integral_constant_expression_p=*/false);
10569 if (e1
== error_mark_node
|| e2
== error_mark_node
)
10570 return error_mark_node
;
10572 return build_nt (ARRAY_REF
, e1
, e2
, NULL_TREE
, NULL_TREE
);
10577 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
10578 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
10579 if (e1
== error_mark_node
|| e2
== error_mark_node
)
10580 return error_mark_node
;
10582 return build_qualified_name (/*type=*/NULL_TREE
,
10583 e1
, e2
, QUALIFIED_NAME_IS_TEMPLATE (t
));
10590 ++cp_unevaluated_operand
;
10591 ++c_inhibit_evaluation_warnings
;
10593 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
10595 /*integral_constant_expression_p=*/false);
10597 --cp_unevaluated_operand
;
10598 --c_inhibit_evaluation_warnings
;
10600 type
= finish_typeof (type
);
10601 return cp_build_qualified_type_real (type
,
10603 | cp_type_quals (type
),
10607 case DECLTYPE_TYPE
:
10611 ++cp_unevaluated_operand
;
10612 ++c_inhibit_evaluation_warnings
;
10614 type
= tsubst_expr (DECLTYPE_TYPE_EXPR (t
), args
,
10616 /*integral_constant_expression_p=*/false);
10618 --cp_unevaluated_operand
;
10619 --c_inhibit_evaluation_warnings
;
10621 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
10622 type
= lambda_capture_field_type (type
);
10623 else if (DECLTYPE_FOR_LAMBDA_RETURN (t
))
10624 type
= lambda_return_type (type
);
10626 type
= finish_decltype_type
10627 (type
, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
));
10628 return cp_build_qualified_type_real (type
,
10630 | cp_type_quals (type
),
10634 case TYPE_ARGUMENT_PACK
:
10635 case NONTYPE_ARGUMENT_PACK
:
10637 tree r
= TYPE_P (t
)
10638 ? cxx_make_type (TREE_CODE (t
))
10639 : make_node (TREE_CODE (t
));
10641 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
10645 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
10647 /* For template nontype argument packs, also substitute into
10649 if (TREE_CODE (t
) == NONTYPE_ARGUMENT_PACK
)
10650 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10657 sorry ("use of %qs in template",
10658 tree_code_name
[(int) TREE_CODE (t
)]);
10659 return error_mark_node
;
10663 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
10664 type of the expression on the left-hand side of the "." or "->"
10668 tsubst_baselink (tree baselink
, tree object_type
,
10669 tree args
, tsubst_flags_t complain
, tree in_decl
)
10672 tree qualifying_scope
;
10675 tree template_args
= 0;
10676 bool template_id_p
= false;
10678 /* A baselink indicates a function from a base class. Both the
10679 BASELINK_ACCESS_BINFO and the base class referenced may
10680 indicate bases of the template class, rather than the
10681 instantiated class. In addition, lookups that were not
10682 ambiguous before may be ambiguous now. Therefore, we perform
10683 the lookup again. */
10684 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
10685 qualifying_scope
= tsubst (qualifying_scope
, args
,
10686 complain
, in_decl
);
10687 fns
= BASELINK_FUNCTIONS (baselink
);
10688 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
10689 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
10691 template_id_p
= true;
10692 template_args
= TREE_OPERAND (fns
, 1);
10693 fns
= TREE_OPERAND (fns
, 0);
10695 template_args
= tsubst_template_args (template_args
, args
,
10696 complain
, in_decl
);
10698 name
= DECL_NAME (get_first_fn (fns
));
10699 if (IDENTIFIER_TYPENAME_P (name
))
10700 name
= mangle_conv_op_name_for_type (optype
);
10701 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
10703 /* If lookup found a single function, mark it as used at this
10704 point. (If it lookup found multiple functions the one selected
10705 later by overload resolution will be marked as used at that
10707 if (BASELINK_P (baselink
))
10708 fns
= BASELINK_FUNCTIONS (baselink
);
10709 if (!template_id_p
&& !really_overloaded_fn (fns
))
10710 mark_used (OVL_CURRENT (fns
));
10712 /* Add back the template arguments, if present. */
10713 if (BASELINK_P (baselink
) && template_id_p
)
10714 BASELINK_FUNCTIONS (baselink
)
10715 = build_nt (TEMPLATE_ID_EXPR
,
10716 BASELINK_FUNCTIONS (baselink
),
10718 /* Update the conversion operator type. */
10719 BASELINK_OPTYPE (baselink
) = optype
;
10722 object_type
= current_class_type
;
10723 return adjust_result_of_qualified_name_lookup (baselink
,
10728 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
10729 true if the qualified-id will be a postfix-expression in-and-of
10730 itself; false if more of the postfix-expression follows the
10731 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
10735 tsubst_qualified_id (tree qualified_id
, tree args
,
10736 tsubst_flags_t complain
, tree in_decl
,
10737 bool done
, bool address_p
)
10743 tree template_args
;
10745 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
10747 /* Figure out what name to look up. */
10748 name
= TREE_OPERAND (qualified_id
, 1);
10749 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
10751 is_template
= true;
10752 template_args
= TREE_OPERAND (name
, 1);
10754 template_args
= tsubst_template_args (template_args
, args
,
10755 complain
, in_decl
);
10756 name
= TREE_OPERAND (name
, 0);
10760 is_template
= false;
10761 template_args
= NULL_TREE
;
10764 /* Substitute into the qualifying scope. When there are no ARGS, we
10765 are just trying to simplify a non-dependent expression. In that
10766 case the qualifying scope may be dependent, and, in any case,
10767 substituting will not help. */
10768 scope
= TREE_OPERAND (qualified_id
, 0);
10771 scope
= tsubst (scope
, args
, complain
, in_decl
);
10772 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
10777 if (dependent_scope_p (scope
))
10778 return build_qualified_name (NULL_TREE
, scope
, expr
,
10779 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
10781 if (!BASELINK_P (name
) && !DECL_P (expr
))
10783 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
10785 /* A BIT_NOT_EXPR is used to represent a destructor. */
10786 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
10788 error ("qualifying type %qT does not match destructor name ~%qT",
10789 scope
, TREE_OPERAND (expr
, 0));
10790 expr
= error_mark_node
;
10793 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
10794 /*is_type_p=*/0, false);
10797 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
10798 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
10799 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
10801 if (complain
& tf_error
)
10803 error ("dependent-name %qE is parsed as a non-type, but "
10804 "instantiation yields a type", qualified_id
);
10805 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
10807 return error_mark_node
;
10813 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
10815 /* Remember that there was a reference to this entity. */
10819 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
10821 if (complain
& tf_error
)
10822 qualified_name_lookup_error (scope
,
10823 TREE_OPERAND (qualified_id
, 1),
10824 expr
, input_location
);
10825 return error_mark_node
;
10829 expr
= lookup_template_function (expr
, template_args
);
10831 if (expr
== error_mark_node
&& complain
& tf_error
)
10832 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
10833 expr
, input_location
);
10834 else if (TYPE_P (scope
))
10836 expr
= (adjust_result_of_qualified_name_lookup
10837 (expr
, scope
, current_class_type
));
10838 expr
= (finish_qualified_id_expr
10839 (scope
, expr
, done
, address_p
,
10840 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
10841 /*template_arg_p=*/false));
10844 /* Expressions do not generally have reference type. */
10845 if (TREE_CODE (expr
) != SCOPE_REF
10846 /* However, if we're about to form a pointer-to-member, we just
10847 want the referenced member referenced. */
10848 && TREE_CODE (expr
) != OFFSET_REF
)
10849 expr
= convert_from_reference (expr
);
10854 /* Like tsubst, but deals with expressions. This function just replaces
10855 template parms; to finish processing the resultant expression, use
10859 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10861 enum tree_code code
;
10864 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
10867 code
= TREE_CODE (t
);
10872 r
= retrieve_local_specialization (t
);
10877 /* This can happen for a parameter name used later in a function
10878 declaration (such as in a late-specified return type). Just
10879 make a dummy decl, since it's only used for its type. */
10880 gcc_assert (cp_unevaluated_operand
!= 0);
10881 /* We copy T because want to tsubst the PARM_DECL only,
10882 not the following PARM_DECLs that are chained to T. */
10884 r
= tsubst_decl (c
, args
, complain
);
10885 /* Give it the template pattern as its context; its true context
10886 hasn't been instantiated yet and this is good enough for
10888 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
10891 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
10892 r
= ARGUMENT_PACK_SELECT_ARG (r
);
10901 if (DECL_TEMPLATE_PARM_P (t
))
10902 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
10903 /* There is no need to substitute into namespace-scope
10905 if (DECL_NAMESPACE_SCOPE_P (t
))
10907 /* If ARGS is NULL, then T is known to be non-dependent. */
10908 if (args
== NULL_TREE
)
10909 return integral_constant_value (t
);
10911 /* Unfortunately, we cannot just call lookup_name here.
10914 template <int I> int f() {
10916 struct S { void g() { E e = a; } };
10919 When we instantiate f<7>::S::g(), say, lookup_name is not
10920 clever enough to find f<7>::a. */
10922 = tsubst_aggr_type (TREE_TYPE (t
), args
, complain
, in_decl
,
10923 /*entering_scope=*/0);
10925 for (v
= TYPE_VALUES (enum_type
);
10927 v
= TREE_CHAIN (v
))
10928 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
10929 return TREE_VALUE (v
);
10931 /* We didn't find the name. That should never happen; if
10932 name-lookup found it during preliminary parsing, we
10933 should find it again here during instantiation. */
10934 gcc_unreachable ();
10939 if (DECL_CONTEXT (t
))
10943 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
10944 /*entering_scope=*/1);
10945 if (ctx
!= DECL_CONTEXT (t
))
10947 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
10950 if (complain
& tf_error
)
10951 error ("using invalid field %qD", t
);
10952 return error_mark_node
;
10961 case FUNCTION_DECL
:
10962 if ((DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
10963 || local_variable_p (t
))
10964 t
= tsubst (t
, args
, complain
, in_decl
);
10969 return tsubst_baselink (t
, current_class_type
, args
, complain
, in_decl
);
10971 case TEMPLATE_DECL
:
10972 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10973 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
10974 args
, complain
, in_decl
);
10975 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
10976 return tsubst (t
, args
, complain
, in_decl
);
10977 else if (DECL_CLASS_SCOPE_P (t
)
10978 && uses_template_parms (DECL_CONTEXT (t
)))
10980 /* Template template argument like the following example need
10983 template <template <class> class TT> struct C {};
10984 template <class T> struct D {
10985 template <class U> struct E {};
10990 We are processing the template argument `E' in #1 for
10991 the template instantiation #2. Originally, `E' is a
10992 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
10993 have to substitute this with one having context `D<int>'. */
10995 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
10996 return lookup_field (context
, DECL_NAME(t
), 0, false);
10999 /* Ordinary template template argument. */
11003 case REINTERPRET_CAST_EXPR
:
11004 case CONST_CAST_EXPR
:
11005 case STATIC_CAST_EXPR
:
11006 case DYNAMIC_CAST_EXPR
:
11009 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
11010 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
11013 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
11015 /* We only want to compute the number of arguments. */
11016 tree expanded
= tsubst_pack_expansion (TREE_OPERAND (t
, 0), args
,
11017 complain
, in_decl
);
11020 if (TREE_CODE (expanded
) == TREE_VEC
)
11021 len
= TREE_VEC_LENGTH (expanded
);
11023 if (expanded
== error_mark_node
)
11024 return error_mark_node
;
11025 else if (PACK_EXPANSION_P (expanded
)
11026 || (TREE_CODE (expanded
) == TREE_VEC
11028 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
11030 if (TREE_CODE (expanded
) == TREE_VEC
)
11031 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
11033 if (TYPE_P (expanded
))
11034 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
11035 complain
& tf_error
);
11037 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
11038 complain
& tf_error
);
11041 return build_int_cst (size_type_node
, len
);
11047 case TRUTH_NOT_EXPR
:
11050 case UNARY_PLUS_EXPR
: /* Unary + */
11055 case REALPART_EXPR
:
11056 case IMAGPART_EXPR
:
11058 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
11059 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
11061 case COMPONENT_REF
:
11066 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
11067 name
= TREE_OPERAND (t
, 1);
11068 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
11070 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
11071 complain
, in_decl
);
11072 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
11074 else if (TREE_CODE (name
) == SCOPE_REF
11075 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
11077 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
11078 complain
, in_decl
);
11079 name
= TREE_OPERAND (name
, 1);
11080 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
11081 complain
, in_decl
);
11082 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
11083 name
= build_qualified_name (/*type=*/NULL_TREE
,
11085 /*template_p=*/false);
11087 else if (TREE_CODE (name
) == BASELINK
)
11088 name
= tsubst_baselink (name
,
11089 non_reference (TREE_TYPE (object
)),
11093 name
= tsubst_copy (name
, args
, complain
, in_decl
);
11094 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
11100 case TRUNC_DIV_EXPR
:
11101 case CEIL_DIV_EXPR
:
11102 case FLOOR_DIV_EXPR
:
11103 case ROUND_DIV_EXPR
:
11104 case EXACT_DIV_EXPR
:
11108 case TRUNC_MOD_EXPR
:
11109 case FLOOR_MOD_EXPR
:
11110 case TRUTH_ANDIF_EXPR
:
11111 case TRUTH_ORIF_EXPR
:
11112 case TRUTH_AND_EXPR
:
11113 case TRUTH_OR_EXPR
:
11126 case COMPOUND_EXPR
:
11129 case PREDECREMENT_EXPR
:
11130 case PREINCREMENT_EXPR
:
11131 case POSTDECREMENT_EXPR
:
11132 case POSTINCREMENT_EXPR
:
11134 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11135 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
11138 return build_qualified_name (/*type=*/NULL_TREE
,
11139 tsubst_copy (TREE_OPERAND (t
, 0),
11140 args
, complain
, in_decl
),
11141 tsubst_copy (TREE_OPERAND (t
, 1),
11142 args
, complain
, in_decl
),
11143 QUALIFIED_NAME_IS_TEMPLATE (t
));
11148 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11149 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
11150 NULL_TREE
, NULL_TREE
);
11154 int n
= VL_EXP_OPERAND_LENGTH (t
);
11155 tree result
= build_vl_exp (CALL_EXPR
, n
);
11157 for (i
= 0; i
< n
; i
++)
11158 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
11159 complain
, in_decl
);
11165 case PSEUDO_DTOR_EXPR
:
11168 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11169 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
11170 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
11171 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
11178 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11179 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
11180 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
11181 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
11188 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11189 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
11190 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
11191 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
11195 case TEMPLATE_ID_EXPR
:
11197 /* Substituted template arguments */
11198 tree fn
= TREE_OPERAND (t
, 0);
11199 tree targs
= TREE_OPERAND (t
, 1);
11201 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
11203 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
11205 return lookup_template_function (fn
, targs
);
11210 tree purpose
, value
, chain
;
11212 if (t
== void_list_node
)
11215 purpose
= TREE_PURPOSE (t
);
11217 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
11218 value
= TREE_VALUE (t
);
11220 value
= tsubst_copy (value
, args
, complain
, in_decl
);
11221 chain
= TREE_CHAIN (t
);
11222 if (chain
&& chain
!= void_type_node
)
11223 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
11224 if (purpose
== TREE_PURPOSE (t
)
11225 && value
== TREE_VALUE (t
)
11226 && chain
== TREE_CHAIN (t
))
11228 return tree_cons (purpose
, value
, chain
);
11233 case ENUMERAL_TYPE
:
11235 case TEMPLATE_TYPE_PARM
:
11236 case TEMPLATE_TEMPLATE_PARM
:
11237 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11238 case TEMPLATE_PARM_INDEX
:
11240 case REFERENCE_TYPE
:
11242 case FUNCTION_TYPE
:
11245 case TYPENAME_TYPE
:
11246 case UNBOUND_CLASS_TEMPLATE
:
11248 case DECLTYPE_TYPE
:
11250 return tsubst (t
, args
, complain
, in_decl
);
11252 case IDENTIFIER_NODE
:
11253 if (IDENTIFIER_TYPENAME_P (t
))
11255 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11256 return mangle_conv_op_name_for_type (new_type
);
11262 /* This is handled by tsubst_copy_and_build. */
11263 gcc_unreachable ();
11266 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
11268 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
11270 case CLEANUP_POINT_EXPR
:
11271 /* We shouldn't have built any of these during initial template
11272 generation. Instead, they should be built during instantiation
11273 in response to the saved STMT_IS_FULL_EXPR_P setting. */
11274 gcc_unreachable ();
11277 mark_used (TREE_OPERAND (t
, 1));
11280 case EXPR_PACK_EXPANSION
:
11281 error ("invalid use of pack expansion expression");
11282 return error_mark_node
;
11284 case NONTYPE_ARGUMENT_PACK
:
11285 error ("use %<...%> to expand argument pack");
11286 return error_mark_node
;
11293 /* Like tsubst_copy, but specifically for OpenMP clauses. */
11296 tsubst_omp_clauses (tree clauses
, tree args
, tsubst_flags_t complain
,
11299 tree new_clauses
= NULL
, nc
, oc
;
11301 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
11303 nc
= copy_node (oc
);
11304 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
11307 switch (OMP_CLAUSE_CODE (nc
))
11309 case OMP_CLAUSE_LASTPRIVATE
:
11310 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
11312 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
11313 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
11314 in_decl
, /*integral_constant_expression_p=*/false);
11315 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
11316 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
11319 case OMP_CLAUSE_PRIVATE
:
11320 case OMP_CLAUSE_SHARED
:
11321 case OMP_CLAUSE_FIRSTPRIVATE
:
11322 case OMP_CLAUSE_REDUCTION
:
11323 case OMP_CLAUSE_COPYIN
:
11324 case OMP_CLAUSE_COPYPRIVATE
:
11325 case OMP_CLAUSE_IF
:
11326 case OMP_CLAUSE_NUM_THREADS
:
11327 case OMP_CLAUSE_SCHEDULE
:
11328 case OMP_CLAUSE_COLLAPSE
:
11329 OMP_CLAUSE_OPERAND (nc
, 0)
11330 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
11331 in_decl
, /*integral_constant_expression_p=*/false);
11333 case OMP_CLAUSE_NOWAIT
:
11334 case OMP_CLAUSE_ORDERED
:
11335 case OMP_CLAUSE_DEFAULT
:
11336 case OMP_CLAUSE_UNTIED
:
11339 gcc_unreachable ();
11343 return finish_omp_clauses (nreverse (new_clauses
));
11346 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
11349 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
11352 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
11354 tree purpose
, value
, chain
;
11359 if (TREE_CODE (t
) != TREE_LIST
)
11360 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
11361 /*function_p=*/false,
11362 /*integral_constant_expression_p=*/false);
11364 if (t
== void_list_node
)
11367 purpose
= TREE_PURPOSE (t
);
11369 purpose
= RECUR (purpose
);
11370 value
= TREE_VALUE (t
);
11371 if (value
&& TREE_CODE (value
) != LABEL_DECL
)
11372 value
= RECUR (value
);
11373 chain
= TREE_CHAIN (t
);
11374 if (chain
&& chain
!= void_type_node
)
11375 chain
= RECUR (chain
);
11376 return tree_cons (purpose
, value
, chain
);
11380 /* Substitute one OMP_FOR iterator. */
11383 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
11384 tree condv
, tree incrv
, tree
*clauses
,
11385 tree args
, tsubst_flags_t complain
, tree in_decl
,
11386 bool integral_constant_expression_p
)
11388 #define RECUR(NODE) \
11389 tsubst_expr ((NODE), args, complain, in_decl, \
11390 integral_constant_expression_p)
11391 tree decl
, init
, cond
, incr
, auto_node
;
11393 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
11394 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
11395 decl
= RECUR (TREE_OPERAND (init
, 0));
11396 init
= TREE_OPERAND (init
, 1);
11397 auto_node
= type_uses_auto (TREE_TYPE (decl
));
11398 if (auto_node
&& init
)
11400 tree init_expr
= init
;
11401 if (TREE_CODE (init_expr
) == DECL_EXPR
)
11402 init_expr
= DECL_INITIAL (DECL_EXPR_DECL (init_expr
));
11403 init_expr
= RECUR (init_expr
);
11405 = do_auto_deduction (TREE_TYPE (decl
), init_expr
, auto_node
);
11407 gcc_assert (!type_dependent_expression_p (decl
));
11409 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
11411 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
11412 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
11413 if (TREE_CODE (incr
) == MODIFY_EXPR
)
11414 incr
= build_x_modify_expr (RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
11415 RECUR (TREE_OPERAND (incr
, 1)),
11418 incr
= RECUR (incr
);
11419 TREE_VEC_ELT (declv
, i
) = decl
;
11420 TREE_VEC_ELT (initv
, i
) = init
;
11421 TREE_VEC_ELT (condv
, i
) = cond
;
11422 TREE_VEC_ELT (incrv
, i
) = incr
;
11426 if (init
&& TREE_CODE (init
) != DECL_EXPR
)
11429 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
11431 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
11432 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
11433 && OMP_CLAUSE_DECL (c
) == decl
)
11435 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
11436 && OMP_CLAUSE_DECL (c
) == decl
)
11437 error ("iteration variable %qD should not be firstprivate", decl
);
11438 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
11439 && OMP_CLAUSE_DECL (c
) == decl
)
11440 error ("iteration variable %qD should not be reduction", decl
);
11444 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
11445 OMP_CLAUSE_DECL (c
) = decl
;
11446 c
= finish_omp_clauses (c
);
11449 OMP_CLAUSE_CHAIN (c
) = *clauses
;
11454 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
11455 if (COMPARISON_CLASS_P (cond
))
11456 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
11457 RECUR (TREE_OPERAND (cond
, 0)),
11458 RECUR (TREE_OPERAND (cond
, 1)));
11460 cond
= RECUR (cond
);
11461 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
11462 switch (TREE_CODE (incr
))
11464 case PREINCREMENT_EXPR
:
11465 case PREDECREMENT_EXPR
:
11466 case POSTINCREMENT_EXPR
:
11467 case POSTDECREMENT_EXPR
:
11468 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
11469 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
11472 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
11473 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
11475 tree rhs
= TREE_OPERAND (incr
, 1);
11476 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
11477 RECUR (TREE_OPERAND (incr
, 0)),
11478 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
11479 RECUR (TREE_OPERAND (rhs
, 0)),
11480 RECUR (TREE_OPERAND (rhs
, 1))));
11483 incr
= RECUR (incr
);
11486 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
11487 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
11489 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
11490 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
11491 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
11492 TREE_TYPE (decl
), lhs
,
11493 RECUR (TREE_OPERAND (incr
, 2))));
11495 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
11496 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
11497 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
11499 tree rhs
= TREE_OPERAND (incr
, 2);
11500 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
11501 RECUR (TREE_OPERAND (incr
, 0)),
11502 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
11503 RECUR (TREE_OPERAND (rhs
, 0)),
11504 RECUR (TREE_OPERAND (rhs
, 1))));
11507 incr
= RECUR (incr
);
11510 incr
= RECUR (incr
);
11514 TREE_VEC_ELT (declv
, i
) = decl
;
11515 TREE_VEC_ELT (initv
, i
) = init
;
11516 TREE_VEC_ELT (condv
, i
) = cond
;
11517 TREE_VEC_ELT (incrv
, i
) = incr
;
11521 /* Like tsubst_copy for expressions, etc. but also does semantic
11525 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
11526 bool integral_constant_expression_p
)
11528 #define RECUR(NODE) \
11529 tsubst_expr ((NODE), args, complain, in_decl, \
11530 integral_constant_expression_p)
11534 if (t
== NULL_TREE
|| t
== error_mark_node
)
11537 if (EXPR_HAS_LOCATION (t
))
11538 input_location
= EXPR_LOCATION (t
);
11539 if (STATEMENT_CODE_P (TREE_CODE (t
)))
11540 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
11542 switch (TREE_CODE (t
))
11544 case STATEMENT_LIST
:
11546 tree_stmt_iterator i
;
11547 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
11548 RECUR (tsi_stmt (i
));
11552 case CTOR_INITIALIZER
:
11553 finish_mem_initializers (tsubst_initializer_list
11554 (TREE_OPERAND (t
, 0), args
));
11558 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
11562 tmp
= RECUR (EXPR_STMT_EXPR (t
));
11563 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
11564 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
11566 finish_expr_stmt (tmp
);
11570 do_using_directive (RECUR (USING_STMT_NAMESPACE (t
)));
11578 decl
= DECL_EXPR_DECL (t
);
11579 if (TREE_CODE (decl
) == LABEL_DECL
)
11580 finish_label_decl (DECL_NAME (decl
));
11581 else if (TREE_CODE (decl
) == USING_DECL
)
11583 tree scope
= USING_DECL_SCOPE (decl
);
11584 tree name
= DECL_NAME (decl
);
11587 scope
= RECUR (scope
);
11588 decl
= lookup_qualified_name (scope
, name
,
11589 /*is_type_p=*/false,
11590 /*complain=*/false);
11591 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
11592 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
11594 do_local_using_decl (decl
, scope
, name
);
11598 init
= DECL_INITIAL (decl
);
11599 decl
= tsubst (decl
, args
, complain
, in_decl
);
11600 if (decl
!= error_mark_node
)
11602 /* By marking the declaration as instantiated, we avoid
11603 trying to instantiate it. Since instantiate_decl can't
11604 handle local variables, and since we've already done
11605 all that needs to be done, that's the right thing to
11607 if (TREE_CODE (decl
) == VAR_DECL
)
11608 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
11609 if (TREE_CODE (decl
) == VAR_DECL
11610 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
11611 /* Anonymous aggregates are a special case. */
11612 finish_anon_union (decl
);
11615 maybe_push_decl (decl
);
11616 if (TREE_CODE (decl
) == VAR_DECL
11617 && DECL_PRETTY_FUNCTION_P (decl
))
11619 /* For __PRETTY_FUNCTION__ we have to adjust the
11621 const char *const name
11622 = cxx_printable_name (current_function_decl
, 2);
11623 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
11627 tree t
= RECUR (init
);
11630 /* If we had an initializer but it
11631 instantiated to nothing,
11632 value-initialize the object. This will
11633 only occur when the initializer was a
11634 pack expansion where the parameter packs
11635 used in that expansion were of length
11637 init
= build_value_init (TREE_TYPE (decl
));
11642 cp_finish_decl (decl
, init
, false, NULL_TREE
, 0);
11647 /* A DECL_EXPR can also be used as an expression, in the condition
11648 clause of an if/for/while construct. */
11653 stmt
= begin_for_stmt ();
11654 RECUR (FOR_INIT_STMT (t
));
11655 finish_for_init_stmt (stmt
);
11656 tmp
= RECUR (FOR_COND (t
));
11657 finish_for_cond (tmp
, stmt
);
11658 tmp
= RECUR (FOR_EXPR (t
));
11659 finish_for_expr (tmp
, stmt
);
11660 RECUR (FOR_BODY (t
));
11661 finish_for_stmt (stmt
);
11665 stmt
= begin_while_stmt ();
11666 tmp
= RECUR (WHILE_COND (t
));
11667 finish_while_stmt_cond (tmp
, stmt
);
11668 RECUR (WHILE_BODY (t
));
11669 finish_while_stmt (stmt
);
11673 stmt
= begin_do_stmt ();
11674 RECUR (DO_BODY (t
));
11675 finish_do_body (stmt
);
11676 tmp
= RECUR (DO_COND (t
));
11677 finish_do_stmt (tmp
, stmt
);
11681 stmt
= begin_if_stmt ();
11682 tmp
= RECUR (IF_COND (t
));
11683 finish_if_stmt_cond (tmp
, stmt
);
11684 RECUR (THEN_CLAUSE (t
));
11685 finish_then_clause (stmt
);
11687 if (ELSE_CLAUSE (t
))
11689 begin_else_clause (stmt
);
11690 RECUR (ELSE_CLAUSE (t
));
11691 finish_else_clause (stmt
);
11694 finish_if_stmt (stmt
);
11698 if (BIND_EXPR_BODY_BLOCK (t
))
11699 stmt
= begin_function_body ();
11701 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
11702 ? BCS_TRY_BLOCK
: 0);
11704 RECUR (BIND_EXPR_BODY (t
));
11706 if (BIND_EXPR_BODY_BLOCK (t
))
11707 finish_function_body (stmt
);
11709 finish_compound_stmt (stmt
);
11713 finish_break_stmt ();
11716 case CONTINUE_STMT
:
11717 finish_continue_stmt ();
11721 stmt
= begin_switch_stmt ();
11722 tmp
= RECUR (SWITCH_STMT_COND (t
));
11723 finish_switch_cond (tmp
, stmt
);
11724 RECUR (SWITCH_STMT_BODY (t
));
11725 finish_switch_stmt (stmt
);
11728 case CASE_LABEL_EXPR
:
11729 finish_case_label (EXPR_LOCATION (t
),
11730 RECUR (CASE_LOW (t
)),
11731 RECUR (CASE_HIGH (t
)));
11736 tree decl
= LABEL_EXPR_LABEL (t
);
11739 label
= finish_label_stmt (DECL_NAME (decl
));
11740 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
11741 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
11746 tmp
= GOTO_DESTINATION (t
);
11747 if (TREE_CODE (tmp
) != LABEL_DECL
)
11748 /* Computed goto's must be tsubst'd into. On the other hand,
11749 non-computed gotos must not be; the identifier in question
11750 will have no binding. */
11753 tmp
= DECL_NAME (tmp
);
11754 finish_goto_stmt (tmp
);
11758 tmp
= finish_asm_stmt
11759 (ASM_VOLATILE_P (t
),
11760 RECUR (ASM_STRING (t
)),
11761 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
11762 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
11763 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
11764 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
11766 tree asm_expr
= tmp
;
11767 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
11768 asm_expr
= TREE_OPERAND (asm_expr
, 0);
11769 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
11776 stmt
= begin_try_block ();
11777 RECUR (TRY_STMTS (t
));
11778 finish_cleanup_try_block (stmt
);
11779 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
11783 tree compound_stmt
= NULL_TREE
;
11785 if (FN_TRY_BLOCK_P (t
))
11786 stmt
= begin_function_try_block (&compound_stmt
);
11788 stmt
= begin_try_block ();
11790 RECUR (TRY_STMTS (t
));
11792 if (FN_TRY_BLOCK_P (t
))
11793 finish_function_try_block (stmt
);
11795 finish_try_block (stmt
);
11797 RECUR (TRY_HANDLERS (t
));
11798 if (FN_TRY_BLOCK_P (t
))
11799 finish_function_handler_sequence (stmt
, compound_stmt
);
11801 finish_handler_sequence (stmt
);
11807 tree decl
= HANDLER_PARMS (t
);
11811 decl
= tsubst (decl
, args
, complain
, in_decl
);
11812 /* Prevent instantiate_decl from trying to instantiate
11813 this variable. We've already done all that needs to be
11815 if (decl
!= error_mark_node
)
11816 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
11818 stmt
= begin_handler ();
11819 finish_handler_parms (decl
, stmt
);
11820 RECUR (HANDLER_BODY (t
));
11821 finish_handler (stmt
);
11826 tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
11829 case STATIC_ASSERT
:
11832 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
11835 /*integral_constant_expression_p=*/true);
11836 finish_static_assert (condition
,
11837 STATIC_ASSERT_MESSAGE (t
),
11838 STATIC_ASSERT_SOURCE_LOCATION (t
),
11839 /*member_p=*/false);
11844 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
),
11845 args
, complain
, in_decl
);
11846 stmt
= begin_omp_parallel ();
11847 RECUR (OMP_PARALLEL_BODY (t
));
11848 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
11849 = OMP_PARALLEL_COMBINED (t
);
11853 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
),
11854 args
, complain
, in_decl
);
11855 stmt
= begin_omp_task ();
11856 RECUR (OMP_TASK_BODY (t
));
11857 finish_omp_task (tmp
, stmt
);
11862 tree clauses
, body
, pre_body
;
11863 tree declv
, initv
, condv
, incrv
;
11866 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
),
11867 args
, complain
, in_decl
);
11868 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
11869 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
11870 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
11871 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
11873 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
11874 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
11875 &clauses
, args
, complain
, in_decl
,
11876 integral_constant_expression_p
);
11878 stmt
= begin_omp_structured_block ();
11880 for (i
= 0; i
< TREE_VEC_LENGTH (initv
); i
++)
11881 if (TREE_VEC_ELT (initv
, i
) == NULL
11882 || TREE_CODE (TREE_VEC_ELT (initv
, i
)) != DECL_EXPR
)
11883 TREE_VEC_ELT (initv
, i
) = RECUR (TREE_VEC_ELT (initv
, i
));
11884 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv
, i
))))
11886 tree init
= RECUR (TREE_VEC_ELT (initv
, i
));
11887 gcc_assert (init
== TREE_VEC_ELT (declv
, i
));
11888 TREE_VEC_ELT (initv
, i
) = NULL_TREE
;
11892 tree decl_expr
= TREE_VEC_ELT (initv
, i
);
11893 tree init
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
11894 gcc_assert (init
!= NULL
);
11895 TREE_VEC_ELT (initv
, i
) = RECUR (init
);
11896 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL
;
11898 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init
;
11901 pre_body
= push_stmt_list ();
11902 RECUR (OMP_FOR_PRE_BODY (t
));
11903 pre_body
= pop_stmt_list (pre_body
);
11905 body
= push_stmt_list ();
11906 RECUR (OMP_FOR_BODY (t
));
11907 body
= pop_stmt_list (body
);
11909 t
= finish_omp_for (EXPR_LOCATION (t
), declv
, initv
, condv
, incrv
,
11910 body
, pre_body
, clauses
);
11912 add_stmt (finish_omp_structured_block (stmt
));
11918 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), args
, complain
, in_decl
);
11919 stmt
= push_stmt_list ();
11920 RECUR (OMP_BODY (t
));
11921 stmt
= pop_stmt_list (stmt
);
11924 OMP_BODY (t
) = stmt
;
11925 OMP_CLAUSES (t
) = tmp
;
11933 stmt
= push_stmt_list ();
11934 RECUR (OMP_BODY (t
));
11935 stmt
= pop_stmt_list (stmt
);
11938 OMP_BODY (t
) = stmt
;
11943 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
11945 tree op1
= TREE_OPERAND (t
, 1);
11946 tree lhs
= RECUR (TREE_OPERAND (op1
, 0));
11947 tree rhs
= RECUR (TREE_OPERAND (op1
, 1));
11948 finish_omp_atomic (TREE_CODE (op1
), lhs
, rhs
);
11952 case EXPR_PACK_EXPANSION
:
11953 error ("invalid use of pack expansion expression");
11954 return error_mark_node
;
11956 case NONTYPE_ARGUMENT_PACK
:
11957 error ("use %<...%> to expand argument pack");
11958 return error_mark_node
;
11961 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
11963 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
11964 /*function_p=*/false,
11965 integral_constant_expression_p
);
11972 /* T is a postfix-expression that is not being used in a function
11973 call. Return the substituted version of T. */
11976 tsubst_non_call_postfix_expression (tree t
, tree args
,
11977 tsubst_flags_t complain
,
11980 if (TREE_CODE (t
) == SCOPE_REF
)
11981 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
11982 /*done=*/false, /*address_p=*/false);
11984 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
11985 /*function_p=*/false,
11986 /*integral_constant_expression_p=*/false);
11991 /* Like tsubst but deals with expressions and performs semantic
11992 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
11995 tsubst_copy_and_build (tree t
,
11997 tsubst_flags_t complain
,
12000 bool integral_constant_expression_p
)
12002 #define RECUR(NODE) \
12003 tsubst_copy_and_build (NODE, args, complain, in_decl, \
12004 /*function_p=*/false, \
12005 integral_constant_expression_p)
12009 if (t
== NULL_TREE
|| t
== error_mark_node
)
12012 switch (TREE_CODE (t
))
12016 /* Fall through. */
12017 case IDENTIFIER_NODE
:
12021 bool non_integral_constant_expression_p
;
12022 const char *error_msg
;
12024 if (IDENTIFIER_TYPENAME_P (t
))
12026 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12027 t
= mangle_conv_op_name_for_type (new_type
);
12030 /* Look up the name. */
12031 decl
= lookup_name (t
);
12033 /* By convention, expressions use ERROR_MARK_NODE to indicate
12034 failure, not NULL_TREE. */
12035 if (decl
== NULL_TREE
)
12036 decl
= error_mark_node
;
12038 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
12040 integral_constant_expression_p
,
12041 /*allow_non_integral_constant_expression_p=*/false,
12042 &non_integral_constant_expression_p
,
12043 /*template_p=*/false,
12045 /*address_p=*/false,
12046 /*template_arg_p=*/false,
12051 if (!function_p
&& TREE_CODE (decl
) == IDENTIFIER_NODE
)
12052 decl
= unqualified_name_lookup_error (decl
);
12056 case TEMPLATE_ID_EXPR
:
12059 tree templ
= RECUR (TREE_OPERAND (t
, 0));
12060 tree targs
= TREE_OPERAND (t
, 1);
12063 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12065 if (TREE_CODE (templ
) == COMPONENT_REF
)
12067 object
= TREE_OPERAND (templ
, 0);
12068 templ
= TREE_OPERAND (templ
, 1);
12071 object
= NULL_TREE
;
12072 templ
= lookup_template_function (templ
, targs
);
12075 return build3 (COMPONENT_REF
, TREE_TYPE (templ
),
12076 object
, templ
, NULL_TREE
);
12078 return baselink_for_fns (templ
);
12083 tree r
= RECUR (TREE_OPERAND (t
, 0));
12085 if (REFERENCE_REF_P (t
))
12087 /* A type conversion to reference type will be enclosed in
12088 such an indirect ref, but the substitution of the cast
12089 will have also added such an indirect ref. */
12090 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
12091 r
= convert_from_reference (r
);
12094 r
= build_x_indirect_ref (r
, RO_UNARY_STAR
, complain
);
12100 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12101 RECUR (TREE_OPERAND (t
, 0)));
12104 case REINTERPRET_CAST_EXPR
:
12105 case CONST_CAST_EXPR
:
12106 case DYNAMIC_CAST_EXPR
:
12107 case STATIC_CAST_EXPR
:
12112 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12113 if (integral_constant_expression_p
12114 && !cast_valid_in_integral_constant_expression_p (type
))
12116 if (complain
& tf_error
)
12117 error ("a cast to a type other than an integral or "
12118 "enumeration type cannot appear in a constant-expression");
12119 return error_mark_node
;
12122 op
= RECUR (TREE_OPERAND (t
, 0));
12124 switch (TREE_CODE (t
))
12127 return build_functional_cast (type
, op
, complain
);
12128 case REINTERPRET_CAST_EXPR
:
12129 return build_reinterpret_cast (type
, op
, complain
);
12130 case CONST_CAST_EXPR
:
12131 return build_const_cast (type
, op
, complain
);
12132 case DYNAMIC_CAST_EXPR
:
12133 return build_dynamic_cast (type
, op
, complain
);
12134 case STATIC_CAST_EXPR
:
12135 return build_static_cast (type
, op
, complain
);
12137 gcc_unreachable ();
12141 case POSTDECREMENT_EXPR
:
12142 case POSTINCREMENT_EXPR
:
12143 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
12144 args
, complain
, in_decl
);
12145 return build_x_unary_op (TREE_CODE (t
), op1
, complain
);
12147 case PREDECREMENT_EXPR
:
12148 case PREINCREMENT_EXPR
:
12152 case TRUTH_NOT_EXPR
:
12153 case UNARY_PLUS_EXPR
: /* Unary + */
12154 case REALPART_EXPR
:
12155 case IMAGPART_EXPR
:
12156 return build_x_unary_op (TREE_CODE (t
), RECUR (TREE_OPERAND (t
, 0)),
12160 op1
= TREE_OPERAND (t
, 0);
12161 if (TREE_CODE (op1
) == SCOPE_REF
)
12162 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
12163 /*done=*/true, /*address_p=*/true);
12165 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
12167 if (TREE_CODE (op1
) == LABEL_DECL
)
12168 return finish_label_address_expr (DECL_NAME (op1
),
12169 EXPR_LOCATION (op1
));
12170 return build_x_unary_op (ADDR_EXPR
, op1
, complain
);
12175 case TRUNC_DIV_EXPR
:
12176 case CEIL_DIV_EXPR
:
12177 case FLOOR_DIV_EXPR
:
12178 case ROUND_DIV_EXPR
:
12179 case EXACT_DIV_EXPR
:
12183 case TRUNC_MOD_EXPR
:
12184 case FLOOR_MOD_EXPR
:
12185 case TRUTH_ANDIF_EXPR
:
12186 case TRUTH_ORIF_EXPR
:
12187 case TRUTH_AND_EXPR
:
12188 case TRUTH_OR_EXPR
:
12203 return build_x_binary_op
12205 RECUR (TREE_OPERAND (t
, 0)),
12206 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
12208 : TREE_CODE (TREE_OPERAND (t
, 0))),
12209 RECUR (TREE_OPERAND (t
, 1)),
12210 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
12212 : TREE_CODE (TREE_OPERAND (t
, 1))),
12213 /*overloaded_p=*/NULL
,
12217 return tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
12218 /*address_p=*/false);
12220 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
12221 args
, complain
, in_decl
);
12222 return build_x_array_ref (op1
, RECUR (TREE_OPERAND (t
, 1)), complain
);
12225 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
12226 return tsubst_copy (t
, args
, complain
, in_decl
);
12230 op1
= TREE_OPERAND (t
, 0);
12233 /* When there are no ARGS, we are trying to evaluate a
12234 non-dependent expression from the parser. Trying to do
12235 the substitutions may not work. */
12237 op1
= TREE_TYPE (op1
);
12241 ++cp_unevaluated_operand
;
12242 ++c_inhibit_evaluation_warnings
;
12243 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
12244 /*function_p=*/false,
12245 /*integral_constant_expression_p=*/false);
12246 --cp_unevaluated_operand
;
12247 --c_inhibit_evaluation_warnings
;
12250 return cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
12251 complain
& tf_error
);
12253 return cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
12254 complain
& tf_error
);
12256 case NOEXCEPT_EXPR
:
12257 op1
= TREE_OPERAND (t
, 0);
12258 ++cp_unevaluated_operand
;
12259 ++c_inhibit_evaluation_warnings
;
12260 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
12261 /*function_p=*/false,
12262 /*integral_constant_expression_p=*/false);
12263 --cp_unevaluated_operand
;
12264 --c_inhibit_evaluation_warnings
;
12265 return finish_noexcept_expr (op1
);
12269 tree r
= build_x_modify_expr
12270 (RECUR (TREE_OPERAND (t
, 0)),
12271 TREE_CODE (TREE_OPERAND (t
, 1)),
12272 RECUR (TREE_OPERAND (t
, 2)),
12274 /* TREE_NO_WARNING must be set if either the expression was
12275 parenthesized or it uses an operator such as >>= rather
12276 than plain assignment. In the former case, it was already
12277 set and must be copied. In the latter case,
12278 build_x_modify_expr sets it and it must not be reset
12280 if (TREE_NO_WARNING (t
))
12281 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12286 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
12287 args
, complain
, in_decl
);
12288 /* Remember that there was a reference to this entity. */
12291 return build_x_arrow (op1
);
12295 tree placement
= RECUR (TREE_OPERAND (t
, 0));
12296 tree init
= RECUR (TREE_OPERAND (t
, 3));
12297 VEC(tree
,gc
) *placement_vec
;
12298 VEC(tree
,gc
) *init_vec
;
12301 if (placement
== NULL_TREE
)
12302 placement_vec
= NULL
;
12305 placement_vec
= make_tree_vector ();
12306 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
12307 VEC_safe_push (tree
, gc
, placement_vec
, TREE_VALUE (placement
));
12310 /* If there was an initializer in the original tree, but it
12311 instantiated to an empty list, then we should pass a
12312 non-NULL empty vector to tell build_new that it was an
12313 empty initializer() rather than no initializer. This can
12314 only happen when the initializer is a pack expansion whose
12315 parameter packs are of length zero. */
12316 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
12320 init_vec
= make_tree_vector ();
12321 if (init
== void_zero_node
)
12322 gcc_assert (init_vec
!= NULL
);
12325 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
12326 VEC_safe_push (tree
, gc
, init_vec
, TREE_VALUE (init
));
12330 ret
= build_new (&placement_vec
,
12331 RECUR (TREE_OPERAND (t
, 1)),
12332 RECUR (TREE_OPERAND (t
, 2)),
12334 NEW_EXPR_USE_GLOBAL (t
),
12337 if (placement_vec
!= NULL
)
12338 release_tree_vector (placement_vec
);
12339 if (init_vec
!= NULL
)
12340 release_tree_vector (init_vec
);
12346 return delete_sanity
12347 (RECUR (TREE_OPERAND (t
, 0)),
12348 RECUR (TREE_OPERAND (t
, 1)),
12349 DELETE_EXPR_USE_VEC (t
),
12350 DELETE_EXPR_USE_GLOBAL (t
));
12352 case COMPOUND_EXPR
:
12353 return build_x_compound_expr (RECUR (TREE_OPERAND (t
, 0)),
12354 RECUR (TREE_OPERAND (t
, 1)),
12360 VEC(tree
,gc
) *call_args
;
12361 unsigned int nargs
, i
;
12366 function
= CALL_EXPR_FN (t
);
12367 /* When we parsed the expression, we determined whether or
12368 not Koenig lookup should be performed. */
12369 koenig_p
= KOENIG_LOOKUP_P (t
);
12370 if (TREE_CODE (function
) == SCOPE_REF
)
12372 qualified_p
= true;
12373 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
12375 /*address_p=*/false);
12379 if (TREE_CODE (function
) == COMPONENT_REF
)
12381 tree op
= TREE_OPERAND (function
, 1);
12383 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
12384 || (BASELINK_P (op
)
12385 && BASELINK_QUALIFIED_P (op
)));
12388 qualified_p
= false;
12390 function
= tsubst_copy_and_build (function
, args
, complain
,
12393 integral_constant_expression_p
);
12395 if (BASELINK_P (function
))
12396 qualified_p
= true;
12399 nargs
= call_expr_nargs (t
);
12400 call_args
= make_tree_vector ();
12401 for (i
= 0; i
< nargs
; ++i
)
12403 tree arg
= CALL_EXPR_ARG (t
, i
);
12405 if (!PACK_EXPANSION_P (arg
))
12406 VEC_safe_push (tree
, gc
, call_args
,
12407 RECUR (CALL_EXPR_ARG (t
, i
)));
12410 /* Expand the pack expansion and push each entry onto
12412 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
12413 if (TREE_CODE (arg
) == TREE_VEC
)
12415 unsigned int len
, j
;
12417 len
= TREE_VEC_LENGTH (arg
);
12418 for (j
= 0; j
< len
; ++j
)
12420 tree value
= TREE_VEC_ELT (arg
, j
);
12421 if (value
!= NULL_TREE
)
12422 value
= convert_from_reference (value
);
12423 VEC_safe_push (tree
, gc
, call_args
, value
);
12428 /* A partial substitution. Add one entry. */
12429 VEC_safe_push (tree
, gc
, call_args
, arg
);
12434 /* We do not perform argument-dependent lookup if normal
12435 lookup finds a non-function, in accordance with the
12436 expected resolution of DR 218. */
12438 && ((is_overloaded_fn (function
)
12439 /* If lookup found a member function, the Koenig lookup is
12440 not appropriate, even if an unqualified-name was used
12441 to denote the function. */
12442 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
12443 || TREE_CODE (function
) == IDENTIFIER_NODE
)
12444 /* Only do this when substitution turns a dependent call
12445 into a non-dependent call. */
12446 && type_dependent_expression_p_push (t
)
12447 && !any_type_dependent_arguments_p (call_args
))
12448 function
= perform_koenig_lookup (function
, call_args
);
12450 if (TREE_CODE (function
) == IDENTIFIER_NODE
)
12452 unqualified_name_lookup_error (function
);
12453 release_tree_vector (call_args
);
12454 return error_mark_node
;
12457 /* Remember that there was a reference to this entity. */
12458 if (DECL_P (function
))
12459 mark_used (function
);
12461 if (TREE_CODE (function
) == OFFSET_REF
)
12462 ret
= build_offset_ref_call_from_tree (function
, &call_args
);
12463 else if (TREE_CODE (function
) == COMPONENT_REF
)
12465 if (!BASELINK_P (TREE_OPERAND (function
, 1)))
12466 ret
= finish_call_expr (function
, &call_args
,
12467 /*disallow_virtual=*/false,
12468 /*koenig_p=*/false,
12471 ret
= (build_new_method_call
12472 (TREE_OPERAND (function
, 0),
12473 TREE_OPERAND (function
, 1),
12474 &call_args
, NULL_TREE
,
12475 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
12480 ret
= finish_call_expr (function
, &call_args
,
12481 /*disallow_virtual=*/qualified_p
,
12485 release_tree_vector (call_args
);
12491 return build_x_conditional_expr
12492 (RECUR (TREE_OPERAND (t
, 0)),
12493 RECUR (TREE_OPERAND (t
, 1)),
12494 RECUR (TREE_OPERAND (t
, 2)),
12497 case PSEUDO_DTOR_EXPR
:
12498 return finish_pseudo_destructor_expr
12499 (RECUR (TREE_OPERAND (t
, 0)),
12500 RECUR (TREE_OPERAND (t
, 1)),
12501 RECUR (TREE_OPERAND (t
, 2)));
12505 tree purpose
, value
, chain
;
12507 if (t
== void_list_node
)
12510 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
12511 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
12513 /* We have pack expansions, so expand those and
12514 create a new list out of it. */
12515 tree purposevec
= NULL_TREE
;
12516 tree valuevec
= NULL_TREE
;
12520 /* Expand the argument expressions. */
12521 if (TREE_PURPOSE (t
))
12522 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
12523 complain
, in_decl
);
12524 if (TREE_VALUE (t
))
12525 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
12526 complain
, in_decl
);
12528 /* Build the rest of the list. */
12529 chain
= TREE_CHAIN (t
);
12530 if (chain
&& chain
!= void_type_node
)
12531 chain
= RECUR (chain
);
12533 /* Determine the number of arguments. */
12534 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
12536 len
= TREE_VEC_LENGTH (purposevec
);
12537 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
12539 else if (TREE_CODE (valuevec
) == TREE_VEC
)
12540 len
= TREE_VEC_LENGTH (valuevec
);
12543 /* Since we only performed a partial substitution into
12544 the argument pack, we only return a single list
12546 if (purposevec
== TREE_PURPOSE (t
)
12547 && valuevec
== TREE_VALUE (t
)
12548 && chain
== TREE_CHAIN (t
))
12551 return tree_cons (purposevec
, valuevec
, chain
);
12554 /* Convert the argument vectors into a TREE_LIST */
12558 /* Grab the Ith values. */
12560 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
12563 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
12566 /* Build the list (backwards). */
12567 chain
= tree_cons (purpose
, value
, chain
);
12573 purpose
= TREE_PURPOSE (t
);
12575 purpose
= RECUR (purpose
);
12576 value
= TREE_VALUE (t
);
12578 value
= RECUR (value
);
12579 chain
= TREE_CHAIN (t
);
12580 if (chain
&& chain
!= void_type_node
)
12581 chain
= RECUR (chain
);
12582 if (purpose
== TREE_PURPOSE (t
)
12583 && value
== TREE_VALUE (t
)
12584 && chain
== TREE_CHAIN (t
))
12586 return tree_cons (purpose
, value
, chain
);
12589 case COMPONENT_REF
:
12595 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
12596 args
, complain
, in_decl
);
12597 /* Remember that there was a reference to this entity. */
12598 if (DECL_P (object
))
12599 mark_used (object
);
12600 object_type
= TREE_TYPE (object
);
12602 member
= TREE_OPERAND (t
, 1);
12603 if (BASELINK_P (member
))
12604 member
= tsubst_baselink (member
,
12605 non_reference (TREE_TYPE (object
)),
12606 args
, complain
, in_decl
);
12608 member
= tsubst_copy (member
, args
, complain
, in_decl
);
12609 if (member
== error_mark_node
)
12610 return error_mark_node
;
12612 if (object_type
&& !CLASS_TYPE_P (object_type
))
12614 if (SCALAR_TYPE_P (object_type
))
12616 tree s
= NULL_TREE
;
12617 tree dtor
= member
;
12619 if (TREE_CODE (dtor
) == SCOPE_REF
)
12621 s
= TREE_OPERAND (dtor
, 0);
12622 dtor
= TREE_OPERAND (dtor
, 1);
12624 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
12626 dtor
= TREE_OPERAND (dtor
, 0);
12628 return finish_pseudo_destructor_expr (object
, s
, dtor
);
12632 else if (TREE_CODE (member
) == SCOPE_REF
12633 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
12638 /* Lookup the template functions now that we know what the
12640 tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
12641 args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
12642 member
= lookup_qualified_name (TREE_OPERAND (member
, 0), tmpl
,
12643 /*is_type_p=*/false,
12644 /*complain=*/false);
12645 if (BASELINK_P (member
))
12647 BASELINK_FUNCTIONS (member
)
12648 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
12650 member
= (adjust_result_of_qualified_name_lookup
12651 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
12656 qualified_name_lookup_error (object_type
, tmpl
, member
,
12658 return error_mark_node
;
12661 else if (TREE_CODE (member
) == SCOPE_REF
12662 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
12663 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
12665 if (complain
& tf_error
)
12667 if (TYPE_P (TREE_OPERAND (member
, 0)))
12668 error ("%qT is not a class or namespace",
12669 TREE_OPERAND (member
, 0));
12671 error ("%qD is not a class or namespace",
12672 TREE_OPERAND (member
, 0));
12674 return error_mark_node
;
12676 else if (TREE_CODE (member
) == FIELD_DECL
)
12677 return finish_non_static_data_member (member
, object
, NULL_TREE
);
12679 return finish_class_member_access_expr (object
, member
,
12680 /*template_p=*/false,
12686 (RECUR (TREE_OPERAND (t
, 0)));
12690 VEC(constructor_elt
,gc
) *n
;
12691 constructor_elt
*ce
;
12692 unsigned HOST_WIDE_INT idx
;
12693 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12694 bool process_index_p
;
12696 bool need_copy_p
= false;
12699 if (type
== error_mark_node
)
12700 return error_mark_node
;
12702 /* digest_init will do the wrong thing if we let it. */
12703 if (type
&& TYPE_PTRMEMFUNC_P (type
))
12706 /* We do not want to process the index of aggregate
12707 initializers as they are identifier nodes which will be
12708 looked up by digest_init. */
12709 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
12711 n
= VEC_copy (constructor_elt
, gc
, CONSTRUCTOR_ELTS (t
));
12712 newlen
= VEC_length (constructor_elt
, n
);
12713 for (idx
= 0; VEC_iterate (constructor_elt
, n
, idx
, ce
); idx
++)
12715 if (ce
->index
&& process_index_p
)
12716 ce
->index
= RECUR (ce
->index
);
12718 if (PACK_EXPANSION_P (ce
->value
))
12720 /* Substitute into the pack expansion. */
12721 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
12724 if (ce
->value
== error_mark_node
)
12726 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
12727 /* Just move the argument into place. */
12728 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
12731 /* Update the length of the final CONSTRUCTOR
12732 arguments vector, and note that we will need to
12734 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
12735 need_copy_p
= true;
12739 ce
->value
= RECUR (ce
->value
);
12744 VEC(constructor_elt
,gc
) *old_n
= n
;
12746 n
= VEC_alloc (constructor_elt
, gc
, newlen
);
12747 for (idx
= 0; VEC_iterate (constructor_elt
, old_n
, idx
, ce
);
12750 if (TREE_CODE (ce
->value
) == TREE_VEC
)
12752 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
12753 for (i
= 0; i
< len
; ++i
)
12754 CONSTRUCTOR_APPEND_ELT (n
, 0,
12755 TREE_VEC_ELT (ce
->value
, i
));
12758 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
12762 r
= build_constructor (init_list_type_node
, n
);
12763 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
12765 if (TREE_HAS_CONSTRUCTOR (t
))
12766 return finish_compound_literal (type
, r
);
12773 tree operand_0
= RECUR (TREE_OPERAND (t
, 0));
12774 if (TYPE_P (operand_0
))
12775 return get_typeid (operand_0
);
12776 return build_typeid (operand_0
);
12786 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
12788 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
12789 /* If the original type was a reference, we'll be wrapped in
12790 the appropriate INDIRECT_REF. */
12791 r
= convert_from_reference (r
);
12796 return build_x_va_arg (RECUR (TREE_OPERAND (t
, 0)),
12797 tsubst_copy (TREE_TYPE (t
), args
, complain
,
12800 case OFFSETOF_EXPR
:
12801 return finish_offsetof (RECUR (TREE_OPERAND (t
, 0)));
12805 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
12806 complain
, in_decl
);
12808 tree type2
= TRAIT_EXPR_TYPE2 (t
);
12810 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
12812 return finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
);
12817 tree old_stmt_expr
= cur_stmt_expr
;
12818 tree stmt_expr
= begin_stmt_expr ();
12820 cur_stmt_expr
= stmt_expr
;
12821 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
12822 integral_constant_expression_p
);
12823 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
12824 cur_stmt_expr
= old_stmt_expr
;
12826 /* If the resulting list of expression statement is empty,
12827 fold it further into void_zero_node. */
12828 if (empty_expr_stmt_p (stmt_expr
))
12829 stmt_expr
= void_zero_node
;
12835 t
= tsubst_copy (t
, args
, complain
, in_decl
);
12836 /* As in finish_id_expression, we resolve enumeration constants
12837 to their underlying values. */
12838 if (TREE_CODE (t
) == CONST_DECL
)
12840 used_types_insert (TREE_TYPE (t
));
12841 return DECL_INITIAL (t
);
12847 tree r
= build_lambda_expr ();
12849 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
12850 TREE_TYPE (r
) = type
;
12851 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
12853 LAMBDA_EXPR_LOCATION (r
)
12854 = LAMBDA_EXPR_LOCATION (t
);
12855 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
12856 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
12857 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
12858 LAMBDA_EXPR_DISCRIMINATOR (r
)
12859 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
12860 LAMBDA_EXPR_CAPTURE_LIST (r
)
12861 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t
));
12862 LAMBDA_EXPR_THIS_CAPTURE (r
)
12863 = RECUR (LAMBDA_EXPR_THIS_CAPTURE (t
));
12864 LAMBDA_EXPR_EXTRA_SCOPE (r
)
12865 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t
));
12867 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
12868 determine_visibility (TYPE_NAME (type
));
12869 /* Now that we know visibility, instantiate the type so we have a
12870 declaration of the op() for later calls to lambda_function. */
12871 complete_type (type
);
12873 type
= tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
12875 apply_lambda_return_type (r
, type
);
12877 return build_lambda_object (r
);
12881 /* Handle Objective-C++ constructs, if appropriate. */
12884 = objcp_tsubst_copy_and_build (t
, args
, complain
,
12885 in_decl
, /*function_p=*/false);
12889 return tsubst_copy (t
, args
, complain
, in_decl
);
12895 /* Verify that the instantiated ARGS are valid. For type arguments,
12896 make sure that the type's linkage is ok. For non-type arguments,
12897 make sure they are constants if they are integral or enumerations.
12898 Emit an error under control of COMPLAIN, and return TRUE on error. */
12901 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
12903 if (ARGUMENT_PACK_P (t
))
12905 tree vec
= ARGUMENT_PACK_ARGS (t
);
12906 int len
= TREE_VEC_LENGTH (vec
);
12907 bool result
= false;
12910 for (i
= 0; i
< len
; ++i
)
12911 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
12915 else if (TYPE_P (t
))
12917 /* [basic.link]: A name with no linkage (notably, the name
12918 of a class or enumeration declared in a local scope)
12919 shall not be used to declare an entity with linkage.
12920 This implies that names with no linkage cannot be used as
12923 DR 757 relaxes this restriction for C++0x. */
12924 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
12925 : no_linkage_check (t
, /*relaxed_p=*/false));
12929 /* DR 488 makes use of a type with no linkage cause
12930 type deduction to fail. */
12931 if (complain
& tf_error
)
12933 if (TYPE_ANONYMOUS_P (nt
))
12934 error ("%qT is/uses anonymous type", t
);
12936 error ("template argument for %qD uses local type %qT",
12941 /* In order to avoid all sorts of complications, we do not
12942 allow variably-modified types as template arguments. */
12943 else if (variably_modified_type_p (t
, NULL_TREE
))
12945 if (complain
& tf_error
)
12946 error ("%qT is a variably modified type", t
);
12950 /* A non-type argument of integral or enumerated type must be a
12952 else if (TREE_TYPE (t
)
12953 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
12954 && !TREE_CONSTANT (t
))
12956 if (complain
& tf_error
)
12957 error ("integral expression %qE is not constant", t
);
12964 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
12966 int ix
, len
= DECL_NTPARMS (tmpl
);
12967 bool result
= false;
12969 for (ix
= 0; ix
!= len
; ix
++)
12971 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
12974 if (result
&& (complain
& tf_error
))
12975 error (" trying to instantiate %qD", tmpl
);
12979 /* Instantiate the indicated variable or function template TMPL with
12980 the template arguments in TARG_PTR. */
12983 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
12985 tree targ_ptr
= orig_args
;
12989 HOST_WIDE_INT saved_processing_template_decl
;
12991 if (tmpl
== error_mark_node
)
12992 return error_mark_node
;
12994 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
12996 /* If this function is a clone, handle it specially. */
12997 if (DECL_CLONED_FUNCTION_P (tmpl
))
13002 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
13003 DECL_CLONED_FUNCTION. */
13004 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
13005 targ_ptr
, complain
);
13006 if (spec
== error_mark_node
)
13007 return error_mark_node
;
13009 /* Look for the clone. */
13010 FOR_EACH_CLONE (clone
, spec
)
13011 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
13013 /* We should always have found the clone by now. */
13014 gcc_unreachable ();
13018 /* Check to see if we already have this specialization. */
13019 gen_tmpl
= most_general_template (tmpl
);
13020 if (tmpl
!= gen_tmpl
)
13021 /* The TMPL is a partial instantiation. To get a full set of
13022 arguments we must add the arguments used to perform the
13023 partial instantiation. */
13024 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
13027 /* It would be nice to avoid hashing here and then again in tsubst_decl,
13028 but it doesn't seem to be on the hot path. */
13029 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
13031 gcc_assert (tmpl
== gen_tmpl
13032 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
13034 || fndecl
== NULL_TREE
);
13036 if (spec
!= NULL_TREE
)
13039 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
13041 return error_mark_node
;
13043 /* We are building a FUNCTION_DECL, during which the access of its
13044 parameters and return types have to be checked. However this
13045 FUNCTION_DECL which is the desired context for access checking
13046 is not built yet. We solve this chicken-and-egg problem by
13047 deferring all checks until we have the FUNCTION_DECL. */
13048 push_deferring_access_checks (dk_deferred
);
13050 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
13051 (because, for example, we have encountered a non-dependent
13052 function call in the body of a template function and must now
13053 determine which of several overloaded functions will be called),
13054 within the instantiation itself we are not processing a
13056 saved_processing_template_decl
= processing_template_decl
;
13057 processing_template_decl
= 0;
13058 /* Substitute template parameters to obtain the specialization. */
13059 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
13060 targ_ptr
, complain
, gen_tmpl
);
13061 processing_template_decl
= saved_processing_template_decl
;
13062 if (fndecl
== error_mark_node
)
13063 return error_mark_node
;
13065 /* Now we know the specialization, compute access previously
13067 push_access_scope (fndecl
);
13069 /* Some typedefs referenced from within the template code need to be access
13070 checked at template instantiation time, i.e now. These types were
13071 added to the template at parsing time. Let's get those and perfom
13072 the acces checks then. */
13073 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl
), targ_ptr
);
13074 perform_deferred_access_checks ();
13075 pop_access_scope (fndecl
);
13076 pop_deferring_access_checks ();
13078 /* The DECL_TI_TEMPLATE should always be the immediate parent
13079 template, not the most general template. */
13080 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
13082 /* If we've just instantiated the main entry point for a function,
13083 instantiate all the alternate entry points as well. We do this
13084 by cloning the instantiation of the main entry point, not by
13085 instantiating the template clones. */
13086 if (TREE_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl
)))
13087 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
13092 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
13093 NARGS elements of the arguments that are being used when calling
13094 it. TARGS is a vector into which the deduced template arguments
13097 Return zero for success, 2 for an incomplete match that doesn't resolve
13098 all the types, and 1 for complete failure. An error message will be
13099 printed only for an incomplete match.
13101 If FN is a conversion operator, or we are trying to produce a specific
13102 specialization, RETURN_TYPE is the return type desired.
13104 The EXPLICIT_TARGS are explicit template arguments provided via a
13107 The parameter STRICT is one of:
13110 We are deducing arguments for a function call, as in
13111 [temp.deduct.call].
13114 We are deducing arguments for a conversion function, as in
13115 [temp.deduct.conv].
13118 We are deducing arguments when doing an explicit instantiation
13119 as in [temp.explicit], when determining an explicit specialization
13120 as in [temp.expl.spec], or when taking the address of a function
13121 template, as in [temp.deduct.funcaddr]. */
13124 fn_type_unification (tree fn
,
13125 tree explicit_targs
,
13128 unsigned int nargs
,
13130 unification_kind_t strict
,
13136 bool incomplete_argument_packs_p
= false;
13138 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
13140 fntype
= TREE_TYPE (fn
);
13141 if (explicit_targs
)
13145 The specified template arguments must match the template
13146 parameters in kind (i.e., type, nontype, template), and there
13147 must not be more arguments than there are parameters;
13148 otherwise type deduction fails.
13150 Nontype arguments must match the types of the corresponding
13151 nontype template parameters, or must be convertible to the
13152 types of the corresponding nontype parameters as specified in
13153 _temp.arg.nontype_, otherwise type deduction fails.
13155 All references in the function type of the function template
13156 to the corresponding template parameters are replaced by the
13157 specified template argument values. If a substitution in a
13158 template parameter or in the function type of the function
13159 template results in an invalid type, type deduction fails. */
13160 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
13161 int i
, len
= TREE_VEC_LENGTH (tparms
);
13162 tree converted_args
;
13163 bool incomplete
= false;
13165 if (explicit_targs
== error_mark_node
)
13169 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
, tf_none
,
13170 /*require_all_args=*/false,
13171 /*use_default_args=*/false));
13172 if (converted_args
== error_mark_node
)
13175 /* Substitute the explicit args into the function type. This is
13176 necessary so that, for instance, explicitly declared function
13177 arguments can match null pointed constants. If we were given
13178 an incomplete set of explicit args, we must not do semantic
13179 processing during substitution as we could create partial
13181 for (i
= 0; i
< len
; i
++)
13183 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
13184 bool parameter_pack
= false;
13186 /* Dig out the actual parm. */
13187 if (TREE_CODE (parm
) == TYPE_DECL
13188 || TREE_CODE (parm
) == TEMPLATE_DECL
)
13190 parm
= TREE_TYPE (parm
);
13191 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
13193 else if (TREE_CODE (parm
) == PARM_DECL
)
13195 parm
= DECL_INITIAL (parm
);
13196 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
13199 if (parameter_pack
)
13203 template_parm_level_and_index (parm
, &level
, &idx
);
13205 /* Mark the argument pack as "incomplete". We could
13206 still deduce more arguments during unification. */
13207 targ
= TMPL_ARG (converted_args
, level
, idx
);
13210 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
13211 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
13212 = ARGUMENT_PACK_ARGS (targ
);
13215 /* We have some incomplete argument packs. */
13216 incomplete_argument_packs_p
= true;
13220 if (incomplete_argument_packs_p
)
13221 /* Any substitution is guaranteed to be incomplete if there
13222 are incomplete argument packs, because we can still deduce
13226 incomplete
= NUM_TMPL_ARGS (explicit_targs
) != NUM_TMPL_ARGS (targs
);
13228 processing_template_decl
+= incomplete
;
13229 fntype
= tsubst (fntype
, converted_args
, tf_none
, NULL_TREE
);
13230 processing_template_decl
-= incomplete
;
13232 if (fntype
== error_mark_node
)
13235 /* Place the explicitly specified arguments in TARGS. */
13236 for (i
= NUM_TMPL_ARGS (converted_args
); i
--;)
13237 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (converted_args
, i
);
13240 /* Never do unification on the 'this' parameter. */
13241 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
13247 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
13248 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
13249 new_args
[0] = return_type
;
13250 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
13255 /* We allow incomplete unification without an error message here
13256 because the standard doesn't seem to explicitly prohibit it. Our
13257 callers must be ready to deal with unification failures in any
13259 result
= type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
13260 targs
, parms
, args
, nargs
, /*subr=*/0,
13263 if (result
== 0 && incomplete_argument_packs_p
)
13265 int i
, len
= NUM_TMPL_ARGS (targs
);
13267 /* Clear the "incomplete" flags on all argument packs. */
13268 for (i
= 0; i
< len
; i
++)
13270 tree arg
= TREE_VEC_ELT (targs
, i
);
13271 if (ARGUMENT_PACK_P (arg
))
13273 ARGUMENT_PACK_INCOMPLETE_P (arg
) = 0;
13274 ARGUMENT_PACK_EXPLICIT_ARGS (arg
) = NULL_TREE
;
13279 /* Now that we have bindings for all of the template arguments,
13280 ensure that the arguments deduced for the template template
13281 parameters have compatible template parameter lists. We cannot
13282 check this property before we have deduced all template
13283 arguments, because the template parameter types of a template
13284 template parameter might depend on prior template parameters
13285 deduced after the template template parameter. The following
13286 ill-formed example illustrates this issue:
13288 template<typename T, template<T> class C> void f(C<5>, T);
13290 template<int N> struct X {};
13293 f(X<5>(), 5l); // error: template argument deduction fails
13296 The template parameter list of 'C' depends on the template type
13297 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
13298 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
13299 time that we deduce 'C'. */
13301 && !template_template_parm_bindings_ok_p
13302 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
13306 /* All is well so far. Now, check:
13310 When all template arguments have been deduced, all uses of
13311 template parameters in nondeduced contexts are replaced with
13312 the corresponding deduced argument values. If the
13313 substitution results in an invalid type, as described above,
13314 type deduction fails. */
13316 tree substed
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
13317 if (substed
== error_mark_node
)
13320 /* If we're looking for an exact match, check that what we got
13321 is indeed an exact match. It might not be if some template
13322 parameters are used in non-deduced contexts. */
13323 if (strict
== DEDUCE_EXACT
)
13328 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (substed
));
13330 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
13331 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
13332 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
13340 /* Adjust types before performing type deduction, as described in
13341 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
13342 sections are symmetric. PARM is the type of a function parameter
13343 or the return type of the conversion function. ARG is the type of
13344 the argument passed to the call, or the type of the value
13345 initialized with the result of the conversion function.
13346 ARG_EXPR is the original argument expression, which may be null. */
13349 maybe_adjust_types_for_deduction (unification_kind_t strict
,
13363 /* Swap PARM and ARG throughout the remainder of this
13364 function; the handling is precisely symmetric since PARM
13365 will initialize ARG rather than vice versa. */
13373 /* Core issue #873: Do the DR606 thing (see below) for these cases,
13374 too, but here handle it by stripping the reference from PARM
13375 rather than by adding it to ARG. */
13376 if (TREE_CODE (*parm
) == REFERENCE_TYPE
13377 && TYPE_REF_IS_RVALUE (*parm
)
13378 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
13379 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
13380 && TREE_CODE (*arg
) == REFERENCE_TYPE
13381 && !TYPE_REF_IS_RVALUE (*arg
))
13382 *parm
= TREE_TYPE (*parm
);
13383 /* Nothing else to do in this case. */
13387 gcc_unreachable ();
13390 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
13392 /* [temp.deduct.call]
13394 If P is not a reference type:
13396 --If A is an array type, the pointer type produced by the
13397 array-to-pointer standard conversion (_conv.array_) is
13398 used in place of A for type deduction; otherwise,
13400 --If A is a function type, the pointer type produced by
13401 the function-to-pointer standard conversion
13402 (_conv.func_) is used in place of A for type deduction;
13405 --If A is a cv-qualified type, the top level
13406 cv-qualifiers of A's type are ignored for type
13408 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
13409 *arg
= build_pointer_type (TREE_TYPE (*arg
));
13410 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
13411 *arg
= build_pointer_type (*arg
);
13413 *arg
= TYPE_MAIN_VARIANT (*arg
);
13416 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
13417 of the form T&&, where T is a template parameter, and the argument
13418 is an lvalue, T is deduced as A& */
13419 if (TREE_CODE (*parm
) == REFERENCE_TYPE
13420 && TYPE_REF_IS_RVALUE (*parm
)
13421 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
13422 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
13423 && arg_expr
&& real_lvalue_p (arg_expr
))
13424 *arg
= build_reference_type (*arg
);
13426 /* [temp.deduct.call]
13428 If P is a cv-qualified type, the top level cv-qualifiers
13429 of P's type are ignored for type deduction. If P is a
13430 reference type, the type referred to by P is used for
13432 *parm
= TYPE_MAIN_VARIANT (*parm
);
13433 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
13435 *parm
= TREE_TYPE (*parm
);
13436 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
13439 /* DR 322. For conversion deduction, remove a reference type on parm
13440 too (which has been swapped into ARG). */
13441 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
13442 *arg
= TREE_TYPE (*arg
);
13447 /* Most parms like fn_type_unification.
13449 If SUBR is 1, we're being called recursively (to unify the
13450 arguments of a function or method parameter of a function
13454 type_unification_real (tree tparms
,
13458 unsigned int xnargs
,
13460 unification_kind_t strict
,
13463 tree parm
, arg
, arg_expr
;
13465 int ntparms
= TREE_VEC_LENGTH (tparms
);
13467 int saw_undeduced
= 0;
13470 unsigned int nargs
;
13473 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
13474 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
13475 gcc_assert (ntparms
> 0);
13477 /* Reset the number of non-defaulted template arguments contained
13479 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
13484 sub_strict
= (UNIFY_ALLOW_OUTER_LEVEL
| UNIFY_ALLOW_MORE_CV_QUAL
13485 | UNIFY_ALLOW_DERIVED
);
13489 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
13493 sub_strict
= UNIFY_ALLOW_NONE
;
13497 gcc_unreachable ();
13506 while (parms
&& parms
!= void_list_node
13509 if (TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
13512 parm
= TREE_VALUE (parms
);
13513 parms
= TREE_CHAIN (parms
);
13518 if (arg
== error_mark_node
)
13520 if (arg
== unknown_type_node
)
13521 /* We can't deduce anything from this, but we might get all the
13522 template args from other function args. */
13525 /* Conversions will be performed on a function argument that
13526 corresponds with a function parameter that contains only
13527 non-deducible template parameters and explicitly specified
13528 template parameters. */
13529 if (!uses_template_parms (parm
))
13534 type
= TREE_TYPE (arg
);
13538 if (same_type_p (parm
, type
))
13540 if (strict
!= DEDUCE_EXACT
13541 && can_convert_arg (parm
, type
, TYPE_P (arg
) ? NULL_TREE
: arg
,
13550 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
13551 if (type_unknown_p (arg
))
13553 /* [temp.deduct.type]
13555 A template-argument can be deduced from a pointer to
13556 function or pointer to member function argument if
13557 the set of overloaded functions does not contain
13558 function templates and at most one of a set of
13559 overloaded functions provides a unique match. */
13560 if (resolve_overloaded_unification
13561 (tparms
, targs
, parm
, arg
, strict
, sub_strict
))
13567 arg
= unlowered_expr_type (arg
);
13568 if (arg
== error_mark_node
)
13573 int arg_strict
= sub_strict
;
13576 arg_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
,
13579 if (arg
== init_list_type_node
&& arg_expr
)
13581 if (unify (tparms
, targs
, parm
, arg
, arg_strict
))
13588 && parms
!= void_list_node
13589 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
13591 /* Unify the remaining arguments with the pack expansion type. */
13593 tree parmvec
= make_tree_vec (1);
13595 /* Allocate a TREE_VEC and copy in all of the arguments */
13596 argvec
= make_tree_vec (nargs
- ia
);
13597 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
13598 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
13600 /* Copy the parameter into parmvec. */
13601 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
13602 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
13603 /*call_args_p=*/true, /*subr=*/subr
))
13606 /* Advance to the end of the list of parameters. */
13607 parms
= TREE_CHAIN (parms
);
13610 /* Fail if we've reached the end of the parm list, and more args
13611 are present, and the parm list isn't variadic. */
13612 if (ia
< nargs
&& parms
== void_list_node
)
13614 /* Fail if parms are left and they don't have default values. */
13615 if (parms
&& parms
!= void_list_node
13616 && TREE_PURPOSE (parms
) == NULL_TREE
)
13620 for (i
= 0; i
< ntparms
; i
++)
13621 if (!TREE_VEC_ELT (targs
, i
))
13625 if (TREE_VEC_ELT (tparms
, i
) == error_mark_node
)
13628 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
13630 /* If this is an undeduced nontype parameter that depends on
13631 a type parameter, try another pass; its type may have been
13632 deduced from a later argument than the one from which
13633 this parameter can be deduced. */
13634 if (TREE_CODE (tparm
) == PARM_DECL
13635 && uses_template_parms (TREE_TYPE (tparm
))
13636 && !saw_undeduced
++)
13639 /* Core issue #226 (C++0x) [temp.deduct]:
13641 If a template argument has not been deduced, its
13642 default template argument, if any, is used.
13644 When we are in C++98 mode, TREE_PURPOSE will either
13645 be NULL_TREE or ERROR_MARK_NODE, so we do not need
13646 to explicitly check cxx_dialect here. */
13647 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
13649 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
13650 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
13651 arg
= tsubst_template_arg (arg
, targs
, tf_none
, NULL_TREE
);
13652 arg
= convert_template_argument (parm
, arg
, targs
, tf_none
,
13654 if (arg
== error_mark_node
)
13658 TREE_VEC_ELT (targs
, i
) = arg
;
13659 /* The position of the first default template argument,
13660 is also the number of non-defaulted arguments in TARGS.
13662 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
13663 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
13668 /* If the type parameter is a parameter pack, then it will
13669 be deduced to an empty parameter pack. */
13670 if (template_parameter_pack_p (tparm
))
13674 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
13676 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
13677 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
13678 TREE_CONSTANT (arg
) = 1;
13681 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
13683 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
13685 TREE_VEC_ELT (targs
, i
) = arg
;
13691 #ifdef ENABLE_CHECKING
13692 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
13693 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
13699 /* Subroutine of type_unification_real. Args are like the variables
13700 at the call site. ARG is an overloaded function (or template-id);
13701 we try deducing template args from each of the overloads, and if
13702 only one succeeds, we go with that. Modifies TARGS and returns
13703 true on success. */
13706 resolve_overloaded_unification (tree tparms
,
13710 unification_kind_t strict
,
13713 tree tempargs
= copy_node (targs
);
13715 tree goodfn
= NULL_TREE
;
13718 if (TREE_CODE (arg
) == ADDR_EXPR
)
13720 arg
= TREE_OPERAND (arg
, 0);
13726 if (TREE_CODE (arg
) == COMPONENT_REF
)
13727 /* Handle `&x' where `x' is some static or non-static member
13729 arg
= TREE_OPERAND (arg
, 1);
13731 if (TREE_CODE (arg
) == OFFSET_REF
)
13732 arg
= TREE_OPERAND (arg
, 1);
13734 /* Strip baselink information. */
13735 if (BASELINK_P (arg
))
13736 arg
= BASELINK_FUNCTIONS (arg
);
13738 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
13740 /* If we got some explicit template args, we need to plug them into
13741 the affected templates before we try to unify, in case the
13742 explicit args will completely resolve the templates in question. */
13744 tree expl_subargs
= TREE_OPERAND (arg
, 1);
13745 arg
= TREE_OPERAND (arg
, 0);
13747 for (; arg
; arg
= OVL_NEXT (arg
))
13749 tree fn
= OVL_CURRENT (arg
);
13750 tree subargs
, elem
;
13752 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
13755 ++processing_template_decl
;
13756 subargs
= get_bindings (fn
, DECL_TEMPLATE_RESULT (fn
),
13757 expl_subargs
, /*check_ret=*/false);
13760 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
13761 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
13762 elem
, strict
, sub_strict
, addr_p
)
13763 && (!goodfn
|| !decls_match (goodfn
, elem
)))
13769 --processing_template_decl
;
13772 else if (TREE_CODE (arg
) != OVERLOAD
13773 && TREE_CODE (arg
) != FUNCTION_DECL
)
13774 /* If ARG is, for example, "(0, &f)" then its type will be unknown
13775 -- but the deduction does not succeed because the expression is
13776 not just the function on its own. */
13779 for (; arg
; arg
= OVL_NEXT (arg
))
13780 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
13781 TREE_TYPE (OVL_CURRENT (arg
)),
13782 strict
, sub_strict
, addr_p
)
13783 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
13785 goodfn
= OVL_CURRENT (arg
);
13789 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13790 to function or pointer to member function argument if the set of
13791 overloaded functions does not contain function templates and at most
13792 one of a set of overloaded functions provides a unique match.
13794 So if we found multiple possibilities, we return success but don't
13795 deduce anything. */
13799 int i
= TREE_VEC_LENGTH (targs
);
13801 if (TREE_VEC_ELT (tempargs
, i
))
13802 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
13810 /* Core DR 115: In contexts where deduction is done and fails, or in
13811 contexts where deduction is not done, if a template argument list is
13812 specified and it, along with any default template arguments, identifies
13813 a single function template specialization, then the template-id is an
13814 lvalue for the function template specialization. */
13817 resolve_nondeduced_context (tree orig_expr
)
13819 tree expr
, offset
, baselink
;
13822 if (!type_unknown_p (orig_expr
))
13827 offset
= NULL_TREE
;
13828 baselink
= NULL_TREE
;
13830 if (TREE_CODE (expr
) == ADDR_EXPR
)
13832 expr
= TREE_OPERAND (expr
, 0);
13835 if (TREE_CODE (expr
) == OFFSET_REF
)
13838 expr
= TREE_OPERAND (expr
, 1);
13840 if (TREE_CODE (expr
) == BASELINK
)
13843 expr
= BASELINK_FUNCTIONS (expr
);
13846 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
13849 tree goodfn
= NULL_TREE
;
13851 /* If we got some explicit template args, we need to plug them into
13852 the affected templates before we try to unify, in case the
13853 explicit args will completely resolve the templates in question. */
13855 tree expl_subargs
= TREE_OPERAND (expr
, 1);
13856 tree arg
= TREE_OPERAND (expr
, 0);
13857 tree badfn
= NULL_TREE
;
13858 tree badargs
= NULL_TREE
;
13860 for (; arg
; arg
= OVL_NEXT (arg
))
13862 tree fn
= OVL_CURRENT (arg
);
13863 tree subargs
, elem
;
13865 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
13868 ++processing_template_decl
;
13869 subargs
= get_bindings (fn
, DECL_TEMPLATE_RESULT (fn
),
13870 expl_subargs
, /*check_ret=*/false);
13871 if (subargs
&& !any_dependent_template_arguments_p (subargs
))
13873 elem
= instantiate_template (fn
, subargs
, tf_none
);
13874 if (elem
== error_mark_node
)
13879 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
13885 --processing_template_decl
;
13891 expr
= build_baselink (BASELINK_BINFO (baselink
),
13892 BASELINK_ACCESS_BINFO (baselink
),
13893 expr
, BASELINK_OPTYPE (baselink
));
13895 expr
= build2 (OFFSET_REF
, TREE_TYPE (expr
),
13896 TREE_OPERAND (offset
, 0), expr
);
13898 expr
= build_address (expr
);
13901 else if (good
== 0 && badargs
)
13902 /* There were no good options and at least one bad one, so let the
13903 user know what the problem is. */
13904 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
13909 /* Subroutine of resolve_overloaded_unification; does deduction for a single
13910 overload. Fills TARGS with any deduced arguments, or error_mark_node if
13911 different overloads deduce different arguments for a given parm.
13912 ADDR_P is true if the expression for which deduction is being
13913 performed was of the form "& fn" rather than simply "fn".
13915 Returns 1 on success. */
13918 try_one_overload (tree tparms
,
13923 unification_kind_t strict
,
13931 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13932 to function or pointer to member function argument if the set of
13933 overloaded functions does not contain function templates and at most
13934 one of a set of overloaded functions provides a unique match.
13936 So if this is a template, just return success. */
13938 if (uses_template_parms (arg
))
13941 if (TREE_CODE (arg
) == METHOD_TYPE
)
13942 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
13944 arg
= build_pointer_type (arg
);
13946 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
13948 /* We don't copy orig_targs for this because if we have already deduced
13949 some template args from previous args, unify would complain when we
13950 try to deduce a template parameter for the same argument, even though
13951 there isn't really a conflict. */
13952 nargs
= TREE_VEC_LENGTH (targs
);
13953 tempargs
= make_tree_vec (nargs
);
13955 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
) != 0)
13958 /* First make sure we didn't deduce anything that conflicts with
13959 explicitly specified args. */
13960 for (i
= nargs
; i
--; )
13962 tree elt
= TREE_VEC_ELT (tempargs
, i
);
13963 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
13967 else if (uses_template_parms (elt
))
13968 /* Since we're unifying against ourselves, we will fill in
13969 template args used in the function parm list with our own
13970 template parms. Discard them. */
13971 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
13972 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
13976 for (i
= nargs
; i
--; )
13978 tree elt
= TREE_VEC_ELT (tempargs
, i
);
13981 TREE_VEC_ELT (targs
, i
) = elt
;
13987 /* PARM is a template class (perhaps with unbound template
13988 parameters). ARG is a fully instantiated type. If ARG can be
13989 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
13990 TARGS are as for unify. */
13993 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
)
13995 tree copy_of_targs
;
13997 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
13998 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
13999 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
14002 /* We need to make a new template argument vector for the call to
14003 unify. If we used TARGS, we'd clutter it up with the result of
14004 the attempted unification, even if this class didn't work out.
14005 We also don't want to commit ourselves to all the unifications
14006 we've already done, since unification is supposed to be done on
14007 an argument-by-argument basis. In other words, consider the
14008 following pathological case:
14010 template <int I, int J, int K>
14013 template <int I, int J>
14014 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
14016 template <int I, int J, int K>
14017 void f(S<I, J, K>, S<I, I, I>);
14026 Now, by the time we consider the unification involving `s2', we
14027 already know that we must have `f<0, 0, 0>'. But, even though
14028 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
14029 because there are two ways to unify base classes of S<0, 1, 2>
14030 with S<I, I, I>. If we kept the already deduced knowledge, we
14031 would reject the possibility I=1. */
14032 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
14034 /* If unification failed, we're done. */
14035 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
14036 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
))
14042 /* Given a template type PARM and a class type ARG, find the unique
14043 base type in ARG that is an instance of PARM. We do not examine
14044 ARG itself; only its base-classes. If there is not exactly one
14045 appropriate base class, return NULL_TREE. PARM may be the type of
14046 a partial specialization, as well as a plain template type. Used
14050 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
)
14052 tree rval
= NULL_TREE
;
14055 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
14057 binfo
= TYPE_BINFO (complete_type (arg
));
14059 /* The type could not be completed. */
14062 /* Walk in inheritance graph order. The search order is not
14063 important, and this avoids multiple walks of virtual bases. */
14064 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
14066 tree r
= try_class_unification (tparms
, targs
, parm
, BINFO_TYPE (binfo
));
14070 /* If there is more than one satisfactory baseclass, then:
14074 If they yield more than one possible deduced A, the type
14078 if (rval
&& !same_type_p (r
, rval
))
14088 /* Returns the level of DECL, which declares a template parameter. */
14091 template_decl_level (tree decl
)
14093 switch (TREE_CODE (decl
))
14096 case TEMPLATE_DECL
:
14097 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
14100 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
14103 gcc_unreachable ();
14108 /* Decide whether ARG can be unified with PARM, considering only the
14109 cv-qualifiers of each type, given STRICT as documented for unify.
14110 Returns nonzero iff the unification is OK on that basis. */
14113 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
14115 int arg_quals
= cp_type_quals (arg
);
14116 int parm_quals
= cp_type_quals (parm
);
14118 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
14119 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
14121 /* Although a CVR qualifier is ignored when being applied to a
14122 substituted template parameter ([8.3.2]/1 for example), that
14123 does not allow us to unify "const T" with "int&" because both
14124 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
14125 It is ok when we're allowing additional CV qualifiers
14126 at the outer level [14.8.2.1]/3,1st bullet. */
14127 if ((TREE_CODE (arg
) == REFERENCE_TYPE
14128 || TREE_CODE (arg
) == FUNCTION_TYPE
14129 || TREE_CODE (arg
) == METHOD_TYPE
)
14130 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
14133 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
14134 && (parm_quals
& TYPE_QUAL_RESTRICT
))
14138 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
14139 && (arg_quals
& parm_quals
) != parm_quals
)
14142 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
14143 && (parm_quals
& arg_quals
) != arg_quals
)
14149 /* Determines the LEVEL and INDEX for the template parameter PARM. */
14151 template_parm_level_and_index (tree parm
, int* level
, int* index
)
14153 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
14154 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
14155 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
14157 *index
= TEMPLATE_TYPE_IDX (parm
);
14158 *level
= TEMPLATE_TYPE_LEVEL (parm
);
14162 *index
= TEMPLATE_PARM_IDX (parm
);
14163 *level
= TEMPLATE_PARM_LEVEL (parm
);
14167 /* Unifies the remaining arguments in PACKED_ARGS with the pack
14168 expansion at the end of PACKED_PARMS. Returns 0 if the type
14169 deduction succeeds, 1 otherwise. STRICT is the same as in
14170 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
14171 call argument list. We'll need to adjust the arguments to make them
14172 types. SUBR tells us if this is from a recursive call to
14173 type_unification_real. */
14175 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
14176 tree packed_args
, int strict
, bool call_args_p
,
14180 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
14181 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
14182 tree pack
, packs
= NULL_TREE
;
14183 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
14184 int len
= TREE_VEC_LENGTH (packed_args
);
14186 /* Determine the parameter packs we will be deducing from the
14187 pattern, and record their current deductions. */
14188 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
14189 pack
; pack
= TREE_CHAIN (pack
))
14191 tree parm_pack
= TREE_VALUE (pack
);
14194 /* Determine the index and level of this parameter pack. */
14195 template_parm_level_and_index (parm_pack
, &level
, &idx
);
14197 /* Keep track of the parameter packs and their corresponding
14199 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
14200 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
14203 /* Loop through all of the arguments that have not yet been
14204 unified and unify each with the pattern. */
14205 for (i
= start
; i
< len
; i
++)
14207 tree parm
= pattern
;
14209 /* For each parameter pack, clear out the deduced value so that
14210 we can deduce it again. */
14211 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
14214 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
14216 TMPL_ARG (targs
, level
, idx
) = NULL_TREE
;
14219 /* Unify the pattern with the current argument. */
14221 tree arg
= TREE_VEC_ELT (packed_args
, i
);
14222 tree arg_expr
= NULL_TREE
;
14223 int arg_strict
= strict
;
14224 bool skip_arg_p
= false;
14230 /* This mirrors what we do in type_unification_real. */
14234 sub_strict
= (UNIFY_ALLOW_OUTER_LEVEL
14235 | UNIFY_ALLOW_MORE_CV_QUAL
14236 | UNIFY_ALLOW_DERIVED
);
14240 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
14244 sub_strict
= UNIFY_ALLOW_NONE
;
14248 gcc_unreachable ();
14253 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
14254 if (type_unknown_p (arg
))
14256 /* [temp.deduct.type] A template-argument can be
14257 deduced from a pointer to function or pointer
14258 to member function argument if the set of
14259 overloaded functions does not contain function
14260 templates and at most one of a set of
14261 overloaded functions provides a unique
14264 if (resolve_overloaded_unification
14265 (tparms
, targs
, parm
, arg
,
14266 (unification_kind_t
) strict
,
14276 arg
= unlowered_expr_type (arg
);
14277 if (arg
== error_mark_node
)
14282 arg_strict
= sub_strict
;
14286 maybe_adjust_types_for_deduction ((unification_kind_t
) strict
,
14287 &parm
, &arg
, arg_expr
);
14292 /* For deduction from an init-list we need the actual list. */
14293 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
14295 if (unify (tparms
, targs
, parm
, arg
, arg_strict
))
14300 /* For each parameter pack, collect the deduced value. */
14301 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
14304 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
14306 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
14307 TMPL_ARG (targs
, level
, idx
);
14311 /* Verify that the results of unification with the parameter packs
14312 produce results consistent with what we've seen before, and make
14313 the deduced argument packs available. */
14314 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
14316 tree old_pack
= TREE_VALUE (pack
);
14317 tree new_args
= TREE_TYPE (pack
);
14318 int i
, len
= TREE_VEC_LENGTH (new_args
);
14320 bool nondeduced_p
= false;
14322 /* By default keep the original deduced argument pack.
14323 If necessary, more specific code is going to update the
14324 resulting deduced argument later down in this function. */
14325 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
14326 TMPL_ARG (targs
, level
, idx
) = old_pack
;
14328 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
14329 actually deduce anything. */
14330 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
14331 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
14332 nondeduced_p
= true;
14336 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
14338 /* Prepend the explicit arguments onto NEW_ARGS. */
14339 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
14340 tree old_args
= new_args
;
14341 int i
, explicit_len
= TREE_VEC_LENGTH (explicit_args
);
14342 int len
= explicit_len
+ TREE_VEC_LENGTH (old_args
);
14344 /* Copy the explicit arguments. */
14345 new_args
= make_tree_vec (len
);
14346 for (i
= 0; i
< explicit_len
; i
++)
14347 TREE_VEC_ELT (new_args
, i
) = TREE_VEC_ELT (explicit_args
, i
);
14349 /* Copy the deduced arguments. */
14350 for (; i
< len
; i
++)
14351 TREE_VEC_ELT (new_args
, i
) =
14352 TREE_VEC_ELT (old_args
, i
- explicit_len
);
14358 /* Build the deduced *_ARGUMENT_PACK. */
14359 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
14361 result
= make_node (NONTYPE_ARGUMENT_PACK
);
14362 TREE_TYPE (result
) =
14363 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
14364 TREE_CONSTANT (result
) = 1;
14367 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
14369 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
14371 /* Note the deduced argument packs for this parameter
14373 TMPL_ARG (targs
, level
, idx
) = result
;
14375 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
14376 && (ARGUMENT_PACK_ARGS (old_pack
)
14377 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
14379 /* We only had the explicitly-provided arguments before, but
14380 now we have a complete set of arguments. */
14381 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
14383 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
14384 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
14385 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
14387 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack
),
14389 /* Inconsistent unification of this parameter pack. */
14396 /* Deduce the value of template parameters. TPARMS is the (innermost)
14397 set of template parameters to a template. TARGS is the bindings
14398 for those template parameters, as determined thus far; TARGS may
14399 include template arguments for outer levels of template parameters
14400 as well. PARM is a parameter to a template function, or a
14401 subcomponent of that parameter; ARG is the corresponding argument.
14402 This function attempts to match PARM with ARG in a manner
14403 consistent with the existing assignments in TARGS. If more values
14404 are deduced, then TARGS is updated.
14406 Returns 0 if the type deduction succeeds, 1 otherwise. The
14407 parameter STRICT is a bitwise or of the following flags:
14410 Require an exact match between PARM and ARG.
14411 UNIFY_ALLOW_MORE_CV_QUAL:
14412 Allow the deduced ARG to be more cv-qualified (by qualification
14413 conversion) than ARG.
14414 UNIFY_ALLOW_LESS_CV_QUAL:
14415 Allow the deduced ARG to be less cv-qualified than ARG.
14416 UNIFY_ALLOW_DERIVED:
14417 Allow the deduced ARG to be a template base class of ARG,
14418 or a pointer to a template base class of the type pointed to by
14420 UNIFY_ALLOW_INTEGER:
14421 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
14422 case for more information.
14423 UNIFY_ALLOW_OUTER_LEVEL:
14424 This is the outermost level of a deduction. Used to determine validity
14425 of qualification conversions. A valid qualification conversion must
14426 have const qualified pointers leading up to the inner type which
14427 requires additional CV quals, except at the outer level, where const
14428 is not required [conv.qual]. It would be normal to set this flag in
14429 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
14430 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
14431 This is the outermost level of a deduction, and PARM can be more CV
14432 qualified at this point.
14433 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
14434 This is the outermost level of a deduction, and PARM can be less CV
14435 qualified at this point. */
14438 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
)
14443 int strict_in
= strict
;
14445 /* I don't think this will do the right thing with respect to types.
14446 But the only case I've seen it in so far has been array bounds, where
14447 signedness is the only information lost, and I think that will be
14449 while (TREE_CODE (parm
) == NOP_EXPR
)
14450 parm
= TREE_OPERAND (parm
, 0);
14452 if (arg
== error_mark_node
)
14454 if (arg
== unknown_type_node
14455 || arg
== init_list_type_node
)
14456 /* We can't deduce anything from this, but we might get all the
14457 template args from other function args. */
14460 /* If PARM uses template parameters, then we can't bail out here,
14461 even if ARG == PARM, since we won't record unifications for the
14462 template parameters. We might need them if we're trying to
14463 figure out which of two things is more specialized. */
14464 if (arg
== parm
&& !uses_template_parms (parm
))
14467 /* Handle init lists early, so the rest of the function can assume
14468 we're dealing with a type. */
14469 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
14473 tree orig_parm
= parm
;
14475 /* Replace T with std::initializer_list<T> for deduction. */
14476 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
14477 && flag_deduce_init_list
)
14478 parm
= listify (parm
);
14480 if (!is_std_init_list (parm
))
14481 /* We can only deduce from an initializer list argument if the
14482 parameter is std::initializer_list; otherwise this is a
14483 non-deduced context. */
14486 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
14488 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
14490 int elt_strict
= strict
;
14492 if (elt
== error_mark_node
)
14495 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
14497 tree type
= TREE_TYPE (elt
);
14498 /* It should only be possible to get here for a call. */
14499 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
14500 elt_strict
|= maybe_adjust_types_for_deduction
14501 (DEDUCE_CALL
, &elttype
, &type
, elt
);
14505 if (unify (tparms
, targs
, elttype
, elt
, elt_strict
))
14509 /* If the std::initializer_list<T> deduction worked, replace the
14510 deduced A with std::initializer_list<A>. */
14511 if (orig_parm
!= parm
)
14513 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
14514 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
14515 targ
= listify (targ
);
14516 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
14521 /* Immediately reject some pairs that won't unify because of
14522 cv-qualification mismatches. */
14523 if (TREE_CODE (arg
) == TREE_CODE (parm
)
14525 /* It is the elements of the array which hold the cv quals of an array
14526 type, and the elements might be template type parms. We'll check
14527 when we recurse. */
14528 && TREE_CODE (arg
) != ARRAY_TYPE
14529 /* We check the cv-qualifiers when unifying with template type
14530 parameters below. We want to allow ARG `const T' to unify with
14531 PARM `T' for example, when computing which of two templates
14532 is more specialized, for example. */
14533 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
14534 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
14537 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
14538 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
14539 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
14540 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
14541 strict
&= ~UNIFY_ALLOW_DERIVED
;
14542 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
14543 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
14545 switch (TREE_CODE (parm
))
14547 case TYPENAME_TYPE
:
14549 case UNBOUND_CLASS_TEMPLATE
:
14550 /* In a type which contains a nested-name-specifier, template
14551 argument values cannot be deduced for template parameters used
14552 within the nested-name-specifier. */
14555 case TEMPLATE_TYPE_PARM
:
14556 case TEMPLATE_TEMPLATE_PARM
:
14557 case BOUND_TEMPLATE_TEMPLATE_PARM
:
14558 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
14559 if (tparm
== error_mark_node
)
14562 if (TEMPLATE_TYPE_LEVEL (parm
)
14563 != template_decl_level (tparm
))
14564 /* The PARM is not one we're trying to unify. Just check
14565 to see if it matches ARG. */
14566 return (TREE_CODE (arg
) == TREE_CODE (parm
)
14567 && same_type_p (parm
, arg
)) ? 0 : 1;
14568 idx
= TEMPLATE_TYPE_IDX (parm
);
14569 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
14570 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
14572 /* Check for mixed types and values. */
14573 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
14574 && TREE_CODE (tparm
) != TYPE_DECL
)
14575 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
14576 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
14579 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
14581 /* ARG must be constructed from a template class or a template
14582 template parameter. */
14583 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
14584 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
14588 tree parmvec
= TYPE_TI_ARGS (parm
);
14589 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
14591 = DECL_INNERMOST_TEMPLATE_PARMS
14592 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
14594 int parm_variadic_p
= 0;
14596 /* The resolution to DR150 makes clear that default
14597 arguments for an N-argument may not be used to bind T
14598 to a template template parameter with fewer than N
14599 parameters. It is not safe to permit the binding of
14600 default arguments as an extension, as that may change
14601 the meaning of a conforming program. Consider:
14603 struct Dense { static const unsigned int dim = 1; };
14605 template <template <typename> class View,
14607 void operator+(float, View<Block> const&);
14609 template <typename Block,
14610 unsigned int Dim = Block::dim>
14611 struct Lvalue_proxy { operator float() const; };
14615 Lvalue_proxy<Dense> p;
14620 Here, if Lvalue_proxy is permitted to bind to View, then
14621 the global operator+ will be used; if they are not, the
14622 Lvalue_proxy will be converted to float. */
14623 if (coerce_template_parms (parm_parms
,
14625 TYPE_TI_TEMPLATE (parm
),
14627 /*require_all_args=*/true,
14628 /*use_default_args=*/false)
14629 == error_mark_node
)
14632 /* Deduce arguments T, i from TT<T> or TT<i>.
14633 We check each element of PARMVEC and ARGVEC individually
14634 rather than the whole TREE_VEC since they can have
14635 different number of elements. */
14637 parmvec
= expand_template_argument_pack (parmvec
);
14638 argvec
= expand_template_argument_pack (argvec
);
14640 len
= TREE_VEC_LENGTH (parmvec
);
14642 /* Check if the parameters end in a pack, making them
14645 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
14646 parm_variadic_p
= 1;
14648 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
14651 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
14653 if (unify (tparms
, targs
,
14654 TREE_VEC_ELT (parmvec
, i
),
14655 TREE_VEC_ELT (argvec
, i
),
14660 if (parm_variadic_p
14661 && unify_pack_expansion (tparms
, targs
,
14664 /*call_args_p=*/false,
14668 arg
= TYPE_TI_TEMPLATE (arg
);
14670 /* Fall through to deduce template name. */
14673 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
14674 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
14676 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
14678 /* Simple cases: Value already set, does match or doesn't. */
14679 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
14686 /* If PARM is `const T' and ARG is only `int', we don't have
14687 a match unless we are allowing additional qualification.
14688 If ARG is `const int' and PARM is just `T' that's OK;
14689 that binds `const int' to `T'. */
14690 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
14694 /* Consider the case where ARG is `const volatile int' and
14695 PARM is `const T'. Then, T should be `volatile int'. */
14696 arg
= cp_build_qualified_type_real
14697 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
14698 if (arg
== error_mark_node
)
14701 /* Simple cases: Value already set, does match or doesn't. */
14702 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
14707 /* Make sure that ARG is not a variable-sized array. (Note
14708 that were talking about variable-sized arrays (like
14709 `int[n]'), rather than arrays of unknown size (like
14710 `int[]').) We'll get very confused by such a type since
14711 the bound of the array will not be computable in an
14712 instantiation. Besides, such types are not allowed in
14713 ISO C++, so we can do as we please here. */
14714 if (variably_modified_type_p (arg
, NULL_TREE
))
14717 /* Strip typedefs as in convert_template_argument. */
14718 arg
= strip_typedefs (arg
);
14721 /* If ARG is a parameter pack or an expansion, we cannot unify
14722 against it unless PARM is also a parameter pack. */
14723 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
14724 && !template_parameter_pack_p (parm
))
14727 /* If the argument deduction results is a METHOD_TYPE,
14728 then there is a problem.
14729 METHOD_TYPE doesn't map to any real C++ type the result of
14730 the deduction can not be of that type. */
14731 if (TREE_CODE (arg
) == METHOD_TYPE
)
14734 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
14737 case TEMPLATE_PARM_INDEX
:
14738 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
14739 if (tparm
== error_mark_node
)
14742 if (TEMPLATE_PARM_LEVEL (parm
)
14743 != template_decl_level (tparm
))
14744 /* The PARM is not one we're trying to unify. Just check
14745 to see if it matches ARG. */
14746 return !(TREE_CODE (arg
) == TREE_CODE (parm
)
14747 && cp_tree_equal (parm
, arg
));
14749 idx
= TEMPLATE_PARM_IDX (parm
);
14750 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
14753 return !cp_tree_equal (targ
, arg
);
14755 /* [temp.deduct.type] If, in the declaration of a function template
14756 with a non-type template-parameter, the non-type
14757 template-parameter is used in an expression in the function
14758 parameter-list and, if the corresponding template-argument is
14759 deduced, the template-argument type shall match the type of the
14760 template-parameter exactly, except that a template-argument
14761 deduced from an array bound may be of any integral type.
14762 The non-type parameter might use already deduced type parameters. */
14763 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
14764 if (!TREE_TYPE (arg
))
14765 /* Template-parameter dependent expression. Just accept it for now.
14766 It will later be processed in convert_template_argument. */
14768 else if (same_type_p (TREE_TYPE (arg
), tparm
))
14770 else if ((strict
& UNIFY_ALLOW_INTEGER
)
14771 && (TREE_CODE (tparm
) == INTEGER_TYPE
14772 || TREE_CODE (tparm
) == BOOLEAN_TYPE
))
14773 /* Convert the ARG to the type of PARM; the deduced non-type
14774 template argument must exactly match the types of the
14775 corresponding parameter. */
14776 arg
= fold (build_nop (tparm
, arg
));
14777 else if (uses_template_parms (tparm
))
14778 /* We haven't deduced the type of this parameter yet. Try again
14784 /* If ARG is a parameter pack or an expansion, we cannot unify
14785 against it unless PARM is also a parameter pack. */
14786 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
14787 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
14790 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
14795 /* A pointer-to-member constant can be unified only with
14796 another constant. */
14797 if (TREE_CODE (arg
) != PTRMEM_CST
)
14800 /* Just unify the class member. It would be useless (and possibly
14801 wrong, depending on the strict flags) to unify also
14802 PTRMEM_CST_CLASS, because we want to be sure that both parm and
14803 arg refer to the same variable, even if through different
14804 classes. For instance:
14806 struct A { int x; };
14809 Unification of &A::x and &B::x must succeed. */
14810 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
14811 PTRMEM_CST_MEMBER (arg
), strict
);
14816 if (TREE_CODE (arg
) != POINTER_TYPE
)
14819 /* [temp.deduct.call]
14821 A can be another pointer or pointer to member type that can
14822 be converted to the deduced A via a qualification
14823 conversion (_conv.qual_).
14825 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
14826 This will allow for additional cv-qualification of the
14827 pointed-to types if appropriate. */
14829 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
14830 /* The derived-to-base conversion only persists through one
14831 level of pointers. */
14832 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
14834 return unify (tparms
, targs
, TREE_TYPE (parm
),
14835 TREE_TYPE (arg
), strict
);
14838 case REFERENCE_TYPE
:
14839 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
14841 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
14842 strict
& UNIFY_ALLOW_MORE_CV_QUAL
);
14845 if (TREE_CODE (arg
) != ARRAY_TYPE
)
14847 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
14848 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
14850 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
14857 /* Our representation of array types uses "N - 1" as the
14858 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
14859 not an integer constant. We cannot unify arbitrarily
14860 complex expressions, so we eliminate the MINUS_EXPRs
14862 parm_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (parm
));
14863 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
14866 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
14867 parm_max
= TREE_OPERAND (parm_max
, 0);
14869 arg_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (arg
));
14870 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
14873 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
14874 trying to unify the type of a variable with the type
14875 of a template parameter. For example:
14877 template <unsigned int N>
14878 void f (char (&) [N]);
14885 Here, the type of the ARG will be "int [g(i)]", and
14886 may be a SAVE_EXPR, etc. */
14887 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
14889 arg_max
= TREE_OPERAND (arg_max
, 0);
14892 /* If only one of the bounds used a MINUS_EXPR, compensate
14893 by adding one to the other bound. */
14894 if (parm_cst
&& !arg_cst
)
14895 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
14899 else if (arg_cst
&& !parm_cst
)
14900 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
14905 if (unify (tparms
, targs
, parm_max
, arg_max
, UNIFY_ALLOW_INTEGER
))
14908 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
14909 strict
& UNIFY_ALLOW_MORE_CV_QUAL
);
14916 case ENUMERAL_TYPE
:
14918 if (TREE_CODE (arg
) != TREE_CODE (parm
))
14921 /* We have already checked cv-qualification at the top of the
14923 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
14926 /* As far as unification is concerned, this wins. Later checks
14927 will invalidate it if necessary. */
14930 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
14931 /* Type INTEGER_CST can come from ordinary constant template args. */
14933 while (TREE_CODE (arg
) == NOP_EXPR
)
14934 arg
= TREE_OPERAND (arg
, 0);
14936 if (TREE_CODE (arg
) != INTEGER_CST
)
14938 return !tree_int_cst_equal (parm
, arg
);
14943 if (TREE_CODE (arg
) != TREE_VEC
)
14945 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
14947 for (i
= 0; i
< TREE_VEC_LENGTH (parm
); ++i
)
14948 if (unify (tparms
, targs
,
14949 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
14957 if (TREE_CODE (arg
) != TREE_CODE (parm
))
14960 if (TYPE_PTRMEMFUNC_P (parm
))
14962 if (!TYPE_PTRMEMFUNC_P (arg
))
14965 return unify (tparms
, targs
,
14966 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
14967 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
14971 if (CLASSTYPE_TEMPLATE_INFO (parm
))
14973 tree t
= NULL_TREE
;
14975 if (strict_in
& UNIFY_ALLOW_DERIVED
)
14977 /* First, we try to unify the PARM and ARG directly. */
14978 t
= try_class_unification (tparms
, targs
,
14983 /* Fallback to the special case allowed in
14984 [temp.deduct.call]:
14986 If P is a class, and P has the form
14987 template-id, then A can be a derived class of
14988 the deduced A. Likewise, if P is a pointer to
14989 a class of the form template-id, A can be a
14990 pointer to a derived class pointed to by the
14992 t
= get_template_base (tparms
, targs
, parm
, arg
);
14998 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
14999 && (CLASSTYPE_TI_TEMPLATE (parm
)
15000 == CLASSTYPE_TI_TEMPLATE (arg
)))
15001 /* Perhaps PARM is something like S<U> and ARG is S<int>.
15002 Then, we should unify `int' and `U'. */
15005 /* There's no chance of unification succeeding. */
15008 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
15009 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
);
15011 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
15016 case FUNCTION_TYPE
:
15018 unsigned int nargs
;
15023 if (TREE_CODE (arg
) != TREE_CODE (parm
))
15026 /* CV qualifications for methods can never be deduced, they must
15027 match exactly. We need to check them explicitly here,
15028 because type_unification_real treats them as any other
15029 cv-qualified parameter. */
15030 if (TREE_CODE (parm
) == METHOD_TYPE
15031 && (!check_cv_quals_for_unify
15033 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg
))),
15034 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm
))))))
15037 if (unify (tparms
, targs
, TREE_TYPE (parm
),
15038 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
))
15041 nargs
= list_length (TYPE_ARG_TYPES (arg
));
15042 args
= XALLOCAVEC (tree
, nargs
);
15043 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
15044 a
!= NULL_TREE
&& a
!= void_list_node
;
15045 a
= TREE_CHAIN (a
), ++i
)
15046 args
[i
] = TREE_VALUE (a
);
15049 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
15050 args
, nargs
, 1, DEDUCE_EXACT
,
15055 /* Unify a pointer to member with a pointer to member function, which
15056 deduces the type of the member as a function type. */
15057 if (TYPE_PTRMEMFUNC_P (arg
))
15062 /* Check top-level cv qualifiers */
15063 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
15066 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
15067 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
), UNIFY_ALLOW_NONE
))
15070 /* Determine the type of the function we are unifying against. */
15071 method_type
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg
));
15073 build_function_type (TREE_TYPE (method_type
),
15074 TREE_CHAIN (TYPE_ARG_TYPES (method_type
)));
15076 /* Extract the cv-qualifiers of the member function from the
15077 implicit object parameter and place them on the function
15078 type to be restored later. */
15079 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (method_type
));
15080 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
);
15083 if (TREE_CODE (arg
) != OFFSET_TYPE
)
15085 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
15086 TYPE_OFFSET_BASETYPE (arg
), UNIFY_ALLOW_NONE
))
15088 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
15092 if (DECL_TEMPLATE_PARM_P (parm
))
15093 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
);
15094 if (arg
!= integral_constant_value (parm
))
15099 case TEMPLATE_DECL
:
15100 /* Matched cases are handled by the ARG == PARM test above. */
15104 /* A non-type template parameter that is a variable should be a
15105 an integral constant, in which case, it whould have been
15106 folded into its (constant) value. So we should not be getting
15107 a variable here. */
15108 gcc_unreachable ();
15110 case TYPE_ARGUMENT_PACK
:
15111 case NONTYPE_ARGUMENT_PACK
:
15113 tree packed_parms
= ARGUMENT_PACK_ARGS (parm
);
15114 tree packed_args
= ARGUMENT_PACK_ARGS (arg
);
15115 int i
, len
= TREE_VEC_LENGTH (packed_parms
);
15116 int argslen
= TREE_VEC_LENGTH (packed_args
);
15117 int parm_variadic_p
= 0;
15119 for (i
= 0; i
< len
; ++i
)
15121 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms
, i
)))
15124 /* We can unify against something with a trailing
15126 parm_variadic_p
= 1;
15128 /* Since there is something following the pack
15129 expansion, we cannot unify this template argument
15136 /* If we don't have enough arguments to satisfy the parameters
15137 (not counting the pack expression at the end), or we have
15138 too many arguments for a parameter list that doesn't end in
15139 a pack expression, we can't unify. */
15140 if (argslen
< (len
- parm_variadic_p
)
15141 || (argslen
> len
&& !parm_variadic_p
))
15144 /* Unify all of the parameters that precede the (optional)
15145 pack expression. */
15146 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
15148 if (unify (tparms
, targs
, TREE_VEC_ELT (packed_parms
, i
),
15149 TREE_VEC_ELT (packed_args
, i
), strict
))
15153 if (parm_variadic_p
)
15154 return unify_pack_expansion (tparms
, targs
,
15155 packed_parms
, packed_args
,
15156 strict
, /*call_args_p=*/false,
15164 case DECLTYPE_TYPE
:
15165 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
15170 /* Unification fails if we hit an error node. */
15174 gcc_assert (EXPR_P (parm
));
15176 /* We must be looking at an expression. This can happen with
15180 void foo(S<I>, S<I + 2>);
15182 This is a "nondeduced context":
15186 The nondeduced contexts are:
15188 --A type that is a template-id in which one or more of
15189 the template-arguments is an expression that references
15190 a template-parameter.
15192 In these cases, we assume deduction succeeded, but don't
15193 actually infer any unifications. */
15195 if (!uses_template_parms (parm
)
15196 && !template_args_equal (parm
, arg
))
15203 /* Note that DECL can be defined in this translation unit, if
15207 mark_definable (tree decl
)
15210 DECL_NOT_REALLY_EXTERN (decl
) = 1;
15211 FOR_EACH_CLONE (clone
, decl
)
15212 DECL_NOT_REALLY_EXTERN (clone
) = 1;
15215 /* Called if RESULT is explicitly instantiated, or is a member of an
15216 explicitly instantiated class. */
15219 mark_decl_instantiated (tree result
, int extern_p
)
15221 SET_DECL_EXPLICIT_INSTANTIATION (result
);
15223 /* If this entity has already been written out, it's too late to
15224 make any modifications. */
15225 if (TREE_ASM_WRITTEN (result
))
15228 if (TREE_CODE (result
) != FUNCTION_DECL
)
15229 /* The TREE_PUBLIC flag for function declarations will have been
15230 set correctly by tsubst. */
15231 TREE_PUBLIC (result
) = 1;
15233 /* This might have been set by an earlier implicit instantiation. */
15234 DECL_COMDAT (result
) = 0;
15237 DECL_NOT_REALLY_EXTERN (result
) = 0;
15240 mark_definable (result
);
15241 /* Always make artificials weak. */
15242 if (DECL_ARTIFICIAL (result
) && flag_weak
)
15243 comdat_linkage (result
);
15244 /* For WIN32 we also want to put explicit instantiations in
15245 linkonce sections. */
15246 else if (TREE_PUBLIC (result
))
15247 maybe_make_one_only (result
);
15250 /* If EXTERN_P, then this function will not be emitted -- unless
15251 followed by an explicit instantiation, at which point its linkage
15252 will be adjusted. If !EXTERN_P, then this function will be
15253 emitted here. In neither circumstance do we want
15254 import_export_decl to adjust the linkage. */
15255 DECL_INTERFACE_KNOWN (result
) = 1;
15258 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
15259 important template arguments. If any are missing, we check whether
15260 they're important by using error_mark_node for substituting into any
15261 args that were used for partial ordering (the ones between ARGS and END)
15262 and seeing if it bubbles up. */
15265 check_undeduced_parms (tree targs
, tree args
, tree end
)
15267 bool found
= false;
15269 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
15270 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
15273 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
15277 for (; args
!= end
; args
= TREE_CHAIN (args
))
15279 tree substed
= tsubst (TREE_VALUE (args
), targs
, tf_none
, NULL_TREE
);
15280 if (substed
== error_mark_node
)
15287 /* Given two function templates PAT1 and PAT2, return:
15289 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
15290 -1 if PAT2 is more specialized than PAT1.
15291 0 if neither is more specialized.
15293 LEN indicates the number of parameters we should consider
15294 (defaulted parameters should not be considered).
15296 The 1998 std underspecified function template partial ordering, and
15297 DR214 addresses the issue. We take pairs of arguments, one from
15298 each of the templates, and deduce them against each other. One of
15299 the templates will be more specialized if all the *other*
15300 template's arguments deduce against its arguments and at least one
15301 of its arguments *does* *not* deduce against the other template's
15302 corresponding argument. Deduction is done as for class templates.
15303 The arguments used in deduction have reference and top level cv
15304 qualifiers removed. Iff both arguments were originally reference
15305 types *and* deduction succeeds in both directions, the template
15306 with the more cv-qualified argument wins for that pairing (if
15307 neither is more cv-qualified, they both are equal). Unlike regular
15308 deduction, after all the arguments have been deduced in this way,
15309 we do *not* verify the deduced template argument values can be
15310 substituted into non-deduced contexts.
15312 The logic can be a bit confusing here, because we look at deduce1 and
15313 targs1 to see if pat2 is at least as specialized, and vice versa; if we
15314 can find template arguments for pat1 to make arg1 look like arg2, that
15315 means that arg2 is at least as specialized as arg1. */
15318 more_specialized_fn (tree pat1
, tree pat2
, int len
)
15320 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
15321 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
15322 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
15323 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
15324 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
15325 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
15326 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
15327 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
15328 tree origs1
, origs2
;
15329 bool lose1
= false;
15330 bool lose2
= false;
15332 /* Remove the this parameter from non-static member functions. If
15333 one is a non-static member function and the other is not a static
15334 member function, remove the first parameter from that function
15335 also. This situation occurs for operator functions where we
15336 locate both a member function (with this pointer) and non-member
15337 operator (with explicit first operand). */
15338 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
15340 len
--; /* LEN is the number of significant arguments for DECL1 */
15341 args1
= TREE_CHAIN (args1
);
15342 if (!DECL_STATIC_FUNCTION_P (decl2
))
15343 args2
= TREE_CHAIN (args2
);
15345 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
15347 args2
= TREE_CHAIN (args2
);
15348 if (!DECL_STATIC_FUNCTION_P (decl1
))
15351 args1
= TREE_CHAIN (args1
);
15355 /* If only one is a conversion operator, they are unordered. */
15356 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
15359 /* Consider the return type for a conversion function */
15360 if (DECL_CONV_FN_P (decl1
))
15362 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
15363 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
15367 processing_template_decl
++;
15373 /* Stop when an ellipsis is seen. */
15374 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
15376 tree arg1
= TREE_VALUE (args1
);
15377 tree arg2
= TREE_VALUE (args2
);
15378 int deduce1
, deduce2
;
15382 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
15383 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
15385 /* When both arguments are pack expansions, we need only
15386 unify the patterns themselves. */
15387 arg1
= PACK_EXPANSION_PATTERN (arg1
);
15388 arg2
= PACK_EXPANSION_PATTERN (arg2
);
15390 /* This is the last comparison we need to do. */
15394 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
15396 arg1
= TREE_TYPE (arg1
);
15397 quals1
= cp_type_quals (arg1
);
15400 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
15402 arg2
= TREE_TYPE (arg2
);
15403 quals2
= cp_type_quals (arg2
);
15406 if ((quals1
< 0) != (quals2
< 0))
15408 /* Only of the args is a reference, see if we should apply
15409 array/function pointer decay to it. This is not part of
15410 DR214, but is, IMHO, consistent with the deduction rules
15411 for the function call itself, and with our earlier
15412 implementation of the underspecified partial ordering
15413 rules. (nathan). */
15416 switch (TREE_CODE (arg1
))
15419 arg1
= TREE_TYPE (arg1
);
15421 case FUNCTION_TYPE
:
15422 arg1
= build_pointer_type (arg1
);
15431 switch (TREE_CODE (arg2
))
15434 arg2
= TREE_TYPE (arg2
);
15436 case FUNCTION_TYPE
:
15437 arg2
= build_pointer_type (arg2
);
15446 arg1
= TYPE_MAIN_VARIANT (arg1
);
15447 arg2
= TYPE_MAIN_VARIANT (arg2
);
15449 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
15451 int i
, len2
= list_length (args2
);
15452 tree parmvec
= make_tree_vec (1);
15453 tree argvec
= make_tree_vec (len2
);
15456 /* Setup the parameter vector, which contains only ARG1. */
15457 TREE_VEC_ELT (parmvec
, 0) = arg1
;
15459 /* Setup the argument vector, which contains the remaining
15461 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
15462 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
15464 deduce1
= !unify_pack_expansion (tparms1
, targs1
, parmvec
,
15465 argvec
, UNIFY_ALLOW_NONE
,
15466 /*call_args_p=*/false,
15469 /* We cannot deduce in the other direction, because ARG1 is
15470 a pack expansion but ARG2 is not. */
15473 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
15475 int i
, len1
= list_length (args1
);
15476 tree parmvec
= make_tree_vec (1);
15477 tree argvec
= make_tree_vec (len1
);
15480 /* Setup the parameter vector, which contains only ARG1. */
15481 TREE_VEC_ELT (parmvec
, 0) = arg2
;
15483 /* Setup the argument vector, which contains the remaining
15485 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
15486 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
15488 deduce2
= !unify_pack_expansion (tparms2
, targs2
, parmvec
,
15489 argvec
, UNIFY_ALLOW_NONE
,
15490 /*call_args_p=*/false,
15493 /* We cannot deduce in the other direction, because ARG2 is
15494 a pack expansion but ARG1 is not.*/
15500 /* The normal case, where neither argument is a pack
15502 deduce1
= !unify (tparms1
, targs1
, arg1
, arg2
, UNIFY_ALLOW_NONE
);
15503 deduce2
= !unify (tparms2
, targs2
, arg2
, arg1
, UNIFY_ALLOW_NONE
);
15506 /* If we couldn't deduce arguments for tparms1 to make arg1 match
15507 arg2, then arg2 is not as specialized as arg1. */
15513 /* "If, for a given type, deduction succeeds in both directions
15514 (i.e., the types are identical after the transformations above)
15515 and if the type from the argument template is more cv-qualified
15516 than the type from the parameter template (as described above)
15517 that type is considered to be more specialized than the other. If
15518 neither type is more cv-qualified than the other then neither type
15519 is more specialized than the other." */
15521 if (deduce1
&& deduce2
15522 && quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
15524 if ((quals1
& quals2
) == quals2
)
15526 if ((quals1
& quals2
) == quals1
)
15530 if (lose1
&& lose2
)
15531 /* We've failed to deduce something in either direction.
15532 These must be unordered. */
15535 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
15536 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
15537 /* We have already processed all of the arguments in our
15538 handing of the pack expansion type. */
15541 args1
= TREE_CHAIN (args1
);
15542 args2
= TREE_CHAIN (args2
);
15545 /* "In most cases, all template parameters must have values in order for
15546 deduction to succeed, but for partial ordering purposes a template
15547 parameter may remain without a value provided it is not used in the
15548 types being used for partial ordering."
15550 Thus, if we are missing any of the targs1 we need to substitute into
15551 origs1, then pat2 is not as specialized as pat1. This can happen when
15552 there is a nondeduced context. */
15553 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
15555 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
15558 processing_template_decl
--;
15560 /* All things being equal, if the next argument is a pack expansion
15561 for one function but not for the other, prefer the
15562 non-variadic function. FIXME this is bogus; see c++/41958. */
15564 && args1
&& TREE_VALUE (args1
)
15565 && args2
&& TREE_VALUE (args2
))
15567 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
15568 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
15571 if (lose1
== lose2
)
15579 /* Determine which of two partial specializations is more specialized.
15581 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
15582 to the first partial specialization. The TREE_VALUE is the
15583 innermost set of template parameters for the partial
15584 specialization. PAT2 is similar, but for the second template.
15586 Return 1 if the first partial specialization is more specialized;
15587 -1 if the second is more specialized; 0 if neither is more
15590 See [temp.class.order] for information about determining which of
15591 two templates is more specialized. */
15594 more_specialized_class (tree pat1
, tree pat2
)
15599 bool any_deductions
= false;
15601 tmpl1
= TREE_TYPE (pat1
);
15602 tmpl2
= TREE_TYPE (pat2
);
15604 /* Just like what happens for functions, if we are ordering between
15605 different class template specializations, we may encounter dependent
15606 types in the arguments, and we need our dependency check functions
15607 to behave correctly. */
15608 ++processing_template_decl
;
15609 targs
= get_class_bindings (TREE_VALUE (pat1
),
15610 CLASSTYPE_TI_ARGS (tmpl1
),
15611 CLASSTYPE_TI_ARGS (tmpl2
));
15615 any_deductions
= true;
15618 targs
= get_class_bindings (TREE_VALUE (pat2
),
15619 CLASSTYPE_TI_ARGS (tmpl2
),
15620 CLASSTYPE_TI_ARGS (tmpl1
));
15624 any_deductions
= true;
15626 --processing_template_decl
;
15628 /* In the case of a tie where at least one of the class templates
15629 has a parameter pack at the end, the template with the most
15630 non-packed parameters wins. */
15633 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
15634 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
15636 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
15637 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
15638 int len1
= TREE_VEC_LENGTH (args1
);
15639 int len2
= TREE_VEC_LENGTH (args2
);
15641 /* We don't count the pack expansion at the end. */
15642 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
15644 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
15649 else if (len1
< len2
)
15656 /* Return the template arguments that will produce the function signature
15657 DECL from the function template FN, with the explicit template
15658 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
15659 also match. Return NULL_TREE if no satisfactory arguments could be
15663 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
15665 int ntparms
= DECL_NTPARMS (fn
);
15666 tree targs
= make_tree_vec (ntparms
);
15668 tree decl_arg_types
;
15670 unsigned int nargs
, ix
;
15673 /* Substitute the explicit template arguments into the type of DECL.
15674 The call to fn_type_unification will handle substitution into the
15676 decl_type
= TREE_TYPE (decl
);
15677 if (explicit_args
&& uses_template_parms (decl_type
))
15680 tree converted_args
;
15682 if (DECL_TEMPLATE_INFO (decl
))
15683 tmpl
= DECL_TI_TEMPLATE (decl
);
15685 /* We can get here for some invalid specializations. */
15689 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
15690 explicit_args
, NULL_TREE
,
15692 /*require_all_args=*/false,
15693 /*use_default_args=*/false);
15694 if (converted_args
== error_mark_node
)
15697 decl_type
= tsubst (decl_type
, converted_args
, tf_none
, NULL_TREE
);
15698 if (decl_type
== error_mark_node
)
15702 /* Never do unification on the 'this' parameter. */
15703 decl_arg_types
= skip_artificial_parms_for (decl
,
15704 TYPE_ARG_TYPES (decl_type
));
15706 nargs
= list_length (decl_arg_types
);
15707 args
= XALLOCAVEC (tree
, nargs
);
15708 for (arg
= decl_arg_types
, ix
= 0;
15709 arg
!= NULL_TREE
&& arg
!= void_list_node
;
15710 arg
= TREE_CHAIN (arg
), ++ix
)
15711 args
[ix
] = TREE_VALUE (arg
);
15713 if (fn_type_unification (fn
, explicit_args
, targs
,
15715 (check_rettype
|| DECL_CONV_FN_P (fn
)
15716 ? TREE_TYPE (decl_type
) : NULL_TREE
),
15717 DEDUCE_EXACT
, LOOKUP_NORMAL
))
15723 /* Return the innermost template arguments that, when applied to a
15724 template specialization whose innermost template parameters are
15725 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
15728 For example, suppose we have:
15730 template <class T, class U> struct S {};
15731 template <class T> struct S<T*, int> {};
15733 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
15734 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
15735 int}. The resulting vector will be {double}, indicating that `T'
15736 is bound to `double'. */
15739 get_class_bindings (tree tparms
, tree spec_args
, tree args
)
15741 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
15743 tree innermost_deduced_args
;
15745 innermost_deduced_args
= make_tree_vec (ntparms
);
15746 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
15748 deduced_args
= copy_node (args
);
15749 SET_TMPL_ARGS_LEVEL (deduced_args
,
15750 TMPL_ARGS_DEPTH (deduced_args
),
15751 innermost_deduced_args
);
15754 deduced_args
= innermost_deduced_args
;
15756 if (unify (tparms
, deduced_args
,
15757 INNERMOST_TEMPLATE_ARGS (spec_args
),
15758 INNERMOST_TEMPLATE_ARGS (args
),
15762 for (i
= 0; i
< ntparms
; ++i
)
15763 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
15766 /* Verify that nondeduced template arguments agree with the type
15767 obtained from argument deduction.
15771 struct A { typedef int X; };
15772 template <class T, class U> struct C {};
15773 template <class T> struct C<T, typename T::X> {};
15775 Then with the instantiation `C<A, int>', we can deduce that
15776 `T' is `A' but unify () does not check whether `typename T::X'
15778 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
15779 if (spec_args
== error_mark_node
15780 /* We only need to check the innermost arguments; the other
15781 arguments will always agree. */
15782 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
15783 INNERMOST_TEMPLATE_ARGS (args
)))
15786 /* Now that we have bindings for all of the template arguments,
15787 ensure that the arguments deduced for the template template
15788 parameters have compatible template parameter lists. See the use
15789 of template_template_parm_bindings_ok_p in fn_type_unification
15790 for more information. */
15791 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
15794 return deduced_args
;
15797 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
15798 Return the TREE_LIST node with the most specialized template, if
15799 any. If there is no most specialized template, the error_mark_node
15802 Note that this function does not look at, or modify, the
15803 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
15804 returned is one of the elements of INSTANTIATIONS, callers may
15805 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
15806 and retrieve it from the value returned. */
15809 most_specialized_instantiation (tree templates
)
15813 ++processing_template_decl
;
15816 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
15820 if (get_bindings (TREE_VALUE (champ
),
15821 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
15822 NULL_TREE
, /*check_ret=*/false))
15825 if (get_bindings (TREE_VALUE (fn
),
15826 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
15827 NULL_TREE
, /*check_ret=*/false))
15834 /* Equally specialized, move to next function. If there
15835 is no next function, nothing's most specialized. */
15836 fn
= TREE_CHAIN (fn
);
15844 /* Now verify that champ is better than everything earlier in the
15845 instantiation list. */
15846 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
15847 if (get_bindings (TREE_VALUE (champ
),
15848 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
15849 NULL_TREE
, /*check_ret=*/false)
15850 || !get_bindings (TREE_VALUE (fn
),
15851 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
15852 NULL_TREE
, /*check_ret=*/false))
15858 processing_template_decl
--;
15861 return error_mark_node
;
15866 /* If DECL is a specialization of some template, return the most
15867 general such template. Otherwise, returns NULL_TREE.
15869 For example, given:
15871 template <class T> struct S { template <class U> void f(U); };
15873 if TMPL is `template <class U> void S<int>::f(U)' this will return
15874 the full template. This function will not trace past partial
15875 specializations, however. For example, given in addition:
15877 template <class T> struct S<T*> { template <class U> void f(U); };
15879 if TMPL is `template <class U> void S<int*>::f(U)' this will return
15880 `template <class T> template <class U> S<T*>::f(U)'. */
15883 most_general_template (tree decl
)
15885 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
15886 an immediate specialization. */
15887 if (TREE_CODE (decl
) == FUNCTION_DECL
)
15889 if (DECL_TEMPLATE_INFO (decl
)) {
15890 decl
= DECL_TI_TEMPLATE (decl
);
15892 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
15893 template friend. */
15894 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
15900 /* Look for more and more general templates. */
15901 while (DECL_TEMPLATE_INFO (decl
))
15903 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
15904 (See cp-tree.h for details.) */
15905 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
15908 if (CLASS_TYPE_P (TREE_TYPE (decl
))
15909 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
15912 /* Stop if we run into an explicitly specialized class template. */
15913 if (!DECL_NAMESPACE_SCOPE_P (decl
)
15914 && DECL_CONTEXT (decl
)
15915 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
15918 decl
= DECL_TI_TEMPLATE (decl
);
15924 /* Return the most specialized of the class template partial
15925 specializations of TMPL which can produce TYPE, a specialization of
15926 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
15927 a _TYPE node corresponding to the partial specialization, while the
15928 TREE_PURPOSE is the set of template arguments that must be
15929 substituted into the TREE_TYPE in order to generate TYPE.
15931 If the choice of partial specialization is ambiguous, a diagnostic
15932 is issued, and the error_mark_node is returned. If there are no
15933 partial specializations of TMPL matching TYPE, then NULL_TREE is
15937 most_specialized_class (tree type
, tree tmpl
)
15939 tree list
= NULL_TREE
;
15945 tree outer_args
= NULL_TREE
;
15947 tmpl
= most_general_template (tmpl
);
15948 args
= CLASSTYPE_TI_ARGS (type
);
15950 /* For determining which partial specialization to use, only the
15951 innermost args are interesting. */
15952 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
15954 outer_args
= strip_innermost_template_args (args
, 1);
15955 args
= INNERMOST_TEMPLATE_ARGS (args
);
15958 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
15960 tree partial_spec_args
;
15962 tree parms
= TREE_VALUE (t
);
15964 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
15966 ++processing_template_decl
;
15972 /* Discard the outer levels of args, and then substitute in the
15973 template args from the enclosing class. */
15974 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
15975 partial_spec_args
= tsubst_template_args
15976 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
15978 /* PARMS already refers to just the innermost parms, but the
15979 template parms in partial_spec_args had their levels lowered
15980 by tsubst, so we need to do the same for the parm list. We
15981 can't just tsubst the TREE_VEC itself, as tsubst wants to
15982 treat a TREE_VEC as an argument vector. */
15983 parms
= copy_node (parms
);
15984 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
15985 TREE_VEC_ELT (parms
, i
) =
15986 tsubst (TREE_VEC_ELT (parms
, i
), outer_args
, tf_none
, NULL_TREE
);
15990 partial_spec_args
=
15991 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
15992 add_to_template_args (outer_args
,
15993 partial_spec_args
),
15995 /*require_all_args=*/true,
15996 /*use_default_args=*/true);
15998 --processing_template_decl
;
16000 if (partial_spec_args
== error_mark_node
)
16001 return error_mark_node
;
16003 spec_args
= get_class_bindings (parms
,
16009 spec_args
= add_to_template_args (outer_args
, spec_args
);
16010 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
16011 TREE_TYPE (list
) = TREE_TYPE (t
);
16018 ambiguous_p
= false;
16021 t
= TREE_CHAIN (t
);
16022 for (; t
; t
= TREE_CHAIN (t
))
16024 fate
= more_specialized_class (champ
, t
);
16031 t
= TREE_CHAIN (t
);
16034 ambiguous_p
= true;
16043 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
16045 fate
= more_specialized_class (champ
, t
);
16048 ambiguous_p
= true;
16056 char *spaces
= NULL
;
16057 error ("ambiguous class template instantiation for %q#T", type
);
16058 str
= TREE_CHAIN (list
) ? _("candidates are:") : _("candidate is:");
16059 for (t
= list
; t
; t
= TREE_CHAIN (t
))
16061 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
16062 spaces
= spaces
? spaces
: get_spaces (str
);
16065 return error_mark_node
;
16071 /* Explicitly instantiate DECL. */
16074 do_decl_instantiation (tree decl
, tree storage
)
16076 tree result
= NULL_TREE
;
16079 if (!decl
|| decl
== error_mark_node
)
16080 /* An error occurred, for which grokdeclarator has already issued
16081 an appropriate message. */
16083 else if (! DECL_LANG_SPECIFIC (decl
))
16085 error ("explicit instantiation of non-template %q#D", decl
);
16088 else if (TREE_CODE (decl
) == VAR_DECL
)
16090 /* There is an asymmetry here in the way VAR_DECLs and
16091 FUNCTION_DECLs are handled by grokdeclarator. In the case of
16092 the latter, the DECL we get back will be marked as a
16093 template instantiation, and the appropriate
16094 DECL_TEMPLATE_INFO will be set up. This does not happen for
16095 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
16096 should handle VAR_DECLs as it currently handles
16098 if (!DECL_CLASS_SCOPE_P (decl
))
16100 error ("%qD is not a static data member of a class template", decl
);
16103 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
16104 if (!result
|| TREE_CODE (result
) != VAR_DECL
)
16106 error ("no matching template for %qD found", decl
);
16109 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
16111 error ("type %qT for explicit instantiation %qD does not match "
16112 "declared type %qT", TREE_TYPE (result
), decl
,
16117 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
16119 error ("explicit instantiation of %q#D", decl
);
16125 /* Check for various error cases. Note that if the explicit
16126 instantiation is valid the RESULT will currently be marked as an
16127 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
16128 until we get here. */
16130 if (DECL_TEMPLATE_SPECIALIZATION (result
))
16132 /* DR 259 [temp.spec].
16134 Both an explicit instantiation and a declaration of an explicit
16135 specialization shall not appear in a program unless the explicit
16136 instantiation follows a declaration of the explicit specialization.
16138 For a given set of template parameters, if an explicit
16139 instantiation of a template appears after a declaration of an
16140 explicit specialization for that template, the explicit
16141 instantiation has no effect. */
16144 else if (DECL_EXPLICIT_INSTANTIATION (result
))
16148 No program shall explicitly instantiate any template more
16151 We check DECL_NOT_REALLY_EXTERN so as not to complain when
16152 the first instantiation was `extern' and the second is not,
16153 and EXTERN_P for the opposite case. */
16154 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
16155 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
16156 /* If an "extern" explicit instantiation follows an ordinary
16157 explicit instantiation, the template is instantiated. */
16161 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
16163 error ("no matching template for %qD found", result
);
16166 else if (!DECL_TEMPLATE_INFO (result
))
16168 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
16172 if (storage
== NULL_TREE
)
16174 else if (storage
== ridpointers
[(int) RID_EXTERN
])
16176 if (!in_system_header
&& (cxx_dialect
== cxx98
))
16177 pedwarn (input_location
, OPT_pedantic
,
16178 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
16183 error ("storage class %qD applied to template instantiation", storage
);
16185 check_explicit_instantiation_namespace (result
);
16186 mark_decl_instantiated (result
, extern_p
);
16188 instantiate_decl (result
, /*defer_ok=*/1,
16189 /*expl_inst_class_mem_p=*/false);
16193 mark_class_instantiated (tree t
, int extern_p
)
16195 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
16196 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
16197 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
16198 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
16201 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
16202 rest_of_type_compilation (t
, 1);
16206 /* Called from do_type_instantiation through binding_table_foreach to
16207 do recursive instantiation for the type bound in ENTRY. */
16209 bt_instantiate_type_proc (binding_entry entry
, void *data
)
16211 tree storage
= *(tree
*) data
;
16213 if (MAYBE_CLASS_TYPE_P (entry
->type
)
16214 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
16215 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
16218 /* Called from do_type_instantiation to instantiate a member
16219 (a member function or a static member variable) of an
16220 explicitly instantiated class template. */
16222 instantiate_class_member (tree decl
, int extern_p
)
16224 mark_decl_instantiated (decl
, extern_p
);
16226 instantiate_decl (decl
, /*defer_ok=*/1,
16227 /*expl_inst_class_mem_p=*/true);
16230 /* Perform an explicit instantiation of template class T. STORAGE, if
16231 non-null, is the RID for extern, inline or static. COMPLAIN is
16232 nonzero if this is called from the parser, zero if called recursively,
16233 since the standard is unclear (as detailed below). */
16236 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
16241 int previous_instantiation_extern_p
= 0;
16243 if (TREE_CODE (t
) == TYPE_DECL
)
16246 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
16248 error ("explicit instantiation of non-template type %qT", t
);
16254 if (!COMPLETE_TYPE_P (t
))
16256 if (complain
& tf_error
)
16257 error ("explicit instantiation of %q#T before definition of template",
16262 if (storage
!= NULL_TREE
)
16264 if (!in_system_header
)
16266 if (storage
== ridpointers
[(int) RID_EXTERN
])
16268 if (cxx_dialect
== cxx98
)
16269 pedwarn (input_location
, OPT_pedantic
,
16270 "ISO C++ 1998 forbids the use of %<extern%> on "
16271 "explicit instantiations");
16274 pedwarn (input_location
, OPT_pedantic
,
16275 "ISO C++ forbids the use of %qE"
16276 " on explicit instantiations", storage
);
16279 if (storage
== ridpointers
[(int) RID_INLINE
])
16281 else if (storage
== ridpointers
[(int) RID_EXTERN
])
16283 else if (storage
== ridpointers
[(int) RID_STATIC
])
16287 error ("storage class %qD applied to template instantiation",
16293 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
16295 /* DR 259 [temp.spec].
16297 Both an explicit instantiation and a declaration of an explicit
16298 specialization shall not appear in a program unless the explicit
16299 instantiation follows a declaration of the explicit specialization.
16301 For a given set of template parameters, if an explicit
16302 instantiation of a template appears after a declaration of an
16303 explicit specialization for that template, the explicit
16304 instantiation has no effect. */
16307 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
16311 No program shall explicitly instantiate any template more
16314 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
16315 instantiation was `extern'. If EXTERN_P then the second is.
16316 These cases are OK. */
16317 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
16319 if (!previous_instantiation_extern_p
&& !extern_p
16320 && (complain
& tf_error
))
16321 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
16323 /* If we've already instantiated the template, just return now. */
16324 if (!CLASSTYPE_INTERFACE_ONLY (t
))
16328 check_explicit_instantiation_namespace (TYPE_NAME (t
));
16329 mark_class_instantiated (t
, extern_p
);
16337 /* In contrast to implicit instantiation, where only the
16338 declarations, and not the definitions, of members are
16339 instantiated, we have here:
16343 The explicit instantiation of a class template specialization
16344 implies the instantiation of all of its members not
16345 previously explicitly specialized in the translation unit
16346 containing the explicit instantiation.
16348 Of course, we can't instantiate member template classes, since
16349 we don't have any arguments for them. Note that the standard
16350 is unclear on whether the instantiation of the members are
16351 *explicit* instantiations or not. However, the most natural
16352 interpretation is that it should be an explicit instantiation. */
16355 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
16356 if (TREE_CODE (tmp
) == FUNCTION_DECL
16357 && DECL_TEMPLATE_INSTANTIATION (tmp
))
16358 instantiate_class_member (tmp
, extern_p
);
16360 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
16361 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
16362 instantiate_class_member (tmp
, extern_p
);
16364 if (CLASSTYPE_NESTED_UTDS (t
))
16365 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
16366 bt_instantiate_type_proc
, &storage
);
16370 /* Given a function DECL, which is a specialization of TMPL, modify
16371 DECL to be a re-instantiation of TMPL with the same template
16372 arguments. TMPL should be the template into which tsubst'ing
16373 should occur for DECL, not the most general template.
16375 One reason for doing this is a scenario like this:
16378 void f(const T&, int i);
16380 void g() { f(3, 7); }
16383 void f(const T& t, const int i) { }
16385 Note that when the template is first instantiated, with
16386 instantiate_template, the resulting DECL will have no name for the
16387 first parameter, and the wrong type for the second. So, when we go
16388 to instantiate the DECL, we regenerate it. */
16391 regenerate_decl_from_template (tree decl
, tree tmpl
)
16393 /* The arguments used to instantiate DECL, from the most general
16398 args
= DECL_TI_ARGS (decl
);
16399 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
16401 /* Make sure that we can see identifiers, and compute access
16403 push_access_scope (decl
);
16405 if (TREE_CODE (decl
) == FUNCTION_DECL
)
16413 args_depth
= TMPL_ARGS_DEPTH (args
);
16414 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
16415 if (args_depth
> parms_depth
)
16416 args
= get_innermost_template_args (args
, parms_depth
);
16418 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
16419 args
, tf_error
, NULL_TREE
);
16421 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
16424 /* Merge parameter declarations. */
16425 decl_parm
= skip_artificial_parms_for (decl
,
16426 DECL_ARGUMENTS (decl
));
16428 = skip_artificial_parms_for (code_pattern
,
16429 DECL_ARGUMENTS (code_pattern
));
16430 while (decl_parm
&& !FUNCTION_PARAMETER_PACK_P (pattern_parm
))
16435 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
16436 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
16437 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
16439 parm_type
= type_decays_to (parm_type
);
16440 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
16441 TREE_TYPE (decl_parm
) = parm_type
;
16442 attributes
= DECL_ATTRIBUTES (pattern_parm
);
16443 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
16445 DECL_ATTRIBUTES (decl_parm
) = attributes
;
16446 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
16448 decl_parm
= TREE_CHAIN (decl_parm
);
16449 pattern_parm
= TREE_CHAIN (pattern_parm
);
16451 /* Merge any parameters that match with the function parameter
16453 if (pattern_parm
&& FUNCTION_PARAMETER_PACK_P (pattern_parm
))
16456 tree expanded_types
;
16457 /* Expand the TYPE_PACK_EXPANSION that provides the types for
16458 the parameters in this function parameter pack. */
16459 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
16460 args
, tf_error
, NULL_TREE
);
16461 len
= TREE_VEC_LENGTH (expanded_types
);
16462 for (i
= 0; i
< len
; i
++)
16467 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
16468 /* Rename the parameter to include the index. */
16469 DECL_NAME (decl_parm
) =
16470 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
16471 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
16472 parm_type
= type_decays_to (parm_type
);
16473 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
16474 TREE_TYPE (decl_parm
) = parm_type
;
16475 attributes
= DECL_ATTRIBUTES (pattern_parm
);
16476 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
16478 DECL_ATTRIBUTES (decl_parm
) = attributes
;
16479 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
16481 decl_parm
= TREE_CHAIN (decl_parm
);
16484 /* Merge additional specifiers from the CODE_PATTERN. */
16485 if (DECL_DECLARED_INLINE_P (code_pattern
)
16486 && !DECL_DECLARED_INLINE_P (decl
))
16487 DECL_DECLARED_INLINE_P (decl
) = 1;
16489 else if (TREE_CODE (decl
) == VAR_DECL
)
16491 DECL_INITIAL (decl
) =
16492 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
16493 tf_error
, DECL_TI_TEMPLATE (decl
),
16494 /*integral_constant_expression_p=*/false);
16495 if (VAR_HAD_UNKNOWN_BOUND (decl
))
16496 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
16497 tf_error
, DECL_TI_TEMPLATE (decl
));
16500 gcc_unreachable ();
16502 pop_access_scope (decl
);
16505 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
16506 substituted to get DECL. */
16509 template_for_substitution (tree decl
)
16511 tree tmpl
= DECL_TI_TEMPLATE (decl
);
16513 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
16514 for the instantiation. This is not always the most general
16515 template. Consider, for example:
16518 struct S { template <class U> void f();
16519 template <> void f<int>(); };
16521 and an instantiation of S<double>::f<int>. We want TD to be the
16522 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
16523 while (/* An instantiation cannot have a definition, so we need a
16524 more general template. */
16525 DECL_TEMPLATE_INSTANTIATION (tmpl
)
16526 /* We must also deal with friend templates. Given:
16528 template <class T> struct S {
16529 template <class U> friend void f() {};
16532 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
16533 so far as the language is concerned, but that's still
16534 where we get the pattern for the instantiation from. On
16535 other hand, if the definition comes outside the class, say:
16537 template <class T> struct S {
16538 template <class U> friend void f();
16540 template <class U> friend void f() {}
16542 we don't need to look any further. That's what the check for
16543 DECL_INITIAL is for. */
16544 || (TREE_CODE (decl
) == FUNCTION_DECL
16545 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
16546 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
16548 /* The present template, TD, should not be a definition. If it
16549 were a definition, we should be using it! Note that we
16550 cannot restructure the loop to just keep going until we find
16551 a template with a definition, since that might go too far if
16552 a specialization was declared, but not defined. */
16553 gcc_assert (TREE_CODE (decl
) != VAR_DECL
16554 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
16556 /* Fetch the more general template. */
16557 tmpl
= DECL_TI_TEMPLATE (tmpl
);
16563 /* Returns true if we need to instantiate this template instance even if we
16564 know we aren't going to emit it.. */
16567 always_instantiate_p (tree decl
)
16569 /* We always instantiate inline functions so that we can inline them. An
16570 explicit instantiation declaration prohibits implicit instantiation of
16571 non-inline functions. With high levels of optimization, we would
16572 normally inline non-inline functions -- but we're not allowed to do
16573 that for "extern template" functions. Therefore, we check
16574 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
16575 return ((TREE_CODE (decl
) == FUNCTION_DECL
16576 && DECL_DECLARED_INLINE_P (decl
))
16577 /* And we need to instantiate static data members so that
16578 their initializers are available in integral constant
16580 || (TREE_CODE (decl
) == VAR_DECL
16581 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
)));
16584 /* Produce the definition of D, a _DECL generated from a template. If
16585 DEFER_OK is nonzero, then we don't have to actually do the
16586 instantiation now; we just have to do it sometime. Normally it is
16587 an error if this is an explicit instantiation but D is undefined.
16588 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
16589 explicitly instantiated class template. */
16592 instantiate_decl (tree d
, int defer_ok
,
16593 bool expl_inst_class_mem_p
)
16595 tree tmpl
= DECL_TI_TEMPLATE (d
);
16602 bool pattern_defined
;
16604 location_t saved_loc
= input_location
;
16607 /* This function should only be used to instantiate templates for
16608 functions and static member variables. */
16609 gcc_assert (TREE_CODE (d
) == FUNCTION_DECL
16610 || TREE_CODE (d
) == VAR_DECL
);
16612 /* Variables are never deferred; if instantiation is required, they
16613 are instantiated right away. That allows for better code in the
16614 case that an expression refers to the value of the variable --
16615 if the variable has a constant value the referring expression can
16616 take advantage of that fact. */
16617 if (TREE_CODE (d
) == VAR_DECL
)
16620 /* Don't instantiate cloned functions. Instead, instantiate the
16621 functions they cloned. */
16622 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
16623 d
= DECL_CLONED_FUNCTION (d
);
16625 if (DECL_TEMPLATE_INSTANTIATED (d
)
16626 || DECL_TEMPLATE_SPECIALIZATION (d
))
16627 /* D has already been instantiated or explicitly specialized, so
16628 there's nothing for us to do here.
16630 It might seem reasonable to check whether or not D is an explicit
16631 instantiation, and, if so, stop here. But when an explicit
16632 instantiation is deferred until the end of the compilation,
16633 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
16634 the instantiation. */
16637 /* Check to see whether we know that this template will be
16638 instantiated in some other file, as with "extern template"
16640 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
16642 /* In general, we do not instantiate such templates. */
16643 if (external_p
&& !always_instantiate_p (d
))
16646 gen_tmpl
= most_general_template (tmpl
);
16647 gen_args
= DECL_TI_ARGS (d
);
16649 if (tmpl
!= gen_tmpl
)
16650 /* We should already have the extra args. */
16651 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
16652 == TMPL_ARGS_DEPTH (gen_args
));
16653 /* And what's in the hash table should match D. */
16654 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
16655 || spec
== NULL_TREE
);
16657 /* This needs to happen before any tsubsting. */
16658 if (! push_tinst_level (d
))
16661 timevar_push (TV_PARSE
);
16663 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
16664 for the instantiation. */
16665 td
= template_for_substitution (d
);
16666 code_pattern
= DECL_TEMPLATE_RESULT (td
);
16668 /* We should never be trying to instantiate a member of a class
16669 template or partial specialization. */
16670 gcc_assert (d
!= code_pattern
);
16672 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
16673 || DECL_TEMPLATE_SPECIALIZATION (td
))
16674 /* In the case of a friend template whose definition is provided
16675 outside the class, we may have too many arguments. Drop the
16676 ones we don't need. The same is true for specializations. */
16677 args
= get_innermost_template_args
16678 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
16682 if (TREE_CODE (d
) == FUNCTION_DECL
)
16683 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
);
16685 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
16687 /* We may be in the middle of deferred access check. Disable it now. */
16688 push_deferring_access_checks (dk_no_deferred
);
16690 /* Unless an explicit instantiation directive has already determined
16691 the linkage of D, remember that a definition is available for
16693 if (pattern_defined
16694 && !DECL_INTERFACE_KNOWN (d
)
16695 && !DECL_NOT_REALLY_EXTERN (d
))
16696 mark_definable (d
);
16698 input_location
= DECL_SOURCE_LOCATION (d
);
16700 /* If D is a member of an explicitly instantiated class template,
16701 and no definition is available, treat it like an implicit
16703 if (!pattern_defined
&& expl_inst_class_mem_p
16704 && DECL_EXPLICIT_INSTANTIATION (d
))
16706 DECL_NOT_REALLY_EXTERN (d
) = 0;
16707 DECL_INTERFACE_KNOWN (d
) = 0;
16708 SET_DECL_IMPLICIT_INSTANTIATION (d
);
16711 /* Recheck the substitutions to obtain any warning messages
16712 about ignoring cv qualifiers. Don't do this for artificial decls,
16713 as it breaks the context-sensitive substitution for lambda op(). */
16714 if (!defer_ok
&& !DECL_ARTIFICIAL (d
))
16716 tree gen
= DECL_TEMPLATE_RESULT (gen_tmpl
);
16717 tree type
= TREE_TYPE (gen
);
16719 /* Make sure that we can see identifiers, and compute access
16720 correctly. D is already the target FUNCTION_DECL with the
16722 push_access_scope (d
);
16724 if (TREE_CODE (gen
) == FUNCTION_DECL
)
16726 tsubst (DECL_ARGUMENTS (gen
), gen_args
, tf_warning_or_error
, d
);
16727 tsubst_exception_specification (type
, gen_args
, tf_warning_or_error
,
16729 /* Don't simply tsubst the function type, as that will give
16730 duplicate warnings about poor parameter qualifications.
16731 The function arguments are the same as the decl_arguments
16732 without the top level cv qualifiers. */
16733 type
= TREE_TYPE (type
);
16735 tsubst (type
, gen_args
, tf_warning_or_error
, d
);
16737 pop_access_scope (d
);
16740 /* Defer all other templates, unless we have been explicitly
16741 forbidden from doing so. */
16742 if (/* If there is no definition, we cannot instantiate the
16745 /* If it's OK to postpone instantiation, do so. */
16747 /* If this is a static data member that will be defined
16748 elsewhere, we don't want to instantiate the entire data
16749 member, but we do want to instantiate the initializer so that
16750 we can substitute that elsewhere. */
16751 || (external_p
&& TREE_CODE (d
) == VAR_DECL
))
16753 /* The definition of the static data member is now required so
16754 we must substitute the initializer. */
16755 if (TREE_CODE (d
) == VAR_DECL
16756 && !DECL_INITIAL (d
)
16757 && DECL_INITIAL (code_pattern
))
16762 ns
= decl_namespace_context (d
);
16763 push_nested_namespace (ns
);
16764 push_nested_class (DECL_CONTEXT (d
));
16765 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
16767 tf_warning_or_error
, NULL_TREE
,
16768 /*integral_constant_expression_p=*/false);
16769 cp_finish_decl (d
, init
, /*init_const_expr_p=*/false,
16770 /*asmspec_tree=*/NULL_TREE
,
16771 LOOKUP_ONLYCONVERTING
);
16772 pop_nested_class ();
16773 pop_nested_namespace (ns
);
16776 /* We restore the source position here because it's used by
16777 add_pending_template. */
16778 input_location
= saved_loc
;
16780 if (at_eof
&& !pattern_defined
16781 && DECL_EXPLICIT_INSTANTIATION (d
)
16782 && DECL_NOT_REALLY_EXTERN (d
))
16785 The definition of a non-exported function template, a
16786 non-exported member function template, or a non-exported
16787 member function or static data member of a class template
16788 shall be present in every translation unit in which it is
16789 explicitly instantiated. */
16790 permerror (input_location
, "explicit instantiation of %qD "
16791 "but no definition available", d
);
16793 /* ??? Historically, we have instantiated inline functions, even
16794 when marked as "extern template". */
16795 if (!(external_p
&& TREE_CODE (d
) == VAR_DECL
))
16796 add_pending_template (d
);
16799 /* Tell the repository that D is available in this translation unit
16800 -- and see if it is supposed to be instantiated here. */
16801 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
16803 /* In a PCH file, despite the fact that the repository hasn't
16804 requested instantiation in the PCH it is still possible that
16805 an instantiation will be required in a file that includes the
16808 add_pending_template (d
);
16809 /* Instantiate inline functions so that the inliner can do its
16810 job, even though we'll not be emitting a copy of this
16812 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
16816 need_push
= !cfun
|| !global_bindings_p ();
16818 push_to_top_level ();
16820 /* Mark D as instantiated so that recursive calls to
16821 instantiate_decl do not try to instantiate it again. */
16822 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
16824 /* Regenerate the declaration in case the template has been modified
16825 by a subsequent redeclaration. */
16826 regenerate_decl_from_template (d
, td
);
16828 /* We already set the file and line above. Reset them now in case
16829 they changed as a result of calling regenerate_decl_from_template. */
16830 input_location
= DECL_SOURCE_LOCATION (d
);
16832 if (TREE_CODE (d
) == VAR_DECL
)
16836 /* Clear out DECL_RTL; whatever was there before may not be right
16837 since we've reset the type of the declaration. */
16838 SET_DECL_RTL (d
, NULL
);
16839 DECL_IN_AGGR_P (d
) = 0;
16841 /* The initializer is placed in DECL_INITIAL by
16842 regenerate_decl_from_template. Pull it out so that
16843 cp_finish_decl can process it. */
16844 init
= DECL_INITIAL (d
);
16845 DECL_INITIAL (d
) = NULL_TREE
;
16846 DECL_INITIALIZED_P (d
) = 0;
16848 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
16849 initializer. That function will defer actual emission until
16850 we have a chance to determine linkage. */
16851 DECL_EXTERNAL (d
) = 0;
16853 /* Enter the scope of D so that access-checking works correctly. */
16854 push_nested_class (DECL_CONTEXT (d
));
16855 cp_finish_decl (d
, init
, false, NULL_TREE
, 0);
16856 pop_nested_class ();
16858 else if (TREE_CODE (d
) == FUNCTION_DECL
)
16860 htab_t saved_local_specializations
;
16865 /* Save away the current list, in case we are instantiating one
16866 template from within the body of another. */
16867 saved_local_specializations
= local_specializations
;
16869 /* Set up the list of local specializations. */
16870 local_specializations
= htab_create (37,
16871 hash_local_specialization
,
16872 eq_local_specializations
,
16875 /* Set up context. */
16876 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
16878 /* Create substitution entries for the parameters. */
16879 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
16880 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
16881 spec_parm
= DECL_ARGUMENTS (d
);
16882 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
16884 register_local_specialization (spec_parm
, tmpl_parm
);
16885 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
16886 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
16888 while (tmpl_parm
&& !FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
16890 register_local_specialization (spec_parm
, tmpl_parm
);
16891 tmpl_parm
= TREE_CHAIN (tmpl_parm
);
16892 spec_parm
= TREE_CHAIN (spec_parm
);
16894 if (tmpl_parm
&& FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
16896 /* Register the (value) argument pack as a specialization of
16897 TMPL_PARM, then move on. */
16898 tree argpack
= make_fnparm_pack (spec_parm
);
16899 register_local_specialization (argpack
, tmpl_parm
);
16900 tmpl_parm
= TREE_CHAIN (tmpl_parm
);
16901 spec_parm
= NULL_TREE
;
16903 gcc_assert (!spec_parm
);
16905 /* Substitute into the body of the function. */
16906 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
16907 tf_warning_or_error
, tmpl
,
16908 /*integral_constant_expression_p=*/false);
16910 /* Set the current input_location to the end of the function
16911 so that finish_function knows where we are. */
16912 input_location
= DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
16914 /* We don't need the local specializations any more. */
16915 htab_delete (local_specializations
);
16916 local_specializations
= saved_local_specializations
;
16918 /* Finish the function. */
16919 d
= finish_function (0);
16920 expand_or_defer_fn (d
);
16923 /* We're not deferring instantiation any more. */
16924 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
16927 pop_from_top_level ();
16930 input_location
= saved_loc
;
16931 pop_deferring_access_checks ();
16932 pop_tinst_level ();
16934 timevar_pop (TV_PARSE
);
16939 /* Run through the list of templates that we wish we could
16940 instantiate, and instantiate any we can. RETRIES is the
16941 number of times we retry pending template instantiation. */
16944 instantiate_pending_templates (int retries
)
16947 location_t saved_loc
= input_location
;
16949 /* Instantiating templates may trigger vtable generation. This in turn
16950 may require further template instantiations. We place a limit here
16951 to avoid infinite loop. */
16952 if (pending_templates
&& retries
>= max_tinst_depth
)
16954 tree decl
= pending_templates
->tinst
->decl
;
16956 error ("template instantiation depth exceeds maximum of %d"
16957 " instantiating %q+D, possibly from virtual table generation"
16958 " (use -ftemplate-depth= to increase the maximum)",
16959 max_tinst_depth
, decl
);
16960 if (TREE_CODE (decl
) == FUNCTION_DECL
)
16961 /* Pretend that we defined it. */
16962 DECL_INITIAL (decl
) = error_mark_node
;
16968 struct pending_template
**t
= &pending_templates
;
16969 struct pending_template
*last
= NULL
;
16973 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
16974 bool complete
= false;
16976 if (TYPE_P (instantiation
))
16980 if (!COMPLETE_TYPE_P (instantiation
))
16982 instantiate_class_template (instantiation
);
16983 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
16984 for (fn
= TYPE_METHODS (instantiation
);
16986 fn
= TREE_CHAIN (fn
))
16987 if (! DECL_ARTIFICIAL (fn
))
16988 instantiate_decl (fn
,
16990 /*expl_inst_class_mem_p=*/false);
16991 if (COMPLETE_TYPE_P (instantiation
))
16995 complete
= COMPLETE_TYPE_P (instantiation
);
16999 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
17000 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
17003 = instantiate_decl (instantiation
,
17005 /*expl_inst_class_mem_p=*/false);
17006 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
17010 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
17011 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
17015 /* If INSTANTIATION has been instantiated, then we don't
17016 need to consider it again in the future. */
17024 current_tinst_level
= NULL
;
17026 last_pending_template
= last
;
17028 while (reconsider
);
17030 input_location
= saved_loc
;
17033 /* Substitute ARGVEC into T, which is a list of initializers for
17034 either base class or a non-static data member. The TREE_PURPOSEs
17035 are DECLs, and the TREE_VALUEs are the initializer values. Used by
17036 instantiate_decl. */
17039 tsubst_initializer_list (tree t
, tree argvec
)
17041 tree inits
= NULL_TREE
;
17043 for (; t
; t
= TREE_CHAIN (t
))
17047 tree expanded_bases
= NULL_TREE
;
17048 tree expanded_arguments
= NULL_TREE
;
17051 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
17056 /* Expand the base class expansion type into separate base
17058 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
17059 tf_warning_or_error
,
17061 if (expanded_bases
== error_mark_node
)
17064 /* We'll be building separate TREE_LISTs of arguments for
17066 len
= TREE_VEC_LENGTH (expanded_bases
);
17067 expanded_arguments
= make_tree_vec (len
);
17068 for (i
= 0; i
< len
; i
++)
17069 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
17071 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
17072 expand each argument in the TREE_VALUE of t. */
17073 expr
= make_node (EXPR_PACK_EXPANSION
);
17074 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
17075 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
17077 if (TREE_VALUE (t
) == void_type_node
)
17078 /* VOID_TYPE_NODE is used to indicate
17079 value-initialization. */
17081 for (i
= 0; i
< len
; i
++)
17082 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
17086 /* Substitute parameter packs into each argument in the
17088 in_base_initializer
= 1;
17089 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
17091 tree expanded_exprs
;
17093 /* Expand the argument. */
17094 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
17096 = tsubst_pack_expansion (expr
, argvec
,
17097 tf_warning_or_error
,
17099 if (expanded_exprs
== error_mark_node
)
17102 /* Prepend each of the expanded expressions to the
17103 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
17104 for (i
= 0; i
< len
; i
++)
17106 TREE_VEC_ELT (expanded_arguments
, i
) =
17107 tree_cons (NULL_TREE
,
17108 TREE_VEC_ELT (expanded_exprs
, i
),
17109 TREE_VEC_ELT (expanded_arguments
, i
));
17112 in_base_initializer
= 0;
17114 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
17115 since we built them backwards. */
17116 for (i
= 0; i
< len
; i
++)
17118 TREE_VEC_ELT (expanded_arguments
, i
) =
17119 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
17124 for (i
= 0; i
< len
; ++i
)
17126 if (expanded_bases
)
17128 decl
= TREE_VEC_ELT (expanded_bases
, i
);
17129 decl
= expand_member_init (decl
);
17130 init
= TREE_VEC_ELT (expanded_arguments
, i
);
17134 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
17135 tf_warning_or_error
, NULL_TREE
);
17137 decl
= expand_member_init (decl
);
17138 if (decl
&& !DECL_P (decl
))
17139 in_base_initializer
= 1;
17141 init
= tsubst_expr (TREE_VALUE (t
), argvec
,
17142 tf_warning_or_error
, NULL_TREE
,
17143 /*integral_constant_expression_p=*/false);
17144 in_base_initializer
= 0;
17149 init
= build_tree_list (decl
, init
);
17150 TREE_CHAIN (init
) = inits
;
17158 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
17161 set_current_access_from_decl (tree decl
)
17163 if (TREE_PRIVATE (decl
))
17164 current_access_specifier
= access_private_node
;
17165 else if (TREE_PROTECTED (decl
))
17166 current_access_specifier
= access_protected_node
;
17168 current_access_specifier
= access_public_node
;
17171 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
17172 is the instantiation (which should have been created with
17173 start_enum) and ARGS are the template arguments to use. */
17176 tsubst_enum (tree tag
, tree newtag
, tree args
)
17180 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
17185 decl
= TREE_VALUE (e
);
17186 /* Note that in a template enum, the TREE_VALUE is the
17187 CONST_DECL, not the corresponding INTEGER_CST. */
17188 value
= tsubst_expr (DECL_INITIAL (decl
),
17189 args
, tf_warning_or_error
, NULL_TREE
,
17190 /*integral_constant_expression_p=*/true);
17192 /* Give this enumeration constant the correct access. */
17193 set_current_access_from_decl (decl
);
17195 /* Actually build the enumerator itself. */
17196 build_enumerator (DECL_NAME (decl
), value
, newtag
);
17199 finish_enum (newtag
);
17200 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
17201 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
17204 /* DECL is a FUNCTION_DECL that is a template specialization. Return
17205 its type -- but without substituting the innermost set of template
17206 arguments. So, innermost set of template parameters will appear in
17210 get_mostly_instantiated_function_type (tree decl
)
17218 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
17219 targs
= DECL_TI_ARGS (decl
);
17220 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
17221 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
17223 /* There should be as many levels of arguments as there are levels
17225 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
17227 fn_type
= TREE_TYPE (tmpl
);
17229 if (parm_depth
== 1)
17230 /* No substitution is necessary. */
17234 int i
, save_access_control
;
17237 /* Replace the innermost level of the TARGS with NULL_TREEs to
17238 let tsubst know not to substitute for those parameters. */
17239 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
17240 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
17241 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
17242 TMPL_ARGS_LEVEL (targs
, i
));
17243 SET_TMPL_ARGS_LEVEL (partial_args
,
17244 TMPL_ARGS_DEPTH (targs
),
17245 make_tree_vec (DECL_NTPARMS (tmpl
)));
17247 /* Disable access control as this function is used only during
17249 save_access_control
= flag_access_control
;
17250 flag_access_control
= 0;
17252 ++processing_template_decl
;
17253 /* Now, do the (partial) substitution to figure out the
17254 appropriate function type. */
17255 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
17256 --processing_template_decl
;
17258 /* Substitute into the template parameters to obtain the real
17259 innermost set of parameters. This step is important if the
17260 innermost set of template parameters contains value
17261 parameters whose types depend on outer template parameters. */
17262 TREE_VEC_LENGTH (partial_args
)--;
17263 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
17265 flag_access_control
= save_access_control
;
17271 /* Return truthvalue if we're processing a template different from
17272 the last one involved in diagnostics. */
17274 problematic_instantiation_changed (void)
17276 return last_template_error_tick
!= tinst_level_tick
;
17279 /* Remember current template involved in diagnostics. */
17281 record_last_problematic_instantiation (void)
17283 last_template_error_tick
= tinst_level_tick
;
17286 struct tinst_level
*
17287 current_instantiation (void)
17289 return current_tinst_level
;
17292 /* [temp.param] Check that template non-type parm TYPE is of an allowable
17293 type. Return zero for ok, nonzero for disallowed. Issue error and
17294 warning messages under control of COMPLAIN. */
17297 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
17299 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
17301 else if (POINTER_TYPE_P (type
))
17303 else if (TYPE_PTR_TO_MEMBER_P (type
))
17305 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
17307 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
17310 if (complain
& tf_error
)
17311 error ("%q#T is not a valid type for a template constant parameter", type
);
17315 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
17316 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
17319 dependent_type_p_r (tree type
)
17325 A type is dependent if it is:
17327 -- a template parameter. Template template parameters are types
17328 for us (since TYPE_P holds true for them) so we handle
17330 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
17331 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
17333 /* -- a qualified-id with a nested-name-specifier which contains a
17334 class-name that names a dependent type or whose unqualified-id
17335 names a dependent type. */
17336 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17338 /* -- a cv-qualified type where the cv-unqualified type is
17340 type
= TYPE_MAIN_VARIANT (type
);
17341 /* -- a compound type constructed from any dependent type. */
17342 if (TYPE_PTR_TO_MEMBER_P (type
))
17343 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
17344 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
17346 else if (TREE_CODE (type
) == POINTER_TYPE
17347 || TREE_CODE (type
) == REFERENCE_TYPE
)
17348 return dependent_type_p (TREE_TYPE (type
));
17349 else if (TREE_CODE (type
) == FUNCTION_TYPE
17350 || TREE_CODE (type
) == METHOD_TYPE
)
17354 if (dependent_type_p (TREE_TYPE (type
)))
17356 for (arg_type
= TYPE_ARG_TYPES (type
);
17358 arg_type
= TREE_CHAIN (arg_type
))
17359 if (dependent_type_p (TREE_VALUE (arg_type
)))
17363 /* -- an array type constructed from any dependent type or whose
17364 size is specified by a constant expression that is
17365 value-dependent. */
17366 if (TREE_CODE (type
) == ARRAY_TYPE
)
17368 if (TYPE_DOMAIN (type
)
17369 && dependent_type_p (TYPE_DOMAIN (type
)))
17371 return dependent_type_p (TREE_TYPE (type
));
17373 else if (TREE_CODE (type
) == INTEGER_TYPE
17374 && !TREE_CONSTANT (TYPE_MAX_VALUE (type
)))
17376 /* If this is the TYPE_DOMAIN of an array type, consider it
17377 dependent. We already checked for value-dependence in
17378 compute_array_index_type. */
17379 return type_dependent_expression_p (TYPE_MAX_VALUE (type
));
17382 /* -- a template-id in which either the template name is a template
17384 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17386 /* ... or any of the template arguments is a dependent type or
17387 an expression that is type-dependent or value-dependent. */
17388 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
17389 && (any_dependent_template_arguments_p
17390 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
17393 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
17394 argument of the `typeof' expression is not type-dependent, then
17395 it should already been have resolved. */
17396 if (TREE_CODE (type
) == TYPEOF_TYPE
17397 || TREE_CODE (type
) == DECLTYPE_TYPE
)
17400 /* A template argument pack is dependent if any of its packed
17402 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
17404 tree args
= ARGUMENT_PACK_ARGS (type
);
17405 int i
, len
= TREE_VEC_LENGTH (args
);
17406 for (i
= 0; i
< len
; ++i
)
17407 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
17411 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
17412 be template parameters. */
17413 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
17416 /* The standard does not specifically mention types that are local
17417 to template functions or local classes, but they should be
17418 considered dependent too. For example:
17420 template <int I> void f() {
17425 The size of `E' cannot be known until the value of `I' has been
17426 determined. Therefore, `E' must be considered dependent. */
17427 scope
= TYPE_CONTEXT (type
);
17428 if (scope
&& TYPE_P (scope
))
17429 return dependent_type_p (scope
);
17430 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
17431 return type_dependent_expression_p (scope
);
17433 /* Other types are non-dependent. */
17437 /* Returns TRUE if TYPE is dependent, in the sense of
17438 [temp.dep.type]. */
17441 dependent_type_p (tree type
)
17443 /* If there are no template parameters in scope, then there can't be
17444 any dependent types. */
17445 if (!processing_template_decl
)
17447 /* If we are not processing a template, then nobody should be
17448 providing us with a dependent type. */
17450 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
17454 /* If the type is NULL, we have not computed a type for the entity
17455 in question; in that case, the type is dependent. */
17459 /* Erroneous types can be considered non-dependent. */
17460 if (type
== error_mark_node
)
17463 /* If we have not already computed the appropriate value for TYPE,
17465 if (!TYPE_DEPENDENT_P_VALID (type
))
17467 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
17468 TYPE_DEPENDENT_P_VALID (type
) = 1;
17471 return TYPE_DEPENDENT_P (type
);
17474 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
17475 lookup. In other words, a dependent type that is not the current
17479 dependent_scope_p (tree scope
)
17481 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
17482 && !currently_open_class (scope
));
17485 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
17488 dependent_scope_ref_p (tree expression
, bool criterion (tree
))
17493 gcc_assert (TREE_CODE (expression
) == SCOPE_REF
);
17495 if (!TYPE_P (TREE_OPERAND (expression
, 0)))
17498 scope
= TREE_OPERAND (expression
, 0);
17499 name
= TREE_OPERAND (expression
, 1);
17503 An id-expression is type-dependent if it contains a
17504 nested-name-specifier that contains a class-name that names a
17506 /* The suggested resolution to Core Issue 224 implies that if the
17507 qualifying type is the current class, then we must peek
17510 && currently_open_class (scope
)
17511 && !criterion (name
))
17513 if (dependent_type_p (scope
))
17519 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
17520 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
17524 value_dependent_expression_p (tree expression
)
17526 if (!processing_template_decl
)
17529 /* A name declared with a dependent type. */
17530 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
17533 switch (TREE_CODE (expression
))
17535 case IDENTIFIER_NODE
:
17536 /* A name that has not been looked up -- must be dependent. */
17539 case TEMPLATE_PARM_INDEX
:
17540 /* A non-type template parm. */
17544 /* A non-type template parm. */
17545 if (DECL_TEMPLATE_PARM_P (expression
))
17547 return value_dependent_expression_p (DECL_INITIAL (expression
));
17550 /* A constant with integral or enumeration type and is initialized
17551 with an expression that is value-dependent. */
17552 if (DECL_INITIAL (expression
)
17553 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression
))
17554 && value_dependent_expression_p (DECL_INITIAL (expression
)))
17558 case DYNAMIC_CAST_EXPR
:
17559 case STATIC_CAST_EXPR
:
17560 case CONST_CAST_EXPR
:
17561 case REINTERPRET_CAST_EXPR
:
17563 /* These expressions are value-dependent if the type to which
17564 the cast occurs is dependent or the expression being casted
17565 is value-dependent. */
17567 tree type
= TREE_TYPE (expression
);
17569 if (dependent_type_p (type
))
17572 /* A functional cast has a list of operands. */
17573 expression
= TREE_OPERAND (expression
, 0);
17576 /* If there are no operands, it must be an expression such
17577 as "int()". This should not happen for aggregate types
17578 because it would form non-constant expressions. */
17579 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
17584 if (TREE_CODE (expression
) == TREE_LIST
)
17585 return any_value_dependent_elements_p (expression
);
17587 return value_dependent_expression_p (expression
);
17592 /* A `sizeof' expression is value-dependent if the operand is
17593 type-dependent or is a pack expansion. */
17594 expression
= TREE_OPERAND (expression
, 0);
17595 if (PACK_EXPANSION_P (expression
))
17597 else if (TYPE_P (expression
))
17598 return dependent_type_p (expression
);
17599 return type_dependent_expression_p (expression
);
17601 case NOEXCEPT_EXPR
:
17602 expression
= TREE_OPERAND (expression
, 0);
17603 /* FIXME why check value-dependency? */
17604 return (type_dependent_expression_p (expression
)
17605 || value_dependent_expression_p (expression
));
17608 return dependent_scope_ref_p (expression
, value_dependent_expression_p
);
17610 case COMPONENT_REF
:
17611 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
17612 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
17615 /* A CALL_EXPR may appear in a constant expression if it is a
17616 call to a builtin function, e.g., __builtin_constant_p. All
17617 such calls are value-dependent. */
17620 case NONTYPE_ARGUMENT_PACK
:
17621 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
17622 is value-dependent. */
17624 tree values
= ARGUMENT_PACK_ARGS (expression
);
17625 int i
, len
= TREE_VEC_LENGTH (values
);
17627 for (i
= 0; i
< len
; ++i
)
17628 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
17636 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
17637 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
17638 || (type2
? dependent_type_p (type2
) : false));
17642 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
17643 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
17646 /* A constant expression is value-dependent if any subexpression is
17647 value-dependent. */
17648 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
17650 case tcc_reference
:
17652 return (value_dependent_expression_p
17653 (TREE_OPERAND (expression
, 0)));
17655 case tcc_comparison
:
17657 return ((value_dependent_expression_p
17658 (TREE_OPERAND (expression
, 0)))
17659 || (value_dependent_expression_p
17660 (TREE_OPERAND (expression
, 1))));
17662 case tcc_expression
:
17666 for (i
= 0; i
< TREE_OPERAND_LENGTH (expression
); ++i
)
17667 /* In some cases, some of the operands may be missing.
17668 (For example, in the case of PREDECREMENT_EXPR, the
17669 amount to increment by may be missing.) That doesn't
17670 make the expression dependent. */
17671 if (TREE_OPERAND (expression
, i
)
17672 && (value_dependent_expression_p
17673 (TREE_OPERAND (expression
, i
))))
17683 /* The expression is not value-dependent. */
17687 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
17688 [temp.dep.expr]. */
17691 type_dependent_expression_p (tree expression
)
17693 if (!processing_template_decl
)
17696 if (expression
== error_mark_node
)
17699 /* An unresolved name is always dependent. */
17700 if (TREE_CODE (expression
) == IDENTIFIER_NODE
17701 || TREE_CODE (expression
) == USING_DECL
)
17704 /* Some expression forms are never type-dependent. */
17705 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
17706 || TREE_CODE (expression
) == SIZEOF_EXPR
17707 || TREE_CODE (expression
) == ALIGNOF_EXPR
17708 || TREE_CODE (expression
) == NOEXCEPT_EXPR
17709 || TREE_CODE (expression
) == TRAIT_EXPR
17710 || TREE_CODE (expression
) == TYPEID_EXPR
17711 || TREE_CODE (expression
) == DELETE_EXPR
17712 || TREE_CODE (expression
) == VEC_DELETE_EXPR
17713 || TREE_CODE (expression
) == THROW_EXPR
)
17716 /* The types of these expressions depends only on the type to which
17717 the cast occurs. */
17718 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
17719 || TREE_CODE (expression
) == STATIC_CAST_EXPR
17720 || TREE_CODE (expression
) == CONST_CAST_EXPR
17721 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
17722 || TREE_CODE (expression
) == CAST_EXPR
)
17723 return dependent_type_p (TREE_TYPE (expression
));
17725 /* The types of these expressions depends only on the type created
17726 by the expression. */
17727 if (TREE_CODE (expression
) == NEW_EXPR
17728 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
17730 /* For NEW_EXPR tree nodes created inside a template, either
17731 the object type itself or a TREE_LIST may appear as the
17733 tree type
= TREE_OPERAND (expression
, 1);
17734 if (TREE_CODE (type
) == TREE_LIST
)
17735 /* This is an array type. We need to check array dimensions
17737 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
17738 || value_dependent_expression_p
17739 (TREE_OPERAND (TREE_VALUE (type
), 1));
17741 return dependent_type_p (type
);
17744 if (TREE_CODE (expression
) == SCOPE_REF
17745 && dependent_scope_ref_p (expression
,
17746 type_dependent_expression_p
))
17749 if (TREE_CODE (expression
) == FUNCTION_DECL
17750 && DECL_LANG_SPECIFIC (expression
)
17751 && DECL_TEMPLATE_INFO (expression
)
17752 && (any_dependent_template_arguments_p
17753 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
17756 if (TREE_CODE (expression
) == TEMPLATE_DECL
17757 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
17760 if (TREE_CODE (expression
) == STMT_EXPR
)
17761 expression
= stmt_expr_value_expr (expression
);
17763 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
17768 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
17770 if (type_dependent_expression_p (elt
))
17776 /* A static data member of the current instantiation with incomplete
17777 array type is type-dependent, as the definition and specializations
17778 can have different bounds. */
17779 if (TREE_CODE (expression
) == VAR_DECL
17780 && DECL_CLASS_SCOPE_P (expression
)
17781 && dependent_type_p (DECL_CONTEXT (expression
))
17782 && VAR_HAD_UNKNOWN_BOUND (expression
))
17785 if (TREE_TYPE (expression
) == unknown_type_node
)
17787 if (TREE_CODE (expression
) == ADDR_EXPR
)
17788 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
17789 if (TREE_CODE (expression
) == COMPONENT_REF
17790 || TREE_CODE (expression
) == OFFSET_REF
)
17792 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
17794 expression
= TREE_OPERAND (expression
, 1);
17795 if (TREE_CODE (expression
) == IDENTIFIER_NODE
)
17798 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
17799 if (TREE_CODE (expression
) == SCOPE_REF
)
17802 if (TREE_CODE (expression
) == BASELINK
)
17803 expression
= BASELINK_FUNCTIONS (expression
);
17805 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
17807 if (any_dependent_template_arguments_p
17808 (TREE_OPERAND (expression
, 1)))
17810 expression
= TREE_OPERAND (expression
, 0);
17812 gcc_assert (TREE_CODE (expression
) == OVERLOAD
17813 || TREE_CODE (expression
) == FUNCTION_DECL
);
17817 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
17819 expression
= OVL_NEXT (expression
);
17824 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
17826 return (dependent_type_p (TREE_TYPE (expression
)));
17829 /* Like type_dependent_expression_p, but it also works while not processing
17830 a template definition, i.e. during substitution or mangling. */
17833 type_dependent_expression_p_push (tree expr
)
17836 ++processing_template_decl
;
17837 b
= type_dependent_expression_p (expr
);
17838 --processing_template_decl
;
17842 /* Returns TRUE if ARGS contains a type-dependent expression. */
17845 any_type_dependent_arguments_p (const VEC(tree
,gc
) *args
)
17850 for (i
= 0; VEC_iterate (tree
, args
, i
, arg
); ++i
)
17852 if (type_dependent_expression_p (arg
))
17858 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
17859 expressions) contains any value-dependent expressions. */
17862 any_value_dependent_elements_p (const_tree list
)
17864 for (; list
; list
= TREE_CHAIN (list
))
17865 if (value_dependent_expression_p (TREE_VALUE (list
)))
17871 /* Returns TRUE if the ARG (a template argument) is dependent. */
17874 dependent_template_arg_p (tree arg
)
17876 if (!processing_template_decl
)
17879 if (TREE_CODE (arg
) == TEMPLATE_DECL
17880 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
17881 return dependent_template_p (arg
);
17882 else if (ARGUMENT_PACK_P (arg
))
17884 tree args
= ARGUMENT_PACK_ARGS (arg
);
17885 int i
, len
= TREE_VEC_LENGTH (args
);
17886 for (i
= 0; i
< len
; ++i
)
17888 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
17894 else if (TYPE_P (arg
))
17895 return dependent_type_p (arg
);
17897 return (type_dependent_expression_p (arg
)
17898 || value_dependent_expression_p (arg
));
17901 /* Returns true if ARGS (a collection of template arguments) contains
17902 any types that require structural equality testing. */
17905 any_template_arguments_need_structural_equality_p (tree args
)
17912 if (args
== error_mark_node
)
17915 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
17917 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
17918 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
17920 tree arg
= TREE_VEC_ELT (level
, j
);
17921 tree packed_args
= NULL_TREE
;
17924 if (ARGUMENT_PACK_P (arg
))
17926 /* Look inside the argument pack. */
17927 packed_args
= ARGUMENT_PACK_ARGS (arg
);
17928 len
= TREE_VEC_LENGTH (packed_args
);
17931 for (k
= 0; k
< len
; ++k
)
17934 arg
= TREE_VEC_ELT (packed_args
, k
);
17936 if (error_operand_p (arg
))
17938 else if (TREE_CODE (arg
) == TEMPLATE_DECL
17939 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
17941 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
17943 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
17944 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
17953 /* Returns true if ARGS (a collection of template arguments) contains
17954 any dependent arguments. */
17957 any_dependent_template_arguments_p (const_tree args
)
17964 if (args
== error_mark_node
)
17967 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
17969 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
17970 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
17971 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
17978 /* Returns TRUE if the template TMPL is dependent. */
17981 dependent_template_p (tree tmpl
)
17983 if (TREE_CODE (tmpl
) == OVERLOAD
)
17987 if (dependent_template_p (OVL_FUNCTION (tmpl
)))
17989 tmpl
= OVL_CHAIN (tmpl
);
17994 /* Template template parameters are dependent. */
17995 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
17996 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
17998 /* So are names that have not been looked up. */
17999 if (TREE_CODE (tmpl
) == SCOPE_REF
18000 || TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
18002 /* So are member templates of dependent classes. */
18003 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
18004 return dependent_type_p (DECL_CONTEXT (tmpl
));
18008 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
18011 dependent_template_id_p (tree tmpl
, tree args
)
18013 return (dependent_template_p (tmpl
)
18014 || any_dependent_template_arguments_p (args
));
18017 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
18021 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
18025 if (!processing_template_decl
)
18028 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
18030 tree decl
= TREE_VEC_ELT (declv
, i
);
18031 tree init
= TREE_VEC_ELT (initv
, i
);
18032 tree cond
= TREE_VEC_ELT (condv
, i
);
18033 tree incr
= TREE_VEC_ELT (incrv
, i
);
18035 if (type_dependent_expression_p (decl
))
18038 if (init
&& type_dependent_expression_p (init
))
18041 if (type_dependent_expression_p (cond
))
18044 if (COMPARISON_CLASS_P (cond
)
18045 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
18046 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
18049 if (TREE_CODE (incr
) == MODOP_EXPR
)
18051 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
18052 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
18055 else if (type_dependent_expression_p (incr
))
18057 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
18059 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
18061 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
18063 tree t
= TREE_OPERAND (incr
, 1);
18064 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
18065 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
18074 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
18075 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
18076 no such TYPE can be found. Note that this function peers inside
18077 uninstantiated templates and therefore should be used only in
18078 extremely limited situations. ONLY_CURRENT_P restricts this
18079 peering to the currently open classes hierarchy (which is required
18080 when comparing types). */
18083 resolve_typename_type (tree type
, bool only_current_p
)
18092 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
18094 scope
= TYPE_CONTEXT (type
);
18095 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
18096 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
18097 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
18098 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
18099 identifier of the TYPENAME_TYPE anymore.
18100 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
18101 TYPENAME_TYPE instead, we avoid messing up with a possible
18102 typedef variant case. */
18103 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
18105 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
18106 it first before we can figure out what NAME refers to. */
18107 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
18108 scope
= resolve_typename_type (scope
, only_current_p
);
18109 /* If we don't know what SCOPE refers to, then we cannot resolve the
18111 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
18113 /* If the SCOPE is a template type parameter, we have no way of
18114 resolving the name. */
18115 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
18117 /* If the SCOPE is not the current instantiation, there's no reason
18118 to look inside it. */
18119 if (only_current_p
&& !currently_open_class (scope
))
18121 /* If this is a typedef, we don't want to look inside (c++/11987). */
18122 if (typedef_variant_p (type
))
18124 /* If SCOPE isn't the template itself, it will not have a valid
18125 TYPE_FIELDS list. */
18126 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
18127 /* scope is either the template itself or a compatible instantiation
18128 like X<T>, so look up the name in the original template. */
18129 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
18131 /* scope is a partial instantiation, so we can't do the lookup or we
18132 will lose the template arguments. */
18134 /* Enter the SCOPE so that name lookup will be resolved as if we
18135 were in the class definition. In particular, SCOPE will no
18136 longer be considered a dependent type. */
18137 pushed_scope
= push_scope (scope
);
18138 /* Look up the declaration. */
18139 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true);
18141 result
= NULL_TREE
;
18143 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
18144 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
18147 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == IDENTIFIER_NODE
18148 && TREE_CODE (decl
) == TYPE_DECL
)
18150 result
= TREE_TYPE (decl
);
18151 if (result
== error_mark_node
)
18152 result
= NULL_TREE
;
18154 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
18155 && DECL_CLASS_TEMPLATE_P (decl
))
18159 /* Obtain the template and the arguments. */
18160 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
18161 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
18162 /* Instantiate the template. */
18163 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
18164 /*entering_scope=*/0,
18165 tf_error
| tf_user
);
18166 if (result
== error_mark_node
)
18167 result
= NULL_TREE
;
18170 /* Leave the SCOPE. */
18172 pop_scope (pushed_scope
);
18174 /* If we failed to resolve it, return the original typename. */
18178 /* If lookup found a typename type, resolve that too. */
18179 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
18181 /* Ill-formed programs can cause infinite recursion here, so we
18182 must catch that. */
18183 TYPENAME_IS_RESOLVING_P (type
) = 1;
18184 result
= resolve_typename_type (result
, only_current_p
);
18185 TYPENAME_IS_RESOLVING_P (type
) = 0;
18188 /* Qualify the resulting type. */
18189 quals
= cp_type_quals (type
);
18191 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
18196 /* EXPR is an expression which is not type-dependent. Return a proxy
18197 for EXPR that can be used to compute the types of larger
18198 expressions containing EXPR. */
18201 build_non_dependent_expr (tree expr
)
18205 /* Preserve null pointer constants so that the type of things like
18206 "p == 0" where "p" is a pointer can be determined. */
18207 if (null_ptr_cst_p (expr
))
18209 /* Preserve OVERLOADs; the functions must be available to resolve
18212 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
18213 inner_expr
= stmt_expr_value_expr (inner_expr
);
18214 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
18215 inner_expr
= TREE_OPERAND (inner_expr
, 0);
18216 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
18217 inner_expr
= TREE_OPERAND (inner_expr
, 1);
18218 if (is_overloaded_fn (inner_expr
)
18219 || TREE_CODE (inner_expr
) == OFFSET_REF
)
18221 /* There is no need to return a proxy for a variable. */
18222 if (TREE_CODE (expr
) == VAR_DECL
)
18224 /* Preserve string constants; conversions from string constants to
18225 "char *" are allowed, even though normally a "const char *"
18226 cannot be used to initialize a "char *". */
18227 if (TREE_CODE (expr
) == STRING_CST
)
18229 /* Preserve arithmetic constants, as an optimization -- there is no
18230 reason to create a new node. */
18231 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
18233 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
18234 There is at least one place where we want to know that a
18235 particular expression is a throw-expression: when checking a ?:
18236 expression, there are special rules if the second or third
18237 argument is a throw-expression. */
18238 if (TREE_CODE (expr
) == THROW_EXPR
)
18241 if (TREE_CODE (expr
) == COND_EXPR
)
18242 return build3 (COND_EXPR
,
18244 TREE_OPERAND (expr
, 0),
18245 (TREE_OPERAND (expr
, 1)
18246 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
18247 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
18248 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
18249 if (TREE_CODE (expr
) == COMPOUND_EXPR
18250 && !COMPOUND_EXPR_OVERLOADED (expr
))
18251 return build2 (COMPOUND_EXPR
,
18253 TREE_OPERAND (expr
, 0),
18254 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
18256 /* If the type is unknown, it can't really be non-dependent */
18257 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
18259 /* Otherwise, build a NON_DEPENDENT_EXPR.
18261 REFERENCE_TYPEs are not stripped for expressions in templates
18262 because doing so would play havoc with mangling. Consider, for
18265 template <typename T> void f<T& g>() { g(); }
18267 In the body of "f", the expression for "g" will have
18268 REFERENCE_TYPE, even though the standard says that it should
18269 not. The reason is that we must preserve the syntactic form of
18270 the expression so that mangling (say) "f<g>" inside the body of
18271 "f" works out correctly. Therefore, the REFERENCE_TYPE is
18273 return build1 (NON_DEPENDENT_EXPR
, non_reference (TREE_TYPE (expr
)), expr
);
18276 /* ARGS is a vector of expressions as arguments to a function call.
18277 Replace the arguments with equivalent non-dependent expressions.
18278 This modifies ARGS in place. */
18281 make_args_non_dependent (VEC(tree
,gc
) *args
)
18286 for (ix
= 0; VEC_iterate (tree
, args
, ix
, arg
); ++ix
)
18288 tree newarg
= build_non_dependent_expr (arg
);
18290 VEC_replace (tree
, args
, ix
, newarg
);
18294 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
18295 with a level one deeper than the actual template parms. */
18300 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
18301 TYPE_NAME (au
) = build_decl (BUILTINS_LOCATION
,
18302 TYPE_DECL
, get_identifier ("auto"), au
);
18303 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
18304 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
18305 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
18306 TYPE_NAME (au
), NULL_TREE
);
18307 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
18308 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
18309 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
18314 /* Given type ARG, return std::initializer_list<ARG>. */
18319 tree std_init_list
= namespace_binding
18320 (get_identifier ("initializer_list"), std_node
);
18322 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
18324 error ("deducing from brace-enclosed initializer list requires "
18325 "#include <initializer_list>");
18326 return error_mark_node
;
18328 argvec
= make_tree_vec (1);
18329 TREE_VEC_ELT (argvec
, 0) = arg
;
18330 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
18331 NULL_TREE
, 0, tf_warning_or_error
);
18334 /* Replace auto in TYPE with std::initializer_list<auto>. */
18337 listify_autos (tree type
, tree auto_node
)
18339 tree init_auto
= listify (auto_node
);
18340 tree argvec
= make_tree_vec (1);
18341 TREE_VEC_ELT (argvec
, 0) = init_auto
;
18342 if (processing_template_decl
)
18343 argvec
= add_to_template_args (current_template_args (), argvec
);
18344 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
18347 /* walk_tree helper for do_auto_deduction. */
18350 contains_auto_r (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
18353 /* Is this a variable with the type we're looking for? */
18355 && TREE_TYPE (*tp
) == type
)
18361 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
18362 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
18365 do_auto_deduction (tree type
, tree init
, tree auto_node
)
18367 tree parms
, tparms
, targs
;
18372 /* The name of the object being declared shall not appear in the
18373 initializer expression. */
18374 decl
= cp_walk_tree_without_duplicates (&init
, contains_auto_r
, type
);
18377 error ("variable %q#D with %<auto%> type used in its own "
18378 "initializer", decl
);
18379 return error_mark_node
;
18382 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
18383 with either a new invented type template parameter U or, if the
18384 initializer is a braced-init-list (8.5.4), with
18385 std::initializer_list<U>. */
18386 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
18387 type
= listify_autos (type
, auto_node
);
18389 parms
= build_tree_list (NULL_TREE
, type
);
18391 tparms
= make_tree_vec (1);
18392 targs
= make_tree_vec (1);
18393 TREE_VEC_ELT (tparms
, 0)
18394 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
18395 val
= type_unification_real (tparms
, targs
, parms
, args
, 1, 0,
18396 DEDUCE_CALL
, LOOKUP_NORMAL
);
18399 error ("unable to deduce %qT from %qE", type
, init
);
18400 return error_mark_node
;
18403 /* If the list of declarators contains more than one declarator, the type
18404 of each declared variable is determined as described above. If the
18405 type deduced for the template parameter U is not the same in each
18406 deduction, the program is ill-formed. */
18407 if (TREE_TYPE (auto_node
)
18408 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
18410 error ("inconsistent deduction for %qT: %qT and then %qT",
18411 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
18412 return error_mark_node
;
18414 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
18416 if (processing_template_decl
)
18417 targs
= add_to_template_args (current_template_args (), targs
);
18418 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
18421 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
18425 splice_late_return_type (tree type
, tree late_return_type
)
18429 if (late_return_type
== NULL_TREE
)
18431 argvec
= make_tree_vec (1);
18432 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
18433 if (processing_template_decl
)
18434 argvec
= add_to_template_args (current_template_args (), argvec
);
18435 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
18438 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
18441 is_auto (const_tree type
)
18443 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
18444 && TYPE_IDENTIFIER (type
) == get_identifier ("auto"))
18450 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
18451 appear as a type-specifier for the declaration in question, we don't
18452 have to look through the whole type. */
18455 type_uses_auto (tree type
)
18457 enum tree_code code
;
18458 if (is_auto (type
))
18461 code
= TREE_CODE (type
);
18463 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
18464 || code
== OFFSET_TYPE
|| code
== FUNCTION_TYPE
18465 || code
== METHOD_TYPE
|| code
== ARRAY_TYPE
)
18466 return type_uses_auto (TREE_TYPE (type
));
18468 if (TYPE_PTRMEMFUNC_P (type
))
18469 return type_uses_auto (TREE_TYPE (TREE_TYPE
18470 (TYPE_PTRMEMFUNC_FN_TYPE (type
))));
18475 /* For a given template T, return the vector of typedefs referenced
18476 in T for which access check is needed at T instantiation time.
18477 T is either a FUNCTION_DECL or a RECORD_TYPE.
18478 Those typedefs were added to T by the function
18479 append_type_to_template_for_access_check. */
18481 VEC(qualified_typedef_usage_t
,gc
)*
18482 get_types_needing_access_check (tree t
)
18485 VEC(qualified_typedef_usage_t
,gc
) *result
= NULL
;
18487 if (!t
|| t
== error_mark_node
)
18490 if (!(ti
= get_template_info (t
)))
18493 if (CLASS_TYPE_P (t
)
18494 || TREE_CODE (t
) == FUNCTION_DECL
)
18496 if (!TI_TEMPLATE (ti
))
18499 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
18505 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
18506 tied to T. That list of typedefs will be access checked at
18507 T instantiation time.
18508 T is either a FUNCTION_DECL or a RECORD_TYPE.
18509 TYPE_DECL is a TYPE_DECL node representing a typedef.
18510 SCOPE is the scope through which TYPE_DECL is accessed.
18511 LOCATION is the location of the usage point of TYPE_DECL.
18513 This function is a subroutine of
18514 append_type_to_template_for_access_check. */
18517 append_type_to_template_for_access_check_1 (tree t
,
18520 location_t location
)
18522 qualified_typedef_usage_t typedef_usage
;
18525 if (!t
|| t
== error_mark_node
)
18528 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
18529 || CLASS_TYPE_P (t
))
18531 && TREE_CODE (type_decl
) == TYPE_DECL
18534 if (!(ti
= get_template_info (t
)))
18537 gcc_assert (TI_TEMPLATE (ti
));
18539 typedef_usage
.typedef_decl
= type_decl
;
18540 typedef_usage
.context
= scope
;
18541 typedef_usage
.locus
= location
;
18543 VEC_safe_push (qualified_typedef_usage_t
, gc
,
18544 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
),
18548 /* Append TYPE_DECL to the template TEMPL.
18549 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
18550 At TEMPL instanciation time, TYPE_DECL will be checked to see
18551 if it can be accessed through SCOPE.
18552 LOCATION is the location of the usage point of TYPE_DECL.
18554 e.g. consider the following code snippet:
18561 template<class U> struct S
18563 C::myint mi; // <-- usage point of the typedef C::myint
18568 At S<char> instantiation time, we need to check the access of C::myint
18569 In other words, we need to check the access of the myint typedef through
18570 the C scope. For that purpose, this function will add the myint typedef
18571 and the scope C through which its being accessed to a list of typedefs
18572 tied to the template S. That list will be walked at template instantiation
18573 time and access check performed on each typedefs it contains.
18574 Note that this particular code snippet should yield an error because
18575 myint is private to C. */
18578 append_type_to_template_for_access_check (tree templ
,
18581 location_t location
)
18583 qualified_typedef_usage_t
*iter
;
18586 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
18588 /* Make sure we don't append the type to the template twice. */
18590 VEC_iterate (qualified_typedef_usage_t
,
18591 get_types_needing_access_check (templ
),
18594 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
18597 append_type_to_template_for_access_check_1 (templ
, type_decl
,
18601 /* Set up the hash tables for template instantiations. */
18604 init_template_processing (void)
18606 decl_specializations
= htab_create_ggc (37,
18607 hash_specialization
,
18608 eq_specializations
,
18610 type_specializations
= htab_create_ggc (37,
18611 hash_specialization
,
18612 eq_specializations
,
18616 /* Print stats about the template hash tables for -fstats. */
18619 print_template_statistics (void)
18621 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
18622 "%f collisions\n", (long) htab_size (decl_specializations
),
18623 (long) htab_elements (decl_specializations
),
18624 htab_collisions (decl_specializations
));
18625 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
18626 "%f collisions\n", (long) htab_size (type_specializations
),
18627 (long) htab_elements (type_specializations
),
18628 htab_collisions (type_specializations
));
18631 #include "gt-cp-pt.h"