1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
29 #include "coretypes.h"
33 #include "pointer-set.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "tree-inline.h"
43 #include "tree-iterator.h"
45 /* The type of functions taking a tree, and some additional data, and
47 typedef int (*tree_fn_t
) (tree
, void*);
49 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
50 instantiations have been deferred, either because their definitions
51 were not yet available, or because we were putting off doing the work. */
52 struct GTY ((chain_next ("%h.next"))) pending_template
{
53 struct pending_template
*next
;
54 struct tinst_level
*tinst
;
57 static GTY(()) struct pending_template
*pending_templates
;
58 static GTY(()) struct pending_template
*last_pending_template
;
60 int processing_template_parmlist
;
61 static int template_header_count
;
63 static GTY(()) tree saved_trees
;
64 static vec
<int> inline_parm_levels
;
66 static GTY(()) struct tinst_level
*current_tinst_level
;
68 static GTY(()) tree saved_access_scope
;
70 /* Live only within one (recursive) call to tsubst_expr. We use
71 this to pass the statement expression node from the STMT_EXPR
72 to the EXPR_STMT that is its result. */
73 static tree cur_stmt_expr
;
75 /* A map from local variable declarations in the body of the template
76 presently being instantiated to the corresponding instantiated
78 static struct pointer_map_t
*local_specializations
;
80 /* True if we've recursed into fn_type_unification too many times. */
81 static bool excessive_deduction_depth
;
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
, va_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
, tree
);
129 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
131 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
133 static void tsubst_enum (tree
, tree
, tree
);
134 static tree
add_to_template_args (tree
, tree
);
135 static tree
add_outermost_template_args (tree
, tree
);
136 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
137 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
139 static int type_unification_real (tree
, tree
, tree
, const tree
*,
140 unsigned int, int, unification_kind_t
, int,
142 static void note_template_header (int);
143 static tree
convert_nontype_argument_function (tree
, tree
);
144 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
145 static tree
convert_template_argument (tree
, tree
, tree
,
146 tsubst_flags_t
, int, tree
);
147 static int for_each_template_parm (tree
, tree_fn_t
, void*,
148 struct pointer_set_t
*, bool);
149 static tree
expand_template_argument_pack (tree
);
150 static tree
build_template_parm_index (int, int, int, tree
, tree
);
151 static bool inline_needs_template_parms (tree
);
152 static void push_inline_template_parms_recursive (tree
, int);
153 static tree
retrieve_local_specialization (tree
);
154 static void register_local_specialization (tree
, tree
);
155 static hashval_t
hash_specialization (const void *p
);
156 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
157 static int mark_template_parm (tree
, void *);
158 static int template_parm_this_level_p (tree
, void *);
159 static tree
tsubst_friend_function (tree
, tree
);
160 static tree
tsubst_friend_class (tree
, tree
);
161 static int can_complete_type_without_circularity (tree
);
162 static tree
get_bindings (tree
, tree
, tree
, bool);
163 static int template_decl_level (tree
);
164 static int check_cv_quals_for_unify (int, tree
, tree
);
165 static void template_parm_level_and_index (tree
, int*, int*);
166 static int unify_pack_expansion (tree
, tree
, tree
,
167 tree
, unification_kind_t
, bool, bool);
168 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
169 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
170 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
171 static void regenerate_decl_from_template (tree
, tree
);
172 static tree
most_specialized_class (tree
, tree
, tsubst_flags_t
);
173 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
174 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
175 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
176 static bool check_specialization_scope (void);
177 static tree
process_partial_specialization (tree
);
178 static void set_current_access_from_decl (tree
);
179 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
181 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
182 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
184 static bool template_template_parm_bindings_ok_p (tree
, tree
);
185 static int template_args_equal (tree
, tree
);
186 static void tsubst_default_arguments (tree
);
187 static tree
for_each_template_parm_r (tree
*, int *, void *);
188 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
189 static void copy_default_args_to_explicit_spec (tree
);
190 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
191 static bool dependent_template_arg_p (tree
);
192 static bool any_template_arguments_need_structural_equality_p (tree
);
193 static bool dependent_type_p_r (tree
);
194 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
, bool);
195 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
196 static tree
tsubst_pack_expansion (tree
, tree
, tsubst_flags_t
, tree
);
197 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
198 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
199 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
201 static tree
listify (tree
);
202 static tree
listify_autos (tree
, tree
);
203 static tree
template_parm_to_arg (tree t
);
204 static tree
current_template_args (void);
205 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
206 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
208 /* Make the current scope suitable for access checking when we are
209 processing T. T can be FUNCTION_DECL for instantiated function
210 template, VAR_DECL for static member variable, or TYPE_DECL for
211 alias template (needed by instantiate_decl). */
214 push_access_scope (tree t
)
216 gcc_assert (TREE_CODE (t
) == FUNCTION_DECL
217 || TREE_CODE (t
) == TYPE_DECL
218 || TREE_CODE (t
) == VAR_DECL
);
220 if (DECL_FRIEND_CONTEXT (t
))
221 push_nested_class (DECL_FRIEND_CONTEXT (t
));
222 else if (DECL_CLASS_SCOPE_P (t
))
223 push_nested_class (DECL_CONTEXT (t
));
225 push_to_top_level ();
227 if (TREE_CODE (t
) == FUNCTION_DECL
)
229 saved_access_scope
= tree_cons
230 (NULL_TREE
, current_function_decl
, saved_access_scope
);
231 current_function_decl
= t
;
235 /* Restore the scope set up by push_access_scope. T is the node we
239 pop_access_scope (tree t
)
241 if (TREE_CODE (t
) == FUNCTION_DECL
)
243 current_function_decl
= TREE_VALUE (saved_access_scope
);
244 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
247 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
250 pop_from_top_level ();
253 /* Do any processing required when DECL (a member template
254 declaration) is finished. Returns the TEMPLATE_DECL corresponding
255 to DECL, unless it is a specialization, in which case the DECL
256 itself is returned. */
259 finish_member_template_decl (tree decl
)
261 if (decl
== error_mark_node
)
262 return error_mark_node
;
264 gcc_assert (DECL_P (decl
));
266 if (TREE_CODE (decl
) == TYPE_DECL
)
270 type
= TREE_TYPE (decl
);
271 if (type
== error_mark_node
)
272 return error_mark_node
;
273 if (MAYBE_CLASS_TYPE_P (type
)
274 && CLASSTYPE_TEMPLATE_INFO (type
)
275 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
277 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
278 check_member_template (tmpl
);
283 else if (TREE_CODE (decl
) == FIELD_DECL
)
284 error ("data member %qD cannot be a member template", decl
);
285 else if (DECL_TEMPLATE_INFO (decl
))
287 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
289 check_member_template (DECL_TI_TEMPLATE (decl
));
290 return DECL_TI_TEMPLATE (decl
);
296 error ("invalid member template declaration %qD", decl
);
298 return error_mark_node
;
301 /* Create a template info node. */
304 build_template_info (tree template_decl
, tree template_args
)
306 tree result
= make_node (TEMPLATE_INFO
);
307 TI_TEMPLATE (result
) = template_decl
;
308 TI_ARGS (result
) = template_args
;
312 /* Return the template info node corresponding to T, whatever T is. */
315 get_template_info (const_tree t
)
317 tree tinfo
= NULL_TREE
;
319 if (!t
|| t
== error_mark_node
)
322 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
323 tinfo
= DECL_TEMPLATE_INFO (t
);
325 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
328 if (TAGGED_TYPE_P (t
))
329 tinfo
= TYPE_TEMPLATE_INFO (t
);
330 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
331 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
336 /* Returns the template nesting level of the indicated class TYPE.
346 A<T>::B<U> has depth two, while A<T> has depth one.
347 Both A<T>::B<int> and A<int>::B<U> have depth one, if
348 they are instantiations, not specializations.
350 This function is guaranteed to return 0 if passed NULL_TREE so
351 that, for example, `template_class_depth (current_class_type)' is
355 template_class_depth (tree type
)
360 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
361 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
362 ? CP_DECL_CONTEXT (type
) : CP_TYPE_CONTEXT (type
))
364 tree tinfo
= get_template_info (type
);
366 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
367 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
374 /* Subroutine of maybe_begin_member_template_processing.
375 Returns true if processing DECL needs us to push template parms. */
378 inline_needs_template_parms (tree decl
)
380 if (! DECL_TEMPLATE_INFO (decl
))
383 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
384 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
387 /* Subroutine of maybe_begin_member_template_processing.
388 Push the template parms in PARMS, starting from LEVELS steps into the
389 chain, and ending at the beginning, since template parms are listed
393 push_inline_template_parms_recursive (tree parmlist
, int levels
)
395 tree parms
= TREE_VALUE (parmlist
);
399 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
401 ++processing_template_decl
;
402 current_template_parms
403 = tree_cons (size_int (processing_template_decl
),
404 parms
, current_template_parms
);
405 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
407 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
409 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
411 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
413 if (parm
== error_mark_node
)
416 gcc_assert (DECL_P (parm
));
418 switch (TREE_CODE (parm
))
427 /* Make a CONST_DECL as is done in process_template_parm.
428 It is ugly that we recreate this here; the original
429 version built in process_template_parm is no longer
431 tree decl
= build_decl (DECL_SOURCE_LOCATION (parm
),
432 CONST_DECL
, DECL_NAME (parm
),
434 DECL_ARTIFICIAL (decl
) = 1;
435 TREE_CONSTANT (decl
) = 1;
436 TREE_READONLY (decl
) = 1;
437 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
438 SET_DECL_TEMPLATE_PARM_P (decl
);
449 /* Restore the template parameter context for a member template or
450 a friend template defined in a class definition. */
453 maybe_begin_member_template_processing (tree decl
)
458 if (inline_needs_template_parms (decl
))
460 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
461 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
463 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
466 parms
= TREE_CHAIN (parms
);
469 push_inline_template_parms_recursive (parms
, levels
);
472 /* Remember how many levels of template parameters we pushed so that
473 we can pop them later. */
474 inline_parm_levels
.safe_push (levels
);
477 /* Undo the effects of maybe_begin_member_template_processing. */
480 maybe_end_member_template_processing (void)
485 if (inline_parm_levels
.length () == 0)
488 last
= inline_parm_levels
.pop ();
489 for (i
= 0; i
< last
; ++i
)
491 --processing_template_decl
;
492 current_template_parms
= TREE_CHAIN (current_template_parms
);
497 /* Return a new template argument vector which contains all of ARGS,
498 but has as its innermost set of arguments the EXTRA_ARGS. */
501 add_to_template_args (tree args
, tree extra_args
)
508 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
511 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
512 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
514 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
515 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
517 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
518 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
523 /* Like add_to_template_args, but only the outermost ARGS are added to
524 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
525 (EXTRA_ARGS) levels are added. This function is used to combine
526 the template arguments from a partial instantiation with the
527 template arguments used to attain the full instantiation from the
528 partial instantiation. */
531 add_outermost_template_args (tree args
, tree extra_args
)
535 /* If there are more levels of EXTRA_ARGS than there are ARGS,
536 something very fishy is going on. */
537 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
539 /* If *all* the new arguments will be the EXTRA_ARGS, just return
541 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
544 /* For the moment, we make ARGS look like it contains fewer levels. */
545 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
547 new_args
= add_to_template_args (args
, extra_args
);
549 /* Now, we restore ARGS to its full dimensions. */
550 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
555 /* Return the N levels of innermost template arguments from the ARGS. */
558 get_innermost_template_args (tree args
, int n
)
566 /* If N is 1, just return the innermost set of template arguments. */
568 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
570 /* If we're not removing anything, just return the arguments we were
572 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
573 gcc_assert (extra_levels
>= 0);
574 if (extra_levels
== 0)
577 /* Make a new set of arguments, not containing the outer arguments. */
578 new_args
= make_tree_vec (n
);
579 for (i
= 1; i
<= n
; ++i
)
580 SET_TMPL_ARGS_LEVEL (new_args
, i
,
581 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
586 /* The inverse of get_innermost_template_args: Return all but the innermost
587 EXTRA_LEVELS levels of template arguments from the ARGS. */
590 strip_innermost_template_args (tree args
, int extra_levels
)
593 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
598 /* If N is 1, just return the outermost set of template arguments. */
600 return TMPL_ARGS_LEVEL (args
, 1);
602 /* If we're not removing anything, just return the arguments we were
604 gcc_assert (extra_levels
>= 0);
605 if (extra_levels
== 0)
608 /* Make a new set of arguments, not containing the inner arguments. */
609 new_args
= make_tree_vec (n
);
610 for (i
= 1; i
<= n
; ++i
)
611 SET_TMPL_ARGS_LEVEL (new_args
, i
,
612 TMPL_ARGS_LEVEL (args
, i
));
617 /* We've got a template header coming up; push to a new level for storing
621 begin_template_parm_list (void)
623 /* We use a non-tag-transparent scope here, which causes pushtag to
624 put tags in this scope, rather than in the enclosing class or
625 namespace scope. This is the right thing, since we want
626 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
627 global template class, push_template_decl handles putting the
628 TEMPLATE_DECL into top-level scope. For a nested template class,
631 template <class T> struct S1 {
632 template <class T> struct S2 {};
635 pushtag contains special code to call pushdecl_with_scope on the
636 TEMPLATE_DECL for S2. */
637 begin_scope (sk_template_parms
, NULL
);
638 ++processing_template_decl
;
639 ++processing_template_parmlist
;
640 note_template_header (0);
643 /* This routine is called when a specialization is declared. If it is
644 invalid to declare a specialization here, an error is reported and
645 false is returned, otherwise this routine will return true. */
648 check_specialization_scope (void)
650 tree scope
= current_scope ();
654 An explicit specialization shall be declared in the namespace of
655 which the template is a member, or, for member templates, in the
656 namespace of which the enclosing class or enclosing class
657 template is a member. An explicit specialization of a member
658 function, member class or static data member of a class template
659 shall be declared in the namespace of which the class template
661 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
663 error ("explicit specialization in non-namespace scope %qD", scope
);
669 In an explicit specialization declaration for a member of a class
670 template or a member template that appears in namespace scope,
671 the member template and some of its enclosing class templates may
672 remain unspecialized, except that the declaration shall not
673 explicitly specialize a class member template if its enclosing
674 class templates are not explicitly specialized as well. */
675 if (current_template_parms
)
677 error ("enclosing class templates are not explicitly specialized");
684 /* We've just seen template <>. */
687 begin_specialization (void)
689 begin_scope (sk_template_spec
, NULL
);
690 note_template_header (1);
691 return check_specialization_scope ();
694 /* Called at then end of processing a declaration preceded by
698 end_specialization (void)
701 reset_specialization ();
704 /* Any template <>'s that we have seen thus far are not referring to a
705 function specialization. */
708 reset_specialization (void)
710 processing_specialization
= 0;
711 template_header_count
= 0;
714 /* We've just seen a template header. If SPECIALIZATION is nonzero,
715 it was of the form template <>. */
718 note_template_header (int specialization
)
720 processing_specialization
= specialization
;
721 template_header_count
++;
724 /* We're beginning an explicit instantiation. */
727 begin_explicit_instantiation (void)
729 gcc_assert (!processing_explicit_instantiation
);
730 processing_explicit_instantiation
= true;
735 end_explicit_instantiation (void)
737 gcc_assert (processing_explicit_instantiation
);
738 processing_explicit_instantiation
= false;
741 /* An explicit specialization or partial specialization of TMPL is being
742 declared. Check that the namespace in which the specialization is
743 occurring is permissible. Returns false iff it is invalid to
744 specialize TMPL in the current namespace. */
747 check_specialization_namespace (tree tmpl
)
749 tree tpl_ns
= decl_namespace_context (tmpl
);
753 An explicit specialization shall be declared in the namespace of
754 which the template is a member, or, for member templates, in the
755 namespace of which the enclosing class or enclosing class
756 template is a member. An explicit specialization of a member
757 function, member class or static data member of a class template
758 shall be declared in the namespace of which the class template is
760 if (current_scope() != DECL_CONTEXT (tmpl
)
761 && !at_namespace_scope_p ())
763 error ("specialization of %qD must appear at namespace scope", tmpl
);
766 if (is_associated_namespace (current_namespace
, tpl_ns
))
767 /* Same or super-using namespace. */
771 permerror (input_location
, "specialization of %qD in different namespace", tmpl
);
772 permerror (input_location
, " from definition of %q+#D", tmpl
);
777 /* SPEC is an explicit instantiation. Check that it is valid to
778 perform this explicit instantiation in the current namespace. */
781 check_explicit_instantiation_namespace (tree spec
)
785 /* DR 275: An explicit instantiation shall appear in an enclosing
786 namespace of its template. */
787 ns
= decl_namespace_context (spec
);
788 if (!is_ancestor (current_namespace
, ns
))
789 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
790 "(which does not enclose namespace %qD)",
791 spec
, current_namespace
, ns
);
794 /* The TYPE is being declared. If it is a template type, that means it
795 is a partial specialization. Do appropriate error-checking. */
798 maybe_process_partial_specialization (tree type
)
802 if (type
== error_mark_node
)
803 return error_mark_node
;
805 /* A lambda that appears in specialization context is not itself a
807 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
810 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
812 error ("name of class shadows template template parameter %qD",
814 return error_mark_node
;
817 context
= TYPE_CONTEXT (type
);
819 if (TYPE_ALIAS_P (type
))
821 if (TYPE_TEMPLATE_INFO (type
)
822 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type
)))
823 error ("specialization of alias template %qD",
824 TYPE_TI_TEMPLATE (type
));
826 error ("explicit specialization of non-template %qT", type
);
827 return error_mark_node
;
829 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
831 /* This is for ordinary explicit specialization and partial
832 specialization of a template class such as:
834 template <> class C<int>;
838 template <class T> class C<T*>;
840 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
842 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
843 && !COMPLETE_TYPE_P (type
))
845 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
));
846 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
847 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
848 if (processing_template_decl
)
850 if (push_template_decl (TYPE_MAIN_DECL (type
))
852 return error_mark_node
;
855 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
856 error ("specialization of %qT after instantiation", type
);
857 else if (errorcount
&& !processing_specialization
858 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
859 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
860 /* Trying to define a specialization either without a template<> header
861 or in an inappropriate place. We've already given an error, so just
862 bail now so we don't actually define the specialization. */
863 return error_mark_node
;
865 else if (CLASS_TYPE_P (type
)
866 && !CLASSTYPE_USE_TEMPLATE (type
)
867 && CLASSTYPE_TEMPLATE_INFO (type
)
868 && context
&& CLASS_TYPE_P (context
)
869 && CLASSTYPE_TEMPLATE_INFO (context
))
871 /* This is for an explicit specialization of member class
872 template according to [temp.expl.spec/18]:
874 template <> template <class U> class C<int>::D;
876 The context `C<int>' must be an implicit instantiation.
877 Otherwise this is just a member class template declared
880 template <> class C<int> { template <class U> class D; };
881 template <> template <class U> class C<int>::D;
883 In the first case, `C<int>::D' is a specialization of `C<T>::D'
884 while in the second case, `C<int>::D' is a primary template
885 and `C<T>::D' may not exist. */
887 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
888 && !COMPLETE_TYPE_P (type
))
891 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
893 if (current_namespace
894 != decl_namespace_context (tmpl
))
896 permerror (input_location
, "specializing %q#T in different namespace", type
);
897 permerror (input_location
, " from definition of %q+#D", tmpl
);
900 /* Check for invalid specialization after instantiation:
902 template <> template <> class C<int>::D<int>;
903 template <> template <class U> class C<int>::D; */
905 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
906 t
; t
= TREE_CHAIN (t
))
908 tree inst
= TREE_VALUE (t
);
909 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
))
911 /* We already have a full specialization of this partial
912 instantiation. Reassign it to the new member
913 specialization template. */
918 elt
.tmpl
= most_general_template (tmpl
);
919 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
922 htab_remove_elt (type_specializations
, &elt
);
925 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
927 slot
= htab_find_slot (type_specializations
, &elt
, INSERT
);
928 entry
= ggc_alloc_spec_entry ();
932 else if (COMPLETE_OR_OPEN_TYPE_P (inst
))
933 /* But if we've had an implicit instantiation, that's a
934 problem ([temp.expl.spec]/6). */
935 error ("specialization %qT after instantiation %qT",
939 /* Mark TYPE as a specialization. And as a result, we only
940 have one level of template argument for the innermost
942 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
943 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
944 CLASSTYPE_TI_ARGS (type
)
945 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
948 else if (processing_specialization
)
950 /* Someday C++0x may allow for enum template specialization. */
951 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
952 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
953 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
954 "of %qD not allowed by ISO C++", type
);
957 error ("explicit specialization of non-template %qT", type
);
958 return error_mark_node
;
965 /* Returns nonzero if we can optimize the retrieval of specializations
966 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
967 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
970 optimize_specialization_lookup_p (tree tmpl
)
972 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
973 && DECL_CLASS_SCOPE_P (tmpl
)
974 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
976 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
977 /* The optimized lookup depends on the fact that the
978 template arguments for the member function template apply
979 purely to the containing class, which is not true if the
980 containing class is an explicit or partial
982 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
983 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
984 && !DECL_CONV_FN_P (tmpl
)
985 /* It is possible to have a template that is not a member
986 template and is not a member of a template class:
988 template <typename T>
989 struct S { friend A::f(); };
991 Here, the friend function is a template, but the context does
992 not have template information. The optimized lookup relies
993 on having ARGS be the template arguments for both the class
994 and the function template. */
995 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
998 /* Retrieve the specialization (in the sense of [temp.spec] - a
999 specialization is either an instantiation or an explicit
1000 specialization) of TMPL for the given template ARGS. If there is
1001 no such specialization, return NULL_TREE. The ARGS are a vector of
1002 arguments, or a vector of vectors of arguments, in the case of
1003 templates with more than one level of parameters.
1005 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1006 then we search for a partial specialization matching ARGS. This
1007 parameter is ignored if TMPL is not a class template. */
1010 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1012 if (tmpl
== NULL_TREE
)
1015 if (args
== error_mark_node
)
1018 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
1020 /* There should be as many levels of arguments as there are
1021 levels of parameters. */
1022 gcc_assert (TMPL_ARGS_DEPTH (args
)
1023 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
1025 if (optimize_specialization_lookup_p (tmpl
))
1027 tree class_template
;
1028 tree class_specialization
;
1029 vec
<tree
, va_gc
> *methods
;
1033 /* The template arguments actually apply to the containing
1034 class. Find the class specialization with those
1036 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1037 class_specialization
1038 = retrieve_specialization (class_template
, args
, 0);
1039 if (!class_specialization
)
1041 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1042 for the specialization. */
1043 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1046 /* Iterate through the methods with the indicated name, looking
1047 for the one that has an instance of TMPL. */
1048 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1049 for (fns
= (*methods
)[idx
]; fns
; fns
= OVL_NEXT (fns
))
1051 tree fn
= OVL_CURRENT (fns
);
1052 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1053 /* using-declarations can add base methods to the method vec,
1054 and we don't want those here. */
1055 && DECL_CONTEXT (fn
) == class_specialization
)
1064 htab_t specializations
;
1068 elt
.spec
= NULL_TREE
;
1070 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1071 specializations
= type_specializations
;
1073 specializations
= decl_specializations
;
1076 hash
= hash_specialization (&elt
);
1077 found
= (spec_entry
*) htab_find_with_hash (specializations
, &elt
, hash
);
1085 /* Like retrieve_specialization, but for local declarations. */
1088 retrieve_local_specialization (tree tmpl
)
1092 if (local_specializations
== NULL
)
1095 slot
= pointer_map_contains (local_specializations
, tmpl
);
1096 return slot
? (tree
) *slot
: NULL_TREE
;
1099 /* Returns nonzero iff DECL is a specialization of TMPL. */
1102 is_specialization_of (tree decl
, tree tmpl
)
1106 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1110 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1116 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1118 for (t
= TREE_TYPE (decl
);
1120 t
= CLASSTYPE_USE_TEMPLATE (t
)
1121 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1122 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1129 /* Returns nonzero iff DECL is a specialization of friend declaration
1130 FRIEND_DECL according to [temp.friend]. */
1133 is_specialization_of_friend (tree decl
, tree friend_decl
)
1135 bool need_template
= true;
1138 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1139 || TREE_CODE (decl
) == TYPE_DECL
);
1141 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1142 of a template class, we want to check if DECL is a specialization
1144 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1145 && DECL_TEMPLATE_INFO (friend_decl
)
1146 && !DECL_USE_TEMPLATE (friend_decl
))
1148 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1149 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1150 need_template
= false;
1152 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1153 && !PRIMARY_TEMPLATE_P (friend_decl
))
1154 need_template
= false;
1156 /* There is nothing to do if this is not a template friend. */
1157 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1160 if (is_specialization_of (decl
, friend_decl
))
1164 A member of a class template may be declared to be a friend of a
1165 non-template class. In this case, the corresponding member of
1166 every specialization of the class template is a friend of the
1167 class granting friendship.
1169 For example, given a template friend declaration
1171 template <class T> friend void A<T>::f();
1173 the member function below is considered a friend
1175 template <> struct A<int> {
1179 For this type of template friend, TEMPLATE_DEPTH below will be
1180 nonzero. To determine if DECL is a friend of FRIEND, we first
1181 check if the enclosing class is a specialization of another. */
1183 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1185 && DECL_CLASS_SCOPE_P (decl
)
1186 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1187 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1189 /* Next, we check the members themselves. In order to handle
1190 a few tricky cases, such as when FRIEND_DECL's are
1192 template <class T> friend void A<T>::g(T t);
1193 template <class T> template <T t> friend void A<T>::h();
1197 void A<int>::g(int);
1198 template <int> void A<int>::h();
1200 we need to figure out ARGS, the template arguments from
1201 the context of DECL. This is required for template substitution
1202 of `T' in the function parameter of `g' and template parameter
1203 of `h' in the above examples. Here ARGS corresponds to `int'. */
1205 tree context
= DECL_CONTEXT (decl
);
1206 tree args
= NULL_TREE
;
1207 int current_depth
= 0;
1209 while (current_depth
< template_depth
)
1211 if (CLASSTYPE_TEMPLATE_INFO (context
))
1213 if (current_depth
== 0)
1214 args
= TYPE_TI_ARGS (context
);
1216 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1219 context
= TYPE_CONTEXT (context
);
1222 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1227 tree friend_args_type
;
1228 tree decl_args_type
;
1230 /* Make sure that both DECL and FRIEND_DECL are templates or
1232 is_template
= DECL_TEMPLATE_INFO (decl
)
1233 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1234 if (need_template
^ is_template
)
1236 else if (is_template
)
1238 /* If both are templates, check template parameter list. */
1240 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1242 if (!comp_template_parms
1243 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1247 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1250 decl_type
= TREE_TYPE (decl
);
1252 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1253 tf_none
, NULL_TREE
);
1254 if (friend_type
== error_mark_node
)
1257 /* Check if return types match. */
1258 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1261 /* Check if function parameter types match, ignoring the
1262 `this' parameter. */
1263 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1264 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1265 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1266 friend_args_type
= TREE_CHAIN (friend_args_type
);
1267 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1268 decl_args_type
= TREE_CHAIN (decl_args_type
);
1270 return compparms (decl_args_type
, friend_args_type
);
1274 /* DECL is a TYPE_DECL */
1276 tree decl_type
= TREE_TYPE (decl
);
1278 /* Make sure that both DECL and FRIEND_DECL are templates or
1281 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1282 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1284 if (need_template
^ is_template
)
1286 else if (is_template
)
1289 /* If both are templates, check the name of the two
1290 TEMPLATE_DECL's first because is_friend didn't. */
1291 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1292 != DECL_NAME (friend_decl
))
1295 /* Now check template parameter list. */
1297 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1299 return comp_template_parms
1300 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1304 return (DECL_NAME (decl
)
1305 == DECL_NAME (friend_decl
));
1311 /* Register the specialization SPEC as a specialization of TMPL with
1312 the indicated ARGS. IS_FRIEND indicates whether the specialization
1313 is actually just a friend declaration. Returns SPEC, or an
1314 equivalent prior declaration, if available. */
1317 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1324 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
));
1326 if (TREE_CODE (spec
) == FUNCTION_DECL
1327 && uses_template_parms (DECL_TI_ARGS (spec
)))
1328 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1329 register it; we want the corresponding TEMPLATE_DECL instead.
1330 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1331 the more obvious `uses_template_parms (spec)' to avoid problems
1332 with default function arguments. In particular, given
1333 something like this:
1335 template <class T> void f(T t1, T t = T())
1337 the default argument expression is not substituted for in an
1338 instantiation unless and until it is actually needed. */
1341 if (optimize_specialization_lookup_p (tmpl
))
1342 /* We don't put these specializations in the hash table, but we might
1343 want to give an error about a mismatch. */
1344 fn
= retrieve_specialization (tmpl
, args
, 0);
1352 hash
= hash_specialization (&elt
);
1355 htab_find_slot_with_hash (decl_specializations
, &elt
, hash
, INSERT
);
1357 fn
= ((spec_entry
*) *slot
)->spec
;
1362 /* We can sometimes try to re-register a specialization that we've
1363 already got. In particular, regenerate_decl_from_template calls
1364 duplicate_decls which will update the specialization list. But,
1365 we'll still get called again here anyhow. It's more convenient
1366 to simply allow this than to try to prevent it. */
1369 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1371 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1373 if (DECL_ODR_USED (fn
)
1374 || DECL_EXPLICIT_INSTANTIATION (fn
))
1376 error ("specialization of %qD after instantiation",
1378 return error_mark_node
;
1383 /* This situation should occur only if the first
1384 specialization is an implicit instantiation, the
1385 second is an explicit specialization, and the
1386 implicit instantiation has not yet been used. That
1387 situation can occur if we have implicitly
1388 instantiated a member function and then specialized
1391 We can also wind up here if a friend declaration that
1392 looked like an instantiation turns out to be a
1395 template <class T> void foo(T);
1396 class S { friend void foo<>(int) };
1397 template <> void foo(int);
1399 We transform the existing DECL in place so that any
1400 pointers to it become pointers to the updated
1403 If there was a definition for the template, but not
1404 for the specialization, we want this to look as if
1405 there were no definition, and vice versa. */
1406 DECL_INITIAL (fn
) = NULL_TREE
;
1407 duplicate_decls (spec
, fn
, is_friend
);
1408 /* The call to duplicate_decls will have applied
1411 An explicit specialization of a function template
1412 is inline only if it is explicitly declared to be,
1413 and independently of whether its function template
1416 to the primary function; now copy the inline bits to
1417 the various clones. */
1418 FOR_EACH_CLONE (clone
, fn
)
1420 DECL_DECLARED_INLINE_P (clone
)
1421 = DECL_DECLARED_INLINE_P (fn
);
1422 DECL_SOURCE_LOCATION (clone
)
1423 = DECL_SOURCE_LOCATION (fn
);
1425 check_specialization_namespace (tmpl
);
1430 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1432 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1433 /* Dup decl failed, but this is a new definition. Set the
1434 line number so any errors match this new
1436 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1442 return duplicate_decls (spec
, fn
, is_friend
);
1444 /* A specialization must be declared in the same namespace as the
1445 template it is specializing. */
1446 if (DECL_TEMPLATE_SPECIALIZATION (spec
)
1447 && !check_specialization_namespace (tmpl
))
1448 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1450 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1452 spec_entry
*entry
= ggc_alloc_spec_entry ();
1453 gcc_assert (tmpl
&& args
&& spec
);
1456 if (TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1457 && PRIMARY_TEMPLATE_P (tmpl
)
1458 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1459 /* TMPL is a forward declaration of a template function; keep a list
1460 of all specializations in case we need to reassign them to a friend
1461 template later in tsubst_friend_function. */
1462 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1463 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1469 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1470 TMPL and ARGS members, ignores SPEC. */
1472 int comparing_specializations
;
1475 eq_specializations (const void *p1
, const void *p2
)
1477 const spec_entry
*e1
= (const spec_entry
*)p1
;
1478 const spec_entry
*e2
= (const spec_entry
*)p2
;
1481 ++comparing_specializations
;
1482 equal
= (e1
->tmpl
== e2
->tmpl
1483 && comp_template_args (e1
->args
, e2
->args
));
1484 --comparing_specializations
;
1489 /* Returns a hash for a template TMPL and template arguments ARGS. */
1492 hash_tmpl_and_args (tree tmpl
, tree args
)
1494 hashval_t val
= DECL_UID (tmpl
);
1495 return iterative_hash_template_arg (args
, val
);
1498 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1502 hash_specialization (const void *p
)
1504 const spec_entry
*e
= (const spec_entry
*)p
;
1505 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1508 /* Recursively calculate a hash value for a template argument ARG, for use
1509 in the hash tables of template specializations. */
1512 iterative_hash_template_arg (tree arg
, hashval_t val
)
1514 unsigned HOST_WIDE_INT i
;
1515 enum tree_code code
;
1518 if (arg
== NULL_TREE
)
1519 return iterative_hash_object (arg
, val
);
1524 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1525 /* We can get one of these when re-hashing a previous entry in the middle
1526 of substituting into a pack expansion. Just look through it. */
1527 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1529 code
= TREE_CODE (arg
);
1530 tclass
= TREE_CODE_CLASS (code
);
1532 val
= iterative_hash_object (code
, val
);
1539 case IDENTIFIER_NODE
:
1540 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1544 int i
, len
= TREE_VEC_LENGTH (arg
);
1545 for (i
= 0; i
< len
; ++i
)
1546 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1550 case TYPE_PACK_EXPANSION
:
1551 case EXPR_PACK_EXPANSION
:
1552 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1553 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1555 case TYPE_ARGUMENT_PACK
:
1556 case NONTYPE_ARGUMENT_PACK
:
1557 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1560 for (; arg
; arg
= TREE_CHAIN (arg
))
1561 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1565 for (; arg
; arg
= OVL_NEXT (arg
))
1566 val
= iterative_hash_template_arg (OVL_CURRENT (arg
), val
);
1572 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1574 val
= iterative_hash_template_arg (field
, val
);
1575 val
= iterative_hash_template_arg (value
, val
);
1581 if (!DECL_ARTIFICIAL (arg
))
1583 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1584 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1586 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1589 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1592 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1593 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1595 case TEMPLATE_PARM_INDEX
:
1596 val
= iterative_hash_template_arg
1597 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1598 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1599 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1602 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1603 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1604 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1607 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1609 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1613 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1614 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1615 val
= iterative_hash_object (code
, val
);
1616 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1619 /* A lambda can't appear in a template arg, but don't crash on
1621 gcc_assert (seen_error ());
1625 case IMPLICIT_CONV_EXPR
:
1626 case STATIC_CAST_EXPR
:
1627 case REINTERPRET_CAST_EXPR
:
1628 case CONST_CAST_EXPR
:
1629 case DYNAMIC_CAST_EXPR
:
1631 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1632 /* Now hash operands as usual. */
1642 if (TYPE_CANONICAL (arg
))
1643 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1645 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1646 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1647 /* Otherwise just compare the types during lookup. */
1650 case tcc_declaration
:
1652 return iterative_hash_expr (arg
, val
);
1655 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1657 unsigned n
= cp_tree_operand_length (arg
);
1658 for (i
= 0; i
< n
; ++i
)
1659 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1667 /* Unregister the specialization SPEC as a specialization of TMPL.
1668 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1669 if the SPEC was listed as a specialization of TMPL.
1671 Note that SPEC has been ggc_freed, so we can't look inside it. */
1674 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1679 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1680 elt
.args
= TI_ARGS (tinfo
);
1681 elt
.spec
= NULL_TREE
;
1683 entry
= (spec_entry
*) htab_find (decl_specializations
, &elt
);
1686 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1687 gcc_assert (new_spec
!= NULL_TREE
);
1688 entry
->spec
= new_spec
;
1695 /* Like register_specialization, but for local declarations. We are
1696 registering SPEC, an instantiation of TMPL. */
1699 register_local_specialization (tree spec
, tree tmpl
)
1703 slot
= pointer_map_insert (local_specializations
, tmpl
);
1707 /* TYPE is a class type. Returns true if TYPE is an explicitly
1708 specialized class. */
1711 explicit_class_specialization_p (tree type
)
1713 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1715 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1718 /* Print the list of functions at FNS, going through all the overloads
1719 for each element of the list. Alternatively, FNS can not be a
1720 TREE_LIST, in which case it will be printed together with all the
1723 MORE and *STR should respectively be FALSE and NULL when the function
1724 is called from the outside. They are used internally on recursive
1725 calls. print_candidates manages the two parameters and leaves NULL
1726 in *STR when it ends. */
1729 print_candidates_1 (tree fns
, bool more
, const char **str
)
1732 char *spaces
= NULL
;
1734 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1735 if (TREE_CODE (fn
) == TREE_LIST
)
1737 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1738 print_candidates_1 (TREE_VALUE (fn2
),
1739 TREE_CHAIN (fn2
) || more
, str
);
1743 tree cand
= OVL_CURRENT (fn
);
1746 /* Pick the prefix string. */
1747 if (!more
&& !OVL_NEXT (fns
))
1749 inform (DECL_SOURCE_LOCATION (cand
),
1750 "candidate is: %#D", cand
);
1754 *str
= _("candidates are:");
1755 spaces
= get_spaces (*str
);
1757 inform (DECL_SOURCE_LOCATION (cand
), "%s %#D", *str
, cand
);
1758 *str
= spaces
? spaces
: *str
;
1768 /* Print the list of candidate FNS in an error message. FNS can also
1769 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1772 print_candidates (tree fns
)
1774 const char *str
= NULL
;
1775 print_candidates_1 (fns
, false, &str
);
1776 gcc_assert (str
== NULL
);
1779 /* Returns the template (one of the functions given by TEMPLATE_ID)
1780 which can be specialized to match the indicated DECL with the
1781 explicit template args given in TEMPLATE_ID. The DECL may be
1782 NULL_TREE if none is available. In that case, the functions in
1783 TEMPLATE_ID are non-members.
1785 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1786 specialization of a member template.
1788 The TEMPLATE_COUNT is the number of references to qualifying
1789 template classes that appeared in the name of the function. See
1790 check_explicit_specialization for a more accurate description.
1792 TSK indicates what kind of template declaration (if any) is being
1793 declared. TSK_TEMPLATE indicates that the declaration given by
1794 DECL, though a FUNCTION_DECL, has template parameters, and is
1795 therefore a template function.
1797 The template args (those explicitly specified and those deduced)
1798 are output in a newly created vector *TARGS_OUT.
1800 If it is impossible to determine the result, an error message is
1801 issued. The error_mark_node is returned to indicate failure. */
1804 determine_specialization (tree template_id
,
1807 int need_member_template
,
1813 tree explicit_targs
;
1814 tree candidates
= NULL_TREE
;
1815 /* A TREE_LIST of templates of which DECL may be a specialization.
1816 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1817 corresponding TREE_PURPOSE is the set of template arguments that,
1818 when used to instantiate the template, would produce a function
1819 with the signature of DECL. */
1820 tree templates
= NULL_TREE
;
1822 cp_binding_level
*b
;
1824 *targs_out
= NULL_TREE
;
1826 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1827 return error_mark_node
;
1829 /* We shouldn't be specializing a member template of an
1830 unspecialized class template; we already gave an error in
1831 check_specialization_scope, now avoid crashing. */
1832 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
1833 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
1835 gcc_assert (errorcount
);
1836 return error_mark_node
;
1839 fns
= TREE_OPERAND (template_id
, 0);
1840 explicit_targs
= TREE_OPERAND (template_id
, 1);
1842 if (fns
== error_mark_node
)
1843 return error_mark_node
;
1845 /* Check for baselinks. */
1846 if (BASELINK_P (fns
))
1847 fns
= BASELINK_FUNCTIONS (fns
);
1849 if (!is_overloaded_fn (fns
))
1851 error ("%qD is not a function template", fns
);
1852 return error_mark_node
;
1855 /* Count the number of template headers specified for this
1858 for (b
= current_binding_level
;
1859 b
->kind
== sk_template_parms
;
1863 for (; fns
; fns
= OVL_NEXT (fns
))
1865 tree fn
= OVL_CURRENT (fns
);
1867 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1869 tree decl_arg_types
;
1873 /* In case of explicit specialization, we need to check if
1874 the number of template headers appearing in the specialization
1875 is correct. This is usually done in check_explicit_specialization,
1876 but the check done there cannot be exhaustive when specializing
1877 member functions. Consider the following code:
1879 template <> void A<int>::f(int);
1880 template <> template <> void A<int>::f(int);
1882 Assuming that A<int> is not itself an explicit specialization
1883 already, the first line specializes "f" which is a non-template
1884 member function, whilst the second line specializes "f" which
1885 is a template member function. So both lines are syntactically
1886 correct, and check_explicit_specialization does not reject
1889 Here, we can do better, as we are matching the specialization
1890 against the declarations. We count the number of template
1891 headers, and we check if they match TEMPLATE_COUNT + 1
1892 (TEMPLATE_COUNT is the number of qualifying template classes,
1893 plus there must be another header for the member template
1896 Notice that if header_count is zero, this is not a
1897 specialization but rather a template instantiation, so there
1898 is no check we can perform here. */
1899 if (header_count
&& header_count
!= template_count
+ 1)
1902 /* Check that the number of template arguments at the
1903 innermost level for DECL is the same as for FN. */
1904 if (current_binding_level
->kind
== sk_template_parms
1905 && !current_binding_level
->explicit_spec_p
1906 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1907 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1908 (current_template_parms
))))
1911 /* DECL might be a specialization of FN. */
1912 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1913 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1915 /* For a non-static member function, we need to make sure
1916 that the const qualification is the same. Since
1917 get_bindings does not try to merge the "this" parameter,
1918 we must do the comparison explicitly. */
1919 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1920 && !same_type_p (TREE_VALUE (fn_arg_types
),
1921 TREE_VALUE (decl_arg_types
)))
1924 /* Skip the "this" parameter and, for constructors of
1925 classes with virtual bases, the VTT parameter. A
1926 full specialization of a constructor will have a VTT
1927 parameter, but a template never will. */
1929 = skip_artificial_parms_for (decl
, decl_arg_types
);
1931 = skip_artificial_parms_for (fn
, fn_arg_types
);
1933 /* Function templates cannot be specializations; there are
1934 no partial specializations of functions. Therefore, if
1935 the type of DECL does not match FN, there is no
1937 if (tsk
== tsk_template
)
1939 if (compparms (fn_arg_types
, decl_arg_types
))
1940 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1944 /* See whether this function might be a specialization of this
1945 template. Suppress access control because we might be trying
1946 to make this specialization a friend, and we have already done
1947 access control for the declaration of the specialization. */
1948 push_deferring_access_checks (dk_no_check
);
1949 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1950 pop_deferring_access_checks ();
1953 /* We cannot deduce template arguments that when used to
1954 specialize TMPL will produce DECL. */
1957 /* Make sure that the deduced arguments actually work. */
1958 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
1959 if (insttype
== error_mark_node
)
1962 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
1963 if (!compparms (fn_arg_types
, decl_arg_types
))
1966 /* Save this template, and the arguments deduced. */
1967 templates
= tree_cons (targs
, fn
, templates
);
1969 else if (need_member_template
)
1970 /* FN is an ordinary member function, and we need a
1971 specialization of a member template. */
1973 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1974 /* We can get IDENTIFIER_NODEs here in certain erroneous
1977 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1978 /* This is just an ordinary non-member function. Nothing can
1979 be a specialization of that. */
1981 else if (DECL_ARTIFICIAL (fn
))
1982 /* Cannot specialize functions that are created implicitly. */
1986 tree decl_arg_types
;
1988 /* This is an ordinary member function. However, since
1989 we're here, we can assume it's enclosing class is a
1990 template class. For example,
1992 template <typename T> struct S { void f(); };
1993 template <> void S<int>::f() {}
1995 Here, S<int>::f is a non-template, but S<int> is a
1996 template class. If FN has the same type as DECL, we
1997 might be in business. */
1999 if (!DECL_TEMPLATE_INFO (fn
))
2000 /* Its enclosing class is an explicit specialization
2001 of a template class. This is not a candidate. */
2004 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2005 TREE_TYPE (TREE_TYPE (fn
))))
2006 /* The return types differ. */
2009 /* Adjust the type of DECL in case FN is a static member. */
2010 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2011 if (DECL_STATIC_FUNCTION_P (fn
)
2012 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2013 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2015 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2018 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2022 if (templates
&& TREE_CHAIN (templates
))
2028 It is possible for a specialization with a given function
2029 signature to be instantiated from more than one function
2030 template. In such cases, explicit specification of the
2031 template arguments must be used to uniquely identify the
2032 function template specialization being specialized.
2034 Note that here, there's no suggestion that we're supposed to
2035 determine which of the candidate templates is most
2036 specialized. However, we, also have:
2040 Partial ordering of overloaded function template
2041 declarations is used in the following contexts to select
2042 the function template to which a function template
2043 specialization refers:
2045 -- when an explicit specialization refers to a function
2048 So, we do use the partial ordering rules, at least for now.
2049 This extension can only serve to make invalid programs valid,
2050 so it's safe. And, there is strong anecdotal evidence that
2051 the committee intended the partial ordering rules to apply;
2052 the EDG front end has that behavior, and John Spicer claims
2053 that the committee simply forgot to delete the wording in
2054 [temp.expl.spec]. */
2055 tree tmpl
= most_specialized_instantiation (templates
);
2056 if (tmpl
!= error_mark_node
)
2059 TREE_CHAIN (templates
) = NULL_TREE
;
2063 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2065 error ("template-id %qD for %q+D does not match any template "
2066 "declaration", template_id
, decl
);
2067 if (header_count
&& header_count
!= template_count
+ 1)
2068 inform (input_location
, "saw %d %<template<>%>, need %d for "
2069 "specializing a member function template",
2070 header_count
, template_count
+ 1);
2071 return error_mark_node
;
2073 else if ((templates
&& TREE_CHAIN (templates
))
2074 || (candidates
&& TREE_CHAIN (candidates
))
2075 || (templates
&& candidates
))
2077 error ("ambiguous template specialization %qD for %q+D",
2079 candidates
= chainon (candidates
, templates
);
2080 print_candidates (candidates
);
2081 return error_mark_node
;
2084 /* We have one, and exactly one, match. */
2087 tree fn
= TREE_VALUE (candidates
);
2088 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2089 /* DECL is a re-declaration or partial instantiation of a template
2091 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2093 /* It was a specialization of an ordinary member function in a
2095 return DECL_TI_TEMPLATE (fn
);
2098 /* It was a specialization of a template. */
2099 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2100 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2102 *targs_out
= copy_node (targs
);
2103 SET_TMPL_ARGS_LEVEL (*targs_out
,
2104 TMPL_ARGS_DEPTH (*targs_out
),
2105 TREE_PURPOSE (templates
));
2108 *targs_out
= TREE_PURPOSE (templates
);
2109 return TREE_VALUE (templates
);
2112 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2113 but with the default argument values filled in from those in the
2117 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2120 tree new_spec_types
;
2125 if (spec_types
== void_list_node
)
2126 return void_list_node
;
2128 /* Substitute into the rest of the list. */
2130 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2131 TREE_CHAIN (tmpl_types
));
2133 /* Add the default argument for this parameter. */
2134 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2135 TREE_VALUE (spec_types
),
2139 /* DECL is an explicit specialization. Replicate default arguments
2140 from the template it specializes. (That way, code like:
2142 template <class T> void f(T = 3);
2143 template <> void f(double);
2146 works, as required.) An alternative approach would be to look up
2147 the correct default arguments at the call-site, but this approach
2148 is consistent with how implicit instantiations are handled. */
2151 copy_default_args_to_explicit_spec (tree decl
)
2156 tree new_spec_types
;
2160 tree object_type
= NULL_TREE
;
2161 tree in_charge
= NULL_TREE
;
2162 tree vtt
= NULL_TREE
;
2164 /* See if there's anything we need to do. */
2165 tmpl
= DECL_TI_TEMPLATE (decl
);
2166 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2167 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2168 if (TREE_PURPOSE (t
))
2173 old_type
= TREE_TYPE (decl
);
2174 spec_types
= TYPE_ARG_TYPES (old_type
);
2176 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2178 /* Remove the this pointer, but remember the object's type for
2180 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2181 spec_types
= TREE_CHAIN (spec_types
);
2182 tmpl_types
= TREE_CHAIN (tmpl_types
);
2184 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2186 /* DECL may contain more parameters than TMPL due to the extra
2187 in-charge parameter in constructors and destructors. */
2188 in_charge
= spec_types
;
2189 spec_types
= TREE_CHAIN (spec_types
);
2191 if (DECL_HAS_VTT_PARM_P (decl
))
2194 spec_types
= TREE_CHAIN (spec_types
);
2198 /* Compute the merged default arguments. */
2200 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2202 /* Compute the new FUNCTION_TYPE. */
2206 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2211 /* Put the in-charge parameter back. */
2212 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2213 TREE_VALUE (in_charge
),
2216 new_type
= build_method_type_directly (object_type
,
2217 TREE_TYPE (old_type
),
2221 new_type
= build_function_type (TREE_TYPE (old_type
),
2223 new_type
= cp_build_type_attribute_variant (new_type
,
2224 TYPE_ATTRIBUTES (old_type
));
2225 new_type
= build_exception_variant (new_type
,
2226 TYPE_RAISES_EXCEPTIONS (old_type
));
2227 TREE_TYPE (decl
) = new_type
;
2230 /* Return the number of template headers we expect to see for a definition
2231 or specialization of CTYPE or one of its non-template members. */
2234 num_template_headers_for_class (tree ctype
)
2236 int num_templates
= 0;
2238 while (ctype
&& CLASS_TYPE_P (ctype
))
2240 /* You're supposed to have one `template <...>' for every
2241 template class, but you don't need one for a full
2242 specialization. For example:
2244 template <class T> struct S{};
2245 template <> struct S<int> { void f(); };
2246 void S<int>::f () {}
2248 is correct; there shouldn't be a `template <>' for the
2249 definition of `S<int>::f'. */
2250 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2251 /* If CTYPE does not have template information of any
2252 kind, then it is not a template, nor is it nested
2253 within a template. */
2255 if (explicit_class_specialization_p (ctype
))
2257 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2260 ctype
= TYPE_CONTEXT (ctype
);
2263 return num_templates
;
2266 /* Do a simple sanity check on the template headers that precede the
2267 variable declaration DECL. */
2270 check_template_variable (tree decl
)
2272 tree ctx
= CP_DECL_CONTEXT (decl
);
2273 int wanted
= num_template_headers_for_class (ctx
);
2274 if (!TYPE_P (ctx
) || !CLASSTYPE_TEMPLATE_INFO (ctx
))
2275 permerror (DECL_SOURCE_LOCATION (decl
),
2276 "%qD is not a static data member of a class template", decl
);
2277 else if (template_header_count
> wanted
)
2279 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2280 "too many template headers for %D (should be %d)",
2282 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2283 inform (DECL_SOURCE_LOCATION (decl
),
2284 "members of an explicitly specialized class are defined "
2285 "without a template header");
2289 /* Check to see if the function just declared, as indicated in
2290 DECLARATOR, and in DECL, is a specialization of a function
2291 template. We may also discover that the declaration is an explicit
2292 instantiation at this point.
2294 Returns DECL, or an equivalent declaration that should be used
2295 instead if all goes well. Issues an error message if something is
2296 amiss. Returns error_mark_node if the error is not easily
2299 FLAGS is a bitmask consisting of the following flags:
2301 2: The function has a definition.
2302 4: The function is a friend.
2304 The TEMPLATE_COUNT is the number of references to qualifying
2305 template classes that appeared in the name of the function. For
2308 template <class T> struct S { void f(); };
2311 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2312 classes are not counted in the TEMPLATE_COUNT, so that in
2314 template <class T> struct S {};
2315 template <> struct S<int> { void f(); }
2316 template <> void S<int>::f();
2318 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2319 invalid; there should be no template <>.)
2321 If the function is a specialization, it is marked as such via
2322 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2323 is set up correctly, and it is added to the list of specializations
2324 for that template. */
2327 check_explicit_specialization (tree declarator
,
2332 int have_def
= flags
& 2;
2333 int is_friend
= flags
& 4;
2334 int specialization
= 0;
2335 int explicit_instantiation
= 0;
2336 int member_specialization
= 0;
2337 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2338 tree dname
= DECL_NAME (decl
);
2343 if (!processing_specialization
)
2346 tsk
= tsk_excessive_parms
;
2349 tsk
= current_tmpl_spec_kind (template_count
);
2354 if (processing_specialization
)
2357 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2359 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2362 /* This could be something like:
2364 template <class T> void f(T);
2365 class S { friend void f<>(int); } */
2369 /* This case handles bogus declarations like template <>
2370 template <class T> void f<int>(); */
2372 error ("template-id %qD in declaration of primary template",
2379 case tsk_invalid_member_spec
:
2380 /* The error has already been reported in
2381 check_specialization_scope. */
2382 return error_mark_node
;
2384 case tsk_invalid_expl_inst
:
2385 error ("template parameter list used in explicit instantiation");
2391 error ("definition provided for explicit instantiation");
2393 explicit_instantiation
= 1;
2396 case tsk_excessive_parms
:
2397 case tsk_insufficient_parms
:
2398 if (tsk
== tsk_excessive_parms
)
2399 error ("too many template parameter lists in declaration of %qD",
2401 else if (template_header_count
)
2402 error("too few template parameter lists in declaration of %qD", decl
);
2404 error("explicit specialization of %qD must be introduced by "
2405 "%<template <>%>", decl
);
2409 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2411 member_specialization
= 1;
2417 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2419 /* This case handles bogus declarations like template <>
2420 template <class T> void f<int>(); */
2422 if (uses_template_parms (declarator
))
2423 error ("function template partial specialization %qD "
2424 "is not allowed", declarator
);
2426 error ("template-id %qD in declaration of primary template",
2431 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2432 /* This is a specialization of a member template, without
2433 specialization the containing class. Something like:
2435 template <class T> struct S {
2436 template <class U> void f (U);
2438 template <> template <class U> void S<int>::f(U) {}
2440 That's a specialization -- but of the entire template. */
2448 if (specialization
|| member_specialization
)
2450 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2451 for (; t
; t
= TREE_CHAIN (t
))
2452 if (TREE_PURPOSE (t
))
2454 permerror (input_location
,
2455 "default argument specified in explicit specialization");
2460 if (specialization
|| member_specialization
|| explicit_instantiation
)
2462 tree tmpl
= NULL_TREE
;
2463 tree targs
= NULL_TREE
;
2465 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2466 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2470 gcc_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
);
2475 /* If there is no class context, the explicit instantiation
2476 must be at namespace scope. */
2477 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2479 /* Find the namespace binding, using the declaration
2481 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2483 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2485 error ("%qD is not a template function", dname
);
2486 fns
= error_mark_node
;
2490 tree fn
= OVL_CURRENT (fns
);
2491 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2492 CP_DECL_CONTEXT (fn
)))
2493 error ("%qD is not declared in %qD",
2494 decl
, current_namespace
);
2498 declarator
= lookup_template_function (fns
, NULL_TREE
);
2501 if (declarator
== error_mark_node
)
2502 return error_mark_node
;
2504 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2506 if (!explicit_instantiation
)
2507 /* A specialization in class scope. This is invalid,
2508 but the error will already have been flagged by
2509 check_specialization_scope. */
2510 return error_mark_node
;
2513 /* It's not valid to write an explicit instantiation in
2516 class C { template void f(); }
2518 This case is caught by the parser. However, on
2521 template class C { void f(); };
2523 (which is invalid) we can get here. The error will be
2530 else if (ctype
!= NULL_TREE
2531 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
2534 /* Find the list of functions in ctype that have the same
2535 name as the declared function. */
2536 tree name
= TREE_OPERAND (declarator
, 0);
2537 tree fns
= NULL_TREE
;
2540 if (constructor_name_p (name
, ctype
))
2542 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2544 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2545 : !CLASSTYPE_DESTRUCTORS (ctype
))
2547 /* From [temp.expl.spec]:
2549 If such an explicit specialization for the member
2550 of a class template names an implicitly-declared
2551 special member function (clause _special_), the
2552 program is ill-formed.
2554 Similar language is found in [temp.explicit]. */
2555 error ("specialization of implicitly-declared special member function");
2556 return error_mark_node
;
2559 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2562 if (!DECL_CONV_FN_P (decl
))
2564 idx
= lookup_fnfields_1 (ctype
, name
);
2566 fns
= (*CLASSTYPE_METHOD_VEC (ctype
))[idx
];
2570 vec
<tree
, va_gc
> *methods
;
2573 /* For a type-conversion operator, we cannot do a
2574 name-based lookup. We might be looking for `operator
2575 int' which will be a specialization of `operator T'.
2576 So, we find *all* the conversion operators, and then
2577 select from them. */
2580 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2582 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2583 methods
->iterate (idx
, &ovl
);
2586 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2587 /* There are no more conversion functions. */
2590 /* Glue all these conversion functions together
2591 with those we already have. */
2592 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2593 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2597 if (fns
== NULL_TREE
)
2599 error ("no member function %qD declared in %qT", name
, ctype
);
2600 return error_mark_node
;
2603 TREE_OPERAND (declarator
, 0) = fns
;
2606 /* Figure out what exactly is being specialized at this point.
2607 Note that for an explicit instantiation, even one for a
2608 member function, we cannot tell apriori whether the
2609 instantiation is for a member template, or just a member
2610 function of a template class. Even if a member template is
2611 being instantiated, the member template arguments may be
2612 elided if they can be deduced from the rest of the
2614 tmpl
= determine_specialization (declarator
, decl
,
2616 member_specialization
,
2620 if (!tmpl
|| tmpl
== error_mark_node
)
2621 /* We couldn't figure out what this declaration was
2623 return error_mark_node
;
2626 tree gen_tmpl
= most_general_template (tmpl
);
2628 if (explicit_instantiation
)
2630 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2631 is done by do_decl_instantiation later. */
2633 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2634 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2636 if (arg_depth
> parm_depth
)
2638 /* If TMPL is not the most general template (for
2639 example, if TMPL is a friend template that is
2640 injected into namespace scope), then there will
2641 be too many levels of TARGS. Remove some of them
2646 new_targs
= make_tree_vec (parm_depth
);
2647 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2648 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2649 = TREE_VEC_ELT (targs
, i
);
2653 return instantiate_template (tmpl
, targs
, tf_error
);
2656 /* If we thought that the DECL was a member function, but it
2657 turns out to be specializing a static member function,
2658 make DECL a static member function as well. */
2659 if (DECL_STATIC_FUNCTION_P (tmpl
)
2660 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2661 revert_static_member_fn (decl
);
2663 /* If this is a specialization of a member template of a
2664 template class, we want to return the TEMPLATE_DECL, not
2665 the specialization of it. */
2666 if (tsk
== tsk_template
)
2668 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2669 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2670 DECL_INITIAL (result
) = NULL_TREE
;
2674 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2675 DECL_SOURCE_LOCATION (result
)
2676 = DECL_SOURCE_LOCATION (decl
);
2677 /* We want to use the argument list specified in the
2678 definition, not in the original declaration. */
2679 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2680 for (parm
= DECL_ARGUMENTS (result
); parm
;
2681 parm
= DECL_CHAIN (parm
))
2682 DECL_CONTEXT (parm
) = result
;
2684 return register_specialization (tmpl
, gen_tmpl
, targs
,
2688 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2689 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2691 /* Inherit default function arguments from the template
2692 DECL is specializing. */
2693 copy_default_args_to_explicit_spec (decl
);
2695 /* This specialization has the same protection as the
2696 template it specializes. */
2697 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2698 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2700 /* 7.1.1-1 [dcl.stc]
2702 A storage-class-specifier shall not be specified in an
2703 explicit specialization...
2705 The parser rejects these, so unless action is taken here,
2706 explicit function specializations will always appear with
2709 The action recommended by the C++ CWG in response to C++
2710 defect report 605 is to make the storage class and linkage
2711 of the explicit specialization match the templated function:
2713 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2715 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2717 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2718 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2720 /* This specialization has the same linkage and visibility as
2721 the function template it specializes. */
2722 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2723 if (! TREE_PUBLIC (decl
))
2725 DECL_INTERFACE_KNOWN (decl
) = 1;
2726 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2728 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2729 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2731 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2732 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2736 /* If DECL is a friend declaration, declared using an
2737 unqualified name, the namespace associated with DECL may
2738 have been set incorrectly. For example, in:
2740 template <typename T> void f(T);
2742 struct S { friend void f<int>(int); }
2745 we will have set the DECL_CONTEXT for the friend
2746 declaration to N, rather than to the global namespace. */
2747 if (DECL_NAMESPACE_SCOPE_P (decl
))
2748 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2750 if (is_friend
&& !have_def
)
2751 /* This is not really a declaration of a specialization.
2752 It's just the name of an instantiation. But, it's not
2753 a request for an instantiation, either. */
2754 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2755 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2756 /* This is indeed a specialization. In case of constructors
2757 and destructors, we need in-charge and not-in-charge
2758 versions in V3 ABI. */
2759 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2761 /* Register this specialization so that we can find it
2763 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2770 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2771 parameters. These are represented in the same format used for
2772 DECL_TEMPLATE_PARMS. */
2775 comp_template_parms (const_tree parms1
, const_tree parms2
)
2780 if (parms1
== parms2
)
2783 for (p1
= parms1
, p2
= parms2
;
2784 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2785 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2787 tree t1
= TREE_VALUE (p1
);
2788 tree t2
= TREE_VALUE (p2
);
2791 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2792 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2794 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2797 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2799 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2800 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2802 /* If either of the template parameters are invalid, assume
2803 they match for the sake of error recovery. */
2804 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2807 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2810 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2811 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2812 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2814 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2819 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2820 /* One set of parameters has more parameters lists than the
2827 /* Determine whether PARM is a parameter pack. */
2830 template_parameter_pack_p (const_tree parm
)
2832 /* Determine if we have a non-type template parameter pack. */
2833 if (TREE_CODE (parm
) == PARM_DECL
)
2834 return (DECL_TEMPLATE_PARM_P (parm
)
2835 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2836 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
2837 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
2839 /* If this is a list of template parameters, we could get a
2840 TYPE_DECL or a TEMPLATE_DECL. */
2841 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2842 parm
= TREE_TYPE (parm
);
2844 /* Otherwise it must be a type template parameter. */
2845 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2846 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2847 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2850 /* Determine if T is a function parameter pack. */
2853 function_parameter_pack_p (const_tree t
)
2855 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2856 return FUNCTION_PARAMETER_PACK_P (t
);
2860 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2861 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2864 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2866 if (! primary_func_tmpl_inst
2867 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2868 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2871 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2874 /* Return true iff the function parameter PARAM_DECL was expanded
2875 from the function parameter pack PACK. */
2878 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
2880 if (DECL_ARTIFICIAL (param_decl
)
2881 || !function_parameter_pack_p (pack
))
2884 /* The parameter pack and its pack arguments have the same
2886 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
2889 /* Determine whether ARGS describes a variadic template args list,
2890 i.e., one that is terminated by a template argument pack. */
2893 template_args_variadic_p (tree args
)
2898 if (args
== NULL_TREE
)
2901 args
= INNERMOST_TEMPLATE_ARGS (args
);
2902 nargs
= TREE_VEC_LENGTH (args
);
2907 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2909 return ARGUMENT_PACK_P (last_parm
);
2912 /* Generate a new name for the parameter pack name NAME (an
2913 IDENTIFIER_NODE) that incorporates its */
2916 make_ith_pack_parameter_name (tree name
, int i
)
2918 /* Munge the name to include the parameter index. */
2919 #define NUMBUF_LEN 128
2920 char numbuf
[NUMBUF_LEN
];
2924 if (name
== NULL_TREE
)
2926 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
2927 newname_len
= IDENTIFIER_LENGTH (name
)
2928 + strlen (numbuf
) + 2;
2929 newname
= (char*)alloca (newname_len
);
2930 snprintf (newname
, newname_len
,
2931 "%s#%i", IDENTIFIER_POINTER (name
), i
);
2932 return get_identifier (newname
);
2935 /* Return true if T is a primary function, class or alias template
2939 primary_template_instantiation_p (const_tree t
)
2944 if (TREE_CODE (t
) == FUNCTION_DECL
)
2945 return DECL_LANG_SPECIFIC (t
)
2946 && DECL_TEMPLATE_INSTANTIATION (t
)
2947 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
2948 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
2949 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
2950 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
2951 else if (alias_template_specialization_p (t
))
2956 /* Return true if PARM is a template template parameter. */
2959 template_template_parameter_p (const_tree parm
)
2961 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
2964 /* Return true iff PARM is a DECL representing a type template
2968 template_type_parameter_p (const_tree parm
)
2971 && (TREE_CODE (parm
) == TYPE_DECL
2972 || TREE_CODE (parm
) == TEMPLATE_DECL
)
2973 && DECL_TEMPLATE_PARM_P (parm
));
2976 /* Return the template parameters of T if T is a
2977 primary template instantiation, NULL otherwise. */
2980 get_primary_template_innermost_parameters (const_tree t
)
2982 tree parms
= NULL
, template_info
= NULL
;
2984 if ((template_info
= get_template_info (t
))
2985 && primary_template_instantiation_p (t
))
2986 parms
= INNERMOST_TEMPLATE_PARMS
2987 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
2992 /* Return the template parameters of the LEVELth level from the full list
2993 of template parameters PARMS. */
2996 get_template_parms_at_level (tree parms
, int level
)
3000 || TREE_CODE (parms
) != TREE_LIST
3001 || level
> TMPL_PARMS_DEPTH (parms
))
3004 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3005 if (TMPL_PARMS_DEPTH (p
) == level
)
3011 /* Returns the template arguments of T if T is a template instantiation,
3015 get_template_innermost_arguments (const_tree t
)
3017 tree args
= NULL
, template_info
= NULL
;
3019 if ((template_info
= get_template_info (t
))
3020 && TI_ARGS (template_info
))
3021 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3026 /* Return the argument pack elements of T if T is a template argument pack,
3030 get_template_argument_pack_elems (const_tree t
)
3032 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3033 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3036 return ARGUMENT_PACK_ARGS (t
);
3039 /* Structure used to track the progress of find_parameter_packs_r. */
3040 struct find_parameter_pack_data
3042 /* TREE_LIST that will contain all of the parameter packs found by
3044 tree
* parameter_packs
;
3046 /* Set of AST nodes that have been visited by the traversal. */
3047 struct pointer_set_t
*visited
;
3050 /* Identifies all of the argument packs that occur in a template
3051 argument and appends them to the TREE_LIST inside DATA, which is a
3052 find_parameter_pack_data structure. This is a subroutine of
3053 make_pack_expansion and uses_parameter_packs. */
3055 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3058 struct find_parameter_pack_data
* ppd
=
3059 (struct find_parameter_pack_data
*)data
;
3060 bool parameter_pack_p
= false;
3062 /* Handle type aliases/typedefs. */
3065 && TREE_CODE (TYPE_NAME (t
)) == TYPE_DECL
3066 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3068 if (TYPE_TEMPLATE_INFO (t
))
3069 cp_walk_tree (&TYPE_TI_ARGS (t
),
3070 &find_parameter_packs_r
,
3076 /* Identify whether this is a parameter pack or not. */
3077 switch (TREE_CODE (t
))
3079 case TEMPLATE_PARM_INDEX
:
3080 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3081 parameter_pack_p
= true;
3084 case TEMPLATE_TYPE_PARM
:
3085 t
= TYPE_MAIN_VARIANT (t
);
3086 case TEMPLATE_TEMPLATE_PARM
:
3087 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3088 parameter_pack_p
= true;
3092 if (FUNCTION_PARAMETER_PACK_P (t
))
3094 /* We don't want to walk into the type of a PARM_DECL,
3095 because we don't want to see the type parameter pack. */
3097 parameter_pack_p
= true;
3102 parameter_pack_p
= true;
3105 /* Not a parameter pack. */
3109 if (parameter_pack_p
)
3111 /* Add this parameter pack to the list. */
3112 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3116 cp_walk_tree (&TYPE_CONTEXT (t
),
3117 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3119 /* This switch statement will return immediately if we don't find a
3121 switch (TREE_CODE (t
))
3123 case TEMPLATE_PARM_INDEX
:
3126 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3127 /* Check the template itself. */
3128 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3129 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3130 /* Check the template arguments. */
3131 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3136 case TEMPLATE_TYPE_PARM
:
3137 case TEMPLATE_TEMPLATE_PARM
:
3144 if (TYPE_PTRMEMFUNC_P (t
))
3150 if (TYPE_TEMPLATE_INFO (t
))
3151 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
3152 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3159 cp_walk_tree (&TREE_TYPE (t
),
3160 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3164 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3169 case TYPE_PACK_EXPANSION
:
3170 case EXPR_PACK_EXPANSION
:
3175 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3180 case IDENTIFIER_NODE
:
3181 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3193 /* Determines if the expression or type T uses any parameter packs. */
3195 uses_parameter_packs (tree t
)
3197 tree parameter_packs
= NULL_TREE
;
3198 struct find_parameter_pack_data ppd
;
3199 ppd
.parameter_packs
= ¶meter_packs
;
3200 ppd
.visited
= pointer_set_create ();
3201 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3202 pointer_set_destroy (ppd
.visited
);
3203 return parameter_packs
!= NULL_TREE
;
3206 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3207 representation a base-class initializer into a parameter pack
3208 expansion. If all goes well, the resulting node will be an
3209 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3212 make_pack_expansion (tree arg
)
3215 tree parameter_packs
= NULL_TREE
;
3216 bool for_types
= false;
3217 struct find_parameter_pack_data ppd
;
3219 if (!arg
|| arg
== error_mark_node
)
3222 if (TREE_CODE (arg
) == TREE_LIST
)
3224 /* The only time we will see a TREE_LIST here is for a base
3225 class initializer. In this case, the TREE_PURPOSE will be a
3226 _TYPE node (representing the base class expansion we're
3227 initializing) and the TREE_VALUE will be a TREE_LIST
3228 containing the initialization arguments.
3230 The resulting expansion looks somewhat different from most
3231 expansions. Rather than returning just one _EXPANSION, we
3232 return a TREE_LIST whose TREE_PURPOSE is a
3233 TYPE_PACK_EXPANSION containing the bases that will be
3234 initialized. The TREE_VALUE will be identical to the
3235 original TREE_VALUE, which is a list of arguments that will
3236 be passed to each base. We do not introduce any new pack
3237 expansion nodes into the TREE_VALUE (although it is possible
3238 that some already exist), because the TREE_PURPOSE and
3239 TREE_VALUE all need to be expanded together with the same
3240 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3241 resulting TREE_PURPOSE will mention the parameter packs in
3242 both the bases and the arguments to the bases. */
3245 tree parameter_packs
= NULL_TREE
;
3247 /* Determine which parameter packs will be used by the base
3249 ppd
.visited
= pointer_set_create ();
3250 ppd
.parameter_packs
= ¶meter_packs
;
3251 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3254 if (parameter_packs
== NULL_TREE
)
3256 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3257 pointer_set_destroy (ppd
.visited
);
3258 return error_mark_node
;
3261 if (TREE_VALUE (arg
) != void_type_node
)
3263 /* Collect the sets of parameter packs used in each of the
3264 initialization arguments. */
3265 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3267 /* Determine which parameter packs will be expanded in this
3269 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3274 pointer_set_destroy (ppd
.visited
);
3276 /* Create the pack expansion type for the base type. */
3277 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3278 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3279 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3281 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3282 they will rarely be compared to anything. */
3283 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3285 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3288 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3291 /* Build the PACK_EXPANSION_* node. */
3293 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3294 : make_node (EXPR_PACK_EXPANSION
);
3295 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3296 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3298 /* Propagate type and const-expression information. */
3299 TREE_TYPE (result
) = TREE_TYPE (arg
);
3300 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3303 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3304 they will rarely be compared to anything. */
3305 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3307 /* Determine which parameter packs will be expanded. */
3308 ppd
.parameter_packs
= ¶meter_packs
;
3309 ppd
.visited
= pointer_set_create ();
3310 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3311 pointer_set_destroy (ppd
.visited
);
3313 /* Make sure we found some parameter packs. */
3314 if (parameter_packs
== NULL_TREE
)
3317 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3319 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3320 return error_mark_node
;
3322 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3324 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3329 /* Checks T for any "bare" parameter packs, which have not yet been
3330 expanded, and issues an error if any are found. This operation can
3331 only be done on full expressions or types (e.g., an expression
3332 statement, "if" condition, etc.), because we could have expressions like:
3334 foo(f(g(h(args)))...)
3336 where "args" is a parameter pack. check_for_bare_parameter_packs
3337 should not be called for the subexpressions args, h(args),
3338 g(h(args)), or f(g(h(args))), because we would produce erroneous
3341 Returns TRUE and emits an error if there were bare parameter packs,
3342 returns FALSE otherwise. */
3344 check_for_bare_parameter_packs (tree t
)
3346 tree parameter_packs
= NULL_TREE
;
3347 struct find_parameter_pack_data ppd
;
3349 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3352 if (TREE_CODE (t
) == TYPE_DECL
)
3355 ppd
.parameter_packs
= ¶meter_packs
;
3356 ppd
.visited
= pointer_set_create ();
3357 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3358 pointer_set_destroy (ppd
.visited
);
3360 if (parameter_packs
)
3362 error ("parameter packs not expanded with %<...%>:");
3363 while (parameter_packs
)
3365 tree pack
= TREE_VALUE (parameter_packs
);
3366 tree name
= NULL_TREE
;
3368 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3369 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3370 name
= TYPE_NAME (pack
);
3371 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3372 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3374 name
= DECL_NAME (pack
);
3377 inform (input_location
, " %qD", name
);
3379 inform (input_location
, " <anonymous>");
3381 parameter_packs
= TREE_CHAIN (parameter_packs
);
3390 /* Expand any parameter packs that occur in the template arguments in
3393 expand_template_argument_pack (tree args
)
3395 tree result_args
= NULL_TREE
;
3396 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3397 int num_result_args
= -1;
3398 int non_default_args_count
= -1;
3400 /* First, determine if we need to expand anything, and the number of
3401 slots we'll need. */
3402 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3404 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3405 if (arg
== NULL_TREE
)
3407 if (ARGUMENT_PACK_P (arg
))
3409 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3410 if (num_result_args
< 0)
3411 num_result_args
= in_arg
+ num_packed
;
3413 num_result_args
+= num_packed
;
3417 if (num_result_args
>= 0)
3422 /* If no expansion is necessary, we're done. */
3423 if (num_result_args
< 0)
3426 /* Expand arguments. */
3427 result_args
= make_tree_vec (num_result_args
);
3428 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3429 non_default_args_count
=
3430 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3431 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3433 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3434 if (ARGUMENT_PACK_P (arg
))
3436 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3437 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3438 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3439 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3440 if (non_default_args_count
> 0)
3441 non_default_args_count
+= num_packed
;
3445 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3449 if (non_default_args_count
>= 0)
3450 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3454 /* Checks if DECL shadows a template parameter.
3456 [temp.local]: A template-parameter shall not be redeclared within its
3457 scope (including nested scopes).
3459 Emits an error and returns TRUE if the DECL shadows a parameter,
3460 returns FALSE otherwise. */
3463 check_template_shadow (tree decl
)
3467 /* If we're not in a template, we can't possibly shadow a template
3469 if (!current_template_parms
)
3472 /* Figure out what we're shadowing. */
3473 if (TREE_CODE (decl
) == OVERLOAD
)
3474 decl
= OVL_CURRENT (decl
);
3475 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3477 /* If there's no previous binding for this name, we're not shadowing
3478 anything, let alone a template parameter. */
3482 /* If we're not shadowing a template parameter, we're done. Note
3483 that OLDDECL might be an OVERLOAD (or perhaps even an
3484 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3486 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3489 /* We check for decl != olddecl to avoid bogus errors for using a
3490 name inside a class. We check TPFI to avoid duplicate errors for
3491 inline member templates. */
3493 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
3496 error ("declaration of %q+#D", decl
);
3497 error (" shadows template parm %q+#D", olddecl
);
3501 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3502 ORIG_LEVEL, DECL, and TYPE. */
3505 build_template_parm_index (int index
,
3511 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3512 TEMPLATE_PARM_IDX (t
) = index
;
3513 TEMPLATE_PARM_LEVEL (t
) = level
;
3514 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3515 TEMPLATE_PARM_DECL (t
) = decl
;
3516 TREE_TYPE (t
) = type
;
3517 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3518 TREE_READONLY (t
) = TREE_READONLY (decl
);
3523 /* Find the canonical type parameter for the given template type
3524 parameter. Returns the canonical type parameter, which may be TYPE
3525 if no such parameter existed. */
3528 canonical_type_parameter (tree type
)
3531 int idx
= TEMPLATE_TYPE_IDX (type
);
3532 if (!canonical_template_parms
)
3533 vec_alloc (canonical_template_parms
, idx
+1);
3535 while (canonical_template_parms
->length () <= (unsigned)idx
)
3536 vec_safe_push (canonical_template_parms
, NULL_TREE
);
3538 list
= (*canonical_template_parms
)[idx
];
3539 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3540 list
= TREE_CHAIN (list
);
3543 return TREE_VALUE (list
);
3546 (*canonical_template_parms
)[idx
]
3547 = tree_cons (NULL_TREE
, type
,
3548 (*canonical_template_parms
)[idx
]);
3553 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3554 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3555 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3556 new one is created. */
3559 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3560 tsubst_flags_t complain
)
3562 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3563 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3564 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3565 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3567 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3570 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3571 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3572 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3573 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3574 DECL_ARTIFICIAL (decl
) = 1;
3575 SET_DECL_TEMPLATE_PARM_P (decl
);
3577 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3578 TEMPLATE_PARM_LEVEL (index
) - levels
,
3579 TEMPLATE_PARM_ORIG_LEVEL (index
),
3581 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3582 TEMPLATE_PARM_PARAMETER_PACK (t
)
3583 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3585 /* Template template parameters need this. */
3586 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3587 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3588 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3592 return TEMPLATE_PARM_DESCENDANTS (index
);
3595 /* Process information from new template parameter PARM and append it
3596 to the LIST being built. This new parameter is a non-type
3597 parameter iff IS_NON_TYPE is true. This new parameter is a
3598 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3599 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3600 parameter list PARM belongs to. This is used used to create a
3601 proper canonical type for the type of PARM that is to be created,
3602 iff PARM is a type. If the size is not known, this parameter shall
3606 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
3607 bool is_non_type
, bool is_parameter_pack
)
3614 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3615 defval
= TREE_PURPOSE (parm
);
3619 tree p
= tree_last (list
);
3621 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3624 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3625 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3627 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3637 parm
= TREE_VALUE (parm
);
3639 SET_DECL_TEMPLATE_PARM_P (parm
);
3641 if (TREE_TYPE (parm
) == error_mark_node
)
3643 err_parm_list
= build_tree_list (defval
, parm
);
3644 TREE_VALUE (err_parm_list
) = error_mark_node
;
3645 return chainon (list
, err_parm_list
);
3651 The top-level cv-qualifiers on the template-parameter are
3652 ignored when determining its type. */
3653 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3654 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3656 err_parm_list
= build_tree_list (defval
, parm
);
3657 TREE_VALUE (err_parm_list
) = error_mark_node
;
3658 return chainon (list
, err_parm_list
);
3661 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
)
3663 /* This template parameter is not a parameter pack, but it
3664 should be. Complain about "bare" parameter packs. */
3665 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3667 /* Recover by calling this a parameter pack. */
3668 is_parameter_pack
= true;
3672 /* A template parameter is not modifiable. */
3673 TREE_CONSTANT (parm
) = 1;
3674 TREE_READONLY (parm
) = 1;
3675 decl
= build_decl (parm_loc
,
3676 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3677 TREE_CONSTANT (decl
) = 1;
3678 TREE_READONLY (decl
) = 1;
3679 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3680 = build_template_parm_index (idx
, processing_template_decl
,
3681 processing_template_decl
,
3682 decl
, TREE_TYPE (parm
));
3684 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3685 = is_parameter_pack
;
3690 parm
= TREE_VALUE (TREE_VALUE (parm
));
3692 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3694 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3695 /* This is for distinguishing between real templates and template
3696 template parameters */
3697 TREE_TYPE (parm
) = t
;
3698 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3703 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3704 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3705 decl
= build_decl (parm_loc
,
3706 TYPE_DECL
, parm
, t
);
3709 TYPE_NAME (t
) = decl
;
3710 TYPE_STUB_DECL (t
) = decl
;
3712 TEMPLATE_TYPE_PARM_INDEX (t
)
3713 = build_template_parm_index (idx
, processing_template_decl
,
3714 processing_template_decl
,
3715 decl
, TREE_TYPE (parm
));
3716 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3717 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3719 DECL_ARTIFICIAL (decl
) = 1;
3720 SET_DECL_TEMPLATE_PARM_P (decl
);
3722 parm
= build_tree_list (defval
, parm
);
3723 return chainon (list
, parm
);
3726 /* The end of a template parameter list has been reached. Process the
3727 tree list into a parameter vector, converting each parameter into a more
3728 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3732 end_template_parm_list (tree parms
)
3736 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3738 current_template_parms
3739 = tree_cons (size_int (processing_template_decl
),
3740 saved_parmlist
, current_template_parms
);
3742 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3744 next
= TREE_CHAIN (parm
);
3745 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3746 TREE_CHAIN (parm
) = NULL_TREE
;
3749 --processing_template_parmlist
;
3751 return saved_parmlist
;
3754 /* end_template_decl is called after a template declaration is seen. */
3757 end_template_decl (void)
3759 reset_specialization ();
3761 if (! processing_template_decl
)
3764 /* This matches the pushlevel in begin_template_parm_list. */
3767 --processing_template_decl
;
3768 current_template_parms
= TREE_CHAIN (current_template_parms
);
3771 /* Takes a TREE_LIST representing a template parameter and convert it
3772 into an argument suitable to be passed to the type substitution
3773 functions. Note that If the TREE_LIST contains an error_mark
3774 node, the returned argument is error_mark_node. */
3777 template_parm_to_arg (tree t
)
3781 || TREE_CODE (t
) != TREE_LIST
)
3784 if (error_operand_p (TREE_VALUE (t
)))
3785 return error_mark_node
;
3789 if (TREE_CODE (t
) == TYPE_DECL
3790 || TREE_CODE (t
) == TEMPLATE_DECL
)
3794 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3796 /* Turn this argument into a TYPE_ARGUMENT_PACK
3797 with a single element, which expands T. */
3798 tree vec
= make_tree_vec (1);
3799 #ifdef ENABLE_CHECKING
3800 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3801 (vec
, TREE_VEC_LENGTH (vec
));
3803 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3805 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
3806 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3811 t
= DECL_INITIAL (t
);
3813 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3815 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3816 with a single element, which expands T. */
3817 tree vec
= make_tree_vec (1);
3818 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3819 #ifdef ENABLE_CHECKING
3820 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3821 (vec
, TREE_VEC_LENGTH (vec
));
3823 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3825 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3826 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3827 TREE_TYPE (t
) = type
;
3833 /* Given a set of template parameters, return them as a set of template
3834 arguments. The template parameters are represented as a TREE_VEC, in
3835 the form documented in cp-tree.h for template arguments. */
3838 template_parms_to_args (tree parms
)
3841 tree args
= NULL_TREE
;
3842 int length
= TMPL_PARMS_DEPTH (parms
);
3845 /* If there is only one level of template parameters, we do not
3846 create a TREE_VEC of TREE_VECs. Instead, we return a single
3847 TREE_VEC containing the arguments. */
3849 args
= make_tree_vec (length
);
3851 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
3853 tree a
= copy_node (TREE_VALUE (header
));
3856 TREE_TYPE (a
) = NULL_TREE
;
3857 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
3858 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
3860 #ifdef ENABLE_CHECKING
3861 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
3865 TREE_VEC_ELT (args
, --l
) = a
;
3870 if (length
> 1 && TREE_VEC_ELT (args
, 0) == NULL_TREE
)
3871 /* This can happen for template parms of a template template
3874 template<template<class T, class U> class TT> struct S;
3876 Consider the level of the parms of TT; T and U both have
3877 level 2; TT has no template parm of level 1. So in this case
3878 the first element of full_template_args is NULL_TREE. If we
3879 leave it like this TMPL_ARG_DEPTH on args returns 1 instead
3880 of 2. This will make tsubst wrongly consider that T and U
3881 have level 1. Instead, let's create a dummy vector as the
3882 first element of full_template_args so that TMPL_ARG_DEPTH
3883 returns the correct depth for args. */
3884 TREE_VEC_ELT (args
, 0) = make_tree_vec (1);
3888 /* Within the declaration of a template, return the currently active
3889 template parameters as an argument TREE_VEC. */
3892 current_template_args (void)
3894 return template_parms_to_args (current_template_parms
);
3897 /* Update the declared TYPE by doing any lookups which were thought to be
3898 dependent, but are not now that we know the SCOPE of the declarator. */
3901 maybe_update_decl_type (tree orig_type
, tree scope
)
3903 tree type
= orig_type
;
3905 if (type
== NULL_TREE
)
3908 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3909 type
= TREE_TYPE (type
);
3911 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
3912 && dependent_type_p (type
)
3913 /* Don't bother building up the args in this case. */
3914 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
3916 /* tsubst in the args corresponding to the template parameters,
3917 including auto if present. Most things will be unchanged, but
3918 make_typename_type and tsubst_qualified_id will resolve
3919 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3920 tree args
= current_template_args ();
3921 tree auto_node
= type_uses_auto (type
);
3925 tree auto_vec
= make_tree_vec (1);
3926 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
3927 args
= add_to_template_args (args
, auto_vec
);
3929 pushed
= push_scope (scope
);
3930 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
3935 if (type
== error_mark_node
)
3938 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3940 if (same_type_p (type
, TREE_TYPE (orig_type
)))
3943 type
= TYPE_NAME (type
);
3948 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3949 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3950 a member template. Used by push_template_decl below. */
3953 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
3955 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
3956 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
3957 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
3958 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3959 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
3964 struct template_parm_data
3966 /* The level of the template parameters we are currently
3970 /* The index of the specialization argument we are currently
3974 /* An array whose size is the number of template parameters. The
3975 elements are nonzero if the parameter has been used in any one
3976 of the arguments processed so far. */
3979 /* An array whose size is the number of template arguments. The
3980 elements are nonzero if the argument makes use of template
3981 parameters of this level. */
3982 int* arg_uses_template_parms
;
3985 /* Subroutine of push_template_decl used to see if each template
3986 parameter in a partial specialization is used in the explicit
3987 argument list. If T is of the LEVEL given in DATA (which is
3988 treated as a template_parm_data*), then DATA->PARMS is marked
3992 mark_template_parm (tree t
, void* data
)
3996 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
3998 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4000 level
= TEMPLATE_PARM_LEVEL (t
);
4001 idx
= TEMPLATE_PARM_IDX (t
);
4005 level
= TEMPLATE_TYPE_LEVEL (t
);
4006 idx
= TEMPLATE_TYPE_IDX (t
);
4009 if (level
== tpd
->level
)
4011 tpd
->parms
[idx
] = 1;
4012 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4015 /* Return zero so that for_each_template_parm will continue the
4016 traversal of the tree; we want to mark *every* template parm. */
4020 /* Process the partial specialization DECL. */
4023 process_partial_specialization (tree decl
)
4025 tree type
= TREE_TYPE (decl
);
4026 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
4027 tree specargs
= CLASSTYPE_TI_ARGS (type
);
4028 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4029 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4032 int nargs
= TREE_VEC_LENGTH (inner_args
);
4035 bool did_error_intro
= false;
4036 struct template_parm_data tpd
;
4037 struct template_parm_data tpd2
;
4039 gcc_assert (current_template_parms
);
4041 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4042 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4044 /* We check that each of the template parameters given in the
4045 partial specialization is used in the argument list to the
4046 specialization. For example:
4048 template <class T> struct S;
4049 template <class T> struct S<T*>;
4051 The second declaration is OK because `T*' uses the template
4052 parameter T, whereas
4054 template <class T> struct S<int>;
4056 is no good. Even trickier is:
4067 The S2<T> declaration is actually invalid; it is a
4068 full-specialization. Of course,
4071 struct S2<T (*)(U)>;
4073 or some such would have been OK. */
4074 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4075 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4076 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4078 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4079 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4080 for (i
= 0; i
< nargs
; ++i
)
4082 tpd
.current_arg
= i
;
4083 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4084 &mark_template_parm
,
4087 /*include_nondeduced_p=*/false);
4089 for (i
= 0; i
< ntparms
; ++i
)
4090 if (tpd
.parms
[i
] == 0)
4092 /* One of the template parms was not used in the
4094 if (!did_error_intro
)
4096 error ("template parameters not used in partial specialization:");
4097 did_error_intro
= true;
4100 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4103 if (did_error_intro
)
4104 return error_mark_node
;
4106 /* [temp.class.spec]
4108 The argument list of the specialization shall not be identical to
4109 the implicit argument list of the primary template. */
4110 if (comp_template_args
4112 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4114 error ("partial specialization %qT does not specialize any template arguments", type
);
4116 /* A partial specialization that replaces multiple parameters of the
4117 primary template with a pack expansion is less specialized for those
4119 if (nargs
< DECL_NTPARMS (maintmpl
))
4121 error ("partial specialization is not more specialized than the "
4122 "primary template because it replaces multiple parameters "
4123 "with a pack expansion");
4124 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4128 /* [temp.class.spec]
4130 A partially specialized non-type argument expression shall not
4131 involve template parameters of the partial specialization except
4132 when the argument expression is a simple identifier.
4134 The type of a template parameter corresponding to a specialized
4135 non-type argument shall not be dependent on a parameter of the
4138 Also, we verify that pack expansions only occur at the
4139 end of the argument list. */
4140 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4142 for (i
= 0; i
< nargs
; ++i
)
4144 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4145 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4146 tree packed_args
= NULL_TREE
;
4149 if (ARGUMENT_PACK_P (arg
))
4151 /* Extract the arguments from the argument pack. We'll be
4152 iterating over these in the following loop. */
4153 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4154 len
= TREE_VEC_LENGTH (packed_args
);
4157 for (j
= 0; j
< len
; j
++)
4160 /* Get the Jth argument in the parameter pack. */
4161 arg
= TREE_VEC_ELT (packed_args
, j
);
4163 if (PACK_EXPANSION_P (arg
))
4165 /* Pack expansions must come at the end of the
4167 if ((packed_args
&& j
< len
- 1)
4168 || (!packed_args
&& i
< nargs
- 1))
4170 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4171 error ("parameter pack argument %qE must be at the "
4172 "end of the template argument list", arg
);
4174 error ("parameter pack argument %qT must be at the "
4175 "end of the template argument list", arg
);
4179 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4180 /* We only care about the pattern. */
4181 arg
= PACK_EXPANSION_PATTERN (arg
);
4183 if (/* These first two lines are the `non-type' bit. */
4185 && TREE_CODE (arg
) != TEMPLATE_DECL
4186 /* This next line is the `argument expression is not just a
4187 simple identifier' condition and also the `specialized
4188 non-type argument' bit. */
4189 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
4191 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4192 || (packed_args
&& uses_template_parms (arg
)))
4193 error ("template argument %qE involves template parameter(s)",
4197 /* Look at the corresponding template parameter,
4198 marking which template parameters its type depends
4200 tree type
= TREE_TYPE (parm
);
4204 /* We haven't yet initialized TPD2. Do so now. */
4205 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4206 /* The number of parameters here is the number in the
4207 main template, which, as checked in the assertion
4209 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4211 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4214 /* Mark the template parameters. But this time, we're
4215 looking for the template parameters of the main
4216 template, not in the specialization. */
4217 tpd2
.current_arg
= i
;
4218 tpd2
.arg_uses_template_parms
[i
] = 0;
4219 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4220 for_each_template_parm (type
,
4221 &mark_template_parm
,
4224 /*include_nondeduced_p=*/false);
4226 if (tpd2
.arg_uses_template_parms
[i
])
4228 /* The type depended on some template parameters.
4229 If they are fully specialized in the
4230 specialization, that's OK. */
4233 for (j
= 0; j
< nargs
; ++j
)
4234 if (tpd2
.parms
[j
] != 0
4235 && tpd
.arg_uses_template_parms
[j
])
4238 error_n (input_location
, count
,
4239 "type %qT of template argument %qE depends "
4240 "on a template parameter",
4241 "type %qT of template argument %qE depends "
4242 "on template parameters",
4251 /* We should only get here once. */
4252 gcc_assert (!COMPLETE_TYPE_P (type
));
4254 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4255 = tree_cons (specargs
, inner_parms
,
4256 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4257 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4259 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4260 inst
= TREE_CHAIN (inst
))
4262 tree inst_type
= TREE_VALUE (inst
);
4263 if (COMPLETE_TYPE_P (inst_type
)
4264 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type
))
4266 tree spec
= most_specialized_class (inst_type
, maintmpl
, tf_none
);
4267 if (spec
&& TREE_TYPE (spec
) == type
)
4268 permerror (input_location
,
4269 "partial specialization of %qT after instantiation "
4270 "of %qT", type
, inst_type
);
4277 /* Check that a template declaration's use of default arguments and
4278 parameter packs is not invalid. Here, PARMS are the template
4279 parameters. IS_PRIMARY is true if DECL is the thing declared by
4280 a primary template. IS_PARTIAL is true if DECL is a partial
4283 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4284 declaration (but not a definition); 1 indicates a declaration, 2
4285 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4286 emitted for extraneous default arguments.
4288 Returns TRUE if there were no errors found, FALSE otherwise. */
4291 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4292 bool is_partial
, int is_friend_decl
)
4295 int last_level_to_check
;
4297 bool no_errors
= true;
4301 A default template-argument shall not be specified in a
4302 function template declaration or a function template definition, nor
4303 in the template-parameter-list of the definition of a member of a
4306 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
4307 /* You can't have a function template declaration in a local
4308 scope, nor you can you define a member of a class template in a
4312 if (TREE_CODE (decl
) == TYPE_DECL
4314 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4315 /* A lambda doesn't have an explicit declaration; don't complain
4316 about the parms of the enclosing class. */
4319 if (current_class_type
4320 && !TYPE_BEING_DEFINED (current_class_type
)
4321 && DECL_LANG_SPECIFIC (decl
)
4322 && DECL_DECLARES_FUNCTION_P (decl
)
4323 /* If this is either a friend defined in the scope of the class
4324 or a member function. */
4325 && (DECL_FUNCTION_MEMBER_P (decl
)
4326 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4327 : DECL_FRIEND_CONTEXT (decl
)
4328 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4330 /* And, if it was a member function, it really was defined in
4331 the scope of the class. */
4332 && (!DECL_FUNCTION_MEMBER_P (decl
)
4333 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4334 /* We already checked these parameters when the template was
4335 declared, so there's no need to do it again now. This function
4336 was defined in class scope, but we're processing it's body now
4337 that the class is complete. */
4340 /* Core issue 226 (C++0x only): the following only applies to class
4343 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
4347 If a template-parameter has a default template-argument, all
4348 subsequent template-parameters shall have a default
4349 template-argument supplied. */
4350 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4352 tree inner_parms
= TREE_VALUE (parm_level
);
4353 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4354 int seen_def_arg_p
= 0;
4357 for (i
= 0; i
< ntparms
; ++i
)
4359 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4361 if (parm
== error_mark_node
)
4364 if (TREE_PURPOSE (parm
))
4366 else if (seen_def_arg_p
4367 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4369 error ("no default argument for %qD", TREE_VALUE (parm
));
4370 /* For better subsequent error-recovery, we indicate that
4371 there should have been a default argument. */
4372 TREE_PURPOSE (parm
) = error_mark_node
;
4375 else if (!is_partial
4377 /* Don't complain about an enclosing partial
4379 && parm_level
== parms
4380 && TREE_CODE (decl
) == TYPE_DECL
4382 && template_parameter_pack_p (TREE_VALUE (parm
)))
4384 /* A primary class template can only have one
4385 parameter pack, at the end of the template
4388 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
4389 error ("parameter pack %qE must be at the end of the"
4390 " template parameter list", TREE_VALUE (parm
));
4392 error ("parameter pack %qT must be at the end of the"
4393 " template parameter list",
4394 TREE_TYPE (TREE_VALUE (parm
)));
4396 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4404 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4408 /* For an ordinary class template, default template arguments are
4409 allowed at the innermost level, e.g.:
4410 template <class T = int>
4412 but, in a partial specialization, they're not allowed even
4413 there, as we have in [temp.class.spec]:
4415 The template parameter list of a specialization shall not
4416 contain default template argument values.
4418 So, for a partial specialization, or for a function template
4419 (in C++98/C++03), we look at all of them. */
4422 /* But, for a primary class template that is not a partial
4423 specialization we look at all template parameters except the
4425 parms
= TREE_CHAIN (parms
);
4427 /* Figure out what error message to issue. */
4428 if (is_friend_decl
== 2)
4429 msg
= G_("default template arguments may not be used in function template "
4430 "friend re-declaration");
4431 else if (is_friend_decl
)
4432 msg
= G_("default template arguments may not be used in function template "
4433 "friend declarations");
4434 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4435 msg
= G_("default template arguments may not be used in function templates "
4436 "without -std=c++11 or -std=gnu++11");
4437 else if (is_partial
)
4438 msg
= G_("default template arguments may not be used in "
4439 "partial specializations");
4441 msg
= G_("default argument for template parameter for class enclosing %qD");
4443 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4444 /* If we're inside a class definition, there's no need to
4445 examine the parameters to the class itself. On the one
4446 hand, they will be checked when the class is defined, and,
4447 on the other, default arguments are valid in things like:
4448 template <class T = double>
4449 struct S { template <class U> void f(U); };
4450 Here the default argument for `S' has no bearing on the
4451 declaration of `f'. */
4452 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4454 /* Check everything. */
4455 last_level_to_check
= 0;
4457 for (parm_level
= parms
;
4458 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4459 parm_level
= TREE_CHAIN (parm_level
))
4461 tree inner_parms
= TREE_VALUE (parm_level
);
4465 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4466 for (i
= 0; i
< ntparms
; ++i
)
4468 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4471 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4476 if (is_friend_decl
== 2)
4483 /* Clear out the default argument so that we are not
4485 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4489 /* At this point, if we're still interested in issuing messages,
4490 they must apply to classes surrounding the object declared. */
4492 msg
= G_("default argument for template parameter for class "
4499 /* Worker for push_template_decl_real, called via
4500 for_each_template_parm. DATA is really an int, indicating the
4501 level of the parameters we are interested in. If T is a template
4502 parameter of that level, return nonzero. */
4505 template_parm_this_level_p (tree t
, void* data
)
4507 int this_level
= *(int *)data
;
4510 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4511 level
= TEMPLATE_PARM_LEVEL (t
);
4513 level
= TEMPLATE_TYPE_LEVEL (t
);
4514 return level
== this_level
;
4517 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4518 parameters given by current_template_args, or reuses a
4519 previously existing one, if appropriate. Returns the DECL, or an
4520 equivalent one, if it is replaced via a call to duplicate_decls.
4522 If IS_FRIEND is true, DECL is a friend declaration. */
4525 push_template_decl_real (tree decl
, bool is_friend
)
4533 int new_template_p
= 0;
4534 /* True if the template is a member template, in the sense of
4536 bool member_template_p
= false;
4538 if (decl
== error_mark_node
|| !current_template_parms
)
4539 return error_mark_node
;
4541 /* See if this is a partial specialization. */
4542 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
4543 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4544 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
4546 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4550 /* For a friend, we want the context of the friend function, not
4551 the type of which it is a friend. */
4552 ctx
= CP_DECL_CONTEXT (decl
);
4553 else if (CP_DECL_CONTEXT (decl
)
4554 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4555 /* In the case of a virtual function, we want the class in which
4557 ctx
= CP_DECL_CONTEXT (decl
);
4559 /* Otherwise, if we're currently defining some class, the DECL
4560 is assumed to be a member of the class. */
4561 ctx
= current_scope ();
4563 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4566 if (!DECL_CONTEXT (decl
))
4567 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4569 /* See if this is a primary template. */
4570 if (is_friend
&& ctx
)
4571 /* A friend template that specifies a class context, i.e.
4572 template <typename T> friend void A<T>::f();
4576 is_primary
= template_parm_scope_p ();
4580 if (DECL_CLASS_SCOPE_P (decl
))
4581 member_template_p
= true;
4582 if (TREE_CODE (decl
) == TYPE_DECL
4583 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4585 error ("template class without a name");
4586 return error_mark_node
;
4588 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4590 if (DECL_DESTRUCTOR_P (decl
))
4594 A destructor shall not be a member template. */
4595 error ("destructor %qD declared as member template", decl
);
4596 return error_mark_node
;
4598 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4599 && (!prototype_p (TREE_TYPE (decl
))
4600 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4601 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4602 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4603 == void_list_node
)))
4605 /* [basic.stc.dynamic.allocation]
4607 An allocation function can be a function
4608 template. ... Template allocation functions shall
4609 have two or more parameters. */
4610 error ("invalid template declaration of %qD", decl
);
4611 return error_mark_node
;
4614 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4615 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4617 else if (TREE_CODE (decl
) == TYPE_DECL
4618 && TYPE_DECL_ALIAS_P (decl
))
4619 /* alias-declaration */
4620 gcc_assert (!DECL_ARTIFICIAL (decl
));
4623 error ("template declaration of %q#D", decl
);
4624 return error_mark_node
;
4628 /* Check to see that the rules regarding the use of default
4629 arguments are not being violated. */
4630 check_default_tmpl_args (decl
, current_template_parms
,
4631 is_primary
, is_partial
, /*is_friend_decl=*/0);
4633 /* Ensure that there are no parameter packs in the type of this
4634 declaration that have not been expanded. */
4635 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4637 /* Check each of the arguments individually to see if there are
4638 any bare parameter packs. */
4639 tree type
= TREE_TYPE (decl
);
4640 tree arg
= DECL_ARGUMENTS (decl
);
4641 tree argtype
= TYPE_ARG_TYPES (type
);
4643 while (arg
&& argtype
)
4645 if (!FUNCTION_PARAMETER_PACK_P (arg
)
4646 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4648 /* This is a PARM_DECL that contains unexpanded parameter
4649 packs. We have already complained about this in the
4650 check_for_bare_parameter_packs call, so just replace
4651 these types with ERROR_MARK_NODE. */
4652 TREE_TYPE (arg
) = error_mark_node
;
4653 TREE_VALUE (argtype
) = error_mark_node
;
4656 arg
= DECL_CHAIN (arg
);
4657 argtype
= TREE_CHAIN (argtype
);
4660 /* Check for bare parameter packs in the return type and the
4661 exception specifiers. */
4662 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4663 /* Errors were already issued, set return type to int
4664 as the frontend doesn't expect error_mark_node as
4666 TREE_TYPE (type
) = integer_type_node
;
4667 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4668 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4670 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
4671 && TYPE_DECL_ALIAS_P (decl
))
4672 ? DECL_ORIGINAL_TYPE (decl
)
4673 : TREE_TYPE (decl
)))
4675 TREE_TYPE (decl
) = error_mark_node
;
4676 return error_mark_node
;
4680 return process_partial_specialization (decl
);
4682 args
= current_template_args ();
4685 || TREE_CODE (ctx
) == FUNCTION_DECL
4686 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4687 || (TREE_CODE (decl
) == TYPE_DECL
4688 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4689 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4691 if (DECL_LANG_SPECIFIC (decl
)
4692 && DECL_TEMPLATE_INFO (decl
)
4693 && DECL_TI_TEMPLATE (decl
))
4694 tmpl
= DECL_TI_TEMPLATE (decl
);
4695 /* If DECL is a TYPE_DECL for a class-template, then there won't
4696 be DECL_LANG_SPECIFIC. The information equivalent to
4697 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4698 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4699 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4700 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4702 /* Since a template declaration already existed for this
4703 class-type, we must be redeclaring it here. Make sure
4704 that the redeclaration is valid. */
4705 redeclare_class_template (TREE_TYPE (decl
),
4706 current_template_parms
);
4707 /* We don't need to create a new TEMPLATE_DECL; just use the
4708 one we already had. */
4709 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4713 tmpl
= build_template_decl (decl
, current_template_parms
,
4717 if (DECL_LANG_SPECIFIC (decl
)
4718 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4720 /* A specialization of a member template of a template
4722 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4723 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4724 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4730 tree a
, t
, current
, parms
;
4732 tree tinfo
= get_template_info (decl
);
4736 error ("template definition of non-template %q#D", decl
);
4737 return error_mark_node
;
4740 tmpl
= TI_TEMPLATE (tinfo
);
4742 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4743 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4744 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4745 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4749 /* The declaration is a specialization of a member
4750 template, declared outside the class. Therefore, the
4751 innermost template arguments will be NULL, so we
4752 replace them with the arguments determined by the
4753 earlier call to check_explicit_specialization. */
4754 args
= DECL_TI_ARGS (decl
);
4757 = build_template_decl (decl
, current_template_parms
,
4759 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4760 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4761 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4762 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4763 DECL_TEMPLATE_INFO (new_tmpl
)
4764 = build_template_info (tmpl
, args
);
4766 register_specialization (new_tmpl
,
4767 most_general_template (tmpl
),
4773 /* Make sure the template headers we got make sense. */
4775 parms
= DECL_TEMPLATE_PARMS (tmpl
);
4776 i
= TMPL_PARMS_DEPTH (parms
);
4777 if (TMPL_ARGS_DEPTH (args
) != i
)
4779 error ("expected %d levels of template parms for %q#D, got %d",
4780 i
, decl
, TMPL_ARGS_DEPTH (args
));
4783 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
4785 a
= TMPL_ARGS_LEVEL (args
, i
);
4786 t
= INNERMOST_TEMPLATE_PARMS (parms
);
4788 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
4790 if (current
== decl
)
4791 error ("got %d template parameters for %q#D",
4792 TREE_VEC_LENGTH (a
), decl
);
4794 error ("got %d template parameters for %q#T",
4795 TREE_VEC_LENGTH (a
), current
);
4796 error (" but %d required", TREE_VEC_LENGTH (t
));
4797 /* Avoid crash in import_export_decl. */
4798 DECL_INTERFACE_KNOWN (decl
) = 1;
4799 return error_mark_node
;
4802 if (current
== decl
)
4804 else if (current
== NULL_TREE
)
4805 /* Can happen in erroneous input. */
4808 current
= (TYPE_P (current
)
4809 ? TYPE_CONTEXT (current
)
4810 : DECL_CONTEXT (current
));
4813 /* Check that the parms are used in the appropriate qualifying scopes
4814 in the declarator. */
4815 if (!comp_template_args
4817 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
4820 template arguments to %qD do not match original template %qD",
4821 decl
, DECL_TEMPLATE_RESULT (tmpl
));
4822 if (!uses_template_parms (TI_ARGS (tinfo
)))
4823 inform (input_location
, "use template<> for an explicit specialization");
4824 /* Avoid crash in import_export_decl. */
4825 DECL_INTERFACE_KNOWN (decl
) = 1;
4826 return error_mark_node
;
4830 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4831 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4833 /* Push template declarations for global functions and types. Note
4834 that we do not try to push a global template friend declared in a
4835 template class; such a thing may well depend on the template
4836 parameters of the class. */
4837 if (new_template_p
&& !ctx
4838 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
4840 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
4841 if (tmpl
== error_mark_node
)
4842 return error_mark_node
;
4844 /* Hide template friend classes that haven't been declared yet. */
4845 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
4847 DECL_ANTICIPATED (tmpl
) = 1;
4848 DECL_FRIEND_P (tmpl
) = 1;
4854 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
4857 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4858 if (DECL_CONV_FN_P (tmpl
))
4860 int depth
= TMPL_PARMS_DEPTH (parms
);
4862 /* It is a conversion operator. See if the type converted to
4863 depends on innermost template operands. */
4865 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
4867 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
4870 /* Give template template parms a DECL_CONTEXT of the template
4871 for which they are a parameter. */
4872 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4873 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
4875 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4876 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4877 DECL_CONTEXT (parm
) = tmpl
;
4881 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4882 back to its most general template. If TMPL is a specialization,
4883 ARGS may only have the innermost set of arguments. Add the missing
4884 argument levels if necessary. */
4885 if (DECL_TEMPLATE_INFO (tmpl
))
4886 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
4888 info
= build_template_info (tmpl
, args
);
4890 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
4891 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
4894 if (is_primary
&& !DECL_LANG_SPECIFIC (decl
))
4895 retrofit_lang_decl (decl
);
4896 if (DECL_LANG_SPECIFIC (decl
))
4897 DECL_TEMPLATE_INFO (decl
) = info
;
4900 return DECL_TEMPLATE_RESULT (tmpl
);
4904 push_template_decl (tree decl
)
4906 return push_template_decl_real (decl
, false);
4909 /* FN is an inheriting constructor that inherits from the constructor
4910 template INHERITED; turn FN into a constructor template with a matching
4914 add_inherited_template_parms (tree fn
, tree inherited
)
4917 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
4918 inner_parms
= copy_node (inner_parms
);
4920 = tree_cons (size_int (processing_template_decl
+ 1),
4921 inner_parms
, current_template_parms
);
4922 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
4923 tree args
= template_parms_to_args (parms
);
4924 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
4925 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
4926 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
4927 DECL_ARTIFICIAL (tmpl
) = true;
4928 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4932 /* Called when a class template TYPE is redeclared with the indicated
4933 template PARMS, e.g.:
4935 template <class T> struct S;
4936 template <class T> struct S {}; */
4939 redeclare_class_template (tree type
, tree parms
)
4945 if (!TYPE_TEMPLATE_INFO (type
))
4947 error ("%qT is not a template type", type
);
4951 tmpl
= TYPE_TI_TEMPLATE (type
);
4952 if (!PRIMARY_TEMPLATE_P (tmpl
))
4953 /* The type is nested in some template class. Nothing to worry
4954 about here; there are no new template parameters for the nested
4960 error ("template specifiers not specified in declaration of %qD",
4965 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4966 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
4968 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
4970 error_n (input_location
, TREE_VEC_LENGTH (parms
),
4971 "redeclared with %d template parameter",
4972 "redeclared with %d template parameters",
4973 TREE_VEC_LENGTH (parms
));
4974 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
4975 "previous declaration %q+D used %d template parameter",
4976 "previous declaration %q+D used %d template parameters",
4977 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
4981 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
4988 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
4989 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
4992 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
4993 if (tmpl_parm
== error_mark_node
)
4996 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4997 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
4998 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5000 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5002 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5003 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5004 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5005 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5006 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5007 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5008 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5009 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5010 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5012 error ("template parameter %q+#D", tmpl_parm
);
5013 error ("redeclared here as %q#D", parm
);
5017 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5019 /* We have in [temp.param]:
5021 A template-parameter may not be given default arguments
5022 by two different declarations in the same scope. */
5023 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5024 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5025 "original definition appeared here");
5029 if (parm_default
!= NULL_TREE
)
5030 /* Update the previous template parameters (which are the ones
5031 that will really count) with the new default value. */
5032 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5033 else if (tmpl_default
!= NULL_TREE
)
5034 /* Update the new parameters, too; they'll be used as the
5035 parameters for any members. */
5036 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5042 /* Simplify EXPR if it is a non-dependent expression. Returns the
5043 (possibly simplified) expression. */
5046 fold_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5048 if (expr
== NULL_TREE
)
5051 /* If we're in a template, but EXPR isn't value dependent, simplify
5052 it. We're supposed to treat:
5054 template <typename T> void f(T[1 + 1]);
5055 template <typename T> void f(T[2]);
5057 as two declarations of the same function, for example. */
5058 if (processing_template_decl
5059 && !instantiation_dependent_expression_p (expr
)
5060 && potential_constant_expression (expr
))
5062 HOST_WIDE_INT saved_processing_template_decl
;
5064 saved_processing_template_decl
= processing_template_decl
;
5065 processing_template_decl
= 0;
5066 expr
= tsubst_copy_and_build (expr
,
5069 /*in_decl=*/NULL_TREE
,
5070 /*function_p=*/false,
5071 /*integral_constant_expression_p=*/true);
5072 processing_template_decl
= saved_processing_template_decl
;
5078 fold_non_dependent_expr (tree expr
)
5080 return fold_non_dependent_expr_sfinae (expr
, tf_error
);
5083 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5084 template declaration, or a TYPE_DECL for an alias declaration. */
5087 alias_type_or_template_p (tree t
)
5091 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5094 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5095 || DECL_ALIAS_TEMPLATE_P (t
));
5098 /* Return TRUE iff is a specialization of an alias template. */
5101 alias_template_specialization_p (const_tree t
)
5107 && TYPE_TEMPLATE_INFO (t
)
5108 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t
))
5109 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t
)));
5112 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5113 must be a function or a pointer-to-function type, as specified
5114 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5115 and check that the resulting function has external linkage. */
5118 convert_nontype_argument_function (tree type
, tree expr
)
5122 linkage_kind linkage
;
5124 fn
= instantiate_type (type
, fns
, tf_none
);
5125 if (fn
== error_mark_node
)
5126 return error_mark_node
;
5129 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5130 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5131 if (BASELINK_P (fn_no_ptr
))
5132 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5134 /* [temp.arg.nontype]/1
5136 A template-argument for a non-type, non-template template-parameter
5139 -- the address of an object or function with external [C++11: or
5140 internal] linkage. */
5142 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
5144 error ("%qE is not a valid template argument for type %qT", expr
, type
);
5145 if (TREE_CODE (type
) == POINTER_TYPE
)
5146 error ("it must be the address of a function with external linkage");
5148 error ("it must be the name of a function with external linkage");
5152 linkage
= decl_linkage (fn_no_ptr
);
5153 if (cxx_dialect
>= cxx0x
? linkage
== lk_none
: linkage
!= lk_external
)
5155 if (cxx_dialect
>= cxx0x
)
5156 error ("%qE is not a valid template argument for type %qT "
5157 "because %qD has no linkage",
5158 expr
, type
, fn_no_ptr
);
5160 error ("%qE is not a valid template argument for type %qT "
5161 "because %qD does not have external linkage",
5162 expr
, type
, fn_no_ptr
);
5169 /* Subroutine of convert_nontype_argument.
5170 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5171 Emit an error otherwise. */
5174 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5175 tsubst_flags_t complain
)
5178 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5180 if (cxx_dialect
>= cxx0x
&& null_member_pointer_value_p (expr
))
5182 if (complain
& tf_error
)
5184 error ("%qE is not a valid template argument for type %qT",
5186 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5191 /* Returns TRUE iff the address of OP is value-dependent.
5193 14.6.2.4 [temp.dep.temp]:
5194 A non-integral non-type template-argument is dependent if its type is
5195 dependent or it has either of the following forms
5198 and contains a nested-name-specifier which specifies a class-name that
5199 names a dependent type.
5201 We generalize this to just say that the address of a member of a
5202 dependent class is value-dependent; the above doesn't cover the
5203 address of a static data member named with an unqualified-id. */
5206 has_value_dependent_address (tree op
)
5208 /* We could use get_inner_reference here, but there's no need;
5209 this is only relevant for template non-type arguments, which
5210 can only be expressed as &id-expression. */
5213 tree ctx
= CP_DECL_CONTEXT (op
);
5214 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5221 /* The next set of functions are used for providing helpful explanatory
5222 diagnostics for failed overload resolution. Their messages should be
5223 indented by two spaces for consistency with the messages in
5227 unify_success (bool /*explain_p*/)
5233 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5236 inform (input_location
,
5237 " couldn't deduce template parameter %qD", parm
);
5242 unify_invalid (bool /*explain_p*/)
5248 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5251 inform (input_location
,
5252 " types %qT and %qT have incompatible cv-qualifiers",
5258 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5261 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5266 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
5269 inform (input_location
,
5270 " template parameter %qD is not a parameter pack, but "
5277 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
5280 inform (input_location
,
5281 " template argument %qE does not match "
5282 "pointer-to-member constant %qE",
5288 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
5291 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
5296 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
5299 inform (input_location
,
5300 " inconsistent parameter pack deduction with %qT and %qT",
5306 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
5311 inform (input_location
,
5312 " deduced conflicting types for parameter %qT (%qT and %qT)",
5313 parm
, first
, second
);
5315 inform (input_location
,
5316 " deduced conflicting values for non-type parameter "
5317 "%qE (%qE and %qE)", parm
, first
, second
);
5323 unify_vla_arg (bool explain_p
, tree arg
)
5326 inform (input_location
,
5327 " variable-sized array type %qT is not "
5328 "a valid template argument",
5334 unify_method_type_error (bool explain_p
, tree arg
)
5337 inform (input_location
,
5338 " member function type %qT is not a valid template argument",
5344 unify_arity (bool explain_p
, int have
, int wanted
)
5347 inform_n (input_location
, wanted
,
5348 " candidate expects %d argument, %d provided",
5349 " candidate expects %d arguments, %d provided",
5355 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
5357 return unify_arity (explain_p
, have
, wanted
);
5361 unify_too_few_arguments (bool explain_p
, int have
, int wanted
)
5363 return unify_arity (explain_p
, have
, wanted
);
5367 unify_arg_conversion (bool explain_p
, tree to_type
,
5368 tree from_type
, tree arg
)
5371 inform (input_location
, " cannot convert %qE (type %qT) to type %qT",
5372 arg
, from_type
, to_type
);
5377 unify_no_common_base (bool explain_p
, enum template_base_result r
,
5378 tree parm
, tree arg
)
5383 case tbr_ambiguous_baseclass
:
5384 inform (input_location
, " %qT is an ambiguous base class of %qT",
5388 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
5395 unify_inconsistent_template_template_parameters (bool explain_p
)
5398 inform (input_location
,
5399 " template parameters of a template template argument are "
5400 "inconsistent with other deduced template arguments");
5405 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
5408 inform (input_location
,
5409 " can't deduce a template for %qT from non-template type %qT",
5415 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
5418 inform (input_location
,
5419 " template argument %qE does not match %qD", arg
, parm
);
5424 unify_overload_resolution_failure (bool explain_p
, tree arg
)
5427 inform (input_location
,
5428 " could not resolve address from overloaded function %qE",
5433 /* Attempt to convert the non-type template parameter EXPR to the
5434 indicated TYPE. If the conversion is successful, return the
5435 converted value. If the conversion is unsuccessful, return
5436 NULL_TREE if we issued an error message, or error_mark_node if we
5437 did not. We issue error messages for out-and-out bad template
5438 parameters, but not simply because the conversion failed, since we
5439 might be just trying to do argument deduction. Both TYPE and EXPR
5440 must be non-dependent.
5442 The conversion follows the special rules described in
5443 [temp.arg.nontype], and it is much more strict than an implicit
5446 This function is called twice for each template argument (see
5447 lookup_template_class for a more accurate description of this
5448 problem). This means that we need to handle expressions which
5449 are not valid in a C++ source, but can be created from the
5450 first call (for instance, casts to perform conversions). These
5451 hacks can go away after we fix the double coercion problem. */
5454 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5458 /* Detect immediately string literals as invalid non-type argument.
5459 This special-case is not needed for correctness (we would easily
5460 catch this later), but only to provide better diagnostic for this
5461 common user mistake. As suggested by DR 100, we do not mention
5462 linkage issues in the diagnostic as this is not the point. */
5463 /* FIXME we're making this OK. */
5464 if (TREE_CODE (expr
) == STRING_CST
)
5466 if (complain
& tf_error
)
5467 error ("%qE is not a valid template argument for type %qT "
5468 "because string literals can never be used in this context",
5473 /* Add the ADDR_EXPR now for the benefit of
5474 value_dependent_expression_p. */
5475 if (TYPE_PTROBV_P (type
)
5476 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
5478 expr
= decay_conversion (expr
, complain
);
5479 if (expr
== error_mark_node
)
5480 return error_mark_node
;
5483 /* If we are in a template, EXPR may be non-dependent, but still
5484 have a syntactic, rather than semantic, form. For example, EXPR
5485 might be a SCOPE_REF, rather than the VAR_DECL to which the
5486 SCOPE_REF refers. Preserving the qualifying scope is necessary
5487 so that access checking can be performed when the template is
5488 instantiated -- but here we need the resolved form so that we can
5489 convert the argument. */
5490 if (TYPE_REF_OBJ_P (type
)
5491 && has_value_dependent_address (expr
))
5492 /* If we want the address and it's value-dependent, don't fold. */;
5493 else if (!type_unknown_p (expr
))
5494 expr
= fold_non_dependent_expr_sfinae (expr
, complain
);
5495 if (error_operand_p (expr
))
5496 return error_mark_node
;
5497 expr_type
= TREE_TYPE (expr
);
5498 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5499 expr
= mark_lvalue_use (expr
);
5501 expr
= mark_rvalue_use (expr
);
5503 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5504 to a non-type argument of "nullptr". */
5505 if (expr
== nullptr_node
&& TYPE_PTR_OR_PTRMEM_P (type
))
5506 expr
= convert (type
, expr
);
5508 /* In C++11, integral or enumeration non-type template arguments can be
5509 arbitrary constant expressions. Pointer and pointer to
5510 member arguments can be general constant expressions that evaluate
5511 to a null value, but otherwise still need to be of a specific form. */
5512 if (cxx_dialect
>= cxx0x
)
5514 if (TREE_CODE (expr
) == PTRMEM_CST
)
5515 /* A PTRMEM_CST is already constant, and a valid template
5516 argument for a parameter of pointer to member type, we just want
5517 to leave it in that form rather than lower it to a
5519 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5520 expr
= maybe_constant_value (expr
);
5521 else if (TYPE_PTR_OR_PTRMEM_P (type
))
5523 tree folded
= maybe_constant_value (expr
);
5524 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
5525 : null_member_pointer_value_p (folded
))
5530 /* HACK: Due to double coercion, we can get a
5531 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5532 which is the tree that we built on the first call (see
5533 below when coercing to reference to object or to reference to
5534 function). We just strip everything and get to the arg.
5535 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5537 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5539 tree probe_type
, probe
= expr
;
5540 if (REFERENCE_REF_P (probe
))
5541 probe
= TREE_OPERAND (probe
, 0);
5542 probe_type
= TREE_TYPE (probe
);
5543 if (TREE_CODE (probe
) == NOP_EXPR
)
5545 /* ??? Maybe we could use convert_from_reference here, but we
5546 would need to relax its constraints because the NOP_EXPR
5547 could actually change the type to something more cv-qualified,
5548 and this is not folded by convert_from_reference. */
5549 tree addr
= TREE_OPERAND (probe
, 0);
5550 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
5551 && TREE_CODE (addr
) == ADDR_EXPR
5552 && TREE_CODE (TREE_TYPE (addr
)) == POINTER_TYPE
5553 && (same_type_ignoring_top_level_qualifiers_p
5554 (TREE_TYPE (probe_type
),
5555 TREE_TYPE (TREE_TYPE (addr
)))))
5557 expr
= TREE_OPERAND (addr
, 0);
5558 expr_type
= TREE_TYPE (expr
);
5563 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5564 parameter is a pointer to object, through decay and
5565 qualification conversion. Let's strip everything. */
5566 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
5570 if (TREE_CODE (probe
) == ADDR_EXPR
5571 && TREE_CODE (TREE_TYPE (probe
)) == POINTER_TYPE
)
5573 /* Skip the ADDR_EXPR only if it is part of the decay for
5574 an array. Otherwise, it is part of the original argument
5575 in the source code. */
5576 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe
, 0))) == ARRAY_TYPE
)
5577 probe
= TREE_OPERAND (probe
, 0);
5579 expr_type
= TREE_TYPE (expr
);
5583 /* [temp.arg.nontype]/5, bullet 1
5585 For a non-type template-parameter of integral or enumeration type,
5586 integral promotions (_conv.prom_) and integral conversions
5587 (_conv.integral_) are applied. */
5588 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5590 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
5591 t
= maybe_constant_value (t
);
5592 if (t
!= error_mark_node
)
5595 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
5596 return error_mark_node
;
5598 /* Notice that there are constant expressions like '4 % 0' which
5599 do not fold into integer constants. */
5600 if (TREE_CODE (expr
) != INTEGER_CST
)
5602 if (complain
& tf_error
)
5604 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
5605 if (processing_template_decl
5606 && !require_potential_constant_expression (expr
))
5608 expr
= cxx_constant_value (expr
);
5609 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
5610 inform (EXPR_LOC_OR_HERE (expr
),
5611 "in template argument for type %qT ", type
);
5612 if (expr
== error_mark_node
)
5614 /* else cxx_constant_value complained but gave us
5615 a real constant, so go ahead. */
5616 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
5622 /* [temp.arg.nontype]/5, bullet 2
5624 For a non-type template-parameter of type pointer to object,
5625 qualification conversions (_conv.qual_) and the array-to-pointer
5626 conversion (_conv.array_) are applied. */
5627 else if (TYPE_PTROBV_P (type
))
5629 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5631 A template-argument for a non-type, non-template template-parameter
5632 shall be one of: [...]
5634 -- the name of a non-type template-parameter;
5635 -- the address of an object or function with external linkage, [...]
5636 expressed as "& id-expression" where the & is optional if the name
5637 refers to a function or array, or if the corresponding
5638 template-parameter is a reference.
5640 Here, we do not care about functions, as they are invalid anyway
5641 for a parameter of type pointer-to-object. */
5643 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5644 /* Non-type template parameters are OK. */
5646 else if (cxx_dialect
>= cxx0x
&& integer_zerop (expr
))
5647 /* Null pointer values are OK in C++11. */;
5648 else if (TREE_CODE (expr
) != ADDR_EXPR
5649 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5651 if (TREE_CODE (expr
) == VAR_DECL
)
5653 error ("%qD is not a valid template argument "
5654 "because %qD is a variable, not the address of "
5659 if (POINTER_TYPE_P (expr_type
))
5661 error ("%qE is not a valid template argument for %qT"
5662 "because it is not the address of a variable",
5666 /* Other values, like integer constants, might be valid
5667 non-type arguments of some other type. */
5668 return error_mark_node
;
5674 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
5675 ? TREE_OPERAND (expr
, 0) : expr
);
5676 if (TREE_CODE (decl
) != VAR_DECL
)
5678 error ("%qE is not a valid template argument of type %qT "
5679 "because %qE is not a variable",
5683 else if (cxx_dialect
< cxx0x
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
5685 error ("%qE is not a valid template argument of type %qT "
5686 "because %qD does not have external linkage",
5690 else if (cxx_dialect
>= cxx0x
&& decl_linkage (decl
) == lk_none
)
5692 error ("%qE is not a valid template argument of type %qT "
5693 "because %qD has no linkage",
5699 expr
= decay_conversion (expr
, complain
);
5700 if (expr
== error_mark_node
)
5701 return error_mark_node
;
5703 expr
= perform_qualification_conversions (type
, expr
);
5704 if (expr
== error_mark_node
)
5705 return error_mark_node
;
5707 /* [temp.arg.nontype]/5, bullet 3
5709 For a non-type template-parameter of type reference to object, no
5710 conversions apply. The type referred to by the reference may be more
5711 cv-qualified than the (otherwise identical) type of the
5712 template-argument. The template-parameter is bound directly to the
5713 template-argument, which must be an lvalue. */
5714 else if (TYPE_REF_OBJ_P (type
))
5716 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
5718 return error_mark_node
;
5720 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5722 error ("%qE is not a valid template argument for type %qT "
5723 "because of conflicts in cv-qualification", expr
, type
);
5727 if (!real_lvalue_p (expr
))
5729 error ("%qE is not a valid template argument for type %qT "
5730 "because it is not an lvalue", expr
, type
);
5734 /* [temp.arg.nontype]/1
5736 A template-argument for a non-type, non-template template-parameter
5737 shall be one of: [...]
5739 -- the address of an object or function with external linkage. */
5740 if (TREE_CODE (expr
) == INDIRECT_REF
5741 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5743 expr
= TREE_OPERAND (expr
, 0);
5746 error ("%q#D is not a valid template argument for type %qT "
5747 "because a reference variable does not have a constant "
5748 "address", expr
, type
);
5755 error ("%qE is not a valid template argument for type %qT "
5756 "because it is not an object with external linkage",
5761 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5763 error ("%qE is not a valid template argument for type %qT "
5764 "because object %qD has not external linkage",
5769 expr
= build_nop (type
, build_address (expr
));
5771 /* [temp.arg.nontype]/5, bullet 4
5773 For a non-type template-parameter of type pointer to function, only
5774 the function-to-pointer conversion (_conv.func_) is applied. If the
5775 template-argument represents a set of overloaded functions (or a
5776 pointer to such), the matching function is selected from the set
5778 else if (TYPE_PTRFN_P (type
))
5780 /* If the argument is a template-id, we might not have enough
5781 context information to decay the pointer. */
5782 if (!type_unknown_p (expr_type
))
5784 expr
= decay_conversion (expr
, complain
);
5785 if (expr
== error_mark_node
)
5786 return error_mark_node
;
5789 if (cxx_dialect
>= cxx0x
&& integer_zerop (expr
))
5790 /* Null pointer values are OK in C++11. */
5791 return perform_qualification_conversions (type
, expr
);
5793 expr
= convert_nontype_argument_function (type
, expr
);
5794 if (!expr
|| expr
== error_mark_node
)
5797 /* [temp.arg.nontype]/5, bullet 5
5799 For a non-type template-parameter of type reference to function, no
5800 conversions apply. If the template-argument represents a set of
5801 overloaded functions, the matching function is selected from the set
5803 else if (TYPE_REFFN_P (type
))
5805 if (TREE_CODE (expr
) == ADDR_EXPR
)
5807 error ("%qE is not a valid template argument for type %qT "
5808 "because it is a pointer", expr
, type
);
5809 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
5813 expr
= convert_nontype_argument_function (type
, expr
);
5814 if (!expr
|| expr
== error_mark_node
)
5817 expr
= build_nop (type
, build_address (expr
));
5819 /* [temp.arg.nontype]/5, bullet 6
5821 For a non-type template-parameter of type pointer to member function,
5822 no conversions apply. If the template-argument represents a set of
5823 overloaded member functions, the matching member function is selected
5824 from the set (_over.over_). */
5825 else if (TYPE_PTRMEMFUNC_P (type
))
5827 expr
= instantiate_type (type
, expr
, tf_none
);
5828 if (expr
== error_mark_node
)
5829 return error_mark_node
;
5831 /* [temp.arg.nontype] bullet 1 says the pointer to member
5832 expression must be a pointer-to-member constant. */
5833 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5834 return error_mark_node
;
5836 /* There is no way to disable standard conversions in
5837 resolve_address_of_overloaded_function (called by
5838 instantiate_type). It is possible that the call succeeded by
5839 converting &B::I to &D::I (where B is a base of D), so we need
5840 to reject this conversion here.
5842 Actually, even if there was a way to disable standard conversions,
5843 it would still be better to reject them here so that we can
5844 provide a superior diagnostic. */
5845 if (!same_type_p (TREE_TYPE (expr
), type
))
5847 error ("%qE is not a valid template argument for type %qT "
5848 "because it is of type %qT", expr
, type
,
5850 /* If we are just one standard conversion off, explain. */
5851 if (can_convert (type
, TREE_TYPE (expr
), complain
))
5852 inform (input_location
,
5853 "standard conversions are not allowed in this context");
5857 /* [temp.arg.nontype]/5, bullet 7
5859 For a non-type template-parameter of type pointer to data member,
5860 qualification conversions (_conv.qual_) are applied. */
5861 else if (TYPE_PTRDATAMEM_P (type
))
5863 /* [temp.arg.nontype] bullet 1 says the pointer to member
5864 expression must be a pointer-to-member constant. */
5865 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5866 return error_mark_node
;
5868 expr
= perform_qualification_conversions (type
, expr
);
5869 if (expr
== error_mark_node
)
5872 else if (NULLPTR_TYPE_P (type
))
5874 if (expr
!= nullptr_node
)
5876 error ("%qE is not a valid template argument for type %qT "
5877 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
5882 /* A template non-type parameter must be one of the above. */
5886 /* Sanity check: did we actually convert the argument to the
5888 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5889 (type
, TREE_TYPE (expr
)));
5893 /* Subroutine of coerce_template_template_parms, which returns 1 if
5894 PARM_PARM and ARG_PARM match using the rule for the template
5895 parameters of template template parameters. Both PARM and ARG are
5896 template parameters; the rest of the arguments are the same as for
5897 coerce_template_template_parms.
5900 coerce_template_template_parm (tree parm
,
5902 tsubst_flags_t complain
,
5906 if (arg
== NULL_TREE
|| arg
== error_mark_node
5907 || parm
== NULL_TREE
|| parm
== error_mark_node
)
5910 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5913 switch (TREE_CODE (parm
))
5916 /* We encounter instantiations of templates like
5917 template <template <template <class> class> class TT>
5920 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
5921 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
5923 if (!coerce_template_template_parms
5924 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
5930 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
5931 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5932 /* Argument is a parameter pack but parameter is not. */
5937 /* The tsubst call is used to handle cases such as
5939 template <int> class C {};
5940 template <class T, template <T> class TT> class D {};
5943 i.e. the parameter list of TT depends on earlier parameters. */
5944 if (!uses_template_parms (TREE_TYPE (arg
))
5946 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
5950 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
5951 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
5952 /* Argument is a parameter pack but parameter is not. */
5965 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5966 template template parameters. Both PARM_PARMS and ARG_PARMS are
5967 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5970 Consider the example:
5971 template <class T> class A;
5972 template<template <class U> class TT> class B;
5974 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5975 the parameters to A, and OUTER_ARGS contains A. */
5978 coerce_template_template_parms (tree parm_parms
,
5980 tsubst_flags_t complain
,
5984 int nparms
, nargs
, i
;
5988 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
5989 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
5991 nparms
= TREE_VEC_LENGTH (parm_parms
);
5992 nargs
= TREE_VEC_LENGTH (arg_parms
);
5994 /* Determine whether we have a parameter pack at the end of the
5995 template template parameter's template parameter list. */
5996 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
5998 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
6000 if (parm
== error_mark_node
)
6003 switch (TREE_CODE (parm
))
6007 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6012 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6022 && !(variadic_p
&& nargs
>= nparms
- 1))
6025 /* Check all of the template parameters except the parameter pack at
6026 the end (if any). */
6027 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6029 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6030 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6033 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6034 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6036 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6044 /* Check each of the template parameters in the template
6045 argument against the template parameter pack at the end of
6046 the template template parameter. */
6047 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6050 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6052 for (; i
< nargs
; ++i
)
6054 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6057 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6059 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6068 /* Verifies that the deduced template arguments (in TARGS) for the
6069 template template parameters (in TPARMS) represent valid bindings,
6070 by comparing the template parameter list of each template argument
6071 to the template parameter list of its corresponding template
6072 template parameter, in accordance with DR150. This
6073 routine can only be called after all template arguments have been
6074 deduced. It will return TRUE if all of the template template
6075 parameter bindings are okay, FALSE otherwise. */
6077 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6079 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6082 /* We're dealing with template parms in this process. */
6083 ++processing_template_decl
;
6085 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6087 for (i
= 0; i
< ntparms
; ++i
)
6089 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6090 tree targ
= TREE_VEC_ELT (targs
, i
);
6092 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6094 tree packed_args
= NULL_TREE
;
6097 if (ARGUMENT_PACK_P (targ
))
6099 /* Look inside the argument pack. */
6100 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6101 len
= TREE_VEC_LENGTH (packed_args
);
6104 for (idx
= 0; idx
< len
; ++idx
)
6106 tree targ_parms
= NULL_TREE
;
6109 /* Extract the next argument from the argument
6111 targ
= TREE_VEC_ELT (packed_args
, idx
);
6113 if (PACK_EXPANSION_P (targ
))
6114 /* Look at the pattern of the pack expansion. */
6115 targ
= PACK_EXPANSION_PATTERN (targ
);
6117 /* Extract the template parameters from the template
6119 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6120 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6121 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6122 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6124 /* Verify that we can coerce the template template
6125 parameters from the template argument to the template
6126 parameter. This requires an exact match. */
6128 && !coerce_template_template_parms
6129 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6144 --processing_template_decl
;
6148 /* Since type attributes aren't mangled, we need to strip them from
6149 template type arguments. */
6152 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6155 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6157 mv
= TYPE_MAIN_VARIANT (arg
);
6158 arg
= strip_typedefs (arg
);
6159 if (TYPE_ALIGN (arg
) != TYPE_ALIGN (mv
)
6160 || TYPE_ATTRIBUTES (arg
) != TYPE_ATTRIBUTES (mv
))
6162 if (complain
& tf_warning
)
6163 warning (0, "ignoring attributes on template argument %qT", arg
);
6164 arg
= build_aligned_type (arg
, TYPE_ALIGN (mv
));
6165 arg
= cp_build_type_attribute_variant (arg
, TYPE_ATTRIBUTES (mv
));
6170 /* Convert the indicated template ARG as necessary to match the
6171 indicated template PARM. Returns the converted ARG, or
6172 error_mark_node if the conversion was unsuccessful. Error and
6173 warning messages are issued under control of COMPLAIN. This
6174 conversion is for the Ith parameter in the parameter list. ARGS is
6175 the full set of template arguments deduced so far. */
6178 convert_template_argument (tree parm
,
6181 tsubst_flags_t complain
,
6187 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
6189 if (TREE_CODE (arg
) == TREE_LIST
6190 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
6192 /* The template argument was the name of some
6193 member function. That's usually
6194 invalid, but static members are OK. In any
6195 case, grab the underlying fields/functions
6196 and issue an error later if required. */
6197 orig_arg
= TREE_VALUE (arg
);
6198 TREE_TYPE (arg
) = unknown_type_node
;
6203 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
6204 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
6205 || requires_tmpl_type
);
6207 /* When determining whether an argument pack expansion is a template,
6208 look at the pattern. */
6209 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
6210 arg
= PACK_EXPANSION_PATTERN (arg
);
6212 /* Deal with an injected-class-name used as a template template arg. */
6213 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
6215 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
6216 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6218 if (cxx_dialect
>= cxx0x
)
6219 /* OK under DR 1004. */;
6220 else if (complain
& tf_warning_or_error
)
6221 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
6222 " used as template template argument", TYPE_NAME (arg
));
6223 else if (flag_pedantic_errors
)
6231 ((TREE_CODE (arg
) == TEMPLATE_DECL
6232 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
6233 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
6234 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6235 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
6238 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6239 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
6240 arg
= TYPE_STUB_DECL (arg
);
6242 is_type
= TYPE_P (arg
) || is_tmpl_type
;
6244 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
6245 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
6247 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
6249 if (complain
& tf_error
)
6250 error ("invalid use of destructor %qE as a type", orig_arg
);
6251 return error_mark_node
;
6254 permerror (input_location
,
6255 "to refer to a type member of a template parameter, "
6256 "use %<typename %E%>", orig_arg
);
6258 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
6259 TREE_OPERAND (arg
, 1),
6265 if (is_type
!= requires_type
)
6269 if (complain
& tf_error
)
6271 error ("type/value mismatch at argument %d in template "
6272 "parameter list for %qD",
6275 error (" expected a constant of type %qT, got %qT",
6277 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6278 else if (requires_tmpl_type
)
6279 error (" expected a class template, got %qE", orig_arg
);
6281 error (" expected a type, got %qE", orig_arg
);
6284 return error_mark_node
;
6286 if (is_tmpl_type
^ requires_tmpl_type
)
6288 if (in_decl
&& (complain
& tf_error
))
6290 error ("type/value mismatch at argument %d in template "
6291 "parameter list for %qD",
6294 error (" expected a type, got %qT", DECL_NAME (arg
));
6296 error (" expected a class template, got %qT", orig_arg
);
6298 return error_mark_node
;
6303 if (requires_tmpl_type
)
6305 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6307 else if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6308 /* The number of argument required is not known yet.
6309 Just accept it for now. */
6310 val
= TREE_TYPE (arg
);
6313 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6316 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6318 if (coerce_template_template_parms (parmparm
, argparm
,
6324 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6326 if (val
!= error_mark_node
)
6328 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6329 val
= TREE_TYPE (val
);
6330 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6331 val
= make_pack_expansion (val
);
6336 if (in_decl
&& (complain
& tf_error
))
6338 error ("type/value mismatch at argument %d in "
6339 "template parameter list for %qD",
6341 error (" expected a template of type %qD, got %qT",
6345 val
= error_mark_node
;
6351 /* We only form one instance of each template specialization.
6352 Therefore, if we use a non-canonical variant (i.e., a
6353 typedef), any future messages referring to the type will use
6354 the typedef, which is confusing if those future uses do not
6355 themselves also use the typedef. */
6357 val
= canonicalize_type_argument (val
, complain
);
6361 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6363 if (invalid_nontype_parm_type_p (t
, complain
))
6364 return error_mark_node
;
6366 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6368 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6372 /* Not sure if this is reachable, but it doesn't hurt
6374 error ("type mismatch in nontype parameter pack");
6375 val
= error_mark_node
;
6378 else if (!uses_template_parms (orig_arg
) && !uses_template_parms (t
))
6379 /* We used to call digest_init here. However, digest_init
6380 will report errors, which we don't want when complain
6381 is zero. More importantly, digest_init will try too
6382 hard to convert things: for example, `0' should not be
6383 converted to pointer type at this point according to
6384 the standard. Accepting this is not merely an
6385 extension, since deciding whether or not these
6386 conversions can occur is part of determining which
6387 function template to call, or whether a given explicit
6388 argument specification is valid. */
6389 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6391 val
= strip_typedefs_expr (orig_arg
);
6393 if (val
== NULL_TREE
)
6394 val
= error_mark_node
;
6395 else if (val
== error_mark_node
&& (complain
& tf_error
))
6396 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6398 if (TREE_CODE (val
) == SCOPE_REF
)
6400 /* Strip typedefs from the SCOPE_REF. */
6401 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
6402 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
6404 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6405 QUALIFIED_NAME_IS_TEMPLATE (val
));
6412 /* Coerces the remaining template arguments in INNER_ARGS (from
6413 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6414 Returns the coerced argument pack. PARM_IDX is the position of this
6415 parameter in the template parameter list. ARGS is the original
6416 template argument list. */
6418 coerce_template_parameter_pack (tree parms
,
6426 tsubst_flags_t complain
)
6428 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6429 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6432 tree packed_types
= NULL_TREE
;
6434 if (arg_idx
> nargs
)
6437 packed_args
= make_tree_vec (nargs
- arg_idx
);
6439 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
6440 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
6442 /* When the template parameter is a non-type template
6443 parameter pack whose type uses parameter packs, we need
6444 to look at each of the template arguments
6445 separately. Build a vector of the types for these
6446 non-type template parameters in PACKED_TYPES. */
6448 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
6449 packed_types
= tsubst_pack_expansion (expansion
, args
,
6452 if (packed_types
== error_mark_node
)
6453 return error_mark_node
;
6455 /* Check that we have the right number of arguments. */
6457 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
6458 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
6461 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
6462 error ("wrong number of template arguments (%d, should be %d)",
6463 nargs
, needed_parms
);
6464 return error_mark_node
;
6467 /* If we aren't able to check the actual arguments now
6468 (because they haven't been expanded yet), we can at least
6469 verify that all of the types used for the non-type
6470 template parameter pack are, in fact, valid for non-type
6471 template parameters. */
6473 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6475 int j
, len
= TREE_VEC_LENGTH (packed_types
);
6476 for (j
= 0; j
< len
; ++j
)
6478 tree t
= TREE_VEC_ELT (packed_types
, j
);
6479 if (invalid_nontype_parm_type_p (t
, complain
))
6480 return error_mark_node
;
6485 /* Convert the remaining arguments, which will be a part of the
6486 parameter pack "parm". */
6487 for (; arg_idx
< nargs
; ++arg_idx
)
6489 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6490 tree actual_parm
= TREE_VALUE (parm
);
6492 if (packed_types
&& !PACK_EXPANSION_P (arg
))
6494 /* When we have a vector of types (corresponding to the
6495 non-type template parameter pack that uses parameter
6496 packs in its type, as mention above), and the
6497 argument is not an expansion (which expands to a
6498 currently unknown number of arguments), clone the
6499 parm and give it the next type in PACKED_TYPES. */
6500 actual_parm
= copy_node (actual_parm
);
6501 TREE_TYPE (actual_parm
) =
6502 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
6505 if (arg
== error_mark_node
)
6507 if (complain
& tf_error
)
6508 error ("template argument %d is invalid", arg_idx
+ 1);
6511 arg
= convert_template_argument (actual_parm
,
6512 arg
, new_args
, complain
, parm_idx
,
6514 if (arg
== error_mark_node
)
6516 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
6519 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6520 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
6521 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
6524 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
6525 TREE_TYPE (argument_pack
)
6526 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
6527 TREE_CONSTANT (argument_pack
) = 1;
6530 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
6531 #ifdef ENABLE_CHECKING
6532 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
6533 TREE_VEC_LENGTH (packed_args
));
6535 return argument_pack
;
6538 /* Returns true if the template argument vector ARGS contains
6539 any pack expansions, false otherwise. */
6542 any_pack_expanson_args_p (tree args
)
6546 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
6547 if (PACK_EXPANSION_P (TREE_VEC_ELT (args
, i
)))
6552 /* Convert all template arguments to their appropriate types, and
6553 return a vector containing the innermost resulting template
6554 arguments. If any error occurs, return error_mark_node. Error and
6555 warning messages are issued under control of COMPLAIN.
6557 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6558 for arguments not specified in ARGS. Otherwise, if
6559 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6560 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6561 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6565 coerce_template_parms (tree parms
,
6568 tsubst_flags_t complain
,
6569 bool require_all_args
,
6570 bool use_default_args
)
6572 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
6575 tree new_inner_args
;
6576 int saved_unevaluated_operand
;
6577 int saved_inhibit_evaluation_warnings
;
6579 /* When used as a boolean value, indicates whether this is a
6580 variadic template parameter list. Since it's an int, we can also
6581 subtract it from nparms to get the number of non-variadic
6584 int post_variadic_parms
= 0;
6586 if (args
== error_mark_node
)
6587 return error_mark_node
;
6589 nparms
= TREE_VEC_LENGTH (parms
);
6591 /* Determine if there are any parameter packs. */
6592 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
6594 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
6596 ++post_variadic_parms
;
6597 if (template_parameter_pack_p (tparm
))
6601 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
6602 /* If there are no parameters that follow a parameter pack, we need to
6603 expand any argument packs so that we can deduce a parameter pack from
6604 some non-packed args followed by an argument pack, as in variadic85.C.
6605 If there are such parameters, we need to leave argument packs intact
6606 so the arguments are assigned properly. This can happen when dealing
6607 with a nested class inside a partial specialization of a class
6608 template, as in variadic92.C, or when deducing a template parameter pack
6609 from a sub-declarator, as in variadic114.C. */
6610 if (!post_variadic_parms
)
6611 inner_args
= expand_template_argument_pack (inner_args
);
6613 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6614 if ((nargs
> nparms
&& !variadic_p
)
6615 || (nargs
< nparms
- variadic_p
6617 && !any_pack_expanson_args_p (inner_args
)
6618 && (!use_default_args
6619 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
6620 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
6622 if (complain
& tf_error
)
6626 nparms
-= variadic_p
;
6627 error ("wrong number of template arguments "
6628 "(%d, should be %d or more)", nargs
, nparms
);
6631 error ("wrong number of template arguments "
6632 "(%d, should be %d)", nargs
, nparms
);
6635 error ("provided for %q+D", in_decl
);
6638 return error_mark_node
;
6641 /* We need to evaluate the template arguments, even though this
6642 template-id may be nested within a "sizeof". */
6643 saved_unevaluated_operand
= cp_unevaluated_operand
;
6644 cp_unevaluated_operand
= 0;
6645 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6646 c_inhibit_evaluation_warnings
= 0;
6647 new_inner_args
= make_tree_vec (nparms
);
6648 new_args
= add_outermost_template_args (args
, new_inner_args
);
6649 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
6654 /* Get the Ith template parameter. */
6655 parm
= TREE_VEC_ELT (parms
, parm_idx
);
6657 if (parm
== error_mark_node
)
6659 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
6663 /* Calculate the next argument. */
6664 if (arg_idx
< nargs
)
6665 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6669 if (template_parameter_pack_p (TREE_VALUE (parm
))
6670 && !(arg
&& ARGUMENT_PACK_P (arg
)))
6672 /* All remaining arguments will be placed in the
6673 template parameter pack PARM. */
6674 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
6675 inner_args
, arg_idx
,
6679 /* Store this argument. */
6680 if (arg
== error_mark_node
)
6682 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
6684 /* We are done with all of the arguments. */
6691 if (PACK_EXPANSION_P (arg
))
6693 /* We don't know how many args we have yet, just
6694 use the unconverted ones for now. */
6695 new_inner_args
= inner_args
;
6699 else if (require_all_args
)
6701 /* There must be a default arg in this case. */
6702 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
6704 /* The position of the first default template argument,
6705 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6707 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6708 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
, arg_idx
);
6713 if (arg
== error_mark_node
)
6715 if (complain
& tf_error
)
6716 error ("template argument %d is invalid", arg_idx
+ 1);
6719 /* This only occurs if there was an error in the template
6720 parameter list itself (which we would already have
6721 reported) that we are trying to recover from, e.g., a class
6722 template with a parameter list such as
6723 template<typename..., typename>. */
6726 arg
= convert_template_argument (TREE_VALUE (parm
),
6727 arg
, new_args
, complain
,
6730 if (arg
== error_mark_node
)
6732 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
6734 cp_unevaluated_operand
= saved_unevaluated_operand
;
6735 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
6738 return error_mark_node
;
6740 #ifdef ENABLE_CHECKING
6741 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6742 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6743 TREE_VEC_LENGTH (new_inner_args
));
6746 return new_inner_args
;
6749 /* Like coerce_template_parms. If PARMS represents all template
6750 parameters levels, this function returns a vector of vectors
6751 representing all the resulting argument levels. Note that in this
6752 case, only the innermost arguments are coerced because the
6753 outermost ones are supposed to have been coerced already.
6755 Otherwise, if PARMS represents only (the innermost) vector of
6756 parameters, this function returns a vector containing just the
6757 innermost resulting arguments. */
6760 coerce_innermost_template_parms (tree parms
,
6763 tsubst_flags_t complain
,
6764 bool require_all_args
,
6765 bool use_default_args
)
6767 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
6768 int args_depth
= TMPL_ARGS_DEPTH (args
);
6771 if (parms_depth
> 1)
6773 coerced_args
= make_tree_vec (parms_depth
);
6777 for (level
= parms
, cur_depth
= parms_depth
;
6778 parms_depth
> 0 && level
!= NULL_TREE
;
6779 level
= TREE_CHAIN (level
), --cur_depth
)
6782 if (cur_depth
== args_depth
)
6783 l
= coerce_template_parms (TREE_VALUE (level
),
6784 args
, in_decl
, complain
,
6788 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
6790 if (l
== error_mark_node
)
6791 return error_mark_node
;
6793 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
6797 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
6798 args
, in_decl
, complain
,
6801 return coerced_args
;
6804 /* Returns 1 if template args OT and NT are equivalent. */
6807 template_args_equal (tree ot
, tree nt
)
6811 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
6814 if (TREE_CODE (nt
) == TREE_VEC
)
6815 /* For member templates */
6816 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
6817 else if (PACK_EXPANSION_P (ot
))
6818 return (PACK_EXPANSION_P (nt
)
6819 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
6820 PACK_EXPANSION_PATTERN (nt
))
6821 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
6822 PACK_EXPANSION_EXTRA_ARGS (nt
)));
6823 else if (ARGUMENT_PACK_P (ot
))
6828 if (!ARGUMENT_PACK_P (nt
))
6831 opack
= ARGUMENT_PACK_ARGS (ot
);
6832 npack
= ARGUMENT_PACK_ARGS (nt
);
6833 len
= TREE_VEC_LENGTH (opack
);
6834 if (TREE_VEC_LENGTH (npack
) != len
)
6836 for (i
= 0; i
< len
; ++i
)
6837 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
6838 TREE_VEC_ELT (npack
, i
)))
6842 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
6844 /* We get here probably because we are in the middle of substituting
6845 into the pattern of a pack expansion. In that case the
6846 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6847 interested in. So we want to use the initial pack argument for
6849 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
6850 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
6851 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
6852 return template_args_equal (ot
, nt
);
6854 else if (TYPE_P (nt
))
6855 return TYPE_P (ot
) && same_type_p (ot
, nt
);
6856 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
6859 return cp_tree_equal (ot
, nt
);
6862 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6863 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6864 NEWARG_PTR with the offending arguments if they are non-NULL. */
6867 comp_template_args_with_info (tree oldargs
, tree newargs
,
6868 tree
*oldarg_ptr
, tree
*newarg_ptr
)
6872 if (oldargs
== newargs
)
6875 if (!oldargs
|| !newargs
)
6878 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
6881 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
6883 tree nt
= TREE_VEC_ELT (newargs
, i
);
6884 tree ot
= TREE_VEC_ELT (oldargs
, i
);
6886 if (! template_args_equal (ot
, nt
))
6888 if (oldarg_ptr
!= NULL
)
6890 if (newarg_ptr
!= NULL
)
6898 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6899 of template arguments. Returns 0 otherwise. */
6902 comp_template_args (tree oldargs
, tree newargs
)
6904 return comp_template_args_with_info (oldargs
, newargs
, NULL
, NULL
);
6908 add_pending_template (tree d
)
6910 tree ti
= (TYPE_P (d
)
6911 ? CLASSTYPE_TEMPLATE_INFO (d
)
6912 : DECL_TEMPLATE_INFO (d
));
6913 struct pending_template
*pt
;
6916 if (TI_PENDING_TEMPLATE_FLAG (ti
))
6919 /* We are called both from instantiate_decl, where we've already had a
6920 tinst_level pushed, and instantiate_template, where we haven't.
6922 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
6925 push_tinst_level (d
);
6927 pt
= ggc_alloc_pending_template ();
6929 pt
->tinst
= current_tinst_level
;
6930 if (last_pending_template
)
6931 last_pending_template
->next
= pt
;
6933 pending_templates
= pt
;
6935 last_pending_template
= pt
;
6937 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
6944 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6945 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6946 documentation for TEMPLATE_ID_EXPR. */
6949 lookup_template_function (tree fns
, tree arglist
)
6953 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
6954 return error_mark_node
;
6956 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
6958 if (!is_overloaded_fn (fns
) && TREE_CODE (fns
) != IDENTIFIER_NODE
)
6960 error ("%q#D is not a function template", fns
);
6961 return error_mark_node
;
6964 if (BASELINK_P (fns
))
6966 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
6968 BASELINK_FUNCTIONS (fns
),
6973 type
= TREE_TYPE (fns
);
6974 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
6975 type
= unknown_type_node
;
6977 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
6980 /* Within the scope of a template class S<T>, the name S gets bound
6981 (in build_self_reference) to a TYPE_DECL for the class, not a
6982 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6983 or one of its enclosing classes, and that type is a template,
6984 return the associated TEMPLATE_DECL. Otherwise, the original
6987 Also handle the case when DECL is a TREE_LIST of ambiguous
6988 injected-class-names from different bases. */
6991 maybe_get_template_decl_from_type_decl (tree decl
)
6993 if (decl
== NULL_TREE
)
6996 /* DR 176: A lookup that finds an injected-class-name (10.2
6997 [class.member.lookup]) can result in an ambiguity in certain cases
6998 (for example, if it is found in more than one base class). If all of
6999 the injected-class-names that are found refer to specializations of
7000 the same class template, and if the name is followed by a
7001 template-argument-list, the reference refers to the class template
7002 itself and not a specialization thereof, and is not ambiguous. */
7003 if (TREE_CODE (decl
) == TREE_LIST
)
7005 tree t
, tmpl
= NULL_TREE
;
7006 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
7008 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
7011 else if (tmpl
!= elt
)
7014 if (tmpl
&& t
== NULL_TREE
)
7020 return (decl
!= NULL_TREE
7021 && DECL_SELF_REFERENCE_P (decl
)
7022 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
7023 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
7026 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
7027 parameters, find the desired type.
7029 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7031 IN_DECL, if non-NULL, is the template declaration we are trying to
7034 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7035 the class we are looking up.
7037 Issue error and warning messages under control of COMPLAIN.
7039 If the template class is really a local class in a template
7040 function, then the FUNCTION_CONTEXT is the function in which it is
7043 ??? Note that this function is currently called *twice* for each
7044 template-id: the first time from the parser, while creating the
7045 incomplete type (finish_template_type), and the second type during the
7046 real instantiation (instantiate_template_class). This is surely something
7047 that we want to avoid. It also causes some problems with argument
7048 coercion (see convert_nontype_argument for more information on this). */
7051 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
7052 int entering_scope
, tsubst_flags_t complain
)
7054 tree templ
= NULL_TREE
, parmlist
;
7061 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
7063 tree value
= innermost_non_namespace_value (d1
);
7064 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
7069 push_decl_namespace (context
);
7070 templ
= lookup_name (d1
);
7071 templ
= maybe_get_template_decl_from_type_decl (templ
);
7073 pop_decl_namespace ();
7076 context
= DECL_CONTEXT (templ
);
7078 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
7080 tree type
= TREE_TYPE (d1
);
7082 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7083 an implicit typename for the second A. Deal with it. */
7084 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
7085 type
= TREE_TYPE (type
);
7087 if (CLASSTYPE_TEMPLATE_INFO (type
))
7089 templ
= CLASSTYPE_TI_TEMPLATE (type
);
7090 d1
= DECL_NAME (templ
);
7093 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
7094 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
7096 templ
= TYPE_TI_TEMPLATE (d1
);
7097 d1
= DECL_NAME (templ
);
7099 else if (TREE_CODE (d1
) == TEMPLATE_DECL
7100 && DECL_TEMPLATE_RESULT (d1
)
7101 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
7104 d1
= DECL_NAME (templ
);
7105 context
= DECL_CONTEXT (templ
);
7108 /* Issue an error message if we didn't find a template. */
7111 if (complain
& tf_error
)
7112 error ("%qT is not a template", d1
);
7113 return error_mark_node
;
7116 if (TREE_CODE (templ
) != TEMPLATE_DECL
7117 /* Make sure it's a user visible template, if it was named by
7119 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
7120 && !PRIMARY_TEMPLATE_P (templ
)))
7122 if (complain
& tf_error
)
7124 error ("non-template type %qT used as a template", d1
);
7126 error ("for template declaration %q+D", in_decl
);
7128 return error_mark_node
;
7131 complain
&= ~tf_user
;
7133 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
7135 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7136 template arguments */
7142 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7144 /* Consider an example where a template template parameter declared as
7146 template <class T, class U = std::allocator<T> > class TT
7148 The template parameter level of T and U are one level larger than
7149 of TT. To proper process the default argument of U, say when an
7150 instantiation `TT<int>' is seen, we need to build the full
7151 arguments containing {int} as the innermost level. Outer levels,
7152 available when not appearing as default template argument, can be
7153 obtained from the arguments of the enclosing template.
7155 Suppose that TT is later substituted with std::vector. The above
7156 instantiation is `TT<int, std::allocator<T> >' with TT at
7157 level 1, and T at level 2, while the template arguments at level 1
7158 becomes {std::vector} and the inner level 2 is {int}. */
7160 outer
= DECL_CONTEXT (templ
);
7162 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7163 else if (current_template_parms
)
7164 /* This is an argument of the current template, so we haven't set
7165 DECL_CONTEXT yet. */
7166 outer
= current_template_args ();
7169 arglist
= add_to_template_args (outer
, arglist
);
7171 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
7173 /*require_all_args=*/true,
7174 /*use_default_args=*/true);
7175 if (arglist2
== error_mark_node
7176 || (!uses_template_parms (arglist2
)
7177 && check_instantiated_args (templ
, arglist2
, complain
)))
7178 return error_mark_node
;
7180 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
7185 tree template_type
= TREE_TYPE (templ
);
7188 tree found
= NULL_TREE
;
7191 int is_dependent_type
;
7192 int use_partial_inst_tmpl
= false;
7194 if (template_type
== error_mark_node
)
7195 /* An error occured while building the template TEMPL, and a
7196 diagnostic has most certainly been emitted for that
7197 already. Let's propagate that error. */
7198 return error_mark_node
;
7200 gen_tmpl
= most_general_template (templ
);
7201 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7202 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
7203 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7205 if (arg_depth
== 1 && parm_depth
> 1)
7207 /* We've been given an incomplete set of template arguments.
7210 template <class T> struct S1 {
7211 template <class U> struct S2 {};
7212 template <class U> struct S2<U*> {};
7215 we will be called with an ARGLIST of `U*', but the
7216 TEMPLATE will be `template <class T> template
7217 <class U> struct S1<T>::S2'. We must fill in the missing
7220 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
7222 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7225 /* Now we should have enough arguments. */
7226 gcc_assert (parm_depth
== arg_depth
);
7228 /* From here on, we're only interested in the most general
7231 /* Calculate the BOUND_ARGS. These will be the args that are
7232 actually tsubst'd into the definition to create the
7236 /* We have multiple levels of arguments to coerce, at once. */
7238 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
7240 tree bound_args
= make_tree_vec (parm_depth
);
7242 for (i
= saved_depth
,
7243 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7244 i
> 0 && t
!= NULL_TREE
;
7245 --i
, t
= TREE_CHAIN (t
))
7248 if (i
== saved_depth
)
7249 a
= coerce_template_parms (TREE_VALUE (t
),
7252 /*require_all_args=*/true,
7253 /*use_default_args=*/true);
7255 /* Outer levels should have already been coerced. */
7256 a
= TMPL_ARGS_LEVEL (arglist
, i
);
7258 /* Don't process further if one of the levels fails. */
7259 if (a
== error_mark_node
)
7261 /* Restore the ARGLIST to its full size. */
7262 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7263 return error_mark_node
;
7266 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
7268 /* We temporarily reduce the length of the ARGLIST so
7269 that coerce_template_parms will see only the arguments
7270 corresponding to the template parameters it is
7272 TREE_VEC_LENGTH (arglist
)--;
7275 /* Restore the ARGLIST to its full size. */
7276 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7278 arglist
= bound_args
;
7282 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
7283 INNERMOST_TEMPLATE_ARGS (arglist
),
7286 /*require_all_args=*/true,
7287 /*use_default_args=*/true);
7289 if (arglist
== error_mark_node
)
7290 /* We were unable to bind the arguments. */
7291 return error_mark_node
;
7293 /* In the scope of a template class, explicit references to the
7294 template class refer to the type of the template, not any
7295 instantiation of it. For example, in:
7297 template <class T> class C { void f(C<T>); }
7299 the `C<T>' is just the same as `C'. Outside of the
7300 class, however, such a reference is an instantiation. */
7302 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
7303 || currently_open_class (template_type
))
7304 /* comp_template_args is expensive, check it last. */
7305 && comp_template_args (TYPE_TI_ARGS (template_type
),
7307 return template_type
;
7309 /* If we already have this specialization, return it. */
7310 elt
.tmpl
= gen_tmpl
;
7312 hash
= hash_specialization (&elt
);
7313 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
7319 is_dependent_type
= uses_template_parms (arglist
);
7321 /* If the deduced arguments are invalid, then the binding
7323 if (!is_dependent_type
7324 && check_instantiated_args (gen_tmpl
,
7325 INNERMOST_TEMPLATE_ARGS (arglist
),
7327 return error_mark_node
;
7329 if (!is_dependent_type
7330 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
7331 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
7332 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
7334 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
7335 DECL_NAME (gen_tmpl
),
7336 /*tag_scope=*/ts_global
);
7340 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
7342 if (context
== error_mark_node
)
7343 return error_mark_node
;
7346 context
= global_namespace
;
7348 /* Create the type. */
7349 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
7351 if (!is_dependent_type
)
7353 set_current_access_from_decl (TYPE_NAME (template_type
));
7354 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
7355 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
7356 arglist
, complain
, in_decl
),
7357 SCOPED_ENUM_P (template_type
), NULL
);
7361 /* We don't want to call start_enum for this type, since
7362 the values for the enumeration constants may involve
7363 template parameters. And, no one should be interested
7364 in the enumeration constants for such a type. */
7365 t
= cxx_make_type (ENUMERAL_TYPE
);
7366 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
7368 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
7369 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
7370 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7372 else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7374 /* The user referred to a specialization of an alias
7375 template represented by GEN_TMPL.
7377 [temp.alias]/2 says:
7379 When a template-id refers to the specialization of an
7380 alias template, it is equivalent to the associated
7381 type obtained by substitution of its
7382 template-arguments for the template-parameters in the
7383 type-id of the alias template. */
7385 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
7386 /* Note that the call above (by indirectly calling
7387 register_specialization in tsubst_decl) registers the
7388 TYPE_DECL representing the specialization of the alias
7389 template. So next time someone substitutes ARGLIST for
7390 the template parms into the alias template (GEN_TMPL),
7391 she'll get that TYPE_DECL back. */
7393 if (t
== error_mark_node
)
7396 else if (CLASS_TYPE_P (template_type
))
7398 t
= make_class_type (TREE_CODE (template_type
));
7399 CLASSTYPE_DECLARED_CLASS (t
)
7400 = CLASSTYPE_DECLARED_CLASS (template_type
);
7401 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7402 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7404 /* A local class. Make sure the decl gets registered properly. */
7405 if (context
== current_function_decl
)
7406 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7408 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7409 /* This instantiation is another name for the primary
7410 template type. Set the TYPE_CANONICAL field
7412 TYPE_CANONICAL (t
) = template_type
;
7413 else if (any_template_arguments_need_structural_equality_p (arglist
))
7414 /* Some of the template arguments require structural
7415 equality testing, so this template class requires
7416 structural equality testing. */
7417 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7422 /* If we called start_enum or pushtag above, this information
7423 will already be set up. */
7426 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
7428 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
7429 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
7430 DECL_SOURCE_LOCATION (type_decl
)
7431 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
7434 type_decl
= TYPE_NAME (t
);
7436 if (CLASS_TYPE_P (template_type
))
7438 TREE_PRIVATE (type_decl
)
7439 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
7440 TREE_PROTECTED (type_decl
)
7441 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
7442 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
7444 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
7445 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
7449 /* Let's consider the explicit specialization of a member
7450 of a class template specialization that is implicitely instantiated,
7455 template<class U> struct M {}; //#0
7460 struct S<int>::M<char> //#1
7464 [temp.expl.spec]/4 says this is valid.
7466 In this case, when we write:
7469 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7472 When we encounter #1, we want to store the partial instantiation
7473 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
7475 For all cases other than this "explicit specialization of member of a
7476 class template", we just want to store the most general template into
7477 the CLASSTYPE_TI_TEMPLATE of M.
7479 This case of "explicit specialization of member of a class template"
7481 1/ the enclosing class is an instantiation of, and therefore not
7482 the same as, the context of the most general template, and
7483 2/ we aren't looking at the partial instantiation itself, i.e.
7484 the innermost arguments are not the same as the innermost parms of
7485 the most general template.
7487 So it's only when 1/ and 2/ happens that we want to use the partial
7488 instantiation of the member template in lieu of its most general
7491 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
7492 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
7493 /* the enclosing class must be an instantiation... */
7494 && CLASS_TYPE_P (context
)
7495 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
7497 tree partial_inst_args
;
7498 TREE_VEC_LENGTH (arglist
)--;
7499 ++processing_template_decl
;
7501 tsubst (INNERMOST_TEMPLATE_ARGS
7502 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
7503 arglist
, complain
, NULL_TREE
);
7504 --processing_template_decl
;
7505 TREE_VEC_LENGTH (arglist
)++;
7506 use_partial_inst_tmpl
=
7507 /*...and we must not be looking at the partial instantiation
7509 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
7513 if (!use_partial_inst_tmpl
)
7514 /* This case is easy; there are no member templates involved. */
7518 /* This is a full instantiation of a member template. Find
7519 the partial instantiation of which this is an instance. */
7521 /* Temporarily reduce by one the number of levels in the ARGLIST
7522 so as to avoid comparing the last set of arguments. */
7523 TREE_VEC_LENGTH (arglist
)--;
7524 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
7525 TREE_VEC_LENGTH (arglist
)++;
7526 /* FOUND is either a proper class type, or an alias
7527 template specialization. In the later case, it's a
7528 TYPE_DECL, resulting from the substituting of arguments
7529 for parameters in the TYPE_DECL of the alias template
7530 done earlier. So be careful while getting the template
7532 found
= TREE_CODE (found
) == TYPE_DECL
7533 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
7534 : CLASSTYPE_TI_TEMPLATE (found
);
7537 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
7540 slot
= htab_find_slot_with_hash (type_specializations
,
7541 &elt
, hash
, INSERT
);
7542 entry
= ggc_alloc_spec_entry ();
7546 /* Note this use of the partial instantiation so we can check it
7547 later in maybe_process_partial_specialization. */
7548 DECL_TEMPLATE_INSTANTIATIONS (templ
)
7549 = tree_cons (arglist
, t
,
7550 DECL_TEMPLATE_INSTANTIATIONS (templ
));
7552 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
)
7553 /* Now that the type has been registered on the instantiations
7554 list, we set up the enumerators. Because the enumeration
7555 constants may involve the enumeration type itself, we make
7556 sure to register the type first, and then create the
7557 constants. That way, doing tsubst_expr for the enumeration
7558 constants won't result in recursive calls here; we'll find
7559 the instantiation and exit above. */
7560 tsubst_enum (template_type
, t
, arglist
);
7562 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
7563 /* If the type makes use of template parameters, the
7564 code that generates debugging information will crash. */
7565 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
7567 /* Possibly limit visibility based on template args. */
7568 TREE_PUBLIC (type_decl
) = 1;
7569 determine_visibility (type_decl
);
7575 /* Wrapper for lookup_template_class_1. */
7578 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
7579 int entering_scope
, tsubst_flags_t complain
)
7582 timevar_push (TV_TEMPLATE_INST
);
7583 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
7584 entering_scope
, complain
);
7585 timevar_pop (TV_TEMPLATE_INST
);
7593 /* True when we should also visit template parameters that occur in
7594 non-deduced contexts. */
7595 bool include_nondeduced_p
;
7596 struct pointer_set_t
*visited
;
7599 /* Called from for_each_template_parm via walk_tree. */
7602 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
7605 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
7606 tree_fn_t fn
= pfd
->fn
;
7607 void *data
= pfd
->data
;
7610 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
7611 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
7612 pfd
->include_nondeduced_p
))
7613 return error_mark_node
;
7615 switch (TREE_CODE (t
))
7618 if (TYPE_PTRMEMFUNC_P (t
))
7624 if (!TYPE_TEMPLATE_INFO (t
))
7626 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
7627 fn
, data
, pfd
->visited
,
7628 pfd
->include_nondeduced_p
))
7629 return error_mark_node
;
7633 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
7634 fn
, data
, pfd
->visited
,
7635 pfd
->include_nondeduced_p
)
7636 || for_each_template_parm (TYPE_MAX_VALUE (t
),
7637 fn
, data
, pfd
->visited
,
7638 pfd
->include_nondeduced_p
))
7639 return error_mark_node
;
7643 /* Since we're not going to walk subtrees, we have to do this
7645 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
7646 pfd
->visited
, pfd
->include_nondeduced_p
))
7647 return error_mark_node
;
7651 /* Check the return type. */
7652 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7653 pfd
->include_nondeduced_p
))
7654 return error_mark_node
;
7656 /* Check the parameter types. Since default arguments are not
7657 instantiated until they are needed, the TYPE_ARG_TYPES may
7658 contain expressions that involve template parameters. But,
7659 no-one should be looking at them yet. And, once they're
7660 instantiated, they don't contain template parameters, so
7661 there's no point in looking at them then, either. */
7665 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
7666 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
7667 pfd
->visited
, pfd
->include_nondeduced_p
))
7668 return error_mark_node
;
7670 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7671 want walk_tree walking into them itself. */
7677 case UNDERLYING_TYPE
:
7678 if (pfd
->include_nondeduced_p
7679 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
7681 pfd
->include_nondeduced_p
))
7682 return error_mark_node
;
7687 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
7688 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
7689 pfd
->visited
, pfd
->include_nondeduced_p
))
7690 return error_mark_node
;
7695 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
7696 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
7697 pfd
->visited
, pfd
->include_nondeduced_p
))
7698 return error_mark_node
;
7699 if (DECL_CONTEXT (t
)
7700 && pfd
->include_nondeduced_p
7701 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
7702 pfd
->visited
, pfd
->include_nondeduced_p
))
7703 return error_mark_node
;
7706 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7707 /* Record template parameters such as `T' inside `TT<T>'. */
7708 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
7709 pfd
->include_nondeduced_p
))
7710 return error_mark_node
;
7713 case TEMPLATE_TEMPLATE_PARM
:
7714 case TEMPLATE_TYPE_PARM
:
7715 case TEMPLATE_PARM_INDEX
:
7716 if (fn
&& (*fn
)(t
, data
))
7717 return error_mark_node
;
7719 return error_mark_node
;
7723 /* A template template parameter is encountered. */
7724 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
7725 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7726 pfd
->include_nondeduced_p
))
7727 return error_mark_node
;
7729 /* Already substituted template template parameter */
7735 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
7737 pfd
->include_nondeduced_p
))
7738 return error_mark_node
;
7742 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
7743 && pfd
->include_nondeduced_p
7744 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7745 (TREE_TYPE (t
)), fn
, data
,
7746 pfd
->visited
, pfd
->include_nondeduced_p
))
7747 return error_mark_node
;
7752 /* If there's no type, then this thing must be some expression
7753 involving template parameters. */
7754 if (!fn
&& !TREE_TYPE (t
))
7755 return error_mark_node
;
7760 case IMPLICIT_CONV_EXPR
:
7761 case REINTERPRET_CAST_EXPR
:
7762 case CONST_CAST_EXPR
:
7763 case STATIC_CAST_EXPR
:
7764 case DYNAMIC_CAST_EXPR
:
7768 case PSEUDO_DTOR_EXPR
:
7770 return error_mark_node
;
7777 /* We didn't find any template parameters we liked. */
7781 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7782 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7783 call FN with the parameter and the DATA.
7784 If FN returns nonzero, the iteration is terminated, and
7785 for_each_template_parm returns 1. Otherwise, the iteration
7786 continues. If FN never returns a nonzero value, the value
7787 returned by for_each_template_parm is 0. If FN is NULL, it is
7788 considered to be the function which always returns 1.
7790 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7791 parameters that occur in non-deduced contexts. When false, only
7792 visits those template parameters that can be deduced. */
7795 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
7796 struct pointer_set_t
*visited
,
7797 bool include_nondeduced_p
)
7799 struct pair_fn_data pfd
;
7805 pfd
.include_nondeduced_p
= include_nondeduced_p
;
7807 /* Walk the tree. (Conceptually, we would like to walk without
7808 duplicates, but for_each_template_parm_r recursively calls
7809 for_each_template_parm, so we would need to reorganize a fair
7810 bit to use walk_tree_without_duplicates, so we keep our own
7813 pfd
.visited
= visited
;
7815 pfd
.visited
= pointer_set_create ();
7816 result
= cp_walk_tree (&t
,
7817 for_each_template_parm_r
,
7819 pfd
.visited
) != NULL_TREE
;
7824 pointer_set_destroy (pfd
.visited
);
7831 /* Returns true if T depends on any template parameter. */
7834 uses_template_parms (tree t
)
7837 int saved_processing_template_decl
;
7839 saved_processing_template_decl
= processing_template_decl
;
7840 if (!saved_processing_template_decl
)
7841 processing_template_decl
= 1;
7843 dependent_p
= dependent_type_p (t
);
7844 else if (TREE_CODE (t
) == TREE_VEC
)
7845 dependent_p
= any_dependent_template_arguments_p (t
);
7846 else if (TREE_CODE (t
) == TREE_LIST
)
7847 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
7848 || uses_template_parms (TREE_CHAIN (t
)));
7849 else if (TREE_CODE (t
) == TYPE_DECL
)
7850 dependent_p
= dependent_type_p (TREE_TYPE (t
));
7853 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
7854 || TREE_CODE (t
) == OVERLOAD
7856 || TREE_CODE (t
) == IDENTIFIER_NODE
7857 || TREE_CODE (t
) == TRAIT_EXPR
7858 || TREE_CODE (t
) == CONSTRUCTOR
7859 || CONSTANT_CLASS_P (t
))
7860 dependent_p
= (type_dependent_expression_p (t
)
7861 || value_dependent_expression_p (t
));
7864 gcc_assert (t
== error_mark_node
);
7865 dependent_p
= false;
7868 processing_template_decl
= saved_processing_template_decl
;
7873 /* Returns true iff current_function_decl is an incompletely instantiated
7874 template. Useful instead of processing_template_decl because the latter
7875 is set to 0 during fold_non_dependent_expr. */
7878 in_template_function (void)
7880 tree fn
= current_function_decl
;
7882 ++processing_template_decl
;
7883 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
7884 && DECL_TEMPLATE_INFO (fn
)
7885 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
7886 --processing_template_decl
;
7890 /* Returns true if T depends on any template parameter with level LEVEL. */
7893 uses_template_parms_level (tree t
, int level
)
7895 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
7896 /*include_nondeduced_p=*/true);
7899 /* Returns TRUE iff INST is an instantiation we don't need to do in an
7900 ill-formed translation unit, i.e. a variable or function that isn't
7901 usable in a constant expression. */
7904 neglectable_inst_p (tree d
)
7907 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
7908 : decl_maybe_constant_var_p (d
)));
7911 /* Returns TRUE iff we should refuse to instantiate DECL because it's
7912 neglectable and instantiated from within an erroneous instantiation. */
7915 limit_bad_template_recursion (tree decl
)
7917 struct tinst_level
*lev
= current_tinst_level
;
7918 int errs
= errorcount
+ sorrycount
;
7919 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
7922 for (; lev
; lev
= lev
->next
)
7923 if (neglectable_inst_p (lev
->decl
))
7926 return (lev
&& errs
> lev
->errors
);
7929 static int tinst_depth
;
7930 extern int max_tinst_depth
;
7933 static GTY(()) struct tinst_level
*last_error_tinst_level
;
7935 /* We're starting to instantiate D; record the template instantiation context
7936 for diagnostics and to restore it later. */
7939 push_tinst_level (tree d
)
7941 struct tinst_level
*new_level
;
7943 if (tinst_depth
>= max_tinst_depth
)
7945 last_error_tinst_level
= current_tinst_level
;
7946 if (TREE_CODE (d
) == TREE_LIST
)
7947 error ("template instantiation depth exceeds maximum of %d (use "
7948 "-ftemplate-depth= to increase the maximum) substituting %qS",
7949 max_tinst_depth
, d
);
7951 error ("template instantiation depth exceeds maximum of %d (use "
7952 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7953 max_tinst_depth
, d
);
7955 print_instantiation_context ();
7960 /* If the current instantiation caused problems, don't let it instantiate
7961 anything else. Do allow deduction substitution and decls usable in
7962 constant expressions. */
7963 if (limit_bad_template_recursion (d
))
7966 new_level
= ggc_alloc_tinst_level ();
7967 new_level
->decl
= d
;
7968 new_level
->locus
= input_location
;
7969 new_level
->errors
= errorcount
+sorrycount
;
7970 new_level
->in_system_header_p
= in_system_header
;
7971 new_level
->next
= current_tinst_level
;
7972 current_tinst_level
= new_level
;
7975 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
7976 depth_reached
= tinst_depth
;
7981 /* We're done instantiating this template; return to the instantiation
7985 pop_tinst_level (void)
7987 /* Restore the filename and line number stashed away when we started
7988 this instantiation. */
7989 input_location
= current_tinst_level
->locus
;
7990 current_tinst_level
= current_tinst_level
->next
;
7994 /* We're instantiating a deferred template; restore the template
7995 instantiation context in which the instantiation was requested, which
7996 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7999 reopen_tinst_level (struct tinst_level
*level
)
8001 struct tinst_level
*t
;
8004 for (t
= level
; t
; t
= t
->next
)
8007 current_tinst_level
= level
;
8009 if (current_tinst_level
)
8010 current_tinst_level
->errors
= errorcount
+sorrycount
;
8014 /* Returns the TINST_LEVEL which gives the original instantiation
8017 struct tinst_level
*
8018 outermost_tinst_level (void)
8020 struct tinst_level
*level
= current_tinst_level
;
8023 level
= level
->next
;
8027 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8028 vector of template arguments, as for tsubst.
8030 Returns an appropriate tsubst'd friend declaration. */
8033 tsubst_friend_function (tree decl
, tree args
)
8037 if (TREE_CODE (decl
) == FUNCTION_DECL
8038 && DECL_TEMPLATE_INSTANTIATION (decl
)
8039 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
8040 /* This was a friend declared with an explicit template
8041 argument list, e.g.:
8045 to indicate that f was a template instantiation, not a new
8046 function declaration. Now, we have to figure out what
8047 instantiation of what template. */
8049 tree template_id
, arglist
, fns
;
8052 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
8054 /* Friend functions are looked up in the containing namespace scope.
8055 We must enter that scope, to avoid finding member functions of the
8056 current class with same name. */
8057 push_nested_namespace (ns
);
8058 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
8059 tf_warning_or_error
, NULL_TREE
,
8060 /*integral_constant_expression_p=*/false);
8061 pop_nested_namespace (ns
);
8062 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
8063 tf_warning_or_error
, NULL_TREE
);
8064 template_id
= lookup_template_function (fns
, arglist
);
8066 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8067 tmpl
= determine_specialization (template_id
, new_friend
,
8069 /*need_member_template=*/0,
8070 TREE_VEC_LENGTH (args
),
8072 return instantiate_template (tmpl
, new_args
, tf_error
);
8075 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8077 /* The NEW_FRIEND will look like an instantiation, to the
8078 compiler, but is not an instantiation from the point of view of
8079 the language. For example, we might have had:
8081 template <class T> struct S {
8082 template <class U> friend void f(T, U);
8085 Then, in S<int>, template <class U> void f(int, U) is not an
8086 instantiation of anything. */
8087 if (new_friend
== error_mark_node
)
8088 return error_mark_node
;
8090 DECL_USE_TEMPLATE (new_friend
) = 0;
8091 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8093 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
8094 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
8095 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
8098 /* The mangled name for the NEW_FRIEND is incorrect. The function
8099 is not a template instantiation and should not be mangled like
8100 one. Therefore, we forget the mangling here; we'll recompute it
8101 later if we need it. */
8102 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
8104 SET_DECL_RTL (new_friend
, NULL
);
8105 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
8108 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
8111 tree new_friend_template_info
;
8112 tree new_friend_result_template_info
;
8114 int new_friend_is_defn
;
8116 /* We must save some information from NEW_FRIEND before calling
8117 duplicate decls since that function will free NEW_FRIEND if
8119 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
8120 new_friend_is_defn
=
8121 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8122 (template_for_substitution (new_friend
)))
8124 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
8126 /* This declaration is a `primary' template. */
8127 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
8129 new_friend_result_template_info
8130 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
8133 new_friend_result_template_info
= NULL_TREE
;
8135 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8136 if (new_friend_is_defn
)
8137 DECL_INITIAL (new_friend
) = error_mark_node
;
8139 /* Inside pushdecl_namespace_level, we will push into the
8140 current namespace. However, the friend function should go
8141 into the namespace of the template. */
8142 ns
= decl_namespace_context (new_friend
);
8143 push_nested_namespace (ns
);
8144 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
8145 pop_nested_namespace (ns
);
8147 if (old_decl
== error_mark_node
)
8148 return error_mark_node
;
8150 if (old_decl
!= new_friend
)
8152 /* This new friend declaration matched an existing
8153 declaration. For example, given:
8155 template <class T> void f(T);
8156 template <class U> class C {
8157 template <class T> friend void f(T) {}
8160 the friend declaration actually provides the definition
8161 of `f', once C has been instantiated for some type. So,
8162 old_decl will be the out-of-class template declaration,
8163 while new_friend is the in-class definition.
8165 But, if `f' was called before this point, the
8166 instantiation of `f' will have DECL_TI_ARGS corresponding
8167 to `T' but not to `U', references to which might appear
8168 in the definition of `f'. Previously, the most general
8169 template for an instantiation of `f' was the out-of-class
8170 version; now it is the in-class version. Therefore, we
8171 run through all specialization of `f', adding to their
8172 DECL_TI_ARGS appropriately. In particular, they need a
8173 new set of outer arguments, corresponding to the
8174 arguments for this class instantiation.
8176 The same situation can arise with something like this:
8179 template <class T> class C {
8183 when `C<int>' is instantiated. Now, `f(int)' is defined
8186 if (!new_friend_is_defn
)
8187 /* On the other hand, if the in-class declaration does
8188 *not* provide a definition, then we don't want to alter
8189 existing definitions. We can just leave everything
8194 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
8195 tree new_args
= TI_ARGS (new_friend_template_info
);
8197 /* Overwrite whatever template info was there before, if
8198 any, with the new template information pertaining to
8200 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
8202 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
8204 /* We should have called reregister_specialization in
8206 gcc_assert (retrieve_specialization (new_template
,
8210 /* Instantiate it if the global has already been used. */
8211 if (DECL_ODR_USED (old_decl
))
8212 instantiate_decl (old_decl
, /*defer_ok=*/true,
8213 /*expl_inst_class_mem_p=*/false);
8219 /* Indicate that the old function template is a partial
8221 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
8222 = new_friend_result_template_info
;
8224 gcc_assert (new_template
8225 == most_general_template (new_template
));
8226 gcc_assert (new_template
!= old_decl
);
8228 /* Reassign any specializations already in the hash table
8229 to the new more general template, and add the
8230 additional template args. */
8231 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
8235 tree spec
= TREE_VALUE (t
);
8238 elt
.tmpl
= old_decl
;
8239 elt
.args
= DECL_TI_ARGS (spec
);
8240 elt
.spec
= NULL_TREE
;
8242 htab_remove_elt (decl_specializations
, &elt
);
8245 = add_outermost_template_args (new_args
,
8246 DECL_TI_ARGS (spec
));
8248 register_specialization
8249 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
8252 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
8256 /* The information from NEW_FRIEND has been merged into OLD_DECL
8257 by duplicate_decls. */
8258 new_friend
= old_decl
;
8263 tree context
= DECL_CONTEXT (new_friend
);
8267 template <class T> class C {
8268 template <class U> friend void C1<U>::f (); // case 1
8269 friend void C2<T>::f (); // case 2
8271 we only need to make sure CONTEXT is a complete type for
8272 case 2. To distinguish between the two cases, we note that
8273 CONTEXT of case 1 remains dependent type after tsubst while
8274 this isn't true for case 2. */
8275 ++processing_template_decl
;
8276 dependent_p
= dependent_type_p (context
);
8277 --processing_template_decl
;
8280 && !complete_type_or_else (context
, NULL_TREE
))
8281 return error_mark_node
;
8283 if (COMPLETE_TYPE_P (context
))
8285 /* Check to see that the declaration is really present, and,
8286 possibly obtain an improved declaration. */
8287 tree fn
= check_classfn (context
,
8288 new_friend
, NULL_TREE
);
8298 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8299 template arguments, as for tsubst.
8301 Returns an appropriate tsubst'd friend type or error_mark_node on
8305 tsubst_friend_class (tree friend_tmpl
, tree args
)
8311 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
8313 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
8314 return TREE_TYPE (t
);
8317 context
= CP_DECL_CONTEXT (friend_tmpl
);
8319 if (context
!= global_namespace
)
8321 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8322 push_nested_namespace (context
);
8324 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
8327 /* Look for a class template declaration. We look for hidden names
8328 because two friend declarations of the same template are the
8329 same. For example, in:
8332 template <typename> friend class F;
8334 template <typename> struct B {
8335 template <typename> friend class F;
8338 both F templates are the same. */
8339 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
8340 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
8342 /* But, if we don't find one, it might be because we're in a
8343 situation like this:
8351 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8352 for `S<int>', not the TEMPLATE_DECL. */
8353 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
8355 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
8356 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
8359 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
8361 /* The friend template has already been declared. Just
8362 check to see that the declarations match, and install any new
8363 default parameters. We must tsubst the default parameters,
8364 of course. We only need the innermost template parameters
8365 because that is all that redeclare_class_template will look
8367 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
8368 > TMPL_ARGS_DEPTH (args
))
8371 location_t saved_input_location
;
8372 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
8373 args
, tf_warning_or_error
);
8375 saved_input_location
= input_location
;
8376 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
8377 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
8378 input_location
= saved_input_location
;
8382 friend_type
= TREE_TYPE (tmpl
);
8386 /* The friend template has not already been declared. In this
8387 case, the instantiation of the template class will cause the
8388 injection of this template into the global scope. */
8389 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
8390 if (tmpl
== error_mark_node
)
8391 return error_mark_node
;
8393 /* The new TMPL is not an instantiation of anything, so we
8394 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8395 the new type because that is supposed to be the corresponding
8396 template decl, i.e., TMPL. */
8397 DECL_USE_TEMPLATE (tmpl
) = 0;
8398 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
8399 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
8400 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
8401 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
8403 /* Inject this template into the global scope. */
8404 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
8407 if (context
!= global_namespace
)
8409 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8410 pop_nested_namespace (context
);
8412 pop_nested_class ();
8418 /* Returns zero if TYPE cannot be completed later due to circularity.
8419 Otherwise returns one. */
8422 can_complete_type_without_circularity (tree type
)
8424 if (type
== NULL_TREE
|| type
== error_mark_node
)
8426 else if (COMPLETE_TYPE_P (type
))
8428 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
8429 return can_complete_type_without_circularity (TREE_TYPE (type
));
8430 else if (CLASS_TYPE_P (type
)
8431 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
8437 /* Apply any attributes which had to be deferred until instantiation
8438 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8439 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8442 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
8443 tree args
, tsubst_flags_t complain
, tree in_decl
)
8445 tree last_dep
= NULL_TREE
;
8449 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
8450 if (ATTR_IS_DEPENDENT (t
))
8453 attributes
= copy_list (attributes
);
8457 if (DECL_P (*decl_p
))
8459 if (TREE_TYPE (*decl_p
) == error_mark_node
)
8461 p
= &DECL_ATTRIBUTES (*decl_p
);
8464 p
= &TYPE_ATTRIBUTES (*decl_p
);
8468 tree late_attrs
= NULL_TREE
;
8469 tree
*q
= &late_attrs
;
8471 for (*p
= attributes
; *p
; )
8474 if (ATTR_IS_DEPENDENT (t
))
8476 *p
= TREE_CHAIN (t
);
8477 TREE_CHAIN (t
) = NULL_TREE
;
8478 /* If the first attribute argument is an identifier, don't
8479 pass it through tsubst. Attributes like mode, format,
8480 cleanup and several target specific attributes expect it
8483 && TREE_CODE (TREE_VALUE (t
)) == TREE_LIST
8484 && TREE_VALUE (TREE_VALUE (t
))
8485 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t
)))
8486 == IDENTIFIER_NODE
))
8489 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
8491 /*integral_constant_expression_p=*/false);
8492 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
8494 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
8499 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
8500 /*integral_constant_expression_p=*/false);
8502 q
= &TREE_CHAIN (t
);
8505 p
= &TREE_CHAIN (t
);
8508 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
8512 /* Perform (or defer) access check for typedefs that were referenced
8513 from within the template TMPL code.
8514 This is a subroutine of instantiate_decl and instantiate_class_template.
8515 TMPL is the template to consider and TARGS is the list of arguments of
8519 perform_typedefs_access_check (tree tmpl
, tree targs
)
8521 location_t saved_location
;
8523 qualified_typedef_usage_t
*iter
;
8526 || (!CLASS_TYPE_P (tmpl
)
8527 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
8530 saved_location
= input_location
;
8531 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
8533 tree type_decl
= iter
->typedef_decl
;
8534 tree type_scope
= iter
->context
;
8536 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
8539 if (uses_template_parms (type_decl
))
8540 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
8541 if (uses_template_parms (type_scope
))
8542 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
8544 /* Make access check error messages point to the location
8545 of the use of the typedef. */
8546 input_location
= iter
->locus
;
8547 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
8548 type_decl
, type_decl
,
8549 tf_warning_or_error
);
8551 input_location
= saved_location
;
8555 instantiate_class_template_1 (tree type
)
8557 tree templ
, args
, pattern
, t
, member
;
8561 unsigned int saved_maximum_field_alignment
;
8564 if (type
== error_mark_node
)
8565 return error_mark_node
;
8567 if (COMPLETE_OR_OPEN_TYPE_P (type
)
8568 || uses_template_parms (type
))
8571 /* Figure out which template is being instantiated. */
8572 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
8573 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
8575 /* Determine what specialization of the original template to
8577 t
= most_specialized_class (type
, templ
, tf_warning_or_error
);
8578 if (t
== error_mark_node
)
8580 TYPE_BEING_DEFINED (type
) = 1;
8581 return error_mark_node
;
8585 /* This TYPE is actually an instantiation of a partial
8586 specialization. We replace the innermost set of ARGS with
8587 the arguments appropriate for substitution. For example,
8590 template <class T> struct S {};
8591 template <class T> struct S<T*> {};
8593 and supposing that we are instantiating S<int*>, ARGS will
8594 presently be {int*} -- but we need {int}. */
8595 pattern
= TREE_TYPE (t
);
8596 args
= TREE_PURPOSE (t
);
8600 pattern
= TREE_TYPE (templ
);
8601 args
= CLASSTYPE_TI_ARGS (type
);
8604 /* If the template we're instantiating is incomplete, then clearly
8605 there's nothing we can do. */
8606 if (!COMPLETE_TYPE_P (pattern
))
8609 /* If we've recursively instantiated too many templates, stop. */
8610 if (! push_tinst_level (type
))
8613 /* Now we're really doing the instantiation. Mark the type as in
8614 the process of being defined. */
8615 TYPE_BEING_DEFINED (type
) = 1;
8617 /* We may be in the middle of deferred access check. Disable
8619 push_deferring_access_checks (dk_no_deferred
);
8621 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
8623 push_to_top_level ();
8624 /* Use #pragma pack from the template context. */
8625 saved_maximum_field_alignment
= maximum_field_alignment
;
8626 maximum_field_alignment
= TYPE_PRECISION (pattern
);
8628 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
8630 /* Set the input location to the most specialized template definition.
8631 This is needed if tsubsting causes an error. */
8632 typedecl
= TYPE_MAIN_DECL (pattern
);
8633 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
8634 DECL_SOURCE_LOCATION (typedecl
);
8636 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
8637 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
8638 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
8639 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
8640 if (ANON_AGGR_TYPE_P (pattern
))
8641 SET_ANON_AGGR_TYPE_P (type
);
8642 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
8644 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
8645 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
8646 /* Adjust visibility for template arguments. */
8647 determine_visibility (TYPE_MAIN_DECL (type
));
8649 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
8651 pbinfo
= TYPE_BINFO (pattern
);
8653 /* We should never instantiate a nested class before its enclosing
8654 class; we need to look up the nested class by name before we can
8655 instantiate it, and that lookup should instantiate the enclosing
8657 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
8658 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
8660 base_list
= NULL_TREE
;
8661 if (BINFO_N_BASE_BINFOS (pbinfo
))
8667 /* We must enter the scope containing the type, as that is where
8668 the accessibility of types named in dependent bases are
8670 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
8672 /* Substitute into each of the bases to determine the actual
8674 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
8677 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
8678 tree expanded_bases
= NULL_TREE
;
8681 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
8684 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
8685 args
, tf_error
, NULL_TREE
);
8686 if (expanded_bases
== error_mark_node
)
8689 len
= TREE_VEC_LENGTH (expanded_bases
);
8692 for (idx
= 0; idx
< len
; idx
++)
8695 /* Extract the already-expanded base class. */
8696 base
= TREE_VEC_ELT (expanded_bases
, idx
);
8698 /* Substitute to figure out the base class. */
8699 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
8702 if (base
== error_mark_node
)
8705 base_list
= tree_cons (access
, base
, base_list
);
8706 if (BINFO_VIRTUAL_P (pbase_binfo
))
8707 TREE_TYPE (base_list
) = integer_type_node
;
8711 /* The list is now in reverse order; correct that. */
8712 base_list
= nreverse (base_list
);
8715 pop_scope (pushed_scope
);
8717 /* Now call xref_basetypes to set up all the base-class
8719 xref_basetypes (type
, base_list
);
8721 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
8722 (int) ATTR_FLAG_TYPE_IN_PLACE
,
8723 args
, tf_error
, NULL_TREE
);
8724 fixup_attribute_variants (type
);
8726 /* Now that our base classes are set up, enter the scope of the
8727 class, so that name lookups into base classes, etc. will work
8728 correctly. This is precisely analogous to what we do in
8729 begin_class_definition when defining an ordinary non-template
8730 class, except we also need to push the enclosing classes. */
8731 push_nested_class (type
);
8733 /* Now members are processed in the order of declaration. */
8734 for (member
= CLASSTYPE_DECL_LIST (pattern
);
8735 member
; member
= TREE_CHAIN (member
))
8737 tree t
= TREE_VALUE (member
);
8739 if (TREE_PURPOSE (member
))
8743 /* Build new CLASSTYPE_NESTED_UTDS. */
8746 bool class_template_p
;
8748 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
8749 && TYPE_LANG_SPECIFIC (t
)
8750 && CLASSTYPE_IS_TEMPLATE (t
));
8751 /* If the member is a class template, then -- even after
8752 substitution -- there may be dependent types in the
8753 template argument list for the class. We increment
8754 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8755 that function will assume that no types are dependent
8756 when outside of a template. */
8757 if (class_template_p
)
8758 ++processing_template_decl
;
8759 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8760 if (class_template_p
)
8761 --processing_template_decl
;
8762 if (newtag
== error_mark_node
)
8765 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
8767 tree name
= TYPE_IDENTIFIER (t
);
8769 if (class_template_p
)
8770 /* Unfortunately, lookup_template_class sets
8771 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8772 instantiation (i.e., for the type of a member
8773 template class nested within a template class.)
8774 This behavior is required for
8775 maybe_process_partial_specialization to work
8776 correctly, but is not accurate in this case;
8777 the TAG is not an instantiation of anything.
8778 (The corresponding TEMPLATE_DECL is an
8779 instantiation, but the TYPE is not.) */
8780 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
8782 /* Now, we call pushtag to put this NEWTAG into the scope of
8783 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8784 pushtag calling push_template_decl. We don't have to do
8785 this for enums because it will already have been done in
8788 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
8789 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
8792 else if (TREE_CODE (t
) == FUNCTION_DECL
8793 || DECL_FUNCTION_TEMPLATE_P (t
))
8795 /* Build new TYPE_METHODS. */
8798 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8799 ++processing_template_decl
;
8800 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8801 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8802 --processing_template_decl
;
8803 set_current_access_from_decl (r
);
8804 finish_member_declaration (r
);
8805 /* Instantiate members marked with attribute used. */
8806 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8811 /* Build new TYPE_FIELDS. */
8812 if (TREE_CODE (t
) == STATIC_ASSERT
)
8816 ++c_inhibit_evaluation_warnings
;
8818 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
8819 tf_warning_or_error
, NULL_TREE
,
8820 /*integral_constant_expression_p=*/true);
8821 --c_inhibit_evaluation_warnings
;
8823 finish_static_assert (condition
,
8824 STATIC_ASSERT_MESSAGE (t
),
8825 STATIC_ASSERT_SOURCE_LOCATION (t
),
8828 else if (TREE_CODE (t
) != CONST_DECL
)
8832 /* The file and line for this declaration, to
8833 assist in error message reporting. Since we
8834 called push_tinst_level above, we don't need to
8836 input_location
= DECL_SOURCE_LOCATION (t
);
8838 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8839 ++processing_template_decl
;
8840 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
8841 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8842 --processing_template_decl
;
8843 if (TREE_CODE (r
) == VAR_DECL
)
8847 [t]he initialization (and any associated
8848 side-effects) of a static data member does
8849 not occur unless the static data member is
8850 itself used in a way that requires the
8851 definition of the static data member to
8854 Therefore, we do not substitute into the
8855 initialized for the static data member here. */
8856 finish_static_data_member_decl
8859 /*init_const_expr_p=*/false,
8860 /*asmspec_tree=*/NULL_TREE
,
8862 /* Instantiate members marked with attribute used. */
8863 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8866 else if (TREE_CODE (r
) == FIELD_DECL
)
8868 /* Determine whether R has a valid type and can be
8869 completed later. If R is invalid, then it is
8870 replaced by error_mark_node so that it will not be
8871 added to TYPE_FIELDS. */
8872 tree rtype
= TREE_TYPE (r
);
8873 if (can_complete_type_without_circularity (rtype
))
8874 complete_type (rtype
);
8876 if (!COMPLETE_TYPE_P (rtype
))
8878 cxx_incomplete_type_error (r
, rtype
);
8879 r
= error_mark_node
;
8883 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8884 such a thing will already have been added to the field
8885 list by tsubst_enum in finish_member_declaration in the
8886 CLASSTYPE_NESTED_UTDS case above. */
8887 if (!(TREE_CODE (r
) == TYPE_DECL
8888 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
8889 && DECL_ARTIFICIAL (r
)))
8891 set_current_access_from_decl (r
);
8892 finish_member_declaration (r
);
8899 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
8900 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
8902 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8904 tree friend_type
= t
;
8905 bool adjust_processing_template_decl
= false;
8907 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
8909 /* template <class T> friend class C; */
8910 friend_type
= tsubst_friend_class (friend_type
, args
);
8911 adjust_processing_template_decl
= true;
8913 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
8915 /* template <class T> friend class C::D; */
8916 friend_type
= tsubst (friend_type
, args
,
8917 tf_warning_or_error
, NULL_TREE
);
8918 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
8919 friend_type
= TREE_TYPE (friend_type
);
8920 adjust_processing_template_decl
= true;
8922 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
8923 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
8925 /* This could be either
8929 when dependent_type_p is false or
8931 template <class U> friend class T::C;
8934 friend_type
= tsubst (friend_type
, args
,
8935 tf_warning_or_error
, NULL_TREE
);
8936 /* Bump processing_template_decl for correct
8937 dependent_type_p calculation. */
8938 ++processing_template_decl
;
8939 if (dependent_type_p (friend_type
))
8940 adjust_processing_template_decl
= true;
8941 --processing_template_decl
;
8943 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
8944 && hidden_name_p (TYPE_NAME (friend_type
)))
8948 where C hasn't been declared yet. Let's lookup name
8949 from namespace scope directly, bypassing any name that
8950 come from dependent base class. */
8951 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
8953 /* The call to xref_tag_from_type does injection for friend
8955 push_nested_namespace (ns
);
8957 xref_tag_from_type (friend_type
, NULL_TREE
,
8958 /*tag_scope=*/ts_current
);
8959 pop_nested_namespace (ns
);
8961 else if (uses_template_parms (friend_type
))
8962 /* friend class C<T>; */
8963 friend_type
= tsubst (friend_type
, args
,
8964 tf_warning_or_error
, NULL_TREE
);
8969 where C is already declared or
8971 friend class C<int>;
8973 We don't have to do anything in these cases. */
8975 if (adjust_processing_template_decl
)
8976 /* Trick make_friend_class into realizing that the friend
8977 we're adding is a template, not an ordinary class. It's
8978 important that we use make_friend_class since it will
8979 perform some error-checking and output cross-reference
8981 ++processing_template_decl
;
8983 if (friend_type
!= error_mark_node
)
8984 make_friend_class (type
, friend_type
, /*complain=*/false);
8986 if (adjust_processing_template_decl
)
8987 --processing_template_decl
;
8991 /* Build new DECL_FRIENDLIST. */
8994 /* The file and line for this declaration, to
8995 assist in error message reporting. Since we
8996 called push_tinst_level above, we don't need to
8998 input_location
= DECL_SOURCE_LOCATION (t
);
9000 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9002 ++processing_template_decl
;
9003 push_deferring_access_checks (dk_no_check
);
9006 r
= tsubst_friend_function (t
, args
);
9007 add_friend (type
, r
, /*complain=*/false);
9008 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9010 pop_deferring_access_checks ();
9011 --processing_template_decl
;
9017 if (tree expr
= CLASSTYPE_LAMBDA_EXPR (type
))
9019 tree decl
= lambda_function (type
);
9022 instantiate_decl (decl
, false, false);
9024 /* We need to instantiate the capture list from the template
9025 after we've instantiated the closure members, but before we
9026 consider adding the conversion op. Also keep any captures
9027 that may have been added during instantiation of the op(). */
9028 tree tmpl_expr
= CLASSTYPE_LAMBDA_EXPR (pattern
);
9030 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr
),
9031 args
, tf_warning_or_error
, NULL_TREE
,
9034 LAMBDA_EXPR_CAPTURE_LIST (expr
)
9035 = chainon (tmpl_cap
, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr
)));
9037 maybe_add_lambda_conv_op (type
);
9040 gcc_assert (errorcount
);
9043 /* Set the file and line number information to whatever is given for
9044 the class itself. This puts error messages involving generated
9045 implicit functions at a predictable point, and the same point
9046 that would be used for non-template classes. */
9047 input_location
= DECL_SOURCE_LOCATION (typedecl
);
9049 unreverse_member_declarations (type
);
9050 finish_struct_1 (type
);
9051 TYPE_BEING_DEFINED (type
) = 0;
9053 /* We don't instantiate default arguments for member functions. 14.7.1:
9055 The implicit instantiation of a class template specialization causes
9056 the implicit instantiation of the declarations, but not of the
9057 definitions or default arguments, of the class member functions,
9058 member classes, static data members and member templates.... */
9060 /* Some typedefs referenced from within the template code need to be access
9061 checked at template instantiation time, i.e now. These types were
9062 added to the template at parsing time. Let's get those and perform
9063 the access checks then. */
9064 perform_typedefs_access_check (pattern
, args
);
9065 perform_deferred_access_checks (tf_warning_or_error
);
9066 pop_nested_class ();
9067 maximum_field_alignment
= saved_maximum_field_alignment
;
9069 pop_from_top_level ();
9070 pop_deferring_access_checks ();
9073 /* The vtable for a template class can be emitted in any translation
9074 unit in which the class is instantiated. When there is no key
9075 method, however, finish_struct_1 will already have added TYPE to
9076 the keyed_classes list. */
9077 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
9078 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
9083 /* Wrapper for instantiate_class_template_1. */
9086 instantiate_class_template (tree type
)
9089 timevar_push (TV_TEMPLATE_INST
);
9090 ret
= instantiate_class_template_1 (type
);
9091 timevar_pop (TV_TEMPLATE_INST
);
9096 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9102 else if (TYPE_P (t
))
9103 r
= tsubst (t
, args
, complain
, in_decl
);
9106 if (!(complain
& tf_warning
))
9107 ++c_inhibit_evaluation_warnings
;
9108 r
= tsubst_expr (t
, args
, complain
, in_decl
,
9109 /*integral_constant_expression_p=*/true);
9110 if (!(complain
& tf_warning
))
9111 --c_inhibit_evaluation_warnings
;
9112 /* Preserve the raw-reference nature of T. */
9113 if (TREE_TYPE (t
) && TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
9114 && REFERENCE_REF_P (r
))
9115 r
= TREE_OPERAND (r
, 0);
9120 /* Given a function parameter pack TMPL_PARM and some function parameters
9121 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9122 and set *SPEC_P to point at the next point in the list. */
9125 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
9127 /* Collect all of the extra "packed" parameters into an
9131 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
9132 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
9133 tree spec_parm
= *spec_p
;
9136 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
9138 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
9141 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9142 parmvec
= make_tree_vec (len
);
9143 parmtypevec
= make_tree_vec (len
);
9144 spec_parm
= *spec_p
;
9145 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
9147 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
9148 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
9151 /* Build the argument packs. */
9152 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
9153 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
9154 TREE_TYPE (argpack
) = argtypepack
;
9155 *spec_p
= spec_parm
;
9160 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9161 NONTYPE_ARGUMENT_PACK. */
9164 make_fnparm_pack (tree spec_parm
)
9166 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
9169 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9173 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
9175 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
9176 if (i
>= TREE_VEC_LENGTH (vec
))
9178 return PACK_EXPANSION_P (TREE_VEC_ELT (vec
, i
));
9182 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9185 make_argument_pack_select (tree arg_pack
, unsigned index
)
9187 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
9189 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
9190 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9195 /* This is a subroutine of tsubst_pack_expansion.
9197 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9198 mechanism to store the (non complete list of) arguments of the
9199 substitution and return a non substituted pack expansion, in order
9200 to wait for when we have enough arguments to really perform the
9204 use_pack_expansion_extra_args_p (tree parm_packs
,
9208 if (parm_packs
== NULL_TREE
)
9211 bool has_expansion_arg
= false;
9212 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
9214 bool has_non_expansion_arg
= false;
9215 for (tree parm_pack
= parm_packs
;
9217 parm_pack
= TREE_CHAIN (parm_pack
))
9219 tree arg
= TREE_VALUE (parm_pack
);
9221 if (argument_pack_element_is_expansion_p (arg
, i
))
9222 has_expansion_arg
= true;
9224 has_non_expansion_arg
= true;
9227 /* If one pack has an expansion and another pack has a normal
9228 argument or if one pack has an empty argument another one
9229 hasn't then tsubst_pack_expansion cannot perform the
9230 substitution and need to fall back on the
9231 PACK_EXPANSION_EXTRA mechanism. */
9232 if ((has_expansion_arg
&& has_non_expansion_arg
)
9233 || (has_empty_arg
&& (has_expansion_arg
|| has_non_expansion_arg
)))
9239 /* [temp.variadic]/6 says that:
9241 The instantiation of a pack expansion [...]
9242 produces a list E1,E2, ..., En, where N is the number of elements
9243 in the pack expansion parameters.
9245 This subroutine of tsubst_pack_expansion produces one of these Ei.
9247 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9248 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9249 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9250 INDEX is the index 'i' of the element Ei to produce. ARGS,
9251 COMPLAIN, and IN_DECL are the same parameters as for the
9252 tsubst_pack_expansion function.
9254 The function returns the resulting Ei upon successful completion,
9257 Note that this function possibly modifies the ARGS parameter, so
9258 it's the responsibility of the caller to restore it. */
9261 gen_elem_of_pack_expansion_instantiation (tree pattern
,
9264 tree args
/* This parm gets
9266 tsubst_flags_t complain
,
9270 bool ith_elem_is_expansion
= false;
9272 /* For each parameter pack, change the substitution of the parameter
9273 pack to the ith argument in its argument pack, then expand the
9275 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
9277 tree parm
= TREE_PURPOSE (pack
);
9278 tree arg_pack
= TREE_VALUE (pack
);
9279 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
9281 ith_elem_is_expansion
|=
9282 argument_pack_element_is_expansion_p (arg_pack
, index
);
9284 /* Select the Ith argument from the pack. */
9285 if (TREE_CODE (parm
) == PARM_DECL
)
9289 aps
= make_argument_pack_select (arg_pack
, index
);
9291 register_local_specialization (aps
, parm
);
9294 aps
= retrieve_local_specialization (parm
);
9299 template_parm_level_and_index (parm
, &level
, &idx
);
9303 aps
= make_argument_pack_select (arg_pack
, index
);
9304 /* Update the corresponding argument. */
9305 TMPL_ARG (args
, level
, idx
) = aps
;
9308 /* Re-use the ARGUMENT_PACK_SELECT. */
9309 aps
= TMPL_ARG (args
, level
, idx
);
9311 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9314 /* Substitute into the PATTERN with the (possibly altered)
9316 if (!TYPE_P (pattern
))
9317 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9318 /*integral_constant_expression_p=*/false);
9320 t
= tsubst (pattern
, args
, complain
, in_decl
);
9322 /* If the Ith argument pack element is a pack expansion, then
9323 the Ith element resulting from the substituting is going to
9324 be a pack expansion as well. */
9325 if (ith_elem_is_expansion
)
9326 t
= make_pack_expansion (t
);
9331 /* Substitute ARGS into T, which is an pack expansion
9332 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9333 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9334 (if only a partial substitution could be performed) or
9335 ERROR_MARK_NODE if there was an error. */
9337 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
9341 tree pack
, packs
= NULL_TREE
;
9342 bool unsubstituted_packs
= false;
9345 struct pointer_map_t
*saved_local_specializations
= NULL
;
9346 bool need_local_specializations
= false;
9349 gcc_assert (PACK_EXPANSION_P (t
));
9350 pattern
= PACK_EXPANSION_PATTERN (t
);
9352 /* Add in any args remembered from an earlier partial instantiation. */
9353 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
9355 levels
= TMPL_ARGS_DEPTH (args
);
9357 /* Determine the argument packs that will instantiate the parameter
9358 packs used in the expansion expression. While we're at it,
9359 compute the number of arguments to be expanded and make sure it
9361 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
9362 pack
= TREE_CHAIN (pack
))
9364 tree parm_pack
= TREE_VALUE (pack
);
9365 tree arg_pack
= NULL_TREE
;
9366 tree orig_arg
= NULL_TREE
;
9369 if (TREE_CODE (parm_pack
) == BASES
)
9371 if (BASES_DIRECT (parm_pack
))
9372 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9373 args
, complain
, in_decl
, false));
9375 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9376 args
, complain
, in_decl
, false));
9378 if (TREE_CODE (parm_pack
) == PARM_DECL
)
9380 if (PACK_EXPANSION_LOCAL_P (t
))
9381 arg_pack
= retrieve_local_specialization (parm_pack
);
9384 /* We can't rely on local_specializations for a parameter
9385 name used later in a function declaration (such as in a
9386 late-specified return type). Even if it exists, it might
9387 have the wrong value for a recursive call. Just make a
9388 dummy decl, since it's only used for its type. */
9389 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
9390 if (arg_pack
&& FUNCTION_PARAMETER_PACK_P (arg_pack
))
9391 /* Partial instantiation of the parm_pack, we can't build
9392 up an argument pack yet. */
9393 arg_pack
= NULL_TREE
;
9395 arg_pack
= make_fnparm_pack (arg_pack
);
9396 need_local_specializations
= true;
9402 template_parm_level_and_index (parm_pack
, &level
, &idx
);
9404 if (level
<= levels
)
9405 arg_pack
= TMPL_ARG (args
, level
, idx
);
9408 orig_arg
= arg_pack
;
9409 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
9410 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
9412 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
9413 /* This can only happen if we forget to expand an argument
9414 pack somewhere else. Just return an error, silently. */
9416 result
= make_tree_vec (1);
9417 TREE_VEC_ELT (result
, 0) = error_mark_node
;
9424 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
9426 /* Don't bother trying to do a partial substitution with
9427 incomplete packs; we'll try again after deduction. */
9428 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
9433 else if (len
!= my_len
)
9435 if (!(complain
& tf_error
))
9436 /* Fail quietly. */;
9437 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
9438 error ("mismatched argument pack lengths while expanding "
9442 error ("mismatched argument pack lengths while expanding "
9445 return error_mark_node
;
9448 /* Keep track of the parameter packs and their corresponding
9450 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
9451 TREE_TYPE (packs
) = orig_arg
;
9455 /* We can't substitute for this parameter pack. We use a flag as
9456 well as the missing_level counter because function parameter
9457 packs don't have a level. */
9458 unsubstituted_packs
= true;
9462 /* We cannot expand this expansion expression, because we don't have
9463 all of the argument packs we need. */
9464 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
9466 /* We got some full packs, but we can't substitute them in until we
9467 have values for all the packs. So remember these until then. */
9469 t
= make_pack_expansion (pattern
);
9470 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
9473 else if (unsubstituted_packs
)
9475 /* There were no real arguments, we're just replacing a parameter
9476 pack with another version of itself. Substitute into the
9477 pattern and return a PACK_EXPANSION_*. The caller will need to
9479 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
9480 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9481 /*integral_constant_expression_p=*/false);
9483 t
= tsubst (pattern
, args
, complain
, in_decl
);
9484 t
= make_pack_expansion (t
);
9488 gcc_assert (len
>= 0);
9490 if (need_local_specializations
)
9492 /* We're in a late-specified return type, so create our own local
9493 specializations map; the current map is either NULL or (in the
9494 case of recursive unification) might have bindings that we don't
9495 want to use or alter. */
9496 saved_local_specializations
= local_specializations
;
9497 local_specializations
= pointer_map_create ();
9500 /* For each argument in each argument pack, substitute into the
9502 result
= make_tree_vec (len
);
9503 for (i
= 0; i
< len
; ++i
)
9505 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
9509 TREE_VEC_ELT (result
, i
) = t
;
9510 if (t
== error_mark_node
)
9512 result
= error_mark_node
;
9517 /* Update ARGS to restore the substitution from parameter packs to
9518 their argument packs. */
9519 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9521 tree parm
= TREE_PURPOSE (pack
);
9523 if (TREE_CODE (parm
) == PARM_DECL
)
9524 register_local_specialization (TREE_TYPE (pack
), parm
);
9529 if (TREE_VALUE (pack
) == NULL_TREE
)
9532 template_parm_level_and_index (parm
, &level
, &idx
);
9534 /* Update the corresponding argument. */
9535 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
9536 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
9539 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
9543 if (need_local_specializations
)
9545 pointer_map_destroy (local_specializations
);
9546 local_specializations
= saved_local_specializations
;
9552 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9553 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9554 parameter packs; all parms generated from a function parameter pack will
9555 have the same DECL_PARM_INDEX. */
9558 get_pattern_parm (tree parm
, tree tmpl
)
9560 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9563 if (DECL_ARTIFICIAL (parm
))
9565 for (patparm
= DECL_ARGUMENTS (pattern
);
9566 patparm
; patparm
= DECL_CHAIN (patparm
))
9567 if (DECL_ARTIFICIAL (patparm
)
9568 && DECL_NAME (parm
) == DECL_NAME (patparm
))
9573 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
9574 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
9575 gcc_assert (DECL_PARM_INDEX (patparm
)
9576 == DECL_PARM_INDEX (parm
));
9582 /* Substitute ARGS into the vector or list of template arguments T. */
9585 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9588 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
9591 if (t
== error_mark_node
)
9592 return error_mark_node
;
9594 len
= TREE_VEC_LENGTH (t
);
9595 elts
= XALLOCAVEC (tree
, len
);
9597 for (i
= 0; i
< len
; i
++)
9599 tree orig_arg
= TREE_VEC_ELT (t
, i
);
9602 if (TREE_CODE (orig_arg
) == TREE_VEC
)
9603 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
9604 else if (PACK_EXPANSION_P (orig_arg
))
9606 /* Substitute into an expansion expression. */
9607 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
9609 if (TREE_CODE (new_arg
) == TREE_VEC
)
9610 /* Add to the expanded length adjustment the number of
9611 expanded arguments. We subtract one from this
9612 measurement, because the argument pack expression
9613 itself is already counted as 1 in
9614 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9615 the argument pack is empty. */
9616 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
9618 else if (ARGUMENT_PACK_P (orig_arg
))
9620 /* Substitute into each of the arguments. */
9621 new_arg
= TYPE_P (orig_arg
)
9622 ? cxx_make_type (TREE_CODE (orig_arg
))
9623 : make_node (TREE_CODE (orig_arg
));
9625 SET_ARGUMENT_PACK_ARGS (
9627 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
9628 args
, complain
, in_decl
));
9630 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
9631 new_arg
= error_mark_node
;
9633 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
9634 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
9636 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
9638 if (TREE_TYPE (new_arg
) == error_mark_node
)
9639 new_arg
= error_mark_node
;
9643 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
9645 if (new_arg
== error_mark_node
)
9646 return error_mark_node
;
9649 if (new_arg
!= orig_arg
)
9656 /* Make space for the expanded arguments coming from template
9658 t
= make_tree_vec (len
+ expanded_len_adjust
);
9659 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9660 arguments for a member template.
9661 In that case each TREE_VEC in ORIG_T represents a level of template
9662 arguments, and ORIG_T won't carry any non defaulted argument count.
9663 It will rather be the nested TREE_VECs that will carry one.
9664 In other words, ORIG_T carries a non defaulted argument count only
9665 if it doesn't contain any nested TREE_VEC. */
9666 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
9668 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
9669 count
+= expanded_len_adjust
;
9670 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
9672 for (i
= 0, out
= 0; i
< len
; i
++)
9674 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
9675 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
9676 && TREE_CODE (elts
[i
]) == TREE_VEC
)
9680 /* Now expand the template argument pack "in place". */
9681 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
9682 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
9686 TREE_VEC_ELT (t
, out
) = elts
[i
];
9694 /* Return the result of substituting ARGS into the template parameters
9695 given by PARMS. If there are m levels of ARGS and m + n levels of
9696 PARMS, then the result will contain n levels of PARMS. For
9697 example, if PARMS is `template <class T> template <class U>
9698 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9699 result will be `template <int*, double, class V>'. */
9702 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
9707 /* When substituting into a template, we must set
9708 PROCESSING_TEMPLATE_DECL as the template parameters may be
9709 dependent if they are based on one-another, and the dependency
9710 predicates are short-circuit outside of templates. */
9711 ++processing_template_decl
;
9713 for (new_parms
= &r
;
9714 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
9715 new_parms
= &(TREE_CHAIN (*new_parms
)),
9716 parms
= TREE_CHAIN (parms
))
9719 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
9722 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
9726 if (parms
== error_mark_node
)
9729 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
9731 if (tuple
== error_mark_node
)
9734 TREE_VEC_ELT (new_vec
, i
) =
9735 tsubst_template_parm (tuple
, args
, complain
);
9739 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
9740 - TMPL_ARGS_DEPTH (args
)),
9741 new_vec
, NULL_TREE
);
9744 --processing_template_decl
;
9749 /* Return the result of substituting ARGS into one template parameter
9750 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9751 parameter and which TREE_PURPOSE is the default argument of the
9752 template parameter. */
9755 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
9757 tree default_value
, parm_decl
;
9759 if (args
== NULL_TREE
9761 || t
== error_mark_node
)
9764 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
9766 default_value
= TREE_PURPOSE (t
);
9767 parm_decl
= TREE_VALUE (t
);
9769 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
9770 if (TREE_CODE (parm_decl
) == PARM_DECL
9771 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
9772 parm_decl
= error_mark_node
;
9773 default_value
= tsubst_template_arg (default_value
, args
,
9774 complain
, NULL_TREE
);
9776 return build_tree_list (default_value
, parm_decl
);
9779 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9780 type T. If T is not an aggregate or enumeration type, it is
9781 handled as if by tsubst. IN_DECL is as for tsubst. If
9782 ENTERING_SCOPE is nonzero, T is the context for a template which
9783 we are presently tsubst'ing. Return the substituted value. */
9786 tsubst_aggr_type (tree t
,
9788 tsubst_flags_t complain
,
9795 switch (TREE_CODE (t
))
9798 if (TYPE_PTRMEMFUNC_P (t
))
9799 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
9801 /* Else fall through. */
9804 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
9809 int saved_unevaluated_operand
;
9810 int saved_inhibit_evaluation_warnings
;
9812 /* In "sizeof(X<I>)" we need to evaluate "I". */
9813 saved_unevaluated_operand
= cp_unevaluated_operand
;
9814 cp_unevaluated_operand
= 0;
9815 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9816 c_inhibit_evaluation_warnings
= 0;
9818 /* First, determine the context for the type we are looking
9820 context
= TYPE_CONTEXT (t
);
9821 if (context
&& TYPE_P (context
))
9823 context
= tsubst_aggr_type (context
, args
, complain
,
9824 in_decl
, /*entering_scope=*/1);
9825 /* If context is a nested class inside a class template,
9826 it may still need to be instantiated (c++/33959). */
9827 context
= complete_type (context
);
9830 /* Then, figure out what arguments are appropriate for the
9831 type we are trying to find. For example, given:
9833 template <class T> struct S;
9834 template <class T, class U> void f(T, U) { S<U> su; }
9836 and supposing that we are instantiating f<int, double>,
9837 then our ARGS will be {int, double}, but, when looking up
9838 S we only want {double}. */
9839 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
9841 if (argvec
== error_mark_node
)
9842 r
= error_mark_node
;
9845 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
9846 entering_scope
, complain
);
9847 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
9850 cp_unevaluated_operand
= saved_unevaluated_operand
;
9851 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
9856 /* This is not a template type, so there's nothing to do. */
9860 return tsubst (t
, args
, complain
, in_decl
);
9864 /* Substitute into the default argument ARG (a default argument for
9865 FN), which has the indicated TYPE. */
9868 tsubst_default_argument (tree fn
, tree type
, tree arg
)
9870 tree saved_class_ptr
= NULL_TREE
;
9871 tree saved_class_ref
= NULL_TREE
;
9872 int errs
= errorcount
+ sorrycount
;
9874 /* This can happen in invalid code. */
9875 if (TREE_CODE (arg
) == DEFAULT_ARG
)
9878 /* This default argument came from a template. Instantiate the
9879 default argument here, not in tsubst. In the case of
9888 we must be careful to do name lookup in the scope of S<T>,
9889 rather than in the current class. */
9890 push_access_scope (fn
);
9891 /* The "this" pointer is not valid in a default argument. */
9894 saved_class_ptr
= current_class_ptr
;
9895 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
9896 saved_class_ref
= current_class_ref
;
9897 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
9900 push_deferring_access_checks(dk_no_deferred
);
9901 /* The default argument expression may cause implicitly defined
9902 member functions to be synthesized, which will result in garbage
9903 collection. We must treat this situation as if we were within
9904 the body of function so as to avoid collecting live data on the
9907 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
9908 tf_warning_or_error
, NULL_TREE
,
9909 /*integral_constant_expression_p=*/false);
9911 pop_deferring_access_checks();
9913 /* Restore the "this" pointer. */
9916 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
9917 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
9920 if (errorcount
+sorrycount
> errs
)
9921 inform (input_location
,
9922 " when instantiating default argument for call to %D", fn
);
9924 /* Make sure the default argument is reasonable. */
9925 arg
= check_default_argument (type
, arg
);
9927 pop_access_scope (fn
);
9932 /* Substitute into all the default arguments for FN. */
9935 tsubst_default_arguments (tree fn
)
9940 tmpl_args
= DECL_TI_ARGS (fn
);
9942 /* If this function is not yet instantiated, we certainly don't need
9943 its default arguments. */
9944 if (uses_template_parms (tmpl_args
))
9946 /* Don't do this again for clones. */
9947 if (DECL_CLONED_FUNCTION_P (fn
))
9950 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
9952 arg
= TREE_CHAIN (arg
))
9953 if (TREE_PURPOSE (arg
))
9954 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
9956 TREE_PURPOSE (arg
));
9959 /* Substitute the ARGS into the T, which is a _DECL. Return the
9960 result of the substitution. Issue error and warning messages under
9961 control of COMPLAIN. */
9964 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
9966 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
9967 location_t saved_loc
;
9972 /* Set the filename and linenumber to improve error-reporting. */
9973 saved_loc
= input_location
;
9974 input_location
= DECL_SOURCE_LOCATION (t
);
9976 switch (TREE_CODE (t
))
9980 /* We can get here when processing a member function template,
9981 member class template, or template template parameter. */
9982 tree decl
= DECL_TEMPLATE_RESULT (t
);
9987 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9989 /* Template template parameter is treated here. */
9990 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9991 if (new_type
== error_mark_node
)
9992 RETURN (error_mark_node
);
9993 /* If we get a real template back, return it. This can happen in
9994 the context of most_specialized_class. */
9995 if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
9999 DECL_CHAIN (r
) = NULL_TREE
;
10000 TREE_TYPE (r
) = new_type
;
10001 DECL_TEMPLATE_RESULT (r
)
10002 = build_decl (DECL_SOURCE_LOCATION (decl
),
10003 TYPE_DECL
, DECL_NAME (decl
), new_type
);
10004 DECL_TEMPLATE_PARMS (r
)
10005 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10007 TYPE_NAME (new_type
) = r
;
10011 /* We might already have an instance of this template.
10012 The ARGS are for the surrounding class type, so the
10013 full args contain the tsubst'd args for the context,
10014 plus the innermost args from the template decl. */
10015 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
10016 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
10017 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
10018 /* Because this is a template, the arguments will still be
10019 dependent, even after substitution. If
10020 PROCESSING_TEMPLATE_DECL is not set, the dependency
10021 predicates will short-circuit. */
10022 ++processing_template_decl
;
10023 full_args
= tsubst_template_args (tmpl_args
, args
,
10024 complain
, in_decl
);
10025 --processing_template_decl
;
10026 if (full_args
== error_mark_node
)
10027 RETURN (error_mark_node
);
10029 /* If this is a default template template argument,
10030 tsubst might not have changed anything. */
10031 if (full_args
== tmpl_args
)
10034 hash
= hash_tmpl_and_args (t
, full_args
);
10035 spec
= retrieve_specialization (t
, full_args
, hash
);
10036 if (spec
!= NULL_TREE
)
10042 /* Make a new template decl. It will be similar to the
10043 original, but will record the current template arguments.
10044 We also create a new function declaration, which is just
10045 like the old one, but points to this new template, rather
10046 than the old one. */
10048 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
10049 DECL_CHAIN (r
) = NULL_TREE
;
10051 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10053 if (TREE_CODE (decl
) == TYPE_DECL
10054 && !TYPE_DECL_ALIAS_P (decl
))
10057 ++processing_template_decl
;
10058 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10059 --processing_template_decl
;
10060 if (new_type
== error_mark_node
)
10061 RETURN (error_mark_node
);
10063 TREE_TYPE (r
) = new_type
;
10064 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
10065 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
10066 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
10067 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
10072 ++processing_template_decl
;
10073 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
10074 --processing_template_decl
;
10075 if (new_decl
== error_mark_node
)
10076 RETURN (error_mark_node
);
10078 DECL_TEMPLATE_RESULT (r
) = new_decl
;
10079 DECL_TI_TEMPLATE (new_decl
) = r
;
10080 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
10081 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
10082 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
10085 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10086 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
10087 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
10089 /* The template parameters for this new template are all the
10090 template parameters for the old template, except the
10091 outermost level of parameters. */
10092 DECL_TEMPLATE_PARMS (r
)
10093 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10096 if (PRIMARY_TEMPLATE_P (t
))
10097 DECL_PRIMARY_TEMPLATE (r
) = r
;
10099 if (TREE_CODE (decl
) != TYPE_DECL
)
10100 /* Record this non-type partial instantiation. */
10101 register_specialization (r
, t
,
10102 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
10107 case FUNCTION_DECL
:
10110 tree argvec
= NULL_TREE
;
10118 /* Nobody should be tsubst'ing into non-template functions. */
10119 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
10121 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
10126 /* If T is not dependent, just return it. We have to
10127 increment PROCESSING_TEMPLATE_DECL because
10128 value_dependent_expression_p assumes that nothing is
10129 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10130 ++processing_template_decl
;
10131 dependent_p
= value_dependent_expression_p (t
);
10132 --processing_template_decl
;
10136 /* Calculate the most general template of which R is a
10137 specialization, and the complete set of arguments used to
10139 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
10140 argvec
= tsubst_template_args (DECL_TI_ARGS
10141 (DECL_TEMPLATE_RESULT
10142 (DECL_TI_TEMPLATE (t
))),
10143 args
, complain
, in_decl
);
10144 if (argvec
== error_mark_node
)
10145 RETURN (error_mark_node
);
10147 /* Check to see if we already have this specialization. */
10148 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10149 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10157 /* We can see more levels of arguments than parameters if
10158 there was a specialization of a member template, like
10161 template <class T> struct S { template <class U> void f(); }
10162 template <> template <class U> void S<int>::f(U);
10164 Here, we'll be substituting into the specialization,
10165 because that's where we can find the code we actually
10166 want to generate, but we'll have enough arguments for
10167 the most general template.
10169 We also deal with the peculiar case:
10171 template <class T> struct S {
10172 template <class U> friend void f();
10174 template <class U> void f() {}
10176 template void f<double>();
10178 Here, the ARGS for the instantiation of will be {int,
10179 double}. But, we only need as many ARGS as there are
10180 levels of template parameters in CODE_PATTERN. We are
10181 careful not to get fooled into reducing the ARGS in
10184 template <class T> struct S { template <class U> void f(U); }
10185 template <class T> template <> void S<T>::f(int) {}
10187 which we can spot because the pattern will be a
10188 specialization in this case. */
10189 args_depth
= TMPL_ARGS_DEPTH (args
);
10191 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
10192 if (args_depth
> parms_depth
10193 && !DECL_TEMPLATE_SPECIALIZATION (t
))
10194 args
= get_innermost_template_args (args
, parms_depth
);
10198 /* This special case arises when we have something like this:
10200 template <class T> struct S {
10201 friend void f<int>(int, double);
10204 Here, the DECL_TI_TEMPLATE for the friend declaration
10205 will be an IDENTIFIER_NODE. We are being called from
10206 tsubst_friend_function, and we want only to create a
10207 new decl (R) with appropriate types so that we can call
10208 determine_specialization. */
10209 gen_tmpl
= NULL_TREE
;
10212 if (DECL_CLASS_SCOPE_P (t
))
10214 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
10218 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
10219 complain
, t
, /*entering_scope=*/1);
10224 ctx
= DECL_CONTEXT (t
);
10226 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10227 if (type
== error_mark_node
)
10228 RETURN (error_mark_node
);
10230 /* If we hit excessive deduction depth, the type is bogus even if
10231 it isn't error_mark_node, so don't build a decl. */
10232 if (excessive_deduction_depth
)
10233 RETURN (error_mark_node
);
10235 /* We do NOT check for matching decls pushed separately at this
10236 point, as they may not represent instantiations of this
10237 template, and in any case are considered separate under the
10240 DECL_USE_TEMPLATE (r
) = 0;
10241 TREE_TYPE (r
) = type
;
10242 /* Clear out the mangled name and RTL for the instantiation. */
10243 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10244 SET_DECL_RTL (r
, NULL
);
10245 /* Leave DECL_INITIAL set on deleted instantiations. */
10246 if (!DECL_DELETED_FN (r
))
10247 DECL_INITIAL (r
) = NULL_TREE
;
10248 DECL_CONTEXT (r
) = ctx
;
10250 if (member
&& DECL_CONV_FN_P (r
))
10251 /* Type-conversion operator. Reconstruct the name, in
10252 case it's the name of one of the template's parameters. */
10253 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
10255 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
10257 DECL_RESULT (r
) = NULL_TREE
;
10259 TREE_STATIC (r
) = 0;
10260 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
10261 DECL_EXTERNAL (r
) = 1;
10262 /* If this is an instantiation of a function with internal
10263 linkage, we already know what object file linkage will be
10264 assigned to the instantiation. */
10265 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
10266 DECL_DEFER_OUTPUT (r
) = 0;
10267 DECL_CHAIN (r
) = NULL_TREE
;
10268 DECL_PENDING_INLINE_INFO (r
) = 0;
10269 DECL_PENDING_INLINE_P (r
) = 0;
10270 DECL_SAVED_TREE (r
) = NULL_TREE
;
10271 DECL_STRUCT_FUNCTION (r
) = NULL
;
10273 /* We'll re-clone as appropriate in instantiate_template. */
10274 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
10276 /* If we aren't complaining now, return on error before we register
10277 the specialization so that we'll complain eventually. */
10278 if ((complain
& tf_error
) == 0
10279 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10280 && !grok_op_properties (r
, /*complain=*/false))
10281 RETURN (error_mark_node
);
10283 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10284 this in the special friend case mentioned above where
10285 GEN_TMPL is NULL. */
10288 DECL_TEMPLATE_INFO (r
)
10289 = build_template_info (gen_tmpl
, argvec
);
10290 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10293 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10295 /* We instantiated this while substituting into
10296 the type earlier (template/friend54.C). */
10299 /* We're not supposed to instantiate default arguments
10300 until they are called, for a template. But, for a
10303 template <class T> void f ()
10304 { extern void g(int i = T()); }
10306 we should do the substitution when the template is
10307 instantiated. We handle the member function case in
10308 instantiate_class_template since the default arguments
10309 might refer to other members of the class. */
10311 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
10312 && !uses_template_parms (argvec
))
10313 tsubst_default_arguments (r
);
10316 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
10318 /* Copy the list of befriending classes. */
10319 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
10321 friends
= &TREE_CHAIN (*friends
))
10323 *friends
= copy_node (*friends
);
10324 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
10329 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
10331 maybe_retrofit_in_chrg (r
);
10332 if (DECL_CONSTRUCTOR_P (r
))
10333 grok_ctor_properties (ctx
, r
);
10334 if (DECL_INHERITED_CTOR_BASE (r
))
10335 deduce_inheriting_ctor (r
);
10336 /* If this is an instantiation of a member template, clone it.
10337 If it isn't, that'll be handled by
10338 clone_constructors_and_destructors. */
10339 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
10340 clone_function_decl (r
, /*update_method_vec_p=*/0);
10342 else if ((complain
& tf_error
) != 0
10343 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10344 && !grok_op_properties (r
, /*complain=*/true))
10345 RETURN (error_mark_node
);
10347 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
10348 SET_DECL_FRIEND_CONTEXT (r
,
10349 tsubst (DECL_FRIEND_CONTEXT (t
),
10350 args
, complain
, in_decl
));
10352 /* Possibly limit visibility based on template args. */
10353 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10354 if (DECL_VISIBILITY_SPECIFIED (t
))
10356 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10357 DECL_ATTRIBUTES (r
)
10358 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10360 determine_visibility (r
);
10361 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
10362 && !processing_template_decl
)
10363 defaulted_late_check (r
);
10365 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10366 args
, complain
, in_decl
);
10372 tree type
= NULL_TREE
;
10374 tree expanded_types
= NULL_TREE
;
10375 tree prev_r
= NULL_TREE
;
10376 tree first_r
= NULL_TREE
;
10378 if (FUNCTION_PARAMETER_PACK_P (t
))
10380 /* If there is a local specialization that isn't a
10381 parameter pack, it means that we're doing a "simple"
10382 substitution from inside tsubst_pack_expansion. Just
10383 return the local specialization (which will be a single
10385 tree spec
= retrieve_local_specialization (t
);
10387 && TREE_CODE (spec
) == PARM_DECL
10388 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
10391 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10392 the parameters in this function parameter pack. */
10393 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10394 complain
, in_decl
);
10395 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10397 len
= TREE_VEC_LENGTH (expanded_types
);
10399 /* Zero-length parameter packs are boring. Just substitute
10402 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
10407 /* All we did was update the type. Make a note of that. */
10408 type
= expanded_types
;
10409 expanded_types
= NULL_TREE
;
10413 /* Loop through all of the parameter's we'll build. When T is
10414 a function parameter pack, LEN is the number of expanded
10415 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10417 for (i
= 0; i
< len
; ++i
)
10421 if (DECL_TEMPLATE_PARM_P (t
))
10422 SET_DECL_TEMPLATE_PARM_P (r
);
10424 if (expanded_types
)
10425 /* We're on the Ith parameter of the function parameter
10428 /* An argument of a function parameter pack is not a parameter
10430 FUNCTION_PARAMETER_PACK_P (r
) = false;
10432 /* Get the Ith type. */
10433 type
= TREE_VEC_ELT (expanded_types
, i
);
10435 /* Rename the parameter to include the index. */
10437 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10440 /* We're dealing with a normal parameter. */
10441 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10443 type
= type_decays_to (type
);
10444 TREE_TYPE (r
) = type
;
10445 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10447 if (DECL_INITIAL (r
))
10449 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
10450 DECL_INITIAL (r
) = TREE_TYPE (r
);
10452 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
10453 complain
, in_decl
);
10456 DECL_CONTEXT (r
) = NULL_TREE
;
10458 if (!DECL_TEMPLATE_PARM_P (r
))
10459 DECL_ARG_TYPE (r
) = type_passed_as (type
);
10461 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10462 args
, complain
, in_decl
);
10464 /* Keep track of the first new parameter we
10465 generate. That's what will be returned to the
10470 /* Build a proper chain of parameters when substituting
10471 into a function parameter pack. */
10473 DECL_CHAIN (prev_r
) = r
;
10476 /* If cp_unevaluated_operand is set, we're just looking for a
10477 single dummy parameter, so don't keep going. */
10478 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
10479 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
10480 complain
, DECL_CHAIN (t
));
10482 /* FIRST_R contains the start of the chain we've built. */
10492 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10493 if (type
== error_mark_node
)
10494 RETURN (error_mark_node
);
10495 TREE_TYPE (r
) = type
;
10496 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10498 if (DECL_C_BIT_FIELD (r
))
10499 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10500 non-bit-fields DECL_INITIAL is a non-static data member
10501 initializer, which gets deferred instantiation. */
10503 = tsubst_expr (DECL_INITIAL (t
), args
,
10505 /*integral_constant_expression_p=*/true);
10506 else if (DECL_INITIAL (t
))
10508 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10509 NSDMI in perform_member_init. Still set DECL_INITIAL
10510 so that we know there is one. */
10511 DECL_INITIAL (r
) = void_zero_node
;
10512 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
10513 retrofit_lang_decl (r
);
10514 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10516 /* We don't have to set DECL_CONTEXT here; it is set by
10517 finish_member_declaration. */
10518 DECL_CHAIN (r
) = NULL_TREE
;
10519 if (VOID_TYPE_P (type
))
10520 error ("instantiation of %q+D as type %qT", r
, type
);
10522 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10523 args
, complain
, in_decl
);
10528 /* We reach here only for member using decls. We also need to check
10529 uses_template_parms because DECL_DEPENDENT_P is not set for a
10530 using-declaration that designates a member of the current
10531 instantiation (c++/53549). */
10532 if (DECL_DEPENDENT_P (t
)
10533 || uses_template_parms (USING_DECL_SCOPE (t
)))
10535 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
10536 complain
, in_decl
);
10537 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
10538 r
= do_class_using_decl (inst_scope
, name
);
10540 r
= error_mark_node
;
10543 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
10544 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
10550 DECL_CHAIN (r
) = NULL_TREE
;
10557 tree argvec
= NULL_TREE
;
10558 tree gen_tmpl
= NULL_TREE
;
10560 tree tmpl
= NULL_TREE
;
10562 tree type
= NULL_TREE
;
10565 if (TREE_CODE (t
) == TYPE_DECL
10566 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
10568 /* If this is the canonical decl, we don't have to
10569 mess with instantiations, and often we can't (for
10570 typename, template type parms and such). Note that
10571 TYPE_NAME is not correct for the above test if
10572 we've copied the type for a typedef. */
10573 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10574 if (type
== error_mark_node
)
10575 RETURN (error_mark_node
);
10576 r
= TYPE_NAME (type
);
10580 /* Check to see if we already have the specialization we
10583 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
10585 /* T is a static data member or namespace-scope entity.
10586 We have to substitute into namespace-scope variables
10587 (even though such entities are never templates) because
10590 template <class T> void f() { extern T t; }
10592 where the entity referenced is not known until
10593 instantiation time. */
10595 ctx
= DECL_CONTEXT (t
);
10596 if (DECL_CLASS_SCOPE_P (t
))
10598 ctx
= tsubst_aggr_type (ctx
, args
,
10600 in_decl
, /*entering_scope=*/1);
10601 /* If CTX is unchanged, then T is in fact the
10602 specialization we want. That situation occurs when
10603 referencing a static data member within in its own
10604 class. We can use pointer equality, rather than
10605 same_type_p, because DECL_CONTEXT is always
10607 if (ctx
== DECL_CONTEXT (t
)
10608 && (TREE_CODE (t
) != TYPE_DECL
10609 /* ... unless T is a member template; in which
10610 case our caller can be willing to create a
10611 specialization of that template represented
10613 || !(DECL_TI_TEMPLATE (t
)
10614 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))))
10620 tmpl
= DECL_TI_TEMPLATE (t
);
10621 gen_tmpl
= most_general_template (tmpl
);
10622 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
10623 if (argvec
== error_mark_node
)
10624 RETURN (error_mark_node
);
10625 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10626 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10631 /* A local variable. */
10633 /* Subsequent calls to pushdecl will fill this in. */
10635 spec
= retrieve_local_specialization (t
);
10637 /* If we already have the specialization we need, there is
10638 nothing more to do. */
10645 if (TREE_CODE (t
) == VAR_DECL
&& DECL_ANON_UNION_VAR_P (t
))
10647 /* Just use name lookup to find a member alias for an anonymous
10648 union, but then add it to the hash table. */
10649 r
= lookup_name (DECL_NAME (t
));
10650 gcc_assert (DECL_ANON_UNION_VAR_P (r
));
10651 register_local_specialization (r
, t
);
10655 /* Create a new node for the specialization we need. */
10657 if (type
== NULL_TREE
)
10659 if (is_typedef_decl (t
))
10660 type
= DECL_ORIGINAL_TYPE (t
);
10662 type
= TREE_TYPE (t
);
10663 if (TREE_CODE (t
) == VAR_DECL
10664 && VAR_HAD_UNKNOWN_BOUND (t
)
10665 && type
!= error_mark_node
)
10666 type
= strip_array_domain (type
);
10667 type
= tsubst (type
, args
, complain
, in_decl
);
10669 if (TREE_CODE (r
) == VAR_DECL
)
10671 /* Even if the original location is out of scope, the
10672 newly substituted one is not. */
10673 DECL_DEAD_FOR_LOCAL (r
) = 0;
10674 DECL_INITIALIZED_P (r
) = 0;
10675 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
10676 if (type
== error_mark_node
)
10677 RETURN (error_mark_node
);
10678 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10680 /* It may seem that this case cannot occur, since:
10685 declares a function, not a variable. However:
10688 template <typename T> void g() { T t; }
10689 template void g<f>();
10691 is an attempt to declare a variable with function
10693 error ("variable %qD has function type",
10694 /* R is not yet sufficiently initialized, so we
10695 just use its name. */
10697 RETURN (error_mark_node
);
10699 type
= complete_type (type
);
10700 /* Wait until cp_finish_decl to set this again, to handle
10701 circular dependency (template/instantiate6.C). */
10702 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
10703 type
= check_var_type (DECL_NAME (r
), type
);
10705 if (DECL_HAS_VALUE_EXPR_P (t
))
10707 tree ve
= DECL_VALUE_EXPR (t
);
10708 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
10709 /*constant_expression_p=*/false);
10710 if (REFERENCE_REF_P (ve
))
10712 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
10713 ve
= TREE_OPERAND (ve
, 0);
10715 SET_DECL_VALUE_EXPR (r
, ve
);
10718 else if (DECL_SELF_REFERENCE_P (t
))
10719 SET_DECL_SELF_REFERENCE_P (r
);
10720 TREE_TYPE (r
) = type
;
10721 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10722 DECL_CONTEXT (r
) = ctx
;
10723 /* Clear out the mangled name and RTL for the instantiation. */
10724 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10725 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10726 SET_DECL_RTL (r
, NULL
);
10727 /* The initializer must not be expanded until it is required;
10728 see [temp.inst]. */
10729 DECL_INITIAL (r
) = NULL_TREE
;
10730 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10731 SET_DECL_RTL (r
, NULL
);
10732 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
10733 if (TREE_CODE (r
) == VAR_DECL
)
10735 /* Possibly limit visibility based on template args. */
10736 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10737 if (DECL_VISIBILITY_SPECIFIED (t
))
10739 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10740 DECL_ATTRIBUTES (r
)
10741 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10743 determine_visibility (r
);
10748 /* A static data member declaration is always marked
10749 external when it is declared in-class, even if an
10750 initializer is present. We mimic the non-template
10751 processing here. */
10752 DECL_EXTERNAL (r
) = 1;
10754 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10755 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
10756 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10758 else if (cp_unevaluated_operand
)
10760 /* We're substituting this var in a decltype outside of its
10761 scope, such as for a lambda return type. Don't add it to
10762 local_specializations, do perform auto deduction. */
10763 tree auto_node
= type_uses_auto (type
);
10767 = tsubst_expr (DECL_INITIAL (t
), args
, complain
, in_decl
,
10768 /*constant_expression_p=*/false);
10769 init
= resolve_nondeduced_context (init
);
10770 TREE_TYPE (r
) = type
10771 = do_auto_deduction (type
, init
, auto_node
);
10775 register_local_specialization (r
, t
);
10777 DECL_CHAIN (r
) = NULL_TREE
;
10779 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
10781 args
, complain
, in_decl
);
10783 /* Preserve a typedef that names a type. */
10784 if (is_typedef_decl (r
))
10786 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
10787 set_underlying_type (r
);
10790 layout_decl (r
, 0);
10795 gcc_unreachable ();
10800 /* Restore the file and line information. */
10801 input_location
= saved_loc
;
10806 /* Substitute into the ARG_TYPES of a function type.
10807 If END is a TREE_CHAIN, leave it and any following types
10811 tsubst_arg_types (tree arg_types
,
10814 tsubst_flags_t complain
,
10817 tree remaining_arg_types
;
10818 tree type
= NULL_TREE
;
10820 tree expanded_args
= NULL_TREE
;
10823 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
10826 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
10827 args
, end
, complain
, in_decl
);
10828 if (remaining_arg_types
== error_mark_node
)
10829 return error_mark_node
;
10831 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
10833 /* For a pack expansion, perform substitution on the
10834 entire expression. Later on, we'll handle the arguments
10836 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
10837 args
, complain
, in_decl
);
10839 if (TREE_CODE (expanded_args
) == TREE_VEC
)
10840 /* So that we'll spin through the parameters, one by one. */
10841 i
= TREE_VEC_LENGTH (expanded_args
);
10844 /* We only partially substituted into the parameter
10845 pack. Our type is TYPE_PACK_EXPANSION. */
10846 type
= expanded_args
;
10847 expanded_args
= NULL_TREE
;
10855 type
= TREE_VEC_ELT (expanded_args
, i
);
10857 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
10859 if (type
== error_mark_node
)
10860 return error_mark_node
;
10861 if (VOID_TYPE_P (type
))
10863 if (complain
& tf_error
)
10865 error ("invalid parameter type %qT", type
);
10867 error ("in declaration %q+D", in_decl
);
10869 return error_mark_node
;
10872 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
10873 return error_mark_node
;
10875 /* Do array-to-pointer, function-to-pointer conversion, and ignore
10876 top-level qualifiers as required. */
10877 type
= cv_unqualified (type_decays_to (type
));
10879 /* We do not substitute into default arguments here. The standard
10880 mandates that they be instantiated only when needed, which is
10881 done in build_over_call. */
10882 default_arg
= TREE_PURPOSE (arg_types
);
10884 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
10886 /* We've instantiated a template before its default arguments
10887 have been parsed. This can happen for a nested template
10888 class, and is not an error unless we require the default
10889 argument in a call of this function. */
10890 remaining_arg_types
=
10891 tree_cons (default_arg
, type
, remaining_arg_types
);
10892 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
10895 remaining_arg_types
=
10896 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
10899 return remaining_arg_types
;
10902 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
10903 *not* handle the exception-specification for FNTYPE, because the
10904 initial substitution of explicitly provided template parameters
10905 during argument deduction forbids substitution into the
10906 exception-specification:
10910 All references in the function type of the function template to the
10911 corresponding template parameters are replaced by the specified tem-
10912 plate argument values. If a substitution in a template parameter or
10913 in the function type of the function template results in an invalid
10914 type, type deduction fails. [Note: The equivalent substitution in
10915 exception specifications is done only when the function is instanti-
10916 ated, at which point a program is ill-formed if the substitution
10917 results in an invalid type.] */
10920 tsubst_function_type (tree t
,
10922 tsubst_flags_t complain
,
10929 /* The TYPE_CONTEXT is not used for function/method types. */
10930 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
10932 /* Substitute the return type. */
10933 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10934 if (return_type
== error_mark_node
)
10935 return error_mark_node
;
10936 /* DR 486 clarifies that creation of a function type with an
10937 invalid return type is a deduction failure. */
10938 if (TREE_CODE (return_type
) == ARRAY_TYPE
10939 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
10941 if (complain
& tf_error
)
10943 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
10944 error ("function returning an array");
10946 error ("function returning a function");
10948 return error_mark_node
;
10951 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
10952 return error_mark_node
;
10954 /* Substitute the argument types. */
10955 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
10956 complain
, in_decl
);
10957 if (arg_types
== error_mark_node
)
10958 return error_mark_node
;
10960 /* Construct a new type node and return it. */
10961 if (TREE_CODE (t
) == FUNCTION_TYPE
)
10963 fntype
= build_function_type (return_type
, arg_types
);
10964 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
10968 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
10969 if (! MAYBE_CLASS_TYPE_P (r
))
10973 Type deduction may fail for any of the following
10976 -- Attempting to create "pointer to member of T" when T
10977 is not a class type. */
10978 if (complain
& tf_error
)
10979 error ("creating pointer to member function of non-class type %qT",
10981 return error_mark_node
;
10984 fntype
= build_method_type_directly (r
, return_type
,
10985 TREE_CHAIN (arg_types
));
10987 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
10992 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
10993 ARGS into that specification, and return the substituted
10994 specification. If there is no specification, return NULL_TREE. */
10997 tsubst_exception_specification (tree fntype
,
10999 tsubst_flags_t complain
,
11006 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
11007 new_specs
= NULL_TREE
;
11008 if (specs
&& TREE_PURPOSE (specs
))
11010 /* A noexcept-specifier. */
11011 tree expr
= TREE_PURPOSE (specs
);
11012 if (TREE_CODE (expr
) == INTEGER_CST
)
11016 /* Defer instantiation of noexcept-specifiers to avoid
11017 excessive instantiations (c++/49107). */
11018 new_specs
= make_node (DEFERRED_NOEXCEPT
);
11019 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
11021 /* We already partially instantiated this member template,
11022 so combine the new args with the old. */
11023 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
11024 = DEFERRED_NOEXCEPT_PATTERN (expr
);
11025 DEFERRED_NOEXCEPT_ARGS (new_specs
)
11026 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
11030 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
11031 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
11035 new_specs
= tsubst_copy_and_build
11036 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
11037 /*integral_constant_expression_p=*/true);
11038 new_specs
= build_noexcept_spec (new_specs
, complain
);
11042 if (! TREE_VALUE (specs
))
11049 tree expanded_specs
= NULL_TREE
;
11051 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
11053 /* Expand the pack expansion type. */
11054 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
11058 if (expanded_specs
== error_mark_node
)
11059 return error_mark_node
;
11060 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
11061 len
= TREE_VEC_LENGTH (expanded_specs
);
11064 /* We're substituting into a member template, so
11065 we got a TYPE_PACK_EXPANSION back. Add that
11066 expansion and move on. */
11067 gcc_assert (TREE_CODE (expanded_specs
)
11068 == TYPE_PACK_EXPANSION
);
11069 new_specs
= add_exception_specifier (new_specs
,
11072 specs
= TREE_CHAIN (specs
);
11077 for (i
= 0; i
< len
; ++i
)
11079 if (expanded_specs
)
11080 spec
= TREE_VEC_ELT (expanded_specs
, i
);
11082 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
11083 if (spec
== error_mark_node
)
11085 new_specs
= add_exception_specifier (new_specs
, spec
,
11089 specs
= TREE_CHAIN (specs
);
11095 /* Take the tree structure T and replace template parameters used
11096 therein with the argument vector ARGS. IN_DECL is an associated
11097 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11098 Issue error and warning messages under control of COMPLAIN. Note
11099 that we must be relatively non-tolerant of extensions here, in
11100 order to preserve conformance; if we allow substitutions that
11101 should not be allowed, we may allow argument deductions that should
11102 not succeed, and therefore report ambiguous overload situations
11103 where there are none. In theory, we could allow the substitution,
11104 but indicate that it should have failed, and allow our caller to
11105 make sure that the right thing happens, but we don't try to do this
11108 This function is used for dealing with types, decls and the like;
11109 for expressions, use tsubst_expr or tsubst_copy. */
11112 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11114 enum tree_code code
;
11115 tree type
, r
= NULL_TREE
;
11117 if (t
== NULL_TREE
|| t
== error_mark_node
11118 || t
== integer_type_node
11119 || t
== void_type_node
11120 || t
== char_type_node
11121 || t
== unknown_type_node
11122 || TREE_CODE (t
) == NAMESPACE_DECL
11123 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
11127 return tsubst_decl (t
, args
, complain
);
11129 if (args
== NULL_TREE
)
11132 code
= TREE_CODE (t
);
11134 if (code
== IDENTIFIER_NODE
)
11135 type
= IDENTIFIER_TYPE_VALUE (t
);
11137 type
= TREE_TYPE (t
);
11139 gcc_assert (type
!= unknown_type_node
);
11141 /* Reuse typedefs. We need to do this to handle dependent attributes,
11142 such as attribute aligned. */
11144 && typedef_variant_p (t
))
11146 tree decl
= TYPE_NAME (t
);
11148 if (alias_template_specialization_p (t
))
11150 /* DECL represents an alias template and we want to
11152 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11153 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11154 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
11156 else if (DECL_CLASS_SCOPE_P (decl
)
11157 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11158 && uses_template_parms (DECL_CONTEXT (decl
)))
11160 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11161 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11162 r
= retrieve_specialization (tmpl
, gen_args
, 0);
11164 else if (DECL_FUNCTION_SCOPE_P (decl
)
11165 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11166 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
11167 r
= retrieve_local_specialization (decl
);
11169 /* The typedef is from a non-template context. */
11175 r
= cp_build_qualified_type_real
11176 (r
, cp_type_quals (t
) | cp_type_quals (r
),
11177 complain
| tf_ignore_bad_quals
);
11182 /* We don't have an instantiation yet, so drop the typedef. */
11183 int quals
= cp_type_quals (t
);
11184 t
= DECL_ORIGINAL_TYPE (decl
);
11185 t
= cp_build_qualified_type_real (t
, quals
,
11186 complain
| tf_ignore_bad_quals
);
11191 && code
!= TYPENAME_TYPE
11192 && code
!= TEMPLATE_TYPE_PARM
11193 && code
!= IDENTIFIER_NODE
11194 && code
!= FUNCTION_TYPE
11195 && code
!= METHOD_TYPE
)
11196 type
= tsubst (type
, args
, complain
, in_decl
);
11197 if (type
== error_mark_node
)
11198 return error_mark_node
;
11204 case ENUMERAL_TYPE
:
11205 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
11206 /*entering_scope=*/0);
11209 case IDENTIFIER_NODE
:
11220 if (t
== integer_type_node
)
11223 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
11224 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
11228 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
11230 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
11231 /*integral_constant_expression_p=*/false);
11233 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11235 if (TREE_CODE (max
) == NOP_EXPR
11236 && TREE_SIDE_EFFECTS (omax
)
11237 && !TREE_TYPE (max
))
11238 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
11240 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11241 with TREE_SIDE_EFFECTS that indicates this is not an integral
11242 constant expression. */
11243 if (processing_template_decl
11244 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
11246 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
11247 TREE_SIDE_EFFECTS (max
) = 1;
11250 return compute_array_index_type (NULL_TREE
, max
, complain
);
11253 case TEMPLATE_TYPE_PARM
:
11254 case TEMPLATE_TEMPLATE_PARM
:
11255 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11256 case TEMPLATE_PARM_INDEX
:
11261 tree arg
= NULL_TREE
;
11265 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
11266 template_parm_level_and_index (t
, &level
, &idx
);
11268 levels
= TMPL_ARGS_DEPTH (args
);
11269 if (level
<= levels
)
11271 arg
= TMPL_ARG (args
, level
, idx
);
11273 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
11275 /* See through ARGUMENT_PACK_SELECT arguments. */
11276 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
11277 /* If the selected argument is an expansion E, that most
11278 likely means we were called from
11279 gen_elem_of_pack_expansion_instantiation during the
11280 substituting of pack an argument pack (which Ith
11281 element is a pack expansion, where I is
11282 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11283 In this case, the Ith element resulting from this
11284 substituting is going to be a pack expansion, which
11285 pattern is the pattern of E. Let's return the
11287 gen_elem_of_pack_expansion_instantiation will
11288 build the resulting pack expansion from it. */
11289 if (PACK_EXPANSION_P (arg
))
11290 arg
= PACK_EXPANSION_PATTERN (arg
);
11294 if (arg
== error_mark_node
)
11295 return error_mark_node
;
11296 else if (arg
!= NULL_TREE
)
11298 if (ARGUMENT_PACK_P (arg
))
11299 /* If ARG is an argument pack, we don't actually want to
11300 perform a substitution here, because substitutions
11301 for argument packs are only done
11302 element-by-element. We can get to this point when
11303 substituting the type of a non-type template
11304 parameter pack, when that type actually contains
11305 template parameter packs from an outer template, e.g.,
11307 template<typename... Types> struct A {
11308 template<Types... Values> struct B { };
11312 if (code
== TEMPLATE_TYPE_PARM
)
11315 gcc_assert (TYPE_P (arg
));
11317 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
11319 return cp_build_qualified_type_real
11320 (arg
, quals
, complain
| tf_ignore_bad_quals
);
11322 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11324 /* We are processing a type constructed from a
11325 template template parameter. */
11326 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
11327 args
, complain
, in_decl
);
11328 if (argvec
== error_mark_node
)
11329 return error_mark_node
;
11331 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
11332 || TREE_CODE (arg
) == TEMPLATE_DECL
11333 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
11335 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
11336 /* Consider this code:
11338 template <template <class> class Template>
11340 template <class Arg> using Bind = Template<Arg>;
11343 template <template <class> class Template, class Arg>
11344 using Instantiate = Template<Arg>; //#0
11346 template <template <class> class Template,
11349 Instantiate<Internal<Template>::template Bind,
11352 When #1 is parsed, the
11353 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11354 parameter `Template' in #0 matches the
11355 UNBOUND_CLASS_TEMPLATE representing the argument
11356 `Internal<Template>::template Bind'; We then want
11357 to assemble the type `Bind<Argument>' that can't
11358 be fully created right now, because
11359 `Internal<Template>' not being complete, the Bind
11360 template cannot be looked up in that context. So
11361 we need to "store" `Bind<Argument>' for later
11362 when the context of Bind becomes complete. Let's
11363 store that in a TYPENAME_TYPE. */
11364 return make_typename_type (TYPE_CONTEXT (arg
),
11365 build_nt (TEMPLATE_ID_EXPR
,
11366 TYPE_IDENTIFIER (arg
),
11371 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11372 are resolving nested-types in the signature of a
11373 member function templates. Otherwise ARG is a
11374 TEMPLATE_DECL and is the real template to be
11376 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
11377 arg
= TYPE_NAME (arg
);
11379 r
= lookup_template_class (arg
,
11381 DECL_CONTEXT (arg
),
11382 /*entering_scope=*/0,
11384 return cp_build_qualified_type_real
11385 (r
, cp_type_quals (t
), complain
);
11388 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11389 return convert_from_reference (unshare_expr (arg
));
11393 /* This can happen during the attempted tsubst'ing in
11394 unify. This means that we don't yet have any information
11395 about the template parameter in question. */
11398 /* Early in template argument deduction substitution, we don't
11399 want to reduce the level of 'auto', or it will be confused
11400 with a normal template parm in subsequent deduction. */
11401 if (is_auto (t
) && (complain
& tf_partial
))
11404 /* If we get here, we must have been looking at a parm for a
11405 more deeply nested template. Make a new version of this
11406 template parameter, but with a lower level. */
11409 case TEMPLATE_TYPE_PARM
:
11410 case TEMPLATE_TEMPLATE_PARM
:
11411 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11412 if (cp_type_quals (t
))
11414 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
11415 r
= cp_build_qualified_type_real
11416 (r
, cp_type_quals (t
),
11417 complain
| (code
== TEMPLATE_TYPE_PARM
11418 ? tf_ignore_bad_quals
: 0));
11423 TEMPLATE_TYPE_PARM_INDEX (r
)
11424 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
11425 r
, levels
, args
, complain
);
11426 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
11427 TYPE_MAIN_VARIANT (r
) = r
;
11428 TYPE_POINTER_TO (r
) = NULL_TREE
;
11429 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
11431 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
11432 /* We have reduced the level of the template
11433 template parameter, but not the levels of its
11434 template parameters, so canonical_type_parameter
11435 will not be able to find the canonical template
11436 template parameter for this level. Thus, we
11437 require structural equality checking to compare
11438 TEMPLATE_TEMPLATE_PARMs. */
11439 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11440 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
11441 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11443 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
11445 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11447 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
11448 complain
, in_decl
);
11449 if (argvec
== error_mark_node
)
11450 return error_mark_node
;
11452 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
11453 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
11458 case TEMPLATE_PARM_INDEX
:
11459 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
11463 gcc_unreachable ();
11471 tree purpose
, value
, chain
;
11473 if (t
== void_list_node
)
11476 purpose
= TREE_PURPOSE (t
);
11479 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
11480 if (purpose
== error_mark_node
)
11481 return error_mark_node
;
11483 value
= TREE_VALUE (t
);
11486 value
= tsubst (value
, args
, complain
, in_decl
);
11487 if (value
== error_mark_node
)
11488 return error_mark_node
;
11490 chain
= TREE_CHAIN (t
);
11491 if (chain
&& chain
!= void_type_node
)
11493 chain
= tsubst (chain
, args
, complain
, in_decl
);
11494 if (chain
== error_mark_node
)
11495 return error_mark_node
;
11497 if (purpose
== TREE_PURPOSE (t
)
11498 && value
== TREE_VALUE (t
)
11499 && chain
== TREE_CHAIN (t
))
11501 return hash_tree_cons (purpose
, value
, chain
);
11505 /* We should never be tsubsting a binfo. */
11506 gcc_unreachable ();
11509 /* A vector of template arguments. */
11510 gcc_assert (!type
);
11511 return tsubst_template_args (t
, args
, complain
, in_decl
);
11514 case REFERENCE_TYPE
:
11516 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
11521 Type deduction may fail for any of the following
11524 -- Attempting to create a pointer to reference type.
11525 -- Attempting to create a reference to a reference type or
11526 a reference to void.
11528 Core issue 106 says that creating a reference to a reference
11529 during instantiation is no longer a cause for failure. We
11530 only enforce this check in strict C++98 mode. */
11531 if ((TREE_CODE (type
) == REFERENCE_TYPE
11532 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
11533 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
11535 static location_t last_loc
;
11537 /* We keep track of the last time we issued this error
11538 message to avoid spewing a ton of messages during a
11539 single bad template instantiation. */
11540 if (complain
& tf_error
11541 && last_loc
!= input_location
)
11543 if (TREE_CODE (type
) == VOID_TYPE
)
11544 error ("forming reference to void");
11545 else if (code
== POINTER_TYPE
)
11546 error ("forming pointer to reference type %qT", type
);
11548 error ("forming reference to reference type %qT", type
);
11549 last_loc
= input_location
;
11552 return error_mark_node
;
11554 else if (code
== POINTER_TYPE
)
11556 r
= build_pointer_type (type
);
11557 if (TREE_CODE (type
) == METHOD_TYPE
)
11558 r
= build_ptrmemfunc_type (r
);
11560 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
11561 /* In C++0x, during template argument substitution, when there is an
11562 attempt to create a reference to a reference type, reference
11563 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11565 "If a template-argument for a template-parameter T names a type
11566 that is a reference to a type A, an attempt to create the type
11567 'lvalue reference to cv T' creates the type 'lvalue reference to
11568 A,' while an attempt to create the type type rvalue reference to
11569 cv T' creates the type T"
11571 r
= cp_build_reference_type
11573 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
11575 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
11576 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11578 if (r
!= error_mark_node
)
11579 /* Will this ever be needed for TYPE_..._TO values? */
11586 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
11587 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
11591 Type deduction may fail for any of the following
11594 -- Attempting to create "pointer to member of T" when T
11595 is not a class type. */
11596 if (complain
& tf_error
)
11597 error ("creating pointer to member of non-class type %qT", r
);
11598 return error_mark_node
;
11600 if (TREE_CODE (type
) == REFERENCE_TYPE
)
11602 if (complain
& tf_error
)
11603 error ("creating pointer to member reference type %qT", type
);
11604 return error_mark_node
;
11606 if (TREE_CODE (type
) == VOID_TYPE
)
11608 if (complain
& tf_error
)
11609 error ("creating pointer to member of type void");
11610 return error_mark_node
;
11612 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
11613 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11615 /* The type of the implicit object parameter gets its
11616 cv-qualifiers from the FUNCTION_TYPE. */
11618 tree method_type
= build_memfn_type (type
, r
, type_memfn_quals (type
));
11619 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
11620 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
11624 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
11628 case FUNCTION_TYPE
:
11633 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
11634 if (fntype
== error_mark_node
)
11635 return error_mark_node
;
11637 /* Substitute the exception specification. */
11638 specs
= tsubst_exception_specification (t
, args
, complain
,
11639 in_decl
, /*defer_ok*/true);
11640 if (specs
== error_mark_node
)
11641 return error_mark_node
;
11643 fntype
= build_exception_variant (fntype
, specs
);
11648 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
11649 if (domain
== error_mark_node
)
11650 return error_mark_node
;
11652 /* As an optimization, we avoid regenerating the array type if
11653 it will obviously be the same as T. */
11654 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
11657 /* These checks should match the ones in grokdeclarator.
11661 The deduction may fail for any of the following reasons:
11663 -- Attempting to create an array with an element type that
11664 is void, a function type, or a reference type, or [DR337]
11665 an abstract class type. */
11666 if (TREE_CODE (type
) == VOID_TYPE
11667 || TREE_CODE (type
) == FUNCTION_TYPE
11668 || TREE_CODE (type
) == REFERENCE_TYPE
)
11670 if (complain
& tf_error
)
11671 error ("creating array of %qT", type
);
11672 return error_mark_node
;
11675 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
11676 return error_mark_node
;
11678 r
= build_cplus_array_type (type
, domain
);
11680 if (TYPE_USER_ALIGN (t
))
11682 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
11683 TYPE_USER_ALIGN (r
) = 1;
11689 case TYPENAME_TYPE
:
11691 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11692 in_decl
, /*entering_scope=*/1);
11693 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
11694 complain
, in_decl
);
11696 if (ctx
== error_mark_node
|| f
== error_mark_node
)
11697 return error_mark_node
;
11699 if (!MAYBE_CLASS_TYPE_P (ctx
))
11701 if (complain
& tf_error
)
11702 error ("%qT is not a class, struct, or union type", ctx
);
11703 return error_mark_node
;
11705 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
11707 /* Normally, make_typename_type does not require that the CTX
11708 have complete type in order to allow things like:
11710 template <class T> struct S { typename S<T>::X Y; };
11712 But, such constructs have already been resolved by this
11713 point, so here CTX really should have complete type, unless
11714 it's a partial instantiation. */
11715 ctx
= complete_type (ctx
);
11716 if (!COMPLETE_TYPE_P (ctx
))
11718 if (complain
& tf_error
)
11719 cxx_incomplete_type_error (NULL_TREE
, ctx
);
11720 return error_mark_node
;
11724 f
= make_typename_type (ctx
, f
, typename_type
,
11725 complain
| tf_keep_type_decl
);
11726 if (f
== error_mark_node
)
11728 if (TREE_CODE (f
) == TYPE_DECL
)
11730 complain
|= tf_ignore_bad_quals
;
11734 if (TREE_CODE (f
) != TYPENAME_TYPE
)
11736 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
11738 if (complain
& tf_error
)
11739 error ("%qT resolves to %qT, which is not an enumeration type",
11742 return error_mark_node
;
11744 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
11746 if (complain
& tf_error
)
11747 error ("%qT resolves to %qT, which is is not a class type",
11750 return error_mark_node
;
11754 return cp_build_qualified_type_real
11755 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
11758 case UNBOUND_CLASS_TEMPLATE
:
11760 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11761 in_decl
, /*entering_scope=*/1);
11762 tree name
= TYPE_IDENTIFIER (t
);
11763 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
11765 if (ctx
== error_mark_node
|| name
== error_mark_node
)
11766 return error_mark_node
;
11769 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
11770 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
11777 ++cp_unevaluated_operand
;
11778 ++c_inhibit_evaluation_warnings
;
11780 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
11782 /*integral_constant_expression_p=*/false);
11784 --cp_unevaluated_operand
;
11785 --c_inhibit_evaluation_warnings
;
11787 type
= finish_typeof (type
);
11788 return cp_build_qualified_type_real (type
,
11790 | cp_type_quals (type
),
11794 case DECLTYPE_TYPE
:
11798 ++cp_unevaluated_operand
;
11799 ++c_inhibit_evaluation_warnings
;
11801 type
= tsubst_expr (DECLTYPE_TYPE_EXPR (t
), args
,
11802 complain
|tf_decltype
, in_decl
,
11803 /*integral_constant_expression_p=*/false);
11805 --cp_unevaluated_operand
;
11806 --c_inhibit_evaluation_warnings
;
11808 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
11809 type
= lambda_capture_field_type (type
);
11810 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
11811 type
= lambda_proxy_type (type
);
11813 type
= finish_decltype_type
11814 (type
, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
), complain
);
11815 return cp_build_qualified_type_real (type
,
11817 | cp_type_quals (type
),
11821 case UNDERLYING_TYPE
:
11823 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
11824 complain
, in_decl
);
11825 return finish_underlying_type (type
);
11828 case TYPE_ARGUMENT_PACK
:
11829 case NONTYPE_ARGUMENT_PACK
:
11831 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
11833 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
11837 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
11839 /* For template nontype argument packs, also substitute into
11841 if (code
== NONTYPE_ARGUMENT_PACK
)
11842 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11860 /* We should use one of the expression tsubsts for these codes. */
11861 gcc_unreachable ();
11864 sorry ("use of %qs in template", tree_code_name
[(int) code
]);
11865 return error_mark_node
;
11869 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
11870 type of the expression on the left-hand side of the "." or "->"
11874 tsubst_baselink (tree baselink
, tree object_type
,
11875 tree args
, tsubst_flags_t complain
, tree in_decl
)
11878 tree qualifying_scope
;
11881 tree template_args
= 0;
11882 bool template_id_p
= false;
11883 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
11885 /* A baselink indicates a function from a base class. Both the
11886 BASELINK_ACCESS_BINFO and the base class referenced may
11887 indicate bases of the template class, rather than the
11888 instantiated class. In addition, lookups that were not
11889 ambiguous before may be ambiguous now. Therefore, we perform
11890 the lookup again. */
11891 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
11892 qualifying_scope
= tsubst (qualifying_scope
, args
,
11893 complain
, in_decl
);
11894 fns
= BASELINK_FUNCTIONS (baselink
);
11895 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
11896 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
11898 template_id_p
= true;
11899 template_args
= TREE_OPERAND (fns
, 1);
11900 fns
= TREE_OPERAND (fns
, 0);
11902 template_args
= tsubst_template_args (template_args
, args
,
11903 complain
, in_decl
);
11905 name
= DECL_NAME (get_first_fn (fns
));
11906 if (IDENTIFIER_TYPENAME_P (name
))
11907 name
= mangle_conv_op_name_for_type (optype
);
11908 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
11910 return error_mark_node
;
11912 /* If lookup found a single function, mark it as used at this
11913 point. (If it lookup found multiple functions the one selected
11914 later by overload resolution will be marked as used at that
11916 if (BASELINK_P (baselink
))
11917 fns
= BASELINK_FUNCTIONS (baselink
);
11918 if (!template_id_p
&& !really_overloaded_fn (fns
))
11919 mark_used (OVL_CURRENT (fns
));
11921 /* Add back the template arguments, if present. */
11922 if (BASELINK_P (baselink
) && template_id_p
)
11923 BASELINK_FUNCTIONS (baselink
)
11924 = build_nt (TEMPLATE_ID_EXPR
,
11925 BASELINK_FUNCTIONS (baselink
),
11927 /* Update the conversion operator type. */
11928 BASELINK_OPTYPE (baselink
) = optype
;
11931 object_type
= current_class_type
;
11934 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
11940 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
11941 true if the qualified-id will be a postfix-expression in-and-of
11942 itself; false if more of the postfix-expression follows the
11943 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
11947 tsubst_qualified_id (tree qualified_id
, tree args
,
11948 tsubst_flags_t complain
, tree in_decl
,
11949 bool done
, bool address_p
)
11955 tree template_args
;
11956 location_t loc
= UNKNOWN_LOCATION
;
11958 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
11960 /* Figure out what name to look up. */
11961 name
= TREE_OPERAND (qualified_id
, 1);
11962 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
11964 is_template
= true;
11965 loc
= EXPR_LOCATION (name
);
11966 template_args
= TREE_OPERAND (name
, 1);
11968 template_args
= tsubst_template_args (template_args
, args
,
11969 complain
, in_decl
);
11970 name
= TREE_OPERAND (name
, 0);
11974 is_template
= false;
11975 template_args
= NULL_TREE
;
11978 /* Substitute into the qualifying scope. When there are no ARGS, we
11979 are just trying to simplify a non-dependent expression. In that
11980 case the qualifying scope may be dependent, and, in any case,
11981 substituting will not help. */
11982 scope
= TREE_OPERAND (qualified_id
, 0);
11985 scope
= tsubst (scope
, args
, complain
, in_decl
);
11986 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
11991 if (dependent_scope_p (scope
))
11994 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
11995 return build_qualified_name (NULL_TREE
, scope
, expr
,
11996 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
11999 if (!BASELINK_P (name
) && !DECL_P (expr
))
12001 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
12003 /* A BIT_NOT_EXPR is used to represent a destructor. */
12004 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
12006 error ("qualifying type %qT does not match destructor name ~%qT",
12007 scope
, TREE_OPERAND (expr
, 0));
12008 expr
= error_mark_node
;
12011 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
12012 /*is_type_p=*/0, false);
12015 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
12016 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
12017 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
12019 if (complain
& tf_error
)
12021 error ("dependent-name %qE is parsed as a non-type, but "
12022 "instantiation yields a type", qualified_id
);
12023 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
12025 return error_mark_node
;
12031 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
12033 /* Remember that there was a reference to this entity. */
12037 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
12039 if (complain
& tf_error
)
12040 qualified_name_lookup_error (scope
,
12041 TREE_OPERAND (qualified_id
, 1),
12042 expr
, input_location
);
12043 return error_mark_node
;
12047 expr
= lookup_template_function (expr
, template_args
);
12049 if (expr
== error_mark_node
&& complain
& tf_error
)
12050 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
12051 expr
, input_location
);
12052 else if (TYPE_P (scope
))
12054 expr
= (adjust_result_of_qualified_name_lookup
12055 (expr
, scope
, current_nonlambda_class_type ()));
12056 expr
= (finish_qualified_id_expr
12057 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
12058 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
12059 /*template_arg_p=*/false));
12062 /* Expressions do not generally have reference type. */
12063 if (TREE_CODE (expr
) != SCOPE_REF
12064 /* However, if we're about to form a pointer-to-member, we just
12065 want the referenced member referenced. */
12066 && TREE_CODE (expr
) != OFFSET_REF
)
12067 expr
= convert_from_reference (expr
);
12072 /* Like tsubst, but deals with expressions. This function just replaces
12073 template parms; to finish processing the resultant expression, use
12074 tsubst_copy_and_build or tsubst_expr. */
12077 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12079 enum tree_code code
;
12082 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
12085 code
= TREE_CODE (t
);
12090 r
= retrieve_local_specialization (t
);
12092 if (r
== NULL_TREE
)
12094 /* We get here for a use of 'this' in an NSDMI. */
12095 if (DECL_NAME (t
) == this_identifier
12096 && at_function_scope_p ()
12097 && DECL_CONSTRUCTOR_P (current_function_decl
))
12098 return current_class_ptr
;
12100 /* This can happen for a parameter name used later in a function
12101 declaration (such as in a late-specified return type). Just
12102 make a dummy decl, since it's only used for its type. */
12103 gcc_assert (cp_unevaluated_operand
!= 0);
12104 r
= tsubst_decl (t
, args
, complain
);
12105 /* Give it the template pattern as its context; its true context
12106 hasn't been instantiated yet and this is good enough for
12108 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
12111 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12112 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12121 if (DECL_TEMPLATE_PARM_P (t
))
12122 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
12123 /* There is no need to substitute into namespace-scope
12125 if (DECL_NAMESPACE_SCOPE_P (t
))
12127 /* If ARGS is NULL, then T is known to be non-dependent. */
12128 if (args
== NULL_TREE
)
12129 return integral_constant_value (t
);
12131 /* Unfortunately, we cannot just call lookup_name here.
12134 template <int I> int f() {
12136 struct S { void g() { E e = a; } };
12139 When we instantiate f<7>::S::g(), say, lookup_name is not
12140 clever enough to find f<7>::a. */
12142 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12143 /*entering_scope=*/0);
12145 for (v
= TYPE_VALUES (enum_type
);
12147 v
= TREE_CHAIN (v
))
12148 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
12149 return TREE_VALUE (v
);
12151 /* We didn't find the name. That should never happen; if
12152 name-lookup found it during preliminary parsing, we
12153 should find it again here during instantiation. */
12154 gcc_unreachable ();
12159 if (DECL_CONTEXT (t
))
12163 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12164 /*entering_scope=*/1);
12165 if (ctx
!= DECL_CONTEXT (t
))
12167 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
12170 if (complain
& tf_error
)
12171 error ("using invalid field %qD", t
);
12172 return error_mark_node
;
12181 case FUNCTION_DECL
:
12182 if ((DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
12183 || local_variable_p (t
))
12184 t
= tsubst (t
, args
, complain
, in_decl
);
12188 case NAMESPACE_DECL
:
12192 /* An OVERLOAD will always be a non-dependent overload set; an
12193 overload set from function scope will just be represented with an
12194 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12195 gcc_assert (!uses_template_parms (t
));
12199 return tsubst_baselink (t
, current_class_type
, args
, complain
, in_decl
);
12201 case TEMPLATE_DECL
:
12202 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12203 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
12204 args
, complain
, in_decl
);
12205 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
12206 return tsubst (t
, args
, complain
, in_decl
);
12207 else if (DECL_CLASS_SCOPE_P (t
)
12208 && uses_template_parms (DECL_CONTEXT (t
)))
12210 /* Template template argument like the following example need
12213 template <template <class> class TT> struct C {};
12214 template <class T> struct D {
12215 template <class U> struct E {};
12220 We are processing the template argument `E' in #1 for
12221 the template instantiation #2. Originally, `E' is a
12222 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12223 have to substitute this with one having context `D<int>'. */
12225 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
12226 return lookup_field (context
, DECL_NAME(t
), 0, false);
12229 /* Ordinary template template argument. */
12233 case REINTERPRET_CAST_EXPR
:
12234 case CONST_CAST_EXPR
:
12235 case STATIC_CAST_EXPR
:
12236 case DYNAMIC_CAST_EXPR
:
12237 case IMPLICIT_CONV_EXPR
:
12241 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12242 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12245 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
12248 tree expanded
, op
= TREE_OPERAND (t
, 0);
12251 if (SIZEOF_EXPR_TYPE_P (t
))
12252 op
= TREE_TYPE (op
);
12254 ++cp_unevaluated_operand
;
12255 ++c_inhibit_evaluation_warnings
;
12256 /* We only want to compute the number of arguments. */
12257 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
12258 --cp_unevaluated_operand
;
12259 --c_inhibit_evaluation_warnings
;
12261 if (TREE_CODE (expanded
) == TREE_VEC
)
12262 len
= TREE_VEC_LENGTH (expanded
);
12264 if (expanded
== error_mark_node
)
12265 return error_mark_node
;
12266 else if (PACK_EXPANSION_P (expanded
)
12267 || (TREE_CODE (expanded
) == TREE_VEC
12269 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
12271 if (TREE_CODE (expanded
) == TREE_VEC
)
12272 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
12274 if (TYPE_P (expanded
))
12275 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
12276 complain
& tf_error
);
12278 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
12279 complain
& tf_error
);
12282 return build_int_cst (size_type_node
, len
);
12284 if (SIZEOF_EXPR_TYPE_P (t
))
12286 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
12287 args
, complain
, in_decl
);
12288 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
12289 r
= build1 (SIZEOF_EXPR
,
12290 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
12291 SIZEOF_EXPR_TYPE_P (r
) = 1;
12298 case TRUTH_NOT_EXPR
:
12301 case UNARY_PLUS_EXPR
: /* Unary + */
12303 case AT_ENCODE_EXPR
:
12307 case REALPART_EXPR
:
12308 case IMAGPART_EXPR
:
12310 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12311 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12313 case COMPONENT_REF
:
12318 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
12319 name
= TREE_OPERAND (t
, 1);
12320 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
12322 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12323 complain
, in_decl
);
12324 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12326 else if (TREE_CODE (name
) == SCOPE_REF
12327 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
12329 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12330 complain
, in_decl
);
12331 name
= TREE_OPERAND (name
, 1);
12332 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12333 complain
, in_decl
);
12334 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12335 name
= build_qualified_name (/*type=*/NULL_TREE
,
12337 /*template_p=*/false);
12339 else if (BASELINK_P (name
))
12340 name
= tsubst_baselink (name
,
12341 non_reference (TREE_TYPE (object
)),
12345 name
= tsubst_copy (name
, args
, complain
, in_decl
);
12346 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
12352 case TRUNC_DIV_EXPR
:
12353 case CEIL_DIV_EXPR
:
12354 case FLOOR_DIV_EXPR
:
12355 case ROUND_DIV_EXPR
:
12356 case EXACT_DIV_EXPR
:
12360 case TRUNC_MOD_EXPR
:
12361 case FLOOR_MOD_EXPR
:
12362 case TRUTH_ANDIF_EXPR
:
12363 case TRUTH_ORIF_EXPR
:
12364 case TRUTH_AND_EXPR
:
12365 case TRUTH_OR_EXPR
:
12378 case COMPOUND_EXPR
:
12381 case PREDECREMENT_EXPR
:
12382 case PREINCREMENT_EXPR
:
12383 case POSTDECREMENT_EXPR
:
12384 case POSTINCREMENT_EXPR
:
12386 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12387 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12390 return build_qualified_name (/*type=*/NULL_TREE
,
12391 tsubst_copy (TREE_OPERAND (t
, 0),
12392 args
, complain
, in_decl
),
12393 tsubst_copy (TREE_OPERAND (t
, 1),
12394 args
, complain
, in_decl
),
12395 QUALIFIED_NAME_IS_TEMPLATE (t
));
12400 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12401 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12402 NULL_TREE
, NULL_TREE
);
12406 int n
= VL_EXP_OPERAND_LENGTH (t
);
12407 tree result
= build_vl_exp (CALL_EXPR
, n
);
12409 for (i
= 0; i
< n
; i
++)
12410 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
12411 complain
, in_decl
);
12417 case PSEUDO_DTOR_EXPR
:
12420 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12421 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12422 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12423 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12430 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12431 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12432 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12433 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
12440 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12441 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12442 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
12443 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
12447 case TEMPLATE_ID_EXPR
:
12449 /* Substituted template arguments */
12450 tree fn
= TREE_OPERAND (t
, 0);
12451 tree targs
= TREE_OPERAND (t
, 1);
12453 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
12455 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12457 return lookup_template_function (fn
, targs
);
12462 tree purpose
, value
, chain
;
12464 if (t
== void_list_node
)
12467 purpose
= TREE_PURPOSE (t
);
12469 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
12470 value
= TREE_VALUE (t
);
12472 value
= tsubst_copy (value
, args
, complain
, in_decl
);
12473 chain
= TREE_CHAIN (t
);
12474 if (chain
&& chain
!= void_type_node
)
12475 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
12476 if (purpose
== TREE_PURPOSE (t
)
12477 && value
== TREE_VALUE (t
)
12478 && chain
== TREE_CHAIN (t
))
12480 return tree_cons (purpose
, value
, chain
);
12485 case ENUMERAL_TYPE
:
12487 case TEMPLATE_TYPE_PARM
:
12488 case TEMPLATE_TEMPLATE_PARM
:
12489 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12490 case TEMPLATE_PARM_INDEX
:
12492 case REFERENCE_TYPE
:
12494 case FUNCTION_TYPE
:
12497 case TYPENAME_TYPE
:
12498 case UNBOUND_CLASS_TEMPLATE
:
12500 case DECLTYPE_TYPE
:
12502 return tsubst (t
, args
, complain
, in_decl
);
12504 case IDENTIFIER_NODE
:
12505 if (IDENTIFIER_TYPENAME_P (t
))
12507 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12508 return mangle_conv_op_name_for_type (new_type
);
12514 /* This is handled by tsubst_copy_and_build. */
12515 gcc_unreachable ();
12518 return build_x_va_arg (EXPR_LOCATION (t
),
12519 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
12521 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
12523 case CLEANUP_POINT_EXPR
:
12524 /* We shouldn't have built any of these during initial template
12525 generation. Instead, they should be built during instantiation
12526 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12527 gcc_unreachable ();
12531 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12532 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12533 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12534 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
12535 mark_used (TREE_OPERAND (r
, 1));
12538 case EXPR_PACK_EXPANSION
:
12539 error ("invalid use of pack expansion expression");
12540 return error_mark_node
;
12542 case NONTYPE_ARGUMENT_PACK
:
12543 error ("use %<...%> to expand argument pack");
12544 return error_mark_node
;
12551 /* Instantiate any typedefs in the type. */
12552 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12553 r
= fold_convert (type
, t
);
12554 gcc_assert (TREE_CODE (r
) == code
);
12559 /* These can sometimes show up in a partial instantiation, but never
12560 involve template parms. */
12561 gcc_assert (!uses_template_parms (t
));
12565 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12566 gcc_checking_assert (false);
12571 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12574 tsubst_omp_clauses (tree clauses
, tree args
, tsubst_flags_t complain
,
12577 tree new_clauses
= NULL
, nc
, oc
;
12579 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
12581 nc
= copy_node (oc
);
12582 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
12585 switch (OMP_CLAUSE_CODE (nc
))
12587 case OMP_CLAUSE_LASTPRIVATE
:
12588 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
12590 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
12591 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
12592 in_decl
, /*integral_constant_expression_p=*/false);
12593 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
12594 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
12597 case OMP_CLAUSE_PRIVATE
:
12598 case OMP_CLAUSE_SHARED
:
12599 case OMP_CLAUSE_FIRSTPRIVATE
:
12600 case OMP_CLAUSE_REDUCTION
:
12601 case OMP_CLAUSE_COPYIN
:
12602 case OMP_CLAUSE_COPYPRIVATE
:
12603 case OMP_CLAUSE_IF
:
12604 case OMP_CLAUSE_NUM_THREADS
:
12605 case OMP_CLAUSE_SCHEDULE
:
12606 case OMP_CLAUSE_COLLAPSE
:
12607 case OMP_CLAUSE_FINAL
:
12608 OMP_CLAUSE_OPERAND (nc
, 0)
12609 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12610 in_decl
, /*integral_constant_expression_p=*/false);
12612 case OMP_CLAUSE_NOWAIT
:
12613 case OMP_CLAUSE_ORDERED
:
12614 case OMP_CLAUSE_DEFAULT
:
12615 case OMP_CLAUSE_UNTIED
:
12616 case OMP_CLAUSE_MERGEABLE
:
12619 gcc_unreachable ();
12623 return finish_omp_clauses (nreverse (new_clauses
));
12626 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12629 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
12632 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12634 tree purpose
, value
, chain
;
12639 if (TREE_CODE (t
) != TREE_LIST
)
12640 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12641 /*function_p=*/false,
12642 /*integral_constant_expression_p=*/false);
12644 if (t
== void_list_node
)
12647 purpose
= TREE_PURPOSE (t
);
12649 purpose
= RECUR (purpose
);
12650 value
= TREE_VALUE (t
);
12653 if (TREE_CODE (value
) != LABEL_DECL
)
12654 value
= RECUR (value
);
12657 value
= lookup_label (DECL_NAME (value
));
12658 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
12659 TREE_USED (value
) = 1;
12662 chain
= TREE_CHAIN (t
);
12663 if (chain
&& chain
!= void_type_node
)
12664 chain
= RECUR (chain
);
12665 return tree_cons (purpose
, value
, chain
);
12669 /* Substitute one OMP_FOR iterator. */
12672 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
12673 tree condv
, tree incrv
, tree
*clauses
,
12674 tree args
, tsubst_flags_t complain
, tree in_decl
,
12675 bool integral_constant_expression_p
)
12677 #define RECUR(NODE) \
12678 tsubst_expr ((NODE), args, complain, in_decl, \
12679 integral_constant_expression_p)
12680 tree decl
, init
, cond
, incr
;
12683 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
12684 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
12685 decl
= TREE_OPERAND (init
, 0);
12686 init
= TREE_OPERAND (init
, 1);
12687 /* Do this before substituting into decl to handle 'auto'. */
12688 init_decl
= (init
&& TREE_CODE (init
) == DECL_EXPR
);
12689 init
= RECUR (init
);
12690 decl
= RECUR (decl
);
12693 gcc_assert (!processing_template_decl
);
12694 init
= DECL_INITIAL (decl
);
12695 DECL_INITIAL (decl
) = NULL_TREE
;
12698 gcc_assert (!type_dependent_expression_p (decl
));
12700 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
12702 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
12703 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
12704 if (TREE_CODE (incr
) == MODIFY_EXPR
)
12705 incr
= build_x_modify_expr (EXPR_LOCATION (incr
),
12706 RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
12707 RECUR (TREE_OPERAND (incr
, 1)),
12710 incr
= RECUR (incr
);
12711 TREE_VEC_ELT (declv
, i
) = decl
;
12712 TREE_VEC_ELT (initv
, i
) = init
;
12713 TREE_VEC_ELT (condv
, i
) = cond
;
12714 TREE_VEC_ELT (incrv
, i
) = incr
;
12718 if (init
&& !init_decl
)
12721 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
12723 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
12724 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
12725 && OMP_CLAUSE_DECL (c
) == decl
)
12727 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
12728 && OMP_CLAUSE_DECL (c
) == decl
)
12729 error ("iteration variable %qD should not be firstprivate", decl
);
12730 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
12731 && OMP_CLAUSE_DECL (c
) == decl
)
12732 error ("iteration variable %qD should not be reduction", decl
);
12736 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
12737 OMP_CLAUSE_DECL (c
) = decl
;
12738 c
= finish_omp_clauses (c
);
12741 OMP_CLAUSE_CHAIN (c
) = *clauses
;
12746 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
12747 if (COMPARISON_CLASS_P (cond
))
12748 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
12749 RECUR (TREE_OPERAND (cond
, 0)),
12750 RECUR (TREE_OPERAND (cond
, 1)));
12752 cond
= RECUR (cond
);
12753 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
12754 switch (TREE_CODE (incr
))
12756 case PREINCREMENT_EXPR
:
12757 case PREDECREMENT_EXPR
:
12758 case POSTINCREMENT_EXPR
:
12759 case POSTDECREMENT_EXPR
:
12760 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
12761 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
12764 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
12765 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
12767 tree rhs
= TREE_OPERAND (incr
, 1);
12768 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
12769 RECUR (TREE_OPERAND (incr
, 0)),
12770 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
12771 RECUR (TREE_OPERAND (rhs
, 0)),
12772 RECUR (TREE_OPERAND (rhs
, 1))));
12775 incr
= RECUR (incr
);
12778 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
12779 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
12781 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
12782 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
12783 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
12784 TREE_TYPE (decl
), lhs
,
12785 RECUR (TREE_OPERAND (incr
, 2))));
12787 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
12788 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
12789 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
12791 tree rhs
= TREE_OPERAND (incr
, 2);
12792 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
12793 RECUR (TREE_OPERAND (incr
, 0)),
12794 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
12795 RECUR (TREE_OPERAND (rhs
, 0)),
12796 RECUR (TREE_OPERAND (rhs
, 1))));
12799 incr
= RECUR (incr
);
12802 incr
= RECUR (incr
);
12806 TREE_VEC_ELT (declv
, i
) = decl
;
12807 TREE_VEC_ELT (initv
, i
) = init
;
12808 TREE_VEC_ELT (condv
, i
) = cond
;
12809 TREE_VEC_ELT (incrv
, i
) = incr
;
12813 /* Like tsubst_copy for expressions, etc. but also does semantic
12817 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
12818 bool integral_constant_expression_p
)
12820 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12821 #define RECUR(NODE) \
12822 tsubst_expr ((NODE), args, complain, in_decl, \
12823 integral_constant_expression_p)
12829 if (t
== NULL_TREE
|| t
== error_mark_node
)
12832 loc
= input_location
;
12833 if (EXPR_HAS_LOCATION (t
))
12834 input_location
= EXPR_LOCATION (t
);
12835 if (STATEMENT_CODE_P (TREE_CODE (t
)))
12836 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
12838 switch (TREE_CODE (t
))
12840 case STATEMENT_LIST
:
12842 tree_stmt_iterator i
;
12843 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
12844 RECUR (tsi_stmt (i
));
12848 case CTOR_INITIALIZER
:
12849 finish_mem_initializers (tsubst_initializer_list
12850 (TREE_OPERAND (t
, 0), args
));
12854 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
12858 tmp
= RECUR (EXPR_STMT_EXPR (t
));
12859 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
12860 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
12862 finish_expr_stmt (tmp
);
12866 do_using_directive (USING_STMT_NAMESPACE (t
));
12871 tree decl
, pattern_decl
;
12874 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
12875 if (TREE_CODE (decl
) == LABEL_DECL
)
12876 finish_label_decl (DECL_NAME (decl
));
12877 else if (TREE_CODE (decl
) == USING_DECL
)
12879 tree scope
= USING_DECL_SCOPE (decl
);
12880 tree name
= DECL_NAME (decl
);
12883 scope
= tsubst (scope
, args
, complain
, in_decl
);
12884 decl
= lookup_qualified_name (scope
, name
,
12885 /*is_type_p=*/false,
12886 /*complain=*/false);
12887 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
12888 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
12890 do_local_using_decl (decl
, scope
, name
);
12894 init
= DECL_INITIAL (decl
);
12895 decl
= tsubst (decl
, args
, complain
, in_decl
);
12896 if (decl
!= error_mark_node
)
12898 /* By marking the declaration as instantiated, we avoid
12899 trying to instantiate it. Since instantiate_decl can't
12900 handle local variables, and since we've already done
12901 all that needs to be done, that's the right thing to
12903 if (TREE_CODE (decl
) == VAR_DECL
)
12904 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
12905 if (TREE_CODE (decl
) == VAR_DECL
12906 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
12907 /* Anonymous aggregates are a special case. */
12908 finish_anon_union (decl
);
12909 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
12911 DECL_CONTEXT (decl
) = current_function_decl
;
12912 if (DECL_NAME (decl
) == this_identifier
)
12914 tree lam
= DECL_CONTEXT (current_function_decl
);
12915 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
12916 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
12918 insert_capture_proxy (decl
);
12920 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
12921 /* We already did a pushtag. */;
12924 int const_init
= false;
12925 maybe_push_decl (decl
);
12926 if (TREE_CODE (decl
) == VAR_DECL
12927 && DECL_PRETTY_FUNCTION_P (decl
))
12929 /* For __PRETTY_FUNCTION__ we have to adjust the
12931 const char *const name
12932 = cxx_printable_name (current_function_decl
, 2);
12933 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
12937 tree t
= RECUR (init
);
12941 /* If we had an initializer but it
12942 instantiated to nothing,
12943 value-initialize the object. This will
12944 only occur when the initializer was a
12945 pack expansion where the parameter packs
12946 used in that expansion were of length
12948 init
= build_value_init (TREE_TYPE (decl
),
12950 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
12951 init
= get_target_expr_sfinae (init
, complain
);
12957 if (TREE_CODE (decl
) == VAR_DECL
)
12958 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
12960 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
12969 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12970 RECUR (FOR_INIT_STMT (t
));
12971 finish_for_init_stmt (stmt
);
12972 tmp
= RECUR (FOR_COND (t
));
12973 finish_for_cond (tmp
, stmt
);
12974 tmp
= RECUR (FOR_EXPR (t
));
12975 finish_for_expr (tmp
, stmt
);
12976 RECUR (FOR_BODY (t
));
12977 finish_for_stmt (stmt
);
12980 case RANGE_FOR_STMT
:
12983 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12984 decl
= RANGE_FOR_DECL (t
);
12985 decl
= tsubst (decl
, args
, complain
, in_decl
);
12986 maybe_push_decl (decl
);
12987 expr
= RECUR (RANGE_FOR_EXPR (t
));
12988 stmt
= cp_convert_range_for (stmt
, decl
, expr
);
12989 RECUR (RANGE_FOR_BODY (t
));
12990 finish_for_stmt (stmt
);
12995 stmt
= begin_while_stmt ();
12996 tmp
= RECUR (WHILE_COND (t
));
12997 finish_while_stmt_cond (tmp
, stmt
);
12998 RECUR (WHILE_BODY (t
));
12999 finish_while_stmt (stmt
);
13003 stmt
= begin_do_stmt ();
13004 RECUR (DO_BODY (t
));
13005 finish_do_body (stmt
);
13006 tmp
= RECUR (DO_COND (t
));
13007 finish_do_stmt (tmp
, stmt
);
13011 stmt
= begin_if_stmt ();
13012 tmp
= RECUR (IF_COND (t
));
13013 finish_if_stmt_cond (tmp
, stmt
);
13014 RECUR (THEN_CLAUSE (t
));
13015 finish_then_clause (stmt
);
13017 if (ELSE_CLAUSE (t
))
13019 begin_else_clause (stmt
);
13020 RECUR (ELSE_CLAUSE (t
));
13021 finish_else_clause (stmt
);
13024 finish_if_stmt (stmt
);
13028 if (BIND_EXPR_BODY_BLOCK (t
))
13029 stmt
= begin_function_body ();
13031 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
13032 ? BCS_TRY_BLOCK
: 0);
13034 RECUR (BIND_EXPR_BODY (t
));
13036 if (BIND_EXPR_BODY_BLOCK (t
))
13037 finish_function_body (stmt
);
13039 finish_compound_stmt (stmt
);
13043 finish_break_stmt ();
13046 case CONTINUE_STMT
:
13047 finish_continue_stmt ();
13051 stmt
= begin_switch_stmt ();
13052 tmp
= RECUR (SWITCH_STMT_COND (t
));
13053 finish_switch_cond (tmp
, stmt
);
13054 RECUR (SWITCH_STMT_BODY (t
));
13055 finish_switch_stmt (stmt
);
13058 case CASE_LABEL_EXPR
:
13059 finish_case_label (EXPR_LOCATION (t
),
13060 RECUR (CASE_LOW (t
)),
13061 RECUR (CASE_HIGH (t
)));
13066 tree decl
= LABEL_EXPR_LABEL (t
);
13069 label
= finish_label_stmt (DECL_NAME (decl
));
13070 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
13071 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
13076 tmp
= GOTO_DESTINATION (t
);
13077 if (TREE_CODE (tmp
) != LABEL_DECL
)
13078 /* Computed goto's must be tsubst'd into. On the other hand,
13079 non-computed gotos must not be; the identifier in question
13080 will have no binding. */
13083 tmp
= DECL_NAME (tmp
);
13084 finish_goto_stmt (tmp
);
13088 tmp
= finish_asm_stmt
13089 (ASM_VOLATILE_P (t
),
13090 RECUR (ASM_STRING (t
)),
13091 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
13092 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
13093 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
13094 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
13096 tree asm_expr
= tmp
;
13097 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
13098 asm_expr
= TREE_OPERAND (asm_expr
, 0);
13099 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
13106 stmt
= begin_try_block ();
13107 RECUR (TRY_STMTS (t
));
13108 finish_cleanup_try_block (stmt
);
13109 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
13113 tree compound_stmt
= NULL_TREE
;
13115 if (FN_TRY_BLOCK_P (t
))
13116 stmt
= begin_function_try_block (&compound_stmt
);
13118 stmt
= begin_try_block ();
13120 RECUR (TRY_STMTS (t
));
13122 if (FN_TRY_BLOCK_P (t
))
13123 finish_function_try_block (stmt
);
13125 finish_try_block (stmt
);
13127 RECUR (TRY_HANDLERS (t
));
13128 if (FN_TRY_BLOCK_P (t
))
13129 finish_function_handler_sequence (stmt
, compound_stmt
);
13131 finish_handler_sequence (stmt
);
13137 tree decl
= HANDLER_PARMS (t
);
13141 decl
= tsubst (decl
, args
, complain
, in_decl
);
13142 /* Prevent instantiate_decl from trying to instantiate
13143 this variable. We've already done all that needs to be
13145 if (decl
!= error_mark_node
)
13146 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13148 stmt
= begin_handler ();
13149 finish_handler_parms (decl
, stmt
);
13150 RECUR (HANDLER_BODY (t
));
13151 finish_handler (stmt
);
13156 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
13157 if (CLASS_TYPE_P (tmp
))
13159 /* Local classes are not independent templates; they are
13160 instantiated along with their containing function. And this
13161 way we don't have to deal with pushing out of one local class
13162 to instantiate a member of another local class. */
13164 /* Closures are handled by the LAMBDA_EXPR. */
13165 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
13166 complete_type (tmp
);
13167 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
13168 if (!DECL_ARTIFICIAL (fn
))
13169 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
13173 case STATIC_ASSERT
:
13177 ++c_inhibit_evaluation_warnings
;
13179 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
13182 /*integral_constant_expression_p=*/true);
13183 --c_inhibit_evaluation_warnings
;
13185 finish_static_assert (condition
,
13186 STATIC_ASSERT_MESSAGE (t
),
13187 STATIC_ASSERT_SOURCE_LOCATION (t
),
13188 /*member_p=*/false);
13193 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
),
13194 args
, complain
, in_decl
);
13195 stmt
= begin_omp_parallel ();
13196 RECUR (OMP_PARALLEL_BODY (t
));
13197 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
13198 = OMP_PARALLEL_COMBINED (t
);
13202 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
),
13203 args
, complain
, in_decl
);
13204 stmt
= begin_omp_task ();
13205 RECUR (OMP_TASK_BODY (t
));
13206 finish_omp_task (tmp
, stmt
);
13211 tree clauses
, body
, pre_body
;
13212 tree declv
, initv
, condv
, incrv
;
13215 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
),
13216 args
, complain
, in_decl
);
13217 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13218 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13219 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13220 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13222 stmt
= begin_omp_structured_block ();
13224 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
13225 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
13226 &clauses
, args
, complain
, in_decl
,
13227 integral_constant_expression_p
);
13229 pre_body
= push_stmt_list ();
13230 RECUR (OMP_FOR_PRE_BODY (t
));
13231 pre_body
= pop_stmt_list (pre_body
);
13233 body
= push_stmt_list ();
13234 RECUR (OMP_FOR_BODY (t
));
13235 body
= pop_stmt_list (body
);
13237 t
= finish_omp_for (EXPR_LOCATION (t
), declv
, initv
, condv
, incrv
,
13238 body
, pre_body
, clauses
);
13240 add_stmt (finish_omp_structured_block (stmt
));
13246 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), args
, complain
, in_decl
);
13247 stmt
= push_stmt_list ();
13248 RECUR (OMP_BODY (t
));
13249 stmt
= pop_stmt_list (stmt
);
13252 OMP_BODY (t
) = stmt
;
13253 OMP_CLAUSES (t
) = tmp
;
13261 stmt
= push_stmt_list ();
13262 RECUR (OMP_BODY (t
));
13263 stmt
= pop_stmt_list (stmt
);
13266 OMP_BODY (t
) = stmt
;
13271 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
13272 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
13274 tree op1
= TREE_OPERAND (t
, 1);
13275 tree rhs1
= NULL_TREE
;
13277 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
13279 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
13280 op1
= TREE_OPERAND (op1
, 1);
13282 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13283 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13284 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
13285 NULL_TREE
, NULL_TREE
, rhs1
);
13289 tree op1
= TREE_OPERAND (t
, 1);
13290 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
13291 tree rhs1
= NULL_TREE
;
13292 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
13293 enum tree_code opcode
= NOP_EXPR
;
13294 if (code
== OMP_ATOMIC_READ
)
13296 v
= RECUR (TREE_OPERAND (op1
, 0));
13297 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13299 else if (code
== OMP_ATOMIC_CAPTURE_OLD
13300 || code
== OMP_ATOMIC_CAPTURE_NEW
)
13302 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
13303 v
= RECUR (TREE_OPERAND (op1
, 0));
13304 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13305 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
13307 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
13308 op11
= TREE_OPERAND (op11
, 1);
13310 lhs
= RECUR (TREE_OPERAND (op11
, 0));
13311 rhs
= RECUR (TREE_OPERAND (op11
, 1));
13312 opcode
= TREE_CODE (op11
);
13317 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13318 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13320 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
);
13324 case TRANSACTION_EXPR
:
13327 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
13328 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
13330 if (TRANSACTION_EXPR_IS_STMT (t
))
13332 tree body
= TRANSACTION_EXPR_BODY (t
);
13333 tree noex
= NULL_TREE
;
13334 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
13336 noex
= MUST_NOT_THROW_COND (body
);
13337 if (noex
== NULL_TREE
)
13338 noex
= boolean_true_node
;
13339 body
= TREE_OPERAND (body
, 0);
13341 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
13343 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
13347 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
13348 RECUR (TRANSACTION_EXPR_BODY (t
)),
13355 case MUST_NOT_THROW_EXPR
:
13356 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t
, 0)),
13357 RECUR (MUST_NOT_THROW_COND (t
))));
13359 case EXPR_PACK_EXPANSION
:
13360 error ("invalid use of pack expansion expression");
13361 RETURN (error_mark_node
);
13363 case NONTYPE_ARGUMENT_PACK
:
13364 error ("use %<...%> to expand argument pack");
13365 RETURN (error_mark_node
);
13367 case COMPOUND_EXPR
:
13368 tmp
= RECUR (TREE_OPERAND (t
, 0));
13369 if (tmp
== NULL_TREE
)
13370 /* If the first operand was a statement, we're done with it. */
13371 RETURN (RECUR (TREE_OPERAND (t
, 1)));
13372 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
13373 RECUR (TREE_OPERAND (t
, 1)),
13377 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
13379 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13380 /*function_p=*/false,
13381 integral_constant_expression_p
));
13384 RETURN (NULL_TREE
);
13386 input_location
= loc
;
13392 /* T is a postfix-expression that is not being used in a function
13393 call. Return the substituted version of T. */
13396 tsubst_non_call_postfix_expression (tree t
, tree args
,
13397 tsubst_flags_t complain
,
13400 if (TREE_CODE (t
) == SCOPE_REF
)
13401 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
13402 /*done=*/false, /*address_p=*/false);
13404 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13405 /*function_p=*/false,
13406 /*integral_constant_expression_p=*/false);
13411 /* Like tsubst but deals with expressions and performs semantic
13412 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13415 tsubst_copy_and_build (tree t
,
13417 tsubst_flags_t complain
,
13420 bool integral_constant_expression_p
)
13422 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13423 #define RECUR(NODE) \
13424 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13425 /*function_p=*/false, \
13426 integral_constant_expression_p)
13431 if (t
== NULL_TREE
|| t
== error_mark_node
)
13434 loc
= input_location
;
13435 if (EXPR_HAS_LOCATION (t
))
13436 input_location
= EXPR_LOCATION (t
);
13438 /* N3276 decltype magic only applies to calls at the top level or on the
13439 right side of a comma. */
13440 if (TREE_CODE (t
) != CALL_EXPR
13441 && TREE_CODE (t
) != COMPOUND_EXPR
)
13442 complain
&= ~tf_decltype
;
13444 switch (TREE_CODE (t
))
13448 /* Fall through. */
13449 case IDENTIFIER_NODE
:
13453 bool non_integral_constant_expression_p
;
13454 const char *error_msg
;
13456 if (IDENTIFIER_TYPENAME_P (t
))
13458 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13459 t
= mangle_conv_op_name_for_type (new_type
);
13462 /* Look up the name. */
13463 decl
= lookup_name (t
);
13465 /* By convention, expressions use ERROR_MARK_NODE to indicate
13466 failure, not NULL_TREE. */
13467 if (decl
== NULL_TREE
)
13468 decl
= error_mark_node
;
13470 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
13472 integral_constant_expression_p
,
13473 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx0x
),
13474 &non_integral_constant_expression_p
,
13475 /*template_p=*/false,
13477 /*address_p=*/false,
13478 /*template_arg_p=*/false,
13483 if (!function_p
&& TREE_CODE (decl
) == IDENTIFIER_NODE
)
13485 if (complain
& tf_error
)
13486 unqualified_name_lookup_error (decl
);
13487 decl
= error_mark_node
;
13492 case TEMPLATE_ID_EXPR
:
13495 tree templ
= RECUR (TREE_OPERAND (t
, 0));
13496 tree targs
= TREE_OPERAND (t
, 1);
13499 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
13501 if (TREE_CODE (templ
) == COMPONENT_REF
)
13503 object
= TREE_OPERAND (templ
, 0);
13504 templ
= TREE_OPERAND (templ
, 1);
13507 object
= NULL_TREE
;
13508 templ
= lookup_template_function (templ
, targs
);
13511 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
13512 object
, templ
, NULL_TREE
));
13514 RETURN (baselink_for_fns (templ
));
13519 tree r
= RECUR (TREE_OPERAND (t
, 0));
13521 if (REFERENCE_REF_P (t
))
13523 /* A type conversion to reference type will be enclosed in
13524 such an indirect ref, but the substitution of the cast
13525 will have also added such an indirect ref. */
13526 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
13527 r
= convert_from_reference (r
);
13530 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
, complain
);
13536 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13537 RECUR (TREE_OPERAND (t
, 0))));
13539 case IMPLICIT_CONV_EXPR
:
13541 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13542 tree expr
= RECUR (TREE_OPERAND (t
, 0));
13543 int flags
= LOOKUP_IMPLICIT
;
13544 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
13545 flags
= LOOKUP_NORMAL
;
13546 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
13553 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13554 RECUR (TREE_OPERAND (t
, 0))));
13557 case REINTERPRET_CAST_EXPR
:
13558 case CONST_CAST_EXPR
:
13559 case DYNAMIC_CAST_EXPR
:
13560 case STATIC_CAST_EXPR
:
13563 tree op
, r
= NULL_TREE
;
13565 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13566 if (integral_constant_expression_p
13567 && !cast_valid_in_integral_constant_expression_p (type
))
13569 if (complain
& tf_error
)
13570 error ("a cast to a type other than an integral or "
13571 "enumeration type cannot appear in a constant-expression");
13572 RETURN (error_mark_node
);
13575 op
= RECUR (TREE_OPERAND (t
, 0));
13577 ++c_inhibit_evaluation_warnings
;
13578 switch (TREE_CODE (t
))
13581 r
= build_functional_cast (type
, op
, complain
);
13583 case REINTERPRET_CAST_EXPR
:
13584 r
= build_reinterpret_cast (type
, op
, complain
);
13586 case CONST_CAST_EXPR
:
13587 r
= build_const_cast (type
, op
, complain
);
13589 case DYNAMIC_CAST_EXPR
:
13590 r
= build_dynamic_cast (type
, op
, complain
);
13592 case STATIC_CAST_EXPR
:
13593 r
= build_static_cast (type
, op
, complain
);
13596 gcc_unreachable ();
13598 --c_inhibit_evaluation_warnings
;
13603 case POSTDECREMENT_EXPR
:
13604 case POSTINCREMENT_EXPR
:
13605 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13606 args
, complain
, in_decl
);
13607 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
, complain
));
13609 case PREDECREMENT_EXPR
:
13610 case PREINCREMENT_EXPR
:
13614 case TRUTH_NOT_EXPR
:
13615 case UNARY_PLUS_EXPR
: /* Unary + */
13616 case REALPART_EXPR
:
13617 case IMAGPART_EXPR
:
13618 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
13619 RECUR (TREE_OPERAND (t
, 0)), complain
));
13621 case FIX_TRUNC_EXPR
:
13622 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
13626 op1
= TREE_OPERAND (t
, 0);
13627 if (TREE_CODE (op1
) == LABEL_DECL
)
13628 RETURN (finish_label_address_expr (DECL_NAME (op1
),
13629 EXPR_LOCATION (op1
)));
13630 if (TREE_CODE (op1
) == SCOPE_REF
)
13631 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
13632 /*done=*/true, /*address_p=*/true);
13634 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
13636 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
, complain
));
13641 case TRUNC_DIV_EXPR
:
13642 case CEIL_DIV_EXPR
:
13643 case FLOOR_DIV_EXPR
:
13644 case ROUND_DIV_EXPR
:
13645 case EXACT_DIV_EXPR
:
13649 case TRUNC_MOD_EXPR
:
13650 case FLOOR_MOD_EXPR
:
13651 case TRUTH_ANDIF_EXPR
:
13652 case TRUTH_ORIF_EXPR
:
13653 case TRUTH_AND_EXPR
:
13654 case TRUTH_OR_EXPR
:
13672 ++c_inhibit_evaluation_warnings
;
13674 r
= build_x_binary_op
13675 (input_location
, TREE_CODE (t
),
13676 RECUR (TREE_OPERAND (t
, 0)),
13677 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
13679 : TREE_CODE (TREE_OPERAND (t
, 0))),
13680 RECUR (TREE_OPERAND (t
, 1)),
13681 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
13683 : TREE_CODE (TREE_OPERAND (t
, 1))),
13686 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
13687 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13689 --c_inhibit_evaluation_warnings
;
13695 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
13696 /*address_p=*/false));
13698 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13699 args
, complain
, in_decl
);
13700 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
13701 RECUR (TREE_OPERAND (t
, 1)), complain
));
13704 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
13705 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
13712 op1
= TREE_OPERAND (t
, 0);
13713 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
13714 op1
= TREE_TYPE (op1
);
13717 /* When there are no ARGS, we are trying to evaluate a
13718 non-dependent expression from the parser. Trying to do
13719 the substitutions may not work. */
13721 op1
= TREE_TYPE (op1
);
13725 ++cp_unevaluated_operand
;
13726 ++c_inhibit_evaluation_warnings
;
13728 op1
= tsubst (op1
, args
, complain
, in_decl
);
13730 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13731 /*function_p=*/false,
13732 /*integral_constant_expression_p=*/
13734 --cp_unevaluated_operand
;
13735 --c_inhibit_evaluation_warnings
;
13738 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
13739 complain
& tf_error
);
13741 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
13742 complain
& tf_error
);
13743 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
13745 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
13747 if (!processing_template_decl
&& TYPE_P (op1
))
13749 r
= build_min (SIZEOF_EXPR
, size_type_node
,
13750 build1 (NOP_EXPR
, op1
, error_mark_node
));
13751 SIZEOF_EXPR_TYPE_P (r
) = 1;
13754 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
13755 TREE_SIDE_EFFECTS (r
) = 0;
13756 TREE_READONLY (r
) = 1;
13758 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
13763 case AT_ENCODE_EXPR
:
13765 op1
= TREE_OPERAND (t
, 0);
13766 ++cp_unevaluated_operand
;
13767 ++c_inhibit_evaluation_warnings
;
13768 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13769 /*function_p=*/false,
13770 /*integral_constant_expression_p=*/false);
13771 --cp_unevaluated_operand
;
13772 --c_inhibit_evaluation_warnings
;
13773 RETURN (objc_build_encode_expr (op1
));
13776 case NOEXCEPT_EXPR
:
13777 op1
= TREE_OPERAND (t
, 0);
13778 ++cp_unevaluated_operand
;
13779 ++c_inhibit_evaluation_warnings
;
13780 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13781 /*function_p=*/false,
13782 /*integral_constant_expression_p=*/false);
13783 --cp_unevaluated_operand
;
13784 --c_inhibit_evaluation_warnings
;
13785 RETURN (finish_noexcept_expr (op1
, complain
));
13789 tree r
= build_x_modify_expr
13790 (EXPR_LOCATION (t
),
13791 RECUR (TREE_OPERAND (t
, 0)),
13792 TREE_CODE (TREE_OPERAND (t
, 1)),
13793 RECUR (TREE_OPERAND (t
, 2)),
13795 /* TREE_NO_WARNING must be set if either the expression was
13796 parenthesized or it uses an operator such as >>= rather
13797 than plain assignment. In the former case, it was already
13798 set and must be copied. In the latter case,
13799 build_x_modify_expr sets it and it must not be reset
13801 if (TREE_NO_WARNING (t
))
13802 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13807 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13808 args
, complain
, in_decl
);
13809 /* Remember that there was a reference to this entity. */
13812 RETURN (build_x_arrow (input_location
, op1
, complain
));
13816 tree placement
= RECUR (TREE_OPERAND (t
, 0));
13817 tree init
= RECUR (TREE_OPERAND (t
, 3));
13818 vec
<tree
, va_gc
> *placement_vec
;
13819 vec
<tree
, va_gc
> *init_vec
;
13822 if (placement
== NULL_TREE
)
13823 placement_vec
= NULL
;
13826 placement_vec
= make_tree_vector ();
13827 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
13828 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
13831 /* If there was an initializer in the original tree, but it
13832 instantiated to an empty list, then we should pass a
13833 non-NULL empty vector to tell build_new that it was an
13834 empty initializer() rather than no initializer. This can
13835 only happen when the initializer is a pack expansion whose
13836 parameter packs are of length zero. */
13837 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
13841 init_vec
= make_tree_vector ();
13842 if (init
== void_zero_node
)
13843 gcc_assert (init_vec
!= NULL
);
13846 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
13847 vec_safe_push (init_vec
, TREE_VALUE (init
));
13851 ret
= build_new (&placement_vec
,
13852 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
13853 RECUR (TREE_OPERAND (t
, 2)),
13855 NEW_EXPR_USE_GLOBAL (t
),
13858 if (placement_vec
!= NULL
)
13859 release_tree_vector (placement_vec
);
13860 if (init_vec
!= NULL
)
13861 release_tree_vector (init_vec
);
13867 RETURN (delete_sanity
13868 (RECUR (TREE_OPERAND (t
, 0)),
13869 RECUR (TREE_OPERAND (t
, 1)),
13870 DELETE_EXPR_USE_VEC (t
),
13871 DELETE_EXPR_USE_GLOBAL (t
),
13874 case COMPOUND_EXPR
:
13876 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
13877 complain
& ~tf_decltype
, in_decl
,
13878 /*function_p=*/false,
13879 integral_constant_expression_p
);
13880 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
13882 RECUR (TREE_OPERAND (t
, 1)),
13889 vec
<tree
, va_gc
> *call_args
;
13890 unsigned int nargs
, i
;
13895 /* Don't pass tf_decltype down to subexpressions. */
13896 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
13897 complain
&= ~tf_decltype
;
13899 function
= CALL_EXPR_FN (t
);
13900 /* When we parsed the expression, we determined whether or
13901 not Koenig lookup should be performed. */
13902 koenig_p
= KOENIG_LOOKUP_P (t
);
13903 if (TREE_CODE (function
) == SCOPE_REF
)
13905 qualified_p
= true;
13906 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
13908 /*address_p=*/false);
13910 else if (koenig_p
&& TREE_CODE (function
) == IDENTIFIER_NODE
)
13912 /* Do nothing; calling tsubst_copy_and_build on an identifier
13913 would incorrectly perform unqualified lookup again.
13915 Note that we can also have an IDENTIFIER_NODE if the earlier
13916 unqualified lookup found a member function; in that case
13917 koenig_p will be false and we do want to do the lookup
13918 again to find the instantiated member function.
13920 FIXME but doing that causes c++/15272, so we need to stop
13921 using IDENTIFIER_NODE in that situation. */
13922 qualified_p
= false;
13926 if (TREE_CODE (function
) == COMPONENT_REF
)
13928 tree op
= TREE_OPERAND (function
, 1);
13930 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
13931 || (BASELINK_P (op
)
13932 && BASELINK_QUALIFIED_P (op
)));
13935 qualified_p
= false;
13937 if (TREE_CODE (function
) == ADDR_EXPR
13938 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
13939 /* Avoid error about taking the address of a constructor. */
13940 function
= TREE_OPERAND (function
, 0);
13942 function
= tsubst_copy_and_build (function
, args
, complain
,
13945 integral_constant_expression_p
);
13947 if (BASELINK_P (function
))
13948 qualified_p
= true;
13951 nargs
= call_expr_nargs (t
);
13952 call_args
= make_tree_vector ();
13953 for (i
= 0; i
< nargs
; ++i
)
13955 tree arg
= CALL_EXPR_ARG (t
, i
);
13957 if (!PACK_EXPANSION_P (arg
))
13958 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
13961 /* Expand the pack expansion and push each entry onto
13963 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
13964 if (TREE_CODE (arg
) == TREE_VEC
)
13966 unsigned int len
, j
;
13968 len
= TREE_VEC_LENGTH (arg
);
13969 for (j
= 0; j
< len
; ++j
)
13971 tree value
= TREE_VEC_ELT (arg
, j
);
13972 if (value
!= NULL_TREE
)
13973 value
= convert_from_reference (value
);
13974 vec_safe_push (call_args
, value
);
13979 /* A partial substitution. Add one entry. */
13980 vec_safe_push (call_args
, arg
);
13985 /* We do not perform argument-dependent lookup if normal
13986 lookup finds a non-function, in accordance with the
13987 expected resolution of DR 218. */
13989 && ((is_overloaded_fn (function
)
13990 /* If lookup found a member function, the Koenig lookup is
13991 not appropriate, even if an unqualified-name was used
13992 to denote the function. */
13993 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
13994 || TREE_CODE (function
) == IDENTIFIER_NODE
)
13995 /* Only do this when substitution turns a dependent call
13996 into a non-dependent call. */
13997 && type_dependent_expression_p_push (t
)
13998 && !any_type_dependent_arguments_p (call_args
))
13999 function
= perform_koenig_lookup (function
, call_args
, false,
14002 if (TREE_CODE (function
) == IDENTIFIER_NODE
14003 && !any_type_dependent_arguments_p (call_args
))
14005 if (koenig_p
&& (complain
& tf_warning_or_error
))
14007 /* For backwards compatibility and good diagnostics, try
14008 the unqualified lookup again if we aren't in SFINAE
14010 tree unq
= (tsubst_copy_and_build
14011 (function
, args
, complain
, in_decl
, true,
14012 integral_constant_expression_p
));
14013 if (unq
== error_mark_node
)
14014 RETURN (error_mark_node
);
14016 if (unq
!= function
)
14019 if (TREE_CODE (fn
) == INDIRECT_REF
)
14020 fn
= TREE_OPERAND (fn
, 0);
14021 if (TREE_CODE (fn
) == COMPONENT_REF
)
14022 fn
= TREE_OPERAND (fn
, 1);
14023 if (is_overloaded_fn (fn
))
14024 fn
= get_first_fn (fn
);
14025 permerror (EXPR_LOC_OR_HERE (t
),
14026 "%qD was not declared in this scope, "
14027 "and no declarations were found by "
14028 "argument-dependent lookup at the point "
14029 "of instantiation", function
);
14031 /* Can't say anything more. */;
14032 else if (DECL_CLASS_SCOPE_P (fn
))
14034 inform (EXPR_LOC_OR_HERE (t
),
14035 "declarations in dependent base %qT are "
14036 "not found by unqualified lookup",
14037 DECL_CLASS_CONTEXT (fn
));
14038 if (current_class_ptr
)
14039 inform (EXPR_LOC_OR_HERE (t
),
14040 "use %<this->%D%> instead", function
);
14042 inform (EXPR_LOC_OR_HERE (t
),
14043 "use %<%T::%D%> instead",
14044 current_class_name
, function
);
14047 inform (0, "%q+D declared here, later in the "
14048 "translation unit", fn
);
14052 if (TREE_CODE (function
) == IDENTIFIER_NODE
)
14054 if (complain
& tf_error
)
14055 unqualified_name_lookup_error (function
);
14056 release_tree_vector (call_args
);
14057 RETURN (error_mark_node
);
14061 /* Remember that there was a reference to this entity. */
14062 if (DECL_P (function
))
14063 mark_used (function
);
14065 /* Put back tf_decltype for the actual call. */
14066 complain
|= decltype_flag
;
14068 if (TREE_CODE (function
) == OFFSET_REF
)
14069 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
14071 else if (TREE_CODE (function
) == COMPONENT_REF
)
14073 tree instance
= TREE_OPERAND (function
, 0);
14074 tree fn
= TREE_OPERAND (function
, 1);
14076 if (processing_template_decl
14077 && (type_dependent_expression_p (instance
)
14078 || (!BASELINK_P (fn
)
14079 && TREE_CODE (fn
) != FIELD_DECL
)
14080 || type_dependent_expression_p (fn
)
14081 || any_type_dependent_arguments_p (call_args
)))
14082 ret
= build_nt_call_vec (function
, call_args
);
14083 else if (!BASELINK_P (fn
))
14084 ret
= finish_call_expr (function
, &call_args
,
14085 /*disallow_virtual=*/false,
14086 /*koenig_p=*/false,
14089 ret
= (build_new_method_call
14091 &call_args
, NULL_TREE
,
14092 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
14097 ret
= finish_call_expr (function
, &call_args
,
14098 /*disallow_virtual=*/qualified_p
,
14102 release_tree_vector (call_args
);
14109 tree cond
= RECUR (TREE_OPERAND (t
, 0));
14112 if (TREE_CODE (cond
) == INTEGER_CST
)
14114 if (integer_zerop (cond
))
14116 ++c_inhibit_evaluation_warnings
;
14117 exp1
= RECUR (TREE_OPERAND (t
, 1));
14118 --c_inhibit_evaluation_warnings
;
14119 exp2
= RECUR (TREE_OPERAND (t
, 2));
14123 exp1
= RECUR (TREE_OPERAND (t
, 1));
14124 ++c_inhibit_evaluation_warnings
;
14125 exp2
= RECUR (TREE_OPERAND (t
, 2));
14126 --c_inhibit_evaluation_warnings
;
14131 exp1
= RECUR (TREE_OPERAND (t
, 1));
14132 exp2
= RECUR (TREE_OPERAND (t
, 2));
14135 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
14136 cond
, exp1
, exp2
, complain
));
14139 case PSEUDO_DTOR_EXPR
:
14140 RETURN (finish_pseudo_destructor_expr
14141 (RECUR (TREE_OPERAND (t
, 0)),
14142 RECUR (TREE_OPERAND (t
, 1)),
14143 tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
)));
14147 tree purpose
, value
, chain
;
14149 if (t
== void_list_node
)
14152 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
14153 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
14155 /* We have pack expansions, so expand those and
14156 create a new list out of it. */
14157 tree purposevec
= NULL_TREE
;
14158 tree valuevec
= NULL_TREE
;
14162 /* Expand the argument expressions. */
14163 if (TREE_PURPOSE (t
))
14164 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
14165 complain
, in_decl
);
14166 if (TREE_VALUE (t
))
14167 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
14168 complain
, in_decl
);
14170 /* Build the rest of the list. */
14171 chain
= TREE_CHAIN (t
);
14172 if (chain
&& chain
!= void_type_node
)
14173 chain
= RECUR (chain
);
14175 /* Determine the number of arguments. */
14176 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
14178 len
= TREE_VEC_LENGTH (purposevec
);
14179 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
14181 else if (TREE_CODE (valuevec
) == TREE_VEC
)
14182 len
= TREE_VEC_LENGTH (valuevec
);
14185 /* Since we only performed a partial substitution into
14186 the argument pack, we only RETURN (a single list
14188 if (purposevec
== TREE_PURPOSE (t
)
14189 && valuevec
== TREE_VALUE (t
)
14190 && chain
== TREE_CHAIN (t
))
14193 RETURN (tree_cons (purposevec
, valuevec
, chain
));
14196 /* Convert the argument vectors into a TREE_LIST */
14200 /* Grab the Ith values. */
14202 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
14205 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
14208 /* Build the list (backwards). */
14209 chain
= tree_cons (purpose
, value
, chain
);
14215 purpose
= TREE_PURPOSE (t
);
14217 purpose
= RECUR (purpose
);
14218 value
= TREE_VALUE (t
);
14220 value
= RECUR (value
);
14221 chain
= TREE_CHAIN (t
);
14222 if (chain
&& chain
!= void_type_node
)
14223 chain
= RECUR (chain
);
14224 if (purpose
== TREE_PURPOSE (t
)
14225 && value
== TREE_VALUE (t
)
14226 && chain
== TREE_CHAIN (t
))
14228 RETURN (tree_cons (purpose
, value
, chain
));
14231 case COMPONENT_REF
:
14237 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14238 args
, complain
, in_decl
);
14239 /* Remember that there was a reference to this entity. */
14240 if (DECL_P (object
))
14241 mark_used (object
);
14242 object_type
= TREE_TYPE (object
);
14244 member
= TREE_OPERAND (t
, 1);
14245 if (BASELINK_P (member
))
14246 member
= tsubst_baselink (member
,
14247 non_reference (TREE_TYPE (object
)),
14248 args
, complain
, in_decl
);
14250 member
= tsubst_copy (member
, args
, complain
, in_decl
);
14251 if (member
== error_mark_node
)
14252 RETURN (error_mark_node
);
14254 if (type_dependent_expression_p (object
))
14255 /* We can't do much here. */;
14256 else if (!CLASS_TYPE_P (object_type
))
14258 if (scalarish_type_p (object_type
))
14260 tree s
= NULL_TREE
;
14261 tree dtor
= member
;
14263 if (TREE_CODE (dtor
) == SCOPE_REF
)
14265 s
= TREE_OPERAND (dtor
, 0);
14266 dtor
= TREE_OPERAND (dtor
, 1);
14268 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
14270 dtor
= TREE_OPERAND (dtor
, 0);
14272 RETURN (finish_pseudo_destructor_expr (object
, s
, dtor
));
14276 else if (TREE_CODE (member
) == SCOPE_REF
14277 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
14279 /* Lookup the template functions now that we know what the
14281 tree scope
= TREE_OPERAND (member
, 0);
14282 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
14283 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
14284 member
= lookup_qualified_name (scope
, tmpl
,
14285 /*is_type_p=*/false,
14286 /*complain=*/false);
14287 if (BASELINK_P (member
))
14289 BASELINK_FUNCTIONS (member
)
14290 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
14292 member
= (adjust_result_of_qualified_name_lookup
14293 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
14298 qualified_name_lookup_error (scope
, tmpl
, member
,
14300 RETURN (error_mark_node
);
14303 else if (TREE_CODE (member
) == SCOPE_REF
14304 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
14305 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
14307 if (complain
& tf_error
)
14309 if (TYPE_P (TREE_OPERAND (member
, 0)))
14310 error ("%qT is not a class or namespace",
14311 TREE_OPERAND (member
, 0));
14313 error ("%qD is not a class or namespace",
14314 TREE_OPERAND (member
, 0));
14316 RETURN (error_mark_node
);
14318 else if (TREE_CODE (member
) == FIELD_DECL
)
14319 RETURN (finish_non_static_data_member (member
, object
, NULL_TREE
));
14321 RETURN (finish_class_member_access_expr (object
, member
,
14322 /*template_p=*/false,
14327 RETURN (build_throw
14328 (RECUR (TREE_OPERAND (t
, 0))));
14332 vec
<constructor_elt
, va_gc
> *n
;
14333 constructor_elt
*ce
;
14334 unsigned HOST_WIDE_INT idx
;
14335 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14336 bool process_index_p
;
14338 bool need_copy_p
= false;
14341 if (type
== error_mark_node
)
14342 RETURN (error_mark_node
);
14344 /* digest_init will do the wrong thing if we let it. */
14345 if (type
&& TYPE_PTRMEMFUNC_P (type
))
14348 /* We do not want to process the index of aggregate
14349 initializers as they are identifier nodes which will be
14350 looked up by digest_init. */
14351 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
14353 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
14354 newlen
= vec_safe_length (n
);
14355 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
14357 if (ce
->index
&& process_index_p
)
14358 ce
->index
= RECUR (ce
->index
);
14360 if (PACK_EXPANSION_P (ce
->value
))
14362 /* Substitute into the pack expansion. */
14363 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
14366 if (ce
->value
== error_mark_node
14367 || PACK_EXPANSION_P (ce
->value
))
14369 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
14370 /* Just move the argument into place. */
14371 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
14374 /* Update the length of the final CONSTRUCTOR
14375 arguments vector, and note that we will need to
14377 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
14378 need_copy_p
= true;
14382 ce
->value
= RECUR (ce
->value
);
14387 vec
<constructor_elt
, va_gc
> *old_n
= n
;
14389 vec_alloc (n
, newlen
);
14390 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
14392 if (TREE_CODE (ce
->value
) == TREE_VEC
)
14394 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
14395 for (i
= 0; i
< len
; ++i
)
14396 CONSTRUCTOR_APPEND_ELT (n
, 0,
14397 TREE_VEC_ELT (ce
->value
, i
));
14400 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
14404 r
= build_constructor (init_list_type_node
, n
);
14405 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
14407 if (TREE_HAS_CONSTRUCTOR (t
))
14408 RETURN (finish_compound_literal (type
, r
, complain
));
14410 TREE_TYPE (r
) = type
;
14416 tree operand_0
= TREE_OPERAND (t
, 0);
14417 if (TYPE_P (operand_0
))
14419 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
14420 RETURN (get_typeid (operand_0
, complain
));
14424 operand_0
= RECUR (operand_0
);
14425 RETURN (build_typeid (operand_0
, complain
));
14436 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
14438 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
14439 /* If the original type was a reference, we'll be wrapped in
14440 the appropriate INDIRECT_REF. */
14441 r
= convert_from_reference (r
);
14446 RETURN (build_x_va_arg (EXPR_LOCATION (t
),
14447 RECUR (TREE_OPERAND (t
, 0)),
14448 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
)));
14450 case OFFSETOF_EXPR
:
14451 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t
, 0))));
14455 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
14456 complain
, in_decl
);
14458 tree type2
= TRAIT_EXPR_TYPE2 (t
);
14460 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
14462 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
14467 tree old_stmt_expr
= cur_stmt_expr
;
14468 tree stmt_expr
= begin_stmt_expr ();
14470 cur_stmt_expr
= stmt_expr
;
14471 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
14472 integral_constant_expression_p
);
14473 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
14474 cur_stmt_expr
= old_stmt_expr
;
14476 /* If the resulting list of expression statement is empty,
14477 fold it further into void_zero_node. */
14478 if (empty_expr_stmt_p (stmt_expr
))
14479 stmt_expr
= void_zero_node
;
14481 RETURN (stmt_expr
);
14486 tree r
= build_lambda_expr ();
14488 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
14489 LAMBDA_EXPR_CLOSURE (r
) = type
;
14490 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
14492 LAMBDA_EXPR_LOCATION (r
)
14493 = LAMBDA_EXPR_LOCATION (t
);
14494 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
14495 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
14496 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
14497 LAMBDA_EXPR_DISCRIMINATOR (r
)
14498 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
14499 /* For a function scope, we want to use tsubst so that we don't
14500 complain about referring to an auto function before its return
14501 type has been deduced. Otherwise, we want to use tsubst_copy so
14502 that we look up the existing field/parameter/variable rather
14503 than build a new one. */
14504 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
14505 if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
14506 scope
= tsubst (scope
, args
, complain
, in_decl
);
14507 else if (scope
&& TREE_CODE (scope
) == PARM_DECL
)
14509 /* Look up the parameter we want directly, as tsubst_copy
14510 doesn't do what we need. */
14511 tree fn
= tsubst (DECL_CONTEXT (scope
), args
, complain
, in_decl
);
14512 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
14513 while (DECL_PARM_INDEX (parm
) != DECL_PARM_INDEX (scope
))
14514 parm
= DECL_CHAIN (parm
);
14516 /* FIXME Work around the parm not having DECL_CONTEXT set. */
14517 if (DECL_CONTEXT (scope
) == NULL_TREE
)
14518 DECL_CONTEXT (scope
) = fn
;
14521 scope
= RECUR (scope
);
14522 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
14523 LAMBDA_EXPR_RETURN_TYPE (r
)
14524 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
14526 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
14527 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
14529 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
14530 determine_visibility (TYPE_NAME (type
));
14531 /* Now that we know visibility, instantiate the type so we have a
14532 declaration of the op() for later calls to lambda_function. */
14533 complete_type (type
);
14535 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
14537 RETURN (build_lambda_object (r
));
14541 /* We can get here for a constant initializer of non-dependent type.
14542 FIXME stop folding in cp_parser_initializer_clause. */
14544 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
14549 case TRANSACTION_EXPR
:
14550 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
14551 integral_constant_expression_p
));
14554 /* Handle Objective-C++ constructs, if appropriate. */
14557 = objcp_tsubst_copy_and_build (t
, args
, complain
,
14558 in_decl
, /*function_p=*/false);
14562 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
14568 input_location
= loc
;
14572 /* Verify that the instantiated ARGS are valid. For type arguments,
14573 make sure that the type's linkage is ok. For non-type arguments,
14574 make sure they are constants if they are integral or enumerations.
14575 Emit an error under control of COMPLAIN, and return TRUE on error. */
14578 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
14580 if (dependent_template_arg_p (t
))
14582 if (ARGUMENT_PACK_P (t
))
14584 tree vec
= ARGUMENT_PACK_ARGS (t
);
14585 int len
= TREE_VEC_LENGTH (vec
);
14586 bool result
= false;
14589 for (i
= 0; i
< len
; ++i
)
14590 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
14594 else if (TYPE_P (t
))
14596 /* [basic.link]: A name with no linkage (notably, the name
14597 of a class or enumeration declared in a local scope)
14598 shall not be used to declare an entity with linkage.
14599 This implies that names with no linkage cannot be used as
14602 DR 757 relaxes this restriction for C++0x. */
14603 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
14604 : no_linkage_check (t
, /*relaxed_p=*/false));
14608 /* DR 488 makes use of a type with no linkage cause
14609 type deduction to fail. */
14610 if (complain
& tf_error
)
14612 if (TYPE_ANONYMOUS_P (nt
))
14613 error ("%qT is/uses anonymous type", t
);
14615 error ("template argument for %qD uses local type %qT",
14620 /* In order to avoid all sorts of complications, we do not
14621 allow variably-modified types as template arguments. */
14622 else if (variably_modified_type_p (t
, NULL_TREE
))
14624 if (complain
& tf_error
)
14625 error ("%qT is a variably modified type", t
);
14629 /* Class template and alias template arguments should be OK. */
14630 else if (DECL_TYPE_TEMPLATE_P (t
))
14632 /* A non-type argument of integral or enumerated type must be a
14634 else if (TREE_TYPE (t
)
14635 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
14636 && !TREE_CONSTANT (t
))
14638 if (complain
& tf_error
)
14639 error ("integral expression %qE is not constant", t
);
14646 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
14648 int ix
, len
= DECL_NTPARMS (tmpl
);
14649 bool result
= false;
14651 for (ix
= 0; ix
!= len
; ix
++)
14653 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
14656 if (result
&& (complain
& tf_error
))
14657 error (" trying to instantiate %qD", tmpl
);
14661 /* We're out of SFINAE context now, so generate diagnostics for the access
14662 errors we saw earlier when instantiating D from TMPL and ARGS. */
14665 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
14667 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
14668 tree type
= TREE_TYPE (pattern
);
14669 location_t loc
= input_location
;
14671 push_access_scope (d
);
14672 push_deferring_access_checks (dk_no_deferred
);
14673 input_location
= DECL_SOURCE_LOCATION (pattern
);
14674 tsubst (type
, args
, tf_warning_or_error
, d
);
14675 input_location
= loc
;
14676 pop_deferring_access_checks ();
14677 pop_access_scope (d
);
14680 /* Instantiate the indicated variable, function, or alias template TMPL with
14681 the template arguments in TARG_PTR. */
14684 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
14686 tree targ_ptr
= orig_args
;
14690 bool access_ok
= true;
14692 if (tmpl
== error_mark_node
)
14693 return error_mark_node
;
14695 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
14697 /* If this function is a clone, handle it specially. */
14698 if (DECL_CLONED_FUNCTION_P (tmpl
))
14703 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
14704 DECL_CLONED_FUNCTION. */
14705 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
14706 targ_ptr
, complain
);
14707 if (spec
== error_mark_node
)
14708 return error_mark_node
;
14710 /* Look for the clone. */
14711 FOR_EACH_CLONE (clone
, spec
)
14712 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
14714 /* We should always have found the clone by now. */
14715 gcc_unreachable ();
14719 /* Check to see if we already have this specialization. */
14720 gen_tmpl
= most_general_template (tmpl
);
14721 if (tmpl
!= gen_tmpl
)
14722 /* The TMPL is a partial instantiation. To get a full set of
14723 arguments we must add the arguments used to perform the
14724 partial instantiation. */
14725 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
14728 /* It would be nice to avoid hashing here and then again in tsubst_decl,
14729 but it doesn't seem to be on the hot path. */
14730 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
14732 gcc_assert (tmpl
== gen_tmpl
14733 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
14735 || fndecl
== NULL_TREE
);
14737 if (spec
!= NULL_TREE
)
14739 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
14741 if (complain
& tf_error
)
14742 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
14743 return error_mark_node
;
14748 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
14750 return error_mark_node
;
14752 /* We are building a FUNCTION_DECL, during which the access of its
14753 parameters and return types have to be checked. However this
14754 FUNCTION_DECL which is the desired context for access checking
14755 is not built yet. We solve this chicken-and-egg problem by
14756 deferring all checks until we have the FUNCTION_DECL. */
14757 push_deferring_access_checks (dk_deferred
);
14759 /* Instantiation of the function happens in the context of the function
14760 template, not the context of the overload resolution we're doing. */
14761 push_to_top_level ();
14762 /* If there are dependent arguments, e.g. because we're doing partial
14763 ordering, make sure processing_template_decl stays set. */
14764 if (uses_template_parms (targ_ptr
))
14765 ++processing_template_decl
;
14766 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
14768 tree ctx
= tsubst (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
14769 complain
, gen_tmpl
);
14770 push_nested_class (ctx
);
14772 /* Substitute template parameters to obtain the specialization. */
14773 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
14774 targ_ptr
, complain
, gen_tmpl
);
14775 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
14776 pop_nested_class ();
14777 pop_from_top_level ();
14779 if (fndecl
== error_mark_node
)
14781 pop_deferring_access_checks ();
14782 return error_mark_node
;
14785 /* The DECL_TI_TEMPLATE should always be the immediate parent
14786 template, not the most general template. */
14787 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
14789 /* Now we know the specialization, compute access previously
14791 push_access_scope (fndecl
);
14792 if (!perform_deferred_access_checks (complain
))
14794 pop_access_scope (fndecl
);
14795 pop_deferring_access_checks ();
14797 /* If we've just instantiated the main entry point for a function,
14798 instantiate all the alternate entry points as well. We do this
14799 by cloning the instantiation of the main entry point, not by
14800 instantiating the template clones. */
14801 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
14802 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
14806 if (!(complain
& tf_error
))
14808 /* Remember to reinstantiate when we're out of SFINAE so the user
14809 can see the errors. */
14810 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
14812 return error_mark_node
;
14817 /* Wrapper for instantiate_template_1. */
14820 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
14823 timevar_push (TV_TEMPLATE_INST
);
14824 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
14825 timevar_pop (TV_TEMPLATE_INST
);
14829 /* Instantiate the alias template TMPL with ARGS. Also push a template
14830 instantiation level, which instantiate_template doesn't do because
14831 functions and variables have sufficient context established by the
14835 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
14837 struct pending_template
*old_last_pend
= last_pending_template
;
14838 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
14839 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
14840 return error_mark_node
;
14841 tree tinst
= build_tree_list (tmpl
, args
);
14842 if (!push_tinst_level (tinst
))
14845 return error_mark_node
;
14849 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
14850 args
, tmpl
, complain
,
14851 /*require_all_args=*/true,
14852 /*use_default_args=*/true);
14854 tree r
= instantiate_template (tmpl
, args
, complain
);
14855 pop_tinst_level ();
14856 /* We can't free this if a pending_template entry or last_error_tinst_level
14857 is pointing at it. */
14858 if (last_pending_template
== old_last_pend
14859 && last_error_tinst_level
== old_error_tinst
)
14865 /* PARM is a template parameter pack for FN. Returns true iff
14866 PARM is used in a deducible way in the argument list of FN. */
14869 pack_deducible_p (tree parm
, tree fn
)
14871 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
14872 for (; t
; t
= TREE_CHAIN (t
))
14874 tree type
= TREE_VALUE (t
);
14876 if (!PACK_EXPANSION_P (type
))
14878 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
14879 packs
; packs
= TREE_CHAIN (packs
))
14880 if (TREE_VALUE (packs
) == parm
)
14882 /* The template parameter pack is used in a function parameter
14883 pack. If this is the end of the parameter list, the
14884 template parameter pack is deducible. */
14885 if (TREE_CHAIN (t
) == void_list_node
)
14888 /* Otherwise, not. Well, it could be deduced from
14889 a non-pack parameter, but doing so would end up with
14890 a deduction mismatch, so don't bother. */
14894 /* The template parameter pack isn't used in any function parameter
14895 packs, but it might be used deeper, e.g. tuple<Args...>. */
14899 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
14900 NARGS elements of the arguments that are being used when calling
14901 it. TARGS is a vector into which the deduced template arguments
14904 Return zero for success, 2 for an incomplete match that doesn't resolve
14905 all the types, and 1 for complete failure. An error message will be
14906 printed only for an incomplete match.
14908 If FN is a conversion operator, or we are trying to produce a specific
14909 specialization, RETURN_TYPE is the return type desired.
14911 The EXPLICIT_TARGS are explicit template arguments provided via a
14914 The parameter STRICT is one of:
14917 We are deducing arguments for a function call, as in
14918 [temp.deduct.call].
14921 We are deducing arguments for a conversion function, as in
14922 [temp.deduct.conv].
14925 We are deducing arguments when doing an explicit instantiation
14926 as in [temp.explicit], when determining an explicit specialization
14927 as in [temp.expl.spec], or when taking the address of a function
14928 template, as in [temp.deduct.funcaddr]. */
14931 fn_type_unification (tree fn
,
14932 tree explicit_targs
,
14935 unsigned int nargs
,
14937 unification_kind_t strict
,
14943 tree decl
= NULL_TREE
;
14944 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
14946 static int deduction_depth
;
14947 struct pending_template
*old_last_pend
= last_pending_template
;
14948 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
14949 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
14951 tree r
= error_mark_node
;
14953 /* In C++0x, it's possible to have a function template whose type depends
14954 on itself recursively. This is most obvious with decltype, but can also
14955 occur with enumeration scope (c++/48969). So we need to catch infinite
14956 recursion and reject the substitution at deduction time; this function
14957 will return error_mark_node for any repeated substitution.
14959 This also catches excessive recursion such as when f<N> depends on
14960 f<N-1> across all integers, and returns error_mark_node for all the
14961 substitutions back up to the initial one.
14963 This is, of course, not reentrant. */
14964 if (excessive_deduction_depth
)
14965 return error_mark_node
;
14966 tinst
= build_tree_list (fn
, NULL_TREE
);
14968 push_deferring_access_checks (dk_deferred
);
14970 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
14972 fntype
= TREE_TYPE (fn
);
14973 if (explicit_targs
)
14977 The specified template arguments must match the template
14978 parameters in kind (i.e., type, nontype, template), and there
14979 must not be more arguments than there are parameters;
14980 otherwise type deduction fails.
14982 Nontype arguments must match the types of the corresponding
14983 nontype template parameters, or must be convertible to the
14984 types of the corresponding nontype parameters as specified in
14985 _temp.arg.nontype_, otherwise type deduction fails.
14987 All references in the function type of the function template
14988 to the corresponding template parameters are replaced by the
14989 specified template argument values. If a substitution in a
14990 template parameter or in the function type of the function
14991 template results in an invalid type, type deduction fails. */
14992 int i
, len
= TREE_VEC_LENGTH (tparms
);
14993 location_t loc
= input_location
;
14994 bool incomplete
= false;
14996 /* Adjust any explicit template arguments before entering the
14997 substitution context. */
14999 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
15001 /*require_all_args=*/false,
15002 /*use_default_args=*/false));
15003 if (explicit_targs
== error_mark_node
)
15006 /* Substitute the explicit args into the function type. This is
15007 necessary so that, for instance, explicitly declared function
15008 arguments can match null pointed constants. If we were given
15009 an incomplete set of explicit args, we must not do semantic
15010 processing during substitution as we could create partial
15012 for (i
= 0; i
< len
; i
++)
15014 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
15015 bool parameter_pack
= false;
15016 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
15018 /* Dig out the actual parm. */
15019 if (TREE_CODE (parm
) == TYPE_DECL
15020 || TREE_CODE (parm
) == TEMPLATE_DECL
)
15022 parm
= TREE_TYPE (parm
);
15023 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
15025 else if (TREE_CODE (parm
) == PARM_DECL
)
15027 parm
= DECL_INITIAL (parm
);
15028 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
15031 if (!parameter_pack
&& targ
== NULL_TREE
)
15032 /* No explicit argument for this template parameter. */
15035 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
15037 /* Mark the argument pack as "incomplete". We could
15038 still deduce more arguments during unification.
15039 We remove this mark in type_unification_real. */
15042 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
15043 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
15044 = ARGUMENT_PACK_ARGS (targ
);
15047 /* We have some incomplete argument packs. */
15052 TREE_VALUE (tinst
) = explicit_targs
;
15053 if (!push_tinst_level (tinst
))
15055 excessive_deduction_depth
= true;
15058 processing_template_decl
+= incomplete
;
15059 input_location
= DECL_SOURCE_LOCATION (fn
);
15060 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
15061 complain
| tf_partial
, NULL_TREE
);
15062 input_location
= loc
;
15063 processing_template_decl
-= incomplete
;
15064 pop_tinst_level ();
15066 if (fntype
== error_mark_node
)
15069 /* Throw away these access checks; we'll see them again in
15070 instantiate_template and they might have the wrong
15071 access path at this point. */
15072 pop_deferring_access_checks ();
15073 push_deferring_access_checks (dk_deferred
);
15075 /* Place the explicitly specified arguments in TARGS. */
15076 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
15077 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
15080 /* Never do unification on the 'this' parameter. */
15081 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
15087 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
15088 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
15089 new_args
[0] = return_type
;
15090 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
15095 /* We allow incomplete unification without an error message here
15096 because the standard doesn't seem to explicitly prohibit it. Our
15097 callers must be ready to deal with unification failures in any
15100 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15101 targs
, parms
, args
, nargs
, /*subr=*/0,
15102 strict
, flags
, explain_p
);
15106 /* Now that we have bindings for all of the template arguments,
15107 ensure that the arguments deduced for the template template
15108 parameters have compatible template parameter lists. We cannot
15109 check this property before we have deduced all template
15110 arguments, because the template parameter types of a template
15111 template parameter might depend on prior template parameters
15112 deduced after the template template parameter. The following
15113 ill-formed example illustrates this issue:
15115 template<typename T, template<T> class C> void f(C<5>, T);
15117 template<int N> struct X {};
15120 f(X<5>(), 5l); // error: template argument deduction fails
15123 The template parameter list of 'C' depends on the template type
15124 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15125 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15126 time that we deduce 'C'. */
15127 if (!template_template_parm_bindings_ok_p
15128 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
15130 unify_inconsistent_template_template_parameters (explain_p
);
15134 /* All is well so far. Now, check:
15138 When all template arguments have been deduced, all uses of
15139 template parameters in nondeduced contexts are replaced with
15140 the corresponding deduced argument values. If the
15141 substitution results in an invalid type, as described above,
15142 type deduction fails. */
15143 TREE_VALUE (tinst
) = targs
;
15144 if (!push_tinst_level (tinst
))
15146 excessive_deduction_depth
= true;
15149 decl
= instantiate_template (fn
, targs
, complain
);
15150 pop_tinst_level ();
15152 if (decl
== error_mark_node
)
15155 /* Now perform any access checks encountered during deduction, such as
15156 for default template arguments. */
15157 push_access_scope (decl
);
15158 ok
= perform_deferred_access_checks (complain
);
15159 pop_access_scope (decl
);
15163 /* If we're looking for an exact match, check that what we got
15164 is indeed an exact match. It might not be if some template
15165 parameters are used in non-deduced contexts. */
15166 if (strict
== DEDUCE_EXACT
)
15168 tree substed
= TREE_TYPE (decl
);
15172 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
15174 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
15175 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
15176 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
15178 unify_type_mismatch (explain_p
, args
[i
],
15179 TREE_VALUE (sarg
));
15187 pop_deferring_access_checks ();
15189 if (excessive_deduction_depth
)
15191 if (deduction_depth
== 0)
15192 /* Reset once we're all the way out. */
15193 excessive_deduction_depth
= false;
15196 /* We can't free this if a pending_template entry or last_error_tinst_level
15197 is pointing at it. */
15198 if (last_pending_template
== old_last_pend
15199 && last_error_tinst_level
== old_error_tinst
)
15205 /* Adjust types before performing type deduction, as described in
15206 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15207 sections are symmetric. PARM is the type of a function parameter
15208 or the return type of the conversion function. ARG is the type of
15209 the argument passed to the call, or the type of the value
15210 initialized with the result of the conversion function.
15211 ARG_EXPR is the original argument expression, which may be null. */
15214 maybe_adjust_types_for_deduction (unification_kind_t strict
,
15228 /* Swap PARM and ARG throughout the remainder of this
15229 function; the handling is precisely symmetric since PARM
15230 will initialize ARG rather than vice versa. */
15238 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15239 too, but here handle it by stripping the reference from PARM
15240 rather than by adding it to ARG. */
15241 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15242 && TYPE_REF_IS_RVALUE (*parm
)
15243 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15244 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15245 && TREE_CODE (*arg
) == REFERENCE_TYPE
15246 && !TYPE_REF_IS_RVALUE (*arg
))
15247 *parm
= TREE_TYPE (*parm
);
15248 /* Nothing else to do in this case. */
15252 gcc_unreachable ();
15255 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
15257 /* [temp.deduct.call]
15259 If P is not a reference type:
15261 --If A is an array type, the pointer type produced by the
15262 array-to-pointer standard conversion (_conv.array_) is
15263 used in place of A for type deduction; otherwise,
15265 --If A is a function type, the pointer type produced by
15266 the function-to-pointer standard conversion
15267 (_conv.func_) is used in place of A for type deduction;
15270 --If A is a cv-qualified type, the top level
15271 cv-qualifiers of A's type are ignored for type
15273 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
15274 *arg
= build_pointer_type (TREE_TYPE (*arg
));
15275 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
15276 *arg
= build_pointer_type (*arg
);
15278 *arg
= TYPE_MAIN_VARIANT (*arg
);
15281 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15282 of the form T&&, where T is a template parameter, and the argument
15283 is an lvalue, T is deduced as A& */
15284 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15285 && TYPE_REF_IS_RVALUE (*parm
)
15286 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15287 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15288 && (arg_expr
? real_lvalue_p (arg_expr
)
15289 /* try_one_overload doesn't provide an arg_expr, but
15290 functions are always lvalues. */
15291 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
15292 *arg
= build_reference_type (*arg
);
15294 /* [temp.deduct.call]
15296 If P is a cv-qualified type, the top level cv-qualifiers
15297 of P's type are ignored for type deduction. If P is a
15298 reference type, the type referred to by P is used for
15300 *parm
= TYPE_MAIN_VARIANT (*parm
);
15301 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
15303 *parm
= TREE_TYPE (*parm
);
15304 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
15307 /* DR 322. For conversion deduction, remove a reference type on parm
15308 too (which has been swapped into ARG). */
15309 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
15310 *arg
= TREE_TYPE (*arg
);
15315 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15316 template which does contain any deducible template parameters; check if
15317 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15318 unify_one_argument. */
15321 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
15322 int flags
, bool explain_p
)
15327 type
= TREE_TYPE (arg
);
15331 if (same_type_p (parm
, type
))
15332 return unify_success (explain_p
);
15334 if (strict
== DEDUCE_CONV
)
15336 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
15337 explain_p
? tf_warning_or_error
: tf_none
))
15338 return unify_success (explain_p
);
15340 else if (strict
!= DEDUCE_EXACT
)
15342 if (can_convert_arg (parm
, type
,
15343 TYPE_P (arg
) ? NULL_TREE
: arg
,
15344 flags
, explain_p
? tf_warning_or_error
: tf_none
))
15345 return unify_success (explain_p
);
15348 if (strict
== DEDUCE_EXACT
)
15349 return unify_type_mismatch (explain_p
, parm
, arg
);
15351 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
15354 /* Subroutine of type_unification_real and unify_pack_expansion to
15355 handle unification of a single P/A pair. Parameters are as
15356 for those functions. */
15359 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
15360 int subr
, unification_kind_t strict
, int flags
,
15363 tree arg_expr
= NULL_TREE
;
15366 if (arg
== error_mark_node
|| parm
== error_mark_node
)
15367 return unify_invalid (explain_p
);
15368 if (arg
== unknown_type_node
)
15369 /* We can't deduce anything from this, but we might get all the
15370 template args from other function args. */
15371 return unify_success (explain_p
);
15373 /* FIXME uses_deducible_template_parms */
15374 if (TYPE_P (parm
) && !uses_template_parms (parm
))
15375 return check_non_deducible_conversion (parm
, arg
, strict
, flags
,
15381 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
15382 | UNIFY_ALLOW_MORE_CV_QUAL
15383 | UNIFY_ALLOW_DERIVED
);
15387 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
15391 arg_strict
= UNIFY_ALLOW_NONE
;
15395 gcc_unreachable ();
15398 /* We only do these transformations if this is the top-level
15399 parameter_type_list in a call or declaration matching; in other
15400 situations (nested function declarators, template argument lists) we
15401 won't be comparing a type to an expression, and we don't do any type
15407 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
15408 if (type_unknown_p (arg
))
15410 /* [temp.deduct.type] A template-argument can be
15411 deduced from a pointer to function or pointer
15412 to member function argument if the set of
15413 overloaded functions does not contain function
15414 templates and at most one of a set of
15415 overloaded functions provides a unique
15418 if (resolve_overloaded_unification
15419 (tparms
, targs
, parm
, arg
, strict
,
15420 arg_strict
, explain_p
))
15421 return unify_success (explain_p
);
15422 return unify_overload_resolution_failure (explain_p
, arg
);
15426 arg
= unlowered_expr_type (arg
);
15427 if (arg
== error_mark_node
)
15428 return unify_invalid (explain_p
);
15432 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
15435 gcc_assert ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
15436 == (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
));
15438 /* For deduction from an init-list we need the actual list. */
15439 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
15441 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
15444 /* Most parms like fn_type_unification.
15446 If SUBR is 1, we're being called recursively (to unify the
15447 arguments of a function or method parameter of a function
15451 type_unification_real (tree tparms
,
15455 unsigned int xnargs
,
15457 unification_kind_t strict
,
15463 int ntparms
= TREE_VEC_LENGTH (tparms
);
15464 int saw_undeduced
= 0;
15467 unsigned int nargs
;
15470 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
15471 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
15472 gcc_assert (ntparms
> 0);
15474 /* Reset the number of non-defaulted template arguments contained
15476 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
15484 while (parms
&& parms
!= void_list_node
15487 parm
= TREE_VALUE (parms
);
15489 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
15490 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
15491 /* For a function parameter pack that occurs at the end of the
15492 parameter-declaration-list, the type A of each remaining
15493 argument of the call is compared with the type P of the
15494 declarator-id of the function parameter pack. */
15497 parms
= TREE_CHAIN (parms
);
15499 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
15500 /* For a function parameter pack that does not occur at the
15501 end of the parameter-declaration-list, the type of the
15502 parameter pack is a non-deduced context. */
15508 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
15514 && parms
!= void_list_node
15515 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
15517 /* Unify the remaining arguments with the pack expansion type. */
15519 tree parmvec
= make_tree_vec (1);
15521 /* Allocate a TREE_VEC and copy in all of the arguments */
15522 argvec
= make_tree_vec (nargs
- ia
);
15523 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
15524 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
15526 /* Copy the parameter into parmvec. */
15527 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
15528 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
15529 /*subr=*/subr
, explain_p
))
15532 /* Advance to the end of the list of parameters. */
15533 parms
= TREE_CHAIN (parms
);
15536 /* Fail if we've reached the end of the parm list, and more args
15537 are present, and the parm list isn't variadic. */
15538 if (ia
< nargs
&& parms
== void_list_node
)
15539 return unify_too_many_arguments (explain_p
, nargs
, ia
);
15540 /* Fail if parms are left and they don't have default values. */
15541 if (parms
&& parms
!= void_list_node
15542 && TREE_PURPOSE (parms
) == NULL_TREE
)
15544 unsigned int count
= nargs
;
15546 while (p
&& p
!= void_list_node
)
15549 p
= TREE_CHAIN (p
);
15551 return unify_too_few_arguments (explain_p
, ia
, count
);
15556 tsubst_flags_t complain
= (explain_p
15557 ? tf_warning_or_error
15560 for (i
= 0; i
< ntparms
; i
++)
15562 tree targ
= TREE_VEC_ELT (targs
, i
);
15563 tree tparm
= TREE_VEC_ELT (tparms
, i
);
15565 /* Clear the "incomplete" flags on all argument packs now so that
15566 substituting them into later default arguments works. */
15567 if (targ
&& ARGUMENT_PACK_P (targ
))
15569 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
15570 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
15573 if (targ
|| tparm
== error_mark_node
)
15575 tparm
= TREE_VALUE (tparm
);
15577 /* If this is an undeduced nontype parameter that depends on
15578 a type parameter, try another pass; its type may have been
15579 deduced from a later argument than the one from which
15580 this parameter can be deduced. */
15581 if (TREE_CODE (tparm
) == PARM_DECL
15582 && uses_template_parms (TREE_TYPE (tparm
))
15583 && !saw_undeduced
++)
15586 /* Core issue #226 (C++0x) [temp.deduct]:
15588 If a template argument has not been deduced, its
15589 default template argument, if any, is used.
15591 When we are in C++98 mode, TREE_PURPOSE will either
15592 be NULL_TREE or ERROR_MARK_NODE, so we do not need
15593 to explicitly check cxx_dialect here. */
15594 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
15596 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
15597 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
15598 location_t save_loc
= input_location
;
15600 input_location
= DECL_SOURCE_LOCATION (parm
);
15601 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
15602 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
15604 input_location
= save_loc
;
15605 if (arg
== error_mark_node
)
15609 TREE_VEC_ELT (targs
, i
) = arg
;
15610 /* The position of the first default template argument,
15611 is also the number of non-defaulted arguments in TARGS.
15613 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
15614 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
15619 /* If the type parameter is a parameter pack, then it will
15620 be deduced to an empty parameter pack. */
15621 if (template_parameter_pack_p (tparm
))
15625 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
15627 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
15628 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
15629 TREE_CONSTANT (arg
) = 1;
15632 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
15634 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
15636 TREE_VEC_ELT (targs
, i
) = arg
;
15640 return unify_parameter_deduction_failure (explain_p
, tparm
);
15643 #ifdef ENABLE_CHECKING
15644 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
15645 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
15648 return unify_success (explain_p
);
15651 /* Subroutine of type_unification_real. Args are like the variables
15652 at the call site. ARG is an overloaded function (or template-id);
15653 we try deducing template args from each of the overloads, and if
15654 only one succeeds, we go with that. Modifies TARGS and returns
15655 true on success. */
15658 resolve_overloaded_unification (tree tparms
,
15662 unification_kind_t strict
,
15666 tree tempargs
= copy_node (targs
);
15668 tree goodfn
= NULL_TREE
;
15671 if (TREE_CODE (arg
) == ADDR_EXPR
)
15673 arg
= TREE_OPERAND (arg
, 0);
15679 if (TREE_CODE (arg
) == COMPONENT_REF
)
15680 /* Handle `&x' where `x' is some static or non-static member
15682 arg
= TREE_OPERAND (arg
, 1);
15684 if (TREE_CODE (arg
) == OFFSET_REF
)
15685 arg
= TREE_OPERAND (arg
, 1);
15687 /* Strip baselink information. */
15688 if (BASELINK_P (arg
))
15689 arg
= BASELINK_FUNCTIONS (arg
);
15691 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
15693 /* If we got some explicit template args, we need to plug them into
15694 the affected templates before we try to unify, in case the
15695 explicit args will completely resolve the templates in question. */
15698 tree expl_subargs
= TREE_OPERAND (arg
, 1);
15699 arg
= TREE_OPERAND (arg
, 0);
15701 for (; arg
; arg
= OVL_NEXT (arg
))
15703 tree fn
= OVL_CURRENT (arg
);
15704 tree subargs
, elem
;
15706 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
15709 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15710 expl_subargs
, NULL_TREE
, tf_none
,
15711 /*require_all_args=*/true,
15712 /*use_default_args=*/true);
15713 if (subargs
!= error_mark_node
15714 && !any_dependent_template_arguments_p (subargs
))
15716 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
15717 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
15718 elem
, strict
, sub_strict
, addr_p
, explain_p
)
15719 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
15728 /* If no templates (or more than one) are fully resolved by the
15729 explicit arguments, this template-id is a non-deduced context; it
15730 could still be OK if we deduce all template arguments for the
15731 enclosing call through other arguments. */
15735 else if (TREE_CODE (arg
) != OVERLOAD
15736 && TREE_CODE (arg
) != FUNCTION_DECL
)
15737 /* If ARG is, for example, "(0, &f)" then its type will be unknown
15738 -- but the deduction does not succeed because the expression is
15739 not just the function on its own. */
15742 for (; arg
; arg
= OVL_NEXT (arg
))
15743 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
15744 TREE_TYPE (OVL_CURRENT (arg
)),
15745 strict
, sub_strict
, addr_p
, explain_p
)
15746 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
15748 goodfn
= OVL_CURRENT (arg
);
15752 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15753 to function or pointer to member function argument if the set of
15754 overloaded functions does not contain function templates and at most
15755 one of a set of overloaded functions provides a unique match.
15757 So if we found multiple possibilities, we return success but don't
15758 deduce anything. */
15762 int i
= TREE_VEC_LENGTH (targs
);
15764 if (TREE_VEC_ELT (tempargs
, i
))
15765 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
15773 /* Core DR 115: In contexts where deduction is done and fails, or in
15774 contexts where deduction is not done, if a template argument list is
15775 specified and it, along with any default template arguments, identifies
15776 a single function template specialization, then the template-id is an
15777 lvalue for the function template specialization. */
15780 resolve_nondeduced_context (tree orig_expr
)
15782 tree expr
, offset
, baselink
;
15785 if (!type_unknown_p (orig_expr
))
15790 offset
= NULL_TREE
;
15791 baselink
= NULL_TREE
;
15793 if (TREE_CODE (expr
) == ADDR_EXPR
)
15795 expr
= TREE_OPERAND (expr
, 0);
15798 if (TREE_CODE (expr
) == OFFSET_REF
)
15801 expr
= TREE_OPERAND (expr
, 1);
15803 if (BASELINK_P (expr
))
15806 expr
= BASELINK_FUNCTIONS (expr
);
15809 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
15812 tree goodfn
= NULL_TREE
;
15814 /* If we got some explicit template args, we need to plug them into
15815 the affected templates before we try to unify, in case the
15816 explicit args will completely resolve the templates in question. */
15818 tree expl_subargs
= TREE_OPERAND (expr
, 1);
15819 tree arg
= TREE_OPERAND (expr
, 0);
15820 tree badfn
= NULL_TREE
;
15821 tree badargs
= NULL_TREE
;
15823 for (; arg
; arg
= OVL_NEXT (arg
))
15825 tree fn
= OVL_CURRENT (arg
);
15826 tree subargs
, elem
;
15828 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
15831 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15832 expl_subargs
, NULL_TREE
, tf_none
,
15833 /*require_all_args=*/true,
15834 /*use_default_args=*/true);
15835 if (subargs
!= error_mark_node
15836 && !any_dependent_template_arguments_p (subargs
))
15838 elem
= instantiate_template (fn
, subargs
, tf_none
);
15839 if (elem
== error_mark_node
)
15844 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
15853 mark_used (goodfn
);
15856 expr
= build_baselink (BASELINK_BINFO (baselink
),
15857 BASELINK_ACCESS_BINFO (baselink
),
15858 expr
, BASELINK_OPTYPE (baselink
));
15862 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
15863 expr
= build_offset_ref (base
, expr
, addr
);
15866 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
15869 else if (good
== 0 && badargs
)
15870 /* There were no good options and at least one bad one, so let the
15871 user know what the problem is. */
15872 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
15877 /* Subroutine of resolve_overloaded_unification; does deduction for a single
15878 overload. Fills TARGS with any deduced arguments, or error_mark_node if
15879 different overloads deduce different arguments for a given parm.
15880 ADDR_P is true if the expression for which deduction is being
15881 performed was of the form "& fn" rather than simply "fn".
15883 Returns 1 on success. */
15886 try_one_overload (tree tparms
,
15891 unification_kind_t strict
,
15900 if (arg
== error_mark_node
)
15903 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15904 to function or pointer to member function argument if the set of
15905 overloaded functions does not contain function templates and at most
15906 one of a set of overloaded functions provides a unique match.
15908 So if this is a template, just return success. */
15910 if (uses_template_parms (arg
))
15913 if (TREE_CODE (arg
) == METHOD_TYPE
)
15914 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
15916 arg
= build_pointer_type (arg
);
15918 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
15920 /* We don't copy orig_targs for this because if we have already deduced
15921 some template args from previous args, unify would complain when we
15922 try to deduce a template parameter for the same argument, even though
15923 there isn't really a conflict. */
15924 nargs
= TREE_VEC_LENGTH (targs
);
15925 tempargs
= make_tree_vec (nargs
);
15927 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
15930 /* First make sure we didn't deduce anything that conflicts with
15931 explicitly specified args. */
15932 for (i
= nargs
; i
--; )
15934 tree elt
= TREE_VEC_ELT (tempargs
, i
);
15935 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
15939 else if (uses_template_parms (elt
))
15940 /* Since we're unifying against ourselves, we will fill in
15941 template args used in the function parm list with our own
15942 template parms. Discard them. */
15943 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
15944 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
15948 for (i
= nargs
; i
--; )
15950 tree elt
= TREE_VEC_ELT (tempargs
, i
);
15953 TREE_VEC_ELT (targs
, i
) = elt
;
15959 /* PARM is a template class (perhaps with unbound template
15960 parameters). ARG is a fully instantiated type. If ARG can be
15961 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
15962 TARGS are as for unify. */
15965 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
15968 tree copy_of_targs
;
15970 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
15971 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
15972 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
15975 /* We need to make a new template argument vector for the call to
15976 unify. If we used TARGS, we'd clutter it up with the result of
15977 the attempted unification, even if this class didn't work out.
15978 We also don't want to commit ourselves to all the unifications
15979 we've already done, since unification is supposed to be done on
15980 an argument-by-argument basis. In other words, consider the
15981 following pathological case:
15983 template <int I, int J, int K>
15986 template <int I, int J>
15987 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
15989 template <int I, int J, int K>
15990 void f(S<I, J, K>, S<I, I, I>);
15999 Now, by the time we consider the unification involving `s2', we
16000 already know that we must have `f<0, 0, 0>'. But, even though
16001 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16002 because there are two ways to unify base classes of S<0, 1, 2>
16003 with S<I, I, I>. If we kept the already deduced knowledge, we
16004 would reject the possibility I=1. */
16005 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
16007 /* If unification failed, we're done. */
16008 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
16009 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
16015 /* Given a template type PARM and a class type ARG, find the unique
16016 base type in ARG that is an instance of PARM. We do not examine
16017 ARG itself; only its base-classes. If there is not exactly one
16018 appropriate base class, return NULL_TREE. PARM may be the type of
16019 a partial specialization, as well as a plain template type. Used
16022 static enum template_base_result
16023 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
16024 bool explain_p
, tree
*result
)
16026 tree rval
= NULL_TREE
;
16029 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
16031 binfo
= TYPE_BINFO (complete_type (arg
));
16034 /* The type could not be completed. */
16035 *result
= NULL_TREE
;
16036 return tbr_incomplete_type
;
16039 /* Walk in inheritance graph order. The search order is not
16040 important, and this avoids multiple walks of virtual bases. */
16041 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
16043 tree r
= try_class_unification (tparms
, targs
, parm
,
16044 BINFO_TYPE (binfo
), explain_p
);
16048 /* If there is more than one satisfactory baseclass, then:
16052 If they yield more than one possible deduced A, the type
16056 if (rval
&& !same_type_p (r
, rval
))
16058 *result
= NULL_TREE
;
16059 return tbr_ambiguous_baseclass
;
16067 return tbr_success
;
16070 /* Returns the level of DECL, which declares a template parameter. */
16073 template_decl_level (tree decl
)
16075 switch (TREE_CODE (decl
))
16078 case TEMPLATE_DECL
:
16079 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
16082 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
16085 gcc_unreachable ();
16090 /* Decide whether ARG can be unified with PARM, considering only the
16091 cv-qualifiers of each type, given STRICT as documented for unify.
16092 Returns nonzero iff the unification is OK on that basis. */
16095 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
16097 int arg_quals
= cp_type_quals (arg
);
16098 int parm_quals
= cp_type_quals (parm
);
16100 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16101 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
16103 /* Although a CVR qualifier is ignored when being applied to a
16104 substituted template parameter ([8.3.2]/1 for example), that
16105 does not allow us to unify "const T" with "int&" because both
16106 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16107 It is ok when we're allowing additional CV qualifiers
16108 at the outer level [14.8.2.1]/3,1st bullet. */
16109 if ((TREE_CODE (arg
) == REFERENCE_TYPE
16110 || TREE_CODE (arg
) == FUNCTION_TYPE
16111 || TREE_CODE (arg
) == METHOD_TYPE
)
16112 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
16115 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
16116 && (parm_quals
& TYPE_QUAL_RESTRICT
))
16120 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
16121 && (arg_quals
& parm_quals
) != parm_quals
)
16124 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
16125 && (parm_quals
& arg_quals
) != arg_quals
)
16131 /* Determines the LEVEL and INDEX for the template parameter PARM. */
16133 template_parm_level_and_index (tree parm
, int* level
, int* index
)
16135 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16136 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16137 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16139 *index
= TEMPLATE_TYPE_IDX (parm
);
16140 *level
= TEMPLATE_TYPE_LEVEL (parm
);
16144 *index
= TEMPLATE_PARM_IDX (parm
);
16145 *level
= TEMPLATE_PARM_LEVEL (parm
);
16149 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
16151 if (unify (TP, TA, P, A, S, EP)) \
16155 /* Unifies the remaining arguments in PACKED_ARGS with the pack
16156 expansion at the end of PACKED_PARMS. Returns 0 if the type
16157 deduction succeeds, 1 otherwise. STRICT is the same as in
16158 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
16159 call argument list. We'll need to adjust the arguments to make them
16160 types. SUBR tells us if this is from a recursive call to
16161 type_unification_real, or for comparing two template argument
16165 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
16166 tree packed_args
, unification_kind_t strict
,
16167 bool subr
, bool explain_p
)
16170 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
16171 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
16172 tree pack
, packs
= NULL_TREE
;
16173 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
16174 int len
= TREE_VEC_LENGTH (packed_args
);
16176 /* Determine the parameter packs we will be deducing from the
16177 pattern, and record their current deductions. */
16178 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
16179 pack
; pack
= TREE_CHAIN (pack
))
16181 tree parm_pack
= TREE_VALUE (pack
);
16184 /* Determine the index and level of this parameter pack. */
16185 template_parm_level_and_index (parm_pack
, &level
, &idx
);
16187 /* Keep track of the parameter packs and their corresponding
16189 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
16190 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
16193 /* Loop through all of the arguments that have not yet been
16194 unified and unify each with the pattern. */
16195 for (i
= start
; i
< len
; i
++)
16198 bool any_explicit
= false;
16199 tree arg
= TREE_VEC_ELT (packed_args
, i
);
16201 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16202 or the element of its argument pack at the current index if
16203 this argument was explicitly specified. */
16204 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16208 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16211 if (TREE_VALUE (pack
)
16212 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
16213 && (i
< TREE_VEC_LENGTH (pargs
)))
16215 any_explicit
= true;
16216 arg
= TREE_VEC_ELT (pargs
, i
);
16218 TMPL_ARG (targs
, level
, idx
) = arg
;
16221 /* If we had explicit template arguments, substitute them into the
16222 pattern before deduction. */
16225 /* Some arguments might still be unspecified or dependent. */
16227 ++processing_template_decl
;
16228 dependent
= any_dependent_template_arguments_p (targs
);
16230 --processing_template_decl
;
16231 parm
= tsubst (pattern
, targs
,
16232 explain_p
? tf_warning_or_error
: tf_none
,
16235 --processing_template_decl
;
16236 if (parm
== error_mark_node
)
16242 /* Unify the pattern with the current argument. */
16243 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
16244 LOOKUP_IMPLICIT
, explain_p
))
16247 /* For each parameter pack, collect the deduced value. */
16248 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16251 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16253 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
16254 TMPL_ARG (targs
, level
, idx
);
16258 /* Verify that the results of unification with the parameter packs
16259 produce results consistent with what we've seen before, and make
16260 the deduced argument packs available. */
16261 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16263 tree old_pack
= TREE_VALUE (pack
);
16264 tree new_args
= TREE_TYPE (pack
);
16265 int i
, len
= TREE_VEC_LENGTH (new_args
);
16267 bool nondeduced_p
= false;
16269 /* By default keep the original deduced argument pack.
16270 If necessary, more specific code is going to update the
16271 resulting deduced argument later down in this function. */
16272 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16273 TMPL_ARG (targs
, level
, idx
) = old_pack
;
16275 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16276 actually deduce anything. */
16277 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
16278 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
16279 nondeduced_p
= true;
16283 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
16285 /* If we had fewer function args than explicit template args,
16286 just use the explicits. */
16287 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16288 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
16289 if (len
< explicit_len
)
16290 new_args
= explicit_args
;
16296 /* Build the deduced *_ARGUMENT_PACK. */
16297 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
16299 result
= make_node (NONTYPE_ARGUMENT_PACK
);
16300 TREE_TYPE (result
) =
16301 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
16302 TREE_CONSTANT (result
) = 1;
16305 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
16307 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
16309 /* Note the deduced argument packs for this parameter
16311 TMPL_ARG (targs
, level
, idx
) = result
;
16313 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
16314 && (ARGUMENT_PACK_ARGS (old_pack
)
16315 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
16317 /* We only had the explicitly-provided arguments before, but
16318 now we have a complete set of arguments. */
16319 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16321 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
16322 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
16323 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
16327 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
16328 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
16330 if (!comp_template_args_with_info (old_args
, new_args
,
16331 &bad_old_arg
, &bad_new_arg
))
16332 /* Inconsistent unification of this parameter pack. */
16333 return unify_parameter_pack_inconsistent (explain_p
,
16339 return unify_success (explain_p
);
16342 /* Deduce the value of template parameters. TPARMS is the (innermost)
16343 set of template parameters to a template. TARGS is the bindings
16344 for those template parameters, as determined thus far; TARGS may
16345 include template arguments for outer levels of template parameters
16346 as well. PARM is a parameter to a template function, or a
16347 subcomponent of that parameter; ARG is the corresponding argument.
16348 This function attempts to match PARM with ARG in a manner
16349 consistent with the existing assignments in TARGS. If more values
16350 are deduced, then TARGS is updated.
16352 Returns 0 if the type deduction succeeds, 1 otherwise. The
16353 parameter STRICT is a bitwise or of the following flags:
16356 Require an exact match between PARM and ARG.
16357 UNIFY_ALLOW_MORE_CV_QUAL:
16358 Allow the deduced ARG to be more cv-qualified (by qualification
16359 conversion) than ARG.
16360 UNIFY_ALLOW_LESS_CV_QUAL:
16361 Allow the deduced ARG to be less cv-qualified than ARG.
16362 UNIFY_ALLOW_DERIVED:
16363 Allow the deduced ARG to be a template base class of ARG,
16364 or a pointer to a template base class of the type pointed to by
16366 UNIFY_ALLOW_INTEGER:
16367 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
16368 case for more information.
16369 UNIFY_ALLOW_OUTER_LEVEL:
16370 This is the outermost level of a deduction. Used to determine validity
16371 of qualification conversions. A valid qualification conversion must
16372 have const qualified pointers leading up to the inner type which
16373 requires additional CV quals, except at the outer level, where const
16374 is not required [conv.qual]. It would be normal to set this flag in
16375 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
16376 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
16377 This is the outermost level of a deduction, and PARM can be more CV
16378 qualified at this point.
16379 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
16380 This is the outermost level of a deduction, and PARM can be less CV
16381 qualified at this point. */
16384 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
16390 int strict_in
= strict
;
16392 /* I don't think this will do the right thing with respect to types.
16393 But the only case I've seen it in so far has been array bounds, where
16394 signedness is the only information lost, and I think that will be
16396 while (TREE_CODE (parm
) == NOP_EXPR
)
16397 parm
= TREE_OPERAND (parm
, 0);
16399 if (arg
== error_mark_node
)
16400 return unify_invalid (explain_p
);
16401 if (arg
== unknown_type_node
16402 || arg
== init_list_type_node
)
16403 /* We can't deduce anything from this, but we might get all the
16404 template args from other function args. */
16405 return unify_success (explain_p
);
16407 /* If PARM uses template parameters, then we can't bail out here,
16408 even if ARG == PARM, since we won't record unifications for the
16409 template parameters. We might need them if we're trying to
16410 figure out which of two things is more specialized. */
16411 if (arg
== parm
&& !uses_template_parms (parm
))
16412 return unify_success (explain_p
);
16414 /* Handle init lists early, so the rest of the function can assume
16415 we're dealing with a type. */
16416 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
16420 tree orig_parm
= parm
;
16422 /* Replace T with std::initializer_list<T> for deduction. */
16423 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16424 && flag_deduce_init_list
)
16425 parm
= listify (parm
);
16427 if (!is_std_init_list (parm
))
16428 /* We can only deduce from an initializer list argument if the
16429 parameter is std::initializer_list; otherwise this is a
16430 non-deduced context. */
16431 return unify_success (explain_p
);
16433 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
16435 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
16437 int elt_strict
= strict
;
16439 if (elt
== error_mark_node
)
16440 return unify_invalid (explain_p
);
16442 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
16444 tree type
= TREE_TYPE (elt
);
16445 /* It should only be possible to get here for a call. */
16446 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
16447 elt_strict
|= maybe_adjust_types_for_deduction
16448 (DEDUCE_CALL
, &elttype
, &type
, elt
);
16452 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
16456 /* If the std::initializer_list<T> deduction worked, replace the
16457 deduced A with std::initializer_list<A>. */
16458 if (orig_parm
!= parm
)
16460 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
16461 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16462 targ
= listify (targ
);
16463 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
16465 return unify_success (explain_p
);
16468 /* Immediately reject some pairs that won't unify because of
16469 cv-qualification mismatches. */
16470 if (TREE_CODE (arg
) == TREE_CODE (parm
)
16472 /* It is the elements of the array which hold the cv quals of an array
16473 type, and the elements might be template type parms. We'll check
16474 when we recurse. */
16475 && TREE_CODE (arg
) != ARRAY_TYPE
16476 /* We check the cv-qualifiers when unifying with template type
16477 parameters below. We want to allow ARG `const T' to unify with
16478 PARM `T' for example, when computing which of two templates
16479 is more specialized, for example. */
16480 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
16481 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
16482 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16484 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
16485 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
16486 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
16487 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
16488 strict
&= ~UNIFY_ALLOW_DERIVED
;
16489 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
16490 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
16492 switch (TREE_CODE (parm
))
16494 case TYPENAME_TYPE
:
16496 case UNBOUND_CLASS_TEMPLATE
:
16497 /* In a type which contains a nested-name-specifier, template
16498 argument values cannot be deduced for template parameters used
16499 within the nested-name-specifier. */
16500 return unify_success (explain_p
);
16502 case TEMPLATE_TYPE_PARM
:
16503 case TEMPLATE_TEMPLATE_PARM
:
16504 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16505 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
16506 if (tparm
== error_mark_node
)
16507 return unify_invalid (explain_p
);
16509 if (TEMPLATE_TYPE_LEVEL (parm
)
16510 != template_decl_level (tparm
))
16511 /* The PARM is not one we're trying to unify. Just check
16512 to see if it matches ARG. */
16514 if (TREE_CODE (arg
) == TREE_CODE (parm
)
16515 && (is_auto (parm
) ? is_auto (arg
)
16516 : same_type_p (parm
, arg
)))
16517 return unify_success (explain_p
);
16519 return unify_type_mismatch (explain_p
, parm
, arg
);
16521 idx
= TEMPLATE_TYPE_IDX (parm
);
16522 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16523 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
16524 if (tparm
== error_mark_node
)
16525 return unify_invalid (explain_p
);
16527 /* Check for mixed types and values. */
16528 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16529 && TREE_CODE (tparm
) != TYPE_DECL
)
16530 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16531 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
16532 gcc_unreachable ();
16534 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16536 /* ARG must be constructed from a template class or a template
16537 template parameter. */
16538 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
16539 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
16540 return unify_template_deduction_failure (explain_p
, parm
, arg
);
16543 tree parmvec
= TYPE_TI_ARGS (parm
);
16544 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
16545 tree full_argvec
= add_to_template_args (targs
, argvec
);
16547 = DECL_INNERMOST_TEMPLATE_PARMS
16548 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
16550 int parm_variadic_p
= 0;
16552 /* The resolution to DR150 makes clear that default
16553 arguments for an N-argument may not be used to bind T
16554 to a template template parameter with fewer than N
16555 parameters. It is not safe to permit the binding of
16556 default arguments as an extension, as that may change
16557 the meaning of a conforming program. Consider:
16559 struct Dense { static const unsigned int dim = 1; };
16561 template <template <typename> class View,
16563 void operator+(float, View<Block> const&);
16565 template <typename Block,
16566 unsigned int Dim = Block::dim>
16567 struct Lvalue_proxy { operator float() const; };
16571 Lvalue_proxy<Dense> p;
16576 Here, if Lvalue_proxy is permitted to bind to View, then
16577 the global operator+ will be used; if they are not, the
16578 Lvalue_proxy will be converted to float. */
16579 if (coerce_template_parms (parm_parms
,
16581 TYPE_TI_TEMPLATE (parm
),
16583 ? tf_warning_or_error
16585 /*require_all_args=*/true,
16586 /*use_default_args=*/false)
16587 == error_mark_node
)
16590 /* Deduce arguments T, i from TT<T> or TT<i>.
16591 We check each element of PARMVEC and ARGVEC individually
16592 rather than the whole TREE_VEC since they can have
16593 different number of elements. */
16595 parmvec
= expand_template_argument_pack (parmvec
);
16596 argvec
= expand_template_argument_pack (argvec
);
16598 len
= TREE_VEC_LENGTH (parmvec
);
16600 /* Check if the parameters end in a pack, making them
16603 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
16604 parm_variadic_p
= 1;
16606 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16607 /* If the template argument list of P contains a pack
16608 expansion that is not the last template argument, the
16609 entire template argument list is a non-deduced
16611 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
16612 return unify_success (explain_p
);
16614 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
16615 return unify_too_few_arguments (explain_p
,
16616 TREE_VEC_LENGTH (argvec
), len
);
16618 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16620 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
16621 TREE_VEC_ELT (parmvec
, i
),
16622 TREE_VEC_ELT (argvec
, i
),
16623 UNIFY_ALLOW_NONE
, explain_p
);
16626 if (parm_variadic_p
16627 && unify_pack_expansion (tparms
, targs
,
16630 /*subr=*/true, explain_p
))
16633 arg
= TYPE_TI_TEMPLATE (arg
);
16635 /* Fall through to deduce template name. */
16638 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16639 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16641 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
16643 /* Simple cases: Value already set, does match or doesn't. */
16644 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
16645 return unify_success (explain_p
);
16647 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
16651 /* If PARM is `const T' and ARG is only `int', we don't have
16652 a match unless we are allowing additional qualification.
16653 If ARG is `const int' and PARM is just `T' that's OK;
16654 that binds `const int' to `T'. */
16655 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
16657 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16659 /* Consider the case where ARG is `const volatile int' and
16660 PARM is `const T'. Then, T should be `volatile int'. */
16661 arg
= cp_build_qualified_type_real
16662 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
16663 if (arg
== error_mark_node
)
16664 return unify_invalid (explain_p
);
16666 /* Simple cases: Value already set, does match or doesn't. */
16667 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
16668 return unify_success (explain_p
);
16670 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
16672 /* Make sure that ARG is not a variable-sized array. (Note
16673 that were talking about variable-sized arrays (like
16674 `int[n]'), rather than arrays of unknown size (like
16675 `int[]').) We'll get very confused by such a type since
16676 the bound of the array is not constant, and therefore
16677 not mangleable. Besides, such types are not allowed in
16678 ISO C++, so we can do as we please here. We do allow
16679 them for 'auto' deduction, since that isn't ABI-exposed. */
16680 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
16681 return unify_vla_arg (explain_p
, arg
);
16683 /* Strip typedefs as in convert_template_argument. */
16684 arg
= canonicalize_type_argument (arg
, tf_none
);
16687 /* If ARG is a parameter pack or an expansion, we cannot unify
16688 against it unless PARM is also a parameter pack. */
16689 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
16690 && !template_parameter_pack_p (parm
))
16691 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
16693 /* If the argument deduction results is a METHOD_TYPE,
16694 then there is a problem.
16695 METHOD_TYPE doesn't map to any real C++ type the result of
16696 the deduction can not be of that type. */
16697 if (TREE_CODE (arg
) == METHOD_TYPE
)
16698 return unify_method_type_error (explain_p
, arg
);
16700 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
16701 return unify_success (explain_p
);
16703 case TEMPLATE_PARM_INDEX
:
16704 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
16705 if (tparm
== error_mark_node
)
16706 return unify_invalid (explain_p
);
16708 if (TEMPLATE_PARM_LEVEL (parm
)
16709 != template_decl_level (tparm
))
16711 /* The PARM is not one we're trying to unify. Just check
16712 to see if it matches ARG. */
16713 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
16714 && cp_tree_equal (parm
, arg
));
16716 unify_expression_unequal (explain_p
, parm
, arg
);
16720 idx
= TEMPLATE_PARM_IDX (parm
);
16721 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16725 int x
= !cp_tree_equal (targ
, arg
);
16727 unify_inconsistency (explain_p
, parm
, targ
, arg
);
16731 /* [temp.deduct.type] If, in the declaration of a function template
16732 with a non-type template-parameter, the non-type
16733 template-parameter is used in an expression in the function
16734 parameter-list and, if the corresponding template-argument is
16735 deduced, the template-argument type shall match the type of the
16736 template-parameter exactly, except that a template-argument
16737 deduced from an array bound may be of any integral type.
16738 The non-type parameter might use already deduced type parameters. */
16739 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
16740 if (!TREE_TYPE (arg
))
16741 /* Template-parameter dependent expression. Just accept it for now.
16742 It will later be processed in convert_template_argument. */
16744 else if (same_type_p (TREE_TYPE (arg
), tparm
))
16746 else if ((strict
& UNIFY_ALLOW_INTEGER
)
16747 && (TREE_CODE (tparm
) == INTEGER_TYPE
16748 || TREE_CODE (tparm
) == BOOLEAN_TYPE
))
16749 /* Convert the ARG to the type of PARM; the deduced non-type
16750 template argument must exactly match the types of the
16751 corresponding parameter. */
16752 arg
= fold (build_nop (tparm
, arg
));
16753 else if (uses_template_parms (tparm
))
16754 /* We haven't deduced the type of this parameter yet. Try again
16756 return unify_success (explain_p
);
16758 return unify_type_mismatch (explain_p
, tparm
, arg
);
16760 /* If ARG is a parameter pack or an expansion, we cannot unify
16761 against it unless PARM is also a parameter pack. */
16762 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
16763 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
16764 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
16766 arg
= strip_typedefs_expr (arg
);
16767 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
16768 return unify_success (explain_p
);
16772 /* A pointer-to-member constant can be unified only with
16773 another constant. */
16774 if (TREE_CODE (arg
) != PTRMEM_CST
)
16775 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
16777 /* Just unify the class member. It would be useless (and possibly
16778 wrong, depending on the strict flags) to unify also
16779 PTRMEM_CST_CLASS, because we want to be sure that both parm and
16780 arg refer to the same variable, even if through different
16781 classes. For instance:
16783 struct A { int x; };
16786 Unification of &A::x and &B::x must succeed. */
16787 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
16788 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
16793 if (TREE_CODE (arg
) != POINTER_TYPE
)
16794 return unify_type_mismatch (explain_p
, parm
, arg
);
16796 /* [temp.deduct.call]
16798 A can be another pointer or pointer to member type that can
16799 be converted to the deduced A via a qualification
16800 conversion (_conv.qual_).
16802 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
16803 This will allow for additional cv-qualification of the
16804 pointed-to types if appropriate. */
16806 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
16807 /* The derived-to-base conversion only persists through one
16808 level of pointers. */
16809 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
16811 return unify (tparms
, targs
, TREE_TYPE (parm
),
16812 TREE_TYPE (arg
), strict
, explain_p
);
16815 case REFERENCE_TYPE
:
16816 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
16817 return unify_type_mismatch (explain_p
, parm
, arg
);
16818 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16819 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
16822 if (TREE_CODE (arg
) != ARRAY_TYPE
)
16823 return unify_type_mismatch (explain_p
, parm
, arg
);
16824 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
16825 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
16826 return unify_type_mismatch (explain_p
, parm
, arg
);
16827 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16828 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
16829 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
16836 /* Our representation of array types uses "N - 1" as the
16837 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
16838 not an integer constant. We cannot unify arbitrarily
16839 complex expressions, so we eliminate the MINUS_EXPRs
16841 parm_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (parm
));
16842 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
16845 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
16846 parm_max
= TREE_OPERAND (parm_max
, 0);
16848 arg_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (arg
));
16849 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
16852 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
16853 trying to unify the type of a variable with the type
16854 of a template parameter. For example:
16856 template <unsigned int N>
16857 void f (char (&) [N]);
16864 Here, the type of the ARG will be "int [g(i)]", and
16865 may be a SAVE_EXPR, etc. */
16866 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
16867 return unify_vla_arg (explain_p
, arg
);
16868 arg_max
= TREE_OPERAND (arg_max
, 0);
16871 /* If only one of the bounds used a MINUS_EXPR, compensate
16872 by adding one to the other bound. */
16873 if (parm_cst
&& !arg_cst
)
16874 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
16878 else if (arg_cst
&& !parm_cst
)
16879 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
16884 RECUR_AND_CHECK_FAILURE (tparms
, targs
, parm_max
, arg_max
,
16885 UNIFY_ALLOW_INTEGER
, explain_p
);
16887 return unify_success (explain_p
);
16894 case ENUMERAL_TYPE
:
16897 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16898 return unify_type_mismatch (explain_p
, parm
, arg
);
16900 /* We have already checked cv-qualification at the top of the
16902 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
16903 return unify_type_mismatch (explain_p
, parm
, arg
);
16905 /* As far as unification is concerned, this wins. Later checks
16906 will invalidate it if necessary. */
16907 return unify_success (explain_p
);
16909 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
16910 /* Type INTEGER_CST can come from ordinary constant template args. */
16912 while (TREE_CODE (arg
) == NOP_EXPR
)
16913 arg
= TREE_OPERAND (arg
, 0);
16915 if (TREE_CODE (arg
) != INTEGER_CST
)
16916 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16917 return (tree_int_cst_equal (parm
, arg
)
16918 ? unify_success (explain_p
)
16919 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
16923 int i
, len
, argslen
;
16924 int parm_variadic_p
= 0;
16926 if (TREE_CODE (arg
) != TREE_VEC
)
16927 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16929 len
= TREE_VEC_LENGTH (parm
);
16930 argslen
= TREE_VEC_LENGTH (arg
);
16932 /* Check for pack expansions in the parameters. */
16933 for (i
= 0; i
< len
; ++i
)
16935 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
16938 /* We can unify against something with a trailing
16940 parm_variadic_p
= 1;
16942 /* [temp.deduct.type]/9: If the template argument list of
16943 P contains a pack expansion that is not the last
16944 template argument, the entire template argument list
16945 is a non-deduced context. */
16946 return unify_success (explain_p
);
16950 /* If we don't have enough arguments to satisfy the parameters
16951 (not counting the pack expression at the end), or we have
16952 too many arguments for a parameter list that doesn't end in
16953 a pack expression, we can't unify. */
16954 if (parm_variadic_p
16955 ? argslen
< len
- parm_variadic_p
16957 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
16959 /* Unify all of the parameters that precede the (optional)
16960 pack expression. */
16961 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16963 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
16964 TREE_VEC_ELT (parm
, i
),
16965 TREE_VEC_ELT (arg
, i
),
16966 UNIFY_ALLOW_NONE
, explain_p
);
16968 if (parm_variadic_p
)
16969 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
16971 /*subr=*/true, explain_p
);
16972 return unify_success (explain_p
);
16977 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16978 return unify_type_mismatch (explain_p
, parm
, arg
);
16980 if (TYPE_PTRMEMFUNC_P (parm
))
16982 if (!TYPE_PTRMEMFUNC_P (arg
))
16983 return unify_type_mismatch (explain_p
, parm
, arg
);
16985 return unify (tparms
, targs
,
16986 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
16987 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
16988 strict
, explain_p
);
16991 if (CLASSTYPE_TEMPLATE_INFO (parm
))
16993 tree t
= NULL_TREE
;
16995 if (strict_in
& UNIFY_ALLOW_DERIVED
)
16997 /* First, we try to unify the PARM and ARG directly. */
16998 t
= try_class_unification (tparms
, targs
,
16999 parm
, arg
, explain_p
);
17003 /* Fallback to the special case allowed in
17004 [temp.deduct.call]:
17006 If P is a class, and P has the form
17007 template-id, then A can be a derived class of
17008 the deduced A. Likewise, if P is a pointer to
17009 a class of the form template-id, A can be a
17010 pointer to a derived class pointed to by the
17012 enum template_base_result r
;
17013 r
= get_template_base (tparms
, targs
, parm
, arg
,
17017 return unify_no_common_base (explain_p
, r
, parm
, arg
);
17020 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
17021 && (CLASSTYPE_TI_TEMPLATE (parm
)
17022 == CLASSTYPE_TI_TEMPLATE (arg
)))
17023 /* Perhaps PARM is something like S<U> and ARG is S<int>.
17024 Then, we should unify `int' and `U'. */
17027 /* There's no chance of unification succeeding. */
17028 return unify_type_mismatch (explain_p
, parm
, arg
);
17030 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
17031 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
17033 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
17034 return unify_type_mismatch (explain_p
, parm
, arg
);
17035 return unify_success (explain_p
);
17038 case FUNCTION_TYPE
:
17040 unsigned int nargs
;
17045 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17046 return unify_type_mismatch (explain_p
, parm
, arg
);
17048 /* CV qualifications for methods can never be deduced, they must
17049 match exactly. We need to check them explicitly here,
17050 because type_unification_real treats them as any other
17051 cv-qualified parameter. */
17052 if (TREE_CODE (parm
) == METHOD_TYPE
17053 && (!check_cv_quals_for_unify
17055 class_of_this_parm (arg
),
17056 class_of_this_parm (parm
))))
17057 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17059 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
17060 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
17062 nargs
= list_length (TYPE_ARG_TYPES (arg
));
17063 args
= XALLOCAVEC (tree
, nargs
);
17064 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
17065 a
!= NULL_TREE
&& a
!= void_list_node
;
17066 a
= TREE_CHAIN (a
), ++i
)
17067 args
[i
] = TREE_VALUE (a
);
17070 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
17071 args
, nargs
, 1, DEDUCE_EXACT
,
17072 LOOKUP_NORMAL
, explain_p
);
17076 /* Unify a pointer to member with a pointer to member function, which
17077 deduces the type of the member as a function type. */
17078 if (TYPE_PTRMEMFUNC_P (arg
))
17083 /* Check top-level cv qualifiers */
17084 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
17085 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17087 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
17088 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
17089 UNIFY_ALLOW_NONE
, explain_p
);
17091 /* Determine the type of the function we are unifying against. */
17092 method_type
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg
));
17094 build_function_type (TREE_TYPE (method_type
),
17095 TREE_CHAIN (TYPE_ARG_TYPES (method_type
)));
17097 /* Extract the cv-qualifiers of the member function from the
17098 implicit object parameter and place them on the function
17099 type to be restored later. */
17100 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (method_type
));
17101 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
17104 if (TREE_CODE (arg
) != OFFSET_TYPE
)
17105 return unify_type_mismatch (explain_p
, parm
, arg
);
17106 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
17107 TYPE_OFFSET_BASETYPE (arg
),
17108 UNIFY_ALLOW_NONE
, explain_p
);
17109 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17110 strict
, explain_p
);
17113 if (DECL_TEMPLATE_PARM_P (parm
))
17114 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
17115 if (arg
!= integral_constant_value (parm
))
17116 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17117 return unify_success (explain_p
);
17120 case TEMPLATE_DECL
:
17121 /* Matched cases are handled by the ARG == PARM test above. */
17122 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17125 /* A non-type template parameter that is a variable should be a
17126 an integral constant, in which case, it whould have been
17127 folded into its (constant) value. So we should not be getting
17128 a variable here. */
17129 gcc_unreachable ();
17131 case TYPE_ARGUMENT_PACK
:
17132 case NONTYPE_ARGUMENT_PACK
:
17133 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
17134 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
17137 case DECLTYPE_TYPE
:
17138 case UNDERLYING_TYPE
:
17139 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
17140 or UNDERLYING_TYPE nodes. */
17141 return unify_success (explain_p
);
17144 /* Unification fails if we hit an error node. */
17145 return unify_invalid (explain_p
);
17148 /* An unresolved overload is a nondeduced context. */
17149 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
17150 return unify_success (explain_p
);
17151 gcc_assert (EXPR_P (parm
));
17153 /* We must be looking at an expression. This can happen with
17157 void foo(S<I>, S<I + 2>);
17159 This is a "nondeduced context":
17163 The nondeduced contexts are:
17165 --A type that is a template-id in which one or more of
17166 the template-arguments is an expression that references
17167 a template-parameter.
17169 In these cases, we assume deduction succeeded, but don't
17170 actually infer any unifications. */
17172 if (!uses_template_parms (parm
)
17173 && !template_args_equal (parm
, arg
))
17174 return unify_expression_unequal (explain_p
, parm
, arg
);
17176 return unify_success (explain_p
);
17179 #undef RECUR_AND_CHECK_FAILURE
17181 /* Note that DECL can be defined in this translation unit, if
17185 mark_definable (tree decl
)
17188 DECL_NOT_REALLY_EXTERN (decl
) = 1;
17189 FOR_EACH_CLONE (clone
, decl
)
17190 DECL_NOT_REALLY_EXTERN (clone
) = 1;
17193 /* Called if RESULT is explicitly instantiated, or is a member of an
17194 explicitly instantiated class. */
17197 mark_decl_instantiated (tree result
, int extern_p
)
17199 SET_DECL_EXPLICIT_INSTANTIATION (result
);
17201 /* If this entity has already been written out, it's too late to
17202 make any modifications. */
17203 if (TREE_ASM_WRITTEN (result
))
17206 if (TREE_CODE (result
) != FUNCTION_DECL
)
17207 /* The TREE_PUBLIC flag for function declarations will have been
17208 set correctly by tsubst. */
17209 TREE_PUBLIC (result
) = 1;
17211 /* This might have been set by an earlier implicit instantiation. */
17212 DECL_COMDAT (result
) = 0;
17215 DECL_NOT_REALLY_EXTERN (result
) = 0;
17218 mark_definable (result
);
17219 /* Always make artificials weak. */
17220 if (DECL_ARTIFICIAL (result
) && flag_weak
)
17221 comdat_linkage (result
);
17222 /* For WIN32 we also want to put explicit instantiations in
17223 linkonce sections. */
17224 else if (TREE_PUBLIC (result
))
17225 maybe_make_one_only (result
);
17228 /* If EXTERN_P, then this function will not be emitted -- unless
17229 followed by an explicit instantiation, at which point its linkage
17230 will be adjusted. If !EXTERN_P, then this function will be
17231 emitted here. In neither circumstance do we want
17232 import_export_decl to adjust the linkage. */
17233 DECL_INTERFACE_KNOWN (result
) = 1;
17236 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17237 important template arguments. If any are missing, we check whether
17238 they're important by using error_mark_node for substituting into any
17239 args that were used for partial ordering (the ones between ARGS and END)
17240 and seeing if it bubbles up. */
17243 check_undeduced_parms (tree targs
, tree args
, tree end
)
17245 bool found
= false;
17247 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
17248 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
17251 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
17255 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
17256 if (substed
== error_mark_node
)
17262 /* Given two function templates PAT1 and PAT2, return:
17264 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17265 -1 if PAT2 is more specialized than PAT1.
17266 0 if neither is more specialized.
17268 LEN indicates the number of parameters we should consider
17269 (defaulted parameters should not be considered).
17271 The 1998 std underspecified function template partial ordering, and
17272 DR214 addresses the issue. We take pairs of arguments, one from
17273 each of the templates, and deduce them against each other. One of
17274 the templates will be more specialized if all the *other*
17275 template's arguments deduce against its arguments and at least one
17276 of its arguments *does* *not* deduce against the other template's
17277 corresponding argument. Deduction is done as for class templates.
17278 The arguments used in deduction have reference and top level cv
17279 qualifiers removed. Iff both arguments were originally reference
17280 types *and* deduction succeeds in both directions, the template
17281 with the more cv-qualified argument wins for that pairing (if
17282 neither is more cv-qualified, they both are equal). Unlike regular
17283 deduction, after all the arguments have been deduced in this way,
17284 we do *not* verify the deduced template argument values can be
17285 substituted into non-deduced contexts.
17287 The logic can be a bit confusing here, because we look at deduce1 and
17288 targs1 to see if pat2 is at least as specialized, and vice versa; if we
17289 can find template arguments for pat1 to make arg1 look like arg2, that
17290 means that arg2 is at least as specialized as arg1. */
17293 more_specialized_fn (tree pat1
, tree pat2
, int len
)
17295 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
17296 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
17297 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
17298 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
17299 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
17300 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
17301 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
17302 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
17303 tree origs1
, origs2
;
17304 bool lose1
= false;
17305 bool lose2
= false;
17307 /* Remove the this parameter from non-static member functions. If
17308 one is a non-static member function and the other is not a static
17309 member function, remove the first parameter from that function
17310 also. This situation occurs for operator functions where we
17311 locate both a member function (with this pointer) and non-member
17312 operator (with explicit first operand). */
17313 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
17315 len
--; /* LEN is the number of significant arguments for DECL1 */
17316 args1
= TREE_CHAIN (args1
);
17317 if (!DECL_STATIC_FUNCTION_P (decl2
))
17318 args2
= TREE_CHAIN (args2
);
17320 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
17322 args2
= TREE_CHAIN (args2
);
17323 if (!DECL_STATIC_FUNCTION_P (decl1
))
17326 args1
= TREE_CHAIN (args1
);
17330 /* If only one is a conversion operator, they are unordered. */
17331 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
17334 /* Consider the return type for a conversion function */
17335 if (DECL_CONV_FN_P (decl1
))
17337 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
17338 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
17342 processing_template_decl
++;
17348 /* Stop when an ellipsis is seen. */
17349 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
17351 tree arg1
= TREE_VALUE (args1
);
17352 tree arg2
= TREE_VALUE (args2
);
17353 int deduce1
, deduce2
;
17357 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
17358 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17360 /* When both arguments are pack expansions, we need only
17361 unify the patterns themselves. */
17362 arg1
= PACK_EXPANSION_PATTERN (arg1
);
17363 arg2
= PACK_EXPANSION_PATTERN (arg2
);
17365 /* This is the last comparison we need to do. */
17369 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
17371 arg1
= TREE_TYPE (arg1
);
17372 quals1
= cp_type_quals (arg1
);
17375 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
17377 arg2
= TREE_TYPE (arg2
);
17378 quals2
= cp_type_quals (arg2
);
17381 arg1
= TYPE_MAIN_VARIANT (arg1
);
17382 arg2
= TYPE_MAIN_VARIANT (arg2
);
17384 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
17386 int i
, len2
= list_length (args2
);
17387 tree parmvec
= make_tree_vec (1);
17388 tree argvec
= make_tree_vec (len2
);
17391 /* Setup the parameter vector, which contains only ARG1. */
17392 TREE_VEC_ELT (parmvec
, 0) = arg1
;
17394 /* Setup the argument vector, which contains the remaining
17396 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
17397 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
17399 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
17400 argvec
, DEDUCE_EXACT
,
17401 /*subr=*/true, /*explain_p=*/false)
17404 /* We cannot deduce in the other direction, because ARG1 is
17405 a pack expansion but ARG2 is not. */
17408 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17410 int i
, len1
= list_length (args1
);
17411 tree parmvec
= make_tree_vec (1);
17412 tree argvec
= make_tree_vec (len1
);
17415 /* Setup the parameter vector, which contains only ARG1. */
17416 TREE_VEC_ELT (parmvec
, 0) = arg2
;
17418 /* Setup the argument vector, which contains the remaining
17420 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
17421 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
17423 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
17424 argvec
, DEDUCE_EXACT
,
17425 /*subr=*/true, /*explain_p=*/false)
17428 /* We cannot deduce in the other direction, because ARG2 is
17429 a pack expansion but ARG1 is not.*/
17435 /* The normal case, where neither argument is a pack
17437 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
17438 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
17440 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
17441 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
17445 /* If we couldn't deduce arguments for tparms1 to make arg1 match
17446 arg2, then arg2 is not as specialized as arg1. */
17452 /* "If, for a given type, deduction succeeds in both directions
17453 (i.e., the types are identical after the transformations above)
17454 and if the type from the argument template is more cv-qualified
17455 than the type from the parameter template (as described above)
17456 that type is considered to be more specialized than the other. If
17457 neither type is more cv-qualified than the other then neither type
17458 is more specialized than the other." */
17460 if (deduce1
&& deduce2
17461 && quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
17463 if ((quals1
& quals2
) == quals2
)
17465 if ((quals1
& quals2
) == quals1
)
17469 if (lose1
&& lose2
)
17470 /* We've failed to deduce something in either direction.
17471 These must be unordered. */
17474 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
17475 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17476 /* We have already processed all of the arguments in our
17477 handing of the pack expansion type. */
17480 args1
= TREE_CHAIN (args1
);
17481 args2
= TREE_CHAIN (args2
);
17484 /* "In most cases, all template parameters must have values in order for
17485 deduction to succeed, but for partial ordering purposes a template
17486 parameter may remain without a value provided it is not used in the
17487 types being used for partial ordering."
17489 Thus, if we are missing any of the targs1 we need to substitute into
17490 origs1, then pat2 is not as specialized as pat1. This can happen when
17491 there is a nondeduced context. */
17492 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
17494 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
17497 processing_template_decl
--;
17499 /* All things being equal, if the next argument is a pack expansion
17500 for one function but not for the other, prefer the
17501 non-variadic function. FIXME this is bogus; see c++/41958. */
17503 && args1
&& TREE_VALUE (args1
)
17504 && args2
&& TREE_VALUE (args2
))
17506 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
17507 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
17510 if (lose1
== lose2
)
17518 /* Determine which of two partial specializations of MAIN_TMPL is more
17521 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
17522 to the first partial specialization. The TREE_VALUE is the
17523 innermost set of template parameters for the partial
17524 specialization. PAT2 is similar, but for the second template.
17526 Return 1 if the first partial specialization is more specialized;
17527 -1 if the second is more specialized; 0 if neither is more
17530 See [temp.class.order] for information about determining which of
17531 two templates is more specialized. */
17534 more_specialized_class (tree main_tmpl
, tree pat1
, tree pat2
)
17539 bool any_deductions
= false;
17541 tmpl1
= TREE_TYPE (pat1
);
17542 tmpl2
= TREE_TYPE (pat2
);
17544 /* Just like what happens for functions, if we are ordering between
17545 different class template specializations, we may encounter dependent
17546 types in the arguments, and we need our dependency check functions
17547 to behave correctly. */
17548 ++processing_template_decl
;
17549 targs
= get_class_bindings (main_tmpl
, TREE_VALUE (pat1
),
17550 CLASSTYPE_TI_ARGS (tmpl1
),
17551 CLASSTYPE_TI_ARGS (tmpl2
));
17555 any_deductions
= true;
17558 targs
= get_class_bindings (main_tmpl
, TREE_VALUE (pat2
),
17559 CLASSTYPE_TI_ARGS (tmpl2
),
17560 CLASSTYPE_TI_ARGS (tmpl1
));
17564 any_deductions
= true;
17566 --processing_template_decl
;
17568 /* In the case of a tie where at least one of the class templates
17569 has a parameter pack at the end, the template with the most
17570 non-packed parameters wins. */
17573 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
17574 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
17576 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
17577 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
17578 int len1
= TREE_VEC_LENGTH (args1
);
17579 int len2
= TREE_VEC_LENGTH (args2
);
17581 /* We don't count the pack expansion at the end. */
17582 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
17584 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
17589 else if (len1
< len2
)
17596 /* Return the template arguments that will produce the function signature
17597 DECL from the function template FN, with the explicit template
17598 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
17599 also match. Return NULL_TREE if no satisfactory arguments could be
17603 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
17605 int ntparms
= DECL_NTPARMS (fn
);
17606 tree targs
= make_tree_vec (ntparms
);
17607 tree decl_type
= TREE_TYPE (decl
);
17608 tree decl_arg_types
;
17610 unsigned int nargs
, ix
;
17613 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
17615 /* Never do unification on the 'this' parameter. */
17616 decl_arg_types
= skip_artificial_parms_for (decl
,
17617 TYPE_ARG_TYPES (decl_type
));
17619 nargs
= list_length (decl_arg_types
);
17620 args
= XALLOCAVEC (tree
, nargs
);
17621 for (arg
= decl_arg_types
, ix
= 0;
17622 arg
!= NULL_TREE
&& arg
!= void_list_node
;
17623 arg
= TREE_CHAIN (arg
), ++ix
)
17624 args
[ix
] = TREE_VALUE (arg
);
17626 if (fn_type_unification (fn
, explicit_args
, targs
,
17628 (check_rettype
|| DECL_CONV_FN_P (fn
)
17629 ? TREE_TYPE (decl_type
) : NULL_TREE
),
17630 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false)
17631 == error_mark_node
)
17637 /* Return the innermost template arguments that, when applied to a partial
17638 specialization of MAIN_TMPL whose innermost template parameters are
17639 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
17642 For example, suppose we have:
17644 template <class T, class U> struct S {};
17645 template <class T> struct S<T*, int> {};
17647 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
17648 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
17649 int}. The resulting vector will be {double}, indicating that `T'
17650 is bound to `double'. */
17653 get_class_bindings (tree main_tmpl
, tree tparms
, tree spec_args
, tree args
)
17655 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
17657 tree innermost_deduced_args
;
17659 innermost_deduced_args
= make_tree_vec (ntparms
);
17660 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
17662 deduced_args
= copy_node (args
);
17663 SET_TMPL_ARGS_LEVEL (deduced_args
,
17664 TMPL_ARGS_DEPTH (deduced_args
),
17665 innermost_deduced_args
);
17668 deduced_args
= innermost_deduced_args
;
17670 if (unify (tparms
, deduced_args
,
17671 INNERMOST_TEMPLATE_ARGS (spec_args
),
17672 INNERMOST_TEMPLATE_ARGS (args
),
17673 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
17676 for (i
= 0; i
< ntparms
; ++i
)
17677 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
17680 /* Verify that nondeduced template arguments agree with the type
17681 obtained from argument deduction.
17685 struct A { typedef int X; };
17686 template <class T, class U> struct C {};
17687 template <class T> struct C<T, typename T::X> {};
17689 Then with the instantiation `C<A, int>', we can deduce that
17690 `T' is `A' but unify () does not check whether `typename T::X'
17692 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
17693 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (main_tmpl
),
17694 spec_args
, main_tmpl
,
17695 tf_none
, false, false);
17696 if (spec_args
== error_mark_node
17697 /* We only need to check the innermost arguments; the other
17698 arguments will always agree. */
17699 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
17700 INNERMOST_TEMPLATE_ARGS (args
)))
17703 /* Now that we have bindings for all of the template arguments,
17704 ensure that the arguments deduced for the template template
17705 parameters have compatible template parameter lists. See the use
17706 of template_template_parm_bindings_ok_p in fn_type_unification
17707 for more information. */
17708 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
17711 return deduced_args
;
17714 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
17715 Return the TREE_LIST node with the most specialized template, if
17716 any. If there is no most specialized template, the error_mark_node
17719 Note that this function does not look at, or modify, the
17720 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
17721 returned is one of the elements of INSTANTIATIONS, callers may
17722 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
17723 and retrieve it from the value returned. */
17726 most_specialized_instantiation (tree templates
)
17730 ++processing_template_decl
;
17733 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
17737 if (get_bindings (TREE_VALUE (champ
),
17738 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
17739 NULL_TREE
, /*check_ret=*/true))
17742 if (get_bindings (TREE_VALUE (fn
),
17743 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
17744 NULL_TREE
, /*check_ret=*/true))
17751 /* Equally specialized, move to next function. If there
17752 is no next function, nothing's most specialized. */
17753 fn
= TREE_CHAIN (fn
);
17761 /* Now verify that champ is better than everything earlier in the
17762 instantiation list. */
17763 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
17764 if (get_bindings (TREE_VALUE (champ
),
17765 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
17766 NULL_TREE
, /*check_ret=*/true)
17767 || !get_bindings (TREE_VALUE (fn
),
17768 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
17769 NULL_TREE
, /*check_ret=*/true))
17775 processing_template_decl
--;
17778 return error_mark_node
;
17783 /* If DECL is a specialization of some template, return the most
17784 general such template. Otherwise, returns NULL_TREE.
17786 For example, given:
17788 template <class T> struct S { template <class U> void f(U); };
17790 if TMPL is `template <class U> void S<int>::f(U)' this will return
17791 the full template. This function will not trace past partial
17792 specializations, however. For example, given in addition:
17794 template <class T> struct S<T*> { template <class U> void f(U); };
17796 if TMPL is `template <class U> void S<int*>::f(U)' this will return
17797 `template <class T> template <class U> S<T*>::f(U)'. */
17800 most_general_template (tree decl
)
17802 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
17803 an immediate specialization. */
17804 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17806 if (DECL_TEMPLATE_INFO (decl
)) {
17807 decl
= DECL_TI_TEMPLATE (decl
);
17809 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
17810 template friend. */
17811 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
17817 /* Look for more and more general templates. */
17818 while (DECL_TEMPLATE_INFO (decl
))
17820 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
17821 (See cp-tree.h for details.) */
17822 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
17825 if (CLASS_TYPE_P (TREE_TYPE (decl
))
17826 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
17829 /* Stop if we run into an explicitly specialized class template. */
17830 if (!DECL_NAMESPACE_SCOPE_P (decl
)
17831 && DECL_CONTEXT (decl
)
17832 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
17835 decl
= DECL_TI_TEMPLATE (decl
);
17841 /* Return the most specialized of the class template partial
17842 specializations of TMPL which can produce TYPE, a specialization of
17843 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
17844 a _TYPE node corresponding to the partial specialization, while the
17845 TREE_PURPOSE is the set of template arguments that must be
17846 substituted into the TREE_TYPE in order to generate TYPE.
17848 If the choice of partial specialization is ambiguous, a diagnostic
17849 is issued, and the error_mark_node is returned. If there are no
17850 partial specializations of TMPL matching TYPE, then NULL_TREE is
17854 most_specialized_class (tree type
, tree tmpl
, tsubst_flags_t complain
)
17856 tree list
= NULL_TREE
;
17862 tree outer_args
= NULL_TREE
;
17864 tmpl
= most_general_template (tmpl
);
17865 args
= CLASSTYPE_TI_ARGS (type
);
17867 /* For determining which partial specialization to use, only the
17868 innermost args are interesting. */
17869 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
17871 outer_args
= strip_innermost_template_args (args
, 1);
17872 args
= INNERMOST_TEMPLATE_ARGS (args
);
17875 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
17877 tree partial_spec_args
;
17879 tree parms
= TREE_VALUE (t
);
17881 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
17883 ++processing_template_decl
;
17889 /* Discard the outer levels of args, and then substitute in the
17890 template args from the enclosing class. */
17891 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
17892 partial_spec_args
= tsubst_template_args
17893 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
17895 /* PARMS already refers to just the innermost parms, but the
17896 template parms in partial_spec_args had their levels lowered
17897 by tsubst, so we need to do the same for the parm list. We
17898 can't just tsubst the TREE_VEC itself, as tsubst wants to
17899 treat a TREE_VEC as an argument vector. */
17900 parms
= copy_node (parms
);
17901 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
17902 TREE_VEC_ELT (parms
, i
) =
17903 tsubst (TREE_VEC_ELT (parms
, i
), outer_args
, tf_none
, NULL_TREE
);
17907 partial_spec_args
=
17908 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
17909 add_to_template_args (outer_args
,
17910 partial_spec_args
),
17912 /*require_all_args=*/true,
17913 /*use_default_args=*/true);
17915 --processing_template_decl
;
17917 if (partial_spec_args
== error_mark_node
)
17918 return error_mark_node
;
17920 spec_args
= get_class_bindings (tmpl
, parms
,
17926 spec_args
= add_to_template_args (outer_args
, spec_args
);
17927 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
17928 TREE_TYPE (list
) = TREE_TYPE (t
);
17935 ambiguous_p
= false;
17938 t
= TREE_CHAIN (t
);
17939 for (; t
; t
= TREE_CHAIN (t
))
17941 fate
= more_specialized_class (tmpl
, champ
, t
);
17948 t
= TREE_CHAIN (t
);
17951 ambiguous_p
= true;
17960 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
17962 fate
= more_specialized_class (tmpl
, champ
, t
);
17965 ambiguous_p
= true;
17973 char *spaces
= NULL
;
17974 if (!(complain
& tf_error
))
17975 return error_mark_node
;
17976 error ("ambiguous class template instantiation for %q#T", type
);
17977 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
17978 for (t
= list
; t
; t
= TREE_CHAIN (t
))
17980 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
17981 spaces
= spaces
? spaces
: get_spaces (str
);
17984 return error_mark_node
;
17990 /* Explicitly instantiate DECL. */
17993 do_decl_instantiation (tree decl
, tree storage
)
17995 tree result
= NULL_TREE
;
17998 if (!decl
|| decl
== error_mark_node
)
17999 /* An error occurred, for which grokdeclarator has already issued
18000 an appropriate message. */
18002 else if (! DECL_LANG_SPECIFIC (decl
))
18004 error ("explicit instantiation of non-template %q#D", decl
);
18007 else if (TREE_CODE (decl
) == VAR_DECL
)
18009 /* There is an asymmetry here in the way VAR_DECLs and
18010 FUNCTION_DECLs are handled by grokdeclarator. In the case of
18011 the latter, the DECL we get back will be marked as a
18012 template instantiation, and the appropriate
18013 DECL_TEMPLATE_INFO will be set up. This does not happen for
18014 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
18015 should handle VAR_DECLs as it currently handles
18017 if (!DECL_CLASS_SCOPE_P (decl
))
18019 error ("%qD is not a static data member of a class template", decl
);
18022 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
18023 if (!result
|| TREE_CODE (result
) != VAR_DECL
)
18025 error ("no matching template for %qD found", decl
);
18028 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
18030 error ("type %qT for explicit instantiation %qD does not match "
18031 "declared type %qT", TREE_TYPE (result
), decl
,
18036 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
18038 error ("explicit instantiation of %q#D", decl
);
18044 /* Check for various error cases. Note that if the explicit
18045 instantiation is valid the RESULT will currently be marked as an
18046 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
18047 until we get here. */
18049 if (DECL_TEMPLATE_SPECIALIZATION (result
))
18051 /* DR 259 [temp.spec].
18053 Both an explicit instantiation and a declaration of an explicit
18054 specialization shall not appear in a program unless the explicit
18055 instantiation follows a declaration of the explicit specialization.
18057 For a given set of template parameters, if an explicit
18058 instantiation of a template appears after a declaration of an
18059 explicit specialization for that template, the explicit
18060 instantiation has no effect. */
18063 else if (DECL_EXPLICIT_INSTANTIATION (result
))
18067 No program shall explicitly instantiate any template more
18070 We check DECL_NOT_REALLY_EXTERN so as not to complain when
18071 the first instantiation was `extern' and the second is not,
18072 and EXTERN_P for the opposite case. */
18073 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
18074 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
18075 /* If an "extern" explicit instantiation follows an ordinary
18076 explicit instantiation, the template is instantiated. */
18080 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
18082 error ("no matching template for %qD found", result
);
18085 else if (!DECL_TEMPLATE_INFO (result
))
18087 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
18091 if (storage
== NULL_TREE
)
18093 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18095 if (!in_system_header
&& (cxx_dialect
== cxx98
))
18096 pedwarn (input_location
, OPT_Wpedantic
,
18097 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
18102 error ("storage class %qD applied to template instantiation", storage
);
18104 check_explicit_instantiation_namespace (result
);
18105 mark_decl_instantiated (result
, extern_p
);
18107 instantiate_decl (result
, /*defer_ok=*/1,
18108 /*expl_inst_class_mem_p=*/false);
18112 mark_class_instantiated (tree t
, int extern_p
)
18114 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
18115 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
18116 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
18117 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
18120 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
18121 rest_of_type_compilation (t
, 1);
18125 /* Called from do_type_instantiation through binding_table_foreach to
18126 do recursive instantiation for the type bound in ENTRY. */
18128 bt_instantiate_type_proc (binding_entry entry
, void *data
)
18130 tree storage
= *(tree
*) data
;
18132 if (MAYBE_CLASS_TYPE_P (entry
->type
)
18133 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
18134 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
18137 /* Called from do_type_instantiation to instantiate a member
18138 (a member function or a static member variable) of an
18139 explicitly instantiated class template. */
18141 instantiate_class_member (tree decl
, int extern_p
)
18143 mark_decl_instantiated (decl
, extern_p
);
18145 instantiate_decl (decl
, /*defer_ok=*/1,
18146 /*expl_inst_class_mem_p=*/true);
18149 /* Perform an explicit instantiation of template class T. STORAGE, if
18150 non-null, is the RID for extern, inline or static. COMPLAIN is
18151 nonzero if this is called from the parser, zero if called recursively,
18152 since the standard is unclear (as detailed below). */
18155 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
18160 int previous_instantiation_extern_p
= 0;
18162 if (TREE_CODE (t
) == TYPE_DECL
)
18165 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
18168 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
18170 error ("explicit instantiation of non-class template %qD", tmpl
);
18172 error ("explicit instantiation of non-template type %qT", t
);
18178 if (!COMPLETE_TYPE_P (t
))
18180 if (complain
& tf_error
)
18181 error ("explicit instantiation of %q#T before definition of template",
18186 if (storage
!= NULL_TREE
)
18188 if (!in_system_header
)
18190 if (storage
== ridpointers
[(int) RID_EXTERN
])
18192 if (cxx_dialect
== cxx98
)
18193 pedwarn (input_location
, OPT_Wpedantic
,
18194 "ISO C++ 1998 forbids the use of %<extern%> on "
18195 "explicit instantiations");
18198 pedwarn (input_location
, OPT_Wpedantic
,
18199 "ISO C++ forbids the use of %qE"
18200 " on explicit instantiations", storage
);
18203 if (storage
== ridpointers
[(int) RID_INLINE
])
18205 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18207 else if (storage
== ridpointers
[(int) RID_STATIC
])
18211 error ("storage class %qD applied to template instantiation",
18217 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
18219 /* DR 259 [temp.spec].
18221 Both an explicit instantiation and a declaration of an explicit
18222 specialization shall not appear in a program unless the explicit
18223 instantiation follows a declaration of the explicit specialization.
18225 For a given set of template parameters, if an explicit
18226 instantiation of a template appears after a declaration of an
18227 explicit specialization for that template, the explicit
18228 instantiation has no effect. */
18231 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
18235 No program shall explicitly instantiate any template more
18238 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18239 instantiation was `extern'. If EXTERN_P then the second is.
18240 These cases are OK. */
18241 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
18243 if (!previous_instantiation_extern_p
&& !extern_p
18244 && (complain
& tf_error
))
18245 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
18247 /* If we've already instantiated the template, just return now. */
18248 if (!CLASSTYPE_INTERFACE_ONLY (t
))
18252 check_explicit_instantiation_namespace (TYPE_NAME (t
));
18253 mark_class_instantiated (t
, extern_p
);
18261 /* In contrast to implicit instantiation, where only the
18262 declarations, and not the definitions, of members are
18263 instantiated, we have here:
18267 The explicit instantiation of a class template specialization
18268 implies the instantiation of all of its members not
18269 previously explicitly specialized in the translation unit
18270 containing the explicit instantiation.
18272 Of course, we can't instantiate member template classes, since
18273 we don't have any arguments for them. Note that the standard
18274 is unclear on whether the instantiation of the members are
18275 *explicit* instantiations or not. However, the most natural
18276 interpretation is that it should be an explicit instantiation. */
18279 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18280 if (TREE_CODE (tmp
) == FUNCTION_DECL
18281 && DECL_TEMPLATE_INSTANTIATION (tmp
))
18282 instantiate_class_member (tmp
, extern_p
);
18284 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18285 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
18286 instantiate_class_member (tmp
, extern_p
);
18288 if (CLASSTYPE_NESTED_UTDS (t
))
18289 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
18290 bt_instantiate_type_proc
, &storage
);
18294 /* Given a function DECL, which is a specialization of TMPL, modify
18295 DECL to be a re-instantiation of TMPL with the same template
18296 arguments. TMPL should be the template into which tsubst'ing
18297 should occur for DECL, not the most general template.
18299 One reason for doing this is a scenario like this:
18302 void f(const T&, int i);
18304 void g() { f(3, 7); }
18307 void f(const T& t, const int i) { }
18309 Note that when the template is first instantiated, with
18310 instantiate_template, the resulting DECL will have no name for the
18311 first parameter, and the wrong type for the second. So, when we go
18312 to instantiate the DECL, we regenerate it. */
18315 regenerate_decl_from_template (tree decl
, tree tmpl
)
18317 /* The arguments used to instantiate DECL, from the most general
18322 args
= DECL_TI_ARGS (decl
);
18323 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
18325 /* Make sure that we can see identifiers, and compute access
18327 push_access_scope (decl
);
18329 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18337 args_depth
= TMPL_ARGS_DEPTH (args
);
18338 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
18339 if (args_depth
> parms_depth
)
18340 args
= get_innermost_template_args (args
, parms_depth
);
18342 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
18343 args
, tf_error
, NULL_TREE
,
18344 /*defer_ok*/false);
18345 if (specs
&& specs
!= error_mark_node
)
18346 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
18349 /* Merge parameter declarations. */
18350 decl_parm
= skip_artificial_parms_for (decl
,
18351 DECL_ARGUMENTS (decl
));
18353 = skip_artificial_parms_for (code_pattern
,
18354 DECL_ARGUMENTS (code_pattern
));
18355 while (decl_parm
&& !FUNCTION_PARAMETER_PACK_P (pattern_parm
))
18360 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
18361 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
18362 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
18364 parm_type
= type_decays_to (parm_type
);
18365 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
18366 TREE_TYPE (decl_parm
) = parm_type
;
18367 attributes
= DECL_ATTRIBUTES (pattern_parm
);
18368 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
18370 DECL_ATTRIBUTES (decl_parm
) = attributes
;
18371 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
18373 decl_parm
= DECL_CHAIN (decl_parm
);
18374 pattern_parm
= DECL_CHAIN (pattern_parm
);
18376 /* Merge any parameters that match with the function parameter
18378 if (pattern_parm
&& FUNCTION_PARAMETER_PACK_P (pattern_parm
))
18381 tree expanded_types
;
18382 /* Expand the TYPE_PACK_EXPANSION that provides the types for
18383 the parameters in this function parameter pack. */
18384 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
18385 args
, tf_error
, NULL_TREE
);
18386 len
= TREE_VEC_LENGTH (expanded_types
);
18387 for (i
= 0; i
< len
; i
++)
18392 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
18393 /* Rename the parameter to include the index. */
18394 DECL_NAME (decl_parm
) =
18395 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
18396 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
18397 parm_type
= type_decays_to (parm_type
);
18398 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
18399 TREE_TYPE (decl_parm
) = parm_type
;
18400 attributes
= DECL_ATTRIBUTES (pattern_parm
);
18401 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
18403 DECL_ATTRIBUTES (decl_parm
) = attributes
;
18404 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
18406 decl_parm
= DECL_CHAIN (decl_parm
);
18409 /* Merge additional specifiers from the CODE_PATTERN. */
18410 if (DECL_DECLARED_INLINE_P (code_pattern
)
18411 && !DECL_DECLARED_INLINE_P (decl
))
18412 DECL_DECLARED_INLINE_P (decl
) = 1;
18414 else if (TREE_CODE (decl
) == VAR_DECL
)
18416 DECL_INITIAL (decl
) =
18417 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
18418 tf_error
, DECL_TI_TEMPLATE (decl
),
18419 /*integral_constant_expression_p=*/false);
18420 if (VAR_HAD_UNKNOWN_BOUND (decl
))
18421 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
18422 tf_error
, DECL_TI_TEMPLATE (decl
));
18425 gcc_unreachable ();
18427 pop_access_scope (decl
);
18430 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
18431 substituted to get DECL. */
18434 template_for_substitution (tree decl
)
18436 tree tmpl
= DECL_TI_TEMPLATE (decl
);
18438 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
18439 for the instantiation. This is not always the most general
18440 template. Consider, for example:
18443 struct S { template <class U> void f();
18444 template <> void f<int>(); };
18446 and an instantiation of S<double>::f<int>. We want TD to be the
18447 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
18448 while (/* An instantiation cannot have a definition, so we need a
18449 more general template. */
18450 DECL_TEMPLATE_INSTANTIATION (tmpl
)
18451 /* We must also deal with friend templates. Given:
18453 template <class T> struct S {
18454 template <class U> friend void f() {};
18457 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
18458 so far as the language is concerned, but that's still
18459 where we get the pattern for the instantiation from. On
18460 other hand, if the definition comes outside the class, say:
18462 template <class T> struct S {
18463 template <class U> friend void f();
18465 template <class U> friend void f() {}
18467 we don't need to look any further. That's what the check for
18468 DECL_INITIAL is for. */
18469 || (TREE_CODE (decl
) == FUNCTION_DECL
18470 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
18471 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
18473 /* The present template, TD, should not be a definition. If it
18474 were a definition, we should be using it! Note that we
18475 cannot restructure the loop to just keep going until we find
18476 a template with a definition, since that might go too far if
18477 a specialization was declared, but not defined. */
18478 gcc_assert (TREE_CODE (decl
) != VAR_DECL
18479 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
18481 /* Fetch the more general template. */
18482 tmpl
= DECL_TI_TEMPLATE (tmpl
);
18488 /* Returns true if we need to instantiate this template instance even if we
18489 know we aren't going to emit it.. */
18492 always_instantiate_p (tree decl
)
18494 /* We always instantiate inline functions so that we can inline them. An
18495 explicit instantiation declaration prohibits implicit instantiation of
18496 non-inline functions. With high levels of optimization, we would
18497 normally inline non-inline functions -- but we're not allowed to do
18498 that for "extern template" functions. Therefore, we check
18499 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
18500 return ((TREE_CODE (decl
) == FUNCTION_DECL
18501 && (DECL_DECLARED_INLINE_P (decl
)
18502 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
18503 /* And we need to instantiate static data members so that
18504 their initializers are available in integral constant
18506 || (TREE_CODE (decl
) == VAR_DECL
18507 && decl_maybe_constant_var_p (decl
)));
18510 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
18511 instantiate it now, modifying TREE_TYPE (fn). */
18514 maybe_instantiate_noexcept (tree fn
)
18516 tree fntype
, spec
, noex
, clone
;
18518 if (DECL_CLONED_FUNCTION_P (fn
))
18519 fn
= DECL_CLONED_FUNCTION (fn
);
18520 fntype
= TREE_TYPE (fn
);
18521 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
18523 if (!DEFERRED_NOEXCEPT_SPEC_P (spec
))
18526 noex
= TREE_PURPOSE (spec
);
18528 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
18530 if (push_tinst_level (fn
))
18532 push_access_scope (fn
);
18533 push_deferring_access_checks (dk_no_deferred
);
18534 input_location
= DECL_SOURCE_LOCATION (fn
);
18535 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
18536 DEFERRED_NOEXCEPT_ARGS (noex
),
18537 tf_warning_or_error
, fn
,
18538 /*function_p=*/false,
18539 /*integral_constant_expression_p=*/true);
18540 pop_deferring_access_checks ();
18541 pop_access_scope (fn
);
18542 pop_tinst_level ();
18543 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
18544 if (spec
== error_mark_node
)
18545 spec
= noexcept_false_spec
;
18548 spec
= noexcept_false_spec
;
18552 /* This is an implicitly declared function, so NOEX is a list of
18553 other functions to evaluate and merge. */
18555 spec
= noexcept_true_spec
;
18556 for (elt
= noex
; elt
; elt
= OVL_NEXT (elt
))
18558 tree fn
= OVL_CURRENT (elt
);
18560 maybe_instantiate_noexcept (fn
);
18561 subspec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
18562 spec
= merge_exception_specifiers (spec
, subspec
, NULL_TREE
);
18566 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
18568 FOR_EACH_CLONE (clone
, fn
)
18570 if (TREE_TYPE (clone
) == fntype
)
18571 TREE_TYPE (clone
) = TREE_TYPE (fn
);
18573 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
18577 /* Produce the definition of D, a _DECL generated from a template. If
18578 DEFER_OK is nonzero, then we don't have to actually do the
18579 instantiation now; we just have to do it sometime. Normally it is
18580 an error if this is an explicit instantiation but D is undefined.
18581 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
18582 explicitly instantiated class template. */
18585 instantiate_decl (tree d
, int defer_ok
,
18586 bool expl_inst_class_mem_p
)
18588 tree tmpl
= DECL_TI_TEMPLATE (d
);
18595 bool pattern_defined
;
18596 location_t saved_loc
= input_location
;
18601 /* This function should only be used to instantiate templates for
18602 functions and static member variables. */
18603 gcc_assert (TREE_CODE (d
) == FUNCTION_DECL
18604 || TREE_CODE (d
) == VAR_DECL
);
18606 /* Variables are never deferred; if instantiation is required, they
18607 are instantiated right away. That allows for better code in the
18608 case that an expression refers to the value of the variable --
18609 if the variable has a constant value the referring expression can
18610 take advantage of that fact. */
18611 if (TREE_CODE (d
) == VAR_DECL
18612 || DECL_DECLARED_CONSTEXPR_P (d
))
18615 /* Don't instantiate cloned functions. Instead, instantiate the
18616 functions they cloned. */
18617 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
18618 d
= DECL_CLONED_FUNCTION (d
);
18620 if (DECL_TEMPLATE_INSTANTIATED (d
)
18621 || (TREE_CODE (d
) == FUNCTION_DECL
18622 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
18623 || DECL_TEMPLATE_SPECIALIZATION (d
))
18624 /* D has already been instantiated or explicitly specialized, so
18625 there's nothing for us to do here.
18627 It might seem reasonable to check whether or not D is an explicit
18628 instantiation, and, if so, stop here. But when an explicit
18629 instantiation is deferred until the end of the compilation,
18630 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
18631 the instantiation. */
18634 /* Check to see whether we know that this template will be
18635 instantiated in some other file, as with "extern template"
18637 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
18639 /* In general, we do not instantiate such templates. */
18640 if (external_p
&& !always_instantiate_p (d
))
18643 gen_tmpl
= most_general_template (tmpl
);
18644 gen_args
= DECL_TI_ARGS (d
);
18646 if (tmpl
!= gen_tmpl
)
18647 /* We should already have the extra args. */
18648 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
18649 == TMPL_ARGS_DEPTH (gen_args
));
18650 /* And what's in the hash table should match D. */
18651 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
18652 || spec
== NULL_TREE
);
18654 /* This needs to happen before any tsubsting. */
18655 if (! push_tinst_level (d
))
18658 timevar_push (TV_TEMPLATE_INST
);
18660 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
18661 for the instantiation. */
18662 td
= template_for_substitution (d
);
18663 code_pattern
= DECL_TEMPLATE_RESULT (td
);
18665 /* We should never be trying to instantiate a member of a class
18666 template or partial specialization. */
18667 gcc_assert (d
!= code_pattern
);
18669 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
18670 || DECL_TEMPLATE_SPECIALIZATION (td
))
18671 /* In the case of a friend template whose definition is provided
18672 outside the class, we may have too many arguments. Drop the
18673 ones we don't need. The same is true for specializations. */
18674 args
= get_innermost_template_args
18675 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
18679 if (TREE_CODE (d
) == FUNCTION_DECL
)
18680 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
18681 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
));
18683 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
18685 /* We may be in the middle of deferred access check. Disable it now. */
18686 push_deferring_access_checks (dk_no_deferred
);
18688 /* Unless an explicit instantiation directive has already determined
18689 the linkage of D, remember that a definition is available for
18691 if (pattern_defined
18692 && !DECL_INTERFACE_KNOWN (d
)
18693 && !DECL_NOT_REALLY_EXTERN (d
))
18694 mark_definable (d
);
18696 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
18697 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
18698 input_location
= DECL_SOURCE_LOCATION (d
);
18700 /* If D is a member of an explicitly instantiated class template,
18701 and no definition is available, treat it like an implicit
18703 if (!pattern_defined
&& expl_inst_class_mem_p
18704 && DECL_EXPLICIT_INSTANTIATION (d
))
18706 /* Leave linkage flags alone on instantiations with anonymous
18708 if (TREE_PUBLIC (d
))
18710 DECL_NOT_REALLY_EXTERN (d
) = 0;
18711 DECL_INTERFACE_KNOWN (d
) = 0;
18713 SET_DECL_IMPLICIT_INSTANTIATION (d
);
18716 if (TREE_CODE (d
) == FUNCTION_DECL
)
18717 maybe_instantiate_noexcept (d
);
18719 /* Defer all other templates, unless we have been explicitly
18720 forbidden from doing so. */
18721 if (/* If there is no definition, we cannot instantiate the
18724 /* If it's OK to postpone instantiation, do so. */
18726 /* If this is a static data member that will be defined
18727 elsewhere, we don't want to instantiate the entire data
18728 member, but we do want to instantiate the initializer so that
18729 we can substitute that elsewhere. */
18730 || (external_p
&& TREE_CODE (d
) == VAR_DECL
))
18732 /* The definition of the static data member is now required so
18733 we must substitute the initializer. */
18734 if (TREE_CODE (d
) == VAR_DECL
18735 && !DECL_INITIAL (d
)
18736 && DECL_INITIAL (code_pattern
))
18740 bool const_init
= false;
18742 ns
= decl_namespace_context (d
);
18743 push_nested_namespace (ns
);
18744 push_nested_class (DECL_CONTEXT (d
));
18745 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
18747 tf_warning_or_error
, NULL_TREE
,
18748 /*integral_constant_expression_p=*/false);
18749 /* Make sure the initializer is still constant, in case of
18750 circular dependency (template/instantiate6.C). */
18752 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
18753 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
18754 /*asmspec_tree=*/NULL_TREE
,
18755 LOOKUP_ONLYCONVERTING
);
18756 pop_nested_class ();
18757 pop_nested_namespace (ns
);
18760 /* We restore the source position here because it's used by
18761 add_pending_template. */
18762 input_location
= saved_loc
;
18764 if (at_eof
&& !pattern_defined
18765 && DECL_EXPLICIT_INSTANTIATION (d
)
18766 && DECL_NOT_REALLY_EXTERN (d
))
18769 The definition of a non-exported function template, a
18770 non-exported member function template, or a non-exported
18771 member function or static data member of a class template
18772 shall be present in every translation unit in which it is
18773 explicitly instantiated. */
18774 permerror (input_location
, "explicit instantiation of %qD "
18775 "but no definition available", d
);
18777 /* If we're in unevaluated context, we just wanted to get the
18778 constant value; this isn't an odr use, so don't queue
18779 a full instantiation. */
18780 if (cp_unevaluated_operand
!= 0)
18782 /* ??? Historically, we have instantiated inline functions, even
18783 when marked as "extern template". */
18784 if (!(external_p
&& TREE_CODE (d
) == VAR_DECL
))
18785 add_pending_template (d
);
18788 /* Tell the repository that D is available in this translation unit
18789 -- and see if it is supposed to be instantiated here. */
18790 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
18792 /* In a PCH file, despite the fact that the repository hasn't
18793 requested instantiation in the PCH it is still possible that
18794 an instantiation will be required in a file that includes the
18797 add_pending_template (d
);
18798 /* Instantiate inline functions so that the inliner can do its
18799 job, even though we'll not be emitting a copy of this
18801 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
18805 fn_context
= decl_function_context (d
);
18806 nested
= (current_function_decl
!= NULL_TREE
);
18808 push_to_top_level ();
18810 push_function_context ();
18812 /* Mark D as instantiated so that recursive calls to
18813 instantiate_decl do not try to instantiate it again. */
18814 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
18816 /* Regenerate the declaration in case the template has been modified
18817 by a subsequent redeclaration. */
18818 regenerate_decl_from_template (d
, td
);
18820 /* We already set the file and line above. Reset them now in case
18821 they changed as a result of calling regenerate_decl_from_template. */
18822 input_location
= DECL_SOURCE_LOCATION (d
);
18824 if (TREE_CODE (d
) == VAR_DECL
)
18827 bool const_init
= false;
18829 /* Clear out DECL_RTL; whatever was there before may not be right
18830 since we've reset the type of the declaration. */
18831 SET_DECL_RTL (d
, NULL
);
18832 DECL_IN_AGGR_P (d
) = 0;
18834 /* The initializer is placed in DECL_INITIAL by
18835 regenerate_decl_from_template so we don't need to
18836 push/pop_access_scope again here. Pull it out so that
18837 cp_finish_decl can process it. */
18838 init
= DECL_INITIAL (d
);
18839 DECL_INITIAL (d
) = NULL_TREE
;
18840 DECL_INITIALIZED_P (d
) = 0;
18842 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
18843 initializer. That function will defer actual emission until
18844 we have a chance to determine linkage. */
18845 DECL_EXTERNAL (d
) = 0;
18847 /* Enter the scope of D so that access-checking works correctly. */
18848 push_nested_class (DECL_CONTEXT (d
));
18849 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
18850 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
18851 pop_nested_class ();
18853 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
18854 synthesize_method (d
);
18855 else if (TREE_CODE (d
) == FUNCTION_DECL
)
18857 struct pointer_map_t
*saved_local_specializations
;
18862 /* Save away the current list, in case we are instantiating one
18863 template from within the body of another. */
18864 saved_local_specializations
= local_specializations
;
18866 /* Set up the list of local specializations. */
18867 local_specializations
= pointer_map_create ();
18869 /* Set up context. */
18870 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
18872 /* Some typedefs referenced from within the template code need to be
18873 access checked at template instantiation time, i.e now. These
18874 types were added to the template at parsing time. Let's get those
18875 and perform the access checks then. */
18876 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl
),
18879 /* Create substitution entries for the parameters. */
18880 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
18881 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
18882 spec_parm
= DECL_ARGUMENTS (d
);
18883 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
18885 register_local_specialization (spec_parm
, tmpl_parm
);
18886 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
18887 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
18889 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
18891 if (!FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
18893 register_local_specialization (spec_parm
, tmpl_parm
);
18894 spec_parm
= DECL_CHAIN (spec_parm
);
18898 /* Register the (value) argument pack as a specialization of
18899 TMPL_PARM, then move on. */
18900 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
18901 register_local_specialization (argpack
, tmpl_parm
);
18904 gcc_assert (!spec_parm
);
18906 /* Substitute into the body of the function. */
18907 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
18908 tf_warning_or_error
, tmpl
,
18909 /*integral_constant_expression_p=*/false);
18911 /* Set the current input_location to the end of the function
18912 so that finish_function knows where we are. */
18913 input_location
= DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
18915 /* We don't need the local specializations any more. */
18916 pointer_map_destroy (local_specializations
);
18917 local_specializations
= saved_local_specializations
;
18919 /* Finish the function. */
18920 d
= finish_function (0);
18921 expand_or_defer_fn (d
);
18924 /* We're not deferring instantiation any more. */
18925 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
18928 pop_from_top_level ();
18930 pop_function_context ();
18933 input_location
= saved_loc
;
18934 pop_deferring_access_checks ();
18935 pop_tinst_level ();
18937 timevar_pop (TV_TEMPLATE_INST
);
18942 /* Run through the list of templates that we wish we could
18943 instantiate, and instantiate any we can. RETRIES is the
18944 number of times we retry pending template instantiation. */
18947 instantiate_pending_templates (int retries
)
18950 location_t saved_loc
= input_location
;
18952 /* Instantiating templates may trigger vtable generation. This in turn
18953 may require further template instantiations. We place a limit here
18954 to avoid infinite loop. */
18955 if (pending_templates
&& retries
>= max_tinst_depth
)
18957 tree decl
= pending_templates
->tinst
->decl
;
18959 error ("template instantiation depth exceeds maximum of %d"
18960 " instantiating %q+D, possibly from virtual table generation"
18961 " (use -ftemplate-depth= to increase the maximum)",
18962 max_tinst_depth
, decl
);
18963 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18964 /* Pretend that we defined it. */
18965 DECL_INITIAL (decl
) = error_mark_node
;
18971 struct pending_template
**t
= &pending_templates
;
18972 struct pending_template
*last
= NULL
;
18976 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
18977 bool complete
= false;
18979 if (TYPE_P (instantiation
))
18983 if (!COMPLETE_TYPE_P (instantiation
))
18985 instantiate_class_template (instantiation
);
18986 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
18987 for (fn
= TYPE_METHODS (instantiation
);
18989 fn
= TREE_CHAIN (fn
))
18990 if (! DECL_ARTIFICIAL (fn
))
18991 instantiate_decl (fn
,
18993 /*expl_inst_class_mem_p=*/false);
18994 if (COMPLETE_TYPE_P (instantiation
))
18998 complete
= COMPLETE_TYPE_P (instantiation
);
19002 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
19003 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
19006 = instantiate_decl (instantiation
,
19008 /*expl_inst_class_mem_p=*/false);
19009 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
19013 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
19014 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
19018 /* If INSTANTIATION has been instantiated, then we don't
19019 need to consider it again in the future. */
19027 current_tinst_level
= NULL
;
19029 last_pending_template
= last
;
19031 while (reconsider
);
19033 input_location
= saved_loc
;
19036 /* Substitute ARGVEC into T, which is a list of initializers for
19037 either base class or a non-static data member. The TREE_PURPOSEs
19038 are DECLs, and the TREE_VALUEs are the initializer values. Used by
19039 instantiate_decl. */
19042 tsubst_initializer_list (tree t
, tree argvec
)
19044 tree inits
= NULL_TREE
;
19046 for (; t
; t
= TREE_CHAIN (t
))
19050 tree expanded_bases
= NULL_TREE
;
19051 tree expanded_arguments
= NULL_TREE
;
19054 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
19059 /* Expand the base class expansion type into separate base
19061 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
19062 tf_warning_or_error
,
19064 if (expanded_bases
== error_mark_node
)
19067 /* We'll be building separate TREE_LISTs of arguments for
19069 len
= TREE_VEC_LENGTH (expanded_bases
);
19070 expanded_arguments
= make_tree_vec (len
);
19071 for (i
= 0; i
< len
; i
++)
19072 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
19074 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
19075 expand each argument in the TREE_VALUE of t. */
19076 expr
= make_node (EXPR_PACK_EXPANSION
);
19077 PACK_EXPANSION_LOCAL_P (expr
) = true;
19078 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
19079 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
19081 if (TREE_VALUE (t
) == void_type_node
)
19082 /* VOID_TYPE_NODE is used to indicate
19083 value-initialization. */
19085 for (i
= 0; i
< len
; i
++)
19086 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
19090 /* Substitute parameter packs into each argument in the
19092 in_base_initializer
= 1;
19093 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
19095 tree expanded_exprs
;
19097 /* Expand the argument. */
19098 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
19100 = tsubst_pack_expansion (expr
, argvec
,
19101 tf_warning_or_error
,
19103 if (expanded_exprs
== error_mark_node
)
19106 /* Prepend each of the expanded expressions to the
19107 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
19108 for (i
= 0; i
< len
; i
++)
19110 TREE_VEC_ELT (expanded_arguments
, i
) =
19111 tree_cons (NULL_TREE
,
19112 TREE_VEC_ELT (expanded_exprs
, i
),
19113 TREE_VEC_ELT (expanded_arguments
, i
));
19116 in_base_initializer
= 0;
19118 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
19119 since we built them backwards. */
19120 for (i
= 0; i
< len
; i
++)
19122 TREE_VEC_ELT (expanded_arguments
, i
) =
19123 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
19128 for (i
= 0; i
< len
; ++i
)
19130 if (expanded_bases
)
19132 decl
= TREE_VEC_ELT (expanded_bases
, i
);
19133 decl
= expand_member_init (decl
);
19134 init
= TREE_VEC_ELT (expanded_arguments
, i
);
19139 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
19140 tf_warning_or_error
, NULL_TREE
);
19142 decl
= expand_member_init (decl
);
19143 if (decl
&& !DECL_P (decl
))
19144 in_base_initializer
= 1;
19146 init
= TREE_VALUE (t
);
19148 if (init
!= void_type_node
)
19149 init
= tsubst_expr (init
, argvec
,
19150 tf_warning_or_error
, NULL_TREE
,
19151 /*integral_constant_expression_p=*/false);
19152 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
19153 /* If we had an initializer but it instantiated to nothing,
19154 value-initialize the object. This will only occur when
19155 the initializer was a pack expansion where the parameter
19156 packs used in that expansion were of length zero. */
19157 init
= void_type_node
;
19158 in_base_initializer
= 0;
19163 init
= build_tree_list (decl
, init
);
19164 TREE_CHAIN (init
) = inits
;
19172 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19175 set_current_access_from_decl (tree decl
)
19177 if (TREE_PRIVATE (decl
))
19178 current_access_specifier
= access_private_node
;
19179 else if (TREE_PROTECTED (decl
))
19180 current_access_specifier
= access_protected_node
;
19182 current_access_specifier
= access_public_node
;
19185 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19186 is the instantiation (which should have been created with
19187 start_enum) and ARGS are the template arguments to use. */
19190 tsubst_enum (tree tag
, tree newtag
, tree args
)
19194 if (SCOPED_ENUM_P (newtag
))
19195 begin_scope (sk_scoped_enum
, newtag
);
19197 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
19202 decl
= TREE_VALUE (e
);
19203 /* Note that in a template enum, the TREE_VALUE is the
19204 CONST_DECL, not the corresponding INTEGER_CST. */
19205 value
= tsubst_expr (DECL_INITIAL (decl
),
19206 args
, tf_warning_or_error
, NULL_TREE
,
19207 /*integral_constant_expression_p=*/true);
19209 /* Give this enumeration constant the correct access. */
19210 set_current_access_from_decl (decl
);
19212 /* Actually build the enumerator itself. */
19214 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
19217 if (SCOPED_ENUM_P (newtag
))
19220 finish_enum_value_list (newtag
);
19221 finish_enum (newtag
);
19223 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
19224 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
19227 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19228 its type -- but without substituting the innermost set of template
19229 arguments. So, innermost set of template parameters will appear in
19233 get_mostly_instantiated_function_type (tree decl
)
19241 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
19242 targs
= DECL_TI_ARGS (decl
);
19243 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
19244 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
19246 /* There should be as many levels of arguments as there are levels
19248 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
19250 fn_type
= TREE_TYPE (tmpl
);
19252 if (parm_depth
== 1)
19253 /* No substitution is necessary. */
19260 /* Replace the innermost level of the TARGS with NULL_TREEs to
19261 let tsubst know not to substitute for those parameters. */
19262 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
19263 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
19264 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
19265 TMPL_ARGS_LEVEL (targs
, i
));
19266 SET_TMPL_ARGS_LEVEL (partial_args
,
19267 TMPL_ARGS_DEPTH (targs
),
19268 make_tree_vec (DECL_NTPARMS (tmpl
)));
19270 /* Make sure that we can see identifiers, and compute access
19272 push_access_scope (decl
);
19274 ++processing_template_decl
;
19275 /* Now, do the (partial) substitution to figure out the
19276 appropriate function type. */
19277 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
19278 --processing_template_decl
;
19280 /* Substitute into the template parameters to obtain the real
19281 innermost set of parameters. This step is important if the
19282 innermost set of template parameters contains value
19283 parameters whose types depend on outer template parameters. */
19284 TREE_VEC_LENGTH (partial_args
)--;
19285 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
19287 pop_access_scope (decl
);
19293 /* Return truthvalue if we're processing a template different from
19294 the last one involved in diagnostics. */
19296 problematic_instantiation_changed (void)
19298 return current_tinst_level
!= last_error_tinst_level
;
19301 /* Remember current template involved in diagnostics. */
19303 record_last_problematic_instantiation (void)
19305 last_error_tinst_level
= current_tinst_level
;
19308 struct tinst_level
*
19309 current_instantiation (void)
19311 return current_tinst_level
;
19314 /* [temp.param] Check that template non-type parm TYPE is of an allowable
19315 type. Return zero for ok, nonzero for disallowed. Issue error and
19316 warning messages under control of COMPLAIN. */
19319 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
19321 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
19323 else if (POINTER_TYPE_P (type
))
19325 else if (TYPE_PTRMEM_P (type
))
19327 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
19329 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
19331 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
19333 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
19336 if (complain
& tf_error
)
19338 if (type
== error_mark_node
)
19339 inform (input_location
, "invalid template non-type parameter");
19341 error ("%q#T is not a valid type for a template non-type parameter",
19347 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
19348 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
19351 dependent_type_p_r (tree type
)
19357 A type is dependent if it is:
19359 -- a template parameter. Template template parameters are types
19360 for us (since TYPE_P holds true for them) so we handle
19362 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
19363 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
19365 /* -- a qualified-id with a nested-name-specifier which contains a
19366 class-name that names a dependent type or whose unqualified-id
19367 names a dependent type. */
19368 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19370 /* -- a cv-qualified type where the cv-unqualified type is
19372 type
= TYPE_MAIN_VARIANT (type
);
19373 /* -- a compound type constructed from any dependent type. */
19374 if (TYPE_PTRMEM_P (type
))
19375 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
19376 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
19378 else if (TREE_CODE (type
) == POINTER_TYPE
19379 || TREE_CODE (type
) == REFERENCE_TYPE
)
19380 return dependent_type_p (TREE_TYPE (type
));
19381 else if (TREE_CODE (type
) == FUNCTION_TYPE
19382 || TREE_CODE (type
) == METHOD_TYPE
)
19386 if (dependent_type_p (TREE_TYPE (type
)))
19388 for (arg_type
= TYPE_ARG_TYPES (type
);
19390 arg_type
= TREE_CHAIN (arg_type
))
19391 if (dependent_type_p (TREE_VALUE (arg_type
)))
19395 /* -- an array type constructed from any dependent type or whose
19396 size is specified by a constant expression that is
19399 We checked for type- and value-dependence of the bounds in
19400 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
19401 if (TREE_CODE (type
) == ARRAY_TYPE
)
19403 if (TYPE_DOMAIN (type
)
19404 && dependent_type_p (TYPE_DOMAIN (type
)))
19406 return dependent_type_p (TREE_TYPE (type
));
19409 /* -- a template-id in which either the template name is a template
19411 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19413 /* ... or any of the template arguments is a dependent type or
19414 an expression that is type-dependent or value-dependent. */
19415 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
19416 && (any_dependent_template_arguments_p
19417 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
19420 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
19421 dependent; if the argument of the `typeof' expression is not
19422 type-dependent, then it should already been have resolved. */
19423 if (TREE_CODE (type
) == TYPEOF_TYPE
19424 || TREE_CODE (type
) == DECLTYPE_TYPE
19425 || TREE_CODE (type
) == UNDERLYING_TYPE
)
19428 /* A template argument pack is dependent if any of its packed
19430 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
19432 tree args
= ARGUMENT_PACK_ARGS (type
);
19433 int i
, len
= TREE_VEC_LENGTH (args
);
19434 for (i
= 0; i
< len
; ++i
)
19435 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
19439 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
19440 be template parameters. */
19441 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
19444 /* The standard does not specifically mention types that are local
19445 to template functions or local classes, but they should be
19446 considered dependent too. For example:
19448 template <int I> void f() {
19453 The size of `E' cannot be known until the value of `I' has been
19454 determined. Therefore, `E' must be considered dependent. */
19455 scope
= TYPE_CONTEXT (type
);
19456 if (scope
&& TYPE_P (scope
))
19457 return dependent_type_p (scope
);
19458 /* Don't use type_dependent_expression_p here, as it can lead
19459 to infinite recursion trying to determine whether a lambda
19460 nested in a lambda is dependent (c++/47687). */
19461 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
19462 && DECL_LANG_SPECIFIC (scope
)
19463 && DECL_TEMPLATE_INFO (scope
)
19464 && (any_dependent_template_arguments_p
19465 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
19468 /* Other types are non-dependent. */
19472 /* Returns TRUE if TYPE is dependent, in the sense of
19473 [temp.dep.type]. Note that a NULL type is considered dependent. */
19476 dependent_type_p (tree type
)
19478 /* If there are no template parameters in scope, then there can't be
19479 any dependent types. */
19480 if (!processing_template_decl
)
19482 /* If we are not processing a template, then nobody should be
19483 providing us with a dependent type. */
19485 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
19489 /* If the type is NULL, we have not computed a type for the entity
19490 in question; in that case, the type is dependent. */
19494 /* Erroneous types can be considered non-dependent. */
19495 if (type
== error_mark_node
)
19498 /* If we have not already computed the appropriate value for TYPE,
19500 if (!TYPE_DEPENDENT_P_VALID (type
))
19502 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
19503 TYPE_DEPENDENT_P_VALID (type
) = 1;
19506 return TYPE_DEPENDENT_P (type
);
19509 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
19510 lookup. In other words, a dependent type that is not the current
19514 dependent_scope_p (tree scope
)
19516 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
19517 && !currently_open_class (scope
));
19520 /* T is a SCOPE_REF; return whether we need to consider it
19521 instantiation-dependent so that we can check access at instantiation
19522 time even though we know which member it resolves to. */
19525 instantiation_dependent_scope_ref_p (tree t
)
19527 if (DECL_P (TREE_OPERAND (t
, 1))
19528 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
19529 && accessible_in_template_p (TREE_OPERAND (t
, 0),
19530 TREE_OPERAND (t
, 1)))
19536 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
19537 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
19540 /* Note that this predicate is not appropriate for general expressions;
19541 only constant expressions (that satisfy potential_constant_expression)
19542 can be tested for value dependence. */
19545 value_dependent_expression_p (tree expression
)
19547 if (!processing_template_decl
)
19550 /* A name declared with a dependent type. */
19551 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
19554 switch (TREE_CODE (expression
))
19556 case IDENTIFIER_NODE
:
19557 /* A name that has not been looked up -- must be dependent. */
19560 case TEMPLATE_PARM_INDEX
:
19561 /* A non-type template parm. */
19565 /* A non-type template parm. */
19566 if (DECL_TEMPLATE_PARM_P (expression
))
19568 return value_dependent_expression_p (DECL_INITIAL (expression
));
19571 /* A constant with literal type and is initialized
19572 with an expression that is value-dependent.
19574 Note that a non-dependent parenthesized initializer will have
19575 already been replaced with its constant value, so if we see
19576 a TREE_LIST it must be dependent. */
19577 if (DECL_INITIAL (expression
)
19578 && decl_constant_var_p (expression
)
19579 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
19580 || value_dependent_expression_p (DECL_INITIAL (expression
))))
19584 case DYNAMIC_CAST_EXPR
:
19585 case STATIC_CAST_EXPR
:
19586 case CONST_CAST_EXPR
:
19587 case REINTERPRET_CAST_EXPR
:
19589 /* These expressions are value-dependent if the type to which
19590 the cast occurs is dependent or the expression being casted
19591 is value-dependent. */
19593 tree type
= TREE_TYPE (expression
);
19595 if (dependent_type_p (type
))
19598 /* A functional cast has a list of operands. */
19599 expression
= TREE_OPERAND (expression
, 0);
19602 /* If there are no operands, it must be an expression such
19603 as "int()". This should not happen for aggregate types
19604 because it would form non-constant expressions. */
19605 gcc_assert (cxx_dialect
>= cxx0x
19606 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
19611 if (TREE_CODE (expression
) == TREE_LIST
)
19612 return any_value_dependent_elements_p (expression
);
19614 return value_dependent_expression_p (expression
);
19618 if (SIZEOF_EXPR_TYPE_P (expression
))
19619 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
19623 /* A `sizeof' expression is value-dependent if the operand is
19624 type-dependent or is a pack expansion. */
19625 expression
= TREE_OPERAND (expression
, 0);
19626 if (PACK_EXPANSION_P (expression
))
19628 else if (TYPE_P (expression
))
19629 return dependent_type_p (expression
);
19630 return instantiation_dependent_expression_p (expression
);
19632 case AT_ENCODE_EXPR
:
19633 /* An 'encode' expression is value-dependent if the operand is
19635 expression
= TREE_OPERAND (expression
, 0);
19636 return dependent_type_p (expression
);
19638 case NOEXCEPT_EXPR
:
19639 expression
= TREE_OPERAND (expression
, 0);
19640 return instantiation_dependent_expression_p (expression
);
19643 /* All instantiation-dependent expressions should also be considered
19644 value-dependent. */
19645 return instantiation_dependent_scope_ref_p (expression
);
19647 case COMPONENT_REF
:
19648 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
19649 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
19651 case NONTYPE_ARGUMENT_PACK
:
19652 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
19653 is value-dependent. */
19655 tree values
= ARGUMENT_PACK_ARGS (expression
);
19656 int i
, len
= TREE_VEC_LENGTH (values
);
19658 for (i
= 0; i
< len
; ++i
)
19659 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
19667 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
19668 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
19669 || (type2
? dependent_type_p (type2
) : false));
19673 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19674 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
19677 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19678 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
19682 tree op
= TREE_OPERAND (expression
, 0);
19683 return (value_dependent_expression_p (op
)
19684 || has_value_dependent_address (op
));
19689 tree fn
= get_callee_fndecl (expression
);
19691 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
19693 nargs
= call_expr_nargs (expression
);
19694 for (i
= 0; i
< nargs
; ++i
)
19696 tree op
= CALL_EXPR_ARG (expression
, i
);
19697 /* In a call to a constexpr member function, look through the
19698 implicit ADDR_EXPR on the object argument so that it doesn't
19699 cause the call to be considered value-dependent. We also
19700 look through it in potential_constant_expression. */
19701 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
19702 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
19703 && TREE_CODE (op
) == ADDR_EXPR
)
19704 op
= TREE_OPERAND (op
, 0);
19705 if (value_dependent_expression_p (op
))
19711 case TEMPLATE_ID_EXPR
:
19712 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
19714 return type_dependent_expression_p (expression
);
19720 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
19721 if (value_dependent_expression_p (val
))
19727 /* Treat a GNU statement expression as dependent to avoid crashing
19728 under fold_non_dependent_expr; it can't be constant. */
19732 /* A constant expression is value-dependent if any subexpression is
19733 value-dependent. */
19734 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
19736 case tcc_reference
:
19738 case tcc_comparison
:
19740 case tcc_expression
:
19743 int i
, len
= cp_tree_operand_length (expression
);
19745 for (i
= 0; i
< len
; i
++)
19747 tree t
= TREE_OPERAND (expression
, i
);
19749 /* In some cases, some of the operands may be missing.l
19750 (For example, in the case of PREDECREMENT_EXPR, the
19751 amount to increment by may be missing.) That doesn't
19752 make the expression dependent. */
19753 if (t
&& value_dependent_expression_p (t
))
19764 /* The expression is not value-dependent. */
19768 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
19769 [temp.dep.expr]. Note that an expression with no type is
19770 considered dependent. Other parts of the compiler arrange for an
19771 expression with type-dependent subexpressions to have no type, so
19772 this function doesn't have to be fully recursive. */
19775 type_dependent_expression_p (tree expression
)
19777 if (!processing_template_decl
)
19780 if (expression
== error_mark_node
)
19783 /* An unresolved name is always dependent. */
19784 if (TREE_CODE (expression
) == IDENTIFIER_NODE
19785 || TREE_CODE (expression
) == USING_DECL
)
19788 /* Some expression forms are never type-dependent. */
19789 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
19790 || TREE_CODE (expression
) == SIZEOF_EXPR
19791 || TREE_CODE (expression
) == ALIGNOF_EXPR
19792 || TREE_CODE (expression
) == AT_ENCODE_EXPR
19793 || TREE_CODE (expression
) == NOEXCEPT_EXPR
19794 || TREE_CODE (expression
) == TRAIT_EXPR
19795 || TREE_CODE (expression
) == TYPEID_EXPR
19796 || TREE_CODE (expression
) == DELETE_EXPR
19797 || TREE_CODE (expression
) == VEC_DELETE_EXPR
19798 || TREE_CODE (expression
) == THROW_EXPR
)
19801 /* The types of these expressions depends only on the type to which
19802 the cast occurs. */
19803 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
19804 || TREE_CODE (expression
) == STATIC_CAST_EXPR
19805 || TREE_CODE (expression
) == CONST_CAST_EXPR
19806 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
19807 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
19808 || TREE_CODE (expression
) == CAST_EXPR
)
19809 return dependent_type_p (TREE_TYPE (expression
));
19811 /* The types of these expressions depends only on the type created
19812 by the expression. */
19813 if (TREE_CODE (expression
) == NEW_EXPR
19814 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
19816 /* For NEW_EXPR tree nodes created inside a template, either
19817 the object type itself or a TREE_LIST may appear as the
19819 tree type
= TREE_OPERAND (expression
, 1);
19820 if (TREE_CODE (type
) == TREE_LIST
)
19821 /* This is an array type. We need to check array dimensions
19823 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
19824 || value_dependent_expression_p
19825 (TREE_OPERAND (TREE_VALUE (type
), 1));
19827 return dependent_type_p (type
);
19830 if (TREE_CODE (expression
) == SCOPE_REF
)
19832 tree scope
= TREE_OPERAND (expression
, 0);
19833 tree name
= TREE_OPERAND (expression
, 1);
19835 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
19836 contains an identifier associated by name lookup with one or more
19837 declarations declared with a dependent type, or...a
19838 nested-name-specifier or qualified-id that names a member of an
19839 unknown specialization. */
19840 return (type_dependent_expression_p (name
)
19841 || dependent_scope_p (scope
));
19844 if (TREE_CODE (expression
) == FUNCTION_DECL
19845 && DECL_LANG_SPECIFIC (expression
)
19846 && DECL_TEMPLATE_INFO (expression
)
19847 && (any_dependent_template_arguments_p
19848 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
19851 if (TREE_CODE (expression
) == TEMPLATE_DECL
19852 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
19855 if (TREE_CODE (expression
) == STMT_EXPR
)
19856 expression
= stmt_expr_value_expr (expression
);
19858 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
19863 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
19865 if (type_dependent_expression_p (elt
))
19871 /* A static data member of the current instantiation with incomplete
19872 array type is type-dependent, as the definition and specializations
19873 can have different bounds. */
19874 if (TREE_CODE (expression
) == VAR_DECL
19875 && DECL_CLASS_SCOPE_P (expression
)
19876 && dependent_type_p (DECL_CONTEXT (expression
))
19877 && VAR_HAD_UNKNOWN_BOUND (expression
))
19880 if (TREE_TYPE (expression
) == unknown_type_node
)
19882 if (TREE_CODE (expression
) == ADDR_EXPR
)
19883 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
19884 if (TREE_CODE (expression
) == COMPONENT_REF
19885 || TREE_CODE (expression
) == OFFSET_REF
)
19887 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19889 expression
= TREE_OPERAND (expression
, 1);
19890 if (TREE_CODE (expression
) == IDENTIFIER_NODE
)
19893 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
19894 if (TREE_CODE (expression
) == SCOPE_REF
)
19897 if (BASELINK_P (expression
))
19898 expression
= BASELINK_FUNCTIONS (expression
);
19900 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
19902 if (any_dependent_template_arguments_p
19903 (TREE_OPERAND (expression
, 1)))
19905 expression
= TREE_OPERAND (expression
, 0);
19907 gcc_assert (TREE_CODE (expression
) == OVERLOAD
19908 || TREE_CODE (expression
) == FUNCTION_DECL
);
19912 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
19914 expression
= OVL_NEXT (expression
);
19919 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
19921 return (dependent_type_p (TREE_TYPE (expression
)));
19924 /* walk_tree callback function for instantiation_dependent_expression_p,
19925 below. Returns non-zero if a dependent subexpression is found. */
19928 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
19933 /* We don't have to worry about decltype currently because decltype
19934 of an instantiation-dependent expr is a dependent type. This
19935 might change depending on the resolution of DR 1172. */
19936 *walk_subtrees
= false;
19939 enum tree_code code
= TREE_CODE (*tp
);
19942 /* Don't treat an argument list as dependent just because it has no
19948 case TEMPLATE_PARM_INDEX
:
19951 /* Handle expressions with type operands. */
19955 case AT_ENCODE_EXPR
:
19957 tree op
= TREE_OPERAND (*tp
, 0);
19958 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
19959 op
= TREE_TYPE (op
);
19962 if (dependent_type_p (op
))
19966 *walk_subtrees
= false;
19974 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp
))
19975 || dependent_type_p (TRAIT_EXPR_TYPE2 (*tp
)))
19977 *walk_subtrees
= false;
19980 case COMPONENT_REF
:
19981 if (TREE_CODE (TREE_OPERAND (*tp
, 1)) == IDENTIFIER_NODE
)
19982 /* In a template, finish_class_member_access_expr creates a
19983 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
19984 type-dependent, so that we can check access control at
19985 instantiation time (PR 42277). See also Core issue 1273. */
19990 if (instantiation_dependent_scope_ref_p (*tp
))
19995 /* Treat statement-expressions as dependent. */
20003 if (type_dependent_expression_p (*tp
))
20009 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
20010 sense defined by the ABI:
20012 "An expression is instantiation-dependent if it is type-dependent
20013 or value-dependent, or it has a subexpression that is type-dependent
20014 or value-dependent." */
20017 instantiation_dependent_expression_p (tree expression
)
20021 if (!processing_template_decl
)
20024 if (expression
== error_mark_node
)
20027 result
= cp_walk_tree_without_duplicates (&expression
,
20028 instantiation_dependent_r
, NULL
);
20029 return result
!= NULL_TREE
;
20032 /* Like type_dependent_expression_p, but it also works while not processing
20033 a template definition, i.e. during substitution or mangling. */
20036 type_dependent_expression_p_push (tree expr
)
20039 ++processing_template_decl
;
20040 b
= type_dependent_expression_p (expr
);
20041 --processing_template_decl
;
20045 /* Returns TRUE if ARGS contains a type-dependent expression. */
20048 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
20053 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
20055 if (type_dependent_expression_p (arg
))
20061 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20062 expressions) contains any type-dependent expressions. */
20065 any_type_dependent_elements_p (const_tree list
)
20067 for (; list
; list
= TREE_CHAIN (list
))
20068 if (value_dependent_expression_p (TREE_VALUE (list
)))
20074 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20075 expressions) contains any value-dependent expressions. */
20078 any_value_dependent_elements_p (const_tree list
)
20080 for (; list
; list
= TREE_CHAIN (list
))
20081 if (value_dependent_expression_p (TREE_VALUE (list
)))
20087 /* Returns TRUE if the ARG (a template argument) is dependent. */
20090 dependent_template_arg_p (tree arg
)
20092 if (!processing_template_decl
)
20095 /* Assume a template argument that was wrongly written by the user
20096 is dependent. This is consistent with what
20097 any_dependent_template_arguments_p [that calls this function]
20099 if (!arg
|| arg
== error_mark_node
)
20102 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
20103 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
20105 if (TREE_CODE (arg
) == TEMPLATE_DECL
20106 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
20107 return dependent_template_p (arg
);
20108 else if (ARGUMENT_PACK_P (arg
))
20110 tree args
= ARGUMENT_PACK_ARGS (arg
);
20111 int i
, len
= TREE_VEC_LENGTH (args
);
20112 for (i
= 0; i
< len
; ++i
)
20114 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
20120 else if (TYPE_P (arg
))
20121 return dependent_type_p (arg
);
20123 return (type_dependent_expression_p (arg
)
20124 || value_dependent_expression_p (arg
));
20127 /* Returns true if ARGS (a collection of template arguments) contains
20128 any types that require structural equality testing. */
20131 any_template_arguments_need_structural_equality_p (tree args
)
20138 if (args
== error_mark_node
)
20141 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
20143 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
20144 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
20146 tree arg
= TREE_VEC_ELT (level
, j
);
20147 tree packed_args
= NULL_TREE
;
20150 if (ARGUMENT_PACK_P (arg
))
20152 /* Look inside the argument pack. */
20153 packed_args
= ARGUMENT_PACK_ARGS (arg
);
20154 len
= TREE_VEC_LENGTH (packed_args
);
20157 for (k
= 0; k
< len
; ++k
)
20160 arg
= TREE_VEC_ELT (packed_args
, k
);
20162 if (error_operand_p (arg
))
20164 else if (TREE_CODE (arg
) == TEMPLATE_DECL
20165 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
20167 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
20169 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
20170 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
20179 /* Returns true if ARGS (a collection of template arguments) contains
20180 any dependent arguments. */
20183 any_dependent_template_arguments_p (const_tree args
)
20190 if (args
== error_mark_node
)
20193 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
20195 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
20196 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
20197 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
20204 /* Returns TRUE if the template TMPL is dependent. */
20207 dependent_template_p (tree tmpl
)
20209 if (TREE_CODE (tmpl
) == OVERLOAD
)
20213 if (dependent_template_p (OVL_CURRENT (tmpl
)))
20215 tmpl
= OVL_NEXT (tmpl
);
20220 /* Template template parameters are dependent. */
20221 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
20222 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
20224 /* So are names that have not been looked up. */
20225 if (TREE_CODE (tmpl
) == SCOPE_REF
20226 || TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
20228 /* So are member templates of dependent classes. */
20229 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
20230 return dependent_type_p (DECL_CONTEXT (tmpl
));
20234 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
20237 dependent_template_id_p (tree tmpl
, tree args
)
20239 return (dependent_template_p (tmpl
)
20240 || any_dependent_template_arguments_p (args
));
20243 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
20247 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
20251 if (!processing_template_decl
)
20254 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
20256 tree decl
= TREE_VEC_ELT (declv
, i
);
20257 tree init
= TREE_VEC_ELT (initv
, i
);
20258 tree cond
= TREE_VEC_ELT (condv
, i
);
20259 tree incr
= TREE_VEC_ELT (incrv
, i
);
20261 if (type_dependent_expression_p (decl
))
20264 if (init
&& type_dependent_expression_p (init
))
20267 if (type_dependent_expression_p (cond
))
20270 if (COMPARISON_CLASS_P (cond
)
20271 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
20272 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
20275 if (TREE_CODE (incr
) == MODOP_EXPR
)
20277 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
20278 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
20281 else if (type_dependent_expression_p (incr
))
20283 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
20285 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
20287 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
20289 tree t
= TREE_OPERAND (incr
, 1);
20290 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
20291 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
20300 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
20301 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
20302 no such TYPE can be found. Note that this function peers inside
20303 uninstantiated templates and therefore should be used only in
20304 extremely limited situations. ONLY_CURRENT_P restricts this
20305 peering to the currently open classes hierarchy (which is required
20306 when comparing types). */
20309 resolve_typename_type (tree type
, bool only_current_p
)
20318 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
20320 scope
= TYPE_CONTEXT (type
);
20321 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
20322 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
20323 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
20324 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
20325 identifier of the TYPENAME_TYPE anymore.
20326 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
20327 TYPENAME_TYPE instead, we avoid messing up with a possible
20328 typedef variant case. */
20329 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
20331 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
20332 it first before we can figure out what NAME refers to. */
20333 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
20335 if (TYPENAME_IS_RESOLVING_P (scope
))
20336 /* Given a class template A with a dependent base with nested type C,
20337 typedef typename A::C::C C will land us here, as trying to resolve
20338 the initial A::C leads to the local C typedef, which leads back to
20339 A::C::C. So we break the recursion now. */
20342 scope
= resolve_typename_type (scope
, only_current_p
);
20344 /* If we don't know what SCOPE refers to, then we cannot resolve the
20346 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
20348 /* If the SCOPE is a template type parameter, we have no way of
20349 resolving the name. */
20350 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
20352 /* If the SCOPE is not the current instantiation, there's no reason
20353 to look inside it. */
20354 if (only_current_p
&& !currently_open_class (scope
))
20356 /* If this is a typedef, we don't want to look inside (c++/11987). */
20357 if (typedef_variant_p (type
))
20359 /* If SCOPE isn't the template itself, it will not have a valid
20360 TYPE_FIELDS list. */
20361 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
20362 /* scope is either the template itself or a compatible instantiation
20363 like X<T>, so look up the name in the original template. */
20364 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
20366 /* scope is a partial instantiation, so we can't do the lookup or we
20367 will lose the template arguments. */
20369 /* Enter the SCOPE so that name lookup will be resolved as if we
20370 were in the class definition. In particular, SCOPE will no
20371 longer be considered a dependent type. */
20372 pushed_scope
= push_scope (scope
);
20373 /* Look up the declaration. */
20374 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
20375 tf_warning_or_error
);
20377 result
= NULL_TREE
;
20379 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
20380 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
20383 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == IDENTIFIER_NODE
20384 && TREE_CODE (decl
) == TYPE_DECL
)
20386 result
= TREE_TYPE (decl
);
20387 if (result
== error_mark_node
)
20388 result
= NULL_TREE
;
20390 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
20391 && DECL_CLASS_TEMPLATE_P (decl
))
20395 /* Obtain the template and the arguments. */
20396 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
20397 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
20398 /* Instantiate the template. */
20399 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
20400 /*entering_scope=*/0,
20401 tf_error
| tf_user
);
20402 if (result
== error_mark_node
)
20403 result
= NULL_TREE
;
20406 /* Leave the SCOPE. */
20408 pop_scope (pushed_scope
);
20410 /* If we failed to resolve it, return the original typename. */
20414 /* If lookup found a typename type, resolve that too. */
20415 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
20417 /* Ill-formed programs can cause infinite recursion here, so we
20418 must catch that. */
20419 TYPENAME_IS_RESOLVING_P (type
) = 1;
20420 result
= resolve_typename_type (result
, only_current_p
);
20421 TYPENAME_IS_RESOLVING_P (type
) = 0;
20424 /* Qualify the resulting type. */
20425 quals
= cp_type_quals (type
);
20427 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
20432 /* EXPR is an expression which is not type-dependent. Return a proxy
20433 for EXPR that can be used to compute the types of larger
20434 expressions containing EXPR. */
20437 build_non_dependent_expr (tree expr
)
20441 #ifdef ENABLE_CHECKING
20442 /* Try to get a constant value for all non-dependent expressions in
20443 order to expose bugs in *_dependent_expression_p and constexpr. */
20444 if (cxx_dialect
>= cxx0x
20445 && !instantiation_dependent_expression_p (expr
))
20446 maybe_constant_value (fold_non_dependent_expr_sfinae (expr
, tf_none
));
20449 /* Preserve OVERLOADs; the functions must be available to resolve
20452 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
20453 inner_expr
= stmt_expr_value_expr (inner_expr
);
20454 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
20455 inner_expr
= TREE_OPERAND (inner_expr
, 0);
20456 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
20457 inner_expr
= TREE_OPERAND (inner_expr
, 1);
20458 if (is_overloaded_fn (inner_expr
)
20459 || TREE_CODE (inner_expr
) == OFFSET_REF
)
20461 /* There is no need to return a proxy for a variable. */
20462 if (TREE_CODE (expr
) == VAR_DECL
)
20464 /* Preserve string constants; conversions from string constants to
20465 "char *" are allowed, even though normally a "const char *"
20466 cannot be used to initialize a "char *". */
20467 if (TREE_CODE (expr
) == STRING_CST
)
20469 /* Preserve arithmetic constants, as an optimization -- there is no
20470 reason to create a new node. */
20471 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
20473 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
20474 There is at least one place where we want to know that a
20475 particular expression is a throw-expression: when checking a ?:
20476 expression, there are special rules if the second or third
20477 argument is a throw-expression. */
20478 if (TREE_CODE (expr
) == THROW_EXPR
)
20481 /* Don't wrap an initializer list, we need to be able to look inside. */
20482 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
20485 /* Don't wrap a dummy object, we need to be able to test for it. */
20486 if (is_dummy_object (expr
))
20489 if (TREE_CODE (expr
) == COND_EXPR
)
20490 return build3 (COND_EXPR
,
20492 TREE_OPERAND (expr
, 0),
20493 (TREE_OPERAND (expr
, 1)
20494 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
20495 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
20496 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
20497 if (TREE_CODE (expr
) == COMPOUND_EXPR
20498 && !COMPOUND_EXPR_OVERLOADED (expr
))
20499 return build2 (COMPOUND_EXPR
,
20501 TREE_OPERAND (expr
, 0),
20502 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
20504 /* If the type is unknown, it can't really be non-dependent */
20505 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
20507 /* Otherwise, build a NON_DEPENDENT_EXPR. */
20508 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
20511 /* ARGS is a vector of expressions as arguments to a function call.
20512 Replace the arguments with equivalent non-dependent expressions.
20513 This modifies ARGS in place. */
20516 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
20521 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
20523 tree newarg
= build_non_dependent_expr (arg
);
20525 (*args
)[ix
] = newarg
;
20529 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
20530 with a level one deeper than the actual template parms. */
20535 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
20536 TYPE_NAME (au
) = build_decl (BUILTINS_LOCATION
,
20537 TYPE_DECL
, get_identifier ("auto"), au
);
20538 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
20539 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
20540 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
20541 TYPE_NAME (au
), NULL_TREE
);
20542 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
20543 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
20544 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
20549 /* Given type ARG, return std::initializer_list<ARG>. */
20554 tree std_init_list
= namespace_binding
20555 (get_identifier ("initializer_list"), std_node
);
20557 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
20559 error ("deducing from brace-enclosed initializer list requires "
20560 "#include <initializer_list>");
20561 return error_mark_node
;
20563 argvec
= make_tree_vec (1);
20564 TREE_VEC_ELT (argvec
, 0) = arg
;
20565 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
20566 NULL_TREE
, 0, tf_warning_or_error
);
20569 /* Replace auto in TYPE with std::initializer_list<auto>. */
20572 listify_autos (tree type
, tree auto_node
)
20574 tree init_auto
= listify (auto_node
);
20575 tree argvec
= make_tree_vec (1);
20576 TREE_VEC_ELT (argvec
, 0) = init_auto
;
20577 if (processing_template_decl
)
20578 argvec
= add_to_template_args (current_template_args (), argvec
);
20579 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
20582 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
20583 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
20586 do_auto_deduction (tree type
, tree init
, tree auto_node
)
20588 tree parms
, tparms
, targs
;
20592 if (init
== error_mark_node
)
20593 return error_mark_node
;
20595 if (type_dependent_expression_p (init
))
20596 /* Defining a subset of type-dependent expressions that we can deduce
20597 from ahead of time isn't worth the trouble. */
20600 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
20601 with either a new invented type template parameter U or, if the
20602 initializer is a braced-init-list (8.5.4), with
20603 std::initializer_list<U>. */
20604 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
20605 type
= listify_autos (type
, auto_node
);
20607 init
= resolve_nondeduced_context (init
);
20609 parms
= build_tree_list (NULL_TREE
, type
);
20611 tparms
= make_tree_vec (1);
20612 targs
= make_tree_vec (1);
20613 TREE_VEC_ELT (tparms
, 0)
20614 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
20615 val
= type_unification_real (tparms
, targs
, parms
, args
, 1, 0,
20616 DEDUCE_CALL
, LOOKUP_NORMAL
,
20617 /*explain_p=*/false);
20620 if (processing_template_decl
)
20621 /* Try again at instantiation time. */
20623 if (type
&& type
!= error_mark_node
)
20624 /* If type is error_mark_node a diagnostic must have been
20625 emitted by now. Also, having a mention to '<type error>'
20626 in the diagnostic is not really useful to the user. */
20628 if (cfun
&& auto_node
== current_function_auto_return_pattern
20629 && LAMBDA_FUNCTION_P (current_function_decl
))
20630 error ("unable to deduce lambda return type from %qE", init
);
20632 error ("unable to deduce %qT from %qE", type
, init
);
20634 return error_mark_node
;
20637 /* If the list of declarators contains more than one declarator, the type
20638 of each declared variable is determined as described above. If the
20639 type deduced for the template parameter U is not the same in each
20640 deduction, the program is ill-formed. */
20641 if (TREE_TYPE (auto_node
)
20642 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
20644 if (cfun
&& auto_node
== current_function_auto_return_pattern
20645 && LAMBDA_FUNCTION_P (current_function_decl
))
20646 error ("inconsistent types %qT and %qT deduced for "
20647 "lambda return type", TREE_TYPE (auto_node
),
20648 TREE_VEC_ELT (targs
, 0));
20650 error ("inconsistent deduction for %qT: %qT and then %qT",
20651 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
20652 return error_mark_node
;
20654 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
20656 if (processing_template_decl
)
20657 targs
= add_to_template_args (current_template_args (), targs
);
20658 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
20661 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
20665 splice_late_return_type (tree type
, tree late_return_type
)
20669 if (late_return_type
== NULL_TREE
)
20671 argvec
= make_tree_vec (1);
20672 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
20673 if (processing_template_parmlist
)
20674 /* For a late-specified return type in a template type-parameter, we
20675 need to add a dummy argument level for its parmlist. */
20676 argvec
= add_to_template_args
20677 (make_tree_vec (processing_template_parmlist
), argvec
);
20678 if (current_template_parms
)
20679 argvec
= add_to_template_args (current_template_args (), argvec
);
20680 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
20683 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
20686 is_auto (const_tree type
)
20688 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20689 && TYPE_IDENTIFIER (type
) == get_identifier ("auto"))
20695 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
20696 appear as a type-specifier for the declaration in question, we don't
20697 have to look through the whole type. */
20700 type_uses_auto (tree type
)
20702 enum tree_code code
;
20703 if (is_auto (type
))
20706 code
= TREE_CODE (type
);
20708 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
20709 || code
== OFFSET_TYPE
|| code
== FUNCTION_TYPE
20710 || code
== METHOD_TYPE
|| code
== ARRAY_TYPE
)
20711 return type_uses_auto (TREE_TYPE (type
));
20713 if (TYPE_PTRMEMFUNC_P (type
))
20714 return type_uses_auto (TREE_TYPE (TREE_TYPE
20715 (TYPE_PTRMEMFUNC_FN_TYPE (type
))));
20720 /* For a given template T, return the vector of typedefs referenced
20721 in T for which access check is needed at T instantiation time.
20722 T is either a FUNCTION_DECL or a RECORD_TYPE.
20723 Those typedefs were added to T by the function
20724 append_type_to_template_for_access_check. */
20726 vec
<qualified_typedef_usage_t
, va_gc
> *
20727 get_types_needing_access_check (tree t
)
20730 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
20732 if (!t
|| t
== error_mark_node
)
20735 if (!(ti
= get_template_info (t
)))
20738 if (CLASS_TYPE_P (t
)
20739 || TREE_CODE (t
) == FUNCTION_DECL
)
20741 if (!TI_TEMPLATE (ti
))
20744 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
20750 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
20751 tied to T. That list of typedefs will be access checked at
20752 T instantiation time.
20753 T is either a FUNCTION_DECL or a RECORD_TYPE.
20754 TYPE_DECL is a TYPE_DECL node representing a typedef.
20755 SCOPE is the scope through which TYPE_DECL is accessed.
20756 LOCATION is the location of the usage point of TYPE_DECL.
20758 This function is a subroutine of
20759 append_type_to_template_for_access_check. */
20762 append_type_to_template_for_access_check_1 (tree t
,
20765 location_t location
)
20767 qualified_typedef_usage_t typedef_usage
;
20770 if (!t
|| t
== error_mark_node
)
20773 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
20774 || CLASS_TYPE_P (t
))
20776 && TREE_CODE (type_decl
) == TYPE_DECL
20779 if (!(ti
= get_template_info (t
)))
20782 gcc_assert (TI_TEMPLATE (ti
));
20784 typedef_usage
.typedef_decl
= type_decl
;
20785 typedef_usage
.context
= scope
;
20786 typedef_usage
.locus
= location
;
20788 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
20791 /* Append TYPE_DECL to the template TEMPL.
20792 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
20793 At TEMPL instanciation time, TYPE_DECL will be checked to see
20794 if it can be accessed through SCOPE.
20795 LOCATION is the location of the usage point of TYPE_DECL.
20797 e.g. consider the following code snippet:
20804 template<class U> struct S
20806 C::myint mi; // <-- usage point of the typedef C::myint
20811 At S<char> instantiation time, we need to check the access of C::myint
20812 In other words, we need to check the access of the myint typedef through
20813 the C scope. For that purpose, this function will add the myint typedef
20814 and the scope C through which its being accessed to a list of typedefs
20815 tied to the template S. That list will be walked at template instantiation
20816 time and access check performed on each typedefs it contains.
20817 Note that this particular code snippet should yield an error because
20818 myint is private to C. */
20821 append_type_to_template_for_access_check (tree templ
,
20824 location_t location
)
20826 qualified_typedef_usage_t
*iter
;
20829 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
20831 /* Make sure we don't append the type to the template twice. */
20832 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
20833 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
20836 append_type_to_template_for_access_check_1 (templ
, type_decl
,
20840 /* Set up the hash tables for template instantiations. */
20843 init_template_processing (void)
20845 decl_specializations
= htab_create_ggc (37,
20846 hash_specialization
,
20847 eq_specializations
,
20849 type_specializations
= htab_create_ggc (37,
20850 hash_specialization
,
20851 eq_specializations
,
20855 /* Print stats about the template hash tables for -fstats. */
20858 print_template_statistics (void)
20860 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
20861 "%f collisions\n", (long) htab_size (decl_specializations
),
20862 (long) htab_elements (decl_specializations
),
20863 htab_collisions (decl_specializations
));
20864 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
20865 "%f collisions\n", (long) htab_size (type_specializations
),
20866 (long) htab_elements (type_specializations
),
20867 htab_collisions (type_specializations
));
20870 #include "gt-cp-pt.h"