1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
5 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
6 Rewritten by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* Known bugs or deficiencies include:
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
31 #include "coretypes.h"
35 #include "pointer-set.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "cp-objcp-common.h"
41 #include "tree-inline.h"
45 #include "tree-iterator.h"
48 /* The type of functions taking a tree, and some additional data, and
50 typedef int (*tree_fn_t
) (tree
, void*);
52 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
53 instantiations have been deferred, either because their definitions
54 were not yet available, or because we were putting off doing the work. */
55 struct GTY ((chain_next ("%h.next"))) pending_template
{
56 struct pending_template
*next
;
57 struct tinst_level
*tinst
;
60 static GTY(()) struct pending_template
*pending_templates
;
61 static GTY(()) struct pending_template
*last_pending_template
;
63 int processing_template_parmlist
;
64 static int template_header_count
;
66 static GTY(()) tree saved_trees
;
67 static VEC(int,heap
) *inline_parm_levels
;
69 static GTY(()) struct tinst_level
*current_tinst_level
;
71 static GTY(()) tree saved_access_scope
;
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr
;
78 /* A map from local variable declarations in the body of the template
79 presently being instantiated to the corresponding instantiated
81 static struct pointer_map_t
*local_specializations
;
83 typedef struct GTY(()) spec_entry
90 static GTY ((param_is (spec_entry
)))
91 htab_t decl_specializations
;
93 static GTY ((param_is (spec_entry
)))
94 htab_t type_specializations
;
96 /* Contains canonical template parameter types. The vector is indexed by
97 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
98 TREE_LIST, whose TREE_VALUEs contain the canonical template
99 parameters of various types and levels. */
100 static GTY(()) VEC(tree
,gc
) *canonical_template_parms
;
102 #define UNIFY_ALLOW_NONE 0
103 #define UNIFY_ALLOW_MORE_CV_QUAL 1
104 #define UNIFY_ALLOW_LESS_CV_QUAL 2
105 #define UNIFY_ALLOW_DERIVED 4
106 #define UNIFY_ALLOW_INTEGER 8
107 #define UNIFY_ALLOW_OUTER_LEVEL 16
108 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
109 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
111 enum template_base_result
{
113 tbr_ambiguous_baseclass
,
117 static void push_access_scope (tree
);
118 static void pop_access_scope (tree
);
119 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
120 unification_kind_t
, int,
122 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
123 unification_kind_t
, int, bool, bool);
124 static int unify (tree
, tree
, tree
, tree
, int, bool);
125 static void add_pending_template (tree
);
126 static tree
reopen_tinst_level (struct tinst_level
*);
127 static tree
tsubst_initializer_list (tree
, tree
);
128 static tree
get_class_bindings (tree
, tree
, tree
);
129 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
131 static void tsubst_enum (tree
, tree
, tree
);
132 static tree
add_to_template_args (tree
, tree
);
133 static tree
add_outermost_template_args (tree
, tree
);
134 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
135 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
137 static int type_unification_real (tree
, tree
, tree
, const tree
*,
138 unsigned int, int, unification_kind_t
, int,
140 static void note_template_header (int);
141 static tree
convert_nontype_argument_function (tree
, tree
);
142 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
143 static tree
convert_template_argument (tree
, tree
, tree
,
144 tsubst_flags_t
, int, tree
);
145 static int for_each_template_parm (tree
, tree_fn_t
, void*,
146 struct pointer_set_t
*, bool);
147 static tree
expand_template_argument_pack (tree
);
148 static tree
build_template_parm_index (int, int, int, tree
, tree
);
149 static bool inline_needs_template_parms (tree
);
150 static void push_inline_template_parms_recursive (tree
, int);
151 static tree
retrieve_local_specialization (tree
);
152 static void register_local_specialization (tree
, tree
);
153 static hashval_t
hash_specialization (const void *p
);
154 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
155 static int mark_template_parm (tree
, void *);
156 static int template_parm_this_level_p (tree
, void *);
157 static tree
tsubst_friend_function (tree
, tree
);
158 static tree
tsubst_friend_class (tree
, tree
);
159 static int can_complete_type_without_circularity (tree
);
160 static tree
get_bindings (tree
, tree
, tree
, bool);
161 static int template_decl_level (tree
);
162 static int check_cv_quals_for_unify (int, tree
, tree
);
163 static void template_parm_level_and_index (tree
, int*, int*);
164 static int unify_pack_expansion (tree
, tree
, tree
,
165 tree
, unification_kind_t
, bool, bool);
166 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
167 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
168 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
169 static void regenerate_decl_from_template (tree
, tree
);
170 static tree
most_specialized_class (tree
, tree
, tsubst_flags_t
);
171 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
172 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
173 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
174 static bool check_specialization_scope (void);
175 static tree
process_partial_specialization (tree
);
176 static void set_current_access_from_decl (tree
);
177 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
179 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
180 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
182 static bool template_template_parm_bindings_ok_p (tree
, tree
);
183 static int template_args_equal (tree
, tree
);
184 static void tsubst_default_arguments (tree
);
185 static tree
for_each_template_parm_r (tree
*, int *, void *);
186 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
187 static void copy_default_args_to_explicit_spec (tree
);
188 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
189 static bool dependent_template_arg_p (tree
);
190 static bool any_template_arguments_need_structural_equality_p (tree
);
191 static bool dependent_type_p_r (tree
);
192 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
, bool);
193 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
194 static tree
tsubst_pack_expansion (tree
, tree
, tsubst_flags_t
, tree
);
195 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
196 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
197 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
199 static tree
listify (tree
);
200 static tree
listify_autos (tree
, tree
);
201 static tree
template_parm_to_arg (tree t
);
202 static bool arg_from_parm_pack_p (tree
, tree
);
203 static tree
current_template_args (void);
204 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
206 /* Make the current scope suitable for access checking when we are
207 processing T. T can be FUNCTION_DECL for instantiated function
208 template, or VAR_DECL for static member variable (need by
209 instantiate_decl). */
212 push_access_scope (tree t
)
214 gcc_assert (TREE_CODE (t
) == FUNCTION_DECL
215 || TREE_CODE (t
) == VAR_DECL
);
217 if (DECL_FRIEND_CONTEXT (t
))
218 push_nested_class (DECL_FRIEND_CONTEXT (t
));
219 else if (DECL_CLASS_SCOPE_P (t
))
220 push_nested_class (DECL_CONTEXT (t
));
222 push_to_top_level ();
224 if (TREE_CODE (t
) == FUNCTION_DECL
)
226 saved_access_scope
= tree_cons
227 (NULL_TREE
, current_function_decl
, saved_access_scope
);
228 current_function_decl
= t
;
232 /* Restore the scope set up by push_access_scope. T is the node we
236 pop_access_scope (tree t
)
238 if (TREE_CODE (t
) == FUNCTION_DECL
)
240 current_function_decl
= TREE_VALUE (saved_access_scope
);
241 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
244 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
247 pop_from_top_level ();
250 /* Do any processing required when DECL (a member template
251 declaration) is finished. Returns the TEMPLATE_DECL corresponding
252 to DECL, unless it is a specialization, in which case the DECL
253 itself is returned. */
256 finish_member_template_decl (tree decl
)
258 if (decl
== error_mark_node
)
259 return error_mark_node
;
261 gcc_assert (DECL_P (decl
));
263 if (TREE_CODE (decl
) == TYPE_DECL
)
267 type
= TREE_TYPE (decl
);
268 if (type
== error_mark_node
)
269 return error_mark_node
;
270 if (MAYBE_CLASS_TYPE_P (type
)
271 && CLASSTYPE_TEMPLATE_INFO (type
)
272 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
274 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
275 check_member_template (tmpl
);
280 else if (TREE_CODE (decl
) == FIELD_DECL
)
281 error ("data member %qD cannot be a member template", decl
);
282 else if (DECL_TEMPLATE_INFO (decl
))
284 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
286 check_member_template (DECL_TI_TEMPLATE (decl
));
287 return DECL_TI_TEMPLATE (decl
);
293 error ("invalid member template declaration %qD", decl
);
295 return error_mark_node
;
298 /* Create a template info node. */
301 build_template_info (tree template_decl
, tree template_args
)
303 tree result
= make_node (TEMPLATE_INFO
);
304 TI_TEMPLATE (result
) = template_decl
;
305 TI_ARGS (result
) = template_args
;
309 /* Return the template info node corresponding to T, whatever T is. */
312 get_template_info (const_tree t
)
314 tree tinfo
= NULL_TREE
;
316 if (!t
|| t
== error_mark_node
)
319 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
320 tinfo
= DECL_TEMPLATE_INFO (t
);
322 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
325 if (TAGGED_TYPE_P (t
))
326 tinfo
= TYPE_TEMPLATE_INFO (t
);
327 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
328 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
333 /* Returns the template nesting level of the indicated class TYPE.
343 A<T>::B<U> has depth two, while A<T> has depth one.
344 Both A<T>::B<int> and A<int>::B<U> have depth one, if
345 they are instantiations, not specializations.
347 This function is guaranteed to return 0 if passed NULL_TREE so
348 that, for example, `template_class_depth (current_class_type)' is
352 template_class_depth (tree type
)
357 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
358 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
359 ? CP_DECL_CONTEXT (type
) : CP_TYPE_CONTEXT (type
))
361 tree tinfo
= get_template_info (type
);
363 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
364 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
371 /* Subroutine of maybe_begin_member_template_processing.
372 Returns true if processing DECL needs us to push template parms. */
375 inline_needs_template_parms (tree decl
)
377 if (! DECL_TEMPLATE_INFO (decl
))
380 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
381 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
384 /* Subroutine of maybe_begin_member_template_processing.
385 Push the template parms in PARMS, starting from LEVELS steps into the
386 chain, and ending at the beginning, since template parms are listed
390 push_inline_template_parms_recursive (tree parmlist
, int levels
)
392 tree parms
= TREE_VALUE (parmlist
);
396 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
398 ++processing_template_decl
;
399 current_template_parms
400 = tree_cons (size_int (processing_template_decl
),
401 parms
, current_template_parms
);
402 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
404 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
406 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
408 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
410 if (parm
== error_mark_node
)
413 gcc_assert (DECL_P (parm
));
415 switch (TREE_CODE (parm
))
424 /* Make a CONST_DECL as is done in process_template_parm.
425 It is ugly that we recreate this here; the original
426 version built in process_template_parm is no longer
428 tree decl
= build_decl (DECL_SOURCE_LOCATION (parm
),
429 CONST_DECL
, DECL_NAME (parm
),
431 DECL_ARTIFICIAL (decl
) = 1;
432 TREE_CONSTANT (decl
) = 1;
433 TREE_READONLY (decl
) = 1;
434 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
435 SET_DECL_TEMPLATE_PARM_P (decl
);
446 /* Restore the template parameter context for a member template or
447 a friend template defined in a class definition. */
450 maybe_begin_member_template_processing (tree decl
)
455 if (inline_needs_template_parms (decl
))
457 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
458 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
460 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
463 parms
= TREE_CHAIN (parms
);
466 push_inline_template_parms_recursive (parms
, levels
);
469 /* Remember how many levels of template parameters we pushed so that
470 we can pop them later. */
471 VEC_safe_push (int, heap
, inline_parm_levels
, levels
);
474 /* Undo the effects of maybe_begin_member_template_processing. */
477 maybe_end_member_template_processing (void)
482 if (VEC_length (int, inline_parm_levels
) == 0)
485 last
= VEC_pop (int, inline_parm_levels
);
486 for (i
= 0; i
< last
; ++i
)
488 --processing_template_decl
;
489 current_template_parms
= TREE_CHAIN (current_template_parms
);
494 /* Return a new template argument vector which contains all of ARGS,
495 but has as its innermost set of arguments the EXTRA_ARGS. */
498 add_to_template_args (tree args
, tree extra_args
)
505 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
508 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
509 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
511 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
512 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
514 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
515 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
520 /* Like add_to_template_args, but only the outermost ARGS are added to
521 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
522 (EXTRA_ARGS) levels are added. This function is used to combine
523 the template arguments from a partial instantiation with the
524 template arguments used to attain the full instantiation from the
525 partial instantiation. */
528 add_outermost_template_args (tree args
, tree extra_args
)
532 /* If there are more levels of EXTRA_ARGS than there are ARGS,
533 something very fishy is going on. */
534 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
536 /* If *all* the new arguments will be the EXTRA_ARGS, just return
538 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
541 /* For the moment, we make ARGS look like it contains fewer levels. */
542 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
544 new_args
= add_to_template_args (args
, extra_args
);
546 /* Now, we restore ARGS to its full dimensions. */
547 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
552 /* Return the N levels of innermost template arguments from the ARGS. */
555 get_innermost_template_args (tree args
, int n
)
563 /* If N is 1, just return the innermost set of template arguments. */
565 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
567 /* If we're not removing anything, just return the arguments we were
569 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
570 gcc_assert (extra_levels
>= 0);
571 if (extra_levels
== 0)
574 /* Make a new set of arguments, not containing the outer arguments. */
575 new_args
= make_tree_vec (n
);
576 for (i
= 1; i
<= n
; ++i
)
577 SET_TMPL_ARGS_LEVEL (new_args
, i
,
578 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
583 /* The inverse of get_innermost_template_args: Return all but the innermost
584 EXTRA_LEVELS levels of template arguments from the ARGS. */
587 strip_innermost_template_args (tree args
, int extra_levels
)
590 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
595 /* If N is 1, just return the outermost set of template arguments. */
597 return TMPL_ARGS_LEVEL (args
, 1);
599 /* If we're not removing anything, just return the arguments we were
601 gcc_assert (extra_levels
>= 0);
602 if (extra_levels
== 0)
605 /* Make a new set of arguments, not containing the inner arguments. */
606 new_args
= make_tree_vec (n
);
607 for (i
= 1; i
<= n
; ++i
)
608 SET_TMPL_ARGS_LEVEL (new_args
, i
,
609 TMPL_ARGS_LEVEL (args
, i
));
614 /* We've got a template header coming up; push to a new level for storing
618 begin_template_parm_list (void)
620 /* We use a non-tag-transparent scope here, which causes pushtag to
621 put tags in this scope, rather than in the enclosing class or
622 namespace scope. This is the right thing, since we want
623 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
624 global template class, push_template_decl handles putting the
625 TEMPLATE_DECL into top-level scope. For a nested template class,
628 template <class T> struct S1 {
629 template <class T> struct S2 {};
632 pushtag contains special code to call pushdecl_with_scope on the
633 TEMPLATE_DECL for S2. */
634 begin_scope (sk_template_parms
, NULL
);
635 ++processing_template_decl
;
636 ++processing_template_parmlist
;
637 note_template_header (0);
640 /* This routine is called when a specialization is declared. If it is
641 invalid to declare a specialization here, an error is reported and
642 false is returned, otherwise this routine will return true. */
645 check_specialization_scope (void)
647 tree scope
= current_scope ();
651 An explicit specialization shall be declared in the namespace of
652 which the template is a member, or, for member templates, in the
653 namespace of which the enclosing class or enclosing class
654 template is a member. An explicit specialization of a member
655 function, member class or static data member of a class template
656 shall be declared in the namespace of which the class template
658 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
660 error ("explicit specialization in non-namespace scope %qD", scope
);
666 In an explicit specialization declaration for a member of a class
667 template or a member template that appears in namespace scope,
668 the member template and some of its enclosing class templates may
669 remain unspecialized, except that the declaration shall not
670 explicitly specialize a class member template if its enclosing
671 class templates are not explicitly specialized as well. */
672 if (current_template_parms
)
674 error ("enclosing class templates are not explicitly specialized");
681 /* We've just seen template <>. */
684 begin_specialization (void)
686 begin_scope (sk_template_spec
, NULL
);
687 note_template_header (1);
688 return check_specialization_scope ();
691 /* Called at then end of processing a declaration preceded by
695 end_specialization (void)
698 reset_specialization ();
701 /* Any template <>'s that we have seen thus far are not referring to a
702 function specialization. */
705 reset_specialization (void)
707 processing_specialization
= 0;
708 template_header_count
= 0;
711 /* We've just seen a template header. If SPECIALIZATION is nonzero,
712 it was of the form template <>. */
715 note_template_header (int specialization
)
717 processing_specialization
= specialization
;
718 template_header_count
++;
721 /* We're beginning an explicit instantiation. */
724 begin_explicit_instantiation (void)
726 gcc_assert (!processing_explicit_instantiation
);
727 processing_explicit_instantiation
= true;
732 end_explicit_instantiation (void)
734 gcc_assert (processing_explicit_instantiation
);
735 processing_explicit_instantiation
= false;
738 /* An explicit specialization or partial specialization TMPL is being
739 declared. Check that the namespace in which the specialization is
740 occurring is permissible. Returns false iff it is invalid to
741 specialize TMPL in the current namespace. */
744 check_specialization_namespace (tree tmpl
)
746 tree tpl_ns
= decl_namespace_context (tmpl
);
750 An explicit specialization shall be declared in the namespace of
751 which the template is a member, or, for member templates, in the
752 namespace of which the enclosing class or enclosing class
753 template is a member. An explicit specialization of a member
754 function, member class or static data member of a class template
755 shall be declared in the namespace of which the class template is
757 if (current_scope() != DECL_CONTEXT (tmpl
)
758 && !at_namespace_scope_p ())
760 error ("specialization of %qD must appear at namespace scope", tmpl
);
763 if (is_associated_namespace (current_namespace
, tpl_ns
))
764 /* Same or super-using namespace. */
768 permerror (input_location
, "specialization of %qD in different namespace", tmpl
);
769 permerror (input_location
, " from definition of %q+#D", tmpl
);
774 /* SPEC is an explicit instantiation. Check that it is valid to
775 perform this explicit instantiation in the current namespace. */
778 check_explicit_instantiation_namespace (tree spec
)
782 /* DR 275: An explicit instantiation shall appear in an enclosing
783 namespace of its template. */
784 ns
= decl_namespace_context (spec
);
785 if (!is_ancestor (current_namespace
, ns
))
786 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
787 "(which does not enclose namespace %qD)",
788 spec
, current_namespace
, ns
);
791 /* The TYPE is being declared. If it is a template type, that means it
792 is a partial specialization. Do appropriate error-checking. */
795 maybe_process_partial_specialization (tree type
)
799 if (type
== error_mark_node
)
800 return error_mark_node
;
802 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
804 error ("name of class shadows template template parameter %qD",
806 return error_mark_node
;
809 context
= TYPE_CONTEXT (type
);
811 if ((CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
812 /* Consider non-class instantiations of alias templates as
815 && TYPE_TEMPLATE_INFO (type
)
816 && DECL_LANG_SPECIFIC (TYPE_NAME (type
))
817 && DECL_USE_TEMPLATE (TYPE_NAME (type
))))
819 /* This is for ordinary explicit specialization and partial
820 specialization of a template class such as:
822 template <> class C<int>;
826 template <class T> class C<T*>;
828 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
830 if (CLASS_TYPE_P (type
)
831 && CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
832 && !COMPLETE_TYPE_P (type
))
834 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
));
835 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
836 if (processing_template_decl
)
838 if (push_template_decl (TYPE_MAIN_DECL (type
))
840 return error_mark_node
;
843 else if (CLASS_TYPE_P (type
)
844 && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
845 error ("specialization of %qT after instantiation", type
);
847 if (DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type
)))
849 error ("partial specialization of alias template %qD",
850 TYPE_TI_TEMPLATE (type
));
851 return error_mark_node
;
854 else if (CLASS_TYPE_P (type
)
855 && !CLASSTYPE_USE_TEMPLATE (type
)
856 && CLASSTYPE_TEMPLATE_INFO (type
)
857 && context
&& CLASS_TYPE_P (context
)
858 && CLASSTYPE_TEMPLATE_INFO (context
))
860 /* This is for an explicit specialization of member class
861 template according to [temp.expl.spec/18]:
863 template <> template <class U> class C<int>::D;
865 The context `C<int>' must be an implicit instantiation.
866 Otherwise this is just a member class template declared
869 template <> class C<int> { template <class U> class D; };
870 template <> template <class U> class C<int>::D;
872 In the first case, `C<int>::D' is a specialization of `C<T>::D'
873 while in the second case, `C<int>::D' is a primary template
874 and `C<T>::D' may not exist. */
876 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
877 && !COMPLETE_TYPE_P (type
))
880 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
882 if (current_namespace
883 != decl_namespace_context (tmpl
))
885 permerror (input_location
, "specializing %q#T in different namespace", type
);
886 permerror (input_location
, " from definition of %q+#D", tmpl
);
889 /* Check for invalid specialization after instantiation:
891 template <> template <> class C<int>::D<int>;
892 template <> template <class U> class C<int>::D; */
894 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
895 t
; t
= TREE_CHAIN (t
))
897 tree inst
= TREE_VALUE (t
);
898 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
))
900 /* We already have a full specialization of this partial
901 instantiation. Reassign it to the new member
902 specialization template. */
907 elt
.tmpl
= most_general_template (tmpl
);
908 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
911 htab_remove_elt (type_specializations
, &elt
);
914 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
916 slot
= htab_find_slot (type_specializations
, &elt
, INSERT
);
917 entry
= ggc_alloc_spec_entry ();
921 else if (COMPLETE_OR_OPEN_TYPE_P (inst
))
922 /* But if we've had an implicit instantiation, that's a
923 problem ([temp.expl.spec]/6). */
924 error ("specialization %qT after instantiation %qT",
928 /* Mark TYPE as a specialization. And as a result, we only
929 have one level of template argument for the innermost
931 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
932 CLASSTYPE_TI_ARGS (type
)
933 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
936 else if (processing_specialization
)
938 /* Someday C++0x may allow for enum template specialization. */
939 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
940 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
941 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
942 "of %qD not allowed by ISO C++", type
);
945 error ("explicit specialization of non-template %qT", type
);
946 return error_mark_node
;
953 /* Returns nonzero if we can optimize the retrieval of specializations
954 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
955 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
958 optimize_specialization_lookup_p (tree tmpl
)
960 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
961 && DECL_CLASS_SCOPE_P (tmpl
)
962 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
964 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
965 /* The optimized lookup depends on the fact that the
966 template arguments for the member function template apply
967 purely to the containing class, which is not true if the
968 containing class is an explicit or partial
970 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
971 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
972 && !DECL_CONV_FN_P (tmpl
)
973 /* It is possible to have a template that is not a member
974 template and is not a member of a template class:
976 template <typename T>
977 struct S { friend A::f(); };
979 Here, the friend function is a template, but the context does
980 not have template information. The optimized lookup relies
981 on having ARGS be the template arguments for both the class
982 and the function template. */
983 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
986 /* Retrieve the specialization (in the sense of [temp.spec] - a
987 specialization is either an instantiation or an explicit
988 specialization) of TMPL for the given template ARGS. If there is
989 no such specialization, return NULL_TREE. The ARGS are a vector of
990 arguments, or a vector of vectors of arguments, in the case of
991 templates with more than one level of parameters.
993 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
994 then we search for a partial specialization matching ARGS. This
995 parameter is ignored if TMPL is not a class template. */
998 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1000 if (args
== error_mark_node
)
1003 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
1005 /* There should be as many levels of arguments as there are
1006 levels of parameters. */
1007 gcc_assert (TMPL_ARGS_DEPTH (args
)
1008 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
1010 if (optimize_specialization_lookup_p (tmpl
))
1012 tree class_template
;
1013 tree class_specialization
;
1014 VEC(tree
,gc
) *methods
;
1018 /* The template arguments actually apply to the containing
1019 class. Find the class specialization with those
1021 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1022 class_specialization
1023 = retrieve_specialization (class_template
, args
, 0);
1024 if (!class_specialization
)
1026 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1027 for the specialization. */
1028 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1031 /* Iterate through the methods with the indicated name, looking
1032 for the one that has an instance of TMPL. */
1033 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1034 for (fns
= VEC_index (tree
, methods
, idx
); fns
; fns
= OVL_NEXT (fns
))
1036 tree fn
= OVL_CURRENT (fns
);
1037 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1038 /* using-declarations can add base methods to the method vec,
1039 and we don't want those here. */
1040 && DECL_CONTEXT (fn
) == class_specialization
)
1049 htab_t specializations
;
1053 elt
.spec
= NULL_TREE
;
1055 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1056 specializations
= type_specializations
;
1058 specializations
= decl_specializations
;
1061 hash
= hash_specialization (&elt
);
1062 found
= (spec_entry
*) htab_find_with_hash (specializations
, &elt
, hash
);
1070 /* Like retrieve_specialization, but for local declarations. */
1073 retrieve_local_specialization (tree tmpl
)
1077 if (local_specializations
== NULL
)
1080 slot
= pointer_map_contains (local_specializations
, tmpl
);
1081 return slot
? (tree
) *slot
: NULL_TREE
;
1084 /* Returns nonzero iff DECL is a specialization of TMPL. */
1087 is_specialization_of (tree decl
, tree tmpl
)
1091 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1095 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1101 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1103 for (t
= TREE_TYPE (decl
);
1105 t
= CLASSTYPE_USE_TEMPLATE (t
)
1106 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1107 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1114 /* Returns nonzero iff DECL is a specialization of friend declaration
1115 FRIEND_DECL according to [temp.friend]. */
1118 is_specialization_of_friend (tree decl
, tree friend_decl
)
1120 bool need_template
= true;
1123 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1124 || TREE_CODE (decl
) == TYPE_DECL
);
1126 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1127 of a template class, we want to check if DECL is a specialization
1129 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1130 && DECL_TEMPLATE_INFO (friend_decl
)
1131 && !DECL_USE_TEMPLATE (friend_decl
))
1133 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1134 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1135 need_template
= false;
1137 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1138 && !PRIMARY_TEMPLATE_P (friend_decl
))
1139 need_template
= false;
1141 /* There is nothing to do if this is not a template friend. */
1142 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1145 if (is_specialization_of (decl
, friend_decl
))
1149 A member of a class template may be declared to be a friend of a
1150 non-template class. In this case, the corresponding member of
1151 every specialization of the class template is a friend of the
1152 class granting friendship.
1154 For example, given a template friend declaration
1156 template <class T> friend void A<T>::f();
1158 the member function below is considered a friend
1160 template <> struct A<int> {
1164 For this type of template friend, TEMPLATE_DEPTH below will be
1165 nonzero. To determine if DECL is a friend of FRIEND, we first
1166 check if the enclosing class is a specialization of another. */
1168 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1170 && DECL_CLASS_SCOPE_P (decl
)
1171 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1172 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1174 /* Next, we check the members themselves. In order to handle
1175 a few tricky cases, such as when FRIEND_DECL's are
1177 template <class T> friend void A<T>::g(T t);
1178 template <class T> template <T t> friend void A<T>::h();
1182 void A<int>::g(int);
1183 template <int> void A<int>::h();
1185 we need to figure out ARGS, the template arguments from
1186 the context of DECL. This is required for template substitution
1187 of `T' in the function parameter of `g' and template parameter
1188 of `h' in the above examples. Here ARGS corresponds to `int'. */
1190 tree context
= DECL_CONTEXT (decl
);
1191 tree args
= NULL_TREE
;
1192 int current_depth
= 0;
1194 while (current_depth
< template_depth
)
1196 if (CLASSTYPE_TEMPLATE_INFO (context
))
1198 if (current_depth
== 0)
1199 args
= TYPE_TI_ARGS (context
);
1201 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1204 context
= TYPE_CONTEXT (context
);
1207 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1212 tree friend_args_type
;
1213 tree decl_args_type
;
1215 /* Make sure that both DECL and FRIEND_DECL are templates or
1217 is_template
= DECL_TEMPLATE_INFO (decl
)
1218 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1219 if (need_template
^ is_template
)
1221 else if (is_template
)
1223 /* If both are templates, check template parameter list. */
1225 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1227 if (!comp_template_parms
1228 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1232 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1235 decl_type
= TREE_TYPE (decl
);
1237 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1238 tf_none
, NULL_TREE
);
1239 if (friend_type
== error_mark_node
)
1242 /* Check if return types match. */
1243 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1246 /* Check if function parameter types match, ignoring the
1247 `this' parameter. */
1248 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1249 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1250 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1251 friend_args_type
= TREE_CHAIN (friend_args_type
);
1252 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1253 decl_args_type
= TREE_CHAIN (decl_args_type
);
1255 return compparms (decl_args_type
, friend_args_type
);
1259 /* DECL is a TYPE_DECL */
1261 tree decl_type
= TREE_TYPE (decl
);
1263 /* Make sure that both DECL and FRIEND_DECL are templates or
1266 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1267 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1269 if (need_template
^ is_template
)
1271 else if (is_template
)
1274 /* If both are templates, check the name of the two
1275 TEMPLATE_DECL's first because is_friend didn't. */
1276 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1277 != DECL_NAME (friend_decl
))
1280 /* Now check template parameter list. */
1282 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1284 return comp_template_parms
1285 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1289 return (DECL_NAME (decl
)
1290 == DECL_NAME (friend_decl
));
1296 /* Register the specialization SPEC as a specialization of TMPL with
1297 the indicated ARGS. IS_FRIEND indicates whether the specialization
1298 is actually just a friend declaration. Returns SPEC, or an
1299 equivalent prior declaration, if available. */
1302 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1309 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
));
1311 if (TREE_CODE (spec
) == FUNCTION_DECL
1312 && uses_template_parms (DECL_TI_ARGS (spec
)))
1313 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1314 register it; we want the corresponding TEMPLATE_DECL instead.
1315 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1316 the more obvious `uses_template_parms (spec)' to avoid problems
1317 with default function arguments. In particular, given
1318 something like this:
1320 template <class T> void f(T t1, T t = T())
1322 the default argument expression is not substituted for in an
1323 instantiation unless and until it is actually needed. */
1326 if (optimize_specialization_lookup_p (tmpl
))
1327 /* We don't put these specializations in the hash table, but we might
1328 want to give an error about a mismatch. */
1329 fn
= retrieve_specialization (tmpl
, args
, 0);
1337 hash
= hash_specialization (&elt
);
1340 htab_find_slot_with_hash (decl_specializations
, &elt
, hash
, INSERT
);
1342 fn
= ((spec_entry
*) *slot
)->spec
;
1347 /* We can sometimes try to re-register a specialization that we've
1348 already got. In particular, regenerate_decl_from_template calls
1349 duplicate_decls which will update the specialization list. But,
1350 we'll still get called again here anyhow. It's more convenient
1351 to simply allow this than to try to prevent it. */
1354 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1356 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1358 if (DECL_ODR_USED (fn
)
1359 || DECL_EXPLICIT_INSTANTIATION (fn
))
1361 error ("specialization of %qD after instantiation",
1363 return error_mark_node
;
1368 /* This situation should occur only if the first
1369 specialization is an implicit instantiation, the
1370 second is an explicit specialization, and the
1371 implicit instantiation has not yet been used. That
1372 situation can occur if we have implicitly
1373 instantiated a member function and then specialized
1376 We can also wind up here if a friend declaration that
1377 looked like an instantiation turns out to be a
1380 template <class T> void foo(T);
1381 class S { friend void foo<>(int) };
1382 template <> void foo(int);
1384 We transform the existing DECL in place so that any
1385 pointers to it become pointers to the updated
1388 If there was a definition for the template, but not
1389 for the specialization, we want this to look as if
1390 there were no definition, and vice versa. */
1391 DECL_INITIAL (fn
) = NULL_TREE
;
1392 duplicate_decls (spec
, fn
, is_friend
);
1393 /* The call to duplicate_decls will have applied
1396 An explicit specialization of a function template
1397 is inline only if it is explicitly declared to be,
1398 and independently of whether its function template
1401 to the primary function; now copy the inline bits to
1402 the various clones. */
1403 FOR_EACH_CLONE (clone
, fn
)
1405 DECL_DECLARED_INLINE_P (clone
)
1406 = DECL_DECLARED_INLINE_P (fn
);
1407 DECL_SOURCE_LOCATION (clone
)
1408 = DECL_SOURCE_LOCATION (fn
);
1410 check_specialization_namespace (fn
);
1415 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1417 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1418 /* Dup decl failed, but this is a new definition. Set the
1419 line number so any errors match this new
1421 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1427 return duplicate_decls (spec
, fn
, is_friend
);
1429 /* A specialization must be declared in the same namespace as the
1430 template it is specializing. */
1431 if (DECL_TEMPLATE_SPECIALIZATION (spec
)
1432 && !check_specialization_namespace (tmpl
))
1433 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1435 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1437 spec_entry
*entry
= ggc_alloc_spec_entry ();
1438 gcc_assert (tmpl
&& args
&& spec
);
1441 if (TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1442 && PRIMARY_TEMPLATE_P (tmpl
)
1443 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1444 /* TMPL is a forward declaration of a template function; keep a list
1445 of all specializations in case we need to reassign them to a friend
1446 template later in tsubst_friend_function. */
1447 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1448 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1454 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1455 TMPL and ARGS members, ignores SPEC. */
1458 eq_specializations (const void *p1
, const void *p2
)
1460 const spec_entry
*e1
= (const spec_entry
*)p1
;
1461 const spec_entry
*e2
= (const spec_entry
*)p2
;
1463 return (e1
->tmpl
== e2
->tmpl
1464 && comp_template_args (e1
->args
, e2
->args
));
1467 /* Returns a hash for a template TMPL and template arguments ARGS. */
1470 hash_tmpl_and_args (tree tmpl
, tree args
)
1472 hashval_t val
= DECL_UID (tmpl
);
1473 return iterative_hash_template_arg (args
, val
);
1476 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1480 hash_specialization (const void *p
)
1482 const spec_entry
*e
= (const spec_entry
*)p
;
1483 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1486 /* Recursively calculate a hash value for a template argument ARG, for use
1487 in the hash tables of template specializations. */
1490 iterative_hash_template_arg (tree arg
, hashval_t val
)
1492 unsigned HOST_WIDE_INT i
;
1493 enum tree_code code
;
1496 if (arg
== NULL_TREE
)
1497 return iterative_hash_object (arg
, val
);
1502 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1503 /* We can get one of these when re-hashing a previous entry in the middle
1504 of substituting into a pack expansion. Just look through it. */
1505 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1507 code
= TREE_CODE (arg
);
1508 tclass
= TREE_CODE_CLASS (code
);
1510 val
= iterative_hash_object (code
, val
);
1517 case IDENTIFIER_NODE
:
1518 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1522 int i
, len
= TREE_VEC_LENGTH (arg
);
1523 for (i
= 0; i
< len
; ++i
)
1524 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1528 case TYPE_PACK_EXPANSION
:
1529 case EXPR_PACK_EXPANSION
:
1530 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1531 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1533 case TYPE_ARGUMENT_PACK
:
1534 case NONTYPE_ARGUMENT_PACK
:
1535 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1538 for (; arg
; arg
= TREE_CHAIN (arg
))
1539 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1543 for (; arg
; arg
= OVL_NEXT (arg
))
1544 val
= iterative_hash_template_arg (OVL_CURRENT (arg
), val
);
1550 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1552 val
= iterative_hash_template_arg (field
, val
);
1553 val
= iterative_hash_template_arg (value
, val
);
1559 if (!DECL_ARTIFICIAL (arg
))
1561 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1562 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1564 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1567 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1570 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1571 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1573 case TEMPLATE_PARM_INDEX
:
1574 val
= iterative_hash_template_arg
1575 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1576 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1577 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1580 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1581 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1582 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1585 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1587 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1591 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1592 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1593 val
= iterative_hash_object (code
, val
);
1594 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1597 /* A lambda can't appear in a template arg, but don't crash on
1599 gcc_assert (seen_error ());
1603 case IMPLICIT_CONV_EXPR
:
1604 case STATIC_CAST_EXPR
:
1605 case REINTERPRET_CAST_EXPR
:
1606 case CONST_CAST_EXPR
:
1607 case DYNAMIC_CAST_EXPR
:
1609 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1610 /* Now hash operands as usual. */
1620 if (TYPE_CANONICAL (arg
))
1621 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1623 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1624 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1625 /* Otherwise just compare the types during lookup. */
1628 case tcc_declaration
:
1630 return iterative_hash_expr (arg
, val
);
1633 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1635 unsigned n
= cp_tree_operand_length (arg
);
1636 for (i
= 0; i
< n
; ++i
)
1637 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1645 /* Unregister the specialization SPEC as a specialization of TMPL.
1646 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1647 if the SPEC was listed as a specialization of TMPL.
1649 Note that SPEC has been ggc_freed, so we can't look inside it. */
1652 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1657 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1658 elt
.args
= TI_ARGS (tinfo
);
1659 elt
.spec
= NULL_TREE
;
1661 entry
= (spec_entry
*) htab_find (decl_specializations
, &elt
);
1664 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1665 gcc_assert (new_spec
!= NULL_TREE
);
1666 entry
->spec
= new_spec
;
1673 /* Like register_specialization, but for local declarations. We are
1674 registering SPEC, an instantiation of TMPL. */
1677 register_local_specialization (tree spec
, tree tmpl
)
1681 slot
= pointer_map_insert (local_specializations
, tmpl
);
1685 /* TYPE is a class type. Returns true if TYPE is an explicitly
1686 specialized class. */
1689 explicit_class_specialization_p (tree type
)
1691 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1693 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1696 /* Print the list of functions at FNS, going through all the overloads
1697 for each element of the list. Alternatively, FNS can not be a
1698 TREE_LIST, in which case it will be printed together with all the
1701 MORE and *STR should respectively be FALSE and NULL when the function
1702 is called from the outside. They are used internally on recursive
1703 calls. print_candidates manages the two parameters and leaves NULL
1704 in *STR when it ends. */
1707 print_candidates_1 (tree fns
, bool more
, const char **str
)
1710 char *spaces
= NULL
;
1712 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1713 if (TREE_CODE (fn
) == TREE_LIST
)
1715 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1716 print_candidates_1 (TREE_VALUE (fn2
),
1717 TREE_CHAIN (fn2
) || more
, str
);
1723 /* Pick the prefix string. */
1724 if (!more
&& !OVL_NEXT (fns
))
1726 error ("candidate is: %+#D", OVL_CURRENT (fn
));
1730 *str
= _("candidates are:");
1731 spaces
= get_spaces (*str
);
1733 error ("%s %+#D", *str
, OVL_CURRENT (fn
));
1734 *str
= spaces
? spaces
: *str
;
1744 /* Print the list of candidate FNS in an error message. FNS can also
1745 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1748 print_candidates (tree fns
)
1750 const char *str
= NULL
;
1751 print_candidates_1 (fns
, false, &str
);
1752 gcc_assert (str
== NULL
);
1755 /* Returns the template (one of the functions given by TEMPLATE_ID)
1756 which can be specialized to match the indicated DECL with the
1757 explicit template args given in TEMPLATE_ID. The DECL may be
1758 NULL_TREE if none is available. In that case, the functions in
1759 TEMPLATE_ID are non-members.
1761 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1762 specialization of a member template.
1764 The TEMPLATE_COUNT is the number of references to qualifying
1765 template classes that appeared in the name of the function. See
1766 check_explicit_specialization for a more accurate description.
1768 TSK indicates what kind of template declaration (if any) is being
1769 declared. TSK_TEMPLATE indicates that the declaration given by
1770 DECL, though a FUNCTION_DECL, has template parameters, and is
1771 therefore a template function.
1773 The template args (those explicitly specified and those deduced)
1774 are output in a newly created vector *TARGS_OUT.
1776 If it is impossible to determine the result, an error message is
1777 issued. The error_mark_node is returned to indicate failure. */
1780 determine_specialization (tree template_id
,
1783 int need_member_template
,
1789 tree explicit_targs
;
1790 tree candidates
= NULL_TREE
;
1791 /* A TREE_LIST of templates of which DECL may be a specialization.
1792 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1793 corresponding TREE_PURPOSE is the set of template arguments that,
1794 when used to instantiate the template, would produce a function
1795 with the signature of DECL. */
1796 tree templates
= NULL_TREE
;
1798 cp_binding_level
*b
;
1800 *targs_out
= NULL_TREE
;
1802 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1803 return error_mark_node
;
1805 fns
= TREE_OPERAND (template_id
, 0);
1806 explicit_targs
= TREE_OPERAND (template_id
, 1);
1808 if (fns
== error_mark_node
)
1809 return error_mark_node
;
1811 /* Check for baselinks. */
1812 if (BASELINK_P (fns
))
1813 fns
= BASELINK_FUNCTIONS (fns
);
1815 if (!is_overloaded_fn (fns
))
1817 error ("%qD is not a function template", fns
);
1818 return error_mark_node
;
1821 /* Count the number of template headers specified for this
1824 for (b
= current_binding_level
;
1825 b
->kind
== sk_template_parms
;
1829 for (; fns
; fns
= OVL_NEXT (fns
))
1831 tree fn
= OVL_CURRENT (fns
);
1833 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1835 tree decl_arg_types
;
1839 /* In case of explicit specialization, we need to check if
1840 the number of template headers appearing in the specialization
1841 is correct. This is usually done in check_explicit_specialization,
1842 but the check done there cannot be exhaustive when specializing
1843 member functions. Consider the following code:
1845 template <> void A<int>::f(int);
1846 template <> template <> void A<int>::f(int);
1848 Assuming that A<int> is not itself an explicit specialization
1849 already, the first line specializes "f" which is a non-template
1850 member function, whilst the second line specializes "f" which
1851 is a template member function. So both lines are syntactically
1852 correct, and check_explicit_specialization does not reject
1855 Here, we can do better, as we are matching the specialization
1856 against the declarations. We count the number of template
1857 headers, and we check if they match TEMPLATE_COUNT + 1
1858 (TEMPLATE_COUNT is the number of qualifying template classes,
1859 plus there must be another header for the member template
1862 Notice that if header_count is zero, this is not a
1863 specialization but rather a template instantiation, so there
1864 is no check we can perform here. */
1865 if (header_count
&& header_count
!= template_count
+ 1)
1868 /* Check that the number of template arguments at the
1869 innermost level for DECL is the same as for FN. */
1870 if (current_binding_level
->kind
== sk_template_parms
1871 && !current_binding_level
->explicit_spec_p
1872 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1873 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1874 (current_template_parms
))))
1877 /* DECL might be a specialization of FN. */
1878 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1879 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1881 /* For a non-static member function, we need to make sure
1882 that the const qualification is the same. Since
1883 get_bindings does not try to merge the "this" parameter,
1884 we must do the comparison explicitly. */
1885 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1886 && !same_type_p (TREE_VALUE (fn_arg_types
),
1887 TREE_VALUE (decl_arg_types
)))
1890 /* Skip the "this" parameter and, for constructors of
1891 classes with virtual bases, the VTT parameter. A
1892 full specialization of a constructor will have a VTT
1893 parameter, but a template never will. */
1895 = skip_artificial_parms_for (decl
, decl_arg_types
);
1897 = skip_artificial_parms_for (fn
, fn_arg_types
);
1899 /* Function templates cannot be specializations; there are
1900 no partial specializations of functions. Therefore, if
1901 the type of DECL does not match FN, there is no
1903 if (tsk
== tsk_template
)
1905 if (compparms (fn_arg_types
, decl_arg_types
))
1906 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1910 /* See whether this function might be a specialization of this
1912 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1915 /* We cannot deduce template arguments that when used to
1916 specialize TMPL will produce DECL. */
1919 /* Make sure that the deduced arguments actually work. */
1920 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
1921 if (insttype
== error_mark_node
)
1924 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
1925 if (!compparms (fn_arg_types
, decl_arg_types
))
1928 /* Save this template, and the arguments deduced. */
1929 templates
= tree_cons (targs
, fn
, templates
);
1931 else if (need_member_template
)
1932 /* FN is an ordinary member function, and we need a
1933 specialization of a member template. */
1935 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1936 /* We can get IDENTIFIER_NODEs here in certain erroneous
1939 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1940 /* This is just an ordinary non-member function. Nothing can
1941 be a specialization of that. */
1943 else if (DECL_ARTIFICIAL (fn
))
1944 /* Cannot specialize functions that are created implicitly. */
1948 tree decl_arg_types
;
1950 /* This is an ordinary member function. However, since
1951 we're here, we can assume it's enclosing class is a
1952 template class. For example,
1954 template <typename T> struct S { void f(); };
1955 template <> void S<int>::f() {}
1957 Here, S<int>::f is a non-template, but S<int> is a
1958 template class. If FN has the same type as DECL, we
1959 might be in business. */
1961 if (!DECL_TEMPLATE_INFO (fn
))
1962 /* Its enclosing class is an explicit specialization
1963 of a template class. This is not a candidate. */
1966 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
1967 TREE_TYPE (TREE_TYPE (fn
))))
1968 /* The return types differ. */
1971 /* Adjust the type of DECL in case FN is a static member. */
1972 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1973 if (DECL_STATIC_FUNCTION_P (fn
)
1974 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1975 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1977 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1980 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1984 if (templates
&& TREE_CHAIN (templates
))
1990 It is possible for a specialization with a given function
1991 signature to be instantiated from more than one function
1992 template. In such cases, explicit specification of the
1993 template arguments must be used to uniquely identify the
1994 function template specialization being specialized.
1996 Note that here, there's no suggestion that we're supposed to
1997 determine which of the candidate templates is most
1998 specialized. However, we, also have:
2002 Partial ordering of overloaded function template
2003 declarations is used in the following contexts to select
2004 the function template to which a function template
2005 specialization refers:
2007 -- when an explicit specialization refers to a function
2010 So, we do use the partial ordering rules, at least for now.
2011 This extension can only serve to make invalid programs valid,
2012 so it's safe. And, there is strong anecdotal evidence that
2013 the committee intended the partial ordering rules to apply;
2014 the EDG front end has that behavior, and John Spicer claims
2015 that the committee simply forgot to delete the wording in
2016 [temp.expl.spec]. */
2017 tree tmpl
= most_specialized_instantiation (templates
);
2018 if (tmpl
!= error_mark_node
)
2021 TREE_CHAIN (templates
) = NULL_TREE
;
2025 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2027 error ("template-id %qD for %q+D does not match any template "
2028 "declaration", template_id
, decl
);
2029 if (header_count
&& header_count
!= template_count
+ 1)
2030 inform (input_location
, "saw %d %<template<>%>, need %d for "
2031 "specializing a member function template",
2032 header_count
, template_count
+ 1);
2033 return error_mark_node
;
2035 else if ((templates
&& TREE_CHAIN (templates
))
2036 || (candidates
&& TREE_CHAIN (candidates
))
2037 || (templates
&& candidates
))
2039 error ("ambiguous template specialization %qD for %q+D",
2041 candidates
= chainon (candidates
, templates
);
2042 print_candidates (candidates
);
2043 return error_mark_node
;
2046 /* We have one, and exactly one, match. */
2049 tree fn
= TREE_VALUE (candidates
);
2050 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2051 /* DECL is a re-declaration or partial instantiation of a template
2053 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2055 /* It was a specialization of an ordinary member function in a
2057 return DECL_TI_TEMPLATE (fn
);
2060 /* It was a specialization of a template. */
2061 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2062 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2064 *targs_out
= copy_node (targs
);
2065 SET_TMPL_ARGS_LEVEL (*targs_out
,
2066 TMPL_ARGS_DEPTH (*targs_out
),
2067 TREE_PURPOSE (templates
));
2070 *targs_out
= TREE_PURPOSE (templates
);
2071 return TREE_VALUE (templates
);
2074 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2075 but with the default argument values filled in from those in the
2079 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2082 tree new_spec_types
;
2087 if (spec_types
== void_list_node
)
2088 return void_list_node
;
2090 /* Substitute into the rest of the list. */
2092 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2093 TREE_CHAIN (tmpl_types
));
2095 /* Add the default argument for this parameter. */
2096 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2097 TREE_VALUE (spec_types
),
2101 /* DECL is an explicit specialization. Replicate default arguments
2102 from the template it specializes. (That way, code like:
2104 template <class T> void f(T = 3);
2105 template <> void f(double);
2108 works, as required.) An alternative approach would be to look up
2109 the correct default arguments at the call-site, but this approach
2110 is consistent with how implicit instantiations are handled. */
2113 copy_default_args_to_explicit_spec (tree decl
)
2118 tree new_spec_types
;
2122 tree object_type
= NULL_TREE
;
2123 tree in_charge
= NULL_TREE
;
2124 tree vtt
= NULL_TREE
;
2126 /* See if there's anything we need to do. */
2127 tmpl
= DECL_TI_TEMPLATE (decl
);
2128 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2129 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2130 if (TREE_PURPOSE (t
))
2135 old_type
= TREE_TYPE (decl
);
2136 spec_types
= TYPE_ARG_TYPES (old_type
);
2138 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2140 /* Remove the this pointer, but remember the object's type for
2142 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2143 spec_types
= TREE_CHAIN (spec_types
);
2144 tmpl_types
= TREE_CHAIN (tmpl_types
);
2146 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2148 /* DECL may contain more parameters than TMPL due to the extra
2149 in-charge parameter in constructors and destructors. */
2150 in_charge
= spec_types
;
2151 spec_types
= TREE_CHAIN (spec_types
);
2153 if (DECL_HAS_VTT_PARM_P (decl
))
2156 spec_types
= TREE_CHAIN (spec_types
);
2160 /* Compute the merged default arguments. */
2162 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2164 /* Compute the new FUNCTION_TYPE. */
2168 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2173 /* Put the in-charge parameter back. */
2174 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2175 TREE_VALUE (in_charge
),
2178 new_type
= build_method_type_directly (object_type
,
2179 TREE_TYPE (old_type
),
2183 new_type
= build_function_type (TREE_TYPE (old_type
),
2185 new_type
= cp_build_type_attribute_variant (new_type
,
2186 TYPE_ATTRIBUTES (old_type
));
2187 new_type
= build_exception_variant (new_type
,
2188 TYPE_RAISES_EXCEPTIONS (old_type
));
2189 TREE_TYPE (decl
) = new_type
;
2192 /* Check to see if the function just declared, as indicated in
2193 DECLARATOR, and in DECL, is a specialization of a function
2194 template. We may also discover that the declaration is an explicit
2195 instantiation at this point.
2197 Returns DECL, or an equivalent declaration that should be used
2198 instead if all goes well. Issues an error message if something is
2199 amiss. Returns error_mark_node if the error is not easily
2202 FLAGS is a bitmask consisting of the following flags:
2204 2: The function has a definition.
2205 4: The function is a friend.
2207 The TEMPLATE_COUNT is the number of references to qualifying
2208 template classes that appeared in the name of the function. For
2211 template <class T> struct S { void f(); };
2214 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2215 classes are not counted in the TEMPLATE_COUNT, so that in
2217 template <class T> struct S {};
2218 template <> struct S<int> { void f(); }
2219 template <> void S<int>::f();
2221 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2222 invalid; there should be no template <>.)
2224 If the function is a specialization, it is marked as such via
2225 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2226 is set up correctly, and it is added to the list of specializations
2227 for that template. */
2230 check_explicit_specialization (tree declarator
,
2235 int have_def
= flags
& 2;
2236 int is_friend
= flags
& 4;
2237 int specialization
= 0;
2238 int explicit_instantiation
= 0;
2239 int member_specialization
= 0;
2240 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2241 tree dname
= DECL_NAME (decl
);
2246 if (!processing_specialization
)
2249 tsk
= tsk_excessive_parms
;
2252 tsk
= current_tmpl_spec_kind (template_count
);
2257 if (processing_specialization
)
2260 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2262 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2265 /* This could be something like:
2267 template <class T> void f(T);
2268 class S { friend void f<>(int); } */
2272 /* This case handles bogus declarations like template <>
2273 template <class T> void f<int>(); */
2275 error ("template-id %qD in declaration of primary template",
2282 case tsk_invalid_member_spec
:
2283 /* The error has already been reported in
2284 check_specialization_scope. */
2285 return error_mark_node
;
2287 case tsk_invalid_expl_inst
:
2288 error ("template parameter list used in explicit instantiation");
2294 error ("definition provided for explicit instantiation");
2296 explicit_instantiation
= 1;
2299 case tsk_excessive_parms
:
2300 case tsk_insufficient_parms
:
2301 if (tsk
== tsk_excessive_parms
)
2302 error ("too many template parameter lists in declaration of %qD",
2304 else if (template_header_count
)
2305 error("too few template parameter lists in declaration of %qD", decl
);
2307 error("explicit specialization of %qD must be introduced by "
2308 "%<template <>%>", decl
);
2312 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2314 member_specialization
= 1;
2320 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2322 /* This case handles bogus declarations like template <>
2323 template <class T> void f<int>(); */
2325 if (uses_template_parms (declarator
))
2326 error ("function template partial specialization %qD "
2327 "is not allowed", declarator
);
2329 error ("template-id %qD in declaration of primary template",
2334 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2335 /* This is a specialization of a member template, without
2336 specialization the containing class. Something like:
2338 template <class T> struct S {
2339 template <class U> void f (U);
2341 template <> template <class U> void S<int>::f(U) {}
2343 That's a specialization -- but of the entire template. */
2351 if (specialization
|| member_specialization
)
2353 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2354 for (; t
; t
= TREE_CHAIN (t
))
2355 if (TREE_PURPOSE (t
))
2357 permerror (input_location
,
2358 "default argument specified in explicit specialization");
2363 if (specialization
|| member_specialization
|| explicit_instantiation
)
2365 tree tmpl
= NULL_TREE
;
2366 tree targs
= NULL_TREE
;
2368 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2369 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2373 gcc_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
);
2378 /* If there is no class context, the explicit instantiation
2379 must be at namespace scope. */
2380 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2382 /* Find the namespace binding, using the declaration
2384 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2386 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2388 error ("%qD is not a template function", dname
);
2389 fns
= error_mark_node
;
2393 tree fn
= OVL_CURRENT (fns
);
2394 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2395 CP_DECL_CONTEXT (fn
)))
2396 error ("%qD is not declared in %qD",
2397 decl
, current_namespace
);
2401 declarator
= lookup_template_function (fns
, NULL_TREE
);
2404 if (declarator
== error_mark_node
)
2405 return error_mark_node
;
2407 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2409 if (!explicit_instantiation
)
2410 /* A specialization in class scope. This is invalid,
2411 but the error will already have been flagged by
2412 check_specialization_scope. */
2413 return error_mark_node
;
2416 /* It's not valid to write an explicit instantiation in
2419 class C { template void f(); }
2421 This case is caught by the parser. However, on
2424 template class C { void f(); };
2426 (which is invalid) we can get here. The error will be
2433 else if (ctype
!= NULL_TREE
2434 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
2437 /* Find the list of functions in ctype that have the same
2438 name as the declared function. */
2439 tree name
= TREE_OPERAND (declarator
, 0);
2440 tree fns
= NULL_TREE
;
2443 if (constructor_name_p (name
, ctype
))
2445 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2447 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2448 : !CLASSTYPE_DESTRUCTORS (ctype
))
2450 /* From [temp.expl.spec]:
2452 If such an explicit specialization for the member
2453 of a class template names an implicitly-declared
2454 special member function (clause _special_), the
2455 program is ill-formed.
2457 Similar language is found in [temp.explicit]. */
2458 error ("specialization of implicitly-declared special member function");
2459 return error_mark_node
;
2462 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2465 if (!DECL_CONV_FN_P (decl
))
2467 idx
= lookup_fnfields_1 (ctype
, name
);
2469 fns
= VEC_index (tree
, CLASSTYPE_METHOD_VEC (ctype
), idx
);
2473 VEC(tree
,gc
) *methods
;
2476 /* For a type-conversion operator, we cannot do a
2477 name-based lookup. We might be looking for `operator
2478 int' which will be a specialization of `operator T'.
2479 So, we find *all* the conversion operators, and then
2480 select from them. */
2483 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2485 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2486 VEC_iterate (tree
, methods
, idx
, ovl
);
2489 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2490 /* There are no more conversion functions. */
2493 /* Glue all these conversion functions together
2494 with those we already have. */
2495 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2496 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2500 if (fns
== NULL_TREE
)
2502 error ("no member function %qD declared in %qT", name
, ctype
);
2503 return error_mark_node
;
2506 TREE_OPERAND (declarator
, 0) = fns
;
2509 /* Figure out what exactly is being specialized at this point.
2510 Note that for an explicit instantiation, even one for a
2511 member function, we cannot tell apriori whether the
2512 instantiation is for a member template, or just a member
2513 function of a template class. Even if a member template is
2514 being instantiated, the member template arguments may be
2515 elided if they can be deduced from the rest of the
2517 tmpl
= determine_specialization (declarator
, decl
,
2519 member_specialization
,
2523 if (!tmpl
|| tmpl
== error_mark_node
)
2524 /* We couldn't figure out what this declaration was
2526 return error_mark_node
;
2529 tree gen_tmpl
= most_general_template (tmpl
);
2531 if (explicit_instantiation
)
2533 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2534 is done by do_decl_instantiation later. */
2536 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2537 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2539 if (arg_depth
> parm_depth
)
2541 /* If TMPL is not the most general template (for
2542 example, if TMPL is a friend template that is
2543 injected into namespace scope), then there will
2544 be too many levels of TARGS. Remove some of them
2549 new_targs
= make_tree_vec (parm_depth
);
2550 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2551 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2552 = TREE_VEC_ELT (targs
, i
);
2556 return instantiate_template (tmpl
, targs
, tf_error
);
2559 /* If we thought that the DECL was a member function, but it
2560 turns out to be specializing a static member function,
2561 make DECL a static member function as well. */
2562 if (DECL_STATIC_FUNCTION_P (tmpl
)
2563 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2564 revert_static_member_fn (decl
);
2566 /* If this is a specialization of a member template of a
2567 template class, we want to return the TEMPLATE_DECL, not
2568 the specialization of it. */
2569 if (tsk
== tsk_template
)
2571 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2572 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2573 DECL_INITIAL (result
) = NULL_TREE
;
2577 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2578 DECL_SOURCE_LOCATION (result
)
2579 = DECL_SOURCE_LOCATION (decl
);
2580 /* We want to use the argument list specified in the
2581 definition, not in the original declaration. */
2582 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2583 for (parm
= DECL_ARGUMENTS (result
); parm
;
2584 parm
= DECL_CHAIN (parm
))
2585 DECL_CONTEXT (parm
) = result
;
2587 return register_specialization (tmpl
, gen_tmpl
, targs
,
2591 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2592 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2594 /* Inherit default function arguments from the template
2595 DECL is specializing. */
2596 copy_default_args_to_explicit_spec (decl
);
2598 /* This specialization has the same protection as the
2599 template it specializes. */
2600 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2601 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2603 /* 7.1.1-1 [dcl.stc]
2605 A storage-class-specifier shall not be specified in an
2606 explicit specialization...
2608 The parser rejects these, so unless action is taken here,
2609 explicit function specializations will always appear with
2612 The action recommended by the C++ CWG in response to C++
2613 defect report 605 is to make the storage class and linkage
2614 of the explicit specialization match the templated function:
2616 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2618 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2620 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2621 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2623 /* This specialization has the same linkage and visibility as
2624 the function template it specializes. */
2625 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2626 if (! TREE_PUBLIC (decl
))
2628 DECL_INTERFACE_KNOWN (decl
) = 1;
2629 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2631 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2632 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2634 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2635 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2639 /* If DECL is a friend declaration, declared using an
2640 unqualified name, the namespace associated with DECL may
2641 have been set incorrectly. For example, in:
2643 template <typename T> void f(T);
2645 struct S { friend void f<int>(int); }
2648 we will have set the DECL_CONTEXT for the friend
2649 declaration to N, rather than to the global namespace. */
2650 if (DECL_NAMESPACE_SCOPE_P (decl
))
2651 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2653 if (is_friend
&& !have_def
)
2654 /* This is not really a declaration of a specialization.
2655 It's just the name of an instantiation. But, it's not
2656 a request for an instantiation, either. */
2657 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2658 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2659 /* This is indeed a specialization. In case of constructors
2660 and destructors, we need in-charge and not-in-charge
2661 versions in V3 ABI. */
2662 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2664 /* Register this specialization so that we can find it
2666 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2673 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2674 parameters. These are represented in the same format used for
2675 DECL_TEMPLATE_PARMS. */
2678 comp_template_parms (const_tree parms1
, const_tree parms2
)
2683 if (parms1
== parms2
)
2686 for (p1
= parms1
, p2
= parms2
;
2687 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2688 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2690 tree t1
= TREE_VALUE (p1
);
2691 tree t2
= TREE_VALUE (p2
);
2694 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2695 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2697 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2700 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2702 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2703 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2705 /* If either of the template parameters are invalid, assume
2706 they match for the sake of error recovery. */
2707 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2710 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2713 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2714 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2715 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2717 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2722 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2723 /* One set of parameters has more parameters lists than the
2730 /* Determine whether PARM is a parameter pack. */
2733 template_parameter_pack_p (const_tree parm
)
2735 /* Determine if we have a non-type template parameter pack. */
2736 if (TREE_CODE (parm
) == PARM_DECL
)
2737 return (DECL_TEMPLATE_PARM_P (parm
)
2738 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2739 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
2740 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
2742 /* If this is a list of template parameters, we could get a
2743 TYPE_DECL or a TEMPLATE_DECL. */
2744 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2745 parm
= TREE_TYPE (parm
);
2747 /* Otherwise it must be a type template parameter. */
2748 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2749 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2750 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2753 /* Determine if T is a function parameter pack. */
2756 function_parameter_pack_p (const_tree t
)
2758 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2759 return FUNCTION_PARAMETER_PACK_P (t
);
2763 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2764 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2767 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2769 if (! primary_func_tmpl_inst
2770 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2771 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2774 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2777 /* Return true iff the function parameter PARAM_DECL was expanded
2778 from the function parameter pack PACK. */
2781 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
2783 if (DECL_ARTIFICIAL (param_decl
)
2784 || !function_parameter_pack_p (pack
))
2787 /* The parameter pack and its pack arguments have the same
2789 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
2792 /* Determine whether ARGS describes a variadic template args list,
2793 i.e., one that is terminated by a template argument pack. */
2796 template_args_variadic_p (tree args
)
2801 if (args
== NULL_TREE
)
2804 args
= INNERMOST_TEMPLATE_ARGS (args
);
2805 nargs
= TREE_VEC_LENGTH (args
);
2810 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2812 return ARGUMENT_PACK_P (last_parm
);
2815 /* Generate a new name for the parameter pack name NAME (an
2816 IDENTIFIER_NODE) that incorporates its */
2819 make_ith_pack_parameter_name (tree name
, int i
)
2821 /* Munge the name to include the parameter index. */
2822 #define NUMBUF_LEN 128
2823 char numbuf
[NUMBUF_LEN
];
2827 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
2828 newname_len
= IDENTIFIER_LENGTH (name
)
2829 + strlen (numbuf
) + 2;
2830 newname
= (char*)alloca (newname_len
);
2831 snprintf (newname
, newname_len
,
2832 "%s#%i", IDENTIFIER_POINTER (name
), i
);
2833 return get_identifier (newname
);
2836 /* Return true if T is a primary function, class or alias template
2840 primary_template_instantiation_p (const_tree t
)
2845 if (TREE_CODE (t
) == FUNCTION_DECL
)
2846 return DECL_LANG_SPECIFIC (t
)
2847 && DECL_TEMPLATE_INSTANTIATION (t
)
2848 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
2849 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
2850 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
2851 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
2853 && TYPE_TEMPLATE_INFO (t
)
2854 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t
))
2855 && DECL_TEMPLATE_INSTANTIATION (TYPE_NAME (t
)))
2860 /* Return true if PARM is a template template parameter. */
2863 template_template_parameter_p (const_tree parm
)
2865 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
2868 /* Return true iff PARM is a DECL representing a type template
2872 template_type_parameter_p (const_tree parm
)
2875 && (TREE_CODE (parm
) == TYPE_DECL
2876 || TREE_CODE (parm
) == TEMPLATE_DECL
)
2877 && DECL_TEMPLATE_PARM_P (parm
));
2880 /* Return the template parameters of T if T is a
2881 primary template instantiation, NULL otherwise. */
2884 get_primary_template_innermost_parameters (const_tree t
)
2886 tree parms
= NULL
, template_info
= NULL
;
2888 if ((template_info
= get_template_info (t
))
2889 && primary_template_instantiation_p (t
))
2890 parms
= INNERMOST_TEMPLATE_PARMS
2891 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
2896 /* Return the template parameters of the LEVELth level from the full list
2897 of template parameters PARMS. */
2900 get_template_parms_at_level (tree parms
, int level
)
2904 || TREE_CODE (parms
) != TREE_LIST
2905 || level
> TMPL_PARMS_DEPTH (parms
))
2908 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
2909 if (TMPL_PARMS_DEPTH (p
) == level
)
2915 /* Returns the template arguments of T if T is a template instantiation,
2919 get_template_innermost_arguments (const_tree t
)
2921 tree args
= NULL
, template_info
= NULL
;
2923 if ((template_info
= get_template_info (t
))
2924 && TI_ARGS (template_info
))
2925 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
2930 /* Return the argument pack elements of T if T is a template argument pack,
2934 get_template_argument_pack_elems (const_tree t
)
2936 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
2937 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
2940 return ARGUMENT_PACK_ARGS (t
);
2943 /* Structure used to track the progress of find_parameter_packs_r. */
2944 struct find_parameter_pack_data
2946 /* TREE_LIST that will contain all of the parameter packs found by
2948 tree
* parameter_packs
;
2950 /* Set of AST nodes that have been visited by the traversal. */
2951 struct pointer_set_t
*visited
;
2954 /* Identifies all of the argument packs that occur in a template
2955 argument and appends them to the TREE_LIST inside DATA, which is a
2956 find_parameter_pack_data structure. This is a subroutine of
2957 make_pack_expansion and uses_parameter_packs. */
2959 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
2962 struct find_parameter_pack_data
* ppd
=
2963 (struct find_parameter_pack_data
*)data
;
2964 bool parameter_pack_p
= false;
2966 /* Handle type aliases/typedefs. */
2969 && TREE_CODE (TYPE_NAME (t
)) == TYPE_DECL
2970 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
2972 if (TYPE_TEMPLATE_INFO (t
))
2973 cp_walk_tree (&TYPE_TI_ARGS (t
),
2974 &find_parameter_packs_r
,
2980 /* Identify whether this is a parameter pack or not. */
2981 switch (TREE_CODE (t
))
2983 case TEMPLATE_PARM_INDEX
:
2984 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
2985 parameter_pack_p
= true;
2988 case TEMPLATE_TYPE_PARM
:
2989 t
= TYPE_MAIN_VARIANT (t
);
2990 case TEMPLATE_TEMPLATE_PARM
:
2991 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
2992 parameter_pack_p
= true;
2996 if (FUNCTION_PARAMETER_PACK_P (t
))
2998 /* We don't want to walk into the type of a PARM_DECL,
2999 because we don't want to see the type parameter pack. */
3001 parameter_pack_p
= true;
3006 parameter_pack_p
= true;
3009 /* Not a parameter pack. */
3013 if (parameter_pack_p
)
3015 /* Add this parameter pack to the list. */
3016 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3020 cp_walk_tree (&TYPE_CONTEXT (t
),
3021 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3023 /* This switch statement will return immediately if we don't find a
3025 switch (TREE_CODE (t
))
3027 case TEMPLATE_PARM_INDEX
:
3030 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3031 /* Check the template itself. */
3032 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3033 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3034 /* Check the template arguments. */
3035 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3040 case TEMPLATE_TYPE_PARM
:
3041 case TEMPLATE_TEMPLATE_PARM
:
3048 if (TYPE_PTRMEMFUNC_P (t
))
3054 if (TYPE_TEMPLATE_INFO (t
))
3055 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
3056 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3063 cp_walk_tree (&TREE_TYPE (t
),
3064 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3068 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3073 case TYPE_PACK_EXPANSION
:
3074 case EXPR_PACK_EXPANSION
:
3079 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3084 case IDENTIFIER_NODE
:
3085 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3097 /* Determines if the expression or type T uses any parameter packs. */
3099 uses_parameter_packs (tree t
)
3101 tree parameter_packs
= NULL_TREE
;
3102 struct find_parameter_pack_data ppd
;
3103 ppd
.parameter_packs
= ¶meter_packs
;
3104 ppd
.visited
= pointer_set_create ();
3105 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3106 pointer_set_destroy (ppd
.visited
);
3107 return parameter_packs
!= NULL_TREE
;
3110 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3111 representation a base-class initializer into a parameter pack
3112 expansion. If all goes well, the resulting node will be an
3113 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3116 make_pack_expansion (tree arg
)
3119 tree parameter_packs
= NULL_TREE
;
3120 bool for_types
= false;
3121 struct find_parameter_pack_data ppd
;
3123 if (!arg
|| arg
== error_mark_node
)
3126 if (TREE_CODE (arg
) == TREE_LIST
)
3128 /* The only time we will see a TREE_LIST here is for a base
3129 class initializer. In this case, the TREE_PURPOSE will be a
3130 _TYPE node (representing the base class expansion we're
3131 initializing) and the TREE_VALUE will be a TREE_LIST
3132 containing the initialization arguments.
3134 The resulting expansion looks somewhat different from most
3135 expansions. Rather than returning just one _EXPANSION, we
3136 return a TREE_LIST whose TREE_PURPOSE is a
3137 TYPE_PACK_EXPANSION containing the bases that will be
3138 initialized. The TREE_VALUE will be identical to the
3139 original TREE_VALUE, which is a list of arguments that will
3140 be passed to each base. We do not introduce any new pack
3141 expansion nodes into the TREE_VALUE (although it is possible
3142 that some already exist), because the TREE_PURPOSE and
3143 TREE_VALUE all need to be expanded together with the same
3144 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3145 resulting TREE_PURPOSE will mention the parameter packs in
3146 both the bases and the arguments to the bases. */
3149 tree parameter_packs
= NULL_TREE
;
3151 /* Determine which parameter packs will be used by the base
3153 ppd
.visited
= pointer_set_create ();
3154 ppd
.parameter_packs
= ¶meter_packs
;
3155 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3158 if (parameter_packs
== NULL_TREE
)
3160 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3161 pointer_set_destroy (ppd
.visited
);
3162 return error_mark_node
;
3165 if (TREE_VALUE (arg
) != void_type_node
)
3167 /* Collect the sets of parameter packs used in each of the
3168 initialization arguments. */
3169 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3171 /* Determine which parameter packs will be expanded in this
3173 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3178 pointer_set_destroy (ppd
.visited
);
3180 /* Create the pack expansion type for the base type. */
3181 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3182 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3183 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3185 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3186 they will rarely be compared to anything. */
3187 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3189 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3192 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3195 /* Build the PACK_EXPANSION_* node. */
3197 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3198 : make_node (EXPR_PACK_EXPANSION
);
3199 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3200 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3202 /* Propagate type and const-expression information. */
3203 TREE_TYPE (result
) = TREE_TYPE (arg
);
3204 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3207 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3208 they will rarely be compared to anything. */
3209 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3211 /* Determine which parameter packs will be expanded. */
3212 ppd
.parameter_packs
= ¶meter_packs
;
3213 ppd
.visited
= pointer_set_create ();
3214 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3215 pointer_set_destroy (ppd
.visited
);
3217 /* Make sure we found some parameter packs. */
3218 if (parameter_packs
== NULL_TREE
)
3221 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3223 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3224 return error_mark_node
;
3226 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3228 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3233 /* Checks T for any "bare" parameter packs, which have not yet been
3234 expanded, and issues an error if any are found. This operation can
3235 only be done on full expressions or types (e.g., an expression
3236 statement, "if" condition, etc.), because we could have expressions like:
3238 foo(f(g(h(args)))...)
3240 where "args" is a parameter pack. check_for_bare_parameter_packs
3241 should not be called for the subexpressions args, h(args),
3242 g(h(args)), or f(g(h(args))), because we would produce erroneous
3245 Returns TRUE and emits an error if there were bare parameter packs,
3246 returns FALSE otherwise. */
3248 check_for_bare_parameter_packs (tree t
)
3250 tree parameter_packs
= NULL_TREE
;
3251 struct find_parameter_pack_data ppd
;
3253 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3256 if (TREE_CODE (t
) == TYPE_DECL
)
3259 ppd
.parameter_packs
= ¶meter_packs
;
3260 ppd
.visited
= pointer_set_create ();
3261 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3262 pointer_set_destroy (ppd
.visited
);
3264 if (parameter_packs
)
3266 error ("parameter packs not expanded with %<...%>:");
3267 while (parameter_packs
)
3269 tree pack
= TREE_VALUE (parameter_packs
);
3270 tree name
= NULL_TREE
;
3272 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3273 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3274 name
= TYPE_NAME (pack
);
3275 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3276 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3278 name
= DECL_NAME (pack
);
3281 inform (input_location
, " %qD", name
);
3283 inform (input_location
, " <anonymous>");
3285 parameter_packs
= TREE_CHAIN (parameter_packs
);
3294 /* Expand any parameter packs that occur in the template arguments in
3297 expand_template_argument_pack (tree args
)
3299 tree result_args
= NULL_TREE
;
3300 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3301 int num_result_args
= -1;
3302 int non_default_args_count
= -1;
3304 /* First, determine if we need to expand anything, and the number of
3305 slots we'll need. */
3306 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3308 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3309 if (arg
== NULL_TREE
)
3311 if (ARGUMENT_PACK_P (arg
))
3313 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3314 if (num_result_args
< 0)
3315 num_result_args
= in_arg
+ num_packed
;
3317 num_result_args
+= num_packed
;
3321 if (num_result_args
>= 0)
3326 /* If no expansion is necessary, we're done. */
3327 if (num_result_args
< 0)
3330 /* Expand arguments. */
3331 result_args
= make_tree_vec (num_result_args
);
3332 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3333 non_default_args_count
=
3334 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3335 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3337 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3338 if (ARGUMENT_PACK_P (arg
))
3340 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3341 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3342 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3343 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3344 if (non_default_args_count
> 0)
3345 non_default_args_count
+= num_packed
;
3349 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3353 if (non_default_args_count
>= 0)
3354 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3358 /* Checks if DECL shadows a template parameter.
3360 [temp.local]: A template-parameter shall not be redeclared within its
3361 scope (including nested scopes).
3363 Emits an error and returns TRUE if the DECL shadows a parameter,
3364 returns FALSE otherwise. */
3367 check_template_shadow (tree decl
)
3371 /* If we're not in a template, we can't possibly shadow a template
3373 if (!current_template_parms
)
3376 /* Figure out what we're shadowing. */
3377 if (TREE_CODE (decl
) == OVERLOAD
)
3378 decl
= OVL_CURRENT (decl
);
3379 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3381 /* If there's no previous binding for this name, we're not shadowing
3382 anything, let alone a template parameter. */
3386 /* If we're not shadowing a template parameter, we're done. Note
3387 that OLDDECL might be an OVERLOAD (or perhaps even an
3388 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3390 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3393 /* We check for decl != olddecl to avoid bogus errors for using a
3394 name inside a class. We check TPFI to avoid duplicate errors for
3395 inline member templates. */
3397 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
3400 error ("declaration of %q+#D", decl
);
3401 error (" shadows template parm %q+#D", olddecl
);
3405 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3406 ORIG_LEVEL, DECL, and TYPE. */
3409 build_template_parm_index (int index
,
3415 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3416 TEMPLATE_PARM_IDX (t
) = index
;
3417 TEMPLATE_PARM_LEVEL (t
) = level
;
3418 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3419 TEMPLATE_PARM_DECL (t
) = decl
;
3420 TREE_TYPE (t
) = type
;
3421 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3422 TREE_READONLY (t
) = TREE_READONLY (decl
);
3427 /* Find the canonical type parameter for the given template type
3428 parameter. Returns the canonical type parameter, which may be TYPE
3429 if no such parameter existed. */
3432 canonical_type_parameter (tree type
)
3435 int idx
= TEMPLATE_TYPE_IDX (type
);
3436 if (!canonical_template_parms
)
3437 canonical_template_parms
= VEC_alloc (tree
, gc
, idx
+1);
3439 while (VEC_length (tree
, canonical_template_parms
) <= (unsigned)idx
)
3440 VEC_safe_push (tree
, gc
, canonical_template_parms
, NULL_TREE
);
3442 list
= VEC_index (tree
, canonical_template_parms
, idx
);
3443 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3444 list
= TREE_CHAIN (list
);
3447 return TREE_VALUE (list
);
3450 VEC_replace(tree
, canonical_template_parms
, idx
,
3451 tree_cons (NULL_TREE
, type
,
3452 VEC_index (tree
, canonical_template_parms
, idx
)));
3457 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3458 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3459 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3460 new one is created. */
3463 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3464 tsubst_flags_t complain
)
3466 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3467 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3468 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3469 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3471 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3474 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3475 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3476 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3477 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3478 DECL_ARTIFICIAL (decl
) = 1;
3479 SET_DECL_TEMPLATE_PARM_P (decl
);
3481 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3482 TEMPLATE_PARM_LEVEL (index
) - levels
,
3483 TEMPLATE_PARM_ORIG_LEVEL (index
),
3485 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3486 TEMPLATE_PARM_PARAMETER_PACK (t
)
3487 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3489 /* Template template parameters need this. */
3490 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3491 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3492 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3496 return TEMPLATE_PARM_DESCENDANTS (index
);
3499 /* Process information from new template parameter PARM and append it
3500 to the LIST being built. This new parameter is a non-type
3501 parameter iff IS_NON_TYPE is true. This new parameter is a
3502 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3503 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3504 parameter list PARM belongs to. This is used used to create a
3505 proper canonical type for the type of PARM that is to be created,
3506 iff PARM is a type. If the size is not known, this parameter shall
3510 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
3511 bool is_non_type
, bool is_parameter_pack
)
3518 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3519 defval
= TREE_PURPOSE (parm
);
3523 tree p
= tree_last (list
);
3525 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3528 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3529 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3531 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3541 parm
= TREE_VALUE (parm
);
3543 SET_DECL_TEMPLATE_PARM_P (parm
);
3545 if (TREE_TYPE (parm
) == error_mark_node
)
3547 err_parm_list
= build_tree_list (defval
, parm
);
3548 TREE_VALUE (err_parm_list
) = error_mark_node
;
3549 return chainon (list
, err_parm_list
);
3555 The top-level cv-qualifiers on the template-parameter are
3556 ignored when determining its type. */
3557 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3558 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3560 err_parm_list
= build_tree_list (defval
, parm
);
3561 TREE_VALUE (err_parm_list
) = error_mark_node
;
3562 return chainon (list
, err_parm_list
);
3565 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
)
3567 /* This template parameter is not a parameter pack, but it
3568 should be. Complain about "bare" parameter packs. */
3569 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3571 /* Recover by calling this a parameter pack. */
3572 is_parameter_pack
= true;
3576 /* A template parameter is not modifiable. */
3577 TREE_CONSTANT (parm
) = 1;
3578 TREE_READONLY (parm
) = 1;
3579 decl
= build_decl (parm_loc
,
3580 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3581 TREE_CONSTANT (decl
) = 1;
3582 TREE_READONLY (decl
) = 1;
3583 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3584 = build_template_parm_index (idx
, processing_template_decl
,
3585 processing_template_decl
,
3586 decl
, TREE_TYPE (parm
));
3588 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3589 = is_parameter_pack
;
3594 parm
= TREE_VALUE (TREE_VALUE (parm
));
3596 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3598 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3599 /* This is for distinguishing between real templates and template
3600 template parameters */
3601 TREE_TYPE (parm
) = t
;
3602 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3607 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3608 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3609 decl
= build_decl (parm_loc
,
3610 TYPE_DECL
, parm
, t
);
3613 TYPE_NAME (t
) = decl
;
3614 TYPE_STUB_DECL (t
) = decl
;
3616 TEMPLATE_TYPE_PARM_INDEX (t
)
3617 = build_template_parm_index (idx
, processing_template_decl
,
3618 processing_template_decl
,
3619 decl
, TREE_TYPE (parm
));
3620 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3621 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3623 DECL_ARTIFICIAL (decl
) = 1;
3624 SET_DECL_TEMPLATE_PARM_P (decl
);
3626 parm
= build_tree_list (defval
, parm
);
3627 return chainon (list
, parm
);
3630 /* The end of a template parameter list has been reached. Process the
3631 tree list into a parameter vector, converting each parameter into a more
3632 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3636 end_template_parm_list (tree parms
)
3640 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3642 current_template_parms
3643 = tree_cons (size_int (processing_template_decl
),
3644 saved_parmlist
, current_template_parms
);
3646 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3648 next
= TREE_CHAIN (parm
);
3649 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3650 TREE_CHAIN (parm
) = NULL_TREE
;
3653 --processing_template_parmlist
;
3655 return saved_parmlist
;
3658 /* end_template_decl is called after a template declaration is seen. */
3661 end_template_decl (void)
3663 reset_specialization ();
3665 if (! processing_template_decl
)
3668 /* This matches the pushlevel in begin_template_parm_list. */
3671 --processing_template_decl
;
3672 current_template_parms
= TREE_CHAIN (current_template_parms
);
3675 /* Takes a TREE_LIST representing a template parameter and convert it
3676 into an argument suitable to be passed to the type substitution
3677 functions. Note that If the TREE_LIST contains an error_mark
3678 node, the returned argument is error_mark_node. */
3681 template_parm_to_arg (tree t
)
3685 || TREE_CODE (t
) != TREE_LIST
)
3688 if (error_operand_p (TREE_VALUE (t
)))
3689 return error_mark_node
;
3693 if (TREE_CODE (t
) == TYPE_DECL
3694 || TREE_CODE (t
) == TEMPLATE_DECL
)
3698 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3700 /* Turn this argument into a TYPE_ARGUMENT_PACK
3701 with a single element, which expands T. */
3702 tree vec
= make_tree_vec (1);
3703 #ifdef ENABLE_CHECKING
3704 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3705 (vec
, TREE_VEC_LENGTH (vec
));
3707 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3709 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
3710 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3715 t
= DECL_INITIAL (t
);
3717 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3719 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3720 with a single element, which expands T. */
3721 tree vec
= make_tree_vec (1);
3722 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3723 #ifdef ENABLE_CHECKING
3724 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3725 (vec
, TREE_VEC_LENGTH (vec
));
3727 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3729 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3730 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3731 TREE_TYPE (t
) = type
;
3737 /* This function returns TRUE if PARM_PACK is a template parameter
3738 pack and if ARG_PACK is what template_parm_to_arg returned when
3739 passed PARM_PACK. */
3742 arg_from_parm_pack_p (tree arg_pack
, tree parm_pack
)
3744 /* For clarity in the comments below let's use the representation
3745 argument_pack<elements>' to denote an argument pack and its
3748 In the 'if' block below, we want to detect cases where
3749 ARG_PACK is argument_pack<PARM_PACK...>. I.e, we want to
3750 check if ARG_PACK is an argument pack which sole element is
3751 the expansion of PARM_PACK. That argument pack is typically
3752 created by template_parm_to_arg when passed a parameter
3756 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
)) == 1
3757 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0)))
3759 tree expansion
= TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0);
3760 tree pattern
= PACK_EXPANSION_PATTERN (expansion
);
3761 if ((TYPE_P (pattern
) && same_type_p (pattern
, parm_pack
))
3762 || (!TYPE_P (pattern
) && cp_tree_equal (parm_pack
, pattern
)))
3763 /* The argument pack that the parameter maps to is just an
3764 expansion of the parameter itself, such as one would
3765 find in the implicit typedef of a class inside the
3766 class itself. Consider this parameter "unsubstituted",
3767 so that we will maintain the outer pack expansion. */
3773 /* Within the declaration of a template, return all levels of template
3774 parameters that apply. The template parameters are represented as
3775 a TREE_VEC, in the form documented in cp-tree.h for template
3779 current_template_args (void)
3782 tree args
= NULL_TREE
;
3783 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
3786 /* If there is only one level of template parameters, we do not
3787 create a TREE_VEC of TREE_VECs. Instead, we return a single
3788 TREE_VEC containing the arguments. */
3790 args
= make_tree_vec (length
);
3792 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
3794 tree a
= copy_node (TREE_VALUE (header
));
3797 TREE_TYPE (a
) = NULL_TREE
;
3798 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
3799 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
3801 #ifdef ENABLE_CHECKING
3802 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
3806 TREE_VEC_ELT (args
, --l
) = a
;
3811 if (length
> 1 && TREE_VEC_ELT (args
, 0) == NULL_TREE
)
3812 /* This can happen for template parms of a template template
3815 template<template<class T, class U> class TT> struct S;
3817 Consider the level of the parms of TT; T and U both have
3818 level 2; TT has no template parm of level 1. So in this case
3819 the first element of full_template_args is NULL_TREE. If we
3820 leave it like this TMPL_ARG_DEPTH on args returns 1 instead
3821 of 2. This will make tsubst wrongly consider that T and U
3822 have level 1. Instead, let's create a dummy vector as the
3823 first element of full_template_args so that TMPL_ARG_DEPTH
3824 returns the correct depth for args. */
3825 TREE_VEC_ELT (args
, 0) = make_tree_vec (1);
3829 /* Update the declared TYPE by doing any lookups which were thought to be
3830 dependent, but are not now that we know the SCOPE of the declarator. */
3833 maybe_update_decl_type (tree orig_type
, tree scope
)
3835 tree type
= orig_type
;
3837 if (type
== NULL_TREE
)
3840 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3841 type
= TREE_TYPE (type
);
3843 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
3844 && dependent_type_p (type
)
3845 /* Don't bother building up the args in this case. */
3846 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
3848 /* tsubst in the args corresponding to the template parameters,
3849 including auto if present. Most things will be unchanged, but
3850 make_typename_type and tsubst_qualified_id will resolve
3851 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3852 tree args
= current_template_args ();
3853 tree auto_node
= type_uses_auto (type
);
3857 tree auto_vec
= make_tree_vec (1);
3858 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
3859 args
= add_to_template_args (args
, auto_vec
);
3861 pushed
= push_scope (scope
);
3862 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
3867 if (type
== error_mark_node
)
3870 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3872 if (same_type_p (type
, TREE_TYPE (orig_type
)))
3875 type
= TYPE_NAME (type
);
3880 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3881 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3882 a member template. Used by push_template_decl below. */
3885 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
3887 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
3888 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
3889 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
3890 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3891 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
3896 struct template_parm_data
3898 /* The level of the template parameters we are currently
3902 /* The index of the specialization argument we are currently
3906 /* An array whose size is the number of template parameters. The
3907 elements are nonzero if the parameter has been used in any one
3908 of the arguments processed so far. */
3911 /* An array whose size is the number of template arguments. The
3912 elements are nonzero if the argument makes use of template
3913 parameters of this level. */
3914 int* arg_uses_template_parms
;
3917 /* Subroutine of push_template_decl used to see if each template
3918 parameter in a partial specialization is used in the explicit
3919 argument list. If T is of the LEVEL given in DATA (which is
3920 treated as a template_parm_data*), then DATA->PARMS is marked
3924 mark_template_parm (tree t
, void* data
)
3928 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
3930 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
3932 level
= TEMPLATE_PARM_LEVEL (t
);
3933 idx
= TEMPLATE_PARM_IDX (t
);
3937 level
= TEMPLATE_TYPE_LEVEL (t
);
3938 idx
= TEMPLATE_TYPE_IDX (t
);
3941 if (level
== tpd
->level
)
3943 tpd
->parms
[idx
] = 1;
3944 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
3947 /* Return zero so that for_each_template_parm will continue the
3948 traversal of the tree; we want to mark *every* template parm. */
3952 /* Process the partial specialization DECL. */
3955 process_partial_specialization (tree decl
)
3957 tree type
= TREE_TYPE (decl
);
3958 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
3959 tree specargs
= CLASSTYPE_TI_ARGS (type
);
3960 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
3961 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
3964 int nargs
= TREE_VEC_LENGTH (inner_args
);
3967 bool did_error_intro
= false;
3968 struct template_parm_data tpd
;
3969 struct template_parm_data tpd2
;
3971 gcc_assert (current_template_parms
);
3973 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
3974 ntparms
= TREE_VEC_LENGTH (inner_parms
);
3976 /* We check that each of the template parameters given in the
3977 partial specialization is used in the argument list to the
3978 specialization. For example:
3980 template <class T> struct S;
3981 template <class T> struct S<T*>;
3983 The second declaration is OK because `T*' uses the template
3984 parameter T, whereas
3986 template <class T> struct S<int>;
3988 is no good. Even trickier is:
3999 The S2<T> declaration is actually invalid; it is a
4000 full-specialization. Of course,
4003 struct S2<T (*)(U)>;
4005 or some such would have been OK. */
4006 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4007 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4008 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4010 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4011 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4012 for (i
= 0; i
< nargs
; ++i
)
4014 tpd
.current_arg
= i
;
4015 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4016 &mark_template_parm
,
4019 /*include_nondeduced_p=*/false);
4021 for (i
= 0; i
< ntparms
; ++i
)
4022 if (tpd
.parms
[i
] == 0)
4024 /* One of the template parms was not used in the
4026 if (!did_error_intro
)
4028 error ("template parameters not used in partial specialization:");
4029 did_error_intro
= true;
4032 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4035 if (did_error_intro
)
4036 return error_mark_node
;
4038 /* [temp.class.spec]
4040 The argument list of the specialization shall not be identical to
4041 the implicit argument list of the primary template. */
4042 if (comp_template_args
4044 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4046 error ("partial specialization %qT does not specialize any template arguments", type
);
4048 /* A partial specialization that replaces multiple parameters of the
4049 primary template with a pack expansion is less specialized for those
4051 if (nargs
< DECL_NTPARMS (maintmpl
))
4053 error ("partial specialization is not more specialized than the "
4054 "primary template because it replaces multiple parameters "
4055 "with a pack expansion");
4056 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4060 /* [temp.class.spec]
4062 A partially specialized non-type argument expression shall not
4063 involve template parameters of the partial specialization except
4064 when the argument expression is a simple identifier.
4066 The type of a template parameter corresponding to a specialized
4067 non-type argument shall not be dependent on a parameter of the
4070 Also, we verify that pack expansions only occur at the
4071 end of the argument list. */
4072 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4074 for (i
= 0; i
< nargs
; ++i
)
4076 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4077 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4078 tree packed_args
= NULL_TREE
;
4081 if (ARGUMENT_PACK_P (arg
))
4083 /* Extract the arguments from the argument pack. We'll be
4084 iterating over these in the following loop. */
4085 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4086 len
= TREE_VEC_LENGTH (packed_args
);
4089 for (j
= 0; j
< len
; j
++)
4092 /* Get the Jth argument in the parameter pack. */
4093 arg
= TREE_VEC_ELT (packed_args
, j
);
4095 if (PACK_EXPANSION_P (arg
))
4097 /* Pack expansions must come at the end of the
4099 if ((packed_args
&& j
< len
- 1)
4100 || (!packed_args
&& i
< nargs
- 1))
4102 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4103 error ("parameter pack argument %qE must be at the "
4104 "end of the template argument list", arg
);
4106 error ("parameter pack argument %qT must be at the "
4107 "end of the template argument list", arg
);
4111 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4112 /* We only care about the pattern. */
4113 arg
= PACK_EXPANSION_PATTERN (arg
);
4115 if (/* These first two lines are the `non-type' bit. */
4117 && TREE_CODE (arg
) != TEMPLATE_DECL
4118 /* This next line is the `argument expression is not just a
4119 simple identifier' condition and also the `specialized
4120 non-type argument' bit. */
4121 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
4123 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4124 || (packed_args
&& uses_template_parms (arg
)))
4125 error ("template argument %qE involves template parameter(s)",
4129 /* Look at the corresponding template parameter,
4130 marking which template parameters its type depends
4132 tree type
= TREE_TYPE (parm
);
4136 /* We haven't yet initialized TPD2. Do so now. */
4137 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4138 /* The number of parameters here is the number in the
4139 main template, which, as checked in the assertion
4141 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4143 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4146 /* Mark the template parameters. But this time, we're
4147 looking for the template parameters of the main
4148 template, not in the specialization. */
4149 tpd2
.current_arg
= i
;
4150 tpd2
.arg_uses_template_parms
[i
] = 0;
4151 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4152 for_each_template_parm (type
,
4153 &mark_template_parm
,
4156 /*include_nondeduced_p=*/false);
4158 if (tpd2
.arg_uses_template_parms
[i
])
4160 /* The type depended on some template parameters.
4161 If they are fully specialized in the
4162 specialization, that's OK. */
4165 for (j
= 0; j
< nargs
; ++j
)
4166 if (tpd2
.parms
[j
] != 0
4167 && tpd
.arg_uses_template_parms
[j
])
4170 error_n (input_location
, count
,
4171 "type %qT of template argument %qE depends "
4172 "on a template parameter",
4173 "type %qT of template argument %qE depends "
4174 "on template parameters",
4183 /* We should only get here once. */
4184 gcc_assert (!COMPLETE_TYPE_P (type
));
4186 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4187 = tree_cons (specargs
, inner_parms
,
4188 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4189 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4191 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4192 inst
= TREE_CHAIN (inst
))
4194 tree inst_type
= TREE_VALUE (inst
);
4195 if (COMPLETE_TYPE_P (inst_type
)
4196 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type
))
4198 tree spec
= most_specialized_class (inst_type
, maintmpl
, tf_none
);
4199 if (spec
&& TREE_TYPE (spec
) == type
)
4200 permerror (input_location
,
4201 "partial specialization of %qT after instantiation "
4202 "of %qT", type
, inst_type
);
4209 /* Check that a template declaration's use of default arguments and
4210 parameter packs is not invalid. Here, PARMS are the template
4211 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
4212 a primary template. IS_PARTIAL is nonzero if DECL is a partial
4216 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4217 declaration (but not a definition); 1 indicates a declaration, 2
4218 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4219 emitted for extraneous default arguments.
4221 Returns TRUE if there were no errors found, FALSE otherwise. */
4224 check_default_tmpl_args (tree decl
, tree parms
, int is_primary
,
4225 int is_partial
, int is_friend_decl
)
4228 int last_level_to_check
;
4230 bool no_errors
= true;
4234 A default template-argument shall not be specified in a
4235 function template declaration or a function template definition, nor
4236 in the template-parameter-list of the definition of a member of a
4239 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
4240 /* You can't have a function template declaration in a local
4241 scope, nor you can you define a member of a class template in a
4245 if (current_class_type
4246 && !TYPE_BEING_DEFINED (current_class_type
)
4247 && DECL_LANG_SPECIFIC (decl
)
4248 && DECL_DECLARES_FUNCTION_P (decl
)
4249 /* If this is either a friend defined in the scope of the class
4250 or a member function. */
4251 && (DECL_FUNCTION_MEMBER_P (decl
)
4252 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4253 : DECL_FRIEND_CONTEXT (decl
)
4254 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4256 /* And, if it was a member function, it really was defined in
4257 the scope of the class. */
4258 && (!DECL_FUNCTION_MEMBER_P (decl
)
4259 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4260 /* We already checked these parameters when the template was
4261 declared, so there's no need to do it again now. This function
4262 was defined in class scope, but we're processing it's body now
4263 that the class is complete. */
4266 /* Core issue 226 (C++0x only): the following only applies to class
4268 if ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
)
4272 If a template-parameter has a default template-argument, all
4273 subsequent template-parameters shall have a default
4274 template-argument supplied. */
4275 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4277 tree inner_parms
= TREE_VALUE (parm_level
);
4278 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4279 int seen_def_arg_p
= 0;
4282 for (i
= 0; i
< ntparms
; ++i
)
4284 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4286 if (parm
== error_mark_node
)
4289 if (TREE_PURPOSE (parm
))
4291 else if (seen_def_arg_p
4292 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4294 error ("no default argument for %qD", TREE_VALUE (parm
));
4295 /* For better subsequent error-recovery, we indicate that
4296 there should have been a default argument. */
4297 TREE_PURPOSE (parm
) = error_mark_node
;
4303 /* Don't complain about an enclosing partial
4305 && parm_level
== parms
4306 && TREE_CODE (decl
) == TYPE_DECL
4308 && template_parameter_pack_p (TREE_VALUE (parm
)))
4310 /* A primary class template can only have one
4311 parameter pack, at the end of the template
4314 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
4315 error ("parameter pack %qE must be at the end of the"
4316 " template parameter list", TREE_VALUE (parm
));
4318 error ("parameter pack %qT must be at the end of the"
4319 " template parameter list",
4320 TREE_TYPE (TREE_VALUE (parm
)));
4322 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4330 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4334 /* For an ordinary class template, default template arguments are
4335 allowed at the innermost level, e.g.:
4336 template <class T = int>
4338 but, in a partial specialization, they're not allowed even
4339 there, as we have in [temp.class.spec]:
4341 The template parameter list of a specialization shall not
4342 contain default template argument values.
4344 So, for a partial specialization, or for a function template
4345 (in C++98/C++03), we look at all of them. */
4348 /* But, for a primary class template that is not a partial
4349 specialization we look at all template parameters except the
4351 parms
= TREE_CHAIN (parms
);
4353 /* Figure out what error message to issue. */
4354 if (is_friend_decl
== 2)
4355 msg
= G_("default template arguments may not be used in function template "
4356 "friend re-declaration");
4357 else if (is_friend_decl
)
4358 msg
= G_("default template arguments may not be used in function template "
4359 "friend declarations");
4360 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4361 msg
= G_("default template arguments may not be used in function templates "
4362 "without -std=c++11 or -std=gnu++11");
4363 else if (is_partial
)
4364 msg
= G_("default template arguments may not be used in "
4365 "partial specializations");
4367 msg
= G_("default argument for template parameter for class enclosing %qD");
4369 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4370 /* If we're inside a class definition, there's no need to
4371 examine the parameters to the class itself. On the one
4372 hand, they will be checked when the class is defined, and,
4373 on the other, default arguments are valid in things like:
4374 template <class T = double>
4375 struct S { template <class U> void f(U); };
4376 Here the default argument for `S' has no bearing on the
4377 declaration of `f'. */
4378 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4380 /* Check everything. */
4381 last_level_to_check
= 0;
4383 for (parm_level
= parms
;
4384 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4385 parm_level
= TREE_CHAIN (parm_level
))
4387 tree inner_parms
= TREE_VALUE (parm_level
);
4391 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4392 for (i
= 0; i
< ntparms
; ++i
)
4394 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4397 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4402 if (is_friend_decl
== 2)
4409 /* Clear out the default argument so that we are not
4411 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4415 /* At this point, if we're still interested in issuing messages,
4416 they must apply to classes surrounding the object declared. */
4418 msg
= G_("default argument for template parameter for class "
4425 /* Worker for push_template_decl_real, called via
4426 for_each_template_parm. DATA is really an int, indicating the
4427 level of the parameters we are interested in. If T is a template
4428 parameter of that level, return nonzero. */
4431 template_parm_this_level_p (tree t
, void* data
)
4433 int this_level
= *(int *)data
;
4436 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4437 level
= TEMPLATE_PARM_LEVEL (t
);
4439 level
= TEMPLATE_TYPE_LEVEL (t
);
4440 return level
== this_level
;
4443 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4444 parameters given by current_template_args, or reuses a
4445 previously existing one, if appropriate. Returns the DECL, or an
4446 equivalent one, if it is replaced via a call to duplicate_decls.
4448 If IS_FRIEND is true, DECL is a friend declaration. */
4451 push_template_decl_real (tree decl
, bool is_friend
)
4459 int new_template_p
= 0;
4460 /* True if the template is a member template, in the sense of
4462 bool member_template_p
= false;
4464 if (decl
== error_mark_node
|| !current_template_parms
)
4465 return error_mark_node
;
4467 /* See if this is a partial specialization. */
4468 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
4469 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4470 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
4472 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4476 /* For a friend, we want the context of the friend function, not
4477 the type of which it is a friend. */
4478 ctx
= CP_DECL_CONTEXT (decl
);
4479 else if (CP_DECL_CONTEXT (decl
)
4480 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4481 /* In the case of a virtual function, we want the class in which
4483 ctx
= CP_DECL_CONTEXT (decl
);
4485 /* Otherwise, if we're currently defining some class, the DECL
4486 is assumed to be a member of the class. */
4487 ctx
= current_scope ();
4489 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4492 if (!DECL_CONTEXT (decl
))
4493 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4495 /* See if this is a primary template. */
4496 if (is_friend
&& ctx
)
4497 /* A friend template that specifies a class context, i.e.
4498 template <typename T> friend void A<T>::f();
4502 primary
= template_parm_scope_p ();
4506 if (DECL_CLASS_SCOPE_P (decl
))
4507 member_template_p
= true;
4508 if (TREE_CODE (decl
) == TYPE_DECL
4509 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4511 error ("template class without a name");
4512 return error_mark_node
;
4514 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4516 if (DECL_DESTRUCTOR_P (decl
))
4520 A destructor shall not be a member template. */
4521 error ("destructor %qD declared as member template", decl
);
4522 return error_mark_node
;
4524 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4525 && (!prototype_p (TREE_TYPE (decl
))
4526 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4527 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4528 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4529 == void_list_node
)))
4531 /* [basic.stc.dynamic.allocation]
4533 An allocation function can be a function
4534 template. ... Template allocation functions shall
4535 have two or more parameters. */
4536 error ("invalid template declaration of %qD", decl
);
4537 return error_mark_node
;
4540 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4541 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4543 else if (TREE_CODE (decl
) == TYPE_DECL
4544 && TYPE_DECL_ALIAS_P (decl
))
4545 /* alias-declaration */
4546 gcc_assert (!DECL_ARTIFICIAL (decl
));
4549 error ("template declaration of %q#D", decl
);
4550 return error_mark_node
;
4554 /* Check to see that the rules regarding the use of default
4555 arguments are not being violated. */
4556 check_default_tmpl_args (decl
, current_template_parms
,
4557 primary
, is_partial
, /*is_friend_decl=*/0);
4559 /* Ensure that there are no parameter packs in the type of this
4560 declaration that have not been expanded. */
4561 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4563 /* Check each of the arguments individually to see if there are
4564 any bare parameter packs. */
4565 tree type
= TREE_TYPE (decl
);
4566 tree arg
= DECL_ARGUMENTS (decl
);
4567 tree argtype
= TYPE_ARG_TYPES (type
);
4569 while (arg
&& argtype
)
4571 if (!FUNCTION_PARAMETER_PACK_P (arg
)
4572 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4574 /* This is a PARM_DECL that contains unexpanded parameter
4575 packs. We have already complained about this in the
4576 check_for_bare_parameter_packs call, so just replace
4577 these types with ERROR_MARK_NODE. */
4578 TREE_TYPE (arg
) = error_mark_node
;
4579 TREE_VALUE (argtype
) = error_mark_node
;
4582 arg
= DECL_CHAIN (arg
);
4583 argtype
= TREE_CHAIN (argtype
);
4586 /* Check for bare parameter packs in the return type and the
4587 exception specifiers. */
4588 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4589 /* Errors were already issued, set return type to int
4590 as the frontend doesn't expect error_mark_node as
4592 TREE_TYPE (type
) = integer_type_node
;
4593 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4594 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4596 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
4597 && TYPE_DECL_ALIAS_P (decl
))
4598 ? DECL_ORIGINAL_TYPE (decl
)
4599 : TREE_TYPE (decl
)))
4601 TREE_TYPE (decl
) = error_mark_node
;
4602 return error_mark_node
;
4606 return process_partial_specialization (decl
);
4608 args
= current_template_args ();
4611 || TREE_CODE (ctx
) == FUNCTION_DECL
4612 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4613 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4615 if (DECL_LANG_SPECIFIC (decl
)
4616 && DECL_TEMPLATE_INFO (decl
)
4617 && DECL_TI_TEMPLATE (decl
))
4618 tmpl
= DECL_TI_TEMPLATE (decl
);
4619 /* If DECL is a TYPE_DECL for a class-template, then there won't
4620 be DECL_LANG_SPECIFIC. The information equivalent to
4621 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4622 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4623 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4624 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4626 /* Since a template declaration already existed for this
4627 class-type, we must be redeclaring it here. Make sure
4628 that the redeclaration is valid. */
4629 redeclare_class_template (TREE_TYPE (decl
),
4630 current_template_parms
);
4631 /* We don't need to create a new TEMPLATE_DECL; just use the
4632 one we already had. */
4633 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4637 tmpl
= build_template_decl (decl
, current_template_parms
,
4641 if (DECL_LANG_SPECIFIC (decl
)
4642 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4644 /* A specialization of a member template of a template
4646 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4647 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4648 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4654 tree a
, t
, current
, parms
;
4656 tree tinfo
= get_template_info (decl
);
4660 error ("template definition of non-template %q#D", decl
);
4661 return error_mark_node
;
4664 tmpl
= TI_TEMPLATE (tinfo
);
4666 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4667 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4668 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4669 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4673 /* The declaration is a specialization of a member
4674 template, declared outside the class. Therefore, the
4675 innermost template arguments will be NULL, so we
4676 replace them with the arguments determined by the
4677 earlier call to check_explicit_specialization. */
4678 args
= DECL_TI_ARGS (decl
);
4681 = build_template_decl (decl
, current_template_parms
,
4683 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4684 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4685 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4686 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4687 DECL_TEMPLATE_INFO (new_tmpl
)
4688 = build_template_info (tmpl
, args
);
4690 register_specialization (new_tmpl
,
4691 most_general_template (tmpl
),
4697 /* Make sure the template headers we got make sense. */
4699 parms
= DECL_TEMPLATE_PARMS (tmpl
);
4700 i
= TMPL_PARMS_DEPTH (parms
);
4701 if (TMPL_ARGS_DEPTH (args
) != i
)
4703 error ("expected %d levels of template parms for %q#D, got %d",
4704 i
, decl
, TMPL_ARGS_DEPTH (args
));
4707 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
4709 a
= TMPL_ARGS_LEVEL (args
, i
);
4710 t
= INNERMOST_TEMPLATE_PARMS (parms
);
4712 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
4714 if (current
== decl
)
4715 error ("got %d template parameters for %q#D",
4716 TREE_VEC_LENGTH (a
), decl
);
4718 error ("got %d template parameters for %q#T",
4719 TREE_VEC_LENGTH (a
), current
);
4720 error (" but %d required", TREE_VEC_LENGTH (t
));
4721 return error_mark_node
;
4724 if (current
== decl
)
4726 else if (current
== NULL_TREE
)
4727 /* Can happen in erroneous input. */
4730 current
= (TYPE_P (current
)
4731 ? TYPE_CONTEXT (current
)
4732 : DECL_CONTEXT (current
));
4735 /* Check that the parms are used in the appropriate qualifying scopes
4736 in the declarator. */
4737 if (!comp_template_args
4739 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
4742 template arguments to %qD do not match original template %qD",
4743 decl
, DECL_TEMPLATE_RESULT (tmpl
));
4744 if (!uses_template_parms (TI_ARGS (tinfo
)))
4745 inform (input_location
, "use template<> for an explicit specialization");
4746 /* Avoid crash in import_export_decl. */
4747 DECL_INTERFACE_KNOWN (decl
) = 1;
4748 return error_mark_node
;
4752 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4753 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4755 /* Push template declarations for global functions and types. Note
4756 that we do not try to push a global template friend declared in a
4757 template class; such a thing may well depend on the template
4758 parameters of the class. */
4759 if (new_template_p
&& !ctx
4760 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
4762 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
4763 if (tmpl
== error_mark_node
)
4764 return error_mark_node
;
4766 /* Hide template friend classes that haven't been declared yet. */
4767 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
4769 DECL_ANTICIPATED (tmpl
) = 1;
4770 DECL_FRIEND_P (tmpl
) = 1;
4776 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
4779 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4780 if (DECL_CONV_FN_P (tmpl
))
4782 int depth
= TMPL_PARMS_DEPTH (parms
);
4784 /* It is a conversion operator. See if the type converted to
4785 depends on innermost template operands. */
4787 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
4789 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
4792 /* Give template template parms a DECL_CONTEXT of the template
4793 for which they are a parameter. */
4794 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4795 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
4797 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4798 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4799 DECL_CONTEXT (parm
) = tmpl
;
4803 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4804 back to its most general template. If TMPL is a specialization,
4805 ARGS may only have the innermost set of arguments. Add the missing
4806 argument levels if necessary. */
4807 if (DECL_TEMPLATE_INFO (tmpl
))
4808 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
4810 info
= build_template_info (tmpl
, args
);
4812 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
4813 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
4816 if (primary
&& !DECL_LANG_SPECIFIC (decl
))
4817 retrofit_lang_decl (decl
);
4818 if (DECL_LANG_SPECIFIC (decl
))
4819 DECL_TEMPLATE_INFO (decl
) = info
;
4822 return DECL_TEMPLATE_RESULT (tmpl
);
4826 push_template_decl (tree decl
)
4828 return push_template_decl_real (decl
, false);
4831 /* Called when a class template TYPE is redeclared with the indicated
4832 template PARMS, e.g.:
4834 template <class T> struct S;
4835 template <class T> struct S {}; */
4838 redeclare_class_template (tree type
, tree parms
)
4844 if (!TYPE_TEMPLATE_INFO (type
))
4846 error ("%qT is not a template type", type
);
4850 tmpl
= TYPE_TI_TEMPLATE (type
);
4851 if (!PRIMARY_TEMPLATE_P (tmpl
))
4852 /* The type is nested in some template class. Nothing to worry
4853 about here; there are no new template parameters for the nested
4859 error ("template specifiers not specified in declaration of %qD",
4864 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4865 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
4867 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
4869 error_n (input_location
, TREE_VEC_LENGTH (parms
),
4870 "redeclared with %d template parameter",
4871 "redeclared with %d template parameters",
4872 TREE_VEC_LENGTH (parms
));
4873 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
4874 "previous declaration %q+D used %d template parameter",
4875 "previous declaration %q+D used %d template parameters",
4876 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
4880 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
4887 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
4888 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
4891 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
4892 if (tmpl_parm
== error_mark_node
)
4895 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4896 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
4897 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
4899 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4901 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
4902 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
4903 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
4904 || (TREE_CODE (tmpl_parm
) != PARM_DECL
4905 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
4906 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
4907 || (TREE_CODE (tmpl_parm
) == PARM_DECL
4908 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
4909 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
4911 error ("template parameter %q+#D", tmpl_parm
);
4912 error ("redeclared here as %q#D", parm
);
4916 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
4918 /* We have in [temp.param]:
4920 A template-parameter may not be given default arguments
4921 by two different declarations in the same scope. */
4922 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
4923 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
4924 "original definition appeared here");
4928 if (parm_default
!= NULL_TREE
)
4929 /* Update the previous template parameters (which are the ones
4930 that will really count) with the new default value. */
4931 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
4932 else if (tmpl_default
!= NULL_TREE
)
4933 /* Update the new parameters, too; they'll be used as the
4934 parameters for any members. */
4935 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
4941 /* Simplify EXPR if it is a non-dependent expression. Returns the
4942 (possibly simplified) expression. */
4945 fold_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
4947 if (expr
== NULL_TREE
)
4950 /* If we're in a template, but EXPR isn't value dependent, simplify
4951 it. We're supposed to treat:
4953 template <typename T> void f(T[1 + 1]);
4954 template <typename T> void f(T[2]);
4956 as two declarations of the same function, for example. */
4957 if (processing_template_decl
4958 && !type_dependent_expression_p (expr
)
4959 && potential_constant_expression (expr
)
4960 && !value_dependent_expression_p (expr
))
4962 HOST_WIDE_INT saved_processing_template_decl
;
4964 saved_processing_template_decl
= processing_template_decl
;
4965 processing_template_decl
= 0;
4966 expr
= tsubst_copy_and_build (expr
,
4969 /*in_decl=*/NULL_TREE
,
4970 /*function_p=*/false,
4971 /*integral_constant_expression_p=*/true);
4972 processing_template_decl
= saved_processing_template_decl
;
4978 fold_non_dependent_expr (tree expr
)
4980 return fold_non_dependent_expr_sfinae (expr
, tf_error
);
4983 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
4984 template declaration, or a TYPE_DECL for an alias declaration. */
4987 alias_type_or_template_p (tree t
)
4991 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
4994 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
4995 || DECL_ALIAS_TEMPLATE_P (t
));
4998 /* Return TRUE iff is a specialization of an alias template. */
5001 alias_template_specialization_p (tree t
)
5005 return (primary_template_instantiation_p (t
)
5006 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t
)));
5009 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5010 must be a function or a pointer-to-function type, as specified
5011 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5012 and check that the resulting function has external linkage. */
5015 convert_nontype_argument_function (tree type
, tree expr
)
5019 linkage_kind linkage
;
5021 fn
= instantiate_type (type
, fns
, tf_none
);
5022 if (fn
== error_mark_node
)
5023 return error_mark_node
;
5026 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5027 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5028 if (BASELINK_P (fn_no_ptr
))
5029 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5031 /* [temp.arg.nontype]/1
5033 A template-argument for a non-type, non-template template-parameter
5036 -- the address of an object or function with external [C++11: or
5037 internal] linkage. */
5038 linkage
= decl_linkage (fn_no_ptr
);
5039 if (cxx_dialect
>= cxx0x
? linkage
== lk_none
: linkage
!= lk_external
)
5041 if (cxx_dialect
>= cxx0x
)
5042 error ("%qE is not a valid template argument for type %qT "
5043 "because %qD has no linkage",
5044 expr
, type
, fn_no_ptr
);
5046 error ("%qE is not a valid template argument for type %qT "
5047 "because %qD does not have external linkage",
5048 expr
, type
, fn_no_ptr
);
5055 /* Subroutine of convert_nontype_argument.
5056 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5057 Emit an error otherwise. */
5060 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5061 tsubst_flags_t complain
)
5064 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5066 if (cxx_dialect
>= cxx0x
&& null_member_pointer_value_p (expr
))
5068 if (complain
& tf_error
)
5070 error ("%qE is not a valid template argument for type %qT",
5072 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5077 /* Returns TRUE iff the address of OP is value-dependent.
5079 14.6.2.4 [temp.dep.temp]:
5080 A non-integral non-type template-argument is dependent if its type is
5081 dependent or it has either of the following forms
5084 and contains a nested-name-specifier which specifies a class-name that
5085 names a dependent type.
5087 We generalize this to just say that the address of a member of a
5088 dependent class is value-dependent; the above doesn't cover the
5089 address of a static data member named with an unqualified-id. */
5092 has_value_dependent_address (tree op
)
5094 /* We could use get_inner_reference here, but there's no need;
5095 this is only relevant for template non-type arguments, which
5096 can only be expressed as &id-expression. */
5099 tree ctx
= CP_DECL_CONTEXT (op
);
5100 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5107 /* The next set of functions are used for providing helpful explanatory
5108 diagnostics for failed overload resolution. Their messages should be
5109 indented by two spaces for consistency with the messages in
5113 unify_success (bool explain_p ATTRIBUTE_UNUSED
)
5119 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5122 inform (input_location
,
5123 " couldn't deduce template parameter %qD", parm
);
5128 unify_invalid (bool explain_p ATTRIBUTE_UNUSED
)
5134 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5137 inform (input_location
,
5138 " types %qT and %qT have incompatible cv-qualifiers",
5144 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5147 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5152 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
5155 inform (input_location
,
5156 " template parameter %qD is not a parameter pack, but "
5163 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
5166 inform (input_location
,
5167 " template argument %qE does not match "
5168 "pointer-to-member constant %qE",
5174 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
5177 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
5182 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
5185 inform (input_location
,
5186 " inconsistent parameter pack deduction with %qT and %qT",
5192 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
5197 inform (input_location
,
5198 " deduced conflicting types for parameter %qT (%qT and %qT)",
5199 parm
, first
, second
);
5201 inform (input_location
,
5202 " deduced conflicting values for non-type parameter "
5203 "%qE (%qE and %qE)", parm
, first
, second
);
5209 unify_vla_arg (bool explain_p
, tree arg
)
5212 inform (input_location
,
5213 " variable-sized array type %qT is not "
5214 "a valid template argument",
5220 unify_method_type_error (bool explain_p
, tree arg
)
5223 inform (input_location
,
5224 " member function type %qT is not a valid template argument",
5230 unify_arity (bool explain_p
, int have
, int wanted
)
5233 inform_n (input_location
, wanted
,
5234 " candidate expects %d argument, %d provided",
5235 " candidate expects %d arguments, %d provided",
5241 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
5243 return unify_arity (explain_p
, have
, wanted
);
5247 unify_too_few_arguments (bool explain_p
, int have
, int wanted
)
5249 return unify_arity (explain_p
, have
, wanted
);
5253 unify_arg_conversion (bool explain_p
, tree to_type
,
5254 tree from_type
, tree arg
)
5257 inform (input_location
, " cannot convert %qE (type %qT) to type %qT",
5258 arg
, from_type
, to_type
);
5263 unify_no_common_base (bool explain_p
, enum template_base_result r
,
5264 tree parm
, tree arg
)
5269 case tbr_ambiguous_baseclass
:
5270 inform (input_location
, " %qT is an ambiguous base class of %qT",
5274 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
5281 unify_inconsistent_template_template_parameters (bool explain_p
)
5284 inform (input_location
,
5285 " template parameters of a template template argument are "
5286 "inconsistent with other deduced template arguments");
5291 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
5294 inform (input_location
,
5295 " can't deduce a template for %qT from non-template type %qT",
5301 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
5304 inform (input_location
,
5305 " template argument %qE does not match %qD", arg
, parm
);
5310 unify_overload_resolution_failure (bool explain_p
, tree arg
)
5313 inform (input_location
,
5314 " could not resolve address from overloaded function %qE",
5319 /* Attempt to convert the non-type template parameter EXPR to the
5320 indicated TYPE. If the conversion is successful, return the
5321 converted value. If the conversion is unsuccessful, return
5322 NULL_TREE if we issued an error message, or error_mark_node if we
5323 did not. We issue error messages for out-and-out bad template
5324 parameters, but not simply because the conversion failed, since we
5325 might be just trying to do argument deduction. Both TYPE and EXPR
5326 must be non-dependent.
5328 The conversion follows the special rules described in
5329 [temp.arg.nontype], and it is much more strict than an implicit
5332 This function is called twice for each template argument (see
5333 lookup_template_class for a more accurate description of this
5334 problem). This means that we need to handle expressions which
5335 are not valid in a C++ source, but can be created from the
5336 first call (for instance, casts to perform conversions). These
5337 hacks can go away after we fix the double coercion problem. */
5340 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5344 /* Detect immediately string literals as invalid non-type argument.
5345 This special-case is not needed for correctness (we would easily
5346 catch this later), but only to provide better diagnostic for this
5347 common user mistake. As suggested by DR 100, we do not mention
5348 linkage issues in the diagnostic as this is not the point. */
5349 /* FIXME we're making this OK. */
5350 if (TREE_CODE (expr
) == STRING_CST
)
5352 if (complain
& tf_error
)
5353 error ("%qE is not a valid template argument for type %qT "
5354 "because string literals can never be used in this context",
5359 /* Add the ADDR_EXPR now for the benefit of
5360 value_dependent_expression_p. */
5361 if (TYPE_PTROBV_P (type
)
5362 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
5364 expr
= decay_conversion (expr
, complain
);
5365 if (expr
== error_mark_node
)
5366 return error_mark_node
;
5369 /* If we are in a template, EXPR may be non-dependent, but still
5370 have a syntactic, rather than semantic, form. For example, EXPR
5371 might be a SCOPE_REF, rather than the VAR_DECL to which the
5372 SCOPE_REF refers. Preserving the qualifying scope is necessary
5373 so that access checking can be performed when the template is
5374 instantiated -- but here we need the resolved form so that we can
5375 convert the argument. */
5376 if (TYPE_REF_OBJ_P (type
)
5377 && has_value_dependent_address (expr
))
5378 /* If we want the address and it's value-dependent, don't fold. */;
5379 else if (!type_unknown_p (expr
))
5380 expr
= fold_non_dependent_expr_sfinae (expr
, complain
);
5381 if (error_operand_p (expr
))
5382 return error_mark_node
;
5383 expr_type
= TREE_TYPE (expr
);
5384 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5385 expr
= mark_lvalue_use (expr
);
5387 expr
= mark_rvalue_use (expr
);
5389 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5390 to a non-type argument of "nullptr". */
5391 if (expr
== nullptr_node
&& TYPE_PTR_OR_PTRMEM_P (type
))
5392 expr
= convert (type
, expr
);
5394 /* In C++11, integral or enumeration non-type template arguments can be
5395 arbitrary constant expressions. Pointer and pointer to
5396 member arguments can be general constant expressions that evaluate
5397 to a null value, but otherwise still need to be of a specific form. */
5398 if (cxx_dialect
>= cxx0x
)
5400 if (TREE_CODE (expr
) == PTRMEM_CST
)
5401 /* A PTRMEM_CST is already constant, and a valid template
5402 argument for a parameter of pointer to member type, we just want
5403 to leave it in that form rather than lower it to a
5405 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5406 expr
= maybe_constant_value (expr
);
5407 else if (TYPE_PTR_OR_PTRMEM_P (type
))
5409 tree folded
= maybe_constant_value (expr
);
5410 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
5411 : null_member_pointer_value_p (folded
))
5416 /* HACK: Due to double coercion, we can get a
5417 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5418 which is the tree that we built on the first call (see
5419 below when coercing to reference to object or to reference to
5420 function). We just strip everything and get to the arg.
5421 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5423 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5425 tree probe_type
, probe
= expr
;
5426 if (REFERENCE_REF_P (probe
))
5427 probe
= TREE_OPERAND (probe
, 0);
5428 probe_type
= TREE_TYPE (probe
);
5429 if (TREE_CODE (probe
) == NOP_EXPR
)
5431 /* ??? Maybe we could use convert_from_reference here, but we
5432 would need to relax its constraints because the NOP_EXPR
5433 could actually change the type to something more cv-qualified,
5434 and this is not folded by convert_from_reference. */
5435 tree addr
= TREE_OPERAND (probe
, 0);
5436 gcc_assert (TREE_CODE (probe_type
) == REFERENCE_TYPE
);
5437 gcc_assert (TREE_CODE (addr
) == ADDR_EXPR
);
5438 gcc_assert (TREE_CODE (TREE_TYPE (addr
)) == POINTER_TYPE
);
5439 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5440 (TREE_TYPE (probe_type
),
5441 TREE_TYPE (TREE_TYPE (addr
))));
5443 expr
= TREE_OPERAND (addr
, 0);
5444 expr_type
= TREE_TYPE (expr
);
5448 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5449 parameter is a pointer to object, through decay and
5450 qualification conversion. Let's strip everything. */
5451 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
5454 gcc_assert (TREE_CODE (expr
) == ADDR_EXPR
);
5455 gcc_assert (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
);
5456 /* Skip the ADDR_EXPR only if it is part of the decay for
5457 an array. Otherwise, it is part of the original argument
5458 in the source code. */
5459 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == ARRAY_TYPE
)
5460 expr
= TREE_OPERAND (expr
, 0);
5461 expr_type
= TREE_TYPE (expr
);
5464 /* [temp.arg.nontype]/5, bullet 1
5466 For a non-type template-parameter of integral or enumeration type,
5467 integral promotions (_conv.prom_) and integral conversions
5468 (_conv.integral_) are applied. */
5469 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5471 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
5472 t
= maybe_constant_value (t
);
5473 if (t
!= error_mark_node
)
5476 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
5477 return error_mark_node
;
5479 /* Notice that there are constant expressions like '4 % 0' which
5480 do not fold into integer constants. */
5481 if (TREE_CODE (expr
) != INTEGER_CST
)
5483 if (complain
& tf_error
)
5485 int errs
= errorcount
, warns
= warningcount
;
5486 if (processing_template_decl
5487 && !require_potential_constant_expression (expr
))
5489 expr
= cxx_constant_value (expr
);
5490 if (errorcount
> errs
|| warningcount
> warns
)
5491 inform (EXPR_LOC_OR_HERE (expr
),
5492 "in template argument for type %qT ", type
);
5493 if (expr
== error_mark_node
)
5495 /* else cxx_constant_value complained but gave us
5496 a real constant, so go ahead. */
5497 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
5503 /* [temp.arg.nontype]/5, bullet 2
5505 For a non-type template-parameter of type pointer to object,
5506 qualification conversions (_conv.qual_) and the array-to-pointer
5507 conversion (_conv.array_) are applied. */
5508 else if (TYPE_PTROBV_P (type
))
5510 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5512 A template-argument for a non-type, non-template template-parameter
5513 shall be one of: [...]
5515 -- the name of a non-type template-parameter;
5516 -- the address of an object or function with external linkage, [...]
5517 expressed as "& id-expression" where the & is optional if the name
5518 refers to a function or array, or if the corresponding
5519 template-parameter is a reference.
5521 Here, we do not care about functions, as they are invalid anyway
5522 for a parameter of type pointer-to-object. */
5524 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5525 /* Non-type template parameters are OK. */
5527 else if (cxx_dialect
>= cxx0x
&& integer_zerop (expr
))
5528 /* Null pointer values are OK in C++11. */;
5529 else if (TREE_CODE (expr
) != ADDR_EXPR
5530 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5532 if (TREE_CODE (expr
) == VAR_DECL
)
5534 error ("%qD is not a valid template argument "
5535 "because %qD is a variable, not the address of "
5540 /* Other values, like integer constants, might be valid
5541 non-type arguments of some other type. */
5542 return error_mark_node
;
5548 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
5549 ? TREE_OPERAND (expr
, 0) : expr
);
5550 if (TREE_CODE (decl
) != VAR_DECL
)
5552 error ("%qE is not a valid template argument of type %qT "
5553 "because %qE is not a variable",
5557 else if (cxx_dialect
< cxx0x
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
5559 error ("%qE is not a valid template argument of type %qT "
5560 "because %qD does not have external linkage",
5564 else if (cxx_dialect
>= cxx0x
&& decl_linkage (decl
) == lk_none
)
5566 error ("%qE is not a valid template argument of type %qT "
5567 "because %qD has no linkage",
5573 expr
= decay_conversion (expr
, complain
);
5574 if (expr
== error_mark_node
)
5575 return error_mark_node
;
5577 expr
= perform_qualification_conversions (type
, expr
);
5578 if (expr
== error_mark_node
)
5579 return error_mark_node
;
5581 /* [temp.arg.nontype]/5, bullet 3
5583 For a non-type template-parameter of type reference to object, no
5584 conversions apply. The type referred to by the reference may be more
5585 cv-qualified than the (otherwise identical) type of the
5586 template-argument. The template-parameter is bound directly to the
5587 template-argument, which must be an lvalue. */
5588 else if (TYPE_REF_OBJ_P (type
))
5590 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
5592 return error_mark_node
;
5594 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5596 error ("%qE is not a valid template argument for type %qT "
5597 "because of conflicts in cv-qualification", expr
, type
);
5601 if (!real_lvalue_p (expr
))
5603 error ("%qE is not a valid template argument for type %qT "
5604 "because it is not an lvalue", expr
, type
);
5608 /* [temp.arg.nontype]/1
5610 A template-argument for a non-type, non-template template-parameter
5611 shall be one of: [...]
5613 -- the address of an object or function with external linkage. */
5614 if (TREE_CODE (expr
) == INDIRECT_REF
5615 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5617 expr
= TREE_OPERAND (expr
, 0);
5620 error ("%q#D is not a valid template argument for type %qT "
5621 "because a reference variable does not have a constant "
5622 "address", expr
, type
);
5629 error ("%qE is not a valid template argument for type %qT "
5630 "because it is not an object with external linkage",
5635 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5637 error ("%qE is not a valid template argument for type %qT "
5638 "because object %qD has not external linkage",
5643 expr
= build_nop (type
, build_address (expr
));
5645 /* [temp.arg.nontype]/5, bullet 4
5647 For a non-type template-parameter of type pointer to function, only
5648 the function-to-pointer conversion (_conv.func_) is applied. If the
5649 template-argument represents a set of overloaded functions (or a
5650 pointer to such), the matching function is selected from the set
5652 else if (TYPE_PTRFN_P (type
))
5654 /* If the argument is a template-id, we might not have enough
5655 context information to decay the pointer. */
5656 if (!type_unknown_p (expr_type
))
5658 expr
= decay_conversion (expr
, complain
);
5659 if (expr
== error_mark_node
)
5660 return error_mark_node
;
5663 if (cxx_dialect
>= cxx0x
&& integer_zerop (expr
))
5664 /* Null pointer values are OK in C++11. */
5665 return perform_qualification_conversions (type
, expr
);
5667 expr
= convert_nontype_argument_function (type
, expr
);
5668 if (!expr
|| expr
== error_mark_node
)
5671 if (TREE_CODE (expr
) != ADDR_EXPR
)
5673 error ("%qE is not a valid template argument for type %qT", expr
, type
);
5674 error ("it must be the address of a function with external linkage");
5678 /* [temp.arg.nontype]/5, bullet 5
5680 For a non-type template-parameter of type reference to function, no
5681 conversions apply. If the template-argument represents a set of
5682 overloaded functions, the matching function is selected from the set
5684 else if (TYPE_REFFN_P (type
))
5686 if (TREE_CODE (expr
) == ADDR_EXPR
)
5688 error ("%qE is not a valid template argument for type %qT "
5689 "because it is a pointer", expr
, type
);
5690 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
5694 expr
= convert_nontype_argument_function (TREE_TYPE (type
), expr
);
5695 if (!expr
|| expr
== error_mark_node
)
5698 expr
= build_nop (type
, build_address (expr
));
5700 /* [temp.arg.nontype]/5, bullet 6
5702 For a non-type template-parameter of type pointer to member function,
5703 no conversions apply. If the template-argument represents a set of
5704 overloaded member functions, the matching member function is selected
5705 from the set (_over.over_). */
5706 else if (TYPE_PTRMEMFUNC_P (type
))
5708 expr
= instantiate_type (type
, expr
, tf_none
);
5709 if (expr
== error_mark_node
)
5710 return error_mark_node
;
5712 /* [temp.arg.nontype] bullet 1 says the pointer to member
5713 expression must be a pointer-to-member constant. */
5714 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5715 return error_mark_node
;
5717 /* There is no way to disable standard conversions in
5718 resolve_address_of_overloaded_function (called by
5719 instantiate_type). It is possible that the call succeeded by
5720 converting &B::I to &D::I (where B is a base of D), so we need
5721 to reject this conversion here.
5723 Actually, even if there was a way to disable standard conversions,
5724 it would still be better to reject them here so that we can
5725 provide a superior diagnostic. */
5726 if (!same_type_p (TREE_TYPE (expr
), type
))
5728 error ("%qE is not a valid template argument for type %qT "
5729 "because it is of type %qT", expr
, type
,
5731 /* If we are just one standard conversion off, explain. */
5732 if (can_convert (type
, TREE_TYPE (expr
), complain
))
5733 inform (input_location
,
5734 "standard conversions are not allowed in this context");
5738 /* [temp.arg.nontype]/5, bullet 7
5740 For a non-type template-parameter of type pointer to data member,
5741 qualification conversions (_conv.qual_) are applied. */
5742 else if (TYPE_PTRDATAMEM_P (type
))
5744 /* [temp.arg.nontype] bullet 1 says the pointer to member
5745 expression must be a pointer-to-member constant. */
5746 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5747 return error_mark_node
;
5749 expr
= perform_qualification_conversions (type
, expr
);
5750 if (expr
== error_mark_node
)
5753 else if (NULLPTR_TYPE_P (type
))
5755 if (expr
!= nullptr_node
)
5757 error ("%qE is not a valid template argument for type %qT "
5758 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
5763 /* A template non-type parameter must be one of the above. */
5767 /* Sanity check: did we actually convert the argument to the
5769 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5770 (type
, TREE_TYPE (expr
)));
5774 /* Subroutine of coerce_template_template_parms, which returns 1 if
5775 PARM_PARM and ARG_PARM match using the rule for the template
5776 parameters of template template parameters. Both PARM and ARG are
5777 template parameters; the rest of the arguments are the same as for
5778 coerce_template_template_parms.
5781 coerce_template_template_parm (tree parm
,
5783 tsubst_flags_t complain
,
5787 if (arg
== NULL_TREE
|| arg
== error_mark_node
5788 || parm
== NULL_TREE
|| parm
== error_mark_node
)
5791 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5794 switch (TREE_CODE (parm
))
5797 /* We encounter instantiations of templates like
5798 template <template <template <class> class> class TT>
5801 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
5802 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
5804 if (!coerce_template_template_parms
5805 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
5811 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
5812 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5813 /* Argument is a parameter pack but parameter is not. */
5818 /* The tsubst call is used to handle cases such as
5820 template <int> class C {};
5821 template <class T, template <T> class TT> class D {};
5824 i.e. the parameter list of TT depends on earlier parameters. */
5825 if (!uses_template_parms (TREE_TYPE (arg
))
5827 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
5831 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
5832 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
5833 /* Argument is a parameter pack but parameter is not. */
5846 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5847 template template parameters. Both PARM_PARMS and ARG_PARMS are
5848 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5851 Consider the example:
5852 template <class T> class A;
5853 template<template <class U> class TT> class B;
5855 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5856 the parameters to A, and OUTER_ARGS contains A. */
5859 coerce_template_template_parms (tree parm_parms
,
5861 tsubst_flags_t complain
,
5865 int nparms
, nargs
, i
;
5869 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
5870 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
5872 nparms
= TREE_VEC_LENGTH (parm_parms
);
5873 nargs
= TREE_VEC_LENGTH (arg_parms
);
5875 /* Determine whether we have a parameter pack at the end of the
5876 template template parameter's template parameter list. */
5877 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
5879 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
5881 if (parm
== error_mark_node
)
5884 switch (TREE_CODE (parm
))
5888 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5893 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
5903 && !(variadic_p
&& nargs
>= nparms
- 1))
5906 /* Check all of the template parameters except the parameter pack at
5907 the end (if any). */
5908 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
5910 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
5911 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
5914 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
5915 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
5917 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
5925 /* Check each of the template parameters in the template
5926 argument against the template parameter pack at the end of
5927 the template template parameter. */
5928 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
5931 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
5933 for (; i
< nargs
; ++i
)
5935 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
5938 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
5940 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
5949 /* Verifies that the deduced template arguments (in TARGS) for the
5950 template template parameters (in TPARMS) represent valid bindings,
5951 by comparing the template parameter list of each template argument
5952 to the template parameter list of its corresponding template
5953 template parameter, in accordance with DR150. This
5954 routine can only be called after all template arguments have been
5955 deduced. It will return TRUE if all of the template template
5956 parameter bindings are okay, FALSE otherwise. */
5958 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
5960 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
5963 /* We're dealing with template parms in this process. */
5964 ++processing_template_decl
;
5966 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
5968 for (i
= 0; i
< ntparms
; ++i
)
5970 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
5971 tree targ
= TREE_VEC_ELT (targs
, i
);
5973 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
5975 tree packed_args
= NULL_TREE
;
5978 if (ARGUMENT_PACK_P (targ
))
5980 /* Look inside the argument pack. */
5981 packed_args
= ARGUMENT_PACK_ARGS (targ
);
5982 len
= TREE_VEC_LENGTH (packed_args
);
5985 for (idx
= 0; idx
< len
; ++idx
)
5987 tree targ_parms
= NULL_TREE
;
5990 /* Extract the next argument from the argument
5992 targ
= TREE_VEC_ELT (packed_args
, idx
);
5994 if (PACK_EXPANSION_P (targ
))
5995 /* Look at the pattern of the pack expansion. */
5996 targ
= PACK_EXPANSION_PATTERN (targ
);
5998 /* Extract the template parameters from the template
6000 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6001 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6002 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6003 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6005 /* Verify that we can coerce the template template
6006 parameters from the template argument to the template
6007 parameter. This requires an exact match. */
6009 && !coerce_template_template_parms
6010 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6025 --processing_template_decl
;
6029 /* Since type attributes aren't mangled, we need to strip them from
6030 template type arguments. */
6033 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6036 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6038 mv
= TYPE_MAIN_VARIANT (arg
);
6039 arg
= strip_typedefs (arg
);
6040 if (TYPE_ALIGN (arg
) != TYPE_ALIGN (mv
)
6041 || TYPE_ATTRIBUTES (arg
) != TYPE_ATTRIBUTES (mv
))
6043 if (complain
& tf_warning
)
6044 warning (0, "ignoring attributes on template argument %qT", arg
);
6045 arg
= build_aligned_type (arg
, TYPE_ALIGN (mv
));
6046 arg
= cp_build_type_attribute_variant (arg
, TYPE_ATTRIBUTES (mv
));
6051 /* Convert the indicated template ARG as necessary to match the
6052 indicated template PARM. Returns the converted ARG, or
6053 error_mark_node if the conversion was unsuccessful. Error and
6054 warning messages are issued under control of COMPLAIN. This
6055 conversion is for the Ith parameter in the parameter list. ARGS is
6056 the full set of template arguments deduced so far. */
6059 convert_template_argument (tree parm
,
6062 tsubst_flags_t complain
,
6068 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
6070 if (TREE_CODE (arg
) == TREE_LIST
6071 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
6073 /* The template argument was the name of some
6074 member function. That's usually
6075 invalid, but static members are OK. In any
6076 case, grab the underlying fields/functions
6077 and issue an error later if required. */
6078 orig_arg
= TREE_VALUE (arg
);
6079 TREE_TYPE (arg
) = unknown_type_node
;
6084 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
6085 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
6086 || requires_tmpl_type
);
6088 /* When determining whether an argument pack expansion is a template,
6089 look at the pattern. */
6090 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
6091 arg
= PACK_EXPANSION_PATTERN (arg
);
6093 /* Deal with an injected-class-name used as a template template arg. */
6094 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
6096 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
6097 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6099 if (cxx_dialect
>= cxx0x
)
6100 /* OK under DR 1004. */;
6101 else if (complain
& tf_warning_or_error
)
6102 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
6103 " used as template template argument", TYPE_NAME (arg
));
6104 else if (flag_pedantic_errors
)
6112 ((TREE_CODE (arg
) == TEMPLATE_DECL
6113 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
6114 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
6115 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6116 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
6119 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6120 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
6121 arg
= TYPE_STUB_DECL (arg
);
6123 is_type
= TYPE_P (arg
) || is_tmpl_type
;
6125 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
6126 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
6128 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
6130 if (complain
& tf_error
)
6131 error ("invalid use of destructor %qE as a type", orig_arg
);
6132 return error_mark_node
;
6135 permerror (input_location
,
6136 "to refer to a type member of a template parameter, "
6137 "use %<typename %E%>", orig_arg
);
6139 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
6140 TREE_OPERAND (arg
, 1),
6142 complain
& tf_error
);
6146 if (is_type
!= requires_type
)
6150 if (complain
& tf_error
)
6152 error ("type/value mismatch at argument %d in template "
6153 "parameter list for %qD",
6156 error (" expected a constant of type %qT, got %qT",
6158 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6159 else if (requires_tmpl_type
)
6160 error (" expected a class template, got %qE", orig_arg
);
6162 error (" expected a type, got %qE", orig_arg
);
6165 return error_mark_node
;
6167 if (is_tmpl_type
^ requires_tmpl_type
)
6169 if (in_decl
&& (complain
& tf_error
))
6171 error ("type/value mismatch at argument %d in template "
6172 "parameter list for %qD",
6175 error (" expected a type, got %qT", DECL_NAME (arg
));
6177 error (" expected a class template, got %qT", orig_arg
);
6179 return error_mark_node
;
6184 if (requires_tmpl_type
)
6186 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6188 else if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6189 /* The number of argument required is not known yet.
6190 Just accept it for now. */
6191 val
= TREE_TYPE (arg
);
6194 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6197 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6199 if (coerce_template_template_parms (parmparm
, argparm
,
6205 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6207 if (val
!= error_mark_node
)
6209 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6210 val
= TREE_TYPE (val
);
6211 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6212 val
= make_pack_expansion (val
);
6217 if (in_decl
&& (complain
& tf_error
))
6219 error ("type/value mismatch at argument %d in "
6220 "template parameter list for %qD",
6222 error (" expected a template of type %qD, got %qT",
6226 val
= error_mark_node
;
6232 /* We only form one instance of each template specialization.
6233 Therefore, if we use a non-canonical variant (i.e., a
6234 typedef), any future messages referring to the type will use
6235 the typedef, which is confusing if those future uses do not
6236 themselves also use the typedef. */
6238 val
= canonicalize_type_argument (val
, complain
);
6242 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6244 if (invalid_nontype_parm_type_p (t
, complain
))
6245 return error_mark_node
;
6247 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6249 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6253 /* Not sure if this is reachable, but it doesn't hurt
6255 error ("type mismatch in nontype parameter pack");
6256 val
= error_mark_node
;
6259 else if (!uses_template_parms (orig_arg
) && !uses_template_parms (t
))
6260 /* We used to call digest_init here. However, digest_init
6261 will report errors, which we don't want when complain
6262 is zero. More importantly, digest_init will try too
6263 hard to convert things: for example, `0' should not be
6264 converted to pointer type at this point according to
6265 the standard. Accepting this is not merely an
6266 extension, since deciding whether or not these
6267 conversions can occur is part of determining which
6268 function template to call, or whether a given explicit
6269 argument specification is valid. */
6270 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6272 val
= strip_typedefs_expr (orig_arg
);
6274 if (val
== NULL_TREE
)
6275 val
= error_mark_node
;
6276 else if (val
== error_mark_node
&& (complain
& tf_error
))
6277 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6279 if (TREE_CODE (val
) == SCOPE_REF
)
6281 /* Strip typedefs from the SCOPE_REF. */
6282 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
6283 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
6285 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6286 QUALIFIED_NAME_IS_TEMPLATE (val
));
6293 /* Coerces the remaining template arguments in INNER_ARGS (from
6294 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6295 Returns the coerced argument pack. PARM_IDX is the position of this
6296 parameter in the template parameter list. ARGS is the original
6297 template argument list. */
6299 coerce_template_parameter_pack (tree parms
,
6307 tsubst_flags_t complain
)
6309 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6310 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6313 tree packed_types
= NULL_TREE
;
6315 if (arg_idx
> nargs
)
6318 packed_args
= make_tree_vec (nargs
- arg_idx
);
6320 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
6321 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
6323 /* When the template parameter is a non-type template
6324 parameter pack whose type uses parameter packs, we need
6325 to look at each of the template arguments
6326 separately. Build a vector of the types for these
6327 non-type template parameters in PACKED_TYPES. */
6329 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
6330 packed_types
= tsubst_pack_expansion (expansion
, args
,
6333 if (packed_types
== error_mark_node
)
6334 return error_mark_node
;
6336 /* Check that we have the right number of arguments. */
6338 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
6339 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
6342 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
6343 error ("wrong number of template arguments (%d, should be %d)",
6344 nargs
, needed_parms
);
6345 return error_mark_node
;
6348 /* If we aren't able to check the actual arguments now
6349 (because they haven't been expanded yet), we can at least
6350 verify that all of the types used for the non-type
6351 template parameter pack are, in fact, valid for non-type
6352 template parameters. */
6354 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6356 int j
, len
= TREE_VEC_LENGTH (packed_types
);
6357 for (j
= 0; j
< len
; ++j
)
6359 tree t
= TREE_VEC_ELT (packed_types
, j
);
6360 if (invalid_nontype_parm_type_p (t
, complain
))
6361 return error_mark_node
;
6366 /* Convert the remaining arguments, which will be a part of the
6367 parameter pack "parm". */
6368 for (; arg_idx
< nargs
; ++arg_idx
)
6370 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6371 tree actual_parm
= TREE_VALUE (parm
);
6373 if (packed_types
&& !PACK_EXPANSION_P (arg
))
6375 /* When we have a vector of types (corresponding to the
6376 non-type template parameter pack that uses parameter
6377 packs in its type, as mention above), and the
6378 argument is not an expansion (which expands to a
6379 currently unknown number of arguments), clone the
6380 parm and give it the next type in PACKED_TYPES. */
6381 actual_parm
= copy_node (actual_parm
);
6382 TREE_TYPE (actual_parm
) =
6383 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
6386 if (arg
== error_mark_node
)
6388 if (complain
& tf_error
)
6389 error ("template argument %d is invalid", arg_idx
+ 1);
6392 arg
= convert_template_argument (actual_parm
,
6393 arg
, new_args
, complain
, parm_idx
,
6395 if (arg
== error_mark_node
)
6397 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
6400 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6401 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
6402 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
6405 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
6406 TREE_TYPE (argument_pack
)
6407 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
6408 TREE_CONSTANT (argument_pack
) = 1;
6411 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
6412 #ifdef ENABLE_CHECKING
6413 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
6414 TREE_VEC_LENGTH (packed_args
));
6416 return argument_pack
;
6419 /* Returns true if the template argument vector ARGS contains
6420 any pack expansions, false otherwise. */
6423 any_pack_expanson_args_p (tree args
)
6427 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
6428 if (PACK_EXPANSION_P (TREE_VEC_ELT (args
, i
)))
6433 /* Convert all template arguments to their appropriate types, and
6434 return a vector containing the innermost resulting template
6435 arguments. If any error occurs, return error_mark_node. Error and
6436 warning messages are issued under control of COMPLAIN.
6438 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6439 for arguments not specified in ARGS. Otherwise, if
6440 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6441 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6442 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6446 coerce_template_parms (tree parms
,
6449 tsubst_flags_t complain
,
6450 bool require_all_args
,
6451 bool use_default_args
)
6453 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
6456 tree new_inner_args
;
6457 int saved_unevaluated_operand
;
6458 int saved_inhibit_evaluation_warnings
;
6460 /* When used as a boolean value, indicates whether this is a
6461 variadic template parameter list. Since it's an int, we can also
6462 subtract it from nparms to get the number of non-variadic
6465 int post_variadic_parms
= 0;
6467 if (args
== error_mark_node
)
6468 return error_mark_node
;
6470 nparms
= TREE_VEC_LENGTH (parms
);
6472 /* Determine if there are any parameter packs. */
6473 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
6475 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
6477 ++post_variadic_parms
;
6478 if (template_parameter_pack_p (tparm
))
6482 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
6483 /* If there are no parameters that follow a parameter pack, we need to
6484 expand any argument packs so that we can deduce a parameter pack from
6485 some non-packed args followed by an argument pack, as in variadic85.C.
6486 If there are such parameters, we need to leave argument packs intact
6487 so the arguments are assigned properly. This can happen when dealing
6488 with a nested class inside a partial specialization of a class
6489 template, as in variadic92.C, or when deducing a template parameter pack
6490 from a sub-declarator, as in variadic114.C. */
6491 if (!post_variadic_parms
)
6492 inner_args
= expand_template_argument_pack (inner_args
);
6494 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6495 if ((nargs
> nparms
&& !variadic_p
)
6496 || (nargs
< nparms
- variadic_p
6498 && !any_pack_expanson_args_p (inner_args
)
6499 && (!use_default_args
6500 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
6501 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
6503 if (complain
& tf_error
)
6507 nparms
-= variadic_p
;
6508 error ("wrong number of template arguments "
6509 "(%d, should be %d or more)", nargs
, nparms
);
6512 error ("wrong number of template arguments "
6513 "(%d, should be %d)", nargs
, nparms
);
6516 error ("provided for %q+D", in_decl
);
6519 return error_mark_node
;
6522 /* We need to evaluate the template arguments, even though this
6523 template-id may be nested within a "sizeof". */
6524 saved_unevaluated_operand
= cp_unevaluated_operand
;
6525 cp_unevaluated_operand
= 0;
6526 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6527 c_inhibit_evaluation_warnings
= 0;
6528 new_inner_args
= make_tree_vec (nparms
);
6529 new_args
= add_outermost_template_args (args
, new_inner_args
);
6530 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
6535 /* Get the Ith template parameter. */
6536 parm
= TREE_VEC_ELT (parms
, parm_idx
);
6538 if (parm
== error_mark_node
)
6540 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
6544 /* Calculate the next argument. */
6545 if (arg_idx
< nargs
)
6546 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6550 if (template_parameter_pack_p (TREE_VALUE (parm
))
6551 && !(arg
&& ARGUMENT_PACK_P (arg
)))
6553 /* All remaining arguments will be placed in the
6554 template parameter pack PARM. */
6555 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
6556 inner_args
, arg_idx
,
6560 /* Store this argument. */
6561 if (arg
== error_mark_node
)
6563 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
6565 /* We are done with all of the arguments. */
6572 if (PACK_EXPANSION_P (arg
))
6574 /* We don't know how many args we have yet, just
6575 use the unconverted ones for now. */
6576 new_inner_args
= inner_args
;
6580 else if (require_all_args
)
6582 /* There must be a default arg in this case. */
6583 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
6585 /* The position of the first default template argument,
6586 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6588 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6589 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
, arg_idx
);
6594 if (arg
== error_mark_node
)
6596 if (complain
& tf_error
)
6597 error ("template argument %d is invalid", arg_idx
+ 1);
6600 /* This only occurs if there was an error in the template
6601 parameter list itself (which we would already have
6602 reported) that we are trying to recover from, e.g., a class
6603 template with a parameter list such as
6604 template<typename..., typename>. */
6607 arg
= convert_template_argument (TREE_VALUE (parm
),
6608 arg
, new_args
, complain
,
6611 if (arg
== error_mark_node
)
6613 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
6615 cp_unevaluated_operand
= saved_unevaluated_operand
;
6616 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
6619 return error_mark_node
;
6621 #ifdef ENABLE_CHECKING
6622 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6623 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6624 TREE_VEC_LENGTH (new_inner_args
));
6627 return new_inner_args
;
6630 /* Returns 1 if template args OT and NT are equivalent. */
6633 template_args_equal (tree ot
, tree nt
)
6637 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
6640 if (TREE_CODE (nt
) == TREE_VEC
)
6641 /* For member templates */
6642 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
6643 else if (PACK_EXPANSION_P (ot
))
6644 return (PACK_EXPANSION_P (nt
)
6645 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
6646 PACK_EXPANSION_PATTERN (nt
))
6647 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
6648 PACK_EXPANSION_EXTRA_ARGS (nt
)));
6649 else if (ARGUMENT_PACK_P (ot
))
6654 if (!ARGUMENT_PACK_P (nt
))
6657 opack
= ARGUMENT_PACK_ARGS (ot
);
6658 npack
= ARGUMENT_PACK_ARGS (nt
);
6659 len
= TREE_VEC_LENGTH (opack
);
6660 if (TREE_VEC_LENGTH (npack
) != len
)
6662 for (i
= 0; i
< len
; ++i
)
6663 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
6664 TREE_VEC_ELT (npack
, i
)))
6668 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
6670 /* We get here probably because we are in the middle of substituting
6671 into the pattern of a pack expansion. In that case the
6672 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6673 interested in. So we want to use the initial pack argument for
6675 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
6676 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
6677 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
6678 return template_args_equal (ot
, nt
);
6680 else if (TYPE_P (nt
))
6681 return TYPE_P (ot
) && same_type_p (ot
, nt
);
6682 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
6685 return cp_tree_equal (ot
, nt
);
6688 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6689 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6690 NEWARG_PTR with the offending arguments if they are non-NULL. */
6693 comp_template_args_with_info (tree oldargs
, tree newargs
,
6694 tree
*oldarg_ptr
, tree
*newarg_ptr
)
6698 if (oldargs
== newargs
)
6701 if (!oldargs
|| !newargs
)
6704 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
6707 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
6709 tree nt
= TREE_VEC_ELT (newargs
, i
);
6710 tree ot
= TREE_VEC_ELT (oldargs
, i
);
6712 if (! template_args_equal (ot
, nt
))
6714 if (oldarg_ptr
!= NULL
)
6716 if (newarg_ptr
!= NULL
)
6724 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6725 of template arguments. Returns 0 otherwise. */
6728 comp_template_args (tree oldargs
, tree newargs
)
6730 return comp_template_args_with_info (oldargs
, newargs
, NULL
, NULL
);
6734 add_pending_template (tree d
)
6736 tree ti
= (TYPE_P (d
)
6737 ? CLASSTYPE_TEMPLATE_INFO (d
)
6738 : DECL_TEMPLATE_INFO (d
));
6739 struct pending_template
*pt
;
6742 if (TI_PENDING_TEMPLATE_FLAG (ti
))
6745 /* We are called both from instantiate_decl, where we've already had a
6746 tinst_level pushed, and instantiate_template, where we haven't.
6748 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
6751 push_tinst_level (d
);
6753 pt
= ggc_alloc_pending_template ();
6755 pt
->tinst
= current_tinst_level
;
6756 if (last_pending_template
)
6757 last_pending_template
->next
= pt
;
6759 pending_templates
= pt
;
6761 last_pending_template
= pt
;
6763 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
6770 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6771 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6772 documentation for TEMPLATE_ID_EXPR. */
6775 lookup_template_function (tree fns
, tree arglist
)
6779 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
6780 return error_mark_node
;
6782 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
6784 if (!is_overloaded_fn (fns
) && TREE_CODE (fns
) != IDENTIFIER_NODE
)
6786 error ("%q#D is not a function template", fns
);
6787 return error_mark_node
;
6790 if (BASELINK_P (fns
))
6792 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
6794 BASELINK_FUNCTIONS (fns
),
6799 type
= TREE_TYPE (fns
);
6800 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
6801 type
= unknown_type_node
;
6803 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
6806 /* Within the scope of a template class S<T>, the name S gets bound
6807 (in build_self_reference) to a TYPE_DECL for the class, not a
6808 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6809 or one of its enclosing classes, and that type is a template,
6810 return the associated TEMPLATE_DECL. Otherwise, the original
6813 Also handle the case when DECL is a TREE_LIST of ambiguous
6814 injected-class-names from different bases. */
6817 maybe_get_template_decl_from_type_decl (tree decl
)
6819 if (decl
== NULL_TREE
)
6822 /* DR 176: A lookup that finds an injected-class-name (10.2
6823 [class.member.lookup]) can result in an ambiguity in certain cases
6824 (for example, if it is found in more than one base class). If all of
6825 the injected-class-names that are found refer to specializations of
6826 the same class template, and if the name is followed by a
6827 template-argument-list, the reference refers to the class template
6828 itself and not a specialization thereof, and is not ambiguous. */
6829 if (TREE_CODE (decl
) == TREE_LIST
)
6831 tree t
, tmpl
= NULL_TREE
;
6832 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
6834 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
6837 else if (tmpl
!= elt
)
6840 if (tmpl
&& t
== NULL_TREE
)
6846 return (decl
!= NULL_TREE
6847 && DECL_SELF_REFERENCE_P (decl
)
6848 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
6849 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
6852 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6853 parameters, find the desired type.
6855 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6857 IN_DECL, if non-NULL, is the template declaration we are trying to
6860 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6861 the class we are looking up.
6863 Issue error and warning messages under control of COMPLAIN.
6865 If the template class is really a local class in a template
6866 function, then the FUNCTION_CONTEXT is the function in which it is
6869 ??? Note that this function is currently called *twice* for each
6870 template-id: the first time from the parser, while creating the
6871 incomplete type (finish_template_type), and the second type during the
6872 real instantiation (instantiate_template_class). This is surely something
6873 that we want to avoid. It also causes some problems with argument
6874 coercion (see convert_nontype_argument for more information on this). */
6877 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
6878 int entering_scope
, tsubst_flags_t complain
)
6880 tree templ
= NULL_TREE
, parmlist
;
6887 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
6889 tree value
= innermost_non_namespace_value (d1
);
6890 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
6895 push_decl_namespace (context
);
6896 templ
= lookup_name (d1
);
6897 templ
= maybe_get_template_decl_from_type_decl (templ
);
6899 pop_decl_namespace ();
6902 context
= DECL_CONTEXT (templ
);
6904 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
6906 tree type
= TREE_TYPE (d1
);
6908 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6909 an implicit typename for the second A. Deal with it. */
6910 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
6911 type
= TREE_TYPE (type
);
6913 if (CLASSTYPE_TEMPLATE_INFO (type
))
6915 templ
= CLASSTYPE_TI_TEMPLATE (type
);
6916 d1
= DECL_NAME (templ
);
6919 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
6920 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
6922 templ
= TYPE_TI_TEMPLATE (d1
);
6923 d1
= DECL_NAME (templ
);
6925 else if (TREE_CODE (d1
) == TEMPLATE_DECL
6926 && DECL_TEMPLATE_RESULT (d1
)
6927 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
6930 d1
= DECL_NAME (templ
);
6931 context
= DECL_CONTEXT (templ
);
6934 /* Issue an error message if we didn't find a template. */
6937 if (complain
& tf_error
)
6938 error ("%qT is not a template", d1
);
6939 return error_mark_node
;
6942 if (TREE_CODE (templ
) != TEMPLATE_DECL
6943 /* Make sure it's a user visible template, if it was named by
6945 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
6946 && !PRIMARY_TEMPLATE_P (templ
)))
6948 if (complain
& tf_error
)
6950 error ("non-template type %qT used as a template", d1
);
6952 error ("for template declaration %q+D", in_decl
);
6954 return error_mark_node
;
6957 complain
&= ~tf_user
;
6959 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
6961 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
6962 template arguments */
6968 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
6970 /* Consider an example where a template template parameter declared as
6972 template <class T, class U = std::allocator<T> > class TT
6974 The template parameter level of T and U are one level larger than
6975 of TT. To proper process the default argument of U, say when an
6976 instantiation `TT<int>' is seen, we need to build the full
6977 arguments containing {int} as the innermost level. Outer levels,
6978 available when not appearing as default template argument, can be
6979 obtained from the arguments of the enclosing template.
6981 Suppose that TT is later substituted with std::vector. The above
6982 instantiation is `TT<int, std::allocator<T> >' with TT at
6983 level 1, and T at level 2, while the template arguments at level 1
6984 becomes {std::vector} and the inner level 2 is {int}. */
6986 outer
= DECL_CONTEXT (templ
);
6988 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
6989 else if (current_template_parms
)
6990 /* This is an argument of the current template, so we haven't set
6991 DECL_CONTEXT yet. */
6992 outer
= current_template_args ();
6995 arglist
= add_to_template_args (outer
, arglist
);
6997 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
6999 /*require_all_args=*/true,
7000 /*use_default_args=*/true);
7001 if (arglist2
== error_mark_node
7002 || (!uses_template_parms (arglist2
)
7003 && check_instantiated_args (templ
, arglist2
, complain
)))
7004 return error_mark_node
;
7006 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
7011 tree template_type
= TREE_TYPE (templ
);
7014 tree found
= NULL_TREE
;
7017 int is_dependent_type
;
7018 int use_partial_inst_tmpl
= false;
7020 if (template_type
== error_mark_node
)
7021 /* An error occured while building the template TEMPL, and a
7022 diagnostic has most certainly been emitted for that
7023 already. Let's propagate that error. */
7024 return error_mark_node
;
7026 gen_tmpl
= most_general_template (templ
);
7027 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7028 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
7029 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7031 if (arg_depth
== 1 && parm_depth
> 1)
7033 /* We've been given an incomplete set of template arguments.
7036 template <class T> struct S1 {
7037 template <class U> struct S2 {};
7038 template <class U> struct S2<U*> {};
7041 we will be called with an ARGLIST of `U*', but the
7042 TEMPLATE will be `template <class T> template
7043 <class U> struct S1<T>::S2'. We must fill in the missing
7046 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
7048 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7051 /* Now we should have enough arguments. */
7052 gcc_assert (parm_depth
== arg_depth
);
7054 /* From here on, we're only interested in the most general
7057 /* Calculate the BOUND_ARGS. These will be the args that are
7058 actually tsubst'd into the definition to create the
7062 /* We have multiple levels of arguments to coerce, at once. */
7064 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
7066 tree bound_args
= make_tree_vec (parm_depth
);
7068 for (i
= saved_depth
,
7069 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7070 i
> 0 && t
!= NULL_TREE
;
7071 --i
, t
= TREE_CHAIN (t
))
7074 if (i
== saved_depth
)
7075 a
= coerce_template_parms (TREE_VALUE (t
),
7078 /*require_all_args=*/true,
7079 /*use_default_args=*/true);
7081 /* Outer levels should have already been coerced. */
7082 a
= TMPL_ARGS_LEVEL (arglist
, i
);
7084 /* Don't process further if one of the levels fails. */
7085 if (a
== error_mark_node
)
7087 /* Restore the ARGLIST to its full size. */
7088 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7089 return error_mark_node
;
7092 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
7094 /* We temporarily reduce the length of the ARGLIST so
7095 that coerce_template_parms will see only the arguments
7096 corresponding to the template parameters it is
7098 TREE_VEC_LENGTH (arglist
)--;
7101 /* Restore the ARGLIST to its full size. */
7102 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7104 arglist
= bound_args
;
7108 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
7109 INNERMOST_TEMPLATE_ARGS (arglist
),
7112 /*require_all_args=*/true,
7113 /*use_default_args=*/true);
7115 if (arglist
== error_mark_node
)
7116 /* We were unable to bind the arguments. */
7117 return error_mark_node
;
7119 /* In the scope of a template class, explicit references to the
7120 template class refer to the type of the template, not any
7121 instantiation of it. For example, in:
7123 template <class T> class C { void f(C<T>); }
7125 the `C<T>' is just the same as `C'. Outside of the
7126 class, however, such a reference is an instantiation. */
7128 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
7129 || currently_open_class (template_type
))
7130 /* comp_template_args is expensive, check it last. */
7131 && comp_template_args (TYPE_TI_ARGS (template_type
),
7133 return template_type
;
7135 /* If we already have this specialization, return it. */
7136 elt
.tmpl
= gen_tmpl
;
7138 hash
= hash_specialization (&elt
);
7139 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
7145 is_dependent_type
= uses_template_parms (arglist
);
7147 /* If the deduced arguments are invalid, then the binding
7149 if (!is_dependent_type
7150 && check_instantiated_args (gen_tmpl
,
7151 INNERMOST_TEMPLATE_ARGS (arglist
),
7153 return error_mark_node
;
7155 if (!is_dependent_type
7156 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
7157 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
7158 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
7160 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
7161 DECL_NAME (gen_tmpl
),
7162 /*tag_scope=*/ts_global
);
7166 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
7168 if (context
== error_mark_node
)
7169 return error_mark_node
;
7172 context
= global_namespace
;
7174 /* Create the type. */
7175 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
7177 if (!is_dependent_type
)
7179 set_current_access_from_decl (TYPE_NAME (template_type
));
7180 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
7181 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
7182 arglist
, complain
, in_decl
),
7183 SCOPED_ENUM_P (template_type
), NULL
);
7187 /* We don't want to call start_enum for this type, since
7188 the values for the enumeration constants may involve
7189 template parameters. And, no one should be interested
7190 in the enumeration constants for such a type. */
7191 t
= cxx_make_type (ENUMERAL_TYPE
);
7192 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
7194 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
7195 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
7196 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7198 else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7200 /* The user referred to a specialization of an alias
7201 template represented by GEN_TMPL.
7203 [temp.alias]/2 says:
7205 When a template-id refers to the specialization of an
7206 alias template, it is equivalent to the associated
7207 type obtained by substitution of its
7208 template-arguments for the template-parameters in the
7209 type-id of the alias template. */
7211 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
7212 /* Note that the call above (by indirectly calling
7213 register_specialization in tsubst_decl) registers the
7214 TYPE_DECL representing the specialization of the alias
7215 template. So next time someone substitutes ARGLIST for
7216 the template parms into the alias template (GEN_TMPL),
7217 she'll get that TYPE_DECL back. */
7219 if (t
== error_mark_node
)
7222 else if (CLASS_TYPE_P (template_type
))
7224 t
= make_class_type (TREE_CODE (template_type
));
7225 CLASSTYPE_DECLARED_CLASS (t
)
7226 = CLASSTYPE_DECLARED_CLASS (template_type
);
7227 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7228 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7230 /* A local class. Make sure the decl gets registered properly. */
7231 if (context
== current_function_decl
)
7232 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7234 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7235 /* This instantiation is another name for the primary
7236 template type. Set the TYPE_CANONICAL field
7238 TYPE_CANONICAL (t
) = template_type
;
7239 else if (any_template_arguments_need_structural_equality_p (arglist
))
7240 /* Some of the template arguments require structural
7241 equality testing, so this template class requires
7242 structural equality testing. */
7243 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7248 /* If we called start_enum or pushtag above, this information
7249 will already be set up. */
7252 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
7254 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
7255 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
7256 DECL_SOURCE_LOCATION (type_decl
)
7257 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
7260 type_decl
= TYPE_NAME (t
);
7262 if (CLASS_TYPE_P (template_type
))
7264 TREE_PRIVATE (type_decl
)
7265 = TREE_PRIVATE (TYPE_STUB_DECL (template_type
));
7266 TREE_PROTECTED (type_decl
)
7267 = TREE_PROTECTED (TYPE_STUB_DECL (template_type
));
7268 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
7270 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
7271 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
7275 /* Let's consider the explicit specialization of a member
7276 of a class template specialization that is implicitely instantiated,
7281 template<class U> struct M {}; //#0
7286 struct S<int>::M<char> //#1
7290 [temp.expl.spec]/4 says this is valid.
7292 In this case, when we write:
7295 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7298 When we encounter #1, we want to store the partial instantiation
7299 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
7301 For all cases other than this "explicit specialization of member of a
7302 class template", we just want to store the most general template into
7303 the CLASSTYPE_TI_TEMPLATE of M.
7305 This case of "explicit specialization of member of a class template"
7307 1/ the enclosing class is an instantiation of, and therefore not
7308 the same as, the context of the most general template, and
7309 2/ we aren't looking at the partial instantiation itself, i.e.
7310 the innermost arguments are not the same as the innermost parms of
7311 the most general template.
7313 So it's only when 1/ and 2/ happens that we want to use the partial
7314 instantiation of the member template in lieu of its most general
7317 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
7318 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
7319 /* the enclosing class must be an instantiation... */
7320 && CLASS_TYPE_P (context
)
7321 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
7323 tree partial_inst_args
;
7324 TREE_VEC_LENGTH (arglist
)--;
7325 ++processing_template_decl
;
7327 tsubst (INNERMOST_TEMPLATE_ARGS
7328 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
7329 arglist
, complain
, NULL_TREE
);
7330 --processing_template_decl
;
7331 TREE_VEC_LENGTH (arglist
)++;
7332 use_partial_inst_tmpl
=
7333 /*...and we must not be looking at the partial instantiation
7335 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
7339 if (!use_partial_inst_tmpl
)
7340 /* This case is easy; there are no member templates involved. */
7344 /* This is a full instantiation of a member template. Find
7345 the partial instantiation of which this is an instance. */
7347 /* Temporarily reduce by one the number of levels in the ARGLIST
7348 so as to avoid comparing the last set of arguments. */
7349 TREE_VEC_LENGTH (arglist
)--;
7350 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
7351 TREE_VEC_LENGTH (arglist
)++;
7352 /* FOUND is either a proper class type, or an alias
7353 template specialization. In the later case, it's a
7354 TYPE_DECL, resulting from the substituting of arguments
7355 for parameters in the TYPE_DECL of the alias template
7356 done earlier. So be careful while getting the template
7358 found
= TREE_CODE (found
) == TYPE_DECL
7359 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
7360 : CLASSTYPE_TI_TEMPLATE (found
);
7363 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
7366 slot
= htab_find_slot_with_hash (type_specializations
,
7367 &elt
, hash
, INSERT
);
7368 entry
= ggc_alloc_spec_entry ();
7372 /* Note this use of the partial instantiation so we can check it
7373 later in maybe_process_partial_specialization. */
7374 DECL_TEMPLATE_INSTANTIATIONS (templ
)
7375 = tree_cons (arglist
, t
,
7376 DECL_TEMPLATE_INSTANTIATIONS (templ
));
7378 if (TREE_CODE (t
) == ENUMERAL_TYPE
&& !is_dependent_type
)
7379 /* Now that the type has been registered on the instantiations
7380 list, we set up the enumerators. Because the enumeration
7381 constants may involve the enumeration type itself, we make
7382 sure to register the type first, and then create the
7383 constants. That way, doing tsubst_expr for the enumeration
7384 constants won't result in recursive calls here; we'll find
7385 the instantiation and exit above. */
7386 tsubst_enum (template_type
, t
, arglist
);
7388 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
7389 /* If the type makes use of template parameters, the
7390 code that generates debugging information will crash. */
7391 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
7393 /* Possibly limit visibility based on template args. */
7394 TREE_PUBLIC (type_decl
) = 1;
7395 determine_visibility (type_decl
);
7401 /* Wrapper for lookup_template_class_1. */
7404 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
7405 int entering_scope
, tsubst_flags_t complain
)
7408 timevar_push (TV_TEMPLATE_INST
);
7409 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
7410 entering_scope
, complain
);
7411 timevar_pop (TV_TEMPLATE_INST
);
7419 /* True when we should also visit template parameters that occur in
7420 non-deduced contexts. */
7421 bool include_nondeduced_p
;
7422 struct pointer_set_t
*visited
;
7425 /* Called from for_each_template_parm via walk_tree. */
7428 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
7431 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
7432 tree_fn_t fn
= pfd
->fn
;
7433 void *data
= pfd
->data
;
7436 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
7437 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
7438 pfd
->include_nondeduced_p
))
7439 return error_mark_node
;
7441 switch (TREE_CODE (t
))
7444 if (TYPE_PTRMEMFUNC_P (t
))
7450 if (!TYPE_TEMPLATE_INFO (t
))
7452 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
7453 fn
, data
, pfd
->visited
,
7454 pfd
->include_nondeduced_p
))
7455 return error_mark_node
;
7459 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
7460 fn
, data
, pfd
->visited
,
7461 pfd
->include_nondeduced_p
)
7462 || for_each_template_parm (TYPE_MAX_VALUE (t
),
7463 fn
, data
, pfd
->visited
,
7464 pfd
->include_nondeduced_p
))
7465 return error_mark_node
;
7469 /* Since we're not going to walk subtrees, we have to do this
7471 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
7472 pfd
->visited
, pfd
->include_nondeduced_p
))
7473 return error_mark_node
;
7477 /* Check the return type. */
7478 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7479 pfd
->include_nondeduced_p
))
7480 return error_mark_node
;
7482 /* Check the parameter types. Since default arguments are not
7483 instantiated until they are needed, the TYPE_ARG_TYPES may
7484 contain expressions that involve template parameters. But,
7485 no-one should be looking at them yet. And, once they're
7486 instantiated, they don't contain template parameters, so
7487 there's no point in looking at them then, either. */
7491 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
7492 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
7493 pfd
->visited
, pfd
->include_nondeduced_p
))
7494 return error_mark_node
;
7496 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7497 want walk_tree walking into them itself. */
7503 case UNDERLYING_TYPE
:
7504 if (pfd
->include_nondeduced_p
7505 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
7507 pfd
->include_nondeduced_p
))
7508 return error_mark_node
;
7513 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
7514 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
7515 pfd
->visited
, pfd
->include_nondeduced_p
))
7516 return error_mark_node
;
7521 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
7522 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
7523 pfd
->visited
, pfd
->include_nondeduced_p
))
7524 return error_mark_node
;
7525 if (DECL_CONTEXT (t
)
7526 && pfd
->include_nondeduced_p
7527 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
7528 pfd
->visited
, pfd
->include_nondeduced_p
))
7529 return error_mark_node
;
7532 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7533 /* Record template parameters such as `T' inside `TT<T>'. */
7534 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
7535 pfd
->include_nondeduced_p
))
7536 return error_mark_node
;
7539 case TEMPLATE_TEMPLATE_PARM
:
7540 case TEMPLATE_TYPE_PARM
:
7541 case TEMPLATE_PARM_INDEX
:
7542 if (fn
&& (*fn
)(t
, data
))
7543 return error_mark_node
;
7545 return error_mark_node
;
7549 /* A template template parameter is encountered. */
7550 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
7551 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7552 pfd
->include_nondeduced_p
))
7553 return error_mark_node
;
7555 /* Already substituted template template parameter */
7561 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
7563 pfd
->include_nondeduced_p
))
7564 return error_mark_node
;
7568 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
7569 && pfd
->include_nondeduced_p
7570 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7571 (TREE_TYPE (t
)), fn
, data
,
7572 pfd
->visited
, pfd
->include_nondeduced_p
))
7573 return error_mark_node
;
7578 /* If there's no type, then this thing must be some expression
7579 involving template parameters. */
7580 if (!fn
&& !TREE_TYPE (t
))
7581 return error_mark_node
;
7586 case IMPLICIT_CONV_EXPR
:
7587 case REINTERPRET_CAST_EXPR
:
7588 case CONST_CAST_EXPR
:
7589 case STATIC_CAST_EXPR
:
7590 case DYNAMIC_CAST_EXPR
:
7594 case PSEUDO_DTOR_EXPR
:
7596 return error_mark_node
;
7603 /* We didn't find any template parameters we liked. */
7607 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7608 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7609 call FN with the parameter and the DATA.
7610 If FN returns nonzero, the iteration is terminated, and
7611 for_each_template_parm returns 1. Otherwise, the iteration
7612 continues. If FN never returns a nonzero value, the value
7613 returned by for_each_template_parm is 0. If FN is NULL, it is
7614 considered to be the function which always returns 1.
7616 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7617 parameters that occur in non-deduced contexts. When false, only
7618 visits those template parameters that can be deduced. */
7621 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
7622 struct pointer_set_t
*visited
,
7623 bool include_nondeduced_p
)
7625 struct pair_fn_data pfd
;
7631 pfd
.include_nondeduced_p
= include_nondeduced_p
;
7633 /* Walk the tree. (Conceptually, we would like to walk without
7634 duplicates, but for_each_template_parm_r recursively calls
7635 for_each_template_parm, so we would need to reorganize a fair
7636 bit to use walk_tree_without_duplicates, so we keep our own
7639 pfd
.visited
= visited
;
7641 pfd
.visited
= pointer_set_create ();
7642 result
= cp_walk_tree (&t
,
7643 for_each_template_parm_r
,
7645 pfd
.visited
) != NULL_TREE
;
7650 pointer_set_destroy (pfd
.visited
);
7657 /* Returns true if T depends on any template parameter. */
7660 uses_template_parms (tree t
)
7663 int saved_processing_template_decl
;
7665 saved_processing_template_decl
= processing_template_decl
;
7666 if (!saved_processing_template_decl
)
7667 processing_template_decl
= 1;
7669 dependent_p
= dependent_type_p (t
);
7670 else if (TREE_CODE (t
) == TREE_VEC
)
7671 dependent_p
= any_dependent_template_arguments_p (t
);
7672 else if (TREE_CODE (t
) == TREE_LIST
)
7673 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
7674 || uses_template_parms (TREE_CHAIN (t
)));
7675 else if (TREE_CODE (t
) == TYPE_DECL
)
7676 dependent_p
= dependent_type_p (TREE_TYPE (t
));
7679 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
7680 || TREE_CODE (t
) == OVERLOAD
7682 || TREE_CODE (t
) == IDENTIFIER_NODE
7683 || TREE_CODE (t
) == TRAIT_EXPR
7684 || TREE_CODE (t
) == CONSTRUCTOR
7685 || CONSTANT_CLASS_P (t
))
7686 dependent_p
= (type_dependent_expression_p (t
)
7687 || value_dependent_expression_p (t
));
7690 gcc_assert (t
== error_mark_node
);
7691 dependent_p
= false;
7694 processing_template_decl
= saved_processing_template_decl
;
7699 /* Returns true iff current_function_decl is an incompletely instantiated
7700 template. Useful instead of processing_template_decl because the latter
7701 is set to 0 during fold_non_dependent_expr. */
7704 in_template_function (void)
7706 tree fn
= current_function_decl
;
7708 ++processing_template_decl
;
7709 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
7710 && DECL_TEMPLATE_INFO (fn
)
7711 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
7712 --processing_template_decl
;
7716 /* Returns true if T depends on any template parameter with level LEVEL. */
7719 uses_template_parms_level (tree t
, int level
)
7721 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
7722 /*include_nondeduced_p=*/true);
7725 /* Returns TRUE iff INST is an instantiation we don't need to do in an
7726 ill-formed translation unit, i.e. a variable or function that isn't
7727 usable in a constant expression. */
7730 neglectable_inst_p (tree d
)
7733 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
7734 : decl_maybe_constant_var_p (d
)));
7737 /* Returns TRUE iff we should refuse to instantiate DECL because it's
7738 neglectable and instantiated from within an erroneous instantiation. */
7741 limit_bad_template_recursion (tree decl
)
7743 struct tinst_level
*lev
= current_tinst_level
;
7744 int errs
= errorcount
+ sorrycount
;
7745 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
7748 for (; lev
; lev
= lev
->next
)
7749 if (neglectable_inst_p (lev
->decl
))
7752 return (lev
&& errs
> lev
->errors
);
7755 static int tinst_depth
;
7756 extern int max_tinst_depth
;
7757 #ifdef GATHER_STATISTICS
7760 static GTY(()) struct tinst_level
*last_error_tinst_level
;
7762 /* We're starting to instantiate D; record the template instantiation context
7763 for diagnostics and to restore it later. */
7766 push_tinst_level (tree d
)
7768 struct tinst_level
*new_level
;
7770 if (tinst_depth
>= max_tinst_depth
)
7772 last_error_tinst_level
= current_tinst_level
;
7773 if (TREE_CODE (d
) == TREE_LIST
)
7774 error ("template instantiation depth exceeds maximum of %d (use "
7775 "-ftemplate-depth= to increase the maximum) substituting %qS",
7776 max_tinst_depth
, d
);
7778 error ("template instantiation depth exceeds maximum of %d (use "
7779 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7780 max_tinst_depth
, d
);
7782 print_instantiation_context ();
7787 /* If the current instantiation caused problems, don't let it instantiate
7788 anything else. Do allow deduction substitution and decls usable in
7789 constant expressions. */
7790 if (limit_bad_template_recursion (d
))
7793 new_level
= ggc_alloc_tinst_level ();
7794 new_level
->decl
= d
;
7795 new_level
->locus
= input_location
;
7796 new_level
->errors
= errorcount
+sorrycount
;
7797 new_level
->in_system_header_p
= in_system_header
;
7798 new_level
->next
= current_tinst_level
;
7799 current_tinst_level
= new_level
;
7802 #ifdef GATHER_STATISTICS
7803 if (tinst_depth
> depth_reached
)
7804 depth_reached
= tinst_depth
;
7810 /* We're done instantiating this template; return to the instantiation
7814 pop_tinst_level (void)
7816 /* Restore the filename and line number stashed away when we started
7817 this instantiation. */
7818 input_location
= current_tinst_level
->locus
;
7819 current_tinst_level
= current_tinst_level
->next
;
7823 /* We're instantiating a deferred template; restore the template
7824 instantiation context in which the instantiation was requested, which
7825 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7828 reopen_tinst_level (struct tinst_level
*level
)
7830 struct tinst_level
*t
;
7833 for (t
= level
; t
; t
= t
->next
)
7836 current_tinst_level
= level
;
7838 if (current_tinst_level
)
7839 current_tinst_level
->errors
= errorcount
+sorrycount
;
7843 /* Returns the TINST_LEVEL which gives the original instantiation
7846 struct tinst_level
*
7847 outermost_tinst_level (void)
7849 struct tinst_level
*level
= current_tinst_level
;
7852 level
= level
->next
;
7856 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
7857 vector of template arguments, as for tsubst.
7859 Returns an appropriate tsubst'd friend declaration. */
7862 tsubst_friend_function (tree decl
, tree args
)
7866 if (TREE_CODE (decl
) == FUNCTION_DECL
7867 && DECL_TEMPLATE_INSTANTIATION (decl
)
7868 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
7869 /* This was a friend declared with an explicit template
7870 argument list, e.g.:
7874 to indicate that f was a template instantiation, not a new
7875 function declaration. Now, we have to figure out what
7876 instantiation of what template. */
7878 tree template_id
, arglist
, fns
;
7881 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
7883 /* Friend functions are looked up in the containing namespace scope.
7884 We must enter that scope, to avoid finding member functions of the
7885 current class with same name. */
7886 push_nested_namespace (ns
);
7887 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
7888 tf_warning_or_error
, NULL_TREE
,
7889 /*integral_constant_expression_p=*/false);
7890 pop_nested_namespace (ns
);
7891 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
7892 tf_warning_or_error
, NULL_TREE
);
7893 template_id
= lookup_template_function (fns
, arglist
);
7895 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
7896 tmpl
= determine_specialization (template_id
, new_friend
,
7898 /*need_member_template=*/0,
7899 TREE_VEC_LENGTH (args
),
7901 return instantiate_template (tmpl
, new_args
, tf_error
);
7904 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
7906 /* The NEW_FRIEND will look like an instantiation, to the
7907 compiler, but is not an instantiation from the point of view of
7908 the language. For example, we might have had:
7910 template <class T> struct S {
7911 template <class U> friend void f(T, U);
7914 Then, in S<int>, template <class U> void f(int, U) is not an
7915 instantiation of anything. */
7916 if (new_friend
== error_mark_node
)
7917 return error_mark_node
;
7919 DECL_USE_TEMPLATE (new_friend
) = 0;
7920 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
7922 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
7923 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
7924 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
7927 /* The mangled name for the NEW_FRIEND is incorrect. The function
7928 is not a template instantiation and should not be mangled like
7929 one. Therefore, we forget the mangling here; we'll recompute it
7930 later if we need it. */
7931 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
7933 SET_DECL_RTL (new_friend
, NULL
);
7934 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
7937 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
7940 tree new_friend_template_info
;
7941 tree new_friend_result_template_info
;
7943 int new_friend_is_defn
;
7945 /* We must save some information from NEW_FRIEND before calling
7946 duplicate decls since that function will free NEW_FRIEND if
7948 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
7949 new_friend_is_defn
=
7950 (DECL_INITIAL (DECL_TEMPLATE_RESULT
7951 (template_for_substitution (new_friend
)))
7953 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
7955 /* This declaration is a `primary' template. */
7956 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
7958 new_friend_result_template_info
7959 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
7962 new_friend_result_template_info
= NULL_TREE
;
7964 /* Make the init_value nonzero so pushdecl knows this is a defn. */
7965 if (new_friend_is_defn
)
7966 DECL_INITIAL (new_friend
) = error_mark_node
;
7968 /* Inside pushdecl_namespace_level, we will push into the
7969 current namespace. However, the friend function should go
7970 into the namespace of the template. */
7971 ns
= decl_namespace_context (new_friend
);
7972 push_nested_namespace (ns
);
7973 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
7974 pop_nested_namespace (ns
);
7976 if (old_decl
== error_mark_node
)
7977 return error_mark_node
;
7979 if (old_decl
!= new_friend
)
7981 /* This new friend declaration matched an existing
7982 declaration. For example, given:
7984 template <class T> void f(T);
7985 template <class U> class C {
7986 template <class T> friend void f(T) {}
7989 the friend declaration actually provides the definition
7990 of `f', once C has been instantiated for some type. So,
7991 old_decl will be the out-of-class template declaration,
7992 while new_friend is the in-class definition.
7994 But, if `f' was called before this point, the
7995 instantiation of `f' will have DECL_TI_ARGS corresponding
7996 to `T' but not to `U', references to which might appear
7997 in the definition of `f'. Previously, the most general
7998 template for an instantiation of `f' was the out-of-class
7999 version; now it is the in-class version. Therefore, we
8000 run through all specialization of `f', adding to their
8001 DECL_TI_ARGS appropriately. In particular, they need a
8002 new set of outer arguments, corresponding to the
8003 arguments for this class instantiation.
8005 The same situation can arise with something like this:
8008 template <class T> class C {
8012 when `C<int>' is instantiated. Now, `f(int)' is defined
8015 if (!new_friend_is_defn
)
8016 /* On the other hand, if the in-class declaration does
8017 *not* provide a definition, then we don't want to alter
8018 existing definitions. We can just leave everything
8023 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
8024 tree new_args
= TI_ARGS (new_friend_template_info
);
8026 /* Overwrite whatever template info was there before, if
8027 any, with the new template information pertaining to
8029 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
8031 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
8033 /* We should have called reregister_specialization in
8035 gcc_assert (retrieve_specialization (new_template
,
8039 /* Instantiate it if the global has already been used. */
8040 if (DECL_ODR_USED (old_decl
))
8041 instantiate_decl (old_decl
, /*defer_ok=*/true,
8042 /*expl_inst_class_mem_p=*/false);
8048 /* Indicate that the old function template is a partial
8050 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
8051 = new_friend_result_template_info
;
8053 gcc_assert (new_template
8054 == most_general_template (new_template
));
8055 gcc_assert (new_template
!= old_decl
);
8057 /* Reassign any specializations already in the hash table
8058 to the new more general template, and add the
8059 additional template args. */
8060 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
8064 tree spec
= TREE_VALUE (t
);
8067 elt
.tmpl
= old_decl
;
8068 elt
.args
= DECL_TI_ARGS (spec
);
8069 elt
.spec
= NULL_TREE
;
8071 htab_remove_elt (decl_specializations
, &elt
);
8074 = add_outermost_template_args (new_args
,
8075 DECL_TI_ARGS (spec
));
8077 register_specialization
8078 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
8081 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
8085 /* The information from NEW_FRIEND has been merged into OLD_DECL
8086 by duplicate_decls. */
8087 new_friend
= old_decl
;
8092 tree context
= DECL_CONTEXT (new_friend
);
8096 template <class T> class C {
8097 template <class U> friend void C1<U>::f (); // case 1
8098 friend void C2<T>::f (); // case 2
8100 we only need to make sure CONTEXT is a complete type for
8101 case 2. To distinguish between the two cases, we note that
8102 CONTEXT of case 1 remains dependent type after tsubst while
8103 this isn't true for case 2. */
8104 ++processing_template_decl
;
8105 dependent_p
= dependent_type_p (context
);
8106 --processing_template_decl
;
8109 && !complete_type_or_else (context
, NULL_TREE
))
8110 return error_mark_node
;
8112 if (COMPLETE_TYPE_P (context
))
8114 /* Check to see that the declaration is really present, and,
8115 possibly obtain an improved declaration. */
8116 tree fn
= check_classfn (context
,
8117 new_friend
, NULL_TREE
);
8127 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8128 template arguments, as for tsubst.
8130 Returns an appropriate tsubst'd friend type or error_mark_node on
8134 tsubst_friend_class (tree friend_tmpl
, tree args
)
8140 context
= CP_DECL_CONTEXT (friend_tmpl
);
8142 if (context
!= global_namespace
)
8144 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8145 push_nested_namespace (context
);
8147 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
8150 /* Look for a class template declaration. We look for hidden names
8151 because two friend declarations of the same template are the
8152 same. For example, in:
8155 template <typename> friend class F;
8157 template <typename> struct B {
8158 template <typename> friend class F;
8161 both F templates are the same. */
8162 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
8163 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
8165 /* But, if we don't find one, it might be because we're in a
8166 situation like this:
8174 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8175 for `S<int>', not the TEMPLATE_DECL. */
8176 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
8178 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
8179 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
8182 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
8184 /* The friend template has already been declared. Just
8185 check to see that the declarations match, and install any new
8186 default parameters. We must tsubst the default parameters,
8187 of course. We only need the innermost template parameters
8188 because that is all that redeclare_class_template will look
8190 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
8191 > TMPL_ARGS_DEPTH (args
))
8194 location_t saved_input_location
;
8195 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
8196 args
, tf_warning_or_error
);
8198 saved_input_location
= input_location
;
8199 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
8200 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
8201 input_location
= saved_input_location
;
8205 friend_type
= TREE_TYPE (tmpl
);
8209 /* The friend template has not already been declared. In this
8210 case, the instantiation of the template class will cause the
8211 injection of this template into the global scope. */
8212 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
8213 if (tmpl
== error_mark_node
)
8214 return error_mark_node
;
8216 /* The new TMPL is not an instantiation of anything, so we
8217 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8218 the new type because that is supposed to be the corresponding
8219 template decl, i.e., TMPL. */
8220 DECL_USE_TEMPLATE (tmpl
) = 0;
8221 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
8222 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
8223 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
8224 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
8226 /* Inject this template into the global scope. */
8227 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
8230 if (context
!= global_namespace
)
8232 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8233 pop_nested_namespace (context
);
8235 pop_nested_class ();
8241 /* Returns zero if TYPE cannot be completed later due to circularity.
8242 Otherwise returns one. */
8245 can_complete_type_without_circularity (tree type
)
8247 if (type
== NULL_TREE
|| type
== error_mark_node
)
8249 else if (COMPLETE_TYPE_P (type
))
8251 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
8252 return can_complete_type_without_circularity (TREE_TYPE (type
));
8253 else if (CLASS_TYPE_P (type
)
8254 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
8260 /* Apply any attributes which had to be deferred until instantiation
8261 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8262 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8265 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
8266 tree args
, tsubst_flags_t complain
, tree in_decl
)
8268 tree last_dep
= NULL_TREE
;
8272 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
8273 if (ATTR_IS_DEPENDENT (t
))
8276 attributes
= copy_list (attributes
);
8280 if (DECL_P (*decl_p
))
8282 if (TREE_TYPE (*decl_p
) == error_mark_node
)
8284 p
= &DECL_ATTRIBUTES (*decl_p
);
8287 p
= &TYPE_ATTRIBUTES (*decl_p
);
8291 tree late_attrs
= NULL_TREE
;
8292 tree
*q
= &late_attrs
;
8294 for (*p
= attributes
; *p
; )
8297 if (ATTR_IS_DEPENDENT (t
))
8299 *p
= TREE_CHAIN (t
);
8300 TREE_CHAIN (t
) = NULL_TREE
;
8301 /* If the first attribute argument is an identifier, don't
8302 pass it through tsubst. Attributes like mode, format,
8303 cleanup and several target specific attributes expect it
8306 && TREE_CODE (TREE_VALUE (t
)) == TREE_LIST
8307 && TREE_VALUE (TREE_VALUE (t
))
8308 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t
)))
8309 == IDENTIFIER_NODE
))
8312 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
8314 /*integral_constant_expression_p=*/false);
8315 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
8317 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
8322 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
8323 /*integral_constant_expression_p=*/false);
8325 q
= &TREE_CHAIN (t
);
8328 p
= &TREE_CHAIN (t
);
8331 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
8335 /* Perform (or defer) access check for typedefs that were referenced
8336 from within the template TMPL code.
8337 This is a subroutine of instantiate_decl and instantiate_class_template.
8338 TMPL is the template to consider and TARGS is the list of arguments of
8342 perform_typedefs_access_check (tree tmpl
, tree targs
)
8344 location_t saved_location
;
8346 qualified_typedef_usage_t
*iter
;
8349 || (!CLASS_TYPE_P (tmpl
)
8350 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
8353 saved_location
= input_location
;
8354 FOR_EACH_VEC_ELT (qualified_typedef_usage_t
,
8355 get_types_needing_access_check (tmpl
),
8358 tree type_decl
= iter
->typedef_decl
;
8359 tree type_scope
= iter
->context
;
8361 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
8364 if (uses_template_parms (type_decl
))
8365 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
8366 if (uses_template_parms (type_scope
))
8367 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
8369 /* Make access check error messages point to the location
8370 of the use of the typedef. */
8371 input_location
= iter
->locus
;
8372 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
8373 type_decl
, type_decl
);
8375 input_location
= saved_location
;
8379 instantiate_class_template_1 (tree type
)
8381 tree templ
, args
, pattern
, t
, member
;
8385 unsigned int saved_maximum_field_alignment
;
8388 if (type
== error_mark_node
)
8389 return error_mark_node
;
8391 if (COMPLETE_OR_OPEN_TYPE_P (type
)
8392 || uses_template_parms (type
))
8395 /* Figure out which template is being instantiated. */
8396 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
8397 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
8399 /* Determine what specialization of the original template to
8401 t
= most_specialized_class (type
, templ
, tf_warning_or_error
);
8402 if (t
== error_mark_node
)
8404 TYPE_BEING_DEFINED (type
) = 1;
8405 return error_mark_node
;
8409 /* This TYPE is actually an instantiation of a partial
8410 specialization. We replace the innermost set of ARGS with
8411 the arguments appropriate for substitution. For example,
8414 template <class T> struct S {};
8415 template <class T> struct S<T*> {};
8417 and supposing that we are instantiating S<int*>, ARGS will
8418 presently be {int*} -- but we need {int}. */
8419 pattern
= TREE_TYPE (t
);
8420 args
= TREE_PURPOSE (t
);
8424 pattern
= TREE_TYPE (templ
);
8425 args
= CLASSTYPE_TI_ARGS (type
);
8428 /* If the template we're instantiating is incomplete, then clearly
8429 there's nothing we can do. */
8430 if (!COMPLETE_TYPE_P (pattern
))
8433 /* If we've recursively instantiated too many templates, stop. */
8434 if (! push_tinst_level (type
))
8437 /* Now we're really doing the instantiation. Mark the type as in
8438 the process of being defined. */
8439 TYPE_BEING_DEFINED (type
) = 1;
8441 /* We may be in the middle of deferred access check. Disable
8443 push_deferring_access_checks (dk_no_deferred
);
8445 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
8447 push_to_top_level ();
8448 /* Use #pragma pack from the template context. */
8449 saved_maximum_field_alignment
= maximum_field_alignment
;
8450 maximum_field_alignment
= TYPE_PRECISION (pattern
);
8452 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
8454 /* Set the input location to the most specialized template definition.
8455 This is needed if tsubsting causes an error. */
8456 typedecl
= TYPE_MAIN_DECL (pattern
);
8457 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
8458 DECL_SOURCE_LOCATION (typedecl
);
8460 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
8461 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
8462 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
8463 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
8464 if (ANON_AGGR_TYPE_P (pattern
))
8465 SET_ANON_AGGR_TYPE_P (type
);
8466 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
8468 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
8469 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
8470 /* Adjust visibility for template arguments. */
8471 determine_visibility (TYPE_MAIN_DECL (type
));
8473 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
8475 pbinfo
= TYPE_BINFO (pattern
);
8477 /* We should never instantiate a nested class before its enclosing
8478 class; we need to look up the nested class by name before we can
8479 instantiate it, and that lookup should instantiate the enclosing
8481 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
8482 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
8484 base_list
= NULL_TREE
;
8485 if (BINFO_N_BASE_BINFOS (pbinfo
))
8491 /* We must enter the scope containing the type, as that is where
8492 the accessibility of types named in dependent bases are
8494 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
8496 /* Substitute into each of the bases to determine the actual
8498 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
8501 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
8502 tree expanded_bases
= NULL_TREE
;
8505 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
8508 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
8509 args
, tf_error
, NULL_TREE
);
8510 if (expanded_bases
== error_mark_node
)
8513 len
= TREE_VEC_LENGTH (expanded_bases
);
8516 for (idx
= 0; idx
< len
; idx
++)
8519 /* Extract the already-expanded base class. */
8520 base
= TREE_VEC_ELT (expanded_bases
, idx
);
8522 /* Substitute to figure out the base class. */
8523 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
8526 if (base
== error_mark_node
)
8529 base_list
= tree_cons (access
, base
, base_list
);
8530 if (BINFO_VIRTUAL_P (pbase_binfo
))
8531 TREE_TYPE (base_list
) = integer_type_node
;
8535 /* The list is now in reverse order; correct that. */
8536 base_list
= nreverse (base_list
);
8539 pop_scope (pushed_scope
);
8541 /* Now call xref_basetypes to set up all the base-class
8543 xref_basetypes (type
, base_list
);
8545 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
8546 (int) ATTR_FLAG_TYPE_IN_PLACE
,
8547 args
, tf_error
, NULL_TREE
);
8548 fixup_attribute_variants (type
);
8550 /* Now that our base classes are set up, enter the scope of the
8551 class, so that name lookups into base classes, etc. will work
8552 correctly. This is precisely analogous to what we do in
8553 begin_class_definition when defining an ordinary non-template
8554 class, except we also need to push the enclosing classes. */
8555 push_nested_class (type
);
8557 /* Now members are processed in the order of declaration. */
8558 for (member
= CLASSTYPE_DECL_LIST (pattern
);
8559 member
; member
= TREE_CHAIN (member
))
8561 tree t
= TREE_VALUE (member
);
8563 if (TREE_PURPOSE (member
))
8567 /* Build new CLASSTYPE_NESTED_UTDS. */
8570 bool class_template_p
;
8572 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
8573 && TYPE_LANG_SPECIFIC (t
)
8574 && CLASSTYPE_IS_TEMPLATE (t
));
8575 /* If the member is a class template, then -- even after
8576 substitution -- there may be dependent types in the
8577 template argument list for the class. We increment
8578 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8579 that function will assume that no types are dependent
8580 when outside of a template. */
8581 if (class_template_p
)
8582 ++processing_template_decl
;
8583 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8584 if (class_template_p
)
8585 --processing_template_decl
;
8586 if (newtag
== error_mark_node
)
8589 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
8591 tree name
= TYPE_IDENTIFIER (t
);
8593 if (class_template_p
)
8594 /* Unfortunately, lookup_template_class sets
8595 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8596 instantiation (i.e., for the type of a member
8597 template class nested within a template class.)
8598 This behavior is required for
8599 maybe_process_partial_specialization to work
8600 correctly, but is not accurate in this case;
8601 the TAG is not an instantiation of anything.
8602 (The corresponding TEMPLATE_DECL is an
8603 instantiation, but the TYPE is not.) */
8604 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
8606 /* Now, we call pushtag to put this NEWTAG into the scope of
8607 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8608 pushtag calling push_template_decl. We don't have to do
8609 this for enums because it will already have been done in
8612 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
8613 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
8616 else if (TREE_CODE (t
) == FUNCTION_DECL
8617 || DECL_FUNCTION_TEMPLATE_P (t
))
8619 /* Build new TYPE_METHODS. */
8622 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8623 ++processing_template_decl
;
8624 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8625 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8626 --processing_template_decl
;
8627 set_current_access_from_decl (r
);
8628 finish_member_declaration (r
);
8629 /* Instantiate members marked with attribute used. */
8630 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8635 /* Build new TYPE_FIELDS. */
8636 if (TREE_CODE (t
) == STATIC_ASSERT
)
8640 ++c_inhibit_evaluation_warnings
;
8642 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
8643 tf_warning_or_error
, NULL_TREE
,
8644 /*integral_constant_expression_p=*/true);
8645 --c_inhibit_evaluation_warnings
;
8647 finish_static_assert (condition
,
8648 STATIC_ASSERT_MESSAGE (t
),
8649 STATIC_ASSERT_SOURCE_LOCATION (t
),
8652 else if (TREE_CODE (t
) != CONST_DECL
)
8656 /* The file and line for this declaration, to
8657 assist in error message reporting. Since we
8658 called push_tinst_level above, we don't need to
8660 input_location
= DECL_SOURCE_LOCATION (t
);
8662 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8663 ++processing_template_decl
;
8664 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
8665 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8666 --processing_template_decl
;
8667 if (TREE_CODE (r
) == VAR_DECL
)
8671 [t]he initialization (and any associated
8672 side-effects) of a static data member does
8673 not occur unless the static data member is
8674 itself used in a way that requires the
8675 definition of the static data member to
8678 Therefore, we do not substitute into the
8679 initialized for the static data member here. */
8680 finish_static_data_member_decl
8683 /*init_const_expr_p=*/false,
8684 /*asmspec_tree=*/NULL_TREE
,
8686 /* Instantiate members marked with attribute used. */
8687 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8690 else if (TREE_CODE (r
) == FIELD_DECL
)
8692 /* Determine whether R has a valid type and can be
8693 completed later. If R is invalid, then it is
8694 replaced by error_mark_node so that it will not be
8695 added to TYPE_FIELDS. */
8696 tree rtype
= TREE_TYPE (r
);
8697 if (can_complete_type_without_circularity (rtype
))
8698 complete_type (rtype
);
8700 if (!COMPLETE_TYPE_P (rtype
))
8702 cxx_incomplete_type_error (r
, rtype
);
8703 r
= error_mark_node
;
8707 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8708 such a thing will already have been added to the field
8709 list by tsubst_enum in finish_member_declaration in the
8710 CLASSTYPE_NESTED_UTDS case above. */
8711 if (!(TREE_CODE (r
) == TYPE_DECL
8712 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
8713 && DECL_ARTIFICIAL (r
)))
8715 set_current_access_from_decl (r
);
8716 finish_member_declaration (r
);
8723 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
))
8725 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8727 tree friend_type
= t
;
8728 bool adjust_processing_template_decl
= false;
8730 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
8732 /* template <class T> friend class C; */
8733 friend_type
= tsubst_friend_class (friend_type
, args
);
8734 adjust_processing_template_decl
= true;
8736 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
8738 /* template <class T> friend class C::D; */
8739 friend_type
= tsubst (friend_type
, args
,
8740 tf_warning_or_error
, NULL_TREE
);
8741 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
8742 friend_type
= TREE_TYPE (friend_type
);
8743 adjust_processing_template_decl
= true;
8745 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
8746 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
8748 /* This could be either
8752 when dependent_type_p is false or
8754 template <class U> friend class T::C;
8757 friend_type
= tsubst (friend_type
, args
,
8758 tf_warning_or_error
, NULL_TREE
);
8759 /* Bump processing_template_decl for correct
8760 dependent_type_p calculation. */
8761 ++processing_template_decl
;
8762 if (dependent_type_p (friend_type
))
8763 adjust_processing_template_decl
= true;
8764 --processing_template_decl
;
8766 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
8767 && hidden_name_p (TYPE_NAME (friend_type
)))
8771 where C hasn't been declared yet. Let's lookup name
8772 from namespace scope directly, bypassing any name that
8773 come from dependent base class. */
8774 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
8776 /* The call to xref_tag_from_type does injection for friend
8778 push_nested_namespace (ns
);
8780 xref_tag_from_type (friend_type
, NULL_TREE
,
8781 /*tag_scope=*/ts_current
);
8782 pop_nested_namespace (ns
);
8784 else if (uses_template_parms (friend_type
))
8785 /* friend class C<T>; */
8786 friend_type
= tsubst (friend_type
, args
,
8787 tf_warning_or_error
, NULL_TREE
);
8792 where C is already declared or
8794 friend class C<int>;
8796 We don't have to do anything in these cases. */
8798 if (adjust_processing_template_decl
)
8799 /* Trick make_friend_class into realizing that the friend
8800 we're adding is a template, not an ordinary class. It's
8801 important that we use make_friend_class since it will
8802 perform some error-checking and output cross-reference
8804 ++processing_template_decl
;
8806 if (friend_type
!= error_mark_node
)
8807 make_friend_class (type
, friend_type
, /*complain=*/false);
8809 if (adjust_processing_template_decl
)
8810 --processing_template_decl
;
8814 /* Build new DECL_FRIENDLIST. */
8817 /* The file and line for this declaration, to
8818 assist in error message reporting. Since we
8819 called push_tinst_level above, we don't need to
8821 input_location
= DECL_SOURCE_LOCATION (t
);
8823 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8825 ++processing_template_decl
;
8826 push_deferring_access_checks (dk_no_check
);
8829 r
= tsubst_friend_function (t
, args
);
8830 add_friend (type
, r
, /*complain=*/false);
8831 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8833 pop_deferring_access_checks ();
8834 --processing_template_decl
;
8840 if (CLASSTYPE_LAMBDA_EXPR (type
))
8842 tree decl
= lambda_function (type
);
8845 tree lam
= CLASSTYPE_LAMBDA_EXPR (type
);
8846 LAMBDA_EXPR_THIS_CAPTURE (lam
)
8847 = lookup_field_1 (type
, get_identifier ("__this"), false);
8849 instantiate_decl (decl
, false, false);
8850 maybe_add_lambda_conv_op (type
);
8852 LAMBDA_EXPR_THIS_CAPTURE (lam
) = NULL_TREE
;
8855 gcc_assert (errorcount
);
8858 /* Set the file and line number information to whatever is given for
8859 the class itself. This puts error messages involving generated
8860 implicit functions at a predictable point, and the same point
8861 that would be used for non-template classes. */
8862 input_location
= DECL_SOURCE_LOCATION (typedecl
);
8864 unreverse_member_declarations (type
);
8865 finish_struct_1 (type
);
8866 TYPE_BEING_DEFINED (type
) = 0;
8868 /* We don't instantiate default arguments for member functions. 14.7.1:
8870 The implicit instantiation of a class template specialization causes
8871 the implicit instantiation of the declarations, but not of the
8872 definitions or default arguments, of the class member functions,
8873 member classes, static data members and member templates.... */
8875 /* Some typedefs referenced from within the template code need to be access
8876 checked at template instantiation time, i.e now. These types were
8877 added to the template at parsing time. Let's get those and perform
8878 the access checks then. */
8879 perform_typedefs_access_check (pattern
, args
);
8880 perform_deferred_access_checks ();
8881 pop_nested_class ();
8882 maximum_field_alignment
= saved_maximum_field_alignment
;
8884 pop_from_top_level ();
8885 pop_deferring_access_checks ();
8888 /* The vtable for a template class can be emitted in any translation
8889 unit in which the class is instantiated. When there is no key
8890 method, however, finish_struct_1 will already have added TYPE to
8891 the keyed_classes list. */
8892 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
8893 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
8898 /* Wrapper for instantiate_class_template_1. */
8901 instantiate_class_template (tree type
)
8904 timevar_push (TV_TEMPLATE_INST
);
8905 ret
= instantiate_class_template_1 (type
);
8906 timevar_pop (TV_TEMPLATE_INST
);
8911 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
8917 else if (TYPE_P (t
))
8918 r
= tsubst (t
, args
, complain
, in_decl
);
8921 if (!(complain
& tf_warning
))
8922 ++c_inhibit_evaluation_warnings
;
8923 r
= tsubst_expr (t
, args
, complain
, in_decl
,
8924 /*integral_constant_expression_p=*/true);
8925 if (!(complain
& tf_warning
))
8926 --c_inhibit_evaluation_warnings
;
8927 /* Preserve the raw-reference nature of T. */
8928 if (TREE_TYPE (t
) && TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
8929 && REFERENCE_REF_P (r
))
8930 r
= TREE_OPERAND (r
, 0);
8935 /* Given a function parameter pack TMPL_PARM and some function parameters
8936 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
8937 and set *SPEC_P to point at the next point in the list. */
8940 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
8942 /* Collect all of the extra "packed" parameters into an
8946 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
8947 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8948 tree spec_parm
= *spec_p
;
8951 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
8953 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
8956 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
8957 parmvec
= make_tree_vec (len
);
8958 parmtypevec
= make_tree_vec (len
);
8959 spec_parm
= *spec_p
;
8960 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
8962 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
8963 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
8966 /* Build the argument packs. */
8967 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
8968 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
8969 TREE_TYPE (argpack
) = argtypepack
;
8970 *spec_p
= spec_parm
;
8975 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
8976 NONTYPE_ARGUMENT_PACK. */
8979 make_fnparm_pack (tree spec_parm
)
8981 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
8984 /* Substitute ARGS into T, which is an pack expansion
8985 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
8986 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
8987 (if only a partial substitution could be performed) or
8988 ERROR_MARK_NODE if there was an error. */
8990 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
8994 tree pack
, packs
= NULL_TREE
;
8995 bool unsubstituted_packs
= false;
8996 bool real_packs
= false;
8997 int missing_level
= 0;
9000 struct pointer_map_t
*saved_local_specializations
= NULL
;
9001 bool need_local_specializations
= false;
9004 gcc_assert (PACK_EXPANSION_P (t
));
9005 pattern
= PACK_EXPANSION_PATTERN (t
);
9007 /* Add in any args remembered from an earlier partial instantiation. */
9008 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
9010 levels
= TMPL_ARGS_DEPTH (args
);
9012 /* Determine the argument packs that will instantiate the parameter
9013 packs used in the expansion expression. While we're at it,
9014 compute the number of arguments to be expanded and make sure it
9016 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
9017 pack
= TREE_CHAIN (pack
))
9019 tree parm_pack
= TREE_VALUE (pack
);
9020 tree arg_pack
= NULL_TREE
;
9021 tree orig_arg
= NULL_TREE
;
9024 if (TREE_CODE (parm_pack
) == BASES
)
9026 if (BASES_DIRECT (parm_pack
))
9027 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9028 args
, complain
, in_decl
, false));
9030 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9031 args
, complain
, in_decl
, false));
9033 if (TREE_CODE (parm_pack
) == PARM_DECL
)
9035 if (PACK_EXPANSION_LOCAL_P (t
))
9036 arg_pack
= retrieve_local_specialization (parm_pack
);
9039 /* We can't rely on local_specializations for a parameter
9040 name used later in a function declaration (such as in a
9041 late-specified return type). Even if it exists, it might
9042 have the wrong value for a recursive call. Just make a
9043 dummy decl, since it's only used for its type. */
9044 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
9045 if (arg_pack
&& FUNCTION_PARAMETER_PACK_P (arg_pack
))
9046 /* Partial instantiation of the parm_pack, we can't build
9047 up an argument pack yet. */
9048 arg_pack
= NULL_TREE
;
9050 arg_pack
= make_fnparm_pack (arg_pack
);
9051 need_local_specializations
= true;
9057 template_parm_level_and_index (parm_pack
, &level
, &idx
);
9059 if (level
<= levels
)
9060 arg_pack
= TMPL_ARG (args
, level
, idx
);
9063 orig_arg
= arg_pack
;
9064 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
9065 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
9067 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
9068 /* This can only happen if we forget to expand an argument
9069 pack somewhere else. Just return an error, silently. */
9071 result
= make_tree_vec (1);
9072 TREE_VEC_ELT (result
, 0) = error_mark_node
;
9076 if (arg_from_parm_pack_p (arg_pack
, parm_pack
))
9077 /* The argument pack that the parameter maps to is just an
9078 expansion of the parameter itself, such as one would find
9079 in the implicit typedef of a class inside the class itself.
9080 Consider this parameter "unsubstituted", so that we will
9081 maintain the outer pack expansion. */
9082 arg_pack
= NULL_TREE
;
9087 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
9089 /* Don't bother trying to do a partial substitution with
9090 incomplete packs; we'll try again after deduction. */
9091 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
9096 else if (len
!= my_len
)
9098 if (!(complain
& tf_error
))
9099 /* Fail quietly. */;
9100 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
9101 error ("mismatched argument pack lengths while expanding "
9105 error ("mismatched argument pack lengths while expanding "
9108 return error_mark_node
;
9111 if (TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
)) == 1
9112 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
),
9114 /* This isn't a real argument pack yet. */;
9118 /* Keep track of the parameter packs and their corresponding
9120 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
9121 TREE_TYPE (packs
) = orig_arg
;
9125 /* We can't substitute for this parameter pack. We use a flag as
9126 well as the missing_level counter because function parameter
9127 packs don't have a level. */
9128 unsubstituted_packs
= true;
9129 if (!missing_level
|| missing_level
> level
)
9130 missing_level
= level
;
9134 /* We cannot expand this expansion expression, because we don't have
9135 all of the argument packs we need. */
9136 if (unsubstituted_packs
)
9140 /* We got some full packs, but we can't substitute them in until we
9141 have values for all the packs. So remember these until then. */
9144 t
= make_pack_expansion (pattern
);
9146 /* The call to add_to_template_args above assumes no overlap
9147 between saved args and new args, so prune away any fake
9148 args, i.e. those that satisfied arg_from_parm_pack_p above. */
9149 if (missing_level
&& levels
>= missing_level
)
9151 gcc_assert (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
)
9152 && missing_level
> 1);
9153 TREE_VEC_LENGTH (args
) = missing_level
- 1;
9154 save_args
= copy_node (args
);
9155 TREE_VEC_LENGTH (args
) = levels
;
9160 PACK_EXPANSION_EXTRA_ARGS (t
) = save_args
;
9164 /* There were no real arguments, we're just replacing a parameter
9165 pack with another version of itself. Substitute into the
9166 pattern and return a PACK_EXPANSION_*. The caller will need to
9168 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
9169 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9170 /*integral_constant_expression_p=*/false);
9172 t
= tsubst (pattern
, args
, complain
, in_decl
);
9173 t
= make_pack_expansion (t
);
9178 /* We could not find any argument packs that work. */
9180 return error_mark_node
;
9182 if (need_local_specializations
)
9184 /* We're in a late-specified return type, so create our own local
9185 specializations map; the current map is either NULL or (in the
9186 case of recursive unification) might have bindings that we don't
9187 want to use or alter. */
9188 saved_local_specializations
= local_specializations
;
9189 local_specializations
= pointer_map_create ();
9192 /* For each argument in each argument pack, substitute into the
9194 result
= make_tree_vec (len
);
9195 for (i
= 0; i
< len
; ++i
)
9197 /* For parameter pack, change the substitution of the parameter
9198 pack to the ith argument in its argument pack, then expand
9200 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9202 tree parm
= TREE_PURPOSE (pack
);
9205 /* Select the Ith argument from the pack. */
9206 if (TREE_CODE (parm
) == PARM_DECL
)
9210 arg
= make_node (ARGUMENT_PACK_SELECT
);
9211 ARGUMENT_PACK_SELECT_FROM_PACK (arg
) = TREE_VALUE (pack
);
9213 register_local_specialization (arg
, parm
);
9216 arg
= retrieve_local_specialization (parm
);
9221 template_parm_level_and_index (parm
, &level
, &idx
);
9225 arg
= make_node (ARGUMENT_PACK_SELECT
);
9226 ARGUMENT_PACK_SELECT_FROM_PACK (arg
) = TREE_VALUE (pack
);
9227 /* Update the corresponding argument. */
9228 TMPL_ARG (args
, level
, idx
) = arg
;
9231 /* Re-use the ARGUMENT_PACK_SELECT. */
9232 arg
= TMPL_ARG (args
, level
, idx
);
9234 ARGUMENT_PACK_SELECT_INDEX (arg
) = i
;
9237 /* Substitute into the PATTERN with the altered arguments. */
9238 if (!TYPE_P (pattern
))
9239 TREE_VEC_ELT (result
, i
) =
9240 tsubst_expr (pattern
, args
, complain
, in_decl
,
9241 /*integral_constant_expression_p=*/false);
9243 TREE_VEC_ELT (result
, i
) = tsubst (pattern
, args
, complain
, in_decl
);
9245 if (TREE_VEC_ELT (result
, i
) == error_mark_node
)
9247 result
= error_mark_node
;
9252 /* Update ARGS to restore the substitution from parameter packs to
9253 their argument packs. */
9254 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9256 tree parm
= TREE_PURPOSE (pack
);
9258 if (TREE_CODE (parm
) == PARM_DECL
)
9259 register_local_specialization (TREE_TYPE (pack
), parm
);
9263 template_parm_level_and_index (parm
, &level
, &idx
);
9265 /* Update the corresponding argument. */
9266 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
9267 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
9270 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
9274 if (need_local_specializations
)
9276 pointer_map_destroy (local_specializations
);
9277 local_specializations
= saved_local_specializations
;
9283 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9284 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9285 parameter packs; all parms generated from a function parameter pack will
9286 have the same DECL_PARM_INDEX. */
9289 get_pattern_parm (tree parm
, tree tmpl
)
9291 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9294 if (DECL_ARTIFICIAL (parm
))
9296 for (patparm
= DECL_ARGUMENTS (pattern
);
9297 patparm
; patparm
= DECL_CHAIN (patparm
))
9298 if (DECL_ARTIFICIAL (patparm
)
9299 && DECL_NAME (parm
) == DECL_NAME (patparm
))
9304 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
9305 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
9306 gcc_assert (DECL_PARM_INDEX (patparm
)
9307 == DECL_PARM_INDEX (parm
));
9313 /* Substitute ARGS into the vector or list of template arguments T. */
9316 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9319 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
9322 if (t
== error_mark_node
)
9323 return error_mark_node
;
9325 len
= TREE_VEC_LENGTH (t
);
9326 elts
= XALLOCAVEC (tree
, len
);
9328 for (i
= 0; i
< len
; i
++)
9330 tree orig_arg
= TREE_VEC_ELT (t
, i
);
9333 if (TREE_CODE (orig_arg
) == TREE_VEC
)
9334 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
9335 else if (PACK_EXPANSION_P (orig_arg
))
9337 /* Substitute into an expansion expression. */
9338 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
9340 if (TREE_CODE (new_arg
) == TREE_VEC
)
9341 /* Add to the expanded length adjustment the number of
9342 expanded arguments. We subtract one from this
9343 measurement, because the argument pack expression
9344 itself is already counted as 1 in
9345 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9346 the argument pack is empty. */
9347 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
9349 else if (ARGUMENT_PACK_P (orig_arg
))
9351 /* Substitute into each of the arguments. */
9352 new_arg
= TYPE_P (orig_arg
)
9353 ? cxx_make_type (TREE_CODE (orig_arg
))
9354 : make_node (TREE_CODE (orig_arg
));
9356 SET_ARGUMENT_PACK_ARGS (
9358 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
9359 args
, complain
, in_decl
));
9361 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
9362 new_arg
= error_mark_node
;
9364 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
9365 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
9367 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
9369 if (TREE_TYPE (new_arg
) == error_mark_node
)
9370 new_arg
= error_mark_node
;
9374 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
9376 if (new_arg
== error_mark_node
)
9377 return error_mark_node
;
9380 if (new_arg
!= orig_arg
)
9387 /* Make space for the expanded arguments coming from template
9389 t
= make_tree_vec (len
+ expanded_len_adjust
);
9390 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9391 arguments for a member template.
9392 In that case each TREE_VEC in ORIG_T represents a level of template
9393 arguments, and ORIG_T won't carry any non defaulted argument count.
9394 It will rather be the nested TREE_VECs that will carry one.
9395 In other words, ORIG_T carries a non defaulted argument count only
9396 if it doesn't contain any nested TREE_VEC. */
9397 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
9399 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
9400 count
+= expanded_len_adjust
;
9401 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
9403 for (i
= 0, out
= 0; i
< len
; i
++)
9405 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
9406 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
9407 && TREE_CODE (elts
[i
]) == TREE_VEC
)
9411 /* Now expand the template argument pack "in place". */
9412 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
9413 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
9417 TREE_VEC_ELT (t
, out
) = elts
[i
];
9425 /* Return the result of substituting ARGS into the template parameters
9426 given by PARMS. If there are m levels of ARGS and m + n levels of
9427 PARMS, then the result will contain n levels of PARMS. For
9428 example, if PARMS is `template <class T> template <class U>
9429 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9430 result will be `template <int*, double, class V>'. */
9433 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
9438 /* When substituting into a template, we must set
9439 PROCESSING_TEMPLATE_DECL as the template parameters may be
9440 dependent if they are based on one-another, and the dependency
9441 predicates are short-circuit outside of templates. */
9442 ++processing_template_decl
;
9444 for (new_parms
= &r
;
9445 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
9446 new_parms
= &(TREE_CHAIN (*new_parms
)),
9447 parms
= TREE_CHAIN (parms
))
9450 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
9453 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
9457 if (parms
== error_mark_node
)
9460 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
9462 if (tuple
== error_mark_node
)
9465 TREE_VEC_ELT (new_vec
, i
) =
9466 tsubst_template_parm (tuple
, args
, complain
);
9470 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
9471 - TMPL_ARGS_DEPTH (args
)),
9472 new_vec
, NULL_TREE
);
9475 --processing_template_decl
;
9480 /* Return the result of substituting ARGS into one template parameter
9481 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9482 parameter and which TREE_PURPOSE is the default argument of the
9483 template parameter. */
9486 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
9488 tree default_value
, parm_decl
;
9490 if (args
== NULL_TREE
9492 || t
== error_mark_node
)
9495 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
9497 default_value
= TREE_PURPOSE (t
);
9498 parm_decl
= TREE_VALUE (t
);
9500 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
9501 if (TREE_CODE (parm_decl
) == PARM_DECL
9502 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
9503 parm_decl
= error_mark_node
;
9504 default_value
= tsubst_template_arg (default_value
, args
,
9505 complain
, NULL_TREE
);
9507 return build_tree_list (default_value
, parm_decl
);
9510 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9511 type T. If T is not an aggregate or enumeration type, it is
9512 handled as if by tsubst. IN_DECL is as for tsubst. If
9513 ENTERING_SCOPE is nonzero, T is the context for a template which
9514 we are presently tsubst'ing. Return the substituted value. */
9517 tsubst_aggr_type (tree t
,
9519 tsubst_flags_t complain
,
9526 switch (TREE_CODE (t
))
9529 if (TYPE_PTRMEMFUNC_P (t
))
9530 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
9532 /* Else fall through. */
9535 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
9540 int saved_unevaluated_operand
;
9541 int saved_inhibit_evaluation_warnings
;
9543 /* In "sizeof(X<I>)" we need to evaluate "I". */
9544 saved_unevaluated_operand
= cp_unevaluated_operand
;
9545 cp_unevaluated_operand
= 0;
9546 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9547 c_inhibit_evaluation_warnings
= 0;
9549 /* First, determine the context for the type we are looking
9551 context
= TYPE_CONTEXT (t
);
9552 if (context
&& TYPE_P (context
))
9554 context
= tsubst_aggr_type (context
, args
, complain
,
9555 in_decl
, /*entering_scope=*/1);
9556 /* If context is a nested class inside a class template,
9557 it may still need to be instantiated (c++/33959). */
9558 context
= complete_type (context
);
9561 /* Then, figure out what arguments are appropriate for the
9562 type we are trying to find. For example, given:
9564 template <class T> struct S;
9565 template <class T, class U> void f(T, U) { S<U> su; }
9567 and supposing that we are instantiating f<int, double>,
9568 then our ARGS will be {int, double}, but, when looking up
9569 S we only want {double}. */
9570 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
9572 if (argvec
== error_mark_node
)
9573 r
= error_mark_node
;
9576 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
9577 entering_scope
, complain
);
9578 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
9581 cp_unevaluated_operand
= saved_unevaluated_operand
;
9582 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
9587 /* This is not a template type, so there's nothing to do. */
9591 return tsubst (t
, args
, complain
, in_decl
);
9595 /* Substitute into the default argument ARG (a default argument for
9596 FN), which has the indicated TYPE. */
9599 tsubst_default_argument (tree fn
, tree type
, tree arg
)
9601 tree saved_class_ptr
= NULL_TREE
;
9602 tree saved_class_ref
= NULL_TREE
;
9604 /* This can happen in invalid code. */
9605 if (TREE_CODE (arg
) == DEFAULT_ARG
)
9608 /* This default argument came from a template. Instantiate the
9609 default argument here, not in tsubst. In the case of
9618 we must be careful to do name lookup in the scope of S<T>,
9619 rather than in the current class. */
9620 push_access_scope (fn
);
9621 /* The "this" pointer is not valid in a default argument. */
9624 saved_class_ptr
= current_class_ptr
;
9625 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
9626 saved_class_ref
= current_class_ref
;
9627 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
9630 push_deferring_access_checks(dk_no_deferred
);
9631 /* The default argument expression may cause implicitly defined
9632 member functions to be synthesized, which will result in garbage
9633 collection. We must treat this situation as if we were within
9634 the body of function so as to avoid collecting live data on the
9637 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
9638 tf_warning_or_error
, NULL_TREE
,
9639 /*integral_constant_expression_p=*/false);
9641 pop_deferring_access_checks();
9643 /* Restore the "this" pointer. */
9646 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
9647 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
9650 /* Make sure the default argument is reasonable. */
9651 arg
= check_default_argument (type
, arg
);
9653 pop_access_scope (fn
);
9658 /* Substitute into all the default arguments for FN. */
9661 tsubst_default_arguments (tree fn
)
9666 tmpl_args
= DECL_TI_ARGS (fn
);
9668 /* If this function is not yet instantiated, we certainly don't need
9669 its default arguments. */
9670 if (uses_template_parms (tmpl_args
))
9672 /* Don't do this again for clones. */
9673 if (DECL_CLONED_FUNCTION_P (fn
))
9676 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
9678 arg
= TREE_CHAIN (arg
))
9679 if (TREE_PURPOSE (arg
))
9680 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
9682 TREE_PURPOSE (arg
));
9685 /* Substitute the ARGS into the T, which is a _DECL. Return the
9686 result of the substitution. Issue error and warning messages under
9687 control of COMPLAIN. */
9690 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
9692 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
9693 location_t saved_loc
;
9698 /* Set the filename and linenumber to improve error-reporting. */
9699 saved_loc
= input_location
;
9700 input_location
= DECL_SOURCE_LOCATION (t
);
9702 switch (TREE_CODE (t
))
9706 /* We can get here when processing a member function template,
9707 member class template, or template template parameter. */
9708 tree decl
= DECL_TEMPLATE_RESULT (t
);
9713 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9715 /* Template template parameter is treated here. */
9716 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9717 if (new_type
== error_mark_node
)
9718 RETURN (error_mark_node
);
9721 DECL_CHAIN (r
) = NULL_TREE
;
9722 TREE_TYPE (r
) = new_type
;
9723 DECL_TEMPLATE_RESULT (r
)
9724 = build_decl (DECL_SOURCE_LOCATION (decl
),
9725 TYPE_DECL
, DECL_NAME (decl
), new_type
);
9726 DECL_TEMPLATE_PARMS (r
)
9727 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
9729 TYPE_NAME (new_type
) = r
;
9733 /* We might already have an instance of this template.
9734 The ARGS are for the surrounding class type, so the
9735 full args contain the tsubst'd args for the context,
9736 plus the innermost args from the template decl. */
9737 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
9738 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
9739 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
9740 /* Because this is a template, the arguments will still be
9741 dependent, even after substitution. If
9742 PROCESSING_TEMPLATE_DECL is not set, the dependency
9743 predicates will short-circuit. */
9744 ++processing_template_decl
;
9745 full_args
= tsubst_template_args (tmpl_args
, args
,
9747 --processing_template_decl
;
9748 if (full_args
== error_mark_node
)
9749 RETURN (error_mark_node
);
9751 /* If this is a default template template argument,
9752 tsubst might not have changed anything. */
9753 if (full_args
== tmpl_args
)
9756 hash
= hash_tmpl_and_args (t
, full_args
);
9757 spec
= retrieve_specialization (t
, full_args
, hash
);
9758 if (spec
!= NULL_TREE
)
9764 /* Make a new template decl. It will be similar to the
9765 original, but will record the current template arguments.
9766 We also create a new function declaration, which is just
9767 like the old one, but points to this new template, rather
9768 than the old one. */
9770 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
9771 DECL_CHAIN (r
) = NULL_TREE
;
9773 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
9775 if (TREE_CODE (decl
) == TYPE_DECL
9776 && !TYPE_DECL_ALIAS_P (decl
))
9779 ++processing_template_decl
;
9780 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9781 --processing_template_decl
;
9782 if (new_type
== error_mark_node
)
9783 RETURN (error_mark_node
);
9785 TREE_TYPE (r
) = new_type
;
9786 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
9787 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
9788 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
9789 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
9794 ++processing_template_decl
;
9795 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
9796 --processing_template_decl
;
9797 if (new_decl
== error_mark_node
)
9798 RETURN (error_mark_node
);
9800 DECL_TEMPLATE_RESULT (r
) = new_decl
;
9801 DECL_TI_TEMPLATE (new_decl
) = r
;
9802 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
9803 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
9804 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
9807 SET_DECL_IMPLICIT_INSTANTIATION (r
);
9808 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
9809 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
9811 /* The template parameters for this new template are all the
9812 template parameters for the old template, except the
9813 outermost level of parameters. */
9814 DECL_TEMPLATE_PARMS (r
)
9815 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
9818 if (PRIMARY_TEMPLATE_P (t
))
9819 DECL_PRIMARY_TEMPLATE (r
) = r
;
9821 if (TREE_CODE (decl
) != TYPE_DECL
)
9822 /* Record this non-type partial instantiation. */
9823 register_specialization (r
, t
,
9824 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
9832 tree argvec
= NULL_TREE
;
9840 /* Nobody should be tsubst'ing into non-template functions. */
9841 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
9843 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
9848 /* If T is not dependent, just return it. We have to
9849 increment PROCESSING_TEMPLATE_DECL because
9850 value_dependent_expression_p assumes that nothing is
9851 dependent when PROCESSING_TEMPLATE_DECL is zero. */
9852 ++processing_template_decl
;
9853 dependent_p
= value_dependent_expression_p (t
);
9854 --processing_template_decl
;
9858 /* Calculate the most general template of which R is a
9859 specialization, and the complete set of arguments used to
9861 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
9862 argvec
= tsubst_template_args (DECL_TI_ARGS
9863 (DECL_TEMPLATE_RESULT
9864 (DECL_TI_TEMPLATE (t
))),
9865 args
, complain
, in_decl
);
9866 if (argvec
== error_mark_node
)
9867 RETURN (error_mark_node
);
9869 /* Check to see if we already have this specialization. */
9870 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
9871 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
9879 /* We can see more levels of arguments than parameters if
9880 there was a specialization of a member template, like
9883 template <class T> struct S { template <class U> void f(); }
9884 template <> template <class U> void S<int>::f(U);
9886 Here, we'll be substituting into the specialization,
9887 because that's where we can find the code we actually
9888 want to generate, but we'll have enough arguments for
9889 the most general template.
9891 We also deal with the peculiar case:
9893 template <class T> struct S {
9894 template <class U> friend void f();
9896 template <class U> void f() {}
9898 template void f<double>();
9900 Here, the ARGS for the instantiation of will be {int,
9901 double}. But, we only need as many ARGS as there are
9902 levels of template parameters in CODE_PATTERN. We are
9903 careful not to get fooled into reducing the ARGS in
9906 template <class T> struct S { template <class U> void f(U); }
9907 template <class T> template <> void S<T>::f(int) {}
9909 which we can spot because the pattern will be a
9910 specialization in this case. */
9911 args_depth
= TMPL_ARGS_DEPTH (args
);
9913 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
9914 if (args_depth
> parms_depth
9915 && !DECL_TEMPLATE_SPECIALIZATION (t
))
9916 args
= get_innermost_template_args (args
, parms_depth
);
9920 /* This special case arises when we have something like this:
9922 template <class T> struct S {
9923 friend void f<int>(int, double);
9926 Here, the DECL_TI_TEMPLATE for the friend declaration
9927 will be an IDENTIFIER_NODE. We are being called from
9928 tsubst_friend_function, and we want only to create a
9929 new decl (R) with appropriate types so that we can call
9930 determine_specialization. */
9931 gen_tmpl
= NULL_TREE
;
9934 if (DECL_CLASS_SCOPE_P (t
))
9936 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
9940 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
9941 complain
, t
, /*entering_scope=*/1);
9946 ctx
= DECL_CONTEXT (t
);
9948 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9949 if (type
== error_mark_node
)
9950 RETURN (error_mark_node
);
9952 /* We do NOT check for matching decls pushed separately at this
9953 point, as they may not represent instantiations of this
9954 template, and in any case are considered separate under the
9957 DECL_USE_TEMPLATE (r
) = 0;
9958 TREE_TYPE (r
) = type
;
9959 /* Clear out the mangled name and RTL for the instantiation. */
9960 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
9961 SET_DECL_RTL (r
, NULL
);
9962 /* Leave DECL_INITIAL set on deleted instantiations. */
9963 if (!DECL_DELETED_FN (r
))
9964 DECL_INITIAL (r
) = NULL_TREE
;
9965 DECL_CONTEXT (r
) = ctx
;
9967 if (member
&& DECL_CONV_FN_P (r
))
9968 /* Type-conversion operator. Reconstruct the name, in
9969 case it's the name of one of the template's parameters. */
9970 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
9972 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
9974 DECL_RESULT (r
) = NULL_TREE
;
9976 TREE_STATIC (r
) = 0;
9977 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
9978 DECL_EXTERNAL (r
) = 1;
9979 /* If this is an instantiation of a function with internal
9980 linkage, we already know what object file linkage will be
9981 assigned to the instantiation. */
9982 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
9983 DECL_DEFER_OUTPUT (r
) = 0;
9984 DECL_CHAIN (r
) = NULL_TREE
;
9985 DECL_PENDING_INLINE_INFO (r
) = 0;
9986 DECL_PENDING_INLINE_P (r
) = 0;
9987 DECL_SAVED_TREE (r
) = NULL_TREE
;
9988 DECL_STRUCT_FUNCTION (r
) = NULL
;
9990 /* We'll re-clone as appropriate in instantiate_template. */
9991 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
9993 /* If we aren't complaining now, return on error before we register
9994 the specialization so that we'll complain eventually. */
9995 if ((complain
& tf_error
) == 0
9996 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
9997 && !grok_op_properties (r
, /*complain=*/false))
9998 RETURN (error_mark_node
);
10000 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10001 this in the special friend case mentioned above where
10002 GEN_TMPL is NULL. */
10005 DECL_TEMPLATE_INFO (r
)
10006 = build_template_info (gen_tmpl
, argvec
);
10007 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10008 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10010 /* We're not supposed to instantiate default arguments
10011 until they are called, for a template. But, for a
10014 template <class T> void f ()
10015 { extern void g(int i = T()); }
10017 we should do the substitution when the template is
10018 instantiated. We handle the member function case in
10019 instantiate_class_template since the default arguments
10020 might refer to other members of the class. */
10022 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
10023 && !uses_template_parms (argvec
))
10024 tsubst_default_arguments (r
);
10027 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
10029 /* Copy the list of befriending classes. */
10030 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
10032 friends
= &TREE_CHAIN (*friends
))
10034 *friends
= copy_node (*friends
);
10035 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
10040 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
10042 maybe_retrofit_in_chrg (r
);
10043 if (DECL_CONSTRUCTOR_P (r
))
10044 grok_ctor_properties (ctx
, r
);
10045 /* If this is an instantiation of a member template, clone it.
10046 If it isn't, that'll be handled by
10047 clone_constructors_and_destructors. */
10048 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
10049 clone_function_decl (r
, /*update_method_vec_p=*/0);
10051 else if ((complain
& tf_error
) != 0
10052 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10053 && !grok_op_properties (r
, /*complain=*/true))
10054 RETURN (error_mark_node
);
10056 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
10057 SET_DECL_FRIEND_CONTEXT (r
,
10058 tsubst (DECL_FRIEND_CONTEXT (t
),
10059 args
, complain
, in_decl
));
10061 /* Possibly limit visibility based on template args. */
10062 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10063 if (DECL_VISIBILITY_SPECIFIED (t
))
10065 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10066 DECL_ATTRIBUTES (r
)
10067 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10069 determine_visibility (r
);
10070 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
10071 && !processing_template_decl
)
10072 defaulted_late_check (r
);
10074 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10075 args
, complain
, in_decl
);
10081 tree type
= NULL_TREE
;
10083 tree expanded_types
= NULL_TREE
;
10084 tree prev_r
= NULL_TREE
;
10085 tree first_r
= NULL_TREE
;
10087 if (FUNCTION_PARAMETER_PACK_P (t
))
10089 /* If there is a local specialization that isn't a
10090 parameter pack, it means that we're doing a "simple"
10091 substitution from inside tsubst_pack_expansion. Just
10092 return the local specialization (which will be a single
10094 tree spec
= retrieve_local_specialization (t
);
10096 && TREE_CODE (spec
) == PARM_DECL
10097 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
10100 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10101 the parameters in this function parameter pack. */
10102 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10103 complain
, in_decl
);
10104 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10106 len
= TREE_VEC_LENGTH (expanded_types
);
10108 /* Zero-length parameter packs are boring. Just substitute
10111 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
10116 /* All we did was update the type. Make a note of that. */
10117 type
= expanded_types
;
10118 expanded_types
= NULL_TREE
;
10122 /* Loop through all of the parameter's we'll build. When T is
10123 a function parameter pack, LEN is the number of expanded
10124 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10126 for (i
= 0; i
< len
; ++i
)
10130 if (DECL_TEMPLATE_PARM_P (t
))
10131 SET_DECL_TEMPLATE_PARM_P (r
);
10133 if (expanded_types
)
10134 /* We're on the Ith parameter of the function parameter
10137 /* An argument of a function parameter pack is not a parameter
10139 FUNCTION_PARAMETER_PACK_P (r
) = false;
10141 /* Get the Ith type. */
10142 type
= TREE_VEC_ELT (expanded_types
, i
);
10145 /* Rename the parameter to include the index. */
10147 make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10150 /* We're dealing with a normal parameter. */
10151 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10153 type
= type_decays_to (type
);
10154 TREE_TYPE (r
) = type
;
10155 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10157 if (DECL_INITIAL (r
))
10159 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
10160 DECL_INITIAL (r
) = TREE_TYPE (r
);
10162 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
10163 complain
, in_decl
);
10166 DECL_CONTEXT (r
) = NULL_TREE
;
10168 if (!DECL_TEMPLATE_PARM_P (r
))
10169 DECL_ARG_TYPE (r
) = type_passed_as (type
);
10171 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10172 args
, complain
, in_decl
);
10174 /* Keep track of the first new parameter we
10175 generate. That's what will be returned to the
10180 /* Build a proper chain of parameters when substituting
10181 into a function parameter pack. */
10183 DECL_CHAIN (prev_r
) = r
;
10186 /* If cp_unevaluated_operand is set, we're just looking for a
10187 single dummy parameter, so don't keep going. */
10188 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
10189 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
10190 complain
, DECL_CHAIN (t
));
10192 /* FIRST_R contains the start of the chain we've built. */
10202 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10203 if (type
== error_mark_node
)
10204 RETURN (error_mark_node
);
10205 TREE_TYPE (r
) = type
;
10206 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10208 if (DECL_C_BIT_FIELD (r
))
10209 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10210 non-bit-fields DECL_INITIAL is a non-static data member
10211 initializer, which gets deferred instantiation. */
10213 = tsubst_expr (DECL_INITIAL (t
), args
,
10215 /*integral_constant_expression_p=*/true);
10216 else if (DECL_INITIAL (t
))
10218 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10219 NSDMI in perform_member_init. Still set DECL_INITIAL
10220 so that we know there is one. */
10221 DECL_INITIAL (r
) = void_zero_node
;
10222 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
10223 retrofit_lang_decl (r
);
10224 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10226 /* We don't have to set DECL_CONTEXT here; it is set by
10227 finish_member_declaration. */
10228 DECL_CHAIN (r
) = NULL_TREE
;
10229 if (VOID_TYPE_P (type
))
10230 error ("instantiation of %q+D as type %qT", r
, type
);
10232 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10233 args
, complain
, in_decl
);
10238 /* We reach here only for member using decls. */
10239 if (DECL_DEPENDENT_P (t
))
10241 r
= do_class_using_decl
10242 (tsubst_copy (USING_DECL_SCOPE (t
), args
, complain
, in_decl
),
10243 tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
));
10245 r
= error_mark_node
;
10248 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
10249 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
10255 DECL_CHAIN (r
) = NULL_TREE
;
10262 tree argvec
= NULL_TREE
;
10263 tree gen_tmpl
= NULL_TREE
;
10265 tree tmpl
= NULL_TREE
;
10267 tree type
= NULL_TREE
;
10270 if (TREE_CODE (t
) == TYPE_DECL
10271 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
10273 /* If this is the canonical decl, we don't have to
10274 mess with instantiations, and often we can't (for
10275 typename, template type parms and such). Note that
10276 TYPE_NAME is not correct for the above test if
10277 we've copied the type for a typedef. */
10278 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10279 if (type
== error_mark_node
)
10280 RETURN (error_mark_node
);
10281 r
= TYPE_NAME (type
);
10285 /* Check to see if we already have the specialization we
10288 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
10290 /* T is a static data member or namespace-scope entity.
10291 We have to substitute into namespace-scope variables
10292 (even though such entities are never templates) because
10295 template <class T> void f() { extern T t; }
10297 where the entity referenced is not known until
10298 instantiation time. */
10300 ctx
= DECL_CONTEXT (t
);
10301 if (DECL_CLASS_SCOPE_P (t
))
10303 ctx
= tsubst_aggr_type (ctx
, args
,
10305 in_decl
, /*entering_scope=*/1);
10306 /* If CTX is unchanged, then T is in fact the
10307 specialization we want. That situation occurs when
10308 referencing a static data member within in its own
10309 class. We can use pointer equality, rather than
10310 same_type_p, because DECL_CONTEXT is always
10312 if (ctx
== DECL_CONTEXT (t
)
10313 && (TREE_CODE (t
) != TYPE_DECL
10314 /* ... unless T is a member template; in which
10315 case our caller can be willing to create a
10316 specialization of that template represented
10318 || !(DECL_TI_TEMPLATE (t
)
10319 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))))
10325 tmpl
= DECL_TI_TEMPLATE (t
);
10326 gen_tmpl
= most_general_template (tmpl
);
10327 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
10328 if (argvec
== error_mark_node
)
10329 RETURN (error_mark_node
);
10330 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10331 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10336 /* A local variable. */
10338 /* Subsequent calls to pushdecl will fill this in. */
10340 spec
= retrieve_local_specialization (t
);
10342 /* If we already have the specialization we need, there is
10343 nothing more to do. */
10350 /* Create a new node for the specialization we need. */
10352 if (type
== NULL_TREE
)
10354 if (is_typedef_decl (t
))
10355 type
= DECL_ORIGINAL_TYPE (t
);
10357 type
= TREE_TYPE (t
);
10358 if (TREE_CODE (t
) == VAR_DECL
10359 && VAR_HAD_UNKNOWN_BOUND (t
)
10360 && type
!= error_mark_node
)
10361 type
= strip_array_domain (type
);
10362 type
= tsubst (type
, args
, complain
, in_decl
);
10364 if (TREE_CODE (r
) == VAR_DECL
)
10366 /* Even if the original location is out of scope, the
10367 newly substituted one is not. */
10368 DECL_DEAD_FOR_LOCAL (r
) = 0;
10369 DECL_INITIALIZED_P (r
) = 0;
10370 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
10371 if (type
== error_mark_node
)
10372 RETURN (error_mark_node
);
10373 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10375 /* It may seem that this case cannot occur, since:
10380 declares a function, not a variable. However:
10383 template <typename T> void g() { T t; }
10384 template void g<f>();
10386 is an attempt to declare a variable with function
10388 error ("variable %qD has function type",
10389 /* R is not yet sufficiently initialized, so we
10390 just use its name. */
10392 RETURN (error_mark_node
);
10394 type
= complete_type (type
);
10395 /* Wait until cp_finish_decl to set this again, to handle
10396 circular dependency (template/instantiate6.C). */
10397 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
10398 type
= check_var_type (DECL_NAME (r
), type
);
10400 if (DECL_HAS_VALUE_EXPR_P (t
))
10402 tree ve
= DECL_VALUE_EXPR (t
);
10403 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
10404 /*constant_expression_p=*/false);
10405 if (REFERENCE_REF_P (ve
))
10407 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
10408 ve
= TREE_OPERAND (ve
, 0);
10410 SET_DECL_VALUE_EXPR (r
, ve
);
10413 else if (DECL_SELF_REFERENCE_P (t
))
10414 SET_DECL_SELF_REFERENCE_P (r
);
10415 TREE_TYPE (r
) = type
;
10416 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10417 DECL_CONTEXT (r
) = ctx
;
10418 /* Clear out the mangled name and RTL for the instantiation. */
10419 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10420 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10421 SET_DECL_RTL (r
, NULL
);
10422 /* The initializer must not be expanded until it is required;
10423 see [temp.inst]. */
10424 DECL_INITIAL (r
) = NULL_TREE
;
10425 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10426 SET_DECL_RTL (r
, NULL
);
10427 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
10428 if (TREE_CODE (r
) == VAR_DECL
)
10430 /* Possibly limit visibility based on template args. */
10431 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10432 if (DECL_VISIBILITY_SPECIFIED (t
))
10434 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10435 DECL_ATTRIBUTES (r
)
10436 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10438 determine_visibility (r
);
10443 /* A static data member declaration is always marked
10444 external when it is declared in-class, even if an
10445 initializer is present. We mimic the non-template
10446 processing here. */
10447 DECL_EXTERNAL (r
) = 1;
10449 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10450 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
10451 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10453 else if (cp_unevaluated_operand
)
10455 /* We're substituting this var in a decltype outside of its
10456 scope, such as for a lambda return type. Don't add it to
10457 local_specializations, do perform auto deduction. */
10458 tree auto_node
= type_uses_auto (type
);
10462 = tsubst_expr (DECL_INITIAL (t
), args
, complain
, in_decl
,
10463 /*constant_expression_p=*/false);
10464 init
= resolve_nondeduced_context (init
);
10465 TREE_TYPE (r
) = type
10466 = do_auto_deduction (type
, init
, auto_node
);
10470 register_local_specialization (r
, t
);
10472 DECL_CHAIN (r
) = NULL_TREE
;
10474 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
10476 args
, complain
, in_decl
);
10478 /* Preserve a typedef that names a type. */
10479 if (is_typedef_decl (r
))
10481 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
10482 set_underlying_type (r
);
10485 layout_decl (r
, 0);
10490 gcc_unreachable ();
10495 /* Restore the file and line information. */
10496 input_location
= saved_loc
;
10501 /* Substitute into the ARG_TYPES of a function type.
10502 If END is a TREE_CHAIN, leave it and any following types
10506 tsubst_arg_types (tree arg_types
,
10509 tsubst_flags_t complain
,
10512 tree remaining_arg_types
;
10513 tree type
= NULL_TREE
;
10515 tree expanded_args
= NULL_TREE
;
10518 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
10521 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
10522 args
, end
, complain
, in_decl
);
10523 if (remaining_arg_types
== error_mark_node
)
10524 return error_mark_node
;
10526 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
10528 /* For a pack expansion, perform substitution on the
10529 entire expression. Later on, we'll handle the arguments
10531 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
10532 args
, complain
, in_decl
);
10534 if (TREE_CODE (expanded_args
) == TREE_VEC
)
10535 /* So that we'll spin through the parameters, one by one. */
10536 i
= TREE_VEC_LENGTH (expanded_args
);
10539 /* We only partially substituted into the parameter
10540 pack. Our type is TYPE_PACK_EXPANSION. */
10541 type
= expanded_args
;
10542 expanded_args
= NULL_TREE
;
10550 type
= TREE_VEC_ELT (expanded_args
, i
);
10552 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
10554 if (type
== error_mark_node
)
10555 return error_mark_node
;
10556 if (VOID_TYPE_P (type
))
10558 if (complain
& tf_error
)
10560 error ("invalid parameter type %qT", type
);
10562 error ("in declaration %q+D", in_decl
);
10564 return error_mark_node
;
10567 /* Do array-to-pointer, function-to-pointer conversion, and ignore
10568 top-level qualifiers as required. */
10569 type
= cv_unqualified (type_decays_to (type
));
10571 /* We do not substitute into default arguments here. The standard
10572 mandates that they be instantiated only when needed, which is
10573 done in build_over_call. */
10574 default_arg
= TREE_PURPOSE (arg_types
);
10576 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
10578 /* We've instantiated a template before its default arguments
10579 have been parsed. This can happen for a nested template
10580 class, and is not an error unless we require the default
10581 argument in a call of this function. */
10582 remaining_arg_types
=
10583 tree_cons (default_arg
, type
, remaining_arg_types
);
10584 VEC_safe_push (tree
, gc
, DEFARG_INSTANTIATIONS (default_arg
),
10585 remaining_arg_types
);
10588 remaining_arg_types
=
10589 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
10592 return remaining_arg_types
;
10595 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
10596 *not* handle the exception-specification for FNTYPE, because the
10597 initial substitution of explicitly provided template parameters
10598 during argument deduction forbids substitution into the
10599 exception-specification:
10603 All references in the function type of the function template to the
10604 corresponding template parameters are replaced by the specified tem-
10605 plate argument values. If a substitution in a template parameter or
10606 in the function type of the function template results in an invalid
10607 type, type deduction fails. [Note: The equivalent substitution in
10608 exception specifications is done only when the function is instanti-
10609 ated, at which point a program is ill-formed if the substitution
10610 results in an invalid type.] */
10613 tsubst_function_type (tree t
,
10615 tsubst_flags_t complain
,
10622 /* The TYPE_CONTEXT is not used for function/method types. */
10623 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
10625 /* Substitute the return type. */
10626 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10627 if (return_type
== error_mark_node
)
10628 return error_mark_node
;
10629 /* The standard does not presently indicate that creation of a
10630 function type with an invalid return type is a deduction failure.
10631 However, that is clearly analogous to creating an array of "void"
10632 or a reference to a reference. This is core issue #486. */
10633 if (TREE_CODE (return_type
) == ARRAY_TYPE
10634 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
10636 if (complain
& tf_error
)
10638 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
10639 error ("function returning an array");
10641 error ("function returning a function");
10643 return error_mark_node
;
10646 /* Substitute the argument types. */
10647 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
10648 complain
, in_decl
);
10649 if (arg_types
== error_mark_node
)
10650 return error_mark_node
;
10652 /* Construct a new type node and return it. */
10653 if (TREE_CODE (t
) == FUNCTION_TYPE
)
10655 fntype
= build_function_type (return_type
, arg_types
);
10656 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
10660 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
10661 if (! MAYBE_CLASS_TYPE_P (r
))
10665 Type deduction may fail for any of the following
10668 -- Attempting to create "pointer to member of T" when T
10669 is not a class type. */
10670 if (complain
& tf_error
)
10671 error ("creating pointer to member function of non-class type %qT",
10673 return error_mark_node
;
10676 fntype
= build_method_type_directly (r
, return_type
,
10677 TREE_CHAIN (arg_types
));
10679 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
10684 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
10685 ARGS into that specification, and return the substituted
10686 specification. If there is no specification, return NULL_TREE. */
10689 tsubst_exception_specification (tree fntype
,
10691 tsubst_flags_t complain
,
10698 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
10699 new_specs
= NULL_TREE
;
10700 if (specs
&& TREE_PURPOSE (specs
))
10702 /* A noexcept-specifier. */
10703 tree expr
= TREE_PURPOSE (specs
);
10704 if (expr
== boolean_true_node
|| expr
== boolean_false_node
)
10708 /* Defer instantiation of noexcept-specifiers to avoid
10709 excessive instantiations (c++/49107). */
10710 new_specs
= make_node (DEFERRED_NOEXCEPT
);
10711 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
10713 /* We already partially instantiated this member template,
10714 so combine the new args with the old. */
10715 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
10716 = DEFERRED_NOEXCEPT_PATTERN (expr
);
10717 DEFERRED_NOEXCEPT_ARGS (new_specs
)
10718 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
10722 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
10723 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
10727 new_specs
= tsubst_copy_and_build
10728 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
10729 /*integral_constant_expression_p=*/true);
10730 new_specs
= build_noexcept_spec (new_specs
, complain
);
10734 if (! TREE_VALUE (specs
))
10741 tree expanded_specs
= NULL_TREE
;
10743 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
10745 /* Expand the pack expansion type. */
10746 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
10750 if (expanded_specs
== error_mark_node
)
10751 return error_mark_node
;
10752 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
10753 len
= TREE_VEC_LENGTH (expanded_specs
);
10756 /* We're substituting into a member template, so
10757 we got a TYPE_PACK_EXPANSION back. Add that
10758 expansion and move on. */
10759 gcc_assert (TREE_CODE (expanded_specs
)
10760 == TYPE_PACK_EXPANSION
);
10761 new_specs
= add_exception_specifier (new_specs
,
10764 specs
= TREE_CHAIN (specs
);
10769 for (i
= 0; i
< len
; ++i
)
10771 if (expanded_specs
)
10772 spec
= TREE_VEC_ELT (expanded_specs
, i
);
10774 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
10775 if (spec
== error_mark_node
)
10777 new_specs
= add_exception_specifier (new_specs
, spec
,
10781 specs
= TREE_CHAIN (specs
);
10787 /* Take the tree structure T and replace template parameters used
10788 therein with the argument vector ARGS. IN_DECL is an associated
10789 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
10790 Issue error and warning messages under control of COMPLAIN. Note
10791 that we must be relatively non-tolerant of extensions here, in
10792 order to preserve conformance; if we allow substitutions that
10793 should not be allowed, we may allow argument deductions that should
10794 not succeed, and therefore report ambiguous overload situations
10795 where there are none. In theory, we could allow the substitution,
10796 but indicate that it should have failed, and allow our caller to
10797 make sure that the right thing happens, but we don't try to do this
10800 This function is used for dealing with types, decls and the like;
10801 for expressions, use tsubst_expr or tsubst_copy. */
10804 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10806 enum tree_code code
;
10807 tree type
, r
= NULL_TREE
;
10809 if (t
== NULL_TREE
|| t
== error_mark_node
10810 || t
== integer_type_node
10811 || t
== void_type_node
10812 || t
== char_type_node
10813 || t
== unknown_type_node
10814 || TREE_CODE (t
) == NAMESPACE_DECL
10815 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
10819 return tsubst_decl (t
, args
, complain
);
10821 if (args
== NULL_TREE
)
10824 code
= TREE_CODE (t
);
10826 if (code
== IDENTIFIER_NODE
)
10827 type
= IDENTIFIER_TYPE_VALUE (t
);
10829 type
= TREE_TYPE (t
);
10831 gcc_assert (type
!= unknown_type_node
);
10833 /* Reuse typedefs. We need to do this to handle dependent attributes,
10834 such as attribute aligned. */
10836 && typedef_variant_p (t
))
10838 tree decl
= TYPE_NAME (t
);
10840 if (TYPE_DECL_ALIAS_P (decl
)
10841 && DECL_LANG_SPECIFIC (decl
)
10842 && DECL_TEMPLATE_INFO (decl
)
10843 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
10845 /* DECL represents an alias template and we want to
10846 instantiate it. Let's substitute our arguments for the
10847 template parameters into the declaration and get the
10849 r
= tsubst (decl
, args
, complain
, decl
);
10851 else if (DECL_CLASS_SCOPE_P (decl
)
10852 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
10853 && uses_template_parms (DECL_CONTEXT (decl
)))
10855 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
10856 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
10857 r
= retrieve_specialization (tmpl
, gen_args
, 0);
10859 else if (DECL_FUNCTION_SCOPE_P (decl
)
10860 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
10861 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
10862 r
= retrieve_local_specialization (decl
);
10864 /* The typedef is from a non-template context. */
10870 r
= cp_build_qualified_type_real
10871 (r
, cp_type_quals (t
) | cp_type_quals (r
),
10872 complain
| tf_ignore_bad_quals
);
10876 /* We don't have an instantiation yet, so drop the typedef. */
10877 t
= DECL_ORIGINAL_TYPE (decl
);
10881 && code
!= TYPENAME_TYPE
10882 && code
!= TEMPLATE_TYPE_PARM
10883 && code
!= IDENTIFIER_NODE
10884 && code
!= FUNCTION_TYPE
10885 && code
!= METHOD_TYPE
)
10886 type
= tsubst (type
, args
, complain
, in_decl
);
10887 if (type
== error_mark_node
)
10888 return error_mark_node
;
10894 case ENUMERAL_TYPE
:
10895 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
10896 /*entering_scope=*/0);
10899 case IDENTIFIER_NODE
:
10910 if (t
== integer_type_node
)
10913 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
10914 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
10918 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
10920 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
10921 /*integral_constant_expression_p=*/false);
10923 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
10925 if (TREE_CODE (max
) == NOP_EXPR
10926 && TREE_SIDE_EFFECTS (omax
)
10927 && !TREE_TYPE (max
))
10928 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
10930 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
10931 with TREE_SIDE_EFFECTS that indicates this is not an integral
10932 constant expression. */
10933 if (processing_template_decl
10934 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
10936 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
10937 TREE_SIDE_EFFECTS (max
) = 1;
10940 return compute_array_index_type (NULL_TREE
, max
, complain
);
10943 case TEMPLATE_TYPE_PARM
:
10944 case TEMPLATE_TEMPLATE_PARM
:
10945 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10946 case TEMPLATE_PARM_INDEX
:
10951 tree arg
= NULL_TREE
;
10955 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
10956 template_parm_level_and_index (t
, &level
, &idx
);
10958 levels
= TMPL_ARGS_DEPTH (args
);
10959 if (level
<= levels
)
10961 arg
= TMPL_ARG (args
, level
, idx
);
10963 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
10964 /* See through ARGUMENT_PACK_SELECT arguments. */
10965 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
10968 if (arg
== error_mark_node
)
10969 return error_mark_node
;
10970 else if (arg
!= NULL_TREE
)
10972 if (ARGUMENT_PACK_P (arg
))
10973 /* If ARG is an argument pack, we don't actually want to
10974 perform a substitution here, because substitutions
10975 for argument packs are only done
10976 element-by-element. We can get to this point when
10977 substituting the type of a non-type template
10978 parameter pack, when that type actually contains
10979 template parameter packs from an outer template, e.g.,
10981 template<typename... Types> struct A {
10982 template<Types... Values> struct B { };
10986 if (code
== TEMPLATE_TYPE_PARM
)
10989 gcc_assert (TYPE_P (arg
));
10991 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
10993 return cp_build_qualified_type_real
10994 (arg
, quals
, complain
| tf_ignore_bad_quals
);
10996 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
10998 /* We are processing a type constructed from a
10999 template template parameter. */
11000 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
11001 args
, complain
, in_decl
);
11002 if (argvec
== error_mark_node
)
11003 return error_mark_node
;
11005 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
11006 || TREE_CODE (arg
) == TEMPLATE_DECL
11007 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
11009 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
11010 /* Consider this code:
11012 template <template <class> class Template>
11014 template <class Arg> using Bind = Template<Arg>;
11017 template <template <class> class Template, class Arg>
11018 using Instantiate = Template<Arg>; //#0
11020 template <template <class> class Template,
11023 Instantiate<Internal<Template>::template Bind,
11026 When #1 is parsed, the
11027 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11028 parameter `Template' in #0 matches the
11029 UNBOUND_CLASS_TEMPLATE representing the argument
11030 `Internal<Template>::template Bind'; We then want
11031 to assemble the type `Bind<Argument>' that can't
11032 be fully created right now, because
11033 `Internal<Template>' not being complete, the Bind
11034 template cannot be looked up in that context. So
11035 we need to "store" `Bind<Argument>' for later
11036 when the context of Bind becomes complete. Let's
11037 store that in a TYPENAME_TYPE. */
11038 return make_typename_type (TYPE_CONTEXT (arg
),
11039 build_nt (TEMPLATE_ID_EXPR
,
11040 TYPE_IDENTIFIER (arg
),
11045 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11046 are resolving nested-types in the signature of a
11047 member function templates. Otherwise ARG is a
11048 TEMPLATE_DECL and is the real template to be
11050 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
11051 arg
= TYPE_NAME (arg
);
11053 r
= lookup_template_class (arg
,
11055 DECL_CONTEXT (arg
),
11056 /*entering_scope=*/0,
11058 return cp_build_qualified_type_real
11059 (r
, cp_type_quals (t
), complain
);
11062 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11063 return convert_from_reference (unshare_expr (arg
));
11067 /* This can happen during the attempted tsubst'ing in
11068 unify. This means that we don't yet have any information
11069 about the template parameter in question. */
11072 /* Early in template argument deduction substitution, we don't
11073 want to reduce the level of 'auto', or it will be confused
11074 with a normal template parm in subsequent deduction. */
11075 if (is_auto (t
) && (complain
& tf_partial
))
11078 /* If we get here, we must have been looking at a parm for a
11079 more deeply nested template. Make a new version of this
11080 template parameter, but with a lower level. */
11083 case TEMPLATE_TYPE_PARM
:
11084 case TEMPLATE_TEMPLATE_PARM
:
11085 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11086 if (cp_type_quals (t
))
11088 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
11089 r
= cp_build_qualified_type_real
11090 (r
, cp_type_quals (t
),
11091 complain
| (code
== TEMPLATE_TYPE_PARM
11092 ? tf_ignore_bad_quals
: 0));
11097 TEMPLATE_TYPE_PARM_INDEX (r
)
11098 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
11099 r
, levels
, args
, complain
);
11100 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
11101 TYPE_MAIN_VARIANT (r
) = r
;
11102 TYPE_POINTER_TO (r
) = NULL_TREE
;
11103 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
11105 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
11106 /* We have reduced the level of the template
11107 template parameter, but not the levels of its
11108 template parameters, so canonical_type_parameter
11109 will not be able to find the canonical template
11110 template parameter for this level. Thus, we
11111 require structural equality checking to compare
11112 TEMPLATE_TEMPLATE_PARMs. */
11113 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11114 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
11115 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11117 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
11119 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11121 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
11122 complain
, in_decl
);
11123 if (argvec
== error_mark_node
)
11124 return error_mark_node
;
11126 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
11127 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
11132 case TEMPLATE_PARM_INDEX
:
11133 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
11137 gcc_unreachable ();
11145 tree purpose
, value
, chain
;
11147 if (t
== void_list_node
)
11150 purpose
= TREE_PURPOSE (t
);
11153 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
11154 if (purpose
== error_mark_node
)
11155 return error_mark_node
;
11157 value
= TREE_VALUE (t
);
11160 value
= tsubst (value
, args
, complain
, in_decl
);
11161 if (value
== error_mark_node
)
11162 return error_mark_node
;
11164 chain
= TREE_CHAIN (t
);
11165 if (chain
&& chain
!= void_type_node
)
11167 chain
= tsubst (chain
, args
, complain
, in_decl
);
11168 if (chain
== error_mark_node
)
11169 return error_mark_node
;
11171 if (purpose
== TREE_PURPOSE (t
)
11172 && value
== TREE_VALUE (t
)
11173 && chain
== TREE_CHAIN (t
))
11175 return hash_tree_cons (purpose
, value
, chain
);
11179 /* We should never be tsubsting a binfo. */
11180 gcc_unreachable ();
11183 /* A vector of template arguments. */
11184 gcc_assert (!type
);
11185 return tsubst_template_args (t
, args
, complain
, in_decl
);
11188 case REFERENCE_TYPE
:
11190 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
11195 Type deduction may fail for any of the following
11198 -- Attempting to create a pointer to reference type.
11199 -- Attempting to create a reference to a reference type or
11200 a reference to void.
11202 Core issue 106 says that creating a reference to a reference
11203 during instantiation is no longer a cause for failure. We
11204 only enforce this check in strict C++98 mode. */
11205 if ((TREE_CODE (type
) == REFERENCE_TYPE
11206 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
11207 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
11209 static location_t last_loc
;
11211 /* We keep track of the last time we issued this error
11212 message to avoid spewing a ton of messages during a
11213 single bad template instantiation. */
11214 if (complain
& tf_error
11215 && last_loc
!= input_location
)
11217 if (TREE_CODE (type
) == VOID_TYPE
)
11218 error ("forming reference to void");
11219 else if (code
== POINTER_TYPE
)
11220 error ("forming pointer to reference type %qT", type
);
11222 error ("forming reference to reference type %qT", type
);
11223 last_loc
= input_location
;
11226 return error_mark_node
;
11228 else if (code
== POINTER_TYPE
)
11230 r
= build_pointer_type (type
);
11231 if (TREE_CODE (type
) == METHOD_TYPE
)
11232 r
= build_ptrmemfunc_type (r
);
11234 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
11235 /* In C++0x, during template argument substitution, when there is an
11236 attempt to create a reference to a reference type, reference
11237 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11239 "If a template-argument for a template-parameter T names a type
11240 that is a reference to a type A, an attempt to create the type
11241 'lvalue reference to cv T' creates the type 'lvalue reference to
11242 A,' while an attempt to create the type type rvalue reference to
11243 cv T' creates the type T"
11245 r
= cp_build_reference_type
11247 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
11249 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
11250 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11252 if (r
!= error_mark_node
)
11253 /* Will this ever be needed for TYPE_..._TO values? */
11260 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
11261 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
11265 Type deduction may fail for any of the following
11268 -- Attempting to create "pointer to member of T" when T
11269 is not a class type. */
11270 if (complain
& tf_error
)
11271 error ("creating pointer to member of non-class type %qT", r
);
11272 return error_mark_node
;
11274 if (TREE_CODE (type
) == REFERENCE_TYPE
)
11276 if (complain
& tf_error
)
11277 error ("creating pointer to member reference type %qT", type
);
11278 return error_mark_node
;
11280 if (TREE_CODE (type
) == VOID_TYPE
)
11282 if (complain
& tf_error
)
11283 error ("creating pointer to member of type void");
11284 return error_mark_node
;
11286 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
11287 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11289 /* The type of the implicit object parameter gets its
11290 cv-qualifiers from the FUNCTION_TYPE. */
11292 tree method_type
= build_memfn_type (type
, r
, type_memfn_quals (type
));
11293 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
11294 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
11298 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
11302 case FUNCTION_TYPE
:
11307 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
11308 if (fntype
== error_mark_node
)
11309 return error_mark_node
;
11311 /* Substitute the exception specification. */
11312 specs
= tsubst_exception_specification (t
, args
, complain
,
11313 in_decl
, /*defer_ok*/true);
11314 if (specs
== error_mark_node
)
11315 return error_mark_node
;
11317 fntype
= build_exception_variant (fntype
, specs
);
11322 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
11323 if (domain
== error_mark_node
)
11324 return error_mark_node
;
11326 /* As an optimization, we avoid regenerating the array type if
11327 it will obviously be the same as T. */
11328 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
11331 /* These checks should match the ones in grokdeclarator.
11335 The deduction may fail for any of the following reasons:
11337 -- Attempting to create an array with an element type that
11338 is void, a function type, or a reference type, or [DR337]
11339 an abstract class type. */
11340 if (TREE_CODE (type
) == VOID_TYPE
11341 || TREE_CODE (type
) == FUNCTION_TYPE
11342 || TREE_CODE (type
) == REFERENCE_TYPE
)
11344 if (complain
& tf_error
)
11345 error ("creating array of %qT", type
);
11346 return error_mark_node
;
11348 if (ABSTRACT_CLASS_TYPE_P (type
))
11350 if (complain
& tf_error
)
11351 error ("creating array of %qT, which is an abstract class type",
11353 return error_mark_node
;
11356 r
= build_cplus_array_type (type
, domain
);
11358 if (TYPE_USER_ALIGN (t
))
11360 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
11361 TYPE_USER_ALIGN (r
) = 1;
11367 case TYPENAME_TYPE
:
11369 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11370 in_decl
, /*entering_scope=*/1);
11371 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
11372 complain
, in_decl
);
11374 if (ctx
== error_mark_node
|| f
== error_mark_node
)
11375 return error_mark_node
;
11377 if (!MAYBE_CLASS_TYPE_P (ctx
))
11379 if (complain
& tf_error
)
11380 error ("%qT is not a class, struct, or union type", ctx
);
11381 return error_mark_node
;
11383 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
11385 /* Normally, make_typename_type does not require that the CTX
11386 have complete type in order to allow things like:
11388 template <class T> struct S { typename S<T>::X Y; };
11390 But, such constructs have already been resolved by this
11391 point, so here CTX really should have complete type, unless
11392 it's a partial instantiation. */
11393 ctx
= complete_type (ctx
);
11394 if (!COMPLETE_TYPE_P (ctx
))
11396 if (complain
& tf_error
)
11397 cxx_incomplete_type_error (NULL_TREE
, ctx
);
11398 return error_mark_node
;
11402 f
= make_typename_type (ctx
, f
, typename_type
,
11403 (complain
& tf_error
) | tf_keep_type_decl
);
11404 if (f
== error_mark_node
)
11406 if (TREE_CODE (f
) == TYPE_DECL
)
11408 complain
|= tf_ignore_bad_quals
;
11412 if (TREE_CODE (f
) != TYPENAME_TYPE
)
11414 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
11416 if (complain
& tf_error
)
11417 error ("%qT resolves to %qT, which is not an enumeration type",
11420 return error_mark_node
;
11422 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
11424 if (complain
& tf_error
)
11425 error ("%qT resolves to %qT, which is is not a class type",
11428 return error_mark_node
;
11432 return cp_build_qualified_type_real
11433 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
11436 case UNBOUND_CLASS_TEMPLATE
:
11438 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11439 in_decl
, /*entering_scope=*/1);
11440 tree name
= TYPE_IDENTIFIER (t
);
11441 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
11443 if (ctx
== error_mark_node
|| name
== error_mark_node
)
11444 return error_mark_node
;
11447 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
11448 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
11455 ++cp_unevaluated_operand
;
11456 ++c_inhibit_evaluation_warnings
;
11458 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
11460 /*integral_constant_expression_p=*/false);
11462 --cp_unevaluated_operand
;
11463 --c_inhibit_evaluation_warnings
;
11465 type
= finish_typeof (type
);
11466 return cp_build_qualified_type_real (type
,
11468 | cp_type_quals (type
),
11472 case DECLTYPE_TYPE
:
11476 ++cp_unevaluated_operand
;
11477 ++c_inhibit_evaluation_warnings
;
11479 type
= tsubst_expr (DECLTYPE_TYPE_EXPR (t
), args
,
11481 /*integral_constant_expression_p=*/false);
11483 --cp_unevaluated_operand
;
11484 --c_inhibit_evaluation_warnings
;
11486 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
11487 type
= lambda_capture_field_type (type
);
11488 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
11489 type
= lambda_proxy_type (type
);
11491 type
= finish_decltype_type
11492 (type
, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
), complain
);
11493 return cp_build_qualified_type_real (type
,
11495 | cp_type_quals (type
),
11499 case UNDERLYING_TYPE
:
11501 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
11502 complain
, in_decl
);
11503 return finish_underlying_type (type
);
11506 case TYPE_ARGUMENT_PACK
:
11507 case NONTYPE_ARGUMENT_PACK
:
11509 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
11511 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
11515 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
11517 /* For template nontype argument packs, also substitute into
11519 if (code
== NONTYPE_ARGUMENT_PACK
)
11520 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11538 /* We should use one of the expression tsubsts for these codes. */
11539 gcc_unreachable ();
11542 sorry ("use of %qs in template", tree_code_name
[(int) code
]);
11543 return error_mark_node
;
11547 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
11548 type of the expression on the left-hand side of the "." or "->"
11552 tsubst_baselink (tree baselink
, tree object_type
,
11553 tree args
, tsubst_flags_t complain
, tree in_decl
)
11556 tree qualifying_scope
;
11559 tree template_args
= 0;
11560 bool template_id_p
= false;
11561 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
11563 /* A baselink indicates a function from a base class. Both the
11564 BASELINK_ACCESS_BINFO and the base class referenced may
11565 indicate bases of the template class, rather than the
11566 instantiated class. In addition, lookups that were not
11567 ambiguous before may be ambiguous now. Therefore, we perform
11568 the lookup again. */
11569 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
11570 qualifying_scope
= tsubst (qualifying_scope
, args
,
11571 complain
, in_decl
);
11572 fns
= BASELINK_FUNCTIONS (baselink
);
11573 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
11574 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
11576 template_id_p
= true;
11577 template_args
= TREE_OPERAND (fns
, 1);
11578 fns
= TREE_OPERAND (fns
, 0);
11580 template_args
= tsubst_template_args (template_args
, args
,
11581 complain
, in_decl
);
11583 name
= DECL_NAME (get_first_fn (fns
));
11584 if (IDENTIFIER_TYPENAME_P (name
))
11585 name
= mangle_conv_op_name_for_type (optype
);
11586 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
11588 return error_mark_node
;
11590 /* If lookup found a single function, mark it as used at this
11591 point. (If it lookup found multiple functions the one selected
11592 later by overload resolution will be marked as used at that
11594 if (BASELINK_P (baselink
))
11595 fns
= BASELINK_FUNCTIONS (baselink
);
11596 if (!template_id_p
&& !really_overloaded_fn (fns
))
11597 mark_used (OVL_CURRENT (fns
));
11599 /* Add back the template arguments, if present. */
11600 if (BASELINK_P (baselink
) && template_id_p
)
11601 BASELINK_FUNCTIONS (baselink
)
11602 = build_nt (TEMPLATE_ID_EXPR
,
11603 BASELINK_FUNCTIONS (baselink
),
11605 /* Update the conversion operator type. */
11606 BASELINK_OPTYPE (baselink
) = optype
;
11609 object_type
= current_class_type
;
11612 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
11618 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
11619 true if the qualified-id will be a postfix-expression in-and-of
11620 itself; false if more of the postfix-expression follows the
11621 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
11625 tsubst_qualified_id (tree qualified_id
, tree args
,
11626 tsubst_flags_t complain
, tree in_decl
,
11627 bool done
, bool address_p
)
11633 tree template_args
;
11634 location_t loc
= UNKNOWN_LOCATION
;
11636 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
11638 /* Figure out what name to look up. */
11639 name
= TREE_OPERAND (qualified_id
, 1);
11640 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
11642 is_template
= true;
11643 loc
= EXPR_LOCATION (name
);
11644 template_args
= TREE_OPERAND (name
, 1);
11646 template_args
= tsubst_template_args (template_args
, args
,
11647 complain
, in_decl
);
11648 name
= TREE_OPERAND (name
, 0);
11652 is_template
= false;
11653 template_args
= NULL_TREE
;
11656 /* Substitute into the qualifying scope. When there are no ARGS, we
11657 are just trying to simplify a non-dependent expression. In that
11658 case the qualifying scope may be dependent, and, in any case,
11659 substituting will not help. */
11660 scope
= TREE_OPERAND (qualified_id
, 0);
11663 scope
= tsubst (scope
, args
, complain
, in_decl
);
11664 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
11669 if (dependent_scope_p (scope
))
11672 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
11673 return build_qualified_name (NULL_TREE
, scope
, expr
,
11674 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
11677 if (!BASELINK_P (name
) && !DECL_P (expr
))
11679 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
11681 /* A BIT_NOT_EXPR is used to represent a destructor. */
11682 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
11684 error ("qualifying type %qT does not match destructor name ~%qT",
11685 scope
, TREE_OPERAND (expr
, 0));
11686 expr
= error_mark_node
;
11689 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
11690 /*is_type_p=*/0, false);
11693 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
11694 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
11695 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
11697 if (complain
& tf_error
)
11699 error ("dependent-name %qE is parsed as a non-type, but "
11700 "instantiation yields a type", qualified_id
);
11701 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
11703 return error_mark_node
;
11709 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
11711 /* Remember that there was a reference to this entity. */
11715 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
11717 if (complain
& tf_error
)
11718 qualified_name_lookup_error (scope
,
11719 TREE_OPERAND (qualified_id
, 1),
11720 expr
, input_location
);
11721 return error_mark_node
;
11725 expr
= lookup_template_function (expr
, template_args
);
11727 if (expr
== error_mark_node
&& complain
& tf_error
)
11728 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
11729 expr
, input_location
);
11730 else if (TYPE_P (scope
))
11732 expr
= (adjust_result_of_qualified_name_lookup
11733 (expr
, scope
, current_class_type
));
11734 expr
= (finish_qualified_id_expr
11735 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
11736 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
11737 /*template_arg_p=*/false));
11740 /* Expressions do not generally have reference type. */
11741 if (TREE_CODE (expr
) != SCOPE_REF
11742 /* However, if we're about to form a pointer-to-member, we just
11743 want the referenced member referenced. */
11744 && TREE_CODE (expr
) != OFFSET_REF
)
11745 expr
= convert_from_reference (expr
);
11750 /* Like tsubst, but deals with expressions. This function just replaces
11751 template parms; to finish processing the resultant expression, use
11752 tsubst_copy_and_build or tsubst_expr. */
11755 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11757 enum tree_code code
;
11760 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
11763 code
= TREE_CODE (t
);
11768 r
= retrieve_local_specialization (t
);
11770 if (r
== NULL_TREE
)
11772 /* We get here for a use of 'this' in an NSDMI. */
11773 if (DECL_NAME (t
) == this_identifier
11774 && at_function_scope_p ()
11775 && DECL_CONSTRUCTOR_P (current_function_decl
))
11776 return current_class_ptr
;
11778 /* This can happen for a parameter name used later in a function
11779 declaration (such as in a late-specified return type). Just
11780 make a dummy decl, since it's only used for its type. */
11781 gcc_assert (cp_unevaluated_operand
!= 0);
11782 r
= tsubst_decl (t
, args
, complain
);
11783 /* Give it the template pattern as its context; its true context
11784 hasn't been instantiated yet and this is good enough for
11786 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
11789 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
11790 r
= ARGUMENT_PACK_SELECT_ARG (r
);
11799 if (DECL_TEMPLATE_PARM_P (t
))
11800 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
11801 /* There is no need to substitute into namespace-scope
11803 if (DECL_NAMESPACE_SCOPE_P (t
))
11805 /* If ARGS is NULL, then T is known to be non-dependent. */
11806 if (args
== NULL_TREE
)
11807 return integral_constant_value (t
);
11809 /* Unfortunately, we cannot just call lookup_name here.
11812 template <int I> int f() {
11814 struct S { void g() { E e = a; } };
11817 When we instantiate f<7>::S::g(), say, lookup_name is not
11818 clever enough to find f<7>::a. */
11820 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
11821 /*entering_scope=*/0);
11823 for (v
= TYPE_VALUES (enum_type
);
11825 v
= TREE_CHAIN (v
))
11826 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
11827 return TREE_VALUE (v
);
11829 /* We didn't find the name. That should never happen; if
11830 name-lookup found it during preliminary parsing, we
11831 should find it again here during instantiation. */
11832 gcc_unreachable ();
11837 if (DECL_CONTEXT (t
))
11841 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
11842 /*entering_scope=*/1);
11843 if (ctx
!= DECL_CONTEXT (t
))
11845 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
11848 if (complain
& tf_error
)
11849 error ("using invalid field %qD", t
);
11850 return error_mark_node
;
11859 case FUNCTION_DECL
:
11860 if ((DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
11861 || local_variable_p (t
))
11862 t
= tsubst (t
, args
, complain
, in_decl
);
11866 case NAMESPACE_DECL
:
11870 /* An OVERLOAD will always be a non-dependent overload set; an
11871 overload set from function scope will just be represented with an
11872 IDENTIFIER_NODE, and from class scope with a BASELINK. */
11873 gcc_assert (!uses_template_parms (t
));
11877 return tsubst_baselink (t
, current_class_type
, args
, complain
, in_decl
);
11879 case TEMPLATE_DECL
:
11880 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11881 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
11882 args
, complain
, in_decl
);
11883 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
11884 return tsubst (t
, args
, complain
, in_decl
);
11885 else if (DECL_CLASS_SCOPE_P (t
)
11886 && uses_template_parms (DECL_CONTEXT (t
)))
11888 /* Template template argument like the following example need
11891 template <template <class> class TT> struct C {};
11892 template <class T> struct D {
11893 template <class U> struct E {};
11898 We are processing the template argument `E' in #1 for
11899 the template instantiation #2. Originally, `E' is a
11900 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
11901 have to substitute this with one having context `D<int>'. */
11903 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
11904 return lookup_field (context
, DECL_NAME(t
), 0, false);
11907 /* Ordinary template template argument. */
11911 case REINTERPRET_CAST_EXPR
:
11912 case CONST_CAST_EXPR
:
11913 case STATIC_CAST_EXPR
:
11914 case DYNAMIC_CAST_EXPR
:
11915 case IMPLICIT_CONV_EXPR
:
11919 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
11920 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
11923 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
11929 ++cp_unevaluated_operand
;
11930 ++c_inhibit_evaluation_warnings
;
11931 /* We only want to compute the number of arguments. */
11932 expanded
= tsubst_pack_expansion (TREE_OPERAND (t
, 0), args
,
11933 complain
, in_decl
);
11934 --cp_unevaluated_operand
;
11935 --c_inhibit_evaluation_warnings
;
11937 if (TREE_CODE (expanded
) == TREE_VEC
)
11938 len
= TREE_VEC_LENGTH (expanded
);
11940 if (expanded
== error_mark_node
)
11941 return error_mark_node
;
11942 else if (PACK_EXPANSION_P (expanded
)
11943 || (TREE_CODE (expanded
) == TREE_VEC
11945 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
11947 if (TREE_CODE (expanded
) == TREE_VEC
)
11948 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
11950 if (TYPE_P (expanded
))
11951 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
11952 complain
& tf_error
);
11954 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
11955 complain
& tf_error
);
11958 return build_int_cst (size_type_node
, len
);
11964 case TRUTH_NOT_EXPR
:
11967 case UNARY_PLUS_EXPR
: /* Unary + */
11969 case AT_ENCODE_EXPR
:
11973 case REALPART_EXPR
:
11974 case IMAGPART_EXPR
:
11976 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
11977 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
11979 case COMPONENT_REF
:
11984 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
11985 name
= TREE_OPERAND (t
, 1);
11986 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
11988 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
11989 complain
, in_decl
);
11990 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
11992 else if (TREE_CODE (name
) == SCOPE_REF
11993 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
11995 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
11996 complain
, in_decl
);
11997 name
= TREE_OPERAND (name
, 1);
11998 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
11999 complain
, in_decl
);
12000 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12001 name
= build_qualified_name (/*type=*/NULL_TREE
,
12003 /*template_p=*/false);
12005 else if (BASELINK_P (name
))
12006 name
= tsubst_baselink (name
,
12007 non_reference (TREE_TYPE (object
)),
12011 name
= tsubst_copy (name
, args
, complain
, in_decl
);
12012 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
12018 case TRUNC_DIV_EXPR
:
12019 case CEIL_DIV_EXPR
:
12020 case FLOOR_DIV_EXPR
:
12021 case ROUND_DIV_EXPR
:
12022 case EXACT_DIV_EXPR
:
12026 case TRUNC_MOD_EXPR
:
12027 case FLOOR_MOD_EXPR
:
12028 case TRUTH_ANDIF_EXPR
:
12029 case TRUTH_ORIF_EXPR
:
12030 case TRUTH_AND_EXPR
:
12031 case TRUTH_OR_EXPR
:
12044 case COMPOUND_EXPR
:
12047 case PREDECREMENT_EXPR
:
12048 case PREINCREMENT_EXPR
:
12049 case POSTDECREMENT_EXPR
:
12050 case POSTINCREMENT_EXPR
:
12052 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12053 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12056 return build_qualified_name (/*type=*/NULL_TREE
,
12057 tsubst_copy (TREE_OPERAND (t
, 0),
12058 args
, complain
, in_decl
),
12059 tsubst_copy (TREE_OPERAND (t
, 1),
12060 args
, complain
, in_decl
),
12061 QUALIFIED_NAME_IS_TEMPLATE (t
));
12066 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12067 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12068 NULL_TREE
, NULL_TREE
);
12072 int n
= VL_EXP_OPERAND_LENGTH (t
);
12073 tree result
= build_vl_exp (CALL_EXPR
, n
);
12075 for (i
= 0; i
< n
; i
++)
12076 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
12077 complain
, in_decl
);
12083 case PSEUDO_DTOR_EXPR
:
12086 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12087 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12088 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12089 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12096 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12097 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12098 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12099 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
12106 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12107 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12108 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
12109 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
12113 case TEMPLATE_ID_EXPR
:
12115 /* Substituted template arguments */
12116 tree fn
= TREE_OPERAND (t
, 0);
12117 tree targs
= TREE_OPERAND (t
, 1);
12119 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
12121 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12123 return lookup_template_function (fn
, targs
);
12128 tree purpose
, value
, chain
;
12130 if (t
== void_list_node
)
12133 purpose
= TREE_PURPOSE (t
);
12135 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
12136 value
= TREE_VALUE (t
);
12138 value
= tsubst_copy (value
, args
, complain
, in_decl
);
12139 chain
= TREE_CHAIN (t
);
12140 if (chain
&& chain
!= void_type_node
)
12141 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
12142 if (purpose
== TREE_PURPOSE (t
)
12143 && value
== TREE_VALUE (t
)
12144 && chain
== TREE_CHAIN (t
))
12146 return tree_cons (purpose
, value
, chain
);
12151 case ENUMERAL_TYPE
:
12153 case TEMPLATE_TYPE_PARM
:
12154 case TEMPLATE_TEMPLATE_PARM
:
12155 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12156 case TEMPLATE_PARM_INDEX
:
12158 case REFERENCE_TYPE
:
12160 case FUNCTION_TYPE
:
12163 case TYPENAME_TYPE
:
12164 case UNBOUND_CLASS_TEMPLATE
:
12166 case DECLTYPE_TYPE
:
12168 return tsubst (t
, args
, complain
, in_decl
);
12170 case IDENTIFIER_NODE
:
12171 if (IDENTIFIER_TYPENAME_P (t
))
12173 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12174 return mangle_conv_op_name_for_type (new_type
);
12180 /* This is handled by tsubst_copy_and_build. */
12181 gcc_unreachable ();
12184 return build_x_va_arg (EXPR_LOCATION (t
),
12185 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
12187 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
12189 case CLEANUP_POINT_EXPR
:
12190 /* We shouldn't have built any of these during initial template
12191 generation. Instead, they should be built during instantiation
12192 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12193 gcc_unreachable ();
12197 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12198 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12199 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12200 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
12201 mark_used (TREE_OPERAND (r
, 1));
12204 case EXPR_PACK_EXPANSION
:
12205 error ("invalid use of pack expansion expression");
12206 return error_mark_node
;
12208 case NONTYPE_ARGUMENT_PACK
:
12209 error ("use %<...%> to expand argument pack");
12210 return error_mark_node
;
12217 /* Instantiate any typedefs in the type. */
12218 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12219 r
= fold_convert (type
, t
);
12220 gcc_assert (TREE_CODE (r
) == code
);
12225 /* These can sometimes show up in a partial instantiation, but never
12226 involve template parms. */
12227 gcc_assert (!uses_template_parms (t
));
12231 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12232 gcc_checking_assert (false);
12237 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12240 tsubst_omp_clauses (tree clauses
, tree args
, tsubst_flags_t complain
,
12243 tree new_clauses
= NULL
, nc
, oc
;
12245 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
12247 nc
= copy_node (oc
);
12248 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
12251 switch (OMP_CLAUSE_CODE (nc
))
12253 case OMP_CLAUSE_LASTPRIVATE
:
12254 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
12256 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
12257 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
12258 in_decl
, /*integral_constant_expression_p=*/false);
12259 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
12260 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
12263 case OMP_CLAUSE_PRIVATE
:
12264 case OMP_CLAUSE_SHARED
:
12265 case OMP_CLAUSE_FIRSTPRIVATE
:
12266 case OMP_CLAUSE_REDUCTION
:
12267 case OMP_CLAUSE_COPYIN
:
12268 case OMP_CLAUSE_COPYPRIVATE
:
12269 case OMP_CLAUSE_IF
:
12270 case OMP_CLAUSE_NUM_THREADS
:
12271 case OMP_CLAUSE_SCHEDULE
:
12272 case OMP_CLAUSE_COLLAPSE
:
12273 case OMP_CLAUSE_FINAL
:
12274 OMP_CLAUSE_OPERAND (nc
, 0)
12275 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12276 in_decl
, /*integral_constant_expression_p=*/false);
12278 case OMP_CLAUSE_NOWAIT
:
12279 case OMP_CLAUSE_ORDERED
:
12280 case OMP_CLAUSE_DEFAULT
:
12281 case OMP_CLAUSE_UNTIED
:
12282 case OMP_CLAUSE_MERGEABLE
:
12285 gcc_unreachable ();
12289 return finish_omp_clauses (nreverse (new_clauses
));
12292 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12295 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
12298 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12300 tree purpose
, value
, chain
;
12305 if (TREE_CODE (t
) != TREE_LIST
)
12306 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12307 /*function_p=*/false,
12308 /*integral_constant_expression_p=*/false);
12310 if (t
== void_list_node
)
12313 purpose
= TREE_PURPOSE (t
);
12315 purpose
= RECUR (purpose
);
12316 value
= TREE_VALUE (t
);
12319 if (TREE_CODE (value
) != LABEL_DECL
)
12320 value
= RECUR (value
);
12323 value
= lookup_label (DECL_NAME (value
));
12324 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
12325 TREE_USED (value
) = 1;
12328 chain
= TREE_CHAIN (t
);
12329 if (chain
&& chain
!= void_type_node
)
12330 chain
= RECUR (chain
);
12331 return tree_cons (purpose
, value
, chain
);
12335 /* Substitute one OMP_FOR iterator. */
12338 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
12339 tree condv
, tree incrv
, tree
*clauses
,
12340 tree args
, tsubst_flags_t complain
, tree in_decl
,
12341 bool integral_constant_expression_p
)
12343 #define RECUR(NODE) \
12344 tsubst_expr ((NODE), args, complain, in_decl, \
12345 integral_constant_expression_p)
12346 tree decl
, init
, cond
, incr
;
12349 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
12350 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
12351 decl
= TREE_OPERAND (init
, 0);
12352 init
= TREE_OPERAND (init
, 1);
12353 /* Do this before substituting into decl to handle 'auto'. */
12354 init_decl
= (init
&& TREE_CODE (init
) == DECL_EXPR
);
12355 init
= RECUR (init
);
12356 decl
= RECUR (decl
);
12359 gcc_assert (!processing_template_decl
);
12360 init
= DECL_INITIAL (decl
);
12361 DECL_INITIAL (decl
) = NULL_TREE
;
12364 gcc_assert (!type_dependent_expression_p (decl
));
12366 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
12368 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
12369 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
12370 if (TREE_CODE (incr
) == MODIFY_EXPR
)
12371 incr
= build_x_modify_expr (EXPR_LOCATION (incr
),
12372 RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
12373 RECUR (TREE_OPERAND (incr
, 1)),
12376 incr
= RECUR (incr
);
12377 TREE_VEC_ELT (declv
, i
) = decl
;
12378 TREE_VEC_ELT (initv
, i
) = init
;
12379 TREE_VEC_ELT (condv
, i
) = cond
;
12380 TREE_VEC_ELT (incrv
, i
) = incr
;
12384 if (init
&& !init_decl
)
12387 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
12389 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
12390 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
12391 && OMP_CLAUSE_DECL (c
) == decl
)
12393 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
12394 && OMP_CLAUSE_DECL (c
) == decl
)
12395 error ("iteration variable %qD should not be firstprivate", decl
);
12396 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
12397 && OMP_CLAUSE_DECL (c
) == decl
)
12398 error ("iteration variable %qD should not be reduction", decl
);
12402 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
12403 OMP_CLAUSE_DECL (c
) = decl
;
12404 c
= finish_omp_clauses (c
);
12407 OMP_CLAUSE_CHAIN (c
) = *clauses
;
12412 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
12413 if (COMPARISON_CLASS_P (cond
))
12414 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
12415 RECUR (TREE_OPERAND (cond
, 0)),
12416 RECUR (TREE_OPERAND (cond
, 1)));
12418 cond
= RECUR (cond
);
12419 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
12420 switch (TREE_CODE (incr
))
12422 case PREINCREMENT_EXPR
:
12423 case PREDECREMENT_EXPR
:
12424 case POSTINCREMENT_EXPR
:
12425 case POSTDECREMENT_EXPR
:
12426 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
12427 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
12430 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
12431 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
12433 tree rhs
= TREE_OPERAND (incr
, 1);
12434 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
12435 RECUR (TREE_OPERAND (incr
, 0)),
12436 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
12437 RECUR (TREE_OPERAND (rhs
, 0)),
12438 RECUR (TREE_OPERAND (rhs
, 1))));
12441 incr
= RECUR (incr
);
12444 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
12445 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
12447 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
12448 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
12449 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
12450 TREE_TYPE (decl
), lhs
,
12451 RECUR (TREE_OPERAND (incr
, 2))));
12453 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
12454 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
12455 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
12457 tree rhs
= TREE_OPERAND (incr
, 2);
12458 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
12459 RECUR (TREE_OPERAND (incr
, 0)),
12460 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
12461 RECUR (TREE_OPERAND (rhs
, 0)),
12462 RECUR (TREE_OPERAND (rhs
, 1))));
12465 incr
= RECUR (incr
);
12468 incr
= RECUR (incr
);
12472 TREE_VEC_ELT (declv
, i
) = decl
;
12473 TREE_VEC_ELT (initv
, i
) = init
;
12474 TREE_VEC_ELT (condv
, i
) = cond
;
12475 TREE_VEC_ELT (incrv
, i
) = incr
;
12479 /* Like tsubst_copy for expressions, etc. but also does semantic
12483 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
12484 bool integral_constant_expression_p
)
12486 #define RECUR(NODE) \
12487 tsubst_expr ((NODE), args, complain, in_decl, \
12488 integral_constant_expression_p)
12492 if (t
== NULL_TREE
|| t
== error_mark_node
)
12495 if (EXPR_HAS_LOCATION (t
))
12496 input_location
= EXPR_LOCATION (t
);
12497 if (STATEMENT_CODE_P (TREE_CODE (t
)))
12498 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
12500 switch (TREE_CODE (t
))
12502 case STATEMENT_LIST
:
12504 tree_stmt_iterator i
;
12505 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
12506 RECUR (tsi_stmt (i
));
12510 case CTOR_INITIALIZER
:
12511 finish_mem_initializers (tsubst_initializer_list
12512 (TREE_OPERAND (t
, 0), args
));
12516 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
12520 tmp
= RECUR (EXPR_STMT_EXPR (t
));
12521 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
12522 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
12524 finish_expr_stmt (tmp
);
12528 do_using_directive (USING_STMT_NAMESPACE (t
));
12533 tree decl
, pattern_decl
;
12536 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
12537 if (TREE_CODE (decl
) == LABEL_DECL
)
12538 finish_label_decl (DECL_NAME (decl
));
12539 else if (TREE_CODE (decl
) == USING_DECL
)
12541 tree scope
= USING_DECL_SCOPE (decl
);
12542 tree name
= DECL_NAME (decl
);
12545 scope
= tsubst (scope
, args
, complain
, in_decl
);
12546 decl
= lookup_qualified_name (scope
, name
,
12547 /*is_type_p=*/false,
12548 /*complain=*/false);
12549 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
12550 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
12552 do_local_using_decl (decl
, scope
, name
);
12556 init
= DECL_INITIAL (decl
);
12557 decl
= tsubst (decl
, args
, complain
, in_decl
);
12558 if (decl
!= error_mark_node
)
12560 /* By marking the declaration as instantiated, we avoid
12561 trying to instantiate it. Since instantiate_decl can't
12562 handle local variables, and since we've already done
12563 all that needs to be done, that's the right thing to
12565 if (TREE_CODE (decl
) == VAR_DECL
)
12566 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
12567 if (TREE_CODE (decl
) == VAR_DECL
12568 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
12569 /* Anonymous aggregates are a special case. */
12570 finish_anon_union (decl
);
12571 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
12573 DECL_CONTEXT (decl
) = current_function_decl
;
12574 insert_capture_proxy (decl
);
12576 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
12577 /* We already did a pushtag. */;
12580 int const_init
= false;
12581 maybe_push_decl (decl
);
12582 if (TREE_CODE (decl
) == VAR_DECL
12583 && DECL_PRETTY_FUNCTION_P (decl
))
12585 /* For __PRETTY_FUNCTION__ we have to adjust the
12587 const char *const name
12588 = cxx_printable_name (current_function_decl
, 2);
12589 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
12593 tree t
= RECUR (init
);
12597 /* If we had an initializer but it
12598 instantiated to nothing,
12599 value-initialize the object. This will
12600 only occur when the initializer was a
12601 pack expansion where the parameter packs
12602 used in that expansion were of length
12604 init
= build_value_init (TREE_TYPE (decl
),
12606 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
12607 init
= get_target_expr_sfinae (init
, complain
);
12613 if (TREE_CODE (decl
) == VAR_DECL
)
12614 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
12616 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
12625 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12626 RECUR (FOR_INIT_STMT (t
));
12627 finish_for_init_stmt (stmt
);
12628 tmp
= RECUR (FOR_COND (t
));
12629 finish_for_cond (tmp
, stmt
);
12630 tmp
= RECUR (FOR_EXPR (t
));
12631 finish_for_expr (tmp
, stmt
);
12632 RECUR (FOR_BODY (t
));
12633 finish_for_stmt (stmt
);
12636 case RANGE_FOR_STMT
:
12639 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12640 decl
= RANGE_FOR_DECL (t
);
12641 decl
= tsubst (decl
, args
, complain
, in_decl
);
12642 maybe_push_decl (decl
);
12643 expr
= RECUR (RANGE_FOR_EXPR (t
));
12644 stmt
= cp_convert_range_for (stmt
, decl
, expr
);
12645 RECUR (RANGE_FOR_BODY (t
));
12646 finish_for_stmt (stmt
);
12651 stmt
= begin_while_stmt ();
12652 tmp
= RECUR (WHILE_COND (t
));
12653 finish_while_stmt_cond (tmp
, stmt
);
12654 RECUR (WHILE_BODY (t
));
12655 finish_while_stmt (stmt
);
12659 stmt
= begin_do_stmt ();
12660 RECUR (DO_BODY (t
));
12661 finish_do_body (stmt
);
12662 tmp
= RECUR (DO_COND (t
));
12663 finish_do_stmt (tmp
, stmt
);
12667 stmt
= begin_if_stmt ();
12668 tmp
= RECUR (IF_COND (t
));
12669 finish_if_stmt_cond (tmp
, stmt
);
12670 RECUR (THEN_CLAUSE (t
));
12671 finish_then_clause (stmt
);
12673 if (ELSE_CLAUSE (t
))
12675 begin_else_clause (stmt
);
12676 RECUR (ELSE_CLAUSE (t
));
12677 finish_else_clause (stmt
);
12680 finish_if_stmt (stmt
);
12684 if (BIND_EXPR_BODY_BLOCK (t
))
12685 stmt
= begin_function_body ();
12687 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
12688 ? BCS_TRY_BLOCK
: 0);
12690 RECUR (BIND_EXPR_BODY (t
));
12692 if (BIND_EXPR_BODY_BLOCK (t
))
12693 finish_function_body (stmt
);
12695 finish_compound_stmt (stmt
);
12699 finish_break_stmt ();
12702 case CONTINUE_STMT
:
12703 finish_continue_stmt ();
12707 stmt
= begin_switch_stmt ();
12708 tmp
= RECUR (SWITCH_STMT_COND (t
));
12709 finish_switch_cond (tmp
, stmt
);
12710 RECUR (SWITCH_STMT_BODY (t
));
12711 finish_switch_stmt (stmt
);
12714 case CASE_LABEL_EXPR
:
12715 finish_case_label (EXPR_LOCATION (t
),
12716 RECUR (CASE_LOW (t
)),
12717 RECUR (CASE_HIGH (t
)));
12722 tree decl
= LABEL_EXPR_LABEL (t
);
12725 label
= finish_label_stmt (DECL_NAME (decl
));
12726 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
12727 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
12732 tmp
= GOTO_DESTINATION (t
);
12733 if (TREE_CODE (tmp
) != LABEL_DECL
)
12734 /* Computed goto's must be tsubst'd into. On the other hand,
12735 non-computed gotos must not be; the identifier in question
12736 will have no binding. */
12739 tmp
= DECL_NAME (tmp
);
12740 finish_goto_stmt (tmp
);
12744 tmp
= finish_asm_stmt
12745 (ASM_VOLATILE_P (t
),
12746 RECUR (ASM_STRING (t
)),
12747 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
12748 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
12749 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
12750 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
12752 tree asm_expr
= tmp
;
12753 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
12754 asm_expr
= TREE_OPERAND (asm_expr
, 0);
12755 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
12762 stmt
= begin_try_block ();
12763 RECUR (TRY_STMTS (t
));
12764 finish_cleanup_try_block (stmt
);
12765 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
12769 tree compound_stmt
= NULL_TREE
;
12771 if (FN_TRY_BLOCK_P (t
))
12772 stmt
= begin_function_try_block (&compound_stmt
);
12774 stmt
= begin_try_block ();
12776 RECUR (TRY_STMTS (t
));
12778 if (FN_TRY_BLOCK_P (t
))
12779 finish_function_try_block (stmt
);
12781 finish_try_block (stmt
);
12783 RECUR (TRY_HANDLERS (t
));
12784 if (FN_TRY_BLOCK_P (t
))
12785 finish_function_handler_sequence (stmt
, compound_stmt
);
12787 finish_handler_sequence (stmt
);
12793 tree decl
= HANDLER_PARMS (t
);
12797 decl
= tsubst (decl
, args
, complain
, in_decl
);
12798 /* Prevent instantiate_decl from trying to instantiate
12799 this variable. We've already done all that needs to be
12801 if (decl
!= error_mark_node
)
12802 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
12804 stmt
= begin_handler ();
12805 finish_handler_parms (decl
, stmt
);
12806 RECUR (HANDLER_BODY (t
));
12807 finish_handler (stmt
);
12812 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
12813 if (CLASS_TYPE_P (tmp
))
12815 /* Local classes are not independent templates; they are
12816 instantiated along with their containing function. And this
12817 way we don't have to deal with pushing out of one local class
12818 to instantiate a member of another local class. */
12820 /* Closures are handled by the LAMBDA_EXPR. */
12821 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
12822 complete_type (tmp
);
12823 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
12824 if (!DECL_ARTIFICIAL (fn
))
12825 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
12829 case STATIC_ASSERT
:
12833 ++c_inhibit_evaluation_warnings
;
12835 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
12838 /*integral_constant_expression_p=*/true);
12839 --c_inhibit_evaluation_warnings
;
12841 finish_static_assert (condition
,
12842 STATIC_ASSERT_MESSAGE (t
),
12843 STATIC_ASSERT_SOURCE_LOCATION (t
),
12844 /*member_p=*/false);
12849 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
),
12850 args
, complain
, in_decl
);
12851 stmt
= begin_omp_parallel ();
12852 RECUR (OMP_PARALLEL_BODY (t
));
12853 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
12854 = OMP_PARALLEL_COMBINED (t
);
12858 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
),
12859 args
, complain
, in_decl
);
12860 stmt
= begin_omp_task ();
12861 RECUR (OMP_TASK_BODY (t
));
12862 finish_omp_task (tmp
, stmt
);
12867 tree clauses
, body
, pre_body
;
12868 tree declv
, initv
, condv
, incrv
;
12871 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
),
12872 args
, complain
, in_decl
);
12873 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
12874 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
12875 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
12876 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
12878 stmt
= begin_omp_structured_block ();
12880 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
12881 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
12882 &clauses
, args
, complain
, in_decl
,
12883 integral_constant_expression_p
);
12885 pre_body
= push_stmt_list ();
12886 RECUR (OMP_FOR_PRE_BODY (t
));
12887 pre_body
= pop_stmt_list (pre_body
);
12889 body
= push_stmt_list ();
12890 RECUR (OMP_FOR_BODY (t
));
12891 body
= pop_stmt_list (body
);
12893 t
= finish_omp_for (EXPR_LOCATION (t
), declv
, initv
, condv
, incrv
,
12894 body
, pre_body
, clauses
);
12896 add_stmt (finish_omp_structured_block (stmt
));
12902 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), args
, complain
, in_decl
);
12903 stmt
= push_stmt_list ();
12904 RECUR (OMP_BODY (t
));
12905 stmt
= pop_stmt_list (stmt
);
12908 OMP_BODY (t
) = stmt
;
12909 OMP_CLAUSES (t
) = tmp
;
12917 stmt
= push_stmt_list ();
12918 RECUR (OMP_BODY (t
));
12919 stmt
= pop_stmt_list (stmt
);
12922 OMP_BODY (t
) = stmt
;
12927 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
12928 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
12930 tree op1
= TREE_OPERAND (t
, 1);
12931 tree rhs1
= NULL_TREE
;
12933 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
12935 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
12936 op1
= TREE_OPERAND (op1
, 1);
12938 lhs
= RECUR (TREE_OPERAND (op1
, 0));
12939 rhs
= RECUR (TREE_OPERAND (op1
, 1));
12940 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
12941 NULL_TREE
, NULL_TREE
, rhs1
);
12945 tree op1
= TREE_OPERAND (t
, 1);
12946 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
12947 tree rhs1
= NULL_TREE
;
12948 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
12949 enum tree_code opcode
= NOP_EXPR
;
12950 if (code
== OMP_ATOMIC_READ
)
12952 v
= RECUR (TREE_OPERAND (op1
, 0));
12953 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
12955 else if (code
== OMP_ATOMIC_CAPTURE_OLD
12956 || code
== OMP_ATOMIC_CAPTURE_NEW
)
12958 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
12959 v
= RECUR (TREE_OPERAND (op1
, 0));
12960 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
12961 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
12963 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
12964 op11
= TREE_OPERAND (op11
, 1);
12966 lhs
= RECUR (TREE_OPERAND (op11
, 0));
12967 rhs
= RECUR (TREE_OPERAND (op11
, 1));
12968 opcode
= TREE_CODE (op11
);
12973 lhs
= RECUR (TREE_OPERAND (op1
, 0));
12974 rhs
= RECUR (TREE_OPERAND (op1
, 1));
12976 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
);
12980 case TRANSACTION_EXPR
:
12983 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
12984 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
12986 if (TRANSACTION_EXPR_IS_STMT (t
))
12988 tree body
= TRANSACTION_EXPR_BODY (t
);
12989 tree noex
= NULL_TREE
;
12990 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
12992 noex
= MUST_NOT_THROW_COND (body
);
12993 if (noex
== NULL_TREE
)
12994 noex
= boolean_true_node
;
12995 body
= TREE_OPERAND (body
, 0);
12997 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
12999 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
13003 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
13004 RECUR (TRANSACTION_EXPR_BODY (t
)),
13011 case MUST_NOT_THROW_EXPR
:
13012 return build_must_not_throw_expr (RECUR (TREE_OPERAND (t
, 0)),
13013 RECUR (MUST_NOT_THROW_COND (t
)));
13015 case EXPR_PACK_EXPANSION
:
13016 error ("invalid use of pack expansion expression");
13017 return error_mark_node
;
13019 case NONTYPE_ARGUMENT_PACK
:
13020 error ("use %<...%> to expand argument pack");
13021 return error_mark_node
;
13023 case COMPOUND_EXPR
:
13024 tmp
= RECUR (TREE_OPERAND (t
, 0));
13025 if (tmp
== NULL_TREE
)
13026 /* If the first operand was a statement, we're done with it. */
13027 return RECUR (TREE_OPERAND (t
, 1));
13028 return build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
13029 RECUR (TREE_OPERAND (t
, 1)),
13033 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
13035 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13036 /*function_p=*/false,
13037 integral_constant_expression_p
);
13044 /* T is a postfix-expression that is not being used in a function
13045 call. Return the substituted version of T. */
13048 tsubst_non_call_postfix_expression (tree t
, tree args
,
13049 tsubst_flags_t complain
,
13052 if (TREE_CODE (t
) == SCOPE_REF
)
13053 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
13054 /*done=*/false, /*address_p=*/false);
13056 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13057 /*function_p=*/false,
13058 /*integral_constant_expression_p=*/false);
13063 /* Like tsubst but deals with expressions and performs semantic
13064 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13067 tsubst_copy_and_build (tree t
,
13069 tsubst_flags_t complain
,
13072 bool integral_constant_expression_p
)
13074 #define RECUR(NODE) \
13075 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13076 /*function_p=*/false, \
13077 integral_constant_expression_p)
13081 if (t
== NULL_TREE
|| t
== error_mark_node
)
13084 switch (TREE_CODE (t
))
13088 /* Fall through. */
13089 case IDENTIFIER_NODE
:
13093 bool non_integral_constant_expression_p
;
13094 const char *error_msg
;
13096 if (IDENTIFIER_TYPENAME_P (t
))
13098 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13099 t
= mangle_conv_op_name_for_type (new_type
);
13102 /* Look up the name. */
13103 decl
= lookup_name (t
);
13105 /* By convention, expressions use ERROR_MARK_NODE to indicate
13106 failure, not NULL_TREE. */
13107 if (decl
== NULL_TREE
)
13108 decl
= error_mark_node
;
13110 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
13112 integral_constant_expression_p
,
13113 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx0x
),
13114 &non_integral_constant_expression_p
,
13115 /*template_p=*/false,
13117 /*address_p=*/false,
13118 /*template_arg_p=*/false,
13123 if (!function_p
&& TREE_CODE (decl
) == IDENTIFIER_NODE
)
13125 if (complain
& tf_error
)
13126 unqualified_name_lookup_error (decl
);
13127 decl
= error_mark_node
;
13132 case TEMPLATE_ID_EXPR
:
13135 tree templ
= RECUR (TREE_OPERAND (t
, 0));
13136 tree targs
= TREE_OPERAND (t
, 1);
13139 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
13141 if (TREE_CODE (templ
) == COMPONENT_REF
)
13143 object
= TREE_OPERAND (templ
, 0);
13144 templ
= TREE_OPERAND (templ
, 1);
13147 object
= NULL_TREE
;
13148 templ
= lookup_template_function (templ
, targs
);
13151 return build3 (COMPONENT_REF
, TREE_TYPE (templ
),
13152 object
, templ
, NULL_TREE
);
13154 return baselink_for_fns (templ
);
13159 tree r
= RECUR (TREE_OPERAND (t
, 0));
13161 if (REFERENCE_REF_P (t
))
13163 /* A type conversion to reference type will be enclosed in
13164 such an indirect ref, but the substitution of the cast
13165 will have also added such an indirect ref. */
13166 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
13167 r
= convert_from_reference (r
);
13170 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
, complain
);
13176 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13177 RECUR (TREE_OPERAND (t
, 0)));
13179 case IMPLICIT_CONV_EXPR
:
13181 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13182 tree expr
= RECUR (TREE_OPERAND (t
, 0));
13183 int flags
= LOOKUP_IMPLICIT
;
13184 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
13185 flags
= LOOKUP_NORMAL
;
13186 return perform_implicit_conversion_flags (type
, expr
, complain
,
13193 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13194 RECUR (TREE_OPERAND (t
, 0)));
13197 case REINTERPRET_CAST_EXPR
:
13198 case CONST_CAST_EXPR
:
13199 case DYNAMIC_CAST_EXPR
:
13200 case STATIC_CAST_EXPR
:
13203 tree op
, r
= NULL_TREE
;
13205 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13206 if (integral_constant_expression_p
13207 && !cast_valid_in_integral_constant_expression_p (type
))
13209 if (complain
& tf_error
)
13210 error ("a cast to a type other than an integral or "
13211 "enumeration type cannot appear in a constant-expression");
13212 return error_mark_node
;
13215 op
= RECUR (TREE_OPERAND (t
, 0));
13217 ++c_inhibit_evaluation_warnings
;
13218 switch (TREE_CODE (t
))
13221 r
= build_functional_cast (type
, op
, complain
);
13223 case REINTERPRET_CAST_EXPR
:
13224 r
= build_reinterpret_cast (type
, op
, complain
);
13226 case CONST_CAST_EXPR
:
13227 r
= build_const_cast (type
, op
, complain
);
13229 case DYNAMIC_CAST_EXPR
:
13230 r
= build_dynamic_cast (type
, op
, complain
);
13232 case STATIC_CAST_EXPR
:
13233 r
= build_static_cast (type
, op
, complain
);
13236 gcc_unreachable ();
13238 --c_inhibit_evaluation_warnings
;
13243 case POSTDECREMENT_EXPR
:
13244 case POSTINCREMENT_EXPR
:
13245 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13246 args
, complain
, in_decl
);
13247 return build_x_unary_op (input_location
, TREE_CODE (t
), op1
, complain
);
13249 case PREDECREMENT_EXPR
:
13250 case PREINCREMENT_EXPR
:
13254 case TRUTH_NOT_EXPR
:
13255 case UNARY_PLUS_EXPR
: /* Unary + */
13256 case REALPART_EXPR
:
13257 case IMAGPART_EXPR
:
13258 return build_x_unary_op (input_location
, TREE_CODE (t
),
13259 RECUR (TREE_OPERAND (t
, 0)), complain
);
13261 case FIX_TRUNC_EXPR
:
13262 return cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
13266 op1
= TREE_OPERAND (t
, 0);
13267 if (TREE_CODE (op1
) == LABEL_DECL
)
13268 return finish_label_address_expr (DECL_NAME (op1
),
13269 EXPR_LOCATION (op1
));
13270 if (TREE_CODE (op1
) == SCOPE_REF
)
13271 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
13272 /*done=*/true, /*address_p=*/true);
13274 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
13276 return build_x_unary_op (input_location
, ADDR_EXPR
, op1
, complain
);
13281 case TRUNC_DIV_EXPR
:
13282 case CEIL_DIV_EXPR
:
13283 case FLOOR_DIV_EXPR
:
13284 case ROUND_DIV_EXPR
:
13285 case EXACT_DIV_EXPR
:
13289 case TRUNC_MOD_EXPR
:
13290 case FLOOR_MOD_EXPR
:
13291 case TRUTH_ANDIF_EXPR
:
13292 case TRUTH_ORIF_EXPR
:
13293 case TRUTH_AND_EXPR
:
13294 case TRUTH_OR_EXPR
:
13312 ++c_inhibit_evaluation_warnings
;
13314 r
= build_x_binary_op
13315 (input_location
, TREE_CODE (t
),
13316 RECUR (TREE_OPERAND (t
, 0)),
13317 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
13319 : TREE_CODE (TREE_OPERAND (t
, 0))),
13320 RECUR (TREE_OPERAND (t
, 1)),
13321 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
13323 : TREE_CODE (TREE_OPERAND (t
, 1))),
13326 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
13327 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13329 --c_inhibit_evaluation_warnings
;
13335 return tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
13336 /*address_p=*/false);
13338 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13339 args
, complain
, in_decl
);
13340 return build_x_array_ref (EXPR_LOCATION (t
), op1
,
13341 RECUR (TREE_OPERAND (t
, 1)), complain
);
13344 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
13345 return tsubst_copy (t
, args
, complain
, in_decl
);
13349 op1
= TREE_OPERAND (t
, 0);
13352 /* When there are no ARGS, we are trying to evaluate a
13353 non-dependent expression from the parser. Trying to do
13354 the substitutions may not work. */
13356 op1
= TREE_TYPE (op1
);
13360 ++cp_unevaluated_operand
;
13361 ++c_inhibit_evaluation_warnings
;
13362 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13363 /*function_p=*/false,
13364 /*integral_constant_expression_p=*/false);
13365 --cp_unevaluated_operand
;
13366 --c_inhibit_evaluation_warnings
;
13369 return cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
13370 complain
& tf_error
);
13372 return cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
13373 complain
& tf_error
);
13375 case AT_ENCODE_EXPR
:
13377 op1
= TREE_OPERAND (t
, 0);
13378 ++cp_unevaluated_operand
;
13379 ++c_inhibit_evaluation_warnings
;
13380 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13381 /*function_p=*/false,
13382 /*integral_constant_expression_p=*/false);
13383 --cp_unevaluated_operand
;
13384 --c_inhibit_evaluation_warnings
;
13385 return objc_build_encode_expr (op1
);
13388 case NOEXCEPT_EXPR
:
13389 op1
= TREE_OPERAND (t
, 0);
13390 ++cp_unevaluated_operand
;
13391 ++c_inhibit_evaluation_warnings
;
13392 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13393 /*function_p=*/false,
13394 /*integral_constant_expression_p=*/false);
13395 --cp_unevaluated_operand
;
13396 --c_inhibit_evaluation_warnings
;
13397 return finish_noexcept_expr (op1
, complain
);
13401 tree r
= build_x_modify_expr
13402 (EXPR_LOCATION (t
),
13403 RECUR (TREE_OPERAND (t
, 0)),
13404 TREE_CODE (TREE_OPERAND (t
, 1)),
13405 RECUR (TREE_OPERAND (t
, 2)),
13407 /* TREE_NO_WARNING must be set if either the expression was
13408 parenthesized or it uses an operator such as >>= rather
13409 than plain assignment. In the former case, it was already
13410 set and must be copied. In the latter case,
13411 build_x_modify_expr sets it and it must not be reset
13413 if (TREE_NO_WARNING (t
))
13414 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13419 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13420 args
, complain
, in_decl
);
13421 /* Remember that there was a reference to this entity. */
13424 return build_x_arrow (input_location
, op1
, complain
);
13428 tree placement
= RECUR (TREE_OPERAND (t
, 0));
13429 tree init
= RECUR (TREE_OPERAND (t
, 3));
13430 VEC(tree
,gc
) *placement_vec
;
13431 VEC(tree
,gc
) *init_vec
;
13434 if (placement
== NULL_TREE
)
13435 placement_vec
= NULL
;
13438 placement_vec
= make_tree_vector ();
13439 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
13440 VEC_safe_push (tree
, gc
, placement_vec
, TREE_VALUE (placement
));
13443 /* If there was an initializer in the original tree, but it
13444 instantiated to an empty list, then we should pass a
13445 non-NULL empty vector to tell build_new that it was an
13446 empty initializer() rather than no initializer. This can
13447 only happen when the initializer is a pack expansion whose
13448 parameter packs are of length zero. */
13449 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
13453 init_vec
= make_tree_vector ();
13454 if (init
== void_zero_node
)
13455 gcc_assert (init_vec
!= NULL
);
13458 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
13459 VEC_safe_push (tree
, gc
, init_vec
, TREE_VALUE (init
));
13463 ret
= build_new (&placement_vec
,
13464 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
13465 RECUR (TREE_OPERAND (t
, 2)),
13467 NEW_EXPR_USE_GLOBAL (t
),
13470 if (placement_vec
!= NULL
)
13471 release_tree_vector (placement_vec
);
13472 if (init_vec
!= NULL
)
13473 release_tree_vector (init_vec
);
13479 return delete_sanity
13480 (RECUR (TREE_OPERAND (t
, 0)),
13481 RECUR (TREE_OPERAND (t
, 1)),
13482 DELETE_EXPR_USE_VEC (t
),
13483 DELETE_EXPR_USE_GLOBAL (t
),
13486 case COMPOUND_EXPR
:
13487 return build_x_compound_expr (EXPR_LOCATION (t
),
13488 RECUR (TREE_OPERAND (t
, 0)),
13489 RECUR (TREE_OPERAND (t
, 1)),
13495 VEC(tree
,gc
) *call_args
;
13496 unsigned int nargs
, i
;
13501 function
= CALL_EXPR_FN (t
);
13502 /* When we parsed the expression, we determined whether or
13503 not Koenig lookup should be performed. */
13504 koenig_p
= KOENIG_LOOKUP_P (t
);
13505 if (TREE_CODE (function
) == SCOPE_REF
)
13507 qualified_p
= true;
13508 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
13510 /*address_p=*/false);
13512 else if (koenig_p
&& TREE_CODE (function
) == IDENTIFIER_NODE
)
13514 /* Do nothing; calling tsubst_copy_and_build on an identifier
13515 would incorrectly perform unqualified lookup again.
13517 Note that we can also have an IDENTIFIER_NODE if the earlier
13518 unqualified lookup found a member function; in that case
13519 koenig_p will be false and we do want to do the lookup
13520 again to find the instantiated member function.
13522 FIXME but doing that causes c++/15272, so we need to stop
13523 using IDENTIFIER_NODE in that situation. */
13524 qualified_p
= false;
13528 if (TREE_CODE (function
) == COMPONENT_REF
)
13530 tree op
= TREE_OPERAND (function
, 1);
13532 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
13533 || (BASELINK_P (op
)
13534 && BASELINK_QUALIFIED_P (op
)));
13537 qualified_p
= false;
13539 function
= tsubst_copy_and_build (function
, args
, complain
,
13542 integral_constant_expression_p
);
13544 if (BASELINK_P (function
))
13545 qualified_p
= true;
13548 nargs
= call_expr_nargs (t
);
13549 call_args
= make_tree_vector ();
13550 for (i
= 0; i
< nargs
; ++i
)
13552 tree arg
= CALL_EXPR_ARG (t
, i
);
13554 if (!PACK_EXPANSION_P (arg
))
13555 VEC_safe_push (tree
, gc
, call_args
,
13556 RECUR (CALL_EXPR_ARG (t
, i
)));
13559 /* Expand the pack expansion and push each entry onto
13561 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
13562 if (TREE_CODE (arg
) == TREE_VEC
)
13564 unsigned int len
, j
;
13566 len
= TREE_VEC_LENGTH (arg
);
13567 for (j
= 0; j
< len
; ++j
)
13569 tree value
= TREE_VEC_ELT (arg
, j
);
13570 if (value
!= NULL_TREE
)
13571 value
= convert_from_reference (value
);
13572 VEC_safe_push (tree
, gc
, call_args
, value
);
13577 /* A partial substitution. Add one entry. */
13578 VEC_safe_push (tree
, gc
, call_args
, arg
);
13583 /* We do not perform argument-dependent lookup if normal
13584 lookup finds a non-function, in accordance with the
13585 expected resolution of DR 218. */
13587 && ((is_overloaded_fn (function
)
13588 /* If lookup found a member function, the Koenig lookup is
13589 not appropriate, even if an unqualified-name was used
13590 to denote the function. */
13591 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
13592 || TREE_CODE (function
) == IDENTIFIER_NODE
)
13593 /* Only do this when substitution turns a dependent call
13594 into a non-dependent call. */
13595 && type_dependent_expression_p_push (t
)
13596 && !any_type_dependent_arguments_p (call_args
))
13597 function
= perform_koenig_lookup (function
, call_args
, false,
13600 if (TREE_CODE (function
) == IDENTIFIER_NODE
13601 && !any_type_dependent_arguments_p (call_args
))
13603 if (koenig_p
&& (complain
& tf_warning_or_error
))
13605 /* For backwards compatibility and good diagnostics, try
13606 the unqualified lookup again if we aren't in SFINAE
13608 tree unq
= (tsubst_copy_and_build
13609 (function
, args
, complain
, in_decl
, true,
13610 integral_constant_expression_p
));
13611 if (unq
== error_mark_node
)
13612 return error_mark_node
;
13614 if (unq
!= function
)
13617 if (TREE_CODE (fn
) == INDIRECT_REF
)
13618 fn
= TREE_OPERAND (fn
, 0);
13619 if (TREE_CODE (fn
) == COMPONENT_REF
)
13620 fn
= TREE_OPERAND (fn
, 1);
13621 if (is_overloaded_fn (fn
))
13622 fn
= get_first_fn (fn
);
13623 permerror (EXPR_LOC_OR_HERE (t
),
13624 "%qD was not declared in this scope, "
13625 "and no declarations were found by "
13626 "argument-dependent lookup at the point "
13627 "of instantiation", function
);
13629 /* Can't say anything more. */;
13630 else if (DECL_CLASS_SCOPE_P (fn
))
13632 inform (EXPR_LOC_OR_HERE (t
),
13633 "declarations in dependent base %qT are "
13634 "not found by unqualified lookup",
13635 DECL_CLASS_CONTEXT (fn
));
13636 if (current_class_ptr
)
13637 inform (EXPR_LOC_OR_HERE (t
),
13638 "use %<this->%D%> instead", function
);
13640 inform (EXPR_LOC_OR_HERE (t
),
13641 "use %<%T::%D%> instead",
13642 current_class_name
, function
);
13645 inform (0, "%q+D declared here, later in the "
13646 "translation unit", fn
);
13650 if (TREE_CODE (function
) == IDENTIFIER_NODE
)
13652 unqualified_name_lookup_error (function
);
13653 release_tree_vector (call_args
);
13654 return error_mark_node
;
13658 /* Remember that there was a reference to this entity. */
13659 if (DECL_P (function
))
13660 mark_used (function
);
13662 if (TREE_CODE (function
) == OFFSET_REF
)
13663 ret
= build_offset_ref_call_from_tree (function
, &call_args
);
13664 else if (TREE_CODE (function
) == COMPONENT_REF
)
13666 tree instance
= TREE_OPERAND (function
, 0);
13667 tree fn
= TREE_OPERAND (function
, 1);
13669 if (processing_template_decl
13670 && (type_dependent_expression_p (instance
)
13671 || (!BASELINK_P (fn
)
13672 && TREE_CODE (fn
) != FIELD_DECL
)
13673 || type_dependent_expression_p (fn
)
13674 || any_type_dependent_arguments_p (call_args
)))
13675 ret
= build_nt_call_vec (function
, call_args
);
13676 else if (!BASELINK_P (fn
))
13677 ret
= finish_call_expr (function
, &call_args
,
13678 /*disallow_virtual=*/false,
13679 /*koenig_p=*/false,
13682 ret
= (build_new_method_call
13684 &call_args
, NULL_TREE
,
13685 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
13690 ret
= finish_call_expr (function
, &call_args
,
13691 /*disallow_virtual=*/qualified_p
,
13695 release_tree_vector (call_args
);
13702 tree cond
= RECUR (TREE_OPERAND (t
, 0));
13705 if (TREE_CODE (cond
) == INTEGER_CST
)
13707 if (integer_zerop (cond
))
13709 ++c_inhibit_evaluation_warnings
;
13710 exp1
= RECUR (TREE_OPERAND (t
, 1));
13711 --c_inhibit_evaluation_warnings
;
13712 exp2
= RECUR (TREE_OPERAND (t
, 2));
13716 exp1
= RECUR (TREE_OPERAND (t
, 1));
13717 ++c_inhibit_evaluation_warnings
;
13718 exp2
= RECUR (TREE_OPERAND (t
, 2));
13719 --c_inhibit_evaluation_warnings
;
13724 exp1
= RECUR (TREE_OPERAND (t
, 1));
13725 exp2
= RECUR (TREE_OPERAND (t
, 2));
13728 return build_x_conditional_expr (EXPR_LOCATION (t
),
13729 cond
, exp1
, exp2
, complain
);
13732 case PSEUDO_DTOR_EXPR
:
13733 return finish_pseudo_destructor_expr
13734 (RECUR (TREE_OPERAND (t
, 0)),
13735 RECUR (TREE_OPERAND (t
, 1)),
13736 tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
13740 tree purpose
, value
, chain
;
13742 if (t
== void_list_node
)
13745 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
13746 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
13748 /* We have pack expansions, so expand those and
13749 create a new list out of it. */
13750 tree purposevec
= NULL_TREE
;
13751 tree valuevec
= NULL_TREE
;
13755 /* Expand the argument expressions. */
13756 if (TREE_PURPOSE (t
))
13757 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
13758 complain
, in_decl
);
13759 if (TREE_VALUE (t
))
13760 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
13761 complain
, in_decl
);
13763 /* Build the rest of the list. */
13764 chain
= TREE_CHAIN (t
);
13765 if (chain
&& chain
!= void_type_node
)
13766 chain
= RECUR (chain
);
13768 /* Determine the number of arguments. */
13769 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
13771 len
= TREE_VEC_LENGTH (purposevec
);
13772 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
13774 else if (TREE_CODE (valuevec
) == TREE_VEC
)
13775 len
= TREE_VEC_LENGTH (valuevec
);
13778 /* Since we only performed a partial substitution into
13779 the argument pack, we only return a single list
13781 if (purposevec
== TREE_PURPOSE (t
)
13782 && valuevec
== TREE_VALUE (t
)
13783 && chain
== TREE_CHAIN (t
))
13786 return tree_cons (purposevec
, valuevec
, chain
);
13789 /* Convert the argument vectors into a TREE_LIST */
13793 /* Grab the Ith values. */
13795 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
13798 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
13801 /* Build the list (backwards). */
13802 chain
= tree_cons (purpose
, value
, chain
);
13808 purpose
= TREE_PURPOSE (t
);
13810 purpose
= RECUR (purpose
);
13811 value
= TREE_VALUE (t
);
13813 value
= RECUR (value
);
13814 chain
= TREE_CHAIN (t
);
13815 if (chain
&& chain
!= void_type_node
)
13816 chain
= RECUR (chain
);
13817 if (purpose
== TREE_PURPOSE (t
)
13818 && value
== TREE_VALUE (t
)
13819 && chain
== TREE_CHAIN (t
))
13821 return tree_cons (purpose
, value
, chain
);
13824 case COMPONENT_REF
:
13830 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13831 args
, complain
, in_decl
);
13832 /* Remember that there was a reference to this entity. */
13833 if (DECL_P (object
))
13834 mark_used (object
);
13835 object_type
= TREE_TYPE (object
);
13837 member
= TREE_OPERAND (t
, 1);
13838 if (BASELINK_P (member
))
13839 member
= tsubst_baselink (member
,
13840 non_reference (TREE_TYPE (object
)),
13841 args
, complain
, in_decl
);
13843 member
= tsubst_copy (member
, args
, complain
, in_decl
);
13844 if (member
== error_mark_node
)
13845 return error_mark_node
;
13847 if (type_dependent_expression_p (object
))
13848 /* We can't do much here. */;
13849 else if (!CLASS_TYPE_P (object_type
))
13851 if (SCALAR_TYPE_P (object_type
))
13853 tree s
= NULL_TREE
;
13854 tree dtor
= member
;
13856 if (TREE_CODE (dtor
) == SCOPE_REF
)
13858 s
= TREE_OPERAND (dtor
, 0);
13859 dtor
= TREE_OPERAND (dtor
, 1);
13861 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
13863 dtor
= TREE_OPERAND (dtor
, 0);
13865 return finish_pseudo_destructor_expr (object
, s
, dtor
);
13869 else if (TREE_CODE (member
) == SCOPE_REF
13870 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
13872 /* Lookup the template functions now that we know what the
13874 tree scope
= TREE_OPERAND (member
, 0);
13875 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
13876 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
13877 member
= lookup_qualified_name (scope
, tmpl
,
13878 /*is_type_p=*/false,
13879 /*complain=*/false);
13880 if (BASELINK_P (member
))
13882 BASELINK_FUNCTIONS (member
)
13883 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
13885 member
= (adjust_result_of_qualified_name_lookup
13886 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
13891 qualified_name_lookup_error (scope
, tmpl
, member
,
13893 return error_mark_node
;
13896 else if (TREE_CODE (member
) == SCOPE_REF
13897 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
13898 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
13900 if (complain
& tf_error
)
13902 if (TYPE_P (TREE_OPERAND (member
, 0)))
13903 error ("%qT is not a class or namespace",
13904 TREE_OPERAND (member
, 0));
13906 error ("%qD is not a class or namespace",
13907 TREE_OPERAND (member
, 0));
13909 return error_mark_node
;
13911 else if (TREE_CODE (member
) == FIELD_DECL
)
13912 return finish_non_static_data_member (member
, object
, NULL_TREE
);
13914 return finish_class_member_access_expr (object
, member
,
13915 /*template_p=*/false,
13921 (RECUR (TREE_OPERAND (t
, 0)));
13925 VEC(constructor_elt
,gc
) *n
;
13926 constructor_elt
*ce
;
13927 unsigned HOST_WIDE_INT idx
;
13928 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13929 bool process_index_p
;
13931 bool need_copy_p
= false;
13934 if (type
== error_mark_node
)
13935 return error_mark_node
;
13937 /* digest_init will do the wrong thing if we let it. */
13938 if (type
&& TYPE_PTRMEMFUNC_P (type
))
13941 /* We do not want to process the index of aggregate
13942 initializers as they are identifier nodes which will be
13943 looked up by digest_init. */
13944 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
13946 n
= VEC_copy (constructor_elt
, gc
, CONSTRUCTOR_ELTS (t
));
13947 newlen
= VEC_length (constructor_elt
, n
);
13948 FOR_EACH_VEC_ELT (constructor_elt
, n
, idx
, ce
)
13950 if (ce
->index
&& process_index_p
)
13951 ce
->index
= RECUR (ce
->index
);
13953 if (PACK_EXPANSION_P (ce
->value
))
13955 /* Substitute into the pack expansion. */
13956 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
13959 if (ce
->value
== error_mark_node
13960 || PACK_EXPANSION_P (ce
->value
))
13962 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
13963 /* Just move the argument into place. */
13964 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
13967 /* Update the length of the final CONSTRUCTOR
13968 arguments vector, and note that we will need to
13970 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
13971 need_copy_p
= true;
13975 ce
->value
= RECUR (ce
->value
);
13980 VEC(constructor_elt
,gc
) *old_n
= n
;
13982 n
= VEC_alloc (constructor_elt
, gc
, newlen
);
13983 FOR_EACH_VEC_ELT (constructor_elt
, old_n
, idx
, ce
)
13985 if (TREE_CODE (ce
->value
) == TREE_VEC
)
13987 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
13988 for (i
= 0; i
< len
; ++i
)
13989 CONSTRUCTOR_APPEND_ELT (n
, 0,
13990 TREE_VEC_ELT (ce
->value
, i
));
13993 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
13997 r
= build_constructor (init_list_type_node
, n
);
13998 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
14000 if (TREE_HAS_CONSTRUCTOR (t
))
14001 return finish_compound_literal (type
, r
, complain
);
14003 TREE_TYPE (r
) = type
;
14009 tree operand_0
= TREE_OPERAND (t
, 0);
14010 if (TYPE_P (operand_0
))
14012 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
14013 return get_typeid (operand_0
);
14017 operand_0
= RECUR (operand_0
);
14018 return build_typeid (operand_0
);
14029 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
14031 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
14032 /* If the original type was a reference, we'll be wrapped in
14033 the appropriate INDIRECT_REF. */
14034 r
= convert_from_reference (r
);
14039 return build_x_va_arg (EXPR_LOCATION (t
),
14040 RECUR (TREE_OPERAND (t
, 0)),
14041 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
14043 case OFFSETOF_EXPR
:
14044 return finish_offsetof (RECUR (TREE_OPERAND (t
, 0)));
14048 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
14049 complain
, in_decl
);
14051 tree type2
= TRAIT_EXPR_TYPE2 (t
);
14053 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
14055 return finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
);
14060 tree old_stmt_expr
= cur_stmt_expr
;
14061 tree stmt_expr
= begin_stmt_expr ();
14063 cur_stmt_expr
= stmt_expr
;
14064 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
14065 integral_constant_expression_p
);
14066 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
14067 cur_stmt_expr
= old_stmt_expr
;
14069 /* If the resulting list of expression statement is empty,
14070 fold it further into void_zero_node. */
14071 if (empty_expr_stmt_p (stmt_expr
))
14072 stmt_expr
= void_zero_node
;
14079 tree r
= build_lambda_expr ();
14081 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
14082 LAMBDA_EXPR_CLOSURE (r
) = type
;
14083 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
14085 LAMBDA_EXPR_LOCATION (r
)
14086 = LAMBDA_EXPR_LOCATION (t
);
14087 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
14088 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
14089 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
14090 LAMBDA_EXPR_DISCRIMINATOR (r
)
14091 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
14092 LAMBDA_EXPR_EXTRA_SCOPE (r
)
14093 = tsubst (LAMBDA_EXPR_EXTRA_SCOPE (t
), args
, complain
, in_decl
);
14094 LAMBDA_EXPR_RETURN_TYPE (r
)
14095 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
14097 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
14098 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
14100 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
14101 determine_visibility (TYPE_NAME (type
));
14102 /* Now that we know visibility, instantiate the type so we have a
14103 declaration of the op() for later calls to lambda_function. */
14104 complete_type (type
);
14106 /* The capture list refers to closure members, so this needs to
14107 wait until after we finish instantiating the type. */
14108 LAMBDA_EXPR_CAPTURE_LIST (r
)
14109 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t
));
14111 return build_lambda_object (r
);
14115 /* We can get here for a constant initializer of non-dependent type.
14116 FIXME stop folding in cp_parser_initializer_clause. */
14117 gcc_assert (TREE_CONSTANT (t
));
14119 tree r
= get_target_expr (RECUR (TARGET_EXPR_INITIAL (t
)));
14120 TREE_CONSTANT (r
) = true;
14124 case TRANSACTION_EXPR
:
14125 return tsubst_expr(t
, args
, complain
, in_decl
,
14126 integral_constant_expression_p
);
14129 /* Handle Objective-C++ constructs, if appropriate. */
14132 = objcp_tsubst_copy_and_build (t
, args
, complain
,
14133 in_decl
, /*function_p=*/false);
14137 return tsubst_copy (t
, args
, complain
, in_decl
);
14143 /* Verify that the instantiated ARGS are valid. For type arguments,
14144 make sure that the type's linkage is ok. For non-type arguments,
14145 make sure they are constants if they are integral or enumerations.
14146 Emit an error under control of COMPLAIN, and return TRUE on error. */
14149 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
14151 if (ARGUMENT_PACK_P (t
))
14153 tree vec
= ARGUMENT_PACK_ARGS (t
);
14154 int len
= TREE_VEC_LENGTH (vec
);
14155 bool result
= false;
14158 for (i
= 0; i
< len
; ++i
)
14159 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
14163 else if (TYPE_P (t
))
14165 /* [basic.link]: A name with no linkage (notably, the name
14166 of a class or enumeration declared in a local scope)
14167 shall not be used to declare an entity with linkage.
14168 This implies that names with no linkage cannot be used as
14171 DR 757 relaxes this restriction for C++0x. */
14172 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
14173 : no_linkage_check (t
, /*relaxed_p=*/false));
14177 /* DR 488 makes use of a type with no linkage cause
14178 type deduction to fail. */
14179 if (complain
& tf_error
)
14181 if (TYPE_ANONYMOUS_P (nt
))
14182 error ("%qT is/uses anonymous type", t
);
14184 error ("template argument for %qD uses local type %qT",
14189 /* In order to avoid all sorts of complications, we do not
14190 allow variably-modified types as template arguments. */
14191 else if (variably_modified_type_p (t
, NULL_TREE
))
14193 if (complain
& tf_error
)
14194 error ("%qT is a variably modified type", t
);
14198 /* A non-type argument of integral or enumerated type must be a
14200 else if (TREE_TYPE (t
)
14201 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
14202 && !TREE_CONSTANT (t
))
14204 if (complain
& tf_error
)
14205 error ("integral expression %qE is not constant", t
);
14212 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
14214 int ix
, len
= DECL_NTPARMS (tmpl
);
14215 bool result
= false;
14217 for (ix
= 0; ix
!= len
; ix
++)
14219 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
14222 if (result
&& (complain
& tf_error
))
14223 error (" trying to instantiate %qD", tmpl
);
14227 /* In C++0x, it's possible to have a function template whose type depends
14228 on itself recursively. This is most obvious with decltype, but can also
14229 occur with enumeration scope (c++/48969). So we need to catch infinite
14230 recursion and reject the substitution at deduction time; this function
14231 will return error_mark_node for any repeated substitution.
14233 This also catches excessive recursion such as when f<N> depends on
14234 f<N-1> across all integers, and returns error_mark_node for all the
14235 substitutions back up to the initial one.
14237 This is, of course, not reentrant. */
14240 deduction_tsubst_fntype (tree fn
, tree targs
, tsubst_flags_t complain
)
14242 static bool excessive_deduction_depth
;
14243 static int deduction_depth
;
14244 struct pending_template
*old_last_pend
= last_pending_template
;
14245 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
14247 tree fntype
= TREE_TYPE (fn
);
14251 if (excessive_deduction_depth
)
14252 return error_mark_node
;
14254 tinst
= build_tree_list (fn
, targs
);
14255 if (!push_tinst_level (tinst
))
14257 excessive_deduction_depth
= true;
14259 return error_mark_node
;
14262 input_location
= DECL_SOURCE_LOCATION (fn
);
14264 /* We will do access checks in instantiate_template. */
14265 push_deferring_access_checks (dk_deferred
);
14266 r
= tsubst (fntype
, targs
, complain
, NULL_TREE
);
14267 pop_deferring_access_checks ();
14270 if (excessive_deduction_depth
)
14272 r
= error_mark_node
;
14273 if (deduction_depth
== 0)
14274 /* Reset once we're all the way out. */
14275 excessive_deduction_depth
= false;
14278 pop_tinst_level ();
14279 /* We can't free this if a pending_template entry or last_error_tinst_level
14280 is pointing at it. */
14281 if (last_pending_template
== old_last_pend
14282 && last_error_tinst_level
== old_error_tinst
)
14287 /* Instantiate the indicated variable or function template TMPL with
14288 the template arguments in TARG_PTR. */
14291 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
14293 tree targ_ptr
= orig_args
;
14298 if (tmpl
== error_mark_node
)
14299 return error_mark_node
;
14301 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
14303 /* If this function is a clone, handle it specially. */
14304 if (DECL_CLONED_FUNCTION_P (tmpl
))
14309 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
14310 DECL_CLONED_FUNCTION. */
14311 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
14312 targ_ptr
, complain
);
14313 if (spec
== error_mark_node
)
14314 return error_mark_node
;
14316 /* Look for the clone. */
14317 FOR_EACH_CLONE (clone
, spec
)
14318 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
14320 /* We should always have found the clone by now. */
14321 gcc_unreachable ();
14325 /* Check to see if we already have this specialization. */
14326 gen_tmpl
= most_general_template (tmpl
);
14327 if (tmpl
!= gen_tmpl
)
14328 /* The TMPL is a partial instantiation. To get a full set of
14329 arguments we must add the arguments used to perform the
14330 partial instantiation. */
14331 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
14334 /* It would be nice to avoid hashing here and then again in tsubst_decl,
14335 but it doesn't seem to be on the hot path. */
14336 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
14338 gcc_assert (tmpl
== gen_tmpl
14339 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
14341 || fndecl
== NULL_TREE
);
14343 if (spec
!= NULL_TREE
)
14346 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
14348 return error_mark_node
;
14350 /* We are building a FUNCTION_DECL, during which the access of its
14351 parameters and return types have to be checked. However this
14352 FUNCTION_DECL which is the desired context for access checking
14353 is not built yet. We solve this chicken-and-egg problem by
14354 deferring all checks until we have the FUNCTION_DECL. */
14355 push_deferring_access_checks (dk_deferred
);
14357 /* Instantiation of the function happens in the context of the function
14358 template, not the context of the overload resolution we're doing. */
14359 push_to_top_level ();
14360 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
14362 tree ctx
= tsubst (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
14363 complain
, gen_tmpl
);
14364 push_nested_class (ctx
);
14366 /* Substitute template parameters to obtain the specialization. */
14367 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
14368 targ_ptr
, complain
, gen_tmpl
);
14369 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
14370 pop_nested_class ();
14371 pop_from_top_level ();
14373 if (fndecl
== error_mark_node
)
14374 return error_mark_node
;
14376 /* The DECL_TI_TEMPLATE should always be the immediate parent
14377 template, not the most general template. */
14378 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
14380 /* Now we know the specialization, compute access previously
14382 push_access_scope (fndecl
);
14383 perform_deferred_access_checks ();
14384 pop_access_scope (fndecl
);
14385 pop_deferring_access_checks ();
14387 /* If we've just instantiated the main entry point for a function,
14388 instantiate all the alternate entry points as well. We do this
14389 by cloning the instantiation of the main entry point, not by
14390 instantiating the template clones. */
14391 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
14392 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
14397 /* Wrapper for instantiate_template_1. */
14400 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
14403 timevar_push (TV_TEMPLATE_INST
);
14404 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
14405 timevar_pop (TV_TEMPLATE_INST
);
14409 /* PARM is a template parameter pack for FN. Returns true iff
14410 PARM is used in a deducible way in the argument list of FN. */
14413 pack_deducible_p (tree parm
, tree fn
)
14415 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
14416 for (; t
; t
= TREE_CHAIN (t
))
14418 tree type
= TREE_VALUE (t
);
14420 if (!PACK_EXPANSION_P (type
))
14422 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
14423 packs
; packs
= TREE_CHAIN (packs
))
14424 if (TREE_VALUE (packs
) == parm
)
14426 /* The template parameter pack is used in a function parameter
14427 pack. If this is the end of the parameter list, the
14428 template parameter pack is deducible. */
14429 if (TREE_CHAIN (t
) == void_list_node
)
14432 /* Otherwise, not. Well, it could be deduced from
14433 a non-pack parameter, but doing so would end up with
14434 a deduction mismatch, so don't bother. */
14438 /* The template parameter pack isn't used in any function parameter
14439 packs, but it might be used deeper, e.g. tuple<Args...>. */
14443 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
14444 NARGS elements of the arguments that are being used when calling
14445 it. TARGS is a vector into which the deduced template arguments
14448 Return zero for success, 2 for an incomplete match that doesn't resolve
14449 all the types, and 1 for complete failure. An error message will be
14450 printed only for an incomplete match.
14452 If FN is a conversion operator, or we are trying to produce a specific
14453 specialization, RETURN_TYPE is the return type desired.
14455 The EXPLICIT_TARGS are explicit template arguments provided via a
14458 The parameter STRICT is one of:
14461 We are deducing arguments for a function call, as in
14462 [temp.deduct.call].
14465 We are deducing arguments for a conversion function, as in
14466 [temp.deduct.conv].
14469 We are deducing arguments when doing an explicit instantiation
14470 as in [temp.explicit], when determining an explicit specialization
14471 as in [temp.expl.spec], or when taking the address of a function
14472 template, as in [temp.deduct.funcaddr]. */
14475 fn_type_unification (tree fn
,
14476 tree explicit_targs
,
14479 unsigned int nargs
,
14481 unification_kind_t strict
,
14489 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
14491 fntype
= TREE_TYPE (fn
);
14492 if (explicit_targs
)
14496 The specified template arguments must match the template
14497 parameters in kind (i.e., type, nontype, template), and there
14498 must not be more arguments than there are parameters;
14499 otherwise type deduction fails.
14501 Nontype arguments must match the types of the corresponding
14502 nontype template parameters, or must be convertible to the
14503 types of the corresponding nontype parameters as specified in
14504 _temp.arg.nontype_, otherwise type deduction fails.
14506 All references in the function type of the function template
14507 to the corresponding template parameters are replaced by the
14508 specified template argument values. If a substitution in a
14509 template parameter or in the function type of the function
14510 template results in an invalid type, type deduction fails. */
14511 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
14512 int i
, len
= TREE_VEC_LENGTH (tparms
);
14513 tree converted_args
;
14514 bool incomplete
= false;
14516 if (explicit_targs
== error_mark_node
)
14517 return unify_invalid (explain_p
);
14520 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
14522 ? tf_warning_or_error
14524 /*require_all_args=*/false,
14525 /*use_default_args=*/false));
14526 if (converted_args
== error_mark_node
)
14529 /* Substitute the explicit args into the function type. This is
14530 necessary so that, for instance, explicitly declared function
14531 arguments can match null pointed constants. If we were given
14532 an incomplete set of explicit args, we must not do semantic
14533 processing during substitution as we could create partial
14535 for (i
= 0; i
< len
; i
++)
14537 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
14538 bool parameter_pack
= false;
14539 tree targ
= TREE_VEC_ELT (converted_args
, i
);
14541 /* Dig out the actual parm. */
14542 if (TREE_CODE (parm
) == TYPE_DECL
14543 || TREE_CODE (parm
) == TEMPLATE_DECL
)
14545 parm
= TREE_TYPE (parm
);
14546 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
14548 else if (TREE_CODE (parm
) == PARM_DECL
)
14550 parm
= DECL_INITIAL (parm
);
14551 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
14554 if (!parameter_pack
&& targ
== NULL_TREE
)
14555 /* No explicit argument for this template parameter. */
14558 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
14560 /* Mark the argument pack as "incomplete". We could
14561 still deduce more arguments during unification.
14562 We remove this mark in type_unification_real. */
14565 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
14566 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
14567 = ARGUMENT_PACK_ARGS (targ
);
14570 /* We have some incomplete argument packs. */
14575 processing_template_decl
+= incomplete
;
14576 fntype
= deduction_tsubst_fntype (fn
, converted_args
,
14578 ? tf_warning_or_error
14579 : tf_none
) | tf_partial
);
14580 processing_template_decl
-= incomplete
;
14582 if (fntype
== error_mark_node
)
14585 /* Place the explicitly specified arguments in TARGS. */
14586 for (i
= NUM_TMPL_ARGS (converted_args
); i
--;)
14587 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (converted_args
, i
);
14590 /* Never do unification on the 'this' parameter. */
14591 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
14597 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
14598 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
14599 new_args
[0] = return_type
;
14600 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
14605 /* We allow incomplete unification without an error message here
14606 because the standard doesn't seem to explicitly prohibit it. Our
14607 callers must be ready to deal with unification failures in any
14609 result
= type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
14610 targs
, parms
, args
, nargs
, /*subr=*/0,
14611 strict
, flags
, explain_p
);
14613 /* Now that we have bindings for all of the template arguments,
14614 ensure that the arguments deduced for the template template
14615 parameters have compatible template parameter lists. We cannot
14616 check this property before we have deduced all template
14617 arguments, because the template parameter types of a template
14618 template parameter might depend on prior template parameters
14619 deduced after the template template parameter. The following
14620 ill-formed example illustrates this issue:
14622 template<typename T, template<T> class C> void f(C<5>, T);
14624 template<int N> struct X {};
14627 f(X<5>(), 5l); // error: template argument deduction fails
14630 The template parameter list of 'C' depends on the template type
14631 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
14632 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
14633 time that we deduce 'C'. */
14635 && !template_template_parm_bindings_ok_p
14636 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
14637 return unify_inconsistent_template_template_parameters (explain_p
);
14640 /* All is well so far. Now, check:
14644 When all template arguments have been deduced, all uses of
14645 template parameters in nondeduced contexts are replaced with
14646 the corresponding deduced argument values. If the
14647 substitution results in an invalid type, as described above,
14648 type deduction fails. */
14650 tree substed
= deduction_tsubst_fntype (fn
, targs
,
14652 ? tf_warning_or_error
14654 if (substed
== error_mark_node
)
14657 /* If we're looking for an exact match, check that what we got
14658 is indeed an exact match. It might not be if some template
14659 parameters are used in non-deduced contexts. */
14660 if (strict
== DEDUCE_EXACT
)
14665 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (substed
));
14667 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
14668 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
14669 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
14670 return unify_type_mismatch (explain_p
, args
[i
],
14671 TREE_VALUE (sarg
));
14678 /* Adjust types before performing type deduction, as described in
14679 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
14680 sections are symmetric. PARM is the type of a function parameter
14681 or the return type of the conversion function. ARG is the type of
14682 the argument passed to the call, or the type of the value
14683 initialized with the result of the conversion function.
14684 ARG_EXPR is the original argument expression, which may be null. */
14687 maybe_adjust_types_for_deduction (unification_kind_t strict
,
14701 /* Swap PARM and ARG throughout the remainder of this
14702 function; the handling is precisely symmetric since PARM
14703 will initialize ARG rather than vice versa. */
14711 /* Core issue #873: Do the DR606 thing (see below) for these cases,
14712 too, but here handle it by stripping the reference from PARM
14713 rather than by adding it to ARG. */
14714 if (TREE_CODE (*parm
) == REFERENCE_TYPE
14715 && TYPE_REF_IS_RVALUE (*parm
)
14716 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
14717 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
14718 && TREE_CODE (*arg
) == REFERENCE_TYPE
14719 && !TYPE_REF_IS_RVALUE (*arg
))
14720 *parm
= TREE_TYPE (*parm
);
14721 /* Nothing else to do in this case. */
14725 gcc_unreachable ();
14728 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
14730 /* [temp.deduct.call]
14732 If P is not a reference type:
14734 --If A is an array type, the pointer type produced by the
14735 array-to-pointer standard conversion (_conv.array_) is
14736 used in place of A for type deduction; otherwise,
14738 --If A is a function type, the pointer type produced by
14739 the function-to-pointer standard conversion
14740 (_conv.func_) is used in place of A for type deduction;
14743 --If A is a cv-qualified type, the top level
14744 cv-qualifiers of A's type are ignored for type
14746 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
14747 *arg
= build_pointer_type (TREE_TYPE (*arg
));
14748 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
14749 *arg
= build_pointer_type (*arg
);
14751 *arg
= TYPE_MAIN_VARIANT (*arg
);
14754 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
14755 of the form T&&, where T is a template parameter, and the argument
14756 is an lvalue, T is deduced as A& */
14757 if (TREE_CODE (*parm
) == REFERENCE_TYPE
14758 && TYPE_REF_IS_RVALUE (*parm
)
14759 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
14760 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
14761 && (arg_expr
? real_lvalue_p (arg_expr
)
14762 /* try_one_overload doesn't provide an arg_expr, but
14763 functions are always lvalues. */
14764 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
14765 *arg
= build_reference_type (*arg
);
14767 /* [temp.deduct.call]
14769 If P is a cv-qualified type, the top level cv-qualifiers
14770 of P's type are ignored for type deduction. If P is a
14771 reference type, the type referred to by P is used for
14773 *parm
= TYPE_MAIN_VARIANT (*parm
);
14774 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
14776 *parm
= TREE_TYPE (*parm
);
14777 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
14780 /* DR 322. For conversion deduction, remove a reference type on parm
14781 too (which has been swapped into ARG). */
14782 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
14783 *arg
= TREE_TYPE (*arg
);
14788 /* Subroutine of unify_one_argument. PARM is a function parameter of a
14789 template which does contain any deducible template parameters; check if
14790 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
14791 unify_one_argument. */
14794 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
14795 int flags
, bool explain_p
)
14800 type
= TREE_TYPE (arg
);
14804 if (same_type_p (parm
, type
))
14805 return unify_success (explain_p
);
14807 if (strict
== DEDUCE_CONV
)
14809 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
14810 explain_p
? tf_warning_or_error
: tf_none
))
14811 return unify_success (explain_p
);
14813 else if (strict
!= DEDUCE_EXACT
)
14815 if (can_convert_arg (parm
, type
,
14816 TYPE_P (arg
) ? NULL_TREE
: arg
,
14817 flags
, explain_p
? tf_warning_or_error
: tf_none
))
14818 return unify_success (explain_p
);
14821 if (strict
== DEDUCE_EXACT
)
14822 return unify_type_mismatch (explain_p
, parm
, arg
);
14824 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
14827 /* Subroutine of type_unification_real and unify_pack_expansion to
14828 handle unification of a single P/A pair. Parameters are as
14829 for those functions. */
14832 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
14833 int subr
, unification_kind_t strict
, int flags
,
14836 tree arg_expr
= NULL_TREE
;
14839 if (arg
== error_mark_node
|| parm
== error_mark_node
)
14840 return unify_invalid (explain_p
);
14841 if (arg
== unknown_type_node
)
14842 /* We can't deduce anything from this, but we might get all the
14843 template args from other function args. */
14844 return unify_success (explain_p
);
14846 /* FIXME uses_deducible_template_parms */
14847 if (TYPE_P (parm
) && !uses_template_parms (parm
))
14848 return check_non_deducible_conversion (parm
, arg
, strict
, flags
,
14854 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
14855 | UNIFY_ALLOW_MORE_CV_QUAL
14856 | UNIFY_ALLOW_DERIVED
);
14860 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
14864 arg_strict
= UNIFY_ALLOW_NONE
;
14868 gcc_unreachable ();
14871 /* We only do these transformations if this is the top-level
14872 parameter_type_list in a call or declaration matching; in other
14873 situations (nested function declarators, template argument lists) we
14874 won't be comparing a type to an expression, and we don't do any type
14880 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
14881 if (type_unknown_p (arg
))
14883 /* [temp.deduct.type] A template-argument can be
14884 deduced from a pointer to function or pointer
14885 to member function argument if the set of
14886 overloaded functions does not contain function
14887 templates and at most one of a set of
14888 overloaded functions provides a unique
14891 if (resolve_overloaded_unification
14892 (tparms
, targs
, parm
, arg
, strict
,
14893 arg_strict
, explain_p
))
14894 return unify_success (explain_p
);
14895 return unify_overload_resolution_failure (explain_p
, arg
);
14899 arg
= unlowered_expr_type (arg
);
14900 if (arg
== error_mark_node
)
14901 return unify_invalid (explain_p
);
14905 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
14908 gcc_assert ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
14909 == (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
));
14911 /* For deduction from an init-list we need the actual list. */
14912 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
14914 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
14917 /* Most parms like fn_type_unification.
14919 If SUBR is 1, we're being called recursively (to unify the
14920 arguments of a function or method parameter of a function
14924 type_unification_real (tree tparms
,
14928 unsigned int xnargs
,
14930 unification_kind_t strict
,
14936 int ntparms
= TREE_VEC_LENGTH (tparms
);
14937 int saw_undeduced
= 0;
14940 unsigned int nargs
;
14943 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
14944 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
14945 gcc_assert (ntparms
> 0);
14947 /* Reset the number of non-defaulted template arguments contained
14949 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
14957 while (parms
&& parms
!= void_list_node
14960 parm
= TREE_VALUE (parms
);
14962 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
14963 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
14964 /* For a function parameter pack that occurs at the end of the
14965 parameter-declaration-list, the type A of each remaining
14966 argument of the call is compared with the type P of the
14967 declarator-id of the function parameter pack. */
14970 parms
= TREE_CHAIN (parms
);
14972 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
14973 /* For a function parameter pack that does not occur at the
14974 end of the parameter-declaration-list, the type of the
14975 parameter pack is a non-deduced context. */
14981 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
14987 && parms
!= void_list_node
14988 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
14990 /* Unify the remaining arguments with the pack expansion type. */
14992 tree parmvec
= make_tree_vec (1);
14994 /* Allocate a TREE_VEC and copy in all of the arguments */
14995 argvec
= make_tree_vec (nargs
- ia
);
14996 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
14997 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
14999 /* Copy the parameter into parmvec. */
15000 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
15001 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
15002 /*subr=*/subr
, explain_p
))
15005 /* Advance to the end of the list of parameters. */
15006 parms
= TREE_CHAIN (parms
);
15009 /* Fail if we've reached the end of the parm list, and more args
15010 are present, and the parm list isn't variadic. */
15011 if (ia
< nargs
&& parms
== void_list_node
)
15012 return unify_too_many_arguments (explain_p
, nargs
, ia
);
15013 /* Fail if parms are left and they don't have default values. */
15014 if (parms
&& parms
!= void_list_node
15015 && TREE_PURPOSE (parms
) == NULL_TREE
)
15017 unsigned int count
= nargs
;
15019 while (p
&& p
!= void_list_node
)
15022 p
= TREE_CHAIN (p
);
15024 return unify_too_few_arguments (explain_p
, ia
, count
);
15029 tsubst_flags_t complain
= (explain_p
15030 ? tf_warning_or_error
15033 /* Check to see if we need another pass before we start clearing
15034 ARGUMENT_PACK_INCOMPLETE_P. */
15035 for (i
= 0; i
< ntparms
; i
++)
15037 tree targ
= TREE_VEC_ELT (targs
, i
);
15038 tree tparm
= TREE_VEC_ELT (tparms
, i
);
15040 if (targ
|| tparm
== error_mark_node
)
15042 tparm
= TREE_VALUE (tparm
);
15044 /* If this is an undeduced nontype parameter that depends on
15045 a type parameter, try another pass; its type may have been
15046 deduced from a later argument than the one from which
15047 this parameter can be deduced. */
15048 if (TREE_CODE (tparm
) == PARM_DECL
15049 && uses_template_parms (TREE_TYPE (tparm
))
15050 && !saw_undeduced
++)
15054 for (i
= 0; i
< ntparms
; i
++)
15056 tree targ
= TREE_VEC_ELT (targs
, i
);
15057 tree tparm
= TREE_VEC_ELT (tparms
, i
);
15059 /* Clear the "incomplete" flags on all argument packs now so that
15060 substituting them into later default arguments works. */
15061 if (targ
&& ARGUMENT_PACK_P (targ
))
15063 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
15064 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
15067 if (targ
|| tparm
== error_mark_node
)
15069 tparm
= TREE_VALUE (tparm
);
15071 /* Core issue #226 (C++0x) [temp.deduct]:
15073 If a template argument has not been deduced, its
15074 default template argument, if any, is used.
15076 When we are in C++98 mode, TREE_PURPOSE will either
15077 be NULL_TREE or ERROR_MARK_NODE, so we do not need
15078 to explicitly check cxx_dialect here. */
15079 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
15081 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
15082 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
15083 location_t save_loc
= input_location
;
15085 input_location
= DECL_SOURCE_LOCATION (parm
);
15086 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
15087 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
15089 input_location
= save_loc
;
15090 if (arg
== error_mark_node
)
15094 TREE_VEC_ELT (targs
, i
) = arg
;
15095 /* The position of the first default template argument,
15096 is also the number of non-defaulted arguments in TARGS.
15098 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
15099 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
15104 /* If the type parameter is a parameter pack, then it will
15105 be deduced to an empty parameter pack. */
15106 if (template_parameter_pack_p (tparm
))
15110 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
15112 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
15113 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
15114 TREE_CONSTANT (arg
) = 1;
15117 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
15119 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
15121 TREE_VEC_ELT (targs
, i
) = arg
;
15125 return unify_parameter_deduction_failure (explain_p
, tparm
);
15128 #ifdef ENABLE_CHECKING
15129 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
15130 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
15133 return unify_success (explain_p
);
15136 /* Subroutine of type_unification_real. Args are like the variables
15137 at the call site. ARG is an overloaded function (or template-id);
15138 we try deducing template args from each of the overloads, and if
15139 only one succeeds, we go with that. Modifies TARGS and returns
15140 true on success. */
15143 resolve_overloaded_unification (tree tparms
,
15147 unification_kind_t strict
,
15151 tree tempargs
= copy_node (targs
);
15153 tree goodfn
= NULL_TREE
;
15156 if (TREE_CODE (arg
) == ADDR_EXPR
)
15158 arg
= TREE_OPERAND (arg
, 0);
15164 if (TREE_CODE (arg
) == COMPONENT_REF
)
15165 /* Handle `&x' where `x' is some static or non-static member
15167 arg
= TREE_OPERAND (arg
, 1);
15169 if (TREE_CODE (arg
) == OFFSET_REF
)
15170 arg
= TREE_OPERAND (arg
, 1);
15172 /* Strip baselink information. */
15173 if (BASELINK_P (arg
))
15174 arg
= BASELINK_FUNCTIONS (arg
);
15176 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
15178 /* If we got some explicit template args, we need to plug them into
15179 the affected templates before we try to unify, in case the
15180 explicit args will completely resolve the templates in question. */
15183 tree expl_subargs
= TREE_OPERAND (arg
, 1);
15184 arg
= TREE_OPERAND (arg
, 0);
15186 for (; arg
; arg
= OVL_NEXT (arg
))
15188 tree fn
= OVL_CURRENT (arg
);
15189 tree subargs
, elem
;
15191 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
15194 ++processing_template_decl
;
15195 subargs
= get_bindings (fn
, DECL_TEMPLATE_RESULT (fn
),
15196 expl_subargs
, /*check_ret=*/false);
15197 if (subargs
&& !any_dependent_template_arguments_p (subargs
))
15199 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
15200 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
15201 elem
, strict
, sub_strict
, addr_p
, explain_p
)
15202 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
15210 --processing_template_decl
;
15212 /* If no templates (or more than one) are fully resolved by the
15213 explicit arguments, this template-id is a non-deduced context; it
15214 could still be OK if we deduce all template arguments for the
15215 enclosing call through other arguments. */
15219 else if (TREE_CODE (arg
) != OVERLOAD
15220 && TREE_CODE (arg
) != FUNCTION_DECL
)
15221 /* If ARG is, for example, "(0, &f)" then its type will be unknown
15222 -- but the deduction does not succeed because the expression is
15223 not just the function on its own. */
15226 for (; arg
; arg
= OVL_NEXT (arg
))
15227 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
15228 TREE_TYPE (OVL_CURRENT (arg
)),
15229 strict
, sub_strict
, addr_p
, explain_p
)
15230 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
15232 goodfn
= OVL_CURRENT (arg
);
15236 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15237 to function or pointer to member function argument if the set of
15238 overloaded functions does not contain function templates and at most
15239 one of a set of overloaded functions provides a unique match.
15241 So if we found multiple possibilities, we return success but don't
15242 deduce anything. */
15246 int i
= TREE_VEC_LENGTH (targs
);
15248 if (TREE_VEC_ELT (tempargs
, i
))
15249 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
15257 /* Core DR 115: In contexts where deduction is done and fails, or in
15258 contexts where deduction is not done, if a template argument list is
15259 specified and it, along with any default template arguments, identifies
15260 a single function template specialization, then the template-id is an
15261 lvalue for the function template specialization. */
15264 resolve_nondeduced_context (tree orig_expr
)
15266 tree expr
, offset
, baselink
;
15269 if (!type_unknown_p (orig_expr
))
15274 offset
= NULL_TREE
;
15275 baselink
= NULL_TREE
;
15277 if (TREE_CODE (expr
) == ADDR_EXPR
)
15279 expr
= TREE_OPERAND (expr
, 0);
15282 if (TREE_CODE (expr
) == OFFSET_REF
)
15285 expr
= TREE_OPERAND (expr
, 1);
15287 if (BASELINK_P (expr
))
15290 expr
= BASELINK_FUNCTIONS (expr
);
15293 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
15296 tree goodfn
= NULL_TREE
;
15298 /* If we got some explicit template args, we need to plug them into
15299 the affected templates before we try to unify, in case the
15300 explicit args will completely resolve the templates in question. */
15302 tree expl_subargs
= TREE_OPERAND (expr
, 1);
15303 tree arg
= TREE_OPERAND (expr
, 0);
15304 tree badfn
= NULL_TREE
;
15305 tree badargs
= NULL_TREE
;
15307 for (; arg
; arg
= OVL_NEXT (arg
))
15309 tree fn
= OVL_CURRENT (arg
);
15310 tree subargs
, elem
;
15312 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
15315 ++processing_template_decl
;
15316 subargs
= get_bindings (fn
, DECL_TEMPLATE_RESULT (fn
),
15317 expl_subargs
, /*check_ret=*/false);
15318 if (subargs
&& !any_dependent_template_arguments_p (subargs
))
15320 elem
= instantiate_template (fn
, subargs
, tf_none
);
15321 if (elem
== error_mark_node
)
15326 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
15332 --processing_template_decl
;
15336 mark_used (goodfn
);
15339 expr
= build_baselink (BASELINK_BINFO (baselink
),
15340 BASELINK_ACCESS_BINFO (baselink
),
15341 expr
, BASELINK_OPTYPE (baselink
));
15345 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
15346 expr
= build_offset_ref (base
, expr
, addr
);
15349 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
15352 else if (good
== 0 && badargs
)
15353 /* There were no good options and at least one bad one, so let the
15354 user know what the problem is. */
15355 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
15360 /* Subroutine of resolve_overloaded_unification; does deduction for a single
15361 overload. Fills TARGS with any deduced arguments, or error_mark_node if
15362 different overloads deduce different arguments for a given parm.
15363 ADDR_P is true if the expression for which deduction is being
15364 performed was of the form "& fn" rather than simply "fn".
15366 Returns 1 on success. */
15369 try_one_overload (tree tparms
,
15374 unification_kind_t strict
,
15383 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15384 to function or pointer to member function argument if the set of
15385 overloaded functions does not contain function templates and at most
15386 one of a set of overloaded functions provides a unique match.
15388 So if this is a template, just return success. */
15390 if (uses_template_parms (arg
))
15393 if (TREE_CODE (arg
) == METHOD_TYPE
)
15394 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
15396 arg
= build_pointer_type (arg
);
15398 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
15400 /* We don't copy orig_targs for this because if we have already deduced
15401 some template args from previous args, unify would complain when we
15402 try to deduce a template parameter for the same argument, even though
15403 there isn't really a conflict. */
15404 nargs
= TREE_VEC_LENGTH (targs
);
15405 tempargs
= make_tree_vec (nargs
);
15407 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
15410 /* First make sure we didn't deduce anything that conflicts with
15411 explicitly specified args. */
15412 for (i
= nargs
; i
--; )
15414 tree elt
= TREE_VEC_ELT (tempargs
, i
);
15415 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
15419 else if (uses_template_parms (elt
))
15420 /* Since we're unifying against ourselves, we will fill in
15421 template args used in the function parm list with our own
15422 template parms. Discard them. */
15423 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
15424 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
15428 for (i
= nargs
; i
--; )
15430 tree elt
= TREE_VEC_ELT (tempargs
, i
);
15433 TREE_VEC_ELT (targs
, i
) = elt
;
15439 /* PARM is a template class (perhaps with unbound template
15440 parameters). ARG is a fully instantiated type. If ARG can be
15441 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
15442 TARGS are as for unify. */
15445 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
15448 tree copy_of_targs
;
15450 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
15451 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
15452 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
15455 /* We need to make a new template argument vector for the call to
15456 unify. If we used TARGS, we'd clutter it up with the result of
15457 the attempted unification, even if this class didn't work out.
15458 We also don't want to commit ourselves to all the unifications
15459 we've already done, since unification is supposed to be done on
15460 an argument-by-argument basis. In other words, consider the
15461 following pathological case:
15463 template <int I, int J, int K>
15466 template <int I, int J>
15467 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
15469 template <int I, int J, int K>
15470 void f(S<I, J, K>, S<I, I, I>);
15479 Now, by the time we consider the unification involving `s2', we
15480 already know that we must have `f<0, 0, 0>'. But, even though
15481 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
15482 because there are two ways to unify base classes of S<0, 1, 2>
15483 with S<I, I, I>. If we kept the already deduced knowledge, we
15484 would reject the possibility I=1. */
15485 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
15487 /* If unification failed, we're done. */
15488 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
15489 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
15495 /* Given a template type PARM and a class type ARG, find the unique
15496 base type in ARG that is an instance of PARM. We do not examine
15497 ARG itself; only its base-classes. If there is not exactly one
15498 appropriate base class, return NULL_TREE. PARM may be the type of
15499 a partial specialization, as well as a plain template type. Used
15502 static enum template_base_result
15503 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
15504 bool explain_p
, tree
*result
)
15506 tree rval
= NULL_TREE
;
15509 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
15511 binfo
= TYPE_BINFO (complete_type (arg
));
15514 /* The type could not be completed. */
15515 *result
= NULL_TREE
;
15516 return tbr_incomplete_type
;
15519 /* Walk in inheritance graph order. The search order is not
15520 important, and this avoids multiple walks of virtual bases. */
15521 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
15523 tree r
= try_class_unification (tparms
, targs
, parm
,
15524 BINFO_TYPE (binfo
), explain_p
);
15528 /* If there is more than one satisfactory baseclass, then:
15532 If they yield more than one possible deduced A, the type
15536 if (rval
&& !same_type_p (r
, rval
))
15538 *result
= NULL_TREE
;
15539 return tbr_ambiguous_baseclass
;
15547 return tbr_success
;
15550 /* Returns the level of DECL, which declares a template parameter. */
15553 template_decl_level (tree decl
)
15555 switch (TREE_CODE (decl
))
15558 case TEMPLATE_DECL
:
15559 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
15562 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
15565 gcc_unreachable ();
15570 /* Decide whether ARG can be unified with PARM, considering only the
15571 cv-qualifiers of each type, given STRICT as documented for unify.
15572 Returns nonzero iff the unification is OK on that basis. */
15575 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
15577 int arg_quals
= cp_type_quals (arg
);
15578 int parm_quals
= cp_type_quals (parm
);
15580 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
15581 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
15583 /* Although a CVR qualifier is ignored when being applied to a
15584 substituted template parameter ([8.3.2]/1 for example), that
15585 does not allow us to unify "const T" with "int&" because both
15586 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
15587 It is ok when we're allowing additional CV qualifiers
15588 at the outer level [14.8.2.1]/3,1st bullet. */
15589 if ((TREE_CODE (arg
) == REFERENCE_TYPE
15590 || TREE_CODE (arg
) == FUNCTION_TYPE
15591 || TREE_CODE (arg
) == METHOD_TYPE
)
15592 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
15595 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
15596 && (parm_quals
& TYPE_QUAL_RESTRICT
))
15600 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
15601 && (arg_quals
& parm_quals
) != parm_quals
)
15604 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
15605 && (parm_quals
& arg_quals
) != arg_quals
)
15611 /* Determines the LEVEL and INDEX for the template parameter PARM. */
15613 template_parm_level_and_index (tree parm
, int* level
, int* index
)
15615 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
15616 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
15617 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
15619 *index
= TEMPLATE_TYPE_IDX (parm
);
15620 *level
= TEMPLATE_TYPE_LEVEL (parm
);
15624 *index
= TEMPLATE_PARM_IDX (parm
);
15625 *level
= TEMPLATE_PARM_LEVEL (parm
);
15629 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
15631 if (unify (TP, TA, P, A, S, EP)) \
15635 /* Unifies the remaining arguments in PACKED_ARGS with the pack
15636 expansion at the end of PACKED_PARMS. Returns 0 if the type
15637 deduction succeeds, 1 otherwise. STRICT is the same as in
15638 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
15639 call argument list. We'll need to adjust the arguments to make them
15640 types. SUBR tells us if this is from a recursive call to
15641 type_unification_real, or for comparing two template argument
15645 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
15646 tree packed_args
, unification_kind_t strict
,
15647 bool subr
, bool explain_p
)
15650 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
15651 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
15652 tree pack
, packs
= NULL_TREE
;
15653 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
15654 int len
= TREE_VEC_LENGTH (packed_args
);
15656 /* Determine the parameter packs we will be deducing from the
15657 pattern, and record their current deductions. */
15658 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
15659 pack
; pack
= TREE_CHAIN (pack
))
15661 tree parm_pack
= TREE_VALUE (pack
);
15664 /* Determine the index and level of this parameter pack. */
15665 template_parm_level_and_index (parm_pack
, &level
, &idx
);
15667 /* Keep track of the parameter packs and their corresponding
15669 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
15670 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
15673 /* Loop through all of the arguments that have not yet been
15674 unified and unify each with the pattern. */
15675 for (i
= start
; i
< len
; i
++)
15678 bool any_explicit
= false;
15679 tree arg
= TREE_VEC_ELT (packed_args
, i
);
15681 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
15682 or the element of its argument pack at the current index if
15683 this argument was explicitly specified. */
15684 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
15688 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
15691 if (TREE_VALUE (pack
)
15692 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
15693 && (i
< TREE_VEC_LENGTH (pargs
)))
15695 any_explicit
= true;
15696 arg
= TREE_VEC_ELT (pargs
, i
);
15698 TMPL_ARG (targs
, level
, idx
) = arg
;
15701 /* If we had explicit template arguments, substitute them into the
15702 pattern before deduction. */
15705 /* Some arguments might still be unspecified or dependent. */
15707 ++processing_template_decl
;
15708 dependent
= any_dependent_template_arguments_p (targs
);
15710 --processing_template_decl
;
15711 parm
= tsubst (pattern
, targs
,
15712 explain_p
? tf_warning_or_error
: tf_none
,
15715 --processing_template_decl
;
15716 if (parm
== error_mark_node
)
15722 /* Unify the pattern with the current argument. */
15723 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
15724 LOOKUP_IMPLICIT
, explain_p
))
15727 /* For each parameter pack, collect the deduced value. */
15728 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
15731 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
15733 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
15734 TMPL_ARG (targs
, level
, idx
);
15738 /* Verify that the results of unification with the parameter packs
15739 produce results consistent with what we've seen before, and make
15740 the deduced argument packs available. */
15741 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
15743 tree old_pack
= TREE_VALUE (pack
);
15744 tree new_args
= TREE_TYPE (pack
);
15745 int i
, len
= TREE_VEC_LENGTH (new_args
);
15747 bool nondeduced_p
= false;
15749 /* By default keep the original deduced argument pack.
15750 If necessary, more specific code is going to update the
15751 resulting deduced argument later down in this function. */
15752 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
15753 TMPL_ARG (targs
, level
, idx
) = old_pack
;
15755 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
15756 actually deduce anything. */
15757 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
15758 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
15759 nondeduced_p
= true;
15763 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
15765 /* If we had fewer function args than explicit template args,
15766 just use the explicits. */
15767 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
15768 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
15769 if (len
< explicit_len
)
15770 new_args
= explicit_args
;
15776 /* Build the deduced *_ARGUMENT_PACK. */
15777 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
15779 result
= make_node (NONTYPE_ARGUMENT_PACK
);
15780 TREE_TYPE (result
) =
15781 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
15782 TREE_CONSTANT (result
) = 1;
15785 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
15787 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
15789 /* Note the deduced argument packs for this parameter
15791 TMPL_ARG (targs
, level
, idx
) = result
;
15793 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
15794 && (ARGUMENT_PACK_ARGS (old_pack
)
15795 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
15797 /* We only had the explicitly-provided arguments before, but
15798 now we have a complete set of arguments. */
15799 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
15801 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
15802 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
15803 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
15807 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
15808 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
15810 if (!comp_template_args_with_info (old_args
, new_args
,
15811 &bad_old_arg
, &bad_new_arg
))
15812 /* Inconsistent unification of this parameter pack. */
15813 return unify_parameter_pack_inconsistent (explain_p
,
15819 return unify_success (explain_p
);
15822 /* Deduce the value of template parameters. TPARMS is the (innermost)
15823 set of template parameters to a template. TARGS is the bindings
15824 for those template parameters, as determined thus far; TARGS may
15825 include template arguments for outer levels of template parameters
15826 as well. PARM is a parameter to a template function, or a
15827 subcomponent of that parameter; ARG is the corresponding argument.
15828 This function attempts to match PARM with ARG in a manner
15829 consistent with the existing assignments in TARGS. If more values
15830 are deduced, then TARGS is updated.
15832 Returns 0 if the type deduction succeeds, 1 otherwise. The
15833 parameter STRICT is a bitwise or of the following flags:
15836 Require an exact match between PARM and ARG.
15837 UNIFY_ALLOW_MORE_CV_QUAL:
15838 Allow the deduced ARG to be more cv-qualified (by qualification
15839 conversion) than ARG.
15840 UNIFY_ALLOW_LESS_CV_QUAL:
15841 Allow the deduced ARG to be less cv-qualified than ARG.
15842 UNIFY_ALLOW_DERIVED:
15843 Allow the deduced ARG to be a template base class of ARG,
15844 or a pointer to a template base class of the type pointed to by
15846 UNIFY_ALLOW_INTEGER:
15847 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
15848 case for more information.
15849 UNIFY_ALLOW_OUTER_LEVEL:
15850 This is the outermost level of a deduction. Used to determine validity
15851 of qualification conversions. A valid qualification conversion must
15852 have const qualified pointers leading up to the inner type which
15853 requires additional CV quals, except at the outer level, where const
15854 is not required [conv.qual]. It would be normal to set this flag in
15855 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
15856 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
15857 This is the outermost level of a deduction, and PARM can be more CV
15858 qualified at this point.
15859 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
15860 This is the outermost level of a deduction, and PARM can be less CV
15861 qualified at this point. */
15864 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
15870 int strict_in
= strict
;
15872 /* I don't think this will do the right thing with respect to types.
15873 But the only case I've seen it in so far has been array bounds, where
15874 signedness is the only information lost, and I think that will be
15876 while (TREE_CODE (parm
) == NOP_EXPR
)
15877 parm
= TREE_OPERAND (parm
, 0);
15879 if (arg
== error_mark_node
)
15880 return unify_invalid (explain_p
);
15881 if (arg
== unknown_type_node
15882 || arg
== init_list_type_node
)
15883 /* We can't deduce anything from this, but we might get all the
15884 template args from other function args. */
15885 return unify_success (explain_p
);
15887 /* If PARM uses template parameters, then we can't bail out here,
15888 even if ARG == PARM, since we won't record unifications for the
15889 template parameters. We might need them if we're trying to
15890 figure out which of two things is more specialized. */
15891 if (arg
== parm
&& !uses_template_parms (parm
))
15892 return unify_success (explain_p
);
15894 /* Handle init lists early, so the rest of the function can assume
15895 we're dealing with a type. */
15896 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
15900 tree orig_parm
= parm
;
15902 /* Replace T with std::initializer_list<T> for deduction. */
15903 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
15904 && flag_deduce_init_list
)
15905 parm
= listify (parm
);
15907 if (!is_std_init_list (parm
))
15908 /* We can only deduce from an initializer list argument if the
15909 parameter is std::initializer_list; otherwise this is a
15910 non-deduced context. */
15911 return unify_success (explain_p
);
15913 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
15915 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
15917 int elt_strict
= strict
;
15919 if (elt
== error_mark_node
)
15920 return unify_invalid (explain_p
);
15922 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
15924 tree type
= TREE_TYPE (elt
);
15925 /* It should only be possible to get here for a call. */
15926 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
15927 elt_strict
|= maybe_adjust_types_for_deduction
15928 (DEDUCE_CALL
, &elttype
, &type
, elt
);
15932 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
15936 /* If the std::initializer_list<T> deduction worked, replace the
15937 deduced A with std::initializer_list<A>. */
15938 if (orig_parm
!= parm
)
15940 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
15941 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
15942 targ
= listify (targ
);
15943 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
15945 return unify_success (explain_p
);
15948 /* Immediately reject some pairs that won't unify because of
15949 cv-qualification mismatches. */
15950 if (TREE_CODE (arg
) == TREE_CODE (parm
)
15952 /* It is the elements of the array which hold the cv quals of an array
15953 type, and the elements might be template type parms. We'll check
15954 when we recurse. */
15955 && TREE_CODE (arg
) != ARRAY_TYPE
15956 /* We check the cv-qualifiers when unifying with template type
15957 parameters below. We want to allow ARG `const T' to unify with
15958 PARM `T' for example, when computing which of two templates
15959 is more specialized, for example. */
15960 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
15961 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
15962 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
15964 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
15965 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
15966 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
15967 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
15968 strict
&= ~UNIFY_ALLOW_DERIVED
;
15969 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
15970 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
15972 switch (TREE_CODE (parm
))
15974 case TYPENAME_TYPE
:
15976 case UNBOUND_CLASS_TEMPLATE
:
15977 /* In a type which contains a nested-name-specifier, template
15978 argument values cannot be deduced for template parameters used
15979 within the nested-name-specifier. */
15980 return unify_success (explain_p
);
15982 case TEMPLATE_TYPE_PARM
:
15983 case TEMPLATE_TEMPLATE_PARM
:
15984 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15985 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
15986 if (tparm
== error_mark_node
)
15987 return unify_invalid (explain_p
);
15989 if (TEMPLATE_TYPE_LEVEL (parm
)
15990 != template_decl_level (tparm
))
15991 /* The PARM is not one we're trying to unify. Just check
15992 to see if it matches ARG. */
15994 if (TREE_CODE (arg
) == TREE_CODE (parm
)
15995 && (is_auto (parm
) ? is_auto (arg
)
15996 : same_type_p (parm
, arg
)))
15997 return unify_success (explain_p
);
15999 return unify_type_mismatch (explain_p
, parm
, arg
);
16001 idx
= TEMPLATE_TYPE_IDX (parm
);
16002 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16003 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
16004 if (tparm
== error_mark_node
)
16005 return unify_invalid (explain_p
);
16007 /* Check for mixed types and values. */
16008 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16009 && TREE_CODE (tparm
) != TYPE_DECL
)
16010 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16011 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
16012 gcc_unreachable ();
16014 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16016 /* ARG must be constructed from a template class or a template
16017 template parameter. */
16018 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
16019 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
16020 return unify_template_deduction_failure (explain_p
, parm
, arg
);
16023 tree parmvec
= TYPE_TI_ARGS (parm
);
16024 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
16025 tree full_argvec
= add_to_template_args (targs
, argvec
);
16027 = DECL_INNERMOST_TEMPLATE_PARMS
16028 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
16030 int parm_variadic_p
= 0;
16032 /* The resolution to DR150 makes clear that default
16033 arguments for an N-argument may not be used to bind T
16034 to a template template parameter with fewer than N
16035 parameters. It is not safe to permit the binding of
16036 default arguments as an extension, as that may change
16037 the meaning of a conforming program. Consider:
16039 struct Dense { static const unsigned int dim = 1; };
16041 template <template <typename> class View,
16043 void operator+(float, View<Block> const&);
16045 template <typename Block,
16046 unsigned int Dim = Block::dim>
16047 struct Lvalue_proxy { operator float() const; };
16051 Lvalue_proxy<Dense> p;
16056 Here, if Lvalue_proxy is permitted to bind to View, then
16057 the global operator+ will be used; if they are not, the
16058 Lvalue_proxy will be converted to float. */
16059 if (coerce_template_parms (parm_parms
,
16061 TYPE_TI_TEMPLATE (parm
),
16063 ? tf_warning_or_error
16065 /*require_all_args=*/true,
16066 /*use_default_args=*/false)
16067 == error_mark_node
)
16070 /* Deduce arguments T, i from TT<T> or TT<i>.
16071 We check each element of PARMVEC and ARGVEC individually
16072 rather than the whole TREE_VEC since they can have
16073 different number of elements. */
16075 parmvec
= expand_template_argument_pack (parmvec
);
16076 argvec
= expand_template_argument_pack (argvec
);
16078 len
= TREE_VEC_LENGTH (parmvec
);
16080 /* Check if the parameters end in a pack, making them
16083 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
16084 parm_variadic_p
= 1;
16086 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
16087 return unify_too_few_arguments (explain_p
,
16088 TREE_VEC_LENGTH (argvec
), len
);
16090 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16092 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
16093 TREE_VEC_ELT (parmvec
, i
),
16094 TREE_VEC_ELT (argvec
, i
),
16095 UNIFY_ALLOW_NONE
, explain_p
);
16098 if (parm_variadic_p
16099 && unify_pack_expansion (tparms
, targs
,
16102 /*subr=*/true, explain_p
))
16105 arg
= TYPE_TI_TEMPLATE (arg
);
16107 /* Fall through to deduce template name. */
16110 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16111 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16113 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
16115 /* Simple cases: Value already set, does match or doesn't. */
16116 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
16117 return unify_success (explain_p
);
16119 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
16123 /* If PARM is `const T' and ARG is only `int', we don't have
16124 a match unless we are allowing additional qualification.
16125 If ARG is `const int' and PARM is just `T' that's OK;
16126 that binds `const int' to `T'. */
16127 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
16129 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16131 /* Consider the case where ARG is `const volatile int' and
16132 PARM is `const T'. Then, T should be `volatile int'. */
16133 arg
= cp_build_qualified_type_real
16134 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
16135 if (arg
== error_mark_node
)
16136 return unify_invalid (explain_p
);
16138 /* Simple cases: Value already set, does match or doesn't. */
16139 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
16140 return unify_success (explain_p
);
16142 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
16144 /* Make sure that ARG is not a variable-sized array. (Note
16145 that were talking about variable-sized arrays (like
16146 `int[n]'), rather than arrays of unknown size (like
16147 `int[]').) We'll get very confused by such a type since
16148 the bound of the array is not constant, and therefore
16149 not mangleable. Besides, such types are not allowed in
16150 ISO C++, so we can do as we please here. We do allow
16151 them for 'auto' deduction, since that isn't ABI-exposed. */
16152 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
16153 return unify_vla_arg (explain_p
, arg
);
16155 /* Strip typedefs as in convert_template_argument. */
16156 arg
= canonicalize_type_argument (arg
, tf_none
);
16159 /* If ARG is a parameter pack or an expansion, we cannot unify
16160 against it unless PARM is also a parameter pack. */
16161 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
16162 && !template_parameter_pack_p (parm
))
16163 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
16165 /* If the argument deduction results is a METHOD_TYPE,
16166 then there is a problem.
16167 METHOD_TYPE doesn't map to any real C++ type the result of
16168 the deduction can not be of that type. */
16169 if (TREE_CODE (arg
) == METHOD_TYPE
)
16170 return unify_method_type_error (explain_p
, arg
);
16172 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
16173 return unify_success (explain_p
);
16175 case TEMPLATE_PARM_INDEX
:
16176 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
16177 if (tparm
== error_mark_node
)
16178 return unify_invalid (explain_p
);
16180 if (TEMPLATE_PARM_LEVEL (parm
)
16181 != template_decl_level (tparm
))
16183 /* The PARM is not one we're trying to unify. Just check
16184 to see if it matches ARG. */
16185 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
16186 && cp_tree_equal (parm
, arg
));
16188 unify_expression_unequal (explain_p
, parm
, arg
);
16192 idx
= TEMPLATE_PARM_IDX (parm
);
16193 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16197 int x
= !cp_tree_equal (targ
, arg
);
16199 unify_inconsistency (explain_p
, parm
, targ
, arg
);
16203 /* [temp.deduct.type] If, in the declaration of a function template
16204 with a non-type template-parameter, the non-type
16205 template-parameter is used in an expression in the function
16206 parameter-list and, if the corresponding template-argument is
16207 deduced, the template-argument type shall match the type of the
16208 template-parameter exactly, except that a template-argument
16209 deduced from an array bound may be of any integral type.
16210 The non-type parameter might use already deduced type parameters. */
16211 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
16212 if (!TREE_TYPE (arg
))
16213 /* Template-parameter dependent expression. Just accept it for now.
16214 It will later be processed in convert_template_argument. */
16216 else if (same_type_p (TREE_TYPE (arg
), tparm
))
16218 else if ((strict
& UNIFY_ALLOW_INTEGER
)
16219 && (TREE_CODE (tparm
) == INTEGER_TYPE
16220 || TREE_CODE (tparm
) == BOOLEAN_TYPE
))
16221 /* Convert the ARG to the type of PARM; the deduced non-type
16222 template argument must exactly match the types of the
16223 corresponding parameter. */
16224 arg
= fold (build_nop (tparm
, arg
));
16225 else if (uses_template_parms (tparm
))
16226 /* We haven't deduced the type of this parameter yet. Try again
16228 return unify_success (explain_p
);
16230 return unify_type_mismatch (explain_p
, tparm
, arg
);
16232 /* If ARG is a parameter pack or an expansion, we cannot unify
16233 against it unless PARM is also a parameter pack. */
16234 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
16235 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
16236 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
16238 arg
= strip_typedefs_expr (arg
);
16239 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
16240 return unify_success (explain_p
);
16244 /* A pointer-to-member constant can be unified only with
16245 another constant. */
16246 if (TREE_CODE (arg
) != PTRMEM_CST
)
16247 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
16249 /* Just unify the class member. It would be useless (and possibly
16250 wrong, depending on the strict flags) to unify also
16251 PTRMEM_CST_CLASS, because we want to be sure that both parm and
16252 arg refer to the same variable, even if through different
16253 classes. For instance:
16255 struct A { int x; };
16258 Unification of &A::x and &B::x must succeed. */
16259 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
16260 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
16265 if (TREE_CODE (arg
) != POINTER_TYPE
)
16266 return unify_type_mismatch (explain_p
, parm
, arg
);
16268 /* [temp.deduct.call]
16270 A can be another pointer or pointer to member type that can
16271 be converted to the deduced A via a qualification
16272 conversion (_conv.qual_).
16274 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
16275 This will allow for additional cv-qualification of the
16276 pointed-to types if appropriate. */
16278 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
16279 /* The derived-to-base conversion only persists through one
16280 level of pointers. */
16281 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
16283 return unify (tparms
, targs
, TREE_TYPE (parm
),
16284 TREE_TYPE (arg
), strict
, explain_p
);
16287 case REFERENCE_TYPE
:
16288 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
16289 return unify_type_mismatch (explain_p
, parm
, arg
);
16290 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16291 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
16294 if (TREE_CODE (arg
) != ARRAY_TYPE
)
16295 return unify_type_mismatch (explain_p
, parm
, arg
);
16296 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
16297 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
16298 return unify_type_mismatch (explain_p
, parm
, arg
);
16299 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
16306 /* Our representation of array types uses "N - 1" as the
16307 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
16308 not an integer constant. We cannot unify arbitrarily
16309 complex expressions, so we eliminate the MINUS_EXPRs
16311 parm_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (parm
));
16312 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
16315 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
16316 parm_max
= TREE_OPERAND (parm_max
, 0);
16318 arg_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (arg
));
16319 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
16322 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
16323 trying to unify the type of a variable with the type
16324 of a template parameter. For example:
16326 template <unsigned int N>
16327 void f (char (&) [N]);
16334 Here, the type of the ARG will be "int [g(i)]", and
16335 may be a SAVE_EXPR, etc. */
16336 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
16337 return unify_vla_arg (explain_p
, arg
);
16338 arg_max
= TREE_OPERAND (arg_max
, 0);
16341 /* If only one of the bounds used a MINUS_EXPR, compensate
16342 by adding one to the other bound. */
16343 if (parm_cst
&& !arg_cst
)
16344 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
16348 else if (arg_cst
&& !parm_cst
)
16349 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
16354 RECUR_AND_CHECK_FAILURE (tparms
, targs
, parm_max
, arg_max
,
16355 UNIFY_ALLOW_INTEGER
, explain_p
);
16357 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16358 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
16365 case ENUMERAL_TYPE
:
16368 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16369 return unify_type_mismatch (explain_p
, parm
, arg
);
16371 /* We have already checked cv-qualification at the top of the
16373 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
16374 return unify_type_mismatch (explain_p
, parm
, arg
);
16376 /* As far as unification is concerned, this wins. Later checks
16377 will invalidate it if necessary. */
16378 return unify_success (explain_p
);
16380 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
16381 /* Type INTEGER_CST can come from ordinary constant template args. */
16383 while (TREE_CODE (arg
) == NOP_EXPR
)
16384 arg
= TREE_OPERAND (arg
, 0);
16386 if (TREE_CODE (arg
) != INTEGER_CST
)
16387 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16388 return (tree_int_cst_equal (parm
, arg
)
16389 ? unify_success (explain_p
)
16390 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
16394 int i
, len
, argslen
;
16395 int parm_variadic_p
= 0;
16397 if (TREE_CODE (arg
) != TREE_VEC
)
16398 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16400 len
= TREE_VEC_LENGTH (parm
);
16401 argslen
= TREE_VEC_LENGTH (arg
);
16403 /* Check for pack expansions in the parameters. */
16404 for (i
= 0; i
< len
; ++i
)
16406 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
16409 /* We can unify against something with a trailing
16411 parm_variadic_p
= 1;
16413 /* [temp.deduct.type]/9: If the template argument list of
16414 P contains a pack expansion that is not the last
16415 template argument, the entire template argument list
16416 is a non-deduced context. */
16417 return unify_success (explain_p
);
16421 /* If we don't have enough arguments to satisfy the parameters
16422 (not counting the pack expression at the end), or we have
16423 too many arguments for a parameter list that doesn't end in
16424 a pack expression, we can't unify. */
16425 if (parm_variadic_p
16426 ? argslen
< len
- parm_variadic_p
16428 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
16430 /* Unify all of the parameters that precede the (optional)
16431 pack expression. */
16432 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16434 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
16435 TREE_VEC_ELT (parm
, i
),
16436 TREE_VEC_ELT (arg
, i
),
16437 UNIFY_ALLOW_NONE
, explain_p
);
16439 if (parm_variadic_p
)
16440 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
16442 /*subr=*/true, explain_p
);
16443 return unify_success (explain_p
);
16448 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16449 return unify_type_mismatch (explain_p
, parm
, arg
);
16451 if (TYPE_PTRMEMFUNC_P (parm
))
16453 if (!TYPE_PTRMEMFUNC_P (arg
))
16454 return unify_type_mismatch (explain_p
, parm
, arg
);
16456 return unify (tparms
, targs
,
16457 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
16458 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
16459 strict
, explain_p
);
16462 if (CLASSTYPE_TEMPLATE_INFO (parm
))
16464 tree t
= NULL_TREE
;
16466 if (strict_in
& UNIFY_ALLOW_DERIVED
)
16468 /* First, we try to unify the PARM and ARG directly. */
16469 t
= try_class_unification (tparms
, targs
,
16470 parm
, arg
, explain_p
);
16474 /* Fallback to the special case allowed in
16475 [temp.deduct.call]:
16477 If P is a class, and P has the form
16478 template-id, then A can be a derived class of
16479 the deduced A. Likewise, if P is a pointer to
16480 a class of the form template-id, A can be a
16481 pointer to a derived class pointed to by the
16483 enum template_base_result r
;
16484 r
= get_template_base (tparms
, targs
, parm
, arg
,
16488 return unify_no_common_base (explain_p
, r
, parm
, arg
);
16491 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
16492 && (CLASSTYPE_TI_TEMPLATE (parm
)
16493 == CLASSTYPE_TI_TEMPLATE (arg
)))
16494 /* Perhaps PARM is something like S<U> and ARG is S<int>.
16495 Then, we should unify `int' and `U'. */
16498 /* There's no chance of unification succeeding. */
16499 return unify_type_mismatch (explain_p
, parm
, arg
);
16501 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
16502 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
16504 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
16505 return unify_type_mismatch (explain_p
, parm
, arg
);
16506 return unify_success (explain_p
);
16509 case FUNCTION_TYPE
:
16511 unsigned int nargs
;
16516 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16517 return unify_type_mismatch (explain_p
, parm
, arg
);
16519 /* CV qualifications for methods can never be deduced, they must
16520 match exactly. We need to check them explicitly here,
16521 because type_unification_real treats them as any other
16522 cv-qualified parameter. */
16523 if (TREE_CODE (parm
) == METHOD_TYPE
16524 && (!check_cv_quals_for_unify
16526 class_of_this_parm (arg
),
16527 class_of_this_parm (parm
))))
16528 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16530 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
16531 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
16533 nargs
= list_length (TYPE_ARG_TYPES (arg
));
16534 args
= XALLOCAVEC (tree
, nargs
);
16535 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
16536 a
!= NULL_TREE
&& a
!= void_list_node
;
16537 a
= TREE_CHAIN (a
), ++i
)
16538 args
[i
] = TREE_VALUE (a
);
16541 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
16542 args
, nargs
, 1, DEDUCE_EXACT
,
16543 LOOKUP_NORMAL
, explain_p
);
16547 /* Unify a pointer to member with a pointer to member function, which
16548 deduces the type of the member as a function type. */
16549 if (TYPE_PTRMEMFUNC_P (arg
))
16554 /* Check top-level cv qualifiers */
16555 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
16556 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16558 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
16559 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
16560 UNIFY_ALLOW_NONE
, explain_p
);
16562 /* Determine the type of the function we are unifying against. */
16563 method_type
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg
));
16565 build_function_type (TREE_TYPE (method_type
),
16566 TREE_CHAIN (TYPE_ARG_TYPES (method_type
)));
16568 /* Extract the cv-qualifiers of the member function from the
16569 implicit object parameter and place them on the function
16570 type to be restored later. */
16571 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (method_type
));
16572 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
16575 if (TREE_CODE (arg
) != OFFSET_TYPE
)
16576 return unify_type_mismatch (explain_p
, parm
, arg
);
16577 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
16578 TYPE_OFFSET_BASETYPE (arg
),
16579 UNIFY_ALLOW_NONE
, explain_p
);
16580 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16581 strict
, explain_p
);
16584 if (DECL_TEMPLATE_PARM_P (parm
))
16585 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
16586 if (arg
!= integral_constant_value (parm
))
16587 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16588 return unify_success (explain_p
);
16591 case TEMPLATE_DECL
:
16592 /* Matched cases are handled by the ARG == PARM test above. */
16593 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16596 /* A non-type template parameter that is a variable should be a
16597 an integral constant, in which case, it whould have been
16598 folded into its (constant) value. So we should not be getting
16599 a variable here. */
16600 gcc_unreachable ();
16602 case TYPE_ARGUMENT_PACK
:
16603 case NONTYPE_ARGUMENT_PACK
:
16604 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
16605 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
16608 case DECLTYPE_TYPE
:
16609 case UNDERLYING_TYPE
:
16610 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
16611 or UNDERLYING_TYPE nodes. */
16612 return unify_success (explain_p
);
16615 /* Unification fails if we hit an error node. */
16616 return unify_invalid (explain_p
);
16619 /* An unresolved overload is a nondeduced context. */
16620 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
16621 return unify_success (explain_p
);
16622 gcc_assert (EXPR_P (parm
));
16624 /* We must be looking at an expression. This can happen with
16628 void foo(S<I>, S<I + 2>);
16630 This is a "nondeduced context":
16634 The nondeduced contexts are:
16636 --A type that is a template-id in which one or more of
16637 the template-arguments is an expression that references
16638 a template-parameter.
16640 In these cases, we assume deduction succeeded, but don't
16641 actually infer any unifications. */
16643 if (!uses_template_parms (parm
)
16644 && !template_args_equal (parm
, arg
))
16645 return unify_expression_unequal (explain_p
, parm
, arg
);
16647 return unify_success (explain_p
);
16650 #undef RECUR_AND_CHECK_FAILURE
16652 /* Note that DECL can be defined in this translation unit, if
16656 mark_definable (tree decl
)
16659 DECL_NOT_REALLY_EXTERN (decl
) = 1;
16660 FOR_EACH_CLONE (clone
, decl
)
16661 DECL_NOT_REALLY_EXTERN (clone
) = 1;
16664 /* Called if RESULT is explicitly instantiated, or is a member of an
16665 explicitly instantiated class. */
16668 mark_decl_instantiated (tree result
, int extern_p
)
16670 SET_DECL_EXPLICIT_INSTANTIATION (result
);
16672 /* If this entity has already been written out, it's too late to
16673 make any modifications. */
16674 if (TREE_ASM_WRITTEN (result
))
16677 if (TREE_CODE (result
) != FUNCTION_DECL
)
16678 /* The TREE_PUBLIC flag for function declarations will have been
16679 set correctly by tsubst. */
16680 TREE_PUBLIC (result
) = 1;
16682 /* This might have been set by an earlier implicit instantiation. */
16683 DECL_COMDAT (result
) = 0;
16686 DECL_NOT_REALLY_EXTERN (result
) = 0;
16689 mark_definable (result
);
16690 /* Always make artificials weak. */
16691 if (DECL_ARTIFICIAL (result
) && flag_weak
)
16692 comdat_linkage (result
);
16693 /* For WIN32 we also want to put explicit instantiations in
16694 linkonce sections. */
16695 else if (TREE_PUBLIC (result
))
16696 maybe_make_one_only (result
);
16699 /* If EXTERN_P, then this function will not be emitted -- unless
16700 followed by an explicit instantiation, at which point its linkage
16701 will be adjusted. If !EXTERN_P, then this function will be
16702 emitted here. In neither circumstance do we want
16703 import_export_decl to adjust the linkage. */
16704 DECL_INTERFACE_KNOWN (result
) = 1;
16707 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
16708 important template arguments. If any are missing, we check whether
16709 they're important by using error_mark_node for substituting into any
16710 args that were used for partial ordering (the ones between ARGS and END)
16711 and seeing if it bubbles up. */
16714 check_undeduced_parms (tree targs
, tree args
, tree end
)
16716 bool found
= false;
16718 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
16719 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
16722 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
16726 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
16727 if (substed
== error_mark_node
)
16733 /* Given two function templates PAT1 and PAT2, return:
16735 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
16736 -1 if PAT2 is more specialized than PAT1.
16737 0 if neither is more specialized.
16739 LEN indicates the number of parameters we should consider
16740 (defaulted parameters should not be considered).
16742 The 1998 std underspecified function template partial ordering, and
16743 DR214 addresses the issue. We take pairs of arguments, one from
16744 each of the templates, and deduce them against each other. One of
16745 the templates will be more specialized if all the *other*
16746 template's arguments deduce against its arguments and at least one
16747 of its arguments *does* *not* deduce against the other template's
16748 corresponding argument. Deduction is done as for class templates.
16749 The arguments used in deduction have reference and top level cv
16750 qualifiers removed. Iff both arguments were originally reference
16751 types *and* deduction succeeds in both directions, the template
16752 with the more cv-qualified argument wins for that pairing (if
16753 neither is more cv-qualified, they both are equal). Unlike regular
16754 deduction, after all the arguments have been deduced in this way,
16755 we do *not* verify the deduced template argument values can be
16756 substituted into non-deduced contexts.
16758 The logic can be a bit confusing here, because we look at deduce1 and
16759 targs1 to see if pat2 is at least as specialized, and vice versa; if we
16760 can find template arguments for pat1 to make arg1 look like arg2, that
16761 means that arg2 is at least as specialized as arg1. */
16764 more_specialized_fn (tree pat1
, tree pat2
, int len
)
16766 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
16767 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
16768 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
16769 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
16770 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
16771 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
16772 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
16773 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
16774 tree origs1
, origs2
;
16775 bool lose1
= false;
16776 bool lose2
= false;
16778 /* Remove the this parameter from non-static member functions. If
16779 one is a non-static member function and the other is not a static
16780 member function, remove the first parameter from that function
16781 also. This situation occurs for operator functions where we
16782 locate both a member function (with this pointer) and non-member
16783 operator (with explicit first operand). */
16784 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
16786 len
--; /* LEN is the number of significant arguments for DECL1 */
16787 args1
= TREE_CHAIN (args1
);
16788 if (!DECL_STATIC_FUNCTION_P (decl2
))
16789 args2
= TREE_CHAIN (args2
);
16791 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
16793 args2
= TREE_CHAIN (args2
);
16794 if (!DECL_STATIC_FUNCTION_P (decl1
))
16797 args1
= TREE_CHAIN (args1
);
16801 /* If only one is a conversion operator, they are unordered. */
16802 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
16805 /* Consider the return type for a conversion function */
16806 if (DECL_CONV_FN_P (decl1
))
16808 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
16809 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
16813 processing_template_decl
++;
16819 /* Stop when an ellipsis is seen. */
16820 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
16822 tree arg1
= TREE_VALUE (args1
);
16823 tree arg2
= TREE_VALUE (args2
);
16824 int deduce1
, deduce2
;
16828 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
16829 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
16831 /* When both arguments are pack expansions, we need only
16832 unify the patterns themselves. */
16833 arg1
= PACK_EXPANSION_PATTERN (arg1
);
16834 arg2
= PACK_EXPANSION_PATTERN (arg2
);
16836 /* This is the last comparison we need to do. */
16840 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
16842 arg1
= TREE_TYPE (arg1
);
16843 quals1
= cp_type_quals (arg1
);
16846 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
16848 arg2
= TREE_TYPE (arg2
);
16849 quals2
= cp_type_quals (arg2
);
16852 arg1
= TYPE_MAIN_VARIANT (arg1
);
16853 arg2
= TYPE_MAIN_VARIANT (arg2
);
16855 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
16857 int i
, len2
= list_length (args2
);
16858 tree parmvec
= make_tree_vec (1);
16859 tree argvec
= make_tree_vec (len2
);
16862 /* Setup the parameter vector, which contains only ARG1. */
16863 TREE_VEC_ELT (parmvec
, 0) = arg1
;
16865 /* Setup the argument vector, which contains the remaining
16867 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
16868 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
16870 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
16871 argvec
, DEDUCE_EXACT
,
16872 /*subr=*/true, /*explain_p=*/false)
16875 /* We cannot deduce in the other direction, because ARG1 is
16876 a pack expansion but ARG2 is not. */
16879 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
16881 int i
, len1
= list_length (args1
);
16882 tree parmvec
= make_tree_vec (1);
16883 tree argvec
= make_tree_vec (len1
);
16886 /* Setup the parameter vector, which contains only ARG1. */
16887 TREE_VEC_ELT (parmvec
, 0) = arg2
;
16889 /* Setup the argument vector, which contains the remaining
16891 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
16892 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
16894 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
16895 argvec
, DEDUCE_EXACT
,
16896 /*subr=*/true, /*explain_p=*/false)
16899 /* We cannot deduce in the other direction, because ARG2 is
16900 a pack expansion but ARG1 is not.*/
16906 /* The normal case, where neither argument is a pack
16908 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
16909 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
16911 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
16912 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
16916 /* If we couldn't deduce arguments for tparms1 to make arg1 match
16917 arg2, then arg2 is not as specialized as arg1. */
16923 /* "If, for a given type, deduction succeeds in both directions
16924 (i.e., the types are identical after the transformations above)
16925 and if the type from the argument template is more cv-qualified
16926 than the type from the parameter template (as described above)
16927 that type is considered to be more specialized than the other. If
16928 neither type is more cv-qualified than the other then neither type
16929 is more specialized than the other." */
16931 if (deduce1
&& deduce2
16932 && quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
16934 if ((quals1
& quals2
) == quals2
)
16936 if ((quals1
& quals2
) == quals1
)
16940 if (lose1
&& lose2
)
16941 /* We've failed to deduce something in either direction.
16942 These must be unordered. */
16945 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
16946 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
16947 /* We have already processed all of the arguments in our
16948 handing of the pack expansion type. */
16951 args1
= TREE_CHAIN (args1
);
16952 args2
= TREE_CHAIN (args2
);
16955 /* "In most cases, all template parameters must have values in order for
16956 deduction to succeed, but for partial ordering purposes a template
16957 parameter may remain without a value provided it is not used in the
16958 types being used for partial ordering."
16960 Thus, if we are missing any of the targs1 we need to substitute into
16961 origs1, then pat2 is not as specialized as pat1. This can happen when
16962 there is a nondeduced context. */
16963 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
16965 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
16968 processing_template_decl
--;
16970 /* All things being equal, if the next argument is a pack expansion
16971 for one function but not for the other, prefer the
16972 non-variadic function. FIXME this is bogus; see c++/41958. */
16974 && args1
&& TREE_VALUE (args1
)
16975 && args2
&& TREE_VALUE (args2
))
16977 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
16978 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
16981 if (lose1
== lose2
)
16989 /* Determine which of two partial specializations is more specialized.
16991 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
16992 to the first partial specialization. The TREE_VALUE is the
16993 innermost set of template parameters for the partial
16994 specialization. PAT2 is similar, but for the second template.
16996 Return 1 if the first partial specialization is more specialized;
16997 -1 if the second is more specialized; 0 if neither is more
17000 See [temp.class.order] for information about determining which of
17001 two templates is more specialized. */
17004 more_specialized_class (tree pat1
, tree pat2
)
17009 bool any_deductions
= false;
17011 tmpl1
= TREE_TYPE (pat1
);
17012 tmpl2
= TREE_TYPE (pat2
);
17014 /* Just like what happens for functions, if we are ordering between
17015 different class template specializations, we may encounter dependent
17016 types in the arguments, and we need our dependency check functions
17017 to behave correctly. */
17018 ++processing_template_decl
;
17019 targs
= get_class_bindings (TREE_VALUE (pat1
),
17020 CLASSTYPE_TI_ARGS (tmpl1
),
17021 CLASSTYPE_TI_ARGS (tmpl2
));
17025 any_deductions
= true;
17028 targs
= get_class_bindings (TREE_VALUE (pat2
),
17029 CLASSTYPE_TI_ARGS (tmpl2
),
17030 CLASSTYPE_TI_ARGS (tmpl1
));
17034 any_deductions
= true;
17036 --processing_template_decl
;
17038 /* In the case of a tie where at least one of the class templates
17039 has a parameter pack at the end, the template with the most
17040 non-packed parameters wins. */
17043 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
17044 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
17046 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
17047 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
17048 int len1
= TREE_VEC_LENGTH (args1
);
17049 int len2
= TREE_VEC_LENGTH (args2
);
17051 /* We don't count the pack expansion at the end. */
17052 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
17054 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
17059 else if (len1
< len2
)
17066 /* Return the template arguments that will produce the function signature
17067 DECL from the function template FN, with the explicit template
17068 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
17069 also match. Return NULL_TREE if no satisfactory arguments could be
17073 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
17075 int ntparms
= DECL_NTPARMS (fn
);
17076 tree targs
= make_tree_vec (ntparms
);
17078 tree decl_arg_types
;
17080 unsigned int nargs
, ix
;
17083 /* Substitute the explicit template arguments into the type of DECL.
17084 The call to fn_type_unification will handle substitution into the
17086 decl_type
= TREE_TYPE (decl
);
17087 if (explicit_args
&& decl
== DECL_TEMPLATE_RESULT (fn
))
17090 tree converted_args
;
17092 if (DECL_TEMPLATE_INFO (decl
))
17093 tmpl
= DECL_TI_TEMPLATE (decl
);
17095 /* We can get here for some invalid specializations. */
17099 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
17100 explicit_args
, NULL_TREE
,
17102 /*require_all_args=*/false,
17103 /*use_default_args=*/false);
17104 if (converted_args
== error_mark_node
)
17107 decl_type
= tsubst (decl_type
, converted_args
, tf_none
, NULL_TREE
);
17108 if (decl_type
== error_mark_node
)
17112 /* Never do unification on the 'this' parameter. */
17113 decl_arg_types
= skip_artificial_parms_for (decl
,
17114 TYPE_ARG_TYPES (decl_type
));
17116 nargs
= list_length (decl_arg_types
);
17117 args
= XALLOCAVEC (tree
, nargs
);
17118 for (arg
= decl_arg_types
, ix
= 0;
17119 arg
!= NULL_TREE
&& arg
!= void_list_node
;
17120 arg
= TREE_CHAIN (arg
), ++ix
)
17121 args
[ix
] = TREE_VALUE (arg
);
17123 if (fn_type_unification (fn
, explicit_args
, targs
,
17125 (check_rettype
|| DECL_CONV_FN_P (fn
)
17126 ? TREE_TYPE (decl_type
) : NULL_TREE
),
17127 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false))
17133 /* Return the innermost template arguments that, when applied to a
17134 template specialization whose innermost template parameters are
17135 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
17138 For example, suppose we have:
17140 template <class T, class U> struct S {};
17141 template <class T> struct S<T*, int> {};
17143 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
17144 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
17145 int}. The resulting vector will be {double}, indicating that `T'
17146 is bound to `double'. */
17149 get_class_bindings (tree tparms
, tree spec_args
, tree args
)
17151 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
17153 tree innermost_deduced_args
;
17155 innermost_deduced_args
= make_tree_vec (ntparms
);
17156 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
17158 deduced_args
= copy_node (args
);
17159 SET_TMPL_ARGS_LEVEL (deduced_args
,
17160 TMPL_ARGS_DEPTH (deduced_args
),
17161 innermost_deduced_args
);
17164 deduced_args
= innermost_deduced_args
;
17166 if (unify (tparms
, deduced_args
,
17167 INNERMOST_TEMPLATE_ARGS (spec_args
),
17168 INNERMOST_TEMPLATE_ARGS (args
),
17169 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
17172 for (i
= 0; i
< ntparms
; ++i
)
17173 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
17176 /* Verify that nondeduced template arguments agree with the type
17177 obtained from argument deduction.
17181 struct A { typedef int X; };
17182 template <class T, class U> struct C {};
17183 template <class T> struct C<T, typename T::X> {};
17185 Then with the instantiation `C<A, int>', we can deduce that
17186 `T' is `A' but unify () does not check whether `typename T::X'
17188 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
17189 if (spec_args
== error_mark_node
17190 /* We only need to check the innermost arguments; the other
17191 arguments will always agree. */
17192 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
17193 INNERMOST_TEMPLATE_ARGS (args
)))
17196 /* Now that we have bindings for all of the template arguments,
17197 ensure that the arguments deduced for the template template
17198 parameters have compatible template parameter lists. See the use
17199 of template_template_parm_bindings_ok_p in fn_type_unification
17200 for more information. */
17201 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
17204 return deduced_args
;
17207 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
17208 Return the TREE_LIST node with the most specialized template, if
17209 any. If there is no most specialized template, the error_mark_node
17212 Note that this function does not look at, or modify, the
17213 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
17214 returned is one of the elements of INSTANTIATIONS, callers may
17215 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
17216 and retrieve it from the value returned. */
17219 most_specialized_instantiation (tree templates
)
17223 ++processing_template_decl
;
17226 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
17230 if (get_bindings (TREE_VALUE (champ
),
17231 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
17232 NULL_TREE
, /*check_ret=*/true))
17235 if (get_bindings (TREE_VALUE (fn
),
17236 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
17237 NULL_TREE
, /*check_ret=*/true))
17244 /* Equally specialized, move to next function. If there
17245 is no next function, nothing's most specialized. */
17246 fn
= TREE_CHAIN (fn
);
17254 /* Now verify that champ is better than everything earlier in the
17255 instantiation list. */
17256 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
17257 if (get_bindings (TREE_VALUE (champ
),
17258 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
17259 NULL_TREE
, /*check_ret=*/true)
17260 || !get_bindings (TREE_VALUE (fn
),
17261 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
17262 NULL_TREE
, /*check_ret=*/true))
17268 processing_template_decl
--;
17271 return error_mark_node
;
17276 /* If DECL is a specialization of some template, return the most
17277 general such template. Otherwise, returns NULL_TREE.
17279 For example, given:
17281 template <class T> struct S { template <class U> void f(U); };
17283 if TMPL is `template <class U> void S<int>::f(U)' this will return
17284 the full template. This function will not trace past partial
17285 specializations, however. For example, given in addition:
17287 template <class T> struct S<T*> { template <class U> void f(U); };
17289 if TMPL is `template <class U> void S<int*>::f(U)' this will return
17290 `template <class T> template <class U> S<T*>::f(U)'. */
17293 most_general_template (tree decl
)
17295 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
17296 an immediate specialization. */
17297 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17299 if (DECL_TEMPLATE_INFO (decl
)) {
17300 decl
= DECL_TI_TEMPLATE (decl
);
17302 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
17303 template friend. */
17304 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
17310 /* Look for more and more general templates. */
17311 while (DECL_TEMPLATE_INFO (decl
))
17313 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
17314 (See cp-tree.h for details.) */
17315 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
17318 if (CLASS_TYPE_P (TREE_TYPE (decl
))
17319 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
17322 /* Stop if we run into an explicitly specialized class template. */
17323 if (!DECL_NAMESPACE_SCOPE_P (decl
)
17324 && DECL_CONTEXT (decl
)
17325 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
17328 decl
= DECL_TI_TEMPLATE (decl
);
17334 /* Return the most specialized of the class template partial
17335 specializations of TMPL which can produce TYPE, a specialization of
17336 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
17337 a _TYPE node corresponding to the partial specialization, while the
17338 TREE_PURPOSE is the set of template arguments that must be
17339 substituted into the TREE_TYPE in order to generate TYPE.
17341 If the choice of partial specialization is ambiguous, a diagnostic
17342 is issued, and the error_mark_node is returned. If there are no
17343 partial specializations of TMPL matching TYPE, then NULL_TREE is
17347 most_specialized_class (tree type
, tree tmpl
, tsubst_flags_t complain
)
17349 tree list
= NULL_TREE
;
17355 tree outer_args
= NULL_TREE
;
17357 tmpl
= most_general_template (tmpl
);
17358 args
= CLASSTYPE_TI_ARGS (type
);
17360 /* For determining which partial specialization to use, only the
17361 innermost args are interesting. */
17362 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
17364 outer_args
= strip_innermost_template_args (args
, 1);
17365 args
= INNERMOST_TEMPLATE_ARGS (args
);
17368 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
17370 tree partial_spec_args
;
17372 tree parms
= TREE_VALUE (t
);
17374 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
17376 ++processing_template_decl
;
17382 /* Discard the outer levels of args, and then substitute in the
17383 template args from the enclosing class. */
17384 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
17385 partial_spec_args
= tsubst_template_args
17386 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
17388 /* PARMS already refers to just the innermost parms, but the
17389 template parms in partial_spec_args had their levels lowered
17390 by tsubst, so we need to do the same for the parm list. We
17391 can't just tsubst the TREE_VEC itself, as tsubst wants to
17392 treat a TREE_VEC as an argument vector. */
17393 parms
= copy_node (parms
);
17394 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
17395 TREE_VEC_ELT (parms
, i
) =
17396 tsubst (TREE_VEC_ELT (parms
, i
), outer_args
, tf_none
, NULL_TREE
);
17400 partial_spec_args
=
17401 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
17402 add_to_template_args (outer_args
,
17403 partial_spec_args
),
17405 /*require_all_args=*/true,
17406 /*use_default_args=*/true);
17408 --processing_template_decl
;
17410 if (partial_spec_args
== error_mark_node
)
17411 return error_mark_node
;
17413 spec_args
= get_class_bindings (parms
,
17419 spec_args
= add_to_template_args (outer_args
, spec_args
);
17420 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
17421 TREE_TYPE (list
) = TREE_TYPE (t
);
17428 ambiguous_p
= false;
17431 t
= TREE_CHAIN (t
);
17432 for (; t
; t
= TREE_CHAIN (t
))
17434 fate
= more_specialized_class (champ
, t
);
17441 t
= TREE_CHAIN (t
);
17444 ambiguous_p
= true;
17453 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
17455 fate
= more_specialized_class (champ
, t
);
17458 ambiguous_p
= true;
17466 char *spaces
= NULL
;
17467 if (!(complain
& tf_error
))
17468 return error_mark_node
;
17469 error ("ambiguous class template instantiation for %q#T", type
);
17470 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
17471 for (t
= list
; t
; t
= TREE_CHAIN (t
))
17473 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
17474 spaces
= spaces
? spaces
: get_spaces (str
);
17477 return error_mark_node
;
17483 /* Explicitly instantiate DECL. */
17486 do_decl_instantiation (tree decl
, tree storage
)
17488 tree result
= NULL_TREE
;
17491 if (!decl
|| decl
== error_mark_node
)
17492 /* An error occurred, for which grokdeclarator has already issued
17493 an appropriate message. */
17495 else if (! DECL_LANG_SPECIFIC (decl
))
17497 error ("explicit instantiation of non-template %q#D", decl
);
17500 else if (TREE_CODE (decl
) == VAR_DECL
)
17502 /* There is an asymmetry here in the way VAR_DECLs and
17503 FUNCTION_DECLs are handled by grokdeclarator. In the case of
17504 the latter, the DECL we get back will be marked as a
17505 template instantiation, and the appropriate
17506 DECL_TEMPLATE_INFO will be set up. This does not happen for
17507 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
17508 should handle VAR_DECLs as it currently handles
17510 if (!DECL_CLASS_SCOPE_P (decl
))
17512 error ("%qD is not a static data member of a class template", decl
);
17515 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
17516 if (!result
|| TREE_CODE (result
) != VAR_DECL
)
17518 error ("no matching template for %qD found", decl
);
17521 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
17523 error ("type %qT for explicit instantiation %qD does not match "
17524 "declared type %qT", TREE_TYPE (result
), decl
,
17529 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
17531 error ("explicit instantiation of %q#D", decl
);
17537 /* Check for various error cases. Note that if the explicit
17538 instantiation is valid the RESULT will currently be marked as an
17539 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
17540 until we get here. */
17542 if (DECL_TEMPLATE_SPECIALIZATION (result
))
17544 /* DR 259 [temp.spec].
17546 Both an explicit instantiation and a declaration of an explicit
17547 specialization shall not appear in a program unless the explicit
17548 instantiation follows a declaration of the explicit specialization.
17550 For a given set of template parameters, if an explicit
17551 instantiation of a template appears after a declaration of an
17552 explicit specialization for that template, the explicit
17553 instantiation has no effect. */
17556 else if (DECL_EXPLICIT_INSTANTIATION (result
))
17560 No program shall explicitly instantiate any template more
17563 We check DECL_NOT_REALLY_EXTERN so as not to complain when
17564 the first instantiation was `extern' and the second is not,
17565 and EXTERN_P for the opposite case. */
17566 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
17567 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
17568 /* If an "extern" explicit instantiation follows an ordinary
17569 explicit instantiation, the template is instantiated. */
17573 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
17575 error ("no matching template for %qD found", result
);
17578 else if (!DECL_TEMPLATE_INFO (result
))
17580 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
17584 if (storage
== NULL_TREE
)
17586 else if (storage
== ridpointers
[(int) RID_EXTERN
])
17588 if (!in_system_header
&& (cxx_dialect
== cxx98
))
17589 pedwarn (input_location
, OPT_Wpedantic
,
17590 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
17595 error ("storage class %qD applied to template instantiation", storage
);
17597 check_explicit_instantiation_namespace (result
);
17598 mark_decl_instantiated (result
, extern_p
);
17600 instantiate_decl (result
, /*defer_ok=*/1,
17601 /*expl_inst_class_mem_p=*/false);
17605 mark_class_instantiated (tree t
, int extern_p
)
17607 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
17608 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
17609 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
17610 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
17613 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
17614 rest_of_type_compilation (t
, 1);
17618 /* Called from do_type_instantiation through binding_table_foreach to
17619 do recursive instantiation for the type bound in ENTRY. */
17621 bt_instantiate_type_proc (binding_entry entry
, void *data
)
17623 tree storage
= *(tree
*) data
;
17625 if (MAYBE_CLASS_TYPE_P (entry
->type
)
17626 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
17627 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
17630 /* Called from do_type_instantiation to instantiate a member
17631 (a member function or a static member variable) of an
17632 explicitly instantiated class template. */
17634 instantiate_class_member (tree decl
, int extern_p
)
17636 mark_decl_instantiated (decl
, extern_p
);
17638 instantiate_decl (decl
, /*defer_ok=*/1,
17639 /*expl_inst_class_mem_p=*/true);
17642 /* Perform an explicit instantiation of template class T. STORAGE, if
17643 non-null, is the RID for extern, inline or static. COMPLAIN is
17644 nonzero if this is called from the parser, zero if called recursively,
17645 since the standard is unclear (as detailed below). */
17648 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
17653 int previous_instantiation_extern_p
= 0;
17655 if (TREE_CODE (t
) == TYPE_DECL
)
17658 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
17661 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
17663 error ("explicit instantiation of non-class template %qD", tmpl
);
17665 error ("explicit instantiation of non-template type %qT", t
);
17671 if (!COMPLETE_TYPE_P (t
))
17673 if (complain
& tf_error
)
17674 error ("explicit instantiation of %q#T before definition of template",
17679 if (storage
!= NULL_TREE
)
17681 if (!in_system_header
)
17683 if (storage
== ridpointers
[(int) RID_EXTERN
])
17685 if (cxx_dialect
== cxx98
)
17686 pedwarn (input_location
, OPT_Wpedantic
,
17687 "ISO C++ 1998 forbids the use of %<extern%> on "
17688 "explicit instantiations");
17691 pedwarn (input_location
, OPT_Wpedantic
,
17692 "ISO C++ forbids the use of %qE"
17693 " on explicit instantiations", storage
);
17696 if (storage
== ridpointers
[(int) RID_INLINE
])
17698 else if (storage
== ridpointers
[(int) RID_EXTERN
])
17700 else if (storage
== ridpointers
[(int) RID_STATIC
])
17704 error ("storage class %qD applied to template instantiation",
17710 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
17712 /* DR 259 [temp.spec].
17714 Both an explicit instantiation and a declaration of an explicit
17715 specialization shall not appear in a program unless the explicit
17716 instantiation follows a declaration of the explicit specialization.
17718 For a given set of template parameters, if an explicit
17719 instantiation of a template appears after a declaration of an
17720 explicit specialization for that template, the explicit
17721 instantiation has no effect. */
17724 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
17728 No program shall explicitly instantiate any template more
17731 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
17732 instantiation was `extern'. If EXTERN_P then the second is.
17733 These cases are OK. */
17734 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
17736 if (!previous_instantiation_extern_p
&& !extern_p
17737 && (complain
& tf_error
))
17738 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
17740 /* If we've already instantiated the template, just return now. */
17741 if (!CLASSTYPE_INTERFACE_ONLY (t
))
17745 check_explicit_instantiation_namespace (TYPE_NAME (t
));
17746 mark_class_instantiated (t
, extern_p
);
17754 /* In contrast to implicit instantiation, where only the
17755 declarations, and not the definitions, of members are
17756 instantiated, we have here:
17760 The explicit instantiation of a class template specialization
17761 implies the instantiation of all of its members not
17762 previously explicitly specialized in the translation unit
17763 containing the explicit instantiation.
17765 Of course, we can't instantiate member template classes, since
17766 we don't have any arguments for them. Note that the standard
17767 is unclear on whether the instantiation of the members are
17768 *explicit* instantiations or not. However, the most natural
17769 interpretation is that it should be an explicit instantiation. */
17772 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
17773 if (TREE_CODE (tmp
) == FUNCTION_DECL
17774 && DECL_TEMPLATE_INSTANTIATION (tmp
))
17775 instantiate_class_member (tmp
, extern_p
);
17777 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
17778 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
17779 instantiate_class_member (tmp
, extern_p
);
17781 if (CLASSTYPE_NESTED_UTDS (t
))
17782 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
17783 bt_instantiate_type_proc
, &storage
);
17787 /* Given a function DECL, which is a specialization of TMPL, modify
17788 DECL to be a re-instantiation of TMPL with the same template
17789 arguments. TMPL should be the template into which tsubst'ing
17790 should occur for DECL, not the most general template.
17792 One reason for doing this is a scenario like this:
17795 void f(const T&, int i);
17797 void g() { f(3, 7); }
17800 void f(const T& t, const int i) { }
17802 Note that when the template is first instantiated, with
17803 instantiate_template, the resulting DECL will have no name for the
17804 first parameter, and the wrong type for the second. So, when we go
17805 to instantiate the DECL, we regenerate it. */
17808 regenerate_decl_from_template (tree decl
, tree tmpl
)
17810 /* The arguments used to instantiate DECL, from the most general
17815 args
= DECL_TI_ARGS (decl
);
17816 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
17818 /* Make sure that we can see identifiers, and compute access
17820 push_access_scope (decl
);
17822 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17830 args_depth
= TMPL_ARGS_DEPTH (args
);
17831 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
17832 if (args_depth
> parms_depth
)
17833 args
= get_innermost_template_args (args
, parms_depth
);
17835 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
17836 args
, tf_error
, NULL_TREE
,
17837 /*defer_ok*/false);
17838 if (specs
&& specs
!= error_mark_node
)
17839 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
17842 /* Merge parameter declarations. */
17843 decl_parm
= skip_artificial_parms_for (decl
,
17844 DECL_ARGUMENTS (decl
));
17846 = skip_artificial_parms_for (code_pattern
,
17847 DECL_ARGUMENTS (code_pattern
));
17848 while (decl_parm
&& !FUNCTION_PARAMETER_PACK_P (pattern_parm
))
17853 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
17854 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
17855 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
17857 parm_type
= type_decays_to (parm_type
);
17858 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
17859 TREE_TYPE (decl_parm
) = parm_type
;
17860 attributes
= DECL_ATTRIBUTES (pattern_parm
);
17861 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
17863 DECL_ATTRIBUTES (decl_parm
) = attributes
;
17864 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
17866 decl_parm
= DECL_CHAIN (decl_parm
);
17867 pattern_parm
= DECL_CHAIN (pattern_parm
);
17869 /* Merge any parameters that match with the function parameter
17871 if (pattern_parm
&& FUNCTION_PARAMETER_PACK_P (pattern_parm
))
17874 tree expanded_types
;
17875 /* Expand the TYPE_PACK_EXPANSION that provides the types for
17876 the parameters in this function parameter pack. */
17877 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
17878 args
, tf_error
, NULL_TREE
);
17879 len
= TREE_VEC_LENGTH (expanded_types
);
17880 for (i
= 0; i
< len
; i
++)
17885 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
17886 /* Rename the parameter to include the index. */
17887 DECL_NAME (decl_parm
) =
17888 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
17889 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
17890 parm_type
= type_decays_to (parm_type
);
17891 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
17892 TREE_TYPE (decl_parm
) = parm_type
;
17893 attributes
= DECL_ATTRIBUTES (pattern_parm
);
17894 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
17896 DECL_ATTRIBUTES (decl_parm
) = attributes
;
17897 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
17899 decl_parm
= DECL_CHAIN (decl_parm
);
17902 /* Merge additional specifiers from the CODE_PATTERN. */
17903 if (DECL_DECLARED_INLINE_P (code_pattern
)
17904 && !DECL_DECLARED_INLINE_P (decl
))
17905 DECL_DECLARED_INLINE_P (decl
) = 1;
17907 else if (TREE_CODE (decl
) == VAR_DECL
)
17909 DECL_INITIAL (decl
) =
17910 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
17911 tf_error
, DECL_TI_TEMPLATE (decl
),
17912 /*integral_constant_expression_p=*/false);
17913 if (VAR_HAD_UNKNOWN_BOUND (decl
))
17914 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
17915 tf_error
, DECL_TI_TEMPLATE (decl
));
17918 gcc_unreachable ();
17920 pop_access_scope (decl
);
17923 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
17924 substituted to get DECL. */
17927 template_for_substitution (tree decl
)
17929 tree tmpl
= DECL_TI_TEMPLATE (decl
);
17931 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
17932 for the instantiation. This is not always the most general
17933 template. Consider, for example:
17936 struct S { template <class U> void f();
17937 template <> void f<int>(); };
17939 and an instantiation of S<double>::f<int>. We want TD to be the
17940 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
17941 while (/* An instantiation cannot have a definition, so we need a
17942 more general template. */
17943 DECL_TEMPLATE_INSTANTIATION (tmpl
)
17944 /* We must also deal with friend templates. Given:
17946 template <class T> struct S {
17947 template <class U> friend void f() {};
17950 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
17951 so far as the language is concerned, but that's still
17952 where we get the pattern for the instantiation from. On
17953 other hand, if the definition comes outside the class, say:
17955 template <class T> struct S {
17956 template <class U> friend void f();
17958 template <class U> friend void f() {}
17960 we don't need to look any further. That's what the check for
17961 DECL_INITIAL is for. */
17962 || (TREE_CODE (decl
) == FUNCTION_DECL
17963 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
17964 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
17966 /* The present template, TD, should not be a definition. If it
17967 were a definition, we should be using it! Note that we
17968 cannot restructure the loop to just keep going until we find
17969 a template with a definition, since that might go too far if
17970 a specialization was declared, but not defined. */
17971 gcc_assert (TREE_CODE (decl
) != VAR_DECL
17972 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
17974 /* Fetch the more general template. */
17975 tmpl
= DECL_TI_TEMPLATE (tmpl
);
17981 /* Returns true if we need to instantiate this template instance even if we
17982 know we aren't going to emit it.. */
17985 always_instantiate_p (tree decl
)
17987 /* We always instantiate inline functions so that we can inline them. An
17988 explicit instantiation declaration prohibits implicit instantiation of
17989 non-inline functions. With high levels of optimization, we would
17990 normally inline non-inline functions -- but we're not allowed to do
17991 that for "extern template" functions. Therefore, we check
17992 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
17993 return ((TREE_CODE (decl
) == FUNCTION_DECL
17994 && (DECL_DECLARED_INLINE_P (decl
)
17995 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
17996 /* And we need to instantiate static data members so that
17997 their initializers are available in integral constant
17999 || (TREE_CODE (decl
) == VAR_DECL
18000 && decl_maybe_constant_var_p (decl
)));
18003 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
18004 instantiate it now, modifying TREE_TYPE (fn). */
18007 maybe_instantiate_noexcept (tree fn
)
18009 tree fntype
, spec
, noex
, clone
;
18011 if (DECL_CLONED_FUNCTION_P (fn
))
18012 fn
= DECL_CLONED_FUNCTION (fn
);
18013 fntype
= TREE_TYPE (fn
);
18014 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
18016 if (!DEFERRED_NOEXCEPT_SPEC_P (spec
))
18019 noex
= TREE_PURPOSE (spec
);
18021 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
18023 if (push_tinst_level (fn
))
18025 push_access_scope (fn
);
18026 input_location
= DECL_SOURCE_LOCATION (fn
);
18027 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
18028 DEFERRED_NOEXCEPT_ARGS (noex
),
18029 tf_warning_or_error
, fn
,
18030 /*function_p=*/false,
18031 /*integral_constant_expression_p=*/true);
18032 pop_access_scope (fn
);
18033 pop_tinst_level ();
18034 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
18035 if (spec
== error_mark_node
)
18036 spec
= noexcept_false_spec
;
18039 spec
= noexcept_false_spec
;
18043 /* This is an implicitly declared function, so NOEX is a list of
18044 other functions to evaluate and merge. */
18046 spec
= noexcept_true_spec
;
18047 for (elt
= noex
; elt
; elt
= OVL_NEXT (elt
))
18049 tree fn
= OVL_CURRENT (elt
);
18051 maybe_instantiate_noexcept (fn
);
18052 subspec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
18053 spec
= merge_exception_specifiers (spec
, subspec
, NULL_TREE
);
18057 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
18059 FOR_EACH_CLONE (clone
, fn
)
18061 if (TREE_TYPE (clone
) == fntype
)
18062 TREE_TYPE (clone
) = TREE_TYPE (fn
);
18064 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
18068 /* Produce the definition of D, a _DECL generated from a template. If
18069 DEFER_OK is nonzero, then we don't have to actually do the
18070 instantiation now; we just have to do it sometime. Normally it is
18071 an error if this is an explicit instantiation but D is undefined.
18072 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
18073 explicitly instantiated class template. */
18076 instantiate_decl (tree d
, int defer_ok
,
18077 bool expl_inst_class_mem_p
)
18079 tree tmpl
= DECL_TI_TEMPLATE (d
);
18086 bool pattern_defined
;
18087 location_t saved_loc
= input_location
;
18092 /* This function should only be used to instantiate templates for
18093 functions and static member variables. */
18094 gcc_assert (TREE_CODE (d
) == FUNCTION_DECL
18095 || TREE_CODE (d
) == VAR_DECL
);
18097 /* Variables are never deferred; if instantiation is required, they
18098 are instantiated right away. That allows for better code in the
18099 case that an expression refers to the value of the variable --
18100 if the variable has a constant value the referring expression can
18101 take advantage of that fact. */
18102 if (TREE_CODE (d
) == VAR_DECL
18103 || DECL_DECLARED_CONSTEXPR_P (d
))
18106 /* Don't instantiate cloned functions. Instead, instantiate the
18107 functions they cloned. */
18108 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
18109 d
= DECL_CLONED_FUNCTION (d
);
18111 if (DECL_TEMPLATE_INSTANTIATED (d
)
18112 || (TREE_CODE (d
) == FUNCTION_DECL
18113 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
18114 || DECL_TEMPLATE_SPECIALIZATION (d
))
18115 /* D has already been instantiated or explicitly specialized, so
18116 there's nothing for us to do here.
18118 It might seem reasonable to check whether or not D is an explicit
18119 instantiation, and, if so, stop here. But when an explicit
18120 instantiation is deferred until the end of the compilation,
18121 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
18122 the instantiation. */
18125 /* Check to see whether we know that this template will be
18126 instantiated in some other file, as with "extern template"
18128 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
18130 /* In general, we do not instantiate such templates. */
18131 if (external_p
&& !always_instantiate_p (d
))
18134 gen_tmpl
= most_general_template (tmpl
);
18135 gen_args
= DECL_TI_ARGS (d
);
18137 if (tmpl
!= gen_tmpl
)
18138 /* We should already have the extra args. */
18139 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
18140 == TMPL_ARGS_DEPTH (gen_args
));
18141 /* And what's in the hash table should match D. */
18142 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
18143 || spec
== NULL_TREE
);
18145 /* This needs to happen before any tsubsting. */
18146 if (! push_tinst_level (d
))
18149 timevar_push (TV_TEMPLATE_INST
);
18151 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
18152 for the instantiation. */
18153 td
= template_for_substitution (d
);
18154 code_pattern
= DECL_TEMPLATE_RESULT (td
);
18156 /* We should never be trying to instantiate a member of a class
18157 template or partial specialization. */
18158 gcc_assert (d
!= code_pattern
);
18160 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
18161 || DECL_TEMPLATE_SPECIALIZATION (td
))
18162 /* In the case of a friend template whose definition is provided
18163 outside the class, we may have too many arguments. Drop the
18164 ones we don't need. The same is true for specializations. */
18165 args
= get_innermost_template_args
18166 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
18170 if (TREE_CODE (d
) == FUNCTION_DECL
)
18171 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
18172 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
));
18174 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
18176 /* We may be in the middle of deferred access check. Disable it now. */
18177 push_deferring_access_checks (dk_no_deferred
);
18179 /* Unless an explicit instantiation directive has already determined
18180 the linkage of D, remember that a definition is available for
18182 if (pattern_defined
18183 && !DECL_INTERFACE_KNOWN (d
)
18184 && !DECL_NOT_REALLY_EXTERN (d
))
18185 mark_definable (d
);
18187 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
18188 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
18189 input_location
= DECL_SOURCE_LOCATION (d
);
18191 /* If D is a member of an explicitly instantiated class template,
18192 and no definition is available, treat it like an implicit
18194 if (!pattern_defined
&& expl_inst_class_mem_p
18195 && DECL_EXPLICIT_INSTANTIATION (d
))
18197 /* Leave linkage flags alone on instantiations with anonymous
18199 if (TREE_PUBLIC (d
))
18201 DECL_NOT_REALLY_EXTERN (d
) = 0;
18202 DECL_INTERFACE_KNOWN (d
) = 0;
18204 SET_DECL_IMPLICIT_INSTANTIATION (d
);
18207 if (TREE_CODE (d
) == FUNCTION_DECL
)
18208 maybe_instantiate_noexcept (d
);
18210 /* Recheck the substitutions to obtain any warning messages
18211 about ignoring cv qualifiers. Don't do this for artificial decls,
18212 as it breaks the context-sensitive substitution for lambda op(). */
18213 if (!defer_ok
&& !DECL_ARTIFICIAL (d
))
18215 tree gen
= DECL_TEMPLATE_RESULT (gen_tmpl
);
18216 tree type
= TREE_TYPE (gen
);
18218 /* Make sure that we can see identifiers, and compute access
18219 correctly. D is already the target FUNCTION_DECL with the
18221 push_access_scope (d
);
18223 if (TREE_CODE (gen
) == FUNCTION_DECL
)
18225 tsubst (DECL_ARGUMENTS (gen
), gen_args
, tf_warning_or_error
, d
);
18226 tsubst_exception_specification (type
, gen_args
, tf_warning_or_error
,
18227 d
, /*defer_ok*/true);
18228 /* Don't simply tsubst the function type, as that will give
18229 duplicate warnings about poor parameter qualifications.
18230 The function arguments are the same as the decl_arguments
18231 without the top level cv qualifiers. */
18232 type
= TREE_TYPE (type
);
18234 tsubst (type
, gen_args
, tf_warning_or_error
, d
);
18236 pop_access_scope (d
);
18239 /* Defer all other templates, unless we have been explicitly
18240 forbidden from doing so. */
18241 if (/* If there is no definition, we cannot instantiate the
18244 /* If it's OK to postpone instantiation, do so. */
18246 /* If this is a static data member that will be defined
18247 elsewhere, we don't want to instantiate the entire data
18248 member, but we do want to instantiate the initializer so that
18249 we can substitute that elsewhere. */
18250 || (external_p
&& TREE_CODE (d
) == VAR_DECL
))
18252 /* The definition of the static data member is now required so
18253 we must substitute the initializer. */
18254 if (TREE_CODE (d
) == VAR_DECL
18255 && !DECL_INITIAL (d
)
18256 && DECL_INITIAL (code_pattern
))
18260 bool const_init
= false;
18262 ns
= decl_namespace_context (d
);
18263 push_nested_namespace (ns
);
18264 push_nested_class (DECL_CONTEXT (d
));
18265 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
18267 tf_warning_or_error
, NULL_TREE
,
18268 /*integral_constant_expression_p=*/false);
18269 /* Make sure the initializer is still constant, in case of
18270 circular dependency (template/instantiate6.C). */
18272 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
18273 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
18274 /*asmspec_tree=*/NULL_TREE
,
18275 LOOKUP_ONLYCONVERTING
);
18276 pop_nested_class ();
18277 pop_nested_namespace (ns
);
18280 /* We restore the source position here because it's used by
18281 add_pending_template. */
18282 input_location
= saved_loc
;
18284 if (at_eof
&& !pattern_defined
18285 && DECL_EXPLICIT_INSTANTIATION (d
)
18286 && DECL_NOT_REALLY_EXTERN (d
))
18289 The definition of a non-exported function template, a
18290 non-exported member function template, or a non-exported
18291 member function or static data member of a class template
18292 shall be present in every translation unit in which it is
18293 explicitly instantiated. */
18294 permerror (input_location
, "explicit instantiation of %qD "
18295 "but no definition available", d
);
18297 /* If we're in unevaluated context, we just wanted to get the
18298 constant value; this isn't an odr use, so don't queue
18299 a full instantiation. */
18300 if (cp_unevaluated_operand
!= 0)
18302 /* ??? Historically, we have instantiated inline functions, even
18303 when marked as "extern template". */
18304 if (!(external_p
&& TREE_CODE (d
) == VAR_DECL
))
18305 add_pending_template (d
);
18308 /* Tell the repository that D is available in this translation unit
18309 -- and see if it is supposed to be instantiated here. */
18310 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
18312 /* In a PCH file, despite the fact that the repository hasn't
18313 requested instantiation in the PCH it is still possible that
18314 an instantiation will be required in a file that includes the
18317 add_pending_template (d
);
18318 /* Instantiate inline functions so that the inliner can do its
18319 job, even though we'll not be emitting a copy of this
18321 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
18325 fn_context
= decl_function_context (d
);
18326 nested
= (current_function_decl
!= NULL_TREE
);
18328 push_to_top_level ();
18330 push_function_context ();
18332 /* Mark D as instantiated so that recursive calls to
18333 instantiate_decl do not try to instantiate it again. */
18334 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
18336 /* Regenerate the declaration in case the template has been modified
18337 by a subsequent redeclaration. */
18338 regenerate_decl_from_template (d
, td
);
18340 /* We already set the file and line above. Reset them now in case
18341 they changed as a result of calling regenerate_decl_from_template. */
18342 input_location
= DECL_SOURCE_LOCATION (d
);
18344 if (TREE_CODE (d
) == VAR_DECL
)
18347 bool const_init
= false;
18349 /* Clear out DECL_RTL; whatever was there before may not be right
18350 since we've reset the type of the declaration. */
18351 SET_DECL_RTL (d
, NULL
);
18352 DECL_IN_AGGR_P (d
) = 0;
18354 /* The initializer is placed in DECL_INITIAL by
18355 regenerate_decl_from_template so we don't need to
18356 push/pop_access_scope again here. Pull it out so that
18357 cp_finish_decl can process it. */
18358 init
= DECL_INITIAL (d
);
18359 DECL_INITIAL (d
) = NULL_TREE
;
18360 DECL_INITIALIZED_P (d
) = 0;
18362 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
18363 initializer. That function will defer actual emission until
18364 we have a chance to determine linkage. */
18365 DECL_EXTERNAL (d
) = 0;
18367 /* Enter the scope of D so that access-checking works correctly. */
18368 push_nested_class (DECL_CONTEXT (d
));
18369 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
18370 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
18371 pop_nested_class ();
18373 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
18374 synthesize_method (d
);
18375 else if (TREE_CODE (d
) == FUNCTION_DECL
)
18377 struct pointer_map_t
*saved_local_specializations
;
18382 /* Save away the current list, in case we are instantiating one
18383 template from within the body of another. */
18384 saved_local_specializations
= local_specializations
;
18386 /* Set up the list of local specializations. */
18387 local_specializations
= pointer_map_create ();
18389 /* Set up context. */
18390 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
18392 /* Some typedefs referenced from within the template code need to be
18393 access checked at template instantiation time, i.e now. These
18394 types were added to the template at parsing time. Let's get those
18395 and perform the access checks then. */
18396 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl
),
18399 /* Create substitution entries for the parameters. */
18400 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
18401 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
18402 spec_parm
= DECL_ARGUMENTS (d
);
18403 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
18405 register_local_specialization (spec_parm
, tmpl_parm
);
18406 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
18407 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
18409 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
18411 if (!FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
18413 register_local_specialization (spec_parm
, tmpl_parm
);
18414 spec_parm
= DECL_CHAIN (spec_parm
);
18418 /* Register the (value) argument pack as a specialization of
18419 TMPL_PARM, then move on. */
18420 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
18421 register_local_specialization (argpack
, tmpl_parm
);
18424 gcc_assert (!spec_parm
);
18426 /* Substitute into the body of the function. */
18427 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
18428 tf_warning_or_error
, tmpl
,
18429 /*integral_constant_expression_p=*/false);
18431 /* Set the current input_location to the end of the function
18432 so that finish_function knows where we are. */
18433 input_location
= DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
18435 /* We don't need the local specializations any more. */
18436 pointer_map_destroy (local_specializations
);
18437 local_specializations
= saved_local_specializations
;
18439 /* Finish the function. */
18440 d
= finish_function (0);
18441 expand_or_defer_fn (d
);
18444 /* We're not deferring instantiation any more. */
18445 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
18448 pop_from_top_level ();
18450 pop_function_context ();
18453 input_location
= saved_loc
;
18454 pop_deferring_access_checks ();
18455 pop_tinst_level ();
18457 timevar_pop (TV_TEMPLATE_INST
);
18462 /* Run through the list of templates that we wish we could
18463 instantiate, and instantiate any we can. RETRIES is the
18464 number of times we retry pending template instantiation. */
18467 instantiate_pending_templates (int retries
)
18470 location_t saved_loc
= input_location
;
18472 /* Instantiating templates may trigger vtable generation. This in turn
18473 may require further template instantiations. We place a limit here
18474 to avoid infinite loop. */
18475 if (pending_templates
&& retries
>= max_tinst_depth
)
18477 tree decl
= pending_templates
->tinst
->decl
;
18479 error ("template instantiation depth exceeds maximum of %d"
18480 " instantiating %q+D, possibly from virtual table generation"
18481 " (use -ftemplate-depth= to increase the maximum)",
18482 max_tinst_depth
, decl
);
18483 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18484 /* Pretend that we defined it. */
18485 DECL_INITIAL (decl
) = error_mark_node
;
18491 struct pending_template
**t
= &pending_templates
;
18492 struct pending_template
*last
= NULL
;
18496 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
18497 bool complete
= false;
18499 if (TYPE_P (instantiation
))
18503 if (!COMPLETE_TYPE_P (instantiation
))
18505 instantiate_class_template (instantiation
);
18506 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
18507 for (fn
= TYPE_METHODS (instantiation
);
18509 fn
= TREE_CHAIN (fn
))
18510 if (! DECL_ARTIFICIAL (fn
))
18511 instantiate_decl (fn
,
18513 /*expl_inst_class_mem_p=*/false);
18514 if (COMPLETE_TYPE_P (instantiation
))
18518 complete
= COMPLETE_TYPE_P (instantiation
);
18522 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
18523 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
18526 = instantiate_decl (instantiation
,
18528 /*expl_inst_class_mem_p=*/false);
18529 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
18533 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
18534 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
18538 /* If INSTANTIATION has been instantiated, then we don't
18539 need to consider it again in the future. */
18547 current_tinst_level
= NULL
;
18549 last_pending_template
= last
;
18551 while (reconsider
);
18553 input_location
= saved_loc
;
18556 /* Substitute ARGVEC into T, which is a list of initializers for
18557 either base class or a non-static data member. The TREE_PURPOSEs
18558 are DECLs, and the TREE_VALUEs are the initializer values. Used by
18559 instantiate_decl. */
18562 tsubst_initializer_list (tree t
, tree argvec
)
18564 tree inits
= NULL_TREE
;
18566 for (; t
; t
= TREE_CHAIN (t
))
18570 tree expanded_bases
= NULL_TREE
;
18571 tree expanded_arguments
= NULL_TREE
;
18574 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
18579 /* Expand the base class expansion type into separate base
18581 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
18582 tf_warning_or_error
,
18584 if (expanded_bases
== error_mark_node
)
18587 /* We'll be building separate TREE_LISTs of arguments for
18589 len
= TREE_VEC_LENGTH (expanded_bases
);
18590 expanded_arguments
= make_tree_vec (len
);
18591 for (i
= 0; i
< len
; i
++)
18592 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
18594 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
18595 expand each argument in the TREE_VALUE of t. */
18596 expr
= make_node (EXPR_PACK_EXPANSION
);
18597 PACK_EXPANSION_LOCAL_P (expr
) = true;
18598 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
18599 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
18601 if (TREE_VALUE (t
) == void_type_node
)
18602 /* VOID_TYPE_NODE is used to indicate
18603 value-initialization. */
18605 for (i
= 0; i
< len
; i
++)
18606 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
18610 /* Substitute parameter packs into each argument in the
18612 in_base_initializer
= 1;
18613 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
18615 tree expanded_exprs
;
18617 /* Expand the argument. */
18618 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
18620 = tsubst_pack_expansion (expr
, argvec
,
18621 tf_warning_or_error
,
18623 if (expanded_exprs
== error_mark_node
)
18626 /* Prepend each of the expanded expressions to the
18627 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
18628 for (i
= 0; i
< len
; i
++)
18630 TREE_VEC_ELT (expanded_arguments
, i
) =
18631 tree_cons (NULL_TREE
,
18632 TREE_VEC_ELT (expanded_exprs
, i
),
18633 TREE_VEC_ELT (expanded_arguments
, i
));
18636 in_base_initializer
= 0;
18638 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
18639 since we built them backwards. */
18640 for (i
= 0; i
< len
; i
++)
18642 TREE_VEC_ELT (expanded_arguments
, i
) =
18643 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
18648 for (i
= 0; i
< len
; ++i
)
18650 if (expanded_bases
)
18652 decl
= TREE_VEC_ELT (expanded_bases
, i
);
18653 decl
= expand_member_init (decl
);
18654 init
= TREE_VEC_ELT (expanded_arguments
, i
);
18659 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
18660 tf_warning_or_error
, NULL_TREE
);
18662 decl
= expand_member_init (decl
);
18663 if (decl
&& !DECL_P (decl
))
18664 in_base_initializer
= 1;
18666 init
= TREE_VALUE (t
);
18668 if (init
!= void_type_node
)
18669 init
= tsubst_expr (init
, argvec
,
18670 tf_warning_or_error
, NULL_TREE
,
18671 /*integral_constant_expression_p=*/false);
18672 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
18673 /* If we had an initializer but it instantiated to nothing,
18674 value-initialize the object. This will only occur when
18675 the initializer was a pack expansion where the parameter
18676 packs used in that expansion were of length zero. */
18677 init
= void_type_node
;
18678 in_base_initializer
= 0;
18683 init
= build_tree_list (decl
, init
);
18684 TREE_CHAIN (init
) = inits
;
18692 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
18695 set_current_access_from_decl (tree decl
)
18697 if (TREE_PRIVATE (decl
))
18698 current_access_specifier
= access_private_node
;
18699 else if (TREE_PROTECTED (decl
))
18700 current_access_specifier
= access_protected_node
;
18702 current_access_specifier
= access_public_node
;
18705 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
18706 is the instantiation (which should have been created with
18707 start_enum) and ARGS are the template arguments to use. */
18710 tsubst_enum (tree tag
, tree newtag
, tree args
)
18714 if (SCOPED_ENUM_P (newtag
))
18715 begin_scope (sk_scoped_enum
, newtag
);
18717 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
18722 decl
= TREE_VALUE (e
);
18723 /* Note that in a template enum, the TREE_VALUE is the
18724 CONST_DECL, not the corresponding INTEGER_CST. */
18725 value
= tsubst_expr (DECL_INITIAL (decl
),
18726 args
, tf_warning_or_error
, NULL_TREE
,
18727 /*integral_constant_expression_p=*/true);
18729 /* Give this enumeration constant the correct access. */
18730 set_current_access_from_decl (decl
);
18732 /* Actually build the enumerator itself. */
18734 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
18737 if (SCOPED_ENUM_P (newtag
))
18740 finish_enum_value_list (newtag
);
18741 finish_enum (newtag
);
18743 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
18744 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
18747 /* DECL is a FUNCTION_DECL that is a template specialization. Return
18748 its type -- but without substituting the innermost set of template
18749 arguments. So, innermost set of template parameters will appear in
18753 get_mostly_instantiated_function_type (tree decl
)
18761 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
18762 targs
= DECL_TI_ARGS (decl
);
18763 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
18764 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
18766 /* There should be as many levels of arguments as there are levels
18768 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
18770 fn_type
= TREE_TYPE (tmpl
);
18772 if (parm_depth
== 1)
18773 /* No substitution is necessary. */
18780 /* Replace the innermost level of the TARGS with NULL_TREEs to
18781 let tsubst know not to substitute for those parameters. */
18782 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
18783 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
18784 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
18785 TMPL_ARGS_LEVEL (targs
, i
));
18786 SET_TMPL_ARGS_LEVEL (partial_args
,
18787 TMPL_ARGS_DEPTH (targs
),
18788 make_tree_vec (DECL_NTPARMS (tmpl
)));
18790 /* Make sure that we can see identifiers, and compute access
18792 push_access_scope (decl
);
18794 ++processing_template_decl
;
18795 /* Now, do the (partial) substitution to figure out the
18796 appropriate function type. */
18797 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
18798 --processing_template_decl
;
18800 /* Substitute into the template parameters to obtain the real
18801 innermost set of parameters. This step is important if the
18802 innermost set of template parameters contains value
18803 parameters whose types depend on outer template parameters. */
18804 TREE_VEC_LENGTH (partial_args
)--;
18805 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
18807 pop_access_scope (decl
);
18813 /* Return truthvalue if we're processing a template different from
18814 the last one involved in diagnostics. */
18816 problematic_instantiation_changed (void)
18818 return current_tinst_level
!= last_error_tinst_level
;
18821 /* Remember current template involved in diagnostics. */
18823 record_last_problematic_instantiation (void)
18825 last_error_tinst_level
= current_tinst_level
;
18828 struct tinst_level
*
18829 current_instantiation (void)
18831 return current_tinst_level
;
18834 /* [temp.param] Check that template non-type parm TYPE is of an allowable
18835 type. Return zero for ok, nonzero for disallowed. Issue error and
18836 warning messages under control of COMPLAIN. */
18839 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
18841 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
18843 else if (POINTER_TYPE_P (type
))
18845 else if (TYPE_PTRMEM_P (type
))
18847 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
18849 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
18851 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
18853 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
18856 if (complain
& tf_error
)
18858 if (type
== error_mark_node
)
18859 inform (input_location
, "invalid template non-type parameter");
18861 error ("%q#T is not a valid type for a template non-type parameter",
18867 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
18868 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
18871 dependent_type_p_r (tree type
)
18877 A type is dependent if it is:
18879 -- a template parameter. Template template parameters are types
18880 for us (since TYPE_P holds true for them) so we handle
18882 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
18883 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
18885 /* -- a qualified-id with a nested-name-specifier which contains a
18886 class-name that names a dependent type or whose unqualified-id
18887 names a dependent type. */
18888 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18890 /* -- a cv-qualified type where the cv-unqualified type is
18892 type
= TYPE_MAIN_VARIANT (type
);
18893 /* -- a compound type constructed from any dependent type. */
18894 if (TYPE_PTRMEM_P (type
))
18895 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
18896 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
18898 else if (TREE_CODE (type
) == POINTER_TYPE
18899 || TREE_CODE (type
) == REFERENCE_TYPE
)
18900 return dependent_type_p (TREE_TYPE (type
));
18901 else if (TREE_CODE (type
) == FUNCTION_TYPE
18902 || TREE_CODE (type
) == METHOD_TYPE
)
18906 if (dependent_type_p (TREE_TYPE (type
)))
18908 for (arg_type
= TYPE_ARG_TYPES (type
);
18910 arg_type
= TREE_CHAIN (arg_type
))
18911 if (dependent_type_p (TREE_VALUE (arg_type
)))
18915 /* -- an array type constructed from any dependent type or whose
18916 size is specified by a constant expression that is
18919 We checked for type- and value-dependence of the bounds in
18920 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
18921 if (TREE_CODE (type
) == ARRAY_TYPE
)
18923 if (TYPE_DOMAIN (type
)
18924 && dependent_type_p (TYPE_DOMAIN (type
)))
18926 return dependent_type_p (TREE_TYPE (type
));
18929 /* -- a template-id in which either the template name is a template
18931 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
18933 /* ... or any of the template arguments is a dependent type or
18934 an expression that is type-dependent or value-dependent. */
18935 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
18936 && (any_dependent_template_arguments_p
18937 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
18940 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
18941 dependent; if the argument of the `typeof' expression is not
18942 type-dependent, then it should already been have resolved. */
18943 if (TREE_CODE (type
) == TYPEOF_TYPE
18944 || TREE_CODE (type
) == DECLTYPE_TYPE
18945 || TREE_CODE (type
) == UNDERLYING_TYPE
)
18948 /* A template argument pack is dependent if any of its packed
18950 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
18952 tree args
= ARGUMENT_PACK_ARGS (type
);
18953 int i
, len
= TREE_VEC_LENGTH (args
);
18954 for (i
= 0; i
< len
; ++i
)
18955 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
18959 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
18960 be template parameters. */
18961 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
18964 /* The standard does not specifically mention types that are local
18965 to template functions or local classes, but they should be
18966 considered dependent too. For example:
18968 template <int I> void f() {
18973 The size of `E' cannot be known until the value of `I' has been
18974 determined. Therefore, `E' must be considered dependent. */
18975 scope
= TYPE_CONTEXT (type
);
18976 if (scope
&& TYPE_P (scope
))
18977 return dependent_type_p (scope
);
18978 /* Don't use type_dependent_expression_p here, as it can lead
18979 to infinite recursion trying to determine whether a lambda
18980 nested in a lambda is dependent (c++/47687). */
18981 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
18982 && DECL_LANG_SPECIFIC (scope
)
18983 && DECL_TEMPLATE_INFO (scope
)
18984 && (any_dependent_template_arguments_p
18985 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
18988 /* Other types are non-dependent. */
18992 /* Returns TRUE if TYPE is dependent, in the sense of
18993 [temp.dep.type]. Note that a NULL type is considered dependent. */
18996 dependent_type_p (tree type
)
18998 /* If there are no template parameters in scope, then there can't be
18999 any dependent types. */
19000 if (!processing_template_decl
)
19002 /* If we are not processing a template, then nobody should be
19003 providing us with a dependent type. */
19005 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
19009 /* If the type is NULL, we have not computed a type for the entity
19010 in question; in that case, the type is dependent. */
19014 /* Erroneous types can be considered non-dependent. */
19015 if (type
== error_mark_node
)
19018 /* If we have not already computed the appropriate value for TYPE,
19020 if (!TYPE_DEPENDENT_P_VALID (type
))
19022 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
19023 TYPE_DEPENDENT_P_VALID (type
) = 1;
19026 return TYPE_DEPENDENT_P (type
);
19029 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
19030 lookup. In other words, a dependent type that is not the current
19034 dependent_scope_p (tree scope
)
19036 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
19037 && !currently_open_class (scope
));
19040 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
19041 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
19044 /* Note that this predicate is not appropriate for general expressions;
19045 only constant expressions (that satisfy potential_constant_expression)
19046 can be tested for value dependence.
19048 We should really also have a predicate for "instantiation-dependent".
19050 fold_non_dependent_expr: fold if constant and not type-dependent and not value-dependent.
19051 (what about instantiation-dependent constant-expressions?)
19052 is_late_template_attribute: defer if instantiation-dependent.
19053 compute_array_index_type: proceed if constant and not t- or v-dependent
19054 if instantiation-dependent, need to remember full expression
19055 uses_template_parms: FIXME - need to audit callers
19056 tsubst_decl [function_decl]: Why is this using value_dependent_expression_p?
19057 dependent_type_p [array_type]: dependent if index type is dependent
19059 static_assert - instantiation-dependent */
19062 value_dependent_expression_p (tree expression
)
19064 if (!processing_template_decl
)
19067 /* A name declared with a dependent type. */
19068 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
19071 switch (TREE_CODE (expression
))
19073 case IDENTIFIER_NODE
:
19074 /* A name that has not been looked up -- must be dependent. */
19077 case TEMPLATE_PARM_INDEX
:
19078 /* A non-type template parm. */
19082 /* A non-type template parm. */
19083 if (DECL_TEMPLATE_PARM_P (expression
))
19085 return value_dependent_expression_p (DECL_INITIAL (expression
));
19088 /* A constant with literal type and is initialized
19089 with an expression that is value-dependent. */
19090 if (DECL_INITIAL (expression
)
19091 && decl_constant_var_p (expression
)
19092 && value_dependent_expression_p (DECL_INITIAL (expression
)))
19096 case DYNAMIC_CAST_EXPR
:
19097 case STATIC_CAST_EXPR
:
19098 case CONST_CAST_EXPR
:
19099 case REINTERPRET_CAST_EXPR
:
19101 /* These expressions are value-dependent if the type to which
19102 the cast occurs is dependent or the expression being casted
19103 is value-dependent. */
19105 tree type
= TREE_TYPE (expression
);
19107 if (dependent_type_p (type
))
19110 /* A functional cast has a list of operands. */
19111 expression
= TREE_OPERAND (expression
, 0);
19114 /* If there are no operands, it must be an expression such
19115 as "int()". This should not happen for aggregate types
19116 because it would form non-constant expressions. */
19117 gcc_assert (cxx_dialect
>= cxx0x
19118 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
19123 if (TREE_CODE (expression
) == TREE_LIST
)
19124 return any_value_dependent_elements_p (expression
);
19126 return value_dependent_expression_p (expression
);
19132 /* A `sizeof' expression is value-dependent if the operand is
19133 type-dependent or is a pack expansion. */
19134 expression
= TREE_OPERAND (expression
, 0);
19135 if (PACK_EXPANSION_P (expression
))
19137 else if (TYPE_P (expression
))
19138 return dependent_type_p (expression
);
19139 return type_dependent_expression_p (expression
);
19141 case AT_ENCODE_EXPR
:
19142 /* An 'encode' expression is value-dependent if the operand is
19144 expression
= TREE_OPERAND (expression
, 0);
19145 return dependent_type_p (expression
);
19147 case NOEXCEPT_EXPR
:
19148 expression
= TREE_OPERAND (expression
, 0);
19149 return type_dependent_expression_p (expression
);
19153 tree name
= TREE_OPERAND (expression
, 1);
19154 return value_dependent_expression_p (name
);
19157 case COMPONENT_REF
:
19158 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
19159 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
19161 case NONTYPE_ARGUMENT_PACK
:
19162 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
19163 is value-dependent. */
19165 tree values
= ARGUMENT_PACK_ARGS (expression
);
19166 int i
, len
= TREE_VEC_LENGTH (values
);
19168 for (i
= 0; i
< len
; ++i
)
19169 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
19177 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
19178 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
19179 || (type2
? dependent_type_p (type2
) : false));
19183 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19184 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
19187 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19188 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
19192 tree op
= TREE_OPERAND (expression
, 0);
19193 return (value_dependent_expression_p (op
)
19194 || has_value_dependent_address (op
));
19199 tree fn
= get_callee_fndecl (expression
);
19201 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
19203 nargs
= call_expr_nargs (expression
);
19204 for (i
= 0; i
< nargs
; ++i
)
19206 tree op
= CALL_EXPR_ARG (expression
, i
);
19207 /* In a call to a constexpr member function, look through the
19208 implicit ADDR_EXPR on the object argument so that it doesn't
19209 cause the call to be considered value-dependent. We also
19210 look through it in potential_constant_expression. */
19211 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
19212 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
19213 && TREE_CODE (op
) == ADDR_EXPR
)
19214 op
= TREE_OPERAND (op
, 0);
19215 if (value_dependent_expression_p (op
))
19221 case TEMPLATE_ID_EXPR
:
19222 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
19224 return type_dependent_expression_p (expression
);
19230 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
19231 if (value_dependent_expression_p (val
))
19237 /* Treat a GNU statement expression as dependent to avoid crashing
19238 under fold_non_dependent_expr; it can't be constant. */
19242 /* A constant expression is value-dependent if any subexpression is
19243 value-dependent. */
19244 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
19246 case tcc_reference
:
19248 case tcc_comparison
:
19250 case tcc_expression
:
19253 int i
, len
= cp_tree_operand_length (expression
);
19255 for (i
= 0; i
< len
; i
++)
19257 tree t
= TREE_OPERAND (expression
, i
);
19259 /* In some cases, some of the operands may be missing.l
19260 (For example, in the case of PREDECREMENT_EXPR, the
19261 amount to increment by may be missing.) That doesn't
19262 make the expression dependent. */
19263 if (t
&& value_dependent_expression_p (t
))
19274 /* The expression is not value-dependent. */
19278 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
19279 [temp.dep.expr]. Note that an expression with no type is
19280 considered dependent. Other parts of the compiler arrange for an
19281 expression with type-dependent subexpressions to have no type, so
19282 this function doesn't have to be fully recursive. */
19285 type_dependent_expression_p (tree expression
)
19287 if (!processing_template_decl
)
19290 if (expression
== error_mark_node
)
19293 /* An unresolved name is always dependent. */
19294 if (TREE_CODE (expression
) == IDENTIFIER_NODE
19295 || TREE_CODE (expression
) == USING_DECL
)
19298 /* Some expression forms are never type-dependent. */
19299 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
19300 || TREE_CODE (expression
) == SIZEOF_EXPR
19301 || TREE_CODE (expression
) == ALIGNOF_EXPR
19302 || TREE_CODE (expression
) == AT_ENCODE_EXPR
19303 || TREE_CODE (expression
) == NOEXCEPT_EXPR
19304 || TREE_CODE (expression
) == TRAIT_EXPR
19305 || TREE_CODE (expression
) == TYPEID_EXPR
19306 || TREE_CODE (expression
) == DELETE_EXPR
19307 || TREE_CODE (expression
) == VEC_DELETE_EXPR
19308 || TREE_CODE (expression
) == THROW_EXPR
)
19311 /* The types of these expressions depends only on the type to which
19312 the cast occurs. */
19313 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
19314 || TREE_CODE (expression
) == STATIC_CAST_EXPR
19315 || TREE_CODE (expression
) == CONST_CAST_EXPR
19316 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
19317 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
19318 || TREE_CODE (expression
) == CAST_EXPR
)
19319 return dependent_type_p (TREE_TYPE (expression
));
19321 /* The types of these expressions depends only on the type created
19322 by the expression. */
19323 if (TREE_CODE (expression
) == NEW_EXPR
19324 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
19326 /* For NEW_EXPR tree nodes created inside a template, either
19327 the object type itself or a TREE_LIST may appear as the
19329 tree type
= TREE_OPERAND (expression
, 1);
19330 if (TREE_CODE (type
) == TREE_LIST
)
19331 /* This is an array type. We need to check array dimensions
19333 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
19334 || value_dependent_expression_p
19335 (TREE_OPERAND (TREE_VALUE (type
), 1));
19337 return dependent_type_p (type
);
19340 if (TREE_CODE (expression
) == SCOPE_REF
)
19342 tree scope
= TREE_OPERAND (expression
, 0);
19343 tree name
= TREE_OPERAND (expression
, 1);
19345 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
19346 contains an identifier associated by name lookup with one or more
19347 declarations declared with a dependent type, or...a
19348 nested-name-specifier or qualified-id that names a member of an
19349 unknown specialization. */
19350 return (type_dependent_expression_p (name
)
19351 || dependent_scope_p (scope
));
19354 if (TREE_CODE (expression
) == FUNCTION_DECL
19355 && DECL_LANG_SPECIFIC (expression
)
19356 && DECL_TEMPLATE_INFO (expression
)
19357 && (any_dependent_template_arguments_p
19358 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
19361 if (TREE_CODE (expression
) == TEMPLATE_DECL
19362 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
19365 if (TREE_CODE (expression
) == STMT_EXPR
)
19366 expression
= stmt_expr_value_expr (expression
);
19368 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
19373 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
19375 if (type_dependent_expression_p (elt
))
19381 /* A static data member of the current instantiation with incomplete
19382 array type is type-dependent, as the definition and specializations
19383 can have different bounds. */
19384 if (TREE_CODE (expression
) == VAR_DECL
19385 && DECL_CLASS_SCOPE_P (expression
)
19386 && dependent_type_p (DECL_CONTEXT (expression
))
19387 && VAR_HAD_UNKNOWN_BOUND (expression
))
19390 if (TREE_TYPE (expression
) == unknown_type_node
)
19392 if (TREE_CODE (expression
) == ADDR_EXPR
)
19393 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
19394 if (TREE_CODE (expression
) == COMPONENT_REF
19395 || TREE_CODE (expression
) == OFFSET_REF
)
19397 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19399 expression
= TREE_OPERAND (expression
, 1);
19400 if (TREE_CODE (expression
) == IDENTIFIER_NODE
)
19403 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
19404 if (TREE_CODE (expression
) == SCOPE_REF
)
19407 if (BASELINK_P (expression
))
19408 expression
= BASELINK_FUNCTIONS (expression
);
19410 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
19412 if (any_dependent_template_arguments_p
19413 (TREE_OPERAND (expression
, 1)))
19415 expression
= TREE_OPERAND (expression
, 0);
19417 gcc_assert (TREE_CODE (expression
) == OVERLOAD
19418 || TREE_CODE (expression
) == FUNCTION_DECL
);
19422 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
19424 expression
= OVL_NEXT (expression
);
19429 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
19431 return (dependent_type_p (TREE_TYPE (expression
)));
19434 /* Like type_dependent_expression_p, but it also works while not processing
19435 a template definition, i.e. during substitution or mangling. */
19438 type_dependent_expression_p_push (tree expr
)
19441 ++processing_template_decl
;
19442 b
= type_dependent_expression_p (expr
);
19443 --processing_template_decl
;
19447 /* Returns TRUE if ARGS contains a type-dependent expression. */
19450 any_type_dependent_arguments_p (const VEC(tree
,gc
) *args
)
19455 FOR_EACH_VEC_ELT (tree
, args
, i
, arg
)
19457 if (type_dependent_expression_p (arg
))
19463 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
19464 expressions) contains any type-dependent expressions. */
19467 any_type_dependent_elements_p (const_tree list
)
19469 for (; list
; list
= TREE_CHAIN (list
))
19470 if (value_dependent_expression_p (TREE_VALUE (list
)))
19476 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
19477 expressions) contains any value-dependent expressions. */
19480 any_value_dependent_elements_p (const_tree list
)
19482 for (; list
; list
= TREE_CHAIN (list
))
19483 if (value_dependent_expression_p (TREE_VALUE (list
)))
19489 /* Returns TRUE if the ARG (a template argument) is dependent. */
19492 dependent_template_arg_p (tree arg
)
19494 if (!processing_template_decl
)
19497 /* Assume a template argument that was wrongly written by the user
19498 is dependent. This is consistent with what
19499 any_dependent_template_arguments_p [that calls this function]
19501 if (!arg
|| arg
== error_mark_node
)
19504 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
19505 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
19507 if (TREE_CODE (arg
) == TEMPLATE_DECL
19508 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
19509 return dependent_template_p (arg
);
19510 else if (ARGUMENT_PACK_P (arg
))
19512 tree args
= ARGUMENT_PACK_ARGS (arg
);
19513 int i
, len
= TREE_VEC_LENGTH (args
);
19514 for (i
= 0; i
< len
; ++i
)
19516 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
19522 else if (TYPE_P (arg
))
19523 return dependent_type_p (arg
);
19525 return (type_dependent_expression_p (arg
)
19526 || value_dependent_expression_p (arg
));
19529 /* Returns true if ARGS (a collection of template arguments) contains
19530 any types that require structural equality testing. */
19533 any_template_arguments_need_structural_equality_p (tree args
)
19540 if (args
== error_mark_node
)
19543 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
19545 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
19546 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
19548 tree arg
= TREE_VEC_ELT (level
, j
);
19549 tree packed_args
= NULL_TREE
;
19552 if (ARGUMENT_PACK_P (arg
))
19554 /* Look inside the argument pack. */
19555 packed_args
= ARGUMENT_PACK_ARGS (arg
);
19556 len
= TREE_VEC_LENGTH (packed_args
);
19559 for (k
= 0; k
< len
; ++k
)
19562 arg
= TREE_VEC_ELT (packed_args
, k
);
19564 if (error_operand_p (arg
))
19566 else if (TREE_CODE (arg
) == TEMPLATE_DECL
19567 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
19569 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
19571 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
19572 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
19581 /* Returns true if ARGS (a collection of template arguments) contains
19582 any dependent arguments. */
19585 any_dependent_template_arguments_p (const_tree args
)
19592 if (args
== error_mark_node
)
19595 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
19597 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
19598 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
19599 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
19606 /* Returns TRUE if the template TMPL is dependent. */
19609 dependent_template_p (tree tmpl
)
19611 if (TREE_CODE (tmpl
) == OVERLOAD
)
19615 if (dependent_template_p (OVL_CURRENT (tmpl
)))
19617 tmpl
= OVL_NEXT (tmpl
);
19622 /* Template template parameters are dependent. */
19623 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
19624 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
19626 /* So are names that have not been looked up. */
19627 if (TREE_CODE (tmpl
) == SCOPE_REF
19628 || TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
19630 /* So are member templates of dependent classes. */
19631 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
19632 return dependent_type_p (DECL_CONTEXT (tmpl
));
19636 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
19639 dependent_template_id_p (tree tmpl
, tree args
)
19641 return (dependent_template_p (tmpl
)
19642 || any_dependent_template_arguments_p (args
));
19645 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
19649 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
19653 if (!processing_template_decl
)
19656 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
19658 tree decl
= TREE_VEC_ELT (declv
, i
);
19659 tree init
= TREE_VEC_ELT (initv
, i
);
19660 tree cond
= TREE_VEC_ELT (condv
, i
);
19661 tree incr
= TREE_VEC_ELT (incrv
, i
);
19663 if (type_dependent_expression_p (decl
))
19666 if (init
&& type_dependent_expression_p (init
))
19669 if (type_dependent_expression_p (cond
))
19672 if (COMPARISON_CLASS_P (cond
)
19673 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
19674 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
19677 if (TREE_CODE (incr
) == MODOP_EXPR
)
19679 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
19680 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
19683 else if (type_dependent_expression_p (incr
))
19685 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
19687 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
19689 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
19691 tree t
= TREE_OPERAND (incr
, 1);
19692 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
19693 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
19702 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
19703 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
19704 no such TYPE can be found. Note that this function peers inside
19705 uninstantiated templates and therefore should be used only in
19706 extremely limited situations. ONLY_CURRENT_P restricts this
19707 peering to the currently open classes hierarchy (which is required
19708 when comparing types). */
19711 resolve_typename_type (tree type
, bool only_current_p
)
19720 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
19722 scope
= TYPE_CONTEXT (type
);
19723 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
19724 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
19725 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
19726 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
19727 identifier of the TYPENAME_TYPE anymore.
19728 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
19729 TYPENAME_TYPE instead, we avoid messing up with a possible
19730 typedef variant case. */
19731 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
19733 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
19734 it first before we can figure out what NAME refers to. */
19735 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
19736 scope
= resolve_typename_type (scope
, only_current_p
);
19737 /* If we don't know what SCOPE refers to, then we cannot resolve the
19739 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
19741 /* If the SCOPE is a template type parameter, we have no way of
19742 resolving the name. */
19743 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
19745 /* If the SCOPE is not the current instantiation, there's no reason
19746 to look inside it. */
19747 if (only_current_p
&& !currently_open_class (scope
))
19749 /* If this is a typedef, we don't want to look inside (c++/11987). */
19750 if (typedef_variant_p (type
))
19752 /* If SCOPE isn't the template itself, it will not have a valid
19753 TYPE_FIELDS list. */
19754 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
19755 /* scope is either the template itself or a compatible instantiation
19756 like X<T>, so look up the name in the original template. */
19757 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
19759 /* scope is a partial instantiation, so we can't do the lookup or we
19760 will lose the template arguments. */
19762 /* Enter the SCOPE so that name lookup will be resolved as if we
19763 were in the class definition. In particular, SCOPE will no
19764 longer be considered a dependent type. */
19765 pushed_scope
= push_scope (scope
);
19766 /* Look up the declaration. */
19767 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
19768 tf_warning_or_error
);
19770 result
= NULL_TREE
;
19772 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
19773 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
19776 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == IDENTIFIER_NODE
19777 && TREE_CODE (decl
) == TYPE_DECL
)
19779 result
= TREE_TYPE (decl
);
19780 if (result
== error_mark_node
)
19781 result
= NULL_TREE
;
19783 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
19784 && DECL_CLASS_TEMPLATE_P (decl
))
19788 /* Obtain the template and the arguments. */
19789 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
19790 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
19791 /* Instantiate the template. */
19792 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
19793 /*entering_scope=*/0,
19794 tf_error
| tf_user
);
19795 if (result
== error_mark_node
)
19796 result
= NULL_TREE
;
19799 /* Leave the SCOPE. */
19801 pop_scope (pushed_scope
);
19803 /* If we failed to resolve it, return the original typename. */
19807 /* If lookup found a typename type, resolve that too. */
19808 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
19810 /* Ill-formed programs can cause infinite recursion here, so we
19811 must catch that. */
19812 TYPENAME_IS_RESOLVING_P (type
) = 1;
19813 result
= resolve_typename_type (result
, only_current_p
);
19814 TYPENAME_IS_RESOLVING_P (type
) = 0;
19817 /* Qualify the resulting type. */
19818 quals
= cp_type_quals (type
);
19820 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
19825 /* EXPR is an expression which is not type-dependent. Return a proxy
19826 for EXPR that can be used to compute the types of larger
19827 expressions containing EXPR. */
19830 build_non_dependent_expr (tree expr
)
19834 #ifdef ENABLE_CHECKING
19835 /* Try to get a constant value for all non-type-dependent expressions in
19836 order to expose bugs in *_dependent_expression_p and constexpr. */
19837 if (cxx_dialect
>= cxx0x
)
19838 maybe_constant_value (fold_non_dependent_expr_sfinae (expr
, tf_none
));
19841 /* Preserve OVERLOADs; the functions must be available to resolve
19844 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
19845 inner_expr
= stmt_expr_value_expr (inner_expr
);
19846 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
19847 inner_expr
= TREE_OPERAND (inner_expr
, 0);
19848 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
19849 inner_expr
= TREE_OPERAND (inner_expr
, 1);
19850 if (is_overloaded_fn (inner_expr
)
19851 || TREE_CODE (inner_expr
) == OFFSET_REF
)
19853 /* There is no need to return a proxy for a variable. */
19854 if (TREE_CODE (expr
) == VAR_DECL
)
19856 /* Preserve string constants; conversions from string constants to
19857 "char *" are allowed, even though normally a "const char *"
19858 cannot be used to initialize a "char *". */
19859 if (TREE_CODE (expr
) == STRING_CST
)
19861 /* Preserve arithmetic constants, as an optimization -- there is no
19862 reason to create a new node. */
19863 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
19865 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
19866 There is at least one place where we want to know that a
19867 particular expression is a throw-expression: when checking a ?:
19868 expression, there are special rules if the second or third
19869 argument is a throw-expression. */
19870 if (TREE_CODE (expr
) == THROW_EXPR
)
19873 /* Don't wrap an initializer list, we need to be able to look inside. */
19874 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
19877 /* Don't wrap a dummy object, we need to be able to test for it. */
19878 if (is_dummy_object (expr
))
19881 if (TREE_CODE (expr
) == COND_EXPR
)
19882 return build3 (COND_EXPR
,
19884 TREE_OPERAND (expr
, 0),
19885 (TREE_OPERAND (expr
, 1)
19886 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
19887 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
19888 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
19889 if (TREE_CODE (expr
) == COMPOUND_EXPR
19890 && !COMPOUND_EXPR_OVERLOADED (expr
))
19891 return build2 (COMPOUND_EXPR
,
19893 TREE_OPERAND (expr
, 0),
19894 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
19896 /* If the type is unknown, it can't really be non-dependent */
19897 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
19899 /* Otherwise, build a NON_DEPENDENT_EXPR. */
19900 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
19903 /* ARGS is a vector of expressions as arguments to a function call.
19904 Replace the arguments with equivalent non-dependent expressions.
19905 This modifies ARGS in place. */
19908 make_args_non_dependent (VEC(tree
,gc
) *args
)
19913 FOR_EACH_VEC_ELT (tree
, args
, ix
, arg
)
19915 tree newarg
= build_non_dependent_expr (arg
);
19917 VEC_replace (tree
, args
, ix
, newarg
);
19921 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
19922 with a level one deeper than the actual template parms. */
19927 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
19928 TYPE_NAME (au
) = build_decl (BUILTINS_LOCATION
,
19929 TYPE_DECL
, get_identifier ("auto"), au
);
19930 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
19931 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
19932 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
19933 TYPE_NAME (au
), NULL_TREE
);
19934 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
19935 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
19936 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
19941 /* Given type ARG, return std::initializer_list<ARG>. */
19946 tree std_init_list
= namespace_binding
19947 (get_identifier ("initializer_list"), std_node
);
19949 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
19951 error ("deducing from brace-enclosed initializer list requires "
19952 "#include <initializer_list>");
19953 return error_mark_node
;
19955 argvec
= make_tree_vec (1);
19956 TREE_VEC_ELT (argvec
, 0) = arg
;
19957 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
19958 NULL_TREE
, 0, tf_warning_or_error
);
19961 /* Replace auto in TYPE with std::initializer_list<auto>. */
19964 listify_autos (tree type
, tree auto_node
)
19966 tree init_auto
= listify (auto_node
);
19967 tree argvec
= make_tree_vec (1);
19968 TREE_VEC_ELT (argvec
, 0) = init_auto
;
19969 if (processing_template_decl
)
19970 argvec
= add_to_template_args (current_template_args (), argvec
);
19971 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
19974 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
19975 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
19978 do_auto_deduction (tree type
, tree init
, tree auto_node
)
19980 tree parms
, tparms
, targs
;
19984 if (init
== error_mark_node
)
19985 return error_mark_node
;
19987 if (type_dependent_expression_p (init
))
19988 /* Defining a subset of type-dependent expressions that we can deduce
19989 from ahead of time isn't worth the trouble. */
19992 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
19993 with either a new invented type template parameter U or, if the
19994 initializer is a braced-init-list (8.5.4), with
19995 std::initializer_list<U>. */
19996 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
19997 type
= listify_autos (type
, auto_node
);
19999 init
= resolve_nondeduced_context (init
);
20001 parms
= build_tree_list (NULL_TREE
, type
);
20003 tparms
= make_tree_vec (1);
20004 targs
= make_tree_vec (1);
20005 TREE_VEC_ELT (tparms
, 0)
20006 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
20007 val
= type_unification_real (tparms
, targs
, parms
, args
, 1, 0,
20008 DEDUCE_CALL
, LOOKUP_NORMAL
,
20009 /*explain_p=*/false);
20012 if (processing_template_decl
)
20013 /* Try again at instantiation time. */
20015 if (type
&& type
!= error_mark_node
)
20016 /* If type is error_mark_node a diagnostic must have been
20017 emitted by now. Also, having a mention to '<type error>'
20018 in the diagnostic is not really useful to the user. */
20020 if (cfun
&& auto_node
== current_function_auto_return_pattern
20021 && LAMBDA_FUNCTION_P (current_function_decl
))
20022 error ("unable to deduce lambda return type from %qE", init
);
20024 error ("unable to deduce %qT from %qE", type
, init
);
20026 return error_mark_node
;
20029 /* If the list of declarators contains more than one declarator, the type
20030 of each declared variable is determined as described above. If the
20031 type deduced for the template parameter U is not the same in each
20032 deduction, the program is ill-formed. */
20033 if (TREE_TYPE (auto_node
)
20034 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
20036 if (cfun
&& auto_node
== current_function_auto_return_pattern
20037 && LAMBDA_FUNCTION_P (current_function_decl
))
20038 error ("inconsistent types %qT and %qT deduced for "
20039 "lambda return type", TREE_TYPE (auto_node
),
20040 TREE_VEC_ELT (targs
, 0));
20042 error ("inconsistent deduction for %qT: %qT and then %qT",
20043 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
20044 return error_mark_node
;
20046 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
20048 if (processing_template_decl
)
20049 targs
= add_to_template_args (current_template_args (), targs
);
20050 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
20053 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
20057 splice_late_return_type (tree type
, tree late_return_type
)
20061 if (late_return_type
== NULL_TREE
)
20063 argvec
= make_tree_vec (1);
20064 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
20065 if (processing_template_parmlist
)
20066 /* For a late-specified return type in a template type-parameter, we
20067 need to add a dummy argument level for its parmlist. */
20068 argvec
= add_to_template_args
20069 (make_tree_vec (processing_template_parmlist
), argvec
);
20070 if (current_template_parms
)
20071 argvec
= add_to_template_args (current_template_args (), argvec
);
20072 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
20075 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
20078 is_auto (const_tree type
)
20080 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20081 && TYPE_IDENTIFIER (type
) == get_identifier ("auto"))
20087 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
20088 appear as a type-specifier for the declaration in question, we don't
20089 have to look through the whole type. */
20092 type_uses_auto (tree type
)
20094 enum tree_code code
;
20095 if (is_auto (type
))
20098 code
= TREE_CODE (type
);
20100 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
20101 || code
== OFFSET_TYPE
|| code
== FUNCTION_TYPE
20102 || code
== METHOD_TYPE
|| code
== ARRAY_TYPE
)
20103 return type_uses_auto (TREE_TYPE (type
));
20105 if (TYPE_PTRMEMFUNC_P (type
))
20106 return type_uses_auto (TREE_TYPE (TREE_TYPE
20107 (TYPE_PTRMEMFUNC_FN_TYPE (type
))));
20112 /* For a given template T, return the vector of typedefs referenced
20113 in T for which access check is needed at T instantiation time.
20114 T is either a FUNCTION_DECL or a RECORD_TYPE.
20115 Those typedefs were added to T by the function
20116 append_type_to_template_for_access_check. */
20118 VEC(qualified_typedef_usage_t
,gc
)*
20119 get_types_needing_access_check (tree t
)
20122 VEC(qualified_typedef_usage_t
,gc
) *result
= NULL
;
20124 if (!t
|| t
== error_mark_node
)
20127 if (!(ti
= get_template_info (t
)))
20130 if (CLASS_TYPE_P (t
)
20131 || TREE_CODE (t
) == FUNCTION_DECL
)
20133 if (!TI_TEMPLATE (ti
))
20136 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
20142 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
20143 tied to T. That list of typedefs will be access checked at
20144 T instantiation time.
20145 T is either a FUNCTION_DECL or a RECORD_TYPE.
20146 TYPE_DECL is a TYPE_DECL node representing a typedef.
20147 SCOPE is the scope through which TYPE_DECL is accessed.
20148 LOCATION is the location of the usage point of TYPE_DECL.
20150 This function is a subroutine of
20151 append_type_to_template_for_access_check. */
20154 append_type_to_template_for_access_check_1 (tree t
,
20157 location_t location
)
20159 qualified_typedef_usage_t typedef_usage
;
20162 if (!t
|| t
== error_mark_node
)
20165 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
20166 || CLASS_TYPE_P (t
))
20168 && TREE_CODE (type_decl
) == TYPE_DECL
20171 if (!(ti
= get_template_info (t
)))
20174 gcc_assert (TI_TEMPLATE (ti
));
20176 typedef_usage
.typedef_decl
= type_decl
;
20177 typedef_usage
.context
= scope
;
20178 typedef_usage
.locus
= location
;
20180 VEC_safe_push (qualified_typedef_usage_t
, gc
,
20181 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
),
20185 /* Append TYPE_DECL to the template TEMPL.
20186 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
20187 At TEMPL instanciation time, TYPE_DECL will be checked to see
20188 if it can be accessed through SCOPE.
20189 LOCATION is the location of the usage point of TYPE_DECL.
20191 e.g. consider the following code snippet:
20198 template<class U> struct S
20200 C::myint mi; // <-- usage point of the typedef C::myint
20205 At S<char> instantiation time, we need to check the access of C::myint
20206 In other words, we need to check the access of the myint typedef through
20207 the C scope. For that purpose, this function will add the myint typedef
20208 and the scope C through which its being accessed to a list of typedefs
20209 tied to the template S. That list will be walked at template instantiation
20210 time and access check performed on each typedefs it contains.
20211 Note that this particular code snippet should yield an error because
20212 myint is private to C. */
20215 append_type_to_template_for_access_check (tree templ
,
20218 location_t location
)
20220 qualified_typedef_usage_t
*iter
;
20223 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
20225 /* Make sure we don't append the type to the template twice. */
20226 FOR_EACH_VEC_ELT (qualified_typedef_usage_t
,
20227 get_types_needing_access_check (templ
),
20229 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
20232 append_type_to_template_for_access_check_1 (templ
, type_decl
,
20236 /* Set up the hash tables for template instantiations. */
20239 init_template_processing (void)
20241 decl_specializations
= htab_create_ggc (37,
20242 hash_specialization
,
20243 eq_specializations
,
20245 type_specializations
= htab_create_ggc (37,
20246 hash_specialization
,
20247 eq_specializations
,
20251 /* Print stats about the template hash tables for -fstats. */
20254 print_template_statistics (void)
20256 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
20257 "%f collisions\n", (long) htab_size (decl_specializations
),
20258 (long) htab_elements (decl_specializations
),
20259 htab_collisions (decl_specializations
));
20260 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
20261 "%f collisions\n", (long) htab_size (type_specializations
),
20262 (long) htab_elements (type_specializations
),
20263 htab_collisions (type_specializations
));
20266 #include "gt-cp-pt.h"